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 0x030000
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 */
217 /* Further user-interface events */
218 TP_HKEY_EV_PALM_DETECTED
= 0x60b0, /* palm hoveres keyboard */
219 TP_HKEY_EV_PALM_UNDETECTED
= 0x60b1, /* palm removed */
222 TP_HKEY_EV_RFKILL_CHANGED
= 0x7000, /* rfkill switch changed */
225 /****************************************************************************
229 #define TPACPI_NAME "thinkpad"
230 #define TPACPI_DESC "ThinkPad ACPI Extras"
231 #define TPACPI_FILE TPACPI_NAME "_acpi"
232 #define TPACPI_URL "http://ibm-acpi.sf.net/"
233 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
235 #define TPACPI_PROC_DIR "ibm"
236 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
237 #define TPACPI_DRVR_NAME TPACPI_FILE
238 #define TPACPI_DRVR_SHORTNAME "tpacpi"
239 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
241 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
242 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
244 #define TPACPI_MAX_ACPI_ARGS 3
246 /* Debugging printk groups */
247 #define TPACPI_DBG_ALL 0xffff
248 #define TPACPI_DBG_DISCLOSETASK 0x8000
249 #define TPACPI_DBG_INIT 0x0001
250 #define TPACPI_DBG_EXIT 0x0002
251 #define TPACPI_DBG_RFKILL 0x0004
252 #define TPACPI_DBG_HKEY 0x0008
253 #define TPACPI_DBG_FAN 0x0010
254 #define TPACPI_DBG_BRGHT 0x0020
255 #define TPACPI_DBG_MIXER 0x0040
257 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
258 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
259 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
262 /****************************************************************************
263 * Driver-wide structs and misc. variables
268 struct tp_acpi_drv_struct
{
269 const struct acpi_device_id
*hid
;
270 struct acpi_driver
*driver
;
272 void (*notify
) (struct ibm_struct
*, u32
);
275 struct acpi_device
*device
;
281 int (*read
) (struct seq_file
*);
282 int (*write
) (char *);
284 void (*resume
) (void);
285 void (*suspend
) (void);
286 void (*shutdown
) (void);
288 struct list_head all_drivers
;
290 struct tp_acpi_drv_struct
*acpi
;
293 u8 acpi_driver_registered
:1;
294 u8 acpi_notify_installed
:1;
301 struct ibm_init_struct
{
304 int (*init
) (struct ibm_init_struct
*);
305 umode_t base_procfs_mode
;
306 struct ibm_struct
*data
;
315 TP_HOTKEY_TABLET_NONE
= 0,
316 TP_HOTKEY_TABLET_USES_MHKG
,
317 TP_HOTKEY_TABLET_USES_GMMS
,
322 u32 bright_acpimode
:1;
326 u32 fan_ctrl_status_undef
:1;
328 u32 beep_needs_two_args
:1;
329 u32 mixer_no_level_control
:1;
330 u32 input_device_registered
:1;
331 u32 platform_drv_registered
:1;
332 u32 platform_drv_attrs_registered
:1;
333 u32 sensors_pdrv_registered
:1;
334 u32 sensors_pdrv_attrs_registered
:1;
335 u32 sensors_pdev_attrs_registered
:1;
336 u32 hotkey_poll_active
:1;
337 u32 has_adaptive_kbd
:1;
341 u16 hotkey_mask_ff
:1;
342 u16 volume_ctrl_forbidden
:1;
345 struct thinkpad_id_data
{
346 unsigned int vendor
; /* ThinkPad vendor:
347 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
349 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
350 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
352 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
354 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
357 char *model_str
; /* ThinkPad T43 */
358 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
360 static struct thinkpad_id_data thinkpad_id
;
363 TPACPI_LIFE_INIT
= 0,
368 static int experimental
;
369 static u32 dbg_level
;
371 static struct workqueue_struct
*tpacpi_wq
;
379 /* tpacpi LED class */
380 struct tpacpi_led_classdev
{
381 struct led_classdev led_classdev
;
385 /* brightness level capabilities */
386 static unsigned int bright_maxlvl
; /* 0 = unknown */
388 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
389 static int dbg_wlswemul
;
390 static bool tpacpi_wlsw_emulstate
;
391 static int dbg_bluetoothemul
;
392 static bool tpacpi_bluetooth_emulstate
;
393 static int dbg_wwanemul
;
394 static bool tpacpi_wwan_emulstate
;
395 static int dbg_uwbemul
;
396 static bool tpacpi_uwb_emulstate
;
400 /*************************************************************************
404 #define dbg_printk(a_dbg_level, format, arg...) \
406 if (dbg_level & (a_dbg_level)) \
407 printk(KERN_DEBUG pr_fmt("%s: " format), \
411 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
412 #define vdbg_printk dbg_printk
413 static const char *str_supported(int is_supported
);
415 static inline const char *str_supported(int is_supported
) { return ""; }
416 #define vdbg_printk(a_dbg_level, format, arg...) \
417 do { if (0) no_printk(format, ##arg); } while (0)
420 static void tpacpi_log_usertask(const char * const what
)
422 printk(KERN_DEBUG
pr_fmt("%s: access by process with PID %d\n"),
423 what
, task_tgid_vnr(current
));
426 #define tpacpi_disclose_usertask(what, format, arg...) \
428 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
429 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
430 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
431 what, task_tgid_vnr(current), ## arg); \
436 * Quirk handling helpers
438 * ThinkPad IDs and versions seen in the field so far
439 * are two-characters from the set [0-9A-Z], i.e. base 36.
441 * We use values well outside that range as specials.
444 #define TPACPI_MATCH_ANY 0xffffU
445 #define TPACPI_MATCH_UNKNOWN 0U
447 /* TPID('1', 'Y') == 0x5931 */
448 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
450 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
451 { .vendor = PCI_VENDOR_ID_IBM, \
452 .bios = TPID(__id1, __id2), \
453 .ec = TPACPI_MATCH_ANY, \
454 .quirks = (__quirk) }
456 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
457 { .vendor = PCI_VENDOR_ID_LENOVO, \
458 .bios = TPID(__id1, __id2), \
459 .ec = TPACPI_MATCH_ANY, \
460 .quirks = (__quirk) }
462 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_LENOVO, \
464 .bios = TPACPI_MATCH_ANY, \
465 .ec = TPID(__id1, __id2), \
466 .quirks = (__quirk) }
468 struct tpacpi_quirk
{
472 unsigned long quirks
;
476 * tpacpi_check_quirks() - search BIOS/EC version on a list
477 * @qlist: array of &struct tpacpi_quirk
478 * @qlist_size: number of elements in @qlist
480 * Iterates over a quirks list until one is found that matches the
481 * ThinkPad's vendor, BIOS and EC model.
483 * Returns 0 if nothing matches, otherwise returns the quirks field of
484 * the matching &struct tpacpi_quirk entry.
486 * The match criteria is: vendor, ec and bios much match.
488 static unsigned long __init
tpacpi_check_quirks(
489 const struct tpacpi_quirk
*qlist
,
490 unsigned int qlist_size
)
493 if ((qlist
->vendor
== thinkpad_id
.vendor
||
494 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
495 (qlist
->bios
== thinkpad_id
.bios_model
||
496 qlist
->bios
== TPACPI_MATCH_ANY
) &&
497 (qlist
->ec
== thinkpad_id
.ec_model
||
498 qlist
->ec
== TPACPI_MATCH_ANY
))
499 return qlist
->quirks
;
507 static inline bool __pure __init
tpacpi_is_lenovo(void)
509 return thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
;
512 static inline bool __pure __init
tpacpi_is_ibm(void)
514 return thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
;
517 /****************************************************************************
518 ****************************************************************************
520 * ACPI Helpers and device model
522 ****************************************************************************
523 ****************************************************************************/
525 /*************************************************************************
529 static acpi_handle root_handle
;
530 static acpi_handle ec_handle
;
532 #define TPACPI_HANDLE(object, parent, paths...) \
533 static acpi_handle object##_handle; \
534 static const acpi_handle * const object##_parent __initconst = \
536 static char *object##_paths[] __initdata = { paths }
538 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
539 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
541 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
543 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
544 "\\CMS", /* R40, R40e */
547 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
548 "^HKEY", /* R30, R31 */
549 "HKEY", /* all others */
552 /*************************************************************************
556 static int acpi_evalf(acpi_handle handle
,
557 int *res
, char *method
, char *fmt
, ...)
560 struct acpi_object_list params
;
561 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
562 struct acpi_buffer result
, *resultp
;
563 union acpi_object out_obj
;
571 pr_err("acpi_evalf() called with empty format\n");
584 params
.pointer
= &in_objs
[0];
591 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
592 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
594 /* add more types as needed */
596 pr_err("acpi_evalf() called with invalid format character '%c'\n",
604 if (res_type
!= 'v') {
605 result
.length
= sizeof(out_obj
);
606 result
.pointer
= &out_obj
;
611 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
615 success
= (status
== AE_OK
&&
616 out_obj
.type
== ACPI_TYPE_INTEGER
);
618 *res
= out_obj
.integer
.value
;
621 success
= status
== AE_OK
;
623 /* add more types as needed */
625 pr_err("acpi_evalf() called with invalid format character '%c'\n",
630 if (!success
&& !quiet
)
631 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
632 method
, fmt0
, acpi_format_exception(status
));
637 static int acpi_ec_read(int i
, u8
*p
)
642 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
646 if (ec_read(i
, p
) < 0)
653 static int acpi_ec_write(int i
, u8 v
)
656 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
659 if (ec_write(i
, v
) < 0)
666 static int issue_thinkpad_cmos_command(int cmos_cmd
)
671 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
677 /*************************************************************************
681 #define TPACPI_ACPIHANDLE_INIT(object) \
682 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
683 object##_paths, ARRAY_SIZE(object##_paths))
685 static void __init
drv_acpi_handle_init(const char *name
,
686 acpi_handle
*handle
, const acpi_handle parent
,
687 char **paths
, const int num_paths
)
692 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
695 for (i
= 0; i
< num_paths
; i
++) {
696 status
= acpi_get_handle(parent
, paths
[i
], handle
);
697 if (ACPI_SUCCESS(status
)) {
698 dbg_printk(TPACPI_DBG_INIT
,
699 "Found ACPI handle %s for %s\n",
705 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
710 static acpi_status __init
tpacpi_acpi_handle_locate_callback(acpi_handle handle
,
711 u32 level
, void *context
, void **return_value
)
713 struct acpi_device
*dev
;
714 if (!strcmp(context
, "video")) {
715 if (acpi_bus_get_device(handle
, &dev
))
717 if (strcmp(ACPI_VIDEO_HID
, acpi_device_hid(dev
)))
721 *(acpi_handle
*)return_value
= handle
;
723 return AE_CTRL_TERMINATE
;
726 static void __init
tpacpi_acpi_handle_locate(const char *name
,
731 acpi_handle device_found
;
733 BUG_ON(!name
|| !handle
);
734 vdbg_printk(TPACPI_DBG_INIT
,
735 "trying to locate ACPI handle for %s, using HID %s\n",
736 name
, hid
? hid
: "NULL");
738 memset(&device_found
, 0, sizeof(device_found
));
739 status
= acpi_get_devices(hid
, tpacpi_acpi_handle_locate_callback
,
740 (void *)name
, &device_found
);
744 if (ACPI_SUCCESS(status
)) {
745 *handle
= device_found
;
746 dbg_printk(TPACPI_DBG_INIT
,
747 "Found ACPI handle for %s\n", name
);
749 vdbg_printk(TPACPI_DBG_INIT
,
750 "Could not locate an ACPI handle for %s: %s\n",
751 name
, acpi_format_exception(status
));
755 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
757 struct ibm_struct
*ibm
= data
;
759 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
762 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
765 ibm
->acpi
->notify(ibm
, event
);
768 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
775 if (!*ibm
->acpi
->handle
)
778 vdbg_printk(TPACPI_DBG_INIT
,
779 "setting up ACPI notify for %s\n", ibm
->name
);
781 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
783 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm
->name
, rc
);
787 ibm
->acpi
->device
->driver_data
= ibm
;
788 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
789 TPACPI_ACPI_EVENT_PREFIX
,
792 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
793 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
794 if (ACPI_FAILURE(status
)) {
795 if (status
== AE_ALREADY_EXISTS
) {
796 pr_notice("another device driver is already handling %s events\n",
799 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
800 ibm
->name
, acpi_format_exception(status
));
804 ibm
->flags
.acpi_notify_installed
= 1;
808 static int __init
tpacpi_device_add(struct acpi_device
*device
)
813 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
817 dbg_printk(TPACPI_DBG_INIT
,
818 "registering %s as an ACPI driver\n", ibm
->name
);
822 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
823 if (!ibm
->acpi
->driver
) {
824 pr_err("failed to allocate memory for ibm->acpi->driver\n");
828 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
829 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
831 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
833 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
835 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
837 kfree(ibm
->acpi
->driver
);
838 ibm
->acpi
->driver
= NULL
;
840 ibm
->flags
.acpi_driver_registered
= 1;
846 /****************************************************************************
847 ****************************************************************************
851 ****************************************************************************
852 ****************************************************************************/
854 static int dispatch_proc_show(struct seq_file
*m
, void *v
)
856 struct ibm_struct
*ibm
= m
->private;
858 if (!ibm
|| !ibm
->read
)
863 static int dispatch_proc_open(struct inode
*inode
, struct file
*file
)
865 return single_open(file
, dispatch_proc_show
, PDE_DATA(inode
));
868 static ssize_t
dispatch_proc_write(struct file
*file
,
869 const char __user
*userbuf
,
870 size_t count
, loff_t
*pos
)
872 struct ibm_struct
*ibm
= PDE_DATA(file_inode(file
));
876 if (!ibm
|| !ibm
->write
)
878 if (count
> PAGE_SIZE
- 2)
881 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
885 if (copy_from_user(kernbuf
, userbuf
, count
)) {
891 strcat(kernbuf
, ",");
892 ret
= ibm
->write(kernbuf
);
901 static const struct file_operations dispatch_proc_fops
= {
902 .owner
= THIS_MODULE
,
903 .open
= dispatch_proc_open
,
906 .release
= single_release
,
907 .write
= dispatch_proc_write
,
910 static char *next_cmd(char **cmds
)
915 while ((end
= strchr(start
, ',')) && end
== start
)
927 /****************************************************************************
928 ****************************************************************************
930 * Device model: input, hwmon and platform
932 ****************************************************************************
933 ****************************************************************************/
935 static struct platform_device
*tpacpi_pdev
;
936 static struct platform_device
*tpacpi_sensors_pdev
;
937 static struct device
*tpacpi_hwmon
;
938 static struct input_dev
*tpacpi_inputdev
;
939 static struct mutex tpacpi_inputdev_send_mutex
;
940 static LIST_HEAD(tpacpi_all_drivers
);
942 #ifdef CONFIG_PM_SLEEP
943 static int tpacpi_suspend_handler(struct device
*dev
)
945 struct ibm_struct
*ibm
, *itmp
;
947 list_for_each_entry_safe(ibm
, itmp
,
957 static int tpacpi_resume_handler(struct device
*dev
)
959 struct ibm_struct
*ibm
, *itmp
;
961 list_for_each_entry_safe(ibm
, itmp
,
972 static SIMPLE_DEV_PM_OPS(tpacpi_pm
,
973 tpacpi_suspend_handler
, tpacpi_resume_handler
);
975 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
977 struct ibm_struct
*ibm
, *itmp
;
979 list_for_each_entry_safe(ibm
, itmp
,
987 static struct platform_driver tpacpi_pdriver
= {
989 .name
= TPACPI_DRVR_NAME
,
992 .shutdown
= tpacpi_shutdown_handler
,
995 static struct platform_driver tpacpi_hwmon_pdriver
= {
997 .name
= TPACPI_HWMON_DRVR_NAME
,
1001 /*************************************************************************
1002 * sysfs support helpers
1005 struct attribute_set
{
1006 unsigned int members
, max_members
;
1007 struct attribute_group group
;
1010 struct attribute_set_obj
{
1011 struct attribute_set s
;
1012 struct attribute
*a
;
1013 } __attribute__((packed
));
1015 static struct attribute_set
*create_attr_set(unsigned int max_members
,
1018 struct attribute_set_obj
*sobj
;
1020 if (max_members
== 0)
1023 /* Allocates space for implicit NULL at the end too */
1024 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
1025 max_members
* sizeof(struct attribute
*),
1029 sobj
->s
.max_members
= max_members
;
1030 sobj
->s
.group
.attrs
= &sobj
->a
;
1031 sobj
->s
.group
.name
= name
;
1036 #define destroy_attr_set(_set) \
1039 /* not multi-threaded safe, use it in a single thread per set */
1040 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
1045 if (s
->members
>= s
->max_members
)
1048 s
->group
.attrs
[s
->members
] = attr
;
1054 static int add_many_to_attr_set(struct attribute_set
*s
,
1055 struct attribute
**attr
,
1060 for (i
= 0; i
< count
; i
++) {
1061 res
= add_to_attr_set(s
, attr
[i
]);
1069 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
1071 sysfs_remove_group(kobj
, &s
->group
);
1072 destroy_attr_set(s
);
1075 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1076 sysfs_create_group(_kobj, &_attr_set->group)
1078 static int parse_strtoul(const char *buf
,
1079 unsigned long max
, unsigned long *value
)
1083 *value
= simple_strtoul(skip_spaces(buf
), &endp
, 0);
1084 endp
= skip_spaces(endp
);
1085 if (*endp
|| *value
> max
)
1091 static void tpacpi_disable_brightness_delay(void)
1093 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
1094 pr_notice("ACPI backlight control delay disabled\n");
1097 static void printk_deprecated_attribute(const char * const what
,
1098 const char * const details
)
1100 tpacpi_log_usertask("deprecated sysfs attribute");
1101 pr_warn("WARNING: sysfs attribute %s is deprecated and will be removed. %s\n",
1105 /*************************************************************************
1106 * rfkill and radio control support helpers
1110 * ThinkPad-ACPI firmware handling model:
1112 * WLSW (master wireless switch) is event-driven, and is common to all
1113 * firmware-controlled radios. It cannot be controlled, just monitored,
1114 * as expected. It overrides all radio state in firmware
1116 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1117 * (TODO: verify how WLSW interacts with the returned radio state).
1119 * The only time there are shadow radio state changes, is when
1120 * masked-off hotkeys are used.
1124 * Internal driver API for radio state:
1126 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1127 * bool: true means radio blocked (off)
1129 enum tpacpi_rfkill_state
{
1130 TPACPI_RFK_RADIO_OFF
= 0,
1134 /* rfkill switches */
1135 enum tpacpi_rfk_id
{
1136 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
1137 TPACPI_RFK_WWAN_SW_ID
,
1138 TPACPI_RFK_UWB_SW_ID
,
1142 static const char *tpacpi_rfkill_names
[] = {
1143 [TPACPI_RFK_BLUETOOTH_SW_ID
] = "bluetooth",
1144 [TPACPI_RFK_WWAN_SW_ID
] = "wwan",
1145 [TPACPI_RFK_UWB_SW_ID
] = "uwb",
1146 [TPACPI_RFK_SW_MAX
] = NULL
1149 /* ThinkPad-ACPI rfkill subdriver */
1151 struct rfkill
*rfkill
;
1152 enum tpacpi_rfk_id id
;
1153 const struct tpacpi_rfk_ops
*ops
;
1156 struct tpacpi_rfk_ops
{
1157 /* firmware interface */
1158 int (*get_status
)(void);
1159 int (*set_status
)(const enum tpacpi_rfkill_state
);
1162 static struct tpacpi_rfk
*tpacpi_rfkill_switches
[TPACPI_RFK_SW_MAX
];
1164 /* Query FW and update rfkill sw state for a given rfkill switch */
1165 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk
*tp_rfk
)
1172 status
= (tp_rfk
->ops
->get_status
)();
1176 rfkill_set_sw_state(tp_rfk
->rfkill
,
1177 (status
== TPACPI_RFK_RADIO_OFF
));
1182 /* Query FW and update rfkill sw state for all rfkill switches */
1183 static void tpacpi_rfk_update_swstate_all(void)
1187 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++)
1188 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[i
]);
1192 * Sync the HW-blocking state of all rfkill switches,
1193 * do notice it causes the rfkill core to schedule uevents
1195 static void tpacpi_rfk_update_hwblock_state(bool blocked
)
1198 struct tpacpi_rfk
*tp_rfk
;
1200 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++) {
1201 tp_rfk
= tpacpi_rfkill_switches
[i
];
1203 if (rfkill_set_hw_state(tp_rfk
->rfkill
,
1205 /* ignore -- we track sw block */
1211 /* Call to get the WLSW state from the firmware */
1212 static int hotkey_get_wlsw(void);
1214 /* Call to query WLSW state and update all rfkill switches */
1215 static bool tpacpi_rfk_check_hwblock_state(void)
1217 int res
= hotkey_get_wlsw();
1220 /* When unknown or unsupported, we have to assume it is unblocked */
1224 hw_blocked
= (res
== TPACPI_RFK_RADIO_OFF
);
1225 tpacpi_rfk_update_hwblock_state(hw_blocked
);
1230 static int tpacpi_rfk_hook_set_block(void *data
, bool blocked
)
1232 struct tpacpi_rfk
*tp_rfk
= data
;
1235 dbg_printk(TPACPI_DBG_RFKILL
,
1236 "request to change radio state to %s\n",
1237 blocked
? "blocked" : "unblocked");
1239 /* try to set radio state */
1240 res
= (tp_rfk
->ops
->set_status
)(blocked
?
1241 TPACPI_RFK_RADIO_OFF
: TPACPI_RFK_RADIO_ON
);
1243 /* and update the rfkill core with whatever the FW really did */
1244 tpacpi_rfk_update_swstate(tp_rfk
);
1246 return (res
< 0) ? res
: 0;
1249 static const struct rfkill_ops tpacpi_rfk_rfkill_ops
= {
1250 .set_block
= tpacpi_rfk_hook_set_block
,
1253 static int __init
tpacpi_new_rfkill(const enum tpacpi_rfk_id id
,
1254 const struct tpacpi_rfk_ops
*tp_rfkops
,
1255 const enum rfkill_type rfktype
,
1257 const bool set_default
)
1259 struct tpacpi_rfk
*atp_rfk
;
1261 bool sw_state
= false;
1265 BUG_ON(id
>= TPACPI_RFK_SW_MAX
|| tpacpi_rfkill_switches
[id
]);
1267 atp_rfk
= kzalloc(sizeof(struct tpacpi_rfk
), GFP_KERNEL
);
1269 atp_rfk
->rfkill
= rfkill_alloc(name
,
1272 &tpacpi_rfk_rfkill_ops
,
1274 if (!atp_rfk
|| !atp_rfk
->rfkill
) {
1275 pr_err("failed to allocate memory for rfkill class\n");
1281 atp_rfk
->ops
= tp_rfkops
;
1283 sw_status
= (tp_rfkops
->get_status
)();
1284 if (sw_status
< 0) {
1285 pr_err("failed to read initial state for %s, error %d\n",
1288 sw_state
= (sw_status
== TPACPI_RFK_RADIO_OFF
);
1290 /* try to keep the initial state, since we ask the
1291 * firmware to preserve it across S5 in NVRAM */
1292 rfkill_init_sw_state(atp_rfk
->rfkill
, sw_state
);
1295 hw_state
= tpacpi_rfk_check_hwblock_state();
1296 rfkill_set_hw_state(atp_rfk
->rfkill
, hw_state
);
1298 res
= rfkill_register(atp_rfk
->rfkill
);
1300 pr_err("failed to register %s rfkill switch: %d\n", name
, res
);
1301 rfkill_destroy(atp_rfk
->rfkill
);
1306 tpacpi_rfkill_switches
[id
] = atp_rfk
;
1308 pr_info("rfkill switch %s: radio is %sblocked\n",
1309 name
, (sw_state
|| hw_state
) ? "" : "un");
1313 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id
)
1315 struct tpacpi_rfk
*tp_rfk
;
1317 BUG_ON(id
>= TPACPI_RFK_SW_MAX
);
1319 tp_rfk
= tpacpi_rfkill_switches
[id
];
1321 rfkill_unregister(tp_rfk
->rfkill
);
1322 rfkill_destroy(tp_rfk
->rfkill
);
1323 tpacpi_rfkill_switches
[id
] = NULL
;
1328 static void printk_deprecated_rfkill_attribute(const char * const what
)
1330 printk_deprecated_attribute(what
,
1331 "Please switch to generic rfkill before year 2010");
1334 /* sysfs <radio> enable ------------------------------------------------ */
1335 static ssize_t
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id
,
1336 struct device_attribute
*attr
,
1341 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1343 /* This is in the ABI... */
1344 if (tpacpi_rfk_check_hwblock_state()) {
1345 status
= TPACPI_RFK_RADIO_OFF
;
1347 status
= tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1352 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1353 (status
== TPACPI_RFK_RADIO_ON
) ? 1 : 0);
1356 static ssize_t
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id
,
1357 struct device_attribute
*attr
,
1358 const char *buf
, size_t count
)
1363 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1365 if (parse_strtoul(buf
, 1, &t
))
1368 tpacpi_disclose_usertask(attr
->attr
.name
, "set to %ld\n", t
);
1370 /* This is in the ABI... */
1371 if (tpacpi_rfk_check_hwblock_state() && !!t
)
1374 res
= tpacpi_rfkill_switches
[id
]->ops
->set_status((!!t
) ?
1375 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
);
1376 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1378 return (res
< 0) ? res
: count
;
1381 /* procfs -------------------------------------------------------------- */
1382 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id
, struct seq_file
*m
)
1384 if (id
>= TPACPI_RFK_SW_MAX
)
1385 seq_printf(m
, "status:\t\tnot supported\n");
1389 /* This is in the ABI... */
1390 if (tpacpi_rfk_check_hwblock_state()) {
1391 status
= TPACPI_RFK_RADIO_OFF
;
1393 status
= tpacpi_rfk_update_swstate(
1394 tpacpi_rfkill_switches
[id
]);
1399 seq_printf(m
, "status:\t\t%s\n",
1400 (status
== TPACPI_RFK_RADIO_ON
) ?
1401 "enabled" : "disabled");
1402 seq_printf(m
, "commands:\tenable, disable\n");
1408 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id
, char *buf
)
1414 if (id
>= TPACPI_RFK_SW_MAX
)
1417 while ((cmd
= next_cmd(&buf
))) {
1418 if (strlencmp(cmd
, "enable") == 0)
1419 status
= TPACPI_RFK_RADIO_ON
;
1420 else if (strlencmp(cmd
, "disable") == 0)
1421 status
= TPACPI_RFK_RADIO_OFF
;
1427 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1428 (status
== TPACPI_RFK_RADIO_ON
) ?
1429 "enable" : "disable",
1430 tpacpi_rfkill_names
[id
]);
1431 res
= (tpacpi_rfkill_switches
[id
]->ops
->set_status
)(status
);
1432 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1438 /*************************************************************************
1439 * thinkpad-acpi driver attributes
1442 /* interface_version --------------------------------------------------- */
1443 static ssize_t
interface_version_show(struct device_driver
*drv
, char *buf
)
1445 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1447 static DRIVER_ATTR_RO(interface_version
);
1449 /* debug_level --------------------------------------------------------- */
1450 static ssize_t
debug_level_show(struct device_driver
*drv
, char *buf
)
1452 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1455 static ssize_t
debug_level_store(struct device_driver
*drv
, const char *buf
,
1460 if (parse_strtoul(buf
, 0xffff, &t
))
1467 static DRIVER_ATTR_RW(debug_level
);
1469 /* version ------------------------------------------------------------- */
1470 static ssize_t
version_show(struct device_driver
*drv
, char *buf
)
1472 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1473 TPACPI_DESC
, TPACPI_VERSION
);
1475 static DRIVER_ATTR_RO(version
);
1477 /* --------------------------------------------------------------------- */
1479 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1481 /* wlsw_emulstate ------------------------------------------------------ */
1482 static ssize_t
wlsw_emulstate_show(struct device_driver
*drv
, char *buf
)
1484 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1487 static ssize_t
wlsw_emulstate_store(struct device_driver
*drv
, const char *buf
,
1492 if (parse_strtoul(buf
, 1, &t
))
1495 if (tpacpi_wlsw_emulstate
!= !!t
) {
1496 tpacpi_wlsw_emulstate
= !!t
;
1497 tpacpi_rfk_update_hwblock_state(!t
); /* negative logic */
1502 static DRIVER_ATTR_RW(wlsw_emulstate
);
1504 /* bluetooth_emulstate ------------------------------------------------- */
1505 static ssize_t
bluetooth_emulstate_show(struct device_driver
*drv
, char *buf
)
1507 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1510 static ssize_t
bluetooth_emulstate_store(struct device_driver
*drv
,
1511 const char *buf
, size_t count
)
1515 if (parse_strtoul(buf
, 1, &t
))
1518 tpacpi_bluetooth_emulstate
= !!t
;
1522 static DRIVER_ATTR_RW(bluetooth_emulstate
);
1524 /* wwan_emulstate ------------------------------------------------- */
1525 static ssize_t
wwan_emulstate_show(struct device_driver
*drv
, char *buf
)
1527 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1530 static ssize_t
wwan_emulstate_store(struct device_driver
*drv
, const char *buf
,
1535 if (parse_strtoul(buf
, 1, &t
))
1538 tpacpi_wwan_emulstate
= !!t
;
1542 static DRIVER_ATTR_RW(wwan_emulstate
);
1544 /* uwb_emulstate ------------------------------------------------- */
1545 static ssize_t
uwb_emulstate_show(struct device_driver
*drv
, char *buf
)
1547 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1550 static ssize_t
uwb_emulstate_store(struct device_driver
*drv
, const char *buf
,
1555 if (parse_strtoul(buf
, 1, &t
))
1558 tpacpi_uwb_emulstate
= !!t
;
1562 static DRIVER_ATTR_RW(uwb_emulstate
);
1565 /* --------------------------------------------------------------------- */
1567 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1568 &driver_attr_debug_level
, &driver_attr_version
,
1569 &driver_attr_interface_version
,
1572 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1578 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1579 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1583 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1584 if (!res
&& dbg_wlswemul
)
1585 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1586 if (!res
&& dbg_bluetoothemul
)
1587 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1588 if (!res
&& dbg_wwanemul
)
1589 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1590 if (!res
&& dbg_uwbemul
)
1591 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1597 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1601 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1602 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1604 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1605 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1606 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1607 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1608 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1612 /*************************************************************************
1617 * Table of recommended minimum BIOS versions
1619 * Reasons for listing:
1620 * 1. Stable BIOS, listed because the unknown amount of
1621 * bugs and bad ACPI behaviour on older versions
1623 * 2. BIOS or EC fw with known bugs that trigger on Linux
1625 * 3. BIOS with known reduced functionality in older versions
1627 * We recommend the latest BIOS and EC version.
1628 * We only support the latest BIOS and EC fw version as a rule.
1630 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1631 * Information from users in ThinkWiki
1633 * WARNING: we use this table also to detect that the machine is
1634 * a ThinkPad in some cases, so don't remove entries lightly.
1637 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1638 { .vendor = (__v), \
1639 .bios = TPID(__id1, __id2), \
1640 .ec = TPACPI_MATCH_ANY, \
1641 .quirks = TPACPI_MATCH_ANY << 16 \
1642 | (__bv1) << 8 | (__bv2) }
1644 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1645 __eid, __ev1, __ev2) \
1646 { .vendor = (__v), \
1647 .bios = TPID(__bid1, __bid2), \
1649 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1650 | (__bv1) << 8 | (__bv2) }
1652 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1653 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1655 /* Outdated IBM BIOSes often lack the EC id string */
1656 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1657 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1658 __bv1, __bv2, TPID(__id1, __id2), \
1660 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1661 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1664 /* Outdated IBM BIOSes often lack the EC id string */
1665 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1666 __eid1, __eid2, __ev1, __ev2) \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1668 __bv1, __bv2, TPID(__eid1, __eid2), \
1670 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1671 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1674 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1675 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1677 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1678 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1679 __bv1, __bv2, TPID(__id1, __id2), \
1682 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1683 __eid1, __eid2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1685 __bv1, __bv2, TPID(__eid1, __eid2), \
1688 static const struct tpacpi_quirk tpacpi_bios_version_qtable
[] __initconst
= {
1689 /* Numeric models ------------------ */
1690 /* FW MODEL BIOS VERS */
1691 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1692 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1693 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1694 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1695 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1696 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1697 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1698 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1699 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1701 /* A-series ------------------------- */
1702 /* FW MODEL BIOS VERS EC VERS */
1703 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1704 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1705 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1706 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1707 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1708 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1709 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1710 TPV_QI0('1', '3', '2', '0'), /* A22m */
1711 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1712 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1713 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1715 /* G-series ------------------------- */
1716 /* FW MODEL BIOS VERS */
1717 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1718 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1720 /* R-series, T-series --------------- */
1721 /* FW MODEL BIOS VERS EC VERS */
1722 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1723 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1724 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1725 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1726 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1727 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1728 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1729 T40/p, T41/p, T42/p (1) */
1730 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1731 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1732 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1733 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1735 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1736 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1737 TPV_QI0('1', '6', '3', '2'), /* T22 */
1738 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1739 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1740 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1742 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1743 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1744 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1746 /* BIOS FW BIOS VERS EC FW EC VERS */
1747 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1748 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1750 /* X-series ------------------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1753 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1754 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1755 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1756 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1757 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1758 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1760 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1761 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1763 /* (0) - older versions lack DMI EC fw string and functionality */
1764 /* (1) - older versions known to lack functionality */
1775 static void __init
tpacpi_check_outdated_fw(void)
1777 unsigned long fwvers
;
1778 u16 ec_version
, bios_version
;
1780 fwvers
= tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1781 ARRAY_SIZE(tpacpi_bios_version_qtable
));
1786 bios_version
= fwvers
& 0xffffU
;
1787 ec_version
= (fwvers
>> 16) & 0xffffU
;
1789 /* note that unknown versions are set to 0x0000 and we use that */
1790 if ((bios_version
> thinkpad_id
.bios_release
) ||
1791 (ec_version
> thinkpad_id
.ec_release
&&
1792 ec_version
!= TPACPI_MATCH_ANY
)) {
1794 * The changelogs would let us track down the exact
1795 * reason, but it is just too much of a pain to track
1796 * it. We only list BIOSes that are either really
1797 * broken, or really stable to begin with, so it is
1798 * best if the user upgrades the firmware anyway.
1800 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1801 pr_warn("WARNING: This firmware may be missing critical bug fixes and/or important features\n");
1805 static bool __init
tpacpi_is_fw_known(void)
1807 return tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable
)) != 0;
1811 /****************************************************************************
1812 ****************************************************************************
1816 ****************************************************************************
1817 ****************************************************************************/
1819 /*************************************************************************
1820 * thinkpad-acpi metadata subdriver
1823 static int thinkpad_acpi_driver_read(struct seq_file
*m
)
1825 seq_printf(m
, "driver:\t\t%s\n", TPACPI_DESC
);
1826 seq_printf(m
, "version:\t%s\n", TPACPI_VERSION
);
1830 static struct ibm_struct thinkpad_acpi_driver_data
= {
1832 .read
= thinkpad_acpi_driver_read
,
1835 /*************************************************************************
1840 * ThinkPad firmware event model
1842 * The ThinkPad firmware has two main event interfaces: normal ACPI
1843 * notifications (which follow the ACPI standard), and a private event
1846 * The private event interface also issues events for the hotkeys. As
1847 * the driver gained features, the event handling code ended up being
1848 * built around the hotkey subdriver. This will need to be refactored
1849 * to a more formal event API eventually.
1851 * Some "hotkeys" are actually supposed to be used as event reports,
1852 * such as "brightness has changed", "volume has changed", depending on
1853 * the ThinkPad model and how the firmware is operating.
1855 * Unlike other classes, hotkey-class events have mask/unmask control on
1856 * non-ancient firmware. However, how it behaves changes a lot with the
1857 * firmware model and version.
1860 enum { /* hot key scan codes (derived from ACPI DSDT) */
1861 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1862 TP_ACPI_HOTKEYSCAN_FNF2
,
1863 TP_ACPI_HOTKEYSCAN_FNF3
,
1864 TP_ACPI_HOTKEYSCAN_FNF4
,
1865 TP_ACPI_HOTKEYSCAN_FNF5
,
1866 TP_ACPI_HOTKEYSCAN_FNF6
,
1867 TP_ACPI_HOTKEYSCAN_FNF7
,
1868 TP_ACPI_HOTKEYSCAN_FNF8
,
1869 TP_ACPI_HOTKEYSCAN_FNF9
,
1870 TP_ACPI_HOTKEYSCAN_FNF10
,
1871 TP_ACPI_HOTKEYSCAN_FNF11
,
1872 TP_ACPI_HOTKEYSCAN_FNF12
,
1873 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1874 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1875 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1876 TP_ACPI_HOTKEYSCAN_FNHOME
,
1877 TP_ACPI_HOTKEYSCAN_FNEND
,
1878 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1879 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1880 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1881 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1882 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1883 TP_ACPI_HOTKEYSCAN_MUTE
,
1884 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1885 TP_ACPI_HOTKEYSCAN_UNK1
,
1886 TP_ACPI_HOTKEYSCAN_UNK2
,
1887 TP_ACPI_HOTKEYSCAN_UNK3
,
1888 TP_ACPI_HOTKEYSCAN_UNK4
,
1889 TP_ACPI_HOTKEYSCAN_UNK5
,
1890 TP_ACPI_HOTKEYSCAN_UNK6
,
1891 TP_ACPI_HOTKEYSCAN_UNK7
,
1892 TP_ACPI_HOTKEYSCAN_UNK8
,
1894 /* Adaptive keyboard keycodes */
1895 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START
,
1896 TP_ACPI_HOTKEYSCAN_MUTE2
= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START
,
1897 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO
,
1898 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL
,
1899 TP_ACPI_HOTKEYSCAN_CLOUD
,
1900 TP_ACPI_HOTKEYSCAN_UNK9
,
1901 TP_ACPI_HOTKEYSCAN_VOICE
,
1902 TP_ACPI_HOTKEYSCAN_UNK10
,
1903 TP_ACPI_HOTKEYSCAN_GESTURES
,
1904 TP_ACPI_HOTKEYSCAN_UNK11
,
1905 TP_ACPI_HOTKEYSCAN_UNK12
,
1906 TP_ACPI_HOTKEYSCAN_UNK13
,
1907 TP_ACPI_HOTKEYSCAN_CONFIG
,
1908 TP_ACPI_HOTKEYSCAN_NEW_TAB
,
1909 TP_ACPI_HOTKEYSCAN_RELOAD
,
1910 TP_ACPI_HOTKEYSCAN_BACK
,
1911 TP_ACPI_HOTKEYSCAN_MIC_DOWN
,
1912 TP_ACPI_HOTKEYSCAN_MIC_UP
,
1913 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION
,
1914 TP_ACPI_HOTKEYSCAN_CAMERA_MODE
,
1915 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY
,
1917 /* Lenovo extended keymap, starting at 0x1300 */
1918 TP_ACPI_HOTKEYSCAN_EXTENDED_START
,
1919 /* first new observed key (star, favorites) is 0x1311 */
1920 TP_ACPI_HOTKEYSCAN_STAR
= 69,
1921 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL2
,
1922 TP_ACPI_HOTKEYSCAN_UNK25
,
1923 TP_ACPI_HOTKEYSCAN_BLUETOOTH
,
1924 TP_ACPI_HOTKEYSCAN_KEYBOARD
,
1926 /* Hotkey keymap size */
1927 TPACPI_HOTKEY_MAP_LEN
1930 enum { /* Keys/events available through NVRAM polling */
1931 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1932 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1935 enum { /* Positions of some of the keys in hotkey masks */
1936 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1937 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1938 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1939 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1940 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1941 TP_ACPI_HKEY_KBD_LIGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1942 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1943 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1944 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1945 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1946 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1949 enum { /* NVRAM to ACPI HKEY group map */
1950 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1951 TP_ACPI_HKEY_ZOOM_MASK
|
1952 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1953 TP_ACPI_HKEY_HIBERNATE_MASK
,
1954 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1955 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1956 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1957 TP_ACPI_HKEY_VOLDWN_MASK
|
1958 TP_ACPI_HKEY_MUTE_MASK
,
1961 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1962 struct tp_nvram_state
{
1963 u16 thinkpad_toggle
:1;
1965 u16 display_toggle
:1;
1966 u16 thinklight_toggle
:1;
1967 u16 hibernate_toggle
:1;
1968 u16 displayexp_toggle
:1;
1969 u16 display_state
:1;
1970 u16 brightness_toggle
:1;
1971 u16 volume_toggle
:1;
1974 u8 brightness_level
;
1978 /* kthread for the hotkey poller */
1979 static struct task_struct
*tpacpi_hotkey_task
;
1982 * Acquire mutex to write poller control variables as an
1985 * Increment hotkey_config_change when changing them if you
1986 * want the kthread to forget old state.
1988 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1990 static struct mutex hotkey_thread_data_mutex
;
1991 static unsigned int hotkey_config_change
;
1994 * hotkey poller control variables
1996 * Must be atomic or readers will also need to acquire mutex
1998 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1999 * should be used only when the changes need to be taken as
2000 * a block, OR when one needs to force the kthread to forget
2003 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
2004 static unsigned int hotkey_poll_freq
= 10; /* Hz */
2006 #define HOTKEY_CONFIG_CRITICAL_START \
2008 mutex_lock(&hotkey_thread_data_mutex); \
2009 hotkey_config_change++; \
2011 #define HOTKEY_CONFIG_CRITICAL_END \
2012 mutex_unlock(&hotkey_thread_data_mutex);
2014 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2016 #define hotkey_source_mask 0U
2017 #define HOTKEY_CONFIG_CRITICAL_START
2018 #define HOTKEY_CONFIG_CRITICAL_END
2020 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2022 static struct mutex hotkey_mutex
;
2024 static enum { /* Reasons for waking up */
2025 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
2026 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
2027 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
2028 } hotkey_wakeup_reason
;
2030 static int hotkey_autosleep_ack
;
2032 static u32 hotkey_orig_mask
; /* events the BIOS had enabled */
2033 static u32 hotkey_all_mask
; /* all events supported in fw */
2034 static u32 hotkey_adaptive_all_mask
; /* all adaptive events supported in fw */
2035 static u32 hotkey_reserved_mask
; /* events better left disabled */
2036 static u32 hotkey_driver_mask
; /* events needed by the driver */
2037 static u32 hotkey_user_mask
; /* events visible to userspace */
2038 static u32 hotkey_acpi_mask
; /* events enabled in firmware */
2040 static u16
*hotkey_keycode_map
;
2042 static struct attribute_set
*hotkey_dev_attributes
;
2044 static void tpacpi_driver_event(const unsigned int hkey_event
);
2045 static void hotkey_driver_event(const unsigned int scancode
);
2046 static void hotkey_poll_setup(const bool may_warn
);
2048 /* HKEY.MHKG() return bits */
2049 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2051 TP_ACPI_MULTI_MODE_INVALID
= 0,
2052 TP_ACPI_MULTI_MODE_UNKNOWN
= 1 << 0,
2053 TP_ACPI_MULTI_MODE_LAPTOP
= 1 << 1,
2054 TP_ACPI_MULTI_MODE_TABLET
= 1 << 2,
2055 TP_ACPI_MULTI_MODE_FLAT
= 1 << 3,
2056 TP_ACPI_MULTI_MODE_STAND
= 1 << 4,
2057 TP_ACPI_MULTI_MODE_TENT
= 1 << 5,
2058 TP_ACPI_MULTI_MODE_STAND_TENT
= 1 << 6,
2062 /* The following modes are considered tablet mode for the purpose of
2063 * reporting the status to userspace. i.e. in all these modes it makes
2064 * sense to disable the laptop input devices such as touchpad and
2067 TP_ACPI_MULTI_MODE_TABLET_LIKE
= TP_ACPI_MULTI_MODE_TABLET
|
2068 TP_ACPI_MULTI_MODE_STAND
|
2069 TP_ACPI_MULTI_MODE_TENT
|
2070 TP_ACPI_MULTI_MODE_STAND_TENT
,
2073 static int hotkey_get_wlsw(void)
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_gmms_get_tablet_mode(int s
, int *has_tablet_mode
)
2094 int type
= (s
>> 16) & 0xffff;
2095 int value
= s
& 0xffff;
2096 int mode
= TP_ACPI_MULTI_MODE_INVALID
;
2097 int valid_modes
= 0;
2099 if (has_tablet_mode
)
2100 *has_tablet_mode
= 0;
2104 valid_modes
= TP_ACPI_MULTI_MODE_LAPTOP
|
2105 TP_ACPI_MULTI_MODE_TABLET
|
2106 TP_ACPI_MULTI_MODE_STAND_TENT
;
2109 valid_modes
= TP_ACPI_MULTI_MODE_LAPTOP
|
2110 TP_ACPI_MULTI_MODE_FLAT
|
2111 TP_ACPI_MULTI_MODE_TABLET
|
2112 TP_ACPI_MULTI_MODE_STAND
|
2113 TP_ACPI_MULTI_MODE_TENT
;
2116 valid_modes
= TP_ACPI_MULTI_MODE_LAPTOP
|
2117 TP_ACPI_MULTI_MODE_FLAT
;
2121 /* In mode 4, FLAT is not specified as a valid mode. However,
2122 * it can be seen at least on the X1 Yoga 2nd Generation.
2124 valid_modes
= TP_ACPI_MULTI_MODE_LAPTOP
|
2125 TP_ACPI_MULTI_MODE_FLAT
|
2126 TP_ACPI_MULTI_MODE_TABLET
|
2127 TP_ACPI_MULTI_MODE_STAND
|
2128 TP_ACPI_MULTI_MODE_TENT
;
2131 pr_err("Unknown multi mode status type %d with value 0x%04X, please report this to %s\n",
2132 type
, value
, TPACPI_MAIL
);
2136 if (has_tablet_mode
&& (valid_modes
& TP_ACPI_MULTI_MODE_TABLET_LIKE
))
2137 *has_tablet_mode
= 1;
2141 mode
= TP_ACPI_MULTI_MODE_LAPTOP
;
2144 mode
= TP_ACPI_MULTI_MODE_FLAT
;
2147 mode
= TP_ACPI_MULTI_MODE_TABLET
;
2151 mode
= TP_ACPI_MULTI_MODE_STAND_TENT
;
2153 mode
= TP_ACPI_MULTI_MODE_STAND
;
2156 mode
= TP_ACPI_MULTI_MODE_TENT
;
2159 if (type
== 5 && value
== 0xffff) {
2160 pr_warn("Multi mode status is undetected, assuming laptop\n");
2165 if (!(mode
& valid_modes
)) {
2166 pr_err("Unknown/reserved multi mode value 0x%04X for type %d, please report this to %s\n",
2167 value
, type
, TPACPI_MAIL
);
2171 return !!(mode
& TP_ACPI_MULTI_MODE_TABLET_LIKE
);
2174 static int hotkey_get_tablet_mode(int *status
)
2178 switch (tp_features
.hotkey_tablet
) {
2179 case TP_HOTKEY_TABLET_USES_MHKG
:
2180 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
2183 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
2185 case TP_HOTKEY_TABLET_USES_GMMS
:
2186 if (!acpi_evalf(hkey_handle
, &s
, "GMMS", "dd", 0))
2189 *status
= hotkey_gmms_get_tablet_mode(s
, NULL
);
2199 * Reads current event mask from firmware, and updates
2200 * hotkey_acpi_mask accordingly. Also resets any bits
2201 * from hotkey_user_mask that are unavailable to be
2202 * delivered (shadow requirement of the userspace ABI).
2204 * Call with hotkey_mutex held
2206 static int hotkey_mask_get(void)
2208 if (tp_features
.hotkey_mask
) {
2211 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
2214 hotkey_acpi_mask
= m
;
2216 /* no mask support doesn't mean no event support... */
2217 hotkey_acpi_mask
= hotkey_all_mask
;
2220 /* sync userspace-visible mask */
2221 hotkey_user_mask
&= (hotkey_acpi_mask
| hotkey_source_mask
);
2226 static void hotkey_mask_warn_incomplete_mask(void)
2228 /* log only what the user can fix... */
2229 const u32 wantedmask
= hotkey_driver_mask
&
2230 ~(hotkey_acpi_mask
| hotkey_source_mask
) &
2231 (hotkey_all_mask
| TPACPI_HKEY_NVRAM_KNOWN_MASK
);
2234 pr_notice("required events 0x%08x not enabled!\n", wantedmask
);
2238 * Set the firmware mask when supported
2240 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2242 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2244 * Call with hotkey_mutex held
2246 static int hotkey_mask_set(u32 mask
)
2251 const u32 fwmask
= mask
& ~hotkey_source_mask
;
2253 if (tp_features
.hotkey_mask
) {
2254 for (i
= 0; i
< 32; i
++) {
2255 if (!acpi_evalf(hkey_handle
,
2256 NULL
, "MHKM", "vdd", i
+ 1,
2257 !!(mask
& (1 << i
)))) {
2265 * We *must* make an inconditional call to hotkey_mask_get to
2266 * refresh hotkey_acpi_mask and update hotkey_user_mask
2268 * Take the opportunity to also log when we cannot _enable_
2271 if (!hotkey_mask_get() && !rc
&& (fwmask
& ~hotkey_acpi_mask
)) {
2272 pr_notice("asked for hotkey mask 0x%08x, but firmware forced it to 0x%08x\n",
2273 fwmask
, hotkey_acpi_mask
);
2276 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
2277 hotkey_mask_warn_incomplete_mask();
2283 * Sets hotkey_user_mask and tries to set the firmware mask
2285 * Call with hotkey_mutex held
2287 static int hotkey_user_mask_set(const u32 mask
)
2291 /* Give people a chance to notice they are doing something that
2292 * is bound to go boom on their users sooner or later */
2293 if (!tp_warned
.hotkey_mask_ff
&&
2294 (mask
== 0xffff || mask
== 0xffffff ||
2295 mask
== 0xffffffff)) {
2296 tp_warned
.hotkey_mask_ff
= 1;
2297 pr_notice("setting the hotkey mask to 0x%08x is likely not the best way to go about it\n",
2299 pr_notice("please consider using the driver defaults, and refer to up-to-date thinkpad-acpi documentation\n");
2302 /* Try to enable what the user asked for, plus whatever we need.
2303 * this syncs everything but won't enable bits in hotkey_user_mask */
2304 rc
= hotkey_mask_set((mask
| hotkey_driver_mask
) & ~hotkey_source_mask
);
2306 /* Enable the available bits in hotkey_user_mask */
2307 hotkey_user_mask
= mask
& (hotkey_acpi_mask
| hotkey_source_mask
);
2313 * Sets the driver hotkey mask.
2315 * Can be called even if the hotkey subdriver is inactive
2317 static int tpacpi_hotkey_driver_mask_set(const u32 mask
)
2321 /* Do the right thing if hotkey_init has not been called yet */
2322 if (!tp_features
.hotkey
) {
2323 hotkey_driver_mask
= mask
;
2327 mutex_lock(&hotkey_mutex
);
2329 HOTKEY_CONFIG_CRITICAL_START
2330 hotkey_driver_mask
= mask
;
2331 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2332 hotkey_source_mask
|= (mask
& ~hotkey_all_mask
);
2334 HOTKEY_CONFIG_CRITICAL_END
2336 rc
= hotkey_mask_set((hotkey_acpi_mask
| hotkey_driver_mask
) &
2337 ~hotkey_source_mask
);
2338 hotkey_poll_setup(true);
2340 mutex_unlock(&hotkey_mutex
);
2345 static int hotkey_status_get(int *status
)
2347 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2353 static int hotkey_status_set(bool enable
)
2355 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2361 static void tpacpi_input_send_tabletsw(void)
2365 if (tp_features
.hotkey_tablet
&&
2366 !hotkey_get_tablet_mode(&state
)) {
2367 mutex_lock(&tpacpi_inputdev_send_mutex
);
2369 input_report_switch(tpacpi_inputdev
,
2370 SW_TABLET_MODE
, !!state
);
2371 input_sync(tpacpi_inputdev
);
2373 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2377 /* Do NOT call without validating scancode first */
2378 static void tpacpi_input_send_key(const unsigned int scancode
)
2380 const unsigned int keycode
= hotkey_keycode_map
[scancode
];
2382 if (keycode
!= KEY_RESERVED
) {
2383 mutex_lock(&tpacpi_inputdev_send_mutex
);
2385 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
, scancode
);
2386 input_report_key(tpacpi_inputdev
, keycode
, 1);
2387 input_sync(tpacpi_inputdev
);
2389 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
, scancode
);
2390 input_report_key(tpacpi_inputdev
, keycode
, 0);
2391 input_sync(tpacpi_inputdev
);
2393 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2397 /* Do NOT call without validating scancode first */
2398 static void tpacpi_input_send_key_masked(const unsigned int scancode
)
2400 hotkey_driver_event(scancode
);
2401 if (hotkey_user_mask
& (1 << scancode
))
2402 tpacpi_input_send_key(scancode
);
2405 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2406 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
2408 /* Do NOT call without validating scancode first */
2409 static void tpacpi_hotkey_send_key(unsigned int scancode
)
2411 tpacpi_input_send_key_masked(scancode
);
2414 static void hotkey_read_nvram(struct tp_nvram_state
*n
, const u32 m
)
2418 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
2419 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
2420 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
2421 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
2422 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
2423 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
2425 if (m
& TP_ACPI_HKEY_KBD_LIGHT_MASK
) {
2426 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
2427 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
2429 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
2430 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
2431 n
->displayexp_toggle
=
2432 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
2434 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
2435 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
2436 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
2437 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
2438 n
->brightness_toggle
=
2439 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
2441 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
2442 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
2443 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
2444 >> TP_NVRAM_POS_LEVEL_VOLUME
;
2445 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
2446 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
2450 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2452 if ((event_mask & (1 << __scancode)) && \
2453 oldn->__member != newn->__member) \
2454 tpacpi_hotkey_send_key(__scancode); \
2457 #define TPACPI_MAY_SEND_KEY(__scancode) \
2459 if (event_mask & (1 << __scancode)) \
2460 tpacpi_hotkey_send_key(__scancode); \
2463 static void issue_volchange(const unsigned int oldvol
,
2464 const unsigned int newvol
,
2465 const u32 event_mask
)
2467 unsigned int i
= oldvol
;
2469 while (i
> newvol
) {
2470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2473 while (i
< newvol
) {
2474 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2479 static void issue_brightnesschange(const unsigned int oldbrt
,
2480 const unsigned int newbrt
,
2481 const u32 event_mask
)
2483 unsigned int i
= oldbrt
;
2485 while (i
> newbrt
) {
2486 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2489 while (i
< newbrt
) {
2490 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2495 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
2496 struct tp_nvram_state
*newn
,
2497 const u32 event_mask
)
2500 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
2501 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
2502 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
2503 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
2505 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
2507 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
2512 * This code is supposed to duplicate the IBM firmware behaviour:
2513 * - Pressing MUTE issues mute hotkey message, even when already mute
2514 * - Pressing Volume up/down issues volume up/down hotkey messages,
2515 * even when already at maximum or minimum volume
2516 * - The act of unmuting issues volume up/down notification,
2517 * depending which key was used to unmute
2519 * We are constrained to what the NVRAM can tell us, which is not much
2520 * and certainly not enough if more than one volume hotkey was pressed
2521 * since the last poll cycle.
2523 * Just to make our life interesting, some newer Lenovo ThinkPads have
2524 * bugs in the BIOS and may fail to update volume_toggle properly.
2529 oldn
->volume_toggle
!= newn
->volume_toggle
||
2530 oldn
->volume_level
!= newn
->volume_level
) {
2531 /* recently muted, or repeated mute keypress, or
2532 * multiple presses ending in mute */
2533 issue_volchange(oldn
->volume_level
, newn
->volume_level
,
2535 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2540 /* recently unmuted, issue 'unmute' keypress */
2541 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2543 if (oldn
->volume_level
!= newn
->volume_level
) {
2544 issue_volchange(oldn
->volume_level
, newn
->volume_level
,
2546 } else if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
2547 /* repeated vol up/down keypress at end of scale ? */
2548 if (newn
->volume_level
== 0)
2549 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2550 else if (newn
->volume_level
>= TP_NVRAM_LEVEL_VOLUME_MAX
)
2551 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2555 /* handle brightness */
2556 if (oldn
->brightness_level
!= newn
->brightness_level
) {
2557 issue_brightnesschange(oldn
->brightness_level
,
2558 newn
->brightness_level
, event_mask
);
2559 } else if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
2560 /* repeated key presses that didn't change state */
2561 if (newn
->brightness_level
== 0)
2562 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2563 else if (newn
->brightness_level
>= bright_maxlvl
2564 && !tp_features
.bright_unkfw
)
2565 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2568 #undef TPACPI_COMPARE_KEY
2569 #undef TPACPI_MAY_SEND_KEY
2575 * We track all events in hotkey_source_mask all the time, since
2576 * most of them are edge-based. We only issue those requested by
2577 * hotkey_user_mask or hotkey_driver_mask, though.
2579 static int hotkey_kthread(void *data
)
2581 struct tp_nvram_state s
[2];
2582 u32 poll_mask
, event_mask
;
2583 unsigned int si
, so
;
2585 unsigned int change_detector
;
2586 unsigned int poll_freq
;
2589 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
2598 /* Initial state for compares */
2599 mutex_lock(&hotkey_thread_data_mutex
);
2600 change_detector
= hotkey_config_change
;
2601 poll_mask
= hotkey_source_mask
;
2602 event_mask
= hotkey_source_mask
&
2603 (hotkey_driver_mask
| hotkey_user_mask
);
2604 poll_freq
= hotkey_poll_freq
;
2605 mutex_unlock(&hotkey_thread_data_mutex
);
2606 hotkey_read_nvram(&s
[so
], poll_mask
);
2608 while (!kthread_should_stop()) {
2610 if (likely(poll_freq
))
2613 t
= 100; /* should never happen... */
2615 t
= msleep_interruptible(t
);
2616 if (unlikely(kthread_freezable_should_stop(&was_frozen
)))
2619 if (t
> 0 && !was_frozen
)
2622 mutex_lock(&hotkey_thread_data_mutex
);
2623 if (was_frozen
|| hotkey_config_change
!= change_detector
) {
2624 /* forget old state on thaw or config change */
2627 change_detector
= hotkey_config_change
;
2629 poll_mask
= hotkey_source_mask
;
2630 event_mask
= hotkey_source_mask
&
2631 (hotkey_driver_mask
| hotkey_user_mask
);
2632 poll_freq
= hotkey_poll_freq
;
2633 mutex_unlock(&hotkey_thread_data_mutex
);
2635 if (likely(poll_mask
)) {
2636 hotkey_read_nvram(&s
[si
], poll_mask
);
2637 if (likely(si
!= so
)) {
2638 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
2651 /* call with hotkey_mutex held */
2652 static void hotkey_poll_stop_sync(void)
2654 if (tpacpi_hotkey_task
) {
2655 kthread_stop(tpacpi_hotkey_task
);
2656 tpacpi_hotkey_task
= NULL
;
2660 /* call with hotkey_mutex held */
2661 static void hotkey_poll_setup(const bool may_warn
)
2663 const u32 poll_driver_mask
= hotkey_driver_mask
& hotkey_source_mask
;
2664 const u32 poll_user_mask
= hotkey_user_mask
& hotkey_source_mask
;
2666 if (hotkey_poll_freq
> 0 &&
2667 (poll_driver_mask
||
2668 (poll_user_mask
&& tpacpi_inputdev
->users
> 0))) {
2669 if (!tpacpi_hotkey_task
) {
2670 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
2671 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
2672 if (IS_ERR(tpacpi_hotkey_task
)) {
2673 tpacpi_hotkey_task
= NULL
;
2674 pr_err("could not create kernel thread for hotkey polling\n");
2678 hotkey_poll_stop_sync();
2679 if (may_warn
&& (poll_driver_mask
|| poll_user_mask
) &&
2680 hotkey_poll_freq
== 0) {
2681 pr_notice("hot keys 0x%08x and/or events 0x%08x require polling, which is currently disabled\n",
2682 poll_user_mask
, poll_driver_mask
);
2687 static void hotkey_poll_setup_safe(const bool may_warn
)
2689 mutex_lock(&hotkey_mutex
);
2690 hotkey_poll_setup(may_warn
);
2691 mutex_unlock(&hotkey_mutex
);
2694 /* call with hotkey_mutex held */
2695 static void hotkey_poll_set_freq(unsigned int freq
)
2698 hotkey_poll_stop_sync();
2700 hotkey_poll_freq
= freq
;
2703 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2705 static void hotkey_poll_setup(const bool __unused
)
2709 static void hotkey_poll_setup_safe(const bool __unused
)
2713 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2715 static int hotkey_inputdev_open(struct input_dev
*dev
)
2717 switch (tpacpi_lifecycle
) {
2718 case TPACPI_LIFE_INIT
:
2719 case TPACPI_LIFE_RUNNING
:
2720 hotkey_poll_setup_safe(false);
2722 case TPACPI_LIFE_EXITING
:
2726 /* Should only happen if tpacpi_lifecycle is corrupt */
2731 static void hotkey_inputdev_close(struct input_dev
*dev
)
2733 /* disable hotkey polling when possible */
2734 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
&&
2735 !(hotkey_source_mask
& hotkey_driver_mask
))
2736 hotkey_poll_setup_safe(false);
2739 /* sysfs hotkey enable ------------------------------------------------- */
2740 static ssize_t
hotkey_enable_show(struct device
*dev
,
2741 struct device_attribute
*attr
,
2746 printk_deprecated_attribute("hotkey_enable",
2747 "Hotkey reporting is always enabled");
2749 res
= hotkey_status_get(&status
);
2753 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
2756 static ssize_t
hotkey_enable_store(struct device
*dev
,
2757 struct device_attribute
*attr
,
2758 const char *buf
, size_t count
)
2762 printk_deprecated_attribute("hotkey_enable",
2763 "Hotkeys can be disabled through hotkey_mask");
2765 if (parse_strtoul(buf
, 1, &t
))
2774 static DEVICE_ATTR_RW(hotkey_enable
);
2776 /* sysfs hotkey mask --------------------------------------------------- */
2777 static ssize_t
hotkey_mask_show(struct device
*dev
,
2778 struct device_attribute
*attr
,
2781 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_user_mask
);
2784 static ssize_t
hotkey_mask_store(struct device
*dev
,
2785 struct device_attribute
*attr
,
2786 const char *buf
, size_t count
)
2791 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2794 if (mutex_lock_killable(&hotkey_mutex
))
2795 return -ERESTARTSYS
;
2797 res
= hotkey_user_mask_set(t
);
2799 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2800 hotkey_poll_setup(true);
2803 mutex_unlock(&hotkey_mutex
);
2805 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2807 return (res
) ? res
: count
;
2810 static DEVICE_ATTR_RW(hotkey_mask
);
2812 /* sysfs hotkey bios_enabled ------------------------------------------- */
2813 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2814 struct device_attribute
*attr
,
2817 return sprintf(buf
, "0\n");
2820 static DEVICE_ATTR_RO(hotkey_bios_enabled
);
2822 /* sysfs hotkey bios_mask ---------------------------------------------- */
2823 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2824 struct device_attribute
*attr
,
2827 printk_deprecated_attribute("hotkey_bios_mask",
2828 "This attribute is useless.");
2829 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2832 static DEVICE_ATTR_RO(hotkey_bios_mask
);
2834 /* sysfs hotkey all_mask ----------------------------------------------- */
2835 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2836 struct device_attribute
*attr
,
2839 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2840 hotkey_all_mask
| hotkey_source_mask
);
2843 static DEVICE_ATTR_RO(hotkey_all_mask
);
2845 /* sysfs hotkey all_mask ----------------------------------------------- */
2846 static ssize_t
hotkey_adaptive_all_mask_show(struct device
*dev
,
2847 struct device_attribute
*attr
,
2850 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2851 hotkey_adaptive_all_mask
| hotkey_source_mask
);
2854 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask
);
2856 /* sysfs hotkey recommended_mask --------------------------------------- */
2857 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2858 struct device_attribute
*attr
,
2861 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2862 (hotkey_all_mask
| hotkey_source_mask
)
2863 & ~hotkey_reserved_mask
);
2866 static DEVICE_ATTR_RO(hotkey_recommended_mask
);
2868 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2870 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2871 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2872 struct device_attribute
*attr
,
2875 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2878 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2879 struct device_attribute
*attr
,
2880 const char *buf
, size_t count
)
2886 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2887 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2890 if (mutex_lock_killable(&hotkey_mutex
))
2891 return -ERESTARTSYS
;
2893 HOTKEY_CONFIG_CRITICAL_START
2894 hotkey_source_mask
= t
;
2895 HOTKEY_CONFIG_CRITICAL_END
2897 rc
= hotkey_mask_set((hotkey_user_mask
| hotkey_driver_mask
) &
2898 ~hotkey_source_mask
);
2899 hotkey_poll_setup(true);
2901 /* check if events needed by the driver got disabled */
2902 r_ev
= hotkey_driver_mask
& ~(hotkey_acpi_mask
& hotkey_all_mask
)
2903 & ~hotkey_source_mask
& TPACPI_HKEY_NVRAM_KNOWN_MASK
;
2905 mutex_unlock(&hotkey_mutex
);
2908 pr_err("hotkey_source_mask: failed to update the firmware event mask!\n");
2911 pr_notice("hotkey_source_mask: some important events were disabled: 0x%04x\n",
2914 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2916 return (rc
< 0) ? rc
: count
;
2919 static DEVICE_ATTR_RW(hotkey_source_mask
);
2921 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2922 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2923 struct device_attribute
*attr
,
2926 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2929 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2930 struct device_attribute
*attr
,
2931 const char *buf
, size_t count
)
2935 if (parse_strtoul(buf
, 25, &t
))
2938 if (mutex_lock_killable(&hotkey_mutex
))
2939 return -ERESTARTSYS
;
2941 hotkey_poll_set_freq(t
);
2942 hotkey_poll_setup(true);
2944 mutex_unlock(&hotkey_mutex
);
2946 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2951 static DEVICE_ATTR_RW(hotkey_poll_freq
);
2953 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2955 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2956 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2957 struct device_attribute
*attr
,
2961 res
= hotkey_get_wlsw();
2965 /* Opportunistic update */
2966 tpacpi_rfk_update_hwblock_state((res
== TPACPI_RFK_RADIO_OFF
));
2968 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2969 (res
== TPACPI_RFK_RADIO_OFF
) ? 0 : 1);
2972 static DEVICE_ATTR_RO(hotkey_radio_sw
);
2974 static void hotkey_radio_sw_notify_change(void)
2976 if (tp_features
.hotkey_wlsw
)
2977 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2981 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2982 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2983 struct device_attribute
*attr
,
2987 res
= hotkey_get_tablet_mode(&s
);
2991 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2994 static DEVICE_ATTR_RO(hotkey_tablet_mode
);
2996 static void hotkey_tablet_mode_notify_change(void)
2998 if (tp_features
.hotkey_tablet
)
2999 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
3000 "hotkey_tablet_mode");
3003 /* sysfs wakeup reason (pollable) -------------------------------------- */
3004 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
3005 struct device_attribute
*attr
,
3008 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
3011 static DEVICE_ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
3013 static void hotkey_wakeup_reason_notify_change(void)
3015 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
3019 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
3020 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
3021 struct device_attribute
*attr
,
3024 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
3027 static DEVICE_ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
3028 hotkey_wakeup_hotunplug_complete_show
, NULL
);
3030 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
3032 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
3033 "wakeup_hotunplug_complete");
3036 /* sysfs adaptive kbd mode --------------------------------------------- */
3038 static int adaptive_keyboard_get_mode(void);
3039 static int adaptive_keyboard_set_mode(int new_mode
);
3041 enum ADAPTIVE_KEY_MODE
{
3044 WEB_CONFERENCE_MODE
,
3049 static ssize_t
adaptive_kbd_mode_show(struct device
*dev
,
3050 struct device_attribute
*attr
,
3055 current_mode
= adaptive_keyboard_get_mode();
3056 if (current_mode
< 0)
3057 return current_mode
;
3059 return snprintf(buf
, PAGE_SIZE
, "%d\n", current_mode
);
3062 static ssize_t
adaptive_kbd_mode_store(struct device
*dev
,
3063 struct device_attribute
*attr
,
3064 const char *buf
, size_t count
)
3069 if (parse_strtoul(buf
, LAYFLAT_MODE
, &t
))
3072 res
= adaptive_keyboard_set_mode(t
);
3073 return (res
< 0) ? res
: count
;
3076 static DEVICE_ATTR_RW(adaptive_kbd_mode
);
3078 static struct attribute
*adaptive_kbd_attributes
[] = {
3079 &dev_attr_adaptive_kbd_mode
.attr
,
3083 static const struct attribute_group adaptive_kbd_attr_group
= {
3084 .attrs
= adaptive_kbd_attributes
,
3087 /* --------------------------------------------------------------------- */
3089 static struct attribute
*hotkey_attributes
[] __initdata
= {
3090 &dev_attr_hotkey_enable
.attr
,
3091 &dev_attr_hotkey_bios_enabled
.attr
,
3092 &dev_attr_hotkey_bios_mask
.attr
,
3093 &dev_attr_wakeup_reason
.attr
,
3094 &dev_attr_wakeup_hotunplug_complete
.attr
,
3095 &dev_attr_hotkey_mask
.attr
,
3096 &dev_attr_hotkey_all_mask
.attr
,
3097 &dev_attr_hotkey_adaptive_all_mask
.attr
,
3098 &dev_attr_hotkey_recommended_mask
.attr
,
3099 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3100 &dev_attr_hotkey_source_mask
.attr
,
3101 &dev_attr_hotkey_poll_freq
.attr
,
3106 * Sync both the hw and sw blocking state of all switches
3108 static void tpacpi_send_radiosw_update(void)
3113 * We must sync all rfkill controllers *before* issuing any
3114 * rfkill input events, or we will race the rfkill core input
3117 * tpacpi_inputdev_send_mutex works as a synchronization point
3120 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3123 wlsw
= hotkey_get_wlsw();
3125 /* Sync hw blocking state first if it is hw-blocked */
3126 if (wlsw
== TPACPI_RFK_RADIO_OFF
)
3127 tpacpi_rfk_update_hwblock_state(true);
3129 /* Sync sw blocking state */
3130 tpacpi_rfk_update_swstate_all();
3132 /* Sync hw blocking state last if it is hw-unblocked */
3133 if (wlsw
== TPACPI_RFK_RADIO_ON
)
3134 tpacpi_rfk_update_hwblock_state(false);
3136 /* Issue rfkill input event for WLSW switch */
3138 mutex_lock(&tpacpi_inputdev_send_mutex
);
3140 input_report_switch(tpacpi_inputdev
,
3141 SW_RFKILL_ALL
, (wlsw
> 0));
3142 input_sync(tpacpi_inputdev
);
3144 mutex_unlock(&tpacpi_inputdev_send_mutex
);
3148 * this can be unconditional, as we will poll state again
3149 * if userspace uses the notify to read data
3151 hotkey_radio_sw_notify_change();
3154 static void hotkey_exit(void)
3156 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3157 mutex_lock(&hotkey_mutex
);
3158 hotkey_poll_stop_sync();
3159 mutex_unlock(&hotkey_mutex
);
3162 if (hotkey_dev_attributes
)
3163 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3165 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
3166 "restoring original HKEY status and mask\n");
3167 /* yes, there is a bitwise or below, we want the
3168 * functions to be called even if one of them fail */
3169 if (((tp_features
.hotkey_mask
&&
3170 hotkey_mask_set(hotkey_orig_mask
)) |
3171 hotkey_status_set(false)) != 0)
3172 pr_err("failed to restore hot key mask to BIOS defaults\n");
3175 static void __init
hotkey_unmap(const unsigned int scancode
)
3177 if (hotkey_keycode_map
[scancode
] != KEY_RESERVED
) {
3178 clear_bit(hotkey_keycode_map
[scancode
],
3179 tpacpi_inputdev
->keybit
);
3180 hotkey_keycode_map
[scancode
] = KEY_RESERVED
;
3186 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3189 #define TPACPI_HK_Q_INIMASK 0x0001
3191 static const struct tpacpi_quirk tpacpi_hotkey_qtable
[] __initconst
= {
3192 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK
), /* 600E */
3193 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK
), /* 600E */
3194 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK
), /* 770, 770E, 770ED */
3195 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK
), /* A20m */
3196 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK
), /* A20p */
3197 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK
), /* A21e, A22e */
3198 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK
), /* A21e */
3199 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK
), /* A21m, A22m */
3200 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK
), /* A21p, A22p */
3201 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK
), /* A22e */
3202 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK
), /* A22m */
3203 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK
), /* A30/p (0) */
3204 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK
), /* R30 */
3205 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK
), /* R31 */
3206 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK
), /* T20 */
3207 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK
), /* T21 */
3208 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK
), /* T22 */
3209 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK
), /* X20, X21 */
3210 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK
), /* X22, X23, X24 */
3213 typedef u16 tpacpi_keymap_entry_t
;
3214 typedef tpacpi_keymap_entry_t tpacpi_keymap_t
[TPACPI_HOTKEY_MAP_LEN
];
3216 static int hotkey_init_tablet_mode(void)
3218 int in_tablet_mode
= 0, res
;
3221 if (acpi_evalf(hkey_handle
, &res
, "GMMS", "qdd", 0)) {
3222 int has_tablet_mode
;
3224 in_tablet_mode
= hotkey_gmms_get_tablet_mode(res
,
3226 if (has_tablet_mode
)
3227 tp_features
.hotkey_tablet
= TP_HOTKEY_TABLET_USES_GMMS
;
3229 } else if (acpi_evalf(hkey_handle
, &res
, "MHKG", "qd")) {
3230 /* For X41t, X60t, X61t Tablets... */
3231 tp_features
.hotkey_tablet
= TP_HOTKEY_TABLET_USES_MHKG
;
3232 in_tablet_mode
= !!(res
& TP_HOTKEY_TABLET_MASK
);
3236 if (!tp_features
.hotkey_tablet
)
3239 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3240 type
, in_tablet_mode
? "tablet" : "laptop");
3242 res
= add_to_attr_set(hotkey_dev_attributes
,
3243 &dev_attr_hotkey_tablet_mode
.attr
);
3247 return in_tablet_mode
;
3250 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
3252 /* Requirements for changing the default keymaps:
3254 * 1. Many of the keys are mapped to KEY_RESERVED for very
3255 * good reasons. Do not change them unless you have deep
3256 * knowledge on the IBM and Lenovo ThinkPad firmware for
3257 * the various ThinkPad models. The driver behaves
3258 * differently for KEY_RESERVED: such keys have their
3259 * hot key mask *unset* in mask_recommended, and also
3260 * in the initial hot key mask programmed into the
3261 * firmware at driver load time, which means the firm-
3262 * ware may react very differently if you change them to
3265 * 2. You must be subscribed to the linux-thinkpad and
3266 * ibm-acpi-devel mailing lists, and you should read the
3267 * list archives since 2007 if you want to change the
3268 * keymaps. This requirement exists so that you will
3269 * know the past history of problems with the thinkpad-
3270 * acpi driver keymaps, and also that you will be
3271 * listening to any bug reports;
3273 * 3. Do not send thinkpad-acpi specific patches directly to
3274 * for merging, *ever*. Send them to the linux-acpi
3275 * mailinglist for comments. Merging is to be done only
3276 * through acpi-test and the ACPI maintainer.
3278 * If the above is too much to ask, don't change the keymap.
3279 * Ask the thinkpad-acpi maintainer to do it, instead.
3283 TPACPI_KEYMAP_IBM_GENERIC
= 0,
3284 TPACPI_KEYMAP_LENOVO_GENERIC
,
3287 static const tpacpi_keymap_t tpacpi_keymaps
[] __initconst
= {
3288 /* Generic keymap for IBM ThinkPads */
3289 [TPACPI_KEYMAP_IBM_GENERIC
] = {
3290 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3291 KEY_FN_F1
, KEY_BATTERY
, KEY_COFFEE
, KEY_SLEEP
,
3292 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3293 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3295 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3296 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3297 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3298 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3300 /* brightness: firmware always reacts to them */
3301 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
3302 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
3304 /* Thinklight: firmware always react to it */
3305 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3307 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3308 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3310 /* Volume: firmware always react to it and reprograms
3311 * the built-in *extra* mixer. Never map it to control
3312 * another mixer by default. */
3313 KEY_RESERVED
, /* 0x14: VOLUME UP */
3314 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3315 KEY_RESERVED
, /* 0x16: MUTE */
3317 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3319 /* (assignments unknown, please report if found) */
3320 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3321 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3323 /* No assignments, only used for Adaptive keyboards. */
3324 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3325 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3326 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3327 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3328 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3330 /* No assignment, used for newer Lenovo models */
3331 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3332 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3333 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3334 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3335 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3336 KEY_UNKNOWN
, KEY_UNKNOWN
3340 /* Generic keymap for Lenovo ThinkPads */
3341 [TPACPI_KEYMAP_LENOVO_GENERIC
] = {
3342 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3343 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
3344 KEY_WLAN
, KEY_CAMERA
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3345 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3347 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3348 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3349 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3350 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3352 /* These should be enabled --only-- when ACPI video
3353 * is disabled (i.e. in "vendor" mode), and are handled
3354 * in a special way by the init code */
3355 KEY_BRIGHTNESSUP
, /* 0x0F: FN+HOME (brightness up) */
3356 KEY_BRIGHTNESSDOWN
, /* 0x10: FN+END (brightness down) */
3358 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3360 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3361 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3363 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3364 * react to it and reprograms the built-in *extra* mixer.
3365 * Never map it to control another mixer by default.
3367 * T60?, T61, R60?, R61: firmware and EC tries to send
3368 * these over the regular keyboard, so these are no-ops,
3369 * but there are still weird bugs re. MUTE, so do not
3370 * change unless you get test reports from all Lenovo
3371 * models. May cause the BIOS to interfere with the
3374 KEY_RESERVED
, /* 0x14: VOLUME UP */
3375 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3376 KEY_RESERVED
, /* 0x16: MUTE */
3378 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3380 /* (assignments unknown, please report if found) */
3381 KEY_UNKNOWN
, KEY_UNKNOWN
,
3384 * The mic mute button only sends 0x1a. It does not
3385 * automatically mute the mic or change the mute light.
3387 KEY_MICMUTE
, /* 0x1a: Mic mute (since ?400 or so) */
3389 /* (assignments unknown, please report if found) */
3392 /* Extra keys in use since the X240 / T440 / T540 */
3393 KEY_CONFIG
, KEY_SEARCH
, KEY_SCALE
, KEY_FILE
,
3396 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3397 * The first item in this list is the Mute button which is
3398 * emitted with 0x103 through
3399 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3401 * We'll need to offset those by 0x20.
3403 KEY_RESERVED
, /* Mute held, 0x103 */
3404 KEY_BRIGHTNESS_MIN
, /* Backlight off */
3405 KEY_RESERVED
, /* Clipping tool */
3406 KEY_RESERVED
, /* Cloud */
3408 KEY_VOICECOMMAND
, /* Voice */
3410 KEY_RESERVED
, /* Gestures */
3414 KEY_CONFIG
, /* Settings */
3415 KEY_RESERVED
, /* New tab */
3416 KEY_REFRESH
, /* Reload */
3417 KEY_BACK
, /* Back */
3418 KEY_RESERVED
, /* Microphone down */
3419 KEY_RESERVED
, /* Microphone up */
3420 KEY_RESERVED
, /* Microphone cancellation */
3421 KEY_RESERVED
, /* Camera mode */
3422 KEY_RESERVED
, /* Rotate display, 0x116 */
3425 * These are found in 2017 models (e.g. T470s, X270).
3426 * The lowest known value is 0x311, which according to
3427 * the manual should launch a user defined favorite
3430 * The offset for these is TP_ACPI_HOTKEYSCAN_EXTENDED_START,
3431 * corresponding to 0x34.
3434 /* (assignments unknown, please report if found) */
3435 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3436 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3437 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3438 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3441 KEY_FAVORITES
, /* Favorite app, 0x311 */
3442 KEY_RESERVED
, /* Clipping tool */
3444 KEY_BLUETOOTH
, /* Bluetooth */
3445 KEY_KEYBOARD
/* Keyboard, 0x315 */
3449 static const struct tpacpi_quirk tpacpi_keymap_qtable
[] __initconst
= {
3450 /* Generic maps (fallback) */
3452 .vendor
= PCI_VENDOR_ID_IBM
,
3453 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
3454 .quirks
= TPACPI_KEYMAP_IBM_GENERIC
,
3457 .vendor
= PCI_VENDOR_ID_LENOVO
,
3458 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
3459 .quirks
= TPACPI_KEYMAP_LENOVO_GENERIC
,
3463 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3464 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3469 bool radiosw_state
= false;
3470 bool tabletsw_state
= false;
3472 unsigned long quirks
;
3473 unsigned long keymap_id
;
3475 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3476 "initializing hotkey subdriver\n");
3478 BUG_ON(!tpacpi_inputdev
);
3479 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
3480 tpacpi_inputdev
->close
!= NULL
);
3482 TPACPI_ACPIHANDLE_INIT(hkey
);
3483 mutex_init(&hotkey_mutex
);
3485 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3486 mutex_init(&hotkey_thread_data_mutex
);
3489 /* hotkey not supported on 570 */
3490 tp_features
.hotkey
= hkey_handle
!= NULL
;
3492 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3494 str_supported(tp_features
.hotkey
));
3496 if (!tp_features
.hotkey
)
3499 quirks
= tpacpi_check_quirks(tpacpi_hotkey_qtable
,
3500 ARRAY_SIZE(tpacpi_hotkey_qtable
));
3502 tpacpi_disable_brightness_delay();
3504 /* MUST have enough space for all attributes to be added to
3505 * hotkey_dev_attributes */
3506 hotkey_dev_attributes
= create_attr_set(
3507 ARRAY_SIZE(hotkey_attributes
) + 2,
3509 if (!hotkey_dev_attributes
)
3511 res
= add_many_to_attr_set(hotkey_dev_attributes
,
3513 ARRAY_SIZE(hotkey_attributes
));
3517 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3518 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3519 for HKEY interface version 0x100 */
3520 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
3521 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3522 "firmware HKEY interface version: 0x%x\n",
3525 switch (hkeyv
>> 8) {
3528 * MHKV 0x100 in A31, R40, R40e,
3529 * T4x, X31, and later
3532 /* Paranoia check AND init hotkey_all_mask */
3533 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
3535 pr_err("missing MHKA handler, please report this to %s\n",
3537 /* Fallback: pre-init for FN+F3,F4,F12 */
3538 hotkey_all_mask
= 0x080cU
;
3540 tp_features
.hotkey_mask
= 1;
3546 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3549 /* Paranoia check AND init hotkey_all_mask */
3550 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
3552 pr_err("missing MHKA handler, please report this to %s\n",
3554 /* Fallback: pre-init for FN+F3,F4,F12 */
3555 hotkey_all_mask
= 0x080cU
;
3557 tp_features
.hotkey_mask
= 1;
3561 * Check if we have an adaptive keyboard, like on the
3562 * Lenovo Carbon X1 2014 (2nd Gen).
3564 if (acpi_evalf(hkey_handle
, &hotkey_adaptive_all_mask
,
3566 if (hotkey_adaptive_all_mask
!= 0) {
3567 tp_features
.has_adaptive_kbd
= true;
3568 res
= sysfs_create_group(
3569 &tpacpi_pdev
->dev
.kobj
,
3570 &adaptive_kbd_attr_group
);
3575 tp_features
.has_adaptive_kbd
= false;
3576 hotkey_adaptive_all_mask
= 0x0U
;
3581 pr_err("unknown version of the HKEY interface: 0x%x\n",
3583 pr_err("please report this to %s\n", TPACPI_MAIL
);
3588 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3589 "hotkey masks are %s\n",
3590 str_supported(tp_features
.hotkey_mask
));
3592 /* Init hotkey_all_mask if not initialized yet */
3593 if (!tp_features
.hotkey_mask
&& !hotkey_all_mask
&&
3594 (quirks
& TPACPI_HK_Q_INIMASK
))
3595 hotkey_all_mask
= 0x080cU
; /* FN+F12, FN+F4, FN+F3 */
3597 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3598 if (tp_features
.hotkey_mask
) {
3599 /* hotkey_source_mask *must* be zero for
3600 * the first hotkey_mask_get to return hotkey_orig_mask */
3601 res
= hotkey_mask_get();
3605 hotkey_orig_mask
= hotkey_acpi_mask
;
3607 hotkey_orig_mask
= hotkey_all_mask
;
3608 hotkey_acpi_mask
= hotkey_all_mask
;
3611 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3613 tp_features
.hotkey_wlsw
= 1;
3614 radiosw_state
= !!tpacpi_wlsw_emulstate
;
3615 pr_info("radio switch emulation enabled\n");
3618 /* Not all thinkpads have a hardware radio switch */
3619 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
3620 tp_features
.hotkey_wlsw
= 1;
3621 radiosw_state
= !!status
;
3622 pr_info("radio switch found; radios are %s\n",
3623 enabled(status
, 0));
3625 if (tp_features
.hotkey_wlsw
)
3626 res
= add_to_attr_set(hotkey_dev_attributes
,
3627 &dev_attr_hotkey_radio_sw
.attr
);
3629 res
= hotkey_init_tablet_mode();
3633 tabletsw_state
= res
;
3635 res
= register_attr_set_with_sysfs(hotkey_dev_attributes
,
3636 &tpacpi_pdev
->dev
.kobj
);
3640 /* Set up key map */
3641 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
3643 if (!hotkey_keycode_map
) {
3644 pr_err("failed to allocate memory for key map\n");
3649 keymap_id
= tpacpi_check_quirks(tpacpi_keymap_qtable
,
3650 ARRAY_SIZE(tpacpi_keymap_qtable
));
3651 BUG_ON(keymap_id
>= ARRAY_SIZE(tpacpi_keymaps
));
3652 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3653 "using keymap number %lu\n", keymap_id
);
3655 memcpy(hotkey_keycode_map
, &tpacpi_keymaps
[keymap_id
],
3656 TPACPI_HOTKEY_MAP_SIZE
);
3658 input_set_capability(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
);
3659 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
3660 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
3661 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
3662 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
3663 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
3664 input_set_capability(tpacpi_inputdev
, EV_KEY
,
3665 hotkey_keycode_map
[i
]);
3667 if (i
< sizeof(hotkey_reserved_mask
)*8)
3668 hotkey_reserved_mask
|= 1 << i
;
3672 if (tp_features
.hotkey_wlsw
) {
3673 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_RFKILL_ALL
);
3674 input_report_switch(tpacpi_inputdev
,
3675 SW_RFKILL_ALL
, radiosw_state
);
3677 if (tp_features
.hotkey_tablet
) {
3678 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_TABLET_MODE
);
3679 input_report_switch(tpacpi_inputdev
,
3680 SW_TABLET_MODE
, tabletsw_state
);
3683 /* Do not issue duplicate brightness change events to
3684 * userspace. tpacpi_detect_brightness_capabilities() must have
3685 * been called before this point */
3686 if (acpi_video_get_backlight_type() != acpi_backlight_vendor
) {
3687 pr_info("This ThinkPad has standard ACPI backlight brightness control, supported by the ACPI video driver\n");
3688 pr_notice("Disabling thinkpad-acpi brightness events by default...\n");
3690 /* Disable brightness up/down on Lenovo thinkpads when
3691 * ACPI is handling them, otherwise it is plain impossible
3692 * for userspace to do something even remotely sane */
3693 hotkey_reserved_mask
|=
3694 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
3695 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
3696 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME
);
3697 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND
);
3700 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3701 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
3703 & ~hotkey_reserved_mask
;
3705 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3706 "hotkey source mask 0x%08x, polling freq %u\n",
3707 hotkey_source_mask
, hotkey_poll_freq
);
3710 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3711 "enabling firmware HKEY event interface...\n");
3712 res
= hotkey_status_set(true);
3717 res
= hotkey_mask_set(((hotkey_all_mask
& ~hotkey_reserved_mask
)
3718 | hotkey_driver_mask
)
3719 & ~hotkey_source_mask
);
3720 if (res
< 0 && res
!= -ENXIO
) {
3724 hotkey_user_mask
= (hotkey_acpi_mask
| hotkey_source_mask
)
3725 & ~hotkey_reserved_mask
;
3726 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3727 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3728 hotkey_user_mask
, hotkey_acpi_mask
, hotkey_source_mask
);
3730 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
3731 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
3733 hotkey_poll_setup_safe(true);
3738 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3739 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3740 &adaptive_kbd_attr_group
);
3742 hotkey_dev_attributes
= NULL
;
3744 return (res
< 0) ? res
: 1;
3747 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3748 * mode, Web conference mode, Function mode and Lay-flat mode.
3749 * We support Home mode and Function mode currently.
3751 * Will consider support rest of modes in future.
3754 static const int adaptive_keyboard_modes
[] = {
3756 /* WEB_BROWSER_MODE = 2,
3757 WEB_CONFERENCE_MODE = 3, */
3761 #define DFR_CHANGE_ROW 0x101
3762 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3763 #define FIRST_ADAPTIVE_KEY 0x103
3765 /* press Fn key a while second, it will switch to Function Mode. Then
3766 * release Fn key, previous mode be restored.
3768 static bool adaptive_keyboard_mode_is_saved
;
3769 static int adaptive_keyboard_prev_mode
;
3771 static int adaptive_keyboard_get_mode(void)
3775 if (!acpi_evalf(hkey_handle
, &mode
, "GTRW", "dd", 0)) {
3776 pr_err("Cannot read adaptive keyboard mode\n");
3783 static int adaptive_keyboard_set_mode(int new_mode
)
3786 new_mode
> LAYFLAT_MODE
)
3789 if (!acpi_evalf(hkey_handle
, NULL
, "STRW", "vd", new_mode
)) {
3790 pr_err("Cannot set adaptive keyboard mode\n");
3797 static int adaptive_keyboard_get_next_mode(int mode
)
3800 size_t max_mode
= ARRAY_SIZE(adaptive_keyboard_modes
) - 1;
3802 for (i
= 0; i
<= max_mode
; i
++) {
3803 if (adaptive_keyboard_modes
[i
] == mode
)
3812 return adaptive_keyboard_modes
[i
];
3815 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode
)
3817 int current_mode
= 0;
3822 case DFR_CHANGE_ROW
:
3823 if (adaptive_keyboard_mode_is_saved
) {
3824 new_mode
= adaptive_keyboard_prev_mode
;
3825 adaptive_keyboard_mode_is_saved
= false;
3827 current_mode
= adaptive_keyboard_get_mode();
3828 if (current_mode
< 0)
3830 new_mode
= adaptive_keyboard_get_next_mode(
3834 if (adaptive_keyboard_set_mode(new_mode
) < 0)
3839 case DFR_SHOW_QUICKVIEW_ROW
:
3840 current_mode
= adaptive_keyboard_get_mode();
3841 if (current_mode
< 0)
3844 adaptive_keyboard_prev_mode
= current_mode
;
3845 adaptive_keyboard_mode_is_saved
= true;
3847 if (adaptive_keyboard_set_mode (FUNCTION_MODE
) < 0)
3852 if (scancode
< FIRST_ADAPTIVE_KEY
||
3853 scancode
>= FIRST_ADAPTIVE_KEY
+
3854 TP_ACPI_HOTKEYSCAN_EXTENDED_START
-
3855 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START
) {
3856 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3860 keycode
= hotkey_keycode_map
[scancode
- FIRST_ADAPTIVE_KEY
+
3861 TP_ACPI_HOTKEYSCAN_ADAPTIVE_START
];
3862 if (keycode
!= KEY_RESERVED
) {
3863 mutex_lock(&tpacpi_inputdev_send_mutex
);
3865 input_report_key(tpacpi_inputdev
, keycode
, 1);
3866 input_sync(tpacpi_inputdev
);
3868 input_report_key(tpacpi_inputdev
, keycode
, 0);
3869 input_sync(tpacpi_inputdev
);
3871 mutex_unlock(&tpacpi_inputdev_send_mutex
);
3877 static bool hotkey_notify_hotkey(const u32 hkey
,
3879 bool *ignore_acpi_ev
)
3881 /* 0x1000-0x1FFF: key presses */
3882 unsigned int scancode
= hkey
& 0xfff;
3883 *send_acpi_ev
= true;
3884 *ignore_acpi_ev
= false;
3887 * Original events are in the 0x10XX range, the adaptive keyboard
3888 * found in 2014 X1 Carbon emits events are of 0x11XX. In 2017
3889 * models, additional keys are emitted through 0x13XX.
3891 switch ((hkey
>> 8) & 0xf) {
3894 scancode
<= TP_ACPI_HOTKEYSCAN_ADAPTIVE_START
) {
3895 /* HKEY event 0x1001 is scancode 0x00 */
3897 if (!(hotkey_source_mask
& (1 << scancode
))) {
3898 tpacpi_input_send_key_masked(scancode
);
3899 *send_acpi_ev
= false;
3901 *ignore_acpi_ev
= true;
3908 return adaptive_keyboard_hotkey_notify_hotkey(scancode
);
3911 /* Extended keycodes start at 0x300 and our offset into the map
3912 * TP_ACPI_HOTKEYSCAN_EXTENDED_START. The calculated scancode
3913 * will be positive, but might not be in the correct range.
3915 scancode
-= (0x300 - TP_ACPI_HOTKEYSCAN_EXTENDED_START
);
3916 if (scancode
>= TP_ACPI_HOTKEYSCAN_EXTENDED_START
&&
3917 scancode
< TPACPI_HOTKEY_MAP_LEN
) {
3918 tpacpi_input_send_key(scancode
);
3927 static bool hotkey_notify_wakeup(const u32 hkey
,
3929 bool *ignore_acpi_ev
)
3931 /* 0x2000-0x2FFF: Wakeup reason */
3932 *send_acpi_ev
= true;
3933 *ignore_acpi_ev
= false;
3936 case TP_HKEY_EV_WKUP_S3_UNDOCK
: /* suspend, undock */
3937 case TP_HKEY_EV_WKUP_S4_UNDOCK
: /* hibernation, undock */
3938 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
3939 *ignore_acpi_ev
= true;
3942 case TP_HKEY_EV_WKUP_S3_BAYEJ
: /* suspend, bay eject */
3943 case TP_HKEY_EV_WKUP_S4_BAYEJ
: /* hibernation, bay eject */
3944 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
3945 *ignore_acpi_ev
= true;
3948 case TP_HKEY_EV_WKUP_S3_BATLOW
: /* Battery on critical low level/S3 */
3949 case TP_HKEY_EV_WKUP_S4_BATLOW
: /* Battery on critical low level/S4 */
3950 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3951 /* how to auto-heal: */
3952 /* 2313: woke up from S3, go to S4/S5 */
3953 /* 2413: woke up from S4, go to S5 */
3960 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
3961 pr_info("woke up due to a hot-unplug request...\n");
3962 hotkey_wakeup_reason_notify_change();
3967 static bool hotkey_notify_dockevent(const u32 hkey
,
3969 bool *ignore_acpi_ev
)
3971 /* 0x4000-0x4FFF: dock-related events */
3972 *send_acpi_ev
= true;
3973 *ignore_acpi_ev
= false;
3976 case TP_HKEY_EV_UNDOCK_ACK
:
3977 /* ACPI undock operation completed after wakeup */
3978 hotkey_autosleep_ack
= 1;
3979 pr_info("undocked\n");
3980 hotkey_wakeup_hotunplug_complete_notify_change();
3983 case TP_HKEY_EV_HOTPLUG_DOCK
: /* docked to port replicator */
3984 pr_info("docked into hotplug port replicator\n");
3986 case TP_HKEY_EV_HOTPLUG_UNDOCK
: /* undocked from port replicator */
3987 pr_info("undocked from hotplug port replicator\n");
3995 static bool hotkey_notify_usrevent(const u32 hkey
,
3997 bool *ignore_acpi_ev
)
3999 /* 0x5000-0x5FFF: human interface helpers */
4000 *send_acpi_ev
= true;
4001 *ignore_acpi_ev
= false;
4004 case TP_HKEY_EV_PEN_INSERTED
: /* X61t: tablet pen inserted into bay */
4005 case TP_HKEY_EV_PEN_REMOVED
: /* X61t: tablet pen removed from bay */
4008 case TP_HKEY_EV_TABLET_TABLET
: /* X41t-X61t: tablet mode */
4009 case TP_HKEY_EV_TABLET_NOTEBOOK
: /* X41t-X61t: normal mode */
4010 tpacpi_input_send_tabletsw();
4011 hotkey_tablet_mode_notify_change();
4012 *send_acpi_ev
= false;
4015 case TP_HKEY_EV_LID_CLOSE
: /* Lid closed */
4016 case TP_HKEY_EV_LID_OPEN
: /* Lid opened */
4017 case TP_HKEY_EV_BRGHT_CHANGED
: /* brightness changed */
4018 /* do not propagate these events */
4019 *ignore_acpi_ev
= true;
4027 static void thermal_dump_all_sensors(void);
4029 static bool hotkey_notify_6xxx(const u32 hkey
,
4031 bool *ignore_acpi_ev
)
4035 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
4036 *send_acpi_ev
= true;
4037 *ignore_acpi_ev
= false;
4040 case TP_HKEY_EV_THM_TABLE_CHANGED
:
4041 pr_info("EC reports that Thermal Table has changed\n");
4042 /* recommended action: do nothing, we don't have
4043 * Lenovo ATM information */
4045 case TP_HKEY_EV_ALARM_BAT_HOT
:
4046 pr_crit("THERMAL ALARM: battery is too hot!\n");
4047 /* recommended action: warn user through gui */
4049 case TP_HKEY_EV_ALARM_BAT_XHOT
:
4050 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
4051 /* recommended action: immediate sleep/hibernate */
4053 case TP_HKEY_EV_ALARM_SENSOR_HOT
:
4054 pr_crit("THERMAL ALARM: a sensor reports something is too hot!\n");
4055 /* recommended action: warn user through gui, that */
4056 /* some internal component is too hot */
4058 case TP_HKEY_EV_ALARM_SENSOR_XHOT
:
4059 pr_alert("THERMAL EMERGENCY: a sensor reports something is extremely hot!\n");
4060 /* recommended action: immediate sleep/hibernate */
4062 case TP_HKEY_EV_AC_CHANGED
:
4063 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
4064 * AC status changed; can be triggered by plugging or
4065 * unplugging AC adapter, docking or undocking. */
4069 case TP_HKEY_EV_KEY_NUMLOCK
:
4070 case TP_HKEY_EV_KEY_FN
:
4071 case TP_HKEY_EV_KEY_FN_ESC
:
4072 /* key press events, we just ignore them as long as the EC
4073 * is still reporting them in the normal keyboard stream */
4074 *send_acpi_ev
= false;
4075 *ignore_acpi_ev
= true;
4078 case TP_HKEY_EV_TABLET_CHANGED
:
4079 tpacpi_input_send_tabletsw();
4080 hotkey_tablet_mode_notify_change();
4081 *send_acpi_ev
= false;
4084 case TP_HKEY_EV_PALM_DETECTED
:
4085 case TP_HKEY_EV_PALM_UNDETECTED
:
4086 /* palm detected hovering the keyboard, forward to user-space
4087 * via netlink for consumption */
4091 pr_warn("unknown possible thermal alarm or keyboard event received\n");
4095 thermal_dump_all_sensors();
4100 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
4104 bool ignore_acpi_ev
;
4107 if (event
!= 0x80) {
4108 pr_err("unknown HKEY notification event %d\n", event
);
4109 /* forward it to userspace, maybe it knows how to handle it */
4110 acpi_bus_generate_netlink_event(
4111 ibm
->acpi
->device
->pnp
.device_class
,
4112 dev_name(&ibm
->acpi
->device
->dev
),
4118 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
4119 pr_err("failed to retrieve HKEY event\n");
4128 send_acpi_ev
= true;
4129 ignore_acpi_ev
= false;
4131 switch (hkey
>> 12) {
4133 /* 0x1000-0x1FFF: key presses */
4134 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
4138 /* 0x2000-0x2FFF: Wakeup reason */
4139 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
4143 /* 0x3000-0x3FFF: bay-related wakeups */
4145 case TP_HKEY_EV_BAYEJ_ACK
:
4146 hotkey_autosleep_ack
= 1;
4147 pr_info("bay ejected\n");
4148 hotkey_wakeup_hotunplug_complete_notify_change();
4151 case TP_HKEY_EV_OPTDRV_EJ
:
4152 /* FIXME: kick libata if SATA link offline */
4160 /* 0x4000-0x4FFF: dock-related events */
4161 known_ev
= hotkey_notify_dockevent(hkey
, &send_acpi_ev
,
4165 /* 0x5000-0x5FFF: human interface helpers */
4166 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
4170 /* 0x6000-0x6FFF: thermal alarms/notices and
4171 * keyboard events */
4172 known_ev
= hotkey_notify_6xxx(hkey
, &send_acpi_ev
,
4176 /* 0x7000-0x7FFF: misc */
4177 if (tp_features
.hotkey_wlsw
&&
4178 hkey
== TP_HKEY_EV_RFKILL_CHANGED
) {
4179 tpacpi_send_radiosw_update();
4184 /* fallthrough to default */
4189 pr_notice("unhandled HKEY event 0x%04x\n", hkey
);
4190 pr_notice("please report the conditions when this event happened to %s\n",
4194 /* netlink events */
4195 if (!ignore_acpi_ev
&& send_acpi_ev
) {
4196 acpi_bus_generate_netlink_event(
4197 ibm
->acpi
->device
->pnp
.device_class
,
4198 dev_name(&ibm
->acpi
->device
->dev
),
4204 static void hotkey_suspend(void)
4206 /* Do these on suspend, we get the events on early resume! */
4207 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
4208 hotkey_autosleep_ack
= 0;
4210 /* save previous mode of adaptive keyboard of X1 Carbon */
4211 if (tp_features
.has_adaptive_kbd
) {
4212 if (!acpi_evalf(hkey_handle
, &adaptive_keyboard_prev_mode
,
4214 pr_err("Cannot read adaptive keyboard mode.\n");
4219 static void hotkey_resume(void)
4221 tpacpi_disable_brightness_delay();
4223 if (hotkey_status_set(true) < 0 ||
4224 hotkey_mask_set(hotkey_acpi_mask
) < 0)
4225 pr_err("error while attempting to reset the event firmware interface\n");
4227 tpacpi_send_radiosw_update();
4228 hotkey_tablet_mode_notify_change();
4229 hotkey_wakeup_reason_notify_change();
4230 hotkey_wakeup_hotunplug_complete_notify_change();
4231 hotkey_poll_setup_safe(false);
4233 /* restore previous mode of adapive keyboard of X1 Carbon */
4234 if (tp_features
.has_adaptive_kbd
) {
4235 if (!acpi_evalf(hkey_handle
, NULL
, "STRW", "vd",
4236 adaptive_keyboard_prev_mode
)) {
4237 pr_err("Cannot set adaptive keyboard mode.\n");
4242 /* procfs -------------------------------------------------------------- */
4243 static int hotkey_read(struct seq_file
*m
)
4247 if (!tp_features
.hotkey
) {
4248 seq_printf(m
, "status:\t\tnot supported\n");
4252 if (mutex_lock_killable(&hotkey_mutex
))
4253 return -ERESTARTSYS
;
4254 res
= hotkey_status_get(&status
);
4256 res
= hotkey_mask_get();
4257 mutex_unlock(&hotkey_mutex
);
4261 seq_printf(m
, "status:\t\t%s\n", enabled(status
, 0));
4262 if (hotkey_all_mask
) {
4263 seq_printf(m
, "mask:\t\t0x%08x\n", hotkey_user_mask
);
4264 seq_printf(m
, "commands:\tenable, disable, reset, <mask>\n");
4266 seq_printf(m
, "mask:\t\tnot supported\n");
4267 seq_printf(m
, "commands:\tenable, disable, reset\n");
4273 static void hotkey_enabledisable_warn(bool enable
)
4275 tpacpi_log_usertask("procfs hotkey enable/disable");
4276 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
4277 pr_fmt("hotkey enable/disable functionality has been removed from the driver. Hotkeys are always enabled.\n")))
4278 pr_err("Please remove the hotkey=enable module parameter, it is deprecated. Hotkeys are always enabled.\n");
4281 static int hotkey_write(char *buf
)
4287 if (!tp_features
.hotkey
)
4290 if (mutex_lock_killable(&hotkey_mutex
))
4291 return -ERESTARTSYS
;
4293 mask
= hotkey_user_mask
;
4296 while ((cmd
= next_cmd(&buf
))) {
4297 if (strlencmp(cmd
, "enable") == 0) {
4298 hotkey_enabledisable_warn(1);
4299 } else if (strlencmp(cmd
, "disable") == 0) {
4300 hotkey_enabledisable_warn(0);
4302 } else if (strlencmp(cmd
, "reset") == 0) {
4303 mask
= (hotkey_all_mask
| hotkey_source_mask
)
4304 & ~hotkey_reserved_mask
;
4305 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
4307 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
4316 tpacpi_disclose_usertask("procfs hotkey",
4317 "set mask to 0x%08x\n", mask
);
4318 res
= hotkey_user_mask_set(mask
);
4322 mutex_unlock(&hotkey_mutex
);
4326 static const struct acpi_device_id ibm_htk_device_ids
[] = {
4327 {TPACPI_ACPI_IBM_HKEY_HID
, 0},
4328 {TPACPI_ACPI_LENOVO_HKEY_HID
, 0},
4329 {TPACPI_ACPI_LENOVO_HKEY_V2_HID
, 0},
4333 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
4334 .hid
= ibm_htk_device_ids
,
4335 .notify
= hotkey_notify
,
4336 .handle
= &hkey_handle
,
4337 .type
= ACPI_DEVICE_NOTIFY
,
4340 static struct ibm_struct hotkey_driver_data
= {
4342 .read
= hotkey_read
,
4343 .write
= hotkey_write
,
4344 .exit
= hotkey_exit
,
4345 .resume
= hotkey_resume
,
4346 .suspend
= hotkey_suspend
,
4347 .acpi
= &ibm_hotkey_acpidriver
,
4350 /*************************************************************************
4351 * Bluetooth subdriver
4355 /* ACPI GBDC/SBDC bits */
4356 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
4357 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
4358 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
4359 0 = disable, 1 = enable */
4363 /* ACPI \BLTH commands */
4364 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
4365 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
4366 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
4367 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
4368 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
4371 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4373 static int bluetooth_get_status(void)
4377 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4378 if (dbg_bluetoothemul
)
4379 return (tpacpi_bluetooth_emulstate
) ?
4380 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4383 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
4386 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
4387 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4390 static int bluetooth_set_status(enum tpacpi_rfkill_state state
)
4394 vdbg_printk(TPACPI_DBG_RFKILL
,
4395 "will attempt to %s bluetooth\n",
4396 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4398 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4399 if (dbg_bluetoothemul
) {
4400 tpacpi_bluetooth_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4405 if (state
== TPACPI_RFK_RADIO_ON
)
4406 status
= TP_ACPI_BLUETOOTH_RADIOSSW
4407 | TP_ACPI_BLUETOOTH_RESUMECTRL
;
4411 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
4417 /* sysfs bluetooth enable ---------------------------------------------- */
4418 static ssize_t
bluetooth_enable_show(struct device
*dev
,
4419 struct device_attribute
*attr
,
4422 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID
,
4426 static ssize_t
bluetooth_enable_store(struct device
*dev
,
4427 struct device_attribute
*attr
,
4428 const char *buf
, size_t count
)
4430 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID
,
4434 static DEVICE_ATTR_RW(bluetooth_enable
);
4436 /* --------------------------------------------------------------------- */
4438 static struct attribute
*bluetooth_attributes
[] = {
4439 &dev_attr_bluetooth_enable
.attr
,
4443 static const struct attribute_group bluetooth_attr_group
= {
4444 .attrs
= bluetooth_attributes
,
4447 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops
= {
4448 .get_status
= bluetooth_get_status
,
4449 .set_status
= bluetooth_set_status
,
4452 static void bluetooth_shutdown(void)
4454 /* Order firmware to save current state to NVRAM */
4455 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
4456 TP_ACPI_BLTH_SAVE_STATE
))
4457 pr_notice("failed to save bluetooth state to NVRAM\n");
4459 vdbg_printk(TPACPI_DBG_RFKILL
,
4460 "bluetooth state saved to NVRAM\n");
4463 static void bluetooth_exit(void)
4465 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4466 &bluetooth_attr_group
);
4468 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
4470 bluetooth_shutdown();
4473 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
4478 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4479 "initializing bluetooth subdriver\n");
4481 TPACPI_ACPIHANDLE_INIT(hkey
);
4483 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4484 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4485 tp_features
.bluetooth
= hkey_handle
&&
4486 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
4488 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4489 "bluetooth is %s, status 0x%02x\n",
4490 str_supported(tp_features
.bluetooth
),
4493 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4494 if (dbg_bluetoothemul
) {
4495 tp_features
.bluetooth
= 1;
4496 pr_info("bluetooth switch emulation enabled\n");
4499 if (tp_features
.bluetooth
&&
4500 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
4501 /* no bluetooth hardware present in system */
4502 tp_features
.bluetooth
= 0;
4503 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4504 "bluetooth hardware not installed\n");
4507 if (!tp_features
.bluetooth
)
4510 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
4511 &bluetooth_tprfk_ops
,
4512 RFKILL_TYPE_BLUETOOTH
,
4513 TPACPI_RFK_BLUETOOTH_SW_NAME
,
4518 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4519 &bluetooth_attr_group
);
4521 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
4528 /* procfs -------------------------------------------------------------- */
4529 static int bluetooth_read(struct seq_file
*m
)
4531 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID
, m
);
4534 static int bluetooth_write(char *buf
)
4536 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID
, buf
);
4539 static struct ibm_struct bluetooth_driver_data
= {
4540 .name
= "bluetooth",
4541 .read
= bluetooth_read
,
4542 .write
= bluetooth_write
,
4543 .exit
= bluetooth_exit
,
4544 .shutdown
= bluetooth_shutdown
,
4547 /*************************************************************************
4552 /* ACPI GWAN/SWAN bits */
4553 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
4554 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
4555 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
4556 0 = disable, 1 = enable */
4559 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4561 static int wan_get_status(void)
4565 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4567 return (tpacpi_wwan_emulstate
) ?
4568 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4571 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
4574 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
4575 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4578 static int wan_set_status(enum tpacpi_rfkill_state state
)
4582 vdbg_printk(TPACPI_DBG_RFKILL
,
4583 "will attempt to %s wwan\n",
4584 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4586 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4588 tpacpi_wwan_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4593 if (state
== TPACPI_RFK_RADIO_ON
)
4594 status
= TP_ACPI_WANCARD_RADIOSSW
4595 | TP_ACPI_WANCARD_RESUMECTRL
;
4599 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
4605 /* sysfs wan enable ---------------------------------------------------- */
4606 static ssize_t
wan_enable_show(struct device
*dev
,
4607 struct device_attribute
*attr
,
4610 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID
,
4614 static ssize_t
wan_enable_store(struct device
*dev
,
4615 struct device_attribute
*attr
,
4616 const char *buf
, size_t count
)
4618 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID
,
4622 static DEVICE_ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
4623 wan_enable_show
, wan_enable_store
);
4625 /* --------------------------------------------------------------------- */
4627 static struct attribute
*wan_attributes
[] = {
4628 &dev_attr_wwan_enable
.attr
,
4632 static const struct attribute_group wan_attr_group
= {
4633 .attrs
= wan_attributes
,
4636 static const struct tpacpi_rfk_ops wan_tprfk_ops
= {
4637 .get_status
= wan_get_status
,
4638 .set_status
= wan_set_status
,
4641 static void wan_shutdown(void)
4643 /* Order firmware to save current state to NVRAM */
4644 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
4645 TP_ACPI_WGSV_SAVE_STATE
))
4646 pr_notice("failed to save WWAN state to NVRAM\n");
4648 vdbg_printk(TPACPI_DBG_RFKILL
,
4649 "WWAN state saved to NVRAM\n");
4652 static void wan_exit(void)
4654 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4657 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4662 static int __init
wan_init(struct ibm_init_struct
*iibm
)
4667 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4668 "initializing wan subdriver\n");
4670 TPACPI_ACPIHANDLE_INIT(hkey
);
4672 tp_features
.wan
= hkey_handle
&&
4673 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
4675 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4676 "wan is %s, status 0x%02x\n",
4677 str_supported(tp_features
.wan
),
4680 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4682 tp_features
.wan
= 1;
4683 pr_info("wwan switch emulation enabled\n");
4686 if (tp_features
.wan
&&
4687 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
4688 /* no wan hardware present in system */
4689 tp_features
.wan
= 0;
4690 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4691 "wan hardware not installed\n");
4694 if (!tp_features
.wan
)
4697 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
4700 TPACPI_RFK_WWAN_SW_NAME
,
4705 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4709 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4716 /* procfs -------------------------------------------------------------- */
4717 static int wan_read(struct seq_file
*m
)
4719 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID
, m
);
4722 static int wan_write(char *buf
)
4724 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID
, buf
);
4727 static struct ibm_struct wan_driver_data
= {
4732 .shutdown
= wan_shutdown
,
4735 /*************************************************************************
4740 /* ACPI GUWB/SUWB bits */
4741 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
4742 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
4745 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4747 static int uwb_get_status(void)
4751 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4753 return (tpacpi_uwb_emulstate
) ?
4754 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4757 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
4760 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
4761 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4764 static int uwb_set_status(enum tpacpi_rfkill_state state
)
4768 vdbg_printk(TPACPI_DBG_RFKILL
,
4769 "will attempt to %s UWB\n",
4770 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4772 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4774 tpacpi_uwb_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4779 if (state
== TPACPI_RFK_RADIO_ON
)
4780 status
= TP_ACPI_UWB_RADIOSSW
;
4784 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
4790 /* --------------------------------------------------------------------- */
4792 static const struct tpacpi_rfk_ops uwb_tprfk_ops
= {
4793 .get_status
= uwb_get_status
,
4794 .set_status
= uwb_set_status
,
4797 static void uwb_exit(void)
4799 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID
);
4802 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
4807 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4808 "initializing uwb subdriver\n");
4810 TPACPI_ACPIHANDLE_INIT(hkey
);
4812 tp_features
.uwb
= hkey_handle
&&
4813 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
4815 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4816 "uwb is %s, status 0x%02x\n",
4817 str_supported(tp_features
.uwb
),
4820 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4822 tp_features
.uwb
= 1;
4823 pr_info("uwb switch emulation enabled\n");
4826 if (tp_features
.uwb
&&
4827 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
4828 /* no uwb hardware present in system */
4829 tp_features
.uwb
= 0;
4830 dbg_printk(TPACPI_DBG_INIT
,
4831 "uwb hardware not installed\n");
4834 if (!tp_features
.uwb
)
4837 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
4840 TPACPI_RFK_UWB_SW_NAME
,
4845 static struct ibm_struct uwb_driver_data
= {
4848 .flags
.experimental
= 1,
4851 /*************************************************************************
4855 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4857 enum video_access_mode
{
4858 TPACPI_VIDEO_NONE
= 0,
4859 TPACPI_VIDEO_570
, /* 570 */
4860 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
4861 TPACPI_VIDEO_NEW
, /* all others */
4864 enum { /* video status flags, based on VIDEO_570 */
4865 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
4866 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
4867 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
4870 enum { /* TPACPI_VIDEO_570 constants */
4871 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
4872 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
4873 * video_status_flags */
4874 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
4875 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
4878 static enum video_access_mode video_supported
;
4879 static int video_orig_autosw
;
4881 static int video_autosw_get(void);
4882 static int video_autosw_set(int enable
);
4884 TPACPI_HANDLE(vid
, root
,
4885 "\\_SB.PCI.AGP.VGA", /* 570 */
4886 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4887 "\\_SB.PCI0.VID0", /* 770e */
4888 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4889 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4890 "\\_SB.PCI0.AGP.VID", /* all others */
4893 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4895 static int __init
video_init(struct ibm_init_struct
*iibm
)
4899 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
4901 TPACPI_ACPIHANDLE_INIT(vid
);
4902 if (tpacpi_is_ibm())
4903 TPACPI_ACPIHANDLE_INIT(vid2
);
4905 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
4906 /* G41, assume IVGA doesn't change */
4907 vid_handle
= vid2_handle
;
4910 /* video switching not supported on R30, R31 */
4911 video_supported
= TPACPI_VIDEO_NONE
;
4912 else if (tpacpi_is_ibm() &&
4913 acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
4915 video_supported
= TPACPI_VIDEO_570
;
4916 else if (tpacpi_is_ibm() &&
4917 acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
4918 /* 600e/x, 770e, 770x */
4919 video_supported
= TPACPI_VIDEO_770
;
4922 video_supported
= TPACPI_VIDEO_NEW
;
4924 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
4925 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
4928 return (video_supported
!= TPACPI_VIDEO_NONE
) ? 0 : 1;
4931 static void video_exit(void)
4933 dbg_printk(TPACPI_DBG_EXIT
,
4934 "restoring original video autoswitch mode\n");
4935 if (video_autosw_set(video_orig_autosw
))
4936 pr_err("error while trying to restore original video autoswitch mode\n");
4939 static int video_outputsw_get(void)
4944 switch (video_supported
) {
4945 case TPACPI_VIDEO_570
:
4946 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
4947 TP_ACPI_VIDEO_570_PHSCMD
))
4949 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
4951 case TPACPI_VIDEO_770
:
4952 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4955 status
|= TP_ACPI_VIDEO_S_LCD
;
4956 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4959 status
|= TP_ACPI_VIDEO_S_CRT
;
4961 case TPACPI_VIDEO_NEW
:
4962 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4963 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4966 status
|= TP_ACPI_VIDEO_S_CRT
;
4968 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4969 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4972 status
|= TP_ACPI_VIDEO_S_LCD
;
4973 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4976 status
|= TP_ACPI_VIDEO_S_DVI
;
4985 static int video_outputsw_set(int status
)
4990 switch (video_supported
) {
4991 case TPACPI_VIDEO_570
:
4992 res
= acpi_evalf(NULL
, NULL
,
4993 "\\_SB.PHS2", "vdd",
4994 TP_ACPI_VIDEO_570_PHS2CMD
,
4995 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4997 case TPACPI_VIDEO_770
:
4998 autosw
= video_autosw_get();
5002 res
= video_autosw_set(1);
5005 res
= acpi_evalf(vid_handle
, NULL
,
5006 "ASWT", "vdd", status
* 0x100, 0);
5007 if (!autosw
&& video_autosw_set(autosw
)) {
5008 pr_err("video auto-switch left enabled due to error\n");
5012 case TPACPI_VIDEO_NEW
:
5013 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
5014 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
5020 return (res
) ? 0 : -EIO
;
5023 static int video_autosw_get(void)
5027 switch (video_supported
) {
5028 case TPACPI_VIDEO_570
:
5029 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
5032 case TPACPI_VIDEO_770
:
5033 case TPACPI_VIDEO_NEW
:
5034 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
5044 static int video_autosw_set(int enable
)
5046 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
) ? 1 : 0))
5051 static int video_outputsw_cycle(void)
5053 int autosw
= video_autosw_get();
5059 switch (video_supported
) {
5060 case TPACPI_VIDEO_570
:
5061 res
= video_autosw_set(1);
5064 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
5066 case TPACPI_VIDEO_770
:
5067 case TPACPI_VIDEO_NEW
:
5068 res
= video_autosw_set(1);
5071 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
5076 if (!autosw
&& video_autosw_set(autosw
)) {
5077 pr_err("video auto-switch left enabled due to error\n");
5081 return (res
) ? 0 : -EIO
;
5084 static int video_expand_toggle(void)
5086 switch (video_supported
) {
5087 case TPACPI_VIDEO_570
:
5088 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v") ?
5090 case TPACPI_VIDEO_770
:
5091 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v") ?
5093 case TPACPI_VIDEO_NEW
:
5094 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v") ?
5102 static int video_read(struct seq_file
*m
)
5106 if (video_supported
== TPACPI_VIDEO_NONE
) {
5107 seq_printf(m
, "status:\t\tnot supported\n");
5111 /* Even reads can crash X.org, so... */
5112 if (!capable(CAP_SYS_ADMIN
))
5115 status
= video_outputsw_get();
5119 autosw
= video_autosw_get();
5123 seq_printf(m
, "status:\t\tsupported\n");
5124 seq_printf(m
, "lcd:\t\t%s\n", enabled(status
, 0));
5125 seq_printf(m
, "crt:\t\t%s\n", enabled(status
, 1));
5126 if (video_supported
== TPACPI_VIDEO_NEW
)
5127 seq_printf(m
, "dvi:\t\t%s\n", enabled(status
, 3));
5128 seq_printf(m
, "auto:\t\t%s\n", enabled(autosw
, 0));
5129 seq_printf(m
, "commands:\tlcd_enable, lcd_disable\n");
5130 seq_printf(m
, "commands:\tcrt_enable, crt_disable\n");
5131 if (video_supported
== TPACPI_VIDEO_NEW
)
5132 seq_printf(m
, "commands:\tdvi_enable, dvi_disable\n");
5133 seq_printf(m
, "commands:\tauto_enable, auto_disable\n");
5134 seq_printf(m
, "commands:\tvideo_switch, expand_toggle\n");
5139 static int video_write(char *buf
)
5142 int enable
, disable
, status
;
5145 if (video_supported
== TPACPI_VIDEO_NONE
)
5148 /* Even reads can crash X.org, let alone writes... */
5149 if (!capable(CAP_SYS_ADMIN
))
5155 while ((cmd
= next_cmd(&buf
))) {
5156 if (strlencmp(cmd
, "lcd_enable") == 0) {
5157 enable
|= TP_ACPI_VIDEO_S_LCD
;
5158 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
5159 disable
|= TP_ACPI_VIDEO_S_LCD
;
5160 } else if (strlencmp(cmd
, "crt_enable") == 0) {
5161 enable
|= TP_ACPI_VIDEO_S_CRT
;
5162 } else if (strlencmp(cmd
, "crt_disable") == 0) {
5163 disable
|= TP_ACPI_VIDEO_S_CRT
;
5164 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
5165 strlencmp(cmd
, "dvi_enable") == 0) {
5166 enable
|= TP_ACPI_VIDEO_S_DVI
;
5167 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
5168 strlencmp(cmd
, "dvi_disable") == 0) {
5169 disable
|= TP_ACPI_VIDEO_S_DVI
;
5170 } else if (strlencmp(cmd
, "auto_enable") == 0) {
5171 res
= video_autosw_set(1);
5174 } else if (strlencmp(cmd
, "auto_disable") == 0) {
5175 res
= video_autosw_set(0);
5178 } else if (strlencmp(cmd
, "video_switch") == 0) {
5179 res
= video_outputsw_cycle();
5182 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
5183 res
= video_expand_toggle();
5190 if (enable
|| disable
) {
5191 status
= video_outputsw_get();
5194 res
= video_outputsw_set((status
& ~disable
) | enable
);
5202 static struct ibm_struct video_driver_data
= {
5205 .write
= video_write
,
5209 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5211 /*************************************************************************
5212 * Keyboard backlight subdriver
5215 static enum led_brightness kbdlight_brightness
;
5216 static DEFINE_MUTEX(kbdlight_mutex
);
5218 static int kbdlight_set_level(int level
)
5225 mutex_lock(&kbdlight_mutex
);
5227 if (!acpi_evalf(hkey_handle
, NULL
, "MLCS", "dd", level
))
5230 kbdlight_brightness
= level
;
5232 mutex_unlock(&kbdlight_mutex
);
5237 static int kbdlight_get_level(void)
5244 if (!acpi_evalf(hkey_handle
, &status
, "MLCG", "dd", 0))
5250 return status
& 0x3;
5253 static bool kbdlight_is_supported(void)
5260 if (!acpi_has_method(hkey_handle
, "MLCG")) {
5261 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG is unavailable\n");
5265 if (!acpi_evalf(hkey_handle
, &status
, "MLCG", "qdd", 0)) {
5266 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG failed\n");
5271 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG err: %d\n", status
);
5275 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight MLCG returned 0x%x\n", status
);
5277 * Guessed test for keyboard backlight:
5279 * Machines with backlight keyboard return:
5280 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5281 * b110100010010000000XX - ThinkPad x230
5282 * b010100000010000000XX - ThinkPad x240
5283 * b010100000010000000XX - ThinkPad W541
5284 * (XX is current backlight level)
5286 * Machines without backlight keyboard return:
5287 * b10100001000000000000 - ThinkPad x230
5288 * b10110001000000000000 - ThinkPad E430
5289 * b00000000000000000000 - ThinkPad E450
5291 * Candidate BITs for detection test (XOR):
5292 * b01000000001000000000
5295 return status
& BIT(9);
5298 static int kbdlight_sysfs_set(struct led_classdev
*led_cdev
,
5299 enum led_brightness brightness
)
5301 return kbdlight_set_level(brightness
);
5304 static enum led_brightness
kbdlight_sysfs_get(struct led_classdev
*led_cdev
)
5308 level
= kbdlight_get_level();
5315 static struct tpacpi_led_classdev tpacpi_led_kbdlight
= {
5317 .name
= "tpacpi::kbd_backlight",
5318 .max_brightness
= 2,
5319 .flags
= LED_BRIGHT_HW_CHANGED
,
5320 .brightness_set_blocking
= &kbdlight_sysfs_set
,
5321 .brightness_get
= &kbdlight_sysfs_get
,
5325 static int __init
kbdlight_init(struct ibm_init_struct
*iibm
)
5329 vdbg_printk(TPACPI_DBG_INIT
, "initializing kbdlight subdriver\n");
5331 TPACPI_ACPIHANDLE_INIT(hkey
);
5333 if (!kbdlight_is_supported()) {
5334 tp_features
.kbdlight
= 0;
5335 vdbg_printk(TPACPI_DBG_INIT
, "kbdlight is unsupported\n");
5339 kbdlight_brightness
= kbdlight_sysfs_get(NULL
);
5340 tp_features
.kbdlight
= 1;
5342 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5343 &tpacpi_led_kbdlight
.led_classdev
);
5345 tp_features
.kbdlight
= 0;
5349 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
|
5350 TP_ACPI_HKEY_KBD_LIGHT_MASK
);
5354 static void kbdlight_exit(void)
5356 if (tp_features
.kbdlight
)
5357 led_classdev_unregister(&tpacpi_led_kbdlight
.led_classdev
);
5360 static int kbdlight_set_level_and_update(int level
)
5363 struct led_classdev
*led_cdev
;
5365 ret
= kbdlight_set_level(level
);
5366 led_cdev
= &tpacpi_led_kbdlight
.led_classdev
;
5368 if (ret
== 0 && !(led_cdev
->flags
& LED_SUSPENDED
))
5369 led_cdev
->brightness
= level
;
5374 static int kbdlight_read(struct seq_file
*m
)
5378 if (!tp_features
.kbdlight
) {
5379 seq_printf(m
, "status:\t\tnot supported\n");
5381 level
= kbdlight_get_level();
5383 seq_printf(m
, "status:\t\terror %d\n", level
);
5385 seq_printf(m
, "status:\t\t%d\n", level
);
5386 seq_printf(m
, "commands:\t0, 1, 2\n");
5392 static int kbdlight_write(char *buf
)
5397 if (!tp_features
.kbdlight
)
5400 while ((cmd
= next_cmd(&buf
))) {
5401 if (strlencmp(cmd
, "0") == 0)
5403 else if (strlencmp(cmd
, "1") == 0)
5405 else if (strlencmp(cmd
, "2") == 0)
5414 return kbdlight_set_level_and_update(level
);
5417 static void kbdlight_suspend(void)
5419 struct led_classdev
*led_cdev
;
5421 if (!tp_features
.kbdlight
)
5424 led_cdev
= &tpacpi_led_kbdlight
.led_classdev
;
5425 led_update_brightness(led_cdev
);
5426 led_classdev_suspend(led_cdev
);
5429 static void kbdlight_resume(void)
5431 if (!tp_features
.kbdlight
)
5434 led_classdev_resume(&tpacpi_led_kbdlight
.led_classdev
);
5437 static struct ibm_struct kbdlight_driver_data
= {
5439 .read
= kbdlight_read
,
5440 .write
= kbdlight_write
,
5441 .suspend
= kbdlight_suspend
,
5442 .resume
= kbdlight_resume
,
5443 .exit
= kbdlight_exit
,
5446 /*************************************************************************
5447 * Light (thinklight) subdriver
5450 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5451 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
5453 static int light_get_status(void)
5457 if (tp_features
.light_status
) {
5458 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
5466 static int light_set_status(int status
)
5470 if (tp_features
.light
) {
5472 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
5474 TP_CMOS_THINKLIGHT_ON
:
5475 TP_CMOS_THINKLIGHT_OFF
);
5477 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
5480 return (rc
) ? 0 : -EIO
;
5486 static int light_sysfs_set(struct led_classdev
*led_cdev
,
5487 enum led_brightness brightness
)
5489 return light_set_status((brightness
!= LED_OFF
) ?
5490 TPACPI_LED_ON
: TPACPI_LED_OFF
);
5493 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
5495 return (light_get_status() == 1) ? LED_FULL
: LED_OFF
;
5498 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
5500 .name
= "tpacpi::thinklight",
5501 .brightness_set_blocking
= &light_sysfs_set
,
5502 .brightness_get
= &light_sysfs_get
,
5506 static int __init
light_init(struct ibm_init_struct
*iibm
)
5510 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
5512 if (tpacpi_is_ibm()) {
5513 TPACPI_ACPIHANDLE_INIT(ledb
);
5514 TPACPI_ACPIHANDLE_INIT(lght
);
5516 TPACPI_ACPIHANDLE_INIT(cmos
);
5518 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5519 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
5521 if (tp_features
.light
)
5522 /* light status not supported on
5523 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5524 tp_features
.light_status
=
5525 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
5527 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
5528 str_supported(tp_features
.light
),
5529 str_supported(tp_features
.light_status
));
5531 if (!tp_features
.light
)
5534 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5535 &tpacpi_led_thinklight
.led_classdev
);
5538 tp_features
.light
= 0;
5539 tp_features
.light_status
= 0;
5547 static void light_exit(void)
5549 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
5552 static int light_read(struct seq_file
*m
)
5556 if (!tp_features
.light
) {
5557 seq_printf(m
, "status:\t\tnot supported\n");
5558 } else if (!tp_features
.light_status
) {
5559 seq_printf(m
, "status:\t\tunknown\n");
5560 seq_printf(m
, "commands:\ton, off\n");
5562 status
= light_get_status();
5565 seq_printf(m
, "status:\t\t%s\n", onoff(status
, 0));
5566 seq_printf(m
, "commands:\ton, off\n");
5572 static int light_write(char *buf
)
5577 if (!tp_features
.light
)
5580 while ((cmd
= next_cmd(&buf
))) {
5581 if (strlencmp(cmd
, "on") == 0) {
5583 } else if (strlencmp(cmd
, "off") == 0) {
5589 return light_set_status(newstatus
);
5592 static struct ibm_struct light_driver_data
= {
5595 .write
= light_write
,
5599 /*************************************************************************
5603 /* sysfs cmos_command -------------------------------------------------- */
5604 static ssize_t
cmos_command_store(struct device
*dev
,
5605 struct device_attribute
*attr
,
5606 const char *buf
, size_t count
)
5608 unsigned long cmos_cmd
;
5611 if (parse_strtoul(buf
, 21, &cmos_cmd
))
5614 res
= issue_thinkpad_cmos_command(cmos_cmd
);
5615 return (res
) ? res
: count
;
5618 static DEVICE_ATTR_WO(cmos_command
);
5620 /* --------------------------------------------------------------------- */
5622 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
5626 vdbg_printk(TPACPI_DBG_INIT
,
5627 "initializing cmos commands subdriver\n");
5629 TPACPI_ACPIHANDLE_INIT(cmos
);
5631 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
5632 str_supported(cmos_handle
!= NULL
));
5634 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
5638 return (cmos_handle
) ? 0 : 1;
5641 static void cmos_exit(void)
5643 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
5646 static int cmos_read(struct seq_file
*m
)
5648 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5649 R30, R31, T20-22, X20-21 */
5651 seq_printf(m
, "status:\t\tnot supported\n");
5653 seq_printf(m
, "status:\t\tsupported\n");
5654 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-21)\n");
5660 static int cmos_write(char *buf
)
5665 while ((cmd
= next_cmd(&buf
))) {
5666 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
5667 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
5672 res
= issue_thinkpad_cmos_command(cmos_cmd
);
5680 static struct ibm_struct cmos_driver_data
= {
5683 .write
= cmos_write
,
5687 /*************************************************************************
5691 enum led_access_mode
{
5692 TPACPI_LED_NONE
= 0,
5693 TPACPI_LED_570
, /* 570 */
5694 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5695 TPACPI_LED_NEW
, /* all others */
5698 enum { /* For TPACPI_LED_OLD */
5699 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
5700 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
5701 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
5704 static enum led_access_mode led_supported
;
5706 static acpi_handle led_handle
;
5708 #define TPACPI_LED_NUMLEDS 16
5709 static struct tpacpi_led_classdev
*tpacpi_leds
;
5710 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
5711 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
5712 /* there's a limit of 19 chars + NULL before 2.6.26 */
5714 "tpacpi:orange:batt",
5715 "tpacpi:green:batt",
5716 "tpacpi::dock_active",
5717 "tpacpi::bay_active",
5718 "tpacpi::dock_batt",
5719 "tpacpi::unknown_led",
5721 "tpacpi::dock_status1",
5722 "tpacpi::dock_status2",
5723 "tpacpi::unknown_led2",
5724 "tpacpi::unknown_led3",
5725 "tpacpi::thinkvantage",
5727 #define TPACPI_SAFE_LEDS 0x1081U
5729 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
5731 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5734 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
5738 static int led_get_status(const unsigned int led
)
5741 enum led_status_t led_s
;
5743 switch (led_supported
) {
5744 case TPACPI_LED_570
:
5745 if (!acpi_evalf(ec_handle
,
5746 &status
, "GLED", "dd", 1 << led
))
5748 led_s
= (status
== 0) ?
5753 tpacpi_led_state_cache
[led
] = led_s
;
5762 static int led_set_status(const unsigned int led
,
5763 const enum led_status_t ledstatus
)
5765 /* off, on, blink. Index is led_status_t */
5766 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
5767 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
5771 switch (led_supported
) {
5772 case TPACPI_LED_570
:
5774 if (unlikely(led
> 7))
5776 if (unlikely(tpacpi_is_led_restricted(led
)))
5778 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5779 (1 << led
), led_sled_arg1
[ledstatus
]))
5782 case TPACPI_LED_OLD
:
5783 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5784 if (unlikely(led
> 7))
5786 if (unlikely(tpacpi_is_led_restricted(led
)))
5788 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
5790 rc
= ec_write(TPACPI_LED_EC_HLBL
,
5791 (ledstatus
== TPACPI_LED_BLINK
) << led
);
5793 rc
= ec_write(TPACPI_LED_EC_HLCL
,
5794 (ledstatus
!= TPACPI_LED_OFF
) << led
);
5796 case TPACPI_LED_NEW
:
5798 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
5800 if (unlikely(tpacpi_is_led_restricted(led
)))
5802 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5803 led
, led_led_arg1
[ledstatus
]))
5811 tpacpi_led_state_cache
[led
] = ledstatus
;
5816 static int led_sysfs_set(struct led_classdev
*led_cdev
,
5817 enum led_brightness brightness
)
5819 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5820 struct tpacpi_led_classdev
, led_classdev
);
5821 enum led_status_t new_state
;
5823 if (brightness
== LED_OFF
)
5824 new_state
= TPACPI_LED_OFF
;
5825 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
5826 new_state
= TPACPI_LED_ON
;
5828 new_state
= TPACPI_LED_BLINK
;
5830 return led_set_status(data
->led
, new_state
);
5833 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
5834 unsigned long *delay_on
, unsigned long *delay_off
)
5836 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5837 struct tpacpi_led_classdev
, led_classdev
);
5839 /* Can we choose the flash rate? */
5840 if (*delay_on
== 0 && *delay_off
== 0) {
5841 /* yes. set them to the hardware blink rate (1 Hz) */
5842 *delay_on
= 500; /* ms */
5843 *delay_off
= 500; /* ms */
5844 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
5847 return led_set_status(data
->led
, TPACPI_LED_BLINK
);
5850 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
5854 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5855 struct tpacpi_led_classdev
, led_classdev
);
5857 rc
= led_get_status(data
->led
);
5859 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
5860 rc
= LED_OFF
; /* no error handling in led class :( */
5867 static void led_exit(void)
5871 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5872 if (tpacpi_leds
[i
].led_classdev
.name
)
5873 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
5879 static int __init
tpacpi_init_led(unsigned int led
)
5883 tpacpi_leds
[led
].led
= led
;
5885 /* LEDs with no name don't get registered */
5886 if (!tpacpi_led_names
[led
])
5889 tpacpi_leds
[led
].led_classdev
.brightness_set_blocking
= &led_sysfs_set
;
5890 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
5891 if (led_supported
== TPACPI_LED_570
)
5892 tpacpi_leds
[led
].led_classdev
.brightness_get
=
5895 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
5897 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5898 &tpacpi_leds
[led
].led_classdev
);
5900 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
5905 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
5906 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5907 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5908 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5910 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5911 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5912 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5913 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5914 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5915 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5916 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5917 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5919 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5920 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5921 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5922 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5923 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5925 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5926 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5927 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5928 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5930 /* (1) - may have excess leds enabled on MSB */
5932 /* Defaults (order matters, keep last, don't reorder!) */
5934 .vendor
= PCI_VENDOR_ID_LENOVO
,
5935 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5938 { /* IBM ThinkPads with no EC version string */
5939 .vendor
= PCI_VENDOR_ID_IBM
,
5940 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
5943 { /* IBM ThinkPads with EC version string */
5944 .vendor
= PCI_VENDOR_ID_IBM
,
5945 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5950 #undef TPACPI_LEDQ_IBM
5951 #undef TPACPI_LEDQ_LNV
5953 static enum led_access_mode __init
led_init_detect_mode(void)
5957 if (tpacpi_is_ibm()) {
5959 status
= acpi_get_handle(ec_handle
, "SLED", &led_handle
);
5960 if (ACPI_SUCCESS(status
))
5961 return TPACPI_LED_570
;
5963 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5964 status
= acpi_get_handle(ec_handle
, "SYSL", &led_handle
);
5965 if (ACPI_SUCCESS(status
))
5966 return TPACPI_LED_OLD
;
5970 status
= acpi_get_handle(ec_handle
, "LED", &led_handle
);
5971 if (ACPI_SUCCESS(status
))
5972 return TPACPI_LED_NEW
;
5974 /* R30, R31, and unknown firmwares */
5976 return TPACPI_LED_NONE
;
5979 static int __init
led_init(struct ibm_init_struct
*iibm
)
5983 unsigned long useful_leds
;
5985 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
5987 led_supported
= led_init_detect_mode();
5989 if (led_supported
!= TPACPI_LED_NONE
) {
5990 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
5991 ARRAY_SIZE(led_useful_qtable
));
5995 led_supported
= TPACPI_LED_NONE
;
5999 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
6000 str_supported(led_supported
), led_supported
);
6002 if (led_supported
== TPACPI_LED_NONE
)
6005 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
6008 pr_err("Out of memory for LED data\n");
6012 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
6013 tpacpi_leds
[i
].led
= -1;
6015 if (!tpacpi_is_led_restricted(i
) &&
6016 test_bit(i
, &useful_leds
)) {
6017 rc
= tpacpi_init_led(i
);
6025 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
6026 pr_notice("warning: userspace override of important firmware LEDs is enabled\n");
6031 #define str_led_status(s) \
6032 ((s) == TPACPI_LED_OFF ? "off" : \
6033 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
6035 static int led_read(struct seq_file
*m
)
6037 if (!led_supported
) {
6038 seq_printf(m
, "status:\t\tnot supported\n");
6041 seq_printf(m
, "status:\t\tsupported\n");
6043 if (led_supported
== TPACPI_LED_570
) {
6046 for (i
= 0; i
< 8; i
++) {
6047 status
= led_get_status(i
);
6050 seq_printf(m
, "%d:\t\t%s\n",
6051 i
, str_led_status(status
));
6055 seq_printf(m
, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n");
6060 static int led_write(char *buf
)
6064 enum led_status_t s
;
6069 while ((cmd
= next_cmd(&buf
))) {
6070 if (sscanf(cmd
, "%d", &led
) != 1)
6073 if (led
< 0 || led
> (TPACPI_LED_NUMLEDS
- 1) ||
6074 tpacpi_leds
[led
].led
< 0)
6077 if (strstr(cmd
, "off")) {
6079 } else if (strstr(cmd
, "on")) {
6081 } else if (strstr(cmd
, "blink")) {
6082 s
= TPACPI_LED_BLINK
;
6087 rc
= led_set_status(led
, s
);
6095 static struct ibm_struct led_driver_data
= {
6102 /*************************************************************************
6106 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
6108 #define TPACPI_BEEP_Q1 0x0001
6110 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
6111 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
6112 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
6115 static int __init
beep_init(struct ibm_init_struct
*iibm
)
6117 unsigned long quirks
;
6119 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
6121 TPACPI_ACPIHANDLE_INIT(beep
);
6123 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
6124 str_supported(beep_handle
!= NULL
));
6126 quirks
= tpacpi_check_quirks(beep_quirk_table
,
6127 ARRAY_SIZE(beep_quirk_table
));
6129 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
6131 return (beep_handle
) ? 0 : 1;
6134 static int beep_read(struct seq_file
*m
)
6137 seq_printf(m
, "status:\t\tnot supported\n");
6139 seq_printf(m
, "status:\t\tsupported\n");
6140 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-17)\n");
6146 static int beep_write(char *buf
)
6154 while ((cmd
= next_cmd(&buf
))) {
6155 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
6156 beep_cmd
>= 0 && beep_cmd
<= 17) {
6160 if (tp_features
.beep_needs_two_args
) {
6161 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
6165 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
6174 static struct ibm_struct beep_driver_data
= {
6177 .write
= beep_write
,
6180 /*************************************************************************
6184 enum thermal_access_mode
{
6185 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
6186 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
6187 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
6188 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
6189 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
6192 enum { /* TPACPI_THERMAL_TPEC_* */
6193 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
6194 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
6195 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
6197 TPACPI_THERMAL_SENSOR_NA
= -128000, /* Sensor not available */
6201 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6202 struct ibm_thermal_sensors_struct
{
6203 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
6206 static enum thermal_access_mode thermal_read_mode
;
6208 /* idx is zero-based */
6209 static int thermal_get_sensor(int idx
, s32
*value
)
6215 t
= TP_EC_THERMAL_TMP0
;
6217 switch (thermal_read_mode
) {
6218 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6219 case TPACPI_THERMAL_TPEC_16
:
6220 if (idx
>= 8 && idx
<= 15) {
6221 t
= TP_EC_THERMAL_TMP8
;
6226 case TPACPI_THERMAL_TPEC_8
:
6228 if (!acpi_ec_read(t
+ idx
, &tmp
))
6230 *value
= tmp
* 1000;
6235 case TPACPI_THERMAL_ACPI_UPDT
:
6237 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
6238 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
6240 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
6242 *value
= (t
- 2732) * 100;
6247 case TPACPI_THERMAL_ACPI_TMP07
:
6249 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
6250 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
6252 if (t
> 127 || t
< -127)
6253 t
= TP_EC_THERMAL_TMP_NA
;
6259 case TPACPI_THERMAL_NONE
:
6267 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
6278 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
6281 for (i
= 0 ; i
< n
; i
++) {
6282 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
6290 static void thermal_dump_all_sensors(void)
6293 struct ibm_thermal_sensors_struct t
;
6295 n
= thermal_get_sensors(&t
);
6299 pr_notice("temperatures (Celsius):");
6301 for (i
= 0; i
< n
; i
++) {
6302 if (t
.temp
[i
] != TPACPI_THERMAL_SENSOR_NA
)
6303 pr_cont(" %d", (int)(t
.temp
[i
] / 1000));
6311 /* sysfs temp##_input -------------------------------------------------- */
6313 static ssize_t
thermal_temp_input_show(struct device
*dev
,
6314 struct device_attribute
*attr
,
6317 struct sensor_device_attribute
*sensor_attr
=
6318 to_sensor_dev_attr(attr
);
6319 int idx
= sensor_attr
->index
;
6323 res
= thermal_get_sensor(idx
, &value
);
6326 if (value
== TPACPI_THERMAL_SENSOR_NA
)
6329 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
6332 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6333 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6334 thermal_temp_input_show, NULL, _idxB)
6336 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
6337 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6338 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6339 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6340 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6341 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6342 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6343 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6344 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6345 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6346 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6347 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6348 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6349 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6350 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6351 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6352 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6355 #define THERMAL_ATTRS(X) \
6356 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6358 static struct attribute
*thermal_temp_input_attr
[] = {
6378 static const struct attribute_group thermal_temp_input16_group
= {
6379 .attrs
= thermal_temp_input_attr
6382 static const struct attribute_group thermal_temp_input8_group
= {
6383 .attrs
= &thermal_temp_input_attr
[8]
6386 #undef THERMAL_SENSOR_ATTR_TEMP
6387 #undef THERMAL_ATTRS
6389 /* --------------------------------------------------------------------- */
6391 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
6398 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
6400 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
6402 if (thinkpad_id
.ec_model
) {
6404 * Direct EC access mode: sensors at registers
6405 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6406 * non-implemented, thermal sensors return 0x80 when
6411 for (i
= 0; i
< 8; i
++) {
6412 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
6418 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
6426 /* This is sheer paranoia, but we handle it anyway */
6428 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n");
6429 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
6431 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n");
6432 thermal_read_mode
= TPACPI_THERMAL_NONE
;
6437 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
6439 } else if (acpi_tmp7
) {
6440 if (tpacpi_is_ibm() &&
6441 acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
6442 /* 600e/x, 770e, 770x */
6443 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
6445 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6446 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
6449 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6450 thermal_read_mode
= TPACPI_THERMAL_NONE
;
6453 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
6454 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
6457 switch (thermal_read_mode
) {
6458 case TPACPI_THERMAL_TPEC_16
:
6459 res
= sysfs_create_group(&tpacpi_hwmon
->kobj
,
6460 &thermal_temp_input16_group
);
6464 case TPACPI_THERMAL_TPEC_8
:
6465 case TPACPI_THERMAL_ACPI_TMP07
:
6466 case TPACPI_THERMAL_ACPI_UPDT
:
6467 res
= sysfs_create_group(&tpacpi_hwmon
->kobj
,
6468 &thermal_temp_input8_group
);
6472 case TPACPI_THERMAL_NONE
:
6480 static void thermal_exit(void)
6482 switch (thermal_read_mode
) {
6483 case TPACPI_THERMAL_TPEC_16
:
6484 sysfs_remove_group(&tpacpi_hwmon
->kobj
,
6485 &thermal_temp_input16_group
);
6487 case TPACPI_THERMAL_TPEC_8
:
6488 case TPACPI_THERMAL_ACPI_TMP07
:
6489 case TPACPI_THERMAL_ACPI_UPDT
:
6490 sysfs_remove_group(&tpacpi_hwmon
->kobj
,
6491 &thermal_temp_input8_group
);
6493 case TPACPI_THERMAL_NONE
:
6499 static int thermal_read(struct seq_file
*m
)
6502 struct ibm_thermal_sensors_struct t
;
6504 n
= thermal_get_sensors(&t
);
6505 if (unlikely(n
< 0))
6508 seq_printf(m
, "temperatures:\t");
6511 for (i
= 0; i
< (n
- 1); i
++)
6512 seq_printf(m
, "%d ", t
.temp
[i
] / 1000);
6513 seq_printf(m
, "%d\n", t
.temp
[i
] / 1000);
6515 seq_printf(m
, "not supported\n");
6520 static struct ibm_struct thermal_driver_data
= {
6522 .read
= thermal_read
,
6523 .exit
= thermal_exit
,
6526 /*************************************************************************
6527 * Backlight/brightness subdriver
6530 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6533 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6534 * CMOS NVRAM byte 0x5E, bits 0-3.
6536 * EC HBRV (0x31) has the following layout
6537 * Bit 7: unknown function
6538 * Bit 6: unknown function
6539 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6540 * Bit 4: must be set to zero to avoid problems
6541 * Bit 3-0: backlight brightness level
6543 * brightness_get_raw returns status data in the HBRV layout
6545 * WARNING: The X61 has been verified to use HBRV for something else, so
6546 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6547 * testing on the very early *60 Lenovo models...
6551 TP_EC_BACKLIGHT
= 0x31,
6553 /* TP_EC_BACKLIGHT bitmasks */
6554 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
6555 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
6556 TP_EC_BACKLIGHT_MAPSW
= 0x20,
6559 enum tpacpi_brightness_access_mode
{
6560 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
6561 TPACPI_BRGHT_MODE_EC
, /* EC control */
6562 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
6563 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
6564 TPACPI_BRGHT_MODE_MAX
6567 static struct backlight_device
*ibm_backlight_device
;
6569 static enum tpacpi_brightness_access_mode brightness_mode
=
6570 TPACPI_BRGHT_MODE_MAX
;
6572 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
6574 static struct mutex brightness_mutex
;
6576 /* NVRAM brightness access,
6577 * call with brightness_mutex held! */
6578 static unsigned int tpacpi_brightness_nvram_get(void)
6582 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
6583 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
6584 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
6585 lnvram
&= bright_maxlvl
;
6590 static void tpacpi_brightness_checkpoint_nvram(void)
6595 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
6598 vdbg_printk(TPACPI_DBG_BRGHT
,
6599 "trying to checkpoint backlight level to NVRAM...\n");
6601 if (mutex_lock_killable(&brightness_mutex
) < 0)
6604 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6606 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
6607 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
6609 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
6610 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
6611 /* NVRAM needs update */
6612 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
6613 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
6615 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
6616 dbg_printk(TPACPI_DBG_BRGHT
,
6617 "updated NVRAM backlight level to %u (0x%02x)\n",
6618 (unsigned int) lec
, (unsigned int) b_nvram
);
6620 vdbg_printk(TPACPI_DBG_BRGHT
,
6621 "NVRAM backlight level already is %u (0x%02x)\n",
6622 (unsigned int) lec
, (unsigned int) b_nvram
);
6625 mutex_unlock(&brightness_mutex
);
6629 /* call with brightness_mutex held! */
6630 static int tpacpi_brightness_get_raw(int *status
)
6634 switch (brightness_mode
) {
6635 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6636 *status
= tpacpi_brightness_nvram_get();
6638 case TPACPI_BRGHT_MODE_EC
:
6639 case TPACPI_BRGHT_MODE_ECNVRAM
:
6640 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6649 /* call with brightness_mutex held! */
6650 /* do NOT call with illegal backlight level value */
6651 static int tpacpi_brightness_set_ec(unsigned int value
)
6655 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6658 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
6659 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
6660 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
6666 /* call with brightness_mutex held! */
6667 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
6670 unsigned int current_value
, i
;
6672 current_value
= tpacpi_brightness_nvram_get();
6674 if (value
== current_value
)
6677 cmos_cmd
= (value
> current_value
) ?
6678 TP_CMOS_BRIGHTNESS_UP
:
6679 TP_CMOS_BRIGHTNESS_DOWN
;
6680 inc
= (value
> current_value
) ? 1 : -1;
6682 for (i
= current_value
; i
!= value
; i
+= inc
)
6683 if (issue_thinkpad_cmos_command(cmos_cmd
))
6689 /* May return EINTR which can always be mapped to ERESTARTSYS */
6690 static int brightness_set(unsigned int value
)
6694 if (value
> bright_maxlvl
)
6697 vdbg_printk(TPACPI_DBG_BRGHT
,
6698 "set backlight level to %d\n", value
);
6700 res
= mutex_lock_killable(&brightness_mutex
);
6704 switch (brightness_mode
) {
6705 case TPACPI_BRGHT_MODE_EC
:
6706 case TPACPI_BRGHT_MODE_ECNVRAM
:
6707 res
= tpacpi_brightness_set_ec(value
);
6709 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6710 res
= tpacpi_brightness_set_ucmsstep(value
);
6716 mutex_unlock(&brightness_mutex
);
6720 /* sysfs backlight class ----------------------------------------------- */
6722 static int brightness_update_status(struct backlight_device
*bd
)
6724 unsigned int level
=
6725 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
6726 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
6727 bd
->props
.brightness
: 0;
6729 dbg_printk(TPACPI_DBG_BRGHT
,
6730 "backlight: attempt to set level to %d\n",
6733 /* it is the backlight class's job (caller) to handle
6734 * EINTR and other errors properly */
6735 return brightness_set(level
);
6738 static int brightness_get(struct backlight_device
*bd
)
6742 res
= mutex_lock_killable(&brightness_mutex
);
6746 res
= tpacpi_brightness_get_raw(&status
);
6748 mutex_unlock(&brightness_mutex
);
6753 return status
& TP_EC_BACKLIGHT_LVLMSK
;
6756 static void tpacpi_brightness_notify_change(void)
6758 backlight_force_update(ibm_backlight_device
,
6759 BACKLIGHT_UPDATE_HOTKEY
);
6762 static const struct backlight_ops ibm_backlight_data
= {
6763 .get_brightness
= brightness_get
,
6764 .update_status
= brightness_update_status
,
6767 /* --------------------------------------------------------------------- */
6770 * Call _BCL method of video device. On some ThinkPads this will
6771 * switch the firmware to the ACPI brightness control mode.
6774 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
6776 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
6777 union acpi_object
*obj
;
6778 struct acpi_device
*device
, *child
;
6781 if (acpi_bus_get_device(handle
, &device
))
6785 list_for_each_entry(child
, &device
->children
, node
) {
6786 acpi_status status
= acpi_evaluate_object(child
->handle
, "_BCL",
6788 if (ACPI_FAILURE(status
))
6791 obj
= (union acpi_object
*)buffer
.pointer
;
6792 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
6793 pr_err("Unknown _BCL data, please report this to %s\n",
6797 rc
= obj
->package
.count
;
6802 kfree(buffer
.pointer
);
6808 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6810 static unsigned int __init
tpacpi_check_std_acpi_brightness_support(void)
6812 acpi_handle video_device
;
6815 tpacpi_acpi_handle_locate("video", NULL
, &video_device
);
6817 bcl_levels
= tpacpi_query_bcl_levels(video_device
);
6819 tp_features
.bright_acpimode
= (bcl_levels
> 0);
6821 return (bcl_levels
> 2) ? (bcl_levels
- 2) : 0;
6825 * These are only useful for models that have only one possibility
6826 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6829 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6830 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6831 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6833 static const struct tpacpi_quirk brightness_quirk_table
[] __initconst
= {
6834 /* Models with ATI GPUs known to require ECNVRAM mode */
6835 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC
), /* T43/p ATI */
6837 /* Models with ATI GPUs that can use ECNVRAM */
6838 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC
), /* R50,51 T40-42 */
6839 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6840 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC
), /* R52 */
6841 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6843 /* Models with Intel Extreme Graphics 2 */
6844 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC
), /* X40 */
6845 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6846 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6848 /* Models with Intel GMA900 */
6849 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC
), /* T43, R52 */
6850 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC
), /* X41 */
6851 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC
), /* X41 Tablet */
6855 * Returns < 0 for error, otherwise sets tp_features.bright_*
6856 * and bright_maxlvl.
6858 static void __init
tpacpi_detect_brightness_capabilities(void)
6862 vdbg_printk(TPACPI_DBG_INIT
,
6863 "detecting firmware brightness interface capabilities\n");
6865 /* we could run a quirks check here (same table used by
6866 * brightness_init) if needed */
6869 * We always attempt to detect acpi support, so as to switch
6870 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6871 * going to publish a backlight interface
6873 b
= tpacpi_check_std_acpi_brightness_support();
6883 tp_features
.bright_unkfw
= 1;
6884 bright_maxlvl
= b
- 1;
6886 pr_debug("detected %u brightness levels\n", bright_maxlvl
+ 1);
6889 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
6891 struct backlight_properties props
;
6893 unsigned long quirks
;
6895 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
6897 mutex_init(&brightness_mutex
);
6899 quirks
= tpacpi_check_quirks(brightness_quirk_table
,
6900 ARRAY_SIZE(brightness_quirk_table
));
6902 /* tpacpi_detect_brightness_capabilities() must have run already */
6904 /* if it is unknown, we don't handle it: it wouldn't be safe */
6905 if (tp_features
.bright_unkfw
)
6908 if (!brightness_enable
) {
6909 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6910 "brightness support disabled by module parameter\n");
6914 if (acpi_video_get_backlight_type() != acpi_backlight_vendor
) {
6915 if (brightness_enable
> 1) {
6916 pr_info("Standard ACPI backlight interface available, not loading native one\n");
6918 } else if (brightness_enable
== 1) {
6919 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n");
6922 } else if (tp_features
.bright_acpimode
&& brightness_enable
> 1) {
6923 pr_notice("Standard ACPI backlight interface not available, thinkpad_acpi native brightness control enabled\n");
6927 * Check for module parameter bogosity, note that we
6928 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6929 * able to detect "unspecified"
6931 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
6934 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6935 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
6936 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
6937 if (quirks
& TPACPI_BRGHT_Q_EC
)
6938 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
6940 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
6942 dbg_printk(TPACPI_DBG_BRGHT
,
6943 "driver auto-selected brightness_mode=%d\n",
6948 if (!tpacpi_is_ibm() &&
6949 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
6950 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
6953 if (tpacpi_brightness_get_raw(&b
) < 0)
6956 memset(&props
, 0, sizeof(struct backlight_properties
));
6957 props
.type
= BACKLIGHT_PLATFORM
;
6958 props
.max_brightness
= bright_maxlvl
;
6959 props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
6960 ibm_backlight_device
= backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME
,
6962 &ibm_backlight_data
,
6964 if (IS_ERR(ibm_backlight_device
)) {
6965 int rc
= PTR_ERR(ibm_backlight_device
);
6966 ibm_backlight_device
= NULL
;
6967 pr_err("Could not register backlight device\n");
6970 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6971 "brightness is supported\n");
6973 if (quirks
& TPACPI_BRGHT_Q_ASK
) {
6974 pr_notice("brightness: will use unverified default: brightness_mode=%d\n",
6976 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n",
6980 /* Added by mistake in early 2007. Probably useless, but it could
6981 * be working around some unknown firmware problem where the value
6982 * read at startup doesn't match the real hardware state... so leave
6983 * it in place just in case */
6984 backlight_update_status(ibm_backlight_device
);
6986 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6987 "brightness: registering brightness hotkeys as change notification\n");
6988 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6989 | TP_ACPI_HKEY_BRGHTUP_MASK
6990 | TP_ACPI_HKEY_BRGHTDWN_MASK
);
6994 static void brightness_suspend(void)
6996 tpacpi_brightness_checkpoint_nvram();
6999 static void brightness_shutdown(void)
7001 tpacpi_brightness_checkpoint_nvram();
7004 static void brightness_exit(void)
7006 if (ibm_backlight_device
) {
7007 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
7008 "calling backlight_device_unregister()\n");
7009 backlight_device_unregister(ibm_backlight_device
);
7012 tpacpi_brightness_checkpoint_nvram();
7015 static int brightness_read(struct seq_file
*m
)
7019 level
= brightness_get(NULL
);
7021 seq_printf(m
, "level:\t\tunreadable\n");
7023 seq_printf(m
, "level:\t\t%d\n", level
);
7024 seq_printf(m
, "commands:\tup, down\n");
7025 seq_printf(m
, "commands:\tlevel <level> (<level> is 0-%d)\n",
7032 static int brightness_write(char *buf
)
7038 level
= brightness_get(NULL
);
7042 while ((cmd
= next_cmd(&buf
))) {
7043 if (strlencmp(cmd
, "up") == 0) {
7044 if (level
< bright_maxlvl
)
7046 } else if (strlencmp(cmd
, "down") == 0) {
7049 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
7050 level
>= 0 && level
<= bright_maxlvl
) {
7056 tpacpi_disclose_usertask("procfs brightness",
7057 "set level to %d\n", level
);
7060 * Now we know what the final level should be, so we try to set it.
7061 * Doing it this way makes the syscall restartable in case of EINTR
7063 rc
= brightness_set(level
);
7064 if (!rc
&& ibm_backlight_device
)
7065 backlight_force_update(ibm_backlight_device
,
7066 BACKLIGHT_UPDATE_SYSFS
);
7067 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
7070 static struct ibm_struct brightness_driver_data
= {
7071 .name
= "brightness",
7072 .read
= brightness_read
,
7073 .write
= brightness_write
,
7074 .exit
= brightness_exit
,
7075 .suspend
= brightness_suspend
,
7076 .shutdown
= brightness_shutdown
,
7079 /*************************************************************************
7084 * IBM ThinkPads have a simple volume controller with MUTE gating.
7085 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
7087 * Since the *61 series (and probably also the later *60 series), Lenovo
7088 * ThinkPads only implement the MUTE gate.
7091 * Bit 6: MUTE (1 mutes sound)
7093 * Other bits should be zero as far as we know.
7095 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
7096 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
7097 * such as bit 7 which is used to detect repeated presses of MUTE,
7098 * and we leave them unchanged.
7100 * On newer Lenovo ThinkPads, the EC can automatically change the volume
7101 * in response to user input. Unfortunately, this rarely works well.
7102 * The laptop changes the state of its internal MUTE gate and, on some
7103 * models, sends KEY_MUTE, causing any user code that responds to the
7104 * mute button to get confused. The hardware MUTE gate is also
7105 * unnecessary, since user code can handle the mute button without
7106 * kernel or EC help.
7108 * To avoid confusing userspace, we simply disable all EC-based mute
7109 * and volume controls when possible.
7112 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
7114 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
7115 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
7116 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7118 #if SNDRV_CARDS <= 32
7119 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7121 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7123 static int alsa_index
= DEFAULT_ALSA_IDX
; /* last three slots */
7124 static char *alsa_id
= "ThinkPadEC";
7125 static bool alsa_enable
= SNDRV_DEFAULT_ENABLE1
;
7127 struct tpacpi_alsa_data
{
7128 struct snd_card
*card
;
7129 struct snd_ctl_elem_id
*ctl_mute_id
;
7130 struct snd_ctl_elem_id
*ctl_vol_id
;
7133 static struct snd_card
*alsa_card
;
7138 /* TP_EC_AUDIO bits */
7139 TP_EC_AUDIO_MUTESW
= 6,
7141 /* TP_EC_AUDIO bitmasks */
7142 TP_EC_AUDIO_LVL_MSK
= 0x0F,
7143 TP_EC_AUDIO_MUTESW_MSK
= (1 << TP_EC_AUDIO_MUTESW
),
7145 /* Maximum volume */
7146 TP_EC_VOLUME_MAX
= 14,
7149 enum tpacpi_volume_access_mode
{
7150 TPACPI_VOL_MODE_AUTO
= 0, /* Not implemented yet */
7151 TPACPI_VOL_MODE_EC
, /* Pure EC control */
7152 TPACPI_VOL_MODE_UCMS_STEP
, /* UCMS step-based control: N/A */
7153 TPACPI_VOL_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
7157 enum tpacpi_volume_capabilities
{
7158 TPACPI_VOL_CAP_AUTO
= 0, /* Use white/blacklist */
7159 TPACPI_VOL_CAP_VOLMUTE
, /* Output vol and mute */
7160 TPACPI_VOL_CAP_MUTEONLY
, /* Output mute only */
7164 enum tpacpi_mute_btn_mode
{
7165 TP_EC_MUTE_BTN_LATCH
= 0, /* Mute mutes; up/down unmutes */
7166 /* We don't know what mode 1 is. */
7167 TP_EC_MUTE_BTN_NONE
= 2, /* Mute and up/down are just keys */
7168 TP_EC_MUTE_BTN_TOGGLE
= 3, /* Mute toggles; up/down unmutes */
7171 static enum tpacpi_volume_access_mode volume_mode
=
7172 TPACPI_VOL_MODE_MAX
;
7174 static enum tpacpi_volume_capabilities volume_capabilities
;
7175 static bool volume_control_allowed
;
7176 static bool software_mute_requested
= true;
7177 static bool software_mute_active
;
7178 static int software_mute_orig_mode
;
7181 * Used to syncronize writers to TP_EC_AUDIO and
7182 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7184 static struct mutex volume_mutex
;
7186 static void tpacpi_volume_checkpoint_nvram(void)
7192 if (volume_mode
!= TPACPI_VOL_MODE_ECNVRAM
)
7194 if (!volume_control_allowed
)
7196 if (software_mute_active
)
7199 vdbg_printk(TPACPI_DBG_MIXER
,
7200 "trying to checkpoint mixer state to NVRAM...\n");
7202 if (tp_features
.mixer_no_level_control
)
7203 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
;
7205 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
| TP_EC_AUDIO_LVL_MSK
;
7207 if (mutex_lock_killable(&volume_mutex
) < 0)
7210 if (unlikely(!acpi_ec_read(TP_EC_AUDIO
, &lec
)))
7213 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
7215 if (lec
!= (b_nvram
& ec_mask
)) {
7216 /* NVRAM needs update */
7217 b_nvram
&= ~ec_mask
;
7219 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_MIXER
);
7220 dbg_printk(TPACPI_DBG_MIXER
,
7221 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7222 (unsigned int) lec
, (unsigned int) b_nvram
);
7224 vdbg_printk(TPACPI_DBG_MIXER
,
7225 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7226 (unsigned int) lec
, (unsigned int) b_nvram
);
7230 mutex_unlock(&volume_mutex
);
7233 static int volume_get_status_ec(u8
*status
)
7237 if (!acpi_ec_read(TP_EC_AUDIO
, &s
))
7242 dbg_printk(TPACPI_DBG_MIXER
, "status 0x%02x\n", s
);
7247 static int volume_get_status(u8
*status
)
7249 return volume_get_status_ec(status
);
7252 static int volume_set_status_ec(const u8 status
)
7254 if (!acpi_ec_write(TP_EC_AUDIO
, status
))
7257 dbg_printk(TPACPI_DBG_MIXER
, "set EC mixer to 0x%02x\n", status
);
7260 * On X200s, and possibly on others, it can take a while for
7261 * reads to become correct.
7268 static int volume_set_status(const u8 status
)
7270 return volume_set_status_ec(status
);
7273 /* returns < 0 on error, 0 on no change, 1 on change */
7274 static int __volume_set_mute_ec(const bool mute
)
7279 if (mutex_lock_killable(&volume_mutex
) < 0)
7282 rc
= volume_get_status_ec(&s
);
7286 n
= (mute
) ? s
| TP_EC_AUDIO_MUTESW_MSK
:
7287 s
& ~TP_EC_AUDIO_MUTESW_MSK
;
7290 rc
= volume_set_status_ec(n
);
7296 mutex_unlock(&volume_mutex
);
7300 static int volume_alsa_set_mute(const bool mute
)
7302 dbg_printk(TPACPI_DBG_MIXER
, "ALSA: trying to %smute\n",
7303 (mute
) ? "" : "un");
7304 return __volume_set_mute_ec(mute
);
7307 static int volume_set_mute(const bool mute
)
7311 dbg_printk(TPACPI_DBG_MIXER
, "trying to %smute\n",
7312 (mute
) ? "" : "un");
7314 rc
= __volume_set_mute_ec(mute
);
7315 return (rc
< 0) ? rc
: 0;
7318 /* returns < 0 on error, 0 on no change, 1 on change */
7319 static int __volume_set_volume_ec(const u8 vol
)
7324 if (vol
> TP_EC_VOLUME_MAX
)
7327 if (mutex_lock_killable(&volume_mutex
) < 0)
7330 rc
= volume_get_status_ec(&s
);
7334 n
= (s
& ~TP_EC_AUDIO_LVL_MSK
) | vol
;
7337 rc
= volume_set_status_ec(n
);
7343 mutex_unlock(&volume_mutex
);
7347 static int volume_set_software_mute(bool startup
)
7351 if (!tpacpi_is_lenovo())
7355 if (!acpi_evalf(ec_handle
, &software_mute_orig_mode
,
7359 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7360 "Initial HAUM setting was %d\n",
7361 software_mute_orig_mode
);
7364 if (!acpi_evalf(ec_handle
, &result
, "SAUM", "qdd",
7365 (int)TP_EC_MUTE_BTN_NONE
))
7368 if (result
!= TP_EC_MUTE_BTN_NONE
)
7369 pr_warn("Unexpected SAUM result %d\n",
7373 * In software mute mode, the standard codec controls take
7374 * precendence, so we unmute the ThinkPad HW switch at
7375 * startup. Just on case there are SAUM-capable ThinkPads
7376 * with level controls, set max HW volume as well.
7378 if (tp_features
.mixer_no_level_control
)
7379 result
= volume_set_mute(false);
7381 result
= volume_set_status(TP_EC_VOLUME_MAX
);
7384 pr_warn("Failed to unmute the HW mute switch\n");
7389 static void volume_exit_software_mute(void)
7393 if (!acpi_evalf(ec_handle
, &r
, "SAUM", "qdd", software_mute_orig_mode
)
7394 || r
!= software_mute_orig_mode
)
7395 pr_warn("Failed to restore mute mode\n");
7398 static int volume_alsa_set_volume(const u8 vol
)
7400 dbg_printk(TPACPI_DBG_MIXER
,
7401 "ALSA: trying to set volume level to %hu\n", vol
);
7402 return __volume_set_volume_ec(vol
);
7405 static void volume_alsa_notify_change(void)
7407 struct tpacpi_alsa_data
*d
;
7409 if (alsa_card
&& alsa_card
->private_data
) {
7410 d
= alsa_card
->private_data
;
7412 snd_ctl_notify(alsa_card
,
7413 SNDRV_CTL_EVENT_MASK_VALUE
,
7416 snd_ctl_notify(alsa_card
,
7417 SNDRV_CTL_EVENT_MASK_VALUE
,
7422 static int volume_alsa_vol_info(struct snd_kcontrol
*kcontrol
,
7423 struct snd_ctl_elem_info
*uinfo
)
7425 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
7427 uinfo
->value
.integer
.min
= 0;
7428 uinfo
->value
.integer
.max
= TP_EC_VOLUME_MAX
;
7432 static int volume_alsa_vol_get(struct snd_kcontrol
*kcontrol
,
7433 struct snd_ctl_elem_value
*ucontrol
)
7438 rc
= volume_get_status(&s
);
7442 ucontrol
->value
.integer
.value
[0] = s
& TP_EC_AUDIO_LVL_MSK
;
7446 static int volume_alsa_vol_put(struct snd_kcontrol
*kcontrol
,
7447 struct snd_ctl_elem_value
*ucontrol
)
7449 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7450 ucontrol
->value
.integer
.value
[0]);
7451 return volume_alsa_set_volume(ucontrol
->value
.integer
.value
[0]);
7454 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7456 static int volume_alsa_mute_get(struct snd_kcontrol
*kcontrol
,
7457 struct snd_ctl_elem_value
*ucontrol
)
7462 rc
= volume_get_status(&s
);
7466 ucontrol
->value
.integer
.value
[0] =
7467 (s
& TP_EC_AUDIO_MUTESW_MSK
) ? 0 : 1;
7471 static int volume_alsa_mute_put(struct snd_kcontrol
*kcontrol
,
7472 struct snd_ctl_elem_value
*ucontrol
)
7474 tpacpi_disclose_usertask("ALSA", "%smute\n",
7475 ucontrol
->value
.integer
.value
[0] ?
7477 return volume_alsa_set_mute(!ucontrol
->value
.integer
.value
[0]);
7480 static struct snd_kcontrol_new volume_alsa_control_vol __initdata
= {
7481 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
7482 .name
= "Console Playback Volume",
7484 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
7485 .info
= volume_alsa_vol_info
,
7486 .get
= volume_alsa_vol_get
,
7489 static struct snd_kcontrol_new volume_alsa_control_mute __initdata
= {
7490 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
7491 .name
= "Console Playback Switch",
7493 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
7494 .info
= volume_alsa_mute_info
,
7495 .get
= volume_alsa_mute_get
,
7498 static void volume_suspend(void)
7500 tpacpi_volume_checkpoint_nvram();
7503 static void volume_resume(void)
7505 if (software_mute_active
) {
7506 if (volume_set_software_mute(false) < 0)
7507 pr_warn("Failed to restore software mute\n");
7509 volume_alsa_notify_change();
7513 static void volume_shutdown(void)
7515 tpacpi_volume_checkpoint_nvram();
7518 static void volume_exit(void)
7521 snd_card_free(alsa_card
);
7525 tpacpi_volume_checkpoint_nvram();
7527 if (software_mute_active
)
7528 volume_exit_software_mute();
7531 static int __init
volume_create_alsa_mixer(void)
7533 struct snd_card
*card
;
7534 struct tpacpi_alsa_data
*data
;
7535 struct snd_kcontrol
*ctl_vol
;
7536 struct snd_kcontrol
*ctl_mute
;
7539 rc
= snd_card_new(&tpacpi_pdev
->dev
,
7540 alsa_index
, alsa_id
, THIS_MODULE
,
7541 sizeof(struct tpacpi_alsa_data
), &card
);
7542 if (rc
< 0 || !card
) {
7543 pr_err("Failed to create ALSA card structures: %d\n", rc
);
7547 BUG_ON(!card
->private_data
);
7548 data
= card
->private_data
;
7551 strlcpy(card
->driver
, TPACPI_ALSA_DRVNAME
,
7552 sizeof(card
->driver
));
7553 strlcpy(card
->shortname
, TPACPI_ALSA_SHRTNAME
,
7554 sizeof(card
->shortname
));
7555 snprintf(card
->mixername
, sizeof(card
->mixername
), "ThinkPad EC %s",
7556 (thinkpad_id
.ec_version_str
) ?
7557 thinkpad_id
.ec_version_str
: "(unknown)");
7558 snprintf(card
->longname
, sizeof(card
->longname
),
7559 "%s at EC reg 0x%02x, fw %s", card
->shortname
, TP_EC_AUDIO
,
7560 (thinkpad_id
.ec_version_str
) ?
7561 thinkpad_id
.ec_version_str
: "unknown");
7563 if (volume_control_allowed
) {
7564 volume_alsa_control_vol
.put
= volume_alsa_vol_put
;
7565 volume_alsa_control_vol
.access
=
7566 SNDRV_CTL_ELEM_ACCESS_READWRITE
;
7568 volume_alsa_control_mute
.put
= volume_alsa_mute_put
;
7569 volume_alsa_control_mute
.access
=
7570 SNDRV_CTL_ELEM_ACCESS_READWRITE
;
7573 if (!tp_features
.mixer_no_level_control
) {
7574 ctl_vol
= snd_ctl_new1(&volume_alsa_control_vol
, NULL
);
7575 rc
= snd_ctl_add(card
, ctl_vol
);
7577 pr_err("Failed to create ALSA volume control: %d\n",
7581 data
->ctl_vol_id
= &ctl_vol
->id
;
7584 ctl_mute
= snd_ctl_new1(&volume_alsa_control_mute
, NULL
);
7585 rc
= snd_ctl_add(card
, ctl_mute
);
7587 pr_err("Failed to create ALSA mute control: %d\n", rc
);
7590 data
->ctl_mute_id
= &ctl_mute
->id
;
7592 rc
= snd_card_register(card
);
7594 pr_err("Failed to register ALSA card: %d\n", rc
);
7602 snd_card_free(card
);
7606 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7607 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7609 static const struct tpacpi_quirk volume_quirk_table
[] __initconst
= {
7610 /* Whitelist volume level on all IBM by default */
7611 { .vendor
= PCI_VENDOR_ID_IBM
,
7612 .bios
= TPACPI_MATCH_ANY
,
7613 .ec
= TPACPI_MATCH_ANY
,
7614 .quirks
= TPACPI_VOL_Q_LEVEL
},
7616 /* Lenovo models with volume control (needs confirmation) */
7617 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL
), /* R60/i */
7618 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL
), /* R60e/i */
7619 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL
), /* T60/p */
7620 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL
), /* X60/s */
7621 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL
), /* X60t */
7622 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL
), /* Z60 */
7623 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL
), /* Z61 */
7625 /* Whitelist mute-only on all Lenovo by default */
7626 { .vendor
= PCI_VENDOR_ID_LENOVO
,
7627 .bios
= TPACPI_MATCH_ANY
,
7628 .ec
= TPACPI_MATCH_ANY
,
7629 .quirks
= TPACPI_VOL_Q_MUTEONLY
}
7632 static int __init
volume_init(struct ibm_init_struct
*iibm
)
7634 unsigned long quirks
;
7637 vdbg_printk(TPACPI_DBG_INIT
, "initializing volume subdriver\n");
7639 mutex_init(&volume_mutex
);
7642 * Check for module parameter bogosity, note that we
7643 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7644 * able to detect "unspecified"
7646 if (volume_mode
> TPACPI_VOL_MODE_MAX
)
7649 if (volume_mode
== TPACPI_VOL_MODE_UCMS_STEP
) {
7650 pr_err("UCMS step volume mode not implemented, please contact %s\n",
7655 if (volume_capabilities
>= TPACPI_VOL_CAP_MAX
)
7659 * The ALSA mixer is our primary interface.
7660 * When disabled, don't install the subdriver at all
7663 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7664 "ALSA mixer disabled by parameter, not loading volume subdriver...\n");
7668 quirks
= tpacpi_check_quirks(volume_quirk_table
,
7669 ARRAY_SIZE(volume_quirk_table
));
7671 switch (volume_capabilities
) {
7672 case TPACPI_VOL_CAP_AUTO
:
7673 if (quirks
& TPACPI_VOL_Q_MUTEONLY
)
7674 tp_features
.mixer_no_level_control
= 1;
7675 else if (quirks
& TPACPI_VOL_Q_LEVEL
)
7676 tp_features
.mixer_no_level_control
= 0;
7678 return 1; /* no mixer */
7680 case TPACPI_VOL_CAP_VOLMUTE
:
7681 tp_features
.mixer_no_level_control
= 0;
7683 case TPACPI_VOL_CAP_MUTEONLY
:
7684 tp_features
.mixer_no_level_control
= 1;
7690 if (volume_capabilities
!= TPACPI_VOL_CAP_AUTO
)
7691 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7692 "using user-supplied volume_capabilities=%d\n",
7693 volume_capabilities
);
7695 if (volume_mode
== TPACPI_VOL_MODE_AUTO
||
7696 volume_mode
== TPACPI_VOL_MODE_MAX
) {
7697 volume_mode
= TPACPI_VOL_MODE_ECNVRAM
;
7699 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7700 "driver auto-selected volume_mode=%d\n",
7703 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7704 "using user-supplied volume_mode=%d\n",
7708 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7709 "mute is supported, volume control is %s\n",
7710 str_supported(!tp_features
.mixer_no_level_control
));
7712 if (software_mute_requested
&& volume_set_software_mute(true) == 0) {
7713 software_mute_active
= true;
7715 rc
= volume_create_alsa_mixer();
7717 pr_err("Could not create the ALSA mixer interface\n");
7721 pr_info("Console audio control enabled, mode: %s\n",
7722 (volume_control_allowed
) ?
7723 "override (read/write)" :
7724 "monitor (read only)");
7727 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
7728 "registering volume hotkeys as change notification\n");
7729 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7730 | TP_ACPI_HKEY_VOLUP_MASK
7731 | TP_ACPI_HKEY_VOLDWN_MASK
7732 | TP_ACPI_HKEY_MUTE_MASK
);
7737 static int volume_read(struct seq_file
*m
)
7741 if (volume_get_status(&status
) < 0) {
7742 seq_printf(m
, "level:\t\tunreadable\n");
7744 if (tp_features
.mixer_no_level_control
)
7745 seq_printf(m
, "level:\t\tunsupported\n");
7747 seq_printf(m
, "level:\t\t%d\n",
7748 status
& TP_EC_AUDIO_LVL_MSK
);
7750 seq_printf(m
, "mute:\t\t%s\n",
7751 onoff(status
, TP_EC_AUDIO_MUTESW
));
7753 if (volume_control_allowed
) {
7754 seq_printf(m
, "commands:\tunmute, mute\n");
7755 if (!tp_features
.mixer_no_level_control
) {
7756 seq_printf(m
, "commands:\tup, down\n");
7757 seq_printf(m
, "commands:\tlevel <level> (<level> is 0-%d)\n",
7766 static int volume_write(char *buf
)
7769 u8 new_level
, new_mute
;
7775 * We do allow volume control at driver startup, so that the
7776 * user can set initial state through the volume=... parameter hack.
7778 if (!volume_control_allowed
&& tpacpi_lifecycle
!= TPACPI_LIFE_INIT
) {
7779 if (unlikely(!tp_warned
.volume_ctrl_forbidden
)) {
7780 tp_warned
.volume_ctrl_forbidden
= 1;
7781 pr_notice("Console audio control in monitor mode, changes are not allowed\n");
7782 pr_notice("Use the volume_control=1 module parameter to enable volume control\n");
7787 rc
= volume_get_status(&s
);
7791 new_level
= s
& TP_EC_AUDIO_LVL_MSK
;
7792 new_mute
= s
& TP_EC_AUDIO_MUTESW_MSK
;
7794 while ((cmd
= next_cmd(&buf
))) {
7795 if (!tp_features
.mixer_no_level_control
) {
7796 if (strlencmp(cmd
, "up") == 0) {
7799 else if (new_level
< TP_EC_VOLUME_MAX
)
7802 } else if (strlencmp(cmd
, "down") == 0) {
7805 else if (new_level
> 0)
7808 } else if (sscanf(cmd
, "level %u", &l
) == 1 &&
7809 l
>= 0 && l
<= TP_EC_VOLUME_MAX
) {
7814 if (strlencmp(cmd
, "mute") == 0)
7815 new_mute
= TP_EC_AUDIO_MUTESW_MSK
;
7816 else if (strlencmp(cmd
, "unmute") == 0)
7822 if (tp_features
.mixer_no_level_control
) {
7823 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7824 new_mute
? "" : "un");
7825 rc
= volume_set_mute(!!new_mute
);
7827 tpacpi_disclose_usertask("procfs volume",
7828 "%smute and set level to %d\n",
7829 new_mute
? "" : "un", new_level
);
7830 rc
= volume_set_status(new_mute
| new_level
);
7832 volume_alsa_notify_change();
7834 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
7837 static struct ibm_struct volume_driver_data
= {
7839 .read
= volume_read
,
7840 .write
= volume_write
,
7841 .exit
= volume_exit
,
7842 .suspend
= volume_suspend
,
7843 .resume
= volume_resume
,
7844 .shutdown
= volume_shutdown
,
7847 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7849 #define alsa_card NULL
7851 static inline void volume_alsa_notify_change(void)
7855 static int __init
volume_init(struct ibm_init_struct
*iibm
)
7857 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7862 static struct ibm_struct volume_driver_data
= {
7866 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7868 /*************************************************************************
7875 * TPACPI_FAN_RD_ACPI_GFAN:
7876 * ACPI GFAN method: returns fan level
7878 * see TPACPI_FAN_WR_ACPI_SFAN
7879 * EC 0x2f (HFSP) not available if GFAN exists
7881 * TPACPI_FAN_WR_ACPI_SFAN:
7882 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7884 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7887 * TPACPI_FAN_WR_TPEC:
7888 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7889 * Supported on almost all ThinkPads
7891 * Fan speed changes of any sort (including those caused by the
7892 * disengaged mode) are usually done slowly by the firmware as the
7893 * maximum amount of fan duty cycle change per second seems to be
7896 * Reading is not available if GFAN exists.
7897 * Writing is not available if SFAN exists.
7900 * 7 automatic mode engaged;
7901 * (default operation mode of the ThinkPad)
7902 * fan level is ignored in this mode.
7903 * 6 full speed mode (takes precedence over bit 7);
7904 * not available on all thinkpads. May disable
7905 * the tachometer while the fan controller ramps up
7906 * the speed (which can take up to a few *minutes*).
7907 * Speeds up fan to 100% duty-cycle, which is far above
7908 * the standard RPM levels. It is not impossible that
7909 * it could cause hardware damage.
7910 * 5-3 unused in some models. Extra bits for fan level
7911 * in others, but still useless as all values above
7912 * 7 map to the same speed as level 7 in these models.
7913 * 2-0 fan level (0..7 usually)
7915 * 0x07 = max (set when temperatures critical)
7916 * Some ThinkPads may have other levels, see
7917 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7919 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7920 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7921 * does so, its initial value is meaningless (0x07).
7923 * For firmware bugs, refer to:
7924 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7928 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7929 * Main fan tachometer reading (in RPM)
7931 * This register is present on all ThinkPads with a new-style EC, and
7932 * it is known not to be present on the A21m/e, and T22, as there is
7933 * something else in offset 0x84 according to the ACPI DSDT. Other
7934 * ThinkPads from this same time period (and earlier) probably lack the
7935 * tachometer as well.
7937 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7938 * was never fixed by IBM to report the EC firmware version string
7939 * probably support the tachometer (like the early X models), so
7940 * detecting it is quite hard. We need more data to know for sure.
7942 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7945 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7948 * For firmware bugs, refer to:
7949 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7953 * ThinkPad EC register 0x31 bit 0 (only on select models)
7955 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7956 * show the speed of the main fan. When bit 0 of EC register 0x31
7957 * is one, the tachometer registers show the speed of the auxiliary
7960 * Fan control seems to affect both fans, regardless of the state
7963 * So far, only the firmware for the X60/X61 non-tablet versions
7964 * seem to support this (firmware TP-7M).
7966 * TPACPI_FAN_WR_ACPI_FANS:
7967 * ThinkPad X31, X40, X41. Not available in the X60.
7969 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7970 * high speed. ACPI DSDT seems to map these three speeds to levels
7971 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7972 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7974 * The speeds are stored on handles
7975 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7977 * There are three default speed sets, accessible as handles:
7978 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7980 * ACPI DSDT switches which set is in use depending on various
7983 * TPACPI_FAN_WR_TPEC is also available and should be used to
7984 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7985 * but the ACPI tables just mention level 7.
7988 enum { /* Fan control constants */
7989 fan_status_offset
= 0x2f, /* EC register 0x2f */
7990 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7991 * 0x84 must be read before 0x85 */
7992 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
7993 bit 0 selects which fan is active */
7995 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
7996 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
7998 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
8001 enum fan_status_access_mode
{
8002 TPACPI_FAN_NONE
= 0, /* No fan status or control */
8003 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
8004 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
8007 enum fan_control_access_mode
{
8008 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
8009 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
8010 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
8011 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
8014 enum fan_control_commands
{
8015 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
8016 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
8017 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
8018 * and also watchdog cmd */
8021 static bool fan_control_allowed
;
8023 static enum fan_status_access_mode fan_status_access_mode
;
8024 static enum fan_control_access_mode fan_control_access_mode
;
8025 static enum fan_control_commands fan_control_commands
;
8027 static u8 fan_control_initial_status
;
8028 static u8 fan_control_desired_level
;
8029 static u8 fan_control_resume_level
;
8030 static int fan_watchdog_maxinterval
;
8032 static struct mutex fan_mutex
;
8034 static void fan_watchdog_fire(struct work_struct
*ignored
);
8035 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
8037 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
8038 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
8039 "\\FSPD", /* 600e/x, 770e, 770x */
8041 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
8042 "JFNS", /* 770x-JL */
8046 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
8047 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
8048 * be in auto mode (0x80).
8050 * This is corrected by any write to HFSP either by the driver, or
8053 * We assume 0x07 really means auto mode while this quirk is active,
8054 * as this is far more likely than the ThinkPad being in level 7,
8055 * which is only used by the firmware during thermal emergencies.
8057 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
8058 * TP-70 (T43, R52), which are known to be buggy.
8061 static void fan_quirk1_setup(void)
8063 if (fan_control_initial_status
== 0x07) {
8064 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n");
8065 tp_features
.fan_ctrl_status_undef
= 1;
8069 static void fan_quirk1_handle(u8
*fan_status
)
8071 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
8072 if (*fan_status
!= fan_control_initial_status
) {
8073 /* something changed the HFSP regisnter since
8074 * driver init time, so it is not undefined
8076 tp_features
.fan_ctrl_status_undef
= 0;
8078 /* Return most likely status. In fact, it
8079 * might be the only possible status */
8080 *fan_status
= TP_EC_FAN_AUTO
;
8085 /* Select main fan on X60/X61, NOOP on others */
8086 static bool fan_select_fan1(void)
8088 if (tp_features
.second_fan
) {
8091 if (ec_read(fan_select_offset
, &val
) < 0)
8094 if (ec_write(fan_select_offset
, val
) < 0)
8100 /* Select secondary fan on X60/X61 */
8101 static bool fan_select_fan2(void)
8105 if (!tp_features
.second_fan
)
8108 if (ec_read(fan_select_offset
, &val
) < 0)
8111 if (ec_write(fan_select_offset
, val
) < 0)
8118 * Call with fan_mutex held
8120 static void fan_update_desired_level(u8 status
)
8123 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
8125 fan_control_desired_level
= 7;
8127 fan_control_desired_level
= status
;
8131 static int fan_get_status(u8
*status
)
8136 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8138 switch (fan_status_access_mode
) {
8139 case TPACPI_FAN_RD_ACPI_GFAN
: {
8140 /* 570, 600e/x, 770e, 770x */
8143 if (unlikely(!acpi_evalf(gfan_handle
, &res
, NULL
, "d")))
8147 *status
= res
& 0x07;
8151 case TPACPI_FAN_RD_TPEC
:
8152 /* all except 570, 600e/x, 770e, 770x */
8153 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
8156 if (likely(status
)) {
8158 fan_quirk1_handle(status
);
8170 static int fan_get_status_safe(u8
*status
)
8175 if (mutex_lock_killable(&fan_mutex
))
8176 return -ERESTARTSYS
;
8177 rc
= fan_get_status(&s
);
8179 fan_update_desired_level(s
);
8180 mutex_unlock(&fan_mutex
);
8190 static int fan_get_speed(unsigned int *speed
)
8194 switch (fan_status_access_mode
) {
8195 case TPACPI_FAN_RD_TPEC
:
8196 /* all except 570, 600e/x, 770e, 770x */
8197 if (unlikely(!fan_select_fan1()))
8199 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
8200 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
8204 *speed
= (hi
<< 8) | lo
;
8215 static int fan2_get_speed(unsigned int *speed
)
8220 switch (fan_status_access_mode
) {
8221 case TPACPI_FAN_RD_TPEC
:
8222 /* all except 570, 600e/x, 770e, 770x */
8223 if (unlikely(!fan_select_fan2()))
8225 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
8226 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
8227 fan_select_fan1(); /* play it safe */
8232 *speed
= (hi
<< 8) | lo
;
8243 static int fan_set_level(int level
)
8245 if (!fan_control_allowed
)
8248 switch (fan_control_access_mode
) {
8249 case TPACPI_FAN_WR_ACPI_SFAN
:
8250 if (level
>= 0 && level
<= 7) {
8251 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
8257 case TPACPI_FAN_WR_ACPI_FANS
:
8258 case TPACPI_FAN_WR_TPEC
:
8259 if (!(level
& TP_EC_FAN_AUTO
) &&
8260 !(level
& TP_EC_FAN_FULLSPEED
) &&
8261 ((level
< 0) || (level
> 7)))
8264 /* safety net should the EC not support AUTO
8265 * or FULLSPEED mode bits and just ignore them */
8266 if (level
& TP_EC_FAN_FULLSPEED
)
8267 level
|= 7; /* safety min speed 7 */
8268 else if (level
& TP_EC_FAN_AUTO
)
8269 level
|= 4; /* safety min speed 4 */
8271 if (!acpi_ec_write(fan_status_offset
, level
))
8274 tp_features
.fan_ctrl_status_undef
= 0;
8281 vdbg_printk(TPACPI_DBG_FAN
,
8282 "fan control: set fan control register to 0x%02x\n", level
);
8286 static int fan_set_level_safe(int level
)
8290 if (!fan_control_allowed
)
8293 if (mutex_lock_killable(&fan_mutex
))
8294 return -ERESTARTSYS
;
8296 if (level
== TPACPI_FAN_LAST_LEVEL
)
8297 level
= fan_control_desired_level
;
8299 rc
= fan_set_level(level
);
8301 fan_update_desired_level(level
);
8303 mutex_unlock(&fan_mutex
);
8307 static int fan_set_enable(void)
8312 if (!fan_control_allowed
)
8315 if (mutex_lock_killable(&fan_mutex
))
8316 return -ERESTARTSYS
;
8318 switch (fan_control_access_mode
) {
8319 case TPACPI_FAN_WR_ACPI_FANS
:
8320 case TPACPI_FAN_WR_TPEC
:
8321 rc
= fan_get_status(&s
);
8325 /* Don't go out of emergency fan mode */
8328 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
8331 if (!acpi_ec_write(fan_status_offset
, s
))
8334 tp_features
.fan_ctrl_status_undef
= 0;
8339 case TPACPI_FAN_WR_ACPI_SFAN
:
8340 rc
= fan_get_status(&s
);
8346 /* Set fan to at least level 4 */
8349 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
8359 mutex_unlock(&fan_mutex
);
8362 vdbg_printk(TPACPI_DBG_FAN
,
8363 "fan control: set fan control register to 0x%02x\n",
8368 static int fan_set_disable(void)
8372 if (!fan_control_allowed
)
8375 if (mutex_lock_killable(&fan_mutex
))
8376 return -ERESTARTSYS
;
8379 switch (fan_control_access_mode
) {
8380 case TPACPI_FAN_WR_ACPI_FANS
:
8381 case TPACPI_FAN_WR_TPEC
:
8382 if (!acpi_ec_write(fan_status_offset
, 0x00))
8385 fan_control_desired_level
= 0;
8386 tp_features
.fan_ctrl_status_undef
= 0;
8390 case TPACPI_FAN_WR_ACPI_SFAN
:
8391 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
8394 fan_control_desired_level
= 0;
8402 vdbg_printk(TPACPI_DBG_FAN
,
8403 "fan control: set fan control register to 0\n");
8405 mutex_unlock(&fan_mutex
);
8409 static int fan_set_speed(int speed
)
8413 if (!fan_control_allowed
)
8416 if (mutex_lock_killable(&fan_mutex
))
8417 return -ERESTARTSYS
;
8420 switch (fan_control_access_mode
) {
8421 case TPACPI_FAN_WR_ACPI_FANS
:
8422 if (speed
>= 0 && speed
<= 65535) {
8423 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
8424 speed
, speed
, speed
))
8434 mutex_unlock(&fan_mutex
);
8438 static void fan_watchdog_reset(void)
8440 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
8443 if (fan_watchdog_maxinterval
> 0 &&
8444 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
8445 mod_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
8446 msecs_to_jiffies(fan_watchdog_maxinterval
* 1000));
8448 cancel_delayed_work(&fan_watchdog_task
);
8451 static void fan_watchdog_fire(struct work_struct
*ignored
)
8455 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
8458 pr_notice("fan watchdog: enabling fan\n");
8459 rc
= fan_set_enable();
8461 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n",
8463 /* reschedule for later */
8464 fan_watchdog_reset();
8469 * SYSFS fan layout: hwmon compatible (device)
8472 * 0: "disengaged" mode
8474 * 2: native EC "auto" mode (recommended, hardware default)
8476 * pwm*: set speed in manual mode, ignored otherwise.
8477 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8480 * fan*_input: tachometer reading, RPM
8483 * SYSFS fan layout: extensions
8485 * fan_watchdog (driver):
8486 * fan watchdog interval in seconds, 0 disables (default), max 120
8489 /* sysfs fan pwm1_enable ----------------------------------------------- */
8490 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
8491 struct device_attribute
*attr
,
8497 res
= fan_get_status_safe(&status
);
8501 if (status
& TP_EC_FAN_FULLSPEED
) {
8503 } else if (status
& TP_EC_FAN_AUTO
) {
8508 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
8511 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
8512 struct device_attribute
*attr
,
8513 const char *buf
, size_t count
)
8518 if (parse_strtoul(buf
, 2, &t
))
8521 tpacpi_disclose_usertask("hwmon pwm1_enable",
8522 "set fan mode to %lu\n", t
);
8526 level
= TP_EC_FAN_FULLSPEED
;
8529 level
= TPACPI_FAN_LAST_LEVEL
;
8532 level
= TP_EC_FAN_AUTO
;
8535 /* reserved for software-controlled auto mode */
8541 res
= fan_set_level_safe(level
);
8547 fan_watchdog_reset();
8552 static DEVICE_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
8553 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
8555 /* sysfs fan pwm1 ------------------------------------------------------ */
8556 static ssize_t
fan_pwm1_show(struct device
*dev
,
8557 struct device_attribute
*attr
,
8563 res
= fan_get_status_safe(&status
);
8568 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
8569 status
= fan_control_desired_level
;
8574 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
8577 static ssize_t
fan_pwm1_store(struct device
*dev
,
8578 struct device_attribute
*attr
,
8579 const char *buf
, size_t count
)
8583 u8 status
, newlevel
;
8585 if (parse_strtoul(buf
, 255, &s
))
8588 tpacpi_disclose_usertask("hwmon pwm1",
8589 "set fan speed to %lu\n", s
);
8591 /* scale down from 0-255 to 0-7 */
8592 newlevel
= (s
>> 5) & 0x07;
8594 if (mutex_lock_killable(&fan_mutex
))
8595 return -ERESTARTSYS
;
8597 rc
= fan_get_status(&status
);
8598 if (!rc
&& (status
&
8599 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
8600 rc
= fan_set_level(newlevel
);
8604 fan_update_desired_level(newlevel
);
8605 fan_watchdog_reset();
8609 mutex_unlock(&fan_mutex
);
8610 return (rc
) ? rc
: count
;
8613 static DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, fan_pwm1_show
, fan_pwm1_store
);
8615 /* sysfs fan fan1_input ------------------------------------------------ */
8616 static ssize_t
fan_fan1_input_show(struct device
*dev
,
8617 struct device_attribute
*attr
,
8623 res
= fan_get_speed(&speed
);
8627 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
8630 static DEVICE_ATTR(fan1_input
, S_IRUGO
, fan_fan1_input_show
, NULL
);
8632 /* sysfs fan fan2_input ------------------------------------------------ */
8633 static ssize_t
fan_fan2_input_show(struct device
*dev
,
8634 struct device_attribute
*attr
,
8640 res
= fan2_get_speed(&speed
);
8644 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
8647 static DEVICE_ATTR(fan2_input
, S_IRUGO
, fan_fan2_input_show
, NULL
);
8649 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8650 static ssize_t
fan_watchdog_show(struct device_driver
*drv
, char *buf
)
8652 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
8655 static ssize_t
fan_watchdog_store(struct device_driver
*drv
, const char *buf
,
8660 if (parse_strtoul(buf
, 120, &t
))
8663 if (!fan_control_allowed
)
8666 fan_watchdog_maxinterval
= t
;
8667 fan_watchdog_reset();
8669 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
8673 static DRIVER_ATTR_RW(fan_watchdog
);
8675 /* --------------------------------------------------------------------- */
8676 static struct attribute
*fan_attributes
[] = {
8677 &dev_attr_pwm1_enable
.attr
, &dev_attr_pwm1
.attr
,
8678 &dev_attr_fan1_input
.attr
,
8679 NULL
, /* for fan2_input */
8683 static const struct attribute_group fan_attr_group
= {
8684 .attrs
= fan_attributes
,
8687 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8688 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8690 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8691 { .vendor = PCI_VENDOR_ID_IBM, \
8692 .bios = TPACPI_MATCH_ANY, \
8693 .ec = TPID(__id1, __id2), \
8694 .quirks = __quirks }
8696 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8697 { .vendor = PCI_VENDOR_ID_LENOVO, \
8698 .bios = TPACPI_MATCH_ANY, \
8699 .ec = TPID(__id1, __id2), \
8700 .quirks = __quirks }
8702 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
8703 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
8704 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
8705 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
8706 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
8707 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
8710 #undef TPACPI_FAN_QL
8711 #undef TPACPI_FAN_QI
8713 static int __init
fan_init(struct ibm_init_struct
*iibm
)
8716 unsigned long quirks
;
8718 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8719 "initializing fan subdriver\n");
8721 mutex_init(&fan_mutex
);
8722 fan_status_access_mode
= TPACPI_FAN_NONE
;
8723 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
8724 fan_control_commands
= 0;
8725 fan_watchdog_maxinterval
= 0;
8726 tp_features
.fan_ctrl_status_undef
= 0;
8727 tp_features
.second_fan
= 0;
8728 fan_control_desired_level
= 7;
8730 if (tpacpi_is_ibm()) {
8731 TPACPI_ACPIHANDLE_INIT(fans
);
8732 TPACPI_ACPIHANDLE_INIT(gfan
);
8733 TPACPI_ACPIHANDLE_INIT(sfan
);
8736 quirks
= tpacpi_check_quirks(fan_quirk_table
,
8737 ARRAY_SIZE(fan_quirk_table
));
8740 /* 570, 600e/x, 770e, 770x */
8741 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
8743 /* all other ThinkPads: note that even old-style
8744 * ThinkPad ECs supports the fan control register */
8745 if (likely(acpi_ec_read(fan_status_offset
,
8746 &fan_control_initial_status
))) {
8747 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
8748 if (quirks
& TPACPI_FAN_Q1
)
8750 if (quirks
& TPACPI_FAN_2FAN
) {
8751 tp_features
.second_fan
= 1;
8752 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8753 "secondary fan support enabled\n");
8756 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n");
8763 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
8764 fan_control_commands
|=
8765 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
8768 /* gfan without sfan means no fan control */
8769 /* all other models implement TP EC 0x2f control */
8773 fan_control_access_mode
=
8774 TPACPI_FAN_WR_ACPI_FANS
;
8775 fan_control_commands
|=
8776 TPACPI_FAN_CMD_SPEED
|
8777 TPACPI_FAN_CMD_LEVEL
|
8778 TPACPI_FAN_CMD_ENABLE
;
8780 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
8781 fan_control_commands
|=
8782 TPACPI_FAN_CMD_LEVEL
|
8783 TPACPI_FAN_CMD_ENABLE
;
8788 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8789 "fan is %s, modes %d, %d\n",
8790 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
8791 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
8792 fan_status_access_mode
, fan_control_access_mode
);
8794 /* fan control master switch */
8795 if (!fan_control_allowed
) {
8796 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
8797 fan_control_commands
= 0;
8798 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8799 "fan control features disabled by parameter\n");
8802 /* update fan_control_desired_level */
8803 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
8804 fan_get_status_safe(NULL
);
8806 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
8807 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
8808 if (tp_features
.second_fan
) {
8809 /* attach second fan tachometer */
8810 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
8811 &dev_attr_fan2_input
.attr
;
8813 rc
= sysfs_create_group(&tpacpi_hwmon
->kobj
,
8818 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
8819 &driver_attr_fan_watchdog
);
8821 sysfs_remove_group(&tpacpi_hwmon
->kobj
,
8830 static void fan_exit(void)
8832 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
8833 "cancelling any pending fan watchdog tasks\n");
8835 /* FIXME: can we really do this unconditionally? */
8836 sysfs_remove_group(&tpacpi_hwmon
->kobj
, &fan_attr_group
);
8837 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
8838 &driver_attr_fan_watchdog
);
8840 cancel_delayed_work(&fan_watchdog_task
);
8841 flush_workqueue(tpacpi_wq
);
8844 static void fan_suspend(void)
8848 if (!fan_control_allowed
)
8851 /* Store fan status in cache */
8852 fan_control_resume_level
= 0;
8853 rc
= fan_get_status_safe(&fan_control_resume_level
);
8855 pr_notice("failed to read fan level for later restore during resume: %d\n",
8858 /* if it is undefined, don't attempt to restore it.
8860 if (tp_features
.fan_ctrl_status_undef
)
8861 fan_control_resume_level
= 0;
8864 static void fan_resume(void)
8866 u8 current_level
= 7;
8867 bool do_set
= false;
8870 /* DSDT *always* updates status on resume */
8871 tp_features
.fan_ctrl_status_undef
= 0;
8873 if (!fan_control_allowed
||
8874 !fan_control_resume_level
||
8875 (fan_get_status_safe(¤t_level
) < 0))
8878 switch (fan_control_access_mode
) {
8879 case TPACPI_FAN_WR_ACPI_SFAN
:
8880 /* never decrease fan level */
8881 do_set
= (fan_control_resume_level
> current_level
);
8883 case TPACPI_FAN_WR_ACPI_FANS
:
8884 case TPACPI_FAN_WR_TPEC
:
8885 /* never decrease fan level, scale is:
8886 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8888 * We expect the firmware to set either 7 or AUTO, but we
8889 * handle FULLSPEED out of paranoia.
8891 * So, we can safely only restore FULLSPEED or 7, anything
8892 * else could slow the fan. Restoring AUTO is useless, at
8893 * best that's exactly what the DSDT already set (it is the
8896 * Always keep in mind that the DSDT *will* have set the
8897 * fans to what the vendor supposes is the best level. We
8898 * muck with it only to speed the fan up.
8900 if (fan_control_resume_level
!= 7 &&
8901 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
8904 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
8905 (current_level
!= fan_control_resume_level
);
8911 pr_notice("restoring fan level to 0x%02x\n",
8912 fan_control_resume_level
);
8913 rc
= fan_set_level_safe(fan_control_resume_level
);
8915 pr_notice("failed to restore fan level: %d\n", rc
);
8919 static int fan_read(struct seq_file
*m
)
8923 unsigned int speed
= 0;
8925 switch (fan_status_access_mode
) {
8926 case TPACPI_FAN_RD_ACPI_GFAN
:
8927 /* 570, 600e/x, 770e, 770x */
8928 rc
= fan_get_status_safe(&status
);
8932 seq_printf(m
, "status:\t\t%s\n"
8934 (status
!= 0) ? "enabled" : "disabled", status
);
8937 case TPACPI_FAN_RD_TPEC
:
8938 /* all except 570, 600e/x, 770e, 770x */
8939 rc
= fan_get_status_safe(&status
);
8943 seq_printf(m
, "status:\t\t%s\n",
8944 (status
!= 0) ? "enabled" : "disabled");
8946 rc
= fan_get_speed(&speed
);
8950 seq_printf(m
, "speed:\t\t%d\n", speed
);
8952 if (status
& TP_EC_FAN_FULLSPEED
)
8953 /* Disengaged mode takes precedence */
8954 seq_printf(m
, "level:\t\tdisengaged\n");
8955 else if (status
& TP_EC_FAN_AUTO
)
8956 seq_printf(m
, "level:\t\tauto\n");
8958 seq_printf(m
, "level:\t\t%d\n", status
);
8961 case TPACPI_FAN_NONE
:
8963 seq_printf(m
, "status:\t\tnot supported\n");
8966 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
8967 seq_printf(m
, "commands:\tlevel <level>");
8969 switch (fan_control_access_mode
) {
8970 case TPACPI_FAN_WR_ACPI_SFAN
:
8971 seq_printf(m
, " (<level> is 0-7)\n");
8975 seq_printf(m
, " (<level> is 0-7, auto, disengaged, full-speed)\n");
8980 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
8981 seq_printf(m
, "commands:\tenable, disable\n"
8982 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n");
8984 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
8985 seq_printf(m
, "commands:\tspeed <speed> (<speed> is 0-65535)\n");
8990 static int fan_write_cmd_level(const char *cmd
, int *rc
)
8994 if (strlencmp(cmd
, "level auto") == 0)
8995 level
= TP_EC_FAN_AUTO
;
8996 else if ((strlencmp(cmd
, "level disengaged") == 0) |
8997 (strlencmp(cmd
, "level full-speed") == 0))
8998 level
= TP_EC_FAN_FULLSPEED
;
8999 else if (sscanf(cmd
, "level %d", &level
) != 1)
9002 *rc
= fan_set_level_safe(level
);
9004 pr_err("level command accepted for unsupported access mode %d\n",
9005 fan_control_access_mode
);
9007 tpacpi_disclose_usertask("procfs fan",
9008 "set level to %d\n", level
);
9013 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
9015 if (strlencmp(cmd
, "enable") != 0)
9018 *rc
= fan_set_enable();
9020 pr_err("enable command accepted for unsupported access mode %d\n",
9021 fan_control_access_mode
);
9023 tpacpi_disclose_usertask("procfs fan", "enable\n");
9028 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
9030 if (strlencmp(cmd
, "disable") != 0)
9033 *rc
= fan_set_disable();
9035 pr_err("disable command accepted for unsupported access mode %d\n",
9036 fan_control_access_mode
);
9038 tpacpi_disclose_usertask("procfs fan", "disable\n");
9043 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
9048 * Support speed <low> <medium> <high> ? */
9050 if (sscanf(cmd
, "speed %d", &speed
) != 1)
9053 *rc
= fan_set_speed(speed
);
9055 pr_err("speed command accepted for unsupported access mode %d\n",
9056 fan_control_access_mode
);
9058 tpacpi_disclose_usertask("procfs fan",
9059 "set speed to %d\n", speed
);
9064 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
9068 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
9071 if (interval
< 0 || interval
> 120)
9074 fan_watchdog_maxinterval
= interval
;
9075 tpacpi_disclose_usertask("procfs fan",
9076 "set watchdog timer to %d\n",
9083 static int fan_write(char *buf
)
9088 while (!rc
&& (cmd
= next_cmd(&buf
))) {
9089 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
9090 fan_write_cmd_level(cmd
, &rc
)) &&
9091 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
9092 (fan_write_cmd_enable(cmd
, &rc
) ||
9093 fan_write_cmd_disable(cmd
, &rc
) ||
9094 fan_write_cmd_watchdog(cmd
, &rc
))) &&
9095 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
9096 fan_write_cmd_speed(cmd
, &rc
))
9100 fan_watchdog_reset();
9106 static struct ibm_struct fan_driver_data
= {
9111 .suspend
= fan_suspend
,
9112 .resume
= fan_resume
,
9115 /*************************************************************************
9116 * Mute LED subdriver
9120 struct tp_led_table
{
9127 static struct tp_led_table led_tables
[] = {
9128 [TPACPI_LED_MUTE
] = {
9133 [TPACPI_LED_MICMUTE
] = {
9140 static int mute_led_on_off(struct tp_led_table
*t
, bool state
)
9145 if (ACPI_FAILURE(acpi_get_handle(hkey_handle
, t
->name
, &temp
))) {
9146 pr_warn("Thinkpad ACPI has no %s interface.\n", t
->name
);
9150 if (!acpi_evalf(hkey_handle
, &output
, t
->name
, "dd",
9151 state
? t
->on_value
: t
->off_value
))
9158 int tpacpi_led_set(int whichled
, bool on
)
9160 struct tp_led_table
*t
;
9162 if (whichled
< 0 || whichled
>= TPACPI_LED_MAX
)
9165 t
= &led_tables
[whichled
];
9166 if (t
->state
< 0 || t
->state
== on
)
9168 return mute_led_on_off(t
, on
);
9170 EXPORT_SYMBOL_GPL(tpacpi_led_set
);
9172 static int mute_led_init(struct ibm_init_struct
*iibm
)
9177 for (i
= 0; i
< TPACPI_LED_MAX
; i
++) {
9178 struct tp_led_table
*t
= &led_tables
[i
];
9179 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle
, t
->name
, &temp
)))
9180 mute_led_on_off(t
, false);
9187 static void mute_led_exit(void)
9191 for (i
= 0; i
< TPACPI_LED_MAX
; i
++)
9192 tpacpi_led_set(i
, false);
9195 static void mute_led_resume(void)
9199 for (i
= 0; i
< TPACPI_LED_MAX
; i
++) {
9200 struct tp_led_table
*t
= &led_tables
[i
];
9202 mute_led_on_off(t
, t
->state
);
9206 static struct ibm_struct mute_led_driver_data
= {
9208 .exit
= mute_led_exit
,
9209 .resume
= mute_led_resume
,
9212 /****************************************************************************
9213 ****************************************************************************
9217 ****************************************************************************
9218 ****************************************************************************/
9221 * HKEY event callout for other subdrivers go here
9222 * (yes, it is ugly, but it is quick, safe, and gets the job done
9224 static void tpacpi_driver_event(const unsigned int hkey_event
)
9226 if (ibm_backlight_device
) {
9227 switch (hkey_event
) {
9228 case TP_HKEY_EV_BRGHT_UP
:
9229 case TP_HKEY_EV_BRGHT_DOWN
:
9230 tpacpi_brightness_notify_change();
9234 switch (hkey_event
) {
9235 case TP_HKEY_EV_VOL_UP
:
9236 case TP_HKEY_EV_VOL_DOWN
:
9237 case TP_HKEY_EV_VOL_MUTE
:
9238 volume_alsa_notify_change();
9241 if (tp_features
.kbdlight
&& hkey_event
== TP_HKEY_EV_KBD_LIGHT
) {
9242 enum led_brightness brightness
;
9244 mutex_lock(&kbdlight_mutex
);
9247 * Check the brightness actually changed, setting the brightness
9248 * through kbdlight_set_level() also triggers this event.
9250 brightness
= kbdlight_sysfs_get(NULL
);
9251 if (kbdlight_brightness
!= brightness
) {
9252 kbdlight_brightness
= brightness
;
9253 led_classdev_notify_brightness_hw_changed(
9254 &tpacpi_led_kbdlight
.led_classdev
, brightness
);
9257 mutex_unlock(&kbdlight_mutex
);
9261 static void hotkey_driver_event(const unsigned int scancode
)
9263 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
9266 /* --------------------------------------------------------------------- */
9269 static struct proc_dir_entry
*proc_dir
;
9272 * Module and infrastructure proble, init and exit handling
9275 static bool force_load
;
9277 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9278 static const char * __init
str_supported(int is_supported
)
9280 static char text_unsupported
[] __initdata
= "not supported";
9282 return (is_supported
) ? &text_unsupported
[4] : &text_unsupported
[0];
9284 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9286 static void ibm_exit(struct ibm_struct
*ibm
)
9288 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
9290 list_del_init(&ibm
->all_drivers
);
9292 if (ibm
->flags
.acpi_notify_installed
) {
9293 dbg_printk(TPACPI_DBG_EXIT
,
9294 "%s: acpi_remove_notify_handler\n", ibm
->name
);
9296 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
9298 dispatch_acpi_notify
);
9299 ibm
->flags
.acpi_notify_installed
= 0;
9302 if (ibm
->flags
.proc_created
) {
9303 dbg_printk(TPACPI_DBG_EXIT
,
9304 "%s: remove_proc_entry\n", ibm
->name
);
9305 remove_proc_entry(ibm
->name
, proc_dir
);
9306 ibm
->flags
.proc_created
= 0;
9309 if (ibm
->flags
.acpi_driver_registered
) {
9310 dbg_printk(TPACPI_DBG_EXIT
,
9311 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
9313 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
9314 kfree(ibm
->acpi
->driver
);
9315 ibm
->acpi
->driver
= NULL
;
9316 ibm
->flags
.acpi_driver_registered
= 0;
9319 if (ibm
->flags
.init_called
&& ibm
->exit
) {
9321 ibm
->flags
.init_called
= 0;
9324 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
9327 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
9330 struct ibm_struct
*ibm
= iibm
->data
;
9331 struct proc_dir_entry
*entry
;
9333 BUG_ON(ibm
== NULL
);
9335 INIT_LIST_HEAD(&ibm
->all_drivers
);
9337 if (ibm
->flags
.experimental
&& !experimental
)
9340 dbg_printk(TPACPI_DBG_INIT
,
9341 "probing for %s\n", ibm
->name
);
9344 ret
= iibm
->init(iibm
);
9346 return 0; /* probe failed */
9350 ibm
->flags
.init_called
= 1;
9354 if (ibm
->acpi
->hid
) {
9355 ret
= register_tpacpi_subdriver(ibm
);
9360 if (ibm
->acpi
->notify
) {
9361 ret
= setup_acpi_notify(ibm
);
9362 if (ret
== -ENODEV
) {
9363 pr_notice("disabling subdriver %s\n",
9373 dbg_printk(TPACPI_DBG_INIT
,
9374 "%s installed\n", ibm
->name
);
9377 umode_t mode
= iibm
->base_procfs_mode
;
9383 entry
= proc_create_data(ibm
->name
, mode
, proc_dir
,
9384 &dispatch_proc_fops
, ibm
);
9386 pr_err("unable to create proc entry %s\n", ibm
->name
);
9390 ibm
->flags
.proc_created
= 1;
9393 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
9398 dbg_printk(TPACPI_DBG_INIT
,
9399 "%s: at error exit path with result %d\n",
9403 return (ret
< 0) ? ret
: 0;
9408 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
9410 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
9413 static bool __pure __init
tpacpi_is_valid_fw_id(const char * const s
,
9417 * Most models: xxyTkkWW (#.##c)
9418 * Ancient 570/600 and -SL lacks (#.##c)
9420 if (s
&& strlen(s
) >= 8 &&
9421 tpacpi_is_fw_digit(s
[0]) &&
9422 tpacpi_is_fw_digit(s
[1]) &&
9424 (s
[3] == 'T' || s
[3] == 'N') &&
9425 tpacpi_is_fw_digit(s
[4]) &&
9426 tpacpi_is_fw_digit(s
[5]))
9429 /* New models: xxxyTkkW (#.##c); T550 and some others */
9430 return s
&& strlen(s
) >= 8 &&
9431 tpacpi_is_fw_digit(s
[0]) &&
9432 tpacpi_is_fw_digit(s
[1]) &&
9433 tpacpi_is_fw_digit(s
[2]) &&
9435 (s
[4] == 'T' || s
[4] == 'N') &&
9436 tpacpi_is_fw_digit(s
[5]) &&
9437 tpacpi_is_fw_digit(s
[6]);
9440 /* returns 0 - probe ok, or < 0 - probe error.
9441 * Probe ok doesn't mean thinkpad found.
9442 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9443 static int __must_check __init
get_thinkpad_model_data(
9444 struct thinkpad_id_data
*tp
)
9446 const struct dmi_device
*dev
= NULL
;
9447 char ec_fw_string
[18];
9453 memset(tp
, 0, sizeof(*tp
));
9455 if (dmi_name_in_vendors("IBM"))
9456 tp
->vendor
= PCI_VENDOR_ID_IBM
;
9457 else if (dmi_name_in_vendors("LENOVO"))
9458 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
9462 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
9463 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
9464 if (s
&& !tp
->bios_version_str
)
9467 /* Really ancient ThinkPad 240X will fail this, which is fine */
9468 if (!(tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E') ||
9469 tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'C')))
9472 tp
->bios_model
= tp
->bios_version_str
[0]
9473 | (tp
->bios_version_str
[1] << 8);
9474 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
9475 | tp
->bios_version_str
[5];
9478 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9479 * X32 or newer, all Z series; Some models must have an
9480 * up-to-date BIOS or they will not be detected.
9482 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9484 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
9485 if (sscanf(dev
->name
,
9486 "IBM ThinkPad Embedded Controller -[%17c",
9487 ec_fw_string
) == 1) {
9488 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
9489 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
9491 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
9492 if (!tp
->ec_version_str
)
9495 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
9496 tp
->ec_model
= ec_fw_string
[0]
9497 | (ec_fw_string
[1] << 8);
9498 tp
->ec_release
= (ec_fw_string
[4] << 8)
9501 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n",
9503 pr_notice("please report this to %s\n",
9510 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
9511 if (s
&& !(strncasecmp(s
, "ThinkPad", 8) && strncasecmp(s
, "Lenovo", 6))) {
9512 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
9516 s
= dmi_get_system_info(DMI_BIOS_VENDOR
);
9517 if (s
&& !(strncasecmp(s
, "Lenovo", 6))) {
9518 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
9524 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
9525 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
9526 if (s
&& !tp
->nummodel_str
)
9532 static int __init
probe_for_thinkpad(void)
9539 /* It would be dangerous to run the driver in this case */
9540 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9544 * Non-ancient models have better DMI tagging, but very old models
9545 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9547 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
) ||
9548 (thinkpad_id
.ec_model
!= 0) ||
9549 tpacpi_is_fw_known();
9551 /* The EC handler is required */
9552 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID
, &ec_handle
);
9555 pr_err("Not yet supported ThinkPad detected!\n");
9559 if (!is_thinkpad
&& !force_load
)
9565 static void __init
thinkpad_acpi_init_banner(void)
9567 pr_info("%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
9568 pr_info("%s\n", TPACPI_URL
);
9570 pr_info("ThinkPad BIOS %s, EC %s\n",
9571 (thinkpad_id
.bios_version_str
) ?
9572 thinkpad_id
.bios_version_str
: "unknown",
9573 (thinkpad_id
.ec_version_str
) ?
9574 thinkpad_id
.ec_version_str
: "unknown");
9576 BUG_ON(!thinkpad_id
.vendor
);
9578 if (thinkpad_id
.model_str
)
9579 pr_info("%s %s, model %s\n",
9580 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
9581 "IBM" : ((thinkpad_id
.vendor
==
9582 PCI_VENDOR_ID_LENOVO
) ?
9583 "Lenovo" : "Unknown vendor"),
9584 thinkpad_id
.model_str
,
9585 (thinkpad_id
.nummodel_str
) ?
9586 thinkpad_id
.nummodel_str
: "unknown");
9589 /* Module init, exit, parameters */
9591 static struct ibm_init_struct ibms_init
[] __initdata
= {
9593 .data
= &thinkpad_acpi_driver_data
,
9596 .init
= hotkey_init
,
9597 .data
= &hotkey_driver_data
,
9600 .init
= bluetooth_init
,
9601 .data
= &bluetooth_driver_data
,
9605 .data
= &wan_driver_data
,
9609 .data
= &uwb_driver_data
,
9611 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9614 .base_procfs_mode
= S_IRUSR
,
9615 .data
= &video_driver_data
,
9619 .init
= kbdlight_init
,
9620 .data
= &kbdlight_driver_data
,
9624 .data
= &light_driver_data
,
9628 .data
= &cmos_driver_data
,
9632 .data
= &led_driver_data
,
9636 .data
= &beep_driver_data
,
9639 .init
= thermal_init
,
9640 .data
= &thermal_driver_data
,
9643 .init
= brightness_init
,
9644 .data
= &brightness_driver_data
,
9647 .init
= volume_init
,
9648 .data
= &volume_driver_data
,
9652 .data
= &fan_driver_data
,
9655 .init
= mute_led_init
,
9656 .data
= &mute_led_driver_data
,
9660 static int __init
set_ibm_param(const char *val
, const struct kernel_param
*kp
)
9663 struct ibm_struct
*ibm
;
9665 if (!kp
|| !kp
->name
|| !val
)
9668 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
9669 ibm
= ibms_init
[i
].data
;
9670 WARN_ON(ibm
== NULL
);
9672 if (!ibm
|| !ibm
->name
)
9675 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
9676 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
9678 strcpy(ibms_init
[i
].param
, val
);
9679 strcat(ibms_init
[i
].param
, ",");
9687 module_param(experimental
, int, 0444);
9688 MODULE_PARM_DESC(experimental
,
9689 "Enables experimental features when non-zero");
9691 module_param_named(debug
, dbg_level
, uint
, 0);
9692 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
9694 module_param(force_load
, bool, 0444);
9695 MODULE_PARM_DESC(force_load
,
9696 "Attempts to load the driver even on a mis-identified ThinkPad when true");
9698 module_param_named(fan_control
, fan_control_allowed
, bool, 0444);
9699 MODULE_PARM_DESC(fan_control
,
9700 "Enables setting fan parameters features when true");
9702 module_param_named(brightness_mode
, brightness_mode
, uint
, 0444);
9703 MODULE_PARM_DESC(brightness_mode
,
9704 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9706 module_param(brightness_enable
, uint
, 0444);
9707 MODULE_PARM_DESC(brightness_enable
,
9708 "Enables backlight control when 1, disables when 0");
9710 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9711 module_param_named(volume_mode
, volume_mode
, uint
, 0444);
9712 MODULE_PARM_DESC(volume_mode
,
9713 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9715 module_param_named(volume_capabilities
, volume_capabilities
, uint
, 0444);
9716 MODULE_PARM_DESC(volume_capabilities
,
9717 "Selects the mixer capabilites: 0=auto, 1=volume and mute, 2=mute only");
9719 module_param_named(volume_control
, volume_control_allowed
, bool, 0444);
9720 MODULE_PARM_DESC(volume_control
,
9721 "Enables software override for the console audio control when true");
9723 module_param_named(software_mute
, software_mute_requested
, bool, 0444);
9724 MODULE_PARM_DESC(software_mute
,
9725 "Request full software mute control");
9727 /* ALSA module API parameters */
9728 module_param_named(index
, alsa_index
, int, 0444);
9729 MODULE_PARM_DESC(index
, "ALSA index for the ACPI EC Mixer");
9730 module_param_named(id
, alsa_id
, charp
, 0444);
9731 MODULE_PARM_DESC(id
, "ALSA id for the ACPI EC Mixer");
9732 module_param_named(enable
, alsa_enable
, bool, 0444);
9733 MODULE_PARM_DESC(enable
, "Enable the ALSA interface for the ACPI EC Mixer");
9734 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9736 /* The module parameter can't be read back, that's why 0 is used here */
9737 #define TPACPI_PARAM(feature) \
9738 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9739 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation")
9741 TPACPI_PARAM(hotkey
);
9742 TPACPI_PARAM(bluetooth
);
9743 TPACPI_PARAM(video
);
9744 TPACPI_PARAM(light
);
9748 TPACPI_PARAM(brightness
);
9749 TPACPI_PARAM(volume
);
9752 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9753 module_param(dbg_wlswemul
, uint
, 0444);
9754 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
9755 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
9756 MODULE_PARM_DESC(wlsw_state
,
9757 "Initial state of the emulated WLSW switch");
9759 module_param(dbg_bluetoothemul
, uint
, 0444);
9760 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
9761 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
9762 MODULE_PARM_DESC(bluetooth_state
,
9763 "Initial state of the emulated bluetooth switch");
9765 module_param(dbg_wwanemul
, uint
, 0444);
9766 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
9767 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
9768 MODULE_PARM_DESC(wwan_state
,
9769 "Initial state of the emulated WWAN switch");
9771 module_param(dbg_uwbemul
, uint
, 0444);
9772 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
9773 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
9774 MODULE_PARM_DESC(uwb_state
,
9775 "Initial state of the emulated UWB switch");
9778 static void thinkpad_acpi_module_exit(void)
9780 struct ibm_struct
*ibm
, *itmp
;
9782 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
9784 list_for_each_entry_safe_reverse(ibm
, itmp
,
9785 &tpacpi_all_drivers
,
9790 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
9792 if (tpacpi_inputdev
) {
9793 if (tp_features
.input_device_registered
)
9794 input_unregister_device(tpacpi_inputdev
);
9796 input_free_device(tpacpi_inputdev
);
9797 kfree(hotkey_keycode_map
);
9801 hwmon_device_unregister(tpacpi_hwmon
);
9803 if (tpacpi_sensors_pdev
)
9804 platform_device_unregister(tpacpi_sensors_pdev
);
9806 platform_device_unregister(tpacpi_pdev
);
9808 if (tp_features
.sensors_pdrv_attrs_registered
)
9809 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
9810 if (tp_features
.platform_drv_attrs_registered
)
9811 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
9813 if (tp_features
.sensors_pdrv_registered
)
9814 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
9816 if (tp_features
.platform_drv_registered
)
9817 platform_driver_unregister(&tpacpi_pdriver
);
9820 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
9823 destroy_workqueue(tpacpi_wq
);
9825 kfree(thinkpad_id
.bios_version_str
);
9826 kfree(thinkpad_id
.ec_version_str
);
9827 kfree(thinkpad_id
.model_str
);
9828 kfree(thinkpad_id
.nummodel_str
);
9832 static int __init
thinkpad_acpi_module_init(void)
9836 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
9838 /* Driver-level probe */
9840 ret
= get_thinkpad_model_data(&thinkpad_id
);
9842 pr_err("unable to get DMI data: %d\n", ret
);
9843 thinkpad_acpi_module_exit();
9846 ret
= probe_for_thinkpad();
9848 thinkpad_acpi_module_exit();
9852 /* Driver initialization */
9854 thinkpad_acpi_init_banner();
9855 tpacpi_check_outdated_fw();
9857 TPACPI_ACPIHANDLE_INIT(ecrd
);
9858 TPACPI_ACPIHANDLE_INIT(ecwr
);
9860 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
9862 thinkpad_acpi_module_exit();
9866 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
9868 pr_err("unable to create proc dir " TPACPI_PROC_DIR
"\n");
9869 thinkpad_acpi_module_exit();
9873 ret
= platform_driver_register(&tpacpi_pdriver
);
9875 pr_err("unable to register main platform driver\n");
9876 thinkpad_acpi_module_exit();
9879 tp_features
.platform_drv_registered
= 1;
9881 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
9883 pr_err("unable to register hwmon platform driver\n");
9884 thinkpad_acpi_module_exit();
9887 tp_features
.sensors_pdrv_registered
= 1;
9889 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
9891 tp_features
.platform_drv_attrs_registered
= 1;
9892 ret
= tpacpi_create_driver_attributes(
9893 &tpacpi_hwmon_pdriver
.driver
);
9896 pr_err("unable to create sysfs driver attributes\n");
9897 thinkpad_acpi_module_exit();
9900 tp_features
.sensors_pdrv_attrs_registered
= 1;
9903 /* Device initialization */
9904 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
9906 if (IS_ERR(tpacpi_pdev
)) {
9907 ret
= PTR_ERR(tpacpi_pdev
);
9909 pr_err("unable to register platform device\n");
9910 thinkpad_acpi_module_exit();
9913 tpacpi_sensors_pdev
= platform_device_register_simple(
9914 TPACPI_HWMON_DRVR_NAME
,
9916 if (IS_ERR(tpacpi_sensors_pdev
)) {
9917 ret
= PTR_ERR(tpacpi_sensors_pdev
);
9918 tpacpi_sensors_pdev
= NULL
;
9919 pr_err("unable to register hwmon platform device\n");
9920 thinkpad_acpi_module_exit();
9923 tp_features
.sensors_pdev_attrs_registered
= 1;
9924 tpacpi_hwmon
= hwmon_device_register_with_groups(
9925 &tpacpi_sensors_pdev
->dev
, TPACPI_NAME
, NULL
, NULL
);
9927 if (IS_ERR(tpacpi_hwmon
)) {
9928 ret
= PTR_ERR(tpacpi_hwmon
);
9929 tpacpi_hwmon
= NULL
;
9930 pr_err("unable to register hwmon device\n");
9931 thinkpad_acpi_module_exit();
9934 mutex_init(&tpacpi_inputdev_send_mutex
);
9935 tpacpi_inputdev
= input_allocate_device();
9936 if (!tpacpi_inputdev
) {
9937 thinkpad_acpi_module_exit();
9940 /* Prepare input device, but don't register */
9941 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
9942 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
9943 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
9944 tpacpi_inputdev
->id
.vendor
= thinkpad_id
.vendor
;
9945 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
9946 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
9947 tpacpi_inputdev
->dev
.parent
= &tpacpi_pdev
->dev
;
9950 /* Init subdriver dependencies */
9951 tpacpi_detect_brightness_capabilities();
9953 /* Init subdrivers */
9954 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
9955 ret
= ibm_init(&ibms_init
[i
]);
9956 if (ret
>= 0 && *ibms_init
[i
].param
)
9957 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
9959 thinkpad_acpi_module_exit();
9964 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
9966 ret
= input_register_device(tpacpi_inputdev
);
9968 pr_err("unable to register input device\n");
9969 thinkpad_acpi_module_exit();
9972 tp_features
.input_device_registered
= 1;
9978 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
9981 * This will autoload the driver in almost every ThinkPad
9982 * in widespread use.
9984 * Only _VERY_ old models, like the 240, 240x and 570 lack
9985 * the HKEY event interface.
9987 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
9990 * DMI matching for module autoloading
9992 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9993 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9995 * Only models listed in thinkwiki will be supported, so add yours
9996 * if it is not there yet.
9998 #define IBM_BIOS_MODULE_ALIAS(__type) \
9999 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
10001 /* Ancient thinkpad BIOSes have to be identified by
10002 * BIOS type or model number, and there are far less
10003 * BIOS types than model numbers... */
10004 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
10006 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
10007 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
10008 MODULE_DESCRIPTION(TPACPI_DESC
);
10009 MODULE_VERSION(TPACPI_VERSION
);
10010 MODULE_LICENSE("GPL");
10012 module_init(thinkpad_acpi_module_init
);
10013 module_exit(thinkpad_acpi_module_exit
);