2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.25"
27 #define TPACPI_SYSFS_VERSION 0x020700
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
70 #include <linux/platform_device.h>
71 #include <linux/hwmon.h>
72 #include <linux/hwmon-sysfs.h>
73 #include <linux/input.h>
74 #include <linux/leds.h>
75 #include <linux/rfkill.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
79 #include <linux/acpi.h>
80 #include <linux/pci_ids.h>
81 #include <linux/thinkpad_acpi.h>
82 #include <sound/core.h>
83 #include <sound/control.h>
84 #include <sound/initval.h>
85 #include <asm/uaccess.h>
87 /* ThinkPad CMOS commands */
88 #define TP_CMOS_VOLUME_DOWN 0
89 #define TP_CMOS_VOLUME_UP 1
90 #define TP_CMOS_VOLUME_MUTE 2
91 #define TP_CMOS_BRIGHTNESS_UP 4
92 #define TP_CMOS_BRIGHTNESS_DOWN 5
93 #define TP_CMOS_THINKLIGHT_ON 12
94 #define TP_CMOS_THINKLIGHT_OFF 13
98 TP_NVRAM_ADDR_HK2 = 0x57,
99 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
100 TP_NVRAM_ADDR_VIDEO = 0x59,
101 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
102 TP_NVRAM_ADDR_MIXER = 0x60,
105 /* NVRAM bit masks */
107 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
108 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
109 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
110 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
111 TP_NVRAM_MASK_THINKLIGHT = 0x10,
112 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
113 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
114 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
115 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
116 TP_NVRAM_MASK_MUTE = 0x40,
117 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
118 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
119 TP_NVRAM_POS_LEVEL_VOLUME = 0,
122 /* Misc NVRAM-related */
124 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
128 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
129 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
130 #define TPACPI_ACPI_EC_HID "PNP0C09"
133 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
134 #define TPACPI_HKEY_INPUT_VERSION 0x4101
136 /* ACPI \WGSV commands */
138 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
139 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
140 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
141 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144 /* TP_ACPI_WGSV_GET_STATE bits */
146 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
147 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
148 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
149 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
150 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
151 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
152 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
153 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
154 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
155 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
159 enum tpacpi_hkey_event_t {
161 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
162 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
163 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
164 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
165 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
166 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
168 /* Reasons for waking up from S3/S4 */
169 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
170 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
171 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
172 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
173 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
174 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
176 /* Auto-sleep after eject request */
177 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
178 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
180 /* Misc bay events */
181 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
182 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
183 or port replicator */
184 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
185 dock or port replicator */
187 /* User-interface events */
188 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
189 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
190 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
191 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
192 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
193 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
194 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
196 /* Key-related user-interface events */
197 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
198 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
201 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
202 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
203 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
204 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
205 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
207 /* AC-related events */
208 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
211 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
214 /****************************************************************************
218 #define TPACPI_NAME "thinkpad"
219 #define TPACPI_DESC "ThinkPad ACPI Extras"
220 #define TPACPI_FILE TPACPI_NAME "_acpi"
221 #define TPACPI_URL "http://ibm-acpi.sf.net/"
222 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
224 #define TPACPI_PROC_DIR "ibm"
225 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
226 #define TPACPI_DRVR_NAME TPACPI_FILE
227 #define TPACPI_DRVR_SHORTNAME "tpacpi"
228 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
230 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
231 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
233 #define TPACPI_MAX_ACPI_ARGS 3
235 /* Debugging printk groups */
236 #define TPACPI_DBG_ALL 0xffff
237 #define TPACPI_DBG_DISCLOSETASK 0x8000
238 #define TPACPI_DBG_INIT 0x0001
239 #define TPACPI_DBG_EXIT 0x0002
240 #define TPACPI_DBG_RFKILL 0x0004
241 #define TPACPI_DBG_HKEY 0x0008
242 #define TPACPI_DBG_FAN 0x0010
243 #define TPACPI_DBG_BRGHT 0x0020
244 #define TPACPI_DBG_MIXER 0x0040
246 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
247 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
248 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
251 /****************************************************************************
252 * Driver-wide structs and misc. variables
257 struct tp_acpi_drv_struct {
258 const struct acpi_device_id *hid;
259 struct acpi_driver *driver;
261 void (*notify) (struct ibm_struct *, u32);
264 struct acpi_device *device;
270 int (*read) (struct seq_file *);
271 int (*write) (char *);
273 void (*resume) (void);
274 void (*suspend) (void);
275 void (*shutdown) (void);
277 struct list_head all_drivers;
279 struct tp_acpi_drv_struct *acpi;
282 u8 acpi_driver_registered:1;
283 u8 acpi_notify_installed:1;
290 struct ibm_init_struct {
293 int (*init) (struct ibm_init_struct *);
294 umode_t base_procfs_mode;
295 struct ibm_struct *data;
306 u32 bright_acpimode:1;
310 u32 fan_ctrl_status_undef:1;
312 u32 beep_needs_two_args:1;
313 u32 mixer_no_level_control:1;
314 u32 input_device_registered:1;
315 u32 platform_drv_registered:1;
316 u32 platform_drv_attrs_registered:1;
317 u32 sensors_pdrv_registered:1;
318 u32 sensors_pdrv_attrs_registered:1;
319 u32 sensors_pdev_attrs_registered:1;
320 u32 hotkey_poll_active:1;
324 u16 hotkey_mask_ff:1;
325 u16 volume_ctrl_forbidden:1;
328 struct thinkpad_id_data {
329 unsigned int vendor; /* ThinkPad vendor:
330 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
332 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
333 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
335 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
337 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 char *model_str; /* ThinkPad T43 */
341 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
343 static struct thinkpad_id_data thinkpad_id;
346 TPACPI_LIFE_INIT = 0,
351 static int experimental;
352 static u32 dbg_level;
354 static struct workqueue_struct *tpacpi_wq;
362 /* Special LED class that can defer work */
363 struct tpacpi_led_classdev {
364 struct led_classdev led_classdev;
365 struct work_struct work;
366 enum led_status_t new_state;
370 /* brightness level capabilities */
371 static unsigned int bright_maxlvl; /* 0 = unknown */
373 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
374 static int dbg_wlswemul;
375 static bool tpacpi_wlsw_emulstate;
376 static int dbg_bluetoothemul;
377 static bool tpacpi_bluetooth_emulstate;
378 static int dbg_wwanemul;
379 static bool tpacpi_wwan_emulstate;
380 static int dbg_uwbemul;
381 static bool tpacpi_uwb_emulstate;
385 /*************************************************************************
389 #define dbg_printk(a_dbg_level, format, arg...) \
391 if (dbg_level & (a_dbg_level)) \
392 printk(KERN_DEBUG pr_fmt("%s: " format), \
396 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
397 #define vdbg_printk dbg_printk
398 static const char *str_supported(int is_supported);
400 static inline const char *str_supported(int is_supported) { return ""; }
401 #define vdbg_printk(a_dbg_level, format, arg...) \
402 no_printk(format, ##arg)
405 static void tpacpi_log_usertask(const char * const what)
407 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
408 what, task_tgid_vnr(current));
411 #define tpacpi_disclose_usertask(what, format, arg...) \
413 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
416 what, task_tgid_vnr(current), ## arg); \
421 * Quirk handling helpers
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
426 * We use values well outside that range as specials.
429 #define TPACPI_MATCH_ANY 0xffffU
430 #define TPACPI_MATCH_UNKNOWN 0U
432 /* TPID('1', 'Y') == 0x5931 */
433 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
435 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
453 struct tpacpi_quirk {
457 unsigned long quirks;
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
471 * The match criteria is: vendor, ec and bios much match.
473 static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
492 static inline bool __pure __init tpacpi_is_lenovo(void)
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497 static inline bool __pure __init tpacpi_is_ibm(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502 /****************************************************************************
503 ****************************************************************************
505 * ACPI Helpers and device model
507 ****************************************************************************
508 ****************************************************************************/
510 /*************************************************************************
514 static acpi_handle root_handle;
515 static acpi_handle ec_handle;
517 #define TPACPI_HANDLE(object, parent, paths...) \
518 static acpi_handle object##_handle; \
519 static const acpi_handle * const object##_parent __initconst = \
521 static char *object##_paths[] __initdata = { paths }
523 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
526 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
532 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
537 /*************************************************************************
541 static int acpi_evalf(acpi_handle handle,
542 int *res, char *method, char *fmt, ...)
545 struct acpi_object_list params;
546 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
547 struct acpi_buffer result, *resultp;
548 union acpi_object out_obj;
556 pr_err("acpi_evalf() called with empty format\n");
569 params.pointer = &in_objs[0];
576 in_objs[params.count].integer.value = va_arg(ap, int);
577 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
579 /* add more types as needed */
581 pr_err("acpi_evalf() called "
582 "with invalid format character '%c'\n", c);
589 if (res_type != 'v') {
590 result.length = sizeof(out_obj);
591 result.pointer = &out_obj;
596 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
600 success = (status == AE_OK &&
601 out_obj.type == ACPI_TYPE_INTEGER);
603 *res = out_obj.integer.value;
606 success = status == AE_OK;
608 /* add more types as needed */
610 pr_err("acpi_evalf() called "
611 "with invalid format character '%c'\n", res_type);
615 if (!success && !quiet)
616 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
617 method, fmt0, acpi_format_exception(status));
622 static int acpi_ec_read(int i, u8 *p)
627 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
631 if (ec_read(i, p) < 0)
638 static int acpi_ec_write(int i, u8 v)
641 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
644 if (ec_write(i, v) < 0)
651 static int issue_thinkpad_cmos_command(int cmos_cmd)
656 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
662 /*************************************************************************
666 #define TPACPI_ACPIHANDLE_INIT(object) \
667 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
668 object##_paths, ARRAY_SIZE(object##_paths))
670 static void __init drv_acpi_handle_init(const char *name,
671 acpi_handle *handle, const acpi_handle parent,
672 char **paths, const int num_paths)
677 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
680 for (i = 0; i < num_paths; i++) {
681 status = acpi_get_handle(parent, paths[i], handle);
682 if (ACPI_SUCCESS(status)) {
683 dbg_printk(TPACPI_DBG_INIT,
684 "Found ACPI handle %s for %s\n",
690 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
695 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
696 u32 level, void *context, void **return_value)
698 struct acpi_device *dev;
699 if (!strcmp(context, "video")) {
700 if (acpi_bus_get_device(handle, &dev))
702 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
706 *(acpi_handle *)return_value = handle;
708 return AE_CTRL_TERMINATE;
711 static void __init tpacpi_acpi_handle_locate(const char *name,
716 acpi_handle device_found;
718 BUG_ON(!name || !handle);
719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
721 name, hid ? hid : "NULL");
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
740 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
742 struct ibm_struct *ibm = data;
744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
750 ibm->acpi->notify(ibm, event);
753 static int __init setup_acpi_notify(struct ibm_struct *ibm)
760 if (!*ibm->acpi->handle)
763 vdbg_printk(TPACPI_DBG_INIT,
764 "setting up ACPI notify for %s\n", ibm->name);
766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
768 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
772 ibm->acpi->device->driver_data = ibm;
773 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
774 TPACPI_ACPI_EVENT_PREFIX,
777 status = acpi_install_notify_handler(*ibm->acpi->handle,
778 ibm->acpi->type, dispatch_acpi_notify, ibm);
779 if (ACPI_FAILURE(status)) {
780 if (status == AE_ALREADY_EXISTS) {
781 pr_notice("another device driver is already "
782 "handling %s events\n", ibm->name);
784 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
785 ibm->name, acpi_format_exception(status));
789 ibm->flags.acpi_notify_installed = 1;
793 static int __init tpacpi_device_add(struct acpi_device *device)
798 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
802 dbg_printk(TPACPI_DBG_INIT,
803 "registering %s as an ACPI driver\n", ibm->name);
807 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
808 if (!ibm->acpi->driver) {
809 pr_err("failed to allocate memory for ibm->acpi->driver\n");
813 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
814 ibm->acpi->driver->ids = ibm->acpi->hid;
816 ibm->acpi->driver->ops.add = &tpacpi_device_add;
818 rc = acpi_bus_register_driver(ibm->acpi->driver);
820 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
822 kfree(ibm->acpi->driver);
823 ibm->acpi->driver = NULL;
825 ibm->flags.acpi_driver_registered = 1;
831 /****************************************************************************
832 ****************************************************************************
836 ****************************************************************************
837 ****************************************************************************/
839 static int dispatch_proc_show(struct seq_file *m, void *v)
841 struct ibm_struct *ibm = m->private;
843 if (!ibm || !ibm->read)
848 static int dispatch_proc_open(struct inode *inode, struct file *file)
850 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
853 static ssize_t dispatch_proc_write(struct file *file,
854 const char __user *userbuf,
855 size_t count, loff_t *pos)
857 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
861 if (!ibm || !ibm->write)
863 if (count > PAGE_SIZE - 2)
866 kernbuf = kmalloc(count + 2, GFP_KERNEL);
870 if (copy_from_user(kernbuf, userbuf, count)) {
876 strcat(kernbuf, ",");
877 ret = ibm->write(kernbuf);
886 static const struct file_operations dispatch_proc_fops = {
887 .owner = THIS_MODULE,
888 .open = dispatch_proc_open,
891 .release = single_release,
892 .write = dispatch_proc_write,
895 static char *next_cmd(char **cmds)
900 while ((end = strchr(start, ',')) && end == start)
912 /****************************************************************************
913 ****************************************************************************
915 * Device model: input, hwmon and platform
917 ****************************************************************************
918 ****************************************************************************/
920 static struct platform_device *tpacpi_pdev;
921 static struct platform_device *tpacpi_sensors_pdev;
922 static struct device *tpacpi_hwmon;
923 static struct input_dev *tpacpi_inputdev;
924 static struct mutex tpacpi_inputdev_send_mutex;
925 static LIST_HEAD(tpacpi_all_drivers);
927 #ifdef CONFIG_PM_SLEEP
928 static int tpacpi_suspend_handler(struct device *dev)
930 struct ibm_struct *ibm, *itmp;
932 list_for_each_entry_safe(ibm, itmp,
942 static int tpacpi_resume_handler(struct device *dev)
944 struct ibm_struct *ibm, *itmp;
946 list_for_each_entry_safe(ibm, itmp,
957 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
958 tpacpi_suspend_handler, tpacpi_resume_handler);
960 static void tpacpi_shutdown_handler(struct platform_device *pdev)
962 struct ibm_struct *ibm, *itmp;
964 list_for_each_entry_safe(ibm, itmp,
972 static struct platform_driver tpacpi_pdriver = {
974 .name = TPACPI_DRVR_NAME,
975 .owner = THIS_MODULE,
978 .shutdown = tpacpi_shutdown_handler,
981 static struct platform_driver tpacpi_hwmon_pdriver = {
983 .name = TPACPI_HWMON_DRVR_NAME,
984 .owner = THIS_MODULE,
988 /*************************************************************************
989 * sysfs support helpers
992 struct attribute_set {
993 unsigned int members, max_members;
994 struct attribute_group group;
997 struct attribute_set_obj {
998 struct attribute_set s;
1000 } __attribute__((packed));
1002 static struct attribute_set *create_attr_set(unsigned int max_members,
1005 struct attribute_set_obj *sobj;
1007 if (max_members == 0)
1010 /* Allocates space for implicit NULL at the end too */
1011 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1012 max_members * sizeof(struct attribute *),
1016 sobj->s.max_members = max_members;
1017 sobj->s.group.attrs = &sobj->a;
1018 sobj->s.group.name = name;
1023 #define destroy_attr_set(_set) \
1026 /* not multi-threaded safe, use it in a single thread per set */
1027 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1032 if (s->members >= s->max_members)
1035 s->group.attrs[s->members] = attr;
1041 static int add_many_to_attr_set(struct attribute_set *s,
1042 struct attribute **attr,
1047 for (i = 0; i < count; i++) {
1048 res = add_to_attr_set(s, attr[i]);
1056 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1058 sysfs_remove_group(kobj, &s->group);
1059 destroy_attr_set(s);
1062 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1063 sysfs_create_group(_kobj, &_attr_set->group)
1065 static int parse_strtoul(const char *buf,
1066 unsigned long max, unsigned long *value)
1070 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1071 endp = skip_spaces(endp);
1072 if (*endp || *value > max)
1078 static void tpacpi_disable_brightness_delay(void)
1080 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1081 pr_notice("ACPI backlight control delay disabled\n");
1084 static void printk_deprecated_attribute(const char * const what,
1085 const char * const details)
1087 tpacpi_log_usertask("deprecated sysfs attribute");
1088 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1089 "will be removed. %s\n",
1093 /*************************************************************************
1094 * rfkill and radio control support helpers
1098 * ThinkPad-ACPI firmware handling model:
1100 * WLSW (master wireless switch) is event-driven, and is common to all
1101 * firmware-controlled radios. It cannot be controlled, just monitored,
1102 * as expected. It overrides all radio state in firmware
1104 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1105 * (TODO: verify how WLSW interacts with the returned radio state).
1107 * The only time there are shadow radio state changes, is when
1108 * masked-off hotkeys are used.
1112 * Internal driver API for radio state:
1114 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1115 * bool: true means radio blocked (off)
1117 enum tpacpi_rfkill_state {
1118 TPACPI_RFK_RADIO_OFF = 0,
1122 /* rfkill switches */
1123 enum tpacpi_rfk_id {
1124 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1125 TPACPI_RFK_WWAN_SW_ID,
1126 TPACPI_RFK_UWB_SW_ID,
1130 static const char *tpacpi_rfkill_names[] = {
1131 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1132 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1133 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1134 [TPACPI_RFK_SW_MAX] = NULL
1137 /* ThinkPad-ACPI rfkill subdriver */
1139 struct rfkill *rfkill;
1140 enum tpacpi_rfk_id id;
1141 const struct tpacpi_rfk_ops *ops;
1144 struct tpacpi_rfk_ops {
1145 /* firmware interface */
1146 int (*get_status)(void);
1147 int (*set_status)(const enum tpacpi_rfkill_state);
1150 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1152 /* Query FW and update rfkill sw state for a given rfkill switch */
1153 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1160 status = (tp_rfk->ops->get_status)();
1164 rfkill_set_sw_state(tp_rfk->rfkill,
1165 (status == TPACPI_RFK_RADIO_OFF));
1170 /* Query FW and update rfkill sw state for all rfkill switches */
1171 static void tpacpi_rfk_update_swstate_all(void)
1175 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1176 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1180 * Sync the HW-blocking state of all rfkill switches,
1181 * do notice it causes the rfkill core to schedule uevents
1183 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1186 struct tpacpi_rfk *tp_rfk;
1188 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1189 tp_rfk = tpacpi_rfkill_switches[i];
1191 if (rfkill_set_hw_state(tp_rfk->rfkill,
1193 /* ignore -- we track sw block */
1199 /* Call to get the WLSW state from the firmware */
1200 static int hotkey_get_wlsw(void);
1202 /* Call to query WLSW state and update all rfkill switches */
1203 static bool tpacpi_rfk_check_hwblock_state(void)
1205 int res = hotkey_get_wlsw();
1208 /* When unknown or unsupported, we have to assume it is unblocked */
1212 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1213 tpacpi_rfk_update_hwblock_state(hw_blocked);
1218 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1220 struct tpacpi_rfk *tp_rfk = data;
1223 dbg_printk(TPACPI_DBG_RFKILL,
1224 "request to change radio state to %s\n",
1225 blocked ? "blocked" : "unblocked");
1227 /* try to set radio state */
1228 res = (tp_rfk->ops->set_status)(blocked ?
1229 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1231 /* and update the rfkill core with whatever the FW really did */
1232 tpacpi_rfk_update_swstate(tp_rfk);
1234 return (res < 0) ? res : 0;
1237 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1238 .set_block = tpacpi_rfk_hook_set_block,
1241 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1242 const struct tpacpi_rfk_ops *tp_rfkops,
1243 const enum rfkill_type rfktype,
1245 const bool set_default)
1247 struct tpacpi_rfk *atp_rfk;
1249 bool sw_state = false;
1253 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1255 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1257 atp_rfk->rfkill = rfkill_alloc(name,
1260 &tpacpi_rfk_rfkill_ops,
1262 if (!atp_rfk || !atp_rfk->rfkill) {
1263 pr_err("failed to allocate memory for rfkill class\n");
1269 atp_rfk->ops = tp_rfkops;
1271 sw_status = (tp_rfkops->get_status)();
1272 if (sw_status < 0) {
1273 pr_err("failed to read initial state for %s, error %d\n",
1276 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1278 /* try to keep the initial state, since we ask the
1279 * firmware to preserve it across S5 in NVRAM */
1280 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1283 hw_state = tpacpi_rfk_check_hwblock_state();
1284 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1286 res = rfkill_register(atp_rfk->rfkill);
1288 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1289 rfkill_destroy(atp_rfk->rfkill);
1294 tpacpi_rfkill_switches[id] = atp_rfk;
1296 pr_info("rfkill switch %s: radio is %sblocked\n",
1297 name, (sw_state || hw_state) ? "" : "un");
1301 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1303 struct tpacpi_rfk *tp_rfk;
1305 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1307 tp_rfk = tpacpi_rfkill_switches[id];
1309 rfkill_unregister(tp_rfk->rfkill);
1310 rfkill_destroy(tp_rfk->rfkill);
1311 tpacpi_rfkill_switches[id] = NULL;
1316 static void printk_deprecated_rfkill_attribute(const char * const what)
1318 printk_deprecated_attribute(what,
1319 "Please switch to generic rfkill before year 2010");
1322 /* sysfs <radio> enable ------------------------------------------------ */
1323 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1324 struct device_attribute *attr,
1329 printk_deprecated_rfkill_attribute(attr->attr.name);
1331 /* This is in the ABI... */
1332 if (tpacpi_rfk_check_hwblock_state()) {
1333 status = TPACPI_RFK_RADIO_OFF;
1335 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1340 return snprintf(buf, PAGE_SIZE, "%d\n",
1341 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1344 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1345 struct device_attribute *attr,
1346 const char *buf, size_t count)
1351 printk_deprecated_rfkill_attribute(attr->attr.name);
1353 if (parse_strtoul(buf, 1, &t))
1356 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1358 /* This is in the ABI... */
1359 if (tpacpi_rfk_check_hwblock_state() && !!t)
1362 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1363 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1364 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1366 return (res < 0) ? res : count;
1369 /* procfs -------------------------------------------------------------- */
1370 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1372 if (id >= TPACPI_RFK_SW_MAX)
1373 seq_printf(m, "status:\t\tnot supported\n");
1377 /* This is in the ABI... */
1378 if (tpacpi_rfk_check_hwblock_state()) {
1379 status = TPACPI_RFK_RADIO_OFF;
1381 status = tpacpi_rfk_update_swstate(
1382 tpacpi_rfkill_switches[id]);
1387 seq_printf(m, "status:\t\t%s\n",
1388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enabled" : "disabled");
1390 seq_printf(m, "commands:\tenable, disable\n");
1396 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1402 if (id >= TPACPI_RFK_SW_MAX)
1405 while ((cmd = next_cmd(&buf))) {
1406 if (strlencmp(cmd, "enable") == 0)
1407 status = TPACPI_RFK_RADIO_ON;
1408 else if (strlencmp(cmd, "disable") == 0)
1409 status = TPACPI_RFK_RADIO_OFF;
1415 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1416 (status == TPACPI_RFK_RADIO_ON) ?
1417 "enable" : "disable",
1418 tpacpi_rfkill_names[id]);
1419 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1420 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1426 /*************************************************************************
1427 * thinkpad-acpi driver attributes
1430 /* interface_version --------------------------------------------------- */
1431 static ssize_t tpacpi_driver_interface_version_show(
1432 struct device_driver *drv,
1435 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1438 static DRIVER_ATTR(interface_version, S_IRUGO,
1439 tpacpi_driver_interface_version_show, NULL);
1441 /* debug_level --------------------------------------------------------- */
1442 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1445 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1448 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1449 const char *buf, size_t count)
1453 if (parse_strtoul(buf, 0xffff, &t))
1461 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1462 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1464 /* version ------------------------------------------------------------- */
1465 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1468 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1469 TPACPI_DESC, TPACPI_VERSION);
1472 static DRIVER_ATTR(version, S_IRUGO,
1473 tpacpi_driver_version_show, NULL);
1475 /* --------------------------------------------------------------------- */
1477 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1479 /* wlsw_emulstate ------------------------------------------------------ */
1480 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1486 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1487 const char *buf, size_t count)
1491 if (parse_strtoul(buf, 1, &t))
1494 if (tpacpi_wlsw_emulstate != !!t) {
1495 tpacpi_wlsw_emulstate = !!t;
1496 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1502 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1503 tpacpi_driver_wlsw_emulstate_show,
1504 tpacpi_driver_wlsw_emulstate_store);
1506 /* bluetooth_emulstate ------------------------------------------------- */
1507 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1508 struct device_driver *drv,
1511 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1514 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1515 struct device_driver *drv,
1516 const char *buf, size_t count)
1520 if (parse_strtoul(buf, 1, &t))
1523 tpacpi_bluetooth_emulstate = !!t;
1528 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1529 tpacpi_driver_bluetooth_emulstate_show,
1530 tpacpi_driver_bluetooth_emulstate_store);
1532 /* wwan_emulstate ------------------------------------------------- */
1533 static ssize_t tpacpi_driver_wwan_emulstate_show(
1534 struct device_driver *drv,
1537 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1540 static ssize_t tpacpi_driver_wwan_emulstate_store(
1541 struct device_driver *drv,
1542 const char *buf, size_t count)
1546 if (parse_strtoul(buf, 1, &t))
1549 tpacpi_wwan_emulstate = !!t;
1554 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1555 tpacpi_driver_wwan_emulstate_show,
1556 tpacpi_driver_wwan_emulstate_store);
1558 /* uwb_emulstate ------------------------------------------------- */
1559 static ssize_t tpacpi_driver_uwb_emulstate_show(
1560 struct device_driver *drv,
1563 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1566 static ssize_t tpacpi_driver_uwb_emulstate_store(
1567 struct device_driver *drv,
1568 const char *buf, size_t count)
1572 if (parse_strtoul(buf, 1, &t))
1575 tpacpi_uwb_emulstate = !!t;
1580 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1581 tpacpi_driver_uwb_emulstate_show,
1582 tpacpi_driver_uwb_emulstate_store);
1585 /* --------------------------------------------------------------------- */
1587 static struct driver_attribute *tpacpi_driver_attributes[] = {
1588 &driver_attr_debug_level, &driver_attr_version,
1589 &driver_attr_interface_version,
1592 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1598 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1599 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1603 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1604 if (!res && dbg_wlswemul)
1605 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1606 if (!res && dbg_bluetoothemul)
1607 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1608 if (!res && dbg_wwanemul)
1609 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1610 if (!res && dbg_uwbemul)
1611 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1617 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1621 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1622 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1624 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1625 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1626 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1627 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1628 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1632 /*************************************************************************
1637 * Table of recommended minimum BIOS versions
1639 * Reasons for listing:
1640 * 1. Stable BIOS, listed because the unknown amount of
1641 * bugs and bad ACPI behaviour on older versions
1643 * 2. BIOS or EC fw with known bugs that trigger on Linux
1645 * 3. BIOS with known reduced functionality in older versions
1647 * We recommend the latest BIOS and EC version.
1648 * We only support the latest BIOS and EC fw version as a rule.
1650 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1651 * Information from users in ThinkWiki
1653 * WARNING: we use this table also to detect that the machine is
1654 * a ThinkPad in some cases, so don't remove entries lightly.
1657 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1658 { .vendor = (__v), \
1659 .bios = TPID(__id1, __id2), \
1660 .ec = TPACPI_MATCH_ANY, \
1661 .quirks = TPACPI_MATCH_ANY << 16 \
1662 | (__bv1) << 8 | (__bv2) }
1664 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1665 __eid, __ev1, __ev2) \
1666 { .vendor = (__v), \
1667 .bios = TPID(__bid1, __bid2), \
1669 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1670 | (__bv1) << 8 | (__bv2) }
1672 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1673 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1675 /* Outdated IBM BIOSes often lack the EC id string */
1676 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1677 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1678 __bv1, __bv2, TPID(__id1, __id2), \
1680 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1681 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1684 /* Outdated IBM BIOSes often lack the EC id string */
1685 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1686 __eid1, __eid2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1688 __bv1, __bv2, TPID(__eid1, __eid2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1694 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1695 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1697 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1698 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1699 __bv1, __bv2, TPID(__id1, __id2), \
1702 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1703 __eid1, __eid2, __ev1, __ev2) \
1704 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1705 __bv1, __bv2, TPID(__eid1, __eid2), \
1708 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1709 /* Numeric models ------------------ */
1710 /* FW MODEL BIOS VERS */
1711 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1712 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1713 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1714 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1715 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1716 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1717 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1718 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1719 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1721 /* A-series ------------------------- */
1722 /* FW MODEL BIOS VERS EC VERS */
1723 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1724 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1725 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1726 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1727 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1728 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1729 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1730 TPV_QI0('1', '3', '2', '0'), /* A22m */
1731 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1732 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1733 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1735 /* G-series ------------------------- */
1736 /* FW MODEL BIOS VERS */
1737 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1738 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1740 /* R-series, T-series --------------- */
1741 /* FW MODEL BIOS VERS EC VERS */
1742 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1743 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1744 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1745 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1746 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1747 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1748 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1749 T40/p, T41/p, T42/p (1) */
1750 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1751 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1752 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1753 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1755 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1756 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1757 TPV_QI0('1', '6', '3', '2'), /* T22 */
1758 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1759 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1760 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1762 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1763 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1764 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1766 /* BIOS FW BIOS VERS EC FW EC VERS */
1767 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1768 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1770 /* X-series ------------------------- */
1771 /* FW MODEL BIOS VERS EC VERS */
1772 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1773 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1774 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1775 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1776 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1777 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1778 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1780 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1781 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1783 /* (0) - older versions lack DMI EC fw string and functionality */
1784 /* (1) - older versions known to lack functionality */
1795 static void __init tpacpi_check_outdated_fw(void)
1797 unsigned long fwvers;
1798 u16 ec_version, bios_version;
1800 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1801 ARRAY_SIZE(tpacpi_bios_version_qtable));
1806 bios_version = fwvers & 0xffffU;
1807 ec_version = (fwvers >> 16) & 0xffffU;
1809 /* note that unknown versions are set to 0x0000 and we use that */
1810 if ((bios_version > thinkpad_id.bios_release) ||
1811 (ec_version > thinkpad_id.ec_release &&
1812 ec_version != TPACPI_MATCH_ANY)) {
1814 * The changelogs would let us track down the exact
1815 * reason, but it is just too much of a pain to track
1816 * it. We only list BIOSes that are either really
1817 * broken, or really stable to begin with, so it is
1818 * best if the user upgrades the firmware anyway.
1820 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1821 pr_warn("WARNING: This firmware may be missing critical bug "
1822 "fixes and/or important features\n");
1826 static bool __init tpacpi_is_fw_known(void)
1828 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1829 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1832 /****************************************************************************
1833 ****************************************************************************
1837 ****************************************************************************
1838 ****************************************************************************/
1840 /*************************************************************************
1841 * thinkpad-acpi metadata subdriver
1844 static int thinkpad_acpi_driver_read(struct seq_file *m)
1846 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1847 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1851 static struct ibm_struct thinkpad_acpi_driver_data = {
1853 .read = thinkpad_acpi_driver_read,
1856 /*************************************************************************
1861 * ThinkPad firmware event model
1863 * The ThinkPad firmware has two main event interfaces: normal ACPI
1864 * notifications (which follow the ACPI standard), and a private event
1867 * The private event interface also issues events for the hotkeys. As
1868 * the driver gained features, the event handling code ended up being
1869 * built around the hotkey subdriver. This will need to be refactored
1870 * to a more formal event API eventually.
1872 * Some "hotkeys" are actually supposed to be used as event reports,
1873 * such as "brightness has changed", "volume has changed", depending on
1874 * the ThinkPad model and how the firmware is operating.
1876 * Unlike other classes, hotkey-class events have mask/unmask control on
1877 * non-ancient firmware. However, how it behaves changes a lot with the
1878 * firmware model and version.
1881 enum { /* hot key scan codes (derived from ACPI DSDT) */
1882 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1883 TP_ACPI_HOTKEYSCAN_FNF2,
1884 TP_ACPI_HOTKEYSCAN_FNF3,
1885 TP_ACPI_HOTKEYSCAN_FNF4,
1886 TP_ACPI_HOTKEYSCAN_FNF5,
1887 TP_ACPI_HOTKEYSCAN_FNF6,
1888 TP_ACPI_HOTKEYSCAN_FNF7,
1889 TP_ACPI_HOTKEYSCAN_FNF8,
1890 TP_ACPI_HOTKEYSCAN_FNF9,
1891 TP_ACPI_HOTKEYSCAN_FNF10,
1892 TP_ACPI_HOTKEYSCAN_FNF11,
1893 TP_ACPI_HOTKEYSCAN_FNF12,
1894 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1895 TP_ACPI_HOTKEYSCAN_FNINSERT,
1896 TP_ACPI_HOTKEYSCAN_FNDELETE,
1897 TP_ACPI_HOTKEYSCAN_FNHOME,
1898 TP_ACPI_HOTKEYSCAN_FNEND,
1899 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1900 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1901 TP_ACPI_HOTKEYSCAN_FNSPACE,
1902 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1903 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1904 TP_ACPI_HOTKEYSCAN_MUTE,
1905 TP_ACPI_HOTKEYSCAN_THINKPAD,
1906 TP_ACPI_HOTKEYSCAN_UNK1,
1907 TP_ACPI_HOTKEYSCAN_UNK2,
1908 TP_ACPI_HOTKEYSCAN_UNK3,
1909 TP_ACPI_HOTKEYSCAN_UNK4,
1910 TP_ACPI_HOTKEYSCAN_UNK5,
1911 TP_ACPI_HOTKEYSCAN_UNK6,
1912 TP_ACPI_HOTKEYSCAN_UNK7,
1913 TP_ACPI_HOTKEYSCAN_UNK8,
1915 /* Hotkey keymap size */
1916 TPACPI_HOTKEY_MAP_LEN
1919 enum { /* Keys/events available through NVRAM polling */
1920 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1921 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1924 enum { /* Positions of some of the keys in hotkey masks */
1925 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1926 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1927 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1928 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1929 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1930 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1931 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1932 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1933 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1934 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1935 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1938 enum { /* NVRAM to ACPI HKEY group map */
1939 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1940 TP_ACPI_HKEY_ZOOM_MASK |
1941 TP_ACPI_HKEY_DISPSWTCH_MASK |
1942 TP_ACPI_HKEY_HIBERNATE_MASK,
1943 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1944 TP_ACPI_HKEY_BRGHTDWN_MASK,
1945 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1946 TP_ACPI_HKEY_VOLDWN_MASK |
1947 TP_ACPI_HKEY_MUTE_MASK,
1950 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1951 struct tp_nvram_state {
1952 u16 thinkpad_toggle:1;
1954 u16 display_toggle:1;
1955 u16 thinklight_toggle:1;
1956 u16 hibernate_toggle:1;
1957 u16 displayexp_toggle:1;
1958 u16 display_state:1;
1959 u16 brightness_toggle:1;
1960 u16 volume_toggle:1;
1963 u8 brightness_level;
1967 /* kthread for the hotkey poller */
1968 static struct task_struct *tpacpi_hotkey_task;
1971 * Acquire mutex to write poller control variables as an
1974 * Increment hotkey_config_change when changing them if you
1975 * want the kthread to forget old state.
1977 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1979 static struct mutex hotkey_thread_data_mutex;
1980 static unsigned int hotkey_config_change;
1983 * hotkey poller control variables
1985 * Must be atomic or readers will also need to acquire mutex
1987 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1988 * should be used only when the changes need to be taken as
1989 * a block, OR when one needs to force the kthread to forget
1992 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1993 static unsigned int hotkey_poll_freq = 10; /* Hz */
1995 #define HOTKEY_CONFIG_CRITICAL_START \
1997 mutex_lock(&hotkey_thread_data_mutex); \
1998 hotkey_config_change++; \
2000 #define HOTKEY_CONFIG_CRITICAL_END \
2001 mutex_unlock(&hotkey_thread_data_mutex);
2003 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2005 #define hotkey_source_mask 0U
2006 #define HOTKEY_CONFIG_CRITICAL_START
2007 #define HOTKEY_CONFIG_CRITICAL_END
2009 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2011 static struct mutex hotkey_mutex;
2013 static enum { /* Reasons for waking up */
2014 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2015 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2016 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2017 } hotkey_wakeup_reason;
2019 static int hotkey_autosleep_ack;
2021 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2022 static u32 hotkey_all_mask; /* all events supported in fw */
2023 static u32 hotkey_reserved_mask; /* events better left disabled */
2024 static u32 hotkey_driver_mask; /* events needed by the driver */
2025 static u32 hotkey_user_mask; /* events visible to userspace */
2026 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2028 static u16 *hotkey_keycode_map;
2030 static struct attribute_set *hotkey_dev_attributes;
2032 static void tpacpi_driver_event(const unsigned int hkey_event);
2033 static void hotkey_driver_event(const unsigned int scancode);
2034 static void hotkey_poll_setup(const bool may_warn);
2036 /* HKEY.MHKG() return bits */
2037 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2039 static int hotkey_get_wlsw(void)
2043 if (!tp_features.hotkey_wlsw)
2046 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2048 return (tpacpi_wlsw_emulstate) ?
2049 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2052 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2055 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2058 static int hotkey_get_tablet_mode(int *status)
2062 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2065 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2070 * Reads current event mask from firmware, and updates
2071 * hotkey_acpi_mask accordingly. Also resets any bits
2072 * from hotkey_user_mask that are unavailable to be
2073 * delivered (shadow requirement of the userspace ABI).
2075 * Call with hotkey_mutex held
2077 static int hotkey_mask_get(void)
2079 if (tp_features.hotkey_mask) {
2082 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2085 hotkey_acpi_mask = m;
2087 /* no mask support doesn't mean no event support... */
2088 hotkey_acpi_mask = hotkey_all_mask;
2091 /* sync userspace-visible mask */
2092 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2097 void static hotkey_mask_warn_incomplete_mask(void)
2099 /* log only what the user can fix... */
2100 const u32 wantedmask = hotkey_driver_mask &
2101 ~(hotkey_acpi_mask | hotkey_source_mask) &
2102 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2105 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2109 * Set the firmware mask when supported
2111 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2113 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2115 * Call with hotkey_mutex held
2117 static int hotkey_mask_set(u32 mask)
2122 const u32 fwmask = mask & ~hotkey_source_mask;
2124 if (tp_features.hotkey_mask) {
2125 for (i = 0; i < 32; i++) {
2126 if (!acpi_evalf(hkey_handle,
2127 NULL, "MHKM", "vdd", i + 1,
2128 !!(mask & (1 << i)))) {
2136 * We *must* make an inconditional call to hotkey_mask_get to
2137 * refresh hotkey_acpi_mask and update hotkey_user_mask
2139 * Take the opportunity to also log when we cannot _enable_
2142 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2143 pr_notice("asked for hotkey mask 0x%08x, but "
2144 "firmware forced it to 0x%08x\n",
2145 fwmask, hotkey_acpi_mask);
2148 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2149 hotkey_mask_warn_incomplete_mask();
2155 * Sets hotkey_user_mask and tries to set the firmware mask
2157 * Call with hotkey_mutex held
2159 static int hotkey_user_mask_set(const u32 mask)
2163 /* Give people a chance to notice they are doing something that
2164 * is bound to go boom on their users sooner or later */
2165 if (!tp_warned.hotkey_mask_ff &&
2166 (mask == 0xffff || mask == 0xffffff ||
2167 mask == 0xffffffff)) {
2168 tp_warned.hotkey_mask_ff = 1;
2169 pr_notice("setting the hotkey mask to 0x%08x is likely "
2170 "not the best way to go about it\n", mask);
2171 pr_notice("please consider using the driver defaults, "
2172 "and refer to up-to-date thinkpad-acpi "
2176 /* Try to enable what the user asked for, plus whatever we need.
2177 * this syncs everything but won't enable bits in hotkey_user_mask */
2178 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2180 /* Enable the available bits in hotkey_user_mask */
2181 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2187 * Sets the driver hotkey mask.
2189 * Can be called even if the hotkey subdriver is inactive
2191 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2195 /* Do the right thing if hotkey_init has not been called yet */
2196 if (!tp_features.hotkey) {
2197 hotkey_driver_mask = mask;
2201 mutex_lock(&hotkey_mutex);
2203 HOTKEY_CONFIG_CRITICAL_START
2204 hotkey_driver_mask = mask;
2205 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2206 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2208 HOTKEY_CONFIG_CRITICAL_END
2210 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2211 ~hotkey_source_mask);
2212 hotkey_poll_setup(true);
2214 mutex_unlock(&hotkey_mutex);
2219 static int hotkey_status_get(int *status)
2221 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2227 static int hotkey_status_set(bool enable)
2229 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2235 static void tpacpi_input_send_tabletsw(void)
2239 if (tp_features.hotkey_tablet &&
2240 !hotkey_get_tablet_mode(&state)) {
2241 mutex_lock(&tpacpi_inputdev_send_mutex);
2243 input_report_switch(tpacpi_inputdev,
2244 SW_TABLET_MODE, !!state);
2245 input_sync(tpacpi_inputdev);
2247 mutex_unlock(&tpacpi_inputdev_send_mutex);
2251 /* Do NOT call without validating scancode first */
2252 static void tpacpi_input_send_key(const unsigned int scancode)
2254 const unsigned int keycode = hotkey_keycode_map[scancode];
2256 if (keycode != KEY_RESERVED) {
2257 mutex_lock(&tpacpi_inputdev_send_mutex);
2259 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2260 input_report_key(tpacpi_inputdev, keycode, 1);
2261 input_sync(tpacpi_inputdev);
2263 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2264 input_report_key(tpacpi_inputdev, keycode, 0);
2265 input_sync(tpacpi_inputdev);
2267 mutex_unlock(&tpacpi_inputdev_send_mutex);
2271 /* Do NOT call without validating scancode first */
2272 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2274 hotkey_driver_event(scancode);
2275 if (hotkey_user_mask & (1 << scancode))
2276 tpacpi_input_send_key(scancode);
2279 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2280 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2282 /* Do NOT call without validating scancode first */
2283 static void tpacpi_hotkey_send_key(unsigned int scancode)
2285 tpacpi_input_send_key_masked(scancode);
2288 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2292 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2293 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2294 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2295 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2296 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2297 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2299 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2301 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2303 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2304 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2305 n->displayexp_toggle =
2306 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2308 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2310 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2311 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2312 n->brightness_toggle =
2313 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2315 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2317 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2318 >> TP_NVRAM_POS_LEVEL_VOLUME;
2319 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2320 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2324 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2326 if ((event_mask & (1 << __scancode)) && \
2327 oldn->__member != newn->__member) \
2328 tpacpi_hotkey_send_key(__scancode); \
2331 #define TPACPI_MAY_SEND_KEY(__scancode) \
2333 if (event_mask & (1 << __scancode)) \
2334 tpacpi_hotkey_send_key(__scancode); \
2337 static void issue_volchange(const unsigned int oldvol,
2338 const unsigned int newvol,
2339 const u32 event_mask)
2341 unsigned int i = oldvol;
2343 while (i > newvol) {
2344 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2347 while (i < newvol) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2353 static void issue_brightnesschange(const unsigned int oldbrt,
2354 const unsigned int newbrt,
2355 const u32 event_mask)
2357 unsigned int i = oldbrt;
2359 while (i > newbrt) {
2360 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2363 while (i < newbrt) {
2364 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2369 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2370 struct tp_nvram_state *newn,
2371 const u32 event_mask)
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2376 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2381 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2386 * This code is supposed to duplicate the IBM firmware behaviour:
2387 * - Pressing MUTE issues mute hotkey message, even when already mute
2388 * - Pressing Volume up/down issues volume up/down hotkey messages,
2389 * even when already at maximum or minimum volume
2390 * - The act of unmuting issues volume up/down notification,
2391 * depending which key was used to unmute
2393 * We are constrained to what the NVRAM can tell us, which is not much
2394 * and certainly not enough if more than one volume hotkey was pressed
2395 * since the last poll cycle.
2397 * Just to make our life interesting, some newer Lenovo ThinkPads have
2398 * bugs in the BIOS and may fail to update volume_toggle properly.
2403 oldn->volume_toggle != newn->volume_toggle ||
2404 oldn->volume_level != newn->volume_level) {
2405 /* recently muted, or repeated mute keypress, or
2406 * multiple presses ending in mute */
2407 issue_volchange(oldn->volume_level, newn->volume_level,
2409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2414 /* recently unmuted, issue 'unmute' keypress */
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2417 if (oldn->volume_level != newn->volume_level) {
2418 issue_volchange(oldn->volume_level, newn->volume_level,
2420 } else if (oldn->volume_toggle != newn->volume_toggle) {
2421 /* repeated vol up/down keypress at end of scale ? */
2422 if (newn->volume_level == 0)
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2424 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2429 /* handle brightness */
2430 if (oldn->brightness_level != newn->brightness_level) {
2431 issue_brightnesschange(oldn->brightness_level,
2432 newn->brightness_level, event_mask);
2433 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2434 /* repeated key presses that didn't change state */
2435 if (newn->brightness_level == 0)
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2437 else if (newn->brightness_level >= bright_maxlvl
2438 && !tp_features.bright_unkfw)
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2442 #undef TPACPI_COMPARE_KEY
2443 #undef TPACPI_MAY_SEND_KEY
2449 * We track all events in hotkey_source_mask all the time, since
2450 * most of them are edge-based. We only issue those requested by
2451 * hotkey_user_mask or hotkey_driver_mask, though.
2453 static int hotkey_kthread(void *data)
2455 struct tp_nvram_state s[2];
2456 u32 poll_mask, event_mask;
2457 unsigned int si, so;
2459 unsigned int change_detector;
2460 unsigned int poll_freq;
2463 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2472 /* Initial state for compares */
2473 mutex_lock(&hotkey_thread_data_mutex);
2474 change_detector = hotkey_config_change;
2475 poll_mask = hotkey_source_mask;
2476 event_mask = hotkey_source_mask &
2477 (hotkey_driver_mask | hotkey_user_mask);
2478 poll_freq = hotkey_poll_freq;
2479 mutex_unlock(&hotkey_thread_data_mutex);
2480 hotkey_read_nvram(&s[so], poll_mask);
2482 while (!kthread_should_stop()) {
2484 if (likely(poll_freq))
2487 t = 100; /* should never happen... */
2489 t = msleep_interruptible(t);
2490 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2493 if (t > 0 && !was_frozen)
2496 mutex_lock(&hotkey_thread_data_mutex);
2497 if (was_frozen || hotkey_config_change != change_detector) {
2498 /* forget old state on thaw or config change */
2501 change_detector = hotkey_config_change;
2503 poll_mask = hotkey_source_mask;
2504 event_mask = hotkey_source_mask &
2505 (hotkey_driver_mask | hotkey_user_mask);
2506 poll_freq = hotkey_poll_freq;
2507 mutex_unlock(&hotkey_thread_data_mutex);
2509 if (likely(poll_mask)) {
2510 hotkey_read_nvram(&s[si], poll_mask);
2511 if (likely(si != so)) {
2512 hotkey_compare_and_issue_event(&s[so], &s[si],
2525 /* call with hotkey_mutex held */
2526 static void hotkey_poll_stop_sync(void)
2528 if (tpacpi_hotkey_task) {
2529 kthread_stop(tpacpi_hotkey_task);
2530 tpacpi_hotkey_task = NULL;
2534 /* call with hotkey_mutex held */
2535 static void hotkey_poll_setup(const bool may_warn)
2537 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2538 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2540 if (hotkey_poll_freq > 0 &&
2541 (poll_driver_mask ||
2542 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2543 if (!tpacpi_hotkey_task) {
2544 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2545 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2546 if (IS_ERR(tpacpi_hotkey_task)) {
2547 tpacpi_hotkey_task = NULL;
2548 pr_err("could not create kernel thread "
2549 "for hotkey polling\n");
2553 hotkey_poll_stop_sync();
2554 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2555 hotkey_poll_freq == 0) {
2556 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2557 "require polling, which is currently "
2559 poll_user_mask, poll_driver_mask);
2564 static void hotkey_poll_setup_safe(const bool may_warn)
2566 mutex_lock(&hotkey_mutex);
2567 hotkey_poll_setup(may_warn);
2568 mutex_unlock(&hotkey_mutex);
2571 /* call with hotkey_mutex held */
2572 static void hotkey_poll_set_freq(unsigned int freq)
2575 hotkey_poll_stop_sync();
2577 hotkey_poll_freq = freq;
2580 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2582 static void hotkey_poll_setup(const bool __unused)
2586 static void hotkey_poll_setup_safe(const bool __unused)
2590 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2592 static int hotkey_inputdev_open(struct input_dev *dev)
2594 switch (tpacpi_lifecycle) {
2595 case TPACPI_LIFE_INIT:
2596 case TPACPI_LIFE_RUNNING:
2597 hotkey_poll_setup_safe(false);
2599 case TPACPI_LIFE_EXITING:
2603 /* Should only happen if tpacpi_lifecycle is corrupt */
2608 static void hotkey_inputdev_close(struct input_dev *dev)
2610 /* disable hotkey polling when possible */
2611 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2612 !(hotkey_source_mask & hotkey_driver_mask))
2613 hotkey_poll_setup_safe(false);
2616 /* sysfs hotkey enable ------------------------------------------------- */
2617 static ssize_t hotkey_enable_show(struct device *dev,
2618 struct device_attribute *attr,
2623 printk_deprecated_attribute("hotkey_enable",
2624 "Hotkey reporting is always enabled");
2626 res = hotkey_status_get(&status);
2630 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2633 static ssize_t hotkey_enable_store(struct device *dev,
2634 struct device_attribute *attr,
2635 const char *buf, size_t count)
2639 printk_deprecated_attribute("hotkey_enable",
2640 "Hotkeys can be disabled through hotkey_mask");
2642 if (parse_strtoul(buf, 1, &t))
2651 static struct device_attribute dev_attr_hotkey_enable =
2652 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2653 hotkey_enable_show, hotkey_enable_store);
2655 /* sysfs hotkey mask --------------------------------------------------- */
2656 static ssize_t hotkey_mask_show(struct device *dev,
2657 struct device_attribute *attr,
2660 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2663 static ssize_t hotkey_mask_store(struct device *dev,
2664 struct device_attribute *attr,
2665 const char *buf, size_t count)
2670 if (parse_strtoul(buf, 0xffffffffUL, &t))
2673 if (mutex_lock_killable(&hotkey_mutex))
2674 return -ERESTARTSYS;
2676 res = hotkey_user_mask_set(t);
2678 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2679 hotkey_poll_setup(true);
2682 mutex_unlock(&hotkey_mutex);
2684 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2686 return (res) ? res : count;
2689 static struct device_attribute dev_attr_hotkey_mask =
2690 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2691 hotkey_mask_show, hotkey_mask_store);
2693 /* sysfs hotkey bios_enabled ------------------------------------------- */
2694 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2695 struct device_attribute *attr,
2698 return sprintf(buf, "0\n");
2701 static struct device_attribute dev_attr_hotkey_bios_enabled =
2702 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2704 /* sysfs hotkey bios_mask ---------------------------------------------- */
2705 static ssize_t hotkey_bios_mask_show(struct device *dev,
2706 struct device_attribute *attr,
2709 printk_deprecated_attribute("hotkey_bios_mask",
2710 "This attribute is useless.");
2711 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2714 static struct device_attribute dev_attr_hotkey_bios_mask =
2715 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2717 /* sysfs hotkey all_mask ----------------------------------------------- */
2718 static ssize_t hotkey_all_mask_show(struct device *dev,
2719 struct device_attribute *attr,
2722 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2723 hotkey_all_mask | hotkey_source_mask);
2726 static struct device_attribute dev_attr_hotkey_all_mask =
2727 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2729 /* sysfs hotkey recommended_mask --------------------------------------- */
2730 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2731 struct device_attribute *attr,
2734 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2735 (hotkey_all_mask | hotkey_source_mask)
2736 & ~hotkey_reserved_mask);
2739 static struct device_attribute dev_attr_hotkey_recommended_mask =
2740 __ATTR(hotkey_recommended_mask, S_IRUGO,
2741 hotkey_recommended_mask_show, NULL);
2743 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2745 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2746 static ssize_t hotkey_source_mask_show(struct device *dev,
2747 struct device_attribute *attr,
2750 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2753 static ssize_t hotkey_source_mask_store(struct device *dev,
2754 struct device_attribute *attr,
2755 const char *buf, size_t count)
2761 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2762 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2765 if (mutex_lock_killable(&hotkey_mutex))
2766 return -ERESTARTSYS;
2768 HOTKEY_CONFIG_CRITICAL_START
2769 hotkey_source_mask = t;
2770 HOTKEY_CONFIG_CRITICAL_END
2772 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2773 ~hotkey_source_mask);
2774 hotkey_poll_setup(true);
2776 /* check if events needed by the driver got disabled */
2777 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2778 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2780 mutex_unlock(&hotkey_mutex);
2783 pr_err("hotkey_source_mask: "
2784 "failed to update the firmware event mask!\n");
2787 pr_notice("hotkey_source_mask: "
2788 "some important events were disabled: 0x%04x\n",
2791 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2793 return (rc < 0) ? rc : count;
2796 static struct device_attribute dev_attr_hotkey_source_mask =
2797 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2798 hotkey_source_mask_show, hotkey_source_mask_store);
2800 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2801 static ssize_t hotkey_poll_freq_show(struct device *dev,
2802 struct device_attribute *attr,
2805 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2808 static ssize_t hotkey_poll_freq_store(struct device *dev,
2809 struct device_attribute *attr,
2810 const char *buf, size_t count)
2814 if (parse_strtoul(buf, 25, &t))
2817 if (mutex_lock_killable(&hotkey_mutex))
2818 return -ERESTARTSYS;
2820 hotkey_poll_set_freq(t);
2821 hotkey_poll_setup(true);
2823 mutex_unlock(&hotkey_mutex);
2825 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2830 static struct device_attribute dev_attr_hotkey_poll_freq =
2831 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2832 hotkey_poll_freq_show, hotkey_poll_freq_store);
2834 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2836 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2837 static ssize_t hotkey_radio_sw_show(struct device *dev,
2838 struct device_attribute *attr,
2842 res = hotkey_get_wlsw();
2846 /* Opportunistic update */
2847 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2849 return snprintf(buf, PAGE_SIZE, "%d\n",
2850 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2853 static struct device_attribute dev_attr_hotkey_radio_sw =
2854 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2856 static void hotkey_radio_sw_notify_change(void)
2858 if (tp_features.hotkey_wlsw)
2859 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2863 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2864 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2865 struct device_attribute *attr,
2869 res = hotkey_get_tablet_mode(&s);
2873 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2876 static struct device_attribute dev_attr_hotkey_tablet_mode =
2877 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2879 static void hotkey_tablet_mode_notify_change(void)
2881 if (tp_features.hotkey_tablet)
2882 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2883 "hotkey_tablet_mode");
2886 /* sysfs wakeup reason (pollable) -------------------------------------- */
2887 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2888 struct device_attribute *attr,
2891 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2894 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2895 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2897 static void hotkey_wakeup_reason_notify_change(void)
2899 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2903 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2904 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2905 struct device_attribute *attr,
2908 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2911 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2912 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2913 hotkey_wakeup_hotunplug_complete_show, NULL);
2915 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2917 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2918 "wakeup_hotunplug_complete");
2921 /* --------------------------------------------------------------------- */
2923 static struct attribute *hotkey_attributes[] __initdata = {
2924 &dev_attr_hotkey_enable.attr,
2925 &dev_attr_hotkey_bios_enabled.attr,
2926 &dev_attr_hotkey_bios_mask.attr,
2927 &dev_attr_hotkey_wakeup_reason.attr,
2928 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2929 &dev_attr_hotkey_mask.attr,
2930 &dev_attr_hotkey_all_mask.attr,
2931 &dev_attr_hotkey_recommended_mask.attr,
2932 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2933 &dev_attr_hotkey_source_mask.attr,
2934 &dev_attr_hotkey_poll_freq.attr,
2939 * Sync both the hw and sw blocking state of all switches
2941 static void tpacpi_send_radiosw_update(void)
2946 * We must sync all rfkill controllers *before* issuing any
2947 * rfkill input events, or we will race the rfkill core input
2950 * tpacpi_inputdev_send_mutex works as a synchronization point
2953 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2956 wlsw = hotkey_get_wlsw();
2958 /* Sync hw blocking state first if it is hw-blocked */
2959 if (wlsw == TPACPI_RFK_RADIO_OFF)
2960 tpacpi_rfk_update_hwblock_state(true);
2962 /* Sync sw blocking state */
2963 tpacpi_rfk_update_swstate_all();
2965 /* Sync hw blocking state last if it is hw-unblocked */
2966 if (wlsw == TPACPI_RFK_RADIO_ON)
2967 tpacpi_rfk_update_hwblock_state(false);
2969 /* Issue rfkill input event for WLSW switch */
2971 mutex_lock(&tpacpi_inputdev_send_mutex);
2973 input_report_switch(tpacpi_inputdev,
2974 SW_RFKILL_ALL, (wlsw > 0));
2975 input_sync(tpacpi_inputdev);
2977 mutex_unlock(&tpacpi_inputdev_send_mutex);
2981 * this can be unconditional, as we will poll state again
2982 * if userspace uses the notify to read data
2984 hotkey_radio_sw_notify_change();
2987 static void hotkey_exit(void)
2989 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2990 mutex_lock(&hotkey_mutex);
2991 hotkey_poll_stop_sync();
2992 mutex_unlock(&hotkey_mutex);
2995 if (hotkey_dev_attributes)
2996 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2998 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2999 "restoring original HKEY status and mask\n");
3000 /* yes, there is a bitwise or below, we want the
3001 * functions to be called even if one of them fail */
3002 if (((tp_features.hotkey_mask &&
3003 hotkey_mask_set(hotkey_orig_mask)) |
3004 hotkey_status_set(false)) != 0)
3005 pr_err("failed to restore hot key mask "
3006 "to BIOS defaults\n");
3009 static void __init hotkey_unmap(const unsigned int scancode)
3011 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3012 clear_bit(hotkey_keycode_map[scancode],
3013 tpacpi_inputdev->keybit);
3014 hotkey_keycode_map[scancode] = KEY_RESERVED;
3020 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3023 #define TPACPI_HK_Q_INIMASK 0x0001
3025 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3026 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3027 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3028 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3029 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3030 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3031 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3032 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3033 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3034 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3035 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3036 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3037 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3038 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3039 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3040 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3041 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3042 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3043 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3044 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3047 typedef u16 tpacpi_keymap_entry_t;
3048 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3050 static int __init hotkey_init(struct ibm_init_struct *iibm)
3052 /* Requirements for changing the default keymaps:
3054 * 1. Many of the keys are mapped to KEY_RESERVED for very
3055 * good reasons. Do not change them unless you have deep
3056 * knowledge on the IBM and Lenovo ThinkPad firmware for
3057 * the various ThinkPad models. The driver behaves
3058 * differently for KEY_RESERVED: such keys have their
3059 * hot key mask *unset* in mask_recommended, and also
3060 * in the initial hot key mask programmed into the
3061 * firmware at driver load time, which means the firm-
3062 * ware may react very differently if you change them to
3065 * 2. You must be subscribed to the linux-thinkpad and
3066 * ibm-acpi-devel mailing lists, and you should read the
3067 * list archives since 2007 if you want to change the
3068 * keymaps. This requirement exists so that you will
3069 * know the past history of problems with the thinkpad-
3070 * acpi driver keymaps, and also that you will be
3071 * listening to any bug reports;
3073 * 3. Do not send thinkpad-acpi specific patches directly to
3074 * for merging, *ever*. Send them to the linux-acpi
3075 * mailinglist for comments. Merging is to be done only
3076 * through acpi-test and the ACPI maintainer.
3078 * If the above is too much to ask, don't change the keymap.
3079 * Ask the thinkpad-acpi maintainer to do it, instead.
3083 TPACPI_KEYMAP_IBM_GENERIC = 0,
3084 TPACPI_KEYMAP_LENOVO_GENERIC,
3087 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3088 /* Generic keymap for IBM ThinkPads */
3089 [TPACPI_KEYMAP_IBM_GENERIC] = {
3090 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3091 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3092 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3093 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3095 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3096 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3097 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3098 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3100 /* brightness: firmware always reacts to them */
3101 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3102 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3104 /* Thinklight: firmware always react to it */
3105 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3107 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3108 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3110 /* Volume: firmware always react to it and reprograms
3111 * the built-in *extra* mixer. Never map it to control
3112 * another mixer by default. */
3113 KEY_RESERVED, /* 0x14: VOLUME UP */
3114 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3115 KEY_RESERVED, /* 0x16: MUTE */
3117 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3119 /* (assignments unknown, please report if found) */
3120 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3121 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3124 /* Generic keymap for Lenovo ThinkPads */
3125 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3126 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3127 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3128 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3129 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3131 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3132 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3133 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3134 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3136 /* These should be enabled --only-- when ACPI video
3137 * is disabled (i.e. in "vendor" mode), and are handled
3138 * in a special way by the init code */
3139 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3140 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3142 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3144 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3145 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3147 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3148 * react to it and reprograms the built-in *extra* mixer.
3149 * Never map it to control another mixer by default.
3151 * T60?, T61, R60?, R61: firmware and EC tries to send
3152 * these over the regular keyboard, so these are no-ops,
3153 * but there are still weird bugs re. MUTE, so do not
3154 * change unless you get test reports from all Lenovo
3155 * models. May cause the BIOS to interfere with the
3158 KEY_RESERVED, /* 0x14: VOLUME UP */
3159 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3160 KEY_RESERVED, /* 0x16: MUTE */
3162 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3164 /* (assignments unknown, please report if found) */
3165 KEY_UNKNOWN, KEY_UNKNOWN,
3168 * The mic mute button only sends 0x1a. It does not
3169 * automatically mute the mic or change the mute light.
3171 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3173 /* (assignments unknown, please report if found) */
3176 /* Extra keys in use since the X240 / T440 / T540 */
3177 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_COMPUTER,
3181 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3182 /* Generic maps (fallback) */
3184 .vendor = PCI_VENDOR_ID_IBM,
3185 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3186 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3189 .vendor = PCI_VENDOR_ID_LENOVO,
3190 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3191 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3195 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3196 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3201 bool radiosw_state = false;
3202 bool tabletsw_state = false;
3204 unsigned long quirks;
3205 unsigned long keymap_id;
3207 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3208 "initializing hotkey subdriver\n");
3210 BUG_ON(!tpacpi_inputdev);
3211 BUG_ON(tpacpi_inputdev->open != NULL ||
3212 tpacpi_inputdev->close != NULL);
3214 TPACPI_ACPIHANDLE_INIT(hkey);
3215 mutex_init(&hotkey_mutex);
3217 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3218 mutex_init(&hotkey_thread_data_mutex);
3221 /* hotkey not supported on 570 */
3222 tp_features.hotkey = hkey_handle != NULL;
3224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3226 str_supported(tp_features.hotkey));
3228 if (!tp_features.hotkey)
3231 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3232 ARRAY_SIZE(tpacpi_hotkey_qtable));
3234 tpacpi_disable_brightness_delay();
3236 /* MUST have enough space for all attributes to be added to
3237 * hotkey_dev_attributes */
3238 hotkey_dev_attributes = create_attr_set(
3239 ARRAY_SIZE(hotkey_attributes) + 2,
3241 if (!hotkey_dev_attributes)
3243 res = add_many_to_attr_set(hotkey_dev_attributes,
3245 ARRAY_SIZE(hotkey_attributes));
3249 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3250 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3251 for HKEY interface version 0x100 */
3252 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3253 if ((hkeyv >> 8) != 1) {
3254 pr_err("unknown version of the HKEY interface: 0x%x\n",
3256 pr_err("please report this to %s\n", TPACPI_MAIL);
3259 * MHKV 0x100 in A31, R40, R40e,
3260 * T4x, X31, and later
3262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "firmware HKEY interface version: 0x%x\n",
3266 /* Paranoia check AND init hotkey_all_mask */
3267 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3269 pr_err("missing MHKA handler, "
3270 "please report this to %s\n",
3272 /* Fallback: pre-init for FN+F3,F4,F12 */
3273 hotkey_all_mask = 0x080cU;
3275 tp_features.hotkey_mask = 1;
3280 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3281 "hotkey masks are %s\n",
3282 str_supported(tp_features.hotkey_mask));
3284 /* Init hotkey_all_mask if not initialized yet */
3285 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3286 (quirks & TPACPI_HK_Q_INIMASK))
3287 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3289 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3290 if (tp_features.hotkey_mask) {
3291 /* hotkey_source_mask *must* be zero for
3292 * the first hotkey_mask_get to return hotkey_orig_mask */
3293 res = hotkey_mask_get();
3297 hotkey_orig_mask = hotkey_acpi_mask;
3299 hotkey_orig_mask = hotkey_all_mask;
3300 hotkey_acpi_mask = hotkey_all_mask;
3303 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3305 tp_features.hotkey_wlsw = 1;
3306 radiosw_state = !!tpacpi_wlsw_emulstate;
3307 pr_info("radio switch emulation enabled\n");
3310 /* Not all thinkpads have a hardware radio switch */
3311 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3312 tp_features.hotkey_wlsw = 1;
3313 radiosw_state = !!status;
3314 pr_info("radio switch found; radios are %s\n",
3315 enabled(status, 0));
3317 if (tp_features.hotkey_wlsw)
3318 res = add_to_attr_set(hotkey_dev_attributes,
3319 &dev_attr_hotkey_radio_sw.attr);
3321 /* For X41t, X60t, X61t Tablets... */
3322 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3323 tp_features.hotkey_tablet = 1;
3324 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3325 pr_info("possible tablet mode switch found; "
3326 "ThinkPad in %s mode\n",
3327 (tabletsw_state) ? "tablet" : "laptop");
3328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_tablet_mode.attr);
3333 res = register_attr_set_with_sysfs(
3334 hotkey_dev_attributes,
3335 &tpacpi_pdev->dev.kobj);
3339 /* Set up key map */
3340 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3342 if (!hotkey_keycode_map) {
3343 pr_err("failed to allocate memory for key map\n");
3348 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3349 ARRAY_SIZE(tpacpi_keymap_qtable));
3350 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3351 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3352 "using keymap number %lu\n", keymap_id);
3354 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3355 TPACPI_HOTKEY_MAP_SIZE);
3357 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3358 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3359 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3360 tpacpi_inputdev->keycode = hotkey_keycode_map;
3361 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3362 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3363 input_set_capability(tpacpi_inputdev, EV_KEY,
3364 hotkey_keycode_map[i]);
3366 if (i < sizeof(hotkey_reserved_mask)*8)
3367 hotkey_reserved_mask |= 1 << i;
3371 if (tp_features.hotkey_wlsw) {
3372 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3373 input_report_switch(tpacpi_inputdev,
3374 SW_RFKILL_ALL, radiosw_state);
3376 if (tp_features.hotkey_tablet) {
3377 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3378 input_report_switch(tpacpi_inputdev,
3379 SW_TABLET_MODE, tabletsw_state);
3382 /* Do not issue duplicate brightness change events to
3383 * userspace. tpacpi_detect_brightness_capabilities() must have
3384 * been called before this point */
3385 if (acpi_video_backlight_support()) {
3386 pr_info("This ThinkPad has standard ACPI backlight "
3387 "brightness control, supported by the ACPI "
3389 pr_notice("Disabling thinkpad-acpi brightness events "
3392 /* Disable brightness up/down on Lenovo thinkpads when
3393 * ACPI is handling them, otherwise it is plain impossible
3394 * for userspace to do something even remotely sane */
3395 hotkey_reserved_mask |=
3396 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3397 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3398 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3399 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3402 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3403 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3405 & ~hotkey_reserved_mask;
3407 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3408 "hotkey source mask 0x%08x, polling freq %u\n",
3409 hotkey_source_mask, hotkey_poll_freq);
3412 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3413 "enabling firmware HKEY event interface...\n");
3414 res = hotkey_status_set(true);
3419 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3420 | hotkey_driver_mask)
3421 & ~hotkey_source_mask);
3422 if (res < 0 && res != -ENXIO) {
3426 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3427 & ~hotkey_reserved_mask;
3428 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3429 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3430 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3432 tpacpi_inputdev->open = &hotkey_inputdev_open;
3433 tpacpi_inputdev->close = &hotkey_inputdev_close;
3435 hotkey_poll_setup_safe(true);
3440 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3441 hotkey_dev_attributes = NULL;
3443 return (res < 0)? res : 1;
3446 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3447 * mode, Web conference mode, Function mode and Lay-flat mode.
3448 * We support Home mode and Function mode currently.
3450 * Will consider support rest of modes in future.
3453 enum ADAPTIVE_KEY_MODE {
3456 WEB_CONFERENCE_MODE,
3461 const int adaptive_keyboard_modes[] = {
3463 /* WEB_BROWSER_MODE = 2,
3464 WEB_CONFERENCE_MODE = 3, */
3468 #define DFR_CHANGE_ROW 0x101
3469 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3471 /* press Fn key a while second, it will switch to Function Mode. Then
3472 * release Fn key, previous mode be restored.
3474 static bool adaptive_keyboard_mode_is_saved;
3475 static int adaptive_keyboard_prev_mode;
3477 static int adaptive_keyboard_get_next_mode(int mode)
3480 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3482 for (i = 0; i <= max_mode; i++) {
3483 if (adaptive_keyboard_modes[i] == mode)
3492 return adaptive_keyboard_modes[i];
3495 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3497 u32 current_mode = 0;
3501 case DFR_CHANGE_ROW:
3502 if (adaptive_keyboard_mode_is_saved) {
3503 new_mode = adaptive_keyboard_prev_mode;
3504 adaptive_keyboard_mode_is_saved = false;
3507 hkey_handle, ¤t_mode,
3509 pr_err("Cannot read adaptive keyboard mode\n");
3512 new_mode = adaptive_keyboard_get_next_mode(
3517 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3518 pr_err("Cannot set adaptive keyboard mode\n");
3524 case DFR_SHOW_QUICKVIEW_ROW:
3525 if (!acpi_evalf(hkey_handle,
3526 &adaptive_keyboard_prev_mode,
3528 pr_err("Cannot read adaptive keyboard mode\n");
3531 adaptive_keyboard_mode_is_saved = true;
3533 if (!acpi_evalf(hkey_handle,
3534 NULL, "STRW", "vd", FUNCTION_MODE)) {
3535 pr_err("Cannot set adaptive keyboard mode\n");
3546 static bool hotkey_notify_hotkey(const u32 hkey,
3548 bool *ignore_acpi_ev)
3550 /* 0x1000-0x1FFF: key presses */
3551 unsigned int scancode = hkey & 0xfff;
3552 *send_acpi_ev = true;
3553 *ignore_acpi_ev = false;
3555 /* HKEY event 0x1001 is scancode 0x00 */
3556 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3558 if (!(hotkey_source_mask & (1 << scancode))) {
3559 tpacpi_input_send_key_masked(scancode);
3560 *send_acpi_ev = false;
3562 *ignore_acpi_ev = true;
3566 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3571 static bool hotkey_notify_wakeup(const u32 hkey,
3573 bool *ignore_acpi_ev)
3575 /* 0x2000-0x2FFF: Wakeup reason */
3576 *send_acpi_ev = true;
3577 *ignore_acpi_ev = false;
3580 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3581 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3582 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3583 *ignore_acpi_ev = true;
3586 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3587 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3588 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3589 *ignore_acpi_ev = true;
3592 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3593 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3594 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3595 /* how to auto-heal: */
3596 /* 2313: woke up from S3, go to S4/S5 */
3597 /* 2413: woke up from S4, go to S5 */
3604 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3605 pr_info("woke up due to a hot-unplug request...\n");
3606 hotkey_wakeup_reason_notify_change();
3611 static bool hotkey_notify_dockevent(const u32 hkey,
3613 bool *ignore_acpi_ev)
3615 /* 0x4000-0x4FFF: dock-related events */
3616 *send_acpi_ev = true;
3617 *ignore_acpi_ev = false;
3620 case TP_HKEY_EV_UNDOCK_ACK:
3621 /* ACPI undock operation completed after wakeup */
3622 hotkey_autosleep_ack = 1;
3623 pr_info("undocked\n");
3624 hotkey_wakeup_hotunplug_complete_notify_change();
3627 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3628 pr_info("docked into hotplug port replicator\n");
3630 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3631 pr_info("undocked from hotplug port replicator\n");
3639 static bool hotkey_notify_usrevent(const u32 hkey,
3641 bool *ignore_acpi_ev)
3643 /* 0x5000-0x5FFF: human interface helpers */
3644 *send_acpi_ev = true;
3645 *ignore_acpi_ev = false;
3648 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3649 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3652 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3653 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3654 tpacpi_input_send_tabletsw();
3655 hotkey_tablet_mode_notify_change();
3656 *send_acpi_ev = false;
3659 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3660 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3661 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3662 /* do not propagate these events */
3663 *ignore_acpi_ev = true;
3671 static void thermal_dump_all_sensors(void);
3673 static bool hotkey_notify_6xxx(const u32 hkey,
3675 bool *ignore_acpi_ev)
3679 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3680 *send_acpi_ev = true;
3681 *ignore_acpi_ev = false;
3684 case TP_HKEY_EV_THM_TABLE_CHANGED:
3685 pr_info("EC reports that Thermal Table has changed\n");
3686 /* recommended action: do nothing, we don't have
3687 * Lenovo ATM information */
3689 case TP_HKEY_EV_ALARM_BAT_HOT:
3690 pr_crit("THERMAL ALARM: battery is too hot!\n");
3691 /* recommended action: warn user through gui */
3693 case TP_HKEY_EV_ALARM_BAT_XHOT:
3694 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3695 /* recommended action: immediate sleep/hibernate */
3697 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3698 pr_crit("THERMAL ALARM: "
3699 "a sensor reports something is too hot!\n");
3700 /* recommended action: warn user through gui, that */
3701 /* some internal component is too hot */
3703 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3704 pr_alert("THERMAL EMERGENCY: "
3705 "a sensor reports something is extremely hot!\n");
3706 /* recommended action: immediate sleep/hibernate */
3708 case TP_HKEY_EV_AC_CHANGED:
3709 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3710 * AC status changed; can be triggered by plugging or
3711 * unplugging AC adapter, docking or undocking. */
3715 case TP_HKEY_EV_KEY_NUMLOCK:
3716 case TP_HKEY_EV_KEY_FN:
3717 /* key press events, we just ignore them as long as the EC
3718 * is still reporting them in the normal keyboard stream */
3719 *send_acpi_ev = false;
3720 *ignore_acpi_ev = true;
3724 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3728 thermal_dump_all_sensors();
3733 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3737 bool ignore_acpi_ev;
3740 if (event != 0x80) {
3741 pr_err("unknown HKEY notification event %d\n", event);
3742 /* forward it to userspace, maybe it knows how to handle it */
3743 acpi_bus_generate_netlink_event(
3744 ibm->acpi->device->pnp.device_class,
3745 dev_name(&ibm->acpi->device->dev),
3751 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3752 pr_err("failed to retrieve HKEY event\n");
3761 send_acpi_ev = true;
3762 ignore_acpi_ev = false;
3764 switch (hkey >> 12) {
3766 /* 0x1000-0x1FFF: key presses */
3767 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3771 /* 0x2000-0x2FFF: Wakeup reason */
3772 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3776 /* 0x3000-0x3FFF: bay-related wakeups */
3778 case TP_HKEY_EV_BAYEJ_ACK:
3779 hotkey_autosleep_ack = 1;
3780 pr_info("bay ejected\n");
3781 hotkey_wakeup_hotunplug_complete_notify_change();
3784 case TP_HKEY_EV_OPTDRV_EJ:
3785 /* FIXME: kick libata if SATA link offline */
3793 /* 0x4000-0x4FFF: dock-related events */
3794 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3798 /* 0x5000-0x5FFF: human interface helpers */
3799 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3803 /* 0x6000-0x6FFF: thermal alarms/notices and
3804 * keyboard events */
3805 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3809 /* 0x7000-0x7FFF: misc */
3810 if (tp_features.hotkey_wlsw &&
3811 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3812 tpacpi_send_radiosw_update();
3817 /* fallthrough to default */
3822 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3823 pr_notice("please report the conditions when this "
3824 "event happened to %s\n", TPACPI_MAIL);
3827 /* netlink events */
3828 if (!ignore_acpi_ev && send_acpi_ev) {
3829 acpi_bus_generate_netlink_event(
3830 ibm->acpi->device->pnp.device_class,
3831 dev_name(&ibm->acpi->device->dev),
3837 static void hotkey_suspend(void)
3841 /* Do these on suspend, we get the events on early resume! */
3842 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3843 hotkey_autosleep_ack = 0;
3845 /* save previous mode of adaptive keyboard of X1 Carbon */
3846 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3847 if ((hkeyv >> 8) == 2) {
3848 if (!acpi_evalf(hkey_handle,
3849 &adaptive_keyboard_prev_mode,
3851 pr_err("Cannot read adaptive keyboard mode.\n");
3857 static void hotkey_resume(void)
3861 tpacpi_disable_brightness_delay();
3863 if (hotkey_status_set(true) < 0 ||
3864 hotkey_mask_set(hotkey_acpi_mask) < 0)
3865 pr_err("error while attempting to reset the event "
3866 "firmware interface\n");
3868 tpacpi_send_radiosw_update();
3869 hotkey_tablet_mode_notify_change();
3870 hotkey_wakeup_reason_notify_change();
3871 hotkey_wakeup_hotunplug_complete_notify_change();
3872 hotkey_poll_setup_safe(false);
3874 /* restore previous mode of adapive keyboard of X1 Carbon */
3875 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3876 if ((hkeyv >> 8) == 2) {
3877 if (!acpi_evalf(hkey_handle,
3880 adaptive_keyboard_prev_mode)) {
3881 pr_err("Cannot set adaptive keyboard mode.\n");
3887 /* procfs -------------------------------------------------------------- */
3888 static int hotkey_read(struct seq_file *m)
3892 if (!tp_features.hotkey) {
3893 seq_printf(m, "status:\t\tnot supported\n");
3897 if (mutex_lock_killable(&hotkey_mutex))
3898 return -ERESTARTSYS;
3899 res = hotkey_status_get(&status);
3901 res = hotkey_mask_get();
3902 mutex_unlock(&hotkey_mutex);
3906 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3907 if (hotkey_all_mask) {
3908 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3909 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3911 seq_printf(m, "mask:\t\tnot supported\n");
3912 seq_printf(m, "commands:\tenable, disable, reset\n");
3918 static void hotkey_enabledisable_warn(bool enable)
3920 tpacpi_log_usertask("procfs hotkey enable/disable");
3921 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3922 pr_fmt("hotkey enable/disable functionality has been "
3923 "removed from the driver. "
3924 "Hotkeys are always enabled.\n")))
3925 pr_err("Please remove the hotkey=enable module "
3926 "parameter, it is deprecated. "
3927 "Hotkeys are always enabled.\n");
3930 static int hotkey_write(char *buf)
3936 if (!tp_features.hotkey)
3939 if (mutex_lock_killable(&hotkey_mutex))
3940 return -ERESTARTSYS;
3942 mask = hotkey_user_mask;
3945 while ((cmd = next_cmd(&buf))) {
3946 if (strlencmp(cmd, "enable") == 0) {
3947 hotkey_enabledisable_warn(1);
3948 } else if (strlencmp(cmd, "disable") == 0) {
3949 hotkey_enabledisable_warn(0);
3951 } else if (strlencmp(cmd, "reset") == 0) {
3952 mask = (hotkey_all_mask | hotkey_source_mask)
3953 & ~hotkey_reserved_mask;
3954 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3956 } else if (sscanf(cmd, "%x", &mask) == 1) {
3965 tpacpi_disclose_usertask("procfs hotkey",
3966 "set mask to 0x%08x\n", mask);
3967 res = hotkey_user_mask_set(mask);
3971 mutex_unlock(&hotkey_mutex);
3975 static const struct acpi_device_id ibm_htk_device_ids[] = {
3976 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3977 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3981 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3982 .hid = ibm_htk_device_ids,
3983 .notify = hotkey_notify,
3984 .handle = &hkey_handle,
3985 .type = ACPI_DEVICE_NOTIFY,
3988 static struct ibm_struct hotkey_driver_data = {
3990 .read = hotkey_read,
3991 .write = hotkey_write,
3992 .exit = hotkey_exit,
3993 .resume = hotkey_resume,
3994 .suspend = hotkey_suspend,
3995 .acpi = &ibm_hotkey_acpidriver,
3998 /*************************************************************************
3999 * Bluetooth subdriver
4003 /* ACPI GBDC/SBDC bits */
4004 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4005 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4006 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4007 0 = disable, 1 = enable */
4011 /* ACPI \BLTH commands */
4012 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4013 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4014 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4015 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4016 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4019 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4021 static int bluetooth_get_status(void)
4025 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4026 if (dbg_bluetoothemul)
4027 return (tpacpi_bluetooth_emulstate) ?
4028 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4031 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4034 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4035 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4038 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4042 vdbg_printk(TPACPI_DBG_RFKILL,
4043 "will attempt to %s bluetooth\n",
4044 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4046 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4047 if (dbg_bluetoothemul) {
4048 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4053 if (state == TPACPI_RFK_RADIO_ON)
4054 status = TP_ACPI_BLUETOOTH_RADIOSSW
4055 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4059 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4065 /* sysfs bluetooth enable ---------------------------------------------- */
4066 static ssize_t bluetooth_enable_show(struct device *dev,
4067 struct device_attribute *attr,
4070 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4074 static ssize_t bluetooth_enable_store(struct device *dev,
4075 struct device_attribute *attr,
4076 const char *buf, size_t count)
4078 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4082 static struct device_attribute dev_attr_bluetooth_enable =
4083 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
4084 bluetooth_enable_show, bluetooth_enable_store);
4086 /* --------------------------------------------------------------------- */
4088 static struct attribute *bluetooth_attributes[] = {
4089 &dev_attr_bluetooth_enable.attr,
4093 static const struct attribute_group bluetooth_attr_group = {
4094 .attrs = bluetooth_attributes,
4097 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4098 .get_status = bluetooth_get_status,
4099 .set_status = bluetooth_set_status,
4102 static void bluetooth_shutdown(void)
4104 /* Order firmware to save current state to NVRAM */
4105 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4106 TP_ACPI_BLTH_SAVE_STATE))
4107 pr_notice("failed to save bluetooth state to NVRAM\n");
4109 vdbg_printk(TPACPI_DBG_RFKILL,
4110 "bluetooth state saved to NVRAM\n");
4113 static void bluetooth_exit(void)
4115 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4116 &bluetooth_attr_group);
4118 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4120 bluetooth_shutdown();
4123 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4128 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4129 "initializing bluetooth subdriver\n");
4131 TPACPI_ACPIHANDLE_INIT(hkey);
4133 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4134 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4135 tp_features.bluetooth = hkey_handle &&
4136 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4138 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4139 "bluetooth is %s, status 0x%02x\n",
4140 str_supported(tp_features.bluetooth),
4143 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4144 if (dbg_bluetoothemul) {
4145 tp_features.bluetooth = 1;
4146 pr_info("bluetooth switch emulation enabled\n");
4149 if (tp_features.bluetooth &&
4150 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4151 /* no bluetooth hardware present in system */
4152 tp_features.bluetooth = 0;
4153 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4154 "bluetooth hardware not installed\n");
4157 if (!tp_features.bluetooth)
4160 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4161 &bluetooth_tprfk_ops,
4162 RFKILL_TYPE_BLUETOOTH,
4163 TPACPI_RFK_BLUETOOTH_SW_NAME,
4168 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4169 &bluetooth_attr_group);
4171 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4178 /* procfs -------------------------------------------------------------- */
4179 static int bluetooth_read(struct seq_file *m)
4181 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4184 static int bluetooth_write(char *buf)
4186 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4189 static struct ibm_struct bluetooth_driver_data = {
4190 .name = "bluetooth",
4191 .read = bluetooth_read,
4192 .write = bluetooth_write,
4193 .exit = bluetooth_exit,
4194 .shutdown = bluetooth_shutdown,
4197 /*************************************************************************
4202 /* ACPI GWAN/SWAN bits */
4203 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4204 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4205 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4206 0 = disable, 1 = enable */
4209 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4211 static int wan_get_status(void)
4215 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4217 return (tpacpi_wwan_emulstate) ?
4218 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4221 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4224 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4225 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4228 static int wan_set_status(enum tpacpi_rfkill_state state)
4232 vdbg_printk(TPACPI_DBG_RFKILL,
4233 "will attempt to %s wwan\n",
4234 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4236 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4238 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4243 if (state == TPACPI_RFK_RADIO_ON)
4244 status = TP_ACPI_WANCARD_RADIOSSW
4245 | TP_ACPI_WANCARD_RESUMECTRL;
4249 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4255 /* sysfs wan enable ---------------------------------------------------- */
4256 static ssize_t wan_enable_show(struct device *dev,
4257 struct device_attribute *attr,
4260 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4264 static ssize_t wan_enable_store(struct device *dev,
4265 struct device_attribute *attr,
4266 const char *buf, size_t count)
4268 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4272 static struct device_attribute dev_attr_wan_enable =
4273 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4274 wan_enable_show, wan_enable_store);
4276 /* --------------------------------------------------------------------- */
4278 static struct attribute *wan_attributes[] = {
4279 &dev_attr_wan_enable.attr,
4283 static const struct attribute_group wan_attr_group = {
4284 .attrs = wan_attributes,
4287 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4288 .get_status = wan_get_status,
4289 .set_status = wan_set_status,
4292 static void wan_shutdown(void)
4294 /* Order firmware to save current state to NVRAM */
4295 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4296 TP_ACPI_WGSV_SAVE_STATE))
4297 pr_notice("failed to save WWAN state to NVRAM\n");
4299 vdbg_printk(TPACPI_DBG_RFKILL,
4300 "WWAN state saved to NVRAM\n");
4303 static void wan_exit(void)
4305 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4308 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4313 static int __init wan_init(struct ibm_init_struct *iibm)
4318 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4319 "initializing wan subdriver\n");
4321 TPACPI_ACPIHANDLE_INIT(hkey);
4323 tp_features.wan = hkey_handle &&
4324 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4326 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4327 "wan is %s, status 0x%02x\n",
4328 str_supported(tp_features.wan),
4331 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4333 tp_features.wan = 1;
4334 pr_info("wwan switch emulation enabled\n");
4337 if (tp_features.wan &&
4338 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4339 /* no wan hardware present in system */
4340 tp_features.wan = 0;
4341 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4342 "wan hardware not installed\n");
4345 if (!tp_features.wan)
4348 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4351 TPACPI_RFK_WWAN_SW_NAME,
4356 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4360 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4367 /* procfs -------------------------------------------------------------- */
4368 static int wan_read(struct seq_file *m)
4370 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4373 static int wan_write(char *buf)
4375 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4378 static struct ibm_struct wan_driver_data = {
4383 .shutdown = wan_shutdown,
4386 /*************************************************************************
4391 /* ACPI GUWB/SUWB bits */
4392 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4393 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4396 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4398 static int uwb_get_status(void)
4402 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4404 return (tpacpi_uwb_emulstate) ?
4405 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4408 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4411 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4412 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4415 static int uwb_set_status(enum tpacpi_rfkill_state state)
4419 vdbg_printk(TPACPI_DBG_RFKILL,
4420 "will attempt to %s UWB\n",
4421 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4423 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4425 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4430 if (state == TPACPI_RFK_RADIO_ON)
4431 status = TP_ACPI_UWB_RADIOSSW;
4435 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4441 /* --------------------------------------------------------------------- */
4443 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4444 .get_status = uwb_get_status,
4445 .set_status = uwb_set_status,
4448 static void uwb_exit(void)
4450 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4453 static int __init uwb_init(struct ibm_init_struct *iibm)
4458 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4459 "initializing uwb subdriver\n");
4461 TPACPI_ACPIHANDLE_INIT(hkey);
4463 tp_features.uwb = hkey_handle &&
4464 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4466 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4467 "uwb is %s, status 0x%02x\n",
4468 str_supported(tp_features.uwb),
4471 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4473 tp_features.uwb = 1;
4474 pr_info("uwb switch emulation enabled\n");
4477 if (tp_features.uwb &&
4478 !(status & TP_ACPI_UWB_HWPRESENT)) {
4479 /* no uwb hardware present in system */
4480 tp_features.uwb = 0;
4481 dbg_printk(TPACPI_DBG_INIT,
4482 "uwb hardware not installed\n");
4485 if (!tp_features.uwb)
4488 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4491 TPACPI_RFK_UWB_SW_NAME,
4496 static struct ibm_struct uwb_driver_data = {
4499 .flags.experimental = 1,
4502 /*************************************************************************
4506 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4508 enum video_access_mode {
4509 TPACPI_VIDEO_NONE = 0,
4510 TPACPI_VIDEO_570, /* 570 */
4511 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4512 TPACPI_VIDEO_NEW, /* all others */
4515 enum { /* video status flags, based on VIDEO_570 */
4516 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4517 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4518 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4521 enum { /* TPACPI_VIDEO_570 constants */
4522 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4523 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4524 * video_status_flags */
4525 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4526 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4529 static enum video_access_mode video_supported;
4530 static int video_orig_autosw;
4532 static int video_autosw_get(void);
4533 static int video_autosw_set(int enable);
4535 TPACPI_HANDLE(vid, root,
4536 "\\_SB.PCI.AGP.VGA", /* 570 */
4537 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4538 "\\_SB.PCI0.VID0", /* 770e */
4539 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4540 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4541 "\\_SB.PCI0.AGP.VID", /* all others */
4544 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4546 static int __init video_init(struct ibm_init_struct *iibm)
4550 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4552 TPACPI_ACPIHANDLE_INIT(vid);
4553 if (tpacpi_is_ibm())
4554 TPACPI_ACPIHANDLE_INIT(vid2);
4556 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4557 /* G41, assume IVGA doesn't change */
4558 vid_handle = vid2_handle;
4561 /* video switching not supported on R30, R31 */
4562 video_supported = TPACPI_VIDEO_NONE;
4563 else if (tpacpi_is_ibm() &&
4564 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4566 video_supported = TPACPI_VIDEO_570;
4567 else if (tpacpi_is_ibm() &&
4568 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4569 /* 600e/x, 770e, 770x */
4570 video_supported = TPACPI_VIDEO_770;
4573 video_supported = TPACPI_VIDEO_NEW;
4575 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4576 str_supported(video_supported != TPACPI_VIDEO_NONE),
4579 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4582 static void video_exit(void)
4584 dbg_printk(TPACPI_DBG_EXIT,
4585 "restoring original video autoswitch mode\n");
4586 if (video_autosw_set(video_orig_autosw))
4587 pr_err("error while trying to restore original "
4588 "video autoswitch mode\n");
4591 static int video_outputsw_get(void)
4596 switch (video_supported) {
4597 case TPACPI_VIDEO_570:
4598 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4599 TP_ACPI_VIDEO_570_PHSCMD))
4601 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4603 case TPACPI_VIDEO_770:
4604 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4607 status |= TP_ACPI_VIDEO_S_LCD;
4608 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4611 status |= TP_ACPI_VIDEO_S_CRT;
4613 case TPACPI_VIDEO_NEW:
4614 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4615 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4618 status |= TP_ACPI_VIDEO_S_CRT;
4620 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4621 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4624 status |= TP_ACPI_VIDEO_S_LCD;
4625 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4628 status |= TP_ACPI_VIDEO_S_DVI;
4637 static int video_outputsw_set(int status)
4642 switch (video_supported) {
4643 case TPACPI_VIDEO_570:
4644 res = acpi_evalf(NULL, NULL,
4645 "\\_SB.PHS2", "vdd",
4646 TP_ACPI_VIDEO_570_PHS2CMD,
4647 status | TP_ACPI_VIDEO_570_PHS2SET);
4649 case TPACPI_VIDEO_770:
4650 autosw = video_autosw_get();
4654 res = video_autosw_set(1);
4657 res = acpi_evalf(vid_handle, NULL,
4658 "ASWT", "vdd", status * 0x100, 0);
4659 if (!autosw && video_autosw_set(autosw)) {
4660 pr_err("video auto-switch left enabled due to error\n");
4664 case TPACPI_VIDEO_NEW:
4665 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4666 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4672 return (res)? 0 : -EIO;
4675 static int video_autosw_get(void)
4679 switch (video_supported) {
4680 case TPACPI_VIDEO_570:
4681 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4684 case TPACPI_VIDEO_770:
4685 case TPACPI_VIDEO_NEW:
4686 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4696 static int video_autosw_set(int enable)
4698 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4703 static int video_outputsw_cycle(void)
4705 int autosw = video_autosw_get();
4711 switch (video_supported) {
4712 case TPACPI_VIDEO_570:
4713 res = video_autosw_set(1);
4716 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4718 case TPACPI_VIDEO_770:
4719 case TPACPI_VIDEO_NEW:
4720 res = video_autosw_set(1);
4723 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4728 if (!autosw && video_autosw_set(autosw)) {
4729 pr_err("video auto-switch left enabled due to error\n");
4733 return (res)? 0 : -EIO;
4736 static int video_expand_toggle(void)
4738 switch (video_supported) {
4739 case TPACPI_VIDEO_570:
4740 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4742 case TPACPI_VIDEO_770:
4743 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4745 case TPACPI_VIDEO_NEW:
4746 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4754 static int video_read(struct seq_file *m)
4758 if (video_supported == TPACPI_VIDEO_NONE) {
4759 seq_printf(m, "status:\t\tnot supported\n");
4763 /* Even reads can crash X.org, so... */
4764 if (!capable(CAP_SYS_ADMIN))
4767 status = video_outputsw_get();
4771 autosw = video_autosw_get();
4775 seq_printf(m, "status:\t\tsupported\n");
4776 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4777 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4778 if (video_supported == TPACPI_VIDEO_NEW)
4779 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4780 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4781 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4782 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4783 if (video_supported == TPACPI_VIDEO_NEW)
4784 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4785 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4786 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4791 static int video_write(char *buf)
4794 int enable, disable, status;
4797 if (video_supported == TPACPI_VIDEO_NONE)
4800 /* Even reads can crash X.org, let alone writes... */
4801 if (!capable(CAP_SYS_ADMIN))
4807 while ((cmd = next_cmd(&buf))) {
4808 if (strlencmp(cmd, "lcd_enable") == 0) {
4809 enable |= TP_ACPI_VIDEO_S_LCD;
4810 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4811 disable |= TP_ACPI_VIDEO_S_LCD;
4812 } else if (strlencmp(cmd, "crt_enable") == 0) {
4813 enable |= TP_ACPI_VIDEO_S_CRT;
4814 } else if (strlencmp(cmd, "crt_disable") == 0) {
4815 disable |= TP_ACPI_VIDEO_S_CRT;
4816 } else if (video_supported == TPACPI_VIDEO_NEW &&
4817 strlencmp(cmd, "dvi_enable") == 0) {
4818 enable |= TP_ACPI_VIDEO_S_DVI;
4819 } else if (video_supported == TPACPI_VIDEO_NEW &&
4820 strlencmp(cmd, "dvi_disable") == 0) {
4821 disable |= TP_ACPI_VIDEO_S_DVI;
4822 } else if (strlencmp(cmd, "auto_enable") == 0) {
4823 res = video_autosw_set(1);
4826 } else if (strlencmp(cmd, "auto_disable") == 0) {
4827 res = video_autosw_set(0);
4830 } else if (strlencmp(cmd, "video_switch") == 0) {
4831 res = video_outputsw_cycle();
4834 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4835 res = video_expand_toggle();
4842 if (enable || disable) {
4843 status = video_outputsw_get();
4846 res = video_outputsw_set((status & ~disable) | enable);
4854 static struct ibm_struct video_driver_data = {
4857 .write = video_write,
4861 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4863 /*************************************************************************
4864 * Light (thinklight) subdriver
4867 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4868 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4870 static int light_get_status(void)
4874 if (tp_features.light_status) {
4875 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4883 static int light_set_status(int status)
4887 if (tp_features.light) {
4889 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4891 TP_CMOS_THINKLIGHT_ON :
4892 TP_CMOS_THINKLIGHT_OFF);
4894 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4897 return (rc)? 0 : -EIO;
4903 static void light_set_status_worker(struct work_struct *work)
4905 struct tpacpi_led_classdev *data =
4906 container_of(work, struct tpacpi_led_classdev, work);
4908 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4909 light_set_status((data->new_state != TPACPI_LED_OFF));
4912 static void light_sysfs_set(struct led_classdev *led_cdev,
4913 enum led_brightness brightness)
4915 struct tpacpi_led_classdev *data =
4916 container_of(led_cdev,
4917 struct tpacpi_led_classdev,
4919 data->new_state = (brightness != LED_OFF) ?
4920 TPACPI_LED_ON : TPACPI_LED_OFF;
4921 queue_work(tpacpi_wq, &data->work);
4924 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4926 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4929 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4931 .name = "tpacpi::thinklight",
4932 .brightness_set = &light_sysfs_set,
4933 .brightness_get = &light_sysfs_get,
4937 static int __init light_init(struct ibm_init_struct *iibm)
4941 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4943 if (tpacpi_is_ibm()) {
4944 TPACPI_ACPIHANDLE_INIT(ledb);
4945 TPACPI_ACPIHANDLE_INIT(lght);
4947 TPACPI_ACPIHANDLE_INIT(cmos);
4948 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4950 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4951 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4953 if (tp_features.light)
4954 /* light status not supported on
4955 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4956 tp_features.light_status =
4957 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4959 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4960 str_supported(tp_features.light),
4961 str_supported(tp_features.light_status));
4963 if (!tp_features.light)
4966 rc = led_classdev_register(&tpacpi_pdev->dev,
4967 &tpacpi_led_thinklight.led_classdev);
4970 tp_features.light = 0;
4971 tp_features.light_status = 0;
4979 static void light_exit(void)
4981 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4982 flush_workqueue(tpacpi_wq);
4985 static int light_read(struct seq_file *m)
4989 if (!tp_features.light) {
4990 seq_printf(m, "status:\t\tnot supported\n");
4991 } else if (!tp_features.light_status) {
4992 seq_printf(m, "status:\t\tunknown\n");
4993 seq_printf(m, "commands:\ton, off\n");
4995 status = light_get_status();
4998 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4999 seq_printf(m, "commands:\ton, off\n");
5005 static int light_write(char *buf)
5010 if (!tp_features.light)
5013 while ((cmd = next_cmd(&buf))) {
5014 if (strlencmp(cmd, "on") == 0) {
5016 } else if (strlencmp(cmd, "off") == 0) {
5022 return light_set_status(newstatus);
5025 static struct ibm_struct light_driver_data = {
5028 .write = light_write,
5032 /*************************************************************************
5036 /* sysfs cmos_command -------------------------------------------------- */
5037 static ssize_t cmos_command_store(struct device *dev,
5038 struct device_attribute *attr,
5039 const char *buf, size_t count)
5041 unsigned long cmos_cmd;
5044 if (parse_strtoul(buf, 21, &cmos_cmd))
5047 res = issue_thinkpad_cmos_command(cmos_cmd);
5048 return (res)? res : count;
5051 static struct device_attribute dev_attr_cmos_command =
5052 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
5054 /* --------------------------------------------------------------------- */
5056 static int __init cmos_init(struct ibm_init_struct *iibm)
5060 vdbg_printk(TPACPI_DBG_INIT,
5061 "initializing cmos commands subdriver\n");
5063 TPACPI_ACPIHANDLE_INIT(cmos);
5065 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5066 str_supported(cmos_handle != NULL));
5068 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5072 return (cmos_handle)? 0 : 1;
5075 static void cmos_exit(void)
5077 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5080 static int cmos_read(struct seq_file *m)
5082 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5083 R30, R31, T20-22, X20-21 */
5085 seq_printf(m, "status:\t\tnot supported\n");
5087 seq_printf(m, "status:\t\tsupported\n");
5088 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5094 static int cmos_write(char *buf)
5099 while ((cmd = next_cmd(&buf))) {
5100 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5101 cmos_cmd >= 0 && cmos_cmd <= 21) {
5106 res = issue_thinkpad_cmos_command(cmos_cmd);
5114 static struct ibm_struct cmos_driver_data = {
5117 .write = cmos_write,
5121 /*************************************************************************
5125 enum led_access_mode {
5126 TPACPI_LED_NONE = 0,
5127 TPACPI_LED_570, /* 570 */
5128 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5129 TPACPI_LED_NEW, /* all others */
5132 enum { /* For TPACPI_LED_OLD */
5133 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5134 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5135 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5138 static enum led_access_mode led_supported;
5140 static acpi_handle led_handle;
5142 #define TPACPI_LED_NUMLEDS 16
5143 static struct tpacpi_led_classdev *tpacpi_leds;
5144 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5145 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5146 /* there's a limit of 19 chars + NULL before 2.6.26 */
5148 "tpacpi:orange:batt",
5149 "tpacpi:green:batt",
5150 "tpacpi::dock_active",
5151 "tpacpi::bay_active",
5152 "tpacpi::dock_batt",
5153 "tpacpi::unknown_led",
5155 "tpacpi::dock_status1",
5156 "tpacpi::dock_status2",
5157 "tpacpi::unknown_led2",
5158 "tpacpi::unknown_led3",
5159 "tpacpi::thinkvantage",
5161 #define TPACPI_SAFE_LEDS 0x1081U
5163 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5165 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5168 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5172 static int led_get_status(const unsigned int led)
5175 enum led_status_t led_s;
5177 switch (led_supported) {
5178 case TPACPI_LED_570:
5179 if (!acpi_evalf(ec_handle,
5180 &status, "GLED", "dd", 1 << led))
5182 led_s = (status == 0)?
5187 tpacpi_led_state_cache[led] = led_s;
5196 static int led_set_status(const unsigned int led,
5197 const enum led_status_t ledstatus)
5199 /* off, on, blink. Index is led_status_t */
5200 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5201 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5205 switch (led_supported) {
5206 case TPACPI_LED_570:
5208 if (unlikely(led > 7))
5210 if (unlikely(tpacpi_is_led_restricted(led)))
5212 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5213 (1 << led), led_sled_arg1[ledstatus]))
5216 case TPACPI_LED_OLD:
5217 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5218 if (unlikely(led > 7))
5220 if (unlikely(tpacpi_is_led_restricted(led)))
5222 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5224 rc = ec_write(TPACPI_LED_EC_HLBL,
5225 (ledstatus == TPACPI_LED_BLINK) << led);
5227 rc = ec_write(TPACPI_LED_EC_HLCL,
5228 (ledstatus != TPACPI_LED_OFF) << led);
5230 case TPACPI_LED_NEW:
5232 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5234 if (unlikely(tpacpi_is_led_restricted(led)))
5236 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5237 led, led_led_arg1[ledstatus]))
5245 tpacpi_led_state_cache[led] = ledstatus;
5250 static void led_set_status_worker(struct work_struct *work)
5252 struct tpacpi_led_classdev *data =
5253 container_of(work, struct tpacpi_led_classdev, work);
5255 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5256 led_set_status(data->led, data->new_state);
5259 static void led_sysfs_set(struct led_classdev *led_cdev,
5260 enum led_brightness brightness)
5262 struct tpacpi_led_classdev *data = container_of(led_cdev,
5263 struct tpacpi_led_classdev, led_classdev);
5265 if (brightness == LED_OFF)
5266 data->new_state = TPACPI_LED_OFF;
5267 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5268 data->new_state = TPACPI_LED_ON;
5270 data->new_state = TPACPI_LED_BLINK;
5272 queue_work(tpacpi_wq, &data->work);
5275 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5276 unsigned long *delay_on, unsigned long *delay_off)
5278 struct tpacpi_led_classdev *data = container_of(led_cdev,
5279 struct tpacpi_led_classdev, led_classdev);
5281 /* Can we choose the flash rate? */
5282 if (*delay_on == 0 && *delay_off == 0) {
5283 /* yes. set them to the hardware blink rate (1 Hz) */
5284 *delay_on = 500; /* ms */
5285 *delay_off = 500; /* ms */
5286 } else if ((*delay_on != 500) || (*delay_off != 500))
5289 data->new_state = TPACPI_LED_BLINK;
5290 queue_work(tpacpi_wq, &data->work);
5295 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5299 struct tpacpi_led_classdev *data = container_of(led_cdev,
5300 struct tpacpi_led_classdev, led_classdev);
5302 rc = led_get_status(data->led);
5304 if (rc == TPACPI_LED_OFF || rc < 0)
5305 rc = LED_OFF; /* no error handling in led class :( */
5312 static void led_exit(void)
5316 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5317 if (tpacpi_leds[i].led_classdev.name)
5318 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5321 flush_workqueue(tpacpi_wq);
5325 static int __init tpacpi_init_led(unsigned int led)
5329 tpacpi_leds[led].led = led;
5331 /* LEDs with no name don't get registered */
5332 if (!tpacpi_led_names[led])
5335 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5336 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5337 if (led_supported == TPACPI_LED_570)
5338 tpacpi_leds[led].led_classdev.brightness_get =
5341 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5343 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5345 rc = led_classdev_register(&tpacpi_pdev->dev,
5346 &tpacpi_leds[led].led_classdev);
5348 tpacpi_leds[led].led_classdev.name = NULL;
5353 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5354 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5355 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5356 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5358 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5359 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5360 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5361 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5362 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5363 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5364 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5365 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5367 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5368 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5369 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5370 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5371 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5373 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5374 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5375 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5376 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5378 /* (1) - may have excess leds enabled on MSB */
5380 /* Defaults (order matters, keep last, don't reorder!) */
5382 .vendor = PCI_VENDOR_ID_LENOVO,
5383 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5386 { /* IBM ThinkPads with no EC version string */
5387 .vendor = PCI_VENDOR_ID_IBM,
5388 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5391 { /* IBM ThinkPads with EC version string */
5392 .vendor = PCI_VENDOR_ID_IBM,
5393 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5398 #undef TPACPI_LEDQ_IBM
5399 #undef TPACPI_LEDQ_LNV
5401 static enum led_access_mode __init led_init_detect_mode(void)
5405 if (tpacpi_is_ibm()) {
5407 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5408 if (ACPI_SUCCESS(status))
5409 return TPACPI_LED_570;
5411 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5412 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5413 if (ACPI_SUCCESS(status))
5414 return TPACPI_LED_OLD;
5418 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5419 if (ACPI_SUCCESS(status))
5420 return TPACPI_LED_NEW;
5422 /* R30, R31, and unknown firmwares */
5424 return TPACPI_LED_NONE;
5427 static int __init led_init(struct ibm_init_struct *iibm)
5431 unsigned long useful_leds;
5433 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5435 led_supported = led_init_detect_mode();
5437 if (led_supported != TPACPI_LED_NONE) {
5438 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5439 ARRAY_SIZE(led_useful_qtable));
5443 led_supported = TPACPI_LED_NONE;
5447 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5448 str_supported(led_supported), led_supported);
5450 if (led_supported == TPACPI_LED_NONE)
5453 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5456 pr_err("Out of memory for LED data\n");
5460 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5461 tpacpi_leds[i].led = -1;
5463 if (!tpacpi_is_led_restricted(i) &&
5464 test_bit(i, &useful_leds)) {
5465 rc = tpacpi_init_led(i);
5473 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5474 pr_notice("warning: userspace override of important "
5475 "firmware LEDs is enabled\n");
5480 #define str_led_status(s) \
5481 ((s) == TPACPI_LED_OFF ? "off" : \
5482 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5484 static int led_read(struct seq_file *m)
5486 if (!led_supported) {
5487 seq_printf(m, "status:\t\tnot supported\n");
5490 seq_printf(m, "status:\t\tsupported\n");
5492 if (led_supported == TPACPI_LED_570) {
5495 for (i = 0; i < 8; i++) {
5496 status = led_get_status(i);
5499 seq_printf(m, "%d:\t\t%s\n",
5500 i, str_led_status(status));
5504 seq_printf(m, "commands:\t"
5505 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5510 static int led_write(char *buf)
5514 enum led_status_t s;
5519 while ((cmd = next_cmd(&buf))) {
5520 if (sscanf(cmd, "%d", &led) != 1)
5523 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5524 tpacpi_leds[led].led < 0)
5527 if (strstr(cmd, "off")) {
5529 } else if (strstr(cmd, "on")) {
5531 } else if (strstr(cmd, "blink")) {
5532 s = TPACPI_LED_BLINK;
5537 rc = led_set_status(led, s);
5545 static struct ibm_struct led_driver_data = {
5552 /*************************************************************************
5556 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5558 #define TPACPI_BEEP_Q1 0x0001
5560 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5561 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5562 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5565 static int __init beep_init(struct ibm_init_struct *iibm)
5567 unsigned long quirks;
5569 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5571 TPACPI_ACPIHANDLE_INIT(beep);
5573 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5574 str_supported(beep_handle != NULL));
5576 quirks = tpacpi_check_quirks(beep_quirk_table,
5577 ARRAY_SIZE(beep_quirk_table));
5579 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5581 return (beep_handle)? 0 : 1;
5584 static int beep_read(struct seq_file *m)
5587 seq_printf(m, "status:\t\tnot supported\n");
5589 seq_printf(m, "status:\t\tsupported\n");
5590 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5596 static int beep_write(char *buf)
5604 while ((cmd = next_cmd(&buf))) {
5605 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5606 beep_cmd >= 0 && beep_cmd <= 17) {
5610 if (tp_features.beep_needs_two_args) {
5611 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5615 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5624 static struct ibm_struct beep_driver_data = {
5627 .write = beep_write,
5630 /*************************************************************************
5634 enum thermal_access_mode {
5635 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5636 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5637 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5638 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5639 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5642 enum { /* TPACPI_THERMAL_TPEC_* */
5643 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5644 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5645 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5647 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5651 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5652 struct ibm_thermal_sensors_struct {
5653 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5656 static enum thermal_access_mode thermal_read_mode;
5658 /* idx is zero-based */
5659 static int thermal_get_sensor(int idx, s32 *value)
5665 t = TP_EC_THERMAL_TMP0;
5667 switch (thermal_read_mode) {
5668 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5669 case TPACPI_THERMAL_TPEC_16:
5670 if (idx >= 8 && idx <= 15) {
5671 t = TP_EC_THERMAL_TMP8;
5676 case TPACPI_THERMAL_TPEC_8:
5678 if (!acpi_ec_read(t + idx, &tmp))
5680 *value = tmp * 1000;
5685 case TPACPI_THERMAL_ACPI_UPDT:
5687 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5688 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5690 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5692 *value = (t - 2732) * 100;
5697 case TPACPI_THERMAL_ACPI_TMP07:
5699 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5700 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5702 if (t > 127 || t < -127)
5703 t = TP_EC_THERMAL_TMP_NA;
5709 case TPACPI_THERMAL_NONE:
5717 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5728 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5731 for (i = 0 ; i < n; i++) {
5732 res = thermal_get_sensor(i, &s->temp[i]);
5740 static void thermal_dump_all_sensors(void)
5743 struct ibm_thermal_sensors_struct t;
5745 n = thermal_get_sensors(&t);
5749 pr_notice("temperatures (Celsius):");
5751 for (i = 0; i < n; i++) {
5752 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5753 pr_cont(" %d", (int)(t.temp[i] / 1000));
5761 /* sysfs temp##_input -------------------------------------------------- */
5763 static ssize_t thermal_temp_input_show(struct device *dev,
5764 struct device_attribute *attr,
5767 struct sensor_device_attribute *sensor_attr =
5768 to_sensor_dev_attr(attr);
5769 int idx = sensor_attr->index;
5773 res = thermal_get_sensor(idx, &value);
5776 if (value == TPACPI_THERMAL_SENSOR_NA)
5779 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5782 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5783 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5784 thermal_temp_input_show, NULL, _idxB)
5786 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5787 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5788 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5789 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5790 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5791 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5792 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5793 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5794 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5795 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5796 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5797 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5798 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5799 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5800 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5801 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5802 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5805 #define THERMAL_ATTRS(X) \
5806 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5808 static struct attribute *thermal_temp_input_attr[] = {
5828 static const struct attribute_group thermal_temp_input16_group = {
5829 .attrs = thermal_temp_input_attr
5832 static const struct attribute_group thermal_temp_input8_group = {
5833 .attrs = &thermal_temp_input_attr[8]
5836 #undef THERMAL_SENSOR_ATTR_TEMP
5837 #undef THERMAL_ATTRS
5839 /* --------------------------------------------------------------------- */
5841 static int __init thermal_init(struct ibm_init_struct *iibm)
5848 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5850 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5852 if (thinkpad_id.ec_model) {
5854 * Direct EC access mode: sensors at registers
5855 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5856 * non-implemented, thermal sensors return 0x80 when
5861 for (i = 0; i < 8; i++) {
5862 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5868 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5876 /* This is sheer paranoia, but we handle it anyway */
5878 pr_err("ThinkPad ACPI EC access misbehaving, "
5879 "falling back to ACPI TMPx access "
5881 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5883 pr_err("ThinkPad ACPI EC access misbehaving, "
5884 "disabling thermal sensors access\n");
5885 thermal_read_mode = TPACPI_THERMAL_NONE;
5890 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5892 } else if (acpi_tmp7) {
5893 if (tpacpi_is_ibm() &&
5894 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5895 /* 600e/x, 770e, 770x */
5896 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5898 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5899 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5902 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5903 thermal_read_mode = TPACPI_THERMAL_NONE;
5906 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5907 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5910 switch (thermal_read_mode) {
5911 case TPACPI_THERMAL_TPEC_16:
5912 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5913 &thermal_temp_input16_group);
5917 case TPACPI_THERMAL_TPEC_8:
5918 case TPACPI_THERMAL_ACPI_TMP07:
5919 case TPACPI_THERMAL_ACPI_UPDT:
5920 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5921 &thermal_temp_input8_group);
5925 case TPACPI_THERMAL_NONE:
5933 static void thermal_exit(void)
5935 switch (thermal_read_mode) {
5936 case TPACPI_THERMAL_TPEC_16:
5937 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5938 &thermal_temp_input16_group);
5940 case TPACPI_THERMAL_TPEC_8:
5941 case TPACPI_THERMAL_ACPI_TMP07:
5942 case TPACPI_THERMAL_ACPI_UPDT:
5943 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5944 &thermal_temp_input8_group);
5946 case TPACPI_THERMAL_NONE:
5952 static int thermal_read(struct seq_file *m)
5955 struct ibm_thermal_sensors_struct t;
5957 n = thermal_get_sensors(&t);
5958 if (unlikely(n < 0))
5961 seq_printf(m, "temperatures:\t");
5964 for (i = 0; i < (n - 1); i++)
5965 seq_printf(m, "%d ", t.temp[i] / 1000);
5966 seq_printf(m, "%d\n", t.temp[i] / 1000);
5968 seq_printf(m, "not supported\n");
5973 static struct ibm_struct thermal_driver_data = {
5975 .read = thermal_read,
5976 .exit = thermal_exit,
5979 /*************************************************************************
5980 * Backlight/brightness subdriver
5983 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5986 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5987 * CMOS NVRAM byte 0x5E, bits 0-3.
5989 * EC HBRV (0x31) has the following layout
5990 * Bit 7: unknown function
5991 * Bit 6: unknown function
5992 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5993 * Bit 4: must be set to zero to avoid problems
5994 * Bit 3-0: backlight brightness level
5996 * brightness_get_raw returns status data in the HBRV layout
5998 * WARNING: The X61 has been verified to use HBRV for something else, so
5999 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6000 * testing on the very early *60 Lenovo models...
6004 TP_EC_BACKLIGHT = 0x31,
6006 /* TP_EC_BACKLIGHT bitmasks */
6007 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6008 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6009 TP_EC_BACKLIGHT_MAPSW = 0x20,
6012 enum tpacpi_brightness_access_mode {
6013 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6014 TPACPI_BRGHT_MODE_EC, /* EC control */
6015 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6016 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6017 TPACPI_BRGHT_MODE_MAX
6020 static struct backlight_device *ibm_backlight_device;
6022 static enum tpacpi_brightness_access_mode brightness_mode =
6023 TPACPI_BRGHT_MODE_MAX;
6025 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6027 static struct mutex brightness_mutex;
6029 /* NVRAM brightness access,
6030 * call with brightness_mutex held! */
6031 static unsigned int tpacpi_brightness_nvram_get(void)
6035 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6036 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6037 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6038 lnvram &= bright_maxlvl;
6043 static void tpacpi_brightness_checkpoint_nvram(void)
6048 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6051 vdbg_printk(TPACPI_DBG_BRGHT,
6052 "trying to checkpoint backlight level to NVRAM...\n");
6054 if (mutex_lock_killable(&brightness_mutex) < 0)
6057 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6059 lec &= TP_EC_BACKLIGHT_LVLMSK;
6060 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6062 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6063 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6064 /* NVRAM needs update */
6065 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6066 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6068 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6069 dbg_printk(TPACPI_DBG_BRGHT,
6070 "updated NVRAM backlight level to %u (0x%02x)\n",
6071 (unsigned int) lec, (unsigned int) b_nvram);
6073 vdbg_printk(TPACPI_DBG_BRGHT,
6074 "NVRAM backlight level already is %u (0x%02x)\n",
6075 (unsigned int) lec, (unsigned int) b_nvram);
6078 mutex_unlock(&brightness_mutex);
6082 /* call with brightness_mutex held! */
6083 static int tpacpi_brightness_get_raw(int *status)
6087 switch (brightness_mode) {
6088 case TPACPI_BRGHT_MODE_UCMS_STEP:
6089 *status = tpacpi_brightness_nvram_get();
6091 case TPACPI_BRGHT_MODE_EC:
6092 case TPACPI_BRGHT_MODE_ECNVRAM:
6093 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6102 /* call with brightness_mutex held! */
6103 /* do NOT call with illegal backlight level value */
6104 static int tpacpi_brightness_set_ec(unsigned int value)
6108 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6111 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6112 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6113 (value & TP_EC_BACKLIGHT_LVLMSK))))
6119 /* call with brightness_mutex held! */
6120 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6123 unsigned int current_value, i;
6125 current_value = tpacpi_brightness_nvram_get();
6127 if (value == current_value)
6130 cmos_cmd = (value > current_value) ?
6131 TP_CMOS_BRIGHTNESS_UP :
6132 TP_CMOS_BRIGHTNESS_DOWN;
6133 inc = (value > current_value) ? 1 : -1;
6135 for (i = current_value; i != value; i += inc)
6136 if (issue_thinkpad_cmos_command(cmos_cmd))
6142 /* May return EINTR which can always be mapped to ERESTARTSYS */
6143 static int brightness_set(unsigned int value)
6147 if (value > bright_maxlvl || value < 0)
6150 vdbg_printk(TPACPI_DBG_BRGHT,
6151 "set backlight level to %d\n", value);
6153 res = mutex_lock_killable(&brightness_mutex);
6157 switch (brightness_mode) {
6158 case TPACPI_BRGHT_MODE_EC:
6159 case TPACPI_BRGHT_MODE_ECNVRAM:
6160 res = tpacpi_brightness_set_ec(value);
6162 case TPACPI_BRGHT_MODE_UCMS_STEP:
6163 res = tpacpi_brightness_set_ucmsstep(value);
6169 mutex_unlock(&brightness_mutex);
6173 /* sysfs backlight class ----------------------------------------------- */
6175 static int brightness_update_status(struct backlight_device *bd)
6177 unsigned int level =
6178 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6179 bd->props.power == FB_BLANK_UNBLANK) ?
6180 bd->props.brightness : 0;
6182 dbg_printk(TPACPI_DBG_BRGHT,
6183 "backlight: attempt to set level to %d\n",
6186 /* it is the backlight class's job (caller) to handle
6187 * EINTR and other errors properly */
6188 return brightness_set(level);
6191 static int brightness_get(struct backlight_device *bd)
6195 res = mutex_lock_killable(&brightness_mutex);
6199 res = tpacpi_brightness_get_raw(&status);
6201 mutex_unlock(&brightness_mutex);
6206 return status & TP_EC_BACKLIGHT_LVLMSK;
6209 static void tpacpi_brightness_notify_change(void)
6211 backlight_force_update(ibm_backlight_device,
6212 BACKLIGHT_UPDATE_HOTKEY);
6215 static const struct backlight_ops ibm_backlight_data = {
6216 .get_brightness = brightness_get,
6217 .update_status = brightness_update_status,
6220 /* --------------------------------------------------------------------- */
6223 * Call _BCL method of video device. On some ThinkPads this will
6224 * switch the firmware to the ACPI brightness control mode.
6227 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6229 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6230 union acpi_object *obj;
6231 struct acpi_device *device, *child;
6234 if (acpi_bus_get_device(handle, &device))
6238 list_for_each_entry(child, &device->children, node) {
6239 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6241 if (ACPI_FAILURE(status))
6244 obj = (union acpi_object *)buffer.pointer;
6245 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6246 pr_err("Unknown _BCL data, please report this to %s\n",
6250 rc = obj->package.count;
6255 kfree(buffer.pointer);
6261 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6263 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6265 acpi_handle video_device;
6268 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6270 bcl_levels = tpacpi_query_bcl_levels(video_device);
6272 tp_features.bright_acpimode = (bcl_levels > 0);
6274 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6278 * These are only useful for models that have only one possibility
6279 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6282 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6283 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6284 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6286 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6287 /* Models with ATI GPUs known to require ECNVRAM mode */
6288 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6290 /* Models with ATI GPUs that can use ECNVRAM */
6291 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6292 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6293 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6294 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6296 /* Models with Intel Extreme Graphics 2 */
6297 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6298 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6299 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6301 /* Models with Intel GMA900 */
6302 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6303 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6304 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6308 * Returns < 0 for error, otherwise sets tp_features.bright_*
6309 * and bright_maxlvl.
6311 static void __init tpacpi_detect_brightness_capabilities(void)
6315 vdbg_printk(TPACPI_DBG_INIT,
6316 "detecting firmware brightness interface capabilities\n");
6318 /* we could run a quirks check here (same table used by
6319 * brightness_init) if needed */
6322 * We always attempt to detect acpi support, so as to switch
6323 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6324 * going to publish a backlight interface
6326 b = tpacpi_check_std_acpi_brightness_support();
6330 pr_info("detected a 16-level brightness capable ThinkPad\n");
6335 pr_info("detected a 8-level brightness capable ThinkPad\n");
6338 pr_err("Unsupported brightness interface, "
6339 "please contact %s\n", TPACPI_MAIL);
6340 tp_features.bright_unkfw = 1;
6341 bright_maxlvl = b - 1;
6345 static int __init brightness_init(struct ibm_init_struct *iibm)
6347 struct backlight_properties props;
6349 unsigned long quirks;
6351 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6353 mutex_init(&brightness_mutex);
6355 quirks = tpacpi_check_quirks(brightness_quirk_table,
6356 ARRAY_SIZE(brightness_quirk_table));
6358 /* tpacpi_detect_brightness_capabilities() must have run already */
6360 /* if it is unknown, we don't handle it: it wouldn't be safe */
6361 if (tp_features.bright_unkfw)
6364 if (!brightness_enable) {
6365 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6366 "brightness support disabled by "
6367 "module parameter\n");
6371 if (acpi_video_backlight_support()) {
6372 if (brightness_enable > 1) {
6373 pr_info("Standard ACPI backlight interface "
6374 "available, not loading native one\n");
6376 } else if (brightness_enable == 1) {
6377 pr_warn("Cannot enable backlight brightness support, "
6378 "ACPI is already handling it. Refer to the "
6379 "acpi_backlight kernel parameter.\n");
6382 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6383 pr_notice("Standard ACPI backlight interface not "
6384 "available, thinkpad_acpi native "
6385 "brightness control enabled\n");
6389 * Check for module parameter bogosity, note that we
6390 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6391 * able to detect "unspecified"
6393 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6396 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6397 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6398 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6399 if (quirks & TPACPI_BRGHT_Q_EC)
6400 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6402 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6404 dbg_printk(TPACPI_DBG_BRGHT,
6405 "driver auto-selected brightness_mode=%d\n",
6410 if (!tpacpi_is_ibm() &&
6411 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6412 brightness_mode == TPACPI_BRGHT_MODE_EC))
6415 if (tpacpi_brightness_get_raw(&b) < 0)
6418 memset(&props, 0, sizeof(struct backlight_properties));
6419 props.type = BACKLIGHT_PLATFORM;
6420 props.max_brightness = bright_maxlvl;
6421 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6422 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6424 &ibm_backlight_data,
6426 if (IS_ERR(ibm_backlight_device)) {
6427 int rc = PTR_ERR(ibm_backlight_device);
6428 ibm_backlight_device = NULL;
6429 pr_err("Could not register backlight device\n");
6432 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6433 "brightness is supported\n");
6435 if (quirks & TPACPI_BRGHT_Q_ASK) {
6436 pr_notice("brightness: will use unverified default: "
6437 "brightness_mode=%d\n", brightness_mode);
6438 pr_notice("brightness: please report to %s whether it works well "
6439 "or not on your ThinkPad\n", TPACPI_MAIL);
6442 /* Added by mistake in early 2007. Probably useless, but it could
6443 * be working around some unknown firmware problem where the value
6444 * read at startup doesn't match the real hardware state... so leave
6445 * it in place just in case */
6446 backlight_update_status(ibm_backlight_device);
6448 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6449 "brightness: registering brightness hotkeys "
6450 "as change notification\n");
6451 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6452 | TP_ACPI_HKEY_BRGHTUP_MASK
6453 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6457 static void brightness_suspend(void)
6459 tpacpi_brightness_checkpoint_nvram();
6462 static void brightness_shutdown(void)
6464 tpacpi_brightness_checkpoint_nvram();
6467 static void brightness_exit(void)
6469 if (ibm_backlight_device) {
6470 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6471 "calling backlight_device_unregister()\n");
6472 backlight_device_unregister(ibm_backlight_device);
6475 tpacpi_brightness_checkpoint_nvram();
6478 static int brightness_read(struct seq_file *m)
6482 level = brightness_get(NULL);
6484 seq_printf(m, "level:\t\tunreadable\n");
6486 seq_printf(m, "level:\t\t%d\n", level);
6487 seq_printf(m, "commands:\tup, down\n");
6488 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6495 static int brightness_write(char *buf)
6501 level = brightness_get(NULL);
6505 while ((cmd = next_cmd(&buf))) {
6506 if (strlencmp(cmd, "up") == 0) {
6507 if (level < bright_maxlvl)
6509 } else if (strlencmp(cmd, "down") == 0) {
6512 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6513 level >= 0 && level <= bright_maxlvl) {
6519 tpacpi_disclose_usertask("procfs brightness",
6520 "set level to %d\n", level);
6523 * Now we know what the final level should be, so we try to set it.
6524 * Doing it this way makes the syscall restartable in case of EINTR
6526 rc = brightness_set(level);
6527 if (!rc && ibm_backlight_device)
6528 backlight_force_update(ibm_backlight_device,
6529 BACKLIGHT_UPDATE_SYSFS);
6530 return (rc == -EINTR)? -ERESTARTSYS : rc;
6533 static struct ibm_struct brightness_driver_data = {
6534 .name = "brightness",
6535 .read = brightness_read,
6536 .write = brightness_write,
6537 .exit = brightness_exit,
6538 .suspend = brightness_suspend,
6539 .shutdown = brightness_shutdown,
6542 /*************************************************************************
6547 * IBM ThinkPads have a simple volume controller with MUTE gating.
6548 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6550 * Since the *61 series (and probably also the later *60 series), Lenovo
6551 * ThinkPads only implement the MUTE gate.
6554 * Bit 6: MUTE (1 mutes sound)
6556 * Other bits should be zero as far as we know.
6558 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6559 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6560 * such as bit 7 which is used to detect repeated presses of MUTE,
6561 * and we leave them unchanged.
6564 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6566 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6567 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6568 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6570 #if SNDRV_CARDS <= 32
6571 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
6573 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
6575 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
6576 static char *alsa_id = "ThinkPadEC";
6577 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
6579 struct tpacpi_alsa_data {
6580 struct snd_card *card;
6581 struct snd_ctl_elem_id *ctl_mute_id;
6582 struct snd_ctl_elem_id *ctl_vol_id;
6585 static struct snd_card *alsa_card;
6590 /* TP_EC_AUDIO bits */
6591 TP_EC_AUDIO_MUTESW = 6,
6593 /* TP_EC_AUDIO bitmasks */
6594 TP_EC_AUDIO_LVL_MSK = 0x0F,
6595 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6597 /* Maximum volume */
6598 TP_EC_VOLUME_MAX = 14,
6601 enum tpacpi_volume_access_mode {
6602 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6603 TPACPI_VOL_MODE_EC, /* Pure EC control */
6604 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6605 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6609 enum tpacpi_volume_capabilities {
6610 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6611 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6612 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6616 static enum tpacpi_volume_access_mode volume_mode =
6617 TPACPI_VOL_MODE_MAX;
6619 static enum tpacpi_volume_capabilities volume_capabilities;
6620 static bool volume_control_allowed;
6623 * Used to syncronize writers to TP_EC_AUDIO and
6624 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6626 static struct mutex volume_mutex;
6628 static void tpacpi_volume_checkpoint_nvram(void)
6634 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6636 if (!volume_control_allowed)
6639 vdbg_printk(TPACPI_DBG_MIXER,
6640 "trying to checkpoint mixer state to NVRAM...\n");
6642 if (tp_features.mixer_no_level_control)
6643 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6645 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6647 if (mutex_lock_killable(&volume_mutex) < 0)
6650 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6653 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6655 if (lec != (b_nvram & ec_mask)) {
6656 /* NVRAM needs update */
6657 b_nvram &= ~ec_mask;
6659 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6660 dbg_printk(TPACPI_DBG_MIXER,
6661 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6662 (unsigned int) lec, (unsigned int) b_nvram);
6664 vdbg_printk(TPACPI_DBG_MIXER,
6665 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6666 (unsigned int) lec, (unsigned int) b_nvram);
6670 mutex_unlock(&volume_mutex);
6673 static int volume_get_status_ec(u8 *status)
6677 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6682 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6687 static int volume_get_status(u8 *status)
6689 return volume_get_status_ec(status);
6692 static int volume_set_status_ec(const u8 status)
6694 if (!acpi_ec_write(TP_EC_AUDIO, status))
6697 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6702 static int volume_set_status(const u8 status)
6704 return volume_set_status_ec(status);
6707 /* returns < 0 on error, 0 on no change, 1 on change */
6708 static int __volume_set_mute_ec(const bool mute)
6713 if (mutex_lock_killable(&volume_mutex) < 0)
6716 rc = volume_get_status_ec(&s);
6720 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6721 s & ~TP_EC_AUDIO_MUTESW_MSK;
6724 rc = volume_set_status_ec(n);
6730 mutex_unlock(&volume_mutex);
6734 static int volume_alsa_set_mute(const bool mute)
6736 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6737 (mute) ? "" : "un");
6738 return __volume_set_mute_ec(mute);
6741 static int volume_set_mute(const bool mute)
6745 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6746 (mute) ? "" : "un");
6748 rc = __volume_set_mute_ec(mute);
6749 return (rc < 0) ? rc : 0;
6752 /* returns < 0 on error, 0 on no change, 1 on change */
6753 static int __volume_set_volume_ec(const u8 vol)
6758 if (vol > TP_EC_VOLUME_MAX)
6761 if (mutex_lock_killable(&volume_mutex) < 0)
6764 rc = volume_get_status_ec(&s);
6768 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6771 rc = volume_set_status_ec(n);
6777 mutex_unlock(&volume_mutex);
6781 static int volume_alsa_set_volume(const u8 vol)
6783 dbg_printk(TPACPI_DBG_MIXER,
6784 "ALSA: trying to set volume level to %hu\n", vol);
6785 return __volume_set_volume_ec(vol);
6788 static void volume_alsa_notify_change(void)
6790 struct tpacpi_alsa_data *d;
6792 if (alsa_card && alsa_card->private_data) {
6793 d = alsa_card->private_data;
6795 snd_ctl_notify(alsa_card,
6796 SNDRV_CTL_EVENT_MASK_VALUE,
6799 snd_ctl_notify(alsa_card,
6800 SNDRV_CTL_EVENT_MASK_VALUE,
6805 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6806 struct snd_ctl_elem_info *uinfo)
6808 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6810 uinfo->value.integer.min = 0;
6811 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6815 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6816 struct snd_ctl_elem_value *ucontrol)
6821 rc = volume_get_status(&s);
6825 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6829 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6830 struct snd_ctl_elem_value *ucontrol)
6832 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6833 ucontrol->value.integer.value[0]);
6834 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6837 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6839 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6840 struct snd_ctl_elem_value *ucontrol)
6845 rc = volume_get_status(&s);
6849 ucontrol->value.integer.value[0] =
6850 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6854 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6855 struct snd_ctl_elem_value *ucontrol)
6857 tpacpi_disclose_usertask("ALSA", "%smute\n",
6858 ucontrol->value.integer.value[0] ?
6860 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6863 static struct snd_kcontrol_new volume_alsa_control_vol = {
6864 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6865 .name = "Console Playback Volume",
6867 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6868 .info = volume_alsa_vol_info,
6869 .get = volume_alsa_vol_get,
6872 static struct snd_kcontrol_new volume_alsa_control_mute = {
6873 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6874 .name = "Console Playback Switch",
6876 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6877 .info = volume_alsa_mute_info,
6878 .get = volume_alsa_mute_get,
6881 static void volume_suspend(void)
6883 tpacpi_volume_checkpoint_nvram();
6886 static void volume_resume(void)
6888 volume_alsa_notify_change();
6891 static void volume_shutdown(void)
6893 tpacpi_volume_checkpoint_nvram();
6896 static void volume_exit(void)
6899 snd_card_free(alsa_card);
6903 tpacpi_volume_checkpoint_nvram();
6906 static int __init volume_create_alsa_mixer(void)
6908 struct snd_card *card;
6909 struct tpacpi_alsa_data *data;
6910 struct snd_kcontrol *ctl_vol;
6911 struct snd_kcontrol *ctl_mute;
6914 rc = snd_card_new(&tpacpi_pdev->dev,
6915 alsa_index, alsa_id, THIS_MODULE,
6916 sizeof(struct tpacpi_alsa_data), &card);
6917 if (rc < 0 || !card) {
6918 pr_err("Failed to create ALSA card structures: %d\n", rc);
6922 BUG_ON(!card->private_data);
6923 data = card->private_data;
6926 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6927 sizeof(card->driver));
6928 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6929 sizeof(card->shortname));
6930 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6931 (thinkpad_id.ec_version_str) ?
6932 thinkpad_id.ec_version_str : "(unknown)");
6933 snprintf(card->longname, sizeof(card->longname),
6934 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6935 (thinkpad_id.ec_version_str) ?
6936 thinkpad_id.ec_version_str : "unknown");
6938 if (volume_control_allowed) {
6939 volume_alsa_control_vol.put = volume_alsa_vol_put;
6940 volume_alsa_control_vol.access =
6941 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6943 volume_alsa_control_mute.put = volume_alsa_mute_put;
6944 volume_alsa_control_mute.access =
6945 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6948 if (!tp_features.mixer_no_level_control) {
6949 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6950 rc = snd_ctl_add(card, ctl_vol);
6952 pr_err("Failed to create ALSA volume control: %d\n",
6956 data->ctl_vol_id = &ctl_vol->id;
6959 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6960 rc = snd_ctl_add(card, ctl_mute);
6962 pr_err("Failed to create ALSA mute control: %d\n", rc);
6965 data->ctl_mute_id = &ctl_mute->id;
6967 rc = snd_card_register(card);
6969 pr_err("Failed to register ALSA card: %d\n", rc);
6977 snd_card_free(card);
6981 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6982 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6984 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6985 /* Whitelist volume level on all IBM by default */
6986 { .vendor = PCI_VENDOR_ID_IBM,
6987 .bios = TPACPI_MATCH_ANY,
6988 .ec = TPACPI_MATCH_ANY,
6989 .quirks = TPACPI_VOL_Q_LEVEL },
6991 /* Lenovo models with volume control (needs confirmation) */
6992 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6993 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6994 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6995 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6996 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6997 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6998 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7000 /* Whitelist mute-only on all Lenovo by default */
7001 { .vendor = PCI_VENDOR_ID_LENOVO,
7002 .bios = TPACPI_MATCH_ANY,
7003 .ec = TPACPI_MATCH_ANY,
7004 .quirks = TPACPI_VOL_Q_MUTEONLY }
7007 static int __init volume_init(struct ibm_init_struct *iibm)
7009 unsigned long quirks;
7012 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7014 mutex_init(&volume_mutex);
7017 * Check for module parameter bogosity, note that we
7018 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7019 * able to detect "unspecified"
7021 if (volume_mode > TPACPI_VOL_MODE_MAX)
7024 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7025 pr_err("UCMS step volume mode not implemented, "
7026 "please contact %s\n", TPACPI_MAIL);
7030 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7034 * The ALSA mixer is our primary interface.
7035 * When disabled, don't install the subdriver at all
7038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7039 "ALSA mixer disabled by parameter, "
7040 "not loading volume subdriver...\n");
7044 quirks = tpacpi_check_quirks(volume_quirk_table,
7045 ARRAY_SIZE(volume_quirk_table));
7047 switch (volume_capabilities) {
7048 case TPACPI_VOL_CAP_AUTO:
7049 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7050 tp_features.mixer_no_level_control = 1;
7051 else if (quirks & TPACPI_VOL_Q_LEVEL)
7052 tp_features.mixer_no_level_control = 0;
7054 return 1; /* no mixer */
7056 case TPACPI_VOL_CAP_VOLMUTE:
7057 tp_features.mixer_no_level_control = 0;
7059 case TPACPI_VOL_CAP_MUTEONLY:
7060 tp_features.mixer_no_level_control = 1;
7066 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7067 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7068 "using user-supplied volume_capabilities=%d\n",
7069 volume_capabilities);
7071 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7072 volume_mode == TPACPI_VOL_MODE_MAX) {
7073 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7075 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7076 "driver auto-selected volume_mode=%d\n",
7079 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7080 "using user-supplied volume_mode=%d\n",
7084 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7085 "mute is supported, volume control is %s\n",
7086 str_supported(!tp_features.mixer_no_level_control));
7088 rc = volume_create_alsa_mixer();
7090 pr_err("Could not create the ALSA mixer interface\n");
7094 pr_info("Console audio control enabled, mode: %s\n",
7095 (volume_control_allowed) ?
7096 "override (read/write)" :
7097 "monitor (read only)");
7099 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7100 "registering volume hotkeys as change notification\n");
7101 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7102 | TP_ACPI_HKEY_VOLUP_MASK
7103 | TP_ACPI_HKEY_VOLDWN_MASK
7104 | TP_ACPI_HKEY_MUTE_MASK);
7109 static int volume_read(struct seq_file *m)
7113 if (volume_get_status(&status) < 0) {
7114 seq_printf(m, "level:\t\tunreadable\n");
7116 if (tp_features.mixer_no_level_control)
7117 seq_printf(m, "level:\t\tunsupported\n");
7119 seq_printf(m, "level:\t\t%d\n",
7120 status & TP_EC_AUDIO_LVL_MSK);
7122 seq_printf(m, "mute:\t\t%s\n",
7123 onoff(status, TP_EC_AUDIO_MUTESW));
7125 if (volume_control_allowed) {
7126 seq_printf(m, "commands:\tunmute, mute\n");
7127 if (!tp_features.mixer_no_level_control) {
7129 "commands:\tup, down\n");
7131 "commands:\tlevel <level>"
7132 " (<level> is 0-%d)\n",
7141 static int volume_write(char *buf)
7144 u8 new_level, new_mute;
7150 * We do allow volume control at driver startup, so that the
7151 * user can set initial state through the volume=... parameter hack.
7153 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7154 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7155 tp_warned.volume_ctrl_forbidden = 1;
7156 pr_notice("Console audio control in monitor mode, "
7157 "changes are not allowed\n");
7158 pr_notice("Use the volume_control=1 module parameter "
7159 "to enable volume control\n");
7164 rc = volume_get_status(&s);
7168 new_level = s & TP_EC_AUDIO_LVL_MSK;
7169 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7171 while ((cmd = next_cmd(&buf))) {
7172 if (!tp_features.mixer_no_level_control) {
7173 if (strlencmp(cmd, "up") == 0) {
7176 else if (new_level < TP_EC_VOLUME_MAX)
7179 } else if (strlencmp(cmd, "down") == 0) {
7182 else if (new_level > 0)
7185 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7186 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7191 if (strlencmp(cmd, "mute") == 0)
7192 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7193 else if (strlencmp(cmd, "unmute") == 0)
7199 if (tp_features.mixer_no_level_control) {
7200 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7201 new_mute ? "" : "un");
7202 rc = volume_set_mute(!!new_mute);
7204 tpacpi_disclose_usertask("procfs volume",
7205 "%smute and set level to %d\n",
7206 new_mute ? "" : "un", new_level);
7207 rc = volume_set_status(new_mute | new_level);
7209 volume_alsa_notify_change();
7211 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7214 static struct ibm_struct volume_driver_data = {
7216 .read = volume_read,
7217 .write = volume_write,
7218 .exit = volume_exit,
7219 .suspend = volume_suspend,
7220 .resume = volume_resume,
7221 .shutdown = volume_shutdown,
7224 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7226 #define alsa_card NULL
7228 static void inline volume_alsa_notify_change(void)
7232 static int __init volume_init(struct ibm_init_struct *iibm)
7234 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7239 static struct ibm_struct volume_driver_data = {
7243 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7245 /*************************************************************************
7252 * TPACPI_FAN_RD_ACPI_GFAN:
7253 * ACPI GFAN method: returns fan level
7255 * see TPACPI_FAN_WR_ACPI_SFAN
7256 * EC 0x2f (HFSP) not available if GFAN exists
7258 * TPACPI_FAN_WR_ACPI_SFAN:
7259 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7261 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7264 * TPACPI_FAN_WR_TPEC:
7265 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7266 * Supported on almost all ThinkPads
7268 * Fan speed changes of any sort (including those caused by the
7269 * disengaged mode) are usually done slowly by the firmware as the
7270 * maximum amount of fan duty cycle change per second seems to be
7273 * Reading is not available if GFAN exists.
7274 * Writing is not available if SFAN exists.
7277 * 7 automatic mode engaged;
7278 * (default operation mode of the ThinkPad)
7279 * fan level is ignored in this mode.
7280 * 6 full speed mode (takes precedence over bit 7);
7281 * not available on all thinkpads. May disable
7282 * the tachometer while the fan controller ramps up
7283 * the speed (which can take up to a few *minutes*).
7284 * Speeds up fan to 100% duty-cycle, which is far above
7285 * the standard RPM levels. It is not impossible that
7286 * it could cause hardware damage.
7287 * 5-3 unused in some models. Extra bits for fan level
7288 * in others, but still useless as all values above
7289 * 7 map to the same speed as level 7 in these models.
7290 * 2-0 fan level (0..7 usually)
7292 * 0x07 = max (set when temperatures critical)
7293 * Some ThinkPads may have other levels, see
7294 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7296 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7297 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7298 * does so, its initial value is meaningless (0x07).
7300 * For firmware bugs, refer to:
7301 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7305 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7306 * Main fan tachometer reading (in RPM)
7308 * This register is present on all ThinkPads with a new-style EC, and
7309 * it is known not to be present on the A21m/e, and T22, as there is
7310 * something else in offset 0x84 according to the ACPI DSDT. Other
7311 * ThinkPads from this same time period (and earlier) probably lack the
7312 * tachometer as well.
7314 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7315 * was never fixed by IBM to report the EC firmware version string
7316 * probably support the tachometer (like the early X models), so
7317 * detecting it is quite hard. We need more data to know for sure.
7319 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7322 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7325 * For firmware bugs, refer to:
7326 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7330 * ThinkPad EC register 0x31 bit 0 (only on select models)
7332 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7333 * show the speed of the main fan. When bit 0 of EC register 0x31
7334 * is one, the tachometer registers show the speed of the auxiliary
7337 * Fan control seems to affect both fans, regardless of the state
7340 * So far, only the firmware for the X60/X61 non-tablet versions
7341 * seem to support this (firmware TP-7M).
7343 * TPACPI_FAN_WR_ACPI_FANS:
7344 * ThinkPad X31, X40, X41. Not available in the X60.
7346 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7347 * high speed. ACPI DSDT seems to map these three speeds to levels
7348 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7349 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7351 * The speeds are stored on handles
7352 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7354 * There are three default speed sets, accessible as handles:
7355 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7357 * ACPI DSDT switches which set is in use depending on various
7360 * TPACPI_FAN_WR_TPEC is also available and should be used to
7361 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7362 * but the ACPI tables just mention level 7.
7365 enum { /* Fan control constants */
7366 fan_status_offset = 0x2f, /* EC register 0x2f */
7367 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7368 * 0x84 must be read before 0x85 */
7369 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7370 bit 0 selects which fan is active */
7372 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7373 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7375 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7378 enum fan_status_access_mode {
7379 TPACPI_FAN_NONE = 0, /* No fan status or control */
7380 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7381 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7384 enum fan_control_access_mode {
7385 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7386 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7387 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7388 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7391 enum fan_control_commands {
7392 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7393 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7394 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7395 * and also watchdog cmd */
7398 static bool fan_control_allowed;
7400 static enum fan_status_access_mode fan_status_access_mode;
7401 static enum fan_control_access_mode fan_control_access_mode;
7402 static enum fan_control_commands fan_control_commands;
7404 static u8 fan_control_initial_status;
7405 static u8 fan_control_desired_level;
7406 static u8 fan_control_resume_level;
7407 static int fan_watchdog_maxinterval;
7409 static struct mutex fan_mutex;
7411 static void fan_watchdog_fire(struct work_struct *ignored);
7412 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7414 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7415 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7416 "\\FSPD", /* 600e/x, 770e, 770x */
7418 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7419 "JFNS", /* 770x-JL */
7423 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7424 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7425 * be in auto mode (0x80).
7427 * This is corrected by any write to HFSP either by the driver, or
7430 * We assume 0x07 really means auto mode while this quirk is active,
7431 * as this is far more likely than the ThinkPad being in level 7,
7432 * which is only used by the firmware during thermal emergencies.
7434 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7435 * TP-70 (T43, R52), which are known to be buggy.
7438 static void fan_quirk1_setup(void)
7440 if (fan_control_initial_status == 0x07) {
7441 pr_notice("fan_init: initial fan status is unknown, "
7442 "assuming it is in auto mode\n");
7443 tp_features.fan_ctrl_status_undef = 1;
7447 static void fan_quirk1_handle(u8 *fan_status)
7449 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7450 if (*fan_status != fan_control_initial_status) {
7451 /* something changed the HFSP regisnter since
7452 * driver init time, so it is not undefined
7454 tp_features.fan_ctrl_status_undef = 0;
7456 /* Return most likely status. In fact, it
7457 * might be the only possible status */
7458 *fan_status = TP_EC_FAN_AUTO;
7463 /* Select main fan on X60/X61, NOOP on others */
7464 static bool fan_select_fan1(void)
7466 if (tp_features.second_fan) {
7469 if (ec_read(fan_select_offset, &val) < 0)
7472 if (ec_write(fan_select_offset, val) < 0)
7478 /* Select secondary fan on X60/X61 */
7479 static bool fan_select_fan2(void)
7483 if (!tp_features.second_fan)
7486 if (ec_read(fan_select_offset, &val) < 0)
7489 if (ec_write(fan_select_offset, val) < 0)
7496 * Call with fan_mutex held
7498 static void fan_update_desired_level(u8 status)
7501 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7503 fan_control_desired_level = 7;
7505 fan_control_desired_level = status;
7509 static int fan_get_status(u8 *status)
7514 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7516 switch (fan_status_access_mode) {
7517 case TPACPI_FAN_RD_ACPI_GFAN: {
7518 /* 570, 600e/x, 770e, 770x */
7521 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
7525 *status = res & 0x07;
7529 case TPACPI_FAN_RD_TPEC:
7530 /* all except 570, 600e/x, 770e, 770x */
7531 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7534 if (likely(status)) {
7536 fan_quirk1_handle(status);
7548 static int fan_get_status_safe(u8 *status)
7553 if (mutex_lock_killable(&fan_mutex))
7554 return -ERESTARTSYS;
7555 rc = fan_get_status(&s);
7557 fan_update_desired_level(s);
7558 mutex_unlock(&fan_mutex);
7566 static int fan_get_speed(unsigned int *speed)
7570 switch (fan_status_access_mode) {
7571 case TPACPI_FAN_RD_TPEC:
7572 /* all except 570, 600e/x, 770e, 770x */
7573 if (unlikely(!fan_select_fan1()))
7575 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7576 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7580 *speed = (hi << 8) | lo;
7591 static int fan2_get_speed(unsigned int *speed)
7596 switch (fan_status_access_mode) {
7597 case TPACPI_FAN_RD_TPEC:
7598 /* all except 570, 600e/x, 770e, 770x */
7599 if (unlikely(!fan_select_fan2()))
7601 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7602 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7603 fan_select_fan1(); /* play it safe */
7608 *speed = (hi << 8) | lo;
7619 static int fan_set_level(int level)
7621 if (!fan_control_allowed)
7624 switch (fan_control_access_mode) {
7625 case TPACPI_FAN_WR_ACPI_SFAN:
7626 if (level >= 0 && level <= 7) {
7627 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7633 case TPACPI_FAN_WR_ACPI_FANS:
7634 case TPACPI_FAN_WR_TPEC:
7635 if (!(level & TP_EC_FAN_AUTO) &&
7636 !(level & TP_EC_FAN_FULLSPEED) &&
7637 ((level < 0) || (level > 7)))
7640 /* safety net should the EC not support AUTO
7641 * or FULLSPEED mode bits and just ignore them */
7642 if (level & TP_EC_FAN_FULLSPEED)
7643 level |= 7; /* safety min speed 7 */
7644 else if (level & TP_EC_FAN_AUTO)
7645 level |= 4; /* safety min speed 4 */
7647 if (!acpi_ec_write(fan_status_offset, level))
7650 tp_features.fan_ctrl_status_undef = 0;
7657 vdbg_printk(TPACPI_DBG_FAN,
7658 "fan control: set fan control register to 0x%02x\n", level);
7662 static int fan_set_level_safe(int level)
7666 if (!fan_control_allowed)
7669 if (mutex_lock_killable(&fan_mutex))
7670 return -ERESTARTSYS;
7672 if (level == TPACPI_FAN_LAST_LEVEL)
7673 level = fan_control_desired_level;
7675 rc = fan_set_level(level);
7677 fan_update_desired_level(level);
7679 mutex_unlock(&fan_mutex);
7683 static int fan_set_enable(void)
7688 if (!fan_control_allowed)
7691 if (mutex_lock_killable(&fan_mutex))
7692 return -ERESTARTSYS;
7694 switch (fan_control_access_mode) {
7695 case TPACPI_FAN_WR_ACPI_FANS:
7696 case TPACPI_FAN_WR_TPEC:
7697 rc = fan_get_status(&s);
7701 /* Don't go out of emergency fan mode */
7704 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7707 if (!acpi_ec_write(fan_status_offset, s))
7710 tp_features.fan_ctrl_status_undef = 0;
7715 case TPACPI_FAN_WR_ACPI_SFAN:
7716 rc = fan_get_status(&s);
7722 /* Set fan to at least level 4 */
7725 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7735 mutex_unlock(&fan_mutex);
7738 vdbg_printk(TPACPI_DBG_FAN,
7739 "fan control: set fan control register to 0x%02x\n",
7744 static int fan_set_disable(void)
7748 if (!fan_control_allowed)
7751 if (mutex_lock_killable(&fan_mutex))
7752 return -ERESTARTSYS;
7755 switch (fan_control_access_mode) {
7756 case TPACPI_FAN_WR_ACPI_FANS:
7757 case TPACPI_FAN_WR_TPEC:
7758 if (!acpi_ec_write(fan_status_offset, 0x00))
7761 fan_control_desired_level = 0;
7762 tp_features.fan_ctrl_status_undef = 0;
7766 case TPACPI_FAN_WR_ACPI_SFAN:
7767 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7770 fan_control_desired_level = 0;
7778 vdbg_printk(TPACPI_DBG_FAN,
7779 "fan control: set fan control register to 0\n");
7781 mutex_unlock(&fan_mutex);
7785 static int fan_set_speed(int speed)
7789 if (!fan_control_allowed)
7792 if (mutex_lock_killable(&fan_mutex))
7793 return -ERESTARTSYS;
7796 switch (fan_control_access_mode) {
7797 case TPACPI_FAN_WR_ACPI_FANS:
7798 if (speed >= 0 && speed <= 65535) {
7799 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7800 speed, speed, speed))
7810 mutex_unlock(&fan_mutex);
7814 static void fan_watchdog_reset(void)
7816 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7819 if (fan_watchdog_maxinterval > 0 &&
7820 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7821 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7822 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7824 cancel_delayed_work(&fan_watchdog_task);
7827 static void fan_watchdog_fire(struct work_struct *ignored)
7831 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7834 pr_notice("fan watchdog: enabling fan\n");
7835 rc = fan_set_enable();
7837 pr_err("fan watchdog: error %d while enabling fan, "
7838 "will try again later...\n", -rc);
7839 /* reschedule for later */
7840 fan_watchdog_reset();
7845 * SYSFS fan layout: hwmon compatible (device)
7848 * 0: "disengaged" mode
7850 * 2: native EC "auto" mode (recommended, hardware default)
7852 * pwm*: set speed in manual mode, ignored otherwise.
7853 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7856 * fan*_input: tachometer reading, RPM
7859 * SYSFS fan layout: extensions
7861 * fan_watchdog (driver):
7862 * fan watchdog interval in seconds, 0 disables (default), max 120
7865 /* sysfs fan pwm1_enable ----------------------------------------------- */
7866 static ssize_t fan_pwm1_enable_show(struct device *dev,
7867 struct device_attribute *attr,
7873 res = fan_get_status_safe(&status);
7877 if (status & TP_EC_FAN_FULLSPEED) {
7879 } else if (status & TP_EC_FAN_AUTO) {
7884 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7887 static ssize_t fan_pwm1_enable_store(struct device *dev,
7888 struct device_attribute *attr,
7889 const char *buf, size_t count)
7894 if (parse_strtoul(buf, 2, &t))
7897 tpacpi_disclose_usertask("hwmon pwm1_enable",
7898 "set fan mode to %lu\n", t);
7902 level = TP_EC_FAN_FULLSPEED;
7905 level = TPACPI_FAN_LAST_LEVEL;
7908 level = TP_EC_FAN_AUTO;
7911 /* reserved for software-controlled auto mode */
7917 res = fan_set_level_safe(level);
7923 fan_watchdog_reset();
7928 static struct device_attribute dev_attr_fan_pwm1_enable =
7929 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7930 fan_pwm1_enable_show, fan_pwm1_enable_store);
7932 /* sysfs fan pwm1 ------------------------------------------------------ */
7933 static ssize_t fan_pwm1_show(struct device *dev,
7934 struct device_attribute *attr,
7940 res = fan_get_status_safe(&status);
7945 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7946 status = fan_control_desired_level;
7951 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7954 static ssize_t fan_pwm1_store(struct device *dev,
7955 struct device_attribute *attr,
7956 const char *buf, size_t count)
7960 u8 status, newlevel;
7962 if (parse_strtoul(buf, 255, &s))
7965 tpacpi_disclose_usertask("hwmon pwm1",
7966 "set fan speed to %lu\n", s);
7968 /* scale down from 0-255 to 0-7 */
7969 newlevel = (s >> 5) & 0x07;
7971 if (mutex_lock_killable(&fan_mutex))
7972 return -ERESTARTSYS;
7974 rc = fan_get_status(&status);
7975 if (!rc && (status &
7976 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7977 rc = fan_set_level(newlevel);
7981 fan_update_desired_level(newlevel);
7982 fan_watchdog_reset();
7986 mutex_unlock(&fan_mutex);
7987 return (rc)? rc : count;
7990 static struct device_attribute dev_attr_fan_pwm1 =
7991 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7992 fan_pwm1_show, fan_pwm1_store);
7994 /* sysfs fan fan1_input ------------------------------------------------ */
7995 static ssize_t fan_fan1_input_show(struct device *dev,
7996 struct device_attribute *attr,
8002 res = fan_get_speed(&speed);
8006 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8009 static struct device_attribute dev_attr_fan_fan1_input =
8010 __ATTR(fan1_input, S_IRUGO,
8011 fan_fan1_input_show, NULL);
8013 /* sysfs fan fan2_input ------------------------------------------------ */
8014 static ssize_t fan_fan2_input_show(struct device *dev,
8015 struct device_attribute *attr,
8021 res = fan2_get_speed(&speed);
8025 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8028 static struct device_attribute dev_attr_fan_fan2_input =
8029 __ATTR(fan2_input, S_IRUGO,
8030 fan_fan2_input_show, NULL);
8032 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8033 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8036 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8039 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8040 const char *buf, size_t count)
8044 if (parse_strtoul(buf, 120, &t))
8047 if (!fan_control_allowed)
8050 fan_watchdog_maxinterval = t;
8051 fan_watchdog_reset();
8053 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8058 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8059 fan_fan_watchdog_show, fan_fan_watchdog_store);
8061 /* --------------------------------------------------------------------- */
8062 static struct attribute *fan_attributes[] = {
8063 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8064 &dev_attr_fan_fan1_input.attr,
8065 NULL, /* for fan2_input */
8069 static const struct attribute_group fan_attr_group = {
8070 .attrs = fan_attributes,
8073 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8074 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8076 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8077 { .vendor = PCI_VENDOR_ID_IBM, \
8078 .bios = TPACPI_MATCH_ANY, \
8079 .ec = TPID(__id1, __id2), \
8080 .quirks = __quirks }
8082 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8083 { .vendor = PCI_VENDOR_ID_LENOVO, \
8084 .bios = TPACPI_MATCH_ANY, \
8085 .ec = TPID(__id1, __id2), \
8086 .quirks = __quirks }
8088 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8089 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8090 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8091 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8092 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8093 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8096 #undef TPACPI_FAN_QL
8097 #undef TPACPI_FAN_QI
8099 static int __init fan_init(struct ibm_init_struct *iibm)
8102 unsigned long quirks;
8104 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8105 "initializing fan subdriver\n");
8107 mutex_init(&fan_mutex);
8108 fan_status_access_mode = TPACPI_FAN_NONE;
8109 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8110 fan_control_commands = 0;
8111 fan_watchdog_maxinterval = 0;
8112 tp_features.fan_ctrl_status_undef = 0;
8113 tp_features.second_fan = 0;
8114 fan_control_desired_level = 7;
8116 if (tpacpi_is_ibm()) {
8117 TPACPI_ACPIHANDLE_INIT(fans);
8118 TPACPI_ACPIHANDLE_INIT(gfan);
8119 TPACPI_ACPIHANDLE_INIT(sfan);
8122 quirks = tpacpi_check_quirks(fan_quirk_table,
8123 ARRAY_SIZE(fan_quirk_table));
8126 /* 570, 600e/x, 770e, 770x */
8127 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8129 /* all other ThinkPads: note that even old-style
8130 * ThinkPad ECs supports the fan control register */
8131 if (likely(acpi_ec_read(fan_status_offset,
8132 &fan_control_initial_status))) {
8133 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8134 if (quirks & TPACPI_FAN_Q1)
8136 if (quirks & TPACPI_FAN_2FAN) {
8137 tp_features.second_fan = 1;
8138 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8139 "secondary fan support enabled\n");
8142 pr_err("ThinkPad ACPI EC access misbehaving, "
8143 "fan status and control unavailable\n");
8150 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8151 fan_control_commands |=
8152 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8155 /* gfan without sfan means no fan control */
8156 /* all other models implement TP EC 0x2f control */
8160 fan_control_access_mode =
8161 TPACPI_FAN_WR_ACPI_FANS;
8162 fan_control_commands |=
8163 TPACPI_FAN_CMD_SPEED |
8164 TPACPI_FAN_CMD_LEVEL |
8165 TPACPI_FAN_CMD_ENABLE;
8167 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8168 fan_control_commands |=
8169 TPACPI_FAN_CMD_LEVEL |
8170 TPACPI_FAN_CMD_ENABLE;
8175 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8176 "fan is %s, modes %d, %d\n",
8177 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8178 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8179 fan_status_access_mode, fan_control_access_mode);
8181 /* fan control master switch */
8182 if (!fan_control_allowed) {
8183 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8184 fan_control_commands = 0;
8185 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8186 "fan control features disabled by parameter\n");
8189 /* update fan_control_desired_level */
8190 if (fan_status_access_mode != TPACPI_FAN_NONE)
8191 fan_get_status_safe(NULL);
8193 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8194 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8195 if (tp_features.second_fan) {
8196 /* attach second fan tachometer */
8197 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8198 &dev_attr_fan_fan2_input.attr;
8200 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8205 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8206 &driver_attr_fan_watchdog);
8208 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8217 static void fan_exit(void)
8219 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8220 "cancelling any pending fan watchdog tasks\n");
8222 /* FIXME: can we really do this unconditionally? */
8223 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8224 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8225 &driver_attr_fan_watchdog);
8227 cancel_delayed_work(&fan_watchdog_task);
8228 flush_workqueue(tpacpi_wq);
8231 static void fan_suspend(void)
8235 if (!fan_control_allowed)
8238 /* Store fan status in cache */
8239 fan_control_resume_level = 0;
8240 rc = fan_get_status_safe(&fan_control_resume_level);
8242 pr_notice("failed to read fan level for later "
8243 "restore during resume: %d\n", rc);
8245 /* if it is undefined, don't attempt to restore it.
8247 if (tp_features.fan_ctrl_status_undef)
8248 fan_control_resume_level = 0;
8251 static void fan_resume(void)
8253 u8 current_level = 7;
8254 bool do_set = false;
8257 /* DSDT *always* updates status on resume */
8258 tp_features.fan_ctrl_status_undef = 0;
8260 if (!fan_control_allowed ||
8261 !fan_control_resume_level ||
8262 (fan_get_status_safe(¤t_level) < 0))
8265 switch (fan_control_access_mode) {
8266 case TPACPI_FAN_WR_ACPI_SFAN:
8267 /* never decrease fan level */
8268 do_set = (fan_control_resume_level > current_level);
8270 case TPACPI_FAN_WR_ACPI_FANS:
8271 case TPACPI_FAN_WR_TPEC:
8272 /* never decrease fan level, scale is:
8273 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8275 * We expect the firmware to set either 7 or AUTO, but we
8276 * handle FULLSPEED out of paranoia.
8278 * So, we can safely only restore FULLSPEED or 7, anything
8279 * else could slow the fan. Restoring AUTO is useless, at
8280 * best that's exactly what the DSDT already set (it is the
8283 * Always keep in mind that the DSDT *will* have set the
8284 * fans to what the vendor supposes is the best level. We
8285 * muck with it only to speed the fan up.
8287 if (fan_control_resume_level != 7 &&
8288 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8291 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8292 (current_level != fan_control_resume_level);
8298 pr_notice("restoring fan level to 0x%02x\n",
8299 fan_control_resume_level);
8300 rc = fan_set_level_safe(fan_control_resume_level);
8302 pr_notice("failed to restore fan level: %d\n", rc);
8306 static int fan_read(struct seq_file *m)
8310 unsigned int speed = 0;
8312 switch (fan_status_access_mode) {
8313 case TPACPI_FAN_RD_ACPI_GFAN:
8314 /* 570, 600e/x, 770e, 770x */
8315 rc = fan_get_status_safe(&status);
8319 seq_printf(m, "status:\t\t%s\n"
8321 (status != 0) ? "enabled" : "disabled", status);
8324 case TPACPI_FAN_RD_TPEC:
8325 /* all except 570, 600e/x, 770e, 770x */
8326 rc = fan_get_status_safe(&status);
8330 seq_printf(m, "status:\t\t%s\n",
8331 (status != 0) ? "enabled" : "disabled");
8333 rc = fan_get_speed(&speed);
8337 seq_printf(m, "speed:\t\t%d\n", speed);
8339 if (status & TP_EC_FAN_FULLSPEED)
8340 /* Disengaged mode takes precedence */
8341 seq_printf(m, "level:\t\tdisengaged\n");
8342 else if (status & TP_EC_FAN_AUTO)
8343 seq_printf(m, "level:\t\tauto\n");
8345 seq_printf(m, "level:\t\t%d\n", status);
8348 case TPACPI_FAN_NONE:
8350 seq_printf(m, "status:\t\tnot supported\n");
8353 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8354 seq_printf(m, "commands:\tlevel <level>");
8356 switch (fan_control_access_mode) {
8357 case TPACPI_FAN_WR_ACPI_SFAN:
8358 seq_printf(m, " (<level> is 0-7)\n");
8362 seq_printf(m, " (<level> is 0-7, "
8363 "auto, disengaged, full-speed)\n");
8368 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8369 seq_printf(m, "commands:\tenable, disable\n"
8370 "commands:\twatchdog <timeout> (<timeout> "
8371 "is 0 (off), 1-120 (seconds))\n");
8373 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8374 seq_printf(m, "commands:\tspeed <speed>"
8375 " (<speed> is 0-65535)\n");
8380 static int fan_write_cmd_level(const char *cmd, int *rc)
8384 if (strlencmp(cmd, "level auto") == 0)
8385 level = TP_EC_FAN_AUTO;
8386 else if ((strlencmp(cmd, "level disengaged") == 0) |
8387 (strlencmp(cmd, "level full-speed") == 0))
8388 level = TP_EC_FAN_FULLSPEED;
8389 else if (sscanf(cmd, "level %d", &level) != 1)
8392 *rc = fan_set_level_safe(level);
8394 pr_err("level command accepted for unsupported access mode %d\n",
8395 fan_control_access_mode);
8397 tpacpi_disclose_usertask("procfs fan",
8398 "set level to %d\n", level);
8403 static int fan_write_cmd_enable(const char *cmd, int *rc)
8405 if (strlencmp(cmd, "enable") != 0)
8408 *rc = fan_set_enable();
8410 pr_err("enable command accepted for unsupported access mode %d\n",
8411 fan_control_access_mode);
8413 tpacpi_disclose_usertask("procfs fan", "enable\n");
8418 static int fan_write_cmd_disable(const char *cmd, int *rc)
8420 if (strlencmp(cmd, "disable") != 0)
8423 *rc = fan_set_disable();
8425 pr_err("disable command accepted for unsupported access mode %d\n",
8426 fan_control_access_mode);
8428 tpacpi_disclose_usertask("procfs fan", "disable\n");
8433 static int fan_write_cmd_speed(const char *cmd, int *rc)
8438 * Support speed <low> <medium> <high> ? */
8440 if (sscanf(cmd, "speed %d", &speed) != 1)
8443 *rc = fan_set_speed(speed);
8445 pr_err("speed command accepted for unsupported access mode %d\n",
8446 fan_control_access_mode);
8448 tpacpi_disclose_usertask("procfs fan",
8449 "set speed to %d\n", speed);
8454 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8458 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8461 if (interval < 0 || interval > 120)
8464 fan_watchdog_maxinterval = interval;
8465 tpacpi_disclose_usertask("procfs fan",
8466 "set watchdog timer to %d\n",
8473 static int fan_write(char *buf)
8478 while (!rc && (cmd = next_cmd(&buf))) {
8479 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8480 fan_write_cmd_level(cmd, &rc)) &&
8481 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8482 (fan_write_cmd_enable(cmd, &rc) ||
8483 fan_write_cmd_disable(cmd, &rc) ||
8484 fan_write_cmd_watchdog(cmd, &rc))) &&
8485 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8486 fan_write_cmd_speed(cmd, &rc))
8490 fan_watchdog_reset();
8496 static struct ibm_struct fan_driver_data = {
8501 .suspend = fan_suspend,
8502 .resume = fan_resume,
8505 /*************************************************************************
8506 * Mute LED subdriver
8510 struct tp_led_table {
8517 static struct tp_led_table led_tables[] = {
8518 [TPACPI_LED_MUTE] = {
8523 [TPACPI_LED_MICMUTE] = {
8530 static int mute_led_on_off(struct tp_led_table *t, bool state)
8535 if (!ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp))) {
8536 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
8540 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
8541 state ? t->on_value : t->off_value))
8548 int tpacpi_led_set(int whichled, bool on)
8550 struct tp_led_table *t;
8552 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
8555 t = &led_tables[whichled];
8556 if (t->state < 0 || t->state == on)
8558 return mute_led_on_off(t, on);
8560 EXPORT_SYMBOL_GPL(tpacpi_led_set);
8562 static int mute_led_init(struct ibm_init_struct *iibm)
8567 for (i = 0; i < TPACPI_LED_MAX; i++) {
8568 struct tp_led_table *t = &led_tables[i];
8569 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
8570 mute_led_on_off(t, false);
8577 static void mute_led_exit(void)
8581 for (i = 0; i < TPACPI_LED_MAX; i++)
8582 tpacpi_led_set(i, false);
8585 static void mute_led_resume(void)
8589 for (i = 0; i < TPACPI_LED_MAX; i++) {
8590 struct tp_led_table *t = &led_tables[i];
8592 mute_led_on_off(t, t->state);
8596 static struct ibm_struct mute_led_driver_data = {
8598 .exit = mute_led_exit,
8599 .resume = mute_led_resume,
8602 /****************************************************************************
8603 ****************************************************************************
8607 ****************************************************************************
8608 ****************************************************************************/
8611 * HKEY event callout for other subdrivers go here
8612 * (yes, it is ugly, but it is quick, safe, and gets the job done
8614 static void tpacpi_driver_event(const unsigned int hkey_event)
8616 if (ibm_backlight_device) {
8617 switch (hkey_event) {
8618 case TP_HKEY_EV_BRGHT_UP:
8619 case TP_HKEY_EV_BRGHT_DOWN:
8620 tpacpi_brightness_notify_change();
8624 switch (hkey_event) {
8625 case TP_HKEY_EV_VOL_UP:
8626 case TP_HKEY_EV_VOL_DOWN:
8627 case TP_HKEY_EV_VOL_MUTE:
8628 volume_alsa_notify_change();
8633 static void hotkey_driver_event(const unsigned int scancode)
8635 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8638 /* sysfs name ---------------------------------------------------------- */
8639 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8640 struct device_attribute *attr,
8643 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8646 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8647 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8649 /* --------------------------------------------------------------------- */
8652 static struct proc_dir_entry *proc_dir;
8655 * Module and infrastructure proble, init and exit handling
8658 static bool force_load;
8660 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8661 static const char * __init str_supported(int is_supported)
8663 static char text_unsupported[] __initdata = "not supported";
8665 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8667 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8669 static void ibm_exit(struct ibm_struct *ibm)
8671 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8673 list_del_init(&ibm->all_drivers);
8675 if (ibm->flags.acpi_notify_installed) {
8676 dbg_printk(TPACPI_DBG_EXIT,
8677 "%s: acpi_remove_notify_handler\n", ibm->name);
8679 acpi_remove_notify_handler(*ibm->acpi->handle,
8681 dispatch_acpi_notify);
8682 ibm->flags.acpi_notify_installed = 0;
8685 if (ibm->flags.proc_created) {
8686 dbg_printk(TPACPI_DBG_EXIT,
8687 "%s: remove_proc_entry\n", ibm->name);
8688 remove_proc_entry(ibm->name, proc_dir);
8689 ibm->flags.proc_created = 0;
8692 if (ibm->flags.acpi_driver_registered) {
8693 dbg_printk(TPACPI_DBG_EXIT,
8694 "%s: acpi_bus_unregister_driver\n", ibm->name);
8696 acpi_bus_unregister_driver(ibm->acpi->driver);
8697 kfree(ibm->acpi->driver);
8698 ibm->acpi->driver = NULL;
8699 ibm->flags.acpi_driver_registered = 0;
8702 if (ibm->flags.init_called && ibm->exit) {
8704 ibm->flags.init_called = 0;
8707 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8710 static int __init ibm_init(struct ibm_init_struct *iibm)
8713 struct ibm_struct *ibm = iibm->data;
8714 struct proc_dir_entry *entry;
8716 BUG_ON(ibm == NULL);
8718 INIT_LIST_HEAD(&ibm->all_drivers);
8720 if (ibm->flags.experimental && !experimental)
8723 dbg_printk(TPACPI_DBG_INIT,
8724 "probing for %s\n", ibm->name);
8727 ret = iibm->init(iibm);
8729 return 0; /* probe failed */
8733 ibm->flags.init_called = 1;
8737 if (ibm->acpi->hid) {
8738 ret = register_tpacpi_subdriver(ibm);
8743 if (ibm->acpi->notify) {
8744 ret = setup_acpi_notify(ibm);
8745 if (ret == -ENODEV) {
8746 pr_notice("disabling subdriver %s\n",
8756 dbg_printk(TPACPI_DBG_INIT,
8757 "%s installed\n", ibm->name);
8760 umode_t mode = iibm->base_procfs_mode;
8766 entry = proc_create_data(ibm->name, mode, proc_dir,
8767 &dispatch_proc_fops, ibm);
8769 pr_err("unable to create proc entry %s\n", ibm->name);
8773 ibm->flags.proc_created = 1;
8776 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8781 dbg_printk(TPACPI_DBG_INIT,
8782 "%s: at error exit path with result %d\n",
8786 return (ret < 0)? ret : 0;
8791 static bool __pure __init tpacpi_is_fw_digit(const char c)
8793 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8796 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8797 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8800 return s && strlen(s) >= 8 &&
8801 tpacpi_is_fw_digit(s[0]) &&
8802 tpacpi_is_fw_digit(s[1]) &&
8804 (s[3] == 'T' || s[3] == 'N') &&
8805 tpacpi_is_fw_digit(s[4]) &&
8806 tpacpi_is_fw_digit(s[5]);
8809 /* returns 0 - probe ok, or < 0 - probe error.
8810 * Probe ok doesn't mean thinkpad found.
8811 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8812 static int __must_check __init get_thinkpad_model_data(
8813 struct thinkpad_id_data *tp)
8815 const struct dmi_device *dev = NULL;
8816 char ec_fw_string[18];
8822 memset(tp, 0, sizeof(*tp));
8824 if (dmi_name_in_vendors("IBM"))
8825 tp->vendor = PCI_VENDOR_ID_IBM;
8826 else if (dmi_name_in_vendors("LENOVO"))
8827 tp->vendor = PCI_VENDOR_ID_LENOVO;
8831 s = dmi_get_system_info(DMI_BIOS_VERSION);
8832 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8833 if (s && !tp->bios_version_str)
8836 /* Really ancient ThinkPad 240X will fail this, which is fine */
8837 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8838 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
8841 tp->bios_model = tp->bios_version_str[0]
8842 | (tp->bios_version_str[1] << 8);
8843 tp->bios_release = (tp->bios_version_str[4] << 8)
8844 | tp->bios_version_str[5];
8847 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8848 * X32 or newer, all Z series; Some models must have an
8849 * up-to-date BIOS or they will not be detected.
8851 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8853 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8854 if (sscanf(dev->name,
8855 "IBM ThinkPad Embedded Controller -[%17c",
8856 ec_fw_string) == 1) {
8857 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8858 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8860 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8861 if (!tp->ec_version_str)
8864 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8865 tp->ec_model = ec_fw_string[0]
8866 | (ec_fw_string[1] << 8);
8867 tp->ec_release = (ec_fw_string[4] << 8)
8870 pr_notice("ThinkPad firmware release %s "
8871 "doesn't match the known patterns\n",
8873 pr_notice("please report this to %s\n",
8880 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8881 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
8882 tp->model_str = kstrdup(s, GFP_KERNEL);
8886 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8887 if (s && !(strnicmp(s, "Lenovo", 6))) {
8888 tp->model_str = kstrdup(s, GFP_KERNEL);
8894 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8895 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8896 if (s && !tp->nummodel_str)
8902 static int __init probe_for_thinkpad(void)
8909 /* It would be dangerous to run the driver in this case */
8910 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8914 * Non-ancient models have better DMI tagging, but very old models
8915 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8917 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8918 (thinkpad_id.ec_model != 0) ||
8919 tpacpi_is_fw_known();
8921 /* The EC handler is required */
8922 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8925 pr_err("Not yet supported ThinkPad detected!\n");
8929 if (!is_thinkpad && !force_load)
8935 static void __init thinkpad_acpi_init_banner(void)
8937 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8938 pr_info("%s\n", TPACPI_URL);
8940 pr_info("ThinkPad BIOS %s, EC %s\n",
8941 (thinkpad_id.bios_version_str) ?
8942 thinkpad_id.bios_version_str : "unknown",
8943 (thinkpad_id.ec_version_str) ?
8944 thinkpad_id.ec_version_str : "unknown");
8946 BUG_ON(!thinkpad_id.vendor);
8948 if (thinkpad_id.model_str)
8949 pr_info("%s %s, model %s\n",
8950 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8951 "IBM" : ((thinkpad_id.vendor ==
8952 PCI_VENDOR_ID_LENOVO) ?
8953 "Lenovo" : "Unknown vendor"),
8954 thinkpad_id.model_str,
8955 (thinkpad_id.nummodel_str) ?
8956 thinkpad_id.nummodel_str : "unknown");
8959 /* Module init, exit, parameters */
8961 static struct ibm_init_struct ibms_init[] __initdata = {
8963 .data = &thinkpad_acpi_driver_data,
8966 .init = hotkey_init,
8967 .data = &hotkey_driver_data,
8970 .init = bluetooth_init,
8971 .data = &bluetooth_driver_data,
8975 .data = &wan_driver_data,
8979 .data = &uwb_driver_data,
8981 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8984 .base_procfs_mode = S_IRUSR,
8985 .data = &video_driver_data,
8990 .data = &light_driver_data,
8994 .data = &cmos_driver_data,
8998 .data = &led_driver_data,
9002 .data = &beep_driver_data,
9005 .init = thermal_init,
9006 .data = &thermal_driver_data,
9009 .init = brightness_init,
9010 .data = &brightness_driver_data,
9013 .init = volume_init,
9014 .data = &volume_driver_data,
9018 .data = &fan_driver_data,
9021 .init = mute_led_init,
9022 .data = &mute_led_driver_data,
9026 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9029 struct ibm_struct *ibm;
9031 if (!kp || !kp->name || !val)
9034 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9035 ibm = ibms_init[i].data;
9036 WARN_ON(ibm == NULL);
9038 if (!ibm || !ibm->name)
9041 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9042 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
9044 strcpy(ibms_init[i].param, val);
9045 strcat(ibms_init[i].param, ",");
9053 module_param(experimental, int, 0444);
9054 MODULE_PARM_DESC(experimental,
9055 "Enables experimental features when non-zero");
9057 module_param_named(debug, dbg_level, uint, 0);
9058 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
9060 module_param(force_load, bool, 0444);
9061 MODULE_PARM_DESC(force_load,
9062 "Attempts to load the driver even on a "
9063 "mis-identified ThinkPad when true");
9065 module_param_named(fan_control, fan_control_allowed, bool, 0444);
9066 MODULE_PARM_DESC(fan_control,
9067 "Enables setting fan parameters features when true");
9069 module_param_named(brightness_mode, brightness_mode, uint, 0444);
9070 MODULE_PARM_DESC(brightness_mode,
9071 "Selects brightness control strategy: "
9072 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9074 module_param(brightness_enable, uint, 0444);
9075 MODULE_PARM_DESC(brightness_enable,
9076 "Enables backlight control when 1, disables when 0");
9078 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9079 module_param_named(volume_mode, volume_mode, uint, 0444);
9080 MODULE_PARM_DESC(volume_mode,
9081 "Selects volume control strategy: "
9082 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9084 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9085 MODULE_PARM_DESC(volume_capabilities,
9086 "Selects the mixer capabilites: "
9087 "0=auto, 1=volume and mute, 2=mute only");
9089 module_param_named(volume_control, volume_control_allowed, bool, 0444);
9090 MODULE_PARM_DESC(volume_control,
9091 "Enables software override for the console audio "
9092 "control when true");
9094 /* ALSA module API parameters */
9095 module_param_named(index, alsa_index, int, 0444);
9096 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9097 module_param_named(id, alsa_id, charp, 0444);
9098 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9099 module_param_named(enable, alsa_enable, bool, 0444);
9100 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
9101 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9103 #define TPACPI_PARAM(feature) \
9104 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9105 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
9106 "at module load, see documentation")
9108 TPACPI_PARAM(hotkey);
9109 TPACPI_PARAM(bluetooth);
9110 TPACPI_PARAM(video);
9111 TPACPI_PARAM(light);
9115 TPACPI_PARAM(brightness);
9116 TPACPI_PARAM(volume);
9119 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9120 module_param(dbg_wlswemul, uint, 0444);
9121 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9122 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9123 MODULE_PARM_DESC(wlsw_state,
9124 "Initial state of the emulated WLSW switch");
9126 module_param(dbg_bluetoothemul, uint, 0444);
9127 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9128 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9129 MODULE_PARM_DESC(bluetooth_state,
9130 "Initial state of the emulated bluetooth switch");
9132 module_param(dbg_wwanemul, uint, 0444);
9133 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9134 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9135 MODULE_PARM_DESC(wwan_state,
9136 "Initial state of the emulated WWAN switch");
9138 module_param(dbg_uwbemul, uint, 0444);
9139 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9140 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9141 MODULE_PARM_DESC(uwb_state,
9142 "Initial state of the emulated UWB switch");
9145 static void thinkpad_acpi_module_exit(void)
9147 struct ibm_struct *ibm, *itmp;
9149 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9151 list_for_each_entry_safe_reverse(ibm, itmp,
9152 &tpacpi_all_drivers,
9157 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9159 if (tpacpi_inputdev) {
9160 if (tp_features.input_device_registered)
9161 input_unregister_device(tpacpi_inputdev);
9163 input_free_device(tpacpi_inputdev);
9164 kfree(hotkey_keycode_map);
9168 hwmon_device_unregister(tpacpi_hwmon);
9170 if (tp_features.sensors_pdev_attrs_registered)
9171 device_remove_file(&tpacpi_sensors_pdev->dev,
9172 &dev_attr_thinkpad_acpi_pdev_name);
9173 if (tpacpi_sensors_pdev)
9174 platform_device_unregister(tpacpi_sensors_pdev);
9176 platform_device_unregister(tpacpi_pdev);
9178 if (tp_features.sensors_pdrv_attrs_registered)
9179 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9180 if (tp_features.platform_drv_attrs_registered)
9181 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9183 if (tp_features.sensors_pdrv_registered)
9184 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9186 if (tp_features.platform_drv_registered)
9187 platform_driver_unregister(&tpacpi_pdriver);
9190 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9193 destroy_workqueue(tpacpi_wq);
9195 kfree(thinkpad_id.bios_version_str);
9196 kfree(thinkpad_id.ec_version_str);
9197 kfree(thinkpad_id.model_str);
9198 kfree(thinkpad_id.nummodel_str);
9202 static int __init thinkpad_acpi_module_init(void)
9206 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9208 /* Driver-level probe */
9210 ret = get_thinkpad_model_data(&thinkpad_id);
9212 pr_err("unable to get DMI data: %d\n", ret);
9213 thinkpad_acpi_module_exit();
9216 ret = probe_for_thinkpad();
9218 thinkpad_acpi_module_exit();
9222 /* Driver initialization */
9224 thinkpad_acpi_init_banner();
9225 tpacpi_check_outdated_fw();
9227 TPACPI_ACPIHANDLE_INIT(ecrd);
9228 TPACPI_ACPIHANDLE_INIT(ecwr);
9230 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9232 thinkpad_acpi_module_exit();
9236 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9238 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9239 thinkpad_acpi_module_exit();
9243 ret = platform_driver_register(&tpacpi_pdriver);
9245 pr_err("unable to register main platform driver\n");
9246 thinkpad_acpi_module_exit();
9249 tp_features.platform_drv_registered = 1;
9251 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9253 pr_err("unable to register hwmon platform driver\n");
9254 thinkpad_acpi_module_exit();
9257 tp_features.sensors_pdrv_registered = 1;
9259 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9261 tp_features.platform_drv_attrs_registered = 1;
9262 ret = tpacpi_create_driver_attributes(
9263 &tpacpi_hwmon_pdriver.driver);
9266 pr_err("unable to create sysfs driver attributes\n");
9267 thinkpad_acpi_module_exit();
9270 tp_features.sensors_pdrv_attrs_registered = 1;
9273 /* Device initialization */
9274 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9276 if (IS_ERR(tpacpi_pdev)) {
9277 ret = PTR_ERR(tpacpi_pdev);
9279 pr_err("unable to register platform device\n");
9280 thinkpad_acpi_module_exit();
9283 tpacpi_sensors_pdev = platform_device_register_simple(
9284 TPACPI_HWMON_DRVR_NAME,
9286 if (IS_ERR(tpacpi_sensors_pdev)) {
9287 ret = PTR_ERR(tpacpi_sensors_pdev);
9288 tpacpi_sensors_pdev = NULL;
9289 pr_err("unable to register hwmon platform device\n");
9290 thinkpad_acpi_module_exit();
9293 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9294 &dev_attr_thinkpad_acpi_pdev_name);
9296 pr_err("unable to create sysfs hwmon device attributes\n");
9297 thinkpad_acpi_module_exit();
9300 tp_features.sensors_pdev_attrs_registered = 1;
9301 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9302 if (IS_ERR(tpacpi_hwmon)) {
9303 ret = PTR_ERR(tpacpi_hwmon);
9304 tpacpi_hwmon = NULL;
9305 pr_err("unable to register hwmon device\n");
9306 thinkpad_acpi_module_exit();
9309 mutex_init(&tpacpi_inputdev_send_mutex);
9310 tpacpi_inputdev = input_allocate_device();
9311 if (!tpacpi_inputdev) {
9312 thinkpad_acpi_module_exit();
9315 /* Prepare input device, but don't register */
9316 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9317 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9318 tpacpi_inputdev->id.bustype = BUS_HOST;
9319 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9320 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9321 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9322 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9325 /* Init subdriver dependencies */
9326 tpacpi_detect_brightness_capabilities();
9328 /* Init subdrivers */
9329 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9330 ret = ibm_init(&ibms_init[i]);
9331 if (ret >= 0 && *ibms_init[i].param)
9332 ret = ibms_init[i].data->write(ibms_init[i].param);
9334 thinkpad_acpi_module_exit();
9339 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9341 ret = input_register_device(tpacpi_inputdev);
9343 pr_err("unable to register input device\n");
9344 thinkpad_acpi_module_exit();
9347 tp_features.input_device_registered = 1;
9353 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9356 * This will autoload the driver in almost every ThinkPad
9357 * in widespread use.
9359 * Only _VERY_ old models, like the 240, 240x and 570 lack
9360 * the HKEY event interface.
9362 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9365 * DMI matching for module autoloading
9367 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9368 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9370 * Only models listed in thinkwiki will be supported, so add yours
9371 * if it is not there yet.
9373 #define IBM_BIOS_MODULE_ALIAS(__type) \
9374 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9376 /* Ancient thinkpad BIOSes have to be identified by
9377 * BIOS type or model number, and there are far less
9378 * BIOS types than model numbers... */
9379 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9381 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9382 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9383 MODULE_DESCRIPTION(TPACPI_DESC);
9384 MODULE_VERSION(TPACPI_VERSION);
9385 MODULE_LICENSE("GPL");
9387 module_init(thinkpad_acpi_module_init);
9388 module_exit(thinkpad_acpi_module_exit);