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.24"
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>
65 #include <linux/nvram.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/sysfs.h>
69 #include <linux/backlight.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <asm/uaccess.h>
79 #include <linux/dmi.h>
80 #include <linux/jiffies.h>
81 #include <linux/workqueue.h>
83 #include <sound/core.h>
84 #include <sound/control.h>
85 #include <sound/initval.h>
87 #include <acpi/acpi_drivers.h>
89 #include <linux/pci_ids.h>
92 /* ThinkPad CMOS commands */
93 #define TP_CMOS_VOLUME_DOWN 0
94 #define TP_CMOS_VOLUME_UP 1
95 #define TP_CMOS_VOLUME_MUTE 2
96 #define TP_CMOS_BRIGHTNESS_UP 4
97 #define TP_CMOS_BRIGHTNESS_DOWN 5
98 #define TP_CMOS_THINKLIGHT_ON 12
99 #define TP_CMOS_THINKLIGHT_OFF 13
101 /* NVRAM Addresses */
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
110 /* NVRAM bit masks */
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
127 /* Misc NVRAM-related */
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
133 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
164 enum tpacpi_hkey_event_t {
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
187 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
188 or port replicator */
189 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
190 dock or port replicator */
192 /* User-interface events */
193 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
194 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
195 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
196 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
197 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
198 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
199 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201 /* Key-related user-interface events */
202 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
203 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
206 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
207 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
208 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
209 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
210 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
212 TP_HKEY_EV_UNK_6040 = 0x6040, /* Related to AC change?
213 some sort of APM hint,
217 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
220 /****************************************************************************
224 #define TPACPI_NAME "thinkpad"
225 #define TPACPI_DESC "ThinkPad ACPI Extras"
226 #define TPACPI_FILE TPACPI_NAME "_acpi"
227 #define TPACPI_URL "http://ibm-acpi.sf.net/"
228 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
230 #define TPACPI_PROC_DIR "ibm"
231 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
232 #define TPACPI_DRVR_NAME TPACPI_FILE
233 #define TPACPI_DRVR_SHORTNAME "tpacpi"
234 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
236 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
237 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
239 #define TPACPI_MAX_ACPI_ARGS 3
241 /* Debugging printk groups */
242 #define TPACPI_DBG_ALL 0xffff
243 #define TPACPI_DBG_DISCLOSETASK 0x8000
244 #define TPACPI_DBG_INIT 0x0001
245 #define TPACPI_DBG_EXIT 0x0002
246 #define TPACPI_DBG_RFKILL 0x0004
247 #define TPACPI_DBG_HKEY 0x0008
248 #define TPACPI_DBG_FAN 0x0010
249 #define TPACPI_DBG_BRGHT 0x0020
250 #define TPACPI_DBG_MIXER 0x0040
252 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
253 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
254 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
257 /****************************************************************************
258 * Driver-wide structs and misc. variables
263 struct tp_acpi_drv_struct {
264 const struct acpi_device_id *hid;
265 struct acpi_driver *driver;
267 void (*notify) (struct ibm_struct *, u32);
270 struct acpi_device *device;
276 int (*read) (struct seq_file *);
277 int (*write) (char *);
279 void (*resume) (void);
280 void (*suspend) (void);
281 void (*shutdown) (void);
283 struct list_head all_drivers;
285 struct tp_acpi_drv_struct *acpi;
288 u8 acpi_driver_registered:1;
289 u8 acpi_notify_installed:1;
296 struct ibm_init_struct {
299 int (*init) (struct ibm_init_struct *);
300 umode_t base_procfs_mode;
301 struct ibm_struct *data;
312 u32 bright_acpimode:1;
316 u32 fan_ctrl_status_undef:1;
318 u32 beep_needs_two_args:1;
319 u32 mixer_no_level_control:1;
320 u32 input_device_registered:1;
321 u32 platform_drv_registered:1;
322 u32 platform_drv_attrs_registered:1;
323 u32 sensors_pdrv_registered:1;
324 u32 sensors_pdrv_attrs_registered:1;
325 u32 sensors_pdev_attrs_registered:1;
326 u32 hotkey_poll_active:1;
330 u16 hotkey_mask_ff:1;
331 u16 volume_ctrl_forbidden:1;
334 struct thinkpad_id_data {
335 unsigned int vendor; /* ThinkPad vendor:
336 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
338 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
339 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
341 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
343 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
346 char *model_str; /* ThinkPad T43 */
347 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
349 static struct thinkpad_id_data thinkpad_id;
352 TPACPI_LIFE_INIT = 0,
357 static int experimental;
358 static u32 dbg_level;
360 static struct workqueue_struct *tpacpi_wq;
368 /* Special LED class that can defer work */
369 struct tpacpi_led_classdev {
370 struct led_classdev led_classdev;
371 struct work_struct work;
372 enum led_status_t new_state;
376 /* brightness level capabilities */
377 static unsigned int bright_maxlvl; /* 0 = unknown */
379 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
380 static int dbg_wlswemul;
381 static bool tpacpi_wlsw_emulstate;
382 static int dbg_bluetoothemul;
383 static bool tpacpi_bluetooth_emulstate;
384 static int dbg_wwanemul;
385 static bool tpacpi_wwan_emulstate;
386 static int dbg_uwbemul;
387 static bool tpacpi_uwb_emulstate;
391 /*************************************************************************
395 #define dbg_printk(a_dbg_level, format, arg...) \
397 if (dbg_level & (a_dbg_level)) \
398 printk(KERN_DEBUG pr_fmt("%s: " format), \
402 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
403 #define vdbg_printk dbg_printk
404 static const char *str_supported(int is_supported);
406 static inline const char *str_supported(int is_supported) { return ""; }
407 #define vdbg_printk(a_dbg_level, format, arg...) \
408 no_printk(format, ##arg)
411 static void tpacpi_log_usertask(const char * const what)
413 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
414 what, task_tgid_vnr(current));
417 #define tpacpi_disclose_usertask(what, format, arg...) \
419 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
420 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
421 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
422 what, task_tgid_vnr(current), ## arg); \
427 * Quirk handling helpers
429 * ThinkPad IDs and versions seen in the field so far
430 * are two-characters from the set [0-9A-Z], i.e. base 36.
432 * We use values well outside that range as specials.
435 #define TPACPI_MATCH_ANY 0xffffU
436 #define TPACPI_MATCH_UNKNOWN 0U
438 /* TPID('1', 'Y') == 0x5931 */
439 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
441 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_IBM, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
453 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPACPI_MATCH_ANY, \
456 .ec = TPID(__id1, __id2), \
457 .quirks = (__quirk) }
459 struct tpacpi_quirk {
463 unsigned long quirks;
467 * tpacpi_check_quirks() - search BIOS/EC version on a list
468 * @qlist: array of &struct tpacpi_quirk
469 * @qlist_size: number of elements in @qlist
471 * Iterates over a quirks list until one is found that matches the
472 * ThinkPad's vendor, BIOS and EC model.
474 * Returns 0 if nothing matches, otherwise returns the quirks field of
475 * the matching &struct tpacpi_quirk entry.
477 * The match criteria is: vendor, ec and bios much match.
479 static unsigned long __init tpacpi_check_quirks(
480 const struct tpacpi_quirk *qlist,
481 unsigned int qlist_size)
484 if ((qlist->vendor == thinkpad_id.vendor ||
485 qlist->vendor == TPACPI_MATCH_ANY) &&
486 (qlist->bios == thinkpad_id.bios_model ||
487 qlist->bios == TPACPI_MATCH_ANY) &&
488 (qlist->ec == thinkpad_id.ec_model ||
489 qlist->ec == TPACPI_MATCH_ANY))
490 return qlist->quirks;
498 static inline bool __pure __init tpacpi_is_lenovo(void)
500 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
503 static inline bool __pure __init tpacpi_is_ibm(void)
505 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
508 /****************************************************************************
509 ****************************************************************************
511 * ACPI Helpers and device model
513 ****************************************************************************
514 ****************************************************************************/
516 /*************************************************************************
520 static acpi_handle root_handle;
521 static acpi_handle ec_handle;
523 #define TPACPI_HANDLE(object, parent, paths...) \
524 static acpi_handle object##_handle; \
525 static const acpi_handle *object##_parent __initdata = \
527 static char *object##_paths[] __initdata = { paths }
529 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
530 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
532 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
534 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
535 "\\CMS", /* R40, R40e */
538 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
539 "^HKEY", /* R30, R31 */
540 "HKEY", /* all others */
543 /*************************************************************************
547 static int acpi_evalf(acpi_handle handle,
548 void *res, char *method, char *fmt, ...)
551 struct acpi_object_list params;
552 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
553 struct acpi_buffer result, *resultp;
554 union acpi_object out_obj;
562 pr_err("acpi_evalf() called with empty format\n");
575 params.pointer = &in_objs[0];
582 in_objs[params.count].integer.value = va_arg(ap, int);
583 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
585 /* add more types as needed */
587 pr_err("acpi_evalf() called "
588 "with invalid format character '%c'\n", c);
595 if (res_type != 'v') {
596 result.length = sizeof(out_obj);
597 result.pointer = &out_obj;
602 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
606 success = (status == AE_OK &&
607 out_obj.type == ACPI_TYPE_INTEGER);
609 *(int *)res = out_obj.integer.value;
612 success = status == AE_OK;
614 /* add more types as needed */
616 pr_err("acpi_evalf() called "
617 "with invalid format character '%c'\n", res_type);
621 if (!success && !quiet)
622 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
623 method, fmt0, acpi_format_exception(status));
628 static int acpi_ec_read(int i, u8 *p)
633 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
637 if (ec_read(i, p) < 0)
644 static int acpi_ec_write(int i, u8 v)
647 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
650 if (ec_write(i, v) < 0)
657 static int issue_thinkpad_cmos_command(int cmos_cmd)
662 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
668 /*************************************************************************
672 #define TPACPI_ACPIHANDLE_INIT(object) \
673 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
674 object##_paths, ARRAY_SIZE(object##_paths))
676 static void __init drv_acpi_handle_init(const char *name,
677 acpi_handle *handle, const acpi_handle parent,
678 char **paths, const int num_paths)
683 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
686 for (i = 0; i < num_paths; i++) {
687 status = acpi_get_handle(parent, paths[i], handle);
688 if (ACPI_SUCCESS(status)) {
689 dbg_printk(TPACPI_DBG_INIT,
690 "Found ACPI handle %s for %s\n",
696 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
701 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
702 u32 level, void *context, void **return_value)
704 *(acpi_handle *)return_value = handle;
706 return AE_CTRL_TERMINATE;
709 static void __init tpacpi_acpi_handle_locate(const char *name,
714 acpi_handle device_found;
716 BUG_ON(!name || !hid || !handle);
717 vdbg_printk(TPACPI_DBG_INIT,
718 "trying to locate ACPI handle for %s, using HID %s\n",
721 memset(&device_found, 0, sizeof(device_found));
722 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
723 (void *)name, &device_found);
727 if (ACPI_SUCCESS(status)) {
728 *handle = device_found;
729 dbg_printk(TPACPI_DBG_INIT,
730 "Found ACPI handle for %s\n", name);
732 vdbg_printk(TPACPI_DBG_INIT,
733 "Could not locate an ACPI handle for %s: %s\n",
734 name, acpi_format_exception(status));
738 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
740 struct ibm_struct *ibm = data;
742 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
748 ibm->acpi->notify(ibm, event);
751 static int __init setup_acpi_notify(struct ibm_struct *ibm)
758 if (!*ibm->acpi->handle)
761 vdbg_printk(TPACPI_DBG_INIT,
762 "setting up ACPI notify for %s\n", ibm->name);
764 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
766 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
770 ibm->acpi->device->driver_data = ibm;
771 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
772 TPACPI_ACPI_EVENT_PREFIX,
775 status = acpi_install_notify_handler(*ibm->acpi->handle,
776 ibm->acpi->type, dispatch_acpi_notify, ibm);
777 if (ACPI_FAILURE(status)) {
778 if (status == AE_ALREADY_EXISTS) {
779 pr_notice("another device driver is already "
780 "handling %s events\n", ibm->name);
782 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
783 ibm->name, acpi_format_exception(status));
787 ibm->flags.acpi_notify_installed = 1;
791 static int __init tpacpi_device_add(struct acpi_device *device)
796 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
800 dbg_printk(TPACPI_DBG_INIT,
801 "registering %s as an ACPI driver\n", ibm->name);
805 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
806 if (!ibm->acpi->driver) {
807 pr_err("failed to allocate memory for ibm->acpi->driver\n");
811 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
812 ibm->acpi->driver->ids = ibm->acpi->hid;
814 ibm->acpi->driver->ops.add = &tpacpi_device_add;
816 rc = acpi_bus_register_driver(ibm->acpi->driver);
818 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
820 kfree(ibm->acpi->driver);
821 ibm->acpi->driver = NULL;
823 ibm->flags.acpi_driver_registered = 1;
829 /****************************************************************************
830 ****************************************************************************
834 ****************************************************************************
835 ****************************************************************************/
837 static int dispatch_proc_show(struct seq_file *m, void *v)
839 struct ibm_struct *ibm = m->private;
841 if (!ibm || !ibm->read)
846 static int dispatch_proc_open(struct inode *inode, struct file *file)
848 return single_open(file, dispatch_proc_show, PDE(inode)->data);
851 static ssize_t dispatch_proc_write(struct file *file,
852 const char __user *userbuf,
853 size_t count, loff_t *pos)
855 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
859 if (!ibm || !ibm->write)
861 if (count > PAGE_SIZE - 2)
864 kernbuf = kmalloc(count + 2, GFP_KERNEL);
868 if (copy_from_user(kernbuf, userbuf, count)) {
874 strcat(kernbuf, ",");
875 ret = ibm->write(kernbuf);
884 static const struct file_operations dispatch_proc_fops = {
885 .owner = THIS_MODULE,
886 .open = dispatch_proc_open,
889 .release = single_release,
890 .write = dispatch_proc_write,
893 static char *next_cmd(char **cmds)
898 while ((end = strchr(start, ',')) && end == start)
910 /****************************************************************************
911 ****************************************************************************
913 * Device model: input, hwmon and platform
915 ****************************************************************************
916 ****************************************************************************/
918 static struct platform_device *tpacpi_pdev;
919 static struct platform_device *tpacpi_sensors_pdev;
920 static struct device *tpacpi_hwmon;
921 static struct input_dev *tpacpi_inputdev;
922 static struct mutex tpacpi_inputdev_send_mutex;
923 static LIST_HEAD(tpacpi_all_drivers);
925 static int tpacpi_suspend_handler(struct device *dev)
927 struct ibm_struct *ibm, *itmp;
929 list_for_each_entry_safe(ibm, itmp,
939 static int tpacpi_resume_handler(struct device *dev)
941 struct ibm_struct *ibm, *itmp;
943 list_for_each_entry_safe(ibm, itmp,
953 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
954 tpacpi_suspend_handler, tpacpi_resume_handler);
956 static void tpacpi_shutdown_handler(struct platform_device *pdev)
958 struct ibm_struct *ibm, *itmp;
960 list_for_each_entry_safe(ibm, itmp,
968 static struct platform_driver tpacpi_pdriver = {
970 .name = TPACPI_DRVR_NAME,
971 .owner = THIS_MODULE,
974 .shutdown = tpacpi_shutdown_handler,
977 static struct platform_driver tpacpi_hwmon_pdriver = {
979 .name = TPACPI_HWMON_DRVR_NAME,
980 .owner = THIS_MODULE,
984 /*************************************************************************
985 * sysfs support helpers
988 struct attribute_set {
989 unsigned int members, max_members;
990 struct attribute_group group;
993 struct attribute_set_obj {
994 struct attribute_set s;
996 } __attribute__((packed));
998 static struct attribute_set *create_attr_set(unsigned int max_members,
1001 struct attribute_set_obj *sobj;
1003 if (max_members == 0)
1006 /* Allocates space for implicit NULL at the end too */
1007 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1008 max_members * sizeof(struct attribute *),
1012 sobj->s.max_members = max_members;
1013 sobj->s.group.attrs = &sobj->a;
1014 sobj->s.group.name = name;
1019 #define destroy_attr_set(_set) \
1022 /* not multi-threaded safe, use it in a single thread per set */
1023 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1028 if (s->members >= s->max_members)
1031 s->group.attrs[s->members] = attr;
1037 static int add_many_to_attr_set(struct attribute_set *s,
1038 struct attribute **attr,
1043 for (i = 0; i < count; i++) {
1044 res = add_to_attr_set(s, attr[i]);
1052 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1054 sysfs_remove_group(kobj, &s->group);
1055 destroy_attr_set(s);
1058 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1059 sysfs_create_group(_kobj, &_attr_set->group)
1061 static int parse_strtoul(const char *buf,
1062 unsigned long max, unsigned long *value)
1066 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1067 endp = skip_spaces(endp);
1068 if (*endp || *value > max)
1074 static void tpacpi_disable_brightness_delay(void)
1076 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1077 pr_notice("ACPI backlight control delay disabled\n");
1080 static void printk_deprecated_attribute(const char * const what,
1081 const char * const details)
1083 tpacpi_log_usertask("deprecated sysfs attribute");
1084 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1085 "will be removed. %s\n",
1089 /*************************************************************************
1090 * rfkill and radio control support helpers
1094 * ThinkPad-ACPI firmware handling model:
1096 * WLSW (master wireless switch) is event-driven, and is common to all
1097 * firmware-controlled radios. It cannot be controlled, just monitored,
1098 * as expected. It overrides all radio state in firmware
1100 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1101 * (TODO: verify how WLSW interacts with the returned radio state).
1103 * The only time there are shadow radio state changes, is when
1104 * masked-off hotkeys are used.
1108 * Internal driver API for radio state:
1110 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1111 * bool: true means radio blocked (off)
1113 enum tpacpi_rfkill_state {
1114 TPACPI_RFK_RADIO_OFF = 0,
1118 /* rfkill switches */
1119 enum tpacpi_rfk_id {
1120 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1121 TPACPI_RFK_WWAN_SW_ID,
1122 TPACPI_RFK_UWB_SW_ID,
1126 static const char *tpacpi_rfkill_names[] = {
1127 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1128 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1129 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1130 [TPACPI_RFK_SW_MAX] = NULL
1133 /* ThinkPad-ACPI rfkill subdriver */
1135 struct rfkill *rfkill;
1136 enum tpacpi_rfk_id id;
1137 const struct tpacpi_rfk_ops *ops;
1140 struct tpacpi_rfk_ops {
1141 /* firmware interface */
1142 int (*get_status)(void);
1143 int (*set_status)(const enum tpacpi_rfkill_state);
1146 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1148 /* Query FW and update rfkill sw state for a given rfkill switch */
1149 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1156 status = (tp_rfk->ops->get_status)();
1160 rfkill_set_sw_state(tp_rfk->rfkill,
1161 (status == TPACPI_RFK_RADIO_OFF));
1166 /* Query FW and update rfkill sw state for all rfkill switches */
1167 static void tpacpi_rfk_update_swstate_all(void)
1171 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1172 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1176 * Sync the HW-blocking state of all rfkill switches,
1177 * do notice it causes the rfkill core to schedule uevents
1179 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1182 struct tpacpi_rfk *tp_rfk;
1184 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1185 tp_rfk = tpacpi_rfkill_switches[i];
1187 if (rfkill_set_hw_state(tp_rfk->rfkill,
1189 /* ignore -- we track sw block */
1195 /* Call to get the WLSW state from the firmware */
1196 static int hotkey_get_wlsw(void);
1198 /* Call to query WLSW state and update all rfkill switches */
1199 static bool tpacpi_rfk_check_hwblock_state(void)
1201 int res = hotkey_get_wlsw();
1204 /* When unknown or unsupported, we have to assume it is unblocked */
1208 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1209 tpacpi_rfk_update_hwblock_state(hw_blocked);
1214 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1216 struct tpacpi_rfk *tp_rfk = data;
1219 dbg_printk(TPACPI_DBG_RFKILL,
1220 "request to change radio state to %s\n",
1221 blocked ? "blocked" : "unblocked");
1223 /* try to set radio state */
1224 res = (tp_rfk->ops->set_status)(blocked ?
1225 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1227 /* and update the rfkill core with whatever the FW really did */
1228 tpacpi_rfk_update_swstate(tp_rfk);
1230 return (res < 0) ? res : 0;
1233 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1234 .set_block = tpacpi_rfk_hook_set_block,
1237 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1238 const struct tpacpi_rfk_ops *tp_rfkops,
1239 const enum rfkill_type rfktype,
1241 const bool set_default)
1243 struct tpacpi_rfk *atp_rfk;
1245 bool sw_state = false;
1249 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1251 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1253 atp_rfk->rfkill = rfkill_alloc(name,
1256 &tpacpi_rfk_rfkill_ops,
1258 if (!atp_rfk || !atp_rfk->rfkill) {
1259 pr_err("failed to allocate memory for rfkill class\n");
1265 atp_rfk->ops = tp_rfkops;
1267 sw_status = (tp_rfkops->get_status)();
1268 if (sw_status < 0) {
1269 pr_err("failed to read initial state for %s, error %d\n",
1272 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1274 /* try to keep the initial state, since we ask the
1275 * firmware to preserve it across S5 in NVRAM */
1276 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1279 hw_state = tpacpi_rfk_check_hwblock_state();
1280 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1282 res = rfkill_register(atp_rfk->rfkill);
1284 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1285 rfkill_destroy(atp_rfk->rfkill);
1290 tpacpi_rfkill_switches[id] = atp_rfk;
1292 pr_info("rfkill switch %s: radio is %sblocked\n",
1293 name, (sw_state || hw_state) ? "" : "un");
1297 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1299 struct tpacpi_rfk *tp_rfk;
1301 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1303 tp_rfk = tpacpi_rfkill_switches[id];
1305 rfkill_unregister(tp_rfk->rfkill);
1306 rfkill_destroy(tp_rfk->rfkill);
1307 tpacpi_rfkill_switches[id] = NULL;
1312 static void printk_deprecated_rfkill_attribute(const char * const what)
1314 printk_deprecated_attribute(what,
1315 "Please switch to generic rfkill before year 2010");
1318 /* sysfs <radio> enable ------------------------------------------------ */
1319 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1320 struct device_attribute *attr,
1325 printk_deprecated_rfkill_attribute(attr->attr.name);
1327 /* This is in the ABI... */
1328 if (tpacpi_rfk_check_hwblock_state()) {
1329 status = TPACPI_RFK_RADIO_OFF;
1331 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1336 return snprintf(buf, PAGE_SIZE, "%d\n",
1337 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1340 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1341 struct device_attribute *attr,
1342 const char *buf, size_t count)
1347 printk_deprecated_rfkill_attribute(attr->attr.name);
1349 if (parse_strtoul(buf, 1, &t))
1352 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1354 /* This is in the ABI... */
1355 if (tpacpi_rfk_check_hwblock_state() && !!t)
1358 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1359 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1360 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1362 return (res < 0) ? res : count;
1365 /* procfs -------------------------------------------------------------- */
1366 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1368 if (id >= TPACPI_RFK_SW_MAX)
1369 seq_printf(m, "status:\t\tnot supported\n");
1373 /* This is in the ABI... */
1374 if (tpacpi_rfk_check_hwblock_state()) {
1375 status = TPACPI_RFK_RADIO_OFF;
1377 status = tpacpi_rfk_update_swstate(
1378 tpacpi_rfkill_switches[id]);
1383 seq_printf(m, "status:\t\t%s\n",
1384 (status == TPACPI_RFK_RADIO_ON) ?
1385 "enabled" : "disabled");
1386 seq_printf(m, "commands:\tenable, disable\n");
1392 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1398 if (id >= TPACPI_RFK_SW_MAX)
1401 while ((cmd = next_cmd(&buf))) {
1402 if (strlencmp(cmd, "enable") == 0)
1403 status = TPACPI_RFK_RADIO_ON;
1404 else if (strlencmp(cmd, "disable") == 0)
1405 status = TPACPI_RFK_RADIO_OFF;
1411 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1412 (status == TPACPI_RFK_RADIO_ON) ?
1413 "enable" : "disable",
1414 tpacpi_rfkill_names[id]);
1415 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1416 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1422 /*************************************************************************
1423 * thinkpad-acpi driver attributes
1426 /* interface_version --------------------------------------------------- */
1427 static ssize_t tpacpi_driver_interface_version_show(
1428 struct device_driver *drv,
1431 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1434 static DRIVER_ATTR(interface_version, S_IRUGO,
1435 tpacpi_driver_interface_version_show, NULL);
1437 /* debug_level --------------------------------------------------------- */
1438 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1441 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1444 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1445 const char *buf, size_t count)
1449 if (parse_strtoul(buf, 0xffff, &t))
1457 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1458 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1460 /* version ------------------------------------------------------------- */
1461 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1464 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1465 TPACPI_DESC, TPACPI_VERSION);
1468 static DRIVER_ATTR(version, S_IRUGO,
1469 tpacpi_driver_version_show, NULL);
1471 /* --------------------------------------------------------------------- */
1473 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1475 /* wlsw_emulstate ------------------------------------------------------ */
1476 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1479 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1482 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1483 const char *buf, size_t count)
1487 if (parse_strtoul(buf, 1, &t))
1490 if (tpacpi_wlsw_emulstate != !!t) {
1491 tpacpi_wlsw_emulstate = !!t;
1492 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1498 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1499 tpacpi_driver_wlsw_emulstate_show,
1500 tpacpi_driver_wlsw_emulstate_store);
1502 /* bluetooth_emulstate ------------------------------------------------- */
1503 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1504 struct device_driver *drv,
1507 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1510 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1511 struct device_driver *drv,
1512 const char *buf, size_t count)
1516 if (parse_strtoul(buf, 1, &t))
1519 tpacpi_bluetooth_emulstate = !!t;
1524 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1525 tpacpi_driver_bluetooth_emulstate_show,
1526 tpacpi_driver_bluetooth_emulstate_store);
1528 /* wwan_emulstate ------------------------------------------------- */
1529 static ssize_t tpacpi_driver_wwan_emulstate_show(
1530 struct device_driver *drv,
1533 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1536 static ssize_t tpacpi_driver_wwan_emulstate_store(
1537 struct device_driver *drv,
1538 const char *buf, size_t count)
1542 if (parse_strtoul(buf, 1, &t))
1545 tpacpi_wwan_emulstate = !!t;
1550 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1551 tpacpi_driver_wwan_emulstate_show,
1552 tpacpi_driver_wwan_emulstate_store);
1554 /* uwb_emulstate ------------------------------------------------- */
1555 static ssize_t tpacpi_driver_uwb_emulstate_show(
1556 struct device_driver *drv,
1559 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1562 static ssize_t tpacpi_driver_uwb_emulstate_store(
1563 struct device_driver *drv,
1564 const char *buf, size_t count)
1568 if (parse_strtoul(buf, 1, &t))
1571 tpacpi_uwb_emulstate = !!t;
1576 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1577 tpacpi_driver_uwb_emulstate_show,
1578 tpacpi_driver_uwb_emulstate_store);
1581 /* --------------------------------------------------------------------- */
1583 static struct driver_attribute *tpacpi_driver_attributes[] = {
1584 &driver_attr_debug_level, &driver_attr_version,
1585 &driver_attr_interface_version,
1588 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1594 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1595 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1599 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1600 if (!res && dbg_wlswemul)
1601 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1602 if (!res && dbg_bluetoothemul)
1603 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1604 if (!res && dbg_wwanemul)
1605 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1606 if (!res && dbg_uwbemul)
1607 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1613 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1617 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1618 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1620 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1621 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1622 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1623 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1624 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1628 /*************************************************************************
1633 * Table of recommended minimum BIOS versions
1635 * Reasons for listing:
1636 * 1. Stable BIOS, listed because the unknown amount of
1637 * bugs and bad ACPI behaviour on older versions
1639 * 2. BIOS or EC fw with known bugs that trigger on Linux
1641 * 3. BIOS with known reduced functionality in older versions
1643 * We recommend the latest BIOS and EC version.
1644 * We only support the latest BIOS and EC fw version as a rule.
1646 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1647 * Information from users in ThinkWiki
1649 * WARNING: we use this table also to detect that the machine is
1650 * a ThinkPad in some cases, so don't remove entries lightly.
1653 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1654 { .vendor = (__v), \
1655 .bios = TPID(__id1, __id2), \
1656 .ec = TPACPI_MATCH_ANY, \
1657 .quirks = TPACPI_MATCH_ANY << 16 \
1658 | (__bv1) << 8 | (__bv2) }
1660 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1661 __eid, __ev1, __ev2) \
1662 { .vendor = (__v), \
1663 .bios = TPID(__bid1, __bid2), \
1665 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1666 | (__bv1) << 8 | (__bv2) }
1668 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1669 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1671 /* Outdated IBM BIOSes often lack the EC id string */
1672 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1673 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1674 __bv1, __bv2, TPID(__id1, __id2), \
1676 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1677 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1680 /* Outdated IBM BIOSes often lack the EC id string */
1681 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1682 __eid1, __eid2, __ev1, __ev2) \
1683 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1684 __bv1, __bv2, TPID(__eid1, __eid2), \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1687 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1690 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1691 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1693 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1695 __bv1, __bv2, TPID(__id1, __id2), \
1698 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1699 __eid1, __eid2, __ev1, __ev2) \
1700 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1701 __bv1, __bv2, TPID(__eid1, __eid2), \
1704 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1705 /* Numeric models ------------------ */
1706 /* FW MODEL BIOS VERS */
1707 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1708 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1709 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1710 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1711 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1712 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1713 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1714 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1715 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1717 /* A-series ------------------------- */
1718 /* FW MODEL BIOS VERS EC VERS */
1719 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1720 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1721 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1722 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1723 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1724 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1725 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1726 TPV_QI0('1', '3', '2', '0'), /* A22m */
1727 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1728 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1729 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1731 /* G-series ------------------------- */
1732 /* FW MODEL BIOS VERS */
1733 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1734 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1736 /* R-series, T-series --------------- */
1737 /* FW MODEL BIOS VERS EC VERS */
1738 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1739 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1740 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1741 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1742 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1743 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1744 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1745 T40/p, T41/p, T42/p (1) */
1746 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1747 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1748 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1749 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1751 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1752 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1753 TPV_QI0('1', '6', '3', '2'), /* T22 */
1754 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1755 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1756 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1758 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1759 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1760 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1762 /* BIOS FW BIOS VERS EC FW EC VERS */
1763 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1764 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1766 /* X-series ------------------------- */
1767 /* FW MODEL BIOS VERS EC VERS */
1768 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1769 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1770 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1771 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1772 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1773 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1774 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1776 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1777 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1779 /* (0) - older versions lack DMI EC fw string and functionality */
1780 /* (1) - older versions known to lack functionality */
1791 static void __init tpacpi_check_outdated_fw(void)
1793 unsigned long fwvers;
1794 u16 ec_version, bios_version;
1796 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1797 ARRAY_SIZE(tpacpi_bios_version_qtable));
1802 bios_version = fwvers & 0xffffU;
1803 ec_version = (fwvers >> 16) & 0xffffU;
1805 /* note that unknown versions are set to 0x0000 and we use that */
1806 if ((bios_version > thinkpad_id.bios_release) ||
1807 (ec_version > thinkpad_id.ec_release &&
1808 ec_version != TPACPI_MATCH_ANY)) {
1810 * The changelogs would let us track down the exact
1811 * reason, but it is just too much of a pain to track
1812 * it. We only list BIOSes that are either really
1813 * broken, or really stable to begin with, so it is
1814 * best if the user upgrades the firmware anyway.
1816 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1817 pr_warn("WARNING: This firmware may be missing critical bug "
1818 "fixes and/or important features\n");
1822 static bool __init tpacpi_is_fw_known(void)
1824 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1825 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1828 /****************************************************************************
1829 ****************************************************************************
1833 ****************************************************************************
1834 ****************************************************************************/
1836 /*************************************************************************
1837 * thinkpad-acpi metadata subdriver
1840 static int thinkpad_acpi_driver_read(struct seq_file *m)
1842 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1843 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1847 static struct ibm_struct thinkpad_acpi_driver_data = {
1849 .read = thinkpad_acpi_driver_read,
1852 /*************************************************************************
1857 * ThinkPad firmware event model
1859 * The ThinkPad firmware has two main event interfaces: normal ACPI
1860 * notifications (which follow the ACPI standard), and a private event
1863 * The private event interface also issues events for the hotkeys. As
1864 * the driver gained features, the event handling code ended up being
1865 * built around the hotkey subdriver. This will need to be refactored
1866 * to a more formal event API eventually.
1868 * Some "hotkeys" are actually supposed to be used as event reports,
1869 * such as "brightness has changed", "volume has changed", depending on
1870 * the ThinkPad model and how the firmware is operating.
1872 * Unlike other classes, hotkey-class events have mask/unmask control on
1873 * non-ancient firmware. However, how it behaves changes a lot with the
1874 * firmware model and version.
1877 enum { /* hot key scan codes (derived from ACPI DSDT) */
1878 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1879 TP_ACPI_HOTKEYSCAN_FNF2,
1880 TP_ACPI_HOTKEYSCAN_FNF3,
1881 TP_ACPI_HOTKEYSCAN_FNF4,
1882 TP_ACPI_HOTKEYSCAN_FNF5,
1883 TP_ACPI_HOTKEYSCAN_FNF6,
1884 TP_ACPI_HOTKEYSCAN_FNF7,
1885 TP_ACPI_HOTKEYSCAN_FNF8,
1886 TP_ACPI_HOTKEYSCAN_FNF9,
1887 TP_ACPI_HOTKEYSCAN_FNF10,
1888 TP_ACPI_HOTKEYSCAN_FNF11,
1889 TP_ACPI_HOTKEYSCAN_FNF12,
1890 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1891 TP_ACPI_HOTKEYSCAN_FNINSERT,
1892 TP_ACPI_HOTKEYSCAN_FNDELETE,
1893 TP_ACPI_HOTKEYSCAN_FNHOME,
1894 TP_ACPI_HOTKEYSCAN_FNEND,
1895 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1896 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1897 TP_ACPI_HOTKEYSCAN_FNSPACE,
1898 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1899 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1900 TP_ACPI_HOTKEYSCAN_MUTE,
1901 TP_ACPI_HOTKEYSCAN_THINKPAD,
1902 TP_ACPI_HOTKEYSCAN_UNK1,
1903 TP_ACPI_HOTKEYSCAN_UNK2,
1904 TP_ACPI_HOTKEYSCAN_UNK3,
1905 TP_ACPI_HOTKEYSCAN_UNK4,
1906 TP_ACPI_HOTKEYSCAN_UNK5,
1907 TP_ACPI_HOTKEYSCAN_UNK6,
1908 TP_ACPI_HOTKEYSCAN_UNK7,
1909 TP_ACPI_HOTKEYSCAN_UNK8,
1911 /* Hotkey keymap size */
1912 TPACPI_HOTKEY_MAP_LEN
1915 enum { /* Keys/events available through NVRAM polling */
1916 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1917 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1920 enum { /* Positions of some of the keys in hotkey masks */
1921 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1922 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1923 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1924 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1925 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1926 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1927 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1928 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1929 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1930 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1931 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1934 enum { /* NVRAM to ACPI HKEY group map */
1935 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1936 TP_ACPI_HKEY_ZOOM_MASK |
1937 TP_ACPI_HKEY_DISPSWTCH_MASK |
1938 TP_ACPI_HKEY_HIBERNATE_MASK,
1939 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1940 TP_ACPI_HKEY_BRGHTDWN_MASK,
1941 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1942 TP_ACPI_HKEY_VOLDWN_MASK |
1943 TP_ACPI_HKEY_MUTE_MASK,
1946 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1947 struct tp_nvram_state {
1948 u16 thinkpad_toggle:1;
1950 u16 display_toggle:1;
1951 u16 thinklight_toggle:1;
1952 u16 hibernate_toggle:1;
1953 u16 displayexp_toggle:1;
1954 u16 display_state:1;
1955 u16 brightness_toggle:1;
1956 u16 volume_toggle:1;
1959 u8 brightness_level;
1963 /* kthread for the hotkey poller */
1964 static struct task_struct *tpacpi_hotkey_task;
1966 /* Acquired while the poller kthread is running, use to sync start/stop */
1967 static struct mutex hotkey_thread_mutex;
1970 * Acquire mutex to write poller control variables as an
1973 * Increment hotkey_config_change when changing them if you
1974 * want the kthread to forget old state.
1976 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1978 static struct mutex hotkey_thread_data_mutex;
1979 static unsigned int hotkey_config_change;
1982 * hotkey poller control variables
1984 * Must be atomic or readers will also need to acquire mutex
1986 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1987 * should be used only when the changes need to be taken as
1988 * a block, OR when one needs to force the kthread to forget
1991 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1992 static unsigned int hotkey_poll_freq = 10; /* Hz */
1994 #define HOTKEY_CONFIG_CRITICAL_START \
1996 mutex_lock(&hotkey_thread_data_mutex); \
1997 hotkey_config_change++; \
1999 #define HOTKEY_CONFIG_CRITICAL_END \
2000 mutex_unlock(&hotkey_thread_data_mutex);
2002 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004 #define hotkey_source_mask 0U
2005 #define HOTKEY_CONFIG_CRITICAL_START
2006 #define HOTKEY_CONFIG_CRITICAL_END
2008 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2010 static struct mutex hotkey_mutex;
2012 static enum { /* Reasons for waking up */
2013 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2014 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2015 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2016 } hotkey_wakeup_reason;
2018 static int hotkey_autosleep_ack;
2020 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2021 static u32 hotkey_all_mask; /* all events supported in fw */
2022 static u32 hotkey_reserved_mask; /* events better left disabled */
2023 static u32 hotkey_driver_mask; /* events needed by the driver */
2024 static u32 hotkey_user_mask; /* events visible to userspace */
2025 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2027 static unsigned int hotkey_report_mode;
2029 static u16 *hotkey_keycode_map;
2031 static struct attribute_set *hotkey_dev_attributes;
2033 static void tpacpi_driver_event(const unsigned int hkey_event);
2034 static void hotkey_driver_event(const unsigned int scancode);
2035 static void hotkey_poll_setup(const bool may_warn);
2037 /* HKEY.MHKG() return bits */
2038 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2040 static int hotkey_get_wlsw(void)
2044 if (!tp_features.hotkey_wlsw)
2047 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2049 return (tpacpi_wlsw_emulstate) ?
2050 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2053 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2056 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2059 static int hotkey_get_tablet_mode(int *status)
2063 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2066 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2071 * Reads current event mask from firmware, and updates
2072 * hotkey_acpi_mask accordingly. Also resets any bits
2073 * from hotkey_user_mask that are unavailable to be
2074 * delivered (shadow requirement of the userspace ABI).
2076 * Call with hotkey_mutex held
2078 static int hotkey_mask_get(void)
2080 if (tp_features.hotkey_mask) {
2083 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2086 hotkey_acpi_mask = m;
2088 /* no mask support doesn't mean no event support... */
2089 hotkey_acpi_mask = hotkey_all_mask;
2092 /* sync userspace-visible mask */
2093 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2098 void static hotkey_mask_warn_incomplete_mask(void)
2100 /* log only what the user can fix... */
2101 const u32 wantedmask = hotkey_driver_mask &
2102 ~(hotkey_acpi_mask | hotkey_source_mask) &
2103 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2106 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2110 * Set the firmware mask when supported
2112 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2114 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2116 * Call with hotkey_mutex held
2118 static int hotkey_mask_set(u32 mask)
2123 const u32 fwmask = mask & ~hotkey_source_mask;
2125 if (tp_features.hotkey_mask) {
2126 for (i = 0; i < 32; i++) {
2127 if (!acpi_evalf(hkey_handle,
2128 NULL, "MHKM", "vdd", i + 1,
2129 !!(mask & (1 << i)))) {
2137 * We *must* make an inconditional call to hotkey_mask_get to
2138 * refresh hotkey_acpi_mask and update hotkey_user_mask
2140 * Take the opportunity to also log when we cannot _enable_
2143 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2144 pr_notice("asked for hotkey mask 0x%08x, but "
2145 "firmware forced it to 0x%08x\n",
2146 fwmask, hotkey_acpi_mask);
2149 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2150 hotkey_mask_warn_incomplete_mask();
2156 * Sets hotkey_user_mask and tries to set the firmware mask
2158 * Call with hotkey_mutex held
2160 static int hotkey_user_mask_set(const u32 mask)
2164 /* Give people a chance to notice they are doing something that
2165 * is bound to go boom on their users sooner or later */
2166 if (!tp_warned.hotkey_mask_ff &&
2167 (mask == 0xffff || mask == 0xffffff ||
2168 mask == 0xffffffff)) {
2169 tp_warned.hotkey_mask_ff = 1;
2170 pr_notice("setting the hotkey mask to 0x%08x is likely "
2171 "not the best way to go about it\n", mask);
2172 pr_notice("please consider using the driver defaults, "
2173 "and refer to up-to-date thinkpad-acpi "
2177 /* Try to enable what the user asked for, plus whatever we need.
2178 * this syncs everything but won't enable bits in hotkey_user_mask */
2179 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2181 /* Enable the available bits in hotkey_user_mask */
2182 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2188 * Sets the driver hotkey mask.
2190 * Can be called even if the hotkey subdriver is inactive
2192 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2196 /* Do the right thing if hotkey_init has not been called yet */
2197 if (!tp_features.hotkey) {
2198 hotkey_driver_mask = mask;
2202 mutex_lock(&hotkey_mutex);
2204 HOTKEY_CONFIG_CRITICAL_START
2205 hotkey_driver_mask = mask;
2206 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2207 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2209 HOTKEY_CONFIG_CRITICAL_END
2211 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2212 ~hotkey_source_mask);
2213 hotkey_poll_setup(true);
2215 mutex_unlock(&hotkey_mutex);
2220 static int hotkey_status_get(int *status)
2222 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2228 static int hotkey_status_set(bool enable)
2230 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2236 static void tpacpi_input_send_tabletsw(void)
2240 if (tp_features.hotkey_tablet &&
2241 !hotkey_get_tablet_mode(&state)) {
2242 mutex_lock(&tpacpi_inputdev_send_mutex);
2244 input_report_switch(tpacpi_inputdev,
2245 SW_TABLET_MODE, !!state);
2246 input_sync(tpacpi_inputdev);
2248 mutex_unlock(&tpacpi_inputdev_send_mutex);
2252 /* Do NOT call without validating scancode first */
2253 static void tpacpi_input_send_key(const unsigned int scancode)
2255 const unsigned int keycode = hotkey_keycode_map[scancode];
2257 if (keycode != KEY_RESERVED) {
2258 mutex_lock(&tpacpi_inputdev_send_mutex);
2260 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2261 input_report_key(tpacpi_inputdev, keycode, 1);
2262 input_sync(tpacpi_inputdev);
2264 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2265 input_report_key(tpacpi_inputdev, keycode, 0);
2266 input_sync(tpacpi_inputdev);
2268 mutex_unlock(&tpacpi_inputdev_send_mutex);
2272 /* Do NOT call without validating scancode first */
2273 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2275 hotkey_driver_event(scancode);
2276 if (hotkey_user_mask & (1 << scancode))
2277 tpacpi_input_send_key(scancode);
2280 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2281 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2283 /* Do NOT call without validating scancode first */
2284 static void tpacpi_hotkey_send_key(unsigned int scancode)
2286 tpacpi_input_send_key_masked(scancode);
2287 if (hotkey_report_mode < 2) {
2288 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2289 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2293 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2297 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2298 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2299 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2300 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2301 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2302 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2304 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2305 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2306 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2308 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2310 n->displayexp_toggle =
2311 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2313 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2315 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2316 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2317 n->brightness_toggle =
2318 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2320 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2321 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2322 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2323 >> TP_NVRAM_POS_LEVEL_VOLUME;
2324 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2325 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2329 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2330 struct tp_nvram_state *newn,
2331 const u32 event_mask)
2334 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2336 if ((event_mask & (1 << __scancode)) && \
2337 oldn->__member != newn->__member) \
2338 tpacpi_hotkey_send_key(__scancode); \
2341 #define TPACPI_MAY_SEND_KEY(__scancode) \
2343 if (event_mask & (1 << __scancode)) \
2344 tpacpi_hotkey_send_key(__scancode); \
2347 void issue_volchange(const unsigned int oldvol,
2348 const unsigned int newvol)
2350 unsigned int i = oldvol;
2352 while (i > newvol) {
2353 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2356 while (i < newvol) {
2357 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2362 void issue_brightnesschange(const unsigned int oldbrt,
2363 const unsigned int newbrt)
2365 unsigned int i = oldbrt;
2367 while (i > newbrt) {
2368 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2371 while (i < newbrt) {
2372 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2378 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2380 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2382 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2384 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2389 * This code is supposed to duplicate the IBM firmware behaviour:
2390 * - Pressing MUTE issues mute hotkey message, even when already mute
2391 * - Pressing Volume up/down issues volume up/down hotkey messages,
2392 * even when already at maximum or minimum volume
2393 * - The act of unmuting issues volume up/down notification,
2394 * depending which key was used to unmute
2396 * We are constrained to what the NVRAM can tell us, which is not much
2397 * and certainly not enough if more than one volume hotkey was pressed
2398 * since the last poll cycle.
2400 * Just to make our life interesting, some newer Lenovo ThinkPads have
2401 * bugs in the BIOS and may fail to update volume_toggle properly.
2406 oldn->volume_toggle != newn->volume_toggle ||
2407 oldn->volume_level != newn->volume_level) {
2408 /* recently muted, or repeated mute keypress, or
2409 * multiple presses ending in mute */
2410 issue_volchange(oldn->volume_level, newn->volume_level);
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2416 /* recently unmuted, issue 'unmute' keypress */
2417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2419 if (oldn->volume_level != newn->volume_level) {
2420 issue_volchange(oldn->volume_level, newn->volume_level);
2421 } else if (oldn->volume_toggle != newn->volume_toggle) {
2422 /* repeated vol up/down keypress at end of scale ? */
2423 if (newn->volume_level == 0)
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2425 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 /* handle brightness */
2431 if (oldn->brightness_level != newn->brightness_level) {
2432 issue_brightnesschange(oldn->brightness_level,
2433 newn->brightness_level);
2434 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2435 /* repeated key presses that didn't change state */
2436 if (newn->brightness_level == 0)
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2438 else if (newn->brightness_level >= bright_maxlvl
2439 && !tp_features.bright_unkfw)
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2443 #undef TPACPI_COMPARE_KEY
2444 #undef TPACPI_MAY_SEND_KEY
2450 * We track all events in hotkey_source_mask all the time, since
2451 * most of them are edge-based. We only issue those requested by
2452 * hotkey_user_mask or hotkey_driver_mask, though.
2454 static int hotkey_kthread(void *data)
2456 struct tp_nvram_state s[2];
2457 u32 poll_mask, event_mask;
2458 unsigned int si, so;
2460 unsigned int change_detector;
2461 unsigned int poll_freq;
2464 mutex_lock(&hotkey_thread_mutex);
2466 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2475 /* Initial state for compares */
2476 mutex_lock(&hotkey_thread_data_mutex);
2477 change_detector = hotkey_config_change;
2478 poll_mask = hotkey_source_mask;
2479 event_mask = hotkey_source_mask &
2480 (hotkey_driver_mask | hotkey_user_mask);
2481 poll_freq = hotkey_poll_freq;
2482 mutex_unlock(&hotkey_thread_data_mutex);
2483 hotkey_read_nvram(&s[so], poll_mask);
2485 while (!kthread_should_stop()) {
2487 if (likely(poll_freq))
2490 t = 100; /* should never happen... */
2492 t = msleep_interruptible(t);
2493 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2496 if (t > 0 && !was_frozen)
2499 mutex_lock(&hotkey_thread_data_mutex);
2500 if (was_frozen || hotkey_config_change != change_detector) {
2501 /* forget old state on thaw or config change */
2504 change_detector = hotkey_config_change;
2506 poll_mask = hotkey_source_mask;
2507 event_mask = hotkey_source_mask &
2508 (hotkey_driver_mask | hotkey_user_mask);
2509 poll_freq = hotkey_poll_freq;
2510 mutex_unlock(&hotkey_thread_data_mutex);
2512 if (likely(poll_mask)) {
2513 hotkey_read_nvram(&s[si], poll_mask);
2514 if (likely(si != so)) {
2515 hotkey_compare_and_issue_event(&s[so], &s[si],
2525 mutex_unlock(&hotkey_thread_mutex);
2529 /* call with hotkey_mutex held */
2530 static void hotkey_poll_stop_sync(void)
2532 if (tpacpi_hotkey_task) {
2533 kthread_stop(tpacpi_hotkey_task);
2534 tpacpi_hotkey_task = NULL;
2535 mutex_lock(&hotkey_thread_mutex);
2536 /* at this point, the thread did exit */
2537 mutex_unlock(&hotkey_thread_mutex);
2541 /* call with hotkey_mutex held */
2542 static void hotkey_poll_setup(const bool may_warn)
2544 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2545 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2547 if (hotkey_poll_freq > 0 &&
2548 (poll_driver_mask ||
2549 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2550 if (!tpacpi_hotkey_task) {
2551 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2552 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2553 if (IS_ERR(tpacpi_hotkey_task)) {
2554 tpacpi_hotkey_task = NULL;
2555 pr_err("could not create kernel thread "
2556 "for hotkey polling\n");
2560 hotkey_poll_stop_sync();
2561 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2562 hotkey_poll_freq == 0) {
2563 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2564 "require polling, which is currently "
2566 poll_user_mask, poll_driver_mask);
2571 static void hotkey_poll_setup_safe(const bool may_warn)
2573 mutex_lock(&hotkey_mutex);
2574 hotkey_poll_setup(may_warn);
2575 mutex_unlock(&hotkey_mutex);
2578 /* call with hotkey_mutex held */
2579 static void hotkey_poll_set_freq(unsigned int freq)
2582 hotkey_poll_stop_sync();
2584 hotkey_poll_freq = freq;
2587 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2589 static void hotkey_poll_setup(const bool __unused)
2593 static void hotkey_poll_setup_safe(const bool __unused)
2597 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2599 static int hotkey_inputdev_open(struct input_dev *dev)
2601 switch (tpacpi_lifecycle) {
2602 case TPACPI_LIFE_INIT:
2603 case TPACPI_LIFE_RUNNING:
2604 hotkey_poll_setup_safe(false);
2606 case TPACPI_LIFE_EXITING:
2610 /* Should only happen if tpacpi_lifecycle is corrupt */
2615 static void hotkey_inputdev_close(struct input_dev *dev)
2617 /* disable hotkey polling when possible */
2618 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2619 !(hotkey_source_mask & hotkey_driver_mask))
2620 hotkey_poll_setup_safe(false);
2623 /* sysfs hotkey enable ------------------------------------------------- */
2624 static ssize_t hotkey_enable_show(struct device *dev,
2625 struct device_attribute *attr,
2630 printk_deprecated_attribute("hotkey_enable",
2631 "Hotkey reporting is always enabled");
2633 res = hotkey_status_get(&status);
2637 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2640 static ssize_t hotkey_enable_store(struct device *dev,
2641 struct device_attribute *attr,
2642 const char *buf, size_t count)
2646 printk_deprecated_attribute("hotkey_enable",
2647 "Hotkeys can be disabled through hotkey_mask");
2649 if (parse_strtoul(buf, 1, &t))
2658 static struct device_attribute dev_attr_hotkey_enable =
2659 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2660 hotkey_enable_show, hotkey_enable_store);
2662 /* sysfs hotkey mask --------------------------------------------------- */
2663 static ssize_t hotkey_mask_show(struct device *dev,
2664 struct device_attribute *attr,
2667 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2670 static ssize_t hotkey_mask_store(struct device *dev,
2671 struct device_attribute *attr,
2672 const char *buf, size_t count)
2677 if (parse_strtoul(buf, 0xffffffffUL, &t))
2680 if (mutex_lock_killable(&hotkey_mutex))
2681 return -ERESTARTSYS;
2683 res = hotkey_user_mask_set(t);
2685 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2686 hotkey_poll_setup(true);
2689 mutex_unlock(&hotkey_mutex);
2691 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2693 return (res) ? res : count;
2696 static struct device_attribute dev_attr_hotkey_mask =
2697 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2698 hotkey_mask_show, hotkey_mask_store);
2700 /* sysfs hotkey bios_enabled ------------------------------------------- */
2701 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2702 struct device_attribute *attr,
2705 return sprintf(buf, "0\n");
2708 static struct device_attribute dev_attr_hotkey_bios_enabled =
2709 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2711 /* sysfs hotkey bios_mask ---------------------------------------------- */
2712 static ssize_t hotkey_bios_mask_show(struct device *dev,
2713 struct device_attribute *attr,
2716 printk_deprecated_attribute("hotkey_bios_mask",
2717 "This attribute is useless.");
2718 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2721 static struct device_attribute dev_attr_hotkey_bios_mask =
2722 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2724 /* sysfs hotkey all_mask ----------------------------------------------- */
2725 static ssize_t hotkey_all_mask_show(struct device *dev,
2726 struct device_attribute *attr,
2729 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2730 hotkey_all_mask | hotkey_source_mask);
2733 static struct device_attribute dev_attr_hotkey_all_mask =
2734 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2736 /* sysfs hotkey recommended_mask --------------------------------------- */
2737 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2738 struct device_attribute *attr,
2741 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2742 (hotkey_all_mask | hotkey_source_mask)
2743 & ~hotkey_reserved_mask);
2746 static struct device_attribute dev_attr_hotkey_recommended_mask =
2747 __ATTR(hotkey_recommended_mask, S_IRUGO,
2748 hotkey_recommended_mask_show, NULL);
2750 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2752 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2753 static ssize_t hotkey_source_mask_show(struct device *dev,
2754 struct device_attribute *attr,
2757 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2760 static ssize_t hotkey_source_mask_store(struct device *dev,
2761 struct device_attribute *attr,
2762 const char *buf, size_t count)
2768 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2769 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2772 if (mutex_lock_killable(&hotkey_mutex))
2773 return -ERESTARTSYS;
2775 HOTKEY_CONFIG_CRITICAL_START
2776 hotkey_source_mask = t;
2777 HOTKEY_CONFIG_CRITICAL_END
2779 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2780 ~hotkey_source_mask);
2781 hotkey_poll_setup(true);
2783 /* check if events needed by the driver got disabled */
2784 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2785 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2787 mutex_unlock(&hotkey_mutex);
2790 pr_err("hotkey_source_mask: "
2791 "failed to update the firmware event mask!\n");
2794 pr_notice("hotkey_source_mask: "
2795 "some important events were disabled: 0x%04x\n",
2798 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2800 return (rc < 0) ? rc : count;
2803 static struct device_attribute dev_attr_hotkey_source_mask =
2804 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2805 hotkey_source_mask_show, hotkey_source_mask_store);
2807 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2808 static ssize_t hotkey_poll_freq_show(struct device *dev,
2809 struct device_attribute *attr,
2812 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2815 static ssize_t hotkey_poll_freq_store(struct device *dev,
2816 struct device_attribute *attr,
2817 const char *buf, size_t count)
2821 if (parse_strtoul(buf, 25, &t))
2824 if (mutex_lock_killable(&hotkey_mutex))
2825 return -ERESTARTSYS;
2827 hotkey_poll_set_freq(t);
2828 hotkey_poll_setup(true);
2830 mutex_unlock(&hotkey_mutex);
2832 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2837 static struct device_attribute dev_attr_hotkey_poll_freq =
2838 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2839 hotkey_poll_freq_show, hotkey_poll_freq_store);
2841 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2843 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2844 static ssize_t hotkey_radio_sw_show(struct device *dev,
2845 struct device_attribute *attr,
2849 res = hotkey_get_wlsw();
2853 /* Opportunistic update */
2854 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2856 return snprintf(buf, PAGE_SIZE, "%d\n",
2857 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2860 static struct device_attribute dev_attr_hotkey_radio_sw =
2861 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2863 static void hotkey_radio_sw_notify_change(void)
2865 if (tp_features.hotkey_wlsw)
2866 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2870 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2871 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2872 struct device_attribute *attr,
2876 res = hotkey_get_tablet_mode(&s);
2880 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2883 static struct device_attribute dev_attr_hotkey_tablet_mode =
2884 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2886 static void hotkey_tablet_mode_notify_change(void)
2888 if (tp_features.hotkey_tablet)
2889 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2890 "hotkey_tablet_mode");
2893 /* sysfs hotkey report_mode -------------------------------------------- */
2894 static ssize_t hotkey_report_mode_show(struct device *dev,
2895 struct device_attribute *attr,
2898 return snprintf(buf, PAGE_SIZE, "%d\n",
2899 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2902 static struct device_attribute dev_attr_hotkey_report_mode =
2903 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2905 /* sysfs wakeup reason (pollable) -------------------------------------- */
2906 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2907 struct device_attribute *attr,
2910 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2913 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2914 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2916 static void hotkey_wakeup_reason_notify_change(void)
2918 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2922 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2923 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2924 struct device_attribute *attr,
2927 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2930 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2931 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2932 hotkey_wakeup_hotunplug_complete_show, NULL);
2934 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2936 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2937 "wakeup_hotunplug_complete");
2940 /* --------------------------------------------------------------------- */
2942 static struct attribute *hotkey_attributes[] __initdata = {
2943 &dev_attr_hotkey_enable.attr,
2944 &dev_attr_hotkey_bios_enabled.attr,
2945 &dev_attr_hotkey_bios_mask.attr,
2946 &dev_attr_hotkey_report_mode.attr,
2947 &dev_attr_hotkey_wakeup_reason.attr,
2948 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2949 &dev_attr_hotkey_mask.attr,
2950 &dev_attr_hotkey_all_mask.attr,
2951 &dev_attr_hotkey_recommended_mask.attr,
2952 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2953 &dev_attr_hotkey_source_mask.attr,
2954 &dev_attr_hotkey_poll_freq.attr,
2959 * Sync both the hw and sw blocking state of all switches
2961 static void tpacpi_send_radiosw_update(void)
2966 * We must sync all rfkill controllers *before* issuing any
2967 * rfkill input events, or we will race the rfkill core input
2970 * tpacpi_inputdev_send_mutex works as a synchronization point
2973 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2976 wlsw = hotkey_get_wlsw();
2978 /* Sync hw blocking state first if it is hw-blocked */
2979 if (wlsw == TPACPI_RFK_RADIO_OFF)
2980 tpacpi_rfk_update_hwblock_state(true);
2982 /* Sync sw blocking state */
2983 tpacpi_rfk_update_swstate_all();
2985 /* Sync hw blocking state last if it is hw-unblocked */
2986 if (wlsw == TPACPI_RFK_RADIO_ON)
2987 tpacpi_rfk_update_hwblock_state(false);
2989 /* Issue rfkill input event for WLSW switch */
2991 mutex_lock(&tpacpi_inputdev_send_mutex);
2993 input_report_switch(tpacpi_inputdev,
2994 SW_RFKILL_ALL, (wlsw > 0));
2995 input_sync(tpacpi_inputdev);
2997 mutex_unlock(&tpacpi_inputdev_send_mutex);
3001 * this can be unconditional, as we will poll state again
3002 * if userspace uses the notify to read data
3004 hotkey_radio_sw_notify_change();
3007 static void hotkey_exit(void)
3009 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3010 mutex_lock(&hotkey_mutex);
3011 hotkey_poll_stop_sync();
3012 mutex_unlock(&hotkey_mutex);
3015 if (hotkey_dev_attributes)
3016 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3018 kfree(hotkey_keycode_map);
3020 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3021 "restoring original HKEY status and mask\n");
3022 /* yes, there is a bitwise or below, we want the
3023 * functions to be called even if one of them fail */
3024 if (((tp_features.hotkey_mask &&
3025 hotkey_mask_set(hotkey_orig_mask)) |
3026 hotkey_status_set(false)) != 0)
3027 pr_err("failed to restore hot key mask "
3028 "to BIOS defaults\n");
3031 static void __init hotkey_unmap(const unsigned int scancode)
3033 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3034 clear_bit(hotkey_keycode_map[scancode],
3035 tpacpi_inputdev->keybit);
3036 hotkey_keycode_map[scancode] = KEY_RESERVED;
3042 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3045 #define TPACPI_HK_Q_INIMASK 0x0001
3047 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3048 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3049 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3050 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3051 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3052 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3053 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3054 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3055 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3056 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3057 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3058 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3059 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3060 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3061 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3062 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3063 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3064 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3065 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3066 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3069 typedef u16 tpacpi_keymap_entry_t;
3070 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3072 static int __init hotkey_init(struct ibm_init_struct *iibm)
3074 /* Requirements for changing the default keymaps:
3076 * 1. Many of the keys are mapped to KEY_RESERVED for very
3077 * good reasons. Do not change them unless you have deep
3078 * knowledge on the IBM and Lenovo ThinkPad firmware for
3079 * the various ThinkPad models. The driver behaves
3080 * differently for KEY_RESERVED: such keys have their
3081 * hot key mask *unset* in mask_recommended, and also
3082 * in the initial hot key mask programmed into the
3083 * firmware at driver load time, which means the firm-
3084 * ware may react very differently if you change them to
3087 * 2. You must be subscribed to the linux-thinkpad and
3088 * ibm-acpi-devel mailing lists, and you should read the
3089 * list archives since 2007 if you want to change the
3090 * keymaps. This requirement exists so that you will
3091 * know the past history of problems with the thinkpad-
3092 * acpi driver keymaps, and also that you will be
3093 * listening to any bug reports;
3095 * 3. Do not send thinkpad-acpi specific patches directly to
3096 * for merging, *ever*. Send them to the linux-acpi
3097 * mailinglist for comments. Merging is to be done only
3098 * through acpi-test and the ACPI maintainer.
3100 * If the above is too much to ask, don't change the keymap.
3101 * Ask the thinkpad-acpi maintainer to do it, instead.
3105 TPACPI_KEYMAP_IBM_GENERIC = 0,
3106 TPACPI_KEYMAP_LENOVO_GENERIC,
3109 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3110 /* Generic keymap for IBM ThinkPads */
3111 [TPACPI_KEYMAP_IBM_GENERIC] = {
3112 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3113 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3114 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3115 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3117 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3118 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3119 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3120 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3122 /* brightness: firmware always reacts to them */
3123 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3124 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3126 /* Thinklight: firmware always react to it */
3127 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3129 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3130 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3132 /* Volume: firmware always react to it and reprograms
3133 * the built-in *extra* mixer. Never map it to control
3134 * another mixer by default. */
3135 KEY_RESERVED, /* 0x14: VOLUME UP */
3136 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3137 KEY_RESERVED, /* 0x16: MUTE */
3139 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3141 /* (assignments unknown, please report if found) */
3142 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3143 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3146 /* Generic keymap for Lenovo ThinkPads */
3147 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3148 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3149 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3150 KEY_WLAN, KEY_CAMERA, 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 /* These should be enabled --only-- when ACPI video
3159 * is disabled (i.e. in "vendor" mode), and are handled
3160 * in a special way by the init code */
3161 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3162 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3164 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3166 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3167 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3169 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3170 * react to it and reprograms the built-in *extra* mixer.
3171 * Never map it to control another mixer by default.
3173 * T60?, T61, R60?, R61: firmware and EC tries to send
3174 * these over the regular keyboard, so these are no-ops,
3175 * but there are still weird bugs re. MUTE, so do not
3176 * change unless you get test reports from all Lenovo
3177 * models. May cause the BIOS to interfere with the
3180 KEY_RESERVED, /* 0x14: VOLUME UP */
3181 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3182 KEY_RESERVED, /* 0x16: MUTE */
3184 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3186 /* (assignments unknown, please report if found) */
3187 KEY_UNKNOWN, KEY_UNKNOWN,
3190 * The mic mute button only sends 0x1a. It does not
3191 * automatically mute the mic or change the mute light.
3193 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3195 /* (assignments unknown, please report if found) */
3196 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3201 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3202 /* Generic maps (fallback) */
3204 .vendor = PCI_VENDOR_ID_IBM,
3205 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3206 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3209 .vendor = PCI_VENDOR_ID_LENOVO,
3210 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3211 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3215 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3216 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3221 bool radiosw_state = false;
3222 bool tabletsw_state = false;
3224 unsigned long quirks;
3225 unsigned long keymap_id;
3227 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3228 "initializing hotkey subdriver\n");
3230 BUG_ON(!tpacpi_inputdev);
3231 BUG_ON(tpacpi_inputdev->open != NULL ||
3232 tpacpi_inputdev->close != NULL);
3234 TPACPI_ACPIHANDLE_INIT(hkey);
3235 mutex_init(&hotkey_mutex);
3237 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3238 mutex_init(&hotkey_thread_mutex);
3239 mutex_init(&hotkey_thread_data_mutex);
3242 /* hotkey not supported on 570 */
3243 tp_features.hotkey = hkey_handle != NULL;
3245 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3247 str_supported(tp_features.hotkey));
3249 if (!tp_features.hotkey)
3252 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3253 ARRAY_SIZE(tpacpi_hotkey_qtable));
3255 tpacpi_disable_brightness_delay();
3257 /* MUST have enough space for all attributes to be added to
3258 * hotkey_dev_attributes */
3259 hotkey_dev_attributes = create_attr_set(
3260 ARRAY_SIZE(hotkey_attributes) + 2,
3262 if (!hotkey_dev_attributes)
3264 res = add_many_to_attr_set(hotkey_dev_attributes,
3266 ARRAY_SIZE(hotkey_attributes));
3270 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3271 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3272 for HKEY interface version 0x100 */
3273 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3274 if ((hkeyv >> 8) != 1) {
3275 pr_err("unknown version of the HKEY interface: 0x%x\n",
3277 pr_err("please report this to %s\n", TPACPI_MAIL);
3280 * MHKV 0x100 in A31, R40, R40e,
3281 * T4x, X31, and later
3283 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3284 "firmware HKEY interface version: 0x%x\n",
3287 /* Paranoia check AND init hotkey_all_mask */
3288 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3290 pr_err("missing MHKA handler, "
3291 "please report this to %s\n",
3293 /* Fallback: pre-init for FN+F3,F4,F12 */
3294 hotkey_all_mask = 0x080cU;
3296 tp_features.hotkey_mask = 1;
3301 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3302 "hotkey masks are %s\n",
3303 str_supported(tp_features.hotkey_mask));
3305 /* Init hotkey_all_mask if not initialized yet */
3306 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3307 (quirks & TPACPI_HK_Q_INIMASK))
3308 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3310 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3311 if (tp_features.hotkey_mask) {
3312 /* hotkey_source_mask *must* be zero for
3313 * the first hotkey_mask_get to return hotkey_orig_mask */
3314 res = hotkey_mask_get();
3318 hotkey_orig_mask = hotkey_acpi_mask;
3320 hotkey_orig_mask = hotkey_all_mask;
3321 hotkey_acpi_mask = hotkey_all_mask;
3324 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3326 tp_features.hotkey_wlsw = 1;
3327 radiosw_state = !!tpacpi_wlsw_emulstate;
3328 pr_info("radio switch emulation enabled\n");
3331 /* Not all thinkpads have a hardware radio switch */
3332 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3333 tp_features.hotkey_wlsw = 1;
3334 radiosw_state = !!status;
3335 pr_info("radio switch found; radios are %s\n",
3336 enabled(status, 0));
3338 if (tp_features.hotkey_wlsw)
3339 res = add_to_attr_set(hotkey_dev_attributes,
3340 &dev_attr_hotkey_radio_sw.attr);
3342 /* For X41t, X60t, X61t Tablets... */
3343 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3344 tp_features.hotkey_tablet = 1;
3345 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3346 pr_info("possible tablet mode switch found; "
3347 "ThinkPad in %s mode\n",
3348 (tabletsw_state) ? "tablet" : "laptop");
3349 res = add_to_attr_set(hotkey_dev_attributes,
3350 &dev_attr_hotkey_tablet_mode.attr);
3354 res = register_attr_set_with_sysfs(
3355 hotkey_dev_attributes,
3356 &tpacpi_pdev->dev.kobj);
3360 /* Set up key map */
3361 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3363 if (!hotkey_keycode_map) {
3364 pr_err("failed to allocate memory for key map\n");
3369 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3370 ARRAY_SIZE(tpacpi_keymap_qtable));
3371 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3372 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3373 "using keymap number %lu\n", keymap_id);
3375 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3376 TPACPI_HOTKEY_MAP_SIZE);
3378 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3379 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3380 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3381 tpacpi_inputdev->keycode = hotkey_keycode_map;
3382 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3383 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3384 input_set_capability(tpacpi_inputdev, EV_KEY,
3385 hotkey_keycode_map[i]);
3387 if (i < sizeof(hotkey_reserved_mask)*8)
3388 hotkey_reserved_mask |= 1 << i;
3392 if (tp_features.hotkey_wlsw) {
3393 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3394 input_report_switch(tpacpi_inputdev,
3395 SW_RFKILL_ALL, radiosw_state);
3397 if (tp_features.hotkey_tablet) {
3398 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3399 input_report_switch(tpacpi_inputdev,
3400 SW_TABLET_MODE, tabletsw_state);
3403 /* Do not issue duplicate brightness change events to
3404 * userspace. tpacpi_detect_brightness_capabilities() must have
3405 * been called before this point */
3406 if (acpi_video_backlight_support()) {
3407 pr_info("This ThinkPad has standard ACPI backlight "
3408 "brightness control, supported by the ACPI "
3410 pr_notice("Disabling thinkpad-acpi brightness events "
3413 /* Disable brightness up/down on Lenovo thinkpads when
3414 * ACPI is handling them, otherwise it is plain impossible
3415 * for userspace to do something even remotely sane */
3416 hotkey_reserved_mask |=
3417 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3418 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3419 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3420 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3424 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3426 & ~hotkey_reserved_mask;
3428 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3429 "hotkey source mask 0x%08x, polling freq %u\n",
3430 hotkey_source_mask, hotkey_poll_freq);
3433 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3434 "enabling firmware HKEY event interface...\n");
3435 res = hotkey_status_set(true);
3440 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3441 | hotkey_driver_mask)
3442 & ~hotkey_source_mask);
3443 if (res < 0 && res != -ENXIO) {
3447 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3448 & ~hotkey_reserved_mask;
3449 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3450 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3451 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3453 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3454 "legacy ibm/hotkey event reporting over procfs %s\n",
3455 (hotkey_report_mode < 2) ?
3456 "enabled" : "disabled");
3458 tpacpi_inputdev->open = &hotkey_inputdev_open;
3459 tpacpi_inputdev->close = &hotkey_inputdev_close;
3461 hotkey_poll_setup_safe(true);
3466 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3467 hotkey_dev_attributes = NULL;
3469 return (res < 0)? res : 1;
3472 static bool hotkey_notify_hotkey(const u32 hkey,
3474 bool *ignore_acpi_ev)
3476 /* 0x1000-0x1FFF: key presses */
3477 unsigned int scancode = hkey & 0xfff;
3478 *send_acpi_ev = true;
3479 *ignore_acpi_ev = false;
3481 /* HKEY event 0x1001 is scancode 0x00 */
3482 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3484 if (!(hotkey_source_mask & (1 << scancode))) {
3485 tpacpi_input_send_key_masked(scancode);
3486 *send_acpi_ev = false;
3488 *ignore_acpi_ev = true;
3495 static bool hotkey_notify_wakeup(const u32 hkey,
3497 bool *ignore_acpi_ev)
3499 /* 0x2000-0x2FFF: Wakeup reason */
3500 *send_acpi_ev = true;
3501 *ignore_acpi_ev = false;
3504 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3505 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3506 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3507 *ignore_acpi_ev = true;
3510 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3511 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3512 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3513 *ignore_acpi_ev = true;
3516 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3517 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3518 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3519 /* how to auto-heal: */
3520 /* 2313: woke up from S3, go to S4/S5 */
3521 /* 2413: woke up from S4, go to S5 */
3528 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3529 pr_info("woke up due to a hot-unplug request...\n");
3530 hotkey_wakeup_reason_notify_change();
3535 static bool hotkey_notify_dockevent(const u32 hkey,
3537 bool *ignore_acpi_ev)
3539 /* 0x4000-0x4FFF: dock-related events */
3540 *send_acpi_ev = true;
3541 *ignore_acpi_ev = false;
3544 case TP_HKEY_EV_UNDOCK_ACK:
3545 /* ACPI undock operation completed after wakeup */
3546 hotkey_autosleep_ack = 1;
3547 pr_info("undocked\n");
3548 hotkey_wakeup_hotunplug_complete_notify_change();
3551 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3552 pr_info("docked into hotplug port replicator\n");
3554 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3555 pr_info("undocked from hotplug port replicator\n");
3563 static bool hotkey_notify_usrevent(const u32 hkey,
3565 bool *ignore_acpi_ev)
3567 /* 0x5000-0x5FFF: human interface helpers */
3568 *send_acpi_ev = true;
3569 *ignore_acpi_ev = false;
3572 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3573 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3576 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3577 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3578 tpacpi_input_send_tabletsw();
3579 hotkey_tablet_mode_notify_change();
3580 *send_acpi_ev = false;
3583 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3584 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3585 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3586 /* do not propagate these events */
3587 *ignore_acpi_ev = true;
3595 static void thermal_dump_all_sensors(void);
3597 static bool hotkey_notify_6xxx(const u32 hkey,
3599 bool *ignore_acpi_ev)
3603 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3604 *send_acpi_ev = true;
3605 *ignore_acpi_ev = false;
3608 case TP_HKEY_EV_THM_TABLE_CHANGED:
3609 pr_info("EC reports that Thermal Table has changed\n");
3610 /* recommended action: do nothing, we don't have
3611 * Lenovo ATM information */
3613 case TP_HKEY_EV_ALARM_BAT_HOT:
3614 pr_crit("THERMAL ALARM: battery is too hot!\n");
3615 /* recommended action: warn user through gui */
3617 case TP_HKEY_EV_ALARM_BAT_XHOT:
3618 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3619 /* recommended action: immediate sleep/hibernate */
3621 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3622 pr_crit("THERMAL ALARM: "
3623 "a sensor reports something is too hot!\n");
3624 /* recommended action: warn user through gui, that */
3625 /* some internal component is too hot */
3627 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3628 pr_alert("THERMAL EMERGENCY: "
3629 "a sensor reports something is extremely hot!\n");
3630 /* recommended action: immediate sleep/hibernate */
3633 case TP_HKEY_EV_KEY_NUMLOCK:
3634 case TP_HKEY_EV_KEY_FN:
3635 /* key press events, we just ignore them as long as the EC
3636 * is still reporting them in the normal keyboard stream */
3637 *send_acpi_ev = false;
3638 *ignore_acpi_ev = true;
3642 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3646 thermal_dump_all_sensors();
3651 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3655 bool ignore_acpi_ev;
3658 if (event != 0x80) {
3659 pr_err("unknown HKEY notification event %d\n", event);
3660 /* forward it to userspace, maybe it knows how to handle it */
3661 acpi_bus_generate_netlink_event(
3662 ibm->acpi->device->pnp.device_class,
3663 dev_name(&ibm->acpi->device->dev),
3669 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3670 pr_err("failed to retrieve HKEY event\n");
3679 send_acpi_ev = true;
3680 ignore_acpi_ev = false;
3682 switch (hkey >> 12) {
3684 /* 0x1000-0x1FFF: key presses */
3685 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3689 /* 0x2000-0x2FFF: Wakeup reason */
3690 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3694 /* 0x3000-0x3FFF: bay-related wakeups */
3696 case TP_HKEY_EV_BAYEJ_ACK:
3697 hotkey_autosleep_ack = 1;
3698 pr_info("bay ejected\n");
3699 hotkey_wakeup_hotunplug_complete_notify_change();
3702 case TP_HKEY_EV_OPTDRV_EJ:
3703 /* FIXME: kick libata if SATA link offline */
3711 /* 0x4000-0x4FFF: dock-related events */
3712 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3716 /* 0x5000-0x5FFF: human interface helpers */
3717 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3721 /* 0x6000-0x6FFF: thermal alarms/notices and
3722 * keyboard events */
3723 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3727 /* 0x7000-0x7FFF: misc */
3728 if (tp_features.hotkey_wlsw &&
3729 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3730 tpacpi_send_radiosw_update();
3735 /* fallthrough to default */
3740 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3741 pr_notice("please report the conditions when this "
3742 "event happened to %s\n", TPACPI_MAIL);
3746 if (!ignore_acpi_ev &&
3747 (send_acpi_ev || hotkey_report_mode < 2)) {
3748 acpi_bus_generate_proc_event(ibm->acpi->device,
3752 /* netlink events */
3753 if (!ignore_acpi_ev && send_acpi_ev) {
3754 acpi_bus_generate_netlink_event(
3755 ibm->acpi->device->pnp.device_class,
3756 dev_name(&ibm->acpi->device->dev),
3762 static void hotkey_suspend(void)
3764 /* Do these on suspend, we get the events on early resume! */
3765 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3766 hotkey_autosleep_ack = 0;
3769 static void hotkey_resume(void)
3771 tpacpi_disable_brightness_delay();
3773 if (hotkey_status_set(true) < 0 ||
3774 hotkey_mask_set(hotkey_acpi_mask) < 0)
3775 pr_err("error while attempting to reset the event "
3776 "firmware interface\n");
3778 tpacpi_send_radiosw_update();
3779 hotkey_tablet_mode_notify_change();
3780 hotkey_wakeup_reason_notify_change();
3781 hotkey_wakeup_hotunplug_complete_notify_change();
3782 hotkey_poll_setup_safe(false);
3785 /* procfs -------------------------------------------------------------- */
3786 static int hotkey_read(struct seq_file *m)
3790 if (!tp_features.hotkey) {
3791 seq_printf(m, "status:\t\tnot supported\n");
3795 if (mutex_lock_killable(&hotkey_mutex))
3796 return -ERESTARTSYS;
3797 res = hotkey_status_get(&status);
3799 res = hotkey_mask_get();
3800 mutex_unlock(&hotkey_mutex);
3804 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3805 if (hotkey_all_mask) {
3806 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3807 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3809 seq_printf(m, "mask:\t\tnot supported\n");
3810 seq_printf(m, "commands:\tenable, disable, reset\n");
3816 static void hotkey_enabledisable_warn(bool enable)
3818 tpacpi_log_usertask("procfs hotkey enable/disable");
3819 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3820 pr_fmt("hotkey enable/disable functionality has been "
3821 "removed from the driver. "
3822 "Hotkeys are always enabled.\n")))
3823 pr_err("Please remove the hotkey=enable module "
3824 "parameter, it is deprecated. "
3825 "Hotkeys are always enabled.\n");
3828 static int hotkey_write(char *buf)
3834 if (!tp_features.hotkey)
3837 if (mutex_lock_killable(&hotkey_mutex))
3838 return -ERESTARTSYS;
3840 mask = hotkey_user_mask;
3843 while ((cmd = next_cmd(&buf))) {
3844 if (strlencmp(cmd, "enable") == 0) {
3845 hotkey_enabledisable_warn(1);
3846 } else if (strlencmp(cmd, "disable") == 0) {
3847 hotkey_enabledisable_warn(0);
3849 } else if (strlencmp(cmd, "reset") == 0) {
3850 mask = (hotkey_all_mask | hotkey_source_mask)
3851 & ~hotkey_reserved_mask;
3852 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3854 } else if (sscanf(cmd, "%x", &mask) == 1) {
3863 tpacpi_disclose_usertask("procfs hotkey",
3864 "set mask to 0x%08x\n", mask);
3865 res = hotkey_user_mask_set(mask);
3869 mutex_unlock(&hotkey_mutex);
3873 static const struct acpi_device_id ibm_htk_device_ids[] = {
3874 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3875 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3879 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3880 .hid = ibm_htk_device_ids,
3881 .notify = hotkey_notify,
3882 .handle = &hkey_handle,
3883 .type = ACPI_DEVICE_NOTIFY,
3886 static struct ibm_struct hotkey_driver_data = {
3888 .read = hotkey_read,
3889 .write = hotkey_write,
3890 .exit = hotkey_exit,
3891 .resume = hotkey_resume,
3892 .suspend = hotkey_suspend,
3893 .acpi = &ibm_hotkey_acpidriver,
3896 /*************************************************************************
3897 * Bluetooth subdriver
3901 /* ACPI GBDC/SBDC bits */
3902 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3903 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3904 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3905 0 = disable, 1 = enable */
3909 /* ACPI \BLTH commands */
3910 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3911 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3912 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3913 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3914 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3917 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3919 static int bluetooth_get_status(void)
3923 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3924 if (dbg_bluetoothemul)
3925 return (tpacpi_bluetooth_emulstate) ?
3926 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3929 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3932 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3933 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3936 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3940 vdbg_printk(TPACPI_DBG_RFKILL,
3941 "will attempt to %s bluetooth\n",
3942 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3944 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3945 if (dbg_bluetoothemul) {
3946 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3951 if (state == TPACPI_RFK_RADIO_ON)
3952 status = TP_ACPI_BLUETOOTH_RADIOSSW
3953 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3957 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3963 /* sysfs bluetooth enable ---------------------------------------------- */
3964 static ssize_t bluetooth_enable_show(struct device *dev,
3965 struct device_attribute *attr,
3968 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3972 static ssize_t bluetooth_enable_store(struct device *dev,
3973 struct device_attribute *attr,
3974 const char *buf, size_t count)
3976 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3980 static struct device_attribute dev_attr_bluetooth_enable =
3981 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3982 bluetooth_enable_show, bluetooth_enable_store);
3984 /* --------------------------------------------------------------------- */
3986 static struct attribute *bluetooth_attributes[] = {
3987 &dev_attr_bluetooth_enable.attr,
3991 static const struct attribute_group bluetooth_attr_group = {
3992 .attrs = bluetooth_attributes,
3995 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3996 .get_status = bluetooth_get_status,
3997 .set_status = bluetooth_set_status,
4000 static void bluetooth_shutdown(void)
4002 /* Order firmware to save current state to NVRAM */
4003 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4004 TP_ACPI_BLTH_SAVE_STATE))
4005 pr_notice("failed to save bluetooth state to NVRAM\n");
4007 vdbg_printk(TPACPI_DBG_RFKILL,
4008 "bluetooth state saved to NVRAM\n");
4011 static void bluetooth_exit(void)
4013 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4014 &bluetooth_attr_group);
4016 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4018 bluetooth_shutdown();
4021 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4026 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4027 "initializing bluetooth subdriver\n");
4029 TPACPI_ACPIHANDLE_INIT(hkey);
4031 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4032 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4033 tp_features.bluetooth = hkey_handle &&
4034 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4036 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4037 "bluetooth is %s, status 0x%02x\n",
4038 str_supported(tp_features.bluetooth),
4041 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4042 if (dbg_bluetoothemul) {
4043 tp_features.bluetooth = 1;
4044 pr_info("bluetooth switch emulation enabled\n");
4047 if (tp_features.bluetooth &&
4048 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4049 /* no bluetooth hardware present in system */
4050 tp_features.bluetooth = 0;
4051 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4052 "bluetooth hardware not installed\n");
4055 if (!tp_features.bluetooth)
4058 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4059 &bluetooth_tprfk_ops,
4060 RFKILL_TYPE_BLUETOOTH,
4061 TPACPI_RFK_BLUETOOTH_SW_NAME,
4066 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4067 &bluetooth_attr_group);
4069 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4076 /* procfs -------------------------------------------------------------- */
4077 static int bluetooth_read(struct seq_file *m)
4079 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4082 static int bluetooth_write(char *buf)
4084 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4087 static struct ibm_struct bluetooth_driver_data = {
4088 .name = "bluetooth",
4089 .read = bluetooth_read,
4090 .write = bluetooth_write,
4091 .exit = bluetooth_exit,
4092 .shutdown = bluetooth_shutdown,
4095 /*************************************************************************
4100 /* ACPI GWAN/SWAN bits */
4101 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4102 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4103 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4104 0 = disable, 1 = enable */
4107 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4109 static int wan_get_status(void)
4113 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4115 return (tpacpi_wwan_emulstate) ?
4116 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4119 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4122 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4123 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4126 static int wan_set_status(enum tpacpi_rfkill_state state)
4130 vdbg_printk(TPACPI_DBG_RFKILL,
4131 "will attempt to %s wwan\n",
4132 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4134 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4136 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4141 if (state == TPACPI_RFK_RADIO_ON)
4142 status = TP_ACPI_WANCARD_RADIOSSW
4143 | TP_ACPI_WANCARD_RESUMECTRL;
4147 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4153 /* sysfs wan enable ---------------------------------------------------- */
4154 static ssize_t wan_enable_show(struct device *dev,
4155 struct device_attribute *attr,
4158 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4162 static ssize_t wan_enable_store(struct device *dev,
4163 struct device_attribute *attr,
4164 const char *buf, size_t count)
4166 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4170 static struct device_attribute dev_attr_wan_enable =
4171 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4172 wan_enable_show, wan_enable_store);
4174 /* --------------------------------------------------------------------- */
4176 static struct attribute *wan_attributes[] = {
4177 &dev_attr_wan_enable.attr,
4181 static const struct attribute_group wan_attr_group = {
4182 .attrs = wan_attributes,
4185 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4186 .get_status = wan_get_status,
4187 .set_status = wan_set_status,
4190 static void wan_shutdown(void)
4192 /* Order firmware to save current state to NVRAM */
4193 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4194 TP_ACPI_WGSV_SAVE_STATE))
4195 pr_notice("failed to save WWAN state to NVRAM\n");
4197 vdbg_printk(TPACPI_DBG_RFKILL,
4198 "WWAN state saved to NVRAM\n");
4201 static void wan_exit(void)
4203 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4206 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4211 static int __init wan_init(struct ibm_init_struct *iibm)
4216 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4217 "initializing wan subdriver\n");
4219 TPACPI_ACPIHANDLE_INIT(hkey);
4221 tp_features.wan = hkey_handle &&
4222 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4225 "wan is %s, status 0x%02x\n",
4226 str_supported(tp_features.wan),
4229 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4231 tp_features.wan = 1;
4232 pr_info("wwan switch emulation enabled\n");
4235 if (tp_features.wan &&
4236 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4237 /* no wan hardware present in system */
4238 tp_features.wan = 0;
4239 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4240 "wan hardware not installed\n");
4243 if (!tp_features.wan)
4246 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4249 TPACPI_RFK_WWAN_SW_NAME,
4254 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4258 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4265 /* procfs -------------------------------------------------------------- */
4266 static int wan_read(struct seq_file *m)
4268 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4271 static int wan_write(char *buf)
4273 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4276 static struct ibm_struct wan_driver_data = {
4281 .shutdown = wan_shutdown,
4284 /*************************************************************************
4289 /* ACPI GUWB/SUWB bits */
4290 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4291 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4294 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4296 static int uwb_get_status(void)
4300 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4302 return (tpacpi_uwb_emulstate) ?
4303 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4306 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4309 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4310 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4313 static int uwb_set_status(enum tpacpi_rfkill_state state)
4317 vdbg_printk(TPACPI_DBG_RFKILL,
4318 "will attempt to %s UWB\n",
4319 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4321 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4323 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4328 if (state == TPACPI_RFK_RADIO_ON)
4329 status = TP_ACPI_UWB_RADIOSSW;
4333 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4339 /* --------------------------------------------------------------------- */
4341 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4342 .get_status = uwb_get_status,
4343 .set_status = uwb_set_status,
4346 static void uwb_exit(void)
4348 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4351 static int __init uwb_init(struct ibm_init_struct *iibm)
4356 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4357 "initializing uwb subdriver\n");
4359 TPACPI_ACPIHANDLE_INIT(hkey);
4361 tp_features.uwb = hkey_handle &&
4362 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4364 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4365 "uwb is %s, status 0x%02x\n",
4366 str_supported(tp_features.uwb),
4369 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4371 tp_features.uwb = 1;
4372 pr_info("uwb switch emulation enabled\n");
4375 if (tp_features.uwb &&
4376 !(status & TP_ACPI_UWB_HWPRESENT)) {
4377 /* no uwb hardware present in system */
4378 tp_features.uwb = 0;
4379 dbg_printk(TPACPI_DBG_INIT,
4380 "uwb hardware not installed\n");
4383 if (!tp_features.uwb)
4386 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4389 TPACPI_RFK_UWB_SW_NAME,
4394 static struct ibm_struct uwb_driver_data = {
4397 .flags.experimental = 1,
4400 /*************************************************************************
4404 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4406 enum video_access_mode {
4407 TPACPI_VIDEO_NONE = 0,
4408 TPACPI_VIDEO_570, /* 570 */
4409 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4410 TPACPI_VIDEO_NEW, /* all others */
4413 enum { /* video status flags, based on VIDEO_570 */
4414 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4415 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4416 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4419 enum { /* TPACPI_VIDEO_570 constants */
4420 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4421 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4422 * video_status_flags */
4423 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4424 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4427 static enum video_access_mode video_supported;
4428 static int video_orig_autosw;
4430 static int video_autosw_get(void);
4431 static int video_autosw_set(int enable);
4433 TPACPI_HANDLE(vid, root,
4434 "\\_SB.PCI.AGP.VGA", /* 570 */
4435 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4436 "\\_SB.PCI0.VID0", /* 770e */
4437 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4438 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4439 "\\_SB.PCI0.AGP.VID", /* all others */
4442 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4444 static int __init video_init(struct ibm_init_struct *iibm)
4448 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4450 TPACPI_ACPIHANDLE_INIT(vid);
4451 if (tpacpi_is_ibm())
4452 TPACPI_ACPIHANDLE_INIT(vid2);
4454 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4455 /* G41, assume IVGA doesn't change */
4456 vid_handle = vid2_handle;
4459 /* video switching not supported on R30, R31 */
4460 video_supported = TPACPI_VIDEO_NONE;
4461 else if (tpacpi_is_ibm() &&
4462 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4464 video_supported = TPACPI_VIDEO_570;
4465 else if (tpacpi_is_ibm() &&
4466 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4467 /* 600e/x, 770e, 770x */
4468 video_supported = TPACPI_VIDEO_770;
4471 video_supported = TPACPI_VIDEO_NEW;
4473 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4474 str_supported(video_supported != TPACPI_VIDEO_NONE),
4477 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4480 static void video_exit(void)
4482 dbg_printk(TPACPI_DBG_EXIT,
4483 "restoring original video autoswitch mode\n");
4484 if (video_autosw_set(video_orig_autosw))
4485 pr_err("error while trying to restore original "
4486 "video autoswitch mode\n");
4489 static int video_outputsw_get(void)
4494 switch (video_supported) {
4495 case TPACPI_VIDEO_570:
4496 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4497 TP_ACPI_VIDEO_570_PHSCMD))
4499 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4501 case TPACPI_VIDEO_770:
4502 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4505 status |= TP_ACPI_VIDEO_S_LCD;
4506 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4509 status |= TP_ACPI_VIDEO_S_CRT;
4511 case TPACPI_VIDEO_NEW:
4512 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4513 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4516 status |= TP_ACPI_VIDEO_S_CRT;
4518 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4519 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4522 status |= TP_ACPI_VIDEO_S_LCD;
4523 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4526 status |= TP_ACPI_VIDEO_S_DVI;
4535 static int video_outputsw_set(int status)
4540 switch (video_supported) {
4541 case TPACPI_VIDEO_570:
4542 res = acpi_evalf(NULL, NULL,
4543 "\\_SB.PHS2", "vdd",
4544 TP_ACPI_VIDEO_570_PHS2CMD,
4545 status | TP_ACPI_VIDEO_570_PHS2SET);
4547 case TPACPI_VIDEO_770:
4548 autosw = video_autosw_get();
4552 res = video_autosw_set(1);
4555 res = acpi_evalf(vid_handle, NULL,
4556 "ASWT", "vdd", status * 0x100, 0);
4557 if (!autosw && video_autosw_set(autosw)) {
4558 pr_err("video auto-switch left enabled due to error\n");
4562 case TPACPI_VIDEO_NEW:
4563 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4564 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4570 return (res)? 0 : -EIO;
4573 static int video_autosw_get(void)
4577 switch (video_supported) {
4578 case TPACPI_VIDEO_570:
4579 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4582 case TPACPI_VIDEO_770:
4583 case TPACPI_VIDEO_NEW:
4584 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4594 static int video_autosw_set(int enable)
4596 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4601 static int video_outputsw_cycle(void)
4603 int autosw = video_autosw_get();
4609 switch (video_supported) {
4610 case TPACPI_VIDEO_570:
4611 res = video_autosw_set(1);
4614 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4616 case TPACPI_VIDEO_770:
4617 case TPACPI_VIDEO_NEW:
4618 res = video_autosw_set(1);
4621 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4626 if (!autosw && video_autosw_set(autosw)) {
4627 pr_err("video auto-switch left enabled due to error\n");
4631 return (res)? 0 : -EIO;
4634 static int video_expand_toggle(void)
4636 switch (video_supported) {
4637 case TPACPI_VIDEO_570:
4638 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4640 case TPACPI_VIDEO_770:
4641 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4643 case TPACPI_VIDEO_NEW:
4644 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4652 static int video_read(struct seq_file *m)
4656 if (video_supported == TPACPI_VIDEO_NONE) {
4657 seq_printf(m, "status:\t\tnot supported\n");
4661 /* Even reads can crash X.org, so... */
4662 if (!capable(CAP_SYS_ADMIN))
4665 status = video_outputsw_get();
4669 autosw = video_autosw_get();
4673 seq_printf(m, "status:\t\tsupported\n");
4674 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4675 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4676 if (video_supported == TPACPI_VIDEO_NEW)
4677 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4678 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4679 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4680 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4681 if (video_supported == TPACPI_VIDEO_NEW)
4682 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4683 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4684 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4689 static int video_write(char *buf)
4692 int enable, disable, status;
4695 if (video_supported == TPACPI_VIDEO_NONE)
4698 /* Even reads can crash X.org, let alone writes... */
4699 if (!capable(CAP_SYS_ADMIN))
4705 while ((cmd = next_cmd(&buf))) {
4706 if (strlencmp(cmd, "lcd_enable") == 0) {
4707 enable |= TP_ACPI_VIDEO_S_LCD;
4708 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4709 disable |= TP_ACPI_VIDEO_S_LCD;
4710 } else if (strlencmp(cmd, "crt_enable") == 0) {
4711 enable |= TP_ACPI_VIDEO_S_CRT;
4712 } else if (strlencmp(cmd, "crt_disable") == 0) {
4713 disable |= TP_ACPI_VIDEO_S_CRT;
4714 } else if (video_supported == TPACPI_VIDEO_NEW &&
4715 strlencmp(cmd, "dvi_enable") == 0) {
4716 enable |= TP_ACPI_VIDEO_S_DVI;
4717 } else if (video_supported == TPACPI_VIDEO_NEW &&
4718 strlencmp(cmd, "dvi_disable") == 0) {
4719 disable |= TP_ACPI_VIDEO_S_DVI;
4720 } else if (strlencmp(cmd, "auto_enable") == 0) {
4721 res = video_autosw_set(1);
4724 } else if (strlencmp(cmd, "auto_disable") == 0) {
4725 res = video_autosw_set(0);
4728 } else if (strlencmp(cmd, "video_switch") == 0) {
4729 res = video_outputsw_cycle();
4732 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4733 res = video_expand_toggle();
4740 if (enable || disable) {
4741 status = video_outputsw_get();
4744 res = video_outputsw_set((status & ~disable) | enable);
4752 static struct ibm_struct video_driver_data = {
4755 .write = video_write,
4759 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4761 /*************************************************************************
4762 * Light (thinklight) subdriver
4765 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4766 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4768 static int light_get_status(void)
4772 if (tp_features.light_status) {
4773 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4781 static int light_set_status(int status)
4785 if (tp_features.light) {
4787 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4789 TP_CMOS_THINKLIGHT_ON :
4790 TP_CMOS_THINKLIGHT_OFF);
4792 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4795 return (rc)? 0 : -EIO;
4801 static void light_set_status_worker(struct work_struct *work)
4803 struct tpacpi_led_classdev *data =
4804 container_of(work, struct tpacpi_led_classdev, work);
4806 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4807 light_set_status((data->new_state != TPACPI_LED_OFF));
4810 static void light_sysfs_set(struct led_classdev *led_cdev,
4811 enum led_brightness brightness)
4813 struct tpacpi_led_classdev *data =
4814 container_of(led_cdev,
4815 struct tpacpi_led_classdev,
4817 data->new_state = (brightness != LED_OFF) ?
4818 TPACPI_LED_ON : TPACPI_LED_OFF;
4819 queue_work(tpacpi_wq, &data->work);
4822 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4824 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4827 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4829 .name = "tpacpi::thinklight",
4830 .brightness_set = &light_sysfs_set,
4831 .brightness_get = &light_sysfs_get,
4835 static int __init light_init(struct ibm_init_struct *iibm)
4839 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4841 if (tpacpi_is_ibm()) {
4842 TPACPI_ACPIHANDLE_INIT(ledb);
4843 TPACPI_ACPIHANDLE_INIT(lght);
4845 TPACPI_ACPIHANDLE_INIT(cmos);
4846 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4848 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4849 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4851 if (tp_features.light)
4852 /* light status not supported on
4853 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4854 tp_features.light_status =
4855 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4857 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4858 str_supported(tp_features.light),
4859 str_supported(tp_features.light_status));
4861 if (!tp_features.light)
4864 rc = led_classdev_register(&tpacpi_pdev->dev,
4865 &tpacpi_led_thinklight.led_classdev);
4868 tp_features.light = 0;
4869 tp_features.light_status = 0;
4877 static void light_exit(void)
4879 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4880 if (work_pending(&tpacpi_led_thinklight.work))
4881 flush_workqueue(tpacpi_wq);
4884 static int light_read(struct seq_file *m)
4888 if (!tp_features.light) {
4889 seq_printf(m, "status:\t\tnot supported\n");
4890 } else if (!tp_features.light_status) {
4891 seq_printf(m, "status:\t\tunknown\n");
4892 seq_printf(m, "commands:\ton, off\n");
4894 status = light_get_status();
4897 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4898 seq_printf(m, "commands:\ton, off\n");
4904 static int light_write(char *buf)
4909 if (!tp_features.light)
4912 while ((cmd = next_cmd(&buf))) {
4913 if (strlencmp(cmd, "on") == 0) {
4915 } else if (strlencmp(cmd, "off") == 0) {
4921 return light_set_status(newstatus);
4924 static struct ibm_struct light_driver_data = {
4927 .write = light_write,
4931 /*************************************************************************
4935 /* sysfs cmos_command -------------------------------------------------- */
4936 static ssize_t cmos_command_store(struct device *dev,
4937 struct device_attribute *attr,
4938 const char *buf, size_t count)
4940 unsigned long cmos_cmd;
4943 if (parse_strtoul(buf, 21, &cmos_cmd))
4946 res = issue_thinkpad_cmos_command(cmos_cmd);
4947 return (res)? res : count;
4950 static struct device_attribute dev_attr_cmos_command =
4951 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4953 /* --------------------------------------------------------------------- */
4955 static int __init cmos_init(struct ibm_init_struct *iibm)
4959 vdbg_printk(TPACPI_DBG_INIT,
4960 "initializing cmos commands subdriver\n");
4962 TPACPI_ACPIHANDLE_INIT(cmos);
4964 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4965 str_supported(cmos_handle != NULL));
4967 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4971 return (cmos_handle)? 0 : 1;
4974 static void cmos_exit(void)
4976 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4979 static int cmos_read(struct seq_file *m)
4981 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4982 R30, R31, T20-22, X20-21 */
4984 seq_printf(m, "status:\t\tnot supported\n");
4986 seq_printf(m, "status:\t\tsupported\n");
4987 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4993 static int cmos_write(char *buf)
4998 while ((cmd = next_cmd(&buf))) {
4999 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5000 cmos_cmd >= 0 && cmos_cmd <= 21) {
5005 res = issue_thinkpad_cmos_command(cmos_cmd);
5013 static struct ibm_struct cmos_driver_data = {
5016 .write = cmos_write,
5020 /*************************************************************************
5024 enum led_access_mode {
5025 TPACPI_LED_NONE = 0,
5026 TPACPI_LED_570, /* 570 */
5027 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5028 TPACPI_LED_NEW, /* all others */
5031 enum { /* For TPACPI_LED_OLD */
5032 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5033 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5034 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5037 static enum led_access_mode led_supported;
5039 static acpi_handle led_handle;
5041 #define TPACPI_LED_NUMLEDS 16
5042 static struct tpacpi_led_classdev *tpacpi_leds;
5043 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5044 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5045 /* there's a limit of 19 chars + NULL before 2.6.26 */
5047 "tpacpi:orange:batt",
5048 "tpacpi:green:batt",
5049 "tpacpi::dock_active",
5050 "tpacpi::bay_active",
5051 "tpacpi::dock_batt",
5052 "tpacpi::unknown_led",
5054 "tpacpi::dock_status1",
5055 "tpacpi::dock_status2",
5056 "tpacpi::unknown_led2",
5057 "tpacpi::unknown_led3",
5058 "tpacpi::thinkvantage",
5060 #define TPACPI_SAFE_LEDS 0x1081U
5062 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5064 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5067 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5071 static int led_get_status(const unsigned int led)
5074 enum led_status_t led_s;
5076 switch (led_supported) {
5077 case TPACPI_LED_570:
5078 if (!acpi_evalf(ec_handle,
5079 &status, "GLED", "dd", 1 << led))
5081 led_s = (status == 0)?
5086 tpacpi_led_state_cache[led] = led_s;
5095 static int led_set_status(const unsigned int led,
5096 const enum led_status_t ledstatus)
5098 /* off, on, blink. Index is led_status_t */
5099 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5100 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5104 switch (led_supported) {
5105 case TPACPI_LED_570:
5107 if (unlikely(led > 7))
5109 if (unlikely(tpacpi_is_led_restricted(led)))
5111 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5112 (1 << led), led_sled_arg1[ledstatus]))
5115 case TPACPI_LED_OLD:
5116 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5117 if (unlikely(led > 7))
5119 if (unlikely(tpacpi_is_led_restricted(led)))
5121 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5123 rc = ec_write(TPACPI_LED_EC_HLBL,
5124 (ledstatus == TPACPI_LED_BLINK) << led);
5126 rc = ec_write(TPACPI_LED_EC_HLCL,
5127 (ledstatus != TPACPI_LED_OFF) << led);
5129 case TPACPI_LED_NEW:
5131 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5133 if (unlikely(tpacpi_is_led_restricted(led)))
5135 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5136 led, led_led_arg1[ledstatus]))
5144 tpacpi_led_state_cache[led] = ledstatus;
5149 static void led_set_status_worker(struct work_struct *work)
5151 struct tpacpi_led_classdev *data =
5152 container_of(work, struct tpacpi_led_classdev, work);
5154 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5155 led_set_status(data->led, data->new_state);
5158 static void led_sysfs_set(struct led_classdev *led_cdev,
5159 enum led_brightness brightness)
5161 struct tpacpi_led_classdev *data = container_of(led_cdev,
5162 struct tpacpi_led_classdev, led_classdev);
5164 if (brightness == LED_OFF)
5165 data->new_state = TPACPI_LED_OFF;
5166 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5167 data->new_state = TPACPI_LED_ON;
5169 data->new_state = TPACPI_LED_BLINK;
5171 queue_work(tpacpi_wq, &data->work);
5174 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5175 unsigned long *delay_on, unsigned long *delay_off)
5177 struct tpacpi_led_classdev *data = container_of(led_cdev,
5178 struct tpacpi_led_classdev, led_classdev);
5180 /* Can we choose the flash rate? */
5181 if (*delay_on == 0 && *delay_off == 0) {
5182 /* yes. set them to the hardware blink rate (1 Hz) */
5183 *delay_on = 500; /* ms */
5184 *delay_off = 500; /* ms */
5185 } else if ((*delay_on != 500) || (*delay_off != 500))
5188 data->new_state = TPACPI_LED_BLINK;
5189 queue_work(tpacpi_wq, &data->work);
5194 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5198 struct tpacpi_led_classdev *data = container_of(led_cdev,
5199 struct tpacpi_led_classdev, led_classdev);
5201 rc = led_get_status(data->led);
5203 if (rc == TPACPI_LED_OFF || rc < 0)
5204 rc = LED_OFF; /* no error handling in led class :( */
5211 static void led_exit(void)
5215 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5216 if (tpacpi_leds[i].led_classdev.name)
5217 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5223 static int __init tpacpi_init_led(unsigned int led)
5227 tpacpi_leds[led].led = led;
5229 /* LEDs with no name don't get registered */
5230 if (!tpacpi_led_names[led])
5233 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5234 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5235 if (led_supported == TPACPI_LED_570)
5236 tpacpi_leds[led].led_classdev.brightness_get =
5239 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5241 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5243 rc = led_classdev_register(&tpacpi_pdev->dev,
5244 &tpacpi_leds[led].led_classdev);
5246 tpacpi_leds[led].led_classdev.name = NULL;
5251 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5252 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5253 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5254 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5256 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5257 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5258 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5259 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5260 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5261 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5262 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5263 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5265 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5266 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5267 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5268 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5269 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5271 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5272 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5273 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5274 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5276 /* (1) - may have excess leds enabled on MSB */
5278 /* Defaults (order matters, keep last, don't reorder!) */
5280 .vendor = PCI_VENDOR_ID_LENOVO,
5281 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5284 { /* IBM ThinkPads with no EC version string */
5285 .vendor = PCI_VENDOR_ID_IBM,
5286 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5289 { /* IBM ThinkPads with EC version string */
5290 .vendor = PCI_VENDOR_ID_IBM,
5291 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5296 #undef TPACPI_LEDQ_IBM
5297 #undef TPACPI_LEDQ_LNV
5299 static enum led_access_mode __init led_init_detect_mode(void)
5303 if (tpacpi_is_ibm()) {
5305 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5306 if (ACPI_SUCCESS(status))
5307 return TPACPI_LED_570;
5309 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5310 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5311 if (ACPI_SUCCESS(status))
5312 return TPACPI_LED_OLD;
5316 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5317 if (ACPI_SUCCESS(status))
5318 return TPACPI_LED_NEW;
5320 /* R30, R31, and unknown firmwares */
5322 return TPACPI_LED_NONE;
5325 static int __init led_init(struct ibm_init_struct *iibm)
5329 unsigned long useful_leds;
5331 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5333 led_supported = led_init_detect_mode();
5335 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5336 str_supported(led_supported), led_supported);
5338 if (led_supported == TPACPI_LED_NONE)
5341 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5344 pr_err("Out of memory for LED data\n");
5348 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5349 ARRAY_SIZE(led_useful_qtable));
5351 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5352 if (!tpacpi_is_led_restricted(i) &&
5353 test_bit(i, &useful_leds)) {
5354 rc = tpacpi_init_led(i);
5362 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5363 pr_notice("warning: userspace override of important "
5364 "firmware LEDs is enabled\n");
5369 #define str_led_status(s) \
5370 ((s) == TPACPI_LED_OFF ? "off" : \
5371 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5373 static int led_read(struct seq_file *m)
5375 if (!led_supported) {
5376 seq_printf(m, "status:\t\tnot supported\n");
5379 seq_printf(m, "status:\t\tsupported\n");
5381 if (led_supported == TPACPI_LED_570) {
5384 for (i = 0; i < 8; i++) {
5385 status = led_get_status(i);
5388 seq_printf(m, "%d:\t\t%s\n",
5389 i, str_led_status(status));
5393 seq_printf(m, "commands:\t"
5394 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5399 static int led_write(char *buf)
5403 enum led_status_t s;
5408 while ((cmd = next_cmd(&buf))) {
5409 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5412 if (strstr(cmd, "off")) {
5414 } else if (strstr(cmd, "on")) {
5416 } else if (strstr(cmd, "blink")) {
5417 s = TPACPI_LED_BLINK;
5422 rc = led_set_status(led, s);
5430 static struct ibm_struct led_driver_data = {
5437 /*************************************************************************
5441 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5443 #define TPACPI_BEEP_Q1 0x0001
5445 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5446 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5447 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5450 static int __init beep_init(struct ibm_init_struct *iibm)
5452 unsigned long quirks;
5454 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5456 TPACPI_ACPIHANDLE_INIT(beep);
5458 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5459 str_supported(beep_handle != NULL));
5461 quirks = tpacpi_check_quirks(beep_quirk_table,
5462 ARRAY_SIZE(beep_quirk_table));
5464 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5466 return (beep_handle)? 0 : 1;
5469 static int beep_read(struct seq_file *m)
5472 seq_printf(m, "status:\t\tnot supported\n");
5474 seq_printf(m, "status:\t\tsupported\n");
5475 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5481 static int beep_write(char *buf)
5489 while ((cmd = next_cmd(&buf))) {
5490 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5491 beep_cmd >= 0 && beep_cmd <= 17) {
5495 if (tp_features.beep_needs_two_args) {
5496 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5500 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5509 static struct ibm_struct beep_driver_data = {
5512 .write = beep_write,
5515 /*************************************************************************
5519 enum thermal_access_mode {
5520 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5521 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5522 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5523 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5524 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5527 enum { /* TPACPI_THERMAL_TPEC_* */
5528 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5529 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5530 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5532 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5536 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5537 struct ibm_thermal_sensors_struct {
5538 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5541 static enum thermal_access_mode thermal_read_mode;
5543 /* idx is zero-based */
5544 static int thermal_get_sensor(int idx, s32 *value)
5550 t = TP_EC_THERMAL_TMP0;
5552 switch (thermal_read_mode) {
5553 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5554 case TPACPI_THERMAL_TPEC_16:
5555 if (idx >= 8 && idx <= 15) {
5556 t = TP_EC_THERMAL_TMP8;
5561 case TPACPI_THERMAL_TPEC_8:
5563 if (!acpi_ec_read(t + idx, &tmp))
5565 *value = tmp * 1000;
5570 case TPACPI_THERMAL_ACPI_UPDT:
5572 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5573 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5575 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5577 *value = (t - 2732) * 100;
5582 case TPACPI_THERMAL_ACPI_TMP07:
5584 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5585 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5587 if (t > 127 || t < -127)
5588 t = TP_EC_THERMAL_TMP_NA;
5594 case TPACPI_THERMAL_NONE:
5602 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5613 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5616 for (i = 0 ; i < n; i++) {
5617 res = thermal_get_sensor(i, &s->temp[i]);
5625 static void thermal_dump_all_sensors(void)
5628 struct ibm_thermal_sensors_struct t;
5630 n = thermal_get_sensors(&t);
5634 pr_notice("temperatures (Celsius):");
5636 for (i = 0; i < n; i++) {
5637 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5638 pr_cont(" %d", (int)(t.temp[i] / 1000));
5646 /* sysfs temp##_input -------------------------------------------------- */
5648 static ssize_t thermal_temp_input_show(struct device *dev,
5649 struct device_attribute *attr,
5652 struct sensor_device_attribute *sensor_attr =
5653 to_sensor_dev_attr(attr);
5654 int idx = sensor_attr->index;
5658 res = thermal_get_sensor(idx, &value);
5661 if (value == TPACPI_THERMAL_SENSOR_NA)
5664 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5667 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5668 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5669 thermal_temp_input_show, NULL, _idxB)
5671 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5672 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5673 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5674 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5675 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5676 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5677 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5678 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5679 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5680 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5681 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5682 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5683 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5684 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5685 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5686 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5687 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5690 #define THERMAL_ATTRS(X) \
5691 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5693 static struct attribute *thermal_temp_input_attr[] = {
5713 static const struct attribute_group thermal_temp_input16_group = {
5714 .attrs = thermal_temp_input_attr
5717 static const struct attribute_group thermal_temp_input8_group = {
5718 .attrs = &thermal_temp_input_attr[8]
5721 #undef THERMAL_SENSOR_ATTR_TEMP
5722 #undef THERMAL_ATTRS
5724 /* --------------------------------------------------------------------- */
5726 static int __init thermal_init(struct ibm_init_struct *iibm)
5733 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5735 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5737 if (thinkpad_id.ec_model) {
5739 * Direct EC access mode: sensors at registers
5740 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5741 * non-implemented, thermal sensors return 0x80 when
5746 for (i = 0; i < 8; i++) {
5747 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5753 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5761 /* This is sheer paranoia, but we handle it anyway */
5763 pr_err("ThinkPad ACPI EC access misbehaving, "
5764 "falling back to ACPI TMPx access "
5766 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5768 pr_err("ThinkPad ACPI EC access misbehaving, "
5769 "disabling thermal sensors access\n");
5770 thermal_read_mode = TPACPI_THERMAL_NONE;
5775 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5777 } else if (acpi_tmp7) {
5778 if (tpacpi_is_ibm() &&
5779 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5780 /* 600e/x, 770e, 770x */
5781 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5783 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5784 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5787 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5788 thermal_read_mode = TPACPI_THERMAL_NONE;
5791 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5792 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5795 switch (thermal_read_mode) {
5796 case TPACPI_THERMAL_TPEC_16:
5797 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5798 &thermal_temp_input16_group);
5802 case TPACPI_THERMAL_TPEC_8:
5803 case TPACPI_THERMAL_ACPI_TMP07:
5804 case TPACPI_THERMAL_ACPI_UPDT:
5805 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5806 &thermal_temp_input8_group);
5810 case TPACPI_THERMAL_NONE:
5818 static void thermal_exit(void)
5820 switch (thermal_read_mode) {
5821 case TPACPI_THERMAL_TPEC_16:
5822 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5823 &thermal_temp_input16_group);
5825 case TPACPI_THERMAL_TPEC_8:
5826 case TPACPI_THERMAL_ACPI_TMP07:
5827 case TPACPI_THERMAL_ACPI_UPDT:
5828 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5829 &thermal_temp_input8_group);
5831 case TPACPI_THERMAL_NONE:
5837 static int thermal_read(struct seq_file *m)
5840 struct ibm_thermal_sensors_struct t;
5842 n = thermal_get_sensors(&t);
5843 if (unlikely(n < 0))
5846 seq_printf(m, "temperatures:\t");
5849 for (i = 0; i < (n - 1); i++)
5850 seq_printf(m, "%d ", t.temp[i] / 1000);
5851 seq_printf(m, "%d\n", t.temp[i] / 1000);
5853 seq_printf(m, "not supported\n");
5858 static struct ibm_struct thermal_driver_data = {
5860 .read = thermal_read,
5861 .exit = thermal_exit,
5864 /*************************************************************************
5865 * Backlight/brightness subdriver
5868 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5871 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5872 * CMOS NVRAM byte 0x5E, bits 0-3.
5874 * EC HBRV (0x31) has the following layout
5875 * Bit 7: unknown function
5876 * Bit 6: unknown function
5877 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5878 * Bit 4: must be set to zero to avoid problems
5879 * Bit 3-0: backlight brightness level
5881 * brightness_get_raw returns status data in the HBRV layout
5883 * WARNING: The X61 has been verified to use HBRV for something else, so
5884 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5885 * testing on the very early *60 Lenovo models...
5889 TP_EC_BACKLIGHT = 0x31,
5891 /* TP_EC_BACKLIGHT bitmasks */
5892 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5893 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5894 TP_EC_BACKLIGHT_MAPSW = 0x20,
5897 enum tpacpi_brightness_access_mode {
5898 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5899 TPACPI_BRGHT_MODE_EC, /* EC control */
5900 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5901 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5902 TPACPI_BRGHT_MODE_MAX
5905 static struct backlight_device *ibm_backlight_device;
5907 static enum tpacpi_brightness_access_mode brightness_mode =
5908 TPACPI_BRGHT_MODE_MAX;
5910 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5912 static struct mutex brightness_mutex;
5914 /* NVRAM brightness access,
5915 * call with brightness_mutex held! */
5916 static unsigned int tpacpi_brightness_nvram_get(void)
5920 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5921 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5922 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5923 lnvram &= bright_maxlvl;
5928 static void tpacpi_brightness_checkpoint_nvram(void)
5933 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5936 vdbg_printk(TPACPI_DBG_BRGHT,
5937 "trying to checkpoint backlight level to NVRAM...\n");
5939 if (mutex_lock_killable(&brightness_mutex) < 0)
5942 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5944 lec &= TP_EC_BACKLIGHT_LVLMSK;
5945 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5947 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5948 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5949 /* NVRAM needs update */
5950 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5951 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5953 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5954 dbg_printk(TPACPI_DBG_BRGHT,
5955 "updated NVRAM backlight level to %u (0x%02x)\n",
5956 (unsigned int) lec, (unsigned int) b_nvram);
5958 vdbg_printk(TPACPI_DBG_BRGHT,
5959 "NVRAM backlight level already is %u (0x%02x)\n",
5960 (unsigned int) lec, (unsigned int) b_nvram);
5963 mutex_unlock(&brightness_mutex);
5967 /* call with brightness_mutex held! */
5968 static int tpacpi_brightness_get_raw(int *status)
5972 switch (brightness_mode) {
5973 case TPACPI_BRGHT_MODE_UCMS_STEP:
5974 *status = tpacpi_brightness_nvram_get();
5976 case TPACPI_BRGHT_MODE_EC:
5977 case TPACPI_BRGHT_MODE_ECNVRAM:
5978 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5987 /* call with brightness_mutex held! */
5988 /* do NOT call with illegal backlight level value */
5989 static int tpacpi_brightness_set_ec(unsigned int value)
5993 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5996 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5997 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5998 (value & TP_EC_BACKLIGHT_LVLMSK))))
6004 /* call with brightness_mutex held! */
6005 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6008 unsigned int current_value, i;
6010 current_value = tpacpi_brightness_nvram_get();
6012 if (value == current_value)
6015 cmos_cmd = (value > current_value) ?
6016 TP_CMOS_BRIGHTNESS_UP :
6017 TP_CMOS_BRIGHTNESS_DOWN;
6018 inc = (value > current_value) ? 1 : -1;
6020 for (i = current_value; i != value; i += inc)
6021 if (issue_thinkpad_cmos_command(cmos_cmd))
6027 /* May return EINTR which can always be mapped to ERESTARTSYS */
6028 static int brightness_set(unsigned int value)
6032 if (value > bright_maxlvl || value < 0)
6035 vdbg_printk(TPACPI_DBG_BRGHT,
6036 "set backlight level to %d\n", value);
6038 res = mutex_lock_killable(&brightness_mutex);
6042 switch (brightness_mode) {
6043 case TPACPI_BRGHT_MODE_EC:
6044 case TPACPI_BRGHT_MODE_ECNVRAM:
6045 res = tpacpi_brightness_set_ec(value);
6047 case TPACPI_BRGHT_MODE_UCMS_STEP:
6048 res = tpacpi_brightness_set_ucmsstep(value);
6054 mutex_unlock(&brightness_mutex);
6058 /* sysfs backlight class ----------------------------------------------- */
6060 static int brightness_update_status(struct backlight_device *bd)
6062 unsigned int level =
6063 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6064 bd->props.power == FB_BLANK_UNBLANK) ?
6065 bd->props.brightness : 0;
6067 dbg_printk(TPACPI_DBG_BRGHT,
6068 "backlight: attempt to set level to %d\n",
6071 /* it is the backlight class's job (caller) to handle
6072 * EINTR and other errors properly */
6073 return brightness_set(level);
6076 static int brightness_get(struct backlight_device *bd)
6080 res = mutex_lock_killable(&brightness_mutex);
6084 res = tpacpi_brightness_get_raw(&status);
6086 mutex_unlock(&brightness_mutex);
6091 return status & TP_EC_BACKLIGHT_LVLMSK;
6094 static void tpacpi_brightness_notify_change(void)
6096 backlight_force_update(ibm_backlight_device,
6097 BACKLIGHT_UPDATE_HOTKEY);
6100 static const struct backlight_ops ibm_backlight_data = {
6101 .get_brightness = brightness_get,
6102 .update_status = brightness_update_status,
6105 /* --------------------------------------------------------------------- */
6108 * Call _BCL method of video device. On some ThinkPads this will
6109 * switch the firmware to the ACPI brightness control mode.
6112 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6114 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6115 union acpi_object *obj;
6118 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6119 obj = (union acpi_object *)buffer.pointer;
6120 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6121 pr_err("Unknown _BCL data, please report this to %s\n",
6125 rc = obj->package.count;
6131 kfree(buffer.pointer);
6137 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6139 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6141 acpi_handle video_device;
6144 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6146 bcl_levels = tpacpi_query_bcl_levels(video_device);
6148 tp_features.bright_acpimode = (bcl_levels > 0);
6150 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6154 * These are only useful for models that have only one possibility
6155 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6158 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6159 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6160 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6162 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6163 /* Models with ATI GPUs known to require ECNVRAM mode */
6164 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6166 /* Models with ATI GPUs that can use ECNVRAM */
6167 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6168 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6169 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6170 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6172 /* Models with Intel Extreme Graphics 2 */
6173 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6174 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6175 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6177 /* Models with Intel GMA900 */
6178 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6179 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6180 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6184 * Returns < 0 for error, otherwise sets tp_features.bright_*
6185 * and bright_maxlvl.
6187 static void __init tpacpi_detect_brightness_capabilities(void)
6191 vdbg_printk(TPACPI_DBG_INIT,
6192 "detecting firmware brightness interface capabilities\n");
6194 /* we could run a quirks check here (same table used by
6195 * brightness_init) if needed */
6198 * We always attempt to detect acpi support, so as to switch
6199 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6200 * going to publish a backlight interface
6202 b = tpacpi_check_std_acpi_brightness_support();
6206 pr_info("detected a 16-level brightness capable ThinkPad\n");
6211 pr_info("detected a 8-level brightness capable ThinkPad\n");
6214 pr_err("Unsupported brightness interface, "
6215 "please contact %s\n", TPACPI_MAIL);
6216 tp_features.bright_unkfw = 1;
6217 bright_maxlvl = b - 1;
6221 static int __init brightness_init(struct ibm_init_struct *iibm)
6223 struct backlight_properties props;
6225 unsigned long quirks;
6227 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6229 mutex_init(&brightness_mutex);
6231 quirks = tpacpi_check_quirks(brightness_quirk_table,
6232 ARRAY_SIZE(brightness_quirk_table));
6234 /* tpacpi_detect_brightness_capabilities() must have run already */
6236 /* if it is unknown, we don't handle it: it wouldn't be safe */
6237 if (tp_features.bright_unkfw)
6240 if (!brightness_enable) {
6241 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6242 "brightness support disabled by "
6243 "module parameter\n");
6247 if (acpi_video_backlight_support()) {
6248 if (brightness_enable > 1) {
6249 pr_info("Standard ACPI backlight interface "
6250 "available, not loading native one\n");
6252 } else if (brightness_enable == 1) {
6253 pr_warn("Cannot enable backlight brightness support, "
6254 "ACPI is already handling it. Refer to the "
6255 "acpi_backlight kernel parameter.\n");
6258 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6259 pr_notice("Standard ACPI backlight interface not "
6260 "available, thinkpad_acpi native "
6261 "brightness control enabled\n");
6265 * Check for module parameter bogosity, note that we
6266 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6267 * able to detect "unspecified"
6269 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6272 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6273 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6274 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6275 if (quirks & TPACPI_BRGHT_Q_EC)
6276 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6278 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6280 dbg_printk(TPACPI_DBG_BRGHT,
6281 "driver auto-selected brightness_mode=%d\n",
6286 if (!tpacpi_is_ibm() &&
6287 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6288 brightness_mode == TPACPI_BRGHT_MODE_EC))
6291 if (tpacpi_brightness_get_raw(&b) < 0)
6294 memset(&props, 0, sizeof(struct backlight_properties));
6295 props.type = BACKLIGHT_PLATFORM;
6296 props.max_brightness = bright_maxlvl;
6297 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6298 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6300 &ibm_backlight_data,
6302 if (IS_ERR(ibm_backlight_device)) {
6303 int rc = PTR_ERR(ibm_backlight_device);
6304 ibm_backlight_device = NULL;
6305 pr_err("Could not register backlight device\n");
6308 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6309 "brightness is supported\n");
6311 if (quirks & TPACPI_BRGHT_Q_ASK) {
6312 pr_notice("brightness: will use unverified default: "
6313 "brightness_mode=%d\n", brightness_mode);
6314 pr_notice("brightness: please report to %s whether it works well "
6315 "or not on your ThinkPad\n", TPACPI_MAIL);
6318 /* Added by mistake in early 2007. Probably useless, but it could
6319 * be working around some unknown firmware problem where the value
6320 * read at startup doesn't match the real hardware state... so leave
6321 * it in place just in case */
6322 backlight_update_status(ibm_backlight_device);
6324 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6325 "brightness: registering brightness hotkeys "
6326 "as change notification\n");
6327 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6328 | TP_ACPI_HKEY_BRGHTUP_MASK
6329 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6333 static void brightness_suspend(void)
6335 tpacpi_brightness_checkpoint_nvram();
6338 static void brightness_shutdown(void)
6340 tpacpi_brightness_checkpoint_nvram();
6343 static void brightness_exit(void)
6345 if (ibm_backlight_device) {
6346 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6347 "calling backlight_device_unregister()\n");
6348 backlight_device_unregister(ibm_backlight_device);
6351 tpacpi_brightness_checkpoint_nvram();
6354 static int brightness_read(struct seq_file *m)
6358 level = brightness_get(NULL);
6360 seq_printf(m, "level:\t\tunreadable\n");
6362 seq_printf(m, "level:\t\t%d\n", level);
6363 seq_printf(m, "commands:\tup, down\n");
6364 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6371 static int brightness_write(char *buf)
6377 level = brightness_get(NULL);
6381 while ((cmd = next_cmd(&buf))) {
6382 if (strlencmp(cmd, "up") == 0) {
6383 if (level < bright_maxlvl)
6385 } else if (strlencmp(cmd, "down") == 0) {
6388 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6389 level >= 0 && level <= bright_maxlvl) {
6395 tpacpi_disclose_usertask("procfs brightness",
6396 "set level to %d\n", level);
6399 * Now we know what the final level should be, so we try to set it.
6400 * Doing it this way makes the syscall restartable in case of EINTR
6402 rc = brightness_set(level);
6403 if (!rc && ibm_backlight_device)
6404 backlight_force_update(ibm_backlight_device,
6405 BACKLIGHT_UPDATE_SYSFS);
6406 return (rc == -EINTR)? -ERESTARTSYS : rc;
6409 static struct ibm_struct brightness_driver_data = {
6410 .name = "brightness",
6411 .read = brightness_read,
6412 .write = brightness_write,
6413 .exit = brightness_exit,
6414 .suspend = brightness_suspend,
6415 .shutdown = brightness_shutdown,
6418 /*************************************************************************
6423 * IBM ThinkPads have a simple volume controller with MUTE gating.
6424 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6426 * Since the *61 series (and probably also the later *60 series), Lenovo
6427 * ThinkPads only implement the MUTE gate.
6430 * Bit 6: MUTE (1 mutes sound)
6432 * Other bits should be zero as far as we know.
6434 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6435 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6436 * such as bit 7 which is used to detect repeated presses of MUTE,
6437 * and we leave them unchanged.
6440 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6442 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6443 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6444 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6446 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6447 static char *alsa_id = "ThinkPadEC";
6448 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
6450 struct tpacpi_alsa_data {
6451 struct snd_card *card;
6452 struct snd_ctl_elem_id *ctl_mute_id;
6453 struct snd_ctl_elem_id *ctl_vol_id;
6456 static struct snd_card *alsa_card;
6461 /* TP_EC_AUDIO bits */
6462 TP_EC_AUDIO_MUTESW = 6,
6464 /* TP_EC_AUDIO bitmasks */
6465 TP_EC_AUDIO_LVL_MSK = 0x0F,
6466 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6468 /* Maximum volume */
6469 TP_EC_VOLUME_MAX = 14,
6472 enum tpacpi_volume_access_mode {
6473 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6474 TPACPI_VOL_MODE_EC, /* Pure EC control */
6475 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6476 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6480 enum tpacpi_volume_capabilities {
6481 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6482 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6483 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6487 static enum tpacpi_volume_access_mode volume_mode =
6488 TPACPI_VOL_MODE_MAX;
6490 static enum tpacpi_volume_capabilities volume_capabilities;
6491 static bool volume_control_allowed;
6494 * Used to syncronize writers to TP_EC_AUDIO and
6495 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6497 static struct mutex volume_mutex;
6499 static void tpacpi_volume_checkpoint_nvram(void)
6505 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6507 if (!volume_control_allowed)
6510 vdbg_printk(TPACPI_DBG_MIXER,
6511 "trying to checkpoint mixer state to NVRAM...\n");
6513 if (tp_features.mixer_no_level_control)
6514 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6516 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6518 if (mutex_lock_killable(&volume_mutex) < 0)
6521 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6524 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6526 if (lec != (b_nvram & ec_mask)) {
6527 /* NVRAM needs update */
6528 b_nvram &= ~ec_mask;
6530 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6531 dbg_printk(TPACPI_DBG_MIXER,
6532 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6533 (unsigned int) lec, (unsigned int) b_nvram);
6535 vdbg_printk(TPACPI_DBG_MIXER,
6536 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6537 (unsigned int) lec, (unsigned int) b_nvram);
6541 mutex_unlock(&volume_mutex);
6544 static int volume_get_status_ec(u8 *status)
6548 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6553 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6558 static int volume_get_status(u8 *status)
6560 return volume_get_status_ec(status);
6563 static int volume_set_status_ec(const u8 status)
6565 if (!acpi_ec_write(TP_EC_AUDIO, status))
6568 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6573 static int volume_set_status(const u8 status)
6575 return volume_set_status_ec(status);
6578 /* returns < 0 on error, 0 on no change, 1 on change */
6579 static int __volume_set_mute_ec(const bool mute)
6584 if (mutex_lock_killable(&volume_mutex) < 0)
6587 rc = volume_get_status_ec(&s);
6591 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6592 s & ~TP_EC_AUDIO_MUTESW_MSK;
6595 rc = volume_set_status_ec(n);
6601 mutex_unlock(&volume_mutex);
6605 static int volume_alsa_set_mute(const bool mute)
6607 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6608 (mute) ? "" : "un");
6609 return __volume_set_mute_ec(mute);
6612 static int volume_set_mute(const bool mute)
6616 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6617 (mute) ? "" : "un");
6619 rc = __volume_set_mute_ec(mute);
6620 return (rc < 0) ? rc : 0;
6623 /* returns < 0 on error, 0 on no change, 1 on change */
6624 static int __volume_set_volume_ec(const u8 vol)
6629 if (vol > TP_EC_VOLUME_MAX)
6632 if (mutex_lock_killable(&volume_mutex) < 0)
6635 rc = volume_get_status_ec(&s);
6639 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6642 rc = volume_set_status_ec(n);
6648 mutex_unlock(&volume_mutex);
6652 static int volume_alsa_set_volume(const u8 vol)
6654 dbg_printk(TPACPI_DBG_MIXER,
6655 "ALSA: trying to set volume level to %hu\n", vol);
6656 return __volume_set_volume_ec(vol);
6659 static void volume_alsa_notify_change(void)
6661 struct tpacpi_alsa_data *d;
6663 if (alsa_card && alsa_card->private_data) {
6664 d = alsa_card->private_data;
6666 snd_ctl_notify(alsa_card,
6667 SNDRV_CTL_EVENT_MASK_VALUE,
6670 snd_ctl_notify(alsa_card,
6671 SNDRV_CTL_EVENT_MASK_VALUE,
6676 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6677 struct snd_ctl_elem_info *uinfo)
6679 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6681 uinfo->value.integer.min = 0;
6682 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6686 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6687 struct snd_ctl_elem_value *ucontrol)
6692 rc = volume_get_status(&s);
6696 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6700 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6701 struct snd_ctl_elem_value *ucontrol)
6703 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6704 ucontrol->value.integer.value[0]);
6705 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6708 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6710 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6711 struct snd_ctl_elem_value *ucontrol)
6716 rc = volume_get_status(&s);
6720 ucontrol->value.integer.value[0] =
6721 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6725 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6726 struct snd_ctl_elem_value *ucontrol)
6728 tpacpi_disclose_usertask("ALSA", "%smute\n",
6729 ucontrol->value.integer.value[0] ?
6731 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6734 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6736 .name = "Console Playback Volume",
6738 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6739 .info = volume_alsa_vol_info,
6740 .get = volume_alsa_vol_get,
6743 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6744 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6745 .name = "Console Playback Switch",
6747 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6748 .info = volume_alsa_mute_info,
6749 .get = volume_alsa_mute_get,
6752 static void volume_suspend(void)
6754 tpacpi_volume_checkpoint_nvram();
6757 static void volume_resume(void)
6759 volume_alsa_notify_change();
6762 static void volume_shutdown(void)
6764 tpacpi_volume_checkpoint_nvram();
6767 static void volume_exit(void)
6770 snd_card_free(alsa_card);
6774 tpacpi_volume_checkpoint_nvram();
6777 static int __init volume_create_alsa_mixer(void)
6779 struct snd_card *card;
6780 struct tpacpi_alsa_data *data;
6781 struct snd_kcontrol *ctl_vol;
6782 struct snd_kcontrol *ctl_mute;
6785 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6786 sizeof(struct tpacpi_alsa_data), &card);
6787 if (rc < 0 || !card) {
6788 pr_err("Failed to create ALSA card structures: %d\n", rc);
6792 BUG_ON(!card->private_data);
6793 data = card->private_data;
6796 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6797 sizeof(card->driver));
6798 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6799 sizeof(card->shortname));
6800 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6801 (thinkpad_id.ec_version_str) ?
6802 thinkpad_id.ec_version_str : "(unknown)");
6803 snprintf(card->longname, sizeof(card->longname),
6804 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6805 (thinkpad_id.ec_version_str) ?
6806 thinkpad_id.ec_version_str : "unknown");
6808 if (volume_control_allowed) {
6809 volume_alsa_control_vol.put = volume_alsa_vol_put;
6810 volume_alsa_control_vol.access =
6811 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6813 volume_alsa_control_mute.put = volume_alsa_mute_put;
6814 volume_alsa_control_mute.access =
6815 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6818 if (!tp_features.mixer_no_level_control) {
6819 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6820 rc = snd_ctl_add(card, ctl_vol);
6822 pr_err("Failed to create ALSA volume control: %d\n",
6826 data->ctl_vol_id = &ctl_vol->id;
6829 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6830 rc = snd_ctl_add(card, ctl_mute);
6832 pr_err("Failed to create ALSA mute control: %d\n", rc);
6835 data->ctl_mute_id = &ctl_mute->id;
6837 snd_card_set_dev(card, &tpacpi_pdev->dev);
6838 rc = snd_card_register(card);
6840 pr_err("Failed to register ALSA card: %d\n", rc);
6848 snd_card_free(card);
6852 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6853 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6855 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6856 /* Whitelist volume level on all IBM by default */
6857 { .vendor = PCI_VENDOR_ID_IBM,
6858 .bios = TPACPI_MATCH_ANY,
6859 .ec = TPACPI_MATCH_ANY,
6860 .quirks = TPACPI_VOL_Q_LEVEL },
6862 /* Lenovo models with volume control (needs confirmation) */
6863 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6864 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6865 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6866 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6867 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6868 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6869 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6871 /* Whitelist mute-only on all Lenovo by default */
6872 { .vendor = PCI_VENDOR_ID_LENOVO,
6873 .bios = TPACPI_MATCH_ANY,
6874 .ec = TPACPI_MATCH_ANY,
6875 .quirks = TPACPI_VOL_Q_MUTEONLY }
6878 static int __init volume_init(struct ibm_init_struct *iibm)
6880 unsigned long quirks;
6883 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6885 mutex_init(&volume_mutex);
6888 * Check for module parameter bogosity, note that we
6889 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6890 * able to detect "unspecified"
6892 if (volume_mode > TPACPI_VOL_MODE_MAX)
6895 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6896 pr_err("UCMS step volume mode not implemented, "
6897 "please contact %s\n", TPACPI_MAIL);
6901 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6905 * The ALSA mixer is our primary interface.
6906 * When disabled, don't install the subdriver at all
6909 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6910 "ALSA mixer disabled by parameter, "
6911 "not loading volume subdriver...\n");
6915 quirks = tpacpi_check_quirks(volume_quirk_table,
6916 ARRAY_SIZE(volume_quirk_table));
6918 switch (volume_capabilities) {
6919 case TPACPI_VOL_CAP_AUTO:
6920 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6921 tp_features.mixer_no_level_control = 1;
6922 else if (quirks & TPACPI_VOL_Q_LEVEL)
6923 tp_features.mixer_no_level_control = 0;
6925 return 1; /* no mixer */
6927 case TPACPI_VOL_CAP_VOLMUTE:
6928 tp_features.mixer_no_level_control = 0;
6930 case TPACPI_VOL_CAP_MUTEONLY:
6931 tp_features.mixer_no_level_control = 1;
6937 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6938 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6939 "using user-supplied volume_capabilities=%d\n",
6940 volume_capabilities);
6942 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6943 volume_mode == TPACPI_VOL_MODE_MAX) {
6944 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6946 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6947 "driver auto-selected volume_mode=%d\n",
6950 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6951 "using user-supplied volume_mode=%d\n",
6955 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6956 "mute is supported, volume control is %s\n",
6957 str_supported(!tp_features.mixer_no_level_control));
6959 rc = volume_create_alsa_mixer();
6961 pr_err("Could not create the ALSA mixer interface\n");
6965 pr_info("Console audio control enabled, mode: %s\n",
6966 (volume_control_allowed) ?
6967 "override (read/write)" :
6968 "monitor (read only)");
6970 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6971 "registering volume hotkeys as change notification\n");
6972 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6973 | TP_ACPI_HKEY_VOLUP_MASK
6974 | TP_ACPI_HKEY_VOLDWN_MASK
6975 | TP_ACPI_HKEY_MUTE_MASK);
6980 static int volume_read(struct seq_file *m)
6984 if (volume_get_status(&status) < 0) {
6985 seq_printf(m, "level:\t\tunreadable\n");
6987 if (tp_features.mixer_no_level_control)
6988 seq_printf(m, "level:\t\tunsupported\n");
6990 seq_printf(m, "level:\t\t%d\n",
6991 status & TP_EC_AUDIO_LVL_MSK);
6993 seq_printf(m, "mute:\t\t%s\n",
6994 onoff(status, TP_EC_AUDIO_MUTESW));
6996 if (volume_control_allowed) {
6997 seq_printf(m, "commands:\tunmute, mute\n");
6998 if (!tp_features.mixer_no_level_control) {
7000 "commands:\tup, down\n");
7002 "commands:\tlevel <level>"
7003 " (<level> is 0-%d)\n",
7012 static int volume_write(char *buf)
7015 u8 new_level, new_mute;
7021 * We do allow volume control at driver startup, so that the
7022 * user can set initial state through the volume=... parameter hack.
7024 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7025 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7026 tp_warned.volume_ctrl_forbidden = 1;
7027 pr_notice("Console audio control in monitor mode, "
7028 "changes are not allowed\n");
7029 pr_notice("Use the volume_control=1 module parameter "
7030 "to enable volume control\n");
7035 rc = volume_get_status(&s);
7039 new_level = s & TP_EC_AUDIO_LVL_MSK;
7040 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7042 while ((cmd = next_cmd(&buf))) {
7043 if (!tp_features.mixer_no_level_control) {
7044 if (strlencmp(cmd, "up") == 0) {
7047 else if (new_level < TP_EC_VOLUME_MAX)
7050 } else if (strlencmp(cmd, "down") == 0) {
7053 else if (new_level > 0)
7056 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7057 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7062 if (strlencmp(cmd, "mute") == 0)
7063 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7064 else if (strlencmp(cmd, "unmute") == 0)
7070 if (tp_features.mixer_no_level_control) {
7071 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7072 new_mute ? "" : "un");
7073 rc = volume_set_mute(!!new_mute);
7075 tpacpi_disclose_usertask("procfs volume",
7076 "%smute and set level to %d\n",
7077 new_mute ? "" : "un", new_level);
7078 rc = volume_set_status(new_mute | new_level);
7080 volume_alsa_notify_change();
7082 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7085 static struct ibm_struct volume_driver_data = {
7087 .read = volume_read,
7088 .write = volume_write,
7089 .exit = volume_exit,
7090 .suspend = volume_suspend,
7091 .resume = volume_resume,
7092 .shutdown = volume_shutdown,
7095 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7097 #define alsa_card NULL
7099 static void inline volume_alsa_notify_change(void)
7103 static int __init volume_init(struct ibm_init_struct *iibm)
7105 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7110 static struct ibm_struct volume_driver_data = {
7114 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7116 /*************************************************************************
7123 * TPACPI_FAN_RD_ACPI_GFAN:
7124 * ACPI GFAN method: returns fan level
7126 * see TPACPI_FAN_WR_ACPI_SFAN
7127 * EC 0x2f (HFSP) not available if GFAN exists
7129 * TPACPI_FAN_WR_ACPI_SFAN:
7130 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7132 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7135 * TPACPI_FAN_WR_TPEC:
7136 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7137 * Supported on almost all ThinkPads
7139 * Fan speed changes of any sort (including those caused by the
7140 * disengaged mode) are usually done slowly by the firmware as the
7141 * maximum amount of fan duty cycle change per second seems to be
7144 * Reading is not available if GFAN exists.
7145 * Writing is not available if SFAN exists.
7148 * 7 automatic mode engaged;
7149 * (default operation mode of the ThinkPad)
7150 * fan level is ignored in this mode.
7151 * 6 full speed mode (takes precedence over bit 7);
7152 * not available on all thinkpads. May disable
7153 * the tachometer while the fan controller ramps up
7154 * the speed (which can take up to a few *minutes*).
7155 * Speeds up fan to 100% duty-cycle, which is far above
7156 * the standard RPM levels. It is not impossible that
7157 * it could cause hardware damage.
7158 * 5-3 unused in some models. Extra bits for fan level
7159 * in others, but still useless as all values above
7160 * 7 map to the same speed as level 7 in these models.
7161 * 2-0 fan level (0..7 usually)
7163 * 0x07 = max (set when temperatures critical)
7164 * Some ThinkPads may have other levels, see
7165 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7167 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7168 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7169 * does so, its initial value is meaningless (0x07).
7171 * For firmware bugs, refer to:
7172 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7176 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7177 * Main fan tachometer reading (in RPM)
7179 * This register is present on all ThinkPads with a new-style EC, and
7180 * it is known not to be present on the A21m/e, and T22, as there is
7181 * something else in offset 0x84 according to the ACPI DSDT. Other
7182 * ThinkPads from this same time period (and earlier) probably lack the
7183 * tachometer as well.
7185 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7186 * was never fixed by IBM to report the EC firmware version string
7187 * probably support the tachometer (like the early X models), so
7188 * detecting it is quite hard. We need more data to know for sure.
7190 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7193 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7196 * For firmware bugs, refer to:
7197 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7201 * ThinkPad EC register 0x31 bit 0 (only on select models)
7203 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7204 * show the speed of the main fan. When bit 0 of EC register 0x31
7205 * is one, the tachometer registers show the speed of the auxiliary
7208 * Fan control seems to affect both fans, regardless of the state
7211 * So far, only the firmware for the X60/X61 non-tablet versions
7212 * seem to support this (firmware TP-7M).
7214 * TPACPI_FAN_WR_ACPI_FANS:
7215 * ThinkPad X31, X40, X41. Not available in the X60.
7217 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7218 * high speed. ACPI DSDT seems to map these three speeds to levels
7219 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7220 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7222 * The speeds are stored on handles
7223 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7225 * There are three default speed sets, accessible as handles:
7226 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7228 * ACPI DSDT switches which set is in use depending on various
7231 * TPACPI_FAN_WR_TPEC is also available and should be used to
7232 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7233 * but the ACPI tables just mention level 7.
7236 enum { /* Fan control constants */
7237 fan_status_offset = 0x2f, /* EC register 0x2f */
7238 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7239 * 0x84 must be read before 0x85 */
7240 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7241 bit 0 selects which fan is active */
7243 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7244 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7246 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7249 enum fan_status_access_mode {
7250 TPACPI_FAN_NONE = 0, /* No fan status or control */
7251 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7252 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7255 enum fan_control_access_mode {
7256 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7257 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7258 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7259 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7262 enum fan_control_commands {
7263 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7264 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7265 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7266 * and also watchdog cmd */
7269 static bool fan_control_allowed;
7271 static enum fan_status_access_mode fan_status_access_mode;
7272 static enum fan_control_access_mode fan_control_access_mode;
7273 static enum fan_control_commands fan_control_commands;
7275 static u8 fan_control_initial_status;
7276 static u8 fan_control_desired_level;
7277 static u8 fan_control_resume_level;
7278 static int fan_watchdog_maxinterval;
7280 static struct mutex fan_mutex;
7282 static void fan_watchdog_fire(struct work_struct *ignored);
7283 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7285 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7286 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7287 "\\FSPD", /* 600e/x, 770e, 770x */
7289 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7290 "JFNS", /* 770x-JL */
7294 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7295 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7296 * be in auto mode (0x80).
7298 * This is corrected by any write to HFSP either by the driver, or
7301 * We assume 0x07 really means auto mode while this quirk is active,
7302 * as this is far more likely than the ThinkPad being in level 7,
7303 * which is only used by the firmware during thermal emergencies.
7305 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7306 * TP-70 (T43, R52), which are known to be buggy.
7309 static void fan_quirk1_setup(void)
7311 if (fan_control_initial_status == 0x07) {
7312 pr_notice("fan_init: initial fan status is unknown, "
7313 "assuming it is in auto mode\n");
7314 tp_features.fan_ctrl_status_undef = 1;
7318 static void fan_quirk1_handle(u8 *fan_status)
7320 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7321 if (*fan_status != fan_control_initial_status) {
7322 /* something changed the HFSP regisnter since
7323 * driver init time, so it is not undefined
7325 tp_features.fan_ctrl_status_undef = 0;
7327 /* Return most likely status. In fact, it
7328 * might be the only possible status */
7329 *fan_status = TP_EC_FAN_AUTO;
7334 /* Select main fan on X60/X61, NOOP on others */
7335 static bool fan_select_fan1(void)
7337 if (tp_features.second_fan) {
7340 if (ec_read(fan_select_offset, &val) < 0)
7343 if (ec_write(fan_select_offset, val) < 0)
7349 /* Select secondary fan on X60/X61 */
7350 static bool fan_select_fan2(void)
7354 if (!tp_features.second_fan)
7357 if (ec_read(fan_select_offset, &val) < 0)
7360 if (ec_write(fan_select_offset, val) < 0)
7367 * Call with fan_mutex held
7369 static void fan_update_desired_level(u8 status)
7372 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7374 fan_control_desired_level = 7;
7376 fan_control_desired_level = status;
7380 static int fan_get_status(u8 *status)
7385 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7387 switch (fan_status_access_mode) {
7388 case TPACPI_FAN_RD_ACPI_GFAN:
7389 /* 570, 600e/x, 770e, 770x */
7391 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7399 case TPACPI_FAN_RD_TPEC:
7400 /* all except 570, 600e/x, 770e, 770x */
7401 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7404 if (likely(status)) {
7406 fan_quirk1_handle(status);
7418 static int fan_get_status_safe(u8 *status)
7423 if (mutex_lock_killable(&fan_mutex))
7424 return -ERESTARTSYS;
7425 rc = fan_get_status(&s);
7427 fan_update_desired_level(s);
7428 mutex_unlock(&fan_mutex);
7436 static int fan_get_speed(unsigned int *speed)
7440 switch (fan_status_access_mode) {
7441 case TPACPI_FAN_RD_TPEC:
7442 /* all except 570, 600e/x, 770e, 770x */
7443 if (unlikely(!fan_select_fan1()))
7445 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7446 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7450 *speed = (hi << 8) | lo;
7461 static int fan2_get_speed(unsigned int *speed)
7466 switch (fan_status_access_mode) {
7467 case TPACPI_FAN_RD_TPEC:
7468 /* all except 570, 600e/x, 770e, 770x */
7469 if (unlikely(!fan_select_fan2()))
7471 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7472 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7473 fan_select_fan1(); /* play it safe */
7478 *speed = (hi << 8) | lo;
7489 static int fan_set_level(int level)
7491 if (!fan_control_allowed)
7494 switch (fan_control_access_mode) {
7495 case TPACPI_FAN_WR_ACPI_SFAN:
7496 if (level >= 0 && level <= 7) {
7497 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7503 case TPACPI_FAN_WR_ACPI_FANS:
7504 case TPACPI_FAN_WR_TPEC:
7505 if (!(level & TP_EC_FAN_AUTO) &&
7506 !(level & TP_EC_FAN_FULLSPEED) &&
7507 ((level < 0) || (level > 7)))
7510 /* safety net should the EC not support AUTO
7511 * or FULLSPEED mode bits and just ignore them */
7512 if (level & TP_EC_FAN_FULLSPEED)
7513 level |= 7; /* safety min speed 7 */
7514 else if (level & TP_EC_FAN_AUTO)
7515 level |= 4; /* safety min speed 4 */
7517 if (!acpi_ec_write(fan_status_offset, level))
7520 tp_features.fan_ctrl_status_undef = 0;
7527 vdbg_printk(TPACPI_DBG_FAN,
7528 "fan control: set fan control register to 0x%02x\n", level);
7532 static int fan_set_level_safe(int level)
7536 if (!fan_control_allowed)
7539 if (mutex_lock_killable(&fan_mutex))
7540 return -ERESTARTSYS;
7542 if (level == TPACPI_FAN_LAST_LEVEL)
7543 level = fan_control_desired_level;
7545 rc = fan_set_level(level);
7547 fan_update_desired_level(level);
7549 mutex_unlock(&fan_mutex);
7553 static int fan_set_enable(void)
7558 if (!fan_control_allowed)
7561 if (mutex_lock_killable(&fan_mutex))
7562 return -ERESTARTSYS;
7564 switch (fan_control_access_mode) {
7565 case TPACPI_FAN_WR_ACPI_FANS:
7566 case TPACPI_FAN_WR_TPEC:
7567 rc = fan_get_status(&s);
7571 /* Don't go out of emergency fan mode */
7574 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7577 if (!acpi_ec_write(fan_status_offset, s))
7580 tp_features.fan_ctrl_status_undef = 0;
7585 case TPACPI_FAN_WR_ACPI_SFAN:
7586 rc = fan_get_status(&s);
7592 /* Set fan to at least level 4 */
7595 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7605 mutex_unlock(&fan_mutex);
7608 vdbg_printk(TPACPI_DBG_FAN,
7609 "fan control: set fan control register to 0x%02x\n",
7614 static int fan_set_disable(void)
7618 if (!fan_control_allowed)
7621 if (mutex_lock_killable(&fan_mutex))
7622 return -ERESTARTSYS;
7625 switch (fan_control_access_mode) {
7626 case TPACPI_FAN_WR_ACPI_FANS:
7627 case TPACPI_FAN_WR_TPEC:
7628 if (!acpi_ec_write(fan_status_offset, 0x00))
7631 fan_control_desired_level = 0;
7632 tp_features.fan_ctrl_status_undef = 0;
7636 case TPACPI_FAN_WR_ACPI_SFAN:
7637 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7640 fan_control_desired_level = 0;
7648 vdbg_printk(TPACPI_DBG_FAN,
7649 "fan control: set fan control register to 0\n");
7651 mutex_unlock(&fan_mutex);
7655 static int fan_set_speed(int speed)
7659 if (!fan_control_allowed)
7662 if (mutex_lock_killable(&fan_mutex))
7663 return -ERESTARTSYS;
7666 switch (fan_control_access_mode) {
7667 case TPACPI_FAN_WR_ACPI_FANS:
7668 if (speed >= 0 && speed <= 65535) {
7669 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7670 speed, speed, speed))
7680 mutex_unlock(&fan_mutex);
7684 static void fan_watchdog_reset(void)
7686 static int fan_watchdog_active;
7688 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7691 if (fan_watchdog_active)
7692 cancel_delayed_work(&fan_watchdog_task);
7694 if (fan_watchdog_maxinterval > 0 &&
7695 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7696 fan_watchdog_active = 1;
7697 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7698 msecs_to_jiffies(fan_watchdog_maxinterval
7700 pr_err("failed to queue the fan watchdog, "
7701 "watchdog will not trigger\n");
7704 fan_watchdog_active = 0;
7707 static void fan_watchdog_fire(struct work_struct *ignored)
7711 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7714 pr_notice("fan watchdog: enabling fan\n");
7715 rc = fan_set_enable();
7717 pr_err("fan watchdog: error %d while enabling fan, "
7718 "will try again later...\n", -rc);
7719 /* reschedule for later */
7720 fan_watchdog_reset();
7725 * SYSFS fan layout: hwmon compatible (device)
7728 * 0: "disengaged" mode
7730 * 2: native EC "auto" mode (recommended, hardware default)
7732 * pwm*: set speed in manual mode, ignored otherwise.
7733 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7736 * fan*_input: tachometer reading, RPM
7739 * SYSFS fan layout: extensions
7741 * fan_watchdog (driver):
7742 * fan watchdog interval in seconds, 0 disables (default), max 120
7745 /* sysfs fan pwm1_enable ----------------------------------------------- */
7746 static ssize_t fan_pwm1_enable_show(struct device *dev,
7747 struct device_attribute *attr,
7753 res = fan_get_status_safe(&status);
7757 if (status & TP_EC_FAN_FULLSPEED) {
7759 } else if (status & TP_EC_FAN_AUTO) {
7764 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7767 static ssize_t fan_pwm1_enable_store(struct device *dev,
7768 struct device_attribute *attr,
7769 const char *buf, size_t count)
7774 if (parse_strtoul(buf, 2, &t))
7777 tpacpi_disclose_usertask("hwmon pwm1_enable",
7778 "set fan mode to %lu\n", t);
7782 level = TP_EC_FAN_FULLSPEED;
7785 level = TPACPI_FAN_LAST_LEVEL;
7788 level = TP_EC_FAN_AUTO;
7791 /* reserved for software-controlled auto mode */
7797 res = fan_set_level_safe(level);
7803 fan_watchdog_reset();
7808 static struct device_attribute dev_attr_fan_pwm1_enable =
7809 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7810 fan_pwm1_enable_show, fan_pwm1_enable_store);
7812 /* sysfs fan pwm1 ------------------------------------------------------ */
7813 static ssize_t fan_pwm1_show(struct device *dev,
7814 struct device_attribute *attr,
7820 res = fan_get_status_safe(&status);
7825 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7826 status = fan_control_desired_level;
7831 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7834 static ssize_t fan_pwm1_store(struct device *dev,
7835 struct device_attribute *attr,
7836 const char *buf, size_t count)
7840 u8 status, newlevel;
7842 if (parse_strtoul(buf, 255, &s))
7845 tpacpi_disclose_usertask("hwmon pwm1",
7846 "set fan speed to %lu\n", s);
7848 /* scale down from 0-255 to 0-7 */
7849 newlevel = (s >> 5) & 0x07;
7851 if (mutex_lock_killable(&fan_mutex))
7852 return -ERESTARTSYS;
7854 rc = fan_get_status(&status);
7855 if (!rc && (status &
7856 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7857 rc = fan_set_level(newlevel);
7861 fan_update_desired_level(newlevel);
7862 fan_watchdog_reset();
7866 mutex_unlock(&fan_mutex);
7867 return (rc)? rc : count;
7870 static struct device_attribute dev_attr_fan_pwm1 =
7871 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7872 fan_pwm1_show, fan_pwm1_store);
7874 /* sysfs fan fan1_input ------------------------------------------------ */
7875 static ssize_t fan_fan1_input_show(struct device *dev,
7876 struct device_attribute *attr,
7882 res = fan_get_speed(&speed);
7886 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7889 static struct device_attribute dev_attr_fan_fan1_input =
7890 __ATTR(fan1_input, S_IRUGO,
7891 fan_fan1_input_show, NULL);
7893 /* sysfs fan fan2_input ------------------------------------------------ */
7894 static ssize_t fan_fan2_input_show(struct device *dev,
7895 struct device_attribute *attr,
7901 res = fan2_get_speed(&speed);
7905 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7908 static struct device_attribute dev_attr_fan_fan2_input =
7909 __ATTR(fan2_input, S_IRUGO,
7910 fan_fan2_input_show, NULL);
7912 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7913 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7916 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7919 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7920 const char *buf, size_t count)
7924 if (parse_strtoul(buf, 120, &t))
7927 if (!fan_control_allowed)
7930 fan_watchdog_maxinterval = t;
7931 fan_watchdog_reset();
7933 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7938 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7939 fan_fan_watchdog_show, fan_fan_watchdog_store);
7941 /* --------------------------------------------------------------------- */
7942 static struct attribute *fan_attributes[] = {
7943 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7944 &dev_attr_fan_fan1_input.attr,
7945 NULL, /* for fan2_input */
7949 static const struct attribute_group fan_attr_group = {
7950 .attrs = fan_attributes,
7953 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7954 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7956 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7957 { .vendor = PCI_VENDOR_ID_IBM, \
7958 .bios = TPACPI_MATCH_ANY, \
7959 .ec = TPID(__id1, __id2), \
7960 .quirks = __quirks }
7962 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7963 { .vendor = PCI_VENDOR_ID_LENOVO, \
7964 .bios = TPACPI_MATCH_ANY, \
7965 .ec = TPID(__id1, __id2), \
7966 .quirks = __quirks }
7968 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7969 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7970 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7971 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7972 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7973 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7976 #undef TPACPI_FAN_QL
7977 #undef TPACPI_FAN_QI
7979 static int __init fan_init(struct ibm_init_struct *iibm)
7982 unsigned long quirks;
7984 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7985 "initializing fan subdriver\n");
7987 mutex_init(&fan_mutex);
7988 fan_status_access_mode = TPACPI_FAN_NONE;
7989 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7990 fan_control_commands = 0;
7991 fan_watchdog_maxinterval = 0;
7992 tp_features.fan_ctrl_status_undef = 0;
7993 tp_features.second_fan = 0;
7994 fan_control_desired_level = 7;
7996 if (tpacpi_is_ibm()) {
7997 TPACPI_ACPIHANDLE_INIT(fans);
7998 TPACPI_ACPIHANDLE_INIT(gfan);
7999 TPACPI_ACPIHANDLE_INIT(sfan);
8002 quirks = tpacpi_check_quirks(fan_quirk_table,
8003 ARRAY_SIZE(fan_quirk_table));
8006 /* 570, 600e/x, 770e, 770x */
8007 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8009 /* all other ThinkPads: note that even old-style
8010 * ThinkPad ECs supports the fan control register */
8011 if (likely(acpi_ec_read(fan_status_offset,
8012 &fan_control_initial_status))) {
8013 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8014 if (quirks & TPACPI_FAN_Q1)
8016 if (quirks & TPACPI_FAN_2FAN) {
8017 tp_features.second_fan = 1;
8018 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8019 "secondary fan support enabled\n");
8022 pr_err("ThinkPad ACPI EC access misbehaving, "
8023 "fan status and control unavailable\n");
8030 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8031 fan_control_commands |=
8032 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8035 /* gfan without sfan means no fan control */
8036 /* all other models implement TP EC 0x2f control */
8040 fan_control_access_mode =
8041 TPACPI_FAN_WR_ACPI_FANS;
8042 fan_control_commands |=
8043 TPACPI_FAN_CMD_SPEED |
8044 TPACPI_FAN_CMD_LEVEL |
8045 TPACPI_FAN_CMD_ENABLE;
8047 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8048 fan_control_commands |=
8049 TPACPI_FAN_CMD_LEVEL |
8050 TPACPI_FAN_CMD_ENABLE;
8055 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8056 "fan is %s, modes %d, %d\n",
8057 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8058 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8059 fan_status_access_mode, fan_control_access_mode);
8061 /* fan control master switch */
8062 if (!fan_control_allowed) {
8063 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8064 fan_control_commands = 0;
8065 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8066 "fan control features disabled by parameter\n");
8069 /* update fan_control_desired_level */
8070 if (fan_status_access_mode != TPACPI_FAN_NONE)
8071 fan_get_status_safe(NULL);
8073 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8074 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8075 if (tp_features.second_fan) {
8076 /* attach second fan tachometer */
8077 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8078 &dev_attr_fan_fan2_input.attr;
8080 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8085 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8086 &driver_attr_fan_watchdog);
8088 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8097 static void fan_exit(void)
8099 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8100 "cancelling any pending fan watchdog tasks\n");
8102 /* FIXME: can we really do this unconditionally? */
8103 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8104 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8105 &driver_attr_fan_watchdog);
8107 cancel_delayed_work(&fan_watchdog_task);
8108 flush_workqueue(tpacpi_wq);
8111 static void fan_suspend(void)
8115 if (!fan_control_allowed)
8118 /* Store fan status in cache */
8119 fan_control_resume_level = 0;
8120 rc = fan_get_status_safe(&fan_control_resume_level);
8122 pr_notice("failed to read fan level for later "
8123 "restore during resume: %d\n", rc);
8125 /* if it is undefined, don't attempt to restore it.
8127 if (tp_features.fan_ctrl_status_undef)
8128 fan_control_resume_level = 0;
8131 static void fan_resume(void)
8133 u8 current_level = 7;
8134 bool do_set = false;
8137 /* DSDT *always* updates status on resume */
8138 tp_features.fan_ctrl_status_undef = 0;
8140 if (!fan_control_allowed ||
8141 !fan_control_resume_level ||
8142 (fan_get_status_safe(¤t_level) < 0))
8145 switch (fan_control_access_mode) {
8146 case TPACPI_FAN_WR_ACPI_SFAN:
8147 /* never decrease fan level */
8148 do_set = (fan_control_resume_level > current_level);
8150 case TPACPI_FAN_WR_ACPI_FANS:
8151 case TPACPI_FAN_WR_TPEC:
8152 /* never decrease fan level, scale is:
8153 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8155 * We expect the firmware to set either 7 or AUTO, but we
8156 * handle FULLSPEED out of paranoia.
8158 * So, we can safely only restore FULLSPEED or 7, anything
8159 * else could slow the fan. Restoring AUTO is useless, at
8160 * best that's exactly what the DSDT already set (it is the
8163 * Always keep in mind that the DSDT *will* have set the
8164 * fans to what the vendor supposes is the best level. We
8165 * muck with it only to speed the fan up.
8167 if (fan_control_resume_level != 7 &&
8168 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8171 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8172 (current_level != fan_control_resume_level);
8178 pr_notice("restoring fan level to 0x%02x\n",
8179 fan_control_resume_level);
8180 rc = fan_set_level_safe(fan_control_resume_level);
8182 pr_notice("failed to restore fan level: %d\n", rc);
8186 static int fan_read(struct seq_file *m)
8190 unsigned int speed = 0;
8192 switch (fan_status_access_mode) {
8193 case TPACPI_FAN_RD_ACPI_GFAN:
8194 /* 570, 600e/x, 770e, 770x */
8195 rc = fan_get_status_safe(&status);
8199 seq_printf(m, "status:\t\t%s\n"
8201 (status != 0) ? "enabled" : "disabled", status);
8204 case TPACPI_FAN_RD_TPEC:
8205 /* all except 570, 600e/x, 770e, 770x */
8206 rc = fan_get_status_safe(&status);
8210 seq_printf(m, "status:\t\t%s\n",
8211 (status != 0) ? "enabled" : "disabled");
8213 rc = fan_get_speed(&speed);
8217 seq_printf(m, "speed:\t\t%d\n", speed);
8219 if (status & TP_EC_FAN_FULLSPEED)
8220 /* Disengaged mode takes precedence */
8221 seq_printf(m, "level:\t\tdisengaged\n");
8222 else if (status & TP_EC_FAN_AUTO)
8223 seq_printf(m, "level:\t\tauto\n");
8225 seq_printf(m, "level:\t\t%d\n", status);
8228 case TPACPI_FAN_NONE:
8230 seq_printf(m, "status:\t\tnot supported\n");
8233 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8234 seq_printf(m, "commands:\tlevel <level>");
8236 switch (fan_control_access_mode) {
8237 case TPACPI_FAN_WR_ACPI_SFAN:
8238 seq_printf(m, " (<level> is 0-7)\n");
8242 seq_printf(m, " (<level> is 0-7, "
8243 "auto, disengaged, full-speed)\n");
8248 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8249 seq_printf(m, "commands:\tenable, disable\n"
8250 "commands:\twatchdog <timeout> (<timeout> "
8251 "is 0 (off), 1-120 (seconds))\n");
8253 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8254 seq_printf(m, "commands:\tspeed <speed>"
8255 " (<speed> is 0-65535)\n");
8260 static int fan_write_cmd_level(const char *cmd, int *rc)
8264 if (strlencmp(cmd, "level auto") == 0)
8265 level = TP_EC_FAN_AUTO;
8266 else if ((strlencmp(cmd, "level disengaged") == 0) |
8267 (strlencmp(cmd, "level full-speed") == 0))
8268 level = TP_EC_FAN_FULLSPEED;
8269 else if (sscanf(cmd, "level %d", &level) != 1)
8272 *rc = fan_set_level_safe(level);
8274 pr_err("level command accepted for unsupported access mode %d\n",
8275 fan_control_access_mode);
8277 tpacpi_disclose_usertask("procfs fan",
8278 "set level to %d\n", level);
8283 static int fan_write_cmd_enable(const char *cmd, int *rc)
8285 if (strlencmp(cmd, "enable") != 0)
8288 *rc = fan_set_enable();
8290 pr_err("enable command accepted for unsupported access mode %d\n",
8291 fan_control_access_mode);
8293 tpacpi_disclose_usertask("procfs fan", "enable\n");
8298 static int fan_write_cmd_disable(const char *cmd, int *rc)
8300 if (strlencmp(cmd, "disable") != 0)
8303 *rc = fan_set_disable();
8305 pr_err("disable command accepted for unsupported access mode %d\n",
8306 fan_control_access_mode);
8308 tpacpi_disclose_usertask("procfs fan", "disable\n");
8313 static int fan_write_cmd_speed(const char *cmd, int *rc)
8318 * Support speed <low> <medium> <high> ? */
8320 if (sscanf(cmd, "speed %d", &speed) != 1)
8323 *rc = fan_set_speed(speed);
8325 pr_err("speed command accepted for unsupported access mode %d\n",
8326 fan_control_access_mode);
8328 tpacpi_disclose_usertask("procfs fan",
8329 "set speed to %d\n", speed);
8334 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8338 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8341 if (interval < 0 || interval > 120)
8344 fan_watchdog_maxinterval = interval;
8345 tpacpi_disclose_usertask("procfs fan",
8346 "set watchdog timer to %d\n",
8353 static int fan_write(char *buf)
8358 while (!rc && (cmd = next_cmd(&buf))) {
8359 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8360 fan_write_cmd_level(cmd, &rc)) &&
8361 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8362 (fan_write_cmd_enable(cmd, &rc) ||
8363 fan_write_cmd_disable(cmd, &rc) ||
8364 fan_write_cmd_watchdog(cmd, &rc))) &&
8365 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8366 fan_write_cmd_speed(cmd, &rc))
8370 fan_watchdog_reset();
8376 static struct ibm_struct fan_driver_data = {
8381 .suspend = fan_suspend,
8382 .resume = fan_resume,
8385 /****************************************************************************
8386 ****************************************************************************
8390 ****************************************************************************
8391 ****************************************************************************/
8394 * HKEY event callout for other subdrivers go here
8395 * (yes, it is ugly, but it is quick, safe, and gets the job done
8397 static void tpacpi_driver_event(const unsigned int hkey_event)
8399 if (ibm_backlight_device) {
8400 switch (hkey_event) {
8401 case TP_HKEY_EV_BRGHT_UP:
8402 case TP_HKEY_EV_BRGHT_DOWN:
8403 tpacpi_brightness_notify_change();
8407 switch (hkey_event) {
8408 case TP_HKEY_EV_VOL_UP:
8409 case TP_HKEY_EV_VOL_DOWN:
8410 case TP_HKEY_EV_VOL_MUTE:
8411 volume_alsa_notify_change();
8416 static void hotkey_driver_event(const unsigned int scancode)
8418 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8421 /* sysfs name ---------------------------------------------------------- */
8422 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8423 struct device_attribute *attr,
8426 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8429 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8430 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8432 /* --------------------------------------------------------------------- */
8435 static struct proc_dir_entry *proc_dir;
8438 * Module and infrastructure proble, init and exit handling
8441 static bool force_load;
8443 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8444 static const char * __init str_supported(int is_supported)
8446 static char text_unsupported[] __initdata = "not supported";
8448 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8450 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8452 static void ibm_exit(struct ibm_struct *ibm)
8454 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8456 list_del_init(&ibm->all_drivers);
8458 if (ibm->flags.acpi_notify_installed) {
8459 dbg_printk(TPACPI_DBG_EXIT,
8460 "%s: acpi_remove_notify_handler\n", ibm->name);
8462 acpi_remove_notify_handler(*ibm->acpi->handle,
8464 dispatch_acpi_notify);
8465 ibm->flags.acpi_notify_installed = 0;
8468 if (ibm->flags.proc_created) {
8469 dbg_printk(TPACPI_DBG_EXIT,
8470 "%s: remove_proc_entry\n", ibm->name);
8471 remove_proc_entry(ibm->name, proc_dir);
8472 ibm->flags.proc_created = 0;
8475 if (ibm->flags.acpi_driver_registered) {
8476 dbg_printk(TPACPI_DBG_EXIT,
8477 "%s: acpi_bus_unregister_driver\n", ibm->name);
8479 acpi_bus_unregister_driver(ibm->acpi->driver);
8480 kfree(ibm->acpi->driver);
8481 ibm->acpi->driver = NULL;
8482 ibm->flags.acpi_driver_registered = 0;
8485 if (ibm->flags.init_called && ibm->exit) {
8487 ibm->flags.init_called = 0;
8490 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8493 static int __init ibm_init(struct ibm_init_struct *iibm)
8496 struct ibm_struct *ibm = iibm->data;
8497 struct proc_dir_entry *entry;
8499 BUG_ON(ibm == NULL);
8501 INIT_LIST_HEAD(&ibm->all_drivers);
8503 if (ibm->flags.experimental && !experimental)
8506 dbg_printk(TPACPI_DBG_INIT,
8507 "probing for %s\n", ibm->name);
8510 ret = iibm->init(iibm);
8512 return 0; /* probe failed */
8516 ibm->flags.init_called = 1;
8520 if (ibm->acpi->hid) {
8521 ret = register_tpacpi_subdriver(ibm);
8526 if (ibm->acpi->notify) {
8527 ret = setup_acpi_notify(ibm);
8528 if (ret == -ENODEV) {
8529 pr_notice("disabling subdriver %s\n",
8539 dbg_printk(TPACPI_DBG_INIT,
8540 "%s installed\n", ibm->name);
8543 umode_t mode = iibm->base_procfs_mode;
8549 entry = proc_create_data(ibm->name, mode, proc_dir,
8550 &dispatch_proc_fops, ibm);
8552 pr_err("unable to create proc entry %s\n", ibm->name);
8556 ibm->flags.proc_created = 1;
8559 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8564 dbg_printk(TPACPI_DBG_INIT,
8565 "%s: at error exit path with result %d\n",
8569 return (ret < 0)? ret : 0;
8574 static bool __pure __init tpacpi_is_fw_digit(const char c)
8576 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8579 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8580 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8583 return s && strlen(s) >= 8 &&
8584 tpacpi_is_fw_digit(s[0]) &&
8585 tpacpi_is_fw_digit(s[1]) &&
8586 s[2] == t && s[3] == 'T' &&
8587 tpacpi_is_fw_digit(s[4]) &&
8588 tpacpi_is_fw_digit(s[5]);
8591 /* returns 0 - probe ok, or < 0 - probe error.
8592 * Probe ok doesn't mean thinkpad found.
8593 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8594 static int __must_check __init get_thinkpad_model_data(
8595 struct thinkpad_id_data *tp)
8597 const struct dmi_device *dev = NULL;
8598 char ec_fw_string[18];
8604 memset(tp, 0, sizeof(*tp));
8606 if (dmi_name_in_vendors("IBM"))
8607 tp->vendor = PCI_VENDOR_ID_IBM;
8608 else if (dmi_name_in_vendors("LENOVO"))
8609 tp->vendor = PCI_VENDOR_ID_LENOVO;
8613 s = dmi_get_system_info(DMI_BIOS_VERSION);
8614 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8615 if (s && !tp->bios_version_str)
8618 /* Really ancient ThinkPad 240X will fail this, which is fine */
8619 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8622 tp->bios_model = tp->bios_version_str[0]
8623 | (tp->bios_version_str[1] << 8);
8624 tp->bios_release = (tp->bios_version_str[4] << 8)
8625 | tp->bios_version_str[5];
8628 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8629 * X32 or newer, all Z series; Some models must have an
8630 * up-to-date BIOS or they will not be detected.
8632 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8634 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8635 if (sscanf(dev->name,
8636 "IBM ThinkPad Embedded Controller -[%17c",
8637 ec_fw_string) == 1) {
8638 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8639 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8641 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8642 if (!tp->ec_version_str)
8645 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8646 tp->ec_model = ec_fw_string[0]
8647 | (ec_fw_string[1] << 8);
8648 tp->ec_release = (ec_fw_string[4] << 8)
8651 pr_notice("ThinkPad firmware release %s "
8652 "doesn't match the known patterns\n",
8654 pr_notice("please report this to %s\n",
8661 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8662 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
8663 tp->model_str = kstrdup(s, GFP_KERNEL);
8668 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8669 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8670 if (s && !tp->nummodel_str)
8676 static int __init probe_for_thinkpad(void)
8683 /* It would be dangerous to run the driver in this case */
8684 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8688 * Non-ancient models have better DMI tagging, but very old models
8689 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8691 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8692 (thinkpad_id.ec_model != 0) ||
8693 tpacpi_is_fw_known();
8695 /* The EC handler is required */
8696 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8699 pr_err("Not yet supported ThinkPad detected!\n");
8703 if (!is_thinkpad && !force_load)
8709 static void __init thinkpad_acpi_init_banner(void)
8711 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8712 pr_info("%s\n", TPACPI_URL);
8714 pr_info("ThinkPad BIOS %s, EC %s\n",
8715 (thinkpad_id.bios_version_str) ?
8716 thinkpad_id.bios_version_str : "unknown",
8717 (thinkpad_id.ec_version_str) ?
8718 thinkpad_id.ec_version_str : "unknown");
8720 BUG_ON(!thinkpad_id.vendor);
8722 if (thinkpad_id.model_str)
8723 pr_info("%s %s, model %s\n",
8724 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8725 "IBM" : ((thinkpad_id.vendor ==
8726 PCI_VENDOR_ID_LENOVO) ?
8727 "Lenovo" : "Unknown vendor"),
8728 thinkpad_id.model_str,
8729 (thinkpad_id.nummodel_str) ?
8730 thinkpad_id.nummodel_str : "unknown");
8733 /* Module init, exit, parameters */
8735 static struct ibm_init_struct ibms_init[] __initdata = {
8737 .data = &thinkpad_acpi_driver_data,
8740 .init = hotkey_init,
8741 .data = &hotkey_driver_data,
8744 .init = bluetooth_init,
8745 .data = &bluetooth_driver_data,
8749 .data = &wan_driver_data,
8753 .data = &uwb_driver_data,
8755 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8758 .base_procfs_mode = S_IRUSR,
8759 .data = &video_driver_data,
8764 .data = &light_driver_data,
8768 .data = &cmos_driver_data,
8772 .data = &led_driver_data,
8776 .data = &beep_driver_data,
8779 .init = thermal_init,
8780 .data = &thermal_driver_data,
8783 .init = brightness_init,
8784 .data = &brightness_driver_data,
8787 .init = volume_init,
8788 .data = &volume_driver_data,
8792 .data = &fan_driver_data,
8796 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8799 struct ibm_struct *ibm;
8801 if (!kp || !kp->name || !val)
8804 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8805 ibm = ibms_init[i].data;
8806 WARN_ON(ibm == NULL);
8808 if (!ibm || !ibm->name)
8811 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8812 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8814 strcpy(ibms_init[i].param, val);
8815 strcat(ibms_init[i].param, ",");
8823 module_param(experimental, int, 0444);
8824 MODULE_PARM_DESC(experimental,
8825 "Enables experimental features when non-zero");
8827 module_param_named(debug, dbg_level, uint, 0);
8828 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8830 module_param(force_load, bool, 0444);
8831 MODULE_PARM_DESC(force_load,
8832 "Attempts to load the driver even on a "
8833 "mis-identified ThinkPad when true");
8835 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8836 MODULE_PARM_DESC(fan_control,
8837 "Enables setting fan parameters features when true");
8839 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8840 MODULE_PARM_DESC(brightness_mode,
8841 "Selects brightness control strategy: "
8842 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8844 module_param(brightness_enable, uint, 0444);
8845 MODULE_PARM_DESC(brightness_enable,
8846 "Enables backlight control when 1, disables when 0");
8848 module_param(hotkey_report_mode, uint, 0444);
8849 MODULE_PARM_DESC(hotkey_report_mode,
8850 "used for backwards compatibility with userspace, "
8851 "see documentation");
8853 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8854 module_param_named(volume_mode, volume_mode, uint, 0444);
8855 MODULE_PARM_DESC(volume_mode,
8856 "Selects volume control strategy: "
8857 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8859 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8860 MODULE_PARM_DESC(volume_capabilities,
8861 "Selects the mixer capabilites: "
8862 "0=auto, 1=volume and mute, 2=mute only");
8864 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8865 MODULE_PARM_DESC(volume_control,
8866 "Enables software override for the console audio "
8867 "control when true");
8869 /* ALSA module API parameters */
8870 module_param_named(index, alsa_index, int, 0444);
8871 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8872 module_param_named(id, alsa_id, charp, 0444);
8873 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8874 module_param_named(enable, alsa_enable, bool, 0444);
8875 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8876 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8878 #define TPACPI_PARAM(feature) \
8879 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8880 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8881 "at module load, see documentation")
8883 TPACPI_PARAM(hotkey);
8884 TPACPI_PARAM(bluetooth);
8885 TPACPI_PARAM(video);
8886 TPACPI_PARAM(light);
8890 TPACPI_PARAM(brightness);
8891 TPACPI_PARAM(volume);
8894 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8895 module_param(dbg_wlswemul, uint, 0444);
8896 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8897 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8898 MODULE_PARM_DESC(wlsw_state,
8899 "Initial state of the emulated WLSW switch");
8901 module_param(dbg_bluetoothemul, uint, 0444);
8902 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8903 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8904 MODULE_PARM_DESC(bluetooth_state,
8905 "Initial state of the emulated bluetooth switch");
8907 module_param(dbg_wwanemul, uint, 0444);
8908 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8909 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8910 MODULE_PARM_DESC(wwan_state,
8911 "Initial state of the emulated WWAN switch");
8913 module_param(dbg_uwbemul, uint, 0444);
8914 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8915 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8916 MODULE_PARM_DESC(uwb_state,
8917 "Initial state of the emulated UWB switch");
8920 static void thinkpad_acpi_module_exit(void)
8922 struct ibm_struct *ibm, *itmp;
8924 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8926 list_for_each_entry_safe_reverse(ibm, itmp,
8927 &tpacpi_all_drivers,
8932 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8934 if (tpacpi_inputdev) {
8935 if (tp_features.input_device_registered)
8936 input_unregister_device(tpacpi_inputdev);
8938 input_free_device(tpacpi_inputdev);
8942 hwmon_device_unregister(tpacpi_hwmon);
8944 if (tp_features.sensors_pdev_attrs_registered)
8945 device_remove_file(&tpacpi_sensors_pdev->dev,
8946 &dev_attr_thinkpad_acpi_pdev_name);
8947 if (tpacpi_sensors_pdev)
8948 platform_device_unregister(tpacpi_sensors_pdev);
8950 platform_device_unregister(tpacpi_pdev);
8952 if (tp_features.sensors_pdrv_attrs_registered)
8953 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8954 if (tp_features.platform_drv_attrs_registered)
8955 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8957 if (tp_features.sensors_pdrv_registered)
8958 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8960 if (tp_features.platform_drv_registered)
8961 platform_driver_unregister(&tpacpi_pdriver);
8964 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8967 destroy_workqueue(tpacpi_wq);
8969 kfree(thinkpad_id.bios_version_str);
8970 kfree(thinkpad_id.ec_version_str);
8971 kfree(thinkpad_id.model_str);
8975 static int __init thinkpad_acpi_module_init(void)
8979 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8981 /* Parameter checking */
8982 if (hotkey_report_mode > 2)
8985 /* Driver-level probe */
8987 ret = get_thinkpad_model_data(&thinkpad_id);
8989 pr_err("unable to get DMI data: %d\n", ret);
8990 thinkpad_acpi_module_exit();
8993 ret = probe_for_thinkpad();
8995 thinkpad_acpi_module_exit();
8999 /* Driver initialization */
9001 thinkpad_acpi_init_banner();
9002 tpacpi_check_outdated_fw();
9004 TPACPI_ACPIHANDLE_INIT(ecrd);
9005 TPACPI_ACPIHANDLE_INIT(ecwr);
9007 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9009 thinkpad_acpi_module_exit();
9013 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9015 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9016 thinkpad_acpi_module_exit();
9020 ret = platform_driver_register(&tpacpi_pdriver);
9022 pr_err("unable to register main platform driver\n");
9023 thinkpad_acpi_module_exit();
9026 tp_features.platform_drv_registered = 1;
9028 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9030 pr_err("unable to register hwmon platform driver\n");
9031 thinkpad_acpi_module_exit();
9034 tp_features.sensors_pdrv_registered = 1;
9036 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9038 tp_features.platform_drv_attrs_registered = 1;
9039 ret = tpacpi_create_driver_attributes(
9040 &tpacpi_hwmon_pdriver.driver);
9043 pr_err("unable to create sysfs driver attributes\n");
9044 thinkpad_acpi_module_exit();
9047 tp_features.sensors_pdrv_attrs_registered = 1;
9050 /* Device initialization */
9051 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9053 if (IS_ERR(tpacpi_pdev)) {
9054 ret = PTR_ERR(tpacpi_pdev);
9056 pr_err("unable to register platform device\n");
9057 thinkpad_acpi_module_exit();
9060 tpacpi_sensors_pdev = platform_device_register_simple(
9061 TPACPI_HWMON_DRVR_NAME,
9063 if (IS_ERR(tpacpi_sensors_pdev)) {
9064 ret = PTR_ERR(tpacpi_sensors_pdev);
9065 tpacpi_sensors_pdev = NULL;
9066 pr_err("unable to register hwmon platform device\n");
9067 thinkpad_acpi_module_exit();
9070 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9071 &dev_attr_thinkpad_acpi_pdev_name);
9073 pr_err("unable to create sysfs hwmon device attributes\n");
9074 thinkpad_acpi_module_exit();
9077 tp_features.sensors_pdev_attrs_registered = 1;
9078 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9079 if (IS_ERR(tpacpi_hwmon)) {
9080 ret = PTR_ERR(tpacpi_hwmon);
9081 tpacpi_hwmon = NULL;
9082 pr_err("unable to register hwmon device\n");
9083 thinkpad_acpi_module_exit();
9086 mutex_init(&tpacpi_inputdev_send_mutex);
9087 tpacpi_inputdev = input_allocate_device();
9088 if (!tpacpi_inputdev) {
9089 pr_err("unable to allocate input device\n");
9090 thinkpad_acpi_module_exit();
9093 /* Prepare input device, but don't register */
9094 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9095 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9096 tpacpi_inputdev->id.bustype = BUS_HOST;
9097 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9098 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9099 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9100 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9103 /* Init subdriver dependencies */
9104 tpacpi_detect_brightness_capabilities();
9106 /* Init subdrivers */
9107 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9108 ret = ibm_init(&ibms_init[i]);
9109 if (ret >= 0 && *ibms_init[i].param)
9110 ret = ibms_init[i].data->write(ibms_init[i].param);
9112 thinkpad_acpi_module_exit();
9117 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9119 ret = input_register_device(tpacpi_inputdev);
9121 pr_err("unable to register input device\n");
9122 thinkpad_acpi_module_exit();
9125 tp_features.input_device_registered = 1;
9131 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9134 * This will autoload the driver in almost every ThinkPad
9135 * in widespread use.
9137 * Only _VERY_ old models, like the 240, 240x and 570 lack
9138 * the HKEY event interface.
9140 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9143 * DMI matching for module autoloading
9145 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9146 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9148 * Only models listed in thinkwiki will be supported, so add yours
9149 * if it is not there yet.
9151 #define IBM_BIOS_MODULE_ALIAS(__type) \
9152 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9154 /* Ancient thinkpad BIOSes have to be identified by
9155 * BIOS type or model number, and there are far less
9156 * BIOS types than model numbers... */
9157 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9159 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9160 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9161 MODULE_DESCRIPTION(TPACPI_DESC);
9162 MODULE_VERSION(TPACPI_VERSION);
9163 MODULE_LICENSE("GPL");
9165 module_init(thinkpad_acpi_module_init);
9166 module_exit(thinkpad_acpi_module_exit);