2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.25"
27 #define TPACPI_SYSFS_VERSION 0x020700
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
70 #include <linux/platform_device.h>
71 #include <linux/hwmon.h>
72 #include <linux/hwmon-sysfs.h>
73 #include <linux/input.h>
74 #include <linux/leds.h>
75 #include <linux/rfkill.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
79 #include <linux/acpi.h>
80 #include <linux/pci_ids.h>
81 #include <linux/thinkpad_acpi.h>
82 #include <sound/core.h>
83 #include <sound/control.h>
84 #include <sound/initval.h>
85 #include <asm/uaccess.h>
86 #include <acpi/video.h>
88 /* ThinkPad CMOS commands */
89 #define TP_CMOS_VOLUME_DOWN 0
90 #define TP_CMOS_VOLUME_UP 1
91 #define TP_CMOS_VOLUME_MUTE 2
92 #define TP_CMOS_BRIGHTNESS_UP 4
93 #define TP_CMOS_BRIGHTNESS_DOWN 5
94 #define TP_CMOS_THINKLIGHT_ON 12
95 #define TP_CMOS_THINKLIGHT_OFF 13
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
106 /* NVRAM bit masks */
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
123 /* Misc NVRAM-related */
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
131 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
135 #define TPACPI_HKEY_INPUT_VERSION 0x4101
137 /* ACPI \WGSV commands */
139 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
140 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
141 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
142 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
145 /* TP_ACPI_WGSV_GET_STATE bits */
147 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
148 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
149 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
150 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
151 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
152 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
153 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
154 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
155 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
156 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 enum tpacpi_hkey_event_t {
162 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
163 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
164 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
183 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
184 or port replicator */
185 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
186 dock or port replicator */
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
197 /* Key-related user-interface events */
198 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
199 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
200 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
203 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
204 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
205 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
206 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
207 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
209 /* AC-related events */
210 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
213 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
216 /****************************************************************************
220 #define TPACPI_NAME "thinkpad"
221 #define TPACPI_DESC "ThinkPad ACPI Extras"
222 #define TPACPI_FILE TPACPI_NAME "_acpi"
223 #define TPACPI_URL "http://ibm-acpi.sf.net/"
224 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
226 #define TPACPI_PROC_DIR "ibm"
227 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
228 #define TPACPI_DRVR_NAME TPACPI_FILE
229 #define TPACPI_DRVR_SHORTNAME "tpacpi"
230 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
232 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
233 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
235 #define TPACPI_MAX_ACPI_ARGS 3
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
266 struct acpi_device *device;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
275 void (*resume) (void);
276 void (*suspend) (void);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
292 struct ibm_init_struct {
295 int (*init) (struct ibm_init_struct *);
296 umode_t base_procfs_mode;
297 struct ibm_struct *data;
309 u32 bright_acpimode:1;
313 u32 fan_ctrl_status_undef:1;
315 u32 beep_needs_two_args:1;
316 u32 mixer_no_level_control:1;
317 u32 input_device_registered:1;
318 u32 platform_drv_registered:1;
319 u32 platform_drv_attrs_registered:1;
320 u32 sensors_pdrv_registered:1;
321 u32 sensors_pdrv_attrs_registered:1;
322 u32 sensors_pdev_attrs_registered:1;
323 u32 hotkey_poll_active:1;
324 u32 has_adaptive_kbd:1;
328 u16 hotkey_mask_ff:1;
329 u16 volume_ctrl_forbidden:1;
332 struct thinkpad_id_data {
333 unsigned int vendor; /* ThinkPad vendor:
334 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
336 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
337 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
339 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
341 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
344 char *model_str; /* ThinkPad T43 */
345 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
347 static struct thinkpad_id_data thinkpad_id;
350 TPACPI_LIFE_INIT = 0,
355 static int experimental;
356 static u32 dbg_level;
358 static struct workqueue_struct *tpacpi_wq;
366 /* Special LED class that can defer work */
367 struct tpacpi_led_classdev {
368 struct led_classdev led_classdev;
369 struct work_struct work;
370 enum led_status_t new_state;
374 /* brightness level capabilities */
375 static unsigned int bright_maxlvl; /* 0 = unknown */
377 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
378 static int dbg_wlswemul;
379 static bool tpacpi_wlsw_emulstate;
380 static int dbg_bluetoothemul;
381 static bool tpacpi_bluetooth_emulstate;
382 static int dbg_wwanemul;
383 static bool tpacpi_wwan_emulstate;
384 static int dbg_uwbemul;
385 static bool tpacpi_uwb_emulstate;
389 /*************************************************************************
393 #define dbg_printk(a_dbg_level, format, arg...) \
395 if (dbg_level & (a_dbg_level)) \
396 printk(KERN_DEBUG pr_fmt("%s: " format), \
400 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
401 #define vdbg_printk dbg_printk
402 static const char *str_supported(int is_supported);
404 static inline const char *str_supported(int is_supported) { return ""; }
405 #define vdbg_printk(a_dbg_level, format, arg...) \
406 do { if (0) no_printk(format, ##arg); } while (0)
409 static void tpacpi_log_usertask(const char * const what)
411 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
412 what, task_tgid_vnr(current));
415 #define tpacpi_disclose_usertask(what, format, arg...) \
417 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
418 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
419 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
420 what, task_tgid_vnr(current), ## arg); \
425 * Quirk handling helpers
427 * ThinkPad IDs and versions seen in the field so far
428 * are two-characters from the set [0-9A-Z], i.e. base 36.
430 * We use values well outside that range as specials.
433 #define TPACPI_MATCH_ANY 0xffffU
434 #define TPACPI_MATCH_UNKNOWN 0U
436 /* TPID('1', 'Y') == 0x5931 */
437 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
439 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
440 { .vendor = PCI_VENDOR_ID_IBM, \
441 .bios = TPID(__id1, __id2), \
442 .ec = TPACPI_MATCH_ANY, \
443 .quirks = (__quirk) }
445 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
446 { .vendor = PCI_VENDOR_ID_LENOVO, \
447 .bios = TPID(__id1, __id2), \
448 .ec = TPACPI_MATCH_ANY, \
449 .quirks = (__quirk) }
451 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
452 { .vendor = PCI_VENDOR_ID_LENOVO, \
453 .bios = TPACPI_MATCH_ANY, \
454 .ec = TPID(__id1, __id2), \
455 .quirks = (__quirk) }
457 struct tpacpi_quirk {
461 unsigned long quirks;
465 * tpacpi_check_quirks() - search BIOS/EC version on a list
466 * @qlist: array of &struct tpacpi_quirk
467 * @qlist_size: number of elements in @qlist
469 * Iterates over a quirks list until one is found that matches the
470 * ThinkPad's vendor, BIOS and EC model.
472 * Returns 0 if nothing matches, otherwise returns the quirks field of
473 * the matching &struct tpacpi_quirk entry.
475 * The match criteria is: vendor, ec and bios much match.
477 static unsigned long __init tpacpi_check_quirks(
478 const struct tpacpi_quirk *qlist,
479 unsigned int qlist_size)
482 if ((qlist->vendor == thinkpad_id.vendor ||
483 qlist->vendor == TPACPI_MATCH_ANY) &&
484 (qlist->bios == thinkpad_id.bios_model ||
485 qlist->bios == TPACPI_MATCH_ANY) &&
486 (qlist->ec == thinkpad_id.ec_model ||
487 qlist->ec == TPACPI_MATCH_ANY))
488 return qlist->quirks;
496 static inline bool __pure __init tpacpi_is_lenovo(void)
498 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
501 static inline bool __pure __init tpacpi_is_ibm(void)
503 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
506 /****************************************************************************
507 ****************************************************************************
509 * ACPI Helpers and device model
511 ****************************************************************************
512 ****************************************************************************/
514 /*************************************************************************
518 static acpi_handle root_handle;
519 static acpi_handle ec_handle;
521 #define TPACPI_HANDLE(object, parent, paths...) \
522 static acpi_handle object##_handle; \
523 static const acpi_handle * const object##_parent __initconst = \
525 static char *object##_paths[] __initdata = { paths }
527 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
528 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
530 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
532 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
533 "\\CMS", /* R40, R40e */
536 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
537 "^HKEY", /* R30, R31 */
538 "HKEY", /* all others */
541 /*************************************************************************
545 static int acpi_evalf(acpi_handle handle,
546 int *res, char *method, char *fmt, ...)
549 struct acpi_object_list params;
550 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
551 struct acpi_buffer result, *resultp;
552 union acpi_object out_obj;
560 pr_err("acpi_evalf() called with empty format\n");
573 params.pointer = &in_objs[0];
580 in_objs[params.count].integer.value = va_arg(ap, int);
581 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
583 /* add more types as needed */
585 pr_err("acpi_evalf() called "
586 "with invalid format character '%c'\n", c);
593 if (res_type != 'v') {
594 result.length = sizeof(out_obj);
595 result.pointer = &out_obj;
600 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
604 success = (status == AE_OK &&
605 out_obj.type == ACPI_TYPE_INTEGER);
607 *res = out_obj.integer.value;
610 success = status == AE_OK;
612 /* add more types as needed */
614 pr_err("acpi_evalf() called "
615 "with invalid format character '%c'\n", res_type);
619 if (!success && !quiet)
620 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
621 method, fmt0, acpi_format_exception(status));
626 static int acpi_ec_read(int i, u8 *p)
631 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
635 if (ec_read(i, p) < 0)
642 static int acpi_ec_write(int i, u8 v)
645 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
648 if (ec_write(i, v) < 0)
655 static int issue_thinkpad_cmos_command(int cmos_cmd)
660 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
666 /*************************************************************************
670 #define TPACPI_ACPIHANDLE_INIT(object) \
671 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
672 object##_paths, ARRAY_SIZE(object##_paths))
674 static void __init drv_acpi_handle_init(const char *name,
675 acpi_handle *handle, const acpi_handle parent,
676 char **paths, const int num_paths)
681 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
684 for (i = 0; i < num_paths; i++) {
685 status = acpi_get_handle(parent, paths[i], handle);
686 if (ACPI_SUCCESS(status)) {
687 dbg_printk(TPACPI_DBG_INIT,
688 "Found ACPI handle %s for %s\n",
694 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
699 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
700 u32 level, void *context, void **return_value)
702 struct acpi_device *dev;
703 if (!strcmp(context, "video")) {
704 if (acpi_bus_get_device(handle, &dev))
706 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
710 *(acpi_handle *)return_value = handle;
712 return AE_CTRL_TERMINATE;
715 static void __init tpacpi_acpi_handle_locate(const char *name,
720 acpi_handle device_found;
722 BUG_ON(!name || !handle);
723 vdbg_printk(TPACPI_DBG_INIT,
724 "trying to locate ACPI handle for %s, using HID %s\n",
725 name, hid ? hid : "NULL");
727 memset(&device_found, 0, sizeof(device_found));
728 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
729 (void *)name, &device_found);
733 if (ACPI_SUCCESS(status)) {
734 *handle = device_found;
735 dbg_printk(TPACPI_DBG_INIT,
736 "Found ACPI handle for %s\n", name);
738 vdbg_printk(TPACPI_DBG_INIT,
739 "Could not locate an ACPI handle for %s: %s\n",
740 name, acpi_format_exception(status));
744 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
746 struct ibm_struct *ibm = data;
748 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
751 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
754 ibm->acpi->notify(ibm, event);
757 static int __init setup_acpi_notify(struct ibm_struct *ibm)
764 if (!*ibm->acpi->handle)
767 vdbg_printk(TPACPI_DBG_INIT,
768 "setting up ACPI notify for %s\n", ibm->name);
770 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
772 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
776 ibm->acpi->device->driver_data = ibm;
777 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
778 TPACPI_ACPI_EVENT_PREFIX,
781 status = acpi_install_notify_handler(*ibm->acpi->handle,
782 ibm->acpi->type, dispatch_acpi_notify, ibm);
783 if (ACPI_FAILURE(status)) {
784 if (status == AE_ALREADY_EXISTS) {
785 pr_notice("another device driver is already "
786 "handling %s events\n", ibm->name);
788 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
789 ibm->name, acpi_format_exception(status));
793 ibm->flags.acpi_notify_installed = 1;
797 static int __init tpacpi_device_add(struct acpi_device *device)
802 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
806 dbg_printk(TPACPI_DBG_INIT,
807 "registering %s as an ACPI driver\n", ibm->name);
811 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
812 if (!ibm->acpi->driver) {
813 pr_err("failed to allocate memory for ibm->acpi->driver\n");
817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
818 ibm->acpi->driver->ids = ibm->acpi->hid;
820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
822 rc = acpi_bus_register_driver(ibm->acpi->driver);
824 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
829 ibm->flags.acpi_driver_registered = 1;
835 /****************************************************************************
836 ****************************************************************************
840 ****************************************************************************
841 ****************************************************************************/
843 static int dispatch_proc_show(struct seq_file *m, void *v)
845 struct ibm_struct *ibm = m->private;
847 if (!ibm || !ibm->read)
852 static int dispatch_proc_open(struct inode *inode, struct file *file)
854 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
857 static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
861 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
865 if (!ibm || !ibm->write)
867 if (count > PAGE_SIZE - 2)
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
874 if (copy_from_user(kernbuf, userbuf, count)) {
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
890 static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
895 .release = single_release,
896 .write = dispatch_proc_write,
899 static char *next_cmd(char **cmds)
904 while ((end = strchr(start, ',')) && end == start)
916 /****************************************************************************
917 ****************************************************************************
919 * Device model: input, hwmon and platform
921 ****************************************************************************
922 ****************************************************************************/
924 static struct platform_device *tpacpi_pdev;
925 static struct platform_device *tpacpi_sensors_pdev;
926 static struct device *tpacpi_hwmon;
927 static struct input_dev *tpacpi_inputdev;
928 static struct mutex tpacpi_inputdev_send_mutex;
929 static LIST_HEAD(tpacpi_all_drivers);
931 #ifdef CONFIG_PM_SLEEP
932 static int tpacpi_suspend_handler(struct device *dev)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
946 static int tpacpi_resume_handler(struct device *dev)
948 struct ibm_struct *ibm, *itmp;
950 list_for_each_entry_safe(ibm, itmp,
961 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
962 tpacpi_suspend_handler, tpacpi_resume_handler);
964 static void tpacpi_shutdown_handler(struct platform_device *pdev)
966 struct ibm_struct *ibm, *itmp;
968 list_for_each_entry_safe(ibm, itmp,
976 static struct platform_driver tpacpi_pdriver = {
978 .name = TPACPI_DRVR_NAME,
981 .shutdown = tpacpi_shutdown_handler,
984 static struct platform_driver tpacpi_hwmon_pdriver = {
986 .name = TPACPI_HWMON_DRVR_NAME,
990 /*************************************************************************
991 * sysfs support helpers
994 struct attribute_set {
995 unsigned int members, max_members;
996 struct attribute_group group;
999 struct attribute_set_obj {
1000 struct attribute_set s;
1001 struct attribute *a;
1002 } __attribute__((packed));
1004 static struct attribute_set *create_attr_set(unsigned int max_members,
1007 struct attribute_set_obj *sobj;
1009 if (max_members == 0)
1012 /* Allocates space for implicit NULL at the end too */
1013 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1014 max_members * sizeof(struct attribute *),
1018 sobj->s.max_members = max_members;
1019 sobj->s.group.attrs = &sobj->a;
1020 sobj->s.group.name = name;
1025 #define destroy_attr_set(_set) \
1028 /* not multi-threaded safe, use it in a single thread per set */
1029 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1034 if (s->members >= s->max_members)
1037 s->group.attrs[s->members] = attr;
1043 static int add_many_to_attr_set(struct attribute_set *s,
1044 struct attribute **attr,
1049 for (i = 0; i < count; i++) {
1050 res = add_to_attr_set(s, attr[i]);
1058 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1060 sysfs_remove_group(kobj, &s->group);
1061 destroy_attr_set(s);
1064 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1065 sysfs_create_group(_kobj, &_attr_set->group)
1067 static int parse_strtoul(const char *buf,
1068 unsigned long max, unsigned long *value)
1072 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1073 endp = skip_spaces(endp);
1074 if (*endp || *value > max)
1080 static void tpacpi_disable_brightness_delay(void)
1082 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1083 pr_notice("ACPI backlight control delay disabled\n");
1086 static void printk_deprecated_attribute(const char * const what,
1087 const char * const details)
1089 tpacpi_log_usertask("deprecated sysfs attribute");
1090 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1091 "will be removed. %s\n",
1095 /*************************************************************************
1096 * rfkill and radio control support helpers
1100 * ThinkPad-ACPI firmware handling model:
1102 * WLSW (master wireless switch) is event-driven, and is common to all
1103 * firmware-controlled radios. It cannot be controlled, just monitored,
1104 * as expected. It overrides all radio state in firmware
1106 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1107 * (TODO: verify how WLSW interacts with the returned radio state).
1109 * The only time there are shadow radio state changes, is when
1110 * masked-off hotkeys are used.
1114 * Internal driver API for radio state:
1116 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1117 * bool: true means radio blocked (off)
1119 enum tpacpi_rfkill_state {
1120 TPACPI_RFK_RADIO_OFF = 0,
1124 /* rfkill switches */
1125 enum tpacpi_rfk_id {
1126 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1127 TPACPI_RFK_WWAN_SW_ID,
1128 TPACPI_RFK_UWB_SW_ID,
1132 static const char *tpacpi_rfkill_names[] = {
1133 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1134 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1135 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1136 [TPACPI_RFK_SW_MAX] = NULL
1139 /* ThinkPad-ACPI rfkill subdriver */
1141 struct rfkill *rfkill;
1142 enum tpacpi_rfk_id id;
1143 const struct tpacpi_rfk_ops *ops;
1146 struct tpacpi_rfk_ops {
1147 /* firmware interface */
1148 int (*get_status)(void);
1149 int (*set_status)(const enum tpacpi_rfkill_state);
1152 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1154 /* Query FW and update rfkill sw state for a given rfkill switch */
1155 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1162 status = (tp_rfk->ops->get_status)();
1166 rfkill_set_sw_state(tp_rfk->rfkill,
1167 (status == TPACPI_RFK_RADIO_OFF));
1173 * Sync the HW-blocking state of all rfkill switches,
1174 * do notice it causes the rfkill core to schedule uevents
1176 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1179 struct tpacpi_rfk *tp_rfk;
1181 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1182 tp_rfk = tpacpi_rfkill_switches[i];
1184 if (rfkill_set_hw_state(tp_rfk->rfkill,
1186 /* ignore -- we track sw block */
1192 /* Call to get the WLSW state from the firmware */
1193 static int hotkey_get_wlsw(void);
1195 /* Call to query WLSW state and update all rfkill switches */
1196 static bool tpacpi_rfk_check_hwblock_state(void)
1198 int res = hotkey_get_wlsw();
1201 /* When unknown or unsupported, we have to assume it is unblocked */
1205 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1206 tpacpi_rfk_update_hwblock_state(hw_blocked);
1211 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1213 struct tpacpi_rfk *tp_rfk = data;
1216 dbg_printk(TPACPI_DBG_RFKILL,
1217 "request to change radio state to %s\n",
1218 blocked ? "blocked" : "unblocked");
1220 /* try to set radio state */
1221 res = (tp_rfk->ops->set_status)(blocked ?
1222 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1224 /* and update the rfkill core with whatever the FW really did */
1225 tpacpi_rfk_update_swstate(tp_rfk);
1227 return (res < 0) ? res : 0;
1230 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1231 .set_block = tpacpi_rfk_hook_set_block,
1234 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1235 const struct tpacpi_rfk_ops *tp_rfkops,
1236 const enum rfkill_type rfktype,
1238 const bool set_default)
1240 struct tpacpi_rfk *atp_rfk;
1242 bool sw_state = false;
1246 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1248 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1250 atp_rfk->rfkill = rfkill_alloc(name,
1253 &tpacpi_rfk_rfkill_ops,
1255 if (!atp_rfk || !atp_rfk->rfkill) {
1256 pr_err("failed to allocate memory for rfkill class\n");
1262 atp_rfk->ops = tp_rfkops;
1264 sw_status = (tp_rfkops->get_status)();
1265 if (sw_status < 0) {
1266 pr_err("failed to read initial state for %s, error %d\n",
1269 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1271 /* try to keep the initial state, since we ask the
1272 * firmware to preserve it across S5 in NVRAM */
1273 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1276 hw_state = tpacpi_rfk_check_hwblock_state();
1277 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1279 res = rfkill_register(atp_rfk->rfkill);
1281 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1282 rfkill_destroy(atp_rfk->rfkill);
1287 tpacpi_rfkill_switches[id] = atp_rfk;
1289 pr_info("rfkill switch %s: radio is %sblocked\n",
1290 name, (sw_state || hw_state) ? "" : "un");
1294 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1296 struct tpacpi_rfk *tp_rfk;
1298 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1300 tp_rfk = tpacpi_rfkill_switches[id];
1302 rfkill_unregister(tp_rfk->rfkill);
1303 rfkill_destroy(tp_rfk->rfkill);
1304 tpacpi_rfkill_switches[id] = NULL;
1309 static void printk_deprecated_rfkill_attribute(const char * const what)
1311 printk_deprecated_attribute(what,
1312 "Please switch to generic rfkill before year 2010");
1315 /* sysfs <radio> enable ------------------------------------------------ */
1316 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1317 struct device_attribute *attr,
1322 printk_deprecated_rfkill_attribute(attr->attr.name);
1324 /* This is in the ABI... */
1325 if (tpacpi_rfk_check_hwblock_state()) {
1326 status = TPACPI_RFK_RADIO_OFF;
1328 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1333 return snprintf(buf, PAGE_SIZE, "%d\n",
1334 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1337 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1338 struct device_attribute *attr,
1339 const char *buf, size_t count)
1344 printk_deprecated_rfkill_attribute(attr->attr.name);
1346 if (parse_strtoul(buf, 1, &t))
1349 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1351 /* This is in the ABI... */
1352 if (tpacpi_rfk_check_hwblock_state() && !!t)
1355 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1356 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1357 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1359 return (res < 0) ? res : count;
1362 /* procfs -------------------------------------------------------------- */
1363 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1365 if (id >= TPACPI_RFK_SW_MAX)
1366 seq_printf(m, "status:\t\tnot supported\n");
1370 /* This is in the ABI... */
1371 if (tpacpi_rfk_check_hwblock_state()) {
1372 status = TPACPI_RFK_RADIO_OFF;
1374 status = tpacpi_rfk_update_swstate(
1375 tpacpi_rfkill_switches[id]);
1380 seq_printf(m, "status:\t\t%s\n",
1381 (status == TPACPI_RFK_RADIO_ON) ?
1382 "enabled" : "disabled");
1383 seq_printf(m, "commands:\tenable, disable\n");
1389 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1395 if (id >= TPACPI_RFK_SW_MAX)
1398 while ((cmd = next_cmd(&buf))) {
1399 if (strlencmp(cmd, "enable") == 0)
1400 status = TPACPI_RFK_RADIO_ON;
1401 else if (strlencmp(cmd, "disable") == 0)
1402 status = TPACPI_RFK_RADIO_OFF;
1408 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1409 (status == TPACPI_RFK_RADIO_ON) ?
1410 "enable" : "disable",
1411 tpacpi_rfkill_names[id]);
1412 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1413 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1419 /*************************************************************************
1420 * thinkpad-acpi driver attributes
1423 /* interface_version --------------------------------------------------- */
1424 static ssize_t tpacpi_driver_interface_version_show(
1425 struct device_driver *drv,
1428 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1431 static DRIVER_ATTR(interface_version, S_IRUGO,
1432 tpacpi_driver_interface_version_show, NULL);
1434 /* debug_level --------------------------------------------------------- */
1435 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1438 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1441 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1442 const char *buf, size_t count)
1446 if (parse_strtoul(buf, 0xffff, &t))
1454 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1455 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1457 /* version ------------------------------------------------------------- */
1458 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1461 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1462 TPACPI_DESC, TPACPI_VERSION);
1465 static DRIVER_ATTR(version, S_IRUGO,
1466 tpacpi_driver_version_show, NULL);
1468 /* --------------------------------------------------------------------- */
1470 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1472 /* wlsw_emulstate ------------------------------------------------------ */
1473 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1476 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1479 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1480 const char *buf, size_t count)
1484 if (parse_strtoul(buf, 1, &t))
1487 if (tpacpi_wlsw_emulstate != !!t) {
1488 tpacpi_wlsw_emulstate = !!t;
1489 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1495 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1496 tpacpi_driver_wlsw_emulstate_show,
1497 tpacpi_driver_wlsw_emulstate_store);
1499 /* bluetooth_emulstate ------------------------------------------------- */
1500 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1501 struct device_driver *drv,
1504 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1507 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1508 struct device_driver *drv,
1509 const char *buf, size_t count)
1513 if (parse_strtoul(buf, 1, &t))
1516 tpacpi_bluetooth_emulstate = !!t;
1521 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1522 tpacpi_driver_bluetooth_emulstate_show,
1523 tpacpi_driver_bluetooth_emulstate_store);
1525 /* wwan_emulstate ------------------------------------------------- */
1526 static ssize_t tpacpi_driver_wwan_emulstate_show(
1527 struct device_driver *drv,
1530 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1533 static ssize_t tpacpi_driver_wwan_emulstate_store(
1534 struct device_driver *drv,
1535 const char *buf, size_t count)
1539 if (parse_strtoul(buf, 1, &t))
1542 tpacpi_wwan_emulstate = !!t;
1547 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1548 tpacpi_driver_wwan_emulstate_show,
1549 tpacpi_driver_wwan_emulstate_store);
1551 /* uwb_emulstate ------------------------------------------------- */
1552 static ssize_t tpacpi_driver_uwb_emulstate_show(
1553 struct device_driver *drv,
1556 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1559 static ssize_t tpacpi_driver_uwb_emulstate_store(
1560 struct device_driver *drv,
1561 const char *buf, size_t count)
1565 if (parse_strtoul(buf, 1, &t))
1568 tpacpi_uwb_emulstate = !!t;
1573 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1574 tpacpi_driver_uwb_emulstate_show,
1575 tpacpi_driver_uwb_emulstate_store);
1578 /* --------------------------------------------------------------------- */
1580 static struct driver_attribute *tpacpi_driver_attributes[] = {
1581 &driver_attr_debug_level, &driver_attr_version,
1582 &driver_attr_interface_version,
1585 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1591 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1592 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1596 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1597 if (!res && dbg_wlswemul)
1598 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1599 if (!res && dbg_bluetoothemul)
1600 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1601 if (!res && dbg_wwanemul)
1602 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1603 if (!res && dbg_uwbemul)
1604 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1610 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1614 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1615 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1617 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1618 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1619 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1620 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1621 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1625 /*************************************************************************
1630 * Table of recommended minimum BIOS versions
1632 * Reasons for listing:
1633 * 1. Stable BIOS, listed because the unknown amount of
1634 * bugs and bad ACPI behaviour on older versions
1636 * 2. BIOS or EC fw with known bugs that trigger on Linux
1638 * 3. BIOS with known reduced functionality in older versions
1640 * We recommend the latest BIOS and EC version.
1641 * We only support the latest BIOS and EC fw version as a rule.
1643 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1644 * Information from users in ThinkWiki
1646 * WARNING: we use this table also to detect that the machine is
1647 * a ThinkPad in some cases, so don't remove entries lightly.
1650 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1651 { .vendor = (__v), \
1652 .bios = TPID(__id1, __id2), \
1653 .ec = TPACPI_MATCH_ANY, \
1654 .quirks = TPACPI_MATCH_ANY << 16 \
1655 | (__bv1) << 8 | (__bv2) }
1657 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1658 __eid, __ev1, __ev2) \
1659 { .vendor = (__v), \
1660 .bios = TPID(__bid1, __bid2), \
1662 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1663 | (__bv1) << 8 | (__bv2) }
1665 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1666 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1668 /* Outdated IBM BIOSes often lack the EC id string */
1669 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1670 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1671 __bv1, __bv2, TPID(__id1, __id2), \
1673 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1674 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1677 /* Outdated IBM BIOSes often lack the EC id string */
1678 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1679 __eid1, __eid2, __ev1, __ev2) \
1680 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1681 __bv1, __bv2, TPID(__eid1, __eid2), \
1683 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1684 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1687 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1688 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1690 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1691 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1692 __bv1, __bv2, TPID(__id1, __id2), \
1695 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1698 __bv1, __bv2, TPID(__eid1, __eid2), \
1701 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1702 /* Numeric models ------------------ */
1703 /* FW MODEL BIOS VERS */
1704 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1705 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1706 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1707 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1708 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1709 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1710 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1711 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1712 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1714 /* A-series ------------------------- */
1715 /* FW MODEL BIOS VERS EC VERS */
1716 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1717 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1718 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1719 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1720 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1721 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1722 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1723 TPV_QI0('1', '3', '2', '0'), /* A22m */
1724 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1725 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1726 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1728 /* G-series ------------------------- */
1729 /* FW MODEL BIOS VERS */
1730 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1731 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1733 /* R-series, T-series --------------- */
1734 /* FW MODEL BIOS VERS EC VERS */
1735 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1736 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1737 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1738 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1739 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1740 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1741 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1742 T40/p, T41/p, T42/p (1) */
1743 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1744 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1745 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1746 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1748 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1749 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1750 TPV_QI0('1', '6', '3', '2'), /* T22 */
1751 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1752 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1753 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1755 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1756 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1757 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1759 /* BIOS FW BIOS VERS EC FW EC VERS */
1760 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1761 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1763 /* X-series ------------------------- */
1764 /* FW MODEL BIOS VERS EC VERS */
1765 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1766 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1767 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1768 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1769 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1770 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1771 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1773 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1774 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1776 /* (0) - older versions lack DMI EC fw string and functionality */
1777 /* (1) - older versions known to lack functionality */
1788 static void __init tpacpi_check_outdated_fw(void)
1790 unsigned long fwvers;
1791 u16 ec_version, bios_version;
1793 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1794 ARRAY_SIZE(tpacpi_bios_version_qtable));
1799 bios_version = fwvers & 0xffffU;
1800 ec_version = (fwvers >> 16) & 0xffffU;
1802 /* note that unknown versions are set to 0x0000 and we use that */
1803 if ((bios_version > thinkpad_id.bios_release) ||
1804 (ec_version > thinkpad_id.ec_release &&
1805 ec_version != TPACPI_MATCH_ANY)) {
1807 * The changelogs would let us track down the exact
1808 * reason, but it is just too much of a pain to track
1809 * it. We only list BIOSes that are either really
1810 * broken, or really stable to begin with, so it is
1811 * best if the user upgrades the firmware anyway.
1813 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1814 pr_warn("WARNING: This firmware may be missing critical bug "
1815 "fixes and/or important features\n");
1819 static bool __init tpacpi_is_fw_known(void)
1821 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1822 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1825 /****************************************************************************
1826 ****************************************************************************
1830 ****************************************************************************
1831 ****************************************************************************/
1833 /*************************************************************************
1834 * thinkpad-acpi metadata subdriver
1837 static int thinkpad_acpi_driver_read(struct seq_file *m)
1839 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1840 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1844 static struct ibm_struct thinkpad_acpi_driver_data = {
1846 .read = thinkpad_acpi_driver_read,
1849 /*************************************************************************
1854 * ThinkPad firmware event model
1856 * The ThinkPad firmware has two main event interfaces: normal ACPI
1857 * notifications (which follow the ACPI standard), and a private event
1860 * The private event interface also issues events for the hotkeys. As
1861 * the driver gained features, the event handling code ended up being
1862 * built around the hotkey subdriver. This will need to be refactored
1863 * to a more formal event API eventually.
1865 * Some "hotkeys" are actually supposed to be used as event reports,
1866 * such as "brightness has changed", "volume has changed", depending on
1867 * the ThinkPad model and how the firmware is operating.
1869 * Unlike other classes, hotkey-class events have mask/unmask control on
1870 * non-ancient firmware. However, how it behaves changes a lot with the
1871 * firmware model and version.
1874 enum { /* hot key scan codes (derived from ACPI DSDT) */
1875 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1876 TP_ACPI_HOTKEYSCAN_FNF2,
1877 TP_ACPI_HOTKEYSCAN_FNF3,
1878 TP_ACPI_HOTKEYSCAN_FNF4,
1879 TP_ACPI_HOTKEYSCAN_FNF5,
1880 TP_ACPI_HOTKEYSCAN_FNF6,
1881 TP_ACPI_HOTKEYSCAN_FNF7,
1882 TP_ACPI_HOTKEYSCAN_FNF8,
1883 TP_ACPI_HOTKEYSCAN_FNF9,
1884 TP_ACPI_HOTKEYSCAN_FNF10,
1885 TP_ACPI_HOTKEYSCAN_FNF11,
1886 TP_ACPI_HOTKEYSCAN_FNF12,
1887 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1888 TP_ACPI_HOTKEYSCAN_FNINSERT,
1889 TP_ACPI_HOTKEYSCAN_FNDELETE,
1890 TP_ACPI_HOTKEYSCAN_FNHOME,
1891 TP_ACPI_HOTKEYSCAN_FNEND,
1892 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1893 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1894 TP_ACPI_HOTKEYSCAN_FNSPACE,
1895 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1896 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1897 TP_ACPI_HOTKEYSCAN_MUTE,
1898 TP_ACPI_HOTKEYSCAN_THINKPAD,
1899 TP_ACPI_HOTKEYSCAN_UNK1,
1900 TP_ACPI_HOTKEYSCAN_UNK2,
1901 TP_ACPI_HOTKEYSCAN_UNK3,
1902 TP_ACPI_HOTKEYSCAN_UNK4,
1903 TP_ACPI_HOTKEYSCAN_UNK5,
1904 TP_ACPI_HOTKEYSCAN_UNK6,
1905 TP_ACPI_HOTKEYSCAN_UNK7,
1906 TP_ACPI_HOTKEYSCAN_UNK8,
1908 TP_ACPI_HOTKEYSCAN_MUTE2,
1909 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1910 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1911 TP_ACPI_HOTKEYSCAN_CLOUD,
1912 TP_ACPI_HOTKEYSCAN_UNK9,
1913 TP_ACPI_HOTKEYSCAN_VOICE,
1914 TP_ACPI_HOTKEYSCAN_UNK10,
1915 TP_ACPI_HOTKEYSCAN_GESTURES,
1916 TP_ACPI_HOTKEYSCAN_UNK11,
1917 TP_ACPI_HOTKEYSCAN_UNK12,
1918 TP_ACPI_HOTKEYSCAN_UNK13,
1919 TP_ACPI_HOTKEYSCAN_CONFIG,
1920 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1921 TP_ACPI_HOTKEYSCAN_RELOAD,
1922 TP_ACPI_HOTKEYSCAN_BACK,
1923 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1924 TP_ACPI_HOTKEYSCAN_MIC_UP,
1925 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1926 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1927 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1929 /* Hotkey keymap size */
1930 TPACPI_HOTKEY_MAP_LEN
1933 enum { /* Keys/events available through NVRAM polling */
1934 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1935 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1938 enum { /* Positions of some of the keys in hotkey masks */
1939 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1940 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1941 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1942 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1943 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1944 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1945 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1946 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1947 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1948 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1949 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1952 enum { /* NVRAM to ACPI HKEY group map */
1953 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1954 TP_ACPI_HKEY_ZOOM_MASK |
1955 TP_ACPI_HKEY_DISPSWTCH_MASK |
1956 TP_ACPI_HKEY_HIBERNATE_MASK,
1957 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1958 TP_ACPI_HKEY_BRGHTDWN_MASK,
1959 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1960 TP_ACPI_HKEY_VOLDWN_MASK |
1961 TP_ACPI_HKEY_MUTE_MASK,
1964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1965 struct tp_nvram_state {
1966 u16 thinkpad_toggle:1;
1968 u16 display_toggle:1;
1969 u16 thinklight_toggle:1;
1970 u16 hibernate_toggle:1;
1971 u16 displayexp_toggle:1;
1972 u16 display_state:1;
1973 u16 brightness_toggle:1;
1974 u16 volume_toggle:1;
1977 u8 brightness_level;
1981 /* kthread for the hotkey poller */
1982 static struct task_struct *tpacpi_hotkey_task;
1985 * Acquire mutex to write poller control variables as an
1988 * Increment hotkey_config_change when changing them if you
1989 * want the kthread to forget old state.
1991 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1993 static struct mutex hotkey_thread_data_mutex;
1994 static unsigned int hotkey_config_change;
1997 * hotkey poller control variables
1999 * Must be atomic or readers will also need to acquire mutex
2001 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2002 * should be used only when the changes need to be taken as
2003 * a block, OR when one needs to force the kthread to forget
2006 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2007 static unsigned int hotkey_poll_freq = 10; /* Hz */
2009 #define HOTKEY_CONFIG_CRITICAL_START \
2011 mutex_lock(&hotkey_thread_data_mutex); \
2012 hotkey_config_change++; \
2014 #define HOTKEY_CONFIG_CRITICAL_END \
2015 mutex_unlock(&hotkey_thread_data_mutex);
2017 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2019 #define hotkey_source_mask 0U
2020 #define HOTKEY_CONFIG_CRITICAL_START
2021 #define HOTKEY_CONFIG_CRITICAL_END
2023 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2025 static struct mutex hotkey_mutex;
2027 static enum { /* Reasons for waking up */
2028 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2029 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2030 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2031 } hotkey_wakeup_reason;
2033 static int hotkey_autosleep_ack;
2035 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2036 static u32 hotkey_all_mask; /* all events supported in fw */
2037 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2038 static u32 hotkey_reserved_mask; /* events better left disabled */
2039 static u32 hotkey_driver_mask; /* events needed by the driver */
2040 static u32 hotkey_user_mask; /* events visible to userspace */
2041 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2043 static u16 *hotkey_keycode_map;
2045 static struct attribute_set *hotkey_dev_attributes;
2047 static void tpacpi_driver_event(const unsigned int hkey_event);
2048 static void hotkey_driver_event(const unsigned int scancode);
2049 static void hotkey_poll_setup(const bool may_warn);
2051 /* HKEY.MHKG() return bits */
2052 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2054 static int hotkey_get_wlsw(void)
2058 if (!tp_features.hotkey_wlsw)
2061 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2063 return (tpacpi_wlsw_emulstate) ?
2064 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2067 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2070 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2073 static int hotkey_get_tablet_mode(int *status)
2077 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2080 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2085 * Reads current event mask from firmware, and updates
2086 * hotkey_acpi_mask accordingly. Also resets any bits
2087 * from hotkey_user_mask that are unavailable to be
2088 * delivered (shadow requirement of the userspace ABI).
2090 * Call with hotkey_mutex held
2092 static int hotkey_mask_get(void)
2094 if (tp_features.hotkey_mask) {
2097 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2100 hotkey_acpi_mask = m;
2102 /* no mask support doesn't mean no event support... */
2103 hotkey_acpi_mask = hotkey_all_mask;
2106 /* sync userspace-visible mask */
2107 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2112 static void hotkey_mask_warn_incomplete_mask(void)
2114 /* log only what the user can fix... */
2115 const u32 wantedmask = hotkey_driver_mask &
2116 ~(hotkey_acpi_mask | hotkey_source_mask) &
2117 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2120 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2124 * Set the firmware mask when supported
2126 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2128 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2130 * Call with hotkey_mutex held
2132 static int hotkey_mask_set(u32 mask)
2137 const u32 fwmask = mask & ~hotkey_source_mask;
2139 if (tp_features.hotkey_mask) {
2140 for (i = 0; i < 32; i++) {
2141 if (!acpi_evalf(hkey_handle,
2142 NULL, "MHKM", "vdd", i + 1,
2143 !!(mask & (1 << i)))) {
2151 * We *must* make an inconditional call to hotkey_mask_get to
2152 * refresh hotkey_acpi_mask and update hotkey_user_mask
2154 * Take the opportunity to also log when we cannot _enable_
2157 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2158 pr_notice("asked for hotkey mask 0x%08x, but "
2159 "firmware forced it to 0x%08x\n",
2160 fwmask, hotkey_acpi_mask);
2163 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2164 hotkey_mask_warn_incomplete_mask();
2170 * Sets hotkey_user_mask and tries to set the firmware mask
2172 * Call with hotkey_mutex held
2174 static int hotkey_user_mask_set(const u32 mask)
2178 /* Give people a chance to notice they are doing something that
2179 * is bound to go boom on their users sooner or later */
2180 if (!tp_warned.hotkey_mask_ff &&
2181 (mask == 0xffff || mask == 0xffffff ||
2182 mask == 0xffffffff)) {
2183 tp_warned.hotkey_mask_ff = 1;
2184 pr_notice("setting the hotkey mask to 0x%08x is likely "
2185 "not the best way to go about it\n", mask);
2186 pr_notice("please consider using the driver defaults, "
2187 "and refer to up-to-date thinkpad-acpi "
2191 /* Try to enable what the user asked for, plus whatever we need.
2192 * this syncs everything but won't enable bits in hotkey_user_mask */
2193 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2195 /* Enable the available bits in hotkey_user_mask */
2196 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2202 * Sets the driver hotkey mask.
2204 * Can be called even if the hotkey subdriver is inactive
2206 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2210 /* Do the right thing if hotkey_init has not been called yet */
2211 if (!tp_features.hotkey) {
2212 hotkey_driver_mask = mask;
2216 mutex_lock(&hotkey_mutex);
2218 HOTKEY_CONFIG_CRITICAL_START
2219 hotkey_driver_mask = mask;
2220 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2221 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2223 HOTKEY_CONFIG_CRITICAL_END
2225 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2226 ~hotkey_source_mask);
2227 hotkey_poll_setup(true);
2229 mutex_unlock(&hotkey_mutex);
2234 static int hotkey_status_get(int *status)
2236 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2242 static int hotkey_status_set(bool enable)
2244 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2250 static void tpacpi_input_send_tabletsw(void)
2254 if (tp_features.hotkey_tablet &&
2255 !hotkey_get_tablet_mode(&state)) {
2256 mutex_lock(&tpacpi_inputdev_send_mutex);
2258 input_report_switch(tpacpi_inputdev,
2259 SW_TABLET_MODE, !!state);
2260 input_sync(tpacpi_inputdev);
2262 mutex_unlock(&tpacpi_inputdev_send_mutex);
2266 /* Do NOT call without validating scancode first */
2267 static void tpacpi_input_send_key(const unsigned int scancode)
2269 const unsigned int keycode = hotkey_keycode_map[scancode];
2271 if (keycode != KEY_RESERVED) {
2272 mutex_lock(&tpacpi_inputdev_send_mutex);
2274 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2275 input_report_key(tpacpi_inputdev, keycode, 1);
2276 input_sync(tpacpi_inputdev);
2278 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2279 input_report_key(tpacpi_inputdev, keycode, 0);
2280 input_sync(tpacpi_inputdev);
2282 mutex_unlock(&tpacpi_inputdev_send_mutex);
2286 /* Do NOT call without validating scancode first */
2287 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2289 hotkey_driver_event(scancode);
2290 if (hotkey_user_mask & (1 << scancode))
2291 tpacpi_input_send_key(scancode);
2294 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2295 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2297 /* Do NOT call without validating scancode first */
2298 static void tpacpi_hotkey_send_key(unsigned int scancode)
2300 tpacpi_input_send_key_masked(scancode);
2303 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2307 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2308 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2309 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2310 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2311 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2312 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2314 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2315 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2316 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2318 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2319 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2320 n->displayexp_toggle =
2321 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2323 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2324 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2325 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2326 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2327 n->brightness_toggle =
2328 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2330 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2331 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2332 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2333 >> TP_NVRAM_POS_LEVEL_VOLUME;
2334 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2335 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2339 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2341 if ((event_mask & (1 << __scancode)) && \
2342 oldn->__member != newn->__member) \
2343 tpacpi_hotkey_send_key(__scancode); \
2346 #define TPACPI_MAY_SEND_KEY(__scancode) \
2348 if (event_mask & (1 << __scancode)) \
2349 tpacpi_hotkey_send_key(__scancode); \
2352 static void issue_volchange(const unsigned int oldvol,
2353 const unsigned int newvol,
2354 const u32 event_mask)
2356 unsigned int i = oldvol;
2358 while (i > newvol) {
2359 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2362 while (i < newvol) {
2363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2368 static void issue_brightnesschange(const unsigned int oldbrt,
2369 const unsigned int newbrt,
2370 const u32 event_mask)
2372 unsigned int i = oldbrt;
2374 while (i > newbrt) {
2375 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2378 while (i < newbrt) {
2379 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2384 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2385 struct tp_nvram_state *newn,
2386 const u32 event_mask)
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2401 * This code is supposed to duplicate the IBM firmware behaviour:
2402 * - Pressing MUTE issues mute hotkey message, even when already mute
2403 * - Pressing Volume up/down issues volume up/down hotkey messages,
2404 * even when already at maximum or minimum volume
2405 * - The act of unmuting issues volume up/down notification,
2406 * depending which key was used to unmute
2408 * We are constrained to what the NVRAM can tell us, which is not much
2409 * and certainly not enough if more than one volume hotkey was pressed
2410 * since the last poll cycle.
2412 * Just to make our life interesting, some newer Lenovo ThinkPads have
2413 * bugs in the BIOS and may fail to update volume_toggle properly.
2418 oldn->volume_toggle != newn->volume_toggle ||
2419 oldn->volume_level != newn->volume_level) {
2420 /* recently muted, or repeated mute keypress, or
2421 * multiple presses ending in mute */
2422 issue_volchange(oldn->volume_level, newn->volume_level,
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2429 /* recently unmuted, issue 'unmute' keypress */
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2432 if (oldn->volume_level != newn->volume_level) {
2433 issue_volchange(oldn->volume_level, newn->volume_level,
2435 } else if (oldn->volume_toggle != newn->volume_toggle) {
2436 /* repeated vol up/down keypress at end of scale ? */
2437 if (newn->volume_level == 0)
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2439 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2444 /* handle brightness */
2445 if (oldn->brightness_level != newn->brightness_level) {
2446 issue_brightnesschange(oldn->brightness_level,
2447 newn->brightness_level, event_mask);
2448 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2449 /* repeated key presses that didn't change state */
2450 if (newn->brightness_level == 0)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2452 else if (newn->brightness_level >= bright_maxlvl
2453 && !tp_features.bright_unkfw)
2454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2457 #undef TPACPI_COMPARE_KEY
2458 #undef TPACPI_MAY_SEND_KEY
2464 * We track all events in hotkey_source_mask all the time, since
2465 * most of them are edge-based. We only issue those requested by
2466 * hotkey_user_mask or hotkey_driver_mask, though.
2468 static int hotkey_kthread(void *data)
2470 struct tp_nvram_state s[2] = { 0 };
2471 u32 poll_mask, event_mask;
2472 unsigned int si, so;
2474 unsigned int change_detector;
2475 unsigned int poll_freq;
2478 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2487 /* Initial state for compares */
2488 mutex_lock(&hotkey_thread_data_mutex);
2489 change_detector = hotkey_config_change;
2490 poll_mask = hotkey_source_mask;
2491 event_mask = hotkey_source_mask &
2492 (hotkey_driver_mask | hotkey_user_mask);
2493 poll_freq = hotkey_poll_freq;
2494 mutex_unlock(&hotkey_thread_data_mutex);
2495 hotkey_read_nvram(&s[so], poll_mask);
2497 while (!kthread_should_stop()) {
2499 if (likely(poll_freq))
2502 t = 100; /* should never happen... */
2504 t = msleep_interruptible(t);
2505 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2508 if (t > 0 && !was_frozen)
2511 mutex_lock(&hotkey_thread_data_mutex);
2512 if (was_frozen || hotkey_config_change != change_detector) {
2513 /* forget old state on thaw or config change */
2516 change_detector = hotkey_config_change;
2518 poll_mask = hotkey_source_mask;
2519 event_mask = hotkey_source_mask &
2520 (hotkey_driver_mask | hotkey_user_mask);
2521 poll_freq = hotkey_poll_freq;
2522 mutex_unlock(&hotkey_thread_data_mutex);
2524 if (likely(poll_mask)) {
2525 hotkey_read_nvram(&s[si], poll_mask);
2526 if (likely(si != so)) {
2527 hotkey_compare_and_issue_event(&s[so], &s[si],
2540 /* call with hotkey_mutex held */
2541 static void hotkey_poll_stop_sync(void)
2543 if (tpacpi_hotkey_task) {
2544 kthread_stop(tpacpi_hotkey_task);
2545 tpacpi_hotkey_task = NULL;
2549 /* call with hotkey_mutex held */
2550 static void hotkey_poll_setup(const bool may_warn)
2552 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2553 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2555 if (hotkey_poll_freq > 0 &&
2556 (poll_driver_mask ||
2557 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2558 if (!tpacpi_hotkey_task) {
2559 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2560 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2561 if (IS_ERR(tpacpi_hotkey_task)) {
2562 tpacpi_hotkey_task = NULL;
2563 pr_err("could not create kernel thread "
2564 "for hotkey polling\n");
2568 hotkey_poll_stop_sync();
2569 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2570 hotkey_poll_freq == 0) {
2571 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2572 "require polling, which is currently "
2574 poll_user_mask, poll_driver_mask);
2579 static void hotkey_poll_setup_safe(const bool may_warn)
2581 mutex_lock(&hotkey_mutex);
2582 hotkey_poll_setup(may_warn);
2583 mutex_unlock(&hotkey_mutex);
2586 /* call with hotkey_mutex held */
2587 static void hotkey_poll_set_freq(unsigned int freq)
2590 hotkey_poll_stop_sync();
2592 hotkey_poll_freq = freq;
2595 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2597 static void hotkey_poll_setup(const bool __unused)
2601 static void hotkey_poll_setup_safe(const bool __unused)
2605 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2607 static int hotkey_inputdev_open(struct input_dev *dev)
2609 switch (tpacpi_lifecycle) {
2610 case TPACPI_LIFE_INIT:
2611 case TPACPI_LIFE_RUNNING:
2612 hotkey_poll_setup_safe(false);
2614 case TPACPI_LIFE_EXITING:
2618 /* Should only happen if tpacpi_lifecycle is corrupt */
2623 static void hotkey_inputdev_close(struct input_dev *dev)
2625 /* disable hotkey polling when possible */
2626 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2627 !(hotkey_source_mask & hotkey_driver_mask))
2628 hotkey_poll_setup_safe(false);
2631 /* sysfs hotkey enable ------------------------------------------------- */
2632 static ssize_t hotkey_enable_show(struct device *dev,
2633 struct device_attribute *attr,
2638 printk_deprecated_attribute("hotkey_enable",
2639 "Hotkey reporting is always enabled");
2641 res = hotkey_status_get(&status);
2645 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2648 static ssize_t hotkey_enable_store(struct device *dev,
2649 struct device_attribute *attr,
2650 const char *buf, size_t count)
2654 printk_deprecated_attribute("hotkey_enable",
2655 "Hotkeys can be disabled through hotkey_mask");
2657 if (parse_strtoul(buf, 1, &t))
2666 static DEVICE_ATTR_RW(hotkey_enable);
2668 /* sysfs hotkey mask --------------------------------------------------- */
2669 static ssize_t hotkey_mask_show(struct device *dev,
2670 struct device_attribute *attr,
2673 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2676 static ssize_t hotkey_mask_store(struct device *dev,
2677 struct device_attribute *attr,
2678 const char *buf, size_t count)
2683 if (parse_strtoul(buf, 0xffffffffUL, &t))
2686 if (mutex_lock_killable(&hotkey_mutex))
2687 return -ERESTARTSYS;
2689 res = hotkey_user_mask_set(t);
2691 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2692 hotkey_poll_setup(true);
2695 mutex_unlock(&hotkey_mutex);
2697 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2699 return (res) ? res : count;
2702 static DEVICE_ATTR_RW(hotkey_mask);
2704 /* sysfs hotkey bios_enabled ------------------------------------------- */
2705 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2706 struct device_attribute *attr,
2709 return sprintf(buf, "0\n");
2712 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2714 /* sysfs hotkey bios_mask ---------------------------------------------- */
2715 static ssize_t hotkey_bios_mask_show(struct device *dev,
2716 struct device_attribute *attr,
2719 printk_deprecated_attribute("hotkey_bios_mask",
2720 "This attribute is useless.");
2721 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2724 static DEVICE_ATTR_RO(hotkey_bios_mask);
2726 /* sysfs hotkey all_mask ----------------------------------------------- */
2727 static ssize_t hotkey_all_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2731 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2732 hotkey_all_mask | hotkey_source_mask);
2735 static DEVICE_ATTR_RO(hotkey_all_mask);
2737 /* sysfs hotkey all_mask ----------------------------------------------- */
2738 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2739 struct device_attribute *attr,
2742 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2743 hotkey_adaptive_all_mask | hotkey_source_mask);
2746 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2748 /* sysfs hotkey recommended_mask --------------------------------------- */
2749 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2750 struct device_attribute *attr,
2753 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2754 (hotkey_all_mask | hotkey_source_mask)
2755 & ~hotkey_reserved_mask);
2758 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2760 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2762 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2763 static ssize_t hotkey_source_mask_show(struct device *dev,
2764 struct device_attribute *attr,
2767 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2770 static ssize_t hotkey_source_mask_store(struct device *dev,
2771 struct device_attribute *attr,
2772 const char *buf, size_t count)
2778 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2779 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2782 if (mutex_lock_killable(&hotkey_mutex))
2783 return -ERESTARTSYS;
2785 HOTKEY_CONFIG_CRITICAL_START
2786 hotkey_source_mask = t;
2787 HOTKEY_CONFIG_CRITICAL_END
2789 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2790 ~hotkey_source_mask);
2791 hotkey_poll_setup(true);
2793 /* check if events needed by the driver got disabled */
2794 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2795 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2797 mutex_unlock(&hotkey_mutex);
2800 pr_err("hotkey_source_mask: "
2801 "failed to update the firmware event mask!\n");
2804 pr_notice("hotkey_source_mask: "
2805 "some important events were disabled: 0x%04x\n",
2808 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2810 return (rc < 0) ? rc : count;
2813 static DEVICE_ATTR_RW(hotkey_source_mask);
2815 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2816 static ssize_t hotkey_poll_freq_show(struct device *dev,
2817 struct device_attribute *attr,
2820 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2823 static ssize_t hotkey_poll_freq_store(struct device *dev,
2824 struct device_attribute *attr,
2825 const char *buf, size_t count)
2829 if (parse_strtoul(buf, 25, &t))
2832 if (mutex_lock_killable(&hotkey_mutex))
2833 return -ERESTARTSYS;
2835 hotkey_poll_set_freq(t);
2836 hotkey_poll_setup(true);
2838 mutex_unlock(&hotkey_mutex);
2840 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2845 static DEVICE_ATTR_RW(hotkey_poll_freq);
2847 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2849 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2850 static ssize_t hotkey_radio_sw_show(struct device *dev,
2851 struct device_attribute *attr,
2855 res = hotkey_get_wlsw();
2859 /* Opportunistic update */
2860 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2862 return snprintf(buf, PAGE_SIZE, "%d\n",
2863 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2866 static DEVICE_ATTR_RO(hotkey_radio_sw);
2868 static void hotkey_radio_sw_notify_change(void)
2870 if (tp_features.hotkey_wlsw)
2871 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2875 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2876 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2877 struct device_attribute *attr,
2881 res = hotkey_get_tablet_mode(&s);
2885 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2888 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2890 static void hotkey_tablet_mode_notify_change(void)
2892 if (tp_features.hotkey_tablet)
2893 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2894 "hotkey_tablet_mode");
2897 /* sysfs wakeup reason (pollable) -------------------------------------- */
2898 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2899 struct device_attribute *attr,
2902 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2905 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2907 static void hotkey_wakeup_reason_notify_change(void)
2909 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2913 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2914 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2915 struct device_attribute *attr,
2918 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2921 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2922 hotkey_wakeup_hotunplug_complete_show, NULL);
2924 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2926 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2927 "wakeup_hotunplug_complete");
2930 /* sysfs adaptive kbd mode --------------------------------------------- */
2932 static int adaptive_keyboard_get_mode(void);
2933 static int adaptive_keyboard_set_mode(int new_mode);
2935 enum ADAPTIVE_KEY_MODE {
2938 WEB_CONFERENCE_MODE,
2943 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2944 struct device_attribute *attr,
2949 current_mode = adaptive_keyboard_get_mode();
2950 if (current_mode < 0)
2951 return current_mode;
2953 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2956 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2957 struct device_attribute *attr,
2958 const char *buf, size_t count)
2963 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2966 res = adaptive_keyboard_set_mode(t);
2967 return (res < 0) ? res : count;
2970 static DEVICE_ATTR_RW(adaptive_kbd_mode);
2972 static struct attribute *adaptive_kbd_attributes[] = {
2973 &dev_attr_adaptive_kbd_mode.attr,
2977 static const struct attribute_group adaptive_kbd_attr_group = {
2978 .attrs = adaptive_kbd_attributes,
2981 /* --------------------------------------------------------------------- */
2983 static struct attribute *hotkey_attributes[] __initdata = {
2984 &dev_attr_hotkey_enable.attr,
2985 &dev_attr_hotkey_bios_enabled.attr,
2986 &dev_attr_hotkey_bios_mask.attr,
2987 &dev_attr_wakeup_reason.attr,
2988 &dev_attr_wakeup_hotunplug_complete.attr,
2989 &dev_attr_hotkey_mask.attr,
2990 &dev_attr_hotkey_all_mask.attr,
2991 &dev_attr_hotkey_adaptive_all_mask.attr,
2992 &dev_attr_hotkey_recommended_mask.attr,
2993 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2994 &dev_attr_hotkey_source_mask.attr,
2995 &dev_attr_hotkey_poll_freq.attr,
3000 * Sync both the hw and sw blocking state of all switches
3002 static void tpacpi_send_radiosw_update(void)
3007 * We must sync all rfkill controllers *before* issuing any
3008 * rfkill input events, or we will race the rfkill core input
3011 * tpacpi_inputdev_send_mutex works as a synchronization point
3014 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3017 wlsw = hotkey_get_wlsw();
3019 /* Sync hw blocking state first if it is hw-blocked */
3020 if (wlsw == TPACPI_RFK_RADIO_OFF)
3021 tpacpi_rfk_update_hwblock_state(true);
3023 /* Sync hw blocking state last if it is hw-unblocked */
3024 if (wlsw == TPACPI_RFK_RADIO_ON)
3025 tpacpi_rfk_update_hwblock_state(false);
3027 /* Issue rfkill input event for WLSW switch */
3029 mutex_lock(&tpacpi_inputdev_send_mutex);
3031 input_report_switch(tpacpi_inputdev,
3032 SW_RFKILL_ALL, (wlsw > 0));
3033 input_sync(tpacpi_inputdev);
3035 mutex_unlock(&tpacpi_inputdev_send_mutex);
3039 * this can be unconditional, as we will poll state again
3040 * if userspace uses the notify to read data
3042 hotkey_radio_sw_notify_change();
3045 static void hotkey_exit(void)
3047 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3048 mutex_lock(&hotkey_mutex);
3049 hotkey_poll_stop_sync();
3050 mutex_unlock(&hotkey_mutex);
3053 if (hotkey_dev_attributes)
3054 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3056 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3057 "restoring original HKEY status and mask\n");
3058 /* yes, there is a bitwise or below, we want the
3059 * functions to be called even if one of them fail */
3060 if (((tp_features.hotkey_mask &&
3061 hotkey_mask_set(hotkey_orig_mask)) |
3062 hotkey_status_set(false)) != 0)
3063 pr_err("failed to restore hot key mask "
3064 "to BIOS defaults\n");
3067 static void __init hotkey_unmap(const unsigned int scancode)
3069 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3070 clear_bit(hotkey_keycode_map[scancode],
3071 tpacpi_inputdev->keybit);
3072 hotkey_keycode_map[scancode] = KEY_RESERVED;
3078 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3081 #define TPACPI_HK_Q_INIMASK 0x0001
3083 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3084 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3085 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3086 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3087 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3088 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3089 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3090 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3091 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3092 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3093 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3094 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3095 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3096 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3097 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3098 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3099 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3100 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3101 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3102 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3105 typedef u16 tpacpi_keymap_entry_t;
3106 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3108 static int __init hotkey_init(struct ibm_init_struct *iibm)
3110 /* Requirements for changing the default keymaps:
3112 * 1. Many of the keys are mapped to KEY_RESERVED for very
3113 * good reasons. Do not change them unless you have deep
3114 * knowledge on the IBM and Lenovo ThinkPad firmware for
3115 * the various ThinkPad models. The driver behaves
3116 * differently for KEY_RESERVED: such keys have their
3117 * hot key mask *unset* in mask_recommended, and also
3118 * in the initial hot key mask programmed into the
3119 * firmware at driver load time, which means the firm-
3120 * ware may react very differently if you change them to
3123 * 2. You must be subscribed to the linux-thinkpad and
3124 * ibm-acpi-devel mailing lists, and you should read the
3125 * list archives since 2007 if you want to change the
3126 * keymaps. This requirement exists so that you will
3127 * know the past history of problems with the thinkpad-
3128 * acpi driver keymaps, and also that you will be
3129 * listening to any bug reports;
3131 * 3. Do not send thinkpad-acpi specific patches directly to
3132 * for merging, *ever*. Send them to the linux-acpi
3133 * mailinglist for comments. Merging is to be done only
3134 * through acpi-test and the ACPI maintainer.
3136 * If the above is too much to ask, don't change the keymap.
3137 * Ask the thinkpad-acpi maintainer to do it, instead.
3141 TPACPI_KEYMAP_IBM_GENERIC = 0,
3142 TPACPI_KEYMAP_LENOVO_GENERIC,
3145 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3146 /* Generic keymap for IBM ThinkPads */
3147 [TPACPI_KEYMAP_IBM_GENERIC] = {
3148 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3149 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3150 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3151 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3153 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3154 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3155 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3156 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3158 /* brightness: firmware always reacts to them */
3159 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3160 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3162 /* Thinklight: firmware always react to it */
3163 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3165 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3166 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3168 /* Volume: firmware always react to it and reprograms
3169 * the built-in *extra* mixer. Never map it to control
3170 * another mixer by default. */
3171 KEY_RESERVED, /* 0x14: VOLUME UP */
3172 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3173 KEY_RESERVED, /* 0x16: MUTE */
3175 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3177 /* (assignments unknown, please report if found) */
3178 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3179 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3181 /* No assignments, only used for Adaptive keyboards. */
3182 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3183 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3184 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3185 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3186 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3189 /* Generic keymap for Lenovo ThinkPads */
3190 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3191 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3192 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3193 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3194 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3196 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3197 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3198 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3199 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3201 /* These should be enabled --only-- when ACPI video
3202 * is disabled (i.e. in "vendor" mode), and are handled
3203 * in a special way by the init code */
3204 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3205 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3207 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3209 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3210 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3212 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3213 * react to it and reprograms the built-in *extra* mixer.
3214 * Never map it to control another mixer by default.
3216 * T60?, T61, R60?, R61: firmware and EC tries to send
3217 * these over the regular keyboard, so these are no-ops,
3218 * but there are still weird bugs re. MUTE, so do not
3219 * change unless you get test reports from all Lenovo
3220 * models. May cause the BIOS to interfere with the
3223 KEY_RESERVED, /* 0x14: VOLUME UP */
3224 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3225 KEY_RESERVED, /* 0x16: MUTE */
3227 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3229 /* (assignments unknown, please report if found) */
3230 KEY_UNKNOWN, KEY_UNKNOWN,
3233 * The mic mute button only sends 0x1a. It does not
3234 * automatically mute the mic or change the mute light.
3236 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3238 /* (assignments unknown, please report if found) */
3241 /* Extra keys in use since the X240 / T440 / T540 */
3242 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3245 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3246 * The first item in this list is the Mute button which is
3247 * emitted with 0x103 through
3248 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3250 * We'll need to offset those by 0x20.
3252 KEY_RESERVED, /* Mute held, 0x103 */
3253 KEY_BRIGHTNESS_MIN, /* Backlight off */
3254 KEY_RESERVED, /* Clipping tool */
3255 KEY_RESERVED, /* Cloud */
3257 KEY_VOICECOMMAND, /* Voice */
3259 KEY_RESERVED, /* Gestures */
3263 KEY_CONFIG, /* Settings */
3264 KEY_RESERVED, /* New tab */
3265 KEY_REFRESH, /* Reload */
3266 KEY_BACK, /* Back */
3267 KEY_RESERVED, /* Microphone down */
3268 KEY_RESERVED, /* Microphone up */
3269 KEY_RESERVED, /* Microphone cancellation */
3270 KEY_RESERVED, /* Camera mode */
3271 KEY_RESERVED, /* Rotate display, 0x116 */
3275 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3276 /* Generic maps (fallback) */
3278 .vendor = PCI_VENDOR_ID_IBM,
3279 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3280 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3283 .vendor = PCI_VENDOR_ID_LENOVO,
3284 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3285 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3289 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3290 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3295 bool radiosw_state = false;
3296 bool tabletsw_state = false;
3298 unsigned long quirks;
3299 unsigned long keymap_id;
3301 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3302 "initializing hotkey subdriver\n");
3304 BUG_ON(!tpacpi_inputdev);
3305 BUG_ON(tpacpi_inputdev->open != NULL ||
3306 tpacpi_inputdev->close != NULL);
3308 TPACPI_ACPIHANDLE_INIT(hkey);
3309 mutex_init(&hotkey_mutex);
3311 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3312 mutex_init(&hotkey_thread_data_mutex);
3315 /* hotkey not supported on 570 */
3316 tp_features.hotkey = hkey_handle != NULL;
3318 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3320 str_supported(tp_features.hotkey));
3322 if (!tp_features.hotkey)
3325 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3326 ARRAY_SIZE(tpacpi_hotkey_qtable));
3328 tpacpi_disable_brightness_delay();
3330 /* MUST have enough space for all attributes to be added to
3331 * hotkey_dev_attributes */
3332 hotkey_dev_attributes = create_attr_set(
3333 ARRAY_SIZE(hotkey_attributes) + 2,
3335 if (!hotkey_dev_attributes)
3337 res = add_many_to_attr_set(hotkey_dev_attributes,
3339 ARRAY_SIZE(hotkey_attributes));
3343 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3344 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3345 for HKEY interface version 0x100 */
3346 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3347 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3348 "firmware HKEY interface version: 0x%x\n",
3351 switch (hkeyv >> 8) {
3354 * MHKV 0x100 in A31, R40, R40e,
3355 * T4x, X31, and later
3358 /* Paranoia check AND init hotkey_all_mask */
3359 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3361 pr_err("missing MHKA handler, please report this to %s\n",
3363 /* Fallback: pre-init for FN+F3,F4,F12 */
3364 hotkey_all_mask = 0x080cU;
3366 tp_features.hotkey_mask = 1;
3372 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3375 /* Paranoia check AND init hotkey_all_mask */
3376 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3378 pr_err("missing MHKA handler, please report this to %s\n",
3380 /* Fallback: pre-init for FN+F3,F4,F12 */
3381 hotkey_all_mask = 0x080cU;
3383 tp_features.hotkey_mask = 1;
3387 * Check if we have an adaptive keyboard, like on the
3388 * Lenovo Carbon X1 2014 (2nd Gen).
3390 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3392 if (hotkey_adaptive_all_mask != 0) {
3393 tp_features.has_adaptive_kbd = true;
3394 res = sysfs_create_group(
3395 &tpacpi_pdev->dev.kobj,
3396 &adaptive_kbd_attr_group);
3401 tp_features.has_adaptive_kbd = false;
3402 hotkey_adaptive_all_mask = 0x0U;
3407 pr_err("unknown version of the HKEY interface: 0x%x\n",
3409 pr_err("please report this to %s\n", TPACPI_MAIL);
3414 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3415 "hotkey masks are %s\n",
3416 str_supported(tp_features.hotkey_mask));
3418 /* Init hotkey_all_mask if not initialized yet */
3419 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3420 (quirks & TPACPI_HK_Q_INIMASK))
3421 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3423 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3424 if (tp_features.hotkey_mask) {
3425 /* hotkey_source_mask *must* be zero for
3426 * the first hotkey_mask_get to return hotkey_orig_mask */
3427 res = hotkey_mask_get();
3431 hotkey_orig_mask = hotkey_acpi_mask;
3433 hotkey_orig_mask = hotkey_all_mask;
3434 hotkey_acpi_mask = hotkey_all_mask;
3437 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3439 tp_features.hotkey_wlsw = 1;
3440 radiosw_state = !!tpacpi_wlsw_emulstate;
3441 pr_info("radio switch emulation enabled\n");
3444 /* Not all thinkpads have a hardware radio switch */
3445 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3446 tp_features.hotkey_wlsw = 1;
3447 radiosw_state = !!status;
3448 pr_info("radio switch found; radios are %s\n",
3449 enabled(status, 0));
3451 if (tp_features.hotkey_wlsw)
3452 res = add_to_attr_set(hotkey_dev_attributes,
3453 &dev_attr_hotkey_radio_sw.attr);
3455 /* For X41t, X60t, X61t Tablets... */
3456 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3457 tp_features.hotkey_tablet = 1;
3458 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3459 pr_info("possible tablet mode switch found; "
3460 "ThinkPad in %s mode\n",
3461 (tabletsw_state) ? "tablet" : "laptop");
3462 res = add_to_attr_set(hotkey_dev_attributes,
3463 &dev_attr_hotkey_tablet_mode.attr);
3467 res = register_attr_set_with_sysfs(
3468 hotkey_dev_attributes,
3469 &tpacpi_pdev->dev.kobj);
3473 /* Set up key map */
3474 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3476 if (!hotkey_keycode_map) {
3477 pr_err("failed to allocate memory for key map\n");
3482 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3483 ARRAY_SIZE(tpacpi_keymap_qtable));
3484 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3485 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3486 "using keymap number %lu\n", keymap_id);
3488 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3489 TPACPI_HOTKEY_MAP_SIZE);
3491 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3492 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3493 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3494 tpacpi_inputdev->keycode = hotkey_keycode_map;
3495 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3496 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3497 input_set_capability(tpacpi_inputdev, EV_KEY,
3498 hotkey_keycode_map[i]);
3500 if (i < sizeof(hotkey_reserved_mask)*8)
3501 hotkey_reserved_mask |= 1 << i;
3505 if (tp_features.hotkey_wlsw) {
3506 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3507 input_report_switch(tpacpi_inputdev,
3508 SW_RFKILL_ALL, radiosw_state);
3510 if (tp_features.hotkey_tablet) {
3511 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3512 input_report_switch(tpacpi_inputdev,
3513 SW_TABLET_MODE, tabletsw_state);
3516 /* Do not issue duplicate brightness change events to
3517 * userspace. tpacpi_detect_brightness_capabilities() must have
3518 * been called before this point */
3519 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3520 pr_info("This ThinkPad has standard ACPI backlight "
3521 "brightness control, supported by the ACPI "
3523 pr_notice("Disabling thinkpad-acpi brightness events "
3526 /* Disable brightness up/down on Lenovo thinkpads when
3527 * ACPI is handling them, otherwise it is plain impossible
3528 * for userspace to do something even remotely sane */
3529 hotkey_reserved_mask |=
3530 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3531 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3532 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3533 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3536 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3537 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3539 & ~hotkey_reserved_mask;
3541 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3542 "hotkey source mask 0x%08x, polling freq %u\n",
3543 hotkey_source_mask, hotkey_poll_freq);
3546 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3547 "enabling firmware HKEY event interface...\n");
3548 res = hotkey_status_set(true);
3553 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3554 | hotkey_driver_mask)
3555 & ~hotkey_source_mask);
3556 if (res < 0 && res != -ENXIO) {
3560 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3561 & ~hotkey_reserved_mask;
3562 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3563 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3564 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3566 tpacpi_inputdev->open = &hotkey_inputdev_open;
3567 tpacpi_inputdev->close = &hotkey_inputdev_close;
3569 hotkey_poll_setup_safe(true);
3574 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3575 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3576 &adaptive_kbd_attr_group);
3578 hotkey_dev_attributes = NULL;
3580 return (res < 0) ? res : 1;
3583 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3584 * mode, Web conference mode, Function mode and Lay-flat mode.
3585 * We support Home mode and Function mode currently.
3587 * Will consider support rest of modes in future.
3590 static const int adaptive_keyboard_modes[] = {
3592 /* WEB_BROWSER_MODE = 2,
3593 WEB_CONFERENCE_MODE = 3, */
3597 #define DFR_CHANGE_ROW 0x101
3598 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3599 #define FIRST_ADAPTIVE_KEY 0x103
3600 #define ADAPTIVE_KEY_OFFSET 0x020
3602 /* press Fn key a while second, it will switch to Function Mode. Then
3603 * release Fn key, previous mode be restored.
3605 static bool adaptive_keyboard_mode_is_saved;
3606 static int adaptive_keyboard_prev_mode;
3608 static int adaptive_keyboard_get_mode(void)
3612 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3613 pr_err("Cannot read adaptive keyboard mode\n");
3620 static int adaptive_keyboard_set_mode(int new_mode)
3623 new_mode > LAYFLAT_MODE)
3626 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3627 pr_err("Cannot set adaptive keyboard mode\n");
3634 static int adaptive_keyboard_get_next_mode(int mode)
3637 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3639 for (i = 0; i <= max_mode; i++) {
3640 if (adaptive_keyboard_modes[i] == mode)
3649 return adaptive_keyboard_modes[i];
3652 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3654 int current_mode = 0;
3659 case DFR_CHANGE_ROW:
3660 if (adaptive_keyboard_mode_is_saved) {
3661 new_mode = adaptive_keyboard_prev_mode;
3662 adaptive_keyboard_mode_is_saved = false;
3664 current_mode = adaptive_keyboard_get_mode();
3665 if (current_mode < 0)
3667 new_mode = adaptive_keyboard_get_next_mode(
3671 if (adaptive_keyboard_set_mode(new_mode) < 0)
3676 case DFR_SHOW_QUICKVIEW_ROW:
3677 current_mode = adaptive_keyboard_get_mode();
3678 if (current_mode < 0)
3681 adaptive_keyboard_prev_mode = current_mode;
3682 adaptive_keyboard_mode_is_saved = true;
3684 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3689 if (scancode < FIRST_ADAPTIVE_KEY ||
3690 scancode >= FIRST_ADAPTIVE_KEY + TPACPI_HOTKEY_MAP_LEN -
3691 ADAPTIVE_KEY_OFFSET) {
3692 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3696 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + ADAPTIVE_KEY_OFFSET];
3697 if (keycode != KEY_RESERVED) {
3698 mutex_lock(&tpacpi_inputdev_send_mutex);
3700 input_report_key(tpacpi_inputdev, keycode, 1);
3701 input_sync(tpacpi_inputdev);
3703 input_report_key(tpacpi_inputdev, keycode, 0);
3704 input_sync(tpacpi_inputdev);
3706 mutex_unlock(&tpacpi_inputdev_send_mutex);
3712 static bool hotkey_notify_hotkey(const u32 hkey,
3714 bool *ignore_acpi_ev)
3716 /* 0x1000-0x1FFF: key presses */
3717 unsigned int scancode = hkey & 0xfff;
3718 *send_acpi_ev = true;
3719 *ignore_acpi_ev = false;
3721 /* HKEY event 0x1001 is scancode 0x00 */
3722 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3724 if (!(hotkey_source_mask & (1 << scancode))) {
3725 tpacpi_input_send_key_masked(scancode);
3726 *send_acpi_ev = false;
3728 *ignore_acpi_ev = true;
3732 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3737 static bool hotkey_notify_wakeup(const u32 hkey,
3739 bool *ignore_acpi_ev)
3741 /* 0x2000-0x2FFF: Wakeup reason */
3742 *send_acpi_ev = true;
3743 *ignore_acpi_ev = false;
3746 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3747 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3748 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3749 *ignore_acpi_ev = true;
3752 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3753 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3754 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3755 *ignore_acpi_ev = true;
3758 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3759 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3760 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3761 /* how to auto-heal: */
3762 /* 2313: woke up from S3, go to S4/S5 */
3763 /* 2413: woke up from S4, go to S5 */
3770 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3771 pr_info("woke up due to a hot-unplug request...\n");
3772 hotkey_wakeup_reason_notify_change();
3777 static bool hotkey_notify_dockevent(const u32 hkey,
3779 bool *ignore_acpi_ev)
3781 /* 0x4000-0x4FFF: dock-related events */
3782 *send_acpi_ev = true;
3783 *ignore_acpi_ev = false;
3786 case TP_HKEY_EV_UNDOCK_ACK:
3787 /* ACPI undock operation completed after wakeup */
3788 hotkey_autosleep_ack = 1;
3789 pr_info("undocked\n");
3790 hotkey_wakeup_hotunplug_complete_notify_change();
3793 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3794 pr_info("docked into hotplug port replicator\n");
3796 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3797 pr_info("undocked from hotplug port replicator\n");
3805 static bool hotkey_notify_usrevent(const u32 hkey,
3807 bool *ignore_acpi_ev)
3809 /* 0x5000-0x5FFF: human interface helpers */
3810 *send_acpi_ev = true;
3811 *ignore_acpi_ev = false;
3814 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3815 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3818 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3819 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3820 tpacpi_input_send_tabletsw();
3821 hotkey_tablet_mode_notify_change();
3822 *send_acpi_ev = false;
3825 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3826 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3827 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3828 /* do not propagate these events */
3829 *ignore_acpi_ev = true;
3837 static void thermal_dump_all_sensors(void);
3839 static bool hotkey_notify_6xxx(const u32 hkey,
3841 bool *ignore_acpi_ev)
3845 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3846 *send_acpi_ev = true;
3847 *ignore_acpi_ev = false;
3850 case TP_HKEY_EV_THM_TABLE_CHANGED:
3851 pr_info("EC reports that Thermal Table has changed\n");
3852 /* recommended action: do nothing, we don't have
3853 * Lenovo ATM information */
3855 case TP_HKEY_EV_ALARM_BAT_HOT:
3856 pr_crit("THERMAL ALARM: battery is too hot!\n");
3857 /* recommended action: warn user through gui */
3859 case TP_HKEY_EV_ALARM_BAT_XHOT:
3860 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3861 /* recommended action: immediate sleep/hibernate */
3863 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3864 pr_crit("THERMAL ALARM: "
3865 "a sensor reports something is too hot!\n");
3866 /* recommended action: warn user through gui, that */
3867 /* some internal component is too hot */
3869 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3870 pr_alert("THERMAL EMERGENCY: "
3871 "a sensor reports something is extremely hot!\n");
3872 /* recommended action: immediate sleep/hibernate */
3874 case TP_HKEY_EV_AC_CHANGED:
3875 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3876 * AC status changed; can be triggered by plugging or
3877 * unplugging AC adapter, docking or undocking. */
3881 case TP_HKEY_EV_KEY_NUMLOCK:
3882 case TP_HKEY_EV_KEY_FN:
3883 case TP_HKEY_EV_KEY_FN_ESC:
3884 /* key press events, we just ignore them as long as the EC
3885 * is still reporting them in the normal keyboard stream */
3886 *send_acpi_ev = false;
3887 *ignore_acpi_ev = true;
3891 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3895 thermal_dump_all_sensors();
3900 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3904 bool ignore_acpi_ev;
3907 if (event != 0x80) {
3908 pr_err("unknown HKEY notification event %d\n", event);
3909 /* forward it to userspace, maybe it knows how to handle it */
3910 acpi_bus_generate_netlink_event(
3911 ibm->acpi->device->pnp.device_class,
3912 dev_name(&ibm->acpi->device->dev),
3918 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3919 pr_err("failed to retrieve HKEY event\n");
3928 send_acpi_ev = true;
3929 ignore_acpi_ev = false;
3931 switch (hkey >> 12) {
3933 /* 0x1000-0x1FFF: key presses */
3934 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3938 /* 0x2000-0x2FFF: Wakeup reason */
3939 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3943 /* 0x3000-0x3FFF: bay-related wakeups */
3945 case TP_HKEY_EV_BAYEJ_ACK:
3946 hotkey_autosleep_ack = 1;
3947 pr_info("bay ejected\n");
3948 hotkey_wakeup_hotunplug_complete_notify_change();
3951 case TP_HKEY_EV_OPTDRV_EJ:
3952 /* FIXME: kick libata if SATA link offline */
3960 /* 0x4000-0x4FFF: dock-related events */
3961 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3965 /* 0x5000-0x5FFF: human interface helpers */
3966 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3970 /* 0x6000-0x6FFF: thermal alarms/notices and
3971 * keyboard events */
3972 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3976 /* 0x7000-0x7FFF: misc */
3977 if (tp_features.hotkey_wlsw &&
3978 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3979 tpacpi_send_radiosw_update();
3984 /* fallthrough to default */
3989 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3990 pr_notice("please report the conditions when this "
3991 "event happened to %s\n", TPACPI_MAIL);
3994 /* netlink events */
3995 if (!ignore_acpi_ev && send_acpi_ev) {
3996 acpi_bus_generate_netlink_event(
3997 ibm->acpi->device->pnp.device_class,
3998 dev_name(&ibm->acpi->device->dev),
4004 static void hotkey_suspend(void)
4006 /* Do these on suspend, we get the events on early resume! */
4007 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4008 hotkey_autosleep_ack = 0;
4010 /* save previous mode of adaptive keyboard of X1 Carbon */
4011 if (tp_features.has_adaptive_kbd) {
4012 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4014 pr_err("Cannot read adaptive keyboard mode.\n");
4019 static void hotkey_resume(void)
4021 tpacpi_disable_brightness_delay();
4023 if (hotkey_status_set(true) < 0 ||
4024 hotkey_mask_set(hotkey_acpi_mask) < 0)
4025 pr_err("error while attempting to reset the event "
4026 "firmware interface\n");
4028 tpacpi_send_radiosw_update();
4029 hotkey_tablet_mode_notify_change();
4030 hotkey_wakeup_reason_notify_change();
4031 hotkey_wakeup_hotunplug_complete_notify_change();
4032 hotkey_poll_setup_safe(false);
4034 /* restore previous mode of adapive keyboard of X1 Carbon */
4035 if (tp_features.has_adaptive_kbd) {
4036 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4037 adaptive_keyboard_prev_mode)) {
4038 pr_err("Cannot set adaptive keyboard mode.\n");
4043 /* procfs -------------------------------------------------------------- */
4044 static int hotkey_read(struct seq_file *m)
4048 if (!tp_features.hotkey) {
4049 seq_printf(m, "status:\t\tnot supported\n");
4053 if (mutex_lock_killable(&hotkey_mutex))
4054 return -ERESTARTSYS;
4055 res = hotkey_status_get(&status);
4057 res = hotkey_mask_get();
4058 mutex_unlock(&hotkey_mutex);
4062 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4063 if (hotkey_all_mask) {
4064 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4065 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4067 seq_printf(m, "mask:\t\tnot supported\n");
4068 seq_printf(m, "commands:\tenable, disable, reset\n");
4074 static void hotkey_enabledisable_warn(bool enable)
4076 tpacpi_log_usertask("procfs hotkey enable/disable");
4077 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4078 pr_fmt("hotkey enable/disable functionality has been "
4079 "removed from the driver. "
4080 "Hotkeys are always enabled.\n")))
4081 pr_err("Please remove the hotkey=enable module "
4082 "parameter, it is deprecated. "
4083 "Hotkeys are always enabled.\n");
4086 static int hotkey_write(char *buf)
4092 if (!tp_features.hotkey)
4095 if (mutex_lock_killable(&hotkey_mutex))
4096 return -ERESTARTSYS;
4098 mask = hotkey_user_mask;
4101 while ((cmd = next_cmd(&buf))) {
4102 if (strlencmp(cmd, "enable") == 0) {
4103 hotkey_enabledisable_warn(1);
4104 } else if (strlencmp(cmd, "disable") == 0) {
4105 hotkey_enabledisable_warn(0);
4107 } else if (strlencmp(cmd, "reset") == 0) {
4108 mask = (hotkey_all_mask | hotkey_source_mask)
4109 & ~hotkey_reserved_mask;
4110 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4112 } else if (sscanf(cmd, "%x", &mask) == 1) {
4121 tpacpi_disclose_usertask("procfs hotkey",
4122 "set mask to 0x%08x\n", mask);
4123 res = hotkey_user_mask_set(mask);
4127 mutex_unlock(&hotkey_mutex);
4131 static const struct acpi_device_id ibm_htk_device_ids[] = {
4132 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4133 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4137 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4138 .hid = ibm_htk_device_ids,
4139 .notify = hotkey_notify,
4140 .handle = &hkey_handle,
4141 .type = ACPI_DEVICE_NOTIFY,
4144 static struct ibm_struct hotkey_driver_data = {
4146 .read = hotkey_read,
4147 .write = hotkey_write,
4148 .exit = hotkey_exit,
4149 .resume = hotkey_resume,
4150 .suspend = hotkey_suspend,
4151 .acpi = &ibm_hotkey_acpidriver,
4154 /*************************************************************************
4155 * Bluetooth subdriver
4159 /* ACPI GBDC/SBDC bits */
4160 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4161 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4162 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4163 0 = disable, 1 = enable */
4167 /* ACPI \BLTH commands */
4168 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4169 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4170 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4171 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4172 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4175 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4177 static int bluetooth_get_status(void)
4181 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4182 if (dbg_bluetoothemul)
4183 return (tpacpi_bluetooth_emulstate) ?
4184 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4187 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4190 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4191 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4194 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4198 vdbg_printk(TPACPI_DBG_RFKILL,
4199 "will attempt to %s bluetooth\n",
4200 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4202 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4203 if (dbg_bluetoothemul) {
4204 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4209 if (state == TPACPI_RFK_RADIO_ON)
4210 status = TP_ACPI_BLUETOOTH_RADIOSSW
4211 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4215 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4221 /* sysfs bluetooth enable ---------------------------------------------- */
4222 static ssize_t bluetooth_enable_show(struct device *dev,
4223 struct device_attribute *attr,
4226 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4230 static ssize_t bluetooth_enable_store(struct device *dev,
4231 struct device_attribute *attr,
4232 const char *buf, size_t count)
4234 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4238 static DEVICE_ATTR_RW(bluetooth_enable);
4240 /* --------------------------------------------------------------------- */
4242 static struct attribute *bluetooth_attributes[] = {
4243 &dev_attr_bluetooth_enable.attr,
4247 static const struct attribute_group bluetooth_attr_group = {
4248 .attrs = bluetooth_attributes,
4251 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4252 .get_status = bluetooth_get_status,
4253 .set_status = bluetooth_set_status,
4256 static void bluetooth_shutdown(void)
4258 /* Order firmware to save current state to NVRAM */
4259 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4260 TP_ACPI_BLTH_SAVE_STATE))
4261 pr_notice("failed to save bluetooth state to NVRAM\n");
4263 vdbg_printk(TPACPI_DBG_RFKILL,
4264 "bluetooth state saved to NVRAM\n");
4267 static void bluetooth_exit(void)
4269 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4270 &bluetooth_attr_group);
4272 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4274 bluetooth_shutdown();
4277 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4283 "initializing bluetooth subdriver\n");
4285 TPACPI_ACPIHANDLE_INIT(hkey);
4287 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4288 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4289 tp_features.bluetooth = hkey_handle &&
4290 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4292 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4293 "bluetooth is %s, status 0x%02x\n",
4294 str_supported(tp_features.bluetooth),
4297 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4298 if (dbg_bluetoothemul) {
4299 tp_features.bluetooth = 1;
4300 pr_info("bluetooth switch emulation enabled\n");
4303 if (tp_features.bluetooth &&
4304 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4305 /* no bluetooth hardware present in system */
4306 tp_features.bluetooth = 0;
4307 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4308 "bluetooth hardware not installed\n");
4311 if (!tp_features.bluetooth)
4314 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4315 &bluetooth_tprfk_ops,
4316 RFKILL_TYPE_BLUETOOTH,
4317 TPACPI_RFK_BLUETOOTH_SW_NAME,
4322 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4323 &bluetooth_attr_group);
4325 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4332 /* procfs -------------------------------------------------------------- */
4333 static int bluetooth_read(struct seq_file *m)
4335 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4338 static int bluetooth_write(char *buf)
4340 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4343 static struct ibm_struct bluetooth_driver_data = {
4344 .name = "bluetooth",
4345 .read = bluetooth_read,
4346 .write = bluetooth_write,
4347 .exit = bluetooth_exit,
4348 .shutdown = bluetooth_shutdown,
4351 /*************************************************************************
4356 /* ACPI GWAN/SWAN bits */
4357 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4358 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4359 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4360 0 = disable, 1 = enable */
4363 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4365 static int wan_get_status(void)
4369 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4371 return (tpacpi_wwan_emulstate) ?
4372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4375 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4378 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4379 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4382 static int wan_set_status(enum tpacpi_rfkill_state state)
4386 vdbg_printk(TPACPI_DBG_RFKILL,
4387 "will attempt to %s wwan\n",
4388 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4390 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4392 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4397 if (state == TPACPI_RFK_RADIO_ON)
4398 status = TP_ACPI_WANCARD_RADIOSSW
4399 | TP_ACPI_WANCARD_RESUMECTRL;
4403 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4409 /* sysfs wan enable ---------------------------------------------------- */
4410 static ssize_t wan_enable_show(struct device *dev,
4411 struct device_attribute *attr,
4414 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4418 static ssize_t wan_enable_store(struct device *dev,
4419 struct device_attribute *attr,
4420 const char *buf, size_t count)
4422 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4426 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4427 wan_enable_show, wan_enable_store);
4429 /* --------------------------------------------------------------------- */
4431 static struct attribute *wan_attributes[] = {
4432 &dev_attr_wwan_enable.attr,
4436 static const struct attribute_group wan_attr_group = {
4437 .attrs = wan_attributes,
4440 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4441 .get_status = wan_get_status,
4442 .set_status = wan_set_status,
4445 static void wan_shutdown(void)
4447 /* Order firmware to save current state to NVRAM */
4448 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4449 TP_ACPI_WGSV_SAVE_STATE))
4450 pr_notice("failed to save WWAN state to NVRAM\n");
4452 vdbg_printk(TPACPI_DBG_RFKILL,
4453 "WWAN state saved to NVRAM\n");
4456 static void wan_exit(void)
4458 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4461 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4466 static int __init wan_init(struct ibm_init_struct *iibm)
4471 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4472 "initializing wan subdriver\n");
4474 TPACPI_ACPIHANDLE_INIT(hkey);
4476 tp_features.wan = hkey_handle &&
4477 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4479 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4480 "wan is %s, status 0x%02x\n",
4481 str_supported(tp_features.wan),
4484 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4486 tp_features.wan = 1;
4487 pr_info("wwan switch emulation enabled\n");
4490 if (tp_features.wan &&
4491 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4492 /* no wan hardware present in system */
4493 tp_features.wan = 0;
4494 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4495 "wan hardware not installed\n");
4498 if (!tp_features.wan)
4501 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4504 TPACPI_RFK_WWAN_SW_NAME,
4509 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4513 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4520 /* procfs -------------------------------------------------------------- */
4521 static int wan_read(struct seq_file *m)
4523 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4526 static int wan_write(char *buf)
4528 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4531 static struct ibm_struct wan_driver_data = {
4536 .shutdown = wan_shutdown,
4539 /*************************************************************************
4544 /* ACPI GUWB/SUWB bits */
4545 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4546 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4549 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4551 static int uwb_get_status(void)
4555 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4557 return (tpacpi_uwb_emulstate) ?
4558 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4561 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4564 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4565 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4568 static int uwb_set_status(enum tpacpi_rfkill_state state)
4572 vdbg_printk(TPACPI_DBG_RFKILL,
4573 "will attempt to %s UWB\n",
4574 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4576 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4578 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4583 if (state == TPACPI_RFK_RADIO_ON)
4584 status = TP_ACPI_UWB_RADIOSSW;
4588 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4594 /* --------------------------------------------------------------------- */
4596 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4597 .get_status = uwb_get_status,
4598 .set_status = uwb_set_status,
4601 static void uwb_exit(void)
4603 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4606 static int __init uwb_init(struct ibm_init_struct *iibm)
4611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4612 "initializing uwb subdriver\n");
4614 TPACPI_ACPIHANDLE_INIT(hkey);
4616 tp_features.uwb = hkey_handle &&
4617 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4619 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4620 "uwb is %s, status 0x%02x\n",
4621 str_supported(tp_features.uwb),
4624 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4626 tp_features.uwb = 1;
4627 pr_info("uwb switch emulation enabled\n");
4630 if (tp_features.uwb &&
4631 !(status & TP_ACPI_UWB_HWPRESENT)) {
4632 /* no uwb hardware present in system */
4633 tp_features.uwb = 0;
4634 dbg_printk(TPACPI_DBG_INIT,
4635 "uwb hardware not installed\n");
4638 if (!tp_features.uwb)
4641 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4644 TPACPI_RFK_UWB_SW_NAME,
4649 static struct ibm_struct uwb_driver_data = {
4652 .flags.experimental = 1,
4655 /*************************************************************************
4659 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4661 enum video_access_mode {
4662 TPACPI_VIDEO_NONE = 0,
4663 TPACPI_VIDEO_570, /* 570 */
4664 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4665 TPACPI_VIDEO_NEW, /* all others */
4668 enum { /* video status flags, based on VIDEO_570 */
4669 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4670 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4671 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4674 enum { /* TPACPI_VIDEO_570 constants */
4675 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4676 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4677 * video_status_flags */
4678 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4679 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4682 static enum video_access_mode video_supported;
4683 static int video_orig_autosw;
4685 static int video_autosw_get(void);
4686 static int video_autosw_set(int enable);
4688 TPACPI_HANDLE(vid, root,
4689 "\\_SB.PCI.AGP.VGA", /* 570 */
4690 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4691 "\\_SB.PCI0.VID0", /* 770e */
4692 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4693 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4694 "\\_SB.PCI0.AGP.VID", /* all others */
4697 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4699 static int __init video_init(struct ibm_init_struct *iibm)
4703 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4705 TPACPI_ACPIHANDLE_INIT(vid);
4706 if (tpacpi_is_ibm())
4707 TPACPI_ACPIHANDLE_INIT(vid2);
4709 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4710 /* G41, assume IVGA doesn't change */
4711 vid_handle = vid2_handle;
4714 /* video switching not supported on R30, R31 */
4715 video_supported = TPACPI_VIDEO_NONE;
4716 else if (tpacpi_is_ibm() &&
4717 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4719 video_supported = TPACPI_VIDEO_570;
4720 else if (tpacpi_is_ibm() &&
4721 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4722 /* 600e/x, 770e, 770x */
4723 video_supported = TPACPI_VIDEO_770;
4726 video_supported = TPACPI_VIDEO_NEW;
4728 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4729 str_supported(video_supported != TPACPI_VIDEO_NONE),
4732 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4735 static void video_exit(void)
4737 dbg_printk(TPACPI_DBG_EXIT,
4738 "restoring original video autoswitch mode\n");
4739 if (video_autosw_set(video_orig_autosw))
4740 pr_err("error while trying to restore original "
4741 "video autoswitch mode\n");
4744 static int video_outputsw_get(void)
4749 switch (video_supported) {
4750 case TPACPI_VIDEO_570:
4751 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4752 TP_ACPI_VIDEO_570_PHSCMD))
4754 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4756 case TPACPI_VIDEO_770:
4757 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4760 status |= TP_ACPI_VIDEO_S_LCD;
4761 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4764 status |= TP_ACPI_VIDEO_S_CRT;
4766 case TPACPI_VIDEO_NEW:
4767 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4768 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4771 status |= TP_ACPI_VIDEO_S_CRT;
4773 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4774 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4777 status |= TP_ACPI_VIDEO_S_LCD;
4778 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4781 status |= TP_ACPI_VIDEO_S_DVI;
4790 static int video_outputsw_set(int status)
4795 switch (video_supported) {
4796 case TPACPI_VIDEO_570:
4797 res = acpi_evalf(NULL, NULL,
4798 "\\_SB.PHS2", "vdd",
4799 TP_ACPI_VIDEO_570_PHS2CMD,
4800 status | TP_ACPI_VIDEO_570_PHS2SET);
4802 case TPACPI_VIDEO_770:
4803 autosw = video_autosw_get();
4807 res = video_autosw_set(1);
4810 res = acpi_evalf(vid_handle, NULL,
4811 "ASWT", "vdd", status * 0x100, 0);
4812 if (!autosw && video_autosw_set(autosw)) {
4813 pr_err("video auto-switch left enabled due to error\n");
4817 case TPACPI_VIDEO_NEW:
4818 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4819 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4825 return (res) ? 0 : -EIO;
4828 static int video_autosw_get(void)
4832 switch (video_supported) {
4833 case TPACPI_VIDEO_570:
4834 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4837 case TPACPI_VIDEO_770:
4838 case TPACPI_VIDEO_NEW:
4839 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4849 static int video_autosw_set(int enable)
4851 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4856 static int video_outputsw_cycle(void)
4858 int autosw = video_autosw_get();
4864 switch (video_supported) {
4865 case TPACPI_VIDEO_570:
4866 res = video_autosw_set(1);
4869 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4871 case TPACPI_VIDEO_770:
4872 case TPACPI_VIDEO_NEW:
4873 res = video_autosw_set(1);
4876 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4881 if (!autosw && video_autosw_set(autosw)) {
4882 pr_err("video auto-switch left enabled due to error\n");
4886 return (res) ? 0 : -EIO;
4889 static int video_expand_toggle(void)
4891 switch (video_supported) {
4892 case TPACPI_VIDEO_570:
4893 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
4895 case TPACPI_VIDEO_770:
4896 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
4898 case TPACPI_VIDEO_NEW:
4899 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
4907 static int video_read(struct seq_file *m)
4911 if (video_supported == TPACPI_VIDEO_NONE) {
4912 seq_printf(m, "status:\t\tnot supported\n");
4916 /* Even reads can crash X.org, so... */
4917 if (!capable(CAP_SYS_ADMIN))
4920 status = video_outputsw_get();
4924 autosw = video_autosw_get();
4928 seq_printf(m, "status:\t\tsupported\n");
4929 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4930 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4931 if (video_supported == TPACPI_VIDEO_NEW)
4932 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4933 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4934 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4935 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4936 if (video_supported == TPACPI_VIDEO_NEW)
4937 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4938 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4939 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4944 static int video_write(char *buf)
4947 int enable, disable, status;
4950 if (video_supported == TPACPI_VIDEO_NONE)
4953 /* Even reads can crash X.org, let alone writes... */
4954 if (!capable(CAP_SYS_ADMIN))
4960 while ((cmd = next_cmd(&buf))) {
4961 if (strlencmp(cmd, "lcd_enable") == 0) {
4962 enable |= TP_ACPI_VIDEO_S_LCD;
4963 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4964 disable |= TP_ACPI_VIDEO_S_LCD;
4965 } else if (strlencmp(cmd, "crt_enable") == 0) {
4966 enable |= TP_ACPI_VIDEO_S_CRT;
4967 } else if (strlencmp(cmd, "crt_disable") == 0) {
4968 disable |= TP_ACPI_VIDEO_S_CRT;
4969 } else if (video_supported == TPACPI_VIDEO_NEW &&
4970 strlencmp(cmd, "dvi_enable") == 0) {
4971 enable |= TP_ACPI_VIDEO_S_DVI;
4972 } else if (video_supported == TPACPI_VIDEO_NEW &&
4973 strlencmp(cmd, "dvi_disable") == 0) {
4974 disable |= TP_ACPI_VIDEO_S_DVI;
4975 } else if (strlencmp(cmd, "auto_enable") == 0) {
4976 res = video_autosw_set(1);
4979 } else if (strlencmp(cmd, "auto_disable") == 0) {
4980 res = video_autosw_set(0);
4983 } else if (strlencmp(cmd, "video_switch") == 0) {
4984 res = video_outputsw_cycle();
4987 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4988 res = video_expand_toggle();
4995 if (enable || disable) {
4996 status = video_outputsw_get();
4999 res = video_outputsw_set((status & ~disable) | enable);
5007 static struct ibm_struct video_driver_data = {
5010 .write = video_write,
5014 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5016 /*************************************************************************
5017 * Keyboard backlight subdriver
5020 static int kbdlight_set_level(int level)
5025 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5031 static int kbdlight_set_level_and_update(int level);
5033 static int kbdlight_get_level(void)
5040 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5046 return status & 0x3;
5049 static bool kbdlight_is_supported(void)
5056 if (!acpi_has_method(hkey_handle, "MLCG")) {
5057 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5061 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5062 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5067 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5071 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5073 * Guessed test for keyboard backlight:
5075 * Machines with backlight keyboard return:
5076 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5077 * b110100010010000000XX - ThinkPad x230
5078 * b010100000010000000XX - ThinkPad x240
5079 * b010100000010000000XX - ThinkPad W541
5080 * (XX is current backlight level)
5082 * Machines without backlight keyboard return:
5083 * b10100001000000000000 - ThinkPad x230
5084 * b10110001000000000000 - ThinkPad E430
5085 * b00000000000000000000 - ThinkPad E450
5087 * Candidate BITs for detection test (XOR):
5088 * b01000000001000000000
5091 return status & BIT(9);
5094 static void kbdlight_set_worker(struct work_struct *work)
5096 struct tpacpi_led_classdev *data =
5097 container_of(work, struct tpacpi_led_classdev, work);
5099 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5100 kbdlight_set_level_and_update(data->new_state);
5103 static void kbdlight_sysfs_set(struct led_classdev *led_cdev,
5104 enum led_brightness brightness)
5106 struct tpacpi_led_classdev *data =
5107 container_of(led_cdev,
5108 struct tpacpi_led_classdev,
5110 data->new_state = brightness;
5111 queue_work(tpacpi_wq, &data->work);
5114 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5118 level = kbdlight_get_level();
5125 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5127 .name = "tpacpi::kbd_backlight",
5128 .max_brightness = 2,
5129 .brightness_set = &kbdlight_sysfs_set,
5130 .brightness_get = &kbdlight_sysfs_get,
5134 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5138 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5140 TPACPI_ACPIHANDLE_INIT(hkey);
5141 INIT_WORK(&tpacpi_led_kbdlight.work, kbdlight_set_worker);
5143 if (!kbdlight_is_supported()) {
5144 tp_features.kbdlight = 0;
5145 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5149 tp_features.kbdlight = 1;
5151 rc = led_classdev_register(&tpacpi_pdev->dev,
5152 &tpacpi_led_kbdlight.led_classdev);
5154 tp_features.kbdlight = 0;
5161 static void kbdlight_exit(void)
5163 if (tp_features.kbdlight)
5164 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5165 flush_workqueue(tpacpi_wq);
5168 static int kbdlight_set_level_and_update(int level)
5171 struct led_classdev *led_cdev;
5173 ret = kbdlight_set_level(level);
5174 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5176 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5177 led_cdev->brightness = level;
5182 static int kbdlight_read(struct seq_file *m)
5186 if (!tp_features.kbdlight) {
5187 seq_printf(m, "status:\t\tnot supported\n");
5189 level = kbdlight_get_level();
5191 seq_printf(m, "status:\t\terror %d\n", level);
5193 seq_printf(m, "status:\t\t%d\n", level);
5194 seq_printf(m, "commands:\t0, 1, 2\n");
5200 static int kbdlight_write(char *buf)
5205 if (!tp_features.kbdlight)
5208 while ((cmd = next_cmd(&buf))) {
5209 if (strlencmp(cmd, "0") == 0)
5211 else if (strlencmp(cmd, "1") == 0)
5213 else if (strlencmp(cmd, "2") == 0)
5222 return kbdlight_set_level_and_update(level);
5225 static void kbdlight_suspend(void)
5227 struct led_classdev *led_cdev;
5229 if (!tp_features.kbdlight)
5232 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5233 led_update_brightness(led_cdev);
5234 led_classdev_suspend(led_cdev);
5237 static void kbdlight_resume(void)
5239 if (!tp_features.kbdlight)
5242 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5245 static struct ibm_struct kbdlight_driver_data = {
5247 .read = kbdlight_read,
5248 .write = kbdlight_write,
5249 .suspend = kbdlight_suspend,
5250 .resume = kbdlight_resume,
5251 .exit = kbdlight_exit,
5254 /*************************************************************************
5255 * Light (thinklight) subdriver
5258 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5259 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5261 static int light_get_status(void)
5265 if (tp_features.light_status) {
5266 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5274 static int light_set_status(int status)
5278 if (tp_features.light) {
5280 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5282 TP_CMOS_THINKLIGHT_ON :
5283 TP_CMOS_THINKLIGHT_OFF);
5285 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5288 return (rc) ? 0 : -EIO;
5294 static void light_set_status_worker(struct work_struct *work)
5296 struct tpacpi_led_classdev *data =
5297 container_of(work, struct tpacpi_led_classdev, work);
5299 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5300 light_set_status((data->new_state != TPACPI_LED_OFF));
5303 static void light_sysfs_set(struct led_classdev *led_cdev,
5304 enum led_brightness brightness)
5306 struct tpacpi_led_classdev *data =
5307 container_of(led_cdev,
5308 struct tpacpi_led_classdev,
5310 data->new_state = (brightness != LED_OFF) ?
5311 TPACPI_LED_ON : TPACPI_LED_OFF;
5312 queue_work(tpacpi_wq, &data->work);
5315 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5317 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5320 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5322 .name = "tpacpi::thinklight",
5323 .brightness_set = &light_sysfs_set,
5324 .brightness_get = &light_sysfs_get,
5328 static int __init light_init(struct ibm_init_struct *iibm)
5332 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5334 if (tpacpi_is_ibm()) {
5335 TPACPI_ACPIHANDLE_INIT(ledb);
5336 TPACPI_ACPIHANDLE_INIT(lght);
5338 TPACPI_ACPIHANDLE_INIT(cmos);
5339 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
5341 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5342 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5344 if (tp_features.light)
5345 /* light status not supported on
5346 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5347 tp_features.light_status =
5348 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5350 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5351 str_supported(tp_features.light),
5352 str_supported(tp_features.light_status));
5354 if (!tp_features.light)
5357 rc = led_classdev_register(&tpacpi_pdev->dev,
5358 &tpacpi_led_thinklight.led_classdev);
5361 tp_features.light = 0;
5362 tp_features.light_status = 0;
5370 static void light_exit(void)
5372 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5373 flush_workqueue(tpacpi_wq);
5376 static int light_read(struct seq_file *m)
5380 if (!tp_features.light) {
5381 seq_printf(m, "status:\t\tnot supported\n");
5382 } else if (!tp_features.light_status) {
5383 seq_printf(m, "status:\t\tunknown\n");
5384 seq_printf(m, "commands:\ton, off\n");
5386 status = light_get_status();
5389 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5390 seq_printf(m, "commands:\ton, off\n");
5396 static int light_write(char *buf)
5401 if (!tp_features.light)
5404 while ((cmd = next_cmd(&buf))) {
5405 if (strlencmp(cmd, "on") == 0) {
5407 } else if (strlencmp(cmd, "off") == 0) {
5413 return light_set_status(newstatus);
5416 static struct ibm_struct light_driver_data = {
5419 .write = light_write,
5423 /*************************************************************************
5427 /* sysfs cmos_command -------------------------------------------------- */
5428 static ssize_t cmos_command_store(struct device *dev,
5429 struct device_attribute *attr,
5430 const char *buf, size_t count)
5432 unsigned long cmos_cmd;
5435 if (parse_strtoul(buf, 21, &cmos_cmd))
5438 res = issue_thinkpad_cmos_command(cmos_cmd);
5439 return (res) ? res : count;
5442 static DEVICE_ATTR_WO(cmos_command);
5444 /* --------------------------------------------------------------------- */
5446 static int __init cmos_init(struct ibm_init_struct *iibm)
5450 vdbg_printk(TPACPI_DBG_INIT,
5451 "initializing cmos commands subdriver\n");
5453 TPACPI_ACPIHANDLE_INIT(cmos);
5455 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5456 str_supported(cmos_handle != NULL));
5458 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5462 return (cmos_handle) ? 0 : 1;
5465 static void cmos_exit(void)
5467 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5470 static int cmos_read(struct seq_file *m)
5472 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5473 R30, R31, T20-22, X20-21 */
5475 seq_printf(m, "status:\t\tnot supported\n");
5477 seq_printf(m, "status:\t\tsupported\n");
5478 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5484 static int cmos_write(char *buf)
5489 while ((cmd = next_cmd(&buf))) {
5490 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5491 cmos_cmd >= 0 && cmos_cmd <= 21) {
5496 res = issue_thinkpad_cmos_command(cmos_cmd);
5504 static struct ibm_struct cmos_driver_data = {
5507 .write = cmos_write,
5511 /*************************************************************************
5515 enum led_access_mode {
5516 TPACPI_LED_NONE = 0,
5517 TPACPI_LED_570, /* 570 */
5518 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5519 TPACPI_LED_NEW, /* all others */
5522 enum { /* For TPACPI_LED_OLD */
5523 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5524 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5525 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5528 static enum led_access_mode led_supported;
5530 static acpi_handle led_handle;
5532 #define TPACPI_LED_NUMLEDS 16
5533 static struct tpacpi_led_classdev *tpacpi_leds;
5534 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5535 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5536 /* there's a limit of 19 chars + NULL before 2.6.26 */
5538 "tpacpi:orange:batt",
5539 "tpacpi:green:batt",
5540 "tpacpi::dock_active",
5541 "tpacpi::bay_active",
5542 "tpacpi::dock_batt",
5543 "tpacpi::unknown_led",
5545 "tpacpi::dock_status1",
5546 "tpacpi::dock_status2",
5547 "tpacpi::unknown_led2",
5548 "tpacpi::unknown_led3",
5549 "tpacpi::thinkvantage",
5551 #define TPACPI_SAFE_LEDS 0x1081U
5553 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5555 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5558 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5562 static int led_get_status(const unsigned int led)
5565 enum led_status_t led_s;
5567 switch (led_supported) {
5568 case TPACPI_LED_570:
5569 if (!acpi_evalf(ec_handle,
5570 &status, "GLED", "dd", 1 << led))
5572 led_s = (status == 0) ?
5577 tpacpi_led_state_cache[led] = led_s;
5586 static int led_set_status(const unsigned int led,
5587 const enum led_status_t ledstatus)
5589 /* off, on, blink. Index is led_status_t */
5590 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5591 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5595 switch (led_supported) {
5596 case TPACPI_LED_570:
5598 if (unlikely(led > 7))
5600 if (unlikely(tpacpi_is_led_restricted(led)))
5602 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5603 (1 << led), led_sled_arg1[ledstatus]))
5606 case TPACPI_LED_OLD:
5607 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5608 if (unlikely(led > 7))
5610 if (unlikely(tpacpi_is_led_restricted(led)))
5612 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5614 rc = ec_write(TPACPI_LED_EC_HLBL,
5615 (ledstatus == TPACPI_LED_BLINK) << led);
5617 rc = ec_write(TPACPI_LED_EC_HLCL,
5618 (ledstatus != TPACPI_LED_OFF) << led);
5620 case TPACPI_LED_NEW:
5622 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5624 if (unlikely(tpacpi_is_led_restricted(led)))
5626 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5627 led, led_led_arg1[ledstatus]))
5635 tpacpi_led_state_cache[led] = ledstatus;
5640 static void led_set_status_worker(struct work_struct *work)
5642 struct tpacpi_led_classdev *data =
5643 container_of(work, struct tpacpi_led_classdev, work);
5645 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5646 led_set_status(data->led, data->new_state);
5649 static void led_sysfs_set(struct led_classdev *led_cdev,
5650 enum led_brightness brightness)
5652 struct tpacpi_led_classdev *data = container_of(led_cdev,
5653 struct tpacpi_led_classdev, led_classdev);
5655 if (brightness == LED_OFF)
5656 data->new_state = TPACPI_LED_OFF;
5657 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5658 data->new_state = TPACPI_LED_ON;
5660 data->new_state = TPACPI_LED_BLINK;
5662 queue_work(tpacpi_wq, &data->work);
5665 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5666 unsigned long *delay_on, unsigned long *delay_off)
5668 struct tpacpi_led_classdev *data = container_of(led_cdev,
5669 struct tpacpi_led_classdev, led_classdev);
5671 /* Can we choose the flash rate? */
5672 if (*delay_on == 0 && *delay_off == 0) {
5673 /* yes. set them to the hardware blink rate (1 Hz) */
5674 *delay_on = 500; /* ms */
5675 *delay_off = 500; /* ms */
5676 } else if ((*delay_on != 500) || (*delay_off != 500))
5679 data->new_state = TPACPI_LED_BLINK;
5680 queue_work(tpacpi_wq, &data->work);
5685 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5689 struct tpacpi_led_classdev *data = container_of(led_cdev,
5690 struct tpacpi_led_classdev, led_classdev);
5692 rc = led_get_status(data->led);
5694 if (rc == TPACPI_LED_OFF || rc < 0)
5695 rc = LED_OFF; /* no error handling in led class :( */
5702 static void led_exit(void)
5706 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5707 if (tpacpi_leds[i].led_classdev.name)
5708 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5711 flush_workqueue(tpacpi_wq);
5715 static int __init tpacpi_init_led(unsigned int led)
5719 tpacpi_leds[led].led = led;
5721 /* LEDs with no name don't get registered */
5722 if (!tpacpi_led_names[led])
5725 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5726 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5727 if (led_supported == TPACPI_LED_570)
5728 tpacpi_leds[led].led_classdev.brightness_get =
5731 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5733 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5735 rc = led_classdev_register(&tpacpi_pdev->dev,
5736 &tpacpi_leds[led].led_classdev);
5738 tpacpi_leds[led].led_classdev.name = NULL;
5743 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5744 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5745 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5746 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5748 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5749 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5750 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5751 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5752 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5753 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5754 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5755 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5757 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5758 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5759 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5760 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5761 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5763 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5764 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5765 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5766 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5768 /* (1) - may have excess leds enabled on MSB */
5770 /* Defaults (order matters, keep last, don't reorder!) */
5772 .vendor = PCI_VENDOR_ID_LENOVO,
5773 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5776 { /* IBM ThinkPads with no EC version string */
5777 .vendor = PCI_VENDOR_ID_IBM,
5778 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5781 { /* IBM ThinkPads with EC version string */
5782 .vendor = PCI_VENDOR_ID_IBM,
5783 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5788 #undef TPACPI_LEDQ_IBM
5789 #undef TPACPI_LEDQ_LNV
5791 static enum led_access_mode __init led_init_detect_mode(void)
5795 if (tpacpi_is_ibm()) {
5797 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5798 if (ACPI_SUCCESS(status))
5799 return TPACPI_LED_570;
5801 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5802 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5803 if (ACPI_SUCCESS(status))
5804 return TPACPI_LED_OLD;
5808 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5809 if (ACPI_SUCCESS(status))
5810 return TPACPI_LED_NEW;
5812 /* R30, R31, and unknown firmwares */
5814 return TPACPI_LED_NONE;
5817 static int __init led_init(struct ibm_init_struct *iibm)
5821 unsigned long useful_leds;
5823 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5825 led_supported = led_init_detect_mode();
5827 if (led_supported != TPACPI_LED_NONE) {
5828 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5829 ARRAY_SIZE(led_useful_qtable));
5833 led_supported = TPACPI_LED_NONE;
5837 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5838 str_supported(led_supported), led_supported);
5840 if (led_supported == TPACPI_LED_NONE)
5843 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5846 pr_err("Out of memory for LED data\n");
5850 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5851 tpacpi_leds[i].led = -1;
5853 if (!tpacpi_is_led_restricted(i) &&
5854 test_bit(i, &useful_leds)) {
5855 rc = tpacpi_init_led(i);
5863 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5864 pr_notice("warning: userspace override of important "
5865 "firmware LEDs is enabled\n");
5870 #define str_led_status(s) \
5871 ((s) == TPACPI_LED_OFF ? "off" : \
5872 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5874 static int led_read(struct seq_file *m)
5876 if (!led_supported) {
5877 seq_printf(m, "status:\t\tnot supported\n");
5880 seq_printf(m, "status:\t\tsupported\n");
5882 if (led_supported == TPACPI_LED_570) {
5885 for (i = 0; i < 8; i++) {
5886 status = led_get_status(i);
5889 seq_printf(m, "%d:\t\t%s\n",
5890 i, str_led_status(status));
5894 seq_printf(m, "commands:\t"
5895 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5900 static int led_write(char *buf)
5904 enum led_status_t s;
5909 while ((cmd = next_cmd(&buf))) {
5910 if (sscanf(cmd, "%d", &led) != 1)
5913 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5914 tpacpi_leds[led].led < 0)
5917 if (strstr(cmd, "off")) {
5919 } else if (strstr(cmd, "on")) {
5921 } else if (strstr(cmd, "blink")) {
5922 s = TPACPI_LED_BLINK;
5927 rc = led_set_status(led, s);
5935 static struct ibm_struct led_driver_data = {
5942 /*************************************************************************
5946 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5948 #define TPACPI_BEEP_Q1 0x0001
5950 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5951 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5952 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5955 static int __init beep_init(struct ibm_init_struct *iibm)
5957 unsigned long quirks;
5959 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5961 TPACPI_ACPIHANDLE_INIT(beep);
5963 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5964 str_supported(beep_handle != NULL));
5966 quirks = tpacpi_check_quirks(beep_quirk_table,
5967 ARRAY_SIZE(beep_quirk_table));
5969 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5971 return (beep_handle) ? 0 : 1;
5974 static int beep_read(struct seq_file *m)
5977 seq_printf(m, "status:\t\tnot supported\n");
5979 seq_printf(m, "status:\t\tsupported\n");
5980 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5986 static int beep_write(char *buf)
5994 while ((cmd = next_cmd(&buf))) {
5995 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5996 beep_cmd >= 0 && beep_cmd <= 17) {
6000 if (tp_features.beep_needs_two_args) {
6001 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6005 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6014 static struct ibm_struct beep_driver_data = {
6017 .write = beep_write,
6020 /*************************************************************************
6024 enum thermal_access_mode {
6025 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6026 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6027 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6028 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6029 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6032 enum { /* TPACPI_THERMAL_TPEC_* */
6033 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6034 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6035 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */
6036 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6038 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6042 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6043 struct ibm_thermal_sensors_struct {
6044 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6047 static enum thermal_access_mode thermal_read_mode;
6049 /* idx is zero-based */
6050 static int thermal_get_sensor(int idx, s32 *value)
6056 t = TP_EC_THERMAL_TMP0;
6058 switch (thermal_read_mode) {
6059 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6060 case TPACPI_THERMAL_TPEC_16:
6061 if (idx >= 8 && idx <= 15) {
6062 t = TP_EC_THERMAL_TMP8;
6067 case TPACPI_THERMAL_TPEC_8:
6069 if (!acpi_ec_read(t + idx, &tmp))
6071 *value = tmp * 1000;
6076 case TPACPI_THERMAL_ACPI_UPDT:
6078 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6079 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6081 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6083 *value = (t - 2732) * 100;
6088 case TPACPI_THERMAL_ACPI_TMP07:
6090 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6091 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6093 if (t > 127 || t < -127)
6094 t = TP_EC_THERMAL_TMP_NA;
6100 case TPACPI_THERMAL_NONE:
6108 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6119 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6122 for (i = 0 ; i < n; i++) {
6123 res = thermal_get_sensor(i, &s->temp[i]);
6131 static void thermal_dump_all_sensors(void)
6134 struct ibm_thermal_sensors_struct t;
6136 n = thermal_get_sensors(&t);
6140 pr_notice("temperatures (Celsius):");
6142 for (i = 0; i < n; i++) {
6143 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6144 pr_cont(" %d", (int)(t.temp[i] / 1000));
6152 /* sysfs temp##_input -------------------------------------------------- */
6154 static ssize_t thermal_temp_input_show(struct device *dev,
6155 struct device_attribute *attr,
6158 struct sensor_device_attribute *sensor_attr =
6159 to_sensor_dev_attr(attr);
6160 int idx = sensor_attr->index;
6164 res = thermal_get_sensor(idx, &value);
6167 if (value == TPACPI_THERMAL_SENSOR_NA)
6170 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6173 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6174 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6175 thermal_temp_input_show, NULL, _idxB)
6177 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6178 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6179 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6180 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6181 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6182 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6183 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6184 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6185 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6186 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6187 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6188 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6189 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6190 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6191 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6192 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6193 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6196 #define THERMAL_ATTRS(X) \
6197 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6199 static struct attribute *thermal_temp_input_attr[] = {
6219 static const struct attribute_group thermal_temp_input16_group = {
6220 .attrs = thermal_temp_input_attr
6223 static const struct attribute_group thermal_temp_input8_group = {
6224 .attrs = &thermal_temp_input_attr[8]
6227 #undef THERMAL_SENSOR_ATTR_TEMP
6228 #undef THERMAL_ATTRS
6230 /* --------------------------------------------------------------------- */
6232 static int __init thermal_init(struct ibm_init_struct *iibm)
6234 u8 t, ta1, ta2, ver = 0;
6239 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6241 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6243 if (thinkpad_id.ec_model) {
6245 * Direct EC access mode: sensors at registers
6246 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6247 * non-implemented, thermal sensors return 0x80 when
6249 * The above rule is unfortunately flawed. This has been seen with
6250 * 0xC2 (power supply ID) causing thermal control problems.
6251 * The EC version can be determined by offset 0xEF and at least for
6252 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7
6253 * are not thermal registers.
6255 if (!acpi_ec_read(TP_EC_FUNCREV, &ver))
6256 pr_warn("Thinkpad ACPI EC unable to access EC version\n");
6259 for (i = 0; i < 8; i++) {
6260 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6267 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6276 /* This is sheer paranoia, but we handle it anyway */
6278 pr_err("ThinkPad ACPI EC access misbehaving, "
6279 "falling back to ACPI TMPx access "
6281 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6283 pr_err("ThinkPad ACPI EC access misbehaving, "
6284 "disabling thermal sensors access\n");
6285 thermal_read_mode = TPACPI_THERMAL_NONE;
6289 thermal_read_mode = TPACPI_THERMAL_TPEC_8;
6293 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6295 } else if (acpi_tmp7) {
6296 if (tpacpi_is_ibm() &&
6297 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6298 /* 600e/x, 770e, 770x */
6299 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6301 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6302 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6305 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6306 thermal_read_mode = TPACPI_THERMAL_NONE;
6309 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6310 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6313 switch (thermal_read_mode) {
6314 case TPACPI_THERMAL_TPEC_16:
6315 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6316 &thermal_temp_input16_group);
6320 case TPACPI_THERMAL_TPEC_8:
6321 case TPACPI_THERMAL_ACPI_TMP07:
6322 case TPACPI_THERMAL_ACPI_UPDT:
6323 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6324 &thermal_temp_input8_group);
6328 case TPACPI_THERMAL_NONE:
6336 static void thermal_exit(void)
6338 switch (thermal_read_mode) {
6339 case TPACPI_THERMAL_TPEC_16:
6340 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6341 &thermal_temp_input16_group);
6343 case TPACPI_THERMAL_TPEC_8:
6344 case TPACPI_THERMAL_ACPI_TMP07:
6345 case TPACPI_THERMAL_ACPI_UPDT:
6346 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6347 &thermal_temp_input8_group);
6349 case TPACPI_THERMAL_NONE:
6355 static int thermal_read(struct seq_file *m)
6358 struct ibm_thermal_sensors_struct t;
6360 n = thermal_get_sensors(&t);
6361 if (unlikely(n < 0))
6364 seq_printf(m, "temperatures:\t");
6367 for (i = 0; i < (n - 1); i++)
6368 seq_printf(m, "%d ", t.temp[i] / 1000);
6369 seq_printf(m, "%d\n", t.temp[i] / 1000);
6371 seq_printf(m, "not supported\n");
6376 static struct ibm_struct thermal_driver_data = {
6378 .read = thermal_read,
6379 .exit = thermal_exit,
6382 /*************************************************************************
6383 * Backlight/brightness subdriver
6386 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6389 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6390 * CMOS NVRAM byte 0x5E, bits 0-3.
6392 * EC HBRV (0x31) has the following layout
6393 * Bit 7: unknown function
6394 * Bit 6: unknown function
6395 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6396 * Bit 4: must be set to zero to avoid problems
6397 * Bit 3-0: backlight brightness level
6399 * brightness_get_raw returns status data in the HBRV layout
6401 * WARNING: The X61 has been verified to use HBRV for something else, so
6402 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6403 * testing on the very early *60 Lenovo models...
6407 TP_EC_BACKLIGHT = 0x31,
6409 /* TP_EC_BACKLIGHT bitmasks */
6410 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6411 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6412 TP_EC_BACKLIGHT_MAPSW = 0x20,
6415 enum tpacpi_brightness_access_mode {
6416 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6417 TPACPI_BRGHT_MODE_EC, /* EC control */
6418 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6419 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6420 TPACPI_BRGHT_MODE_MAX
6423 static struct backlight_device *ibm_backlight_device;
6425 static enum tpacpi_brightness_access_mode brightness_mode =
6426 TPACPI_BRGHT_MODE_MAX;
6428 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6430 static struct mutex brightness_mutex;
6432 /* NVRAM brightness access,
6433 * call with brightness_mutex held! */
6434 static unsigned int tpacpi_brightness_nvram_get(void)
6438 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6439 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6440 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6441 lnvram &= bright_maxlvl;
6446 static void tpacpi_brightness_checkpoint_nvram(void)
6451 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6454 vdbg_printk(TPACPI_DBG_BRGHT,
6455 "trying to checkpoint backlight level to NVRAM...\n");
6457 if (mutex_lock_killable(&brightness_mutex) < 0)
6460 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6462 lec &= TP_EC_BACKLIGHT_LVLMSK;
6463 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6465 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6466 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6467 /* NVRAM needs update */
6468 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6469 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6471 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6472 dbg_printk(TPACPI_DBG_BRGHT,
6473 "updated NVRAM backlight level to %u (0x%02x)\n",
6474 (unsigned int) lec, (unsigned int) b_nvram);
6476 vdbg_printk(TPACPI_DBG_BRGHT,
6477 "NVRAM backlight level already is %u (0x%02x)\n",
6478 (unsigned int) lec, (unsigned int) b_nvram);
6481 mutex_unlock(&brightness_mutex);
6485 /* call with brightness_mutex held! */
6486 static int tpacpi_brightness_get_raw(int *status)
6490 switch (brightness_mode) {
6491 case TPACPI_BRGHT_MODE_UCMS_STEP:
6492 *status = tpacpi_brightness_nvram_get();
6494 case TPACPI_BRGHT_MODE_EC:
6495 case TPACPI_BRGHT_MODE_ECNVRAM:
6496 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6505 /* call with brightness_mutex held! */
6506 /* do NOT call with illegal backlight level value */
6507 static int tpacpi_brightness_set_ec(unsigned int value)
6511 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6514 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6515 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6516 (value & TP_EC_BACKLIGHT_LVLMSK))))
6522 /* call with brightness_mutex held! */
6523 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6526 unsigned int current_value, i;
6528 current_value = tpacpi_brightness_nvram_get();
6530 if (value == current_value)
6533 cmos_cmd = (value > current_value) ?
6534 TP_CMOS_BRIGHTNESS_UP :
6535 TP_CMOS_BRIGHTNESS_DOWN;
6536 inc = (value > current_value) ? 1 : -1;
6538 for (i = current_value; i != value; i += inc)
6539 if (issue_thinkpad_cmos_command(cmos_cmd))
6545 /* May return EINTR which can always be mapped to ERESTARTSYS */
6546 static int brightness_set(unsigned int value)
6550 if (value > bright_maxlvl)
6553 vdbg_printk(TPACPI_DBG_BRGHT,
6554 "set backlight level to %d\n", value);
6556 res = mutex_lock_killable(&brightness_mutex);
6560 switch (brightness_mode) {
6561 case TPACPI_BRGHT_MODE_EC:
6562 case TPACPI_BRGHT_MODE_ECNVRAM:
6563 res = tpacpi_brightness_set_ec(value);
6565 case TPACPI_BRGHT_MODE_UCMS_STEP:
6566 res = tpacpi_brightness_set_ucmsstep(value);
6572 mutex_unlock(&brightness_mutex);
6576 /* sysfs backlight class ----------------------------------------------- */
6578 static int brightness_update_status(struct backlight_device *bd)
6580 unsigned int level =
6581 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6582 bd->props.power == FB_BLANK_UNBLANK) ?
6583 bd->props.brightness : 0;
6585 dbg_printk(TPACPI_DBG_BRGHT,
6586 "backlight: attempt to set level to %d\n",
6589 /* it is the backlight class's job (caller) to handle
6590 * EINTR and other errors properly */
6591 return brightness_set(level);
6594 static int brightness_get(struct backlight_device *bd)
6598 res = mutex_lock_killable(&brightness_mutex);
6602 res = tpacpi_brightness_get_raw(&status);
6604 mutex_unlock(&brightness_mutex);
6609 return status & TP_EC_BACKLIGHT_LVLMSK;
6612 static void tpacpi_brightness_notify_change(void)
6614 backlight_force_update(ibm_backlight_device,
6615 BACKLIGHT_UPDATE_HOTKEY);
6618 static const struct backlight_ops ibm_backlight_data = {
6619 .get_brightness = brightness_get,
6620 .update_status = brightness_update_status,
6623 /* --------------------------------------------------------------------- */
6626 * Call _BCL method of video device. On some ThinkPads this will
6627 * switch the firmware to the ACPI brightness control mode.
6630 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6632 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6633 union acpi_object *obj;
6634 struct acpi_device *device, *child;
6637 if (acpi_bus_get_device(handle, &device))
6641 list_for_each_entry(child, &device->children, node) {
6642 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6644 if (ACPI_FAILURE(status)) {
6645 buffer.length = ACPI_ALLOCATE_BUFFER;
6649 obj = (union acpi_object *)buffer.pointer;
6650 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6651 pr_err("Unknown _BCL data, please report this to %s\n",
6655 rc = obj->package.count;
6660 kfree(buffer.pointer);
6666 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6668 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6670 acpi_handle video_device;
6673 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6675 bcl_levels = tpacpi_query_bcl_levels(video_device);
6677 tp_features.bright_acpimode = (bcl_levels > 0);
6679 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6683 * These are only useful for models that have only one possibility
6684 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6687 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6688 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6689 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6691 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6692 /* Models with ATI GPUs known to require ECNVRAM mode */
6693 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6695 /* Models with ATI GPUs that can use ECNVRAM */
6696 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6697 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6698 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6699 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6701 /* Models with Intel Extreme Graphics 2 */
6702 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6703 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6704 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6706 /* Models with Intel GMA900 */
6707 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6708 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6709 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6713 * Returns < 0 for error, otherwise sets tp_features.bright_*
6714 * and bright_maxlvl.
6716 static void __init tpacpi_detect_brightness_capabilities(void)
6720 vdbg_printk(TPACPI_DBG_INIT,
6721 "detecting firmware brightness interface capabilities\n");
6723 /* we could run a quirks check here (same table used by
6724 * brightness_init) if needed */
6727 * We always attempt to detect acpi support, so as to switch
6728 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6729 * going to publish a backlight interface
6731 b = tpacpi_check_std_acpi_brightness_support();
6741 tp_features.bright_unkfw = 1;
6742 bright_maxlvl = b - 1;
6744 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6747 static int __init brightness_init(struct ibm_init_struct *iibm)
6749 struct backlight_properties props;
6751 unsigned long quirks;
6753 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6755 mutex_init(&brightness_mutex);
6757 quirks = tpacpi_check_quirks(brightness_quirk_table,
6758 ARRAY_SIZE(brightness_quirk_table));
6760 /* tpacpi_detect_brightness_capabilities() must have run already */
6762 /* if it is unknown, we don't handle it: it wouldn't be safe */
6763 if (tp_features.bright_unkfw)
6766 if (!brightness_enable) {
6767 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6768 "brightness support disabled by "
6769 "module parameter\n");
6773 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6774 if (brightness_enable > 1) {
6775 pr_info("Standard ACPI backlight interface "
6776 "available, not loading native one\n");
6778 } else if (brightness_enable == 1) {
6779 pr_warn("Cannot enable backlight brightness support, "
6780 "ACPI is already handling it. Refer to the "
6781 "acpi_backlight kernel parameter.\n");
6784 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6785 pr_notice("Standard ACPI backlight interface not "
6786 "available, thinkpad_acpi native "
6787 "brightness control enabled\n");
6791 * Check for module parameter bogosity, note that we
6792 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6793 * able to detect "unspecified"
6795 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6798 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6799 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6800 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6801 if (quirks & TPACPI_BRGHT_Q_EC)
6802 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6804 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6806 dbg_printk(TPACPI_DBG_BRGHT,
6807 "driver auto-selected brightness_mode=%d\n",
6812 if (!tpacpi_is_ibm() &&
6813 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6814 brightness_mode == TPACPI_BRGHT_MODE_EC))
6817 if (tpacpi_brightness_get_raw(&b) < 0)
6820 memset(&props, 0, sizeof(struct backlight_properties));
6821 props.type = BACKLIGHT_PLATFORM;
6822 props.max_brightness = bright_maxlvl;
6823 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6824 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6826 &ibm_backlight_data,
6828 if (IS_ERR(ibm_backlight_device)) {
6829 int rc = PTR_ERR(ibm_backlight_device);
6830 ibm_backlight_device = NULL;
6831 pr_err("Could not register backlight device\n");
6834 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6835 "brightness is supported\n");
6837 if (quirks & TPACPI_BRGHT_Q_ASK) {
6838 pr_notice("brightness: will use unverified default: "
6839 "brightness_mode=%d\n", brightness_mode);
6840 pr_notice("brightness: please report to %s whether it works well "
6841 "or not on your ThinkPad\n", TPACPI_MAIL);
6844 /* Added by mistake in early 2007. Probably useless, but it could
6845 * be working around some unknown firmware problem where the value
6846 * read at startup doesn't match the real hardware state... so leave
6847 * it in place just in case */
6848 backlight_update_status(ibm_backlight_device);
6850 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6851 "brightness: registering brightness hotkeys "
6852 "as change notification\n");
6853 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6854 | TP_ACPI_HKEY_BRGHTUP_MASK
6855 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6859 static void brightness_suspend(void)
6861 tpacpi_brightness_checkpoint_nvram();
6864 static void brightness_shutdown(void)
6866 tpacpi_brightness_checkpoint_nvram();
6869 static void brightness_exit(void)
6871 if (ibm_backlight_device) {
6872 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6873 "calling backlight_device_unregister()\n");
6874 backlight_device_unregister(ibm_backlight_device);
6877 tpacpi_brightness_checkpoint_nvram();
6880 static int brightness_read(struct seq_file *m)
6884 level = brightness_get(NULL);
6886 seq_printf(m, "level:\t\tunreadable\n");
6888 seq_printf(m, "level:\t\t%d\n", level);
6889 seq_printf(m, "commands:\tup, down\n");
6890 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6897 static int brightness_write(char *buf)
6903 level = brightness_get(NULL);
6907 while ((cmd = next_cmd(&buf))) {
6908 if (strlencmp(cmd, "up") == 0) {
6909 if (level < bright_maxlvl)
6911 } else if (strlencmp(cmd, "down") == 0) {
6914 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6915 level >= 0 && level <= bright_maxlvl) {
6921 tpacpi_disclose_usertask("procfs brightness",
6922 "set level to %d\n", level);
6925 * Now we know what the final level should be, so we try to set it.
6926 * Doing it this way makes the syscall restartable in case of EINTR
6928 rc = brightness_set(level);
6929 if (!rc && ibm_backlight_device)
6930 backlight_force_update(ibm_backlight_device,
6931 BACKLIGHT_UPDATE_SYSFS);
6932 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6935 static struct ibm_struct brightness_driver_data = {
6936 .name = "brightness",
6937 .read = brightness_read,
6938 .write = brightness_write,
6939 .exit = brightness_exit,
6940 .suspend = brightness_suspend,
6941 .shutdown = brightness_shutdown,
6944 /*************************************************************************
6949 * IBM ThinkPads have a simple volume controller with MUTE gating.
6950 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6952 * Since the *61 series (and probably also the later *60 series), Lenovo
6953 * ThinkPads only implement the MUTE gate.
6956 * Bit 6: MUTE (1 mutes sound)
6958 * Other bits should be zero as far as we know.
6960 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6961 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6962 * such as bit 7 which is used to detect repeated presses of MUTE,
6963 * and we leave them unchanged.
6965 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6966 * in response to user input. Unfortunately, this rarely works well.
6967 * The laptop changes the state of its internal MUTE gate and, on some
6968 * models, sends KEY_MUTE, causing any user code that responds to the
6969 * mute button to get confused. The hardware MUTE gate is also
6970 * unnecessary, since user code can handle the mute button without
6971 * kernel or EC help.
6973 * To avoid confusing userspace, we simply disable all EC-based mute
6974 * and volume controls when possible.
6977 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6979 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6980 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6981 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6983 #if SNDRV_CARDS <= 32
6984 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
6986 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
6988 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
6989 static char *alsa_id = "ThinkPadEC";
6990 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
6992 struct tpacpi_alsa_data {
6993 struct snd_card *card;
6994 struct snd_ctl_elem_id *ctl_mute_id;
6995 struct snd_ctl_elem_id *ctl_vol_id;
6998 static struct snd_card *alsa_card;
7003 /* TP_EC_AUDIO bits */
7004 TP_EC_AUDIO_MUTESW = 6,
7006 /* TP_EC_AUDIO bitmasks */
7007 TP_EC_AUDIO_LVL_MSK = 0x0F,
7008 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7010 /* Maximum volume */
7011 TP_EC_VOLUME_MAX = 14,
7014 enum tpacpi_volume_access_mode {
7015 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7016 TPACPI_VOL_MODE_EC, /* Pure EC control */
7017 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7018 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7022 enum tpacpi_volume_capabilities {
7023 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7024 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7025 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7029 enum tpacpi_mute_btn_mode {
7030 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7031 /* We don't know what mode 1 is. */
7032 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7033 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7036 static enum tpacpi_volume_access_mode volume_mode =
7037 TPACPI_VOL_MODE_MAX;
7039 static enum tpacpi_volume_capabilities volume_capabilities;
7040 static bool volume_control_allowed;
7041 static bool software_mute_requested = true;
7042 static bool software_mute_active;
7043 static int software_mute_orig_mode;
7046 * Used to syncronize writers to TP_EC_AUDIO and
7047 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7049 static struct mutex volume_mutex;
7051 static void tpacpi_volume_checkpoint_nvram(void)
7057 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7059 if (!volume_control_allowed)
7061 if (software_mute_active)
7064 vdbg_printk(TPACPI_DBG_MIXER,
7065 "trying to checkpoint mixer state to NVRAM...\n");
7067 if (tp_features.mixer_no_level_control)
7068 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7070 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7072 if (mutex_lock_killable(&volume_mutex) < 0)
7075 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7078 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7080 if (lec != (b_nvram & ec_mask)) {
7081 /* NVRAM needs update */
7082 b_nvram &= ~ec_mask;
7084 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7085 dbg_printk(TPACPI_DBG_MIXER,
7086 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7087 (unsigned int) lec, (unsigned int) b_nvram);
7089 vdbg_printk(TPACPI_DBG_MIXER,
7090 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7091 (unsigned int) lec, (unsigned int) b_nvram);
7095 mutex_unlock(&volume_mutex);
7098 static int volume_get_status_ec(u8 *status)
7102 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7107 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7112 static int volume_get_status(u8 *status)
7114 return volume_get_status_ec(status);
7117 static int volume_set_status_ec(const u8 status)
7119 if (!acpi_ec_write(TP_EC_AUDIO, status))
7122 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7125 * On X200s, and possibly on others, it can take a while for
7126 * reads to become correct.
7133 static int volume_set_status(const u8 status)
7135 return volume_set_status_ec(status);
7138 /* returns < 0 on error, 0 on no change, 1 on change */
7139 static int __volume_set_mute_ec(const bool mute)
7144 if (mutex_lock_killable(&volume_mutex) < 0)
7147 rc = volume_get_status_ec(&s);
7151 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7152 s & ~TP_EC_AUDIO_MUTESW_MSK;
7155 rc = volume_set_status_ec(n);
7161 mutex_unlock(&volume_mutex);
7165 static int volume_alsa_set_mute(const bool mute)
7167 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7168 (mute) ? "" : "un");
7169 return __volume_set_mute_ec(mute);
7172 static int volume_set_mute(const bool mute)
7176 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7177 (mute) ? "" : "un");
7179 rc = __volume_set_mute_ec(mute);
7180 return (rc < 0) ? rc : 0;
7183 /* returns < 0 on error, 0 on no change, 1 on change */
7184 static int __volume_set_volume_ec(const u8 vol)
7189 if (vol > TP_EC_VOLUME_MAX)
7192 if (mutex_lock_killable(&volume_mutex) < 0)
7195 rc = volume_get_status_ec(&s);
7199 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7202 rc = volume_set_status_ec(n);
7208 mutex_unlock(&volume_mutex);
7212 static int volume_set_software_mute(bool startup)
7216 if (!tpacpi_is_lenovo())
7220 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7224 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7225 "Initial HAUM setting was %d\n",
7226 software_mute_orig_mode);
7229 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7230 (int)TP_EC_MUTE_BTN_NONE))
7233 if (result != TP_EC_MUTE_BTN_NONE)
7234 pr_warn("Unexpected SAUM result %d\n",
7238 * In software mute mode, the standard codec controls take
7239 * precendence, so we unmute the ThinkPad HW switch at
7240 * startup. Just on case there are SAUM-capable ThinkPads
7241 * with level controls, set max HW volume as well.
7243 if (tp_features.mixer_no_level_control)
7244 result = volume_set_mute(false);
7246 result = volume_set_status(TP_EC_VOLUME_MAX);
7249 pr_warn("Failed to unmute the HW mute switch\n");
7254 static void volume_exit_software_mute(void)
7258 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7259 || r != software_mute_orig_mode)
7260 pr_warn("Failed to restore mute mode\n");
7263 static int volume_alsa_set_volume(const u8 vol)
7265 dbg_printk(TPACPI_DBG_MIXER,
7266 "ALSA: trying to set volume level to %hu\n", vol);
7267 return __volume_set_volume_ec(vol);
7270 static void volume_alsa_notify_change(void)
7272 struct tpacpi_alsa_data *d;
7274 if (alsa_card && alsa_card->private_data) {
7275 d = alsa_card->private_data;
7277 snd_ctl_notify(alsa_card,
7278 SNDRV_CTL_EVENT_MASK_VALUE,
7281 snd_ctl_notify(alsa_card,
7282 SNDRV_CTL_EVENT_MASK_VALUE,
7287 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7288 struct snd_ctl_elem_info *uinfo)
7290 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7292 uinfo->value.integer.min = 0;
7293 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7297 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7298 struct snd_ctl_elem_value *ucontrol)
7303 rc = volume_get_status(&s);
7307 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7311 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7312 struct snd_ctl_elem_value *ucontrol)
7314 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7315 ucontrol->value.integer.value[0]);
7316 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7319 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7321 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7322 struct snd_ctl_elem_value *ucontrol)
7327 rc = volume_get_status(&s);
7331 ucontrol->value.integer.value[0] =
7332 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7336 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7337 struct snd_ctl_elem_value *ucontrol)
7339 tpacpi_disclose_usertask("ALSA", "%smute\n",
7340 ucontrol->value.integer.value[0] ?
7342 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7345 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7346 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7347 .name = "Console Playback Volume",
7349 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7350 .info = volume_alsa_vol_info,
7351 .get = volume_alsa_vol_get,
7354 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7355 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7356 .name = "Console Playback Switch",
7358 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7359 .info = volume_alsa_mute_info,
7360 .get = volume_alsa_mute_get,
7363 static void volume_suspend(void)
7365 tpacpi_volume_checkpoint_nvram();
7368 static void volume_resume(void)
7370 if (software_mute_active) {
7371 if (volume_set_software_mute(false) < 0)
7372 pr_warn("Failed to restore software mute\n");
7374 volume_alsa_notify_change();
7378 static void volume_shutdown(void)
7380 tpacpi_volume_checkpoint_nvram();
7383 static void volume_exit(void)
7386 snd_card_free(alsa_card);
7390 tpacpi_volume_checkpoint_nvram();
7392 if (software_mute_active)
7393 volume_exit_software_mute();
7396 static int __init volume_create_alsa_mixer(void)
7398 struct snd_card *card;
7399 struct tpacpi_alsa_data *data;
7400 struct snd_kcontrol *ctl_vol;
7401 struct snd_kcontrol *ctl_mute;
7404 rc = snd_card_new(&tpacpi_pdev->dev,
7405 alsa_index, alsa_id, THIS_MODULE,
7406 sizeof(struct tpacpi_alsa_data), &card);
7407 if (rc < 0 || !card) {
7408 pr_err("Failed to create ALSA card structures: %d\n", rc);
7412 BUG_ON(!card->private_data);
7413 data = card->private_data;
7416 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7417 sizeof(card->driver));
7418 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7419 sizeof(card->shortname));
7420 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7421 (thinkpad_id.ec_version_str) ?
7422 thinkpad_id.ec_version_str : "(unknown)");
7423 snprintf(card->longname, sizeof(card->longname),
7424 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7425 (thinkpad_id.ec_version_str) ?
7426 thinkpad_id.ec_version_str : "unknown");
7428 if (volume_control_allowed) {
7429 volume_alsa_control_vol.put = volume_alsa_vol_put;
7430 volume_alsa_control_vol.access =
7431 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7433 volume_alsa_control_mute.put = volume_alsa_mute_put;
7434 volume_alsa_control_mute.access =
7435 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7438 if (!tp_features.mixer_no_level_control) {
7439 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7440 rc = snd_ctl_add(card, ctl_vol);
7442 pr_err("Failed to create ALSA volume control: %d\n",
7446 data->ctl_vol_id = &ctl_vol->id;
7449 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7450 rc = snd_ctl_add(card, ctl_mute);
7452 pr_err("Failed to create ALSA mute control: %d\n", rc);
7455 data->ctl_mute_id = &ctl_mute->id;
7457 rc = snd_card_register(card);
7459 pr_err("Failed to register ALSA card: %d\n", rc);
7467 snd_card_free(card);
7471 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7472 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7474 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7475 /* Whitelist volume level on all IBM by default */
7476 { .vendor = PCI_VENDOR_ID_IBM,
7477 .bios = TPACPI_MATCH_ANY,
7478 .ec = TPACPI_MATCH_ANY,
7479 .quirks = TPACPI_VOL_Q_LEVEL },
7481 /* Lenovo models with volume control (needs confirmation) */
7482 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7483 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7484 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7485 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7486 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7487 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7488 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7490 /* Whitelist mute-only on all Lenovo by default */
7491 { .vendor = PCI_VENDOR_ID_LENOVO,
7492 .bios = TPACPI_MATCH_ANY,
7493 .ec = TPACPI_MATCH_ANY,
7494 .quirks = TPACPI_VOL_Q_MUTEONLY }
7497 static int __init volume_init(struct ibm_init_struct *iibm)
7499 unsigned long quirks;
7502 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7504 mutex_init(&volume_mutex);
7507 * Check for module parameter bogosity, note that we
7508 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7509 * able to detect "unspecified"
7511 if (volume_mode > TPACPI_VOL_MODE_MAX)
7514 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7515 pr_err("UCMS step volume mode not implemented, "
7516 "please contact %s\n", TPACPI_MAIL);
7520 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7524 * The ALSA mixer is our primary interface.
7525 * When disabled, don't install the subdriver at all
7528 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7529 "ALSA mixer disabled by parameter, "
7530 "not loading volume subdriver...\n");
7534 quirks = tpacpi_check_quirks(volume_quirk_table,
7535 ARRAY_SIZE(volume_quirk_table));
7537 switch (volume_capabilities) {
7538 case TPACPI_VOL_CAP_AUTO:
7539 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7540 tp_features.mixer_no_level_control = 1;
7541 else if (quirks & TPACPI_VOL_Q_LEVEL)
7542 tp_features.mixer_no_level_control = 0;
7544 return 1; /* no mixer */
7546 case TPACPI_VOL_CAP_VOLMUTE:
7547 tp_features.mixer_no_level_control = 0;
7549 case TPACPI_VOL_CAP_MUTEONLY:
7550 tp_features.mixer_no_level_control = 1;
7556 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7557 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7558 "using user-supplied volume_capabilities=%d\n",
7559 volume_capabilities);
7561 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7562 volume_mode == TPACPI_VOL_MODE_MAX) {
7563 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7565 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7566 "driver auto-selected volume_mode=%d\n",
7569 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7570 "using user-supplied volume_mode=%d\n",
7574 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7575 "mute is supported, volume control is %s\n",
7576 str_supported(!tp_features.mixer_no_level_control));
7578 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7579 software_mute_active = true;
7581 rc = volume_create_alsa_mixer();
7583 pr_err("Could not create the ALSA mixer interface\n");
7587 pr_info("Console audio control enabled, mode: %s\n",
7588 (volume_control_allowed) ?
7589 "override (read/write)" :
7590 "monitor (read only)");
7593 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7594 "registering volume hotkeys as change notification\n");
7595 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7596 | TP_ACPI_HKEY_VOLUP_MASK
7597 | TP_ACPI_HKEY_VOLDWN_MASK
7598 | TP_ACPI_HKEY_MUTE_MASK);
7603 static int volume_read(struct seq_file *m)
7607 if (volume_get_status(&status) < 0) {
7608 seq_printf(m, "level:\t\tunreadable\n");
7610 if (tp_features.mixer_no_level_control)
7611 seq_printf(m, "level:\t\tunsupported\n");
7613 seq_printf(m, "level:\t\t%d\n",
7614 status & TP_EC_AUDIO_LVL_MSK);
7616 seq_printf(m, "mute:\t\t%s\n",
7617 onoff(status, TP_EC_AUDIO_MUTESW));
7619 if (volume_control_allowed) {
7620 seq_printf(m, "commands:\tunmute, mute\n");
7621 if (!tp_features.mixer_no_level_control) {
7623 "commands:\tup, down\n");
7625 "commands:\tlevel <level>"
7626 " (<level> is 0-%d)\n",
7635 static int volume_write(char *buf)
7638 u8 new_level, new_mute;
7644 * We do allow volume control at driver startup, so that the
7645 * user can set initial state through the volume=... parameter hack.
7647 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7648 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7649 tp_warned.volume_ctrl_forbidden = 1;
7650 pr_notice("Console audio control in monitor mode, "
7651 "changes are not allowed\n");
7652 pr_notice("Use the volume_control=1 module parameter "
7653 "to enable volume control\n");
7658 rc = volume_get_status(&s);
7662 new_level = s & TP_EC_AUDIO_LVL_MSK;
7663 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7665 while ((cmd = next_cmd(&buf))) {
7666 if (!tp_features.mixer_no_level_control) {
7667 if (strlencmp(cmd, "up") == 0) {
7670 else if (new_level < TP_EC_VOLUME_MAX)
7673 } else if (strlencmp(cmd, "down") == 0) {
7676 else if (new_level > 0)
7679 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7680 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7685 if (strlencmp(cmd, "mute") == 0)
7686 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7687 else if (strlencmp(cmd, "unmute") == 0)
7693 if (tp_features.mixer_no_level_control) {
7694 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7695 new_mute ? "" : "un");
7696 rc = volume_set_mute(!!new_mute);
7698 tpacpi_disclose_usertask("procfs volume",
7699 "%smute and set level to %d\n",
7700 new_mute ? "" : "un", new_level);
7701 rc = volume_set_status(new_mute | new_level);
7703 volume_alsa_notify_change();
7705 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7708 static struct ibm_struct volume_driver_data = {
7710 .read = volume_read,
7711 .write = volume_write,
7712 .exit = volume_exit,
7713 .suspend = volume_suspend,
7714 .resume = volume_resume,
7715 .shutdown = volume_shutdown,
7718 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7720 #define alsa_card NULL
7722 static void inline volume_alsa_notify_change(void)
7726 static int __init volume_init(struct ibm_init_struct *iibm)
7728 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7733 static struct ibm_struct volume_driver_data = {
7737 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7739 /*************************************************************************
7746 * TPACPI_FAN_RD_ACPI_GFAN:
7747 * ACPI GFAN method: returns fan level
7749 * see TPACPI_FAN_WR_ACPI_SFAN
7750 * EC 0x2f (HFSP) not available if GFAN exists
7752 * TPACPI_FAN_WR_ACPI_SFAN:
7753 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7755 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7758 * TPACPI_FAN_WR_TPEC:
7759 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7760 * Supported on almost all ThinkPads
7762 * Fan speed changes of any sort (including those caused by the
7763 * disengaged mode) are usually done slowly by the firmware as the
7764 * maximum amount of fan duty cycle change per second seems to be
7767 * Reading is not available if GFAN exists.
7768 * Writing is not available if SFAN exists.
7771 * 7 automatic mode engaged;
7772 * (default operation mode of the ThinkPad)
7773 * fan level is ignored in this mode.
7774 * 6 full speed mode (takes precedence over bit 7);
7775 * not available on all thinkpads. May disable
7776 * the tachometer while the fan controller ramps up
7777 * the speed (which can take up to a few *minutes*).
7778 * Speeds up fan to 100% duty-cycle, which is far above
7779 * the standard RPM levels. It is not impossible that
7780 * it could cause hardware damage.
7781 * 5-3 unused in some models. Extra bits for fan level
7782 * in others, but still useless as all values above
7783 * 7 map to the same speed as level 7 in these models.
7784 * 2-0 fan level (0..7 usually)
7786 * 0x07 = max (set when temperatures critical)
7787 * Some ThinkPads may have other levels, see
7788 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7790 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7791 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7792 * does so, its initial value is meaningless (0x07).
7794 * For firmware bugs, refer to:
7795 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7799 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7800 * Main fan tachometer reading (in RPM)
7802 * This register is present on all ThinkPads with a new-style EC, and
7803 * it is known not to be present on the A21m/e, and T22, as there is
7804 * something else in offset 0x84 according to the ACPI DSDT. Other
7805 * ThinkPads from this same time period (and earlier) probably lack the
7806 * tachometer as well.
7808 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7809 * was never fixed by IBM to report the EC firmware version string
7810 * probably support the tachometer (like the early X models), so
7811 * detecting it is quite hard. We need more data to know for sure.
7813 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7816 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7819 * For firmware bugs, refer to:
7820 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7824 * ThinkPad EC register 0x31 bit 0 (only on select models)
7826 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7827 * show the speed of the main fan. When bit 0 of EC register 0x31
7828 * is one, the tachometer registers show the speed of the auxiliary
7831 * Fan control seems to affect both fans, regardless of the state
7834 * So far, only the firmware for the X60/X61 non-tablet versions
7835 * seem to support this (firmware TP-7M).
7837 * TPACPI_FAN_WR_ACPI_FANS:
7838 * ThinkPad X31, X40, X41. Not available in the X60.
7840 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7841 * high speed. ACPI DSDT seems to map these three speeds to levels
7842 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7843 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7845 * The speeds are stored on handles
7846 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7848 * There are three default speed sets, accessible as handles:
7849 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7851 * ACPI DSDT switches which set is in use depending on various
7854 * TPACPI_FAN_WR_TPEC is also available and should be used to
7855 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7856 * but the ACPI tables just mention level 7.
7859 enum { /* Fan control constants */
7860 fan_status_offset = 0x2f, /* EC register 0x2f */
7861 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7862 * 0x84 must be read before 0x85 */
7863 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7864 bit 0 selects which fan is active */
7866 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7867 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7869 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7872 enum fan_status_access_mode {
7873 TPACPI_FAN_NONE = 0, /* No fan status or control */
7874 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7875 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7878 enum fan_control_access_mode {
7879 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7880 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7881 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7882 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7885 enum fan_control_commands {
7886 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7887 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7888 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7889 * and also watchdog cmd */
7892 static bool fan_control_allowed;
7894 static enum fan_status_access_mode fan_status_access_mode;
7895 static enum fan_control_access_mode fan_control_access_mode;
7896 static enum fan_control_commands fan_control_commands;
7898 static u8 fan_control_initial_status;
7899 static u8 fan_control_desired_level;
7900 static u8 fan_control_resume_level;
7901 static int fan_watchdog_maxinterval;
7903 static struct mutex fan_mutex;
7905 static void fan_watchdog_fire(struct work_struct *ignored);
7906 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7908 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7909 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7910 "\\FSPD", /* 600e/x, 770e, 770x */
7912 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7913 "JFNS", /* 770x-JL */
7917 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7918 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7919 * be in auto mode (0x80).
7921 * This is corrected by any write to HFSP either by the driver, or
7924 * We assume 0x07 really means auto mode while this quirk is active,
7925 * as this is far more likely than the ThinkPad being in level 7,
7926 * which is only used by the firmware during thermal emergencies.
7928 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7929 * TP-70 (T43, R52), which are known to be buggy.
7932 static void fan_quirk1_setup(void)
7934 if (fan_control_initial_status == 0x07) {
7935 pr_notice("fan_init: initial fan status is unknown, "
7936 "assuming it is in auto mode\n");
7937 tp_features.fan_ctrl_status_undef = 1;
7941 static void fan_quirk1_handle(u8 *fan_status)
7943 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7944 if (*fan_status != fan_control_initial_status) {
7945 /* something changed the HFSP regisnter since
7946 * driver init time, so it is not undefined
7948 tp_features.fan_ctrl_status_undef = 0;
7950 /* Return most likely status. In fact, it
7951 * might be the only possible status */
7952 *fan_status = TP_EC_FAN_AUTO;
7957 /* Select main fan on X60/X61, NOOP on others */
7958 static bool fan_select_fan1(void)
7960 if (tp_features.second_fan) {
7963 if (ec_read(fan_select_offset, &val) < 0)
7966 if (ec_write(fan_select_offset, val) < 0)
7972 /* Select secondary fan on X60/X61 */
7973 static bool fan_select_fan2(void)
7977 if (!tp_features.second_fan)
7980 if (ec_read(fan_select_offset, &val) < 0)
7983 if (ec_write(fan_select_offset, val) < 0)
7990 * Call with fan_mutex held
7992 static void fan_update_desired_level(u8 status)
7995 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7997 fan_control_desired_level = 7;
7999 fan_control_desired_level = status;
8003 static int fan_get_status(u8 *status)
8008 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8010 switch (fan_status_access_mode) {
8011 case TPACPI_FAN_RD_ACPI_GFAN: {
8012 /* 570, 600e/x, 770e, 770x */
8015 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8019 *status = res & 0x07;
8023 case TPACPI_FAN_RD_TPEC:
8024 /* all except 570, 600e/x, 770e, 770x */
8025 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8028 if (likely(status)) {
8030 fan_quirk1_handle(status);
8042 static int fan_get_status_safe(u8 *status)
8047 if (mutex_lock_killable(&fan_mutex))
8048 return -ERESTARTSYS;
8049 rc = fan_get_status(&s);
8051 fan_update_desired_level(s);
8052 mutex_unlock(&fan_mutex);
8062 static int fan_get_speed(unsigned int *speed)
8066 switch (fan_status_access_mode) {
8067 case TPACPI_FAN_RD_TPEC:
8068 /* all except 570, 600e/x, 770e, 770x */
8069 if (unlikely(!fan_select_fan1()))
8071 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8072 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8076 *speed = (hi << 8) | lo;
8087 static int fan2_get_speed(unsigned int *speed)
8092 switch (fan_status_access_mode) {
8093 case TPACPI_FAN_RD_TPEC:
8094 /* all except 570, 600e/x, 770e, 770x */
8095 if (unlikely(!fan_select_fan2()))
8097 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8098 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8099 fan_select_fan1(); /* play it safe */
8104 *speed = (hi << 8) | lo;
8115 static int fan_set_level(int level)
8117 if (!fan_control_allowed)
8120 switch (fan_control_access_mode) {
8121 case TPACPI_FAN_WR_ACPI_SFAN:
8122 if (level >= 0 && level <= 7) {
8123 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8129 case TPACPI_FAN_WR_ACPI_FANS:
8130 case TPACPI_FAN_WR_TPEC:
8131 if (!(level & TP_EC_FAN_AUTO) &&
8132 !(level & TP_EC_FAN_FULLSPEED) &&
8133 ((level < 0) || (level > 7)))
8136 /* safety net should the EC not support AUTO
8137 * or FULLSPEED mode bits and just ignore them */
8138 if (level & TP_EC_FAN_FULLSPEED)
8139 level |= 7; /* safety min speed 7 */
8140 else if (level & TP_EC_FAN_AUTO)
8141 level |= 4; /* safety min speed 4 */
8143 if (!acpi_ec_write(fan_status_offset, level))
8146 tp_features.fan_ctrl_status_undef = 0;
8153 vdbg_printk(TPACPI_DBG_FAN,
8154 "fan control: set fan control register to 0x%02x\n", level);
8158 static int fan_set_level_safe(int level)
8162 if (!fan_control_allowed)
8165 if (mutex_lock_killable(&fan_mutex))
8166 return -ERESTARTSYS;
8168 if (level == TPACPI_FAN_LAST_LEVEL)
8169 level = fan_control_desired_level;
8171 rc = fan_set_level(level);
8173 fan_update_desired_level(level);
8175 mutex_unlock(&fan_mutex);
8179 static int fan_set_enable(void)
8184 if (!fan_control_allowed)
8187 if (mutex_lock_killable(&fan_mutex))
8188 return -ERESTARTSYS;
8190 switch (fan_control_access_mode) {
8191 case TPACPI_FAN_WR_ACPI_FANS:
8192 case TPACPI_FAN_WR_TPEC:
8193 rc = fan_get_status(&s);
8197 /* Don't go out of emergency fan mode */
8200 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8203 if (!acpi_ec_write(fan_status_offset, s))
8206 tp_features.fan_ctrl_status_undef = 0;
8211 case TPACPI_FAN_WR_ACPI_SFAN:
8212 rc = fan_get_status(&s);
8218 /* Set fan to at least level 4 */
8221 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8231 mutex_unlock(&fan_mutex);
8234 vdbg_printk(TPACPI_DBG_FAN,
8235 "fan control: set fan control register to 0x%02x\n",
8240 static int fan_set_disable(void)
8244 if (!fan_control_allowed)
8247 if (mutex_lock_killable(&fan_mutex))
8248 return -ERESTARTSYS;
8251 switch (fan_control_access_mode) {
8252 case TPACPI_FAN_WR_ACPI_FANS:
8253 case TPACPI_FAN_WR_TPEC:
8254 if (!acpi_ec_write(fan_status_offset, 0x00))
8257 fan_control_desired_level = 0;
8258 tp_features.fan_ctrl_status_undef = 0;
8262 case TPACPI_FAN_WR_ACPI_SFAN:
8263 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8266 fan_control_desired_level = 0;
8274 vdbg_printk(TPACPI_DBG_FAN,
8275 "fan control: set fan control register to 0\n");
8277 mutex_unlock(&fan_mutex);
8281 static int fan_set_speed(int speed)
8285 if (!fan_control_allowed)
8288 if (mutex_lock_killable(&fan_mutex))
8289 return -ERESTARTSYS;
8292 switch (fan_control_access_mode) {
8293 case TPACPI_FAN_WR_ACPI_FANS:
8294 if (speed >= 0 && speed <= 65535) {
8295 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8296 speed, speed, speed))
8306 mutex_unlock(&fan_mutex);
8310 static void fan_watchdog_reset(void)
8312 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8315 if (fan_watchdog_maxinterval > 0 &&
8316 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8317 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8318 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8320 cancel_delayed_work(&fan_watchdog_task);
8323 static void fan_watchdog_fire(struct work_struct *ignored)
8327 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8330 pr_notice("fan watchdog: enabling fan\n");
8331 rc = fan_set_enable();
8333 pr_err("fan watchdog: error %d while enabling fan, "
8334 "will try again later...\n", -rc);
8335 /* reschedule for later */
8336 fan_watchdog_reset();
8341 * SYSFS fan layout: hwmon compatible (device)
8344 * 0: "disengaged" mode
8346 * 2: native EC "auto" mode (recommended, hardware default)
8348 * pwm*: set speed in manual mode, ignored otherwise.
8349 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8352 * fan*_input: tachometer reading, RPM
8355 * SYSFS fan layout: extensions
8357 * fan_watchdog (driver):
8358 * fan watchdog interval in seconds, 0 disables (default), max 120
8361 /* sysfs fan pwm1_enable ----------------------------------------------- */
8362 static ssize_t fan_pwm1_enable_show(struct device *dev,
8363 struct device_attribute *attr,
8369 res = fan_get_status_safe(&status);
8373 if (status & TP_EC_FAN_FULLSPEED) {
8375 } else if (status & TP_EC_FAN_AUTO) {
8380 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8383 static ssize_t fan_pwm1_enable_store(struct device *dev,
8384 struct device_attribute *attr,
8385 const char *buf, size_t count)
8390 if (parse_strtoul(buf, 2, &t))
8393 tpacpi_disclose_usertask("hwmon pwm1_enable",
8394 "set fan mode to %lu\n", t);
8398 level = TP_EC_FAN_FULLSPEED;
8401 level = TPACPI_FAN_LAST_LEVEL;
8404 level = TP_EC_FAN_AUTO;
8407 /* reserved for software-controlled auto mode */
8413 res = fan_set_level_safe(level);
8419 fan_watchdog_reset();
8424 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8425 fan_pwm1_enable_show, fan_pwm1_enable_store);
8427 /* sysfs fan pwm1 ------------------------------------------------------ */
8428 static ssize_t fan_pwm1_show(struct device *dev,
8429 struct device_attribute *attr,
8435 res = fan_get_status_safe(&status);
8440 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8441 status = fan_control_desired_level;
8446 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8449 static ssize_t fan_pwm1_store(struct device *dev,
8450 struct device_attribute *attr,
8451 const char *buf, size_t count)
8455 u8 status, newlevel;
8457 if (parse_strtoul(buf, 255, &s))
8460 tpacpi_disclose_usertask("hwmon pwm1",
8461 "set fan speed to %lu\n", s);
8463 /* scale down from 0-255 to 0-7 */
8464 newlevel = (s >> 5) & 0x07;
8466 if (mutex_lock_killable(&fan_mutex))
8467 return -ERESTARTSYS;
8469 rc = fan_get_status(&status);
8470 if (!rc && (status &
8471 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8472 rc = fan_set_level(newlevel);
8476 fan_update_desired_level(newlevel);
8477 fan_watchdog_reset();
8481 mutex_unlock(&fan_mutex);
8482 return (rc) ? rc : count;
8485 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8487 /* sysfs fan fan1_input ------------------------------------------------ */
8488 static ssize_t fan_fan1_input_show(struct device *dev,
8489 struct device_attribute *attr,
8495 res = fan_get_speed(&speed);
8499 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8502 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8504 /* sysfs fan fan2_input ------------------------------------------------ */
8505 static ssize_t fan_fan2_input_show(struct device *dev,
8506 struct device_attribute *attr,
8512 res = fan2_get_speed(&speed);
8516 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8519 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8521 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8522 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8525 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8528 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8529 const char *buf, size_t count)
8533 if (parse_strtoul(buf, 120, &t))
8536 if (!fan_control_allowed)
8539 fan_watchdog_maxinterval = t;
8540 fan_watchdog_reset();
8542 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8547 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8548 fan_fan_watchdog_show, fan_fan_watchdog_store);
8550 /* --------------------------------------------------------------------- */
8551 static struct attribute *fan_attributes[] = {
8552 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8553 &dev_attr_fan1_input.attr,
8554 NULL, /* for fan2_input */
8558 static const struct attribute_group fan_attr_group = {
8559 .attrs = fan_attributes,
8562 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8563 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8565 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8566 { .vendor = PCI_VENDOR_ID_IBM, \
8567 .bios = TPACPI_MATCH_ANY, \
8568 .ec = TPID(__id1, __id2), \
8569 .quirks = __quirks }
8571 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8572 { .vendor = PCI_VENDOR_ID_LENOVO, \
8573 .bios = TPACPI_MATCH_ANY, \
8574 .ec = TPID(__id1, __id2), \
8575 .quirks = __quirks }
8577 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8578 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8579 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8580 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8581 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8582 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8585 #undef TPACPI_FAN_QL
8586 #undef TPACPI_FAN_QI
8588 static int __init fan_init(struct ibm_init_struct *iibm)
8591 unsigned long quirks;
8593 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8594 "initializing fan subdriver\n");
8596 mutex_init(&fan_mutex);
8597 fan_status_access_mode = TPACPI_FAN_NONE;
8598 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8599 fan_control_commands = 0;
8600 fan_watchdog_maxinterval = 0;
8601 tp_features.fan_ctrl_status_undef = 0;
8602 tp_features.second_fan = 0;
8603 fan_control_desired_level = 7;
8605 if (tpacpi_is_ibm()) {
8606 TPACPI_ACPIHANDLE_INIT(fans);
8607 TPACPI_ACPIHANDLE_INIT(gfan);
8608 TPACPI_ACPIHANDLE_INIT(sfan);
8611 quirks = tpacpi_check_quirks(fan_quirk_table,
8612 ARRAY_SIZE(fan_quirk_table));
8615 /* 570, 600e/x, 770e, 770x */
8616 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8618 /* all other ThinkPads: note that even old-style
8619 * ThinkPad ECs supports the fan control register */
8620 if (likely(acpi_ec_read(fan_status_offset,
8621 &fan_control_initial_status))) {
8622 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8623 if (quirks & TPACPI_FAN_Q1)
8625 if (quirks & TPACPI_FAN_2FAN) {
8626 tp_features.second_fan = 1;
8627 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8628 "secondary fan support enabled\n");
8631 pr_err("ThinkPad ACPI EC access misbehaving, "
8632 "fan status and control unavailable\n");
8639 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8640 fan_control_commands |=
8641 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8644 /* gfan without sfan means no fan control */
8645 /* all other models implement TP EC 0x2f control */
8649 fan_control_access_mode =
8650 TPACPI_FAN_WR_ACPI_FANS;
8651 fan_control_commands |=
8652 TPACPI_FAN_CMD_SPEED |
8653 TPACPI_FAN_CMD_LEVEL |
8654 TPACPI_FAN_CMD_ENABLE;
8656 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8657 fan_control_commands |=
8658 TPACPI_FAN_CMD_LEVEL |
8659 TPACPI_FAN_CMD_ENABLE;
8664 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8665 "fan is %s, modes %d, %d\n",
8666 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8667 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8668 fan_status_access_mode, fan_control_access_mode);
8670 /* fan control master switch */
8671 if (!fan_control_allowed) {
8672 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8673 fan_control_commands = 0;
8674 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8675 "fan control features disabled by parameter\n");
8678 /* update fan_control_desired_level */
8679 if (fan_status_access_mode != TPACPI_FAN_NONE)
8680 fan_get_status_safe(NULL);
8682 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8683 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8684 if (tp_features.second_fan) {
8685 /* attach second fan tachometer */
8686 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8687 &dev_attr_fan2_input.attr;
8689 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8694 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8695 &driver_attr_fan_watchdog);
8697 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8706 static void fan_exit(void)
8708 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8709 "cancelling any pending fan watchdog tasks\n");
8711 /* FIXME: can we really do this unconditionally? */
8712 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8713 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8714 &driver_attr_fan_watchdog);
8716 cancel_delayed_work(&fan_watchdog_task);
8717 flush_workqueue(tpacpi_wq);
8720 static void fan_suspend(void)
8724 if (!fan_control_allowed)
8727 /* Store fan status in cache */
8728 fan_control_resume_level = 0;
8729 rc = fan_get_status_safe(&fan_control_resume_level);
8731 pr_notice("failed to read fan level for later "
8732 "restore during resume: %d\n", rc);
8734 /* if it is undefined, don't attempt to restore it.
8736 if (tp_features.fan_ctrl_status_undef)
8737 fan_control_resume_level = 0;
8740 static void fan_resume(void)
8742 u8 current_level = 7;
8743 bool do_set = false;
8746 /* DSDT *always* updates status on resume */
8747 tp_features.fan_ctrl_status_undef = 0;
8749 if (!fan_control_allowed ||
8750 !fan_control_resume_level ||
8751 (fan_get_status_safe(¤t_level) < 0))
8754 switch (fan_control_access_mode) {
8755 case TPACPI_FAN_WR_ACPI_SFAN:
8756 /* never decrease fan level */
8757 do_set = (fan_control_resume_level > current_level);
8759 case TPACPI_FAN_WR_ACPI_FANS:
8760 case TPACPI_FAN_WR_TPEC:
8761 /* never decrease fan level, scale is:
8762 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8764 * We expect the firmware to set either 7 or AUTO, but we
8765 * handle FULLSPEED out of paranoia.
8767 * So, we can safely only restore FULLSPEED or 7, anything
8768 * else could slow the fan. Restoring AUTO is useless, at
8769 * best that's exactly what the DSDT already set (it is the
8772 * Always keep in mind that the DSDT *will* have set the
8773 * fans to what the vendor supposes is the best level. We
8774 * muck with it only to speed the fan up.
8776 if (fan_control_resume_level != 7 &&
8777 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8780 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8781 (current_level != fan_control_resume_level);
8787 pr_notice("restoring fan level to 0x%02x\n",
8788 fan_control_resume_level);
8789 rc = fan_set_level_safe(fan_control_resume_level);
8791 pr_notice("failed to restore fan level: %d\n", rc);
8795 static int fan_read(struct seq_file *m)
8799 unsigned int speed = 0;
8801 switch (fan_status_access_mode) {
8802 case TPACPI_FAN_RD_ACPI_GFAN:
8803 /* 570, 600e/x, 770e, 770x */
8804 rc = fan_get_status_safe(&status);
8808 seq_printf(m, "status:\t\t%s\n"
8810 (status != 0) ? "enabled" : "disabled", status);
8813 case TPACPI_FAN_RD_TPEC:
8814 /* all except 570, 600e/x, 770e, 770x */
8815 rc = fan_get_status_safe(&status);
8819 seq_printf(m, "status:\t\t%s\n",
8820 (status != 0) ? "enabled" : "disabled");
8822 rc = fan_get_speed(&speed);
8826 seq_printf(m, "speed:\t\t%d\n", speed);
8828 if (status & TP_EC_FAN_FULLSPEED)
8829 /* Disengaged mode takes precedence */
8830 seq_printf(m, "level:\t\tdisengaged\n");
8831 else if (status & TP_EC_FAN_AUTO)
8832 seq_printf(m, "level:\t\tauto\n");
8834 seq_printf(m, "level:\t\t%d\n", status);
8837 case TPACPI_FAN_NONE:
8839 seq_printf(m, "status:\t\tnot supported\n");
8842 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8843 seq_printf(m, "commands:\tlevel <level>");
8845 switch (fan_control_access_mode) {
8846 case TPACPI_FAN_WR_ACPI_SFAN:
8847 seq_printf(m, " (<level> is 0-7)\n");
8851 seq_printf(m, " (<level> is 0-7, "
8852 "auto, disengaged, full-speed)\n");
8857 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8858 seq_printf(m, "commands:\tenable, disable\n"
8859 "commands:\twatchdog <timeout> (<timeout> "
8860 "is 0 (off), 1-120 (seconds))\n");
8862 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8863 seq_printf(m, "commands:\tspeed <speed>"
8864 " (<speed> is 0-65535)\n");
8869 static int fan_write_cmd_level(const char *cmd, int *rc)
8873 if (strlencmp(cmd, "level auto") == 0)
8874 level = TP_EC_FAN_AUTO;
8875 else if ((strlencmp(cmd, "level disengaged") == 0) ||
8876 (strlencmp(cmd, "level full-speed") == 0))
8877 level = TP_EC_FAN_FULLSPEED;
8878 else if (sscanf(cmd, "level %d", &level) != 1)
8881 *rc = fan_set_level_safe(level);
8883 pr_err("level command accepted for unsupported access mode %d\n",
8884 fan_control_access_mode);
8886 tpacpi_disclose_usertask("procfs fan",
8887 "set level to %d\n", level);
8892 static int fan_write_cmd_enable(const char *cmd, int *rc)
8894 if (strlencmp(cmd, "enable") != 0)
8897 *rc = fan_set_enable();
8899 pr_err("enable command accepted for unsupported access mode %d\n",
8900 fan_control_access_mode);
8902 tpacpi_disclose_usertask("procfs fan", "enable\n");
8907 static int fan_write_cmd_disable(const char *cmd, int *rc)
8909 if (strlencmp(cmd, "disable") != 0)
8912 *rc = fan_set_disable();
8914 pr_err("disable command accepted for unsupported access mode %d\n",
8915 fan_control_access_mode);
8917 tpacpi_disclose_usertask("procfs fan", "disable\n");
8922 static int fan_write_cmd_speed(const char *cmd, int *rc)
8927 * Support speed <low> <medium> <high> ? */
8929 if (sscanf(cmd, "speed %d", &speed) != 1)
8932 *rc = fan_set_speed(speed);
8934 pr_err("speed command accepted for unsupported access mode %d\n",
8935 fan_control_access_mode);
8937 tpacpi_disclose_usertask("procfs fan",
8938 "set speed to %d\n", speed);
8943 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8947 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8950 if (interval < 0 || interval > 120)
8953 fan_watchdog_maxinterval = interval;
8954 tpacpi_disclose_usertask("procfs fan",
8955 "set watchdog timer to %d\n",
8962 static int fan_write(char *buf)
8967 while (!rc && (cmd = next_cmd(&buf))) {
8968 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8969 fan_write_cmd_level(cmd, &rc)) &&
8970 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8971 (fan_write_cmd_enable(cmd, &rc) ||
8972 fan_write_cmd_disable(cmd, &rc) ||
8973 fan_write_cmd_watchdog(cmd, &rc))) &&
8974 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8975 fan_write_cmd_speed(cmd, &rc))
8979 fan_watchdog_reset();
8985 static struct ibm_struct fan_driver_data = {
8990 .suspend = fan_suspend,
8991 .resume = fan_resume,
8994 /*************************************************************************
8995 * Mute LED subdriver
8999 struct tp_led_table {
9006 static struct tp_led_table led_tables[] = {
9007 [TPACPI_LED_MUTE] = {
9012 [TPACPI_LED_MICMUTE] = {
9019 static int mute_led_on_off(struct tp_led_table *t, bool state)
9024 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
9025 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9029 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9030 state ? t->on_value : t->off_value))
9037 int tpacpi_led_set(int whichled, bool on)
9039 struct tp_led_table *t;
9041 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9044 t = &led_tables[whichled];
9045 if (t->state < 0 || t->state == on)
9047 return mute_led_on_off(t, on);
9049 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9051 static int mute_led_init(struct ibm_init_struct *iibm)
9056 for (i = 0; i < TPACPI_LED_MAX; i++) {
9057 struct tp_led_table *t = &led_tables[i];
9058 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9059 mute_led_on_off(t, false);
9066 static void mute_led_exit(void)
9070 for (i = 0; i < TPACPI_LED_MAX; i++)
9071 tpacpi_led_set(i, false);
9074 static void mute_led_resume(void)
9078 for (i = 0; i < TPACPI_LED_MAX; i++) {
9079 struct tp_led_table *t = &led_tables[i];
9081 mute_led_on_off(t, t->state);
9085 static struct ibm_struct mute_led_driver_data = {
9087 .exit = mute_led_exit,
9088 .resume = mute_led_resume,
9091 /****************************************************************************
9092 ****************************************************************************
9096 ****************************************************************************
9097 ****************************************************************************/
9100 * HKEY event callout for other subdrivers go here
9101 * (yes, it is ugly, but it is quick, safe, and gets the job done
9103 static void tpacpi_driver_event(const unsigned int hkey_event)
9105 if (ibm_backlight_device) {
9106 switch (hkey_event) {
9107 case TP_HKEY_EV_BRGHT_UP:
9108 case TP_HKEY_EV_BRGHT_DOWN:
9109 tpacpi_brightness_notify_change();
9113 switch (hkey_event) {
9114 case TP_HKEY_EV_VOL_UP:
9115 case TP_HKEY_EV_VOL_DOWN:
9116 case TP_HKEY_EV_VOL_MUTE:
9117 volume_alsa_notify_change();
9122 static void hotkey_driver_event(const unsigned int scancode)
9124 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9127 /* sysfs name ---------------------------------------------------------- */
9128 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9129 struct device_attribute *attr,
9132 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
9135 static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
9137 /* --------------------------------------------------------------------- */
9140 static struct proc_dir_entry *proc_dir;
9143 * Module and infrastructure proble, init and exit handling
9146 static bool force_load;
9148 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9149 static const char * __init str_supported(int is_supported)
9151 static char text_unsupported[] __initdata = "not supported";
9153 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9155 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9157 static void ibm_exit(struct ibm_struct *ibm)
9159 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9161 list_del_init(&ibm->all_drivers);
9163 if (ibm->flags.acpi_notify_installed) {
9164 dbg_printk(TPACPI_DBG_EXIT,
9165 "%s: acpi_remove_notify_handler\n", ibm->name);
9167 acpi_remove_notify_handler(*ibm->acpi->handle,
9169 dispatch_acpi_notify);
9170 ibm->flags.acpi_notify_installed = 0;
9173 if (ibm->flags.proc_created) {
9174 dbg_printk(TPACPI_DBG_EXIT,
9175 "%s: remove_proc_entry\n", ibm->name);
9176 remove_proc_entry(ibm->name, proc_dir);
9177 ibm->flags.proc_created = 0;
9180 if (ibm->flags.acpi_driver_registered) {
9181 dbg_printk(TPACPI_DBG_EXIT,
9182 "%s: acpi_bus_unregister_driver\n", ibm->name);
9184 acpi_bus_unregister_driver(ibm->acpi->driver);
9185 kfree(ibm->acpi->driver);
9186 ibm->acpi->driver = NULL;
9187 ibm->flags.acpi_driver_registered = 0;
9190 if (ibm->flags.init_called && ibm->exit) {
9192 ibm->flags.init_called = 0;
9195 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9198 static int __init ibm_init(struct ibm_init_struct *iibm)
9201 struct ibm_struct *ibm = iibm->data;
9202 struct proc_dir_entry *entry;
9204 BUG_ON(ibm == NULL);
9206 INIT_LIST_HEAD(&ibm->all_drivers);
9208 if (ibm->flags.experimental && !experimental)
9211 dbg_printk(TPACPI_DBG_INIT,
9212 "probing for %s\n", ibm->name);
9215 ret = iibm->init(iibm);
9217 return 0; /* probe failed */
9221 ibm->flags.init_called = 1;
9225 if (ibm->acpi->hid) {
9226 ret = register_tpacpi_subdriver(ibm);
9231 if (ibm->acpi->notify) {
9232 ret = setup_acpi_notify(ibm);
9233 if (ret == -ENODEV) {
9234 pr_notice("disabling subdriver %s\n",
9244 dbg_printk(TPACPI_DBG_INIT,
9245 "%s installed\n", ibm->name);
9248 umode_t mode = iibm->base_procfs_mode;
9254 entry = proc_create_data(ibm->name, mode, proc_dir,
9255 &dispatch_proc_fops, ibm);
9257 pr_err("unable to create proc entry %s\n", ibm->name);
9261 ibm->flags.proc_created = 1;
9264 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9269 dbg_printk(TPACPI_DBG_INIT,
9270 "%s: at error exit path with result %d\n",
9274 return (ret < 0) ? ret : 0;
9279 static bool __pure __init tpacpi_is_fw_digit(const char c)
9281 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9284 static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
9288 * Most models: xxyTkkWW (#.##c)
9289 * Ancient 570/600 and -SL lacks (#.##c)
9291 if (s && strlen(s) >= 8 &&
9292 tpacpi_is_fw_digit(s[0]) &&
9293 tpacpi_is_fw_digit(s[1]) &&
9295 (s[3] == 'T' || s[3] == 'N') &&
9296 tpacpi_is_fw_digit(s[4]) &&
9297 tpacpi_is_fw_digit(s[5]))
9300 /* New models: xxxyTkkW (#.##c); T550 and some others */
9301 return s && strlen(s) >= 8 &&
9302 tpacpi_is_fw_digit(s[0]) &&
9303 tpacpi_is_fw_digit(s[1]) &&
9304 tpacpi_is_fw_digit(s[2]) &&
9306 (s[4] == 'T' || s[4] == 'N') &&
9307 tpacpi_is_fw_digit(s[5]) &&
9308 tpacpi_is_fw_digit(s[6]);
9311 /* returns 0 - probe ok, or < 0 - probe error.
9312 * Probe ok doesn't mean thinkpad found.
9313 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9314 static int __must_check __init get_thinkpad_model_data(
9315 struct thinkpad_id_data *tp)
9317 const struct dmi_device *dev = NULL;
9318 char ec_fw_string[18];
9324 memset(tp, 0, sizeof(*tp));
9326 if (dmi_name_in_vendors("IBM"))
9327 tp->vendor = PCI_VENDOR_ID_IBM;
9328 else if (dmi_name_in_vendors("LENOVO"))
9329 tp->vendor = PCI_VENDOR_ID_LENOVO;
9333 s = dmi_get_system_info(DMI_BIOS_VERSION);
9334 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9335 if (s && !tp->bios_version_str)
9338 /* Really ancient ThinkPad 240X will fail this, which is fine */
9339 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9340 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
9343 tp->bios_model = tp->bios_version_str[0]
9344 | (tp->bios_version_str[1] << 8);
9345 tp->bios_release = (tp->bios_version_str[4] << 8)
9346 | tp->bios_version_str[5];
9349 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9350 * X32 or newer, all Z series; Some models must have an
9351 * up-to-date BIOS or they will not be detected.
9353 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9355 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9356 if (sscanf(dev->name,
9357 "IBM ThinkPad Embedded Controller -[%17c",
9358 ec_fw_string) == 1) {
9359 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9360 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
9362 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
9363 if (!tp->ec_version_str)
9366 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9367 tp->ec_model = ec_fw_string[0]
9368 | (ec_fw_string[1] << 8);
9369 tp->ec_release = (ec_fw_string[4] << 8)
9372 pr_notice("ThinkPad firmware release %s "
9373 "doesn't match the known patterns\n",
9375 pr_notice("please report this to %s\n",
9382 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
9383 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
9384 tp->model_str = kstrdup(s, GFP_KERNEL);
9388 s = dmi_get_system_info(DMI_BIOS_VENDOR);
9389 if (s && !(strncasecmp(s, "Lenovo", 6))) {
9390 tp->model_str = kstrdup(s, GFP_KERNEL);
9396 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9397 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9398 if (s && !tp->nummodel_str)
9404 static int __init probe_for_thinkpad(void)
9411 /* It would be dangerous to run the driver in this case */
9412 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9416 * Non-ancient models have better DMI tagging, but very old models
9417 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9419 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9420 (thinkpad_id.ec_model != 0) ||
9421 tpacpi_is_fw_known();
9423 /* The EC handler is required */
9424 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
9427 pr_err("Not yet supported ThinkPad detected!\n");
9431 if (!is_thinkpad && !force_load)
9437 static void __init thinkpad_acpi_init_banner(void)
9439 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9440 pr_info("%s\n", TPACPI_URL);
9442 pr_info("ThinkPad BIOS %s, EC %s\n",
9443 (thinkpad_id.bios_version_str) ?
9444 thinkpad_id.bios_version_str : "unknown",
9445 (thinkpad_id.ec_version_str) ?
9446 thinkpad_id.ec_version_str : "unknown");
9448 BUG_ON(!thinkpad_id.vendor);
9450 if (thinkpad_id.model_str)
9451 pr_info("%s %s, model %s\n",
9452 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9453 "IBM" : ((thinkpad_id.vendor ==
9454 PCI_VENDOR_ID_LENOVO) ?
9455 "Lenovo" : "Unknown vendor"),
9456 thinkpad_id.model_str,
9457 (thinkpad_id.nummodel_str) ?
9458 thinkpad_id.nummodel_str : "unknown");
9461 /* Module init, exit, parameters */
9463 static struct ibm_init_struct ibms_init[] __initdata = {
9465 .data = &thinkpad_acpi_driver_data,
9468 .init = hotkey_init,
9469 .data = &hotkey_driver_data,
9472 .init = bluetooth_init,
9473 .data = &bluetooth_driver_data,
9477 .data = &wan_driver_data,
9481 .data = &uwb_driver_data,
9483 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9486 .base_procfs_mode = S_IRUSR,
9487 .data = &video_driver_data,
9491 .init = kbdlight_init,
9492 .data = &kbdlight_driver_data,
9496 .data = &light_driver_data,
9500 .data = &cmos_driver_data,
9504 .data = &led_driver_data,
9508 .data = &beep_driver_data,
9511 .init = thermal_init,
9512 .data = &thermal_driver_data,
9515 .init = brightness_init,
9516 .data = &brightness_driver_data,
9519 .init = volume_init,
9520 .data = &volume_driver_data,
9524 .data = &fan_driver_data,
9527 .init = mute_led_init,
9528 .data = &mute_led_driver_data,
9532 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9535 struct ibm_struct *ibm;
9537 if (!kp || !kp->name || !val)
9540 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9541 ibm = ibms_init[i].data;
9542 WARN_ON(ibm == NULL);
9544 if (!ibm || !ibm->name)
9547 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9548 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
9550 strcpy(ibms_init[i].param, val);
9551 strcat(ibms_init[i].param, ",");
9559 module_param(experimental, int, 0444);
9560 MODULE_PARM_DESC(experimental,
9561 "Enables experimental features when non-zero");
9563 module_param_named(debug, dbg_level, uint, 0);
9564 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
9566 module_param(force_load, bool, 0444);
9567 MODULE_PARM_DESC(force_load,
9568 "Attempts to load the driver even on a "
9569 "mis-identified ThinkPad when true");
9571 module_param_named(fan_control, fan_control_allowed, bool, 0444);
9572 MODULE_PARM_DESC(fan_control,
9573 "Enables setting fan parameters features when true");
9575 module_param_named(brightness_mode, brightness_mode, uint, 0444);
9576 MODULE_PARM_DESC(brightness_mode,
9577 "Selects brightness control strategy: "
9578 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9580 module_param(brightness_enable, uint, 0444);
9581 MODULE_PARM_DESC(brightness_enable,
9582 "Enables backlight control when 1, disables when 0");
9584 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9585 module_param_named(volume_mode, volume_mode, uint, 0444);
9586 MODULE_PARM_DESC(volume_mode,
9587 "Selects volume control strategy: "
9588 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9590 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9591 MODULE_PARM_DESC(volume_capabilities,
9592 "Selects the mixer capabilites: "
9593 "0=auto, 1=volume and mute, 2=mute only");
9595 module_param_named(volume_control, volume_control_allowed, bool, 0444);
9596 MODULE_PARM_DESC(volume_control,
9597 "Enables software override for the console audio "
9598 "control when true");
9600 module_param_named(software_mute, software_mute_requested, bool, 0444);
9601 MODULE_PARM_DESC(software_mute,
9602 "Request full software mute control");
9604 /* ALSA module API parameters */
9605 module_param_named(index, alsa_index, int, 0444);
9606 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9607 module_param_named(id, alsa_id, charp, 0444);
9608 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9609 module_param_named(enable, alsa_enable, bool, 0444);
9610 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
9611 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9613 #define TPACPI_PARAM(feature) \
9614 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9615 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
9616 "at module load, see documentation")
9618 TPACPI_PARAM(hotkey);
9619 TPACPI_PARAM(bluetooth);
9620 TPACPI_PARAM(video);
9621 TPACPI_PARAM(light);
9625 TPACPI_PARAM(brightness);
9626 TPACPI_PARAM(volume);
9629 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9630 module_param(dbg_wlswemul, uint, 0444);
9631 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9632 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9633 MODULE_PARM_DESC(wlsw_state,
9634 "Initial state of the emulated WLSW switch");
9636 module_param(dbg_bluetoothemul, uint, 0444);
9637 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9638 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9639 MODULE_PARM_DESC(bluetooth_state,
9640 "Initial state of the emulated bluetooth switch");
9642 module_param(dbg_wwanemul, uint, 0444);
9643 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9644 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9645 MODULE_PARM_DESC(wwan_state,
9646 "Initial state of the emulated WWAN switch");
9648 module_param(dbg_uwbemul, uint, 0444);
9649 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9650 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9651 MODULE_PARM_DESC(uwb_state,
9652 "Initial state of the emulated UWB switch");
9655 static void thinkpad_acpi_module_exit(void)
9657 struct ibm_struct *ibm, *itmp;
9659 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9661 list_for_each_entry_safe_reverse(ibm, itmp,
9662 &tpacpi_all_drivers,
9667 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9669 if (tpacpi_inputdev) {
9670 if (tp_features.input_device_registered)
9671 input_unregister_device(tpacpi_inputdev);
9673 input_free_device(tpacpi_inputdev);
9674 kfree(hotkey_keycode_map);
9678 hwmon_device_unregister(tpacpi_hwmon);
9680 if (tp_features.sensors_pdev_attrs_registered)
9681 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
9682 if (tpacpi_sensors_pdev)
9683 platform_device_unregister(tpacpi_sensors_pdev);
9685 platform_device_unregister(tpacpi_pdev);
9687 if (tp_features.sensors_pdrv_attrs_registered)
9688 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9689 if (tp_features.platform_drv_attrs_registered)
9690 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9692 if (tp_features.sensors_pdrv_registered)
9693 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9695 if (tp_features.platform_drv_registered)
9696 platform_driver_unregister(&tpacpi_pdriver);
9699 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9702 destroy_workqueue(tpacpi_wq);
9704 kfree(thinkpad_id.bios_version_str);
9705 kfree(thinkpad_id.ec_version_str);
9706 kfree(thinkpad_id.model_str);
9707 kfree(thinkpad_id.nummodel_str);
9711 static int __init thinkpad_acpi_module_init(void)
9715 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9717 /* Driver-level probe */
9719 ret = get_thinkpad_model_data(&thinkpad_id);
9721 pr_err("unable to get DMI data: %d\n", ret);
9722 thinkpad_acpi_module_exit();
9725 ret = probe_for_thinkpad();
9727 thinkpad_acpi_module_exit();
9731 /* Driver initialization */
9733 thinkpad_acpi_init_banner();
9734 tpacpi_check_outdated_fw();
9736 TPACPI_ACPIHANDLE_INIT(ecrd);
9737 TPACPI_ACPIHANDLE_INIT(ecwr);
9739 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9741 thinkpad_acpi_module_exit();
9745 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9747 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9748 thinkpad_acpi_module_exit();
9752 ret = platform_driver_register(&tpacpi_pdriver);
9754 pr_err("unable to register main platform driver\n");
9755 thinkpad_acpi_module_exit();
9758 tp_features.platform_drv_registered = 1;
9760 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9762 pr_err("unable to register hwmon platform driver\n");
9763 thinkpad_acpi_module_exit();
9766 tp_features.sensors_pdrv_registered = 1;
9768 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9770 tp_features.platform_drv_attrs_registered = 1;
9771 ret = tpacpi_create_driver_attributes(
9772 &tpacpi_hwmon_pdriver.driver);
9775 pr_err("unable to create sysfs driver attributes\n");
9776 thinkpad_acpi_module_exit();
9779 tp_features.sensors_pdrv_attrs_registered = 1;
9782 /* Device initialization */
9783 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9785 if (IS_ERR(tpacpi_pdev)) {
9786 ret = PTR_ERR(tpacpi_pdev);
9788 pr_err("unable to register platform device\n");
9789 thinkpad_acpi_module_exit();
9792 tpacpi_sensors_pdev = platform_device_register_simple(
9793 TPACPI_HWMON_DRVR_NAME,
9795 if (IS_ERR(tpacpi_sensors_pdev)) {
9796 ret = PTR_ERR(tpacpi_sensors_pdev);
9797 tpacpi_sensors_pdev = NULL;
9798 pr_err("unable to register hwmon platform device\n");
9799 thinkpad_acpi_module_exit();
9802 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
9804 pr_err("unable to create sysfs hwmon device attributes\n");
9805 thinkpad_acpi_module_exit();
9808 tp_features.sensors_pdev_attrs_registered = 1;
9809 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9810 if (IS_ERR(tpacpi_hwmon)) {
9811 ret = PTR_ERR(tpacpi_hwmon);
9812 tpacpi_hwmon = NULL;
9813 pr_err("unable to register hwmon device\n");
9814 thinkpad_acpi_module_exit();
9817 mutex_init(&tpacpi_inputdev_send_mutex);
9818 tpacpi_inputdev = input_allocate_device();
9819 if (!tpacpi_inputdev) {
9820 thinkpad_acpi_module_exit();
9823 /* Prepare input device, but don't register */
9824 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9825 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9826 tpacpi_inputdev->id.bustype = BUS_HOST;
9827 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9828 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9829 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9830 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9833 /* Init subdriver dependencies */
9834 tpacpi_detect_brightness_capabilities();
9836 /* Init subdrivers */
9837 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9838 ret = ibm_init(&ibms_init[i]);
9839 if (ret >= 0 && *ibms_init[i].param)
9840 ret = ibms_init[i].data->write(ibms_init[i].param);
9842 thinkpad_acpi_module_exit();
9847 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9849 ret = input_register_device(tpacpi_inputdev);
9851 pr_err("unable to register input device\n");
9852 thinkpad_acpi_module_exit();
9855 tp_features.input_device_registered = 1;
9861 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9864 * This will autoload the driver in almost every ThinkPad
9865 * in widespread use.
9867 * Only _VERY_ old models, like the 240, 240x and 570 lack
9868 * the HKEY event interface.
9870 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9873 * DMI matching for module autoloading
9875 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9876 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9878 * Only models listed in thinkwiki will be supported, so add yours
9879 * if it is not there yet.
9881 #define IBM_BIOS_MODULE_ALIAS(__type) \
9882 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9884 /* Ancient thinkpad BIOSes have to be identified by
9885 * BIOS type or model number, and there are far less
9886 * BIOS types than model numbers... */
9887 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9889 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9890 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9891 MODULE_DESCRIPTION(TPACPI_DESC);
9892 MODULE_VERSION(TPACPI_VERSION);
9893 MODULE_LICENSE("GPL");
9895 module_init(thinkpad_acpi_module_init);
9896 module_exit(thinkpad_acpi_module_exit);