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.21"
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 <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
79 #include <acpi/acnamesp.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2
= 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT
= 0x58,
97 TP_NVRAM_ADDR_VIDEO
= 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS
= 0x5e,
99 TP_NVRAM_ADDR_MIXER
= 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD
= 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM
= 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY
= 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE
= 0x80,
108 TP_NVRAM_MASK_THINKLIGHT
= 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND
= 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS
= 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS
= 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS
= 0,
113 TP_NVRAM_MASK_MUTE
= 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME
= 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME
= 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME
= 0,
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
127 /****************************************************************************
131 #define TPACPI_NAME "thinkpad"
132 #define TPACPI_DESC "ThinkPad ACPI Extras"
133 #define TPACPI_FILE TPACPI_NAME "_acpi"
134 #define TPACPI_URL "http://ibm-acpi.sf.net/"
135 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
137 #define TPACPI_PROC_DIR "ibm"
138 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
139 #define TPACPI_DRVR_NAME TPACPI_FILE
140 #define TPACPI_DRVR_SHORTNAME "tpacpi"
141 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
143 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
146 #define TPACPI_MAX_ACPI_ARGS 3
148 /* rfkill switches */
150 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
151 TPACPI_RFK_WWAN_SW_ID
,
155 #define TPACPI_LOG TPACPI_FILE ": "
156 #define TPACPI_ERR KERN_ERR TPACPI_LOG
157 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
158 #define TPACPI_INFO KERN_INFO TPACPI_LOG
159 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
161 #define TPACPI_DBG_ALL 0xffff
162 #define TPACPI_DBG_INIT 0x0001
163 #define TPACPI_DBG_EXIT 0x0002
164 #define dbg_printk(a_dbg_level, format, arg...) \
165 do { if (dbg_level & a_dbg_level) \
166 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
168 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
169 #define vdbg_printk(a_dbg_level, format, arg...) \
170 dbg_printk(a_dbg_level, format, ## arg)
171 static const char *str_supported(int is_supported
);
173 #define vdbg_printk(a_dbg_level, format, arg...)
176 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
177 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
178 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
181 /****************************************************************************
182 * Driver-wide structs and misc. variables
187 struct tp_acpi_drv_struct
{
188 const struct acpi_device_id
*hid
;
189 struct acpi_driver
*driver
;
191 void (*notify
) (struct ibm_struct
*, u32
);
194 struct acpi_device
*device
;
200 int (*read
) (char *);
201 int (*write
) (char *);
203 void (*resume
) (void);
204 void (*suspend
) (pm_message_t state
);
206 struct list_head all_drivers
;
208 struct tp_acpi_drv_struct
*acpi
;
211 u8 acpi_driver_registered
:1;
212 u8 acpi_notify_installed
:1;
219 struct ibm_init_struct
{
222 int (*init
) (struct ibm_init_struct
*);
223 struct ibm_struct
*data
;
227 #ifdef CONFIG_THINKPAD_ACPI_BAY
240 u32 bright_16levels
:1;
241 u32 bright_acpimode
:1;
243 u32 fan_ctrl_status_undef
:1;
244 u32 input_device_registered
:1;
245 u32 platform_drv_registered
:1;
246 u32 platform_drv_attrs_registered
:1;
247 u32 sensors_pdrv_registered
:1;
248 u32 sensors_pdrv_attrs_registered
:1;
249 u32 sensors_pdev_attrs_registered
:1;
250 u32 hotkey_poll_active
:1;
254 u16 hotkey_mask_ff
:1;
255 u16 bright_cmos_ec_unsync
:1;
258 struct thinkpad_id_data
{
259 unsigned int vendor
; /* ThinkPad vendor:
260 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
262 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
263 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
265 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
268 char *model_str
; /* ThinkPad T43 */
269 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
271 static struct thinkpad_id_data thinkpad_id
;
274 TPACPI_LIFE_INIT
= 0,
279 static int experimental
;
280 static u32 dbg_level
;
282 static struct workqueue_struct
*tpacpi_wq
;
290 /* Special LED class that can defer work */
291 struct tpacpi_led_classdev
{
292 struct led_classdev led_classdev
;
293 struct work_struct work
;
294 enum led_status_t new_state
;
298 /****************************************************************************
299 ****************************************************************************
301 * ACPI Helpers and device model
303 ****************************************************************************
304 ****************************************************************************/
306 /*************************************************************************
310 static acpi_handle root_handle
;
312 #define TPACPI_HANDLE(object, parent, paths...) \
313 static acpi_handle object##_handle; \
314 static acpi_handle *object##_parent = &parent##_handle; \
315 static char *object##_path; \
316 static char *object##_paths[] = { paths }
318 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
319 "\\_SB.PCI.ISA.EC", /* 570 */
320 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
321 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
322 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
323 "\\_SB.PCI0.ICH3.EC0", /* R31 */
324 "\\_SB.PCI0.LPC.EC", /* all others */
327 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
328 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
330 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
332 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
333 "\\CMS", /* R40, R40e */
336 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
337 "^HKEY", /* R30, R31 */
338 "HKEY", /* all others */
341 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
342 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
343 "\\_SB.PCI0.VID0", /* 770e */
344 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
345 "\\_SB.PCI0.AGP.VID", /* all others */
349 /*************************************************************************
353 static int acpi_evalf(acpi_handle handle
,
354 void *res
, char *method
, char *fmt
, ...)
357 struct acpi_object_list params
;
358 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
359 struct acpi_buffer result
, *resultp
;
360 union acpi_object out_obj
;
368 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
381 params
.pointer
= &in_objs
[0];
388 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
389 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
391 /* add more types as needed */
393 printk(TPACPI_ERR
"acpi_evalf() called "
394 "with invalid format character '%c'\n", c
);
400 if (res_type
!= 'v') {
401 result
.length
= sizeof(out_obj
);
402 result
.pointer
= &out_obj
;
407 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
412 *(int *)res
= out_obj
.integer
.value
;
413 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
416 success
= status
== AE_OK
;
418 /* add more types as needed */
420 printk(TPACPI_ERR
"acpi_evalf() called "
421 "with invalid format character '%c'\n", res_type
);
425 if (!success
&& !quiet
)
426 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
427 method
, fmt0
, status
);
432 static int acpi_ec_read(int i
, u8
*p
)
437 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
441 if (ec_read(i
, p
) < 0)
448 static int acpi_ec_write(int i
, u8 v
)
451 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
454 if (ec_write(i
, v
) < 0)
461 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
462 static int _sta(acpi_handle handle
)
466 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
473 static int issue_thinkpad_cmos_command(int cmos_cmd
)
478 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
484 /*************************************************************************
488 #define TPACPI_ACPIHANDLE_INIT(object) \
489 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
490 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
492 static void drv_acpi_handle_init(char *name
,
493 acpi_handle
*handle
, acpi_handle parent
,
494 char **paths
, int num_paths
, char **path
)
499 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
502 for (i
= 0; i
< num_paths
; i
++) {
503 status
= acpi_get_handle(parent
, paths
[i
], handle
);
504 if (ACPI_SUCCESS(status
)) {
506 dbg_printk(TPACPI_DBG_INIT
,
507 "Found ACPI handle %s for %s\n",
513 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
518 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
520 struct ibm_struct
*ibm
= data
;
522 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
525 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
528 ibm
->acpi
->notify(ibm
, event
);
531 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
538 if (!*ibm
->acpi
->handle
)
541 vdbg_printk(TPACPI_DBG_INIT
,
542 "setting up ACPI notify for %s\n", ibm
->name
);
544 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
546 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
551 acpi_driver_data(ibm
->acpi
->device
) = ibm
;
552 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
553 TPACPI_ACPI_EVENT_PREFIX
,
556 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
557 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
558 if (ACPI_FAILURE(status
)) {
559 if (status
== AE_ALREADY_EXISTS
) {
561 "another device driver is already "
562 "handling %s events\n", ibm
->name
);
565 "acpi_install_notify_handler(%s) failed: %d\n",
570 ibm
->flags
.acpi_notify_installed
= 1;
574 static int __init
tpacpi_device_add(struct acpi_device
*device
)
579 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
583 dbg_printk(TPACPI_DBG_INIT
,
584 "registering %s as an ACPI driver\n", ibm
->name
);
588 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
589 if (!ibm
->acpi
->driver
) {
591 "failed to allocate memory for ibm->acpi->driver\n");
595 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
596 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
598 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
600 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
602 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
604 kfree(ibm
->acpi
->driver
);
605 ibm
->acpi
->driver
= NULL
;
607 ibm
->flags
.acpi_driver_registered
= 1;
613 /****************************************************************************
614 ****************************************************************************
618 ****************************************************************************
619 ****************************************************************************/
621 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
622 int count
, int *eof
, void *data
)
624 struct ibm_struct
*ibm
= data
;
627 if (!ibm
|| !ibm
->read
)
630 len
= ibm
->read(page
);
634 if (len
<= off
+ count
)
646 static int dispatch_procfs_write(struct file
*file
,
647 const char __user
*userbuf
,
648 unsigned long count
, void *data
)
650 struct ibm_struct
*ibm
= data
;
654 if (!ibm
|| !ibm
->write
)
657 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
661 if (copy_from_user(kernbuf
, userbuf
, count
)) {
667 strcat(kernbuf
, ",");
668 ret
= ibm
->write(kernbuf
);
677 static char *next_cmd(char **cmds
)
682 while ((end
= strchr(start
, ',')) && end
== start
)
694 /****************************************************************************
695 ****************************************************************************
697 * Device model: input, hwmon and platform
699 ****************************************************************************
700 ****************************************************************************/
702 static struct platform_device
*tpacpi_pdev
;
703 static struct platform_device
*tpacpi_sensors_pdev
;
704 static struct device
*tpacpi_hwmon
;
705 static struct input_dev
*tpacpi_inputdev
;
706 static struct mutex tpacpi_inputdev_send_mutex
;
707 static LIST_HEAD(tpacpi_all_drivers
);
709 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
712 struct ibm_struct
*ibm
, *itmp
;
714 list_for_each_entry_safe(ibm
, itmp
,
718 (ibm
->suspend
)(state
);
724 static int tpacpi_resume_handler(struct platform_device
*pdev
)
726 struct ibm_struct
*ibm
, *itmp
;
728 list_for_each_entry_safe(ibm
, itmp
,
738 static struct platform_driver tpacpi_pdriver
= {
740 .name
= TPACPI_DRVR_NAME
,
741 .owner
= THIS_MODULE
,
743 .suspend
= tpacpi_suspend_handler
,
744 .resume
= tpacpi_resume_handler
,
747 static struct platform_driver tpacpi_hwmon_pdriver
= {
749 .name
= TPACPI_HWMON_DRVR_NAME
,
750 .owner
= THIS_MODULE
,
754 /*************************************************************************
755 * sysfs support helpers
758 struct attribute_set
{
759 unsigned int members
, max_members
;
760 struct attribute_group group
;
763 struct attribute_set_obj
{
764 struct attribute_set s
;
766 } __attribute__((packed
));
768 static struct attribute_set
*create_attr_set(unsigned int max_members
,
771 struct attribute_set_obj
*sobj
;
773 if (max_members
== 0)
776 /* Allocates space for implicit NULL at the end too */
777 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
778 max_members
* sizeof(struct attribute
*),
782 sobj
->s
.max_members
= max_members
;
783 sobj
->s
.group
.attrs
= &sobj
->a
;
784 sobj
->s
.group
.name
= name
;
789 #define destroy_attr_set(_set) \
792 /* not multi-threaded safe, use it in a single thread per set */
793 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
798 if (s
->members
>= s
->max_members
)
801 s
->group
.attrs
[s
->members
] = attr
;
807 static int add_many_to_attr_set(struct attribute_set
*s
,
808 struct attribute
**attr
,
813 for (i
= 0; i
< count
; i
++) {
814 res
= add_to_attr_set(s
, attr
[i
]);
822 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
824 sysfs_remove_group(kobj
, &s
->group
);
828 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
829 sysfs_create_group(_kobj, &_attr_set->group)
831 static int parse_strtoul(const char *buf
,
832 unsigned long max
, unsigned long *value
)
836 while (*buf
&& isspace(*buf
))
838 *value
= simple_strtoul(buf
, &endp
, 0);
839 while (*endp
&& isspace(*endp
))
841 if (*endp
|| *value
> max
)
847 static void tpacpi_disable_brightness_delay(void)
849 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
851 "ACPI backlight control delay disabled\n");
854 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
856 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
857 union acpi_object
*obj
;
860 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
861 obj
= (union acpi_object
*)buffer
.pointer
;
862 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
863 printk(TPACPI_ERR
"Unknown _BCL data, "
864 "please report this to %s\n", TPACPI_MAIL
);
867 rc
= obj
->package
.count
;
873 kfree(buffer
.pointer
);
877 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
878 u32 lvl
, void *context
, void **rv
)
880 char name
[ACPI_PATH_SEGMENT_LENGTH
];
881 struct acpi_buffer buffer
= { sizeof(name
), &name
};
883 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
884 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
886 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
887 return AE_CTRL_TERMINATE
;
894 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
896 static int __init
tpacpi_check_std_acpi_brightness_support(void)
900 void *bcl_ptr
= &bcl_levels
;
903 TPACPI_ACPIHANDLE_INIT(vid
);
909 * Search for a _BCL method, and execute it. This is safe on all
910 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
911 * BIOS in ACPI backlight control mode. We do NOT have to care
912 * about calling the _BCL method in an enabled video device, any
913 * will do for our purposes.
916 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
917 tpacpi_acpi_walk_find_bcl
, NULL
,
920 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
921 tp_features
.bright_acpimode
= 1;
922 return (bcl_levels
- 2);
928 static int __init
tpacpi_new_rfkill(const unsigned int id
,
930 const enum rfkill_type rfktype
,
932 int (*toggle_radio
)(void *, enum rfkill_state
),
933 int (*get_state
)(void *, enum rfkill_state
*))
936 enum rfkill_state initial_state
;
938 *rfk
= rfkill_allocate(&tpacpi_pdev
->dev
, rfktype
);
941 "failed to allocate memory for rfkill class\n");
946 (*rfk
)->get_state
= get_state
;
947 (*rfk
)->toggle_radio
= toggle_radio
;
949 if (!get_state(NULL
, &initial_state
))
950 (*rfk
)->state
= initial_state
;
952 res
= rfkill_register(*rfk
);
955 "failed to register %s rfkill switch: %d\n",
965 /*************************************************************************
966 * thinkpad-acpi driver attributes
969 /* interface_version --------------------------------------------------- */
970 static ssize_t
tpacpi_driver_interface_version_show(
971 struct device_driver
*drv
,
974 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
977 static DRIVER_ATTR(interface_version
, S_IRUGO
,
978 tpacpi_driver_interface_version_show
, NULL
);
980 /* debug_level --------------------------------------------------------- */
981 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
984 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
987 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
988 const char *buf
, size_t count
)
992 if (parse_strtoul(buf
, 0xffff, &t
))
1000 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1001 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1003 /* version ------------------------------------------------------------- */
1004 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1007 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1008 TPACPI_DESC
, TPACPI_VERSION
);
1011 static DRIVER_ATTR(version
, S_IRUGO
,
1012 tpacpi_driver_version_show
, NULL
);
1014 /* --------------------------------------------------------------------- */
1016 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1017 &driver_attr_debug_level
, &driver_attr_version
,
1018 &driver_attr_interface_version
,
1021 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1027 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1028 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1035 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1039 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1040 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1043 /****************************************************************************
1044 ****************************************************************************
1048 ****************************************************************************
1049 ****************************************************************************/
1051 /*************************************************************************
1052 * thinkpad-acpi init subdriver
1055 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1057 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1058 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1060 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1061 (thinkpad_id
.bios_version_str
) ?
1062 thinkpad_id
.bios_version_str
: "unknown",
1063 (thinkpad_id
.ec_version_str
) ?
1064 thinkpad_id
.ec_version_str
: "unknown");
1066 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1067 printk(TPACPI_INFO
"%s %s, model %s\n",
1068 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1069 "IBM" : ((thinkpad_id
.vendor
==
1070 PCI_VENDOR_ID_LENOVO
) ?
1071 "Lenovo" : "Unknown vendor"),
1072 thinkpad_id
.model_str
,
1073 (thinkpad_id
.nummodel_str
) ?
1074 thinkpad_id
.nummodel_str
: "unknown");
1079 static int thinkpad_acpi_driver_read(char *p
)
1083 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1084 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1089 static struct ibm_struct thinkpad_acpi_driver_data
= {
1091 .read
= thinkpad_acpi_driver_read
,
1094 /*************************************************************************
1098 enum { /* hot key scan codes (derived from ACPI DSDT) */
1099 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1100 TP_ACPI_HOTKEYSCAN_FNF2
,
1101 TP_ACPI_HOTKEYSCAN_FNF3
,
1102 TP_ACPI_HOTKEYSCAN_FNF4
,
1103 TP_ACPI_HOTKEYSCAN_FNF5
,
1104 TP_ACPI_HOTKEYSCAN_FNF6
,
1105 TP_ACPI_HOTKEYSCAN_FNF7
,
1106 TP_ACPI_HOTKEYSCAN_FNF8
,
1107 TP_ACPI_HOTKEYSCAN_FNF9
,
1108 TP_ACPI_HOTKEYSCAN_FNF10
,
1109 TP_ACPI_HOTKEYSCAN_FNF11
,
1110 TP_ACPI_HOTKEYSCAN_FNF12
,
1111 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1112 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1113 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1114 TP_ACPI_HOTKEYSCAN_FNHOME
,
1115 TP_ACPI_HOTKEYSCAN_FNEND
,
1116 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1117 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1118 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1119 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1120 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1121 TP_ACPI_HOTKEYSCAN_MUTE
,
1122 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1125 enum { /* Keys available through NVRAM polling */
1126 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1127 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1130 enum { /* Positions of some of the keys in hotkey masks */
1131 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1132 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1133 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1134 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1135 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1136 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1137 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1138 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1139 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1140 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1141 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1144 enum { /* NVRAM to ACPI HKEY group map */
1145 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1146 TP_ACPI_HKEY_ZOOM_MASK
|
1147 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1148 TP_ACPI_HKEY_HIBERNATE_MASK
,
1149 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1150 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1151 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1152 TP_ACPI_HKEY_VOLDWN_MASK
|
1153 TP_ACPI_HKEY_MUTE_MASK
,
1156 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1157 struct tp_nvram_state
{
1158 u16 thinkpad_toggle
:1;
1160 u16 display_toggle
:1;
1161 u16 thinklight_toggle
:1;
1162 u16 hibernate_toggle
:1;
1163 u16 displayexp_toggle
:1;
1164 u16 display_state
:1;
1165 u16 brightness_toggle
:1;
1166 u16 volume_toggle
:1;
1169 u8 brightness_level
;
1173 static struct task_struct
*tpacpi_hotkey_task
;
1174 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1175 static int hotkey_poll_freq
= 10; /* Hz */
1176 static struct mutex hotkey_thread_mutex
;
1177 static struct mutex hotkey_thread_data_mutex
;
1178 static unsigned int hotkey_config_change
;
1180 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1182 #define hotkey_source_mask 0U
1184 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1186 static struct mutex hotkey_mutex
;
1188 static enum { /* Reasons for waking up */
1189 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1190 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1191 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1192 } hotkey_wakeup_reason
;
1194 static int hotkey_autosleep_ack
;
1196 static int hotkey_orig_status
;
1197 static u32 hotkey_orig_mask
;
1198 static u32 hotkey_all_mask
;
1199 static u32 hotkey_reserved_mask
;
1200 static u32 hotkey_mask
;
1202 static unsigned int hotkey_report_mode
;
1204 static u16
*hotkey_keycode_map
;
1206 static struct attribute_set
*hotkey_dev_attributes
;
1208 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1209 #define HOTKEY_CONFIG_CRITICAL_START \
1211 mutex_lock(&hotkey_thread_data_mutex); \
1212 hotkey_config_change++; \
1214 #define HOTKEY_CONFIG_CRITICAL_END \
1215 mutex_unlock(&hotkey_thread_data_mutex);
1217 #define HOTKEY_CONFIG_CRITICAL_START
1218 #define HOTKEY_CONFIG_CRITICAL_END
1219 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1221 /* HKEY.MHKG() return bits */
1222 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1224 static int hotkey_get_wlsw(int *status
)
1226 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1231 static int hotkey_get_tablet_mode(int *status
)
1235 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1238 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1243 * Call with hotkey_mutex held
1245 static int hotkey_mask_get(void)
1249 if (tp_features
.hotkey_mask
) {
1250 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1253 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1259 * Call with hotkey_mutex held
1261 static int hotkey_mask_set(u32 mask
)
1266 if (tp_features
.hotkey_mask
) {
1267 if (!tp_warned
.hotkey_mask_ff
&&
1268 (mask
== 0xffff || mask
== 0xffffff ||
1269 mask
== 0xffffffff)) {
1270 tp_warned
.hotkey_mask_ff
= 1;
1271 printk(TPACPI_NOTICE
1272 "setting the hotkey mask to 0x%08x is likely "
1273 "not the best way to go about it\n", mask
);
1274 printk(TPACPI_NOTICE
1275 "please consider using the driver defaults, "
1276 "and refer to up-to-date thinkpad-acpi "
1280 HOTKEY_CONFIG_CRITICAL_START
1281 for (i
= 0; i
< 32; i
++) {
1283 /* enable in firmware mask only keys not in NVRAM
1284 * mode, but enable the key in the cached hotkey_mask
1285 * regardless of mode, or the key will end up
1286 * disabled by hotkey_mask_get() */
1287 if (!acpi_evalf(hkey_handle
,
1288 NULL
, "MHKM", "vdd", i
+ 1,
1289 !!((mask
& ~hotkey_source_mask
) & m
))) {
1293 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1296 HOTKEY_CONFIG_CRITICAL_END
1298 /* hotkey_mask_get must be called unconditionally below */
1299 if (!hotkey_mask_get() && !rc
&&
1300 (hotkey_mask
& ~hotkey_source_mask
) !=
1301 (mask
& ~hotkey_source_mask
)) {
1302 printk(TPACPI_NOTICE
1303 "requested hot key mask 0x%08x, but "
1304 "firmware forced it to 0x%08x\n",
1308 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1309 HOTKEY_CONFIG_CRITICAL_START
1310 hotkey_mask
= mask
& hotkey_source_mask
;
1311 HOTKEY_CONFIG_CRITICAL_END
1313 if (hotkey_mask
!= mask
) {
1314 printk(TPACPI_NOTICE
1315 "requested hot key mask 0x%08x, "
1316 "forced to 0x%08x (NVRAM poll mask is "
1317 "0x%08x): no firmware mask support\n",
1318 mask
, hotkey_mask
, hotkey_source_mask
);
1323 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1329 static int hotkey_status_get(int *status
)
1331 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1337 static int hotkey_status_set(int status
)
1339 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", status
))
1345 static void tpacpi_input_send_tabletsw(void)
1349 if (tp_features
.hotkey_tablet
&&
1350 !hotkey_get_tablet_mode(&state
)) {
1351 mutex_lock(&tpacpi_inputdev_send_mutex
);
1353 input_report_switch(tpacpi_inputdev
,
1354 SW_TABLET_MODE
, !!state
);
1355 input_sync(tpacpi_inputdev
);
1357 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1361 static void tpacpi_input_send_key(unsigned int scancode
)
1363 unsigned int keycode
;
1365 keycode
= hotkey_keycode_map
[scancode
];
1367 if (keycode
!= KEY_RESERVED
) {
1368 mutex_lock(&tpacpi_inputdev_send_mutex
);
1370 input_report_key(tpacpi_inputdev
, keycode
, 1);
1371 if (keycode
== KEY_UNKNOWN
)
1372 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1374 input_sync(tpacpi_inputdev
);
1376 input_report_key(tpacpi_inputdev
, keycode
, 0);
1377 if (keycode
== KEY_UNKNOWN
)
1378 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1380 input_sync(tpacpi_inputdev
);
1382 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1386 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1387 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1389 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1391 tpacpi_input_send_key(scancode
);
1392 if (hotkey_report_mode
< 2) {
1393 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1394 0x80, 0x1001 + scancode
);
1398 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1402 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1403 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1404 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1405 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1406 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1407 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1409 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1410 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1411 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1413 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1414 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1415 n
->displayexp_toggle
=
1416 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1418 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1419 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1420 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1421 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1422 n
->brightness_toggle
=
1423 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1425 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1426 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1427 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1428 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1429 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1430 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1434 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1436 if ((mask & (1 << __scancode)) && \
1437 oldn->__member != newn->__member) \
1438 tpacpi_hotkey_send_key(__scancode); \
1441 #define TPACPI_MAY_SEND_KEY(__scancode) \
1442 do { if (mask & (1 << __scancode)) \
1443 tpacpi_hotkey_send_key(__scancode); } while (0)
1445 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1446 struct tp_nvram_state
*newn
,
1449 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1450 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1451 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1452 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1454 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1456 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1459 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1460 if (oldn
->mute
!= newn
->mute
) {
1461 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1463 if (oldn
->volume_level
> newn
->volume_level
) {
1464 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1465 } else if (oldn
->volume_level
< newn
->volume_level
) {
1466 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1467 } else if (oldn
->mute
== newn
->mute
) {
1468 /* repeated key presses that didn't change state */
1470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1471 } else if (newn
->volume_level
!= 0) {
1472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1474 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1479 /* handle brightness */
1480 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1481 if (oldn
->brightness_level
< newn
->brightness_level
) {
1482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1483 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1486 /* repeated key presses that didn't change state */
1487 if (newn
->brightness_level
!= 0) {
1488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1490 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1496 #undef TPACPI_COMPARE_KEY
1497 #undef TPACPI_MAY_SEND_KEY
1499 static int hotkey_kthread(void *data
)
1501 struct tp_nvram_state s
[2];
1503 unsigned int si
, so
;
1505 unsigned int change_detector
, must_reset
;
1507 mutex_lock(&hotkey_thread_mutex
);
1509 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1518 /* Initial state for compares */
1519 mutex_lock(&hotkey_thread_data_mutex
);
1520 change_detector
= hotkey_config_change
;
1521 mask
= hotkey_source_mask
& hotkey_mask
;
1522 mutex_unlock(&hotkey_thread_data_mutex
);
1523 hotkey_read_nvram(&s
[so
], mask
);
1525 while (!kthread_should_stop() && hotkey_poll_freq
) {
1527 t
= 1000/hotkey_poll_freq
;
1528 t
= msleep_interruptible(t
);
1529 if (unlikely(kthread_should_stop()))
1531 must_reset
= try_to_freeze();
1532 if (t
> 0 && !must_reset
)
1535 mutex_lock(&hotkey_thread_data_mutex
);
1536 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1537 /* forget old state on thaw or config change */
1540 change_detector
= hotkey_config_change
;
1542 mask
= hotkey_source_mask
& hotkey_mask
;
1543 mutex_unlock(&hotkey_thread_data_mutex
);
1546 hotkey_read_nvram(&s
[si
], mask
);
1547 if (likely(si
!= so
)) {
1548 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1558 mutex_unlock(&hotkey_thread_mutex
);
1562 static void hotkey_poll_stop_sync(void)
1564 if (tpacpi_hotkey_task
) {
1565 if (frozen(tpacpi_hotkey_task
) ||
1566 freezing(tpacpi_hotkey_task
))
1567 thaw_process(tpacpi_hotkey_task
);
1569 kthread_stop(tpacpi_hotkey_task
);
1570 tpacpi_hotkey_task
= NULL
;
1571 mutex_lock(&hotkey_thread_mutex
);
1572 /* at this point, the thread did exit */
1573 mutex_unlock(&hotkey_thread_mutex
);
1577 /* call with hotkey_mutex held */
1578 static void hotkey_poll_setup(int may_warn
)
1580 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1581 hotkey_poll_freq
> 0 &&
1582 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1583 if (!tpacpi_hotkey_task
) {
1584 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1585 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
1586 if (IS_ERR(tpacpi_hotkey_task
)) {
1587 tpacpi_hotkey_task
= NULL
;
1589 "could not create kernel thread "
1590 "for hotkey polling\n");
1594 hotkey_poll_stop_sync();
1596 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1597 printk(TPACPI_NOTICE
1598 "hot keys 0x%08x require polling, "
1599 "which is currently disabled\n",
1600 hotkey_source_mask
);
1605 static void hotkey_poll_setup_safe(int may_warn
)
1607 mutex_lock(&hotkey_mutex
);
1608 hotkey_poll_setup(may_warn
);
1609 mutex_unlock(&hotkey_mutex
);
1612 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1614 static void hotkey_poll_setup_safe(int __unused
)
1618 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1620 static int hotkey_inputdev_open(struct input_dev
*dev
)
1622 switch (tpacpi_lifecycle
) {
1623 case TPACPI_LIFE_INIT
:
1625 * hotkey_init will call hotkey_poll_setup_safe
1626 * at the appropriate moment
1629 case TPACPI_LIFE_EXITING
:
1631 case TPACPI_LIFE_RUNNING
:
1632 hotkey_poll_setup_safe(0);
1636 /* Should only happen if tpacpi_lifecycle is corrupt */
1641 static void hotkey_inputdev_close(struct input_dev
*dev
)
1643 /* disable hotkey polling when possible */
1644 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1645 hotkey_poll_setup_safe(0);
1648 /* sysfs hotkey enable ------------------------------------------------- */
1649 static ssize_t
hotkey_enable_show(struct device
*dev
,
1650 struct device_attribute
*attr
,
1655 res
= hotkey_status_get(&status
);
1659 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1662 static ssize_t
hotkey_enable_store(struct device
*dev
,
1663 struct device_attribute
*attr
,
1664 const char *buf
, size_t count
)
1669 if (parse_strtoul(buf
, 1, &t
))
1672 res
= hotkey_status_set(t
);
1674 return (res
) ? res
: count
;
1677 static struct device_attribute dev_attr_hotkey_enable
=
1678 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1679 hotkey_enable_show
, hotkey_enable_store
);
1681 /* sysfs hotkey mask --------------------------------------------------- */
1682 static ssize_t
hotkey_mask_show(struct device
*dev
,
1683 struct device_attribute
*attr
,
1688 if (mutex_lock_interruptible(&hotkey_mutex
))
1689 return -ERESTARTSYS
;
1690 res
= hotkey_mask_get();
1691 mutex_unlock(&hotkey_mutex
);
1694 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1697 static ssize_t
hotkey_mask_store(struct device
*dev
,
1698 struct device_attribute
*attr
,
1699 const char *buf
, size_t count
)
1704 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1707 if (mutex_lock_interruptible(&hotkey_mutex
))
1708 return -ERESTARTSYS
;
1710 res
= hotkey_mask_set(t
);
1712 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1713 hotkey_poll_setup(1);
1716 mutex_unlock(&hotkey_mutex
);
1718 return (res
) ? res
: count
;
1721 static struct device_attribute dev_attr_hotkey_mask
=
1722 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1723 hotkey_mask_show
, hotkey_mask_store
);
1725 /* sysfs hotkey bios_enabled ------------------------------------------- */
1726 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1727 struct device_attribute
*attr
,
1730 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_orig_status
);
1733 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1734 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1736 /* sysfs hotkey bios_mask ---------------------------------------------- */
1737 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1738 struct device_attribute
*attr
,
1741 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1744 static struct device_attribute dev_attr_hotkey_bios_mask
=
1745 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1747 /* sysfs hotkey all_mask ----------------------------------------------- */
1748 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
1749 struct device_attribute
*attr
,
1752 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1753 hotkey_all_mask
| hotkey_source_mask
);
1756 static struct device_attribute dev_attr_hotkey_all_mask
=
1757 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
1759 /* sysfs hotkey recommended_mask --------------------------------------- */
1760 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
1761 struct device_attribute
*attr
,
1764 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1765 (hotkey_all_mask
| hotkey_source_mask
)
1766 & ~hotkey_reserved_mask
);
1769 static struct device_attribute dev_attr_hotkey_recommended_mask
=
1770 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
1771 hotkey_recommended_mask_show
, NULL
);
1773 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1775 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1776 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
1777 struct device_attribute
*attr
,
1780 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
1783 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
1784 struct device_attribute
*attr
,
1785 const char *buf
, size_t count
)
1789 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
1790 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
1793 if (mutex_lock_interruptible(&hotkey_mutex
))
1794 return -ERESTARTSYS
;
1796 HOTKEY_CONFIG_CRITICAL_START
1797 hotkey_source_mask
= t
;
1798 HOTKEY_CONFIG_CRITICAL_END
1800 hotkey_poll_setup(1);
1802 mutex_unlock(&hotkey_mutex
);
1807 static struct device_attribute dev_attr_hotkey_source_mask
=
1808 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
1809 hotkey_source_mask_show
, hotkey_source_mask_store
);
1811 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1812 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
1813 struct device_attribute
*attr
,
1816 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
1819 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
1820 struct device_attribute
*attr
,
1821 const char *buf
, size_t count
)
1825 if (parse_strtoul(buf
, 25, &t
))
1828 if (mutex_lock_interruptible(&hotkey_mutex
))
1829 return -ERESTARTSYS
;
1831 hotkey_poll_freq
= t
;
1833 hotkey_poll_setup(1);
1834 mutex_unlock(&hotkey_mutex
);
1839 static struct device_attribute dev_attr_hotkey_poll_freq
=
1840 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
1841 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
1843 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1845 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1846 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
1847 struct device_attribute
*attr
,
1851 res
= hotkey_get_wlsw(&s
);
1855 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1858 static struct device_attribute dev_attr_hotkey_radio_sw
=
1859 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
1861 static void hotkey_radio_sw_notify_change(void)
1863 if (tp_features
.hotkey_wlsw
)
1864 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1868 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1869 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
1870 struct device_attribute
*attr
,
1874 res
= hotkey_get_tablet_mode(&s
);
1878 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
1881 static struct device_attribute dev_attr_hotkey_tablet_mode
=
1882 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
1884 static void hotkey_tablet_mode_notify_change(void)
1886 if (tp_features
.hotkey_tablet
)
1887 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1888 "hotkey_tablet_mode");
1891 /* sysfs hotkey report_mode -------------------------------------------- */
1892 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
1893 struct device_attribute
*attr
,
1896 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1897 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
1900 static struct device_attribute dev_attr_hotkey_report_mode
=
1901 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
1903 /* sysfs wakeup reason (pollable) -------------------------------------- */
1904 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
1905 struct device_attribute
*attr
,
1908 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
1911 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
1912 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
1914 static void hotkey_wakeup_reason_notify_change(void)
1916 if (tp_features
.hotkey_mask
)
1917 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1921 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1922 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
1923 struct device_attribute
*attr
,
1926 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
1929 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
1930 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
1931 hotkey_wakeup_hotunplug_complete_show
, NULL
);
1933 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1935 if (tp_features
.hotkey_mask
)
1936 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
1937 "wakeup_hotunplug_complete");
1940 /* --------------------------------------------------------------------- */
1942 static struct attribute
*hotkey_attributes
[] __initdata
= {
1943 &dev_attr_hotkey_enable
.attr
,
1944 &dev_attr_hotkey_bios_enabled
.attr
,
1945 &dev_attr_hotkey_report_mode
.attr
,
1946 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1947 &dev_attr_hotkey_mask
.attr
,
1948 &dev_attr_hotkey_all_mask
.attr
,
1949 &dev_attr_hotkey_recommended_mask
.attr
,
1950 &dev_attr_hotkey_source_mask
.attr
,
1951 &dev_attr_hotkey_poll_freq
.attr
,
1955 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
1956 &dev_attr_hotkey_bios_mask
.attr
,
1957 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1958 &dev_attr_hotkey_mask
.attr
,
1959 &dev_attr_hotkey_all_mask
.attr
,
1960 &dev_attr_hotkey_recommended_mask
.attr
,
1962 &dev_attr_hotkey_wakeup_reason
.attr
,
1963 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
1966 static void bluetooth_update_rfk(void);
1967 static void wan_update_rfk(void);
1968 static void tpacpi_send_radiosw_update(void)
1972 /* Sync these BEFORE sending any rfkill events */
1973 if (tp_features
.bluetooth
)
1974 bluetooth_update_rfk();
1975 if (tp_features
.wan
)
1978 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
1979 mutex_lock(&tpacpi_inputdev_send_mutex
);
1981 input_report_switch(tpacpi_inputdev
,
1982 SW_RFKILL_ALL
, !!wlsw
);
1983 input_sync(tpacpi_inputdev
);
1985 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1987 hotkey_radio_sw_notify_change();
1990 static void hotkey_exit(void)
1992 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1993 hotkey_poll_stop_sync();
1996 if (hotkey_dev_attributes
)
1997 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
1999 kfree(hotkey_keycode_map
);
2001 if (tp_features
.hotkey
) {
2002 dbg_printk(TPACPI_DBG_EXIT
,
2003 "restoring original hot key mask\n");
2004 /* no short-circuit boolean operator below! */
2005 if ((hotkey_mask_set(hotkey_orig_mask
) |
2006 hotkey_status_set(hotkey_orig_status
)) != 0)
2008 "failed to restore hot key mask "
2009 "to BIOS defaults\n");
2013 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2015 /* Requirements for changing the default keymaps:
2017 * 1. Many of the keys are mapped to KEY_RESERVED for very
2018 * good reasons. Do not change them unless you have deep
2019 * knowledge on the IBM and Lenovo ThinkPad firmware for
2020 * the various ThinkPad models. The driver behaves
2021 * differently for KEY_RESERVED: such keys have their
2022 * hot key mask *unset* in mask_recommended, and also
2023 * in the initial hot key mask programmed into the
2024 * firmware at driver load time, which means the firm-
2025 * ware may react very differently if you change them to
2028 * 2. You must be subscribed to the linux-thinkpad and
2029 * ibm-acpi-devel mailing lists, and you should read the
2030 * list archives since 2007 if you want to change the
2031 * keymaps. This requirement exists so that you will
2032 * know the past history of problems with the thinkpad-
2033 * acpi driver keymaps, and also that you will be
2034 * listening to any bug reports;
2036 * 3. Do not send thinkpad-acpi specific patches directly to
2037 * for merging, *ever*. Send them to the linux-acpi
2038 * mailinglist for comments. Merging is to be done only
2039 * through acpi-test and the ACPI maintainer.
2041 * If the above is too much to ask, don't change the keymap.
2042 * Ask the thinkpad-acpi maintainer to do it, instead.
2044 static u16 ibm_keycode_map
[] __initdata
= {
2045 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2046 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2047 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2048 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2050 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2051 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2052 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2053 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2055 /* brightness: firmware always reacts to them, unless
2056 * X.org did some tricks in the radeon BIOS scratch
2057 * registers of *some* models */
2058 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2059 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2061 /* Thinklight: firmware always react to it */
2062 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2064 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2065 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2067 /* Volume: firmware always react to it and reprograms
2068 * the built-in *extra* mixer. Never map it to control
2069 * another mixer by default. */
2070 KEY_RESERVED
, /* 0x14: VOLUME UP */
2071 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2072 KEY_RESERVED
, /* 0x16: MUTE */
2074 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2076 /* (assignments unknown, please report if found) */
2077 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2078 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2080 static u16 lenovo_keycode_map
[] __initdata
= {
2081 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2082 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2083 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2084 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2086 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2087 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2088 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2089 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2091 /* These either have to go through ACPI video, or
2092 * act like in the IBM ThinkPads, so don't ever
2093 * enable them by default */
2094 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2095 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2097 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2099 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2100 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2102 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2103 * react to it and reprograms the built-in *extra* mixer.
2104 * Never map it to control another mixer by default.
2106 * T60?, T61, R60?, R61: firmware and EC tries to send
2107 * these over the regular keyboard, so these are no-ops,
2108 * but there are still weird bugs re. MUTE, so do not
2109 * change unless you get test reports from all Lenovo
2110 * models. May cause the BIOS to interfere with the
2113 KEY_RESERVED
, /* 0x14: VOLUME UP */
2114 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2115 KEY_RESERVED
, /* 0x16: MUTE */
2117 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2119 /* (assignments unknown, please report if found) */
2120 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2121 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2124 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2125 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2126 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2132 vdbg_printk(TPACPI_DBG_INIT
, "initializing hotkey subdriver\n");
2134 BUG_ON(!tpacpi_inputdev
);
2135 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2136 tpacpi_inputdev
->close
!= NULL
);
2138 TPACPI_ACPIHANDLE_INIT(hkey
);
2139 mutex_init(&hotkey_mutex
);
2141 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2142 mutex_init(&hotkey_thread_mutex
);
2143 mutex_init(&hotkey_thread_data_mutex
);
2146 /* hotkey not supported on 570 */
2147 tp_features
.hotkey
= hkey_handle
!= NULL
;
2149 vdbg_printk(TPACPI_DBG_INIT
, "hotkeys are %s\n",
2150 str_supported(tp_features
.hotkey
));
2152 if (!tp_features
.hotkey
)
2155 tpacpi_disable_brightness_delay();
2157 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2158 if (!hotkey_dev_attributes
)
2160 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2162 ARRAY_SIZE(hotkey_attributes
));
2166 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2167 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2168 for HKEY interface version 0x100 */
2169 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2170 if ((hkeyv
>> 8) != 1) {
2171 printk(TPACPI_ERR
"unknown version of the "
2172 "HKEY interface: 0x%x\n", hkeyv
);
2173 printk(TPACPI_ERR
"please report this to %s\n",
2177 * MHKV 0x100 in A31, R40, R40e,
2178 * T4x, X31, and later
2180 tp_features
.hotkey_mask
= 1;
2184 vdbg_printk(TPACPI_DBG_INIT
, "hotkey masks are %s\n",
2185 str_supported(tp_features
.hotkey_mask
));
2187 if (tp_features
.hotkey_mask
) {
2188 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2191 "missing MHKA handler, "
2192 "please report this to %s\n",
2194 /* FN+F12, FN+F4, FN+F3 */
2195 hotkey_all_mask
= 0x080cU
;
2199 /* hotkey_source_mask *must* be zero for
2200 * the first hotkey_mask_get */
2201 res
= hotkey_status_get(&hotkey_orig_status
);
2205 if (tp_features
.hotkey_mask
) {
2206 res
= hotkey_mask_get();
2210 hotkey_orig_mask
= hotkey_mask
;
2211 res
= add_many_to_attr_set(
2212 hotkey_dev_attributes
,
2213 hotkey_mask_attributes
,
2214 ARRAY_SIZE(hotkey_mask_attributes
));
2219 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2220 if (tp_features
.hotkey_mask
) {
2221 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2224 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2227 vdbg_printk(TPACPI_DBG_INIT
,
2228 "hotkey source mask 0x%08x, polling freq %d\n",
2229 hotkey_source_mask
, hotkey_poll_freq
);
2232 /* Not all thinkpads have a hardware radio switch */
2233 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2234 tp_features
.hotkey_wlsw
= 1;
2236 "radio switch found; radios are %s\n",
2237 enabled(status
, 0));
2239 if (tp_features
.hotkey_wlsw
)
2240 res
= add_to_attr_set(hotkey_dev_attributes
,
2241 &dev_attr_hotkey_radio_sw
.attr
);
2243 /* For X41t, X60t, X61t Tablets... */
2244 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2245 tp_features
.hotkey_tablet
= 1;
2247 "possible tablet mode switch found; "
2248 "ThinkPad in %s mode\n",
2249 (status
& TP_HOTKEY_TABLET_MASK
)?
2250 "tablet" : "laptop");
2251 res
= add_to_attr_set(hotkey_dev_attributes
,
2252 &dev_attr_hotkey_tablet_mode
.attr
);
2256 res
= register_attr_set_with_sysfs(
2257 hotkey_dev_attributes
,
2258 &tpacpi_pdev
->dev
.kobj
);
2262 /* Set up key map */
2264 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2266 if (!hotkey_keycode_map
) {
2268 "failed to allocate memory for key map\n");
2273 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2274 dbg_printk(TPACPI_DBG_INIT
,
2275 "using Lenovo default hot key map\n");
2276 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2277 TPACPI_HOTKEY_MAP_SIZE
);
2279 dbg_printk(TPACPI_DBG_INIT
,
2280 "using IBM default hot key map\n");
2281 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2282 TPACPI_HOTKEY_MAP_SIZE
);
2285 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2286 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2287 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2288 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2289 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2290 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2291 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2292 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2293 set_bit(hotkey_keycode_map
[i
],
2294 tpacpi_inputdev
->keybit
);
2296 if (i
< sizeof(hotkey_reserved_mask
)*8)
2297 hotkey_reserved_mask
|= 1 << i
;
2301 if (tp_features
.hotkey_wlsw
) {
2302 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2303 set_bit(SW_RFKILL_ALL
, tpacpi_inputdev
->swbit
);
2305 if (tp_features
.hotkey_tablet
) {
2306 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2307 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2310 /* Do not issue duplicate brightness change events to
2312 if (!tp_features
.bright_acpimode
)
2313 /* update bright_acpimode... */
2314 tpacpi_check_std_acpi_brightness_support();
2316 if (tp_features
.bright_acpimode
) {
2318 "This ThinkPad has standard ACPI backlight "
2319 "brightness control, supported by the ACPI "
2321 printk(TPACPI_NOTICE
2322 "Disabling thinkpad-acpi brightness events "
2325 /* The hotkey_reserved_mask change below is not
2326 * necessary while the keys are at KEY_RESERVED in the
2327 * default map, but better safe than sorry, leave it
2328 * here as a marker of what we have to do, especially
2329 * when we finally become able to set this at runtime
2330 * on response to X.org requests */
2331 hotkey_reserved_mask
|=
2332 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
2333 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
2336 dbg_printk(TPACPI_DBG_INIT
, "enabling hot key handling\n");
2337 res
= hotkey_status_set(1);
2342 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2343 & ~hotkey_reserved_mask
)
2344 | hotkey_orig_mask
);
2345 if (res
< 0 && res
!= -ENXIO
) {
2350 dbg_printk(TPACPI_DBG_INIT
,
2351 "legacy hot key reporting over procfs %s\n",
2352 (hotkey_report_mode
< 2) ?
2353 "enabled" : "disabled");
2355 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2356 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2358 hotkey_poll_setup_safe(1);
2359 tpacpi_send_radiosw_update();
2360 tpacpi_input_send_tabletsw();
2365 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2366 hotkey_dev_attributes
= NULL
;
2368 return (res
< 0)? res
: 1;
2371 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2374 unsigned int scancode
;
2379 if (event
!= 0x80) {
2381 "unknown HKEY notification event %d\n", event
);
2382 /* forward it to userspace, maybe it knows how to handle it */
2383 acpi_bus_generate_netlink_event(
2384 ibm
->acpi
->device
->pnp
.device_class
,
2385 ibm
->acpi
->device
->dev
.bus_id
,
2391 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2392 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2405 switch (hkey
>> 12) {
2407 /* 0x1000-0x1FFF: key presses */
2408 scancode
= hkey
& 0xfff;
2409 if (scancode
> 0 && scancode
< 0x21) {
2411 if (!(hotkey_source_mask
& (1 << scancode
))) {
2412 tpacpi_input_send_key(scancode
);
2424 case 0x2304: /* suspend, undock */
2425 case 0x2404: /* hibernation, undock */
2426 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2429 case 0x2305: /* suspend, bay eject */
2430 case 0x2405: /* hibernation, bay eject */
2431 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2437 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2439 "woke up due to a hot-unplug "
2441 hotkey_wakeup_reason_notify_change();
2445 /* bay-related wakeups */
2446 if (hkey
== 0x3003) {
2447 hotkey_autosleep_ack
= 1;
2450 hotkey_wakeup_hotunplug_complete_notify_change();
2456 /* dock-related wakeups */
2457 if (hkey
== 0x4003) {
2458 hotkey_autosleep_ack
= 1;
2461 hotkey_wakeup_hotunplug_complete_notify_change();
2467 /* 0x5000-0x5FFF: human interface helpers */
2469 case 0x5010: /* Lenovo new BIOS: brightness changed */
2470 case 0x500b: /* X61t: tablet pen inserted into bay */
2471 case 0x500c: /* X61t: tablet pen removed from bay */
2473 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2474 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2475 tpacpi_input_send_tabletsw();
2476 hotkey_tablet_mode_notify_change();
2481 /* LID switch events. Do not propagate */
2489 /* 0x7000-0x7FFF: misc */
2490 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2491 tpacpi_send_radiosw_update();
2495 /* fallthrough to default */
2500 printk(TPACPI_NOTICE
2501 "unhandled HKEY event 0x%04x\n", hkey
);
2505 if (!ignore_acpi_ev
&&
2506 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2507 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2511 /* netlink events */
2512 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2513 acpi_bus_generate_netlink_event(
2514 ibm
->acpi
->device
->pnp
.device_class
,
2515 ibm
->acpi
->device
->dev
.bus_id
,
2521 static void hotkey_suspend(pm_message_t state
)
2523 /* Do these on suspend, we get the events on early resume! */
2524 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2525 hotkey_autosleep_ack
= 0;
2528 static void hotkey_resume(void)
2530 tpacpi_disable_brightness_delay();
2532 if (hotkey_mask_get())
2534 "error while trying to read hot key mask "
2536 tpacpi_send_radiosw_update();
2537 hotkey_tablet_mode_notify_change();
2538 hotkey_wakeup_reason_notify_change();
2539 hotkey_wakeup_hotunplug_complete_notify_change();
2540 hotkey_poll_setup_safe(0);
2543 /* procfs -------------------------------------------------------------- */
2544 static int hotkey_read(char *p
)
2549 if (!tp_features
.hotkey
) {
2550 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2554 if (mutex_lock_interruptible(&hotkey_mutex
))
2555 return -ERESTARTSYS
;
2556 res
= hotkey_status_get(&status
);
2558 res
= hotkey_mask_get();
2559 mutex_unlock(&hotkey_mutex
);
2563 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2564 if (tp_features
.hotkey_mask
) {
2565 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2566 len
+= sprintf(p
+ len
,
2567 "commands:\tenable, disable, reset, <mask>\n");
2569 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2570 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2576 static int hotkey_write(char *buf
)
2582 if (!tp_features
.hotkey
)
2585 if (mutex_lock_interruptible(&hotkey_mutex
))
2586 return -ERESTARTSYS
;
2592 while ((cmd
= next_cmd(&buf
))) {
2593 if (strlencmp(cmd
, "enable") == 0) {
2595 } else if (strlencmp(cmd
, "disable") == 0) {
2597 } else if (strlencmp(cmd
, "reset") == 0) {
2598 status
= hotkey_orig_status
;
2599 mask
= hotkey_orig_mask
;
2600 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2602 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2610 res
= hotkey_status_set(status
);
2612 if (!res
&& mask
!= hotkey_mask
)
2613 res
= hotkey_mask_set(mask
);
2616 mutex_unlock(&hotkey_mutex
);
2620 static const struct acpi_device_id ibm_htk_device_ids
[] = {
2621 {TPACPI_ACPI_HKEY_HID
, 0},
2625 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
2626 .hid
= ibm_htk_device_ids
,
2627 .notify
= hotkey_notify
,
2628 .handle
= &hkey_handle
,
2629 .type
= ACPI_DEVICE_NOTIFY
,
2632 static struct ibm_struct hotkey_driver_data
= {
2634 .read
= hotkey_read
,
2635 .write
= hotkey_write
,
2636 .exit
= hotkey_exit
,
2637 .resume
= hotkey_resume
,
2638 .suspend
= hotkey_suspend
,
2639 .acpi
= &ibm_hotkey_acpidriver
,
2642 /*************************************************************************
2643 * Bluetooth subdriver
2647 /* ACPI GBDC/SBDC bits */
2648 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
2649 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
2650 TP_ACPI_BLUETOOTH_UNK
= 0x04, /* unknown function */
2653 static struct rfkill
*tpacpi_bluetooth_rfkill
;
2655 static int bluetooth_get_radiosw(void)
2659 if (!tp_features
.bluetooth
)
2662 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2663 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
2664 return RFKILL_STATE_HARD_BLOCKED
;
2666 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2669 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
2670 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
2673 static void bluetooth_update_rfk(void)
2677 if (!tpacpi_bluetooth_rfkill
)
2680 status
= bluetooth_get_radiosw();
2683 rfkill_force_state(tpacpi_bluetooth_rfkill
, status
);
2686 static int bluetooth_set_radiosw(int radio_on
, int update_rfk
)
2690 if (!tp_features
.bluetooth
)
2693 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2694 * reason to risk weird behaviour. */
2695 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
2699 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
2702 status
|= TP_ACPI_BLUETOOTH_RADIOSSW
;
2704 status
&= ~TP_ACPI_BLUETOOTH_RADIOSSW
;
2705 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
2709 bluetooth_update_rfk();
2714 /* sysfs bluetooth enable ---------------------------------------------- */
2715 static ssize_t
bluetooth_enable_show(struct device
*dev
,
2716 struct device_attribute
*attr
,
2721 status
= bluetooth_get_radiosw();
2725 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2726 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
2729 static ssize_t
bluetooth_enable_store(struct device
*dev
,
2730 struct device_attribute
*attr
,
2731 const char *buf
, size_t count
)
2736 if (parse_strtoul(buf
, 1, &t
))
2739 res
= bluetooth_set_radiosw(t
, 1);
2741 return (res
) ? res
: count
;
2744 static struct device_attribute dev_attr_bluetooth_enable
=
2745 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
2746 bluetooth_enable_show
, bluetooth_enable_store
);
2748 /* --------------------------------------------------------------------- */
2750 static struct attribute
*bluetooth_attributes
[] = {
2751 &dev_attr_bluetooth_enable
.attr
,
2755 static const struct attribute_group bluetooth_attr_group
= {
2756 .attrs
= bluetooth_attributes
,
2759 static int tpacpi_bluetooth_rfk_get(void *data
, enum rfkill_state
*state
)
2761 int bts
= bluetooth_get_radiosw();
2770 static int tpacpi_bluetooth_rfk_set(void *data
, enum rfkill_state state
)
2772 return bluetooth_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
2775 static void bluetooth_exit(void)
2777 if (tpacpi_bluetooth_rfkill
)
2778 rfkill_unregister(tpacpi_bluetooth_rfkill
);
2780 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
2781 &bluetooth_attr_group
);
2784 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
2789 vdbg_printk(TPACPI_DBG_INIT
, "initializing bluetooth subdriver\n");
2791 TPACPI_ACPIHANDLE_INIT(hkey
);
2793 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2794 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2795 tp_features
.bluetooth
= hkey_handle
&&
2796 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
2798 vdbg_printk(TPACPI_DBG_INIT
, "bluetooth is %s, status 0x%02x\n",
2799 str_supported(tp_features
.bluetooth
),
2802 if (tp_features
.bluetooth
&&
2803 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
2804 /* no bluetooth hardware present in system */
2805 tp_features
.bluetooth
= 0;
2806 dbg_printk(TPACPI_DBG_INIT
,
2807 "bluetooth hardware not installed\n");
2810 if (!tp_features
.bluetooth
)
2813 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
2814 &bluetooth_attr_group
);
2818 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
2819 &tpacpi_bluetooth_rfkill
,
2820 RFKILL_TYPE_BLUETOOTH
,
2821 "tpacpi_bluetooth_sw",
2822 tpacpi_bluetooth_rfk_set
,
2823 tpacpi_bluetooth_rfk_get
);
2832 /* procfs -------------------------------------------------------------- */
2833 static int bluetooth_read(char *p
)
2836 int status
= bluetooth_get_radiosw();
2838 if (!tp_features
.bluetooth
)
2839 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2841 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
2842 (status
== RFKILL_STATE_UNBLOCKED
) ?
2843 "enabled" : "disabled");
2844 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
2850 static int bluetooth_write(char *buf
)
2854 if (!tp_features
.bluetooth
)
2857 while ((cmd
= next_cmd(&buf
))) {
2858 if (strlencmp(cmd
, "enable") == 0) {
2859 bluetooth_set_radiosw(1, 1);
2860 } else if (strlencmp(cmd
, "disable") == 0) {
2861 bluetooth_set_radiosw(0, 1);
2869 static struct ibm_struct bluetooth_driver_data
= {
2870 .name
= "bluetooth",
2871 .read
= bluetooth_read
,
2872 .write
= bluetooth_write
,
2873 .exit
= bluetooth_exit
,
2876 /*************************************************************************
2881 /* ACPI GWAN/SWAN bits */
2882 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
2883 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
2884 TP_ACPI_WANCARD_UNK
= 0x04, /* unknown function */
2887 static struct rfkill
*tpacpi_wan_rfkill
;
2889 static int wan_get_radiosw(void)
2893 if (!tp_features
.wan
)
2896 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2897 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
2898 return RFKILL_STATE_HARD_BLOCKED
;
2900 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2903 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
2904 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
2907 static void wan_update_rfk(void)
2911 if (!tpacpi_wan_rfkill
)
2914 status
= wan_get_radiosw();
2917 rfkill_force_state(tpacpi_wan_rfkill
, status
);
2920 static int wan_set_radiosw(int radio_on
, int update_rfk
)
2924 if (!tp_features
.wan
)
2927 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2928 * reason to risk weird behaviour. */
2929 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
2933 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
2936 status
|= TP_ACPI_WANCARD_RADIOSSW
;
2938 status
&= ~TP_ACPI_WANCARD_RADIOSSW
;
2939 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
2948 /* sysfs wan enable ---------------------------------------------------- */
2949 static ssize_t
wan_enable_show(struct device
*dev
,
2950 struct device_attribute
*attr
,
2955 status
= wan_get_radiosw();
2959 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2960 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
2963 static ssize_t
wan_enable_store(struct device
*dev
,
2964 struct device_attribute
*attr
,
2965 const char *buf
, size_t count
)
2970 if (parse_strtoul(buf
, 1, &t
))
2973 res
= wan_set_radiosw(t
, 1);
2975 return (res
) ? res
: count
;
2978 static struct device_attribute dev_attr_wan_enable
=
2979 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
2980 wan_enable_show
, wan_enable_store
);
2982 /* --------------------------------------------------------------------- */
2984 static struct attribute
*wan_attributes
[] = {
2985 &dev_attr_wan_enable
.attr
,
2989 static const struct attribute_group wan_attr_group
= {
2990 .attrs
= wan_attributes
,
2993 static int tpacpi_wan_rfk_get(void *data
, enum rfkill_state
*state
)
2995 int wans
= wan_get_radiosw();
3004 static int tpacpi_wan_rfk_set(void *data
, enum rfkill_state state
)
3006 return wan_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3009 static void wan_exit(void)
3011 if (tpacpi_wan_rfkill
)
3012 rfkill_unregister(tpacpi_wan_rfkill
);
3014 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3018 static int __init
wan_init(struct ibm_init_struct
*iibm
)
3023 vdbg_printk(TPACPI_DBG_INIT
, "initializing wan subdriver\n");
3025 TPACPI_ACPIHANDLE_INIT(hkey
);
3027 tp_features
.wan
= hkey_handle
&&
3028 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
3030 vdbg_printk(TPACPI_DBG_INIT
, "wan is %s, status 0x%02x\n",
3031 str_supported(tp_features
.wan
),
3034 if (tp_features
.wan
&&
3035 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
3036 /* no wan hardware present in system */
3037 tp_features
.wan
= 0;
3038 dbg_printk(TPACPI_DBG_INIT
,
3039 "wan hardware not installed\n");
3042 if (!tp_features
.wan
)
3045 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3050 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
3055 tpacpi_wan_rfk_get
);
3064 /* procfs -------------------------------------------------------------- */
3065 static int wan_read(char *p
)
3068 int status
= wan_get_radiosw();
3070 if (!tp_features
.wan
)
3071 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3073 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3074 (status
== RFKILL_STATE_UNBLOCKED
) ?
3075 "enabled" : "disabled");
3076 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3082 static int wan_write(char *buf
)
3086 if (!tp_features
.wan
)
3089 while ((cmd
= next_cmd(&buf
))) {
3090 if (strlencmp(cmd
, "enable") == 0) {
3091 wan_set_radiosw(1, 1);
3092 } else if (strlencmp(cmd
, "disable") == 0) {
3093 wan_set_radiosw(0, 1);
3101 static struct ibm_struct wan_driver_data
= {
3108 /*************************************************************************
3112 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3114 enum video_access_mode
{
3115 TPACPI_VIDEO_NONE
= 0,
3116 TPACPI_VIDEO_570
, /* 570 */
3117 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
3118 TPACPI_VIDEO_NEW
, /* all others */
3121 enum { /* video status flags, based on VIDEO_570 */
3122 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
3123 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
3124 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
3127 enum { /* TPACPI_VIDEO_570 constants */
3128 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
3129 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
3130 * video_status_flags */
3131 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
3132 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
3135 static enum video_access_mode video_supported
;
3136 static int video_orig_autosw
;
3138 static int video_autosw_get(void);
3139 static int video_autosw_set(int enable
);
3141 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3143 static int __init
video_init(struct ibm_init_struct
*iibm
)
3147 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
3149 TPACPI_ACPIHANDLE_INIT(vid
);
3150 TPACPI_ACPIHANDLE_INIT(vid2
);
3152 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
3153 /* G41, assume IVGA doesn't change */
3154 vid_handle
= vid2_handle
;
3157 /* video switching not supported on R30, R31 */
3158 video_supported
= TPACPI_VIDEO_NONE
;
3159 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
3161 video_supported
= TPACPI_VIDEO_570
;
3162 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
3163 /* 600e/x, 770e, 770x */
3164 video_supported
= TPACPI_VIDEO_770
;
3167 video_supported
= TPACPI_VIDEO_NEW
;
3169 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
3170 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
3173 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
3176 static void video_exit(void)
3178 dbg_printk(TPACPI_DBG_EXIT
,
3179 "restoring original video autoswitch mode\n");
3180 if (video_autosw_set(video_orig_autosw
))
3181 printk(TPACPI_ERR
"error while trying to restore original "
3182 "video autoswitch mode\n");
3185 static int video_outputsw_get(void)
3190 switch (video_supported
) {
3191 case TPACPI_VIDEO_570
:
3192 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
3193 TP_ACPI_VIDEO_570_PHSCMD
))
3195 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
3197 case TPACPI_VIDEO_770
:
3198 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3201 status
|= TP_ACPI_VIDEO_S_LCD
;
3202 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3205 status
|= TP_ACPI_VIDEO_S_CRT
;
3207 case TPACPI_VIDEO_NEW
:
3208 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
3209 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3212 status
|= TP_ACPI_VIDEO_S_CRT
;
3214 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
3215 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3218 status
|= TP_ACPI_VIDEO_S_LCD
;
3219 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
3222 status
|= TP_ACPI_VIDEO_S_DVI
;
3231 static int video_outputsw_set(int status
)
3236 switch (video_supported
) {
3237 case TPACPI_VIDEO_570
:
3238 res
= acpi_evalf(NULL
, NULL
,
3239 "\\_SB.PHS2", "vdd",
3240 TP_ACPI_VIDEO_570_PHS2CMD
,
3241 status
| TP_ACPI_VIDEO_570_PHS2SET
);
3243 case TPACPI_VIDEO_770
:
3244 autosw
= video_autosw_get();
3248 res
= video_autosw_set(1);
3251 res
= acpi_evalf(vid_handle
, NULL
,
3252 "ASWT", "vdd", status
* 0x100, 0);
3253 if (!autosw
&& video_autosw_set(autosw
)) {
3255 "video auto-switch left enabled due to error\n");
3259 case TPACPI_VIDEO_NEW
:
3260 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
3261 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
3267 return (res
)? 0 : -EIO
;
3270 static int video_autosw_get(void)
3274 switch (video_supported
) {
3275 case TPACPI_VIDEO_570
:
3276 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
3279 case TPACPI_VIDEO_770
:
3280 case TPACPI_VIDEO_NEW
:
3281 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
3291 static int video_autosw_set(int enable
)
3293 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
3298 static int video_outputsw_cycle(void)
3300 int autosw
= video_autosw_get();
3306 switch (video_supported
) {
3307 case TPACPI_VIDEO_570
:
3308 res
= video_autosw_set(1);
3311 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
3313 case TPACPI_VIDEO_770
:
3314 case TPACPI_VIDEO_NEW
:
3315 res
= video_autosw_set(1);
3318 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
3323 if (!autosw
&& video_autosw_set(autosw
)) {
3325 "video auto-switch left enabled due to error\n");
3329 return (res
)? 0 : -EIO
;
3332 static int video_expand_toggle(void)
3334 switch (video_supported
) {
3335 case TPACPI_VIDEO_570
:
3336 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
3338 case TPACPI_VIDEO_770
:
3339 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
3341 case TPACPI_VIDEO_NEW
:
3342 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
3350 static int video_read(char *p
)
3355 if (video_supported
== TPACPI_VIDEO_NONE
) {
3356 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3360 status
= video_outputsw_get();
3364 autosw
= video_autosw_get();
3368 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3369 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
3370 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
3371 if (video_supported
== TPACPI_VIDEO_NEW
)
3372 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
3373 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
3374 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
3375 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
3376 if (video_supported
== TPACPI_VIDEO_NEW
)
3377 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
3378 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
3379 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
3384 static int video_write(char *buf
)
3387 int enable
, disable
, status
;
3390 if (video_supported
== TPACPI_VIDEO_NONE
)
3396 while ((cmd
= next_cmd(&buf
))) {
3397 if (strlencmp(cmd
, "lcd_enable") == 0) {
3398 enable
|= TP_ACPI_VIDEO_S_LCD
;
3399 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
3400 disable
|= TP_ACPI_VIDEO_S_LCD
;
3401 } else if (strlencmp(cmd
, "crt_enable") == 0) {
3402 enable
|= TP_ACPI_VIDEO_S_CRT
;
3403 } else if (strlencmp(cmd
, "crt_disable") == 0) {
3404 disable
|= TP_ACPI_VIDEO_S_CRT
;
3405 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3406 strlencmp(cmd
, "dvi_enable") == 0) {
3407 enable
|= TP_ACPI_VIDEO_S_DVI
;
3408 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
3409 strlencmp(cmd
, "dvi_disable") == 0) {
3410 disable
|= TP_ACPI_VIDEO_S_DVI
;
3411 } else if (strlencmp(cmd
, "auto_enable") == 0) {
3412 res
= video_autosw_set(1);
3415 } else if (strlencmp(cmd
, "auto_disable") == 0) {
3416 res
= video_autosw_set(0);
3419 } else if (strlencmp(cmd
, "video_switch") == 0) {
3420 res
= video_outputsw_cycle();
3423 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
3424 res
= video_expand_toggle();
3431 if (enable
|| disable
) {
3432 status
= video_outputsw_get();
3435 res
= video_outputsw_set((status
& ~disable
) | enable
);
3443 static struct ibm_struct video_driver_data
= {
3446 .write
= video_write
,
3450 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3452 /*************************************************************************
3453 * Light (thinklight) subdriver
3456 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3457 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
3459 static int light_get_status(void)
3463 if (tp_features
.light_status
) {
3464 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
3472 static int light_set_status(int status
)
3476 if (tp_features
.light
) {
3478 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
3480 TP_CMOS_THINKLIGHT_ON
:
3481 TP_CMOS_THINKLIGHT_OFF
);
3483 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
3486 return (rc
)? 0 : -EIO
;
3492 static void light_set_status_worker(struct work_struct
*work
)
3494 struct tpacpi_led_classdev
*data
=
3495 container_of(work
, struct tpacpi_led_classdev
, work
);
3497 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
3498 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
3501 static void light_sysfs_set(struct led_classdev
*led_cdev
,
3502 enum led_brightness brightness
)
3504 struct tpacpi_led_classdev
*data
=
3505 container_of(led_cdev
,
3506 struct tpacpi_led_classdev
,
3508 data
->new_state
= (brightness
!= LED_OFF
) ?
3509 TPACPI_LED_ON
: TPACPI_LED_OFF
;
3510 queue_work(tpacpi_wq
, &data
->work
);
3513 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
3515 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
3518 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
3520 .name
= "tpacpi::thinklight",
3521 .brightness_set
= &light_sysfs_set
,
3522 .brightness_get
= &light_sysfs_get
,
3526 static int __init
light_init(struct ibm_init_struct
*iibm
)
3530 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
3532 TPACPI_ACPIHANDLE_INIT(ledb
);
3533 TPACPI_ACPIHANDLE_INIT(lght
);
3534 TPACPI_ACPIHANDLE_INIT(cmos
);
3535 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
3537 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3538 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
3540 if (tp_features
.light
)
3541 /* light status not supported on
3542 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3543 tp_features
.light_status
=
3544 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
3546 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
3547 str_supported(tp_features
.light
),
3548 str_supported(tp_features
.light_status
));
3550 if (!tp_features
.light
)
3553 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
3554 &tpacpi_led_thinklight
.led_classdev
);
3557 tp_features
.light
= 0;
3558 tp_features
.light_status
= 0;
3566 static void light_exit(void)
3568 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
3569 if (work_pending(&tpacpi_led_thinklight
.work
))
3570 flush_workqueue(tpacpi_wq
);
3573 static int light_read(char *p
)
3578 if (!tp_features
.light
) {
3579 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3580 } else if (!tp_features
.light_status
) {
3581 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
3582 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3584 status
= light_get_status();
3587 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
3588 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
3594 static int light_write(char *buf
)
3599 if (!tp_features
.light
)
3602 while ((cmd
= next_cmd(&buf
))) {
3603 if (strlencmp(cmd
, "on") == 0) {
3605 } else if (strlencmp(cmd
, "off") == 0) {
3611 return light_set_status(newstatus
);
3614 static struct ibm_struct light_driver_data
= {
3617 .write
= light_write
,
3621 /*************************************************************************
3625 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3627 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
3628 static int dock_read(char *p
);
3629 static int dock_write(char *buf
);
3631 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
3632 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3633 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3634 "\\_SB.PCI.ISA.SLCE", /* 570 */
3635 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3637 /* don't list other alternatives as we install a notify handler on the 570 */
3638 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
3640 static const struct acpi_device_id ibm_pci_device_ids
[] = {
3641 {PCI_ROOT_HID_STRING
, 0},
3645 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
3647 .notify
= dock_notify
,
3648 .handle
= &dock_handle
,
3649 .type
= ACPI_SYSTEM_NOTIFY
,
3652 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3653 * We just use it to get notifications of dock hotplug
3654 * in very old thinkpads */
3655 .hid
= ibm_pci_device_ids
,
3656 .notify
= dock_notify
,
3657 .handle
= &pci_handle
,
3658 .type
= ACPI_SYSTEM_NOTIFY
,
3662 static struct ibm_struct dock_driver_data
[2] = {
3666 .write
= dock_write
,
3667 .acpi
= &ibm_dock_acpidriver
[0],
3671 .acpi
= &ibm_dock_acpidriver
[1],
3675 #define dock_docked() (_sta(dock_handle) & 1)
3677 static int __init
dock_init(struct ibm_init_struct
*iibm
)
3679 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
3681 TPACPI_ACPIHANDLE_INIT(dock
);
3683 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
3684 str_supported(dock_handle
!= NULL
));
3686 return (dock_handle
)? 0 : 1;
3689 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
3693 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
3695 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
3696 dock_driver_data
[0].flags
.acpi_notify_installed
) {
3697 TPACPI_ACPIHANDLE_INIT(pci
);
3698 dock2_needed
= (pci_handle
!= NULL
);
3699 vdbg_printk(TPACPI_DBG_INIT
,
3700 "dock PCI handler for the TP 570 is %s\n",
3701 str_supported(dock2_needed
));
3703 vdbg_printk(TPACPI_DBG_INIT
,
3704 "dock subdriver part 2 not required\n");
3708 return (dock2_needed
)? 0 : 1;
3711 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
3713 int docked
= dock_docked();
3714 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
3715 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
3718 if (event
== 1 && !pci
) /* 570 */
3719 data
= 1; /* button */
3720 else if (event
== 1 && pci
) /* 570 */
3721 data
= 3; /* dock */
3722 else if (event
== 3 && docked
)
3723 data
= 1; /* button */
3724 else if (event
== 3 && !docked
)
3725 data
= 2; /* undock */
3726 else if (event
== 0 && docked
)
3727 data
= 3; /* dock */
3729 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
3730 event
, _sta(dock_handle
));
3731 data
= 0; /* unknown */
3733 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
3734 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
3735 ibm
->acpi
->device
->dev
.bus_id
,
3739 static int dock_read(char *p
)
3742 int docked
= dock_docked();
3745 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3747 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
3749 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
3750 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
3756 static int dock_write(char *buf
)
3763 while ((cmd
= next_cmd(&buf
))) {
3764 if (strlencmp(cmd
, "undock") == 0) {
3765 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
3766 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
3768 } else if (strlencmp(cmd
, "dock") == 0) {
3769 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
3778 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3780 /*************************************************************************
3784 #ifdef CONFIG_THINKPAD_ACPI_BAY
3786 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3787 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3788 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3789 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3790 ); /* A21e, R30, R31 */
3791 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
3792 "_EJ0", /* all others */
3793 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3794 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3795 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3797 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
3801 static int __init
bay_init(struct ibm_init_struct
*iibm
)
3803 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
3805 TPACPI_ACPIHANDLE_INIT(bay
);
3807 TPACPI_ACPIHANDLE_INIT(bay_ej
);
3808 TPACPI_ACPIHANDLE_INIT(bay2
);
3810 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
3812 tp_features
.bay_status
= bay_handle
&&
3813 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
3814 tp_features
.bay_status2
= bay2_handle
&&
3815 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
3817 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
3818 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
3819 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
3820 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
3822 vdbg_printk(TPACPI_DBG_INIT
,
3823 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3824 str_supported(tp_features
.bay_status
),
3825 str_supported(tp_features
.bay_eject
),
3826 str_supported(tp_features
.bay_status2
),
3827 str_supported(tp_features
.bay_eject2
));
3829 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
3830 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
3833 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
3835 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
3836 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
3837 ibm
->acpi
->device
->dev
.bus_id
,
3841 #define bay_occupied(b) (_sta(b##_handle) & 1)
3843 static int bay_read(char *p
)
3846 int occupied
= bay_occupied(bay
);
3847 int occupied2
= bay_occupied(bay2
);
3850 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3851 tp_features
.bay_status
?
3852 (occupied
? "occupied" : "unoccupied") :
3854 if (tp_features
.bay_status2
)
3855 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
3856 "occupied" : "unoccupied");
3858 eject
= tp_features
.bay_eject
&& occupied
;
3859 eject2
= tp_features
.bay_eject2
&& occupied2
;
3861 if (eject
&& eject2
)
3862 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
3864 len
+= sprintf(p
+ len
, "commands:\teject\n");
3866 len
+= sprintf(p
+ len
, "commands:\teject2\n");
3871 static int bay_write(char *buf
)
3875 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
3878 while ((cmd
= next_cmd(&buf
))) {
3879 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
3880 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
3882 } else if (tp_features
.bay_eject2
&&
3883 strlencmp(cmd
, "eject2") == 0) {
3884 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
3893 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
3894 .notify
= bay_notify
,
3895 .handle
= &bay_handle
,
3896 .type
= ACPI_SYSTEM_NOTIFY
,
3899 static struct ibm_struct bay_driver_data
= {
3903 .acpi
= &ibm_bay_acpidriver
,
3906 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3908 /*************************************************************************
3912 /* sysfs cmos_command -------------------------------------------------- */
3913 static ssize_t
cmos_command_store(struct device
*dev
,
3914 struct device_attribute
*attr
,
3915 const char *buf
, size_t count
)
3917 unsigned long cmos_cmd
;
3920 if (parse_strtoul(buf
, 21, &cmos_cmd
))
3923 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3924 return (res
)? res
: count
;
3927 static struct device_attribute dev_attr_cmos_command
=
3928 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
3930 /* --------------------------------------------------------------------- */
3932 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
3936 vdbg_printk(TPACPI_DBG_INIT
,
3937 "initializing cmos commands subdriver\n");
3939 TPACPI_ACPIHANDLE_INIT(cmos
);
3941 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
3942 str_supported(cmos_handle
!= NULL
));
3944 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3948 return (cmos_handle
)? 0 : 1;
3951 static void cmos_exit(void)
3953 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
3956 static int cmos_read(char *p
)
3960 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3961 R30, R31, T20-22, X20-21 */
3963 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3965 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3966 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
3972 static int cmos_write(char *buf
)
3977 while ((cmd
= next_cmd(&buf
))) {
3978 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
3979 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
3984 res
= issue_thinkpad_cmos_command(cmos_cmd
);
3992 static struct ibm_struct cmos_driver_data
= {
3995 .write
= cmos_write
,
3999 /*************************************************************************
4003 enum led_access_mode
{
4004 TPACPI_LED_NONE
= 0,
4005 TPACPI_LED_570
, /* 570 */
4006 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4007 TPACPI_LED_NEW
, /* all others */
4010 enum { /* For TPACPI_LED_OLD */
4011 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4012 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4013 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4016 static enum led_access_mode led_supported
;
4018 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4019 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4020 /* T20-22, X20-21 */
4021 "LED", /* all others */
4024 #define TPACPI_LED_NUMLEDS 8
4025 static struct tpacpi_led_classdev
*tpacpi_leds
;
4026 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
4027 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
4028 /* there's a limit of 19 chars + NULL before 2.6.26 */
4030 "tpacpi:orange:batt",
4031 "tpacpi:green:batt",
4032 "tpacpi::dock_active",
4033 "tpacpi::bay_active",
4034 "tpacpi::dock_batt",
4035 "tpacpi::unknown_led",
4039 static int led_get_status(const unsigned int led
)
4042 enum led_status_t led_s
;
4044 switch (led_supported
) {
4045 case TPACPI_LED_570
:
4046 if (!acpi_evalf(ec_handle
,
4047 &status
, "GLED", "dd", 1 << led
))
4049 led_s
= (status
== 0)?
4054 tpacpi_led_state_cache
[led
] = led_s
;
4063 static int led_set_status(const unsigned int led
,
4064 const enum led_status_t ledstatus
)
4066 /* off, on, blink. Index is led_status_t */
4067 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
4068 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
4072 switch (led_supported
) {
4073 case TPACPI_LED_570
:
4077 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4078 (1 << led
), led_sled_arg1
[ledstatus
]))
4081 case TPACPI_LED_OLD
:
4082 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4085 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
4087 rc
= ec_write(TPACPI_LED_EC_HLBL
,
4088 (ledstatus
== TPACPI_LED_BLINK
) << led
);
4090 rc
= ec_write(TPACPI_LED_EC_HLCL
,
4091 (ledstatus
!= TPACPI_LED_OFF
) << led
);
4093 case TPACPI_LED_NEW
:
4095 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4096 led
, led_led_arg1
[ledstatus
]))
4104 tpacpi_led_state_cache
[led
] = ledstatus
;
4109 static void led_set_status_worker(struct work_struct
*work
)
4111 struct tpacpi_led_classdev
*data
=
4112 container_of(work
, struct tpacpi_led_classdev
, work
);
4114 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4115 led_set_status(data
->led
, data
->new_state
);
4118 static void led_sysfs_set(struct led_classdev
*led_cdev
,
4119 enum led_brightness brightness
)
4121 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4122 struct tpacpi_led_classdev
, led_classdev
);
4124 if (brightness
== LED_OFF
)
4125 data
->new_state
= TPACPI_LED_OFF
;
4126 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
4127 data
->new_state
= TPACPI_LED_ON
;
4129 data
->new_state
= TPACPI_LED_BLINK
;
4131 queue_work(tpacpi_wq
, &data
->work
);
4134 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
4135 unsigned long *delay_on
, unsigned long *delay_off
)
4137 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4138 struct tpacpi_led_classdev
, led_classdev
);
4140 /* Can we choose the flash rate? */
4141 if (*delay_on
== 0 && *delay_off
== 0) {
4142 /* yes. set them to the hardware blink rate (1 Hz) */
4143 *delay_on
= 500; /* ms */
4144 *delay_off
= 500; /* ms */
4145 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
4148 data
->new_state
= TPACPI_LED_BLINK
;
4149 queue_work(tpacpi_wq
, &data
->work
);
4154 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
4158 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4159 struct tpacpi_led_classdev
, led_classdev
);
4161 rc
= led_get_status(data
->led
);
4163 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
4164 rc
= LED_OFF
; /* no error handling in led class :( */
4171 static void led_exit(void)
4175 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4176 if (tpacpi_leds
[i
].led_classdev
.name
)
4177 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
4183 static int __init
led_init(struct ibm_init_struct
*iibm
)
4188 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
4190 TPACPI_ACPIHANDLE_INIT(led
);
4193 /* led not supported on R30, R31 */
4194 led_supported
= TPACPI_LED_NONE
;
4195 else if (strlencmp(led_path
, "SLED") == 0)
4197 led_supported
= TPACPI_LED_570
;
4198 else if (strlencmp(led_path
, "SYSL") == 0)
4199 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4200 led_supported
= TPACPI_LED_OLD
;
4203 led_supported
= TPACPI_LED_NEW
;
4205 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
4206 str_supported(led_supported
), led_supported
);
4208 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
4211 printk(TPACPI_ERR
"Out of memory for LED data\n");
4215 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4216 tpacpi_leds
[i
].led
= i
;
4218 tpacpi_leds
[i
].led_classdev
.brightness_set
= &led_sysfs_set
;
4219 tpacpi_leds
[i
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
4220 if (led_supported
== TPACPI_LED_570
)
4221 tpacpi_leds
[i
].led_classdev
.brightness_get
=
4224 tpacpi_leds
[i
].led_classdev
.name
= tpacpi_led_names
[i
];
4226 INIT_WORK(&tpacpi_leds
[i
].work
, led_set_status_worker
);
4228 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4229 &tpacpi_leds
[i
].led_classdev
);
4231 tpacpi_leds
[i
].led_classdev
.name
= NULL
;
4237 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
4240 #define str_led_status(s) \
4241 ((s) == TPACPI_LED_OFF ? "off" : \
4242 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4244 static int led_read(char *p
)
4248 if (!led_supported
) {
4249 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4252 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4254 if (led_supported
== TPACPI_LED_570
) {
4257 for (i
= 0; i
< 8; i
++) {
4258 status
= led_get_status(i
);
4261 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
4262 i
, str_led_status(status
));
4266 len
+= sprintf(p
+ len
, "commands:\t"
4267 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4272 static int led_write(char *buf
)
4276 enum led_status_t s
;
4281 while ((cmd
= next_cmd(&buf
))) {
4282 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
4285 if (strstr(cmd
, "off")) {
4287 } else if (strstr(cmd
, "on")) {
4289 } else if (strstr(cmd
, "blink")) {
4290 s
= TPACPI_LED_BLINK
;
4295 rc
= led_set_status(led
, s
);
4303 static struct ibm_struct led_driver_data
= {
4310 /*************************************************************************
4314 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
4316 static int __init
beep_init(struct ibm_init_struct
*iibm
)
4318 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
4320 TPACPI_ACPIHANDLE_INIT(beep
);
4322 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
4323 str_supported(beep_handle
!= NULL
));
4325 return (beep_handle
)? 0 : 1;
4328 static int beep_read(char *p
)
4333 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4335 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4336 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
4342 static int beep_write(char *buf
)
4350 while ((cmd
= next_cmd(&buf
))) {
4351 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
4352 beep_cmd
>= 0 && beep_cmd
<= 17) {
4356 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
4363 static struct ibm_struct beep_driver_data
= {
4366 .write
= beep_write
,
4369 /*************************************************************************
4373 enum thermal_access_mode
{
4374 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
4375 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
4376 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
4377 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
4378 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
4381 enum { /* TPACPI_THERMAL_TPEC_* */
4382 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
4383 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
4384 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
4387 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4388 struct ibm_thermal_sensors_struct
{
4389 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
4392 static enum thermal_access_mode thermal_read_mode
;
4394 /* idx is zero-based */
4395 static int thermal_get_sensor(int idx
, s32
*value
)
4401 t
= TP_EC_THERMAL_TMP0
;
4403 switch (thermal_read_mode
) {
4404 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4405 case TPACPI_THERMAL_TPEC_16
:
4406 if (idx
>= 8 && idx
<= 15) {
4407 t
= TP_EC_THERMAL_TMP8
;
4412 case TPACPI_THERMAL_TPEC_8
:
4414 if (!acpi_ec_read(t
+ idx
, &tmp
))
4416 *value
= tmp
* 1000;
4421 case TPACPI_THERMAL_ACPI_UPDT
:
4423 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
4424 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
4426 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
4428 *value
= (t
- 2732) * 100;
4433 case TPACPI_THERMAL_ACPI_TMP07
:
4435 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
4436 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
4438 if (t
> 127 || t
< -127)
4439 t
= TP_EC_THERMAL_TMP_NA
;
4445 case TPACPI_THERMAL_NONE
:
4453 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
4464 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
4467 for (i
= 0 ; i
< n
; i
++) {
4468 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
4476 /* sysfs temp##_input -------------------------------------------------- */
4478 static ssize_t
thermal_temp_input_show(struct device
*dev
,
4479 struct device_attribute
*attr
,
4482 struct sensor_device_attribute
*sensor_attr
=
4483 to_sensor_dev_attr(attr
);
4484 int idx
= sensor_attr
->index
;
4488 res
= thermal_get_sensor(idx
, &value
);
4491 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
4494 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
4497 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4498 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4499 thermal_temp_input_show, NULL, _idxB)
4501 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
4502 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4503 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4504 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4505 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4506 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4507 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4508 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4509 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4510 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4511 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4512 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4513 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4514 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4515 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4516 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4517 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4520 #define THERMAL_ATTRS(X) \
4521 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4523 static struct attribute
*thermal_temp_input_attr
[] = {
4543 static const struct attribute_group thermal_temp_input16_group
= {
4544 .attrs
= thermal_temp_input_attr
4547 static const struct attribute_group thermal_temp_input8_group
= {
4548 .attrs
= &thermal_temp_input_attr
[8]
4551 #undef THERMAL_SENSOR_ATTR_TEMP
4552 #undef THERMAL_ATTRS
4554 /* --------------------------------------------------------------------- */
4556 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
4563 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
4565 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
4567 if (thinkpad_id
.ec_model
) {
4569 * Direct EC access mode: sensors at registers
4570 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4571 * non-implemented, thermal sensors return 0x80 when
4576 for (i
= 0; i
< 8; i
++) {
4577 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
4583 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
4591 /* This is sheer paranoia, but we handle it anyway */
4594 "ThinkPad ACPI EC access misbehaving, "
4595 "falling back to ACPI TMPx access "
4597 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
4600 "ThinkPad ACPI EC access misbehaving, "
4601 "disabling thermal sensors access\n");
4602 thermal_read_mode
= TPACPI_THERMAL_NONE
;
4607 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
4609 } else if (acpi_tmp7
) {
4610 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
4611 /* 600e/x, 770e, 770x */
4612 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
4614 /* Standard ACPI TMPx access, max 8 sensors */
4615 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
4618 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4619 thermal_read_mode
= TPACPI_THERMAL_NONE
;
4622 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
4623 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
4626 switch (thermal_read_mode
) {
4627 case TPACPI_THERMAL_TPEC_16
:
4628 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4629 &thermal_temp_input16_group
);
4633 case TPACPI_THERMAL_TPEC_8
:
4634 case TPACPI_THERMAL_ACPI_TMP07
:
4635 case TPACPI_THERMAL_ACPI_UPDT
:
4636 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4637 &thermal_temp_input8_group
);
4641 case TPACPI_THERMAL_NONE
:
4649 static void thermal_exit(void)
4651 switch (thermal_read_mode
) {
4652 case TPACPI_THERMAL_TPEC_16
:
4653 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4654 &thermal_temp_input16_group
);
4656 case TPACPI_THERMAL_TPEC_8
:
4657 case TPACPI_THERMAL_ACPI_TMP07
:
4658 case TPACPI_THERMAL_ACPI_UPDT
:
4659 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
4660 &thermal_temp_input16_group
);
4662 case TPACPI_THERMAL_NONE
:
4668 static int thermal_read(char *p
)
4672 struct ibm_thermal_sensors_struct t
;
4674 n
= thermal_get_sensors(&t
);
4675 if (unlikely(n
< 0))
4678 len
+= sprintf(p
+ len
, "temperatures:\t");
4681 for (i
= 0; i
< (n
- 1); i
++)
4682 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
4683 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
4685 len
+= sprintf(p
+ len
, "not supported\n");
4690 static struct ibm_struct thermal_driver_data
= {
4692 .read
= thermal_read
,
4693 .exit
= thermal_exit
,
4696 /*************************************************************************
4700 static u8 ecdump_regs
[256];
4702 static int ecdump_read(char *p
)
4708 len
+= sprintf(p
+ len
, "EC "
4709 " +00 +01 +02 +03 +04 +05 +06 +07"
4710 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4711 for (i
= 0; i
< 256; i
+= 16) {
4712 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
4713 for (j
= 0; j
< 16; j
++) {
4714 if (!acpi_ec_read(i
+ j
, &v
))
4716 if (v
!= ecdump_regs
[i
+ j
])
4717 len
+= sprintf(p
+ len
, " *%02x", v
);
4719 len
+= sprintf(p
+ len
, " %02x", v
);
4720 ecdump_regs
[i
+ j
] = v
;
4722 len
+= sprintf(p
+ len
, "\n");
4727 /* These are way too dangerous to advertise openly... */
4729 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
4730 " (<offset> is 00-ff, <value> is 00-ff)\n");
4731 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
4732 " (<offset> is 00-ff, <value> is 0-255)\n");
4737 static int ecdump_write(char *buf
)
4742 while ((cmd
= next_cmd(&buf
))) {
4743 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
4745 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
4749 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
4750 if (!acpi_ec_write(i
, v
))
4759 static struct ibm_struct ecdump_driver_data
= {
4761 .read
= ecdump_read
,
4762 .write
= ecdump_write
,
4763 .flags
.experimental
= 1,
4766 /*************************************************************************
4767 * Backlight/brightness subdriver
4770 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4773 TP_EC_BACKLIGHT
= 0x31,
4775 /* TP_EC_BACKLIGHT bitmasks */
4776 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
4777 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
4778 TP_EC_BACKLIGHT_MAPSW
= 0x20,
4781 static struct backlight_device
*ibm_backlight_device
;
4782 static int brightness_mode
;
4783 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
4785 static struct mutex brightness_mutex
;
4788 * ThinkPads can read brightness from two places: EC 0x31, or
4789 * CMOS NVRAM byte 0x5E, bits 0-3.
4791 * EC 0x31 has the following layout
4792 * Bit 7: unknown function
4793 * Bit 6: unknown function
4794 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4795 * Bit 4: must be set to zero to avoid problems
4796 * Bit 3-0: backlight brightness level
4798 * brightness_get_raw returns status data in the EC 0x31 layout
4800 static int brightness_get_raw(int *status
)
4802 u8 lec
= 0, lcmos
= 0, level
= 0;
4804 if (brightness_mode
& 1) {
4805 if (!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
))
4807 level
= lec
& TP_EC_BACKLIGHT_LVLMSK
;
4809 if (brightness_mode
& 2) {
4810 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
4811 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
4812 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
4813 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
4817 if (brightness_mode
== 3) {
4818 *status
= lec
; /* Prefer EC, CMOS is just a backing store */
4819 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
4821 tp_warned
.bright_cmos_ec_unsync
= 0;
4823 if (!tp_warned
.bright_cmos_ec_unsync
) {
4825 "CMOS NVRAM (%u) and EC (%u) do not "
4826 "agree on display brightness level\n",
4827 (unsigned int) lcmos
,
4828 (unsigned int) lec
);
4829 tp_warned
.bright_cmos_ec_unsync
= 1;
4840 /* May return EINTR which can always be mapped to ERESTARTSYS */
4841 static int brightness_set(int value
)
4843 int cmos_cmd
, inc
, i
, res
;
4847 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
4851 res
= mutex_lock_interruptible(&brightness_mutex
);
4855 res
= brightness_get_raw(¤t_value
);
4859 command_bits
= current_value
& TP_EC_BACKLIGHT_CMDMSK
;
4860 current_value
&= TP_EC_BACKLIGHT_LVLMSK
;
4862 cmos_cmd
= value
> current_value
?
4863 TP_CMOS_BRIGHTNESS_UP
:
4864 TP_CMOS_BRIGHTNESS_DOWN
;
4865 inc
= (value
> current_value
)? 1 : -1;
4868 for (i
= current_value
; i
!= value
; i
+= inc
) {
4869 if ((brightness_mode
& 2) &&
4870 issue_thinkpad_cmos_command(cmos_cmd
)) {
4874 if ((brightness_mode
& 1) &&
4875 !acpi_ec_write(TP_EC_BACKLIGHT
,
4876 (i
+ inc
) | command_bits
)) {
4883 mutex_unlock(&brightness_mutex
);
4887 /* sysfs backlight class ----------------------------------------------- */
4889 static int brightness_update_status(struct backlight_device
*bd
)
4891 /* it is the backlight class's job (caller) to handle
4892 * EINTR and other errors properly */
4893 return brightness_set(
4894 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
4895 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
4896 bd
->props
.brightness
: 0);
4899 static int brightness_get(struct backlight_device
*bd
)
4903 res
= brightness_get_raw(&status
);
4905 return 0; /* FIXME: teach backlight about error handling */
4907 return status
& TP_EC_BACKLIGHT_LVLMSK
;
4910 static struct backlight_ops ibm_backlight_data
= {
4911 .get_brightness
= brightness_get
,
4912 .update_status
= brightness_update_status
,
4915 /* --------------------------------------------------------------------- */
4917 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
4921 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
4923 mutex_init(&brightness_mutex
);
4926 * We always attempt to detect acpi support, so as to switch
4927 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4928 * going to publish a backlight interface
4930 b
= tpacpi_check_std_acpi_brightness_support();
4932 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
4933 printk(TPACPI_NOTICE
4934 "Lenovo BIOS switched to ACPI backlight "
4937 if (brightness_enable
> 1) {
4938 printk(TPACPI_NOTICE
4939 "standard ACPI backlight interface "
4940 "available, not loading native one...\n");
4945 if (!brightness_enable
) {
4946 dbg_printk(TPACPI_DBG_INIT
,
4947 "brightness support disabled by "
4948 "module parameter\n");
4954 "Unsupported brightness interface, "
4955 "please contact %s\n", TPACPI_MAIL
);
4959 tp_features
.bright_16levels
= 1;
4961 if (!brightness_mode
) {
4962 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
4963 brightness_mode
= 2;
4965 brightness_mode
= 3;
4967 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
4971 if (brightness_mode
> 3)
4974 if (brightness_get_raw(&b
) < 0)
4977 if (tp_features
.bright_16levels
)
4979 "detected a 16-level brightness capable ThinkPad\n");
4981 ibm_backlight_device
= backlight_device_register(
4982 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
4983 &ibm_backlight_data
);
4984 if (IS_ERR(ibm_backlight_device
)) {
4985 printk(TPACPI_ERR
"Could not register backlight device\n");
4986 return PTR_ERR(ibm_backlight_device
);
4988 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
4990 ibm_backlight_device
->props
.max_brightness
=
4991 (tp_features
.bright_16levels
)? 15 : 7;
4992 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
4993 backlight_update_status(ibm_backlight_device
);
4998 static void brightness_exit(void)
5000 if (ibm_backlight_device
) {
5001 vdbg_printk(TPACPI_DBG_EXIT
,
5002 "calling backlight_device_unregister()\n");
5003 backlight_device_unregister(ibm_backlight_device
);
5007 static int brightness_read(char *p
)
5012 level
= brightness_get(NULL
);
5014 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5016 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
5017 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
5018 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5019 " (<level> is 0-%d)\n",
5020 (tp_features
.bright_16levels
) ? 15 : 7);
5026 static int brightness_write(char *buf
)
5031 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
5033 level
= brightness_get(NULL
);
5037 while ((cmd
= next_cmd(&buf
))) {
5038 if (strlencmp(cmd
, "up") == 0) {
5039 if (level
< max_level
)
5041 } else if (strlencmp(cmd
, "down") == 0) {
5044 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
5045 level
>= 0 && level
<= max_level
) {
5052 * Now we know what the final level should be, so we try to set it.
5053 * Doing it this way makes the syscall restartable in case of EINTR
5055 rc
= brightness_set(level
);
5056 return (rc
== -EINTR
)? -ERESTARTSYS
: rc
;
5059 static struct ibm_struct brightness_driver_data
= {
5060 .name
= "brightness",
5061 .read
= brightness_read
,
5062 .write
= brightness_write
,
5063 .exit
= brightness_exit
,
5066 /*************************************************************************
5070 static int volume_offset
= 0x30;
5072 static int volume_read(char *p
)
5077 if (!acpi_ec_read(volume_offset
, &level
)) {
5078 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5080 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
5081 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
5082 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
5083 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5084 " (<level> is 0-15)\n");
5090 static int volume_write(char *buf
)
5092 int cmos_cmd
, inc
, i
;
5094 int new_level
, new_mute
;
5097 while ((cmd
= next_cmd(&buf
))) {
5098 if (!acpi_ec_read(volume_offset
, &level
))
5100 new_mute
= mute
= level
& 0x40;
5101 new_level
= level
= level
& 0xf;
5103 if (strlencmp(cmd
, "up") == 0) {
5107 new_level
= level
== 15 ? 15 : level
+ 1;
5108 } else if (strlencmp(cmd
, "down") == 0) {
5112 new_level
= level
== 0 ? 0 : level
- 1;
5113 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
5114 new_level
>= 0 && new_level
<= 15) {
5116 } else if (strlencmp(cmd
, "mute") == 0) {
5121 if (new_level
!= level
) {
5122 /* mute doesn't change */
5124 cmos_cmd
= (new_level
> level
) ?
5125 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
5126 inc
= new_level
> level
? 1 : -1;
5128 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
5129 !acpi_ec_write(volume_offset
, level
)))
5132 for (i
= level
; i
!= new_level
; i
+= inc
)
5133 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5134 !acpi_ec_write(volume_offset
, i
+ inc
))
5138 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
5139 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
5144 if (new_mute
!= mute
) {
5145 /* level doesn't change */
5147 cmos_cmd
= (new_mute
) ?
5148 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
5150 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5151 !acpi_ec_write(volume_offset
, level
+ new_mute
))
5159 static struct ibm_struct volume_driver_data
= {
5161 .read
= volume_read
,
5162 .write
= volume_write
,
5165 /*************************************************************************
5172 * TPACPI_FAN_RD_ACPI_GFAN:
5173 * ACPI GFAN method: returns fan level
5175 * see TPACPI_FAN_WR_ACPI_SFAN
5176 * EC 0x2f (HFSP) not available if GFAN exists
5178 * TPACPI_FAN_WR_ACPI_SFAN:
5179 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5181 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5184 * TPACPI_FAN_WR_TPEC:
5185 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5186 * Supported on almost all ThinkPads
5188 * Fan speed changes of any sort (including those caused by the
5189 * disengaged mode) are usually done slowly by the firmware as the
5190 * maximum ammount of fan duty cycle change per second seems to be
5193 * Reading is not available if GFAN exists.
5194 * Writing is not available if SFAN exists.
5197 * 7 automatic mode engaged;
5198 * (default operation mode of the ThinkPad)
5199 * fan level is ignored in this mode.
5200 * 6 full speed mode (takes precedence over bit 7);
5201 * not available on all thinkpads. May disable
5202 * the tachometer while the fan controller ramps up
5203 * the speed (which can take up to a few *minutes*).
5204 * Speeds up fan to 100% duty-cycle, which is far above
5205 * the standard RPM levels. It is not impossible that
5206 * it could cause hardware damage.
5207 * 5-3 unused in some models. Extra bits for fan level
5208 * in others, but still useless as all values above
5209 * 7 map to the same speed as level 7 in these models.
5210 * 2-0 fan level (0..7 usually)
5212 * 0x07 = max (set when temperatures critical)
5213 * Some ThinkPads may have other levels, see
5214 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5216 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5217 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5218 * does so, its initial value is meaningless (0x07).
5220 * For firmware bugs, refer to:
5221 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5225 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5226 * Main fan tachometer reading (in RPM)
5228 * This register is present on all ThinkPads with a new-style EC, and
5229 * it is known not to be present on the A21m/e, and T22, as there is
5230 * something else in offset 0x84 according to the ACPI DSDT. Other
5231 * ThinkPads from this same time period (and earlier) probably lack the
5232 * tachometer as well.
5234 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5235 * was never fixed by IBM to report the EC firmware version string
5236 * probably support the tachometer (like the early X models), so
5237 * detecting it is quite hard. We need more data to know for sure.
5239 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5242 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5245 * For firmware bugs, refer to:
5246 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5248 * TPACPI_FAN_WR_ACPI_FANS:
5249 * ThinkPad X31, X40, X41. Not available in the X60.
5251 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5252 * high speed. ACPI DSDT seems to map these three speeds to levels
5253 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5254 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5256 * The speeds are stored on handles
5257 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5259 * There are three default speed sets, acessible as handles:
5260 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5262 * ACPI DSDT switches which set is in use depending on various
5265 * TPACPI_FAN_WR_TPEC is also available and should be used to
5266 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5267 * but the ACPI tables just mention level 7.
5270 enum { /* Fan control constants */
5271 fan_status_offset
= 0x2f, /* EC register 0x2f */
5272 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5273 * 0x84 must be read before 0x85 */
5275 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
5276 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
5278 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
5281 enum fan_status_access_mode
{
5282 TPACPI_FAN_NONE
= 0, /* No fan status or control */
5283 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
5284 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5287 enum fan_control_access_mode
{
5288 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
5289 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
5290 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
5291 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
5294 enum fan_control_commands
{
5295 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
5296 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
5297 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
5298 * and also watchdog cmd */
5301 static int fan_control_allowed
;
5303 static enum fan_status_access_mode fan_status_access_mode
;
5304 static enum fan_control_access_mode fan_control_access_mode
;
5305 static enum fan_control_commands fan_control_commands
;
5307 static u8 fan_control_initial_status
;
5308 static u8 fan_control_desired_level
;
5309 static u8 fan_control_resume_level
;
5310 static int fan_watchdog_maxinterval
;
5312 static struct mutex fan_mutex
;
5314 static void fan_watchdog_fire(struct work_struct
*ignored
);
5315 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
5317 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
5318 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
5319 "\\FSPD", /* 600e/x, 770e, 770x */
5321 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
5322 "JFNS", /* 770x-JL */
5326 * Call with fan_mutex held
5328 static void fan_update_desired_level(u8 status
)
5331 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
5333 fan_control_desired_level
= 7;
5335 fan_control_desired_level
= status
;
5339 static int fan_get_status(u8
*status
)
5344 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5346 switch (fan_status_access_mode
) {
5347 case TPACPI_FAN_RD_ACPI_GFAN
:
5348 /* 570, 600e/x, 770e, 770x */
5350 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
5358 case TPACPI_FAN_RD_TPEC
:
5359 /* all except 570, 600e/x, 770e, 770x */
5360 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
5375 static int fan_get_status_safe(u8
*status
)
5380 if (mutex_lock_interruptible(&fan_mutex
))
5381 return -ERESTARTSYS
;
5382 rc
= fan_get_status(&s
);
5384 fan_update_desired_level(s
);
5385 mutex_unlock(&fan_mutex
);
5393 static int fan_get_speed(unsigned int *speed
)
5397 switch (fan_status_access_mode
) {
5398 case TPACPI_FAN_RD_TPEC
:
5399 /* all except 570, 600e/x, 770e, 770x */
5400 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
5401 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
5405 *speed
= (hi
<< 8) | lo
;
5416 static int fan_set_level(int level
)
5418 if (!fan_control_allowed
)
5421 switch (fan_control_access_mode
) {
5422 case TPACPI_FAN_WR_ACPI_SFAN
:
5423 if (level
>= 0 && level
<= 7) {
5424 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
5430 case TPACPI_FAN_WR_ACPI_FANS
:
5431 case TPACPI_FAN_WR_TPEC
:
5432 if (!(level
& TP_EC_FAN_AUTO
) &&
5433 !(level
& TP_EC_FAN_FULLSPEED
) &&
5434 ((level
< 0) || (level
> 7)))
5437 /* safety net should the EC not support AUTO
5438 * or FULLSPEED mode bits and just ignore them */
5439 if (level
& TP_EC_FAN_FULLSPEED
)
5440 level
|= 7; /* safety min speed 7 */
5441 else if (level
& TP_EC_FAN_AUTO
)
5442 level
|= 4; /* safety min speed 4 */
5444 if (!acpi_ec_write(fan_status_offset
, level
))
5447 tp_features
.fan_ctrl_status_undef
= 0;
5456 static int fan_set_level_safe(int level
)
5460 if (!fan_control_allowed
)
5463 if (mutex_lock_interruptible(&fan_mutex
))
5464 return -ERESTARTSYS
;
5466 if (level
== TPACPI_FAN_LAST_LEVEL
)
5467 level
= fan_control_desired_level
;
5469 rc
= fan_set_level(level
);
5471 fan_update_desired_level(level
);
5473 mutex_unlock(&fan_mutex
);
5477 static int fan_set_enable(void)
5482 if (!fan_control_allowed
)
5485 if (mutex_lock_interruptible(&fan_mutex
))
5486 return -ERESTARTSYS
;
5488 switch (fan_control_access_mode
) {
5489 case TPACPI_FAN_WR_ACPI_FANS
:
5490 case TPACPI_FAN_WR_TPEC
:
5491 rc
= fan_get_status(&s
);
5495 /* Don't go out of emergency fan mode */
5498 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
5501 if (!acpi_ec_write(fan_status_offset
, s
))
5504 tp_features
.fan_ctrl_status_undef
= 0;
5509 case TPACPI_FAN_WR_ACPI_SFAN
:
5510 rc
= fan_get_status(&s
);
5516 /* Set fan to at least level 4 */
5519 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
5529 mutex_unlock(&fan_mutex
);
5533 static int fan_set_disable(void)
5537 if (!fan_control_allowed
)
5540 if (mutex_lock_interruptible(&fan_mutex
))
5541 return -ERESTARTSYS
;
5544 switch (fan_control_access_mode
) {
5545 case TPACPI_FAN_WR_ACPI_FANS
:
5546 case TPACPI_FAN_WR_TPEC
:
5547 if (!acpi_ec_write(fan_status_offset
, 0x00))
5550 fan_control_desired_level
= 0;
5551 tp_features
.fan_ctrl_status_undef
= 0;
5555 case TPACPI_FAN_WR_ACPI_SFAN
:
5556 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
5559 fan_control_desired_level
= 0;
5567 mutex_unlock(&fan_mutex
);
5571 static int fan_set_speed(int speed
)
5575 if (!fan_control_allowed
)
5578 if (mutex_lock_interruptible(&fan_mutex
))
5579 return -ERESTARTSYS
;
5582 switch (fan_control_access_mode
) {
5583 case TPACPI_FAN_WR_ACPI_FANS
:
5584 if (speed
>= 0 && speed
<= 65535) {
5585 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
5586 speed
, speed
, speed
))
5596 mutex_unlock(&fan_mutex
);
5600 static void fan_watchdog_reset(void)
5602 static int fan_watchdog_active
;
5604 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
5607 if (fan_watchdog_active
)
5608 cancel_delayed_work(&fan_watchdog_task
);
5610 if (fan_watchdog_maxinterval
> 0 &&
5611 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
5612 fan_watchdog_active
= 1;
5613 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
5614 msecs_to_jiffies(fan_watchdog_maxinterval
5617 "failed to queue the fan watchdog, "
5618 "watchdog will not trigger\n");
5621 fan_watchdog_active
= 0;
5624 static void fan_watchdog_fire(struct work_struct
*ignored
)
5628 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
5631 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
5632 rc
= fan_set_enable();
5634 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
5635 "will try again later...\n", -rc
);
5636 /* reschedule for later */
5637 fan_watchdog_reset();
5642 * SYSFS fan layout: hwmon compatible (device)
5645 * 0: "disengaged" mode
5647 * 2: native EC "auto" mode (recommended, hardware default)
5649 * pwm*: set speed in manual mode, ignored otherwise.
5650 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5653 * fan*_input: tachometer reading, RPM
5656 * SYSFS fan layout: extensions
5658 * fan_watchdog (driver):
5659 * fan watchdog interval in seconds, 0 disables (default), max 120
5662 /* sysfs fan pwm1_enable ----------------------------------------------- */
5663 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
5664 struct device_attribute
*attr
,
5670 res
= fan_get_status_safe(&status
);
5674 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5675 if (status
!= fan_control_initial_status
) {
5676 tp_features
.fan_ctrl_status_undef
= 0;
5678 /* Return most likely status. In fact, it
5679 * might be the only possible status */
5680 status
= TP_EC_FAN_AUTO
;
5684 if (status
& TP_EC_FAN_FULLSPEED
) {
5686 } else if (status
& TP_EC_FAN_AUTO
) {
5691 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
5694 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
5695 struct device_attribute
*attr
,
5696 const char *buf
, size_t count
)
5701 if (parse_strtoul(buf
, 2, &t
))
5706 level
= TP_EC_FAN_FULLSPEED
;
5709 level
= TPACPI_FAN_LAST_LEVEL
;
5712 level
= TP_EC_FAN_AUTO
;
5715 /* reserved for software-controlled auto mode */
5721 res
= fan_set_level_safe(level
);
5727 fan_watchdog_reset();
5732 static struct device_attribute dev_attr_fan_pwm1_enable
=
5733 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
5734 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
5736 /* sysfs fan pwm1 ------------------------------------------------------ */
5737 static ssize_t
fan_pwm1_show(struct device
*dev
,
5738 struct device_attribute
*attr
,
5744 res
= fan_get_status_safe(&status
);
5748 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5749 if (status
!= fan_control_initial_status
) {
5750 tp_features
.fan_ctrl_status_undef
= 0;
5752 status
= TP_EC_FAN_AUTO
;
5757 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
5758 status
= fan_control_desired_level
;
5763 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
5766 static ssize_t
fan_pwm1_store(struct device
*dev
,
5767 struct device_attribute
*attr
,
5768 const char *buf
, size_t count
)
5772 u8 status
, newlevel
;
5774 if (parse_strtoul(buf
, 255, &s
))
5777 /* scale down from 0-255 to 0-7 */
5778 newlevel
= (s
>> 5) & 0x07;
5780 if (mutex_lock_interruptible(&fan_mutex
))
5781 return -ERESTARTSYS
;
5783 rc
= fan_get_status(&status
);
5784 if (!rc
&& (status
&
5785 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
5786 rc
= fan_set_level(newlevel
);
5790 fan_update_desired_level(newlevel
);
5791 fan_watchdog_reset();
5795 mutex_unlock(&fan_mutex
);
5796 return (rc
)? rc
: count
;
5799 static struct device_attribute dev_attr_fan_pwm1
=
5800 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
5801 fan_pwm1_show
, fan_pwm1_store
);
5803 /* sysfs fan fan1_input ------------------------------------------------ */
5804 static ssize_t
fan_fan1_input_show(struct device
*dev
,
5805 struct device_attribute
*attr
,
5811 res
= fan_get_speed(&speed
);
5815 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
5818 static struct device_attribute dev_attr_fan_fan1_input
=
5819 __ATTR(fan1_input
, S_IRUGO
,
5820 fan_fan1_input_show
, NULL
);
5822 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5823 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
5826 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
5829 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
5830 const char *buf
, size_t count
)
5834 if (parse_strtoul(buf
, 120, &t
))
5837 if (!fan_control_allowed
)
5840 fan_watchdog_maxinterval
= t
;
5841 fan_watchdog_reset();
5846 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
5847 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
5849 /* --------------------------------------------------------------------- */
5850 static struct attribute
*fan_attributes
[] = {
5851 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
5852 &dev_attr_fan_fan1_input
.attr
,
5856 static const struct attribute_group fan_attr_group
= {
5857 .attrs
= fan_attributes
,
5860 static int __init
fan_init(struct ibm_init_struct
*iibm
)
5864 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
5866 mutex_init(&fan_mutex
);
5867 fan_status_access_mode
= TPACPI_FAN_NONE
;
5868 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5869 fan_control_commands
= 0;
5870 fan_watchdog_maxinterval
= 0;
5871 tp_features
.fan_ctrl_status_undef
= 0;
5872 fan_control_desired_level
= 7;
5874 TPACPI_ACPIHANDLE_INIT(fans
);
5875 TPACPI_ACPIHANDLE_INIT(gfan
);
5876 TPACPI_ACPIHANDLE_INIT(sfan
);
5879 /* 570, 600e/x, 770e, 770x */
5880 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
5882 /* all other ThinkPads: note that even old-style
5883 * ThinkPad ECs supports the fan control register */
5884 if (likely(acpi_ec_read(fan_status_offset
,
5885 &fan_control_initial_status
))) {
5886 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
5888 /* In some ThinkPads, neither the EC nor the ACPI
5889 * DSDT initialize the fan status, and it ends up
5890 * being set to 0x07 when it *could* be either
5893 * Enable for TP-1Y (T43), TP-78 (R51e),
5894 * TP-76 (R52), TP-70 (T43, R52), which are known
5896 if (fan_control_initial_status
== 0x07) {
5897 switch (thinkpad_id
.ec_model
) {
5898 case 0x5931: /* TP-1Y */
5899 case 0x3837: /* TP-78 */
5900 case 0x3637: /* TP-76 */
5901 case 0x3037: /* TP-70 */
5902 printk(TPACPI_NOTICE
5903 "fan_init: initial fan status "
5904 "is unknown, assuming it is "
5906 tp_features
.fan_ctrl_status_undef
= 1;
5912 "ThinkPad ACPI EC access misbehaving, "
5913 "fan status and control unavailable\n");
5920 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
5921 fan_control_commands
|=
5922 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
5925 /* gfan without sfan means no fan control */
5926 /* all other models implement TP EC 0x2f control */
5930 fan_control_access_mode
=
5931 TPACPI_FAN_WR_ACPI_FANS
;
5932 fan_control_commands
|=
5933 TPACPI_FAN_CMD_SPEED
|
5934 TPACPI_FAN_CMD_LEVEL
|
5935 TPACPI_FAN_CMD_ENABLE
;
5937 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
5938 fan_control_commands
|=
5939 TPACPI_FAN_CMD_LEVEL
|
5940 TPACPI_FAN_CMD_ENABLE
;
5945 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
5946 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
5947 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
5948 fan_status_access_mode
, fan_control_access_mode
);
5950 /* fan control master switch */
5951 if (!fan_control_allowed
) {
5952 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
5953 fan_control_commands
= 0;
5954 dbg_printk(TPACPI_DBG_INIT
,
5955 "fan control features disabled by parameter\n");
5958 /* update fan_control_desired_level */
5959 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
5960 fan_get_status_safe(NULL
);
5962 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
5963 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
5964 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5969 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
5970 &driver_attr_fan_watchdog
);
5972 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5981 static void fan_exit(void)
5983 vdbg_printk(TPACPI_DBG_EXIT
,
5984 "cancelling any pending fan watchdog tasks\n");
5986 /* FIXME: can we really do this unconditionally? */
5987 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
5988 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
5989 &driver_attr_fan_watchdog
);
5991 cancel_delayed_work(&fan_watchdog_task
);
5992 flush_workqueue(tpacpi_wq
);
5995 static void fan_suspend(pm_message_t state
)
5999 if (!fan_control_allowed
)
6002 /* Store fan status in cache */
6003 fan_control_resume_level
= 0;
6004 rc
= fan_get_status_safe(&fan_control_resume_level
);
6006 printk(TPACPI_NOTICE
6007 "failed to read fan level for later "
6008 "restore during resume: %d\n", rc
);
6010 /* if it is undefined, don't attempt to restore it.
6012 if (tp_features
.fan_ctrl_status_undef
)
6013 fan_control_resume_level
= 0;
6016 static void fan_resume(void)
6018 u8 current_level
= 7;
6019 bool do_set
= false;
6022 /* DSDT *always* updates status on resume */
6023 tp_features
.fan_ctrl_status_undef
= 0;
6025 if (!fan_control_allowed
||
6026 !fan_control_resume_level
||
6027 (fan_get_status_safe(¤t_level
) < 0))
6030 switch (fan_control_access_mode
) {
6031 case TPACPI_FAN_WR_ACPI_SFAN
:
6032 /* never decrease fan level */
6033 do_set
= (fan_control_resume_level
> current_level
);
6035 case TPACPI_FAN_WR_ACPI_FANS
:
6036 case TPACPI_FAN_WR_TPEC
:
6037 /* never decrease fan level, scale is:
6038 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6040 * We expect the firmware to set either 7 or AUTO, but we
6041 * handle FULLSPEED out of paranoia.
6043 * So, we can safely only restore FULLSPEED or 7, anything
6044 * else could slow the fan. Restoring AUTO is useless, at
6045 * best that's exactly what the DSDT already set (it is the
6048 * Always keep in mind that the DSDT *will* have set the
6049 * fans to what the vendor supposes is the best level. We
6050 * muck with it only to speed the fan up.
6052 if (fan_control_resume_level
!= 7 &&
6053 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
6056 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
6057 (current_level
!= fan_control_resume_level
);
6063 printk(TPACPI_NOTICE
6064 "restoring fan level to 0x%02x\n",
6065 fan_control_resume_level
);
6066 rc
= fan_set_level_safe(fan_control_resume_level
);
6068 printk(TPACPI_NOTICE
6069 "failed to restore fan level: %d\n", rc
);
6073 static int fan_read(char *p
)
6078 unsigned int speed
= 0;
6080 switch (fan_status_access_mode
) {
6081 case TPACPI_FAN_RD_ACPI_GFAN
:
6082 /* 570, 600e/x, 770e, 770x */
6083 rc
= fan_get_status_safe(&status
);
6087 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
6089 (status
!= 0) ? "enabled" : "disabled", status
);
6092 case TPACPI_FAN_RD_TPEC
:
6093 /* all except 570, 600e/x, 770e, 770x */
6094 rc
= fan_get_status_safe(&status
);
6098 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
6099 if (status
!= fan_control_initial_status
)
6100 tp_features
.fan_ctrl_status_undef
= 0;
6102 /* Return most likely status. In fact, it
6103 * might be the only possible status */
6104 status
= TP_EC_FAN_AUTO
;
6107 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
6108 (status
!= 0) ? "enabled" : "disabled");
6110 rc
= fan_get_speed(&speed
);
6114 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
6116 if (status
& TP_EC_FAN_FULLSPEED
)
6117 /* Disengaged mode takes precedence */
6118 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
6119 else if (status
& TP_EC_FAN_AUTO
)
6120 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
6122 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
6125 case TPACPI_FAN_NONE
:
6127 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
6130 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
6131 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
6133 switch (fan_control_access_mode
) {
6134 case TPACPI_FAN_WR_ACPI_SFAN
:
6135 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
6139 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
6140 "auto, disengaged, full-speed)\n");
6145 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
6146 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
6147 "commands:\twatchdog <timeout> (<timeout> "
6148 "is 0 (off), 1-120 (seconds))\n");
6150 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
6151 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
6152 " (<speed> is 0-65535)\n");
6157 static int fan_write_cmd_level(const char *cmd
, int *rc
)
6161 if (strlencmp(cmd
, "level auto") == 0)
6162 level
= TP_EC_FAN_AUTO
;
6163 else if ((strlencmp(cmd
, "level disengaged") == 0) |
6164 (strlencmp(cmd
, "level full-speed") == 0))
6165 level
= TP_EC_FAN_FULLSPEED
;
6166 else if (sscanf(cmd
, "level %d", &level
) != 1)
6169 *rc
= fan_set_level_safe(level
);
6171 printk(TPACPI_ERR
"level command accepted for unsupported "
6172 "access mode %d", fan_control_access_mode
);
6177 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
6179 if (strlencmp(cmd
, "enable") != 0)
6182 *rc
= fan_set_enable();
6184 printk(TPACPI_ERR
"enable command accepted for unsupported "
6185 "access mode %d", fan_control_access_mode
);
6190 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
6192 if (strlencmp(cmd
, "disable") != 0)
6195 *rc
= fan_set_disable();
6197 printk(TPACPI_ERR
"disable command accepted for unsupported "
6198 "access mode %d", fan_control_access_mode
);
6203 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
6208 * Support speed <low> <medium> <high> ? */
6210 if (sscanf(cmd
, "speed %d", &speed
) != 1)
6213 *rc
= fan_set_speed(speed
);
6215 printk(TPACPI_ERR
"speed command accepted for unsupported "
6216 "access mode %d", fan_control_access_mode
);
6221 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
6225 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
6228 if (interval
< 0 || interval
> 120)
6231 fan_watchdog_maxinterval
= interval
;
6236 static int fan_write(char *buf
)
6241 while (!rc
&& (cmd
= next_cmd(&buf
))) {
6242 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
6243 fan_write_cmd_level(cmd
, &rc
)) &&
6244 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
6245 (fan_write_cmd_enable(cmd
, &rc
) ||
6246 fan_write_cmd_disable(cmd
, &rc
) ||
6247 fan_write_cmd_watchdog(cmd
, &rc
))) &&
6248 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
6249 fan_write_cmd_speed(cmd
, &rc
))
6253 fan_watchdog_reset();
6259 static struct ibm_struct fan_driver_data
= {
6264 .suspend
= fan_suspend
,
6265 .resume
= fan_resume
,
6268 /****************************************************************************
6269 ****************************************************************************
6273 ****************************************************************************
6274 ****************************************************************************/
6276 /* sysfs name ---------------------------------------------------------- */
6277 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
6278 struct device_attribute
*attr
,
6281 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
6284 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
6285 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
6287 /* --------------------------------------------------------------------- */
6290 static struct proc_dir_entry
*proc_dir
;
6293 * Module and infrastructure proble, init and exit handling
6296 static int force_load
;
6298 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6299 static const char * __init
str_supported(int is_supported
)
6301 static char text_unsupported
[] __initdata
= "not supported";
6303 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
6305 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6307 static void ibm_exit(struct ibm_struct
*ibm
)
6309 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
6311 list_del_init(&ibm
->all_drivers
);
6313 if (ibm
->flags
.acpi_notify_installed
) {
6314 dbg_printk(TPACPI_DBG_EXIT
,
6315 "%s: acpi_remove_notify_handler\n", ibm
->name
);
6317 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
6319 dispatch_acpi_notify
);
6320 ibm
->flags
.acpi_notify_installed
= 0;
6321 ibm
->flags
.acpi_notify_installed
= 0;
6324 if (ibm
->flags
.proc_created
) {
6325 dbg_printk(TPACPI_DBG_EXIT
,
6326 "%s: remove_proc_entry\n", ibm
->name
);
6327 remove_proc_entry(ibm
->name
, proc_dir
);
6328 ibm
->flags
.proc_created
= 0;
6331 if (ibm
->flags
.acpi_driver_registered
) {
6332 dbg_printk(TPACPI_DBG_EXIT
,
6333 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
6335 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
6336 kfree(ibm
->acpi
->driver
);
6337 ibm
->acpi
->driver
= NULL
;
6338 ibm
->flags
.acpi_driver_registered
= 0;
6341 if (ibm
->flags
.init_called
&& ibm
->exit
) {
6343 ibm
->flags
.init_called
= 0;
6346 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
6349 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
6352 struct ibm_struct
*ibm
= iibm
->data
;
6353 struct proc_dir_entry
*entry
;
6355 BUG_ON(ibm
== NULL
);
6357 INIT_LIST_HEAD(&ibm
->all_drivers
);
6359 if (ibm
->flags
.experimental
&& !experimental
)
6362 dbg_printk(TPACPI_DBG_INIT
,
6363 "probing for %s\n", ibm
->name
);
6366 ret
= iibm
->init(iibm
);
6368 return 0; /* probe failed */
6372 ibm
->flags
.init_called
= 1;
6376 if (ibm
->acpi
->hid
) {
6377 ret
= register_tpacpi_subdriver(ibm
);
6382 if (ibm
->acpi
->notify
) {
6383 ret
= setup_acpi_notify(ibm
);
6384 if (ret
== -ENODEV
) {
6385 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
6395 dbg_printk(TPACPI_DBG_INIT
,
6396 "%s installed\n", ibm
->name
);
6399 entry
= create_proc_entry(ibm
->name
,
6400 S_IFREG
| S_IRUGO
| S_IWUSR
,
6403 printk(TPACPI_ERR
"unable to create proc entry %s\n",
6408 entry
->owner
= THIS_MODULE
;
6410 entry
->read_proc
= &dispatch_procfs_read
;
6412 entry
->write_proc
= &dispatch_procfs_write
;
6413 ibm
->flags
.proc_created
= 1;
6416 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
6421 dbg_printk(TPACPI_DBG_INIT
,
6422 "%s: at error exit path with result %d\n",
6426 return (ret
< 0)? ret
: 0;
6431 /* returns 0 - probe ok, or < 0 - probe error.
6432 * Probe ok doesn't mean thinkpad found.
6433 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6434 static int __must_check __init
get_thinkpad_model_data(
6435 struct thinkpad_id_data
*tp
)
6437 const struct dmi_device
*dev
= NULL
;
6438 char ec_fw_string
[18];
6444 memset(tp
, 0, sizeof(*tp
));
6446 if (dmi_name_in_vendors("IBM"))
6447 tp
->vendor
= PCI_VENDOR_ID_IBM
;
6448 else if (dmi_name_in_vendors("LENOVO"))
6449 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
6453 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
6454 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
6455 if (s
&& !tp
->bios_version_str
)
6457 if (!tp
->bios_version_str
)
6459 tp
->bios_model
= tp
->bios_version_str
[0]
6460 | (tp
->bios_version_str
[1] << 8);
6463 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6464 * X32 or newer, all Z series; Some models must have an
6465 * up-to-date BIOS or they will not be detected.
6467 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6469 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
6470 if (sscanf(dev
->name
,
6471 "IBM ThinkPad Embedded Controller -[%17c",
6472 ec_fw_string
) == 1) {
6473 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
6474 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
6476 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
6477 if (!tp
->ec_version_str
)
6479 tp
->ec_model
= ec_fw_string
[0]
6480 | (ec_fw_string
[1] << 8);
6485 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
6486 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
6487 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
6492 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
6493 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
6494 if (s
&& !tp
->nummodel_str
)
6500 static int __init
probe_for_thinkpad(void)
6508 * Non-ancient models have better DMI tagging, but very old models
6511 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
6513 /* ec is required because many other handles are relative to it */
6514 TPACPI_ACPIHANDLE_INIT(ec
);
6518 "Not yet supported ThinkPad detected!\n");
6523 * Risks a regression on very old machines, but reduces potential
6524 * false positives a damn great deal
6527 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
6529 if (!is_thinkpad
&& !force_load
)
6536 /* Module init, exit, parameters */
6538 static struct ibm_init_struct ibms_init
[] __initdata
= {
6540 .init
= thinkpad_acpi_driver_init
,
6541 .data
= &thinkpad_acpi_driver_data
,
6544 .init
= hotkey_init
,
6545 .data
= &hotkey_driver_data
,
6548 .init
= bluetooth_init
,
6549 .data
= &bluetooth_driver_data
,
6553 .data
= &wan_driver_data
,
6555 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6558 .data
= &video_driver_data
,
6563 .data
= &light_driver_data
,
6565 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6568 .data
= &dock_driver_data
[0],
6572 .data
= &dock_driver_data
[1],
6575 #ifdef CONFIG_THINKPAD_ACPI_BAY
6578 .data
= &bay_driver_data
,
6583 .data
= &cmos_driver_data
,
6587 .data
= &led_driver_data
,
6591 .data
= &beep_driver_data
,
6594 .init
= thermal_init
,
6595 .data
= &thermal_driver_data
,
6598 .data
= &ecdump_driver_data
,
6601 .init
= brightness_init
,
6602 .data
= &brightness_driver_data
,
6605 .data
= &volume_driver_data
,
6609 .data
= &fan_driver_data
,
6613 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
6616 struct ibm_struct
*ibm
;
6618 if (!kp
|| !kp
->name
|| !val
)
6621 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
6622 ibm
= ibms_init
[i
].data
;
6623 WARN_ON(ibm
== NULL
);
6625 if (!ibm
|| !ibm
->name
)
6628 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
6629 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
6631 strcpy(ibms_init
[i
].param
, val
);
6632 strcat(ibms_init
[i
].param
, ",");
6640 module_param(experimental
, int, 0);
6641 MODULE_PARM_DESC(experimental
,
6642 "Enables experimental features when non-zero");
6644 module_param_named(debug
, dbg_level
, uint
, 0);
6645 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
6647 module_param(force_load
, bool, 0);
6648 MODULE_PARM_DESC(force_load
,
6649 "Attempts to load the driver even on a "
6650 "mis-identified ThinkPad when true");
6652 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
6653 MODULE_PARM_DESC(fan_control
,
6654 "Enables setting fan parameters features when true");
6656 module_param_named(brightness_mode
, brightness_mode
, int, 0);
6657 MODULE_PARM_DESC(brightness_mode
,
6658 "Selects brightness control strategy: "
6659 "0=auto, 1=EC, 2=CMOS, 3=both");
6661 module_param(brightness_enable
, uint
, 0);
6662 MODULE_PARM_DESC(brightness_enable
,
6663 "Enables backlight control when 1, disables when 0");
6665 module_param(hotkey_report_mode
, uint
, 0);
6666 MODULE_PARM_DESC(hotkey_report_mode
,
6667 "used for backwards compatibility with userspace, "
6668 "see documentation");
6670 #define TPACPI_PARAM(feature) \
6671 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6672 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6673 "at module load, see documentation")
6675 TPACPI_PARAM(hotkey
);
6676 TPACPI_PARAM(bluetooth
);
6677 TPACPI_PARAM(video
);
6678 TPACPI_PARAM(light
);
6679 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6682 #ifdef CONFIG_THINKPAD_ACPI_BAY
6684 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6688 TPACPI_PARAM(ecdump
);
6689 TPACPI_PARAM(brightness
);
6690 TPACPI_PARAM(volume
);
6693 static void thinkpad_acpi_module_exit(void)
6695 struct ibm_struct
*ibm
, *itmp
;
6697 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
6699 list_for_each_entry_safe_reverse(ibm
, itmp
,
6700 &tpacpi_all_drivers
,
6705 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
6707 if (tpacpi_inputdev
) {
6708 if (tp_features
.input_device_registered
)
6709 input_unregister_device(tpacpi_inputdev
);
6711 input_free_device(tpacpi_inputdev
);
6715 hwmon_device_unregister(tpacpi_hwmon
);
6717 if (tp_features
.sensors_pdev_attrs_registered
)
6718 device_remove_file(&tpacpi_sensors_pdev
->dev
,
6719 &dev_attr_thinkpad_acpi_pdev_name
);
6720 if (tpacpi_sensors_pdev
)
6721 platform_device_unregister(tpacpi_sensors_pdev
);
6723 platform_device_unregister(tpacpi_pdev
);
6725 if (tp_features
.sensors_pdrv_attrs_registered
)
6726 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
6727 if (tp_features
.platform_drv_attrs_registered
)
6728 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
6730 if (tp_features
.sensors_pdrv_registered
)
6731 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
6733 if (tp_features
.platform_drv_registered
)
6734 platform_driver_unregister(&tpacpi_pdriver
);
6737 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
6740 destroy_workqueue(tpacpi_wq
);
6742 kfree(thinkpad_id
.bios_version_str
);
6743 kfree(thinkpad_id
.ec_version_str
);
6744 kfree(thinkpad_id
.model_str
);
6748 static int __init
thinkpad_acpi_module_init(void)
6752 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
6754 /* Parameter checking */
6755 if (hotkey_report_mode
> 2)
6758 /* Driver-level probe */
6760 ret
= get_thinkpad_model_data(&thinkpad_id
);
6763 "unable to get DMI data: %d\n", ret
);
6764 thinkpad_acpi_module_exit();
6767 ret
= probe_for_thinkpad();
6769 thinkpad_acpi_module_exit();
6773 /* Driver initialization */
6775 TPACPI_ACPIHANDLE_INIT(ecrd
);
6776 TPACPI_ACPIHANDLE_INIT(ecwr
);
6778 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
6780 thinkpad_acpi_module_exit();
6784 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
6787 "unable to create proc dir " TPACPI_PROC_DIR
);
6788 thinkpad_acpi_module_exit();
6791 proc_dir
->owner
= THIS_MODULE
;
6793 ret
= platform_driver_register(&tpacpi_pdriver
);
6796 "unable to register main platform driver\n");
6797 thinkpad_acpi_module_exit();
6800 tp_features
.platform_drv_registered
= 1;
6802 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
6805 "unable to register hwmon platform driver\n");
6806 thinkpad_acpi_module_exit();
6809 tp_features
.sensors_pdrv_registered
= 1;
6811 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
6813 tp_features
.platform_drv_attrs_registered
= 1;
6814 ret
= tpacpi_create_driver_attributes(
6815 &tpacpi_hwmon_pdriver
.driver
);
6819 "unable to create sysfs driver attributes\n");
6820 thinkpad_acpi_module_exit();
6823 tp_features
.sensors_pdrv_attrs_registered
= 1;
6826 /* Device initialization */
6827 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
6829 if (IS_ERR(tpacpi_pdev
)) {
6830 ret
= PTR_ERR(tpacpi_pdev
);
6832 printk(TPACPI_ERR
"unable to register platform device\n");
6833 thinkpad_acpi_module_exit();
6836 tpacpi_sensors_pdev
= platform_device_register_simple(
6837 TPACPI_HWMON_DRVR_NAME
,
6839 if (IS_ERR(tpacpi_sensors_pdev
)) {
6840 ret
= PTR_ERR(tpacpi_sensors_pdev
);
6841 tpacpi_sensors_pdev
= NULL
;
6843 "unable to register hwmon platform device\n");
6844 thinkpad_acpi_module_exit();
6847 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
6848 &dev_attr_thinkpad_acpi_pdev_name
);
6851 "unable to create sysfs hwmon device attributes\n");
6852 thinkpad_acpi_module_exit();
6855 tp_features
.sensors_pdev_attrs_registered
= 1;
6856 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
6857 if (IS_ERR(tpacpi_hwmon
)) {
6858 ret
= PTR_ERR(tpacpi_hwmon
);
6859 tpacpi_hwmon
= NULL
;
6860 printk(TPACPI_ERR
"unable to register hwmon device\n");
6861 thinkpad_acpi_module_exit();
6864 mutex_init(&tpacpi_inputdev_send_mutex
);
6865 tpacpi_inputdev
= input_allocate_device();
6866 if (!tpacpi_inputdev
) {
6867 printk(TPACPI_ERR
"unable to allocate input device\n");
6868 thinkpad_acpi_module_exit();
6871 /* Prepare input device, but don't register */
6872 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
6873 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
6874 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
6875 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
6876 thinkpad_id
.vendor
:
6878 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
6879 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
6881 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
6882 ret
= ibm_init(&ibms_init
[i
]);
6883 if (ret
>= 0 && *ibms_init
[i
].param
)
6884 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
6886 thinkpad_acpi_module_exit();
6890 ret
= input_register_device(tpacpi_inputdev
);
6892 printk(TPACPI_ERR
"unable to register input device\n");
6893 thinkpad_acpi_module_exit();
6896 tp_features
.input_device_registered
= 1;
6899 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
6903 /* Please remove this in year 2009 */
6904 MODULE_ALIAS("ibm_acpi");
6906 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
6909 * DMI matching for module autoloading
6911 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6912 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6914 * Only models listed in thinkwiki will be supported, so add yours
6915 * if it is not there yet.
6917 #define IBM_BIOS_MODULE_ALIAS(__type) \
6918 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
6920 /* Non-ancient thinkpads */
6921 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6922 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6924 /* Ancient thinkpad BIOSes have to be identified by
6925 * BIOS type or model number, and there are far less
6926 * BIOS types than model numbers... */
6927 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
6928 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
6929 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
6931 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6932 MODULE_DESCRIPTION(TPACPI_DESC
);
6933 MODULE_VERSION(TPACPI_VERSION
);
6934 MODULE_LICENSE("GPL");
6936 module_init(thinkpad_acpi_module_init
);
6937 module_exit(thinkpad_acpi_module_exit
);