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 TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020700
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2
= 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT
= 0x58,
97 TP_NVRAM_ADDR_VIDEO
= 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS
= 0x5e,
99 TP_NVRAM_ADDR_MIXER
= 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD
= 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM
= 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY
= 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE
= 0x80,
108 TP_NVRAM_MASK_THINKLIGHT
= 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND
= 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS
= 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS
= 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS
= 0,
113 TP_NVRAM_MASK_MUTE
= 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME
= 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME
= 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME
= 0,
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
128 TP_ACPI_WGSV_GET_STATE
= 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME
= 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
= 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE
= 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
136 TP_ACPI_WGSV_STATE_WWANEXIST
= 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR
= 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES
= 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF
= 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST
= 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR
= 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES
= 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF
= 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST
= 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR
= 0x0020, /* UWB radio enabled */
149 enum tpacpi_hkey_event_t
{
151 TP_HKEY_EV_HOTKEY_BASE
= 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP
= 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN
= 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP
= 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN
= 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE
= 0x1017, /* Mixer output mute */
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK
= 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK
= 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ
= 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ
= 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW
= 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW
= 0x2413, /* battery empty, S4 */
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK
= 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK
= 0x4003, /* undock complete */
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ
= 0x3006, /* opt. drive tray ejected */
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE
= 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN
= 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET
= 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK
= 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED
= 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED
= 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED
= 0x5010, /* backlight control event */
183 TP_HKEY_EV_ALARM_BAT_HOT
= 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT
= 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT
= 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT
= 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED
= 0x6030, /* thermal table changed */
190 TP_HKEY_EV_RFKILL_CHANGED
= 0x7000, /* rfkill switch changed */
193 /****************************************************************************
197 #define TPACPI_NAME "thinkpad"
198 #define TPACPI_DESC "ThinkPad ACPI Extras"
199 #define TPACPI_FILE TPACPI_NAME "_acpi"
200 #define TPACPI_URL "http://ibm-acpi.sf.net/"
201 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
203 #define TPACPI_PROC_DIR "ibm"
204 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
205 #define TPACPI_DRVR_NAME TPACPI_FILE
206 #define TPACPI_DRVR_SHORTNAME "tpacpi"
207 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
209 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
210 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
212 #define TPACPI_MAX_ACPI_ARGS 3
214 /* rfkill switches */
216 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
217 TPACPI_RFK_WWAN_SW_ID
,
218 TPACPI_RFK_UWB_SW_ID
,
222 #define TPACPI_LOG TPACPI_FILE ": "
223 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
224 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
225 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
226 #define TPACPI_ERR KERN_ERR TPACPI_LOG
227 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
228 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
229 #define TPACPI_INFO KERN_INFO TPACPI_LOG
230 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
232 /* Debugging printk groups */
233 #define TPACPI_DBG_ALL 0xffff
234 #define TPACPI_DBG_DISCLOSETASK 0x8000
235 #define TPACPI_DBG_INIT 0x0001
236 #define TPACPI_DBG_EXIT 0x0002
237 #define TPACPI_DBG_RFKILL 0x0004
238 #define TPACPI_DBG_HKEY 0x0008
239 #define TPACPI_DBG_FAN 0x0010
240 #define TPACPI_DBG_BRGHT 0x0020
241 #define TPACPI_DBG_MIXER 0x0040
243 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
244 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
245 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
248 /****************************************************************************
249 * Driver-wide structs and misc. variables
254 struct tp_acpi_drv_struct
{
255 const struct acpi_device_id
*hid
;
256 struct acpi_driver
*driver
;
258 void (*notify
) (struct ibm_struct
*, u32
);
261 struct acpi_device
*device
;
267 int (*read
) (char *);
268 int (*write
) (char *);
270 void (*resume
) (void);
271 void (*suspend
) (pm_message_t state
);
272 void (*shutdown
) (void);
274 struct list_head all_drivers
;
276 struct tp_acpi_drv_struct
*acpi
;
279 u8 acpi_driver_registered
:1;
280 u8 acpi_notify_installed
:1;
287 struct ibm_init_struct
{
290 int (*init
) (struct ibm_init_struct
*);
291 struct ibm_struct
*data
;
302 u32 bright_16levels
:1;
303 u32 bright_acpimode
:1;
306 u32 fan_ctrl_status_undef
:1;
308 u32 beep_needs_two_args
:1;
309 u32 mixer_no_level_control
:1;
310 u32 input_device_registered
:1;
311 u32 platform_drv_registered
:1;
312 u32 platform_drv_attrs_registered
:1;
313 u32 sensors_pdrv_registered
:1;
314 u32 sensors_pdrv_attrs_registered
:1;
315 u32 sensors_pdev_attrs_registered
:1;
316 u32 hotkey_poll_active
:1;
320 u16 hotkey_mask_ff
:1;
321 u16 volume_ctrl_forbidden
:1;
324 struct thinkpad_id_data
{
325 unsigned int vendor
; /* ThinkPad vendor:
326 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
328 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
329 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
331 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
333 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
336 char *model_str
; /* ThinkPad T43 */
337 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
339 static struct thinkpad_id_data thinkpad_id
;
342 TPACPI_LIFE_INIT
= 0,
347 static int experimental
;
348 static u32 dbg_level
;
350 static struct workqueue_struct
*tpacpi_wq
;
358 /* Special LED class that can defer work */
359 struct tpacpi_led_classdev
{
360 struct led_classdev led_classdev
;
361 struct work_struct work
;
362 enum led_status_t new_state
;
366 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
367 static int dbg_wlswemul
;
368 static int tpacpi_wlsw_emulstate
;
369 static int dbg_bluetoothemul
;
370 static int tpacpi_bluetooth_emulstate
;
371 static int dbg_wwanemul
;
372 static int tpacpi_wwan_emulstate
;
373 static int dbg_uwbemul
;
374 static int tpacpi_uwb_emulstate
;
378 /*************************************************************************
382 #define dbg_printk(a_dbg_level, format, arg...) \
383 do { if (dbg_level & (a_dbg_level)) \
384 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
387 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
388 #define vdbg_printk dbg_printk
389 static const char *str_supported(int is_supported
);
391 #define vdbg_printk(a_dbg_level, format, arg...) \
395 static void tpacpi_log_usertask(const char * const what
)
397 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
398 what
, task_tgid_vnr(current
));
401 #define tpacpi_disclose_usertask(what, format, arg...) \
404 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
405 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
406 printk(TPACPI_DEBUG "%s: PID %d: " format, \
407 what, task_tgid_vnr(current), ## arg); \
412 * Quirk handling helpers
414 * ThinkPad IDs and versions seen in the field so far
415 * are two-characters from the set [0-9A-Z], i.e. base 36.
417 * We use values well outside that range as specials.
420 #define TPACPI_MATCH_ANY 0xffffU
421 #define TPACPI_MATCH_UNKNOWN 0U
423 /* TPID('1', 'Y') == 0x5931 */
424 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
426 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
427 { .vendor = PCI_VENDOR_ID_IBM, \
428 .bios = TPID(__id1, __id2), \
429 .ec = TPACPI_MATCH_ANY, \
430 .quirks = (__quirk) }
432 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
433 { .vendor = PCI_VENDOR_ID_LENOVO, \
434 .bios = TPID(__id1, __id2), \
435 .ec = TPACPI_MATCH_ANY, \
436 .quirks = (__quirk) }
438 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
439 { .vendor = PCI_VENDOR_ID_LENOVO, \
440 .bios = TPACPI_MATCH_ANY, \
441 .ec = TPID(__id1, __id2), \
442 .quirks = (__quirk) }
444 struct tpacpi_quirk
{
448 unsigned long quirks
;
452 * tpacpi_check_quirks() - search BIOS/EC version on a list
453 * @qlist: array of &struct tpacpi_quirk
454 * @qlist_size: number of elements in @qlist
456 * Iterates over a quirks list until one is found that matches the
457 * ThinkPad's vendor, BIOS and EC model.
459 * Returns 0 if nothing matches, otherwise returns the quirks field of
460 * the matching &struct tpacpi_quirk entry.
462 * The match criteria is: vendor, ec and bios much match.
464 static unsigned long __init
tpacpi_check_quirks(
465 const struct tpacpi_quirk
*qlist
,
466 unsigned int qlist_size
)
469 if ((qlist
->vendor
== thinkpad_id
.vendor
||
470 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
471 (qlist
->bios
== thinkpad_id
.bios_model
||
472 qlist
->bios
== TPACPI_MATCH_ANY
) &&
473 (qlist
->ec
== thinkpad_id
.ec_model
||
474 qlist
->ec
== TPACPI_MATCH_ANY
))
475 return qlist
->quirks
;
484 /****************************************************************************
485 ****************************************************************************
487 * ACPI Helpers and device model
489 ****************************************************************************
490 ****************************************************************************/
492 /*************************************************************************
496 static acpi_handle root_handle
;
498 #define TPACPI_HANDLE(object, parent, paths...) \
499 static acpi_handle object##_handle; \
500 static acpi_handle *object##_parent = &parent##_handle; \
501 static char *object##_path; \
502 static char *object##_paths[] = { paths }
504 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
505 "\\_SB.PCI.ISA.EC", /* 570 */
506 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
507 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
508 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
509 "\\_SB.PCI0.ICH3.EC0", /* R31 */
510 "\\_SB.PCI0.LPC.EC", /* all others */
513 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
514 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
516 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
518 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
519 "\\CMS", /* R40, R40e */
522 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
523 "^HKEY", /* R30, R31 */
524 "HKEY", /* all others */
527 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
528 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
529 "\\_SB.PCI0.VID0", /* 770e */
530 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
531 "\\_SB.PCI0.AGP.VID", /* all others */
535 /*************************************************************************
539 static int acpi_evalf(acpi_handle handle
,
540 void *res
, char *method
, char *fmt
, ...)
543 struct acpi_object_list params
;
544 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
545 struct acpi_buffer result
, *resultp
;
546 union acpi_object out_obj
;
554 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
567 params
.pointer
= &in_objs
[0];
574 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
575 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
577 /* add more types as needed */
579 printk(TPACPI_ERR
"acpi_evalf() called "
580 "with invalid format character '%c'\n", c
);
586 if (res_type
!= 'v') {
587 result
.length
= sizeof(out_obj
);
588 result
.pointer
= &out_obj
;
593 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
598 *(int *)res
= out_obj
.integer
.value
;
599 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
602 success
= status
== AE_OK
;
604 /* add more types as needed */
606 printk(TPACPI_ERR
"acpi_evalf() called "
607 "with invalid format character '%c'\n", res_type
);
611 if (!success
&& !quiet
)
612 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
613 method
, fmt0
, status
);
618 static int acpi_ec_read(int i
, u8
*p
)
623 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
627 if (ec_read(i
, p
) < 0)
634 static int acpi_ec_write(int i
, u8 v
)
637 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
640 if (ec_write(i
, v
) < 0)
647 static int issue_thinkpad_cmos_command(int cmos_cmd
)
652 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
658 /*************************************************************************
662 #define TPACPI_ACPIHANDLE_INIT(object) \
663 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
664 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
666 static void drv_acpi_handle_init(char *name
,
667 acpi_handle
*handle
, acpi_handle parent
,
668 char **paths
, int num_paths
, char **path
)
673 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
676 for (i
= 0; i
< num_paths
; i
++) {
677 status
= acpi_get_handle(parent
, paths
[i
], handle
);
678 if (ACPI_SUCCESS(status
)) {
680 dbg_printk(TPACPI_DBG_INIT
,
681 "Found ACPI handle %s for %s\n",
687 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
692 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
694 struct ibm_struct
*ibm
= data
;
696 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
699 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
702 ibm
->acpi
->notify(ibm
, event
);
705 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
712 if (!*ibm
->acpi
->handle
)
715 vdbg_printk(TPACPI_DBG_INIT
,
716 "setting up ACPI notify for %s\n", ibm
->name
);
718 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
720 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
725 ibm
->acpi
->device
->driver_data
= ibm
;
726 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
727 TPACPI_ACPI_EVENT_PREFIX
,
730 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
731 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
732 if (ACPI_FAILURE(status
)) {
733 if (status
== AE_ALREADY_EXISTS
) {
735 "another device driver is already "
736 "handling %s events\n", ibm
->name
);
739 "acpi_install_notify_handler(%s) failed: %d\n",
744 ibm
->flags
.acpi_notify_installed
= 1;
748 static int __init
tpacpi_device_add(struct acpi_device
*device
)
753 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
757 dbg_printk(TPACPI_DBG_INIT
,
758 "registering %s as an ACPI driver\n", ibm
->name
);
762 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
763 if (!ibm
->acpi
->driver
) {
765 "failed to allocate memory for ibm->acpi->driver\n");
769 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
770 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
772 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
774 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
776 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
778 kfree(ibm
->acpi
->driver
);
779 ibm
->acpi
->driver
= NULL
;
781 ibm
->flags
.acpi_driver_registered
= 1;
787 /****************************************************************************
788 ****************************************************************************
792 ****************************************************************************
793 ****************************************************************************/
795 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
796 int count
, int *eof
, void *data
)
798 struct ibm_struct
*ibm
= data
;
801 if (!ibm
|| !ibm
->read
)
804 len
= ibm
->read(page
);
808 if (len
<= off
+ count
)
820 static int dispatch_procfs_write(struct file
*file
,
821 const char __user
*userbuf
,
822 unsigned long count
, void *data
)
824 struct ibm_struct
*ibm
= data
;
828 if (!ibm
|| !ibm
->write
)
830 if (count
> PAGE_SIZE
- 2)
833 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
837 if (copy_from_user(kernbuf
, userbuf
, count
)) {
843 strcat(kernbuf
, ",");
844 ret
= ibm
->write(kernbuf
);
853 static char *next_cmd(char **cmds
)
858 while ((end
= strchr(start
, ',')) && end
== start
)
870 /****************************************************************************
871 ****************************************************************************
873 * Device model: input, hwmon and platform
875 ****************************************************************************
876 ****************************************************************************/
878 static struct platform_device
*tpacpi_pdev
;
879 static struct platform_device
*tpacpi_sensors_pdev
;
880 static struct device
*tpacpi_hwmon
;
881 static struct input_dev
*tpacpi_inputdev
;
882 static struct mutex tpacpi_inputdev_send_mutex
;
883 static LIST_HEAD(tpacpi_all_drivers
);
885 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
888 struct ibm_struct
*ibm
, *itmp
;
890 list_for_each_entry_safe(ibm
, itmp
,
894 (ibm
->suspend
)(state
);
900 static int tpacpi_resume_handler(struct platform_device
*pdev
)
902 struct ibm_struct
*ibm
, *itmp
;
904 list_for_each_entry_safe(ibm
, itmp
,
914 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
916 struct ibm_struct
*ibm
, *itmp
;
918 list_for_each_entry_safe(ibm
, itmp
,
926 static struct platform_driver tpacpi_pdriver
= {
928 .name
= TPACPI_DRVR_NAME
,
929 .owner
= THIS_MODULE
,
931 .suspend
= tpacpi_suspend_handler
,
932 .resume
= tpacpi_resume_handler
,
933 .shutdown
= tpacpi_shutdown_handler
,
936 static struct platform_driver tpacpi_hwmon_pdriver
= {
938 .name
= TPACPI_HWMON_DRVR_NAME
,
939 .owner
= THIS_MODULE
,
943 /*************************************************************************
944 * sysfs support helpers
947 struct attribute_set
{
948 unsigned int members
, max_members
;
949 struct attribute_group group
;
952 struct attribute_set_obj
{
953 struct attribute_set s
;
955 } __attribute__((packed
));
957 static struct attribute_set
*create_attr_set(unsigned int max_members
,
960 struct attribute_set_obj
*sobj
;
962 if (max_members
== 0)
965 /* Allocates space for implicit NULL at the end too */
966 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
967 max_members
* sizeof(struct attribute
*),
971 sobj
->s
.max_members
= max_members
;
972 sobj
->s
.group
.attrs
= &sobj
->a
;
973 sobj
->s
.group
.name
= name
;
978 #define destroy_attr_set(_set) \
981 /* not multi-threaded safe, use it in a single thread per set */
982 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
987 if (s
->members
>= s
->max_members
)
990 s
->group
.attrs
[s
->members
] = attr
;
996 static int add_many_to_attr_set(struct attribute_set
*s
,
997 struct attribute
**attr
,
1002 for (i
= 0; i
< count
; i
++) {
1003 res
= add_to_attr_set(s
, attr
[i
]);
1011 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
1013 sysfs_remove_group(kobj
, &s
->group
);
1014 destroy_attr_set(s
);
1017 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1018 sysfs_create_group(_kobj, &_attr_set->group)
1020 static int parse_strtoul(const char *buf
,
1021 unsigned long max
, unsigned long *value
)
1025 while (*buf
&& isspace(*buf
))
1027 *value
= simple_strtoul(buf
, &endp
, 0);
1028 while (*endp
&& isspace(*endp
))
1030 if (*endp
|| *value
> max
)
1036 static void tpacpi_disable_brightness_delay(void)
1038 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
1039 printk(TPACPI_NOTICE
1040 "ACPI backlight control delay disabled\n");
1043 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
1045 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1046 union acpi_object
*obj
;
1049 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
1050 obj
= (union acpi_object
*)buffer
.pointer
;
1051 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
1052 printk(TPACPI_ERR
"Unknown _BCL data, "
1053 "please report this to %s\n", TPACPI_MAIL
);
1056 rc
= obj
->package
.count
;
1062 kfree(buffer
.pointer
);
1066 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
1067 u32 lvl
, void *context
, void **rv
)
1069 char name
[ACPI_PATH_SEGMENT_LENGTH
];
1070 struct acpi_buffer buffer
= { sizeof(name
), &name
};
1072 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
1073 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
1074 BUG_ON(!rv
|| !*rv
);
1075 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
1076 return AE_CTRL_TERMINATE
;
1083 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1085 static int __init
tpacpi_check_std_acpi_brightness_support(void)
1089 void *bcl_ptr
= &bcl_levels
;
1092 TPACPI_ACPIHANDLE_INIT(vid
);
1098 * Search for a _BCL method, and execute it. This is safe on all
1099 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1100 * BIOS in ACPI backlight control mode. We do NOT have to care
1101 * about calling the _BCL method in an enabled video device, any
1102 * will do for our purposes.
1105 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
1106 tpacpi_acpi_walk_find_bcl
, NULL
,
1109 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
1110 tp_features
.bright_acpimode
= 1;
1111 return (bcl_levels
- 2);
1117 static int __init
tpacpi_new_rfkill(const unsigned int id
,
1118 struct rfkill
**rfk
,
1119 const enum rfkill_type rfktype
,
1121 const bool set_default
,
1122 int (*toggle_radio
)(void *, enum rfkill_state
),
1123 int (*get_state
)(void *, enum rfkill_state
*))
1126 enum rfkill_state initial_state
= RFKILL_STATE_SOFT_BLOCKED
;
1128 res
= get_state(NULL
, &initial_state
);
1131 "failed to read initial state for %s, error %d; "
1132 "will turn radio off\n", name
, res
);
1133 } else if (set_default
) {
1134 /* try to set the initial state as the default for the rfkill
1135 * type, since we ask the firmware to preserve it across S5 in
1137 if (rfkill_set_default(rfktype
,
1138 (initial_state
== RFKILL_STATE_UNBLOCKED
) ?
1139 RFKILL_STATE_UNBLOCKED
:
1140 RFKILL_STATE_SOFT_BLOCKED
) == -EPERM
)
1141 vdbg_printk(TPACPI_DBG_RFKILL
,
1142 "Default state for %s cannot be changed\n",
1146 *rfk
= rfkill_allocate(&tpacpi_pdev
->dev
, rfktype
);
1149 "failed to allocate memory for rfkill class\n");
1153 (*rfk
)->name
= name
;
1154 (*rfk
)->get_state
= get_state
;
1155 (*rfk
)->toggle_radio
= toggle_radio
;
1156 (*rfk
)->state
= initial_state
;
1158 res
= rfkill_register(*rfk
);
1161 "failed to register %s rfkill switch: %d\n",
1168 printk(TPACPI_INFO
"rfkill switch %s: radio is %sblocked\n",
1169 name
, (initial_state
== RFKILL_STATE_UNBLOCKED
) ?
1170 "un" : (initial_state
== RFKILL_STATE_SOFT_BLOCKED
) ?
1175 static void printk_deprecated_attribute(const char * const what
,
1176 const char * const details
)
1178 tpacpi_log_usertask("deprecated sysfs attribute");
1179 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1180 "will be removed. %s\n",
1184 static void printk_deprecated_rfkill_attribute(const char * const what
)
1186 printk_deprecated_attribute(what
,
1187 "Please switch to generic rfkill before year 2010");
1190 /*************************************************************************
1191 * thinkpad-acpi driver attributes
1194 /* interface_version --------------------------------------------------- */
1195 static ssize_t
tpacpi_driver_interface_version_show(
1196 struct device_driver
*drv
,
1199 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1202 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1203 tpacpi_driver_interface_version_show
, NULL
);
1205 /* debug_level --------------------------------------------------------- */
1206 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1209 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1212 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1213 const char *buf
, size_t count
)
1217 if (parse_strtoul(buf
, 0xffff, &t
))
1225 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1226 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1228 /* version ------------------------------------------------------------- */
1229 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1232 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1233 TPACPI_DESC
, TPACPI_VERSION
);
1236 static DRIVER_ATTR(version
, S_IRUGO
,
1237 tpacpi_driver_version_show
, NULL
);
1239 /* --------------------------------------------------------------------- */
1241 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1243 static void tpacpi_send_radiosw_update(void);
1245 /* wlsw_emulstate ------------------------------------------------------ */
1246 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1249 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1252 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1253 const char *buf
, size_t count
)
1257 if (parse_strtoul(buf
, 1, &t
))
1260 if (tpacpi_wlsw_emulstate
!= t
) {
1261 tpacpi_wlsw_emulstate
= !!t
;
1262 tpacpi_send_radiosw_update();
1264 tpacpi_wlsw_emulstate
= !!t
;
1269 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1270 tpacpi_driver_wlsw_emulstate_show
,
1271 tpacpi_driver_wlsw_emulstate_store
);
1273 /* bluetooth_emulstate ------------------------------------------------- */
1274 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1275 struct device_driver
*drv
,
1278 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1281 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1282 struct device_driver
*drv
,
1283 const char *buf
, size_t count
)
1287 if (parse_strtoul(buf
, 1, &t
))
1290 tpacpi_bluetooth_emulstate
= !!t
;
1295 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1296 tpacpi_driver_bluetooth_emulstate_show
,
1297 tpacpi_driver_bluetooth_emulstate_store
);
1299 /* wwan_emulstate ------------------------------------------------- */
1300 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1301 struct device_driver
*drv
,
1304 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1307 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1308 struct device_driver
*drv
,
1309 const char *buf
, size_t count
)
1313 if (parse_strtoul(buf
, 1, &t
))
1316 tpacpi_wwan_emulstate
= !!t
;
1321 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1322 tpacpi_driver_wwan_emulstate_show
,
1323 tpacpi_driver_wwan_emulstate_store
);
1325 /* uwb_emulstate ------------------------------------------------- */
1326 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1327 struct device_driver
*drv
,
1330 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1333 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1334 struct device_driver
*drv
,
1335 const char *buf
, size_t count
)
1339 if (parse_strtoul(buf
, 1, &t
))
1342 tpacpi_uwb_emulstate
= !!t
;
1347 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1348 tpacpi_driver_uwb_emulstate_show
,
1349 tpacpi_driver_uwb_emulstate_store
);
1352 /* --------------------------------------------------------------------- */
1354 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1355 &driver_attr_debug_level
, &driver_attr_version
,
1356 &driver_attr_interface_version
,
1359 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1365 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1366 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1370 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1371 if (!res
&& dbg_wlswemul
)
1372 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1373 if (!res
&& dbg_bluetoothemul
)
1374 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1375 if (!res
&& dbg_wwanemul
)
1376 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1377 if (!res
&& dbg_uwbemul
)
1378 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1384 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1388 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1389 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1391 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1392 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1393 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1394 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1395 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1399 /*************************************************************************
1404 * Table of recommended minimum BIOS versions
1406 * Reasons for listing:
1407 * 1. Stable BIOS, listed because the unknown ammount of
1408 * bugs and bad ACPI behaviour on older versions
1410 * 2. BIOS or EC fw with known bugs that trigger on Linux
1412 * 3. BIOS with known reduced functionality in older versions
1414 * We recommend the latest BIOS and EC version.
1415 * We only support the latest BIOS and EC fw version as a rule.
1417 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1418 * Information from users in ThinkWiki
1420 * WARNING: we use this table also to detect that the machine is
1421 * a ThinkPad in some cases, so don't remove entries lightly.
1424 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1425 { .vendor = (__v), \
1426 .bios = TPID(__id1, __id2), \
1427 .ec = TPACPI_MATCH_ANY, \
1428 .quirks = TPACPI_MATCH_ANY << 16 \
1429 | (__bv1) << 8 | (__bv2) }
1431 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1432 __eid, __ev1, __ev2) \
1433 { .vendor = (__v), \
1434 .bios = TPID(__bid1, __bid2), \
1436 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1437 | (__bv1) << 8 | (__bv2) }
1439 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1440 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1442 /* Outdated IBM BIOSes often lack the EC id string */
1443 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1444 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1445 __bv1, __bv2, TPID(__id1, __id2), \
1447 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1448 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1451 /* Outdated IBM BIOSes often lack the EC id string */
1452 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1453 __eid1, __eid2, __ev1, __ev2) \
1454 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1455 __bv1, __bv2, TPID(__eid1, __eid2), \
1457 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1458 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1461 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1462 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1464 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1465 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1466 __bv1, __bv2, TPID(__id1, __id2), \
1469 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1470 __eid1, __eid2, __ev1, __ev2) \
1471 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1472 __bv1, __bv2, TPID(__eid1, __eid2), \
1475 static const struct tpacpi_quirk tpacpi_bios_version_qtable
[] __initconst
= {
1476 /* Numeric models ------------------ */
1477 /* FW MODEL BIOS VERS */
1478 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1479 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1480 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1481 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1482 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1483 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1484 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1485 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1486 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1488 /* A-series ------------------------- */
1489 /* FW MODEL BIOS VERS EC VERS */
1490 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1491 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1492 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1493 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1494 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1495 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1496 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1497 TPV_QI0('1', '3', '2', '0'), /* A22m */
1498 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1499 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1500 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1502 /* G-series ------------------------- */
1503 /* FW MODEL BIOS VERS */
1504 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1505 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1507 /* R-series, T-series --------------- */
1508 /* FW MODEL BIOS VERS EC VERS */
1509 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1510 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1511 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1512 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1513 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1514 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1515 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1516 T40/p, T41/p, T42/p (1) */
1517 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1518 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1519 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1520 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1522 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1523 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1524 TPV_QI0('1', '6', '3', '2'), /* T22 */
1525 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1526 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1527 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1529 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1530 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1531 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1533 /* BIOS FW BIOS VERS EC FW EC VERS */
1534 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1535 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1537 /* X-series ------------------------- */
1538 /* FW MODEL BIOS VERS EC VERS */
1539 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1540 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1541 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1542 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1543 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1544 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1545 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1547 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1548 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1550 /* (0) - older versions lack DMI EC fw string and functionality */
1551 /* (1) - older versions known to lack functionality */
1562 static void __init
tpacpi_check_outdated_fw(void)
1564 unsigned long fwvers
;
1565 u16 ec_version
, bios_version
;
1567 fwvers
= tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1568 ARRAY_SIZE(tpacpi_bios_version_qtable
));
1573 bios_version
= fwvers
& 0xffffU
;
1574 ec_version
= (fwvers
>> 16) & 0xffffU
;
1576 /* note that unknown versions are set to 0x0000 and we use that */
1577 if ((bios_version
> thinkpad_id
.bios_release
) ||
1578 (ec_version
> thinkpad_id
.ec_release
&&
1579 ec_version
!= TPACPI_MATCH_ANY
)) {
1581 * The changelogs would let us track down the exact
1582 * reason, but it is just too much of a pain to track
1583 * it. We only list BIOSes that are either really
1584 * broken, or really stable to begin with, so it is
1585 * best if the user upgrades the firmware anyway.
1588 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1590 "WARNING: This firmware may be missing critical bug "
1591 "fixes and/or important features\n");
1595 static bool __init
tpacpi_is_fw_known(void)
1597 return tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1598 ARRAY_SIZE(tpacpi_bios_version_qtable
)) != 0;
1601 /****************************************************************************
1602 ****************************************************************************
1606 ****************************************************************************
1607 ****************************************************************************/
1609 /*************************************************************************
1610 * thinkpad-acpi init subdriver
1613 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1615 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1616 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1618 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1619 (thinkpad_id
.bios_version_str
) ?
1620 thinkpad_id
.bios_version_str
: "unknown",
1621 (thinkpad_id
.ec_version_str
) ?
1622 thinkpad_id
.ec_version_str
: "unknown");
1624 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1625 printk(TPACPI_INFO
"%s %s, model %s\n",
1626 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1627 "IBM" : ((thinkpad_id
.vendor
==
1628 PCI_VENDOR_ID_LENOVO
) ?
1629 "Lenovo" : "Unknown vendor"),
1630 thinkpad_id
.model_str
,
1631 (thinkpad_id
.nummodel_str
) ?
1632 thinkpad_id
.nummodel_str
: "unknown");
1634 tpacpi_check_outdated_fw();
1638 static int thinkpad_acpi_driver_read(char *p
)
1642 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1643 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1648 static struct ibm_struct thinkpad_acpi_driver_data
= {
1650 .read
= thinkpad_acpi_driver_read
,
1653 /*************************************************************************
1658 * ThinkPad firmware event model
1660 * The ThinkPad firmware has two main event interfaces: normal ACPI
1661 * notifications (which follow the ACPI standard), and a private event
1664 * The private event interface also issues events for the hotkeys. As
1665 * the driver gained features, the event handling code ended up being
1666 * built around the hotkey subdriver. This will need to be refactored
1667 * to a more formal event API eventually.
1669 * Some "hotkeys" are actually supposed to be used as event reports,
1670 * such as "brightness has changed", "volume has changed", depending on
1671 * the ThinkPad model and how the firmware is operating.
1673 * Unlike other classes, hotkey-class events have mask/unmask control on
1674 * non-ancient firmware. However, how it behaves changes a lot with the
1675 * firmware model and version.
1678 enum { /* hot key scan codes (derived from ACPI DSDT) */
1679 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1680 TP_ACPI_HOTKEYSCAN_FNF2
,
1681 TP_ACPI_HOTKEYSCAN_FNF3
,
1682 TP_ACPI_HOTKEYSCAN_FNF4
,
1683 TP_ACPI_HOTKEYSCAN_FNF5
,
1684 TP_ACPI_HOTKEYSCAN_FNF6
,
1685 TP_ACPI_HOTKEYSCAN_FNF7
,
1686 TP_ACPI_HOTKEYSCAN_FNF8
,
1687 TP_ACPI_HOTKEYSCAN_FNF9
,
1688 TP_ACPI_HOTKEYSCAN_FNF10
,
1689 TP_ACPI_HOTKEYSCAN_FNF11
,
1690 TP_ACPI_HOTKEYSCAN_FNF12
,
1691 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1692 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1693 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1694 TP_ACPI_HOTKEYSCAN_FNHOME
,
1695 TP_ACPI_HOTKEYSCAN_FNEND
,
1696 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1697 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1698 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1699 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1700 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1701 TP_ACPI_HOTKEYSCAN_MUTE
,
1702 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1705 enum { /* Keys/events available through NVRAM polling */
1706 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1707 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1710 enum { /* Positions of some of the keys in hotkey masks */
1711 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1712 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1713 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1714 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1715 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1716 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1717 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1718 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1719 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1720 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1721 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1724 enum { /* NVRAM to ACPI HKEY group map */
1725 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1726 TP_ACPI_HKEY_ZOOM_MASK
|
1727 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1728 TP_ACPI_HKEY_HIBERNATE_MASK
,
1729 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1730 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1731 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1732 TP_ACPI_HKEY_VOLDWN_MASK
|
1733 TP_ACPI_HKEY_MUTE_MASK
,
1736 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1737 struct tp_nvram_state
{
1738 u16 thinkpad_toggle
:1;
1740 u16 display_toggle
:1;
1741 u16 thinklight_toggle
:1;
1742 u16 hibernate_toggle
:1;
1743 u16 displayexp_toggle
:1;
1744 u16 display_state
:1;
1745 u16 brightness_toggle
:1;
1746 u16 volume_toggle
:1;
1749 u8 brightness_level
;
1753 /* kthread for the hotkey poller */
1754 static struct task_struct
*tpacpi_hotkey_task
;
1756 /* Acquired while the poller kthread is running, use to sync start/stop */
1757 static struct mutex hotkey_thread_mutex
;
1760 * Acquire mutex to write poller control variables as an
1763 * Increment hotkey_config_change when changing them if you
1764 * want the kthread to forget old state.
1766 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1768 static struct mutex hotkey_thread_data_mutex
;
1769 static unsigned int hotkey_config_change
;
1772 * hotkey poller control variables
1774 * Must be atomic or readers will also need to acquire mutex
1776 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1777 * should be used only when the changes need to be taken as
1778 * a block, OR when one needs to force the kthread to forget
1781 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1782 static unsigned int hotkey_poll_freq
= 10; /* Hz */
1784 #define HOTKEY_CONFIG_CRITICAL_START \
1786 mutex_lock(&hotkey_thread_data_mutex); \
1787 hotkey_config_change++; \
1789 #define HOTKEY_CONFIG_CRITICAL_END \
1790 mutex_unlock(&hotkey_thread_data_mutex);
1792 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1794 #define hotkey_source_mask 0U
1795 #define HOTKEY_CONFIG_CRITICAL_START
1796 #define HOTKEY_CONFIG_CRITICAL_END
1798 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1800 static struct mutex hotkey_mutex
;
1802 static enum { /* Reasons for waking up */
1803 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1804 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1805 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1806 } hotkey_wakeup_reason
;
1808 static int hotkey_autosleep_ack
;
1810 static u32 hotkey_orig_mask
; /* events the BIOS had enabled */
1811 static u32 hotkey_all_mask
; /* all events supported in fw */
1812 static u32 hotkey_reserved_mask
; /* events better left disabled */
1813 static u32 hotkey_driver_mask
; /* events needed by the driver */
1814 static u32 hotkey_user_mask
; /* events visible to userspace */
1815 static u32 hotkey_acpi_mask
; /* events enabled in firmware */
1817 static unsigned int hotkey_report_mode
;
1819 static u16
*hotkey_keycode_map
;
1821 static struct attribute_set
*hotkey_dev_attributes
;
1823 static void tpacpi_driver_event(const unsigned int hkey_event
);
1824 static void hotkey_driver_event(const unsigned int scancode
);
1826 /* HKEY.MHKG() return bits */
1827 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1829 static int hotkey_get_wlsw(int *status
)
1831 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1833 *status
= !!tpacpi_wlsw_emulstate
;
1837 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1842 static int hotkey_get_tablet_mode(int *status
)
1846 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1849 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1854 * Reads current event mask from firmware, and updates
1855 * hotkey_acpi_mask accordingly. Also resets any bits
1856 * from hotkey_user_mask that are unavailable to be
1857 * delivered (shadow requirement of the userspace ABI).
1859 * Call with hotkey_mutex held
1861 static int hotkey_mask_get(void)
1863 if (tp_features
.hotkey_mask
) {
1866 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1869 hotkey_acpi_mask
= m
;
1871 /* no mask support doesn't mean no event support... */
1872 hotkey_acpi_mask
= hotkey_all_mask
;
1875 /* sync userspace-visible mask */
1876 hotkey_user_mask
&= (hotkey_acpi_mask
| hotkey_source_mask
);
1881 void static hotkey_mask_warn_incomplete_mask(void)
1883 /* log only what the user can fix... */
1884 const u32 wantedmask
= hotkey_driver_mask
&
1885 ~(hotkey_acpi_mask
| hotkey_source_mask
) &
1886 (hotkey_all_mask
| TPACPI_HKEY_NVRAM_KNOWN_MASK
);
1889 printk(TPACPI_NOTICE
1890 "required events 0x%08x not enabled!\n",
1895 * Set the firmware mask when supported
1897 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1899 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1901 * Call with hotkey_mutex held
1903 static int hotkey_mask_set(u32 mask
)
1908 const u32 fwmask
= mask
& ~hotkey_source_mask
;
1910 if (tp_features
.hotkey_mask
) {
1911 for (i
= 0; i
< 32; i
++) {
1912 if (!acpi_evalf(hkey_handle
,
1913 NULL
, "MHKM", "vdd", i
+ 1,
1914 !!(mask
& (1 << i
)))) {
1922 * We *must* make an inconditional call to hotkey_mask_get to
1923 * refresh hotkey_acpi_mask and update hotkey_user_mask
1925 * Take the opportunity to also log when we cannot _enable_
1928 if (!hotkey_mask_get() && !rc
&& (fwmask
& ~hotkey_acpi_mask
)) {
1929 printk(TPACPI_NOTICE
1930 "asked for hotkey mask 0x%08x, but "
1931 "firmware forced it to 0x%08x\n",
1932 fwmask
, hotkey_acpi_mask
);
1935 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
1936 hotkey_mask_warn_incomplete_mask();
1942 * Sets hotkey_user_mask and tries to set the firmware mask
1944 * Call with hotkey_mutex held
1946 static int hotkey_user_mask_set(const u32 mask
)
1950 /* Give people a chance to notice they are doing something that
1951 * is bound to go boom on their users sooner or later */
1952 if (!tp_warned
.hotkey_mask_ff
&&
1953 (mask
== 0xffff || mask
== 0xffffff ||
1954 mask
== 0xffffffff)) {
1955 tp_warned
.hotkey_mask_ff
= 1;
1956 printk(TPACPI_NOTICE
1957 "setting the hotkey mask to 0x%08x is likely "
1958 "not the best way to go about it\n", mask
);
1959 printk(TPACPI_NOTICE
1960 "please consider using the driver defaults, "
1961 "and refer to up-to-date thinkpad-acpi "
1965 /* Try to enable what the user asked for, plus whatever we need.
1966 * this syncs everything but won't enable bits in hotkey_user_mask */
1967 rc
= hotkey_mask_set((mask
| hotkey_driver_mask
) & ~hotkey_source_mask
);
1969 /* Enable the available bits in hotkey_user_mask */
1970 hotkey_user_mask
= mask
& (hotkey_acpi_mask
| hotkey_source_mask
);
1976 * Sets the driver hotkey mask.
1978 * Can be called even if the hotkey subdriver is inactive
1980 static int tpacpi_hotkey_driver_mask_set(const u32 mask
)
1984 /* Do the right thing if hotkey_init has not been called yet */
1985 if (!tp_features
.hotkey
) {
1986 hotkey_driver_mask
= mask
;
1990 mutex_lock(&hotkey_mutex
);
1992 HOTKEY_CONFIG_CRITICAL_START
1993 hotkey_driver_mask
= mask
;
1994 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1995 hotkey_source_mask
|= (mask
& ~hotkey_all_mask
);
1997 HOTKEY_CONFIG_CRITICAL_END
1999 rc
= hotkey_mask_set((hotkey_acpi_mask
| hotkey_driver_mask
) &
2000 ~hotkey_source_mask
);
2001 mutex_unlock(&hotkey_mutex
);
2006 static int hotkey_status_get(int *status
)
2008 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2014 static int hotkey_status_set(bool enable
)
2016 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2022 static void tpacpi_input_send_tabletsw(void)
2026 if (tp_features
.hotkey_tablet
&&
2027 !hotkey_get_tablet_mode(&state
)) {
2028 mutex_lock(&tpacpi_inputdev_send_mutex
);
2030 input_report_switch(tpacpi_inputdev
,
2031 SW_TABLET_MODE
, !!state
);
2032 input_sync(tpacpi_inputdev
);
2034 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2038 /* Do NOT call without validating scancode first */
2039 static void tpacpi_input_send_key(const unsigned int scancode
)
2041 const unsigned int keycode
= hotkey_keycode_map
[scancode
];
2043 if (keycode
!= KEY_RESERVED
) {
2044 mutex_lock(&tpacpi_inputdev_send_mutex
);
2046 input_report_key(tpacpi_inputdev
, keycode
, 1);
2047 if (keycode
== KEY_UNKNOWN
)
2048 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
2050 input_sync(tpacpi_inputdev
);
2052 input_report_key(tpacpi_inputdev
, keycode
, 0);
2053 if (keycode
== KEY_UNKNOWN
)
2054 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
2056 input_sync(tpacpi_inputdev
);
2058 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2062 /* Do NOT call without validating scancode first */
2063 static void tpacpi_input_send_key_masked(const unsigned int scancode
)
2065 hotkey_driver_event(scancode
);
2066 if (hotkey_user_mask
& (1 << scancode
))
2067 tpacpi_input_send_key(scancode
);
2070 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2071 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
2073 /* Do NOT call without validating scancode first */
2074 static void tpacpi_hotkey_send_key(unsigned int scancode
)
2076 tpacpi_input_send_key_masked(scancode
);
2077 if (hotkey_report_mode
< 2) {
2078 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
2079 0x80, TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
2083 static void hotkey_read_nvram(struct tp_nvram_state
*n
, const u32 m
)
2087 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
2088 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
2089 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
2090 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
2091 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
2092 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
2094 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
2095 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
2096 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
2098 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
2099 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
2100 n
->displayexp_toggle
=
2101 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
2103 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
2104 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
2105 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
2106 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
2107 n
->brightness_toggle
=
2108 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
2110 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
2111 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
2112 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
2113 >> TP_NVRAM_POS_LEVEL_VOLUME
;
2114 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
2115 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
2119 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
2120 struct tp_nvram_state
*newn
,
2121 const u32 event_mask
)
2124 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2126 if ((event_mask & (1 << __scancode)) && \
2127 oldn->__member != newn->__member) \
2128 tpacpi_hotkey_send_key(__scancode); \
2131 #define TPACPI_MAY_SEND_KEY(__scancode) \
2133 if (event_mask & (1 << __scancode)) \
2134 tpacpi_hotkey_send_key(__scancode); \
2137 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
2138 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
2139 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
2140 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
2142 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
2144 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
2147 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
2148 if (oldn
->mute
!= newn
->mute
) {
2149 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2151 if (oldn
->volume_level
> newn
->volume_level
) {
2152 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2153 } else if (oldn
->volume_level
< newn
->volume_level
) {
2154 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2155 } else if (oldn
->mute
== newn
->mute
) {
2156 /* repeated key presses that didn't change state */
2158 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2159 } else if (newn
->volume_level
!= 0) {
2160 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2162 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2167 /* handle brightness */
2168 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
2169 if (oldn
->brightness_level
< newn
->brightness_level
) {
2170 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2171 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
2172 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2174 /* repeated key presses that didn't change state */
2175 if (newn
->brightness_level
!= 0) {
2176 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2178 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2183 #undef TPACPI_COMPARE_KEY
2184 #undef TPACPI_MAY_SEND_KEY
2190 * We track all events in hotkey_source_mask all the time, since
2191 * most of them are edge-based. We only issue those requested by
2192 * hotkey_user_mask or hotkey_driver_mask, though.
2194 static int hotkey_kthread(void *data
)
2196 struct tp_nvram_state s
[2];
2197 u32 poll_mask
, event_mask
;
2198 unsigned int si
, so
;
2200 unsigned int change_detector
, must_reset
;
2201 unsigned int poll_freq
;
2203 mutex_lock(&hotkey_thread_mutex
);
2205 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
2214 /* Initial state for compares */
2215 mutex_lock(&hotkey_thread_data_mutex
);
2216 change_detector
= hotkey_config_change
;
2217 poll_mask
= hotkey_source_mask
;
2218 event_mask
= hotkey_source_mask
&
2219 (hotkey_driver_mask
| hotkey_user_mask
);
2220 poll_freq
= hotkey_poll_freq
;
2221 mutex_unlock(&hotkey_thread_data_mutex
);
2222 hotkey_read_nvram(&s
[so
], poll_mask
);
2224 while (!kthread_should_stop()) {
2226 if (likely(poll_freq
))
2229 t
= 100; /* should never happen... */
2231 t
= msleep_interruptible(t
);
2232 if (unlikely(kthread_should_stop()))
2234 must_reset
= try_to_freeze();
2235 if (t
> 0 && !must_reset
)
2238 mutex_lock(&hotkey_thread_data_mutex
);
2239 if (must_reset
|| hotkey_config_change
!= change_detector
) {
2240 /* forget old state on thaw or config change */
2243 change_detector
= hotkey_config_change
;
2245 poll_mask
= hotkey_source_mask
;
2246 event_mask
= hotkey_source_mask
&
2247 (hotkey_driver_mask
| hotkey_user_mask
);
2248 poll_freq
= hotkey_poll_freq
;
2249 mutex_unlock(&hotkey_thread_data_mutex
);
2251 if (likely(poll_mask
)) {
2252 hotkey_read_nvram(&s
[si
], poll_mask
);
2253 if (likely(si
!= so
)) {
2254 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
2264 mutex_unlock(&hotkey_thread_mutex
);
2268 /* call with hotkey_mutex held */
2269 static void hotkey_poll_stop_sync(void)
2271 if (tpacpi_hotkey_task
) {
2272 if (frozen(tpacpi_hotkey_task
) ||
2273 freezing(tpacpi_hotkey_task
))
2274 thaw_process(tpacpi_hotkey_task
);
2276 kthread_stop(tpacpi_hotkey_task
);
2277 tpacpi_hotkey_task
= NULL
;
2278 mutex_lock(&hotkey_thread_mutex
);
2279 /* at this point, the thread did exit */
2280 mutex_unlock(&hotkey_thread_mutex
);
2284 /* call with hotkey_mutex held */
2285 static void hotkey_poll_setup(bool may_warn
)
2287 const u32 poll_driver_mask
= hotkey_driver_mask
& hotkey_source_mask
;
2288 const u32 poll_user_mask
= hotkey_user_mask
& hotkey_source_mask
;
2290 if (hotkey_poll_freq
> 0 &&
2291 (poll_driver_mask
||
2292 (poll_user_mask
&& tpacpi_inputdev
->users
> 0))) {
2293 if (!tpacpi_hotkey_task
) {
2294 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
2295 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
2296 if (IS_ERR(tpacpi_hotkey_task
)) {
2297 tpacpi_hotkey_task
= NULL
;
2299 "could not create kernel thread "
2300 "for hotkey polling\n");
2304 hotkey_poll_stop_sync();
2305 if (may_warn
&& (poll_driver_mask
|| poll_user_mask
) &&
2306 hotkey_poll_freq
== 0) {
2307 printk(TPACPI_NOTICE
2308 "hot keys 0x%08x and/or events 0x%08x "
2309 "require polling, which is currently "
2311 poll_user_mask
, poll_driver_mask
);
2316 static void hotkey_poll_setup_safe(bool may_warn
)
2318 mutex_lock(&hotkey_mutex
);
2319 hotkey_poll_setup(may_warn
);
2320 mutex_unlock(&hotkey_mutex
);
2323 /* call with hotkey_mutex held */
2324 static void hotkey_poll_set_freq(unsigned int freq
)
2327 hotkey_poll_stop_sync();
2329 hotkey_poll_freq
= freq
;
2332 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2334 static void hotkey_poll_setup_safe(bool __unused
)
2338 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2340 static int hotkey_inputdev_open(struct input_dev
*dev
)
2342 switch (tpacpi_lifecycle
) {
2343 case TPACPI_LIFE_INIT
:
2345 * hotkey_init will call hotkey_poll_setup_safe
2346 * at the appropriate moment
2349 case TPACPI_LIFE_EXITING
:
2351 case TPACPI_LIFE_RUNNING
:
2352 hotkey_poll_setup_safe(false);
2356 /* Should only happen if tpacpi_lifecycle is corrupt */
2361 static void hotkey_inputdev_close(struct input_dev
*dev
)
2363 /* disable hotkey polling when possible */
2364 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
&&
2365 !(hotkey_source_mask
& hotkey_driver_mask
))
2366 hotkey_poll_setup_safe(false);
2369 /* sysfs hotkey enable ------------------------------------------------- */
2370 static ssize_t
hotkey_enable_show(struct device
*dev
,
2371 struct device_attribute
*attr
,
2376 printk_deprecated_attribute("hotkey_enable",
2377 "Hotkey reporting is always enabled");
2379 res
= hotkey_status_get(&status
);
2383 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
2386 static ssize_t
hotkey_enable_store(struct device
*dev
,
2387 struct device_attribute
*attr
,
2388 const char *buf
, size_t count
)
2392 printk_deprecated_attribute("hotkey_enable",
2393 "Hotkeys can be disabled through hotkey_mask");
2395 if (parse_strtoul(buf
, 1, &t
))
2404 static struct device_attribute dev_attr_hotkey_enable
=
2405 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
2406 hotkey_enable_show
, hotkey_enable_store
);
2408 /* sysfs hotkey mask --------------------------------------------------- */
2409 static ssize_t
hotkey_mask_show(struct device
*dev
,
2410 struct device_attribute
*attr
,
2413 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_user_mask
);
2416 static ssize_t
hotkey_mask_store(struct device
*dev
,
2417 struct device_attribute
*attr
,
2418 const char *buf
, size_t count
)
2423 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2426 if (mutex_lock_killable(&hotkey_mutex
))
2427 return -ERESTARTSYS
;
2429 res
= hotkey_user_mask_set(t
);
2431 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2432 hotkey_poll_setup(true);
2435 mutex_unlock(&hotkey_mutex
);
2437 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2439 return (res
) ? res
: count
;
2442 static struct device_attribute dev_attr_hotkey_mask
=
2443 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
2444 hotkey_mask_show
, hotkey_mask_store
);
2446 /* sysfs hotkey bios_enabled ------------------------------------------- */
2447 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2448 struct device_attribute
*attr
,
2451 return sprintf(buf
, "0\n");
2454 static struct device_attribute dev_attr_hotkey_bios_enabled
=
2455 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
2457 /* sysfs hotkey bios_mask ---------------------------------------------- */
2458 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2459 struct device_attribute
*attr
,
2462 printk_deprecated_attribute("hotkey_bios_mask",
2463 "This attribute is useless.");
2464 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2467 static struct device_attribute dev_attr_hotkey_bios_mask
=
2468 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
2470 /* sysfs hotkey all_mask ----------------------------------------------- */
2471 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2472 struct device_attribute
*attr
,
2475 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2476 hotkey_all_mask
| hotkey_source_mask
);
2479 static struct device_attribute dev_attr_hotkey_all_mask
=
2480 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2482 /* sysfs hotkey recommended_mask --------------------------------------- */
2483 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2484 struct device_attribute
*attr
,
2487 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2488 (hotkey_all_mask
| hotkey_source_mask
)
2489 & ~hotkey_reserved_mask
);
2492 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2493 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2494 hotkey_recommended_mask_show
, NULL
);
2496 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2498 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2499 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2500 struct device_attribute
*attr
,
2503 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2506 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2507 struct device_attribute
*attr
,
2508 const char *buf
, size_t count
)
2514 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2515 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2518 if (mutex_lock_killable(&hotkey_mutex
))
2519 return -ERESTARTSYS
;
2521 HOTKEY_CONFIG_CRITICAL_START
2522 hotkey_source_mask
= t
;
2523 HOTKEY_CONFIG_CRITICAL_END
2525 rc
= hotkey_mask_set((hotkey_user_mask
| hotkey_driver_mask
) &
2526 ~hotkey_source_mask
);
2527 hotkey_poll_setup(true);
2529 /* check if events needed by the driver got disabled */
2530 r_ev
= hotkey_driver_mask
& ~(hotkey_acpi_mask
& hotkey_all_mask
)
2531 & ~hotkey_source_mask
& TPACPI_HKEY_NVRAM_KNOWN_MASK
;
2533 mutex_unlock(&hotkey_mutex
);
2536 printk(TPACPI_ERR
"hotkey_source_mask: failed to update the"
2537 "firmware event mask!\n");
2540 printk(TPACPI_NOTICE
"hotkey_source_mask: "
2541 "some important events were disabled: "
2544 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2546 return (rc
< 0) ? rc
: count
;
2549 static struct device_attribute dev_attr_hotkey_source_mask
=
2550 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2551 hotkey_source_mask_show
, hotkey_source_mask_store
);
2553 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2554 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2555 struct device_attribute
*attr
,
2558 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2561 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2562 struct device_attribute
*attr
,
2563 const char *buf
, size_t count
)
2567 if (parse_strtoul(buf
, 25, &t
))
2570 if (mutex_lock_killable(&hotkey_mutex
))
2571 return -ERESTARTSYS
;
2573 hotkey_poll_set_freq(t
);
2574 hotkey_poll_setup(true);
2576 mutex_unlock(&hotkey_mutex
);
2578 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2583 static struct device_attribute dev_attr_hotkey_poll_freq
=
2584 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2585 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2587 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2589 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2590 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2591 struct device_attribute
*attr
,
2595 res
= hotkey_get_wlsw(&s
);
2599 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2602 static struct device_attribute dev_attr_hotkey_radio_sw
=
2603 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2605 static void hotkey_radio_sw_notify_change(void)
2607 if (tp_features
.hotkey_wlsw
)
2608 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2612 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2613 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2614 struct device_attribute
*attr
,
2618 res
= hotkey_get_tablet_mode(&s
);
2622 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2625 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2626 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2628 static void hotkey_tablet_mode_notify_change(void)
2630 if (tp_features
.hotkey_tablet
)
2631 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2632 "hotkey_tablet_mode");
2635 /* sysfs hotkey report_mode -------------------------------------------- */
2636 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2637 struct device_attribute
*attr
,
2640 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2641 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2644 static struct device_attribute dev_attr_hotkey_report_mode
=
2645 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2647 /* sysfs wakeup reason (pollable) -------------------------------------- */
2648 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2649 struct device_attribute
*attr
,
2652 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2655 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2656 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2658 static void hotkey_wakeup_reason_notify_change(void)
2660 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2664 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2665 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2666 struct device_attribute
*attr
,
2669 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2672 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2673 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2674 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2676 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2678 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2679 "wakeup_hotunplug_complete");
2682 /* --------------------------------------------------------------------- */
2684 static struct attribute
*hotkey_attributes
[] __initdata
= {
2685 &dev_attr_hotkey_enable
.attr
,
2686 &dev_attr_hotkey_bios_enabled
.attr
,
2687 &dev_attr_hotkey_bios_mask
.attr
,
2688 &dev_attr_hotkey_report_mode
.attr
,
2689 &dev_attr_hotkey_wakeup_reason
.attr
,
2690 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2691 &dev_attr_hotkey_mask
.attr
,
2692 &dev_attr_hotkey_all_mask
.attr
,
2693 &dev_attr_hotkey_recommended_mask
.attr
,
2694 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2695 &dev_attr_hotkey_source_mask
.attr
,
2696 &dev_attr_hotkey_poll_freq
.attr
,
2700 static void bluetooth_update_rfk(void);
2701 static void wan_update_rfk(void);
2702 static void uwb_update_rfk(void);
2703 static void tpacpi_send_radiosw_update(void)
2707 /* Sync these BEFORE sending any rfkill events */
2708 if (tp_features
.bluetooth
)
2709 bluetooth_update_rfk();
2710 if (tp_features
.wan
)
2712 if (tp_features
.uwb
)
2715 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
2716 mutex_lock(&tpacpi_inputdev_send_mutex
);
2718 input_report_switch(tpacpi_inputdev
,
2719 SW_RFKILL_ALL
, !!wlsw
);
2720 input_sync(tpacpi_inputdev
);
2722 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2724 hotkey_radio_sw_notify_change();
2727 static void hotkey_exit(void)
2729 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2730 mutex_lock(&hotkey_mutex
);
2731 hotkey_poll_stop_sync();
2732 mutex_unlock(&hotkey_mutex
);
2735 if (hotkey_dev_attributes
)
2736 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2738 kfree(hotkey_keycode_map
);
2740 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
2741 "restoring original HKEY status and mask\n");
2742 /* yes, there is a bitwise or below, we want the
2743 * functions to be called even if one of them fail */
2744 if (((tp_features
.hotkey_mask
&&
2745 hotkey_mask_set(hotkey_orig_mask
)) |
2746 hotkey_status_set(false)) != 0)
2748 "failed to restore hot key mask "
2749 "to BIOS defaults\n");
2752 static void __init
hotkey_unmap(const unsigned int scancode
)
2754 if (hotkey_keycode_map
[scancode
] != KEY_RESERVED
) {
2755 clear_bit(hotkey_keycode_map
[scancode
],
2756 tpacpi_inputdev
->keybit
);
2757 hotkey_keycode_map
[scancode
] = KEY_RESERVED
;
2763 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2766 #define TPACPI_HK_Q_INIMASK 0x0001
2768 static const struct tpacpi_quirk tpacpi_hotkey_qtable
[] __initconst
= {
2769 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK
), /* 600E */
2770 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK
), /* 600E */
2771 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK
), /* 770, 770E, 770ED */
2772 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK
), /* A20m */
2773 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK
), /* A20p */
2774 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK
), /* A21e, A22e */
2775 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK
), /* A21e */
2776 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK
), /* A21m, A22m */
2777 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK
), /* A21p, A22p */
2778 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK
), /* A22e */
2779 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK
), /* A22m */
2780 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK
), /* A30/p (0) */
2781 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK
), /* R30 */
2782 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK
), /* R31 */
2783 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK
), /* T20 */
2784 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK
), /* T21 */
2785 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK
), /* T22 */
2786 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK
), /* X20, X21 */
2787 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK
), /* X22, X23, X24 */
2790 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2792 /* Requirements for changing the default keymaps:
2794 * 1. Many of the keys are mapped to KEY_RESERVED for very
2795 * good reasons. Do not change them unless you have deep
2796 * knowledge on the IBM and Lenovo ThinkPad firmware for
2797 * the various ThinkPad models. The driver behaves
2798 * differently for KEY_RESERVED: such keys have their
2799 * hot key mask *unset* in mask_recommended, and also
2800 * in the initial hot key mask programmed into the
2801 * firmware at driver load time, which means the firm-
2802 * ware may react very differently if you change them to
2805 * 2. You must be subscribed to the linux-thinkpad and
2806 * ibm-acpi-devel mailing lists, and you should read the
2807 * list archives since 2007 if you want to change the
2808 * keymaps. This requirement exists so that you will
2809 * know the past history of problems with the thinkpad-
2810 * acpi driver keymaps, and also that you will be
2811 * listening to any bug reports;
2813 * 3. Do not send thinkpad-acpi specific patches directly to
2814 * for merging, *ever*. Send them to the linux-acpi
2815 * mailinglist for comments. Merging is to be done only
2816 * through acpi-test and the ACPI maintainer.
2818 * If the above is too much to ask, don't change the keymap.
2819 * Ask the thinkpad-acpi maintainer to do it, instead.
2821 static u16 ibm_keycode_map
[] __initdata
= {
2822 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2823 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2824 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2825 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2827 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2828 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2829 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2830 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2832 /* brightness: firmware always reacts to them */
2833 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2834 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2836 /* Thinklight: firmware always react to it */
2837 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2839 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2840 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2842 /* Volume: firmware always react to it and reprograms
2843 * the built-in *extra* mixer. Never map it to control
2844 * another mixer by default. */
2845 KEY_RESERVED
, /* 0x14: VOLUME UP */
2846 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2847 KEY_RESERVED
, /* 0x16: MUTE */
2849 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2851 /* (assignments unknown, please report if found) */
2852 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2853 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2855 static u16 lenovo_keycode_map
[] __initdata
= {
2856 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2857 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2858 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2859 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2861 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2862 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2863 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2864 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2866 /* These should be enabled --only-- when ACPI video
2867 * is disabled (i.e. in "vendor" mode), and are handled
2868 * in a special way by the init code */
2869 KEY_BRIGHTNESSUP
, /* 0x0F: FN+HOME (brightness up) */
2870 KEY_BRIGHTNESSDOWN
, /* 0x10: FN+END (brightness down) */
2872 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2874 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2875 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2877 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2878 * react to it and reprograms the built-in *extra* mixer.
2879 * Never map it to control another mixer by default.
2881 * T60?, T61, R60?, R61: firmware and EC tries to send
2882 * these over the regular keyboard, so these are no-ops,
2883 * but there are still weird bugs re. MUTE, so do not
2884 * change unless you get test reports from all Lenovo
2885 * models. May cause the BIOS to interfere with the
2888 KEY_RESERVED
, /* 0x14: VOLUME UP */
2889 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2890 KEY_RESERVED
, /* 0x16: MUTE */
2892 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2894 /* (assignments unknown, please report if found) */
2895 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2896 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2899 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2900 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2901 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2906 bool radiosw_state
= false;
2907 bool tabletsw_state
= false;
2909 unsigned long quirks
;
2911 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2912 "initializing hotkey subdriver\n");
2914 BUG_ON(!tpacpi_inputdev
);
2915 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2916 tpacpi_inputdev
->close
!= NULL
);
2918 TPACPI_ACPIHANDLE_INIT(hkey
);
2919 mutex_init(&hotkey_mutex
);
2921 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2922 mutex_init(&hotkey_thread_mutex
);
2923 mutex_init(&hotkey_thread_data_mutex
);
2926 /* hotkey not supported on 570 */
2927 tp_features
.hotkey
= hkey_handle
!= NULL
;
2929 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2931 str_supported(tp_features
.hotkey
));
2933 if (!tp_features
.hotkey
)
2936 quirks
= tpacpi_check_quirks(tpacpi_hotkey_qtable
,
2937 ARRAY_SIZE(tpacpi_hotkey_qtable
));
2939 tpacpi_disable_brightness_delay();
2941 /* MUST have enough space for all attributes to be added to
2942 * hotkey_dev_attributes */
2943 hotkey_dev_attributes
= create_attr_set(
2944 ARRAY_SIZE(hotkey_attributes
) + 2,
2946 if (!hotkey_dev_attributes
)
2948 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2950 ARRAY_SIZE(hotkey_attributes
));
2954 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2955 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2956 for HKEY interface version 0x100 */
2957 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2958 if ((hkeyv
>> 8) != 1) {
2959 printk(TPACPI_ERR
"unknown version of the "
2960 "HKEY interface: 0x%x\n", hkeyv
);
2961 printk(TPACPI_ERR
"please report this to %s\n",
2965 * MHKV 0x100 in A31, R40, R40e,
2966 * T4x, X31, and later
2968 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2969 "firmware HKEY interface version: 0x%x\n",
2972 /* Paranoia check AND init hotkey_all_mask */
2973 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2976 "missing MHKA handler, "
2977 "please report this to %s\n",
2979 /* Fallback: pre-init for FN+F3,F4,F12 */
2980 hotkey_all_mask
= 0x080cU
;
2982 tp_features
.hotkey_mask
= 1;
2987 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2988 "hotkey masks are %s\n",
2989 str_supported(tp_features
.hotkey_mask
));
2991 /* Init hotkey_all_mask if not initialized yet */
2992 if (!tp_features
.hotkey_mask
&& !hotkey_all_mask
&&
2993 (quirks
& TPACPI_HK_Q_INIMASK
))
2994 hotkey_all_mask
= 0x080cU
; /* FN+F12, FN+F4, FN+F3 */
2996 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2997 if (tp_features
.hotkey_mask
) {
2998 /* hotkey_source_mask *must* be zero for
2999 * the first hotkey_mask_get to return hotkey_orig_mask */
3000 res
= hotkey_mask_get();
3004 hotkey_orig_mask
= hotkey_acpi_mask
;
3006 hotkey_orig_mask
= hotkey_all_mask
;
3007 hotkey_acpi_mask
= hotkey_all_mask
;
3010 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3012 tp_features
.hotkey_wlsw
= 1;
3013 radiosw_state
= !!tpacpi_wlsw_emulstate
;
3015 "radio switch emulation enabled\n");
3018 /* Not all thinkpads have a hardware radio switch */
3019 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
3020 tp_features
.hotkey_wlsw
= 1;
3021 radiosw_state
= !!status
;
3023 "radio switch found; radios are %s\n",
3024 enabled(status
, 0));
3026 if (tp_features
.hotkey_wlsw
)
3027 res
= add_to_attr_set(hotkey_dev_attributes
,
3028 &dev_attr_hotkey_radio_sw
.attr
);
3030 /* For X41t, X60t, X61t Tablets... */
3031 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
3032 tp_features
.hotkey_tablet
= 1;
3033 tabletsw_state
= !!(status
& TP_HOTKEY_TABLET_MASK
);
3035 "possible tablet mode switch found; "
3036 "ThinkPad in %s mode\n",
3037 (tabletsw_state
) ? "tablet" : "laptop");
3038 res
= add_to_attr_set(hotkey_dev_attributes
,
3039 &dev_attr_hotkey_tablet_mode
.attr
);
3043 res
= register_attr_set_with_sysfs(
3044 hotkey_dev_attributes
,
3045 &tpacpi_pdev
->dev
.kobj
);
3049 /* Set up key map */
3051 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
3053 if (!hotkey_keycode_map
) {
3055 "failed to allocate memory for key map\n");
3060 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
3061 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3062 "using Lenovo default hot key map\n");
3063 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
3064 TPACPI_HOTKEY_MAP_SIZE
);
3066 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3067 "using IBM default hot key map\n");
3068 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
3069 TPACPI_HOTKEY_MAP_SIZE
);
3072 input_set_capability(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
);
3073 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
3074 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
3075 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
3076 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
3077 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
3078 input_set_capability(tpacpi_inputdev
, EV_KEY
,
3079 hotkey_keycode_map
[i
]);
3081 if (i
< sizeof(hotkey_reserved_mask
)*8)
3082 hotkey_reserved_mask
|= 1 << i
;
3086 if (tp_features
.hotkey_wlsw
) {
3087 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_RFKILL_ALL
);
3088 input_report_switch(tpacpi_inputdev
,
3089 SW_RFKILL_ALL
, radiosw_state
);
3091 if (tp_features
.hotkey_tablet
) {
3092 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_TABLET_MODE
);
3093 input_report_switch(tpacpi_inputdev
,
3094 SW_TABLET_MODE
, tabletsw_state
);
3097 /* Do not issue duplicate brightness change events to
3099 if (!tp_features
.bright_acpimode
)
3100 /* update bright_acpimode... */
3101 tpacpi_check_std_acpi_brightness_support();
3103 if (tp_features
.bright_acpimode
&& acpi_video_backlight_support()) {
3105 "This ThinkPad has standard ACPI backlight "
3106 "brightness control, supported by the ACPI "
3108 printk(TPACPI_NOTICE
3109 "Disabling thinkpad-acpi brightness events "
3112 /* Disable brightness up/down on Lenovo thinkpads when
3113 * ACPI is handling them, otherwise it is plain impossible
3114 * for userspace to do something even remotely sane */
3115 hotkey_reserved_mask
|=
3116 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
3117 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
3118 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME
);
3119 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND
);
3122 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3123 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
3125 & ~hotkey_reserved_mask
;
3127 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3128 "hotkey source mask 0x%08x, polling freq %u\n",
3129 hotkey_source_mask
, hotkey_poll_freq
);
3132 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3133 "enabling firmware HKEY event interface...\n");
3134 res
= hotkey_status_set(true);
3139 res
= hotkey_mask_set(((hotkey_all_mask
& ~hotkey_reserved_mask
)
3140 | hotkey_driver_mask
)
3141 & ~hotkey_source_mask
);
3142 if (res
< 0 && res
!= -ENXIO
) {
3146 hotkey_user_mask
= (hotkey_acpi_mask
| hotkey_source_mask
)
3147 & ~hotkey_reserved_mask
;
3148 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3149 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3150 hotkey_user_mask
, hotkey_acpi_mask
, hotkey_source_mask
);
3152 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3153 "legacy ibm/hotkey event reporting over procfs %s\n",
3154 (hotkey_report_mode
< 2) ?
3155 "enabled" : "disabled");
3157 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
3158 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
3160 hotkey_poll_setup_safe(true);
3165 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3166 hotkey_dev_attributes
= NULL
;
3168 return (res
< 0)? res
: 1;
3171 static bool hotkey_notify_hotkey(const u32 hkey
,
3173 bool *ignore_acpi_ev
)
3175 /* 0x1000-0x1FFF: key presses */
3176 unsigned int scancode
= hkey
& 0xfff;
3177 *send_acpi_ev
= true;
3178 *ignore_acpi_ev
= false;
3180 if (scancode
> 0 && scancode
< 0x21) {
3182 if (!(hotkey_source_mask
& (1 << scancode
))) {
3183 tpacpi_input_send_key_masked(scancode
);
3184 *send_acpi_ev
= false;
3186 *ignore_acpi_ev
= true;
3193 static bool hotkey_notify_wakeup(const u32 hkey
,
3195 bool *ignore_acpi_ev
)
3197 /* 0x2000-0x2FFF: Wakeup reason */
3198 *send_acpi_ev
= true;
3199 *ignore_acpi_ev
= false;
3202 case TP_HKEY_EV_WKUP_S3_UNDOCK
: /* suspend, undock */
3203 case TP_HKEY_EV_WKUP_S4_UNDOCK
: /* hibernation, undock */
3204 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
3205 *ignore_acpi_ev
= true;
3208 case TP_HKEY_EV_WKUP_S3_BAYEJ
: /* suspend, bay eject */
3209 case TP_HKEY_EV_WKUP_S4_BAYEJ
: /* hibernation, bay eject */
3210 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
3211 *ignore_acpi_ev
= true;
3214 case TP_HKEY_EV_WKUP_S3_BATLOW
: /* Battery on critical low level/S3 */
3215 case TP_HKEY_EV_WKUP_S4_BATLOW
: /* Battery on critical low level/S4 */
3217 "EMERGENCY WAKEUP: battery almost empty\n");
3218 /* how to auto-heal: */
3219 /* 2313: woke up from S3, go to S4/S5 */
3220 /* 2413: woke up from S4, go to S5 */
3227 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
3229 "woke up due to a hot-unplug "
3231 hotkey_wakeup_reason_notify_change();
3236 static bool hotkey_notify_usrevent(const u32 hkey
,
3238 bool *ignore_acpi_ev
)
3240 /* 0x5000-0x5FFF: human interface helpers */
3241 *send_acpi_ev
= true;
3242 *ignore_acpi_ev
= false;
3245 case TP_HKEY_EV_PEN_INSERTED
: /* X61t: tablet pen inserted into bay */
3246 case TP_HKEY_EV_PEN_REMOVED
: /* X61t: tablet pen removed from bay */
3249 case TP_HKEY_EV_TABLET_TABLET
: /* X41t-X61t: tablet mode */
3250 case TP_HKEY_EV_TABLET_NOTEBOOK
: /* X41t-X61t: normal mode */
3251 tpacpi_input_send_tabletsw();
3252 hotkey_tablet_mode_notify_change();
3253 *send_acpi_ev
= false;
3256 case TP_HKEY_EV_LID_CLOSE
: /* Lid closed */
3257 case TP_HKEY_EV_LID_OPEN
: /* Lid opened */
3258 case TP_HKEY_EV_BRGHT_CHANGED
: /* brightness changed */
3259 /* do not propagate these events */
3260 *ignore_acpi_ev
= true;
3268 static void thermal_dump_all_sensors(void);
3270 static bool hotkey_notify_thermal(const u32 hkey
,
3272 bool *ignore_acpi_ev
)
3276 /* 0x6000-0x6FFF: thermal alarms */
3277 *send_acpi_ev
= true;
3278 *ignore_acpi_ev
= false;
3281 case TP_HKEY_EV_THM_TABLE_CHANGED
:
3283 "EC reports that Thermal Table has changed\n");
3284 /* recommended action: do nothing, we don't have
3285 * Lenovo ATM information */
3287 case TP_HKEY_EV_ALARM_BAT_HOT
:
3289 "THERMAL ALARM: battery is too hot!\n");
3290 /* recommended action: warn user through gui */
3292 case TP_HKEY_EV_ALARM_BAT_XHOT
:
3294 "THERMAL EMERGENCY: battery is extremely hot!\n");
3295 /* recommended action: immediate sleep/hibernate */
3297 case TP_HKEY_EV_ALARM_SENSOR_HOT
:
3300 "a sensor reports something is too hot!\n");
3301 /* recommended action: warn user through gui, that */
3302 /* some internal component is too hot */
3304 case TP_HKEY_EV_ALARM_SENSOR_XHOT
:
3306 "THERMAL EMERGENCY: "
3307 "a sensor reports something is extremely hot!\n");
3308 /* recommended action: immediate sleep/hibernate */
3312 "THERMAL ALERT: unknown thermal alarm received\n");
3316 thermal_dump_all_sensors();
3321 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
3325 bool ignore_acpi_ev
;
3328 if (event
!= 0x80) {
3330 "unknown HKEY notification event %d\n", event
);
3331 /* forward it to userspace, maybe it knows how to handle it */
3332 acpi_bus_generate_netlink_event(
3333 ibm
->acpi
->device
->pnp
.device_class
,
3334 dev_name(&ibm
->acpi
->device
->dev
),
3340 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
3341 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
3350 send_acpi_ev
= true;
3351 ignore_acpi_ev
= false;
3353 switch (hkey
>> 12) {
3355 /* 0x1000-0x1FFF: key presses */
3356 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
3360 /* 0x2000-0x2FFF: Wakeup reason */
3361 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
3365 /* 0x3000-0x3FFF: bay-related wakeups */
3366 if (hkey
== TP_HKEY_EV_BAYEJ_ACK
) {
3367 hotkey_autosleep_ack
= 1;
3370 hotkey_wakeup_hotunplug_complete_notify_change();
3377 /* 0x4000-0x4FFF: dock-related wakeups */
3378 if (hkey
== TP_HKEY_EV_UNDOCK_ACK
) {
3379 hotkey_autosleep_ack
= 1;
3382 hotkey_wakeup_hotunplug_complete_notify_change();
3389 /* 0x5000-0x5FFF: human interface helpers */
3390 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
3394 /* 0x6000-0x6FFF: thermal alarms */
3395 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
3399 /* 0x7000-0x7FFF: misc */
3400 if (tp_features
.hotkey_wlsw
&&
3401 hkey
== TP_HKEY_EV_RFKILL_CHANGED
) {
3402 tpacpi_send_radiosw_update();
3407 /* fallthrough to default */
3412 printk(TPACPI_NOTICE
3413 "unhandled HKEY event 0x%04x\n", hkey
);
3414 printk(TPACPI_NOTICE
3415 "please report the conditions when this "
3416 "event happened to %s\n", TPACPI_MAIL
);
3420 if (!ignore_acpi_ev
&&
3421 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
3422 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
3426 /* netlink events */
3427 if (!ignore_acpi_ev
&& send_acpi_ev
) {
3428 acpi_bus_generate_netlink_event(
3429 ibm
->acpi
->device
->pnp
.device_class
,
3430 dev_name(&ibm
->acpi
->device
->dev
),
3436 static void hotkey_suspend(pm_message_t state
)
3438 /* Do these on suspend, we get the events on early resume! */
3439 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
3440 hotkey_autosleep_ack
= 0;
3443 static void hotkey_resume(void)
3445 tpacpi_disable_brightness_delay();
3447 if (hotkey_status_set(true) < 0 ||
3448 hotkey_mask_set(hotkey_acpi_mask
) < 0)
3450 "error while attempting to reset the event "
3451 "firmware interface\n");
3453 tpacpi_send_radiosw_update();
3454 hotkey_tablet_mode_notify_change();
3455 hotkey_wakeup_reason_notify_change();
3456 hotkey_wakeup_hotunplug_complete_notify_change();
3457 hotkey_poll_setup_safe(false);
3460 /* procfs -------------------------------------------------------------- */
3461 static int hotkey_read(char *p
)
3466 if (!tp_features
.hotkey
) {
3467 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3471 if (mutex_lock_killable(&hotkey_mutex
))
3472 return -ERESTARTSYS
;
3473 res
= hotkey_status_get(&status
);
3475 res
= hotkey_mask_get();
3476 mutex_unlock(&hotkey_mutex
);
3480 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
3481 if (hotkey_all_mask
) {
3482 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_user_mask
);
3483 len
+= sprintf(p
+ len
,
3484 "commands:\tenable, disable, reset, <mask>\n");
3486 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
3487 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
3493 static void hotkey_enabledisable_warn(bool enable
)
3495 tpacpi_log_usertask("procfs hotkey enable/disable");
3496 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
3498 "hotkey enable/disable functionality has been "
3499 "removed from the driver. Hotkeys are always "
3502 "Please remove the hotkey=enable module "
3503 "parameter, it is deprecated. Hotkeys are always "
3507 static int hotkey_write(char *buf
)
3513 if (!tp_features
.hotkey
)
3516 if (mutex_lock_killable(&hotkey_mutex
))
3517 return -ERESTARTSYS
;
3519 mask
= hotkey_user_mask
;
3522 while ((cmd
= next_cmd(&buf
))) {
3523 if (strlencmp(cmd
, "enable") == 0) {
3524 hotkey_enabledisable_warn(1);
3525 } else if (strlencmp(cmd
, "disable") == 0) {
3526 hotkey_enabledisable_warn(0);
3528 } else if (strlencmp(cmd
, "reset") == 0) {
3529 mask
= (hotkey_all_mask
| hotkey_source_mask
)
3530 & ~hotkey_reserved_mask
;
3531 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
3533 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
3542 tpacpi_disclose_usertask("procfs hotkey",
3543 "set mask to 0x%08x\n", mask
);
3544 res
= hotkey_user_mask_set(mask
);
3548 mutex_unlock(&hotkey_mutex
);
3552 static const struct acpi_device_id ibm_htk_device_ids
[] = {
3553 {TPACPI_ACPI_HKEY_HID
, 0},
3557 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
3558 .hid
= ibm_htk_device_ids
,
3559 .notify
= hotkey_notify
,
3560 .handle
= &hkey_handle
,
3561 .type
= ACPI_DEVICE_NOTIFY
,
3564 static struct ibm_struct hotkey_driver_data
= {
3566 .read
= hotkey_read
,
3567 .write
= hotkey_write
,
3568 .exit
= hotkey_exit
,
3569 .resume
= hotkey_resume
,
3570 .suspend
= hotkey_suspend
,
3571 .acpi
= &ibm_hotkey_acpidriver
,
3574 /*************************************************************************
3575 * Bluetooth subdriver
3579 /* ACPI GBDC/SBDC bits */
3580 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3581 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3582 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3587 /* ACPI \BLTH commands */
3588 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3589 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3590 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3591 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3592 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3595 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3597 static struct rfkill
*tpacpi_bluetooth_rfkill
;
3599 static void bluetooth_suspend(pm_message_t state
)
3601 /* Try to make sure radio will resume powered off */
3602 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3603 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
))
3604 vdbg_printk(TPACPI_DBG_RFKILL
,
3605 "bluetooth power down on resume request failed\n");
3608 static int bluetooth_get_radiosw(void)
3612 if (!tp_features
.bluetooth
)
3615 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3616 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3617 return RFKILL_STATE_HARD_BLOCKED
;
3619 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3620 if (dbg_bluetoothemul
)
3621 return (tpacpi_bluetooth_emulstate
) ?
3622 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3625 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3628 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3629 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3632 static void bluetooth_update_rfk(void)
3636 if (!tpacpi_bluetooth_rfkill
)
3639 status
= bluetooth_get_radiosw();
3642 rfkill_force_state(tpacpi_bluetooth_rfkill
, status
);
3644 vdbg_printk(TPACPI_DBG_RFKILL
,
3645 "forced rfkill state to %d\n",
3649 static int bluetooth_set_radiosw(int radio_on
, int update_rfk
)
3653 if (!tp_features
.bluetooth
)
3656 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3657 * reason to risk weird behaviour. */
3658 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3662 vdbg_printk(TPACPI_DBG_RFKILL
,
3663 "will %s bluetooth\n", radio_on
? "enable" : "disable");
3665 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3666 if (dbg_bluetoothemul
) {
3667 tpacpi_bluetooth_emulstate
= !!radio_on
;
3669 bluetooth_update_rfk();
3674 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3676 status
= TP_ACPI_BLUETOOTH_RADIOSSW
;
3679 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3683 bluetooth_update_rfk();
3688 /* sysfs bluetooth enable ---------------------------------------------- */
3689 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3690 struct device_attribute
*attr
,
3695 printk_deprecated_rfkill_attribute("bluetooth_enable");
3697 status
= bluetooth_get_radiosw();
3701 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3702 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3705 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3706 struct device_attribute
*attr
,
3707 const char *buf
, size_t count
)
3712 printk_deprecated_rfkill_attribute("bluetooth_enable");
3714 if (parse_strtoul(buf
, 1, &t
))
3717 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t
);
3719 res
= bluetooth_set_radiosw(t
, 1);
3721 return (res
) ? res
: count
;
3724 static struct device_attribute dev_attr_bluetooth_enable
=
3725 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3726 bluetooth_enable_show
, bluetooth_enable_store
);
3728 /* --------------------------------------------------------------------- */
3730 static struct attribute
*bluetooth_attributes
[] = {
3731 &dev_attr_bluetooth_enable
.attr
,
3735 static const struct attribute_group bluetooth_attr_group
= {
3736 .attrs
= bluetooth_attributes
,
3739 static int tpacpi_bluetooth_rfk_get(void *data
, enum rfkill_state
*state
)
3741 int bts
= bluetooth_get_radiosw();
3750 static int tpacpi_bluetooth_rfk_set(void *data
, enum rfkill_state state
)
3752 dbg_printk(TPACPI_DBG_RFKILL
,
3753 "request to change radio state to %d\n", state
);
3754 return bluetooth_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3757 static void bluetooth_shutdown(void)
3759 /* Order firmware to save current state to NVRAM */
3760 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3761 TP_ACPI_BLTH_SAVE_STATE
))
3762 printk(TPACPI_NOTICE
3763 "failed to save bluetooth state to NVRAM\n");
3765 vdbg_printk(TPACPI_DBG_RFKILL
,
3766 "bluestooth state saved to NVRAM\n");
3769 static void bluetooth_exit(void)
3771 bluetooth_shutdown();
3773 if (tpacpi_bluetooth_rfkill
)
3774 rfkill_unregister(tpacpi_bluetooth_rfkill
);
3776 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3777 &bluetooth_attr_group
);
3780 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3785 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3786 "initializing bluetooth subdriver\n");
3788 TPACPI_ACPIHANDLE_INIT(hkey
);
3790 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3791 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3792 tp_features
.bluetooth
= hkey_handle
&&
3793 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
3795 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3796 "bluetooth is %s, status 0x%02x\n",
3797 str_supported(tp_features
.bluetooth
),
3800 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3801 if (dbg_bluetoothemul
) {
3802 tp_features
.bluetooth
= 1;
3804 "bluetooth switch emulation enabled\n");
3807 if (tp_features
.bluetooth
&&
3808 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
3809 /* no bluetooth hardware present in system */
3810 tp_features
.bluetooth
= 0;
3811 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3812 "bluetooth hardware not installed\n");
3815 if (!tp_features
.bluetooth
)
3818 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3819 &bluetooth_attr_group
);
3823 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
3824 &tpacpi_bluetooth_rfkill
,
3825 RFKILL_TYPE_BLUETOOTH
,
3826 TPACPI_RFK_BLUETOOTH_SW_NAME
,
3828 tpacpi_bluetooth_rfk_set
,
3829 tpacpi_bluetooth_rfk_get
);
3838 /* procfs -------------------------------------------------------------- */
3839 static int bluetooth_read(char *p
)
3842 int status
= bluetooth_get_radiosw();
3844 if (!tp_features
.bluetooth
)
3845 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3847 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3848 (status
== RFKILL_STATE_UNBLOCKED
) ?
3849 "enabled" : "disabled");
3850 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3856 static int bluetooth_write(char *buf
)
3861 if (!tp_features
.bluetooth
)
3864 while ((cmd
= next_cmd(&buf
))) {
3865 if (strlencmp(cmd
, "enable") == 0) {
3867 } else if (strlencmp(cmd
, "disable") == 0) {
3874 tpacpi_disclose_usertask("procfs bluetooth",
3876 state
? "enable" : "disable");
3877 bluetooth_set_radiosw(state
, 1);
3883 static struct ibm_struct bluetooth_driver_data
= {
3884 .name
= "bluetooth",
3885 .read
= bluetooth_read
,
3886 .write
= bluetooth_write
,
3887 .exit
= bluetooth_exit
,
3888 .suspend
= bluetooth_suspend
,
3889 .shutdown
= bluetooth_shutdown
,
3892 /*************************************************************************
3897 /* ACPI GWAN/SWAN bits */
3898 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
3899 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
3900 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
3904 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3906 static struct rfkill
*tpacpi_wan_rfkill
;
3908 static void wan_suspend(pm_message_t state
)
3910 /* Try to make sure radio will resume powered off */
3911 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "qvd",
3912 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
))
3913 vdbg_printk(TPACPI_DBG_RFKILL
,
3914 "WWAN power down on resume request failed\n");
3917 static int wan_get_radiosw(void)
3921 if (!tp_features
.wan
)
3924 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3925 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3926 return RFKILL_STATE_HARD_BLOCKED
;
3928 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3930 return (tpacpi_wwan_emulstate
) ?
3931 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3934 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
3937 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
3938 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3941 static void wan_update_rfk(void)
3945 if (!tpacpi_wan_rfkill
)
3948 status
= wan_get_radiosw();
3951 rfkill_force_state(tpacpi_wan_rfkill
, status
);
3953 vdbg_printk(TPACPI_DBG_RFKILL
,
3954 "forced rfkill state to %d\n",
3958 static int wan_set_radiosw(int radio_on
, int update_rfk
)
3962 if (!tp_features
.wan
)
3965 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3966 * reason to risk weird behaviour. */
3967 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3971 vdbg_printk(TPACPI_DBG_RFKILL
,
3972 "will %s WWAN\n", radio_on
? "enable" : "disable");
3974 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3976 tpacpi_wwan_emulstate
= !!radio_on
;
3983 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3985 status
= TP_ACPI_WANCARD_RADIOSSW
;
3988 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
3997 /* sysfs wan enable ---------------------------------------------------- */
3998 static ssize_t
wan_enable_show(struct device
*dev
,
3999 struct device_attribute
*attr
,
4004 printk_deprecated_rfkill_attribute("wwan_enable");
4006 status
= wan_get_radiosw();
4010 return snprintf(buf
, PAGE_SIZE
, "%d\n",
4011 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
4014 static ssize_t
wan_enable_store(struct device
*dev
,
4015 struct device_attribute
*attr
,
4016 const char *buf
, size_t count
)
4021 printk_deprecated_rfkill_attribute("wwan_enable");
4023 if (parse_strtoul(buf
, 1, &t
))
4026 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t
);
4028 res
= wan_set_radiosw(t
, 1);
4030 return (res
) ? res
: count
;
4033 static struct device_attribute dev_attr_wan_enable
=
4034 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
4035 wan_enable_show
, wan_enable_store
);
4037 /* --------------------------------------------------------------------- */
4039 static struct attribute
*wan_attributes
[] = {
4040 &dev_attr_wan_enable
.attr
,
4044 static const struct attribute_group wan_attr_group
= {
4045 .attrs
= wan_attributes
,
4048 static int tpacpi_wan_rfk_get(void *data
, enum rfkill_state
*state
)
4050 int wans
= wan_get_radiosw();
4059 static int tpacpi_wan_rfk_set(void *data
, enum rfkill_state state
)
4061 dbg_printk(TPACPI_DBG_RFKILL
,
4062 "request to change radio state to %d\n", state
);
4063 return wan_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
4066 static void wan_shutdown(void)
4068 /* Order firmware to save current state to NVRAM */
4069 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
4070 TP_ACPI_WGSV_SAVE_STATE
))
4071 printk(TPACPI_NOTICE
4072 "failed to save WWAN state to NVRAM\n");
4074 vdbg_printk(TPACPI_DBG_RFKILL
,
4075 "WWAN state saved to NVRAM\n");
4078 static void wan_exit(void)
4082 if (tpacpi_wan_rfkill
)
4083 rfkill_unregister(tpacpi_wan_rfkill
);
4085 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4089 static int __init
wan_init(struct ibm_init_struct
*iibm
)
4094 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4095 "initializing wan subdriver\n");
4097 TPACPI_ACPIHANDLE_INIT(hkey
);
4099 tp_features
.wan
= hkey_handle
&&
4100 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
4102 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4103 "wan is %s, status 0x%02x\n",
4104 str_supported(tp_features
.wan
),
4107 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4109 tp_features
.wan
= 1;
4111 "wwan switch emulation enabled\n");
4114 if (tp_features
.wan
&&
4115 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
4116 /* no wan hardware present in system */
4117 tp_features
.wan
= 0;
4118 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4119 "wan hardware not installed\n");
4122 if (!tp_features
.wan
)
4125 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4130 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
4133 TPACPI_RFK_WWAN_SW_NAME
,
4136 tpacpi_wan_rfk_get
);
4145 /* procfs -------------------------------------------------------------- */
4146 static int wan_read(char *p
)
4149 int status
= wan_get_radiosw();
4151 tpacpi_disclose_usertask("procfs wan", "read");
4153 if (!tp_features
.wan
)
4154 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4156 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
4157 (status
== RFKILL_STATE_UNBLOCKED
) ?
4158 "enabled" : "disabled");
4159 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
4165 static int wan_write(char *buf
)
4170 if (!tp_features
.wan
)
4173 while ((cmd
= next_cmd(&buf
))) {
4174 if (strlencmp(cmd
, "enable") == 0) {
4176 } else if (strlencmp(cmd
, "disable") == 0) {
4183 tpacpi_disclose_usertask("procfs wan",
4185 state
? "enable" : "disable");
4186 wan_set_radiosw(state
, 1);
4192 static struct ibm_struct wan_driver_data
= {
4197 .suspend
= wan_suspend
,
4198 .shutdown
= wan_shutdown
,
4201 /*************************************************************************
4206 /* ACPI GUWB/SUWB bits */
4207 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
4208 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
4211 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4213 static struct rfkill
*tpacpi_uwb_rfkill
;
4215 static int uwb_get_radiosw(void)
4219 if (!tp_features
.uwb
)
4222 /* WLSW overrides UWB in firmware/hardware, reflect that */
4223 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
4224 return RFKILL_STATE_HARD_BLOCKED
;
4226 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4228 return (tpacpi_uwb_emulstate
) ?
4229 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
4232 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
4235 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
4236 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
4239 static void uwb_update_rfk(void)
4243 if (!tpacpi_uwb_rfkill
)
4246 status
= uwb_get_radiosw();
4249 rfkill_force_state(tpacpi_uwb_rfkill
, status
);
4251 vdbg_printk(TPACPI_DBG_RFKILL
,
4252 "forced rfkill state to %d\n",
4256 static int uwb_set_radiosw(int radio_on
, int update_rfk
)
4260 if (!tp_features
.uwb
)
4263 /* WLSW overrides UWB in firmware/hardware, but there is no
4264 * reason to risk weird behaviour. */
4265 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
4269 vdbg_printk(TPACPI_DBG_RFKILL
,
4270 "will %s UWB\n", radio_on
? "enable" : "disable");
4272 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4274 tpacpi_uwb_emulstate
= !!radio_on
;
4281 status
= (radio_on
) ? TP_ACPI_UWB_RADIOSSW
: 0;
4282 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
4291 /* --------------------------------------------------------------------- */
4293 static int tpacpi_uwb_rfk_get(void *data
, enum rfkill_state
*state
)
4295 int uwbs
= uwb_get_radiosw();
4304 static int tpacpi_uwb_rfk_set(void *data
, enum rfkill_state state
)
4306 dbg_printk(TPACPI_DBG_RFKILL
,
4307 "request to change radio state to %d\n", state
);
4308 return uwb_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
4311 static void uwb_exit(void)
4313 if (tpacpi_uwb_rfkill
)
4314 rfkill_unregister(tpacpi_uwb_rfkill
);
4317 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
4322 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4323 "initializing uwb subdriver\n");
4325 TPACPI_ACPIHANDLE_INIT(hkey
);
4327 tp_features
.uwb
= hkey_handle
&&
4328 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
4330 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4331 "uwb is %s, status 0x%02x\n",
4332 str_supported(tp_features
.uwb
),
4335 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4337 tp_features
.uwb
= 1;
4339 "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
,
4359 tpacpi_uwb_rfk_get
);
4364 static struct ibm_struct uwb_driver_data
= {
4367 .flags
.experimental
= 1,
4370 /*************************************************************************
4374 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4376 enum video_access_mode
{
4377 TPACPI_VIDEO_NONE
= 0,
4378 TPACPI_VIDEO_570
, /* 570 */
4379 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
4380 TPACPI_VIDEO_NEW
, /* all others */
4383 enum { /* video status flags, based on VIDEO_570 */
4384 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
4385 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
4386 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
4389 enum { /* TPACPI_VIDEO_570 constants */
4390 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
4391 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
4392 * video_status_flags */
4393 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
4394 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
4397 static enum video_access_mode video_supported
;
4398 static int video_orig_autosw
;
4400 static int video_autosw_get(void);
4401 static int video_autosw_set(int enable
);
4403 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4405 static int __init
video_init(struct ibm_init_struct
*iibm
)
4409 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
4411 TPACPI_ACPIHANDLE_INIT(vid
);
4412 TPACPI_ACPIHANDLE_INIT(vid2
);
4414 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
4415 /* G41, assume IVGA doesn't change */
4416 vid_handle
= vid2_handle
;
4419 /* video switching not supported on R30, R31 */
4420 video_supported
= TPACPI_VIDEO_NONE
;
4421 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
4423 video_supported
= TPACPI_VIDEO_570
;
4424 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
4425 /* 600e/x, 770e, 770x */
4426 video_supported
= TPACPI_VIDEO_770
;
4429 video_supported
= TPACPI_VIDEO_NEW
;
4431 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
4432 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
4435 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
4438 static void video_exit(void)
4440 dbg_printk(TPACPI_DBG_EXIT
,
4441 "restoring original video autoswitch mode\n");
4442 if (video_autosw_set(video_orig_autosw
))
4443 printk(TPACPI_ERR
"error while trying to restore original "
4444 "video autoswitch mode\n");
4447 static int video_outputsw_get(void)
4452 switch (video_supported
) {
4453 case TPACPI_VIDEO_570
:
4454 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
4455 TP_ACPI_VIDEO_570_PHSCMD
))
4457 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
4459 case TPACPI_VIDEO_770
:
4460 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4463 status
|= TP_ACPI_VIDEO_S_LCD
;
4464 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4467 status
|= TP_ACPI_VIDEO_S_CRT
;
4469 case TPACPI_VIDEO_NEW
:
4470 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4471 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4474 status
|= TP_ACPI_VIDEO_S_CRT
;
4476 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4477 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4480 status
|= TP_ACPI_VIDEO_S_LCD
;
4481 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4484 status
|= TP_ACPI_VIDEO_S_DVI
;
4493 static int video_outputsw_set(int status
)
4498 switch (video_supported
) {
4499 case TPACPI_VIDEO_570
:
4500 res
= acpi_evalf(NULL
, NULL
,
4501 "\\_SB.PHS2", "vdd",
4502 TP_ACPI_VIDEO_570_PHS2CMD
,
4503 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4505 case TPACPI_VIDEO_770
:
4506 autosw
= video_autosw_get();
4510 res
= video_autosw_set(1);
4513 res
= acpi_evalf(vid_handle
, NULL
,
4514 "ASWT", "vdd", status
* 0x100, 0);
4515 if (!autosw
&& video_autosw_set(autosw
)) {
4517 "video auto-switch left enabled due to error\n");
4521 case TPACPI_VIDEO_NEW
:
4522 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
4523 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
4529 return (res
)? 0 : -EIO
;
4532 static int video_autosw_get(void)
4536 switch (video_supported
) {
4537 case TPACPI_VIDEO_570
:
4538 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
4541 case TPACPI_VIDEO_770
:
4542 case TPACPI_VIDEO_NEW
:
4543 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4553 static int video_autosw_set(int enable
)
4555 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
4560 static int video_outputsw_cycle(void)
4562 int autosw
= video_autosw_get();
4568 switch (video_supported
) {
4569 case TPACPI_VIDEO_570
:
4570 res
= video_autosw_set(1);
4573 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4575 case TPACPI_VIDEO_770
:
4576 case TPACPI_VIDEO_NEW
:
4577 res
= video_autosw_set(1);
4580 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4585 if (!autosw
&& video_autosw_set(autosw
)) {
4587 "video auto-switch left enabled due to error\n");
4591 return (res
)? 0 : -EIO
;
4594 static int video_expand_toggle(void)
4596 switch (video_supported
) {
4597 case TPACPI_VIDEO_570
:
4598 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4600 case TPACPI_VIDEO_770
:
4601 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4603 case TPACPI_VIDEO_NEW
:
4604 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4612 static int video_read(char *p
)
4617 if (video_supported
== TPACPI_VIDEO_NONE
) {
4618 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4622 status
= video_outputsw_get();
4626 autosw
= video_autosw_get();
4630 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4631 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
4632 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
4633 if (video_supported
== TPACPI_VIDEO_NEW
)
4634 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
4635 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
4636 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
4637 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
4638 if (video_supported
== TPACPI_VIDEO_NEW
)
4639 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
4640 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
4641 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4646 static int video_write(char *buf
)
4649 int enable
, disable
, status
;
4652 if (video_supported
== TPACPI_VIDEO_NONE
)
4658 while ((cmd
= next_cmd(&buf
))) {
4659 if (strlencmp(cmd
, "lcd_enable") == 0) {
4660 enable
|= TP_ACPI_VIDEO_S_LCD
;
4661 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4662 disable
|= TP_ACPI_VIDEO_S_LCD
;
4663 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4664 enable
|= TP_ACPI_VIDEO_S_CRT
;
4665 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4666 disable
|= TP_ACPI_VIDEO_S_CRT
;
4667 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4668 strlencmp(cmd
, "dvi_enable") == 0) {
4669 enable
|= TP_ACPI_VIDEO_S_DVI
;
4670 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4671 strlencmp(cmd
, "dvi_disable") == 0) {
4672 disable
|= TP_ACPI_VIDEO_S_DVI
;
4673 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4674 res
= video_autosw_set(1);
4677 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4678 res
= video_autosw_set(0);
4681 } else if (strlencmp(cmd
, "video_switch") == 0) {
4682 res
= video_outputsw_cycle();
4685 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4686 res
= video_expand_toggle();
4693 if (enable
|| disable
) {
4694 status
= video_outputsw_get();
4697 res
= video_outputsw_set((status
& ~disable
) | enable
);
4705 static struct ibm_struct video_driver_data
= {
4708 .write
= video_write
,
4712 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4714 /*************************************************************************
4715 * Light (thinklight) subdriver
4718 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4719 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4721 static int light_get_status(void)
4725 if (tp_features
.light_status
) {
4726 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4734 static int light_set_status(int status
)
4738 if (tp_features
.light
) {
4740 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4742 TP_CMOS_THINKLIGHT_ON
:
4743 TP_CMOS_THINKLIGHT_OFF
);
4745 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4748 return (rc
)? 0 : -EIO
;
4754 static void light_set_status_worker(struct work_struct
*work
)
4756 struct tpacpi_led_classdev
*data
=
4757 container_of(work
, struct tpacpi_led_classdev
, work
);
4759 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4760 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4763 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4764 enum led_brightness brightness
)
4766 struct tpacpi_led_classdev
*data
=
4767 container_of(led_cdev
,
4768 struct tpacpi_led_classdev
,
4770 data
->new_state
= (brightness
!= LED_OFF
) ?
4771 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4772 queue_work(tpacpi_wq
, &data
->work
);
4775 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4777 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4780 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4782 .name
= "tpacpi::thinklight",
4783 .brightness_set
= &light_sysfs_set
,
4784 .brightness_get
= &light_sysfs_get
,
4788 static int __init
light_init(struct ibm_init_struct
*iibm
)
4792 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4794 TPACPI_ACPIHANDLE_INIT(ledb
);
4795 TPACPI_ACPIHANDLE_INIT(lght
);
4796 TPACPI_ACPIHANDLE_INIT(cmos
);
4797 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4799 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4800 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4802 if (tp_features
.light
)
4803 /* light status not supported on
4804 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4805 tp_features
.light_status
=
4806 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4808 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4809 str_supported(tp_features
.light
),
4810 str_supported(tp_features
.light_status
));
4812 if (!tp_features
.light
)
4815 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4816 &tpacpi_led_thinklight
.led_classdev
);
4819 tp_features
.light
= 0;
4820 tp_features
.light_status
= 0;
4828 static void light_exit(void)
4830 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4831 if (work_pending(&tpacpi_led_thinklight
.work
))
4832 flush_workqueue(tpacpi_wq
);
4835 static int light_read(char *p
)
4840 if (!tp_features
.light
) {
4841 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4842 } else if (!tp_features
.light_status
) {
4843 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4844 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4846 status
= light_get_status();
4849 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4850 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4856 static int light_write(char *buf
)
4861 if (!tp_features
.light
)
4864 while ((cmd
= next_cmd(&buf
))) {
4865 if (strlencmp(cmd
, "on") == 0) {
4867 } else if (strlencmp(cmd
, "off") == 0) {
4873 return light_set_status(newstatus
);
4876 static struct ibm_struct light_driver_data
= {
4879 .write
= light_write
,
4883 /*************************************************************************
4887 /* sysfs cmos_command -------------------------------------------------- */
4888 static ssize_t
cmos_command_store(struct device
*dev
,
4889 struct device_attribute
*attr
,
4890 const char *buf
, size_t count
)
4892 unsigned long cmos_cmd
;
4895 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4898 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4899 return (res
)? res
: count
;
4902 static struct device_attribute dev_attr_cmos_command
=
4903 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4905 /* --------------------------------------------------------------------- */
4907 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4911 vdbg_printk(TPACPI_DBG_INIT
,
4912 "initializing cmos commands subdriver\n");
4914 TPACPI_ACPIHANDLE_INIT(cmos
);
4916 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4917 str_supported(cmos_handle
!= NULL
));
4919 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4923 return (cmos_handle
)? 0 : 1;
4926 static void cmos_exit(void)
4928 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4931 static int cmos_read(char *p
)
4935 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4936 R30, R31, T20-22, X20-21 */
4938 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4940 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4941 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4947 static int cmos_write(char *buf
)
4952 while ((cmd
= next_cmd(&buf
))) {
4953 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4954 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4959 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4967 static struct ibm_struct cmos_driver_data
= {
4970 .write
= cmos_write
,
4974 /*************************************************************************
4978 enum led_access_mode
{
4979 TPACPI_LED_NONE
= 0,
4980 TPACPI_LED_570
, /* 570 */
4981 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4982 TPACPI_LED_NEW
, /* all others */
4985 enum { /* For TPACPI_LED_OLD */
4986 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4987 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4988 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4991 static enum led_access_mode led_supported
;
4993 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4994 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4995 /* T20-22, X20-21 */
4996 "LED", /* all others */
4999 #define TPACPI_LED_NUMLEDS 16
5000 static struct tpacpi_led_classdev
*tpacpi_leds
;
5001 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
5002 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
5003 /* there's a limit of 19 chars + NULL before 2.6.26 */
5005 "tpacpi:orange:batt",
5006 "tpacpi:green:batt",
5007 "tpacpi::dock_active",
5008 "tpacpi::bay_active",
5009 "tpacpi::dock_batt",
5010 "tpacpi::unknown_led",
5012 "tpacpi::dock_status1",
5013 "tpacpi::dock_status2",
5014 "tpacpi::unknown_led2",
5015 "tpacpi::unknown_led3",
5016 "tpacpi::thinkvantage",
5018 #define TPACPI_SAFE_LEDS 0x1081U
5020 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
5022 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5025 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
5029 static int led_get_status(const unsigned int led
)
5032 enum led_status_t led_s
;
5034 switch (led_supported
) {
5035 case TPACPI_LED_570
:
5036 if (!acpi_evalf(ec_handle
,
5037 &status
, "GLED", "dd", 1 << led
))
5039 led_s
= (status
== 0)?
5044 tpacpi_led_state_cache
[led
] = led_s
;
5053 static int led_set_status(const unsigned int led
,
5054 const enum led_status_t ledstatus
)
5056 /* off, on, blink. Index is led_status_t */
5057 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
5058 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
5062 switch (led_supported
) {
5063 case TPACPI_LED_570
:
5065 if (unlikely(led
> 7))
5067 if (unlikely(tpacpi_is_led_restricted(led
)))
5069 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5070 (1 << led
), led_sled_arg1
[ledstatus
]))
5073 case TPACPI_LED_OLD
:
5074 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5075 if (unlikely(led
> 7))
5077 if (unlikely(tpacpi_is_led_restricted(led
)))
5079 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
5081 rc
= ec_write(TPACPI_LED_EC_HLBL
,
5082 (ledstatus
== TPACPI_LED_BLINK
) << led
);
5084 rc
= ec_write(TPACPI_LED_EC_HLCL
,
5085 (ledstatus
!= TPACPI_LED_OFF
) << led
);
5087 case TPACPI_LED_NEW
:
5089 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
5091 if (unlikely(tpacpi_is_led_restricted(led
)))
5093 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5094 led
, led_led_arg1
[ledstatus
]))
5102 tpacpi_led_state_cache
[led
] = ledstatus
;
5107 static void led_set_status_worker(struct work_struct
*work
)
5109 struct tpacpi_led_classdev
*data
=
5110 container_of(work
, struct tpacpi_led_classdev
, work
);
5112 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
5113 led_set_status(data
->led
, data
->new_state
);
5116 static void led_sysfs_set(struct led_classdev
*led_cdev
,
5117 enum led_brightness brightness
)
5119 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5120 struct tpacpi_led_classdev
, led_classdev
);
5122 if (brightness
== LED_OFF
)
5123 data
->new_state
= TPACPI_LED_OFF
;
5124 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
5125 data
->new_state
= TPACPI_LED_ON
;
5127 data
->new_state
= TPACPI_LED_BLINK
;
5129 queue_work(tpacpi_wq
, &data
->work
);
5132 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
5133 unsigned long *delay_on
, unsigned long *delay_off
)
5135 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5136 struct tpacpi_led_classdev
, led_classdev
);
5138 /* Can we choose the flash rate? */
5139 if (*delay_on
== 0 && *delay_off
== 0) {
5140 /* yes. set them to the hardware blink rate (1 Hz) */
5141 *delay_on
= 500; /* ms */
5142 *delay_off
= 500; /* ms */
5143 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
5146 data
->new_state
= TPACPI_LED_BLINK
;
5147 queue_work(tpacpi_wq
, &data
->work
);
5152 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
5156 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5157 struct tpacpi_led_classdev
, led_classdev
);
5159 rc
= led_get_status(data
->led
);
5161 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
5162 rc
= LED_OFF
; /* no error handling in led class :( */
5169 static void led_exit(void)
5173 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5174 if (tpacpi_leds
[i
].led_classdev
.name
)
5175 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
5181 static int __init
tpacpi_init_led(unsigned int led
)
5185 tpacpi_leds
[led
].led
= led
;
5187 /* LEDs with no name don't get registered */
5188 if (!tpacpi_led_names
[led
])
5191 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
5192 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
5193 if (led_supported
== TPACPI_LED_570
)
5194 tpacpi_leds
[led
].led_classdev
.brightness_get
=
5197 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
5199 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
5201 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5202 &tpacpi_leds
[led
].led_classdev
);
5204 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
5209 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
5210 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5211 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5212 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5214 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5215 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5216 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5217 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5218 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5219 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5220 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5221 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5223 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5224 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5225 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5226 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5227 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5229 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5230 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5231 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5232 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5234 /* (1) - may have excess leds enabled on MSB */
5236 /* Defaults (order matters, keep last, don't reorder!) */
5238 .vendor
= PCI_VENDOR_ID_LENOVO
,
5239 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5242 { /* IBM ThinkPads with no EC version string */
5243 .vendor
= PCI_VENDOR_ID_IBM
,
5244 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
5247 { /* IBM ThinkPads with EC version string */
5248 .vendor
= PCI_VENDOR_ID_IBM
,
5249 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5254 #undef TPACPI_LEDQ_IBM
5255 #undef TPACPI_LEDQ_LNV
5257 static int __init
led_init(struct ibm_init_struct
*iibm
)
5261 unsigned long useful_leds
;
5263 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
5265 TPACPI_ACPIHANDLE_INIT(led
);
5268 /* led not supported on R30, R31 */
5269 led_supported
= TPACPI_LED_NONE
;
5270 else if (strlencmp(led_path
, "SLED") == 0)
5272 led_supported
= TPACPI_LED_570
;
5273 else if (strlencmp(led_path
, "SYSL") == 0)
5274 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5275 led_supported
= TPACPI_LED_OLD
;
5278 led_supported
= TPACPI_LED_NEW
;
5280 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
5281 str_supported(led_supported
), led_supported
);
5283 if (led_supported
== TPACPI_LED_NONE
)
5286 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
5289 printk(TPACPI_ERR
"Out of memory for LED data\n");
5293 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
5294 ARRAY_SIZE(led_useful_qtable
));
5296 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5297 if (!tpacpi_is_led_restricted(i
) &&
5298 test_bit(i
, &useful_leds
)) {
5299 rc
= tpacpi_init_led(i
);
5307 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5308 printk(TPACPI_NOTICE
5309 "warning: userspace override of important "
5310 "firmware LEDs is enabled\n");
5315 #define str_led_status(s) \
5316 ((s) == TPACPI_LED_OFF ? "off" : \
5317 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5319 static int led_read(char *p
)
5323 if (!led_supported
) {
5324 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5327 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5329 if (led_supported
== TPACPI_LED_570
) {
5332 for (i
= 0; i
< 8; i
++) {
5333 status
= led_get_status(i
);
5336 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
5337 i
, str_led_status(status
));
5341 len
+= sprintf(p
+ len
, "commands:\t"
5342 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5347 static int led_write(char *buf
)
5351 enum led_status_t s
;
5356 while ((cmd
= next_cmd(&buf
))) {
5357 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 15)
5360 if (strstr(cmd
, "off")) {
5362 } else if (strstr(cmd
, "on")) {
5364 } else if (strstr(cmd
, "blink")) {
5365 s
= TPACPI_LED_BLINK
;
5370 rc
= led_set_status(led
, s
);
5378 static struct ibm_struct led_driver_data
= {
5385 /*************************************************************************
5389 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5391 #define TPACPI_BEEP_Q1 0x0001
5393 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
5394 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
5395 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
5398 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5400 unsigned long quirks
;
5402 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5404 TPACPI_ACPIHANDLE_INIT(beep
);
5406 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5407 str_supported(beep_handle
!= NULL
));
5409 quirks
= tpacpi_check_quirks(beep_quirk_table
,
5410 ARRAY_SIZE(beep_quirk_table
));
5412 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
5414 return (beep_handle
)? 0 : 1;
5417 static int beep_read(char *p
)
5422 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5424 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5425 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5431 static int beep_write(char *buf
)
5439 while ((cmd
= next_cmd(&buf
))) {
5440 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5441 beep_cmd
>= 0 && beep_cmd
<= 17) {
5445 if (tp_features
.beep_needs_two_args
) {
5446 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
5450 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
5459 static struct ibm_struct beep_driver_data
= {
5462 .write
= beep_write
,
5465 /*************************************************************************
5469 enum thermal_access_mode
{
5470 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5471 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5472 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5473 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5474 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5477 enum { /* TPACPI_THERMAL_TPEC_* */
5478 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5479 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5480 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5482 TPACPI_THERMAL_SENSOR_NA
= -128000, /* Sensor not available */
5486 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5487 struct ibm_thermal_sensors_struct
{
5488 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5491 static enum thermal_access_mode thermal_read_mode
;
5493 /* idx is zero-based */
5494 static int thermal_get_sensor(int idx
, s32
*value
)
5500 t
= TP_EC_THERMAL_TMP0
;
5502 switch (thermal_read_mode
) {
5503 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5504 case TPACPI_THERMAL_TPEC_16
:
5505 if (idx
>= 8 && idx
<= 15) {
5506 t
= TP_EC_THERMAL_TMP8
;
5511 case TPACPI_THERMAL_TPEC_8
:
5513 if (!acpi_ec_read(t
+ idx
, &tmp
))
5515 *value
= tmp
* 1000;
5520 case TPACPI_THERMAL_ACPI_UPDT
:
5522 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5523 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5525 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5527 *value
= (t
- 2732) * 100;
5532 case TPACPI_THERMAL_ACPI_TMP07
:
5534 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5535 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5537 if (t
> 127 || t
< -127)
5538 t
= TP_EC_THERMAL_TMP_NA
;
5544 case TPACPI_THERMAL_NONE
:
5552 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5563 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5566 for (i
= 0 ; i
< n
; i
++) {
5567 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5575 static void thermal_dump_all_sensors(void)
5578 struct ibm_thermal_sensors_struct t
;
5580 n
= thermal_get_sensors(&t
);
5584 printk(TPACPI_NOTICE
5585 "temperatures (Celsius):");
5587 for (i
= 0; i
< n
; i
++) {
5588 if (t
.temp
[i
] != TPACPI_THERMAL_SENSOR_NA
)
5589 printk(KERN_CONT
" %d", (int)(t
.temp
[i
] / 1000));
5591 printk(KERN_CONT
" N/A");
5594 printk(KERN_CONT
"\n");
5597 /* sysfs temp##_input -------------------------------------------------- */
5599 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5600 struct device_attribute
*attr
,
5603 struct sensor_device_attribute
*sensor_attr
=
5604 to_sensor_dev_attr(attr
);
5605 int idx
= sensor_attr
->index
;
5609 res
= thermal_get_sensor(idx
, &value
);
5612 if (value
== TPACPI_THERMAL_SENSOR_NA
)
5615 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5618 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5619 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5620 thermal_temp_input_show, NULL, _idxB)
5622 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5623 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5624 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5625 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5626 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5627 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5628 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5629 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5630 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5631 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5632 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5633 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5634 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5635 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5636 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5637 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5638 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5641 #define THERMAL_ATTRS(X) \
5642 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5644 static struct attribute
*thermal_temp_input_attr
[] = {
5664 static const struct attribute_group thermal_temp_input16_group
= {
5665 .attrs
= thermal_temp_input_attr
5668 static const struct attribute_group thermal_temp_input8_group
= {
5669 .attrs
= &thermal_temp_input_attr
[8]
5672 #undef THERMAL_SENSOR_ATTR_TEMP
5673 #undef THERMAL_ATTRS
5675 /* --------------------------------------------------------------------- */
5677 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5684 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5686 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5688 if (thinkpad_id
.ec_model
) {
5690 * Direct EC access mode: sensors at registers
5691 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5692 * non-implemented, thermal sensors return 0x80 when
5697 for (i
= 0; i
< 8; i
++) {
5698 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5704 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5712 /* This is sheer paranoia, but we handle it anyway */
5715 "ThinkPad ACPI EC access misbehaving, "
5716 "falling back to ACPI TMPx access "
5718 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5721 "ThinkPad ACPI EC access misbehaving, "
5722 "disabling thermal sensors access\n");
5723 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5728 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5730 } else if (acpi_tmp7
) {
5731 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5732 /* 600e/x, 770e, 770x */
5733 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5735 /* Standard ACPI TMPx access, max 8 sensors */
5736 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5739 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5740 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5743 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5744 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5747 switch (thermal_read_mode
) {
5748 case TPACPI_THERMAL_TPEC_16
:
5749 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5750 &thermal_temp_input16_group
);
5754 case TPACPI_THERMAL_TPEC_8
:
5755 case TPACPI_THERMAL_ACPI_TMP07
:
5756 case TPACPI_THERMAL_ACPI_UPDT
:
5757 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5758 &thermal_temp_input8_group
);
5762 case TPACPI_THERMAL_NONE
:
5770 static void thermal_exit(void)
5772 switch (thermal_read_mode
) {
5773 case TPACPI_THERMAL_TPEC_16
:
5774 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5775 &thermal_temp_input16_group
);
5777 case TPACPI_THERMAL_TPEC_8
:
5778 case TPACPI_THERMAL_ACPI_TMP07
:
5779 case TPACPI_THERMAL_ACPI_UPDT
:
5780 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5781 &thermal_temp_input16_group
);
5783 case TPACPI_THERMAL_NONE
:
5789 static int thermal_read(char *p
)
5793 struct ibm_thermal_sensors_struct t
;
5795 n
= thermal_get_sensors(&t
);
5796 if (unlikely(n
< 0))
5799 len
+= sprintf(p
+ len
, "temperatures:\t");
5802 for (i
= 0; i
< (n
- 1); i
++)
5803 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5804 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5806 len
+= sprintf(p
+ len
, "not supported\n");
5811 static struct ibm_struct thermal_driver_data
= {
5813 .read
= thermal_read
,
5814 .exit
= thermal_exit
,
5817 /*************************************************************************
5821 static u8 ecdump_regs
[256];
5823 static int ecdump_read(char *p
)
5829 len
+= sprintf(p
+ len
, "EC "
5830 " +00 +01 +02 +03 +04 +05 +06 +07"
5831 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5832 for (i
= 0; i
< 256; i
+= 16) {
5833 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5834 for (j
= 0; j
< 16; j
++) {
5835 if (!acpi_ec_read(i
+ j
, &v
))
5837 if (v
!= ecdump_regs
[i
+ j
])
5838 len
+= sprintf(p
+ len
, " *%02x", v
);
5840 len
+= sprintf(p
+ len
, " %02x", v
);
5841 ecdump_regs
[i
+ j
] = v
;
5843 len
+= sprintf(p
+ len
, "\n");
5848 /* These are way too dangerous to advertise openly... */
5850 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5851 " (<offset> is 00-ff, <value> is 00-ff)\n");
5852 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5853 " (<offset> is 00-ff, <value> is 0-255)\n");
5858 static int ecdump_write(char *buf
)
5863 while ((cmd
= next_cmd(&buf
))) {
5864 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5866 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5870 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5871 if (!acpi_ec_write(i
, v
))
5880 static struct ibm_struct ecdump_driver_data
= {
5882 .read
= ecdump_read
,
5883 .write
= ecdump_write
,
5884 .flags
.experimental
= 1,
5887 /*************************************************************************
5888 * Backlight/brightness subdriver
5891 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5894 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5895 * CMOS NVRAM byte 0x5E, bits 0-3.
5897 * EC HBRV (0x31) has the following layout
5898 * Bit 7: unknown function
5899 * Bit 6: unknown function
5900 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5901 * Bit 4: must be set to zero to avoid problems
5902 * Bit 3-0: backlight brightness level
5904 * brightness_get_raw returns status data in the HBRV layout
5906 * WARNING: The X61 has been verified to use HBRV for something else, so
5907 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5908 * testing on the very early *60 Lenovo models...
5912 TP_EC_BACKLIGHT
= 0x31,
5914 /* TP_EC_BACKLIGHT bitmasks */
5915 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5916 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5917 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5920 enum tpacpi_brightness_access_mode
{
5921 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
5922 TPACPI_BRGHT_MODE_EC
, /* EC control */
5923 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
5924 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
5925 TPACPI_BRGHT_MODE_MAX
5928 static struct backlight_device
*ibm_backlight_device
;
5930 static enum tpacpi_brightness_access_mode brightness_mode
=
5931 TPACPI_BRGHT_MODE_MAX
;
5933 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5935 static struct mutex brightness_mutex
;
5937 /* NVRAM brightness access,
5938 * call with brightness_mutex held! */
5939 static unsigned int tpacpi_brightness_nvram_get(void)
5943 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5944 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5945 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5946 lnvram
&= (tp_features
.bright_16levels
) ? 0x0f : 0x07;
5951 static void tpacpi_brightness_checkpoint_nvram(void)
5956 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
5959 vdbg_printk(TPACPI_DBG_BRGHT
,
5960 "trying to checkpoint backlight level to NVRAM...\n");
5962 if (mutex_lock_killable(&brightness_mutex
) < 0)
5965 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5967 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5968 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
5970 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5971 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
5972 /* NVRAM needs update */
5973 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
5974 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
5976 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
5977 dbg_printk(TPACPI_DBG_BRGHT
,
5978 "updated NVRAM backlight level to %u (0x%02x)\n",
5979 (unsigned int) lec
, (unsigned int) b_nvram
);
5981 vdbg_printk(TPACPI_DBG_BRGHT
,
5982 "NVRAM backlight level already is %u (0x%02x)\n",
5983 (unsigned int) lec
, (unsigned int) b_nvram
);
5986 mutex_unlock(&brightness_mutex
);
5990 /* call with brightness_mutex held! */
5991 static int tpacpi_brightness_get_raw(int *status
)
5995 switch (brightness_mode
) {
5996 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5997 *status
= tpacpi_brightness_nvram_get();
5999 case TPACPI_BRGHT_MODE_EC
:
6000 case TPACPI_BRGHT_MODE_ECNVRAM
:
6001 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6010 /* call with brightness_mutex held! */
6011 /* do NOT call with illegal backlight level value */
6012 static int tpacpi_brightness_set_ec(unsigned int value
)
6016 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6019 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
6020 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
6021 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
6027 /* call with brightness_mutex held! */
6028 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
6031 unsigned int current_value
, i
;
6033 current_value
= tpacpi_brightness_nvram_get();
6035 if (value
== current_value
)
6038 cmos_cmd
= (value
> current_value
) ?
6039 TP_CMOS_BRIGHTNESS_UP
:
6040 TP_CMOS_BRIGHTNESS_DOWN
;
6041 inc
= (value
> current_value
) ? 1 : -1;
6043 for (i
= current_value
; i
!= value
; i
+= inc
)
6044 if (issue_thinkpad_cmos_command(cmos_cmd
))
6050 /* May return EINTR which can always be mapped to ERESTARTSYS */
6051 static int brightness_set(unsigned int value
)
6055 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
6059 vdbg_printk(TPACPI_DBG_BRGHT
,
6060 "set backlight level to %d\n", value
);
6062 res
= mutex_lock_killable(&brightness_mutex
);
6066 switch (brightness_mode
) {
6067 case TPACPI_BRGHT_MODE_EC
:
6068 case TPACPI_BRGHT_MODE_ECNVRAM
:
6069 res
= tpacpi_brightness_set_ec(value
);
6071 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6072 res
= tpacpi_brightness_set_ucmsstep(value
);
6078 mutex_unlock(&brightness_mutex
);
6082 /* sysfs backlight class ----------------------------------------------- */
6084 static int brightness_update_status(struct backlight_device
*bd
)
6086 unsigned int level
=
6087 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
6088 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
6089 bd
->props
.brightness
: 0;
6091 dbg_printk(TPACPI_DBG_BRGHT
,
6092 "backlight: attempt to set level to %d\n",
6095 /* it is the backlight class's job (caller) to handle
6096 * EINTR and other errors properly */
6097 return brightness_set(level
);
6100 static int brightness_get(struct backlight_device
*bd
)
6104 res
= mutex_lock_killable(&brightness_mutex
);
6108 res
= tpacpi_brightness_get_raw(&status
);
6110 mutex_unlock(&brightness_mutex
);
6115 return status
& TP_EC_BACKLIGHT_LVLMSK
;
6118 static void tpacpi_brightness_notify_change(void)
6120 backlight_force_update(ibm_backlight_device
,
6121 BACKLIGHT_UPDATE_HOTKEY
);
6124 static struct backlight_ops ibm_backlight_data
= {
6125 .get_brightness
= brightness_get
,
6126 .update_status
= brightness_update_status
,
6129 /* --------------------------------------------------------------------- */
6132 * These are only useful for models that have only one possibility
6133 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6136 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6137 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6138 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6140 static const struct tpacpi_quirk brightness_quirk_table
[] __initconst
= {
6141 /* Models with ATI GPUs known to require ECNVRAM mode */
6142 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC
), /* T43/p ATI */
6144 /* Models with ATI GPUs that can use ECNVRAM */
6145 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC
),
6146 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6147 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6148 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6150 /* Models with Intel Extreme Graphics 2 */
6151 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC
),
6152 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6153 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6155 /* Models with Intel GMA900 */
6156 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC
), /* T43, R52 */
6157 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC
), /* X41 */
6158 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC
), /* X41 Tablet */
6161 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
6164 unsigned long quirks
;
6166 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
6168 mutex_init(&brightness_mutex
);
6170 quirks
= tpacpi_check_quirks(brightness_quirk_table
,
6171 ARRAY_SIZE(brightness_quirk_table
));
6174 * We always attempt to detect acpi support, so as to switch
6175 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6176 * going to publish a backlight interface
6178 b
= tpacpi_check_std_acpi_brightness_support();
6181 if (acpi_video_backlight_support()) {
6182 if (brightness_enable
> 1) {
6183 printk(TPACPI_NOTICE
6184 "Standard ACPI backlight interface "
6185 "available, not loading native one.\n");
6187 } else if (brightness_enable
== 1) {
6188 printk(TPACPI_NOTICE
6189 "Backlight control force enabled, even if standard "
6190 "ACPI backlight interface is available\n");
6193 if (brightness_enable
> 1) {
6194 printk(TPACPI_NOTICE
6195 "Standard ACPI backlight interface not "
6196 "available, thinkpad_acpi native "
6197 "brightness control enabled\n");
6202 if (!brightness_enable
) {
6203 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6204 "brightness support disabled by "
6205 "module parameter\n");
6211 "Unsupported brightness interface, "
6212 "please contact %s\n", TPACPI_MAIL
);
6216 tp_features
.bright_16levels
= 1;
6219 * Check for module parameter bogosity, note that we
6220 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6221 * able to detect "unspecified"
6223 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
6226 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6227 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
6228 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
6229 if (quirks
& TPACPI_BRGHT_Q_EC
)
6230 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
6232 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
6234 dbg_printk(TPACPI_DBG_BRGHT
,
6235 "driver auto-selected brightness_mode=%d\n",
6240 if (thinkpad_id
.vendor
!= PCI_VENDOR_ID_IBM
&&
6241 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
6242 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
6245 if (tpacpi_brightness_get_raw(&b
) < 0)
6248 if (tp_features
.bright_16levels
)
6250 "detected a 16-level brightness capable ThinkPad\n");
6252 ibm_backlight_device
= backlight_device_register(
6253 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
6254 &ibm_backlight_data
);
6255 if (IS_ERR(ibm_backlight_device
)) {
6256 int rc
= PTR_ERR(ibm_backlight_device
);
6257 ibm_backlight_device
= NULL
;
6258 printk(TPACPI_ERR
"Could not register backlight device\n");
6261 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6262 "brightness is supported\n");
6264 if (quirks
& TPACPI_BRGHT_Q_ASK
) {
6265 printk(TPACPI_NOTICE
6266 "brightness: will use unverified default: "
6267 "brightness_mode=%d\n", brightness_mode
);
6268 printk(TPACPI_NOTICE
6269 "brightness: please report to %s whether it works well "
6270 "or not on your ThinkPad\n", TPACPI_MAIL
);
6273 ibm_backlight_device
->props
.max_brightness
=
6274 (tp_features
.bright_16levels
)? 15 : 7;
6275 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
6276 backlight_update_status(ibm_backlight_device
);
6278 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6279 "brightness: registering brightness hotkeys "
6280 "as change notification\n");
6281 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6282 | TP_ACPI_HKEY_BRGHTUP_MASK
6283 | TP_ACPI_HKEY_BRGHTDWN_MASK
);;
6287 static void brightness_suspend(pm_message_t state
)
6289 tpacpi_brightness_checkpoint_nvram();
6292 static void brightness_shutdown(void)
6294 tpacpi_brightness_checkpoint_nvram();
6297 static void brightness_exit(void)
6299 if (ibm_backlight_device
) {
6300 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
6301 "calling backlight_device_unregister()\n");
6302 backlight_device_unregister(ibm_backlight_device
);
6305 tpacpi_brightness_checkpoint_nvram();
6308 static int brightness_read(char *p
)
6313 level
= brightness_get(NULL
);
6315 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6317 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
6318 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
6319 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
6320 " (<level> is 0-%d)\n",
6321 (tp_features
.bright_16levels
) ? 15 : 7);
6327 static int brightness_write(char *buf
)
6332 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
6334 level
= brightness_get(NULL
);
6338 while ((cmd
= next_cmd(&buf
))) {
6339 if (strlencmp(cmd
, "up") == 0) {
6340 if (level
< max_level
)
6342 } else if (strlencmp(cmd
, "down") == 0) {
6345 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
6346 level
>= 0 && level
<= max_level
) {
6352 tpacpi_disclose_usertask("procfs brightness",
6353 "set level to %d\n", level
);
6356 * Now we know what the final level should be, so we try to set it.
6357 * Doing it this way makes the syscall restartable in case of EINTR
6359 rc
= brightness_set(level
);
6360 if (!rc
&& ibm_backlight_device
)
6361 backlight_force_update(ibm_backlight_device
,
6362 BACKLIGHT_UPDATE_SYSFS
);
6363 return (rc
== -EINTR
)? -ERESTARTSYS
: rc
;
6366 static struct ibm_struct brightness_driver_data
= {
6367 .name
= "brightness",
6368 .read
= brightness_read
,
6369 .write
= brightness_write
,
6370 .exit
= brightness_exit
,
6371 .suspend
= brightness_suspend
,
6372 .shutdown
= brightness_shutdown
,
6375 /*************************************************************************
6380 * IBM ThinkPads have a simple volume controller with MUTE gating.
6381 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6383 * Since the *61 series (and probably also the later *60 series), Lenovo
6384 * ThinkPads only implement the MUTE gate.
6387 * Bit 6: MUTE (1 mutes sound)
6389 * Other bits should be zero as far as we know.
6391 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6392 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6393 * such as bit 7 which is used to detect repeated presses of MUTE,
6394 * and we leave them unchanged.
6400 /* TP_EC_AUDIO bits */
6401 TP_EC_AUDIO_MUTESW
= 6,
6403 /* TP_EC_AUDIO bitmasks */
6404 TP_EC_AUDIO_LVL_MSK
= 0x0F,
6405 TP_EC_AUDIO_MUTESW_MSK
= (1 << TP_EC_AUDIO_MUTESW
),
6407 /* Maximum volume */
6408 TP_EC_VOLUME_MAX
= 14,
6411 enum tpacpi_volume_access_mode
{
6412 TPACPI_VOL_MODE_AUTO
= 0, /* Not implemented yet */
6413 TPACPI_VOL_MODE_EC
, /* Pure EC control */
6414 TPACPI_VOL_MODE_UCMS_STEP
, /* UCMS step-based control: N/A */
6415 TPACPI_VOL_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
6419 enum tpacpi_volume_capabilities
{
6420 TPACPI_VOL_CAP_AUTO
= 0, /* Use white/blacklist */
6421 TPACPI_VOL_CAP_VOLMUTE
, /* Output vol and mute */
6422 TPACPI_VOL_CAP_MUTEONLY
, /* Output mute only */
6426 static enum tpacpi_volume_access_mode volume_mode
=
6427 TPACPI_VOL_MODE_MAX
;
6429 static enum tpacpi_volume_capabilities volume_capabilities
;
6430 static int volume_control_allowed
;
6433 * Used to syncronize writers to TP_EC_AUDIO and
6434 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6436 static struct mutex volume_mutex
;
6438 static void tpacpi_volume_checkpoint_nvram(void)
6444 if (volume_mode
!= TPACPI_VOL_MODE_ECNVRAM
)
6446 if (!volume_control_allowed
)
6449 vdbg_printk(TPACPI_DBG_MIXER
,
6450 "trying to checkpoint mixer state to NVRAM...\n");
6452 if (tp_features
.mixer_no_level_control
)
6453 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
;
6455 ec_mask
= TP_EC_AUDIO_MUTESW_MSK
| TP_EC_AUDIO_LVL_MSK
;
6457 if (mutex_lock_killable(&volume_mutex
) < 0)
6460 if (unlikely(!acpi_ec_read(TP_EC_AUDIO
, &lec
)))
6463 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
6465 if (lec
!= (b_nvram
& ec_mask
)) {
6466 /* NVRAM needs update */
6467 b_nvram
&= ~ec_mask
;
6469 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_MIXER
);
6470 dbg_printk(TPACPI_DBG_MIXER
,
6471 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6472 (unsigned int) lec
, (unsigned int) b_nvram
);
6474 vdbg_printk(TPACPI_DBG_MIXER
,
6475 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6476 (unsigned int) lec
, (unsigned int) b_nvram
);
6480 mutex_unlock(&volume_mutex
);
6483 static int volume_get_status_ec(u8
*status
)
6487 if (!acpi_ec_read(TP_EC_AUDIO
, &s
))
6492 dbg_printk(TPACPI_DBG_MIXER
, "status 0x%02x\n", s
);
6497 static int volume_get_status(u8
*status
)
6499 return volume_get_status_ec(status
);
6502 static int volume_set_status_ec(const u8 status
)
6504 if (!acpi_ec_write(TP_EC_AUDIO
, status
))
6507 dbg_printk(TPACPI_DBG_MIXER
, "set EC mixer to 0x%02x\n", status
);
6512 static int volume_set_status(const u8 status
)
6514 return volume_set_status_ec(status
);
6517 static int volume_set_mute_ec(const bool mute
)
6522 if (mutex_lock_killable(&volume_mutex
) < 0)
6525 rc
= volume_get_status_ec(&s
);
6529 n
= (mute
) ? s
| TP_EC_AUDIO_MUTESW_MSK
:
6530 s
& ~TP_EC_AUDIO_MUTESW_MSK
;
6533 rc
= volume_set_status_ec(n
);
6536 mutex_unlock(&volume_mutex
);
6540 static int volume_set_mute(const bool mute
)
6542 dbg_printk(TPACPI_DBG_MIXER
, "trying to %smute\n",
6543 (mute
) ? "" : "un");
6544 return volume_set_mute_ec(mute
);
6547 static int volume_set_volume_ec(const u8 vol
)
6552 if (vol
> TP_EC_VOLUME_MAX
)
6555 if (mutex_lock_killable(&volume_mutex
) < 0)
6558 rc
= volume_get_status_ec(&s
);
6562 n
= (s
& ~TP_EC_AUDIO_LVL_MSK
) | vol
;
6565 rc
= volume_set_status_ec(n
);
6568 mutex_unlock(&volume_mutex
);
6572 static int volume_set_volume(const u8 vol
)
6574 dbg_printk(TPACPI_DBG_MIXER
,
6575 "trying to set volume level to %hu\n", vol
);
6576 return volume_set_volume_ec(vol
);
6579 static void volume_suspend(pm_message_t state
)
6581 tpacpi_volume_checkpoint_nvram();
6584 static void volume_shutdown(void)
6586 tpacpi_volume_checkpoint_nvram();
6589 static void volume_exit(void)
6591 tpacpi_volume_checkpoint_nvram();
6594 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6595 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6597 static const struct tpacpi_quirk volume_quirk_table
[] __initconst
= {
6598 /* Whitelist volume level on all IBM by default */
6599 { .vendor
= PCI_VENDOR_ID_IBM
,
6600 .bios
= TPACPI_MATCH_ANY
,
6601 .ec
= TPACPI_MATCH_ANY
,
6602 .quirks
= TPACPI_VOL_Q_LEVEL
},
6604 /* Lenovo models with volume control (needs confirmation) */
6605 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL
), /* R60/i */
6606 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL
), /* R60e/i */
6607 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL
), /* T60/p */
6608 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL
), /* X60/s */
6609 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL
), /* X60t */
6610 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL
), /* Z60 */
6611 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL
), /* Z61 */
6613 /* Whitelist mute-only on all Lenovo by default */
6614 { .vendor
= PCI_VENDOR_ID_LENOVO
,
6615 .bios
= TPACPI_MATCH_ANY
,
6616 .ec
= TPACPI_MATCH_ANY
,
6617 .quirks
= TPACPI_VOL_Q_MUTEONLY
}
6620 static int __init
volume_init(struct ibm_init_struct
*iibm
)
6622 unsigned long quirks
;
6624 vdbg_printk(TPACPI_DBG_INIT
, "initializing volume subdriver\n");
6626 mutex_init(&volume_mutex
);
6629 * Check for module parameter bogosity, note that we
6630 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6631 * able to detect "unspecified"
6633 if (volume_mode
> TPACPI_VOL_MODE_MAX
)
6636 if (volume_mode
== TPACPI_VOL_MODE_UCMS_STEP
) {
6638 "UCMS step volume mode not implemented, "
6639 "please contact %s\n", TPACPI_MAIL
);
6643 if (volume_capabilities
>= TPACPI_VOL_CAP_MAX
)
6646 quirks
= tpacpi_check_quirks(volume_quirk_table
,
6647 ARRAY_SIZE(volume_quirk_table
));
6649 switch (volume_capabilities
) {
6650 case TPACPI_VOL_CAP_AUTO
:
6651 if (quirks
& TPACPI_VOL_Q_MUTEONLY
)
6652 tp_features
.mixer_no_level_control
= 1;
6653 else if (quirks
& TPACPI_VOL_Q_LEVEL
)
6654 tp_features
.mixer_no_level_control
= 0;
6656 return 1; /* no mixer */
6658 case TPACPI_VOL_CAP_VOLMUTE
:
6659 tp_features
.mixer_no_level_control
= 0;
6661 case TPACPI_VOL_CAP_MUTEONLY
:
6662 tp_features
.mixer_no_level_control
= 1;
6668 if (volume_capabilities
!= TPACPI_VOL_CAP_AUTO
)
6669 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6670 "using user-supplied volume_capabilities=%d\n",
6671 volume_capabilities
);
6673 if (volume_mode
== TPACPI_VOL_MODE_AUTO
||
6674 volume_mode
== TPACPI_VOL_MODE_MAX
) {
6675 volume_mode
= TPACPI_VOL_MODE_ECNVRAM
;
6677 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6678 "driver auto-selected volume_mode=%d\n",
6681 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6682 "using user-supplied volume_mode=%d\n",
6686 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6687 "mute is supported, volume control is %s\n",
6688 str_supported(!tp_features
.mixer_no_level_control
));
6691 "Console audio control enabled, mode: %s\n",
6692 (volume_control_allowed
) ?
6693 "override (read/write)" :
6694 "monitor (read only)");
6699 static int volume_read(char *p
)
6704 if (volume_get_status(&status
) < 0) {
6705 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6707 if (tp_features
.mixer_no_level_control
)
6708 len
+= sprintf(p
+ len
, "level:\t\tunsupported\n");
6710 len
+= sprintf(p
+ len
, "level:\t\t%d\n",
6711 status
& TP_EC_AUDIO_LVL_MSK
);
6713 len
+= sprintf(p
+ len
, "mute:\t\t%s\n",
6714 onoff(status
, TP_EC_AUDIO_MUTESW
));
6716 if (volume_control_allowed
) {
6717 len
+= sprintf(p
+ len
, "commands:\tunmute, mute\n");
6718 if (!tp_features
.mixer_no_level_control
) {
6719 len
+= sprintf(p
+ len
,
6720 "commands:\tup, down\n");
6721 len
+= sprintf(p
+ len
,
6722 "commands:\tlevel <level>"
6723 " (<level> is 0-%d)\n",
6732 static int volume_write(char *buf
)
6735 u8 new_level
, new_mute
;
6741 * We do allow volume control at driver startup, so that the
6742 * user can set initial state through the volume=... parameter hack.
6744 if (!volume_control_allowed
&& tpacpi_lifecycle
!= TPACPI_LIFE_INIT
) {
6745 if (unlikely(!tp_warned
.volume_ctrl_forbidden
)) {
6746 tp_warned
.volume_ctrl_forbidden
= 1;
6747 printk(TPACPI_NOTICE
6748 "Console audio control in monitor mode, "
6749 "changes are not allowed.\n");
6750 printk(TPACPI_NOTICE
6751 "Use the volume_control=1 module parameter "
6752 "to enable volume control\n");
6757 rc
= volume_get_status(&s
);
6761 new_level
= s
& TP_EC_AUDIO_LVL_MSK
;
6762 new_mute
= s
& TP_EC_AUDIO_MUTESW_MSK
;
6764 while ((cmd
= next_cmd(&buf
))) {
6765 if (!tp_features
.mixer_no_level_control
) {
6766 if (strlencmp(cmd
, "up") == 0) {
6769 else if (new_level
< TP_EC_VOLUME_MAX
)
6772 } else if (strlencmp(cmd
, "down") == 0) {
6775 else if (new_level
> 0)
6778 } else if (sscanf(cmd
, "level %u", &l
) == 1 &&
6779 l
>= 0 && l
<= TP_EC_VOLUME_MAX
) {
6784 if (strlencmp(cmd
, "mute") == 0)
6785 new_mute
= TP_EC_AUDIO_MUTESW_MSK
;
6786 else if (strlencmp(cmd
, "unmute") == 0)
6792 if (tp_features
.mixer_no_level_control
) {
6793 tpacpi_disclose_usertask("procfs volume", "%smute\n",
6794 new_mute
? "" : "un");
6795 rc
= volume_set_mute(!!new_mute
);
6797 tpacpi_disclose_usertask("procfs volume",
6798 "%smute and set level to %d\n",
6799 new_mute
? "" : "un", new_level
);
6800 rc
= volume_set_status(new_mute
| new_level
);
6803 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
6806 static struct ibm_struct volume_driver_data
= {
6808 .read
= volume_read
,
6809 .write
= volume_write
,
6810 .exit
= volume_exit
,
6811 .suspend
= volume_suspend
,
6812 .shutdown
= volume_shutdown
,
6815 /*************************************************************************
6822 * TPACPI_FAN_RD_ACPI_GFAN:
6823 * ACPI GFAN method: returns fan level
6825 * see TPACPI_FAN_WR_ACPI_SFAN
6826 * EC 0x2f (HFSP) not available if GFAN exists
6828 * TPACPI_FAN_WR_ACPI_SFAN:
6829 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6831 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6834 * TPACPI_FAN_WR_TPEC:
6835 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6836 * Supported on almost all ThinkPads
6838 * Fan speed changes of any sort (including those caused by the
6839 * disengaged mode) are usually done slowly by the firmware as the
6840 * maximum ammount of fan duty cycle change per second seems to be
6843 * Reading is not available if GFAN exists.
6844 * Writing is not available if SFAN exists.
6847 * 7 automatic mode engaged;
6848 * (default operation mode of the ThinkPad)
6849 * fan level is ignored in this mode.
6850 * 6 full speed mode (takes precedence over bit 7);
6851 * not available on all thinkpads. May disable
6852 * the tachometer while the fan controller ramps up
6853 * the speed (which can take up to a few *minutes*).
6854 * Speeds up fan to 100% duty-cycle, which is far above
6855 * the standard RPM levels. It is not impossible that
6856 * it could cause hardware damage.
6857 * 5-3 unused in some models. Extra bits for fan level
6858 * in others, but still useless as all values above
6859 * 7 map to the same speed as level 7 in these models.
6860 * 2-0 fan level (0..7 usually)
6862 * 0x07 = max (set when temperatures critical)
6863 * Some ThinkPads may have other levels, see
6864 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6866 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6867 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6868 * does so, its initial value is meaningless (0x07).
6870 * For firmware bugs, refer to:
6871 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6875 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6876 * Main fan tachometer reading (in RPM)
6878 * This register is present on all ThinkPads with a new-style EC, and
6879 * it is known not to be present on the A21m/e, and T22, as there is
6880 * something else in offset 0x84 according to the ACPI DSDT. Other
6881 * ThinkPads from this same time period (and earlier) probably lack the
6882 * tachometer as well.
6884 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6885 * was never fixed by IBM to report the EC firmware version string
6886 * probably support the tachometer (like the early X models), so
6887 * detecting it is quite hard. We need more data to know for sure.
6889 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6892 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6895 * For firmware bugs, refer to:
6896 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6900 * ThinkPad EC register 0x31 bit 0 (only on select models)
6902 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6903 * show the speed of the main fan. When bit 0 of EC register 0x31
6904 * is one, the tachometer registers show the speed of the auxiliary
6907 * Fan control seems to affect both fans, regardless of the state
6910 * So far, only the firmware for the X60/X61 non-tablet versions
6911 * seem to support this (firmware TP-7M).
6913 * TPACPI_FAN_WR_ACPI_FANS:
6914 * ThinkPad X31, X40, X41. Not available in the X60.
6916 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6917 * high speed. ACPI DSDT seems to map these three speeds to levels
6918 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6919 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6921 * The speeds are stored on handles
6922 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6924 * There are three default speed sets, acessible as handles:
6925 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6927 * ACPI DSDT switches which set is in use depending on various
6930 * TPACPI_FAN_WR_TPEC is also available and should be used to
6931 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6932 * but the ACPI tables just mention level 7.
6935 enum { /* Fan control constants */
6936 fan_status_offset
= 0x2f, /* EC register 0x2f */
6937 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6938 * 0x84 must be read before 0x85 */
6939 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
6940 bit 0 selects which fan is active */
6942 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
6943 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
6945 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
6948 enum fan_status_access_mode
{
6949 TPACPI_FAN_NONE
= 0, /* No fan status or control */
6950 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
6951 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6954 enum fan_control_access_mode
{
6955 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
6956 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
6957 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
6958 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
6961 enum fan_control_commands
{
6962 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
6963 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
6964 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
6965 * and also watchdog cmd */
6968 static int fan_control_allowed
;
6970 static enum fan_status_access_mode fan_status_access_mode
;
6971 static enum fan_control_access_mode fan_control_access_mode
;
6972 static enum fan_control_commands fan_control_commands
;
6974 static u8 fan_control_initial_status
;
6975 static u8 fan_control_desired_level
;
6976 static u8 fan_control_resume_level
;
6977 static int fan_watchdog_maxinterval
;
6979 static struct mutex fan_mutex
;
6981 static void fan_watchdog_fire(struct work_struct
*ignored
);
6982 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
6984 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
6985 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
6986 "\\FSPD", /* 600e/x, 770e, 770x */
6988 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
6989 "JFNS", /* 770x-JL */
6993 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6994 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6995 * be in auto mode (0x80).
6997 * This is corrected by any write to HFSP either by the driver, or
7000 * We assume 0x07 really means auto mode while this quirk is active,
7001 * as this is far more likely than the ThinkPad being in level 7,
7002 * which is only used by the firmware during thermal emergencies.
7004 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7005 * TP-70 (T43, R52), which are known to be buggy.
7008 static void fan_quirk1_setup(void)
7010 if (fan_control_initial_status
== 0x07) {
7011 printk(TPACPI_NOTICE
7012 "fan_init: initial fan status is unknown, "
7013 "assuming it is in auto mode\n");
7014 tp_features
.fan_ctrl_status_undef
= 1;
7018 static void fan_quirk1_handle(u8
*fan_status
)
7020 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
7021 if (*fan_status
!= fan_control_initial_status
) {
7022 /* something changed the HFSP regisnter since
7023 * driver init time, so it is not undefined
7025 tp_features
.fan_ctrl_status_undef
= 0;
7027 /* Return most likely status. In fact, it
7028 * might be the only possible status */
7029 *fan_status
= TP_EC_FAN_AUTO
;
7034 /* Select main fan on X60/X61, NOOP on others */
7035 static bool fan_select_fan1(void)
7037 if (tp_features
.second_fan
) {
7040 if (ec_read(fan_select_offset
, &val
) < 0)
7043 if (ec_write(fan_select_offset
, val
) < 0)
7049 /* Select secondary fan on X60/X61 */
7050 static bool fan_select_fan2(void)
7054 if (!tp_features
.second_fan
)
7057 if (ec_read(fan_select_offset
, &val
) < 0)
7060 if (ec_write(fan_select_offset
, val
) < 0)
7067 * Call with fan_mutex held
7069 static void fan_update_desired_level(u8 status
)
7072 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
7074 fan_control_desired_level
= 7;
7076 fan_control_desired_level
= status
;
7080 static int fan_get_status(u8
*status
)
7085 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7087 switch (fan_status_access_mode
) {
7088 case TPACPI_FAN_RD_ACPI_GFAN
:
7089 /* 570, 600e/x, 770e, 770x */
7091 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
7099 case TPACPI_FAN_RD_TPEC
:
7100 /* all except 570, 600e/x, 770e, 770x */
7101 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
7104 if (likely(status
)) {
7106 fan_quirk1_handle(status
);
7118 static int fan_get_status_safe(u8
*status
)
7123 if (mutex_lock_killable(&fan_mutex
))
7124 return -ERESTARTSYS
;
7125 rc
= fan_get_status(&s
);
7127 fan_update_desired_level(s
);
7128 mutex_unlock(&fan_mutex
);
7136 static int fan_get_speed(unsigned int *speed
)
7140 switch (fan_status_access_mode
) {
7141 case TPACPI_FAN_RD_TPEC
:
7142 /* all except 570, 600e/x, 770e, 770x */
7143 if (unlikely(!fan_select_fan1()))
7145 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
7146 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
7150 *speed
= (hi
<< 8) | lo
;
7161 static int fan2_get_speed(unsigned int *speed
)
7166 switch (fan_status_access_mode
) {
7167 case TPACPI_FAN_RD_TPEC
:
7168 /* all except 570, 600e/x, 770e, 770x */
7169 if (unlikely(!fan_select_fan2()))
7171 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
7172 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
7173 fan_select_fan1(); /* play it safe */
7178 *speed
= (hi
<< 8) | lo
;
7189 static int fan_set_level(int level
)
7191 if (!fan_control_allowed
)
7194 switch (fan_control_access_mode
) {
7195 case TPACPI_FAN_WR_ACPI_SFAN
:
7196 if (level
>= 0 && level
<= 7) {
7197 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
7203 case TPACPI_FAN_WR_ACPI_FANS
:
7204 case TPACPI_FAN_WR_TPEC
:
7205 if (!(level
& TP_EC_FAN_AUTO
) &&
7206 !(level
& TP_EC_FAN_FULLSPEED
) &&
7207 ((level
< 0) || (level
> 7)))
7210 /* safety net should the EC not support AUTO
7211 * or FULLSPEED mode bits and just ignore them */
7212 if (level
& TP_EC_FAN_FULLSPEED
)
7213 level
|= 7; /* safety min speed 7 */
7214 else if (level
& TP_EC_FAN_AUTO
)
7215 level
|= 4; /* safety min speed 4 */
7217 if (!acpi_ec_write(fan_status_offset
, level
))
7220 tp_features
.fan_ctrl_status_undef
= 0;
7227 vdbg_printk(TPACPI_DBG_FAN
,
7228 "fan control: set fan control register to 0x%02x\n", level
);
7232 static int fan_set_level_safe(int level
)
7236 if (!fan_control_allowed
)
7239 if (mutex_lock_killable(&fan_mutex
))
7240 return -ERESTARTSYS
;
7242 if (level
== TPACPI_FAN_LAST_LEVEL
)
7243 level
= fan_control_desired_level
;
7245 rc
= fan_set_level(level
);
7247 fan_update_desired_level(level
);
7249 mutex_unlock(&fan_mutex
);
7253 static int fan_set_enable(void)
7258 if (!fan_control_allowed
)
7261 if (mutex_lock_killable(&fan_mutex
))
7262 return -ERESTARTSYS
;
7264 switch (fan_control_access_mode
) {
7265 case TPACPI_FAN_WR_ACPI_FANS
:
7266 case TPACPI_FAN_WR_TPEC
:
7267 rc
= fan_get_status(&s
);
7271 /* Don't go out of emergency fan mode */
7274 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
7277 if (!acpi_ec_write(fan_status_offset
, s
))
7280 tp_features
.fan_ctrl_status_undef
= 0;
7285 case TPACPI_FAN_WR_ACPI_SFAN
:
7286 rc
= fan_get_status(&s
);
7292 /* Set fan to at least level 4 */
7295 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
7305 mutex_unlock(&fan_mutex
);
7308 vdbg_printk(TPACPI_DBG_FAN
,
7309 "fan control: set fan control register to 0x%02x\n",
7314 static int fan_set_disable(void)
7318 if (!fan_control_allowed
)
7321 if (mutex_lock_killable(&fan_mutex
))
7322 return -ERESTARTSYS
;
7325 switch (fan_control_access_mode
) {
7326 case TPACPI_FAN_WR_ACPI_FANS
:
7327 case TPACPI_FAN_WR_TPEC
:
7328 if (!acpi_ec_write(fan_status_offset
, 0x00))
7331 fan_control_desired_level
= 0;
7332 tp_features
.fan_ctrl_status_undef
= 0;
7336 case TPACPI_FAN_WR_ACPI_SFAN
:
7337 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
7340 fan_control_desired_level
= 0;
7348 vdbg_printk(TPACPI_DBG_FAN
,
7349 "fan control: set fan control register to 0\n");
7351 mutex_unlock(&fan_mutex
);
7355 static int fan_set_speed(int speed
)
7359 if (!fan_control_allowed
)
7362 if (mutex_lock_killable(&fan_mutex
))
7363 return -ERESTARTSYS
;
7366 switch (fan_control_access_mode
) {
7367 case TPACPI_FAN_WR_ACPI_FANS
:
7368 if (speed
>= 0 && speed
<= 65535) {
7369 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
7370 speed
, speed
, speed
))
7380 mutex_unlock(&fan_mutex
);
7384 static void fan_watchdog_reset(void)
7386 static int fan_watchdog_active
;
7388 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
7391 if (fan_watchdog_active
)
7392 cancel_delayed_work(&fan_watchdog_task
);
7394 if (fan_watchdog_maxinterval
> 0 &&
7395 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
7396 fan_watchdog_active
= 1;
7397 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
7398 msecs_to_jiffies(fan_watchdog_maxinterval
7401 "failed to queue the fan watchdog, "
7402 "watchdog will not trigger\n");
7405 fan_watchdog_active
= 0;
7408 static void fan_watchdog_fire(struct work_struct
*ignored
)
7412 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
7415 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
7416 rc
= fan_set_enable();
7418 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
7419 "will try again later...\n", -rc
);
7420 /* reschedule for later */
7421 fan_watchdog_reset();
7426 * SYSFS fan layout: hwmon compatible (device)
7429 * 0: "disengaged" mode
7431 * 2: native EC "auto" mode (recommended, hardware default)
7433 * pwm*: set speed in manual mode, ignored otherwise.
7434 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7437 * fan*_input: tachometer reading, RPM
7440 * SYSFS fan layout: extensions
7442 * fan_watchdog (driver):
7443 * fan watchdog interval in seconds, 0 disables (default), max 120
7446 /* sysfs fan pwm1_enable ----------------------------------------------- */
7447 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
7448 struct device_attribute
*attr
,
7454 res
= fan_get_status_safe(&status
);
7458 if (status
& TP_EC_FAN_FULLSPEED
) {
7460 } else if (status
& TP_EC_FAN_AUTO
) {
7465 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
7468 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
7469 struct device_attribute
*attr
,
7470 const char *buf
, size_t count
)
7475 if (parse_strtoul(buf
, 2, &t
))
7478 tpacpi_disclose_usertask("hwmon pwm1_enable",
7479 "set fan mode to %lu\n", t
);
7483 level
= TP_EC_FAN_FULLSPEED
;
7486 level
= TPACPI_FAN_LAST_LEVEL
;
7489 level
= TP_EC_FAN_AUTO
;
7492 /* reserved for software-controlled auto mode */
7498 res
= fan_set_level_safe(level
);
7504 fan_watchdog_reset();
7509 static struct device_attribute dev_attr_fan_pwm1_enable
=
7510 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
7511 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
7513 /* sysfs fan pwm1 ------------------------------------------------------ */
7514 static ssize_t
fan_pwm1_show(struct device
*dev
,
7515 struct device_attribute
*attr
,
7521 res
= fan_get_status_safe(&status
);
7526 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
7527 status
= fan_control_desired_level
;
7532 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
7535 static ssize_t
fan_pwm1_store(struct device
*dev
,
7536 struct device_attribute
*attr
,
7537 const char *buf
, size_t count
)
7541 u8 status
, newlevel
;
7543 if (parse_strtoul(buf
, 255, &s
))
7546 tpacpi_disclose_usertask("hwmon pwm1",
7547 "set fan speed to %lu\n", s
);
7549 /* scale down from 0-255 to 0-7 */
7550 newlevel
= (s
>> 5) & 0x07;
7552 if (mutex_lock_killable(&fan_mutex
))
7553 return -ERESTARTSYS
;
7555 rc
= fan_get_status(&status
);
7556 if (!rc
&& (status
&
7557 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
7558 rc
= fan_set_level(newlevel
);
7562 fan_update_desired_level(newlevel
);
7563 fan_watchdog_reset();
7567 mutex_unlock(&fan_mutex
);
7568 return (rc
)? rc
: count
;
7571 static struct device_attribute dev_attr_fan_pwm1
=
7572 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
7573 fan_pwm1_show
, fan_pwm1_store
);
7575 /* sysfs fan fan1_input ------------------------------------------------ */
7576 static ssize_t
fan_fan1_input_show(struct device
*dev
,
7577 struct device_attribute
*attr
,
7583 res
= fan_get_speed(&speed
);
7587 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7590 static struct device_attribute dev_attr_fan_fan1_input
=
7591 __ATTR(fan1_input
, S_IRUGO
,
7592 fan_fan1_input_show
, NULL
);
7594 /* sysfs fan fan2_input ------------------------------------------------ */
7595 static ssize_t
fan_fan2_input_show(struct device
*dev
,
7596 struct device_attribute
*attr
,
7602 res
= fan2_get_speed(&speed
);
7606 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7609 static struct device_attribute dev_attr_fan_fan2_input
=
7610 __ATTR(fan2_input
, S_IRUGO
,
7611 fan_fan2_input_show
, NULL
);
7613 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7614 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
7617 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
7620 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
7621 const char *buf
, size_t count
)
7625 if (parse_strtoul(buf
, 120, &t
))
7628 if (!fan_control_allowed
)
7631 fan_watchdog_maxinterval
= t
;
7632 fan_watchdog_reset();
7634 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
7639 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
7640 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
7642 /* --------------------------------------------------------------------- */
7643 static struct attribute
*fan_attributes
[] = {
7644 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
7645 &dev_attr_fan_fan1_input
.attr
,
7646 NULL
, /* for fan2_input */
7650 static const struct attribute_group fan_attr_group
= {
7651 .attrs
= fan_attributes
,
7654 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7655 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7657 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7658 { .vendor = PCI_VENDOR_ID_IBM, \
7659 .bios = TPACPI_MATCH_ANY, \
7660 .ec = TPID(__id1, __id2), \
7661 .quirks = __quirks }
7663 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7664 { .vendor = PCI_VENDOR_ID_LENOVO, \
7665 .bios = TPACPI_MATCH_ANY, \
7666 .ec = TPID(__id1, __id2), \
7667 .quirks = __quirks }
7669 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
7670 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
7671 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
7672 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
7673 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
7674 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
7677 #undef TPACPI_FAN_QL
7678 #undef TPACPI_FAN_QI
7680 static int __init
fan_init(struct ibm_init_struct
*iibm
)
7683 unsigned long quirks
;
7685 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7686 "initializing fan subdriver\n");
7688 mutex_init(&fan_mutex
);
7689 fan_status_access_mode
= TPACPI_FAN_NONE
;
7690 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7691 fan_control_commands
= 0;
7692 fan_watchdog_maxinterval
= 0;
7693 tp_features
.fan_ctrl_status_undef
= 0;
7694 tp_features
.second_fan
= 0;
7695 fan_control_desired_level
= 7;
7697 TPACPI_ACPIHANDLE_INIT(fans
);
7698 TPACPI_ACPIHANDLE_INIT(gfan
);
7699 TPACPI_ACPIHANDLE_INIT(sfan
);
7701 quirks
= tpacpi_check_quirks(fan_quirk_table
,
7702 ARRAY_SIZE(fan_quirk_table
));
7705 /* 570, 600e/x, 770e, 770x */
7706 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
7708 /* all other ThinkPads: note that even old-style
7709 * ThinkPad ECs supports the fan control register */
7710 if (likely(acpi_ec_read(fan_status_offset
,
7711 &fan_control_initial_status
))) {
7712 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
7713 if (quirks
& TPACPI_FAN_Q1
)
7715 if (quirks
& TPACPI_FAN_2FAN
) {
7716 tp_features
.second_fan
= 1;
7717 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7718 "secondary fan support enabled\n");
7722 "ThinkPad ACPI EC access misbehaving, "
7723 "fan status and control unavailable\n");
7730 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
7731 fan_control_commands
|=
7732 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
7735 /* gfan without sfan means no fan control */
7736 /* all other models implement TP EC 0x2f control */
7740 fan_control_access_mode
=
7741 TPACPI_FAN_WR_ACPI_FANS
;
7742 fan_control_commands
|=
7743 TPACPI_FAN_CMD_SPEED
|
7744 TPACPI_FAN_CMD_LEVEL
|
7745 TPACPI_FAN_CMD_ENABLE
;
7747 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
7748 fan_control_commands
|=
7749 TPACPI_FAN_CMD_LEVEL
|
7750 TPACPI_FAN_CMD_ENABLE
;
7755 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7756 "fan is %s, modes %d, %d\n",
7757 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
7758 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
7759 fan_status_access_mode
, fan_control_access_mode
);
7761 /* fan control master switch */
7762 if (!fan_control_allowed
) {
7763 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7764 fan_control_commands
= 0;
7765 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7766 "fan control features disabled by parameter\n");
7769 /* update fan_control_desired_level */
7770 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
7771 fan_get_status_safe(NULL
);
7773 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
7774 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
7775 if (tp_features
.second_fan
) {
7776 /* attach second fan tachometer */
7777 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
7778 &dev_attr_fan_fan2_input
.attr
;
7780 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7785 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
7786 &driver_attr_fan_watchdog
);
7788 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7797 static void fan_exit(void)
7799 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
7800 "cancelling any pending fan watchdog tasks\n");
7802 /* FIXME: can we really do this unconditionally? */
7803 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
7804 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
7805 &driver_attr_fan_watchdog
);
7807 cancel_delayed_work(&fan_watchdog_task
);
7808 flush_workqueue(tpacpi_wq
);
7811 static void fan_suspend(pm_message_t state
)
7815 if (!fan_control_allowed
)
7818 /* Store fan status in cache */
7819 fan_control_resume_level
= 0;
7820 rc
= fan_get_status_safe(&fan_control_resume_level
);
7822 printk(TPACPI_NOTICE
7823 "failed to read fan level for later "
7824 "restore during resume: %d\n", rc
);
7826 /* if it is undefined, don't attempt to restore it.
7828 if (tp_features
.fan_ctrl_status_undef
)
7829 fan_control_resume_level
= 0;
7832 static void fan_resume(void)
7834 u8 current_level
= 7;
7835 bool do_set
= false;
7838 /* DSDT *always* updates status on resume */
7839 tp_features
.fan_ctrl_status_undef
= 0;
7841 if (!fan_control_allowed
||
7842 !fan_control_resume_level
||
7843 (fan_get_status_safe(¤t_level
) < 0))
7846 switch (fan_control_access_mode
) {
7847 case TPACPI_FAN_WR_ACPI_SFAN
:
7848 /* never decrease fan level */
7849 do_set
= (fan_control_resume_level
> current_level
);
7851 case TPACPI_FAN_WR_ACPI_FANS
:
7852 case TPACPI_FAN_WR_TPEC
:
7853 /* never decrease fan level, scale is:
7854 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7856 * We expect the firmware to set either 7 or AUTO, but we
7857 * handle FULLSPEED out of paranoia.
7859 * So, we can safely only restore FULLSPEED or 7, anything
7860 * else could slow the fan. Restoring AUTO is useless, at
7861 * best that's exactly what the DSDT already set (it is the
7864 * Always keep in mind that the DSDT *will* have set the
7865 * fans to what the vendor supposes is the best level. We
7866 * muck with it only to speed the fan up.
7868 if (fan_control_resume_level
!= 7 &&
7869 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
7872 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
7873 (current_level
!= fan_control_resume_level
);
7879 printk(TPACPI_NOTICE
7880 "restoring fan level to 0x%02x\n",
7881 fan_control_resume_level
);
7882 rc
= fan_set_level_safe(fan_control_resume_level
);
7884 printk(TPACPI_NOTICE
7885 "failed to restore fan level: %d\n", rc
);
7889 static int fan_read(char *p
)
7894 unsigned int speed
= 0;
7896 switch (fan_status_access_mode
) {
7897 case TPACPI_FAN_RD_ACPI_GFAN
:
7898 /* 570, 600e/x, 770e, 770x */
7899 rc
= fan_get_status_safe(&status
);
7903 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
7905 (status
!= 0) ? "enabled" : "disabled", status
);
7908 case TPACPI_FAN_RD_TPEC
:
7909 /* all except 570, 600e/x, 770e, 770x */
7910 rc
= fan_get_status_safe(&status
);
7914 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
7915 (status
!= 0) ? "enabled" : "disabled");
7917 rc
= fan_get_speed(&speed
);
7921 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
7923 if (status
& TP_EC_FAN_FULLSPEED
)
7924 /* Disengaged mode takes precedence */
7925 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
7926 else if (status
& TP_EC_FAN_AUTO
)
7927 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
7929 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
7932 case TPACPI_FAN_NONE
:
7934 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
7937 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
7938 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
7940 switch (fan_control_access_mode
) {
7941 case TPACPI_FAN_WR_ACPI_SFAN
:
7942 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
7946 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
7947 "auto, disengaged, full-speed)\n");
7952 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
7953 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
7954 "commands:\twatchdog <timeout> (<timeout> "
7955 "is 0 (off), 1-120 (seconds))\n");
7957 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
7958 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
7959 " (<speed> is 0-65535)\n");
7964 static int fan_write_cmd_level(const char *cmd
, int *rc
)
7968 if (strlencmp(cmd
, "level auto") == 0)
7969 level
= TP_EC_FAN_AUTO
;
7970 else if ((strlencmp(cmd
, "level disengaged") == 0) |
7971 (strlencmp(cmd
, "level full-speed") == 0))
7972 level
= TP_EC_FAN_FULLSPEED
;
7973 else if (sscanf(cmd
, "level %d", &level
) != 1)
7976 *rc
= fan_set_level_safe(level
);
7978 printk(TPACPI_ERR
"level command accepted for unsupported "
7979 "access mode %d", fan_control_access_mode
);
7981 tpacpi_disclose_usertask("procfs fan",
7982 "set level to %d\n", level
);
7987 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
7989 if (strlencmp(cmd
, "enable") != 0)
7992 *rc
= fan_set_enable();
7994 printk(TPACPI_ERR
"enable command accepted for unsupported "
7995 "access mode %d", fan_control_access_mode
);
7997 tpacpi_disclose_usertask("procfs fan", "enable\n");
8002 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
8004 if (strlencmp(cmd
, "disable") != 0)
8007 *rc
= fan_set_disable();
8009 printk(TPACPI_ERR
"disable command accepted for unsupported "
8010 "access mode %d", fan_control_access_mode
);
8012 tpacpi_disclose_usertask("procfs fan", "disable\n");
8017 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
8022 * Support speed <low> <medium> <high> ? */
8024 if (sscanf(cmd
, "speed %d", &speed
) != 1)
8027 *rc
= fan_set_speed(speed
);
8029 printk(TPACPI_ERR
"speed command accepted for unsupported "
8030 "access mode %d", fan_control_access_mode
);
8032 tpacpi_disclose_usertask("procfs fan",
8033 "set speed to %d\n", speed
);
8038 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
8042 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
8045 if (interval
< 0 || interval
> 120)
8048 fan_watchdog_maxinterval
= interval
;
8049 tpacpi_disclose_usertask("procfs fan",
8050 "set watchdog timer to %d\n",
8057 static int fan_write(char *buf
)
8062 while (!rc
&& (cmd
= next_cmd(&buf
))) {
8063 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
8064 fan_write_cmd_level(cmd
, &rc
)) &&
8065 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
8066 (fan_write_cmd_enable(cmd
, &rc
) ||
8067 fan_write_cmd_disable(cmd
, &rc
) ||
8068 fan_write_cmd_watchdog(cmd
, &rc
))) &&
8069 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
8070 fan_write_cmd_speed(cmd
, &rc
))
8074 fan_watchdog_reset();
8080 static struct ibm_struct fan_driver_data
= {
8085 .suspend
= fan_suspend
,
8086 .resume
= fan_resume
,
8089 /****************************************************************************
8090 ****************************************************************************
8094 ****************************************************************************
8095 ****************************************************************************/
8098 * HKEY event callout for other subdrivers go here
8099 * (yes, it is ugly, but it is quick, safe, and gets the job done
8101 static void tpacpi_driver_event(const unsigned int hkey_event
)
8103 if (ibm_backlight_device
) {
8104 switch (hkey_event
) {
8105 case TP_HKEY_EV_BRGHT_UP
:
8106 case TP_HKEY_EV_BRGHT_DOWN
:
8107 tpacpi_brightness_notify_change();
8114 static void hotkey_driver_event(const unsigned int scancode
)
8116 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
8119 /* sysfs name ---------------------------------------------------------- */
8120 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
8121 struct device_attribute
*attr
,
8124 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
8127 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
8128 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
8130 /* --------------------------------------------------------------------- */
8133 static struct proc_dir_entry
*proc_dir
;
8136 * Module and infrastructure proble, init and exit handling
8139 static int force_load
;
8141 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8142 static const char * __init
str_supported(int is_supported
)
8144 static char text_unsupported
[] __initdata
= "not supported";
8146 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
8148 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8150 static void ibm_exit(struct ibm_struct
*ibm
)
8152 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
8154 list_del_init(&ibm
->all_drivers
);
8156 if (ibm
->flags
.acpi_notify_installed
) {
8157 dbg_printk(TPACPI_DBG_EXIT
,
8158 "%s: acpi_remove_notify_handler\n", ibm
->name
);
8160 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
8162 dispatch_acpi_notify
);
8163 ibm
->flags
.acpi_notify_installed
= 0;
8164 ibm
->flags
.acpi_notify_installed
= 0;
8167 if (ibm
->flags
.proc_created
) {
8168 dbg_printk(TPACPI_DBG_EXIT
,
8169 "%s: remove_proc_entry\n", ibm
->name
);
8170 remove_proc_entry(ibm
->name
, proc_dir
);
8171 ibm
->flags
.proc_created
= 0;
8174 if (ibm
->flags
.acpi_driver_registered
) {
8175 dbg_printk(TPACPI_DBG_EXIT
,
8176 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
8178 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
8179 kfree(ibm
->acpi
->driver
);
8180 ibm
->acpi
->driver
= NULL
;
8181 ibm
->flags
.acpi_driver_registered
= 0;
8184 if (ibm
->flags
.init_called
&& ibm
->exit
) {
8186 ibm
->flags
.init_called
= 0;
8189 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
8192 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
8195 struct ibm_struct
*ibm
= iibm
->data
;
8196 struct proc_dir_entry
*entry
;
8198 BUG_ON(ibm
== NULL
);
8200 INIT_LIST_HEAD(&ibm
->all_drivers
);
8202 if (ibm
->flags
.experimental
&& !experimental
)
8205 dbg_printk(TPACPI_DBG_INIT
,
8206 "probing for %s\n", ibm
->name
);
8209 ret
= iibm
->init(iibm
);
8211 return 0; /* probe failed */
8215 ibm
->flags
.init_called
= 1;
8219 if (ibm
->acpi
->hid
) {
8220 ret
= register_tpacpi_subdriver(ibm
);
8225 if (ibm
->acpi
->notify
) {
8226 ret
= setup_acpi_notify(ibm
);
8227 if (ret
== -ENODEV
) {
8228 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
8238 dbg_printk(TPACPI_DBG_INIT
,
8239 "%s installed\n", ibm
->name
);
8242 entry
= create_proc_entry(ibm
->name
,
8243 S_IFREG
| S_IRUGO
| S_IWUSR
,
8246 printk(TPACPI_ERR
"unable to create proc entry %s\n",
8252 entry
->read_proc
= &dispatch_procfs_read
;
8254 entry
->write_proc
= &dispatch_procfs_write
;
8255 ibm
->flags
.proc_created
= 1;
8258 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
8263 dbg_printk(TPACPI_DBG_INIT
,
8264 "%s: at error exit path with result %d\n",
8268 return (ret
< 0)? ret
: 0;
8273 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
8275 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
8278 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8279 static bool __pure __init
tpacpi_is_valid_fw_id(const char* const s
,
8282 return s
&& strlen(s
) >= 8 &&
8283 tpacpi_is_fw_digit(s
[0]) &&
8284 tpacpi_is_fw_digit(s
[1]) &&
8285 s
[2] == t
&& s
[3] == 'T' &&
8286 tpacpi_is_fw_digit(s
[4]) &&
8287 tpacpi_is_fw_digit(s
[5]) &&
8288 s
[6] == 'W' && s
[7] == 'W';
8291 /* returns 0 - probe ok, or < 0 - probe error.
8292 * Probe ok doesn't mean thinkpad found.
8293 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8294 static int __must_check __init
get_thinkpad_model_data(
8295 struct thinkpad_id_data
*tp
)
8297 const struct dmi_device
*dev
= NULL
;
8298 char ec_fw_string
[18];
8304 memset(tp
, 0, sizeof(*tp
));
8306 if (dmi_name_in_vendors("IBM"))
8307 tp
->vendor
= PCI_VENDOR_ID_IBM
;
8308 else if (dmi_name_in_vendors("LENOVO"))
8309 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
8313 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
8314 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
8315 if (s
&& !tp
->bios_version_str
)
8318 /* Really ancient ThinkPad 240X will fail this, which is fine */
8319 if (!tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E'))
8322 tp
->bios_model
= tp
->bios_version_str
[0]
8323 | (tp
->bios_version_str
[1] << 8);
8324 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
8325 | tp
->bios_version_str
[5];
8328 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8329 * X32 or newer, all Z series; Some models must have an
8330 * up-to-date BIOS or they will not be detected.
8332 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8334 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
8335 if (sscanf(dev
->name
,
8336 "IBM ThinkPad Embedded Controller -[%17c",
8337 ec_fw_string
) == 1) {
8338 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
8339 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
8341 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
8342 if (!tp
->ec_version_str
)
8345 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
8346 tp
->ec_model
= ec_fw_string
[0]
8347 | (ec_fw_string
[1] << 8);
8348 tp
->ec_release
= (ec_fw_string
[4] << 8)
8351 printk(TPACPI_NOTICE
8352 "ThinkPad firmware release %s "
8353 "doesn't match the known patterns\n",
8355 printk(TPACPI_NOTICE
8356 "please report this to %s\n",
8363 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
8364 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
8365 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
8370 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
8371 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
8372 if (s
&& !tp
->nummodel_str
)
8378 static int __init
probe_for_thinkpad(void)
8386 * Non-ancient models have better DMI tagging, but very old models
8387 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8389 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
) ||
8390 (thinkpad_id
.ec_model
!= 0) ||
8391 tpacpi_is_fw_known();
8393 /* ec is required because many other handles are relative to it */
8394 TPACPI_ACPIHANDLE_INIT(ec
);
8398 "Not yet supported ThinkPad detected!\n");
8402 if (!is_thinkpad
&& !force_load
)
8409 /* Module init, exit, parameters */
8411 static struct ibm_init_struct ibms_init
[] __initdata
= {
8413 .init
= thinkpad_acpi_driver_init
,
8414 .data
= &thinkpad_acpi_driver_data
,
8417 .init
= hotkey_init
,
8418 .data
= &hotkey_driver_data
,
8421 .init
= bluetooth_init
,
8422 .data
= &bluetooth_driver_data
,
8426 .data
= &wan_driver_data
,
8430 .data
= &uwb_driver_data
,
8432 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8435 .data
= &video_driver_data
,
8440 .data
= &light_driver_data
,
8444 .data
= &cmos_driver_data
,
8448 .data
= &led_driver_data
,
8452 .data
= &beep_driver_data
,
8455 .init
= thermal_init
,
8456 .data
= &thermal_driver_data
,
8459 .data
= &ecdump_driver_data
,
8462 .init
= brightness_init
,
8463 .data
= &brightness_driver_data
,
8466 .init
= volume_init
,
8467 .data
= &volume_driver_data
,
8471 .data
= &fan_driver_data
,
8475 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
8478 struct ibm_struct
*ibm
;
8480 if (!kp
|| !kp
->name
|| !val
)
8483 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8484 ibm
= ibms_init
[i
].data
;
8485 WARN_ON(ibm
== NULL
);
8487 if (!ibm
|| !ibm
->name
)
8490 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
8491 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
8493 strcpy(ibms_init
[i
].param
, val
);
8494 strcat(ibms_init
[i
].param
, ",");
8502 module_param(experimental
, int, 0444);
8503 MODULE_PARM_DESC(experimental
,
8504 "Enables experimental features when non-zero");
8506 module_param_named(debug
, dbg_level
, uint
, 0);
8507 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
8509 module_param(force_load
, bool, 0444);
8510 MODULE_PARM_DESC(force_load
,
8511 "Attempts to load the driver even on a "
8512 "mis-identified ThinkPad when true");
8514 module_param_named(fan_control
, fan_control_allowed
, bool, 0444);
8515 MODULE_PARM_DESC(fan_control
,
8516 "Enables setting fan parameters features when true");
8518 module_param_named(brightness_mode
, brightness_mode
, uint
, 0444);
8519 MODULE_PARM_DESC(brightness_mode
,
8520 "Selects brightness control strategy: "
8521 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8523 module_param(brightness_enable
, uint
, 0444);
8524 MODULE_PARM_DESC(brightness_enable
,
8525 "Enables backlight control when 1, disables when 0");
8527 module_param(hotkey_report_mode
, uint
, 0444);
8528 MODULE_PARM_DESC(hotkey_report_mode
,
8529 "used for backwards compatibility with userspace, "
8530 "see documentation");
8532 module_param_named(volume_mode
, volume_mode
, uint
, 0444);
8533 MODULE_PARM_DESC(volume_mode
,
8534 "Selects volume control strategy: "
8535 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8537 module_param_named(volume_capabilities
, volume_capabilities
, uint
, 0444);
8538 MODULE_PARM_DESC(volume_capabilities
,
8539 "Selects the mixer capabilites: "
8540 "0=auto, 1=volume and mute, 2=mute only");
8542 module_param_named(volume_control
, volume_control_allowed
, bool, 0444);
8543 MODULE_PARM_DESC(volume_control
,
8544 "Enables software override for the console audio "
8545 "control when true");
8547 #define TPACPI_PARAM(feature) \
8548 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8549 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8550 "at module load, see documentation")
8552 TPACPI_PARAM(hotkey
);
8553 TPACPI_PARAM(bluetooth
);
8554 TPACPI_PARAM(video
);
8555 TPACPI_PARAM(light
);
8559 TPACPI_PARAM(ecdump
);
8560 TPACPI_PARAM(brightness
);
8561 TPACPI_PARAM(volume
);
8564 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8565 module_param(dbg_wlswemul
, uint
, 0444);
8566 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
8567 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
8568 MODULE_PARM_DESC(wlsw_state
,
8569 "Initial state of the emulated WLSW switch");
8571 module_param(dbg_bluetoothemul
, uint
, 0444);
8572 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
8573 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
8574 MODULE_PARM_DESC(bluetooth_state
,
8575 "Initial state of the emulated bluetooth switch");
8577 module_param(dbg_wwanemul
, uint
, 0444);
8578 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
8579 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
8580 MODULE_PARM_DESC(wwan_state
,
8581 "Initial state of the emulated WWAN switch");
8583 module_param(dbg_uwbemul
, uint
, 0444);
8584 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
8585 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
8586 MODULE_PARM_DESC(uwb_state
,
8587 "Initial state of the emulated UWB switch");
8590 static void thinkpad_acpi_module_exit(void)
8592 struct ibm_struct
*ibm
, *itmp
;
8594 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
8596 list_for_each_entry_safe_reverse(ibm
, itmp
,
8597 &tpacpi_all_drivers
,
8602 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
8604 if (tpacpi_inputdev
) {
8605 if (tp_features
.input_device_registered
)
8606 input_unregister_device(tpacpi_inputdev
);
8608 input_free_device(tpacpi_inputdev
);
8612 hwmon_device_unregister(tpacpi_hwmon
);
8614 if (tp_features
.sensors_pdev_attrs_registered
)
8615 device_remove_file(&tpacpi_sensors_pdev
->dev
,
8616 &dev_attr_thinkpad_acpi_pdev_name
);
8617 if (tpacpi_sensors_pdev
)
8618 platform_device_unregister(tpacpi_sensors_pdev
);
8620 platform_device_unregister(tpacpi_pdev
);
8622 if (tp_features
.sensors_pdrv_attrs_registered
)
8623 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
8624 if (tp_features
.platform_drv_attrs_registered
)
8625 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
8627 if (tp_features
.sensors_pdrv_registered
)
8628 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
8630 if (tp_features
.platform_drv_registered
)
8631 platform_driver_unregister(&tpacpi_pdriver
);
8634 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
8637 destroy_workqueue(tpacpi_wq
);
8639 kfree(thinkpad_id
.bios_version_str
);
8640 kfree(thinkpad_id
.ec_version_str
);
8641 kfree(thinkpad_id
.model_str
);
8645 static int __init
thinkpad_acpi_module_init(void)
8649 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
8651 /* Parameter checking */
8652 if (hotkey_report_mode
> 2)
8655 /* Driver-level probe */
8657 ret
= get_thinkpad_model_data(&thinkpad_id
);
8660 "unable to get DMI data: %d\n", ret
);
8661 thinkpad_acpi_module_exit();
8664 ret
= probe_for_thinkpad();
8666 thinkpad_acpi_module_exit();
8670 /* Driver initialization */
8672 TPACPI_ACPIHANDLE_INIT(ecrd
);
8673 TPACPI_ACPIHANDLE_INIT(ecwr
);
8675 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
8677 thinkpad_acpi_module_exit();
8681 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
8684 "unable to create proc dir " TPACPI_PROC_DIR
);
8685 thinkpad_acpi_module_exit();
8689 ret
= platform_driver_register(&tpacpi_pdriver
);
8692 "unable to register main platform driver\n");
8693 thinkpad_acpi_module_exit();
8696 tp_features
.platform_drv_registered
= 1;
8698 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
8701 "unable to register hwmon platform driver\n");
8702 thinkpad_acpi_module_exit();
8705 tp_features
.sensors_pdrv_registered
= 1;
8707 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
8709 tp_features
.platform_drv_attrs_registered
= 1;
8710 ret
= tpacpi_create_driver_attributes(
8711 &tpacpi_hwmon_pdriver
.driver
);
8715 "unable to create sysfs driver attributes\n");
8716 thinkpad_acpi_module_exit();
8719 tp_features
.sensors_pdrv_attrs_registered
= 1;
8722 /* Device initialization */
8723 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
8725 if (IS_ERR(tpacpi_pdev
)) {
8726 ret
= PTR_ERR(tpacpi_pdev
);
8728 printk(TPACPI_ERR
"unable to register platform device\n");
8729 thinkpad_acpi_module_exit();
8732 tpacpi_sensors_pdev
= platform_device_register_simple(
8733 TPACPI_HWMON_DRVR_NAME
,
8735 if (IS_ERR(tpacpi_sensors_pdev
)) {
8736 ret
= PTR_ERR(tpacpi_sensors_pdev
);
8737 tpacpi_sensors_pdev
= NULL
;
8739 "unable to register hwmon platform device\n");
8740 thinkpad_acpi_module_exit();
8743 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
8744 &dev_attr_thinkpad_acpi_pdev_name
);
8747 "unable to create sysfs hwmon device attributes\n");
8748 thinkpad_acpi_module_exit();
8751 tp_features
.sensors_pdev_attrs_registered
= 1;
8752 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
8753 if (IS_ERR(tpacpi_hwmon
)) {
8754 ret
= PTR_ERR(tpacpi_hwmon
);
8755 tpacpi_hwmon
= NULL
;
8756 printk(TPACPI_ERR
"unable to register hwmon device\n");
8757 thinkpad_acpi_module_exit();
8760 mutex_init(&tpacpi_inputdev_send_mutex
);
8761 tpacpi_inputdev
= input_allocate_device();
8762 if (!tpacpi_inputdev
) {
8763 printk(TPACPI_ERR
"unable to allocate input device\n");
8764 thinkpad_acpi_module_exit();
8767 /* Prepare input device, but don't register */
8768 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
8769 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
8770 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
8771 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
8772 thinkpad_id
.vendor
:
8774 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
8775 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
8776 tpacpi_inputdev
->dev
.parent
= &tpacpi_pdev
->dev
;
8778 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8779 ret
= ibm_init(&ibms_init
[i
]);
8780 if (ret
>= 0 && *ibms_init
[i
].param
)
8781 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
8783 thinkpad_acpi_module_exit();
8787 ret
= input_register_device(tpacpi_inputdev
);
8789 printk(TPACPI_ERR
"unable to register input device\n");
8790 thinkpad_acpi_module_exit();
8793 tp_features
.input_device_registered
= 1;
8796 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
8800 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
8803 * This will autoload the driver in almost every ThinkPad
8804 * in widespread use.
8806 * Only _VERY_ old models, like the 240, 240x and 570 lack
8807 * the HKEY event interface.
8809 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
8812 * DMI matching for module autoloading
8814 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8815 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8817 * Only models listed in thinkwiki will be supported, so add yours
8818 * if it is not there yet.
8820 #define IBM_BIOS_MODULE_ALIAS(__type) \
8821 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8823 /* Ancient thinkpad BIOSes have to be identified by
8824 * BIOS type or model number, and there are far less
8825 * BIOS types than model numbers... */
8826 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8828 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8829 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8830 MODULE_DESCRIPTION(TPACPI_DESC
);
8831 MODULE_VERSION(TPACPI_VERSION
);
8832 MODULE_LICENSE("GPL");
8834 module_init(thinkpad_acpi_module_init
);
8835 module_exit(thinkpad_acpi_module_exit
);