xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / platform / x86 / thinkpad_acpi.c
blobd5eaf3b1edba91d53723fddd0c2b49125fd64b9d
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.25"
27 #define TPACPI_SYSFS_VERSION 0x030000
30 * Changelog:
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
69 #include <linux/fb.h>
70 #include <linux/platform_device.h>
71 #include <linux/hwmon.h>
72 #include <linux/hwmon-sysfs.h>
73 #include <linux/input.h>
74 #include <linux/leds.h>
75 #include <linux/rfkill.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
79 #include <linux/acpi.h>
80 #include <linux/pci_ids.h>
81 #include <linux/thinkpad_acpi.h>
82 #include <sound/core.h>
83 #include <sound/control.h>
84 #include <sound/initval.h>
85 #include <linux/uaccess.h>
86 #include <acpi/video.h>
88 /* ThinkPad CMOS commands */
89 #define TP_CMOS_VOLUME_DOWN 0
90 #define TP_CMOS_VOLUME_UP 1
91 #define TP_CMOS_VOLUME_MUTE 2
92 #define TP_CMOS_BRIGHTNESS_UP 4
93 #define TP_CMOS_BRIGHTNESS_DOWN 5
94 #define TP_CMOS_THINKLIGHT_ON 12
95 #define TP_CMOS_THINKLIGHT_OFF 13
97 /* NVRAM Addresses */
98 enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
106 /* NVRAM bit masks */
107 enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
123 /* Misc NVRAM-related */
124 enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
128 /* ACPI HIDs */
129 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
217 /* Further user-interface events */
218 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
219 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
221 /* Misc */
222 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
225 /****************************************************************************
226 * Main driver
229 #define TPACPI_NAME "thinkpad"
230 #define TPACPI_DESC "ThinkPad ACPI Extras"
231 #define TPACPI_FILE TPACPI_NAME "_acpi"
232 #define TPACPI_URL "http://ibm-acpi.sf.net/"
233 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
235 #define TPACPI_PROC_DIR "ibm"
236 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
237 #define TPACPI_DRVR_NAME TPACPI_FILE
238 #define TPACPI_DRVR_SHORTNAME "tpacpi"
239 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
241 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
242 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
244 #define TPACPI_MAX_ACPI_ARGS 3
246 /* Debugging printk groups */
247 #define TPACPI_DBG_ALL 0xffff
248 #define TPACPI_DBG_DISCLOSETASK 0x8000
249 #define TPACPI_DBG_INIT 0x0001
250 #define TPACPI_DBG_EXIT 0x0002
251 #define TPACPI_DBG_RFKILL 0x0004
252 #define TPACPI_DBG_HKEY 0x0008
253 #define TPACPI_DBG_FAN 0x0010
254 #define TPACPI_DBG_BRGHT 0x0020
255 #define TPACPI_DBG_MIXER 0x0040
257 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
258 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
259 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
262 /****************************************************************************
263 * Driver-wide structs and misc. variables
266 struct ibm_struct;
268 struct tp_acpi_drv_struct {
269 const struct acpi_device_id *hid;
270 struct acpi_driver *driver;
272 void (*notify) (struct ibm_struct *, u32);
273 acpi_handle *handle;
274 u32 type;
275 struct acpi_device *device;
278 struct ibm_struct {
279 char *name;
281 int (*read) (struct seq_file *);
282 int (*write) (char *);
283 void (*exit) (void);
284 void (*resume) (void);
285 void (*suspend) (void);
286 void (*shutdown) (void);
288 struct list_head all_drivers;
290 struct tp_acpi_drv_struct *acpi;
292 struct {
293 u8 acpi_driver_registered:1;
294 u8 acpi_notify_installed:1;
295 u8 proc_created:1;
296 u8 init_called:1;
297 u8 experimental:1;
298 } flags;
301 struct ibm_init_struct {
302 char param[32];
304 int (*init) (struct ibm_init_struct *);
305 umode_t base_procfs_mode;
306 struct ibm_struct *data;
309 static struct {
310 u32 bluetooth:1;
311 u32 hotkey:1;
312 u32 hotkey_mask:1;
313 u32 hotkey_wlsw:1;
314 enum {
315 TP_HOTKEY_TABLET_NONE = 0,
316 TP_HOTKEY_TABLET_USES_MHKG,
317 TP_HOTKEY_TABLET_USES_GMMS,
318 } hotkey_tablet;
319 u32 kbdlight:1;
320 u32 light:1;
321 u32 light_status:1;
322 u32 bright_acpimode:1;
323 u32 bright_unkfw:1;
324 u32 wan:1;
325 u32 uwb:1;
326 u32 fan_ctrl_status_undef:1;
327 u32 second_fan:1;
328 u32 beep_needs_two_args:1;
329 u32 mixer_no_level_control:1;
330 u32 input_device_registered:1;
331 u32 platform_drv_registered:1;
332 u32 platform_drv_attrs_registered:1;
333 u32 sensors_pdrv_registered:1;
334 u32 sensors_pdrv_attrs_registered:1;
335 u32 sensors_pdev_attrs_registered:1;
336 u32 hotkey_poll_active:1;
337 u32 has_adaptive_kbd:1;
338 } tp_features;
340 static struct {
341 u16 hotkey_mask_ff:1;
342 u16 volume_ctrl_forbidden:1;
343 } tp_warned;
345 struct thinkpad_id_data {
346 unsigned int vendor; /* ThinkPad vendor:
347 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
349 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
350 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
352 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
353 u16 ec_model;
354 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
355 u16 ec_release;
357 char *model_str; /* ThinkPad T43 */
358 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
360 static struct thinkpad_id_data thinkpad_id;
362 static enum {
363 TPACPI_LIFE_INIT = 0,
364 TPACPI_LIFE_RUNNING,
365 TPACPI_LIFE_EXITING,
366 } tpacpi_lifecycle;
368 static int experimental;
369 static u32 dbg_level;
371 static struct workqueue_struct *tpacpi_wq;
373 enum led_status_t {
374 TPACPI_LED_OFF = 0,
375 TPACPI_LED_ON,
376 TPACPI_LED_BLINK,
379 /* tpacpi LED class */
380 struct tpacpi_led_classdev {
381 struct led_classdev led_classdev;
382 int led;
385 /* brightness level capabilities */
386 static unsigned int bright_maxlvl; /* 0 = unknown */
388 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
389 static int dbg_wlswemul;
390 static bool tpacpi_wlsw_emulstate;
391 static int dbg_bluetoothemul;
392 static bool tpacpi_bluetooth_emulstate;
393 static int dbg_wwanemul;
394 static bool tpacpi_wwan_emulstate;
395 static int dbg_uwbemul;
396 static bool tpacpi_uwb_emulstate;
397 #endif
400 /*************************************************************************
401 * Debugging helpers
404 #define dbg_printk(a_dbg_level, format, arg...) \
405 do { \
406 if (dbg_level & (a_dbg_level)) \
407 printk(KERN_DEBUG pr_fmt("%s: " format), \
408 __func__, ##arg); \
409 } while (0)
411 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
412 #define vdbg_printk dbg_printk
413 static const char *str_supported(int is_supported);
414 #else
415 static inline const char *str_supported(int is_supported) { return ""; }
416 #define vdbg_printk(a_dbg_level, format, arg...) \
417 do { if (0) no_printk(format, ##arg); } while (0)
418 #endif
420 static void tpacpi_log_usertask(const char * const what)
422 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
423 what, task_tgid_vnr(current));
426 #define tpacpi_disclose_usertask(what, format, arg...) \
427 do { \
428 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
429 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
430 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
431 what, task_tgid_vnr(current), ## arg); \
433 } while (0)
436 * Quirk handling helpers
438 * ThinkPad IDs and versions seen in the field so far
439 * are two-characters from the set [0-9A-Z], i.e. base 36.
441 * We use values well outside that range as specials.
444 #define TPACPI_MATCH_ANY 0xffffU
445 #define TPACPI_MATCH_UNKNOWN 0U
447 /* TPID('1', 'Y') == 0x5931 */
448 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
450 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
451 { .vendor = PCI_VENDOR_ID_IBM, \
452 .bios = TPID(__id1, __id2), \
453 .ec = TPACPI_MATCH_ANY, \
454 .quirks = (__quirk) }
456 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
457 { .vendor = PCI_VENDOR_ID_LENOVO, \
458 .bios = TPID(__id1, __id2), \
459 .ec = TPACPI_MATCH_ANY, \
460 .quirks = (__quirk) }
462 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_LENOVO, \
464 .bios = TPACPI_MATCH_ANY, \
465 .ec = TPID(__id1, __id2), \
466 .quirks = (__quirk) }
468 struct tpacpi_quirk {
469 unsigned int vendor;
470 u16 bios;
471 u16 ec;
472 unsigned long quirks;
476 * tpacpi_check_quirks() - search BIOS/EC version on a list
477 * @qlist: array of &struct tpacpi_quirk
478 * @qlist_size: number of elements in @qlist
480 * Iterates over a quirks list until one is found that matches the
481 * ThinkPad's vendor, BIOS and EC model.
483 * Returns 0 if nothing matches, otherwise returns the quirks field of
484 * the matching &struct tpacpi_quirk entry.
486 * The match criteria is: vendor, ec and bios much match.
488 static unsigned long __init tpacpi_check_quirks(
489 const struct tpacpi_quirk *qlist,
490 unsigned int qlist_size)
492 while (qlist_size) {
493 if ((qlist->vendor == thinkpad_id.vendor ||
494 qlist->vendor == TPACPI_MATCH_ANY) &&
495 (qlist->bios == thinkpad_id.bios_model ||
496 qlist->bios == TPACPI_MATCH_ANY) &&
497 (qlist->ec == thinkpad_id.ec_model ||
498 qlist->ec == TPACPI_MATCH_ANY))
499 return qlist->quirks;
501 qlist_size--;
502 qlist++;
504 return 0;
507 static inline bool __pure __init tpacpi_is_lenovo(void)
509 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
512 static inline bool __pure __init tpacpi_is_ibm(void)
514 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
517 /****************************************************************************
518 ****************************************************************************
520 * ACPI Helpers and device model
522 ****************************************************************************
523 ****************************************************************************/
525 /*************************************************************************
526 * ACPI basic handles
529 static acpi_handle root_handle;
530 static acpi_handle ec_handle;
532 #define TPACPI_HANDLE(object, parent, paths...) \
533 static acpi_handle object##_handle; \
534 static const acpi_handle * const object##_parent __initconst = \
535 &parent##_handle; \
536 static char *object##_paths[] __initdata = { paths }
538 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
539 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
541 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
542 /* T4x, X31, X40 */
543 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
544 "\\CMS", /* R40, R40e */
545 ); /* all others */
547 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
548 "^HKEY", /* R30, R31 */
549 "HKEY", /* all others */
550 ); /* 570 */
552 /*************************************************************************
553 * ACPI helpers
556 static int acpi_evalf(acpi_handle handle,
557 int *res, char *method, char *fmt, ...)
559 char *fmt0 = fmt;
560 struct acpi_object_list params;
561 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
562 struct acpi_buffer result, *resultp;
563 union acpi_object out_obj;
564 acpi_status status;
565 va_list ap;
566 char res_type;
567 int success;
568 int quiet;
570 if (!*fmt) {
571 pr_err("acpi_evalf() called with empty format\n");
572 return 0;
575 if (*fmt == 'q') {
576 quiet = 1;
577 fmt++;
578 } else
579 quiet = 0;
581 res_type = *(fmt++);
583 params.count = 0;
584 params.pointer = &in_objs[0];
586 va_start(ap, fmt);
587 while (*fmt) {
588 char c = *(fmt++);
589 switch (c) {
590 case 'd': /* int */
591 in_objs[params.count].integer.value = va_arg(ap, int);
592 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
593 break;
594 /* add more types as needed */
595 default:
596 pr_err("acpi_evalf() called with invalid format character '%c'\n",
598 va_end(ap);
599 return 0;
602 va_end(ap);
604 if (res_type != 'v') {
605 result.length = sizeof(out_obj);
606 result.pointer = &out_obj;
607 resultp = &result;
608 } else
609 resultp = NULL;
611 status = acpi_evaluate_object(handle, method, &params, resultp);
613 switch (res_type) {
614 case 'd': /* int */
615 success = (status == AE_OK &&
616 out_obj.type == ACPI_TYPE_INTEGER);
617 if (success && res)
618 *res = out_obj.integer.value;
619 break;
620 case 'v': /* void */
621 success = status == AE_OK;
622 break;
623 /* add more types as needed */
624 default:
625 pr_err("acpi_evalf() called with invalid format character '%c'\n",
626 res_type);
627 return 0;
630 if (!success && !quiet)
631 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
632 method, fmt0, acpi_format_exception(status));
634 return success;
637 static int acpi_ec_read(int i, u8 *p)
639 int v;
641 if (ecrd_handle) {
642 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
643 return 0;
644 *p = v;
645 } else {
646 if (ec_read(i, p) < 0)
647 return 0;
650 return 1;
653 static int acpi_ec_write(int i, u8 v)
655 if (ecwr_handle) {
656 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
657 return 0;
658 } else {
659 if (ec_write(i, v) < 0)
660 return 0;
663 return 1;
666 static int issue_thinkpad_cmos_command(int cmos_cmd)
668 if (!cmos_handle)
669 return -ENXIO;
671 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
672 return -EIO;
674 return 0;
677 /*************************************************************************
678 * ACPI device model
681 #define TPACPI_ACPIHANDLE_INIT(object) \
682 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
683 object##_paths, ARRAY_SIZE(object##_paths))
685 static void __init drv_acpi_handle_init(const char *name,
686 acpi_handle *handle, const acpi_handle parent,
687 char **paths, const int num_paths)
689 int i;
690 acpi_status status;
692 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
693 name);
695 for (i = 0; i < num_paths; i++) {
696 status = acpi_get_handle(parent, paths[i], handle);
697 if (ACPI_SUCCESS(status)) {
698 dbg_printk(TPACPI_DBG_INIT,
699 "Found ACPI handle %s for %s\n",
700 paths[i], name);
701 return;
705 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
706 name);
707 *handle = NULL;
710 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
711 u32 level, void *context, void **return_value)
713 struct acpi_device *dev;
714 if (!strcmp(context, "video")) {
715 if (acpi_bus_get_device(handle, &dev))
716 return AE_OK;
717 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
718 return AE_OK;
721 *(acpi_handle *)return_value = handle;
723 return AE_CTRL_TERMINATE;
726 static void __init tpacpi_acpi_handle_locate(const char *name,
727 const char *hid,
728 acpi_handle *handle)
730 acpi_status status;
731 acpi_handle device_found;
733 BUG_ON(!name || !handle);
734 vdbg_printk(TPACPI_DBG_INIT,
735 "trying to locate ACPI handle for %s, using HID %s\n",
736 name, hid ? hid : "NULL");
738 memset(&device_found, 0, sizeof(device_found));
739 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
740 (void *)name, &device_found);
742 *handle = NULL;
744 if (ACPI_SUCCESS(status)) {
745 *handle = device_found;
746 dbg_printk(TPACPI_DBG_INIT,
747 "Found ACPI handle for %s\n", name);
748 } else {
749 vdbg_printk(TPACPI_DBG_INIT,
750 "Could not locate an ACPI handle for %s: %s\n",
751 name, acpi_format_exception(status));
755 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
757 struct ibm_struct *ibm = data;
759 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
760 return;
762 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
763 return;
765 ibm->acpi->notify(ibm, event);
768 static int __init setup_acpi_notify(struct ibm_struct *ibm)
770 acpi_status status;
771 int rc;
773 BUG_ON(!ibm->acpi);
775 if (!*ibm->acpi->handle)
776 return 0;
778 vdbg_printk(TPACPI_DBG_INIT,
779 "setting up ACPI notify for %s\n", ibm->name);
781 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
782 if (rc < 0) {
783 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
784 return -ENODEV;
787 ibm->acpi->device->driver_data = ibm;
788 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
789 TPACPI_ACPI_EVENT_PREFIX,
790 ibm->name);
792 status = acpi_install_notify_handler(*ibm->acpi->handle,
793 ibm->acpi->type, dispatch_acpi_notify, ibm);
794 if (ACPI_FAILURE(status)) {
795 if (status == AE_ALREADY_EXISTS) {
796 pr_notice("another device driver is already handling %s events\n",
797 ibm->name);
798 } else {
799 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
800 ibm->name, acpi_format_exception(status));
802 return -ENODEV;
804 ibm->flags.acpi_notify_installed = 1;
805 return 0;
808 static int __init tpacpi_device_add(struct acpi_device *device)
810 return 0;
813 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
815 int rc;
817 dbg_printk(TPACPI_DBG_INIT,
818 "registering %s as an ACPI driver\n", ibm->name);
820 BUG_ON(!ibm->acpi);
822 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
823 if (!ibm->acpi->driver) {
824 pr_err("failed to allocate memory for ibm->acpi->driver\n");
825 return -ENOMEM;
828 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
829 ibm->acpi->driver->ids = ibm->acpi->hid;
831 ibm->acpi->driver->ops.add = &tpacpi_device_add;
833 rc = acpi_bus_register_driver(ibm->acpi->driver);
834 if (rc < 0) {
835 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
836 ibm->name, rc);
837 kfree(ibm->acpi->driver);
838 ibm->acpi->driver = NULL;
839 } else if (!rc)
840 ibm->flags.acpi_driver_registered = 1;
842 return rc;
846 /****************************************************************************
847 ****************************************************************************
849 * Procfs Helpers
851 ****************************************************************************
852 ****************************************************************************/
854 static int dispatch_proc_show(struct seq_file *m, void *v)
856 struct ibm_struct *ibm = m->private;
858 if (!ibm || !ibm->read)
859 return -EINVAL;
860 return ibm->read(m);
863 static int dispatch_proc_open(struct inode *inode, struct file *file)
865 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
868 static ssize_t dispatch_proc_write(struct file *file,
869 const char __user *userbuf,
870 size_t count, loff_t *pos)
872 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
873 char *kernbuf;
874 int ret;
876 if (!ibm || !ibm->write)
877 return -EINVAL;
878 if (count > PAGE_SIZE - 2)
879 return -EINVAL;
881 kernbuf = kmalloc(count + 2, GFP_KERNEL);
882 if (!kernbuf)
883 return -ENOMEM;
885 if (copy_from_user(kernbuf, userbuf, count)) {
886 kfree(kernbuf);
887 return -EFAULT;
890 kernbuf[count] = 0;
891 strcat(kernbuf, ",");
892 ret = ibm->write(kernbuf);
893 if (ret == 0)
894 ret = count;
896 kfree(kernbuf);
898 return ret;
901 static const struct file_operations dispatch_proc_fops = {
902 .owner = THIS_MODULE,
903 .open = dispatch_proc_open,
904 .read = seq_read,
905 .llseek = seq_lseek,
906 .release = single_release,
907 .write = dispatch_proc_write,
910 static char *next_cmd(char **cmds)
912 char *start = *cmds;
913 char *end;
915 while ((end = strchr(start, ',')) && end == start)
916 start = end + 1;
918 if (!end)
919 return NULL;
921 *end = 0;
922 *cmds = end + 1;
923 return start;
927 /****************************************************************************
928 ****************************************************************************
930 * Device model: input, hwmon and platform
932 ****************************************************************************
933 ****************************************************************************/
935 static struct platform_device *tpacpi_pdev;
936 static struct platform_device *tpacpi_sensors_pdev;
937 static struct device *tpacpi_hwmon;
938 static struct input_dev *tpacpi_inputdev;
939 static struct mutex tpacpi_inputdev_send_mutex;
940 static LIST_HEAD(tpacpi_all_drivers);
942 #ifdef CONFIG_PM_SLEEP
943 static int tpacpi_suspend_handler(struct device *dev)
945 struct ibm_struct *ibm, *itmp;
947 list_for_each_entry_safe(ibm, itmp,
948 &tpacpi_all_drivers,
949 all_drivers) {
950 if (ibm->suspend)
951 (ibm->suspend)();
954 return 0;
957 static int tpacpi_resume_handler(struct device *dev)
959 struct ibm_struct *ibm, *itmp;
961 list_for_each_entry_safe(ibm, itmp,
962 &tpacpi_all_drivers,
963 all_drivers) {
964 if (ibm->resume)
965 (ibm->resume)();
968 return 0;
970 #endif
972 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
973 tpacpi_suspend_handler, tpacpi_resume_handler);
975 static void tpacpi_shutdown_handler(struct platform_device *pdev)
977 struct ibm_struct *ibm, *itmp;
979 list_for_each_entry_safe(ibm, itmp,
980 &tpacpi_all_drivers,
981 all_drivers) {
982 if (ibm->shutdown)
983 (ibm->shutdown)();
987 static struct platform_driver tpacpi_pdriver = {
988 .driver = {
989 .name = TPACPI_DRVR_NAME,
990 .pm = &tpacpi_pm,
992 .shutdown = tpacpi_shutdown_handler,
995 static struct platform_driver tpacpi_hwmon_pdriver = {
996 .driver = {
997 .name = TPACPI_HWMON_DRVR_NAME,
1001 /*************************************************************************
1002 * sysfs support helpers
1005 struct attribute_set {
1006 unsigned int members, max_members;
1007 struct attribute_group group;
1010 struct attribute_set_obj {
1011 struct attribute_set s;
1012 struct attribute *a;
1013 } __attribute__((packed));
1015 static struct attribute_set *create_attr_set(unsigned int max_members,
1016 const char *name)
1018 struct attribute_set_obj *sobj;
1020 if (max_members == 0)
1021 return NULL;
1023 /* Allocates space for implicit NULL at the end too */
1024 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1025 max_members * sizeof(struct attribute *),
1026 GFP_KERNEL);
1027 if (!sobj)
1028 return NULL;
1029 sobj->s.max_members = max_members;
1030 sobj->s.group.attrs = &sobj->a;
1031 sobj->s.group.name = name;
1033 return &sobj->s;
1036 #define destroy_attr_set(_set) \
1037 kfree(_set);
1039 /* not multi-threaded safe, use it in a single thread per set */
1040 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1042 if (!s || !attr)
1043 return -EINVAL;
1045 if (s->members >= s->max_members)
1046 return -ENOMEM;
1048 s->group.attrs[s->members] = attr;
1049 s->members++;
1051 return 0;
1054 static int add_many_to_attr_set(struct attribute_set *s,
1055 struct attribute **attr,
1056 unsigned int count)
1058 int i, res;
1060 for (i = 0; i < count; i++) {
1061 res = add_to_attr_set(s, attr[i]);
1062 if (res)
1063 return res;
1066 return 0;
1069 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1071 sysfs_remove_group(kobj, &s->group);
1072 destroy_attr_set(s);
1075 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1076 sysfs_create_group(_kobj, &_attr_set->group)
1078 static int parse_strtoul(const char *buf,
1079 unsigned long max, unsigned long *value)
1081 char *endp;
1083 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1084 endp = skip_spaces(endp);
1085 if (*endp || *value > max)
1086 return -EINVAL;
1088 return 0;
1091 static void tpacpi_disable_brightness_delay(void)
1093 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1094 pr_notice("ACPI backlight control delay disabled\n");
1097 static void printk_deprecated_attribute(const char * const what,
1098 const char * const details)
1100 tpacpi_log_usertask("deprecated sysfs attribute");
1101 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1102 what, details);
1105 /*************************************************************************
1106 * rfkill and radio control support helpers
1110 * ThinkPad-ACPI firmware handling model:
1112 * WLSW (master wireless switch) is event-driven, and is common to all
1113 * firmware-controlled radios. It cannot be controlled, just monitored,
1114 * as expected. It overrides all radio state in firmware
1116 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1117 * (TODO: verify how WLSW interacts with the returned radio state).
1119 * The only time there are shadow radio state changes, is when
1120 * masked-off hotkeys are used.
1124 * Internal driver API for radio state:
1126 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1127 * bool: true means radio blocked (off)
1129 enum tpacpi_rfkill_state {
1130 TPACPI_RFK_RADIO_OFF = 0,
1131 TPACPI_RFK_RADIO_ON
1134 /* rfkill switches */
1135 enum tpacpi_rfk_id {
1136 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1137 TPACPI_RFK_WWAN_SW_ID,
1138 TPACPI_RFK_UWB_SW_ID,
1139 TPACPI_RFK_SW_MAX
1142 static const char *tpacpi_rfkill_names[] = {
1143 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1144 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1145 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1146 [TPACPI_RFK_SW_MAX] = NULL
1149 /* ThinkPad-ACPI rfkill subdriver */
1150 struct tpacpi_rfk {
1151 struct rfkill *rfkill;
1152 enum tpacpi_rfk_id id;
1153 const struct tpacpi_rfk_ops *ops;
1156 struct tpacpi_rfk_ops {
1157 /* firmware interface */
1158 int (*get_status)(void);
1159 int (*set_status)(const enum tpacpi_rfkill_state);
1162 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1164 /* Query FW and update rfkill sw state for a given rfkill switch */
1165 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1167 int status;
1169 if (!tp_rfk)
1170 return -ENODEV;
1172 status = (tp_rfk->ops->get_status)();
1173 if (status < 0)
1174 return status;
1176 rfkill_set_sw_state(tp_rfk->rfkill,
1177 (status == TPACPI_RFK_RADIO_OFF));
1179 return status;
1182 /* Query FW and update rfkill sw state for all rfkill switches */
1183 static void tpacpi_rfk_update_swstate_all(void)
1185 unsigned int i;
1187 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1188 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1192 * Sync the HW-blocking state of all rfkill switches,
1193 * do notice it causes the rfkill core to schedule uevents
1195 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1197 unsigned int i;
1198 struct tpacpi_rfk *tp_rfk;
1200 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1201 tp_rfk = tpacpi_rfkill_switches[i];
1202 if (tp_rfk) {
1203 if (rfkill_set_hw_state(tp_rfk->rfkill,
1204 blocked)) {
1205 /* ignore -- we track sw block */
1211 /* Call to get the WLSW state from the firmware */
1212 static int hotkey_get_wlsw(void);
1214 /* Call to query WLSW state and update all rfkill switches */
1215 static bool tpacpi_rfk_check_hwblock_state(void)
1217 int res = hotkey_get_wlsw();
1218 int hw_blocked;
1220 /* When unknown or unsupported, we have to assume it is unblocked */
1221 if (res < 0)
1222 return false;
1224 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1225 tpacpi_rfk_update_hwblock_state(hw_blocked);
1227 return hw_blocked;
1230 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1232 struct tpacpi_rfk *tp_rfk = data;
1233 int res;
1235 dbg_printk(TPACPI_DBG_RFKILL,
1236 "request to change radio state to %s\n",
1237 blocked ? "blocked" : "unblocked");
1239 /* try to set radio state */
1240 res = (tp_rfk->ops->set_status)(blocked ?
1241 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1243 /* and update the rfkill core with whatever the FW really did */
1244 tpacpi_rfk_update_swstate(tp_rfk);
1246 return (res < 0) ? res : 0;
1249 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1250 .set_block = tpacpi_rfk_hook_set_block,
1253 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1254 const struct tpacpi_rfk_ops *tp_rfkops,
1255 const enum rfkill_type rfktype,
1256 const char *name,
1257 const bool set_default)
1259 struct tpacpi_rfk *atp_rfk;
1260 int res;
1261 bool sw_state = false;
1262 bool hw_state;
1263 int sw_status;
1265 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1267 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1268 if (atp_rfk)
1269 atp_rfk->rfkill = rfkill_alloc(name,
1270 &tpacpi_pdev->dev,
1271 rfktype,
1272 &tpacpi_rfk_rfkill_ops,
1273 atp_rfk);
1274 if (!atp_rfk || !atp_rfk->rfkill) {
1275 pr_err("failed to allocate memory for rfkill class\n");
1276 kfree(atp_rfk);
1277 return -ENOMEM;
1280 atp_rfk->id = id;
1281 atp_rfk->ops = tp_rfkops;
1283 sw_status = (tp_rfkops->get_status)();
1284 if (sw_status < 0) {
1285 pr_err("failed to read initial state for %s, error %d\n",
1286 name, sw_status);
1287 } else {
1288 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1289 if (set_default) {
1290 /* try to keep the initial state, since we ask the
1291 * firmware to preserve it across S5 in NVRAM */
1292 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1295 hw_state = tpacpi_rfk_check_hwblock_state();
1296 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1298 res = rfkill_register(atp_rfk->rfkill);
1299 if (res < 0) {
1300 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1301 rfkill_destroy(atp_rfk->rfkill);
1302 kfree(atp_rfk);
1303 return res;
1306 tpacpi_rfkill_switches[id] = atp_rfk;
1308 pr_info("rfkill switch %s: radio is %sblocked\n",
1309 name, (sw_state || hw_state) ? "" : "un");
1310 return 0;
1313 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1315 struct tpacpi_rfk *tp_rfk;
1317 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1319 tp_rfk = tpacpi_rfkill_switches[id];
1320 if (tp_rfk) {
1321 rfkill_unregister(tp_rfk->rfkill);
1322 rfkill_destroy(tp_rfk->rfkill);
1323 tpacpi_rfkill_switches[id] = NULL;
1324 kfree(tp_rfk);
1328 static void printk_deprecated_rfkill_attribute(const char * const what)
1330 printk_deprecated_attribute(what,
1331 "Please switch to generic rfkill before year 2010");
1334 /* sysfs <radio> enable ------------------------------------------------ */
1335 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1336 struct device_attribute *attr,
1337 char *buf)
1339 int status;
1341 printk_deprecated_rfkill_attribute(attr->attr.name);
1343 /* This is in the ABI... */
1344 if (tpacpi_rfk_check_hwblock_state()) {
1345 status = TPACPI_RFK_RADIO_OFF;
1346 } else {
1347 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1348 if (status < 0)
1349 return status;
1352 return snprintf(buf, PAGE_SIZE, "%d\n",
1353 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1356 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1357 struct device_attribute *attr,
1358 const char *buf, size_t count)
1360 unsigned long t;
1361 int res;
1363 printk_deprecated_rfkill_attribute(attr->attr.name);
1365 if (parse_strtoul(buf, 1, &t))
1366 return -EINVAL;
1368 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1370 /* This is in the ABI... */
1371 if (tpacpi_rfk_check_hwblock_state() && !!t)
1372 return -EPERM;
1374 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1375 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1376 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1378 return (res < 0) ? res : count;
1381 /* procfs -------------------------------------------------------------- */
1382 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1384 if (id >= TPACPI_RFK_SW_MAX)
1385 seq_printf(m, "status:\t\tnot supported\n");
1386 else {
1387 int status;
1389 /* This is in the ABI... */
1390 if (tpacpi_rfk_check_hwblock_state()) {
1391 status = TPACPI_RFK_RADIO_OFF;
1392 } else {
1393 status = tpacpi_rfk_update_swstate(
1394 tpacpi_rfkill_switches[id]);
1395 if (status < 0)
1396 return status;
1399 seq_printf(m, "status:\t\t%s\n",
1400 (status == TPACPI_RFK_RADIO_ON) ?
1401 "enabled" : "disabled");
1402 seq_printf(m, "commands:\tenable, disable\n");
1405 return 0;
1408 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1410 char *cmd;
1411 int status = -1;
1412 int res = 0;
1414 if (id >= TPACPI_RFK_SW_MAX)
1415 return -ENODEV;
1417 while ((cmd = next_cmd(&buf))) {
1418 if (strlencmp(cmd, "enable") == 0)
1419 status = TPACPI_RFK_RADIO_ON;
1420 else if (strlencmp(cmd, "disable") == 0)
1421 status = TPACPI_RFK_RADIO_OFF;
1422 else
1423 return -EINVAL;
1426 if (status != -1) {
1427 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1428 (status == TPACPI_RFK_RADIO_ON) ?
1429 "enable" : "disable",
1430 tpacpi_rfkill_names[id]);
1431 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1432 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1435 return res;
1438 /*************************************************************************
1439 * thinkpad-acpi driver attributes
1442 /* interface_version --------------------------------------------------- */
1443 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1447 static DRIVER_ATTR_RO(interface_version);
1449 /* debug_level --------------------------------------------------------- */
1450 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1452 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1455 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1456 size_t count)
1458 unsigned long t;
1460 if (parse_strtoul(buf, 0xffff, &t))
1461 return -EINVAL;
1463 dbg_level = t;
1465 return count;
1467 static DRIVER_ATTR_RW(debug_level);
1469 /* version ------------------------------------------------------------- */
1470 static ssize_t version_show(struct device_driver *drv, char *buf)
1472 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1473 TPACPI_DESC, TPACPI_VERSION);
1475 static DRIVER_ATTR_RO(version);
1477 /* --------------------------------------------------------------------- */
1479 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1481 /* wlsw_emulstate ------------------------------------------------------ */
1482 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1484 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1487 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1488 size_t count)
1490 unsigned long t;
1492 if (parse_strtoul(buf, 1, &t))
1493 return -EINVAL;
1495 if (tpacpi_wlsw_emulstate != !!t) {
1496 tpacpi_wlsw_emulstate = !!t;
1497 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1500 return count;
1502 static DRIVER_ATTR_RW(wlsw_emulstate);
1504 /* bluetooth_emulstate ------------------------------------------------- */
1505 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1507 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1510 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1511 const char *buf, size_t count)
1513 unsigned long t;
1515 if (parse_strtoul(buf, 1, &t))
1516 return -EINVAL;
1518 tpacpi_bluetooth_emulstate = !!t;
1520 return count;
1522 static DRIVER_ATTR_RW(bluetooth_emulstate);
1524 /* wwan_emulstate ------------------------------------------------- */
1525 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1527 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1530 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1531 size_t count)
1533 unsigned long t;
1535 if (parse_strtoul(buf, 1, &t))
1536 return -EINVAL;
1538 tpacpi_wwan_emulstate = !!t;
1540 return count;
1542 static DRIVER_ATTR_RW(wwan_emulstate);
1544 /* uwb_emulstate ------------------------------------------------- */
1545 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1550 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1551 size_t count)
1553 unsigned long t;
1555 if (parse_strtoul(buf, 1, &t))
1556 return -EINVAL;
1558 tpacpi_uwb_emulstate = !!t;
1560 return count;
1562 static DRIVER_ATTR_RW(uwb_emulstate);
1563 #endif
1565 /* --------------------------------------------------------------------- */
1567 static struct driver_attribute *tpacpi_driver_attributes[] = {
1568 &driver_attr_debug_level, &driver_attr_version,
1569 &driver_attr_interface_version,
1572 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1574 int i, res;
1576 i = 0;
1577 res = 0;
1578 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1579 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1580 i++;
1583 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1584 if (!res && dbg_wlswemul)
1585 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1586 if (!res && dbg_bluetoothemul)
1587 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1588 if (!res && dbg_wwanemul)
1589 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1590 if (!res && dbg_uwbemul)
1591 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1592 #endif
1594 return res;
1597 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1599 int i;
1601 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1602 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1604 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1605 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1606 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1607 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1608 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1609 #endif
1612 /*************************************************************************
1613 * Firmware Data
1617 * Table of recommended minimum BIOS versions
1619 * Reasons for listing:
1620 * 1. Stable BIOS, listed because the unknown amount of
1621 * bugs and bad ACPI behaviour on older versions
1623 * 2. BIOS or EC fw with known bugs that trigger on Linux
1625 * 3. BIOS with known reduced functionality in older versions
1627 * We recommend the latest BIOS and EC version.
1628 * We only support the latest BIOS and EC fw version as a rule.
1630 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1631 * Information from users in ThinkWiki
1633 * WARNING: we use this table also to detect that the machine is
1634 * a ThinkPad in some cases, so don't remove entries lightly.
1637 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1638 { .vendor = (__v), \
1639 .bios = TPID(__id1, __id2), \
1640 .ec = TPACPI_MATCH_ANY, \
1641 .quirks = TPACPI_MATCH_ANY << 16 \
1642 | (__bv1) << 8 | (__bv2) }
1644 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1645 __eid, __ev1, __ev2) \
1646 { .vendor = (__v), \
1647 .bios = TPID(__bid1, __bid2), \
1648 .ec = __eid, \
1649 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1650 | (__bv1) << 8 | (__bv2) }
1652 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1653 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1655 /* Outdated IBM BIOSes often lack the EC id string */
1656 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1657 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1658 __bv1, __bv2, TPID(__id1, __id2), \
1659 __ev1, __ev2), \
1660 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1661 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1662 __ev1, __ev2)
1664 /* Outdated IBM BIOSes often lack the EC id string */
1665 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1666 __eid1, __eid2, __ev1, __ev2) \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1668 __bv1, __bv2, TPID(__eid1, __eid2), \
1669 __ev1, __ev2), \
1670 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1671 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1672 __ev1, __ev2)
1674 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1675 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1677 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1678 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1679 __bv1, __bv2, TPID(__id1, __id2), \
1680 __ev1, __ev2)
1682 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1683 __eid1, __eid2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1685 __bv1, __bv2, TPID(__eid1, __eid2), \
1686 __ev1, __ev2)
1688 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1689 /* Numeric models ------------------ */
1690 /* FW MODEL BIOS VERS */
1691 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1692 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1693 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1694 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1695 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1696 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1697 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1698 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1699 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1701 /* A-series ------------------------- */
1702 /* FW MODEL BIOS VERS EC VERS */
1703 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1704 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1705 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1706 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1707 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1708 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1709 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1710 TPV_QI0('1', '3', '2', '0'), /* A22m */
1711 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1712 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1713 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1715 /* G-series ------------------------- */
1716 /* FW MODEL BIOS VERS */
1717 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1718 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1720 /* R-series, T-series --------------- */
1721 /* FW MODEL BIOS VERS EC VERS */
1722 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1723 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1724 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1725 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1726 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1727 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1728 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1729 T40/p, T41/p, T42/p (1) */
1730 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1731 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1732 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1733 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1735 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1736 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1737 TPV_QI0('1', '6', '3', '2'), /* T22 */
1738 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1739 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1740 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1742 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1743 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1744 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1746 /* BIOS FW BIOS VERS EC FW EC VERS */
1747 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1748 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1750 /* X-series ------------------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1753 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1754 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1755 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1756 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1757 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1758 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1760 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1761 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1763 /* (0) - older versions lack DMI EC fw string and functionality */
1764 /* (1) - older versions known to lack functionality */
1767 #undef TPV_QL1
1768 #undef TPV_QL0
1769 #undef TPV_QI2
1770 #undef TPV_QI1
1771 #undef TPV_QI0
1772 #undef TPV_Q_X
1773 #undef TPV_Q
1775 static void __init tpacpi_check_outdated_fw(void)
1777 unsigned long fwvers;
1778 u16 ec_version, bios_version;
1780 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1781 ARRAY_SIZE(tpacpi_bios_version_qtable));
1783 if (!fwvers)
1784 return;
1786 bios_version = fwvers & 0xffffU;
1787 ec_version = (fwvers >> 16) & 0xffffU;
1789 /* note that unknown versions are set to 0x0000 and we use that */
1790 if ((bios_version > thinkpad_id.bios_release) ||
1791 (ec_version > thinkpad_id.ec_release &&
1792 ec_version != TPACPI_MATCH_ANY)) {
1794 * The changelogs would let us track down the exact
1795 * reason, but it is just too much of a pain to track
1796 * it. We only list BIOSes that are either really
1797 * broken, or really stable to begin with, so it is
1798 * best if the user upgrades the firmware anyway.
1800 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1801 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1805 static bool __init tpacpi_is_fw_known(void)
1807 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1811 /****************************************************************************
1812 ****************************************************************************
1814 * Subdrivers
1816 ****************************************************************************
1817 ****************************************************************************/
1819 /*************************************************************************
1820 * thinkpad-acpi metadata subdriver
1823 static int thinkpad_acpi_driver_read(struct seq_file *m)
1825 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1826 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1827 return 0;
1830 static struct ibm_struct thinkpad_acpi_driver_data = {
1831 .name = "driver",
1832 .read = thinkpad_acpi_driver_read,
1835 /*************************************************************************
1836 * Hotkey subdriver
1840 * ThinkPad firmware event model
1842 * The ThinkPad firmware has two main event interfaces: normal ACPI
1843 * notifications (which follow the ACPI standard), and a private event
1844 * interface.
1846 * The private event interface also issues events for the hotkeys. As
1847 * the driver gained features, the event handling code ended up being
1848 * built around the hotkey subdriver. This will need to be refactored
1849 * to a more formal event API eventually.
1851 * Some "hotkeys" are actually supposed to be used as event reports,
1852 * such as "brightness has changed", "volume has changed", depending on
1853 * the ThinkPad model and how the firmware is operating.
1855 * Unlike other classes, hotkey-class events have mask/unmask control on
1856 * non-ancient firmware. However, how it behaves changes a lot with the
1857 * firmware model and version.
1860 enum { /* hot key scan codes (derived from ACPI DSDT) */
1861 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1862 TP_ACPI_HOTKEYSCAN_FNF2,
1863 TP_ACPI_HOTKEYSCAN_FNF3,
1864 TP_ACPI_HOTKEYSCAN_FNF4,
1865 TP_ACPI_HOTKEYSCAN_FNF5,
1866 TP_ACPI_HOTKEYSCAN_FNF6,
1867 TP_ACPI_HOTKEYSCAN_FNF7,
1868 TP_ACPI_HOTKEYSCAN_FNF8,
1869 TP_ACPI_HOTKEYSCAN_FNF9,
1870 TP_ACPI_HOTKEYSCAN_FNF10,
1871 TP_ACPI_HOTKEYSCAN_FNF11,
1872 TP_ACPI_HOTKEYSCAN_FNF12,
1873 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1874 TP_ACPI_HOTKEYSCAN_FNINSERT,
1875 TP_ACPI_HOTKEYSCAN_FNDELETE,
1876 TP_ACPI_HOTKEYSCAN_FNHOME,
1877 TP_ACPI_HOTKEYSCAN_FNEND,
1878 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1879 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1880 TP_ACPI_HOTKEYSCAN_FNSPACE,
1881 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1882 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1883 TP_ACPI_HOTKEYSCAN_MUTE,
1884 TP_ACPI_HOTKEYSCAN_THINKPAD,
1885 TP_ACPI_HOTKEYSCAN_UNK1,
1886 TP_ACPI_HOTKEYSCAN_UNK2,
1887 TP_ACPI_HOTKEYSCAN_UNK3,
1888 TP_ACPI_HOTKEYSCAN_UNK4,
1889 TP_ACPI_HOTKEYSCAN_UNK5,
1890 TP_ACPI_HOTKEYSCAN_UNK6,
1891 TP_ACPI_HOTKEYSCAN_UNK7,
1892 TP_ACPI_HOTKEYSCAN_UNK8,
1894 /* Adaptive keyboard keycodes */
1895 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1896 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1897 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1898 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1899 TP_ACPI_HOTKEYSCAN_CLOUD,
1900 TP_ACPI_HOTKEYSCAN_UNK9,
1901 TP_ACPI_HOTKEYSCAN_VOICE,
1902 TP_ACPI_HOTKEYSCAN_UNK10,
1903 TP_ACPI_HOTKEYSCAN_GESTURES,
1904 TP_ACPI_HOTKEYSCAN_UNK11,
1905 TP_ACPI_HOTKEYSCAN_UNK12,
1906 TP_ACPI_HOTKEYSCAN_UNK13,
1907 TP_ACPI_HOTKEYSCAN_CONFIG,
1908 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1909 TP_ACPI_HOTKEYSCAN_RELOAD,
1910 TP_ACPI_HOTKEYSCAN_BACK,
1911 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1912 TP_ACPI_HOTKEYSCAN_MIC_UP,
1913 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1914 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1915 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1917 /* Lenovo extended keymap, starting at 0x1300 */
1918 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1919 /* first new observed key (star, favorites) is 0x1311 */
1920 TP_ACPI_HOTKEYSCAN_STAR = 69,
1921 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1922 TP_ACPI_HOTKEYSCAN_UNK25,
1923 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1924 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1926 /* Hotkey keymap size */
1927 TPACPI_HOTKEY_MAP_LEN
1930 enum { /* Keys/events available through NVRAM polling */
1931 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1932 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1935 enum { /* Positions of some of the keys in hotkey masks */
1936 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1937 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1938 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1939 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1940 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1941 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1942 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1943 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1944 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1945 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1946 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1949 enum { /* NVRAM to ACPI HKEY group map */
1950 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1951 TP_ACPI_HKEY_ZOOM_MASK |
1952 TP_ACPI_HKEY_DISPSWTCH_MASK |
1953 TP_ACPI_HKEY_HIBERNATE_MASK,
1954 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1955 TP_ACPI_HKEY_BRGHTDWN_MASK,
1956 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1957 TP_ACPI_HKEY_VOLDWN_MASK |
1958 TP_ACPI_HKEY_MUTE_MASK,
1961 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1962 struct tp_nvram_state {
1963 u16 thinkpad_toggle:1;
1964 u16 zoom_toggle:1;
1965 u16 display_toggle:1;
1966 u16 thinklight_toggle:1;
1967 u16 hibernate_toggle:1;
1968 u16 displayexp_toggle:1;
1969 u16 display_state:1;
1970 u16 brightness_toggle:1;
1971 u16 volume_toggle:1;
1972 u16 mute:1;
1974 u8 brightness_level;
1975 u8 volume_level;
1978 /* kthread for the hotkey poller */
1979 static struct task_struct *tpacpi_hotkey_task;
1982 * Acquire mutex to write poller control variables as an
1983 * atomic block.
1985 * Increment hotkey_config_change when changing them if you
1986 * want the kthread to forget old state.
1988 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1990 static struct mutex hotkey_thread_data_mutex;
1991 static unsigned int hotkey_config_change;
1994 * hotkey poller control variables
1996 * Must be atomic or readers will also need to acquire mutex
1998 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1999 * should be used only when the changes need to be taken as
2000 * a block, OR when one needs to force the kthread to forget
2001 * old state.
2003 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2004 static unsigned int hotkey_poll_freq = 10; /* Hz */
2006 #define HOTKEY_CONFIG_CRITICAL_START \
2007 do { \
2008 mutex_lock(&hotkey_thread_data_mutex); \
2009 hotkey_config_change++; \
2010 } while (0);
2011 #define HOTKEY_CONFIG_CRITICAL_END \
2012 mutex_unlock(&hotkey_thread_data_mutex);
2014 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2016 #define hotkey_source_mask 0U
2017 #define HOTKEY_CONFIG_CRITICAL_START
2018 #define HOTKEY_CONFIG_CRITICAL_END
2020 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2022 static struct mutex hotkey_mutex;
2024 static enum { /* Reasons for waking up */
2025 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2026 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2027 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2028 } hotkey_wakeup_reason;
2030 static int hotkey_autosleep_ack;
2032 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2033 static u32 hotkey_all_mask; /* all events supported in fw */
2034 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2035 static u32 hotkey_reserved_mask; /* events better left disabled */
2036 static u32 hotkey_driver_mask; /* events needed by the driver */
2037 static u32 hotkey_user_mask; /* events visible to userspace */
2038 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2040 static u16 *hotkey_keycode_map;
2042 static struct attribute_set *hotkey_dev_attributes;
2044 static void tpacpi_driver_event(const unsigned int hkey_event);
2045 static void hotkey_driver_event(const unsigned int scancode);
2046 static void hotkey_poll_setup(const bool may_warn);
2048 /* HKEY.MHKG() return bits */
2049 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2050 enum {
2051 TP_ACPI_MULTI_MODE_INVALID = 0,
2052 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2053 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2054 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2055 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2056 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2057 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2058 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2061 enum {
2062 /* The following modes are considered tablet mode for the purpose of
2063 * reporting the status to userspace. i.e. in all these modes it makes
2064 * sense to disable the laptop input devices such as touchpad and
2065 * keyboard.
2067 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2068 TP_ACPI_MULTI_MODE_STAND |
2069 TP_ACPI_MULTI_MODE_TENT |
2070 TP_ACPI_MULTI_MODE_STAND_TENT,
2073 static int hotkey_get_wlsw(void)
2075 int status;
2077 if (!tp_features.hotkey_wlsw)
2078 return -ENODEV;
2080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2081 if (dbg_wlswemul)
2082 return (tpacpi_wlsw_emulstate) ?
2083 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2084 #endif
2086 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2087 return -EIO;
2089 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2092 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2094 int type = (s >> 16) & 0xffff;
2095 int value = s & 0xffff;
2096 int mode = TP_ACPI_MULTI_MODE_INVALID;
2097 int valid_modes = 0;
2099 if (has_tablet_mode)
2100 *has_tablet_mode = 0;
2102 switch (type) {
2103 case 1:
2104 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2105 TP_ACPI_MULTI_MODE_TABLET |
2106 TP_ACPI_MULTI_MODE_STAND_TENT;
2107 break;
2108 case 2:
2109 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2110 TP_ACPI_MULTI_MODE_FLAT |
2111 TP_ACPI_MULTI_MODE_TABLET |
2112 TP_ACPI_MULTI_MODE_STAND |
2113 TP_ACPI_MULTI_MODE_TENT;
2114 break;
2115 case 3:
2116 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2117 TP_ACPI_MULTI_MODE_FLAT;
2118 break;
2119 case 4:
2120 case 5:
2121 /* In mode 4, FLAT is not specified as a valid mode. However,
2122 * it can be seen at least on the X1 Yoga 2nd Generation.
2124 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2125 TP_ACPI_MULTI_MODE_FLAT |
2126 TP_ACPI_MULTI_MODE_TABLET |
2127 TP_ACPI_MULTI_MODE_STAND |
2128 TP_ACPI_MULTI_MODE_TENT;
2129 break;
2130 default:
2131 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2132 type, value, TPACPI_MAIL);
2133 return 0;
2136 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2137 *has_tablet_mode = 1;
2139 switch (value) {
2140 case 1:
2141 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2142 break;
2143 case 2:
2144 mode = TP_ACPI_MULTI_MODE_FLAT;
2145 break;
2146 case 3:
2147 mode = TP_ACPI_MULTI_MODE_TABLET;
2148 break;
2149 case 4:
2150 if (type == 1)
2151 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2152 else
2153 mode = TP_ACPI_MULTI_MODE_STAND;
2154 break;
2155 case 5:
2156 mode = TP_ACPI_MULTI_MODE_TENT;
2157 break;
2158 default:
2159 if (type == 5 && value == 0xffff) {
2160 pr_warn("Multi mode status is undetected, assuming laptop\n");
2161 return 0;
2165 if (!(mode & valid_modes)) {
2166 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2167 value, type, TPACPI_MAIL);
2168 return 0;
2171 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2174 static int hotkey_get_tablet_mode(int *status)
2176 int s;
2178 switch (tp_features.hotkey_tablet) {
2179 case TP_HOTKEY_TABLET_USES_MHKG:
2180 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2181 return -EIO;
2183 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2184 break;
2185 case TP_HOTKEY_TABLET_USES_GMMS:
2186 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2187 return -EIO;
2189 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2190 break;
2191 default:
2192 break;
2195 return 0;
2199 * Reads current event mask from firmware, and updates
2200 * hotkey_acpi_mask accordingly. Also resets any bits
2201 * from hotkey_user_mask that are unavailable to be
2202 * delivered (shadow requirement of the userspace ABI).
2204 * Call with hotkey_mutex held
2206 static int hotkey_mask_get(void)
2208 if (tp_features.hotkey_mask) {
2209 u32 m = 0;
2211 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2212 return -EIO;
2214 hotkey_acpi_mask = m;
2215 } else {
2216 /* no mask support doesn't mean no event support... */
2217 hotkey_acpi_mask = hotkey_all_mask;
2220 /* sync userspace-visible mask */
2221 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2223 return 0;
2226 static void hotkey_mask_warn_incomplete_mask(void)
2228 /* log only what the user can fix... */
2229 const u32 wantedmask = hotkey_driver_mask &
2230 ~(hotkey_acpi_mask | hotkey_source_mask) &
2231 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2233 if (wantedmask)
2234 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2238 * Set the firmware mask when supported
2240 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2242 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2244 * Call with hotkey_mutex held
2246 static int hotkey_mask_set(u32 mask)
2248 int i;
2249 int rc = 0;
2251 const u32 fwmask = mask & ~hotkey_source_mask;
2253 if (tp_features.hotkey_mask) {
2254 for (i = 0; i < 32; i++) {
2255 if (!acpi_evalf(hkey_handle,
2256 NULL, "MHKM", "vdd", i + 1,
2257 !!(mask & (1 << i)))) {
2258 rc = -EIO;
2259 break;
2265 * We *must* make an inconditional call to hotkey_mask_get to
2266 * refresh hotkey_acpi_mask and update hotkey_user_mask
2268 * Take the opportunity to also log when we cannot _enable_
2269 * a given event.
2271 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2272 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2273 fwmask, hotkey_acpi_mask);
2276 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2277 hotkey_mask_warn_incomplete_mask();
2279 return rc;
2283 * Sets hotkey_user_mask and tries to set the firmware mask
2285 * Call with hotkey_mutex held
2287 static int hotkey_user_mask_set(const u32 mask)
2289 int rc;
2291 /* Give people a chance to notice they are doing something that
2292 * is bound to go boom on their users sooner or later */
2293 if (!tp_warned.hotkey_mask_ff &&
2294 (mask == 0xffff || mask == 0xffffff ||
2295 mask == 0xffffffff)) {
2296 tp_warned.hotkey_mask_ff = 1;
2297 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2298 mask);
2299 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2302 /* Try to enable what the user asked for, plus whatever we need.
2303 * this syncs everything but won't enable bits in hotkey_user_mask */
2304 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2306 /* Enable the available bits in hotkey_user_mask */
2307 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2309 return rc;
2313 * Sets the driver hotkey mask.
2315 * Can be called even if the hotkey subdriver is inactive
2317 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2319 int rc;
2321 /* Do the right thing if hotkey_init has not been called yet */
2322 if (!tp_features.hotkey) {
2323 hotkey_driver_mask = mask;
2324 return 0;
2327 mutex_lock(&hotkey_mutex);
2329 HOTKEY_CONFIG_CRITICAL_START
2330 hotkey_driver_mask = mask;
2331 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2332 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2333 #endif
2334 HOTKEY_CONFIG_CRITICAL_END
2336 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2337 ~hotkey_source_mask);
2338 hotkey_poll_setup(true);
2340 mutex_unlock(&hotkey_mutex);
2342 return rc;
2345 static int hotkey_status_get(int *status)
2347 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2348 return -EIO;
2350 return 0;
2353 static int hotkey_status_set(bool enable)
2355 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2356 return -EIO;
2358 return 0;
2361 static void tpacpi_input_send_tabletsw(void)
2363 int state;
2365 if (tp_features.hotkey_tablet &&
2366 !hotkey_get_tablet_mode(&state)) {
2367 mutex_lock(&tpacpi_inputdev_send_mutex);
2369 input_report_switch(tpacpi_inputdev,
2370 SW_TABLET_MODE, !!state);
2371 input_sync(tpacpi_inputdev);
2373 mutex_unlock(&tpacpi_inputdev_send_mutex);
2377 /* Do NOT call without validating scancode first */
2378 static void tpacpi_input_send_key(const unsigned int scancode)
2380 const unsigned int keycode = hotkey_keycode_map[scancode];
2382 if (keycode != KEY_RESERVED) {
2383 mutex_lock(&tpacpi_inputdev_send_mutex);
2385 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2386 input_report_key(tpacpi_inputdev, keycode, 1);
2387 input_sync(tpacpi_inputdev);
2389 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2390 input_report_key(tpacpi_inputdev, keycode, 0);
2391 input_sync(tpacpi_inputdev);
2393 mutex_unlock(&tpacpi_inputdev_send_mutex);
2397 /* Do NOT call without validating scancode first */
2398 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2400 hotkey_driver_event(scancode);
2401 if (hotkey_user_mask & (1 << scancode))
2402 tpacpi_input_send_key(scancode);
2405 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2406 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2408 /* Do NOT call without validating scancode first */
2409 static void tpacpi_hotkey_send_key(unsigned int scancode)
2411 tpacpi_input_send_key_masked(scancode);
2414 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2416 u8 d;
2418 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2419 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2420 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2421 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2422 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2423 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2425 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2426 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2427 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2429 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2430 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2431 n->displayexp_toggle =
2432 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2434 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2435 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2436 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2437 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2438 n->brightness_toggle =
2439 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2441 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2442 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2443 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2444 >> TP_NVRAM_POS_LEVEL_VOLUME;
2445 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2446 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2450 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2451 do { \
2452 if ((event_mask & (1 << __scancode)) && \
2453 oldn->__member != newn->__member) \
2454 tpacpi_hotkey_send_key(__scancode); \
2455 } while (0)
2457 #define TPACPI_MAY_SEND_KEY(__scancode) \
2458 do { \
2459 if (event_mask & (1 << __scancode)) \
2460 tpacpi_hotkey_send_key(__scancode); \
2461 } while (0)
2463 static void issue_volchange(const unsigned int oldvol,
2464 const unsigned int newvol,
2465 const u32 event_mask)
2467 unsigned int i = oldvol;
2469 while (i > newvol) {
2470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2471 i--;
2473 while (i < newvol) {
2474 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2475 i++;
2479 static void issue_brightnesschange(const unsigned int oldbrt,
2480 const unsigned int newbrt,
2481 const u32 event_mask)
2483 unsigned int i = oldbrt;
2485 while (i > newbrt) {
2486 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2487 i--;
2489 while (i < newbrt) {
2490 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2491 i++;
2495 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2496 struct tp_nvram_state *newn,
2497 const u32 event_mask)
2500 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2501 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2502 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2503 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2505 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2507 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2510 * Handle volume
2512 * This code is supposed to duplicate the IBM firmware behaviour:
2513 * - Pressing MUTE issues mute hotkey message, even when already mute
2514 * - Pressing Volume up/down issues volume up/down hotkey messages,
2515 * even when already at maximum or minimum volume
2516 * - The act of unmuting issues volume up/down notification,
2517 * depending which key was used to unmute
2519 * We are constrained to what the NVRAM can tell us, which is not much
2520 * and certainly not enough if more than one volume hotkey was pressed
2521 * since the last poll cycle.
2523 * Just to make our life interesting, some newer Lenovo ThinkPads have
2524 * bugs in the BIOS and may fail to update volume_toggle properly.
2526 if (newn->mute) {
2527 /* muted */
2528 if (!oldn->mute ||
2529 oldn->volume_toggle != newn->volume_toggle ||
2530 oldn->volume_level != newn->volume_level) {
2531 /* recently muted, or repeated mute keypress, or
2532 * multiple presses ending in mute */
2533 issue_volchange(oldn->volume_level, newn->volume_level,
2534 event_mask);
2535 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2537 } else {
2538 /* unmute */
2539 if (oldn->mute) {
2540 /* recently unmuted, issue 'unmute' keypress */
2541 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2543 if (oldn->volume_level != newn->volume_level) {
2544 issue_volchange(oldn->volume_level, newn->volume_level,
2545 event_mask);
2546 } else if (oldn->volume_toggle != newn->volume_toggle) {
2547 /* repeated vol up/down keypress at end of scale ? */
2548 if (newn->volume_level == 0)
2549 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2550 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2551 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2555 /* handle brightness */
2556 if (oldn->brightness_level != newn->brightness_level) {
2557 issue_brightnesschange(oldn->brightness_level,
2558 newn->brightness_level, event_mask);
2559 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2560 /* repeated key presses that didn't change state */
2561 if (newn->brightness_level == 0)
2562 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2563 else if (newn->brightness_level >= bright_maxlvl
2564 && !tp_features.bright_unkfw)
2565 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2568 #undef TPACPI_COMPARE_KEY
2569 #undef TPACPI_MAY_SEND_KEY
2573 * Polling driver
2575 * We track all events in hotkey_source_mask all the time, since
2576 * most of them are edge-based. We only issue those requested by
2577 * hotkey_user_mask or hotkey_driver_mask, though.
2579 static int hotkey_kthread(void *data)
2581 struct tp_nvram_state s[2];
2582 u32 poll_mask, event_mask;
2583 unsigned int si, so;
2584 unsigned long t;
2585 unsigned int change_detector;
2586 unsigned int poll_freq;
2587 bool was_frozen;
2589 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2590 goto exit;
2592 set_freezable();
2594 so = 0;
2595 si = 1;
2596 t = 0;
2598 /* Initial state for compares */
2599 mutex_lock(&hotkey_thread_data_mutex);
2600 change_detector = hotkey_config_change;
2601 poll_mask = hotkey_source_mask;
2602 event_mask = hotkey_source_mask &
2603 (hotkey_driver_mask | hotkey_user_mask);
2604 poll_freq = hotkey_poll_freq;
2605 mutex_unlock(&hotkey_thread_data_mutex);
2606 hotkey_read_nvram(&s[so], poll_mask);
2608 while (!kthread_should_stop()) {
2609 if (t == 0) {
2610 if (likely(poll_freq))
2611 t = 1000/poll_freq;
2612 else
2613 t = 100; /* should never happen... */
2615 t = msleep_interruptible(t);
2616 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2617 break;
2619 if (t > 0 && !was_frozen)
2620 continue;
2622 mutex_lock(&hotkey_thread_data_mutex);
2623 if (was_frozen || hotkey_config_change != change_detector) {
2624 /* forget old state on thaw or config change */
2625 si = so;
2626 t = 0;
2627 change_detector = hotkey_config_change;
2629 poll_mask = hotkey_source_mask;
2630 event_mask = hotkey_source_mask &
2631 (hotkey_driver_mask | hotkey_user_mask);
2632 poll_freq = hotkey_poll_freq;
2633 mutex_unlock(&hotkey_thread_data_mutex);
2635 if (likely(poll_mask)) {
2636 hotkey_read_nvram(&s[si], poll_mask);
2637 if (likely(si != so)) {
2638 hotkey_compare_and_issue_event(&s[so], &s[si],
2639 event_mask);
2643 so = si;
2644 si ^= 1;
2647 exit:
2648 return 0;
2651 /* call with hotkey_mutex held */
2652 static void hotkey_poll_stop_sync(void)
2654 if (tpacpi_hotkey_task) {
2655 kthread_stop(tpacpi_hotkey_task);
2656 tpacpi_hotkey_task = NULL;
2660 /* call with hotkey_mutex held */
2661 static void hotkey_poll_setup(const bool may_warn)
2663 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2664 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2666 if (hotkey_poll_freq > 0 &&
2667 (poll_driver_mask ||
2668 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2669 if (!tpacpi_hotkey_task) {
2670 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2671 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2672 if (IS_ERR(tpacpi_hotkey_task)) {
2673 tpacpi_hotkey_task = NULL;
2674 pr_err("could not create kernel thread for hotkey polling\n");
2677 } else {
2678 hotkey_poll_stop_sync();
2679 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2680 hotkey_poll_freq == 0) {
2681 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2682 poll_user_mask, poll_driver_mask);
2687 static void hotkey_poll_setup_safe(const bool may_warn)
2689 mutex_lock(&hotkey_mutex);
2690 hotkey_poll_setup(may_warn);
2691 mutex_unlock(&hotkey_mutex);
2694 /* call with hotkey_mutex held */
2695 static void hotkey_poll_set_freq(unsigned int freq)
2697 if (!freq)
2698 hotkey_poll_stop_sync();
2700 hotkey_poll_freq = freq;
2703 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2705 static void hotkey_poll_setup(const bool __unused)
2709 static void hotkey_poll_setup_safe(const bool __unused)
2713 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2715 static int hotkey_inputdev_open(struct input_dev *dev)
2717 switch (tpacpi_lifecycle) {
2718 case TPACPI_LIFE_INIT:
2719 case TPACPI_LIFE_RUNNING:
2720 hotkey_poll_setup_safe(false);
2721 return 0;
2722 case TPACPI_LIFE_EXITING:
2723 return -EBUSY;
2726 /* Should only happen if tpacpi_lifecycle is corrupt */
2727 BUG();
2728 return -EBUSY;
2731 static void hotkey_inputdev_close(struct input_dev *dev)
2733 /* disable hotkey polling when possible */
2734 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2735 !(hotkey_source_mask & hotkey_driver_mask))
2736 hotkey_poll_setup_safe(false);
2739 /* sysfs hotkey enable ------------------------------------------------- */
2740 static ssize_t hotkey_enable_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2744 int res, status;
2746 printk_deprecated_attribute("hotkey_enable",
2747 "Hotkey reporting is always enabled");
2749 res = hotkey_status_get(&status);
2750 if (res)
2751 return res;
2753 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2756 static ssize_t hotkey_enable_store(struct device *dev,
2757 struct device_attribute *attr,
2758 const char *buf, size_t count)
2760 unsigned long t;
2762 printk_deprecated_attribute("hotkey_enable",
2763 "Hotkeys can be disabled through hotkey_mask");
2765 if (parse_strtoul(buf, 1, &t))
2766 return -EINVAL;
2768 if (t == 0)
2769 return -EPERM;
2771 return count;
2774 static DEVICE_ATTR_RW(hotkey_enable);
2776 /* sysfs hotkey mask --------------------------------------------------- */
2777 static ssize_t hotkey_mask_show(struct device *dev,
2778 struct device_attribute *attr,
2779 char *buf)
2781 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2784 static ssize_t hotkey_mask_store(struct device *dev,
2785 struct device_attribute *attr,
2786 const char *buf, size_t count)
2788 unsigned long t;
2789 int res;
2791 if (parse_strtoul(buf, 0xffffffffUL, &t))
2792 return -EINVAL;
2794 if (mutex_lock_killable(&hotkey_mutex))
2795 return -ERESTARTSYS;
2797 res = hotkey_user_mask_set(t);
2799 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2800 hotkey_poll_setup(true);
2801 #endif
2803 mutex_unlock(&hotkey_mutex);
2805 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2807 return (res) ? res : count;
2810 static DEVICE_ATTR_RW(hotkey_mask);
2812 /* sysfs hotkey bios_enabled ------------------------------------------- */
2813 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2814 struct device_attribute *attr,
2815 char *buf)
2817 return sprintf(buf, "0\n");
2820 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2822 /* sysfs hotkey bios_mask ---------------------------------------------- */
2823 static ssize_t hotkey_bios_mask_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2827 printk_deprecated_attribute("hotkey_bios_mask",
2828 "This attribute is useless.");
2829 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2832 static DEVICE_ATTR_RO(hotkey_bios_mask);
2834 /* sysfs hotkey all_mask ----------------------------------------------- */
2835 static ssize_t hotkey_all_mask_show(struct device *dev,
2836 struct device_attribute *attr,
2837 char *buf)
2839 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2840 hotkey_all_mask | hotkey_source_mask);
2843 static DEVICE_ATTR_RO(hotkey_all_mask);
2845 /* sysfs hotkey all_mask ----------------------------------------------- */
2846 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2847 struct device_attribute *attr,
2848 char *buf)
2850 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2851 hotkey_adaptive_all_mask | hotkey_source_mask);
2854 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2856 /* sysfs hotkey recommended_mask --------------------------------------- */
2857 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2858 struct device_attribute *attr,
2859 char *buf)
2861 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2862 (hotkey_all_mask | hotkey_source_mask)
2863 & ~hotkey_reserved_mask);
2866 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2868 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2870 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2871 static ssize_t hotkey_source_mask_show(struct device *dev,
2872 struct device_attribute *attr,
2873 char *buf)
2875 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2878 static ssize_t hotkey_source_mask_store(struct device *dev,
2879 struct device_attribute *attr,
2880 const char *buf, size_t count)
2882 unsigned long t;
2883 u32 r_ev;
2884 int rc;
2886 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2887 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2888 return -EINVAL;
2890 if (mutex_lock_killable(&hotkey_mutex))
2891 return -ERESTARTSYS;
2893 HOTKEY_CONFIG_CRITICAL_START
2894 hotkey_source_mask = t;
2895 HOTKEY_CONFIG_CRITICAL_END
2897 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2898 ~hotkey_source_mask);
2899 hotkey_poll_setup(true);
2901 /* check if events needed by the driver got disabled */
2902 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2903 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2905 mutex_unlock(&hotkey_mutex);
2907 if (rc < 0)
2908 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2910 if (r_ev)
2911 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2912 r_ev);
2914 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2916 return (rc < 0) ? rc : count;
2919 static DEVICE_ATTR_RW(hotkey_source_mask);
2921 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2922 static ssize_t hotkey_poll_freq_show(struct device *dev,
2923 struct device_attribute *attr,
2924 char *buf)
2926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2929 static ssize_t hotkey_poll_freq_store(struct device *dev,
2930 struct device_attribute *attr,
2931 const char *buf, size_t count)
2933 unsigned long t;
2935 if (parse_strtoul(buf, 25, &t))
2936 return -EINVAL;
2938 if (mutex_lock_killable(&hotkey_mutex))
2939 return -ERESTARTSYS;
2941 hotkey_poll_set_freq(t);
2942 hotkey_poll_setup(true);
2944 mutex_unlock(&hotkey_mutex);
2946 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2948 return count;
2951 static DEVICE_ATTR_RW(hotkey_poll_freq);
2953 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2955 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2956 static ssize_t hotkey_radio_sw_show(struct device *dev,
2957 struct device_attribute *attr,
2958 char *buf)
2960 int res;
2961 res = hotkey_get_wlsw();
2962 if (res < 0)
2963 return res;
2965 /* Opportunistic update */
2966 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2968 return snprintf(buf, PAGE_SIZE, "%d\n",
2969 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2972 static DEVICE_ATTR_RO(hotkey_radio_sw);
2974 static void hotkey_radio_sw_notify_change(void)
2976 if (tp_features.hotkey_wlsw)
2977 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2978 "hotkey_radio_sw");
2981 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2982 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2983 struct device_attribute *attr,
2984 char *buf)
2986 int res, s;
2987 res = hotkey_get_tablet_mode(&s);
2988 if (res < 0)
2989 return res;
2991 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2994 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2996 static void hotkey_tablet_mode_notify_change(void)
2998 if (tp_features.hotkey_tablet)
2999 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3000 "hotkey_tablet_mode");
3003 /* sysfs wakeup reason (pollable) -------------------------------------- */
3004 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3005 struct device_attribute *attr,
3006 char *buf)
3008 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3011 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3013 static void hotkey_wakeup_reason_notify_change(void)
3015 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3016 "wakeup_reason");
3019 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3020 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3021 struct device_attribute *attr,
3022 char *buf)
3024 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3027 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3028 hotkey_wakeup_hotunplug_complete_show, NULL);
3030 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3032 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3033 "wakeup_hotunplug_complete");
3036 /* sysfs adaptive kbd mode --------------------------------------------- */
3038 static int adaptive_keyboard_get_mode(void);
3039 static int adaptive_keyboard_set_mode(int new_mode);
3041 enum ADAPTIVE_KEY_MODE {
3042 HOME_MODE,
3043 WEB_BROWSER_MODE,
3044 WEB_CONFERENCE_MODE,
3045 FUNCTION_MODE,
3046 LAYFLAT_MODE
3049 static ssize_t adaptive_kbd_mode_show(struct device *dev,
3050 struct device_attribute *attr,
3051 char *buf)
3053 int current_mode;
3055 current_mode = adaptive_keyboard_get_mode();
3056 if (current_mode < 0)
3057 return current_mode;
3059 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3062 static ssize_t adaptive_kbd_mode_store(struct device *dev,
3063 struct device_attribute *attr,
3064 const char *buf, size_t count)
3066 unsigned long t;
3067 int res;
3069 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3070 return -EINVAL;
3072 res = adaptive_keyboard_set_mode(t);
3073 return (res < 0) ? res : count;
3076 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3078 static struct attribute *adaptive_kbd_attributes[] = {
3079 &dev_attr_adaptive_kbd_mode.attr,
3080 NULL
3083 static const struct attribute_group adaptive_kbd_attr_group = {
3084 .attrs = adaptive_kbd_attributes,
3087 /* --------------------------------------------------------------------- */
3089 static struct attribute *hotkey_attributes[] __initdata = {
3090 &dev_attr_hotkey_enable.attr,
3091 &dev_attr_hotkey_bios_enabled.attr,
3092 &dev_attr_hotkey_bios_mask.attr,
3093 &dev_attr_wakeup_reason.attr,
3094 &dev_attr_wakeup_hotunplug_complete.attr,
3095 &dev_attr_hotkey_mask.attr,
3096 &dev_attr_hotkey_all_mask.attr,
3097 &dev_attr_hotkey_adaptive_all_mask.attr,
3098 &dev_attr_hotkey_recommended_mask.attr,
3099 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3100 &dev_attr_hotkey_source_mask.attr,
3101 &dev_attr_hotkey_poll_freq.attr,
3102 #endif
3106 * Sync both the hw and sw blocking state of all switches
3108 static void tpacpi_send_radiosw_update(void)
3110 int wlsw;
3113 * We must sync all rfkill controllers *before* issuing any
3114 * rfkill input events, or we will race the rfkill core input
3115 * handler.
3117 * tpacpi_inputdev_send_mutex works as a synchronization point
3118 * for the above.
3120 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3123 wlsw = hotkey_get_wlsw();
3125 /* Sync hw blocking state first if it is hw-blocked */
3126 if (wlsw == TPACPI_RFK_RADIO_OFF)
3127 tpacpi_rfk_update_hwblock_state(true);
3129 /* Sync sw blocking state */
3130 tpacpi_rfk_update_swstate_all();
3132 /* Sync hw blocking state last if it is hw-unblocked */
3133 if (wlsw == TPACPI_RFK_RADIO_ON)
3134 tpacpi_rfk_update_hwblock_state(false);
3136 /* Issue rfkill input event for WLSW switch */
3137 if (!(wlsw < 0)) {
3138 mutex_lock(&tpacpi_inputdev_send_mutex);
3140 input_report_switch(tpacpi_inputdev,
3141 SW_RFKILL_ALL, (wlsw > 0));
3142 input_sync(tpacpi_inputdev);
3144 mutex_unlock(&tpacpi_inputdev_send_mutex);
3148 * this can be unconditional, as we will poll state again
3149 * if userspace uses the notify to read data
3151 hotkey_radio_sw_notify_change();
3154 static void hotkey_exit(void)
3156 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3157 mutex_lock(&hotkey_mutex);
3158 hotkey_poll_stop_sync();
3159 mutex_unlock(&hotkey_mutex);
3160 #endif
3162 if (hotkey_dev_attributes)
3163 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3165 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3166 "restoring original HKEY status and mask\n");
3167 /* yes, there is a bitwise or below, we want the
3168 * functions to be called even if one of them fail */
3169 if (((tp_features.hotkey_mask &&
3170 hotkey_mask_set(hotkey_orig_mask)) |
3171 hotkey_status_set(false)) != 0)
3172 pr_err("failed to restore hot key mask to BIOS defaults\n");
3175 static void __init hotkey_unmap(const unsigned int scancode)
3177 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3178 clear_bit(hotkey_keycode_map[scancode],
3179 tpacpi_inputdev->keybit);
3180 hotkey_keycode_map[scancode] = KEY_RESERVED;
3185 * HKEY quirks:
3186 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3189 #define TPACPI_HK_Q_INIMASK 0x0001
3191 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3192 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3193 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3194 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3195 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3196 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3197 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3198 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3199 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3200 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3201 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3202 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3203 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3204 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3205 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3206 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3207 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3208 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3209 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3210 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3213 typedef u16 tpacpi_keymap_entry_t;
3214 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3216 static int hotkey_init_tablet_mode(void)
3218 int in_tablet_mode = 0, res;
3219 char *type = NULL;
3221 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3222 int has_tablet_mode;
3224 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3225 &has_tablet_mode);
3226 if (has_tablet_mode)
3227 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3228 type = "GMMS";
3229 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3230 /* For X41t, X60t, X61t Tablets... */
3231 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3232 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3233 type = "MHKG";
3236 if (!tp_features.hotkey_tablet)
3237 return 0;
3239 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3240 type, in_tablet_mode ? "tablet" : "laptop");
3242 res = add_to_attr_set(hotkey_dev_attributes,
3243 &dev_attr_hotkey_tablet_mode.attr);
3244 if (res)
3245 return -1;
3247 return in_tablet_mode;
3250 static int __init hotkey_init(struct ibm_init_struct *iibm)
3252 /* Requirements for changing the default keymaps:
3254 * 1. Many of the keys are mapped to KEY_RESERVED for very
3255 * good reasons. Do not change them unless you have deep
3256 * knowledge on the IBM and Lenovo ThinkPad firmware for
3257 * the various ThinkPad models. The driver behaves
3258 * differently for KEY_RESERVED: such keys have their
3259 * hot key mask *unset* in mask_recommended, and also
3260 * in the initial hot key mask programmed into the
3261 * firmware at driver load time, which means the firm-
3262 * ware may react very differently if you change them to
3263 * something else;
3265 * 2. You must be subscribed to the linux-thinkpad and
3266 * ibm-acpi-devel mailing lists, and you should read the
3267 * list archives since 2007 if you want to change the
3268 * keymaps. This requirement exists so that you will
3269 * know the past history of problems with the thinkpad-
3270 * acpi driver keymaps, and also that you will be
3271 * listening to any bug reports;
3273 * 3. Do not send thinkpad-acpi specific patches directly to
3274 * for merging, *ever*. Send them to the linux-acpi
3275 * mailinglist for comments. Merging is to be done only
3276 * through acpi-test and the ACPI maintainer.
3278 * If the above is too much to ask, don't change the keymap.
3279 * Ask the thinkpad-acpi maintainer to do it, instead.
3282 enum keymap_index {
3283 TPACPI_KEYMAP_IBM_GENERIC = 0,
3284 TPACPI_KEYMAP_LENOVO_GENERIC,
3287 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3288 /* Generic keymap for IBM ThinkPads */
3289 [TPACPI_KEYMAP_IBM_GENERIC] = {
3290 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3291 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3292 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3293 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3295 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3296 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3297 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3298 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3300 /* brightness: firmware always reacts to them */
3301 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3302 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3304 /* Thinklight: firmware always react to it */
3305 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3307 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3308 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3310 /* Volume: firmware always react to it and reprograms
3311 * the built-in *extra* mixer. Never map it to control
3312 * another mixer by default. */
3313 KEY_RESERVED, /* 0x14: VOLUME UP */
3314 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3315 KEY_RESERVED, /* 0x16: MUTE */
3317 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3319 /* (assignments unknown, please report if found) */
3320 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3321 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3323 /* No assignments, only used for Adaptive keyboards. */
3324 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3327 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3328 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3330 /* No assignment, used for newer Lenovo models */
3331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3335 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3336 KEY_UNKNOWN, KEY_UNKNOWN
3340 /* Generic keymap for Lenovo ThinkPads */
3341 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3342 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3343 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3344 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3345 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3347 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3348 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3349 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3350 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3352 /* These should be enabled --only-- when ACPI video
3353 * is disabled (i.e. in "vendor" mode), and are handled
3354 * in a special way by the init code */
3355 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3356 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3358 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3360 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3361 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3363 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3364 * react to it and reprograms the built-in *extra* mixer.
3365 * Never map it to control another mixer by default.
3367 * T60?, T61, R60?, R61: firmware and EC tries to send
3368 * these over the regular keyboard, so these are no-ops,
3369 * but there are still weird bugs re. MUTE, so do not
3370 * change unless you get test reports from all Lenovo
3371 * models. May cause the BIOS to interfere with the
3372 * HDA mixer.
3374 KEY_RESERVED, /* 0x14: VOLUME UP */
3375 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3376 KEY_RESERVED, /* 0x16: MUTE */
3378 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3380 /* (assignments unknown, please report if found) */
3381 KEY_UNKNOWN, KEY_UNKNOWN,
3384 * The mic mute button only sends 0x1a. It does not
3385 * automatically mute the mic or change the mute light.
3387 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3389 /* (assignments unknown, please report if found) */
3390 KEY_UNKNOWN,
3392 /* Extra keys in use since the X240 / T440 / T540 */
3393 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3396 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3397 * The first item in this list is the Mute button which is
3398 * emitted with 0x103 through
3399 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3400 * symbol is held.
3401 * We'll need to offset those by 0x20.
3403 KEY_RESERVED, /* Mute held, 0x103 */
3404 KEY_BRIGHTNESS_MIN, /* Backlight off */
3405 KEY_RESERVED, /* Clipping tool */
3406 KEY_RESERVED, /* Cloud */
3407 KEY_RESERVED,
3408 KEY_VOICECOMMAND, /* Voice */
3409 KEY_RESERVED,
3410 KEY_RESERVED, /* Gestures */
3411 KEY_RESERVED,
3412 KEY_RESERVED,
3413 KEY_RESERVED,
3414 KEY_CONFIG, /* Settings */
3415 KEY_RESERVED, /* New tab */
3416 KEY_REFRESH, /* Reload */
3417 KEY_BACK, /* Back */
3418 KEY_RESERVED, /* Microphone down */
3419 KEY_RESERVED, /* Microphone up */
3420 KEY_RESERVED, /* Microphone cancellation */
3421 KEY_RESERVED, /* Camera mode */
3422 KEY_RESERVED, /* Rotate display, 0x116 */
3425 * These are found in 2017 models (e.g. T470s, X270).
3426 * The lowest known value is 0x311, which according to
3427 * the manual should launch a user defined favorite
3428 * application.
3430 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3431 * corresponding to 0x34.
3434 /* (assignments unknown, please report if found) */
3435 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3436 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3437 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3438 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3439 KEY_UNKNOWN,
3441 KEY_FAVORITES, /* Favorite app, 0x311 */
3442 KEY_RESERVED, /* Clipping tool */
3443 KEY_RESERVED,
3444 KEY_BLUETOOTH, /* Bluetooth */
3445 KEY_KEYBOARD /* Keyboard, 0x315 */
3449 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3450 /* Generic maps (fallback) */
3452 .vendor = PCI_VENDOR_ID_IBM,
3453 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3454 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3457 .vendor = PCI_VENDOR_ID_LENOVO,
3458 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3459 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3463 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3464 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3466 int res, i;
3467 int status;
3468 int hkeyv;
3469 bool radiosw_state = false;
3470 bool tabletsw_state = false;
3472 unsigned long quirks;
3473 unsigned long keymap_id;
3475 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3476 "initializing hotkey subdriver\n");
3478 BUG_ON(!tpacpi_inputdev);
3479 BUG_ON(tpacpi_inputdev->open != NULL ||
3480 tpacpi_inputdev->close != NULL);
3482 TPACPI_ACPIHANDLE_INIT(hkey);
3483 mutex_init(&hotkey_mutex);
3485 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3486 mutex_init(&hotkey_thread_data_mutex);
3487 #endif
3489 /* hotkey not supported on 570 */
3490 tp_features.hotkey = hkey_handle != NULL;
3492 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3493 "hotkeys are %s\n",
3494 str_supported(tp_features.hotkey));
3496 if (!tp_features.hotkey)
3497 return 1;
3499 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3500 ARRAY_SIZE(tpacpi_hotkey_qtable));
3502 tpacpi_disable_brightness_delay();
3504 /* MUST have enough space for all attributes to be added to
3505 * hotkey_dev_attributes */
3506 hotkey_dev_attributes = create_attr_set(
3507 ARRAY_SIZE(hotkey_attributes) + 2,
3508 NULL);
3509 if (!hotkey_dev_attributes)
3510 return -ENOMEM;
3511 res = add_many_to_attr_set(hotkey_dev_attributes,
3512 hotkey_attributes,
3513 ARRAY_SIZE(hotkey_attributes));
3514 if (res)
3515 goto err_exit;
3517 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3518 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3519 for HKEY interface version 0x100 */
3520 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3521 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3522 "firmware HKEY interface version: 0x%x\n",
3523 hkeyv);
3525 switch (hkeyv >> 8) {
3526 case 1:
3528 * MHKV 0x100 in A31, R40, R40e,
3529 * T4x, X31, and later
3532 /* Paranoia check AND init hotkey_all_mask */
3533 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3534 "MHKA", "qd")) {
3535 pr_err("missing MHKA handler, please report this to %s\n",
3536 TPACPI_MAIL);
3537 /* Fallback: pre-init for FN+F3,F4,F12 */
3538 hotkey_all_mask = 0x080cU;
3539 } else {
3540 tp_features.hotkey_mask = 1;
3542 break;
3544 case 2:
3546 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3549 /* Paranoia check AND init hotkey_all_mask */
3550 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3551 "MHKA", "dd", 1)) {
3552 pr_err("missing MHKA handler, please report this to %s\n",
3553 TPACPI_MAIL);
3554 /* Fallback: pre-init for FN+F3,F4,F12 */
3555 hotkey_all_mask = 0x080cU;
3556 } else {
3557 tp_features.hotkey_mask = 1;
3561 * Check if we have an adaptive keyboard, like on the
3562 * Lenovo Carbon X1 2014 (2nd Gen).
3564 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3565 "MHKA", "dd", 2)) {
3566 if (hotkey_adaptive_all_mask != 0) {
3567 tp_features.has_adaptive_kbd = true;
3568 res = sysfs_create_group(
3569 &tpacpi_pdev->dev.kobj,
3570 &adaptive_kbd_attr_group);
3571 if (res)
3572 goto err_exit;
3574 } else {
3575 tp_features.has_adaptive_kbd = false;
3576 hotkey_adaptive_all_mask = 0x0U;
3578 break;
3580 default:
3581 pr_err("unknown version of the HKEY interface: 0x%x\n",
3582 hkeyv);
3583 pr_err("please report this to %s\n", TPACPI_MAIL);
3584 break;
3588 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3589 "hotkey masks are %s\n",
3590 str_supported(tp_features.hotkey_mask));
3592 /* Init hotkey_all_mask if not initialized yet */
3593 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3594 (quirks & TPACPI_HK_Q_INIMASK))
3595 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3597 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3598 if (tp_features.hotkey_mask) {
3599 /* hotkey_source_mask *must* be zero for
3600 * the first hotkey_mask_get to return hotkey_orig_mask */
3601 res = hotkey_mask_get();
3602 if (res)
3603 goto err_exit;
3605 hotkey_orig_mask = hotkey_acpi_mask;
3606 } else {
3607 hotkey_orig_mask = hotkey_all_mask;
3608 hotkey_acpi_mask = hotkey_all_mask;
3611 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3612 if (dbg_wlswemul) {
3613 tp_features.hotkey_wlsw = 1;
3614 radiosw_state = !!tpacpi_wlsw_emulstate;
3615 pr_info("radio switch emulation enabled\n");
3616 } else
3617 #endif
3618 /* Not all thinkpads have a hardware radio switch */
3619 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3620 tp_features.hotkey_wlsw = 1;
3621 radiosw_state = !!status;
3622 pr_info("radio switch found; radios are %s\n",
3623 enabled(status, 0));
3625 if (tp_features.hotkey_wlsw)
3626 res = add_to_attr_set(hotkey_dev_attributes,
3627 &dev_attr_hotkey_radio_sw.attr);
3629 res = hotkey_init_tablet_mode();
3630 if (res < 0)
3631 goto err_exit;
3633 tabletsw_state = res;
3635 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3636 &tpacpi_pdev->dev.kobj);
3637 if (res)
3638 goto err_exit;
3640 /* Set up key map */
3641 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3642 GFP_KERNEL);
3643 if (!hotkey_keycode_map) {
3644 pr_err("failed to allocate memory for key map\n");
3645 res = -ENOMEM;
3646 goto err_exit;
3649 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3650 ARRAY_SIZE(tpacpi_keymap_qtable));
3651 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3652 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3653 "using keymap number %lu\n", keymap_id);
3655 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3656 TPACPI_HOTKEY_MAP_SIZE);
3658 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3659 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3660 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3661 tpacpi_inputdev->keycode = hotkey_keycode_map;
3662 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3663 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3664 input_set_capability(tpacpi_inputdev, EV_KEY,
3665 hotkey_keycode_map[i]);
3666 } else {
3667 if (i < sizeof(hotkey_reserved_mask)*8)
3668 hotkey_reserved_mask |= 1 << i;
3672 if (tp_features.hotkey_wlsw) {
3673 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3674 input_report_switch(tpacpi_inputdev,
3675 SW_RFKILL_ALL, radiosw_state);
3677 if (tp_features.hotkey_tablet) {
3678 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3679 input_report_switch(tpacpi_inputdev,
3680 SW_TABLET_MODE, tabletsw_state);
3683 /* Do not issue duplicate brightness change events to
3684 * userspace. tpacpi_detect_brightness_capabilities() must have
3685 * been called before this point */
3686 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3687 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3688 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3690 /* Disable brightness up/down on Lenovo thinkpads when
3691 * ACPI is handling them, otherwise it is plain impossible
3692 * for userspace to do something even remotely sane */
3693 hotkey_reserved_mask |=
3694 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3695 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3696 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3697 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3700 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3701 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3702 & ~hotkey_all_mask
3703 & ~hotkey_reserved_mask;
3705 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3706 "hotkey source mask 0x%08x, polling freq %u\n",
3707 hotkey_source_mask, hotkey_poll_freq);
3708 #endif
3710 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3711 "enabling firmware HKEY event interface...\n");
3712 res = hotkey_status_set(true);
3713 if (res) {
3714 hotkey_exit();
3715 return res;
3717 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3718 | hotkey_driver_mask)
3719 & ~hotkey_source_mask);
3720 if (res < 0 && res != -ENXIO) {
3721 hotkey_exit();
3722 return res;
3724 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3725 & ~hotkey_reserved_mask;
3726 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3727 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3728 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3730 tpacpi_inputdev->open = &hotkey_inputdev_open;
3731 tpacpi_inputdev->close = &hotkey_inputdev_close;
3733 hotkey_poll_setup_safe(true);
3735 return 0;
3737 err_exit:
3738 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3739 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3740 &adaptive_kbd_attr_group);
3742 hotkey_dev_attributes = NULL;
3744 return (res < 0) ? res : 1;
3747 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3748 * mode, Web conference mode, Function mode and Lay-flat mode.
3749 * We support Home mode and Function mode currently.
3751 * Will consider support rest of modes in future.
3754 static const int adaptive_keyboard_modes[] = {
3755 HOME_MODE,
3756 /* WEB_BROWSER_MODE = 2,
3757 WEB_CONFERENCE_MODE = 3, */
3758 FUNCTION_MODE
3761 #define DFR_CHANGE_ROW 0x101
3762 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3763 #define FIRST_ADAPTIVE_KEY 0x103
3765 /* press Fn key a while second, it will switch to Function Mode. Then
3766 * release Fn key, previous mode be restored.
3768 static bool adaptive_keyboard_mode_is_saved;
3769 static int adaptive_keyboard_prev_mode;
3771 static int adaptive_keyboard_get_mode(void)
3773 int mode = 0;
3775 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3776 pr_err("Cannot read adaptive keyboard mode\n");
3777 return -EIO;
3780 return mode;
3783 static int adaptive_keyboard_set_mode(int new_mode)
3785 if (new_mode < 0 ||
3786 new_mode > LAYFLAT_MODE)
3787 return -EINVAL;
3789 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3790 pr_err("Cannot set adaptive keyboard mode\n");
3791 return -EIO;
3794 return 0;
3797 static int adaptive_keyboard_get_next_mode(int mode)
3799 size_t i;
3800 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3802 for (i = 0; i <= max_mode; i++) {
3803 if (adaptive_keyboard_modes[i] == mode)
3804 break;
3807 if (i >= max_mode)
3808 i = 0;
3809 else
3810 i++;
3812 return adaptive_keyboard_modes[i];
3815 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3817 int current_mode = 0;
3818 int new_mode = 0;
3819 int keycode;
3821 switch (scancode) {
3822 case DFR_CHANGE_ROW:
3823 if (adaptive_keyboard_mode_is_saved) {
3824 new_mode = adaptive_keyboard_prev_mode;
3825 adaptive_keyboard_mode_is_saved = false;
3826 } else {
3827 current_mode = adaptive_keyboard_get_mode();
3828 if (current_mode < 0)
3829 return false;
3830 new_mode = adaptive_keyboard_get_next_mode(
3831 current_mode);
3834 if (adaptive_keyboard_set_mode(new_mode) < 0)
3835 return false;
3837 return true;
3839 case DFR_SHOW_QUICKVIEW_ROW:
3840 current_mode = adaptive_keyboard_get_mode();
3841 if (current_mode < 0)
3842 return false;
3844 adaptive_keyboard_prev_mode = current_mode;
3845 adaptive_keyboard_mode_is_saved = true;
3847 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3848 return false;
3849 return true;
3851 default:
3852 if (scancode < FIRST_ADAPTIVE_KEY ||
3853 scancode >= FIRST_ADAPTIVE_KEY +
3854 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3855 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3856 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3857 scancode);
3858 return false;
3860 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3861 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3862 if (keycode != KEY_RESERVED) {
3863 mutex_lock(&tpacpi_inputdev_send_mutex);
3865 input_report_key(tpacpi_inputdev, keycode, 1);
3866 input_sync(tpacpi_inputdev);
3868 input_report_key(tpacpi_inputdev, keycode, 0);
3869 input_sync(tpacpi_inputdev);
3871 mutex_unlock(&tpacpi_inputdev_send_mutex);
3873 return true;
3877 static bool hotkey_notify_hotkey(const u32 hkey,
3878 bool *send_acpi_ev,
3879 bool *ignore_acpi_ev)
3881 /* 0x1000-0x1FFF: key presses */
3882 unsigned int scancode = hkey & 0xfff;
3883 *send_acpi_ev = true;
3884 *ignore_acpi_ev = false;
3887 * Original events are in the 0x10XX range, the adaptive keyboard
3888 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3889 * models, additional keys are emitted through 0x13XX.
3891 switch ((hkey >> 8) & 0xf) {
3892 case 0:
3893 if (scancode > 0 &&
3894 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3895 /* HKEY event 0x1001 is scancode 0x00 */
3896 scancode--;
3897 if (!(hotkey_source_mask & (1 << scancode))) {
3898 tpacpi_input_send_key_masked(scancode);
3899 *send_acpi_ev = false;
3900 } else {
3901 *ignore_acpi_ev = true;
3903 return true;
3905 break;
3907 case 1:
3908 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3910 case 3:
3911 /* Extended keycodes start at 0x300 and our offset into the map
3912 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3913 * will be positive, but might not be in the correct range.
3915 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3916 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3917 scancode < TPACPI_HOTKEY_MAP_LEN) {
3918 tpacpi_input_send_key(scancode);
3919 return true;
3921 break;
3924 return false;
3927 static bool hotkey_notify_wakeup(const u32 hkey,
3928 bool *send_acpi_ev,
3929 bool *ignore_acpi_ev)
3931 /* 0x2000-0x2FFF: Wakeup reason */
3932 *send_acpi_ev = true;
3933 *ignore_acpi_ev = false;
3935 switch (hkey) {
3936 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3937 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3938 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3939 *ignore_acpi_ev = true;
3940 break;
3942 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3943 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3944 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3945 *ignore_acpi_ev = true;
3946 break;
3948 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3949 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3950 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3951 /* how to auto-heal: */
3952 /* 2313: woke up from S3, go to S4/S5 */
3953 /* 2413: woke up from S4, go to S5 */
3954 break;
3956 default:
3957 return false;
3960 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3961 pr_info("woke up due to a hot-unplug request...\n");
3962 hotkey_wakeup_reason_notify_change();
3964 return true;
3967 static bool hotkey_notify_dockevent(const u32 hkey,
3968 bool *send_acpi_ev,
3969 bool *ignore_acpi_ev)
3971 /* 0x4000-0x4FFF: dock-related events */
3972 *send_acpi_ev = true;
3973 *ignore_acpi_ev = false;
3975 switch (hkey) {
3976 case TP_HKEY_EV_UNDOCK_ACK:
3977 /* ACPI undock operation completed after wakeup */
3978 hotkey_autosleep_ack = 1;
3979 pr_info("undocked\n");
3980 hotkey_wakeup_hotunplug_complete_notify_change();
3981 return true;
3983 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3984 pr_info("docked into hotplug port replicator\n");
3985 return true;
3986 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3987 pr_info("undocked from hotplug port replicator\n");
3988 return true;
3990 default:
3991 return false;
3995 static bool hotkey_notify_usrevent(const u32 hkey,
3996 bool *send_acpi_ev,
3997 bool *ignore_acpi_ev)
3999 /* 0x5000-0x5FFF: human interface helpers */
4000 *send_acpi_ev = true;
4001 *ignore_acpi_ev = false;
4003 switch (hkey) {
4004 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4005 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
4006 return true;
4008 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4009 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4010 tpacpi_input_send_tabletsw();
4011 hotkey_tablet_mode_notify_change();
4012 *send_acpi_ev = false;
4013 return true;
4015 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4016 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4017 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
4018 /* do not propagate these events */
4019 *ignore_acpi_ev = true;
4020 return true;
4022 default:
4023 return false;
4027 static void thermal_dump_all_sensors(void);
4029 static bool hotkey_notify_6xxx(const u32 hkey,
4030 bool *send_acpi_ev,
4031 bool *ignore_acpi_ev)
4033 bool known = true;
4035 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4036 *send_acpi_ev = true;
4037 *ignore_acpi_ev = false;
4039 switch (hkey) {
4040 case TP_HKEY_EV_THM_TABLE_CHANGED:
4041 pr_info("EC reports that Thermal Table has changed\n");
4042 /* recommended action: do nothing, we don't have
4043 * Lenovo ATM information */
4044 return true;
4045 case TP_HKEY_EV_ALARM_BAT_HOT:
4046 pr_crit("THERMAL ALARM: battery is too hot!\n");
4047 /* recommended action: warn user through gui */
4048 break;
4049 case TP_HKEY_EV_ALARM_BAT_XHOT:
4050 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4051 /* recommended action: immediate sleep/hibernate */
4052 break;
4053 case TP_HKEY_EV_ALARM_SENSOR_HOT:
4054 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4055 /* recommended action: warn user through gui, that */
4056 /* some internal component is too hot */
4057 break;
4058 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
4059 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4060 /* recommended action: immediate sleep/hibernate */
4061 break;
4062 case TP_HKEY_EV_AC_CHANGED:
4063 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4064 * AC status changed; can be triggered by plugging or
4065 * unplugging AC adapter, docking or undocking. */
4067 /* fallthrough */
4069 case TP_HKEY_EV_KEY_NUMLOCK:
4070 case TP_HKEY_EV_KEY_FN:
4071 case TP_HKEY_EV_KEY_FN_ESC:
4072 /* key press events, we just ignore them as long as the EC
4073 * is still reporting them in the normal keyboard stream */
4074 *send_acpi_ev = false;
4075 *ignore_acpi_ev = true;
4076 return true;
4078 case TP_HKEY_EV_TABLET_CHANGED:
4079 tpacpi_input_send_tabletsw();
4080 hotkey_tablet_mode_notify_change();
4081 *send_acpi_ev = false;
4082 break;
4084 case TP_HKEY_EV_PALM_DETECTED:
4085 case TP_HKEY_EV_PALM_UNDETECTED:
4086 /* palm detected hovering the keyboard, forward to user-space
4087 * via netlink for consumption */
4088 return true;
4090 default:
4091 pr_warn("unknown possible thermal alarm or keyboard event received\n");
4092 known = false;
4095 thermal_dump_all_sensors();
4097 return known;
4100 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4102 u32 hkey;
4103 bool send_acpi_ev;
4104 bool ignore_acpi_ev;
4105 bool known_ev;
4107 if (event != 0x80) {
4108 pr_err("unknown HKEY notification event %d\n", event);
4109 /* forward it to userspace, maybe it knows how to handle it */
4110 acpi_bus_generate_netlink_event(
4111 ibm->acpi->device->pnp.device_class,
4112 dev_name(&ibm->acpi->device->dev),
4113 event, 0);
4114 return;
4117 while (1) {
4118 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4119 pr_err("failed to retrieve HKEY event\n");
4120 return;
4123 if (hkey == 0) {
4124 /* queue empty */
4125 return;
4128 send_acpi_ev = true;
4129 ignore_acpi_ev = false;
4131 switch (hkey >> 12) {
4132 case 1:
4133 /* 0x1000-0x1FFF: key presses */
4134 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4135 &ignore_acpi_ev);
4136 break;
4137 case 2:
4138 /* 0x2000-0x2FFF: Wakeup reason */
4139 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4140 &ignore_acpi_ev);
4141 break;
4142 case 3:
4143 /* 0x3000-0x3FFF: bay-related wakeups */
4144 switch (hkey) {
4145 case TP_HKEY_EV_BAYEJ_ACK:
4146 hotkey_autosleep_ack = 1;
4147 pr_info("bay ejected\n");
4148 hotkey_wakeup_hotunplug_complete_notify_change();
4149 known_ev = true;
4150 break;
4151 case TP_HKEY_EV_OPTDRV_EJ:
4152 /* FIXME: kick libata if SATA link offline */
4153 known_ev = true;
4154 break;
4155 default:
4156 known_ev = false;
4158 break;
4159 case 4:
4160 /* 0x4000-0x4FFF: dock-related events */
4161 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4162 &ignore_acpi_ev);
4163 break;
4164 case 5:
4165 /* 0x5000-0x5FFF: human interface helpers */
4166 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4167 &ignore_acpi_ev);
4168 break;
4169 case 6:
4170 /* 0x6000-0x6FFF: thermal alarms/notices and
4171 * keyboard events */
4172 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4173 &ignore_acpi_ev);
4174 break;
4175 case 7:
4176 /* 0x7000-0x7FFF: misc */
4177 if (tp_features.hotkey_wlsw &&
4178 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4179 tpacpi_send_radiosw_update();
4180 send_acpi_ev = 0;
4181 known_ev = true;
4182 break;
4184 /* fallthrough to default */
4185 default:
4186 known_ev = false;
4188 if (!known_ev) {
4189 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4190 pr_notice("please report the conditions when this event happened to %s\n",
4191 TPACPI_MAIL);
4194 /* netlink events */
4195 if (!ignore_acpi_ev && send_acpi_ev) {
4196 acpi_bus_generate_netlink_event(
4197 ibm->acpi->device->pnp.device_class,
4198 dev_name(&ibm->acpi->device->dev),
4199 event, hkey);
4204 static void hotkey_suspend(void)
4206 /* Do these on suspend, we get the events on early resume! */
4207 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4208 hotkey_autosleep_ack = 0;
4210 /* save previous mode of adaptive keyboard of X1 Carbon */
4211 if (tp_features.has_adaptive_kbd) {
4212 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4213 "GTRW", "dd", 0)) {
4214 pr_err("Cannot read adaptive keyboard mode.\n");
4219 static void hotkey_resume(void)
4221 tpacpi_disable_brightness_delay();
4223 if (hotkey_status_set(true) < 0 ||
4224 hotkey_mask_set(hotkey_acpi_mask) < 0)
4225 pr_err("error while attempting to reset the event firmware interface\n");
4227 tpacpi_send_radiosw_update();
4228 hotkey_tablet_mode_notify_change();
4229 hotkey_wakeup_reason_notify_change();
4230 hotkey_wakeup_hotunplug_complete_notify_change();
4231 hotkey_poll_setup_safe(false);
4233 /* restore previous mode of adapive keyboard of X1 Carbon */
4234 if (tp_features.has_adaptive_kbd) {
4235 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4236 adaptive_keyboard_prev_mode)) {
4237 pr_err("Cannot set adaptive keyboard mode.\n");
4242 /* procfs -------------------------------------------------------------- */
4243 static int hotkey_read(struct seq_file *m)
4245 int res, status;
4247 if (!tp_features.hotkey) {
4248 seq_printf(m, "status:\t\tnot supported\n");
4249 return 0;
4252 if (mutex_lock_killable(&hotkey_mutex))
4253 return -ERESTARTSYS;
4254 res = hotkey_status_get(&status);
4255 if (!res)
4256 res = hotkey_mask_get();
4257 mutex_unlock(&hotkey_mutex);
4258 if (res)
4259 return res;
4261 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4262 if (hotkey_all_mask) {
4263 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4264 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4265 } else {
4266 seq_printf(m, "mask:\t\tnot supported\n");
4267 seq_printf(m, "commands:\tenable, disable, reset\n");
4270 return 0;
4273 static void hotkey_enabledisable_warn(bool enable)
4275 tpacpi_log_usertask("procfs hotkey enable/disable");
4276 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4277 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4278 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4281 static int hotkey_write(char *buf)
4283 int res;
4284 u32 mask;
4285 char *cmd;
4287 if (!tp_features.hotkey)
4288 return -ENODEV;
4290 if (mutex_lock_killable(&hotkey_mutex))
4291 return -ERESTARTSYS;
4293 mask = hotkey_user_mask;
4295 res = 0;
4296 while ((cmd = next_cmd(&buf))) {
4297 if (strlencmp(cmd, "enable") == 0) {
4298 hotkey_enabledisable_warn(1);
4299 } else if (strlencmp(cmd, "disable") == 0) {
4300 hotkey_enabledisable_warn(0);
4301 res = -EPERM;
4302 } else if (strlencmp(cmd, "reset") == 0) {
4303 mask = (hotkey_all_mask | hotkey_source_mask)
4304 & ~hotkey_reserved_mask;
4305 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4306 /* mask set */
4307 } else if (sscanf(cmd, "%x", &mask) == 1) {
4308 /* mask set */
4309 } else {
4310 res = -EINVAL;
4311 goto errexit;
4315 if (!res) {
4316 tpacpi_disclose_usertask("procfs hotkey",
4317 "set mask to 0x%08x\n", mask);
4318 res = hotkey_user_mask_set(mask);
4321 errexit:
4322 mutex_unlock(&hotkey_mutex);
4323 return res;
4326 static const struct acpi_device_id ibm_htk_device_ids[] = {
4327 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4328 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4329 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4330 {"", 0},
4333 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4334 .hid = ibm_htk_device_ids,
4335 .notify = hotkey_notify,
4336 .handle = &hkey_handle,
4337 .type = ACPI_DEVICE_NOTIFY,
4340 static struct ibm_struct hotkey_driver_data = {
4341 .name = "hotkey",
4342 .read = hotkey_read,
4343 .write = hotkey_write,
4344 .exit = hotkey_exit,
4345 .resume = hotkey_resume,
4346 .suspend = hotkey_suspend,
4347 .acpi = &ibm_hotkey_acpidriver,
4350 /*************************************************************************
4351 * Bluetooth subdriver
4354 enum {
4355 /* ACPI GBDC/SBDC bits */
4356 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4357 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4358 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4359 0 = disable, 1 = enable */
4362 enum {
4363 /* ACPI \BLTH commands */
4364 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4365 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4366 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4367 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4368 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4371 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4373 static int bluetooth_get_status(void)
4375 int status;
4377 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4378 if (dbg_bluetoothemul)
4379 return (tpacpi_bluetooth_emulstate) ?
4380 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4381 #endif
4383 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4384 return -EIO;
4386 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4387 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4390 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4392 int status;
4394 vdbg_printk(TPACPI_DBG_RFKILL,
4395 "will attempt to %s bluetooth\n",
4396 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4398 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4399 if (dbg_bluetoothemul) {
4400 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4401 return 0;
4403 #endif
4405 if (state == TPACPI_RFK_RADIO_ON)
4406 status = TP_ACPI_BLUETOOTH_RADIOSSW
4407 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4408 else
4409 status = 0;
4411 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4412 return -EIO;
4414 return 0;
4417 /* sysfs bluetooth enable ---------------------------------------------- */
4418 static ssize_t bluetooth_enable_show(struct device *dev,
4419 struct device_attribute *attr,
4420 char *buf)
4422 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4423 attr, buf);
4426 static ssize_t bluetooth_enable_store(struct device *dev,
4427 struct device_attribute *attr,
4428 const char *buf, size_t count)
4430 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4431 attr, buf, count);
4434 static DEVICE_ATTR_RW(bluetooth_enable);
4436 /* --------------------------------------------------------------------- */
4438 static struct attribute *bluetooth_attributes[] = {
4439 &dev_attr_bluetooth_enable.attr,
4440 NULL
4443 static const struct attribute_group bluetooth_attr_group = {
4444 .attrs = bluetooth_attributes,
4447 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4448 .get_status = bluetooth_get_status,
4449 .set_status = bluetooth_set_status,
4452 static void bluetooth_shutdown(void)
4454 /* Order firmware to save current state to NVRAM */
4455 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4456 TP_ACPI_BLTH_SAVE_STATE))
4457 pr_notice("failed to save bluetooth state to NVRAM\n");
4458 else
4459 vdbg_printk(TPACPI_DBG_RFKILL,
4460 "bluetooth state saved to NVRAM\n");
4463 static void bluetooth_exit(void)
4465 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4466 &bluetooth_attr_group);
4468 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4470 bluetooth_shutdown();
4473 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4475 int res;
4476 int status = 0;
4478 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4479 "initializing bluetooth subdriver\n");
4481 TPACPI_ACPIHANDLE_INIT(hkey);
4483 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4484 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4485 tp_features.bluetooth = hkey_handle &&
4486 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4488 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4489 "bluetooth is %s, status 0x%02x\n",
4490 str_supported(tp_features.bluetooth),
4491 status);
4493 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4494 if (dbg_bluetoothemul) {
4495 tp_features.bluetooth = 1;
4496 pr_info("bluetooth switch emulation enabled\n");
4497 } else
4498 #endif
4499 if (tp_features.bluetooth &&
4500 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4501 /* no bluetooth hardware present in system */
4502 tp_features.bluetooth = 0;
4503 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4504 "bluetooth hardware not installed\n");
4507 if (!tp_features.bluetooth)
4508 return 1;
4510 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4511 &bluetooth_tprfk_ops,
4512 RFKILL_TYPE_BLUETOOTH,
4513 TPACPI_RFK_BLUETOOTH_SW_NAME,
4514 true);
4515 if (res)
4516 return res;
4518 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4519 &bluetooth_attr_group);
4520 if (res) {
4521 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4522 return res;
4525 return 0;
4528 /* procfs -------------------------------------------------------------- */
4529 static int bluetooth_read(struct seq_file *m)
4531 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4534 static int bluetooth_write(char *buf)
4536 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4539 static struct ibm_struct bluetooth_driver_data = {
4540 .name = "bluetooth",
4541 .read = bluetooth_read,
4542 .write = bluetooth_write,
4543 .exit = bluetooth_exit,
4544 .shutdown = bluetooth_shutdown,
4547 /*************************************************************************
4548 * Wan subdriver
4551 enum {
4552 /* ACPI GWAN/SWAN bits */
4553 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4554 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4555 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4556 0 = disable, 1 = enable */
4559 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4561 static int wan_get_status(void)
4563 int status;
4565 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4566 if (dbg_wwanemul)
4567 return (tpacpi_wwan_emulstate) ?
4568 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4569 #endif
4571 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4572 return -EIO;
4574 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4575 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4578 static int wan_set_status(enum tpacpi_rfkill_state state)
4580 int status;
4582 vdbg_printk(TPACPI_DBG_RFKILL,
4583 "will attempt to %s wwan\n",
4584 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4586 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4587 if (dbg_wwanemul) {
4588 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4589 return 0;
4591 #endif
4593 if (state == TPACPI_RFK_RADIO_ON)
4594 status = TP_ACPI_WANCARD_RADIOSSW
4595 | TP_ACPI_WANCARD_RESUMECTRL;
4596 else
4597 status = 0;
4599 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4600 return -EIO;
4602 return 0;
4605 /* sysfs wan enable ---------------------------------------------------- */
4606 static ssize_t wan_enable_show(struct device *dev,
4607 struct device_attribute *attr,
4608 char *buf)
4610 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4611 attr, buf);
4614 static ssize_t wan_enable_store(struct device *dev,
4615 struct device_attribute *attr,
4616 const char *buf, size_t count)
4618 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4619 attr, buf, count);
4622 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4623 wan_enable_show, wan_enable_store);
4625 /* --------------------------------------------------------------------- */
4627 static struct attribute *wan_attributes[] = {
4628 &dev_attr_wwan_enable.attr,
4629 NULL
4632 static const struct attribute_group wan_attr_group = {
4633 .attrs = wan_attributes,
4636 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4637 .get_status = wan_get_status,
4638 .set_status = wan_set_status,
4641 static void wan_shutdown(void)
4643 /* Order firmware to save current state to NVRAM */
4644 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4645 TP_ACPI_WGSV_SAVE_STATE))
4646 pr_notice("failed to save WWAN state to NVRAM\n");
4647 else
4648 vdbg_printk(TPACPI_DBG_RFKILL,
4649 "WWAN state saved to NVRAM\n");
4652 static void wan_exit(void)
4654 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4655 &wan_attr_group);
4657 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4659 wan_shutdown();
4662 static int __init wan_init(struct ibm_init_struct *iibm)
4664 int res;
4665 int status = 0;
4667 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4668 "initializing wan subdriver\n");
4670 TPACPI_ACPIHANDLE_INIT(hkey);
4672 tp_features.wan = hkey_handle &&
4673 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4675 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4676 "wan is %s, status 0x%02x\n",
4677 str_supported(tp_features.wan),
4678 status);
4680 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4681 if (dbg_wwanemul) {
4682 tp_features.wan = 1;
4683 pr_info("wwan switch emulation enabled\n");
4684 } else
4685 #endif
4686 if (tp_features.wan &&
4687 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4688 /* no wan hardware present in system */
4689 tp_features.wan = 0;
4690 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4691 "wan hardware not installed\n");
4694 if (!tp_features.wan)
4695 return 1;
4697 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4698 &wan_tprfk_ops,
4699 RFKILL_TYPE_WWAN,
4700 TPACPI_RFK_WWAN_SW_NAME,
4701 true);
4702 if (res)
4703 return res;
4705 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4706 &wan_attr_group);
4708 if (res) {
4709 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4710 return res;
4713 return 0;
4716 /* procfs -------------------------------------------------------------- */
4717 static int wan_read(struct seq_file *m)
4719 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4722 static int wan_write(char *buf)
4724 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4727 static struct ibm_struct wan_driver_data = {
4728 .name = "wan",
4729 .read = wan_read,
4730 .write = wan_write,
4731 .exit = wan_exit,
4732 .shutdown = wan_shutdown,
4735 /*************************************************************************
4736 * UWB subdriver
4739 enum {
4740 /* ACPI GUWB/SUWB bits */
4741 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4742 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4745 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4747 static int uwb_get_status(void)
4749 int status;
4751 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4752 if (dbg_uwbemul)
4753 return (tpacpi_uwb_emulstate) ?
4754 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4755 #endif
4757 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4758 return -EIO;
4760 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4761 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4764 static int uwb_set_status(enum tpacpi_rfkill_state state)
4766 int status;
4768 vdbg_printk(TPACPI_DBG_RFKILL,
4769 "will attempt to %s UWB\n",
4770 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4772 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4773 if (dbg_uwbemul) {
4774 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4775 return 0;
4777 #endif
4779 if (state == TPACPI_RFK_RADIO_ON)
4780 status = TP_ACPI_UWB_RADIOSSW;
4781 else
4782 status = 0;
4784 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4785 return -EIO;
4787 return 0;
4790 /* --------------------------------------------------------------------- */
4792 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4793 .get_status = uwb_get_status,
4794 .set_status = uwb_set_status,
4797 static void uwb_exit(void)
4799 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4802 static int __init uwb_init(struct ibm_init_struct *iibm)
4804 int res;
4805 int status = 0;
4807 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4808 "initializing uwb subdriver\n");
4810 TPACPI_ACPIHANDLE_INIT(hkey);
4812 tp_features.uwb = hkey_handle &&
4813 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4815 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4816 "uwb is %s, status 0x%02x\n",
4817 str_supported(tp_features.uwb),
4818 status);
4820 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4821 if (dbg_uwbemul) {
4822 tp_features.uwb = 1;
4823 pr_info("uwb switch emulation enabled\n");
4824 } else
4825 #endif
4826 if (tp_features.uwb &&
4827 !(status & TP_ACPI_UWB_HWPRESENT)) {
4828 /* no uwb hardware present in system */
4829 tp_features.uwb = 0;
4830 dbg_printk(TPACPI_DBG_INIT,
4831 "uwb hardware not installed\n");
4834 if (!tp_features.uwb)
4835 return 1;
4837 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4838 &uwb_tprfk_ops,
4839 RFKILL_TYPE_UWB,
4840 TPACPI_RFK_UWB_SW_NAME,
4841 false);
4842 return res;
4845 static struct ibm_struct uwb_driver_data = {
4846 .name = "uwb",
4847 .exit = uwb_exit,
4848 .flags.experimental = 1,
4851 /*************************************************************************
4852 * Video subdriver
4855 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4857 enum video_access_mode {
4858 TPACPI_VIDEO_NONE = 0,
4859 TPACPI_VIDEO_570, /* 570 */
4860 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4861 TPACPI_VIDEO_NEW, /* all others */
4864 enum { /* video status flags, based on VIDEO_570 */
4865 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4866 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4867 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4870 enum { /* TPACPI_VIDEO_570 constants */
4871 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4872 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4873 * video_status_flags */
4874 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4875 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4878 static enum video_access_mode video_supported;
4879 static int video_orig_autosw;
4881 static int video_autosw_get(void);
4882 static int video_autosw_set(int enable);
4884 TPACPI_HANDLE(vid, root,
4885 "\\_SB.PCI.AGP.VGA", /* 570 */
4886 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4887 "\\_SB.PCI0.VID0", /* 770e */
4888 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4889 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4890 "\\_SB.PCI0.AGP.VID", /* all others */
4891 ); /* R30, R31 */
4893 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4895 static int __init video_init(struct ibm_init_struct *iibm)
4897 int ivga;
4899 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4901 TPACPI_ACPIHANDLE_INIT(vid);
4902 if (tpacpi_is_ibm())
4903 TPACPI_ACPIHANDLE_INIT(vid2);
4905 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4906 /* G41, assume IVGA doesn't change */
4907 vid_handle = vid2_handle;
4909 if (!vid_handle)
4910 /* video switching not supported on R30, R31 */
4911 video_supported = TPACPI_VIDEO_NONE;
4912 else if (tpacpi_is_ibm() &&
4913 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4914 /* 570 */
4915 video_supported = TPACPI_VIDEO_570;
4916 else if (tpacpi_is_ibm() &&
4917 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4918 /* 600e/x, 770e, 770x */
4919 video_supported = TPACPI_VIDEO_770;
4920 else
4921 /* all others */
4922 video_supported = TPACPI_VIDEO_NEW;
4924 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4925 str_supported(video_supported != TPACPI_VIDEO_NONE),
4926 video_supported);
4928 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4931 static void video_exit(void)
4933 dbg_printk(TPACPI_DBG_EXIT,
4934 "restoring original video autoswitch mode\n");
4935 if (video_autosw_set(video_orig_autosw))
4936 pr_err("error while trying to restore original video autoswitch mode\n");
4939 static int video_outputsw_get(void)
4941 int status = 0;
4942 int i;
4944 switch (video_supported) {
4945 case TPACPI_VIDEO_570:
4946 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4947 TP_ACPI_VIDEO_570_PHSCMD))
4948 return -EIO;
4949 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4950 break;
4951 case TPACPI_VIDEO_770:
4952 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4953 return -EIO;
4954 if (i)
4955 status |= TP_ACPI_VIDEO_S_LCD;
4956 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4957 return -EIO;
4958 if (i)
4959 status |= TP_ACPI_VIDEO_S_CRT;
4960 break;
4961 case TPACPI_VIDEO_NEW:
4962 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4963 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4964 return -EIO;
4965 if (i)
4966 status |= TP_ACPI_VIDEO_S_CRT;
4968 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4969 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4970 return -EIO;
4971 if (i)
4972 status |= TP_ACPI_VIDEO_S_LCD;
4973 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4974 return -EIO;
4975 if (i)
4976 status |= TP_ACPI_VIDEO_S_DVI;
4977 break;
4978 default:
4979 return -ENOSYS;
4982 return status;
4985 static int video_outputsw_set(int status)
4987 int autosw;
4988 int res = 0;
4990 switch (video_supported) {
4991 case TPACPI_VIDEO_570:
4992 res = acpi_evalf(NULL, NULL,
4993 "\\_SB.PHS2", "vdd",
4994 TP_ACPI_VIDEO_570_PHS2CMD,
4995 status | TP_ACPI_VIDEO_570_PHS2SET);
4996 break;
4997 case TPACPI_VIDEO_770:
4998 autosw = video_autosw_get();
4999 if (autosw < 0)
5000 return autosw;
5002 res = video_autosw_set(1);
5003 if (res)
5004 return res;
5005 res = acpi_evalf(vid_handle, NULL,
5006 "ASWT", "vdd", status * 0x100, 0);
5007 if (!autosw && video_autosw_set(autosw)) {
5008 pr_err("video auto-switch left enabled due to error\n");
5009 return -EIO;
5011 break;
5012 case TPACPI_VIDEO_NEW:
5013 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5014 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5015 break;
5016 default:
5017 return -ENOSYS;
5020 return (res) ? 0 : -EIO;
5023 static int video_autosw_get(void)
5025 int autosw = 0;
5027 switch (video_supported) {
5028 case TPACPI_VIDEO_570:
5029 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5030 return -EIO;
5031 break;
5032 case TPACPI_VIDEO_770:
5033 case TPACPI_VIDEO_NEW:
5034 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5035 return -EIO;
5036 break;
5037 default:
5038 return -ENOSYS;
5041 return autosw & 1;
5044 static int video_autosw_set(int enable)
5046 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5047 return -EIO;
5048 return 0;
5051 static int video_outputsw_cycle(void)
5053 int autosw = video_autosw_get();
5054 int res;
5056 if (autosw < 0)
5057 return autosw;
5059 switch (video_supported) {
5060 case TPACPI_VIDEO_570:
5061 res = video_autosw_set(1);
5062 if (res)
5063 return res;
5064 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5065 break;
5066 case TPACPI_VIDEO_770:
5067 case TPACPI_VIDEO_NEW:
5068 res = video_autosw_set(1);
5069 if (res)
5070 return res;
5071 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5072 break;
5073 default:
5074 return -ENOSYS;
5076 if (!autosw && video_autosw_set(autosw)) {
5077 pr_err("video auto-switch left enabled due to error\n");
5078 return -EIO;
5081 return (res) ? 0 : -EIO;
5084 static int video_expand_toggle(void)
5086 switch (video_supported) {
5087 case TPACPI_VIDEO_570:
5088 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5089 0 : -EIO;
5090 case TPACPI_VIDEO_770:
5091 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5092 0 : -EIO;
5093 case TPACPI_VIDEO_NEW:
5094 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5095 0 : -EIO;
5096 default:
5097 return -ENOSYS;
5099 /* not reached */
5102 static int video_read(struct seq_file *m)
5104 int status, autosw;
5106 if (video_supported == TPACPI_VIDEO_NONE) {
5107 seq_printf(m, "status:\t\tnot supported\n");
5108 return 0;
5111 /* Even reads can crash X.org, so... */
5112 if (!capable(CAP_SYS_ADMIN))
5113 return -EPERM;
5115 status = video_outputsw_get();
5116 if (status < 0)
5117 return status;
5119 autosw = video_autosw_get();
5120 if (autosw < 0)
5121 return autosw;
5123 seq_printf(m, "status:\t\tsupported\n");
5124 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5125 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5126 if (video_supported == TPACPI_VIDEO_NEW)
5127 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5128 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5129 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5130 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5131 if (video_supported == TPACPI_VIDEO_NEW)
5132 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5133 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5134 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5136 return 0;
5139 static int video_write(char *buf)
5141 char *cmd;
5142 int enable, disable, status;
5143 int res;
5145 if (video_supported == TPACPI_VIDEO_NONE)
5146 return -ENODEV;
5148 /* Even reads can crash X.org, let alone writes... */
5149 if (!capable(CAP_SYS_ADMIN))
5150 return -EPERM;
5152 enable = 0;
5153 disable = 0;
5155 while ((cmd = next_cmd(&buf))) {
5156 if (strlencmp(cmd, "lcd_enable") == 0) {
5157 enable |= TP_ACPI_VIDEO_S_LCD;
5158 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5159 disable |= TP_ACPI_VIDEO_S_LCD;
5160 } else if (strlencmp(cmd, "crt_enable") == 0) {
5161 enable |= TP_ACPI_VIDEO_S_CRT;
5162 } else if (strlencmp(cmd, "crt_disable") == 0) {
5163 disable |= TP_ACPI_VIDEO_S_CRT;
5164 } else if (video_supported == TPACPI_VIDEO_NEW &&
5165 strlencmp(cmd, "dvi_enable") == 0) {
5166 enable |= TP_ACPI_VIDEO_S_DVI;
5167 } else if (video_supported == TPACPI_VIDEO_NEW &&
5168 strlencmp(cmd, "dvi_disable") == 0) {
5169 disable |= TP_ACPI_VIDEO_S_DVI;
5170 } else if (strlencmp(cmd, "auto_enable") == 0) {
5171 res = video_autosw_set(1);
5172 if (res)
5173 return res;
5174 } else if (strlencmp(cmd, "auto_disable") == 0) {
5175 res = video_autosw_set(0);
5176 if (res)
5177 return res;
5178 } else if (strlencmp(cmd, "video_switch") == 0) {
5179 res = video_outputsw_cycle();
5180 if (res)
5181 return res;
5182 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5183 res = video_expand_toggle();
5184 if (res)
5185 return res;
5186 } else
5187 return -EINVAL;
5190 if (enable || disable) {
5191 status = video_outputsw_get();
5192 if (status < 0)
5193 return status;
5194 res = video_outputsw_set((status & ~disable) | enable);
5195 if (res)
5196 return res;
5199 return 0;
5202 static struct ibm_struct video_driver_data = {
5203 .name = "video",
5204 .read = video_read,
5205 .write = video_write,
5206 .exit = video_exit,
5209 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5211 /*************************************************************************
5212 * Keyboard backlight subdriver
5215 static enum led_brightness kbdlight_brightness;
5216 static DEFINE_MUTEX(kbdlight_mutex);
5218 static int kbdlight_set_level(int level)
5220 int ret = 0;
5222 if (!hkey_handle)
5223 return -ENXIO;
5225 mutex_lock(&kbdlight_mutex);
5227 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5228 ret = -EIO;
5229 else
5230 kbdlight_brightness = level;
5232 mutex_unlock(&kbdlight_mutex);
5234 return ret;
5237 static int kbdlight_get_level(void)
5239 int status = 0;
5241 if (!hkey_handle)
5242 return -ENXIO;
5244 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5245 return -EIO;
5247 if (status < 0)
5248 return status;
5250 return status & 0x3;
5253 static bool kbdlight_is_supported(void)
5255 int status = 0;
5257 if (!hkey_handle)
5258 return false;
5260 if (!acpi_has_method(hkey_handle, "MLCG")) {
5261 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5262 return false;
5265 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5266 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5267 return false;
5270 if (status < 0) {
5271 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5272 return false;
5275 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5277 * Guessed test for keyboard backlight:
5279 * Machines with backlight keyboard return:
5280 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5281 * b110100010010000000XX - ThinkPad x230
5282 * b010100000010000000XX - ThinkPad x240
5283 * b010100000010000000XX - ThinkPad W541
5284 * (XX is current backlight level)
5286 * Machines without backlight keyboard return:
5287 * b10100001000000000000 - ThinkPad x230
5288 * b10110001000000000000 - ThinkPad E430
5289 * b00000000000000000000 - ThinkPad E450
5291 * Candidate BITs for detection test (XOR):
5292 * b01000000001000000000
5295 return status & BIT(9);
5298 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5299 enum led_brightness brightness)
5301 return kbdlight_set_level(brightness);
5304 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5306 int level;
5308 level = kbdlight_get_level();
5309 if (level < 0)
5310 return 0;
5312 return level;
5315 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5316 .led_classdev = {
5317 .name = "tpacpi::kbd_backlight",
5318 .max_brightness = 2,
5319 .flags = LED_BRIGHT_HW_CHANGED,
5320 .brightness_set_blocking = &kbdlight_sysfs_set,
5321 .brightness_get = &kbdlight_sysfs_get,
5325 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5327 int rc;
5329 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5331 TPACPI_ACPIHANDLE_INIT(hkey);
5333 if (!kbdlight_is_supported()) {
5334 tp_features.kbdlight = 0;
5335 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5336 return 1;
5339 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5340 tp_features.kbdlight = 1;
5342 rc = led_classdev_register(&tpacpi_pdev->dev,
5343 &tpacpi_led_kbdlight.led_classdev);
5344 if (rc < 0) {
5345 tp_features.kbdlight = 0;
5346 return rc;
5349 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5350 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5351 return 0;
5354 static void kbdlight_exit(void)
5356 if (tp_features.kbdlight)
5357 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5360 static int kbdlight_set_level_and_update(int level)
5362 int ret;
5363 struct led_classdev *led_cdev;
5365 ret = kbdlight_set_level(level);
5366 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5368 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5369 led_cdev->brightness = level;
5371 return ret;
5374 static int kbdlight_read(struct seq_file *m)
5376 int level;
5378 if (!tp_features.kbdlight) {
5379 seq_printf(m, "status:\t\tnot supported\n");
5380 } else {
5381 level = kbdlight_get_level();
5382 if (level < 0)
5383 seq_printf(m, "status:\t\terror %d\n", level);
5384 else
5385 seq_printf(m, "status:\t\t%d\n", level);
5386 seq_printf(m, "commands:\t0, 1, 2\n");
5389 return 0;
5392 static int kbdlight_write(char *buf)
5394 char *cmd;
5395 int level = -1;
5397 if (!tp_features.kbdlight)
5398 return -ENODEV;
5400 while ((cmd = next_cmd(&buf))) {
5401 if (strlencmp(cmd, "0") == 0)
5402 level = 0;
5403 else if (strlencmp(cmd, "1") == 0)
5404 level = 1;
5405 else if (strlencmp(cmd, "2") == 0)
5406 level = 2;
5407 else
5408 return -EINVAL;
5411 if (level == -1)
5412 return -EINVAL;
5414 return kbdlight_set_level_and_update(level);
5417 static void kbdlight_suspend(void)
5419 struct led_classdev *led_cdev;
5421 if (!tp_features.kbdlight)
5422 return;
5424 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5425 led_update_brightness(led_cdev);
5426 led_classdev_suspend(led_cdev);
5429 static void kbdlight_resume(void)
5431 if (!tp_features.kbdlight)
5432 return;
5434 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5437 static struct ibm_struct kbdlight_driver_data = {
5438 .name = "kbdlight",
5439 .read = kbdlight_read,
5440 .write = kbdlight_write,
5441 .suspend = kbdlight_suspend,
5442 .resume = kbdlight_resume,
5443 .exit = kbdlight_exit,
5446 /*************************************************************************
5447 * Light (thinklight) subdriver
5450 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5451 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5453 static int light_get_status(void)
5455 int status = 0;
5457 if (tp_features.light_status) {
5458 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5459 return -EIO;
5460 return (!!status);
5463 return -ENXIO;
5466 static int light_set_status(int status)
5468 int rc;
5470 if (tp_features.light) {
5471 if (cmos_handle) {
5472 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5473 (status) ?
5474 TP_CMOS_THINKLIGHT_ON :
5475 TP_CMOS_THINKLIGHT_OFF);
5476 } else {
5477 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5478 (status) ? 1 : 0);
5480 return (rc) ? 0 : -EIO;
5483 return -ENXIO;
5486 static int light_sysfs_set(struct led_classdev *led_cdev,
5487 enum led_brightness brightness)
5489 return light_set_status((brightness != LED_OFF) ?
5490 TPACPI_LED_ON : TPACPI_LED_OFF);
5493 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5495 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5498 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5499 .led_classdev = {
5500 .name = "tpacpi::thinklight",
5501 .brightness_set_blocking = &light_sysfs_set,
5502 .brightness_get = &light_sysfs_get,
5506 static int __init light_init(struct ibm_init_struct *iibm)
5508 int rc;
5510 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5512 if (tpacpi_is_ibm()) {
5513 TPACPI_ACPIHANDLE_INIT(ledb);
5514 TPACPI_ACPIHANDLE_INIT(lght);
5516 TPACPI_ACPIHANDLE_INIT(cmos);
5518 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5519 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5521 if (tp_features.light)
5522 /* light status not supported on
5523 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5524 tp_features.light_status =
5525 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5527 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5528 str_supported(tp_features.light),
5529 str_supported(tp_features.light_status));
5531 if (!tp_features.light)
5532 return 1;
5534 rc = led_classdev_register(&tpacpi_pdev->dev,
5535 &tpacpi_led_thinklight.led_classdev);
5537 if (rc < 0) {
5538 tp_features.light = 0;
5539 tp_features.light_status = 0;
5540 } else {
5541 rc = 0;
5544 return rc;
5547 static void light_exit(void)
5549 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5552 static int light_read(struct seq_file *m)
5554 int status;
5556 if (!tp_features.light) {
5557 seq_printf(m, "status:\t\tnot supported\n");
5558 } else if (!tp_features.light_status) {
5559 seq_printf(m, "status:\t\tunknown\n");
5560 seq_printf(m, "commands:\ton, off\n");
5561 } else {
5562 status = light_get_status();
5563 if (status < 0)
5564 return status;
5565 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5566 seq_printf(m, "commands:\ton, off\n");
5569 return 0;
5572 static int light_write(char *buf)
5574 char *cmd;
5575 int newstatus = 0;
5577 if (!tp_features.light)
5578 return -ENODEV;
5580 while ((cmd = next_cmd(&buf))) {
5581 if (strlencmp(cmd, "on") == 0) {
5582 newstatus = 1;
5583 } else if (strlencmp(cmd, "off") == 0) {
5584 newstatus = 0;
5585 } else
5586 return -EINVAL;
5589 return light_set_status(newstatus);
5592 static struct ibm_struct light_driver_data = {
5593 .name = "light",
5594 .read = light_read,
5595 .write = light_write,
5596 .exit = light_exit,
5599 /*************************************************************************
5600 * CMOS subdriver
5603 /* sysfs cmos_command -------------------------------------------------- */
5604 static ssize_t cmos_command_store(struct device *dev,
5605 struct device_attribute *attr,
5606 const char *buf, size_t count)
5608 unsigned long cmos_cmd;
5609 int res;
5611 if (parse_strtoul(buf, 21, &cmos_cmd))
5612 return -EINVAL;
5614 res = issue_thinkpad_cmos_command(cmos_cmd);
5615 return (res) ? res : count;
5618 static DEVICE_ATTR_WO(cmos_command);
5620 /* --------------------------------------------------------------------- */
5622 static int __init cmos_init(struct ibm_init_struct *iibm)
5624 int res;
5626 vdbg_printk(TPACPI_DBG_INIT,
5627 "initializing cmos commands subdriver\n");
5629 TPACPI_ACPIHANDLE_INIT(cmos);
5631 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5632 str_supported(cmos_handle != NULL));
5634 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5635 if (res)
5636 return res;
5638 return (cmos_handle) ? 0 : 1;
5641 static void cmos_exit(void)
5643 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5646 static int cmos_read(struct seq_file *m)
5648 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5649 R30, R31, T20-22, X20-21 */
5650 if (!cmos_handle)
5651 seq_printf(m, "status:\t\tnot supported\n");
5652 else {
5653 seq_printf(m, "status:\t\tsupported\n");
5654 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5657 return 0;
5660 static int cmos_write(char *buf)
5662 char *cmd;
5663 int cmos_cmd, res;
5665 while ((cmd = next_cmd(&buf))) {
5666 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5667 cmos_cmd >= 0 && cmos_cmd <= 21) {
5668 /* cmos_cmd set */
5669 } else
5670 return -EINVAL;
5672 res = issue_thinkpad_cmos_command(cmos_cmd);
5673 if (res)
5674 return res;
5677 return 0;
5680 static struct ibm_struct cmos_driver_data = {
5681 .name = "cmos",
5682 .read = cmos_read,
5683 .write = cmos_write,
5684 .exit = cmos_exit,
5687 /*************************************************************************
5688 * LED subdriver
5691 enum led_access_mode {
5692 TPACPI_LED_NONE = 0,
5693 TPACPI_LED_570, /* 570 */
5694 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5695 TPACPI_LED_NEW, /* all others */
5698 enum { /* For TPACPI_LED_OLD */
5699 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5700 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5701 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5704 static enum led_access_mode led_supported;
5706 static acpi_handle led_handle;
5708 #define TPACPI_LED_NUMLEDS 16
5709 static struct tpacpi_led_classdev *tpacpi_leds;
5710 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5711 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5712 /* there's a limit of 19 chars + NULL before 2.6.26 */
5713 "tpacpi::power",
5714 "tpacpi:orange:batt",
5715 "tpacpi:green:batt",
5716 "tpacpi::dock_active",
5717 "tpacpi::bay_active",
5718 "tpacpi::dock_batt",
5719 "tpacpi::unknown_led",
5720 "tpacpi::standby",
5721 "tpacpi::dock_status1",
5722 "tpacpi::dock_status2",
5723 "tpacpi::unknown_led2",
5724 "tpacpi::unknown_led3",
5725 "tpacpi::thinkvantage",
5727 #define TPACPI_SAFE_LEDS 0x1081U
5729 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5731 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5732 return false;
5733 #else
5734 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5735 #endif
5738 static int led_get_status(const unsigned int led)
5740 int status;
5741 enum led_status_t led_s;
5743 switch (led_supported) {
5744 case TPACPI_LED_570:
5745 if (!acpi_evalf(ec_handle,
5746 &status, "GLED", "dd", 1 << led))
5747 return -EIO;
5748 led_s = (status == 0) ?
5749 TPACPI_LED_OFF :
5750 ((status == 1) ?
5751 TPACPI_LED_ON :
5752 TPACPI_LED_BLINK);
5753 tpacpi_led_state_cache[led] = led_s;
5754 return led_s;
5755 default:
5756 return -ENXIO;
5759 /* not reached */
5762 static int led_set_status(const unsigned int led,
5763 const enum led_status_t ledstatus)
5765 /* off, on, blink. Index is led_status_t */
5766 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5767 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5769 int rc = 0;
5771 switch (led_supported) {
5772 case TPACPI_LED_570:
5773 /* 570 */
5774 if (unlikely(led > 7))
5775 return -EINVAL;
5776 if (unlikely(tpacpi_is_led_restricted(led)))
5777 return -EPERM;
5778 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5779 (1 << led), led_sled_arg1[ledstatus]))
5780 rc = -EIO;
5781 break;
5782 case TPACPI_LED_OLD:
5783 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5784 if (unlikely(led > 7))
5785 return -EINVAL;
5786 if (unlikely(tpacpi_is_led_restricted(led)))
5787 return -EPERM;
5788 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5789 if (rc >= 0)
5790 rc = ec_write(TPACPI_LED_EC_HLBL,
5791 (ledstatus == TPACPI_LED_BLINK) << led);
5792 if (rc >= 0)
5793 rc = ec_write(TPACPI_LED_EC_HLCL,
5794 (ledstatus != TPACPI_LED_OFF) << led);
5795 break;
5796 case TPACPI_LED_NEW:
5797 /* all others */
5798 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5799 return -EINVAL;
5800 if (unlikely(tpacpi_is_led_restricted(led)))
5801 return -EPERM;
5802 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5803 led, led_led_arg1[ledstatus]))
5804 rc = -EIO;
5805 break;
5806 default:
5807 rc = -ENXIO;
5810 if (!rc)
5811 tpacpi_led_state_cache[led] = ledstatus;
5813 return rc;
5816 static int led_sysfs_set(struct led_classdev *led_cdev,
5817 enum led_brightness brightness)
5819 struct tpacpi_led_classdev *data = container_of(led_cdev,
5820 struct tpacpi_led_classdev, led_classdev);
5821 enum led_status_t new_state;
5823 if (brightness == LED_OFF)
5824 new_state = TPACPI_LED_OFF;
5825 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5826 new_state = TPACPI_LED_ON;
5827 else
5828 new_state = TPACPI_LED_BLINK;
5830 return led_set_status(data->led, new_state);
5833 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5834 unsigned long *delay_on, unsigned long *delay_off)
5836 struct tpacpi_led_classdev *data = container_of(led_cdev,
5837 struct tpacpi_led_classdev, led_classdev);
5839 /* Can we choose the flash rate? */
5840 if (*delay_on == 0 && *delay_off == 0) {
5841 /* yes. set them to the hardware blink rate (1 Hz) */
5842 *delay_on = 500; /* ms */
5843 *delay_off = 500; /* ms */
5844 } else if ((*delay_on != 500) || (*delay_off != 500))
5845 return -EINVAL;
5847 return led_set_status(data->led, TPACPI_LED_BLINK);
5850 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5852 int rc;
5854 struct tpacpi_led_classdev *data = container_of(led_cdev,
5855 struct tpacpi_led_classdev, led_classdev);
5857 rc = led_get_status(data->led);
5859 if (rc == TPACPI_LED_OFF || rc < 0)
5860 rc = LED_OFF; /* no error handling in led class :( */
5861 else
5862 rc = LED_FULL;
5864 return rc;
5867 static void led_exit(void)
5869 unsigned int i;
5871 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5872 if (tpacpi_leds[i].led_classdev.name)
5873 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5876 kfree(tpacpi_leds);
5879 static int __init tpacpi_init_led(unsigned int led)
5881 int rc;
5883 tpacpi_leds[led].led = led;
5885 /* LEDs with no name don't get registered */
5886 if (!tpacpi_led_names[led])
5887 return 0;
5889 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5890 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5891 if (led_supported == TPACPI_LED_570)
5892 tpacpi_leds[led].led_classdev.brightness_get =
5893 &led_sysfs_get;
5895 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5897 rc = led_classdev_register(&tpacpi_pdev->dev,
5898 &tpacpi_leds[led].led_classdev);
5899 if (rc < 0)
5900 tpacpi_leds[led].led_classdev.name = NULL;
5902 return rc;
5905 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5906 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5907 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5908 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5910 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5911 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5912 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5913 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5914 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5915 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5916 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5917 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5919 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5920 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5921 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5922 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5923 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5925 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5926 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5927 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5928 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5930 /* (1) - may have excess leds enabled on MSB */
5932 /* Defaults (order matters, keep last, don't reorder!) */
5933 { /* Lenovo */
5934 .vendor = PCI_VENDOR_ID_LENOVO,
5935 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5936 .quirks = 0x1fffU,
5938 { /* IBM ThinkPads with no EC version string */
5939 .vendor = PCI_VENDOR_ID_IBM,
5940 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5941 .quirks = 0x00ffU,
5943 { /* IBM ThinkPads with EC version string */
5944 .vendor = PCI_VENDOR_ID_IBM,
5945 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5946 .quirks = 0x00bfU,
5950 #undef TPACPI_LEDQ_IBM
5951 #undef TPACPI_LEDQ_LNV
5953 static enum led_access_mode __init led_init_detect_mode(void)
5955 acpi_status status;
5957 if (tpacpi_is_ibm()) {
5958 /* 570 */
5959 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5960 if (ACPI_SUCCESS(status))
5961 return TPACPI_LED_570;
5963 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5964 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5965 if (ACPI_SUCCESS(status))
5966 return TPACPI_LED_OLD;
5969 /* most others */
5970 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5971 if (ACPI_SUCCESS(status))
5972 return TPACPI_LED_NEW;
5974 /* R30, R31, and unknown firmwares */
5975 led_handle = NULL;
5976 return TPACPI_LED_NONE;
5979 static int __init led_init(struct ibm_init_struct *iibm)
5981 unsigned int i;
5982 int rc;
5983 unsigned long useful_leds;
5985 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5987 led_supported = led_init_detect_mode();
5989 if (led_supported != TPACPI_LED_NONE) {
5990 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5991 ARRAY_SIZE(led_useful_qtable));
5993 if (!useful_leds) {
5994 led_handle = NULL;
5995 led_supported = TPACPI_LED_NONE;
5999 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6000 str_supported(led_supported), led_supported);
6002 if (led_supported == TPACPI_LED_NONE)
6003 return 1;
6005 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
6006 GFP_KERNEL);
6007 if (!tpacpi_leds) {
6008 pr_err("Out of memory for LED data\n");
6009 return -ENOMEM;
6012 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6013 tpacpi_leds[i].led = -1;
6015 if (!tpacpi_is_led_restricted(i) &&
6016 test_bit(i, &useful_leds)) {
6017 rc = tpacpi_init_led(i);
6018 if (rc < 0) {
6019 led_exit();
6020 return rc;
6025 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6026 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6027 #endif
6028 return 0;
6031 #define str_led_status(s) \
6032 ((s) == TPACPI_LED_OFF ? "off" : \
6033 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6035 static int led_read(struct seq_file *m)
6037 if (!led_supported) {
6038 seq_printf(m, "status:\t\tnot supported\n");
6039 return 0;
6041 seq_printf(m, "status:\t\tsupported\n");
6043 if (led_supported == TPACPI_LED_570) {
6044 /* 570 */
6045 int i, status;
6046 for (i = 0; i < 8; i++) {
6047 status = led_get_status(i);
6048 if (status < 0)
6049 return -EIO;
6050 seq_printf(m, "%d:\t\t%s\n",
6051 i, str_led_status(status));
6055 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6057 return 0;
6060 static int led_write(char *buf)
6062 char *cmd;
6063 int led, rc;
6064 enum led_status_t s;
6066 if (!led_supported)
6067 return -ENODEV;
6069 while ((cmd = next_cmd(&buf))) {
6070 if (sscanf(cmd, "%d", &led) != 1)
6071 return -EINVAL;
6073 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6074 tpacpi_leds[led].led < 0)
6075 return -ENODEV;
6077 if (strstr(cmd, "off")) {
6078 s = TPACPI_LED_OFF;
6079 } else if (strstr(cmd, "on")) {
6080 s = TPACPI_LED_ON;
6081 } else if (strstr(cmd, "blink")) {
6082 s = TPACPI_LED_BLINK;
6083 } else {
6084 return -EINVAL;
6087 rc = led_set_status(led, s);
6088 if (rc < 0)
6089 return rc;
6092 return 0;
6095 static struct ibm_struct led_driver_data = {
6096 .name = "led",
6097 .read = led_read,
6098 .write = led_write,
6099 .exit = led_exit,
6102 /*************************************************************************
6103 * Beep subdriver
6106 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6108 #define TPACPI_BEEP_Q1 0x0001
6110 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6111 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6112 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6115 static int __init beep_init(struct ibm_init_struct *iibm)
6117 unsigned long quirks;
6119 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6121 TPACPI_ACPIHANDLE_INIT(beep);
6123 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6124 str_supported(beep_handle != NULL));
6126 quirks = tpacpi_check_quirks(beep_quirk_table,
6127 ARRAY_SIZE(beep_quirk_table));
6129 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6131 return (beep_handle) ? 0 : 1;
6134 static int beep_read(struct seq_file *m)
6136 if (!beep_handle)
6137 seq_printf(m, "status:\t\tnot supported\n");
6138 else {
6139 seq_printf(m, "status:\t\tsupported\n");
6140 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6143 return 0;
6146 static int beep_write(char *buf)
6148 char *cmd;
6149 int beep_cmd;
6151 if (!beep_handle)
6152 return -ENODEV;
6154 while ((cmd = next_cmd(&buf))) {
6155 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6156 beep_cmd >= 0 && beep_cmd <= 17) {
6157 /* beep_cmd set */
6158 } else
6159 return -EINVAL;
6160 if (tp_features.beep_needs_two_args) {
6161 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6162 beep_cmd, 0))
6163 return -EIO;
6164 } else {
6165 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6166 beep_cmd))
6167 return -EIO;
6171 return 0;
6174 static struct ibm_struct beep_driver_data = {
6175 .name = "beep",
6176 .read = beep_read,
6177 .write = beep_write,
6180 /*************************************************************************
6181 * Thermal subdriver
6184 enum thermal_access_mode {
6185 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6186 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6187 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6188 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6189 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6192 enum { /* TPACPI_THERMAL_TPEC_* */
6193 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6194 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6195 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6197 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6201 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6202 struct ibm_thermal_sensors_struct {
6203 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6206 static enum thermal_access_mode thermal_read_mode;
6208 /* idx is zero-based */
6209 static int thermal_get_sensor(int idx, s32 *value)
6211 int t;
6212 s8 tmp;
6213 char tmpi[5];
6215 t = TP_EC_THERMAL_TMP0;
6217 switch (thermal_read_mode) {
6218 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6219 case TPACPI_THERMAL_TPEC_16:
6220 if (idx >= 8 && idx <= 15) {
6221 t = TP_EC_THERMAL_TMP8;
6222 idx -= 8;
6224 /* fallthrough */
6225 #endif
6226 case TPACPI_THERMAL_TPEC_8:
6227 if (idx <= 7) {
6228 if (!acpi_ec_read(t + idx, &tmp))
6229 return -EIO;
6230 *value = tmp * 1000;
6231 return 0;
6233 break;
6235 case TPACPI_THERMAL_ACPI_UPDT:
6236 if (idx <= 7) {
6237 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6238 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6239 return -EIO;
6240 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6241 return -EIO;
6242 *value = (t - 2732) * 100;
6243 return 0;
6245 break;
6247 case TPACPI_THERMAL_ACPI_TMP07:
6248 if (idx <= 7) {
6249 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6250 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6251 return -EIO;
6252 if (t > 127 || t < -127)
6253 t = TP_EC_THERMAL_TMP_NA;
6254 *value = t * 1000;
6255 return 0;
6257 break;
6259 case TPACPI_THERMAL_NONE:
6260 default:
6261 return -ENOSYS;
6264 return -EINVAL;
6267 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6269 int res, i;
6270 int n;
6272 n = 8;
6273 i = 0;
6275 if (!s)
6276 return -EINVAL;
6278 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6279 n = 16;
6281 for (i = 0 ; i < n; i++) {
6282 res = thermal_get_sensor(i, &s->temp[i]);
6283 if (res)
6284 return res;
6287 return n;
6290 static void thermal_dump_all_sensors(void)
6292 int n, i;
6293 struct ibm_thermal_sensors_struct t;
6295 n = thermal_get_sensors(&t);
6296 if (n <= 0)
6297 return;
6299 pr_notice("temperatures (Celsius):");
6301 for (i = 0; i < n; i++) {
6302 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6303 pr_cont(" %d", (int)(t.temp[i] / 1000));
6304 else
6305 pr_cont(" N/A");
6308 pr_cont("\n");
6311 /* sysfs temp##_input -------------------------------------------------- */
6313 static ssize_t thermal_temp_input_show(struct device *dev,
6314 struct device_attribute *attr,
6315 char *buf)
6317 struct sensor_device_attribute *sensor_attr =
6318 to_sensor_dev_attr(attr);
6319 int idx = sensor_attr->index;
6320 s32 value;
6321 int res;
6323 res = thermal_get_sensor(idx, &value);
6324 if (res)
6325 return res;
6326 if (value == TPACPI_THERMAL_SENSOR_NA)
6327 return -ENXIO;
6329 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6332 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6333 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6334 thermal_temp_input_show, NULL, _idxB)
6336 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6337 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6338 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6339 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6340 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6341 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6342 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6343 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6344 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6345 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6346 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6347 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6348 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6349 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6350 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6351 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6352 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6355 #define THERMAL_ATTRS(X) \
6356 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6358 static struct attribute *thermal_temp_input_attr[] = {
6359 THERMAL_ATTRS(8),
6360 THERMAL_ATTRS(9),
6361 THERMAL_ATTRS(10),
6362 THERMAL_ATTRS(11),
6363 THERMAL_ATTRS(12),
6364 THERMAL_ATTRS(13),
6365 THERMAL_ATTRS(14),
6366 THERMAL_ATTRS(15),
6367 THERMAL_ATTRS(0),
6368 THERMAL_ATTRS(1),
6369 THERMAL_ATTRS(2),
6370 THERMAL_ATTRS(3),
6371 THERMAL_ATTRS(4),
6372 THERMAL_ATTRS(5),
6373 THERMAL_ATTRS(6),
6374 THERMAL_ATTRS(7),
6375 NULL
6378 static const struct attribute_group thermal_temp_input16_group = {
6379 .attrs = thermal_temp_input_attr
6382 static const struct attribute_group thermal_temp_input8_group = {
6383 .attrs = &thermal_temp_input_attr[8]
6386 #undef THERMAL_SENSOR_ATTR_TEMP
6387 #undef THERMAL_ATTRS
6389 /* --------------------------------------------------------------------- */
6391 static int __init thermal_init(struct ibm_init_struct *iibm)
6393 u8 t, ta1, ta2;
6394 int i;
6395 int acpi_tmp7;
6396 int res;
6398 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6400 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6402 if (thinkpad_id.ec_model) {
6404 * Direct EC access mode: sensors at registers
6405 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6406 * non-implemented, thermal sensors return 0x80 when
6407 * not available
6410 ta1 = ta2 = 0;
6411 for (i = 0; i < 8; i++) {
6412 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6413 ta1 |= t;
6414 } else {
6415 ta1 = 0;
6416 break;
6418 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6419 ta2 |= t;
6420 } else {
6421 ta1 = 0;
6422 break;
6425 if (ta1 == 0) {
6426 /* This is sheer paranoia, but we handle it anyway */
6427 if (acpi_tmp7) {
6428 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6429 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6430 } else {
6431 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6432 thermal_read_mode = TPACPI_THERMAL_NONE;
6434 } else {
6435 thermal_read_mode =
6436 (ta2 != 0) ?
6437 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6439 } else if (acpi_tmp7) {
6440 if (tpacpi_is_ibm() &&
6441 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6442 /* 600e/x, 770e, 770x */
6443 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6444 } else {
6445 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6446 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6448 } else {
6449 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6450 thermal_read_mode = TPACPI_THERMAL_NONE;
6453 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6454 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6455 thermal_read_mode);
6457 switch (thermal_read_mode) {
6458 case TPACPI_THERMAL_TPEC_16:
6459 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6460 &thermal_temp_input16_group);
6461 if (res)
6462 return res;
6463 break;
6464 case TPACPI_THERMAL_TPEC_8:
6465 case TPACPI_THERMAL_ACPI_TMP07:
6466 case TPACPI_THERMAL_ACPI_UPDT:
6467 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6468 &thermal_temp_input8_group);
6469 if (res)
6470 return res;
6471 break;
6472 case TPACPI_THERMAL_NONE:
6473 default:
6474 return 1;
6477 return 0;
6480 static void thermal_exit(void)
6482 switch (thermal_read_mode) {
6483 case TPACPI_THERMAL_TPEC_16:
6484 sysfs_remove_group(&tpacpi_hwmon->kobj,
6485 &thermal_temp_input16_group);
6486 break;
6487 case TPACPI_THERMAL_TPEC_8:
6488 case TPACPI_THERMAL_ACPI_TMP07:
6489 case TPACPI_THERMAL_ACPI_UPDT:
6490 sysfs_remove_group(&tpacpi_hwmon->kobj,
6491 &thermal_temp_input8_group);
6492 break;
6493 case TPACPI_THERMAL_NONE:
6494 default:
6495 break;
6499 static int thermal_read(struct seq_file *m)
6501 int n, i;
6502 struct ibm_thermal_sensors_struct t;
6504 n = thermal_get_sensors(&t);
6505 if (unlikely(n < 0))
6506 return n;
6508 seq_printf(m, "temperatures:\t");
6510 if (n > 0) {
6511 for (i = 0; i < (n - 1); i++)
6512 seq_printf(m, "%d ", t.temp[i] / 1000);
6513 seq_printf(m, "%d\n", t.temp[i] / 1000);
6514 } else
6515 seq_printf(m, "not supported\n");
6517 return 0;
6520 static struct ibm_struct thermal_driver_data = {
6521 .name = "thermal",
6522 .read = thermal_read,
6523 .exit = thermal_exit,
6526 /*************************************************************************
6527 * Backlight/brightness subdriver
6530 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6533 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6534 * CMOS NVRAM byte 0x5E, bits 0-3.
6536 * EC HBRV (0x31) has the following layout
6537 * Bit 7: unknown function
6538 * Bit 6: unknown function
6539 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6540 * Bit 4: must be set to zero to avoid problems
6541 * Bit 3-0: backlight brightness level
6543 * brightness_get_raw returns status data in the HBRV layout
6545 * WARNING: The X61 has been verified to use HBRV for something else, so
6546 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6547 * testing on the very early *60 Lenovo models...
6550 enum {
6551 TP_EC_BACKLIGHT = 0x31,
6553 /* TP_EC_BACKLIGHT bitmasks */
6554 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6555 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6556 TP_EC_BACKLIGHT_MAPSW = 0x20,
6559 enum tpacpi_brightness_access_mode {
6560 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6561 TPACPI_BRGHT_MODE_EC, /* EC control */
6562 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6563 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6564 TPACPI_BRGHT_MODE_MAX
6567 static struct backlight_device *ibm_backlight_device;
6569 static enum tpacpi_brightness_access_mode brightness_mode =
6570 TPACPI_BRGHT_MODE_MAX;
6572 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6574 static struct mutex brightness_mutex;
6576 /* NVRAM brightness access,
6577 * call with brightness_mutex held! */
6578 static unsigned int tpacpi_brightness_nvram_get(void)
6580 u8 lnvram;
6582 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6583 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6584 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6585 lnvram &= bright_maxlvl;
6587 return lnvram;
6590 static void tpacpi_brightness_checkpoint_nvram(void)
6592 u8 lec = 0;
6593 u8 b_nvram;
6595 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6596 return;
6598 vdbg_printk(TPACPI_DBG_BRGHT,
6599 "trying to checkpoint backlight level to NVRAM...\n");
6601 if (mutex_lock_killable(&brightness_mutex) < 0)
6602 return;
6604 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6605 goto unlock;
6606 lec &= TP_EC_BACKLIGHT_LVLMSK;
6607 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6609 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6610 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6611 /* NVRAM needs update */
6612 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6613 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6614 b_nvram |= lec;
6615 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6616 dbg_printk(TPACPI_DBG_BRGHT,
6617 "updated NVRAM backlight level to %u (0x%02x)\n",
6618 (unsigned int) lec, (unsigned int) b_nvram);
6619 } else
6620 vdbg_printk(TPACPI_DBG_BRGHT,
6621 "NVRAM backlight level already is %u (0x%02x)\n",
6622 (unsigned int) lec, (unsigned int) b_nvram);
6624 unlock:
6625 mutex_unlock(&brightness_mutex);
6629 /* call with brightness_mutex held! */
6630 static int tpacpi_brightness_get_raw(int *status)
6632 u8 lec = 0;
6634 switch (brightness_mode) {
6635 case TPACPI_BRGHT_MODE_UCMS_STEP:
6636 *status = tpacpi_brightness_nvram_get();
6637 return 0;
6638 case TPACPI_BRGHT_MODE_EC:
6639 case TPACPI_BRGHT_MODE_ECNVRAM:
6640 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6641 return -EIO;
6642 *status = lec;
6643 return 0;
6644 default:
6645 return -ENXIO;
6649 /* call with brightness_mutex held! */
6650 /* do NOT call with illegal backlight level value */
6651 static int tpacpi_brightness_set_ec(unsigned int value)
6653 u8 lec = 0;
6655 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6656 return -EIO;
6658 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6659 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6660 (value & TP_EC_BACKLIGHT_LVLMSK))))
6661 return -EIO;
6663 return 0;
6666 /* call with brightness_mutex held! */
6667 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6669 int cmos_cmd, inc;
6670 unsigned int current_value, i;
6672 current_value = tpacpi_brightness_nvram_get();
6674 if (value == current_value)
6675 return 0;
6677 cmos_cmd = (value > current_value) ?
6678 TP_CMOS_BRIGHTNESS_UP :
6679 TP_CMOS_BRIGHTNESS_DOWN;
6680 inc = (value > current_value) ? 1 : -1;
6682 for (i = current_value; i != value; i += inc)
6683 if (issue_thinkpad_cmos_command(cmos_cmd))
6684 return -EIO;
6686 return 0;
6689 /* May return EINTR which can always be mapped to ERESTARTSYS */
6690 static int brightness_set(unsigned int value)
6692 int res;
6694 if (value > bright_maxlvl)
6695 return -EINVAL;
6697 vdbg_printk(TPACPI_DBG_BRGHT,
6698 "set backlight level to %d\n", value);
6700 res = mutex_lock_killable(&brightness_mutex);
6701 if (res < 0)
6702 return res;
6704 switch (brightness_mode) {
6705 case TPACPI_BRGHT_MODE_EC:
6706 case TPACPI_BRGHT_MODE_ECNVRAM:
6707 res = tpacpi_brightness_set_ec(value);
6708 break;
6709 case TPACPI_BRGHT_MODE_UCMS_STEP:
6710 res = tpacpi_brightness_set_ucmsstep(value);
6711 break;
6712 default:
6713 res = -ENXIO;
6716 mutex_unlock(&brightness_mutex);
6717 return res;
6720 /* sysfs backlight class ----------------------------------------------- */
6722 static int brightness_update_status(struct backlight_device *bd)
6724 unsigned int level =
6725 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6726 bd->props.power == FB_BLANK_UNBLANK) ?
6727 bd->props.brightness : 0;
6729 dbg_printk(TPACPI_DBG_BRGHT,
6730 "backlight: attempt to set level to %d\n",
6731 level);
6733 /* it is the backlight class's job (caller) to handle
6734 * EINTR and other errors properly */
6735 return brightness_set(level);
6738 static int brightness_get(struct backlight_device *bd)
6740 int status, res;
6742 res = mutex_lock_killable(&brightness_mutex);
6743 if (res < 0)
6744 return 0;
6746 res = tpacpi_brightness_get_raw(&status);
6748 mutex_unlock(&brightness_mutex);
6750 if (res < 0)
6751 return 0;
6753 return status & TP_EC_BACKLIGHT_LVLMSK;
6756 static void tpacpi_brightness_notify_change(void)
6758 backlight_force_update(ibm_backlight_device,
6759 BACKLIGHT_UPDATE_HOTKEY);
6762 static const struct backlight_ops ibm_backlight_data = {
6763 .get_brightness = brightness_get,
6764 .update_status = brightness_update_status,
6767 /* --------------------------------------------------------------------- */
6770 * Call _BCL method of video device. On some ThinkPads this will
6771 * switch the firmware to the ACPI brightness control mode.
6774 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6776 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6777 union acpi_object *obj;
6778 struct acpi_device *device, *child;
6779 int rc;
6781 if (acpi_bus_get_device(handle, &device))
6782 return 0;
6784 rc = 0;
6785 list_for_each_entry(child, &device->children, node) {
6786 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6787 NULL, &buffer);
6788 if (ACPI_FAILURE(status))
6789 continue;
6791 obj = (union acpi_object *)buffer.pointer;
6792 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6793 pr_err("Unknown _BCL data, please report this to %s\n",
6794 TPACPI_MAIL);
6795 rc = 0;
6796 } else {
6797 rc = obj->package.count;
6799 break;
6802 kfree(buffer.pointer);
6803 return rc;
6808 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6810 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6812 acpi_handle video_device;
6813 int bcl_levels = 0;
6815 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6816 if (video_device)
6817 bcl_levels = tpacpi_query_bcl_levels(video_device);
6819 tp_features.bright_acpimode = (bcl_levels > 0);
6821 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6825 * These are only useful for models that have only one possibility
6826 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6827 * these quirks.
6829 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6830 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6831 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6833 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6834 /* Models with ATI GPUs known to require ECNVRAM mode */
6835 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6837 /* Models with ATI GPUs that can use ECNVRAM */
6838 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6839 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6840 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6841 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6843 /* Models with Intel Extreme Graphics 2 */
6844 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6845 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6846 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6848 /* Models with Intel GMA900 */
6849 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6850 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6851 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6855 * Returns < 0 for error, otherwise sets tp_features.bright_*
6856 * and bright_maxlvl.
6858 static void __init tpacpi_detect_brightness_capabilities(void)
6860 unsigned int b;
6862 vdbg_printk(TPACPI_DBG_INIT,
6863 "detecting firmware brightness interface capabilities\n");
6865 /* we could run a quirks check here (same table used by
6866 * brightness_init) if needed */
6869 * We always attempt to detect acpi support, so as to switch
6870 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6871 * going to publish a backlight interface
6873 b = tpacpi_check_std_acpi_brightness_support();
6874 switch (b) {
6875 case 16:
6876 bright_maxlvl = 15;
6877 break;
6878 case 8:
6879 case 0:
6880 bright_maxlvl = 7;
6881 break;
6882 default:
6883 tp_features.bright_unkfw = 1;
6884 bright_maxlvl = b - 1;
6886 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6889 static int __init brightness_init(struct ibm_init_struct *iibm)
6891 struct backlight_properties props;
6892 int b;
6893 unsigned long quirks;
6895 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6897 mutex_init(&brightness_mutex);
6899 quirks = tpacpi_check_quirks(brightness_quirk_table,
6900 ARRAY_SIZE(brightness_quirk_table));
6902 /* tpacpi_detect_brightness_capabilities() must have run already */
6904 /* if it is unknown, we don't handle it: it wouldn't be safe */
6905 if (tp_features.bright_unkfw)
6906 return 1;
6908 if (!brightness_enable) {
6909 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6910 "brightness support disabled by module parameter\n");
6911 return 1;
6914 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6915 if (brightness_enable > 1) {
6916 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6917 return 1;
6918 } else if (brightness_enable == 1) {
6919 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6920 return 1;
6922 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6923 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
6927 * Check for module parameter bogosity, note that we
6928 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6929 * able to detect "unspecified"
6931 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6932 return -EINVAL;
6934 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6935 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6936 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6937 if (quirks & TPACPI_BRGHT_Q_EC)
6938 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6939 else
6940 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6942 dbg_printk(TPACPI_DBG_BRGHT,
6943 "driver auto-selected brightness_mode=%d\n",
6944 brightness_mode);
6947 /* Safety */
6948 if (!tpacpi_is_ibm() &&
6949 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6950 brightness_mode == TPACPI_BRGHT_MODE_EC))
6951 return -EINVAL;
6953 if (tpacpi_brightness_get_raw(&b) < 0)
6954 return 1;
6956 memset(&props, 0, sizeof(struct backlight_properties));
6957 props.type = BACKLIGHT_PLATFORM;
6958 props.max_brightness = bright_maxlvl;
6959 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6960 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6961 NULL, NULL,
6962 &ibm_backlight_data,
6963 &props);
6964 if (IS_ERR(ibm_backlight_device)) {
6965 int rc = PTR_ERR(ibm_backlight_device);
6966 ibm_backlight_device = NULL;
6967 pr_err("Could not register backlight device\n");
6968 return rc;
6970 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6971 "brightness is supported\n");
6973 if (quirks & TPACPI_BRGHT_Q_ASK) {
6974 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6975 brightness_mode);
6976 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6977 TPACPI_MAIL);
6980 /* Added by mistake in early 2007. Probably useless, but it could
6981 * be working around some unknown firmware problem where the value
6982 * read at startup doesn't match the real hardware state... so leave
6983 * it in place just in case */
6984 backlight_update_status(ibm_backlight_device);
6986 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6987 "brightness: registering brightness hotkeys as change notification\n");
6988 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6989 | TP_ACPI_HKEY_BRGHTUP_MASK
6990 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6991 return 0;
6994 static void brightness_suspend(void)
6996 tpacpi_brightness_checkpoint_nvram();
6999 static void brightness_shutdown(void)
7001 tpacpi_brightness_checkpoint_nvram();
7004 static void brightness_exit(void)
7006 if (ibm_backlight_device) {
7007 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7008 "calling backlight_device_unregister()\n");
7009 backlight_device_unregister(ibm_backlight_device);
7012 tpacpi_brightness_checkpoint_nvram();
7015 static int brightness_read(struct seq_file *m)
7017 int level;
7019 level = brightness_get(NULL);
7020 if (level < 0) {
7021 seq_printf(m, "level:\t\tunreadable\n");
7022 } else {
7023 seq_printf(m, "level:\t\t%d\n", level);
7024 seq_printf(m, "commands:\tup, down\n");
7025 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7026 bright_maxlvl);
7029 return 0;
7032 static int brightness_write(char *buf)
7034 int level;
7035 int rc;
7036 char *cmd;
7038 level = brightness_get(NULL);
7039 if (level < 0)
7040 return level;
7042 while ((cmd = next_cmd(&buf))) {
7043 if (strlencmp(cmd, "up") == 0) {
7044 if (level < bright_maxlvl)
7045 level++;
7046 } else if (strlencmp(cmd, "down") == 0) {
7047 if (level > 0)
7048 level--;
7049 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7050 level >= 0 && level <= bright_maxlvl) {
7051 /* new level set */
7052 } else
7053 return -EINVAL;
7056 tpacpi_disclose_usertask("procfs brightness",
7057 "set level to %d\n", level);
7060 * Now we know what the final level should be, so we try to set it.
7061 * Doing it this way makes the syscall restartable in case of EINTR
7063 rc = brightness_set(level);
7064 if (!rc && ibm_backlight_device)
7065 backlight_force_update(ibm_backlight_device,
7066 BACKLIGHT_UPDATE_SYSFS);
7067 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7070 static struct ibm_struct brightness_driver_data = {
7071 .name = "brightness",
7072 .read = brightness_read,
7073 .write = brightness_write,
7074 .exit = brightness_exit,
7075 .suspend = brightness_suspend,
7076 .shutdown = brightness_shutdown,
7079 /*************************************************************************
7080 * Volume subdriver
7084 * IBM ThinkPads have a simple volume controller with MUTE gating.
7085 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7087 * Since the *61 series (and probably also the later *60 series), Lenovo
7088 * ThinkPads only implement the MUTE gate.
7090 * EC register 0x30
7091 * Bit 6: MUTE (1 mutes sound)
7092 * Bit 3-0: Volume
7093 * Other bits should be zero as far as we know.
7095 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7096 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7097 * such as bit 7 which is used to detect repeated presses of MUTE,
7098 * and we leave them unchanged.
7100 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7101 * in response to user input. Unfortunately, this rarely works well.
7102 * The laptop changes the state of its internal MUTE gate and, on some
7103 * models, sends KEY_MUTE, causing any user code that responds to the
7104 * mute button to get confused. The hardware MUTE gate is also
7105 * unnecessary, since user code can handle the mute button without
7106 * kernel or EC help.
7108 * To avoid confusing userspace, we simply disable all EC-based mute
7109 * and volume controls when possible.
7112 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7114 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7115 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7116 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7118 #if SNDRV_CARDS <= 32
7119 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7120 #else
7121 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7122 #endif
7123 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7124 static char *alsa_id = "ThinkPadEC";
7125 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7127 struct tpacpi_alsa_data {
7128 struct snd_card *card;
7129 struct snd_ctl_elem_id *ctl_mute_id;
7130 struct snd_ctl_elem_id *ctl_vol_id;
7133 static struct snd_card *alsa_card;
7135 enum {
7136 TP_EC_AUDIO = 0x30,
7138 /* TP_EC_AUDIO bits */
7139 TP_EC_AUDIO_MUTESW = 6,
7141 /* TP_EC_AUDIO bitmasks */
7142 TP_EC_AUDIO_LVL_MSK = 0x0F,
7143 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7145 /* Maximum volume */
7146 TP_EC_VOLUME_MAX = 14,
7149 enum tpacpi_volume_access_mode {
7150 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7151 TPACPI_VOL_MODE_EC, /* Pure EC control */
7152 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7153 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7154 TPACPI_VOL_MODE_MAX
7157 enum tpacpi_volume_capabilities {
7158 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7159 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7160 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7161 TPACPI_VOL_CAP_MAX
7164 enum tpacpi_mute_btn_mode {
7165 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7166 /* We don't know what mode 1 is. */
7167 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7168 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7171 static enum tpacpi_volume_access_mode volume_mode =
7172 TPACPI_VOL_MODE_MAX;
7174 static enum tpacpi_volume_capabilities volume_capabilities;
7175 static bool volume_control_allowed;
7176 static bool software_mute_requested = true;
7177 static bool software_mute_active;
7178 static int software_mute_orig_mode;
7181 * Used to syncronize writers to TP_EC_AUDIO and
7182 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7184 static struct mutex volume_mutex;
7186 static void tpacpi_volume_checkpoint_nvram(void)
7188 u8 lec = 0;
7189 u8 b_nvram;
7190 u8 ec_mask;
7192 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7193 return;
7194 if (!volume_control_allowed)
7195 return;
7196 if (software_mute_active)
7197 return;
7199 vdbg_printk(TPACPI_DBG_MIXER,
7200 "trying to checkpoint mixer state to NVRAM...\n");
7202 if (tp_features.mixer_no_level_control)
7203 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7204 else
7205 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7207 if (mutex_lock_killable(&volume_mutex) < 0)
7208 return;
7210 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7211 goto unlock;
7212 lec &= ec_mask;
7213 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7215 if (lec != (b_nvram & ec_mask)) {
7216 /* NVRAM needs update */
7217 b_nvram &= ~ec_mask;
7218 b_nvram |= lec;
7219 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7220 dbg_printk(TPACPI_DBG_MIXER,
7221 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7222 (unsigned int) lec, (unsigned int) b_nvram);
7223 } else {
7224 vdbg_printk(TPACPI_DBG_MIXER,
7225 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7226 (unsigned int) lec, (unsigned int) b_nvram);
7229 unlock:
7230 mutex_unlock(&volume_mutex);
7233 static int volume_get_status_ec(u8 *status)
7235 u8 s;
7237 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7238 return -EIO;
7240 *status = s;
7242 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7244 return 0;
7247 static int volume_get_status(u8 *status)
7249 return volume_get_status_ec(status);
7252 static int volume_set_status_ec(const u8 status)
7254 if (!acpi_ec_write(TP_EC_AUDIO, status))
7255 return -EIO;
7257 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7260 * On X200s, and possibly on others, it can take a while for
7261 * reads to become correct.
7263 msleep(1);
7265 return 0;
7268 static int volume_set_status(const u8 status)
7270 return volume_set_status_ec(status);
7273 /* returns < 0 on error, 0 on no change, 1 on change */
7274 static int __volume_set_mute_ec(const bool mute)
7276 int rc;
7277 u8 s, n;
7279 if (mutex_lock_killable(&volume_mutex) < 0)
7280 return -EINTR;
7282 rc = volume_get_status_ec(&s);
7283 if (rc)
7284 goto unlock;
7286 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7287 s & ~TP_EC_AUDIO_MUTESW_MSK;
7289 if (n != s) {
7290 rc = volume_set_status_ec(n);
7291 if (!rc)
7292 rc = 1;
7295 unlock:
7296 mutex_unlock(&volume_mutex);
7297 return rc;
7300 static int volume_alsa_set_mute(const bool mute)
7302 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7303 (mute) ? "" : "un");
7304 return __volume_set_mute_ec(mute);
7307 static int volume_set_mute(const bool mute)
7309 int rc;
7311 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7312 (mute) ? "" : "un");
7314 rc = __volume_set_mute_ec(mute);
7315 return (rc < 0) ? rc : 0;
7318 /* returns < 0 on error, 0 on no change, 1 on change */
7319 static int __volume_set_volume_ec(const u8 vol)
7321 int rc;
7322 u8 s, n;
7324 if (vol > TP_EC_VOLUME_MAX)
7325 return -EINVAL;
7327 if (mutex_lock_killable(&volume_mutex) < 0)
7328 return -EINTR;
7330 rc = volume_get_status_ec(&s);
7331 if (rc)
7332 goto unlock;
7334 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7336 if (n != s) {
7337 rc = volume_set_status_ec(n);
7338 if (!rc)
7339 rc = 1;
7342 unlock:
7343 mutex_unlock(&volume_mutex);
7344 return rc;
7347 static int volume_set_software_mute(bool startup)
7349 int result;
7351 if (!tpacpi_is_lenovo())
7352 return -ENODEV;
7354 if (startup) {
7355 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7356 "HAUM", "qd"))
7357 return -EIO;
7359 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7360 "Initial HAUM setting was %d\n",
7361 software_mute_orig_mode);
7364 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7365 (int)TP_EC_MUTE_BTN_NONE))
7366 return -EIO;
7368 if (result != TP_EC_MUTE_BTN_NONE)
7369 pr_warn("Unexpected SAUM result %d\n",
7370 result);
7373 * In software mute mode, the standard codec controls take
7374 * precendence, so we unmute the ThinkPad HW switch at
7375 * startup. Just on case there are SAUM-capable ThinkPads
7376 * with level controls, set max HW volume as well.
7378 if (tp_features.mixer_no_level_control)
7379 result = volume_set_mute(false);
7380 else
7381 result = volume_set_status(TP_EC_VOLUME_MAX);
7383 if (result != 0)
7384 pr_warn("Failed to unmute the HW mute switch\n");
7386 return 0;
7389 static void volume_exit_software_mute(void)
7391 int r;
7393 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7394 || r != software_mute_orig_mode)
7395 pr_warn("Failed to restore mute mode\n");
7398 static int volume_alsa_set_volume(const u8 vol)
7400 dbg_printk(TPACPI_DBG_MIXER,
7401 "ALSA: trying to set volume level to %hu\n", vol);
7402 return __volume_set_volume_ec(vol);
7405 static void volume_alsa_notify_change(void)
7407 struct tpacpi_alsa_data *d;
7409 if (alsa_card && alsa_card->private_data) {
7410 d = alsa_card->private_data;
7411 if (d->ctl_mute_id)
7412 snd_ctl_notify(alsa_card,
7413 SNDRV_CTL_EVENT_MASK_VALUE,
7414 d->ctl_mute_id);
7415 if (d->ctl_vol_id)
7416 snd_ctl_notify(alsa_card,
7417 SNDRV_CTL_EVENT_MASK_VALUE,
7418 d->ctl_vol_id);
7422 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7423 struct snd_ctl_elem_info *uinfo)
7425 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7426 uinfo->count = 1;
7427 uinfo->value.integer.min = 0;
7428 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7429 return 0;
7432 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7433 struct snd_ctl_elem_value *ucontrol)
7435 u8 s;
7436 int rc;
7438 rc = volume_get_status(&s);
7439 if (rc < 0)
7440 return rc;
7442 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7443 return 0;
7446 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7447 struct snd_ctl_elem_value *ucontrol)
7449 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7450 ucontrol->value.integer.value[0]);
7451 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7454 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7456 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7457 struct snd_ctl_elem_value *ucontrol)
7459 u8 s;
7460 int rc;
7462 rc = volume_get_status(&s);
7463 if (rc < 0)
7464 return rc;
7466 ucontrol->value.integer.value[0] =
7467 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7468 return 0;
7471 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7472 struct snd_ctl_elem_value *ucontrol)
7474 tpacpi_disclose_usertask("ALSA", "%smute\n",
7475 ucontrol->value.integer.value[0] ?
7476 "un" : "");
7477 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7480 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7481 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7482 .name = "Console Playback Volume",
7483 .index = 0,
7484 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7485 .info = volume_alsa_vol_info,
7486 .get = volume_alsa_vol_get,
7489 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7491 .name = "Console Playback Switch",
7492 .index = 0,
7493 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7494 .info = volume_alsa_mute_info,
7495 .get = volume_alsa_mute_get,
7498 static void volume_suspend(void)
7500 tpacpi_volume_checkpoint_nvram();
7503 static void volume_resume(void)
7505 if (software_mute_active) {
7506 if (volume_set_software_mute(false) < 0)
7507 pr_warn("Failed to restore software mute\n");
7508 } else {
7509 volume_alsa_notify_change();
7513 static void volume_shutdown(void)
7515 tpacpi_volume_checkpoint_nvram();
7518 static void volume_exit(void)
7520 if (alsa_card) {
7521 snd_card_free(alsa_card);
7522 alsa_card = NULL;
7525 tpacpi_volume_checkpoint_nvram();
7527 if (software_mute_active)
7528 volume_exit_software_mute();
7531 static int __init volume_create_alsa_mixer(void)
7533 struct snd_card *card;
7534 struct tpacpi_alsa_data *data;
7535 struct snd_kcontrol *ctl_vol;
7536 struct snd_kcontrol *ctl_mute;
7537 int rc;
7539 rc = snd_card_new(&tpacpi_pdev->dev,
7540 alsa_index, alsa_id, THIS_MODULE,
7541 sizeof(struct tpacpi_alsa_data), &card);
7542 if (rc < 0 || !card) {
7543 pr_err("Failed to create ALSA card structures: %d\n", rc);
7544 return 1;
7547 BUG_ON(!card->private_data);
7548 data = card->private_data;
7549 data->card = card;
7551 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7552 sizeof(card->driver));
7553 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7554 sizeof(card->shortname));
7555 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7556 (thinkpad_id.ec_version_str) ?
7557 thinkpad_id.ec_version_str : "(unknown)");
7558 snprintf(card->longname, sizeof(card->longname),
7559 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7560 (thinkpad_id.ec_version_str) ?
7561 thinkpad_id.ec_version_str : "unknown");
7563 if (volume_control_allowed) {
7564 volume_alsa_control_vol.put = volume_alsa_vol_put;
7565 volume_alsa_control_vol.access =
7566 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7568 volume_alsa_control_mute.put = volume_alsa_mute_put;
7569 volume_alsa_control_mute.access =
7570 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7573 if (!tp_features.mixer_no_level_control) {
7574 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7575 rc = snd_ctl_add(card, ctl_vol);
7576 if (rc < 0) {
7577 pr_err("Failed to create ALSA volume control: %d\n",
7578 rc);
7579 goto err_exit;
7581 data->ctl_vol_id = &ctl_vol->id;
7584 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7585 rc = snd_ctl_add(card, ctl_mute);
7586 if (rc < 0) {
7587 pr_err("Failed to create ALSA mute control: %d\n", rc);
7588 goto err_exit;
7590 data->ctl_mute_id = &ctl_mute->id;
7592 rc = snd_card_register(card);
7593 if (rc < 0) {
7594 pr_err("Failed to register ALSA card: %d\n", rc);
7595 goto err_exit;
7598 alsa_card = card;
7599 return 0;
7601 err_exit:
7602 snd_card_free(card);
7603 return 1;
7606 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7607 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7609 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7610 /* Whitelist volume level on all IBM by default */
7611 { .vendor = PCI_VENDOR_ID_IBM,
7612 .bios = TPACPI_MATCH_ANY,
7613 .ec = TPACPI_MATCH_ANY,
7614 .quirks = TPACPI_VOL_Q_LEVEL },
7616 /* Lenovo models with volume control (needs confirmation) */
7617 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7618 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7619 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7620 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7621 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7622 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7623 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7625 /* Whitelist mute-only on all Lenovo by default */
7626 { .vendor = PCI_VENDOR_ID_LENOVO,
7627 .bios = TPACPI_MATCH_ANY,
7628 .ec = TPACPI_MATCH_ANY,
7629 .quirks = TPACPI_VOL_Q_MUTEONLY }
7632 static int __init volume_init(struct ibm_init_struct *iibm)
7634 unsigned long quirks;
7635 int rc;
7637 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7639 mutex_init(&volume_mutex);
7642 * Check for module parameter bogosity, note that we
7643 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7644 * able to detect "unspecified"
7646 if (volume_mode > TPACPI_VOL_MODE_MAX)
7647 return -EINVAL;
7649 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7650 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7651 TPACPI_MAIL);
7652 return 1;
7655 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7656 return -EINVAL;
7659 * The ALSA mixer is our primary interface.
7660 * When disabled, don't install the subdriver at all
7662 if (!alsa_enable) {
7663 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7664 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7665 return 1;
7668 quirks = tpacpi_check_quirks(volume_quirk_table,
7669 ARRAY_SIZE(volume_quirk_table));
7671 switch (volume_capabilities) {
7672 case TPACPI_VOL_CAP_AUTO:
7673 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7674 tp_features.mixer_no_level_control = 1;
7675 else if (quirks & TPACPI_VOL_Q_LEVEL)
7676 tp_features.mixer_no_level_control = 0;
7677 else
7678 return 1; /* no mixer */
7679 break;
7680 case TPACPI_VOL_CAP_VOLMUTE:
7681 tp_features.mixer_no_level_control = 0;
7682 break;
7683 case TPACPI_VOL_CAP_MUTEONLY:
7684 tp_features.mixer_no_level_control = 1;
7685 break;
7686 default:
7687 return 1;
7690 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7691 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7692 "using user-supplied volume_capabilities=%d\n",
7693 volume_capabilities);
7695 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7696 volume_mode == TPACPI_VOL_MODE_MAX) {
7697 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7699 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7700 "driver auto-selected volume_mode=%d\n",
7701 volume_mode);
7702 } else {
7703 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7704 "using user-supplied volume_mode=%d\n",
7705 volume_mode);
7708 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7709 "mute is supported, volume control is %s\n",
7710 str_supported(!tp_features.mixer_no_level_control));
7712 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7713 software_mute_active = true;
7714 } else {
7715 rc = volume_create_alsa_mixer();
7716 if (rc) {
7717 pr_err("Could not create the ALSA mixer interface\n");
7718 return rc;
7721 pr_info("Console audio control enabled, mode: %s\n",
7722 (volume_control_allowed) ?
7723 "override (read/write)" :
7724 "monitor (read only)");
7727 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7728 "registering volume hotkeys as change notification\n");
7729 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7730 | TP_ACPI_HKEY_VOLUP_MASK
7731 | TP_ACPI_HKEY_VOLDWN_MASK
7732 | TP_ACPI_HKEY_MUTE_MASK);
7734 return 0;
7737 static int volume_read(struct seq_file *m)
7739 u8 status;
7741 if (volume_get_status(&status) < 0) {
7742 seq_printf(m, "level:\t\tunreadable\n");
7743 } else {
7744 if (tp_features.mixer_no_level_control)
7745 seq_printf(m, "level:\t\tunsupported\n");
7746 else
7747 seq_printf(m, "level:\t\t%d\n",
7748 status & TP_EC_AUDIO_LVL_MSK);
7750 seq_printf(m, "mute:\t\t%s\n",
7751 onoff(status, TP_EC_AUDIO_MUTESW));
7753 if (volume_control_allowed) {
7754 seq_printf(m, "commands:\tunmute, mute\n");
7755 if (!tp_features.mixer_no_level_control) {
7756 seq_printf(m, "commands:\tup, down\n");
7757 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7758 TP_EC_VOLUME_MAX);
7763 return 0;
7766 static int volume_write(char *buf)
7768 u8 s;
7769 u8 new_level, new_mute;
7770 int l;
7771 char *cmd;
7772 int rc;
7775 * We do allow volume control at driver startup, so that the
7776 * user can set initial state through the volume=... parameter hack.
7778 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7779 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7780 tp_warned.volume_ctrl_forbidden = 1;
7781 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7782 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7784 return -EPERM;
7787 rc = volume_get_status(&s);
7788 if (rc < 0)
7789 return rc;
7791 new_level = s & TP_EC_AUDIO_LVL_MSK;
7792 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7794 while ((cmd = next_cmd(&buf))) {
7795 if (!tp_features.mixer_no_level_control) {
7796 if (strlencmp(cmd, "up") == 0) {
7797 if (new_mute)
7798 new_mute = 0;
7799 else if (new_level < TP_EC_VOLUME_MAX)
7800 new_level++;
7801 continue;
7802 } else if (strlencmp(cmd, "down") == 0) {
7803 if (new_mute)
7804 new_mute = 0;
7805 else if (new_level > 0)
7806 new_level--;
7807 continue;
7808 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7809 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7810 new_level = l;
7811 continue;
7814 if (strlencmp(cmd, "mute") == 0)
7815 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7816 else if (strlencmp(cmd, "unmute") == 0)
7817 new_mute = 0;
7818 else
7819 return -EINVAL;
7822 if (tp_features.mixer_no_level_control) {
7823 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7824 new_mute ? "" : "un");
7825 rc = volume_set_mute(!!new_mute);
7826 } else {
7827 tpacpi_disclose_usertask("procfs volume",
7828 "%smute and set level to %d\n",
7829 new_mute ? "" : "un", new_level);
7830 rc = volume_set_status(new_mute | new_level);
7832 volume_alsa_notify_change();
7834 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7837 static struct ibm_struct volume_driver_data = {
7838 .name = "volume",
7839 .read = volume_read,
7840 .write = volume_write,
7841 .exit = volume_exit,
7842 .suspend = volume_suspend,
7843 .resume = volume_resume,
7844 .shutdown = volume_shutdown,
7847 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7849 #define alsa_card NULL
7851 static inline void volume_alsa_notify_change(void)
7855 static int __init volume_init(struct ibm_init_struct *iibm)
7857 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7859 return 1;
7862 static struct ibm_struct volume_driver_data = {
7863 .name = "volume",
7866 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7868 /*************************************************************************
7869 * Fan subdriver
7873 * FAN ACCESS MODES
7875 * TPACPI_FAN_RD_ACPI_GFAN:
7876 * ACPI GFAN method: returns fan level
7878 * see TPACPI_FAN_WR_ACPI_SFAN
7879 * EC 0x2f (HFSP) not available if GFAN exists
7881 * TPACPI_FAN_WR_ACPI_SFAN:
7882 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7884 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7885 * it for writing.
7887 * TPACPI_FAN_WR_TPEC:
7888 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7889 * Supported on almost all ThinkPads
7891 * Fan speed changes of any sort (including those caused by the
7892 * disengaged mode) are usually done slowly by the firmware as the
7893 * maximum amount of fan duty cycle change per second seems to be
7894 * limited.
7896 * Reading is not available if GFAN exists.
7897 * Writing is not available if SFAN exists.
7899 * Bits
7900 * 7 automatic mode engaged;
7901 * (default operation mode of the ThinkPad)
7902 * fan level is ignored in this mode.
7903 * 6 full speed mode (takes precedence over bit 7);
7904 * not available on all thinkpads. May disable
7905 * the tachometer while the fan controller ramps up
7906 * the speed (which can take up to a few *minutes*).
7907 * Speeds up fan to 100% duty-cycle, which is far above
7908 * the standard RPM levels. It is not impossible that
7909 * it could cause hardware damage.
7910 * 5-3 unused in some models. Extra bits for fan level
7911 * in others, but still useless as all values above
7912 * 7 map to the same speed as level 7 in these models.
7913 * 2-0 fan level (0..7 usually)
7914 * 0x00 = stop
7915 * 0x07 = max (set when temperatures critical)
7916 * Some ThinkPads may have other levels, see
7917 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7919 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7920 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7921 * does so, its initial value is meaningless (0x07).
7923 * For firmware bugs, refer to:
7924 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7926 * ----
7928 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7929 * Main fan tachometer reading (in RPM)
7931 * This register is present on all ThinkPads with a new-style EC, and
7932 * it is known not to be present on the A21m/e, and T22, as there is
7933 * something else in offset 0x84 according to the ACPI DSDT. Other
7934 * ThinkPads from this same time period (and earlier) probably lack the
7935 * tachometer as well.
7937 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7938 * was never fixed by IBM to report the EC firmware version string
7939 * probably support the tachometer (like the early X models), so
7940 * detecting it is quite hard. We need more data to know for sure.
7942 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7943 * might result.
7945 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7946 * mode.
7948 * For firmware bugs, refer to:
7949 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7951 * ----
7953 * ThinkPad EC register 0x31 bit 0 (only on select models)
7955 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7956 * show the speed of the main fan. When bit 0 of EC register 0x31
7957 * is one, the tachometer registers show the speed of the auxiliary
7958 * fan.
7960 * Fan control seems to affect both fans, regardless of the state
7961 * of this bit.
7963 * So far, only the firmware for the X60/X61 non-tablet versions
7964 * seem to support this (firmware TP-7M).
7966 * TPACPI_FAN_WR_ACPI_FANS:
7967 * ThinkPad X31, X40, X41. Not available in the X60.
7969 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7970 * high speed. ACPI DSDT seems to map these three speeds to levels
7971 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7972 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7974 * The speeds are stored on handles
7975 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7977 * There are three default speed sets, accessible as handles:
7978 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7980 * ACPI DSDT switches which set is in use depending on various
7981 * factors.
7983 * TPACPI_FAN_WR_TPEC is also available and should be used to
7984 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7985 * but the ACPI tables just mention level 7.
7988 enum { /* Fan control constants */
7989 fan_status_offset = 0x2f, /* EC register 0x2f */
7990 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7991 * 0x84 must be read before 0x85 */
7992 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7993 bit 0 selects which fan is active */
7995 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7996 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7998 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8001 enum fan_status_access_mode {
8002 TPACPI_FAN_NONE = 0, /* No fan status or control */
8003 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8004 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8007 enum fan_control_access_mode {
8008 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8009 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8010 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8011 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8014 enum fan_control_commands {
8015 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8016 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8017 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8018 * and also watchdog cmd */
8021 static bool fan_control_allowed;
8023 static enum fan_status_access_mode fan_status_access_mode;
8024 static enum fan_control_access_mode fan_control_access_mode;
8025 static enum fan_control_commands fan_control_commands;
8027 static u8 fan_control_initial_status;
8028 static u8 fan_control_desired_level;
8029 static u8 fan_control_resume_level;
8030 static int fan_watchdog_maxinterval;
8032 static struct mutex fan_mutex;
8034 static void fan_watchdog_fire(struct work_struct *ignored);
8035 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8037 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8038 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8039 "\\FSPD", /* 600e/x, 770e, 770x */
8040 ); /* all others */
8041 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8042 "JFNS", /* 770x-JL */
8043 ); /* all others */
8046 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8047 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8048 * be in auto mode (0x80).
8050 * This is corrected by any write to HFSP either by the driver, or
8051 * by the firmware.
8053 * We assume 0x07 really means auto mode while this quirk is active,
8054 * as this is far more likely than the ThinkPad being in level 7,
8055 * which is only used by the firmware during thermal emergencies.
8057 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8058 * TP-70 (T43, R52), which are known to be buggy.
8061 static void fan_quirk1_setup(void)
8063 if (fan_control_initial_status == 0x07) {
8064 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8065 tp_features.fan_ctrl_status_undef = 1;
8069 static void fan_quirk1_handle(u8 *fan_status)
8071 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8072 if (*fan_status != fan_control_initial_status) {
8073 /* something changed the HFSP regisnter since
8074 * driver init time, so it is not undefined
8075 * anymore */
8076 tp_features.fan_ctrl_status_undef = 0;
8077 } else {
8078 /* Return most likely status. In fact, it
8079 * might be the only possible status */
8080 *fan_status = TP_EC_FAN_AUTO;
8085 /* Select main fan on X60/X61, NOOP on others */
8086 static bool fan_select_fan1(void)
8088 if (tp_features.second_fan) {
8089 u8 val;
8091 if (ec_read(fan_select_offset, &val) < 0)
8092 return false;
8093 val &= 0xFEU;
8094 if (ec_write(fan_select_offset, val) < 0)
8095 return false;
8097 return true;
8100 /* Select secondary fan on X60/X61 */
8101 static bool fan_select_fan2(void)
8103 u8 val;
8105 if (!tp_features.second_fan)
8106 return false;
8108 if (ec_read(fan_select_offset, &val) < 0)
8109 return false;
8110 val |= 0x01U;
8111 if (ec_write(fan_select_offset, val) < 0)
8112 return false;
8114 return true;
8118 * Call with fan_mutex held
8120 static void fan_update_desired_level(u8 status)
8122 if ((status &
8123 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8124 if (status > 7)
8125 fan_control_desired_level = 7;
8126 else
8127 fan_control_desired_level = status;
8131 static int fan_get_status(u8 *status)
8133 u8 s;
8135 /* TODO:
8136 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8138 switch (fan_status_access_mode) {
8139 case TPACPI_FAN_RD_ACPI_GFAN: {
8140 /* 570, 600e/x, 770e, 770x */
8141 int res;
8143 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8144 return -EIO;
8146 if (likely(status))
8147 *status = res & 0x07;
8149 break;
8151 case TPACPI_FAN_RD_TPEC:
8152 /* all except 570, 600e/x, 770e, 770x */
8153 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8154 return -EIO;
8156 if (likely(status)) {
8157 *status = s;
8158 fan_quirk1_handle(status);
8161 break;
8163 default:
8164 return -ENXIO;
8167 return 0;
8170 static int fan_get_status_safe(u8 *status)
8172 int rc;
8173 u8 s;
8175 if (mutex_lock_killable(&fan_mutex))
8176 return -ERESTARTSYS;
8177 rc = fan_get_status(&s);
8178 if (!rc)
8179 fan_update_desired_level(s);
8180 mutex_unlock(&fan_mutex);
8182 if (rc)
8183 return rc;
8184 if (status)
8185 *status = s;
8187 return 0;
8190 static int fan_get_speed(unsigned int *speed)
8192 u8 hi, lo;
8194 switch (fan_status_access_mode) {
8195 case TPACPI_FAN_RD_TPEC:
8196 /* all except 570, 600e/x, 770e, 770x */
8197 if (unlikely(!fan_select_fan1()))
8198 return -EIO;
8199 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8200 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8201 return -EIO;
8203 if (likely(speed))
8204 *speed = (hi << 8) | lo;
8206 break;
8208 default:
8209 return -ENXIO;
8212 return 0;
8215 static int fan2_get_speed(unsigned int *speed)
8217 u8 hi, lo;
8218 bool rc;
8220 switch (fan_status_access_mode) {
8221 case TPACPI_FAN_RD_TPEC:
8222 /* all except 570, 600e/x, 770e, 770x */
8223 if (unlikely(!fan_select_fan2()))
8224 return -EIO;
8225 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8226 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8227 fan_select_fan1(); /* play it safe */
8228 if (rc)
8229 return -EIO;
8231 if (likely(speed))
8232 *speed = (hi << 8) | lo;
8234 break;
8236 default:
8237 return -ENXIO;
8240 return 0;
8243 static int fan_set_level(int level)
8245 if (!fan_control_allowed)
8246 return -EPERM;
8248 switch (fan_control_access_mode) {
8249 case TPACPI_FAN_WR_ACPI_SFAN:
8250 if (level >= 0 && level <= 7) {
8251 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8252 return -EIO;
8253 } else
8254 return -EINVAL;
8255 break;
8257 case TPACPI_FAN_WR_ACPI_FANS:
8258 case TPACPI_FAN_WR_TPEC:
8259 if (!(level & TP_EC_FAN_AUTO) &&
8260 !(level & TP_EC_FAN_FULLSPEED) &&
8261 ((level < 0) || (level > 7)))
8262 return -EINVAL;
8264 /* safety net should the EC not support AUTO
8265 * or FULLSPEED mode bits and just ignore them */
8266 if (level & TP_EC_FAN_FULLSPEED)
8267 level |= 7; /* safety min speed 7 */
8268 else if (level & TP_EC_FAN_AUTO)
8269 level |= 4; /* safety min speed 4 */
8271 if (!acpi_ec_write(fan_status_offset, level))
8272 return -EIO;
8273 else
8274 tp_features.fan_ctrl_status_undef = 0;
8275 break;
8277 default:
8278 return -ENXIO;
8281 vdbg_printk(TPACPI_DBG_FAN,
8282 "fan control: set fan control register to 0x%02x\n", level);
8283 return 0;
8286 static int fan_set_level_safe(int level)
8288 int rc;
8290 if (!fan_control_allowed)
8291 return -EPERM;
8293 if (mutex_lock_killable(&fan_mutex))
8294 return -ERESTARTSYS;
8296 if (level == TPACPI_FAN_LAST_LEVEL)
8297 level = fan_control_desired_level;
8299 rc = fan_set_level(level);
8300 if (!rc)
8301 fan_update_desired_level(level);
8303 mutex_unlock(&fan_mutex);
8304 return rc;
8307 static int fan_set_enable(void)
8309 u8 s;
8310 int rc;
8312 if (!fan_control_allowed)
8313 return -EPERM;
8315 if (mutex_lock_killable(&fan_mutex))
8316 return -ERESTARTSYS;
8318 switch (fan_control_access_mode) {
8319 case TPACPI_FAN_WR_ACPI_FANS:
8320 case TPACPI_FAN_WR_TPEC:
8321 rc = fan_get_status(&s);
8322 if (rc < 0)
8323 break;
8325 /* Don't go out of emergency fan mode */
8326 if (s != 7) {
8327 s &= 0x07;
8328 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8331 if (!acpi_ec_write(fan_status_offset, s))
8332 rc = -EIO;
8333 else {
8334 tp_features.fan_ctrl_status_undef = 0;
8335 rc = 0;
8337 break;
8339 case TPACPI_FAN_WR_ACPI_SFAN:
8340 rc = fan_get_status(&s);
8341 if (rc < 0)
8342 break;
8344 s &= 0x07;
8346 /* Set fan to at least level 4 */
8347 s |= 4;
8349 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8350 rc = -EIO;
8351 else
8352 rc = 0;
8353 break;
8355 default:
8356 rc = -ENXIO;
8359 mutex_unlock(&fan_mutex);
8361 if (!rc)
8362 vdbg_printk(TPACPI_DBG_FAN,
8363 "fan control: set fan control register to 0x%02x\n",
8365 return rc;
8368 static int fan_set_disable(void)
8370 int rc;
8372 if (!fan_control_allowed)
8373 return -EPERM;
8375 if (mutex_lock_killable(&fan_mutex))
8376 return -ERESTARTSYS;
8378 rc = 0;
8379 switch (fan_control_access_mode) {
8380 case TPACPI_FAN_WR_ACPI_FANS:
8381 case TPACPI_FAN_WR_TPEC:
8382 if (!acpi_ec_write(fan_status_offset, 0x00))
8383 rc = -EIO;
8384 else {
8385 fan_control_desired_level = 0;
8386 tp_features.fan_ctrl_status_undef = 0;
8388 break;
8390 case TPACPI_FAN_WR_ACPI_SFAN:
8391 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8392 rc = -EIO;
8393 else
8394 fan_control_desired_level = 0;
8395 break;
8397 default:
8398 rc = -ENXIO;
8401 if (!rc)
8402 vdbg_printk(TPACPI_DBG_FAN,
8403 "fan control: set fan control register to 0\n");
8405 mutex_unlock(&fan_mutex);
8406 return rc;
8409 static int fan_set_speed(int speed)
8411 int rc;
8413 if (!fan_control_allowed)
8414 return -EPERM;
8416 if (mutex_lock_killable(&fan_mutex))
8417 return -ERESTARTSYS;
8419 rc = 0;
8420 switch (fan_control_access_mode) {
8421 case TPACPI_FAN_WR_ACPI_FANS:
8422 if (speed >= 0 && speed <= 65535) {
8423 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8424 speed, speed, speed))
8425 rc = -EIO;
8426 } else
8427 rc = -EINVAL;
8428 break;
8430 default:
8431 rc = -ENXIO;
8434 mutex_unlock(&fan_mutex);
8435 return rc;
8438 static void fan_watchdog_reset(void)
8440 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8441 return;
8443 if (fan_watchdog_maxinterval > 0 &&
8444 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8445 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8446 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8447 else
8448 cancel_delayed_work(&fan_watchdog_task);
8451 static void fan_watchdog_fire(struct work_struct *ignored)
8453 int rc;
8455 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8456 return;
8458 pr_notice("fan watchdog: enabling fan\n");
8459 rc = fan_set_enable();
8460 if (rc < 0) {
8461 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8462 rc);
8463 /* reschedule for later */
8464 fan_watchdog_reset();
8469 * SYSFS fan layout: hwmon compatible (device)
8471 * pwm*_enable:
8472 * 0: "disengaged" mode
8473 * 1: manual mode
8474 * 2: native EC "auto" mode (recommended, hardware default)
8476 * pwm*: set speed in manual mode, ignored otherwise.
8477 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8478 * interpolation.
8480 * fan*_input: tachometer reading, RPM
8483 * SYSFS fan layout: extensions
8485 * fan_watchdog (driver):
8486 * fan watchdog interval in seconds, 0 disables (default), max 120
8489 /* sysfs fan pwm1_enable ----------------------------------------------- */
8490 static ssize_t fan_pwm1_enable_show(struct device *dev,
8491 struct device_attribute *attr,
8492 char *buf)
8494 int res, mode;
8495 u8 status;
8497 res = fan_get_status_safe(&status);
8498 if (res)
8499 return res;
8501 if (status & TP_EC_FAN_FULLSPEED) {
8502 mode = 0;
8503 } else if (status & TP_EC_FAN_AUTO) {
8504 mode = 2;
8505 } else
8506 mode = 1;
8508 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8511 static ssize_t fan_pwm1_enable_store(struct device *dev,
8512 struct device_attribute *attr,
8513 const char *buf, size_t count)
8515 unsigned long t;
8516 int res, level;
8518 if (parse_strtoul(buf, 2, &t))
8519 return -EINVAL;
8521 tpacpi_disclose_usertask("hwmon pwm1_enable",
8522 "set fan mode to %lu\n", t);
8524 switch (t) {
8525 case 0:
8526 level = TP_EC_FAN_FULLSPEED;
8527 break;
8528 case 1:
8529 level = TPACPI_FAN_LAST_LEVEL;
8530 break;
8531 case 2:
8532 level = TP_EC_FAN_AUTO;
8533 break;
8534 case 3:
8535 /* reserved for software-controlled auto mode */
8536 return -ENOSYS;
8537 default:
8538 return -EINVAL;
8541 res = fan_set_level_safe(level);
8542 if (res == -ENXIO)
8543 return -EINVAL;
8544 else if (res < 0)
8545 return res;
8547 fan_watchdog_reset();
8549 return count;
8552 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8553 fan_pwm1_enable_show, fan_pwm1_enable_store);
8555 /* sysfs fan pwm1 ------------------------------------------------------ */
8556 static ssize_t fan_pwm1_show(struct device *dev,
8557 struct device_attribute *attr,
8558 char *buf)
8560 int res;
8561 u8 status;
8563 res = fan_get_status_safe(&status);
8564 if (res)
8565 return res;
8567 if ((status &
8568 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8569 status = fan_control_desired_level;
8571 if (status > 7)
8572 status = 7;
8574 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8577 static ssize_t fan_pwm1_store(struct device *dev,
8578 struct device_attribute *attr,
8579 const char *buf, size_t count)
8581 unsigned long s;
8582 int rc;
8583 u8 status, newlevel;
8585 if (parse_strtoul(buf, 255, &s))
8586 return -EINVAL;
8588 tpacpi_disclose_usertask("hwmon pwm1",
8589 "set fan speed to %lu\n", s);
8591 /* scale down from 0-255 to 0-7 */
8592 newlevel = (s >> 5) & 0x07;
8594 if (mutex_lock_killable(&fan_mutex))
8595 return -ERESTARTSYS;
8597 rc = fan_get_status(&status);
8598 if (!rc && (status &
8599 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8600 rc = fan_set_level(newlevel);
8601 if (rc == -ENXIO)
8602 rc = -EINVAL;
8603 else if (!rc) {
8604 fan_update_desired_level(newlevel);
8605 fan_watchdog_reset();
8609 mutex_unlock(&fan_mutex);
8610 return (rc) ? rc : count;
8613 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8615 /* sysfs fan fan1_input ------------------------------------------------ */
8616 static ssize_t fan_fan1_input_show(struct device *dev,
8617 struct device_attribute *attr,
8618 char *buf)
8620 int res;
8621 unsigned int speed;
8623 res = fan_get_speed(&speed);
8624 if (res < 0)
8625 return res;
8627 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8630 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8632 /* sysfs fan fan2_input ------------------------------------------------ */
8633 static ssize_t fan_fan2_input_show(struct device *dev,
8634 struct device_attribute *attr,
8635 char *buf)
8637 int res;
8638 unsigned int speed;
8640 res = fan2_get_speed(&speed);
8641 if (res < 0)
8642 return res;
8644 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8647 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8649 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8650 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8652 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8655 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8656 size_t count)
8658 unsigned long t;
8660 if (parse_strtoul(buf, 120, &t))
8661 return -EINVAL;
8663 if (!fan_control_allowed)
8664 return -EPERM;
8666 fan_watchdog_maxinterval = t;
8667 fan_watchdog_reset();
8669 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8671 return count;
8673 static DRIVER_ATTR_RW(fan_watchdog);
8675 /* --------------------------------------------------------------------- */
8676 static struct attribute *fan_attributes[] = {
8677 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8678 &dev_attr_fan1_input.attr,
8679 NULL, /* for fan2_input */
8680 NULL
8683 static const struct attribute_group fan_attr_group = {
8684 .attrs = fan_attributes,
8687 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8688 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8690 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8691 { .vendor = PCI_VENDOR_ID_IBM, \
8692 .bios = TPACPI_MATCH_ANY, \
8693 .ec = TPID(__id1, __id2), \
8694 .quirks = __quirks }
8696 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8697 { .vendor = PCI_VENDOR_ID_LENOVO, \
8698 .bios = TPACPI_MATCH_ANY, \
8699 .ec = TPID(__id1, __id2), \
8700 .quirks = __quirks }
8702 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8703 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8704 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8705 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8706 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8707 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8710 #undef TPACPI_FAN_QL
8711 #undef TPACPI_FAN_QI
8713 static int __init fan_init(struct ibm_init_struct *iibm)
8715 int rc;
8716 unsigned long quirks;
8718 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8719 "initializing fan subdriver\n");
8721 mutex_init(&fan_mutex);
8722 fan_status_access_mode = TPACPI_FAN_NONE;
8723 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8724 fan_control_commands = 0;
8725 fan_watchdog_maxinterval = 0;
8726 tp_features.fan_ctrl_status_undef = 0;
8727 tp_features.second_fan = 0;
8728 fan_control_desired_level = 7;
8730 if (tpacpi_is_ibm()) {
8731 TPACPI_ACPIHANDLE_INIT(fans);
8732 TPACPI_ACPIHANDLE_INIT(gfan);
8733 TPACPI_ACPIHANDLE_INIT(sfan);
8736 quirks = tpacpi_check_quirks(fan_quirk_table,
8737 ARRAY_SIZE(fan_quirk_table));
8739 if (gfan_handle) {
8740 /* 570, 600e/x, 770e, 770x */
8741 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8742 } else {
8743 /* all other ThinkPads: note that even old-style
8744 * ThinkPad ECs supports the fan control register */
8745 if (likely(acpi_ec_read(fan_status_offset,
8746 &fan_control_initial_status))) {
8747 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8748 if (quirks & TPACPI_FAN_Q1)
8749 fan_quirk1_setup();
8750 if (quirks & TPACPI_FAN_2FAN) {
8751 tp_features.second_fan = 1;
8752 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8753 "secondary fan support enabled\n");
8755 } else {
8756 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8757 return 1;
8761 if (sfan_handle) {
8762 /* 570, 770x-JL */
8763 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8764 fan_control_commands |=
8765 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8766 } else {
8767 if (!gfan_handle) {
8768 /* gfan without sfan means no fan control */
8769 /* all other models implement TP EC 0x2f control */
8771 if (fans_handle) {
8772 /* X31, X40, X41 */
8773 fan_control_access_mode =
8774 TPACPI_FAN_WR_ACPI_FANS;
8775 fan_control_commands |=
8776 TPACPI_FAN_CMD_SPEED |
8777 TPACPI_FAN_CMD_LEVEL |
8778 TPACPI_FAN_CMD_ENABLE;
8779 } else {
8780 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8781 fan_control_commands |=
8782 TPACPI_FAN_CMD_LEVEL |
8783 TPACPI_FAN_CMD_ENABLE;
8788 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8789 "fan is %s, modes %d, %d\n",
8790 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8791 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8792 fan_status_access_mode, fan_control_access_mode);
8794 /* fan control master switch */
8795 if (!fan_control_allowed) {
8796 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8797 fan_control_commands = 0;
8798 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8799 "fan control features disabled by parameter\n");
8802 /* update fan_control_desired_level */
8803 if (fan_status_access_mode != TPACPI_FAN_NONE)
8804 fan_get_status_safe(NULL);
8806 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8807 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8808 if (tp_features.second_fan) {
8809 /* attach second fan tachometer */
8810 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8811 &dev_attr_fan2_input.attr;
8813 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8814 &fan_attr_group);
8815 if (rc < 0)
8816 return rc;
8818 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8819 &driver_attr_fan_watchdog);
8820 if (rc < 0) {
8821 sysfs_remove_group(&tpacpi_hwmon->kobj,
8822 &fan_attr_group);
8823 return rc;
8825 return 0;
8826 } else
8827 return 1;
8830 static void fan_exit(void)
8832 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8833 "cancelling any pending fan watchdog tasks\n");
8835 /* FIXME: can we really do this unconditionally? */
8836 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8837 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8838 &driver_attr_fan_watchdog);
8840 cancel_delayed_work(&fan_watchdog_task);
8841 flush_workqueue(tpacpi_wq);
8844 static void fan_suspend(void)
8846 int rc;
8848 if (!fan_control_allowed)
8849 return;
8851 /* Store fan status in cache */
8852 fan_control_resume_level = 0;
8853 rc = fan_get_status_safe(&fan_control_resume_level);
8854 if (rc < 0)
8855 pr_notice("failed to read fan level for later restore during resume: %d\n",
8856 rc);
8858 /* if it is undefined, don't attempt to restore it.
8859 * KEEP THIS LAST */
8860 if (tp_features.fan_ctrl_status_undef)
8861 fan_control_resume_level = 0;
8864 static void fan_resume(void)
8866 u8 current_level = 7;
8867 bool do_set = false;
8868 int rc;
8870 /* DSDT *always* updates status on resume */
8871 tp_features.fan_ctrl_status_undef = 0;
8873 if (!fan_control_allowed ||
8874 !fan_control_resume_level ||
8875 (fan_get_status_safe(&current_level) < 0))
8876 return;
8878 switch (fan_control_access_mode) {
8879 case TPACPI_FAN_WR_ACPI_SFAN:
8880 /* never decrease fan level */
8881 do_set = (fan_control_resume_level > current_level);
8882 break;
8883 case TPACPI_FAN_WR_ACPI_FANS:
8884 case TPACPI_FAN_WR_TPEC:
8885 /* never decrease fan level, scale is:
8886 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8888 * We expect the firmware to set either 7 or AUTO, but we
8889 * handle FULLSPEED out of paranoia.
8891 * So, we can safely only restore FULLSPEED or 7, anything
8892 * else could slow the fan. Restoring AUTO is useless, at
8893 * best that's exactly what the DSDT already set (it is the
8894 * slower it uses).
8896 * Always keep in mind that the DSDT *will* have set the
8897 * fans to what the vendor supposes is the best level. We
8898 * muck with it only to speed the fan up.
8900 if (fan_control_resume_level != 7 &&
8901 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8902 return;
8903 else
8904 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8905 (current_level != fan_control_resume_level);
8906 break;
8907 default:
8908 return;
8910 if (do_set) {
8911 pr_notice("restoring fan level to 0x%02x\n",
8912 fan_control_resume_level);
8913 rc = fan_set_level_safe(fan_control_resume_level);
8914 if (rc < 0)
8915 pr_notice("failed to restore fan level: %d\n", rc);
8919 static int fan_read(struct seq_file *m)
8921 int rc;
8922 u8 status;
8923 unsigned int speed = 0;
8925 switch (fan_status_access_mode) {
8926 case TPACPI_FAN_RD_ACPI_GFAN:
8927 /* 570, 600e/x, 770e, 770x */
8928 rc = fan_get_status_safe(&status);
8929 if (rc < 0)
8930 return rc;
8932 seq_printf(m, "status:\t\t%s\n"
8933 "level:\t\t%d\n",
8934 (status != 0) ? "enabled" : "disabled", status);
8935 break;
8937 case TPACPI_FAN_RD_TPEC:
8938 /* all except 570, 600e/x, 770e, 770x */
8939 rc = fan_get_status_safe(&status);
8940 if (rc < 0)
8941 return rc;
8943 seq_printf(m, "status:\t\t%s\n",
8944 (status != 0) ? "enabled" : "disabled");
8946 rc = fan_get_speed(&speed);
8947 if (rc < 0)
8948 return rc;
8950 seq_printf(m, "speed:\t\t%d\n", speed);
8952 if (status & TP_EC_FAN_FULLSPEED)
8953 /* Disengaged mode takes precedence */
8954 seq_printf(m, "level:\t\tdisengaged\n");
8955 else if (status & TP_EC_FAN_AUTO)
8956 seq_printf(m, "level:\t\tauto\n");
8957 else
8958 seq_printf(m, "level:\t\t%d\n", status);
8959 break;
8961 case TPACPI_FAN_NONE:
8962 default:
8963 seq_printf(m, "status:\t\tnot supported\n");
8966 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8967 seq_printf(m, "commands:\tlevel <level>");
8969 switch (fan_control_access_mode) {
8970 case TPACPI_FAN_WR_ACPI_SFAN:
8971 seq_printf(m, " (<level> is 0-7)\n");
8972 break;
8974 default:
8975 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8976 break;
8980 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8981 seq_printf(m, "commands:\tenable, disable\n"
8982 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8984 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8985 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8987 return 0;
8990 static int fan_write_cmd_level(const char *cmd, int *rc)
8992 int level;
8994 if (strlencmp(cmd, "level auto") == 0)
8995 level = TP_EC_FAN_AUTO;
8996 else if ((strlencmp(cmd, "level disengaged") == 0) |
8997 (strlencmp(cmd, "level full-speed") == 0))
8998 level = TP_EC_FAN_FULLSPEED;
8999 else if (sscanf(cmd, "level %d", &level) != 1)
9000 return 0;
9002 *rc = fan_set_level_safe(level);
9003 if (*rc == -ENXIO)
9004 pr_err("level command accepted for unsupported access mode %d\n",
9005 fan_control_access_mode);
9006 else if (!*rc)
9007 tpacpi_disclose_usertask("procfs fan",
9008 "set level to %d\n", level);
9010 return 1;
9013 static int fan_write_cmd_enable(const char *cmd, int *rc)
9015 if (strlencmp(cmd, "enable") != 0)
9016 return 0;
9018 *rc = fan_set_enable();
9019 if (*rc == -ENXIO)
9020 pr_err("enable command accepted for unsupported access mode %d\n",
9021 fan_control_access_mode);
9022 else if (!*rc)
9023 tpacpi_disclose_usertask("procfs fan", "enable\n");
9025 return 1;
9028 static int fan_write_cmd_disable(const char *cmd, int *rc)
9030 if (strlencmp(cmd, "disable") != 0)
9031 return 0;
9033 *rc = fan_set_disable();
9034 if (*rc == -ENXIO)
9035 pr_err("disable command accepted for unsupported access mode %d\n",
9036 fan_control_access_mode);
9037 else if (!*rc)
9038 tpacpi_disclose_usertask("procfs fan", "disable\n");
9040 return 1;
9043 static int fan_write_cmd_speed(const char *cmd, int *rc)
9045 int speed;
9047 /* TODO:
9048 * Support speed <low> <medium> <high> ? */
9050 if (sscanf(cmd, "speed %d", &speed) != 1)
9051 return 0;
9053 *rc = fan_set_speed(speed);
9054 if (*rc == -ENXIO)
9055 pr_err("speed command accepted for unsupported access mode %d\n",
9056 fan_control_access_mode);
9057 else if (!*rc)
9058 tpacpi_disclose_usertask("procfs fan",
9059 "set speed to %d\n", speed);
9061 return 1;
9064 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9066 int interval;
9068 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9069 return 0;
9071 if (interval < 0 || interval > 120)
9072 *rc = -EINVAL;
9073 else {
9074 fan_watchdog_maxinterval = interval;
9075 tpacpi_disclose_usertask("procfs fan",
9076 "set watchdog timer to %d\n",
9077 interval);
9080 return 1;
9083 static int fan_write(char *buf)
9085 char *cmd;
9086 int rc = 0;
9088 while (!rc && (cmd = next_cmd(&buf))) {
9089 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9090 fan_write_cmd_level(cmd, &rc)) &&
9091 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9092 (fan_write_cmd_enable(cmd, &rc) ||
9093 fan_write_cmd_disable(cmd, &rc) ||
9094 fan_write_cmd_watchdog(cmd, &rc))) &&
9095 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9096 fan_write_cmd_speed(cmd, &rc))
9098 rc = -EINVAL;
9099 else if (!rc)
9100 fan_watchdog_reset();
9103 return rc;
9106 static struct ibm_struct fan_driver_data = {
9107 .name = "fan",
9108 .read = fan_read,
9109 .write = fan_write,
9110 .exit = fan_exit,
9111 .suspend = fan_suspend,
9112 .resume = fan_resume,
9115 /*************************************************************************
9116 * Mute LED subdriver
9120 struct tp_led_table {
9121 acpi_string name;
9122 int on_value;
9123 int off_value;
9124 int state;
9127 static struct tp_led_table led_tables[] = {
9128 [TPACPI_LED_MUTE] = {
9129 .name = "SSMS",
9130 .on_value = 1,
9131 .off_value = 0,
9133 [TPACPI_LED_MICMUTE] = {
9134 .name = "MMTS",
9135 .on_value = 2,
9136 .off_value = 0,
9140 static int mute_led_on_off(struct tp_led_table *t, bool state)
9142 acpi_handle temp;
9143 int output;
9145 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9146 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9147 return -EIO;
9150 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9151 state ? t->on_value : t->off_value))
9152 return -EIO;
9154 t->state = state;
9155 return state;
9158 int tpacpi_led_set(int whichled, bool on)
9160 struct tp_led_table *t;
9162 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9163 return -EINVAL;
9165 t = &led_tables[whichled];
9166 if (t->state < 0 || t->state == on)
9167 return t->state;
9168 return mute_led_on_off(t, on);
9170 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9172 static int mute_led_init(struct ibm_init_struct *iibm)
9174 acpi_handle temp;
9175 int i;
9177 for (i = 0; i < TPACPI_LED_MAX; i++) {
9178 struct tp_led_table *t = &led_tables[i];
9179 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9180 mute_led_on_off(t, false);
9181 else
9182 t->state = -ENODEV;
9184 return 0;
9187 static void mute_led_exit(void)
9189 int i;
9191 for (i = 0; i < TPACPI_LED_MAX; i++)
9192 tpacpi_led_set(i, false);
9195 static void mute_led_resume(void)
9197 int i;
9199 for (i = 0; i < TPACPI_LED_MAX; i++) {
9200 struct tp_led_table *t = &led_tables[i];
9201 if (t->state >= 0)
9202 mute_led_on_off(t, t->state);
9206 static struct ibm_struct mute_led_driver_data = {
9207 .name = "mute_led",
9208 .exit = mute_led_exit,
9209 .resume = mute_led_resume,
9212 /****************************************************************************
9213 ****************************************************************************
9215 * Infrastructure
9217 ****************************************************************************
9218 ****************************************************************************/
9221 * HKEY event callout for other subdrivers go here
9222 * (yes, it is ugly, but it is quick, safe, and gets the job done
9224 static void tpacpi_driver_event(const unsigned int hkey_event)
9226 if (ibm_backlight_device) {
9227 switch (hkey_event) {
9228 case TP_HKEY_EV_BRGHT_UP:
9229 case TP_HKEY_EV_BRGHT_DOWN:
9230 tpacpi_brightness_notify_change();
9233 if (alsa_card) {
9234 switch (hkey_event) {
9235 case TP_HKEY_EV_VOL_UP:
9236 case TP_HKEY_EV_VOL_DOWN:
9237 case TP_HKEY_EV_VOL_MUTE:
9238 volume_alsa_notify_change();
9241 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9242 enum led_brightness brightness;
9244 mutex_lock(&kbdlight_mutex);
9247 * Check the brightness actually changed, setting the brightness
9248 * through kbdlight_set_level() also triggers this event.
9250 brightness = kbdlight_sysfs_get(NULL);
9251 if (kbdlight_brightness != brightness) {
9252 kbdlight_brightness = brightness;
9253 led_classdev_notify_brightness_hw_changed(
9254 &tpacpi_led_kbdlight.led_classdev, brightness);
9257 mutex_unlock(&kbdlight_mutex);
9261 static void hotkey_driver_event(const unsigned int scancode)
9263 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9266 /* --------------------------------------------------------------------- */
9268 /* /proc support */
9269 static struct proc_dir_entry *proc_dir;
9272 * Module and infrastructure proble, init and exit handling
9275 static bool force_load;
9277 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9278 static const char * __init str_supported(int is_supported)
9280 static char text_unsupported[] __initdata = "not supported";
9282 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9284 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9286 static void ibm_exit(struct ibm_struct *ibm)
9288 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9290 list_del_init(&ibm->all_drivers);
9292 if (ibm->flags.acpi_notify_installed) {
9293 dbg_printk(TPACPI_DBG_EXIT,
9294 "%s: acpi_remove_notify_handler\n", ibm->name);
9295 BUG_ON(!ibm->acpi);
9296 acpi_remove_notify_handler(*ibm->acpi->handle,
9297 ibm->acpi->type,
9298 dispatch_acpi_notify);
9299 ibm->flags.acpi_notify_installed = 0;
9302 if (ibm->flags.proc_created) {
9303 dbg_printk(TPACPI_DBG_EXIT,
9304 "%s: remove_proc_entry\n", ibm->name);
9305 remove_proc_entry(ibm->name, proc_dir);
9306 ibm->flags.proc_created = 0;
9309 if (ibm->flags.acpi_driver_registered) {
9310 dbg_printk(TPACPI_DBG_EXIT,
9311 "%s: acpi_bus_unregister_driver\n", ibm->name);
9312 BUG_ON(!ibm->acpi);
9313 acpi_bus_unregister_driver(ibm->acpi->driver);
9314 kfree(ibm->acpi->driver);
9315 ibm->acpi->driver = NULL;
9316 ibm->flags.acpi_driver_registered = 0;
9319 if (ibm->flags.init_called && ibm->exit) {
9320 ibm->exit();
9321 ibm->flags.init_called = 0;
9324 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9327 static int __init ibm_init(struct ibm_init_struct *iibm)
9329 int ret;
9330 struct ibm_struct *ibm = iibm->data;
9331 struct proc_dir_entry *entry;
9333 BUG_ON(ibm == NULL);
9335 INIT_LIST_HEAD(&ibm->all_drivers);
9337 if (ibm->flags.experimental && !experimental)
9338 return 0;
9340 dbg_printk(TPACPI_DBG_INIT,
9341 "probing for %s\n", ibm->name);
9343 if (iibm->init) {
9344 ret = iibm->init(iibm);
9345 if (ret > 0)
9346 return 0; /* probe failed */
9347 if (ret)
9348 return ret;
9350 ibm->flags.init_called = 1;
9353 if (ibm->acpi) {
9354 if (ibm->acpi->hid) {
9355 ret = register_tpacpi_subdriver(ibm);
9356 if (ret)
9357 goto err_out;
9360 if (ibm->acpi->notify) {
9361 ret = setup_acpi_notify(ibm);
9362 if (ret == -ENODEV) {
9363 pr_notice("disabling subdriver %s\n",
9364 ibm->name);
9365 ret = 0;
9366 goto err_out;
9368 if (ret < 0)
9369 goto err_out;
9373 dbg_printk(TPACPI_DBG_INIT,
9374 "%s installed\n", ibm->name);
9376 if (ibm->read) {
9377 umode_t mode = iibm->base_procfs_mode;
9379 if (!mode)
9380 mode = S_IRUGO;
9381 if (ibm->write)
9382 mode |= S_IWUSR;
9383 entry = proc_create_data(ibm->name, mode, proc_dir,
9384 &dispatch_proc_fops, ibm);
9385 if (!entry) {
9386 pr_err("unable to create proc entry %s\n", ibm->name);
9387 ret = -ENODEV;
9388 goto err_out;
9390 ibm->flags.proc_created = 1;
9393 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9395 return 0;
9397 err_out:
9398 dbg_printk(TPACPI_DBG_INIT,
9399 "%s: at error exit path with result %d\n",
9400 ibm->name, ret);
9402 ibm_exit(ibm);
9403 return (ret < 0) ? ret : 0;
9406 /* Probing */
9408 static bool __pure __init tpacpi_is_fw_digit(const char c)
9410 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9413 static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
9414 const char t)
9417 * Most models: xxyTkkWW (#.##c)
9418 * Ancient 570/600 and -SL lacks (#.##c)
9420 if (s && strlen(s) >= 8 &&
9421 tpacpi_is_fw_digit(s[0]) &&
9422 tpacpi_is_fw_digit(s[1]) &&
9423 s[2] == t &&
9424 (s[3] == 'T' || s[3] == 'N') &&
9425 tpacpi_is_fw_digit(s[4]) &&
9426 tpacpi_is_fw_digit(s[5]))
9427 return true;
9429 /* New models: xxxyTkkW (#.##c); T550 and some others */
9430 return s && strlen(s) >= 8 &&
9431 tpacpi_is_fw_digit(s[0]) &&
9432 tpacpi_is_fw_digit(s[1]) &&
9433 tpacpi_is_fw_digit(s[2]) &&
9434 s[3] == t &&
9435 (s[4] == 'T' || s[4] == 'N') &&
9436 tpacpi_is_fw_digit(s[5]) &&
9437 tpacpi_is_fw_digit(s[6]);
9440 /* returns 0 - probe ok, or < 0 - probe error.
9441 * Probe ok doesn't mean thinkpad found.
9442 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9443 static int __must_check __init get_thinkpad_model_data(
9444 struct thinkpad_id_data *tp)
9446 const struct dmi_device *dev = NULL;
9447 char ec_fw_string[18];
9448 char const *s;
9450 if (!tp)
9451 return -EINVAL;
9453 memset(tp, 0, sizeof(*tp));
9455 if (dmi_name_in_vendors("IBM"))
9456 tp->vendor = PCI_VENDOR_ID_IBM;
9457 else if (dmi_name_in_vendors("LENOVO"))
9458 tp->vendor = PCI_VENDOR_ID_LENOVO;
9459 else
9460 return 0;
9462 s = dmi_get_system_info(DMI_BIOS_VERSION);
9463 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9464 if (s && !tp->bios_version_str)
9465 return -ENOMEM;
9467 /* Really ancient ThinkPad 240X will fail this, which is fine */
9468 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9469 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
9470 return 0;
9472 tp->bios_model = tp->bios_version_str[0]
9473 | (tp->bios_version_str[1] << 8);
9474 tp->bios_release = (tp->bios_version_str[4] << 8)
9475 | tp->bios_version_str[5];
9478 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9479 * X32 or newer, all Z series; Some models must have an
9480 * up-to-date BIOS or they will not be detected.
9482 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9484 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9485 if (sscanf(dev->name,
9486 "IBM ThinkPad Embedded Controller -[%17c",
9487 ec_fw_string) == 1) {
9488 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9489 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
9491 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
9492 if (!tp->ec_version_str)
9493 return -ENOMEM;
9495 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9496 tp->ec_model = ec_fw_string[0]
9497 | (ec_fw_string[1] << 8);
9498 tp->ec_release = (ec_fw_string[4] << 8)
9499 | ec_fw_string[5];
9500 } else {
9501 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
9502 ec_fw_string);
9503 pr_notice("please report this to %s\n",
9504 TPACPI_MAIL);
9506 break;
9510 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
9511 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
9512 tp->model_str = kstrdup(s, GFP_KERNEL);
9513 if (!tp->model_str)
9514 return -ENOMEM;
9515 } else {
9516 s = dmi_get_system_info(DMI_BIOS_VENDOR);
9517 if (s && !(strncasecmp(s, "Lenovo", 6))) {
9518 tp->model_str = kstrdup(s, GFP_KERNEL);
9519 if (!tp->model_str)
9520 return -ENOMEM;
9524 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9525 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9526 if (s && !tp->nummodel_str)
9527 return -ENOMEM;
9529 return 0;
9532 static int __init probe_for_thinkpad(void)
9534 int is_thinkpad;
9536 if (acpi_disabled)
9537 return -ENODEV;
9539 /* It would be dangerous to run the driver in this case */
9540 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9541 return -ENODEV;
9544 * Non-ancient models have better DMI tagging, but very old models
9545 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9547 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9548 (thinkpad_id.ec_model != 0) ||
9549 tpacpi_is_fw_known();
9551 /* The EC handler is required */
9552 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
9553 if (!ec_handle) {
9554 if (is_thinkpad)
9555 pr_err("Not yet supported ThinkPad detected!\n");
9556 return -ENODEV;
9559 if (!is_thinkpad && !force_load)
9560 return -ENODEV;
9562 return 0;
9565 static void __init thinkpad_acpi_init_banner(void)
9567 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9568 pr_info("%s\n", TPACPI_URL);
9570 pr_info("ThinkPad BIOS %s, EC %s\n",
9571 (thinkpad_id.bios_version_str) ?
9572 thinkpad_id.bios_version_str : "unknown",
9573 (thinkpad_id.ec_version_str) ?
9574 thinkpad_id.ec_version_str : "unknown");
9576 BUG_ON(!thinkpad_id.vendor);
9578 if (thinkpad_id.model_str)
9579 pr_info("%s %s, model %s\n",
9580 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9581 "IBM" : ((thinkpad_id.vendor ==
9582 PCI_VENDOR_ID_LENOVO) ?
9583 "Lenovo" : "Unknown vendor"),
9584 thinkpad_id.model_str,
9585 (thinkpad_id.nummodel_str) ?
9586 thinkpad_id.nummodel_str : "unknown");
9589 /* Module init, exit, parameters */
9591 static struct ibm_init_struct ibms_init[] __initdata = {
9593 .data = &thinkpad_acpi_driver_data,
9596 .init = hotkey_init,
9597 .data = &hotkey_driver_data,
9600 .init = bluetooth_init,
9601 .data = &bluetooth_driver_data,
9604 .init = wan_init,
9605 .data = &wan_driver_data,
9608 .init = uwb_init,
9609 .data = &uwb_driver_data,
9611 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9613 .init = video_init,
9614 .base_procfs_mode = S_IRUSR,
9615 .data = &video_driver_data,
9617 #endif
9619 .init = kbdlight_init,
9620 .data = &kbdlight_driver_data,
9623 .init = light_init,
9624 .data = &light_driver_data,
9627 .init = cmos_init,
9628 .data = &cmos_driver_data,
9631 .init = led_init,
9632 .data = &led_driver_data,
9635 .init = beep_init,
9636 .data = &beep_driver_data,
9639 .init = thermal_init,
9640 .data = &thermal_driver_data,
9643 .init = brightness_init,
9644 .data = &brightness_driver_data,
9647 .init = volume_init,
9648 .data = &volume_driver_data,
9651 .init = fan_init,
9652 .data = &fan_driver_data,
9655 .init = mute_led_init,
9656 .data = &mute_led_driver_data,
9660 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
9662 unsigned int i;
9663 struct ibm_struct *ibm;
9665 if (!kp || !kp->name || !val)
9666 return -EINVAL;
9668 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9669 ibm = ibms_init[i].data;
9670 WARN_ON(ibm == NULL);
9672 if (!ibm || !ibm->name)
9673 continue;
9675 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9676 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
9677 return -ENOSPC;
9678 strcpy(ibms_init[i].param, val);
9679 strcat(ibms_init[i].param, ",");
9680 return 0;
9684 return -EINVAL;
9687 module_param(experimental, int, 0444);
9688 MODULE_PARM_DESC(experimental,
9689 "Enables experimental features when non-zero");
9691 module_param_named(debug, dbg_level, uint, 0);
9692 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
9694 module_param(force_load, bool, 0444);
9695 MODULE_PARM_DESC(force_load,
9696 "Attempts to load the driver even on a mis-identified ThinkPad when true");
9698 module_param_named(fan_control, fan_control_allowed, bool, 0444);
9699 MODULE_PARM_DESC(fan_control,
9700 "Enables setting fan parameters features when true");
9702 module_param_named(brightness_mode, brightness_mode, uint, 0444);
9703 MODULE_PARM_DESC(brightness_mode,
9704 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9706 module_param(brightness_enable, uint, 0444);
9707 MODULE_PARM_DESC(brightness_enable,
9708 "Enables backlight control when 1, disables when 0");
9710 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9711 module_param_named(volume_mode, volume_mode, uint, 0444);
9712 MODULE_PARM_DESC(volume_mode,
9713 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9715 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9716 MODULE_PARM_DESC(volume_capabilities,
9717 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
9719 module_param_named(volume_control, volume_control_allowed, bool, 0444);
9720 MODULE_PARM_DESC(volume_control,
9721 "Enables software override for the console audio control when true");
9723 module_param_named(software_mute, software_mute_requested, bool, 0444);
9724 MODULE_PARM_DESC(software_mute,
9725 "Request full software mute control");
9727 /* ALSA module API parameters */
9728 module_param_named(index, alsa_index, int, 0444);
9729 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9730 module_param_named(id, alsa_id, charp, 0444);
9731 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9732 module_param_named(enable, alsa_enable, bool, 0444);
9733 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
9734 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9736 /* The module parameter can't be read back, that's why 0 is used here */
9737 #define TPACPI_PARAM(feature) \
9738 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9739 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
9741 TPACPI_PARAM(hotkey);
9742 TPACPI_PARAM(bluetooth);
9743 TPACPI_PARAM(video);
9744 TPACPI_PARAM(light);
9745 TPACPI_PARAM(cmos);
9746 TPACPI_PARAM(led);
9747 TPACPI_PARAM(beep);
9748 TPACPI_PARAM(brightness);
9749 TPACPI_PARAM(volume);
9750 TPACPI_PARAM(fan);
9752 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9753 module_param(dbg_wlswemul, uint, 0444);
9754 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9755 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9756 MODULE_PARM_DESC(wlsw_state,
9757 "Initial state of the emulated WLSW switch");
9759 module_param(dbg_bluetoothemul, uint, 0444);
9760 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9761 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9762 MODULE_PARM_DESC(bluetooth_state,
9763 "Initial state of the emulated bluetooth switch");
9765 module_param(dbg_wwanemul, uint, 0444);
9766 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9767 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9768 MODULE_PARM_DESC(wwan_state,
9769 "Initial state of the emulated WWAN switch");
9771 module_param(dbg_uwbemul, uint, 0444);
9772 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9773 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9774 MODULE_PARM_DESC(uwb_state,
9775 "Initial state of the emulated UWB switch");
9776 #endif
9778 static void thinkpad_acpi_module_exit(void)
9780 struct ibm_struct *ibm, *itmp;
9782 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9784 list_for_each_entry_safe_reverse(ibm, itmp,
9785 &tpacpi_all_drivers,
9786 all_drivers) {
9787 ibm_exit(ibm);
9790 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9792 if (tpacpi_inputdev) {
9793 if (tp_features.input_device_registered)
9794 input_unregister_device(tpacpi_inputdev);
9795 else
9796 input_free_device(tpacpi_inputdev);
9797 kfree(hotkey_keycode_map);
9800 if (tpacpi_hwmon)
9801 hwmon_device_unregister(tpacpi_hwmon);
9803 if (tpacpi_sensors_pdev)
9804 platform_device_unregister(tpacpi_sensors_pdev);
9805 if (tpacpi_pdev)
9806 platform_device_unregister(tpacpi_pdev);
9808 if (tp_features.sensors_pdrv_attrs_registered)
9809 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9810 if (tp_features.platform_drv_attrs_registered)
9811 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9813 if (tp_features.sensors_pdrv_registered)
9814 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9816 if (tp_features.platform_drv_registered)
9817 platform_driver_unregister(&tpacpi_pdriver);
9819 if (proc_dir)
9820 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9822 if (tpacpi_wq)
9823 destroy_workqueue(tpacpi_wq);
9825 kfree(thinkpad_id.bios_version_str);
9826 kfree(thinkpad_id.ec_version_str);
9827 kfree(thinkpad_id.model_str);
9828 kfree(thinkpad_id.nummodel_str);
9832 static int __init thinkpad_acpi_module_init(void)
9834 int ret, i;
9836 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9838 /* Driver-level probe */
9840 ret = get_thinkpad_model_data(&thinkpad_id);
9841 if (ret) {
9842 pr_err("unable to get DMI data: %d\n", ret);
9843 thinkpad_acpi_module_exit();
9844 return ret;
9846 ret = probe_for_thinkpad();
9847 if (ret) {
9848 thinkpad_acpi_module_exit();
9849 return ret;
9852 /* Driver initialization */
9854 thinkpad_acpi_init_banner();
9855 tpacpi_check_outdated_fw();
9857 TPACPI_ACPIHANDLE_INIT(ecrd);
9858 TPACPI_ACPIHANDLE_INIT(ecwr);
9860 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9861 if (!tpacpi_wq) {
9862 thinkpad_acpi_module_exit();
9863 return -ENOMEM;
9866 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9867 if (!proc_dir) {
9868 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9869 thinkpad_acpi_module_exit();
9870 return -ENODEV;
9873 ret = platform_driver_register(&tpacpi_pdriver);
9874 if (ret) {
9875 pr_err("unable to register main platform driver\n");
9876 thinkpad_acpi_module_exit();
9877 return ret;
9879 tp_features.platform_drv_registered = 1;
9881 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9882 if (ret) {
9883 pr_err("unable to register hwmon platform driver\n");
9884 thinkpad_acpi_module_exit();
9885 return ret;
9887 tp_features.sensors_pdrv_registered = 1;
9889 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9890 if (!ret) {
9891 tp_features.platform_drv_attrs_registered = 1;
9892 ret = tpacpi_create_driver_attributes(
9893 &tpacpi_hwmon_pdriver.driver);
9895 if (ret) {
9896 pr_err("unable to create sysfs driver attributes\n");
9897 thinkpad_acpi_module_exit();
9898 return ret;
9900 tp_features.sensors_pdrv_attrs_registered = 1;
9903 /* Device initialization */
9904 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9905 NULL, 0);
9906 if (IS_ERR(tpacpi_pdev)) {
9907 ret = PTR_ERR(tpacpi_pdev);
9908 tpacpi_pdev = NULL;
9909 pr_err("unable to register platform device\n");
9910 thinkpad_acpi_module_exit();
9911 return ret;
9913 tpacpi_sensors_pdev = platform_device_register_simple(
9914 TPACPI_HWMON_DRVR_NAME,
9915 -1, NULL, 0);
9916 if (IS_ERR(tpacpi_sensors_pdev)) {
9917 ret = PTR_ERR(tpacpi_sensors_pdev);
9918 tpacpi_sensors_pdev = NULL;
9919 pr_err("unable to register hwmon platform device\n");
9920 thinkpad_acpi_module_exit();
9921 return ret;
9923 tp_features.sensors_pdev_attrs_registered = 1;
9924 tpacpi_hwmon = hwmon_device_register_with_groups(
9925 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
9927 if (IS_ERR(tpacpi_hwmon)) {
9928 ret = PTR_ERR(tpacpi_hwmon);
9929 tpacpi_hwmon = NULL;
9930 pr_err("unable to register hwmon device\n");
9931 thinkpad_acpi_module_exit();
9932 return ret;
9934 mutex_init(&tpacpi_inputdev_send_mutex);
9935 tpacpi_inputdev = input_allocate_device();
9936 if (!tpacpi_inputdev) {
9937 thinkpad_acpi_module_exit();
9938 return -ENOMEM;
9939 } else {
9940 /* Prepare input device, but don't register */
9941 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9942 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9943 tpacpi_inputdev->id.bustype = BUS_HOST;
9944 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9945 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9946 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9947 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9950 /* Init subdriver dependencies */
9951 tpacpi_detect_brightness_capabilities();
9953 /* Init subdrivers */
9954 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9955 ret = ibm_init(&ibms_init[i]);
9956 if (ret >= 0 && *ibms_init[i].param)
9957 ret = ibms_init[i].data->write(ibms_init[i].param);
9958 if (ret < 0) {
9959 thinkpad_acpi_module_exit();
9960 return ret;
9964 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9966 ret = input_register_device(tpacpi_inputdev);
9967 if (ret < 0) {
9968 pr_err("unable to register input device\n");
9969 thinkpad_acpi_module_exit();
9970 return ret;
9971 } else {
9972 tp_features.input_device_registered = 1;
9975 return 0;
9978 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9981 * This will autoload the driver in almost every ThinkPad
9982 * in widespread use.
9984 * Only _VERY_ old models, like the 240, 240x and 570 lack
9985 * the HKEY event interface.
9987 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9990 * DMI matching for module autoloading
9992 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9993 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9995 * Only models listed in thinkwiki will be supported, so add yours
9996 * if it is not there yet.
9998 #define IBM_BIOS_MODULE_ALIAS(__type) \
9999 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10001 /* Ancient thinkpad BIOSes have to be identified by
10002 * BIOS type or model number, and there are far less
10003 * BIOS types than model numbers... */
10004 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
10006 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10007 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
10008 MODULE_DESCRIPTION(TPACPI_DESC);
10009 MODULE_VERSION(TPACPI_VERSION);
10010 MODULE_LICENSE("GPL");
10012 module_init(thinkpad_acpi_module_init);
10013 module_exit(thinkpad_acpi_module_exit);