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.26"
27 #define TPACPI_SYSFS_VERSION 0x030000
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
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>
69 #include <linux/bitops.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <linux/dmi.h>
78 #include <linux/jiffies.h>
79 #include <linux/workqueue.h>
80 #include <linux/acpi.h>
81 #include <linux/pci_ids.h>
82 #include <linux/power_supply.h>
83 #include <linux/thinkpad_acpi.h>
84 #include <sound/core.h>
85 #include <sound/control.h>
86 #include <sound/initval.h>
87 #include <linux/uaccess.h>
88 #include <acpi/battery.h>
89 #include <acpi/video.h>
91 /* ThinkPad CMOS commands */
92 #define TP_CMOS_VOLUME_DOWN 0
93 #define TP_CMOS_VOLUME_UP 1
94 #define TP_CMOS_VOLUME_MUTE 2
95 #define TP_CMOS_BRIGHTNESS_UP 4
96 #define TP_CMOS_BRIGHTNESS_DOWN 5
97 #define TP_CMOS_THINKLIGHT_ON 12
98 #define TP_CMOS_THINKLIGHT_OFF 13
100 /* NVRAM Addresses */
102 TP_NVRAM_ADDR_HK2 = 0x57,
103 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
104 TP_NVRAM_ADDR_VIDEO = 0x59,
105 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
106 TP_NVRAM_ADDR_MIXER = 0x60,
109 /* NVRAM bit masks */
111 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
112 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
113 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
114 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
115 TP_NVRAM_MASK_THINKLIGHT = 0x10,
116 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
117 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
118 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
119 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
120 TP_NVRAM_MASK_MUTE = 0x40,
121 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
122 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
123 TP_NVRAM_POS_LEVEL_VOLUME = 0,
126 /* Misc NVRAM-related */
128 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
132 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
133 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
164 enum tpacpi_hkey_event_t {
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
170 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
171 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
172 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
174 /* Reasons for waking up from S3/S4 */
175 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
176 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
177 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
178 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
179 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
180 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
182 /* Auto-sleep after eject request */
183 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
184 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
186 /* Misc bay events */
187 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
188 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
189 or port replicator */
190 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
191 dock or port replicator */
193 /* User-interface events */
194 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
195 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
196 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
197 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
198 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
199 * enter/leave tablet mode
201 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
202 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
203 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
205 /* Key-related user-interface events */
206 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
207 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
208 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
211 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
212 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
213 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
214 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
215 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
216 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
217 * command completed. Related to
219 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
220 * changed. Related to AML GMTS */
222 /* AC-related events */
223 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
225 /* Further user-interface events */
226 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
227 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
230 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
233 /****************************************************************************
237 #define TPACPI_NAME "thinkpad"
238 #define TPACPI_DESC "ThinkPad ACPI Extras"
239 #define TPACPI_FILE TPACPI_NAME "_acpi"
240 #define TPACPI_URL "http://ibm-acpi.sf.net/"
243 #define TPACPI_PROC_DIR "ibm"
244 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
245 #define TPACPI_DRVR_NAME TPACPI_FILE
246 #define TPACPI_DRVR_SHORTNAME "tpacpi"
247 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
249 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
250 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
252 #define TPACPI_MAX_ACPI_ARGS 3
254 /* Debugging printk groups */
255 #define TPACPI_DBG_ALL 0xffff
256 #define TPACPI_DBG_DISCLOSETASK 0x8000
257 #define TPACPI_DBG_INIT 0x0001
258 #define TPACPI_DBG_EXIT 0x0002
259 #define TPACPI_DBG_RFKILL 0x0004
260 #define TPACPI_DBG_HKEY 0x0008
261 #define TPACPI_DBG_FAN 0x0010
262 #define TPACPI_DBG_BRGHT 0x0020
263 #define TPACPI_DBG_MIXER 0x0040
265 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
266 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
267 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
270 /****************************************************************************
271 * Driver-wide structs and misc. variables
276 struct tp_acpi_drv_struct {
277 const struct acpi_device_id *hid;
278 struct acpi_driver *driver;
280 void (*notify) (struct ibm_struct *, u32);
283 struct acpi_device *device;
289 int (*read) (struct seq_file *);
290 int (*write) (char *);
292 void (*resume) (void);
293 void (*suspend) (void);
294 void (*shutdown) (void);
296 struct list_head all_drivers;
298 struct tp_acpi_drv_struct *acpi;
301 u8 acpi_driver_registered:1;
302 u8 acpi_notify_installed:1;
309 struct ibm_init_struct {
312 int (*init) (struct ibm_init_struct *);
313 umode_t base_procfs_mode;
314 struct ibm_struct *data;
323 TP_HOTKEY_TABLET_NONE = 0,
324 TP_HOTKEY_TABLET_USES_MHKG,
325 TP_HOTKEY_TABLET_USES_GMMS,
330 u32 bright_acpimode:1;
334 u32 fan_ctrl_status_undef:1;
336 u32 beep_needs_two_args:1;
337 u32 mixer_no_level_control:1;
338 u32 battery_force_primary:1;
339 u32 input_device_registered:1;
340 u32 platform_drv_registered:1;
341 u32 platform_drv_attrs_registered:1;
342 u32 sensors_pdrv_registered:1;
343 u32 sensors_pdrv_attrs_registered:1;
344 u32 sensors_pdev_attrs_registered:1;
345 u32 hotkey_poll_active:1;
346 u32 has_adaptive_kbd:1;
350 u16 hotkey_mask_ff:1;
351 u16 volume_ctrl_forbidden:1;
354 struct thinkpad_id_data {
355 unsigned int vendor; /* ThinkPad vendor:
356 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
358 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
359 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
361 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
363 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
366 char *model_str; /* ThinkPad T43 */
367 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
369 static struct thinkpad_id_data thinkpad_id;
372 TPACPI_LIFE_INIT = 0,
377 static int experimental;
378 static u32 dbg_level;
380 static struct workqueue_struct *tpacpi_wq;
388 /* tpacpi LED class */
389 struct tpacpi_led_classdev {
390 struct led_classdev led_classdev;
394 /* brightness level capabilities */
395 static unsigned int bright_maxlvl; /* 0 = unknown */
397 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
398 static int dbg_wlswemul;
399 static bool tpacpi_wlsw_emulstate;
400 static int dbg_bluetoothemul;
401 static bool tpacpi_bluetooth_emulstate;
402 static int dbg_wwanemul;
403 static bool tpacpi_wwan_emulstate;
404 static int dbg_uwbemul;
405 static bool tpacpi_uwb_emulstate;
409 /*************************************************************************
413 #define dbg_printk(a_dbg_level, format, arg...) \
415 if (dbg_level & (a_dbg_level)) \
416 printk(KERN_DEBUG pr_fmt("%s: " format), \
420 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
421 #define vdbg_printk dbg_printk
422 static const char *str_supported(int is_supported);
424 static inline const char *str_supported(int is_supported) { return ""; }
425 #define vdbg_printk(a_dbg_level, format, arg...) \
426 do { if (0) no_printk(format, ##arg); } while (0)
429 static void tpacpi_log_usertask(const char * const what)
431 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
432 what, task_tgid_vnr(current));
435 #define tpacpi_disclose_usertask(what, format, arg...) \
437 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
438 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
439 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
440 what, task_tgid_vnr(current), ## arg); \
445 * Quirk handling helpers
447 * ThinkPad IDs and versions seen in the field so far are
448 * two or three characters from the set [0-9A-Z], i.e. base 36.
450 * We use values well outside that range as specials.
453 #define TPACPI_MATCH_ANY 0xffffffffU
454 #define TPACPI_MATCH_ANY_VERSION 0xffffU
455 #define TPACPI_MATCH_UNKNOWN 0U
457 /* TPID('1', 'Y') == 0x3159 */
458 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
459 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
462 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_IBM, \
464 .bios = TPID(__id1, __id2), \
465 .ec = TPACPI_MATCH_ANY, \
466 .quirks = (__quirk) }
468 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
469 { .vendor = PCI_VENDOR_ID_LENOVO, \
470 .bios = TPID(__id1, __id2), \
471 .ec = TPACPI_MATCH_ANY, \
472 .quirks = (__quirk) }
474 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
475 { .vendor = PCI_VENDOR_ID_LENOVO, \
476 .bios = TPID3(__id1, __id2, __id3), \
477 .ec = TPACPI_MATCH_ANY, \
478 .quirks = (__quirk) }
480 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
481 { .vendor = PCI_VENDOR_ID_LENOVO, \
482 .bios = TPACPI_MATCH_ANY, \
483 .ec = TPID(__id1, __id2), \
484 .quirks = (__quirk) }
486 struct tpacpi_quirk {
490 unsigned long quirks;
494 * tpacpi_check_quirks() - search BIOS/EC version on a list
495 * @qlist: array of &struct tpacpi_quirk
496 * @qlist_size: number of elements in @qlist
498 * Iterates over a quirks list until one is found that matches the
499 * ThinkPad's vendor, BIOS and EC model.
501 * Returns 0 if nothing matches, otherwise returns the quirks field of
502 * the matching &struct tpacpi_quirk entry.
504 * The match criteria is: vendor, ec and bios much match.
506 static unsigned long __init tpacpi_check_quirks(
507 const struct tpacpi_quirk *qlist,
508 unsigned int qlist_size)
511 if ((qlist->vendor == thinkpad_id.vendor ||
512 qlist->vendor == TPACPI_MATCH_ANY) &&
513 (qlist->bios == thinkpad_id.bios_model ||
514 qlist->bios == TPACPI_MATCH_ANY) &&
515 (qlist->ec == thinkpad_id.ec_model ||
516 qlist->ec == TPACPI_MATCH_ANY))
517 return qlist->quirks;
525 static inline bool __pure __init tpacpi_is_lenovo(void)
527 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
530 static inline bool __pure __init tpacpi_is_ibm(void)
532 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
535 /****************************************************************************
536 ****************************************************************************
538 * ACPI Helpers and device model
540 ****************************************************************************
541 ****************************************************************************/
543 /*************************************************************************
547 static acpi_handle root_handle;
548 static acpi_handle ec_handle;
550 #define TPACPI_HANDLE(object, parent, paths...) \
551 static acpi_handle object##_handle; \
552 static const acpi_handle * const object##_parent __initconst = \
554 static char *object##_paths[] __initdata = { paths }
556 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
557 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
559 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
561 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
562 "\\CMS", /* R40, R40e */
565 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
566 "^HKEY", /* R30, R31 */
567 "HKEY", /* all others */
570 /*************************************************************************
574 static int acpi_evalf(acpi_handle handle,
575 int *res, char *method, char *fmt, ...)
578 struct acpi_object_list params;
579 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
580 struct acpi_buffer result, *resultp;
581 union acpi_object out_obj;
589 pr_err("acpi_evalf() called with empty format\n");
602 params.pointer = &in_objs[0];
609 in_objs[params.count].integer.value = va_arg(ap, int);
610 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
612 /* add more types as needed */
614 pr_err("acpi_evalf() called with invalid format character '%c'\n",
622 if (res_type != 'v') {
623 result.length = sizeof(out_obj);
624 result.pointer = &out_obj;
629 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
633 success = (status == AE_OK &&
634 out_obj.type == ACPI_TYPE_INTEGER);
636 *res = out_obj.integer.value;
639 success = status == AE_OK;
641 /* add more types as needed */
643 pr_err("acpi_evalf() called with invalid format character '%c'\n",
648 if (!success && !quiet)
649 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
650 method, fmt0, acpi_format_exception(status));
655 static int acpi_ec_read(int i, u8 *p)
660 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
664 if (ec_read(i, p) < 0)
671 static int acpi_ec_write(int i, u8 v)
674 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
677 if (ec_write(i, v) < 0)
684 static int issue_thinkpad_cmos_command(int cmos_cmd)
689 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
695 /*************************************************************************
699 #define TPACPI_ACPIHANDLE_INIT(object) \
700 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
701 object##_paths, ARRAY_SIZE(object##_paths))
703 static void __init drv_acpi_handle_init(const char *name,
704 acpi_handle *handle, const acpi_handle parent,
705 char **paths, const int num_paths)
710 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
713 for (i = 0; i < num_paths; i++) {
714 status = acpi_get_handle(parent, paths[i], handle);
715 if (ACPI_SUCCESS(status)) {
716 dbg_printk(TPACPI_DBG_INIT,
717 "Found ACPI handle %s for %s\n",
723 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
728 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
729 u32 level, void *context, void **return_value)
731 struct acpi_device *dev;
732 if (!strcmp(context, "video")) {
733 if (acpi_bus_get_device(handle, &dev))
735 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
739 *(acpi_handle *)return_value = handle;
741 return AE_CTRL_TERMINATE;
744 static void __init tpacpi_acpi_handle_locate(const char *name,
749 acpi_handle device_found;
751 BUG_ON(!name || !handle);
752 vdbg_printk(TPACPI_DBG_INIT,
753 "trying to locate ACPI handle for %s, using HID %s\n",
754 name, hid ? hid : "NULL");
756 memset(&device_found, 0, sizeof(device_found));
757 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
758 (void *)name, &device_found);
762 if (ACPI_SUCCESS(status)) {
763 *handle = device_found;
764 dbg_printk(TPACPI_DBG_INIT,
765 "Found ACPI handle for %s\n", name);
767 vdbg_printk(TPACPI_DBG_INIT,
768 "Could not locate an ACPI handle for %s: %s\n",
769 name, acpi_format_exception(status));
773 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
775 struct ibm_struct *ibm = data;
777 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
780 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
783 ibm->acpi->notify(ibm, event);
786 static int __init setup_acpi_notify(struct ibm_struct *ibm)
793 if (!*ibm->acpi->handle)
796 vdbg_printk(TPACPI_DBG_INIT,
797 "setting up ACPI notify for %s\n", ibm->name);
799 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
801 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
805 ibm->acpi->device->driver_data = ibm;
806 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
807 TPACPI_ACPI_EVENT_PREFIX,
810 status = acpi_install_notify_handler(*ibm->acpi->handle,
811 ibm->acpi->type, dispatch_acpi_notify, ibm);
812 if (ACPI_FAILURE(status)) {
813 if (status == AE_ALREADY_EXISTS) {
814 pr_notice("another device driver is already handling %s events\n",
817 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
818 ibm->name, acpi_format_exception(status));
822 ibm->flags.acpi_notify_installed = 1;
826 static int __init tpacpi_device_add(struct acpi_device *device)
831 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
835 dbg_printk(TPACPI_DBG_INIT,
836 "registering %s as an ACPI driver\n", ibm->name);
840 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
841 if (!ibm->acpi->driver) {
842 pr_err("failed to allocate memory for ibm->acpi->driver\n");
846 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
847 ibm->acpi->driver->ids = ibm->acpi->hid;
849 ibm->acpi->driver->ops.add = &tpacpi_device_add;
851 rc = acpi_bus_register_driver(ibm->acpi->driver);
853 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
855 kfree(ibm->acpi->driver);
856 ibm->acpi->driver = NULL;
858 ibm->flags.acpi_driver_registered = 1;
864 /****************************************************************************
865 ****************************************************************************
869 ****************************************************************************
870 ****************************************************************************/
872 static int dispatch_proc_show(struct seq_file *m, void *v)
874 struct ibm_struct *ibm = m->private;
876 if (!ibm || !ibm->read)
881 static int dispatch_proc_open(struct inode *inode, struct file *file)
883 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
886 static ssize_t dispatch_proc_write(struct file *file,
887 const char __user *userbuf,
888 size_t count, loff_t *pos)
890 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
894 if (!ibm || !ibm->write)
896 if (count > PAGE_SIZE - 2)
899 kernbuf = kmalloc(count + 2, GFP_KERNEL);
903 if (copy_from_user(kernbuf, userbuf, count)) {
909 strcat(kernbuf, ",");
910 ret = ibm->write(kernbuf);
919 static const struct file_operations dispatch_proc_fops = {
920 .owner = THIS_MODULE,
921 .open = dispatch_proc_open,
924 .release = single_release,
925 .write = dispatch_proc_write,
928 static char *next_cmd(char **cmds)
933 while ((end = strchr(start, ',')) && end == start)
945 /****************************************************************************
946 ****************************************************************************
948 * Device model: input, hwmon and platform
950 ****************************************************************************
951 ****************************************************************************/
953 static struct platform_device *tpacpi_pdev;
954 static struct platform_device *tpacpi_sensors_pdev;
955 static struct device *tpacpi_hwmon;
956 static struct input_dev *tpacpi_inputdev;
957 static struct mutex tpacpi_inputdev_send_mutex;
958 static LIST_HEAD(tpacpi_all_drivers);
960 #ifdef CONFIG_PM_SLEEP
961 static int tpacpi_suspend_handler(struct device *dev)
963 struct ibm_struct *ibm, *itmp;
965 list_for_each_entry_safe(ibm, itmp,
975 static int tpacpi_resume_handler(struct device *dev)
977 struct ibm_struct *ibm, *itmp;
979 list_for_each_entry_safe(ibm, itmp,
990 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
991 tpacpi_suspend_handler, tpacpi_resume_handler);
993 static void tpacpi_shutdown_handler(struct platform_device *pdev)
995 struct ibm_struct *ibm, *itmp;
997 list_for_each_entry_safe(ibm, itmp,
1005 static struct platform_driver tpacpi_pdriver = {
1007 .name = TPACPI_DRVR_NAME,
1010 .shutdown = tpacpi_shutdown_handler,
1013 static struct platform_driver tpacpi_hwmon_pdriver = {
1015 .name = TPACPI_HWMON_DRVR_NAME,
1019 /*************************************************************************
1020 * sysfs support helpers
1023 struct attribute_set {
1024 unsigned int members, max_members;
1025 struct attribute_group group;
1028 struct attribute_set_obj {
1029 struct attribute_set s;
1030 struct attribute *a;
1031 } __attribute__((packed));
1033 static struct attribute_set *create_attr_set(unsigned int max_members,
1036 struct attribute_set_obj *sobj;
1038 if (max_members == 0)
1041 /* Allocates space for implicit NULL at the end too */
1042 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1043 max_members * sizeof(struct attribute *),
1047 sobj->s.max_members = max_members;
1048 sobj->s.group.attrs = &sobj->a;
1049 sobj->s.group.name = name;
1054 #define destroy_attr_set(_set) \
1057 /* not multi-threaded safe, use it in a single thread per set */
1058 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1063 if (s->members >= s->max_members)
1066 s->group.attrs[s->members] = attr;
1072 static int add_many_to_attr_set(struct attribute_set *s,
1073 struct attribute **attr,
1078 for (i = 0; i < count; i++) {
1079 res = add_to_attr_set(s, attr[i]);
1087 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1089 sysfs_remove_group(kobj, &s->group);
1090 destroy_attr_set(s);
1093 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1094 sysfs_create_group(_kobj, &_attr_set->group)
1096 static int parse_strtoul(const char *buf,
1097 unsigned long max, unsigned long *value)
1101 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1102 endp = skip_spaces(endp);
1103 if (*endp || *value > max)
1109 static void tpacpi_disable_brightness_delay(void)
1111 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1112 pr_notice("ACPI backlight control delay disabled\n");
1115 static void printk_deprecated_attribute(const char * const what,
1116 const char * const details)
1118 tpacpi_log_usertask("deprecated sysfs attribute");
1119 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1123 /*************************************************************************
1124 * rfkill and radio control support helpers
1128 * ThinkPad-ACPI firmware handling model:
1130 * WLSW (master wireless switch) is event-driven, and is common to all
1131 * firmware-controlled radios. It cannot be controlled, just monitored,
1132 * as expected. It overrides all radio state in firmware
1134 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1135 * (TODO: verify how WLSW interacts with the returned radio state).
1137 * The only time there are shadow radio state changes, is when
1138 * masked-off hotkeys are used.
1142 * Internal driver API for radio state:
1144 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1145 * bool: true means radio blocked (off)
1147 enum tpacpi_rfkill_state {
1148 TPACPI_RFK_RADIO_OFF = 0,
1152 /* rfkill switches */
1153 enum tpacpi_rfk_id {
1154 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1155 TPACPI_RFK_WWAN_SW_ID,
1156 TPACPI_RFK_UWB_SW_ID,
1160 static const char *tpacpi_rfkill_names[] = {
1161 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1162 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1163 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1164 [TPACPI_RFK_SW_MAX] = NULL
1167 /* ThinkPad-ACPI rfkill subdriver */
1169 struct rfkill *rfkill;
1170 enum tpacpi_rfk_id id;
1171 const struct tpacpi_rfk_ops *ops;
1174 struct tpacpi_rfk_ops {
1175 /* firmware interface */
1176 int (*get_status)(void);
1177 int (*set_status)(const enum tpacpi_rfkill_state);
1180 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1182 /* Query FW and update rfkill sw state for a given rfkill switch */
1183 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1190 status = (tp_rfk->ops->get_status)();
1194 rfkill_set_sw_state(tp_rfk->rfkill,
1195 (status == TPACPI_RFK_RADIO_OFF));
1200 /* Query FW and update rfkill sw state for all rfkill switches */
1201 static void tpacpi_rfk_update_swstate_all(void)
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1206 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1210 * Sync the HW-blocking state of all rfkill switches,
1211 * do notice it causes the rfkill core to schedule uevents
1213 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1216 struct tpacpi_rfk *tp_rfk;
1218 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1219 tp_rfk = tpacpi_rfkill_switches[i];
1221 if (rfkill_set_hw_state(tp_rfk->rfkill,
1223 /* ignore -- we track sw block */
1229 /* Call to get the WLSW state from the firmware */
1230 static int hotkey_get_wlsw(void);
1232 /* Call to query WLSW state and update all rfkill switches */
1233 static bool tpacpi_rfk_check_hwblock_state(void)
1235 int res = hotkey_get_wlsw();
1238 /* When unknown or unsupported, we have to assume it is unblocked */
1242 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1243 tpacpi_rfk_update_hwblock_state(hw_blocked);
1248 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1250 struct tpacpi_rfk *tp_rfk = data;
1253 dbg_printk(TPACPI_DBG_RFKILL,
1254 "request to change radio state to %s\n",
1255 blocked ? "blocked" : "unblocked");
1257 /* try to set radio state */
1258 res = (tp_rfk->ops->set_status)(blocked ?
1259 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1261 /* and update the rfkill core with whatever the FW really did */
1262 tpacpi_rfk_update_swstate(tp_rfk);
1264 return (res < 0) ? res : 0;
1267 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1268 .set_block = tpacpi_rfk_hook_set_block,
1271 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1272 const struct tpacpi_rfk_ops *tp_rfkops,
1273 const enum rfkill_type rfktype,
1275 const bool set_default)
1277 struct tpacpi_rfk *atp_rfk;
1279 bool sw_state = false;
1283 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1285 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1287 atp_rfk->rfkill = rfkill_alloc(name,
1290 &tpacpi_rfk_rfkill_ops,
1292 if (!atp_rfk || !atp_rfk->rfkill) {
1293 pr_err("failed to allocate memory for rfkill class\n");
1299 atp_rfk->ops = tp_rfkops;
1301 sw_status = (tp_rfkops->get_status)();
1302 if (sw_status < 0) {
1303 pr_err("failed to read initial state for %s, error %d\n",
1306 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1308 /* try to keep the initial state, since we ask the
1309 * firmware to preserve it across S5 in NVRAM */
1310 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1313 hw_state = tpacpi_rfk_check_hwblock_state();
1314 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1316 res = rfkill_register(atp_rfk->rfkill);
1318 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1319 rfkill_destroy(atp_rfk->rfkill);
1324 tpacpi_rfkill_switches[id] = atp_rfk;
1326 pr_info("rfkill switch %s: radio is %sblocked\n",
1327 name, (sw_state || hw_state) ? "" : "un");
1331 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1333 struct tpacpi_rfk *tp_rfk;
1335 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1337 tp_rfk = tpacpi_rfkill_switches[id];
1339 rfkill_unregister(tp_rfk->rfkill);
1340 rfkill_destroy(tp_rfk->rfkill);
1341 tpacpi_rfkill_switches[id] = NULL;
1346 static void printk_deprecated_rfkill_attribute(const char * const what)
1348 printk_deprecated_attribute(what,
1349 "Please switch to generic rfkill before year 2010");
1352 /* sysfs <radio> enable ------------------------------------------------ */
1353 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1361 /* This is in the ABI... */
1362 if (tpacpi_rfk_check_hwblock_state()) {
1363 status = TPACPI_RFK_RADIO_OFF;
1365 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1370 return snprintf(buf, PAGE_SIZE, "%d\n",
1371 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1374 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1375 struct device_attribute *attr,
1376 const char *buf, size_t count)
1381 printk_deprecated_rfkill_attribute(attr->attr.name);
1383 if (parse_strtoul(buf, 1, &t))
1386 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state() && !!t)
1392 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1393 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1394 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1396 return (res < 0) ? res : count;
1399 /* procfs -------------------------------------------------------------- */
1400 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1402 if (id >= TPACPI_RFK_SW_MAX)
1403 seq_printf(m, "status:\t\tnot supported\n");
1407 /* This is in the ABI... */
1408 if (tpacpi_rfk_check_hwblock_state()) {
1409 status = TPACPI_RFK_RADIO_OFF;
1411 status = tpacpi_rfk_update_swstate(
1412 tpacpi_rfkill_switches[id]);
1417 seq_printf(m, "status:\t\t%s\n",
1418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enabled" : "disabled");
1420 seq_printf(m, "commands:\tenable, disable\n");
1426 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1432 if (id >= TPACPI_RFK_SW_MAX)
1435 while ((cmd = next_cmd(&buf))) {
1436 if (strlencmp(cmd, "enable") == 0)
1437 status = TPACPI_RFK_RADIO_ON;
1438 else if (strlencmp(cmd, "disable") == 0)
1439 status = TPACPI_RFK_RADIO_OFF;
1445 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1446 (status == TPACPI_RFK_RADIO_ON) ?
1447 "enable" : "disable",
1448 tpacpi_rfkill_names[id]);
1449 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1450 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1456 /*************************************************************************
1457 * thinkpad-acpi driver attributes
1460 /* interface_version --------------------------------------------------- */
1461 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1463 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1465 static DRIVER_ATTR_RO(interface_version);
1467 /* debug_level --------------------------------------------------------- */
1468 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1470 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1473 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1478 if (parse_strtoul(buf, 0xffff, &t))
1485 static DRIVER_ATTR_RW(debug_level);
1487 /* version ------------------------------------------------------------- */
1488 static ssize_t version_show(struct device_driver *drv, char *buf)
1490 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1491 TPACPI_DESC, TPACPI_VERSION);
1493 static DRIVER_ATTR_RO(version);
1495 /* --------------------------------------------------------------------- */
1497 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1499 /* wlsw_emulstate ------------------------------------------------------ */
1500 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1502 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1505 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1510 if (parse_strtoul(buf, 1, &t))
1513 if (tpacpi_wlsw_emulstate != !!t) {
1514 tpacpi_wlsw_emulstate = !!t;
1515 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1520 static DRIVER_ATTR_RW(wlsw_emulstate);
1522 /* bluetooth_emulstate ------------------------------------------------- */
1523 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1525 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1528 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1529 const char *buf, size_t count)
1533 if (parse_strtoul(buf, 1, &t))
1536 tpacpi_bluetooth_emulstate = !!t;
1540 static DRIVER_ATTR_RW(bluetooth_emulstate);
1542 /* wwan_emulstate ------------------------------------------------- */
1543 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1548 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1553 if (parse_strtoul(buf, 1, &t))
1556 tpacpi_wwan_emulstate = !!t;
1560 static DRIVER_ATTR_RW(wwan_emulstate);
1562 /* uwb_emulstate ------------------------------------------------- */
1563 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1565 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1568 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1573 if (parse_strtoul(buf, 1, &t))
1576 tpacpi_uwb_emulstate = !!t;
1580 static DRIVER_ATTR_RW(uwb_emulstate);
1583 /* --------------------------------------------------------------------- */
1585 static struct driver_attribute *tpacpi_driver_attributes[] = {
1586 &driver_attr_debug_level, &driver_attr_version,
1587 &driver_attr_interface_version,
1590 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1596 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1597 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1601 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1602 if (!res && dbg_wlswemul)
1603 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1604 if (!res && dbg_bluetoothemul)
1605 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1606 if (!res && dbg_wwanemul)
1607 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1608 if (!res && dbg_uwbemul)
1609 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1619 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1622 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1623 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1624 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1625 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1626 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1630 /*************************************************************************
1635 * Table of recommended minimum BIOS versions
1637 * Reasons for listing:
1638 * 1. Stable BIOS, listed because the unknown amount of
1639 * bugs and bad ACPI behaviour on older versions
1641 * 2. BIOS or EC fw with known bugs that trigger on Linux
1643 * 3. BIOS with known reduced functionality in older versions
1645 * We recommend the latest BIOS and EC version.
1646 * We only support the latest BIOS and EC fw version as a rule.
1648 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1649 * Information from users in ThinkWiki
1651 * WARNING: we use this table also to detect that the machine is
1652 * a ThinkPad in some cases, so don't remove entries lightly.
1655 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1656 { .vendor = (__v), \
1657 .bios = TPID(__id1, __id2), \
1658 .ec = TPACPI_MATCH_ANY, \
1659 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1660 | TPVER(__bv1, __bv2) }
1662 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1663 __eid, __ev1, __ev2) \
1664 { .vendor = (__v), \
1665 .bios = TPID(__bid1, __bid2), \
1667 .quirks = TPVER(__ev1, __ev2) << 16 \
1668 | TPVER(__bv1, __bv2) }
1670 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1671 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1673 /* Outdated IBM BIOSes often lack the EC id string */
1674 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1676 __bv1, __bv2, TPID(__id1, __id2), \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1679 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1682 /* Outdated IBM BIOSes often lack the EC id string */
1683 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1684 __eid1, __eid2, __ev1, __ev2) \
1685 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1686 __bv1, __bv2, TPID(__eid1, __eid2), \
1688 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1689 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1693 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1695 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1696 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1697 __bv1, __bv2, TPID(__id1, __id2), \
1700 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1701 __eid1, __eid2, __ev1, __ev2) \
1702 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1703 __bv1, __bv2, TPID(__eid1, __eid2), \
1706 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1707 /* Numeric models ------------------ */
1708 /* FW MODEL BIOS VERS */
1709 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1710 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1711 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1712 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1713 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1714 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1715 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1716 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1717 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1719 /* A-series ------------------------- */
1720 /* FW MODEL BIOS VERS EC VERS */
1721 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1722 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1723 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1724 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1725 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1726 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1727 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1728 TPV_QI0('1', '3', '2', '0'), /* A22m */
1729 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1730 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1731 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1733 /* G-series ------------------------- */
1734 /* FW MODEL BIOS VERS */
1735 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1736 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1738 /* R-series, T-series --------------- */
1739 /* FW MODEL BIOS VERS EC VERS */
1740 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1741 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1742 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1743 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1744 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1745 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1746 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1747 T40/p, T41/p, T42/p (1) */
1748 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1749 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1750 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1751 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1753 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1754 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1755 TPV_QI0('1', '6', '3', '2'), /* T22 */
1756 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1757 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1758 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1760 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1761 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1762 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1764 /* BIOS FW BIOS VERS EC FW EC VERS */
1765 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1766 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1768 /* X-series ------------------------- */
1769 /* FW MODEL BIOS VERS EC VERS */
1770 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1771 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1772 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1773 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1774 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1775 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1776 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1778 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1779 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1781 /* (0) - older versions lack DMI EC fw string and functionality */
1782 /* (1) - older versions known to lack functionality */
1793 static void __init tpacpi_check_outdated_fw(void)
1795 unsigned long fwvers;
1796 u16 ec_version, bios_version;
1798 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1799 ARRAY_SIZE(tpacpi_bios_version_qtable));
1804 bios_version = fwvers & 0xffffU;
1805 ec_version = (fwvers >> 16) & 0xffffU;
1807 /* note that unknown versions are set to 0x0000 and we use that */
1808 if ((bios_version > thinkpad_id.bios_release) ||
1809 (ec_version > thinkpad_id.ec_release &&
1810 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1812 * The changelogs would let us track down the exact
1813 * reason, but it is just too much of a pain to track
1814 * it. We only list BIOSes that are either really
1815 * broken, or really stable to begin with, so it is
1816 * best if the user upgrades the firmware anyway.
1818 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1819 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1823 static bool __init tpacpi_is_fw_known(void)
1825 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1826 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1829 /****************************************************************************
1830 ****************************************************************************
1834 ****************************************************************************
1835 ****************************************************************************/
1837 /*************************************************************************
1838 * thinkpad-acpi metadata subdriver
1841 static int thinkpad_acpi_driver_read(struct seq_file *m)
1843 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1844 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1848 static struct ibm_struct thinkpad_acpi_driver_data = {
1850 .read = thinkpad_acpi_driver_read,
1853 /*************************************************************************
1858 * ThinkPad firmware event model
1860 * The ThinkPad firmware has two main event interfaces: normal ACPI
1861 * notifications (which follow the ACPI standard), and a private event
1864 * The private event interface also issues events for the hotkeys. As
1865 * the driver gained features, the event handling code ended up being
1866 * built around the hotkey subdriver. This will need to be refactored
1867 * to a more formal event API eventually.
1869 * Some "hotkeys" are actually supposed to be used as event reports,
1870 * such as "brightness has changed", "volume has changed", depending on
1871 * the ThinkPad model and how the firmware is operating.
1873 * Unlike other classes, hotkey-class events have mask/unmask control on
1874 * non-ancient firmware. However, how it behaves changes a lot with the
1875 * firmware model and version.
1878 enum { /* hot key scan codes (derived from ACPI DSDT) */
1879 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1880 TP_ACPI_HOTKEYSCAN_FNF2,
1881 TP_ACPI_HOTKEYSCAN_FNF3,
1882 TP_ACPI_HOTKEYSCAN_FNF4,
1883 TP_ACPI_HOTKEYSCAN_FNF5,
1884 TP_ACPI_HOTKEYSCAN_FNF6,
1885 TP_ACPI_HOTKEYSCAN_FNF7,
1886 TP_ACPI_HOTKEYSCAN_FNF8,
1887 TP_ACPI_HOTKEYSCAN_FNF9,
1888 TP_ACPI_HOTKEYSCAN_FNF10,
1889 TP_ACPI_HOTKEYSCAN_FNF11,
1890 TP_ACPI_HOTKEYSCAN_FNF12,
1891 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1892 TP_ACPI_HOTKEYSCAN_FNINSERT,
1893 TP_ACPI_HOTKEYSCAN_FNDELETE,
1894 TP_ACPI_HOTKEYSCAN_FNHOME,
1895 TP_ACPI_HOTKEYSCAN_FNEND,
1896 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1897 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1898 TP_ACPI_HOTKEYSCAN_FNSPACE,
1899 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1900 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1901 TP_ACPI_HOTKEYSCAN_MUTE,
1902 TP_ACPI_HOTKEYSCAN_THINKPAD,
1903 TP_ACPI_HOTKEYSCAN_UNK1,
1904 TP_ACPI_HOTKEYSCAN_UNK2,
1905 TP_ACPI_HOTKEYSCAN_UNK3,
1906 TP_ACPI_HOTKEYSCAN_UNK4,
1907 TP_ACPI_HOTKEYSCAN_UNK5,
1908 TP_ACPI_HOTKEYSCAN_UNK6,
1909 TP_ACPI_HOTKEYSCAN_UNK7,
1910 TP_ACPI_HOTKEYSCAN_UNK8,
1912 /* Adaptive keyboard keycodes */
1913 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1914 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1915 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1916 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1917 TP_ACPI_HOTKEYSCAN_CLOUD,
1918 TP_ACPI_HOTKEYSCAN_UNK9,
1919 TP_ACPI_HOTKEYSCAN_VOICE,
1920 TP_ACPI_HOTKEYSCAN_UNK10,
1921 TP_ACPI_HOTKEYSCAN_GESTURES,
1922 TP_ACPI_HOTKEYSCAN_UNK11,
1923 TP_ACPI_HOTKEYSCAN_UNK12,
1924 TP_ACPI_HOTKEYSCAN_UNK13,
1925 TP_ACPI_HOTKEYSCAN_CONFIG,
1926 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1927 TP_ACPI_HOTKEYSCAN_RELOAD,
1928 TP_ACPI_HOTKEYSCAN_BACK,
1929 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1930 TP_ACPI_HOTKEYSCAN_MIC_UP,
1931 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1932 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1933 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1935 /* Lenovo extended keymap, starting at 0x1300 */
1936 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1937 /* first new observed key (star, favorites) is 0x1311 */
1938 TP_ACPI_HOTKEYSCAN_STAR = 69,
1939 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1940 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1941 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1942 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1944 /* Hotkey keymap size */
1945 TPACPI_HOTKEY_MAP_LEN
1948 enum { /* Keys/events available through NVRAM polling */
1949 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1950 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1953 enum { /* Positions of some of the keys in hotkey masks */
1954 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1955 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1956 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1957 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1958 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1959 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1960 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1961 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1962 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1963 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1964 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1967 enum { /* NVRAM to ACPI HKEY group map */
1968 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1969 TP_ACPI_HKEY_ZOOM_MASK |
1970 TP_ACPI_HKEY_DISPSWTCH_MASK |
1971 TP_ACPI_HKEY_HIBERNATE_MASK,
1972 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1973 TP_ACPI_HKEY_BRGHTDWN_MASK,
1974 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1975 TP_ACPI_HKEY_VOLDWN_MASK |
1976 TP_ACPI_HKEY_MUTE_MASK,
1979 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1980 struct tp_nvram_state {
1981 u16 thinkpad_toggle:1;
1983 u16 display_toggle:1;
1984 u16 thinklight_toggle:1;
1985 u16 hibernate_toggle:1;
1986 u16 displayexp_toggle:1;
1987 u16 display_state:1;
1988 u16 brightness_toggle:1;
1989 u16 volume_toggle:1;
1992 u8 brightness_level;
1996 /* kthread for the hotkey poller */
1997 static struct task_struct *tpacpi_hotkey_task;
2000 * Acquire mutex to write poller control variables as an
2003 * Increment hotkey_config_change when changing them if you
2004 * want the kthread to forget old state.
2006 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2008 static struct mutex hotkey_thread_data_mutex;
2009 static unsigned int hotkey_config_change;
2012 * hotkey poller control variables
2014 * Must be atomic or readers will also need to acquire mutex
2016 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2017 * should be used only when the changes need to be taken as
2018 * a block, OR when one needs to force the kthread to forget
2021 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2022 static unsigned int hotkey_poll_freq = 10; /* Hz */
2024 #define HOTKEY_CONFIG_CRITICAL_START \
2026 mutex_lock(&hotkey_thread_data_mutex); \
2027 hotkey_config_change++; \
2029 #define HOTKEY_CONFIG_CRITICAL_END \
2030 mutex_unlock(&hotkey_thread_data_mutex);
2032 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2034 #define hotkey_source_mask 0U
2035 #define HOTKEY_CONFIG_CRITICAL_START
2036 #define HOTKEY_CONFIG_CRITICAL_END
2038 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2040 static struct mutex hotkey_mutex;
2042 static enum { /* Reasons for waking up */
2043 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2044 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2045 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2046 } hotkey_wakeup_reason;
2048 static int hotkey_autosleep_ack;
2050 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2051 static u32 hotkey_all_mask; /* all events supported in fw */
2052 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2053 static u32 hotkey_reserved_mask; /* events better left disabled */
2054 static u32 hotkey_driver_mask; /* events needed by the driver */
2055 static u32 hotkey_user_mask; /* events visible to userspace */
2056 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2058 static u16 *hotkey_keycode_map;
2060 static struct attribute_set *hotkey_dev_attributes;
2062 static void tpacpi_driver_event(const unsigned int hkey_event);
2063 static void hotkey_driver_event(const unsigned int scancode);
2064 static void hotkey_poll_setup(const bool may_warn);
2066 /* HKEY.MHKG() return bits */
2067 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2069 TP_ACPI_MULTI_MODE_INVALID = 0,
2070 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2071 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2072 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2073 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2074 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2075 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2076 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2080 /* The following modes are considered tablet mode for the purpose of
2081 * reporting the status to userspace. i.e. in all these modes it makes
2082 * sense to disable the laptop input devices such as touchpad and
2085 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2086 TP_ACPI_MULTI_MODE_STAND |
2087 TP_ACPI_MULTI_MODE_TENT |
2088 TP_ACPI_MULTI_MODE_STAND_TENT,
2091 static int hotkey_get_wlsw(void)
2095 if (!tp_features.hotkey_wlsw)
2098 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2100 return (tpacpi_wlsw_emulstate) ?
2101 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2104 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2107 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2110 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2112 int type = (s >> 16) & 0xffff;
2113 int value = s & 0xffff;
2114 int mode = TP_ACPI_MULTI_MODE_INVALID;
2115 int valid_modes = 0;
2117 if (has_tablet_mode)
2118 *has_tablet_mode = 0;
2122 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2123 TP_ACPI_MULTI_MODE_TABLET |
2124 TP_ACPI_MULTI_MODE_STAND_TENT;
2127 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2128 TP_ACPI_MULTI_MODE_FLAT |
2129 TP_ACPI_MULTI_MODE_TABLET |
2130 TP_ACPI_MULTI_MODE_STAND |
2131 TP_ACPI_MULTI_MODE_TENT;
2134 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2135 TP_ACPI_MULTI_MODE_FLAT;
2139 /* In mode 4, FLAT is not specified as a valid mode. However,
2140 * it can be seen at least on the X1 Yoga 2nd Generation.
2142 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2143 TP_ACPI_MULTI_MODE_FLAT |
2144 TP_ACPI_MULTI_MODE_TABLET |
2145 TP_ACPI_MULTI_MODE_STAND |
2146 TP_ACPI_MULTI_MODE_TENT;
2149 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2150 type, value, TPACPI_MAIL);
2154 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2155 *has_tablet_mode = 1;
2159 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2162 mode = TP_ACPI_MULTI_MODE_FLAT;
2165 mode = TP_ACPI_MULTI_MODE_TABLET;
2169 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2171 mode = TP_ACPI_MULTI_MODE_STAND;
2174 mode = TP_ACPI_MULTI_MODE_TENT;
2177 if (type == 5 && value == 0xffff) {
2178 pr_warn("Multi mode status is undetected, assuming laptop\n");
2183 if (!(mode & valid_modes)) {
2184 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2185 value, type, TPACPI_MAIL);
2189 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2192 static int hotkey_get_tablet_mode(int *status)
2196 switch (tp_features.hotkey_tablet) {
2197 case TP_HOTKEY_TABLET_USES_MHKG:
2198 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2201 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2203 case TP_HOTKEY_TABLET_USES_GMMS:
2204 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2207 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2217 * Reads current event mask from firmware, and updates
2218 * hotkey_acpi_mask accordingly. Also resets any bits
2219 * from hotkey_user_mask that are unavailable to be
2220 * delivered (shadow requirement of the userspace ABI).
2222 * Call with hotkey_mutex held
2224 static int hotkey_mask_get(void)
2226 if (tp_features.hotkey_mask) {
2229 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2232 hotkey_acpi_mask = m;
2234 /* no mask support doesn't mean no event support... */
2235 hotkey_acpi_mask = hotkey_all_mask;
2238 /* sync userspace-visible mask */
2239 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2244 static void hotkey_mask_warn_incomplete_mask(void)
2246 /* log only what the user can fix... */
2247 const u32 wantedmask = hotkey_driver_mask &
2248 ~(hotkey_acpi_mask | hotkey_source_mask) &
2249 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2252 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2256 * Set the firmware mask when supported
2258 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2260 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2262 * Call with hotkey_mutex held
2264 static int hotkey_mask_set(u32 mask)
2269 const u32 fwmask = mask & ~hotkey_source_mask;
2271 if (tp_features.hotkey_mask) {
2272 for (i = 0; i < 32; i++) {
2273 if (!acpi_evalf(hkey_handle,
2274 NULL, "MHKM", "vdd", i + 1,
2275 !!(mask & (1 << i)))) {
2283 * We *must* make an inconditional call to hotkey_mask_get to
2284 * refresh hotkey_acpi_mask and update hotkey_user_mask
2286 * Take the opportunity to also log when we cannot _enable_
2289 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2290 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2291 fwmask, hotkey_acpi_mask);
2294 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2295 hotkey_mask_warn_incomplete_mask();
2301 * Sets hotkey_user_mask and tries to set the firmware mask
2303 * Call with hotkey_mutex held
2305 static int hotkey_user_mask_set(const u32 mask)
2309 /* Give people a chance to notice they are doing something that
2310 * is bound to go boom on their users sooner or later */
2311 if (!tp_warned.hotkey_mask_ff &&
2312 (mask == 0xffff || mask == 0xffffff ||
2313 mask == 0xffffffff)) {
2314 tp_warned.hotkey_mask_ff = 1;
2315 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2317 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2320 /* Try to enable what the user asked for, plus whatever we need.
2321 * this syncs everything but won't enable bits in hotkey_user_mask */
2322 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2324 /* Enable the available bits in hotkey_user_mask */
2325 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2331 * Sets the driver hotkey mask.
2333 * Can be called even if the hotkey subdriver is inactive
2335 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2339 /* Do the right thing if hotkey_init has not been called yet */
2340 if (!tp_features.hotkey) {
2341 hotkey_driver_mask = mask;
2345 mutex_lock(&hotkey_mutex);
2347 HOTKEY_CONFIG_CRITICAL_START
2348 hotkey_driver_mask = mask;
2349 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2350 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2352 HOTKEY_CONFIG_CRITICAL_END
2354 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2355 ~hotkey_source_mask);
2356 hotkey_poll_setup(true);
2358 mutex_unlock(&hotkey_mutex);
2363 static int hotkey_status_get(int *status)
2365 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2371 static int hotkey_status_set(bool enable)
2373 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2379 static void tpacpi_input_send_tabletsw(void)
2383 if (tp_features.hotkey_tablet &&
2384 !hotkey_get_tablet_mode(&state)) {
2385 mutex_lock(&tpacpi_inputdev_send_mutex);
2387 input_report_switch(tpacpi_inputdev,
2388 SW_TABLET_MODE, !!state);
2389 input_sync(tpacpi_inputdev);
2391 mutex_unlock(&tpacpi_inputdev_send_mutex);
2395 /* Do NOT call without validating scancode first */
2396 static void tpacpi_input_send_key(const unsigned int scancode)
2398 const unsigned int keycode = hotkey_keycode_map[scancode];
2400 if (keycode != KEY_RESERVED) {
2401 mutex_lock(&tpacpi_inputdev_send_mutex);
2403 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2404 input_report_key(tpacpi_inputdev, keycode, 1);
2405 input_sync(tpacpi_inputdev);
2407 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2408 input_report_key(tpacpi_inputdev, keycode, 0);
2409 input_sync(tpacpi_inputdev);
2411 mutex_unlock(&tpacpi_inputdev_send_mutex);
2415 /* Do NOT call without validating scancode first */
2416 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2418 hotkey_driver_event(scancode);
2419 if (hotkey_user_mask & (1 << scancode))
2420 tpacpi_input_send_key(scancode);
2423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2424 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2426 /* Do NOT call without validating scancode first */
2427 static void tpacpi_hotkey_send_key(unsigned int scancode)
2429 tpacpi_input_send_key_masked(scancode);
2432 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2436 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2437 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2438 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2439 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2440 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2441 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2443 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2444 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2445 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2447 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2448 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2449 n->displayexp_toggle =
2450 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2452 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2453 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2454 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2455 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2456 n->brightness_toggle =
2457 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2459 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2460 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2461 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2462 >> TP_NVRAM_POS_LEVEL_VOLUME;
2463 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2464 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2468 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2470 if ((event_mask & (1 << __scancode)) && \
2471 oldn->__member != newn->__member) \
2472 tpacpi_hotkey_send_key(__scancode); \
2475 #define TPACPI_MAY_SEND_KEY(__scancode) \
2477 if (event_mask & (1 << __scancode)) \
2478 tpacpi_hotkey_send_key(__scancode); \
2481 static void issue_volchange(const unsigned int oldvol,
2482 const unsigned int newvol,
2483 const u32 event_mask)
2485 unsigned int i = oldvol;
2487 while (i > newvol) {
2488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2491 while (i < newvol) {
2492 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2497 static void issue_brightnesschange(const unsigned int oldbrt,
2498 const unsigned int newbrt,
2499 const u32 event_mask)
2501 unsigned int i = oldbrt;
2503 while (i > newbrt) {
2504 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2507 while (i < newbrt) {
2508 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2513 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2514 struct tp_nvram_state *newn,
2515 const u32 event_mask)
2518 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2519 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2520 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2521 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2523 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2525 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2530 * This code is supposed to duplicate the IBM firmware behaviour:
2531 * - Pressing MUTE issues mute hotkey message, even when already mute
2532 * - Pressing Volume up/down issues volume up/down hotkey messages,
2533 * even when already at maximum or minimum volume
2534 * - The act of unmuting issues volume up/down notification,
2535 * depending which key was used to unmute
2537 * We are constrained to what the NVRAM can tell us, which is not much
2538 * and certainly not enough if more than one volume hotkey was pressed
2539 * since the last poll cycle.
2541 * Just to make our life interesting, some newer Lenovo ThinkPads have
2542 * bugs in the BIOS and may fail to update volume_toggle properly.
2547 oldn->volume_toggle != newn->volume_toggle ||
2548 oldn->volume_level != newn->volume_level) {
2549 /* recently muted, or repeated mute keypress, or
2550 * multiple presses ending in mute */
2551 issue_volchange(oldn->volume_level, newn->volume_level,
2553 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2558 /* recently unmuted, issue 'unmute' keypress */
2559 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2561 if (oldn->volume_level != newn->volume_level) {
2562 issue_volchange(oldn->volume_level, newn->volume_level,
2564 } else if (oldn->volume_toggle != newn->volume_toggle) {
2565 /* repeated vol up/down keypress at end of scale ? */
2566 if (newn->volume_level == 0)
2567 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2568 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2569 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2573 /* handle brightness */
2574 if (oldn->brightness_level != newn->brightness_level) {
2575 issue_brightnesschange(oldn->brightness_level,
2576 newn->brightness_level, event_mask);
2577 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2578 /* repeated key presses that didn't change state */
2579 if (newn->brightness_level == 0)
2580 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2581 else if (newn->brightness_level >= bright_maxlvl
2582 && !tp_features.bright_unkfw)
2583 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2586 #undef TPACPI_COMPARE_KEY
2587 #undef TPACPI_MAY_SEND_KEY
2593 * We track all events in hotkey_source_mask all the time, since
2594 * most of them are edge-based. We only issue those requested by
2595 * hotkey_user_mask or hotkey_driver_mask, though.
2597 static int hotkey_kthread(void *data)
2599 struct tp_nvram_state s[2];
2600 u32 poll_mask, event_mask;
2601 unsigned int si, so;
2603 unsigned int change_detector;
2604 unsigned int poll_freq;
2607 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2616 /* Initial state for compares */
2617 mutex_lock(&hotkey_thread_data_mutex);
2618 change_detector = hotkey_config_change;
2619 poll_mask = hotkey_source_mask;
2620 event_mask = hotkey_source_mask &
2621 (hotkey_driver_mask | hotkey_user_mask);
2622 poll_freq = hotkey_poll_freq;
2623 mutex_unlock(&hotkey_thread_data_mutex);
2624 hotkey_read_nvram(&s[so], poll_mask);
2626 while (!kthread_should_stop()) {
2628 if (likely(poll_freq))
2631 t = 100; /* should never happen... */
2633 t = msleep_interruptible(t);
2634 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2637 if (t > 0 && !was_frozen)
2640 mutex_lock(&hotkey_thread_data_mutex);
2641 if (was_frozen || hotkey_config_change != change_detector) {
2642 /* forget old state on thaw or config change */
2645 change_detector = hotkey_config_change;
2647 poll_mask = hotkey_source_mask;
2648 event_mask = hotkey_source_mask &
2649 (hotkey_driver_mask | hotkey_user_mask);
2650 poll_freq = hotkey_poll_freq;
2651 mutex_unlock(&hotkey_thread_data_mutex);
2653 if (likely(poll_mask)) {
2654 hotkey_read_nvram(&s[si], poll_mask);
2655 if (likely(si != so)) {
2656 hotkey_compare_and_issue_event(&s[so], &s[si],
2669 /* call with hotkey_mutex held */
2670 static void hotkey_poll_stop_sync(void)
2672 if (tpacpi_hotkey_task) {
2673 kthread_stop(tpacpi_hotkey_task);
2674 tpacpi_hotkey_task = NULL;
2678 /* call with hotkey_mutex held */
2679 static void hotkey_poll_setup(const bool may_warn)
2681 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2682 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2684 if (hotkey_poll_freq > 0 &&
2685 (poll_driver_mask ||
2686 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2687 if (!tpacpi_hotkey_task) {
2688 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2689 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2690 if (IS_ERR(tpacpi_hotkey_task)) {
2691 tpacpi_hotkey_task = NULL;
2692 pr_err("could not create kernel thread for hotkey polling\n");
2696 hotkey_poll_stop_sync();
2697 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2698 hotkey_poll_freq == 0) {
2699 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2700 poll_user_mask, poll_driver_mask);
2705 static void hotkey_poll_setup_safe(const bool may_warn)
2707 mutex_lock(&hotkey_mutex);
2708 hotkey_poll_setup(may_warn);
2709 mutex_unlock(&hotkey_mutex);
2712 /* call with hotkey_mutex held */
2713 static void hotkey_poll_set_freq(unsigned int freq)
2716 hotkey_poll_stop_sync();
2718 hotkey_poll_freq = freq;
2721 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2723 static void hotkey_poll_setup(const bool __unused)
2727 static void hotkey_poll_setup_safe(const bool __unused)
2731 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2733 static int hotkey_inputdev_open(struct input_dev *dev)
2735 switch (tpacpi_lifecycle) {
2736 case TPACPI_LIFE_INIT:
2737 case TPACPI_LIFE_RUNNING:
2738 hotkey_poll_setup_safe(false);
2740 case TPACPI_LIFE_EXITING:
2744 /* Should only happen if tpacpi_lifecycle is corrupt */
2749 static void hotkey_inputdev_close(struct input_dev *dev)
2751 /* disable hotkey polling when possible */
2752 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2753 !(hotkey_source_mask & hotkey_driver_mask))
2754 hotkey_poll_setup_safe(false);
2757 /* sysfs hotkey enable ------------------------------------------------- */
2758 static ssize_t hotkey_enable_show(struct device *dev,
2759 struct device_attribute *attr,
2764 printk_deprecated_attribute("hotkey_enable",
2765 "Hotkey reporting is always enabled");
2767 res = hotkey_status_get(&status);
2771 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2774 static ssize_t hotkey_enable_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2780 printk_deprecated_attribute("hotkey_enable",
2781 "Hotkeys can be disabled through hotkey_mask");
2783 if (parse_strtoul(buf, 1, &t))
2792 static DEVICE_ATTR_RW(hotkey_enable);
2794 /* sysfs hotkey mask --------------------------------------------------- */
2795 static ssize_t hotkey_mask_show(struct device *dev,
2796 struct device_attribute *attr,
2799 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2802 static ssize_t hotkey_mask_store(struct device *dev,
2803 struct device_attribute *attr,
2804 const char *buf, size_t count)
2809 if (parse_strtoul(buf, 0xffffffffUL, &t))
2812 if (mutex_lock_killable(&hotkey_mutex))
2813 return -ERESTARTSYS;
2815 res = hotkey_user_mask_set(t);
2817 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2818 hotkey_poll_setup(true);
2821 mutex_unlock(&hotkey_mutex);
2823 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2825 return (res) ? res : count;
2828 static DEVICE_ATTR_RW(hotkey_mask);
2830 /* sysfs hotkey bios_enabled ------------------------------------------- */
2831 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2832 struct device_attribute *attr,
2835 return sprintf(buf, "0\n");
2838 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2840 /* sysfs hotkey bios_mask ---------------------------------------------- */
2841 static ssize_t hotkey_bios_mask_show(struct device *dev,
2842 struct device_attribute *attr,
2845 printk_deprecated_attribute("hotkey_bios_mask",
2846 "This attribute is useless.");
2847 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2850 static DEVICE_ATTR_RO(hotkey_bios_mask);
2852 /* sysfs hotkey all_mask ----------------------------------------------- */
2853 static ssize_t hotkey_all_mask_show(struct device *dev,
2854 struct device_attribute *attr,
2857 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2858 hotkey_all_mask | hotkey_source_mask);
2861 static DEVICE_ATTR_RO(hotkey_all_mask);
2863 /* sysfs hotkey all_mask ----------------------------------------------- */
2864 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2865 struct device_attribute *attr,
2868 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2869 hotkey_adaptive_all_mask | hotkey_source_mask);
2872 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2874 /* sysfs hotkey recommended_mask --------------------------------------- */
2875 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2876 struct device_attribute *attr,
2879 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2880 (hotkey_all_mask | hotkey_source_mask)
2881 & ~hotkey_reserved_mask);
2884 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2886 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2888 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2889 static ssize_t hotkey_source_mask_show(struct device *dev,
2890 struct device_attribute *attr,
2893 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2896 static ssize_t hotkey_source_mask_store(struct device *dev,
2897 struct device_attribute *attr,
2898 const char *buf, size_t count)
2904 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2905 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2908 if (mutex_lock_killable(&hotkey_mutex))
2909 return -ERESTARTSYS;
2911 HOTKEY_CONFIG_CRITICAL_START
2912 hotkey_source_mask = t;
2913 HOTKEY_CONFIG_CRITICAL_END
2915 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2916 ~hotkey_source_mask);
2917 hotkey_poll_setup(true);
2919 /* check if events needed by the driver got disabled */
2920 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2921 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2923 mutex_unlock(&hotkey_mutex);
2926 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2929 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2932 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2934 return (rc < 0) ? rc : count;
2937 static DEVICE_ATTR_RW(hotkey_source_mask);
2939 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2940 static ssize_t hotkey_poll_freq_show(struct device *dev,
2941 struct device_attribute *attr,
2944 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2947 static ssize_t hotkey_poll_freq_store(struct device *dev,
2948 struct device_attribute *attr,
2949 const char *buf, size_t count)
2953 if (parse_strtoul(buf, 25, &t))
2956 if (mutex_lock_killable(&hotkey_mutex))
2957 return -ERESTARTSYS;
2959 hotkey_poll_set_freq(t);
2960 hotkey_poll_setup(true);
2962 mutex_unlock(&hotkey_mutex);
2964 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2969 static DEVICE_ATTR_RW(hotkey_poll_freq);
2971 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2973 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2974 static ssize_t hotkey_radio_sw_show(struct device *dev,
2975 struct device_attribute *attr,
2979 res = hotkey_get_wlsw();
2983 /* Opportunistic update */
2984 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2986 return snprintf(buf, PAGE_SIZE, "%d\n",
2987 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2990 static DEVICE_ATTR_RO(hotkey_radio_sw);
2992 static void hotkey_radio_sw_notify_change(void)
2994 if (tp_features.hotkey_wlsw)
2995 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2999 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
3000 static ssize_t hotkey_tablet_mode_show(struct device *dev,
3001 struct device_attribute *attr,
3005 res = hotkey_get_tablet_mode(&s);
3009 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3012 static DEVICE_ATTR_RO(hotkey_tablet_mode);
3014 static void hotkey_tablet_mode_notify_change(void)
3016 if (tp_features.hotkey_tablet)
3017 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3018 "hotkey_tablet_mode");
3021 /* sysfs wakeup reason (pollable) -------------------------------------- */
3022 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3023 struct device_attribute *attr,
3026 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3029 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3031 static void hotkey_wakeup_reason_notify_change(void)
3033 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3037 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3038 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3039 struct device_attribute *attr,
3042 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3045 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3046 hotkey_wakeup_hotunplug_complete_show, NULL);
3048 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3050 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3051 "wakeup_hotunplug_complete");
3054 /* sysfs adaptive kbd mode --------------------------------------------- */
3056 static int adaptive_keyboard_get_mode(void);
3057 static int adaptive_keyboard_set_mode(int new_mode);
3059 enum ADAPTIVE_KEY_MODE {
3062 WEB_CONFERENCE_MODE,
3067 static ssize_t adaptive_kbd_mode_show(struct device *dev,
3068 struct device_attribute *attr,
3073 current_mode = adaptive_keyboard_get_mode();
3074 if (current_mode < 0)
3075 return current_mode;
3077 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3080 static ssize_t adaptive_kbd_mode_store(struct device *dev,
3081 struct device_attribute *attr,
3082 const char *buf, size_t count)
3087 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3090 res = adaptive_keyboard_set_mode(t);
3091 return (res < 0) ? res : count;
3094 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3096 static struct attribute *adaptive_kbd_attributes[] = {
3097 &dev_attr_adaptive_kbd_mode.attr,
3101 static const struct attribute_group adaptive_kbd_attr_group = {
3102 .attrs = adaptive_kbd_attributes,
3105 /* --------------------------------------------------------------------- */
3107 static struct attribute *hotkey_attributes[] __initdata = {
3108 &dev_attr_hotkey_enable.attr,
3109 &dev_attr_hotkey_bios_enabled.attr,
3110 &dev_attr_hotkey_bios_mask.attr,
3111 &dev_attr_wakeup_reason.attr,
3112 &dev_attr_wakeup_hotunplug_complete.attr,
3113 &dev_attr_hotkey_mask.attr,
3114 &dev_attr_hotkey_all_mask.attr,
3115 &dev_attr_hotkey_adaptive_all_mask.attr,
3116 &dev_attr_hotkey_recommended_mask.attr,
3117 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3118 &dev_attr_hotkey_source_mask.attr,
3119 &dev_attr_hotkey_poll_freq.attr,
3124 * Sync both the hw and sw blocking state of all switches
3126 static void tpacpi_send_radiosw_update(void)
3131 * We must sync all rfkill controllers *before* issuing any
3132 * rfkill input events, or we will race the rfkill core input
3135 * tpacpi_inputdev_send_mutex works as a synchronization point
3138 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3141 wlsw = hotkey_get_wlsw();
3143 /* Sync hw blocking state first if it is hw-blocked */
3144 if (wlsw == TPACPI_RFK_RADIO_OFF)
3145 tpacpi_rfk_update_hwblock_state(true);
3147 /* Sync sw blocking state */
3148 tpacpi_rfk_update_swstate_all();
3150 /* Sync hw blocking state last if it is hw-unblocked */
3151 if (wlsw == TPACPI_RFK_RADIO_ON)
3152 tpacpi_rfk_update_hwblock_state(false);
3154 /* Issue rfkill input event for WLSW switch */
3156 mutex_lock(&tpacpi_inputdev_send_mutex);
3158 input_report_switch(tpacpi_inputdev,
3159 SW_RFKILL_ALL, (wlsw > 0));
3160 input_sync(tpacpi_inputdev);
3162 mutex_unlock(&tpacpi_inputdev_send_mutex);
3166 * this can be unconditional, as we will poll state again
3167 * if userspace uses the notify to read data
3169 hotkey_radio_sw_notify_change();
3172 static void hotkey_exit(void)
3174 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3175 mutex_lock(&hotkey_mutex);
3176 hotkey_poll_stop_sync();
3177 mutex_unlock(&hotkey_mutex);
3180 if (hotkey_dev_attributes)
3181 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3183 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3184 "restoring original HKEY status and mask\n");
3185 /* yes, there is a bitwise or below, we want the
3186 * functions to be called even if one of them fail */
3187 if (((tp_features.hotkey_mask &&
3188 hotkey_mask_set(hotkey_orig_mask)) |
3189 hotkey_status_set(false)) != 0)
3190 pr_err("failed to restore hot key mask to BIOS defaults\n");
3193 static void __init hotkey_unmap(const unsigned int scancode)
3195 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3196 clear_bit(hotkey_keycode_map[scancode],
3197 tpacpi_inputdev->keybit);
3198 hotkey_keycode_map[scancode] = KEY_RESERVED;
3204 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3207 #define TPACPI_HK_Q_INIMASK 0x0001
3209 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3210 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3211 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3212 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3213 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3214 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3215 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3216 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3217 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3218 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3219 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3220 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3221 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3222 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3223 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3224 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3225 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3226 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3227 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3228 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3231 typedef u16 tpacpi_keymap_entry_t;
3232 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3234 static int hotkey_init_tablet_mode(void)
3236 int in_tablet_mode = 0, res;
3239 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3240 int has_tablet_mode;
3242 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3244 if (has_tablet_mode)
3245 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3247 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3248 /* For X41t, X60t, X61t Tablets... */
3249 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3250 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3254 if (!tp_features.hotkey_tablet)
3257 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3258 type, in_tablet_mode ? "tablet" : "laptop");
3260 res = add_to_attr_set(hotkey_dev_attributes,
3261 &dev_attr_hotkey_tablet_mode.attr);
3265 return in_tablet_mode;
3268 static int __init hotkey_init(struct ibm_init_struct *iibm)
3270 /* Requirements for changing the default keymaps:
3272 * 1. Many of the keys are mapped to KEY_RESERVED for very
3273 * good reasons. Do not change them unless you have deep
3274 * knowledge on the IBM and Lenovo ThinkPad firmware for
3275 * the various ThinkPad models. The driver behaves
3276 * differently for KEY_RESERVED: such keys have their
3277 * hot key mask *unset* in mask_recommended, and also
3278 * in the initial hot key mask programmed into the
3279 * firmware at driver load time, which means the firm-
3280 * ware may react very differently if you change them to
3283 * 2. You must be subscribed to the linux-thinkpad and
3284 * ibm-acpi-devel mailing lists, and you should read the
3285 * list archives since 2007 if you want to change the
3286 * keymaps. This requirement exists so that you will
3287 * know the past history of problems with the thinkpad-
3288 * acpi driver keymaps, and also that you will be
3289 * listening to any bug reports;
3291 * 3. Do not send thinkpad-acpi specific patches directly to
3292 * for merging, *ever*. Send them to the linux-acpi
3293 * mailinglist for comments. Merging is to be done only
3294 * through acpi-test and the ACPI maintainer.
3296 * If the above is too much to ask, don't change the keymap.
3297 * Ask the thinkpad-acpi maintainer to do it, instead.
3301 TPACPI_KEYMAP_IBM_GENERIC = 0,
3302 TPACPI_KEYMAP_LENOVO_GENERIC,
3305 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3306 /* Generic keymap for IBM ThinkPads */
3307 [TPACPI_KEYMAP_IBM_GENERIC] = {
3308 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3309 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3310 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3311 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3313 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3314 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3315 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3316 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3318 /* brightness: firmware always reacts to them */
3319 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3320 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3322 /* Thinklight: firmware always react to it */
3323 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3325 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3326 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3328 /* Volume: firmware always react to it and reprograms
3329 * the built-in *extra* mixer. Never map it to control
3330 * another mixer by default. */
3331 KEY_RESERVED, /* 0x14: VOLUME UP */
3332 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3333 KEY_RESERVED, /* 0x16: MUTE */
3335 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3337 /* (assignments unknown, please report if found) */
3338 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3341 /* No assignments, only used for Adaptive keyboards. */
3342 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3345 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3346 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3348 /* No assignment, used for newer Lenovo models */
3349 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3350 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3351 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3352 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3353 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3354 KEY_UNKNOWN, KEY_UNKNOWN
3358 /* Generic keymap for Lenovo ThinkPads */
3359 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3360 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3361 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3362 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3363 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3365 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3366 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3367 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3368 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3370 /* These should be enabled --only-- when ACPI video
3371 * is disabled (i.e. in "vendor" mode), and are handled
3372 * in a special way by the init code */
3373 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3374 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3376 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3378 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3379 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3381 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3382 * react to it and reprograms the built-in *extra* mixer.
3383 * Never map it to control another mixer by default.
3385 * T60?, T61, R60?, R61: firmware and EC tries to send
3386 * these over the regular keyboard, so these are no-ops,
3387 * but there are still weird bugs re. MUTE, so do not
3388 * change unless you get test reports from all Lenovo
3389 * models. May cause the BIOS to interfere with the
3392 KEY_RESERVED, /* 0x14: VOLUME UP */
3393 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3394 KEY_RESERVED, /* 0x16: MUTE */
3396 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3398 /* (assignments unknown, please report if found) */
3399 KEY_UNKNOWN, KEY_UNKNOWN,
3402 * The mic mute button only sends 0x1a. It does not
3403 * automatically mute the mic or change the mute light.
3405 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3407 /* (assignments unknown, please report if found) */
3410 /* Extra keys in use since the X240 / T440 / T540 */
3411 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3414 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3415 * The first item in this list is the Mute button which is
3416 * emitted with 0x103 through
3417 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3419 * We'll need to offset those by 0x20.
3421 KEY_RESERVED, /* Mute held, 0x103 */
3422 KEY_BRIGHTNESS_MIN, /* Backlight off */
3423 KEY_RESERVED, /* Clipping tool */
3424 KEY_RESERVED, /* Cloud */
3426 KEY_VOICECOMMAND, /* Voice */
3428 KEY_RESERVED, /* Gestures */
3432 KEY_CONFIG, /* Settings */
3433 KEY_RESERVED, /* New tab */
3434 KEY_REFRESH, /* Reload */
3435 KEY_BACK, /* Back */
3436 KEY_RESERVED, /* Microphone down */
3437 KEY_RESERVED, /* Microphone up */
3438 KEY_RESERVED, /* Microphone cancellation */
3439 KEY_RESERVED, /* Camera mode */
3440 KEY_RESERVED, /* Rotate display, 0x116 */
3443 * These are found in 2017 models (e.g. T470s, X270).
3444 * The lowest known value is 0x311, which according to
3445 * the manual should launch a user defined favorite
3448 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3449 * corresponding to 0x34.
3452 /* (assignments unknown, please report if found) */
3453 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3454 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3455 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3456 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3459 KEY_FAVORITES, /* Favorite app, 0x311 */
3460 KEY_RESERVED, /* Clipping tool */
3461 KEY_CALC, /* Calculator (above numpad, P52) */
3462 KEY_BLUETOOTH, /* Bluetooth */
3463 KEY_KEYBOARD /* Keyboard, 0x315 */
3467 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3468 /* Generic maps (fallback) */
3470 .vendor = PCI_VENDOR_ID_IBM,
3471 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3472 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3475 .vendor = PCI_VENDOR_ID_LENOVO,
3476 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3477 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3481 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3482 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3487 bool radiosw_state = false;
3488 bool tabletsw_state = false;
3490 unsigned long quirks;
3491 unsigned long keymap_id;
3493 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3494 "initializing hotkey subdriver\n");
3496 BUG_ON(!tpacpi_inputdev);
3497 BUG_ON(tpacpi_inputdev->open != NULL ||
3498 tpacpi_inputdev->close != NULL);
3500 TPACPI_ACPIHANDLE_INIT(hkey);
3501 mutex_init(&hotkey_mutex);
3503 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3504 mutex_init(&hotkey_thread_data_mutex);
3507 /* hotkey not supported on 570 */
3508 tp_features.hotkey = hkey_handle != NULL;
3510 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3512 str_supported(tp_features.hotkey));
3514 if (!tp_features.hotkey)
3517 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3518 ARRAY_SIZE(tpacpi_hotkey_qtable));
3520 tpacpi_disable_brightness_delay();
3522 /* MUST have enough space for all attributes to be added to
3523 * hotkey_dev_attributes */
3524 hotkey_dev_attributes = create_attr_set(
3525 ARRAY_SIZE(hotkey_attributes) + 2,
3527 if (!hotkey_dev_attributes)
3529 res = add_many_to_attr_set(hotkey_dev_attributes,
3531 ARRAY_SIZE(hotkey_attributes));
3535 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3536 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3537 for HKEY interface version 0x100 */
3538 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3539 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3540 "firmware HKEY interface version: 0x%x\n",
3543 switch (hkeyv >> 8) {
3546 * MHKV 0x100 in A31, R40, R40e,
3547 * T4x, X31, and later
3550 /* Paranoia check AND init hotkey_all_mask */
3551 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3553 pr_err("missing MHKA handler, please report this to %s\n",
3555 /* Fallback: pre-init for FN+F3,F4,F12 */
3556 hotkey_all_mask = 0x080cU;
3558 tp_features.hotkey_mask = 1;
3564 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3567 /* Paranoia check AND init hotkey_all_mask */
3568 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3570 pr_err("missing MHKA handler, please report this to %s\n",
3572 /* Fallback: pre-init for FN+F3,F4,F12 */
3573 hotkey_all_mask = 0x080cU;
3575 tp_features.hotkey_mask = 1;
3579 * Check if we have an adaptive keyboard, like on the
3580 * Lenovo Carbon X1 2014 (2nd Gen).
3582 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3584 if (hotkey_adaptive_all_mask != 0) {
3585 tp_features.has_adaptive_kbd = true;
3586 res = sysfs_create_group(
3587 &tpacpi_pdev->dev.kobj,
3588 &adaptive_kbd_attr_group);
3593 tp_features.has_adaptive_kbd = false;
3594 hotkey_adaptive_all_mask = 0x0U;
3599 pr_err("unknown version of the HKEY interface: 0x%x\n",
3601 pr_err("please report this to %s\n", TPACPI_MAIL);
3606 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3607 "hotkey masks are %s\n",
3608 str_supported(tp_features.hotkey_mask));
3610 /* Init hotkey_all_mask if not initialized yet */
3611 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3612 (quirks & TPACPI_HK_Q_INIMASK))
3613 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3615 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3616 if (tp_features.hotkey_mask) {
3617 /* hotkey_source_mask *must* be zero for
3618 * the first hotkey_mask_get to return hotkey_orig_mask */
3619 res = hotkey_mask_get();
3623 hotkey_orig_mask = hotkey_acpi_mask;
3625 hotkey_orig_mask = hotkey_all_mask;
3626 hotkey_acpi_mask = hotkey_all_mask;
3629 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3631 tp_features.hotkey_wlsw = 1;
3632 radiosw_state = !!tpacpi_wlsw_emulstate;
3633 pr_info("radio switch emulation enabled\n");
3636 /* Not all thinkpads have a hardware radio switch */
3637 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3638 tp_features.hotkey_wlsw = 1;
3639 radiosw_state = !!status;
3640 pr_info("radio switch found; radios are %s\n",
3641 enabled(status, 0));
3643 if (tp_features.hotkey_wlsw)
3644 res = add_to_attr_set(hotkey_dev_attributes,
3645 &dev_attr_hotkey_radio_sw.attr);
3647 res = hotkey_init_tablet_mode();
3651 tabletsw_state = res;
3653 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3654 &tpacpi_pdev->dev.kobj);
3658 /* Set up key map */
3659 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3661 if (!hotkey_keycode_map) {
3662 pr_err("failed to allocate memory for key map\n");
3667 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3668 ARRAY_SIZE(tpacpi_keymap_qtable));
3669 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3670 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3671 "using keymap number %lu\n", keymap_id);
3673 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3674 TPACPI_HOTKEY_MAP_SIZE);
3676 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3677 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3678 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3679 tpacpi_inputdev->keycode = hotkey_keycode_map;
3680 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3681 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3682 input_set_capability(tpacpi_inputdev, EV_KEY,
3683 hotkey_keycode_map[i]);
3685 if (i < sizeof(hotkey_reserved_mask)*8)
3686 hotkey_reserved_mask |= 1 << i;
3690 if (tp_features.hotkey_wlsw) {
3691 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3692 input_report_switch(tpacpi_inputdev,
3693 SW_RFKILL_ALL, radiosw_state);
3695 if (tp_features.hotkey_tablet) {
3696 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3697 input_report_switch(tpacpi_inputdev,
3698 SW_TABLET_MODE, tabletsw_state);
3701 /* Do not issue duplicate brightness change events to
3702 * userspace. tpacpi_detect_brightness_capabilities() must have
3703 * been called before this point */
3704 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3705 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3706 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3708 /* Disable brightness up/down on Lenovo thinkpads when
3709 * ACPI is handling them, otherwise it is plain impossible
3710 * for userspace to do something even remotely sane */
3711 hotkey_reserved_mask |=
3712 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3713 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3714 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3715 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3718 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3719 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3721 & ~hotkey_reserved_mask;
3723 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3724 "hotkey source mask 0x%08x, polling freq %u\n",
3725 hotkey_source_mask, hotkey_poll_freq);
3728 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3729 "enabling firmware HKEY event interface...\n");
3730 res = hotkey_status_set(true);
3735 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3736 | hotkey_driver_mask)
3737 & ~hotkey_source_mask);
3738 if (res < 0 && res != -ENXIO) {
3742 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3743 & ~hotkey_reserved_mask;
3744 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3745 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3746 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3748 tpacpi_inputdev->open = &hotkey_inputdev_open;
3749 tpacpi_inputdev->close = &hotkey_inputdev_close;
3751 hotkey_poll_setup_safe(true);
3756 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3757 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3758 &adaptive_kbd_attr_group);
3760 hotkey_dev_attributes = NULL;
3762 return (res < 0) ? res : 1;
3765 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3766 * mode, Web conference mode, Function mode and Lay-flat mode.
3767 * We support Home mode and Function mode currently.
3769 * Will consider support rest of modes in future.
3772 static const int adaptive_keyboard_modes[] = {
3774 /* WEB_BROWSER_MODE = 2,
3775 WEB_CONFERENCE_MODE = 3, */
3779 #define DFR_CHANGE_ROW 0x101
3780 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3781 #define FIRST_ADAPTIVE_KEY 0x103
3783 /* press Fn key a while second, it will switch to Function Mode. Then
3784 * release Fn key, previous mode be restored.
3786 static bool adaptive_keyboard_mode_is_saved;
3787 static int adaptive_keyboard_prev_mode;
3789 static int adaptive_keyboard_get_mode(void)
3793 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3794 pr_err("Cannot read adaptive keyboard mode\n");
3801 static int adaptive_keyboard_set_mode(int new_mode)
3804 new_mode > LAYFLAT_MODE)
3807 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3808 pr_err("Cannot set adaptive keyboard mode\n");
3815 static int adaptive_keyboard_get_next_mode(int mode)
3818 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3820 for (i = 0; i <= max_mode; i++) {
3821 if (adaptive_keyboard_modes[i] == mode)
3830 return adaptive_keyboard_modes[i];
3833 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3835 int current_mode = 0;
3840 case DFR_CHANGE_ROW:
3841 if (adaptive_keyboard_mode_is_saved) {
3842 new_mode = adaptive_keyboard_prev_mode;
3843 adaptive_keyboard_mode_is_saved = false;
3845 current_mode = adaptive_keyboard_get_mode();
3846 if (current_mode < 0)
3848 new_mode = adaptive_keyboard_get_next_mode(
3852 if (adaptive_keyboard_set_mode(new_mode) < 0)
3857 case DFR_SHOW_QUICKVIEW_ROW:
3858 current_mode = adaptive_keyboard_get_mode();
3859 if (current_mode < 0)
3862 adaptive_keyboard_prev_mode = current_mode;
3863 adaptive_keyboard_mode_is_saved = true;
3865 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3870 if (scancode < FIRST_ADAPTIVE_KEY ||
3871 scancode >= FIRST_ADAPTIVE_KEY +
3872 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3873 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3874 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3878 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3879 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3880 if (keycode != KEY_RESERVED) {
3881 mutex_lock(&tpacpi_inputdev_send_mutex);
3883 input_report_key(tpacpi_inputdev, keycode, 1);
3884 input_sync(tpacpi_inputdev);
3886 input_report_key(tpacpi_inputdev, keycode, 0);
3887 input_sync(tpacpi_inputdev);
3889 mutex_unlock(&tpacpi_inputdev_send_mutex);
3895 static bool hotkey_notify_hotkey(const u32 hkey,
3897 bool *ignore_acpi_ev)
3899 /* 0x1000-0x1FFF: key presses */
3900 unsigned int scancode = hkey & 0xfff;
3901 *send_acpi_ev = true;
3902 *ignore_acpi_ev = false;
3905 * Original events are in the 0x10XX range, the adaptive keyboard
3906 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3907 * models, additional keys are emitted through 0x13XX.
3909 switch ((hkey >> 8) & 0xf) {
3912 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3913 /* HKEY event 0x1001 is scancode 0x00 */
3915 if (!(hotkey_source_mask & (1 << scancode))) {
3916 tpacpi_input_send_key_masked(scancode);
3917 *send_acpi_ev = false;
3919 *ignore_acpi_ev = true;
3926 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3929 /* Extended keycodes start at 0x300 and our offset into the map
3930 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3931 * will be positive, but might not be in the correct range.
3933 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3934 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3935 scancode < TPACPI_HOTKEY_MAP_LEN) {
3936 tpacpi_input_send_key(scancode);
3945 static bool hotkey_notify_wakeup(const u32 hkey,
3947 bool *ignore_acpi_ev)
3949 /* 0x2000-0x2FFF: Wakeup reason */
3950 *send_acpi_ev = true;
3951 *ignore_acpi_ev = false;
3954 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3955 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3956 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3957 *ignore_acpi_ev = true;
3960 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3961 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3962 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3963 *ignore_acpi_ev = true;
3966 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3967 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3968 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3969 /* how to auto-heal: */
3970 /* 2313: woke up from S3, go to S4/S5 */
3971 /* 2413: woke up from S4, go to S5 */
3978 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3979 pr_info("woke up due to a hot-unplug request...\n");
3980 hotkey_wakeup_reason_notify_change();
3985 static bool hotkey_notify_dockevent(const u32 hkey,
3987 bool *ignore_acpi_ev)
3989 /* 0x4000-0x4FFF: dock-related events */
3990 *send_acpi_ev = true;
3991 *ignore_acpi_ev = false;
3994 case TP_HKEY_EV_UNDOCK_ACK:
3995 /* ACPI undock operation completed after wakeup */
3996 hotkey_autosleep_ack = 1;
3997 pr_info("undocked\n");
3998 hotkey_wakeup_hotunplug_complete_notify_change();
4001 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
4002 pr_info("docked into hotplug port replicator\n");
4004 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
4005 pr_info("undocked from hotplug port replicator\n");
4013 static bool hotkey_notify_usrevent(const u32 hkey,
4015 bool *ignore_acpi_ev)
4017 /* 0x5000-0x5FFF: human interface helpers */
4018 *send_acpi_ev = true;
4019 *ignore_acpi_ev = false;
4022 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4023 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
4026 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4027 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4028 tpacpi_input_send_tabletsw();
4029 hotkey_tablet_mode_notify_change();
4030 *send_acpi_ev = false;
4033 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4034 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4035 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
4036 /* do not propagate these events */
4037 *ignore_acpi_ev = true;
4045 static void thermal_dump_all_sensors(void);
4047 static bool hotkey_notify_6xxx(const u32 hkey,
4049 bool *ignore_acpi_ev)
4051 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4052 *send_acpi_ev = true;
4053 *ignore_acpi_ev = false;
4056 case TP_HKEY_EV_THM_TABLE_CHANGED:
4057 pr_debug("EC reports: Thermal Table has changed\n");
4058 /* recommended action: do nothing, we don't have
4059 * Lenovo ATM information */
4061 case TP_HKEY_EV_THM_CSM_COMPLETED:
4062 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4063 /* recommended action: do nothing, we don't have
4064 * Lenovo ATM information */
4066 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4067 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
4068 /* recommended action: do nothing, we don't have
4069 * Lenovo ATM information */
4071 case TP_HKEY_EV_ALARM_BAT_HOT:
4072 pr_crit("THERMAL ALARM: battery is too hot!\n");
4073 /* recommended action: warn user through gui */
4075 case TP_HKEY_EV_ALARM_BAT_XHOT:
4076 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4077 /* recommended action: immediate sleep/hibernate */
4079 case TP_HKEY_EV_ALARM_SENSOR_HOT:
4080 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4081 /* recommended action: warn user through gui, that */
4082 /* some internal component is too hot */
4084 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
4085 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4086 /* recommended action: immediate sleep/hibernate */
4088 case TP_HKEY_EV_AC_CHANGED:
4089 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4090 * AC status changed; can be triggered by plugging or
4091 * unplugging AC adapter, docking or undocking. */
4095 case TP_HKEY_EV_KEY_NUMLOCK:
4096 case TP_HKEY_EV_KEY_FN:
4097 case TP_HKEY_EV_KEY_FN_ESC:
4098 /* key press events, we just ignore them as long as the EC
4099 * is still reporting them in the normal keyboard stream */
4100 *send_acpi_ev = false;
4101 *ignore_acpi_ev = true;
4104 case TP_HKEY_EV_TABLET_CHANGED:
4105 tpacpi_input_send_tabletsw();
4106 hotkey_tablet_mode_notify_change();
4107 *send_acpi_ev = false;
4110 case TP_HKEY_EV_PALM_DETECTED:
4111 case TP_HKEY_EV_PALM_UNDETECTED:
4112 /* palm detected hovering the keyboard, forward to user-space
4113 * via netlink for consumption */
4117 /* report simply as unknown, no sensor dump */
4121 thermal_dump_all_sensors();
4125 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4129 bool ignore_acpi_ev;
4132 if (event != 0x80) {
4133 pr_err("unknown HKEY notification event %d\n", event);
4134 /* forward it to userspace, maybe it knows how to handle it */
4135 acpi_bus_generate_netlink_event(
4136 ibm->acpi->device->pnp.device_class,
4137 dev_name(&ibm->acpi->device->dev),
4143 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4144 pr_err("failed to retrieve HKEY event\n");
4153 send_acpi_ev = true;
4154 ignore_acpi_ev = false;
4156 switch (hkey >> 12) {
4158 /* 0x1000-0x1FFF: key presses */
4159 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4163 /* 0x2000-0x2FFF: Wakeup reason */
4164 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4168 /* 0x3000-0x3FFF: bay-related wakeups */
4170 case TP_HKEY_EV_BAYEJ_ACK:
4171 hotkey_autosleep_ack = 1;
4172 pr_info("bay ejected\n");
4173 hotkey_wakeup_hotunplug_complete_notify_change();
4176 case TP_HKEY_EV_OPTDRV_EJ:
4177 /* FIXME: kick libata if SATA link offline */
4185 /* 0x4000-0x4FFF: dock-related events */
4186 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4190 /* 0x5000-0x5FFF: human interface helpers */
4191 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4195 /* 0x6000-0x6FFF: thermal alarms/notices and
4196 * keyboard events */
4197 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4201 /* 0x7000-0x7FFF: misc */
4202 if (tp_features.hotkey_wlsw &&
4203 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4204 tpacpi_send_radiosw_update();
4209 /* fallthrough to default */
4214 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4215 pr_notice("please report the conditions when this event happened to %s\n",
4219 /* netlink events */
4220 if (!ignore_acpi_ev && send_acpi_ev) {
4221 acpi_bus_generate_netlink_event(
4222 ibm->acpi->device->pnp.device_class,
4223 dev_name(&ibm->acpi->device->dev),
4229 static void hotkey_suspend(void)
4231 /* Do these on suspend, we get the events on early resume! */
4232 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4233 hotkey_autosleep_ack = 0;
4235 /* save previous mode of adaptive keyboard of X1 Carbon */
4236 if (tp_features.has_adaptive_kbd) {
4237 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4239 pr_err("Cannot read adaptive keyboard mode.\n");
4244 static void hotkey_resume(void)
4246 tpacpi_disable_brightness_delay();
4248 if (hotkey_status_set(true) < 0 ||
4249 hotkey_mask_set(hotkey_acpi_mask) < 0)
4250 pr_err("error while attempting to reset the event firmware interface\n");
4252 tpacpi_send_radiosw_update();
4253 hotkey_tablet_mode_notify_change();
4254 hotkey_wakeup_reason_notify_change();
4255 hotkey_wakeup_hotunplug_complete_notify_change();
4256 hotkey_poll_setup_safe(false);
4258 /* restore previous mode of adapive keyboard of X1 Carbon */
4259 if (tp_features.has_adaptive_kbd) {
4260 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4261 adaptive_keyboard_prev_mode)) {
4262 pr_err("Cannot set adaptive keyboard mode.\n");
4267 /* procfs -------------------------------------------------------------- */
4268 static int hotkey_read(struct seq_file *m)
4272 if (!tp_features.hotkey) {
4273 seq_printf(m, "status:\t\tnot supported\n");
4277 if (mutex_lock_killable(&hotkey_mutex))
4278 return -ERESTARTSYS;
4279 res = hotkey_status_get(&status);
4281 res = hotkey_mask_get();
4282 mutex_unlock(&hotkey_mutex);
4286 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4287 if (hotkey_all_mask) {
4288 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4289 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4291 seq_printf(m, "mask:\t\tnot supported\n");
4292 seq_printf(m, "commands:\tenable, disable, reset\n");
4298 static void hotkey_enabledisable_warn(bool enable)
4300 tpacpi_log_usertask("procfs hotkey enable/disable");
4301 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4302 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4303 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4306 static int hotkey_write(char *buf)
4312 if (!tp_features.hotkey)
4315 if (mutex_lock_killable(&hotkey_mutex))
4316 return -ERESTARTSYS;
4318 mask = hotkey_user_mask;
4321 while ((cmd = next_cmd(&buf))) {
4322 if (strlencmp(cmd, "enable") == 0) {
4323 hotkey_enabledisable_warn(1);
4324 } else if (strlencmp(cmd, "disable") == 0) {
4325 hotkey_enabledisable_warn(0);
4327 } else if (strlencmp(cmd, "reset") == 0) {
4328 mask = (hotkey_all_mask | hotkey_source_mask)
4329 & ~hotkey_reserved_mask;
4330 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4332 } else if (sscanf(cmd, "%x", &mask) == 1) {
4341 tpacpi_disclose_usertask("procfs hotkey",
4342 "set mask to 0x%08x\n", mask);
4343 res = hotkey_user_mask_set(mask);
4347 mutex_unlock(&hotkey_mutex);
4351 static const struct acpi_device_id ibm_htk_device_ids[] = {
4352 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4353 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4354 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4358 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4359 .hid = ibm_htk_device_ids,
4360 .notify = hotkey_notify,
4361 .handle = &hkey_handle,
4362 .type = ACPI_DEVICE_NOTIFY,
4365 static struct ibm_struct hotkey_driver_data = {
4367 .read = hotkey_read,
4368 .write = hotkey_write,
4369 .exit = hotkey_exit,
4370 .resume = hotkey_resume,
4371 .suspend = hotkey_suspend,
4372 .acpi = &ibm_hotkey_acpidriver,
4375 /*************************************************************************
4376 * Bluetooth subdriver
4380 /* ACPI GBDC/SBDC bits */
4381 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4382 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4383 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4384 0 = disable, 1 = enable */
4388 /* ACPI \BLTH commands */
4389 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4390 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4391 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4392 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4393 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4396 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4398 static int bluetooth_get_status(void)
4402 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4403 if (dbg_bluetoothemul)
4404 return (tpacpi_bluetooth_emulstate) ?
4405 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4408 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4411 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4412 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4415 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4419 vdbg_printk(TPACPI_DBG_RFKILL,
4420 "will attempt to %s bluetooth\n",
4421 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4423 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4424 if (dbg_bluetoothemul) {
4425 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4430 if (state == TPACPI_RFK_RADIO_ON)
4431 status = TP_ACPI_BLUETOOTH_RADIOSSW
4432 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4436 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4442 /* sysfs bluetooth enable ---------------------------------------------- */
4443 static ssize_t bluetooth_enable_show(struct device *dev,
4444 struct device_attribute *attr,
4447 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4451 static ssize_t bluetooth_enable_store(struct device *dev,
4452 struct device_attribute *attr,
4453 const char *buf, size_t count)
4455 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4459 static DEVICE_ATTR_RW(bluetooth_enable);
4461 /* --------------------------------------------------------------------- */
4463 static struct attribute *bluetooth_attributes[] = {
4464 &dev_attr_bluetooth_enable.attr,
4468 static const struct attribute_group bluetooth_attr_group = {
4469 .attrs = bluetooth_attributes,
4472 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4473 .get_status = bluetooth_get_status,
4474 .set_status = bluetooth_set_status,
4477 static void bluetooth_shutdown(void)
4479 /* Order firmware to save current state to NVRAM */
4480 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4481 TP_ACPI_BLTH_SAVE_STATE))
4482 pr_notice("failed to save bluetooth state to NVRAM\n");
4484 vdbg_printk(TPACPI_DBG_RFKILL,
4485 "bluetooth state saved to NVRAM\n");
4488 static void bluetooth_exit(void)
4490 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4491 &bluetooth_attr_group);
4493 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4495 bluetooth_shutdown();
4498 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4503 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4504 "initializing bluetooth subdriver\n");
4506 TPACPI_ACPIHANDLE_INIT(hkey);
4508 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4509 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4510 tp_features.bluetooth = hkey_handle &&
4511 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4513 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4514 "bluetooth is %s, status 0x%02x\n",
4515 str_supported(tp_features.bluetooth),
4518 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4519 if (dbg_bluetoothemul) {
4520 tp_features.bluetooth = 1;
4521 pr_info("bluetooth switch emulation enabled\n");
4524 if (tp_features.bluetooth &&
4525 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4526 /* no bluetooth hardware present in system */
4527 tp_features.bluetooth = 0;
4528 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4529 "bluetooth hardware not installed\n");
4532 if (!tp_features.bluetooth)
4535 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4536 &bluetooth_tprfk_ops,
4537 RFKILL_TYPE_BLUETOOTH,
4538 TPACPI_RFK_BLUETOOTH_SW_NAME,
4543 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4544 &bluetooth_attr_group);
4546 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4553 /* procfs -------------------------------------------------------------- */
4554 static int bluetooth_read(struct seq_file *m)
4556 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4559 static int bluetooth_write(char *buf)
4561 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4564 static struct ibm_struct bluetooth_driver_data = {
4565 .name = "bluetooth",
4566 .read = bluetooth_read,
4567 .write = bluetooth_write,
4568 .exit = bluetooth_exit,
4569 .shutdown = bluetooth_shutdown,
4572 /*************************************************************************
4577 /* ACPI GWAN/SWAN bits */
4578 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4579 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4580 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4581 0 = disable, 1 = enable */
4584 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4586 static int wan_get_status(void)
4590 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4592 return (tpacpi_wwan_emulstate) ?
4593 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4596 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4599 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4600 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4603 static int wan_set_status(enum tpacpi_rfkill_state state)
4607 vdbg_printk(TPACPI_DBG_RFKILL,
4608 "will attempt to %s wwan\n",
4609 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4611 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4613 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4618 if (state == TPACPI_RFK_RADIO_ON)
4619 status = TP_ACPI_WANCARD_RADIOSSW
4620 | TP_ACPI_WANCARD_RESUMECTRL;
4624 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4630 /* sysfs wan enable ---------------------------------------------------- */
4631 static ssize_t wan_enable_show(struct device *dev,
4632 struct device_attribute *attr,
4635 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4639 static ssize_t wan_enable_store(struct device *dev,
4640 struct device_attribute *attr,
4641 const char *buf, size_t count)
4643 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4647 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4648 wan_enable_show, wan_enable_store);
4650 /* --------------------------------------------------------------------- */
4652 static struct attribute *wan_attributes[] = {
4653 &dev_attr_wwan_enable.attr,
4657 static const struct attribute_group wan_attr_group = {
4658 .attrs = wan_attributes,
4661 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4662 .get_status = wan_get_status,
4663 .set_status = wan_set_status,
4666 static void wan_shutdown(void)
4668 /* Order firmware to save current state to NVRAM */
4669 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4670 TP_ACPI_WGSV_SAVE_STATE))
4671 pr_notice("failed to save WWAN state to NVRAM\n");
4673 vdbg_printk(TPACPI_DBG_RFKILL,
4674 "WWAN state saved to NVRAM\n");
4677 static void wan_exit(void)
4679 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4682 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4687 static int __init wan_init(struct ibm_init_struct *iibm)
4692 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4693 "initializing wan subdriver\n");
4695 TPACPI_ACPIHANDLE_INIT(hkey);
4697 tp_features.wan = hkey_handle &&
4698 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4700 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4701 "wan is %s, status 0x%02x\n",
4702 str_supported(tp_features.wan),
4705 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4707 tp_features.wan = 1;
4708 pr_info("wwan switch emulation enabled\n");
4711 if (tp_features.wan &&
4712 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4713 /* no wan hardware present in system */
4714 tp_features.wan = 0;
4715 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4716 "wan hardware not installed\n");
4719 if (!tp_features.wan)
4722 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4725 TPACPI_RFK_WWAN_SW_NAME,
4730 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4734 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4741 /* procfs -------------------------------------------------------------- */
4742 static int wan_read(struct seq_file *m)
4744 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4747 static int wan_write(char *buf)
4749 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4752 static struct ibm_struct wan_driver_data = {
4757 .shutdown = wan_shutdown,
4760 /*************************************************************************
4765 /* ACPI GUWB/SUWB bits */
4766 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4767 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4770 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4772 static int uwb_get_status(void)
4776 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4778 return (tpacpi_uwb_emulstate) ?
4779 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4782 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4785 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4786 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4789 static int uwb_set_status(enum tpacpi_rfkill_state state)
4793 vdbg_printk(TPACPI_DBG_RFKILL,
4794 "will attempt to %s UWB\n",
4795 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4797 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4799 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4804 if (state == TPACPI_RFK_RADIO_ON)
4805 status = TP_ACPI_UWB_RADIOSSW;
4809 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4815 /* --------------------------------------------------------------------- */
4817 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4818 .get_status = uwb_get_status,
4819 .set_status = uwb_set_status,
4822 static void uwb_exit(void)
4824 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4827 static int __init uwb_init(struct ibm_init_struct *iibm)
4832 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4833 "initializing uwb subdriver\n");
4835 TPACPI_ACPIHANDLE_INIT(hkey);
4837 tp_features.uwb = hkey_handle &&
4838 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4840 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4841 "uwb is %s, status 0x%02x\n",
4842 str_supported(tp_features.uwb),
4845 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4847 tp_features.uwb = 1;
4848 pr_info("uwb switch emulation enabled\n");
4851 if (tp_features.uwb &&
4852 !(status & TP_ACPI_UWB_HWPRESENT)) {
4853 /* no uwb hardware present in system */
4854 tp_features.uwb = 0;
4855 dbg_printk(TPACPI_DBG_INIT,
4856 "uwb hardware not installed\n");
4859 if (!tp_features.uwb)
4862 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4865 TPACPI_RFK_UWB_SW_NAME,
4870 static struct ibm_struct uwb_driver_data = {
4873 .flags.experimental = 1,
4876 /*************************************************************************
4880 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4882 enum video_access_mode {
4883 TPACPI_VIDEO_NONE = 0,
4884 TPACPI_VIDEO_570, /* 570 */
4885 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4886 TPACPI_VIDEO_NEW, /* all others */
4889 enum { /* video status flags, based on VIDEO_570 */
4890 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4891 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4892 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4895 enum { /* TPACPI_VIDEO_570 constants */
4896 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4897 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4898 * video_status_flags */
4899 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4900 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4903 static enum video_access_mode video_supported;
4904 static int video_orig_autosw;
4906 static int video_autosw_get(void);
4907 static int video_autosw_set(int enable);
4909 TPACPI_HANDLE(vid, root,
4910 "\\_SB.PCI.AGP.VGA", /* 570 */
4911 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4912 "\\_SB.PCI0.VID0", /* 770e */
4913 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4914 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4915 "\\_SB.PCI0.AGP.VID", /* all others */
4918 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4920 static int __init video_init(struct ibm_init_struct *iibm)
4924 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4926 TPACPI_ACPIHANDLE_INIT(vid);
4927 if (tpacpi_is_ibm())
4928 TPACPI_ACPIHANDLE_INIT(vid2);
4930 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4931 /* G41, assume IVGA doesn't change */
4932 vid_handle = vid2_handle;
4935 /* video switching not supported on R30, R31 */
4936 video_supported = TPACPI_VIDEO_NONE;
4937 else if (tpacpi_is_ibm() &&
4938 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4940 video_supported = TPACPI_VIDEO_570;
4941 else if (tpacpi_is_ibm() &&
4942 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4943 /* 600e/x, 770e, 770x */
4944 video_supported = TPACPI_VIDEO_770;
4947 video_supported = TPACPI_VIDEO_NEW;
4949 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4950 str_supported(video_supported != TPACPI_VIDEO_NONE),
4953 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4956 static void video_exit(void)
4958 dbg_printk(TPACPI_DBG_EXIT,
4959 "restoring original video autoswitch mode\n");
4960 if (video_autosw_set(video_orig_autosw))
4961 pr_err("error while trying to restore original video autoswitch mode\n");
4964 static int video_outputsw_get(void)
4969 switch (video_supported) {
4970 case TPACPI_VIDEO_570:
4971 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4972 TP_ACPI_VIDEO_570_PHSCMD))
4974 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4976 case TPACPI_VIDEO_770:
4977 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4980 status |= TP_ACPI_VIDEO_S_LCD;
4981 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4984 status |= TP_ACPI_VIDEO_S_CRT;
4986 case TPACPI_VIDEO_NEW:
4987 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4988 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4991 status |= TP_ACPI_VIDEO_S_CRT;
4993 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4994 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4997 status |= TP_ACPI_VIDEO_S_LCD;
4998 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5001 status |= TP_ACPI_VIDEO_S_DVI;
5010 static int video_outputsw_set(int status)
5015 switch (video_supported) {
5016 case TPACPI_VIDEO_570:
5017 res = acpi_evalf(NULL, NULL,
5018 "\\_SB.PHS2", "vdd",
5019 TP_ACPI_VIDEO_570_PHS2CMD,
5020 status | TP_ACPI_VIDEO_570_PHS2SET);
5022 case TPACPI_VIDEO_770:
5023 autosw = video_autosw_get();
5027 res = video_autosw_set(1);
5030 res = acpi_evalf(vid_handle, NULL,
5031 "ASWT", "vdd", status * 0x100, 0);
5032 if (!autosw && video_autosw_set(autosw)) {
5033 pr_err("video auto-switch left enabled due to error\n");
5037 case TPACPI_VIDEO_NEW:
5038 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5039 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5045 return (res) ? 0 : -EIO;
5048 static int video_autosw_get(void)
5052 switch (video_supported) {
5053 case TPACPI_VIDEO_570:
5054 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5057 case TPACPI_VIDEO_770:
5058 case TPACPI_VIDEO_NEW:
5059 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5069 static int video_autosw_set(int enable)
5071 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5076 static int video_outputsw_cycle(void)
5078 int autosw = video_autosw_get();
5084 switch (video_supported) {
5085 case TPACPI_VIDEO_570:
5086 res = video_autosw_set(1);
5089 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5091 case TPACPI_VIDEO_770:
5092 case TPACPI_VIDEO_NEW:
5093 res = video_autosw_set(1);
5096 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5101 if (!autosw && video_autosw_set(autosw)) {
5102 pr_err("video auto-switch left enabled due to error\n");
5106 return (res) ? 0 : -EIO;
5109 static int video_expand_toggle(void)
5111 switch (video_supported) {
5112 case TPACPI_VIDEO_570:
5113 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5115 case TPACPI_VIDEO_770:
5116 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5118 case TPACPI_VIDEO_NEW:
5119 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5127 static int video_read(struct seq_file *m)
5131 if (video_supported == TPACPI_VIDEO_NONE) {
5132 seq_printf(m, "status:\t\tnot supported\n");
5136 /* Even reads can crash X.org, so... */
5137 if (!capable(CAP_SYS_ADMIN))
5140 status = video_outputsw_get();
5144 autosw = video_autosw_get();
5148 seq_printf(m, "status:\t\tsupported\n");
5149 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5150 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5151 if (video_supported == TPACPI_VIDEO_NEW)
5152 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5153 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5154 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5155 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5156 if (video_supported == TPACPI_VIDEO_NEW)
5157 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5158 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5159 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5164 static int video_write(char *buf)
5167 int enable, disable, status;
5170 if (video_supported == TPACPI_VIDEO_NONE)
5173 /* Even reads can crash X.org, let alone writes... */
5174 if (!capable(CAP_SYS_ADMIN))
5180 while ((cmd = next_cmd(&buf))) {
5181 if (strlencmp(cmd, "lcd_enable") == 0) {
5182 enable |= TP_ACPI_VIDEO_S_LCD;
5183 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5184 disable |= TP_ACPI_VIDEO_S_LCD;
5185 } else if (strlencmp(cmd, "crt_enable") == 0) {
5186 enable |= TP_ACPI_VIDEO_S_CRT;
5187 } else if (strlencmp(cmd, "crt_disable") == 0) {
5188 disable |= TP_ACPI_VIDEO_S_CRT;
5189 } else if (video_supported == TPACPI_VIDEO_NEW &&
5190 strlencmp(cmd, "dvi_enable") == 0) {
5191 enable |= TP_ACPI_VIDEO_S_DVI;
5192 } else if (video_supported == TPACPI_VIDEO_NEW &&
5193 strlencmp(cmd, "dvi_disable") == 0) {
5194 disable |= TP_ACPI_VIDEO_S_DVI;
5195 } else if (strlencmp(cmd, "auto_enable") == 0) {
5196 res = video_autosw_set(1);
5199 } else if (strlencmp(cmd, "auto_disable") == 0) {
5200 res = video_autosw_set(0);
5203 } else if (strlencmp(cmd, "video_switch") == 0) {
5204 res = video_outputsw_cycle();
5207 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5208 res = video_expand_toggle();
5215 if (enable || disable) {
5216 status = video_outputsw_get();
5219 res = video_outputsw_set((status & ~disable) | enable);
5227 static struct ibm_struct video_driver_data = {
5230 .write = video_write,
5234 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5236 /*************************************************************************
5237 * Keyboard backlight subdriver
5240 static enum led_brightness kbdlight_brightness;
5241 static DEFINE_MUTEX(kbdlight_mutex);
5243 static int kbdlight_set_level(int level)
5250 mutex_lock(&kbdlight_mutex);
5252 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5255 kbdlight_brightness = level;
5257 mutex_unlock(&kbdlight_mutex);
5262 static int kbdlight_get_level(void)
5269 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5275 return status & 0x3;
5278 static bool kbdlight_is_supported(void)
5285 if (!acpi_has_method(hkey_handle, "MLCG")) {
5286 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5290 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5291 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5296 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5300 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5302 * Guessed test for keyboard backlight:
5304 * Machines with backlight keyboard return:
5305 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5306 * b110100010010000000XX - ThinkPad x230
5307 * b010100000010000000XX - ThinkPad x240
5308 * b010100000010000000XX - ThinkPad W541
5309 * (XX is current backlight level)
5311 * Machines without backlight keyboard return:
5312 * b10100001000000000000 - ThinkPad x230
5313 * b10110001000000000000 - ThinkPad E430
5314 * b00000000000000000000 - ThinkPad E450
5316 * Candidate BITs for detection test (XOR):
5317 * b01000000001000000000
5320 return status & BIT(9);
5323 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5324 enum led_brightness brightness)
5326 return kbdlight_set_level(brightness);
5329 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5333 level = kbdlight_get_level();
5340 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5342 .name = "tpacpi::kbd_backlight",
5343 .max_brightness = 2,
5344 .flags = LED_BRIGHT_HW_CHANGED,
5345 .brightness_set_blocking = &kbdlight_sysfs_set,
5346 .brightness_get = &kbdlight_sysfs_get,
5350 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5354 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5356 TPACPI_ACPIHANDLE_INIT(hkey);
5358 if (!kbdlight_is_supported()) {
5359 tp_features.kbdlight = 0;
5360 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5364 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5365 tp_features.kbdlight = 1;
5367 rc = led_classdev_register(&tpacpi_pdev->dev,
5368 &tpacpi_led_kbdlight.led_classdev);
5370 tp_features.kbdlight = 0;
5374 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5375 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5379 static void kbdlight_exit(void)
5381 if (tp_features.kbdlight)
5382 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5385 static int kbdlight_set_level_and_update(int level)
5388 struct led_classdev *led_cdev;
5390 ret = kbdlight_set_level(level);
5391 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5393 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5394 led_cdev->brightness = level;
5399 static int kbdlight_read(struct seq_file *m)
5403 if (!tp_features.kbdlight) {
5404 seq_printf(m, "status:\t\tnot supported\n");
5406 level = kbdlight_get_level();
5408 seq_printf(m, "status:\t\terror %d\n", level);
5410 seq_printf(m, "status:\t\t%d\n", level);
5411 seq_printf(m, "commands:\t0, 1, 2\n");
5417 static int kbdlight_write(char *buf)
5422 if (!tp_features.kbdlight)
5425 while ((cmd = next_cmd(&buf))) {
5426 if (strlencmp(cmd, "0") == 0)
5428 else if (strlencmp(cmd, "1") == 0)
5430 else if (strlencmp(cmd, "2") == 0)
5439 return kbdlight_set_level_and_update(level);
5442 static void kbdlight_suspend(void)
5444 struct led_classdev *led_cdev;
5446 if (!tp_features.kbdlight)
5449 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5450 led_update_brightness(led_cdev);
5451 led_classdev_suspend(led_cdev);
5454 static void kbdlight_resume(void)
5456 if (!tp_features.kbdlight)
5459 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5462 static struct ibm_struct kbdlight_driver_data = {
5464 .read = kbdlight_read,
5465 .write = kbdlight_write,
5466 .suspend = kbdlight_suspend,
5467 .resume = kbdlight_resume,
5468 .exit = kbdlight_exit,
5471 /*************************************************************************
5472 * Light (thinklight) subdriver
5475 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5476 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5478 static int light_get_status(void)
5482 if (tp_features.light_status) {
5483 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5491 static int light_set_status(int status)
5495 if (tp_features.light) {
5497 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5499 TP_CMOS_THINKLIGHT_ON :
5500 TP_CMOS_THINKLIGHT_OFF);
5502 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5505 return (rc) ? 0 : -EIO;
5511 static int light_sysfs_set(struct led_classdev *led_cdev,
5512 enum led_brightness brightness)
5514 return light_set_status((brightness != LED_OFF) ?
5515 TPACPI_LED_ON : TPACPI_LED_OFF);
5518 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5520 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5523 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5525 .name = "tpacpi::thinklight",
5526 .brightness_set_blocking = &light_sysfs_set,
5527 .brightness_get = &light_sysfs_get,
5531 static int __init light_init(struct ibm_init_struct *iibm)
5535 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5537 if (tpacpi_is_ibm()) {
5538 TPACPI_ACPIHANDLE_INIT(ledb);
5539 TPACPI_ACPIHANDLE_INIT(lght);
5541 TPACPI_ACPIHANDLE_INIT(cmos);
5543 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5544 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5546 if (tp_features.light)
5547 /* light status not supported on
5548 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5549 tp_features.light_status =
5550 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5552 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5553 str_supported(tp_features.light),
5554 str_supported(tp_features.light_status));
5556 if (!tp_features.light)
5559 rc = led_classdev_register(&tpacpi_pdev->dev,
5560 &tpacpi_led_thinklight.led_classdev);
5563 tp_features.light = 0;
5564 tp_features.light_status = 0;
5572 static void light_exit(void)
5574 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5577 static int light_read(struct seq_file *m)
5581 if (!tp_features.light) {
5582 seq_printf(m, "status:\t\tnot supported\n");
5583 } else if (!tp_features.light_status) {
5584 seq_printf(m, "status:\t\tunknown\n");
5585 seq_printf(m, "commands:\ton, off\n");
5587 status = light_get_status();
5590 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5591 seq_printf(m, "commands:\ton, off\n");
5597 static int light_write(char *buf)
5602 if (!tp_features.light)
5605 while ((cmd = next_cmd(&buf))) {
5606 if (strlencmp(cmd, "on") == 0) {
5608 } else if (strlencmp(cmd, "off") == 0) {
5614 return light_set_status(newstatus);
5617 static struct ibm_struct light_driver_data = {
5620 .write = light_write,
5624 /*************************************************************************
5628 /* sysfs cmos_command -------------------------------------------------- */
5629 static ssize_t cmos_command_store(struct device *dev,
5630 struct device_attribute *attr,
5631 const char *buf, size_t count)
5633 unsigned long cmos_cmd;
5636 if (parse_strtoul(buf, 21, &cmos_cmd))
5639 res = issue_thinkpad_cmos_command(cmos_cmd);
5640 return (res) ? res : count;
5643 static DEVICE_ATTR_WO(cmos_command);
5645 /* --------------------------------------------------------------------- */
5647 static int __init cmos_init(struct ibm_init_struct *iibm)
5651 vdbg_printk(TPACPI_DBG_INIT,
5652 "initializing cmos commands subdriver\n");
5654 TPACPI_ACPIHANDLE_INIT(cmos);
5656 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5657 str_supported(cmos_handle != NULL));
5659 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5663 return (cmos_handle) ? 0 : 1;
5666 static void cmos_exit(void)
5668 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5671 static int cmos_read(struct seq_file *m)
5673 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5674 R30, R31, T20-22, X20-21 */
5676 seq_printf(m, "status:\t\tnot supported\n");
5678 seq_printf(m, "status:\t\tsupported\n");
5679 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5685 static int cmos_write(char *buf)
5690 while ((cmd = next_cmd(&buf))) {
5691 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5692 cmos_cmd >= 0 && cmos_cmd <= 21) {
5697 res = issue_thinkpad_cmos_command(cmos_cmd);
5705 static struct ibm_struct cmos_driver_data = {
5708 .write = cmos_write,
5712 /*************************************************************************
5716 enum led_access_mode {
5717 TPACPI_LED_NONE = 0,
5718 TPACPI_LED_570, /* 570 */
5719 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5720 TPACPI_LED_NEW, /* all others */
5723 enum { /* For TPACPI_LED_OLD */
5724 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5725 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5726 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5729 static enum led_access_mode led_supported;
5731 static acpi_handle led_handle;
5733 #define TPACPI_LED_NUMLEDS 16
5734 static struct tpacpi_led_classdev *tpacpi_leds;
5735 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5736 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5737 /* there's a limit of 19 chars + NULL before 2.6.26 */
5739 "tpacpi:orange:batt",
5740 "tpacpi:green:batt",
5741 "tpacpi::dock_active",
5742 "tpacpi::bay_active",
5743 "tpacpi::dock_batt",
5744 "tpacpi::unknown_led",
5746 "tpacpi::dock_status1",
5747 "tpacpi::dock_status2",
5748 "tpacpi::unknown_led2",
5749 "tpacpi::unknown_led3",
5750 "tpacpi::thinkvantage",
5752 #define TPACPI_SAFE_LEDS 0x1081U
5754 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5756 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5759 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5763 static int led_get_status(const unsigned int led)
5766 enum led_status_t led_s;
5768 switch (led_supported) {
5769 case TPACPI_LED_570:
5770 if (!acpi_evalf(ec_handle,
5771 &status, "GLED", "dd", 1 << led))
5773 led_s = (status == 0) ?
5778 tpacpi_led_state_cache[led] = led_s;
5787 static int led_set_status(const unsigned int led,
5788 const enum led_status_t ledstatus)
5790 /* off, on, blink. Index is led_status_t */
5791 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5792 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5796 switch (led_supported) {
5797 case TPACPI_LED_570:
5799 if (unlikely(led > 7))
5801 if (unlikely(tpacpi_is_led_restricted(led)))
5803 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5804 (1 << led), led_sled_arg1[ledstatus]))
5807 case TPACPI_LED_OLD:
5808 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5809 if (unlikely(led > 7))
5811 if (unlikely(tpacpi_is_led_restricted(led)))
5813 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5815 rc = ec_write(TPACPI_LED_EC_HLBL,
5816 (ledstatus == TPACPI_LED_BLINK) << led);
5818 rc = ec_write(TPACPI_LED_EC_HLCL,
5819 (ledstatus != TPACPI_LED_OFF) << led);
5821 case TPACPI_LED_NEW:
5823 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5825 if (unlikely(tpacpi_is_led_restricted(led)))
5827 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5828 led, led_led_arg1[ledstatus]))
5836 tpacpi_led_state_cache[led] = ledstatus;
5841 static int led_sysfs_set(struct led_classdev *led_cdev,
5842 enum led_brightness brightness)
5844 struct tpacpi_led_classdev *data = container_of(led_cdev,
5845 struct tpacpi_led_classdev, led_classdev);
5846 enum led_status_t new_state;
5848 if (brightness == LED_OFF)
5849 new_state = TPACPI_LED_OFF;
5850 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5851 new_state = TPACPI_LED_ON;
5853 new_state = TPACPI_LED_BLINK;
5855 return led_set_status(data->led, new_state);
5858 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5859 unsigned long *delay_on, unsigned long *delay_off)
5861 struct tpacpi_led_classdev *data = container_of(led_cdev,
5862 struct tpacpi_led_classdev, led_classdev);
5864 /* Can we choose the flash rate? */
5865 if (*delay_on == 0 && *delay_off == 0) {
5866 /* yes. set them to the hardware blink rate (1 Hz) */
5867 *delay_on = 500; /* ms */
5868 *delay_off = 500; /* ms */
5869 } else if ((*delay_on != 500) || (*delay_off != 500))
5872 return led_set_status(data->led, TPACPI_LED_BLINK);
5875 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5879 struct tpacpi_led_classdev *data = container_of(led_cdev,
5880 struct tpacpi_led_classdev, led_classdev);
5882 rc = led_get_status(data->led);
5884 if (rc == TPACPI_LED_OFF || rc < 0)
5885 rc = LED_OFF; /* no error handling in led class :( */
5892 static void led_exit(void)
5896 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5897 if (tpacpi_leds[i].led_classdev.name)
5898 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5904 static int __init tpacpi_init_led(unsigned int led)
5908 tpacpi_leds[led].led = led;
5910 /* LEDs with no name don't get registered */
5911 if (!tpacpi_led_names[led])
5914 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5915 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5916 if (led_supported == TPACPI_LED_570)
5917 tpacpi_leds[led].led_classdev.brightness_get =
5920 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5922 rc = led_classdev_register(&tpacpi_pdev->dev,
5923 &tpacpi_leds[led].led_classdev);
5925 tpacpi_leds[led].led_classdev.name = NULL;
5930 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5931 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5932 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5933 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5935 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5936 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5937 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5938 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5939 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5940 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5941 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5942 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5944 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5945 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5946 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5947 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5948 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5950 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5951 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5952 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5953 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5955 /* (1) - may have excess leds enabled on MSB */
5957 /* Defaults (order matters, keep last, don't reorder!) */
5959 .vendor = PCI_VENDOR_ID_LENOVO,
5960 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5963 { /* IBM ThinkPads with no EC version string */
5964 .vendor = PCI_VENDOR_ID_IBM,
5965 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5968 { /* IBM ThinkPads with EC version string */
5969 .vendor = PCI_VENDOR_ID_IBM,
5970 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5975 #undef TPACPI_LEDQ_IBM
5976 #undef TPACPI_LEDQ_LNV
5978 static enum led_access_mode __init led_init_detect_mode(void)
5982 if (tpacpi_is_ibm()) {
5984 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5985 if (ACPI_SUCCESS(status))
5986 return TPACPI_LED_570;
5988 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5989 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5990 if (ACPI_SUCCESS(status))
5991 return TPACPI_LED_OLD;
5995 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5996 if (ACPI_SUCCESS(status))
5997 return TPACPI_LED_NEW;
5999 /* R30, R31, and unknown firmwares */
6001 return TPACPI_LED_NONE;
6004 static int __init led_init(struct ibm_init_struct *iibm)
6008 unsigned long useful_leds;
6010 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6012 led_supported = led_init_detect_mode();
6014 if (led_supported != TPACPI_LED_NONE) {
6015 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6016 ARRAY_SIZE(led_useful_qtable));
6020 led_supported = TPACPI_LED_NONE;
6024 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6025 str_supported(led_supported), led_supported);
6027 if (led_supported == TPACPI_LED_NONE)
6030 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6033 pr_err("Out of memory for LED data\n");
6037 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6038 tpacpi_leds[i].led = -1;
6040 if (!tpacpi_is_led_restricted(i) &&
6041 test_bit(i, &useful_leds)) {
6042 rc = tpacpi_init_led(i);
6050 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6051 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6056 #define str_led_status(s) \
6057 ((s) == TPACPI_LED_OFF ? "off" : \
6058 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6060 static int led_read(struct seq_file *m)
6062 if (!led_supported) {
6063 seq_printf(m, "status:\t\tnot supported\n");
6066 seq_printf(m, "status:\t\tsupported\n");
6068 if (led_supported == TPACPI_LED_570) {
6071 for (i = 0; i < 8; i++) {
6072 status = led_get_status(i);
6075 seq_printf(m, "%d:\t\t%s\n",
6076 i, str_led_status(status));
6080 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6085 static int led_write(char *buf)
6089 enum led_status_t s;
6094 while ((cmd = next_cmd(&buf))) {
6095 if (sscanf(cmd, "%d", &led) != 1)
6098 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6099 tpacpi_leds[led].led < 0)
6102 if (strstr(cmd, "off")) {
6104 } else if (strstr(cmd, "on")) {
6106 } else if (strstr(cmd, "blink")) {
6107 s = TPACPI_LED_BLINK;
6112 rc = led_set_status(led, s);
6120 static struct ibm_struct led_driver_data = {
6127 /*************************************************************************
6131 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6133 #define TPACPI_BEEP_Q1 0x0001
6135 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6136 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6137 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6140 static int __init beep_init(struct ibm_init_struct *iibm)
6142 unsigned long quirks;
6144 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6146 TPACPI_ACPIHANDLE_INIT(beep);
6148 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6149 str_supported(beep_handle != NULL));
6151 quirks = tpacpi_check_quirks(beep_quirk_table,
6152 ARRAY_SIZE(beep_quirk_table));
6154 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6156 return (beep_handle) ? 0 : 1;
6159 static int beep_read(struct seq_file *m)
6162 seq_printf(m, "status:\t\tnot supported\n");
6164 seq_printf(m, "status:\t\tsupported\n");
6165 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6171 static int beep_write(char *buf)
6179 while ((cmd = next_cmd(&buf))) {
6180 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6181 beep_cmd >= 0 && beep_cmd <= 17) {
6185 if (tp_features.beep_needs_two_args) {
6186 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6190 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6199 static struct ibm_struct beep_driver_data = {
6202 .write = beep_write,
6205 /*************************************************************************
6209 enum thermal_access_mode {
6210 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6211 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6212 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6213 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6214 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6217 enum { /* TPACPI_THERMAL_TPEC_* */
6218 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6219 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6220 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6222 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6226 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6227 struct ibm_thermal_sensors_struct {
6228 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6231 static enum thermal_access_mode thermal_read_mode;
6233 /* idx is zero-based */
6234 static int thermal_get_sensor(int idx, s32 *value)
6240 t = TP_EC_THERMAL_TMP0;
6242 switch (thermal_read_mode) {
6243 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6244 case TPACPI_THERMAL_TPEC_16:
6245 if (idx >= 8 && idx <= 15) {
6246 t = TP_EC_THERMAL_TMP8;
6251 case TPACPI_THERMAL_TPEC_8:
6253 if (!acpi_ec_read(t + idx, &tmp))
6255 *value = tmp * 1000;
6260 case TPACPI_THERMAL_ACPI_UPDT:
6262 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6263 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6265 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6267 *value = (t - 2732) * 100;
6272 case TPACPI_THERMAL_ACPI_TMP07:
6274 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6275 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6277 if (t > 127 || t < -127)
6278 t = TP_EC_THERMAL_TMP_NA;
6284 case TPACPI_THERMAL_NONE:
6292 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6303 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6306 for (i = 0 ; i < n; i++) {
6307 res = thermal_get_sensor(i, &s->temp[i]);
6315 static void thermal_dump_all_sensors(void)
6318 struct ibm_thermal_sensors_struct t;
6320 n = thermal_get_sensors(&t);
6324 pr_notice("temperatures (Celsius):");
6326 for (i = 0; i < n; i++) {
6327 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6328 pr_cont(" %d", (int)(t.temp[i] / 1000));
6336 /* sysfs temp##_input -------------------------------------------------- */
6338 static ssize_t thermal_temp_input_show(struct device *dev,
6339 struct device_attribute *attr,
6342 struct sensor_device_attribute *sensor_attr =
6343 to_sensor_dev_attr(attr);
6344 int idx = sensor_attr->index;
6348 res = thermal_get_sensor(idx, &value);
6351 if (value == TPACPI_THERMAL_SENSOR_NA)
6354 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6357 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6358 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6359 thermal_temp_input_show, NULL, _idxB)
6361 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6362 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6363 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6364 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6365 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6366 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6367 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6368 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6369 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6370 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6371 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6372 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6373 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6374 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6375 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6376 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6377 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6380 #define THERMAL_ATTRS(X) \
6381 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6383 static struct attribute *thermal_temp_input_attr[] = {
6403 static const struct attribute_group thermal_temp_input16_group = {
6404 .attrs = thermal_temp_input_attr
6407 static const struct attribute_group thermal_temp_input8_group = {
6408 .attrs = &thermal_temp_input_attr[8]
6411 #undef THERMAL_SENSOR_ATTR_TEMP
6412 #undef THERMAL_ATTRS
6414 /* --------------------------------------------------------------------- */
6416 static int __init thermal_init(struct ibm_init_struct *iibm)
6423 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6425 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6427 if (thinkpad_id.ec_model) {
6429 * Direct EC access mode: sensors at registers
6430 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6431 * non-implemented, thermal sensors return 0x80 when
6436 for (i = 0; i < 8; i++) {
6437 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6443 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6451 /* This is sheer paranoia, but we handle it anyway */
6453 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6454 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6456 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6457 thermal_read_mode = TPACPI_THERMAL_NONE;
6462 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6464 } else if (acpi_tmp7) {
6465 if (tpacpi_is_ibm() &&
6466 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6467 /* 600e/x, 770e, 770x */
6468 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6470 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6471 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6474 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6475 thermal_read_mode = TPACPI_THERMAL_NONE;
6478 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6479 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6482 switch (thermal_read_mode) {
6483 case TPACPI_THERMAL_TPEC_16:
6484 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6485 &thermal_temp_input16_group);
6489 case TPACPI_THERMAL_TPEC_8:
6490 case TPACPI_THERMAL_ACPI_TMP07:
6491 case TPACPI_THERMAL_ACPI_UPDT:
6492 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6493 &thermal_temp_input8_group);
6497 case TPACPI_THERMAL_NONE:
6505 static void thermal_exit(void)
6507 switch (thermal_read_mode) {
6508 case TPACPI_THERMAL_TPEC_16:
6509 sysfs_remove_group(&tpacpi_hwmon->kobj,
6510 &thermal_temp_input16_group);
6512 case TPACPI_THERMAL_TPEC_8:
6513 case TPACPI_THERMAL_ACPI_TMP07:
6514 case TPACPI_THERMAL_ACPI_UPDT:
6515 sysfs_remove_group(&tpacpi_hwmon->kobj,
6516 &thermal_temp_input8_group);
6518 case TPACPI_THERMAL_NONE:
6524 static int thermal_read(struct seq_file *m)
6527 struct ibm_thermal_sensors_struct t;
6529 n = thermal_get_sensors(&t);
6530 if (unlikely(n < 0))
6533 seq_printf(m, "temperatures:\t");
6536 for (i = 0; i < (n - 1); i++)
6537 seq_printf(m, "%d ", t.temp[i] / 1000);
6538 seq_printf(m, "%d\n", t.temp[i] / 1000);
6540 seq_printf(m, "not supported\n");
6545 static struct ibm_struct thermal_driver_data = {
6547 .read = thermal_read,
6548 .exit = thermal_exit,
6551 /*************************************************************************
6552 * Backlight/brightness subdriver
6555 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6558 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6559 * CMOS NVRAM byte 0x5E, bits 0-3.
6561 * EC HBRV (0x31) has the following layout
6562 * Bit 7: unknown function
6563 * Bit 6: unknown function
6564 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6565 * Bit 4: must be set to zero to avoid problems
6566 * Bit 3-0: backlight brightness level
6568 * brightness_get_raw returns status data in the HBRV layout
6570 * WARNING: The X61 has been verified to use HBRV for something else, so
6571 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6572 * testing on the very early *60 Lenovo models...
6576 TP_EC_BACKLIGHT = 0x31,
6578 /* TP_EC_BACKLIGHT bitmasks */
6579 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6580 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6581 TP_EC_BACKLIGHT_MAPSW = 0x20,
6584 enum tpacpi_brightness_access_mode {
6585 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6586 TPACPI_BRGHT_MODE_EC, /* EC control */
6587 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6588 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6589 TPACPI_BRGHT_MODE_MAX
6592 static struct backlight_device *ibm_backlight_device;
6594 static enum tpacpi_brightness_access_mode brightness_mode =
6595 TPACPI_BRGHT_MODE_MAX;
6597 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6599 static struct mutex brightness_mutex;
6601 /* NVRAM brightness access,
6602 * call with brightness_mutex held! */
6603 static unsigned int tpacpi_brightness_nvram_get(void)
6607 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6608 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6609 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6610 lnvram &= bright_maxlvl;
6615 static void tpacpi_brightness_checkpoint_nvram(void)
6620 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6623 vdbg_printk(TPACPI_DBG_BRGHT,
6624 "trying to checkpoint backlight level to NVRAM...\n");
6626 if (mutex_lock_killable(&brightness_mutex) < 0)
6629 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6631 lec &= TP_EC_BACKLIGHT_LVLMSK;
6632 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6634 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6635 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6636 /* NVRAM needs update */
6637 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6638 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6640 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6641 dbg_printk(TPACPI_DBG_BRGHT,
6642 "updated NVRAM backlight level to %u (0x%02x)\n",
6643 (unsigned int) lec, (unsigned int) b_nvram);
6645 vdbg_printk(TPACPI_DBG_BRGHT,
6646 "NVRAM backlight level already is %u (0x%02x)\n",
6647 (unsigned int) lec, (unsigned int) b_nvram);
6650 mutex_unlock(&brightness_mutex);
6654 /* call with brightness_mutex held! */
6655 static int tpacpi_brightness_get_raw(int *status)
6659 switch (brightness_mode) {
6660 case TPACPI_BRGHT_MODE_UCMS_STEP:
6661 *status = tpacpi_brightness_nvram_get();
6663 case TPACPI_BRGHT_MODE_EC:
6664 case TPACPI_BRGHT_MODE_ECNVRAM:
6665 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6674 /* call with brightness_mutex held! */
6675 /* do NOT call with illegal backlight level value */
6676 static int tpacpi_brightness_set_ec(unsigned int value)
6680 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6683 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6684 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6685 (value & TP_EC_BACKLIGHT_LVLMSK))))
6691 /* call with brightness_mutex held! */
6692 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6695 unsigned int current_value, i;
6697 current_value = tpacpi_brightness_nvram_get();
6699 if (value == current_value)
6702 cmos_cmd = (value > current_value) ?
6703 TP_CMOS_BRIGHTNESS_UP :
6704 TP_CMOS_BRIGHTNESS_DOWN;
6705 inc = (value > current_value) ? 1 : -1;
6707 for (i = current_value; i != value; i += inc)
6708 if (issue_thinkpad_cmos_command(cmos_cmd))
6714 /* May return EINTR which can always be mapped to ERESTARTSYS */
6715 static int brightness_set(unsigned int value)
6719 if (value > bright_maxlvl)
6722 vdbg_printk(TPACPI_DBG_BRGHT,
6723 "set backlight level to %d\n", value);
6725 res = mutex_lock_killable(&brightness_mutex);
6729 switch (brightness_mode) {
6730 case TPACPI_BRGHT_MODE_EC:
6731 case TPACPI_BRGHT_MODE_ECNVRAM:
6732 res = tpacpi_brightness_set_ec(value);
6734 case TPACPI_BRGHT_MODE_UCMS_STEP:
6735 res = tpacpi_brightness_set_ucmsstep(value);
6741 mutex_unlock(&brightness_mutex);
6745 /* sysfs backlight class ----------------------------------------------- */
6747 static int brightness_update_status(struct backlight_device *bd)
6749 unsigned int level =
6750 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6751 bd->props.power == FB_BLANK_UNBLANK) ?
6752 bd->props.brightness : 0;
6754 dbg_printk(TPACPI_DBG_BRGHT,
6755 "backlight: attempt to set level to %d\n",
6758 /* it is the backlight class's job (caller) to handle
6759 * EINTR and other errors properly */
6760 return brightness_set(level);
6763 static int brightness_get(struct backlight_device *bd)
6767 res = mutex_lock_killable(&brightness_mutex);
6771 res = tpacpi_brightness_get_raw(&status);
6773 mutex_unlock(&brightness_mutex);
6778 return status & TP_EC_BACKLIGHT_LVLMSK;
6781 static void tpacpi_brightness_notify_change(void)
6783 backlight_force_update(ibm_backlight_device,
6784 BACKLIGHT_UPDATE_HOTKEY);
6787 static const struct backlight_ops ibm_backlight_data = {
6788 .get_brightness = brightness_get,
6789 .update_status = brightness_update_status,
6792 /* --------------------------------------------------------------------- */
6795 * Call _BCL method of video device. On some ThinkPads this will
6796 * switch the firmware to the ACPI brightness control mode.
6799 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6801 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6802 union acpi_object *obj;
6803 struct acpi_device *device, *child;
6806 if (acpi_bus_get_device(handle, &device))
6810 list_for_each_entry(child, &device->children, node) {
6811 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6813 if (ACPI_FAILURE(status))
6816 obj = (union acpi_object *)buffer.pointer;
6817 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6818 pr_err("Unknown _BCL data, please report this to %s\n",
6822 rc = obj->package.count;
6827 kfree(buffer.pointer);
6833 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6835 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6837 acpi_handle video_device;
6840 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6842 bcl_levels = tpacpi_query_bcl_levels(video_device);
6844 tp_features.bright_acpimode = (bcl_levels > 0);
6846 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6850 * These are only useful for models that have only one possibility
6851 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6854 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6855 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6856 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6858 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6859 /* Models with ATI GPUs known to require ECNVRAM mode */
6860 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6862 /* Models with ATI GPUs that can use ECNVRAM */
6863 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6864 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6865 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6866 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6868 /* Models with Intel Extreme Graphics 2 */
6869 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6870 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6871 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6873 /* Models with Intel GMA900 */
6874 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6875 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6876 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6880 * Returns < 0 for error, otherwise sets tp_features.bright_*
6881 * and bright_maxlvl.
6883 static void __init tpacpi_detect_brightness_capabilities(void)
6887 vdbg_printk(TPACPI_DBG_INIT,
6888 "detecting firmware brightness interface capabilities\n");
6890 /* we could run a quirks check here (same table used by
6891 * brightness_init) if needed */
6894 * We always attempt to detect acpi support, so as to switch
6895 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6896 * going to publish a backlight interface
6898 b = tpacpi_check_std_acpi_brightness_support();
6908 tp_features.bright_unkfw = 1;
6909 bright_maxlvl = b - 1;
6911 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6914 static int __init brightness_init(struct ibm_init_struct *iibm)
6916 struct backlight_properties props;
6918 unsigned long quirks;
6920 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6922 mutex_init(&brightness_mutex);
6924 quirks = tpacpi_check_quirks(brightness_quirk_table,
6925 ARRAY_SIZE(brightness_quirk_table));
6927 /* tpacpi_detect_brightness_capabilities() must have run already */
6929 /* if it is unknown, we don't handle it: it wouldn't be safe */
6930 if (tp_features.bright_unkfw)
6933 if (!brightness_enable) {
6934 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6935 "brightness support disabled by module parameter\n");
6939 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6940 if (brightness_enable > 1) {
6941 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6943 } else if (brightness_enable == 1) {
6944 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6947 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6948 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
6952 * Check for module parameter bogosity, note that we
6953 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6954 * able to detect "unspecified"
6956 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6959 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6960 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6961 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6962 if (quirks & TPACPI_BRGHT_Q_EC)
6963 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6965 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6967 dbg_printk(TPACPI_DBG_BRGHT,
6968 "driver auto-selected brightness_mode=%d\n",
6973 if (!tpacpi_is_ibm() &&
6974 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6975 brightness_mode == TPACPI_BRGHT_MODE_EC))
6978 if (tpacpi_brightness_get_raw(&b) < 0)
6981 memset(&props, 0, sizeof(struct backlight_properties));
6982 props.type = BACKLIGHT_PLATFORM;
6983 props.max_brightness = bright_maxlvl;
6984 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6985 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6987 &ibm_backlight_data,
6989 if (IS_ERR(ibm_backlight_device)) {
6990 int rc = PTR_ERR(ibm_backlight_device);
6991 ibm_backlight_device = NULL;
6992 pr_err("Could not register backlight device\n");
6995 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6996 "brightness is supported\n");
6998 if (quirks & TPACPI_BRGHT_Q_ASK) {
6999 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7001 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7005 /* Added by mistake in early 2007. Probably useless, but it could
7006 * be working around some unknown firmware problem where the value
7007 * read at startup doesn't match the real hardware state... so leave
7008 * it in place just in case */
7009 backlight_update_status(ibm_backlight_device);
7011 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7012 "brightness: registering brightness hotkeys as change notification\n");
7013 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7014 | TP_ACPI_HKEY_BRGHTUP_MASK
7015 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7019 static void brightness_suspend(void)
7021 tpacpi_brightness_checkpoint_nvram();
7024 static void brightness_shutdown(void)
7026 tpacpi_brightness_checkpoint_nvram();
7029 static void brightness_exit(void)
7031 if (ibm_backlight_device) {
7032 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7033 "calling backlight_device_unregister()\n");
7034 backlight_device_unregister(ibm_backlight_device);
7037 tpacpi_brightness_checkpoint_nvram();
7040 static int brightness_read(struct seq_file *m)
7044 level = brightness_get(NULL);
7046 seq_printf(m, "level:\t\tunreadable\n");
7048 seq_printf(m, "level:\t\t%d\n", level);
7049 seq_printf(m, "commands:\tup, down\n");
7050 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7057 static int brightness_write(char *buf)
7063 level = brightness_get(NULL);
7067 while ((cmd = next_cmd(&buf))) {
7068 if (strlencmp(cmd, "up") == 0) {
7069 if (level < bright_maxlvl)
7071 } else if (strlencmp(cmd, "down") == 0) {
7074 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7075 level >= 0 && level <= bright_maxlvl) {
7081 tpacpi_disclose_usertask("procfs brightness",
7082 "set level to %d\n", level);
7085 * Now we know what the final level should be, so we try to set it.
7086 * Doing it this way makes the syscall restartable in case of EINTR
7088 rc = brightness_set(level);
7089 if (!rc && ibm_backlight_device)
7090 backlight_force_update(ibm_backlight_device,
7091 BACKLIGHT_UPDATE_SYSFS);
7092 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7095 static struct ibm_struct brightness_driver_data = {
7096 .name = "brightness",
7097 .read = brightness_read,
7098 .write = brightness_write,
7099 .exit = brightness_exit,
7100 .suspend = brightness_suspend,
7101 .shutdown = brightness_shutdown,
7104 /*************************************************************************
7109 * IBM ThinkPads have a simple volume controller with MUTE gating.
7110 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7112 * Since the *61 series (and probably also the later *60 series), Lenovo
7113 * ThinkPads only implement the MUTE gate.
7116 * Bit 6: MUTE (1 mutes sound)
7118 * Other bits should be zero as far as we know.
7120 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7121 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7122 * such as bit 7 which is used to detect repeated presses of MUTE,
7123 * and we leave them unchanged.
7125 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7126 * in response to user input. Unfortunately, this rarely works well.
7127 * The laptop changes the state of its internal MUTE gate and, on some
7128 * models, sends KEY_MUTE, causing any user code that responds to the
7129 * mute button to get confused. The hardware MUTE gate is also
7130 * unnecessary, since user code can handle the mute button without
7131 * kernel or EC help.
7133 * To avoid confusing userspace, we simply disable all EC-based mute
7134 * and volume controls when possible.
7137 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7139 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7140 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7141 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7143 #if SNDRV_CARDS <= 32
7144 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7146 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7148 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7149 static char *alsa_id = "ThinkPadEC";
7150 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7152 struct tpacpi_alsa_data {
7153 struct snd_card *card;
7154 struct snd_ctl_elem_id *ctl_mute_id;
7155 struct snd_ctl_elem_id *ctl_vol_id;
7158 static struct snd_card *alsa_card;
7163 /* TP_EC_AUDIO bits */
7164 TP_EC_AUDIO_MUTESW = 6,
7166 /* TP_EC_AUDIO bitmasks */
7167 TP_EC_AUDIO_LVL_MSK = 0x0F,
7168 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7170 /* Maximum volume */
7171 TP_EC_VOLUME_MAX = 14,
7174 enum tpacpi_volume_access_mode {
7175 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7176 TPACPI_VOL_MODE_EC, /* Pure EC control */
7177 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7178 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7182 enum tpacpi_volume_capabilities {
7183 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7184 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7185 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7189 enum tpacpi_mute_btn_mode {
7190 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7191 /* We don't know what mode 1 is. */
7192 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7193 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7196 static enum tpacpi_volume_access_mode volume_mode =
7197 TPACPI_VOL_MODE_MAX;
7199 static enum tpacpi_volume_capabilities volume_capabilities;
7200 static bool volume_control_allowed;
7201 static bool software_mute_requested = true;
7202 static bool software_mute_active;
7203 static int software_mute_orig_mode;
7206 * Used to syncronize writers to TP_EC_AUDIO and
7207 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7209 static struct mutex volume_mutex;
7211 static void tpacpi_volume_checkpoint_nvram(void)
7217 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7219 if (!volume_control_allowed)
7221 if (software_mute_active)
7224 vdbg_printk(TPACPI_DBG_MIXER,
7225 "trying to checkpoint mixer state to NVRAM...\n");
7227 if (tp_features.mixer_no_level_control)
7228 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7230 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7232 if (mutex_lock_killable(&volume_mutex) < 0)
7235 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7238 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7240 if (lec != (b_nvram & ec_mask)) {
7241 /* NVRAM needs update */
7242 b_nvram &= ~ec_mask;
7244 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7245 dbg_printk(TPACPI_DBG_MIXER,
7246 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7247 (unsigned int) lec, (unsigned int) b_nvram);
7249 vdbg_printk(TPACPI_DBG_MIXER,
7250 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7251 (unsigned int) lec, (unsigned int) b_nvram);
7255 mutex_unlock(&volume_mutex);
7258 static int volume_get_status_ec(u8 *status)
7262 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7267 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7272 static int volume_get_status(u8 *status)
7274 return volume_get_status_ec(status);
7277 static int volume_set_status_ec(const u8 status)
7279 if (!acpi_ec_write(TP_EC_AUDIO, status))
7282 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7285 * On X200s, and possibly on others, it can take a while for
7286 * reads to become correct.
7293 static int volume_set_status(const u8 status)
7295 return volume_set_status_ec(status);
7298 /* returns < 0 on error, 0 on no change, 1 on change */
7299 static int __volume_set_mute_ec(const bool mute)
7304 if (mutex_lock_killable(&volume_mutex) < 0)
7307 rc = volume_get_status_ec(&s);
7311 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7312 s & ~TP_EC_AUDIO_MUTESW_MSK;
7315 rc = volume_set_status_ec(n);
7321 mutex_unlock(&volume_mutex);
7325 static int volume_alsa_set_mute(const bool mute)
7327 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7328 (mute) ? "" : "un");
7329 return __volume_set_mute_ec(mute);
7332 static int volume_set_mute(const bool mute)
7336 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7337 (mute) ? "" : "un");
7339 rc = __volume_set_mute_ec(mute);
7340 return (rc < 0) ? rc : 0;
7343 /* returns < 0 on error, 0 on no change, 1 on change */
7344 static int __volume_set_volume_ec(const u8 vol)
7349 if (vol > TP_EC_VOLUME_MAX)
7352 if (mutex_lock_killable(&volume_mutex) < 0)
7355 rc = volume_get_status_ec(&s);
7359 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7362 rc = volume_set_status_ec(n);
7368 mutex_unlock(&volume_mutex);
7372 static int volume_set_software_mute(bool startup)
7376 if (!tpacpi_is_lenovo())
7380 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7384 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7385 "Initial HAUM setting was %d\n",
7386 software_mute_orig_mode);
7389 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7390 (int)TP_EC_MUTE_BTN_NONE))
7393 if (result != TP_EC_MUTE_BTN_NONE)
7394 pr_warn("Unexpected SAUM result %d\n",
7398 * In software mute mode, the standard codec controls take
7399 * precendence, so we unmute the ThinkPad HW switch at
7400 * startup. Just on case there are SAUM-capable ThinkPads
7401 * with level controls, set max HW volume as well.
7403 if (tp_features.mixer_no_level_control)
7404 result = volume_set_mute(false);
7406 result = volume_set_status(TP_EC_VOLUME_MAX);
7409 pr_warn("Failed to unmute the HW mute switch\n");
7414 static void volume_exit_software_mute(void)
7418 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7419 || r != software_mute_orig_mode)
7420 pr_warn("Failed to restore mute mode\n");
7423 static int volume_alsa_set_volume(const u8 vol)
7425 dbg_printk(TPACPI_DBG_MIXER,
7426 "ALSA: trying to set volume level to %hu\n", vol);
7427 return __volume_set_volume_ec(vol);
7430 static void volume_alsa_notify_change(void)
7432 struct tpacpi_alsa_data *d;
7434 if (alsa_card && alsa_card->private_data) {
7435 d = alsa_card->private_data;
7437 snd_ctl_notify(alsa_card,
7438 SNDRV_CTL_EVENT_MASK_VALUE,
7441 snd_ctl_notify(alsa_card,
7442 SNDRV_CTL_EVENT_MASK_VALUE,
7447 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7448 struct snd_ctl_elem_info *uinfo)
7450 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7452 uinfo->value.integer.min = 0;
7453 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7457 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7458 struct snd_ctl_elem_value *ucontrol)
7463 rc = volume_get_status(&s);
7467 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7471 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7472 struct snd_ctl_elem_value *ucontrol)
7474 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7475 ucontrol->value.integer.value[0]);
7476 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7479 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7481 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7482 struct snd_ctl_elem_value *ucontrol)
7487 rc = volume_get_status(&s);
7491 ucontrol->value.integer.value[0] =
7492 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7496 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7497 struct snd_ctl_elem_value *ucontrol)
7499 tpacpi_disclose_usertask("ALSA", "%smute\n",
7500 ucontrol->value.integer.value[0] ?
7502 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7505 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7507 .name = "Console Playback Volume",
7509 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7510 .info = volume_alsa_vol_info,
7511 .get = volume_alsa_vol_get,
7514 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7515 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7516 .name = "Console Playback Switch",
7518 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7519 .info = volume_alsa_mute_info,
7520 .get = volume_alsa_mute_get,
7523 static void volume_suspend(void)
7525 tpacpi_volume_checkpoint_nvram();
7528 static void volume_resume(void)
7530 if (software_mute_active) {
7531 if (volume_set_software_mute(false) < 0)
7532 pr_warn("Failed to restore software mute\n");
7534 volume_alsa_notify_change();
7538 static void volume_shutdown(void)
7540 tpacpi_volume_checkpoint_nvram();
7543 static void volume_exit(void)
7546 snd_card_free(alsa_card);
7550 tpacpi_volume_checkpoint_nvram();
7552 if (software_mute_active)
7553 volume_exit_software_mute();
7556 static int __init volume_create_alsa_mixer(void)
7558 struct snd_card *card;
7559 struct tpacpi_alsa_data *data;
7560 struct snd_kcontrol *ctl_vol;
7561 struct snd_kcontrol *ctl_mute;
7564 rc = snd_card_new(&tpacpi_pdev->dev,
7565 alsa_index, alsa_id, THIS_MODULE,
7566 sizeof(struct tpacpi_alsa_data), &card);
7567 if (rc < 0 || !card) {
7568 pr_err("Failed to create ALSA card structures: %d\n", rc);
7572 BUG_ON(!card->private_data);
7573 data = card->private_data;
7576 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7577 sizeof(card->driver));
7578 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7579 sizeof(card->shortname));
7580 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7581 (thinkpad_id.ec_version_str) ?
7582 thinkpad_id.ec_version_str : "(unknown)");
7583 snprintf(card->longname, sizeof(card->longname),
7584 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7585 (thinkpad_id.ec_version_str) ?
7586 thinkpad_id.ec_version_str : "unknown");
7588 if (volume_control_allowed) {
7589 volume_alsa_control_vol.put = volume_alsa_vol_put;
7590 volume_alsa_control_vol.access =
7591 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7593 volume_alsa_control_mute.put = volume_alsa_mute_put;
7594 volume_alsa_control_mute.access =
7595 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7598 if (!tp_features.mixer_no_level_control) {
7599 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7600 rc = snd_ctl_add(card, ctl_vol);
7602 pr_err("Failed to create ALSA volume control: %d\n",
7606 data->ctl_vol_id = &ctl_vol->id;
7609 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7610 rc = snd_ctl_add(card, ctl_mute);
7612 pr_err("Failed to create ALSA mute control: %d\n", rc);
7615 data->ctl_mute_id = &ctl_mute->id;
7617 rc = snd_card_register(card);
7619 pr_err("Failed to register ALSA card: %d\n", rc);
7627 snd_card_free(card);
7631 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7632 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7634 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7635 /* Whitelist volume level on all IBM by default */
7636 { .vendor = PCI_VENDOR_ID_IBM,
7637 .bios = TPACPI_MATCH_ANY,
7638 .ec = TPACPI_MATCH_ANY,
7639 .quirks = TPACPI_VOL_Q_LEVEL },
7641 /* Lenovo models with volume control (needs confirmation) */
7642 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7643 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7644 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7645 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7646 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7647 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7648 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7650 /* Whitelist mute-only on all Lenovo by default */
7651 { .vendor = PCI_VENDOR_ID_LENOVO,
7652 .bios = TPACPI_MATCH_ANY,
7653 .ec = TPACPI_MATCH_ANY,
7654 .quirks = TPACPI_VOL_Q_MUTEONLY }
7657 static int __init volume_init(struct ibm_init_struct *iibm)
7659 unsigned long quirks;
7662 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7664 mutex_init(&volume_mutex);
7667 * Check for module parameter bogosity, note that we
7668 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7669 * able to detect "unspecified"
7671 if (volume_mode > TPACPI_VOL_MODE_MAX)
7674 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7675 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7680 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7684 * The ALSA mixer is our primary interface.
7685 * When disabled, don't install the subdriver at all
7688 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7689 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7693 quirks = tpacpi_check_quirks(volume_quirk_table,
7694 ARRAY_SIZE(volume_quirk_table));
7696 switch (volume_capabilities) {
7697 case TPACPI_VOL_CAP_AUTO:
7698 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7699 tp_features.mixer_no_level_control = 1;
7700 else if (quirks & TPACPI_VOL_Q_LEVEL)
7701 tp_features.mixer_no_level_control = 0;
7703 return 1; /* no mixer */
7705 case TPACPI_VOL_CAP_VOLMUTE:
7706 tp_features.mixer_no_level_control = 0;
7708 case TPACPI_VOL_CAP_MUTEONLY:
7709 tp_features.mixer_no_level_control = 1;
7715 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7716 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7717 "using user-supplied volume_capabilities=%d\n",
7718 volume_capabilities);
7720 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7721 volume_mode == TPACPI_VOL_MODE_MAX) {
7722 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7724 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7725 "driver auto-selected volume_mode=%d\n",
7728 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7729 "using user-supplied volume_mode=%d\n",
7733 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7734 "mute is supported, volume control is %s\n",
7735 str_supported(!tp_features.mixer_no_level_control));
7737 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7738 software_mute_active = true;
7740 rc = volume_create_alsa_mixer();
7742 pr_err("Could not create the ALSA mixer interface\n");
7746 pr_info("Console audio control enabled, mode: %s\n",
7747 (volume_control_allowed) ?
7748 "override (read/write)" :
7749 "monitor (read only)");
7752 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7753 "registering volume hotkeys as change notification\n");
7754 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7755 | TP_ACPI_HKEY_VOLUP_MASK
7756 | TP_ACPI_HKEY_VOLDWN_MASK
7757 | TP_ACPI_HKEY_MUTE_MASK);
7762 static int volume_read(struct seq_file *m)
7766 if (volume_get_status(&status) < 0) {
7767 seq_printf(m, "level:\t\tunreadable\n");
7769 if (tp_features.mixer_no_level_control)
7770 seq_printf(m, "level:\t\tunsupported\n");
7772 seq_printf(m, "level:\t\t%d\n",
7773 status & TP_EC_AUDIO_LVL_MSK);
7775 seq_printf(m, "mute:\t\t%s\n",
7776 onoff(status, TP_EC_AUDIO_MUTESW));
7778 if (volume_control_allowed) {
7779 seq_printf(m, "commands:\tunmute, mute\n");
7780 if (!tp_features.mixer_no_level_control) {
7781 seq_printf(m, "commands:\tup, down\n");
7782 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7791 static int volume_write(char *buf)
7794 u8 new_level, new_mute;
7800 * We do allow volume control at driver startup, so that the
7801 * user can set initial state through the volume=... parameter hack.
7803 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7804 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7805 tp_warned.volume_ctrl_forbidden = 1;
7806 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7807 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7812 rc = volume_get_status(&s);
7816 new_level = s & TP_EC_AUDIO_LVL_MSK;
7817 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7819 while ((cmd = next_cmd(&buf))) {
7820 if (!tp_features.mixer_no_level_control) {
7821 if (strlencmp(cmd, "up") == 0) {
7824 else if (new_level < TP_EC_VOLUME_MAX)
7827 } else if (strlencmp(cmd, "down") == 0) {
7830 else if (new_level > 0)
7833 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7834 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7839 if (strlencmp(cmd, "mute") == 0)
7840 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7841 else if (strlencmp(cmd, "unmute") == 0)
7847 if (tp_features.mixer_no_level_control) {
7848 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7849 new_mute ? "" : "un");
7850 rc = volume_set_mute(!!new_mute);
7852 tpacpi_disclose_usertask("procfs volume",
7853 "%smute and set level to %d\n",
7854 new_mute ? "" : "un", new_level);
7855 rc = volume_set_status(new_mute | new_level);
7857 volume_alsa_notify_change();
7859 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7862 static struct ibm_struct volume_driver_data = {
7864 .read = volume_read,
7865 .write = volume_write,
7866 .exit = volume_exit,
7867 .suspend = volume_suspend,
7868 .resume = volume_resume,
7869 .shutdown = volume_shutdown,
7872 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7874 #define alsa_card NULL
7876 static inline void volume_alsa_notify_change(void)
7880 static int __init volume_init(struct ibm_init_struct *iibm)
7882 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7887 static struct ibm_struct volume_driver_data = {
7891 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7893 /*************************************************************************
7900 * TPACPI_FAN_RD_ACPI_GFAN:
7901 * ACPI GFAN method: returns fan level
7903 * see TPACPI_FAN_WR_ACPI_SFAN
7904 * EC 0x2f (HFSP) not available if GFAN exists
7906 * TPACPI_FAN_WR_ACPI_SFAN:
7907 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7909 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7912 * TPACPI_FAN_WR_TPEC:
7913 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7914 * Supported on almost all ThinkPads
7916 * Fan speed changes of any sort (including those caused by the
7917 * disengaged mode) are usually done slowly by the firmware as the
7918 * maximum amount of fan duty cycle change per second seems to be
7921 * Reading is not available if GFAN exists.
7922 * Writing is not available if SFAN exists.
7925 * 7 automatic mode engaged;
7926 * (default operation mode of the ThinkPad)
7927 * fan level is ignored in this mode.
7928 * 6 full speed mode (takes precedence over bit 7);
7929 * not available on all thinkpads. May disable
7930 * the tachometer while the fan controller ramps up
7931 * the speed (which can take up to a few *minutes*).
7932 * Speeds up fan to 100% duty-cycle, which is far above
7933 * the standard RPM levels. It is not impossible that
7934 * it could cause hardware damage.
7935 * 5-3 unused in some models. Extra bits for fan level
7936 * in others, but still useless as all values above
7937 * 7 map to the same speed as level 7 in these models.
7938 * 2-0 fan level (0..7 usually)
7940 * 0x07 = max (set when temperatures critical)
7941 * Some ThinkPads may have other levels, see
7942 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7944 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7945 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7946 * does so, its initial value is meaningless (0x07).
7948 * For firmware bugs, refer to:
7949 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7953 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7954 * Main fan tachometer reading (in RPM)
7956 * This register is present on all ThinkPads with a new-style EC, and
7957 * it is known not to be present on the A21m/e, and T22, as there is
7958 * something else in offset 0x84 according to the ACPI DSDT. Other
7959 * ThinkPads from this same time period (and earlier) probably lack the
7960 * tachometer as well.
7962 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7963 * was never fixed by IBM to report the EC firmware version string
7964 * probably support the tachometer (like the early X models), so
7965 * detecting it is quite hard. We need more data to know for sure.
7967 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7970 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7973 * For firmware bugs, refer to:
7974 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7978 * ThinkPad EC register 0x31 bit 0 (only on select models)
7980 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7981 * show the speed of the main fan. When bit 0 of EC register 0x31
7982 * is one, the tachometer registers show the speed of the auxiliary
7985 * Fan control seems to affect both fans, regardless of the state
7988 * So far, only the firmware for the X60/X61 non-tablet versions
7989 * seem to support this (firmware TP-7M).
7991 * TPACPI_FAN_WR_ACPI_FANS:
7992 * ThinkPad X31, X40, X41. Not available in the X60.
7994 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7995 * high speed. ACPI DSDT seems to map these three speeds to levels
7996 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7997 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7999 * The speeds are stored on handles
8000 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8002 * There are three default speed sets, accessible as handles:
8003 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8005 * ACPI DSDT switches which set is in use depending on various
8008 * TPACPI_FAN_WR_TPEC is also available and should be used to
8009 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8010 * but the ACPI tables just mention level 7.
8013 enum { /* Fan control constants */
8014 fan_status_offset = 0x2f, /* EC register 0x2f */
8015 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8016 * 0x84 must be read before 0x85 */
8017 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8018 bit 0 selects which fan is active */
8020 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8021 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8023 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8026 enum fan_status_access_mode {
8027 TPACPI_FAN_NONE = 0, /* No fan status or control */
8028 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8029 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8032 enum fan_control_access_mode {
8033 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8034 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8035 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8036 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8039 enum fan_control_commands {
8040 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8041 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8042 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8043 * and also watchdog cmd */
8046 static bool fan_control_allowed;
8048 static enum fan_status_access_mode fan_status_access_mode;
8049 static enum fan_control_access_mode fan_control_access_mode;
8050 static enum fan_control_commands fan_control_commands;
8052 static u8 fan_control_initial_status;
8053 static u8 fan_control_desired_level;
8054 static u8 fan_control_resume_level;
8055 static int fan_watchdog_maxinterval;
8057 static struct mutex fan_mutex;
8059 static void fan_watchdog_fire(struct work_struct *ignored);
8060 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8062 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8063 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8064 "\\FSPD", /* 600e/x, 770e, 770x */
8066 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8067 "JFNS", /* 770x-JL */
8071 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8072 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8073 * be in auto mode (0x80).
8075 * This is corrected by any write to HFSP either by the driver, or
8078 * We assume 0x07 really means auto mode while this quirk is active,
8079 * as this is far more likely than the ThinkPad being in level 7,
8080 * which is only used by the firmware during thermal emergencies.
8082 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8083 * TP-70 (T43, R52), which are known to be buggy.
8086 static void fan_quirk1_setup(void)
8088 if (fan_control_initial_status == 0x07) {
8089 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8090 tp_features.fan_ctrl_status_undef = 1;
8094 static void fan_quirk1_handle(u8 *fan_status)
8096 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8097 if (*fan_status != fan_control_initial_status) {
8098 /* something changed the HFSP regisnter since
8099 * driver init time, so it is not undefined
8101 tp_features.fan_ctrl_status_undef = 0;
8103 /* Return most likely status. In fact, it
8104 * might be the only possible status */
8105 *fan_status = TP_EC_FAN_AUTO;
8110 /* Select main fan on X60/X61, NOOP on others */
8111 static bool fan_select_fan1(void)
8113 if (tp_features.second_fan) {
8116 if (ec_read(fan_select_offset, &val) < 0)
8119 if (ec_write(fan_select_offset, val) < 0)
8125 /* Select secondary fan on X60/X61 */
8126 static bool fan_select_fan2(void)
8130 if (!tp_features.second_fan)
8133 if (ec_read(fan_select_offset, &val) < 0)
8136 if (ec_write(fan_select_offset, val) < 0)
8143 * Call with fan_mutex held
8145 static void fan_update_desired_level(u8 status)
8148 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8150 fan_control_desired_level = 7;
8152 fan_control_desired_level = status;
8156 static int fan_get_status(u8 *status)
8161 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8163 switch (fan_status_access_mode) {
8164 case TPACPI_FAN_RD_ACPI_GFAN: {
8165 /* 570, 600e/x, 770e, 770x */
8168 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8172 *status = res & 0x07;
8176 case TPACPI_FAN_RD_TPEC:
8177 /* all except 570, 600e/x, 770e, 770x */
8178 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8181 if (likely(status)) {
8183 fan_quirk1_handle(status);
8195 static int fan_get_status_safe(u8 *status)
8200 if (mutex_lock_killable(&fan_mutex))
8201 return -ERESTARTSYS;
8202 rc = fan_get_status(&s);
8204 fan_update_desired_level(s);
8205 mutex_unlock(&fan_mutex);
8215 static int fan_get_speed(unsigned int *speed)
8219 switch (fan_status_access_mode) {
8220 case TPACPI_FAN_RD_TPEC:
8221 /* all except 570, 600e/x, 770e, 770x */
8222 if (unlikely(!fan_select_fan1()))
8224 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8225 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8229 *speed = (hi << 8) | lo;
8240 static int fan2_get_speed(unsigned int *speed)
8245 switch (fan_status_access_mode) {
8246 case TPACPI_FAN_RD_TPEC:
8247 /* all except 570, 600e/x, 770e, 770x */
8248 if (unlikely(!fan_select_fan2()))
8250 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8251 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8252 fan_select_fan1(); /* play it safe */
8257 *speed = (hi << 8) | lo;
8268 static int fan_set_level(int level)
8270 if (!fan_control_allowed)
8273 switch (fan_control_access_mode) {
8274 case TPACPI_FAN_WR_ACPI_SFAN:
8275 if (level >= 0 && level <= 7) {
8276 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8282 case TPACPI_FAN_WR_ACPI_FANS:
8283 case TPACPI_FAN_WR_TPEC:
8284 if (!(level & TP_EC_FAN_AUTO) &&
8285 !(level & TP_EC_FAN_FULLSPEED) &&
8286 ((level < 0) || (level > 7)))
8289 /* safety net should the EC not support AUTO
8290 * or FULLSPEED mode bits and just ignore them */
8291 if (level & TP_EC_FAN_FULLSPEED)
8292 level |= 7; /* safety min speed 7 */
8293 else if (level & TP_EC_FAN_AUTO)
8294 level |= 4; /* safety min speed 4 */
8296 if (!acpi_ec_write(fan_status_offset, level))
8299 tp_features.fan_ctrl_status_undef = 0;
8306 vdbg_printk(TPACPI_DBG_FAN,
8307 "fan control: set fan control register to 0x%02x\n", level);
8311 static int fan_set_level_safe(int level)
8315 if (!fan_control_allowed)
8318 if (mutex_lock_killable(&fan_mutex))
8319 return -ERESTARTSYS;
8321 if (level == TPACPI_FAN_LAST_LEVEL)
8322 level = fan_control_desired_level;
8324 rc = fan_set_level(level);
8326 fan_update_desired_level(level);
8328 mutex_unlock(&fan_mutex);
8332 static int fan_set_enable(void)
8337 if (!fan_control_allowed)
8340 if (mutex_lock_killable(&fan_mutex))
8341 return -ERESTARTSYS;
8343 switch (fan_control_access_mode) {
8344 case TPACPI_FAN_WR_ACPI_FANS:
8345 case TPACPI_FAN_WR_TPEC:
8346 rc = fan_get_status(&s);
8350 /* Don't go out of emergency fan mode */
8353 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8356 if (!acpi_ec_write(fan_status_offset, s))
8359 tp_features.fan_ctrl_status_undef = 0;
8364 case TPACPI_FAN_WR_ACPI_SFAN:
8365 rc = fan_get_status(&s);
8371 /* Set fan to at least level 4 */
8374 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8384 mutex_unlock(&fan_mutex);
8387 vdbg_printk(TPACPI_DBG_FAN,
8388 "fan control: set fan control register to 0x%02x\n",
8393 static int fan_set_disable(void)
8397 if (!fan_control_allowed)
8400 if (mutex_lock_killable(&fan_mutex))
8401 return -ERESTARTSYS;
8404 switch (fan_control_access_mode) {
8405 case TPACPI_FAN_WR_ACPI_FANS:
8406 case TPACPI_FAN_WR_TPEC:
8407 if (!acpi_ec_write(fan_status_offset, 0x00))
8410 fan_control_desired_level = 0;
8411 tp_features.fan_ctrl_status_undef = 0;
8415 case TPACPI_FAN_WR_ACPI_SFAN:
8416 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8419 fan_control_desired_level = 0;
8427 vdbg_printk(TPACPI_DBG_FAN,
8428 "fan control: set fan control register to 0\n");
8430 mutex_unlock(&fan_mutex);
8434 static int fan_set_speed(int speed)
8438 if (!fan_control_allowed)
8441 if (mutex_lock_killable(&fan_mutex))
8442 return -ERESTARTSYS;
8445 switch (fan_control_access_mode) {
8446 case TPACPI_FAN_WR_ACPI_FANS:
8447 if (speed >= 0 && speed <= 65535) {
8448 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8449 speed, speed, speed))
8459 mutex_unlock(&fan_mutex);
8463 static void fan_watchdog_reset(void)
8465 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8468 if (fan_watchdog_maxinterval > 0 &&
8469 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8470 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8471 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8473 cancel_delayed_work(&fan_watchdog_task);
8476 static void fan_watchdog_fire(struct work_struct *ignored)
8480 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8483 pr_notice("fan watchdog: enabling fan\n");
8484 rc = fan_set_enable();
8486 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8488 /* reschedule for later */
8489 fan_watchdog_reset();
8494 * SYSFS fan layout: hwmon compatible (device)
8497 * 0: "disengaged" mode
8499 * 2: native EC "auto" mode (recommended, hardware default)
8501 * pwm*: set speed in manual mode, ignored otherwise.
8502 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8505 * fan*_input: tachometer reading, RPM
8508 * SYSFS fan layout: extensions
8510 * fan_watchdog (driver):
8511 * fan watchdog interval in seconds, 0 disables (default), max 120
8514 /* sysfs fan pwm1_enable ----------------------------------------------- */
8515 static ssize_t fan_pwm1_enable_show(struct device *dev,
8516 struct device_attribute *attr,
8522 res = fan_get_status_safe(&status);
8526 if (status & TP_EC_FAN_FULLSPEED) {
8528 } else if (status & TP_EC_FAN_AUTO) {
8533 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8536 static ssize_t fan_pwm1_enable_store(struct device *dev,
8537 struct device_attribute *attr,
8538 const char *buf, size_t count)
8543 if (parse_strtoul(buf, 2, &t))
8546 tpacpi_disclose_usertask("hwmon pwm1_enable",
8547 "set fan mode to %lu\n", t);
8551 level = TP_EC_FAN_FULLSPEED;
8554 level = TPACPI_FAN_LAST_LEVEL;
8557 level = TP_EC_FAN_AUTO;
8560 /* reserved for software-controlled auto mode */
8566 res = fan_set_level_safe(level);
8572 fan_watchdog_reset();
8577 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8578 fan_pwm1_enable_show, fan_pwm1_enable_store);
8580 /* sysfs fan pwm1 ------------------------------------------------------ */
8581 static ssize_t fan_pwm1_show(struct device *dev,
8582 struct device_attribute *attr,
8588 res = fan_get_status_safe(&status);
8593 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8594 status = fan_control_desired_level;
8599 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8602 static ssize_t fan_pwm1_store(struct device *dev,
8603 struct device_attribute *attr,
8604 const char *buf, size_t count)
8608 u8 status, newlevel;
8610 if (parse_strtoul(buf, 255, &s))
8613 tpacpi_disclose_usertask("hwmon pwm1",
8614 "set fan speed to %lu\n", s);
8616 /* scale down from 0-255 to 0-7 */
8617 newlevel = (s >> 5) & 0x07;
8619 if (mutex_lock_killable(&fan_mutex))
8620 return -ERESTARTSYS;
8622 rc = fan_get_status(&status);
8623 if (!rc && (status &
8624 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8625 rc = fan_set_level(newlevel);
8629 fan_update_desired_level(newlevel);
8630 fan_watchdog_reset();
8634 mutex_unlock(&fan_mutex);
8635 return (rc) ? rc : count;
8638 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8640 /* sysfs fan fan1_input ------------------------------------------------ */
8641 static ssize_t fan_fan1_input_show(struct device *dev,
8642 struct device_attribute *attr,
8648 res = fan_get_speed(&speed);
8652 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8655 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8657 /* sysfs fan fan2_input ------------------------------------------------ */
8658 static ssize_t fan_fan2_input_show(struct device *dev,
8659 struct device_attribute *attr,
8665 res = fan2_get_speed(&speed);
8669 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8672 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8674 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8675 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8677 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8680 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8685 if (parse_strtoul(buf, 120, &t))
8688 if (!fan_control_allowed)
8691 fan_watchdog_maxinterval = t;
8692 fan_watchdog_reset();
8694 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8698 static DRIVER_ATTR_RW(fan_watchdog);
8700 /* --------------------------------------------------------------------- */
8701 static struct attribute *fan_attributes[] = {
8702 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8703 &dev_attr_fan1_input.attr,
8704 NULL, /* for fan2_input */
8708 static const struct attribute_group fan_attr_group = {
8709 .attrs = fan_attributes,
8712 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8713 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8715 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8716 { .vendor = PCI_VENDOR_ID_IBM, \
8717 .bios = TPACPI_MATCH_ANY, \
8718 .ec = TPID(__id1, __id2), \
8719 .quirks = __quirks }
8721 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8722 { .vendor = PCI_VENDOR_ID_LENOVO, \
8723 .bios = TPACPI_MATCH_ANY, \
8724 .ec = TPID(__id1, __id2), \
8725 .quirks = __quirks }
8727 #define TPACPI_FAN_QB(__id1, __id2, __quirks) \
8728 { .vendor = PCI_VENDOR_ID_LENOVO, \
8729 .bios = TPID(__id1, __id2), \
8730 .ec = TPACPI_MATCH_ANY, \
8731 .quirks = __quirks }
8733 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8734 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8735 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8736 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8737 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8738 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8739 TPACPI_FAN_QB('N', '1', TPACPI_FAN_2FAN),
8742 #undef TPACPI_FAN_QL
8743 #undef TPACPI_FAN_QI
8744 #undef TPACPI_FAN_QB
8746 static int __init fan_init(struct ibm_init_struct *iibm)
8749 unsigned long quirks;
8751 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8752 "initializing fan subdriver\n");
8754 mutex_init(&fan_mutex);
8755 fan_status_access_mode = TPACPI_FAN_NONE;
8756 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8757 fan_control_commands = 0;
8758 fan_watchdog_maxinterval = 0;
8759 tp_features.fan_ctrl_status_undef = 0;
8760 tp_features.second_fan = 0;
8761 fan_control_desired_level = 7;
8763 if (tpacpi_is_ibm()) {
8764 TPACPI_ACPIHANDLE_INIT(fans);
8765 TPACPI_ACPIHANDLE_INIT(gfan);
8766 TPACPI_ACPIHANDLE_INIT(sfan);
8769 quirks = tpacpi_check_quirks(fan_quirk_table,
8770 ARRAY_SIZE(fan_quirk_table));
8773 /* 570, 600e/x, 770e, 770x */
8774 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8776 /* all other ThinkPads: note that even old-style
8777 * ThinkPad ECs supports the fan control register */
8778 if (likely(acpi_ec_read(fan_status_offset,
8779 &fan_control_initial_status))) {
8780 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8781 if (quirks & TPACPI_FAN_Q1)
8783 if (quirks & TPACPI_FAN_2FAN) {
8784 tp_features.second_fan = 1;
8785 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8786 "secondary fan support enabled\n");
8789 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8796 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8797 fan_control_commands |=
8798 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8801 /* gfan without sfan means no fan control */
8802 /* all other models implement TP EC 0x2f control */
8806 fan_control_access_mode =
8807 TPACPI_FAN_WR_ACPI_FANS;
8808 fan_control_commands |=
8809 TPACPI_FAN_CMD_SPEED |
8810 TPACPI_FAN_CMD_LEVEL |
8811 TPACPI_FAN_CMD_ENABLE;
8813 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8814 fan_control_commands |=
8815 TPACPI_FAN_CMD_LEVEL |
8816 TPACPI_FAN_CMD_ENABLE;
8821 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8822 "fan is %s, modes %d, %d\n",
8823 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8824 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8825 fan_status_access_mode, fan_control_access_mode);
8827 /* fan control master switch */
8828 if (!fan_control_allowed) {
8829 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8830 fan_control_commands = 0;
8831 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8832 "fan control features disabled by parameter\n");
8835 /* update fan_control_desired_level */
8836 if (fan_status_access_mode != TPACPI_FAN_NONE)
8837 fan_get_status_safe(NULL);
8839 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8840 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8841 if (tp_features.second_fan) {
8842 /* attach second fan tachometer */
8843 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8844 &dev_attr_fan2_input.attr;
8846 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8851 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8852 &driver_attr_fan_watchdog);
8854 sysfs_remove_group(&tpacpi_hwmon->kobj,
8863 static void fan_exit(void)
8865 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8866 "cancelling any pending fan watchdog tasks\n");
8868 /* FIXME: can we really do this unconditionally? */
8869 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8870 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8871 &driver_attr_fan_watchdog);
8873 cancel_delayed_work(&fan_watchdog_task);
8874 flush_workqueue(tpacpi_wq);
8877 static void fan_suspend(void)
8881 if (!fan_control_allowed)
8884 /* Store fan status in cache */
8885 fan_control_resume_level = 0;
8886 rc = fan_get_status_safe(&fan_control_resume_level);
8888 pr_notice("failed to read fan level for later restore during resume: %d\n",
8891 /* if it is undefined, don't attempt to restore it.
8893 if (tp_features.fan_ctrl_status_undef)
8894 fan_control_resume_level = 0;
8897 static void fan_resume(void)
8899 u8 current_level = 7;
8900 bool do_set = false;
8903 /* DSDT *always* updates status on resume */
8904 tp_features.fan_ctrl_status_undef = 0;
8906 if (!fan_control_allowed ||
8907 !fan_control_resume_level ||
8908 (fan_get_status_safe(¤t_level) < 0))
8911 switch (fan_control_access_mode) {
8912 case TPACPI_FAN_WR_ACPI_SFAN:
8913 /* never decrease fan level */
8914 do_set = (fan_control_resume_level > current_level);
8916 case TPACPI_FAN_WR_ACPI_FANS:
8917 case TPACPI_FAN_WR_TPEC:
8918 /* never decrease fan level, scale is:
8919 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8921 * We expect the firmware to set either 7 or AUTO, but we
8922 * handle FULLSPEED out of paranoia.
8924 * So, we can safely only restore FULLSPEED or 7, anything
8925 * else could slow the fan. Restoring AUTO is useless, at
8926 * best that's exactly what the DSDT already set (it is the
8929 * Always keep in mind that the DSDT *will* have set the
8930 * fans to what the vendor supposes is the best level. We
8931 * muck with it only to speed the fan up.
8933 if (fan_control_resume_level != 7 &&
8934 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8937 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8938 (current_level != fan_control_resume_level);
8944 pr_notice("restoring fan level to 0x%02x\n",
8945 fan_control_resume_level);
8946 rc = fan_set_level_safe(fan_control_resume_level);
8948 pr_notice("failed to restore fan level: %d\n", rc);
8952 static int fan_read(struct seq_file *m)
8956 unsigned int speed = 0;
8958 switch (fan_status_access_mode) {
8959 case TPACPI_FAN_RD_ACPI_GFAN:
8960 /* 570, 600e/x, 770e, 770x */
8961 rc = fan_get_status_safe(&status);
8965 seq_printf(m, "status:\t\t%s\n"
8967 (status != 0) ? "enabled" : "disabled", status);
8970 case TPACPI_FAN_RD_TPEC:
8971 /* all except 570, 600e/x, 770e, 770x */
8972 rc = fan_get_status_safe(&status);
8976 seq_printf(m, "status:\t\t%s\n",
8977 (status != 0) ? "enabled" : "disabled");
8979 rc = fan_get_speed(&speed);
8983 seq_printf(m, "speed:\t\t%d\n", speed);
8985 if (status & TP_EC_FAN_FULLSPEED)
8986 /* Disengaged mode takes precedence */
8987 seq_printf(m, "level:\t\tdisengaged\n");
8988 else if (status & TP_EC_FAN_AUTO)
8989 seq_printf(m, "level:\t\tauto\n");
8991 seq_printf(m, "level:\t\t%d\n", status);
8994 case TPACPI_FAN_NONE:
8996 seq_printf(m, "status:\t\tnot supported\n");
8999 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9000 seq_printf(m, "commands:\tlevel <level>");
9002 switch (fan_control_access_mode) {
9003 case TPACPI_FAN_WR_ACPI_SFAN:
9004 seq_printf(m, " (<level> is 0-7)\n");
9008 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9013 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9014 seq_printf(m, "commands:\tenable, disable\n"
9015 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9017 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9018 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9023 static int fan_write_cmd_level(const char *cmd, int *rc)
9027 if (strlencmp(cmd, "level auto") == 0)
9028 level = TP_EC_FAN_AUTO;
9029 else if ((strlencmp(cmd, "level disengaged") == 0) |
9030 (strlencmp(cmd, "level full-speed") == 0))
9031 level = TP_EC_FAN_FULLSPEED;
9032 else if (sscanf(cmd, "level %d", &level) != 1)
9035 *rc = fan_set_level_safe(level);
9037 pr_err("level command accepted for unsupported access mode %d\n",
9038 fan_control_access_mode);
9040 tpacpi_disclose_usertask("procfs fan",
9041 "set level to %d\n", level);
9046 static int fan_write_cmd_enable(const char *cmd, int *rc)
9048 if (strlencmp(cmd, "enable") != 0)
9051 *rc = fan_set_enable();
9053 pr_err("enable command accepted for unsupported access mode %d\n",
9054 fan_control_access_mode);
9056 tpacpi_disclose_usertask("procfs fan", "enable\n");
9061 static int fan_write_cmd_disable(const char *cmd, int *rc)
9063 if (strlencmp(cmd, "disable") != 0)
9066 *rc = fan_set_disable();
9068 pr_err("disable command accepted for unsupported access mode %d\n",
9069 fan_control_access_mode);
9071 tpacpi_disclose_usertask("procfs fan", "disable\n");
9076 static int fan_write_cmd_speed(const char *cmd, int *rc)
9081 * Support speed <low> <medium> <high> ? */
9083 if (sscanf(cmd, "speed %d", &speed) != 1)
9086 *rc = fan_set_speed(speed);
9088 pr_err("speed command accepted for unsupported access mode %d\n",
9089 fan_control_access_mode);
9091 tpacpi_disclose_usertask("procfs fan",
9092 "set speed to %d\n", speed);
9097 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9101 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9104 if (interval < 0 || interval > 120)
9107 fan_watchdog_maxinterval = interval;
9108 tpacpi_disclose_usertask("procfs fan",
9109 "set watchdog timer to %d\n",
9116 static int fan_write(char *buf)
9121 while (!rc && (cmd = next_cmd(&buf))) {
9122 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9123 fan_write_cmd_level(cmd, &rc)) &&
9124 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9125 (fan_write_cmd_enable(cmd, &rc) ||
9126 fan_write_cmd_disable(cmd, &rc) ||
9127 fan_write_cmd_watchdog(cmd, &rc))) &&
9128 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9129 fan_write_cmd_speed(cmd, &rc))
9133 fan_watchdog_reset();
9139 static struct ibm_struct fan_driver_data = {
9144 .suspend = fan_suspend,
9145 .resume = fan_resume,
9148 /*************************************************************************
9149 * Mute LED subdriver
9153 struct tp_led_table {
9160 static struct tp_led_table led_tables[] = {
9161 [TPACPI_LED_MUTE] = {
9166 [TPACPI_LED_MICMUTE] = {
9173 static int mute_led_on_off(struct tp_led_table *t, bool state)
9178 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9179 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9183 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9184 state ? t->on_value : t->off_value))
9191 int tpacpi_led_set(int whichled, bool on)
9193 struct tp_led_table *t;
9195 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9198 t = &led_tables[whichled];
9199 if (t->state < 0 || t->state == on)
9201 return mute_led_on_off(t, on);
9203 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9205 static int mute_led_init(struct ibm_init_struct *iibm)
9210 for (i = 0; i < TPACPI_LED_MAX; i++) {
9211 struct tp_led_table *t = &led_tables[i];
9212 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9213 mute_led_on_off(t, false);
9220 static void mute_led_exit(void)
9224 for (i = 0; i < TPACPI_LED_MAX; i++)
9225 tpacpi_led_set(i, false);
9228 static void mute_led_resume(void)
9232 for (i = 0; i < TPACPI_LED_MAX; i++) {
9233 struct tp_led_table *t = &led_tables[i];
9235 mute_led_on_off(t, t->state);
9239 static struct ibm_struct mute_led_driver_data = {
9241 .exit = mute_led_exit,
9242 .resume = mute_led_resume,
9246 * Battery Wear Control Driver
9252 #define GET_START "BCTG"
9253 #define SET_START "BCCS"
9254 #define GET_STOP "BCSG"
9255 #define SET_STOP "BCSS"
9257 #define START_ATTR "charge_start_threshold"
9258 #define STOP_ATTR "charge_stop_threshold"
9267 /* Error condition bit */
9268 METHOD_ERR = BIT(31),
9272 /* This is used in the get/set helpers */
9277 struct tpacpi_battery_data {
9284 struct tpacpi_battery_driver_data {
9285 struct tpacpi_battery_data batteries[3];
9286 int individual_addressing;
9289 static struct tpacpi_battery_driver_data battery_info;
9291 /* ACPI helpers/functions/probes */
9294 * This evaluates a ACPI method call specific to the battery
9295 * ACPI extension. The specifics are that an error is marked
9296 * in the 32rd bit of the response, so we just check that here.
9298 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9302 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9303 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9306 if (response & METHOD_ERR) {
9307 acpi_handle_err(hkey_handle,
9308 "%s evaluated but flagged as error", method);
9315 static int tpacpi_battery_get(int what, int battery, int *ret)
9318 case THRESHOLD_START:
9319 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9322 /* The value is in the low 8 bits of the response */
9325 case THRESHOLD_STOP:
9326 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9328 /* Value is in lower 8 bits */
9331 * On the stop value, if we return 0 that
9332 * does not make any sense. 0 means Default, which
9333 * means that charging stops at 100%, so we return
9340 pr_crit("wrong parameter: %d", what);
9345 static int tpacpi_battery_set(int what, int battery, int value)
9348 /* The first 8 bits are the value of the threshold */
9350 /* The battery ID is in bits 8-9, 2 bits */
9351 param |= battery << 8;
9354 case THRESHOLD_START:
9355 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9356 pr_err("failed to set charge threshold on battery %d",
9361 case THRESHOLD_STOP:
9362 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9363 pr_err("failed to set stop threshold: %d", battery);
9368 pr_crit("wrong parameter: %d", what);
9373 static int tpacpi_battery_probe(int battery)
9377 memset(&battery_info.batteries[battery], 0,
9378 sizeof(battery_info.batteries[battery]));
9381 * 1) Get the current start threshold
9382 * 2) Check for support
9383 * 3) Get the current stop threshold
9384 * 4) Check for support
9386 if (acpi_has_method(hkey_handle, GET_START)) {
9387 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9388 pr_err("Error probing battery %d\n", battery);
9391 /* Individual addressing is in bit 9 */
9393 battery_info.individual_addressing = true;
9394 /* Support is marked in bit 8 */
9396 battery_info.batteries[battery].start_support = 1;
9399 if (tpacpi_battery_get(THRESHOLD_START, battery,
9400 &battery_info.batteries[battery].charge_start)) {
9401 pr_err("Error probing battery %d\n", battery);
9405 if (acpi_has_method(hkey_handle, GET_STOP)) {
9406 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9407 pr_err("Error probing battery stop; %d\n", battery);
9410 /* Support is marked in bit 8 */
9412 battery_info.batteries[battery].stop_support = 1;
9415 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9416 &battery_info.batteries[battery].charge_stop)) {
9417 pr_err("Error probing battery stop: %d\n", battery);
9421 pr_info("battery %d registered (start %d, stop %d)",
9423 battery_info.batteries[battery].charge_start,
9424 battery_info.batteries[battery].charge_stop);
9429 /* General helper functions */
9431 static int tpacpi_battery_get_id(const char *battery_name)
9434 if (strcmp(battery_name, "BAT0") == 0 ||
9435 tp_features.battery_force_primary)
9437 if (strcmp(battery_name, "BAT1") == 0)
9438 return BAT_SECONDARY;
9440 * If for some reason the battery is not BAT0 nor is it
9441 * BAT1, we will assume it's the default, first battery,
9444 pr_warn("unknown battery %s, assuming primary", battery_name);
9448 /* sysfs interface */
9450 static ssize_t tpacpi_battery_store(int what,
9452 const char *buf, size_t count)
9454 struct power_supply *supply = to_power_supply(dev);
9455 unsigned long value;
9458 * Some systems have support for more than
9459 * one battery. If that is the case,
9460 * tpacpi_battery_probe marked that addressing
9461 * them individually is supported, so we do that
9462 * based on the device struct.
9464 * On systems that are not supported, we assume
9465 * the primary as most of the ACPI calls fail
9466 * with "Any Battery" as the parameter.
9468 if (battery_info.individual_addressing)
9469 /* BAT_PRIMARY or BAT_SECONDARY */
9470 battery = tpacpi_battery_get_id(supply->desc->name);
9472 battery = BAT_PRIMARY;
9474 rval = kstrtoul(buf, 10, &value);
9479 case THRESHOLD_START:
9480 if (!battery_info.batteries[battery].start_support)
9482 /* valid values are [0, 99] */
9483 if (value < 0 || value > 99)
9485 if (value > battery_info.batteries[battery].charge_stop)
9487 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9489 battery_info.batteries[battery].charge_start = value;
9492 case THRESHOLD_STOP:
9493 if (!battery_info.batteries[battery].stop_support)
9495 /* valid values are [1, 100] */
9496 if (value < 1 || value > 100)
9498 if (value < battery_info.batteries[battery].charge_start)
9500 battery_info.batteries[battery].charge_stop = value;
9502 * When 100 is passed to stop, we need to flip
9503 * it to 0 as that the EC understands that as
9504 * "Default", which will charge to 100%
9508 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9512 pr_crit("Wrong parameter: %d", what);
9518 static ssize_t tpacpi_battery_show(int what,
9522 struct power_supply *supply = to_power_supply(dev);
9525 * Some systems have support for more than
9526 * one battery. If that is the case,
9527 * tpacpi_battery_probe marked that addressing
9528 * them individually is supported, so we;
9529 * based on the device struct.
9531 * On systems that are not supported, we assume
9532 * the primary as most of the ACPI calls fail
9533 * with "Any Battery" as the parameter.
9535 if (battery_info.individual_addressing)
9536 /* BAT_PRIMARY or BAT_SECONDARY */
9537 battery = tpacpi_battery_get_id(supply->desc->name);
9539 battery = BAT_PRIMARY;
9540 if (tpacpi_battery_get(what, battery, &ret))
9542 return sprintf(buf, "%d\n", ret);
9545 static ssize_t charge_start_threshold_show(struct device *device,
9546 struct device_attribute *attr,
9549 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9552 static ssize_t charge_stop_threshold_show(struct device *device,
9553 struct device_attribute *attr,
9556 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9559 static ssize_t charge_start_threshold_store(struct device *dev,
9560 struct device_attribute *attr,
9561 const char *buf, size_t count)
9563 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9566 static ssize_t charge_stop_threshold_store(struct device *dev,
9567 struct device_attribute *attr,
9568 const char *buf, size_t count)
9570 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9573 static DEVICE_ATTR_RW(charge_start_threshold);
9574 static DEVICE_ATTR_RW(charge_stop_threshold);
9576 static struct attribute *tpacpi_battery_attrs[] = {
9577 &dev_attr_charge_start_threshold.attr,
9578 &dev_attr_charge_stop_threshold.attr,
9582 ATTRIBUTE_GROUPS(tpacpi_battery);
9584 /* ACPI battery hooking */
9586 static int tpacpi_battery_add(struct power_supply *battery)
9588 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9590 if (tpacpi_battery_probe(batteryid))
9592 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9597 static int tpacpi_battery_remove(struct power_supply *battery)
9599 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9603 static struct acpi_battery_hook battery_hook = {
9604 .add_battery = tpacpi_battery_add,
9605 .remove_battery = tpacpi_battery_remove,
9606 .name = "ThinkPad Battery Extension",
9609 /* Subdriver init/exit */
9611 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9613 * Individual addressing is broken on models that expose the
9614 * primary battery as BAT1.
9616 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9617 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9618 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9619 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9620 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9623 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9625 memset(&battery_info, 0, sizeof(battery_info));
9627 tp_features.battery_force_primary = tpacpi_check_quirks(
9628 battery_quirk_table,
9629 ARRAY_SIZE(battery_quirk_table));
9631 battery_hook_register(&battery_hook);
9635 static void tpacpi_battery_exit(void)
9637 battery_hook_unregister(&battery_hook);
9640 static struct ibm_struct battery_driver_data = {
9642 .exit = tpacpi_battery_exit,
9645 /****************************************************************************
9646 ****************************************************************************
9650 ****************************************************************************
9651 ****************************************************************************/
9654 * HKEY event callout for other subdrivers go here
9655 * (yes, it is ugly, but it is quick, safe, and gets the job done
9657 static void tpacpi_driver_event(const unsigned int hkey_event)
9659 if (ibm_backlight_device) {
9660 switch (hkey_event) {
9661 case TP_HKEY_EV_BRGHT_UP:
9662 case TP_HKEY_EV_BRGHT_DOWN:
9663 tpacpi_brightness_notify_change();
9667 switch (hkey_event) {
9668 case TP_HKEY_EV_VOL_UP:
9669 case TP_HKEY_EV_VOL_DOWN:
9670 case TP_HKEY_EV_VOL_MUTE:
9671 volume_alsa_notify_change();
9674 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9675 enum led_brightness brightness;
9677 mutex_lock(&kbdlight_mutex);
9680 * Check the brightness actually changed, setting the brightness
9681 * through kbdlight_set_level() also triggers this event.
9683 brightness = kbdlight_sysfs_get(NULL);
9684 if (kbdlight_brightness != brightness) {
9685 kbdlight_brightness = brightness;
9686 led_classdev_notify_brightness_hw_changed(
9687 &tpacpi_led_kbdlight.led_classdev, brightness);
9690 mutex_unlock(&kbdlight_mutex);
9694 static void hotkey_driver_event(const unsigned int scancode)
9696 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9699 /* --------------------------------------------------------------------- */
9702 static struct proc_dir_entry *proc_dir;
9705 * Module and infrastructure proble, init and exit handling
9708 static bool force_load;
9710 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9711 static const char * __init str_supported(int is_supported)
9713 static char text_unsupported[] __initdata = "not supported";
9715 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9717 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9719 static void ibm_exit(struct ibm_struct *ibm)
9721 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9723 list_del_init(&ibm->all_drivers);
9725 if (ibm->flags.acpi_notify_installed) {
9726 dbg_printk(TPACPI_DBG_EXIT,
9727 "%s: acpi_remove_notify_handler\n", ibm->name);
9729 acpi_remove_notify_handler(*ibm->acpi->handle,
9731 dispatch_acpi_notify);
9732 ibm->flags.acpi_notify_installed = 0;
9735 if (ibm->flags.proc_created) {
9736 dbg_printk(TPACPI_DBG_EXIT,
9737 "%s: remove_proc_entry\n", ibm->name);
9738 remove_proc_entry(ibm->name, proc_dir);
9739 ibm->flags.proc_created = 0;
9742 if (ibm->flags.acpi_driver_registered) {
9743 dbg_printk(TPACPI_DBG_EXIT,
9744 "%s: acpi_bus_unregister_driver\n", ibm->name);
9746 acpi_bus_unregister_driver(ibm->acpi->driver);
9747 kfree(ibm->acpi->driver);
9748 ibm->acpi->driver = NULL;
9749 ibm->flags.acpi_driver_registered = 0;
9752 if (ibm->flags.init_called && ibm->exit) {
9754 ibm->flags.init_called = 0;
9757 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9760 static int __init ibm_init(struct ibm_init_struct *iibm)
9763 struct ibm_struct *ibm = iibm->data;
9764 struct proc_dir_entry *entry;
9766 BUG_ON(ibm == NULL);
9768 INIT_LIST_HEAD(&ibm->all_drivers);
9770 if (ibm->flags.experimental && !experimental)
9773 dbg_printk(TPACPI_DBG_INIT,
9774 "probing for %s\n", ibm->name);
9777 ret = iibm->init(iibm);
9779 return 0; /* probe failed */
9783 ibm->flags.init_called = 1;
9787 if (ibm->acpi->hid) {
9788 ret = register_tpacpi_subdriver(ibm);
9793 if (ibm->acpi->notify) {
9794 ret = setup_acpi_notify(ibm);
9795 if (ret == -ENODEV) {
9796 pr_notice("disabling subdriver %s\n",
9806 dbg_printk(TPACPI_DBG_INIT,
9807 "%s installed\n", ibm->name);
9810 umode_t mode = iibm->base_procfs_mode;
9816 entry = proc_create_data(ibm->name, mode, proc_dir,
9817 &dispatch_proc_fops, ibm);
9819 pr_err("unable to create proc entry %s\n", ibm->name);
9823 ibm->flags.proc_created = 1;
9826 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9831 dbg_printk(TPACPI_DBG_INIT,
9832 "%s: at error exit path with result %d\n",
9836 return (ret < 0) ? ret : 0;
9841 static char __init tpacpi_parse_fw_id(const char * const s,
9842 u32 *model, u16 *release)
9846 if (!s || strlen(s) < 8)
9849 for (i = 0; i < 8; i++)
9850 if (!((s[i] >= '0' && s[i] <= '9') ||
9851 (s[i] >= 'A' && s[i] <= 'Z')))
9855 * Most models: xxyTkkWW (#.##c)
9856 * Ancient 570/600 and -SL lacks (#.##c)
9858 if (s[3] == 'T' || s[3] == 'N') {
9859 *model = TPID(s[0], s[1]);
9860 *release = TPVER(s[4], s[5]);
9863 /* New models: xxxyTkkW (#.##c); T550 and some others */
9864 } else if (s[4] == 'T' || s[4] == 'N') {
9865 *model = TPID3(s[0], s[1], s[2]);
9866 *release = TPVER(s[5], s[6]);
9874 /* returns 0 - probe ok, or < 0 - probe error.
9875 * Probe ok doesn't mean thinkpad found.
9876 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9877 static int __must_check __init get_thinkpad_model_data(
9878 struct thinkpad_id_data *tp)
9880 const struct dmi_device *dev = NULL;
9881 char ec_fw_string[18];
9888 memset(tp, 0, sizeof(*tp));
9890 if (dmi_name_in_vendors("IBM"))
9891 tp->vendor = PCI_VENDOR_ID_IBM;
9892 else if (dmi_name_in_vendors("LENOVO"))
9893 tp->vendor = PCI_VENDOR_ID_LENOVO;
9897 s = dmi_get_system_info(DMI_BIOS_VERSION);
9898 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9899 if (s && !tp->bios_version_str)
9902 /* Really ancient ThinkPad 240X will fail this, which is fine */
9903 t = tpacpi_parse_fw_id(tp->bios_version_str,
9904 &tp->bios_model, &tp->bios_release);
9905 if (t != 'E' && t != 'C')
9909 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9910 * X32 or newer, all Z series; Some models must have an
9911 * up-to-date BIOS or they will not be detected.
9913 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9915 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9916 if (sscanf(dev->name,
9917 "IBM ThinkPad Embedded Controller -[%17c",
9918 ec_fw_string) == 1) {
9919 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9920 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
9922 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
9923 if (!tp->ec_version_str)
9926 t = tpacpi_parse_fw_id(ec_fw_string,
9927 &tp->ec_model, &tp->ec_release);
9929 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
9931 pr_notice("please report this to %s\n",
9938 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
9939 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
9940 tp->model_str = kstrdup(s, GFP_KERNEL);
9944 s = dmi_get_system_info(DMI_BIOS_VENDOR);
9945 if (s && !(strncasecmp(s, "Lenovo", 6))) {
9946 tp->model_str = kstrdup(s, GFP_KERNEL);
9952 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9953 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9954 if (s && !tp->nummodel_str)
9960 static int __init probe_for_thinkpad(void)
9967 /* It would be dangerous to run the driver in this case */
9968 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9972 * Non-ancient models have better DMI tagging, but very old models
9973 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9975 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9976 (thinkpad_id.ec_model != 0) ||
9977 tpacpi_is_fw_known();
9979 /* The EC handler is required */
9980 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
9983 pr_err("Not yet supported ThinkPad detected!\n");
9987 if (!is_thinkpad && !force_load)
9993 static void __init thinkpad_acpi_init_banner(void)
9995 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9996 pr_info("%s\n", TPACPI_URL);
9998 pr_info("ThinkPad BIOS %s, EC %s\n",
9999 (thinkpad_id.bios_version_str) ?
10000 thinkpad_id.bios_version_str : "unknown",
10001 (thinkpad_id.ec_version_str) ?
10002 thinkpad_id.ec_version_str : "unknown");
10004 BUG_ON(!thinkpad_id.vendor);
10006 if (thinkpad_id.model_str)
10007 pr_info("%s %s, model %s\n",
10008 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10009 "IBM" : ((thinkpad_id.vendor ==
10010 PCI_VENDOR_ID_LENOVO) ?
10011 "Lenovo" : "Unknown vendor"),
10012 thinkpad_id.model_str,
10013 (thinkpad_id.nummodel_str) ?
10014 thinkpad_id.nummodel_str : "unknown");
10017 /* Module init, exit, parameters */
10019 static struct ibm_init_struct ibms_init[] __initdata = {
10021 .data = &thinkpad_acpi_driver_data,
10024 .init = hotkey_init,
10025 .data = &hotkey_driver_data,
10028 .init = bluetooth_init,
10029 .data = &bluetooth_driver_data,
10033 .data = &wan_driver_data,
10037 .data = &uwb_driver_data,
10039 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
10041 .init = video_init,
10042 .base_procfs_mode = S_IRUSR,
10043 .data = &video_driver_data,
10047 .init = kbdlight_init,
10048 .data = &kbdlight_driver_data,
10051 .init = light_init,
10052 .data = &light_driver_data,
10056 .data = &cmos_driver_data,
10060 .data = &led_driver_data,
10064 .data = &beep_driver_data,
10067 .init = thermal_init,
10068 .data = &thermal_driver_data,
10071 .init = brightness_init,
10072 .data = &brightness_driver_data,
10075 .init = volume_init,
10076 .data = &volume_driver_data,
10080 .data = &fan_driver_data,
10083 .init = mute_led_init,
10084 .data = &mute_led_driver_data,
10087 .init = tpacpi_battery_init,
10088 .data = &battery_driver_data,
10092 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
10095 struct ibm_struct *ibm;
10097 if (!kp || !kp->name || !val)
10100 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10101 ibm = ibms_init[i].data;
10102 WARN_ON(ibm == NULL);
10104 if (!ibm || !ibm->name)
10107 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10108 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
10110 strcpy(ibms_init[i].param, val);
10111 strcat(ibms_init[i].param, ",");
10119 module_param(experimental, int, 0444);
10120 MODULE_PARM_DESC(experimental,
10121 "Enables experimental features when non-zero");
10123 module_param_named(debug, dbg_level, uint, 0);
10124 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
10126 module_param(force_load, bool, 0444);
10127 MODULE_PARM_DESC(force_load,
10128 "Attempts to load the driver even on a mis-identified ThinkPad when true");
10130 module_param_named(fan_control, fan_control_allowed, bool, 0444);
10131 MODULE_PARM_DESC(fan_control,
10132 "Enables setting fan parameters features when true");
10134 module_param_named(brightness_mode, brightness_mode, uint, 0444);
10135 MODULE_PARM_DESC(brightness_mode,
10136 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
10138 module_param(brightness_enable, uint, 0444);
10139 MODULE_PARM_DESC(brightness_enable,
10140 "Enables backlight control when 1, disables when 0");
10142 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
10143 module_param_named(volume_mode, volume_mode, uint, 0444);
10144 MODULE_PARM_DESC(volume_mode,
10145 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
10147 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10148 MODULE_PARM_DESC(volume_capabilities,
10149 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
10151 module_param_named(volume_control, volume_control_allowed, bool, 0444);
10152 MODULE_PARM_DESC(volume_control,
10153 "Enables software override for the console audio control when true");
10155 module_param_named(software_mute, software_mute_requested, bool, 0444);
10156 MODULE_PARM_DESC(software_mute,
10157 "Request full software mute control");
10159 /* ALSA module API parameters */
10160 module_param_named(index, alsa_index, int, 0444);
10161 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10162 module_param_named(id, alsa_id, charp, 0444);
10163 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10164 module_param_named(enable, alsa_enable, bool, 0444);
10165 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
10166 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
10168 /* The module parameter can't be read back, that's why 0 is used here */
10169 #define TPACPI_PARAM(feature) \
10170 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
10171 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
10173 TPACPI_PARAM(hotkey);
10174 TPACPI_PARAM(bluetooth);
10175 TPACPI_PARAM(video);
10176 TPACPI_PARAM(light);
10177 TPACPI_PARAM(cmos);
10179 TPACPI_PARAM(beep);
10180 TPACPI_PARAM(brightness);
10181 TPACPI_PARAM(volume);
10184 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10185 module_param(dbg_wlswemul, uint, 0444);
10186 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10187 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10188 MODULE_PARM_DESC(wlsw_state,
10189 "Initial state of the emulated WLSW switch");
10191 module_param(dbg_bluetoothemul, uint, 0444);
10192 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10193 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10194 MODULE_PARM_DESC(bluetooth_state,
10195 "Initial state of the emulated bluetooth switch");
10197 module_param(dbg_wwanemul, uint, 0444);
10198 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10199 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10200 MODULE_PARM_DESC(wwan_state,
10201 "Initial state of the emulated WWAN switch");
10203 module_param(dbg_uwbemul, uint, 0444);
10204 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10205 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10206 MODULE_PARM_DESC(uwb_state,
10207 "Initial state of the emulated UWB switch");
10210 static void thinkpad_acpi_module_exit(void)
10212 struct ibm_struct *ibm, *itmp;
10214 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10216 list_for_each_entry_safe_reverse(ibm, itmp,
10217 &tpacpi_all_drivers,
10222 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10224 if (tpacpi_inputdev) {
10225 if (tp_features.input_device_registered)
10226 input_unregister_device(tpacpi_inputdev);
10228 input_free_device(tpacpi_inputdev);
10229 kfree(hotkey_keycode_map);
10233 hwmon_device_unregister(tpacpi_hwmon);
10235 if (tpacpi_sensors_pdev)
10236 platform_device_unregister(tpacpi_sensors_pdev);
10238 platform_device_unregister(tpacpi_pdev);
10240 if (tp_features.sensors_pdrv_attrs_registered)
10241 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10242 if (tp_features.platform_drv_attrs_registered)
10243 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10245 if (tp_features.sensors_pdrv_registered)
10246 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10248 if (tp_features.platform_drv_registered)
10249 platform_driver_unregister(&tpacpi_pdriver);
10252 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
10255 destroy_workqueue(tpacpi_wq);
10257 kfree(thinkpad_id.bios_version_str);
10258 kfree(thinkpad_id.ec_version_str);
10259 kfree(thinkpad_id.model_str);
10260 kfree(thinkpad_id.nummodel_str);
10264 static int __init thinkpad_acpi_module_init(void)
10268 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10270 /* Driver-level probe */
10272 ret = get_thinkpad_model_data(&thinkpad_id);
10274 pr_err("unable to get DMI data: %d\n", ret);
10275 thinkpad_acpi_module_exit();
10278 ret = probe_for_thinkpad();
10280 thinkpad_acpi_module_exit();
10284 /* Driver initialization */
10286 thinkpad_acpi_init_banner();
10287 tpacpi_check_outdated_fw();
10289 TPACPI_ACPIHANDLE_INIT(ecrd);
10290 TPACPI_ACPIHANDLE_INIT(ecwr);
10292 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10294 thinkpad_acpi_module_exit();
10298 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
10300 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
10301 thinkpad_acpi_module_exit();
10305 ret = platform_driver_register(&tpacpi_pdriver);
10307 pr_err("unable to register main platform driver\n");
10308 thinkpad_acpi_module_exit();
10311 tp_features.platform_drv_registered = 1;
10313 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10315 pr_err("unable to register hwmon platform driver\n");
10316 thinkpad_acpi_module_exit();
10319 tp_features.sensors_pdrv_registered = 1;
10321 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
10323 tp_features.platform_drv_attrs_registered = 1;
10324 ret = tpacpi_create_driver_attributes(
10325 &tpacpi_hwmon_pdriver.driver);
10328 pr_err("unable to create sysfs driver attributes\n");
10329 thinkpad_acpi_module_exit();
10332 tp_features.sensors_pdrv_attrs_registered = 1;
10335 /* Device initialization */
10336 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
10338 if (IS_ERR(tpacpi_pdev)) {
10339 ret = PTR_ERR(tpacpi_pdev);
10340 tpacpi_pdev = NULL;
10341 pr_err("unable to register platform device\n");
10342 thinkpad_acpi_module_exit();
10345 tpacpi_sensors_pdev = platform_device_register_simple(
10346 TPACPI_HWMON_DRVR_NAME,
10348 if (IS_ERR(tpacpi_sensors_pdev)) {
10349 ret = PTR_ERR(tpacpi_sensors_pdev);
10350 tpacpi_sensors_pdev = NULL;
10351 pr_err("unable to register hwmon platform device\n");
10352 thinkpad_acpi_module_exit();
10355 tp_features.sensors_pdev_attrs_registered = 1;
10356 tpacpi_hwmon = hwmon_device_register_with_groups(
10357 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10359 if (IS_ERR(tpacpi_hwmon)) {
10360 ret = PTR_ERR(tpacpi_hwmon);
10361 tpacpi_hwmon = NULL;
10362 pr_err("unable to register hwmon device\n");
10363 thinkpad_acpi_module_exit();
10366 mutex_init(&tpacpi_inputdev_send_mutex);
10367 tpacpi_inputdev = input_allocate_device();
10368 if (!tpacpi_inputdev) {
10369 thinkpad_acpi_module_exit();
10372 /* Prepare input device, but don't register */
10373 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
10374 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
10375 tpacpi_inputdev->id.bustype = BUS_HOST;
10376 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
10377 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10378 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
10379 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
10382 /* Init subdriver dependencies */
10383 tpacpi_detect_brightness_capabilities();
10385 /* Init subdrivers */
10386 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10387 ret = ibm_init(&ibms_init[i]);
10388 if (ret >= 0 && *ibms_init[i].param)
10389 ret = ibms_init[i].data->write(ibms_init[i].param);
10391 thinkpad_acpi_module_exit();
10396 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10398 ret = input_register_device(tpacpi_inputdev);
10400 pr_err("unable to register input device\n");
10401 thinkpad_acpi_module_exit();
10404 tp_features.input_device_registered = 1;
10410 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10413 * This will autoload the driver in almost every ThinkPad
10414 * in widespread use.
10416 * Only _VERY_ old models, like the 240, 240x and 570 lack
10417 * the HKEY event interface.
10419 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10422 * DMI matching for module autoloading
10424 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10425 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10427 * Only models listed in thinkwiki will be supported, so add yours
10428 * if it is not there yet.
10430 #define IBM_BIOS_MODULE_ALIAS(__type) \
10431 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10433 /* Ancient thinkpad BIOSes have to be identified by
10434 * BIOS type or model number, and there are far less
10435 * BIOS types than model numbers... */
10436 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
10440 MODULE_DESCRIPTION(TPACPI_DESC);
10441 MODULE_VERSION(TPACPI_VERSION);
10442 MODULE_LICENSE("GPL");
10444 module_init(thinkpad_acpi_module_init);
10445 module_exit(thinkpad_acpi_module_exit);