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 0x020300
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
192 #define TPACPI_DBG_RFKILL 0x0004
194 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
195 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
196 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
199 /****************************************************************************
200 * Driver-wide structs and misc. variables
205 struct tp_acpi_drv_struct
{
206 const struct acpi_device_id
*hid
;
207 struct acpi_driver
*driver
;
209 void (*notify
) (struct ibm_struct
*, u32
);
212 struct acpi_device
*device
;
218 int (*read
) (char *);
219 int (*write
) (char *);
221 void (*resume
) (void);
222 void (*suspend
) (pm_message_t state
);
223 void (*shutdown
) (void);
225 struct list_head all_drivers
;
227 struct tp_acpi_drv_struct
*acpi
;
230 u8 acpi_driver_registered
:1;
231 u8 acpi_notify_installed
:1;
238 struct ibm_init_struct
{
241 int (*init
) (struct ibm_init_struct
*);
242 struct ibm_struct
*data
;
246 #ifdef CONFIG_THINKPAD_ACPI_BAY
259 u32 bright_16levels
:1;
260 u32 bright_acpimode
:1;
263 u32 fan_ctrl_status_undef
:1;
264 u32 input_device_registered
:1;
265 u32 platform_drv_registered
:1;
266 u32 platform_drv_attrs_registered
:1;
267 u32 sensors_pdrv_registered
:1;
268 u32 sensors_pdrv_attrs_registered
:1;
269 u32 sensors_pdev_attrs_registered
:1;
270 u32 hotkey_poll_active
:1;
274 u16 hotkey_mask_ff
:1;
275 u16 bright_cmos_ec_unsync
:1;
278 struct thinkpad_id_data
{
279 unsigned int vendor
; /* ThinkPad vendor:
280 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
282 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
283 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
285 u16 bios_model
; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
288 char *model_str
; /* ThinkPad T43 */
289 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id
;
294 TPACPI_LIFE_INIT
= 0,
299 static int experimental
;
300 static u32 dbg_level
;
302 static struct workqueue_struct
*tpacpi_wq
;
310 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev
{
312 struct led_classdev led_classdev
;
313 struct work_struct work
;
314 enum led_status_t new_state
;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul
;
320 static int tpacpi_wlsw_emulstate
;
321 static int dbg_bluetoothemul
;
322 static int tpacpi_bluetooth_emulstate
;
323 static int dbg_wwanemul
;
324 static int tpacpi_wwan_emulstate
;
325 static int dbg_uwbemul
;
326 static int tpacpi_uwb_emulstate
;
330 /*************************************************************************
334 #define dbg_printk(a_dbg_level, format, arg...) \
335 do { if (dbg_level & (a_dbg_level)) \
336 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 static const char *str_supported(int is_supported
);
343 #define vdbg_printk(a_dbg_level, format, arg...) \
347 static void tpacpi_log_usertask(const char * const what
)
349 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
350 what
, task_tgid_vnr(current
));
353 #define tpacpi_disclose_usertask(what, format, arg...) \
356 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
357 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
358 printk(TPACPI_DEBUG "%s: PID %d: " format, \
359 what, task_tgid_vnr(current), ## arg); \
363 /****************************************************************************
364 ****************************************************************************
366 * ACPI Helpers and device model
368 ****************************************************************************
369 ****************************************************************************/
371 /*************************************************************************
375 static acpi_handle root_handle
;
377 #define TPACPI_HANDLE(object, parent, paths...) \
378 static acpi_handle object##_handle; \
379 static acpi_handle *object##_parent = &parent##_handle; \
380 static char *object##_path; \
381 static char *object##_paths[] = { paths }
383 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
384 "\\_SB.PCI.ISA.EC", /* 570 */
385 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
386 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
387 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
388 "\\_SB.PCI0.ICH3.EC0", /* R31 */
389 "\\_SB.PCI0.LPC.EC", /* all others */
392 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
393 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
395 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
397 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
398 "\\CMS", /* R40, R40e */
401 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
402 "^HKEY", /* R30, R31 */
403 "HKEY", /* all others */
406 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
407 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
408 "\\_SB.PCI0.VID0", /* 770e */
409 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
410 "\\_SB.PCI0.AGP.VID", /* all others */
414 /*************************************************************************
418 static int acpi_evalf(acpi_handle handle
,
419 void *res
, char *method
, char *fmt
, ...)
422 struct acpi_object_list params
;
423 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
424 struct acpi_buffer result
, *resultp
;
425 union acpi_object out_obj
;
433 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
446 params
.pointer
= &in_objs
[0];
453 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
454 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
456 /* add more types as needed */
458 printk(TPACPI_ERR
"acpi_evalf() called "
459 "with invalid format character '%c'\n", c
);
465 if (res_type
!= 'v') {
466 result
.length
= sizeof(out_obj
);
467 result
.pointer
= &out_obj
;
472 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
477 *(int *)res
= out_obj
.integer
.value
;
478 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
481 success
= status
== AE_OK
;
483 /* add more types as needed */
485 printk(TPACPI_ERR
"acpi_evalf() called "
486 "with invalid format character '%c'\n", res_type
);
490 if (!success
&& !quiet
)
491 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
492 method
, fmt0
, status
);
497 static int acpi_ec_read(int i
, u8
*p
)
502 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
506 if (ec_read(i
, p
) < 0)
513 static int acpi_ec_write(int i
, u8 v
)
516 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
519 if (ec_write(i
, v
) < 0)
526 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
527 static int _sta(acpi_handle handle
)
531 if (!handle
|| !acpi_evalf(handle
, &status
, "_STA", "d"))
538 static int issue_thinkpad_cmos_command(int cmos_cmd
)
543 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
549 /*************************************************************************
553 #define TPACPI_ACPIHANDLE_INIT(object) \
554 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
555 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
557 static void drv_acpi_handle_init(char *name
,
558 acpi_handle
*handle
, acpi_handle parent
,
559 char **paths
, int num_paths
, char **path
)
564 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
567 for (i
= 0; i
< num_paths
; i
++) {
568 status
= acpi_get_handle(parent
, paths
[i
], handle
);
569 if (ACPI_SUCCESS(status
)) {
571 dbg_printk(TPACPI_DBG_INIT
,
572 "Found ACPI handle %s for %s\n",
578 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
583 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
585 struct ibm_struct
*ibm
= data
;
587 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
590 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
593 ibm
->acpi
->notify(ibm
, event
);
596 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
603 if (!*ibm
->acpi
->handle
)
606 vdbg_printk(TPACPI_DBG_INIT
,
607 "setting up ACPI notify for %s\n", ibm
->name
);
609 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
611 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
616 ibm
->acpi
->device
->driver_data
= ibm
;
617 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
618 TPACPI_ACPI_EVENT_PREFIX
,
621 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
622 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
623 if (ACPI_FAILURE(status
)) {
624 if (status
== AE_ALREADY_EXISTS
) {
626 "another device driver is already "
627 "handling %s events\n", ibm
->name
);
630 "acpi_install_notify_handler(%s) failed: %d\n",
635 ibm
->flags
.acpi_notify_installed
= 1;
639 static int __init
tpacpi_device_add(struct acpi_device
*device
)
644 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
648 dbg_printk(TPACPI_DBG_INIT
,
649 "registering %s as an ACPI driver\n", ibm
->name
);
653 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
654 if (!ibm
->acpi
->driver
) {
656 "failed to allocate memory for ibm->acpi->driver\n");
660 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
661 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
663 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
665 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
667 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
669 kfree(ibm
->acpi
->driver
);
670 ibm
->acpi
->driver
= NULL
;
672 ibm
->flags
.acpi_driver_registered
= 1;
678 /****************************************************************************
679 ****************************************************************************
683 ****************************************************************************
684 ****************************************************************************/
686 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
687 int count
, int *eof
, void *data
)
689 struct ibm_struct
*ibm
= data
;
692 if (!ibm
|| !ibm
->read
)
695 len
= ibm
->read(page
);
699 if (len
<= off
+ count
)
711 static int dispatch_procfs_write(struct file
*file
,
712 const char __user
*userbuf
,
713 unsigned long count
, void *data
)
715 struct ibm_struct
*ibm
= data
;
719 if (!ibm
|| !ibm
->write
)
722 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
726 if (copy_from_user(kernbuf
, userbuf
, count
)) {
732 strcat(kernbuf
, ",");
733 ret
= ibm
->write(kernbuf
);
742 static char *next_cmd(char **cmds
)
747 while ((end
= strchr(start
, ',')) && end
== start
)
759 /****************************************************************************
760 ****************************************************************************
762 * Device model: input, hwmon and platform
764 ****************************************************************************
765 ****************************************************************************/
767 static struct platform_device
*tpacpi_pdev
;
768 static struct platform_device
*tpacpi_sensors_pdev
;
769 static struct device
*tpacpi_hwmon
;
770 static struct input_dev
*tpacpi_inputdev
;
771 static struct mutex tpacpi_inputdev_send_mutex
;
772 static LIST_HEAD(tpacpi_all_drivers
);
774 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
777 struct ibm_struct
*ibm
, *itmp
;
779 list_for_each_entry_safe(ibm
, itmp
,
783 (ibm
->suspend
)(state
);
789 static int tpacpi_resume_handler(struct platform_device
*pdev
)
791 struct ibm_struct
*ibm
, *itmp
;
793 list_for_each_entry_safe(ibm
, itmp
,
803 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
805 struct ibm_struct
*ibm
, *itmp
;
807 list_for_each_entry_safe(ibm
, itmp
,
815 static struct platform_driver tpacpi_pdriver
= {
817 .name
= TPACPI_DRVR_NAME
,
818 .owner
= THIS_MODULE
,
820 .suspend
= tpacpi_suspend_handler
,
821 .resume
= tpacpi_resume_handler
,
822 .shutdown
= tpacpi_shutdown_handler
,
825 static struct platform_driver tpacpi_hwmon_pdriver
= {
827 .name
= TPACPI_HWMON_DRVR_NAME
,
828 .owner
= THIS_MODULE
,
832 /*************************************************************************
833 * sysfs support helpers
836 struct attribute_set
{
837 unsigned int members
, max_members
;
838 struct attribute_group group
;
841 struct attribute_set_obj
{
842 struct attribute_set s
;
844 } __attribute__((packed
));
846 static struct attribute_set
*create_attr_set(unsigned int max_members
,
849 struct attribute_set_obj
*sobj
;
851 if (max_members
== 0)
854 /* Allocates space for implicit NULL at the end too */
855 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
856 max_members
* sizeof(struct attribute
*),
860 sobj
->s
.max_members
= max_members
;
861 sobj
->s
.group
.attrs
= &sobj
->a
;
862 sobj
->s
.group
.name
= name
;
867 #define destroy_attr_set(_set) \
870 /* not multi-threaded safe, use it in a single thread per set */
871 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
876 if (s
->members
>= s
->max_members
)
879 s
->group
.attrs
[s
->members
] = attr
;
885 static int add_many_to_attr_set(struct attribute_set
*s
,
886 struct attribute
**attr
,
891 for (i
= 0; i
< count
; i
++) {
892 res
= add_to_attr_set(s
, attr
[i
]);
900 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
902 sysfs_remove_group(kobj
, &s
->group
);
906 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
907 sysfs_create_group(_kobj, &_attr_set->group)
909 static int parse_strtoul(const char *buf
,
910 unsigned long max
, unsigned long *value
)
914 while (*buf
&& isspace(*buf
))
916 *value
= simple_strtoul(buf
, &endp
, 0);
917 while (*endp
&& isspace(*endp
))
919 if (*endp
|| *value
> max
)
925 static void tpacpi_disable_brightness_delay(void)
927 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
929 "ACPI backlight control delay disabled\n");
932 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
934 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
935 union acpi_object
*obj
;
938 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
939 obj
= (union acpi_object
*)buffer
.pointer
;
940 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
941 printk(TPACPI_ERR
"Unknown _BCL data, "
942 "please report this to %s\n", TPACPI_MAIL
);
945 rc
= obj
->package
.count
;
951 kfree(buffer
.pointer
);
955 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
956 u32 lvl
, void *context
, void **rv
)
958 char name
[ACPI_PATH_SEGMENT_LENGTH
];
959 struct acpi_buffer buffer
= { sizeof(name
), &name
};
961 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
962 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
964 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
965 return AE_CTRL_TERMINATE
;
972 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
974 static int __init
tpacpi_check_std_acpi_brightness_support(void)
978 void *bcl_ptr
= &bcl_levels
;
981 TPACPI_ACPIHANDLE_INIT(vid
);
987 * Search for a _BCL method, and execute it. This is safe on all
988 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
989 * BIOS in ACPI backlight control mode. We do NOT have to care
990 * about calling the _BCL method in an enabled video device, any
991 * will do for our purposes.
994 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
995 tpacpi_acpi_walk_find_bcl
, NULL
,
998 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
999 tp_features
.bright_acpimode
= 1;
1000 return (bcl_levels
- 2);
1006 static int __init
tpacpi_new_rfkill(const unsigned int id
,
1007 struct rfkill
**rfk
,
1008 const enum rfkill_type rfktype
,
1010 const bool set_default
,
1011 int (*toggle_radio
)(void *, enum rfkill_state
),
1012 int (*get_state
)(void *, enum rfkill_state
*))
1015 enum rfkill_state initial_state
= RFKILL_STATE_SOFT_BLOCKED
;
1017 res
= get_state(NULL
, &initial_state
);
1020 "failed to read initial state for %s, error %d; "
1021 "will turn radio off\n", name
, res
);
1022 } else if (set_default
) {
1023 /* try to set the initial state as the default for the rfkill
1024 * type, since we ask the firmware to preserve it across S5 in
1026 if (rfkill_set_default(rfktype
,
1027 (initial_state
== RFKILL_STATE_UNBLOCKED
) ?
1028 RFKILL_STATE_UNBLOCKED
:
1029 RFKILL_STATE_SOFT_BLOCKED
) == -EPERM
)
1030 vdbg_printk(TPACPI_DBG_RFKILL
,
1031 "Default state for %s cannot be changed\n",
1035 *rfk
= rfkill_allocate(&tpacpi_pdev
->dev
, rfktype
);
1038 "failed to allocate memory for rfkill class\n");
1042 (*rfk
)->name
= name
;
1043 (*rfk
)->get_state
= get_state
;
1044 (*rfk
)->toggle_radio
= toggle_radio
;
1045 (*rfk
)->state
= initial_state
;
1047 res
= rfkill_register(*rfk
);
1050 "failed to register %s rfkill switch: %d\n",
1060 static void printk_deprecated_attribute(const char * const what
,
1061 const char * const details
)
1063 tpacpi_log_usertask("deprecated sysfs attribute");
1064 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1065 "will be removed. %s\n",
1069 static void printk_deprecated_rfkill_attribute(const char * const what
)
1071 printk_deprecated_attribute(what
,
1072 "Please switch to generic rfkill before year 2010");
1075 /*************************************************************************
1076 * thinkpad-acpi driver attributes
1079 /* interface_version --------------------------------------------------- */
1080 static ssize_t
tpacpi_driver_interface_version_show(
1081 struct device_driver
*drv
,
1084 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1087 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1088 tpacpi_driver_interface_version_show
, NULL
);
1090 /* debug_level --------------------------------------------------------- */
1091 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1094 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1097 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1098 const char *buf
, size_t count
)
1102 if (parse_strtoul(buf
, 0xffff, &t
))
1110 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1111 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1113 /* version ------------------------------------------------------------- */
1114 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1117 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1118 TPACPI_DESC
, TPACPI_VERSION
);
1121 static DRIVER_ATTR(version
, S_IRUGO
,
1122 tpacpi_driver_version_show
, NULL
);
1124 /* --------------------------------------------------------------------- */
1126 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1128 static void tpacpi_send_radiosw_update(void);
1130 /* wlsw_emulstate ------------------------------------------------------ */
1131 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1134 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1137 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1138 const char *buf
, size_t count
)
1142 if (parse_strtoul(buf
, 1, &t
))
1145 if (tpacpi_wlsw_emulstate
!= t
) {
1146 tpacpi_wlsw_emulstate
= !!t
;
1147 tpacpi_send_radiosw_update();
1149 tpacpi_wlsw_emulstate
= !!t
;
1154 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1155 tpacpi_driver_wlsw_emulstate_show
,
1156 tpacpi_driver_wlsw_emulstate_store
);
1158 /* bluetooth_emulstate ------------------------------------------------- */
1159 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1160 struct device_driver
*drv
,
1163 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1166 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1167 struct device_driver
*drv
,
1168 const char *buf
, size_t count
)
1172 if (parse_strtoul(buf
, 1, &t
))
1175 tpacpi_bluetooth_emulstate
= !!t
;
1180 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1181 tpacpi_driver_bluetooth_emulstate_show
,
1182 tpacpi_driver_bluetooth_emulstate_store
);
1184 /* wwan_emulstate ------------------------------------------------- */
1185 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1186 struct device_driver
*drv
,
1189 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1192 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1193 struct device_driver
*drv
,
1194 const char *buf
, size_t count
)
1198 if (parse_strtoul(buf
, 1, &t
))
1201 tpacpi_wwan_emulstate
= !!t
;
1206 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1207 tpacpi_driver_wwan_emulstate_show
,
1208 tpacpi_driver_wwan_emulstate_store
);
1210 /* uwb_emulstate ------------------------------------------------- */
1211 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1212 struct device_driver
*drv
,
1215 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1218 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1219 struct device_driver
*drv
,
1220 const char *buf
, size_t count
)
1224 if (parse_strtoul(buf
, 1, &t
))
1227 tpacpi_uwb_emulstate
= !!t
;
1232 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1233 tpacpi_driver_uwb_emulstate_show
,
1234 tpacpi_driver_uwb_emulstate_store
);
1237 /* --------------------------------------------------------------------- */
1239 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1240 &driver_attr_debug_level
, &driver_attr_version
,
1241 &driver_attr_interface_version
,
1244 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1250 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1251 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1255 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1256 if (!res
&& dbg_wlswemul
)
1257 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1258 if (!res
&& dbg_bluetoothemul
)
1259 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1260 if (!res
&& dbg_wwanemul
)
1261 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1262 if (!res
&& dbg_uwbemul
)
1263 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1269 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1273 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1274 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1276 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1277 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1278 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1279 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1280 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1284 /****************************************************************************
1285 ****************************************************************************
1289 ****************************************************************************
1290 ****************************************************************************/
1292 /*************************************************************************
1293 * thinkpad-acpi init subdriver
1296 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1298 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1299 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1301 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1302 (thinkpad_id
.bios_version_str
) ?
1303 thinkpad_id
.bios_version_str
: "unknown",
1304 (thinkpad_id
.ec_version_str
) ?
1305 thinkpad_id
.ec_version_str
: "unknown");
1307 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1308 printk(TPACPI_INFO
"%s %s, model %s\n",
1309 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1310 "IBM" : ((thinkpad_id
.vendor
==
1311 PCI_VENDOR_ID_LENOVO
) ?
1312 "Lenovo" : "Unknown vendor"),
1313 thinkpad_id
.model_str
,
1314 (thinkpad_id
.nummodel_str
) ?
1315 thinkpad_id
.nummodel_str
: "unknown");
1320 static int thinkpad_acpi_driver_read(char *p
)
1324 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1325 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1330 static struct ibm_struct thinkpad_acpi_driver_data
= {
1332 .read
= thinkpad_acpi_driver_read
,
1335 /*************************************************************************
1339 enum { /* hot key scan codes (derived from ACPI DSDT) */
1340 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1341 TP_ACPI_HOTKEYSCAN_FNF2
,
1342 TP_ACPI_HOTKEYSCAN_FNF3
,
1343 TP_ACPI_HOTKEYSCAN_FNF4
,
1344 TP_ACPI_HOTKEYSCAN_FNF5
,
1345 TP_ACPI_HOTKEYSCAN_FNF6
,
1346 TP_ACPI_HOTKEYSCAN_FNF7
,
1347 TP_ACPI_HOTKEYSCAN_FNF8
,
1348 TP_ACPI_HOTKEYSCAN_FNF9
,
1349 TP_ACPI_HOTKEYSCAN_FNF10
,
1350 TP_ACPI_HOTKEYSCAN_FNF11
,
1351 TP_ACPI_HOTKEYSCAN_FNF12
,
1352 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1353 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1354 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1355 TP_ACPI_HOTKEYSCAN_FNHOME
,
1356 TP_ACPI_HOTKEYSCAN_FNEND
,
1357 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1358 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1359 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1360 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1361 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1362 TP_ACPI_HOTKEYSCAN_MUTE
,
1363 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1366 enum { /* Keys available through NVRAM polling */
1367 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1368 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1371 enum { /* Positions of some of the keys in hotkey masks */
1372 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1373 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1374 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1375 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1376 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1377 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1378 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1379 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1380 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1381 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1382 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1385 enum { /* NVRAM to ACPI HKEY group map */
1386 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1387 TP_ACPI_HKEY_ZOOM_MASK
|
1388 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1389 TP_ACPI_HKEY_HIBERNATE_MASK
,
1390 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1391 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1392 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1393 TP_ACPI_HKEY_VOLDWN_MASK
|
1394 TP_ACPI_HKEY_MUTE_MASK
,
1397 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1398 struct tp_nvram_state
{
1399 u16 thinkpad_toggle
:1;
1401 u16 display_toggle
:1;
1402 u16 thinklight_toggle
:1;
1403 u16 hibernate_toggle
:1;
1404 u16 displayexp_toggle
:1;
1405 u16 display_state
:1;
1406 u16 brightness_toggle
:1;
1407 u16 volume_toggle
:1;
1410 u8 brightness_level
;
1414 static struct task_struct
*tpacpi_hotkey_task
;
1415 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
1416 static int hotkey_poll_freq
= 10; /* Hz */
1417 static struct mutex hotkey_thread_mutex
;
1418 static struct mutex hotkey_thread_data_mutex
;
1419 static unsigned int hotkey_config_change
;
1421 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1423 #define hotkey_source_mask 0U
1425 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1427 static struct mutex hotkey_mutex
;
1429 static enum { /* Reasons for waking up */
1430 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
1431 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
1432 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
1433 } hotkey_wakeup_reason
;
1435 static int hotkey_autosleep_ack
;
1437 static u32 hotkey_orig_mask
;
1438 static u32 hotkey_all_mask
;
1439 static u32 hotkey_reserved_mask
;
1440 static u32 hotkey_mask
;
1442 static unsigned int hotkey_report_mode
;
1444 static u16
*hotkey_keycode_map
;
1446 static struct attribute_set
*hotkey_dev_attributes
;
1448 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1449 #define HOTKEY_CONFIG_CRITICAL_START \
1451 mutex_lock(&hotkey_thread_data_mutex); \
1452 hotkey_config_change++; \
1454 #define HOTKEY_CONFIG_CRITICAL_END \
1455 mutex_unlock(&hotkey_thread_data_mutex);
1457 #define HOTKEY_CONFIG_CRITICAL_START
1458 #define HOTKEY_CONFIG_CRITICAL_END
1459 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1461 /* HKEY.MHKG() return bits */
1462 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1464 static int hotkey_get_wlsw(int *status
)
1466 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1468 *status
= !!tpacpi_wlsw_emulstate
;
1472 if (!acpi_evalf(hkey_handle
, status
, "WLSW", "d"))
1477 static int hotkey_get_tablet_mode(int *status
)
1481 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
1484 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
1489 * Call with hotkey_mutex held
1491 static int hotkey_mask_get(void)
1495 if (tp_features
.hotkey_mask
) {
1496 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
1499 hotkey_mask
= m
| (hotkey_source_mask
& hotkey_mask
);
1505 * Call with hotkey_mutex held
1507 static int hotkey_mask_set(u32 mask
)
1512 if (tp_features
.hotkey_mask
) {
1513 if (!tp_warned
.hotkey_mask_ff
&&
1514 (mask
== 0xffff || mask
== 0xffffff ||
1515 mask
== 0xffffffff)) {
1516 tp_warned
.hotkey_mask_ff
= 1;
1517 printk(TPACPI_NOTICE
1518 "setting the hotkey mask to 0x%08x is likely "
1519 "not the best way to go about it\n", mask
);
1520 printk(TPACPI_NOTICE
1521 "please consider using the driver defaults, "
1522 "and refer to up-to-date thinkpad-acpi "
1526 HOTKEY_CONFIG_CRITICAL_START
1527 for (i
= 0; i
< 32; i
++) {
1529 /* enable in firmware mask only keys not in NVRAM
1530 * mode, but enable the key in the cached hotkey_mask
1531 * regardless of mode, or the key will end up
1532 * disabled by hotkey_mask_get() */
1533 if (!acpi_evalf(hkey_handle
,
1534 NULL
, "MHKM", "vdd", i
+ 1,
1535 !!((mask
& ~hotkey_source_mask
) & m
))) {
1539 hotkey_mask
= (hotkey_mask
& ~m
) | (mask
& m
);
1542 HOTKEY_CONFIG_CRITICAL_END
1544 /* hotkey_mask_get must be called unconditionally below */
1545 if (!hotkey_mask_get() && !rc
&&
1546 (hotkey_mask
& ~hotkey_source_mask
) !=
1547 (mask
& ~hotkey_source_mask
)) {
1548 printk(TPACPI_NOTICE
1549 "requested hot key mask 0x%08x, but "
1550 "firmware forced it to 0x%08x\n",
1554 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1555 HOTKEY_CONFIG_CRITICAL_START
1556 hotkey_mask
= mask
& hotkey_source_mask
;
1557 HOTKEY_CONFIG_CRITICAL_END
1559 if (hotkey_mask
!= mask
) {
1560 printk(TPACPI_NOTICE
1561 "requested hot key mask 0x%08x, "
1562 "forced to 0x%08x (NVRAM poll mask is "
1563 "0x%08x): no firmware mask support\n",
1564 mask
, hotkey_mask
, hotkey_source_mask
);
1569 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1575 static int hotkey_status_get(int *status
)
1577 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
1583 static int hotkey_status_set(bool enable
)
1585 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
1591 static void tpacpi_input_send_tabletsw(void)
1595 if (tp_features
.hotkey_tablet
&&
1596 !hotkey_get_tablet_mode(&state
)) {
1597 mutex_lock(&tpacpi_inputdev_send_mutex
);
1599 input_report_switch(tpacpi_inputdev
,
1600 SW_TABLET_MODE
, !!state
);
1601 input_sync(tpacpi_inputdev
);
1603 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1607 static void tpacpi_input_send_key(unsigned int scancode
)
1609 unsigned int keycode
;
1611 keycode
= hotkey_keycode_map
[scancode
];
1613 if (keycode
!= KEY_RESERVED
) {
1614 mutex_lock(&tpacpi_inputdev_send_mutex
);
1616 input_report_key(tpacpi_inputdev
, keycode
, 1);
1617 if (keycode
== KEY_UNKNOWN
)
1618 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1620 input_sync(tpacpi_inputdev
);
1622 input_report_key(tpacpi_inputdev
, keycode
, 0);
1623 if (keycode
== KEY_UNKNOWN
)
1624 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
1626 input_sync(tpacpi_inputdev
);
1628 mutex_unlock(&tpacpi_inputdev_send_mutex
);
1632 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1633 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
1635 static void tpacpi_hotkey_send_key(unsigned int scancode
)
1637 tpacpi_input_send_key(scancode
);
1638 if (hotkey_report_mode
< 2) {
1639 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
1640 0x80, 0x1001 + scancode
);
1644 static void hotkey_read_nvram(struct tp_nvram_state
*n
, u32 m
)
1648 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
1649 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
1650 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
1651 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
1652 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
1653 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
1655 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
1656 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
1657 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
1659 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
1660 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
1661 n
->displayexp_toggle
=
1662 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
1664 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
1665 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
1666 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
1667 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
1668 n
->brightness_toggle
=
1669 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
1671 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
1672 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
1673 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
1674 >> TP_NVRAM_POS_LEVEL_VOLUME
;
1675 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
1676 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
1680 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1682 if ((mask & (1 << __scancode)) && \
1683 oldn->__member != newn->__member) \
1684 tpacpi_hotkey_send_key(__scancode); \
1687 #define TPACPI_MAY_SEND_KEY(__scancode) \
1688 do { if (mask & (1 << __scancode)) \
1689 tpacpi_hotkey_send_key(__scancode); } while (0)
1691 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
1692 struct tp_nvram_state
*newn
,
1695 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
1696 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
1698 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
1700 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
1702 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
1705 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
1706 if (oldn
->mute
!= newn
->mute
) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1709 if (oldn
->volume_level
> newn
->volume_level
) {
1710 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1711 } else if (oldn
->volume_level
< newn
->volume_level
) {
1712 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1713 } else if (oldn
->mute
== newn
->mute
) {
1714 /* repeated key presses that didn't change state */
1716 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
1717 } else if (newn
->volume_level
!= 0) {
1718 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
1720 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
1725 /* handle brightness */
1726 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
1727 if (oldn
->brightness_level
< newn
->brightness_level
) {
1728 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1729 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
1730 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1732 /* repeated key presses that didn't change state */
1733 if (newn
->brightness_level
!= 0) {
1734 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
1736 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
1742 #undef TPACPI_COMPARE_KEY
1743 #undef TPACPI_MAY_SEND_KEY
1745 static int hotkey_kthread(void *data
)
1747 struct tp_nvram_state s
[2];
1749 unsigned int si
, so
;
1751 unsigned int change_detector
, must_reset
;
1753 mutex_lock(&hotkey_thread_mutex
);
1755 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
1764 /* Initial state for compares */
1765 mutex_lock(&hotkey_thread_data_mutex
);
1766 change_detector
= hotkey_config_change
;
1767 mask
= hotkey_source_mask
& hotkey_mask
;
1768 mutex_unlock(&hotkey_thread_data_mutex
);
1769 hotkey_read_nvram(&s
[so
], mask
);
1771 while (!kthread_should_stop() && hotkey_poll_freq
) {
1773 t
= 1000/hotkey_poll_freq
;
1774 t
= msleep_interruptible(t
);
1775 if (unlikely(kthread_should_stop()))
1777 must_reset
= try_to_freeze();
1778 if (t
> 0 && !must_reset
)
1781 mutex_lock(&hotkey_thread_data_mutex
);
1782 if (must_reset
|| hotkey_config_change
!= change_detector
) {
1783 /* forget old state on thaw or config change */
1786 change_detector
= hotkey_config_change
;
1788 mask
= hotkey_source_mask
& hotkey_mask
;
1789 mutex_unlock(&hotkey_thread_data_mutex
);
1792 hotkey_read_nvram(&s
[si
], mask
);
1793 if (likely(si
!= so
)) {
1794 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
1804 mutex_unlock(&hotkey_thread_mutex
);
1808 static void hotkey_poll_stop_sync(void)
1810 if (tpacpi_hotkey_task
) {
1811 if (frozen(tpacpi_hotkey_task
) ||
1812 freezing(tpacpi_hotkey_task
))
1813 thaw_process(tpacpi_hotkey_task
);
1815 kthread_stop(tpacpi_hotkey_task
);
1816 tpacpi_hotkey_task
= NULL
;
1817 mutex_lock(&hotkey_thread_mutex
);
1818 /* at this point, the thread did exit */
1819 mutex_unlock(&hotkey_thread_mutex
);
1823 /* call with hotkey_mutex held */
1824 static void hotkey_poll_setup(int may_warn
)
1826 if ((hotkey_source_mask
& hotkey_mask
) != 0 &&
1827 hotkey_poll_freq
> 0 &&
1828 (tpacpi_inputdev
->users
> 0 || hotkey_report_mode
< 2)) {
1829 if (!tpacpi_hotkey_task
) {
1830 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
1831 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
1832 if (IS_ERR(tpacpi_hotkey_task
)) {
1833 tpacpi_hotkey_task
= NULL
;
1835 "could not create kernel thread "
1836 "for hotkey polling\n");
1840 hotkey_poll_stop_sync();
1842 hotkey_source_mask
!= 0 && hotkey_poll_freq
== 0) {
1843 printk(TPACPI_NOTICE
1844 "hot keys 0x%08x require polling, "
1845 "which is currently disabled\n",
1846 hotkey_source_mask
);
1851 static void hotkey_poll_setup_safe(int may_warn
)
1853 mutex_lock(&hotkey_mutex
);
1854 hotkey_poll_setup(may_warn
);
1855 mutex_unlock(&hotkey_mutex
);
1858 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1860 static void hotkey_poll_setup_safe(int __unused
)
1864 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1866 static int hotkey_inputdev_open(struct input_dev
*dev
)
1868 switch (tpacpi_lifecycle
) {
1869 case TPACPI_LIFE_INIT
:
1871 * hotkey_init will call hotkey_poll_setup_safe
1872 * at the appropriate moment
1875 case TPACPI_LIFE_EXITING
:
1877 case TPACPI_LIFE_RUNNING
:
1878 hotkey_poll_setup_safe(0);
1882 /* Should only happen if tpacpi_lifecycle is corrupt */
1887 static void hotkey_inputdev_close(struct input_dev
*dev
)
1889 /* disable hotkey polling when possible */
1890 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
)
1891 hotkey_poll_setup_safe(0);
1894 /* sysfs hotkey enable ------------------------------------------------- */
1895 static ssize_t
hotkey_enable_show(struct device
*dev
,
1896 struct device_attribute
*attr
,
1901 printk_deprecated_attribute("hotkey_enable",
1902 "Hotkey reporting is always enabled");
1904 res
= hotkey_status_get(&status
);
1908 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
1911 static ssize_t
hotkey_enable_store(struct device
*dev
,
1912 struct device_attribute
*attr
,
1913 const char *buf
, size_t count
)
1917 printk_deprecated_attribute("hotkey_enable",
1918 "Hotkeys can be disabled through hotkey_mask");
1920 if (parse_strtoul(buf
, 1, &t
))
1929 static struct device_attribute dev_attr_hotkey_enable
=
1930 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
1931 hotkey_enable_show
, hotkey_enable_store
);
1933 /* sysfs hotkey mask --------------------------------------------------- */
1934 static ssize_t
hotkey_mask_show(struct device
*dev
,
1935 struct device_attribute
*attr
,
1940 if (mutex_lock_killable(&hotkey_mutex
))
1941 return -ERESTARTSYS
;
1942 res
= hotkey_mask_get();
1943 mutex_unlock(&hotkey_mutex
);
1946 res
: snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_mask
);
1949 static ssize_t
hotkey_mask_store(struct device
*dev
,
1950 struct device_attribute
*attr
,
1951 const char *buf
, size_t count
)
1956 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
1959 if (mutex_lock_killable(&hotkey_mutex
))
1960 return -ERESTARTSYS
;
1962 res
= hotkey_mask_set(t
);
1964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1965 hotkey_poll_setup(1);
1968 mutex_unlock(&hotkey_mutex
);
1970 return (res
) ? res
: count
;
1973 static struct device_attribute dev_attr_hotkey_mask
=
1974 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
1975 hotkey_mask_show
, hotkey_mask_store
);
1977 /* sysfs hotkey bios_enabled ------------------------------------------- */
1978 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
1979 struct device_attribute
*attr
,
1982 return sprintf(buf
, "0\n");
1985 static struct device_attribute dev_attr_hotkey_bios_enabled
=
1986 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
1988 /* sysfs hotkey bios_mask ---------------------------------------------- */
1989 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
1990 struct device_attribute
*attr
,
1993 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
1996 static struct device_attribute dev_attr_hotkey_bios_mask
=
1997 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
1999 /* sysfs hotkey all_mask ----------------------------------------------- */
2000 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2001 struct device_attribute
*attr
,
2004 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2005 hotkey_all_mask
| hotkey_source_mask
);
2008 static struct device_attribute dev_attr_hotkey_all_mask
=
2009 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2011 /* sysfs hotkey recommended_mask --------------------------------------- */
2012 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2013 struct device_attribute
*attr
,
2016 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2017 (hotkey_all_mask
| hotkey_source_mask
)
2018 & ~hotkey_reserved_mask
);
2021 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2022 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2023 hotkey_recommended_mask_show
, NULL
);
2025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2027 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2028 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2029 struct device_attribute
*attr
,
2032 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2035 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2036 struct device_attribute
*attr
,
2037 const char *buf
, size_t count
)
2041 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2042 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2045 if (mutex_lock_killable(&hotkey_mutex
))
2046 return -ERESTARTSYS
;
2048 HOTKEY_CONFIG_CRITICAL_START
2049 hotkey_source_mask
= t
;
2050 HOTKEY_CONFIG_CRITICAL_END
2052 hotkey_poll_setup(1);
2054 mutex_unlock(&hotkey_mutex
);
2059 static struct device_attribute dev_attr_hotkey_source_mask
=
2060 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2061 hotkey_source_mask_show
, hotkey_source_mask_store
);
2063 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2064 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2065 struct device_attribute
*attr
,
2068 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2071 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2072 struct device_attribute
*attr
,
2073 const char *buf
, size_t count
)
2077 if (parse_strtoul(buf
, 25, &t
))
2080 if (mutex_lock_killable(&hotkey_mutex
))
2081 return -ERESTARTSYS
;
2083 hotkey_poll_freq
= t
;
2085 hotkey_poll_setup(1);
2086 mutex_unlock(&hotkey_mutex
);
2091 static struct device_attribute dev_attr_hotkey_poll_freq
=
2092 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2093 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2095 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2097 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2098 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2099 struct device_attribute
*attr
,
2103 res
= hotkey_get_wlsw(&s
);
2107 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2110 static struct device_attribute dev_attr_hotkey_radio_sw
=
2111 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2113 static void hotkey_radio_sw_notify_change(void)
2115 if (tp_features
.hotkey_wlsw
)
2116 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2120 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2121 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2122 struct device_attribute
*attr
,
2126 res
= hotkey_get_tablet_mode(&s
);
2130 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2133 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2134 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2136 static void hotkey_tablet_mode_notify_change(void)
2138 if (tp_features
.hotkey_tablet
)
2139 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2140 "hotkey_tablet_mode");
2143 /* sysfs hotkey report_mode -------------------------------------------- */
2144 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2145 struct device_attribute
*attr
,
2148 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2149 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2152 static struct device_attribute dev_attr_hotkey_report_mode
=
2153 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2155 /* sysfs wakeup reason (pollable) -------------------------------------- */
2156 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2157 struct device_attribute
*attr
,
2160 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2163 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2164 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2166 static void hotkey_wakeup_reason_notify_change(void)
2168 if (tp_features
.hotkey_mask
)
2169 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2173 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2174 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2175 struct device_attribute
*attr
,
2178 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2181 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2182 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2183 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2185 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2187 if (tp_features
.hotkey_mask
)
2188 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2189 "wakeup_hotunplug_complete");
2192 /* --------------------------------------------------------------------- */
2194 static struct attribute
*hotkey_attributes
[] __initdata
= {
2195 &dev_attr_hotkey_enable
.attr
,
2196 &dev_attr_hotkey_bios_enabled
.attr
,
2197 &dev_attr_hotkey_report_mode
.attr
,
2198 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2199 &dev_attr_hotkey_mask
.attr
,
2200 &dev_attr_hotkey_all_mask
.attr
,
2201 &dev_attr_hotkey_recommended_mask
.attr
,
2202 &dev_attr_hotkey_source_mask
.attr
,
2203 &dev_attr_hotkey_poll_freq
.attr
,
2207 static struct attribute
*hotkey_mask_attributes
[] __initdata
= {
2208 &dev_attr_hotkey_bios_mask
.attr
,
2209 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2210 &dev_attr_hotkey_mask
.attr
,
2211 &dev_attr_hotkey_all_mask
.attr
,
2212 &dev_attr_hotkey_recommended_mask
.attr
,
2214 &dev_attr_hotkey_wakeup_reason
.attr
,
2215 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2218 static void bluetooth_update_rfk(void);
2219 static void wan_update_rfk(void);
2220 static void uwb_update_rfk(void);
2221 static void tpacpi_send_radiosw_update(void)
2225 /* Sync these BEFORE sending any rfkill events */
2226 if (tp_features
.bluetooth
)
2227 bluetooth_update_rfk();
2228 if (tp_features
.wan
)
2230 if (tp_features
.uwb
)
2233 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&wlsw
)) {
2234 mutex_lock(&tpacpi_inputdev_send_mutex
);
2236 input_report_switch(tpacpi_inputdev
,
2237 SW_RFKILL_ALL
, !!wlsw
);
2238 input_sync(tpacpi_inputdev
);
2240 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2242 hotkey_radio_sw_notify_change();
2245 static void hotkey_exit(void)
2247 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2248 hotkey_poll_stop_sync();
2251 if (hotkey_dev_attributes
)
2252 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2254 kfree(hotkey_keycode_map
);
2256 if (tp_features
.hotkey
) {
2257 dbg_printk(TPACPI_DBG_EXIT
,
2258 "restoring original hot key mask\n");
2259 /* no short-circuit boolean operator below! */
2260 if ((hotkey_mask_set(hotkey_orig_mask
) |
2261 hotkey_status_set(false)) != 0)
2263 "failed to restore hot key mask "
2264 "to BIOS defaults\n");
2268 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
2270 /* Requirements for changing the default keymaps:
2272 * 1. Many of the keys are mapped to KEY_RESERVED for very
2273 * good reasons. Do not change them unless you have deep
2274 * knowledge on the IBM and Lenovo ThinkPad firmware for
2275 * the various ThinkPad models. The driver behaves
2276 * differently for KEY_RESERVED: such keys have their
2277 * hot key mask *unset* in mask_recommended, and also
2278 * in the initial hot key mask programmed into the
2279 * firmware at driver load time, which means the firm-
2280 * ware may react very differently if you change them to
2283 * 2. You must be subscribed to the linux-thinkpad and
2284 * ibm-acpi-devel mailing lists, and you should read the
2285 * list archives since 2007 if you want to change the
2286 * keymaps. This requirement exists so that you will
2287 * know the past history of problems with the thinkpad-
2288 * acpi driver keymaps, and also that you will be
2289 * listening to any bug reports;
2291 * 3. Do not send thinkpad-acpi specific patches directly to
2292 * for merging, *ever*. Send them to the linux-acpi
2293 * mailinglist for comments. Merging is to be done only
2294 * through acpi-test and the ACPI maintainer.
2296 * If the above is too much to ask, don't change the keymap.
2297 * Ask the thinkpad-acpi maintainer to do it, instead.
2299 static u16 ibm_keycode_map
[] __initdata
= {
2300 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2301 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
2302 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2303 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2305 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2306 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2307 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2308 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2310 /* brightness: firmware always reacts to them, unless
2311 * X.org did some tricks in the radeon BIOS scratch
2312 * registers of *some* models */
2313 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2314 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2316 /* Thinklight: firmware always react to it */
2317 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2319 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2320 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2322 /* Volume: firmware always react to it and reprograms
2323 * the built-in *extra* mixer. Never map it to control
2324 * another mixer by default. */
2325 KEY_RESERVED
, /* 0x14: VOLUME UP */
2326 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2327 KEY_RESERVED
, /* 0x16: MUTE */
2329 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2331 /* (assignments unknown, please report if found) */
2332 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2333 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2335 static u16 lenovo_keycode_map
[] __initdata
= {
2336 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2337 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
2338 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
2339 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
2341 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2342 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
2343 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
2344 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
2346 /* These either have to go through ACPI video, or
2347 * act like in the IBM ThinkPads, so don't ever
2348 * enable them by default */
2349 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
2350 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
2352 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
2354 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
2355 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
2357 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2358 * react to it and reprograms the built-in *extra* mixer.
2359 * Never map it to control another mixer by default.
2361 * T60?, T61, R60?, R61: firmware and EC tries to send
2362 * these over the regular keyboard, so these are no-ops,
2363 * but there are still weird bugs re. MUTE, so do not
2364 * change unless you get test reports from all Lenovo
2365 * models. May cause the BIOS to interfere with the
2368 KEY_RESERVED
, /* 0x14: VOLUME UP */
2369 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
2370 KEY_RESERVED
, /* 0x16: MUTE */
2372 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2374 /* (assignments unknown, please report if found) */
2375 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2376 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
2379 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2380 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2381 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2387 vdbg_printk(TPACPI_DBG_INIT
, "initializing hotkey subdriver\n");
2389 BUG_ON(!tpacpi_inputdev
);
2390 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
2391 tpacpi_inputdev
->close
!= NULL
);
2393 TPACPI_ACPIHANDLE_INIT(hkey
);
2394 mutex_init(&hotkey_mutex
);
2396 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2397 mutex_init(&hotkey_thread_mutex
);
2398 mutex_init(&hotkey_thread_data_mutex
);
2401 /* hotkey not supported on 570 */
2402 tp_features
.hotkey
= hkey_handle
!= NULL
;
2404 vdbg_printk(TPACPI_DBG_INIT
, "hotkeys are %s\n",
2405 str_supported(tp_features
.hotkey
));
2407 if (!tp_features
.hotkey
)
2410 tpacpi_disable_brightness_delay();
2412 hotkey_dev_attributes
= create_attr_set(13, NULL
);
2413 if (!hotkey_dev_attributes
)
2415 res
= add_many_to_attr_set(hotkey_dev_attributes
,
2417 ARRAY_SIZE(hotkey_attributes
));
2421 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2422 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2423 for HKEY interface version 0x100 */
2424 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
2425 if ((hkeyv
>> 8) != 1) {
2426 printk(TPACPI_ERR
"unknown version of the "
2427 "HKEY interface: 0x%x\n", hkeyv
);
2428 printk(TPACPI_ERR
"please report this to %s\n",
2432 * MHKV 0x100 in A31, R40, R40e,
2433 * T4x, X31, and later
2435 tp_features
.hotkey_mask
= 1;
2439 vdbg_printk(TPACPI_DBG_INIT
, "hotkey masks are %s\n",
2440 str_supported(tp_features
.hotkey_mask
));
2442 if (tp_features
.hotkey_mask
) {
2443 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
2446 "missing MHKA handler, "
2447 "please report this to %s\n",
2449 /* FN+F12, FN+F4, FN+F3 */
2450 hotkey_all_mask
= 0x080cU
;
2454 /* hotkey_source_mask *must* be zero for
2455 * the first hotkey_mask_get */
2456 if (tp_features
.hotkey_mask
) {
2457 res
= hotkey_mask_get();
2461 hotkey_orig_mask
= hotkey_mask
;
2462 res
= add_many_to_attr_set(
2463 hotkey_dev_attributes
,
2464 hotkey_mask_attributes
,
2465 ARRAY_SIZE(hotkey_mask_attributes
));
2470 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2471 if (tp_features
.hotkey_mask
) {
2472 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
2475 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
;
2478 vdbg_printk(TPACPI_DBG_INIT
,
2479 "hotkey source mask 0x%08x, polling freq %d\n",
2480 hotkey_source_mask
, hotkey_poll_freq
);
2483 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2485 tp_features
.hotkey_wlsw
= 1;
2487 "radio switch emulation enabled\n");
2490 /* Not all thinkpads have a hardware radio switch */
2491 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
2492 tp_features
.hotkey_wlsw
= 1;
2494 "radio switch found; radios are %s\n",
2495 enabled(status
, 0));
2497 if (tp_features
.hotkey_wlsw
)
2498 res
= add_to_attr_set(hotkey_dev_attributes
,
2499 &dev_attr_hotkey_radio_sw
.attr
);
2501 /* For X41t, X60t, X61t Tablets... */
2502 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
2503 tp_features
.hotkey_tablet
= 1;
2505 "possible tablet mode switch found; "
2506 "ThinkPad in %s mode\n",
2507 (status
& TP_HOTKEY_TABLET_MASK
)?
2508 "tablet" : "laptop");
2509 res
= add_to_attr_set(hotkey_dev_attributes
,
2510 &dev_attr_hotkey_tablet_mode
.attr
);
2514 res
= register_attr_set_with_sysfs(
2515 hotkey_dev_attributes
,
2516 &tpacpi_pdev
->dev
.kobj
);
2520 /* Set up key map */
2522 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
2524 if (!hotkey_keycode_map
) {
2526 "failed to allocate memory for key map\n");
2531 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
2532 dbg_printk(TPACPI_DBG_INIT
,
2533 "using Lenovo default hot key map\n");
2534 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
2535 TPACPI_HOTKEY_MAP_SIZE
);
2537 dbg_printk(TPACPI_DBG_INIT
,
2538 "using IBM default hot key map\n");
2539 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
2540 TPACPI_HOTKEY_MAP_SIZE
);
2543 set_bit(EV_KEY
, tpacpi_inputdev
->evbit
);
2544 set_bit(EV_MSC
, tpacpi_inputdev
->evbit
);
2545 set_bit(MSC_SCAN
, tpacpi_inputdev
->mscbit
);
2546 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
2547 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
2548 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
2549 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
2550 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
2551 set_bit(hotkey_keycode_map
[i
],
2552 tpacpi_inputdev
->keybit
);
2554 if (i
< sizeof(hotkey_reserved_mask
)*8)
2555 hotkey_reserved_mask
|= 1 << i
;
2559 if (tp_features
.hotkey_wlsw
) {
2560 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2561 set_bit(SW_RFKILL_ALL
, tpacpi_inputdev
->swbit
);
2563 if (tp_features
.hotkey_tablet
) {
2564 set_bit(EV_SW
, tpacpi_inputdev
->evbit
);
2565 set_bit(SW_TABLET_MODE
, tpacpi_inputdev
->swbit
);
2568 /* Do not issue duplicate brightness change events to
2570 if (!tp_features
.bright_acpimode
)
2571 /* update bright_acpimode... */
2572 tpacpi_check_std_acpi_brightness_support();
2574 if (tp_features
.bright_acpimode
) {
2576 "This ThinkPad has standard ACPI backlight "
2577 "brightness control, supported by the ACPI "
2579 printk(TPACPI_NOTICE
2580 "Disabling thinkpad-acpi brightness events "
2583 /* The hotkey_reserved_mask change below is not
2584 * necessary while the keys are at KEY_RESERVED in the
2585 * default map, but better safe than sorry, leave it
2586 * here as a marker of what we have to do, especially
2587 * when we finally become able to set this at runtime
2588 * on response to X.org requests */
2589 hotkey_reserved_mask
|=
2590 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
2591 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
2594 dbg_printk(TPACPI_DBG_INIT
, "enabling hot key handling\n");
2595 res
= hotkey_status_set(true);
2600 res
= hotkey_mask_set(((hotkey_all_mask
| hotkey_source_mask
)
2601 & ~hotkey_reserved_mask
)
2602 | hotkey_orig_mask
);
2603 if (res
< 0 && res
!= -ENXIO
) {
2608 dbg_printk(TPACPI_DBG_INIT
,
2609 "legacy hot key reporting over procfs %s\n",
2610 (hotkey_report_mode
< 2) ?
2611 "enabled" : "disabled");
2613 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
2614 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
2616 hotkey_poll_setup_safe(1);
2617 tpacpi_send_radiosw_update();
2618 tpacpi_input_send_tabletsw();
2623 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
2624 hotkey_dev_attributes
= NULL
;
2626 return (res
< 0)? res
: 1;
2629 static bool hotkey_notify_hotkey(const u32 hkey
,
2631 bool *ignore_acpi_ev
)
2633 /* 0x1000-0x1FFF: key presses */
2634 unsigned int scancode
= hkey
& 0xfff;
2635 *send_acpi_ev
= true;
2636 *ignore_acpi_ev
= false;
2638 if (scancode
> 0 && scancode
< 0x21) {
2640 if (!(hotkey_source_mask
& (1 << scancode
))) {
2641 tpacpi_input_send_key(scancode
);
2642 *send_acpi_ev
= false;
2644 *ignore_acpi_ev
= true;
2651 static bool hotkey_notify_wakeup(const u32 hkey
,
2653 bool *ignore_acpi_ev
)
2655 /* 0x2000-0x2FFF: Wakeup reason */
2656 *send_acpi_ev
= true;
2657 *ignore_acpi_ev
= false;
2660 case 0x2304: /* suspend, undock */
2661 case 0x2404: /* hibernation, undock */
2662 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
2663 *ignore_acpi_ev
= true;
2666 case 0x2305: /* suspend, bay eject */
2667 case 0x2405: /* hibernation, bay eject */
2668 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
2669 *ignore_acpi_ev
= true;
2672 case 0x2313: /* Battery on critical low level (S3) */
2673 case 0x2413: /* Battery on critical low level (S4) */
2675 "EMERGENCY WAKEUP: battery almost empty\n");
2676 /* how to auto-heal: */
2677 /* 2313: woke up from S3, go to S4/S5 */
2678 /* 2413: woke up from S4, go to S5 */
2685 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
2687 "woke up due to a hot-unplug "
2689 hotkey_wakeup_reason_notify_change();
2694 static bool hotkey_notify_usrevent(const u32 hkey
,
2696 bool *ignore_acpi_ev
)
2698 /* 0x5000-0x5FFF: human interface helpers */
2699 *send_acpi_ev
= true;
2700 *ignore_acpi_ev
= false;
2703 case 0x5010: /* Lenovo new BIOS: brightness changed */
2704 case 0x500b: /* X61t: tablet pen inserted into bay */
2705 case 0x500c: /* X61t: tablet pen removed from bay */
2708 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2709 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2710 tpacpi_input_send_tabletsw();
2711 hotkey_tablet_mode_notify_change();
2712 *send_acpi_ev
= false;
2717 /* LID switch events. Do not propagate */
2718 *ignore_acpi_ev
= true;
2726 static bool hotkey_notify_thermal(const u32 hkey
,
2728 bool *ignore_acpi_ev
)
2730 /* 0x6000-0x6FFF: thermal alarms */
2731 *send_acpi_ev
= true;
2732 *ignore_acpi_ev
= false;
2737 "THERMAL ALARM: battery is too hot!\n");
2738 /* recommended action: warn user through gui */
2742 "THERMAL EMERGENCY: battery is extremely hot!\n");
2743 /* recommended action: immediate sleep/hibernate */
2748 "a sensor reports something is too hot!\n");
2749 /* recommended action: warn user through gui, that */
2750 /* some internal component is too hot */
2754 "THERMAL EMERGENCY: "
2755 "a sensor reports something is extremely hot!\n");
2756 /* recommended action: immediate sleep/hibernate */
2760 "EC reports that Thermal Table has changed\n");
2761 /* recommended action: do nothing, we don't have
2762 * Lenovo ATM information */
2766 "THERMAL ALERT: unknown thermal alarm received\n");
2771 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
2775 bool ignore_acpi_ev
;
2778 if (event
!= 0x80) {
2780 "unknown HKEY notification event %d\n", event
);
2781 /* forward it to userspace, maybe it knows how to handle it */
2782 acpi_bus_generate_netlink_event(
2783 ibm
->acpi
->device
->pnp
.device_class
,
2784 dev_name(&ibm
->acpi
->device
->dev
),
2790 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
2791 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
2800 send_acpi_ev
= true;
2801 ignore_acpi_ev
= false;
2803 switch (hkey
>> 12) {
2805 /* 0x1000-0x1FFF: key presses */
2806 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
2810 /* 0x2000-0x2FFF: Wakeup reason */
2811 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
2815 /* 0x3000-0x3FFF: bay-related wakeups */
2816 if (hkey
== 0x3003) {
2817 hotkey_autosleep_ack
= 1;
2820 hotkey_wakeup_hotunplug_complete_notify_change();
2827 /* 0x4000-0x4FFF: dock-related wakeups */
2828 if (hkey
== 0x4003) {
2829 hotkey_autosleep_ack
= 1;
2832 hotkey_wakeup_hotunplug_complete_notify_change();
2839 /* 0x5000-0x5FFF: human interface helpers */
2840 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
2844 /* 0x6000-0x6FFF: thermal alarms */
2845 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
2849 /* 0x7000-0x7FFF: misc */
2850 if (tp_features
.hotkey_wlsw
&& hkey
== 0x7000) {
2851 tpacpi_send_radiosw_update();
2856 /* fallthrough to default */
2861 printk(TPACPI_NOTICE
2862 "unhandled HKEY event 0x%04x\n", hkey
);
2863 printk(TPACPI_NOTICE
2864 "please report the conditions when this "
2865 "event happened to %s\n", TPACPI_MAIL
);
2869 if (!ignore_acpi_ev
&&
2870 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
2871 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
2875 /* netlink events */
2876 if (!ignore_acpi_ev
&& send_acpi_ev
) {
2877 acpi_bus_generate_netlink_event(
2878 ibm
->acpi
->device
->pnp
.device_class
,
2879 dev_name(&ibm
->acpi
->device
->dev
),
2885 static void hotkey_suspend(pm_message_t state
)
2887 /* Do these on suspend, we get the events on early resume! */
2888 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
2889 hotkey_autosleep_ack
= 0;
2892 static void hotkey_resume(void)
2894 tpacpi_disable_brightness_delay();
2896 if (hotkey_mask_get())
2898 "error while trying to read hot key mask "
2900 tpacpi_send_radiosw_update();
2901 hotkey_tablet_mode_notify_change();
2902 hotkey_wakeup_reason_notify_change();
2903 hotkey_wakeup_hotunplug_complete_notify_change();
2904 hotkey_poll_setup_safe(0);
2907 /* procfs -------------------------------------------------------------- */
2908 static int hotkey_read(char *p
)
2913 if (!tp_features
.hotkey
) {
2914 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
2918 if (mutex_lock_killable(&hotkey_mutex
))
2919 return -ERESTARTSYS
;
2920 res
= hotkey_status_get(&status
);
2922 res
= hotkey_mask_get();
2923 mutex_unlock(&hotkey_mutex
);
2927 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
2928 if (tp_features
.hotkey_mask
) {
2929 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_mask
);
2930 len
+= sprintf(p
+ len
,
2931 "commands:\tenable, disable, reset, <mask>\n");
2933 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
2934 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
2940 static void hotkey_enabledisable_warn(void)
2942 tpacpi_log_usertask("procfs hotkey enable/disable");
2944 "hotkey enable/disable functionality has been "
2945 "removed from the driver. Hotkeys are always enabled.\n");
2948 static int hotkey_write(char *buf
)
2954 if (!tp_features
.hotkey
)
2957 if (mutex_lock_killable(&hotkey_mutex
))
2958 return -ERESTARTSYS
;
2963 while ((cmd
= next_cmd(&buf
))) {
2964 if (strlencmp(cmd
, "enable") == 0) {
2965 hotkey_enabledisable_warn();
2966 } else if (strlencmp(cmd
, "disable") == 0) {
2967 hotkey_enabledisable_warn();
2969 } else if (strlencmp(cmd
, "reset") == 0) {
2970 mask
= hotkey_orig_mask
;
2971 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
2973 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
2980 if (!res
&& mask
!= hotkey_mask
)
2981 res
= hotkey_mask_set(mask
);
2984 mutex_unlock(&hotkey_mutex
);
2988 static const struct acpi_device_id ibm_htk_device_ids
[] = {
2989 {TPACPI_ACPI_HKEY_HID
, 0},
2993 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
2994 .hid
= ibm_htk_device_ids
,
2995 .notify
= hotkey_notify
,
2996 .handle
= &hkey_handle
,
2997 .type
= ACPI_DEVICE_NOTIFY
,
3000 static struct ibm_struct hotkey_driver_data
= {
3002 .read
= hotkey_read
,
3003 .write
= hotkey_write
,
3004 .exit
= hotkey_exit
,
3005 .resume
= hotkey_resume
,
3006 .suspend
= hotkey_suspend
,
3007 .acpi
= &ibm_hotkey_acpidriver
,
3010 /*************************************************************************
3011 * Bluetooth subdriver
3015 /* ACPI GBDC/SBDC bits */
3016 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3017 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3018 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3023 /* ACPI \BLTH commands */
3024 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3025 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3026 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3027 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3028 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3031 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3033 static struct rfkill
*tpacpi_bluetooth_rfkill
;
3035 static void bluetooth_suspend(pm_message_t state
)
3037 /* Try to make sure radio will resume powered off */
3038 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3039 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
))
3040 vdbg_printk(TPACPI_DBG_RFKILL
,
3041 "bluetooth power down on resume request failed\n");
3044 static int bluetooth_get_radiosw(void)
3048 if (!tp_features
.bluetooth
)
3051 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3052 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3053 return RFKILL_STATE_HARD_BLOCKED
;
3055 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3056 if (dbg_bluetoothemul
)
3057 return (tpacpi_bluetooth_emulstate
) ?
3058 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3061 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3064 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3065 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3068 static void bluetooth_update_rfk(void)
3072 if (!tpacpi_bluetooth_rfkill
)
3075 status
= bluetooth_get_radiosw();
3078 rfkill_force_state(tpacpi_bluetooth_rfkill
, status
);
3080 vdbg_printk(TPACPI_DBG_RFKILL
,
3081 "forced rfkill state to %d\n",
3085 static int bluetooth_set_radiosw(int radio_on
, int update_rfk
)
3089 if (!tp_features
.bluetooth
)
3092 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3093 * reason to risk weird behaviour. */
3094 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3098 vdbg_printk(TPACPI_DBG_RFKILL
,
3099 "will %s bluetooth\n", radio_on
? "enable" : "disable");
3101 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3102 if (dbg_bluetoothemul
) {
3103 tpacpi_bluetooth_emulstate
= !!radio_on
;
3105 bluetooth_update_rfk();
3110 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3112 status
= TP_ACPI_BLUETOOTH_RADIOSSW
;
3115 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3119 bluetooth_update_rfk();
3124 /* sysfs bluetooth enable ---------------------------------------------- */
3125 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3126 struct device_attribute
*attr
,
3131 printk_deprecated_rfkill_attribute("bluetooth_enable");
3133 status
= bluetooth_get_radiosw();
3137 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3138 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3141 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3142 struct device_attribute
*attr
,
3143 const char *buf
, size_t count
)
3148 printk_deprecated_rfkill_attribute("bluetooth_enable");
3150 if (parse_strtoul(buf
, 1, &t
))
3153 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t
);
3155 res
= bluetooth_set_radiosw(t
, 1);
3157 return (res
) ? res
: count
;
3160 static struct device_attribute dev_attr_bluetooth_enable
=
3161 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3162 bluetooth_enable_show
, bluetooth_enable_store
);
3164 /* --------------------------------------------------------------------- */
3166 static struct attribute
*bluetooth_attributes
[] = {
3167 &dev_attr_bluetooth_enable
.attr
,
3171 static const struct attribute_group bluetooth_attr_group
= {
3172 .attrs
= bluetooth_attributes
,
3175 static int tpacpi_bluetooth_rfk_get(void *data
, enum rfkill_state
*state
)
3177 int bts
= bluetooth_get_radiosw();
3186 static int tpacpi_bluetooth_rfk_set(void *data
, enum rfkill_state state
)
3188 dbg_printk(TPACPI_DBG_RFKILL
,
3189 "request to change radio state to %d\n", state
);
3190 return bluetooth_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3193 static void bluetooth_shutdown(void)
3195 /* Order firmware to save current state to NVRAM */
3196 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3197 TP_ACPI_BLTH_SAVE_STATE
))
3198 printk(TPACPI_NOTICE
3199 "failed to save bluetooth state to NVRAM\n");
3201 vdbg_printk(TPACPI_DBG_RFKILL
,
3202 "bluestooth state saved to NVRAM\n");
3205 static void bluetooth_exit(void)
3207 bluetooth_shutdown();
3209 if (tpacpi_bluetooth_rfkill
)
3210 rfkill_unregister(tpacpi_bluetooth_rfkill
);
3212 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3213 &bluetooth_attr_group
);
3216 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3221 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3222 "initializing bluetooth subdriver\n");
3224 TPACPI_ACPIHANDLE_INIT(hkey
);
3226 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3227 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3228 tp_features
.bluetooth
= hkey_handle
&&
3229 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
3231 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3232 "bluetooth is %s, status 0x%02x\n",
3233 str_supported(tp_features
.bluetooth
),
3236 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3237 if (dbg_bluetoothemul
) {
3238 tp_features
.bluetooth
= 1;
3240 "bluetooth switch emulation enabled\n");
3243 if (tp_features
.bluetooth
&&
3244 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
3245 /* no bluetooth hardware present in system */
3246 tp_features
.bluetooth
= 0;
3247 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3248 "bluetooth hardware not installed\n");
3251 if (!tp_features
.bluetooth
)
3254 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3255 &bluetooth_attr_group
);
3259 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
3260 &tpacpi_bluetooth_rfkill
,
3261 RFKILL_TYPE_BLUETOOTH
,
3262 TPACPI_RFK_BLUETOOTH_SW_NAME
,
3264 tpacpi_bluetooth_rfk_set
,
3265 tpacpi_bluetooth_rfk_get
);
3274 /* procfs -------------------------------------------------------------- */
3275 static int bluetooth_read(char *p
)
3278 int status
= bluetooth_get_radiosw();
3280 if (!tp_features
.bluetooth
)
3281 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3283 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3284 (status
== RFKILL_STATE_UNBLOCKED
) ?
3285 "enabled" : "disabled");
3286 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3292 static int bluetooth_write(char *buf
)
3297 if (!tp_features
.bluetooth
)
3300 while ((cmd
= next_cmd(&buf
))) {
3301 if (strlencmp(cmd
, "enable") == 0) {
3303 } else if (strlencmp(cmd
, "disable") == 0) {
3310 tpacpi_disclose_usertask("procfs bluetooth",
3312 state
? "enable" : "disable");
3313 bluetooth_set_radiosw(state
, 1);
3319 static struct ibm_struct bluetooth_driver_data
= {
3320 .name
= "bluetooth",
3321 .read
= bluetooth_read
,
3322 .write
= bluetooth_write
,
3323 .exit
= bluetooth_exit
,
3324 .suspend
= bluetooth_suspend
,
3325 .shutdown
= bluetooth_shutdown
,
3328 /*************************************************************************
3333 /* ACPI GWAN/SWAN bits */
3334 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
3335 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
3336 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
3340 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3342 static struct rfkill
*tpacpi_wan_rfkill
;
3344 static void wan_suspend(pm_message_t state
)
3346 /* Try to make sure radio will resume powered off */
3347 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "qvd",
3348 TP_ACPI_WGSV_PWR_OFF_ON_RESUME
))
3349 vdbg_printk(TPACPI_DBG_RFKILL
,
3350 "WWAN power down on resume request failed\n");
3353 static int wan_get_radiosw(void)
3357 if (!tp_features
.wan
)
3360 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3361 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3362 return RFKILL_STATE_HARD_BLOCKED
;
3364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3366 return (tpacpi_wwan_emulstate
) ?
3367 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3370 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
3373 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
3374 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3377 static void wan_update_rfk(void)
3381 if (!tpacpi_wan_rfkill
)
3384 status
= wan_get_radiosw();
3387 rfkill_force_state(tpacpi_wan_rfkill
, status
);
3389 vdbg_printk(TPACPI_DBG_RFKILL
,
3390 "forced rfkill state to %d\n",
3394 static int wan_set_radiosw(int radio_on
, int update_rfk
)
3398 if (!tp_features
.wan
)
3401 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3402 * reason to risk weird behaviour. */
3403 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3407 vdbg_printk(TPACPI_DBG_RFKILL
,
3408 "will %s WWAN\n", radio_on
? "enable" : "disable");
3410 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3412 tpacpi_wwan_emulstate
= !!radio_on
;
3419 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3421 status
= TP_ACPI_WANCARD_RADIOSSW
;
3424 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
3433 /* sysfs wan enable ---------------------------------------------------- */
3434 static ssize_t
wan_enable_show(struct device
*dev
,
3435 struct device_attribute
*attr
,
3440 printk_deprecated_rfkill_attribute("wwan_enable");
3442 status
= wan_get_radiosw();
3446 return snprintf(buf
, PAGE_SIZE
, "%d\n",
3447 (status
== RFKILL_STATE_UNBLOCKED
) ? 1 : 0);
3450 static ssize_t
wan_enable_store(struct device
*dev
,
3451 struct device_attribute
*attr
,
3452 const char *buf
, size_t count
)
3457 printk_deprecated_rfkill_attribute("wwan_enable");
3459 if (parse_strtoul(buf
, 1, &t
))
3462 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t
);
3464 res
= wan_set_radiosw(t
, 1);
3466 return (res
) ? res
: count
;
3469 static struct device_attribute dev_attr_wan_enable
=
3470 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
3471 wan_enable_show
, wan_enable_store
);
3473 /* --------------------------------------------------------------------- */
3475 static struct attribute
*wan_attributes
[] = {
3476 &dev_attr_wan_enable
.attr
,
3480 static const struct attribute_group wan_attr_group
= {
3481 .attrs
= wan_attributes
,
3484 static int tpacpi_wan_rfk_get(void *data
, enum rfkill_state
*state
)
3486 int wans
= wan_get_radiosw();
3495 static int tpacpi_wan_rfk_set(void *data
, enum rfkill_state state
)
3497 dbg_printk(TPACPI_DBG_RFKILL
,
3498 "request to change radio state to %d\n", state
);
3499 return wan_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3502 static void wan_shutdown(void)
3504 /* Order firmware to save current state to NVRAM */
3505 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
3506 TP_ACPI_WGSV_SAVE_STATE
))
3507 printk(TPACPI_NOTICE
3508 "failed to save WWAN state to NVRAM\n");
3510 vdbg_printk(TPACPI_DBG_RFKILL
,
3511 "WWAN state saved to NVRAM\n");
3514 static void wan_exit(void)
3518 if (tpacpi_wan_rfkill
)
3519 rfkill_unregister(tpacpi_wan_rfkill
);
3521 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3525 static int __init
wan_init(struct ibm_init_struct
*iibm
)
3530 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3531 "initializing wan subdriver\n");
3533 TPACPI_ACPIHANDLE_INIT(hkey
);
3535 tp_features
.wan
= hkey_handle
&&
3536 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
3538 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3539 "wan is %s, status 0x%02x\n",
3540 str_supported(tp_features
.wan
),
3543 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3545 tp_features
.wan
= 1;
3547 "wwan switch emulation enabled\n");
3550 if (tp_features
.wan
&&
3551 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
3552 /* no wan hardware present in system */
3553 tp_features
.wan
= 0;
3554 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3555 "wan hardware not installed\n");
3558 if (!tp_features
.wan
)
3561 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
3566 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
3569 TPACPI_RFK_WWAN_SW_NAME
,
3572 tpacpi_wan_rfk_get
);
3581 /* procfs -------------------------------------------------------------- */
3582 static int wan_read(char *p
)
3585 int status
= wan_get_radiosw();
3587 tpacpi_disclose_usertask("procfs wan", "read");
3589 if (!tp_features
.wan
)
3590 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3592 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
3593 (status
== RFKILL_STATE_UNBLOCKED
) ?
3594 "enabled" : "disabled");
3595 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
3601 static int wan_write(char *buf
)
3606 if (!tp_features
.wan
)
3609 while ((cmd
= next_cmd(&buf
))) {
3610 if (strlencmp(cmd
, "enable") == 0) {
3612 } else if (strlencmp(cmd
, "disable") == 0) {
3619 tpacpi_disclose_usertask("procfs wan",
3621 state
? "enable" : "disable");
3622 wan_set_radiosw(state
, 1);
3628 static struct ibm_struct wan_driver_data
= {
3633 .suspend
= wan_suspend
,
3634 .shutdown
= wan_shutdown
,
3637 /*************************************************************************
3642 /* ACPI GUWB/SUWB bits */
3643 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
3644 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
3647 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3649 static struct rfkill
*tpacpi_uwb_rfkill
;
3651 static int uwb_get_radiosw(void)
3655 if (!tp_features
.uwb
)
3658 /* WLSW overrides UWB in firmware/hardware, reflect that */
3659 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
)
3660 return RFKILL_STATE_HARD_BLOCKED
;
3662 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3664 return (tpacpi_uwb_emulstate
) ?
3665 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3668 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
3671 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
3672 RFKILL_STATE_UNBLOCKED
: RFKILL_STATE_SOFT_BLOCKED
;
3675 static void uwb_update_rfk(void)
3679 if (!tpacpi_uwb_rfkill
)
3682 status
= uwb_get_radiosw();
3685 rfkill_force_state(tpacpi_uwb_rfkill
, status
);
3687 vdbg_printk(TPACPI_DBG_RFKILL
,
3688 "forced rfkill state to %d\n",
3692 static int uwb_set_radiosw(int radio_on
, int update_rfk
)
3696 if (!tp_features
.uwb
)
3699 /* WLSW overrides UWB in firmware/hardware, but there is no
3700 * reason to risk weird behaviour. */
3701 if (tp_features
.hotkey_wlsw
&& !hotkey_get_wlsw(&status
) && !status
3705 vdbg_printk(TPACPI_DBG_RFKILL
,
3706 "will %s UWB\n", radio_on
? "enable" : "disable");
3708 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3710 tpacpi_uwb_emulstate
= !!radio_on
;
3717 status
= (radio_on
) ? TP_ACPI_UWB_RADIOSSW
: 0;
3718 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
3727 /* --------------------------------------------------------------------- */
3729 static int tpacpi_uwb_rfk_get(void *data
, enum rfkill_state
*state
)
3731 int uwbs
= uwb_get_radiosw();
3740 static int tpacpi_uwb_rfk_set(void *data
, enum rfkill_state state
)
3742 dbg_printk(TPACPI_DBG_RFKILL
,
3743 "request to change radio state to %d\n", state
);
3744 return uwb_set_radiosw((state
== RFKILL_STATE_UNBLOCKED
), 0);
3747 static void uwb_exit(void)
3749 if (tpacpi_uwb_rfkill
)
3750 rfkill_unregister(tpacpi_uwb_rfkill
);
3753 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
3758 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3759 "initializing uwb subdriver\n");
3761 TPACPI_ACPIHANDLE_INIT(hkey
);
3763 tp_features
.uwb
= hkey_handle
&&
3764 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
3766 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3767 "uwb is %s, status 0x%02x\n",
3768 str_supported(tp_features
.uwb
),
3771 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3773 tp_features
.uwb
= 1;
3775 "uwb switch emulation enabled\n");
3778 if (tp_features
.uwb
&&
3779 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
3780 /* no uwb hardware present in system */
3781 tp_features
.uwb
= 0;
3782 dbg_printk(TPACPI_DBG_INIT
,
3783 "uwb hardware not installed\n");
3786 if (!tp_features
.uwb
)
3789 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
3792 TPACPI_RFK_UWB_SW_NAME
,
3795 tpacpi_uwb_rfk_get
);
3800 static struct ibm_struct uwb_driver_data
= {
3803 .flags
.experimental
= 1,
3806 /*************************************************************************
3810 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3812 enum video_access_mode
{
3813 TPACPI_VIDEO_NONE
= 0,
3814 TPACPI_VIDEO_570
, /* 570 */
3815 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
3816 TPACPI_VIDEO_NEW
, /* all others */
3819 enum { /* video status flags, based on VIDEO_570 */
3820 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
3821 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
3822 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
3825 enum { /* TPACPI_VIDEO_570 constants */
3826 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
3827 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
3828 * video_status_flags */
3829 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
3830 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
3833 static enum video_access_mode video_supported
;
3834 static int video_orig_autosw
;
3836 static int video_autosw_get(void);
3837 static int video_autosw_set(int enable
);
3839 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3841 static int __init
video_init(struct ibm_init_struct
*iibm
)
3845 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
3847 TPACPI_ACPIHANDLE_INIT(vid
);
3848 TPACPI_ACPIHANDLE_INIT(vid2
);
3850 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
3851 /* G41, assume IVGA doesn't change */
3852 vid_handle
= vid2_handle
;
3855 /* video switching not supported on R30, R31 */
3856 video_supported
= TPACPI_VIDEO_NONE
;
3857 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
3859 video_supported
= TPACPI_VIDEO_570
;
3860 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
3861 /* 600e/x, 770e, 770x */
3862 video_supported
= TPACPI_VIDEO_770
;
3865 video_supported
= TPACPI_VIDEO_NEW
;
3867 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
3868 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
3871 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
3874 static void video_exit(void)
3876 dbg_printk(TPACPI_DBG_EXIT
,
3877 "restoring original video autoswitch mode\n");
3878 if (video_autosw_set(video_orig_autosw
))
3879 printk(TPACPI_ERR
"error while trying to restore original "
3880 "video autoswitch mode\n");
3883 static int video_outputsw_get(void)
3888 switch (video_supported
) {
3889 case TPACPI_VIDEO_570
:
3890 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
3891 TP_ACPI_VIDEO_570_PHSCMD
))
3893 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
3895 case TPACPI_VIDEO_770
:
3896 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3899 status
|= TP_ACPI_VIDEO_S_LCD
;
3900 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3903 status
|= TP_ACPI_VIDEO_S_CRT
;
3905 case TPACPI_VIDEO_NEW
:
3906 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
3907 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
3910 status
|= TP_ACPI_VIDEO_S_CRT
;
3912 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
3913 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
3916 status
|= TP_ACPI_VIDEO_S_LCD
;
3917 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
3920 status
|= TP_ACPI_VIDEO_S_DVI
;
3929 static int video_outputsw_set(int status
)
3934 switch (video_supported
) {
3935 case TPACPI_VIDEO_570
:
3936 res
= acpi_evalf(NULL
, NULL
,
3937 "\\_SB.PHS2", "vdd",
3938 TP_ACPI_VIDEO_570_PHS2CMD
,
3939 status
| TP_ACPI_VIDEO_570_PHS2SET
);
3941 case TPACPI_VIDEO_770
:
3942 autosw
= video_autosw_get();
3946 res
= video_autosw_set(1);
3949 res
= acpi_evalf(vid_handle
, NULL
,
3950 "ASWT", "vdd", status
* 0x100, 0);
3951 if (!autosw
&& video_autosw_set(autosw
)) {
3953 "video auto-switch left enabled due to error\n");
3957 case TPACPI_VIDEO_NEW
:
3958 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
3959 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
3965 return (res
)? 0 : -EIO
;
3968 static int video_autosw_get(void)
3972 switch (video_supported
) {
3973 case TPACPI_VIDEO_570
:
3974 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
3977 case TPACPI_VIDEO_770
:
3978 case TPACPI_VIDEO_NEW
:
3979 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
3989 static int video_autosw_set(int enable
)
3991 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
3996 static int video_outputsw_cycle(void)
3998 int autosw
= video_autosw_get();
4004 switch (video_supported
) {
4005 case TPACPI_VIDEO_570
:
4006 res
= video_autosw_set(1);
4009 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4011 case TPACPI_VIDEO_770
:
4012 case TPACPI_VIDEO_NEW
:
4013 res
= video_autosw_set(1);
4016 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4021 if (!autosw
&& video_autosw_set(autosw
)) {
4023 "video auto-switch left enabled due to error\n");
4027 return (res
)? 0 : -EIO
;
4030 static int video_expand_toggle(void)
4032 switch (video_supported
) {
4033 case TPACPI_VIDEO_570
:
4034 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4036 case TPACPI_VIDEO_770
:
4037 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4039 case TPACPI_VIDEO_NEW
:
4040 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4048 static int video_read(char *p
)
4053 if (video_supported
== TPACPI_VIDEO_NONE
) {
4054 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4058 status
= video_outputsw_get();
4062 autosw
= video_autosw_get();
4066 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4067 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
4068 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
4069 if (video_supported
== TPACPI_VIDEO_NEW
)
4070 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
4071 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
4072 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
4073 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
4074 if (video_supported
== TPACPI_VIDEO_NEW
)
4075 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
4076 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
4077 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4082 static int video_write(char *buf
)
4085 int enable
, disable
, status
;
4088 if (video_supported
== TPACPI_VIDEO_NONE
)
4094 while ((cmd
= next_cmd(&buf
))) {
4095 if (strlencmp(cmd
, "lcd_enable") == 0) {
4096 enable
|= TP_ACPI_VIDEO_S_LCD
;
4097 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4098 disable
|= TP_ACPI_VIDEO_S_LCD
;
4099 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4100 enable
|= TP_ACPI_VIDEO_S_CRT
;
4101 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4102 disable
|= TP_ACPI_VIDEO_S_CRT
;
4103 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4104 strlencmp(cmd
, "dvi_enable") == 0) {
4105 enable
|= TP_ACPI_VIDEO_S_DVI
;
4106 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4107 strlencmp(cmd
, "dvi_disable") == 0) {
4108 disable
|= TP_ACPI_VIDEO_S_DVI
;
4109 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4110 res
= video_autosw_set(1);
4113 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4114 res
= video_autosw_set(0);
4117 } else if (strlencmp(cmd
, "video_switch") == 0) {
4118 res
= video_outputsw_cycle();
4121 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4122 res
= video_expand_toggle();
4129 if (enable
|| disable
) {
4130 status
= video_outputsw_get();
4133 res
= video_outputsw_set((status
& ~disable
) | enable
);
4141 static struct ibm_struct video_driver_data
= {
4144 .write
= video_write
,
4148 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4150 /*************************************************************************
4151 * Light (thinklight) subdriver
4154 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4155 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4157 static int light_get_status(void)
4161 if (tp_features
.light_status
) {
4162 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4170 static int light_set_status(int status
)
4174 if (tp_features
.light
) {
4176 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4178 TP_CMOS_THINKLIGHT_ON
:
4179 TP_CMOS_THINKLIGHT_OFF
);
4181 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4184 return (rc
)? 0 : -EIO
;
4190 static void light_set_status_worker(struct work_struct
*work
)
4192 struct tpacpi_led_classdev
*data
=
4193 container_of(work
, struct tpacpi_led_classdev
, work
);
4195 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4196 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4199 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4200 enum led_brightness brightness
)
4202 struct tpacpi_led_classdev
*data
=
4203 container_of(led_cdev
,
4204 struct tpacpi_led_classdev
,
4206 data
->new_state
= (brightness
!= LED_OFF
) ?
4207 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4208 queue_work(tpacpi_wq
, &data
->work
);
4211 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4213 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4216 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4218 .name
= "tpacpi::thinklight",
4219 .brightness_set
= &light_sysfs_set
,
4220 .brightness_get
= &light_sysfs_get
,
4224 static int __init
light_init(struct ibm_init_struct
*iibm
)
4228 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4230 TPACPI_ACPIHANDLE_INIT(ledb
);
4231 TPACPI_ACPIHANDLE_INIT(lght
);
4232 TPACPI_ACPIHANDLE_INIT(cmos
);
4233 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4235 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4236 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4238 if (tp_features
.light
)
4239 /* light status not supported on
4240 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4241 tp_features
.light_status
=
4242 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4244 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4245 str_supported(tp_features
.light
),
4246 str_supported(tp_features
.light_status
));
4248 if (!tp_features
.light
)
4251 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4252 &tpacpi_led_thinklight
.led_classdev
);
4255 tp_features
.light
= 0;
4256 tp_features
.light_status
= 0;
4264 static void light_exit(void)
4266 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4267 if (work_pending(&tpacpi_led_thinklight
.work
))
4268 flush_workqueue(tpacpi_wq
);
4271 static int light_read(char *p
)
4276 if (!tp_features
.light
) {
4277 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4278 } else if (!tp_features
.light_status
) {
4279 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4280 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4282 status
= light_get_status();
4285 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4286 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4292 static int light_write(char *buf
)
4297 if (!tp_features
.light
)
4300 while ((cmd
= next_cmd(&buf
))) {
4301 if (strlencmp(cmd
, "on") == 0) {
4303 } else if (strlencmp(cmd
, "off") == 0) {
4309 return light_set_status(newstatus
);
4312 static struct ibm_struct light_driver_data
= {
4315 .write
= light_write
,
4319 /*************************************************************************
4323 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4325 static void dock_notify(struct ibm_struct
*ibm
, u32 event
);
4326 static int dock_read(char *p
);
4327 static int dock_write(char *buf
);
4329 TPACPI_HANDLE(dock
, root
, "\\_SB.GDCK", /* X30, X31, X40 */
4330 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4331 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4332 "\\_SB.PCI.ISA.SLCE", /* 570 */
4333 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4335 /* don't list other alternatives as we install a notify handler on the 570 */
4336 TPACPI_HANDLE(pci
, root
, "\\_SB.PCI"); /* 570 */
4338 static const struct acpi_device_id ibm_pci_device_ids
[] = {
4339 {PCI_ROOT_HID_STRING
, 0},
4343 static struct tp_acpi_drv_struct ibm_dock_acpidriver
[2] = {
4345 .notify
= dock_notify
,
4346 .handle
= &dock_handle
,
4347 .type
= ACPI_SYSTEM_NOTIFY
,
4350 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4351 * We just use it to get notifications of dock hotplug
4352 * in very old thinkpads */
4353 .hid
= ibm_pci_device_ids
,
4354 .notify
= dock_notify
,
4355 .handle
= &pci_handle
,
4356 .type
= ACPI_SYSTEM_NOTIFY
,
4360 static struct ibm_struct dock_driver_data
[2] = {
4364 .write
= dock_write
,
4365 .acpi
= &ibm_dock_acpidriver
[0],
4369 .acpi
= &ibm_dock_acpidriver
[1],
4373 #define dock_docked() (_sta(dock_handle) & 1)
4375 static int __init
dock_init(struct ibm_init_struct
*iibm
)
4377 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver\n");
4379 TPACPI_ACPIHANDLE_INIT(dock
);
4381 vdbg_printk(TPACPI_DBG_INIT
, "dock is %s\n",
4382 str_supported(dock_handle
!= NULL
));
4384 return (dock_handle
)? 0 : 1;
4387 static int __init
dock_init2(struct ibm_init_struct
*iibm
)
4391 vdbg_printk(TPACPI_DBG_INIT
, "initializing dock subdriver part 2\n");
4393 if (dock_driver_data
[0].flags
.acpi_driver_registered
&&
4394 dock_driver_data
[0].flags
.acpi_notify_installed
) {
4395 TPACPI_ACPIHANDLE_INIT(pci
);
4396 dock2_needed
= (pci_handle
!= NULL
);
4397 vdbg_printk(TPACPI_DBG_INIT
,
4398 "dock PCI handler for the TP 570 is %s\n",
4399 str_supported(dock2_needed
));
4401 vdbg_printk(TPACPI_DBG_INIT
,
4402 "dock subdriver part 2 not required\n");
4406 return (dock2_needed
)? 0 : 1;
4409 static void dock_notify(struct ibm_struct
*ibm
, u32 event
)
4411 int docked
= dock_docked();
4412 int pci
= ibm
->acpi
->hid
&& ibm
->acpi
->device
&&
4413 acpi_match_device_ids(ibm
->acpi
->device
, ibm_pci_device_ids
);
4416 if (event
== 1 && !pci
) /* 570 */
4417 data
= 1; /* button */
4418 else if (event
== 1 && pci
) /* 570 */
4419 data
= 3; /* dock */
4420 else if (event
== 3 && docked
)
4421 data
= 1; /* button */
4422 else if (event
== 3 && !docked
)
4423 data
= 2; /* undock */
4424 else if (event
== 0 && docked
)
4425 data
= 3; /* dock */
4427 printk(TPACPI_ERR
"unknown dock event %d, status %d\n",
4428 event
, _sta(dock_handle
));
4429 data
= 0; /* unknown */
4431 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, data
);
4432 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4433 dev_name(&ibm
->acpi
->device
->dev
),
4437 static int dock_read(char *p
)
4440 int docked
= dock_docked();
4443 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4445 len
+= sprintf(p
+ len
, "status:\t\tundocked\n");
4447 len
+= sprintf(p
+ len
, "status:\t\tdocked\n");
4448 len
+= sprintf(p
+ len
, "commands:\tdock, undock\n");
4454 static int dock_write(char *buf
)
4461 while ((cmd
= next_cmd(&buf
))) {
4462 if (strlencmp(cmd
, "undock") == 0) {
4463 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 0) ||
4464 !acpi_evalf(dock_handle
, NULL
, "_EJ0", "vd", 1))
4466 } else if (strlencmp(cmd
, "dock") == 0) {
4467 if (!acpi_evalf(dock_handle
, NULL
, "_DCK", "vd", 1))
4476 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4478 /*************************************************************************
4482 #ifdef CONFIG_THINKPAD_ACPI_BAY
4484 TPACPI_HANDLE(bay
, root
, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4485 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4486 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4487 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4488 ); /* A21e, R30, R31 */
4489 TPACPI_HANDLE(bay_ej
, bay
, "_EJ3", /* 600e/x, A2xm/p, A3x */
4490 "_EJ0", /* all others */
4491 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4492 TPACPI_HANDLE(bay2
, root
, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4493 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4495 TPACPI_HANDLE(bay2_ej
, bay2
, "_EJ3", /* 600e/x, 770e, A3x */
4499 static int __init
bay_init(struct ibm_init_struct
*iibm
)
4501 vdbg_printk(TPACPI_DBG_INIT
, "initializing bay subdriver\n");
4503 TPACPI_ACPIHANDLE_INIT(bay
);
4505 TPACPI_ACPIHANDLE_INIT(bay_ej
);
4506 TPACPI_ACPIHANDLE_INIT(bay2
);
4508 TPACPI_ACPIHANDLE_INIT(bay2_ej
);
4510 tp_features
.bay_status
= bay_handle
&&
4511 acpi_evalf(bay_handle
, NULL
, "_STA", "qv");
4512 tp_features
.bay_status2
= bay2_handle
&&
4513 acpi_evalf(bay2_handle
, NULL
, "_STA", "qv");
4515 tp_features
.bay_eject
= bay_handle
&& bay_ej_handle
&&
4516 (strlencmp(bay_ej_path
, "_EJ0") == 0 || experimental
);
4517 tp_features
.bay_eject2
= bay2_handle
&& bay2_ej_handle
&&
4518 (strlencmp(bay2_ej_path
, "_EJ0") == 0 || experimental
);
4520 vdbg_printk(TPACPI_DBG_INIT
,
4521 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4522 str_supported(tp_features
.bay_status
),
4523 str_supported(tp_features
.bay_eject
),
4524 str_supported(tp_features
.bay_status2
),
4525 str_supported(tp_features
.bay_eject2
));
4527 return (tp_features
.bay_status
|| tp_features
.bay_eject
||
4528 tp_features
.bay_status2
|| tp_features
.bay_eject2
)? 0 : 1;
4531 static void bay_notify(struct ibm_struct
*ibm
, u32 event
)
4533 acpi_bus_generate_proc_event(ibm
->acpi
->device
, event
, 0);
4534 acpi_bus_generate_netlink_event(ibm
->acpi
->device
->pnp
.device_class
,
4535 dev_name(&ibm
->acpi
->device
->dev
),
4539 #define bay_occupied(b) (_sta(b##_handle) & 1)
4541 static int bay_read(char *p
)
4544 int occupied
= bay_occupied(bay
);
4545 int occupied2
= bay_occupied(bay2
);
4548 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
4549 tp_features
.bay_status
?
4550 (occupied
? "occupied" : "unoccupied") :
4552 if (tp_features
.bay_status2
)
4553 len
+= sprintf(p
+ len
, "status2:\t%s\n", occupied2
?
4554 "occupied" : "unoccupied");
4556 eject
= tp_features
.bay_eject
&& occupied
;
4557 eject2
= tp_features
.bay_eject2
&& occupied2
;
4559 if (eject
&& eject2
)
4560 len
+= sprintf(p
+ len
, "commands:\teject, eject2\n");
4562 len
+= sprintf(p
+ len
, "commands:\teject\n");
4564 len
+= sprintf(p
+ len
, "commands:\teject2\n");
4569 static int bay_write(char *buf
)
4573 if (!tp_features
.bay_eject
&& !tp_features
.bay_eject2
)
4576 while ((cmd
= next_cmd(&buf
))) {
4577 if (tp_features
.bay_eject
&& strlencmp(cmd
, "eject") == 0) {
4578 if (!acpi_evalf(bay_ej_handle
, NULL
, NULL
, "vd", 1))
4580 } else if (tp_features
.bay_eject2
&&
4581 strlencmp(cmd
, "eject2") == 0) {
4582 if (!acpi_evalf(bay2_ej_handle
, NULL
, NULL
, "vd", 1))
4591 static struct tp_acpi_drv_struct ibm_bay_acpidriver
= {
4592 .notify
= bay_notify
,
4593 .handle
= &bay_handle
,
4594 .type
= ACPI_SYSTEM_NOTIFY
,
4597 static struct ibm_struct bay_driver_data
= {
4601 .acpi
= &ibm_bay_acpidriver
,
4604 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4606 /*************************************************************************
4610 /* sysfs cmos_command -------------------------------------------------- */
4611 static ssize_t
cmos_command_store(struct device
*dev
,
4612 struct device_attribute
*attr
,
4613 const char *buf
, size_t count
)
4615 unsigned long cmos_cmd
;
4618 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4621 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4622 return (res
)? res
: count
;
4625 static struct device_attribute dev_attr_cmos_command
=
4626 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4628 /* --------------------------------------------------------------------- */
4630 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4634 vdbg_printk(TPACPI_DBG_INIT
,
4635 "initializing cmos commands subdriver\n");
4637 TPACPI_ACPIHANDLE_INIT(cmos
);
4639 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4640 str_supported(cmos_handle
!= NULL
));
4642 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4646 return (cmos_handle
)? 0 : 1;
4649 static void cmos_exit(void)
4651 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4654 static int cmos_read(char *p
)
4658 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4659 R30, R31, T20-22, X20-21 */
4661 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4663 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4664 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4670 static int cmos_write(char *buf
)
4675 while ((cmd
= next_cmd(&buf
))) {
4676 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4677 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4682 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4690 static struct ibm_struct cmos_driver_data
= {
4693 .write
= cmos_write
,
4697 /*************************************************************************
4701 enum led_access_mode
{
4702 TPACPI_LED_NONE
= 0,
4703 TPACPI_LED_570
, /* 570 */
4704 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4705 TPACPI_LED_NEW
, /* all others */
4708 enum { /* For TPACPI_LED_OLD */
4709 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4710 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4711 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4714 static enum led_access_mode led_supported
;
4716 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4717 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4718 /* T20-22, X20-21 */
4719 "LED", /* all others */
4722 #define TPACPI_LED_NUMLEDS 8
4723 static struct tpacpi_led_classdev
*tpacpi_leds
;
4724 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
4725 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
4726 /* there's a limit of 19 chars + NULL before 2.6.26 */
4728 "tpacpi:orange:batt",
4729 "tpacpi:green:batt",
4730 "tpacpi::dock_active",
4731 "tpacpi::bay_active",
4732 "tpacpi::dock_batt",
4733 "tpacpi::unknown_led",
4736 #define TPACPI_SAFE_LEDS 0x0081U
4738 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
4740 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4743 return (TPACPI_SAFE_LEDS
& (1 << led
)) == 0;
4747 static int led_get_status(const unsigned int led
)
4750 enum led_status_t led_s
;
4752 switch (led_supported
) {
4753 case TPACPI_LED_570
:
4754 if (!acpi_evalf(ec_handle
,
4755 &status
, "GLED", "dd", 1 << led
))
4757 led_s
= (status
== 0)?
4762 tpacpi_led_state_cache
[led
] = led_s
;
4771 static int led_set_status(const unsigned int led
,
4772 const enum led_status_t ledstatus
)
4774 /* off, on, blink. Index is led_status_t */
4775 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
4776 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
4780 switch (led_supported
) {
4781 case TPACPI_LED_570
:
4783 if (unlikely(led
> 7))
4785 if (unlikely(tpacpi_is_led_restricted(led
)))
4787 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4788 (1 << led
), led_sled_arg1
[ledstatus
]))
4791 case TPACPI_LED_OLD
:
4792 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4793 if (unlikely(led
> 7))
4795 if (unlikely(tpacpi_is_led_restricted(led
)))
4797 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
4799 rc
= ec_write(TPACPI_LED_EC_HLBL
,
4800 (ledstatus
== TPACPI_LED_BLINK
) << led
);
4802 rc
= ec_write(TPACPI_LED_EC_HLCL
,
4803 (ledstatus
!= TPACPI_LED_OFF
) << led
);
4805 case TPACPI_LED_NEW
:
4807 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
4809 if (unlikely(tpacpi_is_led_restricted(led
)))
4811 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
4812 led
, led_led_arg1
[ledstatus
]))
4820 tpacpi_led_state_cache
[led
] = ledstatus
;
4825 static void led_set_status_worker(struct work_struct
*work
)
4827 struct tpacpi_led_classdev
*data
=
4828 container_of(work
, struct tpacpi_led_classdev
, work
);
4830 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4831 led_set_status(data
->led
, data
->new_state
);
4834 static void led_sysfs_set(struct led_classdev
*led_cdev
,
4835 enum led_brightness brightness
)
4837 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4838 struct tpacpi_led_classdev
, led_classdev
);
4840 if (brightness
== LED_OFF
)
4841 data
->new_state
= TPACPI_LED_OFF
;
4842 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
4843 data
->new_state
= TPACPI_LED_ON
;
4845 data
->new_state
= TPACPI_LED_BLINK
;
4847 queue_work(tpacpi_wq
, &data
->work
);
4850 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
4851 unsigned long *delay_on
, unsigned long *delay_off
)
4853 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4854 struct tpacpi_led_classdev
, led_classdev
);
4856 /* Can we choose the flash rate? */
4857 if (*delay_on
== 0 && *delay_off
== 0) {
4858 /* yes. set them to the hardware blink rate (1 Hz) */
4859 *delay_on
= 500; /* ms */
4860 *delay_off
= 500; /* ms */
4861 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
4864 data
->new_state
= TPACPI_LED_BLINK
;
4865 queue_work(tpacpi_wq
, &data
->work
);
4870 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
4874 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
4875 struct tpacpi_led_classdev
, led_classdev
);
4877 rc
= led_get_status(data
->led
);
4879 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
4880 rc
= LED_OFF
; /* no error handling in led class :( */
4887 static void led_exit(void)
4891 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4892 if (tpacpi_leds
[i
].led_classdev
.name
)
4893 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
4899 static int __init
tpacpi_init_led(unsigned int led
)
4903 tpacpi_leds
[led
].led
= led
;
4905 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
4906 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
4907 if (led_supported
== TPACPI_LED_570
)
4908 tpacpi_leds
[led
].led_classdev
.brightness_get
=
4911 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
4913 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
4915 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4916 &tpacpi_leds
[led
].led_classdev
);
4918 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
4923 static int __init
led_init(struct ibm_init_struct
*iibm
)
4928 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
4930 TPACPI_ACPIHANDLE_INIT(led
);
4933 /* led not supported on R30, R31 */
4934 led_supported
= TPACPI_LED_NONE
;
4935 else if (strlencmp(led_path
, "SLED") == 0)
4937 led_supported
= TPACPI_LED_570
;
4938 else if (strlencmp(led_path
, "SYSL") == 0)
4939 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4940 led_supported
= TPACPI_LED_OLD
;
4943 led_supported
= TPACPI_LED_NEW
;
4945 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
4946 str_supported(led_supported
), led_supported
);
4948 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
4951 printk(TPACPI_ERR
"Out of memory for LED data\n");
4955 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
4956 if (!tpacpi_is_led_restricted(i
)) {
4957 rc
= tpacpi_init_led(i
);
4965 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4966 if (led_supported
!= TPACPI_LED_NONE
)
4967 printk(TPACPI_NOTICE
4968 "warning: userspace override of important "
4969 "firmware LEDs is enabled\n");
4971 return (led_supported
!= TPACPI_LED_NONE
)? 0 : 1;
4974 #define str_led_status(s) \
4975 ((s) == TPACPI_LED_OFF ? "off" : \
4976 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4978 static int led_read(char *p
)
4982 if (!led_supported
) {
4983 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4986 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4988 if (led_supported
== TPACPI_LED_570
) {
4991 for (i
= 0; i
< 8; i
++) {
4992 status
= led_get_status(i
);
4995 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
4996 i
, str_led_status(status
));
5000 len
+= sprintf(p
+ len
, "commands:\t"
5001 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5006 static int led_write(char *buf
)
5010 enum led_status_t s
;
5015 while ((cmd
= next_cmd(&buf
))) {
5016 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 7)
5019 if (strstr(cmd
, "off")) {
5021 } else if (strstr(cmd
, "on")) {
5023 } else if (strstr(cmd
, "blink")) {
5024 s
= TPACPI_LED_BLINK
;
5029 rc
= led_set_status(led
, s
);
5037 static struct ibm_struct led_driver_data
= {
5044 /*************************************************************************
5048 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5050 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5052 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5054 TPACPI_ACPIHANDLE_INIT(beep
);
5056 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5057 str_supported(beep_handle
!= NULL
));
5059 return (beep_handle
)? 0 : 1;
5062 static int beep_read(char *p
)
5067 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5069 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5070 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5076 static int beep_write(char *buf
)
5084 while ((cmd
= next_cmd(&buf
))) {
5085 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5086 beep_cmd
>= 0 && beep_cmd
<= 17) {
5090 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd", beep_cmd
, 0))
5097 static struct ibm_struct beep_driver_data
= {
5100 .write
= beep_write
,
5103 /*************************************************************************
5107 enum thermal_access_mode
{
5108 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5109 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5110 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5111 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5112 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5115 enum { /* TPACPI_THERMAL_TPEC_* */
5116 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5117 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5118 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5121 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5122 struct ibm_thermal_sensors_struct
{
5123 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5126 static enum thermal_access_mode thermal_read_mode
;
5128 /* idx is zero-based */
5129 static int thermal_get_sensor(int idx
, s32
*value
)
5135 t
= TP_EC_THERMAL_TMP0
;
5137 switch (thermal_read_mode
) {
5138 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5139 case TPACPI_THERMAL_TPEC_16
:
5140 if (idx
>= 8 && idx
<= 15) {
5141 t
= TP_EC_THERMAL_TMP8
;
5146 case TPACPI_THERMAL_TPEC_8
:
5148 if (!acpi_ec_read(t
+ idx
, &tmp
))
5150 *value
= tmp
* 1000;
5155 case TPACPI_THERMAL_ACPI_UPDT
:
5157 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5158 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5160 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5162 *value
= (t
- 2732) * 100;
5167 case TPACPI_THERMAL_ACPI_TMP07
:
5169 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5170 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5172 if (t
> 127 || t
< -127)
5173 t
= TP_EC_THERMAL_TMP_NA
;
5179 case TPACPI_THERMAL_NONE
:
5187 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5198 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5201 for (i
= 0 ; i
< n
; i
++) {
5202 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5210 /* sysfs temp##_input -------------------------------------------------- */
5212 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5213 struct device_attribute
*attr
,
5216 struct sensor_device_attribute
*sensor_attr
=
5217 to_sensor_dev_attr(attr
);
5218 int idx
= sensor_attr
->index
;
5222 res
= thermal_get_sensor(idx
, &value
);
5225 if (value
== TP_EC_THERMAL_TMP_NA
* 1000)
5228 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5231 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5232 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5233 thermal_temp_input_show, NULL, _idxB)
5235 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5236 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5237 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5238 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5239 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5240 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5241 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5242 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5243 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5244 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5245 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5246 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5247 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5248 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5249 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5250 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5251 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5254 #define THERMAL_ATTRS(X) \
5255 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5257 static struct attribute
*thermal_temp_input_attr
[] = {
5277 static const struct attribute_group thermal_temp_input16_group
= {
5278 .attrs
= thermal_temp_input_attr
5281 static const struct attribute_group thermal_temp_input8_group
= {
5282 .attrs
= &thermal_temp_input_attr
[8]
5285 #undef THERMAL_SENSOR_ATTR_TEMP
5286 #undef THERMAL_ATTRS
5288 /* --------------------------------------------------------------------- */
5290 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5297 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5299 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5301 if (thinkpad_id
.ec_model
) {
5303 * Direct EC access mode: sensors at registers
5304 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5305 * non-implemented, thermal sensors return 0x80 when
5310 for (i
= 0; i
< 8; i
++) {
5311 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5317 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5325 /* This is sheer paranoia, but we handle it anyway */
5328 "ThinkPad ACPI EC access misbehaving, "
5329 "falling back to ACPI TMPx access "
5331 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5334 "ThinkPad ACPI EC access misbehaving, "
5335 "disabling thermal sensors access\n");
5336 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5341 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5343 } else if (acpi_tmp7
) {
5344 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5345 /* 600e/x, 770e, 770x */
5346 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5348 /* Standard ACPI TMPx access, max 8 sensors */
5349 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5352 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5353 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5356 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5357 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5360 switch (thermal_read_mode
) {
5361 case TPACPI_THERMAL_TPEC_16
:
5362 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5363 &thermal_temp_input16_group
);
5367 case TPACPI_THERMAL_TPEC_8
:
5368 case TPACPI_THERMAL_ACPI_TMP07
:
5369 case TPACPI_THERMAL_ACPI_UPDT
:
5370 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5371 &thermal_temp_input8_group
);
5375 case TPACPI_THERMAL_NONE
:
5383 static void thermal_exit(void)
5385 switch (thermal_read_mode
) {
5386 case TPACPI_THERMAL_TPEC_16
:
5387 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5388 &thermal_temp_input16_group
);
5390 case TPACPI_THERMAL_TPEC_8
:
5391 case TPACPI_THERMAL_ACPI_TMP07
:
5392 case TPACPI_THERMAL_ACPI_UPDT
:
5393 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5394 &thermal_temp_input16_group
);
5396 case TPACPI_THERMAL_NONE
:
5402 static int thermal_read(char *p
)
5406 struct ibm_thermal_sensors_struct t
;
5408 n
= thermal_get_sensors(&t
);
5409 if (unlikely(n
< 0))
5412 len
+= sprintf(p
+ len
, "temperatures:\t");
5415 for (i
= 0; i
< (n
- 1); i
++)
5416 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5417 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5419 len
+= sprintf(p
+ len
, "not supported\n");
5424 static struct ibm_struct thermal_driver_data
= {
5426 .read
= thermal_read
,
5427 .exit
= thermal_exit
,
5430 /*************************************************************************
5434 static u8 ecdump_regs
[256];
5436 static int ecdump_read(char *p
)
5442 len
+= sprintf(p
+ len
, "EC "
5443 " +00 +01 +02 +03 +04 +05 +06 +07"
5444 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5445 for (i
= 0; i
< 256; i
+= 16) {
5446 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5447 for (j
= 0; j
< 16; j
++) {
5448 if (!acpi_ec_read(i
+ j
, &v
))
5450 if (v
!= ecdump_regs
[i
+ j
])
5451 len
+= sprintf(p
+ len
, " *%02x", v
);
5453 len
+= sprintf(p
+ len
, " %02x", v
);
5454 ecdump_regs
[i
+ j
] = v
;
5456 len
+= sprintf(p
+ len
, "\n");
5461 /* These are way too dangerous to advertise openly... */
5463 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5464 " (<offset> is 00-ff, <value> is 00-ff)\n");
5465 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5466 " (<offset> is 00-ff, <value> is 0-255)\n");
5471 static int ecdump_write(char *buf
)
5476 while ((cmd
= next_cmd(&buf
))) {
5477 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5479 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5483 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5484 if (!acpi_ec_write(i
, v
))
5493 static struct ibm_struct ecdump_driver_data
= {
5495 .read
= ecdump_read
,
5496 .write
= ecdump_write
,
5497 .flags
.experimental
= 1,
5500 /*************************************************************************
5501 * Backlight/brightness subdriver
5504 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5507 TP_EC_BACKLIGHT
= 0x31,
5509 /* TP_EC_BACKLIGHT bitmasks */
5510 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5511 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5512 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5515 static struct backlight_device
*ibm_backlight_device
;
5516 static int brightness_mode
;
5517 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5519 static struct mutex brightness_mutex
;
5522 * ThinkPads can read brightness from two places: EC 0x31, or
5523 * CMOS NVRAM byte 0x5E, bits 0-3.
5525 * EC 0x31 has the following layout
5526 * Bit 7: unknown function
5527 * Bit 6: unknown function
5528 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5529 * Bit 4: must be set to zero to avoid problems
5530 * Bit 3-0: backlight brightness level
5532 * brightness_get_raw returns status data in the EC 0x31 layout
5534 static int brightness_get_raw(int *status
)
5536 u8 lec
= 0, lcmos
= 0, level
= 0;
5538 if (brightness_mode
& 1) {
5539 if (!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
))
5541 level
= lec
& TP_EC_BACKLIGHT_LVLMSK
;
5543 if (brightness_mode
& 2) {
5544 lcmos
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5545 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5546 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5547 lcmos
&= (tp_features
.bright_16levels
)? 0x0f : 0x07;
5551 if (brightness_mode
== 3) {
5552 *status
= lec
; /* Prefer EC, CMOS is just a backing store */
5553 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5555 tp_warned
.bright_cmos_ec_unsync
= 0;
5557 if (!tp_warned
.bright_cmos_ec_unsync
) {
5559 "CMOS NVRAM (%u) and EC (%u) do not "
5560 "agree on display brightness level\n",
5561 (unsigned int) lcmos
,
5562 (unsigned int) lec
);
5563 tp_warned
.bright_cmos_ec_unsync
= 1;
5574 /* May return EINTR which can always be mapped to ERESTARTSYS */
5575 static int brightness_set(int value
)
5577 int cmos_cmd
, inc
, i
, res
;
5581 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
5585 res
= mutex_lock_killable(&brightness_mutex
);
5589 res
= brightness_get_raw(¤t_value
);
5593 command_bits
= current_value
& TP_EC_BACKLIGHT_CMDMSK
;
5594 current_value
&= TP_EC_BACKLIGHT_LVLMSK
;
5596 cmos_cmd
= value
> current_value
?
5597 TP_CMOS_BRIGHTNESS_UP
:
5598 TP_CMOS_BRIGHTNESS_DOWN
;
5599 inc
= (value
> current_value
)? 1 : -1;
5602 for (i
= current_value
; i
!= value
; i
+= inc
) {
5603 if ((brightness_mode
& 2) &&
5604 issue_thinkpad_cmos_command(cmos_cmd
)) {
5608 if ((brightness_mode
& 1) &&
5609 !acpi_ec_write(TP_EC_BACKLIGHT
,
5610 (i
+ inc
) | command_bits
)) {
5617 mutex_unlock(&brightness_mutex
);
5621 /* sysfs backlight class ----------------------------------------------- */
5623 static int brightness_update_status(struct backlight_device
*bd
)
5625 /* it is the backlight class's job (caller) to handle
5626 * EINTR and other errors properly */
5627 return brightness_set(
5628 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
5629 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
5630 bd
->props
.brightness
: 0);
5633 static int brightness_get(struct backlight_device
*bd
)
5637 res
= brightness_get_raw(&status
);
5639 return 0; /* FIXME: teach backlight about error handling */
5641 return status
& TP_EC_BACKLIGHT_LVLMSK
;
5644 static struct backlight_ops ibm_backlight_data
= {
5645 .get_brightness
= brightness_get
,
5646 .update_status
= brightness_update_status
,
5649 /* --------------------------------------------------------------------- */
5651 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
5655 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
5657 mutex_init(&brightness_mutex
);
5660 * We always attempt to detect acpi support, so as to switch
5661 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5662 * going to publish a backlight interface
5664 b
= tpacpi_check_std_acpi_brightness_support();
5667 if (acpi_video_backlight_support()) {
5668 if (brightness_enable
> 1) {
5669 printk(TPACPI_NOTICE
5670 "Standard ACPI backlight interface "
5671 "available, not loading native one.\n");
5673 } else if (brightness_enable
== 1) {
5674 printk(TPACPI_NOTICE
5675 "Backlight control force enabled, even if standard "
5676 "ACPI backlight interface is available\n");
5679 if (brightness_enable
> 1) {
5680 printk(TPACPI_NOTICE
5681 "Standard ACPI backlight interface not "
5682 "available, thinkpad_acpi native "
5683 "brightness control enabled\n");
5688 if (!brightness_enable
) {
5689 dbg_printk(TPACPI_DBG_INIT
,
5690 "brightness support disabled by "
5691 "module parameter\n");
5697 "Unsupported brightness interface, "
5698 "please contact %s\n", TPACPI_MAIL
);
5702 tp_features
.bright_16levels
= 1;
5704 if (!brightness_mode
) {
5705 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
)
5706 brightness_mode
= 2;
5708 brightness_mode
= 3;
5710 dbg_printk(TPACPI_DBG_INIT
, "selected brightness_mode=%d\n",
5714 if (brightness_mode
> 3)
5717 if (brightness_get_raw(&b
) < 0)
5720 if (tp_features
.bright_16levels
)
5722 "detected a 16-level brightness capable ThinkPad\n");
5724 ibm_backlight_device
= backlight_device_register(
5725 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
5726 &ibm_backlight_data
);
5727 if (IS_ERR(ibm_backlight_device
)) {
5728 printk(TPACPI_ERR
"Could not register backlight device\n");
5729 return PTR_ERR(ibm_backlight_device
);
5731 vdbg_printk(TPACPI_DBG_INIT
, "brightness is supported\n");
5733 ibm_backlight_device
->props
.max_brightness
=
5734 (tp_features
.bright_16levels
)? 15 : 7;
5735 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
5736 backlight_update_status(ibm_backlight_device
);
5741 static void brightness_exit(void)
5743 if (ibm_backlight_device
) {
5744 vdbg_printk(TPACPI_DBG_EXIT
,
5745 "calling backlight_device_unregister()\n");
5746 backlight_device_unregister(ibm_backlight_device
);
5750 static int brightness_read(char *p
)
5755 level
= brightness_get(NULL
);
5757 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5759 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
5760 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
5761 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5762 " (<level> is 0-%d)\n",
5763 (tp_features
.bright_16levels
) ? 15 : 7);
5769 static int brightness_write(char *buf
)
5774 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
5776 level
= brightness_get(NULL
);
5780 while ((cmd
= next_cmd(&buf
))) {
5781 if (strlencmp(cmd
, "up") == 0) {
5782 if (level
< max_level
)
5784 } else if (strlencmp(cmd
, "down") == 0) {
5787 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
5788 level
>= 0 && level
<= max_level
) {
5795 * Now we know what the final level should be, so we try to set it.
5796 * Doing it this way makes the syscall restartable in case of EINTR
5798 rc
= brightness_set(level
);
5799 return (rc
== -EINTR
)? ERESTARTSYS
: rc
;
5802 static struct ibm_struct brightness_driver_data
= {
5803 .name
= "brightness",
5804 .read
= brightness_read
,
5805 .write
= brightness_write
,
5806 .exit
= brightness_exit
,
5809 /*************************************************************************
5813 static int volume_offset
= 0x30;
5815 static int volume_read(char *p
)
5820 if (!acpi_ec_read(volume_offset
, &level
)) {
5821 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
5823 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
& 0xf);
5824 len
+= sprintf(p
+ len
, "mute:\t\t%s\n", onoff(level
, 6));
5825 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
5826 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
5827 " (<level> is 0-15)\n");
5833 static int volume_write(char *buf
)
5835 int cmos_cmd
, inc
, i
;
5837 int new_level
, new_mute
;
5840 while ((cmd
= next_cmd(&buf
))) {
5841 if (!acpi_ec_read(volume_offset
, &level
))
5843 new_mute
= mute
= level
& 0x40;
5844 new_level
= level
= level
& 0xf;
5846 if (strlencmp(cmd
, "up") == 0) {
5850 new_level
= level
== 15 ? 15 : level
+ 1;
5851 } else if (strlencmp(cmd
, "down") == 0) {
5855 new_level
= level
== 0 ? 0 : level
- 1;
5856 } else if (sscanf(cmd
, "level %d", &new_level
) == 1 &&
5857 new_level
>= 0 && new_level
<= 15) {
5859 } else if (strlencmp(cmd
, "mute") == 0) {
5864 if (new_level
!= level
) {
5865 /* mute doesn't change */
5867 cmos_cmd
= (new_level
> level
) ?
5868 TP_CMOS_VOLUME_UP
: TP_CMOS_VOLUME_DOWN
;
5869 inc
= new_level
> level
? 1 : -1;
5871 if (mute
&& (issue_thinkpad_cmos_command(cmos_cmd
) ||
5872 !acpi_ec_write(volume_offset
, level
)))
5875 for (i
= level
; i
!= new_level
; i
+= inc
)
5876 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5877 !acpi_ec_write(volume_offset
, i
+ inc
))
5881 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE
) ||
5882 !acpi_ec_write(volume_offset
, new_level
+ mute
))) {
5887 if (new_mute
!= mute
) {
5888 /* level doesn't change */
5890 cmos_cmd
= (new_mute
) ?
5891 TP_CMOS_VOLUME_MUTE
: TP_CMOS_VOLUME_UP
;
5893 if (issue_thinkpad_cmos_command(cmos_cmd
) ||
5894 !acpi_ec_write(volume_offset
, level
+ new_mute
))
5902 static struct ibm_struct volume_driver_data
= {
5904 .read
= volume_read
,
5905 .write
= volume_write
,
5908 /*************************************************************************
5915 * TPACPI_FAN_RD_ACPI_GFAN:
5916 * ACPI GFAN method: returns fan level
5918 * see TPACPI_FAN_WR_ACPI_SFAN
5919 * EC 0x2f (HFSP) not available if GFAN exists
5921 * TPACPI_FAN_WR_ACPI_SFAN:
5922 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5924 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5927 * TPACPI_FAN_WR_TPEC:
5928 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5929 * Supported on almost all ThinkPads
5931 * Fan speed changes of any sort (including those caused by the
5932 * disengaged mode) are usually done slowly by the firmware as the
5933 * maximum ammount of fan duty cycle change per second seems to be
5936 * Reading is not available if GFAN exists.
5937 * Writing is not available if SFAN exists.
5940 * 7 automatic mode engaged;
5941 * (default operation mode of the ThinkPad)
5942 * fan level is ignored in this mode.
5943 * 6 full speed mode (takes precedence over bit 7);
5944 * not available on all thinkpads. May disable
5945 * the tachometer while the fan controller ramps up
5946 * the speed (which can take up to a few *minutes*).
5947 * Speeds up fan to 100% duty-cycle, which is far above
5948 * the standard RPM levels. It is not impossible that
5949 * it could cause hardware damage.
5950 * 5-3 unused in some models. Extra bits for fan level
5951 * in others, but still useless as all values above
5952 * 7 map to the same speed as level 7 in these models.
5953 * 2-0 fan level (0..7 usually)
5955 * 0x07 = max (set when temperatures critical)
5956 * Some ThinkPads may have other levels, see
5957 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5959 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5960 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5961 * does so, its initial value is meaningless (0x07).
5963 * For firmware bugs, refer to:
5964 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5968 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5969 * Main fan tachometer reading (in RPM)
5971 * This register is present on all ThinkPads with a new-style EC, and
5972 * it is known not to be present on the A21m/e, and T22, as there is
5973 * something else in offset 0x84 according to the ACPI DSDT. Other
5974 * ThinkPads from this same time period (and earlier) probably lack the
5975 * tachometer as well.
5977 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5978 * was never fixed by IBM to report the EC firmware version string
5979 * probably support the tachometer (like the early X models), so
5980 * detecting it is quite hard. We need more data to know for sure.
5982 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5985 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5988 * For firmware bugs, refer to:
5989 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5991 * TPACPI_FAN_WR_ACPI_FANS:
5992 * ThinkPad X31, X40, X41. Not available in the X60.
5994 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5995 * high speed. ACPI DSDT seems to map these three speeds to levels
5996 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5997 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5999 * The speeds are stored on handles
6000 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6002 * There are three default speed sets, acessible as handles:
6003 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6005 * ACPI DSDT switches which set is in use depending on various
6008 * TPACPI_FAN_WR_TPEC is also available and should be used to
6009 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6010 * but the ACPI tables just mention level 7.
6013 enum { /* Fan control constants */
6014 fan_status_offset
= 0x2f, /* EC register 0x2f */
6015 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6016 * 0x84 must be read before 0x85 */
6018 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
6019 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
6021 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
6024 enum fan_status_access_mode
{
6025 TPACPI_FAN_NONE
= 0, /* No fan status or control */
6026 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
6027 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6030 enum fan_control_access_mode
{
6031 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
6032 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
6033 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
6034 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
6037 enum fan_control_commands
{
6038 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
6039 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
6040 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
6041 * and also watchdog cmd */
6044 static int fan_control_allowed
;
6046 static enum fan_status_access_mode fan_status_access_mode
;
6047 static enum fan_control_access_mode fan_control_access_mode
;
6048 static enum fan_control_commands fan_control_commands
;
6050 static u8 fan_control_initial_status
;
6051 static u8 fan_control_desired_level
;
6052 static u8 fan_control_resume_level
;
6053 static int fan_watchdog_maxinterval
;
6055 static struct mutex fan_mutex
;
6057 static void fan_watchdog_fire(struct work_struct
*ignored
);
6058 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
6060 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
6061 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
6062 "\\FSPD", /* 600e/x, 770e, 770x */
6064 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
6065 "JFNS", /* 770x-JL */
6069 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6070 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6071 * be in auto mode (0x80).
6073 * This is corrected by any write to HFSP either by the driver, or
6076 * We assume 0x07 really means auto mode while this quirk is active,
6077 * as this is far more likely than the ThinkPad being in level 7,
6078 * which is only used by the firmware during thermal emergencies.
6081 static void fan_quirk1_detect(void)
6083 /* In some ThinkPads, neither the EC nor the ACPI
6084 * DSDT initialize the HFSP register, and it ends up
6085 * being initially set to 0x07 when it *could* be
6086 * either 0x07 or 0x80.
6088 * Enable for TP-1Y (T43), TP-78 (R51e),
6089 * TP-76 (R52), TP-70 (T43, R52), which are known
6091 if (fan_control_initial_status
== 0x07) {
6092 switch (thinkpad_id
.ec_model
) {
6093 case 0x5931: /* TP-1Y */
6094 case 0x3837: /* TP-78 */
6095 case 0x3637: /* TP-76 */
6096 case 0x3037: /* TP-70 */
6097 printk(TPACPI_NOTICE
6098 "fan_init: initial fan status is unknown, "
6099 "assuming it is in auto mode\n");
6100 tp_features
.fan_ctrl_status_undef
= 1;
6106 static void fan_quirk1_handle(u8
*fan_status
)
6108 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
6109 if (*fan_status
!= fan_control_initial_status
) {
6110 /* something changed the HFSP regisnter since
6111 * driver init time, so it is not undefined
6113 tp_features
.fan_ctrl_status_undef
= 0;
6115 /* Return most likely status. In fact, it
6116 * might be the only possible status */
6117 *fan_status
= TP_EC_FAN_AUTO
;
6123 * Call with fan_mutex held
6125 static void fan_update_desired_level(u8 status
)
6128 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6130 fan_control_desired_level
= 7;
6132 fan_control_desired_level
= status
;
6136 static int fan_get_status(u8
*status
)
6141 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6143 switch (fan_status_access_mode
) {
6144 case TPACPI_FAN_RD_ACPI_GFAN
:
6145 /* 570, 600e/x, 770e, 770x */
6147 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
6155 case TPACPI_FAN_RD_TPEC
:
6156 /* all except 570, 600e/x, 770e, 770x */
6157 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
6160 if (likely(status
)) {
6162 fan_quirk1_handle(status
);
6174 static int fan_get_status_safe(u8
*status
)
6179 if (mutex_lock_killable(&fan_mutex
))
6180 return -ERESTARTSYS
;
6181 rc
= fan_get_status(&s
);
6183 fan_update_desired_level(s
);
6184 mutex_unlock(&fan_mutex
);
6192 static int fan_get_speed(unsigned int *speed
)
6196 switch (fan_status_access_mode
) {
6197 case TPACPI_FAN_RD_TPEC
:
6198 /* all except 570, 600e/x, 770e, 770x */
6199 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
6200 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
6204 *speed
= (hi
<< 8) | lo
;
6215 static int fan_set_level(int level
)
6217 if (!fan_control_allowed
)
6220 switch (fan_control_access_mode
) {
6221 case TPACPI_FAN_WR_ACPI_SFAN
:
6222 if (level
>= 0 && level
<= 7) {
6223 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
6229 case TPACPI_FAN_WR_ACPI_FANS
:
6230 case TPACPI_FAN_WR_TPEC
:
6231 if (!(level
& TP_EC_FAN_AUTO
) &&
6232 !(level
& TP_EC_FAN_FULLSPEED
) &&
6233 ((level
< 0) || (level
> 7)))
6236 /* safety net should the EC not support AUTO
6237 * or FULLSPEED mode bits and just ignore them */
6238 if (level
& TP_EC_FAN_FULLSPEED
)
6239 level
|= 7; /* safety min speed 7 */
6240 else if (level
& TP_EC_FAN_AUTO
)
6241 level
|= 4; /* safety min speed 4 */
6243 if (!acpi_ec_write(fan_status_offset
, level
))
6246 tp_features
.fan_ctrl_status_undef
= 0;
6255 static int fan_set_level_safe(int level
)
6259 if (!fan_control_allowed
)
6262 if (mutex_lock_killable(&fan_mutex
))
6263 return -ERESTARTSYS
;
6265 if (level
== TPACPI_FAN_LAST_LEVEL
)
6266 level
= fan_control_desired_level
;
6268 rc
= fan_set_level(level
);
6270 fan_update_desired_level(level
);
6272 mutex_unlock(&fan_mutex
);
6276 static int fan_set_enable(void)
6281 if (!fan_control_allowed
)
6284 if (mutex_lock_killable(&fan_mutex
))
6285 return -ERESTARTSYS
;
6287 switch (fan_control_access_mode
) {
6288 case TPACPI_FAN_WR_ACPI_FANS
:
6289 case TPACPI_FAN_WR_TPEC
:
6290 rc
= fan_get_status(&s
);
6294 /* Don't go out of emergency fan mode */
6297 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
6300 if (!acpi_ec_write(fan_status_offset
, s
))
6303 tp_features
.fan_ctrl_status_undef
= 0;
6308 case TPACPI_FAN_WR_ACPI_SFAN
:
6309 rc
= fan_get_status(&s
);
6315 /* Set fan to at least level 4 */
6318 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
6328 mutex_unlock(&fan_mutex
);
6332 static int fan_set_disable(void)
6336 if (!fan_control_allowed
)
6339 if (mutex_lock_killable(&fan_mutex
))
6340 return -ERESTARTSYS
;
6343 switch (fan_control_access_mode
) {
6344 case TPACPI_FAN_WR_ACPI_FANS
:
6345 case TPACPI_FAN_WR_TPEC
:
6346 if (!acpi_ec_write(fan_status_offset
, 0x00))
6349 fan_control_desired_level
= 0;
6350 tp_features
.fan_ctrl_status_undef
= 0;
6354 case TPACPI_FAN_WR_ACPI_SFAN
:
6355 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
6358 fan_control_desired_level
= 0;
6366 mutex_unlock(&fan_mutex
);
6370 static int fan_set_speed(int speed
)
6374 if (!fan_control_allowed
)
6377 if (mutex_lock_killable(&fan_mutex
))
6378 return -ERESTARTSYS
;
6381 switch (fan_control_access_mode
) {
6382 case TPACPI_FAN_WR_ACPI_FANS
:
6383 if (speed
>= 0 && speed
<= 65535) {
6384 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
6385 speed
, speed
, speed
))
6395 mutex_unlock(&fan_mutex
);
6399 static void fan_watchdog_reset(void)
6401 static int fan_watchdog_active
;
6403 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
6406 if (fan_watchdog_active
)
6407 cancel_delayed_work(&fan_watchdog_task
);
6409 if (fan_watchdog_maxinterval
> 0 &&
6410 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
6411 fan_watchdog_active
= 1;
6412 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
6413 msecs_to_jiffies(fan_watchdog_maxinterval
6416 "failed to queue the fan watchdog, "
6417 "watchdog will not trigger\n");
6420 fan_watchdog_active
= 0;
6423 static void fan_watchdog_fire(struct work_struct
*ignored
)
6427 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
6430 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
6431 rc
= fan_set_enable();
6433 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
6434 "will try again later...\n", -rc
);
6435 /* reschedule for later */
6436 fan_watchdog_reset();
6441 * SYSFS fan layout: hwmon compatible (device)
6444 * 0: "disengaged" mode
6446 * 2: native EC "auto" mode (recommended, hardware default)
6448 * pwm*: set speed in manual mode, ignored otherwise.
6449 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6452 * fan*_input: tachometer reading, RPM
6455 * SYSFS fan layout: extensions
6457 * fan_watchdog (driver):
6458 * fan watchdog interval in seconds, 0 disables (default), max 120
6461 /* sysfs fan pwm1_enable ----------------------------------------------- */
6462 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
6463 struct device_attribute
*attr
,
6469 res
= fan_get_status_safe(&status
);
6473 if (status
& TP_EC_FAN_FULLSPEED
) {
6475 } else if (status
& TP_EC_FAN_AUTO
) {
6480 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
6483 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
6484 struct device_attribute
*attr
,
6485 const char *buf
, size_t count
)
6490 if (parse_strtoul(buf
, 2, &t
))
6495 level
= TP_EC_FAN_FULLSPEED
;
6498 level
= TPACPI_FAN_LAST_LEVEL
;
6501 level
= TP_EC_FAN_AUTO
;
6504 /* reserved for software-controlled auto mode */
6510 res
= fan_set_level_safe(level
);
6516 fan_watchdog_reset();
6521 static struct device_attribute dev_attr_fan_pwm1_enable
=
6522 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
6523 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
6525 /* sysfs fan pwm1 ------------------------------------------------------ */
6526 static ssize_t
fan_pwm1_show(struct device
*dev
,
6527 struct device_attribute
*attr
,
6533 res
= fan_get_status_safe(&status
);
6538 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
6539 status
= fan_control_desired_level
;
6544 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
6547 static ssize_t
fan_pwm1_store(struct device
*dev
,
6548 struct device_attribute
*attr
,
6549 const char *buf
, size_t count
)
6553 u8 status
, newlevel
;
6555 if (parse_strtoul(buf
, 255, &s
))
6558 /* scale down from 0-255 to 0-7 */
6559 newlevel
= (s
>> 5) & 0x07;
6561 if (mutex_lock_killable(&fan_mutex
))
6562 return -ERESTARTSYS
;
6564 rc
= fan_get_status(&status
);
6565 if (!rc
&& (status
&
6566 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6567 rc
= fan_set_level(newlevel
);
6571 fan_update_desired_level(newlevel
);
6572 fan_watchdog_reset();
6576 mutex_unlock(&fan_mutex
);
6577 return (rc
)? rc
: count
;
6580 static struct device_attribute dev_attr_fan_pwm1
=
6581 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
6582 fan_pwm1_show
, fan_pwm1_store
);
6584 /* sysfs fan fan1_input ------------------------------------------------ */
6585 static ssize_t
fan_fan1_input_show(struct device
*dev
,
6586 struct device_attribute
*attr
,
6592 res
= fan_get_speed(&speed
);
6596 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
6599 static struct device_attribute dev_attr_fan_fan1_input
=
6600 __ATTR(fan1_input
, S_IRUGO
,
6601 fan_fan1_input_show
, NULL
);
6603 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6604 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
6607 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
6610 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
6611 const char *buf
, size_t count
)
6615 if (parse_strtoul(buf
, 120, &t
))
6618 if (!fan_control_allowed
)
6621 fan_watchdog_maxinterval
= t
;
6622 fan_watchdog_reset();
6627 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
6628 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
6630 /* --------------------------------------------------------------------- */
6631 static struct attribute
*fan_attributes
[] = {
6632 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
6633 &dev_attr_fan_fan1_input
.attr
,
6637 static const struct attribute_group fan_attr_group
= {
6638 .attrs
= fan_attributes
,
6641 static int __init
fan_init(struct ibm_init_struct
*iibm
)
6645 vdbg_printk(TPACPI_DBG_INIT
, "initializing fan subdriver\n");
6647 mutex_init(&fan_mutex
);
6648 fan_status_access_mode
= TPACPI_FAN_NONE
;
6649 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
6650 fan_control_commands
= 0;
6651 fan_watchdog_maxinterval
= 0;
6652 tp_features
.fan_ctrl_status_undef
= 0;
6653 fan_control_desired_level
= 7;
6655 TPACPI_ACPIHANDLE_INIT(fans
);
6656 TPACPI_ACPIHANDLE_INIT(gfan
);
6657 TPACPI_ACPIHANDLE_INIT(sfan
);
6660 /* 570, 600e/x, 770e, 770x */
6661 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
6663 /* all other ThinkPads: note that even old-style
6664 * ThinkPad ECs supports the fan control register */
6665 if (likely(acpi_ec_read(fan_status_offset
,
6666 &fan_control_initial_status
))) {
6667 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
6668 fan_quirk1_detect();
6671 "ThinkPad ACPI EC access misbehaving, "
6672 "fan status and control unavailable\n");
6679 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
6680 fan_control_commands
|=
6681 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
6684 /* gfan without sfan means no fan control */
6685 /* all other models implement TP EC 0x2f control */
6689 fan_control_access_mode
=
6690 TPACPI_FAN_WR_ACPI_FANS
;
6691 fan_control_commands
|=
6692 TPACPI_FAN_CMD_SPEED
|
6693 TPACPI_FAN_CMD_LEVEL
|
6694 TPACPI_FAN_CMD_ENABLE
;
6696 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
6697 fan_control_commands
|=
6698 TPACPI_FAN_CMD_LEVEL
|
6699 TPACPI_FAN_CMD_ENABLE
;
6704 vdbg_printk(TPACPI_DBG_INIT
, "fan is %s, modes %d, %d\n",
6705 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
6706 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
6707 fan_status_access_mode
, fan_control_access_mode
);
6709 /* fan control master switch */
6710 if (!fan_control_allowed
) {
6711 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
6712 fan_control_commands
= 0;
6713 dbg_printk(TPACPI_DBG_INIT
,
6714 "fan control features disabled by parameter\n");
6717 /* update fan_control_desired_level */
6718 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
6719 fan_get_status_safe(NULL
);
6721 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
6722 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
6723 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6728 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
6729 &driver_attr_fan_watchdog
);
6731 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
6740 static void fan_exit(void)
6742 vdbg_printk(TPACPI_DBG_EXIT
,
6743 "cancelling any pending fan watchdog tasks\n");
6745 /* FIXME: can we really do this unconditionally? */
6746 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
6747 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
6748 &driver_attr_fan_watchdog
);
6750 cancel_delayed_work(&fan_watchdog_task
);
6751 flush_workqueue(tpacpi_wq
);
6754 static void fan_suspend(pm_message_t state
)
6758 if (!fan_control_allowed
)
6761 /* Store fan status in cache */
6762 fan_control_resume_level
= 0;
6763 rc
= fan_get_status_safe(&fan_control_resume_level
);
6765 printk(TPACPI_NOTICE
6766 "failed to read fan level for later "
6767 "restore during resume: %d\n", rc
);
6769 /* if it is undefined, don't attempt to restore it.
6771 if (tp_features
.fan_ctrl_status_undef
)
6772 fan_control_resume_level
= 0;
6775 static void fan_resume(void)
6777 u8 current_level
= 7;
6778 bool do_set
= false;
6781 /* DSDT *always* updates status on resume */
6782 tp_features
.fan_ctrl_status_undef
= 0;
6784 if (!fan_control_allowed
||
6785 !fan_control_resume_level
||
6786 (fan_get_status_safe(¤t_level
) < 0))
6789 switch (fan_control_access_mode
) {
6790 case TPACPI_FAN_WR_ACPI_SFAN
:
6791 /* never decrease fan level */
6792 do_set
= (fan_control_resume_level
> current_level
);
6794 case TPACPI_FAN_WR_ACPI_FANS
:
6795 case TPACPI_FAN_WR_TPEC
:
6796 /* never decrease fan level, scale is:
6797 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6799 * We expect the firmware to set either 7 or AUTO, but we
6800 * handle FULLSPEED out of paranoia.
6802 * So, we can safely only restore FULLSPEED or 7, anything
6803 * else could slow the fan. Restoring AUTO is useless, at
6804 * best that's exactly what the DSDT already set (it is the
6807 * Always keep in mind that the DSDT *will* have set the
6808 * fans to what the vendor supposes is the best level. We
6809 * muck with it only to speed the fan up.
6811 if (fan_control_resume_level
!= 7 &&
6812 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
6815 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
6816 (current_level
!= fan_control_resume_level
);
6822 printk(TPACPI_NOTICE
6823 "restoring fan level to 0x%02x\n",
6824 fan_control_resume_level
);
6825 rc
= fan_set_level_safe(fan_control_resume_level
);
6827 printk(TPACPI_NOTICE
6828 "failed to restore fan level: %d\n", rc
);
6832 static int fan_read(char *p
)
6837 unsigned int speed
= 0;
6839 switch (fan_status_access_mode
) {
6840 case TPACPI_FAN_RD_ACPI_GFAN
:
6841 /* 570, 600e/x, 770e, 770x */
6842 rc
= fan_get_status_safe(&status
);
6846 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
6848 (status
!= 0) ? "enabled" : "disabled", status
);
6851 case TPACPI_FAN_RD_TPEC
:
6852 /* all except 570, 600e/x, 770e, 770x */
6853 rc
= fan_get_status_safe(&status
);
6857 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
6858 (status
!= 0) ? "enabled" : "disabled");
6860 rc
= fan_get_speed(&speed
);
6864 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
6866 if (status
& TP_EC_FAN_FULLSPEED
)
6867 /* Disengaged mode takes precedence */
6868 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
6869 else if (status
& TP_EC_FAN_AUTO
)
6870 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
6872 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
6875 case TPACPI_FAN_NONE
:
6877 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
6880 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
6881 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
6883 switch (fan_control_access_mode
) {
6884 case TPACPI_FAN_WR_ACPI_SFAN
:
6885 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
6889 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
6890 "auto, disengaged, full-speed)\n");
6895 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
6896 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
6897 "commands:\twatchdog <timeout> (<timeout> "
6898 "is 0 (off), 1-120 (seconds))\n");
6900 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
6901 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
6902 " (<speed> is 0-65535)\n");
6907 static int fan_write_cmd_level(const char *cmd
, int *rc
)
6911 if (strlencmp(cmd
, "level auto") == 0)
6912 level
= TP_EC_FAN_AUTO
;
6913 else if ((strlencmp(cmd
, "level disengaged") == 0) |
6914 (strlencmp(cmd
, "level full-speed") == 0))
6915 level
= TP_EC_FAN_FULLSPEED
;
6916 else if (sscanf(cmd
, "level %d", &level
) != 1)
6919 *rc
= fan_set_level_safe(level
);
6921 printk(TPACPI_ERR
"level command accepted for unsupported "
6922 "access mode %d", fan_control_access_mode
);
6927 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
6929 if (strlencmp(cmd
, "enable") != 0)
6932 *rc
= fan_set_enable();
6934 printk(TPACPI_ERR
"enable command accepted for unsupported "
6935 "access mode %d", fan_control_access_mode
);
6940 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
6942 if (strlencmp(cmd
, "disable") != 0)
6945 *rc
= fan_set_disable();
6947 printk(TPACPI_ERR
"disable command accepted for unsupported "
6948 "access mode %d", fan_control_access_mode
);
6953 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
6958 * Support speed <low> <medium> <high> ? */
6960 if (sscanf(cmd
, "speed %d", &speed
) != 1)
6963 *rc
= fan_set_speed(speed
);
6965 printk(TPACPI_ERR
"speed command accepted for unsupported "
6966 "access mode %d", fan_control_access_mode
);
6971 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
6975 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
6978 if (interval
< 0 || interval
> 120)
6981 fan_watchdog_maxinterval
= interval
;
6986 static int fan_write(char *buf
)
6991 while (!rc
&& (cmd
= next_cmd(&buf
))) {
6992 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
6993 fan_write_cmd_level(cmd
, &rc
)) &&
6994 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
6995 (fan_write_cmd_enable(cmd
, &rc
) ||
6996 fan_write_cmd_disable(cmd
, &rc
) ||
6997 fan_write_cmd_watchdog(cmd
, &rc
))) &&
6998 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
6999 fan_write_cmd_speed(cmd
, &rc
))
7003 fan_watchdog_reset();
7009 static struct ibm_struct fan_driver_data
= {
7014 .suspend
= fan_suspend
,
7015 .resume
= fan_resume
,
7018 /****************************************************************************
7019 ****************************************************************************
7023 ****************************************************************************
7024 ****************************************************************************/
7026 /* sysfs name ---------------------------------------------------------- */
7027 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
7028 struct device_attribute
*attr
,
7031 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
7034 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
7035 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
7037 /* --------------------------------------------------------------------- */
7040 static struct proc_dir_entry
*proc_dir
;
7043 * Module and infrastructure proble, init and exit handling
7046 static int force_load
;
7048 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7049 static const char * __init
str_supported(int is_supported
)
7051 static char text_unsupported
[] __initdata
= "not supported";
7053 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
7055 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7057 static void ibm_exit(struct ibm_struct
*ibm
)
7059 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
7061 list_del_init(&ibm
->all_drivers
);
7063 if (ibm
->flags
.acpi_notify_installed
) {
7064 dbg_printk(TPACPI_DBG_EXIT
,
7065 "%s: acpi_remove_notify_handler\n", ibm
->name
);
7067 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
7069 dispatch_acpi_notify
);
7070 ibm
->flags
.acpi_notify_installed
= 0;
7071 ibm
->flags
.acpi_notify_installed
= 0;
7074 if (ibm
->flags
.proc_created
) {
7075 dbg_printk(TPACPI_DBG_EXIT
,
7076 "%s: remove_proc_entry\n", ibm
->name
);
7077 remove_proc_entry(ibm
->name
, proc_dir
);
7078 ibm
->flags
.proc_created
= 0;
7081 if (ibm
->flags
.acpi_driver_registered
) {
7082 dbg_printk(TPACPI_DBG_EXIT
,
7083 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
7085 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
7086 kfree(ibm
->acpi
->driver
);
7087 ibm
->acpi
->driver
= NULL
;
7088 ibm
->flags
.acpi_driver_registered
= 0;
7091 if (ibm
->flags
.init_called
&& ibm
->exit
) {
7093 ibm
->flags
.init_called
= 0;
7096 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
7099 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
7102 struct ibm_struct
*ibm
= iibm
->data
;
7103 struct proc_dir_entry
*entry
;
7105 BUG_ON(ibm
== NULL
);
7107 INIT_LIST_HEAD(&ibm
->all_drivers
);
7109 if (ibm
->flags
.experimental
&& !experimental
)
7112 dbg_printk(TPACPI_DBG_INIT
,
7113 "probing for %s\n", ibm
->name
);
7116 ret
= iibm
->init(iibm
);
7118 return 0; /* probe failed */
7122 ibm
->flags
.init_called
= 1;
7126 if (ibm
->acpi
->hid
) {
7127 ret
= register_tpacpi_subdriver(ibm
);
7132 if (ibm
->acpi
->notify
) {
7133 ret
= setup_acpi_notify(ibm
);
7134 if (ret
== -ENODEV
) {
7135 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
7145 dbg_printk(TPACPI_DBG_INIT
,
7146 "%s installed\n", ibm
->name
);
7149 entry
= create_proc_entry(ibm
->name
,
7150 S_IFREG
| S_IRUGO
| S_IWUSR
,
7153 printk(TPACPI_ERR
"unable to create proc entry %s\n",
7158 entry
->owner
= THIS_MODULE
;
7160 entry
->read_proc
= &dispatch_procfs_read
;
7162 entry
->write_proc
= &dispatch_procfs_write
;
7163 ibm
->flags
.proc_created
= 1;
7166 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
7171 dbg_printk(TPACPI_DBG_INIT
,
7172 "%s: at error exit path with result %d\n",
7176 return (ret
< 0)? ret
: 0;
7181 /* returns 0 - probe ok, or < 0 - probe error.
7182 * Probe ok doesn't mean thinkpad found.
7183 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7184 static int __must_check __init
get_thinkpad_model_data(
7185 struct thinkpad_id_data
*tp
)
7187 const struct dmi_device
*dev
= NULL
;
7188 char ec_fw_string
[18];
7194 memset(tp
, 0, sizeof(*tp
));
7196 if (dmi_name_in_vendors("IBM"))
7197 tp
->vendor
= PCI_VENDOR_ID_IBM
;
7198 else if (dmi_name_in_vendors("LENOVO"))
7199 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
7203 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
7204 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
7205 if (s
&& !tp
->bios_version_str
)
7207 if (!tp
->bios_version_str
)
7209 tp
->bios_model
= tp
->bios_version_str
[0]
7210 | (tp
->bios_version_str
[1] << 8);
7213 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7214 * X32 or newer, all Z series; Some models must have an
7215 * up-to-date BIOS or they will not be detected.
7217 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7219 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
7220 if (sscanf(dev
->name
,
7221 "IBM ThinkPad Embedded Controller -[%17c",
7222 ec_fw_string
) == 1) {
7223 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
7224 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
7226 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
7227 if (!tp
->ec_version_str
)
7229 tp
->ec_model
= ec_fw_string
[0]
7230 | (ec_fw_string
[1] << 8);
7235 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
7236 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
7237 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
7242 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
7243 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
7244 if (s
&& !tp
->nummodel_str
)
7250 static int __init
probe_for_thinkpad(void)
7258 * Non-ancient models have better DMI tagging, but very old models
7261 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
);
7263 /* ec is required because many other handles are relative to it */
7264 TPACPI_ACPIHANDLE_INIT(ec
);
7268 "Not yet supported ThinkPad detected!\n");
7273 * Risks a regression on very old machines, but reduces potential
7274 * false positives a damn great deal
7277 is_thinkpad
= (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
);
7279 if (!is_thinkpad
&& !force_load
)
7286 /* Module init, exit, parameters */
7288 static struct ibm_init_struct ibms_init
[] __initdata
= {
7290 .init
= thinkpad_acpi_driver_init
,
7291 .data
= &thinkpad_acpi_driver_data
,
7294 .init
= hotkey_init
,
7295 .data
= &hotkey_driver_data
,
7298 .init
= bluetooth_init
,
7299 .data
= &bluetooth_driver_data
,
7303 .data
= &wan_driver_data
,
7307 .data
= &uwb_driver_data
,
7309 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7312 .data
= &video_driver_data
,
7317 .data
= &light_driver_data
,
7319 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7322 .data
= &dock_driver_data
[0],
7326 .data
= &dock_driver_data
[1],
7329 #ifdef CONFIG_THINKPAD_ACPI_BAY
7332 .data
= &bay_driver_data
,
7337 .data
= &cmos_driver_data
,
7341 .data
= &led_driver_data
,
7345 .data
= &beep_driver_data
,
7348 .init
= thermal_init
,
7349 .data
= &thermal_driver_data
,
7352 .data
= &ecdump_driver_data
,
7355 .init
= brightness_init
,
7356 .data
= &brightness_driver_data
,
7359 .data
= &volume_driver_data
,
7363 .data
= &fan_driver_data
,
7367 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
7370 struct ibm_struct
*ibm
;
7372 if (!kp
|| !kp
->name
|| !val
)
7375 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7376 ibm
= ibms_init
[i
].data
;
7377 WARN_ON(ibm
== NULL
);
7379 if (!ibm
|| !ibm
->name
)
7382 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
7383 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
7385 strcpy(ibms_init
[i
].param
, val
);
7386 strcat(ibms_init
[i
].param
, ",");
7394 module_param(experimental
, int, 0);
7395 MODULE_PARM_DESC(experimental
,
7396 "Enables experimental features when non-zero");
7398 module_param_named(debug
, dbg_level
, uint
, 0);
7399 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
7401 module_param(force_load
, bool, 0);
7402 MODULE_PARM_DESC(force_load
,
7403 "Attempts to load the driver even on a "
7404 "mis-identified ThinkPad when true");
7406 module_param_named(fan_control
, fan_control_allowed
, bool, 0);
7407 MODULE_PARM_DESC(fan_control
,
7408 "Enables setting fan parameters features when true");
7410 module_param_named(brightness_mode
, brightness_mode
, int, 0);
7411 MODULE_PARM_DESC(brightness_mode
,
7412 "Selects brightness control strategy: "
7413 "0=auto, 1=EC, 2=CMOS, 3=both");
7415 module_param(brightness_enable
, uint
, 0);
7416 MODULE_PARM_DESC(brightness_enable
,
7417 "Enables backlight control when 1, disables when 0");
7419 module_param(hotkey_report_mode
, uint
, 0);
7420 MODULE_PARM_DESC(hotkey_report_mode
,
7421 "used for backwards compatibility with userspace, "
7422 "see documentation");
7424 #define TPACPI_PARAM(feature) \
7425 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7426 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7427 "at module load, see documentation")
7429 TPACPI_PARAM(hotkey
);
7430 TPACPI_PARAM(bluetooth
);
7431 TPACPI_PARAM(video
);
7432 TPACPI_PARAM(light
);
7433 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7436 #ifdef CONFIG_THINKPAD_ACPI_BAY
7438 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7442 TPACPI_PARAM(ecdump
);
7443 TPACPI_PARAM(brightness
);
7444 TPACPI_PARAM(volume
);
7447 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7448 module_param(dbg_wlswemul
, uint
, 0);
7449 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
7450 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
7451 MODULE_PARM_DESC(wlsw_state
,
7452 "Initial state of the emulated WLSW switch");
7454 module_param(dbg_bluetoothemul
, uint
, 0);
7455 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
7456 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
7457 MODULE_PARM_DESC(bluetooth_state
,
7458 "Initial state of the emulated bluetooth switch");
7460 module_param(dbg_wwanemul
, uint
, 0);
7461 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
7462 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
7463 MODULE_PARM_DESC(wwan_state
,
7464 "Initial state of the emulated WWAN switch");
7466 module_param(dbg_uwbemul
, uint
, 0);
7467 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
7468 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
7469 MODULE_PARM_DESC(uwb_state
,
7470 "Initial state of the emulated UWB switch");
7473 static void thinkpad_acpi_module_exit(void)
7475 struct ibm_struct
*ibm
, *itmp
;
7477 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
7479 list_for_each_entry_safe_reverse(ibm
, itmp
,
7480 &tpacpi_all_drivers
,
7485 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
7487 if (tpacpi_inputdev
) {
7488 if (tp_features
.input_device_registered
)
7489 input_unregister_device(tpacpi_inputdev
);
7491 input_free_device(tpacpi_inputdev
);
7495 hwmon_device_unregister(tpacpi_hwmon
);
7497 if (tp_features
.sensors_pdev_attrs_registered
)
7498 device_remove_file(&tpacpi_sensors_pdev
->dev
,
7499 &dev_attr_thinkpad_acpi_pdev_name
);
7500 if (tpacpi_sensors_pdev
)
7501 platform_device_unregister(tpacpi_sensors_pdev
);
7503 platform_device_unregister(tpacpi_pdev
);
7505 if (tp_features
.sensors_pdrv_attrs_registered
)
7506 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
7507 if (tp_features
.platform_drv_attrs_registered
)
7508 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
7510 if (tp_features
.sensors_pdrv_registered
)
7511 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
7513 if (tp_features
.platform_drv_registered
)
7514 platform_driver_unregister(&tpacpi_pdriver
);
7517 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
7520 destroy_workqueue(tpacpi_wq
);
7522 kfree(thinkpad_id
.bios_version_str
);
7523 kfree(thinkpad_id
.ec_version_str
);
7524 kfree(thinkpad_id
.model_str
);
7528 static int __init
thinkpad_acpi_module_init(void)
7532 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
7534 /* Parameter checking */
7535 if (hotkey_report_mode
> 2)
7538 /* Driver-level probe */
7540 ret
= get_thinkpad_model_data(&thinkpad_id
);
7543 "unable to get DMI data: %d\n", ret
);
7544 thinkpad_acpi_module_exit();
7547 ret
= probe_for_thinkpad();
7549 thinkpad_acpi_module_exit();
7553 /* Driver initialization */
7555 TPACPI_ACPIHANDLE_INIT(ecrd
);
7556 TPACPI_ACPIHANDLE_INIT(ecwr
);
7558 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
7560 thinkpad_acpi_module_exit();
7564 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
7567 "unable to create proc dir " TPACPI_PROC_DIR
);
7568 thinkpad_acpi_module_exit();
7571 proc_dir
->owner
= THIS_MODULE
;
7573 ret
= platform_driver_register(&tpacpi_pdriver
);
7576 "unable to register main platform driver\n");
7577 thinkpad_acpi_module_exit();
7580 tp_features
.platform_drv_registered
= 1;
7582 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
7585 "unable to register hwmon platform driver\n");
7586 thinkpad_acpi_module_exit();
7589 tp_features
.sensors_pdrv_registered
= 1;
7591 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
7593 tp_features
.platform_drv_attrs_registered
= 1;
7594 ret
= tpacpi_create_driver_attributes(
7595 &tpacpi_hwmon_pdriver
.driver
);
7599 "unable to create sysfs driver attributes\n");
7600 thinkpad_acpi_module_exit();
7603 tp_features
.sensors_pdrv_attrs_registered
= 1;
7606 /* Device initialization */
7607 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
7609 if (IS_ERR(tpacpi_pdev
)) {
7610 ret
= PTR_ERR(tpacpi_pdev
);
7612 printk(TPACPI_ERR
"unable to register platform device\n");
7613 thinkpad_acpi_module_exit();
7616 tpacpi_sensors_pdev
= platform_device_register_simple(
7617 TPACPI_HWMON_DRVR_NAME
,
7619 if (IS_ERR(tpacpi_sensors_pdev
)) {
7620 ret
= PTR_ERR(tpacpi_sensors_pdev
);
7621 tpacpi_sensors_pdev
= NULL
;
7623 "unable to register hwmon platform device\n");
7624 thinkpad_acpi_module_exit();
7627 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
7628 &dev_attr_thinkpad_acpi_pdev_name
);
7631 "unable to create sysfs hwmon device attributes\n");
7632 thinkpad_acpi_module_exit();
7635 tp_features
.sensors_pdev_attrs_registered
= 1;
7636 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
7637 if (IS_ERR(tpacpi_hwmon
)) {
7638 ret
= PTR_ERR(tpacpi_hwmon
);
7639 tpacpi_hwmon
= NULL
;
7640 printk(TPACPI_ERR
"unable to register hwmon device\n");
7641 thinkpad_acpi_module_exit();
7644 mutex_init(&tpacpi_inputdev_send_mutex
);
7645 tpacpi_inputdev
= input_allocate_device();
7646 if (!tpacpi_inputdev
) {
7647 printk(TPACPI_ERR
"unable to allocate input device\n");
7648 thinkpad_acpi_module_exit();
7651 /* Prepare input device, but don't register */
7652 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
7653 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
7654 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
7655 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
7656 thinkpad_id
.vendor
:
7658 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
7659 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
7661 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
7662 ret
= ibm_init(&ibms_init
[i
]);
7663 if (ret
>= 0 && *ibms_init
[i
].param
)
7664 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
7666 thinkpad_acpi_module_exit();
7670 ret
= input_register_device(tpacpi_inputdev
);
7672 printk(TPACPI_ERR
"unable to register input device\n");
7673 thinkpad_acpi_module_exit();
7676 tp_features
.input_device_registered
= 1;
7679 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
7683 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
7686 * DMI matching for module autoloading
7688 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7689 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7691 * Only models listed in thinkwiki will be supported, so add yours
7692 * if it is not there yet.
7694 #define IBM_BIOS_MODULE_ALIAS(__type) \
7695 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7697 /* Non-ancient thinkpads */
7698 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7699 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7701 /* Ancient thinkpad BIOSes have to be identified by
7702 * BIOS type or model number, and there are far less
7703 * BIOS types than model numbers... */
7704 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7705 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7706 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7708 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7709 MODULE_DESCRIPTION(TPACPI_DESC
);
7710 MODULE_VERSION(TPACPI_VERSION
);
7711 MODULE_LICENSE("GPL");
7713 module_init(thinkpad_acpi_module_init
);
7714 module_exit(thinkpad_acpi_module_exit
);