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
169 /* rfkill switches */
171 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
172 TPACPI_RFK_WWAN_SW_ID
,
173 TPACPI_RFK_UWB_SW_ID
,
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
195 #define TPACPI_DBG_BRGHT 0x0020
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
208 struct tp_acpi_drv_struct
{
209 const struct acpi_device_id
*hid
;
210 struct acpi_driver
*driver
;
212 void (*notify
) (struct ibm_struct
*, u32
);
215 struct acpi_device
*device
;
221 int (*read
) (char *);
222 int (*write
) (char *);
224 void (*resume
) (void);
225 void (*suspend
) (pm_message_t state
);
226 void (*shutdown
) (void);
228 struct list_head all_drivers
;
230 struct tp_acpi_drv_struct
*acpi
;
233 u8 acpi_driver_registered
:1;
234 u8 acpi_notify_installed
:1;
241 struct ibm_init_struct
{
244 int (*init
) (struct ibm_init_struct
*);
245 struct ibm_struct
*data
;
249 #ifdef CONFIG_THINKPAD_ACPI_BAY
262 u32 bright_16levels
:1;
263 u32 bright_acpimode
:1;
266 u32 fan_ctrl_status_undef
:1;
268 u32 beep_needs_two_args
:1;
269 u32 input_device_registered
:1;
270 u32 platform_drv_registered
:1;
271 u32 platform_drv_attrs_registered
:1;
272 u32 sensors_pdrv_registered
:1;
273 u32 sensors_pdrv_attrs_registered
:1;
274 u32 sensors_pdev_attrs_registered
:1;
275 u32 hotkey_poll_active
:1;
279 u16 hotkey_mask_ff
:1;
282 struct thinkpad_id_data
{
283 unsigned int vendor
; /* ThinkPad vendor:
284 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
286 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
287 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
289 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
291 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
294 char *model_str
; /* ThinkPad T43 */
295 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
297 static struct thinkpad_id_data thinkpad_id
;
300 TPACPI_LIFE_INIT
= 0,
305 static int experimental
;
306 static u32 dbg_level
;
308 static struct workqueue_struct
*tpacpi_wq
;
316 /* Special LED class that can defer work */
317 struct tpacpi_led_classdev
{
318 struct led_classdev led_classdev
;
319 struct work_struct work
;
320 enum led_status_t new_state
;
324 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
325 static int dbg_wlswemul
;
326 static int tpacpi_wlsw_emulstate
;
327 static int dbg_bluetoothemul
;
328 static int tpacpi_bluetooth_emulstate
;
329 static int dbg_wwanemul
;
330 static int tpacpi_wwan_emulstate
;
331 static int dbg_uwbemul
;
332 static int tpacpi_uwb_emulstate
;
336 /*************************************************************************
340 #define dbg_printk(a_dbg_level, format, arg...) \
341 do { if (dbg_level & (a_dbg_level)) \
342 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
345 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
346 #define vdbg_printk dbg_printk
347 static const char *str_supported(int is_supported
);
349 #define vdbg_printk(a_dbg_level, format, arg...) \
353 static void tpacpi_log_usertask(const char * const what
)
355 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
356 what
, task_tgid_vnr(current
));
359 #define tpacpi_disclose_usertask(what, format, arg...) \
362 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
363 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
364 printk(TPACPI_DEBUG "%s: PID %d: " format, \
365 what, task_tgid_vnr(current), ## arg); \
370 * Quirk handling helpers
372 * ThinkPad IDs and versions seen in the field so far
373 * are two-characters from the set [0-9A-Z], i.e. base 36.
375 * We use values well outside that range as specials.
378 #define TPACPI_MATCH_ANY 0xffffU
379 #define TPACPI_MATCH_UNKNOWN 0U
381 /* TPID('1', 'Y') == 0x5931 */
382 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
384 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_IBM, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
390 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
391 { .vendor = PCI_VENDOR_ID_LENOVO, \
392 .bios = TPID(__id1, __id2), \
393 .ec = TPACPI_MATCH_ANY, \
394 .quirks = (__quirk) }
396 struct tpacpi_quirk
{
400 unsigned long quirks
;
404 * tpacpi_check_quirks() - search BIOS/EC version on a list
405 * @qlist: array of &struct tpacpi_quirk
406 * @qlist_size: number of elements in @qlist
408 * Iterates over a quirks list until one is found that matches the
409 * ThinkPad's vendor, BIOS and EC model.
411 * Returns 0 if nothing matches, otherwise returns the quirks field of
412 * the matching &struct tpacpi_quirk entry.
414 * The match criteria is: vendor, ec and bios much match.
416 static unsigned long __init
tpacpi_check_quirks(
417 const struct tpacpi_quirk
*qlist
,
418 unsigned int qlist_size
)
421 if ((qlist
->vendor
== thinkpad_id
.vendor
||
422 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
423 (qlist
->bios
== thinkpad_id
.bios_model
||
424 qlist
->bios
== TPACPI_MATCH_ANY
) &&
425 (qlist
->ec
== thinkpad_id
.ec_model
||
426 qlist
->ec
== TPACPI_MATCH_ANY
))
427 return qlist
->quirks
;
436 /****************************************************************************
437 ****************************************************************************
439 * ACPI Helpers and device model
441 ****************************************************************************
442 ****************************************************************************/
444 /*************************************************************************
448 static acpi_handle root_handle
;
450 #define TPACPI_HANDLE(object, parent, paths...) \
451 static acpi_handle object##_handle; \
452 static acpi_handle *object##_parent = &parent##_handle; \
453 static char *object##_path; \
454 static char *object##_paths[] = { paths }
456 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
457 "\\_SB.PCI.ISA.EC", /* 570 */
458 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
459 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
460 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
461 "\\_SB.PCI0.ICH3.EC0", /* R31 */
462 "\\_SB.PCI0.LPC.EC", /* all others */
465 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
466 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
468 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
470 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
471 "\\CMS", /* R40, R40e */
474 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
475 "^HKEY", /* R30, R31 */
476 "HKEY", /* all others */
479 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
480 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
481 "\\_SB.PCI0.VID0", /* 770e */
482 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
483 "\\_SB.PCI0.AGP.VID", /* all others */
487 /*************************************************************************
491 static int acpi_evalf(acpi_handle handle
,
492 void *res
, char *method
, char *fmt
, ...)
495 struct acpi_object_list params
;
496 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
497 struct acpi_buffer result
, *resultp
;
498 union acpi_object out_obj
;
506 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
519 params
.pointer
= &in_objs
[0];
526 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
527 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
529 /* add more types as needed */
531 printk(TPACPI_ERR
"acpi_evalf() called "
532 "with invalid format character '%c'\n", c
);
538 if (res_type
!= 'v') {
539 result
.length
= sizeof(out_obj
);
540 result
.pointer
= &out_obj
;
545 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
550 *(int *)res
= out_obj
.integer
.value
;
551 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
554 success
= status
== AE_OK
;
556 /* add more types as needed */
558 printk(TPACPI_ERR
"acpi_evalf() called "
559 "with invalid format character '%c'\n", res_type
);
563 if (!success
&& !quiet
)
564 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
565 method
, fmt0
, status
);
570 static int acpi_ec_read(int i
, u8
*p
)
575 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
579 if (ec_read(i
, p
) < 0)
586 static int acpi_ec_write(int i
, u8 v
)
589 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
592 if (ec_write(i
, v
) < 0)
599 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
600 static int _sta(acpi_handle handle
)
604 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
611 static int issue_thinkpad_cmos_command(int cmos_cmd
)
616 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
622 /*************************************************************************
626 #define TPACPI_ACPIHANDLE_INIT(object) \
627 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
628 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
630 static void drv_acpi_handle_init(char *name
,
631 acpi_handle
*handle
, acpi_handle parent
,
632 char **paths
, int num_paths
, char **path
)
637 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
640 for (i
= 0; i
< num_paths
; i
++) {
641 status
= acpi_get_handle(parent
, paths
[i
], handle
);
642 if (ACPI_SUCCESS(status
)) {
644 dbg_printk(TPACPI_DBG_INIT
,
645 "Found ACPI handle %s for %s\n",
651 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
656 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
658 struct ibm_struct
*ibm
= data
;
660 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
663 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
666 ibm
->acpi
->notify(ibm
, event
);
669 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
676 if (!*ibm
->acpi
->handle
)
679 vdbg_printk(TPACPI_DBG_INIT
,
680 "setting up ACPI notify for %s\n", ibm
->name
);
682 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
684 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
689 ibm
->acpi
->device
->driver_data
= ibm
;
690 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
691 TPACPI_ACPI_EVENT_PREFIX
,
694 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
695 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
696 if (ACPI_FAILURE(status
)) {
697 if (status
== AE_ALREADY_EXISTS
) {
699 "another device driver is already "
700 "handling %s events\n", ibm
->name
);
703 "acpi_install_notify_handler(%s) failed: %d\n",
708 ibm
->flags
.acpi_notify_installed
= 1;
712 static int __init
tpacpi_device_add(struct acpi_device
*device
)
717 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
721 dbg_printk(TPACPI_DBG_INIT
,
722 "registering %s as an ACPI driver\n", ibm
->name
);
726 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
727 if (!ibm
->acpi
->driver
) {
729 "failed to allocate memory for ibm->acpi->driver\n");
733 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
734 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
736 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
738 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
740 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
742 kfree(ibm
->acpi
->driver
);
743 ibm
->acpi
->driver
= NULL
;
745 ibm
->flags
.acpi_driver_registered
= 1;
751 /****************************************************************************
752 ****************************************************************************
756 ****************************************************************************
757 ****************************************************************************/
759 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
760 int count
, int *eof
, void *data
)
762 struct ibm_struct
*ibm
= data
;
765 if (!ibm
|| !ibm
->read
)
768 len
= ibm
->read(page
);
772 if (len
<= off
+ count
)
784 static int dispatch_procfs_write(struct file
*file
,
785 const char __user
*userbuf
,
786 unsigned long count
, void *data
)
788 struct ibm_struct
*ibm
= data
;
792 if (!ibm
|| !ibm
->write
)
795 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
799 if (copy_from_user(kernbuf
, userbuf
, count
)) {
805 strcat(kernbuf
, ",");
806 ret
= ibm
->write(kernbuf
);
815 static char *next_cmd(char **cmds
)
820 while ((end
= strchr(start
, ',')) && end
== start
)
832 /****************************************************************************
833 ****************************************************************************
835 * Device model: input, hwmon and platform
837 ****************************************************************************
838 ****************************************************************************/
840 static struct platform_device
*tpacpi_pdev
;
841 static struct platform_device
*tpacpi_sensors_pdev
;
842 static struct device
*tpacpi_hwmon
;
843 static struct input_dev
*tpacpi_inputdev
;
844 static struct mutex tpacpi_inputdev_send_mutex
;
845 static LIST_HEAD(tpacpi_all_drivers
);
847 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
850 struct ibm_struct
*ibm
, *itmp
;
852 list_for_each_entry_safe(ibm
, itmp
,
856 (ibm
->suspend
)(state
);
862 static int tpacpi_resume_handler(struct platform_device
*pdev
)
864 struct ibm_struct
*ibm
, *itmp
;
866 list_for_each_entry_safe(ibm
, itmp
,
876 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
878 struct ibm_struct
*ibm
, *itmp
;
880 list_for_each_entry_safe(ibm
, itmp
,
888 static struct platform_driver tpacpi_pdriver
= {
890 .name
= TPACPI_DRVR_NAME
,
891 .owner
= THIS_MODULE
,
893 .suspend
= tpacpi_suspend_handler
,
894 .resume
= tpacpi_resume_handler
,
895 .shutdown
= tpacpi_shutdown_handler
,
898 static struct platform_driver tpacpi_hwmon_pdriver
= {
900 .name
= TPACPI_HWMON_DRVR_NAME
,
901 .owner
= THIS_MODULE
,
905 /*************************************************************************
906 * sysfs support helpers
909 struct attribute_set
{
910 unsigned int members
, max_members
;
911 struct attribute_group group
;
914 struct attribute_set_obj
{
915 struct attribute_set s
;
917 } __attribute__((packed
));
919 static struct attribute_set
*create_attr_set(unsigned int max_members
,
922 struct attribute_set_obj
*sobj
;
924 if (max_members
== 0)
927 /* Allocates space for implicit NULL at the end too */
928 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
929 max_members
* sizeof(struct attribute
*),
933 sobj
->s
.max_members
= max_members
;
934 sobj
->s
.group
.attrs
= &sobj
->a
;
935 sobj
->s
.group
.name
= name
;
940 #define destroy_attr_set(_set) \
943 /* not multi-threaded safe, use it in a single thread per set */
944 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
949 if (s
->members
>= s
->max_members
)
952 s
->group
.attrs
[s
->members
] = attr
;
958 static int add_many_to_attr_set(struct attribute_set
*s
,
959 struct attribute
**attr
,
964 for (i
= 0; i
< count
; i
++) {
965 res
= add_to_attr_set(s
, attr
[i
]);
973 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
975 sysfs_remove_group(kobj
, &s
->group
);
979 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
980 sysfs_create_group(_kobj, &_attr_set->group)
982 static int parse_strtoul(const char *buf
,
983 unsigned long max
, unsigned long *value
)
987 while (*buf
&& isspace(*buf
))
989 *value
= simple_strtoul(buf
, &endp
, 0);
990 while (*endp
&& isspace(*endp
))
992 if (*endp
|| *value
> max
)
998 static void tpacpi_disable_brightness_delay(void)
1000 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
1001 printk(TPACPI_NOTICE
1002 "ACPI backlight control delay disabled\n");
1005 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
1007 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1008 union acpi_object
*obj
;
1011 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
1012 obj
= (union acpi_object
*)buffer
.pointer
;
1013 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
1014 printk(TPACPI_ERR
"Unknown _BCL data, "
1015 "please report this to %s\n", TPACPI_MAIL
);
1018 rc
= obj
->package
.count
;
1024 kfree(buffer
.pointer
);
1028 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
1029 u32 lvl
, void *context
, void **rv
)
1031 char name
[ACPI_PATH_SEGMENT_LENGTH
];
1032 struct acpi_buffer buffer
= { sizeof(name
), &name
};
1034 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
1035 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
1036 BUG_ON(!rv
|| !*rv
);
1037 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
1038 return AE_CTRL_TERMINATE
;
1045 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1047 static int __init
tpacpi_check_std_acpi_brightness_support(void)
1051 void *bcl_ptr
= &bcl_levels
;
1054 TPACPI_ACPIHANDLE_INIT(vid
);
1060 * Search for a _BCL method, and execute it. This is safe on all
1061 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1062 * BIOS in ACPI backlight control mode. We do NOT have to care
1063 * about calling the _BCL method in an enabled video device, any
1064 * will do for our purposes.
1067 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
1068 tpacpi_acpi_walk_find_bcl
, NULL
,
1071 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
1072 tp_features
.bright_acpimode
= 1;
1073 return (bcl_levels
- 2);
1079 static int __init
tpacpi_new_rfkill(const unsigned int id
,
1080 struct rfkill
**rfk
,
1081 const enum rfkill_type rfktype
,
1083 const bool set_default
,
1084 int (*toggle_radio
)(void *, enum rfkill_state
),
1085 int (*get_state
)(void *, enum rfkill_state
*))
1088 enum rfkill_state initial_state
= RFKILL_STATE_SOFT_BLOCKED
;
1090 res
= get_state(NULL
, &initial_state
);
1093 "failed to read initial state for %s, error %d; "
1094 "will turn radio off\n", name
, res
);
1095 } else if (set_default
) {
1096 /* try to set the initial state as the default for the rfkill
1097 * type, since we ask the firmware to preserve it across S5 in
1099 if (rfkill_set_default(rfktype
,
1100 (initial_state
== RFKILL_STATE_UNBLOCKED
) ?
1101 RFKILL_STATE_UNBLOCKED
:
1102 RFKILL_STATE_SOFT_BLOCKED
) == -EPERM
)
1103 vdbg_printk(TPACPI_DBG_RFKILL
,
1104 "Default state for %s cannot be changed\n",
1108 *rfk
= rfkill_allocate(&tpacpi_pdev
->dev
, rfktype
);
1111 "failed to allocate memory for rfkill class\n");
1115 (*rfk
)->name
= name
;
1116 (*rfk
)->get_state
= get_state
;
1117 (*rfk
)->toggle_radio
= toggle_radio
;
1118 (*rfk
)->state
= initial_state
;
1120 res
= rfkill_register(*rfk
);
1123 "failed to register %s rfkill switch: %d\n",
1133 static void printk_deprecated_attribute(const char * const what
,
1134 const char * const details
)
1136 tpacpi_log_usertask("deprecated sysfs attribute");
1137 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1138 "will be removed. %s\n",
1142 static void printk_deprecated_rfkill_attribute(const char * const what
)
1144 printk_deprecated_attribute(what
,
1145 "Please switch to generic rfkill before year 2010");
1148 /*************************************************************************
1149 * thinkpad-acpi driver attributes
1152 /* interface_version --------------------------------------------------- */
1153 static ssize_t
tpacpi_driver_interface_version_show(
1154 struct device_driver
*drv
,
1157 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1160 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1161 tpacpi_driver_interface_version_show
, NULL
);
1163 /* debug_level --------------------------------------------------------- */
1164 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1167 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1170 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1171 const char *buf
, size_t count
)
1175 if (parse_strtoul(buf
, 0xffff, &t
))
1183 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1184 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1186 /* version ------------------------------------------------------------- */
1187 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1190 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1191 TPACPI_DESC
, TPACPI_VERSION
);
1194 static DRIVER_ATTR(version
, S_IRUGO
,
1195 tpacpi_driver_version_show
, NULL
);
1197 /* --------------------------------------------------------------------- */
1199 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1201 static void tpacpi_send_radiosw_update(void);
1203 /* wlsw_emulstate ------------------------------------------------------ */
1204 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1207 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1210 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1211 const char *buf
, size_t count
)
1215 if (parse_strtoul(buf
, 1, &t
))
1218 if (tpacpi_wlsw_emulstate
!= t
) {
1219 tpacpi_wlsw_emulstate
= !!t
;
1220 tpacpi_send_radiosw_update();
1222 tpacpi_wlsw_emulstate
= !!t
;
1227 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1228 tpacpi_driver_wlsw_emulstate_show
,
1229 tpacpi_driver_wlsw_emulstate_store
);
1231 /* bluetooth_emulstate ------------------------------------------------- */
1232 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1233 struct device_driver
*drv
,
1236 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1239 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1240 struct device_driver
*drv
,
1241 const char *buf
, size_t count
)
1245 if (parse_strtoul(buf
, 1, &t
))
1248 tpacpi_bluetooth_emulstate
= !!t
;
1253 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1254 tpacpi_driver_bluetooth_emulstate_show
,
1255 tpacpi_driver_bluetooth_emulstate_store
);
1257 /* wwan_emulstate ------------------------------------------------- */
1258 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1259 struct device_driver
*drv
,
1262 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1265 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1266 struct device_driver
*drv
,
1267 const char *buf
, size_t count
)
1271 if (parse_strtoul(buf
, 1, &t
))
1274 tpacpi_wwan_emulstate
= !!t
;
1279 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1280 tpacpi_driver_wwan_emulstate_show
,
1281 tpacpi_driver_wwan_emulstate_store
);
1283 /* uwb_emulstate ------------------------------------------------- */
1284 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1285 struct device_driver
*drv
,
1288 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1291 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1292 struct device_driver
*drv
,
1293 const char *buf
, size_t count
)
1297 if (parse_strtoul(buf
, 1, &t
))
1300 tpacpi_uwb_emulstate
= !!t
;
1305 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1306 tpacpi_driver_uwb_emulstate_show
,
1307 tpacpi_driver_uwb_emulstate_store
);
1310 /* --------------------------------------------------------------------- */
1312 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1313 &driver_attr_debug_level
, &driver_attr_version
,
1314 &driver_attr_interface_version
,
1317 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1323 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1324 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1328 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1329 if (!res
&& dbg_wlswemul
)
1330 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1331 if (!res
&& dbg_bluetoothemul
)
1332 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1333 if (!res
&& dbg_wwanemul
)
1334 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1335 if (!res
&& dbg_uwbemul
)
1336 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1342 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1346 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1347 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1349 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1350 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1351 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1352 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1353 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1357 /****************************************************************************
1358 ****************************************************************************
1362 ****************************************************************************
1363 ****************************************************************************/
1365 /*************************************************************************
1366 * thinkpad-acpi init subdriver
1369 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1371 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1372 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1374 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1375 (thinkpad_id
.bios_version_str
) ?
1376 thinkpad_id
.bios_version_str
: "unknown",
1377 (thinkpad_id
.ec_version_str
) ?
1378 thinkpad_id
.ec_version_str
: "unknown");
1380 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1381 printk(TPACPI_INFO
"%s %s, model %s\n",
1382 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1383 "IBM" : ((thinkpad_id
.vendor
==
1384 PCI_VENDOR_ID_LENOVO
) ?
1385 "Lenovo" : "Unknown vendor"),
1386 thinkpad_id
.model_str
,
1387 (thinkpad_id
.nummodel_str
) ?
1388 thinkpad_id
.nummodel_str
: "unknown");
1393 static int thinkpad_acpi_driver_read(char *p
)
1397 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1398 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1403 static struct ibm_struct thinkpad_acpi_driver_data
= {
1405 .read
= thinkpad_acpi_driver_read
,
1408 /*************************************************************************
1412 enum { /* hot key scan codes (derived from ACPI DSDT) */
1413 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1414 TP_ACPI_HOTKEYSCAN_FNF2
,
1415 TP_ACPI_HOTKEYSCAN_FNF3
,
1416 TP_ACPI_HOTKEYSCAN_FNF4
,
1417 TP_ACPI_HOTKEYSCAN_FNF5
,
1418 TP_ACPI_HOTKEYSCAN_FNF6
,
1419 TP_ACPI_HOTKEYSCAN_FNF7
,
1420 TP_ACPI_HOTKEYSCAN_FNF8
,
1421 TP_ACPI_HOTKEYSCAN_FNF9
,
1422 TP_ACPI_HOTKEYSCAN_FNF10
,
1423 TP_ACPI_HOTKEYSCAN_FNF11
,
1424 TP_ACPI_HOTKEYSCAN_FNF12
,
1425 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1426 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1427 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1428 TP_ACPI_HOTKEYSCAN_FNHOME
,
1429 TP_ACPI_HOTKEYSCAN_FNEND
,
1430 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1431 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1432 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1433 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1434 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1435 TP_ACPI_HOTKEYSCAN_MUTE
,
1436 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1439 enum { /* Keys available through NVRAM polling */
1440 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1441 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1444 enum { /* Positions of some of the keys in hotkey masks */
1445 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1446 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1447 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1448 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1449 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1450 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1451 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1452 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1453 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1454 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1455 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1458 enum { /* NVRAM to ACPI HKEY group map */
1459 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1460 TP_ACPI_HKEY_ZOOM_MASK
|
1461 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1462 TP_ACPI_HKEY_HIBERNATE_MASK
,
1463 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1464 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1465 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1466 TP_ACPI_HKEY_VOLDWN_MASK
|
1467 TP_ACPI_HKEY_MUTE_MASK
,
1470 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1471 struct tp_nvram_state
{
1472 u16 thinkpad_toggle
:1;
1474 u16 display_toggle
:1;
1475 u16 thinklight_toggle
:1;
1476 u16 hibernate_toggle
:1;
1477 u16 displayexp_toggle
:1;
1478 u16 display_state
:1;
1479 u16 brightness_toggle
:1;
1480 u16 volume_toggle
:1;
1483 u8 brightness_level
;
1487 static struct task_struct
*tpacpi_hotkey_task
;
1488 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1489 static int hotkey_poll_freq
= 10; /* Hz */
1490 static struct mutex hotkey_thread_mutex
;
1491 static struct mutex hotkey_thread_data_mutex
;
1492 static unsigned int hotkey_config_change
;
1494 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1496 #define hotkey_source_mask 0U
1498 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1500 static struct mutex hotkey_mutex
;
1502 static enum { /* Reasons for waking up */
1503 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1504 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1505 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1506 } hotkey_wakeup_reason
;
1508 static int hotkey_autosleep_ack
;
1510 static u32 hotkey_orig_mask
;
1511 static u32 hotkey_all_mask
;
1512 static u32 hotkey_reserved_mask
;
1513 static u32 hotkey_mask
;
1515 static unsigned int hotkey_report_mode
;
1517 static u16
*hotkey_keycode_map
;
1519 static struct attribute_set
*hotkey_dev_attributes
;
1521 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1522 #define HOTKEY_CONFIG_CRITICAL_START \
1524 mutex_lock(&hotkey_thread_data_mutex); \
1525 hotkey_config_change++; \
1527 #define HOTKEY_CONFIG_CRITICAL_END \
1528 mutex_unlock(&hotkey_thread_data_mutex);
1530 #define HOTKEY_CONFIG_CRITICAL_START
1531 #define HOTKEY_CONFIG_CRITICAL_END
1532 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1534 /* HKEY.MHKG() return bits */
1535 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1537 static int hotkey_get_wlsw(int *status
)
1539 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1541 *status
= !!tpacpi_wlsw_emulstate
;
1545 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1550 static int hotkey_get_tablet_mode(int *status
)
1554 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1557 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1562 * Call with hotkey_mutex held
1564 static int hotkey_mask_get(void)
1568 if (tp_features
.hotkey_mask
) {
1569 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1572 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1578 * Call with hotkey_mutex held
1580 static int hotkey_mask_set(u32 mask
)
1585 if (tp_features
.hotkey_mask
) {
1586 if (!tp_warned
.hotkey_mask_ff
&&
1587 (mask
== 0xffff || mask
== 0xffffff ||
1588 mask
== 0xffffffff)) {
1589 tp_warned
.hotkey_mask_ff
= 1;
1590 printk(TPACPI_NOTICE
1591 "setting the hotkey mask to 0x%08x is likely "
1592 "not the best way to go about it\n", mask
);
1593 printk(TPACPI_NOTICE
1594 "please consider using the driver defaults, "
1595 "and refer to up-to-date thinkpad-acpi "
1599 HOTKEY_CONFIG_CRITICAL_START
1600 for (i
= 0; i
< 32; i
++) {
1602 /* enable in firmware mask only keys not in NVRAM
1603 * mode, but enable the key in the cached hotkey_mask
1604 * regardless of mode, or the key will end up
1605 * disabled by hotkey_mask_get() */
1606 if (!acpi_evalf(hkey_handle
,
1607 NULL
, "MHKM", "vdd", i
+ 1,
1608 !!((mask
& ~hotkey_source_mask
) & m
))) {
1612 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1615 HOTKEY_CONFIG_CRITICAL_END
1617 /* hotkey_mask_get must be called unconditionally below */
1618 if (!hotkey_mask_get() && !rc
&&
1619 (hotkey_mask
& ~hotkey_source_mask
) !=
1620 (mask
& ~hotkey_source_mask
)) {
1621 printk(TPACPI_NOTICE
1622 "requested hot key mask 0x%08x, but "
1623 "firmware forced it to 0x%08x\n",
1627 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1628 HOTKEY_CONFIG_CRITICAL_START
1629 hotkey_mask
= mask
& hotkey_source_mask
;
1630 HOTKEY_CONFIG_CRITICAL_END
1632 if (hotkey_mask
!= mask
) {
1633 printk(TPACPI_NOTICE
1634 "requested hot key mask 0x%08x, "
1635 "forced to 0x%08x (NVRAM poll mask is "
1636 "0x%08x): no firmware mask support\n",
1637 mask
, hotkey_mask
, hotkey_source_mask
);
1642 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1648 static int hotkey_status_get(int *status
)
1650 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1656 static int hotkey_status_set(bool enable
)
1658 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
1664 static void tpacpi_input_send_tabletsw(void)
1668 if (tp_features
.hotkey_tablet
&&
1669 !hotkey_get_tablet_mode(&state
)) {
1670 mutex_lock(&tpacpi_inputdev_send_mutex
);
1672 input_report_switch(tpacpi_inputdev
,
1673 SW_TABLET_MODE
, !!state
);
1674 input_sync(tpacpi_inputdev
);
1676 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1680 static void tpacpi_input_send_key(unsigned int scancode
)
1682 unsigned int keycode
;
1684 keycode
= hotkey_keycode_map
[scancode
];
1686 if (keycode
!= KEY_RESERVED
) {
1687 mutex_lock(&tpacpi_inputdev_send_mutex
);
1689 input_report_key(tpacpi_inputdev
, keycode
, 1);
1690 if (keycode
== KEY_UNKNOWN
)
1691 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1693 input_sync(tpacpi_inputdev
);
1695 input_report_key(tpacpi_inputdev
, keycode
, 0);
1696 if (keycode
== KEY_UNKNOWN
)
1697 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1699 input_sync(tpacpi_inputdev
);
1701 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1705 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1706 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1708 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1710 tpacpi_input_send_key(scancode
);
1711 if (hotkey_report_mode
< 2) {
1712 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1713 0x80, 0x1001 + scancode
);
1717 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1721 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1722 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1723 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1724 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1725 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1726 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1728 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1729 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1730 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1732 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1733 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1734 n
->displayexp_toggle
=
1735 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1737 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1738 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1739 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1740 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1741 n
->brightness_toggle
=
1742 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1744 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1745 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1746 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1747 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1748 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1749 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1753 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1755 if ((mask & (1 << __scancode)) && \
1756 oldn->__member != newn->__member) \
1757 tpacpi_hotkey_send_key(__scancode); \
1760 #define TPACPI_MAY_SEND_KEY(__scancode) \
1761 do { if (mask & (1 << __scancode)) \
1762 tpacpi_hotkey_send_key(__scancode); } while (0)
1764 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1765 struct tp_nvram_state
*newn
,
1768 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1769 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1770 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1771 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1773 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1775 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1778 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1779 if (oldn
->mute
!= newn
->mute
) {
1780 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1782 if (oldn
->volume_level
> newn
->volume_level
) {
1783 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1784 } else if (oldn
->volume_level
< newn
->volume_level
) {
1785 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1786 } else if (oldn
->mute
== newn
->mute
) {
1787 /* repeated key presses that didn't change state */
1789 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1790 } else if (newn
->volume_level
!= 0) {
1791 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1793 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1798 /* handle brightness */
1799 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1800 if (oldn
->brightness_level
< newn
->brightness_level
) {
1801 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1802 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1803 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1805 /* repeated key presses that didn't change state */
1806 if (newn
->brightness_level
!= 0) {
1807 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1809 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1815 #undef TPACPI_COMPARE_KEY
1816 #undef TPACPI_MAY_SEND_KEY
1818 static int hotkey_kthread(void *data
)
1820 struct tp_nvram_state s
[2];
1822 unsigned int si
, so
;
1824 unsigned int change_detector
, must_reset
;
1826 mutex_lock(&hotkey_thread_mutex
);
1828 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1837 /* Initial state for compares */
1838 mutex_lock(&hotkey_thread_data_mutex
);
1839 change_detector
= hotkey_config_change
;
1840 mask
= hotkey_source_mask
& hotkey_mask
;
1841 mutex_unlock(&hotkey_thread_data_mutex
);
1842 hotkey_read_nvram(&s
[so
], mask
);
1844 while (!kthread_should_stop() && hotkey_poll_freq
) {
1846 t
= 1000/hotkey_poll_freq
;
1847 t
= msleep_interruptible(t
);
1848 if (unlikely(kthread_should_stop()))
1850 must_reset
= try_to_freeze();
1851 if (t
> 0 && !must_reset
)
1854 mutex_lock(&hotkey_thread_data_mutex
);
1855 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1856 /* forget old state on thaw or config change */
1859 change_detector
= hotkey_config_change
;
1861 mask
= hotkey_source_mask
& hotkey_mask
;
1862 mutex_unlock(&hotkey_thread_data_mutex
);
1865 hotkey_read_nvram(&s
[si
], mask
);
1866 if (likely(si
!= so
)) {
1867 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1877 mutex_unlock(&hotkey_thread_mutex
);
1881 static void hotkey_poll_stop_sync(void)
1883 if (tpacpi_hotkey_task
) {
1884 if (frozen(tpacpi_hotkey_task
) ||
1885 freezing(tpacpi_hotkey_task
))
1886 thaw_process(tpacpi_hotkey_task
);
1888 kthread_stop(tpacpi_hotkey_task
);
1889 tpacpi_hotkey_task
= NULL
;
1890 mutex_lock(&hotkey_thread_mutex
);
1891 /* at this point, the thread did exit */
1892 mutex_unlock(&hotkey_thread_mutex
);
1896 /* call with hotkey_mutex held */
1897 static void hotkey_poll_setup(int may_warn
)
1899 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1900 hotkey_poll_freq
> 0 &&
1901 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1902 if (!tpacpi_hotkey_task
) {
1903 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1904 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
1905 if (IS_ERR(tpacpi_hotkey_task
)) {
1906 tpacpi_hotkey_task
= NULL
;
1908 "could not create kernel thread "
1909 "for hotkey polling\n");
1913 hotkey_poll_stop_sync();
1915 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1916 printk(TPACPI_NOTICE
1917 "hot keys 0x%08x require polling, "
1918 "which is currently disabled\n",
1919 hotkey_source_mask
);
1924 static void hotkey_poll_setup_safe(int may_warn
)
1926 mutex_lock(&hotkey_mutex
);
1927 hotkey_poll_setup(may_warn
);
1928 mutex_unlock(&hotkey_mutex
);
1931 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1933 static void hotkey_poll_setup_safe(int __unused
)
1937 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1939 static int hotkey_inputdev_open(struct input_dev
*dev
)
1941 switch (tpacpi_lifecycle
) {
1942 case TPACPI_LIFE_INIT
:
1944 * hotkey_init will call hotkey_poll_setup_safe
1945 * at the appropriate moment
1948 case TPACPI_LIFE_EXITING
:
1950 case TPACPI_LIFE_RUNNING
:
1951 hotkey_poll_setup_safe(0);
1955 /* Should only happen if tpacpi_lifecycle is corrupt */
1960 static void hotkey_inputdev_close(struct input_dev
*dev
)
1962 /* disable hotkey polling when possible */
1963 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1964 hotkey_poll_setup_safe(0);
1967 /* sysfs hotkey enable ------------------------------------------------- */
1968 static ssize_t
hotkey_enable_show(struct device
*dev
,
1969 struct device_attribute
*attr
,
1974 printk_deprecated_attribute("hotkey_enable",
1975 "Hotkey reporting is always enabled");
1977 res
= hotkey_status_get(&status
);
1981 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1984 static ssize_t
hotkey_enable_store(struct device
*dev
,
1985 struct device_attribute
*attr
,
1986 const char *buf
, size_t count
)
1990 printk_deprecated_attribute("hotkey_enable",
1991 "Hotkeys can be disabled through hotkey_mask");
1993 if (parse_strtoul(buf
, 1, &t
))
2002 static struct device_attribute dev_attr_hotkey_enable
=
2003 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
2004 hotkey_enable_show
, hotkey_enable_store
);
2006 /* sysfs hotkey mask --------------------------------------------------- */
2007 static ssize_t
hotkey_mask_show(struct device
*dev
,
2008 struct device_attribute
*attr
,
2013 if (mutex_lock_killable(&hotkey_mutex
))
2014 return -ERESTARTSYS
;
2015 res
= hotkey_mask_get();
2016 mutex_unlock(&hotkey_mutex
);
2019 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
2022 static ssize_t
hotkey_mask_store(struct device
*dev
,
2023 struct device_attribute
*attr
,
2024 const char *buf
, size_t count
)
2029 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2032 if (mutex_lock_killable(&hotkey_mutex
))
2033 return -ERESTARTSYS
;
2035 res
= hotkey_mask_set(t
);
2037 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2038 hotkey_poll_setup(1);
2041 mutex_unlock(&hotkey_mutex
);
2043 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2045 return (res
) ? res
: count
;
2048 static struct device_attribute dev_attr_hotkey_mask
=
2049 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
2050 hotkey_mask_show
, hotkey_mask_store
);
2052 /* sysfs hotkey bios_enabled ------------------------------------------- */
2053 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2054 struct device_attribute
*attr
,
2057 return sprintf(buf
, "0\n");
2060 static struct device_attribute dev_attr_hotkey_bios_enabled
=
2061 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
2063 /* sysfs hotkey bios_mask ---------------------------------------------- */
2064 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2065 struct device_attribute
*attr
,
2068 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2071 static struct device_attribute dev_attr_hotkey_bios_mask
=
2072 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
2074 /* sysfs hotkey all_mask ----------------------------------------------- */
2075 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2076 struct device_attribute
*attr
,
2079 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2080 hotkey_all_mask
| hotkey_source_mask
);
2083 static struct device_attribute dev_attr_hotkey_all_mask
=
2084 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2086 /* sysfs hotkey recommended_mask --------------------------------------- */
2087 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2088 struct device_attribute
*attr
,
2091 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2092 (hotkey_all_mask
| hotkey_source_mask
)
2093 & ~hotkey_reserved_mask
);
2096 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2097 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2098 hotkey_recommended_mask_show
, NULL
);
2100 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2102 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2103 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2104 struct device_attribute
*attr
,
2107 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2110 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2111 struct device_attribute
*attr
,
2112 const char *buf
, size_t count
)
2116 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2117 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2120 if (mutex_lock_killable(&hotkey_mutex
))
2121 return -ERESTARTSYS
;
2123 HOTKEY_CONFIG_CRITICAL_START
2124 hotkey_source_mask
= t
;
2125 HOTKEY_CONFIG_CRITICAL_END
2127 hotkey_poll_setup(1);
2129 mutex_unlock(&hotkey_mutex
);
2131 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2136 static struct device_attribute dev_attr_hotkey_source_mask
=
2137 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2138 hotkey_source_mask_show
, hotkey_source_mask_store
);
2140 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2141 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2142 struct device_attribute
*attr
,
2145 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2148 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2149 struct device_attribute
*attr
,
2150 const char *buf
, size_t count
)
2154 if (parse_strtoul(buf
, 25, &t
))
2157 if (mutex_lock_killable(&hotkey_mutex
))
2158 return -ERESTARTSYS
;
2160 hotkey_poll_freq
= t
;
2162 hotkey_poll_setup(1);
2163 mutex_unlock(&hotkey_mutex
);
2165 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2170 static struct device_attribute dev_attr_hotkey_poll_freq
=
2171 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2172 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2174 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2176 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2177 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2178 struct device_attribute
*attr
,
2182 res
= hotkey_get_wlsw(&s
);
2186 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2189 static struct device_attribute dev_attr_hotkey_radio_sw
=
2190 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2192 static void hotkey_radio_sw_notify_change(void)
2194 if (tp_features
.hotkey_wlsw
)
2195 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2199 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2200 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2201 struct device_attribute
*attr
,
2205 res
= hotkey_get_tablet_mode(&s
);
2209 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2212 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2213 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2215 static void hotkey_tablet_mode_notify_change(void)
2217 if (tp_features
.hotkey_tablet
)
2218 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2219 "hotkey_tablet_mode");
2222 /* sysfs hotkey report_mode -------------------------------------------- */
2223 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2224 struct device_attribute
*attr
,
2227 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2228 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2231 static struct device_attribute dev_attr_hotkey_report_mode
=
2232 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2234 /* sysfs wakeup reason (pollable) -------------------------------------- */
2235 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2236 struct device_attribute
*attr
,
2239 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2242 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2243 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2245 static void hotkey_wakeup_reason_notify_change(void)
2247 if (tp_features
.hotkey_mask
)
2248 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2252 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2253 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2254 struct device_attribute
*attr
,
2257 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2260 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2261 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2262 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2264 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2266 if (tp_features
.hotkey_mask
)
2267 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2268 "wakeup_hotunplug_complete");
2271 /* --------------------------------------------------------------------- */
2273 static struct attribute
*hotkey_attributes
[] __initdata
= {
2274 &dev_attr_hotkey_enable
.attr
,
2275 &dev_attr_hotkey_bios_enabled
.attr
,
2276 &dev_attr_hotkey_report_mode
.attr
,
2277 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2278 &dev_attr_hotkey_mask
.attr
,
2279 &dev_attr_hotkey_all_mask
.attr
,
2280 &dev_attr_hotkey_recommended_mask
.attr
,
2281 &dev_attr_hotkey_source_mask
.attr
,
2282 &dev_attr_hotkey_poll_freq
.attr
,
2286 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
2287 &dev_attr_hotkey_bios_mask
.attr
,
2288 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2289 &dev_attr_hotkey_mask
.attr
,
2290 &dev_attr_hotkey_all_mask
.attr
,
2291 &dev_attr_hotkey_recommended_mask
.attr
,
2293 &dev_attr_hotkey_wakeup_reason
.attr
,
2294 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2297 static void bluetooth_update_rfk(void);
2298 static void wan_update_rfk(void);
2299 static void uwb_update_rfk(void);
2300 static void tpacpi_send_radiosw_update(void)
2304 /* Sync these BEFORE sending any rfkill events */
2305 if (tp_features
.bluetooth
)
2306 bluetooth_update_rfk();
2307 if (tp_features
.wan
)
2309 if (tp_features
.uwb
)
2312 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
2313 mutex_lock(&tpacpi_inputdev_send_mutex
);
2315 input_report_switch(tpacpi_inputdev
,
2316 SW_RFKILL_ALL
, !!wlsw
);
2317 input_sync(tpacpi_inputdev
);
2319 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2321 hotkey_radio_sw_notify_change();
2324 static void hotkey_exit(void)
2326 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2327 hotkey_poll_stop_sync();
2330 if (hotkey_dev_attributes
)
2331 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2333 kfree(hotkey_keycode_map
);
2335 if (tp_features
.hotkey
) {
2336 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
2337 "restoring original hot key mask\n");
2338 /* no short-circuit boolean operator below! */
2339 if ((hotkey_mask_set(hotkey_orig_mask
) |
2340 hotkey_status_set(false)) != 0)
2342 "failed to restore hot key mask "
2343 "to BIOS defaults\n");
2347 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2349 /* Requirements for changing the default keymaps:
2351 * 1. Many of the keys are mapped to KEY_RESERVED for very
2352 * good reasons. Do not change them unless you have deep
2353 * knowledge on the IBM and Lenovo ThinkPad firmware for
2354 * the various ThinkPad models. The driver behaves
2355 * differently for KEY_RESERVED: such keys have their
2356 * hot key mask *unset* in mask_recommended, and also
2357 * in the initial hot key mask programmed into the
2358 * firmware at driver load time, which means the firm-
2359 * ware may react very differently if you change them to
2362 * 2. You must be subscribed to the linux-thinkpad and
2363 * ibm-acpi-devel mailing lists, and you should read the
2364 * list archives since 2007 if you want to change the
2365 * keymaps. This requirement exists so that you will
2366 * know the past history of problems with the thinkpad-
2367 * acpi driver keymaps, and also that you will be
2368 * listening to any bug reports;
2370 * 3. Do not send thinkpad-acpi specific patches directly to
2371 * for merging, *ever*. Send them to the linux-acpi
2372 * mailinglist for comments. Merging is to be done only
2373 * through acpi-test and the ACPI maintainer.
2375 * If the above is too much to ask, don't change the keymap.
2376 * Ask the thinkpad-acpi maintainer to do it, instead.
2378 static u16 ibm_keycode_map
[] __initdata
= {
2379 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2380 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2381 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2382 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2384 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2385 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2386 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2387 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2389 /* brightness: firmware always reacts to them, unless
2390 * X.org did some tricks in the radeon BIOS scratch
2391 * registers of *some* models */
2392 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2393 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2395 /* Thinklight: firmware always react to it */
2396 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2398 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2399 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2401 /* Volume: firmware always react to it and reprograms
2402 * the built-in *extra* mixer. Never map it to control
2403 * another mixer by default. */
2404 KEY_RESERVED
, /* 0x14: VOLUME UP */
2405 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2406 KEY_RESERVED
, /* 0x16: MUTE */
2408 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2410 /* (assignments unknown, please report if found) */
2411 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2412 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2414 static u16 lenovo_keycode_map
[] __initdata
= {
2415 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2416 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2417 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2418 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2420 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2421 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2422 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2423 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2425 /* These either have to go through ACPI video, or
2426 * act like in the IBM ThinkPads, so don't ever
2427 * enable them by default */
2428 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2429 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2431 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2433 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2434 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2436 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2437 * react to it and reprograms the built-in *extra* mixer.
2438 * Never map it to control another mixer by default.
2440 * T60?, T61, R60?, R61: firmware and EC tries to send
2441 * these over the regular keyboard, so these are no-ops,
2442 * but there are still weird bugs re. MUTE, so do not
2443 * change unless you get test reports from all Lenovo
2444 * models. May cause the BIOS to interfere with the
2447 KEY_RESERVED
, /* 0x14: VOLUME UP */
2448 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2449 KEY_RESERVED
, /* 0x16: MUTE */
2451 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2453 /* (assignments unknown, please report if found) */
2454 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2455 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2458 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2459 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2460 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2466 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2467 "initializing hotkey subdriver\n");
2469 BUG_ON(!tpacpi_inputdev
);
2470 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2471 tpacpi_inputdev
->close
!= NULL
);
2473 TPACPI_ACPIHANDLE_INIT(hkey
);
2474 mutex_init(&hotkey_mutex
);
2476 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2477 mutex_init(&hotkey_thread_mutex
);
2478 mutex_init(&hotkey_thread_data_mutex
);
2481 /* hotkey not supported on 570 */
2482 tp_features
.hotkey
= hkey_handle
!= NULL
;
2484 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2486 str_supported(tp_features
.hotkey
));
2488 if (!tp_features
.hotkey
)
2491 tpacpi_disable_brightness_delay();
2493 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2494 if (!hotkey_dev_attributes
)
2496 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2498 ARRAY_SIZE(hotkey_attributes
));
2502 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2503 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2504 for HKEY interface version 0x100 */
2505 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2506 if ((hkeyv
>> 8) != 1) {
2507 printk(TPACPI_ERR
"unknown version of the "
2508 "HKEY interface: 0x%x\n", hkeyv
);
2509 printk(TPACPI_ERR
"please report this to %s\n",
2513 * MHKV 0x100 in A31, R40, R40e,
2514 * T4x, X31, and later
2516 tp_features
.hotkey_mask
= 1;
2517 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2518 "firmware HKEY interface version: 0x%x\n",
2523 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2524 "hotkey masks are %s\n",
2525 str_supported(tp_features
.hotkey_mask
));
2527 if (tp_features
.hotkey_mask
) {
2528 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2531 "missing MHKA handler, "
2532 "please report this to %s\n",
2534 /* FN+F12, FN+F4, FN+F3 */
2535 hotkey_all_mask
= 0x080cU
;
2539 /* hotkey_source_mask *must* be zero for
2540 * the first hotkey_mask_get */
2541 if (tp_features
.hotkey_mask
) {
2542 res
= hotkey_mask_get();
2546 hotkey_orig_mask
= hotkey_mask
;
2547 res
= add_many_to_attr_set(
2548 hotkey_dev_attributes
,
2549 hotkey_mask_attributes
,
2550 ARRAY_SIZE(hotkey_mask_attributes
));
2555 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2556 if (tp_features
.hotkey_mask
) {
2557 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2560 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2563 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2564 "hotkey source mask 0x%08x, polling freq %d\n",
2565 hotkey_source_mask
, hotkey_poll_freq
);
2568 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2570 tp_features
.hotkey_wlsw
= 1;
2572 "radio switch emulation enabled\n");
2575 /* Not all thinkpads have a hardware radio switch */
2576 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2577 tp_features
.hotkey_wlsw
= 1;
2579 "radio switch found; radios are %s\n",
2580 enabled(status
, 0));
2582 if (tp_features
.hotkey_wlsw
)
2583 res
= add_to_attr_set(hotkey_dev_attributes
,
2584 &dev_attr_hotkey_radio_sw
.attr
);
2586 /* For X41t, X60t, X61t Tablets... */
2587 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2588 tp_features
.hotkey_tablet
= 1;
2590 "possible tablet mode switch found; "
2591 "ThinkPad in %s mode\n",
2592 (status
& TP_HOTKEY_TABLET_MASK
)?
2593 "tablet" : "laptop");
2594 res
= add_to_attr_set(hotkey_dev_attributes
,
2595 &dev_attr_hotkey_tablet_mode
.attr
);
2599 res
= register_attr_set_with_sysfs(
2600 hotkey_dev_attributes
,
2601 &tpacpi_pdev
->dev
.kobj
);
2605 /* Set up key map */
2607 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2609 if (!hotkey_keycode_map
) {
2611 "failed to allocate memory for key map\n");
2616 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2617 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2618 "using Lenovo default hot key map\n");
2619 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2620 TPACPI_HOTKEY_MAP_SIZE
);
2622 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2623 "using IBM default hot key map\n");
2624 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2625 TPACPI_HOTKEY_MAP_SIZE
);
2628 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2629 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2630 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2631 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2632 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2633 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2634 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2635 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2636 set_bit(hotkey_keycode_map
[i
],
2637 tpacpi_inputdev
->keybit
);
2639 if (i
< sizeof(hotkey_reserved_mask
)*8)
2640 hotkey_reserved_mask
|= 1 << i
;
2644 if (tp_features
.hotkey_wlsw
) {
2645 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2646 set_bit(SW_RFKILL_ALL
, tpacpi_inputdev
->swbit
);
2648 if (tp_features
.hotkey_tablet
) {
2649 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2650 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2653 /* Do not issue duplicate brightness change events to
2655 if (!tp_features
.bright_acpimode
)
2656 /* update bright_acpimode... */
2657 tpacpi_check_std_acpi_brightness_support();
2659 if (tp_features
.bright_acpimode
&& acpi_video_backlight_support()) {
2661 "This ThinkPad has standard ACPI backlight "
2662 "brightness control, supported by the ACPI "
2664 printk(TPACPI_NOTICE
2665 "Disabling thinkpad-acpi brightness events "
2668 /* The hotkey_reserved_mask change below is not
2669 * necessary while the keys are at KEY_RESERVED in the
2670 * default map, but better safe than sorry, leave it
2671 * here as a marker of what we have to do, especially
2672 * when we finally become able to set this at runtime
2673 * on response to X.org requests */
2674 hotkey_reserved_mask
|=
2675 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
2676 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
2679 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2680 "enabling firmware HKEY event interface...\n");
2681 res
= hotkey_status_set(true);
2686 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2687 & ~hotkey_reserved_mask
)
2688 | hotkey_orig_mask
);
2689 if (res
< 0 && res
!= -ENXIO
) {
2694 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2695 "legacy ibm/hotkey event reporting over procfs %s\n",
2696 (hotkey_report_mode
< 2) ?
2697 "enabled" : "disabled");
2699 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2700 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2702 hotkey_poll_setup_safe(1);
2703 tpacpi_send_radiosw_update();
2704 tpacpi_input_send_tabletsw();
2709 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2710 hotkey_dev_attributes
= NULL
;
2712 return (res
< 0)? res
: 1;
2715 static bool hotkey_notify_hotkey(const u32 hkey
,
2717 bool *ignore_acpi_ev
)
2719 /* 0x1000-0x1FFF: key presses */
2720 unsigned int scancode
= hkey
& 0xfff;
2721 *send_acpi_ev
= true;
2722 *ignore_acpi_ev
= false;
2724 if (scancode
> 0 && scancode
< 0x21) {
2726 if (!(hotkey_source_mask
& (1 << scancode
))) {
2727 tpacpi_input_send_key(scancode
);
2728 *send_acpi_ev
= false;
2730 *ignore_acpi_ev
= true;
2737 static bool hotkey_notify_wakeup(const u32 hkey
,
2739 bool *ignore_acpi_ev
)
2741 /* 0x2000-0x2FFF: Wakeup reason */
2742 *send_acpi_ev
= true;
2743 *ignore_acpi_ev
= false;
2746 case 0x2304: /* suspend, undock */
2747 case 0x2404: /* hibernation, undock */
2748 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2749 *ignore_acpi_ev
= true;
2752 case 0x2305: /* suspend, bay eject */
2753 case 0x2405: /* hibernation, bay eject */
2754 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2755 *ignore_acpi_ev
= true;
2758 case 0x2313: /* Battery on critical low level (S3) */
2759 case 0x2413: /* Battery on critical low level (S4) */
2761 "EMERGENCY WAKEUP: battery almost empty\n");
2762 /* how to auto-heal: */
2763 /* 2313: woke up from S3, go to S4/S5 */
2764 /* 2413: woke up from S4, go to S5 */
2771 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2773 "woke up due to a hot-unplug "
2775 hotkey_wakeup_reason_notify_change();
2780 static bool hotkey_notify_usrevent(const u32 hkey
,
2782 bool *ignore_acpi_ev
)
2784 /* 0x5000-0x5FFF: human interface helpers */
2785 *send_acpi_ev
= true;
2786 *ignore_acpi_ev
= false;
2789 case 0x5010: /* Lenovo new BIOS: brightness changed */
2790 case 0x500b: /* X61t: tablet pen inserted into bay */
2791 case 0x500c: /* X61t: tablet pen removed from bay */
2794 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2795 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2796 tpacpi_input_send_tabletsw();
2797 hotkey_tablet_mode_notify_change();
2798 *send_acpi_ev
= false;
2803 /* LID switch events. Do not propagate */
2804 *ignore_acpi_ev
= true;
2812 static bool hotkey_notify_thermal(const u32 hkey
,
2814 bool *ignore_acpi_ev
)
2816 /* 0x6000-0x6FFF: thermal alarms */
2817 *send_acpi_ev
= true;
2818 *ignore_acpi_ev
= false;
2823 "THERMAL ALARM: battery is too hot!\n");
2824 /* recommended action: warn user through gui */
2828 "THERMAL EMERGENCY: battery is extremely hot!\n");
2829 /* recommended action: immediate sleep/hibernate */
2834 "a sensor reports something is too hot!\n");
2835 /* recommended action: warn user through gui, that */
2836 /* some internal component is too hot */
2840 "THERMAL EMERGENCY: "
2841 "a sensor reports something is extremely hot!\n");
2842 /* recommended action: immediate sleep/hibernate */
2846 "EC reports that Thermal Table has changed\n");
2847 /* recommended action: do nothing, we don't have
2848 * Lenovo ATM information */
2852 "THERMAL ALERT: unknown thermal alarm received\n");
2857 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2861 bool ignore_acpi_ev
;
2864 if (event
!= 0x80) {
2866 "unknown HKEY notification event %d\n", event
);
2867 /* forward it to userspace, maybe it knows how to handle it */
2868 acpi_bus_generate_netlink_event(
2869 ibm
->acpi
->device
->pnp
.device_class
,
2870 dev_name(&ibm
->acpi
->device
->dev
),
2876 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2877 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2886 send_acpi_ev
= true;
2887 ignore_acpi_ev
= false;
2889 switch (hkey
>> 12) {
2891 /* 0x1000-0x1FFF: key presses */
2892 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
2896 /* 0x2000-0x2FFF: Wakeup reason */
2897 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
2901 /* 0x3000-0x3FFF: bay-related wakeups */
2902 if (hkey
== 0x3003) {
2903 hotkey_autosleep_ack
= 1;
2906 hotkey_wakeup_hotunplug_complete_notify_change();
2913 /* 0x4000-0x4FFF: dock-related wakeups */
2914 if (hkey
== 0x4003) {
2915 hotkey_autosleep_ack
= 1;
2918 hotkey_wakeup_hotunplug_complete_notify_change();
2925 /* 0x5000-0x5FFF: human interface helpers */
2926 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
2930 /* 0x6000-0x6FFF: thermal alarms */
2931 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
2935 /* 0x7000-0x7FFF: misc */
2936 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2937 tpacpi_send_radiosw_update();
2942 /* fallthrough to default */
2947 printk(TPACPI_NOTICE
2948 "unhandled HKEY event 0x%04x\n", hkey
);
2949 printk(TPACPI_NOTICE
2950 "please report the conditions when this "
2951 "event happened to %s\n", TPACPI_MAIL
);
2955 if (!ignore_acpi_ev
&&
2956 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2957 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2961 /* netlink events */
2962 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2963 acpi_bus_generate_netlink_event(
2964 ibm
->acpi
->device
->pnp
.device_class
,
2965 dev_name(&ibm
->acpi
->device
->dev
),
2971 static void hotkey_suspend(pm_message_t state
)
2973 /* Do these on suspend, we get the events on early resume! */
2974 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2975 hotkey_autosleep_ack
= 0;
2978 static void hotkey_resume(void)
2980 tpacpi_disable_brightness_delay();
2982 if (hotkey_mask_get())
2984 "error while trying to read hot key mask "
2986 tpacpi_send_radiosw_update();
2987 hotkey_tablet_mode_notify_change();
2988 hotkey_wakeup_reason_notify_change();
2989 hotkey_wakeup_hotunplug_complete_notify_change();
2990 hotkey_poll_setup_safe(0);
2993 /* procfs -------------------------------------------------------------- */
2994 static int hotkey_read(char *p
)
2999 if (!tp_features
.hotkey
) {
3000 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3004 if (mutex_lock_killable(&hotkey_mutex
))
3005 return -ERESTARTSYS
;
3006 res
= hotkey_status_get(&status
);
3008 res
= hotkey_mask_get();
3009 mutex_unlock(&hotkey_mutex
);
3013 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
3014 if (tp_features
.hotkey_mask
) {
3015 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
3016 len
+= sprintf(p
+ len
,
3017 "commands:\tenable, disable, reset, <mask>\n");
3019 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
3020 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
3026 static void hotkey_enabledisable_warn(bool enable
)
3028 tpacpi_log_usertask("procfs hotkey enable/disable");
3029 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
3031 "hotkey enable/disable functionality has been "
3032 "removed from the driver. Hotkeys are always "
3035 "Please remove the hotkey=enable module "
3036 "parameter, it is deprecated. Hotkeys are always "
3040 static int hotkey_write(char *buf
)
3046 if (!tp_features
.hotkey
)
3049 if (mutex_lock_killable(&hotkey_mutex
))
3050 return -ERESTARTSYS
;
3055 while ((cmd
= next_cmd(&buf
))) {
3056 if (strlencmp(cmd
, "enable") == 0) {
3057 hotkey_enabledisable_warn(1);
3058 } else if (strlencmp(cmd
, "disable") == 0) {
3059 hotkey_enabledisable_warn(0);
3061 } else if (strlencmp(cmd
, "reset") == 0) {
3062 mask
= hotkey_orig_mask
;
3063 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
3065 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
3074 tpacpi_disclose_usertask("procfs hotkey",
3075 "set mask to 0x%08x\n", mask
);
3077 if (!res
&& mask
!= hotkey_mask
)
3078 res
= hotkey_mask_set(mask
);
3081 mutex_unlock(&hotkey_mutex
);
3085 static const struct acpi_device_id ibm_htk_device_ids
[] = {
3086 {TPACPI_ACPI_HKEY_HID
, 0},
3090 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
3091 .hid
= ibm_htk_device_ids
,
3092 .notify
= hotkey_notify
,
3093 .handle
= &hkey_handle
,
3094 .type
= ACPI_DEVICE_NOTIFY
,
3097 static struct ibm_struct hotkey_driver_data
= {
3099 .read
= hotkey_read
,
3100 .write
= hotkey_write
,
3101 .exit
= hotkey_exit
,
3102 .resume
= hotkey_resume
,
3103 .suspend
= hotkey_suspend
,
3104 .acpi
= &ibm_hotkey_acpidriver
,
3107 /*************************************************************************
3108 * Bluetooth subdriver
3112 /* ACPI GBDC/SBDC bits */
3113 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3114 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3115 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3120 /* ACPI \BLTH commands */
3121 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3122 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3123 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3124 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3125 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3128 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3130 static struct rfkill
*tpacpi_bluetooth_rfkill
;
3132 static void bluetooth_suspend(pm_message_t state
)
3134 /* Try to make sure radio will resume powered off */
3135 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3136 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
))
3137 vdbg_printk(TPACPI_DBG_RFKILL
,
3138 "bluetooth power down on resume request failed\n");
3141 static int bluetooth_get_radiosw(void)
3145 if (!tp_features
.bluetooth
)
3148 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3149 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3150 return RFKILL_STATE_HARD_BLOCKED
;
3152 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3153 if (dbg_bluetoothemul
)
3154 return (tpacpi_bluetooth_emulstate
) ?
3155 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3158 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3161 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3162 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3165 static void bluetooth_update_rfk(void)
3169 if (!tpacpi_bluetooth_rfkill
)
3172 status
= bluetooth_get_radiosw();
3175 rfkill_force_state(tpacpi_bluetooth_rfkill
, status
);
3177 vdbg_printk(TPACPI_DBG_RFKILL
,
3178 "forced rfkill state to %d\n",
3182 static int bluetooth_set_radiosw(int radio_on
, int update_rfk
)
3186 if (!tp_features
.bluetooth
)
3189 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3190 * reason to risk weird behaviour. */
3191 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3195 vdbg_printk(TPACPI_DBG_RFKILL
,
3196 "will %s bluetooth\n", radio_on
? "enable" : "disable");
3198 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3199 if (dbg_bluetoothemul
) {
3200 tpacpi_bluetooth_emulstate
= !!radio_on
;
3202 bluetooth_update_rfk();
3207 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3209 status
= TP_ACPI_BLUETOOTH_RADIOSSW
;
3212 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3216 bluetooth_update_rfk();
3221 /* sysfs bluetooth enable ---------------------------------------------- */
3222 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3223 struct device_attribute
*attr
,
3228 printk_deprecated_rfkill_attribute("bluetooth_enable");
3230 status
= bluetooth_get_radiosw();
3234 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3235 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3238 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3239 struct device_attribute
*attr
,
3240 const char *buf
, size_t count
)
3245 printk_deprecated_rfkill_attribute("bluetooth_enable");
3247 if (parse_strtoul(buf
, 1, &t
))
3250 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t
);
3252 res
= bluetooth_set_radiosw(t
, 1);
3254 return (res
) ? res
: count
;
3257 static struct device_attribute dev_attr_bluetooth_enable
=
3258 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3259 bluetooth_enable_show
, bluetooth_enable_store
);
3261 /* --------------------------------------------------------------------- */
3263 static struct attribute
*bluetooth_attributes
[] = {
3264 &dev_attr_bluetooth_enable
.attr
,
3268 static const struct attribute_group bluetooth_attr_group
= {
3269 .attrs
= bluetooth_attributes
,
3272 static int tpacpi_bluetooth_rfk_get(void *data
, enum rfkill_state
*state
)
3274 int bts
= bluetooth_get_radiosw();
3283 static int tpacpi_bluetooth_rfk_set(void *data
, enum rfkill_state state
)
3285 dbg_printk(TPACPI_DBG_RFKILL
,
3286 "request to change radio state to %d\n", state
);
3287 return bluetooth_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3290 static void bluetooth_shutdown(void)
3292 /* Order firmware to save current state to NVRAM */
3293 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3294 TP_ACPI_BLTH_SAVE_STATE
))
3295 printk(TPACPI_NOTICE
3296 "failed to save bluetooth state to NVRAM\n");
3298 vdbg_printk(TPACPI_DBG_RFKILL
,
3299 "bluestooth state saved to NVRAM\n");
3302 static void bluetooth_exit(void)
3304 bluetooth_shutdown();
3306 if (tpacpi_bluetooth_rfkill
)
3307 rfkill_unregister(tpacpi_bluetooth_rfkill
);
3309 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3310 &bluetooth_attr_group
);
3313 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3318 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3319 "initializing bluetooth subdriver\n");
3321 TPACPI_ACPIHANDLE_INIT(hkey
);
3323 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3324 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3325 tp_features
.bluetooth
= hkey_handle
&&
3326 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
3328 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3329 "bluetooth is %s, status 0x%02x\n",
3330 str_supported(tp_features
.bluetooth
),
3333 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3334 if (dbg_bluetoothemul
) {
3335 tp_features
.bluetooth
= 1;
3337 "bluetooth switch emulation enabled\n");
3340 if (tp_features
.bluetooth
&&
3341 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
3342 /* no bluetooth hardware present in system */
3343 tp_features
.bluetooth
= 0;
3344 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3345 "bluetooth hardware not installed\n");
3348 if (!tp_features
.bluetooth
)
3351 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3352 &bluetooth_attr_group
);
3356 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
3357 &tpacpi_bluetooth_rfkill
,
3358 RFKILL_TYPE_BLUETOOTH
,
3359 TPACPI_RFK_BLUETOOTH_SW_NAME
,
3361 tpacpi_bluetooth_rfk_set
,
3362 tpacpi_bluetooth_rfk_get
);
3371 /* procfs -------------------------------------------------------------- */
3372 static int bluetooth_read(char *p
)
3375 int status
= bluetooth_get_radiosw();
3377 if (!tp_features
.bluetooth
)
3378 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3380 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3381 (status
== RFKILL_STATE_UNBLOCKED
) ?
3382 "enabled" : "disabled");
3383 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3389 static int bluetooth_write(char *buf
)
3394 if (!tp_features
.bluetooth
)
3397 while ((cmd
= next_cmd(&buf
))) {
3398 if (strlencmp(cmd
, "enable") == 0) {
3400 } else if (strlencmp(cmd
, "disable") == 0) {
3407 tpacpi_disclose_usertask("procfs bluetooth",
3409 state
? "enable" : "disable");
3410 bluetooth_set_radiosw(state
, 1);
3416 static struct ibm_struct bluetooth_driver_data
= {
3417 .name
= "bluetooth",
3418 .read
= bluetooth_read
,
3419 .write
= bluetooth_write
,
3420 .exit
= bluetooth_exit
,
3421 .suspend
= bluetooth_suspend
,
3422 .shutdown
= bluetooth_shutdown
,
3425 /*************************************************************************
3430 /* ACPI GWAN/SWAN bits */
3431 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
3432 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
3433 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
3437 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3439 static struct rfkill
*tpacpi_wan_rfkill
;
3441 static void wan_suspend(pm_message_t state
)
3443 /* Try to make sure radio will resume powered off */
3444 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "qvd",
3445 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
))
3446 vdbg_printk(TPACPI_DBG_RFKILL
,
3447 "WWAN power down on resume request failed\n");
3450 static int wan_get_radiosw(void)
3454 if (!tp_features
.wan
)
3457 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3458 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3459 return RFKILL_STATE_HARD_BLOCKED
;
3461 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3463 return (tpacpi_wwan_emulstate
) ?
3464 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3467 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
3470 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
3471 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3474 static void wan_update_rfk(void)
3478 if (!tpacpi_wan_rfkill
)
3481 status
= wan_get_radiosw();
3484 rfkill_force_state(tpacpi_wan_rfkill
, status
);
3486 vdbg_printk(TPACPI_DBG_RFKILL
,
3487 "forced rfkill state to %d\n",
3491 static int wan_set_radiosw(int radio_on
, int update_rfk
)
3495 if (!tp_features
.wan
)
3498 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3499 * reason to risk weird behaviour. */
3500 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3504 vdbg_printk(TPACPI_DBG_RFKILL
,
3505 "will %s WWAN\n", radio_on
? "enable" : "disable");
3507 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3509 tpacpi_wwan_emulstate
= !!radio_on
;
3516 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3518 status
= TP_ACPI_WANCARD_RADIOSSW
;
3521 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
3530 /* sysfs wan enable ---------------------------------------------------- */
3531 static ssize_t
wan_enable_show(struct device
*dev
,
3532 struct device_attribute
*attr
,
3537 printk_deprecated_rfkill_attribute("wwan_enable");
3539 status
= wan_get_radiosw();
3543 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3544 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3547 static ssize_t
wan_enable_store(struct device
*dev
,
3548 struct device_attribute
*attr
,
3549 const char *buf
, size_t count
)
3554 printk_deprecated_rfkill_attribute("wwan_enable");
3556 if (parse_strtoul(buf
, 1, &t
))
3559 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t
);
3561 res
= wan_set_radiosw(t
, 1);
3563 return (res
) ? res
: count
;
3566 static struct device_attribute dev_attr_wan_enable
=
3567 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
3568 wan_enable_show
, wan_enable_store
);
3570 /* --------------------------------------------------------------------- */
3572 static struct attribute
*wan_attributes
[] = {
3573 &dev_attr_wan_enable
.attr
,
3577 static const struct attribute_group wan_attr_group
= {
3578 .attrs
= wan_attributes
,
3581 static int tpacpi_wan_rfk_get(void *data
, enum rfkill_state
*state
)
3583 int wans
= wan_get_radiosw();
3592 static int tpacpi_wan_rfk_set(void *data
, enum rfkill_state state
)
3594 dbg_printk(TPACPI_DBG_RFKILL
,
3595 "request to change radio state to %d\n", state
);
3596 return wan_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3599 static void wan_shutdown(void)
3601 /* Order firmware to save current state to NVRAM */
3602 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
3603 TP_ACPI_WGSV_SAVE_STATE
))
3604 printk(TPACPI_NOTICE
3605 "failed to save WWAN state to NVRAM\n");
3607 vdbg_printk(TPACPI_DBG_RFKILL
,
3608 "WWAN state saved to NVRAM\n");
3611 static void wan_exit(void)
3615 if (tpacpi_wan_rfkill
)
3616 rfkill_unregister(tpacpi_wan_rfkill
);
3618 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3622 static int __init
wan_init(struct ibm_init_struct
*iibm
)
3627 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3628 "initializing wan subdriver\n");
3630 TPACPI_ACPIHANDLE_INIT(hkey
);
3632 tp_features
.wan
= hkey_handle
&&
3633 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
3635 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3636 "wan is %s, status 0x%02x\n",
3637 str_supported(tp_features
.wan
),
3640 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3642 tp_features
.wan
= 1;
3644 "wwan switch emulation enabled\n");
3647 if (tp_features
.wan
&&
3648 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
3649 /* no wan hardware present in system */
3650 tp_features
.wan
= 0;
3651 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3652 "wan hardware not installed\n");
3655 if (!tp_features
.wan
)
3658 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3663 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
3666 TPACPI_RFK_WWAN_SW_NAME
,
3669 tpacpi_wan_rfk_get
);
3678 /* procfs -------------------------------------------------------------- */
3679 static int wan_read(char *p
)
3682 int status
= wan_get_radiosw();
3684 tpacpi_disclose_usertask("procfs wan", "read");
3686 if (!tp_features
.wan
)
3687 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3689 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3690 (status
== RFKILL_STATE_UNBLOCKED
) ?
3691 "enabled" : "disabled");
3692 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3698 static int wan_write(char *buf
)
3703 if (!tp_features
.wan
)
3706 while ((cmd
= next_cmd(&buf
))) {
3707 if (strlencmp(cmd
, "enable") == 0) {
3709 } else if (strlencmp(cmd
, "disable") == 0) {
3716 tpacpi_disclose_usertask("procfs wan",
3718 state
? "enable" : "disable");
3719 wan_set_radiosw(state
, 1);
3725 static struct ibm_struct wan_driver_data
= {
3730 .suspend
= wan_suspend
,
3731 .shutdown
= wan_shutdown
,
3734 /*************************************************************************
3739 /* ACPI GUWB/SUWB bits */
3740 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
3741 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
3744 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3746 static struct rfkill
*tpacpi_uwb_rfkill
;
3748 static int uwb_get_radiosw(void)
3752 if (!tp_features
.uwb
)
3755 /* WLSW overrides UWB in firmware/hardware, reflect that */
3756 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3757 return RFKILL_STATE_HARD_BLOCKED
;
3759 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3761 return (tpacpi_uwb_emulstate
) ?
3762 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3765 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
3768 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
3769 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3772 static void uwb_update_rfk(void)
3776 if (!tpacpi_uwb_rfkill
)
3779 status
= uwb_get_radiosw();
3782 rfkill_force_state(tpacpi_uwb_rfkill
, status
);
3784 vdbg_printk(TPACPI_DBG_RFKILL
,
3785 "forced rfkill state to %d\n",
3789 static int uwb_set_radiosw(int radio_on
, int update_rfk
)
3793 if (!tp_features
.uwb
)
3796 /* WLSW overrides UWB in firmware/hardware, but there is no
3797 * reason to risk weird behaviour. */
3798 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3802 vdbg_printk(TPACPI_DBG_RFKILL
,
3803 "will %s UWB\n", radio_on
? "enable" : "disable");
3805 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3807 tpacpi_uwb_emulstate
= !!radio_on
;
3814 status
= (radio_on
) ? TP_ACPI_UWB_RADIOSSW
: 0;
3815 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
3824 /* --------------------------------------------------------------------- */
3826 static int tpacpi_uwb_rfk_get(void *data
, enum rfkill_state
*state
)
3828 int uwbs
= uwb_get_radiosw();
3837 static int tpacpi_uwb_rfk_set(void *data
, enum rfkill_state state
)
3839 dbg_printk(TPACPI_DBG_RFKILL
,
3840 "request to change radio state to %d\n", state
);
3841 return uwb_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3844 static void uwb_exit(void)
3846 if (tpacpi_uwb_rfkill
)
3847 rfkill_unregister(tpacpi_uwb_rfkill
);
3850 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
3855 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3856 "initializing uwb subdriver\n");
3858 TPACPI_ACPIHANDLE_INIT(hkey
);
3860 tp_features
.uwb
= hkey_handle
&&
3861 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
3863 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3864 "uwb is %s, status 0x%02x\n",
3865 str_supported(tp_features
.uwb
),
3868 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3870 tp_features
.uwb
= 1;
3872 "uwb switch emulation enabled\n");
3875 if (tp_features
.uwb
&&
3876 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
3877 /* no uwb hardware present in system */
3878 tp_features
.uwb
= 0;
3879 dbg_printk(TPACPI_DBG_INIT
,
3880 "uwb hardware not installed\n");
3883 if (!tp_features
.uwb
)
3886 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
3889 TPACPI_RFK_UWB_SW_NAME
,
3892 tpacpi_uwb_rfk_get
);
3897 static struct ibm_struct uwb_driver_data
= {
3900 .flags
.experimental
= 1,
3903 /*************************************************************************
3907 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3909 enum video_access_mode
{
3910 TPACPI_VIDEO_NONE
= 0,
3911 TPACPI_VIDEO_570
, /* 570 */
3912 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
3913 TPACPI_VIDEO_NEW
, /* all others */
3916 enum { /* video status flags, based on VIDEO_570 */
3917 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
3918 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
3919 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
3922 enum { /* TPACPI_VIDEO_570 constants */
3923 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
3924 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
3925 * video_status_flags */
3926 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
3927 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
3930 static enum video_access_mode video_supported
;
3931 static int video_orig_autosw
;
3933 static int video_autosw_get(void);
3934 static int video_autosw_set(int enable
);
3936 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3938 static int __init
video_init(struct ibm_init_struct
*iibm
)
3942 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
3944 TPACPI_ACPIHANDLE_INIT(vid
);
3945 TPACPI_ACPIHANDLE_INIT(vid2
);
3947 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
3948 /* G41, assume IVGA doesn't change */
3949 vid_handle
= vid2_handle
;
3952 /* video switching not supported on R30, R31 */
3953 video_supported
= TPACPI_VIDEO_NONE
;
3954 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
3956 video_supported
= TPACPI_VIDEO_570
;
3957 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
3958 /* 600e/x, 770e, 770x */
3959 video_supported
= TPACPI_VIDEO_770
;
3962 video_supported
= TPACPI_VIDEO_NEW
;
3964 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
3965 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
3968 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
3971 static void video_exit(void)
3973 dbg_printk(TPACPI_DBG_EXIT
,
3974 "restoring original video autoswitch mode\n");
3975 if (video_autosw_set(video_orig_autosw
))
3976 printk(TPACPI_ERR
"error while trying to restore original "
3977 "video autoswitch mode\n");
3980 static int video_outputsw_get(void)
3985 switch (video_supported
) {
3986 case TPACPI_VIDEO_570
:
3987 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
3988 TP_ACPI_VIDEO_570_PHSCMD
))
3990 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
3992 case TPACPI_VIDEO_770
:
3993 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3996 status
|= TP_ACPI_VIDEO_S_LCD
;
3997 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4000 status
|= TP_ACPI_VIDEO_S_CRT
;
4002 case TPACPI_VIDEO_NEW
:
4003 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4004 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4007 status
|= TP_ACPI_VIDEO_S_CRT
;
4009 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4010 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4013 status
|= TP_ACPI_VIDEO_S_LCD
;
4014 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4017 status
|= TP_ACPI_VIDEO_S_DVI
;
4026 static int video_outputsw_set(int status
)
4031 switch (video_supported
) {
4032 case TPACPI_VIDEO_570
:
4033 res
= acpi_evalf(NULL
, NULL
,
4034 "\\_SB.PHS2", "vdd",
4035 TP_ACPI_VIDEO_570_PHS2CMD
,
4036 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4038 case TPACPI_VIDEO_770
:
4039 autosw
= video_autosw_get();
4043 res
= video_autosw_set(1);
4046 res
= acpi_evalf(vid_handle
, NULL
,
4047 "ASWT", "vdd", status
* 0x100, 0);
4048 if (!autosw
&& video_autosw_set(autosw
)) {
4050 "video auto-switch left enabled due to error\n");
4054 case TPACPI_VIDEO_NEW
:
4055 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
4056 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
4062 return (res
)? 0 : -EIO
;
4065 static int video_autosw_get(void)
4069 switch (video_supported
) {
4070 case TPACPI_VIDEO_570
:
4071 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
4074 case TPACPI_VIDEO_770
:
4075 case TPACPI_VIDEO_NEW
:
4076 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4086 static int video_autosw_set(int enable
)
4088 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
4093 static int video_outputsw_cycle(void)
4095 int autosw
= video_autosw_get();
4101 switch (video_supported
) {
4102 case TPACPI_VIDEO_570
:
4103 res
= video_autosw_set(1);
4106 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4108 case TPACPI_VIDEO_770
:
4109 case TPACPI_VIDEO_NEW
:
4110 res
= video_autosw_set(1);
4113 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4118 if (!autosw
&& video_autosw_set(autosw
)) {
4120 "video auto-switch left enabled due to error\n");
4124 return (res
)? 0 : -EIO
;
4127 static int video_expand_toggle(void)
4129 switch (video_supported
) {
4130 case TPACPI_VIDEO_570
:
4131 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4133 case TPACPI_VIDEO_770
:
4134 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4136 case TPACPI_VIDEO_NEW
:
4137 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4145 static int video_read(char *p
)
4150 if (video_supported
== TPACPI_VIDEO_NONE
) {
4151 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4155 status
= video_outputsw_get();
4159 autosw
= video_autosw_get();
4163 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4164 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
4165 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
4166 if (video_supported
== TPACPI_VIDEO_NEW
)
4167 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
4168 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
4169 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
4170 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
4171 if (video_supported
== TPACPI_VIDEO_NEW
)
4172 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
4173 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
4174 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4179 static int video_write(char *buf
)
4182 int enable
, disable
, status
;
4185 if (video_supported
== TPACPI_VIDEO_NONE
)
4191 while ((cmd
= next_cmd(&buf
))) {
4192 if (strlencmp(cmd
, "lcd_enable") == 0) {
4193 enable
|= TP_ACPI_VIDEO_S_LCD
;
4194 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4195 disable
|= TP_ACPI_VIDEO_S_LCD
;
4196 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4197 enable
|= TP_ACPI_VIDEO_S_CRT
;
4198 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4199 disable
|= TP_ACPI_VIDEO_S_CRT
;
4200 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4201 strlencmp(cmd
, "dvi_enable") == 0) {
4202 enable
|= TP_ACPI_VIDEO_S_DVI
;
4203 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4204 strlencmp(cmd
, "dvi_disable") == 0) {
4205 disable
|= TP_ACPI_VIDEO_S_DVI
;
4206 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4207 res
= video_autosw_set(1);
4210 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4211 res
= video_autosw_set(0);
4214 } else if (strlencmp(cmd
, "video_switch") == 0) {
4215 res
= video_outputsw_cycle();
4218 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4219 res
= video_expand_toggle();
4226 if (enable
|| disable
) {
4227 status
= video_outputsw_get();
4230 res
= video_outputsw_set((status
& ~disable
) | enable
);
4238 static struct ibm_struct video_driver_data
= {
4241 .write
= video_write
,
4245 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4247 /*************************************************************************
4248 * Light (thinklight) subdriver
4251 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4252 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4254 static int light_get_status(void)
4258 if (tp_features
.light_status
) {
4259 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4267 static int light_set_status(int status
)
4271 if (tp_features
.light
) {
4273 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4275 TP_CMOS_THINKLIGHT_ON
:
4276 TP_CMOS_THINKLIGHT_OFF
);
4278 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4281 return (rc
)? 0 : -EIO
;
4287 static void light_set_status_worker(struct work_struct
*work
)
4289 struct tpacpi_led_classdev
*data
=
4290 container_of(work
, struct tpacpi_led_classdev
, work
);
4292 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4293 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4296 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4297 enum led_brightness brightness
)
4299 struct tpacpi_led_classdev
*data
=
4300 container_of(led_cdev
,
4301 struct tpacpi_led_classdev
,
4303 data
->new_state
= (brightness
!= LED_OFF
) ?
4304 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4305 queue_work(tpacpi_wq
, &data
->work
);
4308 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4310 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4313 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4315 .name
= "tpacpi::thinklight",
4316 .brightness_set
= &light_sysfs_set
,
4317 .brightness_get
= &light_sysfs_get
,
4321 static int __init
light_init(struct ibm_init_struct
*iibm
)
4325 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4327 TPACPI_ACPIHANDLE_INIT(ledb
);
4328 TPACPI_ACPIHANDLE_INIT(lght
);
4329 TPACPI_ACPIHANDLE_INIT(cmos
);
4330 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4332 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4333 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4335 if (tp_features
.light
)
4336 /* light status not supported on
4337 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4338 tp_features
.light_status
=
4339 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4341 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4342 str_supported(tp_features
.light
),
4343 str_supported(tp_features
.light_status
));
4345 if (!tp_features
.light
)
4348 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4349 &tpacpi_led_thinklight
.led_classdev
);
4352 tp_features
.light
= 0;
4353 tp_features
.light_status
= 0;
4361 static void light_exit(void)
4363 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4364 if (work_pending(&tpacpi_led_thinklight
.work
))
4365 flush_workqueue(tpacpi_wq
);
4368 static int light_read(char *p
)
4373 if (!tp_features
.light
) {
4374 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4375 } else if (!tp_features
.light_status
) {
4376 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4377 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4379 status
= light_get_status();
4382 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4383 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4389 static int light_write(char *buf
)
4394 if (!tp_features
.light
)
4397 while ((cmd
= next_cmd(&buf
))) {
4398 if (strlencmp(cmd
, "on") == 0) {
4400 } else if (strlencmp(cmd
, "off") == 0) {
4406 return light_set_status(newstatus
);
4409 static struct ibm_struct light_driver_data
= {
4412 .write
= light_write
,
4416 /*************************************************************************
4420 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4422 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
4423 static int dock_read(char *p
);
4424 static int dock_write(char *buf
);
4426 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
4427 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4428 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4429 "\\_SB.PCI.ISA.SLCE", /* 570 */
4430 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4432 /* don't list other alternatives as we install a notify handler on the 570 */
4433 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
4435 static const struct acpi_device_id ibm_pci_device_ids
[] = {
4436 {PCI_ROOT_HID_STRING
, 0},
4440 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
4442 .notify
= dock_notify
,
4443 .handle
= &dock_handle
,
4444 .type
= ACPI_SYSTEM_NOTIFY
,
4447 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4448 * We just use it to get notifications of dock hotplug
4449 * in very old thinkpads */
4450 .hid
= ibm_pci_device_ids
,
4451 .notify
= dock_notify
,
4452 .handle
= &pci_handle
,
4453 .type
= ACPI_SYSTEM_NOTIFY
,
4457 static struct ibm_struct dock_driver_data
[2] = {
4461 .write
= dock_write
,
4462 .acpi
= &ibm_dock_acpidriver
[0],
4466 .acpi
= &ibm_dock_acpidriver
[1],
4470 #define dock_docked() (_sta(dock_handle) & 1)
4472 static int __init
dock_init(struct ibm_init_struct
*iibm
)
4474 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
4476 TPACPI_ACPIHANDLE_INIT(dock
);
4478 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
4479 str_supported(dock_handle
!= NULL
));
4481 return (dock_handle
)? 0 : 1;
4484 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
4488 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
4490 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
4491 dock_driver_data
[0].flags
.acpi_notify_installed
) {
4492 TPACPI_ACPIHANDLE_INIT(pci
);
4493 dock2_needed
= (pci_handle
!= NULL
);
4494 vdbg_printk(TPACPI_DBG_INIT
,
4495 "dock PCI handler for the TP 570 is %s\n",
4496 str_supported(dock2_needed
));
4498 vdbg_printk(TPACPI_DBG_INIT
,
4499 "dock subdriver part 2 not required\n");
4503 return (dock2_needed
)? 0 : 1;
4506 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
4508 int docked
= dock_docked();
4509 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
4510 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
4513 if (event
== 1 && !pci
) /* 570 */
4514 data
= 1; /* button */
4515 else if (event
== 1 && pci
) /* 570 */
4516 data
= 3; /* dock */
4517 else if (event
== 3 && docked
)
4518 data
= 1; /* button */
4519 else if (event
== 3 && !docked
)
4520 data
= 2; /* undock */
4521 else if (event
== 0 && docked
)
4522 data
= 3; /* dock */
4524 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
4525 event
, _sta(dock_handle
));
4526 data
= 0; /* unknown */
4528 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
4529 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4530 dev_name(&ibm
->acpi
->device
->dev
),
4534 static int dock_read(char *p
)
4537 int docked
= dock_docked();
4540 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4542 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
4544 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
4545 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
4551 static int dock_write(char *buf
)
4558 while ((cmd
= next_cmd(&buf
))) {
4559 if (strlencmp(cmd
, "undock") == 0) {
4560 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
4561 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
4563 } else if (strlencmp(cmd
, "dock") == 0) {
4564 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
4573 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4575 /*************************************************************************
4579 #ifdef CONFIG_THINKPAD_ACPI_BAY
4581 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4582 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4583 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4584 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4585 ); /* A21e, R30, R31 */
4586 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
4587 "_EJ0", /* all others */
4588 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4589 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4590 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4592 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
4596 static int __init
bay_init(struct ibm_init_struct
*iibm
)
4598 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
4600 TPACPI_ACPIHANDLE_INIT(bay
);
4602 TPACPI_ACPIHANDLE_INIT(bay_ej
);
4603 TPACPI_ACPIHANDLE_INIT(bay2
);
4605 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
4607 tp_features
.bay_status
= bay_handle
&&
4608 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
4609 tp_features
.bay_status2
= bay2_handle
&&
4610 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
4612 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
4613 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
4614 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
4615 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
4617 vdbg_printk(TPACPI_DBG_INIT
,
4618 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4619 str_supported(tp_features
.bay_status
),
4620 str_supported(tp_features
.bay_eject
),
4621 str_supported(tp_features
.bay_status2
),
4622 str_supported(tp_features
.bay_eject2
));
4624 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
4625 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
4628 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
4630 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
4631 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4632 dev_name(&ibm
->acpi
->device
->dev
),
4636 #define bay_occupied(b) (_sta(b##_handle) & 1)
4638 static int bay_read(char *p
)
4641 int occupied
= bay_occupied(bay
);
4642 int occupied2
= bay_occupied(bay2
);
4645 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
4646 tp_features
.bay_status
?
4647 (occupied
? "occupied" : "unoccupied") :
4649 if (tp_features
.bay_status2
)
4650 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
4651 "occupied" : "unoccupied");
4653 eject
= tp_features
.bay_eject
&& occupied
;
4654 eject2
= tp_features
.bay_eject2
&& occupied2
;
4656 if (eject
&& eject2
)
4657 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
4659 len
+= sprintf(p
+ len
, "commands:\teject\n");
4661 len
+= sprintf(p
+ len
, "commands:\teject2\n");
4666 static int bay_write(char *buf
)
4670 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
4673 while ((cmd
= next_cmd(&buf
))) {
4674 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
4675 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
4677 } else if (tp_features
.bay_eject2
&&
4678 strlencmp(cmd
, "eject2") == 0) {
4679 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
4688 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
4689 .notify
= bay_notify
,
4690 .handle
= &bay_handle
,
4691 .type
= ACPI_SYSTEM_NOTIFY
,
4694 static struct ibm_struct bay_driver_data
= {
4698 .acpi
= &ibm_bay_acpidriver
,
4701 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4703 /*************************************************************************
4707 /* sysfs cmos_command -------------------------------------------------- */
4708 static ssize_t
cmos_command_store(struct device
*dev
,
4709 struct device_attribute
*attr
,
4710 const char *buf
, size_t count
)
4712 unsigned long cmos_cmd
;
4715 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4718 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4719 return (res
)? res
: count
;
4722 static struct device_attribute dev_attr_cmos_command
=
4723 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4725 /* --------------------------------------------------------------------- */
4727 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4731 vdbg_printk(TPACPI_DBG_INIT
,
4732 "initializing cmos commands subdriver\n");
4734 TPACPI_ACPIHANDLE_INIT(cmos
);
4736 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4737 str_supported(cmos_handle
!= NULL
));
4739 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4743 return (cmos_handle
)? 0 : 1;
4746 static void cmos_exit(void)
4748 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4751 static int cmos_read(char *p
)
4755 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4756 R30, R31, T20-22, X20-21 */
4758 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4760 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4761 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4767 static int cmos_write(char *buf
)
4772 while ((cmd
= next_cmd(&buf
))) {
4773 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4774 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4779 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4787 static struct ibm_struct cmos_driver_data
= {
4790 .write
= cmos_write
,
4794 /*************************************************************************
4798 enum led_access_mode
{
4799 TPACPI_LED_NONE
= 0,
4800 TPACPI_LED_570
, /* 570 */
4801 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4802 TPACPI_LED_NEW
, /* all others */
4805 enum { /* For TPACPI_LED_OLD */
4806 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4807 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4808 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4811 static enum led_access_mode led_supported
;
4813 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4814 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4815 /* T20-22, X20-21 */
4816 "LED", /* all others */
4819 #define TPACPI_LED_NUMLEDS 16
4820 static struct tpacpi_led_classdev
*tpacpi_leds
;
4821 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
4822 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
4823 /* there's a limit of 19 chars + NULL before 2.6.26 */
4825 "tpacpi:orange:batt",
4826 "tpacpi:green:batt",
4827 "tpacpi::dock_active",
4828 "tpacpi::bay_active",
4829 "tpacpi::dock_batt",
4830 "tpacpi::unknown_led",
4832 "tpacpi::dock_status1",
4833 "tpacpi::dock_status2",
4834 "tpacpi::unknown_led2",
4835 "tpacpi::unknown_led3",
4836 "tpacpi::thinkvantage",
4838 #define TPACPI_SAFE_LEDS 0x1081U
4840 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
4842 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4845 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
4849 static int led_get_status(const unsigned int led
)
4852 enum led_status_t led_s
;
4854 switch (led_supported
) {
4855 case TPACPI_LED_570
:
4856 if (!acpi_evalf(ec_handle
,
4857 &status
, "GLED", "dd", 1 << led
))
4859 led_s
= (status
== 0)?
4864 tpacpi_led_state_cache
[led
] = led_s
;
4873 static int led_set_status(const unsigned int led
,
4874 const enum led_status_t ledstatus
)
4876 /* off, on, blink. Index is led_status_t */
4877 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
4878 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
4882 switch (led_supported
) {
4883 case TPACPI_LED_570
:
4885 if (unlikely(led
> 7))
4887 if (unlikely(tpacpi_is_led_restricted(led
)))
4889 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4890 (1 << led
), led_sled_arg1
[ledstatus
]))
4893 case TPACPI_LED_OLD
:
4894 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4895 if (unlikely(led
> 7))
4897 if (unlikely(tpacpi_is_led_restricted(led
)))
4899 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
4901 rc
= ec_write(TPACPI_LED_EC_HLBL
,
4902 (ledstatus
== TPACPI_LED_BLINK
) << led
);
4904 rc
= ec_write(TPACPI_LED_EC_HLCL
,
4905 (ledstatus
!= TPACPI_LED_OFF
) << led
);
4907 case TPACPI_LED_NEW
:
4909 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
4911 if (unlikely(tpacpi_is_led_restricted(led
)))
4913 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4914 led
, led_led_arg1
[ledstatus
]))
4922 tpacpi_led_state_cache
[led
] = ledstatus
;
4927 static void led_set_status_worker(struct work_struct
*work
)
4929 struct tpacpi_led_classdev
*data
=
4930 container_of(work
, struct tpacpi_led_classdev
, work
);
4932 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4933 led_set_status(data
->led
, data
->new_state
);
4936 static void led_sysfs_set(struct led_classdev
*led_cdev
,
4937 enum led_brightness brightness
)
4939 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4940 struct tpacpi_led_classdev
, led_classdev
);
4942 if (brightness
== LED_OFF
)
4943 data
->new_state
= TPACPI_LED_OFF
;
4944 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
4945 data
->new_state
= TPACPI_LED_ON
;
4947 data
->new_state
= TPACPI_LED_BLINK
;
4949 queue_work(tpacpi_wq
, &data
->work
);
4952 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
4953 unsigned long *delay_on
, unsigned long *delay_off
)
4955 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4956 struct tpacpi_led_classdev
, led_classdev
);
4958 /* Can we choose the flash rate? */
4959 if (*delay_on
== 0 && *delay_off
== 0) {
4960 /* yes. set them to the hardware blink rate (1 Hz) */
4961 *delay_on
= 500; /* ms */
4962 *delay_off
= 500; /* ms */
4963 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
4966 data
->new_state
= TPACPI_LED_BLINK
;
4967 queue_work(tpacpi_wq
, &data
->work
);
4972 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
4976 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4977 struct tpacpi_led_classdev
, led_classdev
);
4979 rc
= led_get_status(data
->led
);
4981 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
4982 rc
= LED_OFF
; /* no error handling in led class :( */
4989 static void led_exit(void)
4993 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4994 if (tpacpi_leds
[i
].led_classdev
.name
)
4995 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
5001 static int __init
tpacpi_init_led(unsigned int led
)
5005 tpacpi_leds
[led
].led
= led
;
5007 /* LEDs with no name don't get registered */
5008 if (!tpacpi_led_names
[led
])
5011 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
5012 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
5013 if (led_supported
== TPACPI_LED_570
)
5014 tpacpi_leds
[led
].led_classdev
.brightness_get
=
5017 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
5019 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
5021 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5022 &tpacpi_leds
[led
].led_classdev
);
5024 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
5029 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
5030 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5031 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5032 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5034 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5035 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5036 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5037 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5038 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5039 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5040 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5041 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5043 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5044 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5045 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5046 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5047 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5049 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5050 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5051 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5052 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5054 /* (1) - may have excess leds enabled on MSB */
5056 /* Defaults (order matters, keep last, don't reorder!) */
5058 .vendor
= PCI_VENDOR_ID_LENOVO
,
5059 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5062 { /* IBM ThinkPads with no EC version string */
5063 .vendor
= PCI_VENDOR_ID_IBM
,
5064 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
5067 { /* IBM ThinkPads with EC version string */
5068 .vendor
= PCI_VENDOR_ID_IBM
,
5069 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5074 #undef TPACPI_LEDQ_IBM
5075 #undef TPACPI_LEDQ_LNV
5077 static int __init
led_init(struct ibm_init_struct
*iibm
)
5081 unsigned long useful_leds
;
5083 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
5085 TPACPI_ACPIHANDLE_INIT(led
);
5088 /* led not supported on R30, R31 */
5089 led_supported
= TPACPI_LED_NONE
;
5090 else if (strlencmp(led_path
, "SLED") == 0)
5092 led_supported
= TPACPI_LED_570
;
5093 else if (strlencmp(led_path
, "SYSL") == 0)
5094 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5095 led_supported
= TPACPI_LED_OLD
;
5098 led_supported
= TPACPI_LED_NEW
;
5100 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
5101 str_supported(led_supported
), led_supported
);
5103 if (led_supported
== TPACPI_LED_NONE
)
5106 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
5109 printk(TPACPI_ERR
"Out of memory for LED data\n");
5113 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
5114 ARRAY_SIZE(led_useful_qtable
));
5116 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5117 if (!tpacpi_is_led_restricted(i
) &&
5118 test_bit(i
, &useful_leds
)) {
5119 rc
= tpacpi_init_led(i
);
5127 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5128 printk(TPACPI_NOTICE
5129 "warning: userspace override of important "
5130 "firmware LEDs is enabled\n");
5135 #define str_led_status(s) \
5136 ((s) == TPACPI_LED_OFF ? "off" : \
5137 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5139 static int led_read(char *p
)
5143 if (!led_supported
) {
5144 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5147 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5149 if (led_supported
== TPACPI_LED_570
) {
5152 for (i
= 0; i
< 8; i
++) {
5153 status
= led_get_status(i
);
5156 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
5157 i
, str_led_status(status
));
5161 len
+= sprintf(p
+ len
, "commands:\t"
5162 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5167 static int led_write(char *buf
)
5171 enum led_status_t s
;
5176 while ((cmd
= next_cmd(&buf
))) {
5177 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 15)
5180 if (strstr(cmd
, "off")) {
5182 } else if (strstr(cmd
, "on")) {
5184 } else if (strstr(cmd
, "blink")) {
5185 s
= TPACPI_LED_BLINK
;
5190 rc
= led_set_status(led
, s
);
5198 static struct ibm_struct led_driver_data
= {
5205 /*************************************************************************
5209 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5211 #define TPACPI_BEEP_Q1 0x0001
5213 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
5214 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
5215 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
5218 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5220 unsigned long quirks
;
5222 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5224 TPACPI_ACPIHANDLE_INIT(beep
);
5226 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5227 str_supported(beep_handle
!= NULL
));
5229 quirks
= tpacpi_check_quirks(beep_quirk_table
,
5230 ARRAY_SIZE(beep_quirk_table
));
5232 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
5234 return (beep_handle
)? 0 : 1;
5237 static int beep_read(char *p
)
5242 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5244 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5245 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5251 static int beep_write(char *buf
)
5259 while ((cmd
= next_cmd(&buf
))) {
5260 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5261 beep_cmd
>= 0 && beep_cmd
<= 17) {
5265 if (tp_features
.beep_needs_two_args
) {
5266 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
5270 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
5279 static struct ibm_struct beep_driver_data
= {
5282 .write
= beep_write
,
5285 /*************************************************************************
5289 enum thermal_access_mode
{
5290 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5291 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5292 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5293 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5294 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5297 enum { /* TPACPI_THERMAL_TPEC_* */
5298 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5299 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5300 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5303 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5304 struct ibm_thermal_sensors_struct
{
5305 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5308 static enum thermal_access_mode thermal_read_mode
;
5310 /* idx is zero-based */
5311 static int thermal_get_sensor(int idx
, s32
*value
)
5317 t
= TP_EC_THERMAL_TMP0
;
5319 switch (thermal_read_mode
) {
5320 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5321 case TPACPI_THERMAL_TPEC_16
:
5322 if (idx
>= 8 && idx
<= 15) {
5323 t
= TP_EC_THERMAL_TMP8
;
5328 case TPACPI_THERMAL_TPEC_8
:
5330 if (!acpi_ec_read(t
+ idx
, &tmp
))
5332 *value
= tmp
* 1000;
5337 case TPACPI_THERMAL_ACPI_UPDT
:
5339 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5340 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5342 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5344 *value
= (t
- 2732) * 100;
5349 case TPACPI_THERMAL_ACPI_TMP07
:
5351 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5352 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5354 if (t
> 127 || t
< -127)
5355 t
= TP_EC_THERMAL_TMP_NA
;
5361 case TPACPI_THERMAL_NONE
:
5369 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5380 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5383 for (i
= 0 ; i
< n
; i
++) {
5384 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5392 /* sysfs temp##_input -------------------------------------------------- */
5394 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5395 struct device_attribute
*attr
,
5398 struct sensor_device_attribute
*sensor_attr
=
5399 to_sensor_dev_attr(attr
);
5400 int idx
= sensor_attr
->index
;
5404 res
= thermal_get_sensor(idx
, &value
);
5407 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
5410 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5413 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5414 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5415 thermal_temp_input_show, NULL, _idxB)
5417 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5418 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5419 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5420 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5421 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5422 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5423 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5424 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5425 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5426 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5427 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5428 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5429 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5430 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5431 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5432 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5433 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5436 #define THERMAL_ATTRS(X) \
5437 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5439 static struct attribute
*thermal_temp_input_attr
[] = {
5459 static const struct attribute_group thermal_temp_input16_group
= {
5460 .attrs
= thermal_temp_input_attr
5463 static const struct attribute_group thermal_temp_input8_group
= {
5464 .attrs
= &thermal_temp_input_attr
[8]
5467 #undef THERMAL_SENSOR_ATTR_TEMP
5468 #undef THERMAL_ATTRS
5470 /* --------------------------------------------------------------------- */
5472 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5479 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5481 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5483 if (thinkpad_id
.ec_model
) {
5485 * Direct EC access mode: sensors at registers
5486 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5487 * non-implemented, thermal sensors return 0x80 when
5492 for (i
= 0; i
< 8; i
++) {
5493 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5499 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5507 /* This is sheer paranoia, but we handle it anyway */
5510 "ThinkPad ACPI EC access misbehaving, "
5511 "falling back to ACPI TMPx access "
5513 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5516 "ThinkPad ACPI EC access misbehaving, "
5517 "disabling thermal sensors access\n");
5518 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5523 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5525 } else if (acpi_tmp7
) {
5526 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5527 /* 600e/x, 770e, 770x */
5528 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5530 /* Standard ACPI TMPx access, max 8 sensors */
5531 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5534 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5535 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5538 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5539 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5542 switch (thermal_read_mode
) {
5543 case TPACPI_THERMAL_TPEC_16
:
5544 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5545 &thermal_temp_input16_group
);
5549 case TPACPI_THERMAL_TPEC_8
:
5550 case TPACPI_THERMAL_ACPI_TMP07
:
5551 case TPACPI_THERMAL_ACPI_UPDT
:
5552 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5553 &thermal_temp_input8_group
);
5557 case TPACPI_THERMAL_NONE
:
5565 static void thermal_exit(void)
5567 switch (thermal_read_mode
) {
5568 case TPACPI_THERMAL_TPEC_16
:
5569 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5570 &thermal_temp_input16_group
);
5572 case TPACPI_THERMAL_TPEC_8
:
5573 case TPACPI_THERMAL_ACPI_TMP07
:
5574 case TPACPI_THERMAL_ACPI_UPDT
:
5575 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5576 &thermal_temp_input16_group
);
5578 case TPACPI_THERMAL_NONE
:
5584 static int thermal_read(char *p
)
5588 struct ibm_thermal_sensors_struct t
;
5590 n
= thermal_get_sensors(&t
);
5591 if (unlikely(n
< 0))
5594 len
+= sprintf(p
+ len
, "temperatures:\t");
5597 for (i
= 0; i
< (n
- 1); i
++)
5598 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5599 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5601 len
+= sprintf(p
+ len
, "not supported\n");
5606 static struct ibm_struct thermal_driver_data
= {
5608 .read
= thermal_read
,
5609 .exit
= thermal_exit
,
5612 /*************************************************************************
5616 static u8 ecdump_regs
[256];
5618 static int ecdump_read(char *p
)
5624 len
+= sprintf(p
+ len
, "EC "
5625 " +00 +01 +02 +03 +04 +05 +06 +07"
5626 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5627 for (i
= 0; i
< 256; i
+= 16) {
5628 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5629 for (j
= 0; j
< 16; j
++) {
5630 if (!acpi_ec_read(i
+ j
, &v
))
5632 if (v
!= ecdump_regs
[i
+ j
])
5633 len
+= sprintf(p
+ len
, " *%02x", v
);
5635 len
+= sprintf(p
+ len
, " %02x", v
);
5636 ecdump_regs
[i
+ j
] = v
;
5638 len
+= sprintf(p
+ len
, "\n");
5643 /* These are way too dangerous to advertise openly... */
5645 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5646 " (<offset> is 00-ff, <value> is 00-ff)\n");
5647 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5648 " (<offset> is 00-ff, <value> is 0-255)\n");
5653 static int ecdump_write(char *buf
)
5658 while ((cmd
= next_cmd(&buf
))) {
5659 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5661 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5665 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5666 if (!acpi_ec_write(i
, v
))
5675 static struct ibm_struct ecdump_driver_data
= {
5677 .read
= ecdump_read
,
5678 .write
= ecdump_write
,
5679 .flags
.experimental
= 1,
5682 /*************************************************************************
5683 * Backlight/brightness subdriver
5686 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5689 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5690 * CMOS NVRAM byte 0x5E, bits 0-3.
5692 * EC HBRV (0x31) has the following layout
5693 * Bit 7: unknown function
5694 * Bit 6: unknown function
5695 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5696 * Bit 4: must be set to zero to avoid problems
5697 * Bit 3-0: backlight brightness level
5699 * brightness_get_raw returns status data in the HBRV layout
5701 * WARNING: The X61 has been verified to use HBRV for something else, so
5702 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5703 * testing on the very early *60 Lenovo models...
5707 TP_EC_BACKLIGHT
= 0x31,
5709 /* TP_EC_BACKLIGHT bitmasks */
5710 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5711 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5712 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5715 enum tpacpi_brightness_access_mode
{
5716 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
5717 TPACPI_BRGHT_MODE_EC
, /* EC control */
5718 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
5719 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
5720 TPACPI_BRGHT_MODE_MAX
5723 static struct backlight_device
*ibm_backlight_device
;
5725 static enum tpacpi_brightness_access_mode brightness_mode
=
5726 TPACPI_BRGHT_MODE_MAX
;
5728 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5730 static struct mutex brightness_mutex
;
5732 /* NVRAM brightness access,
5733 * call with brightness_mutex held! */
5734 static unsigned int tpacpi_brightness_nvram_get(void)
5738 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5739 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5740 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5741 lnvram
&= (tp_features
.bright_16levels
) ? 0x0f : 0x07;
5746 static void tpacpi_brightness_checkpoint_nvram(void)
5751 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
5754 vdbg_printk(TPACPI_DBG_BRGHT
,
5755 "trying to checkpoint backlight level to NVRAM...\n");
5757 if (mutex_lock_killable(&brightness_mutex
) < 0)
5760 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5762 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5763 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
5765 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5766 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
5767 /* NVRAM needs update */
5768 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
5769 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
5771 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
5772 dbg_printk(TPACPI_DBG_BRGHT
,
5773 "updated NVRAM backlight level to %u (0x%02x)\n",
5774 (unsigned int) lec
, (unsigned int) b_nvram
);
5776 vdbg_printk(TPACPI_DBG_BRGHT
,
5777 "NVRAM backlight level already is %u (0x%02x)\n",
5778 (unsigned int) lec
, (unsigned int) b_nvram
);
5781 mutex_unlock(&brightness_mutex
);
5785 /* call with brightness_mutex held! */
5786 static int tpacpi_brightness_get_raw(int *status
)
5790 switch (brightness_mode
) {
5791 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5792 *status
= tpacpi_brightness_nvram_get();
5794 case TPACPI_BRGHT_MODE_EC
:
5795 case TPACPI_BRGHT_MODE_ECNVRAM
:
5796 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5805 /* call with brightness_mutex held! */
5806 /* do NOT call with illegal backlight level value */
5807 static int tpacpi_brightness_set_ec(unsigned int value
)
5811 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5814 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
5815 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
5816 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
5822 /* call with brightness_mutex held! */
5823 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
5826 unsigned int current_value
, i
;
5828 current_value
= tpacpi_brightness_nvram_get();
5830 if (value
== current_value
)
5833 cmos_cmd
= (value
> current_value
) ?
5834 TP_CMOS_BRIGHTNESS_UP
:
5835 TP_CMOS_BRIGHTNESS_DOWN
;
5836 inc
= (value
> current_value
) ? 1 : -1;
5838 for (i
= current_value
; i
!= value
; i
+= inc
)
5839 if (issue_thinkpad_cmos_command(cmos_cmd
))
5845 /* May return EINTR which can always be mapped to ERESTARTSYS */
5846 static int brightness_set(unsigned int value
)
5850 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
5854 vdbg_printk(TPACPI_DBG_BRGHT
,
5855 "set backlight level to %d\n", value
);
5857 res
= mutex_lock_killable(&brightness_mutex
);
5861 switch (brightness_mode
) {
5862 case TPACPI_BRGHT_MODE_EC
:
5863 case TPACPI_BRGHT_MODE_ECNVRAM
:
5864 res
= tpacpi_brightness_set_ec(value
);
5866 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5867 res
= tpacpi_brightness_set_ucmsstep(value
);
5873 mutex_unlock(&brightness_mutex
);
5877 /* sysfs backlight class ----------------------------------------------- */
5879 static int brightness_update_status(struct backlight_device
*bd
)
5881 unsigned int level
=
5882 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
5883 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
5884 bd
->props
.brightness
: 0;
5886 dbg_printk(TPACPI_DBG_BRGHT
,
5887 "backlight: attempt to set level to %d\n",
5890 /* it is the backlight class's job (caller) to handle
5891 * EINTR and other errors properly */
5892 return brightness_set(level
);
5895 static int brightness_get(struct backlight_device
*bd
)
5899 res
= mutex_lock_killable(&brightness_mutex
);
5903 res
= tpacpi_brightness_get_raw(&status
);
5905 mutex_unlock(&brightness_mutex
);
5910 return status
& TP_EC_BACKLIGHT_LVLMSK
;
5913 static struct backlight_ops ibm_backlight_data
= {
5914 .get_brightness
= brightness_get
,
5915 .update_status
= brightness_update_status
,
5918 /* --------------------------------------------------------------------- */
5920 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
5924 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
5926 mutex_init(&brightness_mutex
);
5929 * We always attempt to detect acpi support, so as to switch
5930 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5931 * going to publish a backlight interface
5933 b
= tpacpi_check_std_acpi_brightness_support();
5936 if (acpi_video_backlight_support()) {
5937 if (brightness_enable
> 1) {
5938 printk(TPACPI_NOTICE
5939 "Standard ACPI backlight interface "
5940 "available, not loading native one.\n");
5942 } else if (brightness_enable
== 1) {
5943 printk(TPACPI_NOTICE
5944 "Backlight control force enabled, even if standard "
5945 "ACPI backlight interface is available\n");
5948 if (brightness_enable
> 1) {
5949 printk(TPACPI_NOTICE
5950 "Standard ACPI backlight interface not "
5951 "available, thinkpad_acpi native "
5952 "brightness control enabled\n");
5957 if (!brightness_enable
) {
5958 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
5959 "brightness support disabled by "
5960 "module parameter\n");
5966 "Unsupported brightness interface, "
5967 "please contact %s\n", TPACPI_MAIL
);
5971 tp_features
.bright_16levels
= 1;
5974 * Check for module parameter bogosity, note that we
5975 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5976 * able to detect "unspecified"
5978 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
5981 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5982 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
5983 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
5984 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) {
5986 * IBM models that define HBRV probably have
5987 * EC-based backlight level control
5989 if (acpi_evalf(ec_handle
, NULL
, "HBRV", "qd"))
5990 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5991 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
5993 /* all other IBM ThinkPads */
5994 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
5996 /* All Lenovo ThinkPads */
5997 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
5999 dbg_printk(TPACPI_DBG_BRGHT
,
6000 "selected brightness_mode=%d\n",
6005 if (thinkpad_id
.vendor
!= PCI_VENDOR_ID_IBM
&&
6006 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
6007 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
6010 if (tpacpi_brightness_get_raw(&b
) < 0)
6013 if (tp_features
.bright_16levels
)
6015 "detected a 16-level brightness capable ThinkPad\n");
6017 ibm_backlight_device
= backlight_device_register(
6018 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
6019 &ibm_backlight_data
);
6020 if (IS_ERR(ibm_backlight_device
)) {
6021 printk(TPACPI_ERR
"Could not register backlight device\n");
6022 return PTR_ERR(ibm_backlight_device
);
6024 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6025 "brightness is supported\n");
6027 ibm_backlight_device
->props
.max_brightness
=
6028 (tp_features
.bright_16levels
)? 15 : 7;
6029 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
6030 backlight_update_status(ibm_backlight_device
);
6035 static void brightness_suspend(pm_message_t state
)
6037 tpacpi_brightness_checkpoint_nvram();
6040 static void brightness_shutdown(void)
6042 tpacpi_brightness_checkpoint_nvram();
6045 static void brightness_exit(void)
6047 if (ibm_backlight_device
) {
6048 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
6049 "calling backlight_device_unregister()\n");
6050 backlight_device_unregister(ibm_backlight_device
);
6053 tpacpi_brightness_checkpoint_nvram();
6056 static int brightness_read(char *p
)
6061 level
= brightness_get(NULL
);
6063 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6065 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
6066 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
6067 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
6068 " (<level> is 0-%d)\n",
6069 (tp_features
.bright_16levels
) ? 15 : 7);
6075 static int brightness_write(char *buf
)
6080 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
6082 level
= brightness_get(NULL
);
6086 while ((cmd
= next_cmd(&buf
))) {
6087 if (strlencmp(cmd
, "up") == 0) {
6088 if (level
< max_level
)
6090 } else if (strlencmp(cmd
, "down") == 0) {
6093 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
6094 level
>= 0 && level
<= max_level
) {
6100 tpacpi_disclose_usertask("procfs brightness",
6101 "set level to %d\n", level
);
6104 * Now we know what the final level should be, so we try to set it.
6105 * Doing it this way makes the syscall restartable in case of EINTR
6107 rc
= brightness_set(level
);
6108 return (rc
== -EINTR
)? ERESTARTSYS
: rc
;
6111 static struct ibm_struct brightness_driver_data
= {
6112 .name
= "brightness",
6113 .read
= brightness_read
,
6114 .write
= brightness_write
,
6115 .exit
= brightness_exit
,
6116 .suspend
= brightness_suspend
,
6117 .shutdown
= brightness_shutdown
,
6120 /*************************************************************************
6124 static int volume_offset
= 0x30;
6126 static int volume_read(char *p
)
6131 if (!acpi_ec_read(volume_offset
, &level
)) {
6132 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6134 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
6135 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
6136 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
6137 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
6138 " (<level> is 0-15)\n");
6144 static int volume_write(char *buf
)
6146 int cmos_cmd
, inc
, i
;
6148 int new_level
, new_mute
;
6151 while ((cmd
= next_cmd(&buf
))) {
6152 if (!acpi_ec_read(volume_offset
, &level
))
6154 new_mute
= mute
= level
& 0x40;
6155 new_level
= level
= level
& 0xf;
6157 if (strlencmp(cmd
, "up") == 0) {
6161 new_level
= level
== 15 ? 15 : level
+ 1;
6162 } else if (strlencmp(cmd
, "down") == 0) {
6166 new_level
= level
== 0 ? 0 : level
- 1;
6167 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
6168 new_level
>= 0 && new_level
<= 15) {
6170 } else if (strlencmp(cmd
, "mute") == 0) {
6175 if (new_level
!= level
) {
6176 /* mute doesn't change */
6178 cmos_cmd
= (new_level
> level
) ?
6179 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
6180 inc
= new_level
> level
? 1 : -1;
6182 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
6183 !acpi_ec_write(volume_offset
, level
)))
6186 for (i
= level
; i
!= new_level
; i
+= inc
)
6187 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
6188 !acpi_ec_write(volume_offset
, i
+ inc
))
6192 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
6193 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
6198 if (new_mute
!= mute
) {
6199 /* level doesn't change */
6201 cmos_cmd
= (new_mute
) ?
6202 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
6204 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
6205 !acpi_ec_write(volume_offset
, level
+ new_mute
))
6213 static struct ibm_struct volume_driver_data
= {
6215 .read
= volume_read
,
6216 .write
= volume_write
,
6219 /*************************************************************************
6226 * TPACPI_FAN_RD_ACPI_GFAN:
6227 * ACPI GFAN method: returns fan level
6229 * see TPACPI_FAN_WR_ACPI_SFAN
6230 * EC 0x2f (HFSP) not available if GFAN exists
6232 * TPACPI_FAN_WR_ACPI_SFAN:
6233 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6235 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6238 * TPACPI_FAN_WR_TPEC:
6239 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6240 * Supported on almost all ThinkPads
6242 * Fan speed changes of any sort (including those caused by the
6243 * disengaged mode) are usually done slowly by the firmware as the
6244 * maximum ammount of fan duty cycle change per second seems to be
6247 * Reading is not available if GFAN exists.
6248 * Writing is not available if SFAN exists.
6251 * 7 automatic mode engaged;
6252 * (default operation mode of the ThinkPad)
6253 * fan level is ignored in this mode.
6254 * 6 full speed mode (takes precedence over bit 7);
6255 * not available on all thinkpads. May disable
6256 * the tachometer while the fan controller ramps up
6257 * the speed (which can take up to a few *minutes*).
6258 * Speeds up fan to 100% duty-cycle, which is far above
6259 * the standard RPM levels. It is not impossible that
6260 * it could cause hardware damage.
6261 * 5-3 unused in some models. Extra bits for fan level
6262 * in others, but still useless as all values above
6263 * 7 map to the same speed as level 7 in these models.
6264 * 2-0 fan level (0..7 usually)
6266 * 0x07 = max (set when temperatures critical)
6267 * Some ThinkPads may have other levels, see
6268 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6270 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6271 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6272 * does so, its initial value is meaningless (0x07).
6274 * For firmware bugs, refer to:
6275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6279 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6280 * Main fan tachometer reading (in RPM)
6282 * This register is present on all ThinkPads with a new-style EC, and
6283 * it is known not to be present on the A21m/e, and T22, as there is
6284 * something else in offset 0x84 according to the ACPI DSDT. Other
6285 * ThinkPads from this same time period (and earlier) probably lack the
6286 * tachometer as well.
6288 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6289 * was never fixed by IBM to report the EC firmware version string
6290 * probably support the tachometer (like the early X models), so
6291 * detecting it is quite hard. We need more data to know for sure.
6293 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6296 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6299 * For firmware bugs, refer to:
6300 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6304 * ThinkPad EC register 0x31 bit 0 (only on select models)
6306 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6307 * show the speed of the main fan. When bit 0 of EC register 0x31
6308 * is one, the tachometer registers show the speed of the auxiliary
6311 * Fan control seems to affect both fans, regardless of the state
6314 * So far, only the firmware for the X60/X61 non-tablet versions
6315 * seem to support this (firmware TP-7M).
6317 * TPACPI_FAN_WR_ACPI_FANS:
6318 * ThinkPad X31, X40, X41. Not available in the X60.
6320 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6321 * high speed. ACPI DSDT seems to map these three speeds to levels
6322 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6323 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6325 * The speeds are stored on handles
6326 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6328 * There are three default speed sets, acessible as handles:
6329 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6331 * ACPI DSDT switches which set is in use depending on various
6334 * TPACPI_FAN_WR_TPEC is also available and should be used to
6335 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6336 * but the ACPI tables just mention level 7.
6339 enum { /* Fan control constants */
6340 fan_status_offset
= 0x2f, /* EC register 0x2f */
6341 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6342 * 0x84 must be read before 0x85 */
6343 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
6344 bit 0 selects which fan is active */
6346 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
6347 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
6349 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
6352 enum fan_status_access_mode
{
6353 TPACPI_FAN_NONE
= 0, /* No fan status or control */
6354 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
6355 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6358 enum fan_control_access_mode
{
6359 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
6360 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
6361 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
6362 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
6365 enum fan_control_commands
{
6366 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
6367 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
6368 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
6369 * and also watchdog cmd */
6372 static int fan_control_allowed
;
6374 static enum fan_status_access_mode fan_status_access_mode
;
6375 static enum fan_control_access_mode fan_control_access_mode
;
6376 static enum fan_control_commands fan_control_commands
;
6378 static u8 fan_control_initial_status
;
6379 static u8 fan_control_desired_level
;
6380 static u8 fan_control_resume_level
;
6381 static int fan_watchdog_maxinterval
;
6383 static struct mutex fan_mutex
;
6385 static void fan_watchdog_fire(struct work_struct
*ignored
);
6386 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
6388 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
6389 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
6390 "\\FSPD", /* 600e/x, 770e, 770x */
6392 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
6393 "JFNS", /* 770x-JL */
6397 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6398 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6399 * be in auto mode (0x80).
6401 * This is corrected by any write to HFSP either by the driver, or
6404 * We assume 0x07 really means auto mode while this quirk is active,
6405 * as this is far more likely than the ThinkPad being in level 7,
6406 * which is only used by the firmware during thermal emergencies.
6408 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6409 * TP-70 (T43, R52), which are known to be buggy.
6412 static void fan_quirk1_setup(void)
6414 if (fan_control_initial_status
== 0x07) {
6415 printk(TPACPI_NOTICE
6416 "fan_init: initial fan status is unknown, "
6417 "assuming it is in auto mode\n");
6418 tp_features
.fan_ctrl_status_undef
= 1;
6422 static void fan_quirk1_handle(u8
*fan_status
)
6424 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
6425 if (*fan_status
!= fan_control_initial_status
) {
6426 /* something changed the HFSP regisnter since
6427 * driver init time, so it is not undefined
6429 tp_features
.fan_ctrl_status_undef
= 0;
6431 /* Return most likely status. In fact, it
6432 * might be the only possible status */
6433 *fan_status
= TP_EC_FAN_AUTO
;
6438 /* Select main fan on X60/X61, NOOP on others */
6439 static bool fan_select_fan1(void)
6441 if (tp_features
.second_fan
) {
6444 if (ec_read(fan_select_offset
, &val
) < 0)
6447 if (ec_write(fan_select_offset
, val
) < 0)
6453 /* Select secondary fan on X60/X61 */
6454 static bool fan_select_fan2(void)
6458 if (!tp_features
.second_fan
)
6461 if (ec_read(fan_select_offset
, &val
) < 0)
6464 if (ec_write(fan_select_offset
, val
) < 0)
6471 * Call with fan_mutex held
6473 static void fan_update_desired_level(u8 status
)
6476 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6478 fan_control_desired_level
= 7;
6480 fan_control_desired_level
= status
;
6484 static int fan_get_status(u8
*status
)
6489 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6491 switch (fan_status_access_mode
) {
6492 case TPACPI_FAN_RD_ACPI_GFAN
:
6493 /* 570, 600e/x, 770e, 770x */
6495 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
6503 case TPACPI_FAN_RD_TPEC
:
6504 /* all except 570, 600e/x, 770e, 770x */
6505 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
6508 if (likely(status
)) {
6510 fan_quirk1_handle(status
);
6522 static int fan_get_status_safe(u8
*status
)
6527 if (mutex_lock_killable(&fan_mutex
))
6528 return -ERESTARTSYS
;
6529 rc
= fan_get_status(&s
);
6531 fan_update_desired_level(s
);
6532 mutex_unlock(&fan_mutex
);
6540 static int fan_get_speed(unsigned int *speed
)
6544 switch (fan_status_access_mode
) {
6545 case TPACPI_FAN_RD_TPEC
:
6546 /* all except 570, 600e/x, 770e, 770x */
6547 if (unlikely(!fan_select_fan1()))
6549 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
6550 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
6554 *speed
= (hi
<< 8) | lo
;
6565 static int fan2_get_speed(unsigned int *speed
)
6570 switch (fan_status_access_mode
) {
6571 case TPACPI_FAN_RD_TPEC
:
6572 /* all except 570, 600e/x, 770e, 770x */
6573 if (unlikely(!fan_select_fan2()))
6575 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
6576 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
6577 fan_select_fan1(); /* play it safe */
6582 *speed
= (hi
<< 8) | lo
;
6593 static int fan_set_level(int level
)
6595 if (!fan_control_allowed
)
6598 switch (fan_control_access_mode
) {
6599 case TPACPI_FAN_WR_ACPI_SFAN
:
6600 if (level
>= 0 && level
<= 7) {
6601 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
6607 case TPACPI_FAN_WR_ACPI_FANS
:
6608 case TPACPI_FAN_WR_TPEC
:
6609 if (!(level
& TP_EC_FAN_AUTO
) &&
6610 !(level
& TP_EC_FAN_FULLSPEED
) &&
6611 ((level
< 0) || (level
> 7)))
6614 /* safety net should the EC not support AUTO
6615 * or FULLSPEED mode bits and just ignore them */
6616 if (level
& TP_EC_FAN_FULLSPEED
)
6617 level
|= 7; /* safety min speed 7 */
6618 else if (level
& TP_EC_FAN_AUTO
)
6619 level
|= 4; /* safety min speed 4 */
6621 if (!acpi_ec_write(fan_status_offset
, level
))
6624 tp_features
.fan_ctrl_status_undef
= 0;
6631 vdbg_printk(TPACPI_DBG_FAN
,
6632 "fan control: set fan control register to 0x%02x\n", level
);
6636 static int fan_set_level_safe(int level
)
6640 if (!fan_control_allowed
)
6643 if (mutex_lock_killable(&fan_mutex
))
6644 return -ERESTARTSYS
;
6646 if (level
== TPACPI_FAN_LAST_LEVEL
)
6647 level
= fan_control_desired_level
;
6649 rc
= fan_set_level(level
);
6651 fan_update_desired_level(level
);
6653 mutex_unlock(&fan_mutex
);
6657 static int fan_set_enable(void)
6662 if (!fan_control_allowed
)
6665 if (mutex_lock_killable(&fan_mutex
))
6666 return -ERESTARTSYS
;
6668 switch (fan_control_access_mode
) {
6669 case TPACPI_FAN_WR_ACPI_FANS
:
6670 case TPACPI_FAN_WR_TPEC
:
6671 rc
= fan_get_status(&s
);
6675 /* Don't go out of emergency fan mode */
6678 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
6681 if (!acpi_ec_write(fan_status_offset
, s
))
6684 tp_features
.fan_ctrl_status_undef
= 0;
6689 case TPACPI_FAN_WR_ACPI_SFAN
:
6690 rc
= fan_get_status(&s
);
6696 /* Set fan to at least level 4 */
6699 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
6709 mutex_unlock(&fan_mutex
);
6712 vdbg_printk(TPACPI_DBG_FAN
,
6713 "fan control: set fan control register to 0x%02x\n",
6718 static int fan_set_disable(void)
6722 if (!fan_control_allowed
)
6725 if (mutex_lock_killable(&fan_mutex
))
6726 return -ERESTARTSYS
;
6729 switch (fan_control_access_mode
) {
6730 case TPACPI_FAN_WR_ACPI_FANS
:
6731 case TPACPI_FAN_WR_TPEC
:
6732 if (!acpi_ec_write(fan_status_offset
, 0x00))
6735 fan_control_desired_level
= 0;
6736 tp_features
.fan_ctrl_status_undef
= 0;
6740 case TPACPI_FAN_WR_ACPI_SFAN
:
6741 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
6744 fan_control_desired_level
= 0;
6752 vdbg_printk(TPACPI_DBG_FAN
,
6753 "fan control: set fan control register to 0\n");
6755 mutex_unlock(&fan_mutex
);
6759 static int fan_set_speed(int speed
)
6763 if (!fan_control_allowed
)
6766 if (mutex_lock_killable(&fan_mutex
))
6767 return -ERESTARTSYS
;
6770 switch (fan_control_access_mode
) {
6771 case TPACPI_FAN_WR_ACPI_FANS
:
6772 if (speed
>= 0 && speed
<= 65535) {
6773 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
6774 speed
, speed
, speed
))
6784 mutex_unlock(&fan_mutex
);
6788 static void fan_watchdog_reset(void)
6790 static int fan_watchdog_active
;
6792 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
6795 if (fan_watchdog_active
)
6796 cancel_delayed_work(&fan_watchdog_task
);
6798 if (fan_watchdog_maxinterval
> 0 &&
6799 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
6800 fan_watchdog_active
= 1;
6801 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
6802 msecs_to_jiffies(fan_watchdog_maxinterval
6805 "failed to queue the fan watchdog, "
6806 "watchdog will not trigger\n");
6809 fan_watchdog_active
= 0;
6812 static void fan_watchdog_fire(struct work_struct
*ignored
)
6816 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
6819 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
6820 rc
= fan_set_enable();
6822 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
6823 "will try again later...\n", -rc
);
6824 /* reschedule for later */
6825 fan_watchdog_reset();
6830 * SYSFS fan layout: hwmon compatible (device)
6833 * 0: "disengaged" mode
6835 * 2: native EC "auto" mode (recommended, hardware default)
6837 * pwm*: set speed in manual mode, ignored otherwise.
6838 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6841 * fan*_input: tachometer reading, RPM
6844 * SYSFS fan layout: extensions
6846 * fan_watchdog (driver):
6847 * fan watchdog interval in seconds, 0 disables (default), max 120
6850 /* sysfs fan pwm1_enable ----------------------------------------------- */
6851 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
6852 struct device_attribute
*attr
,
6858 res
= fan_get_status_safe(&status
);
6862 if (status
& TP_EC_FAN_FULLSPEED
) {
6864 } else if (status
& TP_EC_FAN_AUTO
) {
6869 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
6872 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
6873 struct device_attribute
*attr
,
6874 const char *buf
, size_t count
)
6879 if (parse_strtoul(buf
, 2, &t
))
6882 tpacpi_disclose_usertask("hwmon pwm1_enable",
6883 "set fan mode to %lu\n", t
);
6887 level
= TP_EC_FAN_FULLSPEED
;
6890 level
= TPACPI_FAN_LAST_LEVEL
;
6893 level
= TP_EC_FAN_AUTO
;
6896 /* reserved for software-controlled auto mode */
6902 res
= fan_set_level_safe(level
);
6908 fan_watchdog_reset();
6913 static struct device_attribute dev_attr_fan_pwm1_enable
=
6914 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
6915 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
6917 /* sysfs fan pwm1 ------------------------------------------------------ */
6918 static ssize_t
fan_pwm1_show(struct device
*dev
,
6919 struct device_attribute
*attr
,
6925 res
= fan_get_status_safe(&status
);
6930 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
6931 status
= fan_control_desired_level
;
6936 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
6939 static ssize_t
fan_pwm1_store(struct device
*dev
,
6940 struct device_attribute
*attr
,
6941 const char *buf
, size_t count
)
6945 u8 status
, newlevel
;
6947 if (parse_strtoul(buf
, 255, &s
))
6950 tpacpi_disclose_usertask("hwmon pwm1",
6951 "set fan speed to %lu\n", s
);
6953 /* scale down from 0-255 to 0-7 */
6954 newlevel
= (s
>> 5) & 0x07;
6956 if (mutex_lock_killable(&fan_mutex
))
6957 return -ERESTARTSYS
;
6959 rc
= fan_get_status(&status
);
6960 if (!rc
&& (status
&
6961 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6962 rc
= fan_set_level(newlevel
);
6966 fan_update_desired_level(newlevel
);
6967 fan_watchdog_reset();
6971 mutex_unlock(&fan_mutex
);
6972 return (rc
)? rc
: count
;
6975 static struct device_attribute dev_attr_fan_pwm1
=
6976 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
6977 fan_pwm1_show
, fan_pwm1_store
);
6979 /* sysfs fan fan1_input ------------------------------------------------ */
6980 static ssize_t
fan_fan1_input_show(struct device
*dev
,
6981 struct device_attribute
*attr
,
6987 res
= fan_get_speed(&speed
);
6991 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
6994 static struct device_attribute dev_attr_fan_fan1_input
=
6995 __ATTR(fan1_input
, S_IRUGO
,
6996 fan_fan1_input_show
, NULL
);
6998 /* sysfs fan fan2_input ------------------------------------------------ */
6999 static ssize_t
fan_fan2_input_show(struct device
*dev
,
7000 struct device_attribute
*attr
,
7006 res
= fan2_get_speed(&speed
);
7010 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7013 static struct device_attribute dev_attr_fan_fan2_input
=
7014 __ATTR(fan2_input
, S_IRUGO
,
7015 fan_fan2_input_show
, NULL
);
7017 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7018 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
7021 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
7024 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
7025 const char *buf
, size_t count
)
7029 if (parse_strtoul(buf
, 120, &t
))
7032 if (!fan_control_allowed
)
7035 fan_watchdog_maxinterval
= t
;
7036 fan_watchdog_reset();
7038 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
7043 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
7044 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
7046 /* --------------------------------------------------------------------- */
7047 static struct attribute
*fan_attributes
[] = {
7048 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
7049 &dev_attr_fan_fan1_input
.attr
,
7050 NULL
, /* for fan2_input */
7054 static const struct attribute_group fan_attr_group
= {
7055 .attrs
= fan_attributes
,
7058 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7059 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7061 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7062 { .vendor = PCI_VENDOR_ID_IBM, \
7063 .bios = TPACPI_MATCH_ANY, \
7064 .ec = TPID(__id1, __id2), \
7065 .quirks = __quirks }
7067 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7068 { .vendor = PCI_VENDOR_ID_LENOVO, \
7069 .bios = TPACPI_MATCH_ANY, \
7070 .ec = TPID(__id1, __id2), \
7071 .quirks = __quirks }
7073 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
7074 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
7075 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
7076 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
7077 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
7078 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
7081 #undef TPACPI_FAN_QL
7082 #undef TPACPI_FAN_QI
7084 static int __init
fan_init(struct ibm_init_struct
*iibm
)
7087 unsigned long quirks
;
7089 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7090 "initializing fan subdriver\n");
7092 mutex_init(&fan_mutex
);
7093 fan_status_access_mode
= TPACPI_FAN_NONE
;
7094 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7095 fan_control_commands
= 0;
7096 fan_watchdog_maxinterval
= 0;
7097 tp_features
.fan_ctrl_status_undef
= 0;
7098 tp_features
.second_fan
= 0;
7099 fan_control_desired_level
= 7;
7101 TPACPI_ACPIHANDLE_INIT(fans
);
7102 TPACPI_ACPIHANDLE_INIT(gfan
);
7103 TPACPI_ACPIHANDLE_INIT(sfan
);
7105 quirks
= tpacpi_check_quirks(fan_quirk_table
,
7106 ARRAY_SIZE(fan_quirk_table
));
7109 /* 570, 600e/x, 770e, 770x */
7110 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
7112 /* all other ThinkPads: note that even old-style
7113 * ThinkPad ECs supports the fan control register */
7114 if (likely(acpi_ec_read(fan_status_offset
,
7115 &fan_control_initial_status
))) {
7116 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
7117 if (quirks
& TPACPI_FAN_Q1
)
7119 if (quirks
& TPACPI_FAN_2FAN
) {
7120 tp_features
.second_fan
= 1;
7121 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7122 "secondary fan support enabled\n");
7126 "ThinkPad ACPI EC access misbehaving, "
7127 "fan status and control unavailable\n");
7134 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
7135 fan_control_commands
|=
7136 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
7139 /* gfan without sfan means no fan control */
7140 /* all other models implement TP EC 0x2f control */
7144 fan_control_access_mode
=
7145 TPACPI_FAN_WR_ACPI_FANS
;
7146 fan_control_commands
|=
7147 TPACPI_FAN_CMD_SPEED
|
7148 TPACPI_FAN_CMD_LEVEL
|
7149 TPACPI_FAN_CMD_ENABLE
;
7151 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
7152 fan_control_commands
|=
7153 TPACPI_FAN_CMD_LEVEL
|
7154 TPACPI_FAN_CMD_ENABLE
;
7159 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7160 "fan is %s, modes %d, %d\n",
7161 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
7162 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
7163 fan_status_access_mode
, fan_control_access_mode
);
7165 /* fan control master switch */
7166 if (!fan_control_allowed
) {
7167 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7168 fan_control_commands
= 0;
7169 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7170 "fan control features disabled by parameter\n");
7173 /* update fan_control_desired_level */
7174 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
7175 fan_get_status_safe(NULL
);
7177 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
7178 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
7179 if (tp_features
.second_fan
) {
7180 /* attach second fan tachometer */
7181 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
7182 &dev_attr_fan_fan2_input
.attr
;
7184 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7189 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
7190 &driver_attr_fan_watchdog
);
7192 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7201 static void fan_exit(void)
7203 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
7204 "cancelling any pending fan watchdog tasks\n");
7206 /* FIXME: can we really do this unconditionally? */
7207 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
7208 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
7209 &driver_attr_fan_watchdog
);
7211 cancel_delayed_work(&fan_watchdog_task
);
7212 flush_workqueue(tpacpi_wq
);
7215 static void fan_suspend(pm_message_t state
)
7219 if (!fan_control_allowed
)
7222 /* Store fan status in cache */
7223 fan_control_resume_level
= 0;
7224 rc
= fan_get_status_safe(&fan_control_resume_level
);
7226 printk(TPACPI_NOTICE
7227 "failed to read fan level for later "
7228 "restore during resume: %d\n", rc
);
7230 /* if it is undefined, don't attempt to restore it.
7232 if (tp_features
.fan_ctrl_status_undef
)
7233 fan_control_resume_level
= 0;
7236 static void fan_resume(void)
7238 u8 current_level
= 7;
7239 bool do_set
= false;
7242 /* DSDT *always* updates status on resume */
7243 tp_features
.fan_ctrl_status_undef
= 0;
7245 if (!fan_control_allowed
||
7246 !fan_control_resume_level
||
7247 (fan_get_status_safe(¤t_level
) < 0))
7250 switch (fan_control_access_mode
) {
7251 case TPACPI_FAN_WR_ACPI_SFAN
:
7252 /* never decrease fan level */
7253 do_set
= (fan_control_resume_level
> current_level
);
7255 case TPACPI_FAN_WR_ACPI_FANS
:
7256 case TPACPI_FAN_WR_TPEC
:
7257 /* never decrease fan level, scale is:
7258 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7260 * We expect the firmware to set either 7 or AUTO, but we
7261 * handle FULLSPEED out of paranoia.
7263 * So, we can safely only restore FULLSPEED or 7, anything
7264 * else could slow the fan. Restoring AUTO is useless, at
7265 * best that's exactly what the DSDT already set (it is the
7268 * Always keep in mind that the DSDT *will* have set the
7269 * fans to what the vendor supposes is the best level. We
7270 * muck with it only to speed the fan up.
7272 if (fan_control_resume_level
!= 7 &&
7273 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
7276 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
7277 (current_level
!= fan_control_resume_level
);
7283 printk(TPACPI_NOTICE
7284 "restoring fan level to 0x%02x\n",
7285 fan_control_resume_level
);
7286 rc
= fan_set_level_safe(fan_control_resume_level
);
7288 printk(TPACPI_NOTICE
7289 "failed to restore fan level: %d\n", rc
);
7293 static int fan_read(char *p
)
7298 unsigned int speed
= 0;
7300 switch (fan_status_access_mode
) {
7301 case TPACPI_FAN_RD_ACPI_GFAN
:
7302 /* 570, 600e/x, 770e, 770x */
7303 rc
= fan_get_status_safe(&status
);
7307 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
7309 (status
!= 0) ? "enabled" : "disabled", status
);
7312 case TPACPI_FAN_RD_TPEC
:
7313 /* all except 570, 600e/x, 770e, 770x */
7314 rc
= fan_get_status_safe(&status
);
7318 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
7319 (status
!= 0) ? "enabled" : "disabled");
7321 rc
= fan_get_speed(&speed
);
7325 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
7327 if (status
& TP_EC_FAN_FULLSPEED
)
7328 /* Disengaged mode takes precedence */
7329 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
7330 else if (status
& TP_EC_FAN_AUTO
)
7331 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
7333 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
7336 case TPACPI_FAN_NONE
:
7338 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
7341 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
7342 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
7344 switch (fan_control_access_mode
) {
7345 case TPACPI_FAN_WR_ACPI_SFAN
:
7346 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
7350 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
7351 "auto, disengaged, full-speed)\n");
7356 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
7357 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
7358 "commands:\twatchdog <timeout> (<timeout> "
7359 "is 0 (off), 1-120 (seconds))\n");
7361 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
7362 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
7363 " (<speed> is 0-65535)\n");
7368 static int fan_write_cmd_level(const char *cmd
, int *rc
)
7372 if (strlencmp(cmd
, "level auto") == 0)
7373 level
= TP_EC_FAN_AUTO
;
7374 else if ((strlencmp(cmd
, "level disengaged") == 0) |
7375 (strlencmp(cmd
, "level full-speed") == 0))
7376 level
= TP_EC_FAN_FULLSPEED
;
7377 else if (sscanf(cmd
, "level %d", &level
) != 1)
7380 *rc
= fan_set_level_safe(level
);
7382 printk(TPACPI_ERR
"level command accepted for unsupported "
7383 "access mode %d", fan_control_access_mode
);
7385 tpacpi_disclose_usertask("procfs fan",
7386 "set level to %d\n", level
);
7391 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
7393 if (strlencmp(cmd
, "enable") != 0)
7396 *rc
= fan_set_enable();
7398 printk(TPACPI_ERR
"enable command accepted for unsupported "
7399 "access mode %d", fan_control_access_mode
);
7401 tpacpi_disclose_usertask("procfs fan", "enable\n");
7406 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
7408 if (strlencmp(cmd
, "disable") != 0)
7411 *rc
= fan_set_disable();
7413 printk(TPACPI_ERR
"disable command accepted for unsupported "
7414 "access mode %d", fan_control_access_mode
);
7416 tpacpi_disclose_usertask("procfs fan", "disable\n");
7421 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
7426 * Support speed <low> <medium> <high> ? */
7428 if (sscanf(cmd
, "speed %d", &speed
) != 1)
7431 *rc
= fan_set_speed(speed
);
7433 printk(TPACPI_ERR
"speed command accepted for unsupported "
7434 "access mode %d", fan_control_access_mode
);
7436 tpacpi_disclose_usertask("procfs fan",
7437 "set speed to %d\n", speed
);
7442 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
7446 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
7449 if (interval
< 0 || interval
> 120)
7452 fan_watchdog_maxinterval
= interval
;
7453 tpacpi_disclose_usertask("procfs fan",
7454 "set watchdog timer to %d\n",
7461 static int fan_write(char *buf
)
7466 while (!rc
&& (cmd
= next_cmd(&buf
))) {
7467 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
7468 fan_write_cmd_level(cmd
, &rc
)) &&
7469 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
7470 (fan_write_cmd_enable(cmd
, &rc
) ||
7471 fan_write_cmd_disable(cmd
, &rc
) ||
7472 fan_write_cmd_watchdog(cmd
, &rc
))) &&
7473 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
7474 fan_write_cmd_speed(cmd
, &rc
))
7478 fan_watchdog_reset();
7484 static struct ibm_struct fan_driver_data
= {
7489 .suspend
= fan_suspend
,
7490 .resume
= fan_resume
,
7493 /****************************************************************************
7494 ****************************************************************************
7498 ****************************************************************************
7499 ****************************************************************************/
7501 /* sysfs name ---------------------------------------------------------- */
7502 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
7503 struct device_attribute
*attr
,
7506 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
7509 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
7510 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
7512 /* --------------------------------------------------------------------- */
7515 static struct proc_dir_entry
*proc_dir
;
7518 * Module and infrastructure proble, init and exit handling
7521 static int force_load
;
7523 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7524 static const char * __init
str_supported(int is_supported
)
7526 static char text_unsupported
[] __initdata
= "not supported";
7528 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
7530 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7532 static void ibm_exit(struct ibm_struct
*ibm
)
7534 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
7536 list_del_init(&ibm
->all_drivers
);
7538 if (ibm
->flags
.acpi_notify_installed
) {
7539 dbg_printk(TPACPI_DBG_EXIT
,
7540 "%s: acpi_remove_notify_handler\n", ibm
->name
);
7542 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
7544 dispatch_acpi_notify
);
7545 ibm
->flags
.acpi_notify_installed
= 0;
7546 ibm
->flags
.acpi_notify_installed
= 0;
7549 if (ibm
->flags
.proc_created
) {
7550 dbg_printk(TPACPI_DBG_EXIT
,
7551 "%s: remove_proc_entry\n", ibm
->name
);
7552 remove_proc_entry(ibm
->name
, proc_dir
);
7553 ibm
->flags
.proc_created
= 0;
7556 if (ibm
->flags
.acpi_driver_registered
) {
7557 dbg_printk(TPACPI_DBG_EXIT
,
7558 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
7560 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
7561 kfree(ibm
->acpi
->driver
);
7562 ibm
->acpi
->driver
= NULL
;
7563 ibm
->flags
.acpi_driver_registered
= 0;
7566 if (ibm
->flags
.init_called
&& ibm
->exit
) {
7568 ibm
->flags
.init_called
= 0;
7571 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
7574 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
7577 struct ibm_struct
*ibm
= iibm
->data
;
7578 struct proc_dir_entry
*entry
;
7580 BUG_ON(ibm
== NULL
);
7582 INIT_LIST_HEAD(&ibm
->all_drivers
);
7584 if (ibm
->flags
.experimental
&& !experimental
)
7587 dbg_printk(TPACPI_DBG_INIT
,
7588 "probing for %s\n", ibm
->name
);
7591 ret
= iibm
->init(iibm
);
7593 return 0; /* probe failed */
7597 ibm
->flags
.init_called
= 1;
7601 if (ibm
->acpi
->hid
) {
7602 ret
= register_tpacpi_subdriver(ibm
);
7607 if (ibm
->acpi
->notify
) {
7608 ret
= setup_acpi_notify(ibm
);
7609 if (ret
== -ENODEV
) {
7610 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
7620 dbg_printk(TPACPI_DBG_INIT
,
7621 "%s installed\n", ibm
->name
);
7624 entry
= create_proc_entry(ibm
->name
,
7625 S_IFREG
| S_IRUGO
| S_IWUSR
,
7628 printk(TPACPI_ERR
"unable to create proc entry %s\n",
7634 entry
->read_proc
= &dispatch_procfs_read
;
7636 entry
->write_proc
= &dispatch_procfs_write
;
7637 ibm
->flags
.proc_created
= 1;
7640 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
7645 dbg_printk(TPACPI_DBG_INIT
,
7646 "%s: at error exit path with result %d\n",
7650 return (ret
< 0)? ret
: 0;
7655 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
7657 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
7660 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7661 static bool __pure __init
tpacpi_is_valid_fw_id(const char* const s
,
7664 return s
&& strlen(s
) >= 8 &&
7665 tpacpi_is_fw_digit(s
[0]) &&
7666 tpacpi_is_fw_digit(s
[1]) &&
7667 s
[2] == t
&& s
[3] == 'T' &&
7668 tpacpi_is_fw_digit(s
[4]) &&
7669 tpacpi_is_fw_digit(s
[5]) &&
7670 s
[6] == 'W' && s
[7] == 'W';
7673 /* returns 0 - probe ok, or < 0 - probe error.
7674 * Probe ok doesn't mean thinkpad found.
7675 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7676 static int __must_check __init
get_thinkpad_model_data(
7677 struct thinkpad_id_data
*tp
)
7679 const struct dmi_device
*dev
= NULL
;
7680 char ec_fw_string
[18];
7686 memset(tp
, 0, sizeof(*tp
));
7688 if (dmi_name_in_vendors("IBM"))
7689 tp
->vendor
= PCI_VENDOR_ID_IBM
;
7690 else if (dmi_name_in_vendors("LENOVO"))
7691 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
7695 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
7696 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
7697 if (s
&& !tp
->bios_version_str
)
7700 /* Really ancient ThinkPad 240X will fail this, which is fine */
7701 if (!tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E'))
7704 tp
->bios_model
= tp
->bios_version_str
[0]
7705 | (tp
->bios_version_str
[1] << 8);
7706 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
7707 | tp
->bios_version_str
[5];
7710 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7711 * X32 or newer, all Z series; Some models must have an
7712 * up-to-date BIOS or they will not be detected.
7714 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7716 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
7717 if (sscanf(dev
->name
,
7718 "IBM ThinkPad Embedded Controller -[%17c",
7719 ec_fw_string
) == 1) {
7720 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
7721 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
7723 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
7724 if (!tp
->ec_version_str
)
7727 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
7728 tp
->ec_model
= ec_fw_string
[0]
7729 | (ec_fw_string
[1] << 8);
7730 tp
->ec_release
= (ec_fw_string
[4] << 8)
7733 printk(TPACPI_NOTICE
7734 "ThinkPad firmware release %s "
7735 "doesn't match the known patterns\n",
7737 printk(TPACPI_NOTICE
7738 "please report this to %s\n",
7745 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
7746 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
7747 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
7752 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
7753 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
7754 if (s
&& !tp
->nummodel_str
)
7760 static int __init
probe_for_thinkpad(void)
7768 * Non-ancient models have better DMI tagging, but very old models
7771 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
7773 /* ec is required because many other handles are relative to it */
7774 TPACPI_ACPIHANDLE_INIT(ec
);
7778 "Not yet supported ThinkPad detected!\n");
7783 * Risks a regression on very old machines, but reduces potential
7784 * false positives a damn great deal
7787 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
7789 if (!is_thinkpad
&& !force_load
)
7796 /* Module init, exit, parameters */
7798 static struct ibm_init_struct ibms_init
[] __initdata
= {
7800 .init
= thinkpad_acpi_driver_init
,
7801 .data
= &thinkpad_acpi_driver_data
,
7804 .init
= hotkey_init
,
7805 .data
= &hotkey_driver_data
,
7808 .init
= bluetooth_init
,
7809 .data
= &bluetooth_driver_data
,
7813 .data
= &wan_driver_data
,
7817 .data
= &uwb_driver_data
,
7819 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7822 .data
= &video_driver_data
,
7827 .data
= &light_driver_data
,
7829 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7832 .data
= &dock_driver_data
[0],
7836 .data
= &dock_driver_data
[1],
7839 #ifdef CONFIG_THINKPAD_ACPI_BAY
7842 .data
= &bay_driver_data
,
7847 .data
= &cmos_driver_data
,
7851 .data
= &led_driver_data
,
7855 .data
= &beep_driver_data
,
7858 .init
= thermal_init
,
7859 .data
= &thermal_driver_data
,
7862 .data
= &ecdump_driver_data
,
7865 .init
= brightness_init
,
7866 .data
= &brightness_driver_data
,
7869 .data
= &volume_driver_data
,
7873 .data
= &fan_driver_data
,
7877 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
7880 struct ibm_struct
*ibm
;
7882 if (!kp
|| !kp
->name
|| !val
)
7885 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7886 ibm
= ibms_init
[i
].data
;
7887 WARN_ON(ibm
== NULL
);
7889 if (!ibm
|| !ibm
->name
)
7892 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
7893 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
7895 strcpy(ibms_init
[i
].param
, val
);
7896 strcat(ibms_init
[i
].param
, ",");
7904 module_param(experimental
, int, 0);
7905 MODULE_PARM_DESC(experimental
,
7906 "Enables experimental features when non-zero");
7908 module_param_named(debug
, dbg_level
, uint
, 0);
7909 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
7911 module_param(force_load
, bool, 0);
7912 MODULE_PARM_DESC(force_load
,
7913 "Attempts to load the driver even on a "
7914 "mis-identified ThinkPad when true");
7916 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
7917 MODULE_PARM_DESC(fan_control
,
7918 "Enables setting fan parameters features when true");
7920 module_param_named(brightness_mode
, brightness_mode
, uint
, 0);
7921 MODULE_PARM_DESC(brightness_mode
,
7922 "Selects brightness control strategy: "
7923 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7925 module_param(brightness_enable
, uint
, 0);
7926 MODULE_PARM_DESC(brightness_enable
,
7927 "Enables backlight control when 1, disables when 0");
7929 module_param(hotkey_report_mode
, uint
, 0);
7930 MODULE_PARM_DESC(hotkey_report_mode
,
7931 "used for backwards compatibility with userspace, "
7932 "see documentation");
7934 #define TPACPI_PARAM(feature) \
7935 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7936 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7937 "at module load, see documentation")
7939 TPACPI_PARAM(hotkey
);
7940 TPACPI_PARAM(bluetooth
);
7941 TPACPI_PARAM(video
);
7942 TPACPI_PARAM(light
);
7943 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7946 #ifdef CONFIG_THINKPAD_ACPI_BAY
7948 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7952 TPACPI_PARAM(ecdump
);
7953 TPACPI_PARAM(brightness
);
7954 TPACPI_PARAM(volume
);
7957 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7958 module_param(dbg_wlswemul
, uint
, 0);
7959 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
7960 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
7961 MODULE_PARM_DESC(wlsw_state
,
7962 "Initial state of the emulated WLSW switch");
7964 module_param(dbg_bluetoothemul
, uint
, 0);
7965 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
7966 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
7967 MODULE_PARM_DESC(bluetooth_state
,
7968 "Initial state of the emulated bluetooth switch");
7970 module_param(dbg_wwanemul
, uint
, 0);
7971 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
7972 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
7973 MODULE_PARM_DESC(wwan_state
,
7974 "Initial state of the emulated WWAN switch");
7976 module_param(dbg_uwbemul
, uint
, 0);
7977 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
7978 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
7979 MODULE_PARM_DESC(uwb_state
,
7980 "Initial state of the emulated UWB switch");
7983 static void thinkpad_acpi_module_exit(void)
7985 struct ibm_struct
*ibm
, *itmp
;
7987 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
7989 list_for_each_entry_safe_reverse(ibm
, itmp
,
7990 &tpacpi_all_drivers
,
7995 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
7997 if (tpacpi_inputdev
) {
7998 if (tp_features
.input_device_registered
)
7999 input_unregister_device(tpacpi_inputdev
);
8001 input_free_device(tpacpi_inputdev
);
8005 hwmon_device_unregister(tpacpi_hwmon
);
8007 if (tp_features
.sensors_pdev_attrs_registered
)
8008 device_remove_file(&tpacpi_sensors_pdev
->dev
,
8009 &dev_attr_thinkpad_acpi_pdev_name
);
8010 if (tpacpi_sensors_pdev
)
8011 platform_device_unregister(tpacpi_sensors_pdev
);
8013 platform_device_unregister(tpacpi_pdev
);
8015 if (tp_features
.sensors_pdrv_attrs_registered
)
8016 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
8017 if (tp_features
.platform_drv_attrs_registered
)
8018 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
8020 if (tp_features
.sensors_pdrv_registered
)
8021 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
8023 if (tp_features
.platform_drv_registered
)
8024 platform_driver_unregister(&tpacpi_pdriver
);
8027 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
8030 destroy_workqueue(tpacpi_wq
);
8032 kfree(thinkpad_id
.bios_version_str
);
8033 kfree(thinkpad_id
.ec_version_str
);
8034 kfree(thinkpad_id
.model_str
);
8038 static int __init
thinkpad_acpi_module_init(void)
8042 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
8044 /* Parameter checking */
8045 if (hotkey_report_mode
> 2)
8048 /* Driver-level probe */
8050 ret
= get_thinkpad_model_data(&thinkpad_id
);
8053 "unable to get DMI data: %d\n", ret
);
8054 thinkpad_acpi_module_exit();
8057 ret
= probe_for_thinkpad();
8059 thinkpad_acpi_module_exit();
8063 /* Driver initialization */
8065 TPACPI_ACPIHANDLE_INIT(ecrd
);
8066 TPACPI_ACPIHANDLE_INIT(ecwr
);
8068 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
8070 thinkpad_acpi_module_exit();
8074 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
8077 "unable to create proc dir " TPACPI_PROC_DIR
);
8078 thinkpad_acpi_module_exit();
8082 ret
= platform_driver_register(&tpacpi_pdriver
);
8085 "unable to register main platform driver\n");
8086 thinkpad_acpi_module_exit();
8089 tp_features
.platform_drv_registered
= 1;
8091 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
8094 "unable to register hwmon platform driver\n");
8095 thinkpad_acpi_module_exit();
8098 tp_features
.sensors_pdrv_registered
= 1;
8100 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
8102 tp_features
.platform_drv_attrs_registered
= 1;
8103 ret
= tpacpi_create_driver_attributes(
8104 &tpacpi_hwmon_pdriver
.driver
);
8108 "unable to create sysfs driver attributes\n");
8109 thinkpad_acpi_module_exit();
8112 tp_features
.sensors_pdrv_attrs_registered
= 1;
8115 /* Device initialization */
8116 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
8118 if (IS_ERR(tpacpi_pdev
)) {
8119 ret
= PTR_ERR(tpacpi_pdev
);
8121 printk(TPACPI_ERR
"unable to register platform device\n");
8122 thinkpad_acpi_module_exit();
8125 tpacpi_sensors_pdev
= platform_device_register_simple(
8126 TPACPI_HWMON_DRVR_NAME
,
8128 if (IS_ERR(tpacpi_sensors_pdev
)) {
8129 ret
= PTR_ERR(tpacpi_sensors_pdev
);
8130 tpacpi_sensors_pdev
= NULL
;
8132 "unable to register hwmon platform device\n");
8133 thinkpad_acpi_module_exit();
8136 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
8137 &dev_attr_thinkpad_acpi_pdev_name
);
8140 "unable to create sysfs hwmon device attributes\n");
8141 thinkpad_acpi_module_exit();
8144 tp_features
.sensors_pdev_attrs_registered
= 1;
8145 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
8146 if (IS_ERR(tpacpi_hwmon
)) {
8147 ret
= PTR_ERR(tpacpi_hwmon
);
8148 tpacpi_hwmon
= NULL
;
8149 printk(TPACPI_ERR
"unable to register hwmon device\n");
8150 thinkpad_acpi_module_exit();
8153 mutex_init(&tpacpi_inputdev_send_mutex
);
8154 tpacpi_inputdev
= input_allocate_device();
8155 if (!tpacpi_inputdev
) {
8156 printk(TPACPI_ERR
"unable to allocate input device\n");
8157 thinkpad_acpi_module_exit();
8160 /* Prepare input device, but don't register */
8161 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
8162 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
8163 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
8164 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
8165 thinkpad_id
.vendor
:
8167 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
8168 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
8170 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8171 ret
= ibm_init(&ibms_init
[i
]);
8172 if (ret
>= 0 && *ibms_init
[i
].param
)
8173 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
8175 thinkpad_acpi_module_exit();
8179 ret
= input_register_device(tpacpi_inputdev
);
8181 printk(TPACPI_ERR
"unable to register input device\n");
8182 thinkpad_acpi_module_exit();
8185 tp_features
.input_device_registered
= 1;
8188 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
8192 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
8195 * This will autoload the driver in almost every ThinkPad
8196 * in widespread use.
8198 * Only _VERY_ old models, like the 240, 240x and 570 lack
8199 * the HKEY event interface.
8201 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
8204 * DMI matching for module autoloading
8206 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8207 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8209 * Only models listed in thinkwiki will be supported, so add yours
8210 * if it is not there yet.
8212 #define IBM_BIOS_MODULE_ALIAS(__type) \
8213 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8215 /* Ancient thinkpad BIOSes have to be identified by
8216 * BIOS type or model number, and there are far less
8217 * BIOS types than model numbers... */
8218 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8220 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8221 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8222 MODULE_DESCRIPTION(TPACPI_DESC
);
8223 MODULE_VERSION(TPACPI_VERSION
);
8224 MODULE_LICENSE("GPL");
8226 module_init(thinkpad_acpi_module_init
);
8227 module_exit(thinkpad_acpi_module_exit
);