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.22"
25 #define TPACPI_SYSFS_VERSION 0x020300
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
195 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
196 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
197 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
200 /****************************************************************************
201 * Driver-wide structs and misc. variables
206 struct tp_acpi_drv_struct
{
207 const struct acpi_device_id
*hid
;
208 struct acpi_driver
*driver
;
210 void (*notify
) (struct ibm_struct
*, u32
);
213 struct acpi_device
*device
;
219 int (*read
) (char *);
220 int (*write
) (char *);
222 void (*resume
) (void);
223 void (*suspend
) (pm_message_t state
);
224 void (*shutdown
) (void);
226 struct list_head all_drivers
;
228 struct tp_acpi_drv_struct
*acpi
;
231 u8 acpi_driver_registered
:1;
232 u8 acpi_notify_installed
:1;
239 struct ibm_init_struct
{
242 int (*init
) (struct ibm_init_struct
*);
243 struct ibm_struct
*data
;
247 #ifdef CONFIG_THINKPAD_ACPI_BAY
260 u32 bright_16levels
:1;
261 u32 bright_acpimode
:1;
264 u32 fan_ctrl_status_undef
:1;
265 u32 input_device_registered
:1;
266 u32 platform_drv_registered
:1;
267 u32 platform_drv_attrs_registered
:1;
268 u32 sensors_pdrv_registered
:1;
269 u32 sensors_pdrv_attrs_registered
:1;
270 u32 sensors_pdev_attrs_registered
:1;
271 u32 hotkey_poll_active
:1;
275 u16 hotkey_mask_ff
:1;
276 u16 bright_cmos_ec_unsync
:1;
279 struct thinkpad_id_data
{
280 unsigned int vendor
; /* ThinkPad vendor:
281 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
284 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
286 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
289 char *model_str
; /* ThinkPad T43 */
290 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
292 static struct thinkpad_id_data thinkpad_id
;
295 TPACPI_LIFE_INIT
= 0,
300 static int experimental
;
301 static u32 dbg_level
;
303 static struct workqueue_struct
*tpacpi_wq
;
305 /* Special LED class that can defer work */
306 struct tpacpi_led_classdev
{
307 struct led_classdev led_classdev
;
308 struct work_struct work
;
309 enum led_brightness new_brightness
;
313 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
314 static int dbg_wlswemul
;
315 static int tpacpi_wlsw_emulstate
;
316 static int dbg_bluetoothemul
;
317 static int tpacpi_bluetooth_emulstate
;
318 static int dbg_wwanemul
;
319 static int tpacpi_wwan_emulstate
;
320 static int dbg_uwbemul
;
321 static int tpacpi_uwb_emulstate
;
325 /*************************************************************************
329 #define dbg_printk(a_dbg_level, format, arg...) \
330 do { if (dbg_level & (a_dbg_level)) \
331 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
334 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
335 #define vdbg_printk dbg_printk
336 static const char *str_supported(int is_supported
);
338 #define vdbg_printk(a_dbg_level, format, arg...) \
342 static void tpacpi_log_usertask(const char * const what
)
344 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
345 what
, task_tgid_vnr(current
));
348 #define tpacpi_disclose_usertask(what, format, arg...) \
351 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
352 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
353 printk(TPACPI_DEBUG "%s: PID %d: " format, \
354 what, task_tgid_vnr(current), ## arg); \
358 /****************************************************************************
359 ****************************************************************************
361 * ACPI Helpers and device model
363 ****************************************************************************
364 ****************************************************************************/
366 /*************************************************************************
370 static acpi_handle root_handle
;
372 #define TPACPI_HANDLE(object, parent, paths...) \
373 static acpi_handle object##_handle; \
374 static acpi_handle *object##_parent = &parent##_handle; \
375 static char *object##_path; \
376 static char *object##_paths[] = { paths }
378 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
379 "\\_SB.PCI.ISA.EC", /* 570 */
380 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
381 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
382 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
383 "\\_SB.PCI0.ICH3.EC0", /* R31 */
384 "\\_SB.PCI0.LPC.EC", /* all others */
387 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
388 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
390 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
392 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
393 "\\CMS", /* R40, R40e */
396 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
397 "^HKEY", /* R30, R31 */
398 "HKEY", /* all others */
401 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
402 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
403 "\\_SB.PCI0.VID0", /* 770e */
404 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
405 "\\_SB.PCI0.AGP.VID", /* all others */
409 /*************************************************************************
413 static int acpi_evalf(acpi_handle handle
,
414 void *res
, char *method
, char *fmt
, ...)
417 struct acpi_object_list params
;
418 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
419 struct acpi_buffer result
, *resultp
;
420 union acpi_object out_obj
;
428 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
441 params
.pointer
= &in_objs
[0];
448 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
449 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
451 /* add more types as needed */
453 printk(TPACPI_ERR
"acpi_evalf() called "
454 "with invalid format character '%c'\n", c
);
460 if (res_type
!= 'v') {
461 result
.length
= sizeof(out_obj
);
462 result
.pointer
= &out_obj
;
467 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
472 *(int *)res
= out_obj
.integer
.value
;
473 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
476 success
= status
== AE_OK
;
478 /* add more types as needed */
480 printk(TPACPI_ERR
"acpi_evalf() called "
481 "with invalid format character '%c'\n", res_type
);
485 if (!success
&& !quiet
)
486 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
487 method
, fmt0
, status
);
492 static int acpi_ec_read(int i
, u8
*p
)
497 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
501 if (ec_read(i
, p
) < 0)
508 static int acpi_ec_write(int i
, u8 v
)
511 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
514 if (ec_write(i
, v
) < 0)
521 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
522 static int _sta(acpi_handle handle
)
526 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
533 static int issue_thinkpad_cmos_command(int cmos_cmd
)
538 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
544 /*************************************************************************
548 #define TPACPI_ACPIHANDLE_INIT(object) \
549 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
550 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
552 static void drv_acpi_handle_init(char *name
,
553 acpi_handle
*handle
, acpi_handle parent
,
554 char **paths
, int num_paths
, char **path
)
559 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
562 for (i
= 0; i
< num_paths
; i
++) {
563 status
= acpi_get_handle(parent
, paths
[i
], handle
);
564 if (ACPI_SUCCESS(status
)) {
566 dbg_printk(TPACPI_DBG_INIT
,
567 "Found ACPI handle %s for %s\n",
573 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
578 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
580 struct ibm_struct
*ibm
= data
;
582 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
585 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
588 ibm
->acpi
->notify(ibm
, event
);
591 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
598 if (!*ibm
->acpi
->handle
)
601 vdbg_printk(TPACPI_DBG_INIT
,
602 "setting up ACPI notify for %s\n", ibm
->name
);
604 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
606 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
611 ibm
->acpi
->device
->driver_data
= ibm
;
612 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
613 TPACPI_ACPI_EVENT_PREFIX
,
616 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
617 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
618 if (ACPI_FAILURE(status
)) {
619 if (status
== AE_ALREADY_EXISTS
) {
621 "another device driver is already "
622 "handling %s events\n", ibm
->name
);
625 "acpi_install_notify_handler(%s) failed: %d\n",
630 ibm
->flags
.acpi_notify_installed
= 1;
634 static int __init
tpacpi_device_add(struct acpi_device
*device
)
639 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
643 dbg_printk(TPACPI_DBG_INIT
,
644 "registering %s as an ACPI driver\n", ibm
->name
);
648 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
649 if (!ibm
->acpi
->driver
) {
651 "failed to allocate memory for ibm->acpi->driver\n");
655 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
656 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
658 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
660 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
662 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
664 kfree(ibm
->acpi
->driver
);
665 ibm
->acpi
->driver
= NULL
;
667 ibm
->flags
.acpi_driver_registered
= 1;
673 /****************************************************************************
674 ****************************************************************************
678 ****************************************************************************
679 ****************************************************************************/
681 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
682 int count
, int *eof
, void *data
)
684 struct ibm_struct
*ibm
= data
;
687 if (!ibm
|| !ibm
->read
)
690 len
= ibm
->read(page
);
694 if (len
<= off
+ count
)
706 static int dispatch_procfs_write(struct file
*file
,
707 const char __user
*userbuf
,
708 unsigned long count
, void *data
)
710 struct ibm_struct
*ibm
= data
;
714 if (!ibm
|| !ibm
->write
)
717 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
721 if (copy_from_user(kernbuf
, userbuf
, count
)) {
727 strcat(kernbuf
, ",");
728 ret
= ibm
->write(kernbuf
);
737 static char *next_cmd(char **cmds
)
742 while ((end
= strchr(start
, ',')) && end
== start
)
754 /****************************************************************************
755 ****************************************************************************
757 * Device model: input, hwmon and platform
759 ****************************************************************************
760 ****************************************************************************/
762 static struct platform_device
*tpacpi_pdev
;
763 static struct platform_device
*tpacpi_sensors_pdev
;
764 static struct device
*tpacpi_hwmon
;
765 static struct input_dev
*tpacpi_inputdev
;
766 static struct mutex tpacpi_inputdev_send_mutex
;
767 static LIST_HEAD(tpacpi_all_drivers
);
769 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
772 struct ibm_struct
*ibm
, *itmp
;
774 list_for_each_entry_safe(ibm
, itmp
,
778 (ibm
->suspend
)(state
);
784 static int tpacpi_resume_handler(struct platform_device
*pdev
)
786 struct ibm_struct
*ibm
, *itmp
;
788 list_for_each_entry_safe(ibm
, itmp
,
798 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
800 struct ibm_struct
*ibm
, *itmp
;
802 list_for_each_entry_safe(ibm
, itmp
,
810 static struct platform_driver tpacpi_pdriver
= {
812 .name
= TPACPI_DRVR_NAME
,
813 .owner
= THIS_MODULE
,
815 .suspend
= tpacpi_suspend_handler
,
816 .resume
= tpacpi_resume_handler
,
817 .shutdown
= tpacpi_shutdown_handler
,
820 static struct platform_driver tpacpi_hwmon_pdriver
= {
822 .name
= TPACPI_HWMON_DRVR_NAME
,
823 .owner
= THIS_MODULE
,
827 /*************************************************************************
828 * sysfs support helpers
831 struct attribute_set
{
832 unsigned int members
, max_members
;
833 struct attribute_group group
;
836 struct attribute_set_obj
{
837 struct attribute_set s
;
839 } __attribute__((packed
));
841 static struct attribute_set
*create_attr_set(unsigned int max_members
,
844 struct attribute_set_obj
*sobj
;
846 if (max_members
== 0)
849 /* Allocates space for implicit NULL at the end too */
850 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
851 max_members
* sizeof(struct attribute
*),
855 sobj
->s
.max_members
= max_members
;
856 sobj
->s
.group
.attrs
= &sobj
->a
;
857 sobj
->s
.group
.name
= name
;
862 #define destroy_attr_set(_set) \
865 /* not multi-threaded safe, use it in a single thread per set */
866 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
871 if (s
->members
>= s
->max_members
)
874 s
->group
.attrs
[s
->members
] = attr
;
880 static int add_many_to_attr_set(struct attribute_set
*s
,
881 struct attribute
**attr
,
886 for (i
= 0; i
< count
; i
++) {
887 res
= add_to_attr_set(s
, attr
[i
]);
895 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
897 sysfs_remove_group(kobj
, &s
->group
);
901 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
902 sysfs_create_group(_kobj, &_attr_set->group)
904 static int parse_strtoul(const char *buf
,
905 unsigned long max
, unsigned long *value
)
909 while (*buf
&& isspace(*buf
))
911 *value
= simple_strtoul(buf
, &endp
, 0);
912 while (*endp
&& isspace(*endp
))
914 if (*endp
|| *value
> max
)
920 static void tpacpi_disable_brightness_delay(void)
922 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
924 "ACPI backlight control delay disabled\n");
927 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
929 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
930 union acpi_object
*obj
;
933 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
934 obj
= (union acpi_object
*)buffer
.pointer
;
935 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
936 printk(TPACPI_ERR
"Unknown _BCL data, "
937 "please report this to %s\n", TPACPI_MAIL
);
940 rc
= obj
->package
.count
;
946 kfree(buffer
.pointer
);
950 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
951 u32 lvl
, void *context
, void **rv
)
953 char name
[ACPI_PATH_SEGMENT_LENGTH
];
954 struct acpi_buffer buffer
= { sizeof(name
), &name
};
956 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
957 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
959 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
960 return AE_CTRL_TERMINATE
;
967 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
969 static int __init
tpacpi_check_std_acpi_brightness_support(void)
973 void *bcl_ptr
= &bcl_levels
;
976 TPACPI_ACPIHANDLE_INIT(vid
);
982 * Search for a _BCL method, and execute it. This is safe on all
983 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
984 * BIOS in ACPI backlight control mode. We do NOT have to care
985 * about calling the _BCL method in an enabled video device, any
986 * will do for our purposes.
989 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
990 tpacpi_acpi_walk_find_bcl
, NULL
,
993 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
994 tp_features
.bright_acpimode
= 1;
995 return (bcl_levels
- 2);
1001 static int __init
tpacpi_new_rfkill(const unsigned int id
,
1002 struct rfkill
**rfk
,
1003 const enum rfkill_type rfktype
,
1005 const bool set_default
,
1006 int (*toggle_radio
)(void *, enum rfkill_state
),
1007 int (*get_state
)(void *, enum rfkill_state
*))
1010 enum rfkill_state initial_state
= RFKILL_STATE_SOFT_BLOCKED
;
1012 res
= get_state(NULL
, &initial_state
);
1015 "failed to read initial state for %s, error %d; "
1016 "will turn radio off\n", name
, res
);
1017 } else if (set_default
) {
1018 /* try to set the initial state as the default for the rfkill
1019 * type, since we ask the firmware to preserve it across S5 in
1021 if (rfkill_set_default(rfktype
,
1022 (initial_state
== RFKILL_STATE_UNBLOCKED
) ?
1023 RFKILL_STATE_UNBLOCKED
:
1024 RFKILL_STATE_SOFT_BLOCKED
) == -EPERM
)
1025 vdbg_printk(TPACPI_DBG_RFKILL
,
1026 "Default state for %s cannot be changed\n",
1030 *rfk
= rfkill_allocate(&tpacpi_pdev
->dev
, rfktype
);
1033 "failed to allocate memory for rfkill class\n");
1037 (*rfk
)->name
= name
;
1038 (*rfk
)->get_state
= get_state
;
1039 (*rfk
)->toggle_radio
= toggle_radio
;
1040 (*rfk
)->state
= initial_state
;
1042 res
= rfkill_register(*rfk
);
1045 "failed to register %s rfkill switch: %d\n",
1055 static void printk_deprecated_attribute(const char * const what
,
1056 const char * const details
)
1058 tpacpi_log_usertask("deprecated sysfs attribute");
1059 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1060 "will be removed. %s\n",
1064 static void printk_deprecated_rfkill_attribute(const char * const what
)
1066 printk_deprecated_attribute(what
,
1067 "Please switch to generic rfkill before year 2010");
1070 /*************************************************************************
1071 * thinkpad-acpi driver attributes
1074 /* interface_version --------------------------------------------------- */
1075 static ssize_t
tpacpi_driver_interface_version_show(
1076 struct device_driver
*drv
,
1079 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1082 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1083 tpacpi_driver_interface_version_show
, NULL
);
1085 /* debug_level --------------------------------------------------------- */
1086 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1089 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1092 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1093 const char *buf
, size_t count
)
1097 if (parse_strtoul(buf
, 0xffff, &t
))
1105 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1106 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1108 /* version ------------------------------------------------------------- */
1109 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1112 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1113 TPACPI_DESC
, TPACPI_VERSION
);
1116 static DRIVER_ATTR(version
, S_IRUGO
,
1117 tpacpi_driver_version_show
, NULL
);
1119 /* --------------------------------------------------------------------- */
1121 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1123 static void tpacpi_send_radiosw_update(void);
1125 /* wlsw_emulstate ------------------------------------------------------ */
1126 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1129 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1132 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1133 const char *buf
, size_t count
)
1137 if (parse_strtoul(buf
, 1, &t
))
1140 if (tpacpi_wlsw_emulstate
!= t
) {
1141 tpacpi_wlsw_emulstate
= !!t
;
1142 tpacpi_send_radiosw_update();
1144 tpacpi_wlsw_emulstate
= !!t
;
1149 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1150 tpacpi_driver_wlsw_emulstate_show
,
1151 tpacpi_driver_wlsw_emulstate_store
);
1153 /* bluetooth_emulstate ------------------------------------------------- */
1154 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1155 struct device_driver
*drv
,
1158 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1161 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1162 struct device_driver
*drv
,
1163 const char *buf
, size_t count
)
1167 if (parse_strtoul(buf
, 1, &t
))
1170 tpacpi_bluetooth_emulstate
= !!t
;
1175 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1176 tpacpi_driver_bluetooth_emulstate_show
,
1177 tpacpi_driver_bluetooth_emulstate_store
);
1179 /* wwan_emulstate ------------------------------------------------- */
1180 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1181 struct device_driver
*drv
,
1184 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1187 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1188 struct device_driver
*drv
,
1189 const char *buf
, size_t count
)
1193 if (parse_strtoul(buf
, 1, &t
))
1196 tpacpi_wwan_emulstate
= !!t
;
1201 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1202 tpacpi_driver_wwan_emulstate_show
,
1203 tpacpi_driver_wwan_emulstate_store
);
1205 /* uwb_emulstate ------------------------------------------------- */
1206 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1207 struct device_driver
*drv
,
1210 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1213 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1214 struct device_driver
*drv
,
1215 const char *buf
, size_t count
)
1219 if (parse_strtoul(buf
, 1, &t
))
1222 tpacpi_uwb_emulstate
= !!t
;
1227 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1228 tpacpi_driver_uwb_emulstate_show
,
1229 tpacpi_driver_uwb_emulstate_store
);
1232 /* --------------------------------------------------------------------- */
1234 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1235 &driver_attr_debug_level
, &driver_attr_version
,
1236 &driver_attr_interface_version
,
1239 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1245 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1246 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1250 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1251 if (!res
&& dbg_wlswemul
)
1252 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1253 if (!res
&& dbg_bluetoothemul
)
1254 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1255 if (!res
&& dbg_wwanemul
)
1256 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1257 if (!res
&& dbg_uwbemul
)
1258 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1264 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1268 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1269 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1271 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1272 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1273 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1274 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1275 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1279 /****************************************************************************
1280 ****************************************************************************
1284 ****************************************************************************
1285 ****************************************************************************/
1287 /*************************************************************************
1288 * thinkpad-acpi init subdriver
1291 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1293 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1294 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1296 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1297 (thinkpad_id
.bios_version_str
) ?
1298 thinkpad_id
.bios_version_str
: "unknown",
1299 (thinkpad_id
.ec_version_str
) ?
1300 thinkpad_id
.ec_version_str
: "unknown");
1302 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1303 printk(TPACPI_INFO
"%s %s, model %s\n",
1304 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1305 "IBM" : ((thinkpad_id
.vendor
==
1306 PCI_VENDOR_ID_LENOVO
) ?
1307 "Lenovo" : "Unknown vendor"),
1308 thinkpad_id
.model_str
,
1309 (thinkpad_id
.nummodel_str
) ?
1310 thinkpad_id
.nummodel_str
: "unknown");
1315 static int thinkpad_acpi_driver_read(char *p
)
1319 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1320 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1325 static struct ibm_struct thinkpad_acpi_driver_data
= {
1327 .read
= thinkpad_acpi_driver_read
,
1330 /*************************************************************************
1334 enum { /* hot key scan codes (derived from ACPI DSDT) */
1335 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1336 TP_ACPI_HOTKEYSCAN_FNF2
,
1337 TP_ACPI_HOTKEYSCAN_FNF3
,
1338 TP_ACPI_HOTKEYSCAN_FNF4
,
1339 TP_ACPI_HOTKEYSCAN_FNF5
,
1340 TP_ACPI_HOTKEYSCAN_FNF6
,
1341 TP_ACPI_HOTKEYSCAN_FNF7
,
1342 TP_ACPI_HOTKEYSCAN_FNF8
,
1343 TP_ACPI_HOTKEYSCAN_FNF9
,
1344 TP_ACPI_HOTKEYSCAN_FNF10
,
1345 TP_ACPI_HOTKEYSCAN_FNF11
,
1346 TP_ACPI_HOTKEYSCAN_FNF12
,
1347 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1348 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1349 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1350 TP_ACPI_HOTKEYSCAN_FNHOME
,
1351 TP_ACPI_HOTKEYSCAN_FNEND
,
1352 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1353 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1354 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1355 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1356 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1357 TP_ACPI_HOTKEYSCAN_MUTE
,
1358 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1361 enum { /* Keys available through NVRAM polling */
1362 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1363 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1366 enum { /* Positions of some of the keys in hotkey masks */
1367 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1368 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1369 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1370 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1371 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1372 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1373 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1374 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1375 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1376 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1377 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1380 enum { /* NVRAM to ACPI HKEY group map */
1381 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1382 TP_ACPI_HKEY_ZOOM_MASK
|
1383 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1384 TP_ACPI_HKEY_HIBERNATE_MASK
,
1385 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1386 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1387 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1388 TP_ACPI_HKEY_VOLDWN_MASK
|
1389 TP_ACPI_HKEY_MUTE_MASK
,
1392 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1393 struct tp_nvram_state
{
1394 u16 thinkpad_toggle
:1;
1396 u16 display_toggle
:1;
1397 u16 thinklight_toggle
:1;
1398 u16 hibernate_toggle
:1;
1399 u16 displayexp_toggle
:1;
1400 u16 display_state
:1;
1401 u16 brightness_toggle
:1;
1402 u16 volume_toggle
:1;
1405 u8 brightness_level
;
1409 static struct task_struct
*tpacpi_hotkey_task
;
1410 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1411 static int hotkey_poll_freq
= 10; /* Hz */
1412 static struct mutex hotkey_thread_mutex
;
1413 static struct mutex hotkey_thread_data_mutex
;
1414 static unsigned int hotkey_config_change
;
1416 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1418 #define hotkey_source_mask 0U
1420 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1422 static struct mutex hotkey_mutex
;
1424 static enum { /* Reasons for waking up */
1425 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1426 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1427 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1428 } hotkey_wakeup_reason
;
1430 static int hotkey_autosleep_ack
;
1432 static u32 hotkey_orig_mask
;
1433 static u32 hotkey_all_mask
;
1434 static u32 hotkey_reserved_mask
;
1435 static u32 hotkey_mask
;
1437 static unsigned int hotkey_report_mode
;
1439 static u16
*hotkey_keycode_map
;
1441 static struct attribute_set
*hotkey_dev_attributes
;
1443 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1444 #define HOTKEY_CONFIG_CRITICAL_START \
1446 mutex_lock(&hotkey_thread_data_mutex); \
1447 hotkey_config_change++; \
1449 #define HOTKEY_CONFIG_CRITICAL_END \
1450 mutex_unlock(&hotkey_thread_data_mutex);
1452 #define HOTKEY_CONFIG_CRITICAL_START
1453 #define HOTKEY_CONFIG_CRITICAL_END
1454 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1456 /* HKEY.MHKG() return bits */
1457 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1459 static int hotkey_get_wlsw(int *status
)
1461 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1463 *status
= !!tpacpi_wlsw_emulstate
;
1467 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1472 static int hotkey_get_tablet_mode(int *status
)
1476 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1479 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1484 * Call with hotkey_mutex held
1486 static int hotkey_mask_get(void)
1490 if (tp_features
.hotkey_mask
) {
1491 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1494 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1500 * Call with hotkey_mutex held
1502 static int hotkey_mask_set(u32 mask
)
1507 if (tp_features
.hotkey_mask
) {
1508 if (!tp_warned
.hotkey_mask_ff
&&
1509 (mask
== 0xffff || mask
== 0xffffff ||
1510 mask
== 0xffffffff)) {
1511 tp_warned
.hotkey_mask_ff
= 1;
1512 printk(TPACPI_NOTICE
1513 "setting the hotkey mask to 0x%08x is likely "
1514 "not the best way to go about it\n", mask
);
1515 printk(TPACPI_NOTICE
1516 "please consider using the driver defaults, "
1517 "and refer to up-to-date thinkpad-acpi "
1521 HOTKEY_CONFIG_CRITICAL_START
1522 for (i
= 0; i
< 32; i
++) {
1524 /* enable in firmware mask only keys not in NVRAM
1525 * mode, but enable the key in the cached hotkey_mask
1526 * regardless of mode, or the key will end up
1527 * disabled by hotkey_mask_get() */
1528 if (!acpi_evalf(hkey_handle
,
1529 NULL
, "MHKM", "vdd", i
+ 1,
1530 !!((mask
& ~hotkey_source_mask
) & m
))) {
1534 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1537 HOTKEY_CONFIG_CRITICAL_END
1539 /* hotkey_mask_get must be called unconditionally below */
1540 if (!hotkey_mask_get() && !rc
&&
1541 (hotkey_mask
& ~hotkey_source_mask
) !=
1542 (mask
& ~hotkey_source_mask
)) {
1543 printk(TPACPI_NOTICE
1544 "requested hot key mask 0x%08x, but "
1545 "firmware forced it to 0x%08x\n",
1549 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1550 HOTKEY_CONFIG_CRITICAL_START
1551 hotkey_mask
= mask
& hotkey_source_mask
;
1552 HOTKEY_CONFIG_CRITICAL_END
1554 if (hotkey_mask
!= mask
) {
1555 printk(TPACPI_NOTICE
1556 "requested hot key mask 0x%08x, "
1557 "forced to 0x%08x (NVRAM poll mask is "
1558 "0x%08x): no firmware mask support\n",
1559 mask
, hotkey_mask
, hotkey_source_mask
);
1564 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1570 static int hotkey_status_get(int *status
)
1572 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1578 static int hotkey_status_set(bool enable
)
1580 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
1586 static void tpacpi_input_send_tabletsw(void)
1590 if (tp_features
.hotkey_tablet
&&
1591 !hotkey_get_tablet_mode(&state
)) {
1592 mutex_lock(&tpacpi_inputdev_send_mutex
);
1594 input_report_switch(tpacpi_inputdev
,
1595 SW_TABLET_MODE
, !!state
);
1596 input_sync(tpacpi_inputdev
);
1598 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1602 static void tpacpi_input_send_key(unsigned int scancode
)
1604 unsigned int keycode
;
1606 keycode
= hotkey_keycode_map
[scancode
];
1608 if (keycode
!= KEY_RESERVED
) {
1609 mutex_lock(&tpacpi_inputdev_send_mutex
);
1611 input_report_key(tpacpi_inputdev
, keycode
, 1);
1612 if (keycode
== KEY_UNKNOWN
)
1613 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1615 input_sync(tpacpi_inputdev
);
1617 input_report_key(tpacpi_inputdev
, keycode
, 0);
1618 if (keycode
== KEY_UNKNOWN
)
1619 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1621 input_sync(tpacpi_inputdev
);
1623 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1627 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1628 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1630 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1632 tpacpi_input_send_key(scancode
);
1633 if (hotkey_report_mode
< 2) {
1634 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1635 0x80, 0x1001 + scancode
);
1639 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1643 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1644 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1645 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1646 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1647 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1648 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1650 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1651 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1652 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1654 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1655 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1656 n
->displayexp_toggle
=
1657 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1659 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1660 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1661 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1662 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1663 n
->brightness_toggle
=
1664 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1666 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1667 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1668 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1669 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1670 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1671 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1675 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1677 if ((mask & (1 << __scancode)) && \
1678 oldn->__member != newn->__member) \
1679 tpacpi_hotkey_send_key(__scancode); \
1682 #define TPACPI_MAY_SEND_KEY(__scancode) \
1683 do { if (mask & (1 << __scancode)) \
1684 tpacpi_hotkey_send_key(__scancode); } while (0)
1686 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1687 struct tp_nvram_state
*newn
,
1690 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1691 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1695 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1700 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1701 if (oldn
->mute
!= newn
->mute
) {
1702 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1704 if (oldn
->volume_level
> newn
->volume_level
) {
1705 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1706 } else if (oldn
->volume_level
< newn
->volume_level
) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1708 } else if (oldn
->mute
== newn
->mute
) {
1709 /* repeated key presses that didn't change state */
1711 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1712 } else if (newn
->volume_level
!= 0) {
1713 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1715 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1720 /* handle brightness */
1721 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1722 if (oldn
->brightness_level
< newn
->brightness_level
) {
1723 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1724 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1725 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1727 /* repeated key presses that didn't change state */
1728 if (newn
->brightness_level
!= 0) {
1729 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1731 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1737 #undef TPACPI_COMPARE_KEY
1738 #undef TPACPI_MAY_SEND_KEY
1740 static int hotkey_kthread(void *data
)
1742 struct tp_nvram_state s
[2];
1744 unsigned int si
, so
;
1746 unsigned int change_detector
, must_reset
;
1748 mutex_lock(&hotkey_thread_mutex
);
1750 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1759 /* Initial state for compares */
1760 mutex_lock(&hotkey_thread_data_mutex
);
1761 change_detector
= hotkey_config_change
;
1762 mask
= hotkey_source_mask
& hotkey_mask
;
1763 mutex_unlock(&hotkey_thread_data_mutex
);
1764 hotkey_read_nvram(&s
[so
], mask
);
1766 while (!kthread_should_stop() && hotkey_poll_freq
) {
1768 t
= 1000/hotkey_poll_freq
;
1769 t
= msleep_interruptible(t
);
1770 if (unlikely(kthread_should_stop()))
1772 must_reset
= try_to_freeze();
1773 if (t
> 0 && !must_reset
)
1776 mutex_lock(&hotkey_thread_data_mutex
);
1777 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1778 /* forget old state on thaw or config change */
1781 change_detector
= hotkey_config_change
;
1783 mask
= hotkey_source_mask
& hotkey_mask
;
1784 mutex_unlock(&hotkey_thread_data_mutex
);
1787 hotkey_read_nvram(&s
[si
], mask
);
1788 if (likely(si
!= so
)) {
1789 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1799 mutex_unlock(&hotkey_thread_mutex
);
1803 static void hotkey_poll_stop_sync(void)
1805 if (tpacpi_hotkey_task
) {
1806 if (frozen(tpacpi_hotkey_task
) ||
1807 freezing(tpacpi_hotkey_task
))
1808 thaw_process(tpacpi_hotkey_task
);
1810 kthread_stop(tpacpi_hotkey_task
);
1811 tpacpi_hotkey_task
= NULL
;
1812 mutex_lock(&hotkey_thread_mutex
);
1813 /* at this point, the thread did exit */
1814 mutex_unlock(&hotkey_thread_mutex
);
1818 /* call with hotkey_mutex held */
1819 static void hotkey_poll_setup(int may_warn
)
1821 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1822 hotkey_poll_freq
> 0 &&
1823 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1824 if (!tpacpi_hotkey_task
) {
1825 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1826 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
1827 if (IS_ERR(tpacpi_hotkey_task
)) {
1828 tpacpi_hotkey_task
= NULL
;
1830 "could not create kernel thread "
1831 "for hotkey polling\n");
1835 hotkey_poll_stop_sync();
1837 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1838 printk(TPACPI_NOTICE
1839 "hot keys 0x%08x require polling, "
1840 "which is currently disabled\n",
1841 hotkey_source_mask
);
1846 static void hotkey_poll_setup_safe(int may_warn
)
1848 mutex_lock(&hotkey_mutex
);
1849 hotkey_poll_setup(may_warn
);
1850 mutex_unlock(&hotkey_mutex
);
1853 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1855 static void hotkey_poll_setup_safe(int __unused
)
1859 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1861 static int hotkey_inputdev_open(struct input_dev
*dev
)
1863 switch (tpacpi_lifecycle
) {
1864 case TPACPI_LIFE_INIT
:
1866 * hotkey_init will call hotkey_poll_setup_safe
1867 * at the appropriate moment
1870 case TPACPI_LIFE_EXITING
:
1872 case TPACPI_LIFE_RUNNING
:
1873 hotkey_poll_setup_safe(0);
1877 /* Should only happen if tpacpi_lifecycle is corrupt */
1882 static void hotkey_inputdev_close(struct input_dev
*dev
)
1884 /* disable hotkey polling when possible */
1885 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1886 hotkey_poll_setup_safe(0);
1889 /* sysfs hotkey enable ------------------------------------------------- */
1890 static ssize_t
hotkey_enable_show(struct device
*dev
,
1891 struct device_attribute
*attr
,
1896 printk_deprecated_attribute("hotkey_enable",
1897 "Hotkey reporting is always enabled");
1899 res
= hotkey_status_get(&status
);
1903 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1906 static ssize_t
hotkey_enable_store(struct device
*dev
,
1907 struct device_attribute
*attr
,
1908 const char *buf
, size_t count
)
1912 printk_deprecated_attribute("hotkey_enable",
1913 "Hotkeys can be disabled through hotkey_mask");
1915 if (parse_strtoul(buf
, 1, &t
))
1924 static struct device_attribute dev_attr_hotkey_enable
=
1925 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1926 hotkey_enable_show
, hotkey_enable_store
);
1928 /* sysfs hotkey mask --------------------------------------------------- */
1929 static ssize_t
hotkey_mask_show(struct device
*dev
,
1930 struct device_attribute
*attr
,
1935 if (mutex_lock_killable(&hotkey_mutex
))
1936 return -ERESTARTSYS
;
1937 res
= hotkey_mask_get();
1938 mutex_unlock(&hotkey_mutex
);
1941 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1944 static ssize_t
hotkey_mask_store(struct device
*dev
,
1945 struct device_attribute
*attr
,
1946 const char *buf
, size_t count
)
1951 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1954 if (mutex_lock_killable(&hotkey_mutex
))
1955 return -ERESTARTSYS
;
1957 res
= hotkey_mask_set(t
);
1959 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1960 hotkey_poll_setup(1);
1963 mutex_unlock(&hotkey_mutex
);
1965 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
1967 return (res
) ? res
: count
;
1970 static struct device_attribute dev_attr_hotkey_mask
=
1971 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1972 hotkey_mask_show
, hotkey_mask_store
);
1974 /* sysfs hotkey bios_enabled ------------------------------------------- */
1975 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1976 struct device_attribute
*attr
,
1979 return sprintf(buf
, "0\n");
1982 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1983 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1985 /* sysfs hotkey bios_mask ---------------------------------------------- */
1986 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1987 struct device_attribute
*attr
,
1990 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1993 static struct device_attribute dev_attr_hotkey_bios_mask
=
1994 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1996 /* sysfs hotkey all_mask ----------------------------------------------- */
1997 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
1998 struct device_attribute
*attr
,
2001 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2002 hotkey_all_mask
| hotkey_source_mask
);
2005 static struct device_attribute dev_attr_hotkey_all_mask
=
2006 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2008 /* sysfs hotkey recommended_mask --------------------------------------- */
2009 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2010 struct device_attribute
*attr
,
2013 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2014 (hotkey_all_mask
| hotkey_source_mask
)
2015 & ~hotkey_reserved_mask
);
2018 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2019 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2020 hotkey_recommended_mask_show
, NULL
);
2022 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2024 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2025 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2026 struct device_attribute
*attr
,
2029 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2032 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2033 struct device_attribute
*attr
,
2034 const char *buf
, size_t count
)
2038 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2039 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2042 if (mutex_lock_killable(&hotkey_mutex
))
2043 return -ERESTARTSYS
;
2045 HOTKEY_CONFIG_CRITICAL_START
2046 hotkey_source_mask
= t
;
2047 HOTKEY_CONFIG_CRITICAL_END
2049 hotkey_poll_setup(1);
2051 mutex_unlock(&hotkey_mutex
);
2053 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2058 static struct device_attribute dev_attr_hotkey_source_mask
=
2059 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2060 hotkey_source_mask_show
, hotkey_source_mask_store
);
2062 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2063 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2064 struct device_attribute
*attr
,
2067 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2070 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2071 struct device_attribute
*attr
,
2072 const char *buf
, size_t count
)
2076 if (parse_strtoul(buf
, 25, &t
))
2079 if (mutex_lock_killable(&hotkey_mutex
))
2080 return -ERESTARTSYS
;
2082 hotkey_poll_freq
= t
;
2084 hotkey_poll_setup(1);
2085 mutex_unlock(&hotkey_mutex
);
2087 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2092 static struct device_attribute dev_attr_hotkey_poll_freq
=
2093 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2094 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2096 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2098 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2099 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2100 struct device_attribute
*attr
,
2104 res
= hotkey_get_wlsw(&s
);
2108 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2111 static struct device_attribute dev_attr_hotkey_radio_sw
=
2112 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2114 static void hotkey_radio_sw_notify_change(void)
2116 if (tp_features
.hotkey_wlsw
)
2117 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2121 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2122 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2123 struct device_attribute
*attr
,
2127 res
= hotkey_get_tablet_mode(&s
);
2131 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2134 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2135 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2137 static void hotkey_tablet_mode_notify_change(void)
2139 if (tp_features
.hotkey_tablet
)
2140 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2141 "hotkey_tablet_mode");
2144 /* sysfs hotkey report_mode -------------------------------------------- */
2145 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2146 struct device_attribute
*attr
,
2149 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2150 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2153 static struct device_attribute dev_attr_hotkey_report_mode
=
2154 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2156 /* sysfs wakeup reason (pollable) -------------------------------------- */
2157 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2158 struct device_attribute
*attr
,
2161 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2164 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2165 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2167 static void hotkey_wakeup_reason_notify_change(void)
2169 if (tp_features
.hotkey_mask
)
2170 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2174 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2175 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2176 struct device_attribute
*attr
,
2179 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2182 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2183 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2184 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2186 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2188 if (tp_features
.hotkey_mask
)
2189 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2190 "wakeup_hotunplug_complete");
2193 /* --------------------------------------------------------------------- */
2195 static struct attribute
*hotkey_attributes
[] __initdata
= {
2196 &dev_attr_hotkey_enable
.attr
,
2197 &dev_attr_hotkey_bios_enabled
.attr
,
2198 &dev_attr_hotkey_report_mode
.attr
,
2199 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2200 &dev_attr_hotkey_mask
.attr
,
2201 &dev_attr_hotkey_all_mask
.attr
,
2202 &dev_attr_hotkey_recommended_mask
.attr
,
2203 &dev_attr_hotkey_source_mask
.attr
,
2204 &dev_attr_hotkey_poll_freq
.attr
,
2208 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
2209 &dev_attr_hotkey_bios_mask
.attr
,
2210 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2211 &dev_attr_hotkey_mask
.attr
,
2212 &dev_attr_hotkey_all_mask
.attr
,
2213 &dev_attr_hotkey_recommended_mask
.attr
,
2215 &dev_attr_hotkey_wakeup_reason
.attr
,
2216 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2219 static void bluetooth_update_rfk(void);
2220 static void wan_update_rfk(void);
2221 static void uwb_update_rfk(void);
2222 static void tpacpi_send_radiosw_update(void)
2226 /* Sync these BEFORE sending any rfkill events */
2227 if (tp_features
.bluetooth
)
2228 bluetooth_update_rfk();
2229 if (tp_features
.wan
)
2231 if (tp_features
.uwb
)
2234 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
2235 mutex_lock(&tpacpi_inputdev_send_mutex
);
2237 input_report_switch(tpacpi_inputdev
,
2238 SW_RFKILL_ALL
, !!wlsw
);
2239 input_sync(tpacpi_inputdev
);
2241 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2243 hotkey_radio_sw_notify_change();
2246 static void hotkey_exit(void)
2248 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2249 hotkey_poll_stop_sync();
2252 if (hotkey_dev_attributes
)
2253 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2255 kfree(hotkey_keycode_map
);
2257 if (tp_features
.hotkey
) {
2258 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
2259 "restoring original hot key mask\n");
2260 /* no short-circuit boolean operator below! */
2261 if ((hotkey_mask_set(hotkey_orig_mask
) |
2262 hotkey_status_set(false)) != 0)
2264 "failed to restore hot key mask "
2265 "to BIOS defaults\n");
2269 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2271 /* Requirements for changing the default keymaps:
2273 * 1. Many of the keys are mapped to KEY_RESERVED for very
2274 * good reasons. Do not change them unless you have deep
2275 * knowledge on the IBM and Lenovo ThinkPad firmware for
2276 * the various ThinkPad models. The driver behaves
2277 * differently for KEY_RESERVED: such keys have their
2278 * hot key mask *unset* in mask_recommended, and also
2279 * in the initial hot key mask programmed into the
2280 * firmware at driver load time, which means the firm-
2281 * ware may react very differently if you change them to
2284 * 2. You must be subscribed to the linux-thinkpad and
2285 * ibm-acpi-devel mailing lists, and you should read the
2286 * list archives since 2007 if you want to change the
2287 * keymaps. This requirement exists so that you will
2288 * know the past history of problems with the thinkpad-
2289 * acpi driver keymaps, and also that you will be
2290 * listening to any bug reports;
2292 * 3. Do not send thinkpad-acpi specific patches directly to
2293 * for merging, *ever*. Send them to the linux-acpi
2294 * mailinglist for comments. Merging is to be done only
2295 * through acpi-test and the ACPI maintainer.
2297 * If the above is too much to ask, don't change the keymap.
2298 * Ask the thinkpad-acpi maintainer to do it, instead.
2300 static u16 ibm_keycode_map
[] __initdata
= {
2301 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2302 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2303 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2304 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2306 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2307 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2308 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2309 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2311 /* brightness: firmware always reacts to them, unless
2312 * X.org did some tricks in the radeon BIOS scratch
2313 * registers of *some* models */
2314 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2315 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2317 /* Thinklight: firmware always react to it */
2318 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2320 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2321 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2323 /* Volume: firmware always react to it and reprograms
2324 * the built-in *extra* mixer. Never map it to control
2325 * another mixer by default. */
2326 KEY_RESERVED
, /* 0x14: VOLUME UP */
2327 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2328 KEY_RESERVED
, /* 0x16: MUTE */
2330 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2332 /* (assignments unknown, please report if found) */
2333 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2334 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2336 static u16 lenovo_keycode_map
[] __initdata
= {
2337 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2338 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2339 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2340 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2342 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2343 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2344 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2345 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2347 /* These either have to go through ACPI video, or
2348 * act like in the IBM ThinkPads, so don't ever
2349 * enable them by default */
2350 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2351 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2353 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2355 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2356 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2358 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2359 * react to it and reprograms the built-in *extra* mixer.
2360 * Never map it to control another mixer by default.
2362 * T60?, T61, R60?, R61: firmware and EC tries to send
2363 * these over the regular keyboard, so these are no-ops,
2364 * but there are still weird bugs re. MUTE, so do not
2365 * change unless you get test reports from all Lenovo
2366 * models. May cause the BIOS to interfere with the
2369 KEY_RESERVED
, /* 0x14: VOLUME UP */
2370 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2371 KEY_RESERVED
, /* 0x16: MUTE */
2373 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2375 /* (assignments unknown, please report if found) */
2376 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2377 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2380 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2381 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2382 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2388 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2389 "initializing hotkey subdriver\n");
2391 BUG_ON(!tpacpi_inputdev
);
2392 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2393 tpacpi_inputdev
->close
!= NULL
);
2395 TPACPI_ACPIHANDLE_INIT(hkey
);
2396 mutex_init(&hotkey_mutex
);
2398 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2399 mutex_init(&hotkey_thread_mutex
);
2400 mutex_init(&hotkey_thread_data_mutex
);
2403 /* hotkey not supported on 570 */
2404 tp_features
.hotkey
= hkey_handle
!= NULL
;
2406 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2408 str_supported(tp_features
.hotkey
));
2410 if (!tp_features
.hotkey
)
2413 tpacpi_disable_brightness_delay();
2415 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2416 if (!hotkey_dev_attributes
)
2418 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2420 ARRAY_SIZE(hotkey_attributes
));
2424 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2425 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2426 for HKEY interface version 0x100 */
2427 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2428 if ((hkeyv
>> 8) != 1) {
2429 printk(TPACPI_ERR
"unknown version of the "
2430 "HKEY interface: 0x%x\n", hkeyv
);
2431 printk(TPACPI_ERR
"please report this to %s\n",
2435 * MHKV 0x100 in A31, R40, R40e,
2436 * T4x, X31, and later
2438 tp_features
.hotkey_mask
= 1;
2439 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2440 "firmware HKEY interface version: 0x%x\n",
2445 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2446 "hotkey masks are %s\n",
2447 str_supported(tp_features
.hotkey_mask
));
2449 if (tp_features
.hotkey_mask
) {
2450 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2453 "missing MHKA handler, "
2454 "please report this to %s\n",
2456 /* FN+F12, FN+F4, FN+F3 */
2457 hotkey_all_mask
= 0x080cU
;
2461 /* hotkey_source_mask *must* be zero for
2462 * the first hotkey_mask_get */
2463 if (tp_features
.hotkey_mask
) {
2464 res
= hotkey_mask_get();
2468 hotkey_orig_mask
= hotkey_mask
;
2469 res
= add_many_to_attr_set(
2470 hotkey_dev_attributes
,
2471 hotkey_mask_attributes
,
2472 ARRAY_SIZE(hotkey_mask_attributes
));
2477 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2478 if (tp_features
.hotkey_mask
) {
2479 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2482 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2485 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2486 "hotkey source mask 0x%08x, polling freq %d\n",
2487 hotkey_source_mask
, hotkey_poll_freq
);
2490 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2492 tp_features
.hotkey_wlsw
= 1;
2494 "radio switch emulation enabled\n");
2497 /* Not all thinkpads have a hardware radio switch */
2498 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2499 tp_features
.hotkey_wlsw
= 1;
2501 "radio switch found; radios are %s\n",
2502 enabled(status
, 0));
2504 if (tp_features
.hotkey_wlsw
)
2505 res
= add_to_attr_set(hotkey_dev_attributes
,
2506 &dev_attr_hotkey_radio_sw
.attr
);
2508 /* For X41t, X60t, X61t Tablets... */
2509 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2510 tp_features
.hotkey_tablet
= 1;
2512 "possible tablet mode switch found; "
2513 "ThinkPad in %s mode\n",
2514 (status
& TP_HOTKEY_TABLET_MASK
)?
2515 "tablet" : "laptop");
2516 res
= add_to_attr_set(hotkey_dev_attributes
,
2517 &dev_attr_hotkey_tablet_mode
.attr
);
2521 res
= register_attr_set_with_sysfs(
2522 hotkey_dev_attributes
,
2523 &tpacpi_pdev
->dev
.kobj
);
2527 /* Set up key map */
2529 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2531 if (!hotkey_keycode_map
) {
2533 "failed to allocate memory for key map\n");
2538 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2539 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2540 "using Lenovo default hot key map\n");
2541 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2542 TPACPI_HOTKEY_MAP_SIZE
);
2544 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2545 "using IBM default hot key map\n");
2546 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2547 TPACPI_HOTKEY_MAP_SIZE
);
2550 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2551 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2552 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2553 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2554 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2555 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2556 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2557 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2558 set_bit(hotkey_keycode_map
[i
],
2559 tpacpi_inputdev
->keybit
);
2561 if (i
< sizeof(hotkey_reserved_mask
)*8)
2562 hotkey_reserved_mask
|= 1 << i
;
2566 if (tp_features
.hotkey_wlsw
) {
2567 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2568 set_bit(SW_RFKILL_ALL
, tpacpi_inputdev
->swbit
);
2570 if (tp_features
.hotkey_tablet
) {
2571 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2572 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2575 /* Do not issue duplicate brightness change events to
2577 if (!tp_features
.bright_acpimode
)
2578 /* update bright_acpimode... */
2579 tpacpi_check_std_acpi_brightness_support();
2581 if (tp_features
.bright_acpimode
) {
2583 "This ThinkPad has standard ACPI backlight "
2584 "brightness control, supported by the ACPI "
2586 printk(TPACPI_NOTICE
2587 "Disabling thinkpad-acpi brightness events "
2590 /* The hotkey_reserved_mask change below is not
2591 * necessary while the keys are at KEY_RESERVED in the
2592 * default map, but better safe than sorry, leave it
2593 * here as a marker of what we have to do, especially
2594 * when we finally become able to set this at runtime
2595 * on response to X.org requests */
2596 hotkey_reserved_mask
|=
2597 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
2598 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
2601 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2602 "enabling firmware HKEY event interface...\n");
2603 res
= hotkey_status_set(true);
2608 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2609 & ~hotkey_reserved_mask
)
2610 | hotkey_orig_mask
);
2611 if (res
< 0 && res
!= -ENXIO
) {
2616 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
2617 "legacy ibm/hotkey event reporting over procfs %s\n",
2618 (hotkey_report_mode
< 2) ?
2619 "enabled" : "disabled");
2621 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2622 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2624 hotkey_poll_setup_safe(1);
2625 tpacpi_send_radiosw_update();
2626 tpacpi_input_send_tabletsw();
2631 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2632 hotkey_dev_attributes
= NULL
;
2634 return (res
< 0)? res
: 1;
2637 static bool hotkey_notify_hotkey(const u32 hkey
,
2639 bool *ignore_acpi_ev
)
2641 /* 0x1000-0x1FFF: key presses */
2642 unsigned int scancode
= hkey
& 0xfff;
2643 *send_acpi_ev
= true;
2644 *ignore_acpi_ev
= false;
2646 if (scancode
> 0 && scancode
< 0x21) {
2648 if (!(hotkey_source_mask
& (1 << scancode
))) {
2649 tpacpi_input_send_key(scancode
);
2650 *send_acpi_ev
= false;
2652 *ignore_acpi_ev
= true;
2659 static bool hotkey_notify_wakeup(const u32 hkey
,
2661 bool *ignore_acpi_ev
)
2663 /* 0x2000-0x2FFF: Wakeup reason */
2664 *send_acpi_ev
= true;
2665 *ignore_acpi_ev
= false;
2668 case 0x2304: /* suspend, undock */
2669 case 0x2404: /* hibernation, undock */
2670 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2671 *ignore_acpi_ev
= true;
2674 case 0x2305: /* suspend, bay eject */
2675 case 0x2405: /* hibernation, bay eject */
2676 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2677 *ignore_acpi_ev
= true;
2680 case 0x2313: /* Battery on critical low level (S3) */
2681 case 0x2413: /* Battery on critical low level (S4) */
2683 "EMERGENCY WAKEUP: battery almost empty\n");
2684 /* how to auto-heal: */
2685 /* 2313: woke up from S3, go to S4/S5 */
2686 /* 2413: woke up from S4, go to S5 */
2693 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2695 "woke up due to a hot-unplug "
2697 hotkey_wakeup_reason_notify_change();
2702 static bool hotkey_notify_usrevent(const u32 hkey
,
2704 bool *ignore_acpi_ev
)
2706 /* 0x5000-0x5FFF: human interface helpers */
2707 *send_acpi_ev
= true;
2708 *ignore_acpi_ev
= false;
2711 case 0x5010: /* Lenovo new BIOS: brightness changed */
2712 case 0x500b: /* X61t: tablet pen inserted into bay */
2713 case 0x500c: /* X61t: tablet pen removed from bay */
2716 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2717 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2718 tpacpi_input_send_tabletsw();
2719 hotkey_tablet_mode_notify_change();
2720 *send_acpi_ev
= false;
2725 /* LID switch events. Do not propagate */
2726 *ignore_acpi_ev
= true;
2734 static bool hotkey_notify_thermal(const u32 hkey
,
2736 bool *ignore_acpi_ev
)
2738 /* 0x6000-0x6FFF: thermal alarms */
2739 *send_acpi_ev
= true;
2740 *ignore_acpi_ev
= false;
2745 "THERMAL ALARM: battery is too hot!\n");
2746 /* recommended action: warn user through gui */
2750 "THERMAL EMERGENCY: battery is extremely hot!\n");
2751 /* recommended action: immediate sleep/hibernate */
2756 "a sensor reports something is too hot!\n");
2757 /* recommended action: warn user through gui, that */
2758 /* some internal component is too hot */
2762 "THERMAL EMERGENCY: "
2763 "a sensor reports something is extremely hot!\n");
2764 /* recommended action: immediate sleep/hibernate */
2768 "EC reports that Thermal Table has changed\n");
2769 /* recommended action: do nothing, we don't have
2770 * Lenovo ATM information */
2774 "THERMAL ALERT: unknown thermal alarm received\n");
2779 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2783 bool ignore_acpi_ev
;
2786 if (event
!= 0x80) {
2788 "unknown HKEY notification event %d\n", event
);
2789 /* forward it to userspace, maybe it knows how to handle it */
2790 acpi_bus_generate_netlink_event(
2791 ibm
->acpi
->device
->pnp
.device_class
,
2792 dev_name(&ibm
->acpi
->device
->dev
),
2798 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2799 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2808 send_acpi_ev
= true;
2809 ignore_acpi_ev
= false;
2811 switch (hkey
>> 12) {
2813 /* 0x1000-0x1FFF: key presses */
2814 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
2818 /* 0x2000-0x2FFF: Wakeup reason */
2819 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
2823 /* 0x3000-0x3FFF: bay-related wakeups */
2824 if (hkey
== 0x3003) {
2825 hotkey_autosleep_ack
= 1;
2828 hotkey_wakeup_hotunplug_complete_notify_change();
2835 /* 0x4000-0x4FFF: dock-related wakeups */
2836 if (hkey
== 0x4003) {
2837 hotkey_autosleep_ack
= 1;
2840 hotkey_wakeup_hotunplug_complete_notify_change();
2847 /* 0x5000-0x5FFF: human interface helpers */
2848 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
2852 /* 0x6000-0x6FFF: thermal alarms */
2853 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
2857 /* 0x7000-0x7FFF: misc */
2858 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2859 tpacpi_send_radiosw_update();
2864 /* fallthrough to default */
2869 printk(TPACPI_NOTICE
2870 "unhandled HKEY event 0x%04x\n", hkey
);
2871 printk(TPACPI_NOTICE
2872 "please report the conditions when this "
2873 "event happened to %s\n", TPACPI_MAIL
);
2877 if (!ignore_acpi_ev
&&
2878 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2879 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2883 /* netlink events */
2884 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2885 acpi_bus_generate_netlink_event(
2886 ibm
->acpi
->device
->pnp
.device_class
,
2887 dev_name(&ibm
->acpi
->device
->dev
),
2893 static void hotkey_suspend(pm_message_t state
)
2895 /* Do these on suspend, we get the events on early resume! */
2896 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2897 hotkey_autosleep_ack
= 0;
2900 static void hotkey_resume(void)
2902 tpacpi_disable_brightness_delay();
2904 if (hotkey_mask_get())
2906 "error while trying to read hot key mask "
2908 tpacpi_send_radiosw_update();
2909 hotkey_tablet_mode_notify_change();
2910 hotkey_wakeup_reason_notify_change();
2911 hotkey_wakeup_hotunplug_complete_notify_change();
2912 hotkey_poll_setup_safe(0);
2915 /* procfs -------------------------------------------------------------- */
2916 static int hotkey_read(char *p
)
2921 if (!tp_features
.hotkey
) {
2922 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2926 if (mutex_lock_killable(&hotkey_mutex
))
2927 return -ERESTARTSYS
;
2928 res
= hotkey_status_get(&status
);
2930 res
= hotkey_mask_get();
2931 mutex_unlock(&hotkey_mutex
);
2935 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2936 if (tp_features
.hotkey_mask
) {
2937 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2938 len
+= sprintf(p
+ len
,
2939 "commands:\tenable, disable, reset, <mask>\n");
2941 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2942 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2948 static void hotkey_enabledisable_warn(void)
2950 tpacpi_log_usertask("procfs hotkey enable/disable");
2952 "hotkey enable/disable functionality has been "
2953 "removed from the driver. Hotkeys are always enabled.\n");
2956 static int hotkey_write(char *buf
)
2962 if (!tp_features
.hotkey
)
2965 if (mutex_lock_killable(&hotkey_mutex
))
2966 return -ERESTARTSYS
;
2971 while ((cmd
= next_cmd(&buf
))) {
2972 if (strlencmp(cmd
, "enable") == 0) {
2973 hotkey_enabledisable_warn();
2974 } else if (strlencmp(cmd
, "disable") == 0) {
2975 hotkey_enabledisable_warn();
2977 } else if (strlencmp(cmd
, "reset") == 0) {
2978 mask
= hotkey_orig_mask
;
2979 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2981 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2990 tpacpi_disclose_usertask("procfs hotkey",
2991 "set mask to 0x%08x\n", mask
);
2993 if (!res
&& mask
!= hotkey_mask
)
2994 res
= hotkey_mask_set(mask
);
2997 mutex_unlock(&hotkey_mutex
);
3001 static const struct acpi_device_id ibm_htk_device_ids
[] = {
3002 {TPACPI_ACPI_HKEY_HID
, 0},
3006 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
3007 .hid
= ibm_htk_device_ids
,
3008 .notify
= hotkey_notify
,
3009 .handle
= &hkey_handle
,
3010 .type
= ACPI_DEVICE_NOTIFY
,
3013 static struct ibm_struct hotkey_driver_data
= {
3015 .read
= hotkey_read
,
3016 .write
= hotkey_write
,
3017 .exit
= hotkey_exit
,
3018 .resume
= hotkey_resume
,
3019 .suspend
= hotkey_suspend
,
3020 .acpi
= &ibm_hotkey_acpidriver
,
3023 /*************************************************************************
3024 * Bluetooth subdriver
3028 /* ACPI GBDC/SBDC bits */
3029 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3030 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3031 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3036 /* ACPI \BLTH commands */
3037 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3038 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3039 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3040 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3041 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3044 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3046 static struct rfkill
*tpacpi_bluetooth_rfkill
;
3048 static void bluetooth_suspend(pm_message_t state
)
3050 /* Try to make sure radio will resume powered off */
3051 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3052 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
))
3053 vdbg_printk(TPACPI_DBG_RFKILL
,
3054 "bluetooth power down on resume request failed\n");
3057 static int bluetooth_get_radiosw(void)
3061 if (!tp_features
.bluetooth
)
3064 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3065 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3066 return RFKILL_STATE_HARD_BLOCKED
;
3068 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3069 if (dbg_bluetoothemul
)
3070 return (tpacpi_bluetooth_emulstate
) ?
3071 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3074 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3077 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3078 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3081 static void bluetooth_update_rfk(void)
3085 if (!tpacpi_bluetooth_rfkill
)
3088 status
= bluetooth_get_radiosw();
3091 rfkill_force_state(tpacpi_bluetooth_rfkill
, status
);
3093 vdbg_printk(TPACPI_DBG_RFKILL
,
3094 "forced rfkill state to %d\n",
3098 static int bluetooth_set_radiosw(int radio_on
, int update_rfk
)
3102 if (!tp_features
.bluetooth
)
3105 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3106 * reason to risk weird behaviour. */
3107 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3111 vdbg_printk(TPACPI_DBG_RFKILL
,
3112 "will %s bluetooth\n", radio_on
? "enable" : "disable");
3114 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3115 if (dbg_bluetoothemul
) {
3116 tpacpi_bluetooth_emulstate
= !!radio_on
;
3118 bluetooth_update_rfk();
3123 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3125 status
= TP_ACPI_BLUETOOTH_RADIOSSW
;
3128 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3132 bluetooth_update_rfk();
3137 /* sysfs bluetooth enable ---------------------------------------------- */
3138 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3139 struct device_attribute
*attr
,
3144 printk_deprecated_rfkill_attribute("bluetooth_enable");
3146 status
= bluetooth_get_radiosw();
3150 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3151 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3154 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3155 struct device_attribute
*attr
,
3156 const char *buf
, size_t count
)
3161 printk_deprecated_rfkill_attribute("bluetooth_enable");
3163 if (parse_strtoul(buf
, 1, &t
))
3166 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t
);
3168 res
= bluetooth_set_radiosw(t
, 1);
3170 return (res
) ? res
: count
;
3173 static struct device_attribute dev_attr_bluetooth_enable
=
3174 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3175 bluetooth_enable_show
, bluetooth_enable_store
);
3177 /* --------------------------------------------------------------------- */
3179 static struct attribute
*bluetooth_attributes
[] = {
3180 &dev_attr_bluetooth_enable
.attr
,
3184 static const struct attribute_group bluetooth_attr_group
= {
3185 .attrs
= bluetooth_attributes
,
3188 static int tpacpi_bluetooth_rfk_get(void *data
, enum rfkill_state
*state
)
3190 int bts
= bluetooth_get_radiosw();
3199 static int tpacpi_bluetooth_rfk_set(void *data
, enum rfkill_state state
)
3201 dbg_printk(TPACPI_DBG_RFKILL
,
3202 "request to change radio state to %d\n", state
);
3203 return bluetooth_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3206 static void bluetooth_shutdown(void)
3208 /* Order firmware to save current state to NVRAM */
3209 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3210 TP_ACPI_BLTH_SAVE_STATE
))
3211 printk(TPACPI_NOTICE
3212 "failed to save bluetooth state to NVRAM\n");
3214 vdbg_printk(TPACPI_DBG_RFKILL
,
3215 "bluestooth state saved to NVRAM\n");
3218 static void bluetooth_exit(void)
3220 bluetooth_shutdown();
3222 if (tpacpi_bluetooth_rfkill
)
3223 rfkill_unregister(tpacpi_bluetooth_rfkill
);
3225 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3226 &bluetooth_attr_group
);
3229 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3234 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3235 "initializing bluetooth subdriver\n");
3237 TPACPI_ACPIHANDLE_INIT(hkey
);
3239 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3240 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3241 tp_features
.bluetooth
= hkey_handle
&&
3242 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
3244 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3245 "bluetooth is %s, status 0x%02x\n",
3246 str_supported(tp_features
.bluetooth
),
3249 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3250 if (dbg_bluetoothemul
) {
3251 tp_features
.bluetooth
= 1;
3253 "bluetooth switch emulation enabled\n");
3256 if (tp_features
.bluetooth
&&
3257 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
3258 /* no bluetooth hardware present in system */
3259 tp_features
.bluetooth
= 0;
3260 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3261 "bluetooth hardware not installed\n");
3264 if (!tp_features
.bluetooth
)
3267 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3268 &bluetooth_attr_group
);
3272 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
3273 &tpacpi_bluetooth_rfkill
,
3274 RFKILL_TYPE_BLUETOOTH
,
3275 TPACPI_RFK_BLUETOOTH_SW_NAME
,
3277 tpacpi_bluetooth_rfk_set
,
3278 tpacpi_bluetooth_rfk_get
);
3287 /* procfs -------------------------------------------------------------- */
3288 static int bluetooth_read(char *p
)
3291 int status
= bluetooth_get_radiosw();
3293 if (!tp_features
.bluetooth
)
3294 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3296 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3297 (status
== RFKILL_STATE_UNBLOCKED
) ?
3298 "enabled" : "disabled");
3299 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3305 static int bluetooth_write(char *buf
)
3310 if (!tp_features
.bluetooth
)
3313 while ((cmd
= next_cmd(&buf
))) {
3314 if (strlencmp(cmd
, "enable") == 0) {
3316 } else if (strlencmp(cmd
, "disable") == 0) {
3323 tpacpi_disclose_usertask("procfs bluetooth",
3325 state
? "enable" : "disable");
3326 bluetooth_set_radiosw(state
, 1);
3332 static struct ibm_struct bluetooth_driver_data
= {
3333 .name
= "bluetooth",
3334 .read
= bluetooth_read
,
3335 .write
= bluetooth_write
,
3336 .exit
= bluetooth_exit
,
3337 .suspend
= bluetooth_suspend
,
3338 .shutdown
= bluetooth_shutdown
,
3341 /*************************************************************************
3346 /* ACPI GWAN/SWAN bits */
3347 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
3348 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
3349 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
3353 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3355 static struct rfkill
*tpacpi_wan_rfkill
;
3357 static void wan_suspend(pm_message_t state
)
3359 /* Try to make sure radio will resume powered off */
3360 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "qvd",
3361 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
))
3362 vdbg_printk(TPACPI_DBG_RFKILL
,
3363 "WWAN power down on resume request failed\n");
3366 static int wan_get_radiosw(void)
3370 if (!tp_features
.wan
)
3373 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3374 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3375 return RFKILL_STATE_HARD_BLOCKED
;
3377 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3379 return (tpacpi_wwan_emulstate
) ?
3380 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3383 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
3386 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
3387 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3390 static void wan_update_rfk(void)
3394 if (!tpacpi_wan_rfkill
)
3397 status
= wan_get_radiosw();
3400 rfkill_force_state(tpacpi_wan_rfkill
, status
);
3402 vdbg_printk(TPACPI_DBG_RFKILL
,
3403 "forced rfkill state to %d\n",
3407 static int wan_set_radiosw(int radio_on
, int update_rfk
)
3411 if (!tp_features
.wan
)
3414 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3415 * reason to risk weird behaviour. */
3416 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3420 vdbg_printk(TPACPI_DBG_RFKILL
,
3421 "will %s WWAN\n", radio_on
? "enable" : "disable");
3423 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3425 tpacpi_wwan_emulstate
= !!radio_on
;
3432 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3434 status
= TP_ACPI_WANCARD_RADIOSSW
;
3437 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
3446 /* sysfs wan enable ---------------------------------------------------- */
3447 static ssize_t
wan_enable_show(struct device
*dev
,
3448 struct device_attribute
*attr
,
3453 printk_deprecated_rfkill_attribute("wwan_enable");
3455 status
= wan_get_radiosw();
3459 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3460 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3463 static ssize_t
wan_enable_store(struct device
*dev
,
3464 struct device_attribute
*attr
,
3465 const char *buf
, size_t count
)
3470 printk_deprecated_rfkill_attribute("wwan_enable");
3472 if (parse_strtoul(buf
, 1, &t
))
3475 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t
);
3477 res
= wan_set_radiosw(t
, 1);
3479 return (res
) ? res
: count
;
3482 static struct device_attribute dev_attr_wan_enable
=
3483 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
3484 wan_enable_show
, wan_enable_store
);
3486 /* --------------------------------------------------------------------- */
3488 static struct attribute
*wan_attributes
[] = {
3489 &dev_attr_wan_enable
.attr
,
3493 static const struct attribute_group wan_attr_group
= {
3494 .attrs
= wan_attributes
,
3497 static int tpacpi_wan_rfk_get(void *data
, enum rfkill_state
*state
)
3499 int wans
= wan_get_radiosw();
3508 static int tpacpi_wan_rfk_set(void *data
, enum rfkill_state state
)
3510 dbg_printk(TPACPI_DBG_RFKILL
,
3511 "request to change radio state to %d\n", state
);
3512 return wan_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3515 static void wan_shutdown(void)
3517 /* Order firmware to save current state to NVRAM */
3518 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
3519 TP_ACPI_WGSV_SAVE_STATE
))
3520 printk(TPACPI_NOTICE
3521 "failed to save WWAN state to NVRAM\n");
3523 vdbg_printk(TPACPI_DBG_RFKILL
,
3524 "WWAN state saved to NVRAM\n");
3527 static void wan_exit(void)
3531 if (tpacpi_wan_rfkill
)
3532 rfkill_unregister(tpacpi_wan_rfkill
);
3534 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3538 static int __init
wan_init(struct ibm_init_struct
*iibm
)
3543 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3544 "initializing wan subdriver\n");
3546 TPACPI_ACPIHANDLE_INIT(hkey
);
3548 tp_features
.wan
= hkey_handle
&&
3549 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
3551 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3552 "wan is %s, status 0x%02x\n",
3553 str_supported(tp_features
.wan
),
3556 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3558 tp_features
.wan
= 1;
3560 "wwan switch emulation enabled\n");
3563 if (tp_features
.wan
&&
3564 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
3565 /* no wan hardware present in system */
3566 tp_features
.wan
= 0;
3567 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3568 "wan hardware not installed\n");
3571 if (!tp_features
.wan
)
3574 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3579 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
3582 TPACPI_RFK_WWAN_SW_NAME
,
3585 tpacpi_wan_rfk_get
);
3594 /* procfs -------------------------------------------------------------- */
3595 static int wan_read(char *p
)
3598 int status
= wan_get_radiosw();
3600 tpacpi_disclose_usertask("procfs wan", "read");
3602 if (!tp_features
.wan
)
3603 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3605 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3606 (status
== RFKILL_STATE_UNBLOCKED
) ?
3607 "enabled" : "disabled");
3608 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3614 static int wan_write(char *buf
)
3619 if (!tp_features
.wan
)
3622 while ((cmd
= next_cmd(&buf
))) {
3623 if (strlencmp(cmd
, "enable") == 0) {
3625 } else if (strlencmp(cmd
, "disable") == 0) {
3632 tpacpi_disclose_usertask("procfs wan",
3634 state
? "enable" : "disable");
3635 wan_set_radiosw(state
, 1);
3641 static struct ibm_struct wan_driver_data
= {
3646 .suspend
= wan_suspend
,
3647 .shutdown
= wan_shutdown
,
3650 /*************************************************************************
3655 /* ACPI GUWB/SUWB bits */
3656 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
3657 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
3660 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3662 static struct rfkill
*tpacpi_uwb_rfkill
;
3664 static int uwb_get_radiosw(void)
3668 if (!tp_features
.uwb
)
3671 /* WLSW overrides UWB in firmware/hardware, reflect that */
3672 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3673 return RFKILL_STATE_HARD_BLOCKED
;
3675 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3677 return (tpacpi_uwb_emulstate
) ?
3678 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3681 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
3684 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
3685 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3688 static void uwb_update_rfk(void)
3692 if (!tpacpi_uwb_rfkill
)
3695 status
= uwb_get_radiosw();
3698 rfkill_force_state(tpacpi_uwb_rfkill
, status
);
3700 vdbg_printk(TPACPI_DBG_RFKILL
,
3701 "forced rfkill state to %d\n",
3705 static int uwb_set_radiosw(int radio_on
, int update_rfk
)
3709 if (!tp_features
.uwb
)
3712 /* WLSW overrides UWB in firmware/hardware, but there is no
3713 * reason to risk weird behaviour. */
3714 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3718 vdbg_printk(TPACPI_DBG_RFKILL
,
3719 "will %s UWB\n", radio_on
? "enable" : "disable");
3721 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3723 tpacpi_uwb_emulstate
= !!radio_on
;
3730 status
= (radio_on
) ? TP_ACPI_UWB_RADIOSSW
: 0;
3731 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
3740 /* --------------------------------------------------------------------- */
3742 static int tpacpi_uwb_rfk_get(void *data
, enum rfkill_state
*state
)
3744 int uwbs
= uwb_get_radiosw();
3753 static int tpacpi_uwb_rfk_set(void *data
, enum rfkill_state state
)
3755 dbg_printk(TPACPI_DBG_RFKILL
,
3756 "request to change radio state to %d\n", state
);
3757 return uwb_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3760 static void uwb_exit(void)
3762 if (tpacpi_uwb_rfkill
)
3763 rfkill_unregister(tpacpi_uwb_rfkill
);
3766 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
3771 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3772 "initializing uwb subdriver\n");
3774 TPACPI_ACPIHANDLE_INIT(hkey
);
3776 tp_features
.uwb
= hkey_handle
&&
3777 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
3779 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3780 "uwb is %s, status 0x%02x\n",
3781 str_supported(tp_features
.uwb
),
3784 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3786 tp_features
.uwb
= 1;
3788 "uwb switch emulation enabled\n");
3791 if (tp_features
.uwb
&&
3792 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
3793 /* no uwb hardware present in system */
3794 tp_features
.uwb
= 0;
3795 dbg_printk(TPACPI_DBG_INIT
,
3796 "uwb hardware not installed\n");
3799 if (!tp_features
.uwb
)
3802 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
3805 TPACPI_RFK_UWB_SW_NAME
,
3808 tpacpi_uwb_rfk_get
);
3813 static struct ibm_struct uwb_driver_data
= {
3816 .flags
.experimental
= 1,
3819 /*************************************************************************
3823 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3825 enum video_access_mode
{
3826 TPACPI_VIDEO_NONE
= 0,
3827 TPACPI_VIDEO_570
, /* 570 */
3828 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
3829 TPACPI_VIDEO_NEW
, /* all others */
3832 enum { /* video status flags, based on VIDEO_570 */
3833 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
3834 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
3835 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
3838 enum { /* TPACPI_VIDEO_570 constants */
3839 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
3840 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
3841 * video_status_flags */
3842 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
3843 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
3846 static enum video_access_mode video_supported
;
3847 static int video_orig_autosw
;
3849 static int video_autosw_get(void);
3850 static int video_autosw_set(int enable
);
3852 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3854 static int __init
video_init(struct ibm_init_struct
*iibm
)
3858 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
3860 TPACPI_ACPIHANDLE_INIT(vid
);
3861 TPACPI_ACPIHANDLE_INIT(vid2
);
3863 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
3864 /* G41, assume IVGA doesn't change */
3865 vid_handle
= vid2_handle
;
3868 /* video switching not supported on R30, R31 */
3869 video_supported
= TPACPI_VIDEO_NONE
;
3870 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
3872 video_supported
= TPACPI_VIDEO_570
;
3873 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
3874 /* 600e/x, 770e, 770x */
3875 video_supported
= TPACPI_VIDEO_770
;
3878 video_supported
= TPACPI_VIDEO_NEW
;
3880 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
3881 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
3884 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
3887 static void video_exit(void)
3889 dbg_printk(TPACPI_DBG_EXIT
,
3890 "restoring original video autoswitch mode\n");
3891 if (video_autosw_set(video_orig_autosw
))
3892 printk(TPACPI_ERR
"error while trying to restore original "
3893 "video autoswitch mode\n");
3896 static int video_outputsw_get(void)
3901 switch (video_supported
) {
3902 case TPACPI_VIDEO_570
:
3903 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
3904 TP_ACPI_VIDEO_570_PHSCMD
))
3906 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
3908 case TPACPI_VIDEO_770
:
3909 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3912 status
|= TP_ACPI_VIDEO_S_LCD
;
3913 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3916 status
|= TP_ACPI_VIDEO_S_CRT
;
3918 case TPACPI_VIDEO_NEW
:
3919 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
3920 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3923 status
|= TP_ACPI_VIDEO_S_CRT
;
3925 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
3926 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3929 status
|= TP_ACPI_VIDEO_S_LCD
;
3930 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
3933 status
|= TP_ACPI_VIDEO_S_DVI
;
3942 static int video_outputsw_set(int status
)
3947 switch (video_supported
) {
3948 case TPACPI_VIDEO_570
:
3949 res
= acpi_evalf(NULL
, NULL
,
3950 "\\_SB.PHS2", "vdd",
3951 TP_ACPI_VIDEO_570_PHS2CMD
,
3952 status
| TP_ACPI_VIDEO_570_PHS2SET
);
3954 case TPACPI_VIDEO_770
:
3955 autosw
= video_autosw_get();
3959 res
= video_autosw_set(1);
3962 res
= acpi_evalf(vid_handle
, NULL
,
3963 "ASWT", "vdd", status
* 0x100, 0);
3964 if (!autosw
&& video_autosw_set(autosw
)) {
3966 "video auto-switch left enabled due to error\n");
3970 case TPACPI_VIDEO_NEW
:
3971 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
3972 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
3978 return (res
)? 0 : -EIO
;
3981 static int video_autosw_get(void)
3985 switch (video_supported
) {
3986 case TPACPI_VIDEO_570
:
3987 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
3990 case TPACPI_VIDEO_770
:
3991 case TPACPI_VIDEO_NEW
:
3992 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4002 static int video_autosw_set(int enable
)
4004 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
4009 static int video_outputsw_cycle(void)
4011 int autosw
= video_autosw_get();
4017 switch (video_supported
) {
4018 case TPACPI_VIDEO_570
:
4019 res
= video_autosw_set(1);
4022 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4024 case TPACPI_VIDEO_770
:
4025 case TPACPI_VIDEO_NEW
:
4026 res
= video_autosw_set(1);
4029 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4034 if (!autosw
&& video_autosw_set(autosw
)) {
4036 "video auto-switch left enabled due to error\n");
4040 return (res
)? 0 : -EIO
;
4043 static int video_expand_toggle(void)
4045 switch (video_supported
) {
4046 case TPACPI_VIDEO_570
:
4047 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4049 case TPACPI_VIDEO_770
:
4050 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4052 case TPACPI_VIDEO_NEW
:
4053 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4061 static int video_read(char *p
)
4066 if (video_supported
== TPACPI_VIDEO_NONE
) {
4067 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4071 status
= video_outputsw_get();
4075 autosw
= video_autosw_get();
4079 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4080 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
4081 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
4082 if (video_supported
== TPACPI_VIDEO_NEW
)
4083 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
4084 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
4085 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
4086 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
4087 if (video_supported
== TPACPI_VIDEO_NEW
)
4088 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
4089 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
4090 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4095 static int video_write(char *buf
)
4098 int enable
, disable
, status
;
4101 if (video_supported
== TPACPI_VIDEO_NONE
)
4107 while ((cmd
= next_cmd(&buf
))) {
4108 if (strlencmp(cmd
, "lcd_enable") == 0) {
4109 enable
|= TP_ACPI_VIDEO_S_LCD
;
4110 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4111 disable
|= TP_ACPI_VIDEO_S_LCD
;
4112 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4113 enable
|= TP_ACPI_VIDEO_S_CRT
;
4114 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4115 disable
|= TP_ACPI_VIDEO_S_CRT
;
4116 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4117 strlencmp(cmd
, "dvi_enable") == 0) {
4118 enable
|= TP_ACPI_VIDEO_S_DVI
;
4119 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4120 strlencmp(cmd
, "dvi_disable") == 0) {
4121 disable
|= TP_ACPI_VIDEO_S_DVI
;
4122 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4123 res
= video_autosw_set(1);
4126 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4127 res
= video_autosw_set(0);
4130 } else if (strlencmp(cmd
, "video_switch") == 0) {
4131 res
= video_outputsw_cycle();
4134 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4135 res
= video_expand_toggle();
4142 if (enable
|| disable
) {
4143 status
= video_outputsw_get();
4146 res
= video_outputsw_set((status
& ~disable
) | enable
);
4154 static struct ibm_struct video_driver_data
= {
4157 .write
= video_write
,
4161 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4163 /*************************************************************************
4164 * Light (thinklight) subdriver
4167 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4168 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4170 static int light_get_status(void)
4174 if (tp_features
.light_status
) {
4175 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4183 static int light_set_status(int status
)
4187 if (tp_features
.light
) {
4189 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4191 TP_CMOS_THINKLIGHT_ON
:
4192 TP_CMOS_THINKLIGHT_OFF
);
4194 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4197 return (rc
)? 0 : -EIO
;
4203 static void light_set_status_worker(struct work_struct
*work
)
4205 struct tpacpi_led_classdev
*data
=
4206 container_of(work
, struct tpacpi_led_classdev
, work
);
4208 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4209 light_set_status((data
->new_brightness
!= LED_OFF
));
4212 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4213 enum led_brightness brightness
)
4215 struct tpacpi_led_classdev
*data
=
4216 container_of(led_cdev
,
4217 struct tpacpi_led_classdev
,
4219 data
->new_brightness
= brightness
;
4220 queue_work(tpacpi_wq
, &data
->work
);
4223 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4225 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4228 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4230 .name
= "tpacpi::thinklight",
4231 .brightness_set
= &light_sysfs_set
,
4232 .brightness_get
= &light_sysfs_get
,
4236 static int __init
light_init(struct ibm_init_struct
*iibm
)
4240 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4242 TPACPI_ACPIHANDLE_INIT(ledb
);
4243 TPACPI_ACPIHANDLE_INIT(lght
);
4244 TPACPI_ACPIHANDLE_INIT(cmos
);
4245 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4247 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4248 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4250 if (tp_features
.light
)
4251 /* light status not supported on
4252 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4253 tp_features
.light_status
=
4254 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4256 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4257 str_supported(tp_features
.light
),
4258 str_supported(tp_features
.light_status
));
4260 if (!tp_features
.light
)
4263 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4264 &tpacpi_led_thinklight
.led_classdev
);
4267 tp_features
.light
= 0;
4268 tp_features
.light_status
= 0;
4276 static void light_exit(void)
4278 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4279 if (work_pending(&tpacpi_led_thinklight
.work
))
4280 flush_workqueue(tpacpi_wq
);
4283 static int light_read(char *p
)
4288 if (!tp_features
.light
) {
4289 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4290 } else if (!tp_features
.light_status
) {
4291 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4292 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4294 status
= light_get_status();
4297 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4298 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4304 static int light_write(char *buf
)
4309 if (!tp_features
.light
)
4312 while ((cmd
= next_cmd(&buf
))) {
4313 if (strlencmp(cmd
, "on") == 0) {
4315 } else if (strlencmp(cmd
, "off") == 0) {
4321 return light_set_status(newstatus
);
4324 static struct ibm_struct light_driver_data
= {
4327 .write
= light_write
,
4331 /*************************************************************************
4335 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4337 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
4338 static int dock_read(char *p
);
4339 static int dock_write(char *buf
);
4341 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
4342 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4343 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4344 "\\_SB.PCI.ISA.SLCE", /* 570 */
4345 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4347 /* don't list other alternatives as we install a notify handler on the 570 */
4348 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
4350 static const struct acpi_device_id ibm_pci_device_ids
[] = {
4351 {PCI_ROOT_HID_STRING
, 0},
4355 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
4357 .notify
= dock_notify
,
4358 .handle
= &dock_handle
,
4359 .type
= ACPI_SYSTEM_NOTIFY
,
4362 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4363 * We just use it to get notifications of dock hotplug
4364 * in very old thinkpads */
4365 .hid
= ibm_pci_device_ids
,
4366 .notify
= dock_notify
,
4367 .handle
= &pci_handle
,
4368 .type
= ACPI_SYSTEM_NOTIFY
,
4372 static struct ibm_struct dock_driver_data
[2] = {
4376 .write
= dock_write
,
4377 .acpi
= &ibm_dock_acpidriver
[0],
4381 .acpi
= &ibm_dock_acpidriver
[1],
4385 #define dock_docked() (_sta(dock_handle) & 1)
4387 static int __init
dock_init(struct ibm_init_struct
*iibm
)
4389 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
4391 TPACPI_ACPIHANDLE_INIT(dock
);
4393 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
4394 str_supported(dock_handle
!= NULL
));
4396 return (dock_handle
)? 0 : 1;
4399 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
4403 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
4405 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
4406 dock_driver_data
[0].flags
.acpi_notify_installed
) {
4407 TPACPI_ACPIHANDLE_INIT(pci
);
4408 dock2_needed
= (pci_handle
!= NULL
);
4409 vdbg_printk(TPACPI_DBG_INIT
,
4410 "dock PCI handler for the TP 570 is %s\n",
4411 str_supported(dock2_needed
));
4413 vdbg_printk(TPACPI_DBG_INIT
,
4414 "dock subdriver part 2 not required\n");
4418 return (dock2_needed
)? 0 : 1;
4421 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
4423 int docked
= dock_docked();
4424 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
4425 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
4428 if (event
== 1 && !pci
) /* 570 */
4429 data
= 1; /* button */
4430 else if (event
== 1 && pci
) /* 570 */
4431 data
= 3; /* dock */
4432 else if (event
== 3 && docked
)
4433 data
= 1; /* button */
4434 else if (event
== 3 && !docked
)
4435 data
= 2; /* undock */
4436 else if (event
== 0 && docked
)
4437 data
= 3; /* dock */
4439 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
4440 event
, _sta(dock_handle
));
4441 data
= 0; /* unknown */
4443 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
4444 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4445 dev_name(&ibm
->acpi
->device
->dev
),
4449 static int dock_read(char *p
)
4452 int docked
= dock_docked();
4455 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4457 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
4459 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
4460 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
4466 static int dock_write(char *buf
)
4473 while ((cmd
= next_cmd(&buf
))) {
4474 if (strlencmp(cmd
, "undock") == 0) {
4475 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
4476 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
4478 } else if (strlencmp(cmd
, "dock") == 0) {
4479 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
4488 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4490 /*************************************************************************
4494 #ifdef CONFIG_THINKPAD_ACPI_BAY
4496 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4497 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4498 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4499 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4500 ); /* A21e, R30, R31 */
4501 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
4502 "_EJ0", /* all others */
4503 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4504 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4505 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4507 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
4511 static int __init
bay_init(struct ibm_init_struct
*iibm
)
4513 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
4515 TPACPI_ACPIHANDLE_INIT(bay
);
4517 TPACPI_ACPIHANDLE_INIT(bay_ej
);
4518 TPACPI_ACPIHANDLE_INIT(bay2
);
4520 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
4522 tp_features
.bay_status
= bay_handle
&&
4523 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
4524 tp_features
.bay_status2
= bay2_handle
&&
4525 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
4527 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
4528 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
4529 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
4530 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
4532 vdbg_printk(TPACPI_DBG_INIT
,
4533 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4534 str_supported(tp_features
.bay_status
),
4535 str_supported(tp_features
.bay_eject
),
4536 str_supported(tp_features
.bay_status2
),
4537 str_supported(tp_features
.bay_eject2
));
4539 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
4540 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
4543 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
4545 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
4546 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4547 dev_name(&ibm
->acpi
->device
->dev
),
4551 #define bay_occupied(b) (_sta(b##_handle) & 1)
4553 static int bay_read(char *p
)
4556 int occupied
= bay_occupied(bay
);
4557 int occupied2
= bay_occupied(bay2
);
4560 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
4561 tp_features
.bay_status
?
4562 (occupied
? "occupied" : "unoccupied") :
4564 if (tp_features
.bay_status2
)
4565 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
4566 "occupied" : "unoccupied");
4568 eject
= tp_features
.bay_eject
&& occupied
;
4569 eject2
= tp_features
.bay_eject2
&& occupied2
;
4571 if (eject
&& eject2
)
4572 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
4574 len
+= sprintf(p
+ len
, "commands:\teject\n");
4576 len
+= sprintf(p
+ len
, "commands:\teject2\n");
4581 static int bay_write(char *buf
)
4585 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
4588 while ((cmd
= next_cmd(&buf
))) {
4589 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
4590 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
4592 } else if (tp_features
.bay_eject2
&&
4593 strlencmp(cmd
, "eject2") == 0) {
4594 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
4603 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
4604 .notify
= bay_notify
,
4605 .handle
= &bay_handle
,
4606 .type
= ACPI_SYSTEM_NOTIFY
,
4609 static struct ibm_struct bay_driver_data
= {
4613 .acpi
= &ibm_bay_acpidriver
,
4616 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4618 /*************************************************************************
4622 /* sysfs cmos_command -------------------------------------------------- */
4623 static ssize_t
cmos_command_store(struct device
*dev
,
4624 struct device_attribute
*attr
,
4625 const char *buf
, size_t count
)
4627 unsigned long cmos_cmd
;
4630 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4633 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4634 return (res
)? res
: count
;
4637 static struct device_attribute dev_attr_cmos_command
=
4638 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4640 /* --------------------------------------------------------------------- */
4642 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4646 vdbg_printk(TPACPI_DBG_INIT
,
4647 "initializing cmos commands subdriver\n");
4649 TPACPI_ACPIHANDLE_INIT(cmos
);
4651 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4652 str_supported(cmos_handle
!= NULL
));
4654 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4658 return (cmos_handle
)? 0 : 1;
4661 static void cmos_exit(void)
4663 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4666 static int cmos_read(char *p
)
4670 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4671 R30, R31, T20-22, X20-21 */
4673 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4675 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4676 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4682 static int cmos_write(char *buf
)
4687 while ((cmd
= next_cmd(&buf
))) {
4688 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4689 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4694 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4702 static struct ibm_struct cmos_driver_data
= {
4705 .write
= cmos_write
,
4709 /*************************************************************************
4713 enum led_access_mode
{
4714 TPACPI_LED_NONE
= 0,
4715 TPACPI_LED_570
, /* 570 */
4716 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4717 TPACPI_LED_NEW
, /* all others */
4720 enum { /* For TPACPI_LED_OLD */
4721 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4722 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4723 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4732 static enum led_access_mode led_supported
;
4734 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4735 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4736 /* T20-22, X20-21 */
4737 "LED", /* all others */
4740 #define TPACPI_LED_NUMLEDS 8
4741 static struct tpacpi_led_classdev
*tpacpi_leds
;
4742 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
4743 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
4744 /* there's a limit of 19 chars + NULL before 2.6.26 */
4746 "tpacpi:orange:batt",
4747 "tpacpi:green:batt",
4748 "tpacpi::dock_active",
4749 "tpacpi::bay_active",
4750 "tpacpi::dock_batt",
4751 "tpacpi::unknown_led",
4754 #define TPACPI_SAFE_LEDS 0x0081U
4756 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
4758 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4761 return (TPACPI_SAFE_LEDS
& (1 << led
)) == 0;
4765 static int led_get_status(const unsigned int led
)
4768 enum led_status_t led_s
;
4770 switch (led_supported
) {
4771 case TPACPI_LED_570
:
4772 if (!acpi_evalf(ec_handle
,
4773 &status
, "GLED", "dd", 1 << led
))
4775 led_s
= (status
== 0)?
4780 tpacpi_led_state_cache
[led
] = led_s
;
4789 static int led_set_status(const unsigned int led
,
4790 const enum led_status_t ledstatus
)
4792 /* off, on, blink. Index is led_status_t */
4793 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
4794 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
4798 switch (led_supported
) {
4799 case TPACPI_LED_570
:
4801 if (unlikely(led
> 7))
4803 if (unlikely(tpacpi_is_led_restricted(led
)))
4805 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4806 (1 << led
), led_sled_arg1
[ledstatus
]))
4809 case TPACPI_LED_OLD
:
4810 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4811 if (unlikely(led
> 7))
4813 if (unlikely(tpacpi_is_led_restricted(led
)))
4815 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
4817 rc
= ec_write(TPACPI_LED_EC_HLBL
,
4818 (ledstatus
== TPACPI_LED_BLINK
) << led
);
4820 rc
= ec_write(TPACPI_LED_EC_HLCL
,
4821 (ledstatus
!= TPACPI_LED_OFF
) << led
);
4823 case TPACPI_LED_NEW
:
4825 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
4827 if (unlikely(tpacpi_is_led_restricted(led
)))
4829 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4830 led
, led_led_arg1
[ledstatus
]))
4838 tpacpi_led_state_cache
[led
] = ledstatus
;
4843 static void led_sysfs_set_status(unsigned int led
,
4844 enum led_brightness brightness
)
4847 (brightness
== LED_OFF
) ?
4849 (tpacpi_led_state_cache
[led
] == TPACPI_LED_BLINK
) ?
4850 TPACPI_LED_BLINK
: TPACPI_LED_ON
);
4853 static void led_set_status_worker(struct work_struct
*work
)
4855 struct tpacpi_led_classdev
*data
=
4856 container_of(work
, struct tpacpi_led_classdev
, work
);
4858 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4859 led_sysfs_set_status(data
->led
, data
->new_brightness
);
4862 static void led_sysfs_set(struct led_classdev
*led_cdev
,
4863 enum led_brightness brightness
)
4865 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4866 struct tpacpi_led_classdev
, led_classdev
);
4868 data
->new_brightness
= brightness
;
4869 queue_work(tpacpi_wq
, &data
->work
);
4872 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
4873 unsigned long *delay_on
, unsigned long *delay_off
)
4875 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4876 struct tpacpi_led_classdev
, led_classdev
);
4878 /* Can we choose the flash rate? */
4879 if (*delay_on
== 0 && *delay_off
== 0) {
4880 /* yes. set them to the hardware blink rate (1 Hz) */
4881 *delay_on
= 500; /* ms */
4882 *delay_off
= 500; /* ms */
4883 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
4886 data
->new_brightness
= TPACPI_LED_BLINK
;
4887 queue_work(tpacpi_wq
, &data
->work
);
4892 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
4896 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4897 struct tpacpi_led_classdev
, led_classdev
);
4899 rc
= led_get_status(data
->led
);
4901 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
4902 rc
= LED_OFF
; /* no error handling in led class :( */
4909 static void led_exit(void)
4913 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4914 if (tpacpi_leds
[i
].led_classdev
.name
)
4915 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
4921 static int __init
tpacpi_init_led(unsigned int led
)
4925 tpacpi_leds
[led
].led
= led
;
4927 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
4928 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
4929 if (led_supported
== TPACPI_LED_570
)
4930 tpacpi_leds
[led
].led_classdev
.brightness_get
=
4933 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
4935 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
4937 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4938 &tpacpi_leds
[led
].led_classdev
);
4940 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
4945 static int __init
led_init(struct ibm_init_struct
*iibm
)
4950 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
4952 TPACPI_ACPIHANDLE_INIT(led
);
4955 /* led not supported on R30, R31 */
4956 led_supported
= TPACPI_LED_NONE
;
4957 else if (strlencmp(led_path
, "SLED") == 0)
4959 led_supported
= TPACPI_LED_570
;
4960 else if (strlencmp(led_path
, "SYSL") == 0)
4961 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4962 led_supported
= TPACPI_LED_OLD
;
4965 led_supported
= TPACPI_LED_NEW
;
4967 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
4968 str_supported(led_supported
), led_supported
);
4970 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
4973 printk(TPACPI_ERR
"Out of memory for LED data\n");
4977 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4978 if (!tpacpi_is_led_restricted(i
)) {
4979 rc
= tpacpi_init_led(i
);
4987 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4988 if (led_supported
!= TPACPI_LED_NONE
)
4989 printk(TPACPI_NOTICE
4990 "warning: userspace override of important "
4991 "firmware LEDs is enabled\n");
4993 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
4996 #define str_led_status(s) \
4997 ((s) == TPACPI_LED_OFF ? "off" : \
4998 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5000 static int led_read(char *p
)
5004 if (!led_supported
) {
5005 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5008 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5010 if (led_supported
== TPACPI_LED_570
) {
5013 for (i
= 0; i
< 8; i
++) {
5014 status
= led_get_status(i
);
5017 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
5018 i
, str_led_status(status
));
5022 len
+= sprintf(p
+ len
, "commands:\t"
5023 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5028 static int led_write(char *buf
)
5032 enum led_status_t s
;
5037 while ((cmd
= next_cmd(&buf
))) {
5038 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
5041 if (strstr(cmd
, "off")) {
5043 } else if (strstr(cmd
, "on")) {
5045 } else if (strstr(cmd
, "blink")) {
5046 s
= TPACPI_LED_BLINK
;
5051 rc
= led_set_status(led
, s
);
5059 static struct ibm_struct led_driver_data
= {
5066 /*************************************************************************
5070 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5072 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5074 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5076 TPACPI_ACPIHANDLE_INIT(beep
);
5078 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5079 str_supported(beep_handle
!= NULL
));
5081 return (beep_handle
)? 0 : 1;
5084 static int beep_read(char *p
)
5089 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5091 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5092 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5098 static int beep_write(char *buf
)
5106 while ((cmd
= next_cmd(&buf
))) {
5107 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5108 beep_cmd
>= 0 && beep_cmd
<= 17) {
5112 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
5119 static struct ibm_struct beep_driver_data
= {
5122 .write
= beep_write
,
5125 /*************************************************************************
5129 enum thermal_access_mode
{
5130 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5131 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5132 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5133 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5134 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5137 enum { /* TPACPI_THERMAL_TPEC_* */
5138 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5139 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5140 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5143 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5144 struct ibm_thermal_sensors_struct
{
5145 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5148 static enum thermal_access_mode thermal_read_mode
;
5150 /* idx is zero-based */
5151 static int thermal_get_sensor(int idx
, s32
*value
)
5157 t
= TP_EC_THERMAL_TMP0
;
5159 switch (thermal_read_mode
) {
5160 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5161 case TPACPI_THERMAL_TPEC_16
:
5162 if (idx
>= 8 && idx
<= 15) {
5163 t
= TP_EC_THERMAL_TMP8
;
5168 case TPACPI_THERMAL_TPEC_8
:
5170 if (!acpi_ec_read(t
+ idx
, &tmp
))
5172 *value
= tmp
* 1000;
5177 case TPACPI_THERMAL_ACPI_UPDT
:
5179 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5180 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5182 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5184 *value
= (t
- 2732) * 100;
5189 case TPACPI_THERMAL_ACPI_TMP07
:
5191 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5192 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5194 if (t
> 127 || t
< -127)
5195 t
= TP_EC_THERMAL_TMP_NA
;
5201 case TPACPI_THERMAL_NONE
:
5209 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5220 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5223 for (i
= 0 ; i
< n
; i
++) {
5224 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5232 /* sysfs temp##_input -------------------------------------------------- */
5234 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5235 struct device_attribute
*attr
,
5238 struct sensor_device_attribute
*sensor_attr
=
5239 to_sensor_dev_attr(attr
);
5240 int idx
= sensor_attr
->index
;
5244 res
= thermal_get_sensor(idx
, &value
);
5247 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
5250 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5253 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5254 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5255 thermal_temp_input_show, NULL, _idxB)
5257 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5258 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5259 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5260 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5261 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5262 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5263 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5264 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5265 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5266 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5267 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5268 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5269 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5270 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5271 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5272 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5273 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5276 #define THERMAL_ATTRS(X) \
5277 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5279 static struct attribute
*thermal_temp_input_attr
[] = {
5299 static const struct attribute_group thermal_temp_input16_group
= {
5300 .attrs
= thermal_temp_input_attr
5303 static const struct attribute_group thermal_temp_input8_group
= {
5304 .attrs
= &thermal_temp_input_attr
[8]
5307 #undef THERMAL_SENSOR_ATTR_TEMP
5308 #undef THERMAL_ATTRS
5310 /* --------------------------------------------------------------------- */
5312 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5319 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5321 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5323 if (thinkpad_id
.ec_model
) {
5325 * Direct EC access mode: sensors at registers
5326 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5327 * non-implemented, thermal sensors return 0x80 when
5332 for (i
= 0; i
< 8; i
++) {
5333 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5339 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5347 /* This is sheer paranoia, but we handle it anyway */
5350 "ThinkPad ACPI EC access misbehaving, "
5351 "falling back to ACPI TMPx access "
5353 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5356 "ThinkPad ACPI EC access misbehaving, "
5357 "disabling thermal sensors access\n");
5358 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5363 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5365 } else if (acpi_tmp7
) {
5366 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5367 /* 600e/x, 770e, 770x */
5368 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5370 /* Standard ACPI TMPx access, max 8 sensors */
5371 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5374 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5375 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5378 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5379 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5382 switch (thermal_read_mode
) {
5383 case TPACPI_THERMAL_TPEC_16
:
5384 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5385 &thermal_temp_input16_group
);
5389 case TPACPI_THERMAL_TPEC_8
:
5390 case TPACPI_THERMAL_ACPI_TMP07
:
5391 case TPACPI_THERMAL_ACPI_UPDT
:
5392 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5393 &thermal_temp_input8_group
);
5397 case TPACPI_THERMAL_NONE
:
5405 static void thermal_exit(void)
5407 switch (thermal_read_mode
) {
5408 case TPACPI_THERMAL_TPEC_16
:
5409 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5410 &thermal_temp_input16_group
);
5412 case TPACPI_THERMAL_TPEC_8
:
5413 case TPACPI_THERMAL_ACPI_TMP07
:
5414 case TPACPI_THERMAL_ACPI_UPDT
:
5415 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5416 &thermal_temp_input16_group
);
5418 case TPACPI_THERMAL_NONE
:
5424 static int thermal_read(char *p
)
5428 struct ibm_thermal_sensors_struct t
;
5430 n
= thermal_get_sensors(&t
);
5431 if (unlikely(n
< 0))
5434 len
+= sprintf(p
+ len
, "temperatures:\t");
5437 for (i
= 0; i
< (n
- 1); i
++)
5438 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5439 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5441 len
+= sprintf(p
+ len
, "not supported\n");
5446 static struct ibm_struct thermal_driver_data
= {
5448 .read
= thermal_read
,
5449 .exit
= thermal_exit
,
5452 /*************************************************************************
5456 static u8 ecdump_regs
[256];
5458 static int ecdump_read(char *p
)
5464 len
+= sprintf(p
+ len
, "EC "
5465 " +00 +01 +02 +03 +04 +05 +06 +07"
5466 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5467 for (i
= 0; i
< 256; i
+= 16) {
5468 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5469 for (j
= 0; j
< 16; j
++) {
5470 if (!acpi_ec_read(i
+ j
, &v
))
5472 if (v
!= ecdump_regs
[i
+ j
])
5473 len
+= sprintf(p
+ len
, " *%02x", v
);
5475 len
+= sprintf(p
+ len
, " %02x", v
);
5476 ecdump_regs
[i
+ j
] = v
;
5478 len
+= sprintf(p
+ len
, "\n");
5483 /* These are way too dangerous to advertise openly... */
5485 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5486 " (<offset> is 00-ff, <value> is 00-ff)\n");
5487 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5488 " (<offset> is 00-ff, <value> is 0-255)\n");
5493 static int ecdump_write(char *buf
)
5498 while ((cmd
= next_cmd(&buf
))) {
5499 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5501 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5505 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5506 if (!acpi_ec_write(i
, v
))
5515 static struct ibm_struct ecdump_driver_data
= {
5517 .read
= ecdump_read
,
5518 .write
= ecdump_write
,
5519 .flags
.experimental
= 1,
5522 /*************************************************************************
5523 * Backlight/brightness subdriver
5526 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5529 TP_EC_BACKLIGHT
= 0x31,
5531 /* TP_EC_BACKLIGHT bitmasks */
5532 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5533 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5534 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5537 static struct backlight_device
*ibm_backlight_device
;
5538 static int brightness_mode
;
5539 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5541 static struct mutex brightness_mutex
;
5544 * ThinkPads can read brightness from two places: EC 0x31, or
5545 * CMOS NVRAM byte 0x5E, bits 0-3.
5547 * EC 0x31 has the following layout
5548 * Bit 7: unknown function
5549 * Bit 6: unknown function
5550 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5551 * Bit 4: must be set to zero to avoid problems
5552 * Bit 3-0: backlight brightness level
5554 * brightness_get_raw returns status data in the EC 0x31 layout
5556 static int brightness_get_raw(int *status
)
5558 u8 lec
= 0, lcmos
= 0, level
= 0;
5560 if (brightness_mode
& 1) {
5561 if (!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
))
5563 level
= lec
& TP_EC_BACKLIGHT_LVLMSK
;
5565 if (brightness_mode
& 2) {
5566 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5567 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5568 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5569 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
5573 if (brightness_mode
== 3) {
5574 *status
= lec
; /* Prefer EC, CMOS is just a backing store */
5575 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5577 tp_warned
.bright_cmos_ec_unsync
= 0;
5579 if (!tp_warned
.bright_cmos_ec_unsync
) {
5581 "CMOS NVRAM (%u) and EC (%u) do not "
5582 "agree on display brightness level\n",
5583 (unsigned int) lcmos
,
5584 (unsigned int) lec
);
5585 tp_warned
.bright_cmos_ec_unsync
= 1;
5596 /* May return EINTR which can always be mapped to ERESTARTSYS */
5597 static int brightness_set(int value
)
5599 int cmos_cmd
, inc
, i
, res
;
5603 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
5607 res
= mutex_lock_killable(&brightness_mutex
);
5611 res
= brightness_get_raw(¤t_value
);
5615 command_bits
= current_value
& TP_EC_BACKLIGHT_CMDMSK
;
5616 current_value
&= TP_EC_BACKLIGHT_LVLMSK
;
5618 cmos_cmd
= value
> current_value
?
5619 TP_CMOS_BRIGHTNESS_UP
:
5620 TP_CMOS_BRIGHTNESS_DOWN
;
5621 inc
= (value
> current_value
)? 1 : -1;
5624 for (i
= current_value
; i
!= value
; i
+= inc
) {
5625 if ((brightness_mode
& 2) &&
5626 issue_thinkpad_cmos_command(cmos_cmd
)) {
5630 if ((brightness_mode
& 1) &&
5631 !acpi_ec_write(TP_EC_BACKLIGHT
,
5632 (i
+ inc
) | command_bits
)) {
5639 mutex_unlock(&brightness_mutex
);
5643 /* sysfs backlight class ----------------------------------------------- */
5645 static int brightness_update_status(struct backlight_device
*bd
)
5647 /* it is the backlight class's job (caller) to handle
5648 * EINTR and other errors properly */
5649 return brightness_set(
5650 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
5651 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
5652 bd
->props
.brightness
: 0);
5655 static int brightness_get(struct backlight_device
*bd
)
5659 res
= brightness_get_raw(&status
);
5661 return 0; /* FIXME: teach backlight about error handling */
5663 return status
& TP_EC_BACKLIGHT_LVLMSK
;
5666 static struct backlight_ops ibm_backlight_data
= {
5667 .get_brightness
= brightness_get
,
5668 .update_status
= brightness_update_status
,
5671 /* --------------------------------------------------------------------- */
5673 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
5677 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
5679 mutex_init(&brightness_mutex
);
5682 * We always attempt to detect acpi support, so as to switch
5683 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5684 * going to publish a backlight interface
5686 b
= tpacpi_check_std_acpi_brightness_support();
5689 if (acpi_video_backlight_support()) {
5690 if (brightness_enable
> 1) {
5691 printk(TPACPI_NOTICE
5692 "Standard ACPI backlight interface "
5693 "available, not loading native one.\n");
5695 } else if (brightness_enable
== 1) {
5696 printk(TPACPI_NOTICE
5697 "Backlight control force enabled, even if standard "
5698 "ACPI backlight interface is available\n");
5701 if (brightness_enable
> 1) {
5702 printk(TPACPI_NOTICE
5703 "Standard ACPI backlight interface not "
5704 "available, thinkpad_acpi native "
5705 "brightness control enabled\n");
5710 if (!brightness_enable
) {
5711 dbg_printk(TPACPI_DBG_INIT
,
5712 "brightness support disabled by "
5713 "module parameter\n");
5719 "Unsupported brightness interface, "
5720 "please contact %s\n", TPACPI_MAIL
);
5724 tp_features
.bright_16levels
= 1;
5726 if (!brightness_mode
) {
5727 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
5728 brightness_mode
= 2;
5730 brightness_mode
= 3;
5732 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
5736 if (brightness_mode
> 3)
5739 if (brightness_get_raw(&b
) < 0)
5742 if (tp_features
.bright_16levels
)
5744 "detected a 16-level brightness capable ThinkPad\n");
5746 ibm_backlight_device
= backlight_device_register(
5747 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
5748 &ibm_backlight_data
);
5749 if (IS_ERR(ibm_backlight_device
)) {
5750 printk(TPACPI_ERR
"Could not register backlight device\n");
5751 return PTR_ERR(ibm_backlight_device
);
5753 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
5755 ibm_backlight_device
->props
.max_brightness
=
5756 (tp_features
.bright_16levels
)? 15 : 7;
5757 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
5758 backlight_update_status(ibm_backlight_device
);
5763 static void brightness_exit(void)
5765 if (ibm_backlight_device
) {
5766 vdbg_printk(TPACPI_DBG_EXIT
,
5767 "calling backlight_device_unregister()\n");
5768 backlight_device_unregister(ibm_backlight_device
);
5772 static int brightness_read(char *p
)
5777 level
= brightness_get(NULL
);
5779 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5781 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
5782 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
5783 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5784 " (<level> is 0-%d)\n",
5785 (tp_features
.bright_16levels
) ? 15 : 7);
5791 static int brightness_write(char *buf
)
5796 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
5798 level
= brightness_get(NULL
);
5802 while ((cmd
= next_cmd(&buf
))) {
5803 if (strlencmp(cmd
, "up") == 0) {
5804 if (level
< max_level
)
5806 } else if (strlencmp(cmd
, "down") == 0) {
5809 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
5810 level
>= 0 && level
<= max_level
) {
5817 * Now we know what the final level should be, so we try to set it.
5818 * Doing it this way makes the syscall restartable in case of EINTR
5820 rc
= brightness_set(level
);
5821 return (rc
== -EINTR
)? ERESTARTSYS
: rc
;
5824 static struct ibm_struct brightness_driver_data
= {
5825 .name
= "brightness",
5826 .read
= brightness_read
,
5827 .write
= brightness_write
,
5828 .exit
= brightness_exit
,
5831 /*************************************************************************
5835 static int volume_offset
= 0x30;
5837 static int volume_read(char *p
)
5842 if (!acpi_ec_read(volume_offset
, &level
)) {
5843 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5845 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
5846 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
5847 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
5848 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5849 " (<level> is 0-15)\n");
5855 static int volume_write(char *buf
)
5857 int cmos_cmd
, inc
, i
;
5859 int new_level
, new_mute
;
5862 while ((cmd
= next_cmd(&buf
))) {
5863 if (!acpi_ec_read(volume_offset
, &level
))
5865 new_mute
= mute
= level
& 0x40;
5866 new_level
= level
= level
& 0xf;
5868 if (strlencmp(cmd
, "up") == 0) {
5872 new_level
= level
== 15 ? 15 : level
+ 1;
5873 } else if (strlencmp(cmd
, "down") == 0) {
5877 new_level
= level
== 0 ? 0 : level
- 1;
5878 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
5879 new_level
>= 0 && new_level
<= 15) {
5881 } else if (strlencmp(cmd
, "mute") == 0) {
5886 if (new_level
!= level
) {
5887 /* mute doesn't change */
5889 cmos_cmd
= (new_level
> level
) ?
5890 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
5891 inc
= new_level
> level
? 1 : -1;
5893 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
5894 !acpi_ec_write(volume_offset
, level
)))
5897 for (i
= level
; i
!= new_level
; i
+= inc
)
5898 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5899 !acpi_ec_write(volume_offset
, i
+ inc
))
5903 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
5904 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
5909 if (new_mute
!= mute
) {
5910 /* level doesn't change */
5912 cmos_cmd
= (new_mute
) ?
5913 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
5915 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5916 !acpi_ec_write(volume_offset
, level
+ new_mute
))
5924 static struct ibm_struct volume_driver_data
= {
5926 .read
= volume_read
,
5927 .write
= volume_write
,
5930 /*************************************************************************
5937 * TPACPI_FAN_RD_ACPI_GFAN:
5938 * ACPI GFAN method: returns fan level
5940 * see TPACPI_FAN_WR_ACPI_SFAN
5941 * EC 0x2f (HFSP) not available if GFAN exists
5943 * TPACPI_FAN_WR_ACPI_SFAN:
5944 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5946 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5949 * TPACPI_FAN_WR_TPEC:
5950 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5951 * Supported on almost all ThinkPads
5953 * Fan speed changes of any sort (including those caused by the
5954 * disengaged mode) are usually done slowly by the firmware as the
5955 * maximum ammount of fan duty cycle change per second seems to be
5958 * Reading is not available if GFAN exists.
5959 * Writing is not available if SFAN exists.
5962 * 7 automatic mode engaged;
5963 * (default operation mode of the ThinkPad)
5964 * fan level is ignored in this mode.
5965 * 6 full speed mode (takes precedence over bit 7);
5966 * not available on all thinkpads. May disable
5967 * the tachometer while the fan controller ramps up
5968 * the speed (which can take up to a few *minutes*).
5969 * Speeds up fan to 100% duty-cycle, which is far above
5970 * the standard RPM levels. It is not impossible that
5971 * it could cause hardware damage.
5972 * 5-3 unused in some models. Extra bits for fan level
5973 * in others, but still useless as all values above
5974 * 7 map to the same speed as level 7 in these models.
5975 * 2-0 fan level (0..7 usually)
5977 * 0x07 = max (set when temperatures critical)
5978 * Some ThinkPads may have other levels, see
5979 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5981 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5982 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5983 * does so, its initial value is meaningless (0x07).
5985 * For firmware bugs, refer to:
5986 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5990 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5991 * Main fan tachometer reading (in RPM)
5993 * This register is present on all ThinkPads with a new-style EC, and
5994 * it is known not to be present on the A21m/e, and T22, as there is
5995 * something else in offset 0x84 according to the ACPI DSDT. Other
5996 * ThinkPads from this same time period (and earlier) probably lack the
5997 * tachometer as well.
5999 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
6000 * was never fixed by IBM to report the EC firmware version string
6001 * probably support the tachometer (like the early X models), so
6002 * detecting it is quite hard. We need more data to know for sure.
6004 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6007 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6010 * For firmware bugs, refer to:
6011 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6013 * TPACPI_FAN_WR_ACPI_FANS:
6014 * ThinkPad X31, X40, X41. Not available in the X60.
6016 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6017 * high speed. ACPI DSDT seems to map these three speeds to levels
6018 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6019 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6021 * The speeds are stored on handles
6022 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6024 * There are three default speed sets, acessible as handles:
6025 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6027 * ACPI DSDT switches which set is in use depending on various
6030 * TPACPI_FAN_WR_TPEC is also available and should be used to
6031 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6032 * but the ACPI tables just mention level 7.
6035 enum { /* Fan control constants */
6036 fan_status_offset
= 0x2f, /* EC register 0x2f */
6037 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6038 * 0x84 must be read before 0x85 */
6040 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
6041 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
6043 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
6046 enum fan_status_access_mode
{
6047 TPACPI_FAN_NONE
= 0, /* No fan status or control */
6048 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
6049 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6052 enum fan_control_access_mode
{
6053 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
6054 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
6055 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
6056 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
6059 enum fan_control_commands
{
6060 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
6061 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
6062 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
6063 * and also watchdog cmd */
6066 static int fan_control_allowed
;
6068 static enum fan_status_access_mode fan_status_access_mode
;
6069 static enum fan_control_access_mode fan_control_access_mode
;
6070 static enum fan_control_commands fan_control_commands
;
6072 static u8 fan_control_initial_status
;
6073 static u8 fan_control_desired_level
;
6074 static u8 fan_control_resume_level
;
6075 static int fan_watchdog_maxinterval
;
6077 static struct mutex fan_mutex
;
6079 static void fan_watchdog_fire(struct work_struct
*ignored
);
6080 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
6082 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
6083 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
6084 "\\FSPD", /* 600e/x, 770e, 770x */
6086 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
6087 "JFNS", /* 770x-JL */
6091 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6092 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6093 * be in auto mode (0x80).
6095 * This is corrected by any write to HFSP either by the driver, or
6098 * We assume 0x07 really means auto mode while this quirk is active,
6099 * as this is far more likely than the ThinkPad being in level 7,
6100 * which is only used by the firmware during thermal emergencies.
6103 static void fan_quirk1_detect(void)
6105 /* In some ThinkPads, neither the EC nor the ACPI
6106 * DSDT initialize the HFSP register, and it ends up
6107 * being initially set to 0x07 when it *could* be
6108 * either 0x07 or 0x80.
6110 * Enable for TP-1Y (T43), TP-78 (R51e),
6111 * TP-76 (R52), TP-70 (T43, R52), which are known
6113 if (fan_control_initial_status
== 0x07) {
6114 switch (thinkpad_id
.ec_model
) {
6115 case 0x5931: /* TP-1Y */
6116 case 0x3837: /* TP-78 */
6117 case 0x3637: /* TP-76 */
6118 case 0x3037: /* TP-70 */
6119 printk(TPACPI_NOTICE
6120 "fan_init: initial fan status is unknown, "
6121 "assuming it is in auto mode\n");
6122 tp_features
.fan_ctrl_status_undef
= 1;
6128 static void fan_quirk1_handle(u8
*fan_status
)
6130 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
6131 if (*fan_status
!= fan_control_initial_status
) {
6132 /* something changed the HFSP regisnter since
6133 * driver init time, so it is not undefined
6135 tp_features
.fan_ctrl_status_undef
= 0;
6137 /* Return most likely status. In fact, it
6138 * might be the only possible status */
6139 *fan_status
= TP_EC_FAN_AUTO
;
6145 * Call with fan_mutex held
6147 static void fan_update_desired_level(u8 status
)
6150 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6152 fan_control_desired_level
= 7;
6154 fan_control_desired_level
= status
;
6158 static int fan_get_status(u8
*status
)
6163 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6165 switch (fan_status_access_mode
) {
6166 case TPACPI_FAN_RD_ACPI_GFAN
:
6167 /* 570, 600e/x, 770e, 770x */
6169 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
6177 case TPACPI_FAN_RD_TPEC
:
6178 /* all except 570, 600e/x, 770e, 770x */
6179 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
6182 if (likely(status
)) {
6184 fan_quirk1_handle(status
);
6196 static int fan_get_status_safe(u8
*status
)
6201 if (mutex_lock_killable(&fan_mutex
))
6202 return -ERESTARTSYS
;
6203 rc
= fan_get_status(&s
);
6205 fan_update_desired_level(s
);
6206 mutex_unlock(&fan_mutex
);
6214 static int fan_get_speed(unsigned int *speed
)
6218 switch (fan_status_access_mode
) {
6219 case TPACPI_FAN_RD_TPEC
:
6220 /* all except 570, 600e/x, 770e, 770x */
6221 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
6222 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
6226 *speed
= (hi
<< 8) | lo
;
6237 static int fan_set_level(int level
)
6239 if (!fan_control_allowed
)
6242 switch (fan_control_access_mode
) {
6243 case TPACPI_FAN_WR_ACPI_SFAN
:
6244 if (level
>= 0 && level
<= 7) {
6245 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
6251 case TPACPI_FAN_WR_ACPI_FANS
:
6252 case TPACPI_FAN_WR_TPEC
:
6253 if (!(level
& TP_EC_FAN_AUTO
) &&
6254 !(level
& TP_EC_FAN_FULLSPEED
) &&
6255 ((level
< 0) || (level
> 7)))
6258 /* safety net should the EC not support AUTO
6259 * or FULLSPEED mode bits and just ignore them */
6260 if (level
& TP_EC_FAN_FULLSPEED
)
6261 level
|= 7; /* safety min speed 7 */
6262 else if (level
& TP_EC_FAN_AUTO
)
6263 level
|= 4; /* safety min speed 4 */
6265 if (!acpi_ec_write(fan_status_offset
, level
))
6268 tp_features
.fan_ctrl_status_undef
= 0;
6277 static int fan_set_level_safe(int level
)
6281 if (!fan_control_allowed
)
6284 if (mutex_lock_killable(&fan_mutex
))
6285 return -ERESTARTSYS
;
6287 if (level
== TPACPI_FAN_LAST_LEVEL
)
6288 level
= fan_control_desired_level
;
6290 rc
= fan_set_level(level
);
6292 fan_update_desired_level(level
);
6294 mutex_unlock(&fan_mutex
);
6298 static int fan_set_enable(void)
6303 if (!fan_control_allowed
)
6306 if (mutex_lock_killable(&fan_mutex
))
6307 return -ERESTARTSYS
;
6309 switch (fan_control_access_mode
) {
6310 case TPACPI_FAN_WR_ACPI_FANS
:
6311 case TPACPI_FAN_WR_TPEC
:
6312 rc
= fan_get_status(&s
);
6316 /* Don't go out of emergency fan mode */
6319 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
6322 if (!acpi_ec_write(fan_status_offset
, s
))
6325 tp_features
.fan_ctrl_status_undef
= 0;
6330 case TPACPI_FAN_WR_ACPI_SFAN
:
6331 rc
= fan_get_status(&s
);
6337 /* Set fan to at least level 4 */
6340 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
6350 mutex_unlock(&fan_mutex
);
6354 static int fan_set_disable(void)
6358 if (!fan_control_allowed
)
6361 if (mutex_lock_killable(&fan_mutex
))
6362 return -ERESTARTSYS
;
6365 switch (fan_control_access_mode
) {
6366 case TPACPI_FAN_WR_ACPI_FANS
:
6367 case TPACPI_FAN_WR_TPEC
:
6368 if (!acpi_ec_write(fan_status_offset
, 0x00))
6371 fan_control_desired_level
= 0;
6372 tp_features
.fan_ctrl_status_undef
= 0;
6376 case TPACPI_FAN_WR_ACPI_SFAN
:
6377 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
6380 fan_control_desired_level
= 0;
6388 mutex_unlock(&fan_mutex
);
6392 static int fan_set_speed(int speed
)
6396 if (!fan_control_allowed
)
6399 if (mutex_lock_killable(&fan_mutex
))
6400 return -ERESTARTSYS
;
6403 switch (fan_control_access_mode
) {
6404 case TPACPI_FAN_WR_ACPI_FANS
:
6405 if (speed
>= 0 && speed
<= 65535) {
6406 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
6407 speed
, speed
, speed
))
6417 mutex_unlock(&fan_mutex
);
6421 static void fan_watchdog_reset(void)
6423 static int fan_watchdog_active
;
6425 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
6428 if (fan_watchdog_active
)
6429 cancel_delayed_work(&fan_watchdog_task
);
6431 if (fan_watchdog_maxinterval
> 0 &&
6432 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
6433 fan_watchdog_active
= 1;
6434 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
6435 msecs_to_jiffies(fan_watchdog_maxinterval
6438 "failed to queue the fan watchdog, "
6439 "watchdog will not trigger\n");
6442 fan_watchdog_active
= 0;
6445 static void fan_watchdog_fire(struct work_struct
*ignored
)
6449 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
6452 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
6453 rc
= fan_set_enable();
6455 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
6456 "will try again later...\n", -rc
);
6457 /* reschedule for later */
6458 fan_watchdog_reset();
6463 * SYSFS fan layout: hwmon compatible (device)
6466 * 0: "disengaged" mode
6468 * 2: native EC "auto" mode (recommended, hardware default)
6470 * pwm*: set speed in manual mode, ignored otherwise.
6471 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6474 * fan*_input: tachometer reading, RPM
6477 * SYSFS fan layout: extensions
6479 * fan_watchdog (driver):
6480 * fan watchdog interval in seconds, 0 disables (default), max 120
6483 /* sysfs fan pwm1_enable ----------------------------------------------- */
6484 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
6485 struct device_attribute
*attr
,
6491 res
= fan_get_status_safe(&status
);
6495 if (status
& TP_EC_FAN_FULLSPEED
) {
6497 } else if (status
& TP_EC_FAN_AUTO
) {
6502 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
6505 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
6506 struct device_attribute
*attr
,
6507 const char *buf
, size_t count
)
6512 if (parse_strtoul(buf
, 2, &t
))
6517 level
= TP_EC_FAN_FULLSPEED
;
6520 level
= TPACPI_FAN_LAST_LEVEL
;
6523 level
= TP_EC_FAN_AUTO
;
6526 /* reserved for software-controlled auto mode */
6532 res
= fan_set_level_safe(level
);
6538 fan_watchdog_reset();
6543 static struct device_attribute dev_attr_fan_pwm1_enable
=
6544 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
6545 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
6547 /* sysfs fan pwm1 ------------------------------------------------------ */
6548 static ssize_t
fan_pwm1_show(struct device
*dev
,
6549 struct device_attribute
*attr
,
6555 res
= fan_get_status_safe(&status
);
6560 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
6561 status
= fan_control_desired_level
;
6566 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
6569 static ssize_t
fan_pwm1_store(struct device
*dev
,
6570 struct device_attribute
*attr
,
6571 const char *buf
, size_t count
)
6575 u8 status
, newlevel
;
6577 if (parse_strtoul(buf
, 255, &s
))
6580 /* scale down from 0-255 to 0-7 */
6581 newlevel
= (s
>> 5) & 0x07;
6583 if (mutex_lock_killable(&fan_mutex
))
6584 return -ERESTARTSYS
;
6586 rc
= fan_get_status(&status
);
6587 if (!rc
&& (status
&
6588 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6589 rc
= fan_set_level(newlevel
);
6593 fan_update_desired_level(newlevel
);
6594 fan_watchdog_reset();
6598 mutex_unlock(&fan_mutex
);
6599 return (rc
)? rc
: count
;
6602 static struct device_attribute dev_attr_fan_pwm1
=
6603 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
6604 fan_pwm1_show
, fan_pwm1_store
);
6606 /* sysfs fan fan1_input ------------------------------------------------ */
6607 static ssize_t
fan_fan1_input_show(struct device
*dev
,
6608 struct device_attribute
*attr
,
6614 res
= fan_get_speed(&speed
);
6618 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
6621 static struct device_attribute dev_attr_fan_fan1_input
=
6622 __ATTR(fan1_input
, S_IRUGO
,
6623 fan_fan1_input_show
, NULL
);
6625 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6626 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
6629 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
6632 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
6633 const char *buf
, size_t count
)
6637 if (parse_strtoul(buf
, 120, &t
))
6640 if (!fan_control_allowed
)
6643 fan_watchdog_maxinterval
= t
;
6644 fan_watchdog_reset();
6649 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
6650 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
6652 /* --------------------------------------------------------------------- */
6653 static struct attribute
*fan_attributes
[] = {
6654 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
6655 &dev_attr_fan_fan1_input
.attr
,
6659 static const struct attribute_group fan_attr_group
= {
6660 .attrs
= fan_attributes
,
6663 static int __init
fan_init(struct ibm_init_struct
*iibm
)
6667 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
6669 mutex_init(&fan_mutex
);
6670 fan_status_access_mode
= TPACPI_FAN_NONE
;
6671 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
6672 fan_control_commands
= 0;
6673 fan_watchdog_maxinterval
= 0;
6674 tp_features
.fan_ctrl_status_undef
= 0;
6675 fan_control_desired_level
= 7;
6677 TPACPI_ACPIHANDLE_INIT(fans
);
6678 TPACPI_ACPIHANDLE_INIT(gfan
);
6679 TPACPI_ACPIHANDLE_INIT(sfan
);
6682 /* 570, 600e/x, 770e, 770x */
6683 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
6685 /* all other ThinkPads: note that even old-style
6686 * ThinkPad ECs supports the fan control register */
6687 if (likely(acpi_ec_read(fan_status_offset
,
6688 &fan_control_initial_status
))) {
6689 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
6690 fan_quirk1_detect();
6693 "ThinkPad ACPI EC access misbehaving, "
6694 "fan status and control unavailable\n");
6701 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
6702 fan_control_commands
|=
6703 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
6706 /* gfan without sfan means no fan control */
6707 /* all other models implement TP EC 0x2f control */
6711 fan_control_access_mode
=
6712 TPACPI_FAN_WR_ACPI_FANS
;
6713 fan_control_commands
|=
6714 TPACPI_FAN_CMD_SPEED
|
6715 TPACPI_FAN_CMD_LEVEL
|
6716 TPACPI_FAN_CMD_ENABLE
;
6718 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
6719 fan_control_commands
|=
6720 TPACPI_FAN_CMD_LEVEL
|
6721 TPACPI_FAN_CMD_ENABLE
;
6726 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
6727 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
6728 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
6729 fan_status_access_mode
, fan_control_access_mode
);
6731 /* fan control master switch */
6732 if (!fan_control_allowed
) {
6733 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
6734 fan_control_commands
= 0;
6735 dbg_printk(TPACPI_DBG_INIT
,
6736 "fan control features disabled by parameter\n");
6739 /* update fan_control_desired_level */
6740 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
6741 fan_get_status_safe(NULL
);
6743 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
6744 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
6745 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6750 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
6751 &driver_attr_fan_watchdog
);
6753 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6762 static void fan_exit(void)
6764 vdbg_printk(TPACPI_DBG_EXIT
,
6765 "cancelling any pending fan watchdog tasks\n");
6767 /* FIXME: can we really do this unconditionally? */
6768 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
6769 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
6770 &driver_attr_fan_watchdog
);
6772 cancel_delayed_work(&fan_watchdog_task
);
6773 flush_workqueue(tpacpi_wq
);
6776 static void fan_suspend(pm_message_t state
)
6780 if (!fan_control_allowed
)
6783 /* Store fan status in cache */
6784 fan_control_resume_level
= 0;
6785 rc
= fan_get_status_safe(&fan_control_resume_level
);
6787 printk(TPACPI_NOTICE
6788 "failed to read fan level for later "
6789 "restore during resume: %d\n", rc
);
6791 /* if it is undefined, don't attempt to restore it.
6793 if (tp_features
.fan_ctrl_status_undef
)
6794 fan_control_resume_level
= 0;
6797 static void fan_resume(void)
6799 u8 current_level
= 7;
6800 bool do_set
= false;
6803 /* DSDT *always* updates status on resume */
6804 tp_features
.fan_ctrl_status_undef
= 0;
6806 if (!fan_control_allowed
||
6807 !fan_control_resume_level
||
6808 (fan_get_status_safe(¤t_level
) < 0))
6811 switch (fan_control_access_mode
) {
6812 case TPACPI_FAN_WR_ACPI_SFAN
:
6813 /* never decrease fan level */
6814 do_set
= (fan_control_resume_level
> current_level
);
6816 case TPACPI_FAN_WR_ACPI_FANS
:
6817 case TPACPI_FAN_WR_TPEC
:
6818 /* never decrease fan level, scale is:
6819 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6821 * We expect the firmware to set either 7 or AUTO, but we
6822 * handle FULLSPEED out of paranoia.
6824 * So, we can safely only restore FULLSPEED or 7, anything
6825 * else could slow the fan. Restoring AUTO is useless, at
6826 * best that's exactly what the DSDT already set (it is the
6829 * Always keep in mind that the DSDT *will* have set the
6830 * fans to what the vendor supposes is the best level. We
6831 * muck with it only to speed the fan up.
6833 if (fan_control_resume_level
!= 7 &&
6834 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
6837 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
6838 (current_level
!= fan_control_resume_level
);
6844 printk(TPACPI_NOTICE
6845 "restoring fan level to 0x%02x\n",
6846 fan_control_resume_level
);
6847 rc
= fan_set_level_safe(fan_control_resume_level
);
6849 printk(TPACPI_NOTICE
6850 "failed to restore fan level: %d\n", rc
);
6854 static int fan_read(char *p
)
6859 unsigned int speed
= 0;
6861 switch (fan_status_access_mode
) {
6862 case TPACPI_FAN_RD_ACPI_GFAN
:
6863 /* 570, 600e/x, 770e, 770x */
6864 rc
= fan_get_status_safe(&status
);
6868 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
6870 (status
!= 0) ? "enabled" : "disabled", status
);
6873 case TPACPI_FAN_RD_TPEC
:
6874 /* all except 570, 600e/x, 770e, 770x */
6875 rc
= fan_get_status_safe(&status
);
6879 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
6880 (status
!= 0) ? "enabled" : "disabled");
6882 rc
= fan_get_speed(&speed
);
6886 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
6888 if (status
& TP_EC_FAN_FULLSPEED
)
6889 /* Disengaged mode takes precedence */
6890 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
6891 else if (status
& TP_EC_FAN_AUTO
)
6892 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
6894 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
6897 case TPACPI_FAN_NONE
:
6899 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
6902 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
6903 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
6905 switch (fan_control_access_mode
) {
6906 case TPACPI_FAN_WR_ACPI_SFAN
:
6907 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
6911 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
6912 "auto, disengaged, full-speed)\n");
6917 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
6918 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
6919 "commands:\twatchdog <timeout> (<timeout> "
6920 "is 0 (off), 1-120 (seconds))\n");
6922 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
6923 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
6924 " (<speed> is 0-65535)\n");
6929 static int fan_write_cmd_level(const char *cmd
, int *rc
)
6933 if (strlencmp(cmd
, "level auto") == 0)
6934 level
= TP_EC_FAN_AUTO
;
6935 else if ((strlencmp(cmd
, "level disengaged") == 0) |
6936 (strlencmp(cmd
, "level full-speed") == 0))
6937 level
= TP_EC_FAN_FULLSPEED
;
6938 else if (sscanf(cmd
, "level %d", &level
) != 1)
6941 *rc
= fan_set_level_safe(level
);
6943 printk(TPACPI_ERR
"level command accepted for unsupported "
6944 "access mode %d", fan_control_access_mode
);
6949 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
6951 if (strlencmp(cmd
, "enable") != 0)
6954 *rc
= fan_set_enable();
6956 printk(TPACPI_ERR
"enable command accepted for unsupported "
6957 "access mode %d", fan_control_access_mode
);
6962 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
6964 if (strlencmp(cmd
, "disable") != 0)
6967 *rc
= fan_set_disable();
6969 printk(TPACPI_ERR
"disable command accepted for unsupported "
6970 "access mode %d", fan_control_access_mode
);
6975 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
6980 * Support speed <low> <medium> <high> ? */
6982 if (sscanf(cmd
, "speed %d", &speed
) != 1)
6985 *rc
= fan_set_speed(speed
);
6987 printk(TPACPI_ERR
"speed command accepted for unsupported "
6988 "access mode %d", fan_control_access_mode
);
6993 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
6997 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
7000 if (interval
< 0 || interval
> 120)
7003 fan_watchdog_maxinterval
= interval
;
7008 static int fan_write(char *buf
)
7013 while (!rc
&& (cmd
= next_cmd(&buf
))) {
7014 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
7015 fan_write_cmd_level(cmd
, &rc
)) &&
7016 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
7017 (fan_write_cmd_enable(cmd
, &rc
) ||
7018 fan_write_cmd_disable(cmd
, &rc
) ||
7019 fan_write_cmd_watchdog(cmd
, &rc
))) &&
7020 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
7021 fan_write_cmd_speed(cmd
, &rc
))
7025 fan_watchdog_reset();
7031 static struct ibm_struct fan_driver_data
= {
7036 .suspend
= fan_suspend
,
7037 .resume
= fan_resume
,
7040 /****************************************************************************
7041 ****************************************************************************
7045 ****************************************************************************
7046 ****************************************************************************/
7048 /* sysfs name ---------------------------------------------------------- */
7049 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
7050 struct device_attribute
*attr
,
7053 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
7056 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
7057 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
7059 /* --------------------------------------------------------------------- */
7062 static struct proc_dir_entry
*proc_dir
;
7065 * Module and infrastructure proble, init and exit handling
7068 static int force_load
;
7070 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7071 static const char * __init
str_supported(int is_supported
)
7073 static char text_unsupported
[] __initdata
= "not supported";
7075 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
7077 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7079 static void ibm_exit(struct ibm_struct
*ibm
)
7081 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
7083 list_del_init(&ibm
->all_drivers
);
7085 if (ibm
->flags
.acpi_notify_installed
) {
7086 dbg_printk(TPACPI_DBG_EXIT
,
7087 "%s: acpi_remove_notify_handler\n", ibm
->name
);
7089 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
7091 dispatch_acpi_notify
);
7092 ibm
->flags
.acpi_notify_installed
= 0;
7093 ibm
->flags
.acpi_notify_installed
= 0;
7096 if (ibm
->flags
.proc_created
) {
7097 dbg_printk(TPACPI_DBG_EXIT
,
7098 "%s: remove_proc_entry\n", ibm
->name
);
7099 remove_proc_entry(ibm
->name
, proc_dir
);
7100 ibm
->flags
.proc_created
= 0;
7103 if (ibm
->flags
.acpi_driver_registered
) {
7104 dbg_printk(TPACPI_DBG_EXIT
,
7105 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
7107 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
7108 kfree(ibm
->acpi
->driver
);
7109 ibm
->acpi
->driver
= NULL
;
7110 ibm
->flags
.acpi_driver_registered
= 0;
7113 if (ibm
->flags
.init_called
&& ibm
->exit
) {
7115 ibm
->flags
.init_called
= 0;
7118 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
7121 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
7124 struct ibm_struct
*ibm
= iibm
->data
;
7125 struct proc_dir_entry
*entry
;
7127 BUG_ON(ibm
== NULL
);
7129 INIT_LIST_HEAD(&ibm
->all_drivers
);
7131 if (ibm
->flags
.experimental
&& !experimental
)
7134 dbg_printk(TPACPI_DBG_INIT
,
7135 "probing for %s\n", ibm
->name
);
7138 ret
= iibm
->init(iibm
);
7140 return 0; /* probe failed */
7144 ibm
->flags
.init_called
= 1;
7148 if (ibm
->acpi
->hid
) {
7149 ret
= register_tpacpi_subdriver(ibm
);
7154 if (ibm
->acpi
->notify
) {
7155 ret
= setup_acpi_notify(ibm
);
7156 if (ret
== -ENODEV
) {
7157 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
7167 dbg_printk(TPACPI_DBG_INIT
,
7168 "%s installed\n", ibm
->name
);
7171 entry
= create_proc_entry(ibm
->name
,
7172 S_IFREG
| S_IRUGO
| S_IWUSR
,
7175 printk(TPACPI_ERR
"unable to create proc entry %s\n",
7180 entry
->owner
= THIS_MODULE
;
7182 entry
->read_proc
= &dispatch_procfs_read
;
7184 entry
->write_proc
= &dispatch_procfs_write
;
7185 ibm
->flags
.proc_created
= 1;
7188 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
7193 dbg_printk(TPACPI_DBG_INIT
,
7194 "%s: at error exit path with result %d\n",
7198 return (ret
< 0)? ret
: 0;
7203 /* returns 0 - probe ok, or < 0 - probe error.
7204 * Probe ok doesn't mean thinkpad found.
7205 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7206 static int __must_check __init
get_thinkpad_model_data(
7207 struct thinkpad_id_data
*tp
)
7209 const struct dmi_device
*dev
= NULL
;
7210 char ec_fw_string
[18];
7216 memset(tp
, 0, sizeof(*tp
));
7218 if (dmi_name_in_vendors("IBM"))
7219 tp
->vendor
= PCI_VENDOR_ID_IBM
;
7220 else if (dmi_name_in_vendors("LENOVO"))
7221 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
7225 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
7226 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
7227 if (s
&& !tp
->bios_version_str
)
7229 if (!tp
->bios_version_str
)
7231 tp
->bios_model
= tp
->bios_version_str
[0]
7232 | (tp
->bios_version_str
[1] << 8);
7235 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7236 * X32 or newer, all Z series; Some models must have an
7237 * up-to-date BIOS or they will not be detected.
7239 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7241 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
7242 if (sscanf(dev
->name
,
7243 "IBM ThinkPad Embedded Controller -[%17c",
7244 ec_fw_string
) == 1) {
7245 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
7246 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
7248 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
7249 if (!tp
->ec_version_str
)
7251 tp
->ec_model
= ec_fw_string
[0]
7252 | (ec_fw_string
[1] << 8);
7257 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
7258 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
7259 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
7264 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
7265 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
7266 if (s
&& !tp
->nummodel_str
)
7272 static int __init
probe_for_thinkpad(void)
7280 * Non-ancient models have better DMI tagging, but very old models
7283 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
7285 /* ec is required because many other handles are relative to it */
7286 TPACPI_ACPIHANDLE_INIT(ec
);
7290 "Not yet supported ThinkPad detected!\n");
7295 * Risks a regression on very old machines, but reduces potential
7296 * false positives a damn great deal
7299 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
7301 if (!is_thinkpad
&& !force_load
)
7308 /* Module init, exit, parameters */
7310 static struct ibm_init_struct ibms_init
[] __initdata
= {
7312 .init
= thinkpad_acpi_driver_init
,
7313 .data
= &thinkpad_acpi_driver_data
,
7316 .init
= hotkey_init
,
7317 .data
= &hotkey_driver_data
,
7320 .init
= bluetooth_init
,
7321 .data
= &bluetooth_driver_data
,
7325 .data
= &wan_driver_data
,
7329 .data
= &uwb_driver_data
,
7331 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7334 .data
= &video_driver_data
,
7339 .data
= &light_driver_data
,
7341 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7344 .data
= &dock_driver_data
[0],
7348 .data
= &dock_driver_data
[1],
7351 #ifdef CONFIG_THINKPAD_ACPI_BAY
7354 .data
= &bay_driver_data
,
7359 .data
= &cmos_driver_data
,
7363 .data
= &led_driver_data
,
7367 .data
= &beep_driver_data
,
7370 .init
= thermal_init
,
7371 .data
= &thermal_driver_data
,
7374 .data
= &ecdump_driver_data
,
7377 .init
= brightness_init
,
7378 .data
= &brightness_driver_data
,
7381 .data
= &volume_driver_data
,
7385 .data
= &fan_driver_data
,
7389 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
7392 struct ibm_struct
*ibm
;
7394 if (!kp
|| !kp
->name
|| !val
)
7397 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7398 ibm
= ibms_init
[i
].data
;
7399 WARN_ON(ibm
== NULL
);
7401 if (!ibm
|| !ibm
->name
)
7404 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
7405 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
7407 strcpy(ibms_init
[i
].param
, val
);
7408 strcat(ibms_init
[i
].param
, ",");
7416 module_param(experimental
, int, 0);
7417 MODULE_PARM_DESC(experimental
,
7418 "Enables experimental features when non-zero");
7420 module_param_named(debug
, dbg_level
, uint
, 0);
7421 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
7423 module_param(force_load
, bool, 0);
7424 MODULE_PARM_DESC(force_load
,
7425 "Attempts to load the driver even on a "
7426 "mis-identified ThinkPad when true");
7428 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
7429 MODULE_PARM_DESC(fan_control
,
7430 "Enables setting fan parameters features when true");
7432 module_param_named(brightness_mode
, brightness_mode
, int, 0);
7433 MODULE_PARM_DESC(brightness_mode
,
7434 "Selects brightness control strategy: "
7435 "0=auto, 1=EC, 2=CMOS, 3=both");
7437 module_param(brightness_enable
, uint
, 0);
7438 MODULE_PARM_DESC(brightness_enable
,
7439 "Enables backlight control when 1, disables when 0");
7441 module_param(hotkey_report_mode
, uint
, 0);
7442 MODULE_PARM_DESC(hotkey_report_mode
,
7443 "used for backwards compatibility with userspace, "
7444 "see documentation");
7446 #define TPACPI_PARAM(feature) \
7447 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7448 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7449 "at module load, see documentation")
7451 TPACPI_PARAM(hotkey
);
7452 TPACPI_PARAM(bluetooth
);
7453 TPACPI_PARAM(video
);
7454 TPACPI_PARAM(light
);
7455 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7458 #ifdef CONFIG_THINKPAD_ACPI_BAY
7460 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7464 TPACPI_PARAM(ecdump
);
7465 TPACPI_PARAM(brightness
);
7466 TPACPI_PARAM(volume
);
7469 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7470 module_param(dbg_wlswemul
, uint
, 0);
7471 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
7472 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
7473 MODULE_PARM_DESC(wlsw_state
,
7474 "Initial state of the emulated WLSW switch");
7476 module_param(dbg_bluetoothemul
, uint
, 0);
7477 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
7478 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
7479 MODULE_PARM_DESC(bluetooth_state
,
7480 "Initial state of the emulated bluetooth switch");
7482 module_param(dbg_wwanemul
, uint
, 0);
7483 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
7484 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
7485 MODULE_PARM_DESC(wwan_state
,
7486 "Initial state of the emulated WWAN switch");
7488 module_param(dbg_uwbemul
, uint
, 0);
7489 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
7490 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
7491 MODULE_PARM_DESC(uwb_state
,
7492 "Initial state of the emulated UWB switch");
7495 static void thinkpad_acpi_module_exit(void)
7497 struct ibm_struct
*ibm
, *itmp
;
7499 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
7501 list_for_each_entry_safe_reverse(ibm
, itmp
,
7502 &tpacpi_all_drivers
,
7507 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
7509 if (tpacpi_inputdev
) {
7510 if (tp_features
.input_device_registered
)
7511 input_unregister_device(tpacpi_inputdev
);
7513 input_free_device(tpacpi_inputdev
);
7517 hwmon_device_unregister(tpacpi_hwmon
);
7519 if (tp_features
.sensors_pdev_attrs_registered
)
7520 device_remove_file(&tpacpi_sensors_pdev
->dev
,
7521 &dev_attr_thinkpad_acpi_pdev_name
);
7522 if (tpacpi_sensors_pdev
)
7523 platform_device_unregister(tpacpi_sensors_pdev
);
7525 platform_device_unregister(tpacpi_pdev
);
7527 if (tp_features
.sensors_pdrv_attrs_registered
)
7528 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
7529 if (tp_features
.platform_drv_attrs_registered
)
7530 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
7532 if (tp_features
.sensors_pdrv_registered
)
7533 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
7535 if (tp_features
.platform_drv_registered
)
7536 platform_driver_unregister(&tpacpi_pdriver
);
7539 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
7542 destroy_workqueue(tpacpi_wq
);
7544 kfree(thinkpad_id
.bios_version_str
);
7545 kfree(thinkpad_id
.ec_version_str
);
7546 kfree(thinkpad_id
.model_str
);
7550 static int __init
thinkpad_acpi_module_init(void)
7554 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
7556 /* Parameter checking */
7557 if (hotkey_report_mode
> 2)
7560 /* Driver-level probe */
7562 ret
= get_thinkpad_model_data(&thinkpad_id
);
7565 "unable to get DMI data: %d\n", ret
);
7566 thinkpad_acpi_module_exit();
7569 ret
= probe_for_thinkpad();
7571 thinkpad_acpi_module_exit();
7575 /* Driver initialization */
7577 TPACPI_ACPIHANDLE_INIT(ecrd
);
7578 TPACPI_ACPIHANDLE_INIT(ecwr
);
7580 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
7582 thinkpad_acpi_module_exit();
7586 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
7589 "unable to create proc dir " TPACPI_PROC_DIR
);
7590 thinkpad_acpi_module_exit();
7593 proc_dir
->owner
= THIS_MODULE
;
7595 ret
= platform_driver_register(&tpacpi_pdriver
);
7598 "unable to register main platform driver\n");
7599 thinkpad_acpi_module_exit();
7602 tp_features
.platform_drv_registered
= 1;
7604 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
7607 "unable to register hwmon platform driver\n");
7608 thinkpad_acpi_module_exit();
7611 tp_features
.sensors_pdrv_registered
= 1;
7613 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
7615 tp_features
.platform_drv_attrs_registered
= 1;
7616 ret
= tpacpi_create_driver_attributes(
7617 &tpacpi_hwmon_pdriver
.driver
);
7621 "unable to create sysfs driver attributes\n");
7622 thinkpad_acpi_module_exit();
7625 tp_features
.sensors_pdrv_attrs_registered
= 1;
7628 /* Device initialization */
7629 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
7631 if (IS_ERR(tpacpi_pdev
)) {
7632 ret
= PTR_ERR(tpacpi_pdev
);
7634 printk(TPACPI_ERR
"unable to register platform device\n");
7635 thinkpad_acpi_module_exit();
7638 tpacpi_sensors_pdev
= platform_device_register_simple(
7639 TPACPI_HWMON_DRVR_NAME
,
7641 if (IS_ERR(tpacpi_sensors_pdev
)) {
7642 ret
= PTR_ERR(tpacpi_sensors_pdev
);
7643 tpacpi_sensors_pdev
= NULL
;
7645 "unable to register hwmon platform device\n");
7646 thinkpad_acpi_module_exit();
7649 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
7650 &dev_attr_thinkpad_acpi_pdev_name
);
7653 "unable to create sysfs hwmon device attributes\n");
7654 thinkpad_acpi_module_exit();
7657 tp_features
.sensors_pdev_attrs_registered
= 1;
7658 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
7659 if (IS_ERR(tpacpi_hwmon
)) {
7660 ret
= PTR_ERR(tpacpi_hwmon
);
7661 tpacpi_hwmon
= NULL
;
7662 printk(TPACPI_ERR
"unable to register hwmon device\n");
7663 thinkpad_acpi_module_exit();
7666 mutex_init(&tpacpi_inputdev_send_mutex
);
7667 tpacpi_inputdev
= input_allocate_device();
7668 if (!tpacpi_inputdev
) {
7669 printk(TPACPI_ERR
"unable to allocate input device\n");
7670 thinkpad_acpi_module_exit();
7673 /* Prepare input device, but don't register */
7674 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
7675 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
7676 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
7677 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
7678 thinkpad_id
.vendor
:
7680 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
7681 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
7683 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7684 ret
= ibm_init(&ibms_init
[i
]);
7685 if (ret
>= 0 && *ibms_init
[i
].param
)
7686 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
7688 thinkpad_acpi_module_exit();
7692 ret
= input_register_device(tpacpi_inputdev
);
7694 printk(TPACPI_ERR
"unable to register input device\n");
7695 thinkpad_acpi_module_exit();
7698 tp_features
.input_device_registered
= 1;
7701 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
7705 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
7708 * DMI matching for module autoloading
7710 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7711 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7713 * Only models listed in thinkwiki will be supported, so add yours
7714 * if it is not there yet.
7716 #define IBM_BIOS_MODULE_ALIAS(__type) \
7717 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7719 /* Non-ancient thinkpads */
7720 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7721 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7723 /* Ancient thinkpad BIOSes have to be identified by
7724 * BIOS type or model number, and there are far less
7725 * BIOS types than model numbers... */
7726 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7727 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7728 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7730 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7731 MODULE_DESCRIPTION(TPACPI_DESC
);
7732 MODULE_VERSION(TPACPI_VERSION
);
7733 MODULE_LICENSE("GPL");
7735 module_init(thinkpad_acpi_module_init
);
7736 module_exit(thinkpad_acpi_module_exit
);