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
227 u32 bright_16levels
:1;
228 u32 bright_acpimode
:1;
230 u32 fan_ctrl_status_undef
:1;
231 u32 input_device_registered
:1;
232 u32 platform_drv_registered
:1;
233 u32 platform_drv_attrs_registered
:1;
234 u32 sensors_pdrv_registered
:1;
235 u32 sensors_pdrv_attrs_registered
:1;
236 u32 sensors_pdev_attrs_registered
:1;
237 u32 hotkey_poll_active
:1;
240 struct thinkpad_id_data
{
241 unsigned int vendor
; /* ThinkPad vendor:
242 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
244 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
245 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
247 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
252 static struct thinkpad_id_data thinkpad_id
;
255 TPACPI_LIFE_INIT
= 0,
260 static int experimental
;
261 static u32 dbg_level
;
263 /****************************************************************************
264 ****************************************************************************
266 * ACPI Helpers and device model
268 ****************************************************************************
269 ****************************************************************************/
271 /*************************************************************************
275 static acpi_handle root_handle
;
277 #define TPACPI_HANDLE(object, parent, paths...) \
278 static acpi_handle object##_handle; \
279 static acpi_handle *object##_parent = &parent##_handle; \
280 static char *object##_path; \
281 static char *object##_paths[] = { paths }
283 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
284 "\\_SB.PCI.ISA.EC", /* 570 */
285 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
286 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
287 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
288 "\\_SB.PCI0.ICH3.EC0", /* R31 */
289 "\\_SB.PCI0.LPC.EC", /* all others */
292 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
293 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
295 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
297 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
298 "\\CMS", /* R40, R40e */
301 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
302 "^HKEY", /* R30, R31 */
303 "HKEY", /* all others */
306 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
307 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
308 "\\_SB.PCI0.VID0", /* 770e */
309 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
310 "\\_SB.PCI0.AGP.VID", /* all others */
314 /*************************************************************************
318 static int acpi_evalf(acpi_handle handle
,
319 void *res
, char *method
, char *fmt
, ...)
322 struct acpi_object_list params
;
323 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
324 struct acpi_buffer result
, *resultp
;
325 union acpi_object out_obj
;
333 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
346 params
.pointer
= &in_objs
[0];
353 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
354 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
356 /* add more types as needed */
358 printk(TPACPI_ERR
"acpi_evalf() called "
359 "with invalid format character '%c'\n", c
);
365 if (res_type
!= 'v') {
366 result
.length
= sizeof(out_obj
);
367 result
.pointer
= &out_obj
;
372 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
377 *(int *)res
= out_obj
.integer
.value
;
378 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
381 success
= status
== AE_OK
;
383 /* add more types as needed */
385 printk(TPACPI_ERR
"acpi_evalf() called "
386 "with invalid format character '%c'\n", res_type
);
390 if (!success
&& !quiet
)
391 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
392 method
, fmt0
, status
);
397 static int acpi_ec_read(int i
, u8
*p
)
402 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
406 if (ec_read(i
, p
) < 0)
413 static int acpi_ec_write(int i
, u8 v
)
416 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
419 if (ec_write(i
, v
) < 0)
426 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
427 static int _sta(acpi_handle handle
)
431 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
438 static int issue_thinkpad_cmos_command(int cmos_cmd
)
443 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
449 /*************************************************************************
453 #define TPACPI_ACPIHANDLE_INIT(object) \
454 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
455 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
457 static void drv_acpi_handle_init(char *name
,
458 acpi_handle
*handle
, acpi_handle parent
,
459 char **paths
, int num_paths
, char **path
)
464 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
467 for (i
= 0; i
< num_paths
; i
++) {
468 status
= acpi_get_handle(parent
, paths
[i
], handle
);
469 if (ACPI_SUCCESS(status
)) {
471 dbg_printk(TPACPI_DBG_INIT
,
472 "Found ACPI handle %s for %s\n",
478 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
483 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
485 struct ibm_struct
*ibm
= data
;
487 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
490 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
493 ibm
->acpi
->notify(ibm
, event
);
496 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
503 if (!*ibm
->acpi
->handle
)
506 vdbg_printk(TPACPI_DBG_INIT
,
507 "setting up ACPI notify for %s\n", ibm
->name
);
509 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
511 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
516 acpi_driver_data(ibm
->acpi
->device
) = ibm
;
517 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
518 TPACPI_ACPI_EVENT_PREFIX
,
521 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
522 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
523 if (ACPI_FAILURE(status
)) {
524 if (status
== AE_ALREADY_EXISTS
) {
526 "another device driver is already "
527 "handling %s events\n", ibm
->name
);
530 "acpi_install_notify_handler(%s) failed: %d\n",
535 ibm
->flags
.acpi_notify_installed
= 1;
539 static int __init
tpacpi_device_add(struct acpi_device
*device
)
544 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
548 dbg_printk(TPACPI_DBG_INIT
,
549 "registering %s as an ACPI driver\n", ibm
->name
);
553 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
554 if (!ibm
->acpi
->driver
) {
555 printk(TPACPI_ERR
"kzalloc(ibm->driver) failed\n");
559 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
560 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
562 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
564 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
566 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
568 kfree(ibm
->acpi
->driver
);
569 ibm
->acpi
->driver
= NULL
;
571 ibm
->flags
.acpi_driver_registered
= 1;
577 /****************************************************************************
578 ****************************************************************************
582 ****************************************************************************
583 ****************************************************************************/
585 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
586 int count
, int *eof
, void *data
)
588 struct ibm_struct
*ibm
= data
;
591 if (!ibm
|| !ibm
->read
)
594 len
= ibm
->read(page
);
598 if (len
<= off
+ count
)
610 static int dispatch_procfs_write(struct file
*file
,
611 const char __user
*userbuf
,
612 unsigned long count
, void *data
)
614 struct ibm_struct
*ibm
= data
;
618 if (!ibm
|| !ibm
->write
)
621 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
625 if (copy_from_user(kernbuf
, userbuf
, count
)) {
631 strcat(kernbuf
, ",");
632 ret
= ibm
->write(kernbuf
);
641 static char *next_cmd(char **cmds
)
646 while ((end
= strchr(start
, ',')) && end
== start
)
658 /****************************************************************************
659 ****************************************************************************
661 * Device model: input, hwmon and platform
663 ****************************************************************************
664 ****************************************************************************/
666 static struct platform_device
*tpacpi_pdev
;
667 static struct platform_device
*tpacpi_sensors_pdev
;
668 static struct device
*tpacpi_hwmon
;
669 static struct input_dev
*tpacpi_inputdev
;
670 static struct mutex tpacpi_inputdev_send_mutex
;
671 static LIST_HEAD(tpacpi_all_drivers
);
673 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
676 struct ibm_struct
*ibm
, *itmp
;
678 list_for_each_entry_safe(ibm
, itmp
,
682 (ibm
->suspend
)(state
);
688 static int tpacpi_resume_handler(struct platform_device
*pdev
)
690 struct ibm_struct
*ibm
, *itmp
;
692 list_for_each_entry_safe(ibm
, itmp
,
702 static struct platform_driver tpacpi_pdriver
= {
704 .name
= TPACPI_DRVR_NAME
,
705 .owner
= THIS_MODULE
,
707 .suspend
= tpacpi_suspend_handler
,
708 .resume
= tpacpi_resume_handler
,
711 static struct platform_driver tpacpi_hwmon_pdriver
= {
713 .name
= TPACPI_HWMON_DRVR_NAME
,
714 .owner
= THIS_MODULE
,
718 /*************************************************************************
719 * sysfs support helpers
722 struct attribute_set
{
723 unsigned int members
, max_members
;
724 struct attribute_group group
;
727 struct attribute_set_obj
{
728 struct attribute_set s
;
730 } __attribute__((packed
));
732 static struct attribute_set
*create_attr_set(unsigned int max_members
,
735 struct attribute_set_obj
*sobj
;
737 if (max_members
== 0)
740 /* Allocates space for implicit NULL at the end too */
741 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
742 max_members
* sizeof(struct attribute
*),
746 sobj
->s
.max_members
= max_members
;
747 sobj
->s
.group
.attrs
= &sobj
->a
;
748 sobj
->s
.group
.name
= name
;
753 #define destroy_attr_set(_set) \
756 /* not multi-threaded safe, use it in a single thread per set */
757 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
762 if (s
->members
>= s
->max_members
)
765 s
->group
.attrs
[s
->members
] = attr
;
771 static int add_many_to_attr_set(struct attribute_set
*s
,
772 struct attribute
**attr
,
777 for (i
= 0; i
< count
; i
++) {
778 res
= add_to_attr_set(s
, attr
[i
]);
786 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
788 sysfs_remove_group(kobj
, &s
->group
);
792 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
793 sysfs_create_group(_kobj, &_attr_set->group)
795 static int parse_strtoul(const char *buf
,
796 unsigned long max
, unsigned long *value
)
800 while (*buf
&& isspace(*buf
))
802 *value
= simple_strtoul(buf
, &endp
, 0);
803 while (*endp
&& isspace(*endp
))
805 if (*endp
|| *value
> max
)
811 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
813 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
814 union acpi_object
*obj
;
817 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
818 obj
= (union acpi_object
*)buffer
.pointer
;
819 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
820 printk(TPACPI_ERR
"Unknown _BCL data, "
821 "please report this to %s\n", TPACPI_MAIL
);
824 rc
= obj
->package
.count
;
830 kfree(buffer
.pointer
);
834 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
835 u32 lvl
, void *context
, void **rv
)
837 char name
[ACPI_PATH_SEGMENT_LENGTH
];
838 struct acpi_buffer buffer
= { sizeof(name
), &name
};
840 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
841 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
843 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
844 return AE_CTRL_TERMINATE
;
851 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
853 static int __init
tpacpi_check_std_acpi_brightness_support(void)
857 void *bcl_ptr
= &bcl_levels
;
860 TPACPI_ACPIHANDLE_INIT(vid
);
866 * Search for a _BCL method, and execute it. This is safe on all
867 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
868 * BIOS in ACPI backlight control mode. We do NOT have to care
869 * about calling the _BCL method in an enabled video device, any
870 * will do for our purposes.
873 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
874 tpacpi_acpi_walk_find_bcl
, NULL
,
877 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
878 tp_features
.bright_acpimode
= 1;
879 return (bcl_levels
- 2);
885 /*************************************************************************
886 * thinkpad-acpi driver attributes
889 /* interface_version --------------------------------------------------- */
890 static ssize_t
tpacpi_driver_interface_version_show(
891 struct device_driver
*drv
,
894 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
897 static DRIVER_ATTR(interface_version
, S_IRUGO
,
898 tpacpi_driver_interface_version_show
, NULL
);
900 /* debug_level --------------------------------------------------------- */
901 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
904 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
907 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
908 const char *buf
, size_t count
)
912 if (parse_strtoul(buf
, 0xffff, &t
))
920 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
921 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
923 /* version ------------------------------------------------------------- */
924 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
927 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
928 TPACPI_DESC
, TPACPI_VERSION
);
931 static DRIVER_ATTR(version
, S_IRUGO
,
932 tpacpi_driver_version_show
, NULL
);
934 /* --------------------------------------------------------------------- */
936 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
937 &driver_attr_debug_level
, &driver_attr_version
,
938 &driver_attr_interface_version
,
941 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
947 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
948 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
955 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
959 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
960 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
963 /****************************************************************************
964 ****************************************************************************
968 ****************************************************************************
969 ****************************************************************************/
971 /*************************************************************************
972 * thinkpad-acpi init subdriver
975 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
977 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
978 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
980 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
981 (thinkpad_id
.bios_version_str
) ?
982 thinkpad_id
.bios_version_str
: "unknown",
983 (thinkpad_id
.ec_version_str
) ?
984 thinkpad_id
.ec_version_str
: "unknown");
986 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
987 printk(TPACPI_INFO
"%s %s\n",
988 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
989 "IBM" : ((thinkpad_id
.vendor
==
990 PCI_VENDOR_ID_LENOVO
) ?
991 "Lenovo" : "Unknown vendor"),
992 thinkpad_id
.model_str
);
997 static int thinkpad_acpi_driver_read(char *p
)
1001 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1002 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1007 static struct ibm_struct thinkpad_acpi_driver_data
= {
1009 .read
= thinkpad_acpi_driver_read
,
1012 /*************************************************************************
1016 enum { /* hot key scan codes (derived from ACPI DSDT) */
1017 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1018 TP_ACPI_HOTKEYSCAN_FNF2
,
1019 TP_ACPI_HOTKEYSCAN_FNF3
,
1020 TP_ACPI_HOTKEYSCAN_FNF4
,
1021 TP_ACPI_HOTKEYSCAN_FNF5
,
1022 TP_ACPI_HOTKEYSCAN_FNF6
,
1023 TP_ACPI_HOTKEYSCAN_FNF7
,
1024 TP_ACPI_HOTKEYSCAN_FNF8
,
1025 TP_ACPI_HOTKEYSCAN_FNF9
,
1026 TP_ACPI_HOTKEYSCAN_FNF10
,
1027 TP_ACPI_HOTKEYSCAN_FNF11
,
1028 TP_ACPI_HOTKEYSCAN_FNF12
,
1029 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1030 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1031 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1032 TP_ACPI_HOTKEYSCAN_FNHOME
,
1033 TP_ACPI_HOTKEYSCAN_FNEND
,
1034 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1035 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1036 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1037 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1038 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1039 TP_ACPI_HOTKEYSCAN_MUTE
,
1040 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1043 enum { /* Keys available through NVRAM polling */
1044 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1045 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1048 enum { /* Positions of some of the keys in hotkey masks */
1049 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1050 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1051 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1052 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1053 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1054 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1055 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1056 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1057 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1058 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1059 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1062 enum { /* NVRAM to ACPI HKEY group map */
1063 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1064 TP_ACPI_HKEY_ZOOM_MASK
|
1065 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1066 TP_ACPI_HKEY_HIBERNATE_MASK
,
1067 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1068 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1069 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1070 TP_ACPI_HKEY_VOLDWN_MASK
|
1071 TP_ACPI_HKEY_MUTE_MASK
,
1074 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1075 struct tp_nvram_state
{
1076 u16 thinkpad_toggle
:1;
1078 u16 display_toggle
:1;
1079 u16 thinklight_toggle
:1;
1080 u16 hibernate_toggle
:1;
1081 u16 displayexp_toggle
:1;
1082 u16 display_state
:1;
1083 u16 brightness_toggle
:1;
1084 u16 volume_toggle
:1;
1087 u8 brightness_level
;
1091 static struct task_struct
*tpacpi_hotkey_task
;
1092 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1093 static int hotkey_poll_freq
= 10; /* Hz */
1094 static struct mutex hotkey_thread_mutex
;
1095 static struct mutex hotkey_thread_data_mutex
;
1096 static unsigned int hotkey_config_change
;
1098 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1100 #define hotkey_source_mask 0U
1102 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1104 static struct mutex hotkey_mutex
;
1106 static enum { /* Reasons for waking up */
1107 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1108 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1109 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1110 } hotkey_wakeup_reason
;
1112 static int hotkey_autosleep_ack
;
1114 static int hotkey_orig_status
;
1115 static u32 hotkey_orig_mask
;
1116 static u32 hotkey_all_mask
;
1117 static u32 hotkey_reserved_mask
;
1118 static u32 hotkey_mask
;
1120 static unsigned int hotkey_report_mode
;
1122 static u16
*hotkey_keycode_map
;
1124 static struct attribute_set
*hotkey_dev_attributes
;
1126 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1127 #define HOTKEY_CONFIG_CRITICAL_START \
1129 mutex_lock(&hotkey_thread_data_mutex); \
1130 hotkey_config_change++; \
1132 #define HOTKEY_CONFIG_CRITICAL_END \
1133 mutex_unlock(&hotkey_thread_data_mutex);
1135 #define HOTKEY_CONFIG_CRITICAL_START
1136 #define HOTKEY_CONFIG_CRITICAL_END
1137 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1139 /* HKEY.MHKG() return bits */
1140 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1142 static int hotkey_get_wlsw(int *status
)
1144 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1149 static int hotkey_get_tablet_mode(int *status
)
1153 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1156 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1161 * Call with hotkey_mutex held
1163 static int hotkey_mask_get(void)
1167 if (tp_features
.hotkey_mask
) {
1168 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1171 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1177 * Call with hotkey_mutex held
1179 static int hotkey_mask_set(u32 mask
)
1184 if (tp_features
.hotkey_mask
) {
1185 HOTKEY_CONFIG_CRITICAL_START
1186 for (i
= 0; i
< 32; i
++) {
1188 /* enable in firmware mask only keys not in NVRAM
1189 * mode, but enable the key in the cached hotkey_mask
1190 * regardless of mode, or the key will end up
1191 * disabled by hotkey_mask_get() */
1192 if (!acpi_evalf(hkey_handle
,
1193 NULL
, "MHKM", "vdd", i
+ 1,
1194 !!((mask
& ~hotkey_source_mask
) & m
))) {
1198 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1201 HOTKEY_CONFIG_CRITICAL_END
1203 /* hotkey_mask_get must be called unconditionally below */
1204 if (!hotkey_mask_get() && !rc
&&
1205 (hotkey_mask
& ~hotkey_source_mask
) !=
1206 (mask
& ~hotkey_source_mask
)) {
1207 printk(TPACPI_NOTICE
1208 "requested hot key mask 0x%08x, but "
1209 "firmware forced it to 0x%08x\n",
1213 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1214 HOTKEY_CONFIG_CRITICAL_START
1215 hotkey_mask
= mask
& hotkey_source_mask
;
1216 HOTKEY_CONFIG_CRITICAL_END
1218 if (hotkey_mask
!= mask
) {
1219 printk(TPACPI_NOTICE
1220 "requested hot key mask 0x%08x, "
1221 "forced to 0x%08x (NVRAM poll mask is "
1222 "0x%08x): no firmware mask support\n",
1223 mask
, hotkey_mask
, hotkey_source_mask
);
1228 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1234 static int hotkey_status_get(int *status
)
1236 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1242 static int hotkey_status_set(int status
)
1244 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", status
))
1250 static void tpacpi_input_send_radiosw(void)
1254 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
1255 mutex_lock(&tpacpi_inputdev_send_mutex
);
1257 input_report_switch(tpacpi_inputdev
,
1259 input_sync(tpacpi_inputdev
);
1261 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1265 static void tpacpi_input_send_tabletsw(void)
1269 if (tp_features
.hotkey_tablet
&&
1270 !hotkey_get_tablet_mode(&state
)) {
1271 mutex_lock(&tpacpi_inputdev_send_mutex
);
1273 input_report_switch(tpacpi_inputdev
,
1274 SW_TABLET_MODE
, !!state
);
1275 input_sync(tpacpi_inputdev
);
1277 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1281 static void tpacpi_input_send_key(unsigned int scancode
)
1283 unsigned int keycode
;
1285 keycode
= hotkey_keycode_map
[scancode
];
1287 if (keycode
!= KEY_RESERVED
) {
1288 mutex_lock(&tpacpi_inputdev_send_mutex
);
1290 input_report_key(tpacpi_inputdev
, keycode
, 1);
1291 if (keycode
== KEY_UNKNOWN
)
1292 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1294 input_sync(tpacpi_inputdev
);
1296 input_report_key(tpacpi_inputdev
, keycode
, 0);
1297 if (keycode
== KEY_UNKNOWN
)
1298 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1300 input_sync(tpacpi_inputdev
);
1302 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1306 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1307 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1309 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1311 tpacpi_input_send_key(scancode
);
1312 if (hotkey_report_mode
< 2) {
1313 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1314 0x80, 0x1001 + scancode
);
1318 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1322 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1323 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1324 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1325 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1326 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1327 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1329 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1330 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1331 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1333 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1334 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1335 n
->displayexp_toggle
=
1336 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1338 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1339 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1340 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1341 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1342 n
->brightness_toggle
=
1343 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1345 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1346 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1347 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1348 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1349 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1350 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1354 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1356 if ((mask & (1 << __scancode)) && \
1357 oldn->__member != newn->__member) \
1358 tpacpi_hotkey_send_key(__scancode); \
1361 #define TPACPI_MAY_SEND_KEY(__scancode) \
1362 do { if (mask & (1 << __scancode)) \
1363 tpacpi_hotkey_send_key(__scancode); } while (0)
1365 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1366 struct tp_nvram_state
*newn
,
1369 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1370 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1371 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1376 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1379 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1380 if (oldn
->mute
!= newn
->mute
) {
1381 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1383 if (oldn
->volume_level
> newn
->volume_level
) {
1384 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1385 } else if (oldn
->volume_level
< newn
->volume_level
) {
1386 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1387 } else if (oldn
->mute
== newn
->mute
) {
1388 /* repeated key presses that didn't change state */
1390 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1391 } else if (newn
->volume_level
!= 0) {
1392 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1394 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1399 /* handle brightness */
1400 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1401 if (oldn
->brightness_level
< newn
->brightness_level
) {
1402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1403 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1406 /* repeated key presses that didn't change state */
1407 if (newn
->brightness_level
!= 0) {
1408 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1410 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1416 #undef TPACPI_COMPARE_KEY
1417 #undef TPACPI_MAY_SEND_KEY
1419 static int hotkey_kthread(void *data
)
1421 struct tp_nvram_state s
[2];
1423 unsigned int si
, so
;
1425 unsigned int change_detector
, must_reset
;
1427 mutex_lock(&hotkey_thread_mutex
);
1429 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1438 /* Initial state for compares */
1439 mutex_lock(&hotkey_thread_data_mutex
);
1440 change_detector
= hotkey_config_change
;
1441 mask
= hotkey_source_mask
& hotkey_mask
;
1442 mutex_unlock(&hotkey_thread_data_mutex
);
1443 hotkey_read_nvram(&s
[so
], mask
);
1445 while (!kthread_should_stop() && hotkey_poll_freq
) {
1447 t
= 1000/hotkey_poll_freq
;
1448 t
= msleep_interruptible(t
);
1449 if (unlikely(kthread_should_stop()))
1451 must_reset
= try_to_freeze();
1452 if (t
> 0 && !must_reset
)
1455 mutex_lock(&hotkey_thread_data_mutex
);
1456 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1457 /* forget old state on thaw or config change */
1460 change_detector
= hotkey_config_change
;
1462 mask
= hotkey_source_mask
& hotkey_mask
;
1463 mutex_unlock(&hotkey_thread_data_mutex
);
1466 hotkey_read_nvram(&s
[si
], mask
);
1467 if (likely(si
!= so
)) {
1468 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1478 mutex_unlock(&hotkey_thread_mutex
);
1482 static void hotkey_poll_stop_sync(void)
1484 if (tpacpi_hotkey_task
) {
1485 if (frozen(tpacpi_hotkey_task
) ||
1486 freezing(tpacpi_hotkey_task
))
1487 thaw_process(tpacpi_hotkey_task
);
1489 kthread_stop(tpacpi_hotkey_task
);
1490 tpacpi_hotkey_task
= NULL
;
1491 mutex_lock(&hotkey_thread_mutex
);
1492 /* at this point, the thread did exit */
1493 mutex_unlock(&hotkey_thread_mutex
);
1497 /* call with hotkey_mutex held */
1498 static void hotkey_poll_setup(int may_warn
)
1500 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1501 hotkey_poll_freq
> 0 &&
1502 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1503 if (!tpacpi_hotkey_task
) {
1504 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1507 if (IS_ERR(tpacpi_hotkey_task
)) {
1508 tpacpi_hotkey_task
= NULL
;
1510 "could not create kernel thread "
1511 "for hotkey polling\n");
1515 hotkey_poll_stop_sync();
1517 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1518 printk(TPACPI_NOTICE
1519 "hot keys 0x%08x require polling, "
1520 "which is currently disabled\n",
1521 hotkey_source_mask
);
1526 static void hotkey_poll_setup_safe(int may_warn
)
1528 mutex_lock(&hotkey_mutex
);
1529 hotkey_poll_setup(may_warn
);
1530 mutex_unlock(&hotkey_mutex
);
1533 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1535 static void hotkey_poll_setup_safe(int __unused
)
1539 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1541 static int hotkey_inputdev_open(struct input_dev
*dev
)
1543 switch (tpacpi_lifecycle
) {
1544 case TPACPI_LIFE_INIT
:
1546 * hotkey_init will call hotkey_poll_setup_safe
1547 * at the appropriate moment
1550 case TPACPI_LIFE_EXITING
:
1552 case TPACPI_LIFE_RUNNING
:
1553 hotkey_poll_setup_safe(0);
1557 /* Should only happen if tpacpi_lifecycle is corrupt */
1562 static void hotkey_inputdev_close(struct input_dev
*dev
)
1564 /* disable hotkey polling when possible */
1565 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1566 hotkey_poll_setup_safe(0);
1569 /* sysfs hotkey enable ------------------------------------------------- */
1570 static ssize_t
hotkey_enable_show(struct device
*dev
,
1571 struct device_attribute
*attr
,
1576 res
= hotkey_status_get(&status
);
1580 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1583 static ssize_t
hotkey_enable_store(struct device
*dev
,
1584 struct device_attribute
*attr
,
1585 const char *buf
, size_t count
)
1590 if (parse_strtoul(buf
, 1, &t
))
1593 res
= hotkey_status_set(t
);
1595 return (res
) ? res
: count
;
1598 static struct device_attribute dev_attr_hotkey_enable
=
1599 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1600 hotkey_enable_show
, hotkey_enable_store
);
1602 /* sysfs hotkey mask --------------------------------------------------- */
1603 static ssize_t
hotkey_mask_show(struct device
*dev
,
1604 struct device_attribute
*attr
,
1609 if (mutex_lock_interruptible(&hotkey_mutex
))
1610 return -ERESTARTSYS
;
1611 res
= hotkey_mask_get();
1612 mutex_unlock(&hotkey_mutex
);
1615 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1618 static ssize_t
hotkey_mask_store(struct device
*dev
,
1619 struct device_attribute
*attr
,
1620 const char *buf
, size_t count
)
1625 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1628 if (mutex_lock_interruptible(&hotkey_mutex
))
1629 return -ERESTARTSYS
;
1631 res
= hotkey_mask_set(t
);
1633 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1634 hotkey_poll_setup(1);
1637 mutex_unlock(&hotkey_mutex
);
1639 return (res
) ? res
: count
;
1642 static struct device_attribute dev_attr_hotkey_mask
=
1643 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1644 hotkey_mask_show
, hotkey_mask_store
);
1646 /* sysfs hotkey bios_enabled ------------------------------------------- */
1647 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1648 struct device_attribute
*attr
,
1651 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_orig_status
);
1654 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1655 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1657 /* sysfs hotkey bios_mask ---------------------------------------------- */
1658 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1659 struct device_attribute
*attr
,
1662 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1665 static struct device_attribute dev_attr_hotkey_bios_mask
=
1666 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1668 /* sysfs hotkey all_mask ----------------------------------------------- */
1669 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
1670 struct device_attribute
*attr
,
1673 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1674 hotkey_all_mask
| hotkey_source_mask
);
1677 static struct device_attribute dev_attr_hotkey_all_mask
=
1678 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
1680 /* sysfs hotkey recommended_mask --------------------------------------- */
1681 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
1682 struct device_attribute
*attr
,
1685 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1686 (hotkey_all_mask
| hotkey_source_mask
)
1687 & ~hotkey_reserved_mask
);
1690 static struct device_attribute dev_attr_hotkey_recommended_mask
=
1691 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
1692 hotkey_recommended_mask_show
, NULL
);
1694 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1696 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1697 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
1698 struct device_attribute
*attr
,
1701 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
1704 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
1705 struct device_attribute
*attr
,
1706 const char *buf
, size_t count
)
1710 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
1711 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
1714 if (mutex_lock_interruptible(&hotkey_mutex
))
1715 return -ERESTARTSYS
;
1717 HOTKEY_CONFIG_CRITICAL_START
1718 hotkey_source_mask
= t
;
1719 HOTKEY_CONFIG_CRITICAL_END
1721 hotkey_poll_setup(1);
1723 mutex_unlock(&hotkey_mutex
);
1728 static struct device_attribute dev_attr_hotkey_source_mask
=
1729 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
1730 hotkey_source_mask_show
, hotkey_source_mask_store
);
1732 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1733 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
1734 struct device_attribute
*attr
,
1737 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
1740 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
1741 struct device_attribute
*attr
,
1742 const char *buf
, size_t count
)
1746 if (parse_strtoul(buf
, 25, &t
))
1749 if (mutex_lock_interruptible(&hotkey_mutex
))
1750 return -ERESTARTSYS
;
1752 hotkey_poll_freq
= t
;
1754 hotkey_poll_setup(1);
1755 mutex_unlock(&hotkey_mutex
);
1760 static struct device_attribute dev_attr_hotkey_poll_freq
=
1761 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
1762 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
1764 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1766 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1767 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
1768 struct device_attribute
*attr
,
1772 res
= hotkey_get_wlsw(&s
);
1776 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1779 static struct device_attribute dev_attr_hotkey_radio_sw
=
1780 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
1782 static void hotkey_radio_sw_notify_change(void)
1784 if (tp_features
.hotkey_wlsw
)
1785 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1789 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1790 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
1791 struct device_attribute
*attr
,
1795 res
= hotkey_get_tablet_mode(&s
);
1799 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1802 static struct device_attribute dev_attr_hotkey_tablet_mode
=
1803 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
1805 static void hotkey_tablet_mode_notify_change(void)
1807 if (tp_features
.hotkey_tablet
)
1808 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1809 "hotkey_tablet_mode");
1812 /* sysfs hotkey report_mode -------------------------------------------- */
1813 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
1814 struct device_attribute
*attr
,
1817 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1818 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
1821 static struct device_attribute dev_attr_hotkey_report_mode
=
1822 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
1824 /* sysfs wakeup reason (pollable) -------------------------------------- */
1825 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
1826 struct device_attribute
*attr
,
1829 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
1832 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
1833 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
1835 static void hotkey_wakeup_reason_notify_change(void)
1837 if (tp_features
.hotkey_mask
)
1838 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1842 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1843 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
1844 struct device_attribute
*attr
,
1847 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
1850 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
1851 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
1852 hotkey_wakeup_hotunplug_complete_show
, NULL
);
1854 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1856 if (tp_features
.hotkey_mask
)
1857 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1858 "wakeup_hotunplug_complete");
1861 /* --------------------------------------------------------------------- */
1863 static struct attribute
*hotkey_attributes
[] __initdata
= {
1864 &dev_attr_hotkey_enable
.attr
,
1865 &dev_attr_hotkey_bios_enabled
.attr
,
1866 &dev_attr_hotkey_report_mode
.attr
,
1867 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1868 &dev_attr_hotkey_mask
.attr
,
1869 &dev_attr_hotkey_all_mask
.attr
,
1870 &dev_attr_hotkey_recommended_mask
.attr
,
1871 &dev_attr_hotkey_source_mask
.attr
,
1872 &dev_attr_hotkey_poll_freq
.attr
,
1876 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
1877 &dev_attr_hotkey_bios_mask
.attr
,
1878 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1879 &dev_attr_hotkey_mask
.attr
,
1880 &dev_attr_hotkey_all_mask
.attr
,
1881 &dev_attr_hotkey_recommended_mask
.attr
,
1883 &dev_attr_hotkey_wakeup_reason
.attr
,
1884 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
1887 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
1889 /* Requirements for changing the default keymaps:
1891 * 1. Many of the keys are mapped to KEY_RESERVED for very
1892 * good reasons. Do not change them unless you have deep
1893 * knowledge on the IBM and Lenovo ThinkPad firmware for
1894 * the various ThinkPad models. The driver behaves
1895 * differently for KEY_RESERVED: such keys have their
1896 * hot key mask *unset* in mask_recommended, and also
1897 * in the initial hot key mask programmed into the
1898 * firmware at driver load time, which means the firm-
1899 * ware may react very differently if you change them to
1902 * 2. You must be subscribed to the linux-thinkpad and
1903 * ibm-acpi-devel mailing lists, and you should read the
1904 * list archives since 2007 if you want to change the
1905 * keymaps. This requirement exists so that you will
1906 * know the past history of problems with the thinkpad-
1907 * acpi driver keymaps, and also that you will be
1908 * listening to any bug reports;
1910 * 3. Do not send thinkpad-acpi specific patches directly to
1911 * for merging, *ever*. Send them to the linux-acpi
1912 * mailinglist for comments. Merging is to be done only
1913 * through acpi-test and the ACPI maintainer.
1915 * If the above is too much to ask, don't change the keymap.
1916 * Ask the thinkpad-acpi maintainer to do it, instead.
1918 static u16 ibm_keycode_map
[] __initdata
= {
1919 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1920 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
1921 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
1922 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
1924 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1925 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
1926 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
1927 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
1929 /* brightness: firmware always reacts to them, unless
1930 * X.org did some tricks in the radeon BIOS scratch
1931 * registers of *some* models */
1932 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
1933 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
1935 /* Thinklight: firmware always react to it */
1936 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
1938 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
1939 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
1941 /* Volume: firmware always react to it and reprograms
1942 * the built-in *extra* mixer. Never map it to control
1943 * another mixer by default. */
1944 KEY_RESERVED
, /* 0x14: VOLUME UP */
1945 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
1946 KEY_RESERVED
, /* 0x16: MUTE */
1948 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1950 /* (assignments unknown, please report if found) */
1951 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1952 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1954 static u16 lenovo_keycode_map
[] __initdata
= {
1955 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1956 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
1957 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
1958 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
1960 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1961 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
1962 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
1963 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
1965 /* These either have to go through ACPI video, or
1966 * act like in the IBM ThinkPads, so don't ever
1967 * enable them by default */
1968 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
1969 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
1971 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
1973 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
1974 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
1976 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1977 * react to it and reprograms the built-in *extra* mixer.
1978 * Never map it to control another mixer by default.
1980 * T60?, T61, R60?, R61: firmware and EC tries to send
1981 * these over the regular keyboard, so these are no-ops,
1982 * but there are still weird bugs re. MUTE, so do not
1983 * change unless you get test reports from all Lenovo
1984 * models. May cause the BIOS to interfere with the
1987 KEY_RESERVED
, /* 0x14: VOLUME UP */
1988 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
1989 KEY_RESERVED
, /* 0x16: MUTE */
1991 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1993 /* (assignments unknown, please report if found) */
1994 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1995 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
1998 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1999 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2000 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2006 vdbg_printk(TPACPI_DBG_INIT
, "initializing hotkey subdriver\n");
2008 BUG_ON(!tpacpi_inputdev
);
2009 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2010 tpacpi_inputdev
->close
!= NULL
);
2012 TPACPI_ACPIHANDLE_INIT(hkey
);
2013 mutex_init(&hotkey_mutex
);
2015 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2016 mutex_init(&hotkey_thread_mutex
);
2017 mutex_init(&hotkey_thread_data_mutex
);
2020 /* hotkey not supported on 570 */
2021 tp_features
.hotkey
= hkey_handle
!= NULL
;
2023 vdbg_printk(TPACPI_DBG_INIT
, "hotkeys are %s\n",
2024 str_supported(tp_features
.hotkey
));
2026 if (tp_features
.hotkey
) {
2027 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2028 if (!hotkey_dev_attributes
)
2030 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2032 ARRAY_SIZE(hotkey_attributes
));
2036 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2037 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2038 for HKEY interface version 0x100 */
2039 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2040 if ((hkeyv
>> 8) != 1) {
2041 printk(TPACPI_ERR
"unknown version of the "
2042 "HKEY interface: 0x%x\n", hkeyv
);
2043 printk(TPACPI_ERR
"please report this to %s\n",
2047 * MHKV 0x100 in A31, R40, R40e,
2048 * T4x, X31, and later
2050 tp_features
.hotkey_mask
= 1;
2054 vdbg_printk(TPACPI_DBG_INIT
, "hotkey masks are %s\n",
2055 str_supported(tp_features
.hotkey_mask
));
2057 if (tp_features
.hotkey_mask
) {
2058 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2061 "missing MHKA handler, "
2062 "please report this to %s\n",
2064 /* FN+F12, FN+F4, FN+F3 */
2065 hotkey_all_mask
= 0x080cU
;
2069 /* hotkey_source_mask *must* be zero for
2070 * the first hotkey_mask_get */
2071 res
= hotkey_status_get(&hotkey_orig_status
);
2072 if (!res
&& tp_features
.hotkey_mask
) {
2073 res
= hotkey_mask_get();
2074 hotkey_orig_mask
= hotkey_mask
;
2076 res
= add_many_to_attr_set(
2077 hotkey_dev_attributes
,
2078 hotkey_mask_attributes
,
2079 ARRAY_SIZE(hotkey_mask_attributes
));
2083 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2084 if (tp_features
.hotkey_mask
) {
2085 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2088 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2091 vdbg_printk(TPACPI_DBG_INIT
,
2092 "hotkey source mask 0x%08x, polling freq %d\n",
2093 hotkey_source_mask
, hotkey_poll_freq
);
2096 /* Not all thinkpads have a hardware radio switch */
2097 if (!res
&& acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2098 tp_features
.hotkey_wlsw
= 1;
2100 "radio switch found; radios are %s\n",
2101 enabled(status
, 0));
2102 res
= add_to_attr_set(hotkey_dev_attributes
,
2103 &dev_attr_hotkey_radio_sw
.attr
);
2106 /* For X41t, X60t, X61t Tablets... */
2107 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2108 tp_features
.hotkey_tablet
= 1;
2110 "possible tablet mode switch found; "
2111 "ThinkPad in %s mode\n",
2112 (status
& TP_HOTKEY_TABLET_MASK
)?
2113 "tablet" : "laptop");
2114 res
= add_to_attr_set(hotkey_dev_attributes
,
2115 &dev_attr_hotkey_tablet_mode
.attr
);
2119 res
= register_attr_set_with_sysfs(
2120 hotkey_dev_attributes
,
2121 &tpacpi_pdev
->dev
.kobj
);
2125 /* Set up key map */
2127 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2129 if (!hotkey_keycode_map
) {
2131 "failed to allocate memory for key map\n");
2135 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2136 dbg_printk(TPACPI_DBG_INIT
,
2137 "using Lenovo default hot key map\n");
2138 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2139 TPACPI_HOTKEY_MAP_SIZE
);
2141 dbg_printk(TPACPI_DBG_INIT
,
2142 "using IBM default hot key map\n");
2143 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2144 TPACPI_HOTKEY_MAP_SIZE
);
2147 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2148 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2149 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2150 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2151 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2152 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2153 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2154 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2155 set_bit(hotkey_keycode_map
[i
],
2156 tpacpi_inputdev
->keybit
);
2158 if (i
< sizeof(hotkey_reserved_mask
)*8)
2159 hotkey_reserved_mask
|= 1 << i
;
2163 if (tp_features
.hotkey_wlsw
) {
2164 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2165 set_bit(SW_RADIO
, tpacpi_inputdev
->swbit
);
2167 if (tp_features
.hotkey_tablet
) {
2168 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2169 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2172 /* Do not issue duplicate brightness change events to
2174 if (!tp_features
.bright_acpimode
)
2175 /* update bright_acpimode... */
2176 tpacpi_check_std_acpi_brightness_support();
2178 if (tp_features
.bright_acpimode
) {
2180 "This ThinkPad has standard ACPI backlight "
2181 "brightness control, supported by the ACPI "
2183 printk(TPACPI_NOTICE
2184 "Disabling thinkpad-acpi brightness events "
2187 /* The hotkey_reserved_mask change below is not
2188 * necessary while the keys are at KEY_RESERVED in the
2189 * default map, but better safe than sorry, leave it
2190 * here as a marker of what we have to do, especially
2191 * when we finally become able to set this at runtime
2192 * on response to X.org requests */
2193 hotkey_reserved_mask
|=
2194 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
2195 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
2198 dbg_printk(TPACPI_DBG_INIT
,
2199 "enabling hot key handling\n");
2200 res
= hotkey_status_set(1);
2203 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2204 & ~hotkey_reserved_mask
)
2205 | hotkey_orig_mask
);
2206 if (res
< 0 && res
!= -ENXIO
)
2209 dbg_printk(TPACPI_DBG_INIT
,
2210 "legacy hot key reporting over procfs %s\n",
2211 (hotkey_report_mode
< 2) ?
2212 "enabled" : "disabled");
2214 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2215 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2217 hotkey_poll_setup_safe(1);
2218 tpacpi_input_send_radiosw();
2219 tpacpi_input_send_tabletsw();
2222 return (tp_features
.hotkey
)? 0 : 1;
2225 static void hotkey_exit(void)
2227 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2228 hotkey_poll_stop_sync();
2231 if (tp_features
.hotkey
) {
2232 dbg_printk(TPACPI_DBG_EXIT
,
2233 "restoring original hot key mask\n");
2234 /* no short-circuit boolean operator below! */
2235 if ((hotkey_mask_set(hotkey_orig_mask
) |
2236 hotkey_status_set(hotkey_orig_status
)) != 0)
2238 "failed to restore hot key mask "
2239 "to BIOS defaults\n");
2242 if (hotkey_dev_attributes
) {
2243 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2244 hotkey_dev_attributes
= NULL
;
2248 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2251 unsigned int scancode
;
2256 if (event
!= 0x80) {
2258 "unknown HKEY notification event %d\n", event
);
2259 /* forward it to userspace, maybe it knows how to handle it */
2260 acpi_bus_generate_netlink_event(
2261 ibm
->acpi
->device
->pnp
.device_class
,
2262 ibm
->acpi
->device
->dev
.bus_id
,
2268 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2269 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2282 switch (hkey
>> 12) {
2284 /* 0x1000-0x1FFF: key presses */
2285 scancode
= hkey
& 0xfff;
2286 if (scancode
> 0 && scancode
< 0x21) {
2288 if (!(hotkey_source_mask
& (1 << scancode
))) {
2289 tpacpi_input_send_key(scancode
);
2301 case 0x2304: /* suspend, undock */
2302 case 0x2404: /* hibernation, undock */
2303 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2306 case 0x2305: /* suspend, bay eject */
2307 case 0x2405: /* hibernation, bay eject */
2308 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2314 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2316 "woke up due to a hot-unplug "
2318 hotkey_wakeup_reason_notify_change();
2322 /* bay-related wakeups */
2323 if (hkey
== 0x3003) {
2324 hotkey_autosleep_ack
= 1;
2327 hotkey_wakeup_hotunplug_complete_notify_change();
2333 /* dock-related wakeups */
2334 if (hkey
== 0x4003) {
2335 hotkey_autosleep_ack
= 1;
2338 hotkey_wakeup_hotunplug_complete_notify_change();
2344 /* 0x5000-0x5FFF: human interface helpers */
2346 case 0x5010: /* Lenovo new BIOS: brightness changed */
2347 case 0x500b: /* X61t: tablet pen inserted into bay */
2348 case 0x500c: /* X61t: tablet pen removed from bay */
2350 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2351 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2352 tpacpi_input_send_tabletsw();
2353 hotkey_tablet_mode_notify_change();
2358 /* LID switch events. Do not propagate */
2366 /* 0x7000-0x7FFF: misc */
2367 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2368 tpacpi_input_send_radiosw();
2369 hotkey_radio_sw_notify_change();
2373 /* fallthrough to default */
2378 printk(TPACPI_NOTICE
2379 "unhandled HKEY event 0x%04x\n", hkey
);
2383 if (!ignore_acpi_ev
&&
2384 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2385 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2389 /* netlink events */
2390 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2391 acpi_bus_generate_netlink_event(
2392 ibm
->acpi
->device
->pnp
.device_class
,
2393 ibm
->acpi
->device
->dev
.bus_id
,
2399 static void hotkey_suspend(pm_message_t state
)
2401 /* Do these on suspend, we get the events on early resume! */
2402 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2403 hotkey_autosleep_ack
= 0;
2406 static void hotkey_resume(void)
2408 if (hotkey_mask_get())
2410 "error while trying to read hot key mask "
2412 tpacpi_input_send_radiosw();
2413 hotkey_radio_sw_notify_change();
2414 hotkey_tablet_mode_notify_change();
2415 hotkey_wakeup_reason_notify_change();
2416 hotkey_wakeup_hotunplug_complete_notify_change();
2417 hotkey_poll_setup_safe(0);
2420 /* procfs -------------------------------------------------------------- */
2421 static int hotkey_read(char *p
)
2426 if (!tp_features
.hotkey
) {
2427 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2431 if (mutex_lock_interruptible(&hotkey_mutex
))
2432 return -ERESTARTSYS
;
2433 res
= hotkey_status_get(&status
);
2435 res
= hotkey_mask_get();
2436 mutex_unlock(&hotkey_mutex
);
2440 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2441 if (tp_features
.hotkey_mask
) {
2442 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2443 len
+= sprintf(p
+ len
,
2444 "commands:\tenable, disable, reset, <mask>\n");
2446 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2447 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2453 static int hotkey_write(char *buf
)
2459 if (!tp_features
.hotkey
)
2462 if (mutex_lock_interruptible(&hotkey_mutex
))
2463 return -ERESTARTSYS
;
2469 while ((cmd
= next_cmd(&buf
))) {
2470 if (strlencmp(cmd
, "enable") == 0) {
2472 } else if (strlencmp(cmd
, "disable") == 0) {
2474 } else if (strlencmp(cmd
, "reset") == 0) {
2475 status
= hotkey_orig_status
;
2476 mask
= hotkey_orig_mask
;
2477 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2479 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2487 res
= hotkey_status_set(status
);
2489 if (!res
&& mask
!= hotkey_mask
)
2490 res
= hotkey_mask_set(mask
);
2493 mutex_unlock(&hotkey_mutex
);
2497 static const struct acpi_device_id ibm_htk_device_ids
[] = {
2498 {TPACPI_ACPI_HKEY_HID
, 0},
2502 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
2503 .hid
= ibm_htk_device_ids
,
2504 .notify
= hotkey_notify
,
2505 .handle
= &hkey_handle
,
2506 .type
= ACPI_DEVICE_NOTIFY
,
2509 static struct ibm_struct hotkey_driver_data
= {
2511 .read
= hotkey_read
,
2512 .write
= hotkey_write
,
2513 .exit
= hotkey_exit
,
2514 .resume
= hotkey_resume
,
2515 .suspend
= hotkey_suspend
,
2516 .acpi
= &ibm_hotkey_acpidriver
,
2519 /*************************************************************************
2520 * Bluetooth subdriver
2524 /* ACPI GBDC/SBDC bits */
2525 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
2526 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
2527 TP_ACPI_BLUETOOTH_UNK
= 0x04, /* unknown function */
2530 static int bluetooth_get_radiosw(void);
2531 static int bluetooth_set_radiosw(int radio_on
);
2533 /* sysfs bluetooth enable ---------------------------------------------- */
2534 static ssize_t
bluetooth_enable_show(struct device
*dev
,
2535 struct device_attribute
*attr
,
2540 status
= bluetooth_get_radiosw();
2544 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
? 1 : 0);
2547 static ssize_t
bluetooth_enable_store(struct device
*dev
,
2548 struct device_attribute
*attr
,
2549 const char *buf
, size_t count
)
2554 if (parse_strtoul(buf
, 1, &t
))
2557 res
= bluetooth_set_radiosw(t
);
2559 return (res
) ? res
: count
;
2562 static struct device_attribute dev_attr_bluetooth_enable
=
2563 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
2564 bluetooth_enable_show
, bluetooth_enable_store
);
2566 /* --------------------------------------------------------------------- */
2568 static struct attribute
*bluetooth_attributes
[] = {
2569 &dev_attr_bluetooth_enable
.attr
,
2573 static const struct attribute_group bluetooth_attr_group
= {
2574 .attrs
= bluetooth_attributes
,
2577 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
2582 vdbg_printk(TPACPI_DBG_INIT
, "initializing bluetooth subdriver\n");
2584 TPACPI_ACPIHANDLE_INIT(hkey
);
2586 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2587 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2588 tp_features
.bluetooth
= hkey_handle
&&
2589 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
2591 vdbg_printk(TPACPI_DBG_INIT
, "bluetooth is %s, status 0x%02x\n",
2592 str_supported(tp_features
.bluetooth
),
2595 if (tp_features
.bluetooth
) {
2596 if (!(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
2597 /* no bluetooth hardware present in system */
2598 tp_features
.bluetooth
= 0;
2599 dbg_printk(TPACPI_DBG_INIT
,
2600 "bluetooth hardware not installed\n");
2602 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2603 &bluetooth_attr_group
);
2609 return (tp_features
.bluetooth
)? 0 : 1;
2612 static void bluetooth_exit(void)
2614 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2615 &bluetooth_attr_group
);
2618 static int bluetooth_get_radiosw(void)
2622 if (!tp_features
.bluetooth
)
2625 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2628 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0);
2631 static int bluetooth_set_radiosw(int radio_on
)
2635 if (!tp_features
.bluetooth
)
2638 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2641 status
|= TP_ACPI_BLUETOOTH_RADIOSSW
;
2643 status
&= ~TP_ACPI_BLUETOOTH_RADIOSSW
;
2644 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
2650 /* procfs -------------------------------------------------------------- */
2651 static int bluetooth_read(char *p
)
2654 int status
= bluetooth_get_radiosw();
2656 if (!tp_features
.bluetooth
)
2657 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2659 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2660 (status
)? "enabled" : "disabled");
2661 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2667 static int bluetooth_write(char *buf
)
2671 if (!tp_features
.bluetooth
)
2674 while ((cmd
= next_cmd(&buf
))) {
2675 if (strlencmp(cmd
, "enable") == 0) {
2676 bluetooth_set_radiosw(1);
2677 } else if (strlencmp(cmd
, "disable") == 0) {
2678 bluetooth_set_radiosw(0);
2686 static struct ibm_struct bluetooth_driver_data
= {
2687 .name
= "bluetooth",
2688 .read
= bluetooth_read
,
2689 .write
= bluetooth_write
,
2690 .exit
= bluetooth_exit
,
2693 /*************************************************************************
2698 /* ACPI GWAN/SWAN bits */
2699 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
2700 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
2701 TP_ACPI_WANCARD_UNK
= 0x04, /* unknown function */
2704 static int wan_get_radiosw(void);
2705 static int wan_set_radiosw(int radio_on
);
2707 /* sysfs wan enable ---------------------------------------------------- */
2708 static ssize_t
wan_enable_show(struct device
*dev
,
2709 struct device_attribute
*attr
,
2714 status
= wan_get_radiosw();
2718 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
? 1 : 0);
2721 static ssize_t
wan_enable_store(struct device
*dev
,
2722 struct device_attribute
*attr
,
2723 const char *buf
, size_t count
)
2728 if (parse_strtoul(buf
, 1, &t
))
2731 res
= wan_set_radiosw(t
);
2733 return (res
) ? res
: count
;
2736 static struct device_attribute dev_attr_wan_enable
=
2737 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
2738 wan_enable_show
, wan_enable_store
);
2740 /* --------------------------------------------------------------------- */
2742 static struct attribute
*wan_attributes
[] = {
2743 &dev_attr_wan_enable
.attr
,
2747 static const struct attribute_group wan_attr_group
= {
2748 .attrs
= wan_attributes
,
2751 static int __init
wan_init(struct ibm_init_struct
*iibm
)
2756 vdbg_printk(TPACPI_DBG_INIT
, "initializing wan subdriver\n");
2758 TPACPI_ACPIHANDLE_INIT(hkey
);
2760 tp_features
.wan
= hkey_handle
&&
2761 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
2763 vdbg_printk(TPACPI_DBG_INIT
, "wan is %s, status 0x%02x\n",
2764 str_supported(tp_features
.wan
),
2767 if (tp_features
.wan
) {
2768 if (!(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
2769 /* no wan hardware present in system */
2770 tp_features
.wan
= 0;
2771 dbg_printk(TPACPI_DBG_INIT
,
2772 "wan hardware not installed\n");
2774 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2781 return (tp_features
.wan
)? 0 : 1;
2784 static void wan_exit(void)
2786 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2790 static int wan_get_radiosw(void)
2794 if (!tp_features
.wan
)
2797 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2800 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0);
2803 static int wan_set_radiosw(int radio_on
)
2807 if (!tp_features
.wan
)
2810 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2813 status
|= TP_ACPI_WANCARD_RADIOSSW
;
2815 status
&= ~TP_ACPI_WANCARD_RADIOSSW
;
2816 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
2822 /* procfs -------------------------------------------------------------- */
2823 static int wan_read(char *p
)
2826 int status
= wan_get_radiosw();
2828 if (!tp_features
.wan
)
2829 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2831 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2832 (status
)? "enabled" : "disabled");
2833 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2839 static int wan_write(char *buf
)
2843 if (!tp_features
.wan
)
2846 while ((cmd
= next_cmd(&buf
))) {
2847 if (strlencmp(cmd
, "enable") == 0) {
2849 } else if (strlencmp(cmd
, "disable") == 0) {
2858 static struct ibm_struct wan_driver_data
= {
2863 .flags
.experimental
= 1,
2866 /*************************************************************************
2870 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2872 enum video_access_mode
{
2873 TPACPI_VIDEO_NONE
= 0,
2874 TPACPI_VIDEO_570
, /* 570 */
2875 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
2876 TPACPI_VIDEO_NEW
, /* all others */
2879 enum { /* video status flags, based on VIDEO_570 */
2880 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
2881 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
2882 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
2885 enum { /* TPACPI_VIDEO_570 constants */
2886 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
2887 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
2888 * video_status_flags */
2889 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
2890 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
2893 static enum video_access_mode video_supported
;
2894 static int video_orig_autosw
;
2896 static int video_autosw_get(void);
2897 static int video_autosw_set(int enable
);
2899 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2901 static int __init
video_init(struct ibm_init_struct
*iibm
)
2905 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
2907 TPACPI_ACPIHANDLE_INIT(vid
);
2908 TPACPI_ACPIHANDLE_INIT(vid2
);
2910 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
2911 /* G41, assume IVGA doesn't change */
2912 vid_handle
= vid2_handle
;
2915 /* video switching not supported on R30, R31 */
2916 video_supported
= TPACPI_VIDEO_NONE
;
2917 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
2919 video_supported
= TPACPI_VIDEO_570
;
2920 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
2921 /* 600e/x, 770e, 770x */
2922 video_supported
= TPACPI_VIDEO_770
;
2925 video_supported
= TPACPI_VIDEO_NEW
;
2927 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
2928 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
2931 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
2934 static void video_exit(void)
2936 dbg_printk(TPACPI_DBG_EXIT
,
2937 "restoring original video autoswitch mode\n");
2938 if (video_autosw_set(video_orig_autosw
))
2939 printk(TPACPI_ERR
"error while trying to restore original "
2940 "video autoswitch mode\n");
2943 static int video_outputsw_get(void)
2948 switch (video_supported
) {
2949 case TPACPI_VIDEO_570
:
2950 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
2951 TP_ACPI_VIDEO_570_PHSCMD
))
2953 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
2955 case TPACPI_VIDEO_770
:
2956 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
2959 status
|= TP_ACPI_VIDEO_S_LCD
;
2960 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
2963 status
|= TP_ACPI_VIDEO_S_CRT
;
2965 case TPACPI_VIDEO_NEW
:
2966 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
2967 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
2970 status
|= TP_ACPI_VIDEO_S_CRT
;
2972 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
2973 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
2976 status
|= TP_ACPI_VIDEO_S_LCD
;
2977 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
2980 status
|= TP_ACPI_VIDEO_S_DVI
;
2989 static int video_outputsw_set(int status
)
2994 switch (video_supported
) {
2995 case TPACPI_VIDEO_570
:
2996 res
= acpi_evalf(NULL
, NULL
,
2997 "\\_SB.PHS2", "vdd",
2998 TP_ACPI_VIDEO_570_PHS2CMD
,
2999 status
| TP_ACPI_VIDEO_570_PHS2SET
);
3001 case TPACPI_VIDEO_770
:
3002 autosw
= video_autosw_get();
3006 res
= video_autosw_set(1);
3009 res
= acpi_evalf(vid_handle
, NULL
,
3010 "ASWT", "vdd", status
* 0x100, 0);
3011 if (!autosw
&& video_autosw_set(autosw
)) {
3013 "video auto-switch left enabled due to error\n");
3017 case TPACPI_VIDEO_NEW
:
3018 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
3019 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
3025 return (res
)? 0 : -EIO
;
3028 static int video_autosw_get(void)
3032 switch (video_supported
) {
3033 case TPACPI_VIDEO_570
:
3034 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
3037 case TPACPI_VIDEO_770
:
3038 case TPACPI_VIDEO_NEW
:
3039 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
3049 static int video_autosw_set(int enable
)
3051 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
3056 static int video_outputsw_cycle(void)
3058 int autosw
= video_autosw_get();
3064 switch (video_supported
) {
3065 case TPACPI_VIDEO_570
:
3066 res
= video_autosw_set(1);
3069 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
3071 case TPACPI_VIDEO_770
:
3072 case TPACPI_VIDEO_NEW
:
3073 res
= video_autosw_set(1);
3076 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
3081 if (!autosw
&& video_autosw_set(autosw
)) {
3083 "video auto-switch left enabled due to error\n");
3087 return (res
)? 0 : -EIO
;
3090 static int video_expand_toggle(void)
3092 switch (video_supported
) {
3093 case TPACPI_VIDEO_570
:
3094 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
3096 case TPACPI_VIDEO_770
:
3097 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
3099 case TPACPI_VIDEO_NEW
:
3100 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
3108 static int video_read(char *p
)
3113 if (video_supported
== TPACPI_VIDEO_NONE
) {
3114 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3118 status
= video_outputsw_get();
3122 autosw
= video_autosw_get();
3126 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3127 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
3128 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
3129 if (video_supported
== TPACPI_VIDEO_NEW
)
3130 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
3131 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
3132 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
3133 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
3134 if (video_supported
== TPACPI_VIDEO_NEW
)
3135 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
3136 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
3137 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
3142 static int video_write(char *buf
)
3145 int enable
, disable
, status
;
3148 if (video_supported
== TPACPI_VIDEO_NONE
)
3154 while ((cmd
= next_cmd(&buf
))) {
3155 if (strlencmp(cmd
, "lcd_enable") == 0) {
3156 enable
|= TP_ACPI_VIDEO_S_LCD
;
3157 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
3158 disable
|= TP_ACPI_VIDEO_S_LCD
;
3159 } else if (strlencmp(cmd
, "crt_enable") == 0) {
3160 enable
|= TP_ACPI_VIDEO_S_CRT
;
3161 } else if (strlencmp(cmd
, "crt_disable") == 0) {
3162 disable
|= TP_ACPI_VIDEO_S_CRT
;
3163 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3164 strlencmp(cmd
, "dvi_enable") == 0) {
3165 enable
|= TP_ACPI_VIDEO_S_DVI
;
3166 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3167 strlencmp(cmd
, "dvi_disable") == 0) {
3168 disable
|= TP_ACPI_VIDEO_S_DVI
;
3169 } else if (strlencmp(cmd
, "auto_enable") == 0) {
3170 res
= video_autosw_set(1);
3173 } else if (strlencmp(cmd
, "auto_disable") == 0) {
3174 res
= video_autosw_set(0);
3177 } else if (strlencmp(cmd
, "video_switch") == 0) {
3178 res
= video_outputsw_cycle();
3181 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
3182 res
= video_expand_toggle();
3189 if (enable
|| disable
) {
3190 status
= video_outputsw_get();
3193 res
= video_outputsw_set((status
& ~disable
) | enable
);
3201 static struct ibm_struct video_driver_data
= {
3204 .write
= video_write
,
3208 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3210 /*************************************************************************
3211 * Light (thinklight) subdriver
3214 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3215 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
3217 static int __init
light_init(struct ibm_init_struct
*iibm
)
3219 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
3221 TPACPI_ACPIHANDLE_INIT(ledb
);
3222 TPACPI_ACPIHANDLE_INIT(lght
);
3223 TPACPI_ACPIHANDLE_INIT(cmos
);
3225 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3226 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
3228 if (tp_features
.light
)
3229 /* light status not supported on
3230 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3231 tp_features
.light_status
=
3232 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
3234 vdbg_printk(TPACPI_DBG_INIT
, "light is %s\n",
3235 str_supported(tp_features
.light
));
3237 return (tp_features
.light
)? 0 : 1;
3240 static int light_read(char *p
)
3245 if (!tp_features
.light
) {
3246 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3247 } else if (!tp_features
.light_status
) {
3248 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
3249 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3251 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
3253 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
3254 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3260 static int light_write(char *buf
)
3262 int cmos_cmd
, lght_cmd
;
3266 if (!tp_features
.light
)
3269 while ((cmd
= next_cmd(&buf
))) {
3270 if (strlencmp(cmd
, "on") == 0) {
3273 } else if (strlencmp(cmd
, "off") == 0) {
3279 success
= cmos_handle
?
3280 acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
) :
3281 acpi_evalf(lght_handle
, NULL
, NULL
, "vd", lght_cmd
);
3289 static struct ibm_struct light_driver_data
= {
3292 .write
= light_write
,
3295 /*************************************************************************
3299 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3301 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
3302 static int dock_read(char *p
);
3303 static int dock_write(char *buf
);
3305 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
3306 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3307 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3308 "\\_SB.PCI.ISA.SLCE", /* 570 */
3309 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3311 /* don't list other alternatives as we install a notify handler on the 570 */
3312 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
3314 static const struct acpi_device_id ibm_pci_device_ids
[] = {
3315 {PCI_ROOT_HID_STRING
, 0},
3319 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
3321 .notify
= dock_notify
,
3322 .handle
= &dock_handle
,
3323 .type
= ACPI_SYSTEM_NOTIFY
,
3326 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3327 * We just use it to get notifications of dock hotplug
3328 * in very old thinkpads */
3329 .hid
= ibm_pci_device_ids
,
3330 .notify
= dock_notify
,
3331 .handle
= &pci_handle
,
3332 .type
= ACPI_SYSTEM_NOTIFY
,
3336 static struct ibm_struct dock_driver_data
[2] = {
3340 .write
= dock_write
,
3341 .acpi
= &ibm_dock_acpidriver
[0],
3345 .acpi
= &ibm_dock_acpidriver
[1],
3349 #define dock_docked() (_sta(dock_handle) & 1)
3351 static int __init
dock_init(struct ibm_init_struct
*iibm
)
3353 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
3355 TPACPI_ACPIHANDLE_INIT(dock
);
3357 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
3358 str_supported(dock_handle
!= NULL
));
3360 return (dock_handle
)? 0 : 1;
3363 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
3367 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
3369 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
3370 dock_driver_data
[0].flags
.acpi_notify_installed
) {
3371 TPACPI_ACPIHANDLE_INIT(pci
);
3372 dock2_needed
= (pci_handle
!= NULL
);
3373 vdbg_printk(TPACPI_DBG_INIT
,
3374 "dock PCI handler for the TP 570 is %s\n",
3375 str_supported(dock2_needed
));
3377 vdbg_printk(TPACPI_DBG_INIT
,
3378 "dock subdriver part 2 not required\n");
3382 return (dock2_needed
)? 0 : 1;
3385 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
3387 int docked
= dock_docked();
3388 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
3389 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
3392 if (event
== 1 && !pci
) /* 570 */
3393 data
= 1; /* button */
3394 else if (event
== 1 && pci
) /* 570 */
3395 data
= 3; /* dock */
3396 else if (event
== 3 && docked
)
3397 data
= 1; /* button */
3398 else if (event
== 3 && !docked
)
3399 data
= 2; /* undock */
3400 else if (event
== 0 && docked
)
3401 data
= 3; /* dock */
3403 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
3404 event
, _sta(dock_handle
));
3405 data
= 0; /* unknown */
3407 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
3408 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
3409 ibm
->acpi
->device
->dev
.bus_id
,
3413 static int dock_read(char *p
)
3416 int docked
= dock_docked();
3419 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3421 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
3423 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
3424 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
3430 static int dock_write(char *buf
)
3437 while ((cmd
= next_cmd(&buf
))) {
3438 if (strlencmp(cmd
, "undock") == 0) {
3439 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
3440 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
3442 } else if (strlencmp(cmd
, "dock") == 0) {
3443 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
3452 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3454 /*************************************************************************
3458 #ifdef CONFIG_THINKPAD_ACPI_BAY
3460 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3461 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3462 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3463 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3464 ); /* A21e, R30, R31 */
3465 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
3466 "_EJ0", /* all others */
3467 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3468 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3469 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3471 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
3475 static int __init
bay_init(struct ibm_init_struct
*iibm
)
3477 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
3479 TPACPI_ACPIHANDLE_INIT(bay
);
3481 TPACPI_ACPIHANDLE_INIT(bay_ej
);
3482 TPACPI_ACPIHANDLE_INIT(bay2
);
3484 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
3486 tp_features
.bay_status
= bay_handle
&&
3487 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
3488 tp_features
.bay_status2
= bay2_handle
&&
3489 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
3491 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
3492 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
3493 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
3494 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
3496 vdbg_printk(TPACPI_DBG_INIT
,
3497 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3498 str_supported(tp_features
.bay_status
),
3499 str_supported(tp_features
.bay_eject
),
3500 str_supported(tp_features
.bay_status2
),
3501 str_supported(tp_features
.bay_eject2
));
3503 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
3504 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
3507 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
3509 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
3510 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
3511 ibm
->acpi
->device
->dev
.bus_id
,
3515 #define bay_occupied(b) (_sta(b##_handle) & 1)
3517 static int bay_read(char *p
)
3520 int occupied
= bay_occupied(bay
);
3521 int occupied2
= bay_occupied(bay2
);
3524 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3525 tp_features
.bay_status
?
3526 (occupied
? "occupied" : "unoccupied") :
3528 if (tp_features
.bay_status2
)
3529 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
3530 "occupied" : "unoccupied");
3532 eject
= tp_features
.bay_eject
&& occupied
;
3533 eject2
= tp_features
.bay_eject2
&& occupied2
;
3535 if (eject
&& eject2
)
3536 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
3538 len
+= sprintf(p
+ len
, "commands:\teject\n");
3540 len
+= sprintf(p
+ len
, "commands:\teject2\n");
3545 static int bay_write(char *buf
)
3549 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
3552 while ((cmd
= next_cmd(&buf
))) {
3553 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
3554 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
3556 } else if (tp_features
.bay_eject2
&&
3557 strlencmp(cmd
, "eject2") == 0) {
3558 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
3567 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
3568 .notify
= bay_notify
,
3569 .handle
= &bay_handle
,
3570 .type
= ACPI_SYSTEM_NOTIFY
,
3573 static struct ibm_struct bay_driver_data
= {
3577 .acpi
= &ibm_bay_acpidriver
,
3580 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3582 /*************************************************************************
3586 /* sysfs cmos_command -------------------------------------------------- */
3587 static ssize_t
cmos_command_store(struct device
*dev
,
3588 struct device_attribute
*attr
,
3589 const char *buf
, size_t count
)
3591 unsigned long cmos_cmd
;
3594 if (parse_strtoul(buf
, 21, &cmos_cmd
))
3597 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3598 return (res
)? res
: count
;
3601 static struct device_attribute dev_attr_cmos_command
=
3602 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
3604 /* --------------------------------------------------------------------- */
3606 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
3610 vdbg_printk(TPACPI_DBG_INIT
,
3611 "initializing cmos commands subdriver\n");
3613 TPACPI_ACPIHANDLE_INIT(cmos
);
3615 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
3616 str_supported(cmos_handle
!= NULL
));
3618 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3622 return (cmos_handle
)? 0 : 1;
3625 static void cmos_exit(void)
3627 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3630 static int cmos_read(char *p
)
3634 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3635 R30, R31, T20-22, X20-21 */
3637 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3639 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3640 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
3646 static int cmos_write(char *buf
)
3651 while ((cmd
= next_cmd(&buf
))) {
3652 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
3653 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
3658 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3666 static struct ibm_struct cmos_driver_data
= {
3669 .write
= cmos_write
,
3673 /*************************************************************************
3677 enum led_access_mode
{
3678 TPACPI_LED_NONE
= 0,
3679 TPACPI_LED_570
, /* 570 */
3680 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3681 TPACPI_LED_NEW
, /* all others */
3684 enum { /* For TPACPI_LED_OLD */
3685 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
3686 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
3687 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
3690 static enum led_access_mode led_supported
;
3692 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
3693 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3694 /* T20-22, X20-21 */
3695 "LED", /* all others */
3698 static int __init
led_init(struct ibm_init_struct
*iibm
)
3700 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
3702 TPACPI_ACPIHANDLE_INIT(led
);
3705 /* led not supported on R30, R31 */
3706 led_supported
= TPACPI_LED_NONE
;
3707 else if (strlencmp(led_path
, "SLED") == 0)
3709 led_supported
= TPACPI_LED_570
;
3710 else if (strlencmp(led_path
, "SYSL") == 0)
3711 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3712 led_supported
= TPACPI_LED_OLD
;
3715 led_supported
= TPACPI_LED_NEW
;
3717 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
3718 str_supported(led_supported
), led_supported
);
3720 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
3723 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3725 static int led_read(char *p
)
3729 if (!led_supported
) {
3730 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3733 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3735 if (led_supported
== TPACPI_LED_570
) {
3738 for (i
= 0; i
< 8; i
++) {
3739 if (!acpi_evalf(ec_handle
,
3740 &status
, "GLED", "dd", 1 << i
))
3742 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
3743 i
, led_status(status
));
3747 len
+= sprintf(p
+ len
, "commands:\t"
3748 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3753 /* off, on, blink */
3754 static const int led_sled_arg1
[] = { 0, 1, 3 };
3755 static const int led_exp_hlbl
[] = { 0, 0, 1 }; /* led# * */
3756 static const int led_exp_hlcl
[] = { 0, 1, 1 }; /* led# * */
3757 static const int led_led_arg1
[] = { 0, 0x80, 0xc0 };
3759 static int led_write(char *buf
)
3767 while ((cmd
= next_cmd(&buf
))) {
3768 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
3771 if (strstr(cmd
, "off")) {
3773 } else if (strstr(cmd
, "on")) {
3775 } else if (strstr(cmd
, "blink")) {
3780 if (led_supported
== TPACPI_LED_570
) {
3783 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
3784 led
, led_sled_arg1
[ind
]))
3786 } else if (led_supported
== TPACPI_LED_OLD
) {
3787 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3789 ret
= ec_write(TPACPI_LED_EC_HLMS
, led
);
3791 ret
= ec_write(TPACPI_LED_EC_HLBL
,
3792 led
* led_exp_hlbl
[ind
]);
3794 ret
= ec_write(TPACPI_LED_EC_HLCL
,
3795 led
* led_exp_hlcl
[ind
]);
3800 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
3801 led
, led_led_arg1
[ind
]))
3809 static struct ibm_struct led_driver_data
= {
3815 /*************************************************************************
3819 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
3821 static int __init
beep_init(struct ibm_init_struct
*iibm
)
3823 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
3825 TPACPI_ACPIHANDLE_INIT(beep
);
3827 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
3828 str_supported(beep_handle
!= NULL
));
3830 return (beep_handle
)? 0 : 1;
3833 static int beep_read(char *p
)
3838 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3840 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3841 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
3847 static int beep_write(char *buf
)
3855 while ((cmd
= next_cmd(&buf
))) {
3856 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
3857 beep_cmd
>= 0 && beep_cmd
<= 17) {
3861 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
3868 static struct ibm_struct beep_driver_data
= {
3871 .write
= beep_write
,
3874 /*************************************************************************
3878 enum thermal_access_mode
{
3879 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
3880 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
3881 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
3882 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
3883 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
3886 enum { /* TPACPI_THERMAL_TPEC_* */
3887 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
3888 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
3889 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
3892 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3893 struct ibm_thermal_sensors_struct
{
3894 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
3897 static enum thermal_access_mode thermal_read_mode
;
3899 /* idx is zero-based */
3900 static int thermal_get_sensor(int idx
, s32
*value
)
3906 t
= TP_EC_THERMAL_TMP0
;
3908 switch (thermal_read_mode
) {
3909 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3910 case TPACPI_THERMAL_TPEC_16
:
3911 if (idx
>= 8 && idx
<= 15) {
3912 t
= TP_EC_THERMAL_TMP8
;
3917 case TPACPI_THERMAL_TPEC_8
:
3919 if (!acpi_ec_read(t
+ idx
, &tmp
))
3921 *value
= tmp
* 1000;
3926 case TPACPI_THERMAL_ACPI_UPDT
:
3928 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
3929 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
3931 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
3933 *value
= (t
- 2732) * 100;
3938 case TPACPI_THERMAL_ACPI_TMP07
:
3940 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
3941 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
3943 if (t
> 127 || t
< -127)
3944 t
= TP_EC_THERMAL_TMP_NA
;
3950 case TPACPI_THERMAL_NONE
:
3958 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
3969 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
3972 for (i
= 0 ; i
< n
; i
++) {
3973 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
3981 /* sysfs temp##_input -------------------------------------------------- */
3983 static ssize_t
thermal_temp_input_show(struct device
*dev
,
3984 struct device_attribute
*attr
,
3987 struct sensor_device_attribute
*sensor_attr
=
3988 to_sensor_dev_attr(attr
);
3989 int idx
= sensor_attr
->index
;
3993 res
= thermal_get_sensor(idx
, &value
);
3996 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
3999 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
4002 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4003 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4004 thermal_temp_input_show, NULL, _idxB)
4006 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
4007 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4008 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4009 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4010 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4011 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4012 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4013 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4014 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4015 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4016 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4017 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4018 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4019 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4020 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4021 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4022 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4025 #define THERMAL_ATTRS(X) \
4026 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4028 static struct attribute
*thermal_temp_input_attr
[] = {
4048 static const struct attribute_group thermal_temp_input16_group
= {
4049 .attrs
= thermal_temp_input_attr
4052 static const struct attribute_group thermal_temp_input8_group
= {
4053 .attrs
= &thermal_temp_input_attr
[8]
4056 #undef THERMAL_SENSOR_ATTR_TEMP
4057 #undef THERMAL_ATTRS
4059 /* --------------------------------------------------------------------- */
4061 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
4068 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
4070 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
4072 if (thinkpad_id
.ec_model
) {
4074 * Direct EC access mode: sensors at registers
4075 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4076 * non-implemented, thermal sensors return 0x80 when
4081 for (i
= 0; i
< 8; i
++) {
4082 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
4088 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
4096 /* This is sheer paranoia, but we handle it anyway */
4099 "ThinkPad ACPI EC access misbehaving, "
4100 "falling back to ACPI TMPx access "
4102 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
4105 "ThinkPad ACPI EC access misbehaving, "
4106 "disabling thermal sensors access\n");
4107 thermal_read_mode
= TPACPI_THERMAL_NONE
;
4112 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
4114 } else if (acpi_tmp7
) {
4115 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
4116 /* 600e/x, 770e, 770x */
4117 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
4119 /* Standard ACPI TMPx access, max 8 sensors */
4120 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
4123 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4124 thermal_read_mode
= TPACPI_THERMAL_NONE
;
4127 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
4128 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
4131 switch (thermal_read_mode
) {
4132 case TPACPI_THERMAL_TPEC_16
:
4133 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4134 &thermal_temp_input16_group
);
4138 case TPACPI_THERMAL_TPEC_8
:
4139 case TPACPI_THERMAL_ACPI_TMP07
:
4140 case TPACPI_THERMAL_ACPI_UPDT
:
4141 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4142 &thermal_temp_input8_group
);
4146 case TPACPI_THERMAL_NONE
:
4154 static void thermal_exit(void)
4156 switch (thermal_read_mode
) {
4157 case TPACPI_THERMAL_TPEC_16
:
4158 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4159 &thermal_temp_input16_group
);
4161 case TPACPI_THERMAL_TPEC_8
:
4162 case TPACPI_THERMAL_ACPI_TMP07
:
4163 case TPACPI_THERMAL_ACPI_UPDT
:
4164 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4165 &thermal_temp_input16_group
);
4167 case TPACPI_THERMAL_NONE
:
4173 static int thermal_read(char *p
)
4177 struct ibm_thermal_sensors_struct t
;
4179 n
= thermal_get_sensors(&t
);
4180 if (unlikely(n
< 0))
4183 len
+= sprintf(p
+ len
, "temperatures:\t");
4186 for (i
= 0; i
< (n
- 1); i
++)
4187 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
4188 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
4190 len
+= sprintf(p
+ len
, "not supported\n");
4195 static struct ibm_struct thermal_driver_data
= {
4197 .read
= thermal_read
,
4198 .exit
= thermal_exit
,
4201 /*************************************************************************
4205 static u8 ecdump_regs
[256];
4207 static int ecdump_read(char *p
)
4213 len
+= sprintf(p
+ len
, "EC "
4214 " +00 +01 +02 +03 +04 +05 +06 +07"
4215 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4216 for (i
= 0; i
< 256; i
+= 16) {
4217 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
4218 for (j
= 0; j
< 16; j
++) {
4219 if (!acpi_ec_read(i
+ j
, &v
))
4221 if (v
!= ecdump_regs
[i
+ j
])
4222 len
+= sprintf(p
+ len
, " *%02x", v
);
4224 len
+= sprintf(p
+ len
, " %02x", v
);
4225 ecdump_regs
[i
+ j
] = v
;
4227 len
+= sprintf(p
+ len
, "\n");
4232 /* These are way too dangerous to advertise openly... */
4234 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
4235 " (<offset> is 00-ff, <value> is 00-ff)\n");
4236 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
4237 " (<offset> is 00-ff, <value> is 0-255)\n");
4242 static int ecdump_write(char *buf
)
4247 while ((cmd
= next_cmd(&buf
))) {
4248 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
4250 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
4254 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
4255 if (!acpi_ec_write(i
, v
))
4264 static struct ibm_struct ecdump_driver_data
= {
4266 .read
= ecdump_read
,
4267 .write
= ecdump_write
,
4268 .flags
.experimental
= 1,
4271 /*************************************************************************
4272 * Backlight/brightness subdriver
4275 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4277 static struct backlight_device
*ibm_backlight_device
;
4278 static int brightness_offset
= 0x31;
4279 static int brightness_mode
;
4280 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
4282 static struct mutex brightness_mutex
;
4285 * ThinkPads can read brightness from two places: EC 0x31, or
4286 * CMOS NVRAM byte 0x5E, bits 0-3.
4288 static int brightness_get(struct backlight_device
*bd
)
4290 u8 lec
= 0, lcmos
= 0, level
= 0;
4292 if (brightness_mode
& 1) {
4293 if (!acpi_ec_read(brightness_offset
, &lec
))
4295 lec
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4298 if (brightness_mode
& 2) {
4299 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
4300 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
4301 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
4302 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4306 if (brightness_mode
== 3 && lec
!= lcmos
) {
4308 "CMOS NVRAM (%u) and EC (%u) do not agree "
4309 "on display brightness level\n",
4310 (unsigned int) lcmos
,
4311 (unsigned int) lec
);
4318 /* May return EINTR which can always be mapped to ERESTARTSYS */
4319 static int brightness_set(int value
)
4321 int cmos_cmd
, inc
, i
, res
;
4324 if (value
> ((tp_features
.bright_16levels
)? 15 : 7))
4327 res
= mutex_lock_interruptible(&brightness_mutex
);
4331 current_value
= brightness_get(NULL
);
4332 if (current_value
< 0) {
4333 res
= current_value
;
4337 cmos_cmd
= value
> current_value
?
4338 TP_CMOS_BRIGHTNESS_UP
:
4339 TP_CMOS_BRIGHTNESS_DOWN
;
4340 inc
= (value
> current_value
)? 1 : -1;
4343 for (i
= current_value
; i
!= value
; i
+= inc
) {
4344 if ((brightness_mode
& 2) &&
4345 issue_thinkpad_cmos_command(cmos_cmd
)) {
4349 if ((brightness_mode
& 1) &&
4350 !acpi_ec_write(brightness_offset
, i
+ inc
)) {
4357 mutex_unlock(&brightness_mutex
);
4361 /* sysfs backlight class ----------------------------------------------- */
4363 static int brightness_update_status(struct backlight_device
*bd
)
4365 /* it is the backlight class's job (caller) to handle
4366 * EINTR and other errors properly */
4367 return brightness_set(
4368 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
4369 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
4370 bd
->props
.brightness
: 0);
4373 static struct backlight_ops ibm_backlight_data
= {
4374 .get_brightness
= brightness_get
,
4375 .update_status
= brightness_update_status
,
4378 /* --------------------------------------------------------------------- */
4380 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
4384 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
4386 mutex_init(&brightness_mutex
);
4389 * We always attempt to detect acpi support, so as to switch
4390 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4391 * going to publish a backlight interface
4393 b
= tpacpi_check_std_acpi_brightness_support();
4395 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
4396 printk(TPACPI_NOTICE
4397 "Lenovo BIOS switched to ACPI backlight "
4400 if (brightness_enable
> 1) {
4401 printk(TPACPI_NOTICE
4402 "standard ACPI backlight interface "
4403 "available, not loading native one...\n");
4408 if (!brightness_enable
) {
4409 dbg_printk(TPACPI_DBG_INIT
,
4410 "brightness support disabled by "
4411 "module parameter\n");
4417 "Unsupported brightness interface, "
4418 "please contact %s\n", TPACPI_MAIL
);
4422 tp_features
.bright_16levels
= 1;
4424 if (!brightness_mode
) {
4425 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
4426 brightness_mode
= 2;
4428 brightness_mode
= 3;
4430 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
4434 if (brightness_mode
> 3)
4437 b
= brightness_get(NULL
);
4441 if (tp_features
.bright_16levels
)
4443 "detected a 16-level brightness capable ThinkPad\n");
4445 ibm_backlight_device
= backlight_device_register(
4446 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
4447 &ibm_backlight_data
);
4448 if (IS_ERR(ibm_backlight_device
)) {
4449 printk(TPACPI_ERR
"Could not register backlight device\n");
4450 return PTR_ERR(ibm_backlight_device
);
4452 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
4454 ibm_backlight_device
->props
.max_brightness
=
4455 (tp_features
.bright_16levels
)? 15 : 7;
4456 ibm_backlight_device
->props
.brightness
= b
;
4457 backlight_update_status(ibm_backlight_device
);
4462 static void brightness_exit(void)
4464 if (ibm_backlight_device
) {
4465 vdbg_printk(TPACPI_DBG_EXIT
,
4466 "calling backlight_device_unregister()\n");
4467 backlight_device_unregister(ibm_backlight_device
);
4468 ibm_backlight_device
= NULL
;
4472 static int brightness_read(char *p
)
4477 level
= brightness_get(NULL
);
4479 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
4481 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
4482 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
4483 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
4484 " (<level> is 0-%d)\n",
4485 (tp_features
.bright_16levels
) ? 15 : 7);
4491 static int brightness_write(char *buf
)
4496 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
4498 level
= brightness_get(NULL
);
4502 while ((cmd
= next_cmd(&buf
))) {
4503 if (strlencmp(cmd
, "up") == 0) {
4504 if (level
< max_level
)
4506 } else if (strlencmp(cmd
, "down") == 0) {
4509 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
4510 level
>= 0 && level
<= max_level
) {
4517 * Now we know what the final level should be, so we try to set it.
4518 * Doing it this way makes the syscall restartable in case of EINTR
4520 rc
= brightness_set(level
);
4521 return (rc
== -EINTR
)? ERESTARTSYS
: rc
;
4524 static struct ibm_struct brightness_driver_data
= {
4525 .name
= "brightness",
4526 .read
= brightness_read
,
4527 .write
= brightness_write
,
4528 .exit
= brightness_exit
,
4531 /*************************************************************************
4535 static int volume_offset
= 0x30;
4537 static int volume_read(char *p
)
4542 if (!acpi_ec_read(volume_offset
, &level
)) {
4543 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
4545 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
4546 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
4547 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
4548 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
4549 " (<level> is 0-15)\n");
4555 static int volume_write(char *buf
)
4557 int cmos_cmd
, inc
, i
;
4559 int new_level
, new_mute
;
4562 while ((cmd
= next_cmd(&buf
))) {
4563 if (!acpi_ec_read(volume_offset
, &level
))
4565 new_mute
= mute
= level
& 0x40;
4566 new_level
= level
= level
& 0xf;
4568 if (strlencmp(cmd
, "up") == 0) {
4572 new_level
= level
== 15 ? 15 : level
+ 1;
4573 } else if (strlencmp(cmd
, "down") == 0) {
4577 new_level
= level
== 0 ? 0 : level
- 1;
4578 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
4579 new_level
>= 0 && new_level
<= 15) {
4581 } else if (strlencmp(cmd
, "mute") == 0) {
4586 if (new_level
!= level
) {
4587 /* mute doesn't change */
4589 cmos_cmd
= (new_level
> level
) ?
4590 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
4591 inc
= new_level
> level
? 1 : -1;
4593 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
4594 !acpi_ec_write(volume_offset
, level
)))
4597 for (i
= level
; i
!= new_level
; i
+= inc
)
4598 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
4599 !acpi_ec_write(volume_offset
, i
+ inc
))
4603 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
4604 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
4609 if (new_mute
!= mute
) {
4610 /* level doesn't change */
4612 cmos_cmd
= (new_mute
) ?
4613 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
4615 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
4616 !acpi_ec_write(volume_offset
, level
+ new_mute
))
4624 static struct ibm_struct volume_driver_data
= {
4626 .read
= volume_read
,
4627 .write
= volume_write
,
4630 /*************************************************************************
4637 * TPACPI_FAN_RD_ACPI_GFAN:
4638 * ACPI GFAN method: returns fan level
4640 * see TPACPI_FAN_WR_ACPI_SFAN
4641 * EC 0x2f (HFSP) not available if GFAN exists
4643 * TPACPI_FAN_WR_ACPI_SFAN:
4644 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4646 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4649 * TPACPI_FAN_WR_TPEC:
4650 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4651 * Supported on almost all ThinkPads
4653 * Fan speed changes of any sort (including those caused by the
4654 * disengaged mode) are usually done slowly by the firmware as the
4655 * maximum ammount of fan duty cycle change per second seems to be
4658 * Reading is not available if GFAN exists.
4659 * Writing is not available if SFAN exists.
4662 * 7 automatic mode engaged;
4663 * (default operation mode of the ThinkPad)
4664 * fan level is ignored in this mode.
4665 * 6 full speed mode (takes precedence over bit 7);
4666 * not available on all thinkpads. May disable
4667 * the tachometer while the fan controller ramps up
4668 * the speed (which can take up to a few *minutes*).
4669 * Speeds up fan to 100% duty-cycle, which is far above
4670 * the standard RPM levels. It is not impossible that
4671 * it could cause hardware damage.
4672 * 5-3 unused in some models. Extra bits for fan level
4673 * in others, but still useless as all values above
4674 * 7 map to the same speed as level 7 in these models.
4675 * 2-0 fan level (0..7 usually)
4677 * 0x07 = max (set when temperatures critical)
4678 * Some ThinkPads may have other levels, see
4679 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4681 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4682 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4683 * does so, its initial value is meaningless (0x07).
4685 * For firmware bugs, refer to:
4686 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4690 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4691 * Main fan tachometer reading (in RPM)
4693 * This register is present on all ThinkPads with a new-style EC, and
4694 * it is known not to be present on the A21m/e, and T22, as there is
4695 * something else in offset 0x84 according to the ACPI DSDT. Other
4696 * ThinkPads from this same time period (and earlier) probably lack the
4697 * tachometer as well.
4699 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4700 * was never fixed by IBM to report the EC firmware version string
4701 * probably support the tachometer (like the early X models), so
4702 * detecting it is quite hard. We need more data to know for sure.
4704 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4707 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4710 * For firmware bugs, refer to:
4711 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4713 * TPACPI_FAN_WR_ACPI_FANS:
4714 * ThinkPad X31, X40, X41. Not available in the X60.
4716 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4717 * high speed. ACPI DSDT seems to map these three speeds to levels
4718 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4719 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4721 * The speeds are stored on handles
4722 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4724 * There are three default speed sets, acessible as handles:
4725 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4727 * ACPI DSDT switches which set is in use depending on various
4730 * TPACPI_FAN_WR_TPEC is also available and should be used to
4731 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4732 * but the ACPI tables just mention level 7.
4735 enum { /* Fan control constants */
4736 fan_status_offset
= 0x2f, /* EC register 0x2f */
4737 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4738 * 0x84 must be read before 0x85 */
4740 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
4741 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
4743 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
4746 enum fan_status_access_mode
{
4747 TPACPI_FAN_NONE
= 0, /* No fan status or control */
4748 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
4749 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4752 enum fan_control_access_mode
{
4753 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
4754 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
4755 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
4756 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
4759 enum fan_control_commands
{
4760 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
4761 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
4762 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
4763 * and also watchdog cmd */
4766 static int fan_control_allowed
;
4768 static enum fan_status_access_mode fan_status_access_mode
;
4769 static enum fan_control_access_mode fan_control_access_mode
;
4770 static enum fan_control_commands fan_control_commands
;
4772 static u8 fan_control_initial_status
;
4773 static u8 fan_control_desired_level
;
4774 static int fan_watchdog_maxinterval
;
4776 static struct mutex fan_mutex
;
4778 static void fan_watchdog_fire(struct work_struct
*ignored
);
4779 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
4781 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
4782 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
4783 "\\FSPD", /* 600e/x, 770e, 770x */
4785 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
4786 "JFNS", /* 770x-JL */
4790 * Call with fan_mutex held
4792 static void fan_update_desired_level(u8 status
)
4795 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
4797 fan_control_desired_level
= 7;
4799 fan_control_desired_level
= status
;
4803 static int fan_get_status(u8
*status
)
4808 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4810 switch (fan_status_access_mode
) {
4811 case TPACPI_FAN_RD_ACPI_GFAN
:
4812 /* 570, 600e/x, 770e, 770x */
4814 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
4822 case TPACPI_FAN_RD_TPEC
:
4823 /* all except 570, 600e/x, 770e, 770x */
4824 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
4839 static int fan_get_status_safe(u8
*status
)
4844 if (mutex_lock_interruptible(&fan_mutex
))
4845 return -ERESTARTSYS
;
4846 rc
= fan_get_status(&s
);
4848 fan_update_desired_level(s
);
4849 mutex_unlock(&fan_mutex
);
4857 static int fan_get_speed(unsigned int *speed
)
4861 switch (fan_status_access_mode
) {
4862 case TPACPI_FAN_RD_TPEC
:
4863 /* all except 570, 600e/x, 770e, 770x */
4864 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
4865 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
4869 *speed
= (hi
<< 8) | lo
;
4880 static int fan_set_level(int level
)
4882 if (!fan_control_allowed
)
4885 switch (fan_control_access_mode
) {
4886 case TPACPI_FAN_WR_ACPI_SFAN
:
4887 if (level
>= 0 && level
<= 7) {
4888 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
4894 case TPACPI_FAN_WR_ACPI_FANS
:
4895 case TPACPI_FAN_WR_TPEC
:
4896 if ((level
!= TP_EC_FAN_AUTO
) &&
4897 (level
!= TP_EC_FAN_FULLSPEED
) &&
4898 ((level
< 0) || (level
> 7)))
4901 /* safety net should the EC not support AUTO
4902 * or FULLSPEED mode bits and just ignore them */
4903 if (level
& TP_EC_FAN_FULLSPEED
)
4904 level
|= 7; /* safety min speed 7 */
4905 else if (level
& TP_EC_FAN_AUTO
)
4906 level
|= 4; /* safety min speed 4 */
4908 if (!acpi_ec_write(fan_status_offset
, level
))
4911 tp_features
.fan_ctrl_status_undef
= 0;
4920 static int fan_set_level_safe(int level
)
4924 if (!fan_control_allowed
)
4927 if (mutex_lock_interruptible(&fan_mutex
))
4928 return -ERESTARTSYS
;
4930 if (level
== TPACPI_FAN_LAST_LEVEL
)
4931 level
= fan_control_desired_level
;
4933 rc
= fan_set_level(level
);
4935 fan_update_desired_level(level
);
4937 mutex_unlock(&fan_mutex
);
4941 static int fan_set_enable(void)
4946 if (!fan_control_allowed
)
4949 if (mutex_lock_interruptible(&fan_mutex
))
4950 return -ERESTARTSYS
;
4952 switch (fan_control_access_mode
) {
4953 case TPACPI_FAN_WR_ACPI_FANS
:
4954 case TPACPI_FAN_WR_TPEC
:
4955 rc
= fan_get_status(&s
);
4959 /* Don't go out of emergency fan mode */
4962 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
4965 if (!acpi_ec_write(fan_status_offset
, s
))
4968 tp_features
.fan_ctrl_status_undef
= 0;
4973 case TPACPI_FAN_WR_ACPI_SFAN
:
4974 rc
= fan_get_status(&s
);
4980 /* Set fan to at least level 4 */
4983 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
4993 mutex_unlock(&fan_mutex
);
4997 static int fan_set_disable(void)
5001 if (!fan_control_allowed
)
5004 if (mutex_lock_interruptible(&fan_mutex
))
5005 return -ERESTARTSYS
;
5008 switch (fan_control_access_mode
) {
5009 case TPACPI_FAN_WR_ACPI_FANS
:
5010 case TPACPI_FAN_WR_TPEC
:
5011 if (!acpi_ec_write(fan_status_offset
, 0x00))
5014 fan_control_desired_level
= 0;
5015 tp_features
.fan_ctrl_status_undef
= 0;
5019 case TPACPI_FAN_WR_ACPI_SFAN
:
5020 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
5023 fan_control_desired_level
= 0;
5031 mutex_unlock(&fan_mutex
);
5035 static int fan_set_speed(int speed
)
5039 if (!fan_control_allowed
)
5042 if (mutex_lock_interruptible(&fan_mutex
))
5043 return -ERESTARTSYS
;
5046 switch (fan_control_access_mode
) {
5047 case TPACPI_FAN_WR_ACPI_FANS
:
5048 if (speed
>= 0 && speed
<= 65535) {
5049 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
5050 speed
, speed
, speed
))
5060 mutex_unlock(&fan_mutex
);
5064 static void fan_watchdog_reset(void)
5066 static int fan_watchdog_active
;
5068 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
5071 if (fan_watchdog_active
)
5072 cancel_delayed_work(&fan_watchdog_task
);
5074 if (fan_watchdog_maxinterval
> 0 &&
5075 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
5076 fan_watchdog_active
= 1;
5077 if (!schedule_delayed_work(&fan_watchdog_task
,
5078 msecs_to_jiffies(fan_watchdog_maxinterval
5081 "failed to schedule the fan watchdog, "
5082 "watchdog will not trigger\n");
5085 fan_watchdog_active
= 0;
5088 static void fan_watchdog_fire(struct work_struct
*ignored
)
5092 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
5095 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
5096 rc
= fan_set_enable();
5098 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
5099 "will try again later...\n", -rc
);
5100 /* reschedule for later */
5101 fan_watchdog_reset();
5106 * SYSFS fan layout: hwmon compatible (device)
5109 * 0: "disengaged" mode
5111 * 2: native EC "auto" mode (recommended, hardware default)
5113 * pwm*: set speed in manual mode, ignored otherwise.
5114 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5117 * fan*_input: tachometer reading, RPM
5120 * SYSFS fan layout: extensions
5122 * fan_watchdog (driver):
5123 * fan watchdog interval in seconds, 0 disables (default), max 120
5126 /* sysfs fan pwm1_enable ----------------------------------------------- */
5127 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
5128 struct device_attribute
*attr
,
5134 res
= fan_get_status_safe(&status
);
5138 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5139 if (status
!= fan_control_initial_status
) {
5140 tp_features
.fan_ctrl_status_undef
= 0;
5142 /* Return most likely status. In fact, it
5143 * might be the only possible status */
5144 status
= TP_EC_FAN_AUTO
;
5148 if (status
& TP_EC_FAN_FULLSPEED
) {
5150 } else if (status
& TP_EC_FAN_AUTO
) {
5155 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
5158 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
5159 struct device_attribute
*attr
,
5160 const char *buf
, size_t count
)
5165 if (parse_strtoul(buf
, 2, &t
))
5170 level
= TP_EC_FAN_FULLSPEED
;
5173 level
= TPACPI_FAN_LAST_LEVEL
;
5176 level
= TP_EC_FAN_AUTO
;
5179 /* reserved for software-controlled auto mode */
5185 res
= fan_set_level_safe(level
);
5191 fan_watchdog_reset();
5196 static struct device_attribute dev_attr_fan_pwm1_enable
=
5197 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
5198 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
5200 /* sysfs fan pwm1 ------------------------------------------------------ */
5201 static ssize_t
fan_pwm1_show(struct device
*dev
,
5202 struct device_attribute
*attr
,
5208 res
= fan_get_status_safe(&status
);
5212 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5213 if (status
!= fan_control_initial_status
) {
5214 tp_features
.fan_ctrl_status_undef
= 0;
5216 status
= TP_EC_FAN_AUTO
;
5221 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
5222 status
= fan_control_desired_level
;
5227 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
5230 static ssize_t
fan_pwm1_store(struct device
*dev
,
5231 struct device_attribute
*attr
,
5232 const char *buf
, size_t count
)
5236 u8 status
, newlevel
;
5238 if (parse_strtoul(buf
, 255, &s
))
5241 /* scale down from 0-255 to 0-7 */
5242 newlevel
= (s
>> 5) & 0x07;
5244 if (mutex_lock_interruptible(&fan_mutex
))
5245 return -ERESTARTSYS
;
5247 rc
= fan_get_status(&status
);
5248 if (!rc
&& (status
&
5249 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
5250 rc
= fan_set_level(newlevel
);
5254 fan_update_desired_level(newlevel
);
5255 fan_watchdog_reset();
5259 mutex_unlock(&fan_mutex
);
5260 return (rc
)? rc
: count
;
5263 static struct device_attribute dev_attr_fan_pwm1
=
5264 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
5265 fan_pwm1_show
, fan_pwm1_store
);
5267 /* sysfs fan fan1_input ------------------------------------------------ */
5268 static ssize_t
fan_fan1_input_show(struct device
*dev
,
5269 struct device_attribute
*attr
,
5275 res
= fan_get_speed(&speed
);
5279 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
5282 static struct device_attribute dev_attr_fan_fan1_input
=
5283 __ATTR(fan1_input
, S_IRUGO
,
5284 fan_fan1_input_show
, NULL
);
5286 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5287 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
5290 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
5293 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
5294 const char *buf
, size_t count
)
5298 if (parse_strtoul(buf
, 120, &t
))
5301 if (!fan_control_allowed
)
5304 fan_watchdog_maxinterval
= t
;
5305 fan_watchdog_reset();
5310 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
5311 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
5313 /* --------------------------------------------------------------------- */
5314 static struct attribute
*fan_attributes
[] = {
5315 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
5316 &dev_attr_fan_fan1_input
.attr
,
5320 static const struct attribute_group fan_attr_group
= {
5321 .attrs
= fan_attributes
,
5324 static int __init
fan_init(struct ibm_init_struct
*iibm
)
5328 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
5330 mutex_init(&fan_mutex
);
5331 fan_status_access_mode
= TPACPI_FAN_NONE
;
5332 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5333 fan_control_commands
= 0;
5334 fan_watchdog_maxinterval
= 0;
5335 tp_features
.fan_ctrl_status_undef
= 0;
5336 fan_control_desired_level
= 7;
5338 TPACPI_ACPIHANDLE_INIT(fans
);
5339 TPACPI_ACPIHANDLE_INIT(gfan
);
5340 TPACPI_ACPIHANDLE_INIT(sfan
);
5343 /* 570, 600e/x, 770e, 770x */
5344 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
5346 /* all other ThinkPads: note that even old-style
5347 * ThinkPad ECs supports the fan control register */
5348 if (likely(acpi_ec_read(fan_status_offset
,
5349 &fan_control_initial_status
))) {
5350 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
5352 /* In some ThinkPads, neither the EC nor the ACPI
5353 * DSDT initialize the fan status, and it ends up
5354 * being set to 0x07 when it *could* be either
5357 * Enable for TP-1Y (T43), TP-78 (R51e),
5358 * TP-76 (R52), TP-70 (T43, R52), which are known
5360 if (fan_control_initial_status
== 0x07) {
5361 switch (thinkpad_id
.ec_model
) {
5362 case 0x5931: /* TP-1Y */
5363 case 0x3837: /* TP-78 */
5364 case 0x3637: /* TP-76 */
5365 case 0x3037: /* TP-70 */
5366 printk(TPACPI_NOTICE
5367 "fan_init: initial fan status "
5368 "is unknown, assuming it is "
5370 tp_features
.fan_ctrl_status_undef
= 1;
5376 "ThinkPad ACPI EC access misbehaving, "
5377 "fan status and control unavailable\n");
5384 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
5385 fan_control_commands
|=
5386 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
5389 /* gfan without sfan means no fan control */
5390 /* all other models implement TP EC 0x2f control */
5394 fan_control_access_mode
=
5395 TPACPI_FAN_WR_ACPI_FANS
;
5396 fan_control_commands
|=
5397 TPACPI_FAN_CMD_SPEED
|
5398 TPACPI_FAN_CMD_LEVEL
|
5399 TPACPI_FAN_CMD_ENABLE
;
5401 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
5402 fan_control_commands
|=
5403 TPACPI_FAN_CMD_LEVEL
|
5404 TPACPI_FAN_CMD_ENABLE
;
5409 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
5410 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
5411 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
5412 fan_status_access_mode
, fan_control_access_mode
);
5414 /* fan control master switch */
5415 if (!fan_control_allowed
) {
5416 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5417 fan_control_commands
= 0;
5418 dbg_printk(TPACPI_DBG_INIT
,
5419 "fan control features disabled by parameter\n");
5422 /* update fan_control_desired_level */
5423 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
5424 fan_get_status_safe(NULL
);
5426 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
5427 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
5428 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5431 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
5432 &driver_attr_fan_watchdog
);
5440 static void fan_exit(void)
5442 vdbg_printk(TPACPI_DBG_EXIT
,
5443 "cancelling any pending fan watchdog tasks\n");
5445 /* FIXME: can we really do this unconditionally? */
5446 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
5447 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
5448 &driver_attr_fan_watchdog
);
5450 cancel_delayed_work(&fan_watchdog_task
);
5451 flush_scheduled_work();
5454 static int fan_read(char *p
)
5459 unsigned int speed
= 0;
5461 switch (fan_status_access_mode
) {
5462 case TPACPI_FAN_RD_ACPI_GFAN
:
5463 /* 570, 600e/x, 770e, 770x */
5464 rc
= fan_get_status_safe(&status
);
5468 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
5470 (status
!= 0) ? "enabled" : "disabled", status
);
5473 case TPACPI_FAN_RD_TPEC
:
5474 /* all except 570, 600e/x, 770e, 770x */
5475 rc
= fan_get_status_safe(&status
);
5479 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5480 if (status
!= fan_control_initial_status
)
5481 tp_features
.fan_ctrl_status_undef
= 0;
5483 /* Return most likely status. In fact, it
5484 * might be the only possible status */
5485 status
= TP_EC_FAN_AUTO
;
5488 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
5489 (status
!= 0) ? "enabled" : "disabled");
5491 rc
= fan_get_speed(&speed
);
5495 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
5497 if (status
& TP_EC_FAN_FULLSPEED
)
5498 /* Disengaged mode takes precedence */
5499 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
5500 else if (status
& TP_EC_FAN_AUTO
)
5501 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
5503 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
5506 case TPACPI_FAN_NONE
:
5508 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5511 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
5512 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
5514 switch (fan_control_access_mode
) {
5515 case TPACPI_FAN_WR_ACPI_SFAN
:
5516 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
5520 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
5521 "auto, disengaged, full-speed)\n");
5526 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
5527 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
5528 "commands:\twatchdog <timeout> (<timeout> "
5529 "is 0 (off), 1-120 (seconds))\n");
5531 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
5532 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
5533 " (<speed> is 0-65535)\n");
5538 static int fan_write_cmd_level(const char *cmd
, int *rc
)
5542 if (strlencmp(cmd
, "level auto") == 0)
5543 level
= TP_EC_FAN_AUTO
;
5544 else if ((strlencmp(cmd
, "level disengaged") == 0) |
5545 (strlencmp(cmd
, "level full-speed") == 0))
5546 level
= TP_EC_FAN_FULLSPEED
;
5547 else if (sscanf(cmd
, "level %d", &level
) != 1)
5550 *rc
= fan_set_level_safe(level
);
5552 printk(TPACPI_ERR
"level command accepted for unsupported "
5553 "access mode %d", fan_control_access_mode
);
5558 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
5560 if (strlencmp(cmd
, "enable") != 0)
5563 *rc
= fan_set_enable();
5565 printk(TPACPI_ERR
"enable command accepted for unsupported "
5566 "access mode %d", fan_control_access_mode
);
5571 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
5573 if (strlencmp(cmd
, "disable") != 0)
5576 *rc
= fan_set_disable();
5578 printk(TPACPI_ERR
"disable command accepted for unsupported "
5579 "access mode %d", fan_control_access_mode
);
5584 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
5589 * Support speed <low> <medium> <high> ? */
5591 if (sscanf(cmd
, "speed %d", &speed
) != 1)
5594 *rc
= fan_set_speed(speed
);
5596 printk(TPACPI_ERR
"speed command accepted for unsupported "
5597 "access mode %d", fan_control_access_mode
);
5602 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
5606 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
5609 if (interval
< 0 || interval
> 120)
5612 fan_watchdog_maxinterval
= interval
;
5617 static int fan_write(char *buf
)
5622 while (!rc
&& (cmd
= next_cmd(&buf
))) {
5623 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
5624 fan_write_cmd_level(cmd
, &rc
)) &&
5625 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
5626 (fan_write_cmd_enable(cmd
, &rc
) ||
5627 fan_write_cmd_disable(cmd
, &rc
) ||
5628 fan_write_cmd_watchdog(cmd
, &rc
))) &&
5629 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
5630 fan_write_cmd_speed(cmd
, &rc
))
5634 fan_watchdog_reset();
5640 static struct ibm_struct fan_driver_data
= {
5647 /****************************************************************************
5648 ****************************************************************************
5652 ****************************************************************************
5653 ****************************************************************************/
5655 /* sysfs name ---------------------------------------------------------- */
5656 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
5657 struct device_attribute
*attr
,
5660 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
5663 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
5664 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
5666 /* --------------------------------------------------------------------- */
5669 static struct proc_dir_entry
*proc_dir
;
5672 * Module and infrastructure proble, init and exit handling
5675 static int force_load
;
5677 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5678 static const char * __init
str_supported(int is_supported
)
5680 static char text_unsupported
[] __initdata
= "not supported";
5682 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
5684 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5686 static void ibm_exit(struct ibm_struct
*ibm
)
5688 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
5690 list_del_init(&ibm
->all_drivers
);
5692 if (ibm
->flags
.acpi_notify_installed
) {
5693 dbg_printk(TPACPI_DBG_EXIT
,
5694 "%s: acpi_remove_notify_handler\n", ibm
->name
);
5696 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
5698 dispatch_acpi_notify
);
5699 ibm
->flags
.acpi_notify_installed
= 0;
5700 ibm
->flags
.acpi_notify_installed
= 0;
5703 if (ibm
->flags
.proc_created
) {
5704 dbg_printk(TPACPI_DBG_EXIT
,
5705 "%s: remove_proc_entry\n", ibm
->name
);
5706 remove_proc_entry(ibm
->name
, proc_dir
);
5707 ibm
->flags
.proc_created
= 0;
5710 if (ibm
->flags
.acpi_driver_registered
) {
5711 dbg_printk(TPACPI_DBG_EXIT
,
5712 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
5714 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
5715 kfree(ibm
->acpi
->driver
);
5716 ibm
->acpi
->driver
= NULL
;
5717 ibm
->flags
.acpi_driver_registered
= 0;
5720 if (ibm
->flags
.init_called
&& ibm
->exit
) {
5722 ibm
->flags
.init_called
= 0;
5725 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
5728 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
5731 struct ibm_struct
*ibm
= iibm
->data
;
5732 struct proc_dir_entry
*entry
;
5734 BUG_ON(ibm
== NULL
);
5736 INIT_LIST_HEAD(&ibm
->all_drivers
);
5738 if (ibm
->flags
.experimental
&& !experimental
)
5741 dbg_printk(TPACPI_DBG_INIT
,
5742 "probing for %s\n", ibm
->name
);
5745 ret
= iibm
->init(iibm
);
5747 return 0; /* probe failed */
5751 ibm
->flags
.init_called
= 1;
5755 if (ibm
->acpi
->hid
) {
5756 ret
= register_tpacpi_subdriver(ibm
);
5761 if (ibm
->acpi
->notify
) {
5762 ret
= setup_acpi_notify(ibm
);
5763 if (ret
== -ENODEV
) {
5764 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
5774 dbg_printk(TPACPI_DBG_INIT
,
5775 "%s installed\n", ibm
->name
);
5778 entry
= create_proc_entry(ibm
->name
,
5779 S_IFREG
| S_IRUGO
| S_IWUSR
,
5782 printk(TPACPI_ERR
"unable to create proc entry %s\n",
5787 entry
->owner
= THIS_MODULE
;
5789 entry
->read_proc
= &dispatch_procfs_read
;
5791 entry
->write_proc
= &dispatch_procfs_write
;
5792 ibm
->flags
.proc_created
= 1;
5795 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
5800 dbg_printk(TPACPI_DBG_INIT
,
5801 "%s: at error exit path with result %d\n",
5805 return (ret
< 0)? ret
: 0;
5810 static void __init
get_thinkpad_model_data(struct thinkpad_id_data
*tp
)
5812 const struct dmi_device
*dev
= NULL
;
5813 char ec_fw_string
[18];
5818 memset(tp
, 0, sizeof(*tp
));
5820 if (dmi_name_in_vendors("IBM"))
5821 tp
->vendor
= PCI_VENDOR_ID_IBM
;
5822 else if (dmi_name_in_vendors("LENOVO"))
5823 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
5827 tp
->bios_version_str
= kstrdup(dmi_get_system_info(DMI_BIOS_VERSION
),
5829 if (!tp
->bios_version_str
)
5831 tp
->bios_model
= tp
->bios_version_str
[0]
5832 | (tp
->bios_version_str
[1] << 8);
5835 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5836 * X32 or newer, all Z series; Some models must have an
5837 * up-to-date BIOS or they will not be detected.
5839 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5841 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
5842 if (sscanf(dev
->name
,
5843 "IBM ThinkPad Embedded Controller -[%17c",
5844 ec_fw_string
) == 1) {
5845 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
5846 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
5848 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
5849 tp
->ec_model
= ec_fw_string
[0]
5850 | (ec_fw_string
[1] << 8);
5855 tp
->model_str
= kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION
),
5857 if (strnicmp(tp
->model_str
, "ThinkPad", 8) != 0) {
5858 kfree(tp
->model_str
);
5859 tp
->model_str
= NULL
;
5863 static int __init
probe_for_thinkpad(void)
5871 * Non-ancient models have better DMI tagging, but very old models
5874 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
5876 /* ec is required because many other handles are relative to it */
5877 TPACPI_ACPIHANDLE_INIT(ec
);
5881 "Not yet supported ThinkPad detected!\n");
5886 * Risks a regression on very old machines, but reduces potential
5887 * false positives a damn great deal
5890 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
5892 if (!is_thinkpad
&& !force_load
)
5899 /* Module init, exit, parameters */
5901 static struct ibm_init_struct ibms_init
[] __initdata
= {
5903 .init
= thinkpad_acpi_driver_init
,
5904 .data
= &thinkpad_acpi_driver_data
,
5907 .init
= hotkey_init
,
5908 .data
= &hotkey_driver_data
,
5911 .init
= bluetooth_init
,
5912 .data
= &bluetooth_driver_data
,
5916 .data
= &wan_driver_data
,
5918 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5921 .data
= &video_driver_data
,
5926 .data
= &light_driver_data
,
5928 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5931 .data
= &dock_driver_data
[0],
5935 .data
= &dock_driver_data
[1],
5938 #ifdef CONFIG_THINKPAD_ACPI_BAY
5941 .data
= &bay_driver_data
,
5946 .data
= &cmos_driver_data
,
5950 .data
= &led_driver_data
,
5954 .data
= &beep_driver_data
,
5957 .init
= thermal_init
,
5958 .data
= &thermal_driver_data
,
5961 .data
= &ecdump_driver_data
,
5964 .init
= brightness_init
,
5965 .data
= &brightness_driver_data
,
5968 .data
= &volume_driver_data
,
5972 .data
= &fan_driver_data
,
5976 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
5979 struct ibm_struct
*ibm
;
5981 if (!kp
|| !kp
->name
|| !val
)
5984 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
5985 ibm
= ibms_init
[i
].data
;
5986 WARN_ON(ibm
== NULL
);
5988 if (!ibm
|| !ibm
->name
)
5991 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
5992 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
5994 strcpy(ibms_init
[i
].param
, val
);
5995 strcat(ibms_init
[i
].param
, ",");
6003 module_param(experimental
, int, 0);
6004 MODULE_PARM_DESC(experimental
,
6005 "Enables experimental features when non-zero");
6007 module_param_named(debug
, dbg_level
, uint
, 0);
6008 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
6010 module_param(force_load
, bool, 0);
6011 MODULE_PARM_DESC(force_load
,
6012 "Attempts to load the driver even on a "
6013 "mis-identified ThinkPad when true");
6015 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
6016 MODULE_PARM_DESC(fan_control
,
6017 "Enables setting fan parameters features when true");
6019 module_param_named(brightness_mode
, brightness_mode
, int, 0);
6020 MODULE_PARM_DESC(brightness_mode
,
6021 "Selects brightness control strategy: "
6022 "0=auto, 1=EC, 2=CMOS, 3=both");
6024 module_param(brightness_enable
, uint
, 0);
6025 MODULE_PARM_DESC(brightness_enable
,
6026 "Enables backlight control when 1, disables when 0");
6028 module_param(hotkey_report_mode
, uint
, 0);
6029 MODULE_PARM_DESC(hotkey_report_mode
,
6030 "used for backwards compatibility with userspace, "
6031 "see documentation");
6033 #define TPACPI_PARAM(feature) \
6034 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6035 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6036 "at module load, see documentation")
6038 TPACPI_PARAM(hotkey
);
6039 TPACPI_PARAM(bluetooth
);
6040 TPACPI_PARAM(video
);
6041 TPACPI_PARAM(light
);
6042 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6045 #ifdef CONFIG_THINKPAD_ACPI_BAY
6047 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6051 TPACPI_PARAM(ecdump
);
6052 TPACPI_PARAM(brightness
);
6053 TPACPI_PARAM(volume
);
6056 static void thinkpad_acpi_module_exit(void)
6058 struct ibm_struct
*ibm
, *itmp
;
6060 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
6062 list_for_each_entry_safe_reverse(ibm
, itmp
,
6063 &tpacpi_all_drivers
,
6068 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
6070 if (tpacpi_inputdev
) {
6071 if (tp_features
.input_device_registered
)
6072 input_unregister_device(tpacpi_inputdev
);
6074 input_free_device(tpacpi_inputdev
);
6078 hwmon_device_unregister(tpacpi_hwmon
);
6080 if (tp_features
.sensors_pdev_attrs_registered
)
6081 device_remove_file(&tpacpi_sensors_pdev
->dev
,
6082 &dev_attr_thinkpad_acpi_pdev_name
);
6083 if (tpacpi_sensors_pdev
)
6084 platform_device_unregister(tpacpi_sensors_pdev
);
6086 platform_device_unregister(tpacpi_pdev
);
6088 if (tp_features
.sensors_pdrv_attrs_registered
)
6089 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
6090 if (tp_features
.platform_drv_attrs_registered
)
6091 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
6093 if (tp_features
.sensors_pdrv_registered
)
6094 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
6096 if (tp_features
.platform_drv_registered
)
6097 platform_driver_unregister(&tpacpi_pdriver
);
6100 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
6102 kfree(thinkpad_id
.bios_version_str
);
6103 kfree(thinkpad_id
.ec_version_str
);
6104 kfree(thinkpad_id
.model_str
);
6108 static int __init
thinkpad_acpi_module_init(void)
6112 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
6114 /* Parameter checking */
6115 if (hotkey_report_mode
> 2)
6118 /* Driver-level probe */
6120 get_thinkpad_model_data(&thinkpad_id
);
6121 ret
= probe_for_thinkpad();
6123 thinkpad_acpi_module_exit();
6127 /* Driver initialization */
6129 TPACPI_ACPIHANDLE_INIT(ecrd
);
6130 TPACPI_ACPIHANDLE_INIT(ecwr
);
6132 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
6135 "unable to create proc dir " TPACPI_PROC_DIR
);
6136 thinkpad_acpi_module_exit();
6139 proc_dir
->owner
= THIS_MODULE
;
6141 ret
= platform_driver_register(&tpacpi_pdriver
);
6144 "unable to register main platform driver\n");
6145 thinkpad_acpi_module_exit();
6148 tp_features
.platform_drv_registered
= 1;
6150 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
6153 "unable to register hwmon platform driver\n");
6154 thinkpad_acpi_module_exit();
6157 tp_features
.sensors_pdrv_registered
= 1;
6159 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
6161 tp_features
.platform_drv_attrs_registered
= 1;
6162 ret
= tpacpi_create_driver_attributes(
6163 &tpacpi_hwmon_pdriver
.driver
);
6167 "unable to create sysfs driver attributes\n");
6168 thinkpad_acpi_module_exit();
6171 tp_features
.sensors_pdrv_attrs_registered
= 1;
6174 /* Device initialization */
6175 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
6177 if (IS_ERR(tpacpi_pdev
)) {
6178 ret
= PTR_ERR(tpacpi_pdev
);
6180 printk(TPACPI_ERR
"unable to register platform device\n");
6181 thinkpad_acpi_module_exit();
6184 tpacpi_sensors_pdev
= platform_device_register_simple(
6185 TPACPI_HWMON_DRVR_NAME
,
6187 if (IS_ERR(tpacpi_sensors_pdev
)) {
6188 ret
= PTR_ERR(tpacpi_sensors_pdev
);
6189 tpacpi_sensors_pdev
= NULL
;
6191 "unable to register hwmon platform device\n");
6192 thinkpad_acpi_module_exit();
6195 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
6196 &dev_attr_thinkpad_acpi_pdev_name
);
6199 "unable to create sysfs hwmon device attributes\n");
6200 thinkpad_acpi_module_exit();
6203 tp_features
.sensors_pdev_attrs_registered
= 1;
6204 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
6205 if (IS_ERR(tpacpi_hwmon
)) {
6206 ret
= PTR_ERR(tpacpi_hwmon
);
6207 tpacpi_hwmon
= NULL
;
6208 printk(TPACPI_ERR
"unable to register hwmon device\n");
6209 thinkpad_acpi_module_exit();
6212 mutex_init(&tpacpi_inputdev_send_mutex
);
6213 tpacpi_inputdev
= input_allocate_device();
6214 if (!tpacpi_inputdev
) {
6215 printk(TPACPI_ERR
"unable to allocate input device\n");
6216 thinkpad_acpi_module_exit();
6219 /* Prepare input device, but don't register */
6220 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
6221 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
6222 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
6223 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
6224 thinkpad_id
.vendor
:
6226 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
6227 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
6229 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
6230 ret
= ibm_init(&ibms_init
[i
]);
6231 if (ret
>= 0 && *ibms_init
[i
].param
)
6232 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
6234 thinkpad_acpi_module_exit();
6238 ret
= input_register_device(tpacpi_inputdev
);
6240 printk(TPACPI_ERR
"unable to register input device\n");
6241 thinkpad_acpi_module_exit();
6244 tp_features
.input_device_registered
= 1;
6247 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
6251 /* Please remove this in year 2009 */
6252 MODULE_ALIAS("ibm_acpi");
6255 * DMI matching for module autoloading
6257 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6258 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6260 * Only models listed in thinkwiki will be supported, so add yours
6261 * if it is not there yet.
6263 #define IBM_BIOS_MODULE_ALIAS(__type) \
6264 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6266 /* Non-ancient thinkpads */
6267 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6268 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6270 /* Ancient thinkpad BIOSes have to be identified by
6271 * BIOS type or model number, and there are far less
6272 * BIOS types than model numbers... */
6273 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6274 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6275 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6277 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6278 MODULE_DESCRIPTION(TPACPI_DESC
);
6279 MODULE_VERSION(TPACPI_VERSION
);
6280 MODULE_LICENSE("GPL");
6282 module_init(thinkpad_acpi_module_init
);
6283 module_exit(thinkpad_acpi_module_exit
);