2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 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.19-20080213"
25 #define TPACPI_SYSFS_VERSION 0x020200
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/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
91 TP_NVRAM_ADDR_HK2
= 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT
= 0x58,
93 TP_NVRAM_ADDR_VIDEO
= 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS
= 0x5e,
95 TP_NVRAM_ADDR_MIXER
= 0x60,
100 TP_NVRAM_MASK_HKT_THINKPAD
= 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM
= 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY
= 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE
= 0x80,
104 TP_NVRAM_MASK_THINKLIGHT
= 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND
= 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS
= 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS
= 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS
= 0,
109 TP_NVRAM_MASK_MUTE
= 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME
= 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME
= 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME
= 0,
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported
);
160 #define vdbg_printk(a_dbg_level, format, arg...)
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
174 struct tp_acpi_drv_struct
{
176 struct acpi_driver
*driver
;
178 void (*notify
) (struct ibm_struct
*, u32
);
181 struct acpi_device
*device
;
187 int (*read
) (char *);
188 int (*write
) (char *);
190 void (*resume
) (void);
191 void (*suspend
) (pm_message_t state
);
193 struct list_head all_drivers
;
195 struct tp_acpi_drv_struct
*acpi
;
198 u8 acpi_driver_registered
:1;
199 u8 acpi_notify_installed
:1;
206 struct ibm_init_struct
{
209 int (*init
) (struct ibm_init_struct
*);
210 struct ibm_struct
*data
;
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
227 u32 bright_16levels
:1;
228 u32 bright_acpimode
:1;
230 u32 fan_ctrl_status_undef
:1;
231 u32 input_device_registered
:1;
232 u32 hotkey_report_mode_locked
:1;
233 u32 platform_drv_registered
:1;
234 u32 platform_drv_attrs_registered
:1;
235 u32 sensors_pdrv_registered
:1;
236 u32 sensors_pdrv_attrs_registered
:1;
237 u32 sensors_pdev_attrs_registered
:1;
238 u32 hotkey_poll_active
:1;
241 struct thinkpad_id_data
{
242 unsigned int vendor
; /* ThinkPad vendor:
243 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
245 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
246 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
248 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
253 static struct thinkpad_id_data thinkpad_id
;
256 TPACPI_LIFE_INIT
= 0,
261 static int experimental
;
262 static u32 dbg_level
;
264 /****************************************************************************
265 ****************************************************************************
267 * ACPI Helpers and device model
269 ****************************************************************************
270 ****************************************************************************/
272 /*************************************************************************
276 static acpi_handle root_handle
;
278 #define TPACPI_HANDLE(object, parent, paths...) \
279 static acpi_handle object##_handle; \
280 static acpi_handle *object##_parent = &parent##_handle; \
281 static char *object##_path; \
282 static char *object##_paths[] = { paths }
284 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
285 "\\_SB.PCI.ISA.EC", /* 570 */
286 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
287 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
288 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
289 "\\_SB.PCI0.ICH3.EC0", /* R31 */
290 "\\_SB.PCI0.LPC.EC", /* all others */
293 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
294 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
296 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
298 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
299 "\\CMS", /* R40, R40e */
302 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
303 "^HKEY", /* R30, R31 */
304 "HKEY", /* all others */
307 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
308 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
309 "\\_SB.PCI0.VID0", /* 770e */
310 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
311 "\\_SB.PCI0.AGP.VID", /* all others */
315 /*************************************************************************
319 static int acpi_evalf(acpi_handle handle
,
320 void *res
, char *method
, char *fmt
, ...)
323 struct acpi_object_list params
;
324 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
325 struct acpi_buffer result
, *resultp
;
326 union acpi_object out_obj
;
334 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
347 params
.pointer
= &in_objs
[0];
354 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
355 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
357 /* add more types as needed */
359 printk(TPACPI_ERR
"acpi_evalf() called "
360 "with invalid format character '%c'\n", c
);
366 if (res_type
!= 'v') {
367 result
.length
= sizeof(out_obj
);
368 result
.pointer
= &out_obj
;
373 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
378 *(int *)res
= out_obj
.integer
.value
;
379 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
382 success
= status
== AE_OK
;
384 /* add more types as needed */
386 printk(TPACPI_ERR
"acpi_evalf() called "
387 "with invalid format character '%c'\n", res_type
);
391 if (!success
&& !quiet
)
392 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
393 method
, fmt0
, status
);
398 static int acpi_ec_read(int i
, u8
*p
)
403 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
407 if (ec_read(i
, p
) < 0)
414 static int acpi_ec_write(int i
, u8 v
)
417 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
420 if (ec_write(i
, v
) < 0)
427 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
428 static int _sta(acpi_handle handle
)
432 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
439 static int issue_thinkpad_cmos_command(int cmos_cmd
)
444 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
450 /*************************************************************************
454 #define TPACPI_ACPIHANDLE_INIT(object) \
455 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
456 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
458 static void drv_acpi_handle_init(char *name
,
459 acpi_handle
*handle
, acpi_handle parent
,
460 char **paths
, int num_paths
, char **path
)
465 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
468 for (i
= 0; i
< num_paths
; i
++) {
469 status
= acpi_get_handle(parent
, paths
[i
], handle
);
470 if (ACPI_SUCCESS(status
)) {
472 dbg_printk(TPACPI_DBG_INIT
,
473 "Found ACPI handle %s for %s\n",
479 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
484 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
486 struct ibm_struct
*ibm
= data
;
488 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
491 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
494 ibm
->acpi
->notify(ibm
, event
);
497 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
504 if (!*ibm
->acpi
->handle
)
507 vdbg_printk(TPACPI_DBG_INIT
,
508 "setting up ACPI notify for %s\n", ibm
->name
);
510 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
512 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
517 acpi_driver_data(ibm
->acpi
->device
) = ibm
;
518 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
519 TPACPI_ACPI_EVENT_PREFIX
,
522 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
523 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
524 if (ACPI_FAILURE(status
)) {
525 if (status
== AE_ALREADY_EXISTS
) {
527 "another device driver is already "
528 "handling %s events\n", ibm
->name
);
531 "acpi_install_notify_handler(%s) failed: %d\n",
536 ibm
->flags
.acpi_notify_installed
= 1;
540 static int __init
tpacpi_device_add(struct acpi_device
*device
)
545 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
549 dbg_printk(TPACPI_DBG_INIT
,
550 "registering %s as an ACPI driver\n", ibm
->name
);
554 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
555 if (!ibm
->acpi
->driver
) {
556 printk(TPACPI_ERR
"kzalloc(ibm->driver) failed\n");
560 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
561 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
562 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
564 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
566 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
568 kfree(ibm
->acpi
->driver
);
569 ibm
->acpi
->driver
= NULL
;
571 ibm
->flags
.acpi_driver_registered
= 1;
577 /****************************************************************************
578 ****************************************************************************
582 ****************************************************************************
583 ****************************************************************************/
585 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
586 int count
, int *eof
, void *data
)
588 struct ibm_struct
*ibm
= data
;
591 if (!ibm
|| !ibm
->read
)
594 len
= ibm
->read(page
);
598 if (len
<= off
+ count
)
610 static int dispatch_procfs_write(struct file
*file
,
611 const char __user
*userbuf
,
612 unsigned long count
, void *data
)
614 struct ibm_struct
*ibm
= data
;
618 if (!ibm
|| !ibm
->write
)
621 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
625 if (copy_from_user(kernbuf
, userbuf
, count
)) {
631 strcat(kernbuf
, ",");
632 ret
= ibm
->write(kernbuf
);
641 static char *next_cmd(char **cmds
)
646 while ((end
= strchr(start
, ',')) && end
== start
)
658 /****************************************************************************
659 ****************************************************************************
661 * Device model: input, hwmon and platform
663 ****************************************************************************
664 ****************************************************************************/
666 static struct platform_device
*tpacpi_pdev
;
667 static struct platform_device
*tpacpi_sensors_pdev
;
668 static struct class_device
*tpacpi_hwmon
;
669 static struct input_dev
*tpacpi_inputdev
;
670 static struct mutex tpacpi_inputdev_send_mutex
;
671 static LIST_HEAD(tpacpi_all_drivers
);
673 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
676 struct ibm_struct
*ibm
, *itmp
;
678 list_for_each_entry_safe(ibm
, itmp
,
682 (ibm
->suspend
)(state
);
688 static int tpacpi_resume_handler(struct platform_device
*pdev
)
690 struct ibm_struct
*ibm
, *itmp
;
692 list_for_each_entry_safe(ibm
, itmp
,
702 static struct platform_driver tpacpi_pdriver
= {
704 .name
= TPACPI_DRVR_NAME
,
705 .owner
= THIS_MODULE
,
707 .suspend
= tpacpi_suspend_handler
,
708 .resume
= tpacpi_resume_handler
,
711 static struct platform_driver tpacpi_hwmon_pdriver
= {
713 .name
= TPACPI_HWMON_DRVR_NAME
,
714 .owner
= THIS_MODULE
,
718 /*************************************************************************
719 * sysfs support helpers
722 struct attribute_set
{
723 unsigned int members
, max_members
;
724 struct attribute_group group
;
727 struct attribute_set_obj
{
728 struct attribute_set s
;
730 } __attribute__((packed
));
732 static struct attribute_set
*create_attr_set(unsigned int max_members
,
735 struct attribute_set_obj
*sobj
;
737 if (max_members
== 0)
740 /* Allocates space for implicit NULL at the end too */
741 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
742 max_members
* sizeof(struct attribute
*),
746 sobj
->s
.max_members
= max_members
;
747 sobj
->s
.group
.attrs
= &sobj
->a
;
748 sobj
->s
.group
.name
= name
;
753 #define destroy_attr_set(_set) \
756 /* not multi-threaded safe, use it in a single thread per set */
757 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
762 if (s
->members
>= s
->max_members
)
765 s
->group
.attrs
[s
->members
] = attr
;
771 static int add_many_to_attr_set(struct attribute_set
*s
,
772 struct attribute
**attr
,
777 for (i
= 0; i
< count
; i
++) {
778 res
= add_to_attr_set(s
, attr
[i
]);
786 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
788 sysfs_remove_group(kobj
, &s
->group
);
792 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
793 sysfs_create_group(_kobj, &_attr_set->group)
795 static int parse_strtoul(const char *buf
,
796 unsigned long max
, unsigned long *value
)
800 while (*buf
&& isspace(*buf
))
802 *value
= simple_strtoul(buf
, &endp
, 0);
803 while (*endp
&& isspace(*endp
))
805 if (*endp
|| *value
> max
)
811 /*************************************************************************
812 * thinkpad-acpi driver attributes
815 /* interface_version --------------------------------------------------- */
816 static ssize_t
tpacpi_driver_interface_version_show(
817 struct device_driver
*drv
,
820 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
823 static DRIVER_ATTR(interface_version
, S_IRUGO
,
824 tpacpi_driver_interface_version_show
, NULL
);
826 /* debug_level --------------------------------------------------------- */
827 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
830 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
833 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
834 const char *buf
, size_t count
)
838 if (parse_strtoul(buf
, 0xffff, &t
))
846 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
847 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
849 /* version ------------------------------------------------------------- */
850 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
853 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
854 TPACPI_DESC
, TPACPI_VERSION
);
857 static DRIVER_ATTR(version
, S_IRUGO
,
858 tpacpi_driver_version_show
, NULL
);
860 /* --------------------------------------------------------------------- */
862 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
863 &driver_attr_debug_level
, &driver_attr_version
,
864 &driver_attr_interface_version
,
867 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
873 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
874 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
881 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
885 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
886 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
889 /****************************************************************************
890 ****************************************************************************
894 ****************************************************************************
895 ****************************************************************************/
897 /*************************************************************************
898 * thinkpad-acpi init subdriver
901 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
903 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
904 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
906 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
907 (thinkpad_id
.bios_version_str
) ?
908 thinkpad_id
.bios_version_str
: "unknown",
909 (thinkpad_id
.ec_version_str
) ?
910 thinkpad_id
.ec_version_str
: "unknown");
912 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
913 printk(TPACPI_INFO
"%s %s\n",
914 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
915 "IBM" : ((thinkpad_id
.vendor
==
916 PCI_VENDOR_ID_LENOVO
) ?
917 "Lenovo" : "Unknown vendor"),
918 thinkpad_id
.model_str
);
923 static int thinkpad_acpi_driver_read(char *p
)
927 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
928 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
933 static struct ibm_struct thinkpad_acpi_driver_data
= {
935 .read
= thinkpad_acpi_driver_read
,
938 /*************************************************************************
942 enum { /* hot key scan codes (derived from ACPI DSDT) */
943 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
944 TP_ACPI_HOTKEYSCAN_FNF2
,
945 TP_ACPI_HOTKEYSCAN_FNF3
,
946 TP_ACPI_HOTKEYSCAN_FNF4
,
947 TP_ACPI_HOTKEYSCAN_FNF5
,
948 TP_ACPI_HOTKEYSCAN_FNF6
,
949 TP_ACPI_HOTKEYSCAN_FNF7
,
950 TP_ACPI_HOTKEYSCAN_FNF8
,
951 TP_ACPI_HOTKEYSCAN_FNF9
,
952 TP_ACPI_HOTKEYSCAN_FNF10
,
953 TP_ACPI_HOTKEYSCAN_FNF11
,
954 TP_ACPI_HOTKEYSCAN_FNF12
,
955 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
956 TP_ACPI_HOTKEYSCAN_FNINSERT
,
957 TP_ACPI_HOTKEYSCAN_FNDELETE
,
958 TP_ACPI_HOTKEYSCAN_FNHOME
,
959 TP_ACPI_HOTKEYSCAN_FNEND
,
960 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
961 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
962 TP_ACPI_HOTKEYSCAN_FNSPACE
,
963 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
964 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
965 TP_ACPI_HOTKEYSCAN_MUTE
,
966 TP_ACPI_HOTKEYSCAN_THINKPAD
,
969 enum { /* Keys available through NVRAM polling */
970 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
971 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
974 enum { /* Positions of some of the keys in hotkey masks */
975 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
976 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
977 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
978 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
979 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
980 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
981 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
982 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
983 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
984 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
985 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
988 enum { /* NVRAM to ACPI HKEY group map */
989 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
990 TP_ACPI_HKEY_ZOOM_MASK
|
991 TP_ACPI_HKEY_DISPSWTCH_MASK
|
992 TP_ACPI_HKEY_HIBERNATE_MASK
,
993 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
994 TP_ACPI_HKEY_BRGHTDWN_MASK
,
995 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
996 TP_ACPI_HKEY_VOLDWN_MASK
|
997 TP_ACPI_HKEY_MUTE_MASK
,
1000 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1001 struct tp_nvram_state
{
1002 u16 thinkpad_toggle
:1;
1004 u16 display_toggle
:1;
1005 u16 thinklight_toggle
:1;
1006 u16 hibernate_toggle
:1;
1007 u16 displayexp_toggle
:1;
1008 u16 display_state
:1;
1009 u16 brightness_toggle
:1;
1010 u16 volume_toggle
:1;
1013 u8 brightness_level
;
1017 static struct task_struct
*tpacpi_hotkey_task
;
1018 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1019 static int hotkey_poll_freq
= 10; /* Hz */
1020 static struct mutex hotkey_thread_mutex
;
1021 static struct mutex hotkey_thread_data_mutex
;
1022 static unsigned int hotkey_config_change
;
1024 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1026 #define hotkey_source_mask 0U
1028 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1030 static struct mutex hotkey_mutex
;
1032 static enum { /* Reasons for waking up */
1033 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1034 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1035 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1036 } hotkey_wakeup_reason
;
1038 static int hotkey_autosleep_ack
;
1040 static int hotkey_orig_status
;
1041 static u32 hotkey_orig_mask
;
1042 static u32 hotkey_all_mask
;
1043 static u32 hotkey_reserved_mask
;
1044 static u32 hotkey_mask
;
1046 static unsigned int hotkey_report_mode
;
1048 static u16
*hotkey_keycode_map
;
1050 static struct attribute_set
*hotkey_dev_attributes
;
1052 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1053 #define HOTKEY_CONFIG_CRITICAL_START \
1055 mutex_lock(&hotkey_thread_data_mutex); \
1056 hotkey_config_change++; \
1058 #define HOTKEY_CONFIG_CRITICAL_END \
1059 mutex_unlock(&hotkey_thread_data_mutex);
1061 #define HOTKEY_CONFIG_CRITICAL_START
1062 #define HOTKEY_CONFIG_CRITICAL_END
1063 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1065 /* HKEY.MHKG() return bits */
1066 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1068 static int hotkey_get_wlsw(int *status
)
1070 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1075 static int hotkey_get_tablet_mode(int *status
)
1079 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1082 return ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1086 * Call with hotkey_mutex held
1088 static int hotkey_mask_get(void)
1092 if (tp_features
.hotkey_mask
) {
1093 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1096 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1102 * Call with hotkey_mutex held
1104 static int hotkey_mask_set(u32 mask
)
1109 if (tp_features
.hotkey_mask
) {
1110 HOTKEY_CONFIG_CRITICAL_START
1111 for (i
= 0; i
< 32; i
++) {
1113 /* enable in firmware mask only keys not in NVRAM
1114 * mode, but enable the key in the cached hotkey_mask
1115 * regardless of mode, or the key will end up
1116 * disabled by hotkey_mask_get() */
1117 if (!acpi_evalf(hkey_handle
,
1118 NULL
, "MHKM", "vdd", i
+ 1,
1119 !!((mask
& ~hotkey_source_mask
) & m
))) {
1123 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1126 HOTKEY_CONFIG_CRITICAL_END
1128 /* hotkey_mask_get must be called unconditionally below */
1129 if (!hotkey_mask_get() && !rc
&&
1130 (hotkey_mask
& ~hotkey_source_mask
) !=
1131 (mask
& ~hotkey_source_mask
)) {
1132 printk(TPACPI_NOTICE
1133 "requested hot key mask 0x%08x, but "
1134 "firmware forced it to 0x%08x\n",
1138 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1139 HOTKEY_CONFIG_CRITICAL_START
1140 hotkey_mask
= mask
& hotkey_source_mask
;
1141 HOTKEY_CONFIG_CRITICAL_END
1143 if (hotkey_mask
!= mask
) {
1144 printk(TPACPI_NOTICE
1145 "requested hot key mask 0x%08x, "
1146 "forced to 0x%08x (NVRAM poll mask is "
1147 "0x%08x): no firmware mask support\n",
1148 mask
, hotkey_mask
, hotkey_source_mask
);
1153 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1159 static int hotkey_status_get(int *status
)
1161 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1167 static int hotkey_status_set(int status
)
1169 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", status
))
1175 static void tpacpi_input_send_radiosw(void)
1179 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
1180 mutex_lock(&tpacpi_inputdev_send_mutex
);
1182 input_report_switch(tpacpi_inputdev
,
1184 input_sync(tpacpi_inputdev
);
1186 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1190 static void tpacpi_input_send_tabletsw(void)
1194 if (tp_features
.hotkey_tablet
&&
1195 !hotkey_get_tablet_mode(&state
)) {
1196 mutex_lock(&tpacpi_inputdev_send_mutex
);
1198 input_report_switch(tpacpi_inputdev
,
1199 SW_TABLET_MODE
, !!state
);
1200 input_sync(tpacpi_inputdev
);
1202 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1206 static void tpacpi_input_send_key(unsigned int scancode
)
1208 unsigned int keycode
;
1210 keycode
= hotkey_keycode_map
[scancode
];
1212 if (keycode
!= KEY_RESERVED
) {
1213 mutex_lock(&tpacpi_inputdev_send_mutex
);
1215 input_report_key(tpacpi_inputdev
, keycode
, 1);
1216 if (keycode
== KEY_UNKNOWN
)
1217 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1219 input_sync(tpacpi_inputdev
);
1221 input_report_key(tpacpi_inputdev
, keycode
, 0);
1222 if (keycode
== KEY_UNKNOWN
)
1223 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1225 input_sync(tpacpi_inputdev
);
1227 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1231 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1232 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1234 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1236 tpacpi_input_send_key(scancode
);
1237 if (hotkey_report_mode
< 2) {
1238 acpi_bus_generate_event(ibm_hotkey_acpidriver
.device
,
1239 0x80, 0x1001 + scancode
);
1243 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1247 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1248 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1249 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1250 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1251 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1252 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1254 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1255 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1256 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1258 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1259 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1260 n
->displayexp_toggle
=
1261 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1263 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1264 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1265 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1266 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1267 n
->brightness_toggle
=
1268 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1270 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1271 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1272 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1273 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1274 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1275 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1279 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1281 if ((mask & (1 << __scancode)) && \
1282 oldn->__member != newn->__member) \
1283 tpacpi_hotkey_send_key(__scancode); \
1286 #define TPACPI_MAY_SEND_KEY(__scancode) \
1287 do { if (mask & (1 << __scancode)) \
1288 tpacpi_hotkey_send_key(__scancode); } while (0)
1290 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1291 struct tp_nvram_state
*newn
,
1294 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1295 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1296 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1297 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1299 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1301 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1304 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1305 if (oldn
->mute
!= newn
->mute
) {
1306 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1308 if (oldn
->volume_level
> newn
->volume_level
) {
1309 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1310 } else if (oldn
->volume_level
< newn
->volume_level
) {
1311 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1312 } else if (oldn
->mute
== newn
->mute
) {
1313 /* repeated key presses that didn't change state */
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1316 } else if (newn
->volume_level
!= 0) {
1317 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1319 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1324 /* handle brightness */
1325 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1326 if (oldn
->brightness_level
< newn
->brightness_level
) {
1327 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1328 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1329 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1331 /* repeated key presses that didn't change state */
1332 if (newn
->brightness_level
!= 0) {
1333 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1335 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1341 #undef TPACPI_COMPARE_KEY
1342 #undef TPACPI_MAY_SEND_KEY
1344 static int hotkey_kthread(void *data
)
1346 struct tp_nvram_state s
[2];
1348 unsigned int si
, so
;
1350 unsigned int change_detector
, must_reset
;
1352 mutex_lock(&hotkey_thread_mutex
);
1354 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1361 /* Initial state for compares */
1362 mutex_lock(&hotkey_thread_data_mutex
);
1363 change_detector
= hotkey_config_change
;
1364 mask
= hotkey_source_mask
& hotkey_mask
;
1365 mutex_unlock(&hotkey_thread_data_mutex
);
1366 hotkey_read_nvram(&s
[so
], mask
);
1368 while (!kthread_should_stop() && hotkey_poll_freq
) {
1370 t
= 1000/hotkey_poll_freq
;
1371 t
= msleep_interruptible(t
);
1372 if (unlikely(kthread_should_stop()))
1374 must_reset
= try_to_freeze();
1375 if (t
> 0 && !must_reset
)
1378 mutex_lock(&hotkey_thread_data_mutex
);
1379 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1380 /* forget old state on thaw or config change */
1383 change_detector
= hotkey_config_change
;
1385 mask
= hotkey_source_mask
& hotkey_mask
;
1386 mutex_unlock(&hotkey_thread_data_mutex
);
1389 hotkey_read_nvram(&s
[si
], mask
);
1390 if (likely(si
!= so
)) {
1391 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1401 mutex_unlock(&hotkey_thread_mutex
);
1405 static void hotkey_poll_stop_sync(void)
1407 if (tpacpi_hotkey_task
) {
1408 if (frozen(tpacpi_hotkey_task
) ||
1409 freezing(tpacpi_hotkey_task
))
1410 thaw_process(tpacpi_hotkey_task
);
1412 kthread_stop(tpacpi_hotkey_task
);
1413 tpacpi_hotkey_task
= NULL
;
1414 mutex_lock(&hotkey_thread_mutex
);
1415 /* at this point, the thread did exit */
1416 mutex_unlock(&hotkey_thread_mutex
);
1420 /* call with hotkey_mutex held */
1421 static void hotkey_poll_setup(int may_warn
)
1423 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1424 hotkey_poll_freq
> 0 &&
1425 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1426 if (!tpacpi_hotkey_task
) {
1427 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1430 if (IS_ERR(tpacpi_hotkey_task
)) {
1431 tpacpi_hotkey_task
= NULL
;
1433 "could not create kernel thread "
1434 "for hotkey polling\n");
1438 hotkey_poll_stop_sync();
1440 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1441 printk(TPACPI_NOTICE
1442 "hot keys 0x%08x require polling, "
1443 "which is currently disabled\n",
1444 hotkey_source_mask
);
1449 static void hotkey_poll_setup_safe(int may_warn
)
1451 mutex_lock(&hotkey_mutex
);
1452 hotkey_poll_setup(may_warn
);
1453 mutex_unlock(&hotkey_mutex
);
1456 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1458 static void hotkey_poll_setup_safe(int __unused
)
1462 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1464 static int hotkey_inputdev_open(struct input_dev
*dev
)
1466 switch (tpacpi_lifecycle
) {
1467 case TPACPI_LIFE_INIT
:
1469 * hotkey_init will call hotkey_poll_setup_safe
1470 * at the appropriate moment
1473 case TPACPI_LIFE_EXITING
:
1475 case TPACPI_LIFE_RUNNING
:
1476 hotkey_poll_setup_safe(0);
1480 /* Should only happen if tpacpi_lifecycle is corrupt */
1485 static void hotkey_inputdev_close(struct input_dev
*dev
)
1487 /* disable hotkey polling when possible */
1488 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1489 hotkey_poll_setup_safe(0);
1492 /* sysfs hotkey enable ------------------------------------------------- */
1493 static ssize_t
hotkey_enable_show(struct device
*dev
,
1494 struct device_attribute
*attr
,
1499 res
= hotkey_status_get(&status
);
1503 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1506 static ssize_t
hotkey_enable_store(struct device
*dev
,
1507 struct device_attribute
*attr
,
1508 const char *buf
, size_t count
)
1513 if (parse_strtoul(buf
, 1, &t
))
1516 res
= hotkey_status_set(t
);
1518 return (res
) ? res
: count
;
1521 static struct device_attribute dev_attr_hotkey_enable
=
1522 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1523 hotkey_enable_show
, hotkey_enable_store
);
1525 /* sysfs hotkey mask --------------------------------------------------- */
1526 static ssize_t
hotkey_mask_show(struct device
*dev
,
1527 struct device_attribute
*attr
,
1532 if (mutex_lock_interruptible(&hotkey_mutex
))
1533 return -ERESTARTSYS
;
1534 res
= hotkey_mask_get();
1535 mutex_unlock(&hotkey_mutex
);
1538 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1541 static ssize_t
hotkey_mask_store(struct device
*dev
,
1542 struct device_attribute
*attr
,
1543 const char *buf
, size_t count
)
1548 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1551 if (mutex_lock_interruptible(&hotkey_mutex
))
1552 return -ERESTARTSYS
;
1554 res
= hotkey_mask_set(t
);
1556 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1557 hotkey_poll_setup(1);
1560 mutex_unlock(&hotkey_mutex
);
1562 return (res
) ? res
: count
;
1565 static struct device_attribute dev_attr_hotkey_mask
=
1566 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1567 hotkey_mask_show
, hotkey_mask_store
);
1569 /* sysfs hotkey bios_enabled ------------------------------------------- */
1570 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1571 struct device_attribute
*attr
,
1574 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_orig_status
);
1577 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1578 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1580 /* sysfs hotkey bios_mask ---------------------------------------------- */
1581 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1582 struct device_attribute
*attr
,
1585 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1588 static struct device_attribute dev_attr_hotkey_bios_mask
=
1589 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1591 /* sysfs hotkey all_mask ----------------------------------------------- */
1592 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
1593 struct device_attribute
*attr
,
1596 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1597 hotkey_all_mask
| hotkey_source_mask
);
1600 static struct device_attribute dev_attr_hotkey_all_mask
=
1601 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
1603 /* sysfs hotkey recommended_mask --------------------------------------- */
1604 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
1605 struct device_attribute
*attr
,
1608 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1609 (hotkey_all_mask
| hotkey_source_mask
)
1610 & ~hotkey_reserved_mask
);
1613 static struct device_attribute dev_attr_hotkey_recommended_mask
=
1614 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
1615 hotkey_recommended_mask_show
, NULL
);
1617 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1619 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1620 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
1621 struct device_attribute
*attr
,
1624 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
1627 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
1628 struct device_attribute
*attr
,
1629 const char *buf
, size_t count
)
1633 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
1634 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
1637 if (mutex_lock_interruptible(&hotkey_mutex
))
1638 return -ERESTARTSYS
;
1640 HOTKEY_CONFIG_CRITICAL_START
1641 hotkey_source_mask
= t
;
1642 HOTKEY_CONFIG_CRITICAL_END
1644 hotkey_poll_setup(1);
1646 mutex_unlock(&hotkey_mutex
);
1651 static struct device_attribute dev_attr_hotkey_source_mask
=
1652 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
1653 hotkey_source_mask_show
, hotkey_source_mask_store
);
1655 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1656 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
1657 struct device_attribute
*attr
,
1660 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
1663 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
1664 struct device_attribute
*attr
,
1665 const char *buf
, size_t count
)
1669 if (parse_strtoul(buf
, 25, &t
))
1672 if (mutex_lock_interruptible(&hotkey_mutex
))
1673 return -ERESTARTSYS
;
1675 hotkey_poll_freq
= t
;
1677 hotkey_poll_setup(1);
1678 mutex_unlock(&hotkey_mutex
);
1683 static struct device_attribute dev_attr_hotkey_poll_freq
=
1684 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
1685 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
1687 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1689 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1690 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
1691 struct device_attribute
*attr
,
1695 res
= hotkey_get_wlsw(&s
);
1699 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1702 static struct device_attribute dev_attr_hotkey_radio_sw
=
1703 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
1705 static void hotkey_radio_sw_notify_change(void)
1707 if (tp_features
.hotkey_wlsw
)
1708 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1712 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1713 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
1714 struct device_attribute
*attr
,
1718 res
= hotkey_get_tablet_mode(&s
);
1722 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1725 static struct device_attribute dev_attr_hotkey_tablet_mode
=
1726 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
1728 static void hotkey_tablet_mode_notify_change(void)
1730 if (tp_features
.hotkey_tablet
)
1731 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1732 "hotkey_tablet_mode");
1735 /* sysfs hotkey report_mode -------------------------------------------- */
1736 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
1737 struct device_attribute
*attr
,
1740 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1741 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
1744 static ssize_t
hotkey_report_mode_store(struct device
*dev
,
1745 struct device_attribute
*attr
,
1746 const char *buf
, size_t count
)
1751 if (parse_strtoul(buf
, 2, &t
) || (t
< 1))
1754 if (t
!= hotkey_report_mode
) {
1755 if (tp_features
.hotkey_report_mode_locked
)
1757 hotkey_report_mode
= t
;
1758 printk(TPACPI_NOTICE
"hot key report mode set to %d\n",
1759 hotkey_report_mode
);
1762 return (res
) ? res
: count
;
1765 static struct device_attribute dev_attr_hotkey_report_mode
=
1766 __ATTR(hotkey_report_mode
, S_IWUSR
| S_IRUGO
,
1767 hotkey_report_mode_show
, hotkey_report_mode_store
);
1769 /* sysfs wakeup reason (pollable) -------------------------------------- */
1770 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
1771 struct device_attribute
*attr
,
1774 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
1777 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
1778 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
1780 void hotkey_wakeup_reason_notify_change(void)
1782 if (tp_features
.hotkey_mask
)
1783 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1787 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1788 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
1789 struct device_attribute
*attr
,
1792 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
1795 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
1796 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
1797 hotkey_wakeup_hotunplug_complete_show
, NULL
);
1799 void hotkey_wakeup_hotunplug_complete_notify_change(void)
1801 if (tp_features
.hotkey_mask
)
1802 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1803 "wakeup_hotunplug_complete");
1806 /* --------------------------------------------------------------------- */
1808 static struct attribute
*hotkey_attributes
[] __initdata
= {
1809 &dev_attr_hotkey_enable
.attr
,
1810 &dev_attr_hotkey_bios_enabled
.attr
,
1811 &dev_attr_hotkey_report_mode
.attr
,
1812 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1813 &dev_attr_hotkey_mask
.attr
,
1814 &dev_attr_hotkey_all_mask
.attr
,
1815 &dev_attr_hotkey_recommended_mask
.attr
,
1816 &dev_attr_hotkey_source_mask
.attr
,
1817 &dev_attr_hotkey_poll_freq
.attr
,
1821 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
1822 &dev_attr_hotkey_bios_mask
.attr
,
1823 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1824 &dev_attr_hotkey_mask
.attr
,
1825 &dev_attr_hotkey_all_mask
.attr
,
1826 &dev_attr_hotkey_recommended_mask
.attr
,
1828 &dev_attr_hotkey_wakeup_reason
.attr
,
1829 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
1832 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
1834 /* Requirements for changing the default keymaps:
1836 * 1. Many of the keys are mapped to KEY_RESERVED for very
1837 * good reasons. Do not change them unless you have deep
1838 * knowledge on the IBM and Lenovo ThinkPad firmware for
1839 * the various ThinkPad models. The driver behaves
1840 * differently for KEY_RESERVED: such keys have their
1841 * hot key mask *unset* in mask_recommended, and also
1842 * in the initial hot key mask programmed into the
1843 * firmware at driver load time, which means the firm-
1844 * ware may react very differently if you change them to
1847 * 2. You must be subscribed to the linux-thinkpad and
1848 * ibm-acpi-devel mailing lists, and you should read the
1849 * list archives since 2007 if you want to change the
1850 * keymaps. This requirement exists so that you will
1851 * know the past history of problems with the thinkpad-
1852 * acpi driver keymaps, and also that you will be
1853 * listening to any bug reports;
1855 * 3. Do not send thinkpad-acpi specific patches directly to
1856 * for merging, *ever*. Send them to the linux-acpi
1857 * mailinglist for comments. Merging is to be done only
1858 * through acpi-test and the ACPI maintainer.
1860 * If the above is too much to ask, don't change the keymap.
1861 * Ask the thinkpad-acpi maintainer to do it, instead.
1863 static u16 ibm_keycode_map
[] __initdata
= {
1864 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1865 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
1866 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
1867 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
1869 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1870 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
1871 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
1872 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
1874 /* brightness: firmware always reacts to them, unless
1875 * X.org did some tricks in the radeon BIOS scratch
1876 * registers of *some* models */
1877 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
1878 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
1880 /* Thinklight: firmware always react to it */
1881 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
1883 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
1884 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
1886 /* Volume: firmware always react to it and reprograms
1887 * the built-in *extra* mixer. Never map it to control
1888 * another mixer by default. */
1889 KEY_RESERVED
, /* 0x14: VOLUME UP */
1890 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
1891 KEY_RESERVED
, /* 0x16: MUTE */
1893 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1895 /* (assignments unknown, please report if found) */
1896 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1897 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1899 static u16 lenovo_keycode_map
[] __initdata
= {
1900 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1901 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
1902 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
1903 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
1905 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1906 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
1907 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
1908 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
1910 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
1911 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
1913 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
1915 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
1916 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
1918 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1919 * react to it and reprograms the built-in *extra* mixer.
1920 * Never map it to control another mixer by default.
1922 * T60?, T61, R60?, R61: firmware and EC tries to send
1923 * these over the regular keyboard, so these are no-ops,
1924 * but there are still weird bugs re. MUTE, so do not
1925 * change unless you get test reports from all Lenovo
1926 * models. May cause the BIOS to interfere with the
1929 KEY_RESERVED
, /* 0x14: VOLUME UP */
1930 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
1931 KEY_RESERVED
, /* 0x16: MUTE */
1933 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1935 /* (assignments unknown, please report if found) */
1936 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1937 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1940 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1941 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1942 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1948 vdbg_printk(TPACPI_DBG_INIT
, "initializing hotkey subdriver\n");
1950 BUG_ON(!tpacpi_inputdev
);
1951 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
1952 tpacpi_inputdev
->close
!= NULL
);
1954 TPACPI_ACPIHANDLE_INIT(hkey
);
1955 mutex_init(&hotkey_mutex
);
1957 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1958 mutex_init(&hotkey_thread_mutex
);
1959 mutex_init(&hotkey_thread_data_mutex
);
1962 /* hotkey not supported on 570 */
1963 tp_features
.hotkey
= hkey_handle
!= NULL
;
1965 vdbg_printk(TPACPI_DBG_INIT
, "hotkeys are %s\n",
1966 str_supported(tp_features
.hotkey
));
1968 if (tp_features
.hotkey
) {
1969 hotkey_dev_attributes
= create_attr_set(13, NULL
);
1970 if (!hotkey_dev_attributes
)
1972 res
= add_many_to_attr_set(hotkey_dev_attributes
,
1974 ARRAY_SIZE(hotkey_attributes
));
1978 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1979 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1980 for HKEY interface version 0x100 */
1981 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
1982 if ((hkeyv
>> 8) != 1) {
1983 printk(TPACPI_ERR
"unknown version of the "
1984 "HKEY interface: 0x%x\n", hkeyv
);
1985 printk(TPACPI_ERR
"please report this to %s\n",
1989 * MHKV 0x100 in A31, R40, R40e,
1990 * T4x, X31, and later
1992 tp_features
.hotkey_mask
= 1;
1996 vdbg_printk(TPACPI_DBG_INIT
, "hotkey masks are %s\n",
1997 str_supported(tp_features
.hotkey_mask
));
1999 if (tp_features
.hotkey_mask
) {
2000 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2003 "missing MHKA handler, "
2004 "please report this to %s\n",
2006 /* FN+F12, FN+F4, FN+F3 */
2007 hotkey_all_mask
= 0x080cU
;
2011 /* hotkey_source_mask *must* be zero for
2012 * the first hotkey_mask_get */
2013 res
= hotkey_status_get(&hotkey_orig_status
);
2014 if (!res
&& tp_features
.hotkey_mask
) {
2015 res
= hotkey_mask_get();
2016 hotkey_orig_mask
= hotkey_mask
;
2018 res
= add_many_to_attr_set(
2019 hotkey_dev_attributes
,
2020 hotkey_mask_attributes
,
2021 ARRAY_SIZE(hotkey_mask_attributes
));
2025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2026 if (tp_features
.hotkey_mask
) {
2027 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2030 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2033 vdbg_printk(TPACPI_DBG_INIT
,
2034 "hotkey source mask 0x%08x, polling freq %d\n",
2035 hotkey_source_mask
, hotkey_poll_freq
);
2038 /* Not all thinkpads have a hardware radio switch */
2039 if (!res
&& acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2040 tp_features
.hotkey_wlsw
= 1;
2042 "radio switch found; radios are %s\n",
2043 enabled(status
, 0));
2044 res
= add_to_attr_set(hotkey_dev_attributes
,
2045 &dev_attr_hotkey_radio_sw
.attr
);
2048 /* For X41t, X60t, X61t Tablets... */
2049 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2050 tp_features
.hotkey_tablet
= 1;
2052 "possible tablet mode switch found; "
2053 "ThinkPad in %s mode\n",
2054 (status
& TP_HOTKEY_TABLET_MASK
)?
2055 "tablet" : "laptop");
2056 res
= add_to_attr_set(hotkey_dev_attributes
,
2057 &dev_attr_hotkey_tablet_mode
.attr
);
2061 res
= register_attr_set_with_sysfs(
2062 hotkey_dev_attributes
,
2063 &tpacpi_pdev
->dev
.kobj
);
2067 /* Set up key map */
2069 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2071 if (!hotkey_keycode_map
) {
2073 "failed to allocate memory for key map\n");
2077 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2078 dbg_printk(TPACPI_DBG_INIT
,
2079 "using Lenovo default hot key map\n");
2080 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2081 TPACPI_HOTKEY_MAP_SIZE
);
2083 dbg_printk(TPACPI_DBG_INIT
,
2084 "using IBM default hot key map\n");
2085 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2086 TPACPI_HOTKEY_MAP_SIZE
);
2089 /* Do not issue duplicate brightness change events to
2091 if (tp_features
.bright_acpimode
) {
2092 printk(TPACPI_NOTICE
2093 "Brightness hot keys firmware issues ACPI "
2095 printk(TPACPI_NOTICE
2096 "Disabling brightness hot keys on event "
2098 hotkey_keycode_map
[TP_ACPI_HOTKEYSCAN_FNHOME
] =
2100 hotkey_keycode_map
[TP_ACPI_HOTKEYSCAN_FNEND
] =
2104 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2105 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2106 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2107 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2108 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2109 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2110 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2111 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2112 set_bit(hotkey_keycode_map
[i
],
2113 tpacpi_inputdev
->keybit
);
2115 if (i
< sizeof(hotkey_reserved_mask
)*8)
2116 hotkey_reserved_mask
|= 1 << i
;
2120 if (tp_features
.hotkey_wlsw
) {
2121 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2122 set_bit(SW_RADIO
, tpacpi_inputdev
->swbit
);
2124 if (tp_features
.hotkey_tablet
) {
2125 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2126 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2129 dbg_printk(TPACPI_DBG_INIT
,
2130 "enabling hot key handling\n");
2131 res
= hotkey_status_set(1);
2134 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2135 & ~hotkey_reserved_mask
)
2136 | hotkey_orig_mask
);
2137 if (res
< 0 && res
!= -ENXIO
)
2140 if (hotkey_report_mode
> 0) {
2141 tp_features
.hotkey_report_mode_locked
= 1;
2143 hotkey_report_mode
= 1;
2144 vdbg_printk(TPACPI_DBG_INIT
,
2145 "hot key reporting mode can be "
2146 "changed at runtime\n");
2149 dbg_printk(TPACPI_DBG_INIT
,
2150 "legacy hot key reporting over procfs %s\n",
2151 (hotkey_report_mode
< 2) ?
2152 "enabled" : "disabled");
2154 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2155 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2157 hotkey_poll_setup_safe(1);
2158 tpacpi_input_send_radiosw();
2159 tpacpi_input_send_tabletsw();
2162 return (tp_features
.hotkey
)? 0 : 1;
2165 static void hotkey_exit(void)
2167 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2168 hotkey_poll_stop_sync();
2171 if (tp_features
.hotkey
) {
2172 dbg_printk(TPACPI_DBG_EXIT
,
2173 "restoring original hot key mask\n");
2174 /* no short-circuit boolean operator below! */
2175 if ((hotkey_mask_set(hotkey_orig_mask
) |
2176 hotkey_status_set(hotkey_orig_status
)) != 0)
2178 "failed to restore hot key mask "
2179 "to BIOS defaults\n");
2182 if (hotkey_dev_attributes
) {
2183 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2184 hotkey_dev_attributes
= NULL
;
2188 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2191 unsigned int scancode
;
2196 if (event
!= 0x80) {
2198 "unknown HKEY notification event %d\n", event
);
2199 /* forward it to userspace, maybe it knows how to handle it */
2200 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 0);
2206 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2207 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2220 switch (hkey
>> 12) {
2222 /* 0x1000-0x1FFF: key presses */
2223 scancode
= hkey
& 0xfff;
2224 if (scancode
> 0 && scancode
< 0x21) {
2226 if (!(hotkey_source_mask
& (1 << scancode
))) {
2227 tpacpi_input_send_key(scancode
);
2239 case 0x2304: /* suspend, undock */
2240 case 0x2404: /* hibernation, undock */
2241 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2244 case 0x2305: /* suspend, bay eject */
2245 case 0x2405: /* hibernation, bay eject */
2246 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2252 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2254 "woke up due to a hot-unplug "
2256 hotkey_wakeup_reason_notify_change();
2260 /* bay-related wakeups */
2261 if (hkey
== 0x3003) {
2262 hotkey_autosleep_ack
= 1;
2265 hotkey_wakeup_hotunplug_complete_notify_change();
2271 /* dock-related wakeups */
2272 if (hkey
== 0x4003) {
2273 hotkey_autosleep_ack
= 1;
2276 hotkey_wakeup_hotunplug_complete_notify_change();
2282 /* 0x5000-0x5FFF: human interface helpers */
2284 case 0x5010: /* Lenovo new BIOS: brightness changed */
2285 case 0x500b: /* X61t: tablet pen inserted into bay */
2286 case 0x500c: /* X61t: tablet pen removed from bay */
2288 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2289 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2290 tpacpi_input_send_tabletsw();
2291 hotkey_tablet_mode_notify_change();
2296 /* LID switch events. Do not propagate */
2304 /* 0x7000-0x7FFF: misc */
2305 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2306 tpacpi_input_send_radiosw();
2307 hotkey_radio_sw_notify_change();
2311 /* fallthrough to default */
2316 printk(TPACPI_NOTICE
2317 "unhandled HKEY event 0x%04x\n", hkey
);
2320 if (!ignore_acpi_ev
&&
2321 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2322 acpi_bus_generate_event(ibm
->acpi
->device
, event
, hkey
);
2327 static void hotkey_suspend(pm_message_t state
)
2329 /* Do these on suspend, we get the events on early resume! */
2330 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2331 hotkey_autosleep_ack
= 0;
2334 static void hotkey_resume(void)
2336 if (hotkey_mask_get())
2338 "error while trying to read hot key mask "
2340 tpacpi_input_send_radiosw();
2341 hotkey_radio_sw_notify_change();
2342 hotkey_tablet_mode_notify_change();
2343 hotkey_wakeup_reason_notify_change();
2344 hotkey_wakeup_hotunplug_complete_notify_change();
2345 hotkey_poll_setup_safe(0);
2348 /* procfs -------------------------------------------------------------- */
2349 static int hotkey_read(char *p
)
2354 if (!tp_features
.hotkey
) {
2355 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2359 if (mutex_lock_interruptible(&hotkey_mutex
))
2360 return -ERESTARTSYS
;
2361 res
= hotkey_status_get(&status
);
2363 res
= hotkey_mask_get();
2364 mutex_unlock(&hotkey_mutex
);
2368 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2369 if (tp_features
.hotkey_mask
) {
2370 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2371 len
+= sprintf(p
+ len
,
2372 "commands:\tenable, disable, reset, <mask>\n");
2374 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2375 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2381 static int hotkey_write(char *buf
)
2387 if (!tp_features
.hotkey
)
2390 if (mutex_lock_interruptible(&hotkey_mutex
))
2391 return -ERESTARTSYS
;
2397 while ((cmd
= next_cmd(&buf
))) {
2398 if (strlencmp(cmd
, "enable") == 0) {
2400 } else if (strlencmp(cmd
, "disable") == 0) {
2402 } else if (strlencmp(cmd
, "reset") == 0) {
2403 status
= hotkey_orig_status
;
2404 mask
= hotkey_orig_mask
;
2405 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2407 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2415 res
= hotkey_status_set(status
);
2417 if (!res
&& mask
!= hotkey_mask
)
2418 res
= hotkey_mask_set(mask
);
2421 mutex_unlock(&hotkey_mutex
);
2425 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
2426 .hid
= TPACPI_ACPI_HKEY_HID
,
2427 .notify
= hotkey_notify
,
2428 .handle
= &hkey_handle
,
2429 .type
= ACPI_DEVICE_NOTIFY
,
2432 static struct ibm_struct hotkey_driver_data
= {
2434 .read
= hotkey_read
,
2435 .write
= hotkey_write
,
2436 .exit
= hotkey_exit
,
2437 .resume
= hotkey_resume
,
2438 .suspend
= hotkey_suspend
,
2439 .acpi
= &ibm_hotkey_acpidriver
,
2442 /*************************************************************************
2443 * Bluetooth subdriver
2447 /* ACPI GBDC/SBDC bits */
2448 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
2449 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
2450 TP_ACPI_BLUETOOTH_UNK
= 0x04, /* unknown function */
2453 static int bluetooth_get_radiosw(void);
2454 static int bluetooth_set_radiosw(int radio_on
);
2456 /* sysfs bluetooth enable ---------------------------------------------- */
2457 static ssize_t
bluetooth_enable_show(struct device
*dev
,
2458 struct device_attribute
*attr
,
2463 status
= bluetooth_get_radiosw();
2467 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
? 1 : 0);
2470 static ssize_t
bluetooth_enable_store(struct device
*dev
,
2471 struct device_attribute
*attr
,
2472 const char *buf
, size_t count
)
2477 if (parse_strtoul(buf
, 1, &t
))
2480 res
= bluetooth_set_radiosw(t
);
2482 return (res
) ? res
: count
;
2485 static struct device_attribute dev_attr_bluetooth_enable
=
2486 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
2487 bluetooth_enable_show
, bluetooth_enable_store
);
2489 /* --------------------------------------------------------------------- */
2491 static struct attribute
*bluetooth_attributes
[] = {
2492 &dev_attr_bluetooth_enable
.attr
,
2496 static const struct attribute_group bluetooth_attr_group
= {
2497 .attrs
= bluetooth_attributes
,
2500 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
2505 vdbg_printk(TPACPI_DBG_INIT
, "initializing bluetooth subdriver\n");
2507 TPACPI_ACPIHANDLE_INIT(hkey
);
2509 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2510 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2511 tp_features
.bluetooth
= hkey_handle
&&
2512 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
2514 vdbg_printk(TPACPI_DBG_INIT
, "bluetooth is %s, status 0x%02x\n",
2515 str_supported(tp_features
.bluetooth
),
2518 if (tp_features
.bluetooth
) {
2519 if (!(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
2520 /* no bluetooth hardware present in system */
2521 tp_features
.bluetooth
= 0;
2522 dbg_printk(TPACPI_DBG_INIT
,
2523 "bluetooth hardware not installed\n");
2525 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2526 &bluetooth_attr_group
);
2532 return (tp_features
.bluetooth
)? 0 : 1;
2535 static void bluetooth_exit(void)
2537 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2538 &bluetooth_attr_group
);
2541 static int bluetooth_get_radiosw(void)
2545 if (!tp_features
.bluetooth
)
2548 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2551 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0);
2554 static int bluetooth_set_radiosw(int radio_on
)
2558 if (!tp_features
.bluetooth
)
2561 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2564 status
|= TP_ACPI_BLUETOOTH_RADIOSSW
;
2566 status
&= ~TP_ACPI_BLUETOOTH_RADIOSSW
;
2567 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
2573 /* procfs -------------------------------------------------------------- */
2574 static int bluetooth_read(char *p
)
2577 int status
= bluetooth_get_radiosw();
2579 if (!tp_features
.bluetooth
)
2580 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2582 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2583 (status
)? "enabled" : "disabled");
2584 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2590 static int bluetooth_write(char *buf
)
2594 if (!tp_features
.bluetooth
)
2597 while ((cmd
= next_cmd(&buf
))) {
2598 if (strlencmp(cmd
, "enable") == 0) {
2599 bluetooth_set_radiosw(1);
2600 } else if (strlencmp(cmd
, "disable") == 0) {
2601 bluetooth_set_radiosw(0);
2609 static struct ibm_struct bluetooth_driver_data
= {
2610 .name
= "bluetooth",
2611 .read
= bluetooth_read
,
2612 .write
= bluetooth_write
,
2613 .exit
= bluetooth_exit
,
2616 /*************************************************************************
2621 /* ACPI GWAN/SWAN bits */
2622 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
2623 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
2624 TP_ACPI_WANCARD_UNK
= 0x04, /* unknown function */
2627 static int wan_get_radiosw(void);
2628 static int wan_set_radiosw(int radio_on
);
2630 /* sysfs wan enable ---------------------------------------------------- */
2631 static ssize_t
wan_enable_show(struct device
*dev
,
2632 struct device_attribute
*attr
,
2637 status
= wan_get_radiosw();
2641 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
? 1 : 0);
2644 static ssize_t
wan_enable_store(struct device
*dev
,
2645 struct device_attribute
*attr
,
2646 const char *buf
, size_t count
)
2651 if (parse_strtoul(buf
, 1, &t
))
2654 res
= wan_set_radiosw(t
);
2656 return (res
) ? res
: count
;
2659 static struct device_attribute dev_attr_wan_enable
=
2660 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
2661 wan_enable_show
, wan_enable_store
);
2663 /* --------------------------------------------------------------------- */
2665 static struct attribute
*wan_attributes
[] = {
2666 &dev_attr_wan_enable
.attr
,
2670 static const struct attribute_group wan_attr_group
= {
2671 .attrs
= wan_attributes
,
2674 static int __init
wan_init(struct ibm_init_struct
*iibm
)
2679 vdbg_printk(TPACPI_DBG_INIT
, "initializing wan subdriver\n");
2681 TPACPI_ACPIHANDLE_INIT(hkey
);
2683 tp_features
.wan
= hkey_handle
&&
2684 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
2686 vdbg_printk(TPACPI_DBG_INIT
, "wan is %s, status 0x%02x\n",
2687 str_supported(tp_features
.wan
),
2690 if (tp_features
.wan
) {
2691 if (!(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
2692 /* no wan hardware present in system */
2693 tp_features
.wan
= 0;
2694 dbg_printk(TPACPI_DBG_INIT
,
2695 "wan hardware not installed\n");
2697 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2704 return (tp_features
.wan
)? 0 : 1;
2707 static void wan_exit(void)
2709 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2713 static int wan_get_radiosw(void)
2717 if (!tp_features
.wan
)
2720 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2723 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0);
2726 static int wan_set_radiosw(int radio_on
)
2730 if (!tp_features
.wan
)
2733 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2736 status
|= TP_ACPI_WANCARD_RADIOSSW
;
2738 status
&= ~TP_ACPI_WANCARD_RADIOSSW
;
2739 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
2745 /* procfs -------------------------------------------------------------- */
2746 static int wan_read(char *p
)
2749 int status
= wan_get_radiosw();
2751 if (!tp_features
.wan
)
2752 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2754 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2755 (status
)? "enabled" : "disabled");
2756 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2762 static int wan_write(char *buf
)
2766 if (!tp_features
.wan
)
2769 while ((cmd
= next_cmd(&buf
))) {
2770 if (strlencmp(cmd
, "enable") == 0) {
2772 } else if (strlencmp(cmd
, "disable") == 0) {
2781 static struct ibm_struct wan_driver_data
= {
2786 .flags
.experimental
= 1,
2789 /*************************************************************************
2793 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2795 enum video_access_mode
{
2796 TPACPI_VIDEO_NONE
= 0,
2797 TPACPI_VIDEO_570
, /* 570 */
2798 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
2799 TPACPI_VIDEO_NEW
, /* all others */
2802 enum { /* video status flags, based on VIDEO_570 */
2803 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
2804 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
2805 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
2808 enum { /* TPACPI_VIDEO_570 constants */
2809 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
2810 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
2811 * video_status_flags */
2812 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
2813 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
2816 static enum video_access_mode video_supported
;
2817 static int video_orig_autosw
;
2819 static int video_autosw_get(void);
2820 static int video_autosw_set(int enable
);
2822 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2824 static int __init
video_init(struct ibm_init_struct
*iibm
)
2828 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
2830 TPACPI_ACPIHANDLE_INIT(vid
);
2831 TPACPI_ACPIHANDLE_INIT(vid2
);
2833 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
2834 /* G41, assume IVGA doesn't change */
2835 vid_handle
= vid2_handle
;
2838 /* video switching not supported on R30, R31 */
2839 video_supported
= TPACPI_VIDEO_NONE
;
2840 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
2842 video_supported
= TPACPI_VIDEO_570
;
2843 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
2844 /* 600e/x, 770e, 770x */
2845 video_supported
= TPACPI_VIDEO_770
;
2848 video_supported
= TPACPI_VIDEO_NEW
;
2850 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
2851 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
2854 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
2857 static void video_exit(void)
2859 dbg_printk(TPACPI_DBG_EXIT
,
2860 "restoring original video autoswitch mode\n");
2861 if (video_autosw_set(video_orig_autosw
))
2862 printk(TPACPI_ERR
"error while trying to restore original "
2863 "video autoswitch mode\n");
2866 static int video_outputsw_get(void)
2871 switch (video_supported
) {
2872 case TPACPI_VIDEO_570
:
2873 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
2874 TP_ACPI_VIDEO_570_PHSCMD
))
2876 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
2878 case TPACPI_VIDEO_770
:
2879 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
2882 status
|= TP_ACPI_VIDEO_S_LCD
;
2883 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
2886 status
|= TP_ACPI_VIDEO_S_CRT
;
2888 case TPACPI_VIDEO_NEW
:
2889 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
2890 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
2893 status
|= TP_ACPI_VIDEO_S_CRT
;
2895 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
2896 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
2899 status
|= TP_ACPI_VIDEO_S_LCD
;
2900 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
2903 status
|= TP_ACPI_VIDEO_S_DVI
;
2912 static int video_outputsw_set(int status
)
2917 switch (video_supported
) {
2918 case TPACPI_VIDEO_570
:
2919 res
= acpi_evalf(NULL
, NULL
,
2920 "\\_SB.PHS2", "vdd",
2921 TP_ACPI_VIDEO_570_PHS2CMD
,
2922 status
| TP_ACPI_VIDEO_570_PHS2SET
);
2924 case TPACPI_VIDEO_770
:
2925 autosw
= video_autosw_get();
2929 res
= video_autosw_set(1);
2932 res
= acpi_evalf(vid_handle
, NULL
,
2933 "ASWT", "vdd", status
* 0x100, 0);
2934 if (!autosw
&& video_autosw_set(autosw
)) {
2936 "video auto-switch left enabled due to error\n");
2940 case TPACPI_VIDEO_NEW
:
2941 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
2942 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
2948 return (res
)? 0 : -EIO
;
2951 static int video_autosw_get(void)
2955 switch (video_supported
) {
2956 case TPACPI_VIDEO_570
:
2957 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
2960 case TPACPI_VIDEO_770
:
2961 case TPACPI_VIDEO_NEW
:
2962 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
2972 static int video_autosw_set(int enable
)
2974 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
2979 static int video_outputsw_cycle(void)
2981 int autosw
= video_autosw_get();
2987 switch (video_supported
) {
2988 case TPACPI_VIDEO_570
:
2989 res
= video_autosw_set(1);
2992 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
2994 case TPACPI_VIDEO_770
:
2995 case TPACPI_VIDEO_NEW
:
2996 res
= video_autosw_set(1);
2999 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
3004 if (!autosw
&& video_autosw_set(autosw
)) {
3006 "video auto-switch left enabled due to error\n");
3010 return (res
)? 0 : -EIO
;
3013 static int video_expand_toggle(void)
3015 switch (video_supported
) {
3016 case TPACPI_VIDEO_570
:
3017 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
3019 case TPACPI_VIDEO_770
:
3020 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
3022 case TPACPI_VIDEO_NEW
:
3023 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
3031 static int video_read(char *p
)
3036 if (video_supported
== TPACPI_VIDEO_NONE
) {
3037 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3041 status
= video_outputsw_get();
3045 autosw
= video_autosw_get();
3049 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3050 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
3051 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
3052 if (video_supported
== TPACPI_VIDEO_NEW
)
3053 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
3054 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
3055 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
3056 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
3057 if (video_supported
== TPACPI_VIDEO_NEW
)
3058 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
3059 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
3060 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
3065 static int video_write(char *buf
)
3068 int enable
, disable
, status
;
3071 if (video_supported
== TPACPI_VIDEO_NONE
)
3077 while ((cmd
= next_cmd(&buf
))) {
3078 if (strlencmp(cmd
, "lcd_enable") == 0) {
3079 enable
|= TP_ACPI_VIDEO_S_LCD
;
3080 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
3081 disable
|= TP_ACPI_VIDEO_S_LCD
;
3082 } else if (strlencmp(cmd
, "crt_enable") == 0) {
3083 enable
|= TP_ACPI_VIDEO_S_CRT
;
3084 } else if (strlencmp(cmd
, "crt_disable") == 0) {
3085 disable
|= TP_ACPI_VIDEO_S_CRT
;
3086 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3087 strlencmp(cmd
, "dvi_enable") == 0) {
3088 enable
|= TP_ACPI_VIDEO_S_DVI
;
3089 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3090 strlencmp(cmd
, "dvi_disable") == 0) {
3091 disable
|= TP_ACPI_VIDEO_S_DVI
;
3092 } else if (strlencmp(cmd
, "auto_enable") == 0) {
3093 res
= video_autosw_set(1);
3096 } else if (strlencmp(cmd
, "auto_disable") == 0) {
3097 res
= video_autosw_set(0);
3100 } else if (strlencmp(cmd
, "video_switch") == 0) {
3101 res
= video_outputsw_cycle();
3104 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
3105 res
= video_expand_toggle();
3112 if (enable
|| disable
) {
3113 status
= video_outputsw_get();
3116 res
= video_outputsw_set((status
& ~disable
) | enable
);
3124 static struct ibm_struct video_driver_data
= {
3127 .write
= video_write
,
3131 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3133 /*************************************************************************
3134 * Light (thinklight) subdriver
3137 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3138 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
3140 static int __init
light_init(struct ibm_init_struct
*iibm
)
3142 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
3144 TPACPI_ACPIHANDLE_INIT(ledb
);
3145 TPACPI_ACPIHANDLE_INIT(lght
);
3146 TPACPI_ACPIHANDLE_INIT(cmos
);
3148 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3149 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
3151 if (tp_features
.light
)
3152 /* light status not supported on
3153 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3154 tp_features
.light_status
=
3155 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
3157 vdbg_printk(TPACPI_DBG_INIT
, "light is %s\n",
3158 str_supported(tp_features
.light
));
3160 return (tp_features
.light
)? 0 : 1;
3163 static int light_read(char *p
)
3168 if (!tp_features
.light
) {
3169 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3170 } else if (!tp_features
.light_status
) {
3171 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
3172 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3174 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
3176 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
3177 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3183 static int light_write(char *buf
)
3185 int cmos_cmd
, lght_cmd
;
3189 if (!tp_features
.light
)
3192 while ((cmd
= next_cmd(&buf
))) {
3193 if (strlencmp(cmd
, "on") == 0) {
3196 } else if (strlencmp(cmd
, "off") == 0) {
3202 success
= cmos_handle
?
3203 acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
) :
3204 acpi_evalf(lght_handle
, NULL
, NULL
, "vd", lght_cmd
);
3212 static struct ibm_struct light_driver_data
= {
3215 .write
= light_write
,
3218 /*************************************************************************
3222 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3224 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
3225 static int dock_read(char *p
);
3226 static int dock_write(char *buf
);
3228 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
3229 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3230 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3231 "\\_SB.PCI.ISA.SLCE", /* 570 */
3232 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3234 /* don't list other alternatives as we install a notify handler on the 570 */
3235 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
3237 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
3239 .notify
= dock_notify
,
3240 .handle
= &dock_handle
,
3241 .type
= ACPI_SYSTEM_NOTIFY
,
3244 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3245 * We just use it to get notifications of dock hotplug
3246 * in very old thinkpads */
3247 .hid
= PCI_ROOT_HID_STRING
,
3248 .notify
= dock_notify
,
3249 .handle
= &pci_handle
,
3250 .type
= ACPI_SYSTEM_NOTIFY
,
3254 static struct ibm_struct dock_driver_data
[2] = {
3258 .write
= dock_write
,
3259 .acpi
= &ibm_dock_acpidriver
[0],
3263 .acpi
= &ibm_dock_acpidriver
[1],
3267 #define dock_docked() (_sta(dock_handle) & 1)
3269 static int __init
dock_init(struct ibm_init_struct
*iibm
)
3271 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
3273 TPACPI_ACPIHANDLE_INIT(dock
);
3275 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
3276 str_supported(dock_handle
!= NULL
));
3278 return (dock_handle
)? 0 : 1;
3281 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
3285 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
3287 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
3288 dock_driver_data
[0].flags
.acpi_notify_installed
) {
3289 TPACPI_ACPIHANDLE_INIT(pci
);
3290 dock2_needed
= (pci_handle
!= NULL
);
3291 vdbg_printk(TPACPI_DBG_INIT
,
3292 "dock PCI handler for the TP 570 is %s\n",
3293 str_supported(dock2_needed
));
3295 vdbg_printk(TPACPI_DBG_INIT
,
3296 "dock subdriver part 2 not required\n");
3300 return (dock2_needed
)? 0 : 1;
3303 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
3305 int docked
= dock_docked();
3306 int pci
= ibm
->acpi
->hid
&& strstr(ibm
->acpi
->hid
, PCI_ROOT_HID_STRING
);
3308 if (event
== 1 && !pci
) /* 570 */
3309 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 1); /* button */
3310 else if (event
== 1 && pci
) /* 570 */
3311 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 3); /* dock */
3312 else if (event
== 3 && docked
)
3313 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 1); /* button */
3314 else if (event
== 3 && !docked
)
3315 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 2); /* undock */
3316 else if (event
== 0 && docked
)
3317 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 3); /* dock */
3319 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
3320 event
, _sta(dock_handle
));
3321 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 0); /* unknown */
3325 static int dock_read(char *p
)
3328 int docked
= dock_docked();
3331 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3333 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
3335 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
3336 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
3342 static int dock_write(char *buf
)
3349 while ((cmd
= next_cmd(&buf
))) {
3350 if (strlencmp(cmd
, "undock") == 0) {
3351 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
3352 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
3354 } else if (strlencmp(cmd
, "dock") == 0) {
3355 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
3364 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3366 /*************************************************************************
3370 #ifdef CONFIG_THINKPAD_ACPI_BAY
3372 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3373 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3374 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3375 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3376 ); /* A21e, R30, R31 */
3377 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
3378 "_EJ0", /* all others */
3379 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3380 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3381 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3383 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
3387 static int __init
bay_init(struct ibm_init_struct
*iibm
)
3389 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
3391 TPACPI_ACPIHANDLE_INIT(bay
);
3393 TPACPI_ACPIHANDLE_INIT(bay_ej
);
3394 TPACPI_ACPIHANDLE_INIT(bay2
);
3396 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
3398 tp_features
.bay_status
= bay_handle
&&
3399 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
3400 tp_features
.bay_status2
= bay2_handle
&&
3401 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
3403 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
3404 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
3405 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
3406 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
3408 vdbg_printk(TPACPI_DBG_INIT
,
3409 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3410 str_supported(tp_features
.bay_status
),
3411 str_supported(tp_features
.bay_eject
),
3412 str_supported(tp_features
.bay_status2
),
3413 str_supported(tp_features
.bay_eject2
));
3415 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
3416 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
3419 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
3421 acpi_bus_generate_event(ibm
->acpi
->device
, event
, 0);
3424 #define bay_occupied(b) (_sta(b##_handle) & 1)
3426 static int bay_read(char *p
)
3429 int occupied
= bay_occupied(bay
);
3430 int occupied2
= bay_occupied(bay2
);
3433 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3434 tp_features
.bay_status
?
3435 (occupied
? "occupied" : "unoccupied") :
3437 if (tp_features
.bay_status2
)
3438 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
3439 "occupied" : "unoccupied");
3441 eject
= tp_features
.bay_eject
&& occupied
;
3442 eject2
= tp_features
.bay_eject2
&& occupied2
;
3444 if (eject
&& eject2
)
3445 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
3447 len
+= sprintf(p
+ len
, "commands:\teject\n");
3449 len
+= sprintf(p
+ len
, "commands:\teject2\n");
3454 static int bay_write(char *buf
)
3458 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
3461 while ((cmd
= next_cmd(&buf
))) {
3462 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
3463 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
3465 } else if (tp_features
.bay_eject2
&&
3466 strlencmp(cmd
, "eject2") == 0) {
3467 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
3476 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
3477 .notify
= bay_notify
,
3478 .handle
= &bay_handle
,
3479 .type
= ACPI_SYSTEM_NOTIFY
,
3482 static struct ibm_struct bay_driver_data
= {
3486 .acpi
= &ibm_bay_acpidriver
,
3489 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3491 /*************************************************************************
3495 /* sysfs cmos_command -------------------------------------------------- */
3496 static ssize_t
cmos_command_store(struct device
*dev
,
3497 struct device_attribute
*attr
,
3498 const char *buf
, size_t count
)
3500 unsigned long cmos_cmd
;
3503 if (parse_strtoul(buf
, 21, &cmos_cmd
))
3506 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3507 return (res
)? res
: count
;
3510 static struct device_attribute dev_attr_cmos_command
=
3511 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
3513 /* --------------------------------------------------------------------- */
3515 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
3519 vdbg_printk(TPACPI_DBG_INIT
,
3520 "initializing cmos commands subdriver\n");
3522 TPACPI_ACPIHANDLE_INIT(cmos
);
3524 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
3525 str_supported(cmos_handle
!= NULL
));
3527 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3531 return (cmos_handle
)? 0 : 1;
3534 static void cmos_exit(void)
3536 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3539 static int cmos_read(char *p
)
3543 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3544 R30, R31, T20-22, X20-21 */
3546 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3548 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3549 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
3555 static int cmos_write(char *buf
)
3560 while ((cmd
= next_cmd(&buf
))) {
3561 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
3562 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
3567 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3575 static struct ibm_struct cmos_driver_data
= {
3578 .write
= cmos_write
,
3582 /*************************************************************************
3586 enum led_access_mode
{
3587 TPACPI_LED_NONE
= 0,
3588 TPACPI_LED_570
, /* 570 */
3589 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3590 TPACPI_LED_NEW
, /* all others */
3593 enum { /* For TPACPI_LED_OLD */
3594 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
3595 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
3596 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
3599 static enum led_access_mode led_supported
;
3601 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
3602 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3603 /* T20-22, X20-21 */
3604 "LED", /* all others */
3607 static int __init
led_init(struct ibm_init_struct
*iibm
)
3609 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
3611 TPACPI_ACPIHANDLE_INIT(led
);
3614 /* led not supported on R30, R31 */
3615 led_supported
= TPACPI_LED_NONE
;
3616 else if (strlencmp(led_path
, "SLED") == 0)
3618 led_supported
= TPACPI_LED_570
;
3619 else if (strlencmp(led_path
, "SYSL") == 0)
3620 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3621 led_supported
= TPACPI_LED_OLD
;
3624 led_supported
= TPACPI_LED_NEW
;
3626 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
3627 str_supported(led_supported
), led_supported
);
3629 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
3632 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3634 static int led_read(char *p
)
3638 if (!led_supported
) {
3639 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3642 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3644 if (led_supported
== TPACPI_LED_570
) {
3647 for (i
= 0; i
< 8; i
++) {
3648 if (!acpi_evalf(ec_handle
,
3649 &status
, "GLED", "dd", 1 << i
))
3651 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
3652 i
, led_status(status
));
3656 len
+= sprintf(p
+ len
, "commands:\t"
3657 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3662 /* off, on, blink */
3663 static const int led_sled_arg1
[] = { 0, 1, 3 };
3664 static const int led_exp_hlbl
[] = { 0, 0, 1 }; /* led# * */
3665 static const int led_exp_hlcl
[] = { 0, 1, 1 }; /* led# * */
3666 static const int led_led_arg1
[] = { 0, 0x80, 0xc0 };
3668 static int led_write(char *buf
)
3676 while ((cmd
= next_cmd(&buf
))) {
3677 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
3680 if (strstr(cmd
, "off")) {
3682 } else if (strstr(cmd
, "on")) {
3684 } else if (strstr(cmd
, "blink")) {
3689 if (led_supported
== TPACPI_LED_570
) {
3692 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
3693 led
, led_sled_arg1
[ind
]))
3695 } else if (led_supported
== TPACPI_LED_OLD
) {
3696 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3698 ret
= ec_write(TPACPI_LED_EC_HLMS
, led
);
3700 ret
= ec_write(TPACPI_LED_EC_HLBL
,
3701 led
* led_exp_hlbl
[ind
]);
3703 ret
= ec_write(TPACPI_LED_EC_HLCL
,
3704 led
* led_exp_hlcl
[ind
]);
3709 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
3710 led
, led_led_arg1
[ind
]))
3718 static struct ibm_struct led_driver_data
= {
3724 /*************************************************************************
3728 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
3730 static int __init
beep_init(struct ibm_init_struct
*iibm
)
3732 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
3734 TPACPI_ACPIHANDLE_INIT(beep
);
3736 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
3737 str_supported(beep_handle
!= NULL
));
3739 return (beep_handle
)? 0 : 1;
3742 static int beep_read(char *p
)
3747 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3749 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3750 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
3756 static int beep_write(char *buf
)
3764 while ((cmd
= next_cmd(&buf
))) {
3765 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
3766 beep_cmd
>= 0 && beep_cmd
<= 17) {
3770 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
3777 static struct ibm_struct beep_driver_data
= {
3780 .write
= beep_write
,
3783 /*************************************************************************
3787 enum thermal_access_mode
{
3788 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
3789 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
3790 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
3791 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
3792 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
3795 enum { /* TPACPI_THERMAL_TPEC_* */
3796 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
3797 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
3798 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
3801 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3802 struct ibm_thermal_sensors_struct
{
3803 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
3806 static enum thermal_access_mode thermal_read_mode
;
3808 /* idx is zero-based */
3809 static int thermal_get_sensor(int idx
, s32
*value
)
3815 t
= TP_EC_THERMAL_TMP0
;
3817 switch (thermal_read_mode
) {
3818 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3819 case TPACPI_THERMAL_TPEC_16
:
3820 if (idx
>= 8 && idx
<= 15) {
3821 t
= TP_EC_THERMAL_TMP8
;
3826 case TPACPI_THERMAL_TPEC_8
:
3828 if (!acpi_ec_read(t
+ idx
, &tmp
))
3830 *value
= tmp
* 1000;
3835 case TPACPI_THERMAL_ACPI_UPDT
:
3837 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
3838 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
3840 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
3842 *value
= (t
- 2732) * 100;
3847 case TPACPI_THERMAL_ACPI_TMP07
:
3849 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
3850 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
3852 if (t
> 127 || t
< -127)
3853 t
= TP_EC_THERMAL_TMP_NA
;
3859 case TPACPI_THERMAL_NONE
:
3867 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
3878 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
3881 for (i
= 0 ; i
< n
; i
++) {
3882 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
3890 /* sysfs temp##_input -------------------------------------------------- */
3892 static ssize_t
thermal_temp_input_show(struct device
*dev
,
3893 struct device_attribute
*attr
,
3896 struct sensor_device_attribute
*sensor_attr
=
3897 to_sensor_dev_attr(attr
);
3898 int idx
= sensor_attr
->index
;
3902 res
= thermal_get_sensor(idx
, &value
);
3905 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
3908 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
3911 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3912 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3913 thermal_temp_input_show, NULL, _idxB)
3915 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
3916 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3917 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3918 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3919 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3920 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3921 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3922 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3923 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3924 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3925 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3926 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3927 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3928 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3929 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3930 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3931 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3934 #define THERMAL_ATTRS(X) \
3935 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3937 static struct attribute
*thermal_temp_input_attr
[] = {
3957 static const struct attribute_group thermal_temp_input16_group
= {
3958 .attrs
= thermal_temp_input_attr
3961 static const struct attribute_group thermal_temp_input8_group
= {
3962 .attrs
= &thermal_temp_input_attr
[8]
3965 #undef THERMAL_SENSOR_ATTR_TEMP
3966 #undef THERMAL_ATTRS
3968 /* --------------------------------------------------------------------- */
3970 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
3977 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
3979 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
3981 if (thinkpad_id
.ec_model
) {
3983 * Direct EC access mode: sensors at registers
3984 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3985 * non-implemented, thermal sensors return 0x80 when
3990 for (i
= 0; i
< 8; i
++) {
3991 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
3997 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
4005 /* This is sheer paranoia, but we handle it anyway */
4008 "ThinkPad ACPI EC access misbehaving, "
4009 "falling back to ACPI TMPx access "
4011 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
4014 "ThinkPad ACPI EC access misbehaving, "
4015 "disabling thermal sensors access\n");
4016 thermal_read_mode
= TPACPI_THERMAL_NONE
;
4021 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
4023 } else if (acpi_tmp7
) {
4024 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
4025 /* 600e/x, 770e, 770x */
4026 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
4028 /* Standard ACPI TMPx access, max 8 sensors */
4029 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
4032 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4033 thermal_read_mode
= TPACPI_THERMAL_NONE
;
4036 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
4037 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
4040 switch (thermal_read_mode
) {
4041 case TPACPI_THERMAL_TPEC_16
:
4042 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4043 &thermal_temp_input16_group
);
4047 case TPACPI_THERMAL_TPEC_8
:
4048 case TPACPI_THERMAL_ACPI_TMP07
:
4049 case TPACPI_THERMAL_ACPI_UPDT
:
4050 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4051 &thermal_temp_input8_group
);
4055 case TPACPI_THERMAL_NONE
:
4063 static void thermal_exit(void)
4065 switch (thermal_read_mode
) {
4066 case TPACPI_THERMAL_TPEC_16
:
4067 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4068 &thermal_temp_input16_group
);
4070 case TPACPI_THERMAL_TPEC_8
:
4071 case TPACPI_THERMAL_ACPI_TMP07
:
4072 case TPACPI_THERMAL_ACPI_UPDT
:
4073 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4074 &thermal_temp_input16_group
);
4076 case TPACPI_THERMAL_NONE
:
4082 static int thermal_read(char *p
)
4086 struct ibm_thermal_sensors_struct t
;
4088 n
= thermal_get_sensors(&t
);
4089 if (unlikely(n
< 0))
4092 len
+= sprintf(p
+ len
, "temperatures:\t");
4095 for (i
= 0; i
< (n
- 1); i
++)
4096 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
4097 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
4099 len
+= sprintf(p
+ len
, "not supported\n");
4104 static struct ibm_struct thermal_driver_data
= {
4106 .read
= thermal_read
,
4107 .exit
= thermal_exit
,
4110 /*************************************************************************
4114 static u8 ecdump_regs
[256];
4116 static int ecdump_read(char *p
)
4122 len
+= sprintf(p
+ len
, "EC "
4123 " +00 +01 +02 +03 +04 +05 +06 +07"
4124 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4125 for (i
= 0; i
< 256; i
+= 16) {
4126 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
4127 for (j
= 0; j
< 16; j
++) {
4128 if (!acpi_ec_read(i
+ j
, &v
))
4130 if (v
!= ecdump_regs
[i
+ j
])
4131 len
+= sprintf(p
+ len
, " *%02x", v
);
4133 len
+= sprintf(p
+ len
, " %02x", v
);
4134 ecdump_regs
[i
+ j
] = v
;
4136 len
+= sprintf(p
+ len
, "\n");
4141 /* These are way too dangerous to advertise openly... */
4143 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
4144 " (<offset> is 00-ff, <value> is 00-ff)\n");
4145 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
4146 " (<offset> is 00-ff, <value> is 0-255)\n");
4151 static int ecdump_write(char *buf
)
4156 while ((cmd
= next_cmd(&buf
))) {
4157 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
4159 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
4163 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
4164 if (!acpi_ec_write(i
, v
))
4173 static struct ibm_struct ecdump_driver_data
= {
4175 .read
= ecdump_read
,
4176 .write
= ecdump_write
,
4177 .flags
.experimental
= 1,
4180 /*************************************************************************
4181 * Backlight/brightness subdriver
4184 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4186 static struct backlight_device
*ibm_backlight_device
;
4187 static int brightness_offset
= 0x31;
4188 static int brightness_mode
;
4189 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
4191 static struct mutex brightness_mutex
;
4194 * ThinkPads can read brightness from two places: EC 0x31, or
4195 * CMOS NVRAM byte 0x5E, bits 0-3.
4197 static int brightness_get(struct backlight_device
*bd
)
4199 u8 lec
= 0, lcmos
= 0, level
= 0;
4201 if (brightness_mode
& 1) {
4202 if (!acpi_ec_read(brightness_offset
, &lec
))
4204 lec
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4207 if (brightness_mode
& 2) {
4208 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
4209 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
4210 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
4211 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4215 if (brightness_mode
== 3 && lec
!= lcmos
) {
4217 "CMOS NVRAM (%u) and EC (%u) do not agree "
4218 "on display brightness level\n",
4219 (unsigned int) lcmos
,
4220 (unsigned int) lec
);
4227 /* May return EINTR which can always be mapped to ERESTARTSYS */
4228 static int brightness_set(int value
)
4230 int cmos_cmd
, inc
, i
, res
;
4233 if (value
> ((tp_features
.bright_16levels
)? 15 : 7))
4236 res
= mutex_lock_interruptible(&brightness_mutex
);
4240 current_value
= brightness_get(NULL
);
4241 if (current_value
< 0) {
4242 res
= current_value
;
4246 cmos_cmd
= value
> current_value
?
4247 TP_CMOS_BRIGHTNESS_UP
:
4248 TP_CMOS_BRIGHTNESS_DOWN
;
4249 inc
= (value
> current_value
)? 1 : -1;
4252 for (i
= current_value
; i
!= value
; i
+= inc
) {
4253 if ((brightness_mode
& 2) &&
4254 issue_thinkpad_cmos_command(cmos_cmd
)) {
4258 if ((brightness_mode
& 1) &&
4259 !acpi_ec_write(brightness_offset
, i
+ inc
)) {
4266 mutex_unlock(&brightness_mutex
);
4270 /* sysfs backlight class ----------------------------------------------- */
4272 static int brightness_update_status(struct backlight_device
*bd
)
4274 /* it is the backlight class's job (caller) to handle
4275 * EINTR and other errors properly */
4276 return brightness_set(
4277 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
4278 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
4279 bd
->props
.brightness
: 0);
4282 static struct backlight_ops ibm_backlight_data
= {
4283 .get_brightness
= brightness_get
,
4284 .update_status
= brightness_update_status
,
4287 /* --------------------------------------------------------------------- */
4289 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
4291 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
4292 union acpi_object
*obj
;
4295 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
4296 obj
= (union acpi_object
*)buffer
.pointer
;
4297 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
4298 printk(TPACPI_ERR
"Unknown _BCL data, "
4299 "please report this to %s\n", TPACPI_MAIL
);
4302 rc
= obj
->package
.count
;
4308 kfree(buffer
.pointer
);
4312 static acpi_status __init
brightness_find_bcl(acpi_handle handle
, u32 lvl
,
4313 void *context
, void **rv
)
4315 char name
[ACPI_PATH_SEGMENT_LENGTH
];
4316 struct acpi_buffer buffer
= { sizeof(name
), &name
};
4318 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
4319 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
4320 BUG_ON(!rv
|| !*rv
);
4321 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
4322 return AE_CTRL_TERMINATE
;
4329 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
4331 static int __init
brightness_check_std_acpi_support(void)
4335 void *bcl_ptr
= &bcl_levels
;
4338 TPACPI_ACPIHANDLE_INIT(vid
);
4344 * Search for a _BCL method, and execute it. This is safe on all
4345 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
4346 * BIOS in ACPI backlight control mode. We do NOT have to care
4347 * about calling the _BCL method in an enabled video device, any
4348 * will do for our purposes.
4351 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
4352 brightness_find_bcl
, NULL
, &bcl_ptr
);
4354 if (ACPI_SUCCESS(status
) && bcl_levels
> 2)
4355 return (bcl_levels
- 2);
4360 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
4364 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
4366 mutex_init(&brightness_mutex
);
4369 * We always attempt to detect acpi support, so as to switch
4370 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4371 * going to publish a backlight interface
4373 b
= brightness_check_std_acpi_support();
4375 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
4376 printk(TPACPI_NOTICE
4377 "Lenovo BIOS switched to ACPI backlight "
4380 if (brightness_enable
> 1) {
4381 printk(TPACPI_NOTICE
4382 "standard ACPI backlight interface "
4383 "available, not loading native one...\n");
4388 if (!brightness_enable
) {
4389 dbg_printk(TPACPI_DBG_INIT
,
4390 "brightness support disabled by "
4391 "module parameter\n");
4397 "Unsupported brightness interface, "
4398 "please contact %s\n", TPACPI_MAIL
);
4402 tp_features
.bright_16levels
= 1;
4404 if (!brightness_mode
) {
4405 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
4406 brightness_mode
= 2;
4408 brightness_mode
= 3;
4410 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
4414 if (brightness_mode
> 3)
4417 b
= brightness_get(NULL
);
4421 if (tp_features
.bright_16levels
)
4423 "detected a 16-level brightness capable ThinkPad\n");
4425 ibm_backlight_device
= backlight_device_register(
4426 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
4427 &ibm_backlight_data
);
4428 if (IS_ERR(ibm_backlight_device
)) {
4429 printk(TPACPI_ERR
"Could not register backlight device\n");
4430 return PTR_ERR(ibm_backlight_device
);
4432 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
4434 ibm_backlight_device
->props
.max_brightness
=
4435 (tp_features
.bright_16levels
)? 15 : 7;
4436 ibm_backlight_device
->props
.brightness
= b
;
4437 backlight_update_status(ibm_backlight_device
);
4442 static void brightness_exit(void)
4444 if (ibm_backlight_device
) {
4445 vdbg_printk(TPACPI_DBG_EXIT
,
4446 "calling backlight_device_unregister()\n");
4447 backlight_device_unregister(ibm_backlight_device
);
4448 ibm_backlight_device
= NULL
;
4452 static int brightness_read(char *p
)
4457 level
= brightness_get(NULL
);
4459 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
4461 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
4462 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
4463 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
4464 " (<level> is 0-%d)\n",
4465 (tp_features
.bright_16levels
) ? 15 : 7);
4471 static int brightness_write(char *buf
)
4476 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
4478 level
= brightness_get(NULL
);
4482 while ((cmd
= next_cmd(&buf
))) {
4483 if (strlencmp(cmd
, "up") == 0) {
4484 if (level
< max_level
)
4486 } else if (strlencmp(cmd
, "down") == 0) {
4489 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
4490 level
>= 0 && level
<= max_level
) {
4497 * Now we know what the final level should be, so we try to set it.
4498 * Doing it this way makes the syscall restartable in case of EINTR
4500 rc
= brightness_set(level
);
4501 return (rc
== -EINTR
)? ERESTARTSYS
: rc
;
4504 static struct ibm_struct brightness_driver_data
= {
4505 .name
= "brightness",
4506 .read
= brightness_read
,
4507 .write
= brightness_write
,
4508 .exit
= brightness_exit
,
4511 /*************************************************************************
4515 static int volume_offset
= 0x30;
4517 static int volume_read(char *p
)
4522 if (!acpi_ec_read(volume_offset
, &level
)) {
4523 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
4525 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
4526 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
4527 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
4528 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
4529 " (<level> is 0-15)\n");
4535 static int volume_write(char *buf
)
4537 int cmos_cmd
, inc
, i
;
4539 int new_level
, new_mute
;
4542 while ((cmd
= next_cmd(&buf
))) {
4543 if (!acpi_ec_read(volume_offset
, &level
))
4545 new_mute
= mute
= level
& 0x40;
4546 new_level
= level
= level
& 0xf;
4548 if (strlencmp(cmd
, "up") == 0) {
4552 new_level
= level
== 15 ? 15 : level
+ 1;
4553 } else if (strlencmp(cmd
, "down") == 0) {
4557 new_level
= level
== 0 ? 0 : level
- 1;
4558 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
4559 new_level
>= 0 && new_level
<= 15) {
4561 } else if (strlencmp(cmd
, "mute") == 0) {
4566 if (new_level
!= level
) {
4567 /* mute doesn't change */
4569 cmos_cmd
= (new_level
> level
) ?
4570 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
4571 inc
= new_level
> level
? 1 : -1;
4573 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
4574 !acpi_ec_write(volume_offset
, level
)))
4577 for (i
= level
; i
!= new_level
; i
+= inc
)
4578 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
4579 !acpi_ec_write(volume_offset
, i
+ inc
))
4583 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
4584 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
4589 if (new_mute
!= mute
) {
4590 /* level doesn't change */
4592 cmos_cmd
= (new_mute
) ?
4593 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
4595 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
4596 !acpi_ec_write(volume_offset
, level
+ new_mute
))
4604 static struct ibm_struct volume_driver_data
= {
4606 .read
= volume_read
,
4607 .write
= volume_write
,
4610 /*************************************************************************
4617 * TPACPI_FAN_RD_ACPI_GFAN:
4618 * ACPI GFAN method: returns fan level
4620 * see TPACPI_FAN_WR_ACPI_SFAN
4621 * EC 0x2f (HFSP) not available if GFAN exists
4623 * TPACPI_FAN_WR_ACPI_SFAN:
4624 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4626 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4629 * TPACPI_FAN_WR_TPEC:
4630 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4631 * Supported on almost all ThinkPads
4633 * Fan speed changes of any sort (including those caused by the
4634 * disengaged mode) are usually done slowly by the firmware as the
4635 * maximum ammount of fan duty cycle change per second seems to be
4638 * Reading is not available if GFAN exists.
4639 * Writing is not available if SFAN exists.
4642 * 7 automatic mode engaged;
4643 * (default operation mode of the ThinkPad)
4644 * fan level is ignored in this mode.
4645 * 6 full speed mode (takes precedence over bit 7);
4646 * not available on all thinkpads. May disable
4647 * the tachometer while the fan controller ramps up
4648 * the speed (which can take up to a few *minutes*).
4649 * Speeds up fan to 100% duty-cycle, which is far above
4650 * the standard RPM levels. It is not impossible that
4651 * it could cause hardware damage.
4652 * 5-3 unused in some models. Extra bits for fan level
4653 * in others, but still useless as all values above
4654 * 7 map to the same speed as level 7 in these models.
4655 * 2-0 fan level (0..7 usually)
4657 * 0x07 = max (set when temperatures critical)
4658 * Some ThinkPads may have other levels, see
4659 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4661 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4662 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4663 * does so, its initial value is meaningless (0x07).
4665 * For firmware bugs, refer to:
4666 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4670 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4671 * Main fan tachometer reading (in RPM)
4673 * This register is present on all ThinkPads with a new-style EC, and
4674 * it is known not to be present on the A21m/e, and T22, as there is
4675 * something else in offset 0x84 according to the ACPI DSDT. Other
4676 * ThinkPads from this same time period (and earlier) probably lack the
4677 * tachometer as well.
4679 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4680 * was never fixed by IBM to report the EC firmware version string
4681 * probably support the tachometer (like the early X models), so
4682 * detecting it is quite hard. We need more data to know for sure.
4684 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4687 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4690 * For firmware bugs, refer to:
4691 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4693 * TPACPI_FAN_WR_ACPI_FANS:
4694 * ThinkPad X31, X40, X41. Not available in the X60.
4696 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4697 * high speed. ACPI DSDT seems to map these three speeds to levels
4698 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4699 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4701 * The speeds are stored on handles
4702 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4704 * There are three default speed sets, acessible as handles:
4705 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4707 * ACPI DSDT switches which set is in use depending on various
4710 * TPACPI_FAN_WR_TPEC is also available and should be used to
4711 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4712 * but the ACPI tables just mention level 7.
4715 enum { /* Fan control constants */
4716 fan_status_offset
= 0x2f, /* EC register 0x2f */
4717 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4718 * 0x84 must be read before 0x85 */
4720 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
4721 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
4723 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
4726 enum fan_status_access_mode
{
4727 TPACPI_FAN_NONE
= 0, /* No fan status or control */
4728 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
4729 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4732 enum fan_control_access_mode
{
4733 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
4734 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
4735 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
4736 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
4739 enum fan_control_commands
{
4740 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
4741 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
4742 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
4743 * and also watchdog cmd */
4746 static int fan_control_allowed
;
4748 static enum fan_status_access_mode fan_status_access_mode
;
4749 static enum fan_control_access_mode fan_control_access_mode
;
4750 static enum fan_control_commands fan_control_commands
;
4752 static u8 fan_control_initial_status
;
4753 static u8 fan_control_desired_level
;
4754 static int fan_watchdog_maxinterval
;
4756 static struct mutex fan_mutex
;
4758 static void fan_watchdog_fire(struct work_struct
*ignored
);
4759 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
4761 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
4762 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
4763 "\\FSPD", /* 600e/x, 770e, 770x */
4765 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
4766 "JFNS", /* 770x-JL */
4770 * Call with fan_mutex held
4772 static void fan_update_desired_level(u8 status
)
4775 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
4777 fan_control_desired_level
= 7;
4779 fan_control_desired_level
= status
;
4783 static int fan_get_status(u8
*status
)
4788 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4790 switch (fan_status_access_mode
) {
4791 case TPACPI_FAN_RD_ACPI_GFAN
:
4792 /* 570, 600e/x, 770e, 770x */
4794 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
4802 case TPACPI_FAN_RD_TPEC
:
4803 /* all except 570, 600e/x, 770e, 770x */
4804 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
4819 static int fan_get_status_safe(u8
*status
)
4824 if (mutex_lock_interruptible(&fan_mutex
))
4825 return -ERESTARTSYS
;
4826 rc
= fan_get_status(&s
);
4828 fan_update_desired_level(s
);
4829 mutex_unlock(&fan_mutex
);
4837 static int fan_get_speed(unsigned int *speed
)
4841 switch (fan_status_access_mode
) {
4842 case TPACPI_FAN_RD_TPEC
:
4843 /* all except 570, 600e/x, 770e, 770x */
4844 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
4845 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
4849 *speed
= (hi
<< 8) | lo
;
4860 static int fan_set_level(int level
)
4862 if (!fan_control_allowed
)
4865 switch (fan_control_access_mode
) {
4866 case TPACPI_FAN_WR_ACPI_SFAN
:
4867 if (level
>= 0 && level
<= 7) {
4868 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
4874 case TPACPI_FAN_WR_ACPI_FANS
:
4875 case TPACPI_FAN_WR_TPEC
:
4876 if ((level
!= TP_EC_FAN_AUTO
) &&
4877 (level
!= TP_EC_FAN_FULLSPEED
) &&
4878 ((level
< 0) || (level
> 7)))
4881 /* safety net should the EC not support AUTO
4882 * or FULLSPEED mode bits and just ignore them */
4883 if (level
& TP_EC_FAN_FULLSPEED
)
4884 level
|= 7; /* safety min speed 7 */
4885 else if (level
& TP_EC_FAN_AUTO
)
4886 level
|= 4; /* safety min speed 4 */
4888 if (!acpi_ec_write(fan_status_offset
, level
))
4891 tp_features
.fan_ctrl_status_undef
= 0;
4900 static int fan_set_level_safe(int level
)
4904 if (!fan_control_allowed
)
4907 if (mutex_lock_interruptible(&fan_mutex
))
4908 return -ERESTARTSYS
;
4910 if (level
== TPACPI_FAN_LAST_LEVEL
)
4911 level
= fan_control_desired_level
;
4913 rc
= fan_set_level(level
);
4915 fan_update_desired_level(level
);
4917 mutex_unlock(&fan_mutex
);
4921 static int fan_set_enable(void)
4926 if (!fan_control_allowed
)
4929 if (mutex_lock_interruptible(&fan_mutex
))
4930 return -ERESTARTSYS
;
4932 switch (fan_control_access_mode
) {
4933 case TPACPI_FAN_WR_ACPI_FANS
:
4934 case TPACPI_FAN_WR_TPEC
:
4935 rc
= fan_get_status(&s
);
4939 /* Don't go out of emergency fan mode */
4942 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
4945 if (!acpi_ec_write(fan_status_offset
, s
))
4948 tp_features
.fan_ctrl_status_undef
= 0;
4953 case TPACPI_FAN_WR_ACPI_SFAN
:
4954 rc
= fan_get_status(&s
);
4960 /* Set fan to at least level 4 */
4963 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
4973 mutex_unlock(&fan_mutex
);
4977 static int fan_set_disable(void)
4981 if (!fan_control_allowed
)
4984 if (mutex_lock_interruptible(&fan_mutex
))
4985 return -ERESTARTSYS
;
4988 switch (fan_control_access_mode
) {
4989 case TPACPI_FAN_WR_ACPI_FANS
:
4990 case TPACPI_FAN_WR_TPEC
:
4991 if (!acpi_ec_write(fan_status_offset
, 0x00))
4994 fan_control_desired_level
= 0;
4995 tp_features
.fan_ctrl_status_undef
= 0;
4999 case TPACPI_FAN_WR_ACPI_SFAN
:
5000 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
5003 fan_control_desired_level
= 0;
5011 mutex_unlock(&fan_mutex
);
5015 static int fan_set_speed(int speed
)
5019 if (!fan_control_allowed
)
5022 if (mutex_lock_interruptible(&fan_mutex
))
5023 return -ERESTARTSYS
;
5026 switch (fan_control_access_mode
) {
5027 case TPACPI_FAN_WR_ACPI_FANS
:
5028 if (speed
>= 0 && speed
<= 65535) {
5029 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
5030 speed
, speed
, speed
))
5040 mutex_unlock(&fan_mutex
);
5044 static void fan_watchdog_reset(void)
5046 static int fan_watchdog_active
;
5048 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
5051 if (fan_watchdog_active
)
5052 cancel_delayed_work(&fan_watchdog_task
);
5054 if (fan_watchdog_maxinterval
> 0 &&
5055 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
5056 fan_watchdog_active
= 1;
5057 if (!schedule_delayed_work(&fan_watchdog_task
,
5058 msecs_to_jiffies(fan_watchdog_maxinterval
5061 "failed to schedule the fan watchdog, "
5062 "watchdog will not trigger\n");
5065 fan_watchdog_active
= 0;
5068 static void fan_watchdog_fire(struct work_struct
*ignored
)
5072 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
5075 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
5076 rc
= fan_set_enable();
5078 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
5079 "will try again later...\n", -rc
);
5080 /* reschedule for later */
5081 fan_watchdog_reset();
5086 * SYSFS fan layout: hwmon compatible (device)
5089 * 0: "disengaged" mode
5091 * 2: native EC "auto" mode (recommended, hardware default)
5093 * pwm*: set speed in manual mode, ignored otherwise.
5094 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5097 * fan*_input: tachometer reading, RPM
5100 * SYSFS fan layout: extensions
5102 * fan_watchdog (driver):
5103 * fan watchdog interval in seconds, 0 disables (default), max 120
5106 /* sysfs fan pwm1_enable ----------------------------------------------- */
5107 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
5108 struct device_attribute
*attr
,
5114 res
= fan_get_status_safe(&status
);
5118 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5119 if (status
!= fan_control_initial_status
) {
5120 tp_features
.fan_ctrl_status_undef
= 0;
5122 /* Return most likely status. In fact, it
5123 * might be the only possible status */
5124 status
= TP_EC_FAN_AUTO
;
5128 if (status
& TP_EC_FAN_FULLSPEED
) {
5130 } else if (status
& TP_EC_FAN_AUTO
) {
5135 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
5138 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
5139 struct device_attribute
*attr
,
5140 const char *buf
, size_t count
)
5145 if (parse_strtoul(buf
, 2, &t
))
5150 level
= TP_EC_FAN_FULLSPEED
;
5153 level
= TPACPI_FAN_LAST_LEVEL
;
5156 level
= TP_EC_FAN_AUTO
;
5159 /* reserved for software-controlled auto mode */
5165 res
= fan_set_level_safe(level
);
5171 fan_watchdog_reset();
5176 static struct device_attribute dev_attr_fan_pwm1_enable
=
5177 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
5178 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
5180 /* sysfs fan pwm1 ------------------------------------------------------ */
5181 static ssize_t
fan_pwm1_show(struct device
*dev
,
5182 struct device_attribute
*attr
,
5188 res
= fan_get_status_safe(&status
);
5192 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5193 if (status
!= fan_control_initial_status
) {
5194 tp_features
.fan_ctrl_status_undef
= 0;
5196 status
= TP_EC_FAN_AUTO
;
5201 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
5202 status
= fan_control_desired_level
;
5207 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
5210 static ssize_t
fan_pwm1_store(struct device
*dev
,
5211 struct device_attribute
*attr
,
5212 const char *buf
, size_t count
)
5216 u8 status
, newlevel
;
5218 if (parse_strtoul(buf
, 255, &s
))
5221 /* scale down from 0-255 to 0-7 */
5222 newlevel
= (s
>> 5) & 0x07;
5224 if (mutex_lock_interruptible(&fan_mutex
))
5225 return -ERESTARTSYS
;
5227 rc
= fan_get_status(&status
);
5228 if (!rc
&& (status
&
5229 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
5230 rc
= fan_set_level(newlevel
);
5234 fan_update_desired_level(newlevel
);
5235 fan_watchdog_reset();
5239 mutex_unlock(&fan_mutex
);
5240 return (rc
)? rc
: count
;
5243 static struct device_attribute dev_attr_fan_pwm1
=
5244 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
5245 fan_pwm1_show
, fan_pwm1_store
);
5247 /* sysfs fan fan1_input ------------------------------------------------ */
5248 static ssize_t
fan_fan1_input_show(struct device
*dev
,
5249 struct device_attribute
*attr
,
5255 res
= fan_get_speed(&speed
);
5259 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
5262 static struct device_attribute dev_attr_fan_fan1_input
=
5263 __ATTR(fan1_input
, S_IRUGO
,
5264 fan_fan1_input_show
, NULL
);
5266 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5267 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
5270 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
5273 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
5274 const char *buf
, size_t count
)
5278 if (parse_strtoul(buf
, 120, &t
))
5281 if (!fan_control_allowed
)
5284 fan_watchdog_maxinterval
= t
;
5285 fan_watchdog_reset();
5290 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
5291 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
5293 /* --------------------------------------------------------------------- */
5294 static struct attribute
*fan_attributes
[] = {
5295 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
5296 &dev_attr_fan_fan1_input
.attr
,
5300 static const struct attribute_group fan_attr_group
= {
5301 .attrs
= fan_attributes
,
5304 static int __init
fan_init(struct ibm_init_struct
*iibm
)
5308 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
5310 mutex_init(&fan_mutex
);
5311 fan_status_access_mode
= TPACPI_FAN_NONE
;
5312 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5313 fan_control_commands
= 0;
5314 fan_watchdog_maxinterval
= 0;
5315 tp_features
.fan_ctrl_status_undef
= 0;
5316 fan_control_desired_level
= 7;
5318 TPACPI_ACPIHANDLE_INIT(fans
);
5319 TPACPI_ACPIHANDLE_INIT(gfan
);
5320 TPACPI_ACPIHANDLE_INIT(sfan
);
5323 /* 570, 600e/x, 770e, 770x */
5324 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
5326 /* all other ThinkPads: note that even old-style
5327 * ThinkPad ECs supports the fan control register */
5328 if (likely(acpi_ec_read(fan_status_offset
,
5329 &fan_control_initial_status
))) {
5330 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
5332 /* In some ThinkPads, neither the EC nor the ACPI
5333 * DSDT initialize the fan status, and it ends up
5334 * being set to 0x07 when it *could* be either
5337 * Enable for TP-1Y (T43), TP-78 (R51e),
5338 * TP-76 (R52), TP-70 (T43, R52), which are known
5340 if (fan_control_initial_status
== 0x07) {
5341 switch (thinkpad_id
.ec_model
) {
5342 case 0x5931: /* TP-1Y */
5343 case 0x3837: /* TP-78 */
5344 case 0x3637: /* TP-76 */
5345 case 0x3037: /* TP-70 */
5346 printk(TPACPI_NOTICE
5347 "fan_init: initial fan status "
5348 "is unknown, assuming it is "
5350 tp_features
.fan_ctrl_status_undef
= 1;
5356 "ThinkPad ACPI EC access misbehaving, "
5357 "fan status and control unavailable\n");
5364 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
5365 fan_control_commands
|=
5366 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
5369 /* gfan without sfan means no fan control */
5370 /* all other models implement TP EC 0x2f control */
5374 fan_control_access_mode
=
5375 TPACPI_FAN_WR_ACPI_FANS
;
5376 fan_control_commands
|=
5377 TPACPI_FAN_CMD_SPEED
|
5378 TPACPI_FAN_CMD_LEVEL
|
5379 TPACPI_FAN_CMD_ENABLE
;
5381 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
5382 fan_control_commands
|=
5383 TPACPI_FAN_CMD_LEVEL
|
5384 TPACPI_FAN_CMD_ENABLE
;
5389 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
5390 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
5391 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
5392 fan_status_access_mode
, fan_control_access_mode
);
5394 /* fan control master switch */
5395 if (!fan_control_allowed
) {
5396 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5397 fan_control_commands
= 0;
5398 dbg_printk(TPACPI_DBG_INIT
,
5399 "fan control features disabled by parameter\n");
5402 /* update fan_control_desired_level */
5403 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
5404 fan_get_status_safe(NULL
);
5406 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
5407 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
5408 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5411 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
5412 &driver_attr_fan_watchdog
);
5420 static void fan_exit(void)
5422 vdbg_printk(TPACPI_DBG_EXIT
,
5423 "cancelling any pending fan watchdog tasks\n");
5425 /* FIXME: can we really do this unconditionally? */
5426 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
5427 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
5428 &driver_attr_fan_watchdog
);
5430 cancel_delayed_work(&fan_watchdog_task
);
5431 flush_scheduled_work();
5434 static int fan_read(char *p
)
5439 unsigned int speed
= 0;
5441 switch (fan_status_access_mode
) {
5442 case TPACPI_FAN_RD_ACPI_GFAN
:
5443 /* 570, 600e/x, 770e, 770x */
5444 rc
= fan_get_status_safe(&status
);
5448 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
5450 (status
!= 0) ? "enabled" : "disabled", status
);
5453 case TPACPI_FAN_RD_TPEC
:
5454 /* all except 570, 600e/x, 770e, 770x */
5455 rc
= fan_get_status_safe(&status
);
5459 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5460 if (status
!= fan_control_initial_status
)
5461 tp_features
.fan_ctrl_status_undef
= 0;
5463 /* Return most likely status. In fact, it
5464 * might be the only possible status */
5465 status
= TP_EC_FAN_AUTO
;
5468 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
5469 (status
!= 0) ? "enabled" : "disabled");
5471 rc
= fan_get_speed(&speed
);
5475 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
5477 if (status
& TP_EC_FAN_FULLSPEED
)
5478 /* Disengaged mode takes precedence */
5479 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
5480 else if (status
& TP_EC_FAN_AUTO
)
5481 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
5483 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
5486 case TPACPI_FAN_NONE
:
5488 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5491 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
5492 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
5494 switch (fan_control_access_mode
) {
5495 case TPACPI_FAN_WR_ACPI_SFAN
:
5496 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
5500 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
5501 "auto, disengaged, full-speed)\n");
5506 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
5507 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
5508 "commands:\twatchdog <timeout> (<timeout> "
5509 "is 0 (off), 1-120 (seconds))\n");
5511 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
5512 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
5513 " (<speed> is 0-65535)\n");
5518 static int fan_write_cmd_level(const char *cmd
, int *rc
)
5522 if (strlencmp(cmd
, "level auto") == 0)
5523 level
= TP_EC_FAN_AUTO
;
5524 else if ((strlencmp(cmd
, "level disengaged") == 0) |
5525 (strlencmp(cmd
, "level full-speed") == 0))
5526 level
= TP_EC_FAN_FULLSPEED
;
5527 else if (sscanf(cmd
, "level %d", &level
) != 1)
5530 *rc
= fan_set_level_safe(level
);
5532 printk(TPACPI_ERR
"level command accepted for unsupported "
5533 "access mode %d", fan_control_access_mode
);
5538 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
5540 if (strlencmp(cmd
, "enable") != 0)
5543 *rc
= fan_set_enable();
5545 printk(TPACPI_ERR
"enable command accepted for unsupported "
5546 "access mode %d", fan_control_access_mode
);
5551 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
5553 if (strlencmp(cmd
, "disable") != 0)
5556 *rc
= fan_set_disable();
5558 printk(TPACPI_ERR
"disable command accepted for unsupported "
5559 "access mode %d", fan_control_access_mode
);
5564 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
5569 * Support speed <low> <medium> <high> ? */
5571 if (sscanf(cmd
, "speed %d", &speed
) != 1)
5574 *rc
= fan_set_speed(speed
);
5576 printk(TPACPI_ERR
"speed command accepted for unsupported "
5577 "access mode %d", fan_control_access_mode
);
5582 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
5586 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
5589 if (interval
< 0 || interval
> 120)
5592 fan_watchdog_maxinterval
= interval
;
5597 static int fan_write(char *buf
)
5602 while (!rc
&& (cmd
= next_cmd(&buf
))) {
5603 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
5604 fan_write_cmd_level(cmd
, &rc
)) &&
5605 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
5606 (fan_write_cmd_enable(cmd
, &rc
) ||
5607 fan_write_cmd_disable(cmd
, &rc
) ||
5608 fan_write_cmd_watchdog(cmd
, &rc
))) &&
5609 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
5610 fan_write_cmd_speed(cmd
, &rc
))
5614 fan_watchdog_reset();
5620 static struct ibm_struct fan_driver_data
= {
5627 /****************************************************************************
5628 ****************************************************************************
5632 ****************************************************************************
5633 ****************************************************************************/
5635 /* sysfs name ---------------------------------------------------------- */
5636 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
5637 struct device_attribute
*attr
,
5640 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
5643 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
5644 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
5646 /* --------------------------------------------------------------------- */
5649 static struct proc_dir_entry
*proc_dir
;
5652 * Module and infrastructure proble, init and exit handling
5655 static int force_load
;
5657 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5658 static const char * __init
str_supported(int is_supported
)
5660 static char text_unsupported
[] __initdata
= "not supported";
5662 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
5664 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5666 static void ibm_exit(struct ibm_struct
*ibm
)
5668 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
5670 list_del_init(&ibm
->all_drivers
);
5672 if (ibm
->flags
.acpi_notify_installed
) {
5673 dbg_printk(TPACPI_DBG_EXIT
,
5674 "%s: acpi_remove_notify_handler\n", ibm
->name
);
5676 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
5678 dispatch_acpi_notify
);
5679 ibm
->flags
.acpi_notify_installed
= 0;
5680 ibm
->flags
.acpi_notify_installed
= 0;
5683 if (ibm
->flags
.proc_created
) {
5684 dbg_printk(TPACPI_DBG_EXIT
,
5685 "%s: remove_proc_entry\n", ibm
->name
);
5686 remove_proc_entry(ibm
->name
, proc_dir
);
5687 ibm
->flags
.proc_created
= 0;
5690 if (ibm
->flags
.acpi_driver_registered
) {
5691 dbg_printk(TPACPI_DBG_EXIT
,
5692 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
5694 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
5695 kfree(ibm
->acpi
->driver
);
5696 ibm
->acpi
->driver
= NULL
;
5697 ibm
->flags
.acpi_driver_registered
= 0;
5700 if (ibm
->flags
.init_called
&& ibm
->exit
) {
5702 ibm
->flags
.init_called
= 0;
5705 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
5708 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
5711 struct ibm_struct
*ibm
= iibm
->data
;
5712 struct proc_dir_entry
*entry
;
5714 BUG_ON(ibm
== NULL
);
5716 INIT_LIST_HEAD(&ibm
->all_drivers
);
5718 if (ibm
->flags
.experimental
&& !experimental
)
5721 dbg_printk(TPACPI_DBG_INIT
,
5722 "probing for %s\n", ibm
->name
);
5725 ret
= iibm
->init(iibm
);
5727 return 0; /* probe failed */
5731 ibm
->flags
.init_called
= 1;
5735 if (ibm
->acpi
->hid
) {
5736 ret
= register_tpacpi_subdriver(ibm
);
5741 if (ibm
->acpi
->notify
) {
5742 ret
= setup_acpi_notify(ibm
);
5743 if (ret
== -ENODEV
) {
5744 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
5754 dbg_printk(TPACPI_DBG_INIT
,
5755 "%s installed\n", ibm
->name
);
5758 entry
= create_proc_entry(ibm
->name
,
5759 S_IFREG
| S_IRUGO
| S_IWUSR
,
5762 printk(TPACPI_ERR
"unable to create proc entry %s\n",
5767 entry
->owner
= THIS_MODULE
;
5769 entry
->read_proc
= &dispatch_procfs_read
;
5771 entry
->write_proc
= &dispatch_procfs_write
;
5772 ibm
->flags
.proc_created
= 1;
5775 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
5780 dbg_printk(TPACPI_DBG_INIT
,
5781 "%s: at error exit path with result %d\n",
5785 return (ret
< 0)? ret
: 0;
5790 static void __init
get_thinkpad_model_data(struct thinkpad_id_data
*tp
)
5792 struct dmi_device
*dev
= NULL
;
5793 char ec_fw_string
[18];
5798 memset(tp
, 0, sizeof(*tp
));
5800 if (dmi_name_in_vendors("IBM"))
5801 tp
->vendor
= PCI_VENDOR_ID_IBM
;
5802 else if (dmi_name_in_vendors("LENOVO"))
5803 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
5807 tp
->bios_version_str
= kstrdup(dmi_get_system_info(DMI_BIOS_VERSION
),
5809 if (!tp
->bios_version_str
)
5811 tp
->bios_model
= tp
->bios_version_str
[0]
5812 | (tp
->bios_version_str
[1] << 8);
5815 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5816 * X32 or newer, all Z series; Some models must have an
5817 * up-to-date BIOS or they will not be detected.
5819 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5821 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
5822 if (sscanf(dev
->name
,
5823 "IBM ThinkPad Embedded Controller -[%17c",
5824 ec_fw_string
) == 1) {
5825 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
5826 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
5828 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
5829 tp
->ec_model
= ec_fw_string
[0]
5830 | (ec_fw_string
[1] << 8);
5835 tp
->model_str
= kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION
),
5837 if (strnicmp(tp
->model_str
, "ThinkPad", 8) != 0) {
5838 kfree(tp
->model_str
);
5839 tp
->model_str
= NULL
;
5843 static int __init
probe_for_thinkpad(void)
5851 * Non-ancient models have better DMI tagging, but very old models
5854 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
5856 /* ec is required because many other handles are relative to it */
5857 TPACPI_ACPIHANDLE_INIT(ec
);
5861 "Not yet supported ThinkPad detected!\n");
5866 * Risks a regression on very old machines, but reduces potential
5867 * false positives a damn great deal
5870 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
5872 if (!is_thinkpad
&& !force_load
)
5879 /* Module init, exit, parameters */
5881 static struct ibm_init_struct ibms_init
[] __initdata
= {
5883 .init
= thinkpad_acpi_driver_init
,
5884 .data
= &thinkpad_acpi_driver_data
,
5887 .init
= hotkey_init
,
5888 .data
= &hotkey_driver_data
,
5891 .init
= bluetooth_init
,
5892 .data
= &bluetooth_driver_data
,
5896 .data
= &wan_driver_data
,
5898 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5901 .data
= &video_driver_data
,
5906 .data
= &light_driver_data
,
5908 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5911 .data
= &dock_driver_data
[0],
5915 .data
= &dock_driver_data
[1],
5918 #ifdef CONFIG_THINKPAD_ACPI_BAY
5921 .data
= &bay_driver_data
,
5926 .data
= &cmos_driver_data
,
5930 .data
= &led_driver_data
,
5934 .data
= &beep_driver_data
,
5937 .init
= thermal_init
,
5938 .data
= &thermal_driver_data
,
5941 .data
= &ecdump_driver_data
,
5944 .init
= brightness_init
,
5945 .data
= &brightness_driver_data
,
5948 .data
= &volume_driver_data
,
5952 .data
= &fan_driver_data
,
5956 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
5959 struct ibm_struct
*ibm
;
5961 if (!kp
|| !kp
->name
|| !val
)
5964 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
5965 ibm
= ibms_init
[i
].data
;
5966 WARN_ON(ibm
== NULL
);
5968 if (!ibm
|| !ibm
->name
)
5971 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
5972 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
5974 strcpy(ibms_init
[i
].param
, val
);
5975 strcat(ibms_init
[i
].param
, ",");
5983 module_param(experimental
, int, 0);
5984 MODULE_PARM_DESC(experimental
,
5985 "Enables experimental features when non-zero");
5987 module_param_named(debug
, dbg_level
, uint
, 0);
5988 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
5990 module_param(force_load
, bool, 0);
5991 MODULE_PARM_DESC(force_load
,
5992 "Attempts to load the driver even on a "
5993 "mis-identified ThinkPad when true");
5995 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
5996 MODULE_PARM_DESC(fan_control
,
5997 "Enables setting fan parameters features when true");
5999 module_param_named(brightness_mode
, brightness_mode
, int, 0);
6000 MODULE_PARM_DESC(brightness_mode
,
6001 "Selects brightness control strategy: "
6002 "0=auto, 1=EC, 2=CMOS, 3=both");
6004 module_param(brightness_enable
, uint
, 0);
6005 MODULE_PARM_DESC(brightness_enable
,
6006 "Enables backlight control when 1, disables when 0");
6008 module_param(hotkey_report_mode
, uint
, 0);
6009 MODULE_PARM_DESC(hotkey_report_mode
,
6010 "used for backwards compatibility with userspace, "
6011 "see documentation");
6013 #define TPACPI_PARAM(feature) \
6014 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6015 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
6016 "at module load, see documentation")
6018 TPACPI_PARAM(hotkey
);
6019 TPACPI_PARAM(bluetooth
);
6020 TPACPI_PARAM(video
);
6021 TPACPI_PARAM(light
);
6022 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6025 #ifdef CONFIG_THINKPAD_ACPI_BAY
6027 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6031 TPACPI_PARAM(ecdump
);
6032 TPACPI_PARAM(brightness
);
6033 TPACPI_PARAM(volume
);
6036 static void thinkpad_acpi_module_exit(void)
6038 struct ibm_struct
*ibm
, *itmp
;
6040 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
6042 list_for_each_entry_safe_reverse(ibm
, itmp
,
6043 &tpacpi_all_drivers
,
6048 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
6050 if (tpacpi_inputdev
) {
6051 if (tp_features
.input_device_registered
)
6052 input_unregister_device(tpacpi_inputdev
);
6054 input_free_device(tpacpi_inputdev
);
6058 hwmon_device_unregister(tpacpi_hwmon
);
6060 if (tp_features
.sensors_pdev_attrs_registered
)
6061 device_remove_file(&tpacpi_sensors_pdev
->dev
,
6062 &dev_attr_thinkpad_acpi_pdev_name
);
6063 if (tpacpi_sensors_pdev
)
6064 platform_device_unregister(tpacpi_sensors_pdev
);
6066 platform_device_unregister(tpacpi_pdev
);
6068 if (tp_features
.sensors_pdrv_attrs_registered
)
6069 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
6070 if (tp_features
.platform_drv_attrs_registered
)
6071 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
6073 if (tp_features
.sensors_pdrv_registered
)
6074 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
6076 if (tp_features
.platform_drv_registered
)
6077 platform_driver_unregister(&tpacpi_pdriver
);
6080 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
6082 kfree(thinkpad_id
.bios_version_str
);
6083 kfree(thinkpad_id
.ec_version_str
);
6084 kfree(thinkpad_id
.model_str
);
6088 static int __init
thinkpad_acpi_module_init(void)
6092 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
6094 /* Parameter checking */
6095 if (hotkey_report_mode
> 2)
6098 /* Driver-level probe */
6100 get_thinkpad_model_data(&thinkpad_id
);
6101 ret
= probe_for_thinkpad();
6103 thinkpad_acpi_module_exit();
6107 /* Driver initialization */
6109 TPACPI_ACPIHANDLE_INIT(ecrd
);
6110 TPACPI_ACPIHANDLE_INIT(ecwr
);
6112 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
6115 "unable to create proc dir " TPACPI_PROC_DIR
);
6116 thinkpad_acpi_module_exit();
6119 proc_dir
->owner
= THIS_MODULE
;
6121 ret
= platform_driver_register(&tpacpi_pdriver
);
6124 "unable to register main platform driver\n");
6125 thinkpad_acpi_module_exit();
6128 tp_features
.platform_drv_registered
= 1;
6130 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
6133 "unable to register hwmon platform driver\n");
6134 thinkpad_acpi_module_exit();
6137 tp_features
.sensors_pdrv_registered
= 1;
6139 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
6141 tp_features
.platform_drv_attrs_registered
= 1;
6142 ret
= tpacpi_create_driver_attributes(
6143 &tpacpi_hwmon_pdriver
.driver
);
6147 "unable to create sysfs driver attributes\n");
6148 thinkpad_acpi_module_exit();
6151 tp_features
.sensors_pdrv_attrs_registered
= 1;
6154 /* Device initialization */
6155 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
6157 if (IS_ERR(tpacpi_pdev
)) {
6158 ret
= PTR_ERR(tpacpi_pdev
);
6160 printk(TPACPI_ERR
"unable to register platform device\n");
6161 thinkpad_acpi_module_exit();
6164 tpacpi_sensors_pdev
= platform_device_register_simple(
6165 TPACPI_HWMON_DRVR_NAME
,
6167 if (IS_ERR(tpacpi_sensors_pdev
)) {
6168 ret
= PTR_ERR(tpacpi_sensors_pdev
);
6169 tpacpi_sensors_pdev
= NULL
;
6171 "unable to register hwmon platform device\n");
6172 thinkpad_acpi_module_exit();
6175 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
6176 &dev_attr_thinkpad_acpi_pdev_name
);
6179 "unable to create sysfs hwmon device attributes\n");
6180 thinkpad_acpi_module_exit();
6183 tp_features
.sensors_pdev_attrs_registered
= 1;
6184 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
6185 if (IS_ERR(tpacpi_hwmon
)) {
6186 ret
= PTR_ERR(tpacpi_hwmon
);
6187 tpacpi_hwmon
= NULL
;
6188 printk(TPACPI_ERR
"unable to register hwmon device\n");
6189 thinkpad_acpi_module_exit();
6192 mutex_init(&tpacpi_inputdev_send_mutex
);
6193 tpacpi_inputdev
= input_allocate_device();
6194 if (!tpacpi_inputdev
) {
6195 printk(TPACPI_ERR
"unable to allocate input device\n");
6196 thinkpad_acpi_module_exit();
6199 /* Prepare input device, but don't register */
6200 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
6201 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
6202 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
6203 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
6204 thinkpad_id
.vendor
:
6206 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
6207 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
6209 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
6210 ret
= ibm_init(&ibms_init
[i
]);
6211 if (ret
>= 0 && *ibms_init
[i
].param
)
6212 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
6214 thinkpad_acpi_module_exit();
6218 ret
= input_register_device(tpacpi_inputdev
);
6220 printk(TPACPI_ERR
"unable to register input device\n");
6221 thinkpad_acpi_module_exit();
6224 tp_features
.input_device_registered
= 1;
6227 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
6231 /* Please remove this in year 2009 */
6232 MODULE_ALIAS("ibm_acpi");
6235 * DMI matching for module autoloading
6237 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6238 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6240 * Only models listed in thinkwiki will be supported, so add yours
6241 * if it is not there yet.
6243 #define IBM_BIOS_MODULE_ALIAS(__type) \
6244 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6246 /* Non-ancient thinkpads */
6247 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6248 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6250 /* Ancient thinkpad BIOSes have to be identified by
6251 * BIOS type or model number, and there are far less
6252 * BIOS types than model numbers... */
6253 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6254 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6255 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6257 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6258 MODULE_DESCRIPTION(TPACPI_DESC
);
6259 MODULE_VERSION(TPACPI_VERSION
);
6260 MODULE_LICENSE("GPL");
6262 module_init(thinkpad_acpi_module_init
);
6263 module_exit(thinkpad_acpi_module_exit
);