2 * thinkpad_acpi.c - ThinkPad ACPI Extras
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.24"
27 #define TPACPI_SYSFS_VERSION 0x020700
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
43 * 2005-01-16 0.9 use MODULE_VERSION
45 * fix parameter passing on module loading
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
65 #include <linux/nvram.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/sysfs.h>
69 #include <linux/backlight.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <asm/uaccess.h>
79 #include <linux/dmi.h>
80 #include <linux/jiffies.h>
81 #include <linux/workqueue.h>
83 #include <sound/core.h>
84 #include <sound/control.h>
85 #include <sound/initval.h>
87 #include <acpi/acpi_drivers.h>
89 #include <linux/pci_ids.h>
92 /* ThinkPad CMOS commands */
93 #define TP_CMOS_VOLUME_DOWN 0
94 #define TP_CMOS_VOLUME_UP 1
95 #define TP_CMOS_VOLUME_MUTE 2
96 #define TP_CMOS_BRIGHTNESS_UP 4
97 #define TP_CMOS_BRIGHTNESS_DOWN 5
98 #define TP_CMOS_THINKLIGHT_ON 12
99 #define TP_CMOS_THINKLIGHT_OFF 13
101 /* NVRAM Addresses */
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
110 /* NVRAM bit masks */
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
127 /* Misc NVRAM-related */
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
133 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
164 enum tpacpi_hkey_event_t {
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
187 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
188 or port replicator */
189 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
190 dock or port replicator */
192 /* User-interface events */
193 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
194 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
195 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
196 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
197 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
198 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
199 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201 /* Key-related user-interface events */
202 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
203 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
206 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
207 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
208 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
209 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
210 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
212 /* AC-related events */
213 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
216 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219 /****************************************************************************
223 #define TPACPI_NAME "thinkpad"
224 #define TPACPI_DESC "ThinkPad ACPI Extras"
225 #define TPACPI_FILE TPACPI_NAME "_acpi"
226 #define TPACPI_URL "http://ibm-acpi.sf.net/"
229 #define TPACPI_PROC_DIR "ibm"
230 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
231 #define TPACPI_DRVR_NAME TPACPI_FILE
232 #define TPACPI_DRVR_SHORTNAME "tpacpi"
233 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
235 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
236 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
238 #define TPACPI_MAX_ACPI_ARGS 3
240 /* Debugging printk groups */
241 #define TPACPI_DBG_ALL 0xffff
242 #define TPACPI_DBG_DISCLOSETASK 0x8000
243 #define TPACPI_DBG_INIT 0x0001
244 #define TPACPI_DBG_EXIT 0x0002
245 #define TPACPI_DBG_RFKILL 0x0004
246 #define TPACPI_DBG_HKEY 0x0008
247 #define TPACPI_DBG_FAN 0x0010
248 #define TPACPI_DBG_BRGHT 0x0020
249 #define TPACPI_DBG_MIXER 0x0040
251 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
252 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
253 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
256 /****************************************************************************
257 * Driver-wide structs and misc. variables
262 struct tp_acpi_drv_struct {
263 const struct acpi_device_id *hid;
264 struct acpi_driver *driver;
266 void (*notify) (struct ibm_struct *, u32);
269 struct acpi_device *device;
275 int (*read) (struct seq_file *);
276 int (*write) (char *);
278 void (*resume) (void);
279 void (*suspend) (void);
280 void (*shutdown) (void);
282 struct list_head all_drivers;
284 struct tp_acpi_drv_struct *acpi;
287 u8 acpi_driver_registered:1;
288 u8 acpi_notify_installed:1;
295 struct ibm_init_struct {
298 int (*init) (struct ibm_init_struct *);
299 umode_t base_procfs_mode;
300 struct ibm_struct *data;
311 u32 bright_acpimode:1;
315 u32 fan_ctrl_status_undef:1;
317 u32 beep_needs_two_args:1;
318 u32 mixer_no_level_control:1;
319 u32 input_device_registered:1;
320 u32 platform_drv_registered:1;
321 u32 platform_drv_attrs_registered:1;
322 u32 sensors_pdrv_registered:1;
323 u32 sensors_pdrv_attrs_registered:1;
324 u32 sensors_pdev_attrs_registered:1;
325 u32 hotkey_poll_active:1;
329 u16 hotkey_mask_ff:1;
330 u16 volume_ctrl_forbidden:1;
333 struct thinkpad_id_data {
334 unsigned int vendor; /* ThinkPad vendor:
335 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
337 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
338 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
340 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
342 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
345 char *model_str; /* ThinkPad T43 */
346 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
348 static struct thinkpad_id_data thinkpad_id;
351 TPACPI_LIFE_INIT = 0,
356 static int experimental;
357 static u32 dbg_level;
359 static struct workqueue_struct *tpacpi_wq;
367 /* Special LED class that can defer work */
368 struct tpacpi_led_classdev {
369 struct led_classdev led_classdev;
370 struct work_struct work;
371 enum led_status_t new_state;
375 /* brightness level capabilities */
376 static unsigned int bright_maxlvl; /* 0 = unknown */
378 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
379 static int dbg_wlswemul;
380 static bool tpacpi_wlsw_emulstate;
381 static int dbg_bluetoothemul;
382 static bool tpacpi_bluetooth_emulstate;
383 static int dbg_wwanemul;
384 static bool tpacpi_wwan_emulstate;
385 static int dbg_uwbemul;
386 static bool tpacpi_uwb_emulstate;
390 /*************************************************************************
394 #define dbg_printk(a_dbg_level, format, arg...) \
396 if (dbg_level & (a_dbg_level)) \
397 printk(KERN_DEBUG pr_fmt("%s: " format), \
401 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
402 #define vdbg_printk dbg_printk
403 static const char *str_supported(int is_supported);
405 static inline const char *str_supported(int is_supported) { return ""; }
406 #define vdbg_printk(a_dbg_level, format, arg...) \
407 no_printk(format, ##arg)
410 static void tpacpi_log_usertask(const char * const what)
412 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
413 what, task_tgid_vnr(current));
416 #define tpacpi_disclose_usertask(what, format, arg...) \
418 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
419 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
420 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
421 what, task_tgid_vnr(current), ## arg); \
426 * Quirk handling helpers
428 * ThinkPad IDs and versions seen in the field so far
429 * are two-characters from the set [0-9A-Z], i.e. base 36.
431 * We use values well outside that range as specials.
434 #define TPACPI_MATCH_ANY 0xffffU
435 #define TPACPI_MATCH_UNKNOWN 0U
437 /* TPID('1', 'Y') == 0x5931 */
438 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
440 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
441 { .vendor = PCI_VENDOR_ID_IBM, \
442 .bios = TPID(__id1, __id2), \
443 .ec = TPACPI_MATCH_ANY, \
444 .quirks = (__quirk) }
446 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_LENOVO, \
448 .bios = TPID(__id1, __id2), \
449 .ec = TPACPI_MATCH_ANY, \
450 .quirks = (__quirk) }
452 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
453 { .vendor = PCI_VENDOR_ID_LENOVO, \
454 .bios = TPACPI_MATCH_ANY, \
455 .ec = TPID(__id1, __id2), \
456 .quirks = (__quirk) }
458 struct tpacpi_quirk {
462 unsigned long quirks;
466 * tpacpi_check_quirks() - search BIOS/EC version on a list
467 * @qlist: array of &struct tpacpi_quirk
468 * @qlist_size: number of elements in @qlist
470 * Iterates over a quirks list until one is found that matches the
471 * ThinkPad's vendor, BIOS and EC model.
473 * Returns 0 if nothing matches, otherwise returns the quirks field of
474 * the matching &struct tpacpi_quirk entry.
476 * The match criteria is: vendor, ec and bios much match.
478 static unsigned long __init tpacpi_check_quirks(
479 const struct tpacpi_quirk *qlist,
480 unsigned int qlist_size)
483 if ((qlist->vendor == thinkpad_id.vendor ||
484 qlist->vendor == TPACPI_MATCH_ANY) &&
485 (qlist->bios == thinkpad_id.bios_model ||
486 qlist->bios == TPACPI_MATCH_ANY) &&
487 (qlist->ec == thinkpad_id.ec_model ||
488 qlist->ec == TPACPI_MATCH_ANY))
489 return qlist->quirks;
497 static inline bool __pure __init tpacpi_is_lenovo(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
502 static inline bool __pure __init tpacpi_is_ibm(void)
504 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
507 /****************************************************************************
508 ****************************************************************************
510 * ACPI Helpers and device model
512 ****************************************************************************
513 ****************************************************************************/
515 /*************************************************************************
519 static acpi_handle root_handle;
520 static acpi_handle ec_handle;
522 #define TPACPI_HANDLE(object, parent, paths...) \
523 static acpi_handle object##_handle; \
524 static const acpi_handle * const object##_parent __initconst = \
526 static char *object##_paths[] __initdata = { paths }
528 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
529 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
531 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
533 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
534 "\\CMS", /* R40, R40e */
537 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
538 "^HKEY", /* R30, R31 */
539 "HKEY", /* all others */
542 /*************************************************************************
546 static int acpi_evalf(acpi_handle handle,
547 int *res, char *method, char *fmt, ...)
550 struct acpi_object_list params;
551 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
552 struct acpi_buffer result, *resultp;
553 union acpi_object out_obj;
561 pr_err("acpi_evalf() called with empty format\n");
574 params.pointer = &in_objs[0];
581 in_objs[params.count].integer.value = va_arg(ap, int);
582 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
584 /* add more types as needed */
586 pr_err("acpi_evalf() called "
587 "with invalid format character '%c'\n", c);
594 if (res_type != 'v') {
595 result.length = sizeof(out_obj);
596 result.pointer = &out_obj;
601 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
605 success = (status == AE_OK &&
606 out_obj.type == ACPI_TYPE_INTEGER);
608 *res = out_obj.integer.value;
611 success = status == AE_OK;
613 /* add more types as needed */
615 pr_err("acpi_evalf() called "
616 "with invalid format character '%c'\n", res_type);
620 if (!success && !quiet)
621 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
622 method, fmt0, acpi_format_exception(status));
627 static int acpi_ec_read(int i, u8 *p)
632 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
636 if (ec_read(i, p) < 0)
643 static int acpi_ec_write(int i, u8 v)
646 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
649 if (ec_write(i, v) < 0)
656 static int issue_thinkpad_cmos_command(int cmos_cmd)
661 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
667 /*************************************************************************
671 #define TPACPI_ACPIHANDLE_INIT(object) \
672 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
673 object##_paths, ARRAY_SIZE(object##_paths))
675 static void __init drv_acpi_handle_init(const char *name,
676 acpi_handle *handle, const acpi_handle parent,
677 char **paths, const int num_paths)
682 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
685 for (i = 0; i < num_paths; i++) {
686 status = acpi_get_handle(parent, paths[i], handle);
687 if (ACPI_SUCCESS(status)) {
688 dbg_printk(TPACPI_DBG_INIT,
689 "Found ACPI handle %s for %s\n",
695 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
700 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
701 u32 level, void *context, void **return_value)
703 *(acpi_handle *)return_value = handle;
705 return AE_CTRL_TERMINATE;
708 static void __init tpacpi_acpi_handle_locate(const char *name,
713 acpi_handle device_found;
715 BUG_ON(!name || !hid || !handle);
716 vdbg_printk(TPACPI_DBG_INIT,
717 "trying to locate ACPI handle for %s, using HID %s\n",
720 memset(&device_found, 0, sizeof(device_found));
721 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
722 (void *)name, &device_found);
726 if (ACPI_SUCCESS(status)) {
727 *handle = device_found;
728 dbg_printk(TPACPI_DBG_INIT,
729 "Found ACPI handle for %s\n", name);
731 vdbg_printk(TPACPI_DBG_INIT,
732 "Could not locate an ACPI handle for %s: %s\n",
733 name, acpi_format_exception(status));
737 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
739 struct ibm_struct *ibm = data;
741 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
744 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
747 ibm->acpi->notify(ibm, event);
750 static int __init setup_acpi_notify(struct ibm_struct *ibm)
757 if (!*ibm->acpi->handle)
760 vdbg_printk(TPACPI_DBG_INIT,
761 "setting up ACPI notify for %s\n", ibm->name);
763 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
765 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
769 ibm->acpi->device->driver_data = ibm;
770 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
771 TPACPI_ACPI_EVENT_PREFIX,
774 status = acpi_install_notify_handler(*ibm->acpi->handle,
775 ibm->acpi->type, dispatch_acpi_notify, ibm);
776 if (ACPI_FAILURE(status)) {
777 if (status == AE_ALREADY_EXISTS) {
778 pr_notice("another device driver is already "
779 "handling %s events\n", ibm->name);
781 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
782 ibm->name, acpi_format_exception(status));
786 ibm->flags.acpi_notify_installed = 1;
790 static int __init tpacpi_device_add(struct acpi_device *device)
795 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
799 dbg_printk(TPACPI_DBG_INIT,
800 "registering %s as an ACPI driver\n", ibm->name);
804 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
805 if (!ibm->acpi->driver) {
806 pr_err("failed to allocate memory for ibm->acpi->driver\n");
810 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
811 ibm->acpi->driver->ids = ibm->acpi->hid;
813 ibm->acpi->driver->ops.add = &tpacpi_device_add;
815 rc = acpi_bus_register_driver(ibm->acpi->driver);
817 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
819 kfree(ibm->acpi->driver);
820 ibm->acpi->driver = NULL;
822 ibm->flags.acpi_driver_registered = 1;
828 /****************************************************************************
829 ****************************************************************************
833 ****************************************************************************
834 ****************************************************************************/
836 static int dispatch_proc_show(struct seq_file *m, void *v)
838 struct ibm_struct *ibm = m->private;
840 if (!ibm || !ibm->read)
845 static int dispatch_proc_open(struct inode *inode, struct file *file)
847 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
850 static ssize_t dispatch_proc_write(struct file *file,
851 const char __user *userbuf,
852 size_t count, loff_t *pos)
854 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
858 if (!ibm || !ibm->write)
860 if (count > PAGE_SIZE - 2)
863 kernbuf = kmalloc(count + 2, GFP_KERNEL);
867 if (copy_from_user(kernbuf, userbuf, count)) {
873 strcat(kernbuf, ",");
874 ret = ibm->write(kernbuf);
883 static const struct file_operations dispatch_proc_fops = {
884 .owner = THIS_MODULE,
885 .open = dispatch_proc_open,
888 .release = single_release,
889 .write = dispatch_proc_write,
892 static char *next_cmd(char **cmds)
897 while ((end = strchr(start, ',')) && end == start)
909 /****************************************************************************
910 ****************************************************************************
912 * Device model: input, hwmon and platform
914 ****************************************************************************
915 ****************************************************************************/
917 static struct platform_device *tpacpi_pdev;
918 static struct platform_device *tpacpi_sensors_pdev;
919 static struct device *tpacpi_hwmon;
920 static struct input_dev *tpacpi_inputdev;
921 static struct mutex tpacpi_inputdev_send_mutex;
922 static LIST_HEAD(tpacpi_all_drivers);
924 #ifdef CONFIG_PM_SLEEP
925 static int tpacpi_suspend_handler(struct device *dev)
927 struct ibm_struct *ibm, *itmp;
929 list_for_each_entry_safe(ibm, itmp,
939 static int tpacpi_resume_handler(struct device *dev)
941 struct ibm_struct *ibm, *itmp;
943 list_for_each_entry_safe(ibm, itmp,
954 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
955 tpacpi_suspend_handler, tpacpi_resume_handler);
957 static void tpacpi_shutdown_handler(struct platform_device *pdev)
959 struct ibm_struct *ibm, *itmp;
961 list_for_each_entry_safe(ibm, itmp,
969 static struct platform_driver tpacpi_pdriver = {
971 .name = TPACPI_DRVR_NAME,
972 .owner = THIS_MODULE,
975 .shutdown = tpacpi_shutdown_handler,
978 static struct platform_driver tpacpi_hwmon_pdriver = {
980 .name = TPACPI_HWMON_DRVR_NAME,
981 .owner = THIS_MODULE,
985 /*************************************************************************
986 * sysfs support helpers
989 struct attribute_set {
990 unsigned int members, max_members;
991 struct attribute_group group;
994 struct attribute_set_obj {
995 struct attribute_set s;
997 } __attribute__((packed));
999 static struct attribute_set *create_attr_set(unsigned int max_members,
1002 struct attribute_set_obj *sobj;
1004 if (max_members == 0)
1007 /* Allocates space for implicit NULL at the end too */
1008 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1009 max_members * sizeof(struct attribute *),
1013 sobj->s.max_members = max_members;
1014 sobj->s.group.attrs = &sobj->a;
1015 sobj->s.group.name = name;
1020 #define destroy_attr_set(_set) \
1023 /* not multi-threaded safe, use it in a single thread per set */
1024 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1029 if (s->members >= s->max_members)
1032 s->group.attrs[s->members] = attr;
1038 static int add_many_to_attr_set(struct attribute_set *s,
1039 struct attribute **attr,
1044 for (i = 0; i < count; i++) {
1045 res = add_to_attr_set(s, attr[i]);
1053 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1055 sysfs_remove_group(kobj, &s->group);
1056 destroy_attr_set(s);
1059 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1060 sysfs_create_group(_kobj, &_attr_set->group)
1062 static int parse_strtoul(const char *buf,
1063 unsigned long max, unsigned long *value)
1067 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1068 endp = skip_spaces(endp);
1069 if (*endp || *value > max)
1075 static void tpacpi_disable_brightness_delay(void)
1077 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1078 pr_notice("ACPI backlight control delay disabled\n");
1081 static void printk_deprecated_attribute(const char * const what,
1082 const char * const details)
1084 tpacpi_log_usertask("deprecated sysfs attribute");
1085 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1086 "will be removed. %s\n",
1090 /*************************************************************************
1091 * rfkill and radio control support helpers
1095 * ThinkPad-ACPI firmware handling model:
1097 * WLSW (master wireless switch) is event-driven, and is common to all
1098 * firmware-controlled radios. It cannot be controlled, just monitored,
1099 * as expected. It overrides all radio state in firmware
1101 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1102 * (TODO: verify how WLSW interacts with the returned radio state).
1104 * The only time there are shadow radio state changes, is when
1105 * masked-off hotkeys are used.
1109 * Internal driver API for radio state:
1111 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1112 * bool: true means radio blocked (off)
1114 enum tpacpi_rfkill_state {
1115 TPACPI_RFK_RADIO_OFF = 0,
1119 /* rfkill switches */
1120 enum tpacpi_rfk_id {
1121 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1122 TPACPI_RFK_WWAN_SW_ID,
1123 TPACPI_RFK_UWB_SW_ID,
1127 static const char *tpacpi_rfkill_names[] = {
1128 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1129 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1130 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1131 [TPACPI_RFK_SW_MAX] = NULL
1134 /* ThinkPad-ACPI rfkill subdriver */
1136 struct rfkill *rfkill;
1137 enum tpacpi_rfk_id id;
1138 const struct tpacpi_rfk_ops *ops;
1141 struct tpacpi_rfk_ops {
1142 /* firmware interface */
1143 int (*get_status)(void);
1144 int (*set_status)(const enum tpacpi_rfkill_state);
1147 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1149 /* Query FW and update rfkill sw state for a given rfkill switch */
1150 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1157 status = (tp_rfk->ops->get_status)();
1161 rfkill_set_sw_state(tp_rfk->rfkill,
1162 (status == TPACPI_RFK_RADIO_OFF));
1167 /* Query FW and update rfkill sw state for all rfkill switches */
1168 static void tpacpi_rfk_update_swstate_all(void)
1172 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1173 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1177 * Sync the HW-blocking state of all rfkill switches,
1178 * do notice it causes the rfkill core to schedule uevents
1180 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1183 struct tpacpi_rfk *tp_rfk;
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1186 tp_rfk = tpacpi_rfkill_switches[i];
1188 if (rfkill_set_hw_state(tp_rfk->rfkill,
1190 /* ignore -- we track sw block */
1196 /* Call to get the WLSW state from the firmware */
1197 static int hotkey_get_wlsw(void);
1199 /* Call to query WLSW state and update all rfkill switches */
1200 static bool tpacpi_rfk_check_hwblock_state(void)
1202 int res = hotkey_get_wlsw();
1205 /* When unknown or unsupported, we have to assume it is unblocked */
1209 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1210 tpacpi_rfk_update_hwblock_state(hw_blocked);
1215 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1217 struct tpacpi_rfk *tp_rfk = data;
1220 dbg_printk(TPACPI_DBG_RFKILL,
1221 "request to change radio state to %s\n",
1222 blocked ? "blocked" : "unblocked");
1224 /* try to set radio state */
1225 res = (tp_rfk->ops->set_status)(blocked ?
1226 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1228 /* and update the rfkill core with whatever the FW really did */
1229 tpacpi_rfk_update_swstate(tp_rfk);
1231 return (res < 0) ? res : 0;
1234 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1235 .set_block = tpacpi_rfk_hook_set_block,
1238 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1239 const struct tpacpi_rfk_ops *tp_rfkops,
1240 const enum rfkill_type rfktype,
1242 const bool set_default)
1244 struct tpacpi_rfk *atp_rfk;
1246 bool sw_state = false;
1250 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1252 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1254 atp_rfk->rfkill = rfkill_alloc(name,
1257 &tpacpi_rfk_rfkill_ops,
1259 if (!atp_rfk || !atp_rfk->rfkill) {
1260 pr_err("failed to allocate memory for rfkill class\n");
1266 atp_rfk->ops = tp_rfkops;
1268 sw_status = (tp_rfkops->get_status)();
1269 if (sw_status < 0) {
1270 pr_err("failed to read initial state for %s, error %d\n",
1273 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1275 /* try to keep the initial state, since we ask the
1276 * firmware to preserve it across S5 in NVRAM */
1277 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1280 hw_state = tpacpi_rfk_check_hwblock_state();
1281 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1283 res = rfkill_register(atp_rfk->rfkill);
1285 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1286 rfkill_destroy(atp_rfk->rfkill);
1291 tpacpi_rfkill_switches[id] = atp_rfk;
1293 pr_info("rfkill switch %s: radio is %sblocked\n",
1294 name, (sw_state || hw_state) ? "" : "un");
1298 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1300 struct tpacpi_rfk *tp_rfk;
1302 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1304 tp_rfk = tpacpi_rfkill_switches[id];
1306 rfkill_unregister(tp_rfk->rfkill);
1307 rfkill_destroy(tp_rfk->rfkill);
1308 tpacpi_rfkill_switches[id] = NULL;
1313 static void printk_deprecated_rfkill_attribute(const char * const what)
1315 printk_deprecated_attribute(what,
1316 "Please switch to generic rfkill before year 2010");
1319 /* sysfs <radio> enable ------------------------------------------------ */
1320 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1321 struct device_attribute *attr,
1326 printk_deprecated_rfkill_attribute(attr->attr.name);
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state()) {
1330 status = TPACPI_RFK_RADIO_OFF;
1332 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1337 return snprintf(buf, PAGE_SIZE, "%d\n",
1338 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1341 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1342 struct device_attribute *attr,
1343 const char *buf, size_t count)
1348 printk_deprecated_rfkill_attribute(attr->attr.name);
1350 if (parse_strtoul(buf, 1, &t))
1353 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1355 /* This is in the ABI... */
1356 if (tpacpi_rfk_check_hwblock_state() && !!t)
1359 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1360 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1361 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1363 return (res < 0) ? res : count;
1366 /* procfs -------------------------------------------------------------- */
1367 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1369 if (id >= TPACPI_RFK_SW_MAX)
1370 seq_printf(m, "status:\t\tnot supported\n");
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state()) {
1376 status = TPACPI_RFK_RADIO_OFF;
1378 status = tpacpi_rfk_update_swstate(
1379 tpacpi_rfkill_switches[id]);
1384 seq_printf(m, "status:\t\t%s\n",
1385 (status == TPACPI_RFK_RADIO_ON) ?
1386 "enabled" : "disabled");
1387 seq_printf(m, "commands:\tenable, disable\n");
1393 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1399 if (id >= TPACPI_RFK_SW_MAX)
1402 while ((cmd = next_cmd(&buf))) {
1403 if (strlencmp(cmd, "enable") == 0)
1404 status = TPACPI_RFK_RADIO_ON;
1405 else if (strlencmp(cmd, "disable") == 0)
1406 status = TPACPI_RFK_RADIO_OFF;
1412 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1413 (status == TPACPI_RFK_RADIO_ON) ?
1414 "enable" : "disable",
1415 tpacpi_rfkill_names[id]);
1416 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1417 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1423 /*************************************************************************
1424 * thinkpad-acpi driver attributes
1427 /* interface_version --------------------------------------------------- */
1428 static ssize_t tpacpi_driver_interface_version_show(
1429 struct device_driver *drv,
1432 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1435 static DRIVER_ATTR(interface_version, S_IRUGO,
1436 tpacpi_driver_interface_version_show, NULL);
1438 /* debug_level --------------------------------------------------------- */
1439 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1442 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1445 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1446 const char *buf, size_t count)
1450 if (parse_strtoul(buf, 0xffff, &t))
1458 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1459 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1461 /* version ------------------------------------------------------------- */
1462 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1465 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1466 TPACPI_DESC, TPACPI_VERSION);
1469 static DRIVER_ATTR(version, S_IRUGO,
1470 tpacpi_driver_version_show, NULL);
1472 /* --------------------------------------------------------------------- */
1474 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1476 /* wlsw_emulstate ------------------------------------------------------ */
1477 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1480 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1483 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1484 const char *buf, size_t count)
1488 if (parse_strtoul(buf, 1, &t))
1491 if (tpacpi_wlsw_emulstate != !!t) {
1492 tpacpi_wlsw_emulstate = !!t;
1493 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1499 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1500 tpacpi_driver_wlsw_emulstate_show,
1501 tpacpi_driver_wlsw_emulstate_store);
1503 /* bluetooth_emulstate ------------------------------------------------- */
1504 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1505 struct device_driver *drv,
1508 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1511 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1512 struct device_driver *drv,
1513 const char *buf, size_t count)
1517 if (parse_strtoul(buf, 1, &t))
1520 tpacpi_bluetooth_emulstate = !!t;
1525 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1526 tpacpi_driver_bluetooth_emulstate_show,
1527 tpacpi_driver_bluetooth_emulstate_store);
1529 /* wwan_emulstate ------------------------------------------------- */
1530 static ssize_t tpacpi_driver_wwan_emulstate_show(
1531 struct device_driver *drv,
1534 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1537 static ssize_t tpacpi_driver_wwan_emulstate_store(
1538 struct device_driver *drv,
1539 const char *buf, size_t count)
1543 if (parse_strtoul(buf, 1, &t))
1546 tpacpi_wwan_emulstate = !!t;
1551 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1552 tpacpi_driver_wwan_emulstate_show,
1553 tpacpi_driver_wwan_emulstate_store);
1555 /* uwb_emulstate ------------------------------------------------- */
1556 static ssize_t tpacpi_driver_uwb_emulstate_show(
1557 struct device_driver *drv,
1560 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1563 static ssize_t tpacpi_driver_uwb_emulstate_store(
1564 struct device_driver *drv,
1565 const char *buf, size_t count)
1569 if (parse_strtoul(buf, 1, &t))
1572 tpacpi_uwb_emulstate = !!t;
1577 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1578 tpacpi_driver_uwb_emulstate_show,
1579 tpacpi_driver_uwb_emulstate_store);
1582 /* --------------------------------------------------------------------- */
1584 static struct driver_attribute *tpacpi_driver_attributes[] = {
1585 &driver_attr_debug_level, &driver_attr_version,
1586 &driver_attr_interface_version,
1589 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1595 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1596 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1600 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1601 if (!res && dbg_wlswemul)
1602 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1603 if (!res && dbg_bluetoothemul)
1604 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1605 if (!res && dbg_wwanemul)
1606 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1607 if (!res && dbg_uwbemul)
1608 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1614 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1618 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1619 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1621 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1622 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1623 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1624 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1625 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1629 /*************************************************************************
1634 * Table of recommended minimum BIOS versions
1636 * Reasons for listing:
1637 * 1. Stable BIOS, listed because the unknown amount of
1638 * bugs and bad ACPI behaviour on older versions
1640 * 2. BIOS or EC fw with known bugs that trigger on Linux
1642 * 3. BIOS with known reduced functionality in older versions
1644 * We recommend the latest BIOS and EC version.
1645 * We only support the latest BIOS and EC fw version as a rule.
1647 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1648 * Information from users in ThinkWiki
1650 * WARNING: we use this table also to detect that the machine is
1651 * a ThinkPad in some cases, so don't remove entries lightly.
1654 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1655 { .vendor = (__v), \
1656 .bios = TPID(__id1, __id2), \
1657 .ec = TPACPI_MATCH_ANY, \
1658 .quirks = TPACPI_MATCH_ANY << 16 \
1659 | (__bv1) << 8 | (__bv2) }
1661 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1662 __eid, __ev1, __ev2) \
1663 { .vendor = (__v), \
1664 .bios = TPID(__bid1, __bid2), \
1666 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1667 | (__bv1) << 8 | (__bv2) }
1669 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1670 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1672 /* Outdated IBM BIOSes often lack the EC id string */
1673 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1674 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1675 __bv1, __bv2, TPID(__id1, __id2), \
1677 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1678 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1681 /* Outdated IBM BIOSes often lack the EC id string */
1682 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1683 __eid1, __eid2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1685 __bv1, __bv2, TPID(__eid1, __eid2), \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1688 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1691 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1692 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1694 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1696 __bv1, __bv2, TPID(__id1, __id2), \
1699 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1700 __eid1, __eid2, __ev1, __ev2) \
1701 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1702 __bv1, __bv2, TPID(__eid1, __eid2), \
1705 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1706 /* Numeric models ------------------ */
1707 /* FW MODEL BIOS VERS */
1708 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1709 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1710 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1711 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1712 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1713 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1714 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1715 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1716 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1718 /* A-series ------------------------- */
1719 /* FW MODEL BIOS VERS EC VERS */
1720 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1721 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1722 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1723 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1724 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1725 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1726 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1727 TPV_QI0('1', '3', '2', '0'), /* A22m */
1728 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1729 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1730 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1732 /* G-series ------------------------- */
1733 /* FW MODEL BIOS VERS */
1734 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1735 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1737 /* R-series, T-series --------------- */
1738 /* FW MODEL BIOS VERS EC VERS */
1739 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1740 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1741 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1742 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1743 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1744 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1745 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1746 T40/p, T41/p, T42/p (1) */
1747 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1748 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1749 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1750 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1752 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1753 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1754 TPV_QI0('1', '6', '3', '2'), /* T22 */
1755 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1756 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1757 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1759 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1760 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1761 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1763 /* BIOS FW BIOS VERS EC FW EC VERS */
1764 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1765 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1767 /* X-series ------------------------- */
1768 /* FW MODEL BIOS VERS EC VERS */
1769 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1770 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1771 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1772 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1773 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1774 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1775 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1777 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1778 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1780 /* (0) - older versions lack DMI EC fw string and functionality */
1781 /* (1) - older versions known to lack functionality */
1792 static void __init tpacpi_check_outdated_fw(void)
1794 unsigned long fwvers;
1795 u16 ec_version, bios_version;
1797 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1798 ARRAY_SIZE(tpacpi_bios_version_qtable));
1803 bios_version = fwvers & 0xffffU;
1804 ec_version = (fwvers >> 16) & 0xffffU;
1806 /* note that unknown versions are set to 0x0000 and we use that */
1807 if ((bios_version > thinkpad_id.bios_release) ||
1808 (ec_version > thinkpad_id.ec_release &&
1809 ec_version != TPACPI_MATCH_ANY)) {
1811 * The changelogs would let us track down the exact
1812 * reason, but it is just too much of a pain to track
1813 * it. We only list BIOSes that are either really
1814 * broken, or really stable to begin with, so it is
1815 * best if the user upgrades the firmware anyway.
1817 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1818 pr_warn("WARNING: This firmware may be missing critical bug "
1819 "fixes and/or important features\n");
1823 static bool __init tpacpi_is_fw_known(void)
1825 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1826 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1829 /****************************************************************************
1830 ****************************************************************************
1834 ****************************************************************************
1835 ****************************************************************************/
1837 /*************************************************************************
1838 * thinkpad-acpi metadata subdriver
1841 static int thinkpad_acpi_driver_read(struct seq_file *m)
1843 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1844 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1848 static struct ibm_struct thinkpad_acpi_driver_data = {
1850 .read = thinkpad_acpi_driver_read,
1853 /*************************************************************************
1858 * ThinkPad firmware event model
1860 * The ThinkPad firmware has two main event interfaces: normal ACPI
1861 * notifications (which follow the ACPI standard), and a private event
1864 * The private event interface also issues events for the hotkeys. As
1865 * the driver gained features, the event handling code ended up being
1866 * built around the hotkey subdriver. This will need to be refactored
1867 * to a more formal event API eventually.
1869 * Some "hotkeys" are actually supposed to be used as event reports,
1870 * such as "brightness has changed", "volume has changed", depending on
1871 * the ThinkPad model and how the firmware is operating.
1873 * Unlike other classes, hotkey-class events have mask/unmask control on
1874 * non-ancient firmware. However, how it behaves changes a lot with the
1875 * firmware model and version.
1878 enum { /* hot key scan codes (derived from ACPI DSDT) */
1879 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1880 TP_ACPI_HOTKEYSCAN_FNF2,
1881 TP_ACPI_HOTKEYSCAN_FNF3,
1882 TP_ACPI_HOTKEYSCAN_FNF4,
1883 TP_ACPI_HOTKEYSCAN_FNF5,
1884 TP_ACPI_HOTKEYSCAN_FNF6,
1885 TP_ACPI_HOTKEYSCAN_FNF7,
1886 TP_ACPI_HOTKEYSCAN_FNF8,
1887 TP_ACPI_HOTKEYSCAN_FNF9,
1888 TP_ACPI_HOTKEYSCAN_FNF10,
1889 TP_ACPI_HOTKEYSCAN_FNF11,
1890 TP_ACPI_HOTKEYSCAN_FNF12,
1891 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1892 TP_ACPI_HOTKEYSCAN_FNINSERT,
1893 TP_ACPI_HOTKEYSCAN_FNDELETE,
1894 TP_ACPI_HOTKEYSCAN_FNHOME,
1895 TP_ACPI_HOTKEYSCAN_FNEND,
1896 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1897 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1898 TP_ACPI_HOTKEYSCAN_FNSPACE,
1899 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1900 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1901 TP_ACPI_HOTKEYSCAN_MUTE,
1902 TP_ACPI_HOTKEYSCAN_THINKPAD,
1903 TP_ACPI_HOTKEYSCAN_UNK1,
1904 TP_ACPI_HOTKEYSCAN_UNK2,
1905 TP_ACPI_HOTKEYSCAN_UNK3,
1906 TP_ACPI_HOTKEYSCAN_UNK4,
1907 TP_ACPI_HOTKEYSCAN_UNK5,
1908 TP_ACPI_HOTKEYSCAN_UNK6,
1909 TP_ACPI_HOTKEYSCAN_UNK7,
1910 TP_ACPI_HOTKEYSCAN_UNK8,
1912 /* Hotkey keymap size */
1913 TPACPI_HOTKEY_MAP_LEN
1916 enum { /* Keys/events available through NVRAM polling */
1917 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1918 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1921 enum { /* Positions of some of the keys in hotkey masks */
1922 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1923 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1924 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1925 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1926 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1927 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1928 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1929 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1930 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1931 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1932 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1935 enum { /* NVRAM to ACPI HKEY group map */
1936 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1937 TP_ACPI_HKEY_ZOOM_MASK |
1938 TP_ACPI_HKEY_DISPSWTCH_MASK |
1939 TP_ACPI_HKEY_HIBERNATE_MASK,
1940 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1941 TP_ACPI_HKEY_BRGHTDWN_MASK,
1942 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1943 TP_ACPI_HKEY_VOLDWN_MASK |
1944 TP_ACPI_HKEY_MUTE_MASK,
1947 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1948 struct tp_nvram_state {
1949 u16 thinkpad_toggle:1;
1951 u16 display_toggle:1;
1952 u16 thinklight_toggle:1;
1953 u16 hibernate_toggle:1;
1954 u16 displayexp_toggle:1;
1955 u16 display_state:1;
1956 u16 brightness_toggle:1;
1957 u16 volume_toggle:1;
1960 u8 brightness_level;
1964 /* kthread for the hotkey poller */
1965 static struct task_struct *tpacpi_hotkey_task;
1968 * Acquire mutex to write poller control variables as an
1971 * Increment hotkey_config_change when changing them if you
1972 * want the kthread to forget old state.
1974 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1976 static struct mutex hotkey_thread_data_mutex;
1977 static unsigned int hotkey_config_change;
1980 * hotkey poller control variables
1982 * Must be atomic or readers will also need to acquire mutex
1984 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1985 * should be used only when the changes need to be taken as
1986 * a block, OR when one needs to force the kthread to forget
1989 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1990 static unsigned int hotkey_poll_freq = 10; /* Hz */
1992 #define HOTKEY_CONFIG_CRITICAL_START \
1994 mutex_lock(&hotkey_thread_data_mutex); \
1995 hotkey_config_change++; \
1997 #define HOTKEY_CONFIG_CRITICAL_END \
1998 mutex_unlock(&hotkey_thread_data_mutex);
2000 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2002 #define hotkey_source_mask 0U
2003 #define HOTKEY_CONFIG_CRITICAL_START
2004 #define HOTKEY_CONFIG_CRITICAL_END
2006 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2008 static struct mutex hotkey_mutex;
2010 static enum { /* Reasons for waking up */
2011 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2012 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2013 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2014 } hotkey_wakeup_reason;
2016 static int hotkey_autosleep_ack;
2018 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2019 static u32 hotkey_all_mask; /* all events supported in fw */
2020 static u32 hotkey_reserved_mask; /* events better left disabled */
2021 static u32 hotkey_driver_mask; /* events needed by the driver */
2022 static u32 hotkey_user_mask; /* events visible to userspace */
2023 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2025 static unsigned int hotkey_report_mode;
2027 static u16 *hotkey_keycode_map;
2029 static struct attribute_set *hotkey_dev_attributes;
2031 static void tpacpi_driver_event(const unsigned int hkey_event);
2032 static void hotkey_driver_event(const unsigned int scancode);
2033 static void hotkey_poll_setup(const bool may_warn);
2035 /* HKEY.MHKG() return bits */
2036 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2038 static int hotkey_get_wlsw(void)
2042 if (!tp_features.hotkey_wlsw)
2045 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2047 return (tpacpi_wlsw_emulstate) ?
2048 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2051 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2054 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2057 static int hotkey_get_tablet_mode(int *status)
2061 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2064 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2069 * Reads current event mask from firmware, and updates
2070 * hotkey_acpi_mask accordingly. Also resets any bits
2071 * from hotkey_user_mask that are unavailable to be
2072 * delivered (shadow requirement of the userspace ABI).
2074 * Call with hotkey_mutex held
2076 static int hotkey_mask_get(void)
2078 if (tp_features.hotkey_mask) {
2081 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2084 hotkey_acpi_mask = m;
2086 /* no mask support doesn't mean no event support... */
2087 hotkey_acpi_mask = hotkey_all_mask;
2090 /* sync userspace-visible mask */
2091 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2096 void static hotkey_mask_warn_incomplete_mask(void)
2098 /* log only what the user can fix... */
2099 const u32 wantedmask = hotkey_driver_mask &
2100 ~(hotkey_acpi_mask | hotkey_source_mask) &
2101 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2104 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2108 * Set the firmware mask when supported
2110 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2112 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2114 * Call with hotkey_mutex held
2116 static int hotkey_mask_set(u32 mask)
2121 const u32 fwmask = mask & ~hotkey_source_mask;
2123 if (tp_features.hotkey_mask) {
2124 for (i = 0; i < 32; i++) {
2125 if (!acpi_evalf(hkey_handle,
2126 NULL, "MHKM", "vdd", i + 1,
2127 !!(mask & (1 << i)))) {
2135 * We *must* make an inconditional call to hotkey_mask_get to
2136 * refresh hotkey_acpi_mask and update hotkey_user_mask
2138 * Take the opportunity to also log when we cannot _enable_
2141 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2142 pr_notice("asked for hotkey mask 0x%08x, but "
2143 "firmware forced it to 0x%08x\n",
2144 fwmask, hotkey_acpi_mask);
2147 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2148 hotkey_mask_warn_incomplete_mask();
2154 * Sets hotkey_user_mask and tries to set the firmware mask
2156 * Call with hotkey_mutex held
2158 static int hotkey_user_mask_set(const u32 mask)
2162 /* Give people a chance to notice they are doing something that
2163 * is bound to go boom on their users sooner or later */
2164 if (!tp_warned.hotkey_mask_ff &&
2165 (mask == 0xffff || mask == 0xffffff ||
2166 mask == 0xffffffff)) {
2167 tp_warned.hotkey_mask_ff = 1;
2168 pr_notice("setting the hotkey mask to 0x%08x is likely "
2169 "not the best way to go about it\n", mask);
2170 pr_notice("please consider using the driver defaults, "
2171 "and refer to up-to-date thinkpad-acpi "
2175 /* Try to enable what the user asked for, plus whatever we need.
2176 * this syncs everything but won't enable bits in hotkey_user_mask */
2177 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2179 /* Enable the available bits in hotkey_user_mask */
2180 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2186 * Sets the driver hotkey mask.
2188 * Can be called even if the hotkey subdriver is inactive
2190 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2194 /* Do the right thing if hotkey_init has not been called yet */
2195 if (!tp_features.hotkey) {
2196 hotkey_driver_mask = mask;
2200 mutex_lock(&hotkey_mutex);
2202 HOTKEY_CONFIG_CRITICAL_START
2203 hotkey_driver_mask = mask;
2204 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2205 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2207 HOTKEY_CONFIG_CRITICAL_END
2209 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2210 ~hotkey_source_mask);
2211 hotkey_poll_setup(true);
2213 mutex_unlock(&hotkey_mutex);
2218 static int hotkey_status_get(int *status)
2220 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2226 static int hotkey_status_set(bool enable)
2228 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2234 static void tpacpi_input_send_tabletsw(void)
2238 if (tp_features.hotkey_tablet &&
2239 !hotkey_get_tablet_mode(&state)) {
2240 mutex_lock(&tpacpi_inputdev_send_mutex);
2242 input_report_switch(tpacpi_inputdev,
2243 SW_TABLET_MODE, !!state);
2244 input_sync(tpacpi_inputdev);
2246 mutex_unlock(&tpacpi_inputdev_send_mutex);
2250 /* Do NOT call without validating scancode first */
2251 static void tpacpi_input_send_key(const unsigned int scancode)
2253 const unsigned int keycode = hotkey_keycode_map[scancode];
2255 if (keycode != KEY_RESERVED) {
2256 mutex_lock(&tpacpi_inputdev_send_mutex);
2258 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2259 input_report_key(tpacpi_inputdev, keycode, 1);
2260 input_sync(tpacpi_inputdev);
2262 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2263 input_report_key(tpacpi_inputdev, keycode, 0);
2264 input_sync(tpacpi_inputdev);
2266 mutex_unlock(&tpacpi_inputdev_send_mutex);
2270 /* Do NOT call without validating scancode first */
2271 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2273 hotkey_driver_event(scancode);
2274 if (hotkey_user_mask & (1 << scancode))
2275 tpacpi_input_send_key(scancode);
2278 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2279 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2281 /* Do NOT call without validating scancode first */
2282 static void tpacpi_hotkey_send_key(unsigned int scancode)
2284 tpacpi_input_send_key_masked(scancode);
2285 if (hotkey_report_mode < 2) {
2286 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2287 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2291 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2295 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2296 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2297 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2298 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2299 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2300 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2302 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2303 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2304 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2306 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2308 n->displayexp_toggle =
2309 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2311 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2312 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2313 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2314 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2315 n->brightness_toggle =
2316 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2318 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2319 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2320 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2321 >> TP_NVRAM_POS_LEVEL_VOLUME;
2322 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2323 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2327 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2328 struct tp_nvram_state *newn,
2329 const u32 event_mask)
2332 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2334 if ((event_mask & (1 << __scancode)) && \
2335 oldn->__member != newn->__member) \
2336 tpacpi_hotkey_send_key(__scancode); \
2339 #define TPACPI_MAY_SEND_KEY(__scancode) \
2341 if (event_mask & (1 << __scancode)) \
2342 tpacpi_hotkey_send_key(__scancode); \
2345 void issue_volchange(const unsigned int oldvol,
2346 const unsigned int newvol)
2348 unsigned int i = oldvol;
2350 while (i > newvol) {
2351 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2354 while (i < newvol) {
2355 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2360 void issue_brightnesschange(const unsigned int oldbrt,
2361 const unsigned int newbrt)
2363 unsigned int i = oldbrt;
2365 while (i > newbrt) {
2366 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2369 while (i < newbrt) {
2370 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2376 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2378 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2380 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2382 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2387 * This code is supposed to duplicate the IBM firmware behaviour:
2388 * - Pressing MUTE issues mute hotkey message, even when already mute
2389 * - Pressing Volume up/down issues volume up/down hotkey messages,
2390 * even when already at maximum or minimum volume
2391 * - The act of unmuting issues volume up/down notification,
2392 * depending which key was used to unmute
2394 * We are constrained to what the NVRAM can tell us, which is not much
2395 * and certainly not enough if more than one volume hotkey was pressed
2396 * since the last poll cycle.
2398 * Just to make our life interesting, some newer Lenovo ThinkPads have
2399 * bugs in the BIOS and may fail to update volume_toggle properly.
2404 oldn->volume_toggle != newn->volume_toggle ||
2405 oldn->volume_level != newn->volume_level) {
2406 /* recently muted, or repeated mute keypress, or
2407 * multiple presses ending in mute */
2408 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);
2419 } else if (oldn->volume_toggle != newn->volume_toggle) {
2420 /* repeated vol up/down keypress at end of scale ? */
2421 if (newn->volume_level == 0)
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2423 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2428 /* handle brightness */
2429 if (oldn->brightness_level != newn->brightness_level) {
2430 issue_brightnesschange(oldn->brightness_level,
2431 newn->brightness_level);
2432 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2433 /* repeated key presses that didn't change state */
2434 if (newn->brightness_level == 0)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2436 else if (newn->brightness_level >= bright_maxlvl
2437 && !tp_features.bright_unkfw)
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2441 #undef TPACPI_COMPARE_KEY
2442 #undef TPACPI_MAY_SEND_KEY
2448 * We track all events in hotkey_source_mask all the time, since
2449 * most of them are edge-based. We only issue those requested by
2450 * hotkey_user_mask or hotkey_driver_mask, though.
2452 static int hotkey_kthread(void *data)
2454 struct tp_nvram_state s[2];
2455 u32 poll_mask, event_mask;
2456 unsigned int si, so;
2458 unsigned int change_detector;
2459 unsigned int poll_freq;
2462 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2471 /* Initial state for compares */
2472 mutex_lock(&hotkey_thread_data_mutex);
2473 change_detector = hotkey_config_change;
2474 poll_mask = hotkey_source_mask;
2475 event_mask = hotkey_source_mask &
2476 (hotkey_driver_mask | hotkey_user_mask);
2477 poll_freq = hotkey_poll_freq;
2478 mutex_unlock(&hotkey_thread_data_mutex);
2479 hotkey_read_nvram(&s[so], poll_mask);
2481 while (!kthread_should_stop()) {
2483 if (likely(poll_freq))
2486 t = 100; /* should never happen... */
2488 t = msleep_interruptible(t);
2489 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2492 if (t > 0 && !was_frozen)
2495 mutex_lock(&hotkey_thread_data_mutex);
2496 if (was_frozen || hotkey_config_change != change_detector) {
2497 /* forget old state on thaw or config change */
2500 change_detector = hotkey_config_change;
2502 poll_mask = hotkey_source_mask;
2503 event_mask = hotkey_source_mask &
2504 (hotkey_driver_mask | hotkey_user_mask);
2505 poll_freq = hotkey_poll_freq;
2506 mutex_unlock(&hotkey_thread_data_mutex);
2508 if (likely(poll_mask)) {
2509 hotkey_read_nvram(&s[si], poll_mask);
2510 if (likely(si != so)) {
2511 hotkey_compare_and_issue_event(&s[so], &s[si],
2524 /* call with hotkey_mutex held */
2525 static void hotkey_poll_stop_sync(void)
2527 if (tpacpi_hotkey_task) {
2528 kthread_stop(tpacpi_hotkey_task);
2529 tpacpi_hotkey_task = NULL;
2533 /* call with hotkey_mutex held */
2534 static void hotkey_poll_setup(const bool may_warn)
2536 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2537 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2539 if (hotkey_poll_freq > 0 &&
2540 (poll_driver_mask ||
2541 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2542 if (!tpacpi_hotkey_task) {
2543 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2544 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2545 if (IS_ERR(tpacpi_hotkey_task)) {
2546 tpacpi_hotkey_task = NULL;
2547 pr_err("could not create kernel thread "
2548 "for hotkey polling\n");
2552 hotkey_poll_stop_sync();
2553 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2554 hotkey_poll_freq == 0) {
2555 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2556 "require polling, which is currently "
2558 poll_user_mask, poll_driver_mask);
2563 static void hotkey_poll_setup_safe(const bool may_warn)
2565 mutex_lock(&hotkey_mutex);
2566 hotkey_poll_setup(may_warn);
2567 mutex_unlock(&hotkey_mutex);
2570 /* call with hotkey_mutex held */
2571 static void hotkey_poll_set_freq(unsigned int freq)
2574 hotkey_poll_stop_sync();
2576 hotkey_poll_freq = freq;
2579 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2581 static void hotkey_poll_setup(const bool __unused)
2585 static void hotkey_poll_setup_safe(const bool __unused)
2589 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2591 static int hotkey_inputdev_open(struct input_dev *dev)
2593 switch (tpacpi_lifecycle) {
2594 case TPACPI_LIFE_INIT:
2595 case TPACPI_LIFE_RUNNING:
2596 hotkey_poll_setup_safe(false);
2598 case TPACPI_LIFE_EXITING:
2602 /* Should only happen if tpacpi_lifecycle is corrupt */
2607 static void hotkey_inputdev_close(struct input_dev *dev)
2609 /* disable hotkey polling when possible */
2610 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2611 !(hotkey_source_mask & hotkey_driver_mask))
2612 hotkey_poll_setup_safe(false);
2615 /* sysfs hotkey enable ------------------------------------------------- */
2616 static ssize_t hotkey_enable_show(struct device *dev,
2617 struct device_attribute *attr,
2622 printk_deprecated_attribute("hotkey_enable",
2623 "Hotkey reporting is always enabled");
2625 res = hotkey_status_get(&status);
2629 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2632 static ssize_t hotkey_enable_store(struct device *dev,
2633 struct device_attribute *attr,
2634 const char *buf, size_t count)
2638 printk_deprecated_attribute("hotkey_enable",
2639 "Hotkeys can be disabled through hotkey_mask");
2641 if (parse_strtoul(buf, 1, &t))
2650 static struct device_attribute dev_attr_hotkey_enable =
2651 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2652 hotkey_enable_show, hotkey_enable_store);
2654 /* sysfs hotkey mask --------------------------------------------------- */
2655 static ssize_t hotkey_mask_show(struct device *dev,
2656 struct device_attribute *attr,
2659 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2662 static ssize_t hotkey_mask_store(struct device *dev,
2663 struct device_attribute *attr,
2664 const char *buf, size_t count)
2669 if (parse_strtoul(buf, 0xffffffffUL, &t))
2672 if (mutex_lock_killable(&hotkey_mutex))
2673 return -ERESTARTSYS;
2675 res = hotkey_user_mask_set(t);
2677 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2678 hotkey_poll_setup(true);
2681 mutex_unlock(&hotkey_mutex);
2683 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2685 return (res) ? res : count;
2688 static struct device_attribute dev_attr_hotkey_mask =
2689 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2690 hotkey_mask_show, hotkey_mask_store);
2692 /* sysfs hotkey bios_enabled ------------------------------------------- */
2693 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2694 struct device_attribute *attr,
2697 return sprintf(buf, "0\n");
2700 static struct device_attribute dev_attr_hotkey_bios_enabled =
2701 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2703 /* sysfs hotkey bios_mask ---------------------------------------------- */
2704 static ssize_t hotkey_bios_mask_show(struct device *dev,
2705 struct device_attribute *attr,
2708 printk_deprecated_attribute("hotkey_bios_mask",
2709 "This attribute is useless.");
2710 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2713 static struct device_attribute dev_attr_hotkey_bios_mask =
2714 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2716 /* sysfs hotkey all_mask ----------------------------------------------- */
2717 static ssize_t hotkey_all_mask_show(struct device *dev,
2718 struct device_attribute *attr,
2721 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2722 hotkey_all_mask | hotkey_source_mask);
2725 static struct device_attribute dev_attr_hotkey_all_mask =
2726 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2728 /* sysfs hotkey recommended_mask --------------------------------------- */
2729 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2730 struct device_attribute *attr,
2733 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2734 (hotkey_all_mask | hotkey_source_mask)
2735 & ~hotkey_reserved_mask);
2738 static struct device_attribute dev_attr_hotkey_recommended_mask =
2739 __ATTR(hotkey_recommended_mask, S_IRUGO,
2740 hotkey_recommended_mask_show, NULL);
2742 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2744 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2745 static ssize_t hotkey_source_mask_show(struct device *dev,
2746 struct device_attribute *attr,
2749 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2752 static ssize_t hotkey_source_mask_store(struct device *dev,
2753 struct device_attribute *attr,
2754 const char *buf, size_t count)
2760 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2761 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2764 if (mutex_lock_killable(&hotkey_mutex))
2765 return -ERESTARTSYS;
2767 HOTKEY_CONFIG_CRITICAL_START
2768 hotkey_source_mask = t;
2769 HOTKEY_CONFIG_CRITICAL_END
2771 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2772 ~hotkey_source_mask);
2773 hotkey_poll_setup(true);
2775 /* check if events needed by the driver got disabled */
2776 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2777 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2779 mutex_unlock(&hotkey_mutex);
2782 pr_err("hotkey_source_mask: "
2783 "failed to update the firmware event mask!\n");
2786 pr_notice("hotkey_source_mask: "
2787 "some important events were disabled: 0x%04x\n",
2790 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2792 return (rc < 0) ? rc : count;
2795 static struct device_attribute dev_attr_hotkey_source_mask =
2796 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2797 hotkey_source_mask_show, hotkey_source_mask_store);
2799 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2800 static ssize_t hotkey_poll_freq_show(struct device *dev,
2801 struct device_attribute *attr,
2804 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2807 static ssize_t hotkey_poll_freq_store(struct device *dev,
2808 struct device_attribute *attr,
2809 const char *buf, size_t count)
2813 if (parse_strtoul(buf, 25, &t))
2816 if (mutex_lock_killable(&hotkey_mutex))
2817 return -ERESTARTSYS;
2819 hotkey_poll_set_freq(t);
2820 hotkey_poll_setup(true);
2822 mutex_unlock(&hotkey_mutex);
2824 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2829 static struct device_attribute dev_attr_hotkey_poll_freq =
2830 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2831 hotkey_poll_freq_show, hotkey_poll_freq_store);
2833 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2835 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2836 static ssize_t hotkey_radio_sw_show(struct device *dev,
2837 struct device_attribute *attr,
2841 res = hotkey_get_wlsw();
2845 /* Opportunistic update */
2846 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2848 return snprintf(buf, PAGE_SIZE, "%d\n",
2849 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2852 static struct device_attribute dev_attr_hotkey_radio_sw =
2853 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2855 static void hotkey_radio_sw_notify_change(void)
2857 if (tp_features.hotkey_wlsw)
2858 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2862 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2863 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2864 struct device_attribute *attr,
2868 res = hotkey_get_tablet_mode(&s);
2872 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2875 static struct device_attribute dev_attr_hotkey_tablet_mode =
2876 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2878 static void hotkey_tablet_mode_notify_change(void)
2880 if (tp_features.hotkey_tablet)
2881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2882 "hotkey_tablet_mode");
2885 /* sysfs hotkey report_mode -------------------------------------------- */
2886 static ssize_t hotkey_report_mode_show(struct device *dev,
2887 struct device_attribute *attr,
2890 return snprintf(buf, PAGE_SIZE, "%d\n",
2891 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2894 static struct device_attribute dev_attr_hotkey_report_mode =
2895 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2897 /* sysfs wakeup reason (pollable) -------------------------------------- */
2898 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2899 struct device_attribute *attr,
2902 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2905 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2906 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2908 static void hotkey_wakeup_reason_notify_change(void)
2910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2914 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2915 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2916 struct device_attribute *attr,
2919 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2922 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2923 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2924 hotkey_wakeup_hotunplug_complete_show, NULL);
2926 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2928 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2929 "wakeup_hotunplug_complete");
2932 /* --------------------------------------------------------------------- */
2934 static struct attribute *hotkey_attributes[] __initdata = {
2935 &dev_attr_hotkey_enable.attr,
2936 &dev_attr_hotkey_bios_enabled.attr,
2937 &dev_attr_hotkey_bios_mask.attr,
2938 &dev_attr_hotkey_report_mode.attr,
2939 &dev_attr_hotkey_wakeup_reason.attr,
2940 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2941 &dev_attr_hotkey_mask.attr,
2942 &dev_attr_hotkey_all_mask.attr,
2943 &dev_attr_hotkey_recommended_mask.attr,
2944 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2945 &dev_attr_hotkey_source_mask.attr,
2946 &dev_attr_hotkey_poll_freq.attr,
2951 * Sync both the hw and sw blocking state of all switches
2953 static void tpacpi_send_radiosw_update(void)
2958 * We must sync all rfkill controllers *before* issuing any
2959 * rfkill input events, or we will race the rfkill core input
2962 * tpacpi_inputdev_send_mutex works as a synchronization point
2965 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2968 wlsw = hotkey_get_wlsw();
2970 /* Sync hw blocking state first if it is hw-blocked */
2971 if (wlsw == TPACPI_RFK_RADIO_OFF)
2972 tpacpi_rfk_update_hwblock_state(true);
2974 /* Sync sw blocking state */
2975 tpacpi_rfk_update_swstate_all();
2977 /* Sync hw blocking state last if it is hw-unblocked */
2978 if (wlsw == TPACPI_RFK_RADIO_ON)
2979 tpacpi_rfk_update_hwblock_state(false);
2981 /* Issue rfkill input event for WLSW switch */
2983 mutex_lock(&tpacpi_inputdev_send_mutex);
2985 input_report_switch(tpacpi_inputdev,
2986 SW_RFKILL_ALL, (wlsw > 0));
2987 input_sync(tpacpi_inputdev);
2989 mutex_unlock(&tpacpi_inputdev_send_mutex);
2993 * this can be unconditional, as we will poll state again
2994 * if userspace uses the notify to read data
2996 hotkey_radio_sw_notify_change();
2999 static void hotkey_exit(void)
3001 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3002 mutex_lock(&hotkey_mutex);
3003 hotkey_poll_stop_sync();
3004 mutex_unlock(&hotkey_mutex);
3007 if (hotkey_dev_attributes)
3008 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3010 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3011 "restoring original HKEY status and mask\n");
3012 /* yes, there is a bitwise or below, we want the
3013 * functions to be called even if one of them fail */
3014 if (((tp_features.hotkey_mask &&
3015 hotkey_mask_set(hotkey_orig_mask)) |
3016 hotkey_status_set(false)) != 0)
3017 pr_err("failed to restore hot key mask "
3018 "to BIOS defaults\n");
3021 static void __init hotkey_unmap(const unsigned int scancode)
3023 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3024 clear_bit(hotkey_keycode_map[scancode],
3025 tpacpi_inputdev->keybit);
3026 hotkey_keycode_map[scancode] = KEY_RESERVED;
3032 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3035 #define TPACPI_HK_Q_INIMASK 0x0001
3037 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3038 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3039 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3040 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3041 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3042 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3043 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3044 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3045 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3046 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3047 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3048 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3049 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3050 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3051 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3052 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3053 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3054 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3055 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3056 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3059 typedef u16 tpacpi_keymap_entry_t;
3060 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3062 static int __init hotkey_init(struct ibm_init_struct *iibm)
3064 /* Requirements for changing the default keymaps:
3066 * 1. Many of the keys are mapped to KEY_RESERVED for very
3067 * good reasons. Do not change them unless you have deep
3068 * knowledge on the IBM and Lenovo ThinkPad firmware for
3069 * the various ThinkPad models. The driver behaves
3070 * differently for KEY_RESERVED: such keys have their
3071 * hot key mask *unset* in mask_recommended, and also
3072 * in the initial hot key mask programmed into the
3073 * firmware at driver load time, which means the firm-
3074 * ware may react very differently if you change them to
3077 * 2. You must be subscribed to the linux-thinkpad and
3078 * ibm-acpi-devel mailing lists, and you should read the
3079 * list archives since 2007 if you want to change the
3080 * keymaps. This requirement exists so that you will
3081 * know the past history of problems with the thinkpad-
3082 * acpi driver keymaps, and also that you will be
3083 * listening to any bug reports;
3085 * 3. Do not send thinkpad-acpi specific patches directly to
3086 * for merging, *ever*. Send them to the linux-acpi
3087 * mailinglist for comments. Merging is to be done only
3088 * through acpi-test and the ACPI maintainer.
3090 * If the above is too much to ask, don't change the keymap.
3091 * Ask the thinkpad-acpi maintainer to do it, instead.
3095 TPACPI_KEYMAP_IBM_GENERIC = 0,
3096 TPACPI_KEYMAP_LENOVO_GENERIC,
3099 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3100 /* Generic keymap for IBM ThinkPads */
3101 [TPACPI_KEYMAP_IBM_GENERIC] = {
3102 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3103 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3104 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3105 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3107 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3108 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3109 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3110 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3112 /* brightness: firmware always reacts to them */
3113 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3114 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3116 /* Thinklight: firmware always react to it */
3117 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3119 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3120 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3122 /* Volume: firmware always react to it and reprograms
3123 * the built-in *extra* mixer. Never map it to control
3124 * another mixer by default. */
3125 KEY_RESERVED, /* 0x14: VOLUME UP */
3126 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3127 KEY_RESERVED, /* 0x16: MUTE */
3129 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3131 /* (assignments unknown, please report if found) */
3132 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3133 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3136 /* Generic keymap for Lenovo ThinkPads */
3137 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3138 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3139 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3140 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3141 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3143 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3144 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3145 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3146 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3148 /* These should be enabled --only-- when ACPI video
3149 * is disabled (i.e. in "vendor" mode), and are handled
3150 * in a special way by the init code */
3151 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3152 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3154 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3156 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3157 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3159 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3160 * react to it and reprograms the built-in *extra* mixer.
3161 * Never map it to control another mixer by default.
3163 * T60?, T61, R60?, R61: firmware and EC tries to send
3164 * these over the regular keyboard, so these are no-ops,
3165 * but there are still weird bugs re. MUTE, so do not
3166 * change unless you get test reports from all Lenovo
3167 * models. May cause the BIOS to interfere with the
3170 KEY_RESERVED, /* 0x14: VOLUME UP */
3171 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3172 KEY_RESERVED, /* 0x16: MUTE */
3174 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3176 /* (assignments unknown, please report if found) */
3177 KEY_UNKNOWN, KEY_UNKNOWN,
3180 * The mic mute button only sends 0x1a. It does not
3181 * automatically mute the mic or change the mute light.
3183 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3185 /* (assignments unknown, please report if found) */
3186 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3192 /* Generic maps (fallback) */
3194 .vendor = PCI_VENDOR_ID_IBM,
3195 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3196 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3199 .vendor = PCI_VENDOR_ID_LENOVO,
3200 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3201 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3205 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3206 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3211 bool radiosw_state = false;
3212 bool tabletsw_state = false;
3214 unsigned long quirks;
3215 unsigned long keymap_id;
3217 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3218 "initializing hotkey subdriver\n");
3220 BUG_ON(!tpacpi_inputdev);
3221 BUG_ON(tpacpi_inputdev->open != NULL ||
3222 tpacpi_inputdev->close != NULL);
3224 TPACPI_ACPIHANDLE_INIT(hkey);
3225 mutex_init(&hotkey_mutex);
3227 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3228 mutex_init(&hotkey_thread_data_mutex);
3231 /* hotkey not supported on 570 */
3232 tp_features.hotkey = hkey_handle != NULL;
3234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3236 str_supported(tp_features.hotkey));
3238 if (!tp_features.hotkey)
3241 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3242 ARRAY_SIZE(tpacpi_hotkey_qtable));
3244 tpacpi_disable_brightness_delay();
3246 /* MUST have enough space for all attributes to be added to
3247 * hotkey_dev_attributes */
3248 hotkey_dev_attributes = create_attr_set(
3249 ARRAY_SIZE(hotkey_attributes) + 2,
3251 if (!hotkey_dev_attributes)
3253 res = add_many_to_attr_set(hotkey_dev_attributes,
3255 ARRAY_SIZE(hotkey_attributes));
3259 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3260 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3261 for HKEY interface version 0x100 */
3262 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3263 if ((hkeyv >> 8) != 1) {
3264 pr_err("unknown version of the HKEY interface: 0x%x\n",
3266 pr_err("please report this to %s\n", TPACPI_MAIL);
3269 * MHKV 0x100 in A31, R40, R40e,
3270 * T4x, X31, and later
3272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3273 "firmware HKEY interface version: 0x%x\n",
3276 /* Paranoia check AND init hotkey_all_mask */
3277 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3279 pr_err("missing MHKA handler, "
3280 "please report this to %s\n",
3282 /* Fallback: pre-init for FN+F3,F4,F12 */
3283 hotkey_all_mask = 0x080cU;
3285 tp_features.hotkey_mask = 1;
3290 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3291 "hotkey masks are %s\n",
3292 str_supported(tp_features.hotkey_mask));
3294 /* Init hotkey_all_mask if not initialized yet */
3295 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3296 (quirks & TPACPI_HK_Q_INIMASK))
3297 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3299 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3300 if (tp_features.hotkey_mask) {
3301 /* hotkey_source_mask *must* be zero for
3302 * the first hotkey_mask_get to return hotkey_orig_mask */
3303 res = hotkey_mask_get();
3307 hotkey_orig_mask = hotkey_acpi_mask;
3309 hotkey_orig_mask = hotkey_all_mask;
3310 hotkey_acpi_mask = hotkey_all_mask;
3313 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3315 tp_features.hotkey_wlsw = 1;
3316 radiosw_state = !!tpacpi_wlsw_emulstate;
3317 pr_info("radio switch emulation enabled\n");
3320 /* Not all thinkpads have a hardware radio switch */
3321 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3322 tp_features.hotkey_wlsw = 1;
3323 radiosw_state = !!status;
3324 pr_info("radio switch found; radios are %s\n",
3325 enabled(status, 0));
3327 if (tp_features.hotkey_wlsw)
3328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_radio_sw.attr);
3331 /* For X41t, X60t, X61t Tablets... */
3332 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3333 tp_features.hotkey_tablet = 1;
3334 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3335 pr_info("possible tablet mode switch found; "
3336 "ThinkPad in %s mode\n",
3337 (tabletsw_state) ? "tablet" : "laptop");
3338 res = add_to_attr_set(hotkey_dev_attributes,
3339 &dev_attr_hotkey_tablet_mode.attr);
3343 res = register_attr_set_with_sysfs(
3344 hotkey_dev_attributes,
3345 &tpacpi_pdev->dev.kobj);
3349 /* Set up key map */
3350 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3352 if (!hotkey_keycode_map) {
3353 pr_err("failed to allocate memory for key map\n");
3358 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3359 ARRAY_SIZE(tpacpi_keymap_qtable));
3360 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3362 "using keymap number %lu\n", keymap_id);
3364 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3365 TPACPI_HOTKEY_MAP_SIZE);
3367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
3376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
3381 if (tp_features.hotkey_wlsw) {
3382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
3386 if (tp_features.hotkey_tablet) {
3387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
3392 /* Do not issue duplicate brightness change events to
3393 * userspace. tpacpi_detect_brightness_capabilities() must have
3394 * been called before this point */
3395 if (acpi_video_backlight_support()) {
3396 pr_info("This ThinkPad has standard ACPI backlight "
3397 "brightness control, supported by the ACPI "
3399 pr_notice("Disabling thinkpad-acpi brightness events "
3402 /* Disable brightness up/down on Lenovo thinkpads when
3403 * ACPI is handling them, otherwise it is plain impossible
3404 * for userspace to do something even remotely sane */
3405 hotkey_reserved_mask |=
3406 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3407 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3408 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3409 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3412 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3413 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3415 & ~hotkey_reserved_mask;
3417 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "hotkey source mask 0x%08x, polling freq %u\n",
3419 hotkey_source_mask, hotkey_poll_freq);
3422 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "enabling firmware HKEY event interface...\n");
3424 res = hotkey_status_set(true);
3429 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3430 | hotkey_driver_mask)
3431 & ~hotkey_source_mask);
3432 if (res < 0 && res != -ENXIO) {
3436 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3437 & ~hotkey_reserved_mask;
3438 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3439 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3440 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3442 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "legacy ibm/hotkey event reporting over procfs %s\n",
3444 (hotkey_report_mode < 2) ?
3445 "enabled" : "disabled");
3447 tpacpi_inputdev->open = &hotkey_inputdev_open;
3448 tpacpi_inputdev->close = &hotkey_inputdev_close;
3450 hotkey_poll_setup_safe(true);
3455 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3456 hotkey_dev_attributes = NULL;
3458 return (res < 0)? res : 1;
3461 static bool hotkey_notify_hotkey(const u32 hkey,
3463 bool *ignore_acpi_ev)
3465 /* 0x1000-0x1FFF: key presses */
3466 unsigned int scancode = hkey & 0xfff;
3467 *send_acpi_ev = true;
3468 *ignore_acpi_ev = false;
3470 /* HKEY event 0x1001 is scancode 0x00 */
3471 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3473 if (!(hotkey_source_mask & (1 << scancode))) {
3474 tpacpi_input_send_key_masked(scancode);
3475 *send_acpi_ev = false;
3477 *ignore_acpi_ev = true;
3484 static bool hotkey_notify_wakeup(const u32 hkey,
3486 bool *ignore_acpi_ev)
3488 /* 0x2000-0x2FFF: Wakeup reason */
3489 *send_acpi_ev = true;
3490 *ignore_acpi_ev = false;
3493 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3494 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3495 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3496 *ignore_acpi_ev = true;
3499 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3500 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3501 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3502 *ignore_acpi_ev = true;
3505 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3506 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3507 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3508 /* how to auto-heal: */
3509 /* 2313: woke up from S3, go to S4/S5 */
3510 /* 2413: woke up from S4, go to S5 */
3517 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3518 pr_info("woke up due to a hot-unplug request...\n");
3519 hotkey_wakeup_reason_notify_change();
3524 static bool hotkey_notify_dockevent(const u32 hkey,
3526 bool *ignore_acpi_ev)
3528 /* 0x4000-0x4FFF: dock-related events */
3529 *send_acpi_ev = true;
3530 *ignore_acpi_ev = false;
3533 case TP_HKEY_EV_UNDOCK_ACK:
3534 /* ACPI undock operation completed after wakeup */
3535 hotkey_autosleep_ack = 1;
3536 pr_info("undocked\n");
3537 hotkey_wakeup_hotunplug_complete_notify_change();
3540 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3541 pr_info("docked into hotplug port replicator\n");
3543 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3544 pr_info("undocked from hotplug port replicator\n");
3552 static bool hotkey_notify_usrevent(const u32 hkey,
3554 bool *ignore_acpi_ev)
3556 /* 0x5000-0x5FFF: human interface helpers */
3557 *send_acpi_ev = true;
3558 *ignore_acpi_ev = false;
3561 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3562 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3565 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3566 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3567 tpacpi_input_send_tabletsw();
3568 hotkey_tablet_mode_notify_change();
3569 *send_acpi_ev = false;
3572 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3573 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3574 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3575 /* do not propagate these events */
3576 *ignore_acpi_ev = true;
3584 static void thermal_dump_all_sensors(void);
3586 static bool hotkey_notify_6xxx(const u32 hkey,
3588 bool *ignore_acpi_ev)
3592 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3593 *send_acpi_ev = true;
3594 *ignore_acpi_ev = false;
3597 case TP_HKEY_EV_THM_TABLE_CHANGED:
3598 pr_info("EC reports that Thermal Table has changed\n");
3599 /* recommended action: do nothing, we don't have
3600 * Lenovo ATM information */
3602 case TP_HKEY_EV_ALARM_BAT_HOT:
3603 pr_crit("THERMAL ALARM: battery is too hot!\n");
3604 /* recommended action: warn user through gui */
3606 case TP_HKEY_EV_ALARM_BAT_XHOT:
3607 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3608 /* recommended action: immediate sleep/hibernate */
3610 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3611 pr_crit("THERMAL ALARM: "
3612 "a sensor reports something is too hot!\n");
3613 /* recommended action: warn user through gui, that */
3614 /* some internal component is too hot */
3616 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3617 pr_alert("THERMAL EMERGENCY: "
3618 "a sensor reports something is extremely hot!\n");
3619 /* recommended action: immediate sleep/hibernate */
3621 case TP_HKEY_EV_AC_CHANGED:
3622 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3623 * AC status changed; can be triggered by plugging or
3624 * unplugging AC adapter, docking or undocking. */
3628 case TP_HKEY_EV_KEY_NUMLOCK:
3629 case TP_HKEY_EV_KEY_FN:
3630 /* key press events, we just ignore them as long as the EC
3631 * is still reporting them in the normal keyboard stream */
3632 *send_acpi_ev = false;
3633 *ignore_acpi_ev = true;
3637 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3641 thermal_dump_all_sensors();
3646 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3650 bool ignore_acpi_ev;
3653 if (event != 0x80) {
3654 pr_err("unknown HKEY notification event %d\n", event);
3655 /* forward it to userspace, maybe it knows how to handle it */
3656 acpi_bus_generate_netlink_event(
3657 ibm->acpi->device->pnp.device_class,
3658 dev_name(&ibm->acpi->device->dev),
3664 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3665 pr_err("failed to retrieve HKEY event\n");
3674 send_acpi_ev = true;
3675 ignore_acpi_ev = false;
3677 switch (hkey >> 12) {
3679 /* 0x1000-0x1FFF: key presses */
3680 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3684 /* 0x2000-0x2FFF: Wakeup reason */
3685 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3689 /* 0x3000-0x3FFF: bay-related wakeups */
3691 case TP_HKEY_EV_BAYEJ_ACK:
3692 hotkey_autosleep_ack = 1;
3693 pr_info("bay ejected\n");
3694 hotkey_wakeup_hotunplug_complete_notify_change();
3697 case TP_HKEY_EV_OPTDRV_EJ:
3698 /* FIXME: kick libata if SATA link offline */
3706 /* 0x4000-0x4FFF: dock-related events */
3707 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3711 /* 0x5000-0x5FFF: human interface helpers */
3712 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3716 /* 0x6000-0x6FFF: thermal alarms/notices and
3717 * keyboard events */
3718 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3722 /* 0x7000-0x7FFF: misc */
3723 if (tp_features.hotkey_wlsw &&
3724 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3725 tpacpi_send_radiosw_update();
3730 /* fallthrough to default */
3735 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3736 pr_notice("please report the conditions when this "
3737 "event happened to %s\n", TPACPI_MAIL);
3741 if (!ignore_acpi_ev &&
3742 (send_acpi_ev || hotkey_report_mode < 2)) {
3743 acpi_bus_generate_proc_event(ibm->acpi->device,
3747 /* netlink events */
3748 if (!ignore_acpi_ev && send_acpi_ev) {
3749 acpi_bus_generate_netlink_event(
3750 ibm->acpi->device->pnp.device_class,
3751 dev_name(&ibm->acpi->device->dev),
3757 static void hotkey_suspend(void)
3759 /* Do these on suspend, we get the events on early resume! */
3760 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3761 hotkey_autosleep_ack = 0;
3764 static void hotkey_resume(void)
3766 tpacpi_disable_brightness_delay();
3768 if (hotkey_status_set(true) < 0 ||
3769 hotkey_mask_set(hotkey_acpi_mask) < 0)
3770 pr_err("error while attempting to reset the event "
3771 "firmware interface\n");
3773 tpacpi_send_radiosw_update();
3774 hotkey_tablet_mode_notify_change();
3775 hotkey_wakeup_reason_notify_change();
3776 hotkey_wakeup_hotunplug_complete_notify_change();
3777 hotkey_poll_setup_safe(false);
3780 /* procfs -------------------------------------------------------------- */
3781 static int hotkey_read(struct seq_file *m)
3785 if (!tp_features.hotkey) {
3786 seq_printf(m, "status:\t\tnot supported\n");
3790 if (mutex_lock_killable(&hotkey_mutex))
3791 return -ERESTARTSYS;
3792 res = hotkey_status_get(&status);
3794 res = hotkey_mask_get();
3795 mutex_unlock(&hotkey_mutex);
3799 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3800 if (hotkey_all_mask) {
3801 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3802 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3804 seq_printf(m, "mask:\t\tnot supported\n");
3805 seq_printf(m, "commands:\tenable, disable, reset\n");
3811 static void hotkey_enabledisable_warn(bool enable)
3813 tpacpi_log_usertask("procfs hotkey enable/disable");
3814 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3815 pr_fmt("hotkey enable/disable functionality has been "
3816 "removed from the driver. "
3817 "Hotkeys are always enabled.\n")))
3818 pr_err("Please remove the hotkey=enable module "
3819 "parameter, it is deprecated. "
3820 "Hotkeys are always enabled.\n");
3823 static int hotkey_write(char *buf)
3829 if (!tp_features.hotkey)
3832 if (mutex_lock_killable(&hotkey_mutex))
3833 return -ERESTARTSYS;
3835 mask = hotkey_user_mask;
3838 while ((cmd = next_cmd(&buf))) {
3839 if (strlencmp(cmd, "enable") == 0) {
3840 hotkey_enabledisable_warn(1);
3841 } else if (strlencmp(cmd, "disable") == 0) {
3842 hotkey_enabledisable_warn(0);
3844 } else if (strlencmp(cmd, "reset") == 0) {
3845 mask = (hotkey_all_mask | hotkey_source_mask)
3846 & ~hotkey_reserved_mask;
3847 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3849 } else if (sscanf(cmd, "%x", &mask) == 1) {
3858 tpacpi_disclose_usertask("procfs hotkey",
3859 "set mask to 0x%08x\n", mask);
3860 res = hotkey_user_mask_set(mask);
3864 mutex_unlock(&hotkey_mutex);
3868 static const struct acpi_device_id ibm_htk_device_ids[] = {
3869 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3870 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3874 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3875 .hid = ibm_htk_device_ids,
3876 .notify = hotkey_notify,
3877 .handle = &hkey_handle,
3878 .type = ACPI_DEVICE_NOTIFY,
3881 static struct ibm_struct hotkey_driver_data = {
3883 .read = hotkey_read,
3884 .write = hotkey_write,
3885 .exit = hotkey_exit,
3886 .resume = hotkey_resume,
3887 .suspend = hotkey_suspend,
3888 .acpi = &ibm_hotkey_acpidriver,
3891 /*************************************************************************
3892 * Bluetooth subdriver
3896 /* ACPI GBDC/SBDC bits */
3897 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3898 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3899 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3900 0 = disable, 1 = enable */
3904 /* ACPI \BLTH commands */
3905 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3906 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3907 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3908 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3909 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3912 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3914 static int bluetooth_get_status(void)
3918 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3919 if (dbg_bluetoothemul)
3920 return (tpacpi_bluetooth_emulstate) ?
3921 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3924 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3927 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3928 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3931 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3935 vdbg_printk(TPACPI_DBG_RFKILL,
3936 "will attempt to %s bluetooth\n",
3937 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3939 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3940 if (dbg_bluetoothemul) {
3941 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3946 if (state == TPACPI_RFK_RADIO_ON)
3947 status = TP_ACPI_BLUETOOTH_RADIOSSW
3948 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3952 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3958 /* sysfs bluetooth enable ---------------------------------------------- */
3959 static ssize_t bluetooth_enable_show(struct device *dev,
3960 struct device_attribute *attr,
3963 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3967 static ssize_t bluetooth_enable_store(struct device *dev,
3968 struct device_attribute *attr,
3969 const char *buf, size_t count)
3971 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3975 static struct device_attribute dev_attr_bluetooth_enable =
3976 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3977 bluetooth_enable_show, bluetooth_enable_store);
3979 /* --------------------------------------------------------------------- */
3981 static struct attribute *bluetooth_attributes[] = {
3982 &dev_attr_bluetooth_enable.attr,
3986 static const struct attribute_group bluetooth_attr_group = {
3987 .attrs = bluetooth_attributes,
3990 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3991 .get_status = bluetooth_get_status,
3992 .set_status = bluetooth_set_status,
3995 static void bluetooth_shutdown(void)
3997 /* Order firmware to save current state to NVRAM */
3998 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3999 TP_ACPI_BLTH_SAVE_STATE))
4000 pr_notice("failed to save bluetooth state to NVRAM\n");
4002 vdbg_printk(TPACPI_DBG_RFKILL,
4003 "bluetooth state saved to NVRAM\n");
4006 static void bluetooth_exit(void)
4008 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4009 &bluetooth_attr_group);
4011 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4013 bluetooth_shutdown();
4016 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4021 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4022 "initializing bluetooth subdriver\n");
4024 TPACPI_ACPIHANDLE_INIT(hkey);
4026 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4027 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4028 tp_features.bluetooth = hkey_handle &&
4029 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4031 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4032 "bluetooth is %s, status 0x%02x\n",
4033 str_supported(tp_features.bluetooth),
4036 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4037 if (dbg_bluetoothemul) {
4038 tp_features.bluetooth = 1;
4039 pr_info("bluetooth switch emulation enabled\n");
4042 if (tp_features.bluetooth &&
4043 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4044 /* no bluetooth hardware present in system */
4045 tp_features.bluetooth = 0;
4046 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4047 "bluetooth hardware not installed\n");
4050 if (!tp_features.bluetooth)
4053 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4054 &bluetooth_tprfk_ops,
4055 RFKILL_TYPE_BLUETOOTH,
4056 TPACPI_RFK_BLUETOOTH_SW_NAME,
4061 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4062 &bluetooth_attr_group);
4064 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4071 /* procfs -------------------------------------------------------------- */
4072 static int bluetooth_read(struct seq_file *m)
4074 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4077 static int bluetooth_write(char *buf)
4079 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4082 static struct ibm_struct bluetooth_driver_data = {
4083 .name = "bluetooth",
4084 .read = bluetooth_read,
4085 .write = bluetooth_write,
4086 .exit = bluetooth_exit,
4087 .shutdown = bluetooth_shutdown,
4090 /*************************************************************************
4095 /* ACPI GWAN/SWAN bits */
4096 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4097 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4098 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4099 0 = disable, 1 = enable */
4102 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4104 static int wan_get_status(void)
4108 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4110 return (tpacpi_wwan_emulstate) ?
4111 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4114 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4117 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4118 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4121 static int wan_set_status(enum tpacpi_rfkill_state state)
4125 vdbg_printk(TPACPI_DBG_RFKILL,
4126 "will attempt to %s wwan\n",
4127 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4129 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4131 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4136 if (state == TPACPI_RFK_RADIO_ON)
4137 status = TP_ACPI_WANCARD_RADIOSSW
4138 | TP_ACPI_WANCARD_RESUMECTRL;
4142 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4148 /* sysfs wan enable ---------------------------------------------------- */
4149 static ssize_t wan_enable_show(struct device *dev,
4150 struct device_attribute *attr,
4153 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4157 static ssize_t wan_enable_store(struct device *dev,
4158 struct device_attribute *attr,
4159 const char *buf, size_t count)
4161 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4165 static struct device_attribute dev_attr_wan_enable =
4166 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4167 wan_enable_show, wan_enable_store);
4169 /* --------------------------------------------------------------------- */
4171 static struct attribute *wan_attributes[] = {
4172 &dev_attr_wan_enable.attr,
4176 static const struct attribute_group wan_attr_group = {
4177 .attrs = wan_attributes,
4180 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4181 .get_status = wan_get_status,
4182 .set_status = wan_set_status,
4185 static void wan_shutdown(void)
4187 /* Order firmware to save current state to NVRAM */
4188 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4189 TP_ACPI_WGSV_SAVE_STATE))
4190 pr_notice("failed to save WWAN state to NVRAM\n");
4192 vdbg_printk(TPACPI_DBG_RFKILL,
4193 "WWAN state saved to NVRAM\n");
4196 static void wan_exit(void)
4198 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4201 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4206 static int __init wan_init(struct ibm_init_struct *iibm)
4211 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4212 "initializing wan subdriver\n");
4214 TPACPI_ACPIHANDLE_INIT(hkey);
4216 tp_features.wan = hkey_handle &&
4217 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4219 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4220 "wan is %s, status 0x%02x\n",
4221 str_supported(tp_features.wan),
4224 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4226 tp_features.wan = 1;
4227 pr_info("wwan switch emulation enabled\n");
4230 if (tp_features.wan &&
4231 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4232 /* no wan hardware present in system */
4233 tp_features.wan = 0;
4234 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4235 "wan hardware not installed\n");
4238 if (!tp_features.wan)
4241 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4244 TPACPI_RFK_WWAN_SW_NAME,
4249 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4253 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4260 /* procfs -------------------------------------------------------------- */
4261 static int wan_read(struct seq_file *m)
4263 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4266 static int wan_write(char *buf)
4268 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4271 static struct ibm_struct wan_driver_data = {
4276 .shutdown = wan_shutdown,
4279 /*************************************************************************
4284 /* ACPI GUWB/SUWB bits */
4285 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4286 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4289 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4291 static int uwb_get_status(void)
4295 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4297 return (tpacpi_uwb_emulstate) ?
4298 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4301 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4304 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4305 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4308 static int uwb_set_status(enum tpacpi_rfkill_state state)
4312 vdbg_printk(TPACPI_DBG_RFKILL,
4313 "will attempt to %s UWB\n",
4314 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4316 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4318 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4323 if (state == TPACPI_RFK_RADIO_ON)
4324 status = TP_ACPI_UWB_RADIOSSW;
4328 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4334 /* --------------------------------------------------------------------- */
4336 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4337 .get_status = uwb_get_status,
4338 .set_status = uwb_set_status,
4341 static void uwb_exit(void)
4343 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4346 static int __init uwb_init(struct ibm_init_struct *iibm)
4351 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4352 "initializing uwb subdriver\n");
4354 TPACPI_ACPIHANDLE_INIT(hkey);
4356 tp_features.uwb = hkey_handle &&
4357 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4359 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4360 "uwb is %s, status 0x%02x\n",
4361 str_supported(tp_features.uwb),
4364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4366 tp_features.uwb = 1;
4367 pr_info("uwb switch emulation enabled\n");
4370 if (tp_features.uwb &&
4371 !(status & TP_ACPI_UWB_HWPRESENT)) {
4372 /* no uwb hardware present in system */
4373 tp_features.uwb = 0;
4374 dbg_printk(TPACPI_DBG_INIT,
4375 "uwb hardware not installed\n");
4378 if (!tp_features.uwb)
4381 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4384 TPACPI_RFK_UWB_SW_NAME,
4389 static struct ibm_struct uwb_driver_data = {
4392 .flags.experimental = 1,
4395 /*************************************************************************
4399 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4401 enum video_access_mode {
4402 TPACPI_VIDEO_NONE = 0,
4403 TPACPI_VIDEO_570, /* 570 */
4404 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4405 TPACPI_VIDEO_NEW, /* all others */
4408 enum { /* video status flags, based on VIDEO_570 */
4409 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4410 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4411 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4414 enum { /* TPACPI_VIDEO_570 constants */
4415 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4416 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4417 * video_status_flags */
4418 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4419 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4422 static enum video_access_mode video_supported;
4423 static int video_orig_autosw;
4425 static int video_autosw_get(void);
4426 static int video_autosw_set(int enable);
4428 TPACPI_HANDLE(vid, root,
4429 "\\_SB.PCI.AGP.VGA", /* 570 */
4430 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4431 "\\_SB.PCI0.VID0", /* 770e */
4432 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4433 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4434 "\\_SB.PCI0.AGP.VID", /* all others */
4437 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4439 static int __init video_init(struct ibm_init_struct *iibm)
4443 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4445 TPACPI_ACPIHANDLE_INIT(vid);
4446 if (tpacpi_is_ibm())
4447 TPACPI_ACPIHANDLE_INIT(vid2);
4449 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4450 /* G41, assume IVGA doesn't change */
4451 vid_handle = vid2_handle;
4454 /* video switching not supported on R30, R31 */
4455 video_supported = TPACPI_VIDEO_NONE;
4456 else if (tpacpi_is_ibm() &&
4457 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4459 video_supported = TPACPI_VIDEO_570;
4460 else if (tpacpi_is_ibm() &&
4461 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4462 /* 600e/x, 770e, 770x */
4463 video_supported = TPACPI_VIDEO_770;
4466 video_supported = TPACPI_VIDEO_NEW;
4468 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4469 str_supported(video_supported != TPACPI_VIDEO_NONE),
4472 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4475 static void video_exit(void)
4477 dbg_printk(TPACPI_DBG_EXIT,
4478 "restoring original video autoswitch mode\n");
4479 if (video_autosw_set(video_orig_autosw))
4480 pr_err("error while trying to restore original "
4481 "video autoswitch mode\n");
4484 static int video_outputsw_get(void)
4489 switch (video_supported) {
4490 case TPACPI_VIDEO_570:
4491 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4492 TP_ACPI_VIDEO_570_PHSCMD))
4494 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4496 case TPACPI_VIDEO_770:
4497 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4500 status |= TP_ACPI_VIDEO_S_LCD;
4501 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4504 status |= TP_ACPI_VIDEO_S_CRT;
4506 case TPACPI_VIDEO_NEW:
4507 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4508 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4511 status |= TP_ACPI_VIDEO_S_CRT;
4513 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4514 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4517 status |= TP_ACPI_VIDEO_S_LCD;
4518 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4521 status |= TP_ACPI_VIDEO_S_DVI;
4530 static int video_outputsw_set(int status)
4535 switch (video_supported) {
4536 case TPACPI_VIDEO_570:
4537 res = acpi_evalf(NULL, NULL,
4538 "\\_SB.PHS2", "vdd",
4539 TP_ACPI_VIDEO_570_PHS2CMD,
4540 status | TP_ACPI_VIDEO_570_PHS2SET);
4542 case TPACPI_VIDEO_770:
4543 autosw = video_autosw_get();
4547 res = video_autosw_set(1);
4550 res = acpi_evalf(vid_handle, NULL,
4551 "ASWT", "vdd", status * 0x100, 0);
4552 if (!autosw && video_autosw_set(autosw)) {
4553 pr_err("video auto-switch left enabled due to error\n");
4557 case TPACPI_VIDEO_NEW:
4558 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4559 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4565 return (res)? 0 : -EIO;
4568 static int video_autosw_get(void)
4572 switch (video_supported) {
4573 case TPACPI_VIDEO_570:
4574 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4577 case TPACPI_VIDEO_770:
4578 case TPACPI_VIDEO_NEW:
4579 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4589 static int video_autosw_set(int enable)
4591 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4596 static int video_outputsw_cycle(void)
4598 int autosw = video_autosw_get();
4604 switch (video_supported) {
4605 case TPACPI_VIDEO_570:
4606 res = video_autosw_set(1);
4609 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4611 case TPACPI_VIDEO_770:
4612 case TPACPI_VIDEO_NEW:
4613 res = video_autosw_set(1);
4616 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4621 if (!autosw && video_autosw_set(autosw)) {
4622 pr_err("video auto-switch left enabled due to error\n");
4626 return (res)? 0 : -EIO;
4629 static int video_expand_toggle(void)
4631 switch (video_supported) {
4632 case TPACPI_VIDEO_570:
4633 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4635 case TPACPI_VIDEO_770:
4636 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4638 case TPACPI_VIDEO_NEW:
4639 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4647 static int video_read(struct seq_file *m)
4651 if (video_supported == TPACPI_VIDEO_NONE) {
4652 seq_printf(m, "status:\t\tnot supported\n");
4656 /* Even reads can crash X.org, so... */
4657 if (!capable(CAP_SYS_ADMIN))
4660 status = video_outputsw_get();
4664 autosw = video_autosw_get();
4668 seq_printf(m, "status:\t\tsupported\n");
4669 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4670 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4671 if (video_supported == TPACPI_VIDEO_NEW)
4672 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4673 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4674 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4675 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4676 if (video_supported == TPACPI_VIDEO_NEW)
4677 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4678 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4679 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4684 static int video_write(char *buf)
4687 int enable, disable, status;
4690 if (video_supported == TPACPI_VIDEO_NONE)
4693 /* Even reads can crash X.org, let alone writes... */
4694 if (!capable(CAP_SYS_ADMIN))
4700 while ((cmd = next_cmd(&buf))) {
4701 if (strlencmp(cmd, "lcd_enable") == 0) {
4702 enable |= TP_ACPI_VIDEO_S_LCD;
4703 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4704 disable |= TP_ACPI_VIDEO_S_LCD;
4705 } else if (strlencmp(cmd, "crt_enable") == 0) {
4706 enable |= TP_ACPI_VIDEO_S_CRT;
4707 } else if (strlencmp(cmd, "crt_disable") == 0) {
4708 disable |= TP_ACPI_VIDEO_S_CRT;
4709 } else if (video_supported == TPACPI_VIDEO_NEW &&
4710 strlencmp(cmd, "dvi_enable") == 0) {
4711 enable |= TP_ACPI_VIDEO_S_DVI;
4712 } else if (video_supported == TPACPI_VIDEO_NEW &&
4713 strlencmp(cmd, "dvi_disable") == 0) {
4714 disable |= TP_ACPI_VIDEO_S_DVI;
4715 } else if (strlencmp(cmd, "auto_enable") == 0) {
4716 res = video_autosw_set(1);
4719 } else if (strlencmp(cmd, "auto_disable") == 0) {
4720 res = video_autosw_set(0);
4723 } else if (strlencmp(cmd, "video_switch") == 0) {
4724 res = video_outputsw_cycle();
4727 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4728 res = video_expand_toggle();
4735 if (enable || disable) {
4736 status = video_outputsw_get();
4739 res = video_outputsw_set((status & ~disable) | enable);
4747 static struct ibm_struct video_driver_data = {
4750 .write = video_write,
4754 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4756 /*************************************************************************
4757 * Light (thinklight) subdriver
4760 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4761 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4763 static int light_get_status(void)
4767 if (tp_features.light_status) {
4768 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4776 static int light_set_status(int status)
4780 if (tp_features.light) {
4782 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4784 TP_CMOS_THINKLIGHT_ON :
4785 TP_CMOS_THINKLIGHT_OFF);
4787 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4790 return (rc)? 0 : -EIO;
4796 static void light_set_status_worker(struct work_struct *work)
4798 struct tpacpi_led_classdev *data =
4799 container_of(work, struct tpacpi_led_classdev, work);
4801 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4802 light_set_status((data->new_state != TPACPI_LED_OFF));
4805 static void light_sysfs_set(struct led_classdev *led_cdev,
4806 enum led_brightness brightness)
4808 struct tpacpi_led_classdev *data =
4809 container_of(led_cdev,
4810 struct tpacpi_led_classdev,
4812 data->new_state = (brightness != LED_OFF) ?
4813 TPACPI_LED_ON : TPACPI_LED_OFF;
4814 queue_work(tpacpi_wq, &data->work);
4817 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4819 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4822 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4824 .name = "tpacpi::thinklight",
4825 .brightness_set = &light_sysfs_set,
4826 .brightness_get = &light_sysfs_get,
4830 static int __init light_init(struct ibm_init_struct *iibm)
4834 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4836 if (tpacpi_is_ibm()) {
4837 TPACPI_ACPIHANDLE_INIT(ledb);
4838 TPACPI_ACPIHANDLE_INIT(lght);
4840 TPACPI_ACPIHANDLE_INIT(cmos);
4841 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4843 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4844 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4846 if (tp_features.light)
4847 /* light status not supported on
4848 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4849 tp_features.light_status =
4850 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4852 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4853 str_supported(tp_features.light),
4854 str_supported(tp_features.light_status));
4856 if (!tp_features.light)
4859 rc = led_classdev_register(&tpacpi_pdev->dev,
4860 &tpacpi_led_thinklight.led_classdev);
4863 tp_features.light = 0;
4864 tp_features.light_status = 0;
4872 static void light_exit(void)
4874 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4875 flush_workqueue(tpacpi_wq);
4878 static int light_read(struct seq_file *m)
4882 if (!tp_features.light) {
4883 seq_printf(m, "status:\t\tnot supported\n");
4884 } else if (!tp_features.light_status) {
4885 seq_printf(m, "status:\t\tunknown\n");
4886 seq_printf(m, "commands:\ton, off\n");
4888 status = light_get_status();
4891 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4892 seq_printf(m, "commands:\ton, off\n");
4898 static int light_write(char *buf)
4903 if (!tp_features.light)
4906 while ((cmd = next_cmd(&buf))) {
4907 if (strlencmp(cmd, "on") == 0) {
4909 } else if (strlencmp(cmd, "off") == 0) {
4915 return light_set_status(newstatus);
4918 static struct ibm_struct light_driver_data = {
4921 .write = light_write,
4925 /*************************************************************************
4929 /* sysfs cmos_command -------------------------------------------------- */
4930 static ssize_t cmos_command_store(struct device *dev,
4931 struct device_attribute *attr,
4932 const char *buf, size_t count)
4934 unsigned long cmos_cmd;
4937 if (parse_strtoul(buf, 21, &cmos_cmd))
4940 res = issue_thinkpad_cmos_command(cmos_cmd);
4941 return (res)? res : count;
4944 static struct device_attribute dev_attr_cmos_command =
4945 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4947 /* --------------------------------------------------------------------- */
4949 static int __init cmos_init(struct ibm_init_struct *iibm)
4953 vdbg_printk(TPACPI_DBG_INIT,
4954 "initializing cmos commands subdriver\n");
4956 TPACPI_ACPIHANDLE_INIT(cmos);
4958 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4959 str_supported(cmos_handle != NULL));
4961 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4965 return (cmos_handle)? 0 : 1;
4968 static void cmos_exit(void)
4970 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4973 static int cmos_read(struct seq_file *m)
4975 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4976 R30, R31, T20-22, X20-21 */
4978 seq_printf(m, "status:\t\tnot supported\n");
4980 seq_printf(m, "status:\t\tsupported\n");
4981 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4987 static int cmos_write(char *buf)
4992 while ((cmd = next_cmd(&buf))) {
4993 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4994 cmos_cmd >= 0 && cmos_cmd <= 21) {
4999 res = issue_thinkpad_cmos_command(cmos_cmd);
5007 static struct ibm_struct cmos_driver_data = {
5010 .write = cmos_write,
5014 /*************************************************************************
5018 enum led_access_mode {
5019 TPACPI_LED_NONE = 0,
5020 TPACPI_LED_570, /* 570 */
5021 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5022 TPACPI_LED_NEW, /* all others */
5025 enum { /* For TPACPI_LED_OLD */
5026 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5027 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5028 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5031 static enum led_access_mode led_supported;
5033 static acpi_handle led_handle;
5035 #define TPACPI_LED_NUMLEDS 16
5036 static struct tpacpi_led_classdev *tpacpi_leds;
5037 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5038 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5039 /* there's a limit of 19 chars + NULL before 2.6.26 */
5041 "tpacpi:orange:batt",
5042 "tpacpi:green:batt",
5043 "tpacpi::dock_active",
5044 "tpacpi::bay_active",
5045 "tpacpi::dock_batt",
5046 "tpacpi::unknown_led",
5048 "tpacpi::dock_status1",
5049 "tpacpi::dock_status2",
5050 "tpacpi::unknown_led2",
5051 "tpacpi::unknown_led3",
5052 "tpacpi::thinkvantage",
5054 #define TPACPI_SAFE_LEDS 0x1081U
5056 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5058 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5061 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5065 static int led_get_status(const unsigned int led)
5068 enum led_status_t led_s;
5070 switch (led_supported) {
5071 case TPACPI_LED_570:
5072 if (!acpi_evalf(ec_handle,
5073 &status, "GLED", "dd", 1 << led))
5075 led_s = (status == 0)?
5080 tpacpi_led_state_cache[led] = led_s;
5089 static int led_set_status(const unsigned int led,
5090 const enum led_status_t ledstatus)
5092 /* off, on, blink. Index is led_status_t */
5093 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5094 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5098 switch (led_supported) {
5099 case TPACPI_LED_570:
5101 if (unlikely(led > 7))
5103 if (unlikely(tpacpi_is_led_restricted(led)))
5105 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5106 (1 << led), led_sled_arg1[ledstatus]))
5109 case TPACPI_LED_OLD:
5110 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5111 if (unlikely(led > 7))
5113 if (unlikely(tpacpi_is_led_restricted(led)))
5115 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5117 rc = ec_write(TPACPI_LED_EC_HLBL,
5118 (ledstatus == TPACPI_LED_BLINK) << led);
5120 rc = ec_write(TPACPI_LED_EC_HLCL,
5121 (ledstatus != TPACPI_LED_OFF) << led);
5123 case TPACPI_LED_NEW:
5125 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5127 if (unlikely(tpacpi_is_led_restricted(led)))
5129 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5130 led, led_led_arg1[ledstatus]))
5138 tpacpi_led_state_cache[led] = ledstatus;
5143 static void led_set_status_worker(struct work_struct *work)
5145 struct tpacpi_led_classdev *data =
5146 container_of(work, struct tpacpi_led_classdev, work);
5148 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5149 led_set_status(data->led, data->new_state);
5152 static void led_sysfs_set(struct led_classdev *led_cdev,
5153 enum led_brightness brightness)
5155 struct tpacpi_led_classdev *data = container_of(led_cdev,
5156 struct tpacpi_led_classdev, led_classdev);
5158 if (brightness == LED_OFF)
5159 data->new_state = TPACPI_LED_OFF;
5160 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5161 data->new_state = TPACPI_LED_ON;
5163 data->new_state = TPACPI_LED_BLINK;
5165 queue_work(tpacpi_wq, &data->work);
5168 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5169 unsigned long *delay_on, unsigned long *delay_off)
5171 struct tpacpi_led_classdev *data = container_of(led_cdev,
5172 struct tpacpi_led_classdev, led_classdev);
5174 /* Can we choose the flash rate? */
5175 if (*delay_on == 0 && *delay_off == 0) {
5176 /* yes. set them to the hardware blink rate (1 Hz) */
5177 *delay_on = 500; /* ms */
5178 *delay_off = 500; /* ms */
5179 } else if ((*delay_on != 500) || (*delay_off != 500))
5182 data->new_state = TPACPI_LED_BLINK;
5183 queue_work(tpacpi_wq, &data->work);
5188 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5192 struct tpacpi_led_classdev *data = container_of(led_cdev,
5193 struct tpacpi_led_classdev, led_classdev);
5195 rc = led_get_status(data->led);
5197 if (rc == TPACPI_LED_OFF || rc < 0)
5198 rc = LED_OFF; /* no error handling in led class :( */
5205 static void led_exit(void)
5209 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5210 if (tpacpi_leds[i].led_classdev.name)
5211 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5214 flush_workqueue(tpacpi_wq);
5218 static int __init tpacpi_init_led(unsigned int led)
5222 tpacpi_leds[led].led = led;
5224 /* LEDs with no name don't get registered */
5225 if (!tpacpi_led_names[led])
5228 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5229 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5230 if (led_supported == TPACPI_LED_570)
5231 tpacpi_leds[led].led_classdev.brightness_get =
5234 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5236 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5238 rc = led_classdev_register(&tpacpi_pdev->dev,
5239 &tpacpi_leds[led].led_classdev);
5241 tpacpi_leds[led].led_classdev.name = NULL;
5246 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5247 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5248 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5249 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5251 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5252 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5253 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5254 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5255 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5256 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5257 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5258 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5260 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5261 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5262 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5263 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5264 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5266 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5267 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5268 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5269 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5271 /* (1) - may have excess leds enabled on MSB */
5273 /* Defaults (order matters, keep last, don't reorder!) */
5275 .vendor = PCI_VENDOR_ID_LENOVO,
5276 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5279 { /* IBM ThinkPads with no EC version string */
5280 .vendor = PCI_VENDOR_ID_IBM,
5281 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5284 { /* IBM ThinkPads with EC version string */
5285 .vendor = PCI_VENDOR_ID_IBM,
5286 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5291 #undef TPACPI_LEDQ_IBM
5292 #undef TPACPI_LEDQ_LNV
5294 static enum led_access_mode __init led_init_detect_mode(void)
5298 if (tpacpi_is_ibm()) {
5300 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5301 if (ACPI_SUCCESS(status))
5302 return TPACPI_LED_570;
5304 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5305 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5306 if (ACPI_SUCCESS(status))
5307 return TPACPI_LED_OLD;
5311 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5312 if (ACPI_SUCCESS(status))
5313 return TPACPI_LED_NEW;
5315 /* R30, R31, and unknown firmwares */
5317 return TPACPI_LED_NONE;
5320 static int __init led_init(struct ibm_init_struct *iibm)
5324 unsigned long useful_leds;
5326 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5328 led_supported = led_init_detect_mode();
5330 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5331 str_supported(led_supported), led_supported);
5333 if (led_supported == TPACPI_LED_NONE)
5336 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5339 pr_err("Out of memory for LED data\n");
5343 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5344 ARRAY_SIZE(led_useful_qtable));
5346 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5347 if (!tpacpi_is_led_restricted(i) &&
5348 test_bit(i, &useful_leds)) {
5349 rc = tpacpi_init_led(i);
5357 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5358 pr_notice("warning: userspace override of important "
5359 "firmware LEDs is enabled\n");
5364 #define str_led_status(s) \
5365 ((s) == TPACPI_LED_OFF ? "off" : \
5366 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5368 static int led_read(struct seq_file *m)
5370 if (!led_supported) {
5371 seq_printf(m, "status:\t\tnot supported\n");
5374 seq_printf(m, "status:\t\tsupported\n");
5376 if (led_supported == TPACPI_LED_570) {
5379 for (i = 0; i < 8; i++) {
5380 status = led_get_status(i);
5383 seq_printf(m, "%d:\t\t%s\n",
5384 i, str_led_status(status));
5388 seq_printf(m, "commands:\t"
5389 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5394 static int led_write(char *buf)
5398 enum led_status_t s;
5403 while ((cmd = next_cmd(&buf))) {
5404 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5407 if (strstr(cmd, "off")) {
5409 } else if (strstr(cmd, "on")) {
5411 } else if (strstr(cmd, "blink")) {
5412 s = TPACPI_LED_BLINK;
5417 rc = led_set_status(led, s);
5425 static struct ibm_struct led_driver_data = {
5432 /*************************************************************************
5436 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5438 #define TPACPI_BEEP_Q1 0x0001
5440 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5441 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5442 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5445 static int __init beep_init(struct ibm_init_struct *iibm)
5447 unsigned long quirks;
5449 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5451 TPACPI_ACPIHANDLE_INIT(beep);
5453 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5454 str_supported(beep_handle != NULL));
5456 quirks = tpacpi_check_quirks(beep_quirk_table,
5457 ARRAY_SIZE(beep_quirk_table));
5459 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5461 return (beep_handle)? 0 : 1;
5464 static int beep_read(struct seq_file *m)
5467 seq_printf(m, "status:\t\tnot supported\n");
5469 seq_printf(m, "status:\t\tsupported\n");
5470 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5476 static int beep_write(char *buf)
5484 while ((cmd = next_cmd(&buf))) {
5485 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5486 beep_cmd >= 0 && beep_cmd <= 17) {
5490 if (tp_features.beep_needs_two_args) {
5491 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5495 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5504 static struct ibm_struct beep_driver_data = {
5507 .write = beep_write,
5510 /*************************************************************************
5514 enum thermal_access_mode {
5515 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5516 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5517 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5518 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5519 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5522 enum { /* TPACPI_THERMAL_TPEC_* */
5523 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5524 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5525 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5527 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5531 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5532 struct ibm_thermal_sensors_struct {
5533 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5536 static enum thermal_access_mode thermal_read_mode;
5538 /* idx is zero-based */
5539 static int thermal_get_sensor(int idx, s32 *value)
5545 t = TP_EC_THERMAL_TMP0;
5547 switch (thermal_read_mode) {
5548 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5549 case TPACPI_THERMAL_TPEC_16:
5550 if (idx >= 8 && idx <= 15) {
5551 t = TP_EC_THERMAL_TMP8;
5556 case TPACPI_THERMAL_TPEC_8:
5558 if (!acpi_ec_read(t + idx, &tmp))
5560 *value = tmp * 1000;
5565 case TPACPI_THERMAL_ACPI_UPDT:
5567 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5568 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5570 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5572 *value = (t - 2732) * 100;
5577 case TPACPI_THERMAL_ACPI_TMP07:
5579 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5580 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5582 if (t > 127 || t < -127)
5583 t = TP_EC_THERMAL_TMP_NA;
5589 case TPACPI_THERMAL_NONE:
5597 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5608 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5611 for (i = 0 ; i < n; i++) {
5612 res = thermal_get_sensor(i, &s->temp[i]);
5620 static void thermal_dump_all_sensors(void)
5623 struct ibm_thermal_sensors_struct t;
5625 n = thermal_get_sensors(&t);
5629 pr_notice("temperatures (Celsius):");
5631 for (i = 0; i < n; i++) {
5632 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5633 pr_cont(" %d", (int)(t.temp[i] / 1000));
5641 /* sysfs temp##_input -------------------------------------------------- */
5643 static ssize_t thermal_temp_input_show(struct device *dev,
5644 struct device_attribute *attr,
5647 struct sensor_device_attribute *sensor_attr =
5648 to_sensor_dev_attr(attr);
5649 int idx = sensor_attr->index;
5653 res = thermal_get_sensor(idx, &value);
5656 if (value == TPACPI_THERMAL_SENSOR_NA)
5659 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5662 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5663 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5664 thermal_temp_input_show, NULL, _idxB)
5666 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5667 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5668 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5669 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5670 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5671 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5672 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5673 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5674 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5675 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5676 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5677 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5678 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5679 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5680 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5681 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5682 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5685 #define THERMAL_ATTRS(X) \
5686 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5688 static struct attribute *thermal_temp_input_attr[] = {
5708 static const struct attribute_group thermal_temp_input16_group = {
5709 .attrs = thermal_temp_input_attr
5712 static const struct attribute_group thermal_temp_input8_group = {
5713 .attrs = &thermal_temp_input_attr[8]
5716 #undef THERMAL_SENSOR_ATTR_TEMP
5717 #undef THERMAL_ATTRS
5719 /* --------------------------------------------------------------------- */
5721 static int __init thermal_init(struct ibm_init_struct *iibm)
5728 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5730 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5732 if (thinkpad_id.ec_model) {
5734 * Direct EC access mode: sensors at registers
5735 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5736 * non-implemented, thermal sensors return 0x80 when
5741 for (i = 0; i < 8; i++) {
5742 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5748 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5756 /* This is sheer paranoia, but we handle it anyway */
5758 pr_err("ThinkPad ACPI EC access misbehaving, "
5759 "falling back to ACPI TMPx access "
5761 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5763 pr_err("ThinkPad ACPI EC access misbehaving, "
5764 "disabling thermal sensors access\n");
5765 thermal_read_mode = TPACPI_THERMAL_NONE;
5770 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5772 } else if (acpi_tmp7) {
5773 if (tpacpi_is_ibm() &&
5774 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5775 /* 600e/x, 770e, 770x */
5776 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5778 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5779 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5782 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5783 thermal_read_mode = TPACPI_THERMAL_NONE;
5786 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5787 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5790 switch (thermal_read_mode) {
5791 case TPACPI_THERMAL_TPEC_16:
5792 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5793 &thermal_temp_input16_group);
5797 case TPACPI_THERMAL_TPEC_8:
5798 case TPACPI_THERMAL_ACPI_TMP07:
5799 case TPACPI_THERMAL_ACPI_UPDT:
5800 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5801 &thermal_temp_input8_group);
5805 case TPACPI_THERMAL_NONE:
5813 static void thermal_exit(void)
5815 switch (thermal_read_mode) {
5816 case TPACPI_THERMAL_TPEC_16:
5817 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5818 &thermal_temp_input16_group);
5820 case TPACPI_THERMAL_TPEC_8:
5821 case TPACPI_THERMAL_ACPI_TMP07:
5822 case TPACPI_THERMAL_ACPI_UPDT:
5823 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5824 &thermal_temp_input8_group);
5826 case TPACPI_THERMAL_NONE:
5832 static int thermal_read(struct seq_file *m)
5835 struct ibm_thermal_sensors_struct t;
5837 n = thermal_get_sensors(&t);
5838 if (unlikely(n < 0))
5841 seq_printf(m, "temperatures:\t");
5844 for (i = 0; i < (n - 1); i++)
5845 seq_printf(m, "%d ", t.temp[i] / 1000);
5846 seq_printf(m, "%d\n", t.temp[i] / 1000);
5848 seq_printf(m, "not supported\n");
5853 static struct ibm_struct thermal_driver_data = {
5855 .read = thermal_read,
5856 .exit = thermal_exit,
5859 /*************************************************************************
5860 * Backlight/brightness subdriver
5863 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5866 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5867 * CMOS NVRAM byte 0x5E, bits 0-3.
5869 * EC HBRV (0x31) has the following layout
5870 * Bit 7: unknown function
5871 * Bit 6: unknown function
5872 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5873 * Bit 4: must be set to zero to avoid problems
5874 * Bit 3-0: backlight brightness level
5876 * brightness_get_raw returns status data in the HBRV layout
5878 * WARNING: The X61 has been verified to use HBRV for something else, so
5879 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5880 * testing on the very early *60 Lenovo models...
5884 TP_EC_BACKLIGHT = 0x31,
5886 /* TP_EC_BACKLIGHT bitmasks */
5887 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5888 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5889 TP_EC_BACKLIGHT_MAPSW = 0x20,
5892 enum tpacpi_brightness_access_mode {
5893 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5894 TPACPI_BRGHT_MODE_EC, /* EC control */
5895 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5896 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5897 TPACPI_BRGHT_MODE_MAX
5900 static struct backlight_device *ibm_backlight_device;
5902 static enum tpacpi_brightness_access_mode brightness_mode =
5903 TPACPI_BRGHT_MODE_MAX;
5905 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5907 static struct mutex brightness_mutex;
5909 /* NVRAM brightness access,
5910 * call with brightness_mutex held! */
5911 static unsigned int tpacpi_brightness_nvram_get(void)
5915 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5916 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5917 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5918 lnvram &= bright_maxlvl;
5923 static void tpacpi_brightness_checkpoint_nvram(void)
5928 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5931 vdbg_printk(TPACPI_DBG_BRGHT,
5932 "trying to checkpoint backlight level to NVRAM...\n");
5934 if (mutex_lock_killable(&brightness_mutex) < 0)
5937 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5939 lec &= TP_EC_BACKLIGHT_LVLMSK;
5940 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5942 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5943 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5944 /* NVRAM needs update */
5945 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5946 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5948 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5949 dbg_printk(TPACPI_DBG_BRGHT,
5950 "updated NVRAM backlight level to %u (0x%02x)\n",
5951 (unsigned int) lec, (unsigned int) b_nvram);
5953 vdbg_printk(TPACPI_DBG_BRGHT,
5954 "NVRAM backlight level already is %u (0x%02x)\n",
5955 (unsigned int) lec, (unsigned int) b_nvram);
5958 mutex_unlock(&brightness_mutex);
5962 /* call with brightness_mutex held! */
5963 static int tpacpi_brightness_get_raw(int *status)
5967 switch (brightness_mode) {
5968 case TPACPI_BRGHT_MODE_UCMS_STEP:
5969 *status = tpacpi_brightness_nvram_get();
5971 case TPACPI_BRGHT_MODE_EC:
5972 case TPACPI_BRGHT_MODE_ECNVRAM:
5973 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5982 /* call with brightness_mutex held! */
5983 /* do NOT call with illegal backlight level value */
5984 static int tpacpi_brightness_set_ec(unsigned int value)
5988 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5991 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5992 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5993 (value & TP_EC_BACKLIGHT_LVLMSK))))
5999 /* call with brightness_mutex held! */
6000 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6003 unsigned int current_value, i;
6005 current_value = tpacpi_brightness_nvram_get();
6007 if (value == current_value)
6010 cmos_cmd = (value > current_value) ?
6011 TP_CMOS_BRIGHTNESS_UP :
6012 TP_CMOS_BRIGHTNESS_DOWN;
6013 inc = (value > current_value) ? 1 : -1;
6015 for (i = current_value; i != value; i += inc)
6016 if (issue_thinkpad_cmos_command(cmos_cmd))
6022 /* May return EINTR which can always be mapped to ERESTARTSYS */
6023 static int brightness_set(unsigned int value)
6027 if (value > bright_maxlvl || value < 0)
6030 vdbg_printk(TPACPI_DBG_BRGHT,
6031 "set backlight level to %d\n", value);
6033 res = mutex_lock_killable(&brightness_mutex);
6037 switch (brightness_mode) {
6038 case TPACPI_BRGHT_MODE_EC:
6039 case TPACPI_BRGHT_MODE_ECNVRAM:
6040 res = tpacpi_brightness_set_ec(value);
6042 case TPACPI_BRGHT_MODE_UCMS_STEP:
6043 res = tpacpi_brightness_set_ucmsstep(value);
6049 mutex_unlock(&brightness_mutex);
6053 /* sysfs backlight class ----------------------------------------------- */
6055 static int brightness_update_status(struct backlight_device *bd)
6057 unsigned int level =
6058 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6059 bd->props.power == FB_BLANK_UNBLANK) ?
6060 bd->props.brightness : 0;
6062 dbg_printk(TPACPI_DBG_BRGHT,
6063 "backlight: attempt to set level to %d\n",
6066 /* it is the backlight class's job (caller) to handle
6067 * EINTR and other errors properly */
6068 return brightness_set(level);
6071 static int brightness_get(struct backlight_device *bd)
6075 res = mutex_lock_killable(&brightness_mutex);
6079 res = tpacpi_brightness_get_raw(&status);
6081 mutex_unlock(&brightness_mutex);
6086 return status & TP_EC_BACKLIGHT_LVLMSK;
6089 static void tpacpi_brightness_notify_change(void)
6091 backlight_force_update(ibm_backlight_device,
6092 BACKLIGHT_UPDATE_HOTKEY);
6095 static const struct backlight_ops ibm_backlight_data = {
6096 .get_brightness = brightness_get,
6097 .update_status = brightness_update_status,
6100 /* --------------------------------------------------------------------- */
6103 * Call _BCL method of video device. On some ThinkPads this will
6104 * switch the firmware to the ACPI brightness control mode.
6107 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6109 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6110 union acpi_object *obj;
6113 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6114 obj = (union acpi_object *)buffer.pointer;
6115 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6116 pr_err("Unknown _BCL data, please report this to %s\n",
6120 rc = obj->package.count;
6126 kfree(buffer.pointer);
6132 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6134 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6136 acpi_handle video_device;
6139 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6141 bcl_levels = tpacpi_query_bcl_levels(video_device);
6143 tp_features.bright_acpimode = (bcl_levels > 0);
6145 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6149 * These are only useful for models that have only one possibility
6150 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6153 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6154 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6155 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6157 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6158 /* Models with ATI GPUs known to require ECNVRAM mode */
6159 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6161 /* Models with ATI GPUs that can use ECNVRAM */
6162 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6163 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6164 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6165 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6167 /* Models with Intel Extreme Graphics 2 */
6168 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6169 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6170 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6172 /* Models with Intel GMA900 */
6173 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6174 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6175 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6179 * Returns < 0 for error, otherwise sets tp_features.bright_*
6180 * and bright_maxlvl.
6182 static void __init tpacpi_detect_brightness_capabilities(void)
6186 vdbg_printk(TPACPI_DBG_INIT,
6187 "detecting firmware brightness interface capabilities\n");
6189 /* we could run a quirks check here (same table used by
6190 * brightness_init) if needed */
6193 * We always attempt to detect acpi support, so as to switch
6194 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6195 * going to publish a backlight interface
6197 b = tpacpi_check_std_acpi_brightness_support();
6201 pr_info("detected a 16-level brightness capable ThinkPad\n");
6206 pr_info("detected a 8-level brightness capable ThinkPad\n");
6209 pr_err("Unsupported brightness interface, "
6210 "please contact %s\n", TPACPI_MAIL);
6211 tp_features.bright_unkfw = 1;
6212 bright_maxlvl = b - 1;
6216 static int __init brightness_init(struct ibm_init_struct *iibm)
6218 struct backlight_properties props;
6220 unsigned long quirks;
6222 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6224 mutex_init(&brightness_mutex);
6226 quirks = tpacpi_check_quirks(brightness_quirk_table,
6227 ARRAY_SIZE(brightness_quirk_table));
6229 /* tpacpi_detect_brightness_capabilities() must have run already */
6231 /* if it is unknown, we don't handle it: it wouldn't be safe */
6232 if (tp_features.bright_unkfw)
6235 if (!brightness_enable) {
6236 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6237 "brightness support disabled by "
6238 "module parameter\n");
6242 if (acpi_video_backlight_support()) {
6243 if (brightness_enable > 1) {
6244 pr_info("Standard ACPI backlight interface "
6245 "available, not loading native one\n");
6247 } else if (brightness_enable == 1) {
6248 pr_warn("Cannot enable backlight brightness support, "
6249 "ACPI is already handling it. Refer to the "
6250 "acpi_backlight kernel parameter.\n");
6253 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6254 pr_notice("Standard ACPI backlight interface not "
6255 "available, thinkpad_acpi native "
6256 "brightness control enabled\n");
6260 * Check for module parameter bogosity, note that we
6261 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6262 * able to detect "unspecified"
6264 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6267 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6268 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6269 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6270 if (quirks & TPACPI_BRGHT_Q_EC)
6271 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6273 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6275 dbg_printk(TPACPI_DBG_BRGHT,
6276 "driver auto-selected brightness_mode=%d\n",
6281 if (!tpacpi_is_ibm() &&
6282 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6283 brightness_mode == TPACPI_BRGHT_MODE_EC))
6286 if (tpacpi_brightness_get_raw(&b) < 0)
6289 memset(&props, 0, sizeof(struct backlight_properties));
6290 props.type = BACKLIGHT_PLATFORM;
6291 props.max_brightness = bright_maxlvl;
6292 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6293 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6295 &ibm_backlight_data,
6297 if (IS_ERR(ibm_backlight_device)) {
6298 int rc = PTR_ERR(ibm_backlight_device);
6299 ibm_backlight_device = NULL;
6300 pr_err("Could not register backlight device\n");
6303 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6304 "brightness is supported\n");
6306 if (quirks & TPACPI_BRGHT_Q_ASK) {
6307 pr_notice("brightness: will use unverified default: "
6308 "brightness_mode=%d\n", brightness_mode);
6309 pr_notice("brightness: please report to %s whether it works well "
6310 "or not on your ThinkPad\n", TPACPI_MAIL);
6313 /* Added by mistake in early 2007. Probably useless, but it could
6314 * be working around some unknown firmware problem where the value
6315 * read at startup doesn't match the real hardware state... so leave
6316 * it in place just in case */
6317 backlight_update_status(ibm_backlight_device);
6319 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6320 "brightness: registering brightness hotkeys "
6321 "as change notification\n");
6322 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6323 | TP_ACPI_HKEY_BRGHTUP_MASK
6324 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6328 static void brightness_suspend(void)
6330 tpacpi_brightness_checkpoint_nvram();
6333 static void brightness_shutdown(void)
6335 tpacpi_brightness_checkpoint_nvram();
6338 static void brightness_exit(void)
6340 if (ibm_backlight_device) {
6341 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6342 "calling backlight_device_unregister()\n");
6343 backlight_device_unregister(ibm_backlight_device);
6346 tpacpi_brightness_checkpoint_nvram();
6349 static int brightness_read(struct seq_file *m)
6353 level = brightness_get(NULL);
6355 seq_printf(m, "level:\t\tunreadable\n");
6357 seq_printf(m, "level:\t\t%d\n", level);
6358 seq_printf(m, "commands:\tup, down\n");
6359 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6366 static int brightness_write(char *buf)
6372 level = brightness_get(NULL);
6376 while ((cmd = next_cmd(&buf))) {
6377 if (strlencmp(cmd, "up") == 0) {
6378 if (level < bright_maxlvl)
6380 } else if (strlencmp(cmd, "down") == 0) {
6383 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6384 level >= 0 && level <= bright_maxlvl) {
6390 tpacpi_disclose_usertask("procfs brightness",
6391 "set level to %d\n", level);
6394 * Now we know what the final level should be, so we try to set it.
6395 * Doing it this way makes the syscall restartable in case of EINTR
6397 rc = brightness_set(level);
6398 if (!rc && ibm_backlight_device)
6399 backlight_force_update(ibm_backlight_device,
6400 BACKLIGHT_UPDATE_SYSFS);
6401 return (rc == -EINTR)? -ERESTARTSYS : rc;
6404 static struct ibm_struct brightness_driver_data = {
6405 .name = "brightness",
6406 .read = brightness_read,
6407 .write = brightness_write,
6408 .exit = brightness_exit,
6409 .suspend = brightness_suspend,
6410 .shutdown = brightness_shutdown,
6413 /*************************************************************************
6418 * IBM ThinkPads have a simple volume controller with MUTE gating.
6419 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6421 * Since the *61 series (and probably also the later *60 series), Lenovo
6422 * ThinkPads only implement the MUTE gate.
6425 * Bit 6: MUTE (1 mutes sound)
6427 * Other bits should be zero as far as we know.
6429 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6430 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6431 * such as bit 7 which is used to detect repeated presses of MUTE,
6432 * and we leave them unchanged.
6435 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6437 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6438 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6439 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6441 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6442 static char *alsa_id = "ThinkPadEC";
6443 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
6445 struct tpacpi_alsa_data {
6446 struct snd_card *card;
6447 struct snd_ctl_elem_id *ctl_mute_id;
6448 struct snd_ctl_elem_id *ctl_vol_id;
6451 static struct snd_card *alsa_card;
6456 /* TP_EC_AUDIO bits */
6457 TP_EC_AUDIO_MUTESW = 6,
6459 /* TP_EC_AUDIO bitmasks */
6460 TP_EC_AUDIO_LVL_MSK = 0x0F,
6461 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6463 /* Maximum volume */
6464 TP_EC_VOLUME_MAX = 14,
6467 enum tpacpi_volume_access_mode {
6468 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6469 TPACPI_VOL_MODE_EC, /* Pure EC control */
6470 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6471 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6475 enum tpacpi_volume_capabilities {
6476 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6477 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6478 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6482 static enum tpacpi_volume_access_mode volume_mode =
6483 TPACPI_VOL_MODE_MAX;
6485 static enum tpacpi_volume_capabilities volume_capabilities;
6486 static bool volume_control_allowed;
6489 * Used to syncronize writers to TP_EC_AUDIO and
6490 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6492 static struct mutex volume_mutex;
6494 static void tpacpi_volume_checkpoint_nvram(void)
6500 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6502 if (!volume_control_allowed)
6505 vdbg_printk(TPACPI_DBG_MIXER,
6506 "trying to checkpoint mixer state to NVRAM...\n");
6508 if (tp_features.mixer_no_level_control)
6509 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6511 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6513 if (mutex_lock_killable(&volume_mutex) < 0)
6516 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6519 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6521 if (lec != (b_nvram & ec_mask)) {
6522 /* NVRAM needs update */
6523 b_nvram &= ~ec_mask;
6525 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6526 dbg_printk(TPACPI_DBG_MIXER,
6527 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6528 (unsigned int) lec, (unsigned int) b_nvram);
6530 vdbg_printk(TPACPI_DBG_MIXER,
6531 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6532 (unsigned int) lec, (unsigned int) b_nvram);
6536 mutex_unlock(&volume_mutex);
6539 static int volume_get_status_ec(u8 *status)
6543 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6548 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6553 static int volume_get_status(u8 *status)
6555 return volume_get_status_ec(status);
6558 static int volume_set_status_ec(const u8 status)
6560 if (!acpi_ec_write(TP_EC_AUDIO, status))
6563 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6568 static int volume_set_status(const u8 status)
6570 return volume_set_status_ec(status);
6573 /* returns < 0 on error, 0 on no change, 1 on change */
6574 static int __volume_set_mute_ec(const bool mute)
6579 if (mutex_lock_killable(&volume_mutex) < 0)
6582 rc = volume_get_status_ec(&s);
6586 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6587 s & ~TP_EC_AUDIO_MUTESW_MSK;
6590 rc = volume_set_status_ec(n);
6596 mutex_unlock(&volume_mutex);
6600 static int volume_alsa_set_mute(const bool mute)
6602 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6603 (mute) ? "" : "un");
6604 return __volume_set_mute_ec(mute);
6607 static int volume_set_mute(const bool mute)
6611 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6612 (mute) ? "" : "un");
6614 rc = __volume_set_mute_ec(mute);
6615 return (rc < 0) ? rc : 0;
6618 /* returns < 0 on error, 0 on no change, 1 on change */
6619 static int __volume_set_volume_ec(const u8 vol)
6624 if (vol > TP_EC_VOLUME_MAX)
6627 if (mutex_lock_killable(&volume_mutex) < 0)
6630 rc = volume_get_status_ec(&s);
6634 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6637 rc = volume_set_status_ec(n);
6643 mutex_unlock(&volume_mutex);
6647 static int volume_alsa_set_volume(const u8 vol)
6649 dbg_printk(TPACPI_DBG_MIXER,
6650 "ALSA: trying to set volume level to %hu\n", vol);
6651 return __volume_set_volume_ec(vol);
6654 static void volume_alsa_notify_change(void)
6656 struct tpacpi_alsa_data *d;
6658 if (alsa_card && alsa_card->private_data) {
6659 d = alsa_card->private_data;
6661 snd_ctl_notify(alsa_card,
6662 SNDRV_CTL_EVENT_MASK_VALUE,
6665 snd_ctl_notify(alsa_card,
6666 SNDRV_CTL_EVENT_MASK_VALUE,
6671 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6672 struct snd_ctl_elem_info *uinfo)
6674 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6676 uinfo->value.integer.min = 0;
6677 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6681 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6682 struct snd_ctl_elem_value *ucontrol)
6687 rc = volume_get_status(&s);
6691 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6695 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6696 struct snd_ctl_elem_value *ucontrol)
6698 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6699 ucontrol->value.integer.value[0]);
6700 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6703 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6705 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6706 struct snd_ctl_elem_value *ucontrol)
6711 rc = volume_get_status(&s);
6715 ucontrol->value.integer.value[0] =
6716 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6720 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6721 struct snd_ctl_elem_value *ucontrol)
6723 tpacpi_disclose_usertask("ALSA", "%smute\n",
6724 ucontrol->value.integer.value[0] ?
6726 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6729 static struct snd_kcontrol_new volume_alsa_control_vol = {
6730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6731 .name = "Console Playback Volume",
6733 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6734 .info = volume_alsa_vol_info,
6735 .get = volume_alsa_vol_get,
6738 static struct snd_kcontrol_new volume_alsa_control_mute = {
6739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6740 .name = "Console Playback Switch",
6742 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6743 .info = volume_alsa_mute_info,
6744 .get = volume_alsa_mute_get,
6747 static void volume_suspend(void)
6749 tpacpi_volume_checkpoint_nvram();
6752 static void volume_resume(void)
6754 volume_alsa_notify_change();
6757 static void volume_shutdown(void)
6759 tpacpi_volume_checkpoint_nvram();
6762 static void volume_exit(void)
6765 snd_card_free(alsa_card);
6769 tpacpi_volume_checkpoint_nvram();
6772 static int __init volume_create_alsa_mixer(void)
6774 struct snd_card *card;
6775 struct tpacpi_alsa_data *data;
6776 struct snd_kcontrol *ctl_vol;
6777 struct snd_kcontrol *ctl_mute;
6780 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6781 sizeof(struct tpacpi_alsa_data), &card);
6782 if (rc < 0 || !card) {
6783 pr_err("Failed to create ALSA card structures: %d\n", rc);
6787 BUG_ON(!card->private_data);
6788 data = card->private_data;
6791 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6792 sizeof(card->driver));
6793 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6794 sizeof(card->shortname));
6795 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6796 (thinkpad_id.ec_version_str) ?
6797 thinkpad_id.ec_version_str : "(unknown)");
6798 snprintf(card->longname, sizeof(card->longname),
6799 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6800 (thinkpad_id.ec_version_str) ?
6801 thinkpad_id.ec_version_str : "unknown");
6803 if (volume_control_allowed) {
6804 volume_alsa_control_vol.put = volume_alsa_vol_put;
6805 volume_alsa_control_vol.access =
6806 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6808 volume_alsa_control_mute.put = volume_alsa_mute_put;
6809 volume_alsa_control_mute.access =
6810 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6813 if (!tp_features.mixer_no_level_control) {
6814 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6815 rc = snd_ctl_add(card, ctl_vol);
6817 pr_err("Failed to create ALSA volume control: %d\n",
6821 data->ctl_vol_id = &ctl_vol->id;
6824 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6825 rc = snd_ctl_add(card, ctl_mute);
6827 pr_err("Failed to create ALSA mute control: %d\n", rc);
6830 data->ctl_mute_id = &ctl_mute->id;
6832 snd_card_set_dev(card, &tpacpi_pdev->dev);
6833 rc = snd_card_register(card);
6835 pr_err("Failed to register ALSA card: %d\n", rc);
6843 snd_card_free(card);
6847 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6848 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6850 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6851 /* Whitelist volume level on all IBM by default */
6852 { .vendor = PCI_VENDOR_ID_IBM,
6853 .bios = TPACPI_MATCH_ANY,
6854 .ec = TPACPI_MATCH_ANY,
6855 .quirks = TPACPI_VOL_Q_LEVEL },
6857 /* Lenovo models with volume control (needs confirmation) */
6858 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6859 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6860 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6861 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6862 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6863 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6864 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6866 /* Whitelist mute-only on all Lenovo by default */
6867 { .vendor = PCI_VENDOR_ID_LENOVO,
6868 .bios = TPACPI_MATCH_ANY,
6869 .ec = TPACPI_MATCH_ANY,
6870 .quirks = TPACPI_VOL_Q_MUTEONLY }
6873 static int __init volume_init(struct ibm_init_struct *iibm)
6875 unsigned long quirks;
6878 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6880 mutex_init(&volume_mutex);
6883 * Check for module parameter bogosity, note that we
6884 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6885 * able to detect "unspecified"
6887 if (volume_mode > TPACPI_VOL_MODE_MAX)
6890 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6891 pr_err("UCMS step volume mode not implemented, "
6892 "please contact %s\n", TPACPI_MAIL);
6896 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6900 * The ALSA mixer is our primary interface.
6901 * When disabled, don't install the subdriver at all
6904 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6905 "ALSA mixer disabled by parameter, "
6906 "not loading volume subdriver...\n");
6910 quirks = tpacpi_check_quirks(volume_quirk_table,
6911 ARRAY_SIZE(volume_quirk_table));
6913 switch (volume_capabilities) {
6914 case TPACPI_VOL_CAP_AUTO:
6915 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6916 tp_features.mixer_no_level_control = 1;
6917 else if (quirks & TPACPI_VOL_Q_LEVEL)
6918 tp_features.mixer_no_level_control = 0;
6920 return 1; /* no mixer */
6922 case TPACPI_VOL_CAP_VOLMUTE:
6923 tp_features.mixer_no_level_control = 0;
6925 case TPACPI_VOL_CAP_MUTEONLY:
6926 tp_features.mixer_no_level_control = 1;
6932 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6933 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6934 "using user-supplied volume_capabilities=%d\n",
6935 volume_capabilities);
6937 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6938 volume_mode == TPACPI_VOL_MODE_MAX) {
6939 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6941 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6942 "driver auto-selected volume_mode=%d\n",
6945 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6946 "using user-supplied volume_mode=%d\n",
6950 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6951 "mute is supported, volume control is %s\n",
6952 str_supported(!tp_features.mixer_no_level_control));
6954 rc = volume_create_alsa_mixer();
6956 pr_err("Could not create the ALSA mixer interface\n");
6960 pr_info("Console audio control enabled, mode: %s\n",
6961 (volume_control_allowed) ?
6962 "override (read/write)" :
6963 "monitor (read only)");
6965 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6966 "registering volume hotkeys as change notification\n");
6967 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6968 | TP_ACPI_HKEY_VOLUP_MASK
6969 | TP_ACPI_HKEY_VOLDWN_MASK
6970 | TP_ACPI_HKEY_MUTE_MASK);
6975 static int volume_read(struct seq_file *m)
6979 if (volume_get_status(&status) < 0) {
6980 seq_printf(m, "level:\t\tunreadable\n");
6982 if (tp_features.mixer_no_level_control)
6983 seq_printf(m, "level:\t\tunsupported\n");
6985 seq_printf(m, "level:\t\t%d\n",
6986 status & TP_EC_AUDIO_LVL_MSK);
6988 seq_printf(m, "mute:\t\t%s\n",
6989 onoff(status, TP_EC_AUDIO_MUTESW));
6991 if (volume_control_allowed) {
6992 seq_printf(m, "commands:\tunmute, mute\n");
6993 if (!tp_features.mixer_no_level_control) {
6995 "commands:\tup, down\n");
6997 "commands:\tlevel <level>"
6998 " (<level> is 0-%d)\n",
7007 static int volume_write(char *buf)
7010 u8 new_level, new_mute;
7016 * We do allow volume control at driver startup, so that the
7017 * user can set initial state through the volume=... parameter hack.
7019 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7020 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7021 tp_warned.volume_ctrl_forbidden = 1;
7022 pr_notice("Console audio control in monitor mode, "
7023 "changes are not allowed\n");
7024 pr_notice("Use the volume_control=1 module parameter "
7025 "to enable volume control\n");
7030 rc = volume_get_status(&s);
7034 new_level = s & TP_EC_AUDIO_LVL_MSK;
7035 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7037 while ((cmd = next_cmd(&buf))) {
7038 if (!tp_features.mixer_no_level_control) {
7039 if (strlencmp(cmd, "up") == 0) {
7042 else if (new_level < TP_EC_VOLUME_MAX)
7045 } else if (strlencmp(cmd, "down") == 0) {
7048 else if (new_level > 0)
7051 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7052 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7057 if (strlencmp(cmd, "mute") == 0)
7058 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7059 else if (strlencmp(cmd, "unmute") == 0)
7065 if (tp_features.mixer_no_level_control) {
7066 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7067 new_mute ? "" : "un");
7068 rc = volume_set_mute(!!new_mute);
7070 tpacpi_disclose_usertask("procfs volume",
7071 "%smute and set level to %d\n",
7072 new_mute ? "" : "un", new_level);
7073 rc = volume_set_status(new_mute | new_level);
7075 volume_alsa_notify_change();
7077 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7080 static struct ibm_struct volume_driver_data = {
7082 .read = volume_read,
7083 .write = volume_write,
7084 .exit = volume_exit,
7085 .suspend = volume_suspend,
7086 .resume = volume_resume,
7087 .shutdown = volume_shutdown,
7090 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7092 #define alsa_card NULL
7094 static void inline volume_alsa_notify_change(void)
7098 static int __init volume_init(struct ibm_init_struct *iibm)
7100 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7105 static struct ibm_struct volume_driver_data = {
7109 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7111 /*************************************************************************
7118 * TPACPI_FAN_RD_ACPI_GFAN:
7119 * ACPI GFAN method: returns fan level
7121 * see TPACPI_FAN_WR_ACPI_SFAN
7122 * EC 0x2f (HFSP) not available if GFAN exists
7124 * TPACPI_FAN_WR_ACPI_SFAN:
7125 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7127 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7130 * TPACPI_FAN_WR_TPEC:
7131 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7132 * Supported on almost all ThinkPads
7134 * Fan speed changes of any sort (including those caused by the
7135 * disengaged mode) are usually done slowly by the firmware as the
7136 * maximum amount of fan duty cycle change per second seems to be
7139 * Reading is not available if GFAN exists.
7140 * Writing is not available if SFAN exists.
7143 * 7 automatic mode engaged;
7144 * (default operation mode of the ThinkPad)
7145 * fan level is ignored in this mode.
7146 * 6 full speed mode (takes precedence over bit 7);
7147 * not available on all thinkpads. May disable
7148 * the tachometer while the fan controller ramps up
7149 * the speed (which can take up to a few *minutes*).
7150 * Speeds up fan to 100% duty-cycle, which is far above
7151 * the standard RPM levels. It is not impossible that
7152 * it could cause hardware damage.
7153 * 5-3 unused in some models. Extra bits for fan level
7154 * in others, but still useless as all values above
7155 * 7 map to the same speed as level 7 in these models.
7156 * 2-0 fan level (0..7 usually)
7158 * 0x07 = max (set when temperatures critical)
7159 * Some ThinkPads may have other levels, see
7160 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7162 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7163 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7164 * does so, its initial value is meaningless (0x07).
7166 * For firmware bugs, refer to:
7167 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7171 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7172 * Main fan tachometer reading (in RPM)
7174 * This register is present on all ThinkPads with a new-style EC, and
7175 * it is known not to be present on the A21m/e, and T22, as there is
7176 * something else in offset 0x84 according to the ACPI DSDT. Other
7177 * ThinkPads from this same time period (and earlier) probably lack the
7178 * tachometer as well.
7180 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7181 * was never fixed by IBM to report the EC firmware version string
7182 * probably support the tachometer (like the early X models), so
7183 * detecting it is quite hard. We need more data to know for sure.
7185 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7188 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7191 * For firmware bugs, refer to:
7192 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7196 * ThinkPad EC register 0x31 bit 0 (only on select models)
7198 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7199 * show the speed of the main fan. When bit 0 of EC register 0x31
7200 * is one, the tachometer registers show the speed of the auxiliary
7203 * Fan control seems to affect both fans, regardless of the state
7206 * So far, only the firmware for the X60/X61 non-tablet versions
7207 * seem to support this (firmware TP-7M).
7209 * TPACPI_FAN_WR_ACPI_FANS:
7210 * ThinkPad X31, X40, X41. Not available in the X60.
7212 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7213 * high speed. ACPI DSDT seems to map these three speeds to levels
7214 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7215 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7217 * The speeds are stored on handles
7218 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7220 * There are three default speed sets, accessible as handles:
7221 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7223 * ACPI DSDT switches which set is in use depending on various
7226 * TPACPI_FAN_WR_TPEC is also available and should be used to
7227 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7228 * but the ACPI tables just mention level 7.
7231 enum { /* Fan control constants */
7232 fan_status_offset = 0x2f, /* EC register 0x2f */
7233 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7234 * 0x84 must be read before 0x85 */
7235 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7236 bit 0 selects which fan is active */
7238 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7239 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7241 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7244 enum fan_status_access_mode {
7245 TPACPI_FAN_NONE = 0, /* No fan status or control */
7246 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7247 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7250 enum fan_control_access_mode {
7251 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7252 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7253 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7254 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7257 enum fan_control_commands {
7258 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7259 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7260 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7261 * and also watchdog cmd */
7264 static bool fan_control_allowed;
7266 static enum fan_status_access_mode fan_status_access_mode;
7267 static enum fan_control_access_mode fan_control_access_mode;
7268 static enum fan_control_commands fan_control_commands;
7270 static u8 fan_control_initial_status;
7271 static u8 fan_control_desired_level;
7272 static u8 fan_control_resume_level;
7273 static int fan_watchdog_maxinterval;
7275 static struct mutex fan_mutex;
7277 static void fan_watchdog_fire(struct work_struct *ignored);
7278 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7280 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7281 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7282 "\\FSPD", /* 600e/x, 770e, 770x */
7284 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7285 "JFNS", /* 770x-JL */
7289 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7290 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7291 * be in auto mode (0x80).
7293 * This is corrected by any write to HFSP either by the driver, or
7296 * We assume 0x07 really means auto mode while this quirk is active,
7297 * as this is far more likely than the ThinkPad being in level 7,
7298 * which is only used by the firmware during thermal emergencies.
7300 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7301 * TP-70 (T43, R52), which are known to be buggy.
7304 static void fan_quirk1_setup(void)
7306 if (fan_control_initial_status == 0x07) {
7307 pr_notice("fan_init: initial fan status is unknown, "
7308 "assuming it is in auto mode\n");
7309 tp_features.fan_ctrl_status_undef = 1;
7313 static void fan_quirk1_handle(u8 *fan_status)
7315 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7316 if (*fan_status != fan_control_initial_status) {
7317 /* something changed the HFSP regisnter since
7318 * driver init time, so it is not undefined
7320 tp_features.fan_ctrl_status_undef = 0;
7322 /* Return most likely status. In fact, it
7323 * might be the only possible status */
7324 *fan_status = TP_EC_FAN_AUTO;
7329 /* Select main fan on X60/X61, NOOP on others */
7330 static bool fan_select_fan1(void)
7332 if (tp_features.second_fan) {
7335 if (ec_read(fan_select_offset, &val) < 0)
7338 if (ec_write(fan_select_offset, val) < 0)
7344 /* Select secondary fan on X60/X61 */
7345 static bool fan_select_fan2(void)
7349 if (!tp_features.second_fan)
7352 if (ec_read(fan_select_offset, &val) < 0)
7355 if (ec_write(fan_select_offset, val) < 0)
7362 * Call with fan_mutex held
7364 static void fan_update_desired_level(u8 status)
7367 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7369 fan_control_desired_level = 7;
7371 fan_control_desired_level = status;
7375 static int fan_get_status(u8 *status)
7380 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7382 switch (fan_status_access_mode) {
7383 case TPACPI_FAN_RD_ACPI_GFAN: {
7384 /* 570, 600e/x, 770e, 770x */
7387 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
7391 *status = res & 0x07;
7395 case TPACPI_FAN_RD_TPEC:
7396 /* all except 570, 600e/x, 770e, 770x */
7397 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7400 if (likely(status)) {
7402 fan_quirk1_handle(status);
7414 static int fan_get_status_safe(u8 *status)
7419 if (mutex_lock_killable(&fan_mutex))
7420 return -ERESTARTSYS;
7421 rc = fan_get_status(&s);
7423 fan_update_desired_level(s);
7424 mutex_unlock(&fan_mutex);
7432 static int fan_get_speed(unsigned int *speed)
7436 switch (fan_status_access_mode) {
7437 case TPACPI_FAN_RD_TPEC:
7438 /* all except 570, 600e/x, 770e, 770x */
7439 if (unlikely(!fan_select_fan1()))
7441 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7442 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7446 *speed = (hi << 8) | lo;
7457 static int fan2_get_speed(unsigned int *speed)
7462 switch (fan_status_access_mode) {
7463 case TPACPI_FAN_RD_TPEC:
7464 /* all except 570, 600e/x, 770e, 770x */
7465 if (unlikely(!fan_select_fan2()))
7467 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7468 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7469 fan_select_fan1(); /* play it safe */
7474 *speed = (hi << 8) | lo;
7485 static int fan_set_level(int level)
7487 if (!fan_control_allowed)
7490 switch (fan_control_access_mode) {
7491 case TPACPI_FAN_WR_ACPI_SFAN:
7492 if (level >= 0 && level <= 7) {
7493 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7499 case TPACPI_FAN_WR_ACPI_FANS:
7500 case TPACPI_FAN_WR_TPEC:
7501 if (!(level & TP_EC_FAN_AUTO) &&
7502 !(level & TP_EC_FAN_FULLSPEED) &&
7503 ((level < 0) || (level > 7)))
7506 /* safety net should the EC not support AUTO
7507 * or FULLSPEED mode bits and just ignore them */
7508 if (level & TP_EC_FAN_FULLSPEED)
7509 level |= 7; /* safety min speed 7 */
7510 else if (level & TP_EC_FAN_AUTO)
7511 level |= 4; /* safety min speed 4 */
7513 if (!acpi_ec_write(fan_status_offset, level))
7516 tp_features.fan_ctrl_status_undef = 0;
7523 vdbg_printk(TPACPI_DBG_FAN,
7524 "fan control: set fan control register to 0x%02x\n", level);
7528 static int fan_set_level_safe(int level)
7532 if (!fan_control_allowed)
7535 if (mutex_lock_killable(&fan_mutex))
7536 return -ERESTARTSYS;
7538 if (level == TPACPI_FAN_LAST_LEVEL)
7539 level = fan_control_desired_level;
7541 rc = fan_set_level(level);
7543 fan_update_desired_level(level);
7545 mutex_unlock(&fan_mutex);
7549 static int fan_set_enable(void)
7554 if (!fan_control_allowed)
7557 if (mutex_lock_killable(&fan_mutex))
7558 return -ERESTARTSYS;
7560 switch (fan_control_access_mode) {
7561 case TPACPI_FAN_WR_ACPI_FANS:
7562 case TPACPI_FAN_WR_TPEC:
7563 rc = fan_get_status(&s);
7567 /* Don't go out of emergency fan mode */
7570 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7573 if (!acpi_ec_write(fan_status_offset, s))
7576 tp_features.fan_ctrl_status_undef = 0;
7581 case TPACPI_FAN_WR_ACPI_SFAN:
7582 rc = fan_get_status(&s);
7588 /* Set fan to at least level 4 */
7591 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7601 mutex_unlock(&fan_mutex);
7604 vdbg_printk(TPACPI_DBG_FAN,
7605 "fan control: set fan control register to 0x%02x\n",
7610 static int fan_set_disable(void)
7614 if (!fan_control_allowed)
7617 if (mutex_lock_killable(&fan_mutex))
7618 return -ERESTARTSYS;
7621 switch (fan_control_access_mode) {
7622 case TPACPI_FAN_WR_ACPI_FANS:
7623 case TPACPI_FAN_WR_TPEC:
7624 if (!acpi_ec_write(fan_status_offset, 0x00))
7627 fan_control_desired_level = 0;
7628 tp_features.fan_ctrl_status_undef = 0;
7632 case TPACPI_FAN_WR_ACPI_SFAN:
7633 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7636 fan_control_desired_level = 0;
7644 vdbg_printk(TPACPI_DBG_FAN,
7645 "fan control: set fan control register to 0\n");
7647 mutex_unlock(&fan_mutex);
7651 static int fan_set_speed(int speed)
7655 if (!fan_control_allowed)
7658 if (mutex_lock_killable(&fan_mutex))
7659 return -ERESTARTSYS;
7662 switch (fan_control_access_mode) {
7663 case TPACPI_FAN_WR_ACPI_FANS:
7664 if (speed >= 0 && speed <= 65535) {
7665 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7666 speed, speed, speed))
7676 mutex_unlock(&fan_mutex);
7680 static void fan_watchdog_reset(void)
7682 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7685 if (fan_watchdog_maxinterval > 0 &&
7686 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
7687 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
7688 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
7690 cancel_delayed_work(&fan_watchdog_task);
7693 static void fan_watchdog_fire(struct work_struct *ignored)
7697 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7700 pr_notice("fan watchdog: enabling fan\n");
7701 rc = fan_set_enable();
7703 pr_err("fan watchdog: error %d while enabling fan, "
7704 "will try again later...\n", -rc);
7705 /* reschedule for later */
7706 fan_watchdog_reset();
7711 * SYSFS fan layout: hwmon compatible (device)
7714 * 0: "disengaged" mode
7716 * 2: native EC "auto" mode (recommended, hardware default)
7718 * pwm*: set speed in manual mode, ignored otherwise.
7719 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7722 * fan*_input: tachometer reading, RPM
7725 * SYSFS fan layout: extensions
7727 * fan_watchdog (driver):
7728 * fan watchdog interval in seconds, 0 disables (default), max 120
7731 /* sysfs fan pwm1_enable ----------------------------------------------- */
7732 static ssize_t fan_pwm1_enable_show(struct device *dev,
7733 struct device_attribute *attr,
7739 res = fan_get_status_safe(&status);
7743 if (status & TP_EC_FAN_FULLSPEED) {
7745 } else if (status & TP_EC_FAN_AUTO) {
7750 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7753 static ssize_t fan_pwm1_enable_store(struct device *dev,
7754 struct device_attribute *attr,
7755 const char *buf, size_t count)
7760 if (parse_strtoul(buf, 2, &t))
7763 tpacpi_disclose_usertask("hwmon pwm1_enable",
7764 "set fan mode to %lu\n", t);
7768 level = TP_EC_FAN_FULLSPEED;
7771 level = TPACPI_FAN_LAST_LEVEL;
7774 level = TP_EC_FAN_AUTO;
7777 /* reserved for software-controlled auto mode */
7783 res = fan_set_level_safe(level);
7789 fan_watchdog_reset();
7794 static struct device_attribute dev_attr_fan_pwm1_enable =
7795 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7796 fan_pwm1_enable_show, fan_pwm1_enable_store);
7798 /* sysfs fan pwm1 ------------------------------------------------------ */
7799 static ssize_t fan_pwm1_show(struct device *dev,
7800 struct device_attribute *attr,
7806 res = fan_get_status_safe(&status);
7811 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7812 status = fan_control_desired_level;
7817 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7820 static ssize_t fan_pwm1_store(struct device *dev,
7821 struct device_attribute *attr,
7822 const char *buf, size_t count)
7826 u8 status, newlevel;
7828 if (parse_strtoul(buf, 255, &s))
7831 tpacpi_disclose_usertask("hwmon pwm1",
7832 "set fan speed to %lu\n", s);
7834 /* scale down from 0-255 to 0-7 */
7835 newlevel = (s >> 5) & 0x07;
7837 if (mutex_lock_killable(&fan_mutex))
7838 return -ERESTARTSYS;
7840 rc = fan_get_status(&status);
7841 if (!rc && (status &
7842 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7843 rc = fan_set_level(newlevel);
7847 fan_update_desired_level(newlevel);
7848 fan_watchdog_reset();
7852 mutex_unlock(&fan_mutex);
7853 return (rc)? rc : count;
7856 static struct device_attribute dev_attr_fan_pwm1 =
7857 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7858 fan_pwm1_show, fan_pwm1_store);
7860 /* sysfs fan fan1_input ------------------------------------------------ */
7861 static ssize_t fan_fan1_input_show(struct device *dev,
7862 struct device_attribute *attr,
7868 res = fan_get_speed(&speed);
7872 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7875 static struct device_attribute dev_attr_fan_fan1_input =
7876 __ATTR(fan1_input, S_IRUGO,
7877 fan_fan1_input_show, NULL);
7879 /* sysfs fan fan2_input ------------------------------------------------ */
7880 static ssize_t fan_fan2_input_show(struct device *dev,
7881 struct device_attribute *attr,
7887 res = fan2_get_speed(&speed);
7891 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7894 static struct device_attribute dev_attr_fan_fan2_input =
7895 __ATTR(fan2_input, S_IRUGO,
7896 fan_fan2_input_show, NULL);
7898 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7899 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7902 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7905 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7906 const char *buf, size_t count)
7910 if (parse_strtoul(buf, 120, &t))
7913 if (!fan_control_allowed)
7916 fan_watchdog_maxinterval = t;
7917 fan_watchdog_reset();
7919 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7924 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7925 fan_fan_watchdog_show, fan_fan_watchdog_store);
7927 /* --------------------------------------------------------------------- */
7928 static struct attribute *fan_attributes[] = {
7929 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7930 &dev_attr_fan_fan1_input.attr,
7931 NULL, /* for fan2_input */
7935 static const struct attribute_group fan_attr_group = {
7936 .attrs = fan_attributes,
7939 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7940 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7942 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7943 { .vendor = PCI_VENDOR_ID_IBM, \
7944 .bios = TPACPI_MATCH_ANY, \
7945 .ec = TPID(__id1, __id2), \
7946 .quirks = __quirks }
7948 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7949 { .vendor = PCI_VENDOR_ID_LENOVO, \
7950 .bios = TPACPI_MATCH_ANY, \
7951 .ec = TPID(__id1, __id2), \
7952 .quirks = __quirks }
7954 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7955 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7956 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7957 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7958 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7959 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7962 #undef TPACPI_FAN_QL
7963 #undef TPACPI_FAN_QI
7965 static int __init fan_init(struct ibm_init_struct *iibm)
7968 unsigned long quirks;
7970 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7971 "initializing fan subdriver\n");
7973 mutex_init(&fan_mutex);
7974 fan_status_access_mode = TPACPI_FAN_NONE;
7975 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7976 fan_control_commands = 0;
7977 fan_watchdog_maxinterval = 0;
7978 tp_features.fan_ctrl_status_undef = 0;
7979 tp_features.second_fan = 0;
7980 fan_control_desired_level = 7;
7982 if (tpacpi_is_ibm()) {
7983 TPACPI_ACPIHANDLE_INIT(fans);
7984 TPACPI_ACPIHANDLE_INIT(gfan);
7985 TPACPI_ACPIHANDLE_INIT(sfan);
7988 quirks = tpacpi_check_quirks(fan_quirk_table,
7989 ARRAY_SIZE(fan_quirk_table));
7992 /* 570, 600e/x, 770e, 770x */
7993 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7995 /* all other ThinkPads: note that even old-style
7996 * ThinkPad ECs supports the fan control register */
7997 if (likely(acpi_ec_read(fan_status_offset,
7998 &fan_control_initial_status))) {
7999 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8000 if (quirks & TPACPI_FAN_Q1)
8002 if (quirks & TPACPI_FAN_2FAN) {
8003 tp_features.second_fan = 1;
8004 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8005 "secondary fan support enabled\n");
8008 pr_err("ThinkPad ACPI EC access misbehaving, "
8009 "fan status and control unavailable\n");
8016 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8017 fan_control_commands |=
8018 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8021 /* gfan without sfan means no fan control */
8022 /* all other models implement TP EC 0x2f control */
8026 fan_control_access_mode =
8027 TPACPI_FAN_WR_ACPI_FANS;
8028 fan_control_commands |=
8029 TPACPI_FAN_CMD_SPEED |
8030 TPACPI_FAN_CMD_LEVEL |
8031 TPACPI_FAN_CMD_ENABLE;
8033 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8034 fan_control_commands |=
8035 TPACPI_FAN_CMD_LEVEL |
8036 TPACPI_FAN_CMD_ENABLE;
8041 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8042 "fan is %s, modes %d, %d\n",
8043 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8044 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8045 fan_status_access_mode, fan_control_access_mode);
8047 /* fan control master switch */
8048 if (!fan_control_allowed) {
8049 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8050 fan_control_commands = 0;
8051 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8052 "fan control features disabled by parameter\n");
8055 /* update fan_control_desired_level */
8056 if (fan_status_access_mode != TPACPI_FAN_NONE)
8057 fan_get_status_safe(NULL);
8059 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8060 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8061 if (tp_features.second_fan) {
8062 /* attach second fan tachometer */
8063 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8064 &dev_attr_fan_fan2_input.attr;
8066 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8071 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8072 &driver_attr_fan_watchdog);
8074 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8083 static void fan_exit(void)
8085 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8086 "cancelling any pending fan watchdog tasks\n");
8088 /* FIXME: can we really do this unconditionally? */
8089 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8090 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8091 &driver_attr_fan_watchdog);
8093 cancel_delayed_work(&fan_watchdog_task);
8094 flush_workqueue(tpacpi_wq);
8097 static void fan_suspend(void)
8101 if (!fan_control_allowed)
8104 /* Store fan status in cache */
8105 fan_control_resume_level = 0;
8106 rc = fan_get_status_safe(&fan_control_resume_level);
8108 pr_notice("failed to read fan level for later "
8109 "restore during resume: %d\n", rc);
8111 /* if it is undefined, don't attempt to restore it.
8113 if (tp_features.fan_ctrl_status_undef)
8114 fan_control_resume_level = 0;
8117 static void fan_resume(void)
8119 u8 current_level = 7;
8120 bool do_set = false;
8123 /* DSDT *always* updates status on resume */
8124 tp_features.fan_ctrl_status_undef = 0;
8126 if (!fan_control_allowed ||
8127 !fan_control_resume_level ||
8128 (fan_get_status_safe(¤t_level) < 0))
8131 switch (fan_control_access_mode) {
8132 case TPACPI_FAN_WR_ACPI_SFAN:
8133 /* never decrease fan level */
8134 do_set = (fan_control_resume_level > current_level);
8136 case TPACPI_FAN_WR_ACPI_FANS:
8137 case TPACPI_FAN_WR_TPEC:
8138 /* never decrease fan level, scale is:
8139 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8141 * We expect the firmware to set either 7 or AUTO, but we
8142 * handle FULLSPEED out of paranoia.
8144 * So, we can safely only restore FULLSPEED or 7, anything
8145 * else could slow the fan. Restoring AUTO is useless, at
8146 * best that's exactly what the DSDT already set (it is the
8149 * Always keep in mind that the DSDT *will* have set the
8150 * fans to what the vendor supposes is the best level. We
8151 * muck with it only to speed the fan up.
8153 if (fan_control_resume_level != 7 &&
8154 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8157 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8158 (current_level != fan_control_resume_level);
8164 pr_notice("restoring fan level to 0x%02x\n",
8165 fan_control_resume_level);
8166 rc = fan_set_level_safe(fan_control_resume_level);
8168 pr_notice("failed to restore fan level: %d\n", rc);
8172 static int fan_read(struct seq_file *m)
8176 unsigned int speed = 0;
8178 switch (fan_status_access_mode) {
8179 case TPACPI_FAN_RD_ACPI_GFAN:
8180 /* 570, 600e/x, 770e, 770x */
8181 rc = fan_get_status_safe(&status);
8185 seq_printf(m, "status:\t\t%s\n"
8187 (status != 0) ? "enabled" : "disabled", status);
8190 case TPACPI_FAN_RD_TPEC:
8191 /* all except 570, 600e/x, 770e, 770x */
8192 rc = fan_get_status_safe(&status);
8196 seq_printf(m, "status:\t\t%s\n",
8197 (status != 0) ? "enabled" : "disabled");
8199 rc = fan_get_speed(&speed);
8203 seq_printf(m, "speed:\t\t%d\n", speed);
8205 if (status & TP_EC_FAN_FULLSPEED)
8206 /* Disengaged mode takes precedence */
8207 seq_printf(m, "level:\t\tdisengaged\n");
8208 else if (status & TP_EC_FAN_AUTO)
8209 seq_printf(m, "level:\t\tauto\n");
8211 seq_printf(m, "level:\t\t%d\n", status);
8214 case TPACPI_FAN_NONE:
8216 seq_printf(m, "status:\t\tnot supported\n");
8219 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8220 seq_printf(m, "commands:\tlevel <level>");
8222 switch (fan_control_access_mode) {
8223 case TPACPI_FAN_WR_ACPI_SFAN:
8224 seq_printf(m, " (<level> is 0-7)\n");
8228 seq_printf(m, " (<level> is 0-7, "
8229 "auto, disengaged, full-speed)\n");
8234 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8235 seq_printf(m, "commands:\tenable, disable\n"
8236 "commands:\twatchdog <timeout> (<timeout> "
8237 "is 0 (off), 1-120 (seconds))\n");
8239 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8240 seq_printf(m, "commands:\tspeed <speed>"
8241 " (<speed> is 0-65535)\n");
8246 static int fan_write_cmd_level(const char *cmd, int *rc)
8250 if (strlencmp(cmd, "level auto") == 0)
8251 level = TP_EC_FAN_AUTO;
8252 else if ((strlencmp(cmd, "level disengaged") == 0) |
8253 (strlencmp(cmd, "level full-speed") == 0))
8254 level = TP_EC_FAN_FULLSPEED;
8255 else if (sscanf(cmd, "level %d", &level) != 1)
8258 *rc = fan_set_level_safe(level);
8260 pr_err("level command accepted for unsupported access mode %d\n",
8261 fan_control_access_mode);
8263 tpacpi_disclose_usertask("procfs fan",
8264 "set level to %d\n", level);
8269 static int fan_write_cmd_enable(const char *cmd, int *rc)
8271 if (strlencmp(cmd, "enable") != 0)
8274 *rc = fan_set_enable();
8276 pr_err("enable command accepted for unsupported access mode %d\n",
8277 fan_control_access_mode);
8279 tpacpi_disclose_usertask("procfs fan", "enable\n");
8284 static int fan_write_cmd_disable(const char *cmd, int *rc)
8286 if (strlencmp(cmd, "disable") != 0)
8289 *rc = fan_set_disable();
8291 pr_err("disable command accepted for unsupported access mode %d\n",
8292 fan_control_access_mode);
8294 tpacpi_disclose_usertask("procfs fan", "disable\n");
8299 static int fan_write_cmd_speed(const char *cmd, int *rc)
8304 * Support speed <low> <medium> <high> ? */
8306 if (sscanf(cmd, "speed %d", &speed) != 1)
8309 *rc = fan_set_speed(speed);
8311 pr_err("speed command accepted for unsupported access mode %d\n",
8312 fan_control_access_mode);
8314 tpacpi_disclose_usertask("procfs fan",
8315 "set speed to %d\n", speed);
8320 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8324 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8327 if (interval < 0 || interval > 120)
8330 fan_watchdog_maxinterval = interval;
8331 tpacpi_disclose_usertask("procfs fan",
8332 "set watchdog timer to %d\n",
8339 static int fan_write(char *buf)
8344 while (!rc && (cmd = next_cmd(&buf))) {
8345 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8346 fan_write_cmd_level(cmd, &rc)) &&
8347 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8348 (fan_write_cmd_enable(cmd, &rc) ||
8349 fan_write_cmd_disable(cmd, &rc) ||
8350 fan_write_cmd_watchdog(cmd, &rc))) &&
8351 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8352 fan_write_cmd_speed(cmd, &rc))
8356 fan_watchdog_reset();
8362 static struct ibm_struct fan_driver_data = {
8367 .suspend = fan_suspend,
8368 .resume = fan_resume,
8371 /****************************************************************************
8372 ****************************************************************************
8376 ****************************************************************************
8377 ****************************************************************************/
8380 * HKEY event callout for other subdrivers go here
8381 * (yes, it is ugly, but it is quick, safe, and gets the job done
8383 static void tpacpi_driver_event(const unsigned int hkey_event)
8385 if (ibm_backlight_device) {
8386 switch (hkey_event) {
8387 case TP_HKEY_EV_BRGHT_UP:
8388 case TP_HKEY_EV_BRGHT_DOWN:
8389 tpacpi_brightness_notify_change();
8393 switch (hkey_event) {
8394 case TP_HKEY_EV_VOL_UP:
8395 case TP_HKEY_EV_VOL_DOWN:
8396 case TP_HKEY_EV_VOL_MUTE:
8397 volume_alsa_notify_change();
8402 static void hotkey_driver_event(const unsigned int scancode)
8404 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8407 /* sysfs name ---------------------------------------------------------- */
8408 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8409 struct device_attribute *attr,
8412 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8415 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8416 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8418 /* --------------------------------------------------------------------- */
8421 static struct proc_dir_entry *proc_dir;
8424 * Module and infrastructure proble, init and exit handling
8427 static bool force_load;
8429 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8430 static const char * __init str_supported(int is_supported)
8432 static char text_unsupported[] __initdata = "not supported";
8434 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8436 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8438 static void ibm_exit(struct ibm_struct *ibm)
8440 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8442 list_del_init(&ibm->all_drivers);
8444 if (ibm->flags.acpi_notify_installed) {
8445 dbg_printk(TPACPI_DBG_EXIT,
8446 "%s: acpi_remove_notify_handler\n", ibm->name);
8448 acpi_remove_notify_handler(*ibm->acpi->handle,
8450 dispatch_acpi_notify);
8451 ibm->flags.acpi_notify_installed = 0;
8454 if (ibm->flags.proc_created) {
8455 dbg_printk(TPACPI_DBG_EXIT,
8456 "%s: remove_proc_entry\n", ibm->name);
8457 remove_proc_entry(ibm->name, proc_dir);
8458 ibm->flags.proc_created = 0;
8461 if (ibm->flags.acpi_driver_registered) {
8462 dbg_printk(TPACPI_DBG_EXIT,
8463 "%s: acpi_bus_unregister_driver\n", ibm->name);
8465 acpi_bus_unregister_driver(ibm->acpi->driver);
8466 kfree(ibm->acpi->driver);
8467 ibm->acpi->driver = NULL;
8468 ibm->flags.acpi_driver_registered = 0;
8471 if (ibm->flags.init_called && ibm->exit) {
8473 ibm->flags.init_called = 0;
8476 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8479 static int __init ibm_init(struct ibm_init_struct *iibm)
8482 struct ibm_struct *ibm = iibm->data;
8483 struct proc_dir_entry *entry;
8485 BUG_ON(ibm == NULL);
8487 INIT_LIST_HEAD(&ibm->all_drivers);
8489 if (ibm->flags.experimental && !experimental)
8492 dbg_printk(TPACPI_DBG_INIT,
8493 "probing for %s\n", ibm->name);
8496 ret = iibm->init(iibm);
8498 return 0; /* probe failed */
8502 ibm->flags.init_called = 1;
8506 if (ibm->acpi->hid) {
8507 ret = register_tpacpi_subdriver(ibm);
8512 if (ibm->acpi->notify) {
8513 ret = setup_acpi_notify(ibm);
8514 if (ret == -ENODEV) {
8515 pr_notice("disabling subdriver %s\n",
8525 dbg_printk(TPACPI_DBG_INIT,
8526 "%s installed\n", ibm->name);
8529 umode_t mode = iibm->base_procfs_mode;
8535 entry = proc_create_data(ibm->name, mode, proc_dir,
8536 &dispatch_proc_fops, ibm);
8538 pr_err("unable to create proc entry %s\n", ibm->name);
8542 ibm->flags.proc_created = 1;
8545 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8550 dbg_printk(TPACPI_DBG_INIT,
8551 "%s: at error exit path with result %d\n",
8555 return (ret < 0)? ret : 0;
8560 static bool __pure __init tpacpi_is_fw_digit(const char c)
8562 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8565 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8566 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8569 return s && strlen(s) >= 8 &&
8570 tpacpi_is_fw_digit(s[0]) &&
8571 tpacpi_is_fw_digit(s[1]) &&
8573 (s[3] == 'T' || s[3] == 'N') &&
8574 tpacpi_is_fw_digit(s[4]) &&
8575 tpacpi_is_fw_digit(s[5]);
8578 /* returns 0 - probe ok, or < 0 - probe error.
8579 * Probe ok doesn't mean thinkpad found.
8580 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8581 static int __must_check __init get_thinkpad_model_data(
8582 struct thinkpad_id_data *tp)
8584 const struct dmi_device *dev = NULL;
8585 char ec_fw_string[18];
8591 memset(tp, 0, sizeof(*tp));
8593 if (dmi_name_in_vendors("IBM"))
8594 tp->vendor = PCI_VENDOR_ID_IBM;
8595 else if (dmi_name_in_vendors("LENOVO"))
8596 tp->vendor = PCI_VENDOR_ID_LENOVO;
8600 s = dmi_get_system_info(DMI_BIOS_VERSION);
8601 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8602 if (s && !tp->bios_version_str)
8605 /* Really ancient ThinkPad 240X will fail this, which is fine */
8606 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
8607 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
8610 tp->bios_model = tp->bios_version_str[0]
8611 | (tp->bios_version_str[1] << 8);
8612 tp->bios_release = (tp->bios_version_str[4] << 8)
8613 | tp->bios_version_str[5];
8616 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8617 * X32 or newer, all Z series; Some models must have an
8618 * up-to-date BIOS or they will not be detected.
8620 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8622 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8623 if (sscanf(dev->name,
8624 "IBM ThinkPad Embedded Controller -[%17c",
8625 ec_fw_string) == 1) {
8626 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8627 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8629 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8630 if (!tp->ec_version_str)
8633 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8634 tp->ec_model = ec_fw_string[0]
8635 | (ec_fw_string[1] << 8);
8636 tp->ec_release = (ec_fw_string[4] << 8)
8639 pr_notice("ThinkPad firmware release %s "
8640 "doesn't match the known patterns\n",
8642 pr_notice("please report this to %s\n",
8649 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8650 if (s && !(strnicmp(s, "ThinkPad", 8) && strnicmp(s, "Lenovo", 6))) {
8651 tp->model_str = kstrdup(s, GFP_KERNEL);
8655 s = dmi_get_system_info(DMI_BIOS_VENDOR);
8656 if (s && !(strnicmp(s, "Lenovo", 6))) {
8657 tp->model_str = kstrdup(s, GFP_KERNEL);
8663 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8664 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8665 if (s && !tp->nummodel_str)
8671 static int __init probe_for_thinkpad(void)
8678 /* It would be dangerous to run the driver in this case */
8679 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8683 * Non-ancient models have better DMI tagging, but very old models
8684 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8686 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8687 (thinkpad_id.ec_model != 0) ||
8688 tpacpi_is_fw_known();
8690 /* The EC handler is required */
8691 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8694 pr_err("Not yet supported ThinkPad detected!\n");
8698 if (!is_thinkpad && !force_load)
8704 static void __init thinkpad_acpi_init_banner(void)
8706 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8707 pr_info("%s\n", TPACPI_URL);
8709 pr_info("ThinkPad BIOS %s, EC %s\n",
8710 (thinkpad_id.bios_version_str) ?
8711 thinkpad_id.bios_version_str : "unknown",
8712 (thinkpad_id.ec_version_str) ?
8713 thinkpad_id.ec_version_str : "unknown");
8715 BUG_ON(!thinkpad_id.vendor);
8717 if (thinkpad_id.model_str)
8718 pr_info("%s %s, model %s\n",
8719 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8720 "IBM" : ((thinkpad_id.vendor ==
8721 PCI_VENDOR_ID_LENOVO) ?
8722 "Lenovo" : "Unknown vendor"),
8723 thinkpad_id.model_str,
8724 (thinkpad_id.nummodel_str) ?
8725 thinkpad_id.nummodel_str : "unknown");
8728 /* Module init, exit, parameters */
8730 static struct ibm_init_struct ibms_init[] __initdata = {
8732 .data = &thinkpad_acpi_driver_data,
8735 .init = hotkey_init,
8736 .data = &hotkey_driver_data,
8739 .init = bluetooth_init,
8740 .data = &bluetooth_driver_data,
8744 .data = &wan_driver_data,
8748 .data = &uwb_driver_data,
8750 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8753 .base_procfs_mode = S_IRUSR,
8754 .data = &video_driver_data,
8759 .data = &light_driver_data,
8763 .data = &cmos_driver_data,
8767 .data = &led_driver_data,
8771 .data = &beep_driver_data,
8774 .init = thermal_init,
8775 .data = &thermal_driver_data,
8778 .init = brightness_init,
8779 .data = &brightness_driver_data,
8782 .init = volume_init,
8783 .data = &volume_driver_data,
8787 .data = &fan_driver_data,
8791 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8794 struct ibm_struct *ibm;
8796 if (!kp || !kp->name || !val)
8799 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8800 ibm = ibms_init[i].data;
8801 WARN_ON(ibm == NULL);
8803 if (!ibm || !ibm->name)
8806 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8807 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8809 strcpy(ibms_init[i].param, val);
8810 strcat(ibms_init[i].param, ",");
8818 module_param(experimental, int, 0444);
8819 MODULE_PARM_DESC(experimental,
8820 "Enables experimental features when non-zero");
8822 module_param_named(debug, dbg_level, uint, 0);
8823 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8825 module_param(force_load, bool, 0444);
8826 MODULE_PARM_DESC(force_load,
8827 "Attempts to load the driver even on a "
8828 "mis-identified ThinkPad when true");
8830 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8831 MODULE_PARM_DESC(fan_control,
8832 "Enables setting fan parameters features when true");
8834 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8835 MODULE_PARM_DESC(brightness_mode,
8836 "Selects brightness control strategy: "
8837 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8839 module_param(brightness_enable, uint, 0444);
8840 MODULE_PARM_DESC(brightness_enable,
8841 "Enables backlight control when 1, disables when 0");
8843 module_param(hotkey_report_mode, uint, 0444);
8844 MODULE_PARM_DESC(hotkey_report_mode,
8845 "used for backwards compatibility with userspace, "
8846 "see documentation");
8848 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8849 module_param_named(volume_mode, volume_mode, uint, 0444);
8850 MODULE_PARM_DESC(volume_mode,
8851 "Selects volume control strategy: "
8852 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8854 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8855 MODULE_PARM_DESC(volume_capabilities,
8856 "Selects the mixer capabilites: "
8857 "0=auto, 1=volume and mute, 2=mute only");
8859 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8860 MODULE_PARM_DESC(volume_control,
8861 "Enables software override for the console audio "
8862 "control when true");
8864 /* ALSA module API parameters */
8865 module_param_named(index, alsa_index, int, 0444);
8866 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8867 module_param_named(id, alsa_id, charp, 0444);
8868 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8869 module_param_named(enable, alsa_enable, bool, 0444);
8870 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8871 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8873 #define TPACPI_PARAM(feature) \
8874 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8875 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8876 "at module load, see documentation")
8878 TPACPI_PARAM(hotkey);
8879 TPACPI_PARAM(bluetooth);
8880 TPACPI_PARAM(video);
8881 TPACPI_PARAM(light);
8885 TPACPI_PARAM(brightness);
8886 TPACPI_PARAM(volume);
8889 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8890 module_param(dbg_wlswemul, uint, 0444);
8891 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8892 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8893 MODULE_PARM_DESC(wlsw_state,
8894 "Initial state of the emulated WLSW switch");
8896 module_param(dbg_bluetoothemul, uint, 0444);
8897 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8898 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8899 MODULE_PARM_DESC(bluetooth_state,
8900 "Initial state of the emulated bluetooth switch");
8902 module_param(dbg_wwanemul, uint, 0444);
8903 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8904 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8905 MODULE_PARM_DESC(wwan_state,
8906 "Initial state of the emulated WWAN switch");
8908 module_param(dbg_uwbemul, uint, 0444);
8909 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8910 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8911 MODULE_PARM_DESC(uwb_state,
8912 "Initial state of the emulated UWB switch");
8915 static void thinkpad_acpi_module_exit(void)
8917 struct ibm_struct *ibm, *itmp;
8919 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8921 list_for_each_entry_safe_reverse(ibm, itmp,
8922 &tpacpi_all_drivers,
8927 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8929 if (tpacpi_inputdev) {
8930 if (tp_features.input_device_registered)
8931 input_unregister_device(tpacpi_inputdev);
8933 input_free_device(tpacpi_inputdev);
8934 kfree(hotkey_keycode_map);
8938 hwmon_device_unregister(tpacpi_hwmon);
8940 if (tp_features.sensors_pdev_attrs_registered)
8941 device_remove_file(&tpacpi_sensors_pdev->dev,
8942 &dev_attr_thinkpad_acpi_pdev_name);
8943 if (tpacpi_sensors_pdev)
8944 platform_device_unregister(tpacpi_sensors_pdev);
8946 platform_device_unregister(tpacpi_pdev);
8948 if (tp_features.sensors_pdrv_attrs_registered)
8949 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8950 if (tp_features.platform_drv_attrs_registered)
8951 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8953 if (tp_features.sensors_pdrv_registered)
8954 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8956 if (tp_features.platform_drv_registered)
8957 platform_driver_unregister(&tpacpi_pdriver);
8960 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8963 destroy_workqueue(tpacpi_wq);
8965 kfree(thinkpad_id.bios_version_str);
8966 kfree(thinkpad_id.ec_version_str);
8967 kfree(thinkpad_id.model_str);
8968 kfree(thinkpad_id.nummodel_str);
8972 static int __init thinkpad_acpi_module_init(void)
8976 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8978 /* Parameter checking */
8979 if (hotkey_report_mode > 2)
8982 /* Driver-level probe */
8984 ret = get_thinkpad_model_data(&thinkpad_id);
8986 pr_err("unable to get DMI data: %d\n", ret);
8987 thinkpad_acpi_module_exit();
8990 ret = probe_for_thinkpad();
8992 thinkpad_acpi_module_exit();
8996 /* Driver initialization */
8998 thinkpad_acpi_init_banner();
8999 tpacpi_check_outdated_fw();
9001 TPACPI_ACPIHANDLE_INIT(ecrd);
9002 TPACPI_ACPIHANDLE_INIT(ecwr);
9004 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9006 thinkpad_acpi_module_exit();
9010 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9012 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9013 thinkpad_acpi_module_exit();
9017 ret = platform_driver_register(&tpacpi_pdriver);
9019 pr_err("unable to register main platform driver\n");
9020 thinkpad_acpi_module_exit();
9023 tp_features.platform_drv_registered = 1;
9025 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9027 pr_err("unable to register hwmon platform driver\n");
9028 thinkpad_acpi_module_exit();
9031 tp_features.sensors_pdrv_registered = 1;
9033 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9035 tp_features.platform_drv_attrs_registered = 1;
9036 ret = tpacpi_create_driver_attributes(
9037 &tpacpi_hwmon_pdriver.driver);
9040 pr_err("unable to create sysfs driver attributes\n");
9041 thinkpad_acpi_module_exit();
9044 tp_features.sensors_pdrv_attrs_registered = 1;
9047 /* Device initialization */
9048 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9050 if (IS_ERR(tpacpi_pdev)) {
9051 ret = PTR_ERR(tpacpi_pdev);
9053 pr_err("unable to register platform device\n");
9054 thinkpad_acpi_module_exit();
9057 tpacpi_sensors_pdev = platform_device_register_simple(
9058 TPACPI_HWMON_DRVR_NAME,
9060 if (IS_ERR(tpacpi_sensors_pdev)) {
9061 ret = PTR_ERR(tpacpi_sensors_pdev);
9062 tpacpi_sensors_pdev = NULL;
9063 pr_err("unable to register hwmon platform device\n");
9064 thinkpad_acpi_module_exit();
9067 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9068 &dev_attr_thinkpad_acpi_pdev_name);
9070 pr_err("unable to create sysfs hwmon device attributes\n");
9071 thinkpad_acpi_module_exit();
9074 tp_features.sensors_pdev_attrs_registered = 1;
9075 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9076 if (IS_ERR(tpacpi_hwmon)) {
9077 ret = PTR_ERR(tpacpi_hwmon);
9078 tpacpi_hwmon = NULL;
9079 pr_err("unable to register hwmon device\n");
9080 thinkpad_acpi_module_exit();
9083 mutex_init(&tpacpi_inputdev_send_mutex);
9084 tpacpi_inputdev = input_allocate_device();
9085 if (!tpacpi_inputdev) {
9086 pr_err("unable to allocate input device\n");
9087 thinkpad_acpi_module_exit();
9090 /* Prepare input device, but don't register */
9091 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9092 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9093 tpacpi_inputdev->id.bustype = BUS_HOST;
9094 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9095 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9096 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9097 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9100 /* Init subdriver dependencies */
9101 tpacpi_detect_brightness_capabilities();
9103 /* Init subdrivers */
9104 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9105 ret = ibm_init(&ibms_init[i]);
9106 if (ret >= 0 && *ibms_init[i].param)
9107 ret = ibms_init[i].data->write(ibms_init[i].param);
9109 thinkpad_acpi_module_exit();
9114 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9116 ret = input_register_device(tpacpi_inputdev);
9118 pr_err("unable to register input device\n");
9119 thinkpad_acpi_module_exit();
9122 tp_features.input_device_registered = 1;
9128 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9131 * This will autoload the driver in almost every ThinkPad
9132 * in widespread use.
9134 * Only _VERY_ old models, like the 240, 240x and 570 lack
9135 * the HKEY event interface.
9137 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9140 * DMI matching for module autoloading
9142 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9143 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9145 * Only models listed in thinkwiki will be supported, so add yours
9146 * if it is not there yet.
9148 #define IBM_BIOS_MODULE_ALIAS(__type) \
9149 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9151 /* Ancient thinkpad BIOSes have to be identified by
9152 * BIOS type or model number, and there are far less
9153 * BIOS types than model numbers... */
9154 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9158 MODULE_DESCRIPTION(TPACPI_DESC);
9159 MODULE_VERSION(TPACPI_VERSION);
9160 MODULE_LICENSE("GPL");
9162 module_init(thinkpad_acpi_module_init);
9163 module_exit(thinkpad_acpi_module_exit);