2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.25"
27 #define TPACPI_SYSFS_VERSION 0x020700
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
70 #include <linux/platform_device.h>
71 #include <linux/hwmon.h>
72 #include <linux/hwmon-sysfs.h>
73 #include <linux/input.h>
74 #include <linux/leds.h>
75 #include <linux/rfkill.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
79 #include <linux/acpi.h>
80 #include <linux/pci_ids.h>
81 #include <linux/thinkpad_acpi.h>
82 #include <sound/core.h>
83 #include <sound/control.h>
84 #include <sound/initval.h>
85 #include <linux/uaccess.h>
86 #include <acpi/video.h>
88 /* ThinkPad CMOS commands */
89 #define TP_CMOS_VOLUME_DOWN 0
90 #define TP_CMOS_VOLUME_UP 1
91 #define TP_CMOS_VOLUME_MUTE 2
92 #define TP_CMOS_BRIGHTNESS_UP 4
93 #define TP_CMOS_BRIGHTNESS_DOWN 5
94 #define TP_CMOS_THINKLIGHT_ON 12
95 #define TP_CMOS_THINKLIGHT_OFF 13
99 TP_NVRAM_ADDR_HK2
= 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT
= 0x58,
101 TP_NVRAM_ADDR_VIDEO
= 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS
= 0x5e,
103 TP_NVRAM_ADDR_MIXER
= 0x60,
106 /* NVRAM bit masks */
108 TP_NVRAM_MASK_HKT_THINKPAD
= 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM
= 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY
= 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE
= 0x80,
112 TP_NVRAM_MASK_THINKLIGHT
= 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND
= 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS
= 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS
= 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS
= 0,
117 TP_NVRAM_MASK_MUTE
= 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME
= 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME
= 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME
= 0,
123 /* Misc NVRAM-related */
125 TP_NVRAM_LEVEL_VOLUME_MAX
= 14,
129 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
140 TP_ACPI_WGSV_GET_STATE
= 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME
= 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
= 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE
= 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
148 TP_ACPI_WGSV_STATE_WWANEXIST
= 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR
= 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES
= 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF
= 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST
= 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR
= 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES
= 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF
= 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST
= 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR
= 0x0020, /* UWB radio enabled */
161 enum tpacpi_hkey_event_t
{
163 TP_HKEY_EV_HOTKEY_BASE
= 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP
= 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN
= 0x1011, /* Brightness down */
166 TP_HKEY_EV_KBD_LIGHT
= 0x1012, /* Thinklight/kbd backlight */
167 TP_HKEY_EV_VOL_UP
= 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN
= 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE
= 0x1017, /* Mixer output mute */
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK
= 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK
= 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ
= 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ
= 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW
= 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW
= 0x2413, /* battery empty, S4 */
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK
= 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK
= 0x4003, /* undock complete */
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ
= 0x3006, /* opt. drive tray ejected */
185 TP_HKEY_EV_HOTPLUG_DOCK
= 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK
= 0x4011, /* undocked from hotplug
188 dock or port replicator */
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE
= 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN
= 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET
= 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK
= 0x500a, /* tablet swivel down */
195 TP_HKEY_EV_TABLET_CHANGED
= 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
198 TP_HKEY_EV_PEN_INSERTED
= 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED
= 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED
= 0x5010, /* backlight control event */
202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK
= 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN
= 0x6005, /* Fn key pressed? E420 */
205 TP_HKEY_EV_KEY_FN_ESC
= 0x6060, /* Fn+Esc key pressed X240 */
208 TP_HKEY_EV_ALARM_BAT_HOT
= 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT
= 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT
= 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT
= 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED
= 0x6030, /* thermal table changed */
214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED
= 0x6040, /* AC status changed */
218 TP_HKEY_EV_RFKILL_CHANGED
= 0x7000, /* rfkill switch changed */
221 /****************************************************************************
225 #define TPACPI_NAME "thinkpad"
226 #define TPACPI_DESC "ThinkPad ACPI Extras"
227 #define TPACPI_FILE TPACPI_NAME "_acpi"
228 #define TPACPI_URL "http://ibm-acpi.sf.net/"
229 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
231 #define TPACPI_PROC_DIR "ibm"
232 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
233 #define TPACPI_DRVR_NAME TPACPI_FILE
234 #define TPACPI_DRVR_SHORTNAME "tpacpi"
235 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
237 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
238 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
240 #define TPACPI_MAX_ACPI_ARGS 3
242 /* Debugging printk groups */
243 #define TPACPI_DBG_ALL 0xffff
244 #define TPACPI_DBG_DISCLOSETASK 0x8000
245 #define TPACPI_DBG_INIT 0x0001
246 #define TPACPI_DBG_EXIT 0x0002
247 #define TPACPI_DBG_RFKILL 0x0004
248 #define TPACPI_DBG_HKEY 0x0008
249 #define TPACPI_DBG_FAN 0x0010
250 #define TPACPI_DBG_BRGHT 0x0020
251 #define TPACPI_DBG_MIXER 0x0040
253 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
258 /****************************************************************************
259 * Driver-wide structs and misc. variables
264 struct tp_acpi_drv_struct
{
265 const struct acpi_device_id
*hid
;
266 struct acpi_driver
*driver
;
268 void (*notify
) (struct ibm_struct
*, u32
);
271 struct acpi_device
*device
;
277 int (*read
) (struct seq_file
*);
278 int (*write
) (char *);
280 void (*resume
) (void);
281 void (*suspend
) (void);
282 void (*shutdown
) (void);
284 struct list_head all_drivers
;
286 struct tp_acpi_drv_struct
*acpi
;
289 u8 acpi_driver_registered
:1;
290 u8 acpi_notify_installed
:1;
297 struct ibm_init_struct
{
300 int (*init
) (struct ibm_init_struct
*);
301 umode_t base_procfs_mode
;
302 struct ibm_struct
*data
;
311 TP_HOTKEY_TABLET_NONE
= 0,
312 TP_HOTKEY_TABLET_USES_MHKG
,
313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD
,
319 u32 bright_acpimode
:1;
323 u32 fan_ctrl_status_undef
:1;
325 u32 beep_needs_two_args
:1;
326 u32 mixer_no_level_control
:1;
327 u32 input_device_registered
:1;
328 u32 platform_drv_registered
:1;
329 u32 platform_drv_attrs_registered
:1;
330 u32 sensors_pdrv_registered
:1;
331 u32 sensors_pdrv_attrs_registered
:1;
332 u32 sensors_pdev_attrs_registered
:1;
333 u32 hotkey_poll_active
:1;
334 u32 has_adaptive_kbd
:1;
338 u16 hotkey_mask_ff
:1;
339 u16 volume_ctrl_forbidden
:1;
342 struct thinkpad_id_data
{
343 unsigned int vendor
; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
346 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
349 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
351 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
354 char *model_str
; /* ThinkPad T43 */
355 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
357 static struct thinkpad_id_data thinkpad_id
;
360 TPACPI_LIFE_INIT
= 0,
365 static int experimental
;
366 static u32 dbg_level
;
368 static struct workqueue_struct
*tpacpi_wq
;
376 /* tpacpi LED class */
377 struct tpacpi_led_classdev
{
378 struct led_classdev led_classdev
;
382 /* brightness level capabilities */
383 static unsigned int bright_maxlvl
; /* 0 = unknown */
385 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386 static int dbg_wlswemul
;
387 static bool tpacpi_wlsw_emulstate
;
388 static int dbg_bluetoothemul
;
389 static bool tpacpi_bluetooth_emulstate
;
390 static int dbg_wwanemul
;
391 static bool tpacpi_wwan_emulstate
;
392 static int dbg_uwbemul
;
393 static bool tpacpi_uwb_emulstate
;
397 /*************************************************************************
401 #define dbg_printk(a_dbg_level, format, arg...) \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
408 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
409 #define vdbg_printk dbg_printk
410 static const char *str_supported(int is_supported
);
412 static inline const char *str_supported(int is_supported
) { return ""; }
413 #define vdbg_printk(a_dbg_level, format, arg...) \
414 do { if (0) no_printk(format, ##arg); } while (0)
417 static void tpacpi_log_usertask(const char * const what
)
419 printk(KERN_DEBUG
pr_fmt("%s: access by process with PID %d\n"),
420 what
, task_tgid_vnr(current
));
423 #define tpacpi_disclose_usertask(what, format, arg...) \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
433 * Quirk handling helpers
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
438 * We use values well outside that range as specials.
441 #define TPACPI_MATCH_ANY 0xffffU
442 #define TPACPI_MATCH_UNKNOWN 0U
444 /* TPID('1', 'Y') == 0x5931 */
445 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
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_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
465 struct tpacpi_quirk
{
469 unsigned long quirks
;
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
483 * The match criteria is: vendor, ec and bios much match.
485 static unsigned long __init
tpacpi_check_quirks(
486 const struct tpacpi_quirk
*qlist
,
487 unsigned int qlist_size
)
490 if ((qlist
->vendor
== thinkpad_id
.vendor
||
491 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
492 (qlist
->bios
== thinkpad_id
.bios_model
||
493 qlist
->bios
== TPACPI_MATCH_ANY
) &&
494 (qlist
->ec
== thinkpad_id
.ec_model
||
495 qlist
->ec
== TPACPI_MATCH_ANY
))
496 return qlist
->quirks
;
504 static inline bool __pure __init
tpacpi_is_lenovo(void)
506 return thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
;
509 static inline bool __pure __init
tpacpi_is_ibm(void)
511 return thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
;
514 /****************************************************************************
515 ****************************************************************************
517 * ACPI Helpers and device model
519 ****************************************************************************
520 ****************************************************************************/
522 /*************************************************************************
526 static acpi_handle root_handle
;
527 static acpi_handle ec_handle
;
529 #define TPACPI_HANDLE(object, parent, paths...) \
530 static acpi_handle object##_handle; \
531 static const acpi_handle * const object##_parent __initconst = \
533 static char *object##_paths[] __initdata = { paths }
535 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
536 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
538 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
544 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
549 /*************************************************************************
553 static int acpi_evalf(acpi_handle handle
,
554 int *res
, char *method
, char *fmt
, ...)
557 struct acpi_object_list params
;
558 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
559 struct acpi_buffer result
, *resultp
;
560 union acpi_object out_obj
;
568 pr_err("acpi_evalf() called with empty format\n");
581 params
.pointer
= &in_objs
[0];
588 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
589 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
591 /* add more types as needed */
593 pr_err("acpi_evalf() called "
594 "with invalid format character '%c'\n", c
);
601 if (res_type
!= 'v') {
602 result
.length
= sizeof(out_obj
);
603 result
.pointer
= &out_obj
;
608 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
612 success
= (status
== AE_OK
&&
613 out_obj
.type
== ACPI_TYPE_INTEGER
);
615 *res
= out_obj
.integer
.value
;
618 success
= status
== AE_OK
;
620 /* add more types as needed */
622 pr_err("acpi_evalf() called "
623 "with invalid format character '%c'\n", res_type
);
627 if (!success
&& !quiet
)
628 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
629 method
, fmt0
, acpi_format_exception(status
));
634 static int acpi_ec_read(int i
, u8
*p
)
639 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
643 if (ec_read(i
, p
) < 0)
650 static int acpi_ec_write(int i
, u8 v
)
653 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
656 if (ec_write(i
, v
) < 0)
663 static int issue_thinkpad_cmos_command(int cmos_cmd
)
668 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
674 /*************************************************************************
678 #define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
680 object##_paths, ARRAY_SIZE(object##_paths))
682 static void __init
drv_acpi_handle_init(const char *name
,
683 acpi_handle
*handle
, const acpi_handle parent
,
684 char **paths
, const int num_paths
)
689 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
692 for (i
= 0; i
< num_paths
; i
++) {
693 status
= acpi_get_handle(parent
, paths
[i
], handle
);
694 if (ACPI_SUCCESS(status
)) {
695 dbg_printk(TPACPI_DBG_INIT
,
696 "Found ACPI handle %s for %s\n",
702 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
707 static acpi_status __init
tpacpi_acpi_handle_locate_callback(acpi_handle handle
,
708 u32 level
, void *context
, void **return_value
)
710 struct acpi_device
*dev
;
711 if (!strcmp(context
, "video")) {
712 if (acpi_bus_get_device(handle
, &dev
))
714 if (strcmp(ACPI_VIDEO_HID
, acpi_device_hid(dev
)))
718 *(acpi_handle
*)return_value
= handle
;
720 return AE_CTRL_TERMINATE
;
723 static void __init
tpacpi_acpi_handle_locate(const char *name
,
728 acpi_handle device_found
;
730 BUG_ON(!name
|| !handle
);
731 vdbg_printk(TPACPI_DBG_INIT
,
732 "trying to locate ACPI handle for %s, using HID %s\n",
733 name
, hid
? hid
: "NULL");
735 memset(&device_found
, 0, sizeof(device_found
));
736 status
= acpi_get_devices(hid
, tpacpi_acpi_handle_locate_callback
,
737 (void *)name
, &device_found
);
741 if (ACPI_SUCCESS(status
)) {
742 *handle
= device_found
;
743 dbg_printk(TPACPI_DBG_INIT
,
744 "Found ACPI handle for %s\n", name
);
746 vdbg_printk(TPACPI_DBG_INIT
,
747 "Could not locate an ACPI handle for %s: %s\n",
748 name
, acpi_format_exception(status
));
752 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
754 struct ibm_struct
*ibm
= data
;
756 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
759 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
762 ibm
->acpi
->notify(ibm
, event
);
765 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
772 if (!*ibm
->acpi
->handle
)
775 vdbg_printk(TPACPI_DBG_INIT
,
776 "setting up ACPI notify for %s\n", ibm
->name
);
778 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
780 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm
->name
, rc
);
784 ibm
->acpi
->device
->driver_data
= ibm
;
785 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
786 TPACPI_ACPI_EVENT_PREFIX
,
789 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
790 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
791 if (ACPI_FAILURE(status
)) {
792 if (status
== AE_ALREADY_EXISTS
) {
793 pr_notice("another device driver is already "
794 "handling %s events\n", ibm
->name
);
796 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
797 ibm
->name
, acpi_format_exception(status
));
801 ibm
->flags
.acpi_notify_installed
= 1;
805 static int __init
tpacpi_device_add(struct acpi_device
*device
)
810 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
814 dbg_printk(TPACPI_DBG_INIT
,
815 "registering %s as an ACPI driver\n", ibm
->name
);
819 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
820 if (!ibm
->acpi
->driver
) {
821 pr_err("failed to allocate memory for ibm->acpi->driver\n");
825 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
826 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
828 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
830 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
832 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
834 kfree(ibm
->acpi
->driver
);
835 ibm
->acpi
->driver
= NULL
;
837 ibm
->flags
.acpi_driver_registered
= 1;
843 /****************************************************************************
844 ****************************************************************************
848 ****************************************************************************
849 ****************************************************************************/
851 static int dispatch_proc_show(struct seq_file
*m
, void *v
)
853 struct ibm_struct
*ibm
= m
->private;
855 if (!ibm
|| !ibm
->read
)
860 static int dispatch_proc_open(struct inode
*inode
, struct file
*file
)
862 return single_open(file
, dispatch_proc_show
, PDE_DATA(inode
));
865 static ssize_t
dispatch_proc_write(struct file
*file
,
866 const char __user
*userbuf
,
867 size_t count
, loff_t
*pos
)
869 struct ibm_struct
*ibm
= PDE_DATA(file_inode(file
));
873 if (!ibm
|| !ibm
->write
)
875 if (count
> PAGE_SIZE
- 2)
878 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
882 if (copy_from_user(kernbuf
, userbuf
, count
)) {
888 strcat(kernbuf
, ",");
889 ret
= ibm
->write(kernbuf
);
898 static const struct file_operations dispatch_proc_fops
= {
899 .owner
= THIS_MODULE
,
900 .open
= dispatch_proc_open
,
903 .release
= single_release
,
904 .write
= dispatch_proc_write
,
907 static char *next_cmd(char **cmds
)
912 while ((end
= strchr(start
, ',')) && end
== start
)
924 /****************************************************************************
925 ****************************************************************************
927 * Device model: input, hwmon and platform
929 ****************************************************************************
930 ****************************************************************************/
932 static struct platform_device
*tpacpi_pdev
;
933 static struct platform_device
*tpacpi_sensors_pdev
;
934 static struct device
*tpacpi_hwmon
;
935 static struct input_dev
*tpacpi_inputdev
;
936 static struct mutex tpacpi_inputdev_send_mutex
;
937 static LIST_HEAD(tpacpi_all_drivers
);
939 #ifdef CONFIG_PM_SLEEP
940 static int tpacpi_suspend_handler(struct device
*dev
)
942 struct ibm_struct
*ibm
, *itmp
;
944 list_for_each_entry_safe(ibm
, itmp
,
954 static int tpacpi_resume_handler(struct device
*dev
)
956 struct ibm_struct
*ibm
, *itmp
;
958 list_for_each_entry_safe(ibm
, itmp
,
969 static SIMPLE_DEV_PM_OPS(tpacpi_pm
,
970 tpacpi_suspend_handler
, tpacpi_resume_handler
);
972 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
974 struct ibm_struct
*ibm
, *itmp
;
976 list_for_each_entry_safe(ibm
, itmp
,
984 static struct platform_driver tpacpi_pdriver
= {
986 .name
= TPACPI_DRVR_NAME
,
989 .shutdown
= tpacpi_shutdown_handler
,
992 static struct platform_driver tpacpi_hwmon_pdriver
= {
994 .name
= TPACPI_HWMON_DRVR_NAME
,
998 /*************************************************************************
999 * sysfs support helpers
1002 struct attribute_set
{
1003 unsigned int members
, max_members
;
1004 struct attribute_group group
;
1007 struct attribute_set_obj
{
1008 struct attribute_set s
;
1009 struct attribute
*a
;
1010 } __attribute__((packed
));
1012 static struct attribute_set
*create_attr_set(unsigned int max_members
,
1015 struct attribute_set_obj
*sobj
;
1017 if (max_members
== 0)
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
1022 max_members
* sizeof(struct attribute
*),
1026 sobj
->s
.max_members
= max_members
;
1027 sobj
->s
.group
.attrs
= &sobj
->a
;
1028 sobj
->s
.group
.name
= name
;
1033 #define destroy_attr_set(_set) \
1036 /* not multi-threaded safe, use it in a single thread per set */
1037 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
1042 if (s
->members
>= s
->max_members
)
1045 s
->group
.attrs
[s
->members
] = attr
;
1051 static int add_many_to_attr_set(struct attribute_set
*s
,
1052 struct attribute
**attr
,
1057 for (i
= 0; i
< count
; i
++) {
1058 res
= add_to_attr_set(s
, attr
[i
]);
1066 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
1068 sysfs_remove_group(kobj
, &s
->group
);
1069 destroy_attr_set(s
);
1072 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1075 static int parse_strtoul(const char *buf
,
1076 unsigned long max
, unsigned long *value
)
1080 *value
= simple_strtoul(skip_spaces(buf
), &endp
, 0);
1081 endp
= skip_spaces(endp
);
1082 if (*endp
|| *value
> max
)
1088 static void tpacpi_disable_brightness_delay(void)
1090 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
1091 pr_notice("ACPI backlight control delay disabled\n");
1094 static void printk_deprecated_attribute(const char * const what
,
1095 const char * const details
)
1097 tpacpi_log_usertask("deprecated sysfs attribute");
1098 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1099 "will be removed. %s\n",
1103 /*************************************************************************
1104 * rfkill and radio control support helpers
1108 * ThinkPad-ACPI firmware handling model:
1110 * WLSW (master wireless switch) is event-driven, and is common to all
1111 * firmware-controlled radios. It cannot be controlled, just monitored,
1112 * as expected. It overrides all radio state in firmware
1114 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1115 * (TODO: verify how WLSW interacts with the returned radio state).
1117 * The only time there are shadow radio state changes, is when
1118 * masked-off hotkeys are used.
1122 * Internal driver API for radio state:
1124 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1125 * bool: true means radio blocked (off)
1127 enum tpacpi_rfkill_state
{
1128 TPACPI_RFK_RADIO_OFF
= 0,
1132 /* rfkill switches */
1133 enum tpacpi_rfk_id
{
1134 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
1135 TPACPI_RFK_WWAN_SW_ID
,
1136 TPACPI_RFK_UWB_SW_ID
,
1140 static const char *tpacpi_rfkill_names
[] = {
1141 [TPACPI_RFK_BLUETOOTH_SW_ID
] = "bluetooth",
1142 [TPACPI_RFK_WWAN_SW_ID
] = "wwan",
1143 [TPACPI_RFK_UWB_SW_ID
] = "uwb",
1144 [TPACPI_RFK_SW_MAX
] = NULL
1147 /* ThinkPad-ACPI rfkill subdriver */
1149 struct rfkill
*rfkill
;
1150 enum tpacpi_rfk_id id
;
1151 const struct tpacpi_rfk_ops
*ops
;
1154 struct tpacpi_rfk_ops
{
1155 /* firmware interface */
1156 int (*get_status
)(void);
1157 int (*set_status
)(const enum tpacpi_rfkill_state
);
1160 static struct tpacpi_rfk
*tpacpi_rfkill_switches
[TPACPI_RFK_SW_MAX
];
1162 /* Query FW and update rfkill sw state for a given rfkill switch */
1163 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk
*tp_rfk
)
1170 status
= (tp_rfk
->ops
->get_status
)();
1174 rfkill_set_sw_state(tp_rfk
->rfkill
,
1175 (status
== TPACPI_RFK_RADIO_OFF
));
1180 /* Query FW and update rfkill sw state for all rfkill switches */
1181 static void tpacpi_rfk_update_swstate_all(void)
1185 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++)
1186 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[i
]);
1190 * Sync the HW-blocking state of all rfkill switches,
1191 * do notice it causes the rfkill core to schedule uevents
1193 static void tpacpi_rfk_update_hwblock_state(bool blocked
)
1196 struct tpacpi_rfk
*tp_rfk
;
1198 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++) {
1199 tp_rfk
= tpacpi_rfkill_switches
[i
];
1201 if (rfkill_set_hw_state(tp_rfk
->rfkill
,
1203 /* ignore -- we track sw block */
1209 /* Call to get the WLSW state from the firmware */
1210 static int hotkey_get_wlsw(void);
1212 /* Call to query WLSW state and update all rfkill switches */
1213 static bool tpacpi_rfk_check_hwblock_state(void)
1215 int res
= hotkey_get_wlsw();
1218 /* When unknown or unsupported, we have to assume it is unblocked */
1222 hw_blocked
= (res
== TPACPI_RFK_RADIO_OFF
);
1223 tpacpi_rfk_update_hwblock_state(hw_blocked
);
1228 static int tpacpi_rfk_hook_set_block(void *data
, bool blocked
)
1230 struct tpacpi_rfk
*tp_rfk
= data
;
1233 dbg_printk(TPACPI_DBG_RFKILL
,
1234 "request to change radio state to %s\n",
1235 blocked
? "blocked" : "unblocked");
1237 /* try to set radio state */
1238 res
= (tp_rfk
->ops
->set_status
)(blocked
?
1239 TPACPI_RFK_RADIO_OFF
: TPACPI_RFK_RADIO_ON
);
1241 /* and update the rfkill core with whatever the FW really did */
1242 tpacpi_rfk_update_swstate(tp_rfk
);
1244 return (res
< 0) ? res
: 0;
1247 static const struct rfkill_ops tpacpi_rfk_rfkill_ops
= {
1248 .set_block
= tpacpi_rfk_hook_set_block
,
1251 static int __init
tpacpi_new_rfkill(const enum tpacpi_rfk_id id
,
1252 const struct tpacpi_rfk_ops
*tp_rfkops
,
1253 const enum rfkill_type rfktype
,
1255 const bool set_default
)
1257 struct tpacpi_rfk
*atp_rfk
;
1259 bool sw_state
= false;
1263 BUG_ON(id
>= TPACPI_RFK_SW_MAX
|| tpacpi_rfkill_switches
[id
]);
1265 atp_rfk
= kzalloc(sizeof(struct tpacpi_rfk
), GFP_KERNEL
);
1267 atp_rfk
->rfkill
= rfkill_alloc(name
,
1270 &tpacpi_rfk_rfkill_ops
,
1272 if (!atp_rfk
|| !atp_rfk
->rfkill
) {
1273 pr_err("failed to allocate memory for rfkill class\n");
1279 atp_rfk
->ops
= tp_rfkops
;
1281 sw_status
= (tp_rfkops
->get_status
)();
1282 if (sw_status
< 0) {
1283 pr_err("failed to read initial state for %s, error %d\n",
1286 sw_state
= (sw_status
== TPACPI_RFK_RADIO_OFF
);
1288 /* try to keep the initial state, since we ask the
1289 * firmware to preserve it across S5 in NVRAM */
1290 rfkill_init_sw_state(atp_rfk
->rfkill
, sw_state
);
1293 hw_state
= tpacpi_rfk_check_hwblock_state();
1294 rfkill_set_hw_state(atp_rfk
->rfkill
, hw_state
);
1296 res
= rfkill_register(atp_rfk
->rfkill
);
1298 pr_err("failed to register %s rfkill switch: %d\n", name
, res
);
1299 rfkill_destroy(atp_rfk
->rfkill
);
1304 tpacpi_rfkill_switches
[id
] = atp_rfk
;
1306 pr_info("rfkill switch %s: radio is %sblocked\n",
1307 name
, (sw_state
|| hw_state
) ? "" : "un");
1311 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id
)
1313 struct tpacpi_rfk
*tp_rfk
;
1315 BUG_ON(id
>= TPACPI_RFK_SW_MAX
);
1317 tp_rfk
= tpacpi_rfkill_switches
[id
];
1319 rfkill_unregister(tp_rfk
->rfkill
);
1320 rfkill_destroy(tp_rfk
->rfkill
);
1321 tpacpi_rfkill_switches
[id
] = NULL
;
1326 static void printk_deprecated_rfkill_attribute(const char * const what
)
1328 printk_deprecated_attribute(what
,
1329 "Please switch to generic rfkill before year 2010");
1332 /* sysfs <radio> enable ------------------------------------------------ */
1333 static ssize_t
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id
,
1334 struct device_attribute
*attr
,
1339 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state()) {
1343 status
= TPACPI_RFK_RADIO_OFF
;
1345 status
= tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1350 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1351 (status
== TPACPI_RFK_RADIO_ON
) ? 1 : 0);
1354 static ssize_t
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id
,
1355 struct device_attribute
*attr
,
1356 const char *buf
, size_t count
)
1361 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1363 if (parse_strtoul(buf
, 1, &t
))
1366 tpacpi_disclose_usertask(attr
->attr
.name
, "set to %ld\n", t
);
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state() && !!t
)
1372 res
= tpacpi_rfkill_switches
[id
]->ops
->set_status((!!t
) ?
1373 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
);
1374 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1376 return (res
< 0) ? res
: count
;
1379 /* procfs -------------------------------------------------------------- */
1380 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id
, struct seq_file
*m
)
1382 if (id
>= TPACPI_RFK_SW_MAX
)
1383 seq_printf(m
, "status:\t\tnot supported\n");
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status
= TPACPI_RFK_RADIO_OFF
;
1391 status
= tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches
[id
]);
1397 seq_printf(m
, "status:\t\t%s\n",
1398 (status
== TPACPI_RFK_RADIO_ON
) ?
1399 "enabled" : "disabled");
1400 seq_printf(m
, "commands:\tenable, disable\n");
1406 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id
, char *buf
)
1412 if (id
>= TPACPI_RFK_SW_MAX
)
1415 while ((cmd
= next_cmd(&buf
))) {
1416 if (strlencmp(cmd
, "enable") == 0)
1417 status
= TPACPI_RFK_RADIO_ON
;
1418 else if (strlencmp(cmd
, "disable") == 0)
1419 status
= TPACPI_RFK_RADIO_OFF
;
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status
== TPACPI_RFK_RADIO_ON
) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names
[id
]);
1429 res
= (tpacpi_rfkill_switches
[id
]->ops
->set_status
)(status
);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1436 /*************************************************************************
1437 * thinkpad-acpi driver attributes
1440 /* interface_version --------------------------------------------------- */
1441 static ssize_t
tpacpi_driver_interface_version_show(
1442 struct device_driver
*drv
,
1445 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1448 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1449 tpacpi_driver_interface_version_show
, NULL
);
1451 /* debug_level --------------------------------------------------------- */
1452 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1455 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1458 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1459 const char *buf
, size_t count
)
1463 if (parse_strtoul(buf
, 0xffff, &t
))
1471 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1472 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1474 /* version ------------------------------------------------------------- */
1475 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1478 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1479 TPACPI_DESC
, TPACPI_VERSION
);
1482 static DRIVER_ATTR(version
, S_IRUGO
,
1483 tpacpi_driver_version_show
, NULL
);
1485 /* --------------------------------------------------------------------- */
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1493 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1496 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1497 const char *buf
, size_t count
)
1501 if (parse_strtoul(buf
, 1, &t
))
1504 if (tpacpi_wlsw_emulstate
!= !!t
) {
1505 tpacpi_wlsw_emulstate
= !!t
;
1506 tpacpi_rfk_update_hwblock_state(!t
); /* negative logic */
1512 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1513 tpacpi_driver_wlsw_emulstate_show
,
1514 tpacpi_driver_wlsw_emulstate_store
);
1516 /* bluetooth_emulstate ------------------------------------------------- */
1517 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver
*drv
,
1521 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1524 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver
*drv
,
1526 const char *buf
, size_t count
)
1530 if (parse_strtoul(buf
, 1, &t
))
1533 tpacpi_bluetooth_emulstate
= !!t
;
1538 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1539 tpacpi_driver_bluetooth_emulstate_show
,
1540 tpacpi_driver_bluetooth_emulstate_store
);
1542 /* wwan_emulstate ------------------------------------------------- */
1543 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver
*drv
,
1547 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1550 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver
*drv
,
1552 const char *buf
, size_t count
)
1556 if (parse_strtoul(buf
, 1, &t
))
1559 tpacpi_wwan_emulstate
= !!t
;
1564 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1565 tpacpi_driver_wwan_emulstate_show
,
1566 tpacpi_driver_wwan_emulstate_store
);
1568 /* uwb_emulstate ------------------------------------------------- */
1569 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver
*drv
,
1573 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1576 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver
*drv
,
1578 const char *buf
, size_t count
)
1582 if (parse_strtoul(buf
, 1, &t
))
1585 tpacpi_uwb_emulstate
= !!t
;
1590 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1591 tpacpi_driver_uwb_emulstate_show
,
1592 tpacpi_driver_uwb_emulstate_store
);
1595 /* --------------------------------------------------------------------- */
1597 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1598 &driver_attr_debug_level
, &driver_attr_version
,
1599 &driver_attr_interface_version
,
1602 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1608 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1609 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1613 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res
&& dbg_wlswemul
)
1615 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1616 if (!res
&& dbg_bluetoothemul
)
1617 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1618 if (!res
&& dbg_wwanemul
)
1619 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1620 if (!res
&& dbg_uwbemul
)
1621 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1627 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1631 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1632 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1634 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1636 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1637 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1638 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1642 /*************************************************************************
1647 * Table of recommended minimum BIOS versions
1649 * Reasons for listing:
1650 * 1. Stable BIOS, listed because the unknown amount of
1651 * bugs and bad ACPI behaviour on older versions
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1655 * 3. BIOS with known reduced functionality in older versions
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
1667 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1674 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1675 __eid, __ev1, __ev2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
1679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1682 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1685 /* Outdated IBM BIOSes often lack the EC id string */
1686 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1694 /* Outdated IBM BIOSes often lack the EC id string */
1695 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPID(__eid1, __eid2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1704 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1707 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1709 __bv1, __bv2, TPID(__id1, __id2), \
1712 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1715 __bv1, __bv2, TPID(__eid1, __eid2), \
1718 static const struct tpacpi_quirk tpacpi_bios_version_qtable
[] __initconst
= {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1805 static void __init
tpacpi_check_outdated_fw(void)
1807 unsigned long fwvers
;
1808 u16 ec_version
, bios_version
;
1810 fwvers
= tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable
));
1816 bios_version
= fwvers
& 0xffffU
;
1817 ec_version
= (fwvers
>> 16) & 0xffffU
;
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version
> thinkpad_id
.bios_release
) ||
1821 (ec_version
> thinkpad_id
.ec_release
&&
1822 ec_version
!= TPACPI_MATCH_ANY
)) {
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1830 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1831 pr_warn("WARNING: This firmware may be missing critical bug "
1832 "fixes and/or important features\n");
1836 static bool __init
tpacpi_is_fw_known(void)
1838 return tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1839 ARRAY_SIZE(tpacpi_bios_version_qtable
)) != 0;
1842 /****************************************************************************
1843 ****************************************************************************
1847 ****************************************************************************
1848 ****************************************************************************/
1850 /*************************************************************************
1851 * thinkpad-acpi metadata subdriver
1854 static int thinkpad_acpi_driver_read(struct seq_file
*m
)
1856 seq_printf(m
, "driver:\t\t%s\n", TPACPI_DESC
);
1857 seq_printf(m
, "version:\t%s\n", TPACPI_VERSION
);
1861 static struct ibm_struct thinkpad_acpi_driver_data
= {
1863 .read
= thinkpad_acpi_driver_read
,
1866 /*************************************************************************
1871 * ThinkPad firmware event model
1873 * The ThinkPad firmware has two main event interfaces: normal ACPI
1874 * notifications (which follow the ACPI standard), and a private event
1877 * The private event interface also issues events for the hotkeys. As
1878 * the driver gained features, the event handling code ended up being
1879 * built around the hotkey subdriver. This will need to be refactored
1880 * to a more formal event API eventually.
1882 * Some "hotkeys" are actually supposed to be used as event reports,
1883 * such as "brightness has changed", "volume has changed", depending on
1884 * the ThinkPad model and how the firmware is operating.
1886 * Unlike other classes, hotkey-class events have mask/unmask control on
1887 * non-ancient firmware. However, how it behaves changes a lot with the
1888 * firmware model and version.
1891 enum { /* hot key scan codes (derived from ACPI DSDT) */
1892 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1893 TP_ACPI_HOTKEYSCAN_FNF2
,
1894 TP_ACPI_HOTKEYSCAN_FNF3
,
1895 TP_ACPI_HOTKEYSCAN_FNF4
,
1896 TP_ACPI_HOTKEYSCAN_FNF5
,
1897 TP_ACPI_HOTKEYSCAN_FNF6
,
1898 TP_ACPI_HOTKEYSCAN_FNF7
,
1899 TP_ACPI_HOTKEYSCAN_FNF8
,
1900 TP_ACPI_HOTKEYSCAN_FNF9
,
1901 TP_ACPI_HOTKEYSCAN_FNF10
,
1902 TP_ACPI_HOTKEYSCAN_FNF11
,
1903 TP_ACPI_HOTKEYSCAN_FNF12
,
1904 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1905 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1906 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1907 TP_ACPI_HOTKEYSCAN_FNHOME
,
1908 TP_ACPI_HOTKEYSCAN_FNEND
,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1910 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1911 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1913 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1914 TP_ACPI_HOTKEYSCAN_MUTE
,
1915 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1916 TP_ACPI_HOTKEYSCAN_UNK1
,
1917 TP_ACPI_HOTKEYSCAN_UNK2
,
1918 TP_ACPI_HOTKEYSCAN_UNK3
,
1919 TP_ACPI_HOTKEYSCAN_UNK4
,
1920 TP_ACPI_HOTKEYSCAN_UNK5
,
1921 TP_ACPI_HOTKEYSCAN_UNK6
,
1922 TP_ACPI_HOTKEYSCAN_UNK7
,
1923 TP_ACPI_HOTKEYSCAN_UNK8
,
1925 TP_ACPI_HOTKEYSCAN_MUTE2
,
1926 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO
,
1927 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL
,
1928 TP_ACPI_HOTKEYSCAN_CLOUD
,
1929 TP_ACPI_HOTKEYSCAN_UNK9
,
1930 TP_ACPI_HOTKEYSCAN_VOICE
,
1931 TP_ACPI_HOTKEYSCAN_UNK10
,
1932 TP_ACPI_HOTKEYSCAN_GESTURES
,
1933 TP_ACPI_HOTKEYSCAN_UNK11
,
1934 TP_ACPI_HOTKEYSCAN_UNK12
,
1935 TP_ACPI_HOTKEYSCAN_UNK13
,
1936 TP_ACPI_HOTKEYSCAN_CONFIG
,
1937 TP_ACPI_HOTKEYSCAN_NEW_TAB
,
1938 TP_ACPI_HOTKEYSCAN_RELOAD
,
1939 TP_ACPI_HOTKEYSCAN_BACK
,
1940 TP_ACPI_HOTKEYSCAN_MIC_DOWN
,
1941 TP_ACPI_HOTKEYSCAN_MIC_UP
,
1942 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION
,
1943 TP_ACPI_HOTKEYSCAN_CAMERA_MODE
,
1944 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY
,
1946 /* Hotkey keymap size */
1947 TPACPI_HOTKEY_MAP_LEN
1950 enum { /* Keys/events available through NVRAM polling */
1951 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1952 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1955 enum { /* Positions of some of the keys in hotkey masks */
1956 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1957 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1958 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1959 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1960 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1961 TP_ACPI_HKEY_KBD_LIGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1962 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1963 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1964 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1965 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1966 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1969 enum { /* NVRAM to ACPI HKEY group map */
1970 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1971 TP_ACPI_HKEY_ZOOM_MASK
|
1972 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1973 TP_ACPI_HKEY_HIBERNATE_MASK
,
1974 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1975 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1976 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1977 TP_ACPI_HKEY_VOLDWN_MASK
|
1978 TP_ACPI_HKEY_MUTE_MASK
,
1981 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1982 struct tp_nvram_state
{
1983 u16 thinkpad_toggle
:1;
1985 u16 display_toggle
:1;
1986 u16 thinklight_toggle
:1;
1987 u16 hibernate_toggle
:1;
1988 u16 displayexp_toggle
:1;
1989 u16 display_state
:1;
1990 u16 brightness_toggle
:1;
1991 u16 volume_toggle
:1;
1994 u8 brightness_level
;
1998 /* kthread for the hotkey poller */
1999 static struct task_struct
*tpacpi_hotkey_task
;
2002 * Acquire mutex to write poller control variables as an
2005 * Increment hotkey_config_change when changing them if you
2006 * want the kthread to forget old state.
2008 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2010 static struct mutex hotkey_thread_data_mutex
;
2011 static unsigned int hotkey_config_change
;
2014 * hotkey poller control variables
2016 * Must be atomic or readers will also need to acquire mutex
2018 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2019 * should be used only when the changes need to be taken as
2020 * a block, OR when one needs to force the kthread to forget
2023 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
2024 static unsigned int hotkey_poll_freq
= 10; /* Hz */
2026 #define HOTKEY_CONFIG_CRITICAL_START \
2028 mutex_lock(&hotkey_thread_data_mutex); \
2029 hotkey_config_change++; \
2031 #define HOTKEY_CONFIG_CRITICAL_END \
2032 mutex_unlock(&hotkey_thread_data_mutex);
2034 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2036 #define hotkey_source_mask 0U
2037 #define HOTKEY_CONFIG_CRITICAL_START
2038 #define HOTKEY_CONFIG_CRITICAL_END
2040 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2042 static struct mutex hotkey_mutex
;
2044 static enum { /* Reasons for waking up */
2045 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
2046 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
2047 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
2048 } hotkey_wakeup_reason
;
2050 static int hotkey_autosleep_ack
;
2052 static u32 hotkey_orig_mask
; /* events the BIOS had enabled */
2053 static u32 hotkey_all_mask
; /* all events supported in fw */
2054 static u32 hotkey_adaptive_all_mask
; /* all adaptive events supported in fw */
2055 static u32 hotkey_reserved_mask
; /* events better left disabled */
2056 static u32 hotkey_driver_mask
; /* events needed by the driver */
2057 static u32 hotkey_user_mask
; /* events visible to userspace */
2058 static u32 hotkey_acpi_mask
; /* events enabled in firmware */
2060 static u16
*hotkey_keycode_map
;
2062 static struct attribute_set
*hotkey_dev_attributes
;
2064 static void tpacpi_driver_event(const unsigned int hkey_event
);
2065 static void hotkey_driver_event(const unsigned int scancode
);
2066 static void hotkey_poll_setup(const bool may_warn
);
2068 /* HKEY.MHKG() return bits */
2069 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2070 /* ThinkPad X1 Yoga (2016) */
2071 #define TP_EC_CMMD_TABLET_MODE 0x6
2073 static int hotkey_get_wlsw(void)
2077 if (!tp_features
.hotkey_wlsw
)
2080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2082 return (tpacpi_wlsw_emulstate
) ?
2083 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2086 if (!acpi_evalf(hkey_handle
, &status
, "WLSW", "d"))
2089 return (status
) ? TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2092 static int hotkey_get_tablet_mode(int *status
)
2096 switch (tp_features
.hotkey_tablet
) {
2097 case TP_HOTKEY_TABLET_USES_MHKG
:
2098 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
2101 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
2103 case TP_HOTKEY_TABLET_USES_CMMD
:
2104 if (!acpi_evalf(ec_handle
, &s
, "CMMD", "d"))
2107 *status
= (s
== TP_EC_CMMD_TABLET_MODE
);
2117 * Reads current event mask from firmware, and updates
2118 * hotkey_acpi_mask accordingly. Also resets any bits
2119 * from hotkey_user_mask that are unavailable to be
2120 * delivered (shadow requirement of the userspace ABI).
2122 * Call with hotkey_mutex held
2124 static int hotkey_mask_get(void)
2126 if (tp_features
.hotkey_mask
) {
2129 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
2132 hotkey_acpi_mask
= m
;
2134 /* no mask support doesn't mean no event support... */
2135 hotkey_acpi_mask
= hotkey_all_mask
;
2138 /* sync userspace-visible mask */
2139 hotkey_user_mask
&= (hotkey_acpi_mask
| hotkey_source_mask
);
2144 static void hotkey_mask_warn_incomplete_mask(void)
2146 /* log only what the user can fix... */
2147 const u32 wantedmask
= hotkey_driver_mask
&
2148 ~(hotkey_acpi_mask
| hotkey_source_mask
) &
2149 (hotkey_all_mask
| TPACPI_HKEY_NVRAM_KNOWN_MASK
);
2152 pr_notice("required events 0x%08x not enabled!\n", wantedmask
);
2156 * Set the firmware mask when supported
2158 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2160 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2162 * Call with hotkey_mutex held
2164 static int hotkey_mask_set(u32 mask
)
2169 const u32 fwmask
= mask
& ~hotkey_source_mask
;
2171 if (tp_features
.hotkey_mask
) {
2172 for (i
= 0; i
< 32; i
++) {
2173 if (!acpi_evalf(hkey_handle
,
2174 NULL
, "MHKM", "vdd", i
+ 1,
2175 !!(mask
& (1 << i
)))) {
2183 * We *must* make an inconditional call to hotkey_mask_get to
2184 * refresh hotkey_acpi_mask and update hotkey_user_mask
2186 * Take the opportunity to also log when we cannot _enable_
2189 if (!hotkey_mask_get() && !rc
&& (fwmask
& ~hotkey_acpi_mask
)) {
2190 pr_notice("asked for hotkey mask 0x%08x, but "
2191 "firmware forced it to 0x%08x\n",
2192 fwmask
, hotkey_acpi_mask
);
2195 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
2196 hotkey_mask_warn_incomplete_mask();
2202 * Sets hotkey_user_mask and tries to set the firmware mask
2204 * Call with hotkey_mutex held
2206 static int hotkey_user_mask_set(const u32 mask
)
2210 /* Give people a chance to notice they are doing something that
2211 * is bound to go boom on their users sooner or later */
2212 if (!tp_warned
.hotkey_mask_ff
&&
2213 (mask
== 0xffff || mask
== 0xffffff ||
2214 mask
== 0xffffffff)) {
2215 tp_warned
.hotkey_mask_ff
= 1;
2216 pr_notice("setting the hotkey mask to 0x%08x is likely "
2217 "not the best way to go about it\n", mask
);
2218 pr_notice("please consider using the driver defaults, "
2219 "and refer to up-to-date thinkpad-acpi "
2223 /* Try to enable what the user asked for, plus whatever we need.
2224 * this syncs everything but won't enable bits in hotkey_user_mask */
2225 rc
= hotkey_mask_set((mask
| hotkey_driver_mask
) & ~hotkey_source_mask
);
2227 /* Enable the available bits in hotkey_user_mask */
2228 hotkey_user_mask
= mask
& (hotkey_acpi_mask
| hotkey_source_mask
);
2234 * Sets the driver hotkey mask.
2236 * Can be called even if the hotkey subdriver is inactive
2238 static int tpacpi_hotkey_driver_mask_set(const u32 mask
)
2242 /* Do the right thing if hotkey_init has not been called yet */
2243 if (!tp_features
.hotkey
) {
2244 hotkey_driver_mask
= mask
;
2248 mutex_lock(&hotkey_mutex
);
2250 HOTKEY_CONFIG_CRITICAL_START
2251 hotkey_driver_mask
= mask
;
2252 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2253 hotkey_source_mask
|= (mask
& ~hotkey_all_mask
);
2255 HOTKEY_CONFIG_CRITICAL_END
2257 rc
= hotkey_mask_set((hotkey_acpi_mask
| hotkey_driver_mask
) &
2258 ~hotkey_source_mask
);
2259 hotkey_poll_setup(true);
2261 mutex_unlock(&hotkey_mutex
);
2266 static int hotkey_status_get(int *status
)
2268 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2274 static int hotkey_status_set(bool enable
)
2276 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2282 static void tpacpi_input_send_tabletsw(void)
2286 if (tp_features
.hotkey_tablet
&&
2287 !hotkey_get_tablet_mode(&state
)) {
2288 mutex_lock(&tpacpi_inputdev_send_mutex
);
2290 input_report_switch(tpacpi_inputdev
,
2291 SW_TABLET_MODE
, !!state
);
2292 input_sync(tpacpi_inputdev
);
2294 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2298 /* Do NOT call without validating scancode first */
2299 static void tpacpi_input_send_key(const unsigned int scancode
)
2301 const unsigned int keycode
= hotkey_keycode_map
[scancode
];
2303 if (keycode
!= KEY_RESERVED
) {
2304 mutex_lock(&tpacpi_inputdev_send_mutex
);
2306 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
, scancode
);
2307 input_report_key(tpacpi_inputdev
, keycode
, 1);
2308 input_sync(tpacpi_inputdev
);
2310 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
, scancode
);
2311 input_report_key(tpacpi_inputdev
, keycode
, 0);
2312 input_sync(tpacpi_inputdev
);
2314 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2318 /* Do NOT call without validating scancode first */
2319 static void tpacpi_input_send_key_masked(const unsigned int scancode
)
2321 hotkey_driver_event(scancode
);
2322 if (hotkey_user_mask
& (1 << scancode
))
2323 tpacpi_input_send_key(scancode
);
2326 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2327 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
2329 /* Do NOT call without validating scancode first */
2330 static void tpacpi_hotkey_send_key(unsigned int scancode
)
2332 tpacpi_input_send_key_masked(scancode
);
2335 static void hotkey_read_nvram(struct tp_nvram_state
*n
, const u32 m
)
2339 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
2340 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
2341 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
2342 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
2343 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
2344 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
2346 if (m
& TP_ACPI_HKEY_KBD_LIGHT_MASK
) {
2347 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
2348 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
2350 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
2351 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
2352 n
->displayexp_toggle
=
2353 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
2355 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
2356 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
2357 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
2358 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
2359 n
->brightness_toggle
=
2360 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
2362 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
2363 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
2364 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
2365 >> TP_NVRAM_POS_LEVEL_VOLUME
;
2366 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
2367 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
2371 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2373 if ((event_mask & (1 << __scancode)) && \
2374 oldn->__member != newn->__member) \
2375 tpacpi_hotkey_send_key(__scancode); \
2378 #define TPACPI_MAY_SEND_KEY(__scancode) \
2380 if (event_mask & (1 << __scancode)) \
2381 tpacpi_hotkey_send_key(__scancode); \
2384 static void issue_volchange(const unsigned int oldvol
,
2385 const unsigned int newvol
,
2386 const u32 event_mask
)
2388 unsigned int i
= oldvol
;
2390 while (i
> newvol
) {
2391 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2394 while (i
< newvol
) {
2395 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2400 static void issue_brightnesschange(const unsigned int oldbrt
,
2401 const unsigned int newbrt
,
2402 const u32 event_mask
)
2404 unsigned int i
= oldbrt
;
2406 while (i
> newbrt
) {
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2410 while (i
< newbrt
) {
2411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2416 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
2417 struct tp_nvram_state
*newn
,
2418 const u32 event_mask
)
2421 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
2422 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
2423 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
2424 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
2426 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
2428 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
2433 * This code is supposed to duplicate the IBM firmware behaviour:
2434 * - Pressing MUTE issues mute hotkey message, even when already mute
2435 * - Pressing Volume up/down issues volume up/down hotkey messages,
2436 * even when already at maximum or minimum volume
2437 * - The act of unmuting issues volume up/down notification,
2438 * depending which key was used to unmute
2440 * We are constrained to what the NVRAM can tell us, which is not much
2441 * and certainly not enough if more than one volume hotkey was pressed
2442 * since the last poll cycle.
2444 * Just to make our life interesting, some newer Lenovo ThinkPads have
2445 * bugs in the BIOS and may fail to update volume_toggle properly.
2450 oldn
->volume_toggle
!= newn
->volume_toggle
||
2451 oldn
->volume_level
!= newn
->volume_level
) {
2452 /* recently muted, or repeated mute keypress, or
2453 * multiple presses ending in mute */
2454 issue_volchange(oldn
->volume_level
, newn
->volume_level
,
2456 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2461 /* recently unmuted, issue 'unmute' keypress */
2462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2464 if (oldn
->volume_level
!= newn
->volume_level
) {
2465 issue_volchange(oldn
->volume_level
, newn
->volume_level
,
2467 } else if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
2468 /* repeated vol up/down keypress at end of scale ? */
2469 if (newn
->volume_level
== 0)
2470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2471 else if (newn
->volume_level
>= TP_NVRAM_LEVEL_VOLUME_MAX
)
2472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2476 /* handle brightness */
2477 if (oldn
->brightness_level
!= newn
->brightness_level
) {
2478 issue_brightnesschange(oldn
->brightness_level
,
2479 newn
->brightness_level
, event_mask
);
2480 } else if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
2481 /* repeated key presses that didn't change state */
2482 if (newn
->brightness_level
== 0)
2483 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2484 else if (newn
->brightness_level
>= bright_maxlvl
2485 && !tp_features
.bright_unkfw
)
2486 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2489 #undef TPACPI_COMPARE_KEY
2490 #undef TPACPI_MAY_SEND_KEY
2496 * We track all events in hotkey_source_mask all the time, since
2497 * most of them are edge-based. We only issue those requested by
2498 * hotkey_user_mask or hotkey_driver_mask, though.
2500 static int hotkey_kthread(void *data
)
2502 struct tp_nvram_state s
[2];
2503 u32 poll_mask
, event_mask
;
2504 unsigned int si
, so
;
2506 unsigned int change_detector
;
2507 unsigned int poll_freq
;
2510 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
2519 /* Initial state for compares */
2520 mutex_lock(&hotkey_thread_data_mutex
);
2521 change_detector
= hotkey_config_change
;
2522 poll_mask
= hotkey_source_mask
;
2523 event_mask
= hotkey_source_mask
&
2524 (hotkey_driver_mask
| hotkey_user_mask
);
2525 poll_freq
= hotkey_poll_freq
;
2526 mutex_unlock(&hotkey_thread_data_mutex
);
2527 hotkey_read_nvram(&s
[so
], poll_mask
);
2529 while (!kthread_should_stop()) {
2531 if (likely(poll_freq
))
2534 t
= 100; /* should never happen... */
2536 t
= msleep_interruptible(t
);
2537 if (unlikely(kthread_freezable_should_stop(&was_frozen
)))
2540 if (t
> 0 && !was_frozen
)
2543 mutex_lock(&hotkey_thread_data_mutex
);
2544 if (was_frozen
|| hotkey_config_change
!= change_detector
) {
2545 /* forget old state on thaw or config change */
2548 change_detector
= hotkey_config_change
;
2550 poll_mask
= hotkey_source_mask
;
2551 event_mask
= hotkey_source_mask
&
2552 (hotkey_driver_mask
| hotkey_user_mask
);
2553 poll_freq
= hotkey_poll_freq
;
2554 mutex_unlock(&hotkey_thread_data_mutex
);
2556 if (likely(poll_mask
)) {
2557 hotkey_read_nvram(&s
[si
], poll_mask
);
2558 if (likely(si
!= so
)) {
2559 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
2572 /* call with hotkey_mutex held */
2573 static void hotkey_poll_stop_sync(void)
2575 if (tpacpi_hotkey_task
) {
2576 kthread_stop(tpacpi_hotkey_task
);
2577 tpacpi_hotkey_task
= NULL
;
2581 /* call with hotkey_mutex held */
2582 static void hotkey_poll_setup(const bool may_warn
)
2584 const u32 poll_driver_mask
= hotkey_driver_mask
& hotkey_source_mask
;
2585 const u32 poll_user_mask
= hotkey_user_mask
& hotkey_source_mask
;
2587 if (hotkey_poll_freq
> 0 &&
2588 (poll_driver_mask
||
2589 (poll_user_mask
&& tpacpi_inputdev
->users
> 0))) {
2590 if (!tpacpi_hotkey_task
) {
2591 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
2592 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
2593 if (IS_ERR(tpacpi_hotkey_task
)) {
2594 tpacpi_hotkey_task
= NULL
;
2595 pr_err("could not create kernel thread "
2596 "for hotkey polling\n");
2600 hotkey_poll_stop_sync();
2601 if (may_warn
&& (poll_driver_mask
|| poll_user_mask
) &&
2602 hotkey_poll_freq
== 0) {
2603 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2604 "require polling, which is currently "
2606 poll_user_mask
, poll_driver_mask
);
2611 static void hotkey_poll_setup_safe(const bool may_warn
)
2613 mutex_lock(&hotkey_mutex
);
2614 hotkey_poll_setup(may_warn
);
2615 mutex_unlock(&hotkey_mutex
);
2618 /* call with hotkey_mutex held */
2619 static void hotkey_poll_set_freq(unsigned int freq
)
2622 hotkey_poll_stop_sync();
2624 hotkey_poll_freq
= freq
;
2627 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2629 static void hotkey_poll_setup(const bool __unused
)
2633 static void hotkey_poll_setup_safe(const bool __unused
)
2637 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2639 static int hotkey_inputdev_open(struct input_dev
*dev
)
2641 switch (tpacpi_lifecycle
) {
2642 case TPACPI_LIFE_INIT
:
2643 case TPACPI_LIFE_RUNNING
:
2644 hotkey_poll_setup_safe(false);
2646 case TPACPI_LIFE_EXITING
:
2650 /* Should only happen if tpacpi_lifecycle is corrupt */
2655 static void hotkey_inputdev_close(struct input_dev
*dev
)
2657 /* disable hotkey polling when possible */
2658 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
&&
2659 !(hotkey_source_mask
& hotkey_driver_mask
))
2660 hotkey_poll_setup_safe(false);
2663 /* sysfs hotkey enable ------------------------------------------------- */
2664 static ssize_t
hotkey_enable_show(struct device
*dev
,
2665 struct device_attribute
*attr
,
2670 printk_deprecated_attribute("hotkey_enable",
2671 "Hotkey reporting is always enabled");
2673 res
= hotkey_status_get(&status
);
2677 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
2680 static ssize_t
hotkey_enable_store(struct device
*dev
,
2681 struct device_attribute
*attr
,
2682 const char *buf
, size_t count
)
2686 printk_deprecated_attribute("hotkey_enable",
2687 "Hotkeys can be disabled through hotkey_mask");
2689 if (parse_strtoul(buf
, 1, &t
))
2698 static DEVICE_ATTR_RW(hotkey_enable
);
2700 /* sysfs hotkey mask --------------------------------------------------- */
2701 static ssize_t
hotkey_mask_show(struct device
*dev
,
2702 struct device_attribute
*attr
,
2705 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_user_mask
);
2708 static ssize_t
hotkey_mask_store(struct device
*dev
,
2709 struct device_attribute
*attr
,
2710 const char *buf
, size_t count
)
2715 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2718 if (mutex_lock_killable(&hotkey_mutex
))
2719 return -ERESTARTSYS
;
2721 res
= hotkey_user_mask_set(t
);
2723 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2724 hotkey_poll_setup(true);
2727 mutex_unlock(&hotkey_mutex
);
2729 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2731 return (res
) ? res
: count
;
2734 static DEVICE_ATTR_RW(hotkey_mask
);
2736 /* sysfs hotkey bios_enabled ------------------------------------------- */
2737 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2738 struct device_attribute
*attr
,
2741 return sprintf(buf
, "0\n");
2744 static DEVICE_ATTR_RO(hotkey_bios_enabled
);
2746 /* sysfs hotkey bios_mask ---------------------------------------------- */
2747 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2748 struct device_attribute
*attr
,
2751 printk_deprecated_attribute("hotkey_bios_mask",
2752 "This attribute is useless.");
2753 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2756 static DEVICE_ATTR_RO(hotkey_bios_mask
);
2758 /* sysfs hotkey all_mask ----------------------------------------------- */
2759 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2760 struct device_attribute
*attr
,
2763 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2764 hotkey_all_mask
| hotkey_source_mask
);
2767 static DEVICE_ATTR_RO(hotkey_all_mask
);
2769 /* sysfs hotkey all_mask ----------------------------------------------- */
2770 static ssize_t
hotkey_adaptive_all_mask_show(struct device
*dev
,
2771 struct device_attribute
*attr
,
2774 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2775 hotkey_adaptive_all_mask
| hotkey_source_mask
);
2778 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask
);
2780 /* sysfs hotkey recommended_mask --------------------------------------- */
2781 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2782 struct device_attribute
*attr
,
2785 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2786 (hotkey_all_mask
| hotkey_source_mask
)
2787 & ~hotkey_reserved_mask
);
2790 static DEVICE_ATTR_RO(hotkey_recommended_mask
);
2792 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2794 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2795 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2796 struct device_attribute
*attr
,
2799 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2802 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2803 struct device_attribute
*attr
,
2804 const char *buf
, size_t count
)
2810 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2811 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2814 if (mutex_lock_killable(&hotkey_mutex
))
2815 return -ERESTARTSYS
;
2817 HOTKEY_CONFIG_CRITICAL_START
2818 hotkey_source_mask
= t
;
2819 HOTKEY_CONFIG_CRITICAL_END
2821 rc
= hotkey_mask_set((hotkey_user_mask
| hotkey_driver_mask
) &
2822 ~hotkey_source_mask
);
2823 hotkey_poll_setup(true);
2825 /* check if events needed by the driver got disabled */
2826 r_ev
= hotkey_driver_mask
& ~(hotkey_acpi_mask
& hotkey_all_mask
)
2827 & ~hotkey_source_mask
& TPACPI_HKEY_NVRAM_KNOWN_MASK
;
2829 mutex_unlock(&hotkey_mutex
);
2832 pr_err("hotkey_source_mask: "
2833 "failed to update the firmware event mask!\n");
2836 pr_notice("hotkey_source_mask: "
2837 "some important events were disabled: 0x%04x\n",
2840 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2842 return (rc
< 0) ? rc
: count
;
2845 static DEVICE_ATTR_RW(hotkey_source_mask
);
2847 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2848 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2849 struct device_attribute
*attr
,
2852 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2855 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2856 struct device_attribute
*attr
,
2857 const char *buf
, size_t count
)
2861 if (parse_strtoul(buf
, 25, &t
))
2864 if (mutex_lock_killable(&hotkey_mutex
))
2865 return -ERESTARTSYS
;
2867 hotkey_poll_set_freq(t
);
2868 hotkey_poll_setup(true);
2870 mutex_unlock(&hotkey_mutex
);
2872 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2877 static DEVICE_ATTR_RW(hotkey_poll_freq
);
2879 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2881 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2882 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2883 struct device_attribute
*attr
,
2887 res
= hotkey_get_wlsw();
2891 /* Opportunistic update */
2892 tpacpi_rfk_update_hwblock_state((res
== TPACPI_RFK_RADIO_OFF
));
2894 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2895 (res
== TPACPI_RFK_RADIO_OFF
) ? 0 : 1);
2898 static DEVICE_ATTR_RO(hotkey_radio_sw
);
2900 static void hotkey_radio_sw_notify_change(void)
2902 if (tp_features
.hotkey_wlsw
)
2903 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2907 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2908 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2909 struct device_attribute
*attr
,
2913 res
= hotkey_get_tablet_mode(&s
);
2917 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2920 static DEVICE_ATTR_RO(hotkey_tablet_mode
);
2922 static void hotkey_tablet_mode_notify_change(void)
2924 if (tp_features
.hotkey_tablet
)
2925 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2926 "hotkey_tablet_mode");
2929 /* sysfs wakeup reason (pollable) -------------------------------------- */
2930 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2931 struct device_attribute
*attr
,
2934 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2937 static DEVICE_ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2939 static void hotkey_wakeup_reason_notify_change(void)
2941 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2945 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2946 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2947 struct device_attribute
*attr
,
2950 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2953 static DEVICE_ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2954 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2956 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2958 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2959 "wakeup_hotunplug_complete");
2962 /* sysfs adaptive kbd mode --------------------------------------------- */
2964 static int adaptive_keyboard_get_mode(void);
2965 static int adaptive_keyboard_set_mode(int new_mode
);
2967 enum ADAPTIVE_KEY_MODE
{
2970 WEB_CONFERENCE_MODE
,
2975 static ssize_t
adaptive_kbd_mode_show(struct device
*dev
,
2976 struct device_attribute
*attr
,
2981 current_mode
= adaptive_keyboard_get_mode();
2982 if (current_mode
< 0)
2983 return current_mode
;
2985 return snprintf(buf
, PAGE_SIZE
, "%d\n", current_mode
);
2988 static ssize_t
adaptive_kbd_mode_store(struct device
*dev
,
2989 struct device_attribute
*attr
,
2990 const char *buf
, size_t count
)
2995 if (parse_strtoul(buf
, LAYFLAT_MODE
, &t
))
2998 res
= adaptive_keyboard_set_mode(t
);
2999 return (res
< 0) ? res
: count
;
3002 static DEVICE_ATTR_RW(adaptive_kbd_mode
);
3004 static struct attribute
*adaptive_kbd_attributes
[] = {
3005 &dev_attr_adaptive_kbd_mode
.attr
,
3009 static const struct attribute_group adaptive_kbd_attr_group
= {
3010 .attrs
= adaptive_kbd_attributes
,
3013 /* --------------------------------------------------------------------- */
3015 static struct attribute
*hotkey_attributes
[] __initdata
= {
3016 &dev_attr_hotkey_enable
.attr
,
3017 &dev_attr_hotkey_bios_enabled
.attr
,
3018 &dev_attr_hotkey_bios_mask
.attr
,
3019 &dev_attr_wakeup_reason
.attr
,
3020 &dev_attr_wakeup_hotunplug_complete
.attr
,
3021 &dev_attr_hotkey_mask
.attr
,
3022 &dev_attr_hotkey_all_mask
.attr
,
3023 &dev_attr_hotkey_adaptive_all_mask
.attr
,
3024 &dev_attr_hotkey_recommended_mask
.attr
,
3025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3026 &dev_attr_hotkey_source_mask
.attr
,
3027 &dev_attr_hotkey_poll_freq
.attr
,
3032 * Sync both the hw and sw blocking state of all switches
3034 static void tpacpi_send_radiosw_update(void)
3039 * We must sync all rfkill controllers *before* issuing any
3040 * rfkill input events, or we will race the rfkill core input
3043 * tpacpi_inputdev_send_mutex works as a synchronization point
3046 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3049 wlsw
= hotkey_get_wlsw();
3051 /* Sync hw blocking state first if it is hw-blocked */
3052 if (wlsw
== TPACPI_RFK_RADIO_OFF
)
3053 tpacpi_rfk_update_hwblock_state(true);
3055 /* Sync sw blocking state */
3056 tpacpi_rfk_update_swstate_all();
3058 /* Sync hw blocking state last if it is hw-unblocked */
3059 if (wlsw
== TPACPI_RFK_RADIO_ON
)
3060 tpacpi_rfk_update_hwblock_state(false);
3062 /* Issue rfkill input event for WLSW switch */
3064 mutex_lock(&tpacpi_inputdev_send_mutex
);
3066 input_report_switch(tpacpi_inputdev
,
3067 SW_RFKILL_ALL
, (wlsw
> 0));
3068 input_sync(tpacpi_inputdev
);
3070 mutex_unlock(&tpacpi_inputdev_send_mutex
);
3074 * this can be unconditional, as we will poll state again
3075 * if userspace uses the notify to read data
3077 hotkey_radio_sw_notify_change();
3080 static void hotkey_exit(void)
3082 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3083 mutex_lock(&hotkey_mutex
);
3084 hotkey_poll_stop_sync();
3085 mutex_unlock(&hotkey_mutex
);
3088 if (hotkey_dev_attributes
)
3089 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3091 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
3092 "restoring original HKEY status and mask\n");
3093 /* yes, there is a bitwise or below, we want the
3094 * functions to be called even if one of them fail */
3095 if (((tp_features
.hotkey_mask
&&
3096 hotkey_mask_set(hotkey_orig_mask
)) |
3097 hotkey_status_set(false)) != 0)
3098 pr_err("failed to restore hot key mask "
3099 "to BIOS defaults\n");
3102 static void __init
hotkey_unmap(const unsigned int scancode
)
3104 if (hotkey_keycode_map
[scancode
] != KEY_RESERVED
) {
3105 clear_bit(hotkey_keycode_map
[scancode
],
3106 tpacpi_inputdev
->keybit
);
3107 hotkey_keycode_map
[scancode
] = KEY_RESERVED
;
3113 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3116 #define TPACPI_HK_Q_INIMASK 0x0001
3118 static const struct tpacpi_quirk tpacpi_hotkey_qtable
[] __initconst
= {
3119 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK
), /* 600E */
3120 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK
), /* 600E */
3121 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK
), /* 770, 770E, 770ED */
3122 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK
), /* A20m */
3123 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK
), /* A20p */
3124 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK
), /* A21e, A22e */
3125 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK
), /* A21e */
3126 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK
), /* A21m, A22m */
3127 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK
), /* A21p, A22p */
3128 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK
), /* A22e */
3129 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK
), /* A22m */
3130 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK
), /* A30/p (0) */
3131 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK
), /* R30 */
3132 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK
), /* R31 */
3133 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK
), /* T20 */
3134 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK
), /* T21 */
3135 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK
), /* T22 */
3136 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK
), /* X20, X21 */
3137 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK
), /* X22, X23, X24 */
3140 typedef u16 tpacpi_keymap_entry_t
;
3141 typedef tpacpi_keymap_entry_t tpacpi_keymap_t
[TPACPI_HOTKEY_MAP_LEN
];
3143 static int hotkey_init_tablet_mode(void)
3145 int in_tablet_mode
= 0, res
;
3148 if (acpi_evalf(hkey_handle
, &res
, "MHKG", "qd")) {
3149 /* For X41t, X60t, X61t Tablets... */
3150 tp_features
.hotkey_tablet
= TP_HOTKEY_TABLET_USES_MHKG
;
3151 in_tablet_mode
= !!(res
& TP_HOTKEY_TABLET_MASK
);
3153 } else if (acpi_evalf(ec_handle
, &res
, "CMMD", "qd")) {
3154 /* For X1 Yoga (2016) */
3155 tp_features
.hotkey_tablet
= TP_HOTKEY_TABLET_USES_CMMD
;
3156 in_tablet_mode
= res
== TP_EC_CMMD_TABLET_MODE
;
3160 if (!tp_features
.hotkey_tablet
)
3163 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3164 type
, in_tablet_mode
? "tablet" : "laptop");
3166 res
= add_to_attr_set(hotkey_dev_attributes
,
3167 &dev_attr_hotkey_tablet_mode
.attr
);
3171 return in_tablet_mode
;
3174 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
3176 /* Requirements for changing the default keymaps:
3178 * 1. Many of the keys are mapped to KEY_RESERVED for very
3179 * good reasons. Do not change them unless you have deep
3180 * knowledge on the IBM and Lenovo ThinkPad firmware for
3181 * the various ThinkPad models. The driver behaves
3182 * differently for KEY_RESERVED: such keys have their
3183 * hot key mask *unset* in mask_recommended, and also
3184 * in the initial hot key mask programmed into the
3185 * firmware at driver load time, which means the firm-
3186 * ware may react very differently if you change them to
3189 * 2. You must be subscribed to the linux-thinkpad and
3190 * ibm-acpi-devel mailing lists, and you should read the
3191 * list archives since 2007 if you want to change the
3192 * keymaps. This requirement exists so that you will
3193 * know the past history of problems with the thinkpad-
3194 * acpi driver keymaps, and also that you will be
3195 * listening to any bug reports;
3197 * 3. Do not send thinkpad-acpi specific patches directly to
3198 * for merging, *ever*. Send them to the linux-acpi
3199 * mailinglist for comments. Merging is to be done only
3200 * through acpi-test and the ACPI maintainer.
3202 * If the above is too much to ask, don't change the keymap.
3203 * Ask the thinkpad-acpi maintainer to do it, instead.
3207 TPACPI_KEYMAP_IBM_GENERIC
= 0,
3208 TPACPI_KEYMAP_LENOVO_GENERIC
,
3211 static const tpacpi_keymap_t tpacpi_keymaps
[] __initconst
= {
3212 /* Generic keymap for IBM ThinkPads */
3213 [TPACPI_KEYMAP_IBM_GENERIC
] = {
3214 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3215 KEY_FN_F1
, KEY_BATTERY
, KEY_COFFEE
, KEY_SLEEP
,
3216 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3217 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3219 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3220 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3221 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3222 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3224 /* brightness: firmware always reacts to them */
3225 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
3226 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
3228 /* Thinklight: firmware always react to it */
3229 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3231 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3232 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3234 /* Volume: firmware always react to it and reprograms
3235 * the built-in *extra* mixer. Never map it to control
3236 * another mixer by default. */
3237 KEY_RESERVED
, /* 0x14: VOLUME UP */
3238 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3239 KEY_RESERVED
, /* 0x16: MUTE */
3241 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3243 /* (assignments unknown, please report if found) */
3244 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3245 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3247 /* No assignments, only used for Adaptive keyboards. */
3248 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3249 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3250 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3251 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3252 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3255 /* Generic keymap for Lenovo ThinkPads */
3256 [TPACPI_KEYMAP_LENOVO_GENERIC
] = {
3257 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3258 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
3259 KEY_WLAN
, KEY_CAMERA
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3260 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3262 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3263 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3264 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3265 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3267 /* These should be enabled --only-- when ACPI video
3268 * is disabled (i.e. in "vendor" mode), and are handled
3269 * in a special way by the init code */
3270 KEY_BRIGHTNESSUP
, /* 0x0F: FN+HOME (brightness up) */
3271 KEY_BRIGHTNESSDOWN
, /* 0x10: FN+END (brightness down) */
3273 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3275 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3276 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3278 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3279 * react to it and reprograms the built-in *extra* mixer.
3280 * Never map it to control another mixer by default.
3282 * T60?, T61, R60?, R61: firmware and EC tries to send
3283 * these over the regular keyboard, so these are no-ops,
3284 * but there are still weird bugs re. MUTE, so do not
3285 * change unless you get test reports from all Lenovo
3286 * models. May cause the BIOS to interfere with the
3289 KEY_RESERVED
, /* 0x14: VOLUME UP */
3290 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3291 KEY_RESERVED
, /* 0x16: MUTE */
3293 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3295 /* (assignments unknown, please report if found) */
3296 KEY_UNKNOWN
, KEY_UNKNOWN
,
3299 * The mic mute button only sends 0x1a. It does not
3300 * automatically mute the mic or change the mute light.
3302 KEY_MICMUTE
, /* 0x1a: Mic mute (since ?400 or so) */
3304 /* (assignments unknown, please report if found) */
3307 /* Extra keys in use since the X240 / T440 / T540 */
3308 KEY_CONFIG
, KEY_SEARCH
, KEY_SCALE
, KEY_FILE
,
3311 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3312 * The first item in this list is the Mute button which is
3313 * emitted with 0x103 through
3314 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3316 * We'll need to offset those by 0x20.
3318 KEY_RESERVED
, /* Mute held, 0x103 */
3319 KEY_BRIGHTNESS_MIN
, /* Backlight off */
3320 KEY_RESERVED
, /* Clipping tool */
3321 KEY_RESERVED
, /* Cloud */
3323 KEY_VOICECOMMAND
, /* Voice */
3325 KEY_RESERVED
, /* Gestures */
3329 KEY_CONFIG
, /* Settings */
3330 KEY_RESERVED
, /* New tab */
3331 KEY_REFRESH
, /* Reload */
3332 KEY_BACK
, /* Back */
3333 KEY_RESERVED
, /* Microphone down */
3334 KEY_RESERVED
, /* Microphone up */
3335 KEY_RESERVED
, /* Microphone cancellation */
3336 KEY_RESERVED
, /* Camera mode */
3337 KEY_RESERVED
, /* Rotate display, 0x116 */
3341 static const struct tpacpi_quirk tpacpi_keymap_qtable
[] __initconst
= {
3342 /* Generic maps (fallback) */
3344 .vendor
= PCI_VENDOR_ID_IBM
,
3345 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
3346 .quirks
= TPACPI_KEYMAP_IBM_GENERIC
,
3349 .vendor
= PCI_VENDOR_ID_LENOVO
,
3350 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
3351 .quirks
= TPACPI_KEYMAP_LENOVO_GENERIC
,
3355 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3356 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3361 bool radiosw_state
= false;
3362 bool tabletsw_state
= false;
3364 unsigned long quirks
;
3365 unsigned long keymap_id
;
3367 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3368 "initializing hotkey subdriver\n");
3370 BUG_ON(!tpacpi_inputdev
);
3371 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
3372 tpacpi_inputdev
->close
!= NULL
);
3374 TPACPI_ACPIHANDLE_INIT(hkey
);
3375 mutex_init(&hotkey_mutex
);
3377 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3378 mutex_init(&hotkey_thread_data_mutex
);
3381 /* hotkey not supported on 570 */
3382 tp_features
.hotkey
= hkey_handle
!= NULL
;
3384 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3386 str_supported(tp_features
.hotkey
));
3388 if (!tp_features
.hotkey
)
3391 quirks
= tpacpi_check_quirks(tpacpi_hotkey_qtable
,
3392 ARRAY_SIZE(tpacpi_hotkey_qtable
));
3394 tpacpi_disable_brightness_delay();
3396 /* MUST have enough space for all attributes to be added to
3397 * hotkey_dev_attributes */
3398 hotkey_dev_attributes
= create_attr_set(
3399 ARRAY_SIZE(hotkey_attributes
) + 2,
3401 if (!hotkey_dev_attributes
)
3403 res
= add_many_to_attr_set(hotkey_dev_attributes
,
3405 ARRAY_SIZE(hotkey_attributes
));
3409 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3410 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3411 for HKEY interface version 0x100 */
3412 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
3413 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3414 "firmware HKEY interface version: 0x%x\n",
3417 switch (hkeyv
>> 8) {
3420 * MHKV 0x100 in A31, R40, R40e,
3421 * T4x, X31, and later
3424 /* Paranoia check AND init hotkey_all_mask */
3425 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
3427 pr_err("missing MHKA handler, please report this to %s\n",
3429 /* Fallback: pre-init for FN+F3,F4,F12 */
3430 hotkey_all_mask
= 0x080cU
;
3432 tp_features
.hotkey_mask
= 1;
3438 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3441 /* Paranoia check AND init hotkey_all_mask */
3442 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
3444 pr_err("missing MHKA handler, please report this to %s\n",
3446 /* Fallback: pre-init for FN+F3,F4,F12 */
3447 hotkey_all_mask
= 0x080cU
;
3449 tp_features
.hotkey_mask
= 1;
3453 * Check if we have an adaptive keyboard, like on the
3454 * Lenovo Carbon X1 2014 (2nd Gen).
3456 if (acpi_evalf(hkey_handle
, &hotkey_adaptive_all_mask
,
3458 if (hotkey_adaptive_all_mask
!= 0) {
3459 tp_features
.has_adaptive_kbd
= true;
3460 res
= sysfs_create_group(
3461 &tpacpi_pdev
->dev
.kobj
,
3462 &adaptive_kbd_attr_group
);
3467 tp_features
.has_adaptive_kbd
= false;
3468 hotkey_adaptive_all_mask
= 0x0U
;
3473 pr_err("unknown version of the HKEY interface: 0x%x\n",
3475 pr_err("please report this to %s\n", TPACPI_MAIL
);
3480 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3481 "hotkey masks are %s\n",
3482 str_supported(tp_features
.hotkey_mask
));
3484 /* Init hotkey_all_mask if not initialized yet */
3485 if (!tp_features
.hotkey_mask
&& !hotkey_all_mask
&&
3486 (quirks
& TPACPI_HK_Q_INIMASK
))
3487 hotkey_all_mask
= 0x080cU
; /* FN+F12, FN+F4, FN+F3 */
3489 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3490 if (tp_features
.hotkey_mask
) {
3491 /* hotkey_source_mask *must* be zero for
3492 * the first hotkey_mask_get to return hotkey_orig_mask */
3493 res
= hotkey_mask_get();
3497 hotkey_orig_mask
= hotkey_acpi_mask
;
3499 hotkey_orig_mask
= hotkey_all_mask
;
3500 hotkey_acpi_mask
= hotkey_all_mask
;
3503 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3505 tp_features
.hotkey_wlsw
= 1;
3506 radiosw_state
= !!tpacpi_wlsw_emulstate
;
3507 pr_info("radio switch emulation enabled\n");
3510 /* Not all thinkpads have a hardware radio switch */
3511 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
3512 tp_features
.hotkey_wlsw
= 1;
3513 radiosw_state
= !!status
;
3514 pr_info("radio switch found; radios are %s\n",
3515 enabled(status
, 0));
3517 if (tp_features
.hotkey_wlsw
)
3518 res
= add_to_attr_set(hotkey_dev_attributes
,
3519 &dev_attr_hotkey_radio_sw
.attr
);
3521 res
= hotkey_init_tablet_mode();
3525 tabletsw_state
= res
;
3527 res
= register_attr_set_with_sysfs(hotkey_dev_attributes
,
3528 &tpacpi_pdev
->dev
.kobj
);
3532 /* Set up key map */
3533 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
3535 if (!hotkey_keycode_map
) {
3536 pr_err("failed to allocate memory for key map\n");
3541 keymap_id
= tpacpi_check_quirks(tpacpi_keymap_qtable
,
3542 ARRAY_SIZE(tpacpi_keymap_qtable
));
3543 BUG_ON(keymap_id
>= ARRAY_SIZE(tpacpi_keymaps
));
3544 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3545 "using keymap number %lu\n", keymap_id
);
3547 memcpy(hotkey_keycode_map
, &tpacpi_keymaps
[keymap_id
],
3548 TPACPI_HOTKEY_MAP_SIZE
);
3550 input_set_capability(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
);
3551 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
3552 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
3553 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
3554 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
3555 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
3556 input_set_capability(tpacpi_inputdev
, EV_KEY
,
3557 hotkey_keycode_map
[i
]);
3559 if (i
< sizeof(hotkey_reserved_mask
)*8)
3560 hotkey_reserved_mask
|= 1 << i
;
3564 if (tp_features
.hotkey_wlsw
) {
3565 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_RFKILL_ALL
);
3566 input_report_switch(tpacpi_inputdev
,
3567 SW_RFKILL_ALL
, radiosw_state
);
3569 if (tp_features
.hotkey_tablet
) {
3570 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_TABLET_MODE
);
3571 input_report_switch(tpacpi_inputdev
,
3572 SW_TABLET_MODE
, tabletsw_state
);
3575 /* Do not issue duplicate brightness change events to
3576 * userspace. tpacpi_detect_brightness_capabilities() must have
3577 * been called before this point */
3578 if (acpi_video_get_backlight_type() != acpi_backlight_vendor
) {
3579 pr_info("This ThinkPad has standard ACPI backlight "
3580 "brightness control, supported by the ACPI "
3582 pr_notice("Disabling thinkpad-acpi brightness events "
3585 /* Disable brightness up/down on Lenovo thinkpads when
3586 * ACPI is handling them, otherwise it is plain impossible
3587 * for userspace to do something even remotely sane */
3588 hotkey_reserved_mask
|=
3589 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
3590 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
3591 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME
);
3592 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND
);
3595 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3596 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
3598 & ~hotkey_reserved_mask
;
3600 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3601 "hotkey source mask 0x%08x, polling freq %u\n",
3602 hotkey_source_mask
, hotkey_poll_freq
);
3605 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3606 "enabling firmware HKEY event interface...\n");
3607 res
= hotkey_status_set(true);
3612 res
= hotkey_mask_set(((hotkey_all_mask
& ~hotkey_reserved_mask
)
3613 | hotkey_driver_mask
)
3614 & ~hotkey_source_mask
);
3615 if (res
< 0 && res
!= -ENXIO
) {
3619 hotkey_user_mask
= (hotkey_acpi_mask
| hotkey_source_mask
)
3620 & ~hotkey_reserved_mask
;
3621 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3622 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3623 hotkey_user_mask
, hotkey_acpi_mask
, hotkey_source_mask
);
3625 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
3626 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
3628 hotkey_poll_setup_safe(true);
3633 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3634 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3635 &adaptive_kbd_attr_group
);
3637 hotkey_dev_attributes
= NULL
;
3639 return (res
< 0) ? res
: 1;
3642 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3643 * mode, Web conference mode, Function mode and Lay-flat mode.
3644 * We support Home mode and Function mode currently.
3646 * Will consider support rest of modes in future.
3649 static const int adaptive_keyboard_modes
[] = {
3651 /* WEB_BROWSER_MODE = 2,
3652 WEB_CONFERENCE_MODE = 3, */
3656 #define DFR_CHANGE_ROW 0x101
3657 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3658 #define FIRST_ADAPTIVE_KEY 0x103
3659 #define ADAPTIVE_KEY_OFFSET 0x020
3661 /* press Fn key a while second, it will switch to Function Mode. Then
3662 * release Fn key, previous mode be restored.
3664 static bool adaptive_keyboard_mode_is_saved
;
3665 static int adaptive_keyboard_prev_mode
;
3667 static int adaptive_keyboard_get_mode(void)
3671 if (!acpi_evalf(hkey_handle
, &mode
, "GTRW", "dd", 0)) {
3672 pr_err("Cannot read adaptive keyboard mode\n");
3679 static int adaptive_keyboard_set_mode(int new_mode
)
3682 new_mode
> LAYFLAT_MODE
)
3685 if (!acpi_evalf(hkey_handle
, NULL
, "STRW", "vd", new_mode
)) {
3686 pr_err("Cannot set adaptive keyboard mode\n");
3693 static int adaptive_keyboard_get_next_mode(int mode
)
3696 size_t max_mode
= ARRAY_SIZE(adaptive_keyboard_modes
) - 1;
3698 for (i
= 0; i
<= max_mode
; i
++) {
3699 if (adaptive_keyboard_modes
[i
] == mode
)
3708 return adaptive_keyboard_modes
[i
];
3711 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode
)
3713 int current_mode
= 0;
3718 case DFR_CHANGE_ROW
:
3719 if (adaptive_keyboard_mode_is_saved
) {
3720 new_mode
= adaptive_keyboard_prev_mode
;
3721 adaptive_keyboard_mode_is_saved
= false;
3723 current_mode
= adaptive_keyboard_get_mode();
3724 if (current_mode
< 0)
3726 new_mode
= adaptive_keyboard_get_next_mode(
3730 if (adaptive_keyboard_set_mode(new_mode
) < 0)
3735 case DFR_SHOW_QUICKVIEW_ROW
:
3736 current_mode
= adaptive_keyboard_get_mode();
3737 if (current_mode
< 0)
3740 adaptive_keyboard_prev_mode
= current_mode
;
3741 adaptive_keyboard_mode_is_saved
= true;
3743 if (adaptive_keyboard_set_mode (FUNCTION_MODE
) < 0)
3748 if (scancode
< FIRST_ADAPTIVE_KEY
||
3749 scancode
>= FIRST_ADAPTIVE_KEY
+ TPACPI_HOTKEY_MAP_LEN
-
3750 ADAPTIVE_KEY_OFFSET
) {
3751 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3755 keycode
= hotkey_keycode_map
[scancode
- FIRST_ADAPTIVE_KEY
+ ADAPTIVE_KEY_OFFSET
];
3756 if (keycode
!= KEY_RESERVED
) {
3757 mutex_lock(&tpacpi_inputdev_send_mutex
);
3759 input_report_key(tpacpi_inputdev
, keycode
, 1);
3760 input_sync(tpacpi_inputdev
);
3762 input_report_key(tpacpi_inputdev
, keycode
, 0);
3763 input_sync(tpacpi_inputdev
);
3765 mutex_unlock(&tpacpi_inputdev_send_mutex
);
3771 static bool hotkey_notify_hotkey(const u32 hkey
,
3773 bool *ignore_acpi_ev
)
3775 /* 0x1000-0x1FFF: key presses */
3776 unsigned int scancode
= hkey
& 0xfff;
3777 *send_acpi_ev
= true;
3778 *ignore_acpi_ev
= false;
3780 /* HKEY event 0x1001 is scancode 0x00 */
3781 if (scancode
> 0 && scancode
<= TPACPI_HOTKEY_MAP_LEN
) {
3783 if (!(hotkey_source_mask
& (1 << scancode
))) {
3784 tpacpi_input_send_key_masked(scancode
);
3785 *send_acpi_ev
= false;
3787 *ignore_acpi_ev
= true;
3791 return adaptive_keyboard_hotkey_notify_hotkey(scancode
);
3796 static bool hotkey_notify_wakeup(const u32 hkey
,
3798 bool *ignore_acpi_ev
)
3800 /* 0x2000-0x2FFF: Wakeup reason */
3801 *send_acpi_ev
= true;
3802 *ignore_acpi_ev
= false;
3805 case TP_HKEY_EV_WKUP_S3_UNDOCK
: /* suspend, undock */
3806 case TP_HKEY_EV_WKUP_S4_UNDOCK
: /* hibernation, undock */
3807 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
3808 *ignore_acpi_ev
= true;
3811 case TP_HKEY_EV_WKUP_S3_BAYEJ
: /* suspend, bay eject */
3812 case TP_HKEY_EV_WKUP_S4_BAYEJ
: /* hibernation, bay eject */
3813 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
3814 *ignore_acpi_ev
= true;
3817 case TP_HKEY_EV_WKUP_S3_BATLOW
: /* Battery on critical low level/S3 */
3818 case TP_HKEY_EV_WKUP_S4_BATLOW
: /* Battery on critical low level/S4 */
3819 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3820 /* how to auto-heal: */
3821 /* 2313: woke up from S3, go to S4/S5 */
3822 /* 2413: woke up from S4, go to S5 */
3829 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
3830 pr_info("woke up due to a hot-unplug request...\n");
3831 hotkey_wakeup_reason_notify_change();
3836 static bool hotkey_notify_dockevent(const u32 hkey
,
3838 bool *ignore_acpi_ev
)
3840 /* 0x4000-0x4FFF: dock-related events */
3841 *send_acpi_ev
= true;
3842 *ignore_acpi_ev
= false;
3845 case TP_HKEY_EV_UNDOCK_ACK
:
3846 /* ACPI undock operation completed after wakeup */
3847 hotkey_autosleep_ack
= 1;
3848 pr_info("undocked\n");
3849 hotkey_wakeup_hotunplug_complete_notify_change();
3852 case TP_HKEY_EV_HOTPLUG_DOCK
: /* docked to port replicator */
3853 pr_info("docked into hotplug port replicator\n");
3855 case TP_HKEY_EV_HOTPLUG_UNDOCK
: /* undocked from port replicator */
3856 pr_info("undocked from hotplug port replicator\n");
3864 static bool hotkey_notify_usrevent(const u32 hkey
,
3866 bool *ignore_acpi_ev
)
3868 /* 0x5000-0x5FFF: human interface helpers */
3869 *send_acpi_ev
= true;
3870 *ignore_acpi_ev
= false;
3873 case TP_HKEY_EV_PEN_INSERTED
: /* X61t: tablet pen inserted into bay */
3874 case TP_HKEY_EV_PEN_REMOVED
: /* X61t: tablet pen removed from bay */
3877 case TP_HKEY_EV_TABLET_TABLET
: /* X41t-X61t: tablet mode */
3878 case TP_HKEY_EV_TABLET_NOTEBOOK
: /* X41t-X61t: normal mode */
3879 tpacpi_input_send_tabletsw();
3880 hotkey_tablet_mode_notify_change();
3881 *send_acpi_ev
= false;
3884 case TP_HKEY_EV_LID_CLOSE
: /* Lid closed */
3885 case TP_HKEY_EV_LID_OPEN
: /* Lid opened */
3886 case TP_HKEY_EV_BRGHT_CHANGED
: /* brightness changed */
3887 /* do not propagate these events */
3888 *ignore_acpi_ev
= true;
3896 static void thermal_dump_all_sensors(void);
3898 static bool hotkey_notify_6xxx(const u32 hkey
,
3900 bool *ignore_acpi_ev
)
3904 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3905 *send_acpi_ev
= true;
3906 *ignore_acpi_ev
= false;
3909 case TP_HKEY_EV_THM_TABLE_CHANGED
:
3910 pr_info("EC reports that Thermal Table has changed\n");
3911 /* recommended action: do nothing, we don't have
3912 * Lenovo ATM information */
3914 case TP_HKEY_EV_ALARM_BAT_HOT
:
3915 pr_crit("THERMAL ALARM: battery is too hot!\n");
3916 /* recommended action: warn user through gui */
3918 case TP_HKEY_EV_ALARM_BAT_XHOT
:
3919 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3920 /* recommended action: immediate sleep/hibernate */
3922 case TP_HKEY_EV_ALARM_SENSOR_HOT
:
3923 pr_crit("THERMAL ALARM: "
3924 "a sensor reports something is too hot!\n");
3925 /* recommended action: warn user through gui, that */
3926 /* some internal component is too hot */
3928 case TP_HKEY_EV_ALARM_SENSOR_XHOT
:
3929 pr_alert("THERMAL EMERGENCY: "
3930 "a sensor reports something is extremely hot!\n");
3931 /* recommended action: immediate sleep/hibernate */
3933 case TP_HKEY_EV_AC_CHANGED
:
3934 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3935 * AC status changed; can be triggered by plugging or
3936 * unplugging AC adapter, docking or undocking. */
3940 case TP_HKEY_EV_KEY_NUMLOCK
:
3941 case TP_HKEY_EV_KEY_FN
:
3942 case TP_HKEY_EV_KEY_FN_ESC
:
3943 /* key press events, we just ignore them as long as the EC
3944 * is still reporting them in the normal keyboard stream */
3945 *send_acpi_ev
= false;
3946 *ignore_acpi_ev
= true;
3949 case TP_HKEY_EV_TABLET_CHANGED
:
3950 tpacpi_input_send_tabletsw();
3951 hotkey_tablet_mode_notify_change();
3952 *send_acpi_ev
= false;
3956 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3960 thermal_dump_all_sensors();
3965 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
3969 bool ignore_acpi_ev
;
3972 if (event
!= 0x80) {
3973 pr_err("unknown HKEY notification event %d\n", event
);
3974 /* forward it to userspace, maybe it knows how to handle it */
3975 acpi_bus_generate_netlink_event(
3976 ibm
->acpi
->device
->pnp
.device_class
,
3977 dev_name(&ibm
->acpi
->device
->dev
),
3983 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
3984 pr_err("failed to retrieve HKEY event\n");
3993 send_acpi_ev
= true;
3994 ignore_acpi_ev
= false;
3996 switch (hkey
>> 12) {
3998 /* 0x1000-0x1FFF: key presses */
3999 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
4003 /* 0x2000-0x2FFF: Wakeup reason */
4004 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
4008 /* 0x3000-0x3FFF: bay-related wakeups */
4010 case TP_HKEY_EV_BAYEJ_ACK
:
4011 hotkey_autosleep_ack
= 1;
4012 pr_info("bay ejected\n");
4013 hotkey_wakeup_hotunplug_complete_notify_change();
4016 case TP_HKEY_EV_OPTDRV_EJ
:
4017 /* FIXME: kick libata if SATA link offline */
4025 /* 0x4000-0x4FFF: dock-related events */
4026 known_ev
= hotkey_notify_dockevent(hkey
, &send_acpi_ev
,
4030 /* 0x5000-0x5FFF: human interface helpers */
4031 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
4035 /* 0x6000-0x6FFF: thermal alarms/notices and
4036 * keyboard events */
4037 known_ev
= hotkey_notify_6xxx(hkey
, &send_acpi_ev
,
4041 /* 0x7000-0x7FFF: misc */
4042 if (tp_features
.hotkey_wlsw
&&
4043 hkey
== TP_HKEY_EV_RFKILL_CHANGED
) {
4044 tpacpi_send_radiosw_update();
4049 /* fallthrough to default */
4054 pr_notice("unhandled HKEY event 0x%04x\n", hkey
);
4055 pr_notice("please report the conditions when this "
4056 "event happened to %s\n", TPACPI_MAIL
);
4059 /* netlink events */
4060 if (!ignore_acpi_ev
&& send_acpi_ev
) {
4061 acpi_bus_generate_netlink_event(
4062 ibm
->acpi
->device
->pnp
.device_class
,
4063 dev_name(&ibm
->acpi
->device
->dev
),
4069 static void hotkey_suspend(void)
4071 /* Do these on suspend, we get the events on early resume! */
4072 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
4073 hotkey_autosleep_ack
= 0;
4075 /* save previous mode of adaptive keyboard of X1 Carbon */
4076 if (tp_features
.has_adaptive_kbd
) {
4077 if (!acpi_evalf(hkey_handle
, &adaptive_keyboard_prev_mode
,
4079 pr_err("Cannot read adaptive keyboard mode.\n");
4084 static void hotkey_resume(void)
4086 tpacpi_disable_brightness_delay();
4088 if (hotkey_status_set(true) < 0 ||
4089 hotkey_mask_set(hotkey_acpi_mask
) < 0)
4090 pr_err("error while attempting to reset the event "
4091 "firmware interface\n");
4093 tpacpi_send_radiosw_update();
4094 hotkey_tablet_mode_notify_change();
4095 hotkey_wakeup_reason_notify_change();
4096 hotkey_wakeup_hotunplug_complete_notify_change();
4097 hotkey_poll_setup_safe(false);
4099 /* restore previous mode of adapive keyboard of X1 Carbon */
4100 if (tp_features
.has_adaptive_kbd
) {
4101 if (!acpi_evalf(hkey_handle
, NULL
, "STRW", "vd",
4102 adaptive_keyboard_prev_mode
)) {
4103 pr_err("Cannot set adaptive keyboard mode.\n");
4108 /* procfs -------------------------------------------------------------- */
4109 static int hotkey_read(struct seq_file
*m
)
4113 if (!tp_features
.hotkey
) {
4114 seq_printf(m
, "status:\t\tnot supported\n");
4118 if (mutex_lock_killable(&hotkey_mutex
))
4119 return -ERESTARTSYS
;
4120 res
= hotkey_status_get(&status
);
4122 res
= hotkey_mask_get();
4123 mutex_unlock(&hotkey_mutex
);
4127 seq_printf(m
, "status:\t\t%s\n", enabled(status
, 0));
4128 if (hotkey_all_mask
) {
4129 seq_printf(m
, "mask:\t\t0x%08x\n", hotkey_user_mask
);
4130 seq_printf(m
, "commands:\tenable, disable, reset, <mask>\n");
4132 seq_printf(m
, "mask:\t\tnot supported\n");
4133 seq_printf(m
, "commands:\tenable, disable, reset\n");
4139 static void hotkey_enabledisable_warn(bool enable
)
4141 tpacpi_log_usertask("procfs hotkey enable/disable");
4142 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
4143 pr_fmt("hotkey enable/disable functionality has been "
4144 "removed from the driver. "
4145 "Hotkeys are always enabled.\n")))
4146 pr_err("Please remove the hotkey=enable module "
4147 "parameter, it is deprecated. "
4148 "Hotkeys are always enabled.\n");
4151 static int hotkey_write(char *buf
)
4157 if (!tp_features
.hotkey
)
4160 if (mutex_lock_killable(&hotkey_mutex
))
4161 return -ERESTARTSYS
;
4163 mask
= hotkey_user_mask
;
4166 while ((cmd
= next_cmd(&buf
))) {
4167 if (strlencmp(cmd
, "enable") == 0) {
4168 hotkey_enabledisable_warn(1);
4169 } else if (strlencmp(cmd
, "disable") == 0) {
4170 hotkey_enabledisable_warn(0);
4172 } else if (strlencmp(cmd
, "reset") == 0) {
4173 mask
= (hotkey_all_mask
| hotkey_source_mask
)
4174 & ~hotkey_reserved_mask
;
4175 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
4177 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
4186 tpacpi_disclose_usertask("procfs hotkey",
4187 "set mask to 0x%08x\n", mask
);
4188 res
= hotkey_user_mask_set(mask
);
4192 mutex_unlock(&hotkey_mutex
);
4196 static const struct acpi_device_id ibm_htk_device_ids
[] = {
4197 {TPACPI_ACPI_IBM_HKEY_HID
, 0},
4198 {TPACPI_ACPI_LENOVO_HKEY_HID
, 0},
4199 {TPACPI_ACPI_LENOVO_HKEY_V2_HID
, 0},
4203 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
4204 .hid
= ibm_htk_device_ids
,
4205 .notify
= hotkey_notify
,
4206 .handle
= &hkey_handle
,
4207 .type
= ACPI_DEVICE_NOTIFY
,
4210 static struct ibm_struct hotkey_driver_data
= {
4212 .read
= hotkey_read
,
4213 .write
= hotkey_write
,
4214 .exit
= hotkey_exit
,
4215 .resume
= hotkey_resume
,
4216 .suspend
= hotkey_suspend
,
4217 .acpi
= &ibm_hotkey_acpidriver
,
4220 /*************************************************************************
4221 * Bluetooth subdriver
4225 /* ACPI GBDC/SBDC bits */
4226 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
4227 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
4228 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
4229 0 = disable, 1 = enable */
4233 /* ACPI \BLTH commands */
4234 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
4235 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
4236 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
4237 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
4238 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
4241 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4243 static int bluetooth_get_status(void)
4247 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4248 if (dbg_bluetoothemul
)
4249 return (tpacpi_bluetooth_emulstate
) ?
4250 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4253 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
4256 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
4257 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4260 static int bluetooth_set_status(enum tpacpi_rfkill_state state
)
4264 vdbg_printk(TPACPI_DBG_RFKILL
,
4265 "will attempt to %s bluetooth\n",
4266 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4268 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4269 if (dbg_bluetoothemul
) {
4270 tpacpi_bluetooth_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4275 if (state
== TPACPI_RFK_RADIO_ON
)
4276 status
= TP_ACPI_BLUETOOTH_RADIOSSW
4277 | TP_ACPI_BLUETOOTH_RESUMECTRL
;
4281 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
4287 /* sysfs bluetooth enable ---------------------------------------------- */
4288 static ssize_t
bluetooth_enable_show(struct device
*dev
,
4289 struct device_attribute
*attr
,
4292 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID
,
4296 static ssize_t
bluetooth_enable_store(struct device
*dev
,
4297 struct device_attribute
*attr
,
4298 const char *buf
, size_t count
)
4300 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID
,
4304 static DEVICE_ATTR_RW(bluetooth_enable
);
4306 /* --------------------------------------------------------------------- */
4308 static struct attribute
*bluetooth_attributes
[] = {
4309 &dev_attr_bluetooth_enable
.attr
,
4313 static const struct attribute_group bluetooth_attr_group
= {
4314 .attrs
= bluetooth_attributes
,
4317 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops
= {
4318 .get_status
= bluetooth_get_status
,
4319 .set_status
= bluetooth_set_status
,
4322 static void bluetooth_shutdown(void)
4324 /* Order firmware to save current state to NVRAM */
4325 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
4326 TP_ACPI_BLTH_SAVE_STATE
))
4327 pr_notice("failed to save bluetooth state to NVRAM\n");
4329 vdbg_printk(TPACPI_DBG_RFKILL
,
4330 "bluetooth state saved to NVRAM\n");
4333 static void bluetooth_exit(void)
4335 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4336 &bluetooth_attr_group
);
4338 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
4340 bluetooth_shutdown();
4343 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
4348 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4349 "initializing bluetooth subdriver\n");
4351 TPACPI_ACPIHANDLE_INIT(hkey
);
4353 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4354 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4355 tp_features
.bluetooth
= hkey_handle
&&
4356 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
4358 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4359 "bluetooth is %s, status 0x%02x\n",
4360 str_supported(tp_features
.bluetooth
),
4363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4364 if (dbg_bluetoothemul
) {
4365 tp_features
.bluetooth
= 1;
4366 pr_info("bluetooth switch emulation enabled\n");
4369 if (tp_features
.bluetooth
&&
4370 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
4371 /* no bluetooth hardware present in system */
4372 tp_features
.bluetooth
= 0;
4373 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4374 "bluetooth hardware not installed\n");
4377 if (!tp_features
.bluetooth
)
4380 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
4381 &bluetooth_tprfk_ops
,
4382 RFKILL_TYPE_BLUETOOTH
,
4383 TPACPI_RFK_BLUETOOTH_SW_NAME
,
4388 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4389 &bluetooth_attr_group
);
4391 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
4398 /* procfs -------------------------------------------------------------- */
4399 static int bluetooth_read(struct seq_file
*m
)
4401 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID
, m
);
4404 static int bluetooth_write(char *buf
)
4406 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID
, buf
);
4409 static struct ibm_struct bluetooth_driver_data
= {
4410 .name
= "bluetooth",
4411 .read
= bluetooth_read
,
4412 .write
= bluetooth_write
,
4413 .exit
= bluetooth_exit
,
4414 .shutdown
= bluetooth_shutdown
,
4417 /*************************************************************************
4422 /* ACPI GWAN/SWAN bits */
4423 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
4424 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
4425 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
4426 0 = disable, 1 = enable */
4429 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4431 static int wan_get_status(void)
4435 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4437 return (tpacpi_wwan_emulstate
) ?
4438 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4441 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
4444 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
4445 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4448 static int wan_set_status(enum tpacpi_rfkill_state state
)
4452 vdbg_printk(TPACPI_DBG_RFKILL
,
4453 "will attempt to %s wwan\n",
4454 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4456 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4458 tpacpi_wwan_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4463 if (state
== TPACPI_RFK_RADIO_ON
)
4464 status
= TP_ACPI_WANCARD_RADIOSSW
4465 | TP_ACPI_WANCARD_RESUMECTRL
;
4469 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
4475 /* sysfs wan enable ---------------------------------------------------- */
4476 static ssize_t
wan_enable_show(struct device
*dev
,
4477 struct device_attribute
*attr
,
4480 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID
,
4484 static ssize_t
wan_enable_store(struct device
*dev
,
4485 struct device_attribute
*attr
,
4486 const char *buf
, size_t count
)
4488 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID
,
4492 static DEVICE_ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
4493 wan_enable_show
, wan_enable_store
);
4495 /* --------------------------------------------------------------------- */
4497 static struct attribute
*wan_attributes
[] = {
4498 &dev_attr_wwan_enable
.attr
,
4502 static const struct attribute_group wan_attr_group
= {
4503 .attrs
= wan_attributes
,
4506 static const struct tpacpi_rfk_ops wan_tprfk_ops
= {
4507 .get_status
= wan_get_status
,
4508 .set_status
= wan_set_status
,
4511 static void wan_shutdown(void)
4513 /* Order firmware to save current state to NVRAM */
4514 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
4515 TP_ACPI_WGSV_SAVE_STATE
))
4516 pr_notice("failed to save WWAN state to NVRAM\n");
4518 vdbg_printk(TPACPI_DBG_RFKILL
,
4519 "WWAN state saved to NVRAM\n");
4522 static void wan_exit(void)
4524 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4527 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4532 static int __init
wan_init(struct ibm_init_struct
*iibm
)
4537 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4538 "initializing wan subdriver\n");
4540 TPACPI_ACPIHANDLE_INIT(hkey
);
4542 tp_features
.wan
= hkey_handle
&&
4543 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
4545 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4546 "wan is %s, status 0x%02x\n",
4547 str_supported(tp_features
.wan
),
4550 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4552 tp_features
.wan
= 1;
4553 pr_info("wwan switch emulation enabled\n");
4556 if (tp_features
.wan
&&
4557 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
4558 /* no wan hardware present in system */
4559 tp_features
.wan
= 0;
4560 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4561 "wan hardware not installed\n");
4564 if (!tp_features
.wan
)
4567 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
4570 TPACPI_RFK_WWAN_SW_NAME
,
4575 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4579 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4586 /* procfs -------------------------------------------------------------- */
4587 static int wan_read(struct seq_file
*m
)
4589 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID
, m
);
4592 static int wan_write(char *buf
)
4594 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID
, buf
);
4597 static struct ibm_struct wan_driver_data
= {
4602 .shutdown
= wan_shutdown
,
4605 /*************************************************************************
4610 /* ACPI GUWB/SUWB bits */
4611 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
4612 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
4615 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4617 static int uwb_get_status(void)
4621 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4623 return (tpacpi_uwb_emulstate
) ?
4624 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4627 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
4630 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
4631 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4634 static int uwb_set_status(enum tpacpi_rfkill_state state
)
4638 vdbg_printk(TPACPI_DBG_RFKILL
,
4639 "will attempt to %s UWB\n",
4640 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4642 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4644 tpacpi_uwb_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4649 if (state
== TPACPI_RFK_RADIO_ON
)
4650 status
= TP_ACPI_UWB_RADIOSSW
;
4654 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
4660 /* --------------------------------------------------------------------- */
4662 static const struct tpacpi_rfk_ops uwb_tprfk_ops
= {
4663 .get_status
= uwb_get_status
,
4664 .set_status
= uwb_set_status
,
4667 static void uwb_exit(void)
4669 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID
);
4672 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
4677 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4678 "initializing uwb subdriver\n");
4680 TPACPI_ACPIHANDLE_INIT(hkey
);
4682 tp_features
.uwb
= hkey_handle
&&
4683 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
4685 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4686 "uwb is %s, status 0x%02x\n",
4687 str_supported(tp_features
.uwb
),
4690 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4692 tp_features
.uwb
= 1;
4693 pr_info("uwb switch emulation enabled\n");
4696 if (tp_features
.uwb
&&
4697 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
4698 /* no uwb hardware present in system */
4699 tp_features
.uwb
= 0;
4700 dbg_printk(TPACPI_DBG_INIT
,
4701 "uwb hardware not installed\n");
4704 if (!tp_features
.uwb
)
4707 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
4710 TPACPI_RFK_UWB_SW_NAME
,
4715 static struct ibm_struct uwb_driver_data
= {
4718 .flags
.experimental
= 1,
4721 /*************************************************************************
4725 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4727 enum video_access_mode
{
4728 TPACPI_VIDEO_NONE
= 0,
4729 TPACPI_VIDEO_570
, /* 570 */
4730 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
4731 TPACPI_VIDEO_NEW
, /* all others */
4734 enum { /* video status flags, based on VIDEO_570 */
4735 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
4736 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
4737 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
4740 enum { /* TPACPI_VIDEO_570 constants */
4741 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
4742 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
4743 * video_status_flags */
4744 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
4745 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
4748 static enum video_access_mode video_supported
;
4749 static int video_orig_autosw
;
4751 static int video_autosw_get(void);
4752 static int video_autosw_set(int enable
);
4754 TPACPI_HANDLE(vid
, root
,
4755 "\\_SB.PCI.AGP.VGA", /* 570 */
4756 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4757 "\\_SB.PCI0.VID0", /* 770e */
4758 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4759 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4760 "\\_SB.PCI0.AGP.VID", /* all others */
4763 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4765 static int __init
video_init(struct ibm_init_struct
*iibm
)
4769 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
4771 TPACPI_ACPIHANDLE_INIT(vid
);
4772 if (tpacpi_is_ibm())
4773 TPACPI_ACPIHANDLE_INIT(vid2
);
4775 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
4776 /* G41, assume IVGA doesn't change */
4777 vid_handle
= vid2_handle
;
4780 /* video switching not supported on R30, R31 */
4781 video_supported
= TPACPI_VIDEO_NONE
;
4782 else if (tpacpi_is_ibm() &&
4783 acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
4785 video_supported
= TPACPI_VIDEO_570
;
4786 else if (tpacpi_is_ibm() &&
4787 acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
4788 /* 600e/x, 770e, 770x */
4789 video_supported
= TPACPI_VIDEO_770
;
4792 video_supported
= TPACPI_VIDEO_NEW
;
4794 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
4795 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
4798 return (video_supported
!= TPACPI_VIDEO_NONE
) ? 0 : 1;
4801 static void video_exit(void)
4803 dbg_printk(TPACPI_DBG_EXIT
,
4804 "restoring original video autoswitch mode\n");
4805 if (video_autosw_set(video_orig_autosw
))
4806 pr_err("error while trying to restore original "
4807 "video autoswitch mode\n");
4810 static int video_outputsw_get(void)
4815 switch (video_supported
) {
4816 case TPACPI_VIDEO_570
:
4817 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
4818 TP_ACPI_VIDEO_570_PHSCMD
))
4820 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
4822 case TPACPI_VIDEO_770
:
4823 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4826 status
|= TP_ACPI_VIDEO_S_LCD
;
4827 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4830 status
|= TP_ACPI_VIDEO_S_CRT
;
4832 case TPACPI_VIDEO_NEW
:
4833 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4834 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4837 status
|= TP_ACPI_VIDEO_S_CRT
;
4839 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4840 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4843 status
|= TP_ACPI_VIDEO_S_LCD
;
4844 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4847 status
|= TP_ACPI_VIDEO_S_DVI
;
4856 static int video_outputsw_set(int status
)
4861 switch (video_supported
) {
4862 case TPACPI_VIDEO_570
:
4863 res
= acpi_evalf(NULL
, NULL
,
4864 "\\_SB.PHS2", "vdd",
4865 TP_ACPI_VIDEO_570_PHS2CMD
,
4866 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4868 case TPACPI_VIDEO_770
:
4869 autosw
= video_autosw_get();
4873 res
= video_autosw_set(1);
4876 res
= acpi_evalf(vid_handle
, NULL
,
4877 "ASWT", "vdd", status
* 0x100, 0);
4878 if (!autosw
&& video_autosw_set(autosw
)) {
4879 pr_err("video auto-switch left enabled due to error\n");
4883 case TPACPI_VIDEO_NEW
:
4884 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
4885 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
4891 return (res
) ? 0 : -EIO
;
4894 static int video_autosw_get(void)
4898 switch (video_supported
) {
4899 case TPACPI_VIDEO_570
:
4900 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
4903 case TPACPI_VIDEO_770
:
4904 case TPACPI_VIDEO_NEW
:
4905 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4915 static int video_autosw_set(int enable
)
4917 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
) ? 1 : 0))
4922 static int video_outputsw_cycle(void)
4924 int autosw
= video_autosw_get();
4930 switch (video_supported
) {
4931 case TPACPI_VIDEO_570
:
4932 res
= video_autosw_set(1);
4935 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4937 case TPACPI_VIDEO_770
:
4938 case TPACPI_VIDEO_NEW
:
4939 res
= video_autosw_set(1);
4942 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4947 if (!autosw
&& video_autosw_set(autosw
)) {
4948 pr_err("video auto-switch left enabled due to error\n");
4952 return (res
) ? 0 : -EIO
;
4955 static int video_expand_toggle(void)
4957 switch (video_supported
) {
4958 case TPACPI_VIDEO_570
:
4959 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v") ?
4961 case TPACPI_VIDEO_770
:
4962 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v") ?
4964 case TPACPI_VIDEO_NEW
:
4965 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v") ?
4973 static int video_read(struct seq_file
*m
)
4977 if (video_supported
== TPACPI_VIDEO_NONE
) {
4978 seq_printf(m
, "status:\t\tnot supported\n");
4982 /* Even reads can crash X.org, so... */
4983 if (!capable(CAP_SYS_ADMIN
))
4986 status
= video_outputsw_get();
4990 autosw
= video_autosw_get();
4994 seq_printf(m
, "status:\t\tsupported\n");
4995 seq_printf(m
, "lcd:\t\t%s\n", enabled(status
, 0));
4996 seq_printf(m
, "crt:\t\t%s\n", enabled(status
, 1));
4997 if (video_supported
== TPACPI_VIDEO_NEW
)
4998 seq_printf(m
, "dvi:\t\t%s\n", enabled(status
, 3));
4999 seq_printf(m
, "auto:\t\t%s\n", enabled(autosw
, 0));
5000 seq_printf(m
, "commands:\tlcd_enable, lcd_disable\n");
5001 seq_printf(m
, "commands:\tcrt_enable, crt_disable\n");
5002 if (video_supported
== TPACPI_VIDEO_NEW
)
5003 seq_printf(m
, "commands:\tdvi_enable, dvi_disable\n");
5004 seq_printf(m
, "commands:\tauto_enable, auto_disable\n");
5005 seq_printf(m
, "commands:\tvideo_switch, expand_toggle\n");
5010 static int video_write(char *buf
)
5013 int enable
, disable
, status
;
5016 if (video_supported
== TPACPI_VIDEO_NONE
)
5019 /* Even reads can crash X.org, let alone writes... */
5020 if (!capable(CAP_SYS_ADMIN
))
5026 while ((cmd
= next_cmd(&buf
))) {
5027 if (strlencmp(cmd
, "lcd_enable") == 0) {
5028 enable
|= TP_ACPI_VIDEO_S_LCD
;
5029 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
5030 disable
|= TP_ACPI_VIDEO_S_LCD
;
5031 } else if (strlencmp(cmd
, "crt_enable") == 0) {
5032 enable
|= TP_ACPI_VIDEO_S_CRT
;
5033 } else if (strlencmp(cmd
, "crt_disable") == 0) {
5034 disable
|= TP_ACPI_VIDEO_S_CRT
;
5035 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
5036 strlencmp(cmd
, "dvi_enable") == 0) {
5037 enable
|= TP_ACPI_VIDEO_S_DVI
;
5038 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
5039 strlencmp(cmd
, "dvi_disable") == 0) {
5040 disable
|= TP_ACPI_VIDEO_S_DVI
;
5041 } else if (strlencmp(cmd
, "auto_enable") == 0) {
5042 res
= video_autosw_set(1);
5045 } else if (strlencmp(cmd
, "auto_disable") == 0) {
5046 res
= video_autosw_set(0);
5049 } else if (strlencmp(cmd
, "video_switch") == 0) {
5050 res
= video_outputsw_cycle();
5053 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
5054 res
= video_expand_toggle();
5061 if (enable
|| disable
) {
5062 status
= video_outputsw_get();
5065 res
= video_outputsw_set((status
& ~disable
) | enable
);
5073 static struct ibm_struct video_driver_data
= {
5076 .write
= video_write
,
5080 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5082 /*************************************************************************
5083 * Keyboard backlight subdriver
5086 static enum led_brightness kbdlight_brightness
;
5087 static DEFINE_MUTEX(kbdlight_mutex
);
5089 static int kbdlight_set_level(int level
)
5096 mutex_lock(&kbdlight_mutex
);
5098 if (!acpi_evalf(hkey_handle
, NULL
, "MLCS", "dd", level
))
5101 kbdlight_brightness
= level
;
5103 mutex_unlock(&kbdlight_mutex
);
5108 static int kbdlight_get_level(void)
5115 if (!acpi_evalf(hkey_handle
, &status
, "MLCG", "dd", 0))
5121 return status
& 0x3;
5124 static bool kbdlight_is_supported(void)
5131 if (!acpi_has_method(hkey_handle
, "MLCG")) {
5132 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG is unavailable\n");
5136 if (!acpi_evalf(hkey_handle
, &status
, "MLCG", "qdd", 0)) {
5137 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG failed\n");
5142 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG err: %d\n", status
);
5146 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG returned 0x%x\n", status
);
5148 * Guessed test for keyboard backlight:
5150 * Machines with backlight keyboard return:
5151 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5152 * b110100010010000000XX - ThinkPad x230
5153 * b010100000010000000XX - ThinkPad x240
5154 * b010100000010000000XX - ThinkPad W541
5155 * (XX is current backlight level)
5157 * Machines without backlight keyboard return:
5158 * b10100001000000000000 - ThinkPad x230
5159 * b10110001000000000000 - ThinkPad E430
5160 * b00000000000000000000 - ThinkPad E450
5162 * Candidate BITs for detection test (XOR):
5163 * b01000000001000000000
5166 return status
& BIT(9);
5169 static int kbdlight_sysfs_set(struct led_classdev
*led_cdev
,
5170 enum led_brightness brightness
)
5172 return kbdlight_set_level(brightness
);
5175 static enum led_brightness
kbdlight_sysfs_get(struct led_classdev
*led_cdev
)
5179 level
= kbdlight_get_level();
5186 static struct tpacpi_led_classdev tpacpi_led_kbdlight
= {
5188 .name
= "tpacpi::kbd_backlight",
5189 .max_brightness
= 2,
5190 .flags
= LED_BRIGHT_HW_CHANGED
,
5191 .brightness_set_blocking
= &kbdlight_sysfs_set
,
5192 .brightness_get
= &kbdlight_sysfs_get
,
5196 static int __init
kbdlight_init(struct ibm_init_struct
*iibm
)
5200 vdbg_printk(TPACPI_DBG_INIT
, "initializing kbdlight subdriver\n");
5202 TPACPI_ACPIHANDLE_INIT(hkey
);
5204 if (!kbdlight_is_supported()) {
5205 tp_features
.kbdlight
= 0;
5206 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight is unsupported\n");
5210 kbdlight_brightness
= kbdlight_sysfs_get(NULL
);
5211 tp_features
.kbdlight
= 1;
5213 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5214 &tpacpi_led_kbdlight
.led_classdev
);
5216 tp_features
.kbdlight
= 0;
5220 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
|
5221 TP_ACPI_HKEY_KBD_LIGHT_MASK
);
5225 static void kbdlight_exit(void)
5227 if (tp_features
.kbdlight
)
5228 led_classdev_unregister(&tpacpi_led_kbdlight
.led_classdev
);
5231 static int kbdlight_set_level_and_update(int level
)
5234 struct led_classdev
*led_cdev
;
5236 ret
= kbdlight_set_level(level
);
5237 led_cdev
= &tpacpi_led_kbdlight
.led_classdev
;
5239 if (ret
== 0 && !(led_cdev
->flags
& LED_SUSPENDED
))
5240 led_cdev
->brightness
= level
;
5245 static int kbdlight_read(struct seq_file
*m
)
5249 if (!tp_features
.kbdlight
) {
5250 seq_printf(m
, "status:\t\tnot supported\n");
5252 level
= kbdlight_get_level();
5254 seq_printf(m
, "status:\t\terror %d\n", level
);
5256 seq_printf(m
, "status:\t\t%d\n", level
);
5257 seq_printf(m
, "commands:\t0, 1, 2\n");
5263 static int kbdlight_write(char *buf
)
5268 if (!tp_features
.kbdlight
)
5271 while ((cmd
= next_cmd(&buf
))) {
5272 if (strlencmp(cmd
, "0") == 0)
5274 else if (strlencmp(cmd
, "1") == 0)
5276 else if (strlencmp(cmd
, "2") == 0)
5285 return kbdlight_set_level_and_update(level
);
5288 static void kbdlight_suspend(void)
5290 struct led_classdev
*led_cdev
;
5292 if (!tp_features
.kbdlight
)
5295 led_cdev
= &tpacpi_led_kbdlight
.led_classdev
;
5296 led_update_brightness(led_cdev
);
5297 led_classdev_suspend(led_cdev
);
5300 static void kbdlight_resume(void)
5302 if (!tp_features
.kbdlight
)
5305 led_classdev_resume(&tpacpi_led_kbdlight
.led_classdev
);
5308 static struct ibm_struct kbdlight_driver_data
= {
5310 .read
= kbdlight_read
,
5311 .write
= kbdlight_write
,
5312 .suspend
= kbdlight_suspend
,
5313 .resume
= kbdlight_resume
,
5314 .exit
= kbdlight_exit
,
5317 /*************************************************************************
5318 * Light (thinklight) subdriver
5321 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5322 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
5324 static int light_get_status(void)
5328 if (tp_features
.light_status
) {
5329 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
5337 static int light_set_status(int status
)
5341 if (tp_features
.light
) {
5343 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
5345 TP_CMOS_THINKLIGHT_ON
:
5346 TP_CMOS_THINKLIGHT_OFF
);
5348 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
5351 return (rc
) ? 0 : -EIO
;
5357 static int light_sysfs_set(struct led_classdev
*led_cdev
,
5358 enum led_brightness brightness
)
5360 return light_set_status((brightness
!= LED_OFF
) ?
5361 TPACPI_LED_ON
: TPACPI_LED_OFF
);
5364 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
5366 return (light_get_status() == 1) ? LED_FULL
: LED_OFF
;
5369 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
5371 .name
= "tpacpi::thinklight",
5372 .brightness_set_blocking
= &light_sysfs_set
,
5373 .brightness_get
= &light_sysfs_get
,
5377 static int __init
light_init(struct ibm_init_struct
*iibm
)
5381 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
5383 if (tpacpi_is_ibm()) {
5384 TPACPI_ACPIHANDLE_INIT(ledb
);
5385 TPACPI_ACPIHANDLE_INIT(lght
);
5387 TPACPI_ACPIHANDLE_INIT(cmos
);
5389 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5390 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
5392 if (tp_features
.light
)
5393 /* light status not supported on
5394 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5395 tp_features
.light_status
=
5396 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
5398 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
5399 str_supported(tp_features
.light
),
5400 str_supported(tp_features
.light_status
));
5402 if (!tp_features
.light
)
5405 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5406 &tpacpi_led_thinklight
.led_classdev
);
5409 tp_features
.light
= 0;
5410 tp_features
.light_status
= 0;
5418 static void light_exit(void)
5420 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
5423 static int light_read(struct seq_file
*m
)
5427 if (!tp_features
.light
) {
5428 seq_printf(m
, "status:\t\tnot supported\n");
5429 } else if (!tp_features
.light_status
) {
5430 seq_printf(m
, "status:\t\tunknown\n");
5431 seq_printf(m
, "commands:\ton, off\n");
5433 status
= light_get_status();
5436 seq_printf(m
, "status:\t\t%s\n", onoff(status
, 0));
5437 seq_printf(m
, "commands:\ton, off\n");
5443 static int light_write(char *buf
)
5448 if (!tp_features
.light
)
5451 while ((cmd
= next_cmd(&buf
))) {
5452 if (strlencmp(cmd
, "on") == 0) {
5454 } else if (strlencmp(cmd
, "off") == 0) {
5460 return light_set_status(newstatus
);
5463 static struct ibm_struct light_driver_data
= {
5466 .write
= light_write
,
5470 /*************************************************************************
5474 /* sysfs cmos_command -------------------------------------------------- */
5475 static ssize_t
cmos_command_store(struct device
*dev
,
5476 struct device_attribute
*attr
,
5477 const char *buf
, size_t count
)
5479 unsigned long cmos_cmd
;
5482 if (parse_strtoul(buf
, 21, &cmos_cmd
))
5485 res
= issue_thinkpad_cmos_command(cmos_cmd
);
5486 return (res
) ? res
: count
;
5489 static DEVICE_ATTR_WO(cmos_command
);
5491 /* --------------------------------------------------------------------- */
5493 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
5497 vdbg_printk(TPACPI_DBG_INIT
,
5498 "initializing cmos commands subdriver\n");
5500 TPACPI_ACPIHANDLE_INIT(cmos
);
5502 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
5503 str_supported(cmos_handle
!= NULL
));
5505 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
5509 return (cmos_handle
) ? 0 : 1;
5512 static void cmos_exit(void)
5514 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
5517 static int cmos_read(struct seq_file
*m
)
5519 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5520 R30, R31, T20-22, X20-21 */
5522 seq_printf(m
, "status:\t\tnot supported\n");
5524 seq_printf(m
, "status:\t\tsupported\n");
5525 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-21)\n");
5531 static int cmos_write(char *buf
)
5536 while ((cmd
= next_cmd(&buf
))) {
5537 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
5538 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
5543 res
= issue_thinkpad_cmos_command(cmos_cmd
);
5551 static struct ibm_struct cmos_driver_data
= {
5554 .write
= cmos_write
,
5558 /*************************************************************************
5562 enum led_access_mode
{
5563 TPACPI_LED_NONE
= 0,
5564 TPACPI_LED_570
, /* 570 */
5565 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5566 TPACPI_LED_NEW
, /* all others */
5569 enum { /* For TPACPI_LED_OLD */
5570 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
5571 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
5572 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
5575 static enum led_access_mode led_supported
;
5577 static acpi_handle led_handle
;
5579 #define TPACPI_LED_NUMLEDS 16
5580 static struct tpacpi_led_classdev
*tpacpi_leds
;
5581 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
5582 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
5583 /* there's a limit of 19 chars + NULL before 2.6.26 */
5585 "tpacpi:orange:batt",
5586 "tpacpi:green:batt",
5587 "tpacpi::dock_active",
5588 "tpacpi::bay_active",
5589 "tpacpi::dock_batt",
5590 "tpacpi::unknown_led",
5592 "tpacpi::dock_status1",
5593 "tpacpi::dock_status2",
5594 "tpacpi::unknown_led2",
5595 "tpacpi::unknown_led3",
5596 "tpacpi::thinkvantage",
5598 #define TPACPI_SAFE_LEDS 0x1081U
5600 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
5602 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5605 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
5609 static int led_get_status(const unsigned int led
)
5612 enum led_status_t led_s
;
5614 switch (led_supported
) {
5615 case TPACPI_LED_570
:
5616 if (!acpi_evalf(ec_handle
,
5617 &status
, "GLED", "dd", 1 << led
))
5619 led_s
= (status
== 0) ?
5624 tpacpi_led_state_cache
[led
] = led_s
;
5633 static int led_set_status(const unsigned int led
,
5634 const enum led_status_t ledstatus
)
5636 /* off, on, blink. Index is led_status_t */
5637 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
5638 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
5642 switch (led_supported
) {
5643 case TPACPI_LED_570
:
5645 if (unlikely(led
> 7))
5647 if (unlikely(tpacpi_is_led_restricted(led
)))
5649 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5650 (1 << led
), led_sled_arg1
[ledstatus
]))
5653 case TPACPI_LED_OLD
:
5654 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5655 if (unlikely(led
> 7))
5657 if (unlikely(tpacpi_is_led_restricted(led
)))
5659 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
5661 rc
= ec_write(TPACPI_LED_EC_HLBL
,
5662 (ledstatus
== TPACPI_LED_BLINK
) << led
);
5664 rc
= ec_write(TPACPI_LED_EC_HLCL
,
5665 (ledstatus
!= TPACPI_LED_OFF
) << led
);
5667 case TPACPI_LED_NEW
:
5669 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
5671 if (unlikely(tpacpi_is_led_restricted(led
)))
5673 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5674 led
, led_led_arg1
[ledstatus
]))
5682 tpacpi_led_state_cache
[led
] = ledstatus
;
5687 static int led_sysfs_set(struct led_classdev
*led_cdev
,
5688 enum led_brightness brightness
)
5690 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5691 struct tpacpi_led_classdev
, led_classdev
);
5692 enum led_status_t new_state
;
5694 if (brightness
== LED_OFF
)
5695 new_state
= TPACPI_LED_OFF
;
5696 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
5697 new_state
= TPACPI_LED_ON
;
5699 new_state
= TPACPI_LED_BLINK
;
5701 return led_set_status(data
->led
, new_state
);
5704 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
5705 unsigned long *delay_on
, unsigned long *delay_off
)
5707 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5708 struct tpacpi_led_classdev
, led_classdev
);
5710 /* Can we choose the flash rate? */
5711 if (*delay_on
== 0 && *delay_off
== 0) {
5712 /* yes. set them to the hardware blink rate (1 Hz) */
5713 *delay_on
= 500; /* ms */
5714 *delay_off
= 500; /* ms */
5715 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
5718 return led_set_status(data
->led
, TPACPI_LED_BLINK
);
5721 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
5725 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5726 struct tpacpi_led_classdev
, led_classdev
);
5728 rc
= led_get_status(data
->led
);
5730 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
5731 rc
= LED_OFF
; /* no error handling in led class :( */
5738 static void led_exit(void)
5742 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5743 if (tpacpi_leds
[i
].led_classdev
.name
)
5744 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
5750 static int __init
tpacpi_init_led(unsigned int led
)
5754 tpacpi_leds
[led
].led
= led
;
5756 /* LEDs with no name don't get registered */
5757 if (!tpacpi_led_names
[led
])
5760 tpacpi_leds
[led
].led_classdev
.brightness_set_blocking
= &led_sysfs_set
;
5761 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
5762 if (led_supported
== TPACPI_LED_570
)
5763 tpacpi_leds
[led
].led_classdev
.brightness_get
=
5766 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
5768 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5769 &tpacpi_leds
[led
].led_classdev
);
5771 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
5776 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
5777 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5778 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5779 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5781 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5782 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5783 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5784 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5785 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5786 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5787 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5788 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5790 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5791 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5792 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5793 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5794 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5796 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5797 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5798 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5799 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5801 /* (1) - may have excess leds enabled on MSB */
5803 /* Defaults (order matters, keep last, don't reorder!) */
5805 .vendor
= PCI_VENDOR_ID_LENOVO
,
5806 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5809 { /* IBM ThinkPads with no EC version string */
5810 .vendor
= PCI_VENDOR_ID_IBM
,
5811 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
5814 { /* IBM ThinkPads with EC version string */
5815 .vendor
= PCI_VENDOR_ID_IBM
,
5816 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5821 #undef TPACPI_LEDQ_IBM
5822 #undef TPACPI_LEDQ_LNV
5824 static enum led_access_mode __init
led_init_detect_mode(void)
5828 if (tpacpi_is_ibm()) {
5830 status
= acpi_get_handle(ec_handle
, "SLED", &led_handle
);
5831 if (ACPI_SUCCESS(status
))
5832 return TPACPI_LED_570
;
5834 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5835 status
= acpi_get_handle(ec_handle
, "SYSL", &led_handle
);
5836 if (ACPI_SUCCESS(status
))
5837 return TPACPI_LED_OLD
;
5841 status
= acpi_get_handle(ec_handle
, "LED", &led_handle
);
5842 if (ACPI_SUCCESS(status
))
5843 return TPACPI_LED_NEW
;
5845 /* R30, R31, and unknown firmwares */
5847 return TPACPI_LED_NONE
;
5850 static int __init
led_init(struct ibm_init_struct
*iibm
)
5854 unsigned long useful_leds
;
5856 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
5858 led_supported
= led_init_detect_mode();
5860 if (led_supported
!= TPACPI_LED_NONE
) {
5861 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
5862 ARRAY_SIZE(led_useful_qtable
));
5866 led_supported
= TPACPI_LED_NONE
;
5870 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
5871 str_supported(led_supported
), led_supported
);
5873 if (led_supported
== TPACPI_LED_NONE
)
5876 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
5879 pr_err("Out of memory for LED data\n");
5883 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5884 tpacpi_leds
[i
].led
= -1;
5886 if (!tpacpi_is_led_restricted(i
) &&
5887 test_bit(i
, &useful_leds
)) {
5888 rc
= tpacpi_init_led(i
);
5896 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5897 pr_notice("warning: userspace override of important "
5898 "firmware LEDs is enabled\n");
5903 #define str_led_status(s) \
5904 ((s) == TPACPI_LED_OFF ? "off" : \
5905 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5907 static int led_read(struct seq_file
*m
)
5909 if (!led_supported
) {
5910 seq_printf(m
, "status:\t\tnot supported\n");
5913 seq_printf(m
, "status:\t\tsupported\n");
5915 if (led_supported
== TPACPI_LED_570
) {
5918 for (i
= 0; i
< 8; i
++) {
5919 status
= led_get_status(i
);
5922 seq_printf(m
, "%d:\t\t%s\n",
5923 i
, str_led_status(status
));
5927 seq_printf(m
, "commands:\t"
5928 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5933 static int led_write(char *buf
)
5937 enum led_status_t s
;
5942 while ((cmd
= next_cmd(&buf
))) {
5943 if (sscanf(cmd
, "%d", &led
) != 1)
5946 if (led
< 0 || led
> (TPACPI_LED_NUMLEDS
- 1) ||
5947 tpacpi_leds
[led
].led
< 0)
5950 if (strstr(cmd
, "off")) {
5952 } else if (strstr(cmd
, "on")) {
5954 } else if (strstr(cmd
, "blink")) {
5955 s
= TPACPI_LED_BLINK
;
5960 rc
= led_set_status(led
, s
);
5968 static struct ibm_struct led_driver_data
= {
5975 /*************************************************************************
5979 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5981 #define TPACPI_BEEP_Q1 0x0001
5983 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
5984 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
5985 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
5988 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5990 unsigned long quirks
;
5992 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5994 TPACPI_ACPIHANDLE_INIT(beep
);
5996 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5997 str_supported(beep_handle
!= NULL
));
5999 quirks
= tpacpi_check_quirks(beep_quirk_table
,
6000 ARRAY_SIZE(beep_quirk_table
));
6002 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
6004 return (beep_handle
) ? 0 : 1;
6007 static int beep_read(struct seq_file
*m
)
6010 seq_printf(m
, "status:\t\tnot supported\n");
6012 seq_printf(m
, "status:\t\tsupported\n");
6013 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-17)\n");
6019 static int beep_write(char *buf
)
6027 while ((cmd
= next_cmd(&buf
))) {
6028 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
6029 beep_cmd
>= 0 && beep_cmd
<= 17) {
6033 if (tp_features
.beep_needs_two_args
) {
6034 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
6038 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
6047 static struct ibm_struct beep_driver_data
= {
6050 .write
= beep_write
,
6053 /*************************************************************************
6057 enum thermal_access_mode
{
6058 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
6059 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
6060 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
6061 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
6062 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
6065 enum { /* TPACPI_THERMAL_TPEC_* */
6066 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
6067 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
6068 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
6070 TPACPI_THERMAL_SENSOR_NA
= -128000, /* Sensor not available */
6074 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6075 struct ibm_thermal_sensors_struct
{
6076 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
6079 static enum thermal_access_mode thermal_read_mode
;
6081 /* idx is zero-based */
6082 static int thermal_get_sensor(int idx
, s32
*value
)
6088 t
= TP_EC_THERMAL_TMP0
;
6090 switch (thermal_read_mode
) {
6091 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6092 case TPACPI_THERMAL_TPEC_16
:
6093 if (idx
>= 8 && idx
<= 15) {
6094 t
= TP_EC_THERMAL_TMP8
;
6099 case TPACPI_THERMAL_TPEC_8
:
6101 if (!acpi_ec_read(t
+ idx
, &tmp
))
6103 *value
= tmp
* 1000;
6108 case TPACPI_THERMAL_ACPI_UPDT
:
6110 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
6111 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
6113 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
6115 *value
= (t
- 2732) * 100;
6120 case TPACPI_THERMAL_ACPI_TMP07
:
6122 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
6123 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
6125 if (t
> 127 || t
< -127)
6126 t
= TP_EC_THERMAL_TMP_NA
;
6132 case TPACPI_THERMAL_NONE
:
6140 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
6151 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
6154 for (i
= 0 ; i
< n
; i
++) {
6155 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
6163 static void thermal_dump_all_sensors(void)
6166 struct ibm_thermal_sensors_struct t
;
6168 n
= thermal_get_sensors(&t
);
6172 pr_notice("temperatures (Celsius):");
6174 for (i
= 0; i
< n
; i
++) {
6175 if (t
.temp
[i
] != TPACPI_THERMAL_SENSOR_NA
)
6176 pr_cont(" %d", (int)(t
.temp
[i
] / 1000));
6184 /* sysfs temp##_input -------------------------------------------------- */
6186 static ssize_t
thermal_temp_input_show(struct device
*dev
,
6187 struct device_attribute
*attr
,
6190 struct sensor_device_attribute
*sensor_attr
=
6191 to_sensor_dev_attr(attr
);
6192 int idx
= sensor_attr
->index
;
6196 res
= thermal_get_sensor(idx
, &value
);
6199 if (value
== TPACPI_THERMAL_SENSOR_NA
)
6202 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
6205 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6206 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6207 thermal_temp_input_show, NULL, _idxB)
6209 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
6210 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6211 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6212 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6213 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6214 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6215 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6216 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6217 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6218 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6219 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6220 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6221 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6222 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6223 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6224 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6225 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6228 #define THERMAL_ATTRS(X) \
6229 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6231 static struct attribute
*thermal_temp_input_attr
[] = {
6251 static const struct attribute_group thermal_temp_input16_group
= {
6252 .attrs
= thermal_temp_input_attr
6255 static const struct attribute_group thermal_temp_input8_group
= {
6256 .attrs
= &thermal_temp_input_attr
[8]
6259 #undef THERMAL_SENSOR_ATTR_TEMP
6260 #undef THERMAL_ATTRS
6262 /* --------------------------------------------------------------------- */
6264 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
6271 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
6273 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
6275 if (thinkpad_id
.ec_model
) {
6277 * Direct EC access mode: sensors at registers
6278 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6279 * non-implemented, thermal sensors return 0x80 when
6284 for (i
= 0; i
< 8; i
++) {
6285 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
6291 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
6299 /* This is sheer paranoia, but we handle it anyway */
6301 pr_err("ThinkPad ACPI EC access misbehaving, "
6302 "falling back to ACPI TMPx access "
6304 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
6306 pr_err("ThinkPad ACPI EC access misbehaving, "
6307 "disabling thermal sensors access\n");
6308 thermal_read_mode
= TPACPI_THERMAL_NONE
;
6313 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
6315 } else if (acpi_tmp7
) {
6316 if (tpacpi_is_ibm() &&
6317 acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
6318 /* 600e/x, 770e, 770x */
6319 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
6321 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6322 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
6325 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6326 thermal_read_mode
= TPACPI_THERMAL_NONE
;
6329 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
6330 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
6333 switch (thermal_read_mode
) {
6334 case TPACPI_THERMAL_TPEC_16
:
6335 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6336 &thermal_temp_input16_group
);
6340 case TPACPI_THERMAL_TPEC_8
:
6341 case TPACPI_THERMAL_ACPI_TMP07
:
6342 case TPACPI_THERMAL_ACPI_UPDT
:
6343 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6344 &thermal_temp_input8_group
);
6348 case TPACPI_THERMAL_NONE
:
6356 static void thermal_exit(void)
6358 switch (thermal_read_mode
) {
6359 case TPACPI_THERMAL_TPEC_16
:
6360 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6361 &thermal_temp_input16_group
);
6363 case TPACPI_THERMAL_TPEC_8
:
6364 case TPACPI_THERMAL_ACPI_TMP07
:
6365 case TPACPI_THERMAL_ACPI_UPDT
:
6366 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6367 &thermal_temp_input8_group
);
6369 case TPACPI_THERMAL_NONE
:
6375 static int thermal_read(struct seq_file
*m
)
6378 struct ibm_thermal_sensors_struct t
;
6380 n
= thermal_get_sensors(&t
);
6381 if (unlikely(n
< 0))
6384 seq_printf(m
, "temperatures:\t");
6387 for (i
= 0; i
< (n
- 1); i
++)
6388 seq_printf(m
, "%d ", t
.temp
[i
] / 1000);
6389 seq_printf(m
, "%d\n", t
.temp
[i
] / 1000);
6391 seq_printf(m
, "not supported\n");
6396 static struct ibm_struct thermal_driver_data
= {
6398 .read
= thermal_read
,
6399 .exit
= thermal_exit
,
6402 /*************************************************************************
6403 * Backlight/brightness subdriver
6406 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6409 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6410 * CMOS NVRAM byte 0x5E, bits 0-3.
6412 * EC HBRV (0x31) has the following layout
6413 * Bit 7: unknown function
6414 * Bit 6: unknown function
6415 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6416 * Bit 4: must be set to zero to avoid problems
6417 * Bit 3-0: backlight brightness level
6419 * brightness_get_raw returns status data in the HBRV layout
6421 * WARNING: The X61 has been verified to use HBRV for something else, so
6422 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6423 * testing on the very early *60 Lenovo models...
6427 TP_EC_BACKLIGHT
= 0x31,
6429 /* TP_EC_BACKLIGHT bitmasks */
6430 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
6431 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
6432 TP_EC_BACKLIGHT_MAPSW
= 0x20,
6435 enum tpacpi_brightness_access_mode
{
6436 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
6437 TPACPI_BRGHT_MODE_EC
, /* EC control */
6438 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
6439 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
6440 TPACPI_BRGHT_MODE_MAX
6443 static struct backlight_device
*ibm_backlight_device
;
6445 static enum tpacpi_brightness_access_mode brightness_mode
=
6446 TPACPI_BRGHT_MODE_MAX
;
6448 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
6450 static struct mutex brightness_mutex
;
6452 /* NVRAM brightness access,
6453 * call with brightness_mutex held! */
6454 static unsigned int tpacpi_brightness_nvram_get(void)
6458 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
6459 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
6460 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
6461 lnvram
&= bright_maxlvl
;
6466 static void tpacpi_brightness_checkpoint_nvram(void)
6471 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
6474 vdbg_printk(TPACPI_DBG_BRGHT
,
6475 "trying to checkpoint backlight level to NVRAM...\n");
6477 if (mutex_lock_killable(&brightness_mutex
) < 0)
6480 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6482 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
6483 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
6485 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
6486 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
6487 /* NVRAM needs update */
6488 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
6489 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
6491 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
6492 dbg_printk(TPACPI_DBG_BRGHT
,
6493 "updated NVRAM backlight level to %u (0x%02x)\n",
6494 (unsigned int) lec
, (unsigned int) b_nvram
);
6496 vdbg_printk(TPACPI_DBG_BRGHT
,
6497 "NVRAM backlight level already is %u (0x%02x)\n",
6498 (unsigned int) lec
, (unsigned int) b_nvram
);
6501 mutex_unlock(&brightness_mutex
);
6505 /* call with brightness_mutex held! */
6506 static int tpacpi_brightness_get_raw(int *status
)
6510 switch (brightness_mode
) {
6511 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6512 *status
= tpacpi_brightness_nvram_get();
6514 case TPACPI_BRGHT_MODE_EC
:
6515 case TPACPI_BRGHT_MODE_ECNVRAM
:
6516 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6525 /* call with brightness_mutex held! */
6526 /* do NOT call with illegal backlight level value */
6527 static int tpacpi_brightness_set_ec(unsigned int value
)
6531 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6534 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
6535 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
6536 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
6542 /* call with brightness_mutex held! */
6543 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
6546 unsigned int current_value
, i
;
6548 current_value
= tpacpi_brightness_nvram_get();
6550 if (value
== current_value
)
6553 cmos_cmd
= (value
> current_value
) ?
6554 TP_CMOS_BRIGHTNESS_UP
:
6555 TP_CMOS_BRIGHTNESS_DOWN
;
6556 inc
= (value
> current_value
) ? 1 : -1;
6558 for (i
= current_value
; i
!= value
; i
+= inc
)
6559 if (issue_thinkpad_cmos_command(cmos_cmd
))
6565 /* May return EINTR which can always be mapped to ERESTARTSYS */
6566 static int brightness_set(unsigned int value
)
6570 if (value
> bright_maxlvl
)
6573 vdbg_printk(TPACPI_DBG_BRGHT
,
6574 "set backlight level to %d\n", value
);
6576 res
= mutex_lock_killable(&brightness_mutex
);
6580 switch (brightness_mode
) {
6581 case TPACPI_BRGHT_MODE_EC
:
6582 case TPACPI_BRGHT_MODE_ECNVRAM
:
6583 res
= tpacpi_brightness_set_ec(value
);
6585 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6586 res
= tpacpi_brightness_set_ucmsstep(value
);
6592 mutex_unlock(&brightness_mutex
);
6596 /* sysfs backlight class ----------------------------------------------- */
6598 static int brightness_update_status(struct backlight_device
*bd
)
6600 unsigned int level
=
6601 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
6602 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
6603 bd
->props
.brightness
: 0;
6605 dbg_printk(TPACPI_DBG_BRGHT
,
6606 "backlight: attempt to set level to %d\n",
6609 /* it is the backlight class's job (caller) to handle
6610 * EINTR and other errors properly */
6611 return brightness_set(level
);
6614 static int brightness_get(struct backlight_device
*bd
)
6618 res
= mutex_lock_killable(&brightness_mutex
);
6622 res
= tpacpi_brightness_get_raw(&status
);
6624 mutex_unlock(&brightness_mutex
);
6629 return status
& TP_EC_BACKLIGHT_LVLMSK
;
6632 static void tpacpi_brightness_notify_change(void)
6634 backlight_force_update(ibm_backlight_device
,
6635 BACKLIGHT_UPDATE_HOTKEY
);
6638 static const struct backlight_ops ibm_backlight_data
= {
6639 .get_brightness
= brightness_get
,
6640 .update_status
= brightness_update_status
,
6643 /* --------------------------------------------------------------------- */
6646 * Call _BCL method of video device. On some ThinkPads this will
6647 * switch the firmware to the ACPI brightness control mode.
6650 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
6652 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
6653 union acpi_object
*obj
;
6654 struct acpi_device
*device
, *child
;
6657 if (acpi_bus_get_device(handle
, &device
))
6661 list_for_each_entry(child
, &device
->children
, node
) {
6662 acpi_status status
= acpi_evaluate_object(child
->handle
, "_BCL",
6664 if (ACPI_FAILURE(status
))
6667 obj
= (union acpi_object
*)buffer
.pointer
;
6668 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
6669 pr_err("Unknown _BCL data, please report this to %s\n",
6673 rc
= obj
->package
.count
;
6678 kfree(buffer
.pointer
);
6684 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6686 static unsigned int __init
tpacpi_check_std_acpi_brightness_support(void)
6688 acpi_handle video_device
;
6691 tpacpi_acpi_handle_locate("video", NULL
, &video_device
);
6693 bcl_levels
= tpacpi_query_bcl_levels(video_device
);
6695 tp_features
.bright_acpimode
= (bcl_levels
> 0);
6697 return (bcl_levels
> 2) ? (bcl_levels
- 2) : 0;
6701 * These are only useful for models that have only one possibility
6702 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6705 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6706 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6707 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6709 static const struct tpacpi_quirk brightness_quirk_table
[] __initconst
= {
6710 /* Models with ATI GPUs known to require ECNVRAM mode */
6711 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC
), /* T43/p ATI */
6713 /* Models with ATI GPUs that can use ECNVRAM */
6714 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC
), /* R50,51 T40-42 */
6715 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6716 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC
), /* R52 */
6717 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6719 /* Models with Intel Extreme Graphics 2 */
6720 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC
), /* X40 */
6721 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6722 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6724 /* Models with Intel GMA900 */
6725 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC
), /* T43, R52 */
6726 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC
), /* X41 */
6727 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC
), /* X41 Tablet */
6731 * Returns < 0 for error, otherwise sets tp_features.bright_*
6732 * and bright_maxlvl.
6734 static void __init
tpacpi_detect_brightness_capabilities(void)
6738 vdbg_printk(TPACPI_DBG_INIT
,
6739 "detecting firmware brightness interface capabilities\n");
6741 /* we could run a quirks check here (same table used by
6742 * brightness_init) if needed */
6745 * We always attempt to detect acpi support, so as to switch
6746 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6747 * going to publish a backlight interface
6749 b
= tpacpi_check_std_acpi_brightness_support();
6759 tp_features
.bright_unkfw
= 1;
6760 bright_maxlvl
= b
- 1;
6762 pr_debug("detected %u brightness levels\n", bright_maxlvl
+ 1);
6765 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
6767 struct backlight_properties props
;
6769 unsigned long quirks
;
6771 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
6773 mutex_init(&brightness_mutex
);
6775 quirks
= tpacpi_check_quirks(brightness_quirk_table
,
6776 ARRAY_SIZE(brightness_quirk_table
));
6778 /* tpacpi_detect_brightness_capabilities() must have run already */
6780 /* if it is unknown, we don't handle it: it wouldn't be safe */
6781 if (tp_features
.bright_unkfw
)
6784 if (!brightness_enable
) {
6785 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6786 "brightness support disabled by "
6787 "module parameter\n");
6791 if (acpi_video_get_backlight_type() != acpi_backlight_vendor
) {
6792 if (brightness_enable
> 1) {
6793 pr_info("Standard ACPI backlight interface "
6794 "available, not loading native one\n");
6796 } else if (brightness_enable
== 1) {
6797 pr_warn("Cannot enable backlight brightness support, "
6798 "ACPI is already handling it. Refer to the "
6799 "acpi_backlight kernel parameter.\n");
6802 } else if (tp_features
.bright_acpimode
&& brightness_enable
> 1) {
6803 pr_notice("Standard ACPI backlight interface not "
6804 "available, thinkpad_acpi native "
6805 "brightness control enabled\n");
6809 * Check for module parameter bogosity, note that we
6810 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6811 * able to detect "unspecified"
6813 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
6816 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6817 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
6818 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
6819 if (quirks
& TPACPI_BRGHT_Q_EC
)
6820 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
6822 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
6824 dbg_printk(TPACPI_DBG_BRGHT
,
6825 "driver auto-selected brightness_mode=%d\n",
6830 if (!tpacpi_is_ibm() &&
6831 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
6832 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
6835 if (tpacpi_brightness_get_raw(&b
) < 0)
6838 memset(&props
, 0, sizeof(struct backlight_properties
));
6839 props
.type
= BACKLIGHT_PLATFORM
;
6840 props
.max_brightness
= bright_maxlvl
;
6841 props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
6842 ibm_backlight_device
= backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME
,
6844 &ibm_backlight_data
,
6846 if (IS_ERR(ibm_backlight_device
)) {
6847 int rc
= PTR_ERR(ibm_backlight_device
);
6848 ibm_backlight_device
= NULL
;
6849 pr_err("Could not register backlight device\n");
6852 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6853 "brightness is supported\n");
6855 if (quirks
& TPACPI_BRGHT_Q_ASK
) {
6856 pr_notice("brightness: will use unverified default: "
6857 "brightness_mode=%d\n", brightness_mode
);
6858 pr_notice("brightness: please report to %s whether it works well "
6859 "or not on your ThinkPad\n", TPACPI_MAIL
);
6862 /* Added by mistake in early 2007. Probably useless, but it could
6863 * be working around some unknown firmware problem where the value
6864 * read at startup doesn't match the real hardware state... so leave
6865 * it in place just in case */
6866 backlight_update_status(ibm_backlight_device
);
6868 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6869 "brightness: registering brightness hotkeys "
6870 "as change notification\n");
6871 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6872 | TP_ACPI_HKEY_BRGHTUP_MASK
6873 | TP_ACPI_HKEY_BRGHTDWN_MASK
);
6877 static void brightness_suspend(void)
6879 tpacpi_brightness_checkpoint_nvram();
6882 static void brightness_shutdown(void)
6884 tpacpi_brightness_checkpoint_nvram();
6887 static void brightness_exit(void)
6889 if (ibm_backlight_device
) {
6890 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
6891 "calling backlight_device_unregister()\n");
6892 backlight_device_unregister(ibm_backlight_device
);
6895 tpacpi_brightness_checkpoint_nvram();
6898 static int brightness_read(struct seq_file
*m
)
6902 level
= brightness_get(NULL
);
6904 seq_printf(m
, "level:\t\tunreadable\n");
6906 seq_printf(m
, "level:\t\t%d\n", level
);
6907 seq_printf(m
, "commands:\tup, down\n");
6908 seq_printf(m
, "commands:\tlevel <level> (<level> is 0-%d)\n",
6915 static int brightness_write(char *buf
)
6921 level
= brightness_get(NULL
);
6925 while ((cmd
= next_cmd(&buf
))) {
6926 if (strlencmp(cmd
, "up") == 0) {
6927 if (level
< bright_maxlvl
)
6929 } else if (strlencmp(cmd
, "down") == 0) {
6932 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
6933 level
>= 0 && level
<= bright_maxlvl
) {
6939 tpacpi_disclose_usertask("procfs brightness",
6940 "set level to %d\n", level
);
6943 * Now we know what the final level should be, so we try to set it.
6944 * Doing it this way makes the syscall restartable in case of EINTR
6946 rc
= brightness_set(level
);
6947 if (!rc
&& ibm_backlight_device
)
6948 backlight_force_update(ibm_backlight_device
,
6949 BACKLIGHT_UPDATE_SYSFS
);
6950 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
6953 static struct ibm_struct brightness_driver_data
= {
6954 .name
= "brightness",
6955 .read
= brightness_read
,
6956 .write
= brightness_write
,
6957 .exit
= brightness_exit
,
6958 .suspend
= brightness_suspend
,
6959 .shutdown
= brightness_shutdown
,
6962 /*************************************************************************
6967 * IBM ThinkPads have a simple volume controller with MUTE gating.
6968 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6970 * Since the *61 series (and probably also the later *60 series), Lenovo
6971 * ThinkPads only implement the MUTE gate.
6974 * Bit 6: MUTE (1 mutes sound)
6976 * Other bits should be zero as far as we know.
6978 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6979 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6980 * such as bit 7 which is used to detect repeated presses of MUTE,
6981 * and we leave them unchanged.
6983 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6984 * in response to user input. Unfortunately, this rarely works well.
6985 * The laptop changes the state of its internal MUTE gate and, on some
6986 * models, sends KEY_MUTE, causing any user code that responds to the
6987 * mute button to get confused. The hardware MUTE gate is also
6988 * unnecessary, since user code can handle the mute button without
6989 * kernel or EC help.
6991 * To avoid confusing userspace, we simply disable all EC-based mute
6992 * and volume controls when possible.
6995 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6997 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6998 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6999 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7001 #if SNDRV_CARDS <= 32
7002 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7004 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7006 static int alsa_index
= DEFAULT_ALSA_IDX
; /* last three slots */
7007 static char *alsa_id
= "ThinkPadEC";
7008 static bool alsa_enable
= SNDRV_DEFAULT_ENABLE1
;
7010 struct tpacpi_alsa_data
{
7011 struct snd_card
*card
;
7012 struct snd_ctl_elem_id
*ctl_mute_id
;
7013 struct snd_ctl_elem_id
*ctl_vol_id
;
7016 static struct snd_card
*alsa_card
;
7021 /* TP_EC_AUDIO bits */
7022 TP_EC_AUDIO_MUTESW
= 6,
7024 /* TP_EC_AUDIO bitmasks */
7025 TP_EC_AUDIO_LVL_MSK
= 0x0F,
7026 TP_EC_AUDIO_MUTESW_MSK
= (1 << TP_EC_AUDIO_MUTESW
),
7028 /* Maximum volume */
7029 TP_EC_VOLUME_MAX
= 14,
7032 enum tpacpi_volume_access_mode
{
7033 TPACPI_VOL_MODE_AUTO
= 0, /* Not implemented yet */
7034 TPACPI_VOL_MODE_EC
, /* Pure EC control */
7035 TPACPI_VOL_MODE_UCMS_STEP
, /* UCMS step-based control: N/A */
7036 TPACPI_VOL_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
7040 enum tpacpi_volume_capabilities
{
7041 TPACPI_VOL_CAP_AUTO
= 0, /* Use white/blacklist */
7042 TPACPI_VOL_CAP_VOLMUTE
, /* Output vol and mute */
7043 TPACPI_VOL_CAP_MUTEONLY
, /* Output mute only */
7047 enum tpacpi_mute_btn_mode
{
7048 TP_EC_MUTE_BTN_LATCH
= 0, /* Mute mutes; up/down unmutes */
7049 /* We don't know what mode 1 is. */
7050 TP_EC_MUTE_BTN_NONE
= 2, /* Mute and up/down are just keys */
7051 TP_EC_MUTE_BTN_TOGGLE
= 3, /* Mute toggles; up/down unmutes */
7054 static enum tpacpi_volume_access_mode volume_mode
=
7055 TPACPI_VOL_MODE_MAX
;
7057 static enum tpacpi_volume_capabilities volume_capabilities
;
7058 static bool volume_control_allowed
;
7059 static bool software_mute_requested
= true;
7060 static bool software_mute_active
;
7061 static int software_mute_orig_mode
;
7064 * Used to syncronize writers to TP_EC_AUDIO and
7065 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7067 static struct mutex volume_mutex
;
7069 static void tpacpi_volume_checkpoint_nvram(void)
7075 if (volume_mode
!= TPACPI_VOL_MODE_ECNVRAM
)
7077 if (!volume_control_allowed
)
7079 if (software_mute_active
)
7082 vdbg_printk(TPACPI_DBG_MIXER
,
7083 "trying to checkpoint mixer state to NVRAM...\n");
7085 if (tp_features
.mixer_no_level_control
)
7086 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
;
7088 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
| TP_EC_AUDIO_LVL_MSK
;
7090 if (mutex_lock_killable(&volume_mutex
) < 0)
7093 if (unlikely(!acpi_ec_read(TP_EC_AUDIO
, &lec
)))
7096 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
7098 if (lec
!= (b_nvram
& ec_mask
)) {
7099 /* NVRAM needs update */
7100 b_nvram
&= ~ec_mask
;
7102 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_MIXER
);
7103 dbg_printk(TPACPI_DBG_MIXER
,
7104 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7105 (unsigned int) lec
, (unsigned int) b_nvram
);
7107 vdbg_printk(TPACPI_DBG_MIXER
,
7108 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7109 (unsigned int) lec
, (unsigned int) b_nvram
);
7113 mutex_unlock(&volume_mutex
);
7116 static int volume_get_status_ec(u8
*status
)
7120 if (!acpi_ec_read(TP_EC_AUDIO
, &s
))
7125 dbg_printk(TPACPI_DBG_MIXER
, "status 0x%02x\n", s
);
7130 static int volume_get_status(u8
*status
)
7132 return volume_get_status_ec(status
);
7135 static int volume_set_status_ec(const u8 status
)
7137 if (!acpi_ec_write(TP_EC_AUDIO
, status
))
7140 dbg_printk(TPACPI_DBG_MIXER
, "set EC mixer to 0x%02x\n", status
);
7143 * On X200s, and possibly on others, it can take a while for
7144 * reads to become correct.
7151 static int volume_set_status(const u8 status
)
7153 return volume_set_status_ec(status
);
7156 /* returns < 0 on error, 0 on no change, 1 on change */
7157 static int __volume_set_mute_ec(const bool mute
)
7162 if (mutex_lock_killable(&volume_mutex
) < 0)
7165 rc
= volume_get_status_ec(&s
);
7169 n
= (mute
) ? s
| TP_EC_AUDIO_MUTESW_MSK
:
7170 s
& ~TP_EC_AUDIO_MUTESW_MSK
;
7173 rc
= volume_set_status_ec(n
);
7179 mutex_unlock(&volume_mutex
);
7183 static int volume_alsa_set_mute(const bool mute
)
7185 dbg_printk(TPACPI_DBG_MIXER
, "ALSA: trying to %smute\n",
7186 (mute
) ? "" : "un");
7187 return __volume_set_mute_ec(mute
);
7190 static int volume_set_mute(const bool mute
)
7194 dbg_printk(TPACPI_DBG_MIXER
, "trying to %smute\n",
7195 (mute
) ? "" : "un");
7197 rc
= __volume_set_mute_ec(mute
);
7198 return (rc
< 0) ? rc
: 0;
7201 /* returns < 0 on error, 0 on no change, 1 on change */
7202 static int __volume_set_volume_ec(const u8 vol
)
7207 if (vol
> TP_EC_VOLUME_MAX
)
7210 if (mutex_lock_killable(&volume_mutex
) < 0)
7213 rc
= volume_get_status_ec(&s
);
7217 n
= (s
& ~TP_EC_AUDIO_LVL_MSK
) | vol
;
7220 rc
= volume_set_status_ec(n
);
7226 mutex_unlock(&volume_mutex
);
7230 static int volume_set_software_mute(bool startup
)
7234 if (!tpacpi_is_lenovo())
7238 if (!acpi_evalf(ec_handle
, &software_mute_orig_mode
,
7242 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7243 "Initial HAUM setting was %d\n",
7244 software_mute_orig_mode
);
7247 if (!acpi_evalf(ec_handle
, &result
, "SAUM", "qdd",
7248 (int)TP_EC_MUTE_BTN_NONE
))
7251 if (result
!= TP_EC_MUTE_BTN_NONE
)
7252 pr_warn("Unexpected SAUM result %d\n",
7256 * In software mute mode, the standard codec controls take
7257 * precendence, so we unmute the ThinkPad HW switch at
7258 * startup. Just on case there are SAUM-capable ThinkPads
7259 * with level controls, set max HW volume as well.
7261 if (tp_features
.mixer_no_level_control
)
7262 result
= volume_set_mute(false);
7264 result
= volume_set_status(TP_EC_VOLUME_MAX
);
7267 pr_warn("Failed to unmute the HW mute switch\n");
7272 static void volume_exit_software_mute(void)
7276 if (!acpi_evalf(ec_handle
, &r
, "SAUM", "qdd", software_mute_orig_mode
)
7277 || r
!= software_mute_orig_mode
)
7278 pr_warn("Failed to restore mute mode\n");
7281 static int volume_alsa_set_volume(const u8 vol
)
7283 dbg_printk(TPACPI_DBG_MIXER
,
7284 "ALSA: trying to set volume level to %hu\n", vol
);
7285 return __volume_set_volume_ec(vol
);
7288 static void volume_alsa_notify_change(void)
7290 struct tpacpi_alsa_data
*d
;
7292 if (alsa_card
&& alsa_card
->private_data
) {
7293 d
= alsa_card
->private_data
;
7295 snd_ctl_notify(alsa_card
,
7296 SNDRV_CTL_EVENT_MASK_VALUE
,
7299 snd_ctl_notify(alsa_card
,
7300 SNDRV_CTL_EVENT_MASK_VALUE
,
7305 static int volume_alsa_vol_info(struct snd_kcontrol
*kcontrol
,
7306 struct snd_ctl_elem_info
*uinfo
)
7308 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
7310 uinfo
->value
.integer
.min
= 0;
7311 uinfo
->value
.integer
.max
= TP_EC_VOLUME_MAX
;
7315 static int volume_alsa_vol_get(struct snd_kcontrol
*kcontrol
,
7316 struct snd_ctl_elem_value
*ucontrol
)
7321 rc
= volume_get_status(&s
);
7325 ucontrol
->value
.integer
.value
[0] = s
& TP_EC_AUDIO_LVL_MSK
;
7329 static int volume_alsa_vol_put(struct snd_kcontrol
*kcontrol
,
7330 struct snd_ctl_elem_value
*ucontrol
)
7332 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7333 ucontrol
->value
.integer
.value
[0]);
7334 return volume_alsa_set_volume(ucontrol
->value
.integer
.value
[0]);
7337 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7339 static int volume_alsa_mute_get(struct snd_kcontrol
*kcontrol
,
7340 struct snd_ctl_elem_value
*ucontrol
)
7345 rc
= volume_get_status(&s
);
7349 ucontrol
->value
.integer
.value
[0] =
7350 (s
& TP_EC_AUDIO_MUTESW_MSK
) ? 0 : 1;
7354 static int volume_alsa_mute_put(struct snd_kcontrol
*kcontrol
,
7355 struct snd_ctl_elem_value
*ucontrol
)
7357 tpacpi_disclose_usertask("ALSA", "%smute\n",
7358 ucontrol
->value
.integer
.value
[0] ?
7360 return volume_alsa_set_mute(!ucontrol
->value
.integer
.value
[0]);
7363 static struct snd_kcontrol_new volume_alsa_control_vol __initdata
= {
7364 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
7365 .name
= "Console Playback Volume",
7367 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
7368 .info
= volume_alsa_vol_info
,
7369 .get
= volume_alsa_vol_get
,
7372 static struct snd_kcontrol_new volume_alsa_control_mute __initdata
= {
7373 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
7374 .name
= "Console Playback Switch",
7376 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
7377 .info
= volume_alsa_mute_info
,
7378 .get
= volume_alsa_mute_get
,
7381 static void volume_suspend(void)
7383 tpacpi_volume_checkpoint_nvram();
7386 static void volume_resume(void)
7388 if (software_mute_active
) {
7389 if (volume_set_software_mute(false) < 0)
7390 pr_warn("Failed to restore software mute\n");
7392 volume_alsa_notify_change();
7396 static void volume_shutdown(void)
7398 tpacpi_volume_checkpoint_nvram();
7401 static void volume_exit(void)
7404 snd_card_free(alsa_card
);
7408 tpacpi_volume_checkpoint_nvram();
7410 if (software_mute_active
)
7411 volume_exit_software_mute();
7414 static int __init
volume_create_alsa_mixer(void)
7416 struct snd_card
*card
;
7417 struct tpacpi_alsa_data
*data
;
7418 struct snd_kcontrol
*ctl_vol
;
7419 struct snd_kcontrol
*ctl_mute
;
7422 rc
= snd_card_new(&tpacpi_pdev
->dev
,
7423 alsa_index
, alsa_id
, THIS_MODULE
,
7424 sizeof(struct tpacpi_alsa_data
), &card
);
7425 if (rc
< 0 || !card
) {
7426 pr_err("Failed to create ALSA card structures: %d\n", rc
);
7430 BUG_ON(!card
->private_data
);
7431 data
= card
->private_data
;
7434 strlcpy(card
->driver
, TPACPI_ALSA_DRVNAME
,
7435 sizeof(card
->driver
));
7436 strlcpy(card
->shortname
, TPACPI_ALSA_SHRTNAME
,
7437 sizeof(card
->shortname
));
7438 snprintf(card
->mixername
, sizeof(card
->mixername
), "ThinkPad EC %s",
7439 (thinkpad_id
.ec_version_str
) ?
7440 thinkpad_id
.ec_version_str
: "(unknown)");
7441 snprintf(card
->longname
, sizeof(card
->longname
),
7442 "%s at EC reg 0x%02x, fw %s", card
->shortname
, TP_EC_AUDIO
,
7443 (thinkpad_id
.ec_version_str
) ?
7444 thinkpad_id
.ec_version_str
: "unknown");
7446 if (volume_control_allowed
) {
7447 volume_alsa_control_vol
.put
= volume_alsa_vol_put
;
7448 volume_alsa_control_vol
.access
=
7449 SNDRV_CTL_ELEM_ACCESS_READWRITE
;
7451 volume_alsa_control_mute
.put
= volume_alsa_mute_put
;
7452 volume_alsa_control_mute
.access
=
7453 SNDRV_CTL_ELEM_ACCESS_READWRITE
;
7456 if (!tp_features
.mixer_no_level_control
) {
7457 ctl_vol
= snd_ctl_new1(&volume_alsa_control_vol
, NULL
);
7458 rc
= snd_ctl_add(card
, ctl_vol
);
7460 pr_err("Failed to create ALSA volume control: %d\n",
7464 data
->ctl_vol_id
= &ctl_vol
->id
;
7467 ctl_mute
= snd_ctl_new1(&volume_alsa_control_mute
, NULL
);
7468 rc
= snd_ctl_add(card
, ctl_mute
);
7470 pr_err("Failed to create ALSA mute control: %d\n", rc
);
7473 data
->ctl_mute_id
= &ctl_mute
->id
;
7475 rc
= snd_card_register(card
);
7477 pr_err("Failed to register ALSA card: %d\n", rc
);
7485 snd_card_free(card
);
7489 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7490 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7492 static const struct tpacpi_quirk volume_quirk_table
[] __initconst
= {
7493 /* Whitelist volume level on all IBM by default */
7494 { .vendor
= PCI_VENDOR_ID_IBM
,
7495 .bios
= TPACPI_MATCH_ANY
,
7496 .ec
= TPACPI_MATCH_ANY
,
7497 .quirks
= TPACPI_VOL_Q_LEVEL
},
7499 /* Lenovo models with volume control (needs confirmation) */
7500 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL
), /* R60/i */
7501 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL
), /* R60e/i */
7502 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL
), /* T60/p */
7503 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL
), /* X60/s */
7504 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL
), /* X60t */
7505 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL
), /* Z60 */
7506 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL
), /* Z61 */
7508 /* Whitelist mute-only on all Lenovo by default */
7509 { .vendor
= PCI_VENDOR_ID_LENOVO
,
7510 .bios
= TPACPI_MATCH_ANY
,
7511 .ec
= TPACPI_MATCH_ANY
,
7512 .quirks
= TPACPI_VOL_Q_MUTEONLY
}
7515 static int __init
volume_init(struct ibm_init_struct
*iibm
)
7517 unsigned long quirks
;
7520 vdbg_printk(TPACPI_DBG_INIT
, "initializing volume subdriver\n");
7522 mutex_init(&volume_mutex
);
7525 * Check for module parameter bogosity, note that we
7526 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7527 * able to detect "unspecified"
7529 if (volume_mode
> TPACPI_VOL_MODE_MAX
)
7532 if (volume_mode
== TPACPI_VOL_MODE_UCMS_STEP
) {
7533 pr_err("UCMS step volume mode not implemented, "
7534 "please contact %s\n", TPACPI_MAIL
);
7538 if (volume_capabilities
>= TPACPI_VOL_CAP_MAX
)
7542 * The ALSA mixer is our primary interface.
7543 * When disabled, don't install the subdriver at all
7546 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7547 "ALSA mixer disabled by parameter, "
7548 "not loading volume subdriver...\n");
7552 quirks
= tpacpi_check_quirks(volume_quirk_table
,
7553 ARRAY_SIZE(volume_quirk_table
));
7555 switch (volume_capabilities
) {
7556 case TPACPI_VOL_CAP_AUTO
:
7557 if (quirks
& TPACPI_VOL_Q_MUTEONLY
)
7558 tp_features
.mixer_no_level_control
= 1;
7559 else if (quirks
& TPACPI_VOL_Q_LEVEL
)
7560 tp_features
.mixer_no_level_control
= 0;
7562 return 1; /* no mixer */
7564 case TPACPI_VOL_CAP_VOLMUTE
:
7565 tp_features
.mixer_no_level_control
= 0;
7567 case TPACPI_VOL_CAP_MUTEONLY
:
7568 tp_features
.mixer_no_level_control
= 1;
7574 if (volume_capabilities
!= TPACPI_VOL_CAP_AUTO
)
7575 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7576 "using user-supplied volume_capabilities=%d\n",
7577 volume_capabilities
);
7579 if (volume_mode
== TPACPI_VOL_MODE_AUTO
||
7580 volume_mode
== TPACPI_VOL_MODE_MAX
) {
7581 volume_mode
= TPACPI_VOL_MODE_ECNVRAM
;
7583 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7584 "driver auto-selected volume_mode=%d\n",
7587 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7588 "using user-supplied volume_mode=%d\n",
7592 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7593 "mute is supported, volume control is %s\n",
7594 str_supported(!tp_features
.mixer_no_level_control
));
7596 if (software_mute_requested
&& volume_set_software_mute(true) == 0) {
7597 software_mute_active
= true;
7599 rc
= volume_create_alsa_mixer();
7601 pr_err("Could not create the ALSA mixer interface\n");
7605 pr_info("Console audio control enabled, mode: %s\n",
7606 (volume_control_allowed
) ?
7607 "override (read/write)" :
7608 "monitor (read only)");
7611 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7612 "registering volume hotkeys as change notification\n");
7613 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7614 | TP_ACPI_HKEY_VOLUP_MASK
7615 | TP_ACPI_HKEY_VOLDWN_MASK
7616 | TP_ACPI_HKEY_MUTE_MASK
);
7621 static int volume_read(struct seq_file
*m
)
7625 if (volume_get_status(&status
) < 0) {
7626 seq_printf(m
, "level:\t\tunreadable\n");
7628 if (tp_features
.mixer_no_level_control
)
7629 seq_printf(m
, "level:\t\tunsupported\n");
7631 seq_printf(m
, "level:\t\t%d\n",
7632 status
& TP_EC_AUDIO_LVL_MSK
);
7634 seq_printf(m
, "mute:\t\t%s\n",
7635 onoff(status
, TP_EC_AUDIO_MUTESW
));
7637 if (volume_control_allowed
) {
7638 seq_printf(m
, "commands:\tunmute, mute\n");
7639 if (!tp_features
.mixer_no_level_control
) {
7641 "commands:\tup, down\n");
7643 "commands:\tlevel <level>"
7644 " (<level> is 0-%d)\n",
7653 static int volume_write(char *buf
)
7656 u8 new_level
, new_mute
;
7662 * We do allow volume control at driver startup, so that the
7663 * user can set initial state through the volume=... parameter hack.
7665 if (!volume_control_allowed
&& tpacpi_lifecycle
!= TPACPI_LIFE_INIT
) {
7666 if (unlikely(!tp_warned
.volume_ctrl_forbidden
)) {
7667 tp_warned
.volume_ctrl_forbidden
= 1;
7668 pr_notice("Console audio control in monitor mode, "
7669 "changes are not allowed\n");
7670 pr_notice("Use the volume_control=1 module parameter "
7671 "to enable volume control\n");
7676 rc
= volume_get_status(&s
);
7680 new_level
= s
& TP_EC_AUDIO_LVL_MSK
;
7681 new_mute
= s
& TP_EC_AUDIO_MUTESW_MSK
;
7683 while ((cmd
= next_cmd(&buf
))) {
7684 if (!tp_features
.mixer_no_level_control
) {
7685 if (strlencmp(cmd
, "up") == 0) {
7688 else if (new_level
< TP_EC_VOLUME_MAX
)
7691 } else if (strlencmp(cmd
, "down") == 0) {
7694 else if (new_level
> 0)
7697 } else if (sscanf(cmd
, "level %u", &l
) == 1 &&
7698 l
>= 0 && l
<= TP_EC_VOLUME_MAX
) {
7703 if (strlencmp(cmd
, "mute") == 0)
7704 new_mute
= TP_EC_AUDIO_MUTESW_MSK
;
7705 else if (strlencmp(cmd
, "unmute") == 0)
7711 if (tp_features
.mixer_no_level_control
) {
7712 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7713 new_mute
? "" : "un");
7714 rc
= volume_set_mute(!!new_mute
);
7716 tpacpi_disclose_usertask("procfs volume",
7717 "%smute and set level to %d\n",
7718 new_mute
? "" : "un", new_level
);
7719 rc
= volume_set_status(new_mute
| new_level
);
7721 volume_alsa_notify_change();
7723 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
7726 static struct ibm_struct volume_driver_data
= {
7728 .read
= volume_read
,
7729 .write
= volume_write
,
7730 .exit
= volume_exit
,
7731 .suspend
= volume_suspend
,
7732 .resume
= volume_resume
,
7733 .shutdown
= volume_shutdown
,
7736 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7738 #define alsa_card NULL
7740 static inline void volume_alsa_notify_change(void)
7744 static int __init
volume_init(struct ibm_init_struct
*iibm
)
7746 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7751 static struct ibm_struct volume_driver_data
= {
7755 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7757 /*************************************************************************
7764 * TPACPI_FAN_RD_ACPI_GFAN:
7765 * ACPI GFAN method: returns fan level
7767 * see TPACPI_FAN_WR_ACPI_SFAN
7768 * EC 0x2f (HFSP) not available if GFAN exists
7770 * TPACPI_FAN_WR_ACPI_SFAN:
7771 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7773 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7776 * TPACPI_FAN_WR_TPEC:
7777 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7778 * Supported on almost all ThinkPads
7780 * Fan speed changes of any sort (including those caused by the
7781 * disengaged mode) are usually done slowly by the firmware as the
7782 * maximum amount of fan duty cycle change per second seems to be
7785 * Reading is not available if GFAN exists.
7786 * Writing is not available if SFAN exists.
7789 * 7 automatic mode engaged;
7790 * (default operation mode of the ThinkPad)
7791 * fan level is ignored in this mode.
7792 * 6 full speed mode (takes precedence over bit 7);
7793 * not available on all thinkpads. May disable
7794 * the tachometer while the fan controller ramps up
7795 * the speed (which can take up to a few *minutes*).
7796 * Speeds up fan to 100% duty-cycle, which is far above
7797 * the standard RPM levels. It is not impossible that
7798 * it could cause hardware damage.
7799 * 5-3 unused in some models. Extra bits for fan level
7800 * in others, but still useless as all values above
7801 * 7 map to the same speed as level 7 in these models.
7802 * 2-0 fan level (0..7 usually)
7804 * 0x07 = max (set when temperatures critical)
7805 * Some ThinkPads may have other levels, see
7806 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7808 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7809 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7810 * does so, its initial value is meaningless (0x07).
7812 * For firmware bugs, refer to:
7813 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7817 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7818 * Main fan tachometer reading (in RPM)
7820 * This register is present on all ThinkPads with a new-style EC, and
7821 * it is known not to be present on the A21m/e, and T22, as there is
7822 * something else in offset 0x84 according to the ACPI DSDT. Other
7823 * ThinkPads from this same time period (and earlier) probably lack the
7824 * tachometer as well.
7826 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7827 * was never fixed by IBM to report the EC firmware version string
7828 * probably support the tachometer (like the early X models), so
7829 * detecting it is quite hard. We need more data to know for sure.
7831 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7834 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7837 * For firmware bugs, refer to:
7838 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7842 * ThinkPad EC register 0x31 bit 0 (only on select models)
7844 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7845 * show the speed of the main fan. When bit 0 of EC register 0x31
7846 * is one, the tachometer registers show the speed of the auxiliary
7849 * Fan control seems to affect both fans, regardless of the state
7852 * So far, only the firmware for the X60/X61 non-tablet versions
7853 * seem to support this (firmware TP-7M).
7855 * TPACPI_FAN_WR_ACPI_FANS:
7856 * ThinkPad X31, X40, X41. Not available in the X60.
7858 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7859 * high speed. ACPI DSDT seems to map these three speeds to levels
7860 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7861 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7863 * The speeds are stored on handles
7864 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7866 * There are three default speed sets, accessible as handles:
7867 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7869 * ACPI DSDT switches which set is in use depending on various
7872 * TPACPI_FAN_WR_TPEC is also available and should be used to
7873 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7874 * but the ACPI tables just mention level 7.
7877 enum { /* Fan control constants */
7878 fan_status_offset
= 0x2f, /* EC register 0x2f */
7879 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7880 * 0x84 must be read before 0x85 */
7881 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
7882 bit 0 selects which fan is active */
7884 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
7885 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
7887 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
7890 enum fan_status_access_mode
{
7891 TPACPI_FAN_NONE
= 0, /* No fan status or control */
7892 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
7893 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7896 enum fan_control_access_mode
{
7897 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
7898 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
7899 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
7900 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
7903 enum fan_control_commands
{
7904 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
7905 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
7906 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
7907 * and also watchdog cmd */
7910 static bool fan_control_allowed
;
7912 static enum fan_status_access_mode fan_status_access_mode
;
7913 static enum fan_control_access_mode fan_control_access_mode
;
7914 static enum fan_control_commands fan_control_commands
;
7916 static u8 fan_control_initial_status
;
7917 static u8 fan_control_desired_level
;
7918 static u8 fan_control_resume_level
;
7919 static int fan_watchdog_maxinterval
;
7921 static struct mutex fan_mutex
;
7923 static void fan_watchdog_fire(struct work_struct
*ignored
);
7924 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
7926 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
7927 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
7928 "\\FSPD", /* 600e/x, 770e, 770x */
7930 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
7931 "JFNS", /* 770x-JL */
7935 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7936 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7937 * be in auto mode (0x80).
7939 * This is corrected by any write to HFSP either by the driver, or
7942 * We assume 0x07 really means auto mode while this quirk is active,
7943 * as this is far more likely than the ThinkPad being in level 7,
7944 * which is only used by the firmware during thermal emergencies.
7946 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7947 * TP-70 (T43, R52), which are known to be buggy.
7950 static void fan_quirk1_setup(void)
7952 if (fan_control_initial_status
== 0x07) {
7953 pr_notice("fan_init: initial fan status is unknown, "
7954 "assuming it is in auto mode\n");
7955 tp_features
.fan_ctrl_status_undef
= 1;
7959 static void fan_quirk1_handle(u8
*fan_status
)
7961 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
7962 if (*fan_status
!= fan_control_initial_status
) {
7963 /* something changed the HFSP regisnter since
7964 * driver init time, so it is not undefined
7966 tp_features
.fan_ctrl_status_undef
= 0;
7968 /* Return most likely status. In fact, it
7969 * might be the only possible status */
7970 *fan_status
= TP_EC_FAN_AUTO
;
7975 /* Select main fan on X60/X61, NOOP on others */
7976 static bool fan_select_fan1(void)
7978 if (tp_features
.second_fan
) {
7981 if (ec_read(fan_select_offset
, &val
) < 0)
7984 if (ec_write(fan_select_offset
, val
) < 0)
7990 /* Select secondary fan on X60/X61 */
7991 static bool fan_select_fan2(void)
7995 if (!tp_features
.second_fan
)
7998 if (ec_read(fan_select_offset
, &val
) < 0)
8001 if (ec_write(fan_select_offset
, val
) < 0)
8008 * Call with fan_mutex held
8010 static void fan_update_desired_level(u8 status
)
8013 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
8015 fan_control_desired_level
= 7;
8017 fan_control_desired_level
= status
;
8021 static int fan_get_status(u8
*status
)
8026 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8028 switch (fan_status_access_mode
) {
8029 case TPACPI_FAN_RD_ACPI_GFAN
: {
8030 /* 570, 600e/x, 770e, 770x */
8033 if (unlikely(!acpi_evalf(gfan_handle
, &res
, NULL
, "d")))
8037 *status
= res
& 0x07;
8041 case TPACPI_FAN_RD_TPEC
:
8042 /* all except 570, 600e/x, 770e, 770x */
8043 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
8046 if (likely(status
)) {
8048 fan_quirk1_handle(status
);
8060 static int fan_get_status_safe(u8
*status
)
8065 if (mutex_lock_killable(&fan_mutex
))
8066 return -ERESTARTSYS
;
8067 rc
= fan_get_status(&s
);
8069 fan_update_desired_level(s
);
8070 mutex_unlock(&fan_mutex
);
8080 static int fan_get_speed(unsigned int *speed
)
8084 switch (fan_status_access_mode
) {
8085 case TPACPI_FAN_RD_TPEC
:
8086 /* all except 570, 600e/x, 770e, 770x */
8087 if (unlikely(!fan_select_fan1()))
8089 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
8090 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
8094 *speed
= (hi
<< 8) | lo
;
8105 static int fan2_get_speed(unsigned int *speed
)
8110 switch (fan_status_access_mode
) {
8111 case TPACPI_FAN_RD_TPEC
:
8112 /* all except 570, 600e/x, 770e, 770x */
8113 if (unlikely(!fan_select_fan2()))
8115 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
8116 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
8117 fan_select_fan1(); /* play it safe */
8122 *speed
= (hi
<< 8) | lo
;
8133 static int fan_set_level(int level
)
8135 if (!fan_control_allowed
)
8138 switch (fan_control_access_mode
) {
8139 case TPACPI_FAN_WR_ACPI_SFAN
:
8140 if (level
>= 0 && level
<= 7) {
8141 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
8147 case TPACPI_FAN_WR_ACPI_FANS
:
8148 case TPACPI_FAN_WR_TPEC
:
8149 if (!(level
& TP_EC_FAN_AUTO
) &&
8150 !(level
& TP_EC_FAN_FULLSPEED
) &&
8151 ((level
< 0) || (level
> 7)))
8154 /* safety net should the EC not support AUTO
8155 * or FULLSPEED mode bits and just ignore them */
8156 if (level
& TP_EC_FAN_FULLSPEED
)
8157 level
|= 7; /* safety min speed 7 */
8158 else if (level
& TP_EC_FAN_AUTO
)
8159 level
|= 4; /* safety min speed 4 */
8161 if (!acpi_ec_write(fan_status_offset
, level
))
8164 tp_features
.fan_ctrl_status_undef
= 0;
8171 vdbg_printk(TPACPI_DBG_FAN
,
8172 "fan control: set fan control register to 0x%02x\n", level
);
8176 static int fan_set_level_safe(int level
)
8180 if (!fan_control_allowed
)
8183 if (mutex_lock_killable(&fan_mutex
))
8184 return -ERESTARTSYS
;
8186 if (level
== TPACPI_FAN_LAST_LEVEL
)
8187 level
= fan_control_desired_level
;
8189 rc
= fan_set_level(level
);
8191 fan_update_desired_level(level
);
8193 mutex_unlock(&fan_mutex
);
8197 static int fan_set_enable(void)
8202 if (!fan_control_allowed
)
8205 if (mutex_lock_killable(&fan_mutex
))
8206 return -ERESTARTSYS
;
8208 switch (fan_control_access_mode
) {
8209 case TPACPI_FAN_WR_ACPI_FANS
:
8210 case TPACPI_FAN_WR_TPEC
:
8211 rc
= fan_get_status(&s
);
8215 /* Don't go out of emergency fan mode */
8218 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
8221 if (!acpi_ec_write(fan_status_offset
, s
))
8224 tp_features
.fan_ctrl_status_undef
= 0;
8229 case TPACPI_FAN_WR_ACPI_SFAN
:
8230 rc
= fan_get_status(&s
);
8236 /* Set fan to at least level 4 */
8239 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
8249 mutex_unlock(&fan_mutex
);
8252 vdbg_printk(TPACPI_DBG_FAN
,
8253 "fan control: set fan control register to 0x%02x\n",
8258 static int fan_set_disable(void)
8262 if (!fan_control_allowed
)
8265 if (mutex_lock_killable(&fan_mutex
))
8266 return -ERESTARTSYS
;
8269 switch (fan_control_access_mode
) {
8270 case TPACPI_FAN_WR_ACPI_FANS
:
8271 case TPACPI_FAN_WR_TPEC
:
8272 if (!acpi_ec_write(fan_status_offset
, 0x00))
8275 fan_control_desired_level
= 0;
8276 tp_features
.fan_ctrl_status_undef
= 0;
8280 case TPACPI_FAN_WR_ACPI_SFAN
:
8281 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
8284 fan_control_desired_level
= 0;
8292 vdbg_printk(TPACPI_DBG_FAN
,
8293 "fan control: set fan control register to 0\n");
8295 mutex_unlock(&fan_mutex
);
8299 static int fan_set_speed(int speed
)
8303 if (!fan_control_allowed
)
8306 if (mutex_lock_killable(&fan_mutex
))
8307 return -ERESTARTSYS
;
8310 switch (fan_control_access_mode
) {
8311 case TPACPI_FAN_WR_ACPI_FANS
:
8312 if (speed
>= 0 && speed
<= 65535) {
8313 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
8314 speed
, speed
, speed
))
8324 mutex_unlock(&fan_mutex
);
8328 static void fan_watchdog_reset(void)
8330 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
8333 if (fan_watchdog_maxinterval
> 0 &&
8334 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
8335 mod_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
8336 msecs_to_jiffies(fan_watchdog_maxinterval
* 1000));
8338 cancel_delayed_work(&fan_watchdog_task
);
8341 static void fan_watchdog_fire(struct work_struct
*ignored
)
8345 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
8348 pr_notice("fan watchdog: enabling fan\n");
8349 rc
= fan_set_enable();
8351 pr_err("fan watchdog: error %d while enabling fan, "
8352 "will try again later...\n", -rc
);
8353 /* reschedule for later */
8354 fan_watchdog_reset();
8359 * SYSFS fan layout: hwmon compatible (device)
8362 * 0: "disengaged" mode
8364 * 2: native EC "auto" mode (recommended, hardware default)
8366 * pwm*: set speed in manual mode, ignored otherwise.
8367 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8370 * fan*_input: tachometer reading, RPM
8373 * SYSFS fan layout: extensions
8375 * fan_watchdog (driver):
8376 * fan watchdog interval in seconds, 0 disables (default), max 120
8379 /* sysfs fan pwm1_enable ----------------------------------------------- */
8380 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
8381 struct device_attribute
*attr
,
8387 res
= fan_get_status_safe(&status
);
8391 if (status
& TP_EC_FAN_FULLSPEED
) {
8393 } else if (status
& TP_EC_FAN_AUTO
) {
8398 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
8401 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
8402 struct device_attribute
*attr
,
8403 const char *buf
, size_t count
)
8408 if (parse_strtoul(buf
, 2, &t
))
8411 tpacpi_disclose_usertask("hwmon pwm1_enable",
8412 "set fan mode to %lu\n", t
);
8416 level
= TP_EC_FAN_FULLSPEED
;
8419 level
= TPACPI_FAN_LAST_LEVEL
;
8422 level
= TP_EC_FAN_AUTO
;
8425 /* reserved for software-controlled auto mode */
8431 res
= fan_set_level_safe(level
);
8437 fan_watchdog_reset();
8442 static DEVICE_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
8443 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
8445 /* sysfs fan pwm1 ------------------------------------------------------ */
8446 static ssize_t
fan_pwm1_show(struct device
*dev
,
8447 struct device_attribute
*attr
,
8453 res
= fan_get_status_safe(&status
);
8458 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
8459 status
= fan_control_desired_level
;
8464 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
8467 static ssize_t
fan_pwm1_store(struct device
*dev
,
8468 struct device_attribute
*attr
,
8469 const char *buf
, size_t count
)
8473 u8 status
, newlevel
;
8475 if (parse_strtoul(buf
, 255, &s
))
8478 tpacpi_disclose_usertask("hwmon pwm1",
8479 "set fan speed to %lu\n", s
);
8481 /* scale down from 0-255 to 0-7 */
8482 newlevel
= (s
>> 5) & 0x07;
8484 if (mutex_lock_killable(&fan_mutex
))
8485 return -ERESTARTSYS
;
8487 rc
= fan_get_status(&status
);
8488 if (!rc
&& (status
&
8489 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
8490 rc
= fan_set_level(newlevel
);
8494 fan_update_desired_level(newlevel
);
8495 fan_watchdog_reset();
8499 mutex_unlock(&fan_mutex
);
8500 return (rc
) ? rc
: count
;
8503 static DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, fan_pwm1_show
, fan_pwm1_store
);
8505 /* sysfs fan fan1_input ------------------------------------------------ */
8506 static ssize_t
fan_fan1_input_show(struct device
*dev
,
8507 struct device_attribute
*attr
,
8513 res
= fan_get_speed(&speed
);
8517 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
8520 static DEVICE_ATTR(fan1_input
, S_IRUGO
, fan_fan1_input_show
, NULL
);
8522 /* sysfs fan fan2_input ------------------------------------------------ */
8523 static ssize_t
fan_fan2_input_show(struct device
*dev
,
8524 struct device_attribute
*attr
,
8530 res
= fan2_get_speed(&speed
);
8534 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
8537 static DEVICE_ATTR(fan2_input
, S_IRUGO
, fan_fan2_input_show
, NULL
);
8539 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8540 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
8543 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
8546 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
8547 const char *buf
, size_t count
)
8551 if (parse_strtoul(buf
, 120, &t
))
8554 if (!fan_control_allowed
)
8557 fan_watchdog_maxinterval
= t
;
8558 fan_watchdog_reset();
8560 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
8565 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
8566 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
8568 /* --------------------------------------------------------------------- */
8569 static struct attribute
*fan_attributes
[] = {
8570 &dev_attr_pwm1_enable
.attr
, &dev_attr_pwm1
.attr
,
8571 &dev_attr_fan1_input
.attr
,
8572 NULL
, /* for fan2_input */
8576 static const struct attribute_group fan_attr_group
= {
8577 .attrs
= fan_attributes
,
8580 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8581 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8583 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8584 { .vendor = PCI_VENDOR_ID_IBM, \
8585 .bios = TPACPI_MATCH_ANY, \
8586 .ec = TPID(__id1, __id2), \
8587 .quirks = __quirks }
8589 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8590 { .vendor = PCI_VENDOR_ID_LENOVO, \
8591 .bios = TPACPI_MATCH_ANY, \
8592 .ec = TPID(__id1, __id2), \
8593 .quirks = __quirks }
8595 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
8596 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
8597 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
8598 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
8599 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
8600 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
8603 #undef TPACPI_FAN_QL
8604 #undef TPACPI_FAN_QI
8606 static int __init
fan_init(struct ibm_init_struct
*iibm
)
8609 unsigned long quirks
;
8611 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8612 "initializing fan subdriver\n");
8614 mutex_init(&fan_mutex
);
8615 fan_status_access_mode
= TPACPI_FAN_NONE
;
8616 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
8617 fan_control_commands
= 0;
8618 fan_watchdog_maxinterval
= 0;
8619 tp_features
.fan_ctrl_status_undef
= 0;
8620 tp_features
.second_fan
= 0;
8621 fan_control_desired_level
= 7;
8623 if (tpacpi_is_ibm()) {
8624 TPACPI_ACPIHANDLE_INIT(fans
);
8625 TPACPI_ACPIHANDLE_INIT(gfan
);
8626 TPACPI_ACPIHANDLE_INIT(sfan
);
8629 quirks
= tpacpi_check_quirks(fan_quirk_table
,
8630 ARRAY_SIZE(fan_quirk_table
));
8633 /* 570, 600e/x, 770e, 770x */
8634 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
8636 /* all other ThinkPads: note that even old-style
8637 * ThinkPad ECs supports the fan control register */
8638 if (likely(acpi_ec_read(fan_status_offset
,
8639 &fan_control_initial_status
))) {
8640 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
8641 if (quirks
& TPACPI_FAN_Q1
)
8643 if (quirks
& TPACPI_FAN_2FAN
) {
8644 tp_features
.second_fan
= 1;
8645 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8646 "secondary fan support enabled\n");
8649 pr_err("ThinkPad ACPI EC access misbehaving, "
8650 "fan status and control unavailable\n");
8657 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
8658 fan_control_commands
|=
8659 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
8662 /* gfan without sfan means no fan control */
8663 /* all other models implement TP EC 0x2f control */
8667 fan_control_access_mode
=
8668 TPACPI_FAN_WR_ACPI_FANS
;
8669 fan_control_commands
|=
8670 TPACPI_FAN_CMD_SPEED
|
8671 TPACPI_FAN_CMD_LEVEL
|
8672 TPACPI_FAN_CMD_ENABLE
;
8674 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
8675 fan_control_commands
|=
8676 TPACPI_FAN_CMD_LEVEL
|
8677 TPACPI_FAN_CMD_ENABLE
;
8682 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8683 "fan is %s, modes %d, %d\n",
8684 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
8685 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
8686 fan_status_access_mode
, fan_control_access_mode
);
8688 /* fan control master switch */
8689 if (!fan_control_allowed
) {
8690 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
8691 fan_control_commands
= 0;
8692 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8693 "fan control features disabled by parameter\n");
8696 /* update fan_control_desired_level */
8697 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
8698 fan_get_status_safe(NULL
);
8700 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
8701 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
8702 if (tp_features
.second_fan
) {
8703 /* attach second fan tachometer */
8704 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
8705 &dev_attr_fan2_input
.attr
;
8707 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
8712 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
8713 &driver_attr_fan_watchdog
);
8715 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
8724 static void fan_exit(void)
8726 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
8727 "cancelling any pending fan watchdog tasks\n");
8729 /* FIXME: can we really do this unconditionally? */
8730 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
8731 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
8732 &driver_attr_fan_watchdog
);
8734 cancel_delayed_work(&fan_watchdog_task
);
8735 flush_workqueue(tpacpi_wq
);
8738 static void fan_suspend(void)
8742 if (!fan_control_allowed
)
8745 /* Store fan status in cache */
8746 fan_control_resume_level
= 0;
8747 rc
= fan_get_status_safe(&fan_control_resume_level
);
8749 pr_notice("failed to read fan level for later "
8750 "restore during resume: %d\n", rc
);
8752 /* if it is undefined, don't attempt to restore it.
8754 if (tp_features
.fan_ctrl_status_undef
)
8755 fan_control_resume_level
= 0;
8758 static void fan_resume(void)
8760 u8 current_level
= 7;
8761 bool do_set
= false;
8764 /* DSDT *always* updates status on resume */
8765 tp_features
.fan_ctrl_status_undef
= 0;
8767 if (!fan_control_allowed
||
8768 !fan_control_resume_level
||
8769 (fan_get_status_safe(¤t_level
) < 0))
8772 switch (fan_control_access_mode
) {
8773 case TPACPI_FAN_WR_ACPI_SFAN
:
8774 /* never decrease fan level */
8775 do_set
= (fan_control_resume_level
> current_level
);
8777 case TPACPI_FAN_WR_ACPI_FANS
:
8778 case TPACPI_FAN_WR_TPEC
:
8779 /* never decrease fan level, scale is:
8780 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8782 * We expect the firmware to set either 7 or AUTO, but we
8783 * handle FULLSPEED out of paranoia.
8785 * So, we can safely only restore FULLSPEED or 7, anything
8786 * else could slow the fan. Restoring AUTO is useless, at
8787 * best that's exactly what the DSDT already set (it is the
8790 * Always keep in mind that the DSDT *will* have set the
8791 * fans to what the vendor supposes is the best level. We
8792 * muck with it only to speed the fan up.
8794 if (fan_control_resume_level
!= 7 &&
8795 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
8798 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
8799 (current_level
!= fan_control_resume_level
);
8805 pr_notice("restoring fan level to 0x%02x\n",
8806 fan_control_resume_level
);
8807 rc
= fan_set_level_safe(fan_control_resume_level
);
8809 pr_notice("failed to restore fan level: %d\n", rc
);
8813 static int fan_read(struct seq_file
*m
)
8817 unsigned int speed
= 0;
8819 switch (fan_status_access_mode
) {
8820 case TPACPI_FAN_RD_ACPI_GFAN
:
8821 /* 570, 600e/x, 770e, 770x */
8822 rc
= fan_get_status_safe(&status
);
8826 seq_printf(m
, "status:\t\t%s\n"
8828 (status
!= 0) ? "enabled" : "disabled", status
);
8831 case TPACPI_FAN_RD_TPEC
:
8832 /* all except 570, 600e/x, 770e, 770x */
8833 rc
= fan_get_status_safe(&status
);
8837 seq_printf(m
, "status:\t\t%s\n",
8838 (status
!= 0) ? "enabled" : "disabled");
8840 rc
= fan_get_speed(&speed
);
8844 seq_printf(m
, "speed:\t\t%d\n", speed
);
8846 if (status
& TP_EC_FAN_FULLSPEED
)
8847 /* Disengaged mode takes precedence */
8848 seq_printf(m
, "level:\t\tdisengaged\n");
8849 else if (status
& TP_EC_FAN_AUTO
)
8850 seq_printf(m
, "level:\t\tauto\n");
8852 seq_printf(m
, "level:\t\t%d\n", status
);
8855 case TPACPI_FAN_NONE
:
8857 seq_printf(m
, "status:\t\tnot supported\n");
8860 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
8861 seq_printf(m
, "commands:\tlevel <level>");
8863 switch (fan_control_access_mode
) {
8864 case TPACPI_FAN_WR_ACPI_SFAN
:
8865 seq_printf(m
, " (<level> is 0-7)\n");
8869 seq_printf(m
, " (<level> is 0-7, "
8870 "auto, disengaged, full-speed)\n");
8875 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
8876 seq_printf(m
, "commands:\tenable, disable\n"
8877 "commands:\twatchdog <timeout> (<timeout> "
8878 "is 0 (off), 1-120 (seconds))\n");
8880 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
8881 seq_printf(m
, "commands:\tspeed <speed>"
8882 " (<speed> is 0-65535)\n");
8887 static int fan_write_cmd_level(const char *cmd
, int *rc
)
8891 if (strlencmp(cmd
, "level auto") == 0)
8892 level
= TP_EC_FAN_AUTO
;
8893 else if ((strlencmp(cmd
, "level disengaged") == 0) |
8894 (strlencmp(cmd
, "level full-speed") == 0))
8895 level
= TP_EC_FAN_FULLSPEED
;
8896 else if (sscanf(cmd
, "level %d", &level
) != 1)
8899 *rc
= fan_set_level_safe(level
);
8901 pr_err("level command accepted for unsupported access mode %d\n",
8902 fan_control_access_mode
);
8904 tpacpi_disclose_usertask("procfs fan",
8905 "set level to %d\n", level
);
8910 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
8912 if (strlencmp(cmd
, "enable") != 0)
8915 *rc
= fan_set_enable();
8917 pr_err("enable command accepted for unsupported access mode %d\n",
8918 fan_control_access_mode
);
8920 tpacpi_disclose_usertask("procfs fan", "enable\n");
8925 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
8927 if (strlencmp(cmd
, "disable") != 0)
8930 *rc
= fan_set_disable();
8932 pr_err("disable command accepted for unsupported access mode %d\n",
8933 fan_control_access_mode
);
8935 tpacpi_disclose_usertask("procfs fan", "disable\n");
8940 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
8945 * Support speed <low> <medium> <high> ? */
8947 if (sscanf(cmd
, "speed %d", &speed
) != 1)
8950 *rc
= fan_set_speed(speed
);
8952 pr_err("speed command accepted for unsupported access mode %d\n",
8953 fan_control_access_mode
);
8955 tpacpi_disclose_usertask("procfs fan",
8956 "set speed to %d\n", speed
);
8961 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
8965 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
8968 if (interval
< 0 || interval
> 120)
8971 fan_watchdog_maxinterval
= interval
;
8972 tpacpi_disclose_usertask("procfs fan",
8973 "set watchdog timer to %d\n",
8980 static int fan_write(char *buf
)
8985 while (!rc
&& (cmd
= next_cmd(&buf
))) {
8986 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
8987 fan_write_cmd_level(cmd
, &rc
)) &&
8988 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
8989 (fan_write_cmd_enable(cmd
, &rc
) ||
8990 fan_write_cmd_disable(cmd
, &rc
) ||
8991 fan_write_cmd_watchdog(cmd
, &rc
))) &&
8992 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
8993 fan_write_cmd_speed(cmd
, &rc
))
8997 fan_watchdog_reset();
9003 static struct ibm_struct fan_driver_data
= {
9008 .suspend
= fan_suspend
,
9009 .resume
= fan_resume
,
9012 /*************************************************************************
9013 * Mute LED subdriver
9017 struct tp_led_table
{
9024 static struct tp_led_table led_tables
[] = {
9025 [TPACPI_LED_MUTE
] = {
9030 [TPACPI_LED_MICMUTE
] = {
9037 static int mute_led_on_off(struct tp_led_table
*t
, bool state
)
9042 if (ACPI_FAILURE(acpi_get_handle(hkey_handle
, t
->name
, &temp
))) {
9043 pr_warn("Thinkpad ACPI has no %s interface.\n", t
->name
);
9047 if (!acpi_evalf(hkey_handle
, &output
, t
->name
, "dd",
9048 state
? t
->on_value
: t
->off_value
))
9055 int tpacpi_led_set(int whichled
, bool on
)
9057 struct tp_led_table
*t
;
9059 if (whichled
< 0 || whichled
>= TPACPI_LED_MAX
)
9062 t
= &led_tables
[whichled
];
9063 if (t
->state
< 0 || t
->state
== on
)
9065 return mute_led_on_off(t
, on
);
9067 EXPORT_SYMBOL_GPL(tpacpi_led_set
);
9069 static int mute_led_init(struct ibm_init_struct
*iibm
)
9074 for (i
= 0; i
< TPACPI_LED_MAX
; i
++) {
9075 struct tp_led_table
*t
= &led_tables
[i
];
9076 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle
, t
->name
, &temp
)))
9077 mute_led_on_off(t
, false);
9084 static void mute_led_exit(void)
9088 for (i
= 0; i
< TPACPI_LED_MAX
; i
++)
9089 tpacpi_led_set(i
, false);
9092 static void mute_led_resume(void)
9096 for (i
= 0; i
< TPACPI_LED_MAX
; i
++) {
9097 struct tp_led_table
*t
= &led_tables
[i
];
9099 mute_led_on_off(t
, t
->state
);
9103 static struct ibm_struct mute_led_driver_data
= {
9105 .exit
= mute_led_exit
,
9106 .resume
= mute_led_resume
,
9109 /****************************************************************************
9110 ****************************************************************************
9114 ****************************************************************************
9115 ****************************************************************************/
9118 * HKEY event callout for other subdrivers go here
9119 * (yes, it is ugly, but it is quick, safe, and gets the job done
9121 static void tpacpi_driver_event(const unsigned int hkey_event
)
9123 if (ibm_backlight_device
) {
9124 switch (hkey_event
) {
9125 case TP_HKEY_EV_BRGHT_UP
:
9126 case TP_HKEY_EV_BRGHT_DOWN
:
9127 tpacpi_brightness_notify_change();
9131 switch (hkey_event
) {
9132 case TP_HKEY_EV_VOL_UP
:
9133 case TP_HKEY_EV_VOL_DOWN
:
9134 case TP_HKEY_EV_VOL_MUTE
:
9135 volume_alsa_notify_change();
9138 if (tp_features
.kbdlight
&& hkey_event
== TP_HKEY_EV_KBD_LIGHT
) {
9139 enum led_brightness brightness
;
9141 mutex_lock(&kbdlight_mutex
);
9144 * Check the brightness actually changed, setting the brightness
9145 * through kbdlight_set_level() also triggers this event.
9147 brightness
= kbdlight_sysfs_get(NULL
);
9148 if (kbdlight_brightness
!= brightness
) {
9149 kbdlight_brightness
= brightness
;
9150 led_classdev_notify_brightness_hw_changed(
9151 &tpacpi_led_kbdlight
.led_classdev
, brightness
);
9154 mutex_unlock(&kbdlight_mutex
);
9158 static void hotkey_driver_event(const unsigned int scancode
)
9160 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
9163 /* sysfs name ---------------------------------------------------------- */
9164 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
9165 struct device_attribute
*attr
,
9168 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
9171 static DEVICE_ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
9173 /* --------------------------------------------------------------------- */
9176 static struct proc_dir_entry
*proc_dir
;
9179 * Module and infrastructure proble, init and exit handling
9182 static bool force_load
;
9184 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9185 static const char * __init
str_supported(int is_supported
)
9187 static char text_unsupported
[] __initdata
= "not supported";
9189 return (is_supported
) ? &text_unsupported
[4] : &text_unsupported
[0];
9191 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9193 static void ibm_exit(struct ibm_struct
*ibm
)
9195 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
9197 list_del_init(&ibm
->all_drivers
);
9199 if (ibm
->flags
.acpi_notify_installed
) {
9200 dbg_printk(TPACPI_DBG_EXIT
,
9201 "%s: acpi_remove_notify_handler\n", ibm
->name
);
9203 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
9205 dispatch_acpi_notify
);
9206 ibm
->flags
.acpi_notify_installed
= 0;
9209 if (ibm
->flags
.proc_created
) {
9210 dbg_printk(TPACPI_DBG_EXIT
,
9211 "%s: remove_proc_entry\n", ibm
->name
);
9212 remove_proc_entry(ibm
->name
, proc_dir
);
9213 ibm
->flags
.proc_created
= 0;
9216 if (ibm
->flags
.acpi_driver_registered
) {
9217 dbg_printk(TPACPI_DBG_EXIT
,
9218 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
9220 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
9221 kfree(ibm
->acpi
->driver
);
9222 ibm
->acpi
->driver
= NULL
;
9223 ibm
->flags
.acpi_driver_registered
= 0;
9226 if (ibm
->flags
.init_called
&& ibm
->exit
) {
9228 ibm
->flags
.init_called
= 0;
9231 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
9234 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
9237 struct ibm_struct
*ibm
= iibm
->data
;
9238 struct proc_dir_entry
*entry
;
9240 BUG_ON(ibm
== NULL
);
9242 INIT_LIST_HEAD(&ibm
->all_drivers
);
9244 if (ibm
->flags
.experimental
&& !experimental
)
9247 dbg_printk(TPACPI_DBG_INIT
,
9248 "probing for %s\n", ibm
->name
);
9251 ret
= iibm
->init(iibm
);
9253 return 0; /* probe failed */
9257 ibm
->flags
.init_called
= 1;
9261 if (ibm
->acpi
->hid
) {
9262 ret
= register_tpacpi_subdriver(ibm
);
9267 if (ibm
->acpi
->notify
) {
9268 ret
= setup_acpi_notify(ibm
);
9269 if (ret
== -ENODEV
) {
9270 pr_notice("disabling subdriver %s\n",
9280 dbg_printk(TPACPI_DBG_INIT
,
9281 "%s installed\n", ibm
->name
);
9284 umode_t mode
= iibm
->base_procfs_mode
;
9290 entry
= proc_create_data(ibm
->name
, mode
, proc_dir
,
9291 &dispatch_proc_fops
, ibm
);
9293 pr_err("unable to create proc entry %s\n", ibm
->name
);
9297 ibm
->flags
.proc_created
= 1;
9300 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
9305 dbg_printk(TPACPI_DBG_INIT
,
9306 "%s: at error exit path with result %d\n",
9310 return (ret
< 0) ? ret
: 0;
9315 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
9317 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
9320 static bool __pure __init
tpacpi_is_valid_fw_id(const char * const s
,
9324 * Most models: xxyTkkWW (#.##c)
9325 * Ancient 570/600 and -SL lacks (#.##c)
9327 if (s
&& strlen(s
) >= 8 &&
9328 tpacpi_is_fw_digit(s
[0]) &&
9329 tpacpi_is_fw_digit(s
[1]) &&
9331 (s
[3] == 'T' || s
[3] == 'N') &&
9332 tpacpi_is_fw_digit(s
[4]) &&
9333 tpacpi_is_fw_digit(s
[5]))
9336 /* New models: xxxyTkkW (#.##c); T550 and some others */
9337 return s
&& strlen(s
) >= 8 &&
9338 tpacpi_is_fw_digit(s
[0]) &&
9339 tpacpi_is_fw_digit(s
[1]) &&
9340 tpacpi_is_fw_digit(s
[2]) &&
9342 (s
[4] == 'T' || s
[4] == 'N') &&
9343 tpacpi_is_fw_digit(s
[5]) &&
9344 tpacpi_is_fw_digit(s
[6]);
9347 /* returns 0 - probe ok, or < 0 - probe error.
9348 * Probe ok doesn't mean thinkpad found.
9349 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9350 static int __must_check __init
get_thinkpad_model_data(
9351 struct thinkpad_id_data
*tp
)
9353 const struct dmi_device
*dev
= NULL
;
9354 char ec_fw_string
[18];
9360 memset(tp
, 0, sizeof(*tp
));
9362 if (dmi_name_in_vendors("IBM"))
9363 tp
->vendor
= PCI_VENDOR_ID_IBM
;
9364 else if (dmi_name_in_vendors("LENOVO"))
9365 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
9369 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
9370 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
9371 if (s
&& !tp
->bios_version_str
)
9374 /* Really ancient ThinkPad 240X will fail this, which is fine */
9375 if (!(tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E') ||
9376 tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'C')))
9379 tp
->bios_model
= tp
->bios_version_str
[0]
9380 | (tp
->bios_version_str
[1] << 8);
9381 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
9382 | tp
->bios_version_str
[5];
9385 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9386 * X32 or newer, all Z series; Some models must have an
9387 * up-to-date BIOS or they will not be detected.
9389 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9391 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
9392 if (sscanf(dev
->name
,
9393 "IBM ThinkPad Embedded Controller -[%17c",
9394 ec_fw_string
) == 1) {
9395 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
9396 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
9398 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
9399 if (!tp
->ec_version_str
)
9402 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
9403 tp
->ec_model
= ec_fw_string
[0]
9404 | (ec_fw_string
[1] << 8);
9405 tp
->ec_release
= (ec_fw_string
[4] << 8)
9408 pr_notice("ThinkPad firmware release %s "
9409 "doesn't match the known patterns\n",
9411 pr_notice("please report this to %s\n",
9418 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
9419 if (s
&& !(strncasecmp(s
, "ThinkPad", 8) && strncasecmp(s
, "Lenovo", 6))) {
9420 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
9424 s
= dmi_get_system_info(DMI_BIOS_VENDOR
);
9425 if (s
&& !(strncasecmp(s
, "Lenovo", 6))) {
9426 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
9432 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
9433 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
9434 if (s
&& !tp
->nummodel_str
)
9440 static int __init
probe_for_thinkpad(void)
9447 /* It would be dangerous to run the driver in this case */
9448 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9452 * Non-ancient models have better DMI tagging, but very old models
9453 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9455 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
) ||
9456 (thinkpad_id
.ec_model
!= 0) ||
9457 tpacpi_is_fw_known();
9459 /* The EC handler is required */
9460 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID
, &ec_handle
);
9463 pr_err("Not yet supported ThinkPad detected!\n");
9467 if (!is_thinkpad
&& !force_load
)
9473 static void __init
thinkpad_acpi_init_banner(void)
9475 pr_info("%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
9476 pr_info("%s\n", TPACPI_URL
);
9478 pr_info("ThinkPad BIOS %s, EC %s\n",
9479 (thinkpad_id
.bios_version_str
) ?
9480 thinkpad_id
.bios_version_str
: "unknown",
9481 (thinkpad_id
.ec_version_str
) ?
9482 thinkpad_id
.ec_version_str
: "unknown");
9484 BUG_ON(!thinkpad_id
.vendor
);
9486 if (thinkpad_id
.model_str
)
9487 pr_info("%s %s, model %s\n",
9488 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
9489 "IBM" : ((thinkpad_id
.vendor
==
9490 PCI_VENDOR_ID_LENOVO
) ?
9491 "Lenovo" : "Unknown vendor"),
9492 thinkpad_id
.model_str
,
9493 (thinkpad_id
.nummodel_str
) ?
9494 thinkpad_id
.nummodel_str
: "unknown");
9497 /* Module init, exit, parameters */
9499 static struct ibm_init_struct ibms_init
[] __initdata
= {
9501 .data
= &thinkpad_acpi_driver_data
,
9504 .init
= hotkey_init
,
9505 .data
= &hotkey_driver_data
,
9508 .init
= bluetooth_init
,
9509 .data
= &bluetooth_driver_data
,
9513 .data
= &wan_driver_data
,
9517 .data
= &uwb_driver_data
,
9519 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9522 .base_procfs_mode
= S_IRUSR
,
9523 .data
= &video_driver_data
,
9527 .init
= kbdlight_init
,
9528 .data
= &kbdlight_driver_data
,
9532 .data
= &light_driver_data
,
9536 .data
= &cmos_driver_data
,
9540 .data
= &led_driver_data
,
9544 .data
= &beep_driver_data
,
9547 .init
= thermal_init
,
9548 .data
= &thermal_driver_data
,
9551 .init
= brightness_init
,
9552 .data
= &brightness_driver_data
,
9555 .init
= volume_init
,
9556 .data
= &volume_driver_data
,
9560 .data
= &fan_driver_data
,
9563 .init
= mute_led_init
,
9564 .data
= &mute_led_driver_data
,
9568 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
9571 struct ibm_struct
*ibm
;
9573 if (!kp
|| !kp
->name
|| !val
)
9576 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
9577 ibm
= ibms_init
[i
].data
;
9578 WARN_ON(ibm
== NULL
);
9580 if (!ibm
|| !ibm
->name
)
9583 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
9584 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
9586 strcpy(ibms_init
[i
].param
, val
);
9587 strcat(ibms_init
[i
].param
, ",");
9595 module_param(experimental
, int, 0444);
9596 MODULE_PARM_DESC(experimental
,
9597 "Enables experimental features when non-zero");
9599 module_param_named(debug
, dbg_level
, uint
, 0);
9600 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
9602 module_param(force_load
, bool, 0444);
9603 MODULE_PARM_DESC(force_load
,
9604 "Attempts to load the driver even on a "
9605 "mis-identified ThinkPad when true");
9607 module_param_named(fan_control
, fan_control_allowed
, bool, 0444);
9608 MODULE_PARM_DESC(fan_control
,
9609 "Enables setting fan parameters features when true");
9611 module_param_named(brightness_mode
, brightness_mode
, uint
, 0444);
9612 MODULE_PARM_DESC(brightness_mode
,
9613 "Selects brightness control strategy: "
9614 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9616 module_param(brightness_enable
, uint
, 0444);
9617 MODULE_PARM_DESC(brightness_enable
,
9618 "Enables backlight control when 1, disables when 0");
9620 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9621 module_param_named(volume_mode
, volume_mode
, uint
, 0444);
9622 MODULE_PARM_DESC(volume_mode
,
9623 "Selects volume control strategy: "
9624 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9626 module_param_named(volume_capabilities
, volume_capabilities
, uint
, 0444);
9627 MODULE_PARM_DESC(volume_capabilities
,
9628 "Selects the mixer capabilites: "
9629 "0=auto, 1=volume and mute, 2=mute only");
9631 module_param_named(volume_control
, volume_control_allowed
, bool, 0444);
9632 MODULE_PARM_DESC(volume_control
,
9633 "Enables software override for the console audio "
9634 "control when true");
9636 module_param_named(software_mute
, software_mute_requested
, bool, 0444);
9637 MODULE_PARM_DESC(software_mute
,
9638 "Request full software mute control");
9640 /* ALSA module API parameters */
9641 module_param_named(index
, alsa_index
, int, 0444);
9642 MODULE_PARM_DESC(index
, "ALSA index for the ACPI EC Mixer");
9643 module_param_named(id
, alsa_id
, charp
, 0444);
9644 MODULE_PARM_DESC(id
, "ALSA id for the ACPI EC Mixer");
9645 module_param_named(enable
, alsa_enable
, bool, 0444);
9646 MODULE_PARM_DESC(enable
, "Enable the ALSA interface for the ACPI EC Mixer");
9647 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9649 #define TPACPI_PARAM(feature) \
9650 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9651 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
9652 "at module load, see documentation")
9654 TPACPI_PARAM(hotkey
);
9655 TPACPI_PARAM(bluetooth
);
9656 TPACPI_PARAM(video
);
9657 TPACPI_PARAM(light
);
9661 TPACPI_PARAM(brightness
);
9662 TPACPI_PARAM(volume
);
9665 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9666 module_param(dbg_wlswemul
, uint
, 0444);
9667 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
9668 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
9669 MODULE_PARM_DESC(wlsw_state
,
9670 "Initial state of the emulated WLSW switch");
9672 module_param(dbg_bluetoothemul
, uint
, 0444);
9673 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
9674 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
9675 MODULE_PARM_DESC(bluetooth_state
,
9676 "Initial state of the emulated bluetooth switch");
9678 module_param(dbg_wwanemul
, uint
, 0444);
9679 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
9680 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
9681 MODULE_PARM_DESC(wwan_state
,
9682 "Initial state of the emulated WWAN switch");
9684 module_param(dbg_uwbemul
, uint
, 0444);
9685 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
9686 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
9687 MODULE_PARM_DESC(uwb_state
,
9688 "Initial state of the emulated UWB switch");
9691 static void thinkpad_acpi_module_exit(void)
9693 struct ibm_struct
*ibm
, *itmp
;
9695 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
9697 list_for_each_entry_safe_reverse(ibm
, itmp
,
9698 &tpacpi_all_drivers
,
9703 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
9705 if (tpacpi_inputdev
) {
9706 if (tp_features
.input_device_registered
)
9707 input_unregister_device(tpacpi_inputdev
);
9709 input_free_device(tpacpi_inputdev
);
9710 kfree(hotkey_keycode_map
);
9714 hwmon_device_unregister(tpacpi_hwmon
);
9716 if (tp_features
.sensors_pdev_attrs_registered
)
9717 device_remove_file(&tpacpi_sensors_pdev
->dev
, &dev_attr_name
);
9718 if (tpacpi_sensors_pdev
)
9719 platform_device_unregister(tpacpi_sensors_pdev
);
9721 platform_device_unregister(tpacpi_pdev
);
9723 if (tp_features
.sensors_pdrv_attrs_registered
)
9724 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
9725 if (tp_features
.platform_drv_attrs_registered
)
9726 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
9728 if (tp_features
.sensors_pdrv_registered
)
9729 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
9731 if (tp_features
.platform_drv_registered
)
9732 platform_driver_unregister(&tpacpi_pdriver
);
9735 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
9738 destroy_workqueue(tpacpi_wq
);
9740 kfree(thinkpad_id
.bios_version_str
);
9741 kfree(thinkpad_id
.ec_version_str
);
9742 kfree(thinkpad_id
.model_str
);
9743 kfree(thinkpad_id
.nummodel_str
);
9747 static int __init
thinkpad_acpi_module_init(void)
9751 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
9753 /* Driver-level probe */
9755 ret
= get_thinkpad_model_data(&thinkpad_id
);
9757 pr_err("unable to get DMI data: %d\n", ret
);
9758 thinkpad_acpi_module_exit();
9761 ret
= probe_for_thinkpad();
9763 thinkpad_acpi_module_exit();
9767 /* Driver initialization */
9769 thinkpad_acpi_init_banner();
9770 tpacpi_check_outdated_fw();
9772 TPACPI_ACPIHANDLE_INIT(ecrd
);
9773 TPACPI_ACPIHANDLE_INIT(ecwr
);
9775 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
9777 thinkpad_acpi_module_exit();
9781 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
9783 pr_err("unable to create proc dir " TPACPI_PROC_DIR
"\n");
9784 thinkpad_acpi_module_exit();
9788 ret
= platform_driver_register(&tpacpi_pdriver
);
9790 pr_err("unable to register main platform driver\n");
9791 thinkpad_acpi_module_exit();
9794 tp_features
.platform_drv_registered
= 1;
9796 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
9798 pr_err("unable to register hwmon platform driver\n");
9799 thinkpad_acpi_module_exit();
9802 tp_features
.sensors_pdrv_registered
= 1;
9804 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
9806 tp_features
.platform_drv_attrs_registered
= 1;
9807 ret
= tpacpi_create_driver_attributes(
9808 &tpacpi_hwmon_pdriver
.driver
);
9811 pr_err("unable to create sysfs driver attributes\n");
9812 thinkpad_acpi_module_exit();
9815 tp_features
.sensors_pdrv_attrs_registered
= 1;
9818 /* Device initialization */
9819 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
9821 if (IS_ERR(tpacpi_pdev
)) {
9822 ret
= PTR_ERR(tpacpi_pdev
);
9824 pr_err("unable to register platform device\n");
9825 thinkpad_acpi_module_exit();
9828 tpacpi_sensors_pdev
= platform_device_register_simple(
9829 TPACPI_HWMON_DRVR_NAME
,
9831 if (IS_ERR(tpacpi_sensors_pdev
)) {
9832 ret
= PTR_ERR(tpacpi_sensors_pdev
);
9833 tpacpi_sensors_pdev
= NULL
;
9834 pr_err("unable to register hwmon platform device\n");
9835 thinkpad_acpi_module_exit();
9838 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
, &dev_attr_name
);
9840 pr_err("unable to create sysfs hwmon device attributes\n");
9841 thinkpad_acpi_module_exit();
9844 tp_features
.sensors_pdev_attrs_registered
= 1;
9845 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
9846 if (IS_ERR(tpacpi_hwmon
)) {
9847 ret
= PTR_ERR(tpacpi_hwmon
);
9848 tpacpi_hwmon
= NULL
;
9849 pr_err("unable to register hwmon device\n");
9850 thinkpad_acpi_module_exit();
9853 mutex_init(&tpacpi_inputdev_send_mutex
);
9854 tpacpi_inputdev
= input_allocate_device();
9855 if (!tpacpi_inputdev
) {
9856 thinkpad_acpi_module_exit();
9859 /* Prepare input device, but don't register */
9860 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
9861 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
9862 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
9863 tpacpi_inputdev
->id
.vendor
= thinkpad_id
.vendor
;
9864 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
9865 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
9866 tpacpi_inputdev
->dev
.parent
= &tpacpi_pdev
->dev
;
9869 /* Init subdriver dependencies */
9870 tpacpi_detect_brightness_capabilities();
9872 /* Init subdrivers */
9873 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
9874 ret
= ibm_init(&ibms_init
[i
]);
9875 if (ret
>= 0 && *ibms_init
[i
].param
)
9876 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
9878 thinkpad_acpi_module_exit();
9883 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
9885 ret
= input_register_device(tpacpi_inputdev
);
9887 pr_err("unable to register input device\n");
9888 thinkpad_acpi_module_exit();
9891 tp_features
.input_device_registered
= 1;
9897 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
9900 * This will autoload the driver in almost every ThinkPad
9901 * in widespread use.
9903 * Only _VERY_ old models, like the 240, 240x and 570 lack
9904 * the HKEY event interface.
9906 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
9909 * DMI matching for module autoloading
9911 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9912 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9914 * Only models listed in thinkwiki will be supported, so add yours
9915 * if it is not there yet.
9917 #define IBM_BIOS_MODULE_ALIAS(__type) \
9918 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9920 /* Ancient thinkpad BIOSes have to be identified by
9921 * BIOS type or model number, and there are far less
9922 * BIOS types than model numbers... */
9923 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9925 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9926 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9927 MODULE_DESCRIPTION(TPACPI_DESC
);
9928 MODULE_VERSION(TPACPI_VERSION
);
9929 MODULE_LICENSE("GPL");
9931 module_init(thinkpad_acpi_module_init
);
9932 module_exit(thinkpad_acpi_module_exit
);