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"
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
{
175 const struct acpi_device_id
*hid
;
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
226 u32 bright_16levels
:1;
227 u32 bright_acpimode
:1;
229 u32 fan_ctrl_status_undef
:1;
230 u32 input_device_registered
:1;
231 u32 platform_drv_registered
:1;
232 u32 platform_drv_attrs_registered
:1;
233 u32 sensors_pdrv_registered
:1;
234 u32 sensors_pdrv_attrs_registered
:1;
235 u32 sensors_pdev_attrs_registered
:1;
236 u32 hotkey_poll_active
:1;
239 struct thinkpad_id_data
{
240 unsigned int vendor
; /* ThinkPad vendor:
241 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
243 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
244 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
246 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
251 static struct thinkpad_id_data thinkpad_id
;
254 TPACPI_LIFE_INIT
= 0,
259 static int experimental
;
260 static u32 dbg_level
;
262 /****************************************************************************
263 ****************************************************************************
265 * ACPI Helpers and device model
267 ****************************************************************************
268 ****************************************************************************/
270 /*************************************************************************
274 static acpi_handle root_handle
;
276 #define TPACPI_HANDLE(object, parent, paths...) \
277 static acpi_handle object##_handle; \
278 static acpi_handle *object##_parent = &parent##_handle; \
279 static char *object##_path; \
280 static char *object##_paths[] = { paths }
282 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
283 "\\_SB.PCI.ISA.EC", /* 570 */
284 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
285 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
286 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
287 "\\_SB.PCI0.ICH3.EC0", /* R31 */
288 "\\_SB.PCI0.LPC.EC", /* all others */
291 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
292 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
294 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
296 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
297 "\\CMS", /* R40, R40e */
300 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
301 "^HKEY", /* R30, R31 */
302 "HKEY", /* all others */
305 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
306 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
307 "\\_SB.PCI0.VID0", /* 770e */
308 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
309 "\\_SB.PCI0.AGP.VID", /* all others */
313 /*************************************************************************
317 static int acpi_evalf(acpi_handle handle
,
318 void *res
, char *method
, char *fmt
, ...)
321 struct acpi_object_list params
;
322 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
323 struct acpi_buffer result
, *resultp
;
324 union acpi_object out_obj
;
332 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
345 params
.pointer
= &in_objs
[0];
352 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
353 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
355 /* add more types as needed */
357 printk(TPACPI_ERR
"acpi_evalf() called "
358 "with invalid format character '%c'\n", c
);
364 if (res_type
!= 'v') {
365 result
.length
= sizeof(out_obj
);
366 result
.pointer
= &out_obj
;
371 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
376 *(int *)res
= out_obj
.integer
.value
;
377 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
380 success
= status
== AE_OK
;
382 /* add more types as needed */
384 printk(TPACPI_ERR
"acpi_evalf() called "
385 "with invalid format character '%c'\n", res_type
);
389 if (!success
&& !quiet
)
390 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
391 method
, fmt0
, status
);
396 static int acpi_ec_read(int i
, u8
*p
)
401 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
405 if (ec_read(i
, p
) < 0)
412 static int acpi_ec_write(int i
, u8 v
)
415 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
418 if (ec_write(i
, v
) < 0)
425 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
426 static int _sta(acpi_handle handle
)
430 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
437 static int issue_thinkpad_cmos_command(int cmos_cmd
)
442 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
448 /*************************************************************************
452 #define TPACPI_ACPIHANDLE_INIT(object) \
453 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
454 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
456 static void drv_acpi_handle_init(char *name
,
457 acpi_handle
*handle
, acpi_handle parent
,
458 char **paths
, int num_paths
, char **path
)
463 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
466 for (i
= 0; i
< num_paths
; i
++) {
467 status
= acpi_get_handle(parent
, paths
[i
], handle
);
468 if (ACPI_SUCCESS(status
)) {
470 dbg_printk(TPACPI_DBG_INIT
,
471 "Found ACPI handle %s for %s\n",
477 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
482 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
484 struct ibm_struct
*ibm
= data
;
486 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
489 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
492 ibm
->acpi
->notify(ibm
, event
);
495 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
502 if (!*ibm
->acpi
->handle
)
505 vdbg_printk(TPACPI_DBG_INIT
,
506 "setting up ACPI notify for %s\n", ibm
->name
);
508 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
510 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
515 acpi_driver_data(ibm
->acpi
->device
) = ibm
;
516 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
517 TPACPI_ACPI_EVENT_PREFIX
,
520 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
521 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
522 if (ACPI_FAILURE(status
)) {
523 if (status
== AE_ALREADY_EXISTS
) {
525 "another device driver is already "
526 "handling %s events\n", ibm
->name
);
529 "acpi_install_notify_handler(%s) failed: %d\n",
534 ibm
->flags
.acpi_notify_installed
= 1;
538 static int __init
tpacpi_device_add(struct acpi_device
*device
)
543 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
547 dbg_printk(TPACPI_DBG_INIT
,
548 "registering %s as an ACPI driver\n", ibm
->name
);
552 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
553 if (!ibm
->acpi
->driver
) {
554 printk(TPACPI_ERR
"kzalloc(ibm->driver) failed\n");
558 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
559 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
561 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
563 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
565 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
567 kfree(ibm
->acpi
->driver
);
568 ibm
->acpi
->driver
= NULL
;
570 ibm
->flags
.acpi_driver_registered
= 1;
576 /****************************************************************************
577 ****************************************************************************
581 ****************************************************************************
582 ****************************************************************************/
584 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
585 int count
, int *eof
, void *data
)
587 struct ibm_struct
*ibm
= data
;
590 if (!ibm
|| !ibm
->read
)
593 len
= ibm
->read(page
);
597 if (len
<= off
+ count
)
609 static int dispatch_procfs_write(struct file
*file
,
610 const char __user
*userbuf
,
611 unsigned long count
, void *data
)
613 struct ibm_struct
*ibm
= data
;
617 if (!ibm
|| !ibm
->write
)
620 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
624 if (copy_from_user(kernbuf
, userbuf
, count
)) {
630 strcat(kernbuf
, ",");
631 ret
= ibm
->write(kernbuf
);
640 static char *next_cmd(char **cmds
)
645 while ((end
= strchr(start
, ',')) && end
== start
)
657 /****************************************************************************
658 ****************************************************************************
660 * Device model: input, hwmon and platform
662 ****************************************************************************
663 ****************************************************************************/
665 static struct platform_device
*tpacpi_pdev
;
666 static struct platform_device
*tpacpi_sensors_pdev
;
667 static struct device
*tpacpi_hwmon
;
668 static struct input_dev
*tpacpi_inputdev
;
669 static struct mutex tpacpi_inputdev_send_mutex
;
670 static LIST_HEAD(tpacpi_all_drivers
);
672 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
675 struct ibm_struct
*ibm
, *itmp
;
677 list_for_each_entry_safe(ibm
, itmp
,
681 (ibm
->suspend
)(state
);
687 static int tpacpi_resume_handler(struct platform_device
*pdev
)
689 struct ibm_struct
*ibm
, *itmp
;
691 list_for_each_entry_safe(ibm
, itmp
,
701 static struct platform_driver tpacpi_pdriver
= {
703 .name
= TPACPI_DRVR_NAME
,
704 .owner
= THIS_MODULE
,
706 .suspend
= tpacpi_suspend_handler
,
707 .resume
= tpacpi_resume_handler
,
710 static struct platform_driver tpacpi_hwmon_pdriver
= {
712 .name
= TPACPI_HWMON_DRVR_NAME
,
713 .owner
= THIS_MODULE
,
717 /*************************************************************************
718 * sysfs support helpers
721 struct attribute_set
{
722 unsigned int members
, max_members
;
723 struct attribute_group group
;
726 struct attribute_set_obj
{
727 struct attribute_set s
;
729 } __attribute__((packed
));
731 static struct attribute_set
*create_attr_set(unsigned int max_members
,
734 struct attribute_set_obj
*sobj
;
736 if (max_members
== 0)
739 /* Allocates space for implicit NULL at the end too */
740 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
741 max_members
* sizeof(struct attribute
*),
745 sobj
->s
.max_members
= max_members
;
746 sobj
->s
.group
.attrs
= &sobj
->a
;
747 sobj
->s
.group
.name
= name
;
752 #define destroy_attr_set(_set) \
755 /* not multi-threaded safe, use it in a single thread per set */
756 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
761 if (s
->members
>= s
->max_members
)
764 s
->group
.attrs
[s
->members
] = attr
;
770 static int add_many_to_attr_set(struct attribute_set
*s
,
771 struct attribute
**attr
,
776 for (i
= 0; i
< count
; i
++) {
777 res
= add_to_attr_set(s
, attr
[i
]);
785 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
787 sysfs_remove_group(kobj
, &s
->group
);
791 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
792 sysfs_create_group(_kobj, &_attr_set->group)
794 static int parse_strtoul(const char *buf
,
795 unsigned long max
, unsigned long *value
)
799 while (*buf
&& isspace(*buf
))
801 *value
= simple_strtoul(buf
, &endp
, 0);
802 while (*endp
&& isspace(*endp
))
804 if (*endp
|| *value
> max
)
810 /*************************************************************************
811 * thinkpad-acpi driver attributes
814 /* interface_version --------------------------------------------------- */
815 static ssize_t
tpacpi_driver_interface_version_show(
816 struct device_driver
*drv
,
819 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
822 static DRIVER_ATTR(interface_version
, S_IRUGO
,
823 tpacpi_driver_interface_version_show
, NULL
);
825 /* debug_level --------------------------------------------------------- */
826 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
829 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
832 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
833 const char *buf
, size_t count
)
837 if (parse_strtoul(buf
, 0xffff, &t
))
845 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
846 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
848 /* version ------------------------------------------------------------- */
849 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
852 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
853 TPACPI_DESC
, TPACPI_VERSION
);
856 static DRIVER_ATTR(version
, S_IRUGO
,
857 tpacpi_driver_version_show
, NULL
);
859 /* --------------------------------------------------------------------- */
861 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
862 &driver_attr_debug_level
, &driver_attr_version
,
863 &driver_attr_interface_version
,
866 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
872 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
873 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
880 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
884 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
885 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
888 /****************************************************************************
889 ****************************************************************************
893 ****************************************************************************
894 ****************************************************************************/
896 /*************************************************************************
897 * thinkpad-acpi init subdriver
900 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
902 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
903 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
905 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
906 (thinkpad_id
.bios_version_str
) ?
907 thinkpad_id
.bios_version_str
: "unknown",
908 (thinkpad_id
.ec_version_str
) ?
909 thinkpad_id
.ec_version_str
: "unknown");
911 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
912 printk(TPACPI_INFO
"%s %s\n",
913 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
914 "IBM" : ((thinkpad_id
.vendor
==
915 PCI_VENDOR_ID_LENOVO
) ?
916 "Lenovo" : "Unknown vendor"),
917 thinkpad_id
.model_str
);
922 static int thinkpad_acpi_driver_read(char *p
)
926 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
927 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
932 static struct ibm_struct thinkpad_acpi_driver_data
= {
934 .read
= thinkpad_acpi_driver_read
,
937 /*************************************************************************
941 enum { /* hot key scan codes (derived from ACPI DSDT) */
942 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
943 TP_ACPI_HOTKEYSCAN_FNF2
,
944 TP_ACPI_HOTKEYSCAN_FNF3
,
945 TP_ACPI_HOTKEYSCAN_FNF4
,
946 TP_ACPI_HOTKEYSCAN_FNF5
,
947 TP_ACPI_HOTKEYSCAN_FNF6
,
948 TP_ACPI_HOTKEYSCAN_FNF7
,
949 TP_ACPI_HOTKEYSCAN_FNF8
,
950 TP_ACPI_HOTKEYSCAN_FNF9
,
951 TP_ACPI_HOTKEYSCAN_FNF10
,
952 TP_ACPI_HOTKEYSCAN_FNF11
,
953 TP_ACPI_HOTKEYSCAN_FNF12
,
954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
955 TP_ACPI_HOTKEYSCAN_FNINSERT
,
956 TP_ACPI_HOTKEYSCAN_FNDELETE
,
957 TP_ACPI_HOTKEYSCAN_FNHOME
,
958 TP_ACPI_HOTKEYSCAN_FNEND
,
959 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
961 TP_ACPI_HOTKEYSCAN_FNSPACE
,
962 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
964 TP_ACPI_HOTKEYSCAN_MUTE
,
965 TP_ACPI_HOTKEYSCAN_THINKPAD
,
968 enum { /* Keys available through NVRAM polling */
969 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
970 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
973 enum { /* Positions of some of the keys in hotkey masks */
974 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
975 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
976 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
977 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
978 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
979 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
980 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
981 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
982 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
983 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
984 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
987 enum { /* NVRAM to ACPI HKEY group map */
988 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
989 TP_ACPI_HKEY_ZOOM_MASK
|
990 TP_ACPI_HKEY_DISPSWTCH_MASK
|
991 TP_ACPI_HKEY_HIBERNATE_MASK
,
992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
993 TP_ACPI_HKEY_BRGHTDWN_MASK
,
994 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
995 TP_ACPI_HKEY_VOLDWN_MASK
|
996 TP_ACPI_HKEY_MUTE_MASK
,
999 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1000 struct tp_nvram_state
{
1001 u16 thinkpad_toggle
:1;
1003 u16 display_toggle
:1;
1004 u16 thinklight_toggle
:1;
1005 u16 hibernate_toggle
:1;
1006 u16 displayexp_toggle
:1;
1007 u16 display_state
:1;
1008 u16 brightness_toggle
:1;
1009 u16 volume_toggle
:1;
1012 u8 brightness_level
;
1016 static struct task_struct
*tpacpi_hotkey_task
;
1017 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1018 static int hotkey_poll_freq
= 10; /* Hz */
1019 static struct mutex hotkey_thread_mutex
;
1020 static struct mutex hotkey_thread_data_mutex
;
1021 static unsigned int hotkey_config_change
;
1023 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1025 #define hotkey_source_mask 0U
1027 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1029 static struct mutex hotkey_mutex
;
1031 static enum { /* Reasons for waking up */
1032 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1033 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1034 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1035 } hotkey_wakeup_reason
;
1037 static int hotkey_autosleep_ack
;
1039 static int hotkey_orig_status
;
1040 static u32 hotkey_orig_mask
;
1041 static u32 hotkey_all_mask
;
1042 static u32 hotkey_reserved_mask
;
1043 static u32 hotkey_mask
;
1045 static unsigned int hotkey_report_mode
;
1047 static u16
*hotkey_keycode_map
;
1049 static struct attribute_set
*hotkey_dev_attributes
;
1051 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1052 #define HOTKEY_CONFIG_CRITICAL_START \
1054 mutex_lock(&hotkey_thread_data_mutex); \
1055 hotkey_config_change++; \
1057 #define HOTKEY_CONFIG_CRITICAL_END \
1058 mutex_unlock(&hotkey_thread_data_mutex);
1060 #define HOTKEY_CONFIG_CRITICAL_START
1061 #define HOTKEY_CONFIG_CRITICAL_END
1062 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1064 static int hotkey_get_wlsw(int *status
)
1066 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1072 * Call with hotkey_mutex held
1074 static int hotkey_mask_get(void)
1078 if (tp_features
.hotkey_mask
) {
1079 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1082 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1088 * Call with hotkey_mutex held
1090 static int hotkey_mask_set(u32 mask
)
1095 if (tp_features
.hotkey_mask
) {
1096 HOTKEY_CONFIG_CRITICAL_START
1097 for (i
= 0; i
< 32; i
++) {
1099 /* enable in firmware mask only keys not in NVRAM
1100 * mode, but enable the key in the cached hotkey_mask
1101 * regardless of mode, or the key will end up
1102 * disabled by hotkey_mask_get() */
1103 if (!acpi_evalf(hkey_handle
,
1104 NULL
, "MHKM", "vdd", i
+ 1,
1105 !!((mask
& ~hotkey_source_mask
) & m
))) {
1109 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1112 HOTKEY_CONFIG_CRITICAL_END
1114 /* hotkey_mask_get must be called unconditionally below */
1115 if (!hotkey_mask_get() && !rc
&&
1116 (hotkey_mask
& ~hotkey_source_mask
) !=
1117 (mask
& ~hotkey_source_mask
)) {
1118 printk(TPACPI_NOTICE
1119 "requested hot key mask 0x%08x, but "
1120 "firmware forced it to 0x%08x\n",
1124 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1125 HOTKEY_CONFIG_CRITICAL_START
1126 hotkey_mask
= mask
& hotkey_source_mask
;
1127 HOTKEY_CONFIG_CRITICAL_END
1129 if (hotkey_mask
!= mask
) {
1130 printk(TPACPI_NOTICE
1131 "requested hot key mask 0x%08x, "
1132 "forced to 0x%08x (NVRAM poll mask is "
1133 "0x%08x): no firmware mask support\n",
1134 mask
, hotkey_mask
, hotkey_source_mask
);
1139 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1145 static int hotkey_status_get(int *status
)
1147 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1153 static int hotkey_status_set(int status
)
1155 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", status
))
1161 static void tpacpi_input_send_radiosw(void)
1165 mutex_lock(&tpacpi_inputdev_send_mutex
);
1167 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
1168 input_report_switch(tpacpi_inputdev
,
1170 input_sync(tpacpi_inputdev
);
1173 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1176 static void tpacpi_input_send_key(unsigned int scancode
)
1178 unsigned int keycode
;
1180 keycode
= hotkey_keycode_map
[scancode
];
1182 if (keycode
!= KEY_RESERVED
) {
1183 mutex_lock(&tpacpi_inputdev_send_mutex
);
1185 input_report_key(tpacpi_inputdev
, keycode
, 1);
1186 if (keycode
== KEY_UNKNOWN
)
1187 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1189 input_sync(tpacpi_inputdev
);
1191 input_report_key(tpacpi_inputdev
, keycode
, 0);
1192 if (keycode
== KEY_UNKNOWN
)
1193 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1195 input_sync(tpacpi_inputdev
);
1197 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1201 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1202 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1204 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1206 tpacpi_input_send_key(scancode
);
1207 if (hotkey_report_mode
< 2) {
1208 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1209 0x80, 0x1001 + scancode
);
1213 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1217 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1218 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1219 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1220 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1221 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1222 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1224 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1225 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1226 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1228 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1229 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1230 n
->displayexp_toggle
=
1231 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1233 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1234 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1235 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1236 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1237 n
->brightness_toggle
=
1238 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1240 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1241 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1242 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1243 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1244 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1245 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1249 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1251 if ((mask & (1 << __scancode)) && \
1252 oldn->__member != newn->__member) \
1253 tpacpi_hotkey_send_key(__scancode); \
1256 #define TPACPI_MAY_SEND_KEY(__scancode) \
1257 do { if (mask & (1 << __scancode)) \
1258 tpacpi_hotkey_send_key(__scancode); } while (0)
1260 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1261 struct tp_nvram_state
*newn
,
1264 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1265 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1266 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1267 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1269 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1271 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1274 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1275 if (oldn
->mute
!= newn
->mute
) {
1276 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1278 if (oldn
->volume_level
> newn
->volume_level
) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1280 } else if (oldn
->volume_level
< newn
->volume_level
) {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1282 } else if (oldn
->mute
== newn
->mute
) {
1283 /* repeated key presses that didn't change state */
1285 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1286 } else if (newn
->volume_level
!= 0) {
1287 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1294 /* handle brightness */
1295 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1296 if (oldn
->brightness_level
< newn
->brightness_level
) {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1298 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1299 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1301 /* repeated key presses that didn't change state */
1302 if (newn
->brightness_level
!= 0) {
1303 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1305 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1311 #undef TPACPI_COMPARE_KEY
1312 #undef TPACPI_MAY_SEND_KEY
1314 static int hotkey_kthread(void *data
)
1316 struct tp_nvram_state s
[2];
1318 unsigned int si
, so
;
1320 unsigned int change_detector
, must_reset
;
1322 mutex_lock(&hotkey_thread_mutex
);
1324 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1333 /* Initial state for compares */
1334 mutex_lock(&hotkey_thread_data_mutex
);
1335 change_detector
= hotkey_config_change
;
1336 mask
= hotkey_source_mask
& hotkey_mask
;
1337 mutex_unlock(&hotkey_thread_data_mutex
);
1338 hotkey_read_nvram(&s
[so
], mask
);
1340 while (!kthread_should_stop() && hotkey_poll_freq
) {
1342 t
= 1000/hotkey_poll_freq
;
1343 t
= msleep_interruptible(t
);
1344 if (unlikely(kthread_should_stop()))
1346 must_reset
= try_to_freeze();
1347 if (t
> 0 && !must_reset
)
1350 mutex_lock(&hotkey_thread_data_mutex
);
1351 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1352 /* forget old state on thaw or config change */
1355 change_detector
= hotkey_config_change
;
1357 mask
= hotkey_source_mask
& hotkey_mask
;
1358 mutex_unlock(&hotkey_thread_data_mutex
);
1361 hotkey_read_nvram(&s
[si
], mask
);
1362 if (likely(si
!= so
)) {
1363 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1373 mutex_unlock(&hotkey_thread_mutex
);
1377 static void hotkey_poll_stop_sync(void)
1379 if (tpacpi_hotkey_task
) {
1380 if (frozen(tpacpi_hotkey_task
) ||
1381 freezing(tpacpi_hotkey_task
))
1382 thaw_process(tpacpi_hotkey_task
);
1384 kthread_stop(tpacpi_hotkey_task
);
1385 tpacpi_hotkey_task
= NULL
;
1386 mutex_lock(&hotkey_thread_mutex
);
1387 /* at this point, the thread did exit */
1388 mutex_unlock(&hotkey_thread_mutex
);
1392 /* call with hotkey_mutex held */
1393 static void hotkey_poll_setup(int may_warn
)
1395 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1396 hotkey_poll_freq
> 0 &&
1397 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1398 if (!tpacpi_hotkey_task
) {
1399 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1402 if (IS_ERR(tpacpi_hotkey_task
)) {
1403 tpacpi_hotkey_task
= NULL
;
1405 "could not create kernel thread "
1406 "for hotkey polling\n");
1410 hotkey_poll_stop_sync();
1412 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1413 printk(TPACPI_NOTICE
1414 "hot keys 0x%08x require polling, "
1415 "which is currently disabled\n",
1416 hotkey_source_mask
);
1421 static void hotkey_poll_setup_safe(int may_warn
)
1423 mutex_lock(&hotkey_mutex
);
1424 hotkey_poll_setup(may_warn
);
1425 mutex_unlock(&hotkey_mutex
);
1428 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1430 static void hotkey_poll_setup_safe(int __unused
)
1434 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1436 static int hotkey_inputdev_open(struct input_dev
*dev
)
1438 switch (tpacpi_lifecycle
) {
1439 case TPACPI_LIFE_INIT
:
1441 * hotkey_init will call hotkey_poll_setup_safe
1442 * at the appropriate moment
1445 case TPACPI_LIFE_EXITING
:
1447 case TPACPI_LIFE_RUNNING
:
1448 hotkey_poll_setup_safe(0);
1452 /* Should only happen if tpacpi_lifecycle is corrupt */
1457 static void hotkey_inputdev_close(struct input_dev
*dev
)
1459 /* disable hotkey polling when possible */
1460 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1461 hotkey_poll_setup_safe(0);
1464 /* sysfs hotkey enable ------------------------------------------------- */
1465 static ssize_t
hotkey_enable_show(struct device
*dev
,
1466 struct device_attribute
*attr
,
1471 res
= hotkey_status_get(&status
);
1475 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1478 static ssize_t
hotkey_enable_store(struct device
*dev
,
1479 struct device_attribute
*attr
,
1480 const char *buf
, size_t count
)
1485 if (parse_strtoul(buf
, 1, &t
))
1488 res
= hotkey_status_set(t
);
1490 return (res
) ? res
: count
;
1493 static struct device_attribute dev_attr_hotkey_enable
=
1494 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1495 hotkey_enable_show
, hotkey_enable_store
);
1497 /* sysfs hotkey mask --------------------------------------------------- */
1498 static ssize_t
hotkey_mask_show(struct device
*dev
,
1499 struct device_attribute
*attr
,
1504 if (mutex_lock_interruptible(&hotkey_mutex
))
1505 return -ERESTARTSYS
;
1506 res
= hotkey_mask_get();
1507 mutex_unlock(&hotkey_mutex
);
1510 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1513 static ssize_t
hotkey_mask_store(struct device
*dev
,
1514 struct device_attribute
*attr
,
1515 const char *buf
, size_t count
)
1520 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1523 if (mutex_lock_interruptible(&hotkey_mutex
))
1524 return -ERESTARTSYS
;
1526 res
= hotkey_mask_set(t
);
1528 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1529 hotkey_poll_setup(1);
1532 mutex_unlock(&hotkey_mutex
);
1534 return (res
) ? res
: count
;
1537 static struct device_attribute dev_attr_hotkey_mask
=
1538 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1539 hotkey_mask_show
, hotkey_mask_store
);
1541 /* sysfs hotkey bios_enabled ------------------------------------------- */
1542 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1543 struct device_attribute
*attr
,
1546 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_orig_status
);
1549 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1550 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1552 /* sysfs hotkey bios_mask ---------------------------------------------- */
1553 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1554 struct device_attribute
*attr
,
1557 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1560 static struct device_attribute dev_attr_hotkey_bios_mask
=
1561 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1563 /* sysfs hotkey all_mask ----------------------------------------------- */
1564 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
1565 struct device_attribute
*attr
,
1568 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1569 hotkey_all_mask
| hotkey_source_mask
);
1572 static struct device_attribute dev_attr_hotkey_all_mask
=
1573 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
1575 /* sysfs hotkey recommended_mask --------------------------------------- */
1576 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
1577 struct device_attribute
*attr
,
1580 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1581 (hotkey_all_mask
| hotkey_source_mask
)
1582 & ~hotkey_reserved_mask
);
1585 static struct device_attribute dev_attr_hotkey_recommended_mask
=
1586 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
1587 hotkey_recommended_mask_show
, NULL
);
1589 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1591 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1592 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
1593 struct device_attribute
*attr
,
1596 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
1599 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
1600 struct device_attribute
*attr
,
1601 const char *buf
, size_t count
)
1605 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
1606 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
1609 if (mutex_lock_interruptible(&hotkey_mutex
))
1610 return -ERESTARTSYS
;
1612 HOTKEY_CONFIG_CRITICAL_START
1613 hotkey_source_mask
= t
;
1614 HOTKEY_CONFIG_CRITICAL_END
1616 hotkey_poll_setup(1);
1618 mutex_unlock(&hotkey_mutex
);
1623 static struct device_attribute dev_attr_hotkey_source_mask
=
1624 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
1625 hotkey_source_mask_show
, hotkey_source_mask_store
);
1627 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1628 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
1629 struct device_attribute
*attr
,
1632 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
1635 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
1636 struct device_attribute
*attr
,
1637 const char *buf
, size_t count
)
1641 if (parse_strtoul(buf
, 25, &t
))
1644 if (mutex_lock_interruptible(&hotkey_mutex
))
1645 return -ERESTARTSYS
;
1647 hotkey_poll_freq
= t
;
1649 hotkey_poll_setup(1);
1650 mutex_unlock(&hotkey_mutex
);
1655 static struct device_attribute dev_attr_hotkey_poll_freq
=
1656 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
1657 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
1659 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1661 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1662 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
1663 struct device_attribute
*attr
,
1667 res
= hotkey_get_wlsw(&s
);
1671 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1674 static struct device_attribute dev_attr_hotkey_radio_sw
=
1675 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
1677 static void hotkey_radio_sw_notify_change(void)
1679 if (tp_features
.hotkey_wlsw
)
1680 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1684 /* sysfs hotkey report_mode -------------------------------------------- */
1685 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
1686 struct device_attribute
*attr
,
1689 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1690 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
1693 static struct device_attribute dev_attr_hotkey_report_mode
=
1694 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
1696 /* sysfs wakeup reason (pollable) -------------------------------------- */
1697 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
1698 struct device_attribute
*attr
,
1701 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
1704 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
1705 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
1707 void hotkey_wakeup_reason_notify_change(void)
1709 if (tp_features
.hotkey_mask
)
1710 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1714 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1715 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
1716 struct device_attribute
*attr
,
1719 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
1722 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
1723 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
1724 hotkey_wakeup_hotunplug_complete_show
, NULL
);
1726 void hotkey_wakeup_hotunplug_complete_notify_change(void)
1728 if (tp_features
.hotkey_mask
)
1729 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1730 "wakeup_hotunplug_complete");
1733 /* --------------------------------------------------------------------- */
1735 static struct attribute
*hotkey_attributes
[] __initdata
= {
1736 &dev_attr_hotkey_enable
.attr
,
1737 &dev_attr_hotkey_bios_enabled
.attr
,
1738 &dev_attr_hotkey_report_mode
.attr
,
1739 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1740 &dev_attr_hotkey_mask
.attr
,
1741 &dev_attr_hotkey_all_mask
.attr
,
1742 &dev_attr_hotkey_recommended_mask
.attr
,
1743 &dev_attr_hotkey_source_mask
.attr
,
1744 &dev_attr_hotkey_poll_freq
.attr
,
1748 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
1749 &dev_attr_hotkey_bios_mask
.attr
,
1750 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1751 &dev_attr_hotkey_mask
.attr
,
1752 &dev_attr_hotkey_all_mask
.attr
,
1753 &dev_attr_hotkey_recommended_mask
.attr
,
1755 &dev_attr_hotkey_wakeup_reason
.attr
,
1756 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
1759 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
1761 /* Requirements for changing the default keymaps:
1763 * 1. Many of the keys are mapped to KEY_RESERVED for very
1764 * good reasons. Do not change them unless you have deep
1765 * knowledge on the IBM and Lenovo ThinkPad firmware for
1766 * the various ThinkPad models. The driver behaves
1767 * differently for KEY_RESERVED: such keys have their
1768 * hot key mask *unset* in mask_recommended, and also
1769 * in the initial hot key mask programmed into the
1770 * firmware at driver load time, which means the firm-
1771 * ware may react very differently if you change them to
1774 * 2. You must be subscribed to the linux-thinkpad and
1775 * ibm-acpi-devel mailing lists, and you should read the
1776 * list archives since 2007 if you want to change the
1777 * keymaps. This requirement exists so that you will
1778 * know the past history of problems with the thinkpad-
1779 * acpi driver keymaps, and also that you will be
1780 * listening to any bug reports;
1782 * 3. Do not send thinkpad-acpi specific patches directly to
1783 * for merging, *ever*. Send them to the linux-acpi
1784 * mailinglist for comments. Merging is to be done only
1785 * through acpi-test and the ACPI maintainer.
1787 * If the above is too much to ask, don't change the keymap.
1788 * Ask the thinkpad-acpi maintainer to do it, instead.
1790 static u16 ibm_keycode_map
[] __initdata
= {
1791 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1792 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
1793 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
1794 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
1796 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1797 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
1798 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
1799 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
1801 /* brightness: firmware always reacts to them, unless
1802 * X.org did some tricks in the radeon BIOS scratch
1803 * registers of *some* models */
1804 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
1805 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
1807 /* Thinklight: firmware always react to it */
1808 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
1810 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
1811 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
1813 /* Volume: firmware always react to it and reprograms
1814 * the built-in *extra* mixer. Never map it to control
1815 * another mixer by default. */
1816 KEY_RESERVED
, /* 0x14: VOLUME UP */
1817 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
1818 KEY_RESERVED
, /* 0x16: MUTE */
1820 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1822 /* (assignments unknown, please report if found) */
1823 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1824 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1826 static u16 lenovo_keycode_map
[] __initdata
= {
1827 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1828 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
1829 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
1830 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
1832 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1833 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
1834 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
1835 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
1837 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
1838 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
1840 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
1842 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
1843 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
1845 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1846 * react to it and reprograms the built-in *extra* mixer.
1847 * Never map it to control another mixer by default.
1849 * T60?, T61, R60?, R61: firmware and EC tries to send
1850 * these over the regular keyboard, so these are no-ops,
1851 * but there are still weird bugs re. MUTE, so do not
1852 * change unless you get test reports from all Lenovo
1853 * models. May cause the BIOS to interfere with the
1856 KEY_RESERVED
, /* 0x14: VOLUME UP */
1857 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
1858 KEY_RESERVED
, /* 0x16: MUTE */
1860 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1862 /* (assignments unknown, please report if found) */
1863 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1864 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1867 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1868 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1869 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1875 vdbg_printk(TPACPI_DBG_INIT
, "initializing hotkey subdriver\n");
1877 BUG_ON(!tpacpi_inputdev
);
1878 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
1879 tpacpi_inputdev
->close
!= NULL
);
1881 TPACPI_ACPIHANDLE_INIT(hkey
);
1882 mutex_init(&hotkey_mutex
);
1884 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1885 mutex_init(&hotkey_thread_mutex
);
1886 mutex_init(&hotkey_thread_data_mutex
);
1889 /* hotkey not supported on 570 */
1890 tp_features
.hotkey
= hkey_handle
!= NULL
;
1892 vdbg_printk(TPACPI_DBG_INIT
, "hotkeys are %s\n",
1893 str_supported(tp_features
.hotkey
));
1895 if (tp_features
.hotkey
) {
1896 hotkey_dev_attributes
= create_attr_set(12, NULL
);
1897 if (!hotkey_dev_attributes
)
1899 res
= add_many_to_attr_set(hotkey_dev_attributes
,
1901 ARRAY_SIZE(hotkey_attributes
));
1905 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1906 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1907 for HKEY interface version 0x100 */
1908 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
1909 if ((hkeyv
>> 8) != 1) {
1910 printk(TPACPI_ERR
"unknown version of the "
1911 "HKEY interface: 0x%x\n", hkeyv
);
1912 printk(TPACPI_ERR
"please report this to %s\n",
1916 * MHKV 0x100 in A31, R40, R40e,
1917 * T4x, X31, and later
1919 tp_features
.hotkey_mask
= 1;
1923 vdbg_printk(TPACPI_DBG_INIT
, "hotkey masks are %s\n",
1924 str_supported(tp_features
.hotkey_mask
));
1926 if (tp_features
.hotkey_mask
) {
1927 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
1930 "missing MHKA handler, "
1931 "please report this to %s\n",
1933 /* FN+F12, FN+F4, FN+F3 */
1934 hotkey_all_mask
= 0x080cU
;
1938 /* hotkey_source_mask *must* be zero for
1939 * the first hotkey_mask_get */
1940 res
= hotkey_status_get(&hotkey_orig_status
);
1941 if (!res
&& tp_features
.hotkey_mask
) {
1942 res
= hotkey_mask_get();
1943 hotkey_orig_mask
= hotkey_mask
;
1945 res
= add_many_to_attr_set(
1946 hotkey_dev_attributes
,
1947 hotkey_mask_attributes
,
1948 ARRAY_SIZE(hotkey_mask_attributes
));
1952 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1953 if (tp_features
.hotkey_mask
) {
1954 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
1957 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
1960 vdbg_printk(TPACPI_DBG_INIT
,
1961 "hotkey source mask 0x%08x, polling freq %d\n",
1962 hotkey_source_mask
, hotkey_poll_freq
);
1965 /* Not all thinkpads have a hardware radio switch */
1966 if (!res
&& acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
1967 tp_features
.hotkey_wlsw
= 1;
1969 "radio switch found; radios are %s\n",
1970 enabled(status
, 0));
1971 res
= add_to_attr_set(hotkey_dev_attributes
,
1972 &dev_attr_hotkey_radio_sw
.attr
);
1976 res
= register_attr_set_with_sysfs(
1977 hotkey_dev_attributes
,
1978 &tpacpi_pdev
->dev
.kobj
);
1982 /* Set up key map */
1984 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
1986 if (!hotkey_keycode_map
) {
1988 "failed to allocate memory for key map\n");
1992 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
1993 dbg_printk(TPACPI_DBG_INIT
,
1994 "using Lenovo default hot key map\n");
1995 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
1996 TPACPI_HOTKEY_MAP_SIZE
);
1998 dbg_printk(TPACPI_DBG_INIT
,
1999 "using IBM default hot key map\n");
2000 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2001 TPACPI_HOTKEY_MAP_SIZE
);
2004 /* Do not issue duplicate brightness change events to
2006 if (tp_features
.bright_acpimode
) {
2007 printk(TPACPI_NOTICE
2008 "Brightness hot keys firmware issues ACPI "
2010 printk(TPACPI_NOTICE
2011 "Disabling brightness hot keys on event "
2013 hotkey_keycode_map
[TP_ACPI_HOTKEYSCAN_FNHOME
] =
2015 hotkey_keycode_map
[TP_ACPI_HOTKEYSCAN_FNEND
] =
2019 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2020 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2021 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2022 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2023 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2024 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2025 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2026 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2027 set_bit(hotkey_keycode_map
[i
],
2028 tpacpi_inputdev
->keybit
);
2030 if (i
< sizeof(hotkey_reserved_mask
)*8)
2031 hotkey_reserved_mask
|= 1 << i
;
2035 if (tp_features
.hotkey_wlsw
) {
2036 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2037 set_bit(SW_RADIO
, tpacpi_inputdev
->swbit
);
2040 dbg_printk(TPACPI_DBG_INIT
,
2041 "enabling hot key handling\n");
2042 res
= hotkey_status_set(1);
2045 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2046 & ~hotkey_reserved_mask
)
2047 | hotkey_orig_mask
);
2048 if (res
< 0 && res
!= -ENXIO
)
2051 dbg_printk(TPACPI_DBG_INIT
,
2052 "legacy hot key reporting over procfs %s\n",
2053 (hotkey_report_mode
< 2) ?
2054 "enabled" : "disabled");
2056 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2057 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2059 hotkey_poll_setup_safe(1);
2060 tpacpi_input_send_radiosw();
2063 return (tp_features
.hotkey
)? 0 : 1;
2066 static void hotkey_exit(void)
2068 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2069 hotkey_poll_stop_sync();
2072 if (tp_features
.hotkey
) {
2073 dbg_printk(TPACPI_DBG_EXIT
,
2074 "restoring original hot key mask\n");
2075 /* no short-circuit boolean operator below! */
2076 if ((hotkey_mask_set(hotkey_orig_mask
) |
2077 hotkey_status_set(hotkey_orig_status
)) != 0)
2079 "failed to restore hot key mask "
2080 "to BIOS defaults\n");
2083 if (hotkey_dev_attributes
) {
2084 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2085 hotkey_dev_attributes
= NULL
;
2089 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2092 unsigned int scancode
;
2097 if (event
!= 0x80) {
2099 "unknown HKEY notification event %d\n", event
);
2100 /* forward it to userspace, maybe it knows how to handle it */
2101 acpi_bus_generate_netlink_event(
2102 ibm
->acpi
->device
->pnp
.device_class
,
2103 ibm
->acpi
->device
->dev
.bus_id
,
2109 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2110 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2123 switch (hkey
>> 12) {
2125 /* 0x1000-0x1FFF: key presses */
2126 scancode
= hkey
& 0xfff;
2127 if (scancode
> 0 && scancode
< 0x21) {
2129 if (!(hotkey_source_mask
& (1 << scancode
))) {
2130 tpacpi_input_send_key(scancode
);
2142 case 0x2304: /* suspend, undock */
2143 case 0x2404: /* hibernation, undock */
2144 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2147 case 0x2305: /* suspend, bay eject */
2148 case 0x2405: /* hibernation, bay eject */
2149 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2155 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2157 "woke up due to a hot-unplug "
2159 hotkey_wakeup_reason_notify_change();
2163 /* bay-related wakeups */
2164 if (hkey
== 0x3003) {
2165 hotkey_autosleep_ack
= 1;
2168 hotkey_wakeup_hotunplug_complete_notify_change();
2174 /* dock-related wakeups */
2175 if (hkey
== 0x4003) {
2176 hotkey_autosleep_ack
= 1;
2179 hotkey_wakeup_hotunplug_complete_notify_change();
2185 /* 0x5000-0x5FFF: human interface helpers */
2187 case 0x5010: /* Lenovo new BIOS: brightness changed */
2188 case 0x5009: /* X61t: swivel up (tablet mode) */
2189 case 0x500a: /* X61t: swivel down (normal mode) */
2190 case 0x500b: /* X61t: tablet pen inserted into bay */
2191 case 0x500c: /* X61t: tablet pen removed from bay */
2195 /* LID switch events. Do not propagate */
2203 /* 0x7000-0x7FFF: misc */
2204 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2205 tpacpi_input_send_radiosw();
2206 hotkey_radio_sw_notify_change();
2210 /* fallthrough to default */
2215 printk(TPACPI_NOTICE
2216 "unhandled HKEY event 0x%04x\n", hkey
);
2220 if (!ignore_acpi_ev
&&
2221 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2222 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2226 /* netlink events */
2227 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2228 acpi_bus_generate_netlink_event(
2229 ibm
->acpi
->device
->pnp
.device_class
,
2230 ibm
->acpi
->device
->dev
.bus_id
,
2236 static void hotkey_suspend(pm_message_t state
)
2238 /* Do these on suspend, we get the events on early resume! */
2239 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2240 hotkey_autosleep_ack
= 0;
2243 static void hotkey_resume(void)
2245 if (hotkey_mask_get())
2247 "error while trying to read hot key mask "
2249 tpacpi_input_send_radiosw();
2250 hotkey_radio_sw_notify_change();
2251 hotkey_wakeup_reason_notify_change();
2252 hotkey_wakeup_hotunplug_complete_notify_change();
2253 hotkey_poll_setup_safe(0);
2256 /* procfs -------------------------------------------------------------- */
2257 static int hotkey_read(char *p
)
2262 if (!tp_features
.hotkey
) {
2263 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2267 if (mutex_lock_interruptible(&hotkey_mutex
))
2268 return -ERESTARTSYS
;
2269 res
= hotkey_status_get(&status
);
2271 res
= hotkey_mask_get();
2272 mutex_unlock(&hotkey_mutex
);
2276 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2277 if (tp_features
.hotkey_mask
) {
2278 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2279 len
+= sprintf(p
+ len
,
2280 "commands:\tenable, disable, reset, <mask>\n");
2282 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2283 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2289 static int hotkey_write(char *buf
)
2295 if (!tp_features
.hotkey
)
2298 if (mutex_lock_interruptible(&hotkey_mutex
))
2299 return -ERESTARTSYS
;
2305 while ((cmd
= next_cmd(&buf
))) {
2306 if (strlencmp(cmd
, "enable") == 0) {
2308 } else if (strlencmp(cmd
, "disable") == 0) {
2310 } else if (strlencmp(cmd
, "reset") == 0) {
2311 status
= hotkey_orig_status
;
2312 mask
= hotkey_orig_mask
;
2313 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2315 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2323 res
= hotkey_status_set(status
);
2325 if (!res
&& mask
!= hotkey_mask
)
2326 res
= hotkey_mask_set(mask
);
2329 mutex_unlock(&hotkey_mutex
);
2333 static const struct acpi_device_id ibm_htk_device_ids
[] = {
2334 {TPACPI_ACPI_HKEY_HID
, 0},
2338 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
2339 .hid
= ibm_htk_device_ids
,
2340 .notify
= hotkey_notify
,
2341 .handle
= &hkey_handle
,
2342 .type
= ACPI_DEVICE_NOTIFY
,
2345 static struct ibm_struct hotkey_driver_data
= {
2347 .read
= hotkey_read
,
2348 .write
= hotkey_write
,
2349 .exit
= hotkey_exit
,
2350 .resume
= hotkey_resume
,
2351 .suspend
= hotkey_suspend
,
2352 .acpi
= &ibm_hotkey_acpidriver
,
2355 /*************************************************************************
2356 * Bluetooth subdriver
2360 /* ACPI GBDC/SBDC bits */
2361 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
2362 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
2363 TP_ACPI_BLUETOOTH_UNK
= 0x04, /* unknown function */
2366 static int bluetooth_get_radiosw(void);
2367 static int bluetooth_set_radiosw(int radio_on
);
2369 /* sysfs bluetooth enable ---------------------------------------------- */
2370 static ssize_t
bluetooth_enable_show(struct device
*dev
,
2371 struct device_attribute
*attr
,
2376 status
= bluetooth_get_radiosw();
2380 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
? 1 : 0);
2383 static ssize_t
bluetooth_enable_store(struct device
*dev
,
2384 struct device_attribute
*attr
,
2385 const char *buf
, size_t count
)
2390 if (parse_strtoul(buf
, 1, &t
))
2393 res
= bluetooth_set_radiosw(t
);
2395 return (res
) ? res
: count
;
2398 static struct device_attribute dev_attr_bluetooth_enable
=
2399 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
2400 bluetooth_enable_show
, bluetooth_enable_store
);
2402 /* --------------------------------------------------------------------- */
2404 static struct attribute
*bluetooth_attributes
[] = {
2405 &dev_attr_bluetooth_enable
.attr
,
2409 static const struct attribute_group bluetooth_attr_group
= {
2410 .attrs
= bluetooth_attributes
,
2413 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
2418 vdbg_printk(TPACPI_DBG_INIT
, "initializing bluetooth subdriver\n");
2420 TPACPI_ACPIHANDLE_INIT(hkey
);
2422 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2423 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2424 tp_features
.bluetooth
= hkey_handle
&&
2425 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
2427 vdbg_printk(TPACPI_DBG_INIT
, "bluetooth is %s, status 0x%02x\n",
2428 str_supported(tp_features
.bluetooth
),
2431 if (tp_features
.bluetooth
) {
2432 if (!(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
2433 /* no bluetooth hardware present in system */
2434 tp_features
.bluetooth
= 0;
2435 dbg_printk(TPACPI_DBG_INIT
,
2436 "bluetooth hardware not installed\n");
2438 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2439 &bluetooth_attr_group
);
2445 return (tp_features
.bluetooth
)? 0 : 1;
2448 static void bluetooth_exit(void)
2450 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2451 &bluetooth_attr_group
);
2454 static int bluetooth_get_radiosw(void)
2458 if (!tp_features
.bluetooth
)
2461 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2464 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0);
2467 static int bluetooth_set_radiosw(int radio_on
)
2471 if (!tp_features
.bluetooth
)
2474 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2477 status
|= TP_ACPI_BLUETOOTH_RADIOSSW
;
2479 status
&= ~TP_ACPI_BLUETOOTH_RADIOSSW
;
2480 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
2486 /* procfs -------------------------------------------------------------- */
2487 static int bluetooth_read(char *p
)
2490 int status
= bluetooth_get_radiosw();
2492 if (!tp_features
.bluetooth
)
2493 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2495 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2496 (status
)? "enabled" : "disabled");
2497 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2503 static int bluetooth_write(char *buf
)
2507 if (!tp_features
.bluetooth
)
2510 while ((cmd
= next_cmd(&buf
))) {
2511 if (strlencmp(cmd
, "enable") == 0) {
2512 bluetooth_set_radiosw(1);
2513 } else if (strlencmp(cmd
, "disable") == 0) {
2514 bluetooth_set_radiosw(0);
2522 static struct ibm_struct bluetooth_driver_data
= {
2523 .name
= "bluetooth",
2524 .read
= bluetooth_read
,
2525 .write
= bluetooth_write
,
2526 .exit
= bluetooth_exit
,
2529 /*************************************************************************
2534 /* ACPI GWAN/SWAN bits */
2535 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
2536 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
2537 TP_ACPI_WANCARD_UNK
= 0x04, /* unknown function */
2540 static int wan_get_radiosw(void);
2541 static int wan_set_radiosw(int radio_on
);
2543 /* sysfs wan enable ---------------------------------------------------- */
2544 static ssize_t
wan_enable_show(struct device
*dev
,
2545 struct device_attribute
*attr
,
2550 status
= wan_get_radiosw();
2554 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
? 1 : 0);
2557 static ssize_t
wan_enable_store(struct device
*dev
,
2558 struct device_attribute
*attr
,
2559 const char *buf
, size_t count
)
2564 if (parse_strtoul(buf
, 1, &t
))
2567 res
= wan_set_radiosw(t
);
2569 return (res
) ? res
: count
;
2572 static struct device_attribute dev_attr_wan_enable
=
2573 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
2574 wan_enable_show
, wan_enable_store
);
2576 /* --------------------------------------------------------------------- */
2578 static struct attribute
*wan_attributes
[] = {
2579 &dev_attr_wan_enable
.attr
,
2583 static const struct attribute_group wan_attr_group
= {
2584 .attrs
= wan_attributes
,
2587 static int __init
wan_init(struct ibm_init_struct
*iibm
)
2592 vdbg_printk(TPACPI_DBG_INIT
, "initializing wan subdriver\n");
2594 TPACPI_ACPIHANDLE_INIT(hkey
);
2596 tp_features
.wan
= hkey_handle
&&
2597 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
2599 vdbg_printk(TPACPI_DBG_INIT
, "wan is %s, status 0x%02x\n",
2600 str_supported(tp_features
.wan
),
2603 if (tp_features
.wan
) {
2604 if (!(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
2605 /* no wan hardware present in system */
2606 tp_features
.wan
= 0;
2607 dbg_printk(TPACPI_DBG_INIT
,
2608 "wan hardware not installed\n");
2610 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2617 return (tp_features
.wan
)? 0 : 1;
2620 static void wan_exit(void)
2622 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2626 static int wan_get_radiosw(void)
2630 if (!tp_features
.wan
)
2633 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2636 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0);
2639 static int wan_set_radiosw(int radio_on
)
2643 if (!tp_features
.wan
)
2646 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2649 status
|= TP_ACPI_WANCARD_RADIOSSW
;
2651 status
&= ~TP_ACPI_WANCARD_RADIOSSW
;
2652 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
2658 /* procfs -------------------------------------------------------------- */
2659 static int wan_read(char *p
)
2662 int status
= wan_get_radiosw();
2664 if (!tp_features
.wan
)
2665 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2667 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2668 (status
)? "enabled" : "disabled");
2669 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2675 static int wan_write(char *buf
)
2679 if (!tp_features
.wan
)
2682 while ((cmd
= next_cmd(&buf
))) {
2683 if (strlencmp(cmd
, "enable") == 0) {
2685 } else if (strlencmp(cmd
, "disable") == 0) {
2694 static struct ibm_struct wan_driver_data
= {
2699 .flags
.experimental
= 1,
2702 /*************************************************************************
2706 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2708 enum video_access_mode
{
2709 TPACPI_VIDEO_NONE
= 0,
2710 TPACPI_VIDEO_570
, /* 570 */
2711 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
2712 TPACPI_VIDEO_NEW
, /* all others */
2715 enum { /* video status flags, based on VIDEO_570 */
2716 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
2717 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
2718 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
2721 enum { /* TPACPI_VIDEO_570 constants */
2722 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
2723 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
2724 * video_status_flags */
2725 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
2726 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
2729 static enum video_access_mode video_supported
;
2730 static int video_orig_autosw
;
2732 static int video_autosw_get(void);
2733 static int video_autosw_set(int enable
);
2735 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2737 static int __init
video_init(struct ibm_init_struct
*iibm
)
2741 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
2743 TPACPI_ACPIHANDLE_INIT(vid
);
2744 TPACPI_ACPIHANDLE_INIT(vid2
);
2746 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
2747 /* G41, assume IVGA doesn't change */
2748 vid_handle
= vid2_handle
;
2751 /* video switching not supported on R30, R31 */
2752 video_supported
= TPACPI_VIDEO_NONE
;
2753 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
2755 video_supported
= TPACPI_VIDEO_570
;
2756 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
2757 /* 600e/x, 770e, 770x */
2758 video_supported
= TPACPI_VIDEO_770
;
2761 video_supported
= TPACPI_VIDEO_NEW
;
2763 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
2764 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
2767 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
2770 static void video_exit(void)
2772 dbg_printk(TPACPI_DBG_EXIT
,
2773 "restoring original video autoswitch mode\n");
2774 if (video_autosw_set(video_orig_autosw
))
2775 printk(TPACPI_ERR
"error while trying to restore original "
2776 "video autoswitch mode\n");
2779 static int video_outputsw_get(void)
2784 switch (video_supported
) {
2785 case TPACPI_VIDEO_570
:
2786 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
2787 TP_ACPI_VIDEO_570_PHSCMD
))
2789 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
2791 case TPACPI_VIDEO_770
:
2792 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
2795 status
|= TP_ACPI_VIDEO_S_LCD
;
2796 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
2799 status
|= TP_ACPI_VIDEO_S_CRT
;
2801 case TPACPI_VIDEO_NEW
:
2802 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
2803 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
2806 status
|= TP_ACPI_VIDEO_S_CRT
;
2808 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
2809 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
2812 status
|= TP_ACPI_VIDEO_S_LCD
;
2813 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
2816 status
|= TP_ACPI_VIDEO_S_DVI
;
2825 static int video_outputsw_set(int status
)
2830 switch (video_supported
) {
2831 case TPACPI_VIDEO_570
:
2832 res
= acpi_evalf(NULL
, NULL
,
2833 "\\_SB.PHS2", "vdd",
2834 TP_ACPI_VIDEO_570_PHS2CMD
,
2835 status
| TP_ACPI_VIDEO_570_PHS2SET
);
2837 case TPACPI_VIDEO_770
:
2838 autosw
= video_autosw_get();
2842 res
= video_autosw_set(1);
2845 res
= acpi_evalf(vid_handle
, NULL
,
2846 "ASWT", "vdd", status
* 0x100, 0);
2847 if (!autosw
&& video_autosw_set(autosw
)) {
2849 "video auto-switch left enabled due to error\n");
2853 case TPACPI_VIDEO_NEW
:
2854 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
2855 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
2861 return (res
)? 0 : -EIO
;
2864 static int video_autosw_get(void)
2868 switch (video_supported
) {
2869 case TPACPI_VIDEO_570
:
2870 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
2873 case TPACPI_VIDEO_770
:
2874 case TPACPI_VIDEO_NEW
:
2875 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
2885 static int video_autosw_set(int enable
)
2887 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
2892 static int video_outputsw_cycle(void)
2894 int autosw
= video_autosw_get();
2900 switch (video_supported
) {
2901 case TPACPI_VIDEO_570
:
2902 res
= video_autosw_set(1);
2905 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
2907 case TPACPI_VIDEO_770
:
2908 case TPACPI_VIDEO_NEW
:
2909 res
= video_autosw_set(1);
2912 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
2917 if (!autosw
&& video_autosw_set(autosw
)) {
2919 "video auto-switch left enabled due to error\n");
2923 return (res
)? 0 : -EIO
;
2926 static int video_expand_toggle(void)
2928 switch (video_supported
) {
2929 case TPACPI_VIDEO_570
:
2930 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
2932 case TPACPI_VIDEO_770
:
2933 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
2935 case TPACPI_VIDEO_NEW
:
2936 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
2944 static int video_read(char *p
)
2949 if (video_supported
== TPACPI_VIDEO_NONE
) {
2950 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2954 status
= video_outputsw_get();
2958 autosw
= video_autosw_get();
2962 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
2963 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
2964 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
2965 if (video_supported
== TPACPI_VIDEO_NEW
)
2966 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
2967 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
2968 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
2969 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
2970 if (video_supported
== TPACPI_VIDEO_NEW
)
2971 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
2972 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
2973 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
2978 static int video_write(char *buf
)
2981 int enable
, disable
, status
;
2984 if (video_supported
== TPACPI_VIDEO_NONE
)
2990 while ((cmd
= next_cmd(&buf
))) {
2991 if (strlencmp(cmd
, "lcd_enable") == 0) {
2992 enable
|= TP_ACPI_VIDEO_S_LCD
;
2993 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
2994 disable
|= TP_ACPI_VIDEO_S_LCD
;
2995 } else if (strlencmp(cmd
, "crt_enable") == 0) {
2996 enable
|= TP_ACPI_VIDEO_S_CRT
;
2997 } else if (strlencmp(cmd
, "crt_disable") == 0) {
2998 disable
|= TP_ACPI_VIDEO_S_CRT
;
2999 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3000 strlencmp(cmd
, "dvi_enable") == 0) {
3001 enable
|= TP_ACPI_VIDEO_S_DVI
;
3002 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3003 strlencmp(cmd
, "dvi_disable") == 0) {
3004 disable
|= TP_ACPI_VIDEO_S_DVI
;
3005 } else if (strlencmp(cmd
, "auto_enable") == 0) {
3006 res
= video_autosw_set(1);
3009 } else if (strlencmp(cmd
, "auto_disable") == 0) {
3010 res
= video_autosw_set(0);
3013 } else if (strlencmp(cmd
, "video_switch") == 0) {
3014 res
= video_outputsw_cycle();
3017 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
3018 res
= video_expand_toggle();
3025 if (enable
|| disable
) {
3026 status
= video_outputsw_get();
3029 res
= video_outputsw_set((status
& ~disable
) | enable
);
3037 static struct ibm_struct video_driver_data
= {
3040 .write
= video_write
,
3044 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3046 /*************************************************************************
3047 * Light (thinklight) subdriver
3050 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3051 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
3053 static int __init
light_init(struct ibm_init_struct
*iibm
)
3055 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
3057 TPACPI_ACPIHANDLE_INIT(ledb
);
3058 TPACPI_ACPIHANDLE_INIT(lght
);
3059 TPACPI_ACPIHANDLE_INIT(cmos
);
3061 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3062 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
3064 if (tp_features
.light
)
3065 /* light status not supported on
3066 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3067 tp_features
.light_status
=
3068 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
3070 vdbg_printk(TPACPI_DBG_INIT
, "light is %s\n",
3071 str_supported(tp_features
.light
));
3073 return (tp_features
.light
)? 0 : 1;
3076 static int light_read(char *p
)
3081 if (!tp_features
.light
) {
3082 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3083 } else if (!tp_features
.light_status
) {
3084 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
3085 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3087 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
3089 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
3090 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3096 static int light_write(char *buf
)
3098 int cmos_cmd
, lght_cmd
;
3102 if (!tp_features
.light
)
3105 while ((cmd
= next_cmd(&buf
))) {
3106 if (strlencmp(cmd
, "on") == 0) {
3109 } else if (strlencmp(cmd
, "off") == 0) {
3115 success
= cmos_handle
?
3116 acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
) :
3117 acpi_evalf(lght_handle
, NULL
, NULL
, "vd", lght_cmd
);
3125 static struct ibm_struct light_driver_data
= {
3128 .write
= light_write
,
3131 /*************************************************************************
3135 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3137 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
3138 static int dock_read(char *p
);
3139 static int dock_write(char *buf
);
3141 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
3142 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3143 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3144 "\\_SB.PCI.ISA.SLCE", /* 570 */
3145 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3147 /* don't list other alternatives as we install a notify handler on the 570 */
3148 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
3150 static const struct acpi_device_id ibm_pci_device_ids
[] = {
3151 {PCI_ROOT_HID_STRING
, 0},
3155 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
3157 .notify
= dock_notify
,
3158 .handle
= &dock_handle
,
3159 .type
= ACPI_SYSTEM_NOTIFY
,
3162 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3163 * We just use it to get notifications of dock hotplug
3164 * in very old thinkpads */
3165 .hid
= ibm_pci_device_ids
,
3166 .notify
= dock_notify
,
3167 .handle
= &pci_handle
,
3168 .type
= ACPI_SYSTEM_NOTIFY
,
3172 static struct ibm_struct dock_driver_data
[2] = {
3176 .write
= dock_write
,
3177 .acpi
= &ibm_dock_acpidriver
[0],
3181 .acpi
= &ibm_dock_acpidriver
[1],
3185 #define dock_docked() (_sta(dock_handle) & 1)
3187 static int __init
dock_init(struct ibm_init_struct
*iibm
)
3189 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
3191 TPACPI_ACPIHANDLE_INIT(dock
);
3193 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
3194 str_supported(dock_handle
!= NULL
));
3196 return (dock_handle
)? 0 : 1;
3199 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
3203 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
3205 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
3206 dock_driver_data
[0].flags
.acpi_notify_installed
) {
3207 TPACPI_ACPIHANDLE_INIT(pci
);
3208 dock2_needed
= (pci_handle
!= NULL
);
3209 vdbg_printk(TPACPI_DBG_INIT
,
3210 "dock PCI handler for the TP 570 is %s\n",
3211 str_supported(dock2_needed
));
3213 vdbg_printk(TPACPI_DBG_INIT
,
3214 "dock subdriver part 2 not required\n");
3218 return (dock2_needed
)? 0 : 1;
3221 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
3223 int docked
= dock_docked();
3224 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
3225 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
3228 if (event
== 1 && !pci
) /* 570 */
3229 data
= 1; /* button */
3230 else if (event
== 1 && pci
) /* 570 */
3231 data
= 3; /* dock */
3232 else if (event
== 3 && docked
)
3233 data
= 1; /* button */
3234 else if (event
== 3 && !docked
)
3235 data
= 2; /* undock */
3236 else if (event
== 0 && docked
)
3237 data
= 3; /* dock */
3239 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
3240 event
, _sta(dock_handle
));
3241 data
= 0; /* unknown */
3243 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
3244 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
3245 ibm
->acpi
->device
->dev
.bus_id
,
3249 static int dock_read(char *p
)
3252 int docked
= dock_docked();
3255 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3257 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
3259 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
3260 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
3266 static int dock_write(char *buf
)
3273 while ((cmd
= next_cmd(&buf
))) {
3274 if (strlencmp(cmd
, "undock") == 0) {
3275 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
3276 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
3278 } else if (strlencmp(cmd
, "dock") == 0) {
3279 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
3288 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3290 /*************************************************************************
3294 #ifdef CONFIG_THINKPAD_ACPI_BAY
3296 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3297 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3298 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3299 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3300 ); /* A21e, R30, R31 */
3301 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
3302 "_EJ0", /* all others */
3303 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3304 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3305 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3307 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
3311 static int __init
bay_init(struct ibm_init_struct
*iibm
)
3313 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
3315 TPACPI_ACPIHANDLE_INIT(bay
);
3317 TPACPI_ACPIHANDLE_INIT(bay_ej
);
3318 TPACPI_ACPIHANDLE_INIT(bay2
);
3320 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
3322 tp_features
.bay_status
= bay_handle
&&
3323 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
3324 tp_features
.bay_status2
= bay2_handle
&&
3325 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
3327 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
3328 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
3329 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
3330 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
3332 vdbg_printk(TPACPI_DBG_INIT
,
3333 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3334 str_supported(tp_features
.bay_status
),
3335 str_supported(tp_features
.bay_eject
),
3336 str_supported(tp_features
.bay_status2
),
3337 str_supported(tp_features
.bay_eject2
));
3339 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
3340 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
3343 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
3345 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
3346 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
3347 ibm
->acpi
->device
->dev
.bus_id
,
3351 #define bay_occupied(b) (_sta(b##_handle) & 1)
3353 static int bay_read(char *p
)
3356 int occupied
= bay_occupied(bay
);
3357 int occupied2
= bay_occupied(bay2
);
3360 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3361 tp_features
.bay_status
?
3362 (occupied
? "occupied" : "unoccupied") :
3364 if (tp_features
.bay_status2
)
3365 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
3366 "occupied" : "unoccupied");
3368 eject
= tp_features
.bay_eject
&& occupied
;
3369 eject2
= tp_features
.bay_eject2
&& occupied2
;
3371 if (eject
&& eject2
)
3372 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
3374 len
+= sprintf(p
+ len
, "commands:\teject\n");
3376 len
+= sprintf(p
+ len
, "commands:\teject2\n");
3381 static int bay_write(char *buf
)
3385 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
3388 while ((cmd
= next_cmd(&buf
))) {
3389 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
3390 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
3392 } else if (tp_features
.bay_eject2
&&
3393 strlencmp(cmd
, "eject2") == 0) {
3394 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
3403 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
3404 .notify
= bay_notify
,
3405 .handle
= &bay_handle
,
3406 .type
= ACPI_SYSTEM_NOTIFY
,
3409 static struct ibm_struct bay_driver_data
= {
3413 .acpi
= &ibm_bay_acpidriver
,
3416 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3418 /*************************************************************************
3422 /* sysfs cmos_command -------------------------------------------------- */
3423 static ssize_t
cmos_command_store(struct device
*dev
,
3424 struct device_attribute
*attr
,
3425 const char *buf
, size_t count
)
3427 unsigned long cmos_cmd
;
3430 if (parse_strtoul(buf
, 21, &cmos_cmd
))
3433 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3434 return (res
)? res
: count
;
3437 static struct device_attribute dev_attr_cmos_command
=
3438 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
3440 /* --------------------------------------------------------------------- */
3442 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
3446 vdbg_printk(TPACPI_DBG_INIT
,
3447 "initializing cmos commands subdriver\n");
3449 TPACPI_ACPIHANDLE_INIT(cmos
);
3451 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
3452 str_supported(cmos_handle
!= NULL
));
3454 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3458 return (cmos_handle
)? 0 : 1;
3461 static void cmos_exit(void)
3463 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3466 static int cmos_read(char *p
)
3470 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3471 R30, R31, T20-22, X20-21 */
3473 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3475 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3476 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
3482 static int cmos_write(char *buf
)
3487 while ((cmd
= next_cmd(&buf
))) {
3488 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
3489 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
3494 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3502 static struct ibm_struct cmos_driver_data
= {
3505 .write
= cmos_write
,
3509 /*************************************************************************
3513 enum led_access_mode
{
3514 TPACPI_LED_NONE
= 0,
3515 TPACPI_LED_570
, /* 570 */
3516 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3517 TPACPI_LED_NEW
, /* all others */
3520 enum { /* For TPACPI_LED_OLD */
3521 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
3522 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
3523 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
3526 static enum led_access_mode led_supported
;
3528 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
3529 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3530 /* T20-22, X20-21 */
3531 "LED", /* all others */
3534 static int __init
led_init(struct ibm_init_struct
*iibm
)
3536 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
3538 TPACPI_ACPIHANDLE_INIT(led
);
3541 /* led not supported on R30, R31 */
3542 led_supported
= TPACPI_LED_NONE
;
3543 else if (strlencmp(led_path
, "SLED") == 0)
3545 led_supported
= TPACPI_LED_570
;
3546 else if (strlencmp(led_path
, "SYSL") == 0)
3547 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3548 led_supported
= TPACPI_LED_OLD
;
3551 led_supported
= TPACPI_LED_NEW
;
3553 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
3554 str_supported(led_supported
), led_supported
);
3556 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
3559 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3561 static int led_read(char *p
)
3565 if (!led_supported
) {
3566 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3569 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3571 if (led_supported
== TPACPI_LED_570
) {
3574 for (i
= 0; i
< 8; i
++) {
3575 if (!acpi_evalf(ec_handle
,
3576 &status
, "GLED", "dd", 1 << i
))
3578 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
3579 i
, led_status(status
));
3583 len
+= sprintf(p
+ len
, "commands:\t"
3584 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3589 /* off, on, blink */
3590 static const int led_sled_arg1
[] = { 0, 1, 3 };
3591 static const int led_exp_hlbl
[] = { 0, 0, 1 }; /* led# * */
3592 static const int led_exp_hlcl
[] = { 0, 1, 1 }; /* led# * */
3593 static const int led_led_arg1
[] = { 0, 0x80, 0xc0 };
3595 static int led_write(char *buf
)
3603 while ((cmd
= next_cmd(&buf
))) {
3604 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
3607 if (strstr(cmd
, "off")) {
3609 } else if (strstr(cmd
, "on")) {
3611 } else if (strstr(cmd
, "blink")) {
3616 if (led_supported
== TPACPI_LED_570
) {
3619 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
3620 led
, led_sled_arg1
[ind
]))
3622 } else if (led_supported
== TPACPI_LED_OLD
) {
3623 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3625 ret
= ec_write(TPACPI_LED_EC_HLMS
, led
);
3627 ret
= ec_write(TPACPI_LED_EC_HLBL
,
3628 led
* led_exp_hlbl
[ind
]);
3630 ret
= ec_write(TPACPI_LED_EC_HLCL
,
3631 led
* led_exp_hlcl
[ind
]);
3636 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
3637 led
, led_led_arg1
[ind
]))
3645 static struct ibm_struct led_driver_data
= {
3651 /*************************************************************************
3655 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
3657 static int __init
beep_init(struct ibm_init_struct
*iibm
)
3659 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
3661 TPACPI_ACPIHANDLE_INIT(beep
);
3663 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
3664 str_supported(beep_handle
!= NULL
));
3666 return (beep_handle
)? 0 : 1;
3669 static int beep_read(char *p
)
3674 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3676 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3677 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
3683 static int beep_write(char *buf
)
3691 while ((cmd
= next_cmd(&buf
))) {
3692 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
3693 beep_cmd
>= 0 && beep_cmd
<= 17) {
3697 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
3704 static struct ibm_struct beep_driver_data
= {
3707 .write
= beep_write
,
3710 /*************************************************************************
3714 enum thermal_access_mode
{
3715 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
3716 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
3717 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
3718 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
3719 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
3722 enum { /* TPACPI_THERMAL_TPEC_* */
3723 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
3724 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
3725 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
3728 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3729 struct ibm_thermal_sensors_struct
{
3730 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
3733 static enum thermal_access_mode thermal_read_mode
;
3735 /* idx is zero-based */
3736 static int thermal_get_sensor(int idx
, s32
*value
)
3742 t
= TP_EC_THERMAL_TMP0
;
3744 switch (thermal_read_mode
) {
3745 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3746 case TPACPI_THERMAL_TPEC_16
:
3747 if (idx
>= 8 && idx
<= 15) {
3748 t
= TP_EC_THERMAL_TMP8
;
3753 case TPACPI_THERMAL_TPEC_8
:
3755 if (!acpi_ec_read(t
+ idx
, &tmp
))
3757 *value
= tmp
* 1000;
3762 case TPACPI_THERMAL_ACPI_UPDT
:
3764 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
3765 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
3767 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
3769 *value
= (t
- 2732) * 100;
3774 case TPACPI_THERMAL_ACPI_TMP07
:
3776 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
3777 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
3779 if (t
> 127 || t
< -127)
3780 t
= TP_EC_THERMAL_TMP_NA
;
3786 case TPACPI_THERMAL_NONE
:
3794 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
3805 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
3808 for (i
= 0 ; i
< n
; i
++) {
3809 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
3817 /* sysfs temp##_input -------------------------------------------------- */
3819 static ssize_t
thermal_temp_input_show(struct device
*dev
,
3820 struct device_attribute
*attr
,
3823 struct sensor_device_attribute
*sensor_attr
=
3824 to_sensor_dev_attr(attr
);
3825 int idx
= sensor_attr
->index
;
3829 res
= thermal_get_sensor(idx
, &value
);
3832 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
3835 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
3838 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3839 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3840 thermal_temp_input_show, NULL, _idxB)
3842 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
3843 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3844 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3845 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3846 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3847 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3848 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3849 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3850 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3851 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3852 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3853 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3854 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3855 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3856 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3857 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3858 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3861 #define THERMAL_ATTRS(X) \
3862 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3864 static struct attribute
*thermal_temp_input_attr
[] = {
3884 static const struct attribute_group thermal_temp_input16_group
= {
3885 .attrs
= thermal_temp_input_attr
3888 static const struct attribute_group thermal_temp_input8_group
= {
3889 .attrs
= &thermal_temp_input_attr
[8]
3892 #undef THERMAL_SENSOR_ATTR_TEMP
3893 #undef THERMAL_ATTRS
3895 /* --------------------------------------------------------------------- */
3897 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
3904 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
3906 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
3908 if (thinkpad_id
.ec_model
) {
3910 * Direct EC access mode: sensors at registers
3911 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3912 * non-implemented, thermal sensors return 0x80 when
3917 for (i
= 0; i
< 8; i
++) {
3918 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
3924 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
3932 /* This is sheer paranoia, but we handle it anyway */
3935 "ThinkPad ACPI EC access misbehaving, "
3936 "falling back to ACPI TMPx access "
3938 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
3941 "ThinkPad ACPI EC access misbehaving, "
3942 "disabling thermal sensors access\n");
3943 thermal_read_mode
= TPACPI_THERMAL_NONE
;
3948 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
3950 } else if (acpi_tmp7
) {
3951 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
3952 /* 600e/x, 770e, 770x */
3953 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
3955 /* Standard ACPI TMPx access, max 8 sensors */
3956 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
3959 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3960 thermal_read_mode
= TPACPI_THERMAL_NONE
;
3963 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
3964 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
3967 switch (thermal_read_mode
) {
3968 case TPACPI_THERMAL_TPEC_16
:
3969 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
3970 &thermal_temp_input16_group
);
3974 case TPACPI_THERMAL_TPEC_8
:
3975 case TPACPI_THERMAL_ACPI_TMP07
:
3976 case TPACPI_THERMAL_ACPI_UPDT
:
3977 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
3978 &thermal_temp_input8_group
);
3982 case TPACPI_THERMAL_NONE
:
3990 static void thermal_exit(void)
3992 switch (thermal_read_mode
) {
3993 case TPACPI_THERMAL_TPEC_16
:
3994 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
3995 &thermal_temp_input16_group
);
3997 case TPACPI_THERMAL_TPEC_8
:
3998 case TPACPI_THERMAL_ACPI_TMP07
:
3999 case TPACPI_THERMAL_ACPI_UPDT
:
4000 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4001 &thermal_temp_input16_group
);
4003 case TPACPI_THERMAL_NONE
:
4009 static int thermal_read(char *p
)
4013 struct ibm_thermal_sensors_struct t
;
4015 n
= thermal_get_sensors(&t
);
4016 if (unlikely(n
< 0))
4019 len
+= sprintf(p
+ len
, "temperatures:\t");
4022 for (i
= 0; i
< (n
- 1); i
++)
4023 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
4024 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
4026 len
+= sprintf(p
+ len
, "not supported\n");
4031 static struct ibm_struct thermal_driver_data
= {
4033 .read
= thermal_read
,
4034 .exit
= thermal_exit
,
4037 /*************************************************************************
4041 static u8 ecdump_regs
[256];
4043 static int ecdump_read(char *p
)
4049 len
+= sprintf(p
+ len
, "EC "
4050 " +00 +01 +02 +03 +04 +05 +06 +07"
4051 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4052 for (i
= 0; i
< 256; i
+= 16) {
4053 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
4054 for (j
= 0; j
< 16; j
++) {
4055 if (!acpi_ec_read(i
+ j
, &v
))
4057 if (v
!= ecdump_regs
[i
+ j
])
4058 len
+= sprintf(p
+ len
, " *%02x", v
);
4060 len
+= sprintf(p
+ len
, " %02x", v
);
4061 ecdump_regs
[i
+ j
] = v
;
4063 len
+= sprintf(p
+ len
, "\n");
4068 /* These are way too dangerous to advertise openly... */
4070 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
4071 " (<offset> is 00-ff, <value> is 00-ff)\n");
4072 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
4073 " (<offset> is 00-ff, <value> is 0-255)\n");
4078 static int ecdump_write(char *buf
)
4083 while ((cmd
= next_cmd(&buf
))) {
4084 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
4086 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
4090 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
4091 if (!acpi_ec_write(i
, v
))
4100 static struct ibm_struct ecdump_driver_data
= {
4102 .read
= ecdump_read
,
4103 .write
= ecdump_write
,
4104 .flags
.experimental
= 1,
4107 /*************************************************************************
4108 * Backlight/brightness subdriver
4111 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4113 static struct backlight_device
*ibm_backlight_device
;
4114 static int brightness_offset
= 0x31;
4115 static int brightness_mode
;
4116 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
4118 static struct mutex brightness_mutex
;
4121 * ThinkPads can read brightness from two places: EC 0x31, or
4122 * CMOS NVRAM byte 0x5E, bits 0-3.
4124 static int brightness_get(struct backlight_device
*bd
)
4126 u8 lec
= 0, lcmos
= 0, level
= 0;
4128 if (brightness_mode
& 1) {
4129 if (!acpi_ec_read(brightness_offset
, &lec
))
4131 lec
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4134 if (brightness_mode
& 2) {
4135 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
4136 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
4137 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
4138 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4142 if (brightness_mode
== 3 && lec
!= lcmos
) {
4144 "CMOS NVRAM (%u) and EC (%u) do not agree "
4145 "on display brightness level\n",
4146 (unsigned int) lcmos
,
4147 (unsigned int) lec
);
4154 /* May return EINTR which can always be mapped to ERESTARTSYS */
4155 static int brightness_set(int value
)
4157 int cmos_cmd
, inc
, i
, res
;
4160 if (value
> ((tp_features
.bright_16levels
)? 15 : 7))
4163 res
= mutex_lock_interruptible(&brightness_mutex
);
4167 current_value
= brightness_get(NULL
);
4168 if (current_value
< 0) {
4169 res
= current_value
;
4173 cmos_cmd
= value
> current_value
?
4174 TP_CMOS_BRIGHTNESS_UP
:
4175 TP_CMOS_BRIGHTNESS_DOWN
;
4176 inc
= (value
> current_value
)? 1 : -1;
4179 for (i
= current_value
; i
!= value
; i
+= inc
) {
4180 if ((brightness_mode
& 2) &&
4181 issue_thinkpad_cmos_command(cmos_cmd
)) {
4185 if ((brightness_mode
& 1) &&
4186 !acpi_ec_write(brightness_offset
, i
+ inc
)) {
4193 mutex_unlock(&brightness_mutex
);
4197 /* sysfs backlight class ----------------------------------------------- */
4199 static int brightness_update_status(struct backlight_device
*bd
)
4201 /* it is the backlight class's job (caller) to handle
4202 * EINTR and other errors properly */
4203 return brightness_set(
4204 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
4205 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
4206 bd
->props
.brightness
: 0);
4209 static struct backlight_ops ibm_backlight_data
= {
4210 .get_brightness
= brightness_get
,
4211 .update_status
= brightness_update_status
,
4214 /* --------------------------------------------------------------------- */
4216 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
4218 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
4219 union acpi_object
*obj
;
4222 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
4223 obj
= (union acpi_object
*)buffer
.pointer
;
4224 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
4225 printk(TPACPI_ERR
"Unknown _BCL data, "
4226 "please report this to %s\n", TPACPI_MAIL
);
4229 rc
= obj
->package
.count
;
4235 kfree(buffer
.pointer
);
4239 static acpi_status __init
brightness_find_bcl(acpi_handle handle
, u32 lvl
,
4240 void *context
, void **rv
)
4242 char name
[ACPI_PATH_SEGMENT_LENGTH
];
4243 struct acpi_buffer buffer
= { sizeof(name
), &name
};
4245 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
4246 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
4247 BUG_ON(!rv
|| !*rv
);
4248 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
4249 return AE_CTRL_TERMINATE
;
4256 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
4258 static int __init
brightness_check_std_acpi_support(void)
4262 void *bcl_ptr
= &bcl_levels
;
4265 TPACPI_ACPIHANDLE_INIT(vid
);
4271 * Search for a _BCL method, and execute it. This is safe on all
4272 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
4273 * BIOS in ACPI backlight control mode. We do NOT have to care
4274 * about calling the _BCL method in an enabled video device, any
4275 * will do for our purposes.
4278 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
4279 brightness_find_bcl
, NULL
, &bcl_ptr
);
4281 if (ACPI_SUCCESS(status
) && bcl_levels
> 2)
4282 return (bcl_levels
- 2);
4287 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
4291 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
4293 mutex_init(&brightness_mutex
);
4296 * We always attempt to detect acpi support, so as to switch
4297 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4298 * going to publish a backlight interface
4300 b
= brightness_check_std_acpi_support();
4302 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
4303 printk(TPACPI_NOTICE
4304 "Lenovo BIOS switched to ACPI backlight "
4307 if (brightness_enable
> 1) {
4308 printk(TPACPI_NOTICE
4309 "standard ACPI backlight interface "
4310 "available, not loading native one...\n");
4315 if (!brightness_enable
) {
4316 dbg_printk(TPACPI_DBG_INIT
,
4317 "brightness support disabled by "
4318 "module parameter\n");
4324 "Unsupported brightness interface, "
4325 "please contact %s\n", TPACPI_MAIL
);
4329 tp_features
.bright_16levels
= 1;
4331 if (!brightness_mode
) {
4332 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
4333 brightness_mode
= 2;
4335 brightness_mode
= 3;
4337 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
4341 if (brightness_mode
> 3)
4344 b
= brightness_get(NULL
);
4348 if (tp_features
.bright_16levels
)
4350 "detected a 16-level brightness capable ThinkPad\n");
4352 ibm_backlight_device
= backlight_device_register(
4353 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
4354 &ibm_backlight_data
);
4355 if (IS_ERR(ibm_backlight_device
)) {
4356 printk(TPACPI_ERR
"Could not register backlight device\n");
4357 return PTR_ERR(ibm_backlight_device
);
4359 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
4361 ibm_backlight_device
->props
.max_brightness
=
4362 (tp_features
.bright_16levels
)? 15 : 7;
4363 ibm_backlight_device
->props
.brightness
= b
;
4364 backlight_update_status(ibm_backlight_device
);
4369 static void brightness_exit(void)
4371 if (ibm_backlight_device
) {
4372 vdbg_printk(TPACPI_DBG_EXIT
,
4373 "calling backlight_device_unregister()\n");
4374 backlight_device_unregister(ibm_backlight_device
);
4375 ibm_backlight_device
= NULL
;
4379 static int brightness_read(char *p
)
4384 level
= brightness_get(NULL
);
4386 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
4388 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
4389 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
4390 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
4391 " (<level> is 0-%d)\n",
4392 (tp_features
.bright_16levels
) ? 15 : 7);
4398 static int brightness_write(char *buf
)
4403 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
4405 level
= brightness_get(NULL
);
4409 while ((cmd
= next_cmd(&buf
))) {
4410 if (strlencmp(cmd
, "up") == 0) {
4411 if (level
< max_level
)
4413 } else if (strlencmp(cmd
, "down") == 0) {
4416 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
4417 level
>= 0 && level
<= max_level
) {
4424 * Now we know what the final level should be, so we try to set it.
4425 * Doing it this way makes the syscall restartable in case of EINTR
4427 rc
= brightness_set(level
);
4428 return (rc
== -EINTR
)? ERESTARTSYS
: rc
;
4431 static struct ibm_struct brightness_driver_data
= {
4432 .name
= "brightness",
4433 .read
= brightness_read
,
4434 .write
= brightness_write
,
4435 .exit
= brightness_exit
,
4438 /*************************************************************************
4442 static int volume_offset
= 0x30;
4444 static int volume_read(char *p
)
4449 if (!acpi_ec_read(volume_offset
, &level
)) {
4450 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
4452 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
4453 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
4454 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
4455 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
4456 " (<level> is 0-15)\n");
4462 static int volume_write(char *buf
)
4464 int cmos_cmd
, inc
, i
;
4466 int new_level
, new_mute
;
4469 while ((cmd
= next_cmd(&buf
))) {
4470 if (!acpi_ec_read(volume_offset
, &level
))
4472 new_mute
= mute
= level
& 0x40;
4473 new_level
= level
= level
& 0xf;
4475 if (strlencmp(cmd
, "up") == 0) {
4479 new_level
= level
== 15 ? 15 : level
+ 1;
4480 } else if (strlencmp(cmd
, "down") == 0) {
4484 new_level
= level
== 0 ? 0 : level
- 1;
4485 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
4486 new_level
>= 0 && new_level
<= 15) {
4488 } else if (strlencmp(cmd
, "mute") == 0) {
4493 if (new_level
!= level
) {
4494 /* mute doesn't change */
4496 cmos_cmd
= (new_level
> level
) ?
4497 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
4498 inc
= new_level
> level
? 1 : -1;
4500 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
4501 !acpi_ec_write(volume_offset
, level
)))
4504 for (i
= level
; i
!= new_level
; i
+= inc
)
4505 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
4506 !acpi_ec_write(volume_offset
, i
+ inc
))
4510 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
4511 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
4516 if (new_mute
!= mute
) {
4517 /* level doesn't change */
4519 cmos_cmd
= (new_mute
) ?
4520 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
4522 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
4523 !acpi_ec_write(volume_offset
, level
+ new_mute
))
4531 static struct ibm_struct volume_driver_data
= {
4533 .read
= volume_read
,
4534 .write
= volume_write
,
4537 /*************************************************************************
4544 * TPACPI_FAN_RD_ACPI_GFAN:
4545 * ACPI GFAN method: returns fan level
4547 * see TPACPI_FAN_WR_ACPI_SFAN
4548 * EC 0x2f (HFSP) not available if GFAN exists
4550 * TPACPI_FAN_WR_ACPI_SFAN:
4551 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4553 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4556 * TPACPI_FAN_WR_TPEC:
4557 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4558 * Supported on almost all ThinkPads
4560 * Fan speed changes of any sort (including those caused by the
4561 * disengaged mode) are usually done slowly by the firmware as the
4562 * maximum ammount of fan duty cycle change per second seems to be
4565 * Reading is not available if GFAN exists.
4566 * Writing is not available if SFAN exists.
4569 * 7 automatic mode engaged;
4570 * (default operation mode of the ThinkPad)
4571 * fan level is ignored in this mode.
4572 * 6 full speed mode (takes precedence over bit 7);
4573 * not available on all thinkpads. May disable
4574 * the tachometer while the fan controller ramps up
4575 * the speed (which can take up to a few *minutes*).
4576 * Speeds up fan to 100% duty-cycle, which is far above
4577 * the standard RPM levels. It is not impossible that
4578 * it could cause hardware damage.
4579 * 5-3 unused in some models. Extra bits for fan level
4580 * in others, but still useless as all values above
4581 * 7 map to the same speed as level 7 in these models.
4582 * 2-0 fan level (0..7 usually)
4584 * 0x07 = max (set when temperatures critical)
4585 * Some ThinkPads may have other levels, see
4586 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4588 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4589 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4590 * does so, its initial value is meaningless (0x07).
4592 * For firmware bugs, refer to:
4593 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4597 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4598 * Main fan tachometer reading (in RPM)
4600 * This register is present on all ThinkPads with a new-style EC, and
4601 * it is known not to be present on the A21m/e, and T22, as there is
4602 * something else in offset 0x84 according to the ACPI DSDT. Other
4603 * ThinkPads from this same time period (and earlier) probably lack the
4604 * tachometer as well.
4606 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4607 * was never fixed by IBM to report the EC firmware version string
4608 * probably support the tachometer (like the early X models), so
4609 * detecting it is quite hard. We need more data to know for sure.
4611 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4614 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4617 * For firmware bugs, refer to:
4618 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4620 * TPACPI_FAN_WR_ACPI_FANS:
4621 * ThinkPad X31, X40, X41. Not available in the X60.
4623 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4624 * high speed. ACPI DSDT seems to map these three speeds to levels
4625 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4626 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4628 * The speeds are stored on handles
4629 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4631 * There are three default speed sets, acessible as handles:
4632 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4634 * ACPI DSDT switches which set is in use depending on various
4637 * TPACPI_FAN_WR_TPEC is also available and should be used to
4638 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4639 * but the ACPI tables just mention level 7.
4642 enum { /* Fan control constants */
4643 fan_status_offset
= 0x2f, /* EC register 0x2f */
4644 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4645 * 0x84 must be read before 0x85 */
4647 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
4648 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
4650 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
4653 enum fan_status_access_mode
{
4654 TPACPI_FAN_NONE
= 0, /* No fan status or control */
4655 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
4656 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4659 enum fan_control_access_mode
{
4660 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
4661 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
4662 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
4663 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
4666 enum fan_control_commands
{
4667 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
4668 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
4669 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
4670 * and also watchdog cmd */
4673 static int fan_control_allowed
;
4675 static enum fan_status_access_mode fan_status_access_mode
;
4676 static enum fan_control_access_mode fan_control_access_mode
;
4677 static enum fan_control_commands fan_control_commands
;
4679 static u8 fan_control_initial_status
;
4680 static u8 fan_control_desired_level
;
4681 static int fan_watchdog_maxinterval
;
4683 static struct mutex fan_mutex
;
4685 static void fan_watchdog_fire(struct work_struct
*ignored
);
4686 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
4688 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
4689 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
4690 "\\FSPD", /* 600e/x, 770e, 770x */
4692 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
4693 "JFNS", /* 770x-JL */
4697 * Call with fan_mutex held
4699 static void fan_update_desired_level(u8 status
)
4702 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
4704 fan_control_desired_level
= 7;
4706 fan_control_desired_level
= status
;
4710 static int fan_get_status(u8
*status
)
4715 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4717 switch (fan_status_access_mode
) {
4718 case TPACPI_FAN_RD_ACPI_GFAN
:
4719 /* 570, 600e/x, 770e, 770x */
4721 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
4729 case TPACPI_FAN_RD_TPEC
:
4730 /* all except 570, 600e/x, 770e, 770x */
4731 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
4746 static int fan_get_status_safe(u8
*status
)
4751 if (mutex_lock_interruptible(&fan_mutex
))
4752 return -ERESTARTSYS
;
4753 rc
= fan_get_status(&s
);
4755 fan_update_desired_level(s
);
4756 mutex_unlock(&fan_mutex
);
4764 static int fan_get_speed(unsigned int *speed
)
4768 switch (fan_status_access_mode
) {
4769 case TPACPI_FAN_RD_TPEC
:
4770 /* all except 570, 600e/x, 770e, 770x */
4771 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
4772 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
4776 *speed
= (hi
<< 8) | lo
;
4787 static int fan_set_level(int level
)
4789 if (!fan_control_allowed
)
4792 switch (fan_control_access_mode
) {
4793 case TPACPI_FAN_WR_ACPI_SFAN
:
4794 if (level
>= 0 && level
<= 7) {
4795 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
4801 case TPACPI_FAN_WR_ACPI_FANS
:
4802 case TPACPI_FAN_WR_TPEC
:
4803 if ((level
!= TP_EC_FAN_AUTO
) &&
4804 (level
!= TP_EC_FAN_FULLSPEED
) &&
4805 ((level
< 0) || (level
> 7)))
4808 /* safety net should the EC not support AUTO
4809 * or FULLSPEED mode bits and just ignore them */
4810 if (level
& TP_EC_FAN_FULLSPEED
)
4811 level
|= 7; /* safety min speed 7 */
4812 else if (level
& TP_EC_FAN_AUTO
)
4813 level
|= 4; /* safety min speed 4 */
4815 if (!acpi_ec_write(fan_status_offset
, level
))
4818 tp_features
.fan_ctrl_status_undef
= 0;
4827 static int fan_set_level_safe(int level
)
4831 if (!fan_control_allowed
)
4834 if (mutex_lock_interruptible(&fan_mutex
))
4835 return -ERESTARTSYS
;
4837 if (level
== TPACPI_FAN_LAST_LEVEL
)
4838 level
= fan_control_desired_level
;
4840 rc
= fan_set_level(level
);
4842 fan_update_desired_level(level
);
4844 mutex_unlock(&fan_mutex
);
4848 static int fan_set_enable(void)
4853 if (!fan_control_allowed
)
4856 if (mutex_lock_interruptible(&fan_mutex
))
4857 return -ERESTARTSYS
;
4859 switch (fan_control_access_mode
) {
4860 case TPACPI_FAN_WR_ACPI_FANS
:
4861 case TPACPI_FAN_WR_TPEC
:
4862 rc
= fan_get_status(&s
);
4866 /* Don't go out of emergency fan mode */
4869 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
4872 if (!acpi_ec_write(fan_status_offset
, s
))
4875 tp_features
.fan_ctrl_status_undef
= 0;
4880 case TPACPI_FAN_WR_ACPI_SFAN
:
4881 rc
= fan_get_status(&s
);
4887 /* Set fan to at least level 4 */
4890 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
4900 mutex_unlock(&fan_mutex
);
4904 static int fan_set_disable(void)
4908 if (!fan_control_allowed
)
4911 if (mutex_lock_interruptible(&fan_mutex
))
4912 return -ERESTARTSYS
;
4915 switch (fan_control_access_mode
) {
4916 case TPACPI_FAN_WR_ACPI_FANS
:
4917 case TPACPI_FAN_WR_TPEC
:
4918 if (!acpi_ec_write(fan_status_offset
, 0x00))
4921 fan_control_desired_level
= 0;
4922 tp_features
.fan_ctrl_status_undef
= 0;
4926 case TPACPI_FAN_WR_ACPI_SFAN
:
4927 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
4930 fan_control_desired_level
= 0;
4938 mutex_unlock(&fan_mutex
);
4942 static int fan_set_speed(int speed
)
4946 if (!fan_control_allowed
)
4949 if (mutex_lock_interruptible(&fan_mutex
))
4950 return -ERESTARTSYS
;
4953 switch (fan_control_access_mode
) {
4954 case TPACPI_FAN_WR_ACPI_FANS
:
4955 if (speed
>= 0 && speed
<= 65535) {
4956 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
4957 speed
, speed
, speed
))
4967 mutex_unlock(&fan_mutex
);
4971 static void fan_watchdog_reset(void)
4973 static int fan_watchdog_active
;
4975 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
4978 if (fan_watchdog_active
)
4979 cancel_delayed_work(&fan_watchdog_task
);
4981 if (fan_watchdog_maxinterval
> 0 &&
4982 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
4983 fan_watchdog_active
= 1;
4984 if (!schedule_delayed_work(&fan_watchdog_task
,
4985 msecs_to_jiffies(fan_watchdog_maxinterval
4988 "failed to schedule the fan watchdog, "
4989 "watchdog will not trigger\n");
4992 fan_watchdog_active
= 0;
4995 static void fan_watchdog_fire(struct work_struct
*ignored
)
4999 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
5002 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
5003 rc
= fan_set_enable();
5005 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
5006 "will try again later...\n", -rc
);
5007 /* reschedule for later */
5008 fan_watchdog_reset();
5013 * SYSFS fan layout: hwmon compatible (device)
5016 * 0: "disengaged" mode
5018 * 2: native EC "auto" mode (recommended, hardware default)
5020 * pwm*: set speed in manual mode, ignored otherwise.
5021 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5024 * fan*_input: tachometer reading, RPM
5027 * SYSFS fan layout: extensions
5029 * fan_watchdog (driver):
5030 * fan watchdog interval in seconds, 0 disables (default), max 120
5033 /* sysfs fan pwm1_enable ----------------------------------------------- */
5034 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
5035 struct device_attribute
*attr
,
5041 res
= fan_get_status_safe(&status
);
5045 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5046 if (status
!= fan_control_initial_status
) {
5047 tp_features
.fan_ctrl_status_undef
= 0;
5049 /* Return most likely status. In fact, it
5050 * might be the only possible status */
5051 status
= TP_EC_FAN_AUTO
;
5055 if (status
& TP_EC_FAN_FULLSPEED
) {
5057 } else if (status
& TP_EC_FAN_AUTO
) {
5062 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
5065 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
5066 struct device_attribute
*attr
,
5067 const char *buf
, size_t count
)
5072 if (parse_strtoul(buf
, 2, &t
))
5077 level
= TP_EC_FAN_FULLSPEED
;
5080 level
= TPACPI_FAN_LAST_LEVEL
;
5083 level
= TP_EC_FAN_AUTO
;
5086 /* reserved for software-controlled auto mode */
5092 res
= fan_set_level_safe(level
);
5098 fan_watchdog_reset();
5103 static struct device_attribute dev_attr_fan_pwm1_enable
=
5104 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
5105 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
5107 /* sysfs fan pwm1 ------------------------------------------------------ */
5108 static ssize_t
fan_pwm1_show(struct device
*dev
,
5109 struct device_attribute
*attr
,
5115 res
= fan_get_status_safe(&status
);
5119 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5120 if (status
!= fan_control_initial_status
) {
5121 tp_features
.fan_ctrl_status_undef
= 0;
5123 status
= TP_EC_FAN_AUTO
;
5128 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
5129 status
= fan_control_desired_level
;
5134 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
5137 static ssize_t
fan_pwm1_store(struct device
*dev
,
5138 struct device_attribute
*attr
,
5139 const char *buf
, size_t count
)
5143 u8 status
, newlevel
;
5145 if (parse_strtoul(buf
, 255, &s
))
5148 /* scale down from 0-255 to 0-7 */
5149 newlevel
= (s
>> 5) & 0x07;
5151 if (mutex_lock_interruptible(&fan_mutex
))
5152 return -ERESTARTSYS
;
5154 rc
= fan_get_status(&status
);
5155 if (!rc
&& (status
&
5156 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
5157 rc
= fan_set_level(newlevel
);
5161 fan_update_desired_level(newlevel
);
5162 fan_watchdog_reset();
5166 mutex_unlock(&fan_mutex
);
5167 return (rc
)? rc
: count
;
5170 static struct device_attribute dev_attr_fan_pwm1
=
5171 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
5172 fan_pwm1_show
, fan_pwm1_store
);
5174 /* sysfs fan fan1_input ------------------------------------------------ */
5175 static ssize_t
fan_fan1_input_show(struct device
*dev
,
5176 struct device_attribute
*attr
,
5182 res
= fan_get_speed(&speed
);
5186 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
5189 static struct device_attribute dev_attr_fan_fan1_input
=
5190 __ATTR(fan1_input
, S_IRUGO
,
5191 fan_fan1_input_show
, NULL
);
5193 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5194 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
5197 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
5200 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
5201 const char *buf
, size_t count
)
5205 if (parse_strtoul(buf
, 120, &t
))
5208 if (!fan_control_allowed
)
5211 fan_watchdog_maxinterval
= t
;
5212 fan_watchdog_reset();
5217 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
5218 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
5220 /* --------------------------------------------------------------------- */
5221 static struct attribute
*fan_attributes
[] = {
5222 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
5223 &dev_attr_fan_fan1_input
.attr
,
5227 static const struct attribute_group fan_attr_group
= {
5228 .attrs
= fan_attributes
,
5231 static int __init
fan_init(struct ibm_init_struct
*iibm
)
5235 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
5237 mutex_init(&fan_mutex
);
5238 fan_status_access_mode
= TPACPI_FAN_NONE
;
5239 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5240 fan_control_commands
= 0;
5241 fan_watchdog_maxinterval
= 0;
5242 tp_features
.fan_ctrl_status_undef
= 0;
5243 fan_control_desired_level
= 7;
5245 TPACPI_ACPIHANDLE_INIT(fans
);
5246 TPACPI_ACPIHANDLE_INIT(gfan
);
5247 TPACPI_ACPIHANDLE_INIT(sfan
);
5250 /* 570, 600e/x, 770e, 770x */
5251 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
5253 /* all other ThinkPads: note that even old-style
5254 * ThinkPad ECs supports the fan control register */
5255 if (likely(acpi_ec_read(fan_status_offset
,
5256 &fan_control_initial_status
))) {
5257 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
5259 /* In some ThinkPads, neither the EC nor the ACPI
5260 * DSDT initialize the fan status, and it ends up
5261 * being set to 0x07 when it *could* be either
5264 * Enable for TP-1Y (T43), TP-78 (R51e),
5265 * TP-76 (R52), TP-70 (T43, R52), which are known
5267 if (fan_control_initial_status
== 0x07) {
5268 switch (thinkpad_id
.ec_model
) {
5269 case 0x5931: /* TP-1Y */
5270 case 0x3837: /* TP-78 */
5271 case 0x3637: /* TP-76 */
5272 case 0x3037: /* TP-70 */
5273 printk(TPACPI_NOTICE
5274 "fan_init: initial fan status "
5275 "is unknown, assuming it is "
5277 tp_features
.fan_ctrl_status_undef
= 1;
5283 "ThinkPad ACPI EC access misbehaving, "
5284 "fan status and control unavailable\n");
5291 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
5292 fan_control_commands
|=
5293 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
5296 /* gfan without sfan means no fan control */
5297 /* all other models implement TP EC 0x2f control */
5301 fan_control_access_mode
=
5302 TPACPI_FAN_WR_ACPI_FANS
;
5303 fan_control_commands
|=
5304 TPACPI_FAN_CMD_SPEED
|
5305 TPACPI_FAN_CMD_LEVEL
|
5306 TPACPI_FAN_CMD_ENABLE
;
5308 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
5309 fan_control_commands
|=
5310 TPACPI_FAN_CMD_LEVEL
|
5311 TPACPI_FAN_CMD_ENABLE
;
5316 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
5317 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
5318 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
5319 fan_status_access_mode
, fan_control_access_mode
);
5321 /* fan control master switch */
5322 if (!fan_control_allowed
) {
5323 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5324 fan_control_commands
= 0;
5325 dbg_printk(TPACPI_DBG_INIT
,
5326 "fan control features disabled by parameter\n");
5329 /* update fan_control_desired_level */
5330 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
5331 fan_get_status_safe(NULL
);
5333 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
5334 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
5335 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5338 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
5339 &driver_attr_fan_watchdog
);
5347 static void fan_exit(void)
5349 vdbg_printk(TPACPI_DBG_EXIT
,
5350 "cancelling any pending fan watchdog tasks\n");
5352 /* FIXME: can we really do this unconditionally? */
5353 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
5354 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
5355 &driver_attr_fan_watchdog
);
5357 cancel_delayed_work(&fan_watchdog_task
);
5358 flush_scheduled_work();
5361 static int fan_read(char *p
)
5366 unsigned int speed
= 0;
5368 switch (fan_status_access_mode
) {
5369 case TPACPI_FAN_RD_ACPI_GFAN
:
5370 /* 570, 600e/x, 770e, 770x */
5371 rc
= fan_get_status_safe(&status
);
5375 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
5377 (status
!= 0) ? "enabled" : "disabled", status
);
5380 case TPACPI_FAN_RD_TPEC
:
5381 /* all except 570, 600e/x, 770e, 770x */
5382 rc
= fan_get_status_safe(&status
);
5386 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5387 if (status
!= fan_control_initial_status
)
5388 tp_features
.fan_ctrl_status_undef
= 0;
5390 /* Return most likely status. In fact, it
5391 * might be the only possible status */
5392 status
= TP_EC_FAN_AUTO
;
5395 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
5396 (status
!= 0) ? "enabled" : "disabled");
5398 rc
= fan_get_speed(&speed
);
5402 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
5404 if (status
& TP_EC_FAN_FULLSPEED
)
5405 /* Disengaged mode takes precedence */
5406 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
5407 else if (status
& TP_EC_FAN_AUTO
)
5408 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
5410 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
5413 case TPACPI_FAN_NONE
:
5415 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5418 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
5419 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
5421 switch (fan_control_access_mode
) {
5422 case TPACPI_FAN_WR_ACPI_SFAN
:
5423 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
5427 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
5428 "auto, disengaged, full-speed)\n");
5433 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
5434 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
5435 "commands:\twatchdog <timeout> (<timeout> "
5436 "is 0 (off), 1-120 (seconds))\n");
5438 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
5439 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
5440 " (<speed> is 0-65535)\n");
5445 static int fan_write_cmd_level(const char *cmd
, int *rc
)
5449 if (strlencmp(cmd
, "level auto") == 0)
5450 level
= TP_EC_FAN_AUTO
;
5451 else if ((strlencmp(cmd
, "level disengaged") == 0) |
5452 (strlencmp(cmd
, "level full-speed") == 0))
5453 level
= TP_EC_FAN_FULLSPEED
;
5454 else if (sscanf(cmd
, "level %d", &level
) != 1)
5457 *rc
= fan_set_level_safe(level
);
5459 printk(TPACPI_ERR
"level command accepted for unsupported "
5460 "access mode %d", fan_control_access_mode
);
5465 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
5467 if (strlencmp(cmd
, "enable") != 0)
5470 *rc
= fan_set_enable();
5472 printk(TPACPI_ERR
"enable command accepted for unsupported "
5473 "access mode %d", fan_control_access_mode
);
5478 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
5480 if (strlencmp(cmd
, "disable") != 0)
5483 *rc
= fan_set_disable();
5485 printk(TPACPI_ERR
"disable command accepted for unsupported "
5486 "access mode %d", fan_control_access_mode
);
5491 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
5496 * Support speed <low> <medium> <high> ? */
5498 if (sscanf(cmd
, "speed %d", &speed
) != 1)
5501 *rc
= fan_set_speed(speed
);
5503 printk(TPACPI_ERR
"speed command accepted for unsupported "
5504 "access mode %d", fan_control_access_mode
);
5509 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
5513 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
5516 if (interval
< 0 || interval
> 120)
5519 fan_watchdog_maxinterval
= interval
;
5524 static int fan_write(char *buf
)
5529 while (!rc
&& (cmd
= next_cmd(&buf
))) {
5530 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
5531 fan_write_cmd_level(cmd
, &rc
)) &&
5532 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
5533 (fan_write_cmd_enable(cmd
, &rc
) ||
5534 fan_write_cmd_disable(cmd
, &rc
) ||
5535 fan_write_cmd_watchdog(cmd
, &rc
))) &&
5536 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
5537 fan_write_cmd_speed(cmd
, &rc
))
5541 fan_watchdog_reset();
5547 static struct ibm_struct fan_driver_data
= {
5554 /****************************************************************************
5555 ****************************************************************************
5559 ****************************************************************************
5560 ****************************************************************************/
5562 /* sysfs name ---------------------------------------------------------- */
5563 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
5564 struct device_attribute
*attr
,
5567 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
5570 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
5571 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
5573 /* --------------------------------------------------------------------- */
5576 static struct proc_dir_entry
*proc_dir
;
5579 * Module and infrastructure proble, init and exit handling
5582 static int force_load
;
5584 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5585 static const char * __init
str_supported(int is_supported
)
5587 static char text_unsupported
[] __initdata
= "not supported";
5589 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
5591 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5593 static void ibm_exit(struct ibm_struct
*ibm
)
5595 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
5597 list_del_init(&ibm
->all_drivers
);
5599 if (ibm
->flags
.acpi_notify_installed
) {
5600 dbg_printk(TPACPI_DBG_EXIT
,
5601 "%s: acpi_remove_notify_handler\n", ibm
->name
);
5603 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
5605 dispatch_acpi_notify
);
5606 ibm
->flags
.acpi_notify_installed
= 0;
5607 ibm
->flags
.acpi_notify_installed
= 0;
5610 if (ibm
->flags
.proc_created
) {
5611 dbg_printk(TPACPI_DBG_EXIT
,
5612 "%s: remove_proc_entry\n", ibm
->name
);
5613 remove_proc_entry(ibm
->name
, proc_dir
);
5614 ibm
->flags
.proc_created
= 0;
5617 if (ibm
->flags
.acpi_driver_registered
) {
5618 dbg_printk(TPACPI_DBG_EXIT
,
5619 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
5621 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
5622 kfree(ibm
->acpi
->driver
);
5623 ibm
->acpi
->driver
= NULL
;
5624 ibm
->flags
.acpi_driver_registered
= 0;
5627 if (ibm
->flags
.init_called
&& ibm
->exit
) {
5629 ibm
->flags
.init_called
= 0;
5632 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
5635 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
5638 struct ibm_struct
*ibm
= iibm
->data
;
5639 struct proc_dir_entry
*entry
;
5641 BUG_ON(ibm
== NULL
);
5643 INIT_LIST_HEAD(&ibm
->all_drivers
);
5645 if (ibm
->flags
.experimental
&& !experimental
)
5648 dbg_printk(TPACPI_DBG_INIT
,
5649 "probing for %s\n", ibm
->name
);
5652 ret
= iibm
->init(iibm
);
5654 return 0; /* probe failed */
5658 ibm
->flags
.init_called
= 1;
5662 if (ibm
->acpi
->hid
) {
5663 ret
= register_tpacpi_subdriver(ibm
);
5668 if (ibm
->acpi
->notify
) {
5669 ret
= setup_acpi_notify(ibm
);
5670 if (ret
== -ENODEV
) {
5671 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
5681 dbg_printk(TPACPI_DBG_INIT
,
5682 "%s installed\n", ibm
->name
);
5685 entry
= create_proc_entry(ibm
->name
,
5686 S_IFREG
| S_IRUGO
| S_IWUSR
,
5689 printk(TPACPI_ERR
"unable to create proc entry %s\n",
5694 entry
->owner
= THIS_MODULE
;
5696 entry
->read_proc
= &dispatch_procfs_read
;
5698 entry
->write_proc
= &dispatch_procfs_write
;
5699 ibm
->flags
.proc_created
= 1;
5702 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
5707 dbg_printk(TPACPI_DBG_INIT
,
5708 "%s: at error exit path with result %d\n",
5712 return (ret
< 0)? ret
: 0;
5717 static void __init
get_thinkpad_model_data(struct thinkpad_id_data
*tp
)
5719 const struct dmi_device
*dev
= NULL
;
5720 char ec_fw_string
[18];
5725 memset(tp
, 0, sizeof(*tp
));
5727 if (dmi_name_in_vendors("IBM"))
5728 tp
->vendor
= PCI_VENDOR_ID_IBM
;
5729 else if (dmi_name_in_vendors("LENOVO"))
5730 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
5734 tp
->bios_version_str
= kstrdup(dmi_get_system_info(DMI_BIOS_VERSION
),
5736 if (!tp
->bios_version_str
)
5738 tp
->bios_model
= tp
->bios_version_str
[0]
5739 | (tp
->bios_version_str
[1] << 8);
5742 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5743 * X32 or newer, all Z series; Some models must have an
5744 * up-to-date BIOS or they will not be detected.
5746 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5748 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
5749 if (sscanf(dev
->name
,
5750 "IBM ThinkPad Embedded Controller -[%17c",
5751 ec_fw_string
) == 1) {
5752 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
5753 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
5755 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
5756 tp
->ec_model
= ec_fw_string
[0]
5757 | (ec_fw_string
[1] << 8);
5762 tp
->model_str
= kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION
),
5764 if (strnicmp(tp
->model_str
, "ThinkPad", 8) != 0) {
5765 kfree(tp
->model_str
);
5766 tp
->model_str
= NULL
;
5770 static int __init
probe_for_thinkpad(void)
5778 * Non-ancient models have better DMI tagging, but very old models
5781 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
5783 /* ec is required because many other handles are relative to it */
5784 TPACPI_ACPIHANDLE_INIT(ec
);
5788 "Not yet supported ThinkPad detected!\n");
5793 * Risks a regression on very old machines, but reduces potential
5794 * false positives a damn great deal
5797 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
5799 if (!is_thinkpad
&& !force_load
)
5806 /* Module init, exit, parameters */
5808 static struct ibm_init_struct ibms_init
[] __initdata
= {
5810 .init
= thinkpad_acpi_driver_init
,
5811 .data
= &thinkpad_acpi_driver_data
,
5814 .init
= hotkey_init
,
5815 .data
= &hotkey_driver_data
,
5818 .init
= bluetooth_init
,
5819 .data
= &bluetooth_driver_data
,
5823 .data
= &wan_driver_data
,
5825 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5828 .data
= &video_driver_data
,
5833 .data
= &light_driver_data
,
5835 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5838 .data
= &dock_driver_data
[0],
5842 .data
= &dock_driver_data
[1],
5845 #ifdef CONFIG_THINKPAD_ACPI_BAY
5848 .data
= &bay_driver_data
,
5853 .data
= &cmos_driver_data
,
5857 .data
= &led_driver_data
,
5861 .data
= &beep_driver_data
,
5864 .init
= thermal_init
,
5865 .data
= &thermal_driver_data
,
5868 .data
= &ecdump_driver_data
,
5871 .init
= brightness_init
,
5872 .data
= &brightness_driver_data
,
5875 .data
= &volume_driver_data
,
5879 .data
= &fan_driver_data
,
5883 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
5886 struct ibm_struct
*ibm
;
5888 if (!kp
|| !kp
->name
|| !val
)
5891 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
5892 ibm
= ibms_init
[i
].data
;
5893 WARN_ON(ibm
== NULL
);
5895 if (!ibm
|| !ibm
->name
)
5898 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
5899 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
5901 strcpy(ibms_init
[i
].param
, val
);
5902 strcat(ibms_init
[i
].param
, ",");
5910 module_param(experimental
, int, 0);
5911 MODULE_PARM_DESC(experimental
,
5912 "Enables experimental features when non-zero");
5914 module_param_named(debug
, dbg_level
, uint
, 0);
5915 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
5917 module_param(force_load
, bool, 0);
5918 MODULE_PARM_DESC(force_load
,
5919 "Attempts to load the driver even on a "
5920 "mis-identified ThinkPad when true");
5922 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
5923 MODULE_PARM_DESC(fan_control
,
5924 "Enables setting fan parameters features when true");
5926 module_param_named(brightness_mode
, brightness_mode
, int, 0);
5927 MODULE_PARM_DESC(brightness_mode
,
5928 "Selects brightness control strategy: "
5929 "0=auto, 1=EC, 2=CMOS, 3=both");
5931 module_param(brightness_enable
, uint
, 0);
5932 MODULE_PARM_DESC(brightness_enable
,
5933 "Enables backlight control when 1, disables when 0");
5935 module_param(hotkey_report_mode
, uint
, 0);
5936 MODULE_PARM_DESC(hotkey_report_mode
,
5937 "used for backwards compatibility with userspace, "
5938 "see documentation");
5940 #define TPACPI_PARAM(feature) \
5941 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5942 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5943 "at module load, see documentation")
5945 TPACPI_PARAM(hotkey
);
5946 TPACPI_PARAM(bluetooth
);
5947 TPACPI_PARAM(video
);
5948 TPACPI_PARAM(light
);
5949 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5952 #ifdef CONFIG_THINKPAD_ACPI_BAY
5954 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5958 TPACPI_PARAM(ecdump
);
5959 TPACPI_PARAM(brightness
);
5960 TPACPI_PARAM(volume
);
5963 static void thinkpad_acpi_module_exit(void)
5965 struct ibm_struct
*ibm
, *itmp
;
5967 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
5969 list_for_each_entry_safe_reverse(ibm
, itmp
,
5970 &tpacpi_all_drivers
,
5975 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
5977 if (tpacpi_inputdev
) {
5978 if (tp_features
.input_device_registered
)
5979 input_unregister_device(tpacpi_inputdev
);
5981 input_free_device(tpacpi_inputdev
);
5985 hwmon_device_unregister(tpacpi_hwmon
);
5987 if (tp_features
.sensors_pdev_attrs_registered
)
5988 device_remove_file(&tpacpi_sensors_pdev
->dev
,
5989 &dev_attr_thinkpad_acpi_pdev_name
);
5990 if (tpacpi_sensors_pdev
)
5991 platform_device_unregister(tpacpi_sensors_pdev
);
5993 platform_device_unregister(tpacpi_pdev
);
5995 if (tp_features
.sensors_pdrv_attrs_registered
)
5996 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
5997 if (tp_features
.platform_drv_attrs_registered
)
5998 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
6000 if (tp_features
.sensors_pdrv_registered
)
6001 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
6003 if (tp_features
.platform_drv_registered
)
6004 platform_driver_unregister(&tpacpi_pdriver
);
6007 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
6009 kfree(thinkpad_id
.bios_version_str
);
6010 kfree(thinkpad_id
.ec_version_str
);
6011 kfree(thinkpad_id
.model_str
);
6015 static int __init
thinkpad_acpi_module_init(void)
6019 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
6021 /* Parameter checking */
6022 if (hotkey_report_mode
> 2)
6025 /* Driver-level probe */
6027 get_thinkpad_model_data(&thinkpad_id
);
6028 ret
= probe_for_thinkpad();
6030 thinkpad_acpi_module_exit();
6034 /* Driver initialization */
6036 TPACPI_ACPIHANDLE_INIT(ecrd
);
6037 TPACPI_ACPIHANDLE_INIT(ecwr
);
6039 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
6042 "unable to create proc dir " TPACPI_PROC_DIR
);
6043 thinkpad_acpi_module_exit();
6046 proc_dir
->owner
= THIS_MODULE
;
6048 ret
= platform_driver_register(&tpacpi_pdriver
);
6051 "unable to register main platform driver\n");
6052 thinkpad_acpi_module_exit();
6055 tp_features
.platform_drv_registered
= 1;
6057 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
6060 "unable to register hwmon platform driver\n");
6061 thinkpad_acpi_module_exit();
6064 tp_features
.sensors_pdrv_registered
= 1;
6066 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
6068 tp_features
.platform_drv_attrs_registered
= 1;
6069 ret
= tpacpi_create_driver_attributes(
6070 &tpacpi_hwmon_pdriver
.driver
);
6074 "unable to create sysfs driver attributes\n");
6075 thinkpad_acpi_module_exit();
6078 tp_features
.sensors_pdrv_attrs_registered
= 1;
6081 /* Device initialization */
6082 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
6084 if (IS_ERR(tpacpi_pdev
)) {
6085 ret
= PTR_ERR(tpacpi_pdev
);
6087 printk(TPACPI_ERR
"unable to register platform device\n");
6088 thinkpad_acpi_module_exit();
6091 tpacpi_sensors_pdev
= platform_device_register_simple(
6092 TPACPI_HWMON_DRVR_NAME
,
6094 if (IS_ERR(tpacpi_sensors_pdev
)) {
6095 ret
= PTR_ERR(tpacpi_sensors_pdev
);
6096 tpacpi_sensors_pdev
= NULL
;
6098 "unable to register hwmon platform device\n");
6099 thinkpad_acpi_module_exit();
6102 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
6103 &dev_attr_thinkpad_acpi_pdev_name
);
6106 "unable to create sysfs hwmon device attributes\n");
6107 thinkpad_acpi_module_exit();
6110 tp_features
.sensors_pdev_attrs_registered
= 1;
6111 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
6112 if (IS_ERR(tpacpi_hwmon
)) {
6113 ret
= PTR_ERR(tpacpi_hwmon
);
6114 tpacpi_hwmon
= NULL
;
6115 printk(TPACPI_ERR
"unable to register hwmon device\n");
6116 thinkpad_acpi_module_exit();
6119 mutex_init(&tpacpi_inputdev_send_mutex
);
6120 tpacpi_inputdev
= input_allocate_device();
6121 if (!tpacpi_inputdev
) {
6122 printk(TPACPI_ERR
"unable to allocate input device\n");
6123 thinkpad_acpi_module_exit();
6126 /* Prepare input device, but don't register */
6127 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
6128 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
6129 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
6130 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
6131 thinkpad_id
.vendor
:
6133 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
6134 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
6136 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
6137 ret
= ibm_init(&ibms_init
[i
]);
6138 if (ret
>= 0 && *ibms_init
[i
].param
)
6139 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
6141 thinkpad_acpi_module_exit();
6145 ret
= input_register_device(tpacpi_inputdev
);
6147 printk(TPACPI_ERR
"unable to register input device\n");
6148 thinkpad_acpi_module_exit();
6151 tp_features
.input_device_registered
= 1;
6154 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
6158 /* Please remove this in year 2009 */
6159 MODULE_ALIAS("ibm_acpi");
6162 * DMI matching for module autoloading
6164 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6165 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6167 * Only models listed in thinkwiki will be supported, so add yours
6168 * if it is not there yet.
6170 #define IBM_BIOS_MODULE_ALIAS(__type) \
6171 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6173 /* Non-ancient thinkpads */
6174 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6175 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6177 /* Ancient thinkpad BIOSes have to be identified by
6178 * BIOS type or model number, and there are far less
6179 * BIOS types than model numbers... */
6180 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6181 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6182 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6184 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6185 MODULE_DESCRIPTION(TPACPI_DESC
);
6186 MODULE_VERSION(TPACPI_VERSION
);
6187 MODULE_LICENSE("GPL");
6189 module_init(thinkpad_acpi_module_init
);
6190 module_exit(thinkpad_acpi_module_exit
);