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.24"
27 #define TPACPI_SYSFS_VERSION 0x020700
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
65 #include <linux/nvram.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/sysfs.h>
69 #include <linux/backlight.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <asm/uaccess.h>
79 #include <linux/dmi.h>
80 #include <linux/jiffies.h>
81 #include <linux/workqueue.h>
83 #include <sound/core.h>
84 #include <sound/control.h>
85 #include <sound/initval.h>
87 #include <acpi/acpi_drivers.h>
89 #include <linux/pci_ids.h>
92 /* ThinkPad CMOS commands */
93 #define TP_CMOS_VOLUME_DOWN 0
94 #define TP_CMOS_VOLUME_UP 1
95 #define TP_CMOS_VOLUME_MUTE 2
96 #define TP_CMOS_BRIGHTNESS_UP 4
97 #define TP_CMOS_BRIGHTNESS_DOWN 5
98 #define TP_CMOS_THINKLIGHT_ON 12
99 #define TP_CMOS_THINKLIGHT_OFF 13
101 /* NVRAM Addresses */
103 TP_NVRAM_ADDR_HK2
= 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT
= 0x58,
105 TP_NVRAM_ADDR_VIDEO
= 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS
= 0x5e,
107 TP_NVRAM_ADDR_MIXER
= 0x60,
110 /* NVRAM bit masks */
112 TP_NVRAM_MASK_HKT_THINKPAD
= 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM
= 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY
= 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE
= 0x80,
116 TP_NVRAM_MASK_THINKLIGHT
= 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND
= 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS
= 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS
= 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS
= 0,
121 TP_NVRAM_MASK_MUTE
= 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME
= 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME
= 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME
= 0,
127 /* Misc NVRAM-related */
129 TP_NVRAM_LEVEL_VOLUME_MAX
= 14,
133 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
143 TP_ACPI_WGSV_GET_STATE
= 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME
= 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
= 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE
= 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
151 TP_ACPI_WGSV_STATE_WWANEXIST
= 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR
= 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES
= 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF
= 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST
= 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR
= 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES
= 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF
= 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST
= 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR
= 0x0020, /* UWB radio enabled */
164 enum tpacpi_hkey_event_t
{
166 TP_HKEY_EV_HOTKEY_BASE
= 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP
= 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN
= 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP
= 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN
= 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE
= 0x1017, /* Mixer output mute */
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK
= 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK
= 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ
= 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ
= 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW
= 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW
= 0x2413, /* battery empty, S4 */
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK
= 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK
= 0x4003, /* undock complete */
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ
= 0x3006, /* opt. drive tray ejected */
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE
= 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN
= 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET
= 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK
= 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED
= 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED
= 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED
= 0x5010, /* backlight control event */
197 /* Key-related user-interface events */
198 TP_HKEY_EV_KEY_NUMLOCK
= 0x6000, /* NumLock key pressed */
199 TP_HKEY_EV_KEY_FN
= 0x6005, /* Fn key pressed? E420 */
202 TP_HKEY_EV_ALARM_BAT_HOT
= 0x6011, /* battery too hot */
203 TP_HKEY_EV_ALARM_BAT_XHOT
= 0x6012, /* battery critically hot */
204 TP_HKEY_EV_ALARM_SENSOR_HOT
= 0x6021, /* sensor too hot */
205 TP_HKEY_EV_ALARM_SENSOR_XHOT
= 0x6022, /* sensor critically hot */
206 TP_HKEY_EV_THM_TABLE_CHANGED
= 0x6030, /* thermal table changed */
208 TP_HKEY_EV_UNK_6040
= 0x6040, /* Related to AC change?
209 some sort of APM hint,
213 TP_HKEY_EV_RFKILL_CHANGED
= 0x7000, /* rfkill switch changed */
216 /****************************************************************************
220 #define TPACPI_NAME "thinkpad"
221 #define TPACPI_DESC "ThinkPad ACPI Extras"
222 #define TPACPI_FILE TPACPI_NAME "_acpi"
223 #define TPACPI_URL "http://ibm-acpi.sf.net/"
224 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
226 #define TPACPI_PROC_DIR "ibm"
227 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
228 #define TPACPI_DRVR_NAME TPACPI_FILE
229 #define TPACPI_DRVR_SHORTNAME "tpacpi"
230 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
232 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
233 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
235 #define TPACPI_MAX_ACPI_ARGS 3
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
259 struct tp_acpi_drv_struct
{
260 const struct acpi_device_id
*hid
;
261 struct acpi_driver
*driver
;
263 void (*notify
) (struct ibm_struct
*, u32
);
266 struct acpi_device
*device
;
272 int (*read
) (struct seq_file
*);
273 int (*write
) (char *);
275 void (*resume
) (void);
276 void (*suspend
) (pm_message_t state
);
277 void (*shutdown
) (void);
279 struct list_head all_drivers
;
281 struct tp_acpi_drv_struct
*acpi
;
284 u8 acpi_driver_registered
:1;
285 u8 acpi_notify_installed
:1;
292 struct ibm_init_struct
{
295 int (*init
) (struct ibm_init_struct
*);
296 mode_t base_procfs_mode
;
297 struct ibm_struct
*data
;
308 u32 bright_acpimode
:1;
312 u32 fan_ctrl_status_undef
:1;
314 u32 beep_needs_two_args
:1;
315 u32 mixer_no_level_control
:1;
316 u32 input_device_registered
:1;
317 u32 platform_drv_registered
:1;
318 u32 platform_drv_attrs_registered
:1;
319 u32 sensors_pdrv_registered
:1;
320 u32 sensors_pdrv_attrs_registered
:1;
321 u32 sensors_pdev_attrs_registered
:1;
322 u32 hotkey_poll_active
:1;
326 u16 hotkey_mask_ff
:1;
327 u16 volume_ctrl_forbidden
:1;
330 struct thinkpad_id_data
{
331 unsigned int vendor
; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
339 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
342 char *model_str
; /* ThinkPad T43 */
343 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id
;
348 TPACPI_LIFE_INIT
= 0,
353 static int experimental
;
354 static u32 dbg_level
;
356 static struct workqueue_struct
*tpacpi_wq
;
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev
{
366 struct led_classdev led_classdev
;
367 struct work_struct work
;
368 enum led_status_t new_state
;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl
; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul
;
377 static int tpacpi_wlsw_emulstate
;
378 static int dbg_bluetoothemul
;
379 static int tpacpi_bluetooth_emulstate
;
380 static int dbg_wwanemul
;
381 static int tpacpi_wwan_emulstate
;
382 static int dbg_uwbemul
;
383 static int tpacpi_uwb_emulstate
;
387 /*************************************************************************
391 #define dbg_printk(a_dbg_level, format, arg...) \
393 if (dbg_level & (a_dbg_level)) \
394 printk(KERN_DEBUG pr_fmt("%s: " format), \
398 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
399 #define vdbg_printk dbg_printk
400 static const char *str_supported(int is_supported
);
402 static inline const char *str_supported(int is_supported
) { return ""; }
403 #define vdbg_printk(a_dbg_level, format, arg...) \
404 no_printk(format, ##arg)
407 static void tpacpi_log_usertask(const char * const what
)
409 printk(KERN_DEBUG
pr_fmt("%s: access by process with PID %d\n"),
410 what
, task_tgid_vnr(current
));
413 #define tpacpi_disclose_usertask(what, format, arg...) \
415 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
416 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
417 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
418 what, task_tgid_vnr(current), ## arg); \
423 * Quirk handling helpers
425 * ThinkPad IDs and versions seen in the field so far
426 * are two-characters from the set [0-9A-Z], i.e. base 36.
428 * We use values well outside that range as specials.
431 #define TPACPI_MATCH_ANY 0xffffU
432 #define TPACPI_MATCH_UNKNOWN 0U
434 /* TPID('1', 'Y') == 0x5931 */
435 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
437 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_IBM, \
439 .bios = TPID(__id1, __id2), \
440 .ec = TPACPI_MATCH_ANY, \
441 .quirks = (__quirk) }
443 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
444 { .vendor = PCI_VENDOR_ID_LENOVO, \
445 .bios = TPID(__id1, __id2), \
446 .ec = TPACPI_MATCH_ANY, \
447 .quirks = (__quirk) }
449 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
450 { .vendor = PCI_VENDOR_ID_LENOVO, \
451 .bios = TPACPI_MATCH_ANY, \
452 .ec = TPID(__id1, __id2), \
453 .quirks = (__quirk) }
455 struct tpacpi_quirk
{
459 unsigned long quirks
;
463 * tpacpi_check_quirks() - search BIOS/EC version on a list
464 * @qlist: array of &struct tpacpi_quirk
465 * @qlist_size: number of elements in @qlist
467 * Iterates over a quirks list until one is found that matches the
468 * ThinkPad's vendor, BIOS and EC model.
470 * Returns 0 if nothing matches, otherwise returns the quirks field of
471 * the matching &struct tpacpi_quirk entry.
473 * The match criteria is: vendor, ec and bios much match.
475 static unsigned long __init
tpacpi_check_quirks(
476 const struct tpacpi_quirk
*qlist
,
477 unsigned int qlist_size
)
480 if ((qlist
->vendor
== thinkpad_id
.vendor
||
481 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
482 (qlist
->bios
== thinkpad_id
.bios_model
||
483 qlist
->bios
== TPACPI_MATCH_ANY
) &&
484 (qlist
->ec
== thinkpad_id
.ec_model
||
485 qlist
->ec
== TPACPI_MATCH_ANY
))
486 return qlist
->quirks
;
494 static inline bool __pure __init
tpacpi_is_lenovo(void)
496 return thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
;
499 static inline bool __pure __init
tpacpi_is_ibm(void)
501 return thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
;
504 /****************************************************************************
505 ****************************************************************************
507 * ACPI Helpers and device model
509 ****************************************************************************
510 ****************************************************************************/
512 /*************************************************************************
516 static acpi_handle root_handle
;
517 static acpi_handle ec_handle
;
519 #define TPACPI_HANDLE(object, parent, paths...) \
520 static acpi_handle object##_handle; \
521 static const acpi_handle *object##_parent __initdata = \
523 static char *object##_paths[] __initdata = { paths }
525 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
526 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
528 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
530 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
531 "\\CMS", /* R40, R40e */
534 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
535 "^HKEY", /* R30, R31 */
536 "HKEY", /* all others */
539 /*************************************************************************
543 static int acpi_evalf(acpi_handle handle
,
544 void *res
, char *method
, char *fmt
, ...)
547 struct acpi_object_list params
;
548 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
549 struct acpi_buffer result
, *resultp
;
550 union acpi_object out_obj
;
558 pr_err("acpi_evalf() called with empty format\n");
571 params
.pointer
= &in_objs
[0];
578 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
579 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
581 /* add more types as needed */
583 pr_err("acpi_evalf() called "
584 "with invalid format character '%c'\n", c
);
591 if (res_type
!= 'v') {
592 result
.length
= sizeof(out_obj
);
593 result
.pointer
= &out_obj
;
598 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
602 success
= (status
== AE_OK
&&
603 out_obj
.type
== ACPI_TYPE_INTEGER
);
605 *(int *)res
= out_obj
.integer
.value
;
608 success
= status
== AE_OK
;
610 /* add more types as needed */
612 pr_err("acpi_evalf() called "
613 "with invalid format character '%c'\n", res_type
);
617 if (!success
&& !quiet
)
618 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
619 method
, fmt0
, acpi_format_exception(status
));
624 static int acpi_ec_read(int i
, u8
*p
)
629 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
633 if (ec_read(i
, p
) < 0)
640 static int acpi_ec_write(int i
, u8 v
)
643 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
646 if (ec_write(i
, v
) < 0)
653 static int issue_thinkpad_cmos_command(int cmos_cmd
)
658 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
664 /*************************************************************************
668 #define TPACPI_ACPIHANDLE_INIT(object) \
669 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
670 object##_paths, ARRAY_SIZE(object##_paths))
672 static void __init
drv_acpi_handle_init(const char *name
,
673 acpi_handle
*handle
, const acpi_handle parent
,
674 char **paths
, const int num_paths
)
679 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
682 for (i
= 0; i
< num_paths
; i
++) {
683 status
= acpi_get_handle(parent
, paths
[i
], handle
);
684 if (ACPI_SUCCESS(status
)) {
685 dbg_printk(TPACPI_DBG_INIT
,
686 "Found ACPI handle %s for %s\n",
692 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
697 static acpi_status __init
tpacpi_acpi_handle_locate_callback(acpi_handle handle
,
698 u32 level
, void *context
, void **return_value
)
700 *(acpi_handle
*)return_value
= handle
;
702 return AE_CTRL_TERMINATE
;
705 static void __init
tpacpi_acpi_handle_locate(const char *name
,
710 acpi_handle device_found
;
712 BUG_ON(!name
|| !hid
|| !handle
);
713 vdbg_printk(TPACPI_DBG_INIT
,
714 "trying to locate ACPI handle for %s, using HID %s\n",
717 memset(&device_found
, 0, sizeof(device_found
));
718 status
= acpi_get_devices(hid
, tpacpi_acpi_handle_locate_callback
,
719 (void *)name
, &device_found
);
723 if (ACPI_SUCCESS(status
)) {
724 *handle
= device_found
;
725 dbg_printk(TPACPI_DBG_INIT
,
726 "Found ACPI handle for %s\n", name
);
728 vdbg_printk(TPACPI_DBG_INIT
,
729 "Could not locate an ACPI handle for %s: %s\n",
730 name
, acpi_format_exception(status
));
734 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
736 struct ibm_struct
*ibm
= data
;
738 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
741 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
744 ibm
->acpi
->notify(ibm
, event
);
747 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
754 if (!*ibm
->acpi
->handle
)
757 vdbg_printk(TPACPI_DBG_INIT
,
758 "setting up ACPI notify for %s\n", ibm
->name
);
760 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
762 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm
->name
, rc
);
766 ibm
->acpi
->device
->driver_data
= ibm
;
767 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
768 TPACPI_ACPI_EVENT_PREFIX
,
771 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
772 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
773 if (ACPI_FAILURE(status
)) {
774 if (status
== AE_ALREADY_EXISTS
) {
775 pr_notice("another device driver is already "
776 "handling %s events\n", ibm
->name
);
778 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
779 ibm
->name
, acpi_format_exception(status
));
783 ibm
->flags
.acpi_notify_installed
= 1;
787 static int __init
tpacpi_device_add(struct acpi_device
*device
)
792 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
796 dbg_printk(TPACPI_DBG_INIT
,
797 "registering %s as an ACPI driver\n", ibm
->name
);
801 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
802 if (!ibm
->acpi
->driver
) {
803 pr_err("failed to allocate memory for ibm->acpi->driver\n");
807 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
808 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
810 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
812 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
814 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
816 kfree(ibm
->acpi
->driver
);
817 ibm
->acpi
->driver
= NULL
;
819 ibm
->flags
.acpi_driver_registered
= 1;
825 /****************************************************************************
826 ****************************************************************************
830 ****************************************************************************
831 ****************************************************************************/
833 static int dispatch_proc_show(struct seq_file
*m
, void *v
)
835 struct ibm_struct
*ibm
= m
->private;
837 if (!ibm
|| !ibm
->read
)
842 static int dispatch_proc_open(struct inode
*inode
, struct file
*file
)
844 return single_open(file
, dispatch_proc_show
, PDE(inode
)->data
);
847 static ssize_t
dispatch_proc_write(struct file
*file
,
848 const char __user
*userbuf
,
849 size_t count
, loff_t
*pos
)
851 struct ibm_struct
*ibm
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
855 if (!ibm
|| !ibm
->write
)
857 if (count
> PAGE_SIZE
- 2)
860 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
864 if (copy_from_user(kernbuf
, userbuf
, count
)) {
870 strcat(kernbuf
, ",");
871 ret
= ibm
->write(kernbuf
);
880 static const struct file_operations dispatch_proc_fops
= {
881 .owner
= THIS_MODULE
,
882 .open
= dispatch_proc_open
,
885 .release
= single_release
,
886 .write
= dispatch_proc_write
,
889 static char *next_cmd(char **cmds
)
894 while ((end
= strchr(start
, ',')) && end
== start
)
906 /****************************************************************************
907 ****************************************************************************
909 * Device model: input, hwmon and platform
911 ****************************************************************************
912 ****************************************************************************/
914 static struct platform_device
*tpacpi_pdev
;
915 static struct platform_device
*tpacpi_sensors_pdev
;
916 static struct device
*tpacpi_hwmon
;
917 static struct input_dev
*tpacpi_inputdev
;
918 static struct mutex tpacpi_inputdev_send_mutex
;
919 static LIST_HEAD(tpacpi_all_drivers
);
921 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
924 struct ibm_struct
*ibm
, *itmp
;
926 list_for_each_entry_safe(ibm
, itmp
,
930 (ibm
->suspend
)(state
);
936 static int tpacpi_resume_handler(struct platform_device
*pdev
)
938 struct ibm_struct
*ibm
, *itmp
;
940 list_for_each_entry_safe(ibm
, itmp
,
950 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
952 struct ibm_struct
*ibm
, *itmp
;
954 list_for_each_entry_safe(ibm
, itmp
,
962 static struct platform_driver tpacpi_pdriver
= {
964 .name
= TPACPI_DRVR_NAME
,
965 .owner
= THIS_MODULE
,
967 .suspend
= tpacpi_suspend_handler
,
968 .resume
= tpacpi_resume_handler
,
969 .shutdown
= tpacpi_shutdown_handler
,
972 static struct platform_driver tpacpi_hwmon_pdriver
= {
974 .name
= TPACPI_HWMON_DRVR_NAME
,
975 .owner
= THIS_MODULE
,
979 /*************************************************************************
980 * sysfs support helpers
983 struct attribute_set
{
984 unsigned int members
, max_members
;
985 struct attribute_group group
;
988 struct attribute_set_obj
{
989 struct attribute_set s
;
991 } __attribute__((packed
));
993 static struct attribute_set
*create_attr_set(unsigned int max_members
,
996 struct attribute_set_obj
*sobj
;
998 if (max_members
== 0)
1001 /* Allocates space for implicit NULL at the end too */
1002 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
1003 max_members
* sizeof(struct attribute
*),
1007 sobj
->s
.max_members
= max_members
;
1008 sobj
->s
.group
.attrs
= &sobj
->a
;
1009 sobj
->s
.group
.name
= name
;
1014 #define destroy_attr_set(_set) \
1017 /* not multi-threaded safe, use it in a single thread per set */
1018 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
1023 if (s
->members
>= s
->max_members
)
1026 s
->group
.attrs
[s
->members
] = attr
;
1032 static int add_many_to_attr_set(struct attribute_set
*s
,
1033 struct attribute
**attr
,
1038 for (i
= 0; i
< count
; i
++) {
1039 res
= add_to_attr_set(s
, attr
[i
]);
1047 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
1049 sysfs_remove_group(kobj
, &s
->group
);
1050 destroy_attr_set(s
);
1053 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1054 sysfs_create_group(_kobj, &_attr_set->group)
1056 static int parse_strtoul(const char *buf
,
1057 unsigned long max
, unsigned long *value
)
1061 *value
= simple_strtoul(skip_spaces(buf
), &endp
, 0);
1062 endp
= skip_spaces(endp
);
1063 if (*endp
|| *value
> max
)
1069 static void tpacpi_disable_brightness_delay(void)
1071 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
1072 pr_notice("ACPI backlight control delay disabled\n");
1075 static void printk_deprecated_attribute(const char * const what
,
1076 const char * const details
)
1078 tpacpi_log_usertask("deprecated sysfs attribute");
1079 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1080 "will be removed. %s\n",
1084 /*************************************************************************
1085 * rfkill and radio control support helpers
1089 * ThinkPad-ACPI firmware handling model:
1091 * WLSW (master wireless switch) is event-driven, and is common to all
1092 * firmware-controlled radios. It cannot be controlled, just monitored,
1093 * as expected. It overrides all radio state in firmware
1095 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1096 * (TODO: verify how WLSW interacts with the returned radio state).
1098 * The only time there are shadow radio state changes, is when
1099 * masked-off hotkeys are used.
1103 * Internal driver API for radio state:
1105 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1106 * bool: true means radio blocked (off)
1108 enum tpacpi_rfkill_state
{
1109 TPACPI_RFK_RADIO_OFF
= 0,
1113 /* rfkill switches */
1114 enum tpacpi_rfk_id
{
1115 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
1116 TPACPI_RFK_WWAN_SW_ID
,
1117 TPACPI_RFK_UWB_SW_ID
,
1121 static const char *tpacpi_rfkill_names
[] = {
1122 [TPACPI_RFK_BLUETOOTH_SW_ID
] = "bluetooth",
1123 [TPACPI_RFK_WWAN_SW_ID
] = "wwan",
1124 [TPACPI_RFK_UWB_SW_ID
] = "uwb",
1125 [TPACPI_RFK_SW_MAX
] = NULL
1128 /* ThinkPad-ACPI rfkill subdriver */
1130 struct rfkill
*rfkill
;
1131 enum tpacpi_rfk_id id
;
1132 const struct tpacpi_rfk_ops
*ops
;
1135 struct tpacpi_rfk_ops
{
1136 /* firmware interface */
1137 int (*get_status
)(void);
1138 int (*set_status
)(const enum tpacpi_rfkill_state
);
1141 static struct tpacpi_rfk
*tpacpi_rfkill_switches
[TPACPI_RFK_SW_MAX
];
1143 /* Query FW and update rfkill sw state for a given rfkill switch */
1144 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk
*tp_rfk
)
1151 status
= (tp_rfk
->ops
->get_status
)();
1155 rfkill_set_sw_state(tp_rfk
->rfkill
,
1156 (status
== TPACPI_RFK_RADIO_OFF
));
1161 /* Query FW and update rfkill sw state for all rfkill switches */
1162 static void tpacpi_rfk_update_swstate_all(void)
1166 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++)
1167 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[i
]);
1171 * Sync the HW-blocking state of all rfkill switches,
1172 * do notice it causes the rfkill core to schedule uevents
1174 static void tpacpi_rfk_update_hwblock_state(bool blocked
)
1177 struct tpacpi_rfk
*tp_rfk
;
1179 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++) {
1180 tp_rfk
= tpacpi_rfkill_switches
[i
];
1182 if (rfkill_set_hw_state(tp_rfk
->rfkill
,
1184 /* ignore -- we track sw block */
1190 /* Call to get the WLSW state from the firmware */
1191 static int hotkey_get_wlsw(void);
1193 /* Call to query WLSW state and update all rfkill switches */
1194 static bool tpacpi_rfk_check_hwblock_state(void)
1196 int res
= hotkey_get_wlsw();
1199 /* When unknown or unsupported, we have to assume it is unblocked */
1203 hw_blocked
= (res
== TPACPI_RFK_RADIO_OFF
);
1204 tpacpi_rfk_update_hwblock_state(hw_blocked
);
1209 static int tpacpi_rfk_hook_set_block(void *data
, bool blocked
)
1211 struct tpacpi_rfk
*tp_rfk
= data
;
1214 dbg_printk(TPACPI_DBG_RFKILL
,
1215 "request to change radio state to %s\n",
1216 blocked
? "blocked" : "unblocked");
1218 /* try to set radio state */
1219 res
= (tp_rfk
->ops
->set_status
)(blocked
?
1220 TPACPI_RFK_RADIO_OFF
: TPACPI_RFK_RADIO_ON
);
1222 /* and update the rfkill core with whatever the FW really did */
1223 tpacpi_rfk_update_swstate(tp_rfk
);
1225 return (res
< 0) ? res
: 0;
1228 static const struct rfkill_ops tpacpi_rfk_rfkill_ops
= {
1229 .set_block
= tpacpi_rfk_hook_set_block
,
1232 static int __init
tpacpi_new_rfkill(const enum tpacpi_rfk_id id
,
1233 const struct tpacpi_rfk_ops
*tp_rfkops
,
1234 const enum rfkill_type rfktype
,
1236 const bool set_default
)
1238 struct tpacpi_rfk
*atp_rfk
;
1240 bool sw_state
= false;
1244 BUG_ON(id
>= TPACPI_RFK_SW_MAX
|| tpacpi_rfkill_switches
[id
]);
1246 atp_rfk
= kzalloc(sizeof(struct tpacpi_rfk
), GFP_KERNEL
);
1248 atp_rfk
->rfkill
= rfkill_alloc(name
,
1251 &tpacpi_rfk_rfkill_ops
,
1253 if (!atp_rfk
|| !atp_rfk
->rfkill
) {
1254 pr_err("failed to allocate memory for rfkill class\n");
1260 atp_rfk
->ops
= tp_rfkops
;
1262 sw_status
= (tp_rfkops
->get_status
)();
1263 if (sw_status
< 0) {
1264 pr_err("failed to read initial state for %s, error %d\n",
1267 sw_state
= (sw_status
== TPACPI_RFK_RADIO_OFF
);
1269 /* try to keep the initial state, since we ask the
1270 * firmware to preserve it across S5 in NVRAM */
1271 rfkill_init_sw_state(atp_rfk
->rfkill
, sw_state
);
1274 hw_state
= tpacpi_rfk_check_hwblock_state();
1275 rfkill_set_hw_state(atp_rfk
->rfkill
, hw_state
);
1277 res
= rfkill_register(atp_rfk
->rfkill
);
1279 pr_err("failed to register %s rfkill switch: %d\n", name
, res
);
1280 rfkill_destroy(atp_rfk
->rfkill
);
1285 tpacpi_rfkill_switches
[id
] = atp_rfk
;
1287 pr_info("rfkill switch %s: radio is %sblocked\n",
1288 name
, (sw_state
|| hw_state
) ? "" : "un");
1292 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id
)
1294 struct tpacpi_rfk
*tp_rfk
;
1296 BUG_ON(id
>= TPACPI_RFK_SW_MAX
);
1298 tp_rfk
= tpacpi_rfkill_switches
[id
];
1300 rfkill_unregister(tp_rfk
->rfkill
);
1301 rfkill_destroy(tp_rfk
->rfkill
);
1302 tpacpi_rfkill_switches
[id
] = NULL
;
1307 static void printk_deprecated_rfkill_attribute(const char * const what
)
1309 printk_deprecated_attribute(what
,
1310 "Please switch to generic rfkill before year 2010");
1313 /* sysfs <radio> enable ------------------------------------------------ */
1314 static ssize_t
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id
,
1315 struct device_attribute
*attr
,
1320 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1322 /* This is in the ABI... */
1323 if (tpacpi_rfk_check_hwblock_state()) {
1324 status
= TPACPI_RFK_RADIO_OFF
;
1326 status
= tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1331 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1332 (status
== TPACPI_RFK_RADIO_ON
) ? 1 : 0);
1335 static ssize_t
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id
,
1336 struct device_attribute
*attr
,
1337 const char *buf
, size_t count
)
1342 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1344 if (parse_strtoul(buf
, 1, &t
))
1347 tpacpi_disclose_usertask(attr
->attr
.name
, "set to %ld\n", t
);
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state() && !!t
)
1353 res
= tpacpi_rfkill_switches
[id
]->ops
->set_status((!!t
) ?
1354 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
);
1355 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1357 return (res
< 0) ? res
: count
;
1360 /* procfs -------------------------------------------------------------- */
1361 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id
, struct seq_file
*m
)
1363 if (id
>= TPACPI_RFK_SW_MAX
)
1364 seq_printf(m
, "status:\t\tnot supported\n");
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state()) {
1370 status
= TPACPI_RFK_RADIO_OFF
;
1372 status
= tpacpi_rfk_update_swstate(
1373 tpacpi_rfkill_switches
[id
]);
1378 seq_printf(m
, "status:\t\t%s\n",
1379 (status
== TPACPI_RFK_RADIO_ON
) ?
1380 "enabled" : "disabled");
1381 seq_printf(m
, "commands:\tenable, disable\n");
1387 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id
, char *buf
)
1393 if (id
>= TPACPI_RFK_SW_MAX
)
1396 while ((cmd
= next_cmd(&buf
))) {
1397 if (strlencmp(cmd
, "enable") == 0)
1398 status
= TPACPI_RFK_RADIO_ON
;
1399 else if (strlencmp(cmd
, "disable") == 0)
1400 status
= TPACPI_RFK_RADIO_OFF
;
1406 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1407 (status
== TPACPI_RFK_RADIO_ON
) ?
1408 "enable" : "disable",
1409 tpacpi_rfkill_names
[id
]);
1410 res
= (tpacpi_rfkill_switches
[id
]->ops
->set_status
)(status
);
1411 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1417 /*************************************************************************
1418 * thinkpad-acpi driver attributes
1421 /* interface_version --------------------------------------------------- */
1422 static ssize_t
tpacpi_driver_interface_version_show(
1423 struct device_driver
*drv
,
1426 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1429 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1430 tpacpi_driver_interface_version_show
, NULL
);
1432 /* debug_level --------------------------------------------------------- */
1433 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1436 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1439 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1440 const char *buf
, size_t count
)
1444 if (parse_strtoul(buf
, 0xffff, &t
))
1452 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1453 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1455 /* version ------------------------------------------------------------- */
1456 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1459 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1460 TPACPI_DESC
, TPACPI_VERSION
);
1463 static DRIVER_ATTR(version
, S_IRUGO
,
1464 tpacpi_driver_version_show
, NULL
);
1466 /* --------------------------------------------------------------------- */
1468 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1470 /* wlsw_emulstate ------------------------------------------------------ */
1471 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1474 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1477 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1478 const char *buf
, size_t count
)
1482 if (parse_strtoul(buf
, 1, &t
))
1485 if (tpacpi_wlsw_emulstate
!= !!t
) {
1486 tpacpi_wlsw_emulstate
= !!t
;
1487 tpacpi_rfk_update_hwblock_state(!t
); /* negative logic */
1493 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1494 tpacpi_driver_wlsw_emulstate_show
,
1495 tpacpi_driver_wlsw_emulstate_store
);
1497 /* bluetooth_emulstate ------------------------------------------------- */
1498 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1499 struct device_driver
*drv
,
1502 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1505 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1506 struct device_driver
*drv
,
1507 const char *buf
, size_t count
)
1511 if (parse_strtoul(buf
, 1, &t
))
1514 tpacpi_bluetooth_emulstate
= !!t
;
1519 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1520 tpacpi_driver_bluetooth_emulstate_show
,
1521 tpacpi_driver_bluetooth_emulstate_store
);
1523 /* wwan_emulstate ------------------------------------------------- */
1524 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1525 struct device_driver
*drv
,
1528 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1531 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1532 struct device_driver
*drv
,
1533 const char *buf
, size_t count
)
1537 if (parse_strtoul(buf
, 1, &t
))
1540 tpacpi_wwan_emulstate
= !!t
;
1545 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1546 tpacpi_driver_wwan_emulstate_show
,
1547 tpacpi_driver_wwan_emulstate_store
);
1549 /* uwb_emulstate ------------------------------------------------- */
1550 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1551 struct device_driver
*drv
,
1554 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1557 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1558 struct device_driver
*drv
,
1559 const char *buf
, size_t count
)
1563 if (parse_strtoul(buf
, 1, &t
))
1566 tpacpi_uwb_emulstate
= !!t
;
1571 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1572 tpacpi_driver_uwb_emulstate_show
,
1573 tpacpi_driver_uwb_emulstate_store
);
1576 /* --------------------------------------------------------------------- */
1578 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1579 &driver_attr_debug_level
, &driver_attr_version
,
1580 &driver_attr_interface_version
,
1583 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1589 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1590 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1594 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1595 if (!res
&& dbg_wlswemul
)
1596 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1597 if (!res
&& dbg_bluetoothemul
)
1598 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1599 if (!res
&& dbg_wwanemul
)
1600 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1601 if (!res
&& dbg_uwbemul
)
1602 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1608 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1612 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1613 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1615 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1616 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1617 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1618 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1619 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1623 /*************************************************************************
1628 * Table of recommended minimum BIOS versions
1630 * Reasons for listing:
1631 * 1. Stable BIOS, listed because the unknown amount of
1632 * bugs and bad ACPI behaviour on older versions
1634 * 2. BIOS or EC fw with known bugs that trigger on Linux
1636 * 3. BIOS with known reduced functionality in older versions
1638 * We recommend the latest BIOS and EC version.
1639 * We only support the latest BIOS and EC fw version as a rule.
1641 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1642 * Information from users in ThinkWiki
1644 * WARNING: we use this table also to detect that the machine is
1645 * a ThinkPad in some cases, so don't remove entries lightly.
1648 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1649 { .vendor = (__v), \
1650 .bios = TPID(__id1, __id2), \
1651 .ec = TPACPI_MATCH_ANY, \
1652 .quirks = TPACPI_MATCH_ANY << 16 \
1653 | (__bv1) << 8 | (__bv2) }
1655 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1656 __eid, __ev1, __ev2) \
1657 { .vendor = (__v), \
1658 .bios = TPID(__bid1, __bid2), \
1660 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1661 | (__bv1) << 8 | (__bv2) }
1663 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1664 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1666 /* Outdated IBM BIOSes often lack the EC id string */
1667 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1669 __bv1, __bv2, TPID(__id1, __id2), \
1671 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1672 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1675 /* Outdated IBM BIOSes often lack the EC id string */
1676 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1677 __eid1, __eid2, __ev1, __ev2) \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1679 __bv1, __bv2, TPID(__eid1, __eid2), \
1681 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1682 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1685 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1686 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1688 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1689 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1690 __bv1, __bv2, TPID(__id1, __id2), \
1693 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1694 __eid1, __eid2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1696 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 static const struct tpacpi_quirk tpacpi_bios_version_qtable
[] __initconst
= {
1700 /* Numeric models ------------------ */
1701 /* FW MODEL BIOS VERS */
1702 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1703 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1704 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1705 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1706 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1707 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1708 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1709 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1710 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1712 /* A-series ------------------------- */
1713 /* FW MODEL BIOS VERS EC VERS */
1714 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1715 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1716 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1717 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1718 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1719 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1720 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1721 TPV_QI0('1', '3', '2', '0'), /* A22m */
1722 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1723 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1724 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1726 /* G-series ------------------------- */
1727 /* FW MODEL BIOS VERS */
1728 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1729 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1731 /* R-series, T-series --------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1734 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1735 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1736 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1737 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1738 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1739 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1740 T40/p, T41/p, T42/p (1) */
1741 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1742 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1743 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1744 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1746 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1747 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1748 TPV_QI0('1', '6', '3', '2'), /* T22 */
1749 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1750 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1751 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1753 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1754 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1755 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1757 /* BIOS FW BIOS VERS EC FW EC VERS */
1758 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1759 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1761 /* X-series ------------------------- */
1762 /* FW MODEL BIOS VERS EC VERS */
1763 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1764 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1765 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1766 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1767 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1768 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1769 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1771 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1772 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1774 /* (0) - older versions lack DMI EC fw string and functionality */
1775 /* (1) - older versions known to lack functionality */
1786 static void __init
tpacpi_check_outdated_fw(void)
1788 unsigned long fwvers
;
1789 u16 ec_version
, bios_version
;
1791 fwvers
= tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1792 ARRAY_SIZE(tpacpi_bios_version_qtable
));
1797 bios_version
= fwvers
& 0xffffU
;
1798 ec_version
= (fwvers
>> 16) & 0xffffU
;
1800 /* note that unknown versions are set to 0x0000 and we use that */
1801 if ((bios_version
> thinkpad_id
.bios_release
) ||
1802 (ec_version
> thinkpad_id
.ec_release
&&
1803 ec_version
!= TPACPI_MATCH_ANY
)) {
1805 * The changelogs would let us track down the exact
1806 * reason, but it is just too much of a pain to track
1807 * it. We only list BIOSes that are either really
1808 * broken, or really stable to begin with, so it is
1809 * best if the user upgrades the firmware anyway.
1811 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1812 pr_warn("WARNING: This firmware may be missing critical bug "
1813 "fixes and/or important features\n");
1817 static bool __init
tpacpi_is_fw_known(void)
1819 return tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1820 ARRAY_SIZE(tpacpi_bios_version_qtable
)) != 0;
1823 /****************************************************************************
1824 ****************************************************************************
1828 ****************************************************************************
1829 ****************************************************************************/
1831 /*************************************************************************
1832 * thinkpad-acpi metadata subdriver
1835 static int thinkpad_acpi_driver_read(struct seq_file
*m
)
1837 seq_printf(m
, "driver:\t\t%s\n", TPACPI_DESC
);
1838 seq_printf(m
, "version:\t%s\n", TPACPI_VERSION
);
1842 static struct ibm_struct thinkpad_acpi_driver_data
= {
1844 .read
= thinkpad_acpi_driver_read
,
1847 /*************************************************************************
1852 * ThinkPad firmware event model
1854 * The ThinkPad firmware has two main event interfaces: normal ACPI
1855 * notifications (which follow the ACPI standard), and a private event
1858 * The private event interface also issues events for the hotkeys. As
1859 * the driver gained features, the event handling code ended up being
1860 * built around the hotkey subdriver. This will need to be refactored
1861 * to a more formal event API eventually.
1863 * Some "hotkeys" are actually supposed to be used as event reports,
1864 * such as "brightness has changed", "volume has changed", depending on
1865 * the ThinkPad model and how the firmware is operating.
1867 * Unlike other classes, hotkey-class events have mask/unmask control on
1868 * non-ancient firmware. However, how it behaves changes a lot with the
1869 * firmware model and version.
1872 enum { /* hot key scan codes (derived from ACPI DSDT) */
1873 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1874 TP_ACPI_HOTKEYSCAN_FNF2
,
1875 TP_ACPI_HOTKEYSCAN_FNF3
,
1876 TP_ACPI_HOTKEYSCAN_FNF4
,
1877 TP_ACPI_HOTKEYSCAN_FNF5
,
1878 TP_ACPI_HOTKEYSCAN_FNF6
,
1879 TP_ACPI_HOTKEYSCAN_FNF7
,
1880 TP_ACPI_HOTKEYSCAN_FNF8
,
1881 TP_ACPI_HOTKEYSCAN_FNF9
,
1882 TP_ACPI_HOTKEYSCAN_FNF10
,
1883 TP_ACPI_HOTKEYSCAN_FNF11
,
1884 TP_ACPI_HOTKEYSCAN_FNF12
,
1885 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1886 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1887 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1888 TP_ACPI_HOTKEYSCAN_FNHOME
,
1889 TP_ACPI_HOTKEYSCAN_FNEND
,
1890 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1891 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1892 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1893 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1894 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1895 TP_ACPI_HOTKEYSCAN_MUTE
,
1896 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1897 TP_ACPI_HOTKEYSCAN_UNK1
,
1898 TP_ACPI_HOTKEYSCAN_UNK2
,
1899 TP_ACPI_HOTKEYSCAN_UNK3
,
1900 TP_ACPI_HOTKEYSCAN_UNK4
,
1901 TP_ACPI_HOTKEYSCAN_UNK5
,
1902 TP_ACPI_HOTKEYSCAN_UNK6
,
1903 TP_ACPI_HOTKEYSCAN_UNK7
,
1904 TP_ACPI_HOTKEYSCAN_UNK8
,
1906 /* Hotkey keymap size */
1907 TPACPI_HOTKEY_MAP_LEN
1910 enum { /* Keys/events available through NVRAM polling */
1911 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1912 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1915 enum { /* Positions of some of the keys in hotkey masks */
1916 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1917 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1918 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1919 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1920 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1921 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1922 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1923 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1924 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1925 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1926 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1929 enum { /* NVRAM to ACPI HKEY group map */
1930 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1931 TP_ACPI_HKEY_ZOOM_MASK
|
1932 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1933 TP_ACPI_HKEY_HIBERNATE_MASK
,
1934 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1935 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1936 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1937 TP_ACPI_HKEY_VOLDWN_MASK
|
1938 TP_ACPI_HKEY_MUTE_MASK
,
1941 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1942 struct tp_nvram_state
{
1943 u16 thinkpad_toggle
:1;
1945 u16 display_toggle
:1;
1946 u16 thinklight_toggle
:1;
1947 u16 hibernate_toggle
:1;
1948 u16 displayexp_toggle
:1;
1949 u16 display_state
:1;
1950 u16 brightness_toggle
:1;
1951 u16 volume_toggle
:1;
1954 u8 brightness_level
;
1958 /* kthread for the hotkey poller */
1959 static struct task_struct
*tpacpi_hotkey_task
;
1961 /* Acquired while the poller kthread is running, use to sync start/stop */
1962 static struct mutex hotkey_thread_mutex
;
1965 * Acquire mutex to write poller control variables as an
1968 * Increment hotkey_config_change when changing them if you
1969 * want the kthread to forget old state.
1971 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1973 static struct mutex hotkey_thread_data_mutex
;
1974 static unsigned int hotkey_config_change
;
1977 * hotkey poller control variables
1979 * Must be atomic or readers will also need to acquire mutex
1981 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1982 * should be used only when the changes need to be taken as
1983 * a block, OR when one needs to force the kthread to forget
1986 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1987 static unsigned int hotkey_poll_freq
= 10; /* Hz */
1989 #define HOTKEY_CONFIG_CRITICAL_START \
1991 mutex_lock(&hotkey_thread_data_mutex); \
1992 hotkey_config_change++; \
1994 #define HOTKEY_CONFIG_CRITICAL_END \
1995 mutex_unlock(&hotkey_thread_data_mutex);
1997 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1999 #define hotkey_source_mask 0U
2000 #define HOTKEY_CONFIG_CRITICAL_START
2001 #define HOTKEY_CONFIG_CRITICAL_END
2003 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2005 static struct mutex hotkey_mutex
;
2007 static enum { /* Reasons for waking up */
2008 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
2009 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
2010 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
2011 } hotkey_wakeup_reason
;
2013 static int hotkey_autosleep_ack
;
2015 static u32 hotkey_orig_mask
; /* events the BIOS had enabled */
2016 static u32 hotkey_all_mask
; /* all events supported in fw */
2017 static u32 hotkey_reserved_mask
; /* events better left disabled */
2018 static u32 hotkey_driver_mask
; /* events needed by the driver */
2019 static u32 hotkey_user_mask
; /* events visible to userspace */
2020 static u32 hotkey_acpi_mask
; /* events enabled in firmware */
2022 static unsigned int hotkey_report_mode
;
2024 static u16
*hotkey_keycode_map
;
2026 static struct attribute_set
*hotkey_dev_attributes
;
2028 static void tpacpi_driver_event(const unsigned int hkey_event
);
2029 static void hotkey_driver_event(const unsigned int scancode
);
2030 static void hotkey_poll_setup(const bool may_warn
);
2032 /* HKEY.MHKG() return bits */
2033 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2035 static int hotkey_get_wlsw(void)
2039 if (!tp_features
.hotkey_wlsw
)
2042 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2044 return (tpacpi_wlsw_emulstate
) ?
2045 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2048 if (!acpi_evalf(hkey_handle
, &status
, "WLSW", "d"))
2051 return (status
) ? TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2054 static int hotkey_get_tablet_mode(int *status
)
2058 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
2061 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
2066 * Reads current event mask from firmware, and updates
2067 * hotkey_acpi_mask accordingly. Also resets any bits
2068 * from hotkey_user_mask that are unavailable to be
2069 * delivered (shadow requirement of the userspace ABI).
2071 * Call with hotkey_mutex held
2073 static int hotkey_mask_get(void)
2075 if (tp_features
.hotkey_mask
) {
2078 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
2081 hotkey_acpi_mask
= m
;
2083 /* no mask support doesn't mean no event support... */
2084 hotkey_acpi_mask
= hotkey_all_mask
;
2087 /* sync userspace-visible mask */
2088 hotkey_user_mask
&= (hotkey_acpi_mask
| hotkey_source_mask
);
2093 void static hotkey_mask_warn_incomplete_mask(void)
2095 /* log only what the user can fix... */
2096 const u32 wantedmask
= hotkey_driver_mask
&
2097 ~(hotkey_acpi_mask
| hotkey_source_mask
) &
2098 (hotkey_all_mask
| TPACPI_HKEY_NVRAM_KNOWN_MASK
);
2101 pr_notice("required events 0x%08x not enabled!\n", wantedmask
);
2105 * Set the firmware mask when supported
2107 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2109 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2111 * Call with hotkey_mutex held
2113 static int hotkey_mask_set(u32 mask
)
2118 const u32 fwmask
= mask
& ~hotkey_source_mask
;
2120 if (tp_features
.hotkey_mask
) {
2121 for (i
= 0; i
< 32; i
++) {
2122 if (!acpi_evalf(hkey_handle
,
2123 NULL
, "MHKM", "vdd", i
+ 1,
2124 !!(mask
& (1 << i
)))) {
2132 * We *must* make an inconditional call to hotkey_mask_get to
2133 * refresh hotkey_acpi_mask and update hotkey_user_mask
2135 * Take the opportunity to also log when we cannot _enable_
2138 if (!hotkey_mask_get() && !rc
&& (fwmask
& ~hotkey_acpi_mask
)) {
2139 pr_notice("asked for hotkey mask 0x%08x, but "
2140 "firmware forced it to 0x%08x\n",
2141 fwmask
, hotkey_acpi_mask
);
2144 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
2145 hotkey_mask_warn_incomplete_mask();
2151 * Sets hotkey_user_mask and tries to set the firmware mask
2153 * Call with hotkey_mutex held
2155 static int hotkey_user_mask_set(const u32 mask
)
2159 /* Give people a chance to notice they are doing something that
2160 * is bound to go boom on their users sooner or later */
2161 if (!tp_warned
.hotkey_mask_ff
&&
2162 (mask
== 0xffff || mask
== 0xffffff ||
2163 mask
== 0xffffffff)) {
2164 tp_warned
.hotkey_mask_ff
= 1;
2165 pr_notice("setting the hotkey mask to 0x%08x is likely "
2166 "not the best way to go about it\n", mask
);
2167 pr_notice("please consider using the driver defaults, "
2168 "and refer to up-to-date thinkpad-acpi "
2172 /* Try to enable what the user asked for, plus whatever we need.
2173 * this syncs everything but won't enable bits in hotkey_user_mask */
2174 rc
= hotkey_mask_set((mask
| hotkey_driver_mask
) & ~hotkey_source_mask
);
2176 /* Enable the available bits in hotkey_user_mask */
2177 hotkey_user_mask
= mask
& (hotkey_acpi_mask
| hotkey_source_mask
);
2183 * Sets the driver hotkey mask.
2185 * Can be called even if the hotkey subdriver is inactive
2187 static int tpacpi_hotkey_driver_mask_set(const u32 mask
)
2191 /* Do the right thing if hotkey_init has not been called yet */
2192 if (!tp_features
.hotkey
) {
2193 hotkey_driver_mask
= mask
;
2197 mutex_lock(&hotkey_mutex
);
2199 HOTKEY_CONFIG_CRITICAL_START
2200 hotkey_driver_mask
= mask
;
2201 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2202 hotkey_source_mask
|= (mask
& ~hotkey_all_mask
);
2204 HOTKEY_CONFIG_CRITICAL_END
2206 rc
= hotkey_mask_set((hotkey_acpi_mask
| hotkey_driver_mask
) &
2207 ~hotkey_source_mask
);
2208 hotkey_poll_setup(true);
2210 mutex_unlock(&hotkey_mutex
);
2215 static int hotkey_status_get(int *status
)
2217 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2223 static int hotkey_status_set(bool enable
)
2225 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2231 static void tpacpi_input_send_tabletsw(void)
2235 if (tp_features
.hotkey_tablet
&&
2236 !hotkey_get_tablet_mode(&state
)) {
2237 mutex_lock(&tpacpi_inputdev_send_mutex
);
2239 input_report_switch(tpacpi_inputdev
,
2240 SW_TABLET_MODE
, !!state
);
2241 input_sync(tpacpi_inputdev
);
2243 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2247 /* Do NOT call without validating scancode first */
2248 static void tpacpi_input_send_key(const unsigned int scancode
)
2250 const unsigned int keycode
= hotkey_keycode_map
[scancode
];
2252 if (keycode
!= KEY_RESERVED
) {
2253 mutex_lock(&tpacpi_inputdev_send_mutex
);
2255 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
, scancode
);
2256 input_report_key(tpacpi_inputdev
, keycode
, 1);
2257 input_sync(tpacpi_inputdev
);
2259 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
, scancode
);
2260 input_report_key(tpacpi_inputdev
, keycode
, 0);
2261 input_sync(tpacpi_inputdev
);
2263 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2267 /* Do NOT call without validating scancode first */
2268 static void tpacpi_input_send_key_masked(const unsigned int scancode
)
2270 hotkey_driver_event(scancode
);
2271 if (hotkey_user_mask
& (1 << scancode
))
2272 tpacpi_input_send_key(scancode
);
2275 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2276 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
2278 /* Do NOT call without validating scancode first */
2279 static void tpacpi_hotkey_send_key(unsigned int scancode
)
2281 tpacpi_input_send_key_masked(scancode
);
2282 if (hotkey_report_mode
< 2) {
2283 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
2284 0x80, TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
2288 static void hotkey_read_nvram(struct tp_nvram_state
*n
, const u32 m
)
2292 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
2293 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
2294 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
2295 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
2296 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
2297 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
2299 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
2300 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
2301 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
2303 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
2304 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
2305 n
->displayexp_toggle
=
2306 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
2308 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
2309 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
2310 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
2311 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
2312 n
->brightness_toggle
=
2313 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
2315 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
2316 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
2317 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
2318 >> TP_NVRAM_POS_LEVEL_VOLUME
;
2319 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
2320 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
2324 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
2325 struct tp_nvram_state
*newn
,
2326 const u32 event_mask
)
2329 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2331 if ((event_mask & (1 << __scancode)) && \
2332 oldn->__member != newn->__member) \
2333 tpacpi_hotkey_send_key(__scancode); \
2336 #define TPACPI_MAY_SEND_KEY(__scancode) \
2338 if (event_mask & (1 << __scancode)) \
2339 tpacpi_hotkey_send_key(__scancode); \
2342 void issue_volchange(const unsigned int oldvol
,
2343 const unsigned int newvol
)
2345 unsigned int i
= oldvol
;
2347 while (i
> newvol
) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2351 while (i
< newvol
) {
2352 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2357 void issue_brightnesschange(const unsigned int oldbrt
,
2358 const unsigned int newbrt
)
2360 unsigned int i
= oldbrt
;
2362 while (i
> newbrt
) {
2363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2366 while (i
< newbrt
) {
2367 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
2373 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
2384 * This code is supposed to duplicate the IBM firmware behaviour:
2385 * - Pressing MUTE issues mute hotkey message, even when already mute
2386 * - Pressing Volume up/down issues volume up/down hotkey messages,
2387 * even when already at maximum or minimum volume
2388 * - The act of unmuting issues volume up/down notification,
2389 * depending which key was used to unmute
2391 * We are constrained to what the NVRAM can tell us, which is not much
2392 * and certainly not enough if more than one volume hotkey was pressed
2393 * since the last poll cycle.
2395 * Just to make our life interesting, some newer Lenovo ThinkPads have
2396 * bugs in the BIOS and may fail to update volume_toggle properly.
2401 oldn
->volume_toggle
!= newn
->volume_toggle
||
2402 oldn
->volume_level
!= newn
->volume_level
) {
2403 /* recently muted, or repeated mute keypress, or
2404 * multiple presses ending in mute */
2405 issue_volchange(oldn
->volume_level
, newn
->volume_level
);
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2411 /* recently unmuted, issue 'unmute' keypress */
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2414 if (oldn
->volume_level
!= newn
->volume_level
) {
2415 issue_volchange(oldn
->volume_level
, newn
->volume_level
);
2416 } else if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
2417 /* repeated vol up/down keypress at end of scale ? */
2418 if (newn
->volume_level
== 0)
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2420 else if (newn
->volume_level
>= TP_NVRAM_LEVEL_VOLUME_MAX
)
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2425 /* handle brightness */
2426 if (oldn
->brightness_level
!= newn
->brightness_level
) {
2427 issue_brightnesschange(oldn
->brightness_level
,
2428 newn
->brightness_level
);
2429 } else if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
2430 /* repeated key presses that didn't change state */
2431 if (newn
->brightness_level
== 0)
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2433 else if (newn
->brightness_level
>= bright_maxlvl
2434 && !tp_features
.bright_unkfw
)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2438 #undef TPACPI_COMPARE_KEY
2439 #undef TPACPI_MAY_SEND_KEY
2445 * We track all events in hotkey_source_mask all the time, since
2446 * most of them are edge-based. We only issue those requested by
2447 * hotkey_user_mask or hotkey_driver_mask, though.
2449 static int hotkey_kthread(void *data
)
2451 struct tp_nvram_state s
[2];
2452 u32 poll_mask
, event_mask
;
2453 unsigned int si
, so
;
2455 unsigned int change_detector
, must_reset
;
2456 unsigned int poll_freq
;
2458 mutex_lock(&hotkey_thread_mutex
);
2460 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
2469 /* Initial state for compares */
2470 mutex_lock(&hotkey_thread_data_mutex
);
2471 change_detector
= hotkey_config_change
;
2472 poll_mask
= hotkey_source_mask
;
2473 event_mask
= hotkey_source_mask
&
2474 (hotkey_driver_mask
| hotkey_user_mask
);
2475 poll_freq
= hotkey_poll_freq
;
2476 mutex_unlock(&hotkey_thread_data_mutex
);
2477 hotkey_read_nvram(&s
[so
], poll_mask
);
2479 while (!kthread_should_stop()) {
2481 if (likely(poll_freq
))
2484 t
= 100; /* should never happen... */
2486 t
= msleep_interruptible(t
);
2487 if (unlikely(kthread_should_stop()))
2489 must_reset
= try_to_freeze();
2490 if (t
> 0 && !must_reset
)
2493 mutex_lock(&hotkey_thread_data_mutex
);
2494 if (must_reset
|| hotkey_config_change
!= change_detector
) {
2495 /* forget old state on thaw or config change */
2498 change_detector
= hotkey_config_change
;
2500 poll_mask
= hotkey_source_mask
;
2501 event_mask
= hotkey_source_mask
&
2502 (hotkey_driver_mask
| hotkey_user_mask
);
2503 poll_freq
= hotkey_poll_freq
;
2504 mutex_unlock(&hotkey_thread_data_mutex
);
2506 if (likely(poll_mask
)) {
2507 hotkey_read_nvram(&s
[si
], poll_mask
);
2508 if (likely(si
!= so
)) {
2509 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
2519 mutex_unlock(&hotkey_thread_mutex
);
2523 /* call with hotkey_mutex held */
2524 static void hotkey_poll_stop_sync(void)
2526 if (tpacpi_hotkey_task
) {
2527 if (frozen(tpacpi_hotkey_task
) ||
2528 freezing(tpacpi_hotkey_task
))
2529 thaw_process(tpacpi_hotkey_task
);
2531 kthread_stop(tpacpi_hotkey_task
);
2532 tpacpi_hotkey_task
= NULL
;
2533 mutex_lock(&hotkey_thread_mutex
);
2534 /* at this point, the thread did exit */
2535 mutex_unlock(&hotkey_thread_mutex
);
2539 /* call with hotkey_mutex held */
2540 static void hotkey_poll_setup(const bool may_warn
)
2542 const u32 poll_driver_mask
= hotkey_driver_mask
& hotkey_source_mask
;
2543 const u32 poll_user_mask
= hotkey_user_mask
& hotkey_source_mask
;
2545 if (hotkey_poll_freq
> 0 &&
2546 (poll_driver_mask
||
2547 (poll_user_mask
&& tpacpi_inputdev
->users
> 0))) {
2548 if (!tpacpi_hotkey_task
) {
2549 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
2550 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
2551 if (IS_ERR(tpacpi_hotkey_task
)) {
2552 tpacpi_hotkey_task
= NULL
;
2553 pr_err("could not create kernel thread "
2554 "for hotkey polling\n");
2558 hotkey_poll_stop_sync();
2559 if (may_warn
&& (poll_driver_mask
|| poll_user_mask
) &&
2560 hotkey_poll_freq
== 0) {
2561 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2562 "require polling, which is currently "
2564 poll_user_mask
, poll_driver_mask
);
2569 static void hotkey_poll_setup_safe(const bool may_warn
)
2571 mutex_lock(&hotkey_mutex
);
2572 hotkey_poll_setup(may_warn
);
2573 mutex_unlock(&hotkey_mutex
);
2576 /* call with hotkey_mutex held */
2577 static void hotkey_poll_set_freq(unsigned int freq
)
2580 hotkey_poll_stop_sync();
2582 hotkey_poll_freq
= freq
;
2585 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2587 static void hotkey_poll_setup(const bool __unused
)
2591 static void hotkey_poll_setup_safe(const bool __unused
)
2595 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2597 static int hotkey_inputdev_open(struct input_dev
*dev
)
2599 switch (tpacpi_lifecycle
) {
2600 case TPACPI_LIFE_INIT
:
2601 case TPACPI_LIFE_RUNNING
:
2602 hotkey_poll_setup_safe(false);
2604 case TPACPI_LIFE_EXITING
:
2608 /* Should only happen if tpacpi_lifecycle is corrupt */
2613 static void hotkey_inputdev_close(struct input_dev
*dev
)
2615 /* disable hotkey polling when possible */
2616 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
&&
2617 !(hotkey_source_mask
& hotkey_driver_mask
))
2618 hotkey_poll_setup_safe(false);
2621 /* sysfs hotkey enable ------------------------------------------------- */
2622 static ssize_t
hotkey_enable_show(struct device
*dev
,
2623 struct device_attribute
*attr
,
2628 printk_deprecated_attribute("hotkey_enable",
2629 "Hotkey reporting is always enabled");
2631 res
= hotkey_status_get(&status
);
2635 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
2638 static ssize_t
hotkey_enable_store(struct device
*dev
,
2639 struct device_attribute
*attr
,
2640 const char *buf
, size_t count
)
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkeys can be disabled through hotkey_mask");
2647 if (parse_strtoul(buf
, 1, &t
))
2656 static struct device_attribute dev_attr_hotkey_enable
=
2657 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
2658 hotkey_enable_show
, hotkey_enable_store
);
2660 /* sysfs hotkey mask --------------------------------------------------- */
2661 static ssize_t
hotkey_mask_show(struct device
*dev
,
2662 struct device_attribute
*attr
,
2665 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_user_mask
);
2668 static ssize_t
hotkey_mask_store(struct device
*dev
,
2669 struct device_attribute
*attr
,
2670 const char *buf
, size_t count
)
2675 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2678 if (mutex_lock_killable(&hotkey_mutex
))
2679 return -ERESTARTSYS
;
2681 res
= hotkey_user_mask_set(t
);
2683 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2684 hotkey_poll_setup(true);
2687 mutex_unlock(&hotkey_mutex
);
2689 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2691 return (res
) ? res
: count
;
2694 static struct device_attribute dev_attr_hotkey_mask
=
2695 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
2696 hotkey_mask_show
, hotkey_mask_store
);
2698 /* sysfs hotkey bios_enabled ------------------------------------------- */
2699 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2700 struct device_attribute
*attr
,
2703 return sprintf(buf
, "0\n");
2706 static struct device_attribute dev_attr_hotkey_bios_enabled
=
2707 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
2709 /* sysfs hotkey bios_mask ---------------------------------------------- */
2710 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2711 struct device_attribute
*attr
,
2714 printk_deprecated_attribute("hotkey_bios_mask",
2715 "This attribute is useless.");
2716 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2719 static struct device_attribute dev_attr_hotkey_bios_mask
=
2720 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
2722 /* sysfs hotkey all_mask ----------------------------------------------- */
2723 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2724 struct device_attribute
*attr
,
2727 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2728 hotkey_all_mask
| hotkey_source_mask
);
2731 static struct device_attribute dev_attr_hotkey_all_mask
=
2732 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2734 /* sysfs hotkey recommended_mask --------------------------------------- */
2735 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2736 struct device_attribute
*attr
,
2739 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2740 (hotkey_all_mask
| hotkey_source_mask
)
2741 & ~hotkey_reserved_mask
);
2744 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2745 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2746 hotkey_recommended_mask_show
, NULL
);
2748 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2750 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2751 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2752 struct device_attribute
*attr
,
2755 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2758 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2759 struct device_attribute
*attr
,
2760 const char *buf
, size_t count
)
2766 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2767 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2770 if (mutex_lock_killable(&hotkey_mutex
))
2771 return -ERESTARTSYS
;
2773 HOTKEY_CONFIG_CRITICAL_START
2774 hotkey_source_mask
= t
;
2775 HOTKEY_CONFIG_CRITICAL_END
2777 rc
= hotkey_mask_set((hotkey_user_mask
| hotkey_driver_mask
) &
2778 ~hotkey_source_mask
);
2779 hotkey_poll_setup(true);
2781 /* check if events needed by the driver got disabled */
2782 r_ev
= hotkey_driver_mask
& ~(hotkey_acpi_mask
& hotkey_all_mask
)
2783 & ~hotkey_source_mask
& TPACPI_HKEY_NVRAM_KNOWN_MASK
;
2785 mutex_unlock(&hotkey_mutex
);
2788 pr_err("hotkey_source_mask: "
2789 "failed to update the firmware event mask!\n");
2792 pr_notice("hotkey_source_mask: "
2793 "some important events were disabled: 0x%04x\n",
2796 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2798 return (rc
< 0) ? rc
: count
;
2801 static struct device_attribute dev_attr_hotkey_source_mask
=
2802 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2803 hotkey_source_mask_show
, hotkey_source_mask_store
);
2805 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2806 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2807 struct device_attribute
*attr
,
2810 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2813 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2814 struct device_attribute
*attr
,
2815 const char *buf
, size_t count
)
2819 if (parse_strtoul(buf
, 25, &t
))
2822 if (mutex_lock_killable(&hotkey_mutex
))
2823 return -ERESTARTSYS
;
2825 hotkey_poll_set_freq(t
);
2826 hotkey_poll_setup(true);
2828 mutex_unlock(&hotkey_mutex
);
2830 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2835 static struct device_attribute dev_attr_hotkey_poll_freq
=
2836 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2837 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2839 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2841 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2842 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2843 struct device_attribute
*attr
,
2847 res
= hotkey_get_wlsw();
2851 /* Opportunistic update */
2852 tpacpi_rfk_update_hwblock_state((res
== TPACPI_RFK_RADIO_OFF
));
2854 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2855 (res
== TPACPI_RFK_RADIO_OFF
) ? 0 : 1);
2858 static struct device_attribute dev_attr_hotkey_radio_sw
=
2859 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2861 static void hotkey_radio_sw_notify_change(void)
2863 if (tp_features
.hotkey_wlsw
)
2864 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2868 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2869 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2870 struct device_attribute
*attr
,
2874 res
= hotkey_get_tablet_mode(&s
);
2878 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2881 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2882 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2884 static void hotkey_tablet_mode_notify_change(void)
2886 if (tp_features
.hotkey_tablet
)
2887 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2888 "hotkey_tablet_mode");
2891 /* sysfs hotkey report_mode -------------------------------------------- */
2892 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2893 struct device_attribute
*attr
,
2896 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2897 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2900 static struct device_attribute dev_attr_hotkey_report_mode
=
2901 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2903 /* sysfs wakeup reason (pollable) -------------------------------------- */
2904 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2905 struct device_attribute
*attr
,
2908 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2911 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2912 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2914 static void hotkey_wakeup_reason_notify_change(void)
2916 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2920 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2921 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2922 struct device_attribute
*attr
,
2925 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2928 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2929 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2930 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2932 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2934 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2935 "wakeup_hotunplug_complete");
2938 /* --------------------------------------------------------------------- */
2940 static struct attribute
*hotkey_attributes
[] __initdata
= {
2941 &dev_attr_hotkey_enable
.attr
,
2942 &dev_attr_hotkey_bios_enabled
.attr
,
2943 &dev_attr_hotkey_bios_mask
.attr
,
2944 &dev_attr_hotkey_report_mode
.attr
,
2945 &dev_attr_hotkey_wakeup_reason
.attr
,
2946 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2947 &dev_attr_hotkey_mask
.attr
,
2948 &dev_attr_hotkey_all_mask
.attr
,
2949 &dev_attr_hotkey_recommended_mask
.attr
,
2950 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2951 &dev_attr_hotkey_source_mask
.attr
,
2952 &dev_attr_hotkey_poll_freq
.attr
,
2957 * Sync both the hw and sw blocking state of all switches
2959 static void tpacpi_send_radiosw_update(void)
2964 * We must sync all rfkill controllers *before* issuing any
2965 * rfkill input events, or we will race the rfkill core input
2968 * tpacpi_inputdev_send_mutex works as a synchronization point
2971 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2974 wlsw
= hotkey_get_wlsw();
2976 /* Sync hw blocking state first if it is hw-blocked */
2977 if (wlsw
== TPACPI_RFK_RADIO_OFF
)
2978 tpacpi_rfk_update_hwblock_state(true);
2980 /* Sync sw blocking state */
2981 tpacpi_rfk_update_swstate_all();
2983 /* Sync hw blocking state last if it is hw-unblocked */
2984 if (wlsw
== TPACPI_RFK_RADIO_ON
)
2985 tpacpi_rfk_update_hwblock_state(false);
2987 /* Issue rfkill input event for WLSW switch */
2989 mutex_lock(&tpacpi_inputdev_send_mutex
);
2991 input_report_switch(tpacpi_inputdev
,
2992 SW_RFKILL_ALL
, (wlsw
> 0));
2993 input_sync(tpacpi_inputdev
);
2995 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2999 * this can be unconditional, as we will poll state again
3000 * if userspace uses the notify to read data
3002 hotkey_radio_sw_notify_change();
3005 static void hotkey_exit(void)
3007 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3008 mutex_lock(&hotkey_mutex
);
3009 hotkey_poll_stop_sync();
3010 mutex_unlock(&hotkey_mutex
);
3013 if (hotkey_dev_attributes
)
3014 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3016 kfree(hotkey_keycode_map
);
3018 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
3019 "restoring original HKEY status and mask\n");
3020 /* yes, there is a bitwise or below, we want the
3021 * functions to be called even if one of them fail */
3022 if (((tp_features
.hotkey_mask
&&
3023 hotkey_mask_set(hotkey_orig_mask
)) |
3024 hotkey_status_set(false)) != 0)
3025 pr_err("failed to restore hot key mask "
3026 "to BIOS defaults\n");
3029 static void __init
hotkey_unmap(const unsigned int scancode
)
3031 if (hotkey_keycode_map
[scancode
] != KEY_RESERVED
) {
3032 clear_bit(hotkey_keycode_map
[scancode
],
3033 tpacpi_inputdev
->keybit
);
3034 hotkey_keycode_map
[scancode
] = KEY_RESERVED
;
3040 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3043 #define TPACPI_HK_Q_INIMASK 0x0001
3045 static const struct tpacpi_quirk tpacpi_hotkey_qtable
[] __initconst
= {
3046 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK
), /* 600E */
3047 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK
), /* 600E */
3048 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK
), /* 770, 770E, 770ED */
3049 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK
), /* A20m */
3050 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK
), /* A20p */
3051 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK
), /* A21e, A22e */
3052 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK
), /* A21e */
3053 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK
), /* A21m, A22m */
3054 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK
), /* A21p, A22p */
3055 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK
), /* A22e */
3056 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK
), /* A22m */
3057 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK
), /* A30/p (0) */
3058 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK
), /* R30 */
3059 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK
), /* R31 */
3060 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK
), /* T20 */
3061 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK
), /* T21 */
3062 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK
), /* T22 */
3063 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK
), /* X20, X21 */
3064 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK
), /* X22, X23, X24 */
3067 typedef u16 tpacpi_keymap_entry_t
;
3068 typedef tpacpi_keymap_entry_t tpacpi_keymap_t
[TPACPI_HOTKEY_MAP_LEN
];
3070 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
3072 /* Requirements for changing the default keymaps:
3074 * 1. Many of the keys are mapped to KEY_RESERVED for very
3075 * good reasons. Do not change them unless you have deep
3076 * knowledge on the IBM and Lenovo ThinkPad firmware for
3077 * the various ThinkPad models. The driver behaves
3078 * differently for KEY_RESERVED: such keys have their
3079 * hot key mask *unset* in mask_recommended, and also
3080 * in the initial hot key mask programmed into the
3081 * firmware at driver load time, which means the firm-
3082 * ware may react very differently if you change them to
3085 * 2. You must be subscribed to the linux-thinkpad and
3086 * ibm-acpi-devel mailing lists, and you should read the
3087 * list archives since 2007 if you want to change the
3088 * keymaps. This requirement exists so that you will
3089 * know the past history of problems with the thinkpad-
3090 * acpi driver keymaps, and also that you will be
3091 * listening to any bug reports;
3093 * 3. Do not send thinkpad-acpi specific patches directly to
3094 * for merging, *ever*. Send them to the linux-acpi
3095 * mailinglist for comments. Merging is to be done only
3096 * through acpi-test and the ACPI maintainer.
3098 * If the above is too much to ask, don't change the keymap.
3099 * Ask the thinkpad-acpi maintainer to do it, instead.
3103 TPACPI_KEYMAP_IBM_GENERIC
= 0,
3104 TPACPI_KEYMAP_LENOVO_GENERIC
,
3107 static const tpacpi_keymap_t tpacpi_keymaps
[] __initconst
= {
3108 /* Generic keymap for IBM ThinkPads */
3109 [TPACPI_KEYMAP_IBM_GENERIC
] = {
3110 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3111 KEY_FN_F1
, KEY_BATTERY
, KEY_COFFEE
, KEY_SLEEP
,
3112 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3113 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3115 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3116 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3117 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3118 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3120 /* brightness: firmware always reacts to them */
3121 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
3122 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
3124 /* Thinklight: firmware always react to it */
3125 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3127 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3128 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3130 /* Volume: firmware always react to it and reprograms
3131 * the built-in *extra* mixer. Never map it to control
3132 * another mixer by default. */
3133 KEY_RESERVED
, /* 0x14: VOLUME UP */
3134 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3135 KEY_RESERVED
, /* 0x16: MUTE */
3137 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3139 /* (assignments unknown, please report if found) */
3140 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3141 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3144 /* Generic keymap for Lenovo ThinkPads */
3145 [TPACPI_KEYMAP_LENOVO_GENERIC
] = {
3146 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3147 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
3148 KEY_WLAN
, KEY_CAMERA
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3149 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3151 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3152 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3153 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3154 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3156 /* These should be enabled --only-- when ACPI video
3157 * is disabled (i.e. in "vendor" mode), and are handled
3158 * in a special way by the init code */
3159 KEY_BRIGHTNESSUP
, /* 0x0F: FN+HOME (brightness up) */
3160 KEY_BRIGHTNESSDOWN
, /* 0x10: FN+END (brightness down) */
3162 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3164 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3165 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3167 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3168 * react to it and reprograms the built-in *extra* mixer.
3169 * Never map it to control another mixer by default.
3171 * T60?, T61, R60?, R61: firmware and EC tries to send
3172 * these over the regular keyboard, so these are no-ops,
3173 * but there are still weird bugs re. MUTE, so do not
3174 * change unless you get test reports from all Lenovo
3175 * models. May cause the BIOS to interfere with the
3178 KEY_RESERVED
, /* 0x14: VOLUME UP */
3179 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3180 KEY_RESERVED
, /* 0x16: MUTE */
3182 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3184 /* (assignments unknown, please report if found) */
3185 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3186 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3190 static const struct tpacpi_quirk tpacpi_keymap_qtable
[] __initconst
= {
3191 /* Generic maps (fallback) */
3193 .vendor
= PCI_VENDOR_ID_IBM
,
3194 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
3195 .quirks
= TPACPI_KEYMAP_IBM_GENERIC
,
3198 .vendor
= PCI_VENDOR_ID_LENOVO
,
3199 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
3200 .quirks
= TPACPI_KEYMAP_LENOVO_GENERIC
,
3204 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3205 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3210 bool radiosw_state
= false;
3211 bool tabletsw_state
= false;
3213 unsigned long quirks
;
3214 unsigned long keymap_id
;
3216 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3217 "initializing hotkey subdriver\n");
3219 BUG_ON(!tpacpi_inputdev
);
3220 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
3221 tpacpi_inputdev
->close
!= NULL
);
3223 TPACPI_ACPIHANDLE_INIT(hkey
);
3224 mutex_init(&hotkey_mutex
);
3226 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3227 mutex_init(&hotkey_thread_mutex
);
3228 mutex_init(&hotkey_thread_data_mutex
);
3231 /* hotkey not supported on 570 */
3232 tp_features
.hotkey
= hkey_handle
!= NULL
;
3234 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3236 str_supported(tp_features
.hotkey
));
3238 if (!tp_features
.hotkey
)
3241 quirks
= tpacpi_check_quirks(tpacpi_hotkey_qtable
,
3242 ARRAY_SIZE(tpacpi_hotkey_qtable
));
3244 tpacpi_disable_brightness_delay();
3246 /* MUST have enough space for all attributes to be added to
3247 * hotkey_dev_attributes */
3248 hotkey_dev_attributes
= create_attr_set(
3249 ARRAY_SIZE(hotkey_attributes
) + 2,
3251 if (!hotkey_dev_attributes
)
3253 res
= add_many_to_attr_set(hotkey_dev_attributes
,
3255 ARRAY_SIZE(hotkey_attributes
));
3259 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3260 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3261 for HKEY interface version 0x100 */
3262 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
3263 if ((hkeyv
>> 8) != 1) {
3264 pr_err("unknown version of the HKEY interface: 0x%x\n",
3266 pr_err("please report this to %s\n", TPACPI_MAIL
);
3269 * MHKV 0x100 in A31, R40, R40e,
3270 * T4x, X31, and later
3272 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3273 "firmware HKEY interface version: 0x%x\n",
3276 /* Paranoia check AND init hotkey_all_mask */
3277 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
3279 pr_err("missing MHKA handler, "
3280 "please report this to %s\n",
3282 /* Fallback: pre-init for FN+F3,F4,F12 */
3283 hotkey_all_mask
= 0x080cU
;
3285 tp_features
.hotkey_mask
= 1;
3290 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3291 "hotkey masks are %s\n",
3292 str_supported(tp_features
.hotkey_mask
));
3294 /* Init hotkey_all_mask if not initialized yet */
3295 if (!tp_features
.hotkey_mask
&& !hotkey_all_mask
&&
3296 (quirks
& TPACPI_HK_Q_INIMASK
))
3297 hotkey_all_mask
= 0x080cU
; /* FN+F12, FN+F4, FN+F3 */
3299 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3300 if (tp_features
.hotkey_mask
) {
3301 /* hotkey_source_mask *must* be zero for
3302 * the first hotkey_mask_get to return hotkey_orig_mask */
3303 res
= hotkey_mask_get();
3307 hotkey_orig_mask
= hotkey_acpi_mask
;
3309 hotkey_orig_mask
= hotkey_all_mask
;
3310 hotkey_acpi_mask
= hotkey_all_mask
;
3313 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3315 tp_features
.hotkey_wlsw
= 1;
3316 radiosw_state
= !!tpacpi_wlsw_emulstate
;
3317 pr_info("radio switch emulation enabled\n");
3320 /* Not all thinkpads have a hardware radio switch */
3321 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
3322 tp_features
.hotkey_wlsw
= 1;
3323 radiosw_state
= !!status
;
3324 pr_info("radio switch found; radios are %s\n",
3325 enabled(status
, 0));
3327 if (tp_features
.hotkey_wlsw
)
3328 res
= add_to_attr_set(hotkey_dev_attributes
,
3329 &dev_attr_hotkey_radio_sw
.attr
);
3331 /* For X41t, X60t, X61t Tablets... */
3332 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
3333 tp_features
.hotkey_tablet
= 1;
3334 tabletsw_state
= !!(status
& TP_HOTKEY_TABLET_MASK
);
3335 pr_info("possible tablet mode switch found; "
3336 "ThinkPad in %s mode\n",
3337 (tabletsw_state
) ? "tablet" : "laptop");
3338 res
= add_to_attr_set(hotkey_dev_attributes
,
3339 &dev_attr_hotkey_tablet_mode
.attr
);
3343 res
= register_attr_set_with_sysfs(
3344 hotkey_dev_attributes
,
3345 &tpacpi_pdev
->dev
.kobj
);
3349 /* Set up key map */
3350 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
3352 if (!hotkey_keycode_map
) {
3353 pr_err("failed to allocate memory for key map\n");
3358 keymap_id
= tpacpi_check_quirks(tpacpi_keymap_qtable
,
3359 ARRAY_SIZE(tpacpi_keymap_qtable
));
3360 BUG_ON(keymap_id
>= ARRAY_SIZE(tpacpi_keymaps
));
3361 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3362 "using keymap number %lu\n", keymap_id
);
3364 memcpy(hotkey_keycode_map
, &tpacpi_keymaps
[keymap_id
],
3365 TPACPI_HOTKEY_MAP_SIZE
);
3367 input_set_capability(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
);
3368 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
3369 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
3370 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
3371 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
3372 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
3373 input_set_capability(tpacpi_inputdev
, EV_KEY
,
3374 hotkey_keycode_map
[i
]);
3376 if (i
< sizeof(hotkey_reserved_mask
)*8)
3377 hotkey_reserved_mask
|= 1 << i
;
3381 if (tp_features
.hotkey_wlsw
) {
3382 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_RFKILL_ALL
);
3383 input_report_switch(tpacpi_inputdev
,
3384 SW_RFKILL_ALL
, radiosw_state
);
3386 if (tp_features
.hotkey_tablet
) {
3387 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_TABLET_MODE
);
3388 input_report_switch(tpacpi_inputdev
,
3389 SW_TABLET_MODE
, tabletsw_state
);
3392 /* Do not issue duplicate brightness change events to
3393 * userspace. tpacpi_detect_brightness_capabilities() must have
3394 * been called before this point */
3395 if (tp_features
.bright_acpimode
&& acpi_video_backlight_support()) {
3396 pr_info("This ThinkPad has standard ACPI backlight "
3397 "brightness control, supported by the ACPI "
3399 pr_notice("Disabling thinkpad-acpi brightness events "
3402 /* Disable brightness up/down on Lenovo thinkpads when
3403 * ACPI is handling them, otherwise it is plain impossible
3404 * for userspace to do something even remotely sane */
3405 hotkey_reserved_mask
|=
3406 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
3407 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
3408 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME
);
3409 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND
);
3412 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3413 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
3415 & ~hotkey_reserved_mask
;
3417 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3418 "hotkey source mask 0x%08x, polling freq %u\n",
3419 hotkey_source_mask
, hotkey_poll_freq
);
3422 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3423 "enabling firmware HKEY event interface...\n");
3424 res
= hotkey_status_set(true);
3429 res
= hotkey_mask_set(((hotkey_all_mask
& ~hotkey_reserved_mask
)
3430 | hotkey_driver_mask
)
3431 & ~hotkey_source_mask
);
3432 if (res
< 0 && res
!= -ENXIO
) {
3436 hotkey_user_mask
= (hotkey_acpi_mask
| hotkey_source_mask
)
3437 & ~hotkey_reserved_mask
;
3438 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3439 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3440 hotkey_user_mask
, hotkey_acpi_mask
, hotkey_source_mask
);
3442 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3443 "legacy ibm/hotkey event reporting over procfs %s\n",
3444 (hotkey_report_mode
< 2) ?
3445 "enabled" : "disabled");
3447 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
3448 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
3450 hotkey_poll_setup_safe(true);
3455 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3456 hotkey_dev_attributes
= NULL
;
3458 return (res
< 0)? res
: 1;
3461 static bool hotkey_notify_hotkey(const u32 hkey
,
3463 bool *ignore_acpi_ev
)
3465 /* 0x1000-0x1FFF: key presses */
3466 unsigned int scancode
= hkey
& 0xfff;
3467 *send_acpi_ev
= true;
3468 *ignore_acpi_ev
= false;
3470 /* HKEY event 0x1001 is scancode 0x00 */
3471 if (scancode
> 0 && scancode
<= TPACPI_HOTKEY_MAP_LEN
) {
3473 if (!(hotkey_source_mask
& (1 << scancode
))) {
3474 tpacpi_input_send_key_masked(scancode
);
3475 *send_acpi_ev
= false;
3477 *ignore_acpi_ev
= true;
3484 static bool hotkey_notify_wakeup(const u32 hkey
,
3486 bool *ignore_acpi_ev
)
3488 /* 0x2000-0x2FFF: Wakeup reason */
3489 *send_acpi_ev
= true;
3490 *ignore_acpi_ev
= false;
3493 case TP_HKEY_EV_WKUP_S3_UNDOCK
: /* suspend, undock */
3494 case TP_HKEY_EV_WKUP_S4_UNDOCK
: /* hibernation, undock */
3495 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
3496 *ignore_acpi_ev
= true;
3499 case TP_HKEY_EV_WKUP_S3_BAYEJ
: /* suspend, bay eject */
3500 case TP_HKEY_EV_WKUP_S4_BAYEJ
: /* hibernation, bay eject */
3501 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
3502 *ignore_acpi_ev
= true;
3505 case TP_HKEY_EV_WKUP_S3_BATLOW
: /* Battery on critical low level/S3 */
3506 case TP_HKEY_EV_WKUP_S4_BATLOW
: /* Battery on critical low level/S4 */
3507 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3508 /* how to auto-heal: */
3509 /* 2313: woke up from S3, go to S4/S5 */
3510 /* 2413: woke up from S4, go to S5 */
3517 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
3518 pr_info("woke up due to a hot-unplug request...\n");
3519 hotkey_wakeup_reason_notify_change();
3524 static bool hotkey_notify_usrevent(const u32 hkey
,
3526 bool *ignore_acpi_ev
)
3528 /* 0x5000-0x5FFF: human interface helpers */
3529 *send_acpi_ev
= true;
3530 *ignore_acpi_ev
= false;
3533 case TP_HKEY_EV_PEN_INSERTED
: /* X61t: tablet pen inserted into bay */
3534 case TP_HKEY_EV_PEN_REMOVED
: /* X61t: tablet pen removed from bay */
3537 case TP_HKEY_EV_TABLET_TABLET
: /* X41t-X61t: tablet mode */
3538 case TP_HKEY_EV_TABLET_NOTEBOOK
: /* X41t-X61t: normal mode */
3539 tpacpi_input_send_tabletsw();
3540 hotkey_tablet_mode_notify_change();
3541 *send_acpi_ev
= false;
3544 case TP_HKEY_EV_LID_CLOSE
: /* Lid closed */
3545 case TP_HKEY_EV_LID_OPEN
: /* Lid opened */
3546 case TP_HKEY_EV_BRGHT_CHANGED
: /* brightness changed */
3547 /* do not propagate these events */
3548 *ignore_acpi_ev
= true;
3556 static void thermal_dump_all_sensors(void);
3558 static bool hotkey_notify_6xxx(const u32 hkey
,
3560 bool *ignore_acpi_ev
)
3564 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3565 *send_acpi_ev
= true;
3566 *ignore_acpi_ev
= false;
3569 case TP_HKEY_EV_THM_TABLE_CHANGED
:
3570 pr_info("EC reports that Thermal Table has changed\n");
3571 /* recommended action: do nothing, we don't have
3572 * Lenovo ATM information */
3574 case TP_HKEY_EV_ALARM_BAT_HOT
:
3575 pr_crit("THERMAL ALARM: battery is too hot!\n");
3576 /* recommended action: warn user through gui */
3578 case TP_HKEY_EV_ALARM_BAT_XHOT
:
3579 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3580 /* recommended action: immediate sleep/hibernate */
3582 case TP_HKEY_EV_ALARM_SENSOR_HOT
:
3583 pr_crit("THERMAL ALARM: "
3584 "a sensor reports something is too hot!\n");
3585 /* recommended action: warn user through gui, that */
3586 /* some internal component is too hot */
3588 case TP_HKEY_EV_ALARM_SENSOR_XHOT
:
3589 pr_alert("THERMAL EMERGENCY: "
3590 "a sensor reports something is extremely hot!\n");
3591 /* recommended action: immediate sleep/hibernate */
3594 case TP_HKEY_EV_KEY_NUMLOCK
:
3595 case TP_HKEY_EV_KEY_FN
:
3596 /* key press events, we just ignore them as long as the EC
3597 * is still reporting them in the normal keyboard stream */
3598 *send_acpi_ev
= false;
3599 *ignore_acpi_ev
= true;
3603 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3607 thermal_dump_all_sensors();
3612 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
3616 bool ignore_acpi_ev
;
3619 if (event
!= 0x80) {
3620 pr_err("unknown HKEY notification event %d\n", event
);
3621 /* forward it to userspace, maybe it knows how to handle it */
3622 acpi_bus_generate_netlink_event(
3623 ibm
->acpi
->device
->pnp
.device_class
,
3624 dev_name(&ibm
->acpi
->device
->dev
),
3630 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
3631 pr_err("failed to retrieve HKEY event\n");
3640 send_acpi_ev
= true;
3641 ignore_acpi_ev
= false;
3643 switch (hkey
>> 12) {
3645 /* 0x1000-0x1FFF: key presses */
3646 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
3650 /* 0x2000-0x2FFF: Wakeup reason */
3651 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
3655 /* 0x3000-0x3FFF: bay-related wakeups */
3657 case TP_HKEY_EV_BAYEJ_ACK
:
3658 hotkey_autosleep_ack
= 1;
3659 pr_info("bay ejected\n");
3660 hotkey_wakeup_hotunplug_complete_notify_change();
3663 case TP_HKEY_EV_OPTDRV_EJ
:
3664 /* FIXME: kick libata if SATA link offline */
3672 /* 0x4000-0x4FFF: dock-related wakeups */
3673 if (hkey
== TP_HKEY_EV_UNDOCK_ACK
) {
3674 hotkey_autosleep_ack
= 1;
3675 pr_info("undocked\n");
3676 hotkey_wakeup_hotunplug_complete_notify_change();
3683 /* 0x5000-0x5FFF: human interface helpers */
3684 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
3688 /* 0x6000-0x6FFF: thermal alarms/notices and
3689 * keyboard events */
3690 known_ev
= hotkey_notify_6xxx(hkey
, &send_acpi_ev
,
3694 /* 0x7000-0x7FFF: misc */
3695 if (tp_features
.hotkey_wlsw
&&
3696 hkey
== TP_HKEY_EV_RFKILL_CHANGED
) {
3697 tpacpi_send_radiosw_update();
3702 /* fallthrough to default */
3707 pr_notice("unhandled HKEY event 0x%04x\n", hkey
);
3708 pr_notice("please report the conditions when this "
3709 "event happened to %s\n", TPACPI_MAIL
);
3713 if (!ignore_acpi_ev
&&
3714 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
3715 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
3719 /* netlink events */
3720 if (!ignore_acpi_ev
&& send_acpi_ev
) {
3721 acpi_bus_generate_netlink_event(
3722 ibm
->acpi
->device
->pnp
.device_class
,
3723 dev_name(&ibm
->acpi
->device
->dev
),
3729 static void hotkey_suspend(pm_message_t state
)
3731 /* Do these on suspend, we get the events on early resume! */
3732 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
3733 hotkey_autosleep_ack
= 0;
3736 static void hotkey_resume(void)
3738 tpacpi_disable_brightness_delay();
3740 if (hotkey_status_set(true) < 0 ||
3741 hotkey_mask_set(hotkey_acpi_mask
) < 0)
3742 pr_err("error while attempting to reset the event "
3743 "firmware interface\n");
3745 tpacpi_send_radiosw_update();
3746 hotkey_tablet_mode_notify_change();
3747 hotkey_wakeup_reason_notify_change();
3748 hotkey_wakeup_hotunplug_complete_notify_change();
3749 hotkey_poll_setup_safe(false);
3752 /* procfs -------------------------------------------------------------- */
3753 static int hotkey_read(struct seq_file
*m
)
3757 if (!tp_features
.hotkey
) {
3758 seq_printf(m
, "status:\t\tnot supported\n");
3762 if (mutex_lock_killable(&hotkey_mutex
))
3763 return -ERESTARTSYS
;
3764 res
= hotkey_status_get(&status
);
3766 res
= hotkey_mask_get();
3767 mutex_unlock(&hotkey_mutex
);
3771 seq_printf(m
, "status:\t\t%s\n", enabled(status
, 0));
3772 if (hotkey_all_mask
) {
3773 seq_printf(m
, "mask:\t\t0x%08x\n", hotkey_user_mask
);
3774 seq_printf(m
, "commands:\tenable, disable, reset, <mask>\n");
3776 seq_printf(m
, "mask:\t\tnot supported\n");
3777 seq_printf(m
, "commands:\tenable, disable, reset\n");
3783 static void hotkey_enabledisable_warn(bool enable
)
3785 tpacpi_log_usertask("procfs hotkey enable/disable");
3786 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
3787 pr_fmt("hotkey enable/disable functionality has been "
3788 "removed from the driver. "
3789 "Hotkeys are always enabled.\n")))
3790 pr_err("Please remove the hotkey=enable module "
3791 "parameter, it is deprecated. "
3792 "Hotkeys are always enabled.\n");
3795 static int hotkey_write(char *buf
)
3801 if (!tp_features
.hotkey
)
3804 if (mutex_lock_killable(&hotkey_mutex
))
3805 return -ERESTARTSYS
;
3807 mask
= hotkey_user_mask
;
3810 while ((cmd
= next_cmd(&buf
))) {
3811 if (strlencmp(cmd
, "enable") == 0) {
3812 hotkey_enabledisable_warn(1);
3813 } else if (strlencmp(cmd
, "disable") == 0) {
3814 hotkey_enabledisable_warn(0);
3816 } else if (strlencmp(cmd
, "reset") == 0) {
3817 mask
= (hotkey_all_mask
| hotkey_source_mask
)
3818 & ~hotkey_reserved_mask
;
3819 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
3821 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
3830 tpacpi_disclose_usertask("procfs hotkey",
3831 "set mask to 0x%08x\n", mask
);
3832 res
= hotkey_user_mask_set(mask
);
3836 mutex_unlock(&hotkey_mutex
);
3840 static const struct acpi_device_id ibm_htk_device_ids
[] = {
3841 {TPACPI_ACPI_IBM_HKEY_HID
, 0},
3842 {TPACPI_ACPI_LENOVO_HKEY_HID
, 0},
3846 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
3847 .hid
= ibm_htk_device_ids
,
3848 .notify
= hotkey_notify
,
3849 .handle
= &hkey_handle
,
3850 .type
= ACPI_DEVICE_NOTIFY
,
3853 static struct ibm_struct hotkey_driver_data
= {
3855 .read
= hotkey_read
,
3856 .write
= hotkey_write
,
3857 .exit
= hotkey_exit
,
3858 .resume
= hotkey_resume
,
3859 .suspend
= hotkey_suspend
,
3860 .acpi
= &ibm_hotkey_acpidriver
,
3863 /*************************************************************************
3864 * Bluetooth subdriver
3868 /* ACPI GBDC/SBDC bits */
3869 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3870 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3871 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3872 0 = disable, 1 = enable */
3876 /* ACPI \BLTH commands */
3877 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3878 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3879 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3880 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3881 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3884 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3886 static int bluetooth_get_status(void)
3890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_bluetoothemul
)
3892 return (tpacpi_bluetooth_emulstate
) ?
3893 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3896 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3899 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3900 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3903 static int bluetooth_set_status(enum tpacpi_rfkill_state state
)
3907 vdbg_printk(TPACPI_DBG_RFKILL
,
3908 "will attempt to %s bluetooth\n",
3909 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
3911 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3912 if (dbg_bluetoothemul
) {
3913 tpacpi_bluetooth_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
3918 if (state
== TPACPI_RFK_RADIO_ON
)
3919 status
= TP_ACPI_BLUETOOTH_RADIOSSW
3920 | TP_ACPI_BLUETOOTH_RESUMECTRL
;
3924 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3930 /* sysfs bluetooth enable ---------------------------------------------- */
3931 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3932 struct device_attribute
*attr
,
3935 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID
,
3939 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3940 struct device_attribute
*attr
,
3941 const char *buf
, size_t count
)
3943 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID
,
3947 static struct device_attribute dev_attr_bluetooth_enable
=
3948 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3949 bluetooth_enable_show
, bluetooth_enable_store
);
3951 /* --------------------------------------------------------------------- */
3953 static struct attribute
*bluetooth_attributes
[] = {
3954 &dev_attr_bluetooth_enable
.attr
,
3958 static const struct attribute_group bluetooth_attr_group
= {
3959 .attrs
= bluetooth_attributes
,
3962 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops
= {
3963 .get_status
= bluetooth_get_status
,
3964 .set_status
= bluetooth_set_status
,
3967 static void bluetooth_shutdown(void)
3969 /* Order firmware to save current state to NVRAM */
3970 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3971 TP_ACPI_BLTH_SAVE_STATE
))
3972 pr_notice("failed to save bluetooth state to NVRAM\n");
3974 vdbg_printk(TPACPI_DBG_RFKILL
,
3975 "bluestooth state saved to NVRAM\n");
3978 static void bluetooth_exit(void)
3980 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3981 &bluetooth_attr_group
);
3983 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
3985 bluetooth_shutdown();
3988 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3993 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3994 "initializing bluetooth subdriver\n");
3996 TPACPI_ACPIHANDLE_INIT(hkey
);
3998 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3999 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4000 tp_features
.bluetooth
= hkey_handle
&&
4001 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
4003 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4004 "bluetooth is %s, status 0x%02x\n",
4005 str_supported(tp_features
.bluetooth
),
4008 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4009 if (dbg_bluetoothemul
) {
4010 tp_features
.bluetooth
= 1;
4011 pr_info("bluetooth switch emulation enabled\n");
4014 if (tp_features
.bluetooth
&&
4015 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
4016 /* no bluetooth hardware present in system */
4017 tp_features
.bluetooth
= 0;
4018 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4019 "bluetooth hardware not installed\n");
4022 if (!tp_features
.bluetooth
)
4025 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
4026 &bluetooth_tprfk_ops
,
4027 RFKILL_TYPE_BLUETOOTH
,
4028 TPACPI_RFK_BLUETOOTH_SW_NAME
,
4033 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4034 &bluetooth_attr_group
);
4036 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
4043 /* procfs -------------------------------------------------------------- */
4044 static int bluetooth_read(struct seq_file
*m
)
4046 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID
, m
);
4049 static int bluetooth_write(char *buf
)
4051 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID
, buf
);
4054 static struct ibm_struct bluetooth_driver_data
= {
4055 .name
= "bluetooth",
4056 .read
= bluetooth_read
,
4057 .write
= bluetooth_write
,
4058 .exit
= bluetooth_exit
,
4059 .shutdown
= bluetooth_shutdown
,
4062 /*************************************************************************
4067 /* ACPI GWAN/SWAN bits */
4068 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
4069 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
4070 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
4071 0 = disable, 1 = enable */
4074 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4076 static int wan_get_status(void)
4080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4082 return (tpacpi_wwan_emulstate
) ?
4083 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4086 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
4089 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
4090 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4093 static int wan_set_status(enum tpacpi_rfkill_state state
)
4097 vdbg_printk(TPACPI_DBG_RFKILL
,
4098 "will attempt to %s wwan\n",
4099 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4101 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4103 tpacpi_wwan_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4108 if (state
== TPACPI_RFK_RADIO_ON
)
4109 status
= TP_ACPI_WANCARD_RADIOSSW
4110 | TP_ACPI_WANCARD_RESUMECTRL
;
4114 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
4120 /* sysfs wan enable ---------------------------------------------------- */
4121 static ssize_t
wan_enable_show(struct device
*dev
,
4122 struct device_attribute
*attr
,
4125 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID
,
4129 static ssize_t
wan_enable_store(struct device
*dev
,
4130 struct device_attribute
*attr
,
4131 const char *buf
, size_t count
)
4133 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID
,
4137 static struct device_attribute dev_attr_wan_enable
=
4138 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
4139 wan_enable_show
, wan_enable_store
);
4141 /* --------------------------------------------------------------------- */
4143 static struct attribute
*wan_attributes
[] = {
4144 &dev_attr_wan_enable
.attr
,
4148 static const struct attribute_group wan_attr_group
= {
4149 .attrs
= wan_attributes
,
4152 static const struct tpacpi_rfk_ops wan_tprfk_ops
= {
4153 .get_status
= wan_get_status
,
4154 .set_status
= wan_set_status
,
4157 static void wan_shutdown(void)
4159 /* Order firmware to save current state to NVRAM */
4160 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
4161 TP_ACPI_WGSV_SAVE_STATE
))
4162 pr_notice("failed to save WWAN state to NVRAM\n");
4164 vdbg_printk(TPACPI_DBG_RFKILL
,
4165 "WWAN state saved to NVRAM\n");
4168 static void wan_exit(void)
4170 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4173 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4178 static int __init
wan_init(struct ibm_init_struct
*iibm
)
4183 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4184 "initializing wan subdriver\n");
4186 TPACPI_ACPIHANDLE_INIT(hkey
);
4188 tp_features
.wan
= hkey_handle
&&
4189 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
4191 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4192 "wan is %s, status 0x%02x\n",
4193 str_supported(tp_features
.wan
),
4196 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4198 tp_features
.wan
= 1;
4199 pr_info("wwan switch emulation enabled\n");
4202 if (tp_features
.wan
&&
4203 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
4204 /* no wan hardware present in system */
4205 tp_features
.wan
= 0;
4206 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4207 "wan hardware not installed\n");
4210 if (!tp_features
.wan
)
4213 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
4216 TPACPI_RFK_WWAN_SW_NAME
,
4221 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4225 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4232 /* procfs -------------------------------------------------------------- */
4233 static int wan_read(struct seq_file
*m
)
4235 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID
, m
);
4238 static int wan_write(char *buf
)
4240 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID
, buf
);
4243 static struct ibm_struct wan_driver_data
= {
4248 .shutdown
= wan_shutdown
,
4251 /*************************************************************************
4256 /* ACPI GUWB/SUWB bits */
4257 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
4258 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
4261 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4263 static int uwb_get_status(void)
4267 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4269 return (tpacpi_uwb_emulstate
) ?
4270 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4273 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
4276 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
4277 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4280 static int uwb_set_status(enum tpacpi_rfkill_state state
)
4284 vdbg_printk(TPACPI_DBG_RFKILL
,
4285 "will attempt to %s UWB\n",
4286 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4288 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4290 tpacpi_uwb_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4295 if (state
== TPACPI_RFK_RADIO_ON
)
4296 status
= TP_ACPI_UWB_RADIOSSW
;
4300 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
4306 /* --------------------------------------------------------------------- */
4308 static const struct tpacpi_rfk_ops uwb_tprfk_ops
= {
4309 .get_status
= uwb_get_status
,
4310 .set_status
= uwb_set_status
,
4313 static void uwb_exit(void)
4315 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID
);
4318 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
4323 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4324 "initializing uwb subdriver\n");
4326 TPACPI_ACPIHANDLE_INIT(hkey
);
4328 tp_features
.uwb
= hkey_handle
&&
4329 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
4331 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4332 "uwb is %s, status 0x%02x\n",
4333 str_supported(tp_features
.uwb
),
4336 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4338 tp_features
.uwb
= 1;
4339 pr_info("uwb switch emulation enabled\n");
4342 if (tp_features
.uwb
&&
4343 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
4344 /* no uwb hardware present in system */
4345 tp_features
.uwb
= 0;
4346 dbg_printk(TPACPI_DBG_INIT
,
4347 "uwb hardware not installed\n");
4350 if (!tp_features
.uwb
)
4353 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
4356 TPACPI_RFK_UWB_SW_NAME
,
4361 static struct ibm_struct uwb_driver_data
= {
4364 .flags
.experimental
= 1,
4367 /*************************************************************************
4371 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4373 enum video_access_mode
{
4374 TPACPI_VIDEO_NONE
= 0,
4375 TPACPI_VIDEO_570
, /* 570 */
4376 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
4377 TPACPI_VIDEO_NEW
, /* all others */
4380 enum { /* video status flags, based on VIDEO_570 */
4381 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
4382 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
4383 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
4386 enum { /* TPACPI_VIDEO_570 constants */
4387 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
4388 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
4389 * video_status_flags */
4390 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
4391 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
4394 static enum video_access_mode video_supported
;
4395 static int video_orig_autosw
;
4397 static int video_autosw_get(void);
4398 static int video_autosw_set(int enable
);
4400 TPACPI_HANDLE(vid
, root
,
4401 "\\_SB.PCI.AGP.VGA", /* 570 */
4402 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4403 "\\_SB.PCI0.VID0", /* 770e */
4404 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4405 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4406 "\\_SB.PCI0.AGP.VID", /* all others */
4409 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4411 static int __init
video_init(struct ibm_init_struct
*iibm
)
4415 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
4417 TPACPI_ACPIHANDLE_INIT(vid
);
4418 if (tpacpi_is_ibm())
4419 TPACPI_ACPIHANDLE_INIT(vid2
);
4421 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
4422 /* G41, assume IVGA doesn't change */
4423 vid_handle
= vid2_handle
;
4426 /* video switching not supported on R30, R31 */
4427 video_supported
= TPACPI_VIDEO_NONE
;
4428 else if (tpacpi_is_ibm() &&
4429 acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
4431 video_supported
= TPACPI_VIDEO_570
;
4432 else if (tpacpi_is_ibm() &&
4433 acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
4434 /* 600e/x, 770e, 770x */
4435 video_supported
= TPACPI_VIDEO_770
;
4438 video_supported
= TPACPI_VIDEO_NEW
;
4440 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
4441 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
4444 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
4447 static void video_exit(void)
4449 dbg_printk(TPACPI_DBG_EXIT
,
4450 "restoring original video autoswitch mode\n");
4451 if (video_autosw_set(video_orig_autosw
))
4452 pr_err("error while trying to restore original "
4453 "video autoswitch mode\n");
4456 static int video_outputsw_get(void)
4461 switch (video_supported
) {
4462 case TPACPI_VIDEO_570
:
4463 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
4464 TP_ACPI_VIDEO_570_PHSCMD
))
4466 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
4468 case TPACPI_VIDEO_770
:
4469 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4472 status
|= TP_ACPI_VIDEO_S_LCD
;
4473 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4476 status
|= TP_ACPI_VIDEO_S_CRT
;
4478 case TPACPI_VIDEO_NEW
:
4479 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4480 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4483 status
|= TP_ACPI_VIDEO_S_CRT
;
4485 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4486 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4489 status
|= TP_ACPI_VIDEO_S_LCD
;
4490 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4493 status
|= TP_ACPI_VIDEO_S_DVI
;
4502 static int video_outputsw_set(int status
)
4507 switch (video_supported
) {
4508 case TPACPI_VIDEO_570
:
4509 res
= acpi_evalf(NULL
, NULL
,
4510 "\\_SB.PHS2", "vdd",
4511 TP_ACPI_VIDEO_570_PHS2CMD
,
4512 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4514 case TPACPI_VIDEO_770
:
4515 autosw
= video_autosw_get();
4519 res
= video_autosw_set(1);
4522 res
= acpi_evalf(vid_handle
, NULL
,
4523 "ASWT", "vdd", status
* 0x100, 0);
4524 if (!autosw
&& video_autosw_set(autosw
)) {
4525 pr_err("video auto-switch left enabled due to error\n");
4529 case TPACPI_VIDEO_NEW
:
4530 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
4531 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
4537 return (res
)? 0 : -EIO
;
4540 static int video_autosw_get(void)
4544 switch (video_supported
) {
4545 case TPACPI_VIDEO_570
:
4546 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
4549 case TPACPI_VIDEO_770
:
4550 case TPACPI_VIDEO_NEW
:
4551 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4561 static int video_autosw_set(int enable
)
4563 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
4568 static int video_outputsw_cycle(void)
4570 int autosw
= video_autosw_get();
4576 switch (video_supported
) {
4577 case TPACPI_VIDEO_570
:
4578 res
= video_autosw_set(1);
4581 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4583 case TPACPI_VIDEO_770
:
4584 case TPACPI_VIDEO_NEW
:
4585 res
= video_autosw_set(1);
4588 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4593 if (!autosw
&& video_autosw_set(autosw
)) {
4594 pr_err("video auto-switch left enabled due to error\n");
4598 return (res
)? 0 : -EIO
;
4601 static int video_expand_toggle(void)
4603 switch (video_supported
) {
4604 case TPACPI_VIDEO_570
:
4605 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4607 case TPACPI_VIDEO_770
:
4608 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4610 case TPACPI_VIDEO_NEW
:
4611 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4619 static int video_read(struct seq_file
*m
)
4623 if (video_supported
== TPACPI_VIDEO_NONE
) {
4624 seq_printf(m
, "status:\t\tnot supported\n");
4628 /* Even reads can crash X.org, so... */
4629 if (!capable(CAP_SYS_ADMIN
))
4632 status
= video_outputsw_get();
4636 autosw
= video_autosw_get();
4640 seq_printf(m
, "status:\t\tsupported\n");
4641 seq_printf(m
, "lcd:\t\t%s\n", enabled(status
, 0));
4642 seq_printf(m
, "crt:\t\t%s\n", enabled(status
, 1));
4643 if (video_supported
== TPACPI_VIDEO_NEW
)
4644 seq_printf(m
, "dvi:\t\t%s\n", enabled(status
, 3));
4645 seq_printf(m
, "auto:\t\t%s\n", enabled(autosw
, 0));
4646 seq_printf(m
, "commands:\tlcd_enable, lcd_disable\n");
4647 seq_printf(m
, "commands:\tcrt_enable, crt_disable\n");
4648 if (video_supported
== TPACPI_VIDEO_NEW
)
4649 seq_printf(m
, "commands:\tdvi_enable, dvi_disable\n");
4650 seq_printf(m
, "commands:\tauto_enable, auto_disable\n");
4651 seq_printf(m
, "commands:\tvideo_switch, expand_toggle\n");
4656 static int video_write(char *buf
)
4659 int enable
, disable
, status
;
4662 if (video_supported
== TPACPI_VIDEO_NONE
)
4665 /* Even reads can crash X.org, let alone writes... */
4666 if (!capable(CAP_SYS_ADMIN
))
4672 while ((cmd
= next_cmd(&buf
))) {
4673 if (strlencmp(cmd
, "lcd_enable") == 0) {
4674 enable
|= TP_ACPI_VIDEO_S_LCD
;
4675 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4676 disable
|= TP_ACPI_VIDEO_S_LCD
;
4677 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4678 enable
|= TP_ACPI_VIDEO_S_CRT
;
4679 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4680 disable
|= TP_ACPI_VIDEO_S_CRT
;
4681 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4682 strlencmp(cmd
, "dvi_enable") == 0) {
4683 enable
|= TP_ACPI_VIDEO_S_DVI
;
4684 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4685 strlencmp(cmd
, "dvi_disable") == 0) {
4686 disable
|= TP_ACPI_VIDEO_S_DVI
;
4687 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4688 res
= video_autosw_set(1);
4691 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4692 res
= video_autosw_set(0);
4695 } else if (strlencmp(cmd
, "video_switch") == 0) {
4696 res
= video_outputsw_cycle();
4699 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4700 res
= video_expand_toggle();
4707 if (enable
|| disable
) {
4708 status
= video_outputsw_get();
4711 res
= video_outputsw_set((status
& ~disable
) | enable
);
4719 static struct ibm_struct video_driver_data
= {
4722 .write
= video_write
,
4726 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4728 /*************************************************************************
4729 * Light (thinklight) subdriver
4732 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4733 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4735 static int light_get_status(void)
4739 if (tp_features
.light_status
) {
4740 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4748 static int light_set_status(int status
)
4752 if (tp_features
.light
) {
4754 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4756 TP_CMOS_THINKLIGHT_ON
:
4757 TP_CMOS_THINKLIGHT_OFF
);
4759 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4762 return (rc
)? 0 : -EIO
;
4768 static void light_set_status_worker(struct work_struct
*work
)
4770 struct tpacpi_led_classdev
*data
=
4771 container_of(work
, struct tpacpi_led_classdev
, work
);
4773 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4774 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4777 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4778 enum led_brightness brightness
)
4780 struct tpacpi_led_classdev
*data
=
4781 container_of(led_cdev
,
4782 struct tpacpi_led_classdev
,
4784 data
->new_state
= (brightness
!= LED_OFF
) ?
4785 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4786 queue_work(tpacpi_wq
, &data
->work
);
4789 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4791 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4794 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4796 .name
= "tpacpi::thinklight",
4797 .brightness_set
= &light_sysfs_set
,
4798 .brightness_get
= &light_sysfs_get
,
4802 static int __init
light_init(struct ibm_init_struct
*iibm
)
4806 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4808 if (tpacpi_is_ibm()) {
4809 TPACPI_ACPIHANDLE_INIT(ledb
);
4810 TPACPI_ACPIHANDLE_INIT(lght
);
4812 TPACPI_ACPIHANDLE_INIT(cmos
);
4813 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4815 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4816 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4818 if (tp_features
.light
)
4819 /* light status not supported on
4820 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4821 tp_features
.light_status
=
4822 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4824 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4825 str_supported(tp_features
.light
),
4826 str_supported(tp_features
.light_status
));
4828 if (!tp_features
.light
)
4831 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4832 &tpacpi_led_thinklight
.led_classdev
);
4835 tp_features
.light
= 0;
4836 tp_features
.light_status
= 0;
4844 static void light_exit(void)
4846 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4847 if (work_pending(&tpacpi_led_thinklight
.work
))
4848 flush_workqueue(tpacpi_wq
);
4851 static int light_read(struct seq_file
*m
)
4855 if (!tp_features
.light
) {
4856 seq_printf(m
, "status:\t\tnot supported\n");
4857 } else if (!tp_features
.light_status
) {
4858 seq_printf(m
, "status:\t\tunknown\n");
4859 seq_printf(m
, "commands:\ton, off\n");
4861 status
= light_get_status();
4864 seq_printf(m
, "status:\t\t%s\n", onoff(status
, 0));
4865 seq_printf(m
, "commands:\ton, off\n");
4871 static int light_write(char *buf
)
4876 if (!tp_features
.light
)
4879 while ((cmd
= next_cmd(&buf
))) {
4880 if (strlencmp(cmd
, "on") == 0) {
4882 } else if (strlencmp(cmd
, "off") == 0) {
4888 return light_set_status(newstatus
);
4891 static struct ibm_struct light_driver_data
= {
4894 .write
= light_write
,
4898 /*************************************************************************
4902 /* sysfs cmos_command -------------------------------------------------- */
4903 static ssize_t
cmos_command_store(struct device
*dev
,
4904 struct device_attribute
*attr
,
4905 const char *buf
, size_t count
)
4907 unsigned long cmos_cmd
;
4910 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4913 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4914 return (res
)? res
: count
;
4917 static struct device_attribute dev_attr_cmos_command
=
4918 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4920 /* --------------------------------------------------------------------- */
4922 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4926 vdbg_printk(TPACPI_DBG_INIT
,
4927 "initializing cmos commands subdriver\n");
4929 TPACPI_ACPIHANDLE_INIT(cmos
);
4931 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4932 str_supported(cmos_handle
!= NULL
));
4934 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4938 return (cmos_handle
)? 0 : 1;
4941 static void cmos_exit(void)
4943 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4946 static int cmos_read(struct seq_file
*m
)
4948 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4949 R30, R31, T20-22, X20-21 */
4951 seq_printf(m
, "status:\t\tnot supported\n");
4953 seq_printf(m
, "status:\t\tsupported\n");
4954 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4960 static int cmos_write(char *buf
)
4965 while ((cmd
= next_cmd(&buf
))) {
4966 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4967 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4972 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4980 static struct ibm_struct cmos_driver_data
= {
4983 .write
= cmos_write
,
4987 /*************************************************************************
4991 enum led_access_mode
{
4992 TPACPI_LED_NONE
= 0,
4993 TPACPI_LED_570
, /* 570 */
4994 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4995 TPACPI_LED_NEW
, /* all others */
4998 enum { /* For TPACPI_LED_OLD */
4999 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
5000 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
5001 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
5004 static enum led_access_mode led_supported
;
5006 static acpi_handle led_handle
;
5008 #define TPACPI_LED_NUMLEDS 16
5009 static struct tpacpi_led_classdev
*tpacpi_leds
;
5010 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
5011 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
5012 /* there's a limit of 19 chars + NULL before 2.6.26 */
5014 "tpacpi:orange:batt",
5015 "tpacpi:green:batt",
5016 "tpacpi::dock_active",
5017 "tpacpi::bay_active",
5018 "tpacpi::dock_batt",
5019 "tpacpi::unknown_led",
5021 "tpacpi::dock_status1",
5022 "tpacpi::dock_status2",
5023 "tpacpi::unknown_led2",
5024 "tpacpi::unknown_led3",
5025 "tpacpi::thinkvantage",
5027 #define TPACPI_SAFE_LEDS 0x1081U
5029 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
5031 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5034 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
5038 static int led_get_status(const unsigned int led
)
5041 enum led_status_t led_s
;
5043 switch (led_supported
) {
5044 case TPACPI_LED_570
:
5045 if (!acpi_evalf(ec_handle
,
5046 &status
, "GLED", "dd", 1 << led
))
5048 led_s
= (status
== 0)?
5053 tpacpi_led_state_cache
[led
] = led_s
;
5062 static int led_set_status(const unsigned int led
,
5063 const enum led_status_t ledstatus
)
5065 /* off, on, blink. Index is led_status_t */
5066 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
5067 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
5071 switch (led_supported
) {
5072 case TPACPI_LED_570
:
5074 if (unlikely(led
> 7))
5076 if (unlikely(tpacpi_is_led_restricted(led
)))
5078 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5079 (1 << led
), led_sled_arg1
[ledstatus
]))
5082 case TPACPI_LED_OLD
:
5083 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5084 if (unlikely(led
> 7))
5086 if (unlikely(tpacpi_is_led_restricted(led
)))
5088 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
5090 rc
= ec_write(TPACPI_LED_EC_HLBL
,
5091 (ledstatus
== TPACPI_LED_BLINK
) << led
);
5093 rc
= ec_write(TPACPI_LED_EC_HLCL
,
5094 (ledstatus
!= TPACPI_LED_OFF
) << led
);
5096 case TPACPI_LED_NEW
:
5098 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
5100 if (unlikely(tpacpi_is_led_restricted(led
)))
5102 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5103 led
, led_led_arg1
[ledstatus
]))
5111 tpacpi_led_state_cache
[led
] = ledstatus
;
5116 static void led_set_status_worker(struct work_struct
*work
)
5118 struct tpacpi_led_classdev
*data
=
5119 container_of(work
, struct tpacpi_led_classdev
, work
);
5121 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
5122 led_set_status(data
->led
, data
->new_state
);
5125 static void led_sysfs_set(struct led_classdev
*led_cdev
,
5126 enum led_brightness brightness
)
5128 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5129 struct tpacpi_led_classdev
, led_classdev
);
5131 if (brightness
== LED_OFF
)
5132 data
->new_state
= TPACPI_LED_OFF
;
5133 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
5134 data
->new_state
= TPACPI_LED_ON
;
5136 data
->new_state
= TPACPI_LED_BLINK
;
5138 queue_work(tpacpi_wq
, &data
->work
);
5141 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
5142 unsigned long *delay_on
, unsigned long *delay_off
)
5144 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5145 struct tpacpi_led_classdev
, led_classdev
);
5147 /* Can we choose the flash rate? */
5148 if (*delay_on
== 0 && *delay_off
== 0) {
5149 /* yes. set them to the hardware blink rate (1 Hz) */
5150 *delay_on
= 500; /* ms */
5151 *delay_off
= 500; /* ms */
5152 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
5155 data
->new_state
= TPACPI_LED_BLINK
;
5156 queue_work(tpacpi_wq
, &data
->work
);
5161 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
5165 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5166 struct tpacpi_led_classdev
, led_classdev
);
5168 rc
= led_get_status(data
->led
);
5170 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
5171 rc
= LED_OFF
; /* no error handling in led class :( */
5178 static void led_exit(void)
5182 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5183 if (tpacpi_leds
[i
].led_classdev
.name
)
5184 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
5190 static int __init
tpacpi_init_led(unsigned int led
)
5194 tpacpi_leds
[led
].led
= led
;
5196 /* LEDs with no name don't get registered */
5197 if (!tpacpi_led_names
[led
])
5200 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
5201 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
5202 if (led_supported
== TPACPI_LED_570
)
5203 tpacpi_leds
[led
].led_classdev
.brightness_get
=
5206 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
5208 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
5210 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5211 &tpacpi_leds
[led
].led_classdev
);
5213 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
5218 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
5219 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5220 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5221 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5223 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5224 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5225 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5226 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5227 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5228 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5229 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5230 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5232 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5233 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5234 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5235 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5236 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5238 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5239 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5240 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5241 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5243 /* (1) - may have excess leds enabled on MSB */
5245 /* Defaults (order matters, keep last, don't reorder!) */
5247 .vendor
= PCI_VENDOR_ID_LENOVO
,
5248 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5251 { /* IBM ThinkPads with no EC version string */
5252 .vendor
= PCI_VENDOR_ID_IBM
,
5253 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
5256 { /* IBM ThinkPads with EC version string */
5257 .vendor
= PCI_VENDOR_ID_IBM
,
5258 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5263 #undef TPACPI_LEDQ_IBM
5264 #undef TPACPI_LEDQ_LNV
5266 static enum led_access_mode __init
led_init_detect_mode(void)
5270 if (tpacpi_is_ibm()) {
5272 status
= acpi_get_handle(ec_handle
, "SLED", &led_handle
);
5273 if (ACPI_SUCCESS(status
))
5274 return TPACPI_LED_570
;
5276 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5277 status
= acpi_get_handle(ec_handle
, "SYSL", &led_handle
);
5278 if (ACPI_SUCCESS(status
))
5279 return TPACPI_LED_OLD
;
5283 status
= acpi_get_handle(ec_handle
, "LED", &led_handle
);
5284 if (ACPI_SUCCESS(status
))
5285 return TPACPI_LED_NEW
;
5287 /* R30, R31, and unknown firmwares */
5289 return TPACPI_LED_NONE
;
5292 static int __init
led_init(struct ibm_init_struct
*iibm
)
5296 unsigned long useful_leds
;
5298 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
5300 led_supported
= led_init_detect_mode();
5302 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
5303 str_supported(led_supported
), led_supported
);
5305 if (led_supported
== TPACPI_LED_NONE
)
5308 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
5311 pr_err("Out of memory for LED data\n");
5315 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
5316 ARRAY_SIZE(led_useful_qtable
));
5318 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5319 if (!tpacpi_is_led_restricted(i
) &&
5320 test_bit(i
, &useful_leds
)) {
5321 rc
= tpacpi_init_led(i
);
5329 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5330 pr_notice("warning: userspace override of important "
5331 "firmware LEDs is enabled\n");
5336 #define str_led_status(s) \
5337 ((s) == TPACPI_LED_OFF ? "off" : \
5338 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5340 static int led_read(struct seq_file
*m
)
5342 if (!led_supported
) {
5343 seq_printf(m
, "status:\t\tnot supported\n");
5346 seq_printf(m
, "status:\t\tsupported\n");
5348 if (led_supported
== TPACPI_LED_570
) {
5351 for (i
= 0; i
< 8; i
++) {
5352 status
= led_get_status(i
);
5355 seq_printf(m
, "%d:\t\t%s\n",
5356 i
, str_led_status(status
));
5360 seq_printf(m
, "commands:\t"
5361 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5366 static int led_write(char *buf
)
5370 enum led_status_t s
;
5375 while ((cmd
= next_cmd(&buf
))) {
5376 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 15)
5379 if (strstr(cmd
, "off")) {
5381 } else if (strstr(cmd
, "on")) {
5383 } else if (strstr(cmd
, "blink")) {
5384 s
= TPACPI_LED_BLINK
;
5389 rc
= led_set_status(led
, s
);
5397 static struct ibm_struct led_driver_data
= {
5404 /*************************************************************************
5408 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5410 #define TPACPI_BEEP_Q1 0x0001
5412 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
5413 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
5414 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
5417 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5419 unsigned long quirks
;
5421 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5423 TPACPI_ACPIHANDLE_INIT(beep
);
5425 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5426 str_supported(beep_handle
!= NULL
));
5428 quirks
= tpacpi_check_quirks(beep_quirk_table
,
5429 ARRAY_SIZE(beep_quirk_table
));
5431 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
5433 return (beep_handle
)? 0 : 1;
5436 static int beep_read(struct seq_file
*m
)
5439 seq_printf(m
, "status:\t\tnot supported\n");
5441 seq_printf(m
, "status:\t\tsupported\n");
5442 seq_printf(m
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5448 static int beep_write(char *buf
)
5456 while ((cmd
= next_cmd(&buf
))) {
5457 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5458 beep_cmd
>= 0 && beep_cmd
<= 17) {
5462 if (tp_features
.beep_needs_two_args
) {
5463 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
5467 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
5476 static struct ibm_struct beep_driver_data
= {
5479 .write
= beep_write
,
5482 /*************************************************************************
5486 enum thermal_access_mode
{
5487 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5488 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5489 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5490 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5491 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5494 enum { /* TPACPI_THERMAL_TPEC_* */
5495 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5496 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5497 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5499 TPACPI_THERMAL_SENSOR_NA
= -128000, /* Sensor not available */
5503 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5504 struct ibm_thermal_sensors_struct
{
5505 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5508 static enum thermal_access_mode thermal_read_mode
;
5510 /* idx is zero-based */
5511 static int thermal_get_sensor(int idx
, s32
*value
)
5517 t
= TP_EC_THERMAL_TMP0
;
5519 switch (thermal_read_mode
) {
5520 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5521 case TPACPI_THERMAL_TPEC_16
:
5522 if (idx
>= 8 && idx
<= 15) {
5523 t
= TP_EC_THERMAL_TMP8
;
5528 case TPACPI_THERMAL_TPEC_8
:
5530 if (!acpi_ec_read(t
+ idx
, &tmp
))
5532 *value
= tmp
* 1000;
5537 case TPACPI_THERMAL_ACPI_UPDT
:
5539 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5540 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5542 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5544 *value
= (t
- 2732) * 100;
5549 case TPACPI_THERMAL_ACPI_TMP07
:
5551 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5552 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5554 if (t
> 127 || t
< -127)
5555 t
= TP_EC_THERMAL_TMP_NA
;
5561 case TPACPI_THERMAL_NONE
:
5569 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5580 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5583 for (i
= 0 ; i
< n
; i
++) {
5584 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5592 static void thermal_dump_all_sensors(void)
5595 struct ibm_thermal_sensors_struct t
;
5597 n
= thermal_get_sensors(&t
);
5601 pr_notice("temperatures (Celsius):");
5603 for (i
= 0; i
< n
; i
++) {
5604 if (t
.temp
[i
] != TPACPI_THERMAL_SENSOR_NA
)
5605 pr_cont(" %d", (int)(t
.temp
[i
] / 1000));
5613 /* sysfs temp##_input -------------------------------------------------- */
5615 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5616 struct device_attribute
*attr
,
5619 struct sensor_device_attribute
*sensor_attr
=
5620 to_sensor_dev_attr(attr
);
5621 int idx
= sensor_attr
->index
;
5625 res
= thermal_get_sensor(idx
, &value
);
5628 if (value
== TPACPI_THERMAL_SENSOR_NA
)
5631 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5634 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5635 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5636 thermal_temp_input_show, NULL, _idxB)
5638 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5639 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5640 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5641 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5642 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5643 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5644 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5645 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5646 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5647 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5648 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5649 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5650 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5651 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5652 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5653 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5654 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5657 #define THERMAL_ATTRS(X) \
5658 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5660 static struct attribute
*thermal_temp_input_attr
[] = {
5680 static const struct attribute_group thermal_temp_input16_group
= {
5681 .attrs
= thermal_temp_input_attr
5684 static const struct attribute_group thermal_temp_input8_group
= {
5685 .attrs
= &thermal_temp_input_attr
[8]
5688 #undef THERMAL_SENSOR_ATTR_TEMP
5689 #undef THERMAL_ATTRS
5691 /* --------------------------------------------------------------------- */
5693 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5700 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5702 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5704 if (thinkpad_id
.ec_model
) {
5706 * Direct EC access mode: sensors at registers
5707 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5708 * non-implemented, thermal sensors return 0x80 when
5713 for (i
= 0; i
< 8; i
++) {
5714 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5720 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5728 /* This is sheer paranoia, but we handle it anyway */
5730 pr_err("ThinkPad ACPI EC access misbehaving, "
5731 "falling back to ACPI TMPx access "
5733 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5735 pr_err("ThinkPad ACPI EC access misbehaving, "
5736 "disabling thermal sensors access\n");
5737 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5742 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5744 } else if (acpi_tmp7
) {
5745 if (tpacpi_is_ibm() &&
5746 acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5747 /* 600e/x, 770e, 770x */
5748 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5750 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5751 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5754 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5755 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5758 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5759 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5762 switch (thermal_read_mode
) {
5763 case TPACPI_THERMAL_TPEC_16
:
5764 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5765 &thermal_temp_input16_group
);
5769 case TPACPI_THERMAL_TPEC_8
:
5770 case TPACPI_THERMAL_ACPI_TMP07
:
5771 case TPACPI_THERMAL_ACPI_UPDT
:
5772 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5773 &thermal_temp_input8_group
);
5777 case TPACPI_THERMAL_NONE
:
5785 static void thermal_exit(void)
5787 switch (thermal_read_mode
) {
5788 case TPACPI_THERMAL_TPEC_16
:
5789 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5790 &thermal_temp_input16_group
);
5792 case TPACPI_THERMAL_TPEC_8
:
5793 case TPACPI_THERMAL_ACPI_TMP07
:
5794 case TPACPI_THERMAL_ACPI_UPDT
:
5795 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5796 &thermal_temp_input8_group
);
5798 case TPACPI_THERMAL_NONE
:
5804 static int thermal_read(struct seq_file
*m
)
5807 struct ibm_thermal_sensors_struct t
;
5809 n
= thermal_get_sensors(&t
);
5810 if (unlikely(n
< 0))
5813 seq_printf(m
, "temperatures:\t");
5816 for (i
= 0; i
< (n
- 1); i
++)
5817 seq_printf(m
, "%d ", t
.temp
[i
] / 1000);
5818 seq_printf(m
, "%d\n", t
.temp
[i
] / 1000);
5820 seq_printf(m
, "not supported\n");
5825 static struct ibm_struct thermal_driver_data
= {
5827 .read
= thermal_read
,
5828 .exit
= thermal_exit
,
5831 /*************************************************************************
5832 * Backlight/brightness subdriver
5835 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5838 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5839 * CMOS NVRAM byte 0x5E, bits 0-3.
5841 * EC HBRV (0x31) has the following layout
5842 * Bit 7: unknown function
5843 * Bit 6: unknown function
5844 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5845 * Bit 4: must be set to zero to avoid problems
5846 * Bit 3-0: backlight brightness level
5848 * brightness_get_raw returns status data in the HBRV layout
5850 * WARNING: The X61 has been verified to use HBRV for something else, so
5851 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5852 * testing on the very early *60 Lenovo models...
5856 TP_EC_BACKLIGHT
= 0x31,
5858 /* TP_EC_BACKLIGHT bitmasks */
5859 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5860 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5861 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5864 enum tpacpi_brightness_access_mode
{
5865 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
5866 TPACPI_BRGHT_MODE_EC
, /* EC control */
5867 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
5868 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
5869 TPACPI_BRGHT_MODE_MAX
5872 static struct backlight_device
*ibm_backlight_device
;
5874 static enum tpacpi_brightness_access_mode brightness_mode
=
5875 TPACPI_BRGHT_MODE_MAX
;
5877 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5879 static struct mutex brightness_mutex
;
5881 /* NVRAM brightness access,
5882 * call with brightness_mutex held! */
5883 static unsigned int tpacpi_brightness_nvram_get(void)
5887 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5888 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5889 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5890 lnvram
&= bright_maxlvl
;
5895 static void tpacpi_brightness_checkpoint_nvram(void)
5900 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
5903 vdbg_printk(TPACPI_DBG_BRGHT
,
5904 "trying to checkpoint backlight level to NVRAM...\n");
5906 if (mutex_lock_killable(&brightness_mutex
) < 0)
5909 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5911 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5912 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
5914 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5915 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
5916 /* NVRAM needs update */
5917 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
5918 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
5920 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
5921 dbg_printk(TPACPI_DBG_BRGHT
,
5922 "updated NVRAM backlight level to %u (0x%02x)\n",
5923 (unsigned int) lec
, (unsigned int) b_nvram
);
5925 vdbg_printk(TPACPI_DBG_BRGHT
,
5926 "NVRAM backlight level already is %u (0x%02x)\n",
5927 (unsigned int) lec
, (unsigned int) b_nvram
);
5930 mutex_unlock(&brightness_mutex
);
5934 /* call with brightness_mutex held! */
5935 static int tpacpi_brightness_get_raw(int *status
)
5939 switch (brightness_mode
) {
5940 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5941 *status
= tpacpi_brightness_nvram_get();
5943 case TPACPI_BRGHT_MODE_EC
:
5944 case TPACPI_BRGHT_MODE_ECNVRAM
:
5945 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5954 /* call with brightness_mutex held! */
5955 /* do NOT call with illegal backlight level value */
5956 static int tpacpi_brightness_set_ec(unsigned int value
)
5960 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5963 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
5964 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
5965 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
5971 /* call with brightness_mutex held! */
5972 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
5975 unsigned int current_value
, i
;
5977 current_value
= tpacpi_brightness_nvram_get();
5979 if (value
== current_value
)
5982 cmos_cmd
= (value
> current_value
) ?
5983 TP_CMOS_BRIGHTNESS_UP
:
5984 TP_CMOS_BRIGHTNESS_DOWN
;
5985 inc
= (value
> current_value
) ? 1 : -1;
5987 for (i
= current_value
; i
!= value
; i
+= inc
)
5988 if (issue_thinkpad_cmos_command(cmos_cmd
))
5994 /* May return EINTR which can always be mapped to ERESTARTSYS */
5995 static int brightness_set(unsigned int value
)
5999 if (value
> bright_maxlvl
|| value
< 0)
6002 vdbg_printk(TPACPI_DBG_BRGHT
,
6003 "set backlight level to %d\n", value
);
6005 res
= mutex_lock_killable(&brightness_mutex
);
6009 switch (brightness_mode
) {
6010 case TPACPI_BRGHT_MODE_EC
:
6011 case TPACPI_BRGHT_MODE_ECNVRAM
:
6012 res
= tpacpi_brightness_set_ec(value
);
6014 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6015 res
= tpacpi_brightness_set_ucmsstep(value
);
6021 mutex_unlock(&brightness_mutex
);
6025 /* sysfs backlight class ----------------------------------------------- */
6027 static int brightness_update_status(struct backlight_device
*bd
)
6029 unsigned int level
=
6030 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
6031 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
6032 bd
->props
.brightness
: 0;
6034 dbg_printk(TPACPI_DBG_BRGHT
,
6035 "backlight: attempt to set level to %d\n",
6038 /* it is the backlight class's job (caller) to handle
6039 * EINTR and other errors properly */
6040 return brightness_set(level
);
6043 static int brightness_get(struct backlight_device
*bd
)
6047 res
= mutex_lock_killable(&brightness_mutex
);
6051 res
= tpacpi_brightness_get_raw(&status
);
6053 mutex_unlock(&brightness_mutex
);
6058 return status
& TP_EC_BACKLIGHT_LVLMSK
;
6061 static void tpacpi_brightness_notify_change(void)
6063 backlight_force_update(ibm_backlight_device
,
6064 BACKLIGHT_UPDATE_HOTKEY
);
6067 static const struct backlight_ops ibm_backlight_data
= {
6068 .get_brightness
= brightness_get
,
6069 .update_status
= brightness_update_status
,
6072 /* --------------------------------------------------------------------- */
6075 * Call _BCL method of video device. On some ThinkPads this will
6076 * switch the firmware to the ACPI brightness control mode.
6079 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
6081 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
6082 union acpi_object
*obj
;
6085 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, "_BCL", NULL
, &buffer
))) {
6086 obj
= (union acpi_object
*)buffer
.pointer
;
6087 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
6088 pr_err("Unknown _BCL data, please report this to %s\n",
6092 rc
= obj
->package
.count
;
6098 kfree(buffer
.pointer
);
6104 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6106 static unsigned int __init
tpacpi_check_std_acpi_brightness_support(void)
6108 acpi_handle video_device
;
6111 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID
, &video_device
);
6113 bcl_levels
= tpacpi_query_bcl_levels(video_device
);
6115 tp_features
.bright_acpimode
= (bcl_levels
> 0);
6117 return (bcl_levels
> 2) ? (bcl_levels
- 2) : 0;
6121 * These are only useful for models that have only one possibility
6122 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6125 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6126 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6127 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6129 static const struct tpacpi_quirk brightness_quirk_table
[] __initconst
= {
6130 /* Models with ATI GPUs known to require ECNVRAM mode */
6131 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC
), /* T43/p ATI */
6133 /* Models with ATI GPUs that can use ECNVRAM */
6134 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC
), /* R50,51 T40-42 */
6135 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6136 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC
), /* R52 */
6137 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6139 /* Models with Intel Extreme Graphics 2 */
6140 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC
), /* X40 */
6141 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6142 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6144 /* Models with Intel GMA900 */
6145 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC
), /* T43, R52 */
6146 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC
), /* X41 */
6147 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC
), /* X41 Tablet */
6151 * Returns < 0 for error, otherwise sets tp_features.bright_*
6152 * and bright_maxlvl.
6154 static void __init
tpacpi_detect_brightness_capabilities(void)
6158 vdbg_printk(TPACPI_DBG_INIT
,
6159 "detecting firmware brightness interface capabilities\n");
6161 /* we could run a quirks check here (same table used by
6162 * brightness_init) if needed */
6165 * We always attempt to detect acpi support, so as to switch
6166 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6167 * going to publish a backlight interface
6169 b
= tpacpi_check_std_acpi_brightness_support();
6173 pr_info("detected a 16-level brightness capable ThinkPad\n");
6178 pr_info("detected a 8-level brightness capable ThinkPad\n");
6181 pr_err("Unsupported brightness interface, "
6182 "please contact %s\n", TPACPI_MAIL
);
6183 tp_features
.bright_unkfw
= 1;
6184 bright_maxlvl
= b
- 1;
6188 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
6190 struct backlight_properties props
;
6192 unsigned long quirks
;
6194 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
6196 mutex_init(&brightness_mutex
);
6198 quirks
= tpacpi_check_quirks(brightness_quirk_table
,
6199 ARRAY_SIZE(brightness_quirk_table
));
6201 /* tpacpi_detect_brightness_capabilities() must have run already */
6203 /* if it is unknown, we don't handle it: it wouldn't be safe */
6204 if (tp_features
.bright_unkfw
)
6207 if (!brightness_enable
) {
6208 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6209 "brightness support disabled by "
6210 "module parameter\n");
6214 if (acpi_video_backlight_support()) {
6215 if (brightness_enable
> 1) {
6216 pr_info("Standard ACPI backlight interface "
6217 "available, not loading native one\n");
6219 } else if (brightness_enable
== 1) {
6220 pr_warn("Cannot enable backlight brightness support, "
6221 "ACPI is already handling it. Refer to the "
6222 "acpi_backlight kernel parameter.\n");
6225 } else if (tp_features
.bright_acpimode
&& brightness_enable
> 1) {
6226 pr_notice("Standard ACPI backlight interface not "
6227 "available, thinkpad_acpi native "
6228 "brightness control enabled\n");
6232 * Check for module parameter bogosity, note that we
6233 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6234 * able to detect "unspecified"
6236 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
6239 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6240 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
6241 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
6242 if (quirks
& TPACPI_BRGHT_Q_EC
)
6243 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
6245 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
6247 dbg_printk(TPACPI_DBG_BRGHT
,
6248 "driver auto-selected brightness_mode=%d\n",
6253 if (!tpacpi_is_ibm() &&
6254 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
6255 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
6258 if (tpacpi_brightness_get_raw(&b
) < 0)
6261 memset(&props
, 0, sizeof(struct backlight_properties
));
6262 props
.type
= BACKLIGHT_PLATFORM
;
6263 props
.max_brightness
= bright_maxlvl
;
6264 props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
6265 ibm_backlight_device
= backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME
,
6267 &ibm_backlight_data
,
6269 if (IS_ERR(ibm_backlight_device
)) {
6270 int rc
= PTR_ERR(ibm_backlight_device
);
6271 ibm_backlight_device
= NULL
;
6272 pr_err("Could not register backlight device\n");
6275 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6276 "brightness is supported\n");
6278 if (quirks
& TPACPI_BRGHT_Q_ASK
) {
6279 pr_notice("brightness: will use unverified default: "
6280 "brightness_mode=%d\n", brightness_mode
);
6281 pr_notice("brightness: please report to %s whether it works well "
6282 "or not on your ThinkPad\n", TPACPI_MAIL
);
6285 /* Added by mistake in early 2007. Probably useless, but it could
6286 * be working around some unknown firmware problem where the value
6287 * read at startup doesn't match the real hardware state... so leave
6288 * it in place just in case */
6289 backlight_update_status(ibm_backlight_device
);
6291 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6292 "brightness: registering brightness hotkeys "
6293 "as change notification\n");
6294 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6295 | TP_ACPI_HKEY_BRGHTUP_MASK
6296 | TP_ACPI_HKEY_BRGHTDWN_MASK
);
6300 static void brightness_suspend(pm_message_t state
)
6302 tpacpi_brightness_checkpoint_nvram();
6305 static void brightness_shutdown(void)
6307 tpacpi_brightness_checkpoint_nvram();
6310 static void brightness_exit(void)
6312 if (ibm_backlight_device
) {
6313 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
6314 "calling backlight_device_unregister()\n");
6315 backlight_device_unregister(ibm_backlight_device
);
6318 tpacpi_brightness_checkpoint_nvram();
6321 static int brightness_read(struct seq_file
*m
)
6325 level
= brightness_get(NULL
);
6327 seq_printf(m
, "level:\t\tunreadable\n");
6329 seq_printf(m
, "level:\t\t%d\n", level
);
6330 seq_printf(m
, "commands:\tup, down\n");
6331 seq_printf(m
, "commands:\tlevel <level> (<level> is 0-%d)\n",
6338 static int brightness_write(char *buf
)
6344 level
= brightness_get(NULL
);
6348 while ((cmd
= next_cmd(&buf
))) {
6349 if (strlencmp(cmd
, "up") == 0) {
6350 if (level
< bright_maxlvl
)
6352 } else if (strlencmp(cmd
, "down") == 0) {
6355 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
6356 level
>= 0 && level
<= bright_maxlvl
) {
6362 tpacpi_disclose_usertask("procfs brightness",
6363 "set level to %d\n", level
);
6366 * Now we know what the final level should be, so we try to set it.
6367 * Doing it this way makes the syscall restartable in case of EINTR
6369 rc
= brightness_set(level
);
6370 if (!rc
&& ibm_backlight_device
)
6371 backlight_force_update(ibm_backlight_device
,
6372 BACKLIGHT_UPDATE_SYSFS
);
6373 return (rc
== -EINTR
)? -ERESTARTSYS
: rc
;
6376 static struct ibm_struct brightness_driver_data
= {
6377 .name
= "brightness",
6378 .read
= brightness_read
,
6379 .write
= brightness_write
,
6380 .exit
= brightness_exit
,
6381 .suspend
= brightness_suspend
,
6382 .shutdown
= brightness_shutdown
,
6385 /*************************************************************************
6390 * IBM ThinkPads have a simple volume controller with MUTE gating.
6391 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6393 * Since the *61 series (and probably also the later *60 series), Lenovo
6394 * ThinkPads only implement the MUTE gate.
6397 * Bit 6: MUTE (1 mutes sound)
6399 * Other bits should be zero as far as we know.
6401 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6402 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6403 * such as bit 7 which is used to detect repeated presses of MUTE,
6404 * and we leave them unchanged.
6407 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6409 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6410 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6411 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6413 static int alsa_index
= ~((1 << (SNDRV_CARDS
- 3)) - 1); /* last three slots */
6414 static char *alsa_id
= "ThinkPadEC";
6415 static int alsa_enable
= SNDRV_DEFAULT_ENABLE1
;
6417 struct tpacpi_alsa_data
{
6418 struct snd_card
*card
;
6419 struct snd_ctl_elem_id
*ctl_mute_id
;
6420 struct snd_ctl_elem_id
*ctl_vol_id
;
6423 static struct snd_card
*alsa_card
;
6428 /* TP_EC_AUDIO bits */
6429 TP_EC_AUDIO_MUTESW
= 6,
6431 /* TP_EC_AUDIO bitmasks */
6432 TP_EC_AUDIO_LVL_MSK
= 0x0F,
6433 TP_EC_AUDIO_MUTESW_MSK
= (1 << TP_EC_AUDIO_MUTESW
),
6435 /* Maximum volume */
6436 TP_EC_VOLUME_MAX
= 14,
6439 enum tpacpi_volume_access_mode
{
6440 TPACPI_VOL_MODE_AUTO
= 0, /* Not implemented yet */
6441 TPACPI_VOL_MODE_EC
, /* Pure EC control */
6442 TPACPI_VOL_MODE_UCMS_STEP
, /* UCMS step-based control: N/A */
6443 TPACPI_VOL_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
6447 enum tpacpi_volume_capabilities
{
6448 TPACPI_VOL_CAP_AUTO
= 0, /* Use white/blacklist */
6449 TPACPI_VOL_CAP_VOLMUTE
, /* Output vol and mute */
6450 TPACPI_VOL_CAP_MUTEONLY
, /* Output mute only */
6454 static enum tpacpi_volume_access_mode volume_mode
=
6455 TPACPI_VOL_MODE_MAX
;
6457 static enum tpacpi_volume_capabilities volume_capabilities
;
6458 static int volume_control_allowed
;
6461 * Used to syncronize writers to TP_EC_AUDIO and
6462 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6464 static struct mutex volume_mutex
;
6466 static void tpacpi_volume_checkpoint_nvram(void)
6472 if (volume_mode
!= TPACPI_VOL_MODE_ECNVRAM
)
6474 if (!volume_control_allowed
)
6477 vdbg_printk(TPACPI_DBG_MIXER
,
6478 "trying to checkpoint mixer state to NVRAM...\n");
6480 if (tp_features
.mixer_no_level_control
)
6481 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
;
6483 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
| TP_EC_AUDIO_LVL_MSK
;
6485 if (mutex_lock_killable(&volume_mutex
) < 0)
6488 if (unlikely(!acpi_ec_read(TP_EC_AUDIO
, &lec
)))
6491 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
6493 if (lec
!= (b_nvram
& ec_mask
)) {
6494 /* NVRAM needs update */
6495 b_nvram
&= ~ec_mask
;
6497 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_MIXER
);
6498 dbg_printk(TPACPI_DBG_MIXER
,
6499 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6500 (unsigned int) lec
, (unsigned int) b_nvram
);
6502 vdbg_printk(TPACPI_DBG_MIXER
,
6503 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6504 (unsigned int) lec
, (unsigned int) b_nvram
);
6508 mutex_unlock(&volume_mutex
);
6511 static int volume_get_status_ec(u8
*status
)
6515 if (!acpi_ec_read(TP_EC_AUDIO
, &s
))
6520 dbg_printk(TPACPI_DBG_MIXER
, "status 0x%02x\n", s
);
6525 static int volume_get_status(u8
*status
)
6527 return volume_get_status_ec(status
);
6530 static int volume_set_status_ec(const u8 status
)
6532 if (!acpi_ec_write(TP_EC_AUDIO
, status
))
6535 dbg_printk(TPACPI_DBG_MIXER
, "set EC mixer to 0x%02x\n", status
);
6540 static int volume_set_status(const u8 status
)
6542 return volume_set_status_ec(status
);
6545 /* returns < 0 on error, 0 on no change, 1 on change */
6546 static int __volume_set_mute_ec(const bool mute
)
6551 if (mutex_lock_killable(&volume_mutex
) < 0)
6554 rc
= volume_get_status_ec(&s
);
6558 n
= (mute
) ? s
| TP_EC_AUDIO_MUTESW_MSK
:
6559 s
& ~TP_EC_AUDIO_MUTESW_MSK
;
6562 rc
= volume_set_status_ec(n
);
6568 mutex_unlock(&volume_mutex
);
6572 static int volume_alsa_set_mute(const bool mute
)
6574 dbg_printk(TPACPI_DBG_MIXER
, "ALSA: trying to %smute\n",
6575 (mute
) ? "" : "un");
6576 return __volume_set_mute_ec(mute
);
6579 static int volume_set_mute(const bool mute
)
6583 dbg_printk(TPACPI_DBG_MIXER
, "trying to %smute\n",
6584 (mute
) ? "" : "un");
6586 rc
= __volume_set_mute_ec(mute
);
6587 return (rc
< 0) ? rc
: 0;
6590 /* returns < 0 on error, 0 on no change, 1 on change */
6591 static int __volume_set_volume_ec(const u8 vol
)
6596 if (vol
> TP_EC_VOLUME_MAX
)
6599 if (mutex_lock_killable(&volume_mutex
) < 0)
6602 rc
= volume_get_status_ec(&s
);
6606 n
= (s
& ~TP_EC_AUDIO_LVL_MSK
) | vol
;
6609 rc
= volume_set_status_ec(n
);
6615 mutex_unlock(&volume_mutex
);
6619 static int volume_alsa_set_volume(const u8 vol
)
6621 dbg_printk(TPACPI_DBG_MIXER
,
6622 "ALSA: trying to set volume level to %hu\n", vol
);
6623 return __volume_set_volume_ec(vol
);
6626 static void volume_alsa_notify_change(void)
6628 struct tpacpi_alsa_data
*d
;
6630 if (alsa_card
&& alsa_card
->private_data
) {
6631 d
= alsa_card
->private_data
;
6633 snd_ctl_notify(alsa_card
,
6634 SNDRV_CTL_EVENT_MASK_VALUE
,
6637 snd_ctl_notify(alsa_card
,
6638 SNDRV_CTL_EVENT_MASK_VALUE
,
6643 static int volume_alsa_vol_info(struct snd_kcontrol
*kcontrol
,
6644 struct snd_ctl_elem_info
*uinfo
)
6646 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
6648 uinfo
->value
.integer
.min
= 0;
6649 uinfo
->value
.integer
.max
= TP_EC_VOLUME_MAX
;
6653 static int volume_alsa_vol_get(struct snd_kcontrol
*kcontrol
,
6654 struct snd_ctl_elem_value
*ucontrol
)
6659 rc
= volume_get_status(&s
);
6663 ucontrol
->value
.integer
.value
[0] = s
& TP_EC_AUDIO_LVL_MSK
;
6667 static int volume_alsa_vol_put(struct snd_kcontrol
*kcontrol
,
6668 struct snd_ctl_elem_value
*ucontrol
)
6670 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6671 ucontrol
->value
.integer
.value
[0]);
6672 return volume_alsa_set_volume(ucontrol
->value
.integer
.value
[0]);
6675 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6677 static int volume_alsa_mute_get(struct snd_kcontrol
*kcontrol
,
6678 struct snd_ctl_elem_value
*ucontrol
)
6683 rc
= volume_get_status(&s
);
6687 ucontrol
->value
.integer
.value
[0] =
6688 (s
& TP_EC_AUDIO_MUTESW_MSK
) ? 0 : 1;
6692 static int volume_alsa_mute_put(struct snd_kcontrol
*kcontrol
,
6693 struct snd_ctl_elem_value
*ucontrol
)
6695 tpacpi_disclose_usertask("ALSA", "%smute\n",
6696 ucontrol
->value
.integer
.value
[0] ?
6698 return volume_alsa_set_mute(!ucontrol
->value
.integer
.value
[0]);
6701 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata
= {
6702 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
6703 .name
= "Console Playback Volume",
6705 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
6706 .info
= volume_alsa_vol_info
,
6707 .get
= volume_alsa_vol_get
,
6710 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata
= {
6711 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
6712 .name
= "Console Playback Switch",
6714 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
6715 .info
= volume_alsa_mute_info
,
6716 .get
= volume_alsa_mute_get
,
6719 static void volume_suspend(pm_message_t state
)
6721 tpacpi_volume_checkpoint_nvram();
6724 static void volume_resume(void)
6726 volume_alsa_notify_change();
6729 static void volume_shutdown(void)
6731 tpacpi_volume_checkpoint_nvram();
6734 static void volume_exit(void)
6737 snd_card_free(alsa_card
);
6741 tpacpi_volume_checkpoint_nvram();
6744 static int __init
volume_create_alsa_mixer(void)
6746 struct snd_card
*card
;
6747 struct tpacpi_alsa_data
*data
;
6748 struct snd_kcontrol
*ctl_vol
;
6749 struct snd_kcontrol
*ctl_mute
;
6752 rc
= snd_card_create(alsa_index
, alsa_id
, THIS_MODULE
,
6753 sizeof(struct tpacpi_alsa_data
), &card
);
6754 if (rc
< 0 || !card
) {
6755 pr_err("Failed to create ALSA card structures: %d\n", rc
);
6759 BUG_ON(!card
->private_data
);
6760 data
= card
->private_data
;
6763 strlcpy(card
->driver
, TPACPI_ALSA_DRVNAME
,
6764 sizeof(card
->driver
));
6765 strlcpy(card
->shortname
, TPACPI_ALSA_SHRTNAME
,
6766 sizeof(card
->shortname
));
6767 snprintf(card
->mixername
, sizeof(card
->mixername
), "ThinkPad EC %s",
6768 (thinkpad_id
.ec_version_str
) ?
6769 thinkpad_id
.ec_version_str
: "(unknown)");
6770 snprintf(card
->longname
, sizeof(card
->longname
),
6771 "%s at EC reg 0x%02x, fw %s", card
->shortname
, TP_EC_AUDIO
,
6772 (thinkpad_id
.ec_version_str
) ?
6773 thinkpad_id
.ec_version_str
: "unknown");
6775 if (volume_control_allowed
) {
6776 volume_alsa_control_vol
.put
= volume_alsa_vol_put
;
6777 volume_alsa_control_vol
.access
=
6778 SNDRV_CTL_ELEM_ACCESS_READWRITE
;
6780 volume_alsa_control_mute
.put
= volume_alsa_mute_put
;
6781 volume_alsa_control_mute
.access
=
6782 SNDRV_CTL_ELEM_ACCESS_READWRITE
;
6785 if (!tp_features
.mixer_no_level_control
) {
6786 ctl_vol
= snd_ctl_new1(&volume_alsa_control_vol
, NULL
);
6787 rc
= snd_ctl_add(card
, ctl_vol
);
6789 pr_err("Failed to create ALSA volume control: %d\n",
6793 data
->ctl_vol_id
= &ctl_vol
->id
;
6796 ctl_mute
= snd_ctl_new1(&volume_alsa_control_mute
, NULL
);
6797 rc
= snd_ctl_add(card
, ctl_mute
);
6799 pr_err("Failed to create ALSA mute control: %d\n", rc
);
6802 data
->ctl_mute_id
= &ctl_mute
->id
;
6804 snd_card_set_dev(card
, &tpacpi_pdev
->dev
);
6805 rc
= snd_card_register(card
);
6807 pr_err("Failed to register ALSA card: %d\n", rc
);
6815 snd_card_free(card
);
6819 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6820 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6822 static const struct tpacpi_quirk volume_quirk_table
[] __initconst
= {
6823 /* Whitelist volume level on all IBM by default */
6824 { .vendor
= PCI_VENDOR_ID_IBM
,
6825 .bios
= TPACPI_MATCH_ANY
,
6826 .ec
= TPACPI_MATCH_ANY
,
6827 .quirks
= TPACPI_VOL_Q_LEVEL
},
6829 /* Lenovo models with volume control (needs confirmation) */
6830 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL
), /* R60/i */
6831 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL
), /* R60e/i */
6832 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL
), /* T60/p */
6833 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL
), /* X60/s */
6834 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL
), /* X60t */
6835 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL
), /* Z60 */
6836 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL
), /* Z61 */
6838 /* Whitelist mute-only on all Lenovo by default */
6839 { .vendor
= PCI_VENDOR_ID_LENOVO
,
6840 .bios
= TPACPI_MATCH_ANY
,
6841 .ec
= TPACPI_MATCH_ANY
,
6842 .quirks
= TPACPI_VOL_Q_MUTEONLY
}
6845 static int __init
volume_init(struct ibm_init_struct
*iibm
)
6847 unsigned long quirks
;
6850 vdbg_printk(TPACPI_DBG_INIT
, "initializing volume subdriver\n");
6852 mutex_init(&volume_mutex
);
6855 * Check for module parameter bogosity, note that we
6856 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6857 * able to detect "unspecified"
6859 if (volume_mode
> TPACPI_VOL_MODE_MAX
)
6862 if (volume_mode
== TPACPI_VOL_MODE_UCMS_STEP
) {
6863 pr_err("UCMS step volume mode not implemented, "
6864 "please contact %s\n", TPACPI_MAIL
);
6868 if (volume_capabilities
>= TPACPI_VOL_CAP_MAX
)
6872 * The ALSA mixer is our primary interface.
6873 * When disabled, don't install the subdriver at all
6876 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6877 "ALSA mixer disabled by parameter, "
6878 "not loading volume subdriver...\n");
6882 quirks
= tpacpi_check_quirks(volume_quirk_table
,
6883 ARRAY_SIZE(volume_quirk_table
));
6885 switch (volume_capabilities
) {
6886 case TPACPI_VOL_CAP_AUTO
:
6887 if (quirks
& TPACPI_VOL_Q_MUTEONLY
)
6888 tp_features
.mixer_no_level_control
= 1;
6889 else if (quirks
& TPACPI_VOL_Q_LEVEL
)
6890 tp_features
.mixer_no_level_control
= 0;
6892 return 1; /* no mixer */
6894 case TPACPI_VOL_CAP_VOLMUTE
:
6895 tp_features
.mixer_no_level_control
= 0;
6897 case TPACPI_VOL_CAP_MUTEONLY
:
6898 tp_features
.mixer_no_level_control
= 1;
6904 if (volume_capabilities
!= TPACPI_VOL_CAP_AUTO
)
6905 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6906 "using user-supplied volume_capabilities=%d\n",
6907 volume_capabilities
);
6909 if (volume_mode
== TPACPI_VOL_MODE_AUTO
||
6910 volume_mode
== TPACPI_VOL_MODE_MAX
) {
6911 volume_mode
= TPACPI_VOL_MODE_ECNVRAM
;
6913 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6914 "driver auto-selected volume_mode=%d\n",
6917 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6918 "using user-supplied volume_mode=%d\n",
6922 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6923 "mute is supported, volume control is %s\n",
6924 str_supported(!tp_features
.mixer_no_level_control
));
6926 rc
= volume_create_alsa_mixer();
6928 pr_err("Could not create the ALSA mixer interface\n");
6932 pr_info("Console audio control enabled, mode: %s\n",
6933 (volume_control_allowed
) ?
6934 "override (read/write)" :
6935 "monitor (read only)");
6937 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6938 "registering volume hotkeys as change notification\n");
6939 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6940 | TP_ACPI_HKEY_VOLUP_MASK
6941 | TP_ACPI_HKEY_VOLDWN_MASK
6942 | TP_ACPI_HKEY_MUTE_MASK
);
6947 static int volume_read(struct seq_file
*m
)
6951 if (volume_get_status(&status
) < 0) {
6952 seq_printf(m
, "level:\t\tunreadable\n");
6954 if (tp_features
.mixer_no_level_control
)
6955 seq_printf(m
, "level:\t\tunsupported\n");
6957 seq_printf(m
, "level:\t\t%d\n",
6958 status
& TP_EC_AUDIO_LVL_MSK
);
6960 seq_printf(m
, "mute:\t\t%s\n",
6961 onoff(status
, TP_EC_AUDIO_MUTESW
));
6963 if (volume_control_allowed
) {
6964 seq_printf(m
, "commands:\tunmute, mute\n");
6965 if (!tp_features
.mixer_no_level_control
) {
6967 "commands:\tup, down\n");
6969 "commands:\tlevel <level>"
6970 " (<level> is 0-%d)\n",
6979 static int volume_write(char *buf
)
6982 u8 new_level
, new_mute
;
6988 * We do allow volume control at driver startup, so that the
6989 * user can set initial state through the volume=... parameter hack.
6991 if (!volume_control_allowed
&& tpacpi_lifecycle
!= TPACPI_LIFE_INIT
) {
6992 if (unlikely(!tp_warned
.volume_ctrl_forbidden
)) {
6993 tp_warned
.volume_ctrl_forbidden
= 1;
6994 pr_notice("Console audio control in monitor mode, "
6995 "changes are not allowed\n");
6996 pr_notice("Use the volume_control=1 module parameter "
6997 "to enable volume control\n");
7002 rc
= volume_get_status(&s
);
7006 new_level
= s
& TP_EC_AUDIO_LVL_MSK
;
7007 new_mute
= s
& TP_EC_AUDIO_MUTESW_MSK
;
7009 while ((cmd
= next_cmd(&buf
))) {
7010 if (!tp_features
.mixer_no_level_control
) {
7011 if (strlencmp(cmd
, "up") == 0) {
7014 else if (new_level
< TP_EC_VOLUME_MAX
)
7017 } else if (strlencmp(cmd
, "down") == 0) {
7020 else if (new_level
> 0)
7023 } else if (sscanf(cmd
, "level %u", &l
) == 1 &&
7024 l
>= 0 && l
<= TP_EC_VOLUME_MAX
) {
7029 if (strlencmp(cmd
, "mute") == 0)
7030 new_mute
= TP_EC_AUDIO_MUTESW_MSK
;
7031 else if (strlencmp(cmd
, "unmute") == 0)
7037 if (tp_features
.mixer_no_level_control
) {
7038 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7039 new_mute
? "" : "un");
7040 rc
= volume_set_mute(!!new_mute
);
7042 tpacpi_disclose_usertask("procfs volume",
7043 "%smute and set level to %d\n",
7044 new_mute
? "" : "un", new_level
);
7045 rc
= volume_set_status(new_mute
| new_level
);
7047 volume_alsa_notify_change();
7049 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
7052 static struct ibm_struct volume_driver_data
= {
7054 .read
= volume_read
,
7055 .write
= volume_write
,
7056 .exit
= volume_exit
,
7057 .suspend
= volume_suspend
,
7058 .resume
= volume_resume
,
7059 .shutdown
= volume_shutdown
,
7062 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7064 #define alsa_card NULL
7066 static void inline volume_alsa_notify_change(void)
7070 static int __init
volume_init(struct ibm_init_struct
*iibm
)
7072 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7077 static struct ibm_struct volume_driver_data
= {
7081 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7083 /*************************************************************************
7090 * TPACPI_FAN_RD_ACPI_GFAN:
7091 * ACPI GFAN method: returns fan level
7093 * see TPACPI_FAN_WR_ACPI_SFAN
7094 * EC 0x2f (HFSP) not available if GFAN exists
7096 * TPACPI_FAN_WR_ACPI_SFAN:
7097 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7099 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7102 * TPACPI_FAN_WR_TPEC:
7103 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7104 * Supported on almost all ThinkPads
7106 * Fan speed changes of any sort (including those caused by the
7107 * disengaged mode) are usually done slowly by the firmware as the
7108 * maximum amount of fan duty cycle change per second seems to be
7111 * Reading is not available if GFAN exists.
7112 * Writing is not available if SFAN exists.
7115 * 7 automatic mode engaged;
7116 * (default operation mode of the ThinkPad)
7117 * fan level is ignored in this mode.
7118 * 6 full speed mode (takes precedence over bit 7);
7119 * not available on all thinkpads. May disable
7120 * the tachometer while the fan controller ramps up
7121 * the speed (which can take up to a few *minutes*).
7122 * Speeds up fan to 100% duty-cycle, which is far above
7123 * the standard RPM levels. It is not impossible that
7124 * it could cause hardware damage.
7125 * 5-3 unused in some models. Extra bits for fan level
7126 * in others, but still useless as all values above
7127 * 7 map to the same speed as level 7 in these models.
7128 * 2-0 fan level (0..7 usually)
7130 * 0x07 = max (set when temperatures critical)
7131 * Some ThinkPads may have other levels, see
7132 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7134 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7135 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7136 * does so, its initial value is meaningless (0x07).
7138 * For firmware bugs, refer to:
7139 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7143 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7144 * Main fan tachometer reading (in RPM)
7146 * This register is present on all ThinkPads with a new-style EC, and
7147 * it is known not to be present on the A21m/e, and T22, as there is
7148 * something else in offset 0x84 according to the ACPI DSDT. Other
7149 * ThinkPads from this same time period (and earlier) probably lack the
7150 * tachometer as well.
7152 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7153 * was never fixed by IBM to report the EC firmware version string
7154 * probably support the tachometer (like the early X models), so
7155 * detecting it is quite hard. We need more data to know for sure.
7157 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7160 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7163 * For firmware bugs, refer to:
7164 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7168 * ThinkPad EC register 0x31 bit 0 (only on select models)
7170 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7171 * show the speed of the main fan. When bit 0 of EC register 0x31
7172 * is one, the tachometer registers show the speed of the auxiliary
7175 * Fan control seems to affect both fans, regardless of the state
7178 * So far, only the firmware for the X60/X61 non-tablet versions
7179 * seem to support this (firmware TP-7M).
7181 * TPACPI_FAN_WR_ACPI_FANS:
7182 * ThinkPad X31, X40, X41. Not available in the X60.
7184 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7185 * high speed. ACPI DSDT seems to map these three speeds to levels
7186 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7187 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7189 * The speeds are stored on handles
7190 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7192 * There are three default speed sets, accessible as handles:
7193 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7195 * ACPI DSDT switches which set is in use depending on various
7198 * TPACPI_FAN_WR_TPEC is also available and should be used to
7199 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7200 * but the ACPI tables just mention level 7.
7203 enum { /* Fan control constants */
7204 fan_status_offset
= 0x2f, /* EC register 0x2f */
7205 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7206 * 0x84 must be read before 0x85 */
7207 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
7208 bit 0 selects which fan is active */
7210 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
7211 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
7213 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
7216 enum fan_status_access_mode
{
7217 TPACPI_FAN_NONE
= 0, /* No fan status or control */
7218 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
7219 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7222 enum fan_control_access_mode
{
7223 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
7224 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
7225 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
7226 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
7229 enum fan_control_commands
{
7230 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
7231 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
7232 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
7233 * and also watchdog cmd */
7236 static int fan_control_allowed
;
7238 static enum fan_status_access_mode fan_status_access_mode
;
7239 static enum fan_control_access_mode fan_control_access_mode
;
7240 static enum fan_control_commands fan_control_commands
;
7242 static u8 fan_control_initial_status
;
7243 static u8 fan_control_desired_level
;
7244 static u8 fan_control_resume_level
;
7245 static int fan_watchdog_maxinterval
;
7247 static struct mutex fan_mutex
;
7249 static void fan_watchdog_fire(struct work_struct
*ignored
);
7250 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
7252 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
7253 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
7254 "\\FSPD", /* 600e/x, 770e, 770x */
7256 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
7257 "JFNS", /* 770x-JL */
7261 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7262 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7263 * be in auto mode (0x80).
7265 * This is corrected by any write to HFSP either by the driver, or
7268 * We assume 0x07 really means auto mode while this quirk is active,
7269 * as this is far more likely than the ThinkPad being in level 7,
7270 * which is only used by the firmware during thermal emergencies.
7272 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7273 * TP-70 (T43, R52), which are known to be buggy.
7276 static void fan_quirk1_setup(void)
7278 if (fan_control_initial_status
== 0x07) {
7279 pr_notice("fan_init: initial fan status is unknown, "
7280 "assuming it is in auto mode\n");
7281 tp_features
.fan_ctrl_status_undef
= 1;
7285 static void fan_quirk1_handle(u8
*fan_status
)
7287 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
7288 if (*fan_status
!= fan_control_initial_status
) {
7289 /* something changed the HFSP regisnter since
7290 * driver init time, so it is not undefined
7292 tp_features
.fan_ctrl_status_undef
= 0;
7294 /* Return most likely status. In fact, it
7295 * might be the only possible status */
7296 *fan_status
= TP_EC_FAN_AUTO
;
7301 /* Select main fan on X60/X61, NOOP on others */
7302 static bool fan_select_fan1(void)
7304 if (tp_features
.second_fan
) {
7307 if (ec_read(fan_select_offset
, &val
) < 0)
7310 if (ec_write(fan_select_offset
, val
) < 0)
7316 /* Select secondary fan on X60/X61 */
7317 static bool fan_select_fan2(void)
7321 if (!tp_features
.second_fan
)
7324 if (ec_read(fan_select_offset
, &val
) < 0)
7327 if (ec_write(fan_select_offset
, val
) < 0)
7334 * Call with fan_mutex held
7336 static void fan_update_desired_level(u8 status
)
7339 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
7341 fan_control_desired_level
= 7;
7343 fan_control_desired_level
= status
;
7347 static int fan_get_status(u8
*status
)
7352 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7354 switch (fan_status_access_mode
) {
7355 case TPACPI_FAN_RD_ACPI_GFAN
:
7356 /* 570, 600e/x, 770e, 770x */
7358 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
7366 case TPACPI_FAN_RD_TPEC
:
7367 /* all except 570, 600e/x, 770e, 770x */
7368 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
7371 if (likely(status
)) {
7373 fan_quirk1_handle(status
);
7385 static int fan_get_status_safe(u8
*status
)
7390 if (mutex_lock_killable(&fan_mutex
))
7391 return -ERESTARTSYS
;
7392 rc
= fan_get_status(&s
);
7394 fan_update_desired_level(s
);
7395 mutex_unlock(&fan_mutex
);
7403 static int fan_get_speed(unsigned int *speed
)
7407 switch (fan_status_access_mode
) {
7408 case TPACPI_FAN_RD_TPEC
:
7409 /* all except 570, 600e/x, 770e, 770x */
7410 if (unlikely(!fan_select_fan1()))
7412 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
7413 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
7417 *speed
= (hi
<< 8) | lo
;
7428 static int fan2_get_speed(unsigned int *speed
)
7433 switch (fan_status_access_mode
) {
7434 case TPACPI_FAN_RD_TPEC
:
7435 /* all except 570, 600e/x, 770e, 770x */
7436 if (unlikely(!fan_select_fan2()))
7438 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
7439 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
7440 fan_select_fan1(); /* play it safe */
7445 *speed
= (hi
<< 8) | lo
;
7456 static int fan_set_level(int level
)
7458 if (!fan_control_allowed
)
7461 switch (fan_control_access_mode
) {
7462 case TPACPI_FAN_WR_ACPI_SFAN
:
7463 if (level
>= 0 && level
<= 7) {
7464 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
7470 case TPACPI_FAN_WR_ACPI_FANS
:
7471 case TPACPI_FAN_WR_TPEC
:
7472 if (!(level
& TP_EC_FAN_AUTO
) &&
7473 !(level
& TP_EC_FAN_FULLSPEED
) &&
7474 ((level
< 0) || (level
> 7)))
7477 /* safety net should the EC not support AUTO
7478 * or FULLSPEED mode bits and just ignore them */
7479 if (level
& TP_EC_FAN_FULLSPEED
)
7480 level
|= 7; /* safety min speed 7 */
7481 else if (level
& TP_EC_FAN_AUTO
)
7482 level
|= 4; /* safety min speed 4 */
7484 if (!acpi_ec_write(fan_status_offset
, level
))
7487 tp_features
.fan_ctrl_status_undef
= 0;
7494 vdbg_printk(TPACPI_DBG_FAN
,
7495 "fan control: set fan control register to 0x%02x\n", level
);
7499 static int fan_set_level_safe(int level
)
7503 if (!fan_control_allowed
)
7506 if (mutex_lock_killable(&fan_mutex
))
7507 return -ERESTARTSYS
;
7509 if (level
== TPACPI_FAN_LAST_LEVEL
)
7510 level
= fan_control_desired_level
;
7512 rc
= fan_set_level(level
);
7514 fan_update_desired_level(level
);
7516 mutex_unlock(&fan_mutex
);
7520 static int fan_set_enable(void)
7525 if (!fan_control_allowed
)
7528 if (mutex_lock_killable(&fan_mutex
))
7529 return -ERESTARTSYS
;
7531 switch (fan_control_access_mode
) {
7532 case TPACPI_FAN_WR_ACPI_FANS
:
7533 case TPACPI_FAN_WR_TPEC
:
7534 rc
= fan_get_status(&s
);
7538 /* Don't go out of emergency fan mode */
7541 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
7544 if (!acpi_ec_write(fan_status_offset
, s
))
7547 tp_features
.fan_ctrl_status_undef
= 0;
7552 case TPACPI_FAN_WR_ACPI_SFAN
:
7553 rc
= fan_get_status(&s
);
7559 /* Set fan to at least level 4 */
7562 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
7572 mutex_unlock(&fan_mutex
);
7575 vdbg_printk(TPACPI_DBG_FAN
,
7576 "fan control: set fan control register to 0x%02x\n",
7581 static int fan_set_disable(void)
7585 if (!fan_control_allowed
)
7588 if (mutex_lock_killable(&fan_mutex
))
7589 return -ERESTARTSYS
;
7592 switch (fan_control_access_mode
) {
7593 case TPACPI_FAN_WR_ACPI_FANS
:
7594 case TPACPI_FAN_WR_TPEC
:
7595 if (!acpi_ec_write(fan_status_offset
, 0x00))
7598 fan_control_desired_level
= 0;
7599 tp_features
.fan_ctrl_status_undef
= 0;
7603 case TPACPI_FAN_WR_ACPI_SFAN
:
7604 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
7607 fan_control_desired_level
= 0;
7615 vdbg_printk(TPACPI_DBG_FAN
,
7616 "fan control: set fan control register to 0\n");
7618 mutex_unlock(&fan_mutex
);
7622 static int fan_set_speed(int speed
)
7626 if (!fan_control_allowed
)
7629 if (mutex_lock_killable(&fan_mutex
))
7630 return -ERESTARTSYS
;
7633 switch (fan_control_access_mode
) {
7634 case TPACPI_FAN_WR_ACPI_FANS
:
7635 if (speed
>= 0 && speed
<= 65535) {
7636 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
7637 speed
, speed
, speed
))
7647 mutex_unlock(&fan_mutex
);
7651 static void fan_watchdog_reset(void)
7653 static int fan_watchdog_active
;
7655 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
7658 if (fan_watchdog_active
)
7659 cancel_delayed_work(&fan_watchdog_task
);
7661 if (fan_watchdog_maxinterval
> 0 &&
7662 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
7663 fan_watchdog_active
= 1;
7664 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
7665 msecs_to_jiffies(fan_watchdog_maxinterval
7667 pr_err("failed to queue the fan watchdog, "
7668 "watchdog will not trigger\n");
7671 fan_watchdog_active
= 0;
7674 static void fan_watchdog_fire(struct work_struct
*ignored
)
7678 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
7681 pr_notice("fan watchdog: enabling fan\n");
7682 rc
= fan_set_enable();
7684 pr_err("fan watchdog: error %d while enabling fan, "
7685 "will try again later...\n", -rc
);
7686 /* reschedule for later */
7687 fan_watchdog_reset();
7692 * SYSFS fan layout: hwmon compatible (device)
7695 * 0: "disengaged" mode
7697 * 2: native EC "auto" mode (recommended, hardware default)
7699 * pwm*: set speed in manual mode, ignored otherwise.
7700 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7703 * fan*_input: tachometer reading, RPM
7706 * SYSFS fan layout: extensions
7708 * fan_watchdog (driver):
7709 * fan watchdog interval in seconds, 0 disables (default), max 120
7712 /* sysfs fan pwm1_enable ----------------------------------------------- */
7713 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
7714 struct device_attribute
*attr
,
7720 res
= fan_get_status_safe(&status
);
7724 if (status
& TP_EC_FAN_FULLSPEED
) {
7726 } else if (status
& TP_EC_FAN_AUTO
) {
7731 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
7734 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
7735 struct device_attribute
*attr
,
7736 const char *buf
, size_t count
)
7741 if (parse_strtoul(buf
, 2, &t
))
7744 tpacpi_disclose_usertask("hwmon pwm1_enable",
7745 "set fan mode to %lu\n", t
);
7749 level
= TP_EC_FAN_FULLSPEED
;
7752 level
= TPACPI_FAN_LAST_LEVEL
;
7755 level
= TP_EC_FAN_AUTO
;
7758 /* reserved for software-controlled auto mode */
7764 res
= fan_set_level_safe(level
);
7770 fan_watchdog_reset();
7775 static struct device_attribute dev_attr_fan_pwm1_enable
=
7776 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
7777 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
7779 /* sysfs fan pwm1 ------------------------------------------------------ */
7780 static ssize_t
fan_pwm1_show(struct device
*dev
,
7781 struct device_attribute
*attr
,
7787 res
= fan_get_status_safe(&status
);
7792 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
7793 status
= fan_control_desired_level
;
7798 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
7801 static ssize_t
fan_pwm1_store(struct device
*dev
,
7802 struct device_attribute
*attr
,
7803 const char *buf
, size_t count
)
7807 u8 status
, newlevel
;
7809 if (parse_strtoul(buf
, 255, &s
))
7812 tpacpi_disclose_usertask("hwmon pwm1",
7813 "set fan speed to %lu\n", s
);
7815 /* scale down from 0-255 to 0-7 */
7816 newlevel
= (s
>> 5) & 0x07;
7818 if (mutex_lock_killable(&fan_mutex
))
7819 return -ERESTARTSYS
;
7821 rc
= fan_get_status(&status
);
7822 if (!rc
&& (status
&
7823 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
7824 rc
= fan_set_level(newlevel
);
7828 fan_update_desired_level(newlevel
);
7829 fan_watchdog_reset();
7833 mutex_unlock(&fan_mutex
);
7834 return (rc
)? rc
: count
;
7837 static struct device_attribute dev_attr_fan_pwm1
=
7838 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
7839 fan_pwm1_show
, fan_pwm1_store
);
7841 /* sysfs fan fan1_input ------------------------------------------------ */
7842 static ssize_t
fan_fan1_input_show(struct device
*dev
,
7843 struct device_attribute
*attr
,
7849 res
= fan_get_speed(&speed
);
7853 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7856 static struct device_attribute dev_attr_fan_fan1_input
=
7857 __ATTR(fan1_input
, S_IRUGO
,
7858 fan_fan1_input_show
, NULL
);
7860 /* sysfs fan fan2_input ------------------------------------------------ */
7861 static ssize_t
fan_fan2_input_show(struct device
*dev
,
7862 struct device_attribute
*attr
,
7868 res
= fan2_get_speed(&speed
);
7872 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7875 static struct device_attribute dev_attr_fan_fan2_input
=
7876 __ATTR(fan2_input
, S_IRUGO
,
7877 fan_fan2_input_show
, NULL
);
7879 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7880 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
7883 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
7886 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
7887 const char *buf
, size_t count
)
7891 if (parse_strtoul(buf
, 120, &t
))
7894 if (!fan_control_allowed
)
7897 fan_watchdog_maxinterval
= t
;
7898 fan_watchdog_reset();
7900 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
7905 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
7906 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
7908 /* --------------------------------------------------------------------- */
7909 static struct attribute
*fan_attributes
[] = {
7910 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
7911 &dev_attr_fan_fan1_input
.attr
,
7912 NULL
, /* for fan2_input */
7916 static const struct attribute_group fan_attr_group
= {
7917 .attrs
= fan_attributes
,
7920 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7921 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7923 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7924 { .vendor = PCI_VENDOR_ID_IBM, \
7925 .bios = TPACPI_MATCH_ANY, \
7926 .ec = TPID(__id1, __id2), \
7927 .quirks = __quirks }
7929 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7930 { .vendor = PCI_VENDOR_ID_LENOVO, \
7931 .bios = TPACPI_MATCH_ANY, \
7932 .ec = TPID(__id1, __id2), \
7933 .quirks = __quirks }
7935 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
7936 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
7937 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
7938 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
7939 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
7940 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
7943 #undef TPACPI_FAN_QL
7944 #undef TPACPI_FAN_QI
7946 static int __init
fan_init(struct ibm_init_struct
*iibm
)
7949 unsigned long quirks
;
7951 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7952 "initializing fan subdriver\n");
7954 mutex_init(&fan_mutex
);
7955 fan_status_access_mode
= TPACPI_FAN_NONE
;
7956 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7957 fan_control_commands
= 0;
7958 fan_watchdog_maxinterval
= 0;
7959 tp_features
.fan_ctrl_status_undef
= 0;
7960 tp_features
.second_fan
= 0;
7961 fan_control_desired_level
= 7;
7963 if (tpacpi_is_ibm()) {
7964 TPACPI_ACPIHANDLE_INIT(fans
);
7965 TPACPI_ACPIHANDLE_INIT(gfan
);
7966 TPACPI_ACPIHANDLE_INIT(sfan
);
7969 quirks
= tpacpi_check_quirks(fan_quirk_table
,
7970 ARRAY_SIZE(fan_quirk_table
));
7973 /* 570, 600e/x, 770e, 770x */
7974 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
7976 /* all other ThinkPads: note that even old-style
7977 * ThinkPad ECs supports the fan control register */
7978 if (likely(acpi_ec_read(fan_status_offset
,
7979 &fan_control_initial_status
))) {
7980 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
7981 if (quirks
& TPACPI_FAN_Q1
)
7983 if (quirks
& TPACPI_FAN_2FAN
) {
7984 tp_features
.second_fan
= 1;
7985 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7986 "secondary fan support enabled\n");
7989 pr_err("ThinkPad ACPI EC access misbehaving, "
7990 "fan status and control unavailable\n");
7997 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
7998 fan_control_commands
|=
7999 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
8002 /* gfan without sfan means no fan control */
8003 /* all other models implement TP EC 0x2f control */
8007 fan_control_access_mode
=
8008 TPACPI_FAN_WR_ACPI_FANS
;
8009 fan_control_commands
|=
8010 TPACPI_FAN_CMD_SPEED
|
8011 TPACPI_FAN_CMD_LEVEL
|
8012 TPACPI_FAN_CMD_ENABLE
;
8014 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
8015 fan_control_commands
|=
8016 TPACPI_FAN_CMD_LEVEL
|
8017 TPACPI_FAN_CMD_ENABLE
;
8022 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8023 "fan is %s, modes %d, %d\n",
8024 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
8025 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
8026 fan_status_access_mode
, fan_control_access_mode
);
8028 /* fan control master switch */
8029 if (!fan_control_allowed
) {
8030 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
8031 fan_control_commands
= 0;
8032 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
8033 "fan control features disabled by parameter\n");
8036 /* update fan_control_desired_level */
8037 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
8038 fan_get_status_safe(NULL
);
8040 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
8041 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
8042 if (tp_features
.second_fan
) {
8043 /* attach second fan tachometer */
8044 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
8045 &dev_attr_fan_fan2_input
.attr
;
8047 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
8052 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
8053 &driver_attr_fan_watchdog
);
8055 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
8064 static void fan_exit(void)
8066 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
8067 "cancelling any pending fan watchdog tasks\n");
8069 /* FIXME: can we really do this unconditionally? */
8070 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
8071 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
8072 &driver_attr_fan_watchdog
);
8074 cancel_delayed_work(&fan_watchdog_task
);
8075 flush_workqueue(tpacpi_wq
);
8078 static void fan_suspend(pm_message_t state
)
8082 if (!fan_control_allowed
)
8085 /* Store fan status in cache */
8086 fan_control_resume_level
= 0;
8087 rc
= fan_get_status_safe(&fan_control_resume_level
);
8089 pr_notice("failed to read fan level for later "
8090 "restore during resume: %d\n", rc
);
8092 /* if it is undefined, don't attempt to restore it.
8094 if (tp_features
.fan_ctrl_status_undef
)
8095 fan_control_resume_level
= 0;
8098 static void fan_resume(void)
8100 u8 current_level
= 7;
8101 bool do_set
= false;
8104 /* DSDT *always* updates status on resume */
8105 tp_features
.fan_ctrl_status_undef
= 0;
8107 if (!fan_control_allowed
||
8108 !fan_control_resume_level
||
8109 (fan_get_status_safe(¤t_level
) < 0))
8112 switch (fan_control_access_mode
) {
8113 case TPACPI_FAN_WR_ACPI_SFAN
:
8114 /* never decrease fan level */
8115 do_set
= (fan_control_resume_level
> current_level
);
8117 case TPACPI_FAN_WR_ACPI_FANS
:
8118 case TPACPI_FAN_WR_TPEC
:
8119 /* never decrease fan level, scale is:
8120 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8122 * We expect the firmware to set either 7 or AUTO, but we
8123 * handle FULLSPEED out of paranoia.
8125 * So, we can safely only restore FULLSPEED or 7, anything
8126 * else could slow the fan. Restoring AUTO is useless, at
8127 * best that's exactly what the DSDT already set (it is the
8130 * Always keep in mind that the DSDT *will* have set the
8131 * fans to what the vendor supposes is the best level. We
8132 * muck with it only to speed the fan up.
8134 if (fan_control_resume_level
!= 7 &&
8135 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
8138 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
8139 (current_level
!= fan_control_resume_level
);
8145 pr_notice("restoring fan level to 0x%02x\n",
8146 fan_control_resume_level
);
8147 rc
= fan_set_level_safe(fan_control_resume_level
);
8149 pr_notice("failed to restore fan level: %d\n", rc
);
8153 static int fan_read(struct seq_file
*m
)
8157 unsigned int speed
= 0;
8159 switch (fan_status_access_mode
) {
8160 case TPACPI_FAN_RD_ACPI_GFAN
:
8161 /* 570, 600e/x, 770e, 770x */
8162 rc
= fan_get_status_safe(&status
);
8166 seq_printf(m
, "status:\t\t%s\n"
8168 (status
!= 0) ? "enabled" : "disabled", status
);
8171 case TPACPI_FAN_RD_TPEC
:
8172 /* all except 570, 600e/x, 770e, 770x */
8173 rc
= fan_get_status_safe(&status
);
8177 seq_printf(m
, "status:\t\t%s\n",
8178 (status
!= 0) ? "enabled" : "disabled");
8180 rc
= fan_get_speed(&speed
);
8184 seq_printf(m
, "speed:\t\t%d\n", speed
);
8186 if (status
& TP_EC_FAN_FULLSPEED
)
8187 /* Disengaged mode takes precedence */
8188 seq_printf(m
, "level:\t\tdisengaged\n");
8189 else if (status
& TP_EC_FAN_AUTO
)
8190 seq_printf(m
, "level:\t\tauto\n");
8192 seq_printf(m
, "level:\t\t%d\n", status
);
8195 case TPACPI_FAN_NONE
:
8197 seq_printf(m
, "status:\t\tnot supported\n");
8200 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
8201 seq_printf(m
, "commands:\tlevel <level>");
8203 switch (fan_control_access_mode
) {
8204 case TPACPI_FAN_WR_ACPI_SFAN
:
8205 seq_printf(m
, " (<level> is 0-7)\n");
8209 seq_printf(m
, " (<level> is 0-7, "
8210 "auto, disengaged, full-speed)\n");
8215 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
8216 seq_printf(m
, "commands:\tenable, disable\n"
8217 "commands:\twatchdog <timeout> (<timeout> "
8218 "is 0 (off), 1-120 (seconds))\n");
8220 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
8221 seq_printf(m
, "commands:\tspeed <speed>"
8222 " (<speed> is 0-65535)\n");
8227 static int fan_write_cmd_level(const char *cmd
, int *rc
)
8231 if (strlencmp(cmd
, "level auto") == 0)
8232 level
= TP_EC_FAN_AUTO
;
8233 else if ((strlencmp(cmd
, "level disengaged") == 0) |
8234 (strlencmp(cmd
, "level full-speed") == 0))
8235 level
= TP_EC_FAN_FULLSPEED
;
8236 else if (sscanf(cmd
, "level %d", &level
) != 1)
8239 *rc
= fan_set_level_safe(level
);
8241 pr_err("level command accepted for unsupported access mode %d\n",
8242 fan_control_access_mode
);
8244 tpacpi_disclose_usertask("procfs fan",
8245 "set level to %d\n", level
);
8250 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
8252 if (strlencmp(cmd
, "enable") != 0)
8255 *rc
= fan_set_enable();
8257 pr_err("enable command accepted for unsupported access mode %d\n",
8258 fan_control_access_mode
);
8260 tpacpi_disclose_usertask("procfs fan", "enable\n");
8265 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
8267 if (strlencmp(cmd
, "disable") != 0)
8270 *rc
= fan_set_disable();
8272 pr_err("disable command accepted for unsupported access mode %d\n",
8273 fan_control_access_mode
);
8275 tpacpi_disclose_usertask("procfs fan", "disable\n");
8280 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
8285 * Support speed <low> <medium> <high> ? */
8287 if (sscanf(cmd
, "speed %d", &speed
) != 1)
8290 *rc
= fan_set_speed(speed
);
8292 pr_err("speed command accepted for unsupported access mode %d\n",
8293 fan_control_access_mode
);
8295 tpacpi_disclose_usertask("procfs fan",
8296 "set speed to %d\n", speed
);
8301 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
8305 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
8308 if (interval
< 0 || interval
> 120)
8311 fan_watchdog_maxinterval
= interval
;
8312 tpacpi_disclose_usertask("procfs fan",
8313 "set watchdog timer to %d\n",
8320 static int fan_write(char *buf
)
8325 while (!rc
&& (cmd
= next_cmd(&buf
))) {
8326 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
8327 fan_write_cmd_level(cmd
, &rc
)) &&
8328 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
8329 (fan_write_cmd_enable(cmd
, &rc
) ||
8330 fan_write_cmd_disable(cmd
, &rc
) ||
8331 fan_write_cmd_watchdog(cmd
, &rc
))) &&
8332 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
8333 fan_write_cmd_speed(cmd
, &rc
))
8337 fan_watchdog_reset();
8343 static struct ibm_struct fan_driver_data
= {
8348 .suspend
= fan_suspend
,
8349 .resume
= fan_resume
,
8352 /****************************************************************************
8353 ****************************************************************************
8357 ****************************************************************************
8358 ****************************************************************************/
8361 * HKEY event callout for other subdrivers go here
8362 * (yes, it is ugly, but it is quick, safe, and gets the job done
8364 static void tpacpi_driver_event(const unsigned int hkey_event
)
8366 if (ibm_backlight_device
) {
8367 switch (hkey_event
) {
8368 case TP_HKEY_EV_BRGHT_UP
:
8369 case TP_HKEY_EV_BRGHT_DOWN
:
8370 tpacpi_brightness_notify_change();
8374 switch (hkey_event
) {
8375 case TP_HKEY_EV_VOL_UP
:
8376 case TP_HKEY_EV_VOL_DOWN
:
8377 case TP_HKEY_EV_VOL_MUTE
:
8378 volume_alsa_notify_change();
8383 static void hotkey_driver_event(const unsigned int scancode
)
8385 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
8388 /* sysfs name ---------------------------------------------------------- */
8389 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
8390 struct device_attribute
*attr
,
8393 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
8396 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
8397 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
8399 /* --------------------------------------------------------------------- */
8402 static struct proc_dir_entry
*proc_dir
;
8405 * Module and infrastructure proble, init and exit handling
8408 static int force_load
;
8410 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8411 static const char * __init
str_supported(int is_supported
)
8413 static char text_unsupported
[] __initdata
= "not supported";
8415 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
8417 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8419 static void ibm_exit(struct ibm_struct
*ibm
)
8421 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
8423 list_del_init(&ibm
->all_drivers
);
8425 if (ibm
->flags
.acpi_notify_installed
) {
8426 dbg_printk(TPACPI_DBG_EXIT
,
8427 "%s: acpi_remove_notify_handler\n", ibm
->name
);
8429 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
8431 dispatch_acpi_notify
);
8432 ibm
->flags
.acpi_notify_installed
= 0;
8435 if (ibm
->flags
.proc_created
) {
8436 dbg_printk(TPACPI_DBG_EXIT
,
8437 "%s: remove_proc_entry\n", ibm
->name
);
8438 remove_proc_entry(ibm
->name
, proc_dir
);
8439 ibm
->flags
.proc_created
= 0;
8442 if (ibm
->flags
.acpi_driver_registered
) {
8443 dbg_printk(TPACPI_DBG_EXIT
,
8444 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
8446 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
8447 kfree(ibm
->acpi
->driver
);
8448 ibm
->acpi
->driver
= NULL
;
8449 ibm
->flags
.acpi_driver_registered
= 0;
8452 if (ibm
->flags
.init_called
&& ibm
->exit
) {
8454 ibm
->flags
.init_called
= 0;
8457 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
8460 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
8463 struct ibm_struct
*ibm
= iibm
->data
;
8464 struct proc_dir_entry
*entry
;
8466 BUG_ON(ibm
== NULL
);
8468 INIT_LIST_HEAD(&ibm
->all_drivers
);
8470 if (ibm
->flags
.experimental
&& !experimental
)
8473 dbg_printk(TPACPI_DBG_INIT
,
8474 "probing for %s\n", ibm
->name
);
8477 ret
= iibm
->init(iibm
);
8479 return 0; /* probe failed */
8483 ibm
->flags
.init_called
= 1;
8487 if (ibm
->acpi
->hid
) {
8488 ret
= register_tpacpi_subdriver(ibm
);
8493 if (ibm
->acpi
->notify
) {
8494 ret
= setup_acpi_notify(ibm
);
8495 if (ret
== -ENODEV
) {
8496 pr_notice("disabling subdriver %s\n",
8506 dbg_printk(TPACPI_DBG_INIT
,
8507 "%s installed\n", ibm
->name
);
8510 mode_t mode
= iibm
->base_procfs_mode
;
8516 entry
= proc_create_data(ibm
->name
, mode
, proc_dir
,
8517 &dispatch_proc_fops
, ibm
);
8519 pr_err("unable to create proc entry %s\n", ibm
->name
);
8523 ibm
->flags
.proc_created
= 1;
8526 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
8531 dbg_printk(TPACPI_DBG_INIT
,
8532 "%s: at error exit path with result %d\n",
8536 return (ret
< 0)? ret
: 0;
8541 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
8543 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
8546 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8547 static bool __pure __init
tpacpi_is_valid_fw_id(const char* const s
,
8550 return s
&& strlen(s
) >= 8 &&
8551 tpacpi_is_fw_digit(s
[0]) &&
8552 tpacpi_is_fw_digit(s
[1]) &&
8553 s
[2] == t
&& s
[3] == 'T' &&
8554 tpacpi_is_fw_digit(s
[4]) &&
8555 tpacpi_is_fw_digit(s
[5]);
8558 /* returns 0 - probe ok, or < 0 - probe error.
8559 * Probe ok doesn't mean thinkpad found.
8560 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8561 static int __must_check __init
get_thinkpad_model_data(
8562 struct thinkpad_id_data
*tp
)
8564 const struct dmi_device
*dev
= NULL
;
8565 char ec_fw_string
[18];
8571 memset(tp
, 0, sizeof(*tp
));
8573 if (dmi_name_in_vendors("IBM"))
8574 tp
->vendor
= PCI_VENDOR_ID_IBM
;
8575 else if (dmi_name_in_vendors("LENOVO"))
8576 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
8580 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
8581 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
8582 if (s
&& !tp
->bios_version_str
)
8585 /* Really ancient ThinkPad 240X will fail this, which is fine */
8586 if (!tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E'))
8589 tp
->bios_model
= tp
->bios_version_str
[0]
8590 | (tp
->bios_version_str
[1] << 8);
8591 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
8592 | tp
->bios_version_str
[5];
8595 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8596 * X32 or newer, all Z series; Some models must have an
8597 * up-to-date BIOS or they will not be detected.
8599 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8601 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
8602 if (sscanf(dev
->name
,
8603 "IBM ThinkPad Embedded Controller -[%17c",
8604 ec_fw_string
) == 1) {
8605 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
8606 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
8608 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
8609 if (!tp
->ec_version_str
)
8612 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
8613 tp
->ec_model
= ec_fw_string
[0]
8614 | (ec_fw_string
[1] << 8);
8615 tp
->ec_release
= (ec_fw_string
[4] << 8)
8618 pr_notice("ThinkPad firmware release %s "
8619 "doesn't match the known patterns\n",
8621 pr_notice("please report this to %s\n",
8628 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
8629 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
8630 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
8635 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
8636 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
8637 if (s
&& !tp
->nummodel_str
)
8643 static int __init
probe_for_thinkpad(void)
8650 /* It would be dangerous to run the driver in this case */
8651 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8655 * Non-ancient models have better DMI tagging, but very old models
8656 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8658 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
) ||
8659 (thinkpad_id
.ec_model
!= 0) ||
8660 tpacpi_is_fw_known();
8662 /* The EC handler is required */
8663 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID
, &ec_handle
);
8666 pr_err("Not yet supported ThinkPad detected!\n");
8670 if (!is_thinkpad
&& !force_load
)
8676 static void __init
thinkpad_acpi_init_banner(void)
8678 pr_info("%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
8679 pr_info("%s\n", TPACPI_URL
);
8681 pr_info("ThinkPad BIOS %s, EC %s\n",
8682 (thinkpad_id
.bios_version_str
) ?
8683 thinkpad_id
.bios_version_str
: "unknown",
8684 (thinkpad_id
.ec_version_str
) ?
8685 thinkpad_id
.ec_version_str
: "unknown");
8687 BUG_ON(!thinkpad_id
.vendor
);
8689 if (thinkpad_id
.model_str
)
8690 pr_info("%s %s, model %s\n",
8691 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
8692 "IBM" : ((thinkpad_id
.vendor
==
8693 PCI_VENDOR_ID_LENOVO
) ?
8694 "Lenovo" : "Unknown vendor"),
8695 thinkpad_id
.model_str
,
8696 (thinkpad_id
.nummodel_str
) ?
8697 thinkpad_id
.nummodel_str
: "unknown");
8700 /* Module init, exit, parameters */
8702 static struct ibm_init_struct ibms_init
[] __initdata
= {
8704 .data
= &thinkpad_acpi_driver_data
,
8707 .init
= hotkey_init
,
8708 .data
= &hotkey_driver_data
,
8711 .init
= bluetooth_init
,
8712 .data
= &bluetooth_driver_data
,
8716 .data
= &wan_driver_data
,
8720 .data
= &uwb_driver_data
,
8722 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8725 .base_procfs_mode
= S_IRUSR
,
8726 .data
= &video_driver_data
,
8731 .data
= &light_driver_data
,
8735 .data
= &cmos_driver_data
,
8739 .data
= &led_driver_data
,
8743 .data
= &beep_driver_data
,
8746 .init
= thermal_init
,
8747 .data
= &thermal_driver_data
,
8750 .init
= brightness_init
,
8751 .data
= &brightness_driver_data
,
8754 .init
= volume_init
,
8755 .data
= &volume_driver_data
,
8759 .data
= &fan_driver_data
,
8763 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
8766 struct ibm_struct
*ibm
;
8768 if (!kp
|| !kp
->name
|| !val
)
8771 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8772 ibm
= ibms_init
[i
].data
;
8773 WARN_ON(ibm
== NULL
);
8775 if (!ibm
|| !ibm
->name
)
8778 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
8779 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
8781 strcpy(ibms_init
[i
].param
, val
);
8782 strcat(ibms_init
[i
].param
, ",");
8790 module_param(experimental
, int, 0444);
8791 MODULE_PARM_DESC(experimental
,
8792 "Enables experimental features when non-zero");
8794 module_param_named(debug
, dbg_level
, uint
, 0);
8795 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
8797 module_param(force_load
, bool, 0444);
8798 MODULE_PARM_DESC(force_load
,
8799 "Attempts to load the driver even on a "
8800 "mis-identified ThinkPad when true");
8802 module_param_named(fan_control
, fan_control_allowed
, bool, 0444);
8803 MODULE_PARM_DESC(fan_control
,
8804 "Enables setting fan parameters features when true");
8806 module_param_named(brightness_mode
, brightness_mode
, uint
, 0444);
8807 MODULE_PARM_DESC(brightness_mode
,
8808 "Selects brightness control strategy: "
8809 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8811 module_param(brightness_enable
, uint
, 0444);
8812 MODULE_PARM_DESC(brightness_enable
,
8813 "Enables backlight control when 1, disables when 0");
8815 module_param(hotkey_report_mode
, uint
, 0444);
8816 MODULE_PARM_DESC(hotkey_report_mode
,
8817 "used for backwards compatibility with userspace, "
8818 "see documentation");
8820 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8821 module_param_named(volume_mode
, volume_mode
, uint
, 0444);
8822 MODULE_PARM_DESC(volume_mode
,
8823 "Selects volume control strategy: "
8824 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8826 module_param_named(volume_capabilities
, volume_capabilities
, uint
, 0444);
8827 MODULE_PARM_DESC(volume_capabilities
,
8828 "Selects the mixer capabilites: "
8829 "0=auto, 1=volume and mute, 2=mute only");
8831 module_param_named(volume_control
, volume_control_allowed
, bool, 0444);
8832 MODULE_PARM_DESC(volume_control
,
8833 "Enables software override for the console audio "
8834 "control when true");
8836 /* ALSA module API parameters */
8837 module_param_named(index
, alsa_index
, int, 0444);
8838 MODULE_PARM_DESC(index
, "ALSA index for the ACPI EC Mixer");
8839 module_param_named(id
, alsa_id
, charp
, 0444);
8840 MODULE_PARM_DESC(id
, "ALSA id for the ACPI EC Mixer");
8841 module_param_named(enable
, alsa_enable
, bool, 0444);
8842 MODULE_PARM_DESC(enable
, "Enable the ALSA interface for the ACPI EC Mixer");
8843 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8845 #define TPACPI_PARAM(feature) \
8846 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8847 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8848 "at module load, see documentation")
8850 TPACPI_PARAM(hotkey
);
8851 TPACPI_PARAM(bluetooth
);
8852 TPACPI_PARAM(video
);
8853 TPACPI_PARAM(light
);
8857 TPACPI_PARAM(brightness
);
8858 TPACPI_PARAM(volume
);
8861 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8862 module_param(dbg_wlswemul
, uint
, 0444);
8863 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
8864 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
8865 MODULE_PARM_DESC(wlsw_state
,
8866 "Initial state of the emulated WLSW switch");
8868 module_param(dbg_bluetoothemul
, uint
, 0444);
8869 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
8870 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
8871 MODULE_PARM_DESC(bluetooth_state
,
8872 "Initial state of the emulated bluetooth switch");
8874 module_param(dbg_wwanemul
, uint
, 0444);
8875 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
8876 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
8877 MODULE_PARM_DESC(wwan_state
,
8878 "Initial state of the emulated WWAN switch");
8880 module_param(dbg_uwbemul
, uint
, 0444);
8881 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
8882 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
8883 MODULE_PARM_DESC(uwb_state
,
8884 "Initial state of the emulated UWB switch");
8887 static void thinkpad_acpi_module_exit(void)
8889 struct ibm_struct
*ibm
, *itmp
;
8891 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
8893 list_for_each_entry_safe_reverse(ibm
, itmp
,
8894 &tpacpi_all_drivers
,
8899 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
8901 if (tpacpi_inputdev
) {
8902 if (tp_features
.input_device_registered
)
8903 input_unregister_device(tpacpi_inputdev
);
8905 input_free_device(tpacpi_inputdev
);
8909 hwmon_device_unregister(tpacpi_hwmon
);
8911 if (tp_features
.sensors_pdev_attrs_registered
)
8912 device_remove_file(&tpacpi_sensors_pdev
->dev
,
8913 &dev_attr_thinkpad_acpi_pdev_name
);
8914 if (tpacpi_sensors_pdev
)
8915 platform_device_unregister(tpacpi_sensors_pdev
);
8917 platform_device_unregister(tpacpi_pdev
);
8919 if (tp_features
.sensors_pdrv_attrs_registered
)
8920 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
8921 if (tp_features
.platform_drv_attrs_registered
)
8922 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
8924 if (tp_features
.sensors_pdrv_registered
)
8925 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
8927 if (tp_features
.platform_drv_registered
)
8928 platform_driver_unregister(&tpacpi_pdriver
);
8931 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
8934 destroy_workqueue(tpacpi_wq
);
8936 kfree(thinkpad_id
.bios_version_str
);
8937 kfree(thinkpad_id
.ec_version_str
);
8938 kfree(thinkpad_id
.model_str
);
8942 static int __init
thinkpad_acpi_module_init(void)
8946 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
8948 /* Parameter checking */
8949 if (hotkey_report_mode
> 2)
8952 /* Driver-level probe */
8954 ret
= get_thinkpad_model_data(&thinkpad_id
);
8956 pr_err("unable to get DMI data: %d\n", ret
);
8957 thinkpad_acpi_module_exit();
8960 ret
= probe_for_thinkpad();
8962 thinkpad_acpi_module_exit();
8966 /* Driver initialization */
8968 thinkpad_acpi_init_banner();
8969 tpacpi_check_outdated_fw();
8971 TPACPI_ACPIHANDLE_INIT(ecrd
);
8972 TPACPI_ACPIHANDLE_INIT(ecwr
);
8974 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
8976 thinkpad_acpi_module_exit();
8980 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
8982 pr_err("unable to create proc dir " TPACPI_PROC_DIR
"\n");
8983 thinkpad_acpi_module_exit();
8987 ret
= platform_driver_register(&tpacpi_pdriver
);
8989 pr_err("unable to register main platform driver\n");
8990 thinkpad_acpi_module_exit();
8993 tp_features
.platform_drv_registered
= 1;
8995 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
8997 pr_err("unable to register hwmon platform driver\n");
8998 thinkpad_acpi_module_exit();
9001 tp_features
.sensors_pdrv_registered
= 1;
9003 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
9005 tp_features
.platform_drv_attrs_registered
= 1;
9006 ret
= tpacpi_create_driver_attributes(
9007 &tpacpi_hwmon_pdriver
.driver
);
9010 pr_err("unable to create sysfs driver attributes\n");
9011 thinkpad_acpi_module_exit();
9014 tp_features
.sensors_pdrv_attrs_registered
= 1;
9017 /* Device initialization */
9018 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
9020 if (IS_ERR(tpacpi_pdev
)) {
9021 ret
= PTR_ERR(tpacpi_pdev
);
9023 pr_err("unable to register platform device\n");
9024 thinkpad_acpi_module_exit();
9027 tpacpi_sensors_pdev
= platform_device_register_simple(
9028 TPACPI_HWMON_DRVR_NAME
,
9030 if (IS_ERR(tpacpi_sensors_pdev
)) {
9031 ret
= PTR_ERR(tpacpi_sensors_pdev
);
9032 tpacpi_sensors_pdev
= NULL
;
9033 pr_err("unable to register hwmon platform device\n");
9034 thinkpad_acpi_module_exit();
9037 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
9038 &dev_attr_thinkpad_acpi_pdev_name
);
9040 pr_err("unable to create sysfs hwmon device attributes\n");
9041 thinkpad_acpi_module_exit();
9044 tp_features
.sensors_pdev_attrs_registered
= 1;
9045 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
9046 if (IS_ERR(tpacpi_hwmon
)) {
9047 ret
= PTR_ERR(tpacpi_hwmon
);
9048 tpacpi_hwmon
= NULL
;
9049 pr_err("unable to register hwmon device\n");
9050 thinkpad_acpi_module_exit();
9053 mutex_init(&tpacpi_inputdev_send_mutex
);
9054 tpacpi_inputdev
= input_allocate_device();
9055 if (!tpacpi_inputdev
) {
9056 pr_err("unable to allocate input device\n");
9057 thinkpad_acpi_module_exit();
9060 /* Prepare input device, but don't register */
9061 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
9062 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
9063 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
9064 tpacpi_inputdev
->id
.vendor
= thinkpad_id
.vendor
;
9065 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
9066 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
9067 tpacpi_inputdev
->dev
.parent
= &tpacpi_pdev
->dev
;
9070 /* Init subdriver dependencies */
9071 tpacpi_detect_brightness_capabilities();
9073 /* Init subdrivers */
9074 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
9075 ret
= ibm_init(&ibms_init
[i
]);
9076 if (ret
>= 0 && *ibms_init
[i
].param
)
9077 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
9079 thinkpad_acpi_module_exit();
9084 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
9086 ret
= input_register_device(tpacpi_inputdev
);
9088 pr_err("unable to register input device\n");
9089 thinkpad_acpi_module_exit();
9092 tp_features
.input_device_registered
= 1;
9098 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
9101 * This will autoload the driver in almost every ThinkPad
9102 * in widespread use.
9104 * Only _VERY_ old models, like the 240, 240x and 570 lack
9105 * the HKEY event interface.
9107 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
9110 * DMI matching for module autoloading
9112 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9113 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9115 * Only models listed in thinkwiki will be supported, so add yours
9116 * if it is not there yet.
9118 #define IBM_BIOS_MODULE_ALIAS(__type) \
9119 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9121 /* Ancient thinkpad BIOSes have to be identified by
9122 * BIOS type or model number, and there are far less
9123 * BIOS types than model numbers... */
9124 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9126 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9127 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9128 MODULE_DESCRIPTION(TPACPI_DESC
);
9129 MODULE_VERSION(TPACPI_VERSION
);
9130 MODULE_LICENSE("GPL");
9132 module_init(thinkpad_acpi_module_init
);
9133 module_exit(thinkpad_acpi_module_exit
);