1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define TPACPI_VERSION "0.26"
12 #define TPACPI_SYSFS_VERSION 0x030000
16 * 2007-10-20 changelog trimmed down
18 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
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>
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
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,
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 */
113 TP_NVRAM_LEVEL_VOLUME_MAX
= 14,
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"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
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 */
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 */
149 enum tpacpi_hkey_event_t
{
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 */
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
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 */
215 TP_HKEY_EV_RFKILL_CHANGED
= 0x7000, /* rfkill switch changed */
218 /****************************************************************************
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
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
);
268 struct acpi_device
*device
;
274 int (*read
) (struct seq_file
*);
275 int (*write
) (char *);
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
;
286 u8 acpi_driver_registered
:1;
287 u8 acpi_notify_installed
:1;
294 struct ibm_init_struct
{
297 int (*init
) (struct ibm_init_struct
*);
298 umode_t base_procfs_mode
;
299 struct ibm_struct
*data
;
308 TP_HOTKEY_TABLET_NONE
= 0,
309 TP_HOTKEY_TABLET_USES_MHKG
,
310 TP_HOTKEY_TABLET_USES_GMMS
,
315 u32 bright_acpimode
:1;
319 u32 fan_ctrl_status_undef
: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;
335 u16 hotkey_mask_ff
:1;
336 u16 volume_ctrl_forbidden
:1;
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 */
348 u16 bios_release
; /* 1ZETK1WW = 0x4b31, 0 = unknown */
351 char *model_str
; /* ThinkPad T43 */
352 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
354 static struct thinkpad_id_data thinkpad_id
;
357 TPACPI_LIFE_INIT
= 0,
362 static int experimental
;
363 static u32 dbg_level
;
365 static struct workqueue_struct
*tpacpi_wq
;
373 /* tpacpi LED class */
374 struct tpacpi_led_classdev
{
375 struct led_classdev led_classdev
;
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
;
394 /*************************************************************************
398 #define dbg_printk(a_dbg_level, format, arg...) \
400 if (dbg_level & (a_dbg_level)) \
401 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
406 #define vdbg_printk dbg_printk
407 static const char *str_supported(int is_supported
);
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)
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...) \
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); \
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))
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
{
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
)
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
;
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 /*************************************************************************
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 = \
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, */
552 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
553 "\\CMS", /* R40, R40e */
556 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
557 "^HKEY", /* R30, R31 */
558 "HKEY", /* all others */
561 /*************************************************************************
565 static int acpi_evalf(acpi_handle handle
,
566 int *res
, char *method
, char *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
;
580 pr_err("acpi_evalf() called with empty format\n");
593 params
.pointer
= &in_objs
[0];
600 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
601 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
603 /* add more types as needed */
605 pr_err("acpi_evalf() called with invalid format character '%c'\n",
613 if (res_type
!= 'v') {
614 result
.length
= sizeof(out_obj
);
615 result
.pointer
= &out_obj
;
620 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
624 success
= (status
== AE_OK
&&
625 out_obj
.type
== ACPI_TYPE_INTEGER
);
627 *res
= out_obj
.integer
.value
;
630 success
= status
== AE_OK
;
632 /* add more types as needed */
634 pr_err("acpi_evalf() called with invalid format character '%c'\n",
639 if (!success
&& !quiet
)
640 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
641 method
, fmt0
, acpi_format_exception(status
));
646 static int acpi_ec_read(int i
, u8
*p
)
651 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
655 if (ec_read(i
, p
) < 0)
662 static int acpi_ec_write(int i
, u8 v
)
665 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
668 if (ec_write(i
, v
) < 0)
675 static int issue_thinkpad_cmos_command(int cmos_cmd
)
680 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
686 /*************************************************************************
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
)
701 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
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",
714 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
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
))
726 if (strcmp(ACPI_VIDEO_HID
, acpi_device_hid(dev
)))
730 *(acpi_handle
*)return_value
= handle
;
732 return AE_CTRL_TERMINATE
;
735 static void __init
tpacpi_acpi_handle_locate(const char *name
,
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
);
753 if (ACPI_SUCCESS(status
)) {
754 *handle
= device_found
;
755 dbg_printk(TPACPI_DBG_INIT
,
756 "Found ACPI handle for %s\n", name
);
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
)
771 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
774 ibm
->acpi
->notify(ibm
, event
);
777 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
784 if (!*ibm
->acpi
->handle
)
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
);
792 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm
->name
, rc
);
796 ibm
->acpi
->device
->driver_data
= ibm
;
797 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
798 TPACPI_ACPI_EVENT_PREFIX
,
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",
808 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
809 ibm
->name
, acpi_format_exception(status
));
813 ibm
->flags
.acpi_notify_installed
= 1;
817 static int __init
tpacpi_device_add(struct acpi_device
*device
)
822 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
826 dbg_printk(TPACPI_DBG_INIT
,
827 "registering %s as an ACPI driver\n", ibm
->name
);
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");
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
);
844 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
846 kfree(ibm
->acpi
->driver
);
847 ibm
->acpi
->driver
= NULL
;
849 ibm
->flags
.acpi_driver_registered
= 1;
855 /****************************************************************************
856 ****************************************************************************
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
)
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
));
885 if (!ibm
|| !ibm
->write
)
887 if (count
> PAGE_SIZE
- 2)
890 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
894 if (copy_from_user(kernbuf
, userbuf
, count
)) {
900 strcat(kernbuf
, ",");
901 ret
= ibm
->write(kernbuf
);
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
)
923 while ((end
= strchr(start
, ',')) && end
== 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
,
965 static int tpacpi_resume_handler(struct device
*dev
)
967 struct ibm_struct
*ibm
, *itmp
;
969 list_for_each_entry_safe(ibm
, itmp
,
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
,
995 static struct platform_driver tpacpi_pdriver
= {
997 .name
= TPACPI_DRVR_NAME
,
1000 .shutdown
= tpacpi_shutdown_handler
,
1003 static struct platform_driver tpacpi_hwmon_pdriver
= {
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
,
1026 struct attribute_set_obj
*sobj
;
1028 if (max_members
== 0)
1031 /* Allocates space for implicit NULL at the end too */
1032 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
1033 max_members
* sizeof(struct attribute
*),
1037 sobj
->s
.max_members
= max_members
;
1038 sobj
->s
.group
.attrs
= &sobj
->a
;
1039 sobj
->s
.group
.name
= name
;
1044 #define destroy_attr_set(_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
)
1053 if (s
->members
>= s
->max_members
)
1056 s
->group
.attrs
[s
->members
] = attr
;
1062 static int add_many_to_attr_set(struct attribute_set
*s
,
1063 struct attribute
**attr
,
1068 for (i
= 0; i
< count
; i
++) {
1069 res
= add_to_attr_set(s
, attr
[i
]);
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
)
1091 *value
= simple_strtoul(skip_spaces(buf
), &endp
, 0);
1092 endp
= skip_spaces(endp
);
1093 if (*endp
|| *value
> max
)
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",
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,
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
,
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 */
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
)
1180 status
= (tp_rfk
->ops
->get_status
)();
1184 rfkill_set_sw_state(tp_rfk
->rfkill
,
1185 (status
== TPACPI_RFK_RADIO_OFF
));
1190 /* Query FW and update rfkill sw state for all rfkill switches */
1191 static void tpacpi_rfk_update_swstate_all(void)
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
)
1206 struct tpacpi_rfk
*tp_rfk
;
1208 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++) {
1209 tp_rfk
= tpacpi_rfkill_switches
[i
];
1211 if (rfkill_set_hw_state(tp_rfk
->rfkill
,
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();
1228 /* When unknown or unsupported, we have to assume it is unblocked */
1232 hw_blocked
= (res
== TPACPI_RFK_RADIO_OFF
);
1233 tpacpi_rfk_update_hwblock_state(hw_blocked
);
1238 static int tpacpi_rfk_hook_set_block(void *data
, bool blocked
)
1240 struct tpacpi_rfk
*tp_rfk
= data
;
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
,
1265 const bool set_default
)
1267 struct tpacpi_rfk
*atp_rfk
;
1269 bool sw_state
= false;
1273 BUG_ON(id
>= TPACPI_RFK_SW_MAX
|| tpacpi_rfkill_switches
[id
]);
1275 atp_rfk
= kzalloc(sizeof(struct tpacpi_rfk
), GFP_KERNEL
);
1277 atp_rfk
->rfkill
= rfkill_alloc(name
,
1280 &tpacpi_rfk_rfkill_ops
,
1282 if (!atp_rfk
|| !atp_rfk
->rfkill
) {
1283 pr_err("failed to allocate memory for rfkill class\n");
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",
1296 sw_state
= (sw_status
== TPACPI_RFK_RADIO_OFF
);
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
);
1308 pr_err("failed to register %s rfkill switch: %d\n", name
, res
);
1309 rfkill_destroy(atp_rfk
->rfkill
);
1314 tpacpi_rfkill_switches
[id
] = atp_rfk
;
1316 pr_info("rfkill switch %s: radio is %sblocked\n",
1317 name
, (sw_state
|| hw_state
) ? "" : "un");
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
];
1329 rfkill_unregister(tp_rfk
->rfkill
);
1330 rfkill_destroy(tp_rfk
->rfkill
);
1331 tpacpi_rfkill_switches
[id
] = NULL
;
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
,
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
;
1355 status
= tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
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
)
1371 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1373 if (parse_strtoul(buf
, 1, &t
))
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
)
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");
1397 /* This is in the ABI... */
1398 if (tpacpi_rfk_check_hwblock_state()) {
1399 status
= TPACPI_RFK_RADIO_OFF
;
1401 status
= tpacpi_rfk_update_swstate(
1402 tpacpi_rfkill_switches
[id
]);
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");
1416 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id
, char *buf
)
1422 if (id
>= TPACPI_RFK_SW_MAX
)
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
;
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
]);
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
,
1468 if (parse_strtoul(buf
, 0xffff, &t
))
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
,
1500 if (parse_strtoul(buf
, 1, &t
))
1503 if (tpacpi_wlsw_emulstate
!= !!t
) {
1504 tpacpi_wlsw_emulstate
= !!t
;
1505 tpacpi_rfk_update_hwblock_state(!t
); /* negative logic */
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
)
1523 if (parse_strtoul(buf
, 1, &t
))
1526 tpacpi_bluetooth_emulstate
= !!t
;
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
,
1543 if (parse_strtoul(buf
, 1, &t
))
1546 tpacpi_wwan_emulstate
= !!t
;
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
,
1563 if (parse_strtoul(buf
, 1, &t
))
1566 tpacpi_uwb_emulstate
= !!t
;
1570 static DRIVER_ATTR_RW(uwb_emulstate
);
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
)
1586 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1587 res
= driver_create_file(drv
, tpacpi_driver_attributes
[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
);
1605 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
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
);
1620 /*************************************************************************
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), \
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), \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1669 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
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), \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1679 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
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), \
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), \
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 */
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
));
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 ****************************************************************************
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
);
1838 static struct ibm_struct thinkpad_acpi_driver_data
= {
1840 .read
= thinkpad_acpi_driver_read
,
1843 /*************************************************************************
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
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;
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;
1982 u8 brightness_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
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
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 \
2016 mutex_lock(&hotkey_thread_data_mutex); \
2017 hotkey_config_change++; \
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)
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,
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
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)
2085 if (!tp_features
.hotkey_wlsw
)
2088 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2090 return (tpacpi_wlsw_emulstate
) ?
2091 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2094 if (!acpi_evalf(hkey_handle
, &status
, "WLSW", "d"))
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;
2112 valid_modes
= TP_ACPI_MULTI_MODE_LAPTOP
|
2113 TP_ACPI_MULTI_MODE_TABLET
|
2114 TP_ACPI_MULTI_MODE_STAND_TENT
;
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
;
2124 valid_modes
= TP_ACPI_MULTI_MODE_LAPTOP
|
2125 TP_ACPI_MULTI_MODE_FLAT
;
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
;
2139 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2140 type
, value
, TPACPI_MAIL
);
2144 if (has_tablet_mode
&& (valid_modes
& TP_ACPI_MULTI_MODE_TABLET_LIKE
))
2145 *has_tablet_mode
= 1;
2149 mode
= TP_ACPI_MULTI_MODE_LAPTOP
;
2152 mode
= TP_ACPI_MULTI_MODE_FLAT
;
2155 mode
= TP_ACPI_MULTI_MODE_TABLET
;
2159 mode
= TP_ACPI_MULTI_MODE_STAND_TENT
;
2161 mode
= TP_ACPI_MULTI_MODE_STAND
;
2164 mode
= TP_ACPI_MULTI_MODE_TENT
;
2167 if (type
== 5 && value
== 0xffff) {
2168 pr_warn("Multi mode status is undetected, assuming laptop\n");
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
);
2179 return !!(mode
& TP_ACPI_MULTI_MODE_TABLET_LIKE
);
2182 static int hotkey_get_tablet_mode(int *status
)
2186 switch (tp_features
.hotkey_tablet
) {
2187 case TP_HOTKEY_TABLET_USES_MHKG
:
2188 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
2191 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
2193 case TP_HOTKEY_TABLET_USES_GMMS
:
2194 if (!acpi_evalf(hkey_handle
, &s
, "GMMS", "dd", 0))
2197 *status
= hotkey_gmms_get_tablet_mode(s
, NULL
);
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
) {
2219 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
2222 hotkey_acpi_mask
= m
;
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
);
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
);
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
)
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
)))) {
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_
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();
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
)
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",
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
);
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
)
2329 /* Do the right thing if hotkey_init has not been called yet */
2330 if (!tp_features
.hotkey
) {
2331 hotkey_driver_mask
= mask
;
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
);
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
);
2353 static int hotkey_status_get(int *status
)
2355 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2361 static int hotkey_status_set(bool enable
)
2363 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2369 static void tpacpi_input_send_tabletsw(void)
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
)
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) \
2460 if ((event_mask & (1 << __scancode)) && \
2461 oldn->__member != newn->__member) \
2462 tpacpi_hotkey_send_key(__scancode); \
2465 #define TPACPI_MAY_SEND_KEY(__scancode) \
2467 if (event_mask & (1 << __scancode)) \
2468 tpacpi_hotkey_send_key(__scancode); \
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
);
2481 while (i
< newvol
) {
2482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
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
);
2497 while (i
< newbrt
) {
2498 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
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
);
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.
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
,
2543 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_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
,
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
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
;
2593 unsigned int change_detector
;
2594 unsigned int poll_freq
;
2597 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
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()) {
2618 if (likely(poll_freq
))
2621 t
= 100; /* should never happen... */
2623 t
= msleep_interruptible(t
);
2624 if (unlikely(kthread_freezable_should_stop(&was_frozen
)))
2627 if (t
> 0 && !was_frozen
)
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 */
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
],
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");
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
)
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);
2730 case TPACPI_LIFE_EXITING
:
2734 /* Should only happen if tpacpi_lifecycle is corrupt */
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
,
2754 printk_deprecated_attribute("hotkey_enable",
2755 "Hotkey reporting is always enabled");
2757 res
= hotkey_status_get(&status
);
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
)
2770 printk_deprecated_attribute("hotkey_enable",
2771 "Hotkeys can be disabled through hotkey_mask");
2773 if (parse_strtoul(buf
, 1, &t
))
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
,
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
)
2799 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
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);
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
,
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
,
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
,
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
,
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
,
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
,
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
)
2894 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2895 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
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
);
2916 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2919 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
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
,
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
)
2943 if (parse_strtoul(buf
, 25, &t
))
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
);
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
,
2969 res
= hotkey_get_wlsw();
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
,
2989 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2990 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2991 struct device_attribute
*attr
,
2995 res
= hotkey_get_tablet_mode(&s
);
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
,
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
,
3027 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3028 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
3029 struct device_attribute
*attr
,
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
{
3052 WEB_CONFERENCE_MODE
,
3057 static ssize_t
adaptive_kbd_mode_show(struct device
*dev
,
3058 struct device_attribute
*attr
,
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
)
3077 if (parse_strtoul(buf
, LAYFLAT_MODE
, &t
))
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
,
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
,
3114 * Sync both the hw and sw blocking state of all switches
3116 static void tpacpi_send_radiosw_update(void)
3121 * We must sync all rfkill controllers *before* issuing any
3122 * rfkill input events, or we will race the rfkill core input
3125 * tpacpi_inputdev_send_mutex works as a synchronization point
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 */
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
);
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
;
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
;
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
,
3234 if (has_tablet_mode
)
3235 tp_features
.hotkey_tablet
= TP_HOTKEY_TABLET_USES_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
);
3244 if (!tp_features
.hotkey_tablet
)
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
);
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
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.
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
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) */
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
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 */
3416 KEY_VOICECOMMAND
, /* Voice */
3418 KEY_RESERVED
, /* Gestures */
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
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
,
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)
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
);
3497 /* hotkey not supported on 570 */
3498 tp_features
.hotkey
= hkey_handle
!= NULL
;
3500 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3502 str_supported(tp_features
.hotkey
));
3504 if (!tp_features
.hotkey
)
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,
3517 if (!hotkey_dev_attributes
)
3519 res
= add_many_to_attr_set(hotkey_dev_attributes
,
3521 ARRAY_SIZE(hotkey_attributes
));
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",
3533 switch (hkeyv
>> 8) {
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
,
3543 pr_err("missing MHKA handler, please report this to %s\n",
3545 /* Fallback: pre-init for FN+F3,F4,F12 */
3546 hotkey_all_mask
= 0x080cU
;
3548 tp_features
.hotkey_mask
= 1;
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
,
3560 pr_err("missing MHKA handler, please report this to %s\n",
3562 /* Fallback: pre-init for FN+F3,F4,F12 */
3563 hotkey_all_mask
= 0x080cU
;
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
,
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
);
3583 tp_features
.has_adaptive_kbd
= false;
3584 hotkey_adaptive_all_mask
= 0x0U
;
3589 pr_err("unknown version of the HKEY interface: 0x%x\n",
3591 pr_err("please report this to %s\n", TPACPI_MAIL
);
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();
3613 hotkey_orig_mask
= hotkey_acpi_mask
;
3615 hotkey_orig_mask
= hotkey_all_mask
;
3616 hotkey_acpi_mask
= hotkey_all_mask
;
3619 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3621 tp_features
.hotkey_wlsw
= 1;
3622 radiosw_state
= !!tpacpi_wlsw_emulstate
;
3623 pr_info("radio switch emulation enabled\n");
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();
3641 tabletsw_state
= res
;
3643 res
= register_attr_set_with_sysfs(hotkey_dev_attributes
,
3644 &tpacpi_pdev
->dev
.kobj
);
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");
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
]);
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
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
);
3715 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3716 "enabling firmware HKEY event interface...\n");
3717 res
= hotkey_status_set(true);
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
) {
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);
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
[] = {
3761 /* WEB_BROWSER_MODE = 2,
3762 WEB_CONFERENCE_MODE = 3, */
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)
3780 if (!acpi_evalf(hkey_handle
, &mode
, "GTRW", "dd", 0)) {
3781 pr_err("Cannot read adaptive keyboard mode\n");
3788 static int adaptive_keyboard_set_mode(int new_mode
)
3791 new_mode
> LAYFLAT_MODE
)
3794 if (!acpi_evalf(hkey_handle
, NULL
, "STRW", "vd", new_mode
)) {
3795 pr_err("Cannot set adaptive keyboard mode\n");
3802 static int adaptive_keyboard_get_next_mode(int mode
)
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
)
3817 return adaptive_keyboard_modes
[i
];
3820 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode
)
3822 int current_mode
= 0;
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;
3832 current_mode
= adaptive_keyboard_get_mode();
3833 if (current_mode
< 0)
3835 new_mode
= adaptive_keyboard_get_next_mode(
3839 if (adaptive_keyboard_set_mode(new_mode
) < 0)
3844 case DFR_SHOW_QUICKVIEW_ROW
:
3845 current_mode
= adaptive_keyboard_get_mode();
3846 if (current_mode
< 0)
3849 adaptive_keyboard_prev_mode
= current_mode
;
3850 adaptive_keyboard_mode_is_saved
= true;
3852 if (adaptive_keyboard_set_mode (FUNCTION_MODE
) < 0)
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",
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
);
3882 static bool hotkey_notify_hotkey(const u32 hkey
,
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) {
3899 scancode
<= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START
) {
3900 /* HKEY event 0x1001 is scancode 0x00 */
3902 if (!(hotkey_source_mask
& (1 << scancode
))) {
3903 tpacpi_input_send_key_masked(scancode
);
3904 *send_acpi_ev
= false;
3906 *ignore_acpi_ev
= true;
3913 return adaptive_keyboard_hotkey_notify_hotkey(scancode
);
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
);
3932 static bool hotkey_notify_wakeup(const u32 hkey
,
3934 bool *ignore_acpi_ev
)
3936 /* 0x2000-0x2FFF: Wakeup reason */
3937 *send_acpi_ev
= true;
3938 *ignore_acpi_ev
= false;
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;
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;
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 */
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();
3972 static bool hotkey_notify_dockevent(const u32 hkey
,
3974 bool *ignore_acpi_ev
)
3976 /* 0x4000-0x4FFF: dock-related events */
3977 *send_acpi_ev
= true;
3978 *ignore_acpi_ev
= false;
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();
3988 case TP_HKEY_EV_HOTPLUG_DOCK
: /* docked to port replicator */
3989 pr_info("docked into hotplug port replicator\n");
3991 case TP_HKEY_EV_HOTPLUG_UNDOCK
: /* undocked from port replicator */
3992 pr_info("undocked from hotplug port replicator\n");
4000 static bool hotkey_notify_usrevent(const u32 hkey
,
4002 bool *ignore_acpi_ev
)
4004 /* 0x5000-0x5FFF: human interface helpers */
4005 *send_acpi_ev
= true;
4006 *ignore_acpi_ev
= false;
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 */
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;
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;
4032 static void thermal_dump_all_sensors(void);
4034 static bool hotkey_notify_6xxx(const u32 hkey
,
4036 bool *ignore_acpi_ev
)
4038 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4039 *send_acpi_ev
= true;
4040 *ignore_acpi_ev
= false;
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 */
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 */
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 */
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 */
4062 case TP_HKEY_EV_ALARM_BAT_XHOT
:
4063 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4064 /* recommended action: immediate sleep/hibernate */
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 */
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 */
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. */
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;
4091 case TP_HKEY_EV_TABLET_CHANGED
:
4092 tpacpi_input_send_tabletsw();
4093 hotkey_tablet_mode_notify_change();
4094 *send_acpi_ev
= false;
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 */
4104 /* report simply as unknown, no sensor dump */
4108 thermal_dump_all_sensors();
4112 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
4116 bool ignore_acpi_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
),
4130 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
4131 pr_err("failed to retrieve HKEY event\n");
4140 send_acpi_ev
= true;
4141 ignore_acpi_ev
= false;
4143 switch (hkey
>> 12) {
4145 /* 0x1000-0x1FFF: key presses */
4146 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
4150 /* 0x2000-0x2FFF: Wakeup reason */
4151 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
4155 /* 0x3000-0x3FFF: bay-related wakeups */
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();
4163 case TP_HKEY_EV_OPTDRV_EJ
:
4164 /* FIXME: kick libata if SATA link offline */
4172 /* 0x4000-0x4FFF: dock-related events */
4173 known_ev
= hotkey_notify_dockevent(hkey
, &send_acpi_ev
,
4177 /* 0x5000-0x5FFF: human interface helpers */
4178 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
4182 /* 0x6000-0x6FFF: thermal alarms/notices and
4183 * keyboard events */
4184 known_ev
= hotkey_notify_6xxx(hkey
, &send_acpi_ev
,
4188 /* 0x7000-0x7FFF: misc */
4189 if (tp_features
.hotkey_wlsw
&&
4190 hkey
== TP_HKEY_EV_RFKILL_CHANGED
) {
4191 tpacpi_send_radiosw_update();
4196 /* fallthrough - to default */
4201 pr_notice("unhandled HKEY event 0x%04x\n", hkey
);
4202 pr_notice("please report the conditions when this event happened to %s\n",
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
),
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
,
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
)
4259 if (!tp_features
.hotkey
) {
4260 seq_printf(m
, "status:\t\tnot supported\n");
4264 if (mutex_lock_killable(&hotkey_mutex
))
4265 return -ERESTARTSYS
;
4266 res
= hotkey_status_get(&status
);
4268 res
= hotkey_mask_get();
4269 mutex_unlock(&hotkey_mutex
);
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");
4278 seq_printf(m
, "mask:\t\tnot supported\n");
4279 seq_printf(m
, "commands:\tenable, disable, reset\n");
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
)
4299 if (!tp_features
.hotkey
)
4302 if (mutex_lock_killable(&hotkey_mutex
))
4303 return -ERESTARTSYS
;
4305 mask
= hotkey_user_mask
;
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);
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) {
4319 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
4328 tpacpi_disclose_usertask("procfs hotkey",
4329 "set mask to 0x%08x\n", mask
);
4330 res
= hotkey_user_mask_set(mask
);
4334 mutex_unlock(&hotkey_mutex
);
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},
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
= {
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
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 */
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)
4389 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4390 if (dbg_bluetoothemul
)
4391 return (tpacpi_bluetooth_emulstate
) ?
4392 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4395 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
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
)
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
);
4417 if (state
== TPACPI_RFK_RADIO_ON
)
4418 status
= TP_ACPI_BLUETOOTH_RADIOSSW
4419 | TP_ACPI_BLUETOOTH_RESUMECTRL
;
4423 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
4429 /* sysfs bluetooth enable ---------------------------------------------- */
4430 static ssize_t
bluetooth_enable_show(struct device
*dev
,
4431 struct device_attribute
*attr
,
4434 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID
,
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
,
4446 static DEVICE_ATTR_RW(bluetooth_enable
);
4448 /* --------------------------------------------------------------------- */
4450 static struct attribute
*bluetooth_attributes
[] = {
4451 &dev_attr_bluetooth_enable
.attr
,
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");
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",
4489 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
4490 DMI_MATCH(DMI_BOARD_NAME
, "20KU"),
4494 .ident
= "ThinkPad E585",
4496 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
4497 DMI_MATCH(DMI_BOARD_NAME
, "20KV"),
4501 .ident
= "ThinkPad A285 - 20MW",
4503 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
4504 DMI_MATCH(DMI_BOARD_NAME
, "20MW"),
4508 .ident
= "ThinkPad A285 - 20MX",
4510 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
4511 DMI_MATCH(DMI_BOARD_NAME
, "20MX"),
4515 .ident
= "ThinkPad A485 - 20MU",
4517 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
4518 DMI_MATCH(DMI_BOARD_NAME
, "20MU"),
4522 .ident
= "ThinkPad A485 - 20MV",
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");
4553 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
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
),
4573 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4574 if (dbg_bluetoothemul
) {
4575 tp_features
.bluetooth
= 1;
4576 pr_info("bluetooth switch emulation enabled\n");
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
)
4590 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
4591 &bluetooth_tprfk_ops
,
4592 RFKILL_TYPE_BLUETOOTH
,
4593 TPACPI_RFK_BLUETOOTH_SW_NAME
,
4598 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4599 &bluetooth_attr_group
);
4601 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
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 /*************************************************************************
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)
4645 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4647 return (tpacpi_wwan_emulstate
) ?
4648 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4651 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
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
)
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
4668 tpacpi_wwan_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4673 if (state
== TPACPI_RFK_RADIO_ON
)
4674 status
= TP_ACPI_WANCARD_RADIOSSW
4675 | TP_ACPI_WANCARD_RESUMECTRL
;
4679 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
4685 /* sysfs wan enable ---------------------------------------------------- */
4686 static ssize_t
wan_enable_show(struct device
*dev
,
4687 struct device_attribute
*attr
,
4690 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID
,
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
,
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
,
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");
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
,
4737 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4742 static int __init
wan_init(struct ibm_init_struct
*iibm
)
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
),
4760 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4762 tp_features
.wan
= 1;
4763 pr_info("wwan switch emulation enabled\n");
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
)
4777 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
4780 TPACPI_RFK_WWAN_SW_NAME
,
4785 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4789 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
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
= {
4812 .shutdown
= wan_shutdown
,
4815 /*************************************************************************
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)
4831 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4833 return (tpacpi_uwb_emulstate
) ?
4834 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4837 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
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
)
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
4854 tpacpi_uwb_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4859 if (state
== TPACPI_RFK_RADIO_ON
)
4860 status
= TP_ACPI_UWB_RADIOSSW
;
4864 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
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
)
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
),
4900 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4902 tp_features
.uwb
= 1;
4903 pr_info("uwb switch emulation enabled\n");
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
)
4917 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
4920 TPACPI_RFK_UWB_SW_NAME
,
4925 static struct ibm_struct uwb_driver_data
= {
4928 .flags
.experimental
= 1,
4931 /*************************************************************************
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 */
4973 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4975 static int __init
video_init(struct ibm_init_struct
*iibm
)
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
;
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"))
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
;
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
),
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)
5024 switch (video_supported
) {
5025 case TPACPI_VIDEO_570
:
5026 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
5027 TP_ACPI_VIDEO_570_PHSCMD
))
5029 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
5031 case TPACPI_VIDEO_770
:
5032 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
5035 status
|= TP_ACPI_VIDEO_S_LCD
;
5036 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
5039 status
|= TP_ACPI_VIDEO_S_CRT
;
5041 case TPACPI_VIDEO_NEW
:
5042 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
5043 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
5046 status
|= TP_ACPI_VIDEO_S_CRT
;
5048 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
5049 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
5052 status
|= TP_ACPI_VIDEO_S_LCD
;
5053 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
5056 status
|= TP_ACPI_VIDEO_S_DVI
;
5065 static int video_outputsw_set(int status
)
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
);
5077 case TPACPI_VIDEO_770
:
5078 autosw
= video_autosw_get();
5082 res
= video_autosw_set(1);
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");
5092 case TPACPI_VIDEO_NEW
:
5093 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
5094 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
5100 return (res
) ? 0 : -EIO
;
5103 static int video_autosw_get(void)
5107 switch (video_supported
) {
5108 case TPACPI_VIDEO_570
:
5109 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
5112 case TPACPI_VIDEO_770
:
5113 case TPACPI_VIDEO_NEW
:
5114 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
5124 static int video_autosw_set(int enable
)
5126 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
) ? 1 : 0))
5131 static int video_outputsw_cycle(void)
5133 int autosw
= video_autosw_get();
5139 switch (video_supported
) {
5140 case TPACPI_VIDEO_570
:
5141 res
= video_autosw_set(1);
5144 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
5146 case TPACPI_VIDEO_770
:
5147 case TPACPI_VIDEO_NEW
:
5148 res
= video_autosw_set(1);
5151 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
5156 if (!autosw
&& video_autosw_set(autosw
)) {
5157 pr_err("video auto-switch left enabled due to error\n");
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") ?
5170 case TPACPI_VIDEO_770
:
5171 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v") ?
5173 case TPACPI_VIDEO_NEW
:
5174 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v") ?
5182 static int video_read(struct seq_file
*m
)
5186 if (video_supported
== TPACPI_VIDEO_NONE
) {
5187 seq_printf(m
, "status:\t\tnot supported\n");
5191 /* Even reads can crash X.org, so... */
5192 if (!capable(CAP_SYS_ADMIN
))
5195 status
= video_outputsw_get();
5199 autosw
= video_autosw_get();
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");
5219 static int video_write(char *buf
)
5222 int enable
, disable
, status
;
5225 if (video_supported
== TPACPI_VIDEO_NONE
)
5228 /* Even reads can crash X.org, let alone writes... */
5229 if (!capable(CAP_SYS_ADMIN
))
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);
5254 } else if (strlencmp(cmd
, "auto_disable") == 0) {
5255 res
= video_autosw_set(0);
5258 } else if (strlencmp(cmd
, "video_switch") == 0) {
5259 res
= video_outputsw_cycle();
5262 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
5263 res
= video_expand_toggle();
5270 if (enable
|| disable
) {
5271 status
= video_outputsw_get();
5274 res
= video_outputsw_set((status
& ~disable
) | enable
);
5282 static struct ibm_struct video_driver_data
= {
5285 .write
= video_write
,
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
)
5305 mutex_lock(&kbdlight_mutex
);
5307 if (!acpi_evalf(hkey_handle
, NULL
, "MLCS", "dd", level
))
5310 kbdlight_brightness
= level
;
5312 mutex_unlock(&kbdlight_mutex
);
5317 static int kbdlight_get_level(void)
5324 if (!acpi_evalf(hkey_handle
, &status
, "MLCG", "dd", 0))
5330 return status
& 0x3;
5333 static bool kbdlight_is_supported(void)
5340 if (!acpi_has_method(hkey_handle
, "MLCG")) {
5341 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG is unavailable\n");
5345 if (!acpi_evalf(hkey_handle
, &status
, "MLCG", "qdd", 0)) {
5346 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG failed\n");
5351 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG err: %d\n", status
);
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
)
5388 level
= kbdlight_get_level();
5395 static struct tpacpi_led_classdev tpacpi_led_kbdlight
= {
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
)
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");
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
);
5425 tp_features
.kbdlight
= 0;
5429 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
|
5430 TP_ACPI_HKEY_KBD_LIGHT_MASK
);
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
)
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
;
5454 static int kbdlight_read(struct seq_file
*m
)
5458 if (!tp_features
.kbdlight
) {
5459 seq_printf(m
, "status:\t\tnot supported\n");
5461 level
= kbdlight_get_level();
5463 seq_printf(m
, "status:\t\terror %d\n", level
);
5465 seq_printf(m
, "status:\t\t%d\n", level
);
5466 seq_printf(m
, "commands:\t0, 1, 2\n");
5472 static int kbdlight_write(char *buf
)
5477 if (!tp_features
.kbdlight
)
5480 while ((cmd
= next_cmd(&buf
))) {
5481 if (strlencmp(cmd
, "0") == 0)
5483 else if (strlencmp(cmd
, "1") == 0)
5485 else if (strlencmp(cmd
, "2") == 0)
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
)
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
)
5514 led_classdev_resume(&tpacpi_led_kbdlight
.led_classdev
);
5517 static struct ibm_struct kbdlight_driver_data
= {
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)
5537 if (tp_features
.light_status
) {
5538 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
5546 static int light_set_status(int status
)
5550 if (tp_features
.light
) {
5552 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
5554 TP_CMOS_THINKLIGHT_ON
:
5555 TP_CMOS_THINKLIGHT_OFF
);
5557 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
5560 return (rc
) ? 0 : -EIO
;
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
= {
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
)
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
)
5614 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5615 &tpacpi_led_thinklight
.led_classdev
);
5618 tp_features
.light
= 0;
5619 tp_features
.light_status
= 0;
5627 static void light_exit(void)
5629 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
5632 static int light_read(struct seq_file
*m
)
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");
5642 status
= light_get_status();
5645 seq_printf(m
, "status:\t\t%s\n", onoff(status
, 0));
5646 seq_printf(m
, "commands:\ton, off\n");
5652 static int light_write(char *buf
)
5657 if (!tp_features
.light
)
5660 while ((cmd
= next_cmd(&buf
))) {
5661 if (strlencmp(cmd
, "on") == 0) {
5663 } else if (strlencmp(cmd
, "off") == 0) {
5669 return light_set_status(newstatus
);
5672 static struct ibm_struct light_driver_data
= {
5675 .write
= light_write
,
5679 /*************************************************************************
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
;
5691 if (parse_strtoul(buf
, 21, &cmos_cmd
))
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
)
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
);
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 */
5731 seq_printf(m
, "status:\t\tnot supported\n");
5733 seq_printf(m
, "status:\t\tsupported\n");
5734 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-21)\n");
5740 static int cmos_write(char *buf
)
5745 while ((cmd
= next_cmd(&buf
))) {
5746 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
5747 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
5752 res
= issue_thinkpad_cmos_command(cmos_cmd
);
5760 static struct ibm_struct cmos_driver_data
= {
5763 .write
= cmos_write
,
5767 /*************************************************************************
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 */
5794 "tpacpi:orange:batt",
5795 "tpacpi:green:batt",
5796 "tpacpi::dock_active",
5797 "tpacpi::bay_active",
5798 "tpacpi::dock_batt",
5799 "tpacpi::unknown_led",
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
5814 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
5818 static int led_get_status(const unsigned int led
)
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
))
5828 led_s
= (status
== 0) ?
5833 tpacpi_led_state_cache
[led
] = led_s
;
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 };
5851 switch (led_supported
) {
5852 case TPACPI_LED_570
:
5854 if (unlikely(led
> 7))
5856 if (unlikely(tpacpi_is_led_restricted(led
)))
5858 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5859 (1 << led
), led_sled_arg1
[ledstatus
]))
5862 case TPACPI_LED_OLD
:
5863 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5864 if (unlikely(led
> 7))
5866 if (unlikely(tpacpi_is_led_restricted(led
)))
5868 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
5870 rc
= ec_write(TPACPI_LED_EC_HLBL
,
5871 (ledstatus
== TPACPI_LED_BLINK
) << led
);
5873 rc
= ec_write(TPACPI_LED_EC_HLCL
,
5874 (ledstatus
!= TPACPI_LED_OFF
) << led
);
5876 case TPACPI_LED_NEW
:
5878 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
5880 if (unlikely(tpacpi_is_led_restricted(led
)))
5882 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5883 led
, led_led_arg1
[ledstatus
]))
5891 tpacpi_led_state_cache
[led
] = ledstatus
;
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
;
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))
5927 return led_set_status(data
->led
, TPACPI_LED_BLINK
);
5930 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
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 :( */
5947 static void led_exit(void)
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
);
5959 static int __init
tpacpi_init_led(unsigned int led
)
5963 tpacpi_leds
[led
].led
= led
;
5965 /* LEDs with no name don't get registered */
5966 if (!tpacpi_led_names
[led
])
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
=
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
);
5980 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
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!) */
6014 .vendor
= PCI_VENDOR_ID_LENOVO
,
6015 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
6018 { /* IBM ThinkPads with no EC version string */
6019 .vendor
= PCI_VENDOR_ID_IBM
,
6020 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
6023 { /* IBM ThinkPads with EC version string */
6024 .vendor
= PCI_VENDOR_ID_IBM
,
6025 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
6030 static enum led_access_mode __init
led_init_detect_mode(void)
6034 if (tpacpi_is_ibm()) {
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
;
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 */
6053 return TPACPI_LED_NONE
;
6056 static int __init
led_init(struct ibm_init_struct
*iibm
)
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
));
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
)
6082 tpacpi_leds
= kcalloc(TPACPI_LED_NUMLEDS
, sizeof(*tpacpi_leds
),
6085 pr_err("Out of memory for LED data\n");
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
);
6102 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6103 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
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");
6118 seq_printf(m
, "status:\t\tsupported\n");
6120 if (led_supported
== TPACPI_LED_570
) {
6123 for (i
= 0; i
< 8; i
++) {
6124 status
= led_get_status(i
);
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");
6137 static int led_write(char *buf
)
6141 enum led_status_t s
;
6146 while ((cmd
= next_cmd(&buf
))) {
6147 if (sscanf(cmd
, "%d", &led
) != 1)
6150 if (led
< 0 || led
> (TPACPI_LED_NUMLEDS
- 1) ||
6151 tpacpi_leds
[led
].led
< 0)
6154 if (strstr(cmd
, "off")) {
6156 } else if (strstr(cmd
, "on")) {
6158 } else if (strstr(cmd
, "blink")) {
6159 s
= TPACPI_LED_BLINK
;
6164 rc
= led_set_status(led
, s
);
6172 static struct ibm_struct led_driver_data
= {
6179 /*************************************************************************
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
)
6214 seq_printf(m
, "status:\t\tnot supported\n");
6216 seq_printf(m
, "status:\t\tsupported\n");
6217 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-17)\n");
6223 static int beep_write(char *buf
)
6231 while ((cmd
= next_cmd(&buf
))) {
6232 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
6233 beep_cmd
>= 0 && beep_cmd
<= 17) {
6237 if (tp_features
.beep_needs_two_args
) {
6238 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
6242 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
6251 static struct ibm_struct beep_driver_data
= {
6254 .write
= beep_write
,
6257 /*************************************************************************
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
)
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
;
6303 case TPACPI_THERMAL_TPEC_8
:
6305 if (!acpi_ec_read(t
+ idx
, &tmp
))
6307 *value
= tmp
* 1000;
6312 case TPACPI_THERMAL_ACPI_UPDT
:
6314 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
6315 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
6317 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
6319 *value
= (t
- 2732) * 100;
6324 case TPACPI_THERMAL_ACPI_TMP07
:
6326 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
6327 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
6329 if (t
> 127 || t
< -127)
6330 t
= TP_EC_THERMAL_TMP_NA
;
6336 case TPACPI_THERMAL_NONE
:
6344 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
6355 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
6358 for (i
= 0 ; i
< n
; i
++) {
6359 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
6367 static void thermal_dump_all_sensors(void)
6370 struct ibm_thermal_sensors_struct t
;
6372 n
= thermal_get_sensors(&t
);
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));
6388 /* sysfs temp##_input -------------------------------------------------- */
6390 static ssize_t
thermal_temp_input_show(struct device
*dev
,
6391 struct device_attribute
*attr
,
6394 struct sensor_device_attribute
*sensor_attr
=
6395 to_sensor_dev_attr(attr
);
6396 int idx
= sensor_attr
->index
;
6400 res
= thermal_get_sensor(idx
, &value
);
6403 if (value
== TPACPI_THERMAL_SENSOR_NA
)
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
[] = {
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
)
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
6488 for (i
= 0; i
< 8; i
++) {
6489 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
6495 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
6503 /* This is sheer paranoia, but we handle it anyway */
6505 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6506 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
6508 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6509 thermal_read_mode
= TPACPI_THERMAL_NONE
;
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
;
6522 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6523 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
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
),
6534 switch (thermal_read_mode
) {
6535 case TPACPI_THERMAL_TPEC_16
:
6536 res
= sysfs_create_group(&tpacpi_hwmon
->kobj
,
6537 &thermal_temp_input16_group
);
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
);
6549 case TPACPI_THERMAL_NONE
:
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
);
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
);
6570 case TPACPI_THERMAL_NONE
:
6576 static int thermal_read(struct seq_file
*m
)
6579 struct ibm_thermal_sensors_struct t
;
6581 n
= thermal_get_sensors(&t
);
6582 if (unlikely(n
< 0))
6585 seq_printf(m
, "temperatures:\t");
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);
6592 seq_printf(m
, "not supported\n");
6597 static struct ibm_struct thermal_driver_data
= {
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...
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)
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
;
6667 static void tpacpi_brightness_checkpoint_nvram(void)
6672 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
6675 vdbg_printk(TPACPI_DBG_BRGHT
,
6676 "trying to checkpoint backlight level to NVRAM...\n");
6678 if (mutex_lock_killable(&brightness_mutex
) < 0)
6681 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
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
);
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
);
6697 vdbg_printk(TPACPI_DBG_BRGHT
,
6698 "NVRAM backlight level already is %u (0x%02x)\n",
6699 (unsigned int) lec
, (unsigned int) b_nvram
);
6702 mutex_unlock(&brightness_mutex
);
6706 /* call with brightness_mutex held! */
6707 static int tpacpi_brightness_get_raw(int *status
)
6711 switch (brightness_mode
) {
6712 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6713 *status
= tpacpi_brightness_nvram_get();
6715 case TPACPI_BRGHT_MODE_EC
:
6716 case TPACPI_BRGHT_MODE_ECNVRAM
:
6717 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
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
)
6732 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6735 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
6736 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
6737 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
6743 /* call with brightness_mutex held! */
6744 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
6747 unsigned int current_value
, i
;
6749 current_value
= tpacpi_brightness_nvram_get();
6751 if (value
== current_value
)
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
))
6766 /* May return EINTR which can always be mapped to ERESTARTSYS */
6767 static int brightness_set(unsigned int value
)
6771 if (value
> bright_maxlvl
)
6774 vdbg_printk(TPACPI_DBG_BRGHT
,
6775 "set backlight level to %d\n", value
);
6777 res
= mutex_lock_killable(&brightness_mutex
);
6781 switch (brightness_mode
) {
6782 case TPACPI_BRGHT_MODE_EC
:
6783 case TPACPI_BRGHT_MODE_ECNVRAM
:
6784 res
= tpacpi_brightness_set_ec(value
);
6786 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6787 res
= tpacpi_brightness_set_ucmsstep(value
);
6793 mutex_unlock(&brightness_mutex
);
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",
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
)
6819 res
= mutex_lock_killable(&brightness_mutex
);
6823 res
= tpacpi_brightness_get_raw(&status
);
6825 mutex_unlock(&brightness_mutex
);
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
;
6858 if (acpi_bus_get_device(handle
, &device
))
6862 list_for_each_entry(child
, &device
->children
, node
) {
6863 acpi_status status
= acpi_evaluate_object(child
->handle
, "_BCL",
6865 if (ACPI_FAILURE(status
))
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",
6874 rc
= obj
->package
.count
;
6879 kfree(buffer
.pointer
);
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
;
6892 tpacpi_acpi_handle_locate("video", NULL
, &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
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)
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();
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
;
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
)
6985 if (!brightness_enable
) {
6986 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6987 "brightness support disabled by module parameter\n");
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");
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");
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
)
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
;
7017 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
7019 dbg_printk(TPACPI_DBG_BRGHT
,
7020 "driver auto-selected brightness_mode=%d\n",
7025 if (!tpacpi_is_ibm() &&
7026 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
7027 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
7030 if (tpacpi_brightness_get_raw(&b
) < 0)
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
,
7039 &ibm_backlight_data
,
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");
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",
7053 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
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
);
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
)
7096 level
= brightness_get(NULL
);
7098 seq_printf(m
, "level:\t\tunreadable\n");
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",
7109 static int brightness_write(char *buf
)
7115 level
= brightness_get(NULL
);
7119 while ((cmd
= next_cmd(&buf
))) {
7120 if (strlencmp(cmd
, "up") == 0) {
7121 if (level
< bright_maxlvl
)
7123 } else if (strlencmp(cmd
, "down") == 0) {
7126 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
7127 level
>= 0 && level
<= bright_maxlvl
) {
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 /*************************************************************************
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.
7168 * Bit 6: MUTE (1 mutes sound)
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)
7198 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
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
;
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 */
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 */
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)
7269 if (volume_mode
!= TPACPI_VOL_MODE_ECNVRAM
)
7271 if (!volume_control_allowed
)
7273 if (software_mute_active
)
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
;
7282 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
| TP_EC_AUDIO_LVL_MSK
;
7284 if (mutex_lock_killable(&volume_mutex
) < 0)
7287 if (unlikely(!acpi_ec_read(TP_EC_AUDIO
, &lec
)))
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
;
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
);
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
);
7307 mutex_unlock(&volume_mutex
);
7310 static int volume_get_status_ec(u8
*status
)
7314 if (!acpi_ec_read(TP_EC_AUDIO
, &s
))
7319 dbg_printk(TPACPI_DBG_MIXER
, "status 0x%02x\n", s
);
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
))
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.
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
)
7356 if (mutex_lock_killable(&volume_mutex
) < 0)
7359 rc
= volume_get_status_ec(&s
);
7363 n
= (mute
) ? s
| TP_EC_AUDIO_MUTESW_MSK
:
7364 s
& ~TP_EC_AUDIO_MUTESW_MSK
;
7367 rc
= volume_set_status_ec(n
);
7373 mutex_unlock(&volume_mutex
);
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
)
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
)
7401 if (vol
> TP_EC_VOLUME_MAX
)
7404 if (mutex_lock_killable(&volume_mutex
) < 0)
7407 rc
= volume_get_status_ec(&s
);
7411 n
= (s
& ~TP_EC_AUDIO_LVL_MSK
) | vol
;
7414 rc
= volume_set_status_ec(n
);
7420 mutex_unlock(&volume_mutex
);
7424 static int volume_set_software_mute(bool startup
)
7428 if (!tpacpi_is_lenovo())
7432 if (!acpi_evalf(ec_handle
, &software_mute_orig_mode
,
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
))
7445 if (result
!= TP_EC_MUTE_BTN_NONE
)
7446 pr_warn("Unexpected SAUM result %d\n",
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);
7458 result
= volume_set_status(TP_EC_VOLUME_MAX
);
7461 pr_warn("Failed to unmute the HW mute switch\n");
7466 static void volume_exit_software_mute(void)
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
;
7489 snd_ctl_notify(alsa_card
,
7490 SNDRV_CTL_EVENT_MASK_VALUE
,
7493 snd_ctl_notify(alsa_card
,
7494 SNDRV_CTL_EVENT_MASK_VALUE
,
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
;
7504 uinfo
->value
.integer
.min
= 0;
7505 uinfo
->value
.integer
.max
= TP_EC_VOLUME_MAX
;
7509 static int volume_alsa_vol_get(struct snd_kcontrol
*kcontrol
,
7510 struct snd_ctl_elem_value
*ucontrol
)
7515 rc
= volume_get_status(&s
);
7519 ucontrol
->value
.integer
.value
[0] = s
& TP_EC_AUDIO_LVL_MSK
;
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
)
7539 rc
= volume_get_status(&s
);
7543 ucontrol
->value
.integer
.value
[0] =
7544 (s
& TP_EC_AUDIO_MUTESW_MSK
) ? 0 : 1;
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] ?
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",
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",
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");
7586 volume_alsa_notify_change();
7590 static void volume_shutdown(void)
7592 tpacpi_volume_checkpoint_nvram();
7595 static void volume_exit(void)
7598 snd_card_free(alsa_card
);
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
;
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
);
7624 BUG_ON(!card
->private_data
);
7625 data
= card
->private_data
;
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
);
7654 pr_err("Failed to create ALSA volume control: %d\n",
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
);
7664 pr_err("Failed to create ALSA mute control: %d\n", rc
);
7667 data
->ctl_mute_id
= &ctl_mute
->id
;
7669 rc
= snd_card_register(card
);
7671 pr_err("Failed to register ALSA card: %d\n", rc
);
7679 snd_card_free(card
);
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
;
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
)
7726 if (volume_mode
== TPACPI_VOL_MODE_UCMS_STEP
) {
7727 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7732 if (volume_capabilities
>= TPACPI_VOL_CAP_MAX
)
7736 * The ALSA mixer is our primary interface.
7737 * When disabled, don't install the subdriver at all
7740 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7741 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
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;
7755 return 1; /* no mixer */
7757 case TPACPI_VOL_CAP_VOLMUTE
:
7758 tp_features
.mixer_no_level_control
= 0;
7760 case TPACPI_VOL_CAP_MUTEONLY
:
7761 tp_features
.mixer_no_level_control
= 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",
7780 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7781 "using user-supplied volume_mode=%d\n",
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;
7792 rc
= volume_create_alsa_mixer();
7794 pr_err("Could not create the ALSA mixer interface\n");
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
);
7814 static int volume_read(struct seq_file
*m
)
7818 if (volume_get_status(&status
) < 0) {
7819 seq_printf(m
, "level:\t\tunreadable\n");
7821 if (tp_features
.mixer_no_level_control
)
7822 seq_printf(m
, "level:\t\tunsupported\n");
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",
7843 static int volume_write(char *buf
)
7846 u8 new_level
, new_mute
;
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");
7864 rc
= volume_get_status(&s
);
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) {
7876 else if (new_level
< TP_EC_VOLUME_MAX
)
7879 } else if (strlencmp(cmd
, "down") == 0) {
7882 else if (new_level
> 0)
7885 } else if (sscanf(cmd
, "level %u", &l
) == 1 &&
7886 l
>= 0 && l
<= TP_EC_VOLUME_MAX
) {
7891 if (strlencmp(cmd
, "mute") == 0)
7892 new_mute
= TP_EC_AUDIO_MUTESW_MSK
;
7893 else if (strlencmp(cmd
, "unmute") == 0)
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
);
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
= {
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");
7939 static struct ibm_struct volume_driver_data
= {
7943 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7945 /*************************************************************************
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
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
7973 * Reading is not available if GFAN exists.
7974 * Writing is not available if SFAN exists.
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)
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
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
8022 * FIRMWARE BUG: may go stale while the EC is switching to full speed
8025 * For firmware bugs, refer to:
8026 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
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
8037 * Fan control seems to affect both fans, regardless of the state
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
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 */
8118 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
8119 "JFNS", /* 770x-JL */
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
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
8153 tp_features
.fan_ctrl_status_undef
= 0;
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
) {
8168 if (ec_read(fan_select_offset
, &val
) < 0)
8171 if (ec_write(fan_select_offset
, val
) < 0)
8177 /* Select secondary fan on X60/X61 */
8178 static bool fan_select_fan2(void)
8182 if (!tp_features
.second_fan
)
8185 if (ec_read(fan_select_offset
, &val
) < 0)
8188 if (ec_write(fan_select_offset
, val
) < 0)
8195 * Call with fan_mutex held
8197 static void fan_update_desired_level(u8 status
)
8200 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
8202 fan_control_desired_level
= 7;
8204 fan_control_desired_level
= status
;
8208 static int fan_get_status(u8
*status
)
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 */
8220 if (unlikely(!acpi_evalf(gfan_handle
, &res
, NULL
, "d")))
8224 *status
= res
& 0x07;
8228 case TPACPI_FAN_RD_TPEC
:
8229 /* all except 570, 600e/x, 770e, 770x */
8230 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
8233 if (likely(status
)) {
8235 fan_quirk1_handle(status
);
8247 static int fan_get_status_safe(u8
*status
)
8252 if (mutex_lock_killable(&fan_mutex
))
8253 return -ERESTARTSYS
;
8254 rc
= fan_get_status(&s
);
8256 fan_update_desired_level(s
);
8257 mutex_unlock(&fan_mutex
);
8267 static int fan_get_speed(unsigned int *speed
)
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()))
8276 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
8277 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
8281 *speed
= (hi
<< 8) | lo
;
8292 static int fan2_get_speed(unsigned int *speed
)
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()))
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 */
8309 *speed
= (hi
<< 8) | lo
;
8320 static int fan_set_level(int level
)
8322 if (!fan_control_allowed
)
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
))
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)))
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
))
8351 tp_features
.fan_ctrl_status_undef
= 0;
8358 vdbg_printk(TPACPI_DBG_FAN
,
8359 "fan control: set fan control register to 0x%02x\n", level
);
8363 static int fan_set_level_safe(int level
)
8367 if (!fan_control_allowed
)
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
);
8378 fan_update_desired_level(level
);
8380 mutex_unlock(&fan_mutex
);
8384 static int fan_set_enable(void)
8389 if (!fan_control_allowed
)
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
);
8402 /* Don't go out of emergency fan mode */
8405 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
8408 if (!acpi_ec_write(fan_status_offset
, s
))
8411 tp_features
.fan_ctrl_status_undef
= 0;
8416 case TPACPI_FAN_WR_ACPI_SFAN
:
8417 rc
= fan_get_status(&s
);
8423 /* Set fan to at least level 4 */
8426 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
8436 mutex_unlock(&fan_mutex
);
8439 vdbg_printk(TPACPI_DBG_FAN
,
8440 "fan control: set fan control register to 0x%02x\n",
8445 static int fan_set_disable(void)
8449 if (!fan_control_allowed
)
8452 if (mutex_lock_killable(&fan_mutex
))
8453 return -ERESTARTSYS
;
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))
8462 fan_control_desired_level
= 0;
8463 tp_features
.fan_ctrl_status_undef
= 0;
8467 case TPACPI_FAN_WR_ACPI_SFAN
:
8468 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
8471 fan_control_desired_level
= 0;
8479 vdbg_printk(TPACPI_DBG_FAN
,
8480 "fan control: set fan control register to 0\n");
8482 mutex_unlock(&fan_mutex
);
8486 static int fan_set_speed(int speed
)
8490 if (!fan_control_allowed
)
8493 if (mutex_lock_killable(&fan_mutex
))
8494 return -ERESTARTSYS
;
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
))
8511 mutex_unlock(&fan_mutex
);
8515 static void fan_watchdog_reset(void)
8517 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
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));
8525 cancel_delayed_work(&fan_watchdog_task
);
8528 static void fan_watchdog_fire(struct work_struct
*ignored
)
8532 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
8535 pr_notice("fan watchdog: enabling fan\n");
8536 rc
= fan_set_enable();
8538 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8540 /* reschedule for later */
8541 fan_watchdog_reset();
8546 * SYSFS fan layout: hwmon compatible (device)
8549 * 0: "disengaged" 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
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
,
8574 res
= fan_get_status_safe(&status
);
8578 if (status
& TP_EC_FAN_FULLSPEED
) {
8580 } else if (status
& TP_EC_FAN_AUTO
) {
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
)
8595 if (parse_strtoul(buf
, 2, &t
))
8598 tpacpi_disclose_usertask("hwmon pwm1_enable",
8599 "set fan mode to %lu\n", t
);
8603 level
= TP_EC_FAN_FULLSPEED
;
8606 level
= TPACPI_FAN_LAST_LEVEL
;
8609 level
= TP_EC_FAN_AUTO
;
8612 /* reserved for software-controlled auto mode */
8618 res
= fan_set_level_safe(level
);
8624 fan_watchdog_reset();
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
,
8640 res
= fan_get_status_safe(&status
);
8645 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
8646 status
= fan_control_desired_level
;
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
)
8660 u8 status
, newlevel
;
8662 if (parse_strtoul(buf
, 255, &s
))
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
);
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
,
8700 res
= fan_get_speed(&speed
);
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
,
8717 res
= fan2_get_speed(&speed
);
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
,
8737 if (parse_strtoul(buf
, 120, &t
))
8740 if (!fan_control_allowed
)
8743 fan_watchdog_maxinterval
= t
;
8744 fan_watchdog_reset();
8746 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
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 */
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
)
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
));
8803 /* 570, 600e/x, 770e, 770x */
8804 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
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
)
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");
8819 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8826 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
8827 fan_control_commands
|=
8828 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
8831 /* gfan without sfan means no fan control */
8832 /* all other models implement TP EC 0x2f control */
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
;
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
,
8881 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
8882 &driver_attr_fan_watchdog
);
8884 sysfs_remove_group(&tpacpi_hwmon
->kobj
,
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)
8911 if (!fan_control_allowed
)
8914 /* Store fan status in cache */
8915 fan_control_resume_level
= 0;
8916 rc
= fan_get_status_safe(&fan_control_resume_level
);
8918 pr_notice("failed to read fan level for later restore during resume: %d\n",
8921 /* if it is undefined, don't attempt to restore it.
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;
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(¤t_level
) < 0))
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
);
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
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
))
8967 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
8968 (current_level
!= fan_control_resume_level
);
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
);
8978 pr_notice("failed to restore fan level: %d\n", rc
);
8982 static int fan_read(struct seq_file
*m
)
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
);
8995 seq_printf(m
, "status:\t\t%s\n"
8997 (status
!= 0) ? "enabled" : "disabled", status
);
9000 case TPACPI_FAN_RD_TPEC
:
9001 /* all except 570, 600e/x, 770e, 770x */
9002 rc
= fan_get_status_safe(&status
);
9006 seq_printf(m
, "status:\t\t%s\n",
9007 (status
!= 0) ? "enabled" : "disabled");
9009 rc
= fan_get_speed(&speed
);
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");
9021 seq_printf(m
, "level:\t\t%d\n", status
);
9024 case TPACPI_FAN_NONE
:
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");
9038 seq_printf(m
, " (<level> is 0-7, auto, disengaged, full-speed)\n");
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");
9053 static int fan_write_cmd_level(const char *cmd
, int *rc
)
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)
9065 *rc
= fan_set_level_safe(level
);
9067 pr_err("level command accepted for unsupported access mode %d\n",
9068 fan_control_access_mode
);
9070 tpacpi_disclose_usertask("procfs fan",
9071 "set level to %d\n", level
);
9076 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
9078 if (strlencmp(cmd
, "enable") != 0)
9081 *rc
= fan_set_enable();
9083 pr_err("enable command accepted for unsupported access mode %d\n",
9084 fan_control_access_mode
);
9086 tpacpi_disclose_usertask("procfs fan", "enable\n");
9091 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
9093 if (strlencmp(cmd
, "disable") != 0)
9096 *rc
= fan_set_disable();
9098 pr_err("disable command accepted for unsupported access mode %d\n",
9099 fan_control_access_mode
);
9101 tpacpi_disclose_usertask("procfs fan", "disable\n");
9106 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
9111 * Support speed <low> <medium> <high> ? */
9113 if (sscanf(cmd
, "speed %d", &speed
) != 1)
9116 *rc
= fan_set_speed(speed
);
9118 pr_err("speed command accepted for unsupported access mode %d\n",
9119 fan_control_access_mode
);
9121 tpacpi_disclose_usertask("procfs fan",
9122 "set speed to %d\n", speed
);
9127 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
9131 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
9134 if (interval
< 0 || interval
> 120)
9137 fan_watchdog_maxinterval
= interval
;
9138 tpacpi_disclose_usertask("procfs fan",
9139 "set watchdog timer to %d\n",
9146 static int fan_write(char *buf
)
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
))
9163 fan_watchdog_reset();
9169 static struct ibm_struct fan_driver_data
= {
9174 .suspend
= fan_suspend
,
9175 .resume
= fan_resume
,
9178 /*************************************************************************
9179 * Mute LED subdriver
9182 #define TPACPI_LED_MAX 2
9184 struct tp_led_table
{
9191 static struct tp_led_table led_tables
[TPACPI_LED_MAX
] = {
9192 [LED_AUDIO_MUTE
] = {
9197 [LED_AUDIO_MICMUTE
] = {
9204 static int mute_led_on_off(struct tp_led_table
*t
, bool state
)
9209 if (ACPI_FAILURE(acpi_get_handle(hkey_handle
, t
->name
, &temp
))) {
9210 pr_warn("Thinkpad ACPI has no %s interface.\n", t
->name
);
9214 if (!acpi_evalf(hkey_handle
, &output
, t
->name
, "dd",
9215 state
? t
->on_value
: t
->off_value
))
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
)
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
)
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
))) {
9271 mute_led_cdev
[i
].brightness
= ledtrig_audio_get(i
);
9272 err
= led_classdev_register(&tpacpi_pdev
->dev
, &mute_led_cdev
[i
]);
9275 if (led_tables
[i
].state
>= 0)
9276 led_classdev_unregister(&mute_led_cdev
[i
]);
9284 static void mute_led_exit(void)
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)
9300 for (i
= 0; i
< TPACPI_LED_MAX
; i
++) {
9301 struct tp_led_table
*t
= &led_tables
[i
];
9303 mute_led_on_off(t
, t
->state
);
9307 static struct ibm_struct mute_led_driver_data
= {
9309 .exit
= mute_led_exit
,
9310 .resume
= mute_led_resume
,
9314 * Battery Wear Control Driver
9315 * Contact: Ognjen Galic <smclt30p@gmail.com>
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"
9335 /* Error condition bit */
9336 METHOD_ERR
= BIT(31),
9340 /* This is used in the get/set helpers */
9345 struct tpacpi_battery_data
{
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
)
9370 if (!acpi_evalf(hkey_handle
, &response
, method
, "dd", param
)) {
9371 acpi_handle_err(hkey_handle
, "%s: evaluate failed", method
);
9374 if (response
& METHOD_ERR
) {
9375 acpi_handle_err(hkey_handle
,
9376 "%s evaluated but flagged as error", method
);
9383 static int tpacpi_battery_get(int what
, int battery
, int *ret
)
9386 case THRESHOLD_START
:
9387 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START
, ret
, battery
))
9390 /* The value is in the low 8 bits of the response */
9393 case THRESHOLD_STOP
:
9394 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP
, ret
, battery
))
9396 /* Value is in lower 8 bits */
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
9408 pr_crit("wrong parameter: %d", what
);
9413 static int tpacpi_battery_set(int what
, int battery
, int value
)
9416 /* The first 8 bits are the value of the threshold */
9418 /* The battery ID is in bits 8-9, 2 bits */
9419 param
|= battery
<< 8;
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",
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
);
9436 pr_crit("wrong parameter: %d", what
);
9441 static int tpacpi_battery_probe(int battery
)
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
);
9459 /* Individual addressing is in bit 9 */
9461 battery_info
.individual_addressing
= true;
9462 /* Support is marked in bit 8 */
9464 battery_info
.batteries
[battery
].start_support
= 1;
9467 if (tpacpi_battery_get(THRESHOLD_START
, battery
,
9468 &battery_info
.batteries
[battery
].charge_start
)) {
9469 pr_err("Error probing battery %d\n", battery
);
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
);
9478 /* Support is marked in bit 8 */
9480 battery_info
.batteries
[battery
].stop_support
= 1;
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
);
9489 pr_info("battery %d registered (start %d, stop %d)",
9491 battery_info
.batteries
[battery
].charge_start
,
9492 battery_info
.batteries
[battery
].charge_stop
);
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
)
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,
9512 pr_warn("unknown battery %s, assuming primary", battery_name
);
9516 /* sysfs interface */
9518 static ssize_t
tpacpi_battery_store(int what
,
9520 const char *buf
, size_t count
)
9522 struct power_supply
*supply
= to_power_supply(dev
);
9523 unsigned long value
;
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
);
9540 battery
= BAT_PRIMARY
;
9542 rval
= kstrtoul(buf
, 10, &value
);
9547 case THRESHOLD_START
:
9548 if (!battery_info
.batteries
[battery
].start_support
)
9550 /* valid values are [0, 99] */
9551 if (value
< 0 || value
> 99)
9553 if (value
> battery_info
.batteries
[battery
].charge_stop
)
9555 if (tpacpi_battery_set(THRESHOLD_START
, battery
, value
))
9557 battery_info
.batteries
[battery
].charge_start
= value
;
9560 case THRESHOLD_STOP
:
9561 if (!battery_info
.batteries
[battery
].stop_support
)
9563 /* valid values are [1, 100] */
9564 if (value
< 1 || value
> 100)
9566 if (value
< battery_info
.batteries
[battery
].charge_start
)
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%
9576 if (tpacpi_battery_set(THRESHOLD_STOP
, battery
, value
))
9580 pr_crit("Wrong parameter: %d", what
);
9586 static ssize_t
tpacpi_battery_show(int what
,
9590 struct power_supply
*supply
= to_power_supply(dev
);
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
);
9607 battery
= BAT_PRIMARY
;
9608 if (tpacpi_battery_get(what
, battery
, &ret
))
9610 return sprintf(buf
, "%d\n", ret
);
9613 static ssize_t
charge_start_threshold_show(struct device
*device
,
9614 struct device_attribute
*attr
,
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
,
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
,
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
))
9660 if (device_add_groups(&battery
->dev
, tpacpi_battery_groups
))
9665 static int tpacpi_battery_remove(struct power_supply
*battery
)
9667 device_remove_groups(&battery
->dev
, tpacpi_battery_groups
);
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
);
9703 static void tpacpi_battery_exit(void)
9705 battery_hook_unregister(&battery_hook
);
9708 static struct ibm_struct battery_driver_data
= {
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
;
9724 if (ACPI_FAILURE(acpi_get_handle(hkey_handle
, "SSSS", &set_shadow_handle
))) {
9725 pr_warn("Thinkpad ACPI has no %s interface.\n", "SSSS");
9729 if (!acpi_evalf(set_shadow_handle
, &output
, NULL
, "dd", (int)state
))
9732 lcdshadow_state
= state
;
9736 static int lcdshadow_set(bool on
)
9738 if (lcdshadow_state
< 0)
9739 return lcdshadow_state
;
9740 if (lcdshadow_state
== on
)
9742 return lcdshadow_on_off(on
);
9745 static int tpacpi_lcdshadow_init(struct ibm_init_struct
*iibm
)
9747 acpi_handle get_shadow_handle
;
9750 if (ACPI_FAILURE(acpi_get_handle(hkey_handle
, "GSSS", &get_shadow_handle
))) {
9751 lcdshadow_state
= -ENODEV
;
9755 if (!acpi_evalf(get_shadow_handle
, &output
, NULL
, "dd", 0)) {
9756 lcdshadow_state
= -EIO
;
9759 if (!(output
& 0x10000)) {
9760 lcdshadow_state
= -ENODEV
;
9763 lcdshadow_state
= output
& 0x1;
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");
9779 seq_printf(m
, "status:\t\t%d\n", lcdshadow_state
);
9780 seq_puts(m
, "commands:\t0, 1\n");
9786 static int lcdshadow_write(char *buf
)
9791 if (lcdshadow_state
< 0)
9794 while ((cmd
= next_cmd(&buf
))) {
9795 if (strlencmp(cmd
, "0") == 0)
9797 else if (strlencmp(cmd
, "1") == 0)
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 ****************************************************************************
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();
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 /* --------------------------------------------------------------------- */
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
);
9898 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
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
);
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
) {
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
)
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
)
9942 dbg_printk(TPACPI_DBG_INIT
,
9943 "probing for %s\n", ibm
->name
);
9946 ret
= iibm
->init(iibm
);
9948 return 0; /* probe failed */
9952 ibm
->flags
.init_called
= 1;
9956 if (ibm
->acpi
->hid
) {
9957 ret
= register_tpacpi_subdriver(ibm
);
9962 if (ibm
->acpi
->notify
) {
9963 ret
= setup_acpi_notify(ibm
);
9964 if (ret
== -ENODEV
) {
9965 pr_notice("disabling subdriver %s\n",
9975 dbg_printk(TPACPI_DBG_INIT
,
9976 "%s installed\n", ibm
->name
);
9979 umode_t mode
= iibm
->base_procfs_mode
;
9985 entry
= proc_create_data(ibm
->name
, mode
, proc_dir
,
9986 &dispatch_proc_ops
, ibm
);
9988 pr_err("unable to create proc entry %s\n", ibm
->name
);
9992 ibm
->flags
.proc_created
= 1;
9995 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
10000 dbg_printk(TPACPI_DBG_INIT
,
10001 "%s: at error exit path with result %d\n",
10005 return (ret
< 0) ? ret
: 0;
10010 static char __init
tpacpi_parse_fw_id(const char * const s
,
10011 u32
*model
, u16
*release
)
10015 if (!s
|| strlen(s
) < 8)
10018 for (i
= 0; i
< 8; i
++)
10019 if (!((s
[i
] >= '0' && s
[i
] <= '9') ||
10020 (s
[i
] >= 'A' && s
[i
] <= 'Z')))
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]);
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]);
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)
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};
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
;
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
)
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')
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;
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
)
10134 t
= tpacpi_parse_fw_id(ec_fw_string
,
10135 &tp
->ec_model
, &tp
->ec_release
);
10137 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
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
)
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
)
10157 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
10158 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
10159 if (s
&& !tp
->nummodel_str
)
10165 static int __init
probe_for_thinkpad(void)
10172 /* It would be dangerous to run the driver in this case */
10173 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
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
);
10188 pr_err("Not yet supported ThinkPad detected!\n");
10192 if (!is_thinkpad
&& !force_load
)
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
,
10238 .data
= &wan_driver_data
,
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
,
10252 .init
= kbdlight_init
,
10253 .data
= &kbdlight_driver_data
,
10256 .init
= light_init
,
10257 .data
= &light_driver_data
,
10261 .data
= &cmos_driver_data
,
10265 .data
= &led_driver_data
,
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
,
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
)
10304 struct ibm_struct
*ibm
;
10306 if (!kp
|| !kp
->name
|| !val
)
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
)
10316 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
10317 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
10319 strcpy(ibms_init
[i
].param
, val
);
10320 strcat(ibms_init
[i
].param
, ",");
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
);
10388 TPACPI_PARAM(beep
);
10389 TPACPI_PARAM(brightness
);
10390 TPACPI_PARAM(volume
);
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");
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
,
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
);
10437 input_free_device(tpacpi_inputdev
);
10438 kfree(hotkey_keycode_map
);
10442 hwmon_device_unregister(tpacpi_hwmon
);
10444 if (tpacpi_sensors_pdev
)
10445 platform_device_unregister(tpacpi_sensors_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
);
10461 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
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)
10477 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
10479 /* Driver-level probe */
10481 ret
= get_thinkpad_model_data(&thinkpad_id
);
10483 pr_err("unable to get DMI data: %d\n", ret
);
10484 thinkpad_acpi_module_exit();
10487 ret
= probe_for_thinkpad();
10489 thinkpad_acpi_module_exit();
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
);
10503 thinkpad_acpi_module_exit();
10507 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
10509 pr_err("unable to create proc dir " TPACPI_PROC_DIR
"\n");
10510 thinkpad_acpi_module_exit();
10514 ret
= platform_driver_register(&tpacpi_pdriver
);
10516 pr_err("unable to register main platform driver\n");
10517 thinkpad_acpi_module_exit();
10520 tp_features
.platform_drv_registered
= 1;
10522 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
10524 pr_err("unable to register hwmon platform driver\n");
10525 thinkpad_acpi_module_exit();
10528 tp_features
.sensors_pdrv_registered
= 1;
10530 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
10532 tp_features
.platform_drv_attrs_registered
= 1;
10533 ret
= tpacpi_create_driver_attributes(
10534 &tpacpi_hwmon_pdriver
.driver
);
10537 pr_err("unable to create sysfs driver attributes\n");
10538 thinkpad_acpi_module_exit();
10541 tp_features
.sensors_pdrv_attrs_registered
= 1;
10544 /* Device initialization */
10545 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
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();
10554 tpacpi_sensors_pdev
= platform_device_register_simple(
10555 TPACPI_HWMON_DRVR_NAME
,
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();
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();
10575 mutex_init(&tpacpi_inputdev_send_mutex
);
10576 tpacpi_inputdev
= input_allocate_device();
10577 if (!tpacpi_inputdev
) {
10578 thinkpad_acpi_module_exit();
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
);
10600 thinkpad_acpi_module_exit();
10605 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
10607 ret
= input_register_device(tpacpi_inputdev
);
10609 pr_err("unable to register input device\n");
10610 thinkpad_acpi_module_exit();
10613 tp_features
.input_device_registered
= 1;
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
);