2 * thinkpad_acpi.c - ThinkPad ACPI Extras
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 0x020700
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
43 * 2005-01-16 0.9 use MODULE_VERSION
45 * fix parameter passing on module loading
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
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 */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
221 /****************************************************************************
225 #define TPACPI_NAME "thinkpad"
226 #define TPACPI_DESC "ThinkPad ACPI Extras"
227 #define TPACPI_FILE TPACPI_NAME "_acpi"
228 #define TPACPI_URL "http://ibm-acpi.sf.net/"
231 #define TPACPI_PROC_DIR "ibm"
232 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
233 #define TPACPI_DRVR_NAME TPACPI_FILE
234 #define TPACPI_DRVR_SHORTNAME "tpacpi"
235 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
237 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
238 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
240 #define TPACPI_MAX_ACPI_ARGS 3
242 /* Debugging printk groups */
243 #define TPACPI_DBG_ALL 0xffff
244 #define TPACPI_DBG_DISCLOSETASK 0x8000
245 #define TPACPI_DBG_INIT 0x0001
246 #define TPACPI_DBG_EXIT 0x0002
247 #define TPACPI_DBG_RFKILL 0x0004
248 #define TPACPI_DBG_HKEY 0x0008
249 #define TPACPI_DBG_FAN 0x0010
250 #define TPACPI_DBG_BRGHT 0x0020
251 #define TPACPI_DBG_MIXER 0x0040
253 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
258 /****************************************************************************
259 * Driver-wide structs and misc. variables
264 struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
268 void (*notify) (struct ibm_struct *, u32);
271 struct acpi_device *device;
277 int (*read) (struct seq_file *);
278 int (*write) (char *);
280 void (*resume) (void);
281 void (*suspend) (void);
282 void (*shutdown) (void);
284 struct list_head all_drivers;
286 struct tp_acpi_drv_struct *acpi;
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
297 struct ibm_init_struct {
300 int (*init) (struct ibm_init_struct *);
301 umode_t base_procfs_mode;
302 struct ibm_struct *data;
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD,
319 u32 bright_acpimode:1;
323 u32 fan_ctrl_status_undef:1;
325 u32 beep_needs_two_args:1;
326 u32 mixer_no_level_control:1;
327 u32 input_device_registered:1;
328 u32 platform_drv_registered:1;
329 u32 platform_drv_attrs_registered:1;
330 u32 sensors_pdrv_registered:1;
331 u32 sensors_pdrv_attrs_registered:1;
332 u32 sensors_pdev_attrs_registered:1;
333 u32 hotkey_poll_active:1;
334 u32 has_adaptive_kbd:1;
338 u16 hotkey_mask_ff:1;
339 u16 volume_ctrl_forbidden:1;
342 struct thinkpad_id_data {
343 unsigned int vendor; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
346 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
349 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
351 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
354 char *model_str; /* ThinkPad T43 */
355 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
357 static struct thinkpad_id_data thinkpad_id;
360 TPACPI_LIFE_INIT = 0,
365 static int experimental;
366 static u32 dbg_level;
368 static struct workqueue_struct *tpacpi_wq;
376 /* tpacpi LED class */
377 struct tpacpi_led_classdev {
378 struct led_classdev led_classdev;
382 /* brightness level capabilities */
383 static unsigned int bright_maxlvl; /* 0 = unknown */
385 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386 static int dbg_wlswemul;
387 static bool tpacpi_wlsw_emulstate;
388 static int dbg_bluetoothemul;
389 static bool tpacpi_bluetooth_emulstate;
390 static int dbg_wwanemul;
391 static bool tpacpi_wwan_emulstate;
392 static int dbg_uwbemul;
393 static bool tpacpi_uwb_emulstate;
397 /*************************************************************************
401 #define dbg_printk(a_dbg_level, format, arg...) \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
408 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
409 #define vdbg_printk dbg_printk
410 static const char *str_supported(int is_supported);
412 static inline const char *str_supported(int is_supported) { return ""; }
413 #define vdbg_printk(a_dbg_level, format, arg...) \
414 do { if (0) no_printk(format, ##arg); } while (0)
417 static void tpacpi_log_usertask(const char * const what)
419 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
420 what, task_tgid_vnr(current));
423 #define tpacpi_disclose_usertask(what, format, arg...) \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
433 * Quirk handling helpers
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
438 * We use values well outside that range as specials.
441 #define TPACPI_MATCH_ANY 0xffffU
442 #define TPACPI_MATCH_UNKNOWN 0U
444 /* TPID('1', 'Y') == 0x5931 */
445 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
447 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
453 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
459 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
465 struct tpacpi_quirk {
469 unsigned long quirks;
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
483 * The match criteria is: vendor, ec and bios much match.
485 static unsigned long __init tpacpi_check_quirks(
486 const struct tpacpi_quirk *qlist,
487 unsigned int qlist_size)
490 if ((qlist->vendor == thinkpad_id.vendor ||
491 qlist->vendor == TPACPI_MATCH_ANY) &&
492 (qlist->bios == thinkpad_id.bios_model ||
493 qlist->bios == TPACPI_MATCH_ANY) &&
494 (qlist->ec == thinkpad_id.ec_model ||
495 qlist->ec == TPACPI_MATCH_ANY))
496 return qlist->quirks;
504 static inline bool __pure __init tpacpi_is_lenovo(void)
506 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
509 static inline bool __pure __init tpacpi_is_ibm(void)
511 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
514 /****************************************************************************
515 ****************************************************************************
517 * ACPI Helpers and device model
519 ****************************************************************************
520 ****************************************************************************/
522 /*************************************************************************
526 static acpi_handle root_handle;
527 static acpi_handle ec_handle;
529 #define TPACPI_HANDLE(object, parent, paths...) \
530 static acpi_handle object##_handle; \
531 static const acpi_handle * const object##_parent __initconst = \
533 static char *object##_paths[] __initdata = { paths }
535 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
536 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
538 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
544 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
549 /*************************************************************************
553 static int acpi_evalf(acpi_handle handle,
554 int *res, char *method, char *fmt, ...)
557 struct acpi_object_list params;
558 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
559 struct acpi_buffer result, *resultp;
560 union acpi_object out_obj;
568 pr_err("acpi_evalf() called with empty format\n");
581 params.pointer = &in_objs[0];
588 in_objs[params.count].integer.value = va_arg(ap, int);
589 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
591 /* add more types as needed */
593 pr_err("acpi_evalf() called with invalid format character '%c'\n",
601 if (res_type != 'v') {
602 result.length = sizeof(out_obj);
603 result.pointer = &out_obj;
608 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
612 success = (status == AE_OK &&
613 out_obj.type == ACPI_TYPE_INTEGER);
615 *res = out_obj.integer.value;
618 success = status == AE_OK;
620 /* add more types as needed */
622 pr_err("acpi_evalf() called with invalid format character '%c'\n",
627 if (!success && !quiet)
628 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
629 method, fmt0, acpi_format_exception(status));
634 static int acpi_ec_read(int i, u8 *p)
639 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
643 if (ec_read(i, p) < 0)
650 static int acpi_ec_write(int i, u8 v)
653 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
656 if (ec_write(i, v) < 0)
663 static int issue_thinkpad_cmos_command(int cmos_cmd)
668 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
674 /*************************************************************************
678 #define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
680 object##_paths, ARRAY_SIZE(object##_paths))
682 static void __init drv_acpi_handle_init(const char *name,
683 acpi_handle *handle, const acpi_handle parent,
684 char **paths, const int num_paths)
689 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
692 for (i = 0; i < num_paths; i++) {
693 status = acpi_get_handle(parent, paths[i], handle);
694 if (ACPI_SUCCESS(status)) {
695 dbg_printk(TPACPI_DBG_INIT,
696 "Found ACPI handle %s for %s\n",
702 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
707 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
708 u32 level, void *context, void **return_value)
710 struct acpi_device *dev;
711 if (!strcmp(context, "video")) {
712 if (acpi_bus_get_device(handle, &dev))
714 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
718 *(acpi_handle *)return_value = handle;
720 return AE_CTRL_TERMINATE;
723 static void __init tpacpi_acpi_handle_locate(const char *name,
728 acpi_handle device_found;
730 BUG_ON(!name || !handle);
731 vdbg_printk(TPACPI_DBG_INIT,
732 "trying to locate ACPI handle for %s, using HID %s\n",
733 name, hid ? hid : "NULL");
735 memset(&device_found, 0, sizeof(device_found));
736 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
737 (void *)name, &device_found);
741 if (ACPI_SUCCESS(status)) {
742 *handle = device_found;
743 dbg_printk(TPACPI_DBG_INIT,
744 "Found ACPI handle for %s\n", name);
746 vdbg_printk(TPACPI_DBG_INIT,
747 "Could not locate an ACPI handle for %s: %s\n",
748 name, acpi_format_exception(status));
752 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
754 struct ibm_struct *ibm = data;
756 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
759 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
762 ibm->acpi->notify(ibm, event);
765 static int __init setup_acpi_notify(struct ibm_struct *ibm)
772 if (!*ibm->acpi->handle)
775 vdbg_printk(TPACPI_DBG_INIT,
776 "setting up ACPI notify for %s\n", ibm->name);
778 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
780 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
784 ibm->acpi->device->driver_data = ibm;
785 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
786 TPACPI_ACPI_EVENT_PREFIX,
789 status = acpi_install_notify_handler(*ibm->acpi->handle,
790 ibm->acpi->type, dispatch_acpi_notify, ibm);
791 if (ACPI_FAILURE(status)) {
792 if (status == AE_ALREADY_EXISTS) {
793 pr_notice("another device driver is already handling %s events\n",
796 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
797 ibm->name, acpi_format_exception(status));
801 ibm->flags.acpi_notify_installed = 1;
805 static int __init tpacpi_device_add(struct acpi_device *device)
810 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
814 dbg_printk(TPACPI_DBG_INIT,
815 "registering %s as an ACPI driver\n", ibm->name);
819 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
820 if (!ibm->acpi->driver) {
821 pr_err("failed to allocate memory for ibm->acpi->driver\n");
825 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
826 ibm->acpi->driver->ids = ibm->acpi->hid;
828 ibm->acpi->driver->ops.add = &tpacpi_device_add;
830 rc = acpi_bus_register_driver(ibm->acpi->driver);
832 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
834 kfree(ibm->acpi->driver);
835 ibm->acpi->driver = NULL;
837 ibm->flags.acpi_driver_registered = 1;
843 /****************************************************************************
844 ****************************************************************************
848 ****************************************************************************
849 ****************************************************************************/
851 static int dispatch_proc_show(struct seq_file *m, void *v)
853 struct ibm_struct *ibm = m->private;
855 if (!ibm || !ibm->read)
860 static int dispatch_proc_open(struct inode *inode, struct file *file)
862 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
865 static ssize_t dispatch_proc_write(struct file *file,
866 const char __user *userbuf,
867 size_t count, loff_t *pos)
869 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
873 if (!ibm || !ibm->write)
875 if (count > PAGE_SIZE - 2)
878 kernbuf = kmalloc(count + 2, GFP_KERNEL);
882 if (copy_from_user(kernbuf, userbuf, count)) {
888 strcat(kernbuf, ",");
889 ret = ibm->write(kernbuf);
898 static const struct file_operations dispatch_proc_fops = {
899 .owner = THIS_MODULE,
900 .open = dispatch_proc_open,
903 .release = single_release,
904 .write = dispatch_proc_write,
907 static char *next_cmd(char **cmds)
912 while ((end = strchr(start, ',')) && end == start)
924 /****************************************************************************
925 ****************************************************************************
927 * Device model: input, hwmon and platform
929 ****************************************************************************
930 ****************************************************************************/
932 static struct platform_device *tpacpi_pdev;
933 static struct platform_device *tpacpi_sensors_pdev;
934 static struct device *tpacpi_hwmon;
935 static struct input_dev *tpacpi_inputdev;
936 static struct mutex tpacpi_inputdev_send_mutex;
937 static LIST_HEAD(tpacpi_all_drivers);
939 #ifdef CONFIG_PM_SLEEP
940 static int tpacpi_suspend_handler(struct device *dev)
942 struct ibm_struct *ibm, *itmp;
944 list_for_each_entry_safe(ibm, itmp,
954 static int tpacpi_resume_handler(struct device *dev)
956 struct ibm_struct *ibm, *itmp;
958 list_for_each_entry_safe(ibm, itmp,
969 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
970 tpacpi_suspend_handler, tpacpi_resume_handler);
972 static void tpacpi_shutdown_handler(struct platform_device *pdev)
974 struct ibm_struct *ibm, *itmp;
976 list_for_each_entry_safe(ibm, itmp,
984 static struct platform_driver tpacpi_pdriver = {
986 .name = TPACPI_DRVR_NAME,
989 .shutdown = tpacpi_shutdown_handler,
992 static struct platform_driver tpacpi_hwmon_pdriver = {
994 .name = TPACPI_HWMON_DRVR_NAME,
998 /*************************************************************************
999 * sysfs support helpers
1002 struct attribute_set {
1003 unsigned int members, max_members;
1004 struct attribute_group group;
1007 struct attribute_set_obj {
1008 struct attribute_set s;
1009 struct attribute *a;
1010 } __attribute__((packed));
1012 static struct attribute_set *create_attr_set(unsigned int max_members,
1015 struct attribute_set_obj *sobj;
1017 if (max_members == 0)
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1022 max_members * sizeof(struct attribute *),
1026 sobj->s.max_members = max_members;
1027 sobj->s.group.attrs = &sobj->a;
1028 sobj->s.group.name = name;
1033 #define destroy_attr_set(_set) \
1036 /* not multi-threaded safe, use it in a single thread per set */
1037 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1042 if (s->members >= s->max_members)
1045 s->group.attrs[s->members] = attr;
1051 static int add_many_to_attr_set(struct attribute_set *s,
1052 struct attribute **attr,
1057 for (i = 0; i < count; i++) {
1058 res = add_to_attr_set(s, attr[i]);
1066 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1068 sysfs_remove_group(kobj, &s->group);
1069 destroy_attr_set(s);
1072 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1075 static int parse_strtoul(const char *buf,
1076 unsigned long max, unsigned long *value)
1080 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1081 endp = skip_spaces(endp);
1082 if (*endp || *value > max)
1088 static void tpacpi_disable_brightness_delay(void)
1090 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1091 pr_notice("ACPI backlight control delay disabled\n");
1094 static void printk_deprecated_attribute(const char * const what,
1095 const char * const details)
1097 tpacpi_log_usertask("deprecated sysfs attribute");
1098 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1102 /*************************************************************************
1103 * rfkill and radio control support helpers
1107 * ThinkPad-ACPI firmware handling model:
1109 * WLSW (master wireless switch) is event-driven, and is common to all
1110 * firmware-controlled radios. It cannot be controlled, just monitored,
1111 * as expected. It overrides all radio state in firmware
1113 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1114 * (TODO: verify how WLSW interacts with the returned radio state).
1116 * The only time there are shadow radio state changes, is when
1117 * masked-off hotkeys are used.
1121 * Internal driver API for radio state:
1123 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1124 * bool: true means radio blocked (off)
1126 enum tpacpi_rfkill_state {
1127 TPACPI_RFK_RADIO_OFF = 0,
1131 /* rfkill switches */
1132 enum tpacpi_rfk_id {
1133 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1134 TPACPI_RFK_WWAN_SW_ID,
1135 TPACPI_RFK_UWB_SW_ID,
1139 static const char *tpacpi_rfkill_names[] = {
1140 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1141 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1142 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1143 [TPACPI_RFK_SW_MAX] = NULL
1146 /* ThinkPad-ACPI rfkill subdriver */
1148 struct rfkill *rfkill;
1149 enum tpacpi_rfk_id id;
1150 const struct tpacpi_rfk_ops *ops;
1153 struct tpacpi_rfk_ops {
1154 /* firmware interface */
1155 int (*get_status)(void);
1156 int (*set_status)(const enum tpacpi_rfkill_state);
1159 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1161 /* Query FW and update rfkill sw state for a given rfkill switch */
1162 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1169 status = (tp_rfk->ops->get_status)();
1173 rfkill_set_sw_state(tp_rfk->rfkill,
1174 (status == TPACPI_RFK_RADIO_OFF));
1179 /* Query FW and update rfkill sw state for all rfkill switches */
1180 static void tpacpi_rfk_update_swstate_all(void)
1184 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1185 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1189 * Sync the HW-blocking state of all rfkill switches,
1190 * do notice it causes the rfkill core to schedule uevents
1192 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1195 struct tpacpi_rfk *tp_rfk;
1197 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1198 tp_rfk = tpacpi_rfkill_switches[i];
1200 if (rfkill_set_hw_state(tp_rfk->rfkill,
1202 /* ignore -- we track sw block */
1208 /* Call to get the WLSW state from the firmware */
1209 static int hotkey_get_wlsw(void);
1211 /* Call to query WLSW state and update all rfkill switches */
1212 static bool tpacpi_rfk_check_hwblock_state(void)
1214 int res = hotkey_get_wlsw();
1217 /* When unknown or unsupported, we have to assume it is unblocked */
1221 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1222 tpacpi_rfk_update_hwblock_state(hw_blocked);
1227 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1229 struct tpacpi_rfk *tp_rfk = data;
1232 dbg_printk(TPACPI_DBG_RFKILL,
1233 "request to change radio state to %s\n",
1234 blocked ? "blocked" : "unblocked");
1236 /* try to set radio state */
1237 res = (tp_rfk->ops->set_status)(blocked ?
1238 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1240 /* and update the rfkill core with whatever the FW really did */
1241 tpacpi_rfk_update_swstate(tp_rfk);
1243 return (res < 0) ? res : 0;
1246 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1247 .set_block = tpacpi_rfk_hook_set_block,
1250 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1251 const struct tpacpi_rfk_ops *tp_rfkops,
1252 const enum rfkill_type rfktype,
1254 const bool set_default)
1256 struct tpacpi_rfk *atp_rfk;
1258 bool sw_state = false;
1262 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1264 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1266 atp_rfk->rfkill = rfkill_alloc(name,
1269 &tpacpi_rfk_rfkill_ops,
1271 if (!atp_rfk || !atp_rfk->rfkill) {
1272 pr_err("failed to allocate memory for rfkill class\n");
1278 atp_rfk->ops = tp_rfkops;
1280 sw_status = (tp_rfkops->get_status)();
1281 if (sw_status < 0) {
1282 pr_err("failed to read initial state for %s, error %d\n",
1285 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1287 /* try to keep the initial state, since we ask the
1288 * firmware to preserve it across S5 in NVRAM */
1289 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1292 hw_state = tpacpi_rfk_check_hwblock_state();
1293 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1295 res = rfkill_register(atp_rfk->rfkill);
1297 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1298 rfkill_destroy(atp_rfk->rfkill);
1303 tpacpi_rfkill_switches[id] = atp_rfk;
1305 pr_info("rfkill switch %s: radio is %sblocked\n",
1306 name, (sw_state || hw_state) ? "" : "un");
1310 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312 struct tpacpi_rfk *tp_rfk;
1314 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316 tp_rfk = tpacpi_rfkill_switches[id];
1318 rfkill_unregister(tp_rfk->rfkill);
1319 rfkill_destroy(tp_rfk->rfkill);
1320 tpacpi_rfkill_switches[id] = NULL;
1325 static void printk_deprecated_rfkill_attribute(const char * const what)
1327 printk_deprecated_attribute(what,
1328 "Please switch to generic rfkill before year 2010");
1331 /* sysfs <radio> enable ------------------------------------------------ */
1332 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1333 struct device_attribute *attr,
1338 printk_deprecated_rfkill_attribute(attr->attr.name);
1340 /* This is in the ABI... */
1341 if (tpacpi_rfk_check_hwblock_state()) {
1342 status = TPACPI_RFK_RADIO_OFF;
1344 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1349 return snprintf(buf, PAGE_SIZE, "%d\n",
1350 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1353 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 const char *buf, size_t count)
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1362 if (parse_strtoul(buf, 1, &t))
1365 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state() && !!t)
1371 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1373 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375 return (res < 0) ? res : count;
1378 /* procfs -------------------------------------------------------------- */
1379 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1381 if (id >= TPACPI_RFK_SW_MAX)
1382 seq_printf(m, "status:\t\tnot supported\n");
1386 /* This is in the ABI... */
1387 if (tpacpi_rfk_check_hwblock_state()) {
1388 status = TPACPI_RFK_RADIO_OFF;
1390 status = tpacpi_rfk_update_swstate(
1391 tpacpi_rfkill_switches[id]);
1396 seq_printf(m, "status:\t\t%s\n",
1397 (status == TPACPI_RFK_RADIO_ON) ?
1398 "enabled" : "disabled");
1399 seq_printf(m, "commands:\tenable, disable\n");
1405 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1411 if (id >= TPACPI_RFK_SW_MAX)
1414 while ((cmd = next_cmd(&buf))) {
1415 if (strlencmp(cmd, "enable") == 0)
1416 status = TPACPI_RFK_RADIO_ON;
1417 else if (strlencmp(cmd, "disable") == 0)
1418 status = TPACPI_RFK_RADIO_OFF;
1424 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1425 (status == TPACPI_RFK_RADIO_ON) ?
1426 "enable" : "disable",
1427 tpacpi_rfkill_names[id]);
1428 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1429 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1435 /*************************************************************************
1436 * thinkpad-acpi driver attributes
1439 /* interface_version --------------------------------------------------- */
1440 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1442 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1444 static DRIVER_ATTR_RO(interface_version);
1446 /* debug_level --------------------------------------------------------- */
1447 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1449 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1452 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1457 if (parse_strtoul(buf, 0xffff, &t))
1464 static DRIVER_ATTR_RW(debug_level);
1466 /* version ------------------------------------------------------------- */
1467 static ssize_t version_show(struct device_driver *drv, char *buf)
1469 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1470 TPACPI_DESC, TPACPI_VERSION);
1472 static DRIVER_ATTR_RO(version);
1474 /* --------------------------------------------------------------------- */
1476 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1478 /* wlsw_emulstate ------------------------------------------------------ */
1479 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1481 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1484 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1489 if (parse_strtoul(buf, 1, &t))
1492 if (tpacpi_wlsw_emulstate != !!t) {
1493 tpacpi_wlsw_emulstate = !!t;
1494 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1499 static DRIVER_ATTR_RW(wlsw_emulstate);
1501 /* bluetooth_emulstate ------------------------------------------------- */
1502 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1504 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1507 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1508 const char *buf, size_t count)
1512 if (parse_strtoul(buf, 1, &t))
1515 tpacpi_bluetooth_emulstate = !!t;
1519 static DRIVER_ATTR_RW(bluetooth_emulstate);
1521 /* wwan_emulstate ------------------------------------------------- */
1522 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1524 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1527 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1532 if (parse_strtoul(buf, 1, &t))
1535 tpacpi_wwan_emulstate = !!t;
1539 static DRIVER_ATTR_RW(wwan_emulstate);
1541 /* uwb_emulstate ------------------------------------------------- */
1542 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1547 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1552 if (parse_strtoul(buf, 1, &t))
1555 tpacpi_uwb_emulstate = !!t;
1559 static DRIVER_ATTR_RW(uwb_emulstate);
1562 /* --------------------------------------------------------------------- */
1564 static struct driver_attribute *tpacpi_driver_attributes[] = {
1565 &driver_attr_debug_level, &driver_attr_version,
1566 &driver_attr_interface_version,
1569 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1575 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1576 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1580 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1581 if (!res && dbg_wlswemul)
1582 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1583 if (!res && dbg_bluetoothemul)
1584 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1585 if (!res && dbg_wwanemul)
1586 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1587 if (!res && dbg_uwbemul)
1588 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1594 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1598 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1599 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1601 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1602 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1603 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1604 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1605 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1609 /*************************************************************************
1614 * Table of recommended minimum BIOS versions
1616 * Reasons for listing:
1617 * 1. Stable BIOS, listed because the unknown amount of
1618 * bugs and bad ACPI behaviour on older versions
1620 * 2. BIOS or EC fw with known bugs that trigger on Linux
1622 * 3. BIOS with known reduced functionality in older versions
1624 * We recommend the latest BIOS and EC version.
1625 * We only support the latest BIOS and EC fw version as a rule.
1627 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1628 * Information from users in ThinkWiki
1630 * WARNING: we use this table also to detect that the machine is
1631 * a ThinkPad in some cases, so don't remove entries lightly.
1634 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1635 { .vendor = (__v), \
1636 .bios = TPID(__id1, __id2), \
1637 .ec = TPACPI_MATCH_ANY, \
1638 .quirks = TPACPI_MATCH_ANY << 16 \
1639 | (__bv1) << 8 | (__bv2) }
1641 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1642 __eid, __ev1, __ev2) \
1643 { .vendor = (__v), \
1644 .bios = TPID(__bid1, __bid2), \
1646 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1647 | (__bv1) << 8 | (__bv2) }
1649 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1650 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1652 /* Outdated IBM BIOSes often lack the EC id string */
1653 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1654 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1655 __bv1, __bv2, TPID(__id1, __id2), \
1657 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1658 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1661 /* Outdated IBM BIOSes often lack the EC id string */
1662 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1663 __eid1, __eid2, __ev1, __ev2) \
1664 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1665 __bv1, __bv2, TPID(__eid1, __eid2), \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1668 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1671 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1672 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1674 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1676 __bv1, __bv2, TPID(__id1, __id2), \
1679 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1680 __eid1, __eid2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1682 __bv1, __bv2, TPID(__eid1, __eid2), \
1685 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1686 /* Numeric models ------------------ */
1687 /* FW MODEL BIOS VERS */
1688 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1689 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1690 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1691 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1692 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1693 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1694 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1695 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1696 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1698 /* A-series ------------------------- */
1699 /* FW MODEL BIOS VERS EC VERS */
1700 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1701 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1702 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1703 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1704 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1705 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1706 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1707 TPV_QI0('1', '3', '2', '0'), /* A22m */
1708 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1709 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1710 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1712 /* G-series ------------------------- */
1713 /* FW MODEL BIOS VERS */
1714 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1715 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1717 /* R-series, T-series --------------- */
1718 /* FW MODEL BIOS VERS EC VERS */
1719 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1720 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1721 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1722 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1723 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1724 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1725 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1726 T40/p, T41/p, T42/p (1) */
1727 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1728 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1729 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1730 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1732 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1733 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1734 TPV_QI0('1', '6', '3', '2'), /* T22 */
1735 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1736 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1737 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1739 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1740 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1741 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1743 /* BIOS FW BIOS VERS EC FW EC VERS */
1744 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1745 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1747 /* X-series ------------------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1750 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1751 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1752 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1753 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1754 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1755 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1757 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1758 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1760 /* (0) - older versions lack DMI EC fw string and functionality */
1761 /* (1) - older versions known to lack functionality */
1772 static void __init tpacpi_check_outdated_fw(void)
1774 unsigned long fwvers;
1775 u16 ec_version, bios_version;
1777 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1778 ARRAY_SIZE(tpacpi_bios_version_qtable));
1783 bios_version = fwvers & 0xffffU;
1784 ec_version = (fwvers >> 16) & 0xffffU;
1786 /* note that unknown versions are set to 0x0000 and we use that */
1787 if ((bios_version > thinkpad_id.bios_release) ||
1788 (ec_version > thinkpad_id.ec_release &&
1789 ec_version != TPACPI_MATCH_ANY)) {
1791 * The changelogs would let us track down the exact
1792 * reason, but it is just too much of a pain to track
1793 * it. We only list BIOSes that are either really
1794 * broken, or really stable to begin with, so it is
1795 * best if the user upgrades the firmware anyway.
1797 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1798 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1802 static bool __init tpacpi_is_fw_known(void)
1804 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1805 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1808 /****************************************************************************
1809 ****************************************************************************
1813 ****************************************************************************
1814 ****************************************************************************/
1816 /*************************************************************************
1817 * thinkpad-acpi metadata subdriver
1820 static int thinkpad_acpi_driver_read(struct seq_file *m)
1822 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1823 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1827 static struct ibm_struct thinkpad_acpi_driver_data = {
1829 .read = thinkpad_acpi_driver_read,
1832 /*************************************************************************
1837 * ThinkPad firmware event model
1839 * The ThinkPad firmware has two main event interfaces: normal ACPI
1840 * notifications (which follow the ACPI standard), and a private event
1843 * The private event interface also issues events for the hotkeys. As
1844 * the driver gained features, the event handling code ended up being
1845 * built around the hotkey subdriver. This will need to be refactored
1846 * to a more formal event API eventually.
1848 * Some "hotkeys" are actually supposed to be used as event reports,
1849 * such as "brightness has changed", "volume has changed", depending on
1850 * the ThinkPad model and how the firmware is operating.
1852 * Unlike other classes, hotkey-class events have mask/unmask control on
1853 * non-ancient firmware. However, how it behaves changes a lot with the
1854 * firmware model and version.
1857 enum { /* hot key scan codes (derived from ACPI DSDT) */
1858 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1859 TP_ACPI_HOTKEYSCAN_FNF2,
1860 TP_ACPI_HOTKEYSCAN_FNF3,
1861 TP_ACPI_HOTKEYSCAN_FNF4,
1862 TP_ACPI_HOTKEYSCAN_FNF5,
1863 TP_ACPI_HOTKEYSCAN_FNF6,
1864 TP_ACPI_HOTKEYSCAN_FNF7,
1865 TP_ACPI_HOTKEYSCAN_FNF8,
1866 TP_ACPI_HOTKEYSCAN_FNF9,
1867 TP_ACPI_HOTKEYSCAN_FNF10,
1868 TP_ACPI_HOTKEYSCAN_FNF11,
1869 TP_ACPI_HOTKEYSCAN_FNF12,
1870 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1871 TP_ACPI_HOTKEYSCAN_FNINSERT,
1872 TP_ACPI_HOTKEYSCAN_FNDELETE,
1873 TP_ACPI_HOTKEYSCAN_FNHOME,
1874 TP_ACPI_HOTKEYSCAN_FNEND,
1875 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1876 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1877 TP_ACPI_HOTKEYSCAN_FNSPACE,
1878 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1879 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1880 TP_ACPI_HOTKEYSCAN_MUTE,
1881 TP_ACPI_HOTKEYSCAN_THINKPAD,
1882 TP_ACPI_HOTKEYSCAN_UNK1,
1883 TP_ACPI_HOTKEYSCAN_UNK2,
1884 TP_ACPI_HOTKEYSCAN_UNK3,
1885 TP_ACPI_HOTKEYSCAN_UNK4,
1886 TP_ACPI_HOTKEYSCAN_UNK5,
1887 TP_ACPI_HOTKEYSCAN_UNK6,
1888 TP_ACPI_HOTKEYSCAN_UNK7,
1889 TP_ACPI_HOTKEYSCAN_UNK8,
1891 /* Adaptive keyboard keycodes */
1892 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1893 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1894 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1895 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1896 TP_ACPI_HOTKEYSCAN_CLOUD,
1897 TP_ACPI_HOTKEYSCAN_UNK9,
1898 TP_ACPI_HOTKEYSCAN_VOICE,
1899 TP_ACPI_HOTKEYSCAN_UNK10,
1900 TP_ACPI_HOTKEYSCAN_GESTURES,
1901 TP_ACPI_HOTKEYSCAN_UNK11,
1902 TP_ACPI_HOTKEYSCAN_UNK12,
1903 TP_ACPI_HOTKEYSCAN_UNK13,
1904 TP_ACPI_HOTKEYSCAN_CONFIG,
1905 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1906 TP_ACPI_HOTKEYSCAN_RELOAD,
1907 TP_ACPI_HOTKEYSCAN_BACK,
1908 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1909 TP_ACPI_HOTKEYSCAN_MIC_UP,
1910 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1911 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1912 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1914 /* Lenovo extended keymap, starting at 0x1300 */
1915 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1916 /* first new observed key (star, favorites) is 0x1311 */
1917 TP_ACPI_HOTKEYSCAN_STAR = 69,
1918 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1919 TP_ACPI_HOTKEYSCAN_UNK25,
1920 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1921 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1923 /* Hotkey keymap size */
1924 TPACPI_HOTKEY_MAP_LEN
1927 enum { /* Keys/events available through NVRAM polling */
1928 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1929 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1932 enum { /* Positions of some of the keys in hotkey masks */
1933 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1934 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1935 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1936 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1937 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1938 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1939 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1940 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1941 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1942 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1943 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1946 enum { /* NVRAM to ACPI HKEY group map */
1947 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1948 TP_ACPI_HKEY_ZOOM_MASK |
1949 TP_ACPI_HKEY_DISPSWTCH_MASK |
1950 TP_ACPI_HKEY_HIBERNATE_MASK,
1951 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1952 TP_ACPI_HKEY_BRGHTDWN_MASK,
1953 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1954 TP_ACPI_HKEY_VOLDWN_MASK |
1955 TP_ACPI_HKEY_MUTE_MASK,
1958 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1959 struct tp_nvram_state {
1960 u16 thinkpad_toggle:1;
1962 u16 display_toggle:1;
1963 u16 thinklight_toggle:1;
1964 u16 hibernate_toggle:1;
1965 u16 displayexp_toggle:1;
1966 u16 display_state:1;
1967 u16 brightness_toggle:1;
1968 u16 volume_toggle:1;
1971 u8 brightness_level;
1975 /* kthread for the hotkey poller */
1976 static struct task_struct *tpacpi_hotkey_task;
1979 * Acquire mutex to write poller control variables as an
1982 * Increment hotkey_config_change when changing them if you
1983 * want the kthread to forget old state.
1985 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1987 static struct mutex hotkey_thread_data_mutex;
1988 static unsigned int hotkey_config_change;
1991 * hotkey poller control variables
1993 * Must be atomic or readers will also need to acquire mutex
1995 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1996 * should be used only when the changes need to be taken as
1997 * a block, OR when one needs to force the kthread to forget
2000 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2001 static unsigned int hotkey_poll_freq = 10; /* Hz */
2003 #define HOTKEY_CONFIG_CRITICAL_START \
2005 mutex_lock(&hotkey_thread_data_mutex); \
2006 hotkey_config_change++; \
2008 #define HOTKEY_CONFIG_CRITICAL_END \
2009 mutex_unlock(&hotkey_thread_data_mutex);
2011 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2013 #define hotkey_source_mask 0U
2014 #define HOTKEY_CONFIG_CRITICAL_START
2015 #define HOTKEY_CONFIG_CRITICAL_END
2017 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2019 static struct mutex hotkey_mutex;
2021 static enum { /* Reasons for waking up */
2022 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2023 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2024 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2025 } hotkey_wakeup_reason;
2027 static int hotkey_autosleep_ack;
2029 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2030 static u32 hotkey_all_mask; /* all events supported in fw */
2031 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2032 static u32 hotkey_reserved_mask; /* events better left disabled */
2033 static u32 hotkey_driver_mask; /* events needed by the driver */
2034 static u32 hotkey_user_mask; /* events visible to userspace */
2035 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2037 static u16 *hotkey_keycode_map;
2039 static struct attribute_set *hotkey_dev_attributes;
2041 static void tpacpi_driver_event(const unsigned int hkey_event);
2042 static void hotkey_driver_event(const unsigned int scancode);
2043 static void hotkey_poll_setup(const bool may_warn);
2045 /* HKEY.MHKG() return bits */
2046 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2047 /* ThinkPad X1 Yoga (2016) */
2048 #define TP_EC_CMMD_TABLET_MODE 0x6
2050 static int hotkey_get_wlsw(void)
2054 if (!tp_features.hotkey_wlsw)
2057 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2059 return (tpacpi_wlsw_emulstate) ?
2060 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2063 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2066 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2069 static int hotkey_get_tablet_mode(int *status)
2073 switch (tp_features.hotkey_tablet) {
2074 case TP_HOTKEY_TABLET_USES_MHKG:
2075 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2078 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2080 case TP_HOTKEY_TABLET_USES_CMMD:
2081 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2084 *status = (s == TP_EC_CMMD_TABLET_MODE);
2094 * Reads current event mask from firmware, and updates
2095 * hotkey_acpi_mask accordingly. Also resets any bits
2096 * from hotkey_user_mask that are unavailable to be
2097 * delivered (shadow requirement of the userspace ABI).
2099 * Call with hotkey_mutex held
2101 static int hotkey_mask_get(void)
2103 if (tp_features.hotkey_mask) {
2106 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2109 hotkey_acpi_mask = m;
2111 /* no mask support doesn't mean no event support... */
2112 hotkey_acpi_mask = hotkey_all_mask;
2115 /* sync userspace-visible mask */
2116 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2121 static void hotkey_mask_warn_incomplete_mask(void)
2123 /* log only what the user can fix... */
2124 const u32 wantedmask = hotkey_driver_mask &
2125 ~(hotkey_acpi_mask | hotkey_source_mask) &
2126 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2129 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2133 * Set the firmware mask when supported
2135 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2137 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2139 * Call with hotkey_mutex held
2141 static int hotkey_mask_set(u32 mask)
2146 const u32 fwmask = mask & ~hotkey_source_mask;
2148 if (tp_features.hotkey_mask) {
2149 for (i = 0; i < 32; i++) {
2150 if (!acpi_evalf(hkey_handle,
2151 NULL, "MHKM", "vdd", i + 1,
2152 !!(mask & (1 << i)))) {
2160 * We *must* make an inconditional call to hotkey_mask_get to
2161 * refresh hotkey_acpi_mask and update hotkey_user_mask
2163 * Take the opportunity to also log when we cannot _enable_
2166 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2167 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2168 fwmask, hotkey_acpi_mask);
2171 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2172 hotkey_mask_warn_incomplete_mask();
2178 * Sets hotkey_user_mask and tries to set the firmware mask
2180 * Call with hotkey_mutex held
2182 static int hotkey_user_mask_set(const u32 mask)
2186 /* Give people a chance to notice they are doing something that
2187 * is bound to go boom on their users sooner or later */
2188 if (!tp_warned.hotkey_mask_ff &&
2189 (mask == 0xffff || mask == 0xffffff ||
2190 mask == 0xffffffff)) {
2191 tp_warned.hotkey_mask_ff = 1;
2192 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2194 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2197 /* Try to enable what the user asked for, plus whatever we need.
2198 * this syncs everything but won't enable bits in hotkey_user_mask */
2199 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2201 /* Enable the available bits in hotkey_user_mask */
2202 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2208 * Sets the driver hotkey mask.
2210 * Can be called even if the hotkey subdriver is inactive
2212 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2216 /* Do the right thing if hotkey_init has not been called yet */
2217 if (!tp_features.hotkey) {
2218 hotkey_driver_mask = mask;
2222 mutex_lock(&hotkey_mutex);
2224 HOTKEY_CONFIG_CRITICAL_START
2225 hotkey_driver_mask = mask;
2226 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2227 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2229 HOTKEY_CONFIG_CRITICAL_END
2231 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2232 ~hotkey_source_mask);
2233 hotkey_poll_setup(true);
2235 mutex_unlock(&hotkey_mutex);
2240 static int hotkey_status_get(int *status)
2242 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2248 static int hotkey_status_set(bool enable)
2250 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2256 static void tpacpi_input_send_tabletsw(void)
2260 if (tp_features.hotkey_tablet &&
2261 !hotkey_get_tablet_mode(&state)) {
2262 mutex_lock(&tpacpi_inputdev_send_mutex);
2264 input_report_switch(tpacpi_inputdev,
2265 SW_TABLET_MODE, !!state);
2266 input_sync(tpacpi_inputdev);
2268 mutex_unlock(&tpacpi_inputdev_send_mutex);
2272 /* Do NOT call without validating scancode first */
2273 static void tpacpi_input_send_key(const unsigned int scancode)
2275 const unsigned int keycode = hotkey_keycode_map[scancode];
2277 if (keycode != KEY_RESERVED) {
2278 mutex_lock(&tpacpi_inputdev_send_mutex);
2280 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2281 input_report_key(tpacpi_inputdev, keycode, 1);
2282 input_sync(tpacpi_inputdev);
2284 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2285 input_report_key(tpacpi_inputdev, keycode, 0);
2286 input_sync(tpacpi_inputdev);
2288 mutex_unlock(&tpacpi_inputdev_send_mutex);
2292 /* Do NOT call without validating scancode first */
2293 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2295 hotkey_driver_event(scancode);
2296 if (hotkey_user_mask & (1 << scancode))
2297 tpacpi_input_send_key(scancode);
2300 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2301 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2303 /* Do NOT call without validating scancode first */
2304 static void tpacpi_hotkey_send_key(unsigned int scancode)
2306 tpacpi_input_send_key_masked(scancode);
2309 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2313 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2315 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2316 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2317 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2318 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2320 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2321 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2322 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2324 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2326 n->displayexp_toggle =
2327 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2329 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2331 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2332 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2333 n->brightness_toggle =
2334 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2336 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2337 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2338 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2339 >> TP_NVRAM_POS_LEVEL_VOLUME;
2340 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2341 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2345 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2347 if ((event_mask & (1 << __scancode)) && \
2348 oldn->__member != newn->__member) \
2349 tpacpi_hotkey_send_key(__scancode); \
2352 #define TPACPI_MAY_SEND_KEY(__scancode) \
2354 if (event_mask & (1 << __scancode)) \
2355 tpacpi_hotkey_send_key(__scancode); \
2358 static void issue_volchange(const unsigned int oldvol,
2359 const unsigned int newvol,
2360 const u32 event_mask)
2362 unsigned int i = oldvol;
2364 while (i > newvol) {
2365 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2368 while (i < newvol) {
2369 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2374 static void issue_brightnesschange(const unsigned int oldbrt,
2375 const unsigned int newbrt,
2376 const u32 event_mask)
2378 unsigned int i = oldbrt;
2380 while (i > newbrt) {
2381 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2384 while (i < newbrt) {
2385 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2390 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2391 struct tp_nvram_state *newn,
2392 const u32 event_mask)
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2407 * This code is supposed to duplicate the IBM firmware behaviour:
2408 * - Pressing MUTE issues mute hotkey message, even when already mute
2409 * - Pressing Volume up/down issues volume up/down hotkey messages,
2410 * even when already at maximum or minimum volume
2411 * - The act of unmuting issues volume up/down notification,
2412 * depending which key was used to unmute
2414 * We are constrained to what the NVRAM can tell us, which is not much
2415 * and certainly not enough if more than one volume hotkey was pressed
2416 * since the last poll cycle.
2418 * Just to make our life interesting, some newer Lenovo ThinkPads have
2419 * bugs in the BIOS and may fail to update volume_toggle properly.
2424 oldn->volume_toggle != newn->volume_toggle ||
2425 oldn->volume_level != newn->volume_level) {
2426 /* recently muted, or repeated mute keypress, or
2427 * multiple presses ending in mute */
2428 issue_volchange(oldn->volume_level, newn->volume_level,
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2435 /* recently unmuted, issue 'unmute' keypress */
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2438 if (oldn->volume_level != newn->volume_level) {
2439 issue_volchange(oldn->volume_level, newn->volume_level,
2441 } else if (oldn->volume_toggle != newn->volume_toggle) {
2442 /* repeated vol up/down keypress at end of scale ? */
2443 if (newn->volume_level == 0)
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2445 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2450 /* handle brightness */
2451 if (oldn->brightness_level != newn->brightness_level) {
2452 issue_brightnesschange(oldn->brightness_level,
2453 newn->brightness_level, event_mask);
2454 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2455 /* repeated key presses that didn't change state */
2456 if (newn->brightness_level == 0)
2457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2458 else if (newn->brightness_level >= bright_maxlvl
2459 && !tp_features.bright_unkfw)
2460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2463 #undef TPACPI_COMPARE_KEY
2464 #undef TPACPI_MAY_SEND_KEY
2470 * We track all events in hotkey_source_mask all the time, since
2471 * most of them are edge-based. We only issue those requested by
2472 * hotkey_user_mask or hotkey_driver_mask, though.
2474 static int hotkey_kthread(void *data)
2476 struct tp_nvram_state s[2];
2477 u32 poll_mask, event_mask;
2478 unsigned int si, so;
2480 unsigned int change_detector;
2481 unsigned int poll_freq;
2484 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2493 /* Initial state for compares */
2494 mutex_lock(&hotkey_thread_data_mutex);
2495 change_detector = hotkey_config_change;
2496 poll_mask = hotkey_source_mask;
2497 event_mask = hotkey_source_mask &
2498 (hotkey_driver_mask | hotkey_user_mask);
2499 poll_freq = hotkey_poll_freq;
2500 mutex_unlock(&hotkey_thread_data_mutex);
2501 hotkey_read_nvram(&s[so], poll_mask);
2503 while (!kthread_should_stop()) {
2505 if (likely(poll_freq))
2508 t = 100; /* should never happen... */
2510 t = msleep_interruptible(t);
2511 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2514 if (t > 0 && !was_frozen)
2517 mutex_lock(&hotkey_thread_data_mutex);
2518 if (was_frozen || hotkey_config_change != change_detector) {
2519 /* forget old state on thaw or config change */
2522 change_detector = hotkey_config_change;
2524 poll_mask = hotkey_source_mask;
2525 event_mask = hotkey_source_mask &
2526 (hotkey_driver_mask | hotkey_user_mask);
2527 poll_freq = hotkey_poll_freq;
2528 mutex_unlock(&hotkey_thread_data_mutex);
2530 if (likely(poll_mask)) {
2531 hotkey_read_nvram(&s[si], poll_mask);
2532 if (likely(si != so)) {
2533 hotkey_compare_and_issue_event(&s[so], &s[si],
2546 /* call with hotkey_mutex held */
2547 static void hotkey_poll_stop_sync(void)
2549 if (tpacpi_hotkey_task) {
2550 kthread_stop(tpacpi_hotkey_task);
2551 tpacpi_hotkey_task = NULL;
2555 /* call with hotkey_mutex held */
2556 static void hotkey_poll_setup(const bool may_warn)
2558 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2559 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2561 if (hotkey_poll_freq > 0 &&
2562 (poll_driver_mask ||
2563 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2564 if (!tpacpi_hotkey_task) {
2565 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2566 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2567 if (IS_ERR(tpacpi_hotkey_task)) {
2568 tpacpi_hotkey_task = NULL;
2569 pr_err("could not create kernel thread for hotkey polling\n");
2573 hotkey_poll_stop_sync();
2574 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2575 hotkey_poll_freq == 0) {
2576 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2577 poll_user_mask, poll_driver_mask);
2582 static void hotkey_poll_setup_safe(const bool may_warn)
2584 mutex_lock(&hotkey_mutex);
2585 hotkey_poll_setup(may_warn);
2586 mutex_unlock(&hotkey_mutex);
2589 /* call with hotkey_mutex held */
2590 static void hotkey_poll_set_freq(unsigned int freq)
2593 hotkey_poll_stop_sync();
2595 hotkey_poll_freq = freq;
2598 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2600 static void hotkey_poll_setup(const bool __unused)
2604 static void hotkey_poll_setup_safe(const bool __unused)
2608 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2610 static int hotkey_inputdev_open(struct input_dev *dev)
2612 switch (tpacpi_lifecycle) {
2613 case TPACPI_LIFE_INIT:
2614 case TPACPI_LIFE_RUNNING:
2615 hotkey_poll_setup_safe(false);
2617 case TPACPI_LIFE_EXITING:
2621 /* Should only happen if tpacpi_lifecycle is corrupt */
2626 static void hotkey_inputdev_close(struct input_dev *dev)
2628 /* disable hotkey polling when possible */
2629 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2630 !(hotkey_source_mask & hotkey_driver_mask))
2631 hotkey_poll_setup_safe(false);
2634 /* sysfs hotkey enable ------------------------------------------------- */
2635 static ssize_t hotkey_enable_show(struct device *dev,
2636 struct device_attribute *attr,
2641 printk_deprecated_attribute("hotkey_enable",
2642 "Hotkey reporting is always enabled");
2644 res = hotkey_status_get(&status);
2648 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2651 static ssize_t hotkey_enable_store(struct device *dev,
2652 struct device_attribute *attr,
2653 const char *buf, size_t count)
2657 printk_deprecated_attribute("hotkey_enable",
2658 "Hotkeys can be disabled through hotkey_mask");
2660 if (parse_strtoul(buf, 1, &t))
2669 static DEVICE_ATTR_RW(hotkey_enable);
2671 /* sysfs hotkey mask --------------------------------------------------- */
2672 static ssize_t hotkey_mask_show(struct device *dev,
2673 struct device_attribute *attr,
2676 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2679 static ssize_t hotkey_mask_store(struct device *dev,
2680 struct device_attribute *attr,
2681 const char *buf, size_t count)
2686 if (parse_strtoul(buf, 0xffffffffUL, &t))
2689 if (mutex_lock_killable(&hotkey_mutex))
2690 return -ERESTARTSYS;
2692 res = hotkey_user_mask_set(t);
2694 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2695 hotkey_poll_setup(true);
2698 mutex_unlock(&hotkey_mutex);
2700 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2702 return (res) ? res : count;
2705 static DEVICE_ATTR_RW(hotkey_mask);
2707 /* sysfs hotkey bios_enabled ------------------------------------------- */
2708 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2709 struct device_attribute *attr,
2712 return sprintf(buf, "0\n");
2715 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2717 /* sysfs hotkey bios_mask ---------------------------------------------- */
2718 static ssize_t hotkey_bios_mask_show(struct device *dev,
2719 struct device_attribute *attr,
2722 printk_deprecated_attribute("hotkey_bios_mask",
2723 "This attribute is useless.");
2724 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2727 static DEVICE_ATTR_RO(hotkey_bios_mask);
2729 /* sysfs hotkey all_mask ----------------------------------------------- */
2730 static ssize_t hotkey_all_mask_show(struct device *dev,
2731 struct device_attribute *attr,
2734 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2735 hotkey_all_mask | hotkey_source_mask);
2738 static DEVICE_ATTR_RO(hotkey_all_mask);
2740 /* sysfs hotkey all_mask ----------------------------------------------- */
2741 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2746 hotkey_adaptive_all_mask | hotkey_source_mask);
2749 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2751 /* sysfs hotkey recommended_mask --------------------------------------- */
2752 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2757 (hotkey_all_mask | hotkey_source_mask)
2758 & ~hotkey_reserved_mask);
2761 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2763 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2765 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2766 static ssize_t hotkey_source_mask_show(struct device *dev,
2767 struct device_attribute *attr,
2770 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2773 static ssize_t hotkey_source_mask_store(struct device *dev,
2774 struct device_attribute *attr,
2775 const char *buf, size_t count)
2781 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2782 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2785 if (mutex_lock_killable(&hotkey_mutex))
2786 return -ERESTARTSYS;
2788 HOTKEY_CONFIG_CRITICAL_START
2789 hotkey_source_mask = t;
2790 HOTKEY_CONFIG_CRITICAL_END
2792 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2793 ~hotkey_source_mask);
2794 hotkey_poll_setup(true);
2796 /* check if events needed by the driver got disabled */
2797 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2798 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2800 mutex_unlock(&hotkey_mutex);
2803 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2806 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2809 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2811 return (rc < 0) ? rc : count;
2814 static DEVICE_ATTR_RW(hotkey_source_mask);
2816 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2817 static ssize_t hotkey_poll_freq_show(struct device *dev,
2818 struct device_attribute *attr,
2821 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2824 static ssize_t hotkey_poll_freq_store(struct device *dev,
2825 struct device_attribute *attr,
2826 const char *buf, size_t count)
2830 if (parse_strtoul(buf, 25, &t))
2833 if (mutex_lock_killable(&hotkey_mutex))
2834 return -ERESTARTSYS;
2836 hotkey_poll_set_freq(t);
2837 hotkey_poll_setup(true);
2839 mutex_unlock(&hotkey_mutex);
2841 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2846 static DEVICE_ATTR_RW(hotkey_poll_freq);
2848 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2850 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2851 static ssize_t hotkey_radio_sw_show(struct device *dev,
2852 struct device_attribute *attr,
2856 res = hotkey_get_wlsw();
2860 /* Opportunistic update */
2861 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2863 return snprintf(buf, PAGE_SIZE, "%d\n",
2864 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2867 static DEVICE_ATTR_RO(hotkey_radio_sw);
2869 static void hotkey_radio_sw_notify_change(void)
2871 if (tp_features.hotkey_wlsw)
2872 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2876 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2877 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2878 struct device_attribute *attr,
2882 res = hotkey_get_tablet_mode(&s);
2886 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2889 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2891 static void hotkey_tablet_mode_notify_change(void)
2893 if (tp_features.hotkey_tablet)
2894 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2895 "hotkey_tablet_mode");
2898 /* sysfs wakeup reason (pollable) -------------------------------------- */
2899 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2900 struct device_attribute *attr,
2903 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2906 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2908 static void hotkey_wakeup_reason_notify_change(void)
2910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2914 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2915 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2916 struct device_attribute *attr,
2919 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2922 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2923 hotkey_wakeup_hotunplug_complete_show, NULL);
2925 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2927 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2928 "wakeup_hotunplug_complete");
2931 /* sysfs adaptive kbd mode --------------------------------------------- */
2933 static int adaptive_keyboard_get_mode(void);
2934 static int adaptive_keyboard_set_mode(int new_mode);
2936 enum ADAPTIVE_KEY_MODE {
2939 WEB_CONFERENCE_MODE,
2944 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2945 struct device_attribute *attr,
2950 current_mode = adaptive_keyboard_get_mode();
2951 if (current_mode < 0)
2952 return current_mode;
2954 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2957 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2958 struct device_attribute *attr,
2959 const char *buf, size_t count)
2964 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2967 res = adaptive_keyboard_set_mode(t);
2968 return (res < 0) ? res : count;
2971 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2973 static struct attribute *adaptive_kbd_attributes[] = {
2974 &dev_attr_adaptive_kbd_mode.attr,
2978 static const struct attribute_group adaptive_kbd_attr_group = {
2979 .attrs = adaptive_kbd_attributes,
2982 /* --------------------------------------------------------------------- */
2984 static struct attribute *hotkey_attributes[] __initdata = {
2985 &dev_attr_hotkey_enable.attr,
2986 &dev_attr_hotkey_bios_enabled.attr,
2987 &dev_attr_hotkey_bios_mask.attr,
2988 &dev_attr_wakeup_reason.attr,
2989 &dev_attr_wakeup_hotunplug_complete.attr,
2990 &dev_attr_hotkey_mask.attr,
2991 &dev_attr_hotkey_all_mask.attr,
2992 &dev_attr_hotkey_adaptive_all_mask.attr,
2993 &dev_attr_hotkey_recommended_mask.attr,
2994 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2995 &dev_attr_hotkey_source_mask.attr,
2996 &dev_attr_hotkey_poll_freq.attr,
3001 * Sync both the hw and sw blocking state of all switches
3003 static void tpacpi_send_radiosw_update(void)
3008 * We must sync all rfkill controllers *before* issuing any
3009 * rfkill input events, or we will race the rfkill core input
3012 * tpacpi_inputdev_send_mutex works as a synchronization point
3015 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3018 wlsw = hotkey_get_wlsw();
3020 /* Sync hw blocking state first if it is hw-blocked */
3021 if (wlsw == TPACPI_RFK_RADIO_OFF)
3022 tpacpi_rfk_update_hwblock_state(true);
3024 /* Sync sw blocking state */
3025 tpacpi_rfk_update_swstate_all();
3027 /* Sync hw blocking state last if it is hw-unblocked */
3028 if (wlsw == TPACPI_RFK_RADIO_ON)
3029 tpacpi_rfk_update_hwblock_state(false);
3031 /* Issue rfkill input event for WLSW switch */
3033 mutex_lock(&tpacpi_inputdev_send_mutex);
3035 input_report_switch(tpacpi_inputdev,
3036 SW_RFKILL_ALL, (wlsw > 0));
3037 input_sync(tpacpi_inputdev);
3039 mutex_unlock(&tpacpi_inputdev_send_mutex);
3043 * this can be unconditional, as we will poll state again
3044 * if userspace uses the notify to read data
3046 hotkey_radio_sw_notify_change();
3049 static void hotkey_exit(void)
3051 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3052 mutex_lock(&hotkey_mutex);
3053 hotkey_poll_stop_sync();
3054 mutex_unlock(&hotkey_mutex);
3057 if (hotkey_dev_attributes)
3058 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3060 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3061 "restoring original HKEY status and mask\n");
3062 /* yes, there is a bitwise or below, we want the
3063 * functions to be called even if one of them fail */
3064 if (((tp_features.hotkey_mask &&
3065 hotkey_mask_set(hotkey_orig_mask)) |
3066 hotkey_status_set(false)) != 0)
3067 pr_err("failed to restore hot key mask to BIOS defaults\n");
3070 static void __init hotkey_unmap(const unsigned int scancode)
3072 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3073 clear_bit(hotkey_keycode_map[scancode],
3074 tpacpi_inputdev->keybit);
3075 hotkey_keycode_map[scancode] = KEY_RESERVED;
3081 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3084 #define TPACPI_HK_Q_INIMASK 0x0001
3086 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3087 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3088 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3089 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3090 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3091 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3092 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3093 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3094 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3095 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3096 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3097 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3098 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3099 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3100 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3101 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3102 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3103 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3104 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3105 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3108 typedef u16 tpacpi_keymap_entry_t;
3109 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3111 static int hotkey_init_tablet_mode(void)
3113 int in_tablet_mode = 0, res;
3116 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3117 /* For X41t, X60t, X61t Tablets... */
3118 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3119 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3121 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3122 /* For X1 Yoga (2016) */
3123 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3124 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3128 if (!tp_features.hotkey_tablet)
3131 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3132 type, in_tablet_mode ? "tablet" : "laptop");
3134 res = add_to_attr_set(hotkey_dev_attributes,
3135 &dev_attr_hotkey_tablet_mode.attr);
3139 return in_tablet_mode;
3142 static int __init hotkey_init(struct ibm_init_struct *iibm)
3144 /* Requirements for changing the default keymaps:
3146 * 1. Many of the keys are mapped to KEY_RESERVED for very
3147 * good reasons. Do not change them unless you have deep
3148 * knowledge on the IBM and Lenovo ThinkPad firmware for
3149 * the various ThinkPad models. The driver behaves
3150 * differently for KEY_RESERVED: such keys have their
3151 * hot key mask *unset* in mask_recommended, and also
3152 * in the initial hot key mask programmed into the
3153 * firmware at driver load time, which means the firm-
3154 * ware may react very differently if you change them to
3157 * 2. You must be subscribed to the linux-thinkpad and
3158 * ibm-acpi-devel mailing lists, and you should read the
3159 * list archives since 2007 if you want to change the
3160 * keymaps. This requirement exists so that you will
3161 * know the past history of problems with the thinkpad-
3162 * acpi driver keymaps, and also that you will be
3163 * listening to any bug reports;
3165 * 3. Do not send thinkpad-acpi specific patches directly to
3166 * for merging, *ever*. Send them to the linux-acpi
3167 * mailinglist for comments. Merging is to be done only
3168 * through acpi-test and the ACPI maintainer.
3170 * If the above is too much to ask, don't change the keymap.
3171 * Ask the thinkpad-acpi maintainer to do it, instead.
3175 TPACPI_KEYMAP_IBM_GENERIC = 0,
3176 TPACPI_KEYMAP_LENOVO_GENERIC,
3179 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3180 /* Generic keymap for IBM ThinkPads */
3181 [TPACPI_KEYMAP_IBM_GENERIC] = {
3182 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3183 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3184 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3185 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3187 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3188 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3189 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3190 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3192 /* brightness: firmware always reacts to them */
3193 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3194 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3196 /* Thinklight: firmware always react to it */
3197 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3199 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3200 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3202 /* Volume: firmware always react to it and reprograms
3203 * the built-in *extra* mixer. Never map it to control
3204 * another mixer by default. */
3205 KEY_RESERVED, /* 0x14: VOLUME UP */
3206 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3207 KEY_RESERVED, /* 0x16: MUTE */
3209 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3211 /* (assignments unknown, please report if found) */
3212 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3213 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3215 /* No assignments, only used for Adaptive keyboards. */
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3217 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3218 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3219 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3220 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3222 /* No assignment, used for newer Lenovo models */
3223 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3224 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3225 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3226 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3227 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3228 KEY_UNKNOWN, KEY_UNKNOWN
3232 /* Generic keymap for Lenovo ThinkPads */
3233 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3234 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3235 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3236 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3237 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3239 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3240 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3241 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3242 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3244 /* These should be enabled --only-- when ACPI video
3245 * is disabled (i.e. in "vendor" mode), and are handled
3246 * in a special way by the init code */
3247 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3248 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3250 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3252 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3253 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3255 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3256 * react to it and reprograms the built-in *extra* mixer.
3257 * Never map it to control another mixer by default.
3259 * T60?, T61, R60?, R61: firmware and EC tries to send
3260 * these over the regular keyboard, so these are no-ops,
3261 * but there are still weird bugs re. MUTE, so do not
3262 * change unless you get test reports from all Lenovo
3263 * models. May cause the BIOS to interfere with the
3266 KEY_RESERVED, /* 0x14: VOLUME UP */
3267 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3268 KEY_RESERVED, /* 0x16: MUTE */
3270 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3272 /* (assignments unknown, please report if found) */
3273 KEY_UNKNOWN, KEY_UNKNOWN,
3276 * The mic mute button only sends 0x1a. It does not
3277 * automatically mute the mic or change the mute light.
3279 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3281 /* (assignments unknown, please report if found) */
3284 /* Extra keys in use since the X240 / T440 / T540 */
3285 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3288 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3289 * The first item in this list is the Mute button which is
3290 * emitted with 0x103 through
3291 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3293 * We'll need to offset those by 0x20.
3295 KEY_RESERVED, /* Mute held, 0x103 */
3296 KEY_BRIGHTNESS_MIN, /* Backlight off */
3297 KEY_RESERVED, /* Clipping tool */
3298 KEY_RESERVED, /* Cloud */
3300 KEY_VOICECOMMAND, /* Voice */
3302 KEY_RESERVED, /* Gestures */
3306 KEY_CONFIG, /* Settings */
3307 KEY_RESERVED, /* New tab */
3308 KEY_REFRESH, /* Reload */
3309 KEY_BACK, /* Back */
3310 KEY_RESERVED, /* Microphone down */
3311 KEY_RESERVED, /* Microphone up */
3312 KEY_RESERVED, /* Microphone cancellation */
3313 KEY_RESERVED, /* Camera mode */
3314 KEY_RESERVED, /* Rotate display, 0x116 */
3317 * These are found in 2017 models (e.g. T470s, X270).
3318 * The lowest known value is 0x311, which according to
3319 * the manual should launch a user defined favorite
3322 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3323 * corresponding to 0x34.
3326 /* (assignments unknown, please report if found) */
3327 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3328 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3329 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3330 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3333 KEY_FAVORITES, /* Favorite app, 0x311 */
3334 KEY_RESERVED, /* Clipping tool */
3336 KEY_BLUETOOTH, /* Bluetooth */
3337 KEY_KEYBOARD /* Keyboard, 0x315 */
3341 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3342 /* Generic maps (fallback) */
3344 .vendor = PCI_VENDOR_ID_IBM,
3345 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3346 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3349 .vendor = PCI_VENDOR_ID_LENOVO,
3350 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3351 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3355 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3356 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3361 bool radiosw_state = false;
3362 bool tabletsw_state = false;
3364 unsigned long quirks;
3365 unsigned long keymap_id;
3367 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3368 "initializing hotkey subdriver\n");
3370 BUG_ON(!tpacpi_inputdev);
3371 BUG_ON(tpacpi_inputdev->open != NULL ||
3372 tpacpi_inputdev->close != NULL);
3374 TPACPI_ACPIHANDLE_INIT(hkey);
3375 mutex_init(&hotkey_mutex);
3377 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3378 mutex_init(&hotkey_thread_data_mutex);
3381 /* hotkey not supported on 570 */
3382 tp_features.hotkey = hkey_handle != NULL;
3384 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3386 str_supported(tp_features.hotkey));
3388 if (!tp_features.hotkey)
3391 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3392 ARRAY_SIZE(tpacpi_hotkey_qtable));
3394 tpacpi_disable_brightness_delay();
3396 /* MUST have enough space for all attributes to be added to
3397 * hotkey_dev_attributes */
3398 hotkey_dev_attributes = create_attr_set(
3399 ARRAY_SIZE(hotkey_attributes) + 2,
3401 if (!hotkey_dev_attributes)
3403 res = add_many_to_attr_set(hotkey_dev_attributes,
3405 ARRAY_SIZE(hotkey_attributes));
3409 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3410 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3411 for HKEY interface version 0x100 */
3412 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3413 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3414 "firmware HKEY interface version: 0x%x\n",
3417 switch (hkeyv >> 8) {
3420 * MHKV 0x100 in A31, R40, R40e,
3421 * T4x, X31, and later
3424 /* Paranoia check AND init hotkey_all_mask */
3425 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3427 pr_err("missing MHKA handler, please report this to %s\n",
3429 /* Fallback: pre-init for FN+F3,F4,F12 */
3430 hotkey_all_mask = 0x080cU;
3432 tp_features.hotkey_mask = 1;
3438 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3441 /* Paranoia check AND init hotkey_all_mask */
3442 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3444 pr_err("missing MHKA handler, please report this to %s\n",
3446 /* Fallback: pre-init for FN+F3,F4,F12 */
3447 hotkey_all_mask = 0x080cU;
3449 tp_features.hotkey_mask = 1;
3453 * Check if we have an adaptive keyboard, like on the
3454 * Lenovo Carbon X1 2014 (2nd Gen).
3456 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3458 if (hotkey_adaptive_all_mask != 0) {
3459 tp_features.has_adaptive_kbd = true;
3460 res = sysfs_create_group(
3461 &tpacpi_pdev->dev.kobj,
3462 &adaptive_kbd_attr_group);
3467 tp_features.has_adaptive_kbd = false;
3468 hotkey_adaptive_all_mask = 0x0U;
3473 pr_err("unknown version of the HKEY interface: 0x%x\n",
3475 pr_err("please report this to %s\n", TPACPI_MAIL);
3480 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3481 "hotkey masks are %s\n",
3482 str_supported(tp_features.hotkey_mask));
3484 /* Init hotkey_all_mask if not initialized yet */
3485 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3486 (quirks & TPACPI_HK_Q_INIMASK))
3487 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3489 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3490 if (tp_features.hotkey_mask) {
3491 /* hotkey_source_mask *must* be zero for
3492 * the first hotkey_mask_get to return hotkey_orig_mask */
3493 res = hotkey_mask_get();
3497 hotkey_orig_mask = hotkey_acpi_mask;
3499 hotkey_orig_mask = hotkey_all_mask;
3500 hotkey_acpi_mask = hotkey_all_mask;
3503 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3505 tp_features.hotkey_wlsw = 1;
3506 radiosw_state = !!tpacpi_wlsw_emulstate;
3507 pr_info("radio switch emulation enabled\n");
3510 /* Not all thinkpads have a hardware radio switch */
3511 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3512 tp_features.hotkey_wlsw = 1;
3513 radiosw_state = !!status;
3514 pr_info("radio switch found; radios are %s\n",
3515 enabled(status, 0));
3517 if (tp_features.hotkey_wlsw)
3518 res = add_to_attr_set(hotkey_dev_attributes,
3519 &dev_attr_hotkey_radio_sw.attr);
3521 res = hotkey_init_tablet_mode();
3525 tabletsw_state = res;
3527 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3528 &tpacpi_pdev->dev.kobj);
3532 /* Set up key map */
3533 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3535 if (!hotkey_keycode_map) {
3536 pr_err("failed to allocate memory for key map\n");
3541 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3542 ARRAY_SIZE(tpacpi_keymap_qtable));
3543 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3544 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3545 "using keymap number %lu\n", keymap_id);
3547 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3548 TPACPI_HOTKEY_MAP_SIZE);
3550 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3551 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3552 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3553 tpacpi_inputdev->keycode = hotkey_keycode_map;
3554 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3555 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3556 input_set_capability(tpacpi_inputdev, EV_KEY,
3557 hotkey_keycode_map[i]);
3559 if (i < sizeof(hotkey_reserved_mask)*8)
3560 hotkey_reserved_mask |= 1 << i;
3564 if (tp_features.hotkey_wlsw) {
3565 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3566 input_report_switch(tpacpi_inputdev,
3567 SW_RFKILL_ALL, radiosw_state);
3569 if (tp_features.hotkey_tablet) {
3570 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3571 input_report_switch(tpacpi_inputdev,
3572 SW_TABLET_MODE, tabletsw_state);
3575 /* Do not issue duplicate brightness change events to
3576 * userspace. tpacpi_detect_brightness_capabilities() must have
3577 * been called before this point */
3578 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3579 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3580 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3582 /* Disable brightness up/down on Lenovo thinkpads when
3583 * ACPI is handling them, otherwise it is plain impossible
3584 * for userspace to do something even remotely sane */
3585 hotkey_reserved_mask |=
3586 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3587 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3588 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3589 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3592 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3593 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3595 & ~hotkey_reserved_mask;
3597 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3598 "hotkey source mask 0x%08x, polling freq %u\n",
3599 hotkey_source_mask, hotkey_poll_freq);
3602 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3603 "enabling firmware HKEY event interface...\n");
3604 res = hotkey_status_set(true);
3609 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3610 | hotkey_driver_mask)
3611 & ~hotkey_source_mask);
3612 if (res < 0 && res != -ENXIO) {
3616 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3617 & ~hotkey_reserved_mask;
3618 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3619 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3620 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3622 tpacpi_inputdev->open = &hotkey_inputdev_open;
3623 tpacpi_inputdev->close = &hotkey_inputdev_close;
3625 hotkey_poll_setup_safe(true);
3630 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3631 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3632 &adaptive_kbd_attr_group);
3634 hotkey_dev_attributes = NULL;
3636 return (res < 0) ? res : 1;
3639 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3640 * mode, Web conference mode, Function mode and Lay-flat mode.
3641 * We support Home mode and Function mode currently.
3643 * Will consider support rest of modes in future.
3646 static const int adaptive_keyboard_modes[] = {
3648 /* WEB_BROWSER_MODE = 2,
3649 WEB_CONFERENCE_MODE = 3, */
3653 #define DFR_CHANGE_ROW 0x101
3654 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3655 #define FIRST_ADAPTIVE_KEY 0x103
3657 /* press Fn key a while second, it will switch to Function Mode. Then
3658 * release Fn key, previous mode be restored.
3660 static bool adaptive_keyboard_mode_is_saved;
3661 static int adaptive_keyboard_prev_mode;
3663 static int adaptive_keyboard_get_mode(void)
3667 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3668 pr_err("Cannot read adaptive keyboard mode\n");
3675 static int adaptive_keyboard_set_mode(int new_mode)
3678 new_mode > LAYFLAT_MODE)
3681 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3682 pr_err("Cannot set adaptive keyboard mode\n");
3689 static int adaptive_keyboard_get_next_mode(int mode)
3692 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3694 for (i = 0; i <= max_mode; i++) {
3695 if (adaptive_keyboard_modes[i] == mode)
3704 return adaptive_keyboard_modes[i];
3707 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3709 int current_mode = 0;
3714 case DFR_CHANGE_ROW:
3715 if (adaptive_keyboard_mode_is_saved) {
3716 new_mode = adaptive_keyboard_prev_mode;
3717 adaptive_keyboard_mode_is_saved = false;
3719 current_mode = adaptive_keyboard_get_mode();
3720 if (current_mode < 0)
3722 new_mode = adaptive_keyboard_get_next_mode(
3726 if (adaptive_keyboard_set_mode(new_mode) < 0)
3731 case DFR_SHOW_QUICKVIEW_ROW:
3732 current_mode = adaptive_keyboard_get_mode();
3733 if (current_mode < 0)
3736 adaptive_keyboard_prev_mode = current_mode;
3737 adaptive_keyboard_mode_is_saved = true;
3739 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3744 if (scancode < FIRST_ADAPTIVE_KEY ||
3745 scancode >= FIRST_ADAPTIVE_KEY +
3746 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3747 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3748 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3752 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3753 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3754 if (keycode != KEY_RESERVED) {
3755 mutex_lock(&tpacpi_inputdev_send_mutex);
3757 input_report_key(tpacpi_inputdev, keycode, 1);
3758 input_sync(tpacpi_inputdev);
3760 input_report_key(tpacpi_inputdev, keycode, 0);
3761 input_sync(tpacpi_inputdev);
3763 mutex_unlock(&tpacpi_inputdev_send_mutex);
3769 static bool hotkey_notify_hotkey(const u32 hkey,
3771 bool *ignore_acpi_ev)
3773 /* 0x1000-0x1FFF: key presses */
3774 unsigned int scancode = hkey & 0xfff;
3775 *send_acpi_ev = true;
3776 *ignore_acpi_ev = false;
3779 * Original events are in the 0x10XX range, the adaptive keyboard
3780 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3781 * models, additional keys are emitted through 0x13XX.
3783 switch ((hkey >> 8) & 0xf) {
3786 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3787 /* HKEY event 0x1001 is scancode 0x00 */
3789 if (!(hotkey_source_mask & (1 << scancode))) {
3790 tpacpi_input_send_key_masked(scancode);
3791 *send_acpi_ev = false;
3793 *ignore_acpi_ev = true;
3800 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3803 /* Extended keycodes start at 0x300 and our offset into the map
3804 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3805 * will be positive, but might not be in the correct range.
3807 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3808 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3809 scancode < TPACPI_HOTKEY_MAP_LEN) {
3810 tpacpi_input_send_key(scancode);
3819 static bool hotkey_notify_wakeup(const u32 hkey,
3821 bool *ignore_acpi_ev)
3823 /* 0x2000-0x2FFF: Wakeup reason */
3824 *send_acpi_ev = true;
3825 *ignore_acpi_ev = false;
3828 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3829 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3830 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3831 *ignore_acpi_ev = true;
3834 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3835 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3836 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3837 *ignore_acpi_ev = true;
3840 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3841 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3842 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3843 /* how to auto-heal: */
3844 /* 2313: woke up from S3, go to S4/S5 */
3845 /* 2413: woke up from S4, go to S5 */
3852 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3853 pr_info("woke up due to a hot-unplug request...\n");
3854 hotkey_wakeup_reason_notify_change();
3859 static bool hotkey_notify_dockevent(const u32 hkey,
3861 bool *ignore_acpi_ev)
3863 /* 0x4000-0x4FFF: dock-related events */
3864 *send_acpi_ev = true;
3865 *ignore_acpi_ev = false;
3868 case TP_HKEY_EV_UNDOCK_ACK:
3869 /* ACPI undock operation completed after wakeup */
3870 hotkey_autosleep_ack = 1;
3871 pr_info("undocked\n");
3872 hotkey_wakeup_hotunplug_complete_notify_change();
3875 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3876 pr_info("docked into hotplug port replicator\n");
3878 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3879 pr_info("undocked from hotplug port replicator\n");
3887 static bool hotkey_notify_usrevent(const u32 hkey,
3889 bool *ignore_acpi_ev)
3891 /* 0x5000-0x5FFF: human interface helpers */
3892 *send_acpi_ev = true;
3893 *ignore_acpi_ev = false;
3896 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3897 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3900 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3901 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3902 tpacpi_input_send_tabletsw();
3903 hotkey_tablet_mode_notify_change();
3904 *send_acpi_ev = false;
3907 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3908 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3909 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3910 /* do not propagate these events */
3911 *ignore_acpi_ev = true;
3919 static void thermal_dump_all_sensors(void);
3921 static bool hotkey_notify_6xxx(const u32 hkey,
3923 bool *ignore_acpi_ev)
3927 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3928 *send_acpi_ev = true;
3929 *ignore_acpi_ev = false;
3932 case TP_HKEY_EV_THM_TABLE_CHANGED:
3933 pr_info("EC reports that Thermal Table has changed\n");
3934 /* recommended action: do nothing, we don't have
3935 * Lenovo ATM information */
3937 case TP_HKEY_EV_ALARM_BAT_HOT:
3938 pr_crit("THERMAL ALARM: battery is too hot!\n");
3939 /* recommended action: warn user through gui */
3941 case TP_HKEY_EV_ALARM_BAT_XHOT:
3942 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3943 /* recommended action: immediate sleep/hibernate */
3945 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3946 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
3947 /* recommended action: warn user through gui, that */
3948 /* some internal component is too hot */
3950 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3951 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
3952 /* recommended action: immediate sleep/hibernate */
3954 case TP_HKEY_EV_AC_CHANGED:
3955 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3956 * AC status changed; can be triggered by plugging or
3957 * unplugging AC adapter, docking or undocking. */
3961 case TP_HKEY_EV_KEY_NUMLOCK:
3962 case TP_HKEY_EV_KEY_FN:
3963 case TP_HKEY_EV_KEY_FN_ESC:
3964 /* key press events, we just ignore them as long as the EC
3965 * is still reporting them in the normal keyboard stream */
3966 *send_acpi_ev = false;
3967 *ignore_acpi_ev = true;
3970 case TP_HKEY_EV_TABLET_CHANGED:
3971 tpacpi_input_send_tabletsw();
3972 hotkey_tablet_mode_notify_change();
3973 *send_acpi_ev = false;
3977 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3981 thermal_dump_all_sensors();
3986 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3990 bool ignore_acpi_ev;
3993 if (event != 0x80) {
3994 pr_err("unknown HKEY notification event %d\n", event);
3995 /* forward it to userspace, maybe it knows how to handle it */
3996 acpi_bus_generate_netlink_event(
3997 ibm->acpi->device->pnp.device_class,
3998 dev_name(&ibm->acpi->device->dev),
4004 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4005 pr_err("failed to retrieve HKEY event\n");
4014 send_acpi_ev = true;
4015 ignore_acpi_ev = false;
4017 switch (hkey >> 12) {
4019 /* 0x1000-0x1FFF: key presses */
4020 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4024 /* 0x2000-0x2FFF: Wakeup reason */
4025 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4029 /* 0x3000-0x3FFF: bay-related wakeups */
4031 case TP_HKEY_EV_BAYEJ_ACK:
4032 hotkey_autosleep_ack = 1;
4033 pr_info("bay ejected\n");
4034 hotkey_wakeup_hotunplug_complete_notify_change();
4037 case TP_HKEY_EV_OPTDRV_EJ:
4038 /* FIXME: kick libata if SATA link offline */
4046 /* 0x4000-0x4FFF: dock-related events */
4047 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4051 /* 0x5000-0x5FFF: human interface helpers */
4052 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4056 /* 0x6000-0x6FFF: thermal alarms/notices and
4057 * keyboard events */
4058 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4062 /* 0x7000-0x7FFF: misc */
4063 if (tp_features.hotkey_wlsw &&
4064 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4065 tpacpi_send_radiosw_update();
4070 /* fallthrough to default */
4075 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4076 pr_notice("please report the conditions when this event happened to %s\n",
4080 /* netlink events */
4081 if (!ignore_acpi_ev && send_acpi_ev) {
4082 acpi_bus_generate_netlink_event(
4083 ibm->acpi->device->pnp.device_class,
4084 dev_name(&ibm->acpi->device->dev),
4090 static void hotkey_suspend(void)
4092 /* Do these on suspend, we get the events on early resume! */
4093 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4094 hotkey_autosleep_ack = 0;
4096 /* save previous mode of adaptive keyboard of X1 Carbon */
4097 if (tp_features.has_adaptive_kbd) {
4098 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4100 pr_err("Cannot read adaptive keyboard mode.\n");
4105 static void hotkey_resume(void)
4107 tpacpi_disable_brightness_delay();
4109 if (hotkey_status_set(true) < 0 ||
4110 hotkey_mask_set(hotkey_acpi_mask) < 0)
4111 pr_err("error while attempting to reset the event firmware interface\n");
4113 tpacpi_send_radiosw_update();
4114 hotkey_tablet_mode_notify_change();
4115 hotkey_wakeup_reason_notify_change();
4116 hotkey_wakeup_hotunplug_complete_notify_change();
4117 hotkey_poll_setup_safe(false);
4119 /* restore previous mode of adapive keyboard of X1 Carbon */
4120 if (tp_features.has_adaptive_kbd) {
4121 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4122 adaptive_keyboard_prev_mode)) {
4123 pr_err("Cannot set adaptive keyboard mode.\n");
4128 /* procfs -------------------------------------------------------------- */
4129 static int hotkey_read(struct seq_file *m)
4133 if (!tp_features.hotkey) {
4134 seq_printf(m, "status:\t\tnot supported\n");
4138 if (mutex_lock_killable(&hotkey_mutex))
4139 return -ERESTARTSYS;
4140 res = hotkey_status_get(&status);
4142 res = hotkey_mask_get();
4143 mutex_unlock(&hotkey_mutex);
4147 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4148 if (hotkey_all_mask) {
4149 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4150 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4152 seq_printf(m, "mask:\t\tnot supported\n");
4153 seq_printf(m, "commands:\tenable, disable, reset\n");
4159 static void hotkey_enabledisable_warn(bool enable)
4161 tpacpi_log_usertask("procfs hotkey enable/disable");
4162 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4163 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4164 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4167 static int hotkey_write(char *buf)
4173 if (!tp_features.hotkey)
4176 if (mutex_lock_killable(&hotkey_mutex))
4177 return -ERESTARTSYS;
4179 mask = hotkey_user_mask;
4182 while ((cmd = next_cmd(&buf))) {
4183 if (strlencmp(cmd, "enable") == 0) {
4184 hotkey_enabledisable_warn(1);
4185 } else if (strlencmp(cmd, "disable") == 0) {
4186 hotkey_enabledisable_warn(0);
4188 } else if (strlencmp(cmd, "reset") == 0) {
4189 mask = (hotkey_all_mask | hotkey_source_mask)
4190 & ~hotkey_reserved_mask;
4191 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4193 } else if (sscanf(cmd, "%x", &mask) == 1) {
4202 tpacpi_disclose_usertask("procfs hotkey",
4203 "set mask to 0x%08x\n", mask);
4204 res = hotkey_user_mask_set(mask);
4208 mutex_unlock(&hotkey_mutex);
4212 static const struct acpi_device_id ibm_htk_device_ids[] = {
4213 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4214 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4215 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4219 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4220 .hid = ibm_htk_device_ids,
4221 .notify = hotkey_notify,
4222 .handle = &hkey_handle,
4223 .type = ACPI_DEVICE_NOTIFY,
4226 static struct ibm_struct hotkey_driver_data = {
4228 .read = hotkey_read,
4229 .write = hotkey_write,
4230 .exit = hotkey_exit,
4231 .resume = hotkey_resume,
4232 .suspend = hotkey_suspend,
4233 .acpi = &ibm_hotkey_acpidriver,
4236 /*************************************************************************
4237 * Bluetooth subdriver
4241 /* ACPI GBDC/SBDC bits */
4242 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4243 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4244 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4245 0 = disable, 1 = enable */
4249 /* ACPI \BLTH commands */
4250 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4251 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4252 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4253 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4254 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4257 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4259 static int bluetooth_get_status(void)
4263 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4264 if (dbg_bluetoothemul)
4265 return (tpacpi_bluetooth_emulstate) ?
4266 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4269 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4272 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4273 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4276 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4280 vdbg_printk(TPACPI_DBG_RFKILL,
4281 "will attempt to %s bluetooth\n",
4282 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4284 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4285 if (dbg_bluetoothemul) {
4286 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4291 if (state == TPACPI_RFK_RADIO_ON)
4292 status = TP_ACPI_BLUETOOTH_RADIOSSW
4293 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4297 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4303 /* sysfs bluetooth enable ---------------------------------------------- */
4304 static ssize_t bluetooth_enable_show(struct device *dev,
4305 struct device_attribute *attr,
4308 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4312 static ssize_t bluetooth_enable_store(struct device *dev,
4313 struct device_attribute *attr,
4314 const char *buf, size_t count)
4316 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4320 static DEVICE_ATTR_RW(bluetooth_enable);
4322 /* --------------------------------------------------------------------- */
4324 static struct attribute *bluetooth_attributes[] = {
4325 &dev_attr_bluetooth_enable.attr,
4329 static const struct attribute_group bluetooth_attr_group = {
4330 .attrs = bluetooth_attributes,
4333 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4334 .get_status = bluetooth_get_status,
4335 .set_status = bluetooth_set_status,
4338 static void bluetooth_shutdown(void)
4340 /* Order firmware to save current state to NVRAM */
4341 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4342 TP_ACPI_BLTH_SAVE_STATE))
4343 pr_notice("failed to save bluetooth state to NVRAM\n");
4345 vdbg_printk(TPACPI_DBG_RFKILL,
4346 "bluetooth state saved to NVRAM\n");
4349 static void bluetooth_exit(void)
4351 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4352 &bluetooth_attr_group);
4354 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4356 bluetooth_shutdown();
4359 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4364 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4365 "initializing bluetooth subdriver\n");
4367 TPACPI_ACPIHANDLE_INIT(hkey);
4369 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4370 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4371 tp_features.bluetooth = hkey_handle &&
4372 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4374 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4375 "bluetooth is %s, status 0x%02x\n",
4376 str_supported(tp_features.bluetooth),
4379 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4380 if (dbg_bluetoothemul) {
4381 tp_features.bluetooth = 1;
4382 pr_info("bluetooth switch emulation enabled\n");
4385 if (tp_features.bluetooth &&
4386 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4387 /* no bluetooth hardware present in system */
4388 tp_features.bluetooth = 0;
4389 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4390 "bluetooth hardware not installed\n");
4393 if (!tp_features.bluetooth)
4396 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4397 &bluetooth_tprfk_ops,
4398 RFKILL_TYPE_BLUETOOTH,
4399 TPACPI_RFK_BLUETOOTH_SW_NAME,
4404 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4405 &bluetooth_attr_group);
4407 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4414 /* procfs -------------------------------------------------------------- */
4415 static int bluetooth_read(struct seq_file *m)
4417 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4420 static int bluetooth_write(char *buf)
4422 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4425 static struct ibm_struct bluetooth_driver_data = {
4426 .name = "bluetooth",
4427 .read = bluetooth_read,
4428 .write = bluetooth_write,
4429 .exit = bluetooth_exit,
4430 .shutdown = bluetooth_shutdown,
4433 /*************************************************************************
4438 /* ACPI GWAN/SWAN bits */
4439 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4440 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4441 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4442 0 = disable, 1 = enable */
4445 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4447 static int wan_get_status(void)
4451 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4453 return (tpacpi_wwan_emulstate) ?
4454 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4457 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4460 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4461 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4464 static int wan_set_status(enum tpacpi_rfkill_state state)
4468 vdbg_printk(TPACPI_DBG_RFKILL,
4469 "will attempt to %s wwan\n",
4470 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4472 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4474 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4479 if (state == TPACPI_RFK_RADIO_ON)
4480 status = TP_ACPI_WANCARD_RADIOSSW
4481 | TP_ACPI_WANCARD_RESUMECTRL;
4485 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4491 /* sysfs wan enable ---------------------------------------------------- */
4492 static ssize_t wan_enable_show(struct device *dev,
4493 struct device_attribute *attr,
4496 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4500 static ssize_t wan_enable_store(struct device *dev,
4501 struct device_attribute *attr,
4502 const char *buf, size_t count)
4504 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4508 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4509 wan_enable_show, wan_enable_store);
4511 /* --------------------------------------------------------------------- */
4513 static struct attribute *wan_attributes[] = {
4514 &dev_attr_wwan_enable.attr,
4518 static const struct attribute_group wan_attr_group = {
4519 .attrs = wan_attributes,
4522 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4523 .get_status = wan_get_status,
4524 .set_status = wan_set_status,
4527 static void wan_shutdown(void)
4529 /* Order firmware to save current state to NVRAM */
4530 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4531 TP_ACPI_WGSV_SAVE_STATE))
4532 pr_notice("failed to save WWAN state to NVRAM\n");
4534 vdbg_printk(TPACPI_DBG_RFKILL,
4535 "WWAN state saved to NVRAM\n");
4538 static void wan_exit(void)
4540 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4543 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4548 static int __init wan_init(struct ibm_init_struct *iibm)
4553 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4554 "initializing wan subdriver\n");
4556 TPACPI_ACPIHANDLE_INIT(hkey);
4558 tp_features.wan = hkey_handle &&
4559 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4561 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4562 "wan is %s, status 0x%02x\n",
4563 str_supported(tp_features.wan),
4566 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4568 tp_features.wan = 1;
4569 pr_info("wwan switch emulation enabled\n");
4572 if (tp_features.wan &&
4573 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4574 /* no wan hardware present in system */
4575 tp_features.wan = 0;
4576 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4577 "wan hardware not installed\n");
4580 if (!tp_features.wan)
4583 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4586 TPACPI_RFK_WWAN_SW_NAME,
4591 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4595 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4602 /* procfs -------------------------------------------------------------- */
4603 static int wan_read(struct seq_file *m)
4605 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4608 static int wan_write(char *buf)
4610 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4613 static struct ibm_struct wan_driver_data = {
4618 .shutdown = wan_shutdown,
4621 /*************************************************************************
4626 /* ACPI GUWB/SUWB bits */
4627 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4628 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4631 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4633 static int uwb_get_status(void)
4637 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4639 return (tpacpi_uwb_emulstate) ?
4640 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4643 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4646 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4647 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4650 static int uwb_set_status(enum tpacpi_rfkill_state state)
4654 vdbg_printk(TPACPI_DBG_RFKILL,
4655 "will attempt to %s UWB\n",
4656 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4658 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4660 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4665 if (state == TPACPI_RFK_RADIO_ON)
4666 status = TP_ACPI_UWB_RADIOSSW;
4670 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4676 /* --------------------------------------------------------------------- */
4678 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4679 .get_status = uwb_get_status,
4680 .set_status = uwb_set_status,
4683 static void uwb_exit(void)
4685 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4688 static int __init uwb_init(struct ibm_init_struct *iibm)
4693 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4694 "initializing uwb subdriver\n");
4696 TPACPI_ACPIHANDLE_INIT(hkey);
4698 tp_features.uwb = hkey_handle &&
4699 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4701 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4702 "uwb is %s, status 0x%02x\n",
4703 str_supported(tp_features.uwb),
4706 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4708 tp_features.uwb = 1;
4709 pr_info("uwb switch emulation enabled\n");
4712 if (tp_features.uwb &&
4713 !(status & TP_ACPI_UWB_HWPRESENT)) {
4714 /* no uwb hardware present in system */
4715 tp_features.uwb = 0;
4716 dbg_printk(TPACPI_DBG_INIT,
4717 "uwb hardware not installed\n");
4720 if (!tp_features.uwb)
4723 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4726 TPACPI_RFK_UWB_SW_NAME,
4731 static struct ibm_struct uwb_driver_data = {
4734 .flags.experimental = 1,
4737 /*************************************************************************
4741 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4743 enum video_access_mode {
4744 TPACPI_VIDEO_NONE = 0,
4745 TPACPI_VIDEO_570, /* 570 */
4746 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4747 TPACPI_VIDEO_NEW, /* all others */
4750 enum { /* video status flags, based on VIDEO_570 */
4751 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4752 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4753 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4756 enum { /* TPACPI_VIDEO_570 constants */
4757 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4758 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4759 * video_status_flags */
4760 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4761 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4764 static enum video_access_mode video_supported;
4765 static int video_orig_autosw;
4767 static int video_autosw_get(void);
4768 static int video_autosw_set(int enable);
4770 TPACPI_HANDLE(vid, root,
4771 "\\_SB.PCI.AGP.VGA", /* 570 */
4772 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4773 "\\_SB.PCI0.VID0", /* 770e */
4774 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4775 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4776 "\\_SB.PCI0.AGP.VID", /* all others */
4779 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4781 static int __init video_init(struct ibm_init_struct *iibm)
4785 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4787 TPACPI_ACPIHANDLE_INIT(vid);
4788 if (tpacpi_is_ibm())
4789 TPACPI_ACPIHANDLE_INIT(vid2);
4791 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4792 /* G41, assume IVGA doesn't change */
4793 vid_handle = vid2_handle;
4796 /* video switching not supported on R30, R31 */
4797 video_supported = TPACPI_VIDEO_NONE;
4798 else if (tpacpi_is_ibm() &&
4799 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4801 video_supported = TPACPI_VIDEO_570;
4802 else if (tpacpi_is_ibm() &&
4803 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4804 /* 600e/x, 770e, 770x */
4805 video_supported = TPACPI_VIDEO_770;
4808 video_supported = TPACPI_VIDEO_NEW;
4810 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4811 str_supported(video_supported != TPACPI_VIDEO_NONE),
4814 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4817 static void video_exit(void)
4819 dbg_printk(TPACPI_DBG_EXIT,
4820 "restoring original video autoswitch mode\n");
4821 if (video_autosw_set(video_orig_autosw))
4822 pr_err("error while trying to restore original video autoswitch mode\n");
4825 static int video_outputsw_get(void)
4830 switch (video_supported) {
4831 case TPACPI_VIDEO_570:
4832 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4833 TP_ACPI_VIDEO_570_PHSCMD))
4835 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4837 case TPACPI_VIDEO_770:
4838 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4841 status |= TP_ACPI_VIDEO_S_LCD;
4842 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4845 status |= TP_ACPI_VIDEO_S_CRT;
4847 case TPACPI_VIDEO_NEW:
4848 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4849 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4852 status |= TP_ACPI_VIDEO_S_CRT;
4854 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4855 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4858 status |= TP_ACPI_VIDEO_S_LCD;
4859 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4862 status |= TP_ACPI_VIDEO_S_DVI;
4871 static int video_outputsw_set(int status)
4876 switch (video_supported) {
4877 case TPACPI_VIDEO_570:
4878 res = acpi_evalf(NULL, NULL,
4879 "\\_SB.PHS2", "vdd",
4880 TP_ACPI_VIDEO_570_PHS2CMD,
4881 status | TP_ACPI_VIDEO_570_PHS2SET);
4883 case TPACPI_VIDEO_770:
4884 autosw = video_autosw_get();
4888 res = video_autosw_set(1);
4891 res = acpi_evalf(vid_handle, NULL,
4892 "ASWT", "vdd", status * 0x100, 0);
4893 if (!autosw && video_autosw_set(autosw)) {
4894 pr_err("video auto-switch left enabled due to error\n");
4898 case TPACPI_VIDEO_NEW:
4899 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4900 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4906 return (res) ? 0 : -EIO;
4909 static int video_autosw_get(void)
4913 switch (video_supported) {
4914 case TPACPI_VIDEO_570:
4915 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4918 case TPACPI_VIDEO_770:
4919 case TPACPI_VIDEO_NEW:
4920 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4930 static int video_autosw_set(int enable)
4932 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4937 static int video_outputsw_cycle(void)
4939 int autosw = video_autosw_get();
4945 switch (video_supported) {
4946 case TPACPI_VIDEO_570:
4947 res = video_autosw_set(1);
4950 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4952 case TPACPI_VIDEO_770:
4953 case TPACPI_VIDEO_NEW:
4954 res = video_autosw_set(1);
4957 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4962 if (!autosw && video_autosw_set(autosw)) {
4963 pr_err("video auto-switch left enabled due to error\n");
4967 return (res) ? 0 : -EIO;
4970 static int video_expand_toggle(void)
4972 switch (video_supported) {
4973 case TPACPI_VIDEO_570:
4974 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
4976 case TPACPI_VIDEO_770:
4977 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
4979 case TPACPI_VIDEO_NEW:
4980 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
4988 static int video_read(struct seq_file *m)
4992 if (video_supported == TPACPI_VIDEO_NONE) {
4993 seq_printf(m, "status:\t\tnot supported\n");
4997 /* Even reads can crash X.org, so... */
4998 if (!capable(CAP_SYS_ADMIN))
5001 status = video_outputsw_get();
5005 autosw = video_autosw_get();
5009 seq_printf(m, "status:\t\tsupported\n");
5010 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5011 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5012 if (video_supported == TPACPI_VIDEO_NEW)
5013 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5014 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5015 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5016 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5017 if (video_supported == TPACPI_VIDEO_NEW)
5018 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5019 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5020 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5025 static int video_write(char *buf)
5028 int enable, disable, status;
5031 if (video_supported == TPACPI_VIDEO_NONE)
5034 /* Even reads can crash X.org, let alone writes... */
5035 if (!capable(CAP_SYS_ADMIN))
5041 while ((cmd = next_cmd(&buf))) {
5042 if (strlencmp(cmd, "lcd_enable") == 0) {
5043 enable |= TP_ACPI_VIDEO_S_LCD;
5044 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5045 disable |= TP_ACPI_VIDEO_S_LCD;
5046 } else if (strlencmp(cmd, "crt_enable") == 0) {
5047 enable |= TP_ACPI_VIDEO_S_CRT;
5048 } else if (strlencmp(cmd, "crt_disable") == 0) {
5049 disable |= TP_ACPI_VIDEO_S_CRT;
5050 } else if (video_supported == TPACPI_VIDEO_NEW &&
5051 strlencmp(cmd, "dvi_enable") == 0) {
5052 enable |= TP_ACPI_VIDEO_S_DVI;
5053 } else if (video_supported == TPACPI_VIDEO_NEW &&
5054 strlencmp(cmd, "dvi_disable") == 0) {
5055 disable |= TP_ACPI_VIDEO_S_DVI;
5056 } else if (strlencmp(cmd, "auto_enable") == 0) {
5057 res = video_autosw_set(1);
5060 } else if (strlencmp(cmd, "auto_disable") == 0) {
5061 res = video_autosw_set(0);
5064 } else if (strlencmp(cmd, "video_switch") == 0) {
5065 res = video_outputsw_cycle();
5068 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5069 res = video_expand_toggle();
5076 if (enable || disable) {
5077 status = video_outputsw_get();
5080 res = video_outputsw_set((status & ~disable) | enable);
5088 static struct ibm_struct video_driver_data = {
5091 .write = video_write,
5095 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5097 /*************************************************************************
5098 * Keyboard backlight subdriver
5101 static enum led_brightness kbdlight_brightness;
5102 static DEFINE_MUTEX(kbdlight_mutex);
5104 static int kbdlight_set_level(int level)
5111 mutex_lock(&kbdlight_mutex);
5113 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5116 kbdlight_brightness = level;
5118 mutex_unlock(&kbdlight_mutex);
5123 static int kbdlight_get_level(void)
5130 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5136 return status & 0x3;
5139 static bool kbdlight_is_supported(void)
5146 if (!acpi_has_method(hkey_handle, "MLCG")) {
5147 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5151 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5152 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5157 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5161 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5163 * Guessed test for keyboard backlight:
5165 * Machines with backlight keyboard return:
5166 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5167 * b110100010010000000XX - ThinkPad x230
5168 * b010100000010000000XX - ThinkPad x240
5169 * b010100000010000000XX - ThinkPad W541
5170 * (XX is current backlight level)
5172 * Machines without backlight keyboard return:
5173 * b10100001000000000000 - ThinkPad x230
5174 * b10110001000000000000 - ThinkPad E430
5175 * b00000000000000000000 - ThinkPad E450
5177 * Candidate BITs for detection test (XOR):
5178 * b01000000001000000000
5181 return status & BIT(9);
5184 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5185 enum led_brightness brightness)
5187 return kbdlight_set_level(brightness);
5190 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5194 level = kbdlight_get_level();
5201 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5203 .name = "tpacpi::kbd_backlight",
5204 .max_brightness = 2,
5205 .flags = LED_BRIGHT_HW_CHANGED,
5206 .brightness_set_blocking = &kbdlight_sysfs_set,
5207 .brightness_get = &kbdlight_sysfs_get,
5211 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5215 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5217 TPACPI_ACPIHANDLE_INIT(hkey);
5219 if (!kbdlight_is_supported()) {
5220 tp_features.kbdlight = 0;
5221 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5225 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5226 tp_features.kbdlight = 1;
5228 rc = led_classdev_register(&tpacpi_pdev->dev,
5229 &tpacpi_led_kbdlight.led_classdev);
5231 tp_features.kbdlight = 0;
5235 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5236 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5240 static void kbdlight_exit(void)
5242 if (tp_features.kbdlight)
5243 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5246 static int kbdlight_set_level_and_update(int level)
5249 struct led_classdev *led_cdev;
5251 ret = kbdlight_set_level(level);
5252 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5254 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5255 led_cdev->brightness = level;
5260 static int kbdlight_read(struct seq_file *m)
5264 if (!tp_features.kbdlight) {
5265 seq_printf(m, "status:\t\tnot supported\n");
5267 level = kbdlight_get_level();
5269 seq_printf(m, "status:\t\terror %d\n", level);
5271 seq_printf(m, "status:\t\t%d\n", level);
5272 seq_printf(m, "commands:\t0, 1, 2\n");
5278 static int kbdlight_write(char *buf)
5283 if (!tp_features.kbdlight)
5286 while ((cmd = next_cmd(&buf))) {
5287 if (strlencmp(cmd, "0") == 0)
5289 else if (strlencmp(cmd, "1") == 0)
5291 else if (strlencmp(cmd, "2") == 0)
5300 return kbdlight_set_level_and_update(level);
5303 static void kbdlight_suspend(void)
5305 struct led_classdev *led_cdev;
5307 if (!tp_features.kbdlight)
5310 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5311 led_update_brightness(led_cdev);
5312 led_classdev_suspend(led_cdev);
5315 static void kbdlight_resume(void)
5317 if (!tp_features.kbdlight)
5320 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5323 static struct ibm_struct kbdlight_driver_data = {
5325 .read = kbdlight_read,
5326 .write = kbdlight_write,
5327 .suspend = kbdlight_suspend,
5328 .resume = kbdlight_resume,
5329 .exit = kbdlight_exit,
5332 /*************************************************************************
5333 * Light (thinklight) subdriver
5336 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5337 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5339 static int light_get_status(void)
5343 if (tp_features.light_status) {
5344 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5352 static int light_set_status(int status)
5356 if (tp_features.light) {
5358 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5360 TP_CMOS_THINKLIGHT_ON :
5361 TP_CMOS_THINKLIGHT_OFF);
5363 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5366 return (rc) ? 0 : -EIO;
5372 static int light_sysfs_set(struct led_classdev *led_cdev,
5373 enum led_brightness brightness)
5375 return light_set_status((brightness != LED_OFF) ?
5376 TPACPI_LED_ON : TPACPI_LED_OFF);
5379 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5381 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5384 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5386 .name = "tpacpi::thinklight",
5387 .brightness_set_blocking = &light_sysfs_set,
5388 .brightness_get = &light_sysfs_get,
5392 static int __init light_init(struct ibm_init_struct *iibm)
5396 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5398 if (tpacpi_is_ibm()) {
5399 TPACPI_ACPIHANDLE_INIT(ledb);
5400 TPACPI_ACPIHANDLE_INIT(lght);
5402 TPACPI_ACPIHANDLE_INIT(cmos);
5404 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5405 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5407 if (tp_features.light)
5408 /* light status not supported on
5409 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5410 tp_features.light_status =
5411 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5413 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5414 str_supported(tp_features.light),
5415 str_supported(tp_features.light_status));
5417 if (!tp_features.light)
5420 rc = led_classdev_register(&tpacpi_pdev->dev,
5421 &tpacpi_led_thinklight.led_classdev);
5424 tp_features.light = 0;
5425 tp_features.light_status = 0;
5433 static void light_exit(void)
5435 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5438 static int light_read(struct seq_file *m)
5442 if (!tp_features.light) {
5443 seq_printf(m, "status:\t\tnot supported\n");
5444 } else if (!tp_features.light_status) {
5445 seq_printf(m, "status:\t\tunknown\n");
5446 seq_printf(m, "commands:\ton, off\n");
5448 status = light_get_status();
5451 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5452 seq_printf(m, "commands:\ton, off\n");
5458 static int light_write(char *buf)
5463 if (!tp_features.light)
5466 while ((cmd = next_cmd(&buf))) {
5467 if (strlencmp(cmd, "on") == 0) {
5469 } else if (strlencmp(cmd, "off") == 0) {
5475 return light_set_status(newstatus);
5478 static struct ibm_struct light_driver_data = {
5481 .write = light_write,
5485 /*************************************************************************
5489 /* sysfs cmos_command -------------------------------------------------- */
5490 static ssize_t cmos_command_store(struct device *dev,
5491 struct device_attribute *attr,
5492 const char *buf, size_t count)
5494 unsigned long cmos_cmd;
5497 if (parse_strtoul(buf, 21, &cmos_cmd))
5500 res = issue_thinkpad_cmos_command(cmos_cmd);
5501 return (res) ? res : count;
5504 static DEVICE_ATTR_WO(cmos_command);
5506 /* --------------------------------------------------------------------- */
5508 static int __init cmos_init(struct ibm_init_struct *iibm)
5512 vdbg_printk(TPACPI_DBG_INIT,
5513 "initializing cmos commands subdriver\n");
5515 TPACPI_ACPIHANDLE_INIT(cmos);
5517 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5518 str_supported(cmos_handle != NULL));
5520 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5524 return (cmos_handle) ? 0 : 1;
5527 static void cmos_exit(void)
5529 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5532 static int cmos_read(struct seq_file *m)
5534 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5535 R30, R31, T20-22, X20-21 */
5537 seq_printf(m, "status:\t\tnot supported\n");
5539 seq_printf(m, "status:\t\tsupported\n");
5540 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5546 static int cmos_write(char *buf)
5551 while ((cmd = next_cmd(&buf))) {
5552 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5553 cmos_cmd >= 0 && cmos_cmd <= 21) {
5558 res = issue_thinkpad_cmos_command(cmos_cmd);
5566 static struct ibm_struct cmos_driver_data = {
5569 .write = cmos_write,
5573 /*************************************************************************
5577 enum led_access_mode {
5578 TPACPI_LED_NONE = 0,
5579 TPACPI_LED_570, /* 570 */
5580 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5581 TPACPI_LED_NEW, /* all others */
5584 enum { /* For TPACPI_LED_OLD */
5585 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5586 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5587 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5590 static enum led_access_mode led_supported;
5592 static acpi_handle led_handle;
5594 #define TPACPI_LED_NUMLEDS 16
5595 static struct tpacpi_led_classdev *tpacpi_leds;
5596 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5597 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5598 /* there's a limit of 19 chars + NULL before 2.6.26 */
5600 "tpacpi:orange:batt",
5601 "tpacpi:green:batt",
5602 "tpacpi::dock_active",
5603 "tpacpi::bay_active",
5604 "tpacpi::dock_batt",
5605 "tpacpi::unknown_led",
5607 "tpacpi::dock_status1",
5608 "tpacpi::dock_status2",
5609 "tpacpi::unknown_led2",
5610 "tpacpi::unknown_led3",
5611 "tpacpi::thinkvantage",
5613 #define TPACPI_SAFE_LEDS 0x1081U
5615 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5617 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5620 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5624 static int led_get_status(const unsigned int led)
5627 enum led_status_t led_s;
5629 switch (led_supported) {
5630 case TPACPI_LED_570:
5631 if (!acpi_evalf(ec_handle,
5632 &status, "GLED", "dd", 1 << led))
5634 led_s = (status == 0) ?
5639 tpacpi_led_state_cache[led] = led_s;
5648 static int led_set_status(const unsigned int led,
5649 const enum led_status_t ledstatus)
5651 /* off, on, blink. Index is led_status_t */
5652 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5653 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5657 switch (led_supported) {
5658 case TPACPI_LED_570:
5660 if (unlikely(led > 7))
5662 if (unlikely(tpacpi_is_led_restricted(led)))
5664 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5665 (1 << led), led_sled_arg1[ledstatus]))
5668 case TPACPI_LED_OLD:
5669 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5670 if (unlikely(led > 7))
5672 if (unlikely(tpacpi_is_led_restricted(led)))
5674 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5676 rc = ec_write(TPACPI_LED_EC_HLBL,
5677 (ledstatus == TPACPI_LED_BLINK) << led);
5679 rc = ec_write(TPACPI_LED_EC_HLCL,
5680 (ledstatus != TPACPI_LED_OFF) << led);
5682 case TPACPI_LED_NEW:
5684 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5686 if (unlikely(tpacpi_is_led_restricted(led)))
5688 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5689 led, led_led_arg1[ledstatus]))
5697 tpacpi_led_state_cache[led] = ledstatus;
5702 static int led_sysfs_set(struct led_classdev *led_cdev,
5703 enum led_brightness brightness)
5705 struct tpacpi_led_classdev *data = container_of(led_cdev,
5706 struct tpacpi_led_classdev, led_classdev);
5707 enum led_status_t new_state;
5709 if (brightness == LED_OFF)
5710 new_state = TPACPI_LED_OFF;
5711 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5712 new_state = TPACPI_LED_ON;
5714 new_state = TPACPI_LED_BLINK;
5716 return led_set_status(data->led, new_state);
5719 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5720 unsigned long *delay_on, unsigned long *delay_off)
5722 struct tpacpi_led_classdev *data = container_of(led_cdev,
5723 struct tpacpi_led_classdev, led_classdev);
5725 /* Can we choose the flash rate? */
5726 if (*delay_on == 0 && *delay_off == 0) {
5727 /* yes. set them to the hardware blink rate (1 Hz) */
5728 *delay_on = 500; /* ms */
5729 *delay_off = 500; /* ms */
5730 } else if ((*delay_on != 500) || (*delay_off != 500))
5733 return led_set_status(data->led, TPACPI_LED_BLINK);
5736 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5740 struct tpacpi_led_classdev *data = container_of(led_cdev,
5741 struct tpacpi_led_classdev, led_classdev);
5743 rc = led_get_status(data->led);
5745 if (rc == TPACPI_LED_OFF || rc < 0)
5746 rc = LED_OFF; /* no error handling in led class :( */
5753 static void led_exit(void)
5757 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5758 if (tpacpi_leds[i].led_classdev.name)
5759 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5765 static int __init tpacpi_init_led(unsigned int led)
5769 tpacpi_leds[led].led = led;
5771 /* LEDs with no name don't get registered */
5772 if (!tpacpi_led_names[led])
5775 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5776 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5777 if (led_supported == TPACPI_LED_570)
5778 tpacpi_leds[led].led_classdev.brightness_get =
5781 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5783 rc = led_classdev_register(&tpacpi_pdev->dev,
5784 &tpacpi_leds[led].led_classdev);
5786 tpacpi_leds[led].led_classdev.name = NULL;
5791 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5792 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5793 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5794 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5796 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5797 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5798 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5799 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5800 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5801 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5802 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5803 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5805 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5806 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5807 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5808 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5809 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5811 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5812 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5813 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5814 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5816 /* (1) - may have excess leds enabled on MSB */
5818 /* Defaults (order matters, keep last, don't reorder!) */
5820 .vendor = PCI_VENDOR_ID_LENOVO,
5821 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5824 { /* IBM ThinkPads with no EC version string */
5825 .vendor = PCI_VENDOR_ID_IBM,
5826 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5829 { /* IBM ThinkPads with EC version string */
5830 .vendor = PCI_VENDOR_ID_IBM,
5831 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5836 #undef TPACPI_LEDQ_IBM
5837 #undef TPACPI_LEDQ_LNV
5839 static enum led_access_mode __init led_init_detect_mode(void)
5843 if (tpacpi_is_ibm()) {
5845 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5846 if (ACPI_SUCCESS(status))
5847 return TPACPI_LED_570;
5849 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5850 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5851 if (ACPI_SUCCESS(status))
5852 return TPACPI_LED_OLD;
5856 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5857 if (ACPI_SUCCESS(status))
5858 return TPACPI_LED_NEW;
5860 /* R30, R31, and unknown firmwares */
5862 return TPACPI_LED_NONE;
5865 static int __init led_init(struct ibm_init_struct *iibm)
5869 unsigned long useful_leds;
5871 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5873 led_supported = led_init_detect_mode();
5875 if (led_supported != TPACPI_LED_NONE) {
5876 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5877 ARRAY_SIZE(led_useful_qtable));
5881 led_supported = TPACPI_LED_NONE;
5885 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5886 str_supported(led_supported), led_supported);
5888 if (led_supported == TPACPI_LED_NONE)
5891 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5894 pr_err("Out of memory for LED data\n");
5898 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5899 tpacpi_leds[i].led = -1;
5901 if (!tpacpi_is_led_restricted(i) &&
5902 test_bit(i, &useful_leds)) {
5903 rc = tpacpi_init_led(i);
5911 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5912 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
5917 #define str_led_status(s) \
5918 ((s) == TPACPI_LED_OFF ? "off" : \
5919 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5921 static int led_read(struct seq_file *m)
5923 if (!led_supported) {
5924 seq_printf(m, "status:\t\tnot supported\n");
5927 seq_printf(m, "status:\t\tsupported\n");
5929 if (led_supported == TPACPI_LED_570) {
5932 for (i = 0; i < 8; i++) {
5933 status = led_get_status(i);
5936 seq_printf(m, "%d:\t\t%s\n",
5937 i, str_led_status(status));
5941 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5946 static int led_write(char *buf)
5950 enum led_status_t s;
5955 while ((cmd = next_cmd(&buf))) {
5956 if (sscanf(cmd, "%d", &led) != 1)
5959 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5960 tpacpi_leds[led].led < 0)
5963 if (strstr(cmd, "off")) {
5965 } else if (strstr(cmd, "on")) {
5967 } else if (strstr(cmd, "blink")) {
5968 s = TPACPI_LED_BLINK;
5973 rc = led_set_status(led, s);
5981 static struct ibm_struct led_driver_data = {
5988 /*************************************************************************
5992 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5994 #define TPACPI_BEEP_Q1 0x0001
5996 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5997 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5998 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6001 static int __init beep_init(struct ibm_init_struct *iibm)
6003 unsigned long quirks;
6005 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6007 TPACPI_ACPIHANDLE_INIT(beep);
6009 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6010 str_supported(beep_handle != NULL));
6012 quirks = tpacpi_check_quirks(beep_quirk_table,
6013 ARRAY_SIZE(beep_quirk_table));
6015 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6017 return (beep_handle) ? 0 : 1;
6020 static int beep_read(struct seq_file *m)
6023 seq_printf(m, "status:\t\tnot supported\n");
6025 seq_printf(m, "status:\t\tsupported\n");
6026 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6032 static int beep_write(char *buf)
6040 while ((cmd = next_cmd(&buf))) {
6041 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6042 beep_cmd >= 0 && beep_cmd <= 17) {
6046 if (tp_features.beep_needs_two_args) {
6047 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6051 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6060 static struct ibm_struct beep_driver_data = {
6063 .write = beep_write,
6066 /*************************************************************************
6070 enum thermal_access_mode {
6071 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6072 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6073 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6074 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6075 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6078 enum { /* TPACPI_THERMAL_TPEC_* */
6079 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6080 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6081 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6083 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6087 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6088 struct ibm_thermal_sensors_struct {
6089 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6092 static enum thermal_access_mode thermal_read_mode;
6094 /* idx is zero-based */
6095 static int thermal_get_sensor(int idx, s32 *value)
6101 t = TP_EC_THERMAL_TMP0;
6103 switch (thermal_read_mode) {
6104 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6105 case TPACPI_THERMAL_TPEC_16:
6106 if (idx >= 8 && idx <= 15) {
6107 t = TP_EC_THERMAL_TMP8;
6112 case TPACPI_THERMAL_TPEC_8:
6114 if (!acpi_ec_read(t + idx, &tmp))
6116 *value = tmp * 1000;
6121 case TPACPI_THERMAL_ACPI_UPDT:
6123 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6124 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6126 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6128 *value = (t - 2732) * 100;
6133 case TPACPI_THERMAL_ACPI_TMP07:
6135 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6136 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6138 if (t > 127 || t < -127)
6139 t = TP_EC_THERMAL_TMP_NA;
6145 case TPACPI_THERMAL_NONE:
6153 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6164 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6167 for (i = 0 ; i < n; i++) {
6168 res = thermal_get_sensor(i, &s->temp[i]);
6176 static void thermal_dump_all_sensors(void)
6179 struct ibm_thermal_sensors_struct t;
6181 n = thermal_get_sensors(&t);
6185 pr_notice("temperatures (Celsius):");
6187 for (i = 0; i < n; i++) {
6188 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6189 pr_cont(" %d", (int)(t.temp[i] / 1000));
6197 /* sysfs temp##_input -------------------------------------------------- */
6199 static ssize_t thermal_temp_input_show(struct device *dev,
6200 struct device_attribute *attr,
6203 struct sensor_device_attribute *sensor_attr =
6204 to_sensor_dev_attr(attr);
6205 int idx = sensor_attr->index;
6209 res = thermal_get_sensor(idx, &value);
6212 if (value == TPACPI_THERMAL_SENSOR_NA)
6215 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6218 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6219 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6220 thermal_temp_input_show, NULL, _idxB)
6222 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6223 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6224 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6225 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6226 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6227 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6228 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6229 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6230 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6231 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6232 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6233 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6234 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6235 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6236 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6237 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6238 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6241 #define THERMAL_ATTRS(X) \
6242 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6244 static struct attribute *thermal_temp_input_attr[] = {
6264 static const struct attribute_group thermal_temp_input16_group = {
6265 .attrs = thermal_temp_input_attr
6268 static const struct attribute_group thermal_temp_input8_group = {
6269 .attrs = &thermal_temp_input_attr[8]
6272 #undef THERMAL_SENSOR_ATTR_TEMP
6273 #undef THERMAL_ATTRS
6275 /* --------------------------------------------------------------------- */
6277 static int __init thermal_init(struct ibm_init_struct *iibm)
6284 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6286 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6288 if (thinkpad_id.ec_model) {
6290 * Direct EC access mode: sensors at registers
6291 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6292 * non-implemented, thermal sensors return 0x80 when
6297 for (i = 0; i < 8; i++) {
6298 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6304 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6312 /* This is sheer paranoia, but we handle it anyway */
6314 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6315 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6317 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6318 thermal_read_mode = TPACPI_THERMAL_NONE;
6323 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6325 } else if (acpi_tmp7) {
6326 if (tpacpi_is_ibm() &&
6327 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6328 /* 600e/x, 770e, 770x */
6329 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6331 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6332 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6335 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6336 thermal_read_mode = TPACPI_THERMAL_NONE;
6339 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6340 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6343 switch (thermal_read_mode) {
6344 case TPACPI_THERMAL_TPEC_16:
6345 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6346 &thermal_temp_input16_group);
6350 case TPACPI_THERMAL_TPEC_8:
6351 case TPACPI_THERMAL_ACPI_TMP07:
6352 case TPACPI_THERMAL_ACPI_UPDT:
6353 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6354 &thermal_temp_input8_group);
6358 case TPACPI_THERMAL_NONE:
6366 static void thermal_exit(void)
6368 switch (thermal_read_mode) {
6369 case TPACPI_THERMAL_TPEC_16:
6370 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6371 &thermal_temp_input16_group);
6373 case TPACPI_THERMAL_TPEC_8:
6374 case TPACPI_THERMAL_ACPI_TMP07:
6375 case TPACPI_THERMAL_ACPI_UPDT:
6376 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6377 &thermal_temp_input8_group);
6379 case TPACPI_THERMAL_NONE:
6385 static int thermal_read(struct seq_file *m)
6388 struct ibm_thermal_sensors_struct t;
6390 n = thermal_get_sensors(&t);
6391 if (unlikely(n < 0))
6394 seq_printf(m, "temperatures:\t");
6397 for (i = 0; i < (n - 1); i++)
6398 seq_printf(m, "%d ", t.temp[i] / 1000);
6399 seq_printf(m, "%d\n", t.temp[i] / 1000);
6401 seq_printf(m, "not supported\n");
6406 static struct ibm_struct thermal_driver_data = {
6408 .read = thermal_read,
6409 .exit = thermal_exit,
6412 /*************************************************************************
6413 * Backlight/brightness subdriver
6416 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6419 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6420 * CMOS NVRAM byte 0x5E, bits 0-3.
6422 * EC HBRV (0x31) has the following layout
6423 * Bit 7: unknown function
6424 * Bit 6: unknown function
6425 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6426 * Bit 4: must be set to zero to avoid problems
6427 * Bit 3-0: backlight brightness level
6429 * brightness_get_raw returns status data in the HBRV layout
6431 * WARNING: The X61 has been verified to use HBRV for something else, so
6432 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6433 * testing on the very early *60 Lenovo models...
6437 TP_EC_BACKLIGHT = 0x31,
6439 /* TP_EC_BACKLIGHT bitmasks */
6440 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6441 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6442 TP_EC_BACKLIGHT_MAPSW = 0x20,
6445 enum tpacpi_brightness_access_mode {
6446 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6447 TPACPI_BRGHT_MODE_EC, /* EC control */
6448 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6449 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6450 TPACPI_BRGHT_MODE_MAX
6453 static struct backlight_device *ibm_backlight_device;
6455 static enum tpacpi_brightness_access_mode brightness_mode =
6456 TPACPI_BRGHT_MODE_MAX;
6458 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6460 static struct mutex brightness_mutex;
6462 /* NVRAM brightness access,
6463 * call with brightness_mutex held! */
6464 static unsigned int tpacpi_brightness_nvram_get(void)
6468 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6469 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6470 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6471 lnvram &= bright_maxlvl;
6476 static void tpacpi_brightness_checkpoint_nvram(void)
6481 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6484 vdbg_printk(TPACPI_DBG_BRGHT,
6485 "trying to checkpoint backlight level to NVRAM...\n");
6487 if (mutex_lock_killable(&brightness_mutex) < 0)
6490 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6492 lec &= TP_EC_BACKLIGHT_LVLMSK;
6493 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6495 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6496 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6497 /* NVRAM needs update */
6498 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6499 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6501 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6502 dbg_printk(TPACPI_DBG_BRGHT,
6503 "updated NVRAM backlight level to %u (0x%02x)\n",
6504 (unsigned int) lec, (unsigned int) b_nvram);
6506 vdbg_printk(TPACPI_DBG_BRGHT,
6507 "NVRAM backlight level already is %u (0x%02x)\n",
6508 (unsigned int) lec, (unsigned int) b_nvram);
6511 mutex_unlock(&brightness_mutex);
6515 /* call with brightness_mutex held! */
6516 static int tpacpi_brightness_get_raw(int *status)
6520 switch (brightness_mode) {
6521 case TPACPI_BRGHT_MODE_UCMS_STEP:
6522 *status = tpacpi_brightness_nvram_get();
6524 case TPACPI_BRGHT_MODE_EC:
6525 case TPACPI_BRGHT_MODE_ECNVRAM:
6526 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6535 /* call with brightness_mutex held! */
6536 /* do NOT call with illegal backlight level value */
6537 static int tpacpi_brightness_set_ec(unsigned int value)
6541 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6544 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6545 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6546 (value & TP_EC_BACKLIGHT_LVLMSK))))
6552 /* call with brightness_mutex held! */
6553 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6556 unsigned int current_value, i;
6558 current_value = tpacpi_brightness_nvram_get();
6560 if (value == current_value)
6563 cmos_cmd = (value > current_value) ?
6564 TP_CMOS_BRIGHTNESS_UP :
6565 TP_CMOS_BRIGHTNESS_DOWN;
6566 inc = (value > current_value) ? 1 : -1;
6568 for (i = current_value; i != value; i += inc)
6569 if (issue_thinkpad_cmos_command(cmos_cmd))
6575 /* May return EINTR which can always be mapped to ERESTARTSYS */
6576 static int brightness_set(unsigned int value)
6580 if (value > bright_maxlvl)
6583 vdbg_printk(TPACPI_DBG_BRGHT,
6584 "set backlight level to %d\n", value);
6586 res = mutex_lock_killable(&brightness_mutex);
6590 switch (brightness_mode) {
6591 case TPACPI_BRGHT_MODE_EC:
6592 case TPACPI_BRGHT_MODE_ECNVRAM:
6593 res = tpacpi_brightness_set_ec(value);
6595 case TPACPI_BRGHT_MODE_UCMS_STEP:
6596 res = tpacpi_brightness_set_ucmsstep(value);
6602 mutex_unlock(&brightness_mutex);
6606 /* sysfs backlight class ----------------------------------------------- */
6608 static int brightness_update_status(struct backlight_device *bd)
6610 unsigned int level =
6611 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6612 bd->props.power == FB_BLANK_UNBLANK) ?
6613 bd->props.brightness : 0;
6615 dbg_printk(TPACPI_DBG_BRGHT,
6616 "backlight: attempt to set level to %d\n",
6619 /* it is the backlight class's job (caller) to handle
6620 * EINTR and other errors properly */
6621 return brightness_set(level);
6624 static int brightness_get(struct backlight_device *bd)
6628 res = mutex_lock_killable(&brightness_mutex);
6632 res = tpacpi_brightness_get_raw(&status);
6634 mutex_unlock(&brightness_mutex);
6639 return status & TP_EC_BACKLIGHT_LVLMSK;
6642 static void tpacpi_brightness_notify_change(void)
6644 backlight_force_update(ibm_backlight_device,
6645 BACKLIGHT_UPDATE_HOTKEY);
6648 static const struct backlight_ops ibm_backlight_data = {
6649 .get_brightness = brightness_get,
6650 .update_status = brightness_update_status,
6653 /* --------------------------------------------------------------------- */
6656 * Call _BCL method of video device. On some ThinkPads this will
6657 * switch the firmware to the ACPI brightness control mode.
6660 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6662 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6663 union acpi_object *obj;
6664 struct acpi_device *device, *child;
6667 if (acpi_bus_get_device(handle, &device))
6671 list_for_each_entry(child, &device->children, node) {
6672 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6674 if (ACPI_FAILURE(status))
6677 obj = (union acpi_object *)buffer.pointer;
6678 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6679 pr_err("Unknown _BCL data, please report this to %s\n",
6683 rc = obj->package.count;
6688 kfree(buffer.pointer);
6694 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6696 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6698 acpi_handle video_device;
6701 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6703 bcl_levels = tpacpi_query_bcl_levels(video_device);
6705 tp_features.bright_acpimode = (bcl_levels > 0);
6707 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6711 * These are only useful for models that have only one possibility
6712 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6715 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6716 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6717 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6719 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6720 /* Models with ATI GPUs known to require ECNVRAM mode */
6721 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6723 /* Models with ATI GPUs that can use ECNVRAM */
6724 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6725 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6726 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6727 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6729 /* Models with Intel Extreme Graphics 2 */
6730 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6731 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6732 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6734 /* Models with Intel GMA900 */
6735 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6736 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6737 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6741 * Returns < 0 for error, otherwise sets tp_features.bright_*
6742 * and bright_maxlvl.
6744 static void __init tpacpi_detect_brightness_capabilities(void)
6748 vdbg_printk(TPACPI_DBG_INIT,
6749 "detecting firmware brightness interface capabilities\n");
6751 /* we could run a quirks check here (same table used by
6752 * brightness_init) if needed */
6755 * We always attempt to detect acpi support, so as to switch
6756 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6757 * going to publish a backlight interface
6759 b = tpacpi_check_std_acpi_brightness_support();
6769 tp_features.bright_unkfw = 1;
6770 bright_maxlvl = b - 1;
6772 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6775 static int __init brightness_init(struct ibm_init_struct *iibm)
6777 struct backlight_properties props;
6779 unsigned long quirks;
6781 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6783 mutex_init(&brightness_mutex);
6785 quirks = tpacpi_check_quirks(brightness_quirk_table,
6786 ARRAY_SIZE(brightness_quirk_table));
6788 /* tpacpi_detect_brightness_capabilities() must have run already */
6790 /* if it is unknown, we don't handle it: it wouldn't be safe */
6791 if (tp_features.bright_unkfw)
6794 if (!brightness_enable) {
6795 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6796 "brightness support disabled by module parameter\n");
6800 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6801 if (brightness_enable > 1) {
6802 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6804 } else if (brightness_enable == 1) {
6805 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6808 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6809 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
6813 * Check for module parameter bogosity, note that we
6814 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6815 * able to detect "unspecified"
6817 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6820 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6821 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6822 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6823 if (quirks & TPACPI_BRGHT_Q_EC)
6824 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6826 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6828 dbg_printk(TPACPI_DBG_BRGHT,
6829 "driver auto-selected brightness_mode=%d\n",
6834 if (!tpacpi_is_ibm() &&
6835 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6836 brightness_mode == TPACPI_BRGHT_MODE_EC))
6839 if (tpacpi_brightness_get_raw(&b) < 0)
6842 memset(&props, 0, sizeof(struct backlight_properties));
6843 props.type = BACKLIGHT_PLATFORM;
6844 props.max_brightness = bright_maxlvl;
6845 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6846 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6848 &ibm_backlight_data,
6850 if (IS_ERR(ibm_backlight_device)) {
6851 int rc = PTR_ERR(ibm_backlight_device);
6852 ibm_backlight_device = NULL;
6853 pr_err("Could not register backlight device\n");
6856 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6857 "brightness is supported\n");
6859 if (quirks & TPACPI_BRGHT_Q_ASK) {
6860 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6862 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6866 /* Added by mistake in early 2007. Probably useless, but it could
6867 * be working around some unknown firmware problem where the value
6868 * read at startup doesn't match the real hardware state... so leave
6869 * it in place just in case */
6870 backlight_update_status(ibm_backlight_device);
6872 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6873 "brightness: registering brightness hotkeys as change notification\n");
6874 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6875 | TP_ACPI_HKEY_BRGHTUP_MASK
6876 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6880 static void brightness_suspend(void)
6882 tpacpi_brightness_checkpoint_nvram();
6885 static void brightness_shutdown(void)
6887 tpacpi_brightness_checkpoint_nvram();
6890 static void brightness_exit(void)
6892 if (ibm_backlight_device) {
6893 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6894 "calling backlight_device_unregister()\n");
6895 backlight_device_unregister(ibm_backlight_device);
6898 tpacpi_brightness_checkpoint_nvram();
6901 static int brightness_read(struct seq_file *m)
6905 level = brightness_get(NULL);
6907 seq_printf(m, "level:\t\tunreadable\n");
6909 seq_printf(m, "level:\t\t%d\n", level);
6910 seq_printf(m, "commands:\tup, down\n");
6911 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6918 static int brightness_write(char *buf)
6924 level = brightness_get(NULL);
6928 while ((cmd = next_cmd(&buf))) {
6929 if (strlencmp(cmd, "up") == 0) {
6930 if (level < bright_maxlvl)
6932 } else if (strlencmp(cmd, "down") == 0) {
6935 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6936 level >= 0 && level <= bright_maxlvl) {
6942 tpacpi_disclose_usertask("procfs brightness",
6943 "set level to %d\n", level);
6946 * Now we know what the final level should be, so we try to set it.
6947 * Doing it this way makes the syscall restartable in case of EINTR
6949 rc = brightness_set(level);
6950 if (!rc && ibm_backlight_device)
6951 backlight_force_update(ibm_backlight_device,
6952 BACKLIGHT_UPDATE_SYSFS);
6953 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6956 static struct ibm_struct brightness_driver_data = {
6957 .name = "brightness",
6958 .read = brightness_read,
6959 .write = brightness_write,
6960 .exit = brightness_exit,
6961 .suspend = brightness_suspend,
6962 .shutdown = brightness_shutdown,
6965 /*************************************************************************
6970 * IBM ThinkPads have a simple volume controller with MUTE gating.
6971 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6973 * Since the *61 series (and probably also the later *60 series), Lenovo
6974 * ThinkPads only implement the MUTE gate.
6977 * Bit 6: MUTE (1 mutes sound)
6979 * Other bits should be zero as far as we know.
6981 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6982 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6983 * such as bit 7 which is used to detect repeated presses of MUTE,
6984 * and we leave them unchanged.
6986 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6987 * in response to user input. Unfortunately, this rarely works well.
6988 * The laptop changes the state of its internal MUTE gate and, on some
6989 * models, sends KEY_MUTE, causing any user code that responds to the
6990 * mute button to get confused. The hardware MUTE gate is also
6991 * unnecessary, since user code can handle the mute button without
6992 * kernel or EC help.
6994 * To avoid confusing userspace, we simply disable all EC-based mute
6995 * and volume controls when possible.
6998 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7000 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7001 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7002 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7004 #if SNDRV_CARDS <= 32
7005 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7007 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7009 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7010 static char *alsa_id = "ThinkPadEC";
7011 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7013 struct tpacpi_alsa_data {
7014 struct snd_card *card;
7015 struct snd_ctl_elem_id *ctl_mute_id;
7016 struct snd_ctl_elem_id *ctl_vol_id;
7019 static struct snd_card *alsa_card;
7024 /* TP_EC_AUDIO bits */
7025 TP_EC_AUDIO_MUTESW = 6,
7027 /* TP_EC_AUDIO bitmasks */
7028 TP_EC_AUDIO_LVL_MSK = 0x0F,
7029 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7031 /* Maximum volume */
7032 TP_EC_VOLUME_MAX = 14,
7035 enum tpacpi_volume_access_mode {
7036 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7037 TPACPI_VOL_MODE_EC, /* Pure EC control */
7038 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7039 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7043 enum tpacpi_volume_capabilities {
7044 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7045 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7046 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7050 enum tpacpi_mute_btn_mode {
7051 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7052 /* We don't know what mode 1 is. */
7053 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7054 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7057 static enum tpacpi_volume_access_mode volume_mode =
7058 TPACPI_VOL_MODE_MAX;
7060 static enum tpacpi_volume_capabilities volume_capabilities;
7061 static bool volume_control_allowed;
7062 static bool software_mute_requested = true;
7063 static bool software_mute_active;
7064 static int software_mute_orig_mode;
7067 * Used to syncronize writers to TP_EC_AUDIO and
7068 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7070 static struct mutex volume_mutex;
7072 static void tpacpi_volume_checkpoint_nvram(void)
7078 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7080 if (!volume_control_allowed)
7082 if (software_mute_active)
7085 vdbg_printk(TPACPI_DBG_MIXER,
7086 "trying to checkpoint mixer state to NVRAM...\n");
7088 if (tp_features.mixer_no_level_control)
7089 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7091 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7093 if (mutex_lock_killable(&volume_mutex) < 0)
7096 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7099 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7101 if (lec != (b_nvram & ec_mask)) {
7102 /* NVRAM needs update */
7103 b_nvram &= ~ec_mask;
7105 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7106 dbg_printk(TPACPI_DBG_MIXER,
7107 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7108 (unsigned int) lec, (unsigned int) b_nvram);
7110 vdbg_printk(TPACPI_DBG_MIXER,
7111 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7112 (unsigned int) lec, (unsigned int) b_nvram);
7116 mutex_unlock(&volume_mutex);
7119 static int volume_get_status_ec(u8 *status)
7123 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7128 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7133 static int volume_get_status(u8 *status)
7135 return volume_get_status_ec(status);
7138 static int volume_set_status_ec(const u8 status)
7140 if (!acpi_ec_write(TP_EC_AUDIO, status))
7143 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7146 * On X200s, and possibly on others, it can take a while for
7147 * reads to become correct.
7154 static int volume_set_status(const u8 status)
7156 return volume_set_status_ec(status);
7159 /* returns < 0 on error, 0 on no change, 1 on change */
7160 static int __volume_set_mute_ec(const bool mute)
7165 if (mutex_lock_killable(&volume_mutex) < 0)
7168 rc = volume_get_status_ec(&s);
7172 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7173 s & ~TP_EC_AUDIO_MUTESW_MSK;
7176 rc = volume_set_status_ec(n);
7182 mutex_unlock(&volume_mutex);
7186 static int volume_alsa_set_mute(const bool mute)
7188 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7189 (mute) ? "" : "un");
7190 return __volume_set_mute_ec(mute);
7193 static int volume_set_mute(const bool mute)
7197 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7198 (mute) ? "" : "un");
7200 rc = __volume_set_mute_ec(mute);
7201 return (rc < 0) ? rc : 0;
7204 /* returns < 0 on error, 0 on no change, 1 on change */
7205 static int __volume_set_volume_ec(const u8 vol)
7210 if (vol > TP_EC_VOLUME_MAX)
7213 if (mutex_lock_killable(&volume_mutex) < 0)
7216 rc = volume_get_status_ec(&s);
7220 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7223 rc = volume_set_status_ec(n);
7229 mutex_unlock(&volume_mutex);
7233 static int volume_set_software_mute(bool startup)
7237 if (!tpacpi_is_lenovo())
7241 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7245 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7246 "Initial HAUM setting was %d\n",
7247 software_mute_orig_mode);
7250 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7251 (int)TP_EC_MUTE_BTN_NONE))
7254 if (result != TP_EC_MUTE_BTN_NONE)
7255 pr_warn("Unexpected SAUM result %d\n",
7259 * In software mute mode, the standard codec controls take
7260 * precendence, so we unmute the ThinkPad HW switch at
7261 * startup. Just on case there are SAUM-capable ThinkPads
7262 * with level controls, set max HW volume as well.
7264 if (tp_features.mixer_no_level_control)
7265 result = volume_set_mute(false);
7267 result = volume_set_status(TP_EC_VOLUME_MAX);
7270 pr_warn("Failed to unmute the HW mute switch\n");
7275 static void volume_exit_software_mute(void)
7279 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7280 || r != software_mute_orig_mode)
7281 pr_warn("Failed to restore mute mode\n");
7284 static int volume_alsa_set_volume(const u8 vol)
7286 dbg_printk(TPACPI_DBG_MIXER,
7287 "ALSA: trying to set volume level to %hu\n", vol);
7288 return __volume_set_volume_ec(vol);
7291 static void volume_alsa_notify_change(void)
7293 struct tpacpi_alsa_data *d;
7295 if (alsa_card && alsa_card->private_data) {
7296 d = alsa_card->private_data;
7298 snd_ctl_notify(alsa_card,
7299 SNDRV_CTL_EVENT_MASK_VALUE,
7302 snd_ctl_notify(alsa_card,
7303 SNDRV_CTL_EVENT_MASK_VALUE,
7308 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7309 struct snd_ctl_elem_info *uinfo)
7311 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7313 uinfo->value.integer.min = 0;
7314 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7318 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7319 struct snd_ctl_elem_value *ucontrol)
7324 rc = volume_get_status(&s);
7328 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7332 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7333 struct snd_ctl_elem_value *ucontrol)
7335 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7336 ucontrol->value.integer.value[0]);
7337 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7340 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7342 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7343 struct snd_ctl_elem_value *ucontrol)
7348 rc = volume_get_status(&s);
7352 ucontrol->value.integer.value[0] =
7353 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7357 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7358 struct snd_ctl_elem_value *ucontrol)
7360 tpacpi_disclose_usertask("ALSA", "%smute\n",
7361 ucontrol->value.integer.value[0] ?
7363 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7366 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7368 .name = "Console Playback Volume",
7370 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7371 .info = volume_alsa_vol_info,
7372 .get = volume_alsa_vol_get,
7375 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7376 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7377 .name = "Console Playback Switch",
7379 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7380 .info = volume_alsa_mute_info,
7381 .get = volume_alsa_mute_get,
7384 static void volume_suspend(void)
7386 tpacpi_volume_checkpoint_nvram();
7389 static void volume_resume(void)
7391 if (software_mute_active) {
7392 if (volume_set_software_mute(false) < 0)
7393 pr_warn("Failed to restore software mute\n");
7395 volume_alsa_notify_change();
7399 static void volume_shutdown(void)
7401 tpacpi_volume_checkpoint_nvram();
7404 static void volume_exit(void)
7407 snd_card_free(alsa_card);
7411 tpacpi_volume_checkpoint_nvram();
7413 if (software_mute_active)
7414 volume_exit_software_mute();
7417 static int __init volume_create_alsa_mixer(void)
7419 struct snd_card *card;
7420 struct tpacpi_alsa_data *data;
7421 struct snd_kcontrol *ctl_vol;
7422 struct snd_kcontrol *ctl_mute;
7425 rc = snd_card_new(&tpacpi_pdev->dev,
7426 alsa_index, alsa_id, THIS_MODULE,
7427 sizeof(struct tpacpi_alsa_data), &card);
7428 if (rc < 0 || !card) {
7429 pr_err("Failed to create ALSA card structures: %d\n", rc);
7433 BUG_ON(!card->private_data);
7434 data = card->private_data;
7437 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7438 sizeof(card->driver));
7439 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7440 sizeof(card->shortname));
7441 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7442 (thinkpad_id.ec_version_str) ?
7443 thinkpad_id.ec_version_str : "(unknown)");
7444 snprintf(card->longname, sizeof(card->longname),
7445 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7446 (thinkpad_id.ec_version_str) ?
7447 thinkpad_id.ec_version_str : "unknown");
7449 if (volume_control_allowed) {
7450 volume_alsa_control_vol.put = volume_alsa_vol_put;
7451 volume_alsa_control_vol.access =
7452 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7454 volume_alsa_control_mute.put = volume_alsa_mute_put;
7455 volume_alsa_control_mute.access =
7456 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7459 if (!tp_features.mixer_no_level_control) {
7460 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7461 rc = snd_ctl_add(card, ctl_vol);
7463 pr_err("Failed to create ALSA volume control: %d\n",
7467 data->ctl_vol_id = &ctl_vol->id;
7470 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7471 rc = snd_ctl_add(card, ctl_mute);
7473 pr_err("Failed to create ALSA mute control: %d\n", rc);
7476 data->ctl_mute_id = &ctl_mute->id;
7478 rc = snd_card_register(card);
7480 pr_err("Failed to register ALSA card: %d\n", rc);
7488 snd_card_free(card);
7492 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7493 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7495 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7496 /* Whitelist volume level on all IBM by default */
7497 { .vendor = PCI_VENDOR_ID_IBM,
7498 .bios = TPACPI_MATCH_ANY,
7499 .ec = TPACPI_MATCH_ANY,
7500 .quirks = TPACPI_VOL_Q_LEVEL },
7502 /* Lenovo models with volume control (needs confirmation) */
7503 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7504 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7505 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7506 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7507 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7508 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7509 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7511 /* Whitelist mute-only on all Lenovo by default */
7512 { .vendor = PCI_VENDOR_ID_LENOVO,
7513 .bios = TPACPI_MATCH_ANY,
7514 .ec = TPACPI_MATCH_ANY,
7515 .quirks = TPACPI_VOL_Q_MUTEONLY }
7518 static int __init volume_init(struct ibm_init_struct *iibm)
7520 unsigned long quirks;
7523 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7525 mutex_init(&volume_mutex);
7528 * Check for module parameter bogosity, note that we
7529 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7530 * able to detect "unspecified"
7532 if (volume_mode > TPACPI_VOL_MODE_MAX)
7535 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7536 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7541 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7545 * The ALSA mixer is our primary interface.
7546 * When disabled, don't install the subdriver at all
7549 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7550 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7554 quirks = tpacpi_check_quirks(volume_quirk_table,
7555 ARRAY_SIZE(volume_quirk_table));
7557 switch (volume_capabilities) {
7558 case TPACPI_VOL_CAP_AUTO:
7559 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7560 tp_features.mixer_no_level_control = 1;
7561 else if (quirks & TPACPI_VOL_Q_LEVEL)
7562 tp_features.mixer_no_level_control = 0;
7564 return 1; /* no mixer */
7566 case TPACPI_VOL_CAP_VOLMUTE:
7567 tp_features.mixer_no_level_control = 0;
7569 case TPACPI_VOL_CAP_MUTEONLY:
7570 tp_features.mixer_no_level_control = 1;
7576 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7577 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7578 "using user-supplied volume_capabilities=%d\n",
7579 volume_capabilities);
7581 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7582 volume_mode == TPACPI_VOL_MODE_MAX) {
7583 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7585 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7586 "driver auto-selected volume_mode=%d\n",
7589 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7590 "using user-supplied volume_mode=%d\n",
7594 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7595 "mute is supported, volume control is %s\n",
7596 str_supported(!tp_features.mixer_no_level_control));
7598 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7599 software_mute_active = true;
7601 rc = volume_create_alsa_mixer();
7603 pr_err("Could not create the ALSA mixer interface\n");
7607 pr_info("Console audio control enabled, mode: %s\n",
7608 (volume_control_allowed) ?
7609 "override (read/write)" :
7610 "monitor (read only)");
7613 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7614 "registering volume hotkeys as change notification\n");
7615 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7616 | TP_ACPI_HKEY_VOLUP_MASK
7617 | TP_ACPI_HKEY_VOLDWN_MASK
7618 | TP_ACPI_HKEY_MUTE_MASK);
7623 static int volume_read(struct seq_file *m)
7627 if (volume_get_status(&status) < 0) {
7628 seq_printf(m, "level:\t\tunreadable\n");
7630 if (tp_features.mixer_no_level_control)
7631 seq_printf(m, "level:\t\tunsupported\n");
7633 seq_printf(m, "level:\t\t%d\n",
7634 status & TP_EC_AUDIO_LVL_MSK);
7636 seq_printf(m, "mute:\t\t%s\n",
7637 onoff(status, TP_EC_AUDIO_MUTESW));
7639 if (volume_control_allowed) {
7640 seq_printf(m, "commands:\tunmute, mute\n");
7641 if (!tp_features.mixer_no_level_control) {
7642 seq_printf(m, "commands:\tup, down\n");
7643 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7652 static int volume_write(char *buf)
7655 u8 new_level, new_mute;
7661 * We do allow volume control at driver startup, so that the
7662 * user can set initial state through the volume=... parameter hack.
7664 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7665 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7666 tp_warned.volume_ctrl_forbidden = 1;
7667 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7668 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7673 rc = volume_get_status(&s);
7677 new_level = s & TP_EC_AUDIO_LVL_MSK;
7678 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7680 while ((cmd = next_cmd(&buf))) {
7681 if (!tp_features.mixer_no_level_control) {
7682 if (strlencmp(cmd, "up") == 0) {
7685 else if (new_level < TP_EC_VOLUME_MAX)
7688 } else if (strlencmp(cmd, "down") == 0) {
7691 else if (new_level > 0)
7694 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7695 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7700 if (strlencmp(cmd, "mute") == 0)
7701 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7702 else if (strlencmp(cmd, "unmute") == 0)
7708 if (tp_features.mixer_no_level_control) {
7709 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7710 new_mute ? "" : "un");
7711 rc = volume_set_mute(!!new_mute);
7713 tpacpi_disclose_usertask("procfs volume",
7714 "%smute and set level to %d\n",
7715 new_mute ? "" : "un", new_level);
7716 rc = volume_set_status(new_mute | new_level);
7718 volume_alsa_notify_change();
7720 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7723 static struct ibm_struct volume_driver_data = {
7725 .read = volume_read,
7726 .write = volume_write,
7727 .exit = volume_exit,
7728 .suspend = volume_suspend,
7729 .resume = volume_resume,
7730 .shutdown = volume_shutdown,
7733 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7735 #define alsa_card NULL
7737 static inline void volume_alsa_notify_change(void)
7741 static int __init volume_init(struct ibm_init_struct *iibm)
7743 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7748 static struct ibm_struct volume_driver_data = {
7752 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7754 /*************************************************************************
7761 * TPACPI_FAN_RD_ACPI_GFAN:
7762 * ACPI GFAN method: returns fan level
7764 * see TPACPI_FAN_WR_ACPI_SFAN
7765 * EC 0x2f (HFSP) not available if GFAN exists
7767 * TPACPI_FAN_WR_ACPI_SFAN:
7768 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7770 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7773 * TPACPI_FAN_WR_TPEC:
7774 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7775 * Supported on almost all ThinkPads
7777 * Fan speed changes of any sort (including those caused by the
7778 * disengaged mode) are usually done slowly by the firmware as the
7779 * maximum amount of fan duty cycle change per second seems to be
7782 * Reading is not available if GFAN exists.
7783 * Writing is not available if SFAN exists.
7786 * 7 automatic mode engaged;
7787 * (default operation mode of the ThinkPad)
7788 * fan level is ignored in this mode.
7789 * 6 full speed mode (takes precedence over bit 7);
7790 * not available on all thinkpads. May disable
7791 * the tachometer while the fan controller ramps up
7792 * the speed (which can take up to a few *minutes*).
7793 * Speeds up fan to 100% duty-cycle, which is far above
7794 * the standard RPM levels. It is not impossible that
7795 * it could cause hardware damage.
7796 * 5-3 unused in some models. Extra bits for fan level
7797 * in others, but still useless as all values above
7798 * 7 map to the same speed as level 7 in these models.
7799 * 2-0 fan level (0..7 usually)
7801 * 0x07 = max (set when temperatures critical)
7802 * Some ThinkPads may have other levels, see
7803 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7805 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7806 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7807 * does so, its initial value is meaningless (0x07).
7809 * For firmware bugs, refer to:
7810 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7814 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7815 * Main fan tachometer reading (in RPM)
7817 * This register is present on all ThinkPads with a new-style EC, and
7818 * it is known not to be present on the A21m/e, and T22, as there is
7819 * something else in offset 0x84 according to the ACPI DSDT. Other
7820 * ThinkPads from this same time period (and earlier) probably lack the
7821 * tachometer as well.
7823 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7824 * was never fixed by IBM to report the EC firmware version string
7825 * probably support the tachometer (like the early X models), so
7826 * detecting it is quite hard. We need more data to know for sure.
7828 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7831 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7834 * For firmware bugs, refer to:
7835 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7839 * ThinkPad EC register 0x31 bit 0 (only on select models)
7841 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7842 * show the speed of the main fan. When bit 0 of EC register 0x31
7843 * is one, the tachometer registers show the speed of the auxiliary
7846 * Fan control seems to affect both fans, regardless of the state
7849 * So far, only the firmware for the X60/X61 non-tablet versions
7850 * seem to support this (firmware TP-7M).
7852 * TPACPI_FAN_WR_ACPI_FANS:
7853 * ThinkPad X31, X40, X41. Not available in the X60.
7855 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7856 * high speed. ACPI DSDT seems to map these three speeds to levels
7857 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7858 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7860 * The speeds are stored on handles
7861 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7863 * There are three default speed sets, accessible as handles:
7864 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7866 * ACPI DSDT switches which set is in use depending on various
7869 * TPACPI_FAN_WR_TPEC is also available and should be used to
7870 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7871 * but the ACPI tables just mention level 7.
7874 enum { /* Fan control constants */
7875 fan_status_offset = 0x2f, /* EC register 0x2f */
7876 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7877 * 0x84 must be read before 0x85 */
7878 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7879 bit 0 selects which fan is active */
7881 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7882 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7884 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7887 enum fan_status_access_mode {
7888 TPACPI_FAN_NONE = 0, /* No fan status or control */
7889 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7890 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7893 enum fan_control_access_mode {
7894 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7895 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7896 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7897 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7900 enum fan_control_commands {
7901 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7902 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7903 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7904 * and also watchdog cmd */
7907 static bool fan_control_allowed;
7909 static enum fan_status_access_mode fan_status_access_mode;
7910 static enum fan_control_access_mode fan_control_access_mode;
7911 static enum fan_control_commands fan_control_commands;
7913 static u8 fan_control_initial_status;
7914 static u8 fan_control_desired_level;
7915 static u8 fan_control_resume_level;
7916 static int fan_watchdog_maxinterval;
7918 static struct mutex fan_mutex;
7920 static void fan_watchdog_fire(struct work_struct *ignored);
7921 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7923 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7924 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7925 "\\FSPD", /* 600e/x, 770e, 770x */
7927 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7928 "JFNS", /* 770x-JL */
7932 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7933 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7934 * be in auto mode (0x80).
7936 * This is corrected by any write to HFSP either by the driver, or
7939 * We assume 0x07 really means auto mode while this quirk is active,
7940 * as this is far more likely than the ThinkPad being in level 7,
7941 * which is only used by the firmware during thermal emergencies.
7943 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7944 * TP-70 (T43, R52), which are known to be buggy.
7947 static void fan_quirk1_setup(void)
7949 if (fan_control_initial_status == 0x07) {
7950 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
7951 tp_features.fan_ctrl_status_undef = 1;
7955 static void fan_quirk1_handle(u8 *fan_status)
7957 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7958 if (*fan_status != fan_control_initial_status) {
7959 /* something changed the HFSP regisnter since
7960 * driver init time, so it is not undefined
7962 tp_features.fan_ctrl_status_undef = 0;
7964 /* Return most likely status. In fact, it
7965 * might be the only possible status */
7966 *fan_status = TP_EC_FAN_AUTO;
7971 /* Select main fan on X60/X61, NOOP on others */
7972 static bool fan_select_fan1(void)
7974 if (tp_features.second_fan) {
7977 if (ec_read(fan_select_offset, &val) < 0)
7980 if (ec_write(fan_select_offset, val) < 0)
7986 /* Select secondary fan on X60/X61 */
7987 static bool fan_select_fan2(void)
7991 if (!tp_features.second_fan)
7994 if (ec_read(fan_select_offset, &val) < 0)
7997 if (ec_write(fan_select_offset, val) < 0)
8004 * Call with fan_mutex held
8006 static void fan_update_desired_level(u8 status)
8009 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8011 fan_control_desired_level = 7;
8013 fan_control_desired_level = status;
8017 static int fan_get_status(u8 *status)
8022 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8024 switch (fan_status_access_mode) {
8025 case TPACPI_FAN_RD_ACPI_GFAN: {
8026 /* 570, 600e/x, 770e, 770x */
8029 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8033 *status = res & 0x07;
8037 case TPACPI_FAN_RD_TPEC:
8038 /* all except 570, 600e/x, 770e, 770x */
8039 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8042 if (likely(status)) {
8044 fan_quirk1_handle(status);
8056 static int fan_get_status_safe(u8 *status)
8061 if (mutex_lock_killable(&fan_mutex))
8062 return -ERESTARTSYS;
8063 rc = fan_get_status(&s);
8065 fan_update_desired_level(s);
8066 mutex_unlock(&fan_mutex);
8076 static int fan_get_speed(unsigned int *speed)
8080 switch (fan_status_access_mode) {
8081 case TPACPI_FAN_RD_TPEC:
8082 /* all except 570, 600e/x, 770e, 770x */
8083 if (unlikely(!fan_select_fan1()))
8085 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8086 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8090 *speed = (hi << 8) | lo;
8101 static int fan2_get_speed(unsigned int *speed)
8106 switch (fan_status_access_mode) {
8107 case TPACPI_FAN_RD_TPEC:
8108 /* all except 570, 600e/x, 770e, 770x */
8109 if (unlikely(!fan_select_fan2()))
8111 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8112 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8113 fan_select_fan1(); /* play it safe */
8118 *speed = (hi << 8) | lo;
8129 static int fan_set_level(int level)
8131 if (!fan_control_allowed)
8134 switch (fan_control_access_mode) {
8135 case TPACPI_FAN_WR_ACPI_SFAN:
8136 if (level >= 0 && level <= 7) {
8137 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8143 case TPACPI_FAN_WR_ACPI_FANS:
8144 case TPACPI_FAN_WR_TPEC:
8145 if (!(level & TP_EC_FAN_AUTO) &&
8146 !(level & TP_EC_FAN_FULLSPEED) &&
8147 ((level < 0) || (level > 7)))
8150 /* safety net should the EC not support AUTO
8151 * or FULLSPEED mode bits and just ignore them */
8152 if (level & TP_EC_FAN_FULLSPEED)
8153 level |= 7; /* safety min speed 7 */
8154 else if (level & TP_EC_FAN_AUTO)
8155 level |= 4; /* safety min speed 4 */
8157 if (!acpi_ec_write(fan_status_offset, level))
8160 tp_features.fan_ctrl_status_undef = 0;
8167 vdbg_printk(TPACPI_DBG_FAN,
8168 "fan control: set fan control register to 0x%02x\n", level);
8172 static int fan_set_level_safe(int level)
8176 if (!fan_control_allowed)
8179 if (mutex_lock_killable(&fan_mutex))
8180 return -ERESTARTSYS;
8182 if (level == TPACPI_FAN_LAST_LEVEL)
8183 level = fan_control_desired_level;
8185 rc = fan_set_level(level);
8187 fan_update_desired_level(level);
8189 mutex_unlock(&fan_mutex);
8193 static int fan_set_enable(void)
8198 if (!fan_control_allowed)
8201 if (mutex_lock_killable(&fan_mutex))
8202 return -ERESTARTSYS;
8204 switch (fan_control_access_mode) {
8205 case TPACPI_FAN_WR_ACPI_FANS:
8206 case TPACPI_FAN_WR_TPEC:
8207 rc = fan_get_status(&s);
8211 /* Don't go out of emergency fan mode */
8214 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8217 if (!acpi_ec_write(fan_status_offset, s))
8220 tp_features.fan_ctrl_status_undef = 0;
8225 case TPACPI_FAN_WR_ACPI_SFAN:
8226 rc = fan_get_status(&s);
8232 /* Set fan to at least level 4 */
8235 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8245 mutex_unlock(&fan_mutex);
8248 vdbg_printk(TPACPI_DBG_FAN,
8249 "fan control: set fan control register to 0x%02x\n",
8254 static int fan_set_disable(void)
8258 if (!fan_control_allowed)
8261 if (mutex_lock_killable(&fan_mutex))
8262 return -ERESTARTSYS;
8265 switch (fan_control_access_mode) {
8266 case TPACPI_FAN_WR_ACPI_FANS:
8267 case TPACPI_FAN_WR_TPEC:
8268 if (!acpi_ec_write(fan_status_offset, 0x00))
8271 fan_control_desired_level = 0;
8272 tp_features.fan_ctrl_status_undef = 0;
8276 case TPACPI_FAN_WR_ACPI_SFAN:
8277 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8280 fan_control_desired_level = 0;
8288 vdbg_printk(TPACPI_DBG_FAN,
8289 "fan control: set fan control register to 0\n");
8291 mutex_unlock(&fan_mutex);
8295 static int fan_set_speed(int speed)
8299 if (!fan_control_allowed)
8302 if (mutex_lock_killable(&fan_mutex))
8303 return -ERESTARTSYS;
8306 switch (fan_control_access_mode) {
8307 case TPACPI_FAN_WR_ACPI_FANS:
8308 if (speed >= 0 && speed <= 65535) {
8309 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8310 speed, speed, speed))
8320 mutex_unlock(&fan_mutex);
8324 static void fan_watchdog_reset(void)
8326 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8329 if (fan_watchdog_maxinterval > 0 &&
8330 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8331 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8332 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8334 cancel_delayed_work(&fan_watchdog_task);
8337 static void fan_watchdog_fire(struct work_struct *ignored)
8341 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8344 pr_notice("fan watchdog: enabling fan\n");
8345 rc = fan_set_enable();
8347 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8349 /* reschedule for later */
8350 fan_watchdog_reset();
8355 * SYSFS fan layout: hwmon compatible (device)
8358 * 0: "disengaged" mode
8360 * 2: native EC "auto" mode (recommended, hardware default)
8362 * pwm*: set speed in manual mode, ignored otherwise.
8363 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8366 * fan*_input: tachometer reading, RPM
8369 * SYSFS fan layout: extensions
8371 * fan_watchdog (driver):
8372 * fan watchdog interval in seconds, 0 disables (default), max 120
8375 /* sysfs fan pwm1_enable ----------------------------------------------- */
8376 static ssize_t fan_pwm1_enable_show(struct device *dev,
8377 struct device_attribute *attr,
8383 res = fan_get_status_safe(&status);
8387 if (status & TP_EC_FAN_FULLSPEED) {
8389 } else if (status & TP_EC_FAN_AUTO) {
8394 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8397 static ssize_t fan_pwm1_enable_store(struct device *dev,
8398 struct device_attribute *attr,
8399 const char *buf, size_t count)
8404 if (parse_strtoul(buf, 2, &t))
8407 tpacpi_disclose_usertask("hwmon pwm1_enable",
8408 "set fan mode to %lu\n", t);
8412 level = TP_EC_FAN_FULLSPEED;
8415 level = TPACPI_FAN_LAST_LEVEL;
8418 level = TP_EC_FAN_AUTO;
8421 /* reserved for software-controlled auto mode */
8427 res = fan_set_level_safe(level);
8433 fan_watchdog_reset();
8438 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8439 fan_pwm1_enable_show, fan_pwm1_enable_store);
8441 /* sysfs fan pwm1 ------------------------------------------------------ */
8442 static ssize_t fan_pwm1_show(struct device *dev,
8443 struct device_attribute *attr,
8449 res = fan_get_status_safe(&status);
8454 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8455 status = fan_control_desired_level;
8460 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8463 static ssize_t fan_pwm1_store(struct device *dev,
8464 struct device_attribute *attr,
8465 const char *buf, size_t count)
8469 u8 status, newlevel;
8471 if (parse_strtoul(buf, 255, &s))
8474 tpacpi_disclose_usertask("hwmon pwm1",
8475 "set fan speed to %lu\n", s);
8477 /* scale down from 0-255 to 0-7 */
8478 newlevel = (s >> 5) & 0x07;
8480 if (mutex_lock_killable(&fan_mutex))
8481 return -ERESTARTSYS;
8483 rc = fan_get_status(&status);
8484 if (!rc && (status &
8485 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8486 rc = fan_set_level(newlevel);
8490 fan_update_desired_level(newlevel);
8491 fan_watchdog_reset();
8495 mutex_unlock(&fan_mutex);
8496 return (rc) ? rc : count;
8499 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8501 /* sysfs fan fan1_input ------------------------------------------------ */
8502 static ssize_t fan_fan1_input_show(struct device *dev,
8503 struct device_attribute *attr,
8509 res = fan_get_speed(&speed);
8513 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8516 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8518 /* sysfs fan fan2_input ------------------------------------------------ */
8519 static ssize_t fan_fan2_input_show(struct device *dev,
8520 struct device_attribute *attr,
8526 res = fan2_get_speed(&speed);
8530 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8533 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8535 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8536 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8538 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8541 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8546 if (parse_strtoul(buf, 120, &t))
8549 if (!fan_control_allowed)
8552 fan_watchdog_maxinterval = t;
8553 fan_watchdog_reset();
8555 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8559 static DRIVER_ATTR_RW(fan_watchdog);
8561 /* --------------------------------------------------------------------- */
8562 static struct attribute *fan_attributes[] = {
8563 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8564 &dev_attr_fan1_input.attr,
8565 NULL, /* for fan2_input */
8569 static const struct attribute_group fan_attr_group = {
8570 .attrs = fan_attributes,
8573 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8574 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8576 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8577 { .vendor = PCI_VENDOR_ID_IBM, \
8578 .bios = TPACPI_MATCH_ANY, \
8579 .ec = TPID(__id1, __id2), \
8580 .quirks = __quirks }
8582 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8583 { .vendor = PCI_VENDOR_ID_LENOVO, \
8584 .bios = TPACPI_MATCH_ANY, \
8585 .ec = TPID(__id1, __id2), \
8586 .quirks = __quirks }
8588 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8589 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8590 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8591 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8592 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8593 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8596 #undef TPACPI_FAN_QL
8597 #undef TPACPI_FAN_QI
8599 static int __init fan_init(struct ibm_init_struct *iibm)
8602 unsigned long quirks;
8604 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8605 "initializing fan subdriver\n");
8607 mutex_init(&fan_mutex);
8608 fan_status_access_mode = TPACPI_FAN_NONE;
8609 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8610 fan_control_commands = 0;
8611 fan_watchdog_maxinterval = 0;
8612 tp_features.fan_ctrl_status_undef = 0;
8613 tp_features.second_fan = 0;
8614 fan_control_desired_level = 7;
8616 if (tpacpi_is_ibm()) {
8617 TPACPI_ACPIHANDLE_INIT(fans);
8618 TPACPI_ACPIHANDLE_INIT(gfan);
8619 TPACPI_ACPIHANDLE_INIT(sfan);
8622 quirks = tpacpi_check_quirks(fan_quirk_table,
8623 ARRAY_SIZE(fan_quirk_table));
8626 /* 570, 600e/x, 770e, 770x */
8627 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8629 /* all other ThinkPads: note that even old-style
8630 * ThinkPad ECs supports the fan control register */
8631 if (likely(acpi_ec_read(fan_status_offset,
8632 &fan_control_initial_status))) {
8633 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8634 if (quirks & TPACPI_FAN_Q1)
8636 if (quirks & TPACPI_FAN_2FAN) {
8637 tp_features.second_fan = 1;
8638 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8639 "secondary fan support enabled\n");
8642 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8649 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8650 fan_control_commands |=
8651 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8654 /* gfan without sfan means no fan control */
8655 /* all other models implement TP EC 0x2f control */
8659 fan_control_access_mode =
8660 TPACPI_FAN_WR_ACPI_FANS;
8661 fan_control_commands |=
8662 TPACPI_FAN_CMD_SPEED |
8663 TPACPI_FAN_CMD_LEVEL |
8664 TPACPI_FAN_CMD_ENABLE;
8666 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8667 fan_control_commands |=
8668 TPACPI_FAN_CMD_LEVEL |
8669 TPACPI_FAN_CMD_ENABLE;
8674 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8675 "fan is %s, modes %d, %d\n",
8676 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8677 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8678 fan_status_access_mode, fan_control_access_mode);
8680 /* fan control master switch */
8681 if (!fan_control_allowed) {
8682 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8683 fan_control_commands = 0;
8684 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8685 "fan control features disabled by parameter\n");
8688 /* update fan_control_desired_level */
8689 if (fan_status_access_mode != TPACPI_FAN_NONE)
8690 fan_get_status_safe(NULL);
8692 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8693 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8694 if (tp_features.second_fan) {
8695 /* attach second fan tachometer */
8696 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8697 &dev_attr_fan2_input.attr;
8699 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8704 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8705 &driver_attr_fan_watchdog);
8707 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8716 static void fan_exit(void)
8718 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8719 "cancelling any pending fan watchdog tasks\n");
8721 /* FIXME: can we really do this unconditionally? */
8722 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8723 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8724 &driver_attr_fan_watchdog);
8726 cancel_delayed_work(&fan_watchdog_task);
8727 flush_workqueue(tpacpi_wq);
8730 static void fan_suspend(void)
8734 if (!fan_control_allowed)
8737 /* Store fan status in cache */
8738 fan_control_resume_level = 0;
8739 rc = fan_get_status_safe(&fan_control_resume_level);
8741 pr_notice("failed to read fan level for later restore during resume: %d\n",
8744 /* if it is undefined, don't attempt to restore it.
8746 if (tp_features.fan_ctrl_status_undef)
8747 fan_control_resume_level = 0;
8750 static void fan_resume(void)
8752 u8 current_level = 7;
8753 bool do_set = false;
8756 /* DSDT *always* updates status on resume */
8757 tp_features.fan_ctrl_status_undef = 0;
8759 if (!fan_control_allowed ||
8760 !fan_control_resume_level ||
8761 (fan_get_status_safe(¤t_level) < 0))
8764 switch (fan_control_access_mode) {
8765 case TPACPI_FAN_WR_ACPI_SFAN:
8766 /* never decrease fan level */
8767 do_set = (fan_control_resume_level > current_level);
8769 case TPACPI_FAN_WR_ACPI_FANS:
8770 case TPACPI_FAN_WR_TPEC:
8771 /* never decrease fan level, scale is:
8772 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8774 * We expect the firmware to set either 7 or AUTO, but we
8775 * handle FULLSPEED out of paranoia.
8777 * So, we can safely only restore FULLSPEED or 7, anything
8778 * else could slow the fan. Restoring AUTO is useless, at
8779 * best that's exactly what the DSDT already set (it is the
8782 * Always keep in mind that the DSDT *will* have set the
8783 * fans to what the vendor supposes is the best level. We
8784 * muck with it only to speed the fan up.
8786 if (fan_control_resume_level != 7 &&
8787 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8790 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8791 (current_level != fan_control_resume_level);
8797 pr_notice("restoring fan level to 0x%02x\n",
8798 fan_control_resume_level);
8799 rc = fan_set_level_safe(fan_control_resume_level);
8801 pr_notice("failed to restore fan level: %d\n", rc);
8805 static int fan_read(struct seq_file *m)
8809 unsigned int speed = 0;
8811 switch (fan_status_access_mode) {
8812 case TPACPI_FAN_RD_ACPI_GFAN:
8813 /* 570, 600e/x, 770e, 770x */
8814 rc = fan_get_status_safe(&status);
8818 seq_printf(m, "status:\t\t%s\n"
8820 (status != 0) ? "enabled" : "disabled", status);
8823 case TPACPI_FAN_RD_TPEC:
8824 /* all except 570, 600e/x, 770e, 770x */
8825 rc = fan_get_status_safe(&status);
8829 seq_printf(m, "status:\t\t%s\n",
8830 (status != 0) ? "enabled" : "disabled");
8832 rc = fan_get_speed(&speed);
8836 seq_printf(m, "speed:\t\t%d\n", speed);
8838 if (status & TP_EC_FAN_FULLSPEED)
8839 /* Disengaged mode takes precedence */
8840 seq_printf(m, "level:\t\tdisengaged\n");
8841 else if (status & TP_EC_FAN_AUTO)
8842 seq_printf(m, "level:\t\tauto\n");
8844 seq_printf(m, "level:\t\t%d\n", status);
8847 case TPACPI_FAN_NONE:
8849 seq_printf(m, "status:\t\tnot supported\n");
8852 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8853 seq_printf(m, "commands:\tlevel <level>");
8855 switch (fan_control_access_mode) {
8856 case TPACPI_FAN_WR_ACPI_SFAN:
8857 seq_printf(m, " (<level> is 0-7)\n");
8861 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8866 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8867 seq_printf(m, "commands:\tenable, disable\n"
8868 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8870 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8871 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8876 static int fan_write_cmd_level(const char *cmd, int *rc)
8880 if (strlencmp(cmd, "level auto") == 0)
8881 level = TP_EC_FAN_AUTO;
8882 else if ((strlencmp(cmd, "level disengaged") == 0) |
8883 (strlencmp(cmd, "level full-speed") == 0))
8884 level = TP_EC_FAN_FULLSPEED;
8885 else if (sscanf(cmd, "level %d", &level) != 1)
8888 *rc = fan_set_level_safe(level);
8890 pr_err("level command accepted for unsupported access mode %d\n",
8891 fan_control_access_mode);
8893 tpacpi_disclose_usertask("procfs fan",
8894 "set level to %d\n", level);
8899 static int fan_write_cmd_enable(const char *cmd, int *rc)
8901 if (strlencmp(cmd, "enable") != 0)
8904 *rc = fan_set_enable();
8906 pr_err("enable command accepted for unsupported access mode %d\n",
8907 fan_control_access_mode);
8909 tpacpi_disclose_usertask("procfs fan", "enable\n");
8914 static int fan_write_cmd_disable(const char *cmd, int *rc)
8916 if (strlencmp(cmd, "disable") != 0)
8919 *rc = fan_set_disable();
8921 pr_err("disable command accepted for unsupported access mode %d\n",
8922 fan_control_access_mode);
8924 tpacpi_disclose_usertask("procfs fan", "disable\n");
8929 static int fan_write_cmd_speed(const char *cmd, int *rc)
8934 * Support speed <low> <medium> <high> ? */
8936 if (sscanf(cmd, "speed %d", &speed) != 1)
8939 *rc = fan_set_speed(speed);
8941 pr_err("speed command accepted for unsupported access mode %d\n",
8942 fan_control_access_mode);
8944 tpacpi_disclose_usertask("procfs fan",
8945 "set speed to %d\n", speed);
8950 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8954 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8957 if (interval < 0 || interval > 120)
8960 fan_watchdog_maxinterval = interval;
8961 tpacpi_disclose_usertask("procfs fan",
8962 "set watchdog timer to %d\n",
8969 static int fan_write(char *buf)
8974 while (!rc && (cmd = next_cmd(&buf))) {
8975 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8976 fan_write_cmd_level(cmd, &rc)) &&
8977 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8978 (fan_write_cmd_enable(cmd, &rc) ||
8979 fan_write_cmd_disable(cmd, &rc) ||
8980 fan_write_cmd_watchdog(cmd, &rc))) &&
8981 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8982 fan_write_cmd_speed(cmd, &rc))
8986 fan_watchdog_reset();
8992 static struct ibm_struct fan_driver_data = {
8997 .suspend = fan_suspend,
8998 .resume = fan_resume,
9001 /*************************************************************************
9002 * Mute LED subdriver
9006 struct tp_led_table {
9013 static struct tp_led_table led_tables[] = {
9014 [TPACPI_LED_MUTE] = {
9019 [TPACPI_LED_MICMUTE] = {
9026 static int mute_led_on_off(struct tp_led_table *t, bool state)
9031 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9032 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9036 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9037 state ? t->on_value : t->off_value))
9044 int tpacpi_led_set(int whichled, bool on)
9046 struct tp_led_table *t;
9048 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9051 t = &led_tables[whichled];
9052 if (t->state < 0 || t->state == on)
9054 return mute_led_on_off(t, on);
9056 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9058 static int mute_led_init(struct ibm_init_struct *iibm)
9063 for (i = 0; i < TPACPI_LED_MAX; i++) {
9064 struct tp_led_table *t = &led_tables[i];
9065 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9066 mute_led_on_off(t, false);
9073 static void mute_led_exit(void)
9077 for (i = 0; i < TPACPI_LED_MAX; i++)
9078 tpacpi_led_set(i, false);
9081 static void mute_led_resume(void)
9085 for (i = 0; i < TPACPI_LED_MAX; i++) {
9086 struct tp_led_table *t = &led_tables[i];
9088 mute_led_on_off(t, t->state);
9092 static struct ibm_struct mute_led_driver_data = {
9094 .exit = mute_led_exit,
9095 .resume = mute_led_resume,
9098 /****************************************************************************
9099 ****************************************************************************
9103 ****************************************************************************
9104 ****************************************************************************/
9107 * HKEY event callout for other subdrivers go here
9108 * (yes, it is ugly, but it is quick, safe, and gets the job done
9110 static void tpacpi_driver_event(const unsigned int hkey_event)
9112 if (ibm_backlight_device) {
9113 switch (hkey_event) {
9114 case TP_HKEY_EV_BRGHT_UP:
9115 case TP_HKEY_EV_BRGHT_DOWN:
9116 tpacpi_brightness_notify_change();
9120 switch (hkey_event) {
9121 case TP_HKEY_EV_VOL_UP:
9122 case TP_HKEY_EV_VOL_DOWN:
9123 case TP_HKEY_EV_VOL_MUTE:
9124 volume_alsa_notify_change();
9127 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9128 enum led_brightness brightness;
9130 mutex_lock(&kbdlight_mutex);
9133 * Check the brightness actually changed, setting the brightness
9134 * through kbdlight_set_level() also triggers this event.
9136 brightness = kbdlight_sysfs_get(NULL);
9137 if (kbdlight_brightness != brightness) {
9138 kbdlight_brightness = brightness;
9139 led_classdev_notify_brightness_hw_changed(
9140 &tpacpi_led_kbdlight.led_classdev, brightness);
9143 mutex_unlock(&kbdlight_mutex);
9147 static void hotkey_driver_event(const unsigned int scancode)
9149 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9152 /* sysfs name ---------------------------------------------------------- */
9153 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9154 struct device_attribute *attr,
9157 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
9160 static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
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 (tp_features.sensors_pdev_attrs_registered)
9700 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
9701 if (tpacpi_sensors_pdev)
9702 platform_device_unregister(tpacpi_sensors_pdev);
9704 platform_device_unregister(tpacpi_pdev);
9706 if (tp_features.sensors_pdrv_attrs_registered)
9707 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9708 if (tp_features.platform_drv_attrs_registered)
9709 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9711 if (tp_features.sensors_pdrv_registered)
9712 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9714 if (tp_features.platform_drv_registered)
9715 platform_driver_unregister(&tpacpi_pdriver);
9718 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9721 destroy_workqueue(tpacpi_wq);
9723 kfree(thinkpad_id.bios_version_str);
9724 kfree(thinkpad_id.ec_version_str);
9725 kfree(thinkpad_id.model_str);
9726 kfree(thinkpad_id.nummodel_str);
9730 static int __init thinkpad_acpi_module_init(void)
9734 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9736 /* Driver-level probe */
9738 ret = get_thinkpad_model_data(&thinkpad_id);
9740 pr_err("unable to get DMI data: %d\n", ret);
9741 thinkpad_acpi_module_exit();
9744 ret = probe_for_thinkpad();
9746 thinkpad_acpi_module_exit();
9750 /* Driver initialization */
9752 thinkpad_acpi_init_banner();
9753 tpacpi_check_outdated_fw();
9755 TPACPI_ACPIHANDLE_INIT(ecrd);
9756 TPACPI_ACPIHANDLE_INIT(ecwr);
9758 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9760 thinkpad_acpi_module_exit();
9764 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9766 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9767 thinkpad_acpi_module_exit();
9771 ret = platform_driver_register(&tpacpi_pdriver);
9773 pr_err("unable to register main platform driver\n");
9774 thinkpad_acpi_module_exit();
9777 tp_features.platform_drv_registered = 1;
9779 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9781 pr_err("unable to register hwmon platform driver\n");
9782 thinkpad_acpi_module_exit();
9785 tp_features.sensors_pdrv_registered = 1;
9787 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9789 tp_features.platform_drv_attrs_registered = 1;
9790 ret = tpacpi_create_driver_attributes(
9791 &tpacpi_hwmon_pdriver.driver);
9794 pr_err("unable to create sysfs driver attributes\n");
9795 thinkpad_acpi_module_exit();
9798 tp_features.sensors_pdrv_attrs_registered = 1;
9801 /* Device initialization */
9802 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9804 if (IS_ERR(tpacpi_pdev)) {
9805 ret = PTR_ERR(tpacpi_pdev);
9807 pr_err("unable to register platform device\n");
9808 thinkpad_acpi_module_exit();
9811 tpacpi_sensors_pdev = platform_device_register_simple(
9812 TPACPI_HWMON_DRVR_NAME,
9814 if (IS_ERR(tpacpi_sensors_pdev)) {
9815 ret = PTR_ERR(tpacpi_sensors_pdev);
9816 tpacpi_sensors_pdev = NULL;
9817 pr_err("unable to register hwmon platform device\n");
9818 thinkpad_acpi_module_exit();
9821 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
9823 pr_err("unable to create sysfs hwmon device attributes\n");
9824 thinkpad_acpi_module_exit();
9827 tp_features.sensors_pdev_attrs_registered = 1;
9828 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9829 if (IS_ERR(tpacpi_hwmon)) {
9830 ret = PTR_ERR(tpacpi_hwmon);
9831 tpacpi_hwmon = NULL;
9832 pr_err("unable to register hwmon device\n");
9833 thinkpad_acpi_module_exit();
9836 mutex_init(&tpacpi_inputdev_send_mutex);
9837 tpacpi_inputdev = input_allocate_device();
9838 if (!tpacpi_inputdev) {
9839 thinkpad_acpi_module_exit();
9842 /* Prepare input device, but don't register */
9843 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9844 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9845 tpacpi_inputdev->id.bustype = BUS_HOST;
9846 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9847 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9848 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9849 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9852 /* Init subdriver dependencies */
9853 tpacpi_detect_brightness_capabilities();
9855 /* Init subdrivers */
9856 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9857 ret = ibm_init(&ibms_init[i]);
9858 if (ret >= 0 && *ibms_init[i].param)
9859 ret = ibms_init[i].data->write(ibms_init[i].param);
9861 thinkpad_acpi_module_exit();
9866 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9868 ret = input_register_device(tpacpi_inputdev);
9870 pr_err("unable to register input device\n");
9871 thinkpad_acpi_module_exit();
9874 tp_features.input_device_registered = 1;
9880 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9883 * This will autoload the driver in almost every ThinkPad
9884 * in widespread use.
9886 * Only _VERY_ old models, like the 240, 240x and 570 lack
9887 * the HKEY event interface.
9889 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9892 * DMI matching for module autoloading
9894 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9895 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9897 * Only models listed in thinkwiki will be supported, so add yours
9898 * if it is not there yet.
9900 #define IBM_BIOS_MODULE_ALIAS(__type) \
9901 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9903 /* Ancient thinkpad BIOSes have to be identified by
9904 * BIOS type or model number, and there are far less
9905 * BIOS types than model numbers... */
9906 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9910 MODULE_DESCRIPTION(TPACPI_DESC);
9911 MODULE_VERSION(TPACPI_VERSION);
9912 MODULE_LICENSE("GPL");
9914 module_init(thinkpad_acpi_module_init);
9915 module_exit(thinkpad_acpi_module_exit);