treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / platform / x86 / thinkpad_acpi.c
blob8eaadbaf8ffa5281d6db62f9f9152c783e0c69c8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * 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>
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define TPACPI_VERSION "0.26"
12 #define TPACPI_SYSFS_VERSION 0x030000
15 * Changelog:
16 * 2007-10-20 changelog trimmed down
18 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
19 * drivers/misc.
21 * 2006-11-22 0.13 new maintainer
22 * changelog now lives in git commit history, and will
23 * not be updated further in-file.
25 * 2005-03-17 0.11 support for 600e, 770x
26 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
28 * 2005-01-16 0.9 use MODULE_VERSION
29 * thanks to Henrik Brix Andersen <brix@gentoo.org>
30 * fix parameter passing on module loading
31 * thanks to Rusty Russell <rusty@rustcorp.com.au>
32 * thanks to Jim Radford <radford@blackbean.org>
33 * 2004-11-08 0.8 fix init error case, don't return from a macro
34 * thanks to Chris Wright <chrisw@osdl.org>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/string.h>
42 #include <linux/list.h>
43 #include <linux/mutex.h>
44 #include <linux/sched.h>
45 #include <linux/sched/signal.h>
46 #include <linux/kthread.h>
47 #include <linux/freezer.h>
48 #include <linux/delay.h>
49 #include <linux/slab.h>
50 #include <linux/nvram.h>
51 #include <linux/proc_fs.h>
52 #include <linux/seq_file.h>
53 #include <linux/sysfs.h>
54 #include <linux/backlight.h>
55 #include <linux/bitops.h>
56 #include <linux/fb.h>
57 #include <linux/platform_device.h>
58 #include <linux/hwmon.h>
59 #include <linux/hwmon-sysfs.h>
60 #include <linux/input.h>
61 #include <linux/leds.h>
62 #include <linux/rfkill.h>
63 #include <linux/dmi.h>
64 #include <linux/jiffies.h>
65 #include <linux/workqueue.h>
66 #include <linux/acpi.h>
67 #include <linux/pci.h>
68 #include <linux/power_supply.h>
69 #include <sound/core.h>
70 #include <sound/control.h>
71 #include <sound/initval.h>
72 #include <linux/uaccess.h>
73 #include <acpi/battery.h>
74 #include <acpi/video.h>
76 /* ThinkPad CMOS commands */
77 #define TP_CMOS_VOLUME_DOWN 0
78 #define TP_CMOS_VOLUME_UP 1
79 #define TP_CMOS_VOLUME_MUTE 2
80 #define TP_CMOS_BRIGHTNESS_UP 4
81 #define TP_CMOS_BRIGHTNESS_DOWN 5
82 #define TP_CMOS_THINKLIGHT_ON 12
83 #define TP_CMOS_THINKLIGHT_OFF 13
85 /* NVRAM Addresses */
86 enum tp_nvram_addr {
87 TP_NVRAM_ADDR_HK2 = 0x57,
88 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
89 TP_NVRAM_ADDR_VIDEO = 0x59,
90 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
91 TP_NVRAM_ADDR_MIXER = 0x60,
94 /* NVRAM bit masks */
95 enum {
96 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
97 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
98 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
99 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
100 TP_NVRAM_MASK_THINKLIGHT = 0x10,
101 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
102 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
103 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
104 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
105 TP_NVRAM_MASK_MUTE = 0x40,
106 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
107 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
108 TP_NVRAM_POS_LEVEL_VOLUME = 0,
111 /* Misc NVRAM-related */
112 enum {
113 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
116 /* ACPI HIDs */
117 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
118 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
119 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
120 #define TPACPI_ACPI_EC_HID "PNP0C09"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /* HKEY events */
149 enum tpacpi_hkey_event_t {
150 /* Hotkey-related */
151 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
154 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
155 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
156 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
157 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
159 /* Reasons for waking up from S3/S4 */
160 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
161 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
162 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
163 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
164 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
165 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
167 /* Auto-sleep after eject request */
168 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
169 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
171 /* Misc bay events */
172 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
173 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
174 or port replicator */
175 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
176 dock or port replicator */
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
183 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
184 * enter/leave tablet mode
186 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
187 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
188 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
190 /* Key-related user-interface events */
191 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
192 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
193 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
195 /* Thermal events */
196 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
197 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
198 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
199 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
200 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* windows; thermal table changed */
201 TP_HKEY_EV_THM_CSM_COMPLETED = 0x6032, /* windows; thermal control set
202 * command completed. Related to
203 * AML DYTC */
204 TP_HKEY_EV_THM_TRANSFM_CHANGED = 0x60F0, /* windows; thermal transformation
205 * changed. Related to AML GMTS */
207 /* AC-related events */
208 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
210 /* Further user-interface events */
211 TP_HKEY_EV_PALM_DETECTED = 0x60b0, /* palm hoveres keyboard */
212 TP_HKEY_EV_PALM_UNDETECTED = 0x60b1, /* palm removed */
214 /* Misc */
215 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
218 /****************************************************************************
219 * Main driver
222 #define TPACPI_NAME "thinkpad"
223 #define TPACPI_DESC "ThinkPad ACPI Extras"
224 #define TPACPI_FILE TPACPI_NAME "_acpi"
225 #define TPACPI_URL "http://ibm-acpi.sf.net/"
226 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
228 #define TPACPI_PROC_DIR "ibm"
229 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
230 #define TPACPI_DRVR_NAME TPACPI_FILE
231 #define TPACPI_DRVR_SHORTNAME "tpacpi"
232 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
234 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
235 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
237 #define TPACPI_MAX_ACPI_ARGS 3
239 /* Debugging printk groups */
240 #define TPACPI_DBG_ALL 0xffff
241 #define TPACPI_DBG_DISCLOSETASK 0x8000
242 #define TPACPI_DBG_INIT 0x0001
243 #define TPACPI_DBG_EXIT 0x0002
244 #define TPACPI_DBG_RFKILL 0x0004
245 #define TPACPI_DBG_HKEY 0x0008
246 #define TPACPI_DBG_FAN 0x0010
247 #define TPACPI_DBG_BRGHT 0x0020
248 #define TPACPI_DBG_MIXER 0x0040
250 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
251 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
252 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
255 /****************************************************************************
256 * Driver-wide structs and misc. variables
259 struct ibm_struct;
261 struct tp_acpi_drv_struct {
262 const struct acpi_device_id *hid;
263 struct acpi_driver *driver;
265 void (*notify) (struct ibm_struct *, u32);
266 acpi_handle *handle;
267 u32 type;
268 struct acpi_device *device;
271 struct ibm_struct {
272 char *name;
274 int (*read) (struct seq_file *);
275 int (*write) (char *);
276 void (*exit) (void);
277 void (*resume) (void);
278 void (*suspend) (void);
279 void (*shutdown) (void);
281 struct list_head all_drivers;
283 struct tp_acpi_drv_struct *acpi;
285 struct {
286 u8 acpi_driver_registered:1;
287 u8 acpi_notify_installed:1;
288 u8 proc_created:1;
289 u8 init_called:1;
290 u8 experimental:1;
291 } flags;
294 struct ibm_init_struct {
295 char param[32];
297 int (*init) (struct ibm_init_struct *);
298 umode_t base_procfs_mode;
299 struct ibm_struct *data;
302 static struct {
303 u32 bluetooth:1;
304 u32 hotkey:1;
305 u32 hotkey_mask:1;
306 u32 hotkey_wlsw:1;
307 enum {
308 TP_HOTKEY_TABLET_NONE = 0,
309 TP_HOTKEY_TABLET_USES_MHKG,
310 TP_HOTKEY_TABLET_USES_GMMS,
311 } hotkey_tablet;
312 u32 kbdlight:1;
313 u32 light:1;
314 u32 light_status:1;
315 u32 bright_acpimode:1;
316 u32 bright_unkfw:1;
317 u32 wan:1;
318 u32 uwb:1;
319 u32 fan_ctrl_status_undef:1;
320 u32 second_fan:1;
321 u32 beep_needs_two_args:1;
322 u32 mixer_no_level_control:1;
323 u32 battery_force_primary:1;
324 u32 input_device_registered:1;
325 u32 platform_drv_registered:1;
326 u32 platform_drv_attrs_registered:1;
327 u32 sensors_pdrv_registered:1;
328 u32 sensors_pdrv_attrs_registered:1;
329 u32 sensors_pdev_attrs_registered:1;
330 u32 hotkey_poll_active:1;
331 u32 has_adaptive_kbd:1;
332 } tp_features;
334 static struct {
335 u16 hotkey_mask_ff:1;
336 u16 volume_ctrl_forbidden:1;
337 } tp_warned;
339 struct thinkpad_id_data {
340 unsigned int vendor; /* ThinkPad vendor:
341 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
343 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
344 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
346 u32 bios_model; /* 1Y = 0x3159, 0 = unknown */
347 u32 ec_model;
348 u16 bios_release; /* 1ZETK1WW = 0x4b31, 0 = unknown */
349 u16 ec_release;
351 char *model_str; /* ThinkPad T43 */
352 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
354 static struct thinkpad_id_data thinkpad_id;
356 static enum {
357 TPACPI_LIFE_INIT = 0,
358 TPACPI_LIFE_RUNNING,
359 TPACPI_LIFE_EXITING,
360 } tpacpi_lifecycle;
362 static int experimental;
363 static u32 dbg_level;
365 static struct workqueue_struct *tpacpi_wq;
367 enum led_status_t {
368 TPACPI_LED_OFF = 0,
369 TPACPI_LED_ON,
370 TPACPI_LED_BLINK,
373 /* tpacpi LED class */
374 struct tpacpi_led_classdev {
375 struct led_classdev led_classdev;
376 int led;
379 /* brightness level capabilities */
380 static unsigned int bright_maxlvl; /* 0 = unknown */
382 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
383 static int dbg_wlswemul;
384 static bool tpacpi_wlsw_emulstate;
385 static int dbg_bluetoothemul;
386 static bool tpacpi_bluetooth_emulstate;
387 static int dbg_wwanemul;
388 static bool tpacpi_wwan_emulstate;
389 static int dbg_uwbemul;
390 static bool tpacpi_uwb_emulstate;
391 #endif
394 /*************************************************************************
395 * Debugging helpers
398 #define dbg_printk(a_dbg_level, format, arg...) \
399 do { \
400 if (dbg_level & (a_dbg_level)) \
401 printk(KERN_DEBUG pr_fmt("%s: " format), \
402 __func__, ##arg); \
403 } while (0)
405 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
406 #define vdbg_printk dbg_printk
407 static const char *str_supported(int is_supported);
408 #else
409 static inline const char *str_supported(int is_supported) { return ""; }
410 #define vdbg_printk(a_dbg_level, format, arg...) \
411 do { if (0) no_printk(format, ##arg); } while (0)
412 #endif
414 static void tpacpi_log_usertask(const char * const what)
416 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
417 what, task_tgid_vnr(current));
420 #define tpacpi_disclose_usertask(what, format, arg...) \
421 do { \
422 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
423 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
424 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
425 what, task_tgid_vnr(current), ## arg); \
427 } while (0)
430 * Quirk handling helpers
432 * ThinkPad IDs and versions seen in the field so far are
433 * two or three characters from the set [0-9A-Z], i.e. base 36.
435 * We use values well outside that range as specials.
438 #define TPACPI_MATCH_ANY 0xffffffffU
439 #define TPACPI_MATCH_ANY_VERSION 0xffffU
440 #define TPACPI_MATCH_UNKNOWN 0U
442 /* TPID('1', 'Y') == 0x3159 */
443 #define TPID(__c1, __c2) (((__c1) << 8) | (__c2))
444 #define TPID3(__c1, __c2, __c3) (((__c1) << 16) | ((__c2) << 8) | (__c3))
445 #define TPVER TPID
447 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
453 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
459 #define TPACPI_Q_LNV3(__id1, __id2, __id3, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPID3(__id1, __id2, __id3), \
462 .ec = TPACPI_MATCH_ANY, \
463 .quirks = (__quirk) }
465 #define TPACPI_QEC_IBM(__id1, __id2, __quirk) \
466 { .vendor = PCI_VENDOR_ID_IBM, \
467 .bios = TPACPI_MATCH_ANY, \
468 .ec = TPID(__id1, __id2), \
469 .quirks = (__quirk) }
471 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
472 { .vendor = PCI_VENDOR_ID_LENOVO, \
473 .bios = TPACPI_MATCH_ANY, \
474 .ec = TPID(__id1, __id2), \
475 .quirks = (__quirk) }
477 struct tpacpi_quirk {
478 unsigned int vendor;
479 u32 bios;
480 u32 ec;
481 unsigned long quirks;
485 * tpacpi_check_quirks() - search BIOS/EC version on a list
486 * @qlist: array of &struct tpacpi_quirk
487 * @qlist_size: number of elements in @qlist
489 * Iterates over a quirks list until one is found that matches the
490 * ThinkPad's vendor, BIOS and EC model.
492 * Returns 0 if nothing matches, otherwise returns the quirks field of
493 * the matching &struct tpacpi_quirk entry.
495 * The match criteria is: vendor, ec and bios much match.
497 static unsigned long __init tpacpi_check_quirks(
498 const struct tpacpi_quirk *qlist,
499 unsigned int qlist_size)
501 while (qlist_size) {
502 if ((qlist->vendor == thinkpad_id.vendor ||
503 qlist->vendor == TPACPI_MATCH_ANY) &&
504 (qlist->bios == thinkpad_id.bios_model ||
505 qlist->bios == TPACPI_MATCH_ANY) &&
506 (qlist->ec == thinkpad_id.ec_model ||
507 qlist->ec == TPACPI_MATCH_ANY))
508 return qlist->quirks;
510 qlist_size--;
511 qlist++;
513 return 0;
516 static inline bool __pure __init tpacpi_is_lenovo(void)
518 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
521 static inline bool __pure __init tpacpi_is_ibm(void)
523 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
526 /****************************************************************************
527 ****************************************************************************
529 * ACPI Helpers and device model
531 ****************************************************************************
532 ****************************************************************************/
534 /*************************************************************************
535 * ACPI basic handles
538 static acpi_handle root_handle;
539 static acpi_handle ec_handle;
541 #define TPACPI_HANDLE(object, parent, paths...) \
542 static acpi_handle object##_handle; \
543 static const acpi_handle * const object##_parent __initconst = \
544 &parent##_handle; \
545 static char *object##_paths[] __initdata = { paths }
547 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
548 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
550 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
551 /* T4x, X31, X40 */
552 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
553 "\\CMS", /* R40, R40e */
554 ); /* all others */
556 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
557 "^HKEY", /* R30, R31 */
558 "HKEY", /* all others */
559 ); /* 570 */
561 /*************************************************************************
562 * ACPI helpers
565 static int acpi_evalf(acpi_handle handle,
566 int *res, char *method, char *fmt, ...)
568 char *fmt0 = fmt;
569 struct acpi_object_list params;
570 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
571 struct acpi_buffer result, *resultp;
572 union acpi_object out_obj;
573 acpi_status status;
574 va_list ap;
575 char res_type;
576 int success;
577 int quiet;
579 if (!*fmt) {
580 pr_err("acpi_evalf() called with empty format\n");
581 return 0;
584 if (*fmt == 'q') {
585 quiet = 1;
586 fmt++;
587 } else
588 quiet = 0;
590 res_type = *(fmt++);
592 params.count = 0;
593 params.pointer = &in_objs[0];
595 va_start(ap, fmt);
596 while (*fmt) {
597 char c = *(fmt++);
598 switch (c) {
599 case 'd': /* int */
600 in_objs[params.count].integer.value = va_arg(ap, int);
601 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
602 break;
603 /* add more types as needed */
604 default:
605 pr_err("acpi_evalf() called with invalid format character '%c'\n",
607 va_end(ap);
608 return 0;
611 va_end(ap);
613 if (res_type != 'v') {
614 result.length = sizeof(out_obj);
615 result.pointer = &out_obj;
616 resultp = &result;
617 } else
618 resultp = NULL;
620 status = acpi_evaluate_object(handle, method, &params, resultp);
622 switch (res_type) {
623 case 'd': /* int */
624 success = (status == AE_OK &&
625 out_obj.type == ACPI_TYPE_INTEGER);
626 if (success && res)
627 *res = out_obj.integer.value;
628 break;
629 case 'v': /* void */
630 success = status == AE_OK;
631 break;
632 /* add more types as needed */
633 default:
634 pr_err("acpi_evalf() called with invalid format character '%c'\n",
635 res_type);
636 return 0;
639 if (!success && !quiet)
640 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
641 method, fmt0, acpi_format_exception(status));
643 return success;
646 static int acpi_ec_read(int i, u8 *p)
648 int v;
650 if (ecrd_handle) {
651 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
652 return 0;
653 *p = v;
654 } else {
655 if (ec_read(i, p) < 0)
656 return 0;
659 return 1;
662 static int acpi_ec_write(int i, u8 v)
664 if (ecwr_handle) {
665 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
666 return 0;
667 } else {
668 if (ec_write(i, v) < 0)
669 return 0;
672 return 1;
675 static int issue_thinkpad_cmos_command(int cmos_cmd)
677 if (!cmos_handle)
678 return -ENXIO;
680 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
681 return -EIO;
683 return 0;
686 /*************************************************************************
687 * ACPI device model
690 #define TPACPI_ACPIHANDLE_INIT(object) \
691 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
692 object##_paths, ARRAY_SIZE(object##_paths))
694 static void __init drv_acpi_handle_init(const char *name,
695 acpi_handle *handle, const acpi_handle parent,
696 char **paths, const int num_paths)
698 int i;
699 acpi_status status;
701 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
702 name);
704 for (i = 0; i < num_paths; i++) {
705 status = acpi_get_handle(parent, paths[i], handle);
706 if (ACPI_SUCCESS(status)) {
707 dbg_printk(TPACPI_DBG_INIT,
708 "Found ACPI handle %s for %s\n",
709 paths[i], name);
710 return;
714 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
715 name);
716 *handle = NULL;
719 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
720 u32 level, void *context, void **return_value)
722 struct acpi_device *dev;
723 if (!strcmp(context, "video")) {
724 if (acpi_bus_get_device(handle, &dev))
725 return AE_OK;
726 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
727 return AE_OK;
730 *(acpi_handle *)return_value = handle;
732 return AE_CTRL_TERMINATE;
735 static void __init tpacpi_acpi_handle_locate(const char *name,
736 const char *hid,
737 acpi_handle *handle)
739 acpi_status status;
740 acpi_handle device_found;
742 BUG_ON(!name || !handle);
743 vdbg_printk(TPACPI_DBG_INIT,
744 "trying to locate ACPI handle for %s, using HID %s\n",
745 name, hid ? hid : "NULL");
747 memset(&device_found, 0, sizeof(device_found));
748 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
749 (void *)name, &device_found);
751 *handle = NULL;
753 if (ACPI_SUCCESS(status)) {
754 *handle = device_found;
755 dbg_printk(TPACPI_DBG_INIT,
756 "Found ACPI handle for %s\n", name);
757 } else {
758 vdbg_printk(TPACPI_DBG_INIT,
759 "Could not locate an ACPI handle for %s: %s\n",
760 name, acpi_format_exception(status));
764 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
766 struct ibm_struct *ibm = data;
768 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
769 return;
771 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
772 return;
774 ibm->acpi->notify(ibm, event);
777 static int __init setup_acpi_notify(struct ibm_struct *ibm)
779 acpi_status status;
780 int rc;
782 BUG_ON(!ibm->acpi);
784 if (!*ibm->acpi->handle)
785 return 0;
787 vdbg_printk(TPACPI_DBG_INIT,
788 "setting up ACPI notify for %s\n", ibm->name);
790 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
791 if (rc < 0) {
792 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
793 return -ENODEV;
796 ibm->acpi->device->driver_data = ibm;
797 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
798 TPACPI_ACPI_EVENT_PREFIX,
799 ibm->name);
801 status = acpi_install_notify_handler(*ibm->acpi->handle,
802 ibm->acpi->type, dispatch_acpi_notify, ibm);
803 if (ACPI_FAILURE(status)) {
804 if (status == AE_ALREADY_EXISTS) {
805 pr_notice("another device driver is already handling %s events\n",
806 ibm->name);
807 } else {
808 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
809 ibm->name, acpi_format_exception(status));
811 return -ENODEV;
813 ibm->flags.acpi_notify_installed = 1;
814 return 0;
817 static int __init tpacpi_device_add(struct acpi_device *device)
819 return 0;
822 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
824 int rc;
826 dbg_printk(TPACPI_DBG_INIT,
827 "registering %s as an ACPI driver\n", ibm->name);
829 BUG_ON(!ibm->acpi);
831 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
832 if (!ibm->acpi->driver) {
833 pr_err("failed to allocate memory for ibm->acpi->driver\n");
834 return -ENOMEM;
837 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
838 ibm->acpi->driver->ids = ibm->acpi->hid;
840 ibm->acpi->driver->ops.add = &tpacpi_device_add;
842 rc = acpi_bus_register_driver(ibm->acpi->driver);
843 if (rc < 0) {
844 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
845 ibm->name, rc);
846 kfree(ibm->acpi->driver);
847 ibm->acpi->driver = NULL;
848 } else if (!rc)
849 ibm->flags.acpi_driver_registered = 1;
851 return rc;
855 /****************************************************************************
856 ****************************************************************************
858 * Procfs Helpers
860 ****************************************************************************
861 ****************************************************************************/
863 static int dispatch_proc_show(struct seq_file *m, void *v)
865 struct ibm_struct *ibm = m->private;
867 if (!ibm || !ibm->read)
868 return -EINVAL;
869 return ibm->read(m);
872 static int dispatch_proc_open(struct inode *inode, struct file *file)
874 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
877 static ssize_t dispatch_proc_write(struct file *file,
878 const char __user *userbuf,
879 size_t count, loff_t *pos)
881 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
882 char *kernbuf;
883 int ret;
885 if (!ibm || !ibm->write)
886 return -EINVAL;
887 if (count > PAGE_SIZE - 2)
888 return -EINVAL;
890 kernbuf = kmalloc(count + 2, GFP_KERNEL);
891 if (!kernbuf)
892 return -ENOMEM;
894 if (copy_from_user(kernbuf, userbuf, count)) {
895 kfree(kernbuf);
896 return -EFAULT;
899 kernbuf[count] = 0;
900 strcat(kernbuf, ",");
901 ret = ibm->write(kernbuf);
902 if (ret == 0)
903 ret = count;
905 kfree(kernbuf);
907 return ret;
910 static const struct proc_ops dispatch_proc_ops = {
911 .proc_open = dispatch_proc_open,
912 .proc_read = seq_read,
913 .proc_lseek = seq_lseek,
914 .proc_release = single_release,
915 .proc_write = dispatch_proc_write,
918 static char *next_cmd(char **cmds)
920 char *start = *cmds;
921 char *end;
923 while ((end = strchr(start, ',')) && end == start)
924 start = end + 1;
926 if (!end)
927 return NULL;
929 *end = 0;
930 *cmds = end + 1;
931 return start;
935 /****************************************************************************
936 ****************************************************************************
938 * Device model: input, hwmon and platform
940 ****************************************************************************
941 ****************************************************************************/
943 static struct platform_device *tpacpi_pdev;
944 static struct platform_device *tpacpi_sensors_pdev;
945 static struct device *tpacpi_hwmon;
946 static struct input_dev *tpacpi_inputdev;
947 static struct mutex tpacpi_inputdev_send_mutex;
948 static LIST_HEAD(tpacpi_all_drivers);
950 #ifdef CONFIG_PM_SLEEP
951 static int tpacpi_suspend_handler(struct device *dev)
953 struct ibm_struct *ibm, *itmp;
955 list_for_each_entry_safe(ibm, itmp,
956 &tpacpi_all_drivers,
957 all_drivers) {
958 if (ibm->suspend)
959 (ibm->suspend)();
962 return 0;
965 static int tpacpi_resume_handler(struct device *dev)
967 struct ibm_struct *ibm, *itmp;
969 list_for_each_entry_safe(ibm, itmp,
970 &tpacpi_all_drivers,
971 all_drivers) {
972 if (ibm->resume)
973 (ibm->resume)();
976 return 0;
978 #endif
980 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
981 tpacpi_suspend_handler, tpacpi_resume_handler);
983 static void tpacpi_shutdown_handler(struct platform_device *pdev)
985 struct ibm_struct *ibm, *itmp;
987 list_for_each_entry_safe(ibm, itmp,
988 &tpacpi_all_drivers,
989 all_drivers) {
990 if (ibm->shutdown)
991 (ibm->shutdown)();
995 static struct platform_driver tpacpi_pdriver = {
996 .driver = {
997 .name = TPACPI_DRVR_NAME,
998 .pm = &tpacpi_pm,
1000 .shutdown = tpacpi_shutdown_handler,
1003 static struct platform_driver tpacpi_hwmon_pdriver = {
1004 .driver = {
1005 .name = TPACPI_HWMON_DRVR_NAME,
1009 /*************************************************************************
1010 * sysfs support helpers
1013 struct attribute_set {
1014 unsigned int members, max_members;
1015 struct attribute_group group;
1018 struct attribute_set_obj {
1019 struct attribute_set s;
1020 struct attribute *a;
1021 } __attribute__((packed));
1023 static struct attribute_set *create_attr_set(unsigned int max_members,
1024 const char *name)
1026 struct attribute_set_obj *sobj;
1028 if (max_members == 0)
1029 return NULL;
1031 /* Allocates space for implicit NULL at the end too */
1032 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1033 max_members * sizeof(struct attribute *),
1034 GFP_KERNEL);
1035 if (!sobj)
1036 return NULL;
1037 sobj->s.max_members = max_members;
1038 sobj->s.group.attrs = &sobj->a;
1039 sobj->s.group.name = name;
1041 return &sobj->s;
1044 #define destroy_attr_set(_set) \
1045 kfree(_set);
1047 /* not multi-threaded safe, use it in a single thread per set */
1048 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1050 if (!s || !attr)
1051 return -EINVAL;
1053 if (s->members >= s->max_members)
1054 return -ENOMEM;
1056 s->group.attrs[s->members] = attr;
1057 s->members++;
1059 return 0;
1062 static int add_many_to_attr_set(struct attribute_set *s,
1063 struct attribute **attr,
1064 unsigned int count)
1066 int i, res;
1068 for (i = 0; i < count; i++) {
1069 res = add_to_attr_set(s, attr[i]);
1070 if (res)
1071 return res;
1074 return 0;
1077 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1079 sysfs_remove_group(kobj, &s->group);
1080 destroy_attr_set(s);
1083 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1084 sysfs_create_group(_kobj, &_attr_set->group)
1086 static int parse_strtoul(const char *buf,
1087 unsigned long max, unsigned long *value)
1089 char *endp;
1091 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1092 endp = skip_spaces(endp);
1093 if (*endp || *value > max)
1094 return -EINVAL;
1096 return 0;
1099 static void tpacpi_disable_brightness_delay(void)
1101 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1102 pr_notice("ACPI backlight control delay disabled\n");
1105 static void printk_deprecated_attribute(const char * const what,
1106 const char * const details)
1108 tpacpi_log_usertask("deprecated sysfs attribute");
1109 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1110 what, details);
1113 /*************************************************************************
1114 * rfkill and radio control support helpers
1118 * ThinkPad-ACPI firmware handling model:
1120 * WLSW (master wireless switch) is event-driven, and is common to all
1121 * firmware-controlled radios. It cannot be controlled, just monitored,
1122 * as expected. It overrides all radio state in firmware
1124 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1125 * (TODO: verify how WLSW interacts with the returned radio state).
1127 * The only time there are shadow radio state changes, is when
1128 * masked-off hotkeys are used.
1132 * Internal driver API for radio state:
1134 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1135 * bool: true means radio blocked (off)
1137 enum tpacpi_rfkill_state {
1138 TPACPI_RFK_RADIO_OFF = 0,
1139 TPACPI_RFK_RADIO_ON
1142 /* rfkill switches */
1143 enum tpacpi_rfk_id {
1144 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1145 TPACPI_RFK_WWAN_SW_ID,
1146 TPACPI_RFK_UWB_SW_ID,
1147 TPACPI_RFK_SW_MAX
1150 static const char *tpacpi_rfkill_names[] = {
1151 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1152 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1153 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1154 [TPACPI_RFK_SW_MAX] = NULL
1157 /* ThinkPad-ACPI rfkill subdriver */
1158 struct tpacpi_rfk {
1159 struct rfkill *rfkill;
1160 enum tpacpi_rfk_id id;
1161 const struct tpacpi_rfk_ops *ops;
1164 struct tpacpi_rfk_ops {
1165 /* firmware interface */
1166 int (*get_status)(void);
1167 int (*set_status)(const enum tpacpi_rfkill_state);
1170 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1172 /* Query FW and update rfkill sw state for a given rfkill switch */
1173 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1175 int status;
1177 if (!tp_rfk)
1178 return -ENODEV;
1180 status = (tp_rfk->ops->get_status)();
1181 if (status < 0)
1182 return status;
1184 rfkill_set_sw_state(tp_rfk->rfkill,
1185 (status == TPACPI_RFK_RADIO_OFF));
1187 return status;
1190 /* Query FW and update rfkill sw state for all rfkill switches */
1191 static void tpacpi_rfk_update_swstate_all(void)
1193 unsigned int i;
1195 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1196 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1200 * Sync the HW-blocking state of all rfkill switches,
1201 * do notice it causes the rfkill core to schedule uevents
1203 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1205 unsigned int i;
1206 struct tpacpi_rfk *tp_rfk;
1208 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1209 tp_rfk = tpacpi_rfkill_switches[i];
1210 if (tp_rfk) {
1211 if (rfkill_set_hw_state(tp_rfk->rfkill,
1212 blocked)) {
1213 /* ignore -- we track sw block */
1219 /* Call to get the WLSW state from the firmware */
1220 static int hotkey_get_wlsw(void);
1222 /* Call to query WLSW state and update all rfkill switches */
1223 static bool tpacpi_rfk_check_hwblock_state(void)
1225 int res = hotkey_get_wlsw();
1226 int hw_blocked;
1228 /* When unknown or unsupported, we have to assume it is unblocked */
1229 if (res < 0)
1230 return false;
1232 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1233 tpacpi_rfk_update_hwblock_state(hw_blocked);
1235 return hw_blocked;
1238 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1240 struct tpacpi_rfk *tp_rfk = data;
1241 int res;
1243 dbg_printk(TPACPI_DBG_RFKILL,
1244 "request to change radio state to %s\n",
1245 blocked ? "blocked" : "unblocked");
1247 /* try to set radio state */
1248 res = (tp_rfk->ops->set_status)(blocked ?
1249 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1251 /* and update the rfkill core with whatever the FW really did */
1252 tpacpi_rfk_update_swstate(tp_rfk);
1254 return (res < 0) ? res : 0;
1257 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1258 .set_block = tpacpi_rfk_hook_set_block,
1261 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1262 const struct tpacpi_rfk_ops *tp_rfkops,
1263 const enum rfkill_type rfktype,
1264 const char *name,
1265 const bool set_default)
1267 struct tpacpi_rfk *atp_rfk;
1268 int res;
1269 bool sw_state = false;
1270 bool hw_state;
1271 int sw_status;
1273 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1275 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1276 if (atp_rfk)
1277 atp_rfk->rfkill = rfkill_alloc(name,
1278 &tpacpi_pdev->dev,
1279 rfktype,
1280 &tpacpi_rfk_rfkill_ops,
1281 atp_rfk);
1282 if (!atp_rfk || !atp_rfk->rfkill) {
1283 pr_err("failed to allocate memory for rfkill class\n");
1284 kfree(atp_rfk);
1285 return -ENOMEM;
1288 atp_rfk->id = id;
1289 atp_rfk->ops = tp_rfkops;
1291 sw_status = (tp_rfkops->get_status)();
1292 if (sw_status < 0) {
1293 pr_err("failed to read initial state for %s, error %d\n",
1294 name, sw_status);
1295 } else {
1296 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1297 if (set_default) {
1298 /* try to keep the initial state, since we ask the
1299 * firmware to preserve it across S5 in NVRAM */
1300 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1303 hw_state = tpacpi_rfk_check_hwblock_state();
1304 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1306 res = rfkill_register(atp_rfk->rfkill);
1307 if (res < 0) {
1308 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1309 rfkill_destroy(atp_rfk->rfkill);
1310 kfree(atp_rfk);
1311 return res;
1314 tpacpi_rfkill_switches[id] = atp_rfk;
1316 pr_info("rfkill switch %s: radio is %sblocked\n",
1317 name, (sw_state || hw_state) ? "" : "un");
1318 return 0;
1321 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1323 struct tpacpi_rfk *tp_rfk;
1325 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1327 tp_rfk = tpacpi_rfkill_switches[id];
1328 if (tp_rfk) {
1329 rfkill_unregister(tp_rfk->rfkill);
1330 rfkill_destroy(tp_rfk->rfkill);
1331 tpacpi_rfkill_switches[id] = NULL;
1332 kfree(tp_rfk);
1336 static void printk_deprecated_rfkill_attribute(const char * const what)
1338 printk_deprecated_attribute(what,
1339 "Please switch to generic rfkill before year 2010");
1342 /* sysfs <radio> enable ------------------------------------------------ */
1343 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1344 struct device_attribute *attr,
1345 char *buf)
1347 int status;
1349 printk_deprecated_rfkill_attribute(attr->attr.name);
1351 /* This is in the ABI... */
1352 if (tpacpi_rfk_check_hwblock_state()) {
1353 status = TPACPI_RFK_RADIO_OFF;
1354 } else {
1355 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1356 if (status < 0)
1357 return status;
1360 return snprintf(buf, PAGE_SIZE, "%d\n",
1361 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1364 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1365 struct device_attribute *attr,
1366 const char *buf, size_t count)
1368 unsigned long t;
1369 int res;
1371 printk_deprecated_rfkill_attribute(attr->attr.name);
1373 if (parse_strtoul(buf, 1, &t))
1374 return -EINVAL;
1376 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1378 /* This is in the ABI... */
1379 if (tpacpi_rfk_check_hwblock_state() && !!t)
1380 return -EPERM;
1382 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1383 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1384 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1386 return (res < 0) ? res : count;
1389 /* procfs -------------------------------------------------------------- */
1390 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1392 if (id >= TPACPI_RFK_SW_MAX)
1393 seq_printf(m, "status:\t\tnot supported\n");
1394 else {
1395 int status;
1397 /* This is in the ABI... */
1398 if (tpacpi_rfk_check_hwblock_state()) {
1399 status = TPACPI_RFK_RADIO_OFF;
1400 } else {
1401 status = tpacpi_rfk_update_swstate(
1402 tpacpi_rfkill_switches[id]);
1403 if (status < 0)
1404 return status;
1407 seq_printf(m, "status:\t\t%s\n",
1408 (status == TPACPI_RFK_RADIO_ON) ?
1409 "enabled" : "disabled");
1410 seq_printf(m, "commands:\tenable, disable\n");
1413 return 0;
1416 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1418 char *cmd;
1419 int status = -1;
1420 int res = 0;
1422 if (id >= TPACPI_RFK_SW_MAX)
1423 return -ENODEV;
1425 while ((cmd = next_cmd(&buf))) {
1426 if (strlencmp(cmd, "enable") == 0)
1427 status = TPACPI_RFK_RADIO_ON;
1428 else if (strlencmp(cmd, "disable") == 0)
1429 status = TPACPI_RFK_RADIO_OFF;
1430 else
1431 return -EINVAL;
1434 if (status != -1) {
1435 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1436 (status == TPACPI_RFK_RADIO_ON) ?
1437 "enable" : "disable",
1438 tpacpi_rfkill_names[id]);
1439 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1440 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1443 return res;
1446 /*************************************************************************
1447 * thinkpad-acpi driver attributes
1450 /* interface_version --------------------------------------------------- */
1451 static ssize_t interface_version_show(struct device_driver *drv, char *buf)
1453 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1455 static DRIVER_ATTR_RO(interface_version);
1457 /* debug_level --------------------------------------------------------- */
1458 static ssize_t debug_level_show(struct device_driver *drv, char *buf)
1460 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1463 static ssize_t debug_level_store(struct device_driver *drv, const char *buf,
1464 size_t count)
1466 unsigned long t;
1468 if (parse_strtoul(buf, 0xffff, &t))
1469 return -EINVAL;
1471 dbg_level = t;
1473 return count;
1475 static DRIVER_ATTR_RW(debug_level);
1477 /* version ------------------------------------------------------------- */
1478 static ssize_t version_show(struct device_driver *drv, char *buf)
1480 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1481 TPACPI_DESC, TPACPI_VERSION);
1483 static DRIVER_ATTR_RO(version);
1485 /* --------------------------------------------------------------------- */
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t wlsw_emulstate_show(struct device_driver *drv, char *buf)
1492 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1495 static ssize_t wlsw_emulstate_store(struct device_driver *drv, const char *buf,
1496 size_t count)
1498 unsigned long t;
1500 if (parse_strtoul(buf, 1, &t))
1501 return -EINVAL;
1503 if (tpacpi_wlsw_emulstate != !!t) {
1504 tpacpi_wlsw_emulstate = !!t;
1505 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1508 return count;
1510 static DRIVER_ATTR_RW(wlsw_emulstate);
1512 /* bluetooth_emulstate ------------------------------------------------- */
1513 static ssize_t bluetooth_emulstate_show(struct device_driver *drv, char *buf)
1515 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1518 static ssize_t bluetooth_emulstate_store(struct device_driver *drv,
1519 const char *buf, size_t count)
1521 unsigned long t;
1523 if (parse_strtoul(buf, 1, &t))
1524 return -EINVAL;
1526 tpacpi_bluetooth_emulstate = !!t;
1528 return count;
1530 static DRIVER_ATTR_RW(bluetooth_emulstate);
1532 /* wwan_emulstate ------------------------------------------------- */
1533 static ssize_t wwan_emulstate_show(struct device_driver *drv, char *buf)
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1538 static ssize_t wwan_emulstate_store(struct device_driver *drv, const char *buf,
1539 size_t count)
1541 unsigned long t;
1543 if (parse_strtoul(buf, 1, &t))
1544 return -EINVAL;
1546 tpacpi_wwan_emulstate = !!t;
1548 return count;
1550 static DRIVER_ATTR_RW(wwan_emulstate);
1552 /* uwb_emulstate ------------------------------------------------- */
1553 static ssize_t uwb_emulstate_show(struct device_driver *drv, char *buf)
1555 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1558 static ssize_t uwb_emulstate_store(struct device_driver *drv, const char *buf,
1559 size_t count)
1561 unsigned long t;
1563 if (parse_strtoul(buf, 1, &t))
1564 return -EINVAL;
1566 tpacpi_uwb_emulstate = !!t;
1568 return count;
1570 static DRIVER_ATTR_RW(uwb_emulstate);
1571 #endif
1573 /* --------------------------------------------------------------------- */
1575 static struct driver_attribute *tpacpi_driver_attributes[] = {
1576 &driver_attr_debug_level, &driver_attr_version,
1577 &driver_attr_interface_version,
1580 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1582 int i, res;
1584 i = 0;
1585 res = 0;
1586 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1587 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1588 i++;
1591 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1592 if (!res && dbg_wlswemul)
1593 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1594 if (!res && dbg_bluetoothemul)
1595 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1596 if (!res && dbg_wwanemul)
1597 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1598 if (!res && dbg_uwbemul)
1599 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1600 #endif
1602 return res;
1605 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1607 int i;
1609 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1610 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1612 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1613 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1614 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1615 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1616 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1617 #endif
1620 /*************************************************************************
1621 * Firmware Data
1625 * Table of recommended minimum BIOS versions
1627 * Reasons for listing:
1628 * 1. Stable BIOS, listed because the unknown amount of
1629 * bugs and bad ACPI behaviour on older versions
1631 * 2. BIOS or EC fw with known bugs that trigger on Linux
1633 * 3. BIOS with known reduced functionality in older versions
1635 * We recommend the latest BIOS and EC version.
1636 * We only support the latest BIOS and EC fw version as a rule.
1638 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1639 * Information from users in ThinkWiki
1641 * WARNING: we use this table also to detect that the machine is
1642 * a ThinkPad in some cases, so don't remove entries lightly.
1645 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1646 { .vendor = (__v), \
1647 .bios = TPID(__id1, __id2), \
1648 .ec = TPACPI_MATCH_ANY, \
1649 .quirks = TPACPI_MATCH_ANY_VERSION << 16 \
1650 | TPVER(__bv1, __bv2) }
1652 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1653 __eid, __ev1, __ev2) \
1654 { .vendor = (__v), \
1655 .bios = TPID(__bid1, __bid2), \
1656 .ec = __eid, \
1657 .quirks = TPVER(__ev1, __ev2) << 16 \
1658 | TPVER(__bv1, __bv2) }
1660 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1661 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1663 /* Outdated IBM BIOSes often lack the EC id string */
1664 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1665 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1666 __bv1, __bv2, TPID(__id1, __id2), \
1667 __ev1, __ev2), \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1669 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1670 __ev1, __ev2)
1672 /* Outdated IBM BIOSes often lack the EC id string */
1673 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1674 __eid1, __eid2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1676 __bv1, __bv2, TPID(__eid1, __eid2), \
1677 __ev1, __ev2), \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1679 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1680 __ev1, __ev2)
1682 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1685 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1687 __bv1, __bv2, TPID(__id1, __id2), \
1688 __ev1, __ev2)
1690 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1691 __eid1, __eid2, __ev1, __ev2) \
1692 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1693 __bv1, __bv2, TPID(__eid1, __eid2), \
1694 __ev1, __ev2)
1696 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1697 /* Numeric models ------------------ */
1698 /* FW MODEL BIOS VERS */
1699 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1700 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1701 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1702 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1703 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1704 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1705 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1706 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1707 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1709 /* A-series ------------------------- */
1710 /* FW MODEL BIOS VERS EC VERS */
1711 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1712 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1713 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1714 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1715 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1716 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1717 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1718 TPV_QI0('1', '3', '2', '0'), /* A22m */
1719 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1720 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1721 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1723 /* G-series ------------------------- */
1724 /* FW MODEL BIOS VERS */
1725 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1726 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1728 /* R-series, T-series --------------- */
1729 /* FW MODEL BIOS VERS EC VERS */
1730 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1731 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1732 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1733 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1734 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1735 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1736 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1737 T40/p, T41/p, T42/p (1) */
1738 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1739 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1740 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1741 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1743 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1744 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1745 TPV_QI0('1', '6', '3', '2'), /* T22 */
1746 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1747 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1748 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1750 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1751 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1752 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1754 /* BIOS FW BIOS VERS EC FW EC VERS */
1755 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1756 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1758 /* X-series ------------------------- */
1759 /* FW MODEL BIOS VERS EC VERS */
1760 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1761 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1762 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1763 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1764 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1765 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1766 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1768 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1769 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1771 /* (0) - older versions lack DMI EC fw string and functionality */
1772 /* (1) - older versions known to lack functionality */
1775 #undef TPV_QL1
1776 #undef TPV_QL0
1777 #undef TPV_QI2
1778 #undef TPV_QI1
1779 #undef TPV_QI0
1780 #undef TPV_Q_X
1781 #undef TPV_Q
1783 static void __init tpacpi_check_outdated_fw(void)
1785 unsigned long fwvers;
1786 u16 ec_version, bios_version;
1788 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1789 ARRAY_SIZE(tpacpi_bios_version_qtable));
1791 if (!fwvers)
1792 return;
1794 bios_version = fwvers & 0xffffU;
1795 ec_version = (fwvers >> 16) & 0xffffU;
1797 /* note that unknown versions are set to 0x0000 and we use that */
1798 if ((bios_version > thinkpad_id.bios_release) ||
1799 (ec_version > thinkpad_id.ec_release &&
1800 ec_version != TPACPI_MATCH_ANY_VERSION)) {
1802 * The changelogs would let us track down the exact
1803 * reason, but it is just too much of a pain to track
1804 * it. We only list BIOSes that are either really
1805 * broken, or really stable to begin with, so it is
1806 * best if the user upgrades the firmware anyway.
1808 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1809 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1813 static bool __init tpacpi_is_fw_known(void)
1815 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1816 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1819 /****************************************************************************
1820 ****************************************************************************
1822 * Subdrivers
1824 ****************************************************************************
1825 ****************************************************************************/
1827 /*************************************************************************
1828 * thinkpad-acpi metadata subdriver
1831 static int thinkpad_acpi_driver_read(struct seq_file *m)
1833 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1834 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1835 return 0;
1838 static struct ibm_struct thinkpad_acpi_driver_data = {
1839 .name = "driver",
1840 .read = thinkpad_acpi_driver_read,
1843 /*************************************************************************
1844 * Hotkey subdriver
1848 * ThinkPad firmware event model
1850 * The ThinkPad firmware has two main event interfaces: normal ACPI
1851 * notifications (which follow the ACPI standard), and a private event
1852 * interface.
1854 * The private event interface also issues events for the hotkeys. As
1855 * the driver gained features, the event handling code ended up being
1856 * built around the hotkey subdriver. This will need to be refactored
1857 * to a more formal event API eventually.
1859 * Some "hotkeys" are actually supposed to be used as event reports,
1860 * such as "brightness has changed", "volume has changed", depending on
1861 * the ThinkPad model and how the firmware is operating.
1863 * Unlike other classes, hotkey-class events have mask/unmask control on
1864 * non-ancient firmware. However, how it behaves changes a lot with the
1865 * firmware model and version.
1868 enum { /* hot key scan codes (derived from ACPI DSDT) */
1869 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1870 TP_ACPI_HOTKEYSCAN_FNF2,
1871 TP_ACPI_HOTKEYSCAN_FNF3,
1872 TP_ACPI_HOTKEYSCAN_FNF4,
1873 TP_ACPI_HOTKEYSCAN_FNF5,
1874 TP_ACPI_HOTKEYSCAN_FNF6,
1875 TP_ACPI_HOTKEYSCAN_FNF7,
1876 TP_ACPI_HOTKEYSCAN_FNF8,
1877 TP_ACPI_HOTKEYSCAN_FNF9,
1878 TP_ACPI_HOTKEYSCAN_FNF10,
1879 TP_ACPI_HOTKEYSCAN_FNF11,
1880 TP_ACPI_HOTKEYSCAN_FNF12,
1881 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1882 TP_ACPI_HOTKEYSCAN_FNINSERT,
1883 TP_ACPI_HOTKEYSCAN_FNDELETE,
1884 TP_ACPI_HOTKEYSCAN_FNHOME,
1885 TP_ACPI_HOTKEYSCAN_FNEND,
1886 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1887 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1888 TP_ACPI_HOTKEYSCAN_FNSPACE,
1889 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1890 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1891 TP_ACPI_HOTKEYSCAN_MUTE,
1892 TP_ACPI_HOTKEYSCAN_THINKPAD,
1893 TP_ACPI_HOTKEYSCAN_UNK1,
1894 TP_ACPI_HOTKEYSCAN_UNK2,
1895 TP_ACPI_HOTKEYSCAN_UNK3,
1896 TP_ACPI_HOTKEYSCAN_UNK4,
1897 TP_ACPI_HOTKEYSCAN_UNK5,
1898 TP_ACPI_HOTKEYSCAN_UNK6,
1899 TP_ACPI_HOTKEYSCAN_UNK7,
1900 TP_ACPI_HOTKEYSCAN_UNK8,
1902 /* Adaptive keyboard keycodes */
1903 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1904 TP_ACPI_HOTKEYSCAN_MUTE2 = TP_ACPI_HOTKEYSCAN_ADAPTIVE_START,
1905 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1906 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1907 TP_ACPI_HOTKEYSCAN_CLOUD,
1908 TP_ACPI_HOTKEYSCAN_UNK9,
1909 TP_ACPI_HOTKEYSCAN_VOICE,
1910 TP_ACPI_HOTKEYSCAN_UNK10,
1911 TP_ACPI_HOTKEYSCAN_GESTURES,
1912 TP_ACPI_HOTKEYSCAN_UNK11,
1913 TP_ACPI_HOTKEYSCAN_UNK12,
1914 TP_ACPI_HOTKEYSCAN_UNK13,
1915 TP_ACPI_HOTKEYSCAN_CONFIG,
1916 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1917 TP_ACPI_HOTKEYSCAN_RELOAD,
1918 TP_ACPI_HOTKEYSCAN_BACK,
1919 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1920 TP_ACPI_HOTKEYSCAN_MIC_UP,
1921 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1922 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1923 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1925 /* Lenovo extended keymap, starting at 0x1300 */
1926 TP_ACPI_HOTKEYSCAN_EXTENDED_START,
1927 /* first new observed key (star, favorites) is 0x1311 */
1928 TP_ACPI_HOTKEYSCAN_STAR = 69,
1929 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2,
1930 TP_ACPI_HOTKEYSCAN_CALCULATOR,
1931 TP_ACPI_HOTKEYSCAN_BLUETOOTH,
1932 TP_ACPI_HOTKEYSCAN_KEYBOARD,
1934 /* Hotkey keymap size */
1935 TPACPI_HOTKEY_MAP_LEN
1938 enum { /* Keys/events available through NVRAM polling */
1939 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1940 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1943 enum { /* Positions of some of the keys in hotkey masks */
1944 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1945 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1946 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1947 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1948 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1949 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1950 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1951 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1952 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1953 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1954 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1957 enum { /* NVRAM to ACPI HKEY group map */
1958 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1959 TP_ACPI_HKEY_ZOOM_MASK |
1960 TP_ACPI_HKEY_DISPSWTCH_MASK |
1961 TP_ACPI_HKEY_HIBERNATE_MASK,
1962 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1963 TP_ACPI_HKEY_BRGHTDWN_MASK,
1964 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1965 TP_ACPI_HKEY_VOLDWN_MASK |
1966 TP_ACPI_HKEY_MUTE_MASK,
1969 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1970 struct tp_nvram_state {
1971 u16 thinkpad_toggle:1;
1972 u16 zoom_toggle:1;
1973 u16 display_toggle:1;
1974 u16 thinklight_toggle:1;
1975 u16 hibernate_toggle:1;
1976 u16 displayexp_toggle:1;
1977 u16 display_state:1;
1978 u16 brightness_toggle:1;
1979 u16 volume_toggle:1;
1980 u16 mute:1;
1982 u8 brightness_level;
1983 u8 volume_level;
1986 /* kthread for the hotkey poller */
1987 static struct task_struct *tpacpi_hotkey_task;
1990 * Acquire mutex to write poller control variables as an
1991 * atomic block.
1993 * Increment hotkey_config_change when changing them if you
1994 * want the kthread to forget old state.
1996 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1998 static struct mutex hotkey_thread_data_mutex;
1999 static unsigned int hotkey_config_change;
2002 * hotkey poller control variables
2004 * Must be atomic or readers will also need to acquire mutex
2006 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2007 * should be used only when the changes need to be taken as
2008 * a block, OR when one needs to force the kthread to forget
2009 * old state.
2011 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2012 static unsigned int hotkey_poll_freq = 10; /* Hz */
2014 #define HOTKEY_CONFIG_CRITICAL_START \
2015 do { \
2016 mutex_lock(&hotkey_thread_data_mutex); \
2017 hotkey_config_change++; \
2018 } while (0);
2019 #define HOTKEY_CONFIG_CRITICAL_END \
2020 mutex_unlock(&hotkey_thread_data_mutex);
2022 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2024 #define hotkey_source_mask 0U
2025 #define HOTKEY_CONFIG_CRITICAL_START
2026 #define HOTKEY_CONFIG_CRITICAL_END
2028 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2030 static struct mutex hotkey_mutex;
2032 static enum { /* Reasons for waking up */
2033 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2034 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2035 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2036 } hotkey_wakeup_reason;
2038 static int hotkey_autosleep_ack;
2040 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2041 static u32 hotkey_all_mask; /* all events supported in fw */
2042 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2043 static u32 hotkey_reserved_mask; /* events better left disabled */
2044 static u32 hotkey_driver_mask; /* events needed by the driver */
2045 static u32 hotkey_user_mask; /* events visible to userspace */
2046 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2048 static u16 *hotkey_keycode_map;
2050 static struct attribute_set *hotkey_dev_attributes;
2052 static void tpacpi_driver_event(const unsigned int hkey_event);
2053 static void hotkey_driver_event(const unsigned int scancode);
2054 static void hotkey_poll_setup(const bool may_warn);
2056 /* HKEY.MHKG() return bits */
2057 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2058 enum {
2059 TP_ACPI_MULTI_MODE_INVALID = 0,
2060 TP_ACPI_MULTI_MODE_UNKNOWN = 1 << 0,
2061 TP_ACPI_MULTI_MODE_LAPTOP = 1 << 1,
2062 TP_ACPI_MULTI_MODE_TABLET = 1 << 2,
2063 TP_ACPI_MULTI_MODE_FLAT = 1 << 3,
2064 TP_ACPI_MULTI_MODE_STAND = 1 << 4,
2065 TP_ACPI_MULTI_MODE_TENT = 1 << 5,
2066 TP_ACPI_MULTI_MODE_STAND_TENT = 1 << 6,
2069 enum {
2070 /* The following modes are considered tablet mode for the purpose of
2071 * reporting the status to userspace. i.e. in all these modes it makes
2072 * sense to disable the laptop input devices such as touchpad and
2073 * keyboard.
2075 TP_ACPI_MULTI_MODE_TABLET_LIKE = TP_ACPI_MULTI_MODE_TABLET |
2076 TP_ACPI_MULTI_MODE_STAND |
2077 TP_ACPI_MULTI_MODE_TENT |
2078 TP_ACPI_MULTI_MODE_STAND_TENT,
2081 static int hotkey_get_wlsw(void)
2083 int status;
2085 if (!tp_features.hotkey_wlsw)
2086 return -ENODEV;
2088 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2089 if (dbg_wlswemul)
2090 return (tpacpi_wlsw_emulstate) ?
2091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2092 #endif
2094 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2095 return -EIO;
2097 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2100 static int hotkey_gmms_get_tablet_mode(int s, int *has_tablet_mode)
2102 int type = (s >> 16) & 0xffff;
2103 int value = s & 0xffff;
2104 int mode = TP_ACPI_MULTI_MODE_INVALID;
2105 int valid_modes = 0;
2107 if (has_tablet_mode)
2108 *has_tablet_mode = 0;
2110 switch (type) {
2111 case 1:
2112 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2113 TP_ACPI_MULTI_MODE_TABLET |
2114 TP_ACPI_MULTI_MODE_STAND_TENT;
2115 break;
2116 case 2:
2117 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2118 TP_ACPI_MULTI_MODE_FLAT |
2119 TP_ACPI_MULTI_MODE_TABLET |
2120 TP_ACPI_MULTI_MODE_STAND |
2121 TP_ACPI_MULTI_MODE_TENT;
2122 break;
2123 case 3:
2124 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2125 TP_ACPI_MULTI_MODE_FLAT;
2126 break;
2127 case 4:
2128 case 5:
2129 /* In mode 4, FLAT is not specified as a valid mode. However,
2130 * it can be seen at least on the X1 Yoga 2nd Generation.
2132 valid_modes = TP_ACPI_MULTI_MODE_LAPTOP |
2133 TP_ACPI_MULTI_MODE_FLAT |
2134 TP_ACPI_MULTI_MODE_TABLET |
2135 TP_ACPI_MULTI_MODE_STAND |
2136 TP_ACPI_MULTI_MODE_TENT;
2137 break;
2138 default:
2139 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2140 type, value, TPACPI_MAIL);
2141 return 0;
2144 if (has_tablet_mode && (valid_modes & TP_ACPI_MULTI_MODE_TABLET_LIKE))
2145 *has_tablet_mode = 1;
2147 switch (value) {
2148 case 1:
2149 mode = TP_ACPI_MULTI_MODE_LAPTOP;
2150 break;
2151 case 2:
2152 mode = TP_ACPI_MULTI_MODE_FLAT;
2153 break;
2154 case 3:
2155 mode = TP_ACPI_MULTI_MODE_TABLET;
2156 break;
2157 case 4:
2158 if (type == 1)
2159 mode = TP_ACPI_MULTI_MODE_STAND_TENT;
2160 else
2161 mode = TP_ACPI_MULTI_MODE_STAND;
2162 break;
2163 case 5:
2164 mode = TP_ACPI_MULTI_MODE_TENT;
2165 break;
2166 default:
2167 if (type == 5 && value == 0xffff) {
2168 pr_warn("Multi mode status is undetected, assuming laptop\n");
2169 return 0;
2173 if (!(mode & valid_modes)) {
2174 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2175 value, type, TPACPI_MAIL);
2176 return 0;
2179 return !!(mode & TP_ACPI_MULTI_MODE_TABLET_LIKE);
2182 static int hotkey_get_tablet_mode(int *status)
2184 int s;
2186 switch (tp_features.hotkey_tablet) {
2187 case TP_HOTKEY_TABLET_USES_MHKG:
2188 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2189 return -EIO;
2191 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2192 break;
2193 case TP_HOTKEY_TABLET_USES_GMMS:
2194 if (!acpi_evalf(hkey_handle, &s, "GMMS", "dd", 0))
2195 return -EIO;
2197 *status = hotkey_gmms_get_tablet_mode(s, NULL);
2198 break;
2199 default:
2200 break;
2203 return 0;
2207 * Reads current event mask from firmware, and updates
2208 * hotkey_acpi_mask accordingly. Also resets any bits
2209 * from hotkey_user_mask that are unavailable to be
2210 * delivered (shadow requirement of the userspace ABI).
2212 * Call with hotkey_mutex held
2214 static int hotkey_mask_get(void)
2216 if (tp_features.hotkey_mask) {
2217 u32 m = 0;
2219 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2220 return -EIO;
2222 hotkey_acpi_mask = m;
2223 } else {
2224 /* no mask support doesn't mean no event support... */
2225 hotkey_acpi_mask = hotkey_all_mask;
2228 /* sync userspace-visible mask */
2229 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2231 return 0;
2234 static void hotkey_mask_warn_incomplete_mask(void)
2236 /* log only what the user can fix... */
2237 const u32 wantedmask = hotkey_driver_mask &
2238 ~(hotkey_acpi_mask | hotkey_source_mask) &
2239 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2241 if (wantedmask)
2242 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2246 * Set the firmware mask when supported
2248 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2250 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2252 * Call with hotkey_mutex held
2254 static int hotkey_mask_set(u32 mask)
2256 int i;
2257 int rc = 0;
2259 const u32 fwmask = mask & ~hotkey_source_mask;
2261 if (tp_features.hotkey_mask) {
2262 for (i = 0; i < 32; i++) {
2263 if (!acpi_evalf(hkey_handle,
2264 NULL, "MHKM", "vdd", i + 1,
2265 !!(mask & (1 << i)))) {
2266 rc = -EIO;
2267 break;
2273 * We *must* make an inconditional call to hotkey_mask_get to
2274 * refresh hotkey_acpi_mask and update hotkey_user_mask
2276 * Take the opportunity to also log when we cannot _enable_
2277 * a given event.
2279 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2280 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2281 fwmask, hotkey_acpi_mask);
2284 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2285 hotkey_mask_warn_incomplete_mask();
2287 return rc;
2291 * Sets hotkey_user_mask and tries to set the firmware mask
2293 * Call with hotkey_mutex held
2295 static int hotkey_user_mask_set(const u32 mask)
2297 int rc;
2299 /* Give people a chance to notice they are doing something that
2300 * is bound to go boom on their users sooner or later */
2301 if (!tp_warned.hotkey_mask_ff &&
2302 (mask == 0xffff || mask == 0xffffff ||
2303 mask == 0xffffffff)) {
2304 tp_warned.hotkey_mask_ff = 1;
2305 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2306 mask);
2307 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2310 /* Try to enable what the user asked for, plus whatever we need.
2311 * this syncs everything but won't enable bits in hotkey_user_mask */
2312 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2314 /* Enable the available bits in hotkey_user_mask */
2315 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2317 return rc;
2321 * Sets the driver hotkey mask.
2323 * Can be called even if the hotkey subdriver is inactive
2325 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2327 int rc;
2329 /* Do the right thing if hotkey_init has not been called yet */
2330 if (!tp_features.hotkey) {
2331 hotkey_driver_mask = mask;
2332 return 0;
2335 mutex_lock(&hotkey_mutex);
2337 HOTKEY_CONFIG_CRITICAL_START
2338 hotkey_driver_mask = mask;
2339 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2340 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2341 #endif
2342 HOTKEY_CONFIG_CRITICAL_END
2344 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2345 ~hotkey_source_mask);
2346 hotkey_poll_setup(true);
2348 mutex_unlock(&hotkey_mutex);
2350 return rc;
2353 static int hotkey_status_get(int *status)
2355 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2356 return -EIO;
2358 return 0;
2361 static int hotkey_status_set(bool enable)
2363 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2364 return -EIO;
2366 return 0;
2369 static void tpacpi_input_send_tabletsw(void)
2371 int state;
2373 if (tp_features.hotkey_tablet &&
2374 !hotkey_get_tablet_mode(&state)) {
2375 mutex_lock(&tpacpi_inputdev_send_mutex);
2377 input_report_switch(tpacpi_inputdev,
2378 SW_TABLET_MODE, !!state);
2379 input_sync(tpacpi_inputdev);
2381 mutex_unlock(&tpacpi_inputdev_send_mutex);
2385 /* Do NOT call without validating scancode first */
2386 static void tpacpi_input_send_key(const unsigned int scancode)
2388 const unsigned int keycode = hotkey_keycode_map[scancode];
2390 if (keycode != KEY_RESERVED) {
2391 mutex_lock(&tpacpi_inputdev_send_mutex);
2393 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2394 input_report_key(tpacpi_inputdev, keycode, 1);
2395 input_sync(tpacpi_inputdev);
2397 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2398 input_report_key(tpacpi_inputdev, keycode, 0);
2399 input_sync(tpacpi_inputdev);
2401 mutex_unlock(&tpacpi_inputdev_send_mutex);
2405 /* Do NOT call without validating scancode first */
2406 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2408 hotkey_driver_event(scancode);
2409 if (hotkey_user_mask & (1 << scancode))
2410 tpacpi_input_send_key(scancode);
2413 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2414 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2416 /* Do NOT call without validating scancode first */
2417 static void tpacpi_hotkey_send_key(unsigned int scancode)
2419 tpacpi_input_send_key_masked(scancode);
2422 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2424 u8 d;
2426 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2427 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2428 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2429 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2430 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2431 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2433 if (m & TP_ACPI_HKEY_KBD_LIGHT_MASK) {
2434 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2435 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2437 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2438 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2439 n->displayexp_toggle =
2440 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2442 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2443 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2444 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2445 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2446 n->brightness_toggle =
2447 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2449 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2450 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2451 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2452 >> TP_NVRAM_POS_LEVEL_VOLUME;
2453 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2454 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2458 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2459 do { \
2460 if ((event_mask & (1 << __scancode)) && \
2461 oldn->__member != newn->__member) \
2462 tpacpi_hotkey_send_key(__scancode); \
2463 } while (0)
2465 #define TPACPI_MAY_SEND_KEY(__scancode) \
2466 do { \
2467 if (event_mask & (1 << __scancode)) \
2468 tpacpi_hotkey_send_key(__scancode); \
2469 } while (0)
2471 static void issue_volchange(const unsigned int oldvol,
2472 const unsigned int newvol,
2473 const u32 event_mask)
2475 unsigned int i = oldvol;
2477 while (i > newvol) {
2478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2479 i--;
2481 while (i < newvol) {
2482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2483 i++;
2487 static void issue_brightnesschange(const unsigned int oldbrt,
2488 const unsigned int newbrt,
2489 const u32 event_mask)
2491 unsigned int i = oldbrt;
2493 while (i > newbrt) {
2494 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2495 i--;
2497 while (i < newbrt) {
2498 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2499 i++;
2503 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2504 struct tp_nvram_state *newn,
2505 const u32 event_mask)
2508 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2509 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2510 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2511 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2513 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2515 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2518 * Handle volume
2520 * This code is supposed to duplicate the IBM firmware behaviour:
2521 * - Pressing MUTE issues mute hotkey message, even when already mute
2522 * - Pressing Volume up/down issues volume up/down hotkey messages,
2523 * even when already at maximum or minimum volume
2524 * - The act of unmuting issues volume up/down notification,
2525 * depending which key was used to unmute
2527 * We are constrained to what the NVRAM can tell us, which is not much
2528 * and certainly not enough if more than one volume hotkey was pressed
2529 * since the last poll cycle.
2531 * Just to make our life interesting, some newer Lenovo ThinkPads have
2532 * bugs in the BIOS and may fail to update volume_toggle properly.
2534 if (newn->mute) {
2535 /* muted */
2536 if (!oldn->mute ||
2537 oldn->volume_toggle != newn->volume_toggle ||
2538 oldn->volume_level != newn->volume_level) {
2539 /* recently muted, or repeated mute keypress, or
2540 * multiple presses ending in mute */
2541 issue_volchange(oldn->volume_level, newn->volume_level,
2542 event_mask);
2543 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2545 } else {
2546 /* unmute */
2547 if (oldn->mute) {
2548 /* recently unmuted, issue 'unmute' keypress */
2549 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2551 if (oldn->volume_level != newn->volume_level) {
2552 issue_volchange(oldn->volume_level, newn->volume_level,
2553 event_mask);
2554 } else if (oldn->volume_toggle != newn->volume_toggle) {
2555 /* repeated vol up/down keypress at end of scale ? */
2556 if (newn->volume_level == 0)
2557 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2558 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2559 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2563 /* handle brightness */
2564 if (oldn->brightness_level != newn->brightness_level) {
2565 issue_brightnesschange(oldn->brightness_level,
2566 newn->brightness_level, event_mask);
2567 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2568 /* repeated key presses that didn't change state */
2569 if (newn->brightness_level == 0)
2570 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2571 else if (newn->brightness_level >= bright_maxlvl
2572 && !tp_features.bright_unkfw)
2573 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2576 #undef TPACPI_COMPARE_KEY
2577 #undef TPACPI_MAY_SEND_KEY
2581 * Polling driver
2583 * We track all events in hotkey_source_mask all the time, since
2584 * most of them are edge-based. We only issue those requested by
2585 * hotkey_user_mask or hotkey_driver_mask, though.
2587 static int hotkey_kthread(void *data)
2589 struct tp_nvram_state s[2];
2590 u32 poll_mask, event_mask;
2591 unsigned int si, so;
2592 unsigned long t;
2593 unsigned int change_detector;
2594 unsigned int poll_freq;
2595 bool was_frozen;
2597 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2598 goto exit;
2600 set_freezable();
2602 so = 0;
2603 si = 1;
2604 t = 0;
2606 /* Initial state for compares */
2607 mutex_lock(&hotkey_thread_data_mutex);
2608 change_detector = hotkey_config_change;
2609 poll_mask = hotkey_source_mask;
2610 event_mask = hotkey_source_mask &
2611 (hotkey_driver_mask | hotkey_user_mask);
2612 poll_freq = hotkey_poll_freq;
2613 mutex_unlock(&hotkey_thread_data_mutex);
2614 hotkey_read_nvram(&s[so], poll_mask);
2616 while (!kthread_should_stop()) {
2617 if (t == 0) {
2618 if (likely(poll_freq))
2619 t = 1000/poll_freq;
2620 else
2621 t = 100; /* should never happen... */
2623 t = msleep_interruptible(t);
2624 if (unlikely(kthread_freezable_should_stop(&was_frozen)))
2625 break;
2627 if (t > 0 && !was_frozen)
2628 continue;
2630 mutex_lock(&hotkey_thread_data_mutex);
2631 if (was_frozen || hotkey_config_change != change_detector) {
2632 /* forget old state on thaw or config change */
2633 si = so;
2634 t = 0;
2635 change_detector = hotkey_config_change;
2637 poll_mask = hotkey_source_mask;
2638 event_mask = hotkey_source_mask &
2639 (hotkey_driver_mask | hotkey_user_mask);
2640 poll_freq = hotkey_poll_freq;
2641 mutex_unlock(&hotkey_thread_data_mutex);
2643 if (likely(poll_mask)) {
2644 hotkey_read_nvram(&s[si], poll_mask);
2645 if (likely(si != so)) {
2646 hotkey_compare_and_issue_event(&s[so], &s[si],
2647 event_mask);
2651 so = si;
2652 si ^= 1;
2655 exit:
2656 return 0;
2659 /* call with hotkey_mutex held */
2660 static void hotkey_poll_stop_sync(void)
2662 if (tpacpi_hotkey_task) {
2663 kthread_stop(tpacpi_hotkey_task);
2664 tpacpi_hotkey_task = NULL;
2668 /* call with hotkey_mutex held */
2669 static void hotkey_poll_setup(const bool may_warn)
2671 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2672 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2674 if (hotkey_poll_freq > 0 &&
2675 (poll_driver_mask ||
2676 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2677 if (!tpacpi_hotkey_task) {
2678 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2679 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2680 if (IS_ERR(tpacpi_hotkey_task)) {
2681 tpacpi_hotkey_task = NULL;
2682 pr_err("could not create kernel thread for hotkey polling\n");
2685 } else {
2686 hotkey_poll_stop_sync();
2687 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2688 hotkey_poll_freq == 0) {
2689 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2690 poll_user_mask, poll_driver_mask);
2695 static void hotkey_poll_setup_safe(const bool may_warn)
2697 mutex_lock(&hotkey_mutex);
2698 hotkey_poll_setup(may_warn);
2699 mutex_unlock(&hotkey_mutex);
2702 /* call with hotkey_mutex held */
2703 static void hotkey_poll_set_freq(unsigned int freq)
2705 if (!freq)
2706 hotkey_poll_stop_sync();
2708 hotkey_poll_freq = freq;
2711 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2713 static void hotkey_poll_setup(const bool __unused)
2717 static void hotkey_poll_setup_safe(const bool __unused)
2721 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2723 static int hotkey_inputdev_open(struct input_dev *dev)
2725 switch (tpacpi_lifecycle) {
2726 case TPACPI_LIFE_INIT:
2727 case TPACPI_LIFE_RUNNING:
2728 hotkey_poll_setup_safe(false);
2729 return 0;
2730 case TPACPI_LIFE_EXITING:
2731 return -EBUSY;
2734 /* Should only happen if tpacpi_lifecycle is corrupt */
2735 BUG();
2736 return -EBUSY;
2739 static void hotkey_inputdev_close(struct input_dev *dev)
2741 /* disable hotkey polling when possible */
2742 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2743 !(hotkey_source_mask & hotkey_driver_mask))
2744 hotkey_poll_setup_safe(false);
2747 /* sysfs hotkey enable ------------------------------------------------- */
2748 static ssize_t hotkey_enable_show(struct device *dev,
2749 struct device_attribute *attr,
2750 char *buf)
2752 int res, status;
2754 printk_deprecated_attribute("hotkey_enable",
2755 "Hotkey reporting is always enabled");
2757 res = hotkey_status_get(&status);
2758 if (res)
2759 return res;
2761 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2764 static ssize_t hotkey_enable_store(struct device *dev,
2765 struct device_attribute *attr,
2766 const char *buf, size_t count)
2768 unsigned long t;
2770 printk_deprecated_attribute("hotkey_enable",
2771 "Hotkeys can be disabled through hotkey_mask");
2773 if (parse_strtoul(buf, 1, &t))
2774 return -EINVAL;
2776 if (t == 0)
2777 return -EPERM;
2779 return count;
2782 static DEVICE_ATTR_RW(hotkey_enable);
2784 /* sysfs hotkey mask --------------------------------------------------- */
2785 static ssize_t hotkey_mask_show(struct device *dev,
2786 struct device_attribute *attr,
2787 char *buf)
2789 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2792 static ssize_t hotkey_mask_store(struct device *dev,
2793 struct device_attribute *attr,
2794 const char *buf, size_t count)
2796 unsigned long t;
2797 int res;
2799 if (parse_strtoul(buf, 0xffffffffUL, &t))
2800 return -EINVAL;
2802 if (mutex_lock_killable(&hotkey_mutex))
2803 return -ERESTARTSYS;
2805 res = hotkey_user_mask_set(t);
2807 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2808 hotkey_poll_setup(true);
2809 #endif
2811 mutex_unlock(&hotkey_mutex);
2813 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2815 return (res) ? res : count;
2818 static DEVICE_ATTR_RW(hotkey_mask);
2820 /* sysfs hotkey bios_enabled ------------------------------------------- */
2821 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2822 struct device_attribute *attr,
2823 char *buf)
2825 return sprintf(buf, "0\n");
2828 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2830 /* sysfs hotkey bios_mask ---------------------------------------------- */
2831 static ssize_t hotkey_bios_mask_show(struct device *dev,
2832 struct device_attribute *attr,
2833 char *buf)
2835 printk_deprecated_attribute("hotkey_bios_mask",
2836 "This attribute is useless.");
2837 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2840 static DEVICE_ATTR_RO(hotkey_bios_mask);
2842 /* sysfs hotkey all_mask ----------------------------------------------- */
2843 static ssize_t hotkey_all_mask_show(struct device *dev,
2844 struct device_attribute *attr,
2845 char *buf)
2847 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2848 hotkey_all_mask | hotkey_source_mask);
2851 static DEVICE_ATTR_RO(hotkey_all_mask);
2853 /* sysfs hotkey all_mask ----------------------------------------------- */
2854 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2855 struct device_attribute *attr,
2856 char *buf)
2858 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2859 hotkey_adaptive_all_mask | hotkey_source_mask);
2862 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2864 /* sysfs hotkey recommended_mask --------------------------------------- */
2865 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2866 struct device_attribute *attr,
2867 char *buf)
2869 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2870 (hotkey_all_mask | hotkey_source_mask)
2871 & ~hotkey_reserved_mask);
2874 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2876 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2878 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2879 static ssize_t hotkey_source_mask_show(struct device *dev,
2880 struct device_attribute *attr,
2881 char *buf)
2883 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2886 static ssize_t hotkey_source_mask_store(struct device *dev,
2887 struct device_attribute *attr,
2888 const char *buf, size_t count)
2890 unsigned long t;
2891 u32 r_ev;
2892 int rc;
2894 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2895 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2896 return -EINVAL;
2898 if (mutex_lock_killable(&hotkey_mutex))
2899 return -ERESTARTSYS;
2901 HOTKEY_CONFIG_CRITICAL_START
2902 hotkey_source_mask = t;
2903 HOTKEY_CONFIG_CRITICAL_END
2905 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2906 ~hotkey_source_mask);
2907 hotkey_poll_setup(true);
2909 /* check if events needed by the driver got disabled */
2910 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2911 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2913 mutex_unlock(&hotkey_mutex);
2915 if (rc < 0)
2916 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2918 if (r_ev)
2919 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2920 r_ev);
2922 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2924 return (rc < 0) ? rc : count;
2927 static DEVICE_ATTR_RW(hotkey_source_mask);
2929 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2930 static ssize_t hotkey_poll_freq_show(struct device *dev,
2931 struct device_attribute *attr,
2932 char *buf)
2934 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2937 static ssize_t hotkey_poll_freq_store(struct device *dev,
2938 struct device_attribute *attr,
2939 const char *buf, size_t count)
2941 unsigned long t;
2943 if (parse_strtoul(buf, 25, &t))
2944 return -EINVAL;
2946 if (mutex_lock_killable(&hotkey_mutex))
2947 return -ERESTARTSYS;
2949 hotkey_poll_set_freq(t);
2950 hotkey_poll_setup(true);
2952 mutex_unlock(&hotkey_mutex);
2954 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2956 return count;
2959 static DEVICE_ATTR_RW(hotkey_poll_freq);
2961 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2963 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2964 static ssize_t hotkey_radio_sw_show(struct device *dev,
2965 struct device_attribute *attr,
2966 char *buf)
2968 int res;
2969 res = hotkey_get_wlsw();
2970 if (res < 0)
2971 return res;
2973 /* Opportunistic update */
2974 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2976 return snprintf(buf, PAGE_SIZE, "%d\n",
2977 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2980 static DEVICE_ATTR_RO(hotkey_radio_sw);
2982 static void hotkey_radio_sw_notify_change(void)
2984 if (tp_features.hotkey_wlsw)
2985 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2986 "hotkey_radio_sw");
2989 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2990 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2991 struct device_attribute *attr,
2992 char *buf)
2994 int res, s;
2995 res = hotkey_get_tablet_mode(&s);
2996 if (res < 0)
2997 return res;
2999 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
3002 static DEVICE_ATTR_RO(hotkey_tablet_mode);
3004 static void hotkey_tablet_mode_notify_change(void)
3006 if (tp_features.hotkey_tablet)
3007 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3008 "hotkey_tablet_mode");
3011 /* sysfs wakeup reason (pollable) -------------------------------------- */
3012 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
3013 struct device_attribute *attr,
3014 char *buf)
3016 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
3019 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
3021 static void hotkey_wakeup_reason_notify_change(void)
3023 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3024 "wakeup_reason");
3027 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3028 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
3029 struct device_attribute *attr,
3030 char *buf)
3032 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
3035 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
3036 hotkey_wakeup_hotunplug_complete_show, NULL);
3038 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3040 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
3041 "wakeup_hotunplug_complete");
3044 /* sysfs adaptive kbd mode --------------------------------------------- */
3046 static int adaptive_keyboard_get_mode(void);
3047 static int adaptive_keyboard_set_mode(int new_mode);
3049 enum ADAPTIVE_KEY_MODE {
3050 HOME_MODE,
3051 WEB_BROWSER_MODE,
3052 WEB_CONFERENCE_MODE,
3053 FUNCTION_MODE,
3054 LAYFLAT_MODE
3057 static ssize_t adaptive_kbd_mode_show(struct device *dev,
3058 struct device_attribute *attr,
3059 char *buf)
3061 int current_mode;
3063 current_mode = adaptive_keyboard_get_mode();
3064 if (current_mode < 0)
3065 return current_mode;
3067 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
3070 static ssize_t adaptive_kbd_mode_store(struct device *dev,
3071 struct device_attribute *attr,
3072 const char *buf, size_t count)
3074 unsigned long t;
3075 int res;
3077 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
3078 return -EINVAL;
3080 res = adaptive_keyboard_set_mode(t);
3081 return (res < 0) ? res : count;
3084 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3086 static struct attribute *adaptive_kbd_attributes[] = {
3087 &dev_attr_adaptive_kbd_mode.attr,
3088 NULL
3091 static const struct attribute_group adaptive_kbd_attr_group = {
3092 .attrs = adaptive_kbd_attributes,
3095 /* --------------------------------------------------------------------- */
3097 static struct attribute *hotkey_attributes[] __initdata = {
3098 &dev_attr_hotkey_enable.attr,
3099 &dev_attr_hotkey_bios_enabled.attr,
3100 &dev_attr_hotkey_bios_mask.attr,
3101 &dev_attr_wakeup_reason.attr,
3102 &dev_attr_wakeup_hotunplug_complete.attr,
3103 &dev_attr_hotkey_mask.attr,
3104 &dev_attr_hotkey_all_mask.attr,
3105 &dev_attr_hotkey_adaptive_all_mask.attr,
3106 &dev_attr_hotkey_recommended_mask.attr,
3107 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3108 &dev_attr_hotkey_source_mask.attr,
3109 &dev_attr_hotkey_poll_freq.attr,
3110 #endif
3114 * Sync both the hw and sw blocking state of all switches
3116 static void tpacpi_send_radiosw_update(void)
3118 int wlsw;
3121 * We must sync all rfkill controllers *before* issuing any
3122 * rfkill input events, or we will race the rfkill core input
3123 * handler.
3125 * tpacpi_inputdev_send_mutex works as a synchronization point
3126 * for the above.
3128 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3131 wlsw = hotkey_get_wlsw();
3133 /* Sync hw blocking state first if it is hw-blocked */
3134 if (wlsw == TPACPI_RFK_RADIO_OFF)
3135 tpacpi_rfk_update_hwblock_state(true);
3137 /* Sync sw blocking state */
3138 tpacpi_rfk_update_swstate_all();
3140 /* Sync hw blocking state last if it is hw-unblocked */
3141 if (wlsw == TPACPI_RFK_RADIO_ON)
3142 tpacpi_rfk_update_hwblock_state(false);
3144 /* Issue rfkill input event for WLSW switch */
3145 if (!(wlsw < 0)) {
3146 mutex_lock(&tpacpi_inputdev_send_mutex);
3148 input_report_switch(tpacpi_inputdev,
3149 SW_RFKILL_ALL, (wlsw > 0));
3150 input_sync(tpacpi_inputdev);
3152 mutex_unlock(&tpacpi_inputdev_send_mutex);
3156 * this can be unconditional, as we will poll state again
3157 * if userspace uses the notify to read data
3159 hotkey_radio_sw_notify_change();
3162 static void hotkey_exit(void)
3164 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3165 mutex_lock(&hotkey_mutex);
3166 hotkey_poll_stop_sync();
3167 mutex_unlock(&hotkey_mutex);
3168 #endif
3170 if (hotkey_dev_attributes)
3171 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3173 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3174 "restoring original HKEY status and mask\n");
3175 /* yes, there is a bitwise or below, we want the
3176 * functions to be called even if one of them fail */
3177 if (((tp_features.hotkey_mask &&
3178 hotkey_mask_set(hotkey_orig_mask)) |
3179 hotkey_status_set(false)) != 0)
3180 pr_err("failed to restore hot key mask to BIOS defaults\n");
3183 static void __init hotkey_unmap(const unsigned int scancode)
3185 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3186 clear_bit(hotkey_keycode_map[scancode],
3187 tpacpi_inputdev->keybit);
3188 hotkey_keycode_map[scancode] = KEY_RESERVED;
3193 * HKEY quirks:
3194 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3197 #define TPACPI_HK_Q_INIMASK 0x0001
3199 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3200 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3201 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3202 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3203 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3204 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3205 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3206 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3207 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3208 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3209 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3210 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3211 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3212 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3213 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3214 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3215 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3216 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3217 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3218 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3221 typedef u16 tpacpi_keymap_entry_t;
3222 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3224 static int hotkey_init_tablet_mode(void)
3226 int in_tablet_mode = 0, res;
3227 char *type = NULL;
3229 if (acpi_evalf(hkey_handle, &res, "GMMS", "qdd", 0)) {
3230 int has_tablet_mode;
3232 in_tablet_mode = hotkey_gmms_get_tablet_mode(res,
3233 &has_tablet_mode);
3234 if (has_tablet_mode)
3235 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_GMMS;
3236 type = "GMMS";
3237 } else if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3238 /* For X41t, X60t, X61t Tablets... */
3239 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3240 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3241 type = "MHKG";
3244 if (!tp_features.hotkey_tablet)
3245 return 0;
3247 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3248 type, in_tablet_mode ? "tablet" : "laptop");
3250 res = add_to_attr_set(hotkey_dev_attributes,
3251 &dev_attr_hotkey_tablet_mode.attr);
3252 if (res)
3253 return -1;
3255 return in_tablet_mode;
3258 static int __init hotkey_init(struct ibm_init_struct *iibm)
3260 /* Requirements for changing the default keymaps:
3262 * 1. Many of the keys are mapped to KEY_RESERVED for very
3263 * good reasons. Do not change them unless you have deep
3264 * knowledge on the IBM and Lenovo ThinkPad firmware for
3265 * the various ThinkPad models. The driver behaves
3266 * differently for KEY_RESERVED: such keys have their
3267 * hot key mask *unset* in mask_recommended, and also
3268 * in the initial hot key mask programmed into the
3269 * firmware at driver load time, which means the firm-
3270 * ware may react very differently if you change them to
3271 * something else;
3273 * 2. You must be subscribed to the linux-thinkpad and
3274 * ibm-acpi-devel mailing lists, and you should read the
3275 * list archives since 2007 if you want to change the
3276 * keymaps. This requirement exists so that you will
3277 * know the past history of problems with the thinkpad-
3278 * acpi driver keymaps, and also that you will be
3279 * listening to any bug reports;
3281 * 3. Do not send thinkpad-acpi specific patches directly to
3282 * for merging, *ever*. Send them to the linux-acpi
3283 * mailinglist for comments. Merging is to be done only
3284 * through acpi-test and the ACPI maintainer.
3286 * If the above is too much to ask, don't change the keymap.
3287 * Ask the thinkpad-acpi maintainer to do it, instead.
3290 enum keymap_index {
3291 TPACPI_KEYMAP_IBM_GENERIC = 0,
3292 TPACPI_KEYMAP_LENOVO_GENERIC,
3295 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3296 /* Generic keymap for IBM ThinkPads */
3297 [TPACPI_KEYMAP_IBM_GENERIC] = {
3298 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3299 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3300 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3301 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3303 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3304 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3305 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3306 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3308 /* brightness: firmware always reacts to them */
3309 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3310 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3312 /* Thinklight: firmware always react to it */
3313 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3315 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3316 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3318 /* Volume: firmware always react to it and reprograms
3319 * the built-in *extra* mixer. Never map it to control
3320 * another mixer by default. */
3321 KEY_RESERVED, /* 0x14: VOLUME UP */
3322 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3323 KEY_RESERVED, /* 0x16: MUTE */
3325 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3327 /* (assignments unknown, please report if found) */
3328 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3329 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3331 /* No assignments, only used for Adaptive keyboards. */
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, KEY_UNKNOWN,
3338 /* No assignment, used for newer Lenovo models */
3339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3341 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3342 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3343 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3344 KEY_UNKNOWN, KEY_UNKNOWN
3348 /* Generic keymap for Lenovo ThinkPads */
3349 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3350 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3351 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3352 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3353 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3355 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3356 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3357 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3358 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3360 /* These should be enabled --only-- when ACPI video
3361 * is disabled (i.e. in "vendor" mode), and are handled
3362 * in a special way by the init code */
3363 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3364 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3366 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3368 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3369 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3371 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3372 * react to it and reprograms the built-in *extra* mixer.
3373 * Never map it to control another mixer by default.
3375 * T60?, T61, R60?, R61: firmware and EC tries to send
3376 * these over the regular keyboard, so these are no-ops,
3377 * but there are still weird bugs re. MUTE, so do not
3378 * change unless you get test reports from all Lenovo
3379 * models. May cause the BIOS to interfere with the
3380 * HDA mixer.
3382 KEY_RESERVED, /* 0x14: VOLUME UP */
3383 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3384 KEY_RESERVED, /* 0x16: MUTE */
3386 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3388 /* (assignments unknown, please report if found) */
3389 KEY_UNKNOWN, KEY_UNKNOWN,
3392 * The mic mute button only sends 0x1a. It does not
3393 * automatically mute the mic or change the mute light.
3395 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3397 /* (assignments unknown, please report if found) */
3398 KEY_UNKNOWN,
3400 /* Extra keys in use since the X240 / T440 / T540 */
3401 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3404 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3405 * The first item in this list is the Mute button which is
3406 * emitted with 0x103 through
3407 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3408 * symbol is held.
3409 * We'll need to offset those by 0x20.
3411 KEY_RESERVED, /* Mute held, 0x103 */
3412 KEY_BRIGHTNESS_MIN, /* Backlight off */
3413 KEY_RESERVED, /* Clipping tool */
3414 KEY_RESERVED, /* Cloud */
3415 KEY_RESERVED,
3416 KEY_VOICECOMMAND, /* Voice */
3417 KEY_RESERVED,
3418 KEY_RESERVED, /* Gestures */
3419 KEY_RESERVED,
3420 KEY_RESERVED,
3421 KEY_RESERVED,
3422 KEY_CONFIG, /* Settings */
3423 KEY_RESERVED, /* New tab */
3424 KEY_REFRESH, /* Reload */
3425 KEY_BACK, /* Back */
3426 KEY_RESERVED, /* Microphone down */
3427 KEY_RESERVED, /* Microphone up */
3428 KEY_RESERVED, /* Microphone cancellation */
3429 KEY_RESERVED, /* Camera mode */
3430 KEY_RESERVED, /* Rotate display, 0x116 */
3433 * These are found in 2017 models (e.g. T470s, X270).
3434 * The lowest known value is 0x311, which according to
3435 * the manual should launch a user defined favorite
3436 * application.
3438 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3439 * corresponding to 0x34.
3442 /* (assignments unknown, please report if found) */
3443 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3444 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3445 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3446 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3447 KEY_UNKNOWN,
3449 KEY_BOOKMARKS, /* Favorite app, 0x311 */
3450 KEY_RESERVED, /* Clipping tool */
3451 KEY_CALC, /* Calculator (above numpad, P52) */
3452 KEY_BLUETOOTH, /* Bluetooth */
3453 KEY_KEYBOARD /* Keyboard, 0x315 */
3457 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3458 /* Generic maps (fallback) */
3460 .vendor = PCI_VENDOR_ID_IBM,
3461 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3462 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3465 .vendor = PCI_VENDOR_ID_LENOVO,
3466 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3467 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3471 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3472 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3474 int res, i;
3475 int status;
3476 int hkeyv;
3477 bool radiosw_state = false;
3478 bool tabletsw_state = false;
3480 unsigned long quirks;
3481 unsigned long keymap_id;
3483 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3484 "initializing hotkey subdriver\n");
3486 BUG_ON(!tpacpi_inputdev);
3487 BUG_ON(tpacpi_inputdev->open != NULL ||
3488 tpacpi_inputdev->close != NULL);
3490 TPACPI_ACPIHANDLE_INIT(hkey);
3491 mutex_init(&hotkey_mutex);
3493 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3494 mutex_init(&hotkey_thread_data_mutex);
3495 #endif
3497 /* hotkey not supported on 570 */
3498 tp_features.hotkey = hkey_handle != NULL;
3500 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3501 "hotkeys are %s\n",
3502 str_supported(tp_features.hotkey));
3504 if (!tp_features.hotkey)
3505 return 1;
3507 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3508 ARRAY_SIZE(tpacpi_hotkey_qtable));
3510 tpacpi_disable_brightness_delay();
3512 /* MUST have enough space for all attributes to be added to
3513 * hotkey_dev_attributes */
3514 hotkey_dev_attributes = create_attr_set(
3515 ARRAY_SIZE(hotkey_attributes) + 2,
3516 NULL);
3517 if (!hotkey_dev_attributes)
3518 return -ENOMEM;
3519 res = add_many_to_attr_set(hotkey_dev_attributes,
3520 hotkey_attributes,
3521 ARRAY_SIZE(hotkey_attributes));
3522 if (res)
3523 goto err_exit;
3525 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3526 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3527 for HKEY interface version 0x100 */
3528 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3529 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3530 "firmware HKEY interface version: 0x%x\n",
3531 hkeyv);
3533 switch (hkeyv >> 8) {
3534 case 1:
3536 * MHKV 0x100 in A31, R40, R40e,
3537 * T4x, X31, and later
3540 /* Paranoia check AND init hotkey_all_mask */
3541 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3542 "MHKA", "qd")) {
3543 pr_err("missing MHKA handler, please report this to %s\n",
3544 TPACPI_MAIL);
3545 /* Fallback: pre-init for FN+F3,F4,F12 */
3546 hotkey_all_mask = 0x080cU;
3547 } else {
3548 tp_features.hotkey_mask = 1;
3550 break;
3552 case 2:
3554 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3557 /* Paranoia check AND init hotkey_all_mask */
3558 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3559 "MHKA", "dd", 1)) {
3560 pr_err("missing MHKA handler, please report this to %s\n",
3561 TPACPI_MAIL);
3562 /* Fallback: pre-init for FN+F3,F4,F12 */
3563 hotkey_all_mask = 0x080cU;
3564 } else {
3565 tp_features.hotkey_mask = 1;
3569 * Check if we have an adaptive keyboard, like on the
3570 * Lenovo Carbon X1 2014 (2nd Gen).
3572 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3573 "MHKA", "dd", 2)) {
3574 if (hotkey_adaptive_all_mask != 0) {
3575 tp_features.has_adaptive_kbd = true;
3576 res = sysfs_create_group(
3577 &tpacpi_pdev->dev.kobj,
3578 &adaptive_kbd_attr_group);
3579 if (res)
3580 goto err_exit;
3582 } else {
3583 tp_features.has_adaptive_kbd = false;
3584 hotkey_adaptive_all_mask = 0x0U;
3586 break;
3588 default:
3589 pr_err("unknown version of the HKEY interface: 0x%x\n",
3590 hkeyv);
3591 pr_err("please report this to %s\n", TPACPI_MAIL);
3592 break;
3596 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3597 "hotkey masks are %s\n",
3598 str_supported(tp_features.hotkey_mask));
3600 /* Init hotkey_all_mask if not initialized yet */
3601 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3602 (quirks & TPACPI_HK_Q_INIMASK))
3603 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3605 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3606 if (tp_features.hotkey_mask) {
3607 /* hotkey_source_mask *must* be zero for
3608 * the first hotkey_mask_get to return hotkey_orig_mask */
3609 res = hotkey_mask_get();
3610 if (res)
3611 goto err_exit;
3613 hotkey_orig_mask = hotkey_acpi_mask;
3614 } else {
3615 hotkey_orig_mask = hotkey_all_mask;
3616 hotkey_acpi_mask = hotkey_all_mask;
3619 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3620 if (dbg_wlswemul) {
3621 tp_features.hotkey_wlsw = 1;
3622 radiosw_state = !!tpacpi_wlsw_emulstate;
3623 pr_info("radio switch emulation enabled\n");
3624 } else
3625 #endif
3626 /* Not all thinkpads have a hardware radio switch */
3627 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3628 tp_features.hotkey_wlsw = 1;
3629 radiosw_state = !!status;
3630 pr_info("radio switch found; radios are %s\n",
3631 enabled(status, 0));
3633 if (tp_features.hotkey_wlsw)
3634 res = add_to_attr_set(hotkey_dev_attributes,
3635 &dev_attr_hotkey_radio_sw.attr);
3637 res = hotkey_init_tablet_mode();
3638 if (res < 0)
3639 goto err_exit;
3641 tabletsw_state = res;
3643 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3644 &tpacpi_pdev->dev.kobj);
3645 if (res)
3646 goto err_exit;
3648 /* Set up key map */
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 hotkey_keycode_map = kmemdup(&tpacpi_keymaps[keymap_id],
3656 TPACPI_HOTKEY_MAP_SIZE, GFP_KERNEL);
3657 if (!hotkey_keycode_map) {
3658 pr_err("failed to allocate memory for key map\n");
3659 res = -ENOMEM;
3660 goto err_exit;
3663 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3664 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3665 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3666 tpacpi_inputdev->keycode = hotkey_keycode_map;
3667 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3668 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3669 input_set_capability(tpacpi_inputdev, EV_KEY,
3670 hotkey_keycode_map[i]);
3671 } else {
3672 if (i < sizeof(hotkey_reserved_mask)*8)
3673 hotkey_reserved_mask |= 1 << i;
3677 if (tp_features.hotkey_wlsw) {
3678 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3679 input_report_switch(tpacpi_inputdev,
3680 SW_RFKILL_ALL, radiosw_state);
3682 if (tp_features.hotkey_tablet) {
3683 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3684 input_report_switch(tpacpi_inputdev,
3685 SW_TABLET_MODE, tabletsw_state);
3688 /* Do not issue duplicate brightness change events to
3689 * userspace. tpacpi_detect_brightness_capabilities() must have
3690 * been called before this point */
3691 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3692 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3693 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3695 /* Disable brightness up/down on Lenovo thinkpads when
3696 * ACPI is handling them, otherwise it is plain impossible
3697 * for userspace to do something even remotely sane */
3698 hotkey_reserved_mask |=
3699 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3700 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3701 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3702 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3705 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3706 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3707 & ~hotkey_all_mask
3708 & ~hotkey_reserved_mask;
3710 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3711 "hotkey source mask 0x%08x, polling freq %u\n",
3712 hotkey_source_mask, hotkey_poll_freq);
3713 #endif
3715 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3716 "enabling firmware HKEY event interface...\n");
3717 res = hotkey_status_set(true);
3718 if (res) {
3719 hotkey_exit();
3720 return res;
3722 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3723 | hotkey_driver_mask)
3724 & ~hotkey_source_mask);
3725 if (res < 0 && res != -ENXIO) {
3726 hotkey_exit();
3727 return res;
3729 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3730 & ~hotkey_reserved_mask;
3731 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3732 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3733 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3735 tpacpi_inputdev->open = &hotkey_inputdev_open;
3736 tpacpi_inputdev->close = &hotkey_inputdev_close;
3738 hotkey_poll_setup_safe(true);
3740 return 0;
3742 err_exit:
3743 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3744 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3745 &adaptive_kbd_attr_group);
3747 hotkey_dev_attributes = NULL;
3749 return (res < 0) ? res : 1;
3752 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3753 * mode, Web conference mode, Function mode and Lay-flat mode.
3754 * We support Home mode and Function mode currently.
3756 * Will consider support rest of modes in future.
3759 static const int adaptive_keyboard_modes[] = {
3760 HOME_MODE,
3761 /* WEB_BROWSER_MODE = 2,
3762 WEB_CONFERENCE_MODE = 3, */
3763 FUNCTION_MODE
3766 #define DFR_CHANGE_ROW 0x101
3767 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3768 #define FIRST_ADAPTIVE_KEY 0x103
3770 /* press Fn key a while second, it will switch to Function Mode. Then
3771 * release Fn key, previous mode be restored.
3773 static bool adaptive_keyboard_mode_is_saved;
3774 static int adaptive_keyboard_prev_mode;
3776 static int adaptive_keyboard_get_mode(void)
3778 int mode = 0;
3780 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3781 pr_err("Cannot read adaptive keyboard mode\n");
3782 return -EIO;
3785 return mode;
3788 static int adaptive_keyboard_set_mode(int new_mode)
3790 if (new_mode < 0 ||
3791 new_mode > LAYFLAT_MODE)
3792 return -EINVAL;
3794 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3795 pr_err("Cannot set adaptive keyboard mode\n");
3796 return -EIO;
3799 return 0;
3802 static int adaptive_keyboard_get_next_mode(int mode)
3804 size_t i;
3805 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3807 for (i = 0; i <= max_mode; i++) {
3808 if (adaptive_keyboard_modes[i] == mode)
3809 break;
3812 if (i >= max_mode)
3813 i = 0;
3814 else
3815 i++;
3817 return adaptive_keyboard_modes[i];
3820 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3822 int current_mode = 0;
3823 int new_mode = 0;
3824 int keycode;
3826 switch (scancode) {
3827 case DFR_CHANGE_ROW:
3828 if (adaptive_keyboard_mode_is_saved) {
3829 new_mode = adaptive_keyboard_prev_mode;
3830 adaptive_keyboard_mode_is_saved = false;
3831 } else {
3832 current_mode = adaptive_keyboard_get_mode();
3833 if (current_mode < 0)
3834 return false;
3835 new_mode = adaptive_keyboard_get_next_mode(
3836 current_mode);
3839 if (adaptive_keyboard_set_mode(new_mode) < 0)
3840 return false;
3842 return true;
3844 case DFR_SHOW_QUICKVIEW_ROW:
3845 current_mode = adaptive_keyboard_get_mode();
3846 if (current_mode < 0)
3847 return false;
3849 adaptive_keyboard_prev_mode = current_mode;
3850 adaptive_keyboard_mode_is_saved = true;
3852 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3853 return false;
3854 return true;
3856 default:
3857 if (scancode < FIRST_ADAPTIVE_KEY ||
3858 scancode >= FIRST_ADAPTIVE_KEY +
3859 TP_ACPI_HOTKEYSCAN_EXTENDED_START -
3860 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3861 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3862 scancode);
3863 return false;
3865 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY +
3866 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START];
3867 if (keycode != KEY_RESERVED) {
3868 mutex_lock(&tpacpi_inputdev_send_mutex);
3870 input_report_key(tpacpi_inputdev, keycode, 1);
3871 input_sync(tpacpi_inputdev);
3873 input_report_key(tpacpi_inputdev, keycode, 0);
3874 input_sync(tpacpi_inputdev);
3876 mutex_unlock(&tpacpi_inputdev_send_mutex);
3878 return true;
3882 static bool hotkey_notify_hotkey(const u32 hkey,
3883 bool *send_acpi_ev,
3884 bool *ignore_acpi_ev)
3886 /* 0x1000-0x1FFF: key presses */
3887 unsigned int scancode = hkey & 0xfff;
3888 *send_acpi_ev = true;
3889 *ignore_acpi_ev = false;
3892 * Original events are in the 0x10XX range, the adaptive keyboard
3893 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3894 * models, additional keys are emitted through 0x13XX.
3896 switch ((hkey >> 8) & 0xf) {
3897 case 0:
3898 if (scancode > 0 &&
3899 scancode <= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START) {
3900 /* HKEY event 0x1001 is scancode 0x00 */
3901 scancode--;
3902 if (!(hotkey_source_mask & (1 << scancode))) {
3903 tpacpi_input_send_key_masked(scancode);
3904 *send_acpi_ev = false;
3905 } else {
3906 *ignore_acpi_ev = true;
3908 return true;
3910 break;
3912 case 1:
3913 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3915 case 3:
3916 /* Extended keycodes start at 0x300 and our offset into the map
3917 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3918 * will be positive, but might not be in the correct range.
3920 scancode -= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START);
3921 if (scancode >= TP_ACPI_HOTKEYSCAN_EXTENDED_START &&
3922 scancode < TPACPI_HOTKEY_MAP_LEN) {
3923 tpacpi_input_send_key(scancode);
3924 return true;
3926 break;
3929 return false;
3932 static bool hotkey_notify_wakeup(const u32 hkey,
3933 bool *send_acpi_ev,
3934 bool *ignore_acpi_ev)
3936 /* 0x2000-0x2FFF: Wakeup reason */
3937 *send_acpi_ev = true;
3938 *ignore_acpi_ev = false;
3940 switch (hkey) {
3941 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3942 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3943 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3944 *ignore_acpi_ev = true;
3945 break;
3947 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3948 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3949 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3950 *ignore_acpi_ev = true;
3951 break;
3953 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3954 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3955 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3956 /* how to auto-heal: */
3957 /* 2313: woke up from S3, go to S4/S5 */
3958 /* 2413: woke up from S4, go to S5 */
3959 break;
3961 default:
3962 return false;
3965 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3966 pr_info("woke up due to a hot-unplug request...\n");
3967 hotkey_wakeup_reason_notify_change();
3969 return true;
3972 static bool hotkey_notify_dockevent(const u32 hkey,
3973 bool *send_acpi_ev,
3974 bool *ignore_acpi_ev)
3976 /* 0x4000-0x4FFF: dock-related events */
3977 *send_acpi_ev = true;
3978 *ignore_acpi_ev = false;
3980 switch (hkey) {
3981 case TP_HKEY_EV_UNDOCK_ACK:
3982 /* ACPI undock operation completed after wakeup */
3983 hotkey_autosleep_ack = 1;
3984 pr_info("undocked\n");
3985 hotkey_wakeup_hotunplug_complete_notify_change();
3986 return true;
3988 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3989 pr_info("docked into hotplug port replicator\n");
3990 return true;
3991 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3992 pr_info("undocked from hotplug port replicator\n");
3993 return true;
3995 default:
3996 return false;
4000 static bool hotkey_notify_usrevent(const u32 hkey,
4001 bool *send_acpi_ev,
4002 bool *ignore_acpi_ev)
4004 /* 0x5000-0x5FFF: human interface helpers */
4005 *send_acpi_ev = true;
4006 *ignore_acpi_ev = false;
4008 switch (hkey) {
4009 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
4010 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
4011 return true;
4013 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
4014 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
4015 tpacpi_input_send_tabletsw();
4016 hotkey_tablet_mode_notify_change();
4017 *send_acpi_ev = false;
4018 return true;
4020 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
4021 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
4022 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
4023 /* do not propagate these events */
4024 *ignore_acpi_ev = true;
4025 return true;
4027 default:
4028 return false;
4032 static void thermal_dump_all_sensors(void);
4034 static bool hotkey_notify_6xxx(const u32 hkey,
4035 bool *send_acpi_ev,
4036 bool *ignore_acpi_ev)
4038 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4039 *send_acpi_ev = true;
4040 *ignore_acpi_ev = false;
4042 switch (hkey) {
4043 case TP_HKEY_EV_THM_TABLE_CHANGED:
4044 pr_debug("EC reports: Thermal Table has changed\n");
4045 /* recommended action: do nothing, we don't have
4046 * Lenovo ATM information */
4047 return true;
4048 case TP_HKEY_EV_THM_CSM_COMPLETED:
4049 pr_debug("EC reports: Thermal Control Command set completed (DYTC)\n");
4050 /* recommended action: do nothing, we don't have
4051 * Lenovo ATM information */
4052 return true;
4053 case TP_HKEY_EV_THM_TRANSFM_CHANGED:
4054 pr_debug("EC reports: Thermal Transformation changed (GMTS)\n");
4055 /* recommended action: do nothing, we don't have
4056 * Lenovo ATM information */
4057 return true;
4058 case TP_HKEY_EV_ALARM_BAT_HOT:
4059 pr_crit("THERMAL ALARM: battery is too hot!\n");
4060 /* recommended action: warn user through gui */
4061 break;
4062 case TP_HKEY_EV_ALARM_BAT_XHOT:
4063 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4064 /* recommended action: immediate sleep/hibernate */
4065 break;
4066 case TP_HKEY_EV_ALARM_SENSOR_HOT:
4067 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4068 /* recommended action: warn user through gui, that */
4069 /* some internal component is too hot */
4070 break;
4071 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
4072 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4073 /* recommended action: immediate sleep/hibernate */
4074 break;
4075 case TP_HKEY_EV_AC_CHANGED:
4076 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4077 * AC status changed; can be triggered by plugging or
4078 * unplugging AC adapter, docking or undocking. */
4080 /* fallthrough */
4082 case TP_HKEY_EV_KEY_NUMLOCK:
4083 case TP_HKEY_EV_KEY_FN:
4084 case TP_HKEY_EV_KEY_FN_ESC:
4085 /* key press events, we just ignore them as long as the EC
4086 * is still reporting them in the normal keyboard stream */
4087 *send_acpi_ev = false;
4088 *ignore_acpi_ev = true;
4089 return true;
4091 case TP_HKEY_EV_TABLET_CHANGED:
4092 tpacpi_input_send_tabletsw();
4093 hotkey_tablet_mode_notify_change();
4094 *send_acpi_ev = false;
4095 return true;
4097 case TP_HKEY_EV_PALM_DETECTED:
4098 case TP_HKEY_EV_PALM_UNDETECTED:
4099 /* palm detected hovering the keyboard, forward to user-space
4100 * via netlink for consumption */
4101 return true;
4103 default:
4104 /* report simply as unknown, no sensor dump */
4105 return false;
4108 thermal_dump_all_sensors();
4109 return true;
4112 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
4114 u32 hkey;
4115 bool send_acpi_ev;
4116 bool ignore_acpi_ev;
4117 bool known_ev;
4119 if (event != 0x80) {
4120 pr_err("unknown HKEY notification event %d\n", event);
4121 /* forward it to userspace, maybe it knows how to handle it */
4122 acpi_bus_generate_netlink_event(
4123 ibm->acpi->device->pnp.device_class,
4124 dev_name(&ibm->acpi->device->dev),
4125 event, 0);
4126 return;
4129 while (1) {
4130 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
4131 pr_err("failed to retrieve HKEY event\n");
4132 return;
4135 if (hkey == 0) {
4136 /* queue empty */
4137 return;
4140 send_acpi_ev = true;
4141 ignore_acpi_ev = false;
4143 switch (hkey >> 12) {
4144 case 1:
4145 /* 0x1000-0x1FFF: key presses */
4146 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4147 &ignore_acpi_ev);
4148 break;
4149 case 2:
4150 /* 0x2000-0x2FFF: Wakeup reason */
4151 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4152 &ignore_acpi_ev);
4153 break;
4154 case 3:
4155 /* 0x3000-0x3FFF: bay-related wakeups */
4156 switch (hkey) {
4157 case TP_HKEY_EV_BAYEJ_ACK:
4158 hotkey_autosleep_ack = 1;
4159 pr_info("bay ejected\n");
4160 hotkey_wakeup_hotunplug_complete_notify_change();
4161 known_ev = true;
4162 break;
4163 case TP_HKEY_EV_OPTDRV_EJ:
4164 /* FIXME: kick libata if SATA link offline */
4165 known_ev = true;
4166 break;
4167 default:
4168 known_ev = false;
4170 break;
4171 case 4:
4172 /* 0x4000-0x4FFF: dock-related events */
4173 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4174 &ignore_acpi_ev);
4175 break;
4176 case 5:
4177 /* 0x5000-0x5FFF: human interface helpers */
4178 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4179 &ignore_acpi_ev);
4180 break;
4181 case 6:
4182 /* 0x6000-0x6FFF: thermal alarms/notices and
4183 * keyboard events */
4184 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4185 &ignore_acpi_ev);
4186 break;
4187 case 7:
4188 /* 0x7000-0x7FFF: misc */
4189 if (tp_features.hotkey_wlsw &&
4190 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4191 tpacpi_send_radiosw_update();
4192 send_acpi_ev = 0;
4193 known_ev = true;
4194 break;
4196 /* fallthrough - to default */
4197 default:
4198 known_ev = false;
4200 if (!known_ev) {
4201 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4202 pr_notice("please report the conditions when this event happened to %s\n",
4203 TPACPI_MAIL);
4206 /* netlink events */
4207 if (!ignore_acpi_ev && send_acpi_ev) {
4208 acpi_bus_generate_netlink_event(
4209 ibm->acpi->device->pnp.device_class,
4210 dev_name(&ibm->acpi->device->dev),
4211 event, hkey);
4216 static void hotkey_suspend(void)
4218 /* Do these on suspend, we get the events on early resume! */
4219 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4220 hotkey_autosleep_ack = 0;
4222 /* save previous mode of adaptive keyboard of X1 Carbon */
4223 if (tp_features.has_adaptive_kbd) {
4224 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4225 "GTRW", "dd", 0)) {
4226 pr_err("Cannot read adaptive keyboard mode.\n");
4231 static void hotkey_resume(void)
4233 tpacpi_disable_brightness_delay();
4235 if (hotkey_status_set(true) < 0 ||
4236 hotkey_mask_set(hotkey_acpi_mask) < 0)
4237 pr_err("error while attempting to reset the event firmware interface\n");
4239 tpacpi_send_radiosw_update();
4240 hotkey_tablet_mode_notify_change();
4241 hotkey_wakeup_reason_notify_change();
4242 hotkey_wakeup_hotunplug_complete_notify_change();
4243 hotkey_poll_setup_safe(false);
4245 /* restore previous mode of adapive keyboard of X1 Carbon */
4246 if (tp_features.has_adaptive_kbd) {
4247 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4248 adaptive_keyboard_prev_mode)) {
4249 pr_err("Cannot set adaptive keyboard mode.\n");
4254 /* procfs -------------------------------------------------------------- */
4255 static int hotkey_read(struct seq_file *m)
4257 int res, status;
4259 if (!tp_features.hotkey) {
4260 seq_printf(m, "status:\t\tnot supported\n");
4261 return 0;
4264 if (mutex_lock_killable(&hotkey_mutex))
4265 return -ERESTARTSYS;
4266 res = hotkey_status_get(&status);
4267 if (!res)
4268 res = hotkey_mask_get();
4269 mutex_unlock(&hotkey_mutex);
4270 if (res)
4271 return res;
4273 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4274 if (hotkey_all_mask) {
4275 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4276 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4277 } else {
4278 seq_printf(m, "mask:\t\tnot supported\n");
4279 seq_printf(m, "commands:\tenable, disable, reset\n");
4282 return 0;
4285 static void hotkey_enabledisable_warn(bool enable)
4287 tpacpi_log_usertask("procfs hotkey enable/disable");
4288 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4289 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4290 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4293 static int hotkey_write(char *buf)
4295 int res;
4296 u32 mask;
4297 char *cmd;
4299 if (!tp_features.hotkey)
4300 return -ENODEV;
4302 if (mutex_lock_killable(&hotkey_mutex))
4303 return -ERESTARTSYS;
4305 mask = hotkey_user_mask;
4307 res = 0;
4308 while ((cmd = next_cmd(&buf))) {
4309 if (strlencmp(cmd, "enable") == 0) {
4310 hotkey_enabledisable_warn(1);
4311 } else if (strlencmp(cmd, "disable") == 0) {
4312 hotkey_enabledisable_warn(0);
4313 res = -EPERM;
4314 } else if (strlencmp(cmd, "reset") == 0) {
4315 mask = (hotkey_all_mask | hotkey_source_mask)
4316 & ~hotkey_reserved_mask;
4317 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4318 /* mask set */
4319 } else if (sscanf(cmd, "%x", &mask) == 1) {
4320 /* mask set */
4321 } else {
4322 res = -EINVAL;
4323 goto errexit;
4327 if (!res) {
4328 tpacpi_disclose_usertask("procfs hotkey",
4329 "set mask to 0x%08x\n", mask);
4330 res = hotkey_user_mask_set(mask);
4333 errexit:
4334 mutex_unlock(&hotkey_mutex);
4335 return res;
4338 static const struct acpi_device_id ibm_htk_device_ids[] = {
4339 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4340 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4341 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4342 {"", 0},
4345 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4346 .hid = ibm_htk_device_ids,
4347 .notify = hotkey_notify,
4348 .handle = &hkey_handle,
4349 .type = ACPI_DEVICE_NOTIFY,
4352 static struct ibm_struct hotkey_driver_data = {
4353 .name = "hotkey",
4354 .read = hotkey_read,
4355 .write = hotkey_write,
4356 .exit = hotkey_exit,
4357 .resume = hotkey_resume,
4358 .suspend = hotkey_suspend,
4359 .acpi = &ibm_hotkey_acpidriver,
4362 /*************************************************************************
4363 * Bluetooth subdriver
4366 enum {
4367 /* ACPI GBDC/SBDC bits */
4368 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4369 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4370 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4371 0 = disable, 1 = enable */
4374 enum {
4375 /* ACPI \BLTH commands */
4376 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4377 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4378 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4379 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4380 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4383 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4385 static int bluetooth_get_status(void)
4387 int status;
4389 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4390 if (dbg_bluetoothemul)
4391 return (tpacpi_bluetooth_emulstate) ?
4392 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4393 #endif
4395 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4396 return -EIO;
4398 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4399 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4402 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4404 int status;
4406 vdbg_printk(TPACPI_DBG_RFKILL,
4407 "will attempt to %s bluetooth\n",
4408 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4410 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4411 if (dbg_bluetoothemul) {
4412 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4413 return 0;
4415 #endif
4417 if (state == TPACPI_RFK_RADIO_ON)
4418 status = TP_ACPI_BLUETOOTH_RADIOSSW
4419 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4420 else
4421 status = 0;
4423 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4424 return -EIO;
4426 return 0;
4429 /* sysfs bluetooth enable ---------------------------------------------- */
4430 static ssize_t bluetooth_enable_show(struct device *dev,
4431 struct device_attribute *attr,
4432 char *buf)
4434 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4435 attr, buf);
4438 static ssize_t bluetooth_enable_store(struct device *dev,
4439 struct device_attribute *attr,
4440 const char *buf, size_t count)
4442 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4443 attr, buf, count);
4446 static DEVICE_ATTR_RW(bluetooth_enable);
4448 /* --------------------------------------------------------------------- */
4450 static struct attribute *bluetooth_attributes[] = {
4451 &dev_attr_bluetooth_enable.attr,
4452 NULL
4455 static const struct attribute_group bluetooth_attr_group = {
4456 .attrs = bluetooth_attributes,
4459 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4460 .get_status = bluetooth_get_status,
4461 .set_status = bluetooth_set_status,
4464 static void bluetooth_shutdown(void)
4466 /* Order firmware to save current state to NVRAM */
4467 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4468 TP_ACPI_BLTH_SAVE_STATE))
4469 pr_notice("failed to save bluetooth state to NVRAM\n");
4470 else
4471 vdbg_printk(TPACPI_DBG_RFKILL,
4472 "bluetooth state saved to NVRAM\n");
4475 static void bluetooth_exit(void)
4477 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4478 &bluetooth_attr_group);
4480 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4482 bluetooth_shutdown();
4485 static const struct dmi_system_id bt_fwbug_list[] __initconst = {
4487 .ident = "ThinkPad E485",
4488 .matches = {
4489 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4490 DMI_MATCH(DMI_BOARD_NAME, "20KU"),
4494 .ident = "ThinkPad E585",
4495 .matches = {
4496 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4497 DMI_MATCH(DMI_BOARD_NAME, "20KV"),
4501 .ident = "ThinkPad A285 - 20MW",
4502 .matches = {
4503 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4504 DMI_MATCH(DMI_BOARD_NAME, "20MW"),
4508 .ident = "ThinkPad A285 - 20MX",
4509 .matches = {
4510 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4511 DMI_MATCH(DMI_BOARD_NAME, "20MX"),
4515 .ident = "ThinkPad A485 - 20MU",
4516 .matches = {
4517 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4518 DMI_MATCH(DMI_BOARD_NAME, "20MU"),
4522 .ident = "ThinkPad A485 - 20MV",
4523 .matches = {
4524 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
4525 DMI_MATCH(DMI_BOARD_NAME, "20MV"),
4531 static const struct pci_device_id fwbug_cards_ids[] __initconst = {
4532 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) },
4533 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) },
4534 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) },
4539 static int __init have_bt_fwbug(void)
4542 * Some AMD based ThinkPads have a firmware bug that calling
4543 * "GBDC" will cause bluetooth on Intel wireless cards blocked
4545 if (dmi_check_system(bt_fwbug_list) && pci_dev_present(fwbug_cards_ids)) {
4546 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4547 FW_BUG "disable bluetooth subdriver for Intel cards\n");
4548 return 1;
4549 } else
4550 return 0;
4553 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4555 int res;
4556 int status = 0;
4558 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4559 "initializing bluetooth subdriver\n");
4561 TPACPI_ACPIHANDLE_INIT(hkey);
4563 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4564 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4565 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle &&
4566 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4568 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4569 "bluetooth is %s, status 0x%02x\n",
4570 str_supported(tp_features.bluetooth),
4571 status);
4573 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4574 if (dbg_bluetoothemul) {
4575 tp_features.bluetooth = 1;
4576 pr_info("bluetooth switch emulation enabled\n");
4577 } else
4578 #endif
4579 if (tp_features.bluetooth &&
4580 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4581 /* no bluetooth hardware present in system */
4582 tp_features.bluetooth = 0;
4583 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4584 "bluetooth hardware not installed\n");
4587 if (!tp_features.bluetooth)
4588 return 1;
4590 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4591 &bluetooth_tprfk_ops,
4592 RFKILL_TYPE_BLUETOOTH,
4593 TPACPI_RFK_BLUETOOTH_SW_NAME,
4594 true);
4595 if (res)
4596 return res;
4598 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4599 &bluetooth_attr_group);
4600 if (res) {
4601 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4602 return res;
4605 return 0;
4608 /* procfs -------------------------------------------------------------- */
4609 static int bluetooth_read(struct seq_file *m)
4611 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4614 static int bluetooth_write(char *buf)
4616 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4619 static struct ibm_struct bluetooth_driver_data = {
4620 .name = "bluetooth",
4621 .read = bluetooth_read,
4622 .write = bluetooth_write,
4623 .exit = bluetooth_exit,
4624 .shutdown = bluetooth_shutdown,
4627 /*************************************************************************
4628 * Wan subdriver
4631 enum {
4632 /* ACPI GWAN/SWAN bits */
4633 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4634 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4635 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4636 0 = disable, 1 = enable */
4639 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4641 static int wan_get_status(void)
4643 int status;
4645 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4646 if (dbg_wwanemul)
4647 return (tpacpi_wwan_emulstate) ?
4648 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4649 #endif
4651 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4652 return -EIO;
4654 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4655 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4658 static int wan_set_status(enum tpacpi_rfkill_state state)
4660 int status;
4662 vdbg_printk(TPACPI_DBG_RFKILL,
4663 "will attempt to %s wwan\n",
4664 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4666 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4667 if (dbg_wwanemul) {
4668 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4669 return 0;
4671 #endif
4673 if (state == TPACPI_RFK_RADIO_ON)
4674 status = TP_ACPI_WANCARD_RADIOSSW
4675 | TP_ACPI_WANCARD_RESUMECTRL;
4676 else
4677 status = 0;
4679 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4680 return -EIO;
4682 return 0;
4685 /* sysfs wan enable ---------------------------------------------------- */
4686 static ssize_t wan_enable_show(struct device *dev,
4687 struct device_attribute *attr,
4688 char *buf)
4690 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4691 attr, buf);
4694 static ssize_t wan_enable_store(struct device *dev,
4695 struct device_attribute *attr,
4696 const char *buf, size_t count)
4698 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4699 attr, buf, count);
4702 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4703 wan_enable_show, wan_enable_store);
4705 /* --------------------------------------------------------------------- */
4707 static struct attribute *wan_attributes[] = {
4708 &dev_attr_wwan_enable.attr,
4709 NULL
4712 static const struct attribute_group wan_attr_group = {
4713 .attrs = wan_attributes,
4716 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4717 .get_status = wan_get_status,
4718 .set_status = wan_set_status,
4721 static void wan_shutdown(void)
4723 /* Order firmware to save current state to NVRAM */
4724 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4725 TP_ACPI_WGSV_SAVE_STATE))
4726 pr_notice("failed to save WWAN state to NVRAM\n");
4727 else
4728 vdbg_printk(TPACPI_DBG_RFKILL,
4729 "WWAN state saved to NVRAM\n");
4732 static void wan_exit(void)
4734 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4735 &wan_attr_group);
4737 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4739 wan_shutdown();
4742 static int __init wan_init(struct ibm_init_struct *iibm)
4744 int res;
4745 int status = 0;
4747 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4748 "initializing wan subdriver\n");
4750 TPACPI_ACPIHANDLE_INIT(hkey);
4752 tp_features.wan = hkey_handle &&
4753 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4755 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4756 "wan is %s, status 0x%02x\n",
4757 str_supported(tp_features.wan),
4758 status);
4760 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4761 if (dbg_wwanemul) {
4762 tp_features.wan = 1;
4763 pr_info("wwan switch emulation enabled\n");
4764 } else
4765 #endif
4766 if (tp_features.wan &&
4767 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4768 /* no wan hardware present in system */
4769 tp_features.wan = 0;
4770 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4771 "wan hardware not installed\n");
4774 if (!tp_features.wan)
4775 return 1;
4777 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4778 &wan_tprfk_ops,
4779 RFKILL_TYPE_WWAN,
4780 TPACPI_RFK_WWAN_SW_NAME,
4781 true);
4782 if (res)
4783 return res;
4785 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4786 &wan_attr_group);
4788 if (res) {
4789 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4790 return res;
4793 return 0;
4796 /* procfs -------------------------------------------------------------- */
4797 static int wan_read(struct seq_file *m)
4799 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4802 static int wan_write(char *buf)
4804 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4807 static struct ibm_struct wan_driver_data = {
4808 .name = "wan",
4809 .read = wan_read,
4810 .write = wan_write,
4811 .exit = wan_exit,
4812 .shutdown = wan_shutdown,
4815 /*************************************************************************
4816 * UWB subdriver
4819 enum {
4820 /* ACPI GUWB/SUWB bits */
4821 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4822 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4825 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4827 static int uwb_get_status(void)
4829 int status;
4831 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4832 if (dbg_uwbemul)
4833 return (tpacpi_uwb_emulstate) ?
4834 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4835 #endif
4837 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4838 return -EIO;
4840 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4841 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4844 static int uwb_set_status(enum tpacpi_rfkill_state state)
4846 int status;
4848 vdbg_printk(TPACPI_DBG_RFKILL,
4849 "will attempt to %s UWB\n",
4850 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4852 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4853 if (dbg_uwbemul) {
4854 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4855 return 0;
4857 #endif
4859 if (state == TPACPI_RFK_RADIO_ON)
4860 status = TP_ACPI_UWB_RADIOSSW;
4861 else
4862 status = 0;
4864 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4865 return -EIO;
4867 return 0;
4870 /* --------------------------------------------------------------------- */
4872 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4873 .get_status = uwb_get_status,
4874 .set_status = uwb_set_status,
4877 static void uwb_exit(void)
4879 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4882 static int __init uwb_init(struct ibm_init_struct *iibm)
4884 int res;
4885 int status = 0;
4887 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4888 "initializing uwb subdriver\n");
4890 TPACPI_ACPIHANDLE_INIT(hkey);
4892 tp_features.uwb = hkey_handle &&
4893 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4895 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4896 "uwb is %s, status 0x%02x\n",
4897 str_supported(tp_features.uwb),
4898 status);
4900 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4901 if (dbg_uwbemul) {
4902 tp_features.uwb = 1;
4903 pr_info("uwb switch emulation enabled\n");
4904 } else
4905 #endif
4906 if (tp_features.uwb &&
4907 !(status & TP_ACPI_UWB_HWPRESENT)) {
4908 /* no uwb hardware present in system */
4909 tp_features.uwb = 0;
4910 dbg_printk(TPACPI_DBG_INIT,
4911 "uwb hardware not installed\n");
4914 if (!tp_features.uwb)
4915 return 1;
4917 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4918 &uwb_tprfk_ops,
4919 RFKILL_TYPE_UWB,
4920 TPACPI_RFK_UWB_SW_NAME,
4921 false);
4922 return res;
4925 static struct ibm_struct uwb_driver_data = {
4926 .name = "uwb",
4927 .exit = uwb_exit,
4928 .flags.experimental = 1,
4931 /*************************************************************************
4932 * Video subdriver
4935 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4937 enum video_access_mode {
4938 TPACPI_VIDEO_NONE = 0,
4939 TPACPI_VIDEO_570, /* 570 */
4940 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4941 TPACPI_VIDEO_NEW, /* all others */
4944 enum { /* video status flags, based on VIDEO_570 */
4945 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4946 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4947 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4950 enum { /* TPACPI_VIDEO_570 constants */
4951 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4952 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4953 * video_status_flags */
4954 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4955 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4958 static enum video_access_mode video_supported;
4959 static int video_orig_autosw;
4961 static int video_autosw_get(void);
4962 static int video_autosw_set(int enable);
4964 TPACPI_HANDLE(vid, root,
4965 "\\_SB.PCI.AGP.VGA", /* 570 */
4966 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4967 "\\_SB.PCI0.VID0", /* 770e */
4968 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4969 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4970 "\\_SB.PCI0.AGP.VID", /* all others */
4971 ); /* R30, R31 */
4973 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4975 static int __init video_init(struct ibm_init_struct *iibm)
4977 int ivga;
4979 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4981 TPACPI_ACPIHANDLE_INIT(vid);
4982 if (tpacpi_is_ibm())
4983 TPACPI_ACPIHANDLE_INIT(vid2);
4985 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4986 /* G41, assume IVGA doesn't change */
4987 vid_handle = vid2_handle;
4989 if (!vid_handle)
4990 /* video switching not supported on R30, R31 */
4991 video_supported = TPACPI_VIDEO_NONE;
4992 else if (tpacpi_is_ibm() &&
4993 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4994 /* 570 */
4995 video_supported = TPACPI_VIDEO_570;
4996 else if (tpacpi_is_ibm() &&
4997 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4998 /* 600e/x, 770e, 770x */
4999 video_supported = TPACPI_VIDEO_770;
5000 else
5001 /* all others */
5002 video_supported = TPACPI_VIDEO_NEW;
5004 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
5005 str_supported(video_supported != TPACPI_VIDEO_NONE),
5006 video_supported);
5008 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
5011 static void video_exit(void)
5013 dbg_printk(TPACPI_DBG_EXIT,
5014 "restoring original video autoswitch mode\n");
5015 if (video_autosw_set(video_orig_autosw))
5016 pr_err("error while trying to restore original video autoswitch mode\n");
5019 static int video_outputsw_get(void)
5021 int status = 0;
5022 int i;
5024 switch (video_supported) {
5025 case TPACPI_VIDEO_570:
5026 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
5027 TP_ACPI_VIDEO_570_PHSCMD))
5028 return -EIO;
5029 status = i & TP_ACPI_VIDEO_570_PHSMASK;
5030 break;
5031 case TPACPI_VIDEO_770:
5032 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
5033 return -EIO;
5034 if (i)
5035 status |= TP_ACPI_VIDEO_S_LCD;
5036 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
5037 return -EIO;
5038 if (i)
5039 status |= TP_ACPI_VIDEO_S_CRT;
5040 break;
5041 case TPACPI_VIDEO_NEW:
5042 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
5043 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
5044 return -EIO;
5045 if (i)
5046 status |= TP_ACPI_VIDEO_S_CRT;
5048 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
5049 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
5050 return -EIO;
5051 if (i)
5052 status |= TP_ACPI_VIDEO_S_LCD;
5053 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
5054 return -EIO;
5055 if (i)
5056 status |= TP_ACPI_VIDEO_S_DVI;
5057 break;
5058 default:
5059 return -ENOSYS;
5062 return status;
5065 static int video_outputsw_set(int status)
5067 int autosw;
5068 int res = 0;
5070 switch (video_supported) {
5071 case TPACPI_VIDEO_570:
5072 res = acpi_evalf(NULL, NULL,
5073 "\\_SB.PHS2", "vdd",
5074 TP_ACPI_VIDEO_570_PHS2CMD,
5075 status | TP_ACPI_VIDEO_570_PHS2SET);
5076 break;
5077 case TPACPI_VIDEO_770:
5078 autosw = video_autosw_get();
5079 if (autosw < 0)
5080 return autosw;
5082 res = video_autosw_set(1);
5083 if (res)
5084 return res;
5085 res = acpi_evalf(vid_handle, NULL,
5086 "ASWT", "vdd", status * 0x100, 0);
5087 if (!autosw && video_autosw_set(autosw)) {
5088 pr_err("video auto-switch left enabled due to error\n");
5089 return -EIO;
5091 break;
5092 case TPACPI_VIDEO_NEW:
5093 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
5094 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
5095 break;
5096 default:
5097 return -ENOSYS;
5100 return (res) ? 0 : -EIO;
5103 static int video_autosw_get(void)
5105 int autosw = 0;
5107 switch (video_supported) {
5108 case TPACPI_VIDEO_570:
5109 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
5110 return -EIO;
5111 break;
5112 case TPACPI_VIDEO_770:
5113 case TPACPI_VIDEO_NEW:
5114 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
5115 return -EIO;
5116 break;
5117 default:
5118 return -ENOSYS;
5121 return autosw & 1;
5124 static int video_autosw_set(int enable)
5126 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
5127 return -EIO;
5128 return 0;
5131 static int video_outputsw_cycle(void)
5133 int autosw = video_autosw_get();
5134 int res;
5136 if (autosw < 0)
5137 return autosw;
5139 switch (video_supported) {
5140 case TPACPI_VIDEO_570:
5141 res = video_autosw_set(1);
5142 if (res)
5143 return res;
5144 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
5145 break;
5146 case TPACPI_VIDEO_770:
5147 case TPACPI_VIDEO_NEW:
5148 res = video_autosw_set(1);
5149 if (res)
5150 return res;
5151 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
5152 break;
5153 default:
5154 return -ENOSYS;
5156 if (!autosw && video_autosw_set(autosw)) {
5157 pr_err("video auto-switch left enabled due to error\n");
5158 return -EIO;
5161 return (res) ? 0 : -EIO;
5164 static int video_expand_toggle(void)
5166 switch (video_supported) {
5167 case TPACPI_VIDEO_570:
5168 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
5169 0 : -EIO;
5170 case TPACPI_VIDEO_770:
5171 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
5172 0 : -EIO;
5173 case TPACPI_VIDEO_NEW:
5174 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
5175 0 : -EIO;
5176 default:
5177 return -ENOSYS;
5179 /* not reached */
5182 static int video_read(struct seq_file *m)
5184 int status, autosw;
5186 if (video_supported == TPACPI_VIDEO_NONE) {
5187 seq_printf(m, "status:\t\tnot supported\n");
5188 return 0;
5191 /* Even reads can crash X.org, so... */
5192 if (!capable(CAP_SYS_ADMIN))
5193 return -EPERM;
5195 status = video_outputsw_get();
5196 if (status < 0)
5197 return status;
5199 autosw = video_autosw_get();
5200 if (autosw < 0)
5201 return autosw;
5203 seq_printf(m, "status:\t\tsupported\n");
5204 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
5205 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
5206 if (video_supported == TPACPI_VIDEO_NEW)
5207 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
5208 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5209 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5210 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5211 if (video_supported == TPACPI_VIDEO_NEW)
5212 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5213 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5214 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5216 return 0;
5219 static int video_write(char *buf)
5221 char *cmd;
5222 int enable, disable, status;
5223 int res;
5225 if (video_supported == TPACPI_VIDEO_NONE)
5226 return -ENODEV;
5228 /* Even reads can crash X.org, let alone writes... */
5229 if (!capable(CAP_SYS_ADMIN))
5230 return -EPERM;
5232 enable = 0;
5233 disable = 0;
5235 while ((cmd = next_cmd(&buf))) {
5236 if (strlencmp(cmd, "lcd_enable") == 0) {
5237 enable |= TP_ACPI_VIDEO_S_LCD;
5238 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5239 disable |= TP_ACPI_VIDEO_S_LCD;
5240 } else if (strlencmp(cmd, "crt_enable") == 0) {
5241 enable |= TP_ACPI_VIDEO_S_CRT;
5242 } else if (strlencmp(cmd, "crt_disable") == 0) {
5243 disable |= TP_ACPI_VIDEO_S_CRT;
5244 } else if (video_supported == TPACPI_VIDEO_NEW &&
5245 strlencmp(cmd, "dvi_enable") == 0) {
5246 enable |= TP_ACPI_VIDEO_S_DVI;
5247 } else if (video_supported == TPACPI_VIDEO_NEW &&
5248 strlencmp(cmd, "dvi_disable") == 0) {
5249 disable |= TP_ACPI_VIDEO_S_DVI;
5250 } else if (strlencmp(cmd, "auto_enable") == 0) {
5251 res = video_autosw_set(1);
5252 if (res)
5253 return res;
5254 } else if (strlencmp(cmd, "auto_disable") == 0) {
5255 res = video_autosw_set(0);
5256 if (res)
5257 return res;
5258 } else if (strlencmp(cmd, "video_switch") == 0) {
5259 res = video_outputsw_cycle();
5260 if (res)
5261 return res;
5262 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5263 res = video_expand_toggle();
5264 if (res)
5265 return res;
5266 } else
5267 return -EINVAL;
5270 if (enable || disable) {
5271 status = video_outputsw_get();
5272 if (status < 0)
5273 return status;
5274 res = video_outputsw_set((status & ~disable) | enable);
5275 if (res)
5276 return res;
5279 return 0;
5282 static struct ibm_struct video_driver_data = {
5283 .name = "video",
5284 .read = video_read,
5285 .write = video_write,
5286 .exit = video_exit,
5289 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5291 /*************************************************************************
5292 * Keyboard backlight subdriver
5295 static enum led_brightness kbdlight_brightness;
5296 static DEFINE_MUTEX(kbdlight_mutex);
5298 static int kbdlight_set_level(int level)
5300 int ret = 0;
5302 if (!hkey_handle)
5303 return -ENXIO;
5305 mutex_lock(&kbdlight_mutex);
5307 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5308 ret = -EIO;
5309 else
5310 kbdlight_brightness = level;
5312 mutex_unlock(&kbdlight_mutex);
5314 return ret;
5317 static int kbdlight_get_level(void)
5319 int status = 0;
5321 if (!hkey_handle)
5322 return -ENXIO;
5324 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5325 return -EIO;
5327 if (status < 0)
5328 return status;
5330 return status & 0x3;
5333 static bool kbdlight_is_supported(void)
5335 int status = 0;
5337 if (!hkey_handle)
5338 return false;
5340 if (!acpi_has_method(hkey_handle, "MLCG")) {
5341 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5342 return false;
5345 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5346 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5347 return false;
5350 if (status < 0) {
5351 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5352 return false;
5355 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5357 * Guessed test for keyboard backlight:
5359 * Machines with backlight keyboard return:
5360 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5361 * b110100010010000000XX - ThinkPad x230
5362 * b010100000010000000XX - ThinkPad x240
5363 * b010100000010000000XX - ThinkPad W541
5364 * (XX is current backlight level)
5366 * Machines without backlight keyboard return:
5367 * b10100001000000000000 - ThinkPad x230
5368 * b10110001000000000000 - ThinkPad E430
5369 * b00000000000000000000 - ThinkPad E450
5371 * Candidate BITs for detection test (XOR):
5372 * b01000000001000000000
5375 return status & BIT(9);
5378 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5379 enum led_brightness brightness)
5381 return kbdlight_set_level(brightness);
5384 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5386 int level;
5388 level = kbdlight_get_level();
5389 if (level < 0)
5390 return 0;
5392 return level;
5395 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5396 .led_classdev = {
5397 .name = "tpacpi::kbd_backlight",
5398 .max_brightness = 2,
5399 .flags = LED_BRIGHT_HW_CHANGED,
5400 .brightness_set_blocking = &kbdlight_sysfs_set,
5401 .brightness_get = &kbdlight_sysfs_get,
5405 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5407 int rc;
5409 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5411 TPACPI_ACPIHANDLE_INIT(hkey);
5413 if (!kbdlight_is_supported()) {
5414 tp_features.kbdlight = 0;
5415 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5416 return 1;
5419 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5420 tp_features.kbdlight = 1;
5422 rc = led_classdev_register(&tpacpi_pdev->dev,
5423 &tpacpi_led_kbdlight.led_classdev);
5424 if (rc < 0) {
5425 tp_features.kbdlight = 0;
5426 return rc;
5429 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5430 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5431 return 0;
5434 static void kbdlight_exit(void)
5436 if (tp_features.kbdlight)
5437 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5440 static int kbdlight_set_level_and_update(int level)
5442 int ret;
5443 struct led_classdev *led_cdev;
5445 ret = kbdlight_set_level(level);
5446 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5448 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5449 led_cdev->brightness = level;
5451 return ret;
5454 static int kbdlight_read(struct seq_file *m)
5456 int level;
5458 if (!tp_features.kbdlight) {
5459 seq_printf(m, "status:\t\tnot supported\n");
5460 } else {
5461 level = kbdlight_get_level();
5462 if (level < 0)
5463 seq_printf(m, "status:\t\terror %d\n", level);
5464 else
5465 seq_printf(m, "status:\t\t%d\n", level);
5466 seq_printf(m, "commands:\t0, 1, 2\n");
5469 return 0;
5472 static int kbdlight_write(char *buf)
5474 char *cmd;
5475 int level = -1;
5477 if (!tp_features.kbdlight)
5478 return -ENODEV;
5480 while ((cmd = next_cmd(&buf))) {
5481 if (strlencmp(cmd, "0") == 0)
5482 level = 0;
5483 else if (strlencmp(cmd, "1") == 0)
5484 level = 1;
5485 else if (strlencmp(cmd, "2") == 0)
5486 level = 2;
5487 else
5488 return -EINVAL;
5491 if (level == -1)
5492 return -EINVAL;
5494 return kbdlight_set_level_and_update(level);
5497 static void kbdlight_suspend(void)
5499 struct led_classdev *led_cdev;
5501 if (!tp_features.kbdlight)
5502 return;
5504 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5505 led_update_brightness(led_cdev);
5506 led_classdev_suspend(led_cdev);
5509 static void kbdlight_resume(void)
5511 if (!tp_features.kbdlight)
5512 return;
5514 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5517 static struct ibm_struct kbdlight_driver_data = {
5518 .name = "kbdlight",
5519 .read = kbdlight_read,
5520 .write = kbdlight_write,
5521 .suspend = kbdlight_suspend,
5522 .resume = kbdlight_resume,
5523 .exit = kbdlight_exit,
5526 /*************************************************************************
5527 * Light (thinklight) subdriver
5530 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5531 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5533 static int light_get_status(void)
5535 int status = 0;
5537 if (tp_features.light_status) {
5538 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5539 return -EIO;
5540 return (!!status);
5543 return -ENXIO;
5546 static int light_set_status(int status)
5548 int rc;
5550 if (tp_features.light) {
5551 if (cmos_handle) {
5552 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5553 (status) ?
5554 TP_CMOS_THINKLIGHT_ON :
5555 TP_CMOS_THINKLIGHT_OFF);
5556 } else {
5557 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5558 (status) ? 1 : 0);
5560 return (rc) ? 0 : -EIO;
5563 return -ENXIO;
5566 static int light_sysfs_set(struct led_classdev *led_cdev,
5567 enum led_brightness brightness)
5569 return light_set_status((brightness != LED_OFF) ?
5570 TPACPI_LED_ON : TPACPI_LED_OFF);
5573 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5575 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5578 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5579 .led_classdev = {
5580 .name = "tpacpi::thinklight",
5581 .brightness_set_blocking = &light_sysfs_set,
5582 .brightness_get = &light_sysfs_get,
5586 static int __init light_init(struct ibm_init_struct *iibm)
5588 int rc;
5590 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5592 if (tpacpi_is_ibm()) {
5593 TPACPI_ACPIHANDLE_INIT(ledb);
5594 TPACPI_ACPIHANDLE_INIT(lght);
5596 TPACPI_ACPIHANDLE_INIT(cmos);
5598 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5599 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5601 if (tp_features.light)
5602 /* light status not supported on
5603 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5604 tp_features.light_status =
5605 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5607 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5608 str_supported(tp_features.light),
5609 str_supported(tp_features.light_status));
5611 if (!tp_features.light)
5612 return 1;
5614 rc = led_classdev_register(&tpacpi_pdev->dev,
5615 &tpacpi_led_thinklight.led_classdev);
5617 if (rc < 0) {
5618 tp_features.light = 0;
5619 tp_features.light_status = 0;
5620 } else {
5621 rc = 0;
5624 return rc;
5627 static void light_exit(void)
5629 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5632 static int light_read(struct seq_file *m)
5634 int status;
5636 if (!tp_features.light) {
5637 seq_printf(m, "status:\t\tnot supported\n");
5638 } else if (!tp_features.light_status) {
5639 seq_printf(m, "status:\t\tunknown\n");
5640 seq_printf(m, "commands:\ton, off\n");
5641 } else {
5642 status = light_get_status();
5643 if (status < 0)
5644 return status;
5645 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5646 seq_printf(m, "commands:\ton, off\n");
5649 return 0;
5652 static int light_write(char *buf)
5654 char *cmd;
5655 int newstatus = 0;
5657 if (!tp_features.light)
5658 return -ENODEV;
5660 while ((cmd = next_cmd(&buf))) {
5661 if (strlencmp(cmd, "on") == 0) {
5662 newstatus = 1;
5663 } else if (strlencmp(cmd, "off") == 0) {
5664 newstatus = 0;
5665 } else
5666 return -EINVAL;
5669 return light_set_status(newstatus);
5672 static struct ibm_struct light_driver_data = {
5673 .name = "light",
5674 .read = light_read,
5675 .write = light_write,
5676 .exit = light_exit,
5679 /*************************************************************************
5680 * CMOS subdriver
5683 /* sysfs cmos_command -------------------------------------------------- */
5684 static ssize_t cmos_command_store(struct device *dev,
5685 struct device_attribute *attr,
5686 const char *buf, size_t count)
5688 unsigned long cmos_cmd;
5689 int res;
5691 if (parse_strtoul(buf, 21, &cmos_cmd))
5692 return -EINVAL;
5694 res = issue_thinkpad_cmos_command(cmos_cmd);
5695 return (res) ? res : count;
5698 static DEVICE_ATTR_WO(cmos_command);
5700 /* --------------------------------------------------------------------- */
5702 static int __init cmos_init(struct ibm_init_struct *iibm)
5704 int res;
5706 vdbg_printk(TPACPI_DBG_INIT,
5707 "initializing cmos commands subdriver\n");
5709 TPACPI_ACPIHANDLE_INIT(cmos);
5711 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5712 str_supported(cmos_handle != NULL));
5714 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5715 if (res)
5716 return res;
5718 return (cmos_handle) ? 0 : 1;
5721 static void cmos_exit(void)
5723 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5726 static int cmos_read(struct seq_file *m)
5728 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5729 R30, R31, T20-22, X20-21 */
5730 if (!cmos_handle)
5731 seq_printf(m, "status:\t\tnot supported\n");
5732 else {
5733 seq_printf(m, "status:\t\tsupported\n");
5734 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5737 return 0;
5740 static int cmos_write(char *buf)
5742 char *cmd;
5743 int cmos_cmd, res;
5745 while ((cmd = next_cmd(&buf))) {
5746 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5747 cmos_cmd >= 0 && cmos_cmd <= 21) {
5748 /* cmos_cmd set */
5749 } else
5750 return -EINVAL;
5752 res = issue_thinkpad_cmos_command(cmos_cmd);
5753 if (res)
5754 return res;
5757 return 0;
5760 static struct ibm_struct cmos_driver_data = {
5761 .name = "cmos",
5762 .read = cmos_read,
5763 .write = cmos_write,
5764 .exit = cmos_exit,
5767 /*************************************************************************
5768 * LED subdriver
5771 enum led_access_mode {
5772 TPACPI_LED_NONE = 0,
5773 TPACPI_LED_570, /* 570 */
5774 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5775 TPACPI_LED_NEW, /* all others */
5778 enum { /* For TPACPI_LED_OLD */
5779 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5780 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5781 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5784 static enum led_access_mode led_supported;
5786 static acpi_handle led_handle;
5788 #define TPACPI_LED_NUMLEDS 16
5789 static struct tpacpi_led_classdev *tpacpi_leds;
5790 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5791 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5792 /* there's a limit of 19 chars + NULL before 2.6.26 */
5793 "tpacpi::power",
5794 "tpacpi:orange:batt",
5795 "tpacpi:green:batt",
5796 "tpacpi::dock_active",
5797 "tpacpi::bay_active",
5798 "tpacpi::dock_batt",
5799 "tpacpi::unknown_led",
5800 "tpacpi::standby",
5801 "tpacpi::dock_status1",
5802 "tpacpi::dock_status2",
5803 "tpacpi::unknown_led2",
5804 "tpacpi::unknown_led3",
5805 "tpacpi::thinkvantage",
5807 #define TPACPI_SAFE_LEDS 0x1081U
5809 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5811 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5812 return false;
5813 #else
5814 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5815 #endif
5818 static int led_get_status(const unsigned int led)
5820 int status;
5821 enum led_status_t led_s;
5823 switch (led_supported) {
5824 case TPACPI_LED_570:
5825 if (!acpi_evalf(ec_handle,
5826 &status, "GLED", "dd", 1 << led))
5827 return -EIO;
5828 led_s = (status == 0) ?
5829 TPACPI_LED_OFF :
5830 ((status == 1) ?
5831 TPACPI_LED_ON :
5832 TPACPI_LED_BLINK);
5833 tpacpi_led_state_cache[led] = led_s;
5834 return led_s;
5835 default:
5836 return -ENXIO;
5839 /* not reached */
5842 static int led_set_status(const unsigned int led,
5843 const enum led_status_t ledstatus)
5845 /* off, on, blink. Index is led_status_t */
5846 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5847 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5849 int rc = 0;
5851 switch (led_supported) {
5852 case TPACPI_LED_570:
5853 /* 570 */
5854 if (unlikely(led > 7))
5855 return -EINVAL;
5856 if (unlikely(tpacpi_is_led_restricted(led)))
5857 return -EPERM;
5858 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5859 (1 << led), led_sled_arg1[ledstatus]))
5860 return -EIO;
5861 break;
5862 case TPACPI_LED_OLD:
5863 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5864 if (unlikely(led > 7))
5865 return -EINVAL;
5866 if (unlikely(tpacpi_is_led_restricted(led)))
5867 return -EPERM;
5868 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5869 if (rc >= 0)
5870 rc = ec_write(TPACPI_LED_EC_HLBL,
5871 (ledstatus == TPACPI_LED_BLINK) << led);
5872 if (rc >= 0)
5873 rc = ec_write(TPACPI_LED_EC_HLCL,
5874 (ledstatus != TPACPI_LED_OFF) << led);
5875 break;
5876 case TPACPI_LED_NEW:
5877 /* all others */
5878 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5879 return -EINVAL;
5880 if (unlikely(tpacpi_is_led_restricted(led)))
5881 return -EPERM;
5882 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5883 led, led_led_arg1[ledstatus]))
5884 return -EIO;
5885 break;
5886 default:
5887 return -ENXIO;
5890 if (!rc)
5891 tpacpi_led_state_cache[led] = ledstatus;
5893 return rc;
5896 static int led_sysfs_set(struct led_classdev *led_cdev,
5897 enum led_brightness brightness)
5899 struct tpacpi_led_classdev *data = container_of(led_cdev,
5900 struct tpacpi_led_classdev, led_classdev);
5901 enum led_status_t new_state;
5903 if (brightness == LED_OFF)
5904 new_state = TPACPI_LED_OFF;
5905 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5906 new_state = TPACPI_LED_ON;
5907 else
5908 new_state = TPACPI_LED_BLINK;
5910 return led_set_status(data->led, new_state);
5913 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5914 unsigned long *delay_on, unsigned long *delay_off)
5916 struct tpacpi_led_classdev *data = container_of(led_cdev,
5917 struct tpacpi_led_classdev, led_classdev);
5919 /* Can we choose the flash rate? */
5920 if (*delay_on == 0 && *delay_off == 0) {
5921 /* yes. set them to the hardware blink rate (1 Hz) */
5922 *delay_on = 500; /* ms */
5923 *delay_off = 500; /* ms */
5924 } else if ((*delay_on != 500) || (*delay_off != 500))
5925 return -EINVAL;
5927 return led_set_status(data->led, TPACPI_LED_BLINK);
5930 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5932 int rc;
5934 struct tpacpi_led_classdev *data = container_of(led_cdev,
5935 struct tpacpi_led_classdev, led_classdev);
5937 rc = led_get_status(data->led);
5939 if (rc == TPACPI_LED_OFF || rc < 0)
5940 rc = LED_OFF; /* no error handling in led class :( */
5941 else
5942 rc = LED_FULL;
5944 return rc;
5947 static void led_exit(void)
5949 unsigned int i;
5951 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5952 if (tpacpi_leds[i].led_classdev.name)
5953 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5956 kfree(tpacpi_leds);
5959 static int __init tpacpi_init_led(unsigned int led)
5961 int rc;
5963 tpacpi_leds[led].led = led;
5965 /* LEDs with no name don't get registered */
5966 if (!tpacpi_led_names[led])
5967 return 0;
5969 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5970 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5971 if (led_supported == TPACPI_LED_570)
5972 tpacpi_leds[led].led_classdev.brightness_get =
5973 &led_sysfs_get;
5975 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5977 rc = led_classdev_register(&tpacpi_pdev->dev,
5978 &tpacpi_leds[led].led_classdev);
5979 if (rc < 0)
5980 tpacpi_leds[led].led_classdev.name = NULL;
5982 return rc;
5985 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5986 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5987 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5988 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5990 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5991 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5992 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5993 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5994 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5995 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5996 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5997 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5999 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
6000 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
6001 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
6002 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
6003 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
6005 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
6006 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
6007 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
6008 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
6010 /* (1) - may have excess leds enabled on MSB */
6012 /* Defaults (order matters, keep last, don't reorder!) */
6013 { /* Lenovo */
6014 .vendor = PCI_VENDOR_ID_LENOVO,
6015 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6016 .quirks = 0x1fffU,
6018 { /* IBM ThinkPads with no EC version string */
6019 .vendor = PCI_VENDOR_ID_IBM,
6020 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
6021 .quirks = 0x00ffU,
6023 { /* IBM ThinkPads with EC version string */
6024 .vendor = PCI_VENDOR_ID_IBM,
6025 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
6026 .quirks = 0x00bfU,
6030 static enum led_access_mode __init led_init_detect_mode(void)
6032 acpi_status status;
6034 if (tpacpi_is_ibm()) {
6035 /* 570 */
6036 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
6037 if (ACPI_SUCCESS(status))
6038 return TPACPI_LED_570;
6040 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
6041 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
6042 if (ACPI_SUCCESS(status))
6043 return TPACPI_LED_OLD;
6046 /* most others */
6047 status = acpi_get_handle(ec_handle, "LED", &led_handle);
6048 if (ACPI_SUCCESS(status))
6049 return TPACPI_LED_NEW;
6051 /* R30, R31, and unknown firmwares */
6052 led_handle = NULL;
6053 return TPACPI_LED_NONE;
6056 static int __init led_init(struct ibm_init_struct *iibm)
6058 unsigned int i;
6059 int rc;
6060 unsigned long useful_leds;
6062 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
6064 led_supported = led_init_detect_mode();
6066 if (led_supported != TPACPI_LED_NONE) {
6067 useful_leds = tpacpi_check_quirks(led_useful_qtable,
6068 ARRAY_SIZE(led_useful_qtable));
6070 if (!useful_leds) {
6071 led_handle = NULL;
6072 led_supported = TPACPI_LED_NONE;
6076 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
6077 str_supported(led_supported), led_supported);
6079 if (led_supported == TPACPI_LED_NONE)
6080 return 1;
6082 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds),
6083 GFP_KERNEL);
6084 if (!tpacpi_leds) {
6085 pr_err("Out of memory for LED data\n");
6086 return -ENOMEM;
6089 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
6090 tpacpi_leds[i].led = -1;
6092 if (!tpacpi_is_led_restricted(i) &&
6093 test_bit(i, &useful_leds)) {
6094 rc = tpacpi_init_led(i);
6095 if (rc < 0) {
6096 led_exit();
6097 return rc;
6102 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6103 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6104 #endif
6105 return 0;
6108 #define str_led_status(s) \
6109 ((s) == TPACPI_LED_OFF ? "off" : \
6110 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6112 static int led_read(struct seq_file *m)
6114 if (!led_supported) {
6115 seq_printf(m, "status:\t\tnot supported\n");
6116 return 0;
6118 seq_printf(m, "status:\t\tsupported\n");
6120 if (led_supported == TPACPI_LED_570) {
6121 /* 570 */
6122 int i, status;
6123 for (i = 0; i < 8; i++) {
6124 status = led_get_status(i);
6125 if (status < 0)
6126 return -EIO;
6127 seq_printf(m, "%d:\t\t%s\n",
6128 i, str_led_status(status));
6132 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6134 return 0;
6137 static int led_write(char *buf)
6139 char *cmd;
6140 int led, rc;
6141 enum led_status_t s;
6143 if (!led_supported)
6144 return -ENODEV;
6146 while ((cmd = next_cmd(&buf))) {
6147 if (sscanf(cmd, "%d", &led) != 1)
6148 return -EINVAL;
6150 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
6151 tpacpi_leds[led].led < 0)
6152 return -ENODEV;
6154 if (strstr(cmd, "off")) {
6155 s = TPACPI_LED_OFF;
6156 } else if (strstr(cmd, "on")) {
6157 s = TPACPI_LED_ON;
6158 } else if (strstr(cmd, "blink")) {
6159 s = TPACPI_LED_BLINK;
6160 } else {
6161 return -EINVAL;
6164 rc = led_set_status(led, s);
6165 if (rc < 0)
6166 return rc;
6169 return 0;
6172 static struct ibm_struct led_driver_data = {
6173 .name = "led",
6174 .read = led_read,
6175 .write = led_write,
6176 .exit = led_exit,
6179 /*************************************************************************
6180 * Beep subdriver
6183 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
6185 #define TPACPI_BEEP_Q1 0x0001
6187 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
6188 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
6189 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
6192 static int __init beep_init(struct ibm_init_struct *iibm)
6194 unsigned long quirks;
6196 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
6198 TPACPI_ACPIHANDLE_INIT(beep);
6200 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
6201 str_supported(beep_handle != NULL));
6203 quirks = tpacpi_check_quirks(beep_quirk_table,
6204 ARRAY_SIZE(beep_quirk_table));
6206 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6208 return (beep_handle) ? 0 : 1;
6211 static int beep_read(struct seq_file *m)
6213 if (!beep_handle)
6214 seq_printf(m, "status:\t\tnot supported\n");
6215 else {
6216 seq_printf(m, "status:\t\tsupported\n");
6217 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6220 return 0;
6223 static int beep_write(char *buf)
6225 char *cmd;
6226 int beep_cmd;
6228 if (!beep_handle)
6229 return -ENODEV;
6231 while ((cmd = next_cmd(&buf))) {
6232 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6233 beep_cmd >= 0 && beep_cmd <= 17) {
6234 /* beep_cmd set */
6235 } else
6236 return -EINVAL;
6237 if (tp_features.beep_needs_two_args) {
6238 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6239 beep_cmd, 0))
6240 return -EIO;
6241 } else {
6242 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6243 beep_cmd))
6244 return -EIO;
6248 return 0;
6251 static struct ibm_struct beep_driver_data = {
6252 .name = "beep",
6253 .read = beep_read,
6254 .write = beep_write,
6257 /*************************************************************************
6258 * Thermal subdriver
6261 enum thermal_access_mode {
6262 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6263 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6264 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6265 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6266 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6269 enum { /* TPACPI_THERMAL_TPEC_* */
6270 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6271 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6272 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6274 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6278 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6279 struct ibm_thermal_sensors_struct {
6280 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6283 static enum thermal_access_mode thermal_read_mode;
6285 /* idx is zero-based */
6286 static int thermal_get_sensor(int idx, s32 *value)
6288 int t;
6289 s8 tmp;
6290 char tmpi[5];
6292 t = TP_EC_THERMAL_TMP0;
6294 switch (thermal_read_mode) {
6295 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6296 case TPACPI_THERMAL_TPEC_16:
6297 if (idx >= 8 && idx <= 15) {
6298 t = TP_EC_THERMAL_TMP8;
6299 idx -= 8;
6301 #endif
6302 /* fallthrough */
6303 case TPACPI_THERMAL_TPEC_8:
6304 if (idx <= 7) {
6305 if (!acpi_ec_read(t + idx, &tmp))
6306 return -EIO;
6307 *value = tmp * 1000;
6308 return 0;
6310 break;
6312 case TPACPI_THERMAL_ACPI_UPDT:
6313 if (idx <= 7) {
6314 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6315 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6316 return -EIO;
6317 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6318 return -EIO;
6319 *value = (t - 2732) * 100;
6320 return 0;
6322 break;
6324 case TPACPI_THERMAL_ACPI_TMP07:
6325 if (idx <= 7) {
6326 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6327 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6328 return -EIO;
6329 if (t > 127 || t < -127)
6330 t = TP_EC_THERMAL_TMP_NA;
6331 *value = t * 1000;
6332 return 0;
6334 break;
6336 case TPACPI_THERMAL_NONE:
6337 default:
6338 return -ENOSYS;
6341 return -EINVAL;
6344 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6346 int res, i;
6347 int n;
6349 n = 8;
6350 i = 0;
6352 if (!s)
6353 return -EINVAL;
6355 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6356 n = 16;
6358 for (i = 0 ; i < n; i++) {
6359 res = thermal_get_sensor(i, &s->temp[i]);
6360 if (res)
6361 return res;
6364 return n;
6367 static void thermal_dump_all_sensors(void)
6369 int n, i;
6370 struct ibm_thermal_sensors_struct t;
6372 n = thermal_get_sensors(&t);
6373 if (n <= 0)
6374 return;
6376 pr_notice("temperatures (Celsius):");
6378 for (i = 0; i < n; i++) {
6379 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6380 pr_cont(" %d", (int)(t.temp[i] / 1000));
6381 else
6382 pr_cont(" N/A");
6385 pr_cont("\n");
6388 /* sysfs temp##_input -------------------------------------------------- */
6390 static ssize_t thermal_temp_input_show(struct device *dev,
6391 struct device_attribute *attr,
6392 char *buf)
6394 struct sensor_device_attribute *sensor_attr =
6395 to_sensor_dev_attr(attr);
6396 int idx = sensor_attr->index;
6397 s32 value;
6398 int res;
6400 res = thermal_get_sensor(idx, &value);
6401 if (res)
6402 return res;
6403 if (value == TPACPI_THERMAL_SENSOR_NA)
6404 return -ENXIO;
6406 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6409 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6410 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6411 thermal_temp_input_show, NULL, _idxB)
6413 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6414 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6415 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6416 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6417 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6418 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6419 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6420 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6421 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6422 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6423 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6424 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6425 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6426 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6427 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6428 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6429 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6432 #define THERMAL_ATTRS(X) \
6433 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6435 static struct attribute *thermal_temp_input_attr[] = {
6436 THERMAL_ATTRS(8),
6437 THERMAL_ATTRS(9),
6438 THERMAL_ATTRS(10),
6439 THERMAL_ATTRS(11),
6440 THERMAL_ATTRS(12),
6441 THERMAL_ATTRS(13),
6442 THERMAL_ATTRS(14),
6443 THERMAL_ATTRS(15),
6444 THERMAL_ATTRS(0),
6445 THERMAL_ATTRS(1),
6446 THERMAL_ATTRS(2),
6447 THERMAL_ATTRS(3),
6448 THERMAL_ATTRS(4),
6449 THERMAL_ATTRS(5),
6450 THERMAL_ATTRS(6),
6451 THERMAL_ATTRS(7),
6452 NULL
6455 static const struct attribute_group thermal_temp_input16_group = {
6456 .attrs = thermal_temp_input_attr
6459 static const struct attribute_group thermal_temp_input8_group = {
6460 .attrs = &thermal_temp_input_attr[8]
6463 #undef THERMAL_SENSOR_ATTR_TEMP
6464 #undef THERMAL_ATTRS
6466 /* --------------------------------------------------------------------- */
6468 static int __init thermal_init(struct ibm_init_struct *iibm)
6470 u8 t, ta1, ta2;
6471 int i;
6472 int acpi_tmp7;
6473 int res;
6475 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6477 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6479 if (thinkpad_id.ec_model) {
6481 * Direct EC access mode: sensors at registers
6482 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6483 * non-implemented, thermal sensors return 0x80 when
6484 * not available
6487 ta1 = ta2 = 0;
6488 for (i = 0; i < 8; i++) {
6489 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6490 ta1 |= t;
6491 } else {
6492 ta1 = 0;
6493 break;
6495 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6496 ta2 |= t;
6497 } else {
6498 ta1 = 0;
6499 break;
6502 if (ta1 == 0) {
6503 /* This is sheer paranoia, but we handle it anyway */
6504 if (acpi_tmp7) {
6505 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6506 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6507 } else {
6508 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6509 thermal_read_mode = TPACPI_THERMAL_NONE;
6511 } else {
6512 thermal_read_mode =
6513 (ta2 != 0) ?
6514 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6516 } else if (acpi_tmp7) {
6517 if (tpacpi_is_ibm() &&
6518 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6519 /* 600e/x, 770e, 770x */
6520 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6521 } else {
6522 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6523 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6525 } else {
6526 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6527 thermal_read_mode = TPACPI_THERMAL_NONE;
6530 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6531 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6532 thermal_read_mode);
6534 switch (thermal_read_mode) {
6535 case TPACPI_THERMAL_TPEC_16:
6536 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6537 &thermal_temp_input16_group);
6538 if (res)
6539 return res;
6540 break;
6541 case TPACPI_THERMAL_TPEC_8:
6542 case TPACPI_THERMAL_ACPI_TMP07:
6543 case TPACPI_THERMAL_ACPI_UPDT:
6544 res = sysfs_create_group(&tpacpi_hwmon->kobj,
6545 &thermal_temp_input8_group);
6546 if (res)
6547 return res;
6548 break;
6549 case TPACPI_THERMAL_NONE:
6550 default:
6551 return 1;
6554 return 0;
6557 static void thermal_exit(void)
6559 switch (thermal_read_mode) {
6560 case TPACPI_THERMAL_TPEC_16:
6561 sysfs_remove_group(&tpacpi_hwmon->kobj,
6562 &thermal_temp_input16_group);
6563 break;
6564 case TPACPI_THERMAL_TPEC_8:
6565 case TPACPI_THERMAL_ACPI_TMP07:
6566 case TPACPI_THERMAL_ACPI_UPDT:
6567 sysfs_remove_group(&tpacpi_hwmon->kobj,
6568 &thermal_temp_input8_group);
6569 break;
6570 case TPACPI_THERMAL_NONE:
6571 default:
6572 break;
6576 static int thermal_read(struct seq_file *m)
6578 int n, i;
6579 struct ibm_thermal_sensors_struct t;
6581 n = thermal_get_sensors(&t);
6582 if (unlikely(n < 0))
6583 return n;
6585 seq_printf(m, "temperatures:\t");
6587 if (n > 0) {
6588 for (i = 0; i < (n - 1); i++)
6589 seq_printf(m, "%d ", t.temp[i] / 1000);
6590 seq_printf(m, "%d\n", t.temp[i] / 1000);
6591 } else
6592 seq_printf(m, "not supported\n");
6594 return 0;
6597 static struct ibm_struct thermal_driver_data = {
6598 .name = "thermal",
6599 .read = thermal_read,
6600 .exit = thermal_exit,
6603 /*************************************************************************
6604 * Backlight/brightness subdriver
6607 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6610 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6611 * CMOS NVRAM byte 0x5E, bits 0-3.
6613 * EC HBRV (0x31) has the following layout
6614 * Bit 7: unknown function
6615 * Bit 6: unknown function
6616 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6617 * Bit 4: must be set to zero to avoid problems
6618 * Bit 3-0: backlight brightness level
6620 * brightness_get_raw returns status data in the HBRV layout
6622 * WARNING: The X61 has been verified to use HBRV for something else, so
6623 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6624 * testing on the very early *60 Lenovo models...
6627 enum {
6628 TP_EC_BACKLIGHT = 0x31,
6630 /* TP_EC_BACKLIGHT bitmasks */
6631 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6632 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6633 TP_EC_BACKLIGHT_MAPSW = 0x20,
6636 enum tpacpi_brightness_access_mode {
6637 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6638 TPACPI_BRGHT_MODE_EC, /* EC control */
6639 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6640 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6641 TPACPI_BRGHT_MODE_MAX
6644 static struct backlight_device *ibm_backlight_device;
6646 static enum tpacpi_brightness_access_mode brightness_mode =
6647 TPACPI_BRGHT_MODE_MAX;
6649 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6651 static struct mutex brightness_mutex;
6653 /* NVRAM brightness access,
6654 * call with brightness_mutex held! */
6655 static unsigned int tpacpi_brightness_nvram_get(void)
6657 u8 lnvram;
6659 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6660 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6661 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6662 lnvram &= bright_maxlvl;
6664 return lnvram;
6667 static void tpacpi_brightness_checkpoint_nvram(void)
6669 u8 lec = 0;
6670 u8 b_nvram;
6672 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6673 return;
6675 vdbg_printk(TPACPI_DBG_BRGHT,
6676 "trying to checkpoint backlight level to NVRAM...\n");
6678 if (mutex_lock_killable(&brightness_mutex) < 0)
6679 return;
6681 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6682 goto unlock;
6683 lec &= TP_EC_BACKLIGHT_LVLMSK;
6684 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6686 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6687 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6688 /* NVRAM needs update */
6689 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6690 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6691 b_nvram |= lec;
6692 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6693 dbg_printk(TPACPI_DBG_BRGHT,
6694 "updated NVRAM backlight level to %u (0x%02x)\n",
6695 (unsigned int) lec, (unsigned int) b_nvram);
6696 } else
6697 vdbg_printk(TPACPI_DBG_BRGHT,
6698 "NVRAM backlight level already is %u (0x%02x)\n",
6699 (unsigned int) lec, (unsigned int) b_nvram);
6701 unlock:
6702 mutex_unlock(&brightness_mutex);
6706 /* call with brightness_mutex held! */
6707 static int tpacpi_brightness_get_raw(int *status)
6709 u8 lec = 0;
6711 switch (brightness_mode) {
6712 case TPACPI_BRGHT_MODE_UCMS_STEP:
6713 *status = tpacpi_brightness_nvram_get();
6714 return 0;
6715 case TPACPI_BRGHT_MODE_EC:
6716 case TPACPI_BRGHT_MODE_ECNVRAM:
6717 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6718 return -EIO;
6719 *status = lec;
6720 return 0;
6721 default:
6722 return -ENXIO;
6726 /* call with brightness_mutex held! */
6727 /* do NOT call with illegal backlight level value */
6728 static int tpacpi_brightness_set_ec(unsigned int value)
6730 u8 lec = 0;
6732 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6733 return -EIO;
6735 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6736 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6737 (value & TP_EC_BACKLIGHT_LVLMSK))))
6738 return -EIO;
6740 return 0;
6743 /* call with brightness_mutex held! */
6744 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6746 int cmos_cmd, inc;
6747 unsigned int current_value, i;
6749 current_value = tpacpi_brightness_nvram_get();
6751 if (value == current_value)
6752 return 0;
6754 cmos_cmd = (value > current_value) ?
6755 TP_CMOS_BRIGHTNESS_UP :
6756 TP_CMOS_BRIGHTNESS_DOWN;
6757 inc = (value > current_value) ? 1 : -1;
6759 for (i = current_value; i != value; i += inc)
6760 if (issue_thinkpad_cmos_command(cmos_cmd))
6761 return -EIO;
6763 return 0;
6766 /* May return EINTR which can always be mapped to ERESTARTSYS */
6767 static int brightness_set(unsigned int value)
6769 int res;
6771 if (value > bright_maxlvl)
6772 return -EINVAL;
6774 vdbg_printk(TPACPI_DBG_BRGHT,
6775 "set backlight level to %d\n", value);
6777 res = mutex_lock_killable(&brightness_mutex);
6778 if (res < 0)
6779 return res;
6781 switch (brightness_mode) {
6782 case TPACPI_BRGHT_MODE_EC:
6783 case TPACPI_BRGHT_MODE_ECNVRAM:
6784 res = tpacpi_brightness_set_ec(value);
6785 break;
6786 case TPACPI_BRGHT_MODE_UCMS_STEP:
6787 res = tpacpi_brightness_set_ucmsstep(value);
6788 break;
6789 default:
6790 res = -ENXIO;
6793 mutex_unlock(&brightness_mutex);
6794 return res;
6797 /* sysfs backlight class ----------------------------------------------- */
6799 static int brightness_update_status(struct backlight_device *bd)
6801 unsigned int level =
6802 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6803 bd->props.power == FB_BLANK_UNBLANK) ?
6804 bd->props.brightness : 0;
6806 dbg_printk(TPACPI_DBG_BRGHT,
6807 "backlight: attempt to set level to %d\n",
6808 level);
6810 /* it is the backlight class's job (caller) to handle
6811 * EINTR and other errors properly */
6812 return brightness_set(level);
6815 static int brightness_get(struct backlight_device *bd)
6817 int status, res;
6819 res = mutex_lock_killable(&brightness_mutex);
6820 if (res < 0)
6821 return 0;
6823 res = tpacpi_brightness_get_raw(&status);
6825 mutex_unlock(&brightness_mutex);
6827 if (res < 0)
6828 return 0;
6830 return status & TP_EC_BACKLIGHT_LVLMSK;
6833 static void tpacpi_brightness_notify_change(void)
6835 backlight_force_update(ibm_backlight_device,
6836 BACKLIGHT_UPDATE_HOTKEY);
6839 static const struct backlight_ops ibm_backlight_data = {
6840 .get_brightness = brightness_get,
6841 .update_status = brightness_update_status,
6844 /* --------------------------------------------------------------------- */
6847 * Call _BCL method of video device. On some ThinkPads this will
6848 * switch the firmware to the ACPI brightness control mode.
6851 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6853 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6854 union acpi_object *obj;
6855 struct acpi_device *device, *child;
6856 int rc;
6858 if (acpi_bus_get_device(handle, &device))
6859 return 0;
6861 rc = 0;
6862 list_for_each_entry(child, &device->children, node) {
6863 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6864 NULL, &buffer);
6865 if (ACPI_FAILURE(status))
6866 continue;
6868 obj = (union acpi_object *)buffer.pointer;
6869 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6870 pr_err("Unknown _BCL data, please report this to %s\n",
6871 TPACPI_MAIL);
6872 rc = 0;
6873 } else {
6874 rc = obj->package.count;
6876 break;
6879 kfree(buffer.pointer);
6880 return rc;
6885 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6887 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6889 acpi_handle video_device;
6890 int bcl_levels = 0;
6892 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6893 if (video_device)
6894 bcl_levels = tpacpi_query_bcl_levels(video_device);
6896 tp_features.bright_acpimode = (bcl_levels > 0);
6898 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6902 * These are only useful for models that have only one possibility
6903 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6904 * these quirks.
6906 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6907 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6908 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6910 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6911 /* Models with ATI GPUs known to require ECNVRAM mode */
6912 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6914 /* Models with ATI GPUs that can use ECNVRAM */
6915 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6916 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6917 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6918 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6920 /* Models with Intel Extreme Graphics 2 */
6921 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6922 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6923 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6925 /* Models with Intel GMA900 */
6926 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6927 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6928 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6932 * Returns < 0 for error, otherwise sets tp_features.bright_*
6933 * and bright_maxlvl.
6935 static void __init tpacpi_detect_brightness_capabilities(void)
6937 unsigned int b;
6939 vdbg_printk(TPACPI_DBG_INIT,
6940 "detecting firmware brightness interface capabilities\n");
6942 /* we could run a quirks check here (same table used by
6943 * brightness_init) if needed */
6946 * We always attempt to detect acpi support, so as to switch
6947 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6948 * going to publish a backlight interface
6950 b = tpacpi_check_std_acpi_brightness_support();
6951 switch (b) {
6952 case 16:
6953 bright_maxlvl = 15;
6954 break;
6955 case 8:
6956 case 0:
6957 bright_maxlvl = 7;
6958 break;
6959 default:
6960 tp_features.bright_unkfw = 1;
6961 bright_maxlvl = b - 1;
6963 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6966 static int __init brightness_init(struct ibm_init_struct *iibm)
6968 struct backlight_properties props;
6969 int b;
6970 unsigned long quirks;
6972 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6974 mutex_init(&brightness_mutex);
6976 quirks = tpacpi_check_quirks(brightness_quirk_table,
6977 ARRAY_SIZE(brightness_quirk_table));
6979 /* tpacpi_detect_brightness_capabilities() must have run already */
6981 /* if it is unknown, we don't handle it: it wouldn't be safe */
6982 if (tp_features.bright_unkfw)
6983 return 1;
6985 if (!brightness_enable) {
6986 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6987 "brightness support disabled by module parameter\n");
6988 return 1;
6991 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6992 if (brightness_enable > 1) {
6993 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6994 return 1;
6995 } else if (brightness_enable == 1) {
6996 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6997 return 1;
6999 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
7000 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
7004 * Check for module parameter bogosity, note that we
7005 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
7006 * able to detect "unspecified"
7008 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
7009 return -EINVAL;
7011 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
7012 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
7013 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
7014 if (quirks & TPACPI_BRGHT_Q_EC)
7015 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
7016 else
7017 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
7019 dbg_printk(TPACPI_DBG_BRGHT,
7020 "driver auto-selected brightness_mode=%d\n",
7021 brightness_mode);
7024 /* Safety */
7025 if (!tpacpi_is_ibm() &&
7026 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
7027 brightness_mode == TPACPI_BRGHT_MODE_EC))
7028 return -EINVAL;
7030 if (tpacpi_brightness_get_raw(&b) < 0)
7031 return 1;
7033 memset(&props, 0, sizeof(struct backlight_properties));
7034 props.type = BACKLIGHT_PLATFORM;
7035 props.max_brightness = bright_maxlvl;
7036 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
7037 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
7038 NULL, NULL,
7039 &ibm_backlight_data,
7040 &props);
7041 if (IS_ERR(ibm_backlight_device)) {
7042 int rc = PTR_ERR(ibm_backlight_device);
7043 ibm_backlight_device = NULL;
7044 pr_err("Could not register backlight device\n");
7045 return rc;
7047 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7048 "brightness is supported\n");
7050 if (quirks & TPACPI_BRGHT_Q_ASK) {
7051 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
7052 brightness_mode);
7053 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
7054 TPACPI_MAIL);
7057 /* Added by mistake in early 2007. Probably useless, but it could
7058 * be working around some unknown firmware problem where the value
7059 * read at startup doesn't match the real hardware state... so leave
7060 * it in place just in case */
7061 backlight_update_status(ibm_backlight_device);
7063 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
7064 "brightness: registering brightness hotkeys as change notification\n");
7065 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7066 | TP_ACPI_HKEY_BRGHTUP_MASK
7067 | TP_ACPI_HKEY_BRGHTDWN_MASK);
7068 return 0;
7071 static void brightness_suspend(void)
7073 tpacpi_brightness_checkpoint_nvram();
7076 static void brightness_shutdown(void)
7078 tpacpi_brightness_checkpoint_nvram();
7081 static void brightness_exit(void)
7083 if (ibm_backlight_device) {
7084 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
7085 "calling backlight_device_unregister()\n");
7086 backlight_device_unregister(ibm_backlight_device);
7089 tpacpi_brightness_checkpoint_nvram();
7092 static int brightness_read(struct seq_file *m)
7094 int level;
7096 level = brightness_get(NULL);
7097 if (level < 0) {
7098 seq_printf(m, "level:\t\tunreadable\n");
7099 } else {
7100 seq_printf(m, "level:\t\t%d\n", level);
7101 seq_printf(m, "commands:\tup, down\n");
7102 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7103 bright_maxlvl);
7106 return 0;
7109 static int brightness_write(char *buf)
7111 int level;
7112 int rc;
7113 char *cmd;
7115 level = brightness_get(NULL);
7116 if (level < 0)
7117 return level;
7119 while ((cmd = next_cmd(&buf))) {
7120 if (strlencmp(cmd, "up") == 0) {
7121 if (level < bright_maxlvl)
7122 level++;
7123 } else if (strlencmp(cmd, "down") == 0) {
7124 if (level > 0)
7125 level--;
7126 } else if (sscanf(cmd, "level %d", &level) == 1 &&
7127 level >= 0 && level <= bright_maxlvl) {
7128 /* new level set */
7129 } else
7130 return -EINVAL;
7133 tpacpi_disclose_usertask("procfs brightness",
7134 "set level to %d\n", level);
7137 * Now we know what the final level should be, so we try to set it.
7138 * Doing it this way makes the syscall restartable in case of EINTR
7140 rc = brightness_set(level);
7141 if (!rc && ibm_backlight_device)
7142 backlight_force_update(ibm_backlight_device,
7143 BACKLIGHT_UPDATE_SYSFS);
7144 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7147 static struct ibm_struct brightness_driver_data = {
7148 .name = "brightness",
7149 .read = brightness_read,
7150 .write = brightness_write,
7151 .exit = brightness_exit,
7152 .suspend = brightness_suspend,
7153 .shutdown = brightness_shutdown,
7156 /*************************************************************************
7157 * Volume subdriver
7161 * IBM ThinkPads have a simple volume controller with MUTE gating.
7162 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7164 * Since the *61 series (and probably also the later *60 series), Lenovo
7165 * ThinkPads only implement the MUTE gate.
7167 * EC register 0x30
7168 * Bit 6: MUTE (1 mutes sound)
7169 * Bit 3-0: Volume
7170 * Other bits should be zero as far as we know.
7172 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7173 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7174 * such as bit 7 which is used to detect repeated presses of MUTE,
7175 * and we leave them unchanged.
7177 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7178 * in response to user input. Unfortunately, this rarely works well.
7179 * The laptop changes the state of its internal MUTE gate and, on some
7180 * models, sends KEY_MUTE, causing any user code that responds to the
7181 * mute button to get confused. The hardware MUTE gate is also
7182 * unnecessary, since user code can handle the mute button without
7183 * kernel or EC help.
7185 * To avoid confusing userspace, we simply disable all EC-based mute
7186 * and volume controls when possible.
7189 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7191 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7192 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7193 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7195 #if SNDRV_CARDS <= 32
7196 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7197 #else
7198 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7199 #endif
7200 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7201 static char *alsa_id = "ThinkPadEC";
7202 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7204 struct tpacpi_alsa_data {
7205 struct snd_card *card;
7206 struct snd_ctl_elem_id *ctl_mute_id;
7207 struct snd_ctl_elem_id *ctl_vol_id;
7210 static struct snd_card *alsa_card;
7212 enum {
7213 TP_EC_AUDIO = 0x30,
7215 /* TP_EC_AUDIO bits */
7216 TP_EC_AUDIO_MUTESW = 6,
7218 /* TP_EC_AUDIO bitmasks */
7219 TP_EC_AUDIO_LVL_MSK = 0x0F,
7220 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7222 /* Maximum volume */
7223 TP_EC_VOLUME_MAX = 14,
7226 enum tpacpi_volume_access_mode {
7227 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7228 TPACPI_VOL_MODE_EC, /* Pure EC control */
7229 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7230 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7231 TPACPI_VOL_MODE_MAX
7234 enum tpacpi_volume_capabilities {
7235 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7236 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7237 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7238 TPACPI_VOL_CAP_MAX
7241 enum tpacpi_mute_btn_mode {
7242 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7243 /* We don't know what mode 1 is. */
7244 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7245 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7248 static enum tpacpi_volume_access_mode volume_mode =
7249 TPACPI_VOL_MODE_MAX;
7251 static enum tpacpi_volume_capabilities volume_capabilities;
7252 static bool volume_control_allowed;
7253 static bool software_mute_requested = true;
7254 static bool software_mute_active;
7255 static int software_mute_orig_mode;
7258 * Used to syncronize writers to TP_EC_AUDIO and
7259 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7261 static struct mutex volume_mutex;
7263 static void tpacpi_volume_checkpoint_nvram(void)
7265 u8 lec = 0;
7266 u8 b_nvram;
7267 u8 ec_mask;
7269 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7270 return;
7271 if (!volume_control_allowed)
7272 return;
7273 if (software_mute_active)
7274 return;
7276 vdbg_printk(TPACPI_DBG_MIXER,
7277 "trying to checkpoint mixer state to NVRAM...\n");
7279 if (tp_features.mixer_no_level_control)
7280 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7281 else
7282 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7284 if (mutex_lock_killable(&volume_mutex) < 0)
7285 return;
7287 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7288 goto unlock;
7289 lec &= ec_mask;
7290 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7292 if (lec != (b_nvram & ec_mask)) {
7293 /* NVRAM needs update */
7294 b_nvram &= ~ec_mask;
7295 b_nvram |= lec;
7296 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7297 dbg_printk(TPACPI_DBG_MIXER,
7298 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7299 (unsigned int) lec, (unsigned int) b_nvram);
7300 } else {
7301 vdbg_printk(TPACPI_DBG_MIXER,
7302 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7303 (unsigned int) lec, (unsigned int) b_nvram);
7306 unlock:
7307 mutex_unlock(&volume_mutex);
7310 static int volume_get_status_ec(u8 *status)
7312 u8 s;
7314 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7315 return -EIO;
7317 *status = s;
7319 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7321 return 0;
7324 static int volume_get_status(u8 *status)
7326 return volume_get_status_ec(status);
7329 static int volume_set_status_ec(const u8 status)
7331 if (!acpi_ec_write(TP_EC_AUDIO, status))
7332 return -EIO;
7334 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7337 * On X200s, and possibly on others, it can take a while for
7338 * reads to become correct.
7340 msleep(1);
7342 return 0;
7345 static int volume_set_status(const u8 status)
7347 return volume_set_status_ec(status);
7350 /* returns < 0 on error, 0 on no change, 1 on change */
7351 static int __volume_set_mute_ec(const bool mute)
7353 int rc;
7354 u8 s, n;
7356 if (mutex_lock_killable(&volume_mutex) < 0)
7357 return -EINTR;
7359 rc = volume_get_status_ec(&s);
7360 if (rc)
7361 goto unlock;
7363 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7364 s & ~TP_EC_AUDIO_MUTESW_MSK;
7366 if (n != s) {
7367 rc = volume_set_status_ec(n);
7368 if (!rc)
7369 rc = 1;
7372 unlock:
7373 mutex_unlock(&volume_mutex);
7374 return rc;
7377 static int volume_alsa_set_mute(const bool mute)
7379 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7380 (mute) ? "" : "un");
7381 return __volume_set_mute_ec(mute);
7384 static int volume_set_mute(const bool mute)
7386 int rc;
7388 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7389 (mute) ? "" : "un");
7391 rc = __volume_set_mute_ec(mute);
7392 return (rc < 0) ? rc : 0;
7395 /* returns < 0 on error, 0 on no change, 1 on change */
7396 static int __volume_set_volume_ec(const u8 vol)
7398 int rc;
7399 u8 s, n;
7401 if (vol > TP_EC_VOLUME_MAX)
7402 return -EINVAL;
7404 if (mutex_lock_killable(&volume_mutex) < 0)
7405 return -EINTR;
7407 rc = volume_get_status_ec(&s);
7408 if (rc)
7409 goto unlock;
7411 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7413 if (n != s) {
7414 rc = volume_set_status_ec(n);
7415 if (!rc)
7416 rc = 1;
7419 unlock:
7420 mutex_unlock(&volume_mutex);
7421 return rc;
7424 static int volume_set_software_mute(bool startup)
7426 int result;
7428 if (!tpacpi_is_lenovo())
7429 return -ENODEV;
7431 if (startup) {
7432 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7433 "HAUM", "qd"))
7434 return -EIO;
7436 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7437 "Initial HAUM setting was %d\n",
7438 software_mute_orig_mode);
7441 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7442 (int)TP_EC_MUTE_BTN_NONE))
7443 return -EIO;
7445 if (result != TP_EC_MUTE_BTN_NONE)
7446 pr_warn("Unexpected SAUM result %d\n",
7447 result);
7450 * In software mute mode, the standard codec controls take
7451 * precendence, so we unmute the ThinkPad HW switch at
7452 * startup. Just on case there are SAUM-capable ThinkPads
7453 * with level controls, set max HW volume as well.
7455 if (tp_features.mixer_no_level_control)
7456 result = volume_set_mute(false);
7457 else
7458 result = volume_set_status(TP_EC_VOLUME_MAX);
7460 if (result != 0)
7461 pr_warn("Failed to unmute the HW mute switch\n");
7463 return 0;
7466 static void volume_exit_software_mute(void)
7468 int r;
7470 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7471 || r != software_mute_orig_mode)
7472 pr_warn("Failed to restore mute mode\n");
7475 static int volume_alsa_set_volume(const u8 vol)
7477 dbg_printk(TPACPI_DBG_MIXER,
7478 "ALSA: trying to set volume level to %hu\n", vol);
7479 return __volume_set_volume_ec(vol);
7482 static void volume_alsa_notify_change(void)
7484 struct tpacpi_alsa_data *d;
7486 if (alsa_card && alsa_card->private_data) {
7487 d = alsa_card->private_data;
7488 if (d->ctl_mute_id)
7489 snd_ctl_notify(alsa_card,
7490 SNDRV_CTL_EVENT_MASK_VALUE,
7491 d->ctl_mute_id);
7492 if (d->ctl_vol_id)
7493 snd_ctl_notify(alsa_card,
7494 SNDRV_CTL_EVENT_MASK_VALUE,
7495 d->ctl_vol_id);
7499 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7500 struct snd_ctl_elem_info *uinfo)
7502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7503 uinfo->count = 1;
7504 uinfo->value.integer.min = 0;
7505 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7506 return 0;
7509 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7510 struct snd_ctl_elem_value *ucontrol)
7512 u8 s;
7513 int rc;
7515 rc = volume_get_status(&s);
7516 if (rc < 0)
7517 return rc;
7519 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7520 return 0;
7523 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7524 struct snd_ctl_elem_value *ucontrol)
7526 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7527 ucontrol->value.integer.value[0]);
7528 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7531 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7533 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7534 struct snd_ctl_elem_value *ucontrol)
7536 u8 s;
7537 int rc;
7539 rc = volume_get_status(&s);
7540 if (rc < 0)
7541 return rc;
7543 ucontrol->value.integer.value[0] =
7544 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7545 return 0;
7548 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7549 struct snd_ctl_elem_value *ucontrol)
7551 tpacpi_disclose_usertask("ALSA", "%smute\n",
7552 ucontrol->value.integer.value[0] ?
7553 "un" : "");
7554 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7557 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7559 .name = "Console Playback Volume",
7560 .index = 0,
7561 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7562 .info = volume_alsa_vol_info,
7563 .get = volume_alsa_vol_get,
7566 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7567 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7568 .name = "Console Playback Switch",
7569 .index = 0,
7570 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7571 .info = volume_alsa_mute_info,
7572 .get = volume_alsa_mute_get,
7575 static void volume_suspend(void)
7577 tpacpi_volume_checkpoint_nvram();
7580 static void volume_resume(void)
7582 if (software_mute_active) {
7583 if (volume_set_software_mute(false) < 0)
7584 pr_warn("Failed to restore software mute\n");
7585 } else {
7586 volume_alsa_notify_change();
7590 static void volume_shutdown(void)
7592 tpacpi_volume_checkpoint_nvram();
7595 static void volume_exit(void)
7597 if (alsa_card) {
7598 snd_card_free(alsa_card);
7599 alsa_card = NULL;
7602 tpacpi_volume_checkpoint_nvram();
7604 if (software_mute_active)
7605 volume_exit_software_mute();
7608 static int __init volume_create_alsa_mixer(void)
7610 struct snd_card *card;
7611 struct tpacpi_alsa_data *data;
7612 struct snd_kcontrol *ctl_vol;
7613 struct snd_kcontrol *ctl_mute;
7614 int rc;
7616 rc = snd_card_new(&tpacpi_pdev->dev,
7617 alsa_index, alsa_id, THIS_MODULE,
7618 sizeof(struct tpacpi_alsa_data), &card);
7619 if (rc < 0 || !card) {
7620 pr_err("Failed to create ALSA card structures: %d\n", rc);
7621 return 1;
7624 BUG_ON(!card->private_data);
7625 data = card->private_data;
7626 data->card = card;
7628 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7629 sizeof(card->driver));
7630 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7631 sizeof(card->shortname));
7632 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7633 (thinkpad_id.ec_version_str) ?
7634 thinkpad_id.ec_version_str : "(unknown)");
7635 snprintf(card->longname, sizeof(card->longname),
7636 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7637 (thinkpad_id.ec_version_str) ?
7638 thinkpad_id.ec_version_str : "unknown");
7640 if (volume_control_allowed) {
7641 volume_alsa_control_vol.put = volume_alsa_vol_put;
7642 volume_alsa_control_vol.access =
7643 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7645 volume_alsa_control_mute.put = volume_alsa_mute_put;
7646 volume_alsa_control_mute.access =
7647 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7650 if (!tp_features.mixer_no_level_control) {
7651 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7652 rc = snd_ctl_add(card, ctl_vol);
7653 if (rc < 0) {
7654 pr_err("Failed to create ALSA volume control: %d\n",
7655 rc);
7656 goto err_exit;
7658 data->ctl_vol_id = &ctl_vol->id;
7661 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7662 rc = snd_ctl_add(card, ctl_mute);
7663 if (rc < 0) {
7664 pr_err("Failed to create ALSA mute control: %d\n", rc);
7665 goto err_exit;
7667 data->ctl_mute_id = &ctl_mute->id;
7669 rc = snd_card_register(card);
7670 if (rc < 0) {
7671 pr_err("Failed to register ALSA card: %d\n", rc);
7672 goto err_exit;
7675 alsa_card = card;
7676 return 0;
7678 err_exit:
7679 snd_card_free(card);
7680 return 1;
7683 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7684 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7686 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7687 /* Whitelist volume level on all IBM by default */
7688 { .vendor = PCI_VENDOR_ID_IBM,
7689 .bios = TPACPI_MATCH_ANY,
7690 .ec = TPACPI_MATCH_ANY,
7691 .quirks = TPACPI_VOL_Q_LEVEL },
7693 /* Lenovo models with volume control (needs confirmation) */
7694 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7695 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7696 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7697 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7698 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7699 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7700 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7702 /* Whitelist mute-only on all Lenovo by default */
7703 { .vendor = PCI_VENDOR_ID_LENOVO,
7704 .bios = TPACPI_MATCH_ANY,
7705 .ec = TPACPI_MATCH_ANY,
7706 .quirks = TPACPI_VOL_Q_MUTEONLY }
7709 static int __init volume_init(struct ibm_init_struct *iibm)
7711 unsigned long quirks;
7712 int rc;
7714 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7716 mutex_init(&volume_mutex);
7719 * Check for module parameter bogosity, note that we
7720 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7721 * able to detect "unspecified"
7723 if (volume_mode > TPACPI_VOL_MODE_MAX)
7724 return -EINVAL;
7726 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7727 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7728 TPACPI_MAIL);
7729 return 1;
7732 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7733 return -EINVAL;
7736 * The ALSA mixer is our primary interface.
7737 * When disabled, don't install the subdriver at all
7739 if (!alsa_enable) {
7740 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7741 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7742 return 1;
7745 quirks = tpacpi_check_quirks(volume_quirk_table,
7746 ARRAY_SIZE(volume_quirk_table));
7748 switch (volume_capabilities) {
7749 case TPACPI_VOL_CAP_AUTO:
7750 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7751 tp_features.mixer_no_level_control = 1;
7752 else if (quirks & TPACPI_VOL_Q_LEVEL)
7753 tp_features.mixer_no_level_control = 0;
7754 else
7755 return 1; /* no mixer */
7756 break;
7757 case TPACPI_VOL_CAP_VOLMUTE:
7758 tp_features.mixer_no_level_control = 0;
7759 break;
7760 case TPACPI_VOL_CAP_MUTEONLY:
7761 tp_features.mixer_no_level_control = 1;
7762 break;
7763 default:
7764 return 1;
7767 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7768 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7769 "using user-supplied volume_capabilities=%d\n",
7770 volume_capabilities);
7772 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7773 volume_mode == TPACPI_VOL_MODE_MAX) {
7774 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7776 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7777 "driver auto-selected volume_mode=%d\n",
7778 volume_mode);
7779 } else {
7780 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7781 "using user-supplied volume_mode=%d\n",
7782 volume_mode);
7785 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7786 "mute is supported, volume control is %s\n",
7787 str_supported(!tp_features.mixer_no_level_control));
7789 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7790 software_mute_active = true;
7791 } else {
7792 rc = volume_create_alsa_mixer();
7793 if (rc) {
7794 pr_err("Could not create the ALSA mixer interface\n");
7795 return rc;
7798 pr_info("Console audio control enabled, mode: %s\n",
7799 (volume_control_allowed) ?
7800 "override (read/write)" :
7801 "monitor (read only)");
7804 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7805 "registering volume hotkeys as change notification\n");
7806 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7807 | TP_ACPI_HKEY_VOLUP_MASK
7808 | TP_ACPI_HKEY_VOLDWN_MASK
7809 | TP_ACPI_HKEY_MUTE_MASK);
7811 return 0;
7814 static int volume_read(struct seq_file *m)
7816 u8 status;
7818 if (volume_get_status(&status) < 0) {
7819 seq_printf(m, "level:\t\tunreadable\n");
7820 } else {
7821 if (tp_features.mixer_no_level_control)
7822 seq_printf(m, "level:\t\tunsupported\n");
7823 else
7824 seq_printf(m, "level:\t\t%d\n",
7825 status & TP_EC_AUDIO_LVL_MSK);
7827 seq_printf(m, "mute:\t\t%s\n",
7828 onoff(status, TP_EC_AUDIO_MUTESW));
7830 if (volume_control_allowed) {
7831 seq_printf(m, "commands:\tunmute, mute\n");
7832 if (!tp_features.mixer_no_level_control) {
7833 seq_printf(m, "commands:\tup, down\n");
7834 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
7835 TP_EC_VOLUME_MAX);
7840 return 0;
7843 static int volume_write(char *buf)
7845 u8 s;
7846 u8 new_level, new_mute;
7847 int l;
7848 char *cmd;
7849 int rc;
7852 * We do allow volume control at driver startup, so that the
7853 * user can set initial state through the volume=... parameter hack.
7855 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7856 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7857 tp_warned.volume_ctrl_forbidden = 1;
7858 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7859 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7861 return -EPERM;
7864 rc = volume_get_status(&s);
7865 if (rc < 0)
7866 return rc;
7868 new_level = s & TP_EC_AUDIO_LVL_MSK;
7869 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7871 while ((cmd = next_cmd(&buf))) {
7872 if (!tp_features.mixer_no_level_control) {
7873 if (strlencmp(cmd, "up") == 0) {
7874 if (new_mute)
7875 new_mute = 0;
7876 else if (new_level < TP_EC_VOLUME_MAX)
7877 new_level++;
7878 continue;
7879 } else if (strlencmp(cmd, "down") == 0) {
7880 if (new_mute)
7881 new_mute = 0;
7882 else if (new_level > 0)
7883 new_level--;
7884 continue;
7885 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7886 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7887 new_level = l;
7888 continue;
7891 if (strlencmp(cmd, "mute") == 0)
7892 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7893 else if (strlencmp(cmd, "unmute") == 0)
7894 new_mute = 0;
7895 else
7896 return -EINVAL;
7899 if (tp_features.mixer_no_level_control) {
7900 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7901 new_mute ? "" : "un");
7902 rc = volume_set_mute(!!new_mute);
7903 } else {
7904 tpacpi_disclose_usertask("procfs volume",
7905 "%smute and set level to %d\n",
7906 new_mute ? "" : "un", new_level);
7907 rc = volume_set_status(new_mute | new_level);
7909 volume_alsa_notify_change();
7911 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7914 static struct ibm_struct volume_driver_data = {
7915 .name = "volume",
7916 .read = volume_read,
7917 .write = volume_write,
7918 .exit = volume_exit,
7919 .suspend = volume_suspend,
7920 .resume = volume_resume,
7921 .shutdown = volume_shutdown,
7924 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7926 #define alsa_card NULL
7928 static inline void volume_alsa_notify_change(void)
7932 static int __init volume_init(struct ibm_init_struct *iibm)
7934 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7936 return 1;
7939 static struct ibm_struct volume_driver_data = {
7940 .name = "volume",
7943 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7945 /*************************************************************************
7946 * Fan subdriver
7950 * FAN ACCESS MODES
7952 * TPACPI_FAN_RD_ACPI_GFAN:
7953 * ACPI GFAN method: returns fan level
7955 * see TPACPI_FAN_WR_ACPI_SFAN
7956 * EC 0x2f (HFSP) not available if GFAN exists
7958 * TPACPI_FAN_WR_ACPI_SFAN:
7959 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7961 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7962 * it for writing.
7964 * TPACPI_FAN_WR_TPEC:
7965 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7966 * Supported on almost all ThinkPads
7968 * Fan speed changes of any sort (including those caused by the
7969 * disengaged mode) are usually done slowly by the firmware as the
7970 * maximum amount of fan duty cycle change per second seems to be
7971 * limited.
7973 * Reading is not available if GFAN exists.
7974 * Writing is not available if SFAN exists.
7976 * Bits
7977 * 7 automatic mode engaged;
7978 * (default operation mode of the ThinkPad)
7979 * fan level is ignored in this mode.
7980 * 6 full speed mode (takes precedence over bit 7);
7981 * not available on all thinkpads. May disable
7982 * the tachometer while the fan controller ramps up
7983 * the speed (which can take up to a few *minutes*).
7984 * Speeds up fan to 100% duty-cycle, which is far above
7985 * the standard RPM levels. It is not impossible that
7986 * it could cause hardware damage.
7987 * 5-3 unused in some models. Extra bits for fan level
7988 * in others, but still useless as all values above
7989 * 7 map to the same speed as level 7 in these models.
7990 * 2-0 fan level (0..7 usually)
7991 * 0x00 = stop
7992 * 0x07 = max (set when temperatures critical)
7993 * Some ThinkPads may have other levels, see
7994 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7996 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7997 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7998 * does so, its initial value is meaningless (0x07).
8000 * For firmware bugs, refer to:
8001 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8003 * ----
8005 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
8006 * Main fan tachometer reading (in RPM)
8008 * This register is present on all ThinkPads with a new-style EC, and
8009 * it is known not to be present on the A21m/e, and T22, as there is
8010 * something else in offset 0x84 according to the ACPI DSDT. Other
8011 * ThinkPads from this same time period (and earlier) probably lack the
8012 * tachometer as well.
8014 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
8015 * was never fixed by IBM to report the EC firmware version string
8016 * probably support the tachometer (like the early X models), so
8017 * detecting it is quite hard. We need more data to know for sure.
8019 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
8020 * might result.
8022 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8023 * mode.
8025 * For firmware bugs, refer to:
8026 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
8028 * ----
8030 * ThinkPad EC register 0x31 bit 0 (only on select models)
8032 * When bit 0 of EC register 0x31 is zero, the tachometer registers
8033 * show the speed of the main fan. When bit 0 of EC register 0x31
8034 * is one, the tachometer registers show the speed of the auxiliary
8035 * fan.
8037 * Fan control seems to affect both fans, regardless of the state
8038 * of this bit.
8040 * So far, only the firmware for the X60/X61 non-tablet versions
8041 * seem to support this (firmware TP-7M).
8043 * TPACPI_FAN_WR_ACPI_FANS:
8044 * ThinkPad X31, X40, X41. Not available in the X60.
8046 * FANS ACPI handle: takes three arguments: low speed, medium speed,
8047 * high speed. ACPI DSDT seems to map these three speeds to levels
8048 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
8049 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
8051 * The speeds are stored on handles
8052 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
8054 * There are three default speed sets, accessible as handles:
8055 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
8057 * ACPI DSDT switches which set is in use depending on various
8058 * factors.
8060 * TPACPI_FAN_WR_TPEC is also available and should be used to
8061 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
8062 * but the ACPI tables just mention level 7.
8065 enum { /* Fan control constants */
8066 fan_status_offset = 0x2f, /* EC register 0x2f */
8067 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
8068 * 0x84 must be read before 0x85 */
8069 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
8070 bit 0 selects which fan is active */
8072 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
8073 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
8075 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
8078 enum fan_status_access_mode {
8079 TPACPI_FAN_NONE = 0, /* No fan status or control */
8080 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
8081 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8084 enum fan_control_access_mode {
8085 TPACPI_FAN_WR_NONE = 0, /* No fan control */
8086 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
8087 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
8088 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
8091 enum fan_control_commands {
8092 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
8093 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
8094 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
8095 * and also watchdog cmd */
8098 static bool fan_control_allowed;
8100 static enum fan_status_access_mode fan_status_access_mode;
8101 static enum fan_control_access_mode fan_control_access_mode;
8102 static enum fan_control_commands fan_control_commands;
8104 static u8 fan_control_initial_status;
8105 static u8 fan_control_desired_level;
8106 static u8 fan_control_resume_level;
8107 static int fan_watchdog_maxinterval;
8109 static struct mutex fan_mutex;
8111 static void fan_watchdog_fire(struct work_struct *ignored);
8112 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
8114 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
8115 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
8116 "\\FSPD", /* 600e/x, 770e, 770x */
8117 ); /* all others */
8118 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
8119 "JFNS", /* 770x-JL */
8120 ); /* all others */
8123 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8124 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8125 * be in auto mode (0x80).
8127 * This is corrected by any write to HFSP either by the driver, or
8128 * by the firmware.
8130 * We assume 0x07 really means auto mode while this quirk is active,
8131 * as this is far more likely than the ThinkPad being in level 7,
8132 * which is only used by the firmware during thermal emergencies.
8134 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8135 * TP-70 (T43, R52), which are known to be buggy.
8138 static void fan_quirk1_setup(void)
8140 if (fan_control_initial_status == 0x07) {
8141 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8142 tp_features.fan_ctrl_status_undef = 1;
8146 static void fan_quirk1_handle(u8 *fan_status)
8148 if (unlikely(tp_features.fan_ctrl_status_undef)) {
8149 if (*fan_status != fan_control_initial_status) {
8150 /* something changed the HFSP regisnter since
8151 * driver init time, so it is not undefined
8152 * anymore */
8153 tp_features.fan_ctrl_status_undef = 0;
8154 } else {
8155 /* Return most likely status. In fact, it
8156 * might be the only possible status */
8157 *fan_status = TP_EC_FAN_AUTO;
8162 /* Select main fan on X60/X61, NOOP on others */
8163 static bool fan_select_fan1(void)
8165 if (tp_features.second_fan) {
8166 u8 val;
8168 if (ec_read(fan_select_offset, &val) < 0)
8169 return false;
8170 val &= 0xFEU;
8171 if (ec_write(fan_select_offset, val) < 0)
8172 return false;
8174 return true;
8177 /* Select secondary fan on X60/X61 */
8178 static bool fan_select_fan2(void)
8180 u8 val;
8182 if (!tp_features.second_fan)
8183 return false;
8185 if (ec_read(fan_select_offset, &val) < 0)
8186 return false;
8187 val |= 0x01U;
8188 if (ec_write(fan_select_offset, val) < 0)
8189 return false;
8191 return true;
8195 * Call with fan_mutex held
8197 static void fan_update_desired_level(u8 status)
8199 if ((status &
8200 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8201 if (status > 7)
8202 fan_control_desired_level = 7;
8203 else
8204 fan_control_desired_level = status;
8208 static int fan_get_status(u8 *status)
8210 u8 s;
8212 /* TODO:
8213 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8215 switch (fan_status_access_mode) {
8216 case TPACPI_FAN_RD_ACPI_GFAN: {
8217 /* 570, 600e/x, 770e, 770x */
8218 int res;
8220 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8221 return -EIO;
8223 if (likely(status))
8224 *status = res & 0x07;
8226 break;
8228 case TPACPI_FAN_RD_TPEC:
8229 /* all except 570, 600e/x, 770e, 770x */
8230 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8231 return -EIO;
8233 if (likely(status)) {
8234 *status = s;
8235 fan_quirk1_handle(status);
8238 break;
8240 default:
8241 return -ENXIO;
8244 return 0;
8247 static int fan_get_status_safe(u8 *status)
8249 int rc;
8250 u8 s;
8252 if (mutex_lock_killable(&fan_mutex))
8253 return -ERESTARTSYS;
8254 rc = fan_get_status(&s);
8255 if (!rc)
8256 fan_update_desired_level(s);
8257 mutex_unlock(&fan_mutex);
8259 if (rc)
8260 return rc;
8261 if (status)
8262 *status = s;
8264 return 0;
8267 static int fan_get_speed(unsigned int *speed)
8269 u8 hi, lo;
8271 switch (fan_status_access_mode) {
8272 case TPACPI_FAN_RD_TPEC:
8273 /* all except 570, 600e/x, 770e, 770x */
8274 if (unlikely(!fan_select_fan1()))
8275 return -EIO;
8276 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8277 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8278 return -EIO;
8280 if (likely(speed))
8281 *speed = (hi << 8) | lo;
8283 break;
8285 default:
8286 return -ENXIO;
8289 return 0;
8292 static int fan2_get_speed(unsigned int *speed)
8294 u8 hi, lo;
8295 bool rc;
8297 switch (fan_status_access_mode) {
8298 case TPACPI_FAN_RD_TPEC:
8299 /* all except 570, 600e/x, 770e, 770x */
8300 if (unlikely(!fan_select_fan2()))
8301 return -EIO;
8302 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8303 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8304 fan_select_fan1(); /* play it safe */
8305 if (rc)
8306 return -EIO;
8308 if (likely(speed))
8309 *speed = (hi << 8) | lo;
8311 break;
8313 default:
8314 return -ENXIO;
8317 return 0;
8320 static int fan_set_level(int level)
8322 if (!fan_control_allowed)
8323 return -EPERM;
8325 switch (fan_control_access_mode) {
8326 case TPACPI_FAN_WR_ACPI_SFAN:
8327 if (level >= 0 && level <= 7) {
8328 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8329 return -EIO;
8330 } else
8331 return -EINVAL;
8332 break;
8334 case TPACPI_FAN_WR_ACPI_FANS:
8335 case TPACPI_FAN_WR_TPEC:
8336 if (!(level & TP_EC_FAN_AUTO) &&
8337 !(level & TP_EC_FAN_FULLSPEED) &&
8338 ((level < 0) || (level > 7)))
8339 return -EINVAL;
8341 /* safety net should the EC not support AUTO
8342 * or FULLSPEED mode bits and just ignore them */
8343 if (level & TP_EC_FAN_FULLSPEED)
8344 level |= 7; /* safety min speed 7 */
8345 else if (level & TP_EC_FAN_AUTO)
8346 level |= 4; /* safety min speed 4 */
8348 if (!acpi_ec_write(fan_status_offset, level))
8349 return -EIO;
8350 else
8351 tp_features.fan_ctrl_status_undef = 0;
8352 break;
8354 default:
8355 return -ENXIO;
8358 vdbg_printk(TPACPI_DBG_FAN,
8359 "fan control: set fan control register to 0x%02x\n", level);
8360 return 0;
8363 static int fan_set_level_safe(int level)
8365 int rc;
8367 if (!fan_control_allowed)
8368 return -EPERM;
8370 if (mutex_lock_killable(&fan_mutex))
8371 return -ERESTARTSYS;
8373 if (level == TPACPI_FAN_LAST_LEVEL)
8374 level = fan_control_desired_level;
8376 rc = fan_set_level(level);
8377 if (!rc)
8378 fan_update_desired_level(level);
8380 mutex_unlock(&fan_mutex);
8381 return rc;
8384 static int fan_set_enable(void)
8386 u8 s;
8387 int rc;
8389 if (!fan_control_allowed)
8390 return -EPERM;
8392 if (mutex_lock_killable(&fan_mutex))
8393 return -ERESTARTSYS;
8395 switch (fan_control_access_mode) {
8396 case TPACPI_FAN_WR_ACPI_FANS:
8397 case TPACPI_FAN_WR_TPEC:
8398 rc = fan_get_status(&s);
8399 if (rc < 0)
8400 break;
8402 /* Don't go out of emergency fan mode */
8403 if (s != 7) {
8404 s &= 0x07;
8405 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8408 if (!acpi_ec_write(fan_status_offset, s))
8409 rc = -EIO;
8410 else {
8411 tp_features.fan_ctrl_status_undef = 0;
8412 rc = 0;
8414 break;
8416 case TPACPI_FAN_WR_ACPI_SFAN:
8417 rc = fan_get_status(&s);
8418 if (rc < 0)
8419 break;
8421 s &= 0x07;
8423 /* Set fan to at least level 4 */
8424 s |= 4;
8426 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8427 rc = -EIO;
8428 else
8429 rc = 0;
8430 break;
8432 default:
8433 rc = -ENXIO;
8436 mutex_unlock(&fan_mutex);
8438 if (!rc)
8439 vdbg_printk(TPACPI_DBG_FAN,
8440 "fan control: set fan control register to 0x%02x\n",
8442 return rc;
8445 static int fan_set_disable(void)
8447 int rc;
8449 if (!fan_control_allowed)
8450 return -EPERM;
8452 if (mutex_lock_killable(&fan_mutex))
8453 return -ERESTARTSYS;
8455 rc = 0;
8456 switch (fan_control_access_mode) {
8457 case TPACPI_FAN_WR_ACPI_FANS:
8458 case TPACPI_FAN_WR_TPEC:
8459 if (!acpi_ec_write(fan_status_offset, 0x00))
8460 rc = -EIO;
8461 else {
8462 fan_control_desired_level = 0;
8463 tp_features.fan_ctrl_status_undef = 0;
8465 break;
8467 case TPACPI_FAN_WR_ACPI_SFAN:
8468 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8469 rc = -EIO;
8470 else
8471 fan_control_desired_level = 0;
8472 break;
8474 default:
8475 rc = -ENXIO;
8478 if (!rc)
8479 vdbg_printk(TPACPI_DBG_FAN,
8480 "fan control: set fan control register to 0\n");
8482 mutex_unlock(&fan_mutex);
8483 return rc;
8486 static int fan_set_speed(int speed)
8488 int rc;
8490 if (!fan_control_allowed)
8491 return -EPERM;
8493 if (mutex_lock_killable(&fan_mutex))
8494 return -ERESTARTSYS;
8496 rc = 0;
8497 switch (fan_control_access_mode) {
8498 case TPACPI_FAN_WR_ACPI_FANS:
8499 if (speed >= 0 && speed <= 65535) {
8500 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8501 speed, speed, speed))
8502 rc = -EIO;
8503 } else
8504 rc = -EINVAL;
8505 break;
8507 default:
8508 rc = -ENXIO;
8511 mutex_unlock(&fan_mutex);
8512 return rc;
8515 static void fan_watchdog_reset(void)
8517 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8518 return;
8520 if (fan_watchdog_maxinterval > 0 &&
8521 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8522 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8523 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8524 else
8525 cancel_delayed_work(&fan_watchdog_task);
8528 static void fan_watchdog_fire(struct work_struct *ignored)
8530 int rc;
8532 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8533 return;
8535 pr_notice("fan watchdog: enabling fan\n");
8536 rc = fan_set_enable();
8537 if (rc < 0) {
8538 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8539 rc);
8540 /* reschedule for later */
8541 fan_watchdog_reset();
8546 * SYSFS fan layout: hwmon compatible (device)
8548 * pwm*_enable:
8549 * 0: "disengaged" mode
8550 * 1: manual mode
8551 * 2: native EC "auto" mode (recommended, hardware default)
8553 * pwm*: set speed in manual mode, ignored otherwise.
8554 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8555 * interpolation.
8557 * fan*_input: tachometer reading, RPM
8560 * SYSFS fan layout: extensions
8562 * fan_watchdog (driver):
8563 * fan watchdog interval in seconds, 0 disables (default), max 120
8566 /* sysfs fan pwm1_enable ----------------------------------------------- */
8567 static ssize_t fan_pwm1_enable_show(struct device *dev,
8568 struct device_attribute *attr,
8569 char *buf)
8571 int res, mode;
8572 u8 status;
8574 res = fan_get_status_safe(&status);
8575 if (res)
8576 return res;
8578 if (status & TP_EC_FAN_FULLSPEED) {
8579 mode = 0;
8580 } else if (status & TP_EC_FAN_AUTO) {
8581 mode = 2;
8582 } else
8583 mode = 1;
8585 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8588 static ssize_t fan_pwm1_enable_store(struct device *dev,
8589 struct device_attribute *attr,
8590 const char *buf, size_t count)
8592 unsigned long t;
8593 int res, level;
8595 if (parse_strtoul(buf, 2, &t))
8596 return -EINVAL;
8598 tpacpi_disclose_usertask("hwmon pwm1_enable",
8599 "set fan mode to %lu\n", t);
8601 switch (t) {
8602 case 0:
8603 level = TP_EC_FAN_FULLSPEED;
8604 break;
8605 case 1:
8606 level = TPACPI_FAN_LAST_LEVEL;
8607 break;
8608 case 2:
8609 level = TP_EC_FAN_AUTO;
8610 break;
8611 case 3:
8612 /* reserved for software-controlled auto mode */
8613 return -ENOSYS;
8614 default:
8615 return -EINVAL;
8618 res = fan_set_level_safe(level);
8619 if (res == -ENXIO)
8620 return -EINVAL;
8621 else if (res < 0)
8622 return res;
8624 fan_watchdog_reset();
8626 return count;
8629 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8630 fan_pwm1_enable_show, fan_pwm1_enable_store);
8632 /* sysfs fan pwm1 ------------------------------------------------------ */
8633 static ssize_t fan_pwm1_show(struct device *dev,
8634 struct device_attribute *attr,
8635 char *buf)
8637 int res;
8638 u8 status;
8640 res = fan_get_status_safe(&status);
8641 if (res)
8642 return res;
8644 if ((status &
8645 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8646 status = fan_control_desired_level;
8648 if (status > 7)
8649 status = 7;
8651 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8654 static ssize_t fan_pwm1_store(struct device *dev,
8655 struct device_attribute *attr,
8656 const char *buf, size_t count)
8658 unsigned long s;
8659 int rc;
8660 u8 status, newlevel;
8662 if (parse_strtoul(buf, 255, &s))
8663 return -EINVAL;
8665 tpacpi_disclose_usertask("hwmon pwm1",
8666 "set fan speed to %lu\n", s);
8668 /* scale down from 0-255 to 0-7 */
8669 newlevel = (s >> 5) & 0x07;
8671 if (mutex_lock_killable(&fan_mutex))
8672 return -ERESTARTSYS;
8674 rc = fan_get_status(&status);
8675 if (!rc && (status &
8676 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8677 rc = fan_set_level(newlevel);
8678 if (rc == -ENXIO)
8679 rc = -EINVAL;
8680 else if (!rc) {
8681 fan_update_desired_level(newlevel);
8682 fan_watchdog_reset();
8686 mutex_unlock(&fan_mutex);
8687 return (rc) ? rc : count;
8690 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8692 /* sysfs fan fan1_input ------------------------------------------------ */
8693 static ssize_t fan_fan1_input_show(struct device *dev,
8694 struct device_attribute *attr,
8695 char *buf)
8697 int res;
8698 unsigned int speed;
8700 res = fan_get_speed(&speed);
8701 if (res < 0)
8702 return res;
8704 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8707 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8709 /* sysfs fan fan2_input ------------------------------------------------ */
8710 static ssize_t fan_fan2_input_show(struct device *dev,
8711 struct device_attribute *attr,
8712 char *buf)
8714 int res;
8715 unsigned int speed;
8717 res = fan2_get_speed(&speed);
8718 if (res < 0)
8719 return res;
8721 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8724 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8726 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8727 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf)
8729 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8732 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf,
8733 size_t count)
8735 unsigned long t;
8737 if (parse_strtoul(buf, 120, &t))
8738 return -EINVAL;
8740 if (!fan_control_allowed)
8741 return -EPERM;
8743 fan_watchdog_maxinterval = t;
8744 fan_watchdog_reset();
8746 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8748 return count;
8750 static DRIVER_ATTR_RW(fan_watchdog);
8752 /* --------------------------------------------------------------------- */
8753 static struct attribute *fan_attributes[] = {
8754 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8755 &dev_attr_fan1_input.attr,
8756 NULL, /* for fan2_input */
8757 NULL
8760 static const struct attribute_group fan_attr_group = {
8761 .attrs = fan_attributes,
8764 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8765 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8767 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8768 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
8769 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1),
8770 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1),
8771 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1),
8772 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN),
8773 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN),
8776 static int __init fan_init(struct ibm_init_struct *iibm)
8778 int rc;
8779 unsigned long quirks;
8781 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8782 "initializing fan subdriver\n");
8784 mutex_init(&fan_mutex);
8785 fan_status_access_mode = TPACPI_FAN_NONE;
8786 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8787 fan_control_commands = 0;
8788 fan_watchdog_maxinterval = 0;
8789 tp_features.fan_ctrl_status_undef = 0;
8790 tp_features.second_fan = 0;
8791 fan_control_desired_level = 7;
8793 if (tpacpi_is_ibm()) {
8794 TPACPI_ACPIHANDLE_INIT(fans);
8795 TPACPI_ACPIHANDLE_INIT(gfan);
8796 TPACPI_ACPIHANDLE_INIT(sfan);
8799 quirks = tpacpi_check_quirks(fan_quirk_table,
8800 ARRAY_SIZE(fan_quirk_table));
8802 if (gfan_handle) {
8803 /* 570, 600e/x, 770e, 770x */
8804 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8805 } else {
8806 /* all other ThinkPads: note that even old-style
8807 * ThinkPad ECs supports the fan control register */
8808 if (likely(acpi_ec_read(fan_status_offset,
8809 &fan_control_initial_status))) {
8810 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8811 if (quirks & TPACPI_FAN_Q1)
8812 fan_quirk1_setup();
8813 if (quirks & TPACPI_FAN_2FAN) {
8814 tp_features.second_fan = 1;
8815 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8816 "secondary fan support enabled\n");
8818 } else {
8819 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8820 return 1;
8824 if (sfan_handle) {
8825 /* 570, 770x-JL */
8826 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8827 fan_control_commands |=
8828 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8829 } else {
8830 if (!gfan_handle) {
8831 /* gfan without sfan means no fan control */
8832 /* all other models implement TP EC 0x2f control */
8834 if (fans_handle) {
8835 /* X31, X40, X41 */
8836 fan_control_access_mode =
8837 TPACPI_FAN_WR_ACPI_FANS;
8838 fan_control_commands |=
8839 TPACPI_FAN_CMD_SPEED |
8840 TPACPI_FAN_CMD_LEVEL |
8841 TPACPI_FAN_CMD_ENABLE;
8842 } else {
8843 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8844 fan_control_commands |=
8845 TPACPI_FAN_CMD_LEVEL |
8846 TPACPI_FAN_CMD_ENABLE;
8851 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8852 "fan is %s, modes %d, %d\n",
8853 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8854 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8855 fan_status_access_mode, fan_control_access_mode);
8857 /* fan control master switch */
8858 if (!fan_control_allowed) {
8859 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8860 fan_control_commands = 0;
8861 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8862 "fan control features disabled by parameter\n");
8865 /* update fan_control_desired_level */
8866 if (fan_status_access_mode != TPACPI_FAN_NONE)
8867 fan_get_status_safe(NULL);
8869 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8870 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8871 if (tp_features.second_fan) {
8872 /* attach second fan tachometer */
8873 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8874 &dev_attr_fan2_input.attr;
8876 rc = sysfs_create_group(&tpacpi_hwmon->kobj,
8877 &fan_attr_group);
8878 if (rc < 0)
8879 return rc;
8881 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8882 &driver_attr_fan_watchdog);
8883 if (rc < 0) {
8884 sysfs_remove_group(&tpacpi_hwmon->kobj,
8885 &fan_attr_group);
8886 return rc;
8888 return 0;
8889 } else
8890 return 1;
8893 static void fan_exit(void)
8895 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8896 "cancelling any pending fan watchdog tasks\n");
8898 /* FIXME: can we really do this unconditionally? */
8899 sysfs_remove_group(&tpacpi_hwmon->kobj, &fan_attr_group);
8900 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8901 &driver_attr_fan_watchdog);
8903 cancel_delayed_work(&fan_watchdog_task);
8904 flush_workqueue(tpacpi_wq);
8907 static void fan_suspend(void)
8909 int rc;
8911 if (!fan_control_allowed)
8912 return;
8914 /* Store fan status in cache */
8915 fan_control_resume_level = 0;
8916 rc = fan_get_status_safe(&fan_control_resume_level);
8917 if (rc < 0)
8918 pr_notice("failed to read fan level for later restore during resume: %d\n",
8919 rc);
8921 /* if it is undefined, don't attempt to restore it.
8922 * KEEP THIS LAST */
8923 if (tp_features.fan_ctrl_status_undef)
8924 fan_control_resume_level = 0;
8927 static void fan_resume(void)
8929 u8 current_level = 7;
8930 bool do_set = false;
8931 int rc;
8933 /* DSDT *always* updates status on resume */
8934 tp_features.fan_ctrl_status_undef = 0;
8936 if (!fan_control_allowed ||
8937 !fan_control_resume_level ||
8938 (fan_get_status_safe(&current_level) < 0))
8939 return;
8941 switch (fan_control_access_mode) {
8942 case TPACPI_FAN_WR_ACPI_SFAN:
8943 /* never decrease fan level */
8944 do_set = (fan_control_resume_level > current_level);
8945 break;
8946 case TPACPI_FAN_WR_ACPI_FANS:
8947 case TPACPI_FAN_WR_TPEC:
8948 /* never decrease fan level, scale is:
8949 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8951 * We expect the firmware to set either 7 or AUTO, but we
8952 * handle FULLSPEED out of paranoia.
8954 * So, we can safely only restore FULLSPEED or 7, anything
8955 * else could slow the fan. Restoring AUTO is useless, at
8956 * best that's exactly what the DSDT already set (it is the
8957 * slower it uses).
8959 * Always keep in mind that the DSDT *will* have set the
8960 * fans to what the vendor supposes is the best level. We
8961 * muck with it only to speed the fan up.
8963 if (fan_control_resume_level != 7 &&
8964 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8965 return;
8966 else
8967 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8968 (current_level != fan_control_resume_level);
8969 break;
8970 default:
8971 return;
8973 if (do_set) {
8974 pr_notice("restoring fan level to 0x%02x\n",
8975 fan_control_resume_level);
8976 rc = fan_set_level_safe(fan_control_resume_level);
8977 if (rc < 0)
8978 pr_notice("failed to restore fan level: %d\n", rc);
8982 static int fan_read(struct seq_file *m)
8984 int rc;
8985 u8 status;
8986 unsigned int speed = 0;
8988 switch (fan_status_access_mode) {
8989 case TPACPI_FAN_RD_ACPI_GFAN:
8990 /* 570, 600e/x, 770e, 770x */
8991 rc = fan_get_status_safe(&status);
8992 if (rc < 0)
8993 return rc;
8995 seq_printf(m, "status:\t\t%s\n"
8996 "level:\t\t%d\n",
8997 (status != 0) ? "enabled" : "disabled", status);
8998 break;
9000 case TPACPI_FAN_RD_TPEC:
9001 /* all except 570, 600e/x, 770e, 770x */
9002 rc = fan_get_status_safe(&status);
9003 if (rc < 0)
9004 return rc;
9006 seq_printf(m, "status:\t\t%s\n",
9007 (status != 0) ? "enabled" : "disabled");
9009 rc = fan_get_speed(&speed);
9010 if (rc < 0)
9011 return rc;
9013 seq_printf(m, "speed:\t\t%d\n", speed);
9015 if (status & TP_EC_FAN_FULLSPEED)
9016 /* Disengaged mode takes precedence */
9017 seq_printf(m, "level:\t\tdisengaged\n");
9018 else if (status & TP_EC_FAN_AUTO)
9019 seq_printf(m, "level:\t\tauto\n");
9020 else
9021 seq_printf(m, "level:\t\t%d\n", status);
9022 break;
9024 case TPACPI_FAN_NONE:
9025 default:
9026 seq_printf(m, "status:\t\tnot supported\n");
9029 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
9030 seq_printf(m, "commands:\tlevel <level>");
9032 switch (fan_control_access_mode) {
9033 case TPACPI_FAN_WR_ACPI_SFAN:
9034 seq_printf(m, " (<level> is 0-7)\n");
9035 break;
9037 default:
9038 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n");
9039 break;
9043 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
9044 seq_printf(m, "commands:\tenable, disable\n"
9045 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
9047 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
9048 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
9050 return 0;
9053 static int fan_write_cmd_level(const char *cmd, int *rc)
9055 int level;
9057 if (strlencmp(cmd, "level auto") == 0)
9058 level = TP_EC_FAN_AUTO;
9059 else if ((strlencmp(cmd, "level disengaged") == 0) |
9060 (strlencmp(cmd, "level full-speed") == 0))
9061 level = TP_EC_FAN_FULLSPEED;
9062 else if (sscanf(cmd, "level %d", &level) != 1)
9063 return 0;
9065 *rc = fan_set_level_safe(level);
9066 if (*rc == -ENXIO)
9067 pr_err("level command accepted for unsupported access mode %d\n",
9068 fan_control_access_mode);
9069 else if (!*rc)
9070 tpacpi_disclose_usertask("procfs fan",
9071 "set level to %d\n", level);
9073 return 1;
9076 static int fan_write_cmd_enable(const char *cmd, int *rc)
9078 if (strlencmp(cmd, "enable") != 0)
9079 return 0;
9081 *rc = fan_set_enable();
9082 if (*rc == -ENXIO)
9083 pr_err("enable command accepted for unsupported access mode %d\n",
9084 fan_control_access_mode);
9085 else if (!*rc)
9086 tpacpi_disclose_usertask("procfs fan", "enable\n");
9088 return 1;
9091 static int fan_write_cmd_disable(const char *cmd, int *rc)
9093 if (strlencmp(cmd, "disable") != 0)
9094 return 0;
9096 *rc = fan_set_disable();
9097 if (*rc == -ENXIO)
9098 pr_err("disable command accepted for unsupported access mode %d\n",
9099 fan_control_access_mode);
9100 else if (!*rc)
9101 tpacpi_disclose_usertask("procfs fan", "disable\n");
9103 return 1;
9106 static int fan_write_cmd_speed(const char *cmd, int *rc)
9108 int speed;
9110 /* TODO:
9111 * Support speed <low> <medium> <high> ? */
9113 if (sscanf(cmd, "speed %d", &speed) != 1)
9114 return 0;
9116 *rc = fan_set_speed(speed);
9117 if (*rc == -ENXIO)
9118 pr_err("speed command accepted for unsupported access mode %d\n",
9119 fan_control_access_mode);
9120 else if (!*rc)
9121 tpacpi_disclose_usertask("procfs fan",
9122 "set speed to %d\n", speed);
9124 return 1;
9127 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
9129 int interval;
9131 if (sscanf(cmd, "watchdog %d", &interval) != 1)
9132 return 0;
9134 if (interval < 0 || interval > 120)
9135 *rc = -EINVAL;
9136 else {
9137 fan_watchdog_maxinterval = interval;
9138 tpacpi_disclose_usertask("procfs fan",
9139 "set watchdog timer to %d\n",
9140 interval);
9143 return 1;
9146 static int fan_write(char *buf)
9148 char *cmd;
9149 int rc = 0;
9151 while (!rc && (cmd = next_cmd(&buf))) {
9152 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
9153 fan_write_cmd_level(cmd, &rc)) &&
9154 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
9155 (fan_write_cmd_enable(cmd, &rc) ||
9156 fan_write_cmd_disable(cmd, &rc) ||
9157 fan_write_cmd_watchdog(cmd, &rc))) &&
9158 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
9159 fan_write_cmd_speed(cmd, &rc))
9161 rc = -EINVAL;
9162 else if (!rc)
9163 fan_watchdog_reset();
9166 return rc;
9169 static struct ibm_struct fan_driver_data = {
9170 .name = "fan",
9171 .read = fan_read,
9172 .write = fan_write,
9173 .exit = fan_exit,
9174 .suspend = fan_suspend,
9175 .resume = fan_resume,
9178 /*************************************************************************
9179 * Mute LED subdriver
9182 #define TPACPI_LED_MAX 2
9184 struct tp_led_table {
9185 acpi_string name;
9186 int on_value;
9187 int off_value;
9188 int state;
9191 static struct tp_led_table led_tables[TPACPI_LED_MAX] = {
9192 [LED_AUDIO_MUTE] = {
9193 .name = "SSMS",
9194 .on_value = 1,
9195 .off_value = 0,
9197 [LED_AUDIO_MICMUTE] = {
9198 .name = "MMTS",
9199 .on_value = 2,
9200 .off_value = 0,
9204 static int mute_led_on_off(struct tp_led_table *t, bool state)
9206 acpi_handle temp;
9207 int output;
9209 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9210 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9211 return -EIO;
9214 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9215 state ? t->on_value : t->off_value))
9216 return -EIO;
9218 t->state = state;
9219 return state;
9222 static int tpacpi_led_set(int whichled, bool on)
9224 struct tp_led_table *t;
9226 t = &led_tables[whichled];
9227 if (t->state < 0 || t->state == on)
9228 return t->state;
9229 return mute_led_on_off(t, on);
9232 static int tpacpi_led_mute_set(struct led_classdev *led_cdev,
9233 enum led_brightness brightness)
9235 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF);
9238 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev,
9239 enum led_brightness brightness)
9241 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF);
9244 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = {
9245 [LED_AUDIO_MUTE] = {
9246 .name = "platform::mute",
9247 .max_brightness = 1,
9248 .brightness_set_blocking = tpacpi_led_mute_set,
9249 .default_trigger = "audio-mute",
9251 [LED_AUDIO_MICMUTE] = {
9252 .name = "platform::micmute",
9253 .max_brightness = 1,
9254 .brightness_set_blocking = tpacpi_led_micmute_set,
9255 .default_trigger = "audio-micmute",
9259 static int mute_led_init(struct ibm_init_struct *iibm)
9261 acpi_handle temp;
9262 int i, err;
9264 for (i = 0; i < TPACPI_LED_MAX; i++) {
9265 struct tp_led_table *t = &led_tables[i];
9266 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9267 t->state = -ENODEV;
9268 continue;
9271 mute_led_cdev[i].brightness = ledtrig_audio_get(i);
9272 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]);
9273 if (err < 0) {
9274 while (i--) {
9275 if (led_tables[i].state >= 0)
9276 led_classdev_unregister(&mute_led_cdev[i]);
9278 return err;
9281 return 0;
9284 static void mute_led_exit(void)
9286 int i;
9288 for (i = 0; i < TPACPI_LED_MAX; i++) {
9289 if (led_tables[i].state >= 0) {
9290 led_classdev_unregister(&mute_led_cdev[i]);
9291 tpacpi_led_set(i, false);
9296 static void mute_led_resume(void)
9298 int i;
9300 for (i = 0; i < TPACPI_LED_MAX; i++) {
9301 struct tp_led_table *t = &led_tables[i];
9302 if (t->state >= 0)
9303 mute_led_on_off(t, t->state);
9307 static struct ibm_struct mute_led_driver_data = {
9308 .name = "mute_led",
9309 .exit = mute_led_exit,
9310 .resume = mute_led_resume,
9314 * Battery Wear Control Driver
9315 * Contact: Ognjen Galic <smclt30p@gmail.com>
9318 /* Metadata */
9320 #define GET_START "BCTG"
9321 #define SET_START "BCCS"
9322 #define GET_STOP "BCSG"
9323 #define SET_STOP "BCSS"
9325 #define START_ATTR "charge_start_threshold"
9326 #define STOP_ATTR "charge_stop_threshold"
9328 enum {
9329 BAT_ANY = 0,
9330 BAT_PRIMARY = 1,
9331 BAT_SECONDARY = 2
9334 enum {
9335 /* Error condition bit */
9336 METHOD_ERR = BIT(31),
9339 enum {
9340 /* This is used in the get/set helpers */
9341 THRESHOLD_START,
9342 THRESHOLD_STOP,
9345 struct tpacpi_battery_data {
9346 int charge_start;
9347 int start_support;
9348 int charge_stop;
9349 int stop_support;
9352 struct tpacpi_battery_driver_data {
9353 struct tpacpi_battery_data batteries[3];
9354 int individual_addressing;
9357 static struct tpacpi_battery_driver_data battery_info;
9359 /* ACPI helpers/functions/probes */
9362 * This evaluates a ACPI method call specific to the battery
9363 * ACPI extension. The specifics are that an error is marked
9364 * in the 32rd bit of the response, so we just check that here.
9366 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param)
9368 int response;
9370 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) {
9371 acpi_handle_err(hkey_handle, "%s: evaluate failed", method);
9372 return AE_ERROR;
9374 if (response & METHOD_ERR) {
9375 acpi_handle_err(hkey_handle,
9376 "%s evaluated but flagged as error", method);
9377 return AE_ERROR;
9379 *ret = response;
9380 return AE_OK;
9383 static int tpacpi_battery_get(int what, int battery, int *ret)
9385 switch (what) {
9386 case THRESHOLD_START:
9387 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery))
9388 return -ENODEV;
9390 /* The value is in the low 8 bits of the response */
9391 *ret = *ret & 0xFF;
9392 return 0;
9393 case THRESHOLD_STOP:
9394 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery))
9395 return -ENODEV;
9396 /* Value is in lower 8 bits */
9397 *ret = *ret & 0xFF;
9399 * On the stop value, if we return 0 that
9400 * does not make any sense. 0 means Default, which
9401 * means that charging stops at 100%, so we return
9402 * that.
9404 if (*ret == 0)
9405 *ret = 100;
9406 return 0;
9407 default:
9408 pr_crit("wrong parameter: %d", what);
9409 return -EINVAL;
9413 static int tpacpi_battery_set(int what, int battery, int value)
9415 int param, ret;
9416 /* The first 8 bits are the value of the threshold */
9417 param = value;
9418 /* The battery ID is in bits 8-9, 2 bits */
9419 param |= battery << 8;
9421 switch (what) {
9422 case THRESHOLD_START:
9423 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) {
9424 pr_err("failed to set charge threshold on battery %d",
9425 battery);
9426 return -ENODEV;
9428 return 0;
9429 case THRESHOLD_STOP:
9430 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) {
9431 pr_err("failed to set stop threshold: %d", battery);
9432 return -ENODEV;
9434 return 0;
9435 default:
9436 pr_crit("wrong parameter: %d", what);
9437 return -EINVAL;
9441 static int tpacpi_battery_probe(int battery)
9443 int ret = 0;
9445 memset(&battery_info.batteries[battery], 0,
9446 sizeof(battery_info.batteries[battery]));
9449 * 1) Get the current start threshold
9450 * 2) Check for support
9451 * 3) Get the current stop threshold
9452 * 4) Check for support
9454 if (acpi_has_method(hkey_handle, GET_START)) {
9455 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) {
9456 pr_err("Error probing battery %d\n", battery);
9457 return -ENODEV;
9459 /* Individual addressing is in bit 9 */
9460 if (ret & BIT(9))
9461 battery_info.individual_addressing = true;
9462 /* Support is marked in bit 8 */
9463 if (ret & BIT(8))
9464 battery_info.batteries[battery].start_support = 1;
9465 else
9466 return -ENODEV;
9467 if (tpacpi_battery_get(THRESHOLD_START, battery,
9468 &battery_info.batteries[battery].charge_start)) {
9469 pr_err("Error probing battery %d\n", battery);
9470 return -ENODEV;
9473 if (acpi_has_method(hkey_handle, GET_STOP)) {
9474 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) {
9475 pr_err("Error probing battery stop; %d\n", battery);
9476 return -ENODEV;
9478 /* Support is marked in bit 8 */
9479 if (ret & BIT(8))
9480 battery_info.batteries[battery].stop_support = 1;
9481 else
9482 return -ENODEV;
9483 if (tpacpi_battery_get(THRESHOLD_STOP, battery,
9484 &battery_info.batteries[battery].charge_stop)) {
9485 pr_err("Error probing battery stop: %d\n", battery);
9486 return -ENODEV;
9489 pr_info("battery %d registered (start %d, stop %d)",
9490 battery,
9491 battery_info.batteries[battery].charge_start,
9492 battery_info.batteries[battery].charge_stop);
9494 return 0;
9497 /* General helper functions */
9499 static int tpacpi_battery_get_id(const char *battery_name)
9502 if (strcmp(battery_name, "BAT0") == 0 ||
9503 tp_features.battery_force_primary)
9504 return BAT_PRIMARY;
9505 if (strcmp(battery_name, "BAT1") == 0)
9506 return BAT_SECONDARY;
9508 * If for some reason the battery is not BAT0 nor is it
9509 * BAT1, we will assume it's the default, first battery,
9510 * AKA primary.
9512 pr_warn("unknown battery %s, assuming primary", battery_name);
9513 return BAT_PRIMARY;
9516 /* sysfs interface */
9518 static ssize_t tpacpi_battery_store(int what,
9519 struct device *dev,
9520 const char *buf, size_t count)
9522 struct power_supply *supply = to_power_supply(dev);
9523 unsigned long value;
9524 int battery, rval;
9526 * Some systems have support for more than
9527 * one battery. If that is the case,
9528 * tpacpi_battery_probe marked that addressing
9529 * them individually is supported, so we do that
9530 * based on the device struct.
9532 * On systems that are not supported, we assume
9533 * the primary as most of the ACPI calls fail
9534 * with "Any Battery" as the parameter.
9536 if (battery_info.individual_addressing)
9537 /* BAT_PRIMARY or BAT_SECONDARY */
9538 battery = tpacpi_battery_get_id(supply->desc->name);
9539 else
9540 battery = BAT_PRIMARY;
9542 rval = kstrtoul(buf, 10, &value);
9543 if (rval)
9544 return rval;
9546 switch (what) {
9547 case THRESHOLD_START:
9548 if (!battery_info.batteries[battery].start_support)
9549 return -ENODEV;
9550 /* valid values are [0, 99] */
9551 if (value < 0 || value > 99)
9552 return -EINVAL;
9553 if (value > battery_info.batteries[battery].charge_stop)
9554 return -EINVAL;
9555 if (tpacpi_battery_set(THRESHOLD_START, battery, value))
9556 return -ENODEV;
9557 battery_info.batteries[battery].charge_start = value;
9558 return count;
9560 case THRESHOLD_STOP:
9561 if (!battery_info.batteries[battery].stop_support)
9562 return -ENODEV;
9563 /* valid values are [1, 100] */
9564 if (value < 1 || value > 100)
9565 return -EINVAL;
9566 if (value < battery_info.batteries[battery].charge_start)
9567 return -EINVAL;
9568 battery_info.batteries[battery].charge_stop = value;
9570 * When 100 is passed to stop, we need to flip
9571 * it to 0 as that the EC understands that as
9572 * "Default", which will charge to 100%
9574 if (value == 100)
9575 value = 0;
9576 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value))
9577 return -EINVAL;
9578 return count;
9579 default:
9580 pr_crit("Wrong parameter: %d", what);
9581 return -EINVAL;
9583 return count;
9586 static ssize_t tpacpi_battery_show(int what,
9587 struct device *dev,
9588 char *buf)
9590 struct power_supply *supply = to_power_supply(dev);
9591 int ret, battery;
9593 * Some systems have support for more than
9594 * one battery. If that is the case,
9595 * tpacpi_battery_probe marked that addressing
9596 * them individually is supported, so we;
9597 * based on the device struct.
9599 * On systems that are not supported, we assume
9600 * the primary as most of the ACPI calls fail
9601 * with "Any Battery" as the parameter.
9603 if (battery_info.individual_addressing)
9604 /* BAT_PRIMARY or BAT_SECONDARY */
9605 battery = tpacpi_battery_get_id(supply->desc->name);
9606 else
9607 battery = BAT_PRIMARY;
9608 if (tpacpi_battery_get(what, battery, &ret))
9609 return -ENODEV;
9610 return sprintf(buf, "%d\n", ret);
9613 static ssize_t charge_start_threshold_show(struct device *device,
9614 struct device_attribute *attr,
9615 char *buf)
9617 return tpacpi_battery_show(THRESHOLD_START, device, buf);
9620 static ssize_t charge_stop_threshold_show(struct device *device,
9621 struct device_attribute *attr,
9622 char *buf)
9624 return tpacpi_battery_show(THRESHOLD_STOP, device, buf);
9627 static ssize_t charge_start_threshold_store(struct device *dev,
9628 struct device_attribute *attr,
9629 const char *buf, size_t count)
9631 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count);
9634 static ssize_t charge_stop_threshold_store(struct device *dev,
9635 struct device_attribute *attr,
9636 const char *buf, size_t count)
9638 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count);
9641 static DEVICE_ATTR_RW(charge_start_threshold);
9642 static DEVICE_ATTR_RW(charge_stop_threshold);
9644 static struct attribute *tpacpi_battery_attrs[] = {
9645 &dev_attr_charge_start_threshold.attr,
9646 &dev_attr_charge_stop_threshold.attr,
9647 NULL,
9650 ATTRIBUTE_GROUPS(tpacpi_battery);
9652 /* ACPI battery hooking */
9654 static int tpacpi_battery_add(struct power_supply *battery)
9656 int batteryid = tpacpi_battery_get_id(battery->desc->name);
9658 if (tpacpi_battery_probe(batteryid))
9659 return -ENODEV;
9660 if (device_add_groups(&battery->dev, tpacpi_battery_groups))
9661 return -ENODEV;
9662 return 0;
9665 static int tpacpi_battery_remove(struct power_supply *battery)
9667 device_remove_groups(&battery->dev, tpacpi_battery_groups);
9668 return 0;
9671 static struct acpi_battery_hook battery_hook = {
9672 .add_battery = tpacpi_battery_add,
9673 .remove_battery = tpacpi_battery_remove,
9674 .name = "ThinkPad Battery Extension",
9677 /* Subdriver init/exit */
9679 static const struct tpacpi_quirk battery_quirk_table[] __initconst = {
9681 * Individual addressing is broken on models that expose the
9682 * primary battery as BAT1.
9684 TPACPI_Q_LNV('J', '7', true), /* B5400 */
9685 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */
9686 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */
9687 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */
9688 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */
9691 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm)
9693 memset(&battery_info, 0, sizeof(battery_info));
9695 tp_features.battery_force_primary = tpacpi_check_quirks(
9696 battery_quirk_table,
9697 ARRAY_SIZE(battery_quirk_table));
9699 battery_hook_register(&battery_hook);
9700 return 0;
9703 static void tpacpi_battery_exit(void)
9705 battery_hook_unregister(&battery_hook);
9708 static struct ibm_struct battery_driver_data = {
9709 .name = "battery",
9710 .exit = tpacpi_battery_exit,
9713 /*************************************************************************
9714 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature
9717 static int lcdshadow_state;
9719 static int lcdshadow_on_off(bool state)
9721 acpi_handle set_shadow_handle;
9722 int output;
9724 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSSS", &set_shadow_handle))) {
9725 pr_warn("Thinkpad ACPI has no %s interface.\n", "SSSS");
9726 return -EIO;
9729 if (!acpi_evalf(set_shadow_handle, &output, NULL, "dd", (int)state))
9730 return -EIO;
9732 lcdshadow_state = state;
9733 return 0;
9736 static int lcdshadow_set(bool on)
9738 if (lcdshadow_state < 0)
9739 return lcdshadow_state;
9740 if (lcdshadow_state == on)
9741 return 0;
9742 return lcdshadow_on_off(on);
9745 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm)
9747 acpi_handle get_shadow_handle;
9748 int output;
9750 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSSS", &get_shadow_handle))) {
9751 lcdshadow_state = -ENODEV;
9752 return 0;
9755 if (!acpi_evalf(get_shadow_handle, &output, NULL, "dd", 0)) {
9756 lcdshadow_state = -EIO;
9757 return -EIO;
9759 if (!(output & 0x10000)) {
9760 lcdshadow_state = -ENODEV;
9761 return 0;
9763 lcdshadow_state = output & 0x1;
9765 return 0;
9768 static void lcdshadow_resume(void)
9770 if (lcdshadow_state >= 0)
9771 lcdshadow_on_off(lcdshadow_state);
9774 static int lcdshadow_read(struct seq_file *m)
9776 if (lcdshadow_state < 0) {
9777 seq_puts(m, "status:\t\tnot supported\n");
9778 } else {
9779 seq_printf(m, "status:\t\t%d\n", lcdshadow_state);
9780 seq_puts(m, "commands:\t0, 1\n");
9783 return 0;
9786 static int lcdshadow_write(char *buf)
9788 char *cmd;
9789 int state = -1;
9791 if (lcdshadow_state < 0)
9792 return -ENODEV;
9794 while ((cmd = next_cmd(&buf))) {
9795 if (strlencmp(cmd, "0") == 0)
9796 state = 0;
9797 else if (strlencmp(cmd, "1") == 0)
9798 state = 1;
9801 if (state == -1)
9802 return -EINVAL;
9804 return lcdshadow_set(state);
9807 static struct ibm_struct lcdshadow_driver_data = {
9808 .name = "lcdshadow",
9809 .resume = lcdshadow_resume,
9810 .read = lcdshadow_read,
9811 .write = lcdshadow_write,
9814 /****************************************************************************
9815 ****************************************************************************
9817 * Infrastructure
9819 ****************************************************************************
9820 ****************************************************************************/
9823 * HKEY event callout for other subdrivers go here
9824 * (yes, it is ugly, but it is quick, safe, and gets the job done
9826 static void tpacpi_driver_event(const unsigned int hkey_event)
9828 if (ibm_backlight_device) {
9829 switch (hkey_event) {
9830 case TP_HKEY_EV_BRGHT_UP:
9831 case TP_HKEY_EV_BRGHT_DOWN:
9832 tpacpi_brightness_notify_change();
9835 if (alsa_card) {
9836 switch (hkey_event) {
9837 case TP_HKEY_EV_VOL_UP:
9838 case TP_HKEY_EV_VOL_DOWN:
9839 case TP_HKEY_EV_VOL_MUTE:
9840 volume_alsa_notify_change();
9843 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9844 enum led_brightness brightness;
9846 mutex_lock(&kbdlight_mutex);
9849 * Check the brightness actually changed, setting the brightness
9850 * through kbdlight_set_level() also triggers this event.
9852 brightness = kbdlight_sysfs_get(NULL);
9853 if (kbdlight_brightness != brightness) {
9854 kbdlight_brightness = brightness;
9855 led_classdev_notify_brightness_hw_changed(
9856 &tpacpi_led_kbdlight.led_classdev, brightness);
9859 mutex_unlock(&kbdlight_mutex);
9863 static void hotkey_driver_event(const unsigned int scancode)
9865 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9868 /* --------------------------------------------------------------------- */
9870 /* /proc support */
9871 static struct proc_dir_entry *proc_dir;
9874 * Module and infrastructure proble, init and exit handling
9877 static bool force_load;
9879 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9880 static const char * __init str_supported(int is_supported)
9882 static char text_unsupported[] __initdata = "not supported";
9884 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9886 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9888 static void ibm_exit(struct ibm_struct *ibm)
9890 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9892 list_del_init(&ibm->all_drivers);
9894 if (ibm->flags.acpi_notify_installed) {
9895 dbg_printk(TPACPI_DBG_EXIT,
9896 "%s: acpi_remove_notify_handler\n", ibm->name);
9897 BUG_ON(!ibm->acpi);
9898 acpi_remove_notify_handler(*ibm->acpi->handle,
9899 ibm->acpi->type,
9900 dispatch_acpi_notify);
9901 ibm->flags.acpi_notify_installed = 0;
9904 if (ibm->flags.proc_created) {
9905 dbg_printk(TPACPI_DBG_EXIT,
9906 "%s: remove_proc_entry\n", ibm->name);
9907 remove_proc_entry(ibm->name, proc_dir);
9908 ibm->flags.proc_created = 0;
9911 if (ibm->flags.acpi_driver_registered) {
9912 dbg_printk(TPACPI_DBG_EXIT,
9913 "%s: acpi_bus_unregister_driver\n", ibm->name);
9914 BUG_ON(!ibm->acpi);
9915 acpi_bus_unregister_driver(ibm->acpi->driver);
9916 kfree(ibm->acpi->driver);
9917 ibm->acpi->driver = NULL;
9918 ibm->flags.acpi_driver_registered = 0;
9921 if (ibm->flags.init_called && ibm->exit) {
9922 ibm->exit();
9923 ibm->flags.init_called = 0;
9926 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9929 static int __init ibm_init(struct ibm_init_struct *iibm)
9931 int ret;
9932 struct ibm_struct *ibm = iibm->data;
9933 struct proc_dir_entry *entry;
9935 BUG_ON(ibm == NULL);
9937 INIT_LIST_HEAD(&ibm->all_drivers);
9939 if (ibm->flags.experimental && !experimental)
9940 return 0;
9942 dbg_printk(TPACPI_DBG_INIT,
9943 "probing for %s\n", ibm->name);
9945 if (iibm->init) {
9946 ret = iibm->init(iibm);
9947 if (ret > 0)
9948 return 0; /* probe failed */
9949 if (ret)
9950 return ret;
9952 ibm->flags.init_called = 1;
9955 if (ibm->acpi) {
9956 if (ibm->acpi->hid) {
9957 ret = register_tpacpi_subdriver(ibm);
9958 if (ret)
9959 goto err_out;
9962 if (ibm->acpi->notify) {
9963 ret = setup_acpi_notify(ibm);
9964 if (ret == -ENODEV) {
9965 pr_notice("disabling subdriver %s\n",
9966 ibm->name);
9967 ret = 0;
9968 goto err_out;
9970 if (ret < 0)
9971 goto err_out;
9975 dbg_printk(TPACPI_DBG_INIT,
9976 "%s installed\n", ibm->name);
9978 if (ibm->read) {
9979 umode_t mode = iibm->base_procfs_mode;
9981 if (!mode)
9982 mode = S_IRUGO;
9983 if (ibm->write)
9984 mode |= S_IWUSR;
9985 entry = proc_create_data(ibm->name, mode, proc_dir,
9986 &dispatch_proc_ops, ibm);
9987 if (!entry) {
9988 pr_err("unable to create proc entry %s\n", ibm->name);
9989 ret = -ENODEV;
9990 goto err_out;
9992 ibm->flags.proc_created = 1;
9995 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9997 return 0;
9999 err_out:
10000 dbg_printk(TPACPI_DBG_INIT,
10001 "%s: at error exit path with result %d\n",
10002 ibm->name, ret);
10004 ibm_exit(ibm);
10005 return (ret < 0) ? ret : 0;
10008 /* Probing */
10010 static char __init tpacpi_parse_fw_id(const char * const s,
10011 u32 *model, u16 *release)
10013 int i;
10015 if (!s || strlen(s) < 8)
10016 goto invalid;
10018 for (i = 0; i < 8; i++)
10019 if (!((s[i] >= '0' && s[i] <= '9') ||
10020 (s[i] >= 'A' && s[i] <= 'Z')))
10021 goto invalid;
10024 * Most models: xxyTkkWW (#.##c)
10025 * Ancient 570/600 and -SL lacks (#.##c)
10027 if (s[3] == 'T' || s[3] == 'N') {
10028 *model = TPID(s[0], s[1]);
10029 *release = TPVER(s[4], s[5]);
10030 return s[2];
10032 /* New models: xxxyTkkW (#.##c); T550 and some others */
10033 } else if (s[4] == 'T' || s[4] == 'N') {
10034 *model = TPID3(s[0], s[1], s[2]);
10035 *release = TPVER(s[5], s[6]);
10036 return s[3];
10039 invalid:
10040 return '\0';
10043 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private)
10045 char *ec_fw_string = (char *) private;
10046 const char *dmi_data = (const char *)dm;
10048 * ThinkPad Embedded Controller Program Table on newer models
10050 * Offset | Name | Width | Description
10051 * ----------------------------------------------------
10052 * 0x00 | Type | BYTE | 0x8C
10053 * 0x01 | Length | BYTE |
10054 * 0x02 | Handle | WORD | Varies
10055 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO"
10056 * 0x0A | OEM struct offset | BYTE | 0x0B
10057 * 0x0B | OEM struct number | BYTE | 0x07, for this structure
10058 * 0x0C | OEM struct revision | BYTE | 0x01, for this format
10059 * 0x0D | ECP version ID | STR ID |
10060 * 0x0E | ECP release date | STR ID |
10063 /* Return if data structure not match */
10064 if (dm->type != 140 || dm->length < 0x0F ||
10065 memcmp(dmi_data + 4, "LENOVO", 6) != 0 ||
10066 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 ||
10067 dmi_data[0x0C] != 0x01)
10068 return;
10070 /* fwstr is the first 8byte string */
10071 strncpy(ec_fw_string, dmi_data + 0x0F, 8);
10074 /* returns 0 - probe ok, or < 0 - probe error.
10075 * Probe ok doesn't mean thinkpad found.
10076 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
10077 static int __must_check __init get_thinkpad_model_data(
10078 struct thinkpad_id_data *tp)
10080 const struct dmi_device *dev = NULL;
10081 char ec_fw_string[18] = {0};
10082 char const *s;
10083 char t;
10085 if (!tp)
10086 return -EINVAL;
10088 memset(tp, 0, sizeof(*tp));
10090 if (dmi_name_in_vendors("IBM"))
10091 tp->vendor = PCI_VENDOR_ID_IBM;
10092 else if (dmi_name_in_vendors("LENOVO"))
10093 tp->vendor = PCI_VENDOR_ID_LENOVO;
10094 else
10095 return 0;
10097 s = dmi_get_system_info(DMI_BIOS_VERSION);
10098 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
10099 if (s && !tp->bios_version_str)
10100 return -ENOMEM;
10102 /* Really ancient ThinkPad 240X will fail this, which is fine */
10103 t = tpacpi_parse_fw_id(tp->bios_version_str,
10104 &tp->bios_model, &tp->bios_release);
10105 if (t != 'E' && t != 'C')
10106 return 0;
10109 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
10110 * X32 or newer, all Z series; Some models must have an
10111 * up-to-date BIOS or they will not be detected.
10113 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10115 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
10116 if (sscanf(dev->name,
10117 "IBM ThinkPad Embedded Controller -[%17c",
10118 ec_fw_string) == 1) {
10119 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
10120 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
10121 break;
10125 /* Newer ThinkPads have different EC program info table */
10126 if (!ec_fw_string[0])
10127 dmi_walk(find_new_ec_fwstr, &ec_fw_string);
10129 if (ec_fw_string[0]) {
10130 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
10131 if (!tp->ec_version_str)
10132 return -ENOMEM;
10134 t = tpacpi_parse_fw_id(ec_fw_string,
10135 &tp->ec_model, &tp->ec_release);
10136 if (t != 'H') {
10137 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
10138 ec_fw_string);
10139 pr_notice("please report this to %s\n", TPACPI_MAIL);
10143 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
10144 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
10145 tp->model_str = kstrdup(s, GFP_KERNEL);
10146 if (!tp->model_str)
10147 return -ENOMEM;
10148 } else {
10149 s = dmi_get_system_info(DMI_BIOS_VENDOR);
10150 if (s && !(strncasecmp(s, "Lenovo", 6))) {
10151 tp->model_str = kstrdup(s, GFP_KERNEL);
10152 if (!tp->model_str)
10153 return -ENOMEM;
10157 s = dmi_get_system_info(DMI_PRODUCT_NAME);
10158 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
10159 if (s && !tp->nummodel_str)
10160 return -ENOMEM;
10162 return 0;
10165 static int __init probe_for_thinkpad(void)
10167 int is_thinkpad;
10169 if (acpi_disabled)
10170 return -ENODEV;
10172 /* It would be dangerous to run the driver in this case */
10173 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
10174 return -ENODEV;
10177 * Non-ancient models have better DMI tagging, but very old models
10178 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
10180 is_thinkpad = (thinkpad_id.model_str != NULL) ||
10181 (thinkpad_id.ec_model != 0) ||
10182 tpacpi_is_fw_known();
10184 /* The EC handler is required */
10185 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
10186 if (!ec_handle) {
10187 if (is_thinkpad)
10188 pr_err("Not yet supported ThinkPad detected!\n");
10189 return -ENODEV;
10192 if (!is_thinkpad && !force_load)
10193 return -ENODEV;
10195 return 0;
10198 static void __init thinkpad_acpi_init_banner(void)
10200 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
10201 pr_info("%s\n", TPACPI_URL);
10203 pr_info("ThinkPad BIOS %s, EC %s\n",
10204 (thinkpad_id.bios_version_str) ?
10205 thinkpad_id.bios_version_str : "unknown",
10206 (thinkpad_id.ec_version_str) ?
10207 thinkpad_id.ec_version_str : "unknown");
10209 BUG_ON(!thinkpad_id.vendor);
10211 if (thinkpad_id.model_str)
10212 pr_info("%s %s, model %s\n",
10213 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
10214 "IBM" : ((thinkpad_id.vendor ==
10215 PCI_VENDOR_ID_LENOVO) ?
10216 "Lenovo" : "Unknown vendor"),
10217 thinkpad_id.model_str,
10218 (thinkpad_id.nummodel_str) ?
10219 thinkpad_id.nummodel_str : "unknown");
10222 /* Module init, exit, parameters */
10224 static struct ibm_init_struct ibms_init[] __initdata = {
10226 .data = &thinkpad_acpi_driver_data,
10229 .init = hotkey_init,
10230 .data = &hotkey_driver_data,
10233 .init = bluetooth_init,
10234 .data = &bluetooth_driver_data,
10237 .init = wan_init,
10238 .data = &wan_driver_data,
10241 .init = uwb_init,
10242 .data = &uwb_driver_data,
10244 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
10246 .init = video_init,
10247 .base_procfs_mode = S_IRUSR,
10248 .data = &video_driver_data,
10250 #endif
10252 .init = kbdlight_init,
10253 .data = &kbdlight_driver_data,
10256 .init = light_init,
10257 .data = &light_driver_data,
10260 .init = cmos_init,
10261 .data = &cmos_driver_data,
10264 .init = led_init,
10265 .data = &led_driver_data,
10268 .init = beep_init,
10269 .data = &beep_driver_data,
10272 .init = thermal_init,
10273 .data = &thermal_driver_data,
10276 .init = brightness_init,
10277 .data = &brightness_driver_data,
10280 .init = volume_init,
10281 .data = &volume_driver_data,
10284 .init = fan_init,
10285 .data = &fan_driver_data,
10288 .init = mute_led_init,
10289 .data = &mute_led_driver_data,
10292 .init = tpacpi_battery_init,
10293 .data = &battery_driver_data,
10296 .init = tpacpi_lcdshadow_init,
10297 .data = &lcdshadow_driver_data,
10301 static int __init set_ibm_param(const char *val, const struct kernel_param *kp)
10303 unsigned int i;
10304 struct ibm_struct *ibm;
10306 if (!kp || !kp->name || !val)
10307 return -EINVAL;
10309 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10310 ibm = ibms_init[i].data;
10311 WARN_ON(ibm == NULL);
10313 if (!ibm || !ibm->name)
10314 continue;
10316 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
10317 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
10318 return -ENOSPC;
10319 strcpy(ibms_init[i].param, val);
10320 strcat(ibms_init[i].param, ",");
10321 return 0;
10325 return -EINVAL;
10328 module_param(experimental, int, 0444);
10329 MODULE_PARM_DESC(experimental,
10330 "Enables experimental features when non-zero");
10332 module_param_named(debug, dbg_level, uint, 0);
10333 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
10335 module_param(force_load, bool, 0444);
10336 MODULE_PARM_DESC(force_load,
10337 "Attempts to load the driver even on a mis-identified ThinkPad when true");
10339 module_param_named(fan_control, fan_control_allowed, bool, 0444);
10340 MODULE_PARM_DESC(fan_control,
10341 "Enables setting fan parameters features when true");
10343 module_param_named(brightness_mode, brightness_mode, uint, 0444);
10344 MODULE_PARM_DESC(brightness_mode,
10345 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
10347 module_param(brightness_enable, uint, 0444);
10348 MODULE_PARM_DESC(brightness_enable,
10349 "Enables backlight control when 1, disables when 0");
10351 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
10352 module_param_named(volume_mode, volume_mode, uint, 0444);
10353 MODULE_PARM_DESC(volume_mode,
10354 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
10356 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
10357 MODULE_PARM_DESC(volume_capabilities,
10358 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only");
10360 module_param_named(volume_control, volume_control_allowed, bool, 0444);
10361 MODULE_PARM_DESC(volume_control,
10362 "Enables software override for the console audio control when true");
10364 module_param_named(software_mute, software_mute_requested, bool, 0444);
10365 MODULE_PARM_DESC(software_mute,
10366 "Request full software mute control");
10368 /* ALSA module API parameters */
10369 module_param_named(index, alsa_index, int, 0444);
10370 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
10371 module_param_named(id, alsa_id, charp, 0444);
10372 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
10373 module_param_named(enable, alsa_enable, bool, 0444);
10374 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
10375 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
10377 /* The module parameter can't be read back, that's why 0 is used here */
10378 #define TPACPI_PARAM(feature) \
10379 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
10380 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
10382 TPACPI_PARAM(hotkey);
10383 TPACPI_PARAM(bluetooth);
10384 TPACPI_PARAM(video);
10385 TPACPI_PARAM(light);
10386 TPACPI_PARAM(cmos);
10387 TPACPI_PARAM(led);
10388 TPACPI_PARAM(beep);
10389 TPACPI_PARAM(brightness);
10390 TPACPI_PARAM(volume);
10391 TPACPI_PARAM(fan);
10393 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
10394 module_param(dbg_wlswemul, uint, 0444);
10395 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
10396 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
10397 MODULE_PARM_DESC(wlsw_state,
10398 "Initial state of the emulated WLSW switch");
10400 module_param(dbg_bluetoothemul, uint, 0444);
10401 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
10402 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
10403 MODULE_PARM_DESC(bluetooth_state,
10404 "Initial state of the emulated bluetooth switch");
10406 module_param(dbg_wwanemul, uint, 0444);
10407 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
10408 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
10409 MODULE_PARM_DESC(wwan_state,
10410 "Initial state of the emulated WWAN switch");
10412 module_param(dbg_uwbemul, uint, 0444);
10413 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
10414 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
10415 MODULE_PARM_DESC(uwb_state,
10416 "Initial state of the emulated UWB switch");
10417 #endif
10419 static void thinkpad_acpi_module_exit(void)
10421 struct ibm_struct *ibm, *itmp;
10423 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
10425 list_for_each_entry_safe_reverse(ibm, itmp,
10426 &tpacpi_all_drivers,
10427 all_drivers) {
10428 ibm_exit(ibm);
10431 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
10433 if (tpacpi_inputdev) {
10434 if (tp_features.input_device_registered)
10435 input_unregister_device(tpacpi_inputdev);
10436 else
10437 input_free_device(tpacpi_inputdev);
10438 kfree(hotkey_keycode_map);
10441 if (tpacpi_hwmon)
10442 hwmon_device_unregister(tpacpi_hwmon);
10444 if (tpacpi_sensors_pdev)
10445 platform_device_unregister(tpacpi_sensors_pdev);
10446 if (tpacpi_pdev)
10447 platform_device_unregister(tpacpi_pdev);
10449 if (tp_features.sensors_pdrv_attrs_registered)
10450 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
10451 if (tp_features.platform_drv_attrs_registered)
10452 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
10454 if (tp_features.sensors_pdrv_registered)
10455 platform_driver_unregister(&tpacpi_hwmon_pdriver);
10457 if (tp_features.platform_drv_registered)
10458 platform_driver_unregister(&tpacpi_pdriver);
10460 if (proc_dir)
10461 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
10463 if (tpacpi_wq)
10464 destroy_workqueue(tpacpi_wq);
10466 kfree(thinkpad_id.bios_version_str);
10467 kfree(thinkpad_id.ec_version_str);
10468 kfree(thinkpad_id.model_str);
10469 kfree(thinkpad_id.nummodel_str);
10473 static int __init thinkpad_acpi_module_init(void)
10475 int ret, i;
10477 tpacpi_lifecycle = TPACPI_LIFE_INIT;
10479 /* Driver-level probe */
10481 ret = get_thinkpad_model_data(&thinkpad_id);
10482 if (ret) {
10483 pr_err("unable to get DMI data: %d\n", ret);
10484 thinkpad_acpi_module_exit();
10485 return ret;
10487 ret = probe_for_thinkpad();
10488 if (ret) {
10489 thinkpad_acpi_module_exit();
10490 return ret;
10493 /* Driver initialization */
10495 thinkpad_acpi_init_banner();
10496 tpacpi_check_outdated_fw();
10498 TPACPI_ACPIHANDLE_INIT(ecrd);
10499 TPACPI_ACPIHANDLE_INIT(ecwr);
10501 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
10502 if (!tpacpi_wq) {
10503 thinkpad_acpi_module_exit();
10504 return -ENOMEM;
10507 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
10508 if (!proc_dir) {
10509 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
10510 thinkpad_acpi_module_exit();
10511 return -ENODEV;
10514 ret = platform_driver_register(&tpacpi_pdriver);
10515 if (ret) {
10516 pr_err("unable to register main platform driver\n");
10517 thinkpad_acpi_module_exit();
10518 return ret;
10520 tp_features.platform_drv_registered = 1;
10522 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
10523 if (ret) {
10524 pr_err("unable to register hwmon platform driver\n");
10525 thinkpad_acpi_module_exit();
10526 return ret;
10528 tp_features.sensors_pdrv_registered = 1;
10530 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
10531 if (!ret) {
10532 tp_features.platform_drv_attrs_registered = 1;
10533 ret = tpacpi_create_driver_attributes(
10534 &tpacpi_hwmon_pdriver.driver);
10536 if (ret) {
10537 pr_err("unable to create sysfs driver attributes\n");
10538 thinkpad_acpi_module_exit();
10539 return ret;
10541 tp_features.sensors_pdrv_attrs_registered = 1;
10544 /* Device initialization */
10545 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
10546 NULL, 0);
10547 if (IS_ERR(tpacpi_pdev)) {
10548 ret = PTR_ERR(tpacpi_pdev);
10549 tpacpi_pdev = NULL;
10550 pr_err("unable to register platform device\n");
10551 thinkpad_acpi_module_exit();
10552 return ret;
10554 tpacpi_sensors_pdev = platform_device_register_simple(
10555 TPACPI_HWMON_DRVR_NAME,
10556 -1, NULL, 0);
10557 if (IS_ERR(tpacpi_sensors_pdev)) {
10558 ret = PTR_ERR(tpacpi_sensors_pdev);
10559 tpacpi_sensors_pdev = NULL;
10560 pr_err("unable to register hwmon platform device\n");
10561 thinkpad_acpi_module_exit();
10562 return ret;
10564 tp_features.sensors_pdev_attrs_registered = 1;
10565 tpacpi_hwmon = hwmon_device_register_with_groups(
10566 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, NULL);
10568 if (IS_ERR(tpacpi_hwmon)) {
10569 ret = PTR_ERR(tpacpi_hwmon);
10570 tpacpi_hwmon = NULL;
10571 pr_err("unable to register hwmon device\n");
10572 thinkpad_acpi_module_exit();
10573 return ret;
10575 mutex_init(&tpacpi_inputdev_send_mutex);
10576 tpacpi_inputdev = input_allocate_device();
10577 if (!tpacpi_inputdev) {
10578 thinkpad_acpi_module_exit();
10579 return -ENOMEM;
10580 } else {
10581 /* Prepare input device, but don't register */
10582 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
10583 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
10584 tpacpi_inputdev->id.bustype = BUS_HOST;
10585 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
10586 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
10587 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
10588 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
10591 /* Init subdriver dependencies */
10592 tpacpi_detect_brightness_capabilities();
10594 /* Init subdrivers */
10595 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
10596 ret = ibm_init(&ibms_init[i]);
10597 if (ret >= 0 && *ibms_init[i].param)
10598 ret = ibms_init[i].data->write(ibms_init[i].param);
10599 if (ret < 0) {
10600 thinkpad_acpi_module_exit();
10601 return ret;
10605 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
10607 ret = input_register_device(tpacpi_inputdev);
10608 if (ret < 0) {
10609 pr_err("unable to register input device\n");
10610 thinkpad_acpi_module_exit();
10611 return ret;
10612 } else {
10613 tp_features.input_device_registered = 1;
10616 return 0;
10619 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
10622 * This will autoload the driver in almost every ThinkPad
10623 * in widespread use.
10625 * Only _VERY_ old models, like the 240, 240x and 570 lack
10626 * the HKEY event interface.
10628 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
10631 * DMI matching for module autoloading
10633 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
10634 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
10636 * Only models listed in thinkwiki will be supported, so add yours
10637 * if it is not there yet.
10639 #define IBM_BIOS_MODULE_ALIAS(__type) \
10640 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10642 /* Ancient thinkpad BIOSes have to be identified by
10643 * BIOS type or model number, and there are far less
10644 * BIOS types than model numbers... */
10645 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
10647 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10648 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
10649 MODULE_DESCRIPTION(TPACPI_DESC);
10650 MODULE_VERSION(TPACPI_VERSION);
10651 MODULE_LICENSE("GPL");
10653 module_init(thinkpad_acpi_module_init);
10654 module_exit(thinkpad_acpi_module_exit);