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 0x020400
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 */
148 /****************************************************************************
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
170 #define TPACPI_LOG TPACPI_FILE ": "
171 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174 #define TPACPI_ERR KERN_ERR TPACPI_LOG
175 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
176 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177 #define TPACPI_INFO KERN_INFO TPACPI_LOG
178 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
180 /* Debugging printk groups */
181 #define TPACPI_DBG_ALL 0xffff
182 #define TPACPI_DBG_DISCLOSETASK 0x8000
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define TPACPI_DBG_RFKILL 0x0004
186 #define TPACPI_DBG_HKEY 0x0008
187 #define TPACPI_DBG_FAN 0x0010
188 #define TPACPI_DBG_BRGHT 0x0020
190 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
195 /****************************************************************************
196 * Driver-wide structs and misc. variables
201 struct tp_acpi_drv_struct
{
202 const struct acpi_device_id
*hid
;
203 struct acpi_driver
*driver
;
205 void (*notify
) (struct ibm_struct
*, u32
);
208 struct acpi_device
*device
;
214 int (*read
) (char *);
215 int (*write
) (char *);
217 void (*resume
) (void);
218 void (*suspend
) (pm_message_t state
);
219 void (*shutdown
) (void);
221 struct list_head all_drivers
;
223 struct tp_acpi_drv_struct
*acpi
;
226 u8 acpi_driver_registered
:1;
227 u8 acpi_notify_installed
:1;
234 struct ibm_init_struct
{
237 int (*init
) (struct ibm_init_struct
*);
238 struct ibm_struct
*data
;
249 u32 bright_16levels
:1;
250 u32 bright_acpimode
:1;
253 u32 fan_ctrl_status_undef
:1;
255 u32 beep_needs_two_args
:1;
256 u32 input_device_registered
:1;
257 u32 platform_drv_registered
:1;
258 u32 platform_drv_attrs_registered
:1;
259 u32 sensors_pdrv_registered
:1;
260 u32 sensors_pdrv_attrs_registered
:1;
261 u32 sensors_pdev_attrs_registered
:1;
262 u32 hotkey_poll_active
:1;
266 u16 hotkey_mask_ff
:1;
269 struct thinkpad_id_data
{
270 unsigned int vendor
; /* ThinkPad vendor:
271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
273 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
274 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
276 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
278 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
281 char *model_str
; /* ThinkPad T43 */
282 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
284 static struct thinkpad_id_data thinkpad_id
;
287 TPACPI_LIFE_INIT
= 0,
292 static int experimental
;
293 static u32 dbg_level
;
295 static struct workqueue_struct
*tpacpi_wq
;
303 /* Special LED class that can defer work */
304 struct tpacpi_led_classdev
{
305 struct led_classdev led_classdev
;
306 struct work_struct work
;
307 enum led_status_t new_state
;
311 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312 static int dbg_wlswemul
;
313 static int tpacpi_wlsw_emulstate
;
314 static int dbg_bluetoothemul
;
315 static int tpacpi_bluetooth_emulstate
;
316 static int dbg_wwanemul
;
317 static int tpacpi_wwan_emulstate
;
318 static int dbg_uwbemul
;
319 static int tpacpi_uwb_emulstate
;
323 /*************************************************************************
327 #define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
332 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
333 #define vdbg_printk dbg_printk
334 static const char *str_supported(int is_supported
);
336 #define vdbg_printk(a_dbg_level, format, arg...) \
340 static void tpacpi_log_usertask(const char * const what
)
342 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
343 what
, task_tgid_vnr(current
));
346 #define tpacpi_disclose_usertask(what, format, arg...) \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
357 * Quirk handling helpers
359 * ThinkPad IDs and versions seen in the field so far
360 * are two-characters from the set [0-9A-Z], i.e. base 36.
362 * We use values well outside that range as specials.
365 #define TPACPI_MATCH_ANY 0xffffU
366 #define TPACPI_MATCH_UNKNOWN 0U
368 /* TPID('1', 'Y') == 0x5931 */
369 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
371 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
372 { .vendor = PCI_VENDOR_ID_IBM, \
373 .bios = TPID(__id1, __id2), \
374 .ec = TPACPI_MATCH_ANY, \
375 .quirks = (__quirk) }
377 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
378 { .vendor = PCI_VENDOR_ID_LENOVO, \
379 .bios = TPID(__id1, __id2), \
380 .ec = TPACPI_MATCH_ANY, \
381 .quirks = (__quirk) }
383 struct tpacpi_quirk
{
387 unsigned long quirks
;
391 * tpacpi_check_quirks() - search BIOS/EC version on a list
392 * @qlist: array of &struct tpacpi_quirk
393 * @qlist_size: number of elements in @qlist
395 * Iterates over a quirks list until one is found that matches the
396 * ThinkPad's vendor, BIOS and EC model.
398 * Returns 0 if nothing matches, otherwise returns the quirks field of
399 * the matching &struct tpacpi_quirk entry.
401 * The match criteria is: vendor, ec and bios much match.
403 static unsigned long __init
tpacpi_check_quirks(
404 const struct tpacpi_quirk
*qlist
,
405 unsigned int qlist_size
)
408 if ((qlist
->vendor
== thinkpad_id
.vendor
||
409 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
410 (qlist
->bios
== thinkpad_id
.bios_model
||
411 qlist
->bios
== TPACPI_MATCH_ANY
) &&
412 (qlist
->ec
== thinkpad_id
.ec_model
||
413 qlist
->ec
== TPACPI_MATCH_ANY
))
414 return qlist
->quirks
;
423 /****************************************************************************
424 ****************************************************************************
426 * ACPI Helpers and device model
428 ****************************************************************************
429 ****************************************************************************/
431 /*************************************************************************
435 static acpi_handle root_handle
;
437 #define TPACPI_HANDLE(object, parent, paths...) \
438 static acpi_handle object##_handle; \
439 static acpi_handle *object##_parent = &parent##_handle; \
440 static char *object##_path; \
441 static char *object##_paths[] = { paths }
443 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
444 "\\_SB.PCI.ISA.EC", /* 570 */
445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
448 "\\_SB.PCI0.ICH3.EC0", /* R31 */
449 "\\_SB.PCI0.LPC.EC", /* all others */
452 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
453 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
455 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
458 "\\CMS", /* R40, R40e */
461 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
462 "^HKEY", /* R30, R31 */
463 "HKEY", /* all others */
466 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
468 "\\_SB.PCI0.VID0", /* 770e */
469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
470 "\\_SB.PCI0.AGP.VID", /* all others */
474 /*************************************************************************
478 static int acpi_evalf(acpi_handle handle
,
479 void *res
, char *method
, char *fmt
, ...)
482 struct acpi_object_list params
;
483 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
484 struct acpi_buffer result
, *resultp
;
485 union acpi_object out_obj
;
493 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
506 params
.pointer
= &in_objs
[0];
513 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
514 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
516 /* add more types as needed */
518 printk(TPACPI_ERR
"acpi_evalf() called "
519 "with invalid format character '%c'\n", c
);
525 if (res_type
!= 'v') {
526 result
.length
= sizeof(out_obj
);
527 result
.pointer
= &out_obj
;
532 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
537 *(int *)res
= out_obj
.integer
.value
;
538 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
541 success
= status
== AE_OK
;
543 /* add more types as needed */
545 printk(TPACPI_ERR
"acpi_evalf() called "
546 "with invalid format character '%c'\n", res_type
);
550 if (!success
&& !quiet
)
551 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
552 method
, fmt0
, status
);
557 static int acpi_ec_read(int i
, u8
*p
)
562 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
566 if (ec_read(i
, p
) < 0)
573 static int acpi_ec_write(int i
, u8 v
)
576 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
579 if (ec_write(i
, v
) < 0)
586 static int issue_thinkpad_cmos_command(int cmos_cmd
)
591 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
597 /*************************************************************************
601 #define TPACPI_ACPIHANDLE_INIT(object) \
602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
605 static void drv_acpi_handle_init(char *name
,
606 acpi_handle
*handle
, acpi_handle parent
,
607 char **paths
, int num_paths
, char **path
)
612 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
615 for (i
= 0; i
< num_paths
; i
++) {
616 status
= acpi_get_handle(parent
, paths
[i
], handle
);
617 if (ACPI_SUCCESS(status
)) {
619 dbg_printk(TPACPI_DBG_INIT
,
620 "Found ACPI handle %s for %s\n",
626 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
631 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
633 struct ibm_struct
*ibm
= data
;
635 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
638 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
641 ibm
->acpi
->notify(ibm
, event
);
644 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
651 if (!*ibm
->acpi
->handle
)
654 vdbg_printk(TPACPI_DBG_INIT
,
655 "setting up ACPI notify for %s\n", ibm
->name
);
657 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
659 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
664 ibm
->acpi
->device
->driver_data
= ibm
;
665 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
666 TPACPI_ACPI_EVENT_PREFIX
,
669 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
670 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
671 if (ACPI_FAILURE(status
)) {
672 if (status
== AE_ALREADY_EXISTS
) {
674 "another device driver is already "
675 "handling %s events\n", ibm
->name
);
678 "acpi_install_notify_handler(%s) failed: %d\n",
683 ibm
->flags
.acpi_notify_installed
= 1;
687 static int __init
tpacpi_device_add(struct acpi_device
*device
)
692 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
696 dbg_printk(TPACPI_DBG_INIT
,
697 "registering %s as an ACPI driver\n", ibm
->name
);
701 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
702 if (!ibm
->acpi
->driver
) {
704 "failed to allocate memory for ibm->acpi->driver\n");
708 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
709 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
711 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
713 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
715 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
717 kfree(ibm
->acpi
->driver
);
718 ibm
->acpi
->driver
= NULL
;
720 ibm
->flags
.acpi_driver_registered
= 1;
726 /****************************************************************************
727 ****************************************************************************
731 ****************************************************************************
732 ****************************************************************************/
734 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
735 int count
, int *eof
, void *data
)
737 struct ibm_struct
*ibm
= data
;
740 if (!ibm
|| !ibm
->read
)
743 len
= ibm
->read(page
);
747 if (len
<= off
+ count
)
759 static int dispatch_procfs_write(struct file
*file
,
760 const char __user
*userbuf
,
761 unsigned long count
, void *data
)
763 struct ibm_struct
*ibm
= data
;
767 if (!ibm
|| !ibm
->write
)
769 if (count
> PAGE_SIZE
- 2)
772 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
776 if (copy_from_user(kernbuf
, userbuf
, count
)) {
782 strcat(kernbuf
, ",");
783 ret
= ibm
->write(kernbuf
);
792 static char *next_cmd(char **cmds
)
797 while ((end
= strchr(start
, ',')) && end
== start
)
809 /****************************************************************************
810 ****************************************************************************
812 * Device model: input, hwmon and platform
814 ****************************************************************************
815 ****************************************************************************/
817 static struct platform_device
*tpacpi_pdev
;
818 static struct platform_device
*tpacpi_sensors_pdev
;
819 static struct device
*tpacpi_hwmon
;
820 static struct input_dev
*tpacpi_inputdev
;
821 static struct mutex tpacpi_inputdev_send_mutex
;
822 static LIST_HEAD(tpacpi_all_drivers
);
824 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
827 struct ibm_struct
*ibm
, *itmp
;
829 list_for_each_entry_safe(ibm
, itmp
,
833 (ibm
->suspend
)(state
);
839 static int tpacpi_resume_handler(struct platform_device
*pdev
)
841 struct ibm_struct
*ibm
, *itmp
;
843 list_for_each_entry_safe(ibm
, itmp
,
853 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
855 struct ibm_struct
*ibm
, *itmp
;
857 list_for_each_entry_safe(ibm
, itmp
,
865 static struct platform_driver tpacpi_pdriver
= {
867 .name
= TPACPI_DRVR_NAME
,
868 .owner
= THIS_MODULE
,
870 .suspend
= tpacpi_suspend_handler
,
871 .resume
= tpacpi_resume_handler
,
872 .shutdown
= tpacpi_shutdown_handler
,
875 static struct platform_driver tpacpi_hwmon_pdriver
= {
877 .name
= TPACPI_HWMON_DRVR_NAME
,
878 .owner
= THIS_MODULE
,
882 /*************************************************************************
883 * sysfs support helpers
886 struct attribute_set
{
887 unsigned int members
, max_members
;
888 struct attribute_group group
;
891 struct attribute_set_obj
{
892 struct attribute_set s
;
894 } __attribute__((packed
));
896 static struct attribute_set
*create_attr_set(unsigned int max_members
,
899 struct attribute_set_obj
*sobj
;
901 if (max_members
== 0)
904 /* Allocates space for implicit NULL at the end too */
905 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
906 max_members
* sizeof(struct attribute
*),
910 sobj
->s
.max_members
= max_members
;
911 sobj
->s
.group
.attrs
= &sobj
->a
;
912 sobj
->s
.group
.name
= name
;
917 #define destroy_attr_set(_set) \
920 /* not multi-threaded safe, use it in a single thread per set */
921 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
926 if (s
->members
>= s
->max_members
)
929 s
->group
.attrs
[s
->members
] = attr
;
935 static int add_many_to_attr_set(struct attribute_set
*s
,
936 struct attribute
**attr
,
941 for (i
= 0; i
< count
; i
++) {
942 res
= add_to_attr_set(s
, attr
[i
]);
950 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
952 sysfs_remove_group(kobj
, &s
->group
);
956 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
959 static int parse_strtoul(const char *buf
,
960 unsigned long max
, unsigned long *value
)
964 while (*buf
&& isspace(*buf
))
966 *value
= simple_strtoul(buf
, &endp
, 0);
967 while (*endp
&& isspace(*endp
))
969 if (*endp
|| *value
> max
)
975 static void tpacpi_disable_brightness_delay(void)
977 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
979 "ACPI backlight control delay disabled\n");
982 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
984 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
985 union acpi_object
*obj
;
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
989 obj
= (union acpi_object
*)buffer
.pointer
;
990 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
991 printk(TPACPI_ERR
"Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL
);
995 rc
= obj
->package
.count
;
1001 kfree(buffer
.pointer
);
1005 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
1006 u32 lvl
, void *context
, void **rv
)
1008 char name
[ACPI_PATH_SEGMENT_LENGTH
];
1009 struct acpi_buffer buffer
= { sizeof(name
), &name
};
1011 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
1012 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
1013 BUG_ON(!rv
|| !*rv
);
1014 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
1015 return AE_CTRL_TERMINATE
;
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1024 static int __init
tpacpi_check_std_acpi_brightness_support(void)
1028 void *bcl_ptr
= &bcl_levels
;
1031 TPACPI_ACPIHANDLE_INIT(vid
);
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1044 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
1045 tpacpi_acpi_walk_find_bcl
, NULL
,
1048 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
1049 tp_features
.bright_acpimode
= 1;
1050 return (bcl_levels
- 2);
1056 static void printk_deprecated_attribute(const char * const what
,
1057 const char * const details
)
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1065 /*************************************************************************
1066 * rfkill and radio control support helpers
1070 * ThinkPad-ACPI firmware handling model:
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1084 * Internal driver API for radio state:
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1089 enum tpacpi_rfkill_state
{
1090 TPACPI_RFK_RADIO_OFF
= 0,
1094 /* rfkill switches */
1095 enum tpacpi_rfk_id
{
1096 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
1097 TPACPI_RFK_WWAN_SW_ID
,
1098 TPACPI_RFK_UWB_SW_ID
,
1102 static const char *tpacpi_rfkill_names
[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID
] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID
] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID
] = "uwb",
1106 [TPACPI_RFK_SW_MAX
] = NULL
1109 /* ThinkPad-ACPI rfkill subdriver */
1111 struct rfkill
*rfkill
;
1112 enum tpacpi_rfk_id id
;
1113 const struct tpacpi_rfk_ops
*ops
;
1116 struct tpacpi_rfk_ops
{
1117 /* firmware interface */
1118 int (*get_status
)(void);
1119 int (*set_status
)(const enum tpacpi_rfkill_state
);
1122 static struct tpacpi_rfk
*tpacpi_rfkill_switches
[TPACPI_RFK_SW_MAX
];
1124 /* Query FW and update rfkill sw state for a given rfkill switch */
1125 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk
*tp_rfk
)
1132 status
= (tp_rfk
->ops
->get_status
)();
1136 rfkill_set_sw_state(tp_rfk
->rfkill
,
1137 (status
== TPACPI_RFK_RADIO_OFF
));
1142 /* Query FW and update rfkill sw state for all rfkill switches */
1143 static void tpacpi_rfk_update_swstate_all(void)
1147 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[i
]);
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1155 static void tpacpi_rfk_update_hwblock_state(bool blocked
)
1158 struct tpacpi_rfk
*tp_rfk
;
1160 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++) {
1161 tp_rfk
= tpacpi_rfkill_switches
[i
];
1163 if (rfkill_set_hw_state(tp_rfk
->rfkill
,
1165 /* ignore -- we track sw block */
1171 /* Call to get the WLSW state from the firmware */
1172 static int hotkey_get_wlsw(void);
1174 /* Call to query WLSW state and update all rfkill switches */
1175 static bool tpacpi_rfk_check_hwblock_state(void)
1177 int res
= hotkey_get_wlsw();
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1184 hw_blocked
= (res
== TPACPI_RFK_RADIO_OFF
);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked
);
1190 static int tpacpi_rfk_hook_set_block(void *data
, bool blocked
)
1192 struct tpacpi_rfk
*tp_rfk
= data
;
1195 dbg_printk(TPACPI_DBG_RFKILL
,
1196 "request to change radio state to %s\n",
1197 blocked
? "blocked" : "unblocked");
1199 /* try to set radio state */
1200 res
= (tp_rfk
->ops
->set_status
)(blocked
?
1201 TPACPI_RFK_RADIO_OFF
: TPACPI_RFK_RADIO_ON
);
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk
);
1206 return (res
< 0) ? res
: 0;
1209 static const struct rfkill_ops tpacpi_rfk_rfkill_ops
= {
1210 .set_block
= tpacpi_rfk_hook_set_block
,
1213 static int __init
tpacpi_new_rfkill(const enum tpacpi_rfk_id id
,
1214 const struct tpacpi_rfk_ops
*tp_rfkops
,
1215 const enum rfkill_type rfktype
,
1217 const bool set_default
)
1219 struct tpacpi_rfk
*atp_rfk
;
1221 bool sw_state
= false;
1224 BUG_ON(id
>= TPACPI_RFK_SW_MAX
|| tpacpi_rfkill_switches
[id
]);
1226 atp_rfk
= kzalloc(sizeof(struct tpacpi_rfk
), GFP_KERNEL
);
1228 atp_rfk
->rfkill
= rfkill_alloc(name
,
1231 &tpacpi_rfk_rfkill_ops
,
1233 if (!atp_rfk
|| !atp_rfk
->rfkill
) {
1235 "failed to allocate memory for rfkill class\n");
1241 atp_rfk
->ops
= tp_rfkops
;
1243 sw_status
= (tp_rfkops
->get_status
)();
1244 if (sw_status
< 0) {
1246 "failed to read initial state for %s, error %d\n",
1249 sw_state
= (sw_status
== TPACPI_RFK_RADIO_OFF
);
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
1253 rfkill_init_sw_state(atp_rfk
->rfkill
, sw_state
);
1256 rfkill_set_hw_state(atp_rfk
->rfkill
, tpacpi_rfk_check_hwblock_state());
1258 res
= rfkill_register(atp_rfk
->rfkill
);
1261 "failed to register %s rfkill switch: %d\n",
1263 rfkill_destroy(atp_rfk
->rfkill
);
1268 tpacpi_rfkill_switches
[id
] = atp_rfk
;
1272 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id
)
1274 struct tpacpi_rfk
*tp_rfk
;
1276 BUG_ON(id
>= TPACPI_RFK_SW_MAX
);
1278 tp_rfk
= tpacpi_rfkill_switches
[id
];
1280 rfkill_unregister(tp_rfk
->rfkill
);
1281 tpacpi_rfkill_switches
[id
] = NULL
;
1286 static void printk_deprecated_rfkill_attribute(const char * const what
)
1288 printk_deprecated_attribute(what
,
1289 "Please switch to generic rfkill before year 2010");
1292 /* sysfs <radio> enable ------------------------------------------------ */
1293 static ssize_t
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id
,
1294 struct device_attribute
*attr
,
1299 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1301 /* This is in the ABI... */
1302 if (tpacpi_rfk_check_hwblock_state()) {
1303 status
= TPACPI_RFK_RADIO_OFF
;
1305 status
= tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1310 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1311 (status
== TPACPI_RFK_RADIO_ON
) ? 1 : 0);
1314 static ssize_t
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id
,
1315 struct device_attribute
*attr
,
1316 const char *buf
, size_t count
)
1321 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1323 if (parse_strtoul(buf
, 1, &t
))
1326 tpacpi_disclose_usertask(attr
->attr
.name
, "set to %ld\n", t
);
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state() && !!t
)
1332 res
= tpacpi_rfkill_switches
[id
]->ops
->set_status((!!t
) ?
1333 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
);
1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1336 return (res
< 0) ? res
: count
;
1339 /* procfs -------------------------------------------------------------- */
1340 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id
, char *p
)
1344 if (id
>= TPACPI_RFK_SW_MAX
)
1345 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state()) {
1351 status
= TPACPI_RFK_RADIO_OFF
;
1353 status
= tpacpi_rfk_update_swstate(
1354 tpacpi_rfkill_switches
[id
]);
1359 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
1360 (status
== TPACPI_RFK_RADIO_ON
) ?
1361 "enabled" : "disabled");
1362 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
1368 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id
, char *buf
)
1374 if (id
>= TPACPI_RFK_SW_MAX
)
1377 while ((cmd
= next_cmd(&buf
))) {
1378 if (strlencmp(cmd
, "enable") == 0)
1379 status
= TPACPI_RFK_RADIO_ON
;
1380 else if (strlencmp(cmd
, "disable") == 0)
1381 status
= TPACPI_RFK_RADIO_OFF
;
1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1388 (status
== TPACPI_RFK_RADIO_ON
) ?
1389 "enable" : "disable",
1390 tpacpi_rfkill_names
[id
]);
1391 res
= (tpacpi_rfkill_switches
[id
]->ops
->set_status
)(status
);
1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1398 /*************************************************************************
1399 * thinkpad-acpi driver attributes
1402 /* interface_version --------------------------------------------------- */
1403 static ssize_t
tpacpi_driver_interface_version_show(
1404 struct device_driver
*drv
,
1407 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1410 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1411 tpacpi_driver_interface_version_show
, NULL
);
1413 /* debug_level --------------------------------------------------------- */
1414 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1417 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1420 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1421 const char *buf
, size_t count
)
1425 if (parse_strtoul(buf
, 0xffff, &t
))
1433 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1434 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1436 /* version ------------------------------------------------------------- */
1437 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1440 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1441 TPACPI_DESC
, TPACPI_VERSION
);
1444 static DRIVER_ATTR(version
, S_IRUGO
,
1445 tpacpi_driver_version_show
, NULL
);
1447 /* --------------------------------------------------------------------- */
1449 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1451 /* wlsw_emulstate ------------------------------------------------------ */
1452 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1455 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1458 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1459 const char *buf
, size_t count
)
1463 if (parse_strtoul(buf
, 1, &t
))
1466 if (tpacpi_wlsw_emulstate
!= !!t
) {
1467 tpacpi_wlsw_emulstate
= !!t
;
1468 tpacpi_rfk_update_hwblock_state(!t
); /* negative logic */
1474 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1475 tpacpi_driver_wlsw_emulstate_show
,
1476 tpacpi_driver_wlsw_emulstate_store
);
1478 /* bluetooth_emulstate ------------------------------------------------- */
1479 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1480 struct device_driver
*drv
,
1483 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1486 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1487 struct device_driver
*drv
,
1488 const char *buf
, size_t count
)
1492 if (parse_strtoul(buf
, 1, &t
))
1495 tpacpi_bluetooth_emulstate
= !!t
;
1500 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1501 tpacpi_driver_bluetooth_emulstate_show
,
1502 tpacpi_driver_bluetooth_emulstate_store
);
1504 /* wwan_emulstate ------------------------------------------------- */
1505 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1506 struct device_driver
*drv
,
1509 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1512 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1513 struct device_driver
*drv
,
1514 const char *buf
, size_t count
)
1518 if (parse_strtoul(buf
, 1, &t
))
1521 tpacpi_wwan_emulstate
= !!t
;
1526 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1527 tpacpi_driver_wwan_emulstate_show
,
1528 tpacpi_driver_wwan_emulstate_store
);
1530 /* uwb_emulstate ------------------------------------------------- */
1531 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1532 struct device_driver
*drv
,
1535 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1538 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1539 struct device_driver
*drv
,
1540 const char *buf
, size_t count
)
1544 if (parse_strtoul(buf
, 1, &t
))
1547 tpacpi_uwb_emulstate
= !!t
;
1552 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1553 tpacpi_driver_uwb_emulstate_show
,
1554 tpacpi_driver_uwb_emulstate_store
);
1557 /* --------------------------------------------------------------------- */
1559 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1560 &driver_attr_debug_level
, &driver_attr_version
,
1561 &driver_attr_interface_version
,
1564 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1570 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1571 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1575 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1576 if (!res
&& dbg_wlswemul
)
1577 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1578 if (!res
&& dbg_bluetoothemul
)
1579 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1580 if (!res
&& dbg_wwanemul
)
1581 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1582 if (!res
&& dbg_uwbemul
)
1583 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1589 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1593 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1594 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1596 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1597 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1598 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1599 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1600 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1604 /*************************************************************************
1609 * Table of recommended minimum BIOS versions
1611 * Reasons for listing:
1612 * 1. Stable BIOS, listed because the unknown ammount of
1613 * bugs and bad ACPI behaviour on older versions
1615 * 2. BIOS or EC fw with known bugs that trigger on Linux
1617 * 3. BIOS with known reduced functionality in older versions
1619 * We recommend the latest BIOS and EC version.
1620 * We only support the latest BIOS and EC fw version as a rule.
1622 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1623 * Information from users in ThinkWiki
1626 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1627 { .vendor = (__v), \
1628 .bios = TPID(__id1, __id2), \
1629 .ec = TPACPI_MATCH_ANY, \
1630 .quirks = TPACPI_MATCH_ANY << 16 \
1631 | (__bv1) << 8 | (__bv2) }
1633 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1634 __eid1, __eid2, __ev1, __ev2) \
1635 { .vendor = (__v), \
1636 .bios = TPID(__bid1, __bid2), \
1637 .ec = TPID(__eid1, __eid2), \
1638 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1639 | (__bv1) << 8 | (__bv2) }
1641 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1642 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1644 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1645 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1646 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1648 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1649 __eid1, __eid2, __ev1, __ev2) \
1650 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1651 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1653 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1654 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1656 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1657 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1658 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1660 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1661 __eid1, __eid2, __ev1, __ev2) \
1662 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1663 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1665 static const struct tpacpi_quirk tpacpi_bios_version_qtable
[] __initconst
= {
1666 /* Numeric models ------------------ */
1667 /* FW MODEL BIOS VERS */
1668 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1669 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1670 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1671 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1672 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1673 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1674 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1675 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1676 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1678 /* A-series ------------------------- */
1679 /* FW MODEL BIOS VERS EC VERS */
1680 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1681 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1682 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1683 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1684 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1685 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1686 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1687 TPV_QI0('1', '3', '2', '0'), /* A22m */
1688 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1689 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1690 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1692 /* G-series ------------------------- */
1693 /* FW MODEL BIOS VERS */
1694 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1695 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1697 /* R-series, T-series --------------- */
1698 /* FW MODEL BIOS VERS EC VERS */
1699 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1700 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1701 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1702 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1703 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1704 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1705 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1706 T40/p, T41/p, T42/p (1) */
1707 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1708 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1709 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1710 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1712 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1713 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1714 TPV_QI0('1', '6', '3', '2'), /* T22 */
1715 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1716 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1717 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1719 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1720 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1721 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1723 /* BIOS FW BIOS VERS EC FW EC VERS */
1724 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1725 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1727 /* X-series ------------------------- */
1728 /* FW MODEL BIOS VERS EC VERS */
1729 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1730 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1731 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1732 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1733 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1734 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1735 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1737 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1738 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1740 /* (0) - older versions lack DMI EC fw string and functionality */
1741 /* (1) - older versions known to lack functionality */
1752 static void __init
tpacpi_check_outdated_fw(void)
1754 unsigned long fwvers
;
1755 u16 ec_version
, bios_version
;
1757 fwvers
= tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1758 ARRAY_SIZE(tpacpi_bios_version_qtable
));
1763 bios_version
= fwvers
& 0xffffU
;
1764 ec_version
= (fwvers
>> 16) & 0xffffU
;
1766 /* note that unknown versions are set to 0x0000 and we use that */
1767 if ((bios_version
> thinkpad_id
.bios_release
) ||
1768 (ec_version
> thinkpad_id
.ec_release
&&
1769 ec_version
!= TPACPI_MATCH_ANY
)) {
1771 * The changelogs would let us track down the exact
1772 * reason, but it is just too much of a pain to track
1773 * it. We only list BIOSes that are either really
1774 * broken, or really stable to begin with, so it is
1775 * best if the user upgrades the firmware anyway.
1778 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1780 "WARNING: This firmware may be missing critical bug "
1781 "fixes and/or important features\n");
1785 /****************************************************************************
1786 ****************************************************************************
1790 ****************************************************************************
1791 ****************************************************************************/
1793 /*************************************************************************
1794 * thinkpad-acpi init subdriver
1797 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1799 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1800 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1802 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1803 (thinkpad_id
.bios_version_str
) ?
1804 thinkpad_id
.bios_version_str
: "unknown",
1805 (thinkpad_id
.ec_version_str
) ?
1806 thinkpad_id
.ec_version_str
: "unknown");
1808 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1809 printk(TPACPI_INFO
"%s %s, model %s\n",
1810 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1811 "IBM" : ((thinkpad_id
.vendor
==
1812 PCI_VENDOR_ID_LENOVO
) ?
1813 "Lenovo" : "Unknown vendor"),
1814 thinkpad_id
.model_str
,
1815 (thinkpad_id
.nummodel_str
) ?
1816 thinkpad_id
.nummodel_str
: "unknown");
1818 tpacpi_check_outdated_fw();
1822 static int thinkpad_acpi_driver_read(char *p
)
1826 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1827 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1832 static struct ibm_struct thinkpad_acpi_driver_data
= {
1834 .read
= thinkpad_acpi_driver_read
,
1837 /*************************************************************************
1841 enum { /* hot key scan codes (derived from ACPI DSDT) */
1842 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1843 TP_ACPI_HOTKEYSCAN_FNF2
,
1844 TP_ACPI_HOTKEYSCAN_FNF3
,
1845 TP_ACPI_HOTKEYSCAN_FNF4
,
1846 TP_ACPI_HOTKEYSCAN_FNF5
,
1847 TP_ACPI_HOTKEYSCAN_FNF6
,
1848 TP_ACPI_HOTKEYSCAN_FNF7
,
1849 TP_ACPI_HOTKEYSCAN_FNF8
,
1850 TP_ACPI_HOTKEYSCAN_FNF9
,
1851 TP_ACPI_HOTKEYSCAN_FNF10
,
1852 TP_ACPI_HOTKEYSCAN_FNF11
,
1853 TP_ACPI_HOTKEYSCAN_FNF12
,
1854 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1855 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1856 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1857 TP_ACPI_HOTKEYSCAN_FNHOME
,
1858 TP_ACPI_HOTKEYSCAN_FNEND
,
1859 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1860 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1861 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1862 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1863 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1864 TP_ACPI_HOTKEYSCAN_MUTE
,
1865 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1868 enum { /* Keys available through NVRAM polling */
1869 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1870 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1873 enum { /* Positions of some of the keys in hotkey masks */
1874 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1875 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1876 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1877 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1878 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1879 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1880 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1881 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1882 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1883 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1884 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1887 enum { /* NVRAM to ACPI HKEY group map */
1888 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1889 TP_ACPI_HKEY_ZOOM_MASK
|
1890 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1891 TP_ACPI_HKEY_HIBERNATE_MASK
,
1892 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1893 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1894 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1895 TP_ACPI_HKEY_VOLDWN_MASK
|
1896 TP_ACPI_HKEY_MUTE_MASK
,
1899 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1900 struct tp_nvram_state
{
1901 u16 thinkpad_toggle
:1;
1903 u16 display_toggle
:1;
1904 u16 thinklight_toggle
:1;
1905 u16 hibernate_toggle
:1;
1906 u16 displayexp_toggle
:1;
1907 u16 display_state
:1;
1908 u16 brightness_toggle
:1;
1909 u16 volume_toggle
:1;
1912 u8 brightness_level
;
1916 static struct task_struct
*tpacpi_hotkey_task
;
1917 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1918 static int hotkey_poll_freq
= 10; /* Hz */
1919 static struct mutex hotkey_thread_mutex
;
1920 static struct mutex hotkey_thread_data_mutex
;
1921 static unsigned int hotkey_config_change
;
1923 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1925 #define hotkey_source_mask 0U
1927 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1929 static struct mutex hotkey_mutex
;
1931 static enum { /* Reasons for waking up */
1932 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1933 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1934 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1935 } hotkey_wakeup_reason
;
1937 static int hotkey_autosleep_ack
;
1939 static u32 hotkey_orig_mask
;
1940 static u32 hotkey_all_mask
;
1941 static u32 hotkey_reserved_mask
;
1942 static u32 hotkey_mask
;
1944 static unsigned int hotkey_report_mode
;
1946 static u16
*hotkey_keycode_map
;
1948 static struct attribute_set
*hotkey_dev_attributes
;
1950 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1951 #define HOTKEY_CONFIG_CRITICAL_START \
1953 mutex_lock(&hotkey_thread_data_mutex); \
1954 hotkey_config_change++; \
1956 #define HOTKEY_CONFIG_CRITICAL_END \
1957 mutex_unlock(&hotkey_thread_data_mutex);
1959 #define HOTKEY_CONFIG_CRITICAL_START
1960 #define HOTKEY_CONFIG_CRITICAL_END
1961 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1963 /* HKEY.MHKG() return bits */
1964 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1966 static int hotkey_get_wlsw(void)
1970 if (!tp_features
.hotkey_wlsw
)
1973 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1975 return (tpacpi_wlsw_emulstate
) ?
1976 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
1979 if (!acpi_evalf(hkey_handle
, &status
, "WLSW", "d"))
1982 return (status
) ? TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
1985 static int hotkey_get_tablet_mode(int *status
)
1989 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1992 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1997 * Call with hotkey_mutex held
1999 static int hotkey_mask_get(void)
2003 if (tp_features
.hotkey_mask
) {
2004 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
2007 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
2013 * Call with hotkey_mutex held
2015 static int hotkey_mask_set(u32 mask
)
2020 if (tp_features
.hotkey_mask
) {
2021 if (!tp_warned
.hotkey_mask_ff
&&
2022 (mask
== 0xffff || mask
== 0xffffff ||
2023 mask
== 0xffffffff)) {
2024 tp_warned
.hotkey_mask_ff
= 1;
2025 printk(TPACPI_NOTICE
2026 "setting the hotkey mask to 0x%08x is likely "
2027 "not the best way to go about it\n", mask
);
2028 printk(TPACPI_NOTICE
2029 "please consider using the driver defaults, "
2030 "and refer to up-to-date thinkpad-acpi "
2034 HOTKEY_CONFIG_CRITICAL_START
2035 for (i
= 0; i
< 32; i
++) {
2037 /* enable in firmware mask only keys not in NVRAM
2038 * mode, but enable the key in the cached hotkey_mask
2039 * regardless of mode, or the key will end up
2040 * disabled by hotkey_mask_get() */
2041 if (!acpi_evalf(hkey_handle
,
2042 NULL
, "MHKM", "vdd", i
+ 1,
2043 !!((mask
& ~hotkey_source_mask
) & m
))) {
2047 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
2050 HOTKEY_CONFIG_CRITICAL_END
2052 /* hotkey_mask_get must be called unconditionally below */
2053 if (!hotkey_mask_get() && !rc
&&
2054 (hotkey_mask
& ~hotkey_source_mask
) !=
2055 (mask
& ~hotkey_source_mask
)) {
2056 printk(TPACPI_NOTICE
2057 "requested hot key mask 0x%08x, but "
2058 "firmware forced it to 0x%08x\n",
2062 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2063 HOTKEY_CONFIG_CRITICAL_START
2064 hotkey_mask
= mask
& hotkey_source_mask
;
2065 HOTKEY_CONFIG_CRITICAL_END
2067 if (hotkey_mask
!= mask
) {
2068 printk(TPACPI_NOTICE
2069 "requested hot key mask 0x%08x, "
2070 "forced to 0x%08x (NVRAM poll mask is "
2071 "0x%08x): no firmware mask support\n",
2072 mask
, hotkey_mask
, hotkey_source_mask
);
2077 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2083 static int hotkey_status_get(int *status
)
2085 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2091 static int hotkey_status_set(bool enable
)
2093 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2099 static void tpacpi_input_send_tabletsw(void)
2103 if (tp_features
.hotkey_tablet
&&
2104 !hotkey_get_tablet_mode(&state
)) {
2105 mutex_lock(&tpacpi_inputdev_send_mutex
);
2107 input_report_switch(tpacpi_inputdev
,
2108 SW_TABLET_MODE
, !!state
);
2109 input_sync(tpacpi_inputdev
);
2111 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2115 static void tpacpi_input_send_key(unsigned int scancode
)
2117 unsigned int keycode
;
2119 keycode
= hotkey_keycode_map
[scancode
];
2121 if (keycode
!= KEY_RESERVED
) {
2122 mutex_lock(&tpacpi_inputdev_send_mutex
);
2124 input_report_key(tpacpi_inputdev
, keycode
, 1);
2125 if (keycode
== KEY_UNKNOWN
)
2126 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
2128 input_sync(tpacpi_inputdev
);
2130 input_report_key(tpacpi_inputdev
, keycode
, 0);
2131 if (keycode
== KEY_UNKNOWN
)
2132 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
2134 input_sync(tpacpi_inputdev
);
2136 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2140 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2141 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
2143 static void tpacpi_hotkey_send_key(unsigned int scancode
)
2145 tpacpi_input_send_key(scancode
);
2146 if (hotkey_report_mode
< 2) {
2147 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
2148 0x80, 0x1001 + scancode
);
2152 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
2156 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
2157 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
2158 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
2159 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
2160 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
2161 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
2163 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
2164 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
2165 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
2167 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
2168 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
2169 n
->displayexp_toggle
=
2170 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
2172 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
2173 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
2174 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
2175 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
2176 n
->brightness_toggle
=
2177 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
2179 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
2180 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
2181 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
2182 >> TP_NVRAM_POS_LEVEL_VOLUME
;
2183 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
2184 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
2188 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2190 if ((mask & (1 << __scancode)) && \
2191 oldn->__member != newn->__member) \
2192 tpacpi_hotkey_send_key(__scancode); \
2195 #define TPACPI_MAY_SEND_KEY(__scancode) \
2196 do { if (mask & (1 << __scancode)) \
2197 tpacpi_hotkey_send_key(__scancode); } while (0)
2199 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
2200 struct tp_nvram_state
*newn
,
2203 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
2204 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
2205 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
2206 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
2208 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
2210 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
2213 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
2214 if (oldn
->mute
!= newn
->mute
) {
2215 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2217 if (oldn
->volume_level
> newn
->volume_level
) {
2218 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2219 } else if (oldn
->volume_level
< newn
->volume_level
) {
2220 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2221 } else if (oldn
->mute
== newn
->mute
) {
2222 /* repeated key presses that didn't change state */
2224 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2225 } else if (newn
->volume_level
!= 0) {
2226 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2228 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2233 /* handle brightness */
2234 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
2235 if (oldn
->brightness_level
< newn
->brightness_level
) {
2236 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2237 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
2238 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2240 /* repeated key presses that didn't change state */
2241 if (newn
->brightness_level
!= 0) {
2242 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2250 #undef TPACPI_COMPARE_KEY
2251 #undef TPACPI_MAY_SEND_KEY
2253 static int hotkey_kthread(void *data
)
2255 struct tp_nvram_state s
[2];
2257 unsigned int si
, so
;
2259 unsigned int change_detector
, must_reset
;
2261 mutex_lock(&hotkey_thread_mutex
);
2263 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
2272 /* Initial state for compares */
2273 mutex_lock(&hotkey_thread_data_mutex
);
2274 change_detector
= hotkey_config_change
;
2275 mask
= hotkey_source_mask
& hotkey_mask
;
2276 mutex_unlock(&hotkey_thread_data_mutex
);
2277 hotkey_read_nvram(&s
[so
], mask
);
2279 while (!kthread_should_stop() && hotkey_poll_freq
) {
2281 t
= 1000/hotkey_poll_freq
;
2282 t
= msleep_interruptible(t
);
2283 if (unlikely(kthread_should_stop()))
2285 must_reset
= try_to_freeze();
2286 if (t
> 0 && !must_reset
)
2289 mutex_lock(&hotkey_thread_data_mutex
);
2290 if (must_reset
|| hotkey_config_change
!= change_detector
) {
2291 /* forget old state on thaw or config change */
2294 change_detector
= hotkey_config_change
;
2296 mask
= hotkey_source_mask
& hotkey_mask
;
2297 mutex_unlock(&hotkey_thread_data_mutex
);
2300 hotkey_read_nvram(&s
[si
], mask
);
2301 if (likely(si
!= so
)) {
2302 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
2312 mutex_unlock(&hotkey_thread_mutex
);
2316 static void hotkey_poll_stop_sync(void)
2318 if (tpacpi_hotkey_task
) {
2319 if (frozen(tpacpi_hotkey_task
) ||
2320 freezing(tpacpi_hotkey_task
))
2321 thaw_process(tpacpi_hotkey_task
);
2323 kthread_stop(tpacpi_hotkey_task
);
2324 tpacpi_hotkey_task
= NULL
;
2325 mutex_lock(&hotkey_thread_mutex
);
2326 /* at this point, the thread did exit */
2327 mutex_unlock(&hotkey_thread_mutex
);
2331 /* call with hotkey_mutex held */
2332 static void hotkey_poll_setup(int may_warn
)
2334 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
2335 hotkey_poll_freq
> 0 &&
2336 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
2337 if (!tpacpi_hotkey_task
) {
2338 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
2339 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
2340 if (IS_ERR(tpacpi_hotkey_task
)) {
2341 tpacpi_hotkey_task
= NULL
;
2343 "could not create kernel thread "
2344 "for hotkey polling\n");
2348 hotkey_poll_stop_sync();
2350 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
2351 printk(TPACPI_NOTICE
2352 "hot keys 0x%08x require polling, "
2353 "which is currently disabled\n",
2354 hotkey_source_mask
);
2359 static void hotkey_poll_setup_safe(int may_warn
)
2361 mutex_lock(&hotkey_mutex
);
2362 hotkey_poll_setup(may_warn
);
2363 mutex_unlock(&hotkey_mutex
);
2366 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2368 static void hotkey_poll_setup_safe(int __unused
)
2372 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2374 static int hotkey_inputdev_open(struct input_dev
*dev
)
2376 switch (tpacpi_lifecycle
) {
2377 case TPACPI_LIFE_INIT
:
2379 * hotkey_init will call hotkey_poll_setup_safe
2380 * at the appropriate moment
2383 case TPACPI_LIFE_EXITING
:
2385 case TPACPI_LIFE_RUNNING
:
2386 hotkey_poll_setup_safe(0);
2390 /* Should only happen if tpacpi_lifecycle is corrupt */
2395 static void hotkey_inputdev_close(struct input_dev
*dev
)
2397 /* disable hotkey polling when possible */
2398 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
2399 hotkey_poll_setup_safe(0);
2402 /* sysfs hotkey enable ------------------------------------------------- */
2403 static ssize_t
hotkey_enable_show(struct device
*dev
,
2404 struct device_attribute
*attr
,
2409 printk_deprecated_attribute("hotkey_enable",
2410 "Hotkey reporting is always enabled");
2412 res
= hotkey_status_get(&status
);
2416 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
2419 static ssize_t
hotkey_enable_store(struct device
*dev
,
2420 struct device_attribute
*attr
,
2421 const char *buf
, size_t count
)
2425 printk_deprecated_attribute("hotkey_enable",
2426 "Hotkeys can be disabled through hotkey_mask");
2428 if (parse_strtoul(buf
, 1, &t
))
2437 static struct device_attribute dev_attr_hotkey_enable
=
2438 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
2439 hotkey_enable_show
, hotkey_enable_store
);
2441 /* sysfs hotkey mask --------------------------------------------------- */
2442 static ssize_t
hotkey_mask_show(struct device
*dev
,
2443 struct device_attribute
*attr
,
2448 if (mutex_lock_killable(&hotkey_mutex
))
2449 return -ERESTARTSYS
;
2450 res
= hotkey_mask_get();
2451 mutex_unlock(&hotkey_mutex
);
2454 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
2457 static ssize_t
hotkey_mask_store(struct device
*dev
,
2458 struct device_attribute
*attr
,
2459 const char *buf
, size_t count
)
2464 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2467 if (mutex_lock_killable(&hotkey_mutex
))
2468 return -ERESTARTSYS
;
2470 res
= hotkey_mask_set(t
);
2472 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2473 hotkey_poll_setup(1);
2476 mutex_unlock(&hotkey_mutex
);
2478 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2480 return (res
) ? res
: count
;
2483 static struct device_attribute dev_attr_hotkey_mask
=
2484 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
2485 hotkey_mask_show
, hotkey_mask_store
);
2487 /* sysfs hotkey bios_enabled ------------------------------------------- */
2488 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2489 struct device_attribute
*attr
,
2492 return sprintf(buf
, "0\n");
2495 static struct device_attribute dev_attr_hotkey_bios_enabled
=
2496 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
2498 /* sysfs hotkey bios_mask ---------------------------------------------- */
2499 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2500 struct device_attribute
*attr
,
2503 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2506 static struct device_attribute dev_attr_hotkey_bios_mask
=
2507 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
2509 /* sysfs hotkey all_mask ----------------------------------------------- */
2510 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2511 struct device_attribute
*attr
,
2514 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2515 hotkey_all_mask
| hotkey_source_mask
);
2518 static struct device_attribute dev_attr_hotkey_all_mask
=
2519 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2521 /* sysfs hotkey recommended_mask --------------------------------------- */
2522 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2523 struct device_attribute
*attr
,
2526 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2527 (hotkey_all_mask
| hotkey_source_mask
)
2528 & ~hotkey_reserved_mask
);
2531 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2532 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2533 hotkey_recommended_mask_show
, NULL
);
2535 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2537 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2538 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2539 struct device_attribute
*attr
,
2542 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2545 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2546 struct device_attribute
*attr
,
2547 const char *buf
, size_t count
)
2551 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2552 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2555 if (mutex_lock_killable(&hotkey_mutex
))
2556 return -ERESTARTSYS
;
2558 HOTKEY_CONFIG_CRITICAL_START
2559 hotkey_source_mask
= t
;
2560 HOTKEY_CONFIG_CRITICAL_END
2562 hotkey_poll_setup(1);
2564 mutex_unlock(&hotkey_mutex
);
2566 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2571 static struct device_attribute dev_attr_hotkey_source_mask
=
2572 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2573 hotkey_source_mask_show
, hotkey_source_mask_store
);
2575 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2576 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2577 struct device_attribute
*attr
,
2580 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2583 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2584 struct device_attribute
*attr
,
2585 const char *buf
, size_t count
)
2589 if (parse_strtoul(buf
, 25, &t
))
2592 if (mutex_lock_killable(&hotkey_mutex
))
2593 return -ERESTARTSYS
;
2595 hotkey_poll_freq
= t
;
2597 hotkey_poll_setup(1);
2598 mutex_unlock(&hotkey_mutex
);
2600 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2605 static struct device_attribute dev_attr_hotkey_poll_freq
=
2606 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2607 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2609 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2611 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2612 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2613 struct device_attribute
*attr
,
2617 res
= hotkey_get_wlsw();
2621 /* Opportunistic update */
2622 tpacpi_rfk_update_hwblock_state((res
== TPACPI_RFK_RADIO_OFF
));
2624 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2625 (res
== TPACPI_RFK_RADIO_OFF
) ? 0 : 1);
2628 static struct device_attribute dev_attr_hotkey_radio_sw
=
2629 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2631 static void hotkey_radio_sw_notify_change(void)
2633 if (tp_features
.hotkey_wlsw
)
2634 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2638 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2639 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2640 struct device_attribute
*attr
,
2644 res
= hotkey_get_tablet_mode(&s
);
2648 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2651 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2652 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2654 static void hotkey_tablet_mode_notify_change(void)
2656 if (tp_features
.hotkey_tablet
)
2657 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2658 "hotkey_tablet_mode");
2661 /* sysfs hotkey report_mode -------------------------------------------- */
2662 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2663 struct device_attribute
*attr
,
2666 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2667 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2670 static struct device_attribute dev_attr_hotkey_report_mode
=
2671 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2673 /* sysfs wakeup reason (pollable) -------------------------------------- */
2674 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2675 struct device_attribute
*attr
,
2678 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2681 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2682 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2684 static void hotkey_wakeup_reason_notify_change(void)
2686 if (tp_features
.hotkey_mask
)
2687 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2691 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2692 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2693 struct device_attribute
*attr
,
2696 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2699 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2700 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2701 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2703 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2705 if (tp_features
.hotkey_mask
)
2706 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2707 "wakeup_hotunplug_complete");
2710 /* --------------------------------------------------------------------- */
2712 static struct attribute
*hotkey_attributes
[] __initdata
= {
2713 &dev_attr_hotkey_enable
.attr
,
2714 &dev_attr_hotkey_bios_enabled
.attr
,
2715 &dev_attr_hotkey_report_mode
.attr
,
2716 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2717 &dev_attr_hotkey_mask
.attr
,
2718 &dev_attr_hotkey_all_mask
.attr
,
2719 &dev_attr_hotkey_recommended_mask
.attr
,
2720 &dev_attr_hotkey_source_mask
.attr
,
2721 &dev_attr_hotkey_poll_freq
.attr
,
2725 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
2726 &dev_attr_hotkey_bios_mask
.attr
,
2727 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2728 &dev_attr_hotkey_mask
.attr
,
2729 &dev_attr_hotkey_all_mask
.attr
,
2730 &dev_attr_hotkey_recommended_mask
.attr
,
2732 &dev_attr_hotkey_wakeup_reason
.attr
,
2733 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2737 * Sync both the hw and sw blocking state of all switches
2739 static void tpacpi_send_radiosw_update(void)
2744 * We must sync all rfkill controllers *before* issuing any
2745 * rfkill input events, or we will race the rfkill core input
2748 * tpacpi_inputdev_send_mutex works as a syncronization point
2751 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2754 wlsw
= hotkey_get_wlsw();
2756 /* Sync hw blocking state first if it is hw-blocked */
2757 if (wlsw
== TPACPI_RFK_RADIO_OFF
)
2758 tpacpi_rfk_update_hwblock_state(true);
2760 /* Sync sw blocking state */
2761 tpacpi_rfk_update_swstate_all();
2763 /* Sync hw blocking state last if it is hw-unblocked */
2764 if (wlsw
== TPACPI_RFK_RADIO_ON
)
2765 tpacpi_rfk_update_hwblock_state(false);
2767 /* Issue rfkill input event for WLSW switch */
2769 mutex_lock(&tpacpi_inputdev_send_mutex
);
2771 input_report_switch(tpacpi_inputdev
,
2772 SW_RFKILL_ALL
, (wlsw
> 0));
2773 input_sync(tpacpi_inputdev
);
2775 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2779 * this can be unconditional, as we will poll state again
2780 * if userspace uses the notify to read data
2782 hotkey_radio_sw_notify_change();
2785 static void hotkey_exit(void)
2787 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2788 hotkey_poll_stop_sync();
2791 if (hotkey_dev_attributes
)
2792 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2794 kfree(hotkey_keycode_map
);
2796 if (tp_features
.hotkey
) {
2797 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
2798 "restoring original hot key mask\n");
2799 /* no short-circuit boolean operator below! */
2800 if ((hotkey_mask_set(hotkey_orig_mask
) |
2801 hotkey_status_set(false)) != 0)
2803 "failed to restore hot key mask "
2804 "to BIOS defaults\n");
2808 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2810 /* Requirements for changing the default keymaps:
2812 * 1. Many of the keys are mapped to KEY_RESERVED for very
2813 * good reasons. Do not change them unless you have deep
2814 * knowledge on the IBM and Lenovo ThinkPad firmware for
2815 * the various ThinkPad models. The driver behaves
2816 * differently for KEY_RESERVED: such keys have their
2817 * hot key mask *unset* in mask_recommended, and also
2818 * in the initial hot key mask programmed into the
2819 * firmware at driver load time, which means the firm-
2820 * ware may react very differently if you change them to
2823 * 2. You must be subscribed to the linux-thinkpad and
2824 * ibm-acpi-devel mailing lists, and you should read the
2825 * list archives since 2007 if you want to change the
2826 * keymaps. This requirement exists so that you will
2827 * know the past history of problems with the thinkpad-
2828 * acpi driver keymaps, and also that you will be
2829 * listening to any bug reports;
2831 * 3. Do not send thinkpad-acpi specific patches directly to
2832 * for merging, *ever*. Send them to the linux-acpi
2833 * mailinglist for comments. Merging is to be done only
2834 * through acpi-test and the ACPI maintainer.
2836 * If the above is too much to ask, don't change the keymap.
2837 * Ask the thinkpad-acpi maintainer to do it, instead.
2839 static u16 ibm_keycode_map
[] __initdata
= {
2840 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2841 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2842 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2843 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2845 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2846 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2847 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2848 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2850 /* brightness: firmware always reacts to them, unless
2851 * X.org did some tricks in the radeon BIOS scratch
2852 * registers of *some* models */
2853 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2854 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2856 /* Thinklight: firmware always react to it */
2857 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2859 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2860 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2862 /* Volume: firmware always react to it and reprograms
2863 * the built-in *extra* mixer. Never map it to control
2864 * another mixer by default. */
2865 KEY_RESERVED
, /* 0x14: VOLUME UP */
2866 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2867 KEY_RESERVED
, /* 0x16: MUTE */
2869 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2871 /* (assignments unknown, please report if found) */
2872 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2873 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2875 static u16 lenovo_keycode_map
[] __initdata
= {
2876 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2877 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2878 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2879 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2881 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2882 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2883 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2884 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2886 /* These either have to go through ACPI video, or
2887 * act like in the IBM ThinkPads, so don't ever
2888 * enable them by default */
2889 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2890 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2892 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2894 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2895 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2897 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2898 * react to it and reprograms the built-in *extra* mixer.
2899 * Never map it to control another mixer by default.
2901 * T60?, T61, R60?, R61: firmware and EC tries to send
2902 * these over the regular keyboard, so these are no-ops,
2903 * but there are still weird bugs re. MUTE, so do not
2904 * change unless you get test reports from all Lenovo
2905 * models. May cause the BIOS to interfere with the
2908 KEY_RESERVED
, /* 0x14: VOLUME UP */
2909 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2910 KEY_RESERVED
, /* 0x16: MUTE */
2912 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2914 /* (assignments unknown, please report if found) */
2915 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2916 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2919 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2920 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2921 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2927 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2928 "initializing hotkey subdriver\n");
2930 BUG_ON(!tpacpi_inputdev
);
2931 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2932 tpacpi_inputdev
->close
!= NULL
);
2934 TPACPI_ACPIHANDLE_INIT(hkey
);
2935 mutex_init(&hotkey_mutex
);
2937 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2938 mutex_init(&hotkey_thread_mutex
);
2939 mutex_init(&hotkey_thread_data_mutex
);
2942 /* hotkey not supported on 570 */
2943 tp_features
.hotkey
= hkey_handle
!= NULL
;
2945 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2947 str_supported(tp_features
.hotkey
));
2949 if (!tp_features
.hotkey
)
2952 tpacpi_disable_brightness_delay();
2954 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2955 if (!hotkey_dev_attributes
)
2957 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2959 ARRAY_SIZE(hotkey_attributes
));
2963 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2964 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2965 for HKEY interface version 0x100 */
2966 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2967 if ((hkeyv
>> 8) != 1) {
2968 printk(TPACPI_ERR
"unknown version of the "
2969 "HKEY interface: 0x%x\n", hkeyv
);
2970 printk(TPACPI_ERR
"please report this to %s\n",
2974 * MHKV 0x100 in A31, R40, R40e,
2975 * T4x, X31, and later
2977 tp_features
.hotkey_mask
= 1;
2978 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2979 "firmware HKEY interface version: 0x%x\n",
2984 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2985 "hotkey masks are %s\n",
2986 str_supported(tp_features
.hotkey_mask
));
2988 if (tp_features
.hotkey_mask
) {
2989 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2992 "missing MHKA handler, "
2993 "please report this to %s\n",
2995 /* FN+F12, FN+F4, FN+F3 */
2996 hotkey_all_mask
= 0x080cU
;
3000 /* hotkey_source_mask *must* be zero for
3001 * the first hotkey_mask_get */
3002 if (tp_features
.hotkey_mask
) {
3003 res
= hotkey_mask_get();
3007 hotkey_orig_mask
= hotkey_mask
;
3008 res
= add_many_to_attr_set(
3009 hotkey_dev_attributes
,
3010 hotkey_mask_attributes
,
3011 ARRAY_SIZE(hotkey_mask_attributes
));
3016 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3017 if (tp_features
.hotkey_mask
) {
3018 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
3021 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
3024 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3025 "hotkey source mask 0x%08x, polling freq %d\n",
3026 hotkey_source_mask
, hotkey_poll_freq
);
3029 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3031 tp_features
.hotkey_wlsw
= 1;
3033 "radio switch emulation enabled\n");
3036 /* Not all thinkpads have a hardware radio switch */
3037 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
3038 tp_features
.hotkey_wlsw
= 1;
3040 "radio switch found; radios are %s\n",
3041 enabled(status
, 0));
3043 if (tp_features
.hotkey_wlsw
)
3044 res
= add_to_attr_set(hotkey_dev_attributes
,
3045 &dev_attr_hotkey_radio_sw
.attr
);
3047 /* For X41t, X60t, X61t Tablets... */
3048 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
3049 tp_features
.hotkey_tablet
= 1;
3051 "possible tablet mode switch found; "
3052 "ThinkPad in %s mode\n",
3053 (status
& TP_HOTKEY_TABLET_MASK
)?
3054 "tablet" : "laptop");
3055 res
= add_to_attr_set(hotkey_dev_attributes
,
3056 &dev_attr_hotkey_tablet_mode
.attr
);
3060 res
= register_attr_set_with_sysfs(
3061 hotkey_dev_attributes
,
3062 &tpacpi_pdev
->dev
.kobj
);
3066 /* Set up key map */
3068 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
3070 if (!hotkey_keycode_map
) {
3072 "failed to allocate memory for key map\n");
3077 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
3078 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3079 "using Lenovo default hot key map\n");
3080 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
3081 TPACPI_HOTKEY_MAP_SIZE
);
3083 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3084 "using IBM default hot key map\n");
3085 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
3086 TPACPI_HOTKEY_MAP_SIZE
);
3089 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
3090 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
3091 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
3092 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
3093 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
3094 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
3095 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
3096 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
3097 set_bit(hotkey_keycode_map
[i
],
3098 tpacpi_inputdev
->keybit
);
3100 if (i
< sizeof(hotkey_reserved_mask
)*8)
3101 hotkey_reserved_mask
|= 1 << i
;
3105 if (tp_features
.hotkey_wlsw
) {
3106 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
3107 set_bit(SW_RFKILL_ALL
, tpacpi_inputdev
->swbit
);
3109 if (tp_features
.hotkey_tablet
) {
3110 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
3111 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
3114 /* Do not issue duplicate brightness change events to
3116 if (!tp_features
.bright_acpimode
)
3117 /* update bright_acpimode... */
3118 tpacpi_check_std_acpi_brightness_support();
3120 if (tp_features
.bright_acpimode
&& acpi_video_backlight_support()) {
3122 "This ThinkPad has standard ACPI backlight "
3123 "brightness control, supported by the ACPI "
3125 printk(TPACPI_NOTICE
3126 "Disabling thinkpad-acpi brightness events "
3129 /* The hotkey_reserved_mask change below is not
3130 * necessary while the keys are at KEY_RESERVED in the
3131 * default map, but better safe than sorry, leave it
3132 * here as a marker of what we have to do, especially
3133 * when we finally become able to set this at runtime
3134 * on response to X.org requests */
3135 hotkey_reserved_mask
|=
3136 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
3137 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
3140 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3141 "enabling firmware HKEY event interface...\n");
3142 res
= hotkey_status_set(true);
3147 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
3148 & ~hotkey_reserved_mask
)
3149 | hotkey_orig_mask
);
3150 if (res
< 0 && res
!= -ENXIO
) {
3155 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3156 "legacy ibm/hotkey event reporting over procfs %s\n",
3157 (hotkey_report_mode
< 2) ?
3158 "enabled" : "disabled");
3160 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
3161 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
3163 hotkey_poll_setup_safe(1);
3164 tpacpi_send_radiosw_update();
3165 tpacpi_input_send_tabletsw();
3170 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3171 hotkey_dev_attributes
= NULL
;
3173 return (res
< 0)? res
: 1;
3176 static bool hotkey_notify_hotkey(const u32 hkey
,
3178 bool *ignore_acpi_ev
)
3180 /* 0x1000-0x1FFF: key presses */
3181 unsigned int scancode
= hkey
& 0xfff;
3182 *send_acpi_ev
= true;
3183 *ignore_acpi_ev
= false;
3185 if (scancode
> 0 && scancode
< 0x21) {
3187 if (!(hotkey_source_mask
& (1 << scancode
))) {
3188 tpacpi_input_send_key(scancode
);
3189 *send_acpi_ev
= false;
3191 *ignore_acpi_ev
= true;
3198 static bool hotkey_notify_wakeup(const u32 hkey
,
3200 bool *ignore_acpi_ev
)
3202 /* 0x2000-0x2FFF: Wakeup reason */
3203 *send_acpi_ev
= true;
3204 *ignore_acpi_ev
= false;
3207 case 0x2304: /* suspend, undock */
3208 case 0x2404: /* hibernation, undock */
3209 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
3210 *ignore_acpi_ev
= true;
3213 case 0x2305: /* suspend, bay eject */
3214 case 0x2405: /* hibernation, bay eject */
3215 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
3216 *ignore_acpi_ev
= true;
3219 case 0x2313: /* Battery on critical low level (S3) */
3220 case 0x2413: /* Battery on critical low level (S4) */
3222 "EMERGENCY WAKEUP: battery almost empty\n");
3223 /* how to auto-heal: */
3224 /* 2313: woke up from S3, go to S4/S5 */
3225 /* 2413: woke up from S4, go to S5 */
3232 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
3234 "woke up due to a hot-unplug "
3236 hotkey_wakeup_reason_notify_change();
3241 static bool hotkey_notify_usrevent(const u32 hkey
,
3243 bool *ignore_acpi_ev
)
3245 /* 0x5000-0x5FFF: human interface helpers */
3246 *send_acpi_ev
= true;
3247 *ignore_acpi_ev
= false;
3250 case 0x5010: /* Lenovo new BIOS: brightness changed */
3251 case 0x500b: /* X61t: tablet pen inserted into bay */
3252 case 0x500c: /* X61t: tablet pen removed from bay */
3255 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3256 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3257 tpacpi_input_send_tabletsw();
3258 hotkey_tablet_mode_notify_change();
3259 *send_acpi_ev
= false;
3264 /* LID switch events. Do not propagate */
3265 *ignore_acpi_ev
= true;
3273 static bool hotkey_notify_thermal(const u32 hkey
,
3275 bool *ignore_acpi_ev
)
3277 /* 0x6000-0x6FFF: thermal alarms */
3278 *send_acpi_ev
= true;
3279 *ignore_acpi_ev
= false;
3284 "THERMAL ALARM: battery is too hot!\n");
3285 /* recommended action: warn user through gui */
3289 "THERMAL EMERGENCY: battery is extremely hot!\n");
3290 /* recommended action: immediate sleep/hibernate */
3295 "a sensor reports something is too hot!\n");
3296 /* recommended action: warn user through gui, that */
3297 /* some internal component is too hot */
3301 "THERMAL EMERGENCY: "
3302 "a sensor reports something is extremely hot!\n");
3303 /* recommended action: immediate sleep/hibernate */
3307 "EC reports that Thermal Table has changed\n");
3308 /* recommended action: do nothing, we don't have
3309 * Lenovo ATM information */
3313 "THERMAL ALERT: unknown thermal alarm received\n");
3318 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
3322 bool ignore_acpi_ev
;
3325 if (event
!= 0x80) {
3327 "unknown HKEY notification event %d\n", event
);
3328 /* forward it to userspace, maybe it knows how to handle it */
3329 acpi_bus_generate_netlink_event(
3330 ibm
->acpi
->device
->pnp
.device_class
,
3331 dev_name(&ibm
->acpi
->device
->dev
),
3337 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
3338 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
3347 send_acpi_ev
= true;
3348 ignore_acpi_ev
= false;
3350 switch (hkey
>> 12) {
3352 /* 0x1000-0x1FFF: key presses */
3353 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
3357 /* 0x2000-0x2FFF: Wakeup reason */
3358 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
3362 /* 0x3000-0x3FFF: bay-related wakeups */
3363 if (hkey
== 0x3003) {
3364 hotkey_autosleep_ack
= 1;
3367 hotkey_wakeup_hotunplug_complete_notify_change();
3374 /* 0x4000-0x4FFF: dock-related wakeups */
3375 if (hkey
== 0x4003) {
3376 hotkey_autosleep_ack
= 1;
3379 hotkey_wakeup_hotunplug_complete_notify_change();
3386 /* 0x5000-0x5FFF: human interface helpers */
3387 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
3391 /* 0x6000-0x6FFF: thermal alarms */
3392 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
3396 /* 0x7000-0x7FFF: misc */
3397 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
3398 tpacpi_send_radiosw_update();
3403 /* fallthrough to default */
3408 printk(TPACPI_NOTICE
3409 "unhandled HKEY event 0x%04x\n", hkey
);
3410 printk(TPACPI_NOTICE
3411 "please report the conditions when this "
3412 "event happened to %s\n", TPACPI_MAIL
);
3416 if (!ignore_acpi_ev
&&
3417 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
3418 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
3422 /* netlink events */
3423 if (!ignore_acpi_ev
&& send_acpi_ev
) {
3424 acpi_bus_generate_netlink_event(
3425 ibm
->acpi
->device
->pnp
.device_class
,
3426 dev_name(&ibm
->acpi
->device
->dev
),
3432 static void hotkey_suspend(pm_message_t state
)
3434 /* Do these on suspend, we get the events on early resume! */
3435 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
3436 hotkey_autosleep_ack
= 0;
3439 static void hotkey_resume(void)
3441 tpacpi_disable_brightness_delay();
3443 if (hotkey_mask_get())
3445 "error while trying to read hot key mask "
3447 tpacpi_send_radiosw_update();
3448 hotkey_tablet_mode_notify_change();
3449 hotkey_wakeup_reason_notify_change();
3450 hotkey_wakeup_hotunplug_complete_notify_change();
3451 hotkey_poll_setup_safe(0);
3454 /* procfs -------------------------------------------------------------- */
3455 static int hotkey_read(char *p
)
3460 if (!tp_features
.hotkey
) {
3461 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3465 if (mutex_lock_killable(&hotkey_mutex
))
3466 return -ERESTARTSYS
;
3467 res
= hotkey_status_get(&status
);
3469 res
= hotkey_mask_get();
3470 mutex_unlock(&hotkey_mutex
);
3474 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
3475 if (tp_features
.hotkey_mask
) {
3476 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
3477 len
+= sprintf(p
+ len
,
3478 "commands:\tenable, disable, reset, <mask>\n");
3480 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
3481 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
3487 static void hotkey_enabledisable_warn(bool enable
)
3489 tpacpi_log_usertask("procfs hotkey enable/disable");
3490 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
3492 "hotkey enable/disable functionality has been "
3493 "removed from the driver. Hotkeys are always "
3496 "Please remove the hotkey=enable module "
3497 "parameter, it is deprecated. Hotkeys are always "
3501 static int hotkey_write(char *buf
)
3507 if (!tp_features
.hotkey
)
3510 if (mutex_lock_killable(&hotkey_mutex
))
3511 return -ERESTARTSYS
;
3516 while ((cmd
= next_cmd(&buf
))) {
3517 if (strlencmp(cmd
, "enable") == 0) {
3518 hotkey_enabledisable_warn(1);
3519 } else if (strlencmp(cmd
, "disable") == 0) {
3520 hotkey_enabledisable_warn(0);
3522 } else if (strlencmp(cmd
, "reset") == 0) {
3523 mask
= hotkey_orig_mask
;
3524 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
3526 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
3535 tpacpi_disclose_usertask("procfs hotkey",
3536 "set mask to 0x%08x\n", mask
);
3538 if (!res
&& mask
!= hotkey_mask
)
3539 res
= hotkey_mask_set(mask
);
3542 mutex_unlock(&hotkey_mutex
);
3546 static const struct acpi_device_id ibm_htk_device_ids
[] = {
3547 {TPACPI_ACPI_HKEY_HID
, 0},
3551 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
3552 .hid
= ibm_htk_device_ids
,
3553 .notify
= hotkey_notify
,
3554 .handle
= &hkey_handle
,
3555 .type
= ACPI_DEVICE_NOTIFY
,
3558 static struct ibm_struct hotkey_driver_data
= {
3560 .read
= hotkey_read
,
3561 .write
= hotkey_write
,
3562 .exit
= hotkey_exit
,
3563 .resume
= hotkey_resume
,
3564 .suspend
= hotkey_suspend
,
3565 .acpi
= &ibm_hotkey_acpidriver
,
3568 /*************************************************************************
3569 * Bluetooth subdriver
3573 /* ACPI GBDC/SBDC bits */
3574 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3575 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3576 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3581 /* ACPI \BLTH commands */
3582 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3583 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3584 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3585 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3586 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3589 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3591 static int bluetooth_get_status(void)
3595 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3596 if (dbg_bluetoothemul
)
3597 return (tpacpi_bluetooth_emulstate
) ?
3598 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3601 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3604 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3605 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3608 static int bluetooth_set_status(enum tpacpi_rfkill_state state
)
3612 vdbg_printk(TPACPI_DBG_RFKILL
,
3613 "will attempt to %s bluetooth\n",
3614 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
3616 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3617 if (dbg_bluetoothemul
) {
3618 tpacpi_bluetooth_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
3623 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3624 status
= TP_ACPI_BLUETOOTH_RESUMECTRL
;
3625 if (state
== TPACPI_RFK_RADIO_ON
)
3626 status
|= TP_ACPI_BLUETOOTH_RADIOSSW
;
3628 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3634 /* sysfs bluetooth enable ---------------------------------------------- */
3635 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3636 struct device_attribute
*attr
,
3639 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID
,
3643 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3644 struct device_attribute
*attr
,
3645 const char *buf
, size_t count
)
3647 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID
,
3651 static struct device_attribute dev_attr_bluetooth_enable
=
3652 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3653 bluetooth_enable_show
, bluetooth_enable_store
);
3655 /* --------------------------------------------------------------------- */
3657 static struct attribute
*bluetooth_attributes
[] = {
3658 &dev_attr_bluetooth_enable
.attr
,
3662 static const struct attribute_group bluetooth_attr_group
= {
3663 .attrs
= bluetooth_attributes
,
3666 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops
= {
3667 .get_status
= bluetooth_get_status
,
3668 .set_status
= bluetooth_set_status
,
3671 static void bluetooth_shutdown(void)
3673 /* Order firmware to save current state to NVRAM */
3674 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3675 TP_ACPI_BLTH_SAVE_STATE
))
3676 printk(TPACPI_NOTICE
3677 "failed to save bluetooth state to NVRAM\n");
3679 vdbg_printk(TPACPI_DBG_RFKILL
,
3680 "bluestooth state saved to NVRAM\n");
3683 static void bluetooth_exit(void)
3685 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3686 &bluetooth_attr_group
);
3688 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
3690 bluetooth_shutdown();
3693 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3698 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3699 "initializing bluetooth subdriver\n");
3701 TPACPI_ACPIHANDLE_INIT(hkey
);
3703 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3704 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3705 tp_features
.bluetooth
= hkey_handle
&&
3706 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
3708 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3709 "bluetooth is %s, status 0x%02x\n",
3710 str_supported(tp_features
.bluetooth
),
3713 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3714 if (dbg_bluetoothemul
) {
3715 tp_features
.bluetooth
= 1;
3717 "bluetooth switch emulation enabled\n");
3720 if (tp_features
.bluetooth
&&
3721 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
3722 /* no bluetooth hardware present in system */
3723 tp_features
.bluetooth
= 0;
3724 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3725 "bluetooth hardware not installed\n");
3728 if (!tp_features
.bluetooth
)
3731 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
3732 &bluetooth_tprfk_ops
,
3733 RFKILL_TYPE_BLUETOOTH
,
3734 TPACPI_RFK_BLUETOOTH_SW_NAME
,
3739 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3740 &bluetooth_attr_group
);
3742 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
3749 /* procfs -------------------------------------------------------------- */
3750 static int bluetooth_read(char *p
)
3752 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID
, p
);
3755 static int bluetooth_write(char *buf
)
3757 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID
, buf
);
3760 static struct ibm_struct bluetooth_driver_data
= {
3761 .name
= "bluetooth",
3762 .read
= bluetooth_read
,
3763 .write
= bluetooth_write
,
3764 .exit
= bluetooth_exit
,
3765 .shutdown
= bluetooth_shutdown
,
3768 /*************************************************************************
3773 /* ACPI GWAN/SWAN bits */
3774 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
3775 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
3776 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
3780 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3782 static int wan_get_status(void)
3786 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3788 return (tpacpi_wwan_emulstate
) ?
3789 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3792 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
3795 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
3796 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3799 static int wan_set_status(enum tpacpi_rfkill_state state
)
3803 vdbg_printk(TPACPI_DBG_RFKILL
,
3804 "will attempt to %s wwan\n",
3805 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
3807 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3809 tpacpi_wwan_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
3814 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
3815 status
= TP_ACPI_WANCARD_RESUMECTRL
;
3816 if (state
== TPACPI_RFK_RADIO_ON
)
3817 status
|= TP_ACPI_WANCARD_RADIOSSW
;
3819 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
3825 /* sysfs wan enable ---------------------------------------------------- */
3826 static ssize_t
wan_enable_show(struct device
*dev
,
3827 struct device_attribute
*attr
,
3830 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID
,
3834 static ssize_t
wan_enable_store(struct device
*dev
,
3835 struct device_attribute
*attr
,
3836 const char *buf
, size_t count
)
3838 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID
,
3842 static struct device_attribute dev_attr_wan_enable
=
3843 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
3844 wan_enable_show
, wan_enable_store
);
3846 /* --------------------------------------------------------------------- */
3848 static struct attribute
*wan_attributes
[] = {
3849 &dev_attr_wan_enable
.attr
,
3853 static const struct attribute_group wan_attr_group
= {
3854 .attrs
= wan_attributes
,
3857 static const struct tpacpi_rfk_ops wan_tprfk_ops
= {
3858 .get_status
= wan_get_status
,
3859 .set_status
= wan_set_status
,
3862 static void wan_shutdown(void)
3864 /* Order firmware to save current state to NVRAM */
3865 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
3866 TP_ACPI_WGSV_SAVE_STATE
))
3867 printk(TPACPI_NOTICE
3868 "failed to save WWAN state to NVRAM\n");
3870 vdbg_printk(TPACPI_DBG_RFKILL
,
3871 "WWAN state saved to NVRAM\n");
3874 static void wan_exit(void)
3876 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3879 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
3884 static int __init
wan_init(struct ibm_init_struct
*iibm
)
3889 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3890 "initializing wan subdriver\n");
3892 TPACPI_ACPIHANDLE_INIT(hkey
);
3894 tp_features
.wan
= hkey_handle
&&
3895 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
3897 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3898 "wan is %s, status 0x%02x\n",
3899 str_supported(tp_features
.wan
),
3902 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3904 tp_features
.wan
= 1;
3906 "wwan switch emulation enabled\n");
3909 if (tp_features
.wan
&&
3910 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
3911 /* no wan hardware present in system */
3912 tp_features
.wan
= 0;
3913 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3914 "wan hardware not installed\n");
3917 if (!tp_features
.wan
)
3920 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
3923 TPACPI_RFK_WWAN_SW_NAME
,
3928 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3932 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
3939 /* procfs -------------------------------------------------------------- */
3940 static int wan_read(char *p
)
3942 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID
, p
);
3945 static int wan_write(char *buf
)
3947 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID
, buf
);
3950 static struct ibm_struct wan_driver_data
= {
3955 .shutdown
= wan_shutdown
,
3958 /*************************************************************************
3963 /* ACPI GUWB/SUWB bits */
3964 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
3965 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
3968 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3970 static int uwb_get_status(void)
3974 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3976 return (tpacpi_uwb_emulstate
) ?
3977 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3980 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
3983 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
3984 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3987 static int uwb_set_status(enum tpacpi_rfkill_state state
)
3991 vdbg_printk(TPACPI_DBG_RFKILL
,
3992 "will attempt to %s UWB\n",
3993 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
3995 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3997 tpacpi_uwb_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4002 if (state
== TPACPI_RFK_RADIO_ON
)
4003 status
= TP_ACPI_UWB_RADIOSSW
;
4007 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
4013 /* --------------------------------------------------------------------- */
4015 static const struct tpacpi_rfk_ops uwb_tprfk_ops
= {
4016 .get_status
= uwb_get_status
,
4017 .set_status
= uwb_set_status
,
4020 static void uwb_exit(void)
4022 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID
);
4025 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
4030 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4031 "initializing uwb subdriver\n");
4033 TPACPI_ACPIHANDLE_INIT(hkey
);
4035 tp_features
.uwb
= hkey_handle
&&
4036 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
4038 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4039 "uwb is %s, status 0x%02x\n",
4040 str_supported(tp_features
.uwb
),
4043 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4045 tp_features
.uwb
= 1;
4047 "uwb switch emulation enabled\n");
4050 if (tp_features
.uwb
&&
4051 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
4052 /* no uwb hardware present in system */
4053 tp_features
.uwb
= 0;
4054 dbg_printk(TPACPI_DBG_INIT
,
4055 "uwb hardware not installed\n");
4058 if (!tp_features
.uwb
)
4061 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
4064 TPACPI_RFK_UWB_SW_NAME
,
4069 static struct ibm_struct uwb_driver_data
= {
4072 .flags
.experimental
= 1,
4075 /*************************************************************************
4079 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4081 enum video_access_mode
{
4082 TPACPI_VIDEO_NONE
= 0,
4083 TPACPI_VIDEO_570
, /* 570 */
4084 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
4085 TPACPI_VIDEO_NEW
, /* all others */
4088 enum { /* video status flags, based on VIDEO_570 */
4089 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
4090 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
4091 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
4094 enum { /* TPACPI_VIDEO_570 constants */
4095 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
4096 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
4097 * video_status_flags */
4098 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
4099 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
4102 static enum video_access_mode video_supported
;
4103 static int video_orig_autosw
;
4105 static int video_autosw_get(void);
4106 static int video_autosw_set(int enable
);
4108 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4110 static int __init
video_init(struct ibm_init_struct
*iibm
)
4114 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
4116 TPACPI_ACPIHANDLE_INIT(vid
);
4117 TPACPI_ACPIHANDLE_INIT(vid2
);
4119 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
4120 /* G41, assume IVGA doesn't change */
4121 vid_handle
= vid2_handle
;
4124 /* video switching not supported on R30, R31 */
4125 video_supported
= TPACPI_VIDEO_NONE
;
4126 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
4128 video_supported
= TPACPI_VIDEO_570
;
4129 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
4130 /* 600e/x, 770e, 770x */
4131 video_supported
= TPACPI_VIDEO_770
;
4134 video_supported
= TPACPI_VIDEO_NEW
;
4136 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
4137 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
4140 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
4143 static void video_exit(void)
4145 dbg_printk(TPACPI_DBG_EXIT
,
4146 "restoring original video autoswitch mode\n");
4147 if (video_autosw_set(video_orig_autosw
))
4148 printk(TPACPI_ERR
"error while trying to restore original "
4149 "video autoswitch mode\n");
4152 static int video_outputsw_get(void)
4157 switch (video_supported
) {
4158 case TPACPI_VIDEO_570
:
4159 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
4160 TP_ACPI_VIDEO_570_PHSCMD
))
4162 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
4164 case TPACPI_VIDEO_770
:
4165 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4168 status
|= TP_ACPI_VIDEO_S_LCD
;
4169 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4172 status
|= TP_ACPI_VIDEO_S_CRT
;
4174 case TPACPI_VIDEO_NEW
:
4175 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4176 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4179 status
|= TP_ACPI_VIDEO_S_CRT
;
4181 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4182 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4185 status
|= TP_ACPI_VIDEO_S_LCD
;
4186 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4189 status
|= TP_ACPI_VIDEO_S_DVI
;
4198 static int video_outputsw_set(int status
)
4203 switch (video_supported
) {
4204 case TPACPI_VIDEO_570
:
4205 res
= acpi_evalf(NULL
, NULL
,
4206 "\\_SB.PHS2", "vdd",
4207 TP_ACPI_VIDEO_570_PHS2CMD
,
4208 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4210 case TPACPI_VIDEO_770
:
4211 autosw
= video_autosw_get();
4215 res
= video_autosw_set(1);
4218 res
= acpi_evalf(vid_handle
, NULL
,
4219 "ASWT", "vdd", status
* 0x100, 0);
4220 if (!autosw
&& video_autosw_set(autosw
)) {
4222 "video auto-switch left enabled due to error\n");
4226 case TPACPI_VIDEO_NEW
:
4227 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
4228 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
4234 return (res
)? 0 : -EIO
;
4237 static int video_autosw_get(void)
4241 switch (video_supported
) {
4242 case TPACPI_VIDEO_570
:
4243 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
4246 case TPACPI_VIDEO_770
:
4247 case TPACPI_VIDEO_NEW
:
4248 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4258 static int video_autosw_set(int enable
)
4260 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
4265 static int video_outputsw_cycle(void)
4267 int autosw
= video_autosw_get();
4273 switch (video_supported
) {
4274 case TPACPI_VIDEO_570
:
4275 res
= video_autosw_set(1);
4278 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4280 case TPACPI_VIDEO_770
:
4281 case TPACPI_VIDEO_NEW
:
4282 res
= video_autosw_set(1);
4285 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4290 if (!autosw
&& video_autosw_set(autosw
)) {
4292 "video auto-switch left enabled due to error\n");
4296 return (res
)? 0 : -EIO
;
4299 static int video_expand_toggle(void)
4301 switch (video_supported
) {
4302 case TPACPI_VIDEO_570
:
4303 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4305 case TPACPI_VIDEO_770
:
4306 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4308 case TPACPI_VIDEO_NEW
:
4309 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4317 static int video_read(char *p
)
4322 if (video_supported
== TPACPI_VIDEO_NONE
) {
4323 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4327 status
= video_outputsw_get();
4331 autosw
= video_autosw_get();
4335 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4336 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
4337 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
4338 if (video_supported
== TPACPI_VIDEO_NEW
)
4339 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
4340 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
4341 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
4342 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
4343 if (video_supported
== TPACPI_VIDEO_NEW
)
4344 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
4345 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
4346 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4351 static int video_write(char *buf
)
4354 int enable
, disable
, status
;
4357 if (video_supported
== TPACPI_VIDEO_NONE
)
4363 while ((cmd
= next_cmd(&buf
))) {
4364 if (strlencmp(cmd
, "lcd_enable") == 0) {
4365 enable
|= TP_ACPI_VIDEO_S_LCD
;
4366 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4367 disable
|= TP_ACPI_VIDEO_S_LCD
;
4368 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4369 enable
|= TP_ACPI_VIDEO_S_CRT
;
4370 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4371 disable
|= TP_ACPI_VIDEO_S_CRT
;
4372 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4373 strlencmp(cmd
, "dvi_enable") == 0) {
4374 enable
|= TP_ACPI_VIDEO_S_DVI
;
4375 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4376 strlencmp(cmd
, "dvi_disable") == 0) {
4377 disable
|= TP_ACPI_VIDEO_S_DVI
;
4378 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4379 res
= video_autosw_set(1);
4382 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4383 res
= video_autosw_set(0);
4386 } else if (strlencmp(cmd
, "video_switch") == 0) {
4387 res
= video_outputsw_cycle();
4390 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4391 res
= video_expand_toggle();
4398 if (enable
|| disable
) {
4399 status
= video_outputsw_get();
4402 res
= video_outputsw_set((status
& ~disable
) | enable
);
4410 static struct ibm_struct video_driver_data
= {
4413 .write
= video_write
,
4417 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4419 /*************************************************************************
4420 * Light (thinklight) subdriver
4423 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4424 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4426 static int light_get_status(void)
4430 if (tp_features
.light_status
) {
4431 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4439 static int light_set_status(int status
)
4443 if (tp_features
.light
) {
4445 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4447 TP_CMOS_THINKLIGHT_ON
:
4448 TP_CMOS_THINKLIGHT_OFF
);
4450 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4453 return (rc
)? 0 : -EIO
;
4459 static void light_set_status_worker(struct work_struct
*work
)
4461 struct tpacpi_led_classdev
*data
=
4462 container_of(work
, struct tpacpi_led_classdev
, work
);
4464 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4465 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4468 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4469 enum led_brightness brightness
)
4471 struct tpacpi_led_classdev
*data
=
4472 container_of(led_cdev
,
4473 struct tpacpi_led_classdev
,
4475 data
->new_state
= (brightness
!= LED_OFF
) ?
4476 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4477 queue_work(tpacpi_wq
, &data
->work
);
4480 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4482 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4485 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4487 .name
= "tpacpi::thinklight",
4488 .brightness_set
= &light_sysfs_set
,
4489 .brightness_get
= &light_sysfs_get
,
4493 static int __init
light_init(struct ibm_init_struct
*iibm
)
4497 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4499 TPACPI_ACPIHANDLE_INIT(ledb
);
4500 TPACPI_ACPIHANDLE_INIT(lght
);
4501 TPACPI_ACPIHANDLE_INIT(cmos
);
4502 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4504 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4505 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4507 if (tp_features
.light
)
4508 /* light status not supported on
4509 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4510 tp_features
.light_status
=
4511 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4513 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4514 str_supported(tp_features
.light
),
4515 str_supported(tp_features
.light_status
));
4517 if (!tp_features
.light
)
4520 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4521 &tpacpi_led_thinklight
.led_classdev
);
4524 tp_features
.light
= 0;
4525 tp_features
.light_status
= 0;
4533 static void light_exit(void)
4535 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4536 if (work_pending(&tpacpi_led_thinklight
.work
))
4537 flush_workqueue(tpacpi_wq
);
4540 static int light_read(char *p
)
4545 if (!tp_features
.light
) {
4546 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4547 } else if (!tp_features
.light_status
) {
4548 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4549 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4551 status
= light_get_status();
4554 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4555 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4561 static int light_write(char *buf
)
4566 if (!tp_features
.light
)
4569 while ((cmd
= next_cmd(&buf
))) {
4570 if (strlencmp(cmd
, "on") == 0) {
4572 } else if (strlencmp(cmd
, "off") == 0) {
4578 return light_set_status(newstatus
);
4581 static struct ibm_struct light_driver_data
= {
4584 .write
= light_write
,
4588 /*************************************************************************
4592 /* sysfs cmos_command -------------------------------------------------- */
4593 static ssize_t
cmos_command_store(struct device
*dev
,
4594 struct device_attribute
*attr
,
4595 const char *buf
, size_t count
)
4597 unsigned long cmos_cmd
;
4600 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4603 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4604 return (res
)? res
: count
;
4607 static struct device_attribute dev_attr_cmos_command
=
4608 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4610 /* --------------------------------------------------------------------- */
4612 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4616 vdbg_printk(TPACPI_DBG_INIT
,
4617 "initializing cmos commands subdriver\n");
4619 TPACPI_ACPIHANDLE_INIT(cmos
);
4621 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4622 str_supported(cmos_handle
!= NULL
));
4624 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4628 return (cmos_handle
)? 0 : 1;
4631 static void cmos_exit(void)
4633 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4636 static int cmos_read(char *p
)
4640 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4641 R30, R31, T20-22, X20-21 */
4643 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4645 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4646 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4652 static int cmos_write(char *buf
)
4657 while ((cmd
= next_cmd(&buf
))) {
4658 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4659 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4664 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4672 static struct ibm_struct cmos_driver_data
= {
4675 .write
= cmos_write
,
4679 /*************************************************************************
4683 enum led_access_mode
{
4684 TPACPI_LED_NONE
= 0,
4685 TPACPI_LED_570
, /* 570 */
4686 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4687 TPACPI_LED_NEW
, /* all others */
4690 enum { /* For TPACPI_LED_OLD */
4691 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4692 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4693 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4696 static enum led_access_mode led_supported
;
4698 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4699 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4700 /* T20-22, X20-21 */
4701 "LED", /* all others */
4704 #define TPACPI_LED_NUMLEDS 16
4705 static struct tpacpi_led_classdev
*tpacpi_leds
;
4706 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
4707 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
4708 /* there's a limit of 19 chars + NULL before 2.6.26 */
4710 "tpacpi:orange:batt",
4711 "tpacpi:green:batt",
4712 "tpacpi::dock_active",
4713 "tpacpi::bay_active",
4714 "tpacpi::dock_batt",
4715 "tpacpi::unknown_led",
4717 "tpacpi::dock_status1",
4718 "tpacpi::dock_status2",
4719 "tpacpi::unknown_led2",
4720 "tpacpi::unknown_led3",
4721 "tpacpi::thinkvantage",
4723 #define TPACPI_SAFE_LEDS 0x1081U
4725 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
4727 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4730 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
4734 static int led_get_status(const unsigned int led
)
4737 enum led_status_t led_s
;
4739 switch (led_supported
) {
4740 case TPACPI_LED_570
:
4741 if (!acpi_evalf(ec_handle
,
4742 &status
, "GLED", "dd", 1 << led
))
4744 led_s
= (status
== 0)?
4749 tpacpi_led_state_cache
[led
] = led_s
;
4758 static int led_set_status(const unsigned int led
,
4759 const enum led_status_t ledstatus
)
4761 /* off, on, blink. Index is led_status_t */
4762 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
4763 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
4767 switch (led_supported
) {
4768 case TPACPI_LED_570
:
4770 if (unlikely(led
> 7))
4772 if (unlikely(tpacpi_is_led_restricted(led
)))
4774 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4775 (1 << led
), led_sled_arg1
[ledstatus
]))
4778 case TPACPI_LED_OLD
:
4779 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4780 if (unlikely(led
> 7))
4782 if (unlikely(tpacpi_is_led_restricted(led
)))
4784 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
4786 rc
= ec_write(TPACPI_LED_EC_HLBL
,
4787 (ledstatus
== TPACPI_LED_BLINK
) << led
);
4789 rc
= ec_write(TPACPI_LED_EC_HLCL
,
4790 (ledstatus
!= TPACPI_LED_OFF
) << led
);
4792 case TPACPI_LED_NEW
:
4794 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
4796 if (unlikely(tpacpi_is_led_restricted(led
)))
4798 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4799 led
, led_led_arg1
[ledstatus
]))
4807 tpacpi_led_state_cache
[led
] = ledstatus
;
4812 static void led_set_status_worker(struct work_struct
*work
)
4814 struct tpacpi_led_classdev
*data
=
4815 container_of(work
, struct tpacpi_led_classdev
, work
);
4817 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4818 led_set_status(data
->led
, data
->new_state
);
4821 static void led_sysfs_set(struct led_classdev
*led_cdev
,
4822 enum led_brightness brightness
)
4824 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4825 struct tpacpi_led_classdev
, led_classdev
);
4827 if (brightness
== LED_OFF
)
4828 data
->new_state
= TPACPI_LED_OFF
;
4829 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
4830 data
->new_state
= TPACPI_LED_ON
;
4832 data
->new_state
= TPACPI_LED_BLINK
;
4834 queue_work(tpacpi_wq
, &data
->work
);
4837 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
4838 unsigned long *delay_on
, unsigned long *delay_off
)
4840 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4841 struct tpacpi_led_classdev
, led_classdev
);
4843 /* Can we choose the flash rate? */
4844 if (*delay_on
== 0 && *delay_off
== 0) {
4845 /* yes. set them to the hardware blink rate (1 Hz) */
4846 *delay_on
= 500; /* ms */
4847 *delay_off
= 500; /* ms */
4848 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
4851 data
->new_state
= TPACPI_LED_BLINK
;
4852 queue_work(tpacpi_wq
, &data
->work
);
4857 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
4861 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4862 struct tpacpi_led_classdev
, led_classdev
);
4864 rc
= led_get_status(data
->led
);
4866 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
4867 rc
= LED_OFF
; /* no error handling in led class :( */
4874 static void led_exit(void)
4878 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4879 if (tpacpi_leds
[i
].led_classdev
.name
)
4880 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
4886 static int __init
tpacpi_init_led(unsigned int led
)
4890 tpacpi_leds
[led
].led
= led
;
4892 /* LEDs with no name don't get registered */
4893 if (!tpacpi_led_names
[led
])
4896 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
4897 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
4898 if (led_supported
== TPACPI_LED_570
)
4899 tpacpi_leds
[led
].led_classdev
.brightness_get
=
4902 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
4904 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
4906 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4907 &tpacpi_leds
[led
].led_classdev
);
4909 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
4914 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
4915 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4916 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4917 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4919 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4920 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4921 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4922 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4923 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4924 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4925 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4926 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4928 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4929 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4930 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4931 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4932 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4934 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4935 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4936 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4937 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4939 /* (1) - may have excess leds enabled on MSB */
4941 /* Defaults (order matters, keep last, don't reorder!) */
4943 .vendor
= PCI_VENDOR_ID_LENOVO
,
4944 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
4947 { /* IBM ThinkPads with no EC version string */
4948 .vendor
= PCI_VENDOR_ID_IBM
,
4949 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
4952 { /* IBM ThinkPads with EC version string */
4953 .vendor
= PCI_VENDOR_ID_IBM
,
4954 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
4959 #undef TPACPI_LEDQ_IBM
4960 #undef TPACPI_LEDQ_LNV
4962 static int __init
led_init(struct ibm_init_struct
*iibm
)
4966 unsigned long useful_leds
;
4968 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
4970 TPACPI_ACPIHANDLE_INIT(led
);
4973 /* led not supported on R30, R31 */
4974 led_supported
= TPACPI_LED_NONE
;
4975 else if (strlencmp(led_path
, "SLED") == 0)
4977 led_supported
= TPACPI_LED_570
;
4978 else if (strlencmp(led_path
, "SYSL") == 0)
4979 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4980 led_supported
= TPACPI_LED_OLD
;
4983 led_supported
= TPACPI_LED_NEW
;
4985 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
4986 str_supported(led_supported
), led_supported
);
4988 if (led_supported
== TPACPI_LED_NONE
)
4991 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
4994 printk(TPACPI_ERR
"Out of memory for LED data\n");
4998 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
4999 ARRAY_SIZE(led_useful_qtable
));
5001 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5002 if (!tpacpi_is_led_restricted(i
) &&
5003 test_bit(i
, &useful_leds
)) {
5004 rc
= tpacpi_init_led(i
);
5012 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5013 printk(TPACPI_NOTICE
5014 "warning: userspace override of important "
5015 "firmware LEDs is enabled\n");
5020 #define str_led_status(s) \
5021 ((s) == TPACPI_LED_OFF ? "off" : \
5022 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5024 static int led_read(char *p
)
5028 if (!led_supported
) {
5029 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5032 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5034 if (led_supported
== TPACPI_LED_570
) {
5037 for (i
= 0; i
< 8; i
++) {
5038 status
= led_get_status(i
);
5041 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
5042 i
, str_led_status(status
));
5046 len
+= sprintf(p
+ len
, "commands:\t"
5047 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5052 static int led_write(char *buf
)
5056 enum led_status_t s
;
5061 while ((cmd
= next_cmd(&buf
))) {
5062 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 15)
5065 if (strstr(cmd
, "off")) {
5067 } else if (strstr(cmd
, "on")) {
5069 } else if (strstr(cmd
, "blink")) {
5070 s
= TPACPI_LED_BLINK
;
5075 rc
= led_set_status(led
, s
);
5083 static struct ibm_struct led_driver_data
= {
5090 /*************************************************************************
5094 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5096 #define TPACPI_BEEP_Q1 0x0001
5098 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
5099 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
5100 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
5103 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5105 unsigned long quirks
;
5107 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5109 TPACPI_ACPIHANDLE_INIT(beep
);
5111 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5112 str_supported(beep_handle
!= NULL
));
5114 quirks
= tpacpi_check_quirks(beep_quirk_table
,
5115 ARRAY_SIZE(beep_quirk_table
));
5117 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
5119 return (beep_handle
)? 0 : 1;
5122 static int beep_read(char *p
)
5127 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5129 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5130 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5136 static int beep_write(char *buf
)
5144 while ((cmd
= next_cmd(&buf
))) {
5145 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5146 beep_cmd
>= 0 && beep_cmd
<= 17) {
5150 if (tp_features
.beep_needs_two_args
) {
5151 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
5155 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
5164 static struct ibm_struct beep_driver_data
= {
5167 .write
= beep_write
,
5170 /*************************************************************************
5174 enum thermal_access_mode
{
5175 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5176 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5177 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5178 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5179 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5182 enum { /* TPACPI_THERMAL_TPEC_* */
5183 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5184 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5185 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5188 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5189 struct ibm_thermal_sensors_struct
{
5190 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5193 static enum thermal_access_mode thermal_read_mode
;
5195 /* idx is zero-based */
5196 static int thermal_get_sensor(int idx
, s32
*value
)
5202 t
= TP_EC_THERMAL_TMP0
;
5204 switch (thermal_read_mode
) {
5205 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5206 case TPACPI_THERMAL_TPEC_16
:
5207 if (idx
>= 8 && idx
<= 15) {
5208 t
= TP_EC_THERMAL_TMP8
;
5213 case TPACPI_THERMAL_TPEC_8
:
5215 if (!acpi_ec_read(t
+ idx
, &tmp
))
5217 *value
= tmp
* 1000;
5222 case TPACPI_THERMAL_ACPI_UPDT
:
5224 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5225 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5227 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5229 *value
= (t
- 2732) * 100;
5234 case TPACPI_THERMAL_ACPI_TMP07
:
5236 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5237 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5239 if (t
> 127 || t
< -127)
5240 t
= TP_EC_THERMAL_TMP_NA
;
5246 case TPACPI_THERMAL_NONE
:
5254 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5265 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5268 for (i
= 0 ; i
< n
; i
++) {
5269 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5277 /* sysfs temp##_input -------------------------------------------------- */
5279 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5280 struct device_attribute
*attr
,
5283 struct sensor_device_attribute
*sensor_attr
=
5284 to_sensor_dev_attr(attr
);
5285 int idx
= sensor_attr
->index
;
5289 res
= thermal_get_sensor(idx
, &value
);
5292 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
5295 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5298 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5299 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5300 thermal_temp_input_show, NULL, _idxB)
5302 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5303 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5304 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5305 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5306 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5307 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5308 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5309 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5310 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5311 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5312 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5313 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5314 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5315 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5316 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5317 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5318 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5321 #define THERMAL_ATTRS(X) \
5322 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5324 static struct attribute
*thermal_temp_input_attr
[] = {
5344 static const struct attribute_group thermal_temp_input16_group
= {
5345 .attrs
= thermal_temp_input_attr
5348 static const struct attribute_group thermal_temp_input8_group
= {
5349 .attrs
= &thermal_temp_input_attr
[8]
5352 #undef THERMAL_SENSOR_ATTR_TEMP
5353 #undef THERMAL_ATTRS
5355 /* --------------------------------------------------------------------- */
5357 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5364 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5366 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5368 if (thinkpad_id
.ec_model
) {
5370 * Direct EC access mode: sensors at registers
5371 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5372 * non-implemented, thermal sensors return 0x80 when
5377 for (i
= 0; i
< 8; i
++) {
5378 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5384 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5392 /* This is sheer paranoia, but we handle it anyway */
5395 "ThinkPad ACPI EC access misbehaving, "
5396 "falling back to ACPI TMPx access "
5398 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5401 "ThinkPad ACPI EC access misbehaving, "
5402 "disabling thermal sensors access\n");
5403 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5408 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5410 } else if (acpi_tmp7
) {
5411 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5412 /* 600e/x, 770e, 770x */
5413 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5415 /* Standard ACPI TMPx access, max 8 sensors */
5416 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5419 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5420 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5423 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5424 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5427 switch (thermal_read_mode
) {
5428 case TPACPI_THERMAL_TPEC_16
:
5429 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5430 &thermal_temp_input16_group
);
5434 case TPACPI_THERMAL_TPEC_8
:
5435 case TPACPI_THERMAL_ACPI_TMP07
:
5436 case TPACPI_THERMAL_ACPI_UPDT
:
5437 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5438 &thermal_temp_input8_group
);
5442 case TPACPI_THERMAL_NONE
:
5450 static void thermal_exit(void)
5452 switch (thermal_read_mode
) {
5453 case TPACPI_THERMAL_TPEC_16
:
5454 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5455 &thermal_temp_input16_group
);
5457 case TPACPI_THERMAL_TPEC_8
:
5458 case TPACPI_THERMAL_ACPI_TMP07
:
5459 case TPACPI_THERMAL_ACPI_UPDT
:
5460 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5461 &thermal_temp_input16_group
);
5463 case TPACPI_THERMAL_NONE
:
5469 static int thermal_read(char *p
)
5473 struct ibm_thermal_sensors_struct t
;
5475 n
= thermal_get_sensors(&t
);
5476 if (unlikely(n
< 0))
5479 len
+= sprintf(p
+ len
, "temperatures:\t");
5482 for (i
= 0; i
< (n
- 1); i
++)
5483 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5484 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5486 len
+= sprintf(p
+ len
, "not supported\n");
5491 static struct ibm_struct thermal_driver_data
= {
5493 .read
= thermal_read
,
5494 .exit
= thermal_exit
,
5497 /*************************************************************************
5501 static u8 ecdump_regs
[256];
5503 static int ecdump_read(char *p
)
5509 len
+= sprintf(p
+ len
, "EC "
5510 " +00 +01 +02 +03 +04 +05 +06 +07"
5511 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5512 for (i
= 0; i
< 256; i
+= 16) {
5513 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5514 for (j
= 0; j
< 16; j
++) {
5515 if (!acpi_ec_read(i
+ j
, &v
))
5517 if (v
!= ecdump_regs
[i
+ j
])
5518 len
+= sprintf(p
+ len
, " *%02x", v
);
5520 len
+= sprintf(p
+ len
, " %02x", v
);
5521 ecdump_regs
[i
+ j
] = v
;
5523 len
+= sprintf(p
+ len
, "\n");
5528 /* These are way too dangerous to advertise openly... */
5530 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5531 " (<offset> is 00-ff, <value> is 00-ff)\n");
5532 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5533 " (<offset> is 00-ff, <value> is 0-255)\n");
5538 static int ecdump_write(char *buf
)
5543 while ((cmd
= next_cmd(&buf
))) {
5544 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5546 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5550 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5551 if (!acpi_ec_write(i
, v
))
5560 static struct ibm_struct ecdump_driver_data
= {
5562 .read
= ecdump_read
,
5563 .write
= ecdump_write
,
5564 .flags
.experimental
= 1,
5567 /*************************************************************************
5568 * Backlight/brightness subdriver
5571 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5574 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5575 * CMOS NVRAM byte 0x5E, bits 0-3.
5577 * EC HBRV (0x31) has the following layout
5578 * Bit 7: unknown function
5579 * Bit 6: unknown function
5580 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5581 * Bit 4: must be set to zero to avoid problems
5582 * Bit 3-0: backlight brightness level
5584 * brightness_get_raw returns status data in the HBRV layout
5586 * WARNING: The X61 has been verified to use HBRV for something else, so
5587 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5588 * testing on the very early *60 Lenovo models...
5592 TP_EC_BACKLIGHT
= 0x31,
5594 /* TP_EC_BACKLIGHT bitmasks */
5595 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5596 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5597 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5600 enum tpacpi_brightness_access_mode
{
5601 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
5602 TPACPI_BRGHT_MODE_EC
, /* EC control */
5603 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
5604 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
5605 TPACPI_BRGHT_MODE_MAX
5608 static struct backlight_device
*ibm_backlight_device
;
5610 static enum tpacpi_brightness_access_mode brightness_mode
=
5611 TPACPI_BRGHT_MODE_MAX
;
5613 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5615 static struct mutex brightness_mutex
;
5617 /* NVRAM brightness access,
5618 * call with brightness_mutex held! */
5619 static unsigned int tpacpi_brightness_nvram_get(void)
5623 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5624 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5625 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5626 lnvram
&= (tp_features
.bright_16levels
) ? 0x0f : 0x07;
5631 static void tpacpi_brightness_checkpoint_nvram(void)
5636 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
5639 vdbg_printk(TPACPI_DBG_BRGHT
,
5640 "trying to checkpoint backlight level to NVRAM...\n");
5642 if (mutex_lock_killable(&brightness_mutex
) < 0)
5645 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5647 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5648 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
5650 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5651 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
5652 /* NVRAM needs update */
5653 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
5654 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
5656 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
5657 dbg_printk(TPACPI_DBG_BRGHT
,
5658 "updated NVRAM backlight level to %u (0x%02x)\n",
5659 (unsigned int) lec
, (unsigned int) b_nvram
);
5661 vdbg_printk(TPACPI_DBG_BRGHT
,
5662 "NVRAM backlight level already is %u (0x%02x)\n",
5663 (unsigned int) lec
, (unsigned int) b_nvram
);
5666 mutex_unlock(&brightness_mutex
);
5670 /* call with brightness_mutex held! */
5671 static int tpacpi_brightness_get_raw(int *status
)
5675 switch (brightness_mode
) {
5676 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5677 *status
= tpacpi_brightness_nvram_get();
5679 case TPACPI_BRGHT_MODE_EC
:
5680 case TPACPI_BRGHT_MODE_ECNVRAM
:
5681 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5690 /* call with brightness_mutex held! */
5691 /* do NOT call with illegal backlight level value */
5692 static int tpacpi_brightness_set_ec(unsigned int value
)
5696 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5699 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
5700 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
5701 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
5707 /* call with brightness_mutex held! */
5708 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
5711 unsigned int current_value
, i
;
5713 current_value
= tpacpi_brightness_nvram_get();
5715 if (value
== current_value
)
5718 cmos_cmd
= (value
> current_value
) ?
5719 TP_CMOS_BRIGHTNESS_UP
:
5720 TP_CMOS_BRIGHTNESS_DOWN
;
5721 inc
= (value
> current_value
) ? 1 : -1;
5723 for (i
= current_value
; i
!= value
; i
+= inc
)
5724 if (issue_thinkpad_cmos_command(cmos_cmd
))
5730 /* May return EINTR which can always be mapped to ERESTARTSYS */
5731 static int brightness_set(unsigned int value
)
5735 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
5739 vdbg_printk(TPACPI_DBG_BRGHT
,
5740 "set backlight level to %d\n", value
);
5742 res
= mutex_lock_killable(&brightness_mutex
);
5746 switch (brightness_mode
) {
5747 case TPACPI_BRGHT_MODE_EC
:
5748 case TPACPI_BRGHT_MODE_ECNVRAM
:
5749 res
= tpacpi_brightness_set_ec(value
);
5751 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5752 res
= tpacpi_brightness_set_ucmsstep(value
);
5758 mutex_unlock(&brightness_mutex
);
5762 /* sysfs backlight class ----------------------------------------------- */
5764 static int brightness_update_status(struct backlight_device
*bd
)
5766 unsigned int level
=
5767 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
5768 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
5769 bd
->props
.brightness
: 0;
5771 dbg_printk(TPACPI_DBG_BRGHT
,
5772 "backlight: attempt to set level to %d\n",
5775 /* it is the backlight class's job (caller) to handle
5776 * EINTR and other errors properly */
5777 return brightness_set(level
);
5780 static int brightness_get(struct backlight_device
*bd
)
5784 res
= mutex_lock_killable(&brightness_mutex
);
5788 res
= tpacpi_brightness_get_raw(&status
);
5790 mutex_unlock(&brightness_mutex
);
5795 return status
& TP_EC_BACKLIGHT_LVLMSK
;
5798 static struct backlight_ops ibm_backlight_data
= {
5799 .get_brightness
= brightness_get
,
5800 .update_status
= brightness_update_status
,
5803 /* --------------------------------------------------------------------- */
5806 * These are only useful for models that have only one possibility
5807 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5810 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5811 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5812 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5814 static const struct tpacpi_quirk brightness_quirk_table
[] __initconst
= {
5815 /* Models with ATI GPUs known to require ECNVRAM mode */
5816 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC
), /* T43/p ATI */
5818 /* Models with ATI GPUs that can use ECNVRAM */
5819 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC
),
5820 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
5821 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
5822 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
5824 /* Models with Intel Extreme Graphics 2 */
5825 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC
),
5826 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
5827 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
5829 /* Models with Intel GMA900 */
5830 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC
), /* T43, R52 */
5831 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC
), /* X41 */
5832 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC
), /* X41 Tablet */
5835 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
5838 unsigned long quirks
;
5840 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
5842 mutex_init(&brightness_mutex
);
5844 quirks
= tpacpi_check_quirks(brightness_quirk_table
,
5845 ARRAY_SIZE(brightness_quirk_table
));
5848 * We always attempt to detect acpi support, so as to switch
5849 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5850 * going to publish a backlight interface
5852 b
= tpacpi_check_std_acpi_brightness_support();
5855 if (acpi_video_backlight_support()) {
5856 if (brightness_enable
> 1) {
5857 printk(TPACPI_NOTICE
5858 "Standard ACPI backlight interface "
5859 "available, not loading native one.\n");
5861 } else if (brightness_enable
== 1) {
5862 printk(TPACPI_NOTICE
5863 "Backlight control force enabled, even if standard "
5864 "ACPI backlight interface is available\n");
5867 if (brightness_enable
> 1) {
5868 printk(TPACPI_NOTICE
5869 "Standard ACPI backlight interface not "
5870 "available, thinkpad_acpi native "
5871 "brightness control enabled\n");
5876 if (!brightness_enable
) {
5877 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
5878 "brightness support disabled by "
5879 "module parameter\n");
5885 "Unsupported brightness interface, "
5886 "please contact %s\n", TPACPI_MAIL
);
5890 tp_features
.bright_16levels
= 1;
5893 * Check for module parameter bogosity, note that we
5894 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5895 * able to detect "unspecified"
5897 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
5900 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5901 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
5902 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
5903 if (quirks
& TPACPI_BRGHT_Q_EC
)
5904 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
5906 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
5908 dbg_printk(TPACPI_DBG_BRGHT
,
5909 "driver auto-selected brightness_mode=%d\n",
5914 if (thinkpad_id
.vendor
!= PCI_VENDOR_ID_IBM
&&
5915 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
5916 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
5919 if (tpacpi_brightness_get_raw(&b
) < 0)
5922 if (tp_features
.bright_16levels
)
5924 "detected a 16-level brightness capable ThinkPad\n");
5926 ibm_backlight_device
= backlight_device_register(
5927 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
5928 &ibm_backlight_data
);
5929 if (IS_ERR(ibm_backlight_device
)) {
5930 printk(TPACPI_ERR
"Could not register backlight device\n");
5931 return PTR_ERR(ibm_backlight_device
);
5933 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
5934 "brightness is supported\n");
5936 if (quirks
& TPACPI_BRGHT_Q_ASK
) {
5937 printk(TPACPI_NOTICE
5938 "brightness: will use unverified default: "
5939 "brightness_mode=%d\n", brightness_mode
);
5940 printk(TPACPI_NOTICE
5941 "brightness: please report to %s whether it works well "
5942 "or not on your ThinkPad\n", TPACPI_MAIL
);
5945 ibm_backlight_device
->props
.max_brightness
=
5946 (tp_features
.bright_16levels
)? 15 : 7;
5947 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
5948 backlight_update_status(ibm_backlight_device
);
5953 static void brightness_suspend(pm_message_t state
)
5955 tpacpi_brightness_checkpoint_nvram();
5958 static void brightness_shutdown(void)
5960 tpacpi_brightness_checkpoint_nvram();
5963 static void brightness_exit(void)
5965 if (ibm_backlight_device
) {
5966 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
5967 "calling backlight_device_unregister()\n");
5968 backlight_device_unregister(ibm_backlight_device
);
5971 tpacpi_brightness_checkpoint_nvram();
5974 static int brightness_read(char *p
)
5979 level
= brightness_get(NULL
);
5981 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5983 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
5984 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
5985 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5986 " (<level> is 0-%d)\n",
5987 (tp_features
.bright_16levels
) ? 15 : 7);
5993 static int brightness_write(char *buf
)
5998 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
6000 level
= brightness_get(NULL
);
6004 while ((cmd
= next_cmd(&buf
))) {
6005 if (strlencmp(cmd
, "up") == 0) {
6006 if (level
< max_level
)
6008 } else if (strlencmp(cmd
, "down") == 0) {
6011 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
6012 level
>= 0 && level
<= max_level
) {
6018 tpacpi_disclose_usertask("procfs brightness",
6019 "set level to %d\n", level
);
6022 * Now we know what the final level should be, so we try to set it.
6023 * Doing it this way makes the syscall restartable in case of EINTR
6025 rc
= brightness_set(level
);
6026 return (rc
== -EINTR
)? -ERESTARTSYS
: rc
;
6029 static struct ibm_struct brightness_driver_data
= {
6030 .name
= "brightness",
6031 .read
= brightness_read
,
6032 .write
= brightness_write
,
6033 .exit
= brightness_exit
,
6034 .suspend
= brightness_suspend
,
6035 .shutdown
= brightness_shutdown
,
6038 /*************************************************************************
6042 static int volume_offset
= 0x30;
6044 static int volume_read(char *p
)
6049 if (!acpi_ec_read(volume_offset
, &level
)) {
6050 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6052 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
6053 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
6054 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
6055 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
6056 " (<level> is 0-15)\n");
6062 static int volume_write(char *buf
)
6064 int cmos_cmd
, inc
, i
;
6066 int new_level
, new_mute
;
6069 while ((cmd
= next_cmd(&buf
))) {
6070 if (!acpi_ec_read(volume_offset
, &level
))
6072 new_mute
= mute
= level
& 0x40;
6073 new_level
= level
= level
& 0xf;
6075 if (strlencmp(cmd
, "up") == 0) {
6079 new_level
= level
== 15 ? 15 : level
+ 1;
6080 } else if (strlencmp(cmd
, "down") == 0) {
6084 new_level
= level
== 0 ? 0 : level
- 1;
6085 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
6086 new_level
>= 0 && new_level
<= 15) {
6088 } else if (strlencmp(cmd
, "mute") == 0) {
6093 if (new_level
!= level
) {
6094 /* mute doesn't change */
6096 cmos_cmd
= (new_level
> level
) ?
6097 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
6098 inc
= new_level
> level
? 1 : -1;
6100 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
6101 !acpi_ec_write(volume_offset
, level
)))
6104 for (i
= level
; i
!= new_level
; i
+= inc
)
6105 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
6106 !acpi_ec_write(volume_offset
, i
+ inc
))
6110 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
6111 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
6116 if (new_mute
!= mute
) {
6117 /* level doesn't change */
6119 cmos_cmd
= (new_mute
) ?
6120 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
6122 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
6123 !acpi_ec_write(volume_offset
, level
+ new_mute
))
6131 static struct ibm_struct volume_driver_data
= {
6133 .read
= volume_read
,
6134 .write
= volume_write
,
6137 /*************************************************************************
6144 * TPACPI_FAN_RD_ACPI_GFAN:
6145 * ACPI GFAN method: returns fan level
6147 * see TPACPI_FAN_WR_ACPI_SFAN
6148 * EC 0x2f (HFSP) not available if GFAN exists
6150 * TPACPI_FAN_WR_ACPI_SFAN:
6151 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6153 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6156 * TPACPI_FAN_WR_TPEC:
6157 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6158 * Supported on almost all ThinkPads
6160 * Fan speed changes of any sort (including those caused by the
6161 * disengaged mode) are usually done slowly by the firmware as the
6162 * maximum ammount of fan duty cycle change per second seems to be
6165 * Reading is not available if GFAN exists.
6166 * Writing is not available if SFAN exists.
6169 * 7 automatic mode engaged;
6170 * (default operation mode of the ThinkPad)
6171 * fan level is ignored in this mode.
6172 * 6 full speed mode (takes precedence over bit 7);
6173 * not available on all thinkpads. May disable
6174 * the tachometer while the fan controller ramps up
6175 * the speed (which can take up to a few *minutes*).
6176 * Speeds up fan to 100% duty-cycle, which is far above
6177 * the standard RPM levels. It is not impossible that
6178 * it could cause hardware damage.
6179 * 5-3 unused in some models. Extra bits for fan level
6180 * in others, but still useless as all values above
6181 * 7 map to the same speed as level 7 in these models.
6182 * 2-0 fan level (0..7 usually)
6184 * 0x07 = max (set when temperatures critical)
6185 * Some ThinkPads may have other levels, see
6186 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6188 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6189 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6190 * does so, its initial value is meaningless (0x07).
6192 * For firmware bugs, refer to:
6193 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6197 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6198 * Main fan tachometer reading (in RPM)
6200 * This register is present on all ThinkPads with a new-style EC, and
6201 * it is known not to be present on the A21m/e, and T22, as there is
6202 * something else in offset 0x84 according to the ACPI DSDT. Other
6203 * ThinkPads from this same time period (and earlier) probably lack the
6204 * tachometer as well.
6206 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6207 * was never fixed by IBM to report the EC firmware version string
6208 * probably support the tachometer (like the early X models), so
6209 * detecting it is quite hard. We need more data to know for sure.
6211 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6214 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6217 * For firmware bugs, refer to:
6218 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6222 * ThinkPad EC register 0x31 bit 0 (only on select models)
6224 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6225 * show the speed of the main fan. When bit 0 of EC register 0x31
6226 * is one, the tachometer registers show the speed of the auxiliary
6229 * Fan control seems to affect both fans, regardless of the state
6232 * So far, only the firmware for the X60/X61 non-tablet versions
6233 * seem to support this (firmware TP-7M).
6235 * TPACPI_FAN_WR_ACPI_FANS:
6236 * ThinkPad X31, X40, X41. Not available in the X60.
6238 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6239 * high speed. ACPI DSDT seems to map these three speeds to levels
6240 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6241 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6243 * The speeds are stored on handles
6244 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6246 * There are three default speed sets, acessible as handles:
6247 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6249 * ACPI DSDT switches which set is in use depending on various
6252 * TPACPI_FAN_WR_TPEC is also available and should be used to
6253 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6254 * but the ACPI tables just mention level 7.
6257 enum { /* Fan control constants */
6258 fan_status_offset
= 0x2f, /* EC register 0x2f */
6259 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6260 * 0x84 must be read before 0x85 */
6261 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
6262 bit 0 selects which fan is active */
6264 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
6265 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
6267 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
6270 enum fan_status_access_mode
{
6271 TPACPI_FAN_NONE
= 0, /* No fan status or control */
6272 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
6273 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6276 enum fan_control_access_mode
{
6277 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
6278 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
6279 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
6280 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
6283 enum fan_control_commands
{
6284 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
6285 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
6286 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
6287 * and also watchdog cmd */
6290 static int fan_control_allowed
;
6292 static enum fan_status_access_mode fan_status_access_mode
;
6293 static enum fan_control_access_mode fan_control_access_mode
;
6294 static enum fan_control_commands fan_control_commands
;
6296 static u8 fan_control_initial_status
;
6297 static u8 fan_control_desired_level
;
6298 static u8 fan_control_resume_level
;
6299 static int fan_watchdog_maxinterval
;
6301 static struct mutex fan_mutex
;
6303 static void fan_watchdog_fire(struct work_struct
*ignored
);
6304 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
6306 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
6307 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
6308 "\\FSPD", /* 600e/x, 770e, 770x */
6310 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
6311 "JFNS", /* 770x-JL */
6315 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6316 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6317 * be in auto mode (0x80).
6319 * This is corrected by any write to HFSP either by the driver, or
6322 * We assume 0x07 really means auto mode while this quirk is active,
6323 * as this is far more likely than the ThinkPad being in level 7,
6324 * which is only used by the firmware during thermal emergencies.
6326 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6327 * TP-70 (T43, R52), which are known to be buggy.
6330 static void fan_quirk1_setup(void)
6332 if (fan_control_initial_status
== 0x07) {
6333 printk(TPACPI_NOTICE
6334 "fan_init: initial fan status is unknown, "
6335 "assuming it is in auto mode\n");
6336 tp_features
.fan_ctrl_status_undef
= 1;
6340 static void fan_quirk1_handle(u8
*fan_status
)
6342 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
6343 if (*fan_status
!= fan_control_initial_status
) {
6344 /* something changed the HFSP regisnter since
6345 * driver init time, so it is not undefined
6347 tp_features
.fan_ctrl_status_undef
= 0;
6349 /* Return most likely status. In fact, it
6350 * might be the only possible status */
6351 *fan_status
= TP_EC_FAN_AUTO
;
6356 /* Select main fan on X60/X61, NOOP on others */
6357 static bool fan_select_fan1(void)
6359 if (tp_features
.second_fan
) {
6362 if (ec_read(fan_select_offset
, &val
) < 0)
6365 if (ec_write(fan_select_offset
, val
) < 0)
6371 /* Select secondary fan on X60/X61 */
6372 static bool fan_select_fan2(void)
6376 if (!tp_features
.second_fan
)
6379 if (ec_read(fan_select_offset
, &val
) < 0)
6382 if (ec_write(fan_select_offset
, val
) < 0)
6389 * Call with fan_mutex held
6391 static void fan_update_desired_level(u8 status
)
6394 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6396 fan_control_desired_level
= 7;
6398 fan_control_desired_level
= status
;
6402 static int fan_get_status(u8
*status
)
6407 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6409 switch (fan_status_access_mode
) {
6410 case TPACPI_FAN_RD_ACPI_GFAN
:
6411 /* 570, 600e/x, 770e, 770x */
6413 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
6421 case TPACPI_FAN_RD_TPEC
:
6422 /* all except 570, 600e/x, 770e, 770x */
6423 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
6426 if (likely(status
)) {
6428 fan_quirk1_handle(status
);
6440 static int fan_get_status_safe(u8
*status
)
6445 if (mutex_lock_killable(&fan_mutex
))
6446 return -ERESTARTSYS
;
6447 rc
= fan_get_status(&s
);
6449 fan_update_desired_level(s
);
6450 mutex_unlock(&fan_mutex
);
6458 static int fan_get_speed(unsigned int *speed
)
6462 switch (fan_status_access_mode
) {
6463 case TPACPI_FAN_RD_TPEC
:
6464 /* all except 570, 600e/x, 770e, 770x */
6465 if (unlikely(!fan_select_fan1()))
6467 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
6468 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
6472 *speed
= (hi
<< 8) | lo
;
6483 static int fan2_get_speed(unsigned int *speed
)
6488 switch (fan_status_access_mode
) {
6489 case TPACPI_FAN_RD_TPEC
:
6490 /* all except 570, 600e/x, 770e, 770x */
6491 if (unlikely(!fan_select_fan2()))
6493 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
6494 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
6495 fan_select_fan1(); /* play it safe */
6500 *speed
= (hi
<< 8) | lo
;
6511 static int fan_set_level(int level
)
6513 if (!fan_control_allowed
)
6516 switch (fan_control_access_mode
) {
6517 case TPACPI_FAN_WR_ACPI_SFAN
:
6518 if (level
>= 0 && level
<= 7) {
6519 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
6525 case TPACPI_FAN_WR_ACPI_FANS
:
6526 case TPACPI_FAN_WR_TPEC
:
6527 if (!(level
& TP_EC_FAN_AUTO
) &&
6528 !(level
& TP_EC_FAN_FULLSPEED
) &&
6529 ((level
< 0) || (level
> 7)))
6532 /* safety net should the EC not support AUTO
6533 * or FULLSPEED mode bits and just ignore them */
6534 if (level
& TP_EC_FAN_FULLSPEED
)
6535 level
|= 7; /* safety min speed 7 */
6536 else if (level
& TP_EC_FAN_AUTO
)
6537 level
|= 4; /* safety min speed 4 */
6539 if (!acpi_ec_write(fan_status_offset
, level
))
6542 tp_features
.fan_ctrl_status_undef
= 0;
6549 vdbg_printk(TPACPI_DBG_FAN
,
6550 "fan control: set fan control register to 0x%02x\n", level
);
6554 static int fan_set_level_safe(int level
)
6558 if (!fan_control_allowed
)
6561 if (mutex_lock_killable(&fan_mutex
))
6562 return -ERESTARTSYS
;
6564 if (level
== TPACPI_FAN_LAST_LEVEL
)
6565 level
= fan_control_desired_level
;
6567 rc
= fan_set_level(level
);
6569 fan_update_desired_level(level
);
6571 mutex_unlock(&fan_mutex
);
6575 static int fan_set_enable(void)
6580 if (!fan_control_allowed
)
6583 if (mutex_lock_killable(&fan_mutex
))
6584 return -ERESTARTSYS
;
6586 switch (fan_control_access_mode
) {
6587 case TPACPI_FAN_WR_ACPI_FANS
:
6588 case TPACPI_FAN_WR_TPEC
:
6589 rc
= fan_get_status(&s
);
6593 /* Don't go out of emergency fan mode */
6596 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
6599 if (!acpi_ec_write(fan_status_offset
, s
))
6602 tp_features
.fan_ctrl_status_undef
= 0;
6607 case TPACPI_FAN_WR_ACPI_SFAN
:
6608 rc
= fan_get_status(&s
);
6614 /* Set fan to at least level 4 */
6617 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
6627 mutex_unlock(&fan_mutex
);
6630 vdbg_printk(TPACPI_DBG_FAN
,
6631 "fan control: set fan control register to 0x%02x\n",
6636 static int fan_set_disable(void)
6640 if (!fan_control_allowed
)
6643 if (mutex_lock_killable(&fan_mutex
))
6644 return -ERESTARTSYS
;
6647 switch (fan_control_access_mode
) {
6648 case TPACPI_FAN_WR_ACPI_FANS
:
6649 case TPACPI_FAN_WR_TPEC
:
6650 if (!acpi_ec_write(fan_status_offset
, 0x00))
6653 fan_control_desired_level
= 0;
6654 tp_features
.fan_ctrl_status_undef
= 0;
6658 case TPACPI_FAN_WR_ACPI_SFAN
:
6659 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
6662 fan_control_desired_level
= 0;
6670 vdbg_printk(TPACPI_DBG_FAN
,
6671 "fan control: set fan control register to 0\n");
6673 mutex_unlock(&fan_mutex
);
6677 static int fan_set_speed(int speed
)
6681 if (!fan_control_allowed
)
6684 if (mutex_lock_killable(&fan_mutex
))
6685 return -ERESTARTSYS
;
6688 switch (fan_control_access_mode
) {
6689 case TPACPI_FAN_WR_ACPI_FANS
:
6690 if (speed
>= 0 && speed
<= 65535) {
6691 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
6692 speed
, speed
, speed
))
6702 mutex_unlock(&fan_mutex
);
6706 static void fan_watchdog_reset(void)
6708 static int fan_watchdog_active
;
6710 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
6713 if (fan_watchdog_active
)
6714 cancel_delayed_work(&fan_watchdog_task
);
6716 if (fan_watchdog_maxinterval
> 0 &&
6717 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
6718 fan_watchdog_active
= 1;
6719 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
6720 msecs_to_jiffies(fan_watchdog_maxinterval
6723 "failed to queue the fan watchdog, "
6724 "watchdog will not trigger\n");
6727 fan_watchdog_active
= 0;
6730 static void fan_watchdog_fire(struct work_struct
*ignored
)
6734 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
6737 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
6738 rc
= fan_set_enable();
6740 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
6741 "will try again later...\n", -rc
);
6742 /* reschedule for later */
6743 fan_watchdog_reset();
6748 * SYSFS fan layout: hwmon compatible (device)
6751 * 0: "disengaged" mode
6753 * 2: native EC "auto" mode (recommended, hardware default)
6755 * pwm*: set speed in manual mode, ignored otherwise.
6756 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6759 * fan*_input: tachometer reading, RPM
6762 * SYSFS fan layout: extensions
6764 * fan_watchdog (driver):
6765 * fan watchdog interval in seconds, 0 disables (default), max 120
6768 /* sysfs fan pwm1_enable ----------------------------------------------- */
6769 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
6770 struct device_attribute
*attr
,
6776 res
= fan_get_status_safe(&status
);
6780 if (status
& TP_EC_FAN_FULLSPEED
) {
6782 } else if (status
& TP_EC_FAN_AUTO
) {
6787 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
6790 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
6791 struct device_attribute
*attr
,
6792 const char *buf
, size_t count
)
6797 if (parse_strtoul(buf
, 2, &t
))
6800 tpacpi_disclose_usertask("hwmon pwm1_enable",
6801 "set fan mode to %lu\n", t
);
6805 level
= TP_EC_FAN_FULLSPEED
;
6808 level
= TPACPI_FAN_LAST_LEVEL
;
6811 level
= TP_EC_FAN_AUTO
;
6814 /* reserved for software-controlled auto mode */
6820 res
= fan_set_level_safe(level
);
6826 fan_watchdog_reset();
6831 static struct device_attribute dev_attr_fan_pwm1_enable
=
6832 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
6833 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
6835 /* sysfs fan pwm1 ------------------------------------------------------ */
6836 static ssize_t
fan_pwm1_show(struct device
*dev
,
6837 struct device_attribute
*attr
,
6843 res
= fan_get_status_safe(&status
);
6848 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
6849 status
= fan_control_desired_level
;
6854 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
6857 static ssize_t
fan_pwm1_store(struct device
*dev
,
6858 struct device_attribute
*attr
,
6859 const char *buf
, size_t count
)
6863 u8 status
, newlevel
;
6865 if (parse_strtoul(buf
, 255, &s
))
6868 tpacpi_disclose_usertask("hwmon pwm1",
6869 "set fan speed to %lu\n", s
);
6871 /* scale down from 0-255 to 0-7 */
6872 newlevel
= (s
>> 5) & 0x07;
6874 if (mutex_lock_killable(&fan_mutex
))
6875 return -ERESTARTSYS
;
6877 rc
= fan_get_status(&status
);
6878 if (!rc
&& (status
&
6879 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6880 rc
= fan_set_level(newlevel
);
6884 fan_update_desired_level(newlevel
);
6885 fan_watchdog_reset();
6889 mutex_unlock(&fan_mutex
);
6890 return (rc
)? rc
: count
;
6893 static struct device_attribute dev_attr_fan_pwm1
=
6894 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
6895 fan_pwm1_show
, fan_pwm1_store
);
6897 /* sysfs fan fan1_input ------------------------------------------------ */
6898 static ssize_t
fan_fan1_input_show(struct device
*dev
,
6899 struct device_attribute
*attr
,
6905 res
= fan_get_speed(&speed
);
6909 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
6912 static struct device_attribute dev_attr_fan_fan1_input
=
6913 __ATTR(fan1_input
, S_IRUGO
,
6914 fan_fan1_input_show
, NULL
);
6916 /* sysfs fan fan2_input ------------------------------------------------ */
6917 static ssize_t
fan_fan2_input_show(struct device
*dev
,
6918 struct device_attribute
*attr
,
6924 res
= fan2_get_speed(&speed
);
6928 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
6931 static struct device_attribute dev_attr_fan_fan2_input
=
6932 __ATTR(fan2_input
, S_IRUGO
,
6933 fan_fan2_input_show
, NULL
);
6935 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6936 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
6939 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
6942 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
6943 const char *buf
, size_t count
)
6947 if (parse_strtoul(buf
, 120, &t
))
6950 if (!fan_control_allowed
)
6953 fan_watchdog_maxinterval
= t
;
6954 fan_watchdog_reset();
6956 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
6961 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
6962 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
6964 /* --------------------------------------------------------------------- */
6965 static struct attribute
*fan_attributes
[] = {
6966 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
6967 &dev_attr_fan_fan1_input
.attr
,
6968 NULL
, /* for fan2_input */
6972 static const struct attribute_group fan_attr_group
= {
6973 .attrs
= fan_attributes
,
6976 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6977 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
6979 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6980 { .vendor = PCI_VENDOR_ID_IBM, \
6981 .bios = TPACPI_MATCH_ANY, \
6982 .ec = TPID(__id1, __id2), \
6983 .quirks = __quirks }
6985 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
6986 { .vendor = PCI_VENDOR_ID_LENOVO, \
6987 .bios = TPACPI_MATCH_ANY, \
6988 .ec = TPID(__id1, __id2), \
6989 .quirks = __quirks }
6991 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
6992 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
6993 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
6994 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
6995 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
6996 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
6999 #undef TPACPI_FAN_QL
7000 #undef TPACPI_FAN_QI
7002 static int __init
fan_init(struct ibm_init_struct
*iibm
)
7005 unsigned long quirks
;
7007 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7008 "initializing fan subdriver\n");
7010 mutex_init(&fan_mutex
);
7011 fan_status_access_mode
= TPACPI_FAN_NONE
;
7012 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7013 fan_control_commands
= 0;
7014 fan_watchdog_maxinterval
= 0;
7015 tp_features
.fan_ctrl_status_undef
= 0;
7016 tp_features
.second_fan
= 0;
7017 fan_control_desired_level
= 7;
7019 TPACPI_ACPIHANDLE_INIT(fans
);
7020 TPACPI_ACPIHANDLE_INIT(gfan
);
7021 TPACPI_ACPIHANDLE_INIT(sfan
);
7023 quirks
= tpacpi_check_quirks(fan_quirk_table
,
7024 ARRAY_SIZE(fan_quirk_table
));
7027 /* 570, 600e/x, 770e, 770x */
7028 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
7030 /* all other ThinkPads: note that even old-style
7031 * ThinkPad ECs supports the fan control register */
7032 if (likely(acpi_ec_read(fan_status_offset
,
7033 &fan_control_initial_status
))) {
7034 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
7035 if (quirks
& TPACPI_FAN_Q1
)
7037 if (quirks
& TPACPI_FAN_2FAN
) {
7038 tp_features
.second_fan
= 1;
7039 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7040 "secondary fan support enabled\n");
7044 "ThinkPad ACPI EC access misbehaving, "
7045 "fan status and control unavailable\n");
7052 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
7053 fan_control_commands
|=
7054 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
7057 /* gfan without sfan means no fan control */
7058 /* all other models implement TP EC 0x2f control */
7062 fan_control_access_mode
=
7063 TPACPI_FAN_WR_ACPI_FANS
;
7064 fan_control_commands
|=
7065 TPACPI_FAN_CMD_SPEED
|
7066 TPACPI_FAN_CMD_LEVEL
|
7067 TPACPI_FAN_CMD_ENABLE
;
7069 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
7070 fan_control_commands
|=
7071 TPACPI_FAN_CMD_LEVEL
|
7072 TPACPI_FAN_CMD_ENABLE
;
7077 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7078 "fan is %s, modes %d, %d\n",
7079 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
7080 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
7081 fan_status_access_mode
, fan_control_access_mode
);
7083 /* fan control master switch */
7084 if (!fan_control_allowed
) {
7085 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7086 fan_control_commands
= 0;
7087 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7088 "fan control features disabled by parameter\n");
7091 /* update fan_control_desired_level */
7092 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
7093 fan_get_status_safe(NULL
);
7095 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
7096 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
7097 if (tp_features
.second_fan
) {
7098 /* attach second fan tachometer */
7099 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
7100 &dev_attr_fan_fan2_input
.attr
;
7102 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7107 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
7108 &driver_attr_fan_watchdog
);
7110 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7119 static void fan_exit(void)
7121 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
7122 "cancelling any pending fan watchdog tasks\n");
7124 /* FIXME: can we really do this unconditionally? */
7125 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
7126 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
7127 &driver_attr_fan_watchdog
);
7129 cancel_delayed_work(&fan_watchdog_task
);
7130 flush_workqueue(tpacpi_wq
);
7133 static void fan_suspend(pm_message_t state
)
7137 if (!fan_control_allowed
)
7140 /* Store fan status in cache */
7141 fan_control_resume_level
= 0;
7142 rc
= fan_get_status_safe(&fan_control_resume_level
);
7144 printk(TPACPI_NOTICE
7145 "failed to read fan level for later "
7146 "restore during resume: %d\n", rc
);
7148 /* if it is undefined, don't attempt to restore it.
7150 if (tp_features
.fan_ctrl_status_undef
)
7151 fan_control_resume_level
= 0;
7154 static void fan_resume(void)
7156 u8 current_level
= 7;
7157 bool do_set
= false;
7160 /* DSDT *always* updates status on resume */
7161 tp_features
.fan_ctrl_status_undef
= 0;
7163 if (!fan_control_allowed
||
7164 !fan_control_resume_level
||
7165 (fan_get_status_safe(¤t_level
) < 0))
7168 switch (fan_control_access_mode
) {
7169 case TPACPI_FAN_WR_ACPI_SFAN
:
7170 /* never decrease fan level */
7171 do_set
= (fan_control_resume_level
> current_level
);
7173 case TPACPI_FAN_WR_ACPI_FANS
:
7174 case TPACPI_FAN_WR_TPEC
:
7175 /* never decrease fan level, scale is:
7176 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7178 * We expect the firmware to set either 7 or AUTO, but we
7179 * handle FULLSPEED out of paranoia.
7181 * So, we can safely only restore FULLSPEED or 7, anything
7182 * else could slow the fan. Restoring AUTO is useless, at
7183 * best that's exactly what the DSDT already set (it is the
7186 * Always keep in mind that the DSDT *will* have set the
7187 * fans to what the vendor supposes is the best level. We
7188 * muck with it only to speed the fan up.
7190 if (fan_control_resume_level
!= 7 &&
7191 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
7194 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
7195 (current_level
!= fan_control_resume_level
);
7201 printk(TPACPI_NOTICE
7202 "restoring fan level to 0x%02x\n",
7203 fan_control_resume_level
);
7204 rc
= fan_set_level_safe(fan_control_resume_level
);
7206 printk(TPACPI_NOTICE
7207 "failed to restore fan level: %d\n", rc
);
7211 static int fan_read(char *p
)
7216 unsigned int speed
= 0;
7218 switch (fan_status_access_mode
) {
7219 case TPACPI_FAN_RD_ACPI_GFAN
:
7220 /* 570, 600e/x, 770e, 770x */
7221 rc
= fan_get_status_safe(&status
);
7225 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
7227 (status
!= 0) ? "enabled" : "disabled", status
);
7230 case TPACPI_FAN_RD_TPEC
:
7231 /* all except 570, 600e/x, 770e, 770x */
7232 rc
= fan_get_status_safe(&status
);
7236 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
7237 (status
!= 0) ? "enabled" : "disabled");
7239 rc
= fan_get_speed(&speed
);
7243 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
7245 if (status
& TP_EC_FAN_FULLSPEED
)
7246 /* Disengaged mode takes precedence */
7247 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
7248 else if (status
& TP_EC_FAN_AUTO
)
7249 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
7251 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
7254 case TPACPI_FAN_NONE
:
7256 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
7259 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
7260 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
7262 switch (fan_control_access_mode
) {
7263 case TPACPI_FAN_WR_ACPI_SFAN
:
7264 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
7268 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
7269 "auto, disengaged, full-speed)\n");
7274 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
7275 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
7276 "commands:\twatchdog <timeout> (<timeout> "
7277 "is 0 (off), 1-120 (seconds))\n");
7279 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
7280 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
7281 " (<speed> is 0-65535)\n");
7286 static int fan_write_cmd_level(const char *cmd
, int *rc
)
7290 if (strlencmp(cmd
, "level auto") == 0)
7291 level
= TP_EC_FAN_AUTO
;
7292 else if ((strlencmp(cmd
, "level disengaged") == 0) |
7293 (strlencmp(cmd
, "level full-speed") == 0))
7294 level
= TP_EC_FAN_FULLSPEED
;
7295 else if (sscanf(cmd
, "level %d", &level
) != 1)
7298 *rc
= fan_set_level_safe(level
);
7300 printk(TPACPI_ERR
"level command accepted for unsupported "
7301 "access mode %d", fan_control_access_mode
);
7303 tpacpi_disclose_usertask("procfs fan",
7304 "set level to %d\n", level
);
7309 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
7311 if (strlencmp(cmd
, "enable") != 0)
7314 *rc
= fan_set_enable();
7316 printk(TPACPI_ERR
"enable command accepted for unsupported "
7317 "access mode %d", fan_control_access_mode
);
7319 tpacpi_disclose_usertask("procfs fan", "enable\n");
7324 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
7326 if (strlencmp(cmd
, "disable") != 0)
7329 *rc
= fan_set_disable();
7331 printk(TPACPI_ERR
"disable command accepted for unsupported "
7332 "access mode %d", fan_control_access_mode
);
7334 tpacpi_disclose_usertask("procfs fan", "disable\n");
7339 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
7344 * Support speed <low> <medium> <high> ? */
7346 if (sscanf(cmd
, "speed %d", &speed
) != 1)
7349 *rc
= fan_set_speed(speed
);
7351 printk(TPACPI_ERR
"speed command accepted for unsupported "
7352 "access mode %d", fan_control_access_mode
);
7354 tpacpi_disclose_usertask("procfs fan",
7355 "set speed to %d\n", speed
);
7360 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
7364 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
7367 if (interval
< 0 || interval
> 120)
7370 fan_watchdog_maxinterval
= interval
;
7371 tpacpi_disclose_usertask("procfs fan",
7372 "set watchdog timer to %d\n",
7379 static int fan_write(char *buf
)
7384 while (!rc
&& (cmd
= next_cmd(&buf
))) {
7385 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
7386 fan_write_cmd_level(cmd
, &rc
)) &&
7387 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
7388 (fan_write_cmd_enable(cmd
, &rc
) ||
7389 fan_write_cmd_disable(cmd
, &rc
) ||
7390 fan_write_cmd_watchdog(cmd
, &rc
))) &&
7391 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
7392 fan_write_cmd_speed(cmd
, &rc
))
7396 fan_watchdog_reset();
7402 static struct ibm_struct fan_driver_data
= {
7407 .suspend
= fan_suspend
,
7408 .resume
= fan_resume
,
7411 /****************************************************************************
7412 ****************************************************************************
7416 ****************************************************************************
7417 ****************************************************************************/
7419 /* sysfs name ---------------------------------------------------------- */
7420 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
7421 struct device_attribute
*attr
,
7424 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
7427 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
7428 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
7430 /* --------------------------------------------------------------------- */
7433 static struct proc_dir_entry
*proc_dir
;
7436 * Module and infrastructure proble, init and exit handling
7439 static int force_load
;
7441 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7442 static const char * __init
str_supported(int is_supported
)
7444 static char text_unsupported
[] __initdata
= "not supported";
7446 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
7448 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7450 static void ibm_exit(struct ibm_struct
*ibm
)
7452 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
7454 list_del_init(&ibm
->all_drivers
);
7456 if (ibm
->flags
.acpi_notify_installed
) {
7457 dbg_printk(TPACPI_DBG_EXIT
,
7458 "%s: acpi_remove_notify_handler\n", ibm
->name
);
7460 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
7462 dispatch_acpi_notify
);
7463 ibm
->flags
.acpi_notify_installed
= 0;
7464 ibm
->flags
.acpi_notify_installed
= 0;
7467 if (ibm
->flags
.proc_created
) {
7468 dbg_printk(TPACPI_DBG_EXIT
,
7469 "%s: remove_proc_entry\n", ibm
->name
);
7470 remove_proc_entry(ibm
->name
, proc_dir
);
7471 ibm
->flags
.proc_created
= 0;
7474 if (ibm
->flags
.acpi_driver_registered
) {
7475 dbg_printk(TPACPI_DBG_EXIT
,
7476 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
7478 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
7479 kfree(ibm
->acpi
->driver
);
7480 ibm
->acpi
->driver
= NULL
;
7481 ibm
->flags
.acpi_driver_registered
= 0;
7484 if (ibm
->flags
.init_called
&& ibm
->exit
) {
7486 ibm
->flags
.init_called
= 0;
7489 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
7492 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
7495 struct ibm_struct
*ibm
= iibm
->data
;
7496 struct proc_dir_entry
*entry
;
7498 BUG_ON(ibm
== NULL
);
7500 INIT_LIST_HEAD(&ibm
->all_drivers
);
7502 if (ibm
->flags
.experimental
&& !experimental
)
7505 dbg_printk(TPACPI_DBG_INIT
,
7506 "probing for %s\n", ibm
->name
);
7509 ret
= iibm
->init(iibm
);
7511 return 0; /* probe failed */
7515 ibm
->flags
.init_called
= 1;
7519 if (ibm
->acpi
->hid
) {
7520 ret
= register_tpacpi_subdriver(ibm
);
7525 if (ibm
->acpi
->notify
) {
7526 ret
= setup_acpi_notify(ibm
);
7527 if (ret
== -ENODEV
) {
7528 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
7538 dbg_printk(TPACPI_DBG_INIT
,
7539 "%s installed\n", ibm
->name
);
7542 entry
= create_proc_entry(ibm
->name
,
7543 S_IFREG
| S_IRUGO
| S_IWUSR
,
7546 printk(TPACPI_ERR
"unable to create proc entry %s\n",
7552 entry
->read_proc
= &dispatch_procfs_read
;
7554 entry
->write_proc
= &dispatch_procfs_write
;
7555 ibm
->flags
.proc_created
= 1;
7558 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
7563 dbg_printk(TPACPI_DBG_INIT
,
7564 "%s: at error exit path with result %d\n",
7568 return (ret
< 0)? ret
: 0;
7573 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
7575 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
7578 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7579 static bool __pure __init
tpacpi_is_valid_fw_id(const char* const s
,
7582 return s
&& strlen(s
) >= 8 &&
7583 tpacpi_is_fw_digit(s
[0]) &&
7584 tpacpi_is_fw_digit(s
[1]) &&
7585 s
[2] == t
&& s
[3] == 'T' &&
7586 tpacpi_is_fw_digit(s
[4]) &&
7587 tpacpi_is_fw_digit(s
[5]) &&
7588 s
[6] == 'W' && s
[7] == 'W';
7591 /* returns 0 - probe ok, or < 0 - probe error.
7592 * Probe ok doesn't mean thinkpad found.
7593 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7594 static int __must_check __init
get_thinkpad_model_data(
7595 struct thinkpad_id_data
*tp
)
7597 const struct dmi_device
*dev
= NULL
;
7598 char ec_fw_string
[18];
7604 memset(tp
, 0, sizeof(*tp
));
7606 if (dmi_name_in_vendors("IBM"))
7607 tp
->vendor
= PCI_VENDOR_ID_IBM
;
7608 else if (dmi_name_in_vendors("LENOVO"))
7609 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
7613 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
7614 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
7615 if (s
&& !tp
->bios_version_str
)
7618 /* Really ancient ThinkPad 240X will fail this, which is fine */
7619 if (!tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E'))
7622 tp
->bios_model
= tp
->bios_version_str
[0]
7623 | (tp
->bios_version_str
[1] << 8);
7624 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
7625 | tp
->bios_version_str
[5];
7628 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7629 * X32 or newer, all Z series; Some models must have an
7630 * up-to-date BIOS or they will not be detected.
7632 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7634 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
7635 if (sscanf(dev
->name
,
7636 "IBM ThinkPad Embedded Controller -[%17c",
7637 ec_fw_string
) == 1) {
7638 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
7639 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
7641 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
7642 if (!tp
->ec_version_str
)
7645 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
7646 tp
->ec_model
= ec_fw_string
[0]
7647 | (ec_fw_string
[1] << 8);
7648 tp
->ec_release
= (ec_fw_string
[4] << 8)
7651 printk(TPACPI_NOTICE
7652 "ThinkPad firmware release %s "
7653 "doesn't match the known patterns\n",
7655 printk(TPACPI_NOTICE
7656 "please report this to %s\n",
7663 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
7664 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
7665 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
7670 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
7671 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
7672 if (s
&& !tp
->nummodel_str
)
7678 static int __init
probe_for_thinkpad(void)
7686 * Non-ancient models have better DMI tagging, but very old models
7689 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
7691 /* ec is required because many other handles are relative to it */
7692 TPACPI_ACPIHANDLE_INIT(ec
);
7696 "Not yet supported ThinkPad detected!\n");
7701 * Risks a regression on very old machines, but reduces potential
7702 * false positives a damn great deal
7705 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
7707 if (!is_thinkpad
&& !force_load
)
7714 /* Module init, exit, parameters */
7716 static struct ibm_init_struct ibms_init
[] __initdata
= {
7718 .init
= thinkpad_acpi_driver_init
,
7719 .data
= &thinkpad_acpi_driver_data
,
7722 .init
= hotkey_init
,
7723 .data
= &hotkey_driver_data
,
7726 .init
= bluetooth_init
,
7727 .data
= &bluetooth_driver_data
,
7731 .data
= &wan_driver_data
,
7735 .data
= &uwb_driver_data
,
7737 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7740 .data
= &video_driver_data
,
7745 .data
= &light_driver_data
,
7749 .data
= &cmos_driver_data
,
7753 .data
= &led_driver_data
,
7757 .data
= &beep_driver_data
,
7760 .init
= thermal_init
,
7761 .data
= &thermal_driver_data
,
7764 .data
= &ecdump_driver_data
,
7767 .init
= brightness_init
,
7768 .data
= &brightness_driver_data
,
7771 .data
= &volume_driver_data
,
7775 .data
= &fan_driver_data
,
7779 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
7782 struct ibm_struct
*ibm
;
7784 if (!kp
|| !kp
->name
|| !val
)
7787 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7788 ibm
= ibms_init
[i
].data
;
7789 WARN_ON(ibm
== NULL
);
7791 if (!ibm
|| !ibm
->name
)
7794 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
7795 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
7797 strcpy(ibms_init
[i
].param
, val
);
7798 strcat(ibms_init
[i
].param
, ",");
7806 module_param(experimental
, int, 0);
7807 MODULE_PARM_DESC(experimental
,
7808 "Enables experimental features when non-zero");
7810 module_param_named(debug
, dbg_level
, uint
, 0);
7811 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
7813 module_param(force_load
, bool, 0);
7814 MODULE_PARM_DESC(force_load
,
7815 "Attempts to load the driver even on a "
7816 "mis-identified ThinkPad when true");
7818 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
7819 MODULE_PARM_DESC(fan_control
,
7820 "Enables setting fan parameters features when true");
7822 module_param_named(brightness_mode
, brightness_mode
, uint
, 0);
7823 MODULE_PARM_DESC(brightness_mode
,
7824 "Selects brightness control strategy: "
7825 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7827 module_param(brightness_enable
, uint
, 0);
7828 MODULE_PARM_DESC(brightness_enable
,
7829 "Enables backlight control when 1, disables when 0");
7831 module_param(hotkey_report_mode
, uint
, 0);
7832 MODULE_PARM_DESC(hotkey_report_mode
,
7833 "used for backwards compatibility with userspace, "
7834 "see documentation");
7836 #define TPACPI_PARAM(feature) \
7837 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7838 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7839 "at module load, see documentation")
7841 TPACPI_PARAM(hotkey
);
7842 TPACPI_PARAM(bluetooth
);
7843 TPACPI_PARAM(video
);
7844 TPACPI_PARAM(light
);
7848 TPACPI_PARAM(ecdump
);
7849 TPACPI_PARAM(brightness
);
7850 TPACPI_PARAM(volume
);
7853 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7854 module_param(dbg_wlswemul
, uint
, 0);
7855 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
7856 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
7857 MODULE_PARM_DESC(wlsw_state
,
7858 "Initial state of the emulated WLSW switch");
7860 module_param(dbg_bluetoothemul
, uint
, 0);
7861 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
7862 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
7863 MODULE_PARM_DESC(bluetooth_state
,
7864 "Initial state of the emulated bluetooth switch");
7866 module_param(dbg_wwanemul
, uint
, 0);
7867 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
7868 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
7869 MODULE_PARM_DESC(wwan_state
,
7870 "Initial state of the emulated WWAN switch");
7872 module_param(dbg_uwbemul
, uint
, 0);
7873 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
7874 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
7875 MODULE_PARM_DESC(uwb_state
,
7876 "Initial state of the emulated UWB switch");
7879 static void thinkpad_acpi_module_exit(void)
7881 struct ibm_struct
*ibm
, *itmp
;
7883 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
7885 list_for_each_entry_safe_reverse(ibm
, itmp
,
7886 &tpacpi_all_drivers
,
7891 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
7893 if (tpacpi_inputdev
) {
7894 if (tp_features
.input_device_registered
)
7895 input_unregister_device(tpacpi_inputdev
);
7897 input_free_device(tpacpi_inputdev
);
7901 hwmon_device_unregister(tpacpi_hwmon
);
7903 if (tp_features
.sensors_pdev_attrs_registered
)
7904 device_remove_file(&tpacpi_sensors_pdev
->dev
,
7905 &dev_attr_thinkpad_acpi_pdev_name
);
7906 if (tpacpi_sensors_pdev
)
7907 platform_device_unregister(tpacpi_sensors_pdev
);
7909 platform_device_unregister(tpacpi_pdev
);
7911 if (tp_features
.sensors_pdrv_attrs_registered
)
7912 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
7913 if (tp_features
.platform_drv_attrs_registered
)
7914 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
7916 if (tp_features
.sensors_pdrv_registered
)
7917 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
7919 if (tp_features
.platform_drv_registered
)
7920 platform_driver_unregister(&tpacpi_pdriver
);
7923 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
7926 destroy_workqueue(tpacpi_wq
);
7928 kfree(thinkpad_id
.bios_version_str
);
7929 kfree(thinkpad_id
.ec_version_str
);
7930 kfree(thinkpad_id
.model_str
);
7934 static int __init
thinkpad_acpi_module_init(void)
7938 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
7940 /* Parameter checking */
7941 if (hotkey_report_mode
> 2)
7944 /* Driver-level probe */
7946 ret
= get_thinkpad_model_data(&thinkpad_id
);
7949 "unable to get DMI data: %d\n", ret
);
7950 thinkpad_acpi_module_exit();
7953 ret
= probe_for_thinkpad();
7955 thinkpad_acpi_module_exit();
7959 /* Driver initialization */
7961 TPACPI_ACPIHANDLE_INIT(ecrd
);
7962 TPACPI_ACPIHANDLE_INIT(ecwr
);
7964 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
7966 thinkpad_acpi_module_exit();
7970 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
7973 "unable to create proc dir " TPACPI_PROC_DIR
);
7974 thinkpad_acpi_module_exit();
7978 ret
= platform_driver_register(&tpacpi_pdriver
);
7981 "unable to register main platform driver\n");
7982 thinkpad_acpi_module_exit();
7985 tp_features
.platform_drv_registered
= 1;
7987 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
7990 "unable to register hwmon platform driver\n");
7991 thinkpad_acpi_module_exit();
7994 tp_features
.sensors_pdrv_registered
= 1;
7996 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
7998 tp_features
.platform_drv_attrs_registered
= 1;
7999 ret
= tpacpi_create_driver_attributes(
8000 &tpacpi_hwmon_pdriver
.driver
);
8004 "unable to create sysfs driver attributes\n");
8005 thinkpad_acpi_module_exit();
8008 tp_features
.sensors_pdrv_attrs_registered
= 1;
8011 /* Device initialization */
8012 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
8014 if (IS_ERR(tpacpi_pdev
)) {
8015 ret
= PTR_ERR(tpacpi_pdev
);
8017 printk(TPACPI_ERR
"unable to register platform device\n");
8018 thinkpad_acpi_module_exit();
8021 tpacpi_sensors_pdev
= platform_device_register_simple(
8022 TPACPI_HWMON_DRVR_NAME
,
8024 if (IS_ERR(tpacpi_sensors_pdev
)) {
8025 ret
= PTR_ERR(tpacpi_sensors_pdev
);
8026 tpacpi_sensors_pdev
= NULL
;
8028 "unable to register hwmon platform device\n");
8029 thinkpad_acpi_module_exit();
8032 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
8033 &dev_attr_thinkpad_acpi_pdev_name
);
8036 "unable to create sysfs hwmon device attributes\n");
8037 thinkpad_acpi_module_exit();
8040 tp_features
.sensors_pdev_attrs_registered
= 1;
8041 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
8042 if (IS_ERR(tpacpi_hwmon
)) {
8043 ret
= PTR_ERR(tpacpi_hwmon
);
8044 tpacpi_hwmon
= NULL
;
8045 printk(TPACPI_ERR
"unable to register hwmon device\n");
8046 thinkpad_acpi_module_exit();
8049 mutex_init(&tpacpi_inputdev_send_mutex
);
8050 tpacpi_inputdev
= input_allocate_device();
8051 if (!tpacpi_inputdev
) {
8052 printk(TPACPI_ERR
"unable to allocate input device\n");
8053 thinkpad_acpi_module_exit();
8056 /* Prepare input device, but don't register */
8057 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
8058 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
8059 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
8060 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
8061 thinkpad_id
.vendor
:
8063 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
8064 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
8066 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8067 ret
= ibm_init(&ibms_init
[i
]);
8068 if (ret
>= 0 && *ibms_init
[i
].param
)
8069 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
8071 thinkpad_acpi_module_exit();
8075 ret
= input_register_device(tpacpi_inputdev
);
8077 printk(TPACPI_ERR
"unable to register input device\n");
8078 thinkpad_acpi_module_exit();
8081 tp_features
.input_device_registered
= 1;
8084 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
8088 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
8091 * This will autoload the driver in almost every ThinkPad
8092 * in widespread use.
8094 * Only _VERY_ old models, like the 240, 240x and 570 lack
8095 * the HKEY event interface.
8097 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
8100 * DMI matching for module autoloading
8102 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8103 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8105 * Only models listed in thinkwiki will be supported, so add yours
8106 * if it is not there yet.
8108 #define IBM_BIOS_MODULE_ALIAS(__type) \
8109 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8111 /* Ancient thinkpad BIOSes have to be identified by
8112 * BIOS type or model number, and there are far less
8113 * BIOS types than model numbers... */
8114 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8116 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8117 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8118 MODULE_DESCRIPTION(TPACPI_DESC
);
8119 MODULE_VERSION(TPACPI_VERSION
);
8120 MODULE_LICENSE("GPL");
8122 module_init(thinkpad_acpi_module_init
);
8123 module_exit(thinkpad_acpi_module_exit
);