2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #define TPACPI_VERSION "0.22"
25 #define TPACPI_SYSFS_VERSION 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/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2
= 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT
= 0x58,
97 TP_NVRAM_ADDR_VIDEO
= 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS
= 0x5e,
99 TP_NVRAM_ADDR_MIXER
= 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD
= 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM
= 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY
= 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE
= 0x80,
108 TP_NVRAM_MASK_THINKLIGHT
= 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND
= 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS
= 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS
= 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS
= 0,
113 TP_NVRAM_MASK_MUTE
= 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME
= 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME
= 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME
= 0,
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
128 TP_ACPI_WGSV_GET_STATE
= 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME
= 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
= 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE
= 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
136 TP_ACPI_WGSV_STATE_WWANEXIST
= 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR
= 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES
= 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF
= 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST
= 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR
= 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES
= 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF
= 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST
= 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR
= 0x0020, /* UWB radio enabled */
148 /****************************************************************************
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
171 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
172 TPACPI_RFK_WWAN_SW_ID
,
173 TPACPI_RFK_UWB_SW_ID
,
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
193 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
194 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
195 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
198 /****************************************************************************
199 * Driver-wide structs and misc. variables
204 struct tp_acpi_drv_struct
{
205 const struct acpi_device_id
*hid
;
206 struct acpi_driver
*driver
;
208 void (*notify
) (struct ibm_struct
*, u32
);
211 struct acpi_device
*device
;
217 int (*read
) (char *);
218 int (*write
) (char *);
220 void (*resume
) (void);
221 void (*suspend
) (pm_message_t state
);
222 void (*shutdown
) (void);
224 struct list_head all_drivers
;
226 struct tp_acpi_drv_struct
*acpi
;
229 u8 acpi_driver_registered
:1;
230 u8 acpi_notify_installed
:1;
237 struct ibm_init_struct
{
240 int (*init
) (struct ibm_init_struct
*);
241 struct ibm_struct
*data
;
245 #ifdef CONFIG_THINKPAD_ACPI_BAY
258 u32 bright_16levels
:1;
259 u32 bright_acpimode
:1;
262 u32 fan_ctrl_status_undef
:1;
263 u32 input_device_registered
:1;
264 u32 platform_drv_registered
:1;
265 u32 platform_drv_attrs_registered
:1;
266 u32 sensors_pdrv_registered
:1;
267 u32 sensors_pdrv_attrs_registered
:1;
268 u32 sensors_pdev_attrs_registered
:1;
269 u32 hotkey_poll_active
:1;
273 u16 hotkey_mask_ff
:1;
274 u16 bright_cmos_ec_unsync
:1;
277 struct thinkpad_id_data
{
278 unsigned int vendor
; /* ThinkPad vendor:
279 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
281 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
282 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
284 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
287 char *model_str
; /* ThinkPad T43 */
288 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
290 static struct thinkpad_id_data thinkpad_id
;
293 TPACPI_LIFE_INIT
= 0,
298 static int experimental
;
299 static u32 dbg_level
;
301 static struct workqueue_struct
*tpacpi_wq
;
309 /* Special LED class that can defer work */
310 struct tpacpi_led_classdev
{
311 struct led_classdev led_classdev
;
312 struct work_struct work
;
313 enum led_status_t new_state
;
317 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
318 static int dbg_wlswemul
;
319 static int tpacpi_wlsw_emulstate
;
320 static int dbg_bluetoothemul
;
321 static int tpacpi_bluetooth_emulstate
;
322 static int dbg_wwanemul
;
323 static int tpacpi_wwan_emulstate
;
324 static int dbg_uwbemul
;
325 static int tpacpi_uwb_emulstate
;
329 /*************************************************************************
333 #define dbg_printk(a_dbg_level, format, arg...) \
334 do { if (dbg_level & (a_dbg_level)) \
335 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
338 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
339 #define vdbg_printk dbg_printk
340 static const char *str_supported(int is_supported
);
342 #define vdbg_printk(a_dbg_level, format, arg...) \
346 static void tpacpi_log_usertask(const char * const what
)
348 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
349 what
, task_tgid_vnr(current
));
352 #define tpacpi_disclose_usertask(what, format, arg...) \
355 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
356 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
357 printk(TPACPI_DEBUG "%s: PID %d: " format, \
358 what, task_tgid_vnr(current), ## arg); \
362 /****************************************************************************
363 ****************************************************************************
365 * ACPI Helpers and device model
367 ****************************************************************************
368 ****************************************************************************/
370 /*************************************************************************
374 static acpi_handle root_handle
;
376 #define TPACPI_HANDLE(object, parent, paths...) \
377 static acpi_handle object##_handle; \
378 static acpi_handle *object##_parent = &parent##_handle; \
379 static char *object##_path; \
380 static char *object##_paths[] = { paths }
382 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
383 "\\_SB.PCI.ISA.EC", /* 570 */
384 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
385 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
386 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
387 "\\_SB.PCI0.ICH3.EC0", /* R31 */
388 "\\_SB.PCI0.LPC.EC", /* all others */
391 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
392 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
394 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
396 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
397 "\\CMS", /* R40, R40e */
400 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
401 "^HKEY", /* R30, R31 */
402 "HKEY", /* all others */
405 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
406 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
407 "\\_SB.PCI0.VID0", /* 770e */
408 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
409 "\\_SB.PCI0.AGP.VID", /* all others */
413 /*************************************************************************
417 static int acpi_evalf(acpi_handle handle
,
418 void *res
, char *method
, char *fmt
, ...)
421 struct acpi_object_list params
;
422 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
423 struct acpi_buffer result
, *resultp
;
424 union acpi_object out_obj
;
432 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
445 params
.pointer
= &in_objs
[0];
452 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
453 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
455 /* add more types as needed */
457 printk(TPACPI_ERR
"acpi_evalf() called "
458 "with invalid format character '%c'\n", c
);
464 if (res_type
!= 'v') {
465 result
.length
= sizeof(out_obj
);
466 result
.pointer
= &out_obj
;
471 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
476 *(int *)res
= out_obj
.integer
.value
;
477 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
480 success
= status
== AE_OK
;
482 /* add more types as needed */
484 printk(TPACPI_ERR
"acpi_evalf() called "
485 "with invalid format character '%c'\n", res_type
);
489 if (!success
&& !quiet
)
490 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
491 method
, fmt0
, status
);
496 static int acpi_ec_read(int i
, u8
*p
)
501 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
505 if (ec_read(i
, p
) < 0)
512 static int acpi_ec_write(int i
, u8 v
)
515 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
518 if (ec_write(i
, v
) < 0)
525 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
526 static int _sta(acpi_handle handle
)
530 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
537 static int issue_thinkpad_cmos_command(int cmos_cmd
)
542 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
548 /*************************************************************************
552 #define TPACPI_ACPIHANDLE_INIT(object) \
553 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
554 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
556 static void drv_acpi_handle_init(char *name
,
557 acpi_handle
*handle
, acpi_handle parent
,
558 char **paths
, int num_paths
, char **path
)
563 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
566 for (i
= 0; i
< num_paths
; i
++) {
567 status
= acpi_get_handle(parent
, paths
[i
], handle
);
568 if (ACPI_SUCCESS(status
)) {
570 dbg_printk(TPACPI_DBG_INIT
,
571 "Found ACPI handle %s for %s\n",
577 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
582 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
584 struct ibm_struct
*ibm
= data
;
586 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
589 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
592 ibm
->acpi
->notify(ibm
, event
);
595 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
602 if (!*ibm
->acpi
->handle
)
605 vdbg_printk(TPACPI_DBG_INIT
,
606 "setting up ACPI notify for %s\n", ibm
->name
);
608 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
610 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
615 acpi_driver_data(ibm
->acpi
->device
) = ibm
;
616 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
617 TPACPI_ACPI_EVENT_PREFIX
,
620 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
621 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
622 if (ACPI_FAILURE(status
)) {
623 if (status
== AE_ALREADY_EXISTS
) {
625 "another device driver is already "
626 "handling %s events\n", ibm
->name
);
629 "acpi_install_notify_handler(%s) failed: %d\n",
634 ibm
->flags
.acpi_notify_installed
= 1;
638 static int __init
tpacpi_device_add(struct acpi_device
*device
)
643 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
647 dbg_printk(TPACPI_DBG_INIT
,
648 "registering %s as an ACPI driver\n", ibm
->name
);
652 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
653 if (!ibm
->acpi
->driver
) {
655 "failed to allocate memory for ibm->acpi->driver\n");
659 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
660 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
662 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
664 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
666 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
668 kfree(ibm
->acpi
->driver
);
669 ibm
->acpi
->driver
= NULL
;
671 ibm
->flags
.acpi_driver_registered
= 1;
677 /****************************************************************************
678 ****************************************************************************
682 ****************************************************************************
683 ****************************************************************************/
685 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
686 int count
, int *eof
, void *data
)
688 struct ibm_struct
*ibm
= data
;
691 if (!ibm
|| !ibm
->read
)
694 len
= ibm
->read(page
);
698 if (len
<= off
+ count
)
710 static int dispatch_procfs_write(struct file
*file
,
711 const char __user
*userbuf
,
712 unsigned long count
, void *data
)
714 struct ibm_struct
*ibm
= data
;
718 if (!ibm
|| !ibm
->write
)
721 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
725 if (copy_from_user(kernbuf
, userbuf
, count
)) {
731 strcat(kernbuf
, ",");
732 ret
= ibm
->write(kernbuf
);
741 static char *next_cmd(char **cmds
)
746 while ((end
= strchr(start
, ',')) && end
== start
)
758 /****************************************************************************
759 ****************************************************************************
761 * Device model: input, hwmon and platform
763 ****************************************************************************
764 ****************************************************************************/
766 static struct platform_device
*tpacpi_pdev
;
767 static struct platform_device
*tpacpi_sensors_pdev
;
768 static struct device
*tpacpi_hwmon
;
769 static struct input_dev
*tpacpi_inputdev
;
770 static struct mutex tpacpi_inputdev_send_mutex
;
771 static LIST_HEAD(tpacpi_all_drivers
);
773 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
776 struct ibm_struct
*ibm
, *itmp
;
778 list_for_each_entry_safe(ibm
, itmp
,
782 (ibm
->suspend
)(state
);
788 static int tpacpi_resume_handler(struct platform_device
*pdev
)
790 struct ibm_struct
*ibm
, *itmp
;
792 list_for_each_entry_safe(ibm
, itmp
,
802 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
804 struct ibm_struct
*ibm
, *itmp
;
806 list_for_each_entry_safe(ibm
, itmp
,
814 static struct platform_driver tpacpi_pdriver
= {
816 .name
= TPACPI_DRVR_NAME
,
817 .owner
= THIS_MODULE
,
819 .suspend
= tpacpi_suspend_handler
,
820 .resume
= tpacpi_resume_handler
,
821 .shutdown
= tpacpi_shutdown_handler
,
824 static struct platform_driver tpacpi_hwmon_pdriver
= {
826 .name
= TPACPI_HWMON_DRVR_NAME
,
827 .owner
= THIS_MODULE
,
831 /*************************************************************************
832 * sysfs support helpers
835 struct attribute_set
{
836 unsigned int members
, max_members
;
837 struct attribute_group group
;
840 struct attribute_set_obj
{
841 struct attribute_set s
;
843 } __attribute__((packed
));
845 static struct attribute_set
*create_attr_set(unsigned int max_members
,
848 struct attribute_set_obj
*sobj
;
850 if (max_members
== 0)
853 /* Allocates space for implicit NULL at the end too */
854 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
855 max_members
* sizeof(struct attribute
*),
859 sobj
->s
.max_members
= max_members
;
860 sobj
->s
.group
.attrs
= &sobj
->a
;
861 sobj
->s
.group
.name
= name
;
866 #define destroy_attr_set(_set) \
869 /* not multi-threaded safe, use it in a single thread per set */
870 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
875 if (s
->members
>= s
->max_members
)
878 s
->group
.attrs
[s
->members
] = attr
;
884 static int add_many_to_attr_set(struct attribute_set
*s
,
885 struct attribute
**attr
,
890 for (i
= 0; i
< count
; i
++) {
891 res
= add_to_attr_set(s
, attr
[i
]);
899 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
901 sysfs_remove_group(kobj
, &s
->group
);
905 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
906 sysfs_create_group(_kobj, &_attr_set->group)
908 static int parse_strtoul(const char *buf
,
909 unsigned long max
, unsigned long *value
)
913 while (*buf
&& isspace(*buf
))
915 *value
= simple_strtoul(buf
, &endp
, 0);
916 while (*endp
&& isspace(*endp
))
918 if (*endp
|| *value
> max
)
924 static void tpacpi_disable_brightness_delay(void)
926 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
928 "ACPI backlight control delay disabled\n");
931 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
933 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
934 union acpi_object
*obj
;
937 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
938 obj
= (union acpi_object
*)buffer
.pointer
;
939 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
940 printk(TPACPI_ERR
"Unknown _BCL data, "
941 "please report this to %s\n", TPACPI_MAIL
);
944 rc
= obj
->package
.count
;
950 kfree(buffer
.pointer
);
954 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
955 u32 lvl
, void *context
, void **rv
)
957 char name
[ACPI_PATH_SEGMENT_LENGTH
];
958 struct acpi_buffer buffer
= { sizeof(name
), &name
};
960 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
961 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
963 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
964 return AE_CTRL_TERMINATE
;
971 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
973 static int __init
tpacpi_check_std_acpi_brightness_support(void)
977 void *bcl_ptr
= &bcl_levels
;
980 TPACPI_ACPIHANDLE_INIT(vid
);
986 * Search for a _BCL method, and execute it. This is safe on all
987 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
988 * BIOS in ACPI backlight control mode. We do NOT have to care
989 * about calling the _BCL method in an enabled video device, any
990 * will do for our purposes.
993 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
994 tpacpi_acpi_walk_find_bcl
, NULL
,
997 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
998 tp_features
.bright_acpimode
= 1;
999 return (bcl_levels
- 2);
1005 static int __init
tpacpi_new_rfkill(const unsigned int id
,
1006 struct rfkill
**rfk
,
1007 const enum rfkill_type rfktype
,
1009 const bool set_default
,
1010 int (*toggle_radio
)(void *, enum rfkill_state
),
1011 int (*get_state
)(void *, enum rfkill_state
*))
1014 enum rfkill_state initial_state
= RFKILL_STATE_SOFT_BLOCKED
;
1016 res
= get_state(NULL
, &initial_state
);
1019 "failed to read initial state for %s, error %d; "
1020 "will turn radio off\n", name
, res
);
1021 } else if (set_default
) {
1022 /* try to set the initial state as the default for the rfkill
1023 * type, since we ask the firmware to preserve it across S5 in
1025 rfkill_set_default(rfktype
,
1026 (initial_state
== RFKILL_STATE_UNBLOCKED
) ?
1027 RFKILL_STATE_UNBLOCKED
:
1028 RFKILL_STATE_SOFT_BLOCKED
);
1031 *rfk
= rfkill_allocate(&tpacpi_pdev
->dev
, rfktype
);
1034 "failed to allocate memory for rfkill class\n");
1038 (*rfk
)->name
= name
;
1039 (*rfk
)->get_state
= get_state
;
1040 (*rfk
)->toggle_radio
= toggle_radio
;
1041 (*rfk
)->state
= initial_state
;
1043 res
= rfkill_register(*rfk
);
1046 "failed to register %s rfkill switch: %d\n",
1056 static void printk_deprecated_attribute(const char * const what
,
1057 const char * const details
)
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1065 static void printk_deprecated_rfkill_attribute(const char * const what
)
1067 printk_deprecated_attribute(what
,
1068 "Please switch to generic rfkill before year 2010");
1071 /*************************************************************************
1072 * thinkpad-acpi driver attributes
1075 /* interface_version --------------------------------------------------- */
1076 static ssize_t
tpacpi_driver_interface_version_show(
1077 struct device_driver
*drv
,
1080 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1083 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1084 tpacpi_driver_interface_version_show
, NULL
);
1086 /* debug_level --------------------------------------------------------- */
1087 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1090 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1093 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1094 const char *buf
, size_t count
)
1098 if (parse_strtoul(buf
, 0xffff, &t
))
1106 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1107 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1109 /* version ------------------------------------------------------------- */
1110 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1113 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1114 TPACPI_DESC
, TPACPI_VERSION
);
1117 static DRIVER_ATTR(version
, S_IRUGO
,
1118 tpacpi_driver_version_show
, NULL
);
1120 /* --------------------------------------------------------------------- */
1122 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1124 static void tpacpi_send_radiosw_update(void);
1126 /* wlsw_emulstate ------------------------------------------------------ */
1127 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1130 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1133 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1134 const char *buf
, size_t count
)
1138 if (parse_strtoul(buf
, 1, &t
))
1141 if (tpacpi_wlsw_emulstate
!= t
) {
1142 tpacpi_wlsw_emulstate
= !!t
;
1143 tpacpi_send_radiosw_update();
1145 tpacpi_wlsw_emulstate
= !!t
;
1150 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1151 tpacpi_driver_wlsw_emulstate_show
,
1152 tpacpi_driver_wlsw_emulstate_store
);
1154 /* bluetooth_emulstate ------------------------------------------------- */
1155 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1156 struct device_driver
*drv
,
1159 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1162 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1163 struct device_driver
*drv
,
1164 const char *buf
, size_t count
)
1168 if (parse_strtoul(buf
, 1, &t
))
1171 tpacpi_bluetooth_emulstate
= !!t
;
1176 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1177 tpacpi_driver_bluetooth_emulstate_show
,
1178 tpacpi_driver_bluetooth_emulstate_store
);
1180 /* wwan_emulstate ------------------------------------------------- */
1181 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1182 struct device_driver
*drv
,
1185 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1188 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1189 struct device_driver
*drv
,
1190 const char *buf
, size_t count
)
1194 if (parse_strtoul(buf
, 1, &t
))
1197 tpacpi_wwan_emulstate
= !!t
;
1202 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1203 tpacpi_driver_wwan_emulstate_show
,
1204 tpacpi_driver_wwan_emulstate_store
);
1206 /* uwb_emulstate ------------------------------------------------- */
1207 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1208 struct device_driver
*drv
,
1211 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1214 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1215 struct device_driver
*drv
,
1216 const char *buf
, size_t count
)
1220 if (parse_strtoul(buf
, 1, &t
))
1223 tpacpi_uwb_emulstate
= !!t
;
1228 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1229 tpacpi_driver_uwb_emulstate_show
,
1230 tpacpi_driver_uwb_emulstate_store
);
1233 /* --------------------------------------------------------------------- */
1235 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1236 &driver_attr_debug_level
, &driver_attr_version
,
1237 &driver_attr_interface_version
,
1240 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1246 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1247 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1251 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1252 if (!res
&& dbg_wlswemul
)
1253 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1254 if (!res
&& dbg_bluetoothemul
)
1255 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1256 if (!res
&& dbg_wwanemul
)
1257 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1258 if (!res
&& dbg_uwbemul
)
1259 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1265 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1269 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1270 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1272 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1273 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1274 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1275 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1276 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1280 /****************************************************************************
1281 ****************************************************************************
1285 ****************************************************************************
1286 ****************************************************************************/
1288 /*************************************************************************
1289 * thinkpad-acpi init subdriver
1292 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1294 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1295 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1297 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1298 (thinkpad_id
.bios_version_str
) ?
1299 thinkpad_id
.bios_version_str
: "unknown",
1300 (thinkpad_id
.ec_version_str
) ?
1301 thinkpad_id
.ec_version_str
: "unknown");
1303 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1304 printk(TPACPI_INFO
"%s %s, model %s\n",
1305 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1306 "IBM" : ((thinkpad_id
.vendor
==
1307 PCI_VENDOR_ID_LENOVO
) ?
1308 "Lenovo" : "Unknown vendor"),
1309 thinkpad_id
.model_str
,
1310 (thinkpad_id
.nummodel_str
) ?
1311 thinkpad_id
.nummodel_str
: "unknown");
1316 static int thinkpad_acpi_driver_read(char *p
)
1320 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1321 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1326 static struct ibm_struct thinkpad_acpi_driver_data
= {
1328 .read
= thinkpad_acpi_driver_read
,
1331 /*************************************************************************
1335 enum { /* hot key scan codes (derived from ACPI DSDT) */
1336 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1337 TP_ACPI_HOTKEYSCAN_FNF2
,
1338 TP_ACPI_HOTKEYSCAN_FNF3
,
1339 TP_ACPI_HOTKEYSCAN_FNF4
,
1340 TP_ACPI_HOTKEYSCAN_FNF5
,
1341 TP_ACPI_HOTKEYSCAN_FNF6
,
1342 TP_ACPI_HOTKEYSCAN_FNF7
,
1343 TP_ACPI_HOTKEYSCAN_FNF8
,
1344 TP_ACPI_HOTKEYSCAN_FNF9
,
1345 TP_ACPI_HOTKEYSCAN_FNF10
,
1346 TP_ACPI_HOTKEYSCAN_FNF11
,
1347 TP_ACPI_HOTKEYSCAN_FNF12
,
1348 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1349 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1350 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1351 TP_ACPI_HOTKEYSCAN_FNHOME
,
1352 TP_ACPI_HOTKEYSCAN_FNEND
,
1353 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1354 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1355 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1356 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1357 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1358 TP_ACPI_HOTKEYSCAN_MUTE
,
1359 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1362 enum { /* Keys available through NVRAM polling */
1363 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1364 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1367 enum { /* Positions of some of the keys in hotkey masks */
1368 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1369 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1370 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1371 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1372 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1373 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1374 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1375 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1376 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1377 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1378 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1381 enum { /* NVRAM to ACPI HKEY group map */
1382 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1383 TP_ACPI_HKEY_ZOOM_MASK
|
1384 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1385 TP_ACPI_HKEY_HIBERNATE_MASK
,
1386 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1387 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1388 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1389 TP_ACPI_HKEY_VOLDWN_MASK
|
1390 TP_ACPI_HKEY_MUTE_MASK
,
1393 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1394 struct tp_nvram_state
{
1395 u16 thinkpad_toggle
:1;
1397 u16 display_toggle
:1;
1398 u16 thinklight_toggle
:1;
1399 u16 hibernate_toggle
:1;
1400 u16 displayexp_toggle
:1;
1401 u16 display_state
:1;
1402 u16 brightness_toggle
:1;
1403 u16 volume_toggle
:1;
1406 u8 brightness_level
;
1410 static struct task_struct
*tpacpi_hotkey_task
;
1411 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1412 static int hotkey_poll_freq
= 10; /* Hz */
1413 static struct mutex hotkey_thread_mutex
;
1414 static struct mutex hotkey_thread_data_mutex
;
1415 static unsigned int hotkey_config_change
;
1417 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1419 #define hotkey_source_mask 0U
1421 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1423 static struct mutex hotkey_mutex
;
1425 static enum { /* Reasons for waking up */
1426 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1427 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1428 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1429 } hotkey_wakeup_reason
;
1431 static int hotkey_autosleep_ack
;
1433 static int hotkey_orig_status
;
1434 static u32 hotkey_orig_mask
;
1435 static u32 hotkey_all_mask
;
1436 static u32 hotkey_reserved_mask
;
1437 static u32 hotkey_mask
;
1439 static unsigned int hotkey_report_mode
;
1441 static u16
*hotkey_keycode_map
;
1443 static struct attribute_set
*hotkey_dev_attributes
;
1445 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1446 #define HOTKEY_CONFIG_CRITICAL_START \
1448 mutex_lock(&hotkey_thread_data_mutex); \
1449 hotkey_config_change++; \
1451 #define HOTKEY_CONFIG_CRITICAL_END \
1452 mutex_unlock(&hotkey_thread_data_mutex);
1454 #define HOTKEY_CONFIG_CRITICAL_START
1455 #define HOTKEY_CONFIG_CRITICAL_END
1456 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1458 /* HKEY.MHKG() return bits */
1459 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1461 static int hotkey_get_wlsw(int *status
)
1463 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1465 *status
= !!tpacpi_wlsw_emulstate
;
1469 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1474 static int hotkey_get_tablet_mode(int *status
)
1478 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1481 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1486 * Call with hotkey_mutex held
1488 static int hotkey_mask_get(void)
1492 if (tp_features
.hotkey_mask
) {
1493 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1496 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1502 * Call with hotkey_mutex held
1504 static int hotkey_mask_set(u32 mask
)
1509 if (tp_features
.hotkey_mask
) {
1510 if (!tp_warned
.hotkey_mask_ff
&&
1511 (mask
== 0xffff || mask
== 0xffffff ||
1512 mask
== 0xffffffff)) {
1513 tp_warned
.hotkey_mask_ff
= 1;
1514 printk(TPACPI_NOTICE
1515 "setting the hotkey mask to 0x%08x is likely "
1516 "not the best way to go about it\n", mask
);
1517 printk(TPACPI_NOTICE
1518 "please consider using the driver defaults, "
1519 "and refer to up-to-date thinkpad-acpi "
1523 HOTKEY_CONFIG_CRITICAL_START
1524 for (i
= 0; i
< 32; i
++) {
1526 /* enable in firmware mask only keys not in NVRAM
1527 * mode, but enable the key in the cached hotkey_mask
1528 * regardless of mode, or the key will end up
1529 * disabled by hotkey_mask_get() */
1530 if (!acpi_evalf(hkey_handle
,
1531 NULL
, "MHKM", "vdd", i
+ 1,
1532 !!((mask
& ~hotkey_source_mask
) & m
))) {
1536 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1539 HOTKEY_CONFIG_CRITICAL_END
1541 /* hotkey_mask_get must be called unconditionally below */
1542 if (!hotkey_mask_get() && !rc
&&
1543 (hotkey_mask
& ~hotkey_source_mask
) !=
1544 (mask
& ~hotkey_source_mask
)) {
1545 printk(TPACPI_NOTICE
1546 "requested hot key mask 0x%08x, but "
1547 "firmware forced it to 0x%08x\n",
1551 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1552 HOTKEY_CONFIG_CRITICAL_START
1553 hotkey_mask
= mask
& hotkey_source_mask
;
1554 HOTKEY_CONFIG_CRITICAL_END
1556 if (hotkey_mask
!= mask
) {
1557 printk(TPACPI_NOTICE
1558 "requested hot key mask 0x%08x, "
1559 "forced to 0x%08x (NVRAM poll mask is "
1560 "0x%08x): no firmware mask support\n",
1561 mask
, hotkey_mask
, hotkey_source_mask
);
1566 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1572 static int hotkey_status_get(int *status
)
1574 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1580 static int hotkey_status_set(int status
)
1582 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", status
))
1588 static void tpacpi_input_send_tabletsw(void)
1592 if (tp_features
.hotkey_tablet
&&
1593 !hotkey_get_tablet_mode(&state
)) {
1594 mutex_lock(&tpacpi_inputdev_send_mutex
);
1596 input_report_switch(tpacpi_inputdev
,
1597 SW_TABLET_MODE
, !!state
);
1598 input_sync(tpacpi_inputdev
);
1600 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1604 static void tpacpi_input_send_key(unsigned int scancode
)
1606 unsigned int keycode
;
1608 keycode
= hotkey_keycode_map
[scancode
];
1610 if (keycode
!= KEY_RESERVED
) {
1611 mutex_lock(&tpacpi_inputdev_send_mutex
);
1613 input_report_key(tpacpi_inputdev
, keycode
, 1);
1614 if (keycode
== KEY_UNKNOWN
)
1615 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1617 input_sync(tpacpi_inputdev
);
1619 input_report_key(tpacpi_inputdev
, keycode
, 0);
1620 if (keycode
== KEY_UNKNOWN
)
1621 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1623 input_sync(tpacpi_inputdev
);
1625 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1629 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1630 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1632 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1634 tpacpi_input_send_key(scancode
);
1635 if (hotkey_report_mode
< 2) {
1636 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1637 0x80, 0x1001 + scancode
);
1641 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1645 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1646 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1647 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1648 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1649 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1650 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1652 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1653 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1654 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1656 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1657 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1658 n
->displayexp_toggle
=
1659 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1661 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1662 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1663 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1664 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1665 n
->brightness_toggle
=
1666 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1668 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1669 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1670 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1671 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1672 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1673 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1677 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1679 if ((mask & (1 << __scancode)) && \
1680 oldn->__member != newn->__member) \
1681 tpacpi_hotkey_send_key(__scancode); \
1684 #define TPACPI_MAY_SEND_KEY(__scancode) \
1685 do { if (mask & (1 << __scancode)) \
1686 tpacpi_hotkey_send_key(__scancode); } while (0)
1688 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1689 struct tp_nvram_state
*newn
,
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1694 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1695 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1699 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1702 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1703 if (oldn
->mute
!= newn
->mute
) {
1704 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1706 if (oldn
->volume_level
> newn
->volume_level
) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1708 } else if (oldn
->volume_level
< newn
->volume_level
) {
1709 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1710 } else if (oldn
->mute
== newn
->mute
) {
1711 /* repeated key presses that didn't change state */
1713 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1714 } else if (newn
->volume_level
!= 0) {
1715 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1717 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1722 /* handle brightness */
1723 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1724 if (oldn
->brightness_level
< newn
->brightness_level
) {
1725 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1726 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1727 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1729 /* repeated key presses that didn't change state */
1730 if (newn
->brightness_level
!= 0) {
1731 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1733 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1739 #undef TPACPI_COMPARE_KEY
1740 #undef TPACPI_MAY_SEND_KEY
1742 static int hotkey_kthread(void *data
)
1744 struct tp_nvram_state s
[2];
1746 unsigned int si
, so
;
1748 unsigned int change_detector
, must_reset
;
1750 mutex_lock(&hotkey_thread_mutex
);
1752 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1761 /* Initial state for compares */
1762 mutex_lock(&hotkey_thread_data_mutex
);
1763 change_detector
= hotkey_config_change
;
1764 mask
= hotkey_source_mask
& hotkey_mask
;
1765 mutex_unlock(&hotkey_thread_data_mutex
);
1766 hotkey_read_nvram(&s
[so
], mask
);
1768 while (!kthread_should_stop() && hotkey_poll_freq
) {
1770 t
= 1000/hotkey_poll_freq
;
1771 t
= msleep_interruptible(t
);
1772 if (unlikely(kthread_should_stop()))
1774 must_reset
= try_to_freeze();
1775 if (t
> 0 && !must_reset
)
1778 mutex_lock(&hotkey_thread_data_mutex
);
1779 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1780 /* forget old state on thaw or config change */
1783 change_detector
= hotkey_config_change
;
1785 mask
= hotkey_source_mask
& hotkey_mask
;
1786 mutex_unlock(&hotkey_thread_data_mutex
);
1789 hotkey_read_nvram(&s
[si
], mask
);
1790 if (likely(si
!= so
)) {
1791 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1801 mutex_unlock(&hotkey_thread_mutex
);
1805 static void hotkey_poll_stop_sync(void)
1807 if (tpacpi_hotkey_task
) {
1808 if (frozen(tpacpi_hotkey_task
) ||
1809 freezing(tpacpi_hotkey_task
))
1810 thaw_process(tpacpi_hotkey_task
);
1812 kthread_stop(tpacpi_hotkey_task
);
1813 tpacpi_hotkey_task
= NULL
;
1814 mutex_lock(&hotkey_thread_mutex
);
1815 /* at this point, the thread did exit */
1816 mutex_unlock(&hotkey_thread_mutex
);
1820 /* call with hotkey_mutex held */
1821 static void hotkey_poll_setup(int may_warn
)
1823 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1824 hotkey_poll_freq
> 0 &&
1825 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1826 if (!tpacpi_hotkey_task
) {
1827 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1828 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
1829 if (IS_ERR(tpacpi_hotkey_task
)) {
1830 tpacpi_hotkey_task
= NULL
;
1832 "could not create kernel thread "
1833 "for hotkey polling\n");
1837 hotkey_poll_stop_sync();
1839 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1840 printk(TPACPI_NOTICE
1841 "hot keys 0x%08x require polling, "
1842 "which is currently disabled\n",
1843 hotkey_source_mask
);
1848 static void hotkey_poll_setup_safe(int may_warn
)
1850 mutex_lock(&hotkey_mutex
);
1851 hotkey_poll_setup(may_warn
);
1852 mutex_unlock(&hotkey_mutex
);
1855 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1857 static void hotkey_poll_setup_safe(int __unused
)
1861 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1863 static int hotkey_inputdev_open(struct input_dev
*dev
)
1865 switch (tpacpi_lifecycle
) {
1866 case TPACPI_LIFE_INIT
:
1868 * hotkey_init will call hotkey_poll_setup_safe
1869 * at the appropriate moment
1872 case TPACPI_LIFE_EXITING
:
1874 case TPACPI_LIFE_RUNNING
:
1875 hotkey_poll_setup_safe(0);
1879 /* Should only happen if tpacpi_lifecycle is corrupt */
1884 static void hotkey_inputdev_close(struct input_dev
*dev
)
1886 /* disable hotkey polling when possible */
1887 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1888 hotkey_poll_setup_safe(0);
1891 /* sysfs hotkey enable ------------------------------------------------- */
1892 static ssize_t
hotkey_enable_show(struct device
*dev
,
1893 struct device_attribute
*attr
,
1898 res
= hotkey_status_get(&status
);
1902 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1905 static ssize_t
hotkey_enable_store(struct device
*dev
,
1906 struct device_attribute
*attr
,
1907 const char *buf
, size_t count
)
1912 if (parse_strtoul(buf
, 1, &t
))
1915 res
= hotkey_status_set(t
);
1917 return (res
) ? res
: count
;
1920 static struct device_attribute dev_attr_hotkey_enable
=
1921 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1922 hotkey_enable_show
, hotkey_enable_store
);
1924 /* sysfs hotkey mask --------------------------------------------------- */
1925 static ssize_t
hotkey_mask_show(struct device
*dev
,
1926 struct device_attribute
*attr
,
1931 if (mutex_lock_killable(&hotkey_mutex
))
1932 return -ERESTARTSYS
;
1933 res
= hotkey_mask_get();
1934 mutex_unlock(&hotkey_mutex
);
1937 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1940 static ssize_t
hotkey_mask_store(struct device
*dev
,
1941 struct device_attribute
*attr
,
1942 const char *buf
, size_t count
)
1947 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1950 if (mutex_lock_killable(&hotkey_mutex
))
1951 return -ERESTARTSYS
;
1953 res
= hotkey_mask_set(t
);
1955 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1956 hotkey_poll_setup(1);
1959 mutex_unlock(&hotkey_mutex
);
1961 return (res
) ? res
: count
;
1964 static struct device_attribute dev_attr_hotkey_mask
=
1965 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1966 hotkey_mask_show
, hotkey_mask_store
);
1968 /* sysfs hotkey bios_enabled ------------------------------------------- */
1969 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1970 struct device_attribute
*attr
,
1973 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_orig_status
);
1976 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1977 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1979 /* sysfs hotkey bios_mask ---------------------------------------------- */
1980 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1981 struct device_attribute
*attr
,
1984 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1987 static struct device_attribute dev_attr_hotkey_bios_mask
=
1988 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1990 /* sysfs hotkey all_mask ----------------------------------------------- */
1991 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
1992 struct device_attribute
*attr
,
1995 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
1996 hotkey_all_mask
| hotkey_source_mask
);
1999 static struct device_attribute dev_attr_hotkey_all_mask
=
2000 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2002 /* sysfs hotkey recommended_mask --------------------------------------- */
2003 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2004 struct device_attribute
*attr
,
2007 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2008 (hotkey_all_mask
| hotkey_source_mask
)
2009 & ~hotkey_reserved_mask
);
2012 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2013 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2014 hotkey_recommended_mask_show
, NULL
);
2016 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2018 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2019 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2020 struct device_attribute
*attr
,
2023 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2026 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2027 struct device_attribute
*attr
,
2028 const char *buf
, size_t count
)
2032 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2033 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2036 if (mutex_lock_killable(&hotkey_mutex
))
2037 return -ERESTARTSYS
;
2039 HOTKEY_CONFIG_CRITICAL_START
2040 hotkey_source_mask
= t
;
2041 HOTKEY_CONFIG_CRITICAL_END
2043 hotkey_poll_setup(1);
2045 mutex_unlock(&hotkey_mutex
);
2050 static struct device_attribute dev_attr_hotkey_source_mask
=
2051 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2052 hotkey_source_mask_show
, hotkey_source_mask_store
);
2054 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2055 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2056 struct device_attribute
*attr
,
2059 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2062 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2063 struct device_attribute
*attr
,
2064 const char *buf
, size_t count
)
2068 if (parse_strtoul(buf
, 25, &t
))
2071 if (mutex_lock_killable(&hotkey_mutex
))
2072 return -ERESTARTSYS
;
2074 hotkey_poll_freq
= t
;
2076 hotkey_poll_setup(1);
2077 mutex_unlock(&hotkey_mutex
);
2082 static struct device_attribute dev_attr_hotkey_poll_freq
=
2083 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2084 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2086 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2088 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2089 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2090 struct device_attribute
*attr
,
2094 res
= hotkey_get_wlsw(&s
);
2098 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2101 static struct device_attribute dev_attr_hotkey_radio_sw
=
2102 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2104 static void hotkey_radio_sw_notify_change(void)
2106 if (tp_features
.hotkey_wlsw
)
2107 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2111 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2112 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2113 struct device_attribute
*attr
,
2117 res
= hotkey_get_tablet_mode(&s
);
2121 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2124 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2125 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2127 static void hotkey_tablet_mode_notify_change(void)
2129 if (tp_features
.hotkey_tablet
)
2130 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2131 "hotkey_tablet_mode");
2134 /* sysfs hotkey report_mode -------------------------------------------- */
2135 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2136 struct device_attribute
*attr
,
2139 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2140 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2143 static struct device_attribute dev_attr_hotkey_report_mode
=
2144 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2146 /* sysfs wakeup reason (pollable) -------------------------------------- */
2147 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2148 struct device_attribute
*attr
,
2151 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2154 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2155 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2157 static void hotkey_wakeup_reason_notify_change(void)
2159 if (tp_features
.hotkey_mask
)
2160 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2164 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2165 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2166 struct device_attribute
*attr
,
2169 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2172 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2173 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2174 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2176 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2178 if (tp_features
.hotkey_mask
)
2179 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2180 "wakeup_hotunplug_complete");
2183 /* --------------------------------------------------------------------- */
2185 static struct attribute
*hotkey_attributes
[] __initdata
= {
2186 &dev_attr_hotkey_enable
.attr
,
2187 &dev_attr_hotkey_bios_enabled
.attr
,
2188 &dev_attr_hotkey_report_mode
.attr
,
2189 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2190 &dev_attr_hotkey_mask
.attr
,
2191 &dev_attr_hotkey_all_mask
.attr
,
2192 &dev_attr_hotkey_recommended_mask
.attr
,
2193 &dev_attr_hotkey_source_mask
.attr
,
2194 &dev_attr_hotkey_poll_freq
.attr
,
2198 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
2199 &dev_attr_hotkey_bios_mask
.attr
,
2200 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2201 &dev_attr_hotkey_mask
.attr
,
2202 &dev_attr_hotkey_all_mask
.attr
,
2203 &dev_attr_hotkey_recommended_mask
.attr
,
2205 &dev_attr_hotkey_wakeup_reason
.attr
,
2206 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2209 static void bluetooth_update_rfk(void);
2210 static void wan_update_rfk(void);
2211 static void uwb_update_rfk(void);
2212 static void tpacpi_send_radiosw_update(void)
2216 /* Sync these BEFORE sending any rfkill events */
2217 if (tp_features
.bluetooth
)
2218 bluetooth_update_rfk();
2219 if (tp_features
.wan
)
2221 if (tp_features
.uwb
)
2224 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
2225 mutex_lock(&tpacpi_inputdev_send_mutex
);
2227 input_report_switch(tpacpi_inputdev
,
2228 SW_RFKILL_ALL
, !!wlsw
);
2229 input_sync(tpacpi_inputdev
);
2231 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2233 hotkey_radio_sw_notify_change();
2236 static void hotkey_exit(void)
2238 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2239 hotkey_poll_stop_sync();
2242 if (hotkey_dev_attributes
)
2243 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2245 kfree(hotkey_keycode_map
);
2247 if (tp_features
.hotkey
) {
2248 dbg_printk(TPACPI_DBG_EXIT
,
2249 "restoring original hot key mask\n");
2250 /* no short-circuit boolean operator below! */
2251 if ((hotkey_mask_set(hotkey_orig_mask
) |
2252 hotkey_status_set(hotkey_orig_status
)) != 0)
2254 "failed to restore hot key mask "
2255 "to BIOS defaults\n");
2259 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2261 /* Requirements for changing the default keymaps:
2263 * 1. Many of the keys are mapped to KEY_RESERVED for very
2264 * good reasons. Do not change them unless you have deep
2265 * knowledge on the IBM and Lenovo ThinkPad firmware for
2266 * the various ThinkPad models. The driver behaves
2267 * differently for KEY_RESERVED: such keys have their
2268 * hot key mask *unset* in mask_recommended, and also
2269 * in the initial hot key mask programmed into the
2270 * firmware at driver load time, which means the firm-
2271 * ware may react very differently if you change them to
2274 * 2. You must be subscribed to the linux-thinkpad and
2275 * ibm-acpi-devel mailing lists, and you should read the
2276 * list archives since 2007 if you want to change the
2277 * keymaps. This requirement exists so that you will
2278 * know the past history of problems with the thinkpad-
2279 * acpi driver keymaps, and also that you will be
2280 * listening to any bug reports;
2282 * 3. Do not send thinkpad-acpi specific patches directly to
2283 * for merging, *ever*. Send them to the linux-acpi
2284 * mailinglist for comments. Merging is to be done only
2285 * through acpi-test and the ACPI maintainer.
2287 * If the above is too much to ask, don't change the keymap.
2288 * Ask the thinkpad-acpi maintainer to do it, instead.
2290 static u16 ibm_keycode_map
[] __initdata
= {
2291 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2292 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2293 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2294 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2296 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2297 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2298 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2299 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2301 /* brightness: firmware always reacts to them, unless
2302 * X.org did some tricks in the radeon BIOS scratch
2303 * registers of *some* models */
2304 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2305 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2307 /* Thinklight: firmware always react to it */
2308 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2310 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2311 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2313 /* Volume: firmware always react to it and reprograms
2314 * the built-in *extra* mixer. Never map it to control
2315 * another mixer by default. */
2316 KEY_RESERVED
, /* 0x14: VOLUME UP */
2317 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2318 KEY_RESERVED
, /* 0x16: MUTE */
2320 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2322 /* (assignments unknown, please report if found) */
2323 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2324 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2326 static u16 lenovo_keycode_map
[] __initdata
= {
2327 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2328 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2329 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2330 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2332 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2333 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2334 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2335 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2337 /* These either have to go through ACPI video, or
2338 * act like in the IBM ThinkPads, so don't ever
2339 * enable them by default */
2340 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2341 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2343 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2345 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2346 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2348 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2349 * react to it and reprograms the built-in *extra* mixer.
2350 * Never map it to control another mixer by default.
2352 * T60?, T61, R60?, R61: firmware and EC tries to send
2353 * these over the regular keyboard, so these are no-ops,
2354 * but there are still weird bugs re. MUTE, so do not
2355 * change unless you get test reports from all Lenovo
2356 * models. May cause the BIOS to interfere with the
2359 KEY_RESERVED
, /* 0x14: VOLUME UP */
2360 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2361 KEY_RESERVED
, /* 0x16: MUTE */
2363 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2365 /* (assignments unknown, please report if found) */
2366 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2367 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2370 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2371 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2372 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2378 vdbg_printk(TPACPI_DBG_INIT
, "initializing hotkey subdriver\n");
2380 BUG_ON(!tpacpi_inputdev
);
2381 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2382 tpacpi_inputdev
->close
!= NULL
);
2384 TPACPI_ACPIHANDLE_INIT(hkey
);
2385 mutex_init(&hotkey_mutex
);
2387 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2388 mutex_init(&hotkey_thread_mutex
);
2389 mutex_init(&hotkey_thread_data_mutex
);
2392 /* hotkey not supported on 570 */
2393 tp_features
.hotkey
= hkey_handle
!= NULL
;
2395 vdbg_printk(TPACPI_DBG_INIT
, "hotkeys are %s\n",
2396 str_supported(tp_features
.hotkey
));
2398 if (!tp_features
.hotkey
)
2401 tpacpi_disable_brightness_delay();
2403 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2404 if (!hotkey_dev_attributes
)
2406 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2408 ARRAY_SIZE(hotkey_attributes
));
2412 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2413 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2414 for HKEY interface version 0x100 */
2415 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2416 if ((hkeyv
>> 8) != 1) {
2417 printk(TPACPI_ERR
"unknown version of the "
2418 "HKEY interface: 0x%x\n", hkeyv
);
2419 printk(TPACPI_ERR
"please report this to %s\n",
2423 * MHKV 0x100 in A31, R40, R40e,
2424 * T4x, X31, and later
2426 tp_features
.hotkey_mask
= 1;
2430 vdbg_printk(TPACPI_DBG_INIT
, "hotkey masks are %s\n",
2431 str_supported(tp_features
.hotkey_mask
));
2433 if (tp_features
.hotkey_mask
) {
2434 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2437 "missing MHKA handler, "
2438 "please report this to %s\n",
2440 /* FN+F12, FN+F4, FN+F3 */
2441 hotkey_all_mask
= 0x080cU
;
2445 /* hotkey_source_mask *must* be zero for
2446 * the first hotkey_mask_get */
2447 res
= hotkey_status_get(&hotkey_orig_status
);
2451 if (tp_features
.hotkey_mask
) {
2452 res
= hotkey_mask_get();
2456 hotkey_orig_mask
= hotkey_mask
;
2457 res
= add_many_to_attr_set(
2458 hotkey_dev_attributes
,
2459 hotkey_mask_attributes
,
2460 ARRAY_SIZE(hotkey_mask_attributes
));
2465 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2466 if (tp_features
.hotkey_mask
) {
2467 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2470 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2473 vdbg_printk(TPACPI_DBG_INIT
,
2474 "hotkey source mask 0x%08x, polling freq %d\n",
2475 hotkey_source_mask
, hotkey_poll_freq
);
2478 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2480 tp_features
.hotkey_wlsw
= 1;
2482 "radio switch emulation enabled\n");
2485 /* Not all thinkpads have a hardware radio switch */
2486 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2487 tp_features
.hotkey_wlsw
= 1;
2489 "radio switch found; radios are %s\n",
2490 enabled(status
, 0));
2492 if (tp_features
.hotkey_wlsw
)
2493 res
= add_to_attr_set(hotkey_dev_attributes
,
2494 &dev_attr_hotkey_radio_sw
.attr
);
2496 /* For X41t, X60t, X61t Tablets... */
2497 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2498 tp_features
.hotkey_tablet
= 1;
2500 "possible tablet mode switch found; "
2501 "ThinkPad in %s mode\n",
2502 (status
& TP_HOTKEY_TABLET_MASK
)?
2503 "tablet" : "laptop");
2504 res
= add_to_attr_set(hotkey_dev_attributes
,
2505 &dev_attr_hotkey_tablet_mode
.attr
);
2509 res
= register_attr_set_with_sysfs(
2510 hotkey_dev_attributes
,
2511 &tpacpi_pdev
->dev
.kobj
);
2515 /* Set up key map */
2517 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2519 if (!hotkey_keycode_map
) {
2521 "failed to allocate memory for key map\n");
2526 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2527 dbg_printk(TPACPI_DBG_INIT
,
2528 "using Lenovo default hot key map\n");
2529 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2530 TPACPI_HOTKEY_MAP_SIZE
);
2532 dbg_printk(TPACPI_DBG_INIT
,
2533 "using IBM default hot key map\n");
2534 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2535 TPACPI_HOTKEY_MAP_SIZE
);
2538 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2539 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2540 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2541 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2542 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2543 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2544 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2545 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2546 set_bit(hotkey_keycode_map
[i
],
2547 tpacpi_inputdev
->keybit
);
2549 if (i
< sizeof(hotkey_reserved_mask
)*8)
2550 hotkey_reserved_mask
|= 1 << i
;
2554 if (tp_features
.hotkey_wlsw
) {
2555 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2556 set_bit(SW_RFKILL_ALL
, tpacpi_inputdev
->swbit
);
2558 if (tp_features
.hotkey_tablet
) {
2559 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2560 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2563 /* Do not issue duplicate brightness change events to
2565 if (!tp_features
.bright_acpimode
)
2566 /* update bright_acpimode... */
2567 tpacpi_check_std_acpi_brightness_support();
2569 if (tp_features
.bright_acpimode
) {
2571 "This ThinkPad has standard ACPI backlight "
2572 "brightness control, supported by the ACPI "
2574 printk(TPACPI_NOTICE
2575 "Disabling thinkpad-acpi brightness events "
2578 /* The hotkey_reserved_mask change below is not
2579 * necessary while the keys are at KEY_RESERVED in the
2580 * default map, but better safe than sorry, leave it
2581 * here as a marker of what we have to do, especially
2582 * when we finally become able to set this at runtime
2583 * on response to X.org requests */
2584 hotkey_reserved_mask
|=
2585 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
2586 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
2589 dbg_printk(TPACPI_DBG_INIT
, "enabling hot key handling\n");
2590 res
= hotkey_status_set(1);
2595 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2596 & ~hotkey_reserved_mask
)
2597 | hotkey_orig_mask
);
2598 if (res
< 0 && res
!= -ENXIO
) {
2603 dbg_printk(TPACPI_DBG_INIT
,
2604 "legacy hot key reporting over procfs %s\n",
2605 (hotkey_report_mode
< 2) ?
2606 "enabled" : "disabled");
2608 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2609 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2611 hotkey_poll_setup_safe(1);
2612 tpacpi_send_radiosw_update();
2613 tpacpi_input_send_tabletsw();
2618 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2619 hotkey_dev_attributes
= NULL
;
2621 return (res
< 0)? res
: 1;
2624 static bool hotkey_notify_hotkey(const u32 hkey
,
2626 bool *ignore_acpi_ev
)
2628 /* 0x1000-0x1FFF: key presses */
2629 unsigned int scancode
= hkey
& 0xfff;
2630 *send_acpi_ev
= true;
2631 *ignore_acpi_ev
= false;
2633 if (scancode
> 0 && scancode
< 0x21) {
2635 if (!(hotkey_source_mask
& (1 << scancode
))) {
2636 tpacpi_input_send_key(scancode
);
2637 *send_acpi_ev
= false;
2639 *ignore_acpi_ev
= true;
2646 static bool hotkey_notify_wakeup(const u32 hkey
,
2648 bool *ignore_acpi_ev
)
2650 /* 0x2000-0x2FFF: Wakeup reason */
2651 *send_acpi_ev
= true;
2652 *ignore_acpi_ev
= false;
2655 case 0x2304: /* suspend, undock */
2656 case 0x2404: /* hibernation, undock */
2657 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2658 *ignore_acpi_ev
= true;
2661 case 0x2305: /* suspend, bay eject */
2662 case 0x2405: /* hibernation, bay eject */
2663 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2664 *ignore_acpi_ev
= true;
2667 case 0x2313: /* Battery on critical low level (S3) */
2668 case 0x2413: /* Battery on critical low level (S4) */
2670 "EMERGENCY WAKEUP: battery almost empty\n");
2671 /* how to auto-heal: */
2672 /* 2313: woke up from S3, go to S4/S5 */
2673 /* 2413: woke up from S4, go to S5 */
2680 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2682 "woke up due to a hot-unplug "
2684 hotkey_wakeup_reason_notify_change();
2689 static bool hotkey_notify_usrevent(const u32 hkey
,
2691 bool *ignore_acpi_ev
)
2693 /* 0x5000-0x5FFF: human interface helpers */
2694 *send_acpi_ev
= true;
2695 *ignore_acpi_ev
= false;
2698 case 0x5010: /* Lenovo new BIOS: brightness changed */
2699 case 0x500b: /* X61t: tablet pen inserted into bay */
2700 case 0x500c: /* X61t: tablet pen removed from bay */
2703 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2704 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2705 tpacpi_input_send_tabletsw();
2706 hotkey_tablet_mode_notify_change();
2707 *send_acpi_ev
= false;
2712 /* LID switch events. Do not propagate */
2713 *ignore_acpi_ev
= true;
2721 static bool hotkey_notify_thermal(const u32 hkey
,
2723 bool *ignore_acpi_ev
)
2725 /* 0x6000-0x6FFF: thermal alarms */
2726 *send_acpi_ev
= true;
2727 *ignore_acpi_ev
= false;
2732 "THERMAL ALARM: battery is too hot!\n");
2733 /* recommended action: warn user through gui */
2737 "THERMAL EMERGENCY: battery is extremely hot!\n");
2738 /* recommended action: immediate sleep/hibernate */
2743 "a sensor reports something is too hot!\n");
2744 /* recommended action: warn user through gui, that */
2745 /* some internal component is too hot */
2749 "THERMAL EMERGENCY: "
2750 "a sensor reports something is extremely hot!\n");
2751 /* recommended action: immediate sleep/hibernate */
2755 "EC reports that Thermal Table has changed\n");
2756 /* recommended action: do nothing, we don't have
2757 * Lenovo ATM information */
2761 "THERMAL ALERT: unknown thermal alarm received\n");
2766 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2770 bool ignore_acpi_ev
;
2773 if (event
!= 0x80) {
2775 "unknown HKEY notification event %d\n", event
);
2776 /* forward it to userspace, maybe it knows how to handle it */
2777 acpi_bus_generate_netlink_event(
2778 ibm
->acpi
->device
->pnp
.device_class
,
2779 dev_name(&ibm
->acpi
->device
->dev
),
2785 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2786 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2795 send_acpi_ev
= true;
2796 ignore_acpi_ev
= false;
2798 switch (hkey
>> 12) {
2800 /* 0x1000-0x1FFF: key presses */
2801 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
2805 /* 0x2000-0x2FFF: Wakeup reason */
2806 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
2810 /* 0x3000-0x3FFF: bay-related wakeups */
2811 if (hkey
== 0x3003) {
2812 hotkey_autosleep_ack
= 1;
2815 hotkey_wakeup_hotunplug_complete_notify_change();
2822 /* 0x4000-0x4FFF: dock-related wakeups */
2823 if (hkey
== 0x4003) {
2824 hotkey_autosleep_ack
= 1;
2827 hotkey_wakeup_hotunplug_complete_notify_change();
2834 /* 0x5000-0x5FFF: human interface helpers */
2835 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
2839 /* 0x6000-0x6FFF: thermal alarms */
2840 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
2844 /* 0x7000-0x7FFF: misc */
2845 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2846 tpacpi_send_radiosw_update();
2851 /* fallthrough to default */
2856 printk(TPACPI_NOTICE
2857 "unhandled HKEY event 0x%04x\n", hkey
);
2858 printk(TPACPI_NOTICE
2859 "please report the conditions when this "
2860 "event happened to %s\n", TPACPI_MAIL
);
2864 if (!ignore_acpi_ev
&&
2865 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2866 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2870 /* netlink events */
2871 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2872 acpi_bus_generate_netlink_event(
2873 ibm
->acpi
->device
->pnp
.device_class
,
2874 dev_name(&ibm
->acpi
->device
->dev
),
2880 static void hotkey_suspend(pm_message_t state
)
2882 /* Do these on suspend, we get the events on early resume! */
2883 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2884 hotkey_autosleep_ack
= 0;
2887 static void hotkey_resume(void)
2889 tpacpi_disable_brightness_delay();
2891 if (hotkey_mask_get())
2893 "error while trying to read hot key mask "
2895 tpacpi_send_radiosw_update();
2896 hotkey_tablet_mode_notify_change();
2897 hotkey_wakeup_reason_notify_change();
2898 hotkey_wakeup_hotunplug_complete_notify_change();
2899 hotkey_poll_setup_safe(0);
2902 /* procfs -------------------------------------------------------------- */
2903 static int hotkey_read(char *p
)
2908 if (!tp_features
.hotkey
) {
2909 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2913 if (mutex_lock_killable(&hotkey_mutex
))
2914 return -ERESTARTSYS
;
2915 res
= hotkey_status_get(&status
);
2917 res
= hotkey_mask_get();
2918 mutex_unlock(&hotkey_mutex
);
2922 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2923 if (tp_features
.hotkey_mask
) {
2924 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2925 len
+= sprintf(p
+ len
,
2926 "commands:\tenable, disable, reset, <mask>\n");
2928 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2929 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2935 static int hotkey_write(char *buf
)
2941 if (!tp_features
.hotkey
)
2944 if (mutex_lock_killable(&hotkey_mutex
))
2945 return -ERESTARTSYS
;
2951 while ((cmd
= next_cmd(&buf
))) {
2952 if (strlencmp(cmd
, "enable") == 0) {
2954 } else if (strlencmp(cmd
, "disable") == 0) {
2956 } else if (strlencmp(cmd
, "reset") == 0) {
2957 status
= hotkey_orig_status
;
2958 mask
= hotkey_orig_mask
;
2959 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2961 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2969 res
= hotkey_status_set(status
);
2971 if (!res
&& mask
!= hotkey_mask
)
2972 res
= hotkey_mask_set(mask
);
2975 mutex_unlock(&hotkey_mutex
);
2979 static const struct acpi_device_id ibm_htk_device_ids
[] = {
2980 {TPACPI_ACPI_HKEY_HID
, 0},
2984 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
2985 .hid
= ibm_htk_device_ids
,
2986 .notify
= hotkey_notify
,
2987 .handle
= &hkey_handle
,
2988 .type
= ACPI_DEVICE_NOTIFY
,
2991 static struct ibm_struct hotkey_driver_data
= {
2993 .read
= hotkey_read
,
2994 .write
= hotkey_write
,
2995 .exit
= hotkey_exit
,
2996 .resume
= hotkey_resume
,
2997 .suspend
= hotkey_suspend
,
2998 .acpi
= &ibm_hotkey_acpidriver
,
3001 /*************************************************************************
3002 * Bluetooth subdriver
3006 /* ACPI GBDC/SBDC bits */
3007 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3008 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3009 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3014 /* ACPI \BLTH commands */
3015 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3016 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3017 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3018 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3019 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3022 static struct rfkill
*tpacpi_bluetooth_rfkill
;
3024 static void bluetooth_suspend(pm_message_t state
)
3026 /* Try to make sure radio will resume powered off */
3027 acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3028 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
);
3031 static int bluetooth_get_radiosw(void)
3035 if (!tp_features
.bluetooth
)
3038 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3039 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3040 return RFKILL_STATE_HARD_BLOCKED
;
3042 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3043 if (dbg_bluetoothemul
)
3044 return (tpacpi_bluetooth_emulstate
) ?
3045 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3048 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3051 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3052 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3055 static void bluetooth_update_rfk(void)
3059 if (!tpacpi_bluetooth_rfkill
)
3062 status
= bluetooth_get_radiosw();
3065 rfkill_force_state(tpacpi_bluetooth_rfkill
, status
);
3068 static int bluetooth_set_radiosw(int radio_on
, int update_rfk
)
3072 if (!tp_features
.bluetooth
)
3075 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3076 * reason to risk weird behaviour. */
3077 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3082 if (dbg_bluetoothemul
) {
3083 tpacpi_bluetooth_emulstate
= !!radio_on
;
3085 bluetooth_update_rfk();
3090 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3092 status
= TP_ACPI_BLUETOOTH_RADIOSSW
;
3095 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3099 bluetooth_update_rfk();
3104 /* sysfs bluetooth enable ---------------------------------------------- */
3105 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3106 struct device_attribute
*attr
,
3111 printk_deprecated_rfkill_attribute("bluetooth_enable");
3113 status
= bluetooth_get_radiosw();
3117 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3118 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3121 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3122 struct device_attribute
*attr
,
3123 const char *buf
, size_t count
)
3128 printk_deprecated_rfkill_attribute("bluetooth_enable");
3130 if (parse_strtoul(buf
, 1, &t
))
3133 res
= bluetooth_set_radiosw(t
, 1);
3135 return (res
) ? res
: count
;
3138 static struct device_attribute dev_attr_bluetooth_enable
=
3139 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3140 bluetooth_enable_show
, bluetooth_enable_store
);
3142 /* --------------------------------------------------------------------- */
3144 static struct attribute
*bluetooth_attributes
[] = {
3145 &dev_attr_bluetooth_enable
.attr
,
3149 static const struct attribute_group bluetooth_attr_group
= {
3150 .attrs
= bluetooth_attributes
,
3153 static int tpacpi_bluetooth_rfk_get(void *data
, enum rfkill_state
*state
)
3155 int bts
= bluetooth_get_radiosw();
3164 static int tpacpi_bluetooth_rfk_set(void *data
, enum rfkill_state state
)
3166 return bluetooth_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3169 static void bluetooth_shutdown(void)
3171 /* Order firmware to save current state to NVRAM */
3172 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3173 TP_ACPI_BLTH_SAVE_STATE
))
3174 printk(TPACPI_NOTICE
3175 "failed to save bluetooth state to NVRAM\n");
3178 static void bluetooth_exit(void)
3180 bluetooth_shutdown();
3182 if (tpacpi_bluetooth_rfkill
)
3183 rfkill_unregister(tpacpi_bluetooth_rfkill
);
3185 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3186 &bluetooth_attr_group
);
3189 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3194 vdbg_printk(TPACPI_DBG_INIT
, "initializing bluetooth subdriver\n");
3196 TPACPI_ACPIHANDLE_INIT(hkey
);
3198 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3199 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3200 tp_features
.bluetooth
= hkey_handle
&&
3201 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
3203 vdbg_printk(TPACPI_DBG_INIT
, "bluetooth is %s, status 0x%02x\n",
3204 str_supported(tp_features
.bluetooth
),
3207 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3208 if (dbg_bluetoothemul
) {
3209 tp_features
.bluetooth
= 1;
3211 "bluetooth switch emulation enabled\n");
3214 if (tp_features
.bluetooth
&&
3215 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
3216 /* no bluetooth hardware present in system */
3217 tp_features
.bluetooth
= 0;
3218 dbg_printk(TPACPI_DBG_INIT
,
3219 "bluetooth hardware not installed\n");
3222 if (!tp_features
.bluetooth
)
3225 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3226 &bluetooth_attr_group
);
3230 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
3231 &tpacpi_bluetooth_rfkill
,
3232 RFKILL_TYPE_BLUETOOTH
,
3233 "tpacpi_bluetooth_sw",
3235 tpacpi_bluetooth_rfk_set
,
3236 tpacpi_bluetooth_rfk_get
);
3245 /* procfs -------------------------------------------------------------- */
3246 static int bluetooth_read(char *p
)
3249 int status
= bluetooth_get_radiosw();
3251 if (!tp_features
.bluetooth
)
3252 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3254 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3255 (status
== RFKILL_STATE_UNBLOCKED
) ?
3256 "enabled" : "disabled");
3257 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3263 static int bluetooth_write(char *buf
)
3267 if (!tp_features
.bluetooth
)
3270 while ((cmd
= next_cmd(&buf
))) {
3271 if (strlencmp(cmd
, "enable") == 0) {
3272 bluetooth_set_radiosw(1, 1);
3273 } else if (strlencmp(cmd
, "disable") == 0) {
3274 bluetooth_set_radiosw(0, 1);
3282 static struct ibm_struct bluetooth_driver_data
= {
3283 .name
= "bluetooth",
3284 .read
= bluetooth_read
,
3285 .write
= bluetooth_write
,
3286 .exit
= bluetooth_exit
,
3287 .suspend
= bluetooth_suspend
,
3288 .shutdown
= bluetooth_shutdown
,
3291 /*************************************************************************
3296 /* ACPI GWAN/SWAN bits */
3297 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
3298 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
3299 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
3303 static struct rfkill
*tpacpi_wan_rfkill
;
3305 static void wan_suspend(pm_message_t state
)
3307 /* Try to make sure radio will resume powered off */
3308 acpi_evalf(NULL
, NULL
, "\\WGSV", "qvd",
3309 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
);
3312 static int wan_get_radiosw(void)
3316 if (!tp_features
.wan
)
3319 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3320 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3321 return RFKILL_STATE_HARD_BLOCKED
;
3323 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3325 return (tpacpi_wwan_emulstate
) ?
3326 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3329 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
3332 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
3333 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3336 static void wan_update_rfk(void)
3340 if (!tpacpi_wan_rfkill
)
3343 status
= wan_get_radiosw();
3346 rfkill_force_state(tpacpi_wan_rfkill
, status
);
3349 static int wan_set_radiosw(int radio_on
, int update_rfk
)
3353 if (!tp_features
.wan
)
3356 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3357 * reason to risk weird behaviour. */
3358 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3362 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3364 tpacpi_wwan_emulstate
= !!radio_on
;
3371 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3373 status
= TP_ACPI_WANCARD_RADIOSSW
;
3376 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
3385 /* sysfs wan enable ---------------------------------------------------- */
3386 static ssize_t
wan_enable_show(struct device
*dev
,
3387 struct device_attribute
*attr
,
3392 printk_deprecated_rfkill_attribute("wwan_enable");
3394 status
= wan_get_radiosw();
3398 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3399 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3402 static ssize_t
wan_enable_store(struct device
*dev
,
3403 struct device_attribute
*attr
,
3404 const char *buf
, size_t count
)
3409 printk_deprecated_rfkill_attribute("wwan_enable");
3411 if (parse_strtoul(buf
, 1, &t
))
3414 res
= wan_set_radiosw(t
, 1);
3416 return (res
) ? res
: count
;
3419 static struct device_attribute dev_attr_wan_enable
=
3420 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
3421 wan_enable_show
, wan_enable_store
);
3423 /* --------------------------------------------------------------------- */
3425 static struct attribute
*wan_attributes
[] = {
3426 &dev_attr_wan_enable
.attr
,
3430 static const struct attribute_group wan_attr_group
= {
3431 .attrs
= wan_attributes
,
3434 static int tpacpi_wan_rfk_get(void *data
, enum rfkill_state
*state
)
3436 int wans
= wan_get_radiosw();
3445 static int tpacpi_wan_rfk_set(void *data
, enum rfkill_state state
)
3447 return wan_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3450 static void wan_shutdown(void)
3452 /* Order firmware to save current state to NVRAM */
3453 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
3454 TP_ACPI_WGSV_SAVE_STATE
))
3455 printk(TPACPI_NOTICE
3456 "failed to save WWAN state to NVRAM\n");
3459 static void wan_exit(void)
3463 if (tpacpi_wan_rfkill
)
3464 rfkill_unregister(tpacpi_wan_rfkill
);
3466 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3470 static int __init
wan_init(struct ibm_init_struct
*iibm
)
3475 vdbg_printk(TPACPI_DBG_INIT
, "initializing wan subdriver\n");
3477 TPACPI_ACPIHANDLE_INIT(hkey
);
3479 tp_features
.wan
= hkey_handle
&&
3480 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
3482 vdbg_printk(TPACPI_DBG_INIT
, "wan is %s, status 0x%02x\n",
3483 str_supported(tp_features
.wan
),
3486 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3488 tp_features
.wan
= 1;
3490 "wwan switch emulation enabled\n");
3493 if (tp_features
.wan
&&
3494 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
3495 /* no wan hardware present in system */
3496 tp_features
.wan
= 0;
3497 dbg_printk(TPACPI_DBG_INIT
,
3498 "wan hardware not installed\n");
3501 if (!tp_features
.wan
)
3504 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3509 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
3515 tpacpi_wan_rfk_get
);
3524 /* procfs -------------------------------------------------------------- */
3525 static int wan_read(char *p
)
3528 int status
= wan_get_radiosw();
3530 if (!tp_features
.wan
)
3531 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3533 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3534 (status
== RFKILL_STATE_UNBLOCKED
) ?
3535 "enabled" : "disabled");
3536 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3542 static int wan_write(char *buf
)
3546 if (!tp_features
.wan
)
3549 while ((cmd
= next_cmd(&buf
))) {
3550 if (strlencmp(cmd
, "enable") == 0) {
3551 wan_set_radiosw(1, 1);
3552 } else if (strlencmp(cmd
, "disable") == 0) {
3553 wan_set_radiosw(0, 1);
3561 static struct ibm_struct wan_driver_data
= {
3566 .suspend
= wan_suspend
,
3567 .shutdown
= wan_shutdown
,
3570 /*************************************************************************
3575 /* ACPI GUWB/SUWB bits */
3576 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
3577 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
3580 static struct rfkill
*tpacpi_uwb_rfkill
;
3582 static int uwb_get_radiosw(void)
3586 if (!tp_features
.uwb
)
3589 /* WLSW overrides UWB in firmware/hardware, reflect that */
3590 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3591 return RFKILL_STATE_HARD_BLOCKED
;
3593 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3595 return (tpacpi_uwb_emulstate
) ?
3596 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3599 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
3602 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
3603 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3606 static void uwb_update_rfk(void)
3610 if (!tpacpi_uwb_rfkill
)
3613 status
= uwb_get_radiosw();
3616 rfkill_force_state(tpacpi_uwb_rfkill
, status
);
3619 static int uwb_set_radiosw(int radio_on
, int update_rfk
)
3623 if (!tp_features
.uwb
)
3626 /* WLSW overrides UWB in firmware/hardware, but there is no
3627 * reason to risk weird behaviour. */
3628 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3632 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3634 tpacpi_uwb_emulstate
= !!radio_on
;
3641 status
= (radio_on
) ? TP_ACPI_UWB_RADIOSSW
: 0;
3642 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
3651 /* --------------------------------------------------------------------- */
3653 static int tpacpi_uwb_rfk_get(void *data
, enum rfkill_state
*state
)
3655 int uwbs
= uwb_get_radiosw();
3664 static int tpacpi_uwb_rfk_set(void *data
, enum rfkill_state state
)
3666 return uwb_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3669 static void uwb_exit(void)
3671 if (tpacpi_uwb_rfkill
)
3672 rfkill_unregister(tpacpi_uwb_rfkill
);
3675 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
3680 vdbg_printk(TPACPI_DBG_INIT
, "initializing uwb subdriver\n");
3682 TPACPI_ACPIHANDLE_INIT(hkey
);
3684 tp_features
.uwb
= hkey_handle
&&
3685 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
3687 vdbg_printk(TPACPI_DBG_INIT
, "uwb is %s, status 0x%02x\n",
3688 str_supported(tp_features
.uwb
),
3691 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3693 tp_features
.uwb
= 1;
3695 "uwb switch emulation enabled\n");
3698 if (tp_features
.uwb
&&
3699 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
3700 /* no uwb hardware present in system */
3701 tp_features
.uwb
= 0;
3702 dbg_printk(TPACPI_DBG_INIT
,
3703 "uwb hardware not installed\n");
3706 if (!tp_features
.uwb
)
3709 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
3715 tpacpi_uwb_rfk_get
);
3720 static struct ibm_struct uwb_driver_data
= {
3723 .flags
.experimental
= 1,
3726 /*************************************************************************
3730 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3732 enum video_access_mode
{
3733 TPACPI_VIDEO_NONE
= 0,
3734 TPACPI_VIDEO_570
, /* 570 */
3735 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
3736 TPACPI_VIDEO_NEW
, /* all others */
3739 enum { /* video status flags, based on VIDEO_570 */
3740 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
3741 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
3742 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
3745 enum { /* TPACPI_VIDEO_570 constants */
3746 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
3747 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
3748 * video_status_flags */
3749 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
3750 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
3753 static enum video_access_mode video_supported
;
3754 static int video_orig_autosw
;
3756 static int video_autosw_get(void);
3757 static int video_autosw_set(int enable
);
3759 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3761 static int __init
video_init(struct ibm_init_struct
*iibm
)
3765 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
3767 TPACPI_ACPIHANDLE_INIT(vid
);
3768 TPACPI_ACPIHANDLE_INIT(vid2
);
3770 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
3771 /* G41, assume IVGA doesn't change */
3772 vid_handle
= vid2_handle
;
3775 /* video switching not supported on R30, R31 */
3776 video_supported
= TPACPI_VIDEO_NONE
;
3777 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
3779 video_supported
= TPACPI_VIDEO_570
;
3780 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
3781 /* 600e/x, 770e, 770x */
3782 video_supported
= TPACPI_VIDEO_770
;
3785 video_supported
= TPACPI_VIDEO_NEW
;
3787 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
3788 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
3791 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
3794 static void video_exit(void)
3796 dbg_printk(TPACPI_DBG_EXIT
,
3797 "restoring original video autoswitch mode\n");
3798 if (video_autosw_set(video_orig_autosw
))
3799 printk(TPACPI_ERR
"error while trying to restore original "
3800 "video autoswitch mode\n");
3803 static int video_outputsw_get(void)
3808 switch (video_supported
) {
3809 case TPACPI_VIDEO_570
:
3810 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
3811 TP_ACPI_VIDEO_570_PHSCMD
))
3813 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
3815 case TPACPI_VIDEO_770
:
3816 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3819 status
|= TP_ACPI_VIDEO_S_LCD
;
3820 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3823 status
|= TP_ACPI_VIDEO_S_CRT
;
3825 case TPACPI_VIDEO_NEW
:
3826 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
3827 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3830 status
|= TP_ACPI_VIDEO_S_CRT
;
3832 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
3833 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3836 status
|= TP_ACPI_VIDEO_S_LCD
;
3837 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
3840 status
|= TP_ACPI_VIDEO_S_DVI
;
3849 static int video_outputsw_set(int status
)
3854 switch (video_supported
) {
3855 case TPACPI_VIDEO_570
:
3856 res
= acpi_evalf(NULL
, NULL
,
3857 "\\_SB.PHS2", "vdd",
3858 TP_ACPI_VIDEO_570_PHS2CMD
,
3859 status
| TP_ACPI_VIDEO_570_PHS2SET
);
3861 case TPACPI_VIDEO_770
:
3862 autosw
= video_autosw_get();
3866 res
= video_autosw_set(1);
3869 res
= acpi_evalf(vid_handle
, NULL
,
3870 "ASWT", "vdd", status
* 0x100, 0);
3871 if (!autosw
&& video_autosw_set(autosw
)) {
3873 "video auto-switch left enabled due to error\n");
3877 case TPACPI_VIDEO_NEW
:
3878 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
3879 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
3885 return (res
)? 0 : -EIO
;
3888 static int video_autosw_get(void)
3892 switch (video_supported
) {
3893 case TPACPI_VIDEO_570
:
3894 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
3897 case TPACPI_VIDEO_770
:
3898 case TPACPI_VIDEO_NEW
:
3899 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
3909 static int video_autosw_set(int enable
)
3911 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
3916 static int video_outputsw_cycle(void)
3918 int autosw
= video_autosw_get();
3924 switch (video_supported
) {
3925 case TPACPI_VIDEO_570
:
3926 res
= video_autosw_set(1);
3929 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
3931 case TPACPI_VIDEO_770
:
3932 case TPACPI_VIDEO_NEW
:
3933 res
= video_autosw_set(1);
3936 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
3941 if (!autosw
&& video_autosw_set(autosw
)) {
3943 "video auto-switch left enabled due to error\n");
3947 return (res
)? 0 : -EIO
;
3950 static int video_expand_toggle(void)
3952 switch (video_supported
) {
3953 case TPACPI_VIDEO_570
:
3954 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
3956 case TPACPI_VIDEO_770
:
3957 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
3959 case TPACPI_VIDEO_NEW
:
3960 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
3968 static int video_read(char *p
)
3973 if (video_supported
== TPACPI_VIDEO_NONE
) {
3974 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3978 status
= video_outputsw_get();
3982 autosw
= video_autosw_get();
3986 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
3987 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
3988 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
3989 if (video_supported
== TPACPI_VIDEO_NEW
)
3990 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
3991 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
3992 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
3993 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
3994 if (video_supported
== TPACPI_VIDEO_NEW
)
3995 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
3996 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
3997 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4002 static int video_write(char *buf
)
4005 int enable
, disable
, status
;
4008 if (video_supported
== TPACPI_VIDEO_NONE
)
4014 while ((cmd
= next_cmd(&buf
))) {
4015 if (strlencmp(cmd
, "lcd_enable") == 0) {
4016 enable
|= TP_ACPI_VIDEO_S_LCD
;
4017 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4018 disable
|= TP_ACPI_VIDEO_S_LCD
;
4019 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4020 enable
|= TP_ACPI_VIDEO_S_CRT
;
4021 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4022 disable
|= TP_ACPI_VIDEO_S_CRT
;
4023 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4024 strlencmp(cmd
, "dvi_enable") == 0) {
4025 enable
|= TP_ACPI_VIDEO_S_DVI
;
4026 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4027 strlencmp(cmd
, "dvi_disable") == 0) {
4028 disable
|= TP_ACPI_VIDEO_S_DVI
;
4029 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4030 res
= video_autosw_set(1);
4033 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4034 res
= video_autosw_set(0);
4037 } else if (strlencmp(cmd
, "video_switch") == 0) {
4038 res
= video_outputsw_cycle();
4041 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4042 res
= video_expand_toggle();
4049 if (enable
|| disable
) {
4050 status
= video_outputsw_get();
4053 res
= video_outputsw_set((status
& ~disable
) | enable
);
4061 static struct ibm_struct video_driver_data
= {
4064 .write
= video_write
,
4068 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4070 /*************************************************************************
4071 * Light (thinklight) subdriver
4074 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4075 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4077 static int light_get_status(void)
4081 if (tp_features
.light_status
) {
4082 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4090 static int light_set_status(int status
)
4094 if (tp_features
.light
) {
4096 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4098 TP_CMOS_THINKLIGHT_ON
:
4099 TP_CMOS_THINKLIGHT_OFF
);
4101 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4104 return (rc
)? 0 : -EIO
;
4110 static void light_set_status_worker(struct work_struct
*work
)
4112 struct tpacpi_led_classdev
*data
=
4113 container_of(work
, struct tpacpi_led_classdev
, work
);
4115 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4116 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4119 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4120 enum led_brightness brightness
)
4122 struct tpacpi_led_classdev
*data
=
4123 container_of(led_cdev
,
4124 struct tpacpi_led_classdev
,
4126 data
->new_state
= (brightness
!= LED_OFF
) ?
4127 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4128 queue_work(tpacpi_wq
, &data
->work
);
4131 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4133 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4136 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4138 .name
= "tpacpi::thinklight",
4139 .brightness_set
= &light_sysfs_set
,
4140 .brightness_get
= &light_sysfs_get
,
4144 static int __init
light_init(struct ibm_init_struct
*iibm
)
4148 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4150 TPACPI_ACPIHANDLE_INIT(ledb
);
4151 TPACPI_ACPIHANDLE_INIT(lght
);
4152 TPACPI_ACPIHANDLE_INIT(cmos
);
4153 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4155 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4156 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4158 if (tp_features
.light
)
4159 /* light status not supported on
4160 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4161 tp_features
.light_status
=
4162 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4164 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4165 str_supported(tp_features
.light
),
4166 str_supported(tp_features
.light_status
));
4168 if (!tp_features
.light
)
4171 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4172 &tpacpi_led_thinklight
.led_classdev
);
4175 tp_features
.light
= 0;
4176 tp_features
.light_status
= 0;
4184 static void light_exit(void)
4186 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4187 if (work_pending(&tpacpi_led_thinklight
.work
))
4188 flush_workqueue(tpacpi_wq
);
4191 static int light_read(char *p
)
4196 if (!tp_features
.light
) {
4197 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4198 } else if (!tp_features
.light_status
) {
4199 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4200 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4202 status
= light_get_status();
4205 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4206 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4212 static int light_write(char *buf
)
4217 if (!tp_features
.light
)
4220 while ((cmd
= next_cmd(&buf
))) {
4221 if (strlencmp(cmd
, "on") == 0) {
4223 } else if (strlencmp(cmd
, "off") == 0) {
4229 return light_set_status(newstatus
);
4232 static struct ibm_struct light_driver_data
= {
4235 .write
= light_write
,
4239 /*************************************************************************
4243 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4245 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
4246 static int dock_read(char *p
);
4247 static int dock_write(char *buf
);
4249 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
4250 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4251 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4252 "\\_SB.PCI.ISA.SLCE", /* 570 */
4253 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4255 /* don't list other alternatives as we install a notify handler on the 570 */
4256 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
4258 static const struct acpi_device_id ibm_pci_device_ids
[] = {
4259 {PCI_ROOT_HID_STRING
, 0},
4263 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
4265 .notify
= dock_notify
,
4266 .handle
= &dock_handle
,
4267 .type
= ACPI_SYSTEM_NOTIFY
,
4270 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4271 * We just use it to get notifications of dock hotplug
4272 * in very old thinkpads */
4273 .hid
= ibm_pci_device_ids
,
4274 .notify
= dock_notify
,
4275 .handle
= &pci_handle
,
4276 .type
= ACPI_SYSTEM_NOTIFY
,
4280 static struct ibm_struct dock_driver_data
[2] = {
4284 .write
= dock_write
,
4285 .acpi
= &ibm_dock_acpidriver
[0],
4289 .acpi
= &ibm_dock_acpidriver
[1],
4293 #define dock_docked() (_sta(dock_handle) & 1)
4295 static int __init
dock_init(struct ibm_init_struct
*iibm
)
4297 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
4299 TPACPI_ACPIHANDLE_INIT(dock
);
4301 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
4302 str_supported(dock_handle
!= NULL
));
4304 return (dock_handle
)? 0 : 1;
4307 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
4311 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
4313 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
4314 dock_driver_data
[0].flags
.acpi_notify_installed
) {
4315 TPACPI_ACPIHANDLE_INIT(pci
);
4316 dock2_needed
= (pci_handle
!= NULL
);
4317 vdbg_printk(TPACPI_DBG_INIT
,
4318 "dock PCI handler for the TP 570 is %s\n",
4319 str_supported(dock2_needed
));
4321 vdbg_printk(TPACPI_DBG_INIT
,
4322 "dock subdriver part 2 not required\n");
4326 return (dock2_needed
)? 0 : 1;
4329 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
4331 int docked
= dock_docked();
4332 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
4333 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
4336 if (event
== 1 && !pci
) /* 570 */
4337 data
= 1; /* button */
4338 else if (event
== 1 && pci
) /* 570 */
4339 data
= 3; /* dock */
4340 else if (event
== 3 && docked
)
4341 data
= 1; /* button */
4342 else if (event
== 3 && !docked
)
4343 data
= 2; /* undock */
4344 else if (event
== 0 && docked
)
4345 data
= 3; /* dock */
4347 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
4348 event
, _sta(dock_handle
));
4349 data
= 0; /* unknown */
4351 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
4352 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4353 dev_name(&ibm
->acpi
->device
->dev
),
4357 static int dock_read(char *p
)
4360 int docked
= dock_docked();
4363 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4365 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
4367 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
4368 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
4374 static int dock_write(char *buf
)
4381 while ((cmd
= next_cmd(&buf
))) {
4382 if (strlencmp(cmd
, "undock") == 0) {
4383 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
4384 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
4386 } else if (strlencmp(cmd
, "dock") == 0) {
4387 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
4396 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4398 /*************************************************************************
4402 #ifdef CONFIG_THINKPAD_ACPI_BAY
4404 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4405 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4406 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4407 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4408 ); /* A21e, R30, R31 */
4409 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
4410 "_EJ0", /* all others */
4411 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4412 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4413 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4415 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
4419 static int __init
bay_init(struct ibm_init_struct
*iibm
)
4421 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
4423 TPACPI_ACPIHANDLE_INIT(bay
);
4425 TPACPI_ACPIHANDLE_INIT(bay_ej
);
4426 TPACPI_ACPIHANDLE_INIT(bay2
);
4428 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
4430 tp_features
.bay_status
= bay_handle
&&
4431 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
4432 tp_features
.bay_status2
= bay2_handle
&&
4433 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
4435 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
4436 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
4437 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
4438 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
4440 vdbg_printk(TPACPI_DBG_INIT
,
4441 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4442 str_supported(tp_features
.bay_status
),
4443 str_supported(tp_features
.bay_eject
),
4444 str_supported(tp_features
.bay_status2
),
4445 str_supported(tp_features
.bay_eject2
));
4447 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
4448 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
4451 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
4453 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
4454 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4455 dev_name(&ibm
->acpi
->device
->dev
),
4459 #define bay_occupied(b) (_sta(b##_handle) & 1)
4461 static int bay_read(char *p
)
4464 int occupied
= bay_occupied(bay
);
4465 int occupied2
= bay_occupied(bay2
);
4468 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
4469 tp_features
.bay_status
?
4470 (occupied
? "occupied" : "unoccupied") :
4472 if (tp_features
.bay_status2
)
4473 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
4474 "occupied" : "unoccupied");
4476 eject
= tp_features
.bay_eject
&& occupied
;
4477 eject2
= tp_features
.bay_eject2
&& occupied2
;
4479 if (eject
&& eject2
)
4480 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
4482 len
+= sprintf(p
+ len
, "commands:\teject\n");
4484 len
+= sprintf(p
+ len
, "commands:\teject2\n");
4489 static int bay_write(char *buf
)
4493 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
4496 while ((cmd
= next_cmd(&buf
))) {
4497 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
4498 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
4500 } else if (tp_features
.bay_eject2
&&
4501 strlencmp(cmd
, "eject2") == 0) {
4502 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
4511 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
4512 .notify
= bay_notify
,
4513 .handle
= &bay_handle
,
4514 .type
= ACPI_SYSTEM_NOTIFY
,
4517 static struct ibm_struct bay_driver_data
= {
4521 .acpi
= &ibm_bay_acpidriver
,
4524 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4526 /*************************************************************************
4530 /* sysfs cmos_command -------------------------------------------------- */
4531 static ssize_t
cmos_command_store(struct device
*dev
,
4532 struct device_attribute
*attr
,
4533 const char *buf
, size_t count
)
4535 unsigned long cmos_cmd
;
4538 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4541 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4542 return (res
)? res
: count
;
4545 static struct device_attribute dev_attr_cmos_command
=
4546 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4548 /* --------------------------------------------------------------------- */
4550 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4554 vdbg_printk(TPACPI_DBG_INIT
,
4555 "initializing cmos commands subdriver\n");
4557 TPACPI_ACPIHANDLE_INIT(cmos
);
4559 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4560 str_supported(cmos_handle
!= NULL
));
4562 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4566 return (cmos_handle
)? 0 : 1;
4569 static void cmos_exit(void)
4571 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4574 static int cmos_read(char *p
)
4578 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4579 R30, R31, T20-22, X20-21 */
4581 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4583 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4584 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4590 static int cmos_write(char *buf
)
4595 while ((cmd
= next_cmd(&buf
))) {
4596 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4597 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4602 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4610 static struct ibm_struct cmos_driver_data
= {
4613 .write
= cmos_write
,
4617 /*************************************************************************
4621 enum led_access_mode
{
4622 TPACPI_LED_NONE
= 0,
4623 TPACPI_LED_570
, /* 570 */
4624 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4625 TPACPI_LED_NEW
, /* all others */
4628 enum { /* For TPACPI_LED_OLD */
4629 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4630 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4631 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4634 static enum led_access_mode led_supported
;
4636 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4637 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4638 /* T20-22, X20-21 */
4639 "LED", /* all others */
4642 #define TPACPI_LED_NUMLEDS 8
4643 static struct tpacpi_led_classdev
*tpacpi_leds
;
4644 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
4645 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
4646 /* there's a limit of 19 chars + NULL before 2.6.26 */
4648 "tpacpi:orange:batt",
4649 "tpacpi:green:batt",
4650 "tpacpi::dock_active",
4651 "tpacpi::bay_active",
4652 "tpacpi::dock_batt",
4653 "tpacpi::unknown_led",
4657 static int led_get_status(const unsigned int led
)
4660 enum led_status_t led_s
;
4662 switch (led_supported
) {
4663 case TPACPI_LED_570
:
4664 if (!acpi_evalf(ec_handle
,
4665 &status
, "GLED", "dd", 1 << led
))
4667 led_s
= (status
== 0)?
4672 tpacpi_led_state_cache
[led
] = led_s
;
4681 static int led_set_status(const unsigned int led
,
4682 const enum led_status_t ledstatus
)
4684 /* off, on, blink. Index is led_status_t */
4685 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
4686 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
4690 switch (led_supported
) {
4691 case TPACPI_LED_570
:
4695 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4696 (1 << led
), led_sled_arg1
[ledstatus
]))
4699 case TPACPI_LED_OLD
:
4700 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4703 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
4705 rc
= ec_write(TPACPI_LED_EC_HLBL
,
4706 (ledstatus
== TPACPI_LED_BLINK
) << led
);
4708 rc
= ec_write(TPACPI_LED_EC_HLCL
,
4709 (ledstatus
!= TPACPI_LED_OFF
) << led
);
4711 case TPACPI_LED_NEW
:
4713 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4714 led
, led_led_arg1
[ledstatus
]))
4722 tpacpi_led_state_cache
[led
] = ledstatus
;
4727 static void led_set_status_worker(struct work_struct
*work
)
4729 struct tpacpi_led_classdev
*data
=
4730 container_of(work
, struct tpacpi_led_classdev
, work
);
4732 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4733 led_set_status(data
->led
, data
->new_state
);
4736 static void led_sysfs_set(struct led_classdev
*led_cdev
,
4737 enum led_brightness brightness
)
4739 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4740 struct tpacpi_led_classdev
, led_classdev
);
4742 if (brightness
== LED_OFF
)
4743 data
->new_state
= TPACPI_LED_OFF
;
4744 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
4745 data
->new_state
= TPACPI_LED_ON
;
4747 data
->new_state
= TPACPI_LED_BLINK
;
4749 queue_work(tpacpi_wq
, &data
->work
);
4752 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
4753 unsigned long *delay_on
, unsigned long *delay_off
)
4755 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4756 struct tpacpi_led_classdev
, led_classdev
);
4758 /* Can we choose the flash rate? */
4759 if (*delay_on
== 0 && *delay_off
== 0) {
4760 /* yes. set them to the hardware blink rate (1 Hz) */
4761 *delay_on
= 500; /* ms */
4762 *delay_off
= 500; /* ms */
4763 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
4766 data
->new_state
= TPACPI_LED_BLINK
;
4767 queue_work(tpacpi_wq
, &data
->work
);
4772 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
4776 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4777 struct tpacpi_led_classdev
, led_classdev
);
4779 rc
= led_get_status(data
->led
);
4781 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
4782 rc
= LED_OFF
; /* no error handling in led class :( */
4789 static void led_exit(void)
4793 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4794 if (tpacpi_leds
[i
].led_classdev
.name
)
4795 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
4801 static int __init
led_init(struct ibm_init_struct
*iibm
)
4806 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
4808 TPACPI_ACPIHANDLE_INIT(led
);
4811 /* led not supported on R30, R31 */
4812 led_supported
= TPACPI_LED_NONE
;
4813 else if (strlencmp(led_path
, "SLED") == 0)
4815 led_supported
= TPACPI_LED_570
;
4816 else if (strlencmp(led_path
, "SYSL") == 0)
4817 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4818 led_supported
= TPACPI_LED_OLD
;
4821 led_supported
= TPACPI_LED_NEW
;
4823 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
4824 str_supported(led_supported
), led_supported
);
4826 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
4829 printk(TPACPI_ERR
"Out of memory for LED data\n");
4833 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4834 tpacpi_leds
[i
].led
= i
;
4836 tpacpi_leds
[i
].led_classdev
.brightness_set
= &led_sysfs_set
;
4837 tpacpi_leds
[i
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
4838 if (led_supported
== TPACPI_LED_570
)
4839 tpacpi_leds
[i
].led_classdev
.brightness_get
=
4842 tpacpi_leds
[i
].led_classdev
.name
= tpacpi_led_names
[i
];
4844 INIT_WORK(&tpacpi_leds
[i
].work
, led_set_status_worker
);
4846 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4847 &tpacpi_leds
[i
].led_classdev
);
4849 tpacpi_leds
[i
].led_classdev
.name
= NULL
;
4855 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
4858 #define str_led_status(s) \
4859 ((s) == TPACPI_LED_OFF ? "off" : \
4860 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4862 static int led_read(char *p
)
4866 if (!led_supported
) {
4867 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4870 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4872 if (led_supported
== TPACPI_LED_570
) {
4875 for (i
= 0; i
< 8; i
++) {
4876 status
= led_get_status(i
);
4879 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
4880 i
, str_led_status(status
));
4884 len
+= sprintf(p
+ len
, "commands:\t"
4885 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4890 static int led_write(char *buf
)
4894 enum led_status_t s
;
4899 while ((cmd
= next_cmd(&buf
))) {
4900 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
4903 if (strstr(cmd
, "off")) {
4905 } else if (strstr(cmd
, "on")) {
4907 } else if (strstr(cmd
, "blink")) {
4908 s
= TPACPI_LED_BLINK
;
4913 rc
= led_set_status(led
, s
);
4921 static struct ibm_struct led_driver_data
= {
4928 /*************************************************************************
4932 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
4934 static int __init
beep_init(struct ibm_init_struct
*iibm
)
4936 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
4938 TPACPI_ACPIHANDLE_INIT(beep
);
4940 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
4941 str_supported(beep_handle
!= NULL
));
4943 return (beep_handle
)? 0 : 1;
4946 static int beep_read(char *p
)
4951 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4953 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4954 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
4960 static int beep_write(char *buf
)
4968 while ((cmd
= next_cmd(&buf
))) {
4969 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
4970 beep_cmd
>= 0 && beep_cmd
<= 17) {
4974 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
4981 static struct ibm_struct beep_driver_data
= {
4984 .write
= beep_write
,
4987 /*************************************************************************
4991 enum thermal_access_mode
{
4992 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
4993 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
4994 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
4995 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
4996 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
4999 enum { /* TPACPI_THERMAL_TPEC_* */
5000 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5001 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5002 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5005 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5006 struct ibm_thermal_sensors_struct
{
5007 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5010 static enum thermal_access_mode thermal_read_mode
;
5012 /* idx is zero-based */
5013 static int thermal_get_sensor(int idx
, s32
*value
)
5019 t
= TP_EC_THERMAL_TMP0
;
5021 switch (thermal_read_mode
) {
5022 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5023 case TPACPI_THERMAL_TPEC_16
:
5024 if (idx
>= 8 && idx
<= 15) {
5025 t
= TP_EC_THERMAL_TMP8
;
5030 case TPACPI_THERMAL_TPEC_8
:
5032 if (!acpi_ec_read(t
+ idx
, &tmp
))
5034 *value
= tmp
* 1000;
5039 case TPACPI_THERMAL_ACPI_UPDT
:
5041 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5042 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5044 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5046 *value
= (t
- 2732) * 100;
5051 case TPACPI_THERMAL_ACPI_TMP07
:
5053 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5054 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5056 if (t
> 127 || t
< -127)
5057 t
= TP_EC_THERMAL_TMP_NA
;
5063 case TPACPI_THERMAL_NONE
:
5071 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5082 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5085 for (i
= 0 ; i
< n
; i
++) {
5086 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5094 /* sysfs temp##_input -------------------------------------------------- */
5096 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5097 struct device_attribute
*attr
,
5100 struct sensor_device_attribute
*sensor_attr
=
5101 to_sensor_dev_attr(attr
);
5102 int idx
= sensor_attr
->index
;
5106 res
= thermal_get_sensor(idx
, &value
);
5109 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
5112 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5115 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5116 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5117 thermal_temp_input_show, NULL, _idxB)
5119 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5120 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5121 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5122 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5123 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5124 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5125 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5126 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5127 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5128 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5129 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5130 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5131 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5132 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5133 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5134 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5135 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5138 #define THERMAL_ATTRS(X) \
5139 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5141 static struct attribute
*thermal_temp_input_attr
[] = {
5161 static const struct attribute_group thermal_temp_input16_group
= {
5162 .attrs
= thermal_temp_input_attr
5165 static const struct attribute_group thermal_temp_input8_group
= {
5166 .attrs
= &thermal_temp_input_attr
[8]
5169 #undef THERMAL_SENSOR_ATTR_TEMP
5170 #undef THERMAL_ATTRS
5172 /* --------------------------------------------------------------------- */
5174 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5181 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5183 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5185 if (thinkpad_id
.ec_model
) {
5187 * Direct EC access mode: sensors at registers
5188 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5189 * non-implemented, thermal sensors return 0x80 when
5194 for (i
= 0; i
< 8; i
++) {
5195 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5201 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5209 /* This is sheer paranoia, but we handle it anyway */
5212 "ThinkPad ACPI EC access misbehaving, "
5213 "falling back to ACPI TMPx access "
5215 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5218 "ThinkPad ACPI EC access misbehaving, "
5219 "disabling thermal sensors access\n");
5220 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5225 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5227 } else if (acpi_tmp7
) {
5228 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5229 /* 600e/x, 770e, 770x */
5230 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5232 /* Standard ACPI TMPx access, max 8 sensors */
5233 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5236 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5237 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5240 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5241 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5244 switch (thermal_read_mode
) {
5245 case TPACPI_THERMAL_TPEC_16
:
5246 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5247 &thermal_temp_input16_group
);
5251 case TPACPI_THERMAL_TPEC_8
:
5252 case TPACPI_THERMAL_ACPI_TMP07
:
5253 case TPACPI_THERMAL_ACPI_UPDT
:
5254 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5255 &thermal_temp_input8_group
);
5259 case TPACPI_THERMAL_NONE
:
5267 static void thermal_exit(void)
5269 switch (thermal_read_mode
) {
5270 case TPACPI_THERMAL_TPEC_16
:
5271 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5272 &thermal_temp_input16_group
);
5274 case TPACPI_THERMAL_TPEC_8
:
5275 case TPACPI_THERMAL_ACPI_TMP07
:
5276 case TPACPI_THERMAL_ACPI_UPDT
:
5277 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5278 &thermal_temp_input16_group
);
5280 case TPACPI_THERMAL_NONE
:
5286 static int thermal_read(char *p
)
5290 struct ibm_thermal_sensors_struct t
;
5292 n
= thermal_get_sensors(&t
);
5293 if (unlikely(n
< 0))
5296 len
+= sprintf(p
+ len
, "temperatures:\t");
5299 for (i
= 0; i
< (n
- 1); i
++)
5300 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5301 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5303 len
+= sprintf(p
+ len
, "not supported\n");
5308 static struct ibm_struct thermal_driver_data
= {
5310 .read
= thermal_read
,
5311 .exit
= thermal_exit
,
5314 /*************************************************************************
5318 static u8 ecdump_regs
[256];
5320 static int ecdump_read(char *p
)
5326 len
+= sprintf(p
+ len
, "EC "
5327 " +00 +01 +02 +03 +04 +05 +06 +07"
5328 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5329 for (i
= 0; i
< 256; i
+= 16) {
5330 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5331 for (j
= 0; j
< 16; j
++) {
5332 if (!acpi_ec_read(i
+ j
, &v
))
5334 if (v
!= ecdump_regs
[i
+ j
])
5335 len
+= sprintf(p
+ len
, " *%02x", v
);
5337 len
+= sprintf(p
+ len
, " %02x", v
);
5338 ecdump_regs
[i
+ j
] = v
;
5340 len
+= sprintf(p
+ len
, "\n");
5345 /* These are way too dangerous to advertise openly... */
5347 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5348 " (<offset> is 00-ff, <value> is 00-ff)\n");
5349 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5350 " (<offset> is 00-ff, <value> is 0-255)\n");
5355 static int ecdump_write(char *buf
)
5360 while ((cmd
= next_cmd(&buf
))) {
5361 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5363 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5367 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5368 if (!acpi_ec_write(i
, v
))
5377 static struct ibm_struct ecdump_driver_data
= {
5379 .read
= ecdump_read
,
5380 .write
= ecdump_write
,
5381 .flags
.experimental
= 1,
5384 /*************************************************************************
5385 * Backlight/brightness subdriver
5388 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5391 TP_EC_BACKLIGHT
= 0x31,
5393 /* TP_EC_BACKLIGHT bitmasks */
5394 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5395 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5396 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5399 static struct backlight_device
*ibm_backlight_device
;
5400 static int brightness_mode
;
5401 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5403 static struct mutex brightness_mutex
;
5406 * ThinkPads can read brightness from two places: EC 0x31, or
5407 * CMOS NVRAM byte 0x5E, bits 0-3.
5409 * EC 0x31 has the following layout
5410 * Bit 7: unknown function
5411 * Bit 6: unknown function
5412 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5413 * Bit 4: must be set to zero to avoid problems
5414 * Bit 3-0: backlight brightness level
5416 * brightness_get_raw returns status data in the EC 0x31 layout
5418 static int brightness_get_raw(int *status
)
5420 u8 lec
= 0, lcmos
= 0, level
= 0;
5422 if (brightness_mode
& 1) {
5423 if (!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
))
5425 level
= lec
& TP_EC_BACKLIGHT_LVLMSK
;
5427 if (brightness_mode
& 2) {
5428 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5429 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5430 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5431 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
5435 if (brightness_mode
== 3) {
5436 *status
= lec
; /* Prefer EC, CMOS is just a backing store */
5437 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5439 tp_warned
.bright_cmos_ec_unsync
= 0;
5441 if (!tp_warned
.bright_cmos_ec_unsync
) {
5443 "CMOS NVRAM (%u) and EC (%u) do not "
5444 "agree on display brightness level\n",
5445 (unsigned int) lcmos
,
5446 (unsigned int) lec
);
5447 tp_warned
.bright_cmos_ec_unsync
= 1;
5458 /* May return EINTR which can always be mapped to ERESTARTSYS */
5459 static int brightness_set(int value
)
5461 int cmos_cmd
, inc
, i
, res
;
5465 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
5469 res
= mutex_lock_killable(&brightness_mutex
);
5473 res
= brightness_get_raw(¤t_value
);
5477 command_bits
= current_value
& TP_EC_BACKLIGHT_CMDMSK
;
5478 current_value
&= TP_EC_BACKLIGHT_LVLMSK
;
5480 cmos_cmd
= value
> current_value
?
5481 TP_CMOS_BRIGHTNESS_UP
:
5482 TP_CMOS_BRIGHTNESS_DOWN
;
5483 inc
= (value
> current_value
)? 1 : -1;
5486 for (i
= current_value
; i
!= value
; i
+= inc
) {
5487 if ((brightness_mode
& 2) &&
5488 issue_thinkpad_cmos_command(cmos_cmd
)) {
5492 if ((brightness_mode
& 1) &&
5493 !acpi_ec_write(TP_EC_BACKLIGHT
,
5494 (i
+ inc
) | command_bits
)) {
5501 mutex_unlock(&brightness_mutex
);
5505 /* sysfs backlight class ----------------------------------------------- */
5507 static int brightness_update_status(struct backlight_device
*bd
)
5509 /* it is the backlight class's job (caller) to handle
5510 * EINTR and other errors properly */
5511 return brightness_set(
5512 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
5513 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
5514 bd
->props
.brightness
: 0);
5517 static int brightness_get(struct backlight_device
*bd
)
5521 res
= brightness_get_raw(&status
);
5523 return 0; /* FIXME: teach backlight about error handling */
5525 return status
& TP_EC_BACKLIGHT_LVLMSK
;
5528 static struct backlight_ops ibm_backlight_data
= {
5529 .get_brightness
= brightness_get
,
5530 .update_status
= brightness_update_status
,
5533 /* --------------------------------------------------------------------- */
5535 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
5539 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
5541 mutex_init(&brightness_mutex
);
5544 * We always attempt to detect acpi support, so as to switch
5545 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5546 * going to publish a backlight interface
5548 b
= tpacpi_check_std_acpi_brightness_support();
5550 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
5551 printk(TPACPI_NOTICE
5552 "Lenovo BIOS switched to ACPI backlight "
5555 if (brightness_enable
> 1) {
5556 printk(TPACPI_NOTICE
5557 "standard ACPI backlight interface "
5558 "available, not loading native one...\n");
5563 if (!brightness_enable
) {
5564 dbg_printk(TPACPI_DBG_INIT
,
5565 "brightness support disabled by "
5566 "module parameter\n");
5572 "Unsupported brightness interface, "
5573 "please contact %s\n", TPACPI_MAIL
);
5577 tp_features
.bright_16levels
= 1;
5579 if (!brightness_mode
) {
5580 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
5581 brightness_mode
= 2;
5583 brightness_mode
= 3;
5585 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
5589 if (brightness_mode
> 3)
5592 if (brightness_get_raw(&b
) < 0)
5595 if (tp_features
.bright_16levels
)
5597 "detected a 16-level brightness capable ThinkPad\n");
5599 ibm_backlight_device
= backlight_device_register(
5600 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
5601 &ibm_backlight_data
);
5602 if (IS_ERR(ibm_backlight_device
)) {
5603 printk(TPACPI_ERR
"Could not register backlight device\n");
5604 return PTR_ERR(ibm_backlight_device
);
5606 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
5608 ibm_backlight_device
->props
.max_brightness
=
5609 (tp_features
.bright_16levels
)? 15 : 7;
5610 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
5611 backlight_update_status(ibm_backlight_device
);
5616 static void brightness_exit(void)
5618 if (ibm_backlight_device
) {
5619 vdbg_printk(TPACPI_DBG_EXIT
,
5620 "calling backlight_device_unregister()\n");
5621 backlight_device_unregister(ibm_backlight_device
);
5625 static int brightness_read(char *p
)
5630 level
= brightness_get(NULL
);
5632 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5634 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
5635 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
5636 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5637 " (<level> is 0-%d)\n",
5638 (tp_features
.bright_16levels
) ? 15 : 7);
5644 static int brightness_write(char *buf
)
5649 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
5651 level
= brightness_get(NULL
);
5655 while ((cmd
= next_cmd(&buf
))) {
5656 if (strlencmp(cmd
, "up") == 0) {
5657 if (level
< max_level
)
5659 } else if (strlencmp(cmd
, "down") == 0) {
5662 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
5663 level
>= 0 && level
<= max_level
) {
5670 * Now we know what the final level should be, so we try to set it.
5671 * Doing it this way makes the syscall restartable in case of EINTR
5673 rc
= brightness_set(level
);
5674 return (rc
== -EINTR
)? -ERESTARTSYS
: rc
;
5677 static struct ibm_struct brightness_driver_data
= {
5678 .name
= "brightness",
5679 .read
= brightness_read
,
5680 .write
= brightness_write
,
5681 .exit
= brightness_exit
,
5684 /*************************************************************************
5688 static int volume_offset
= 0x30;
5690 static int volume_read(char *p
)
5695 if (!acpi_ec_read(volume_offset
, &level
)) {
5696 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5698 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
5699 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
5700 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
5701 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5702 " (<level> is 0-15)\n");
5708 static int volume_write(char *buf
)
5710 int cmos_cmd
, inc
, i
;
5712 int new_level
, new_mute
;
5715 while ((cmd
= next_cmd(&buf
))) {
5716 if (!acpi_ec_read(volume_offset
, &level
))
5718 new_mute
= mute
= level
& 0x40;
5719 new_level
= level
= level
& 0xf;
5721 if (strlencmp(cmd
, "up") == 0) {
5725 new_level
= level
== 15 ? 15 : level
+ 1;
5726 } else if (strlencmp(cmd
, "down") == 0) {
5730 new_level
= level
== 0 ? 0 : level
- 1;
5731 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
5732 new_level
>= 0 && new_level
<= 15) {
5734 } else if (strlencmp(cmd
, "mute") == 0) {
5739 if (new_level
!= level
) {
5740 /* mute doesn't change */
5742 cmos_cmd
= (new_level
> level
) ?
5743 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
5744 inc
= new_level
> level
? 1 : -1;
5746 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
5747 !acpi_ec_write(volume_offset
, level
)))
5750 for (i
= level
; i
!= new_level
; i
+= inc
)
5751 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5752 !acpi_ec_write(volume_offset
, i
+ inc
))
5756 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
5757 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
5762 if (new_mute
!= mute
) {
5763 /* level doesn't change */
5765 cmos_cmd
= (new_mute
) ?
5766 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
5768 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5769 !acpi_ec_write(volume_offset
, level
+ new_mute
))
5777 static struct ibm_struct volume_driver_data
= {
5779 .read
= volume_read
,
5780 .write
= volume_write
,
5783 /*************************************************************************
5790 * TPACPI_FAN_RD_ACPI_GFAN:
5791 * ACPI GFAN method: returns fan level
5793 * see TPACPI_FAN_WR_ACPI_SFAN
5794 * EC 0x2f (HFSP) not available if GFAN exists
5796 * TPACPI_FAN_WR_ACPI_SFAN:
5797 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5799 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5802 * TPACPI_FAN_WR_TPEC:
5803 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5804 * Supported on almost all ThinkPads
5806 * Fan speed changes of any sort (including those caused by the
5807 * disengaged mode) are usually done slowly by the firmware as the
5808 * maximum ammount of fan duty cycle change per second seems to be
5811 * Reading is not available if GFAN exists.
5812 * Writing is not available if SFAN exists.
5815 * 7 automatic mode engaged;
5816 * (default operation mode of the ThinkPad)
5817 * fan level is ignored in this mode.
5818 * 6 full speed mode (takes precedence over bit 7);
5819 * not available on all thinkpads. May disable
5820 * the tachometer while the fan controller ramps up
5821 * the speed (which can take up to a few *minutes*).
5822 * Speeds up fan to 100% duty-cycle, which is far above
5823 * the standard RPM levels. It is not impossible that
5824 * it could cause hardware damage.
5825 * 5-3 unused in some models. Extra bits for fan level
5826 * in others, but still useless as all values above
5827 * 7 map to the same speed as level 7 in these models.
5828 * 2-0 fan level (0..7 usually)
5830 * 0x07 = max (set when temperatures critical)
5831 * Some ThinkPads may have other levels, see
5832 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5834 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5835 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5836 * does so, its initial value is meaningless (0x07).
5838 * For firmware bugs, refer to:
5839 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5843 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5844 * Main fan tachometer reading (in RPM)
5846 * This register is present on all ThinkPads with a new-style EC, and
5847 * it is known not to be present on the A21m/e, and T22, as there is
5848 * something else in offset 0x84 according to the ACPI DSDT. Other
5849 * ThinkPads from this same time period (and earlier) probably lack the
5850 * tachometer as well.
5852 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5853 * was never fixed by IBM to report the EC firmware version string
5854 * probably support the tachometer (like the early X models), so
5855 * detecting it is quite hard. We need more data to know for sure.
5857 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5860 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5863 * For firmware bugs, refer to:
5864 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5866 * TPACPI_FAN_WR_ACPI_FANS:
5867 * ThinkPad X31, X40, X41. Not available in the X60.
5869 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5870 * high speed. ACPI DSDT seems to map these three speeds to levels
5871 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5872 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5874 * The speeds are stored on handles
5875 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5877 * There are three default speed sets, acessible as handles:
5878 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5880 * ACPI DSDT switches which set is in use depending on various
5883 * TPACPI_FAN_WR_TPEC is also available and should be used to
5884 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5885 * but the ACPI tables just mention level 7.
5888 enum { /* Fan control constants */
5889 fan_status_offset
= 0x2f, /* EC register 0x2f */
5890 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5891 * 0x84 must be read before 0x85 */
5893 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
5894 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
5896 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
5899 enum fan_status_access_mode
{
5900 TPACPI_FAN_NONE
= 0, /* No fan status or control */
5901 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
5902 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5905 enum fan_control_access_mode
{
5906 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
5907 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
5908 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
5909 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
5912 enum fan_control_commands
{
5913 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
5914 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
5915 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
5916 * and also watchdog cmd */
5919 static int fan_control_allowed
;
5921 static enum fan_status_access_mode fan_status_access_mode
;
5922 static enum fan_control_access_mode fan_control_access_mode
;
5923 static enum fan_control_commands fan_control_commands
;
5925 static u8 fan_control_initial_status
;
5926 static u8 fan_control_desired_level
;
5927 static u8 fan_control_resume_level
;
5928 static int fan_watchdog_maxinterval
;
5930 static struct mutex fan_mutex
;
5932 static void fan_watchdog_fire(struct work_struct
*ignored
);
5933 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
5935 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
5936 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
5937 "\\FSPD", /* 600e/x, 770e, 770x */
5939 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
5940 "JFNS", /* 770x-JL */
5944 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5945 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5946 * be in auto mode (0x80).
5948 * This is corrected by any write to HFSP either by the driver, or
5951 * We assume 0x07 really means auto mode while this quirk is active,
5952 * as this is far more likely than the ThinkPad being in level 7,
5953 * which is only used by the firmware during thermal emergencies.
5956 static void fan_quirk1_detect(void)
5958 /* In some ThinkPads, neither the EC nor the ACPI
5959 * DSDT initialize the HFSP register, and it ends up
5960 * being initially set to 0x07 when it *could* be
5961 * either 0x07 or 0x80.
5963 * Enable for TP-1Y (T43), TP-78 (R51e),
5964 * TP-76 (R52), TP-70 (T43, R52), which are known
5966 if (fan_control_initial_status
== 0x07) {
5967 switch (thinkpad_id
.ec_model
) {
5968 case 0x5931: /* TP-1Y */
5969 case 0x3837: /* TP-78 */
5970 case 0x3637: /* TP-76 */
5971 case 0x3037: /* TP-70 */
5972 printk(TPACPI_NOTICE
5973 "fan_init: initial fan status is unknown, "
5974 "assuming it is in auto mode\n");
5975 tp_features
.fan_ctrl_status_undef
= 1;
5981 static void fan_quirk1_handle(u8
*fan_status
)
5983 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
5984 if (*fan_status
!= fan_control_initial_status
) {
5985 /* something changed the HFSP regisnter since
5986 * driver init time, so it is not undefined
5988 tp_features
.fan_ctrl_status_undef
= 0;
5990 /* Return most likely status. In fact, it
5991 * might be the only possible status */
5992 *fan_status
= TP_EC_FAN_AUTO
;
5998 * Call with fan_mutex held
6000 static void fan_update_desired_level(u8 status
)
6003 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6005 fan_control_desired_level
= 7;
6007 fan_control_desired_level
= status
;
6011 static int fan_get_status(u8
*status
)
6016 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6018 switch (fan_status_access_mode
) {
6019 case TPACPI_FAN_RD_ACPI_GFAN
:
6020 /* 570, 600e/x, 770e, 770x */
6022 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
6030 case TPACPI_FAN_RD_TPEC
:
6031 /* all except 570, 600e/x, 770e, 770x */
6032 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
6035 if (likely(status
)) {
6037 fan_quirk1_handle(status
);
6049 static int fan_get_status_safe(u8
*status
)
6054 if (mutex_lock_killable(&fan_mutex
))
6055 return -ERESTARTSYS
;
6056 rc
= fan_get_status(&s
);
6058 fan_update_desired_level(s
);
6059 mutex_unlock(&fan_mutex
);
6067 static int fan_get_speed(unsigned int *speed
)
6071 switch (fan_status_access_mode
) {
6072 case TPACPI_FAN_RD_TPEC
:
6073 /* all except 570, 600e/x, 770e, 770x */
6074 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
6075 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
6079 *speed
= (hi
<< 8) | lo
;
6090 static int fan_set_level(int level
)
6092 if (!fan_control_allowed
)
6095 switch (fan_control_access_mode
) {
6096 case TPACPI_FAN_WR_ACPI_SFAN
:
6097 if (level
>= 0 && level
<= 7) {
6098 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
6104 case TPACPI_FAN_WR_ACPI_FANS
:
6105 case TPACPI_FAN_WR_TPEC
:
6106 if (!(level
& TP_EC_FAN_AUTO
) &&
6107 !(level
& TP_EC_FAN_FULLSPEED
) &&
6108 ((level
< 0) || (level
> 7)))
6111 /* safety net should the EC not support AUTO
6112 * or FULLSPEED mode bits and just ignore them */
6113 if (level
& TP_EC_FAN_FULLSPEED
)
6114 level
|= 7; /* safety min speed 7 */
6115 else if (level
& TP_EC_FAN_AUTO
)
6116 level
|= 4; /* safety min speed 4 */
6118 if (!acpi_ec_write(fan_status_offset
, level
))
6121 tp_features
.fan_ctrl_status_undef
= 0;
6130 static int fan_set_level_safe(int level
)
6134 if (!fan_control_allowed
)
6137 if (mutex_lock_killable(&fan_mutex
))
6138 return -ERESTARTSYS
;
6140 if (level
== TPACPI_FAN_LAST_LEVEL
)
6141 level
= fan_control_desired_level
;
6143 rc
= fan_set_level(level
);
6145 fan_update_desired_level(level
);
6147 mutex_unlock(&fan_mutex
);
6151 static int fan_set_enable(void)
6156 if (!fan_control_allowed
)
6159 if (mutex_lock_killable(&fan_mutex
))
6160 return -ERESTARTSYS
;
6162 switch (fan_control_access_mode
) {
6163 case TPACPI_FAN_WR_ACPI_FANS
:
6164 case TPACPI_FAN_WR_TPEC
:
6165 rc
= fan_get_status(&s
);
6169 /* Don't go out of emergency fan mode */
6172 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
6175 if (!acpi_ec_write(fan_status_offset
, s
))
6178 tp_features
.fan_ctrl_status_undef
= 0;
6183 case TPACPI_FAN_WR_ACPI_SFAN
:
6184 rc
= fan_get_status(&s
);
6190 /* Set fan to at least level 4 */
6193 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
6203 mutex_unlock(&fan_mutex
);
6207 static int fan_set_disable(void)
6211 if (!fan_control_allowed
)
6214 if (mutex_lock_killable(&fan_mutex
))
6215 return -ERESTARTSYS
;
6218 switch (fan_control_access_mode
) {
6219 case TPACPI_FAN_WR_ACPI_FANS
:
6220 case TPACPI_FAN_WR_TPEC
:
6221 if (!acpi_ec_write(fan_status_offset
, 0x00))
6224 fan_control_desired_level
= 0;
6225 tp_features
.fan_ctrl_status_undef
= 0;
6229 case TPACPI_FAN_WR_ACPI_SFAN
:
6230 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
6233 fan_control_desired_level
= 0;
6241 mutex_unlock(&fan_mutex
);
6245 static int fan_set_speed(int speed
)
6249 if (!fan_control_allowed
)
6252 if (mutex_lock_killable(&fan_mutex
))
6253 return -ERESTARTSYS
;
6256 switch (fan_control_access_mode
) {
6257 case TPACPI_FAN_WR_ACPI_FANS
:
6258 if (speed
>= 0 && speed
<= 65535) {
6259 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
6260 speed
, speed
, speed
))
6270 mutex_unlock(&fan_mutex
);
6274 static void fan_watchdog_reset(void)
6276 static int fan_watchdog_active
;
6278 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
6281 if (fan_watchdog_active
)
6282 cancel_delayed_work(&fan_watchdog_task
);
6284 if (fan_watchdog_maxinterval
> 0 &&
6285 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
6286 fan_watchdog_active
= 1;
6287 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
6288 msecs_to_jiffies(fan_watchdog_maxinterval
6291 "failed to queue the fan watchdog, "
6292 "watchdog will not trigger\n");
6295 fan_watchdog_active
= 0;
6298 static void fan_watchdog_fire(struct work_struct
*ignored
)
6302 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
6305 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
6306 rc
= fan_set_enable();
6308 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
6309 "will try again later...\n", -rc
);
6310 /* reschedule for later */
6311 fan_watchdog_reset();
6316 * SYSFS fan layout: hwmon compatible (device)
6319 * 0: "disengaged" mode
6321 * 2: native EC "auto" mode (recommended, hardware default)
6323 * pwm*: set speed in manual mode, ignored otherwise.
6324 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6327 * fan*_input: tachometer reading, RPM
6330 * SYSFS fan layout: extensions
6332 * fan_watchdog (driver):
6333 * fan watchdog interval in seconds, 0 disables (default), max 120
6336 /* sysfs fan pwm1_enable ----------------------------------------------- */
6337 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
6338 struct device_attribute
*attr
,
6344 res
= fan_get_status_safe(&status
);
6348 if (status
& TP_EC_FAN_FULLSPEED
) {
6350 } else if (status
& TP_EC_FAN_AUTO
) {
6355 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
6358 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
6359 struct device_attribute
*attr
,
6360 const char *buf
, size_t count
)
6365 if (parse_strtoul(buf
, 2, &t
))
6370 level
= TP_EC_FAN_FULLSPEED
;
6373 level
= TPACPI_FAN_LAST_LEVEL
;
6376 level
= TP_EC_FAN_AUTO
;
6379 /* reserved for software-controlled auto mode */
6385 res
= fan_set_level_safe(level
);
6391 fan_watchdog_reset();
6396 static struct device_attribute dev_attr_fan_pwm1_enable
=
6397 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
6398 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
6400 /* sysfs fan pwm1 ------------------------------------------------------ */
6401 static ssize_t
fan_pwm1_show(struct device
*dev
,
6402 struct device_attribute
*attr
,
6408 res
= fan_get_status_safe(&status
);
6413 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
6414 status
= fan_control_desired_level
;
6419 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
6422 static ssize_t
fan_pwm1_store(struct device
*dev
,
6423 struct device_attribute
*attr
,
6424 const char *buf
, size_t count
)
6428 u8 status
, newlevel
;
6430 if (parse_strtoul(buf
, 255, &s
))
6433 /* scale down from 0-255 to 0-7 */
6434 newlevel
= (s
>> 5) & 0x07;
6436 if (mutex_lock_killable(&fan_mutex
))
6437 return -ERESTARTSYS
;
6439 rc
= fan_get_status(&status
);
6440 if (!rc
&& (status
&
6441 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6442 rc
= fan_set_level(newlevel
);
6446 fan_update_desired_level(newlevel
);
6447 fan_watchdog_reset();
6451 mutex_unlock(&fan_mutex
);
6452 return (rc
)? rc
: count
;
6455 static struct device_attribute dev_attr_fan_pwm1
=
6456 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
6457 fan_pwm1_show
, fan_pwm1_store
);
6459 /* sysfs fan fan1_input ------------------------------------------------ */
6460 static ssize_t
fan_fan1_input_show(struct device
*dev
,
6461 struct device_attribute
*attr
,
6467 res
= fan_get_speed(&speed
);
6471 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
6474 static struct device_attribute dev_attr_fan_fan1_input
=
6475 __ATTR(fan1_input
, S_IRUGO
,
6476 fan_fan1_input_show
, NULL
);
6478 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6479 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
6482 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
6485 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
6486 const char *buf
, size_t count
)
6490 if (parse_strtoul(buf
, 120, &t
))
6493 if (!fan_control_allowed
)
6496 fan_watchdog_maxinterval
= t
;
6497 fan_watchdog_reset();
6502 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
6503 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
6505 /* --------------------------------------------------------------------- */
6506 static struct attribute
*fan_attributes
[] = {
6507 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
6508 &dev_attr_fan_fan1_input
.attr
,
6512 static const struct attribute_group fan_attr_group
= {
6513 .attrs
= fan_attributes
,
6516 static int __init
fan_init(struct ibm_init_struct
*iibm
)
6520 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
6522 mutex_init(&fan_mutex
);
6523 fan_status_access_mode
= TPACPI_FAN_NONE
;
6524 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
6525 fan_control_commands
= 0;
6526 fan_watchdog_maxinterval
= 0;
6527 tp_features
.fan_ctrl_status_undef
= 0;
6528 fan_control_desired_level
= 7;
6530 TPACPI_ACPIHANDLE_INIT(fans
);
6531 TPACPI_ACPIHANDLE_INIT(gfan
);
6532 TPACPI_ACPIHANDLE_INIT(sfan
);
6535 /* 570, 600e/x, 770e, 770x */
6536 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
6538 /* all other ThinkPads: note that even old-style
6539 * ThinkPad ECs supports the fan control register */
6540 if (likely(acpi_ec_read(fan_status_offset
,
6541 &fan_control_initial_status
))) {
6542 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
6543 fan_quirk1_detect();
6546 "ThinkPad ACPI EC access misbehaving, "
6547 "fan status and control unavailable\n");
6554 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
6555 fan_control_commands
|=
6556 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
6559 /* gfan without sfan means no fan control */
6560 /* all other models implement TP EC 0x2f control */
6564 fan_control_access_mode
=
6565 TPACPI_FAN_WR_ACPI_FANS
;
6566 fan_control_commands
|=
6567 TPACPI_FAN_CMD_SPEED
|
6568 TPACPI_FAN_CMD_LEVEL
|
6569 TPACPI_FAN_CMD_ENABLE
;
6571 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
6572 fan_control_commands
|=
6573 TPACPI_FAN_CMD_LEVEL
|
6574 TPACPI_FAN_CMD_ENABLE
;
6579 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
6580 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
6581 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
6582 fan_status_access_mode
, fan_control_access_mode
);
6584 /* fan control master switch */
6585 if (!fan_control_allowed
) {
6586 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
6587 fan_control_commands
= 0;
6588 dbg_printk(TPACPI_DBG_INIT
,
6589 "fan control features disabled by parameter\n");
6592 /* update fan_control_desired_level */
6593 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
6594 fan_get_status_safe(NULL
);
6596 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
6597 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
6598 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6603 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
6604 &driver_attr_fan_watchdog
);
6606 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6615 static void fan_exit(void)
6617 vdbg_printk(TPACPI_DBG_EXIT
,
6618 "cancelling any pending fan watchdog tasks\n");
6620 /* FIXME: can we really do this unconditionally? */
6621 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
6622 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
6623 &driver_attr_fan_watchdog
);
6625 cancel_delayed_work(&fan_watchdog_task
);
6626 flush_workqueue(tpacpi_wq
);
6629 static void fan_suspend(pm_message_t state
)
6633 if (!fan_control_allowed
)
6636 /* Store fan status in cache */
6637 fan_control_resume_level
= 0;
6638 rc
= fan_get_status_safe(&fan_control_resume_level
);
6640 printk(TPACPI_NOTICE
6641 "failed to read fan level for later "
6642 "restore during resume: %d\n", rc
);
6644 /* if it is undefined, don't attempt to restore it.
6646 if (tp_features
.fan_ctrl_status_undef
)
6647 fan_control_resume_level
= 0;
6650 static void fan_resume(void)
6652 u8 current_level
= 7;
6653 bool do_set
= false;
6656 /* DSDT *always* updates status on resume */
6657 tp_features
.fan_ctrl_status_undef
= 0;
6659 if (!fan_control_allowed
||
6660 !fan_control_resume_level
||
6661 (fan_get_status_safe(¤t_level
) < 0))
6664 switch (fan_control_access_mode
) {
6665 case TPACPI_FAN_WR_ACPI_SFAN
:
6666 /* never decrease fan level */
6667 do_set
= (fan_control_resume_level
> current_level
);
6669 case TPACPI_FAN_WR_ACPI_FANS
:
6670 case TPACPI_FAN_WR_TPEC
:
6671 /* never decrease fan level, scale is:
6672 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6674 * We expect the firmware to set either 7 or AUTO, but we
6675 * handle FULLSPEED out of paranoia.
6677 * So, we can safely only restore FULLSPEED or 7, anything
6678 * else could slow the fan. Restoring AUTO is useless, at
6679 * best that's exactly what the DSDT already set (it is the
6682 * Always keep in mind that the DSDT *will* have set the
6683 * fans to what the vendor supposes is the best level. We
6684 * muck with it only to speed the fan up.
6686 if (fan_control_resume_level
!= 7 &&
6687 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
6690 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
6691 (current_level
!= fan_control_resume_level
);
6697 printk(TPACPI_NOTICE
6698 "restoring fan level to 0x%02x\n",
6699 fan_control_resume_level
);
6700 rc
= fan_set_level_safe(fan_control_resume_level
);
6702 printk(TPACPI_NOTICE
6703 "failed to restore fan level: %d\n", rc
);
6707 static int fan_read(char *p
)
6712 unsigned int speed
= 0;
6714 switch (fan_status_access_mode
) {
6715 case TPACPI_FAN_RD_ACPI_GFAN
:
6716 /* 570, 600e/x, 770e, 770x */
6717 rc
= fan_get_status_safe(&status
);
6721 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
6723 (status
!= 0) ? "enabled" : "disabled", status
);
6726 case TPACPI_FAN_RD_TPEC
:
6727 /* all except 570, 600e/x, 770e, 770x */
6728 rc
= fan_get_status_safe(&status
);
6732 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
6733 (status
!= 0) ? "enabled" : "disabled");
6735 rc
= fan_get_speed(&speed
);
6739 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
6741 if (status
& TP_EC_FAN_FULLSPEED
)
6742 /* Disengaged mode takes precedence */
6743 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
6744 else if (status
& TP_EC_FAN_AUTO
)
6745 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
6747 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
6750 case TPACPI_FAN_NONE
:
6752 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
6755 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
6756 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
6758 switch (fan_control_access_mode
) {
6759 case TPACPI_FAN_WR_ACPI_SFAN
:
6760 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
6764 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
6765 "auto, disengaged, full-speed)\n");
6770 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
6771 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
6772 "commands:\twatchdog <timeout> (<timeout> "
6773 "is 0 (off), 1-120 (seconds))\n");
6775 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
6776 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
6777 " (<speed> is 0-65535)\n");
6782 static int fan_write_cmd_level(const char *cmd
, int *rc
)
6786 if (strlencmp(cmd
, "level auto") == 0)
6787 level
= TP_EC_FAN_AUTO
;
6788 else if ((strlencmp(cmd
, "level disengaged") == 0) |
6789 (strlencmp(cmd
, "level full-speed") == 0))
6790 level
= TP_EC_FAN_FULLSPEED
;
6791 else if (sscanf(cmd
, "level %d", &level
) != 1)
6794 *rc
= fan_set_level_safe(level
);
6796 printk(TPACPI_ERR
"level command accepted for unsupported "
6797 "access mode %d", fan_control_access_mode
);
6802 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
6804 if (strlencmp(cmd
, "enable") != 0)
6807 *rc
= fan_set_enable();
6809 printk(TPACPI_ERR
"enable command accepted for unsupported "
6810 "access mode %d", fan_control_access_mode
);
6815 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
6817 if (strlencmp(cmd
, "disable") != 0)
6820 *rc
= fan_set_disable();
6822 printk(TPACPI_ERR
"disable command accepted for unsupported "
6823 "access mode %d", fan_control_access_mode
);
6828 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
6833 * Support speed <low> <medium> <high> ? */
6835 if (sscanf(cmd
, "speed %d", &speed
) != 1)
6838 *rc
= fan_set_speed(speed
);
6840 printk(TPACPI_ERR
"speed command accepted for unsupported "
6841 "access mode %d", fan_control_access_mode
);
6846 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
6850 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
6853 if (interval
< 0 || interval
> 120)
6856 fan_watchdog_maxinterval
= interval
;
6861 static int fan_write(char *buf
)
6866 while (!rc
&& (cmd
= next_cmd(&buf
))) {
6867 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
6868 fan_write_cmd_level(cmd
, &rc
)) &&
6869 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
6870 (fan_write_cmd_enable(cmd
, &rc
) ||
6871 fan_write_cmd_disable(cmd
, &rc
) ||
6872 fan_write_cmd_watchdog(cmd
, &rc
))) &&
6873 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
6874 fan_write_cmd_speed(cmd
, &rc
))
6878 fan_watchdog_reset();
6884 static struct ibm_struct fan_driver_data
= {
6889 .suspend
= fan_suspend
,
6890 .resume
= fan_resume
,
6893 /****************************************************************************
6894 ****************************************************************************
6898 ****************************************************************************
6899 ****************************************************************************/
6901 /* sysfs name ---------------------------------------------------------- */
6902 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
6903 struct device_attribute
*attr
,
6906 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
6909 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
6910 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
6912 /* --------------------------------------------------------------------- */
6915 static struct proc_dir_entry
*proc_dir
;
6918 * Module and infrastructure proble, init and exit handling
6921 static int force_load
;
6923 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6924 static const char * __init
str_supported(int is_supported
)
6926 static char text_unsupported
[] __initdata
= "not supported";
6928 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
6930 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6932 static void ibm_exit(struct ibm_struct
*ibm
)
6934 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
6936 list_del_init(&ibm
->all_drivers
);
6938 if (ibm
->flags
.acpi_notify_installed
) {
6939 dbg_printk(TPACPI_DBG_EXIT
,
6940 "%s: acpi_remove_notify_handler\n", ibm
->name
);
6942 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
6944 dispatch_acpi_notify
);
6945 ibm
->flags
.acpi_notify_installed
= 0;
6946 ibm
->flags
.acpi_notify_installed
= 0;
6949 if (ibm
->flags
.proc_created
) {
6950 dbg_printk(TPACPI_DBG_EXIT
,
6951 "%s: remove_proc_entry\n", ibm
->name
);
6952 remove_proc_entry(ibm
->name
, proc_dir
);
6953 ibm
->flags
.proc_created
= 0;
6956 if (ibm
->flags
.acpi_driver_registered
) {
6957 dbg_printk(TPACPI_DBG_EXIT
,
6958 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
6960 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
6961 kfree(ibm
->acpi
->driver
);
6962 ibm
->acpi
->driver
= NULL
;
6963 ibm
->flags
.acpi_driver_registered
= 0;
6966 if (ibm
->flags
.init_called
&& ibm
->exit
) {
6968 ibm
->flags
.init_called
= 0;
6971 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
6974 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
6977 struct ibm_struct
*ibm
= iibm
->data
;
6978 struct proc_dir_entry
*entry
;
6980 BUG_ON(ibm
== NULL
);
6982 INIT_LIST_HEAD(&ibm
->all_drivers
);
6984 if (ibm
->flags
.experimental
&& !experimental
)
6987 dbg_printk(TPACPI_DBG_INIT
,
6988 "probing for %s\n", ibm
->name
);
6991 ret
= iibm
->init(iibm
);
6993 return 0; /* probe failed */
6997 ibm
->flags
.init_called
= 1;
7001 if (ibm
->acpi
->hid
) {
7002 ret
= register_tpacpi_subdriver(ibm
);
7007 if (ibm
->acpi
->notify
) {
7008 ret
= setup_acpi_notify(ibm
);
7009 if (ret
== -ENODEV
) {
7010 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
7020 dbg_printk(TPACPI_DBG_INIT
,
7021 "%s installed\n", ibm
->name
);
7024 entry
= create_proc_entry(ibm
->name
,
7025 S_IFREG
| S_IRUGO
| S_IWUSR
,
7028 printk(TPACPI_ERR
"unable to create proc entry %s\n",
7033 entry
->owner
= THIS_MODULE
;
7035 entry
->read_proc
= &dispatch_procfs_read
;
7037 entry
->write_proc
= &dispatch_procfs_write
;
7038 ibm
->flags
.proc_created
= 1;
7041 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
7046 dbg_printk(TPACPI_DBG_INIT
,
7047 "%s: at error exit path with result %d\n",
7051 return (ret
< 0)? ret
: 0;
7056 /* returns 0 - probe ok, or < 0 - probe error.
7057 * Probe ok doesn't mean thinkpad found.
7058 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7059 static int __must_check __init
get_thinkpad_model_data(
7060 struct thinkpad_id_data
*tp
)
7062 const struct dmi_device
*dev
= NULL
;
7063 char ec_fw_string
[18];
7069 memset(tp
, 0, sizeof(*tp
));
7071 if (dmi_name_in_vendors("IBM"))
7072 tp
->vendor
= PCI_VENDOR_ID_IBM
;
7073 else if (dmi_name_in_vendors("LENOVO"))
7074 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
7078 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
7079 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
7080 if (s
&& !tp
->bios_version_str
)
7082 if (!tp
->bios_version_str
)
7084 tp
->bios_model
= tp
->bios_version_str
[0]
7085 | (tp
->bios_version_str
[1] << 8);
7088 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7089 * X32 or newer, all Z series; Some models must have an
7090 * up-to-date BIOS or they will not be detected.
7092 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7094 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
7095 if (sscanf(dev
->name
,
7096 "IBM ThinkPad Embedded Controller -[%17c",
7097 ec_fw_string
) == 1) {
7098 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
7099 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
7101 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
7102 if (!tp
->ec_version_str
)
7104 tp
->ec_model
= ec_fw_string
[0]
7105 | (ec_fw_string
[1] << 8);
7110 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
7111 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
7112 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
7117 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
7118 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
7119 if (s
&& !tp
->nummodel_str
)
7125 static int __init
probe_for_thinkpad(void)
7133 * Non-ancient models have better DMI tagging, but very old models
7136 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
7138 /* ec is required because many other handles are relative to it */
7139 TPACPI_ACPIHANDLE_INIT(ec
);
7143 "Not yet supported ThinkPad detected!\n");
7148 * Risks a regression on very old machines, but reduces potential
7149 * false positives a damn great deal
7152 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
7154 if (!is_thinkpad
&& !force_load
)
7161 /* Module init, exit, parameters */
7163 static struct ibm_init_struct ibms_init
[] __initdata
= {
7165 .init
= thinkpad_acpi_driver_init
,
7166 .data
= &thinkpad_acpi_driver_data
,
7169 .init
= hotkey_init
,
7170 .data
= &hotkey_driver_data
,
7173 .init
= bluetooth_init
,
7174 .data
= &bluetooth_driver_data
,
7178 .data
= &wan_driver_data
,
7182 .data
= &uwb_driver_data
,
7184 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7187 .data
= &video_driver_data
,
7192 .data
= &light_driver_data
,
7194 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7197 .data
= &dock_driver_data
[0],
7201 .data
= &dock_driver_data
[1],
7204 #ifdef CONFIG_THINKPAD_ACPI_BAY
7207 .data
= &bay_driver_data
,
7212 .data
= &cmos_driver_data
,
7216 .data
= &led_driver_data
,
7220 .data
= &beep_driver_data
,
7223 .init
= thermal_init
,
7224 .data
= &thermal_driver_data
,
7227 .data
= &ecdump_driver_data
,
7230 .init
= brightness_init
,
7231 .data
= &brightness_driver_data
,
7234 .data
= &volume_driver_data
,
7238 .data
= &fan_driver_data
,
7242 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
7245 struct ibm_struct
*ibm
;
7247 if (!kp
|| !kp
->name
|| !val
)
7250 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7251 ibm
= ibms_init
[i
].data
;
7252 WARN_ON(ibm
== NULL
);
7254 if (!ibm
|| !ibm
->name
)
7257 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
7258 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
7260 strcpy(ibms_init
[i
].param
, val
);
7261 strcat(ibms_init
[i
].param
, ",");
7269 module_param(experimental
, int, 0);
7270 MODULE_PARM_DESC(experimental
,
7271 "Enables experimental features when non-zero");
7273 module_param_named(debug
, dbg_level
, uint
, 0);
7274 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
7276 module_param(force_load
, bool, 0);
7277 MODULE_PARM_DESC(force_load
,
7278 "Attempts to load the driver even on a "
7279 "mis-identified ThinkPad when true");
7281 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
7282 MODULE_PARM_DESC(fan_control
,
7283 "Enables setting fan parameters features when true");
7285 module_param_named(brightness_mode
, brightness_mode
, int, 0);
7286 MODULE_PARM_DESC(brightness_mode
,
7287 "Selects brightness control strategy: "
7288 "0=auto, 1=EC, 2=CMOS, 3=both");
7290 module_param(brightness_enable
, uint
, 0);
7291 MODULE_PARM_DESC(brightness_enable
,
7292 "Enables backlight control when 1, disables when 0");
7294 module_param(hotkey_report_mode
, uint
, 0);
7295 MODULE_PARM_DESC(hotkey_report_mode
,
7296 "used for backwards compatibility with userspace, "
7297 "see documentation");
7299 #define TPACPI_PARAM(feature) \
7300 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7301 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7302 "at module load, see documentation")
7304 TPACPI_PARAM(hotkey
);
7305 TPACPI_PARAM(bluetooth
);
7306 TPACPI_PARAM(video
);
7307 TPACPI_PARAM(light
);
7308 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7311 #ifdef CONFIG_THINKPAD_ACPI_BAY
7313 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7317 TPACPI_PARAM(ecdump
);
7318 TPACPI_PARAM(brightness
);
7319 TPACPI_PARAM(volume
);
7322 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7323 module_param(dbg_wlswemul
, uint
, 0);
7324 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
7325 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
7326 MODULE_PARM_DESC(wlsw_state
,
7327 "Initial state of the emulated WLSW switch");
7329 module_param(dbg_bluetoothemul
, uint
, 0);
7330 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
7331 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
7332 MODULE_PARM_DESC(bluetooth_state
,
7333 "Initial state of the emulated bluetooth switch");
7335 module_param(dbg_wwanemul
, uint
, 0);
7336 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
7337 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
7338 MODULE_PARM_DESC(wwan_state
,
7339 "Initial state of the emulated WWAN switch");
7341 module_param(dbg_uwbemul
, uint
, 0);
7342 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
7343 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
7344 MODULE_PARM_DESC(uwb_state
,
7345 "Initial state of the emulated UWB switch");
7348 static void thinkpad_acpi_module_exit(void)
7350 struct ibm_struct
*ibm
, *itmp
;
7352 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
7354 list_for_each_entry_safe_reverse(ibm
, itmp
,
7355 &tpacpi_all_drivers
,
7360 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
7362 if (tpacpi_inputdev
) {
7363 if (tp_features
.input_device_registered
)
7364 input_unregister_device(tpacpi_inputdev
);
7366 input_free_device(tpacpi_inputdev
);
7370 hwmon_device_unregister(tpacpi_hwmon
);
7372 if (tp_features
.sensors_pdev_attrs_registered
)
7373 device_remove_file(&tpacpi_sensors_pdev
->dev
,
7374 &dev_attr_thinkpad_acpi_pdev_name
);
7375 if (tpacpi_sensors_pdev
)
7376 platform_device_unregister(tpacpi_sensors_pdev
);
7378 platform_device_unregister(tpacpi_pdev
);
7380 if (tp_features
.sensors_pdrv_attrs_registered
)
7381 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
7382 if (tp_features
.platform_drv_attrs_registered
)
7383 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
7385 if (tp_features
.sensors_pdrv_registered
)
7386 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
7388 if (tp_features
.platform_drv_registered
)
7389 platform_driver_unregister(&tpacpi_pdriver
);
7392 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
7395 destroy_workqueue(tpacpi_wq
);
7397 kfree(thinkpad_id
.bios_version_str
);
7398 kfree(thinkpad_id
.ec_version_str
);
7399 kfree(thinkpad_id
.model_str
);
7403 static int __init
thinkpad_acpi_module_init(void)
7407 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
7409 /* Parameter checking */
7410 if (hotkey_report_mode
> 2)
7413 /* Driver-level probe */
7415 ret
= get_thinkpad_model_data(&thinkpad_id
);
7418 "unable to get DMI data: %d\n", ret
);
7419 thinkpad_acpi_module_exit();
7422 ret
= probe_for_thinkpad();
7424 thinkpad_acpi_module_exit();
7428 /* Driver initialization */
7430 TPACPI_ACPIHANDLE_INIT(ecrd
);
7431 TPACPI_ACPIHANDLE_INIT(ecwr
);
7433 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
7435 thinkpad_acpi_module_exit();
7439 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
7442 "unable to create proc dir " TPACPI_PROC_DIR
);
7443 thinkpad_acpi_module_exit();
7446 proc_dir
->owner
= THIS_MODULE
;
7448 ret
= platform_driver_register(&tpacpi_pdriver
);
7451 "unable to register main platform driver\n");
7452 thinkpad_acpi_module_exit();
7455 tp_features
.platform_drv_registered
= 1;
7457 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
7460 "unable to register hwmon platform driver\n");
7461 thinkpad_acpi_module_exit();
7464 tp_features
.sensors_pdrv_registered
= 1;
7466 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
7468 tp_features
.platform_drv_attrs_registered
= 1;
7469 ret
= tpacpi_create_driver_attributes(
7470 &tpacpi_hwmon_pdriver
.driver
);
7474 "unable to create sysfs driver attributes\n");
7475 thinkpad_acpi_module_exit();
7478 tp_features
.sensors_pdrv_attrs_registered
= 1;
7481 /* Device initialization */
7482 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
7484 if (IS_ERR(tpacpi_pdev
)) {
7485 ret
= PTR_ERR(tpacpi_pdev
);
7487 printk(TPACPI_ERR
"unable to register platform device\n");
7488 thinkpad_acpi_module_exit();
7491 tpacpi_sensors_pdev
= platform_device_register_simple(
7492 TPACPI_HWMON_DRVR_NAME
,
7494 if (IS_ERR(tpacpi_sensors_pdev
)) {
7495 ret
= PTR_ERR(tpacpi_sensors_pdev
);
7496 tpacpi_sensors_pdev
= NULL
;
7498 "unable to register hwmon platform device\n");
7499 thinkpad_acpi_module_exit();
7502 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
7503 &dev_attr_thinkpad_acpi_pdev_name
);
7506 "unable to create sysfs hwmon device attributes\n");
7507 thinkpad_acpi_module_exit();
7510 tp_features
.sensors_pdev_attrs_registered
= 1;
7511 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
7512 if (IS_ERR(tpacpi_hwmon
)) {
7513 ret
= PTR_ERR(tpacpi_hwmon
);
7514 tpacpi_hwmon
= NULL
;
7515 printk(TPACPI_ERR
"unable to register hwmon device\n");
7516 thinkpad_acpi_module_exit();
7519 mutex_init(&tpacpi_inputdev_send_mutex
);
7520 tpacpi_inputdev
= input_allocate_device();
7521 if (!tpacpi_inputdev
) {
7522 printk(TPACPI_ERR
"unable to allocate input device\n");
7523 thinkpad_acpi_module_exit();
7526 /* Prepare input device, but don't register */
7527 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
7528 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
7529 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
7530 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
7531 thinkpad_id
.vendor
:
7533 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
7534 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
7536 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7537 ret
= ibm_init(&ibms_init
[i
]);
7538 if (ret
>= 0 && *ibms_init
[i
].param
)
7539 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
7541 thinkpad_acpi_module_exit();
7545 ret
= input_register_device(tpacpi_inputdev
);
7547 printk(TPACPI_ERR
"unable to register input device\n");
7548 thinkpad_acpi_module_exit();
7551 tp_features
.input_device_registered
= 1;
7554 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
7558 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
7561 * DMI matching for module autoloading
7563 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7564 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7566 * Only models listed in thinkwiki will be supported, so add yours
7567 * if it is not there yet.
7569 #define IBM_BIOS_MODULE_ALIAS(__type) \
7570 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7572 /* Non-ancient thinkpads */
7573 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7574 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7576 /* Ancient thinkpad BIOSes have to be identified by
7577 * BIOS type or model number, and there are far less
7578 * BIOS types than model numbers... */
7579 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7580 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7581 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7583 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7584 MODULE_DESCRIPTION(TPACPI_DESC
);
7585 MODULE_VERSION(TPACPI_VERSION
);
7586 MODULE_LICENSE("GPL");
7588 module_init(thinkpad_acpi_module_init
);
7589 module_exit(thinkpad_acpi_module_exit
);