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.23"
25 #define TPACPI_SYSFS_VERSION 0x020600
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 */
149 enum tpacpi_hkey_event_t
{
151 TP_HKEY_EV_HOTKEY_BASE
= 0x1001, /* first hotkey (FN+F1) */
152 TP_HKEY_EV_BRGHT_UP
= 0x1010, /* Brightness up */
153 TP_HKEY_EV_BRGHT_DOWN
= 0x1011, /* Brightness down */
154 TP_HKEY_EV_VOL_UP
= 0x1015, /* Volume up or unmute */
155 TP_HKEY_EV_VOL_DOWN
= 0x1016, /* Volume down or unmute */
156 TP_HKEY_EV_VOL_MUTE
= 0x1017, /* Mixer output mute */
158 /* Reasons for waking up from S3/S4 */
159 TP_HKEY_EV_WKUP_S3_UNDOCK
= 0x2304, /* undock requested, S3 */
160 TP_HKEY_EV_WKUP_S4_UNDOCK
= 0x2404, /* undock requested, S4 */
161 TP_HKEY_EV_WKUP_S3_BAYEJ
= 0x2305, /* bay ejection req, S3 */
162 TP_HKEY_EV_WKUP_S4_BAYEJ
= 0x2405, /* bay ejection req, S4 */
163 TP_HKEY_EV_WKUP_S3_BATLOW
= 0x2313, /* battery empty, S3 */
164 TP_HKEY_EV_WKUP_S4_BATLOW
= 0x2413, /* battery empty, S4 */
166 /* Auto-sleep after eject request */
167 TP_HKEY_EV_BAYEJ_ACK
= 0x3003, /* bay ejection complete */
168 TP_HKEY_EV_UNDOCK_ACK
= 0x4003, /* undock complete */
170 /* Misc bay events */
171 TP_HKEY_EV_OPTDRV_EJ
= 0x3006, /* opt. drive tray ejected */
173 /* User-interface events */
174 TP_HKEY_EV_LID_CLOSE
= 0x5001, /* laptop lid closed */
175 TP_HKEY_EV_LID_OPEN
= 0x5002, /* laptop lid opened */
176 TP_HKEY_EV_TABLET_TABLET
= 0x5009, /* tablet swivel up */
177 TP_HKEY_EV_TABLET_NOTEBOOK
= 0x500a, /* tablet swivel down */
178 TP_HKEY_EV_PEN_INSERTED
= 0x500b, /* tablet pen inserted */
179 TP_HKEY_EV_PEN_REMOVED
= 0x500c, /* tablet pen removed */
180 TP_HKEY_EV_BRGHT_CHANGED
= 0x5010, /* backlight control event */
183 TP_HKEY_EV_ALARM_BAT_HOT
= 0x6011, /* battery too hot */
184 TP_HKEY_EV_ALARM_BAT_XHOT
= 0x6012, /* battery critically hot */
185 TP_HKEY_EV_ALARM_SENSOR_HOT
= 0x6021, /* sensor too hot */
186 TP_HKEY_EV_ALARM_SENSOR_XHOT
= 0x6022, /* sensor critically hot */
187 TP_HKEY_EV_THM_TABLE_CHANGED
= 0x6030, /* thermal table changed */
190 TP_HKEY_EV_RFKILL_CHANGED
= 0x7000, /* rfkill switch changed */
193 /****************************************************************************
197 #define TPACPI_NAME "thinkpad"
198 #define TPACPI_DESC "ThinkPad ACPI Extras"
199 #define TPACPI_FILE TPACPI_NAME "_acpi"
200 #define TPACPI_URL "http://ibm-acpi.sf.net/"
201 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
203 #define TPACPI_PROC_DIR "ibm"
204 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
205 #define TPACPI_DRVR_NAME TPACPI_FILE
206 #define TPACPI_DRVR_SHORTNAME "tpacpi"
207 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
209 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
210 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
212 #define TPACPI_MAX_ACPI_ARGS 3
215 #define TPACPI_LOG TPACPI_FILE ": "
216 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
217 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
218 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
219 #define TPACPI_ERR KERN_ERR TPACPI_LOG
220 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
221 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
222 #define TPACPI_INFO KERN_INFO TPACPI_LOG
223 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
225 /* Debugging printk groups */
226 #define TPACPI_DBG_ALL 0xffff
227 #define TPACPI_DBG_DISCLOSETASK 0x8000
228 #define TPACPI_DBG_INIT 0x0001
229 #define TPACPI_DBG_EXIT 0x0002
230 #define TPACPI_DBG_RFKILL 0x0004
231 #define TPACPI_DBG_HKEY 0x0008
232 #define TPACPI_DBG_FAN 0x0010
233 #define TPACPI_DBG_BRGHT 0x0020
234 #define TPACPI_DBG_MIXER 0x0040
236 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
237 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
238 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
241 /****************************************************************************
242 * Driver-wide structs and misc. variables
247 struct tp_acpi_drv_struct
{
248 const struct acpi_device_id
*hid
;
249 struct acpi_driver
*driver
;
251 void (*notify
) (struct ibm_struct
*, u32
);
254 struct acpi_device
*device
;
260 int (*read
) (char *);
261 int (*write
) (char *);
263 void (*resume
) (void);
264 void (*suspend
) (pm_message_t state
);
265 void (*shutdown
) (void);
267 struct list_head all_drivers
;
269 struct tp_acpi_drv_struct
*acpi
;
272 u8 acpi_driver_registered
:1;
273 u8 acpi_notify_installed
:1;
280 struct ibm_init_struct
{
283 int (*init
) (struct ibm_init_struct
*);
284 struct ibm_struct
*data
;
295 u32 bright_16levels
:1;
296 u32 bright_acpimode
:1;
299 u32 fan_ctrl_status_undef
:1;
301 u32 beep_needs_two_args
:1;
302 u32 input_device_registered
:1;
303 u32 platform_drv_registered
:1;
304 u32 platform_drv_attrs_registered
:1;
305 u32 sensors_pdrv_registered
:1;
306 u32 sensors_pdrv_attrs_registered
:1;
307 u32 sensors_pdev_attrs_registered
:1;
308 u32 hotkey_poll_active
:1;
312 u16 hotkey_mask_ff
:1;
315 struct thinkpad_id_data
{
316 unsigned int vendor
; /* ThinkPad vendor:
317 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
319 char *bios_version_str
; /* Something like 1ZET51WW (1.03z) */
320 char *ec_version_str
; /* Something like 1ZHT51WW-1.04a */
322 u16 bios_model
; /* 1Y = 0x5931, 0 = unknown */
324 u16 bios_release
; /* 1ZETK1WW = 0x314b, 0 = unknown */
327 char *model_str
; /* ThinkPad T43 */
328 char *nummodel_str
; /* 9384A9C for a 9384-A9C model */
330 static struct thinkpad_id_data thinkpad_id
;
333 TPACPI_LIFE_INIT
= 0,
338 static int experimental
;
339 static u32 dbg_level
;
341 static struct workqueue_struct
*tpacpi_wq
;
349 /* Special LED class that can defer work */
350 struct tpacpi_led_classdev
{
351 struct led_classdev led_classdev
;
352 struct work_struct work
;
353 enum led_status_t new_state
;
357 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
358 static int dbg_wlswemul
;
359 static int tpacpi_wlsw_emulstate
;
360 static int dbg_bluetoothemul
;
361 static int tpacpi_bluetooth_emulstate
;
362 static int dbg_wwanemul
;
363 static int tpacpi_wwan_emulstate
;
364 static int dbg_uwbemul
;
365 static int tpacpi_uwb_emulstate
;
369 /*************************************************************************
373 #define dbg_printk(a_dbg_level, format, arg...) \
374 do { if (dbg_level & (a_dbg_level)) \
375 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
378 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
379 #define vdbg_printk dbg_printk
380 static const char *str_supported(int is_supported
);
382 #define vdbg_printk(a_dbg_level, format, arg...) \
386 static void tpacpi_log_usertask(const char * const what
)
388 printk(TPACPI_DEBUG
"%s: access by process with PID %d\n",
389 what
, task_tgid_vnr(current
));
392 #define tpacpi_disclose_usertask(what, format, arg...) \
395 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
396 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
397 printk(TPACPI_DEBUG "%s: PID %d: " format, \
398 what, task_tgid_vnr(current), ## arg); \
403 * Quirk handling helpers
405 * ThinkPad IDs and versions seen in the field so far
406 * are two-characters from the set [0-9A-Z], i.e. base 36.
408 * We use values well outside that range as specials.
411 #define TPACPI_MATCH_ANY 0xffffU
412 #define TPACPI_MATCH_UNKNOWN 0U
414 /* TPID('1', 'Y') == 0x5931 */
415 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
417 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
418 { .vendor = PCI_VENDOR_ID_IBM, \
419 .bios = TPID(__id1, __id2), \
420 .ec = TPACPI_MATCH_ANY, \
421 .quirks = (__quirk) }
423 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
424 { .vendor = PCI_VENDOR_ID_LENOVO, \
425 .bios = TPID(__id1, __id2), \
426 .ec = TPACPI_MATCH_ANY, \
427 .quirks = (__quirk) }
429 struct tpacpi_quirk
{
433 unsigned long quirks
;
437 * tpacpi_check_quirks() - search BIOS/EC version on a list
438 * @qlist: array of &struct tpacpi_quirk
439 * @qlist_size: number of elements in @qlist
441 * Iterates over a quirks list until one is found that matches the
442 * ThinkPad's vendor, BIOS and EC model.
444 * Returns 0 if nothing matches, otherwise returns the quirks field of
445 * the matching &struct tpacpi_quirk entry.
447 * The match criteria is: vendor, ec and bios much match.
449 static unsigned long __init
tpacpi_check_quirks(
450 const struct tpacpi_quirk
*qlist
,
451 unsigned int qlist_size
)
454 if ((qlist
->vendor
== thinkpad_id
.vendor
||
455 qlist
->vendor
== TPACPI_MATCH_ANY
) &&
456 (qlist
->bios
== thinkpad_id
.bios_model
||
457 qlist
->bios
== TPACPI_MATCH_ANY
) &&
458 (qlist
->ec
== thinkpad_id
.ec_model
||
459 qlist
->ec
== TPACPI_MATCH_ANY
))
460 return qlist
->quirks
;
469 /****************************************************************************
470 ****************************************************************************
472 * ACPI Helpers and device model
474 ****************************************************************************
475 ****************************************************************************/
477 /*************************************************************************
481 static acpi_handle root_handle
;
483 #define TPACPI_HANDLE(object, parent, paths...) \
484 static acpi_handle object##_handle; \
485 static acpi_handle *object##_parent = &parent##_handle; \
486 static char *object##_path; \
487 static char *object##_paths[] = { paths }
489 TPACPI_HANDLE(ec
, root
, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
490 "\\_SB.PCI.ISA.EC", /* 570 */
491 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
492 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
493 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
494 "\\_SB.PCI0.ICH3.EC0", /* R31 */
495 "\\_SB.PCI0.LPC.EC", /* all others */
498 TPACPI_HANDLE(ecrd
, ec
, "ECRD"); /* 570 */
499 TPACPI_HANDLE(ecwr
, ec
, "ECWR"); /* 570 */
501 TPACPI_HANDLE(cmos
, root
, "\\UCMS", /* R50, R50e, R50p, R51, */
503 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
504 "\\CMS", /* R40, R40e */
507 TPACPI_HANDLE(hkey
, ec
, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
508 "^HKEY", /* R30, R31 */
509 "HKEY", /* all others */
512 TPACPI_HANDLE(vid
, root
, "\\_SB.PCI.AGP.VGA", /* 570 */
513 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
514 "\\_SB.PCI0.VID0", /* 770e */
515 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
516 "\\_SB.PCI0.AGP.VID", /* all others */
520 /*************************************************************************
524 static int acpi_evalf(acpi_handle handle
,
525 void *res
, char *method
, char *fmt
, ...)
528 struct acpi_object_list params
;
529 union acpi_object in_objs
[TPACPI_MAX_ACPI_ARGS
];
530 struct acpi_buffer result
, *resultp
;
531 union acpi_object out_obj
;
539 printk(TPACPI_ERR
"acpi_evalf() called with empty format\n");
552 params
.pointer
= &in_objs
[0];
559 in_objs
[params
.count
].integer
.value
= va_arg(ap
, int);
560 in_objs
[params
.count
++].type
= ACPI_TYPE_INTEGER
;
562 /* add more types as needed */
564 printk(TPACPI_ERR
"acpi_evalf() called "
565 "with invalid format character '%c'\n", c
);
571 if (res_type
!= 'v') {
572 result
.length
= sizeof(out_obj
);
573 result
.pointer
= &out_obj
;
578 status
= acpi_evaluate_object(handle
, method
, ¶ms
, resultp
);
583 *(int *)res
= out_obj
.integer
.value
;
584 success
= status
== AE_OK
&& out_obj
.type
== ACPI_TYPE_INTEGER
;
587 success
= status
== AE_OK
;
589 /* add more types as needed */
591 printk(TPACPI_ERR
"acpi_evalf() called "
592 "with invalid format character '%c'\n", res_type
);
596 if (!success
&& !quiet
)
597 printk(TPACPI_ERR
"acpi_evalf(%s, %s, ...) failed: %d\n",
598 method
, fmt0
, status
);
603 static int acpi_ec_read(int i
, u8
*p
)
608 if (!acpi_evalf(ecrd_handle
, &v
, NULL
, "dd", i
))
612 if (ec_read(i
, p
) < 0)
619 static int acpi_ec_write(int i
, u8 v
)
622 if (!acpi_evalf(ecwr_handle
, NULL
, NULL
, "vdd", i
, v
))
625 if (ec_write(i
, v
) < 0)
632 static int issue_thinkpad_cmos_command(int cmos_cmd
)
637 if (!acpi_evalf(cmos_handle
, NULL
, NULL
, "vd", cmos_cmd
))
643 /*************************************************************************
647 #define TPACPI_ACPIHANDLE_INIT(object) \
648 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
649 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
651 static void drv_acpi_handle_init(char *name
,
652 acpi_handle
*handle
, acpi_handle parent
,
653 char **paths
, int num_paths
, char **path
)
658 vdbg_printk(TPACPI_DBG_INIT
, "trying to locate ACPI handle for %s\n",
661 for (i
= 0; i
< num_paths
; i
++) {
662 status
= acpi_get_handle(parent
, paths
[i
], handle
);
663 if (ACPI_SUCCESS(status
)) {
665 dbg_printk(TPACPI_DBG_INIT
,
666 "Found ACPI handle %s for %s\n",
672 vdbg_printk(TPACPI_DBG_INIT
, "ACPI handle for %s not found\n",
677 static void dispatch_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
679 struct ibm_struct
*ibm
= data
;
681 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
684 if (!ibm
|| !ibm
->acpi
|| !ibm
->acpi
->notify
)
687 ibm
->acpi
->notify(ibm
, event
);
690 static int __init
setup_acpi_notify(struct ibm_struct
*ibm
)
697 if (!*ibm
->acpi
->handle
)
700 vdbg_printk(TPACPI_DBG_INIT
,
701 "setting up ACPI notify for %s\n", ibm
->name
);
703 rc
= acpi_bus_get_device(*ibm
->acpi
->handle
, &ibm
->acpi
->device
);
705 printk(TPACPI_ERR
"acpi_bus_get_device(%s) failed: %d\n",
710 ibm
->acpi
->device
->driver_data
= ibm
;
711 sprintf(acpi_device_class(ibm
->acpi
->device
), "%s/%s",
712 TPACPI_ACPI_EVENT_PREFIX
,
715 status
= acpi_install_notify_handler(*ibm
->acpi
->handle
,
716 ibm
->acpi
->type
, dispatch_acpi_notify
, ibm
);
717 if (ACPI_FAILURE(status
)) {
718 if (status
== AE_ALREADY_EXISTS
) {
720 "another device driver is already "
721 "handling %s events\n", ibm
->name
);
724 "acpi_install_notify_handler(%s) failed: %d\n",
729 ibm
->flags
.acpi_notify_installed
= 1;
733 static int __init
tpacpi_device_add(struct acpi_device
*device
)
738 static int __init
register_tpacpi_subdriver(struct ibm_struct
*ibm
)
742 dbg_printk(TPACPI_DBG_INIT
,
743 "registering %s as an ACPI driver\n", ibm
->name
);
747 ibm
->acpi
->driver
= kzalloc(sizeof(struct acpi_driver
), GFP_KERNEL
);
748 if (!ibm
->acpi
->driver
) {
750 "failed to allocate memory for ibm->acpi->driver\n");
754 sprintf(ibm
->acpi
->driver
->name
, "%s_%s", TPACPI_NAME
, ibm
->name
);
755 ibm
->acpi
->driver
->ids
= ibm
->acpi
->hid
;
757 ibm
->acpi
->driver
->ops
.add
= &tpacpi_device_add
;
759 rc
= acpi_bus_register_driver(ibm
->acpi
->driver
);
761 printk(TPACPI_ERR
"acpi_bus_register_driver(%s) failed: %d\n",
763 kfree(ibm
->acpi
->driver
);
764 ibm
->acpi
->driver
= NULL
;
766 ibm
->flags
.acpi_driver_registered
= 1;
772 /****************************************************************************
773 ****************************************************************************
777 ****************************************************************************
778 ****************************************************************************/
780 static int dispatch_procfs_read(char *page
, char **start
, off_t off
,
781 int count
, int *eof
, void *data
)
783 struct ibm_struct
*ibm
= data
;
786 if (!ibm
|| !ibm
->read
)
789 len
= ibm
->read(page
);
793 if (len
<= off
+ count
)
805 static int dispatch_procfs_write(struct file
*file
,
806 const char __user
*userbuf
,
807 unsigned long count
, void *data
)
809 struct ibm_struct
*ibm
= data
;
813 if (!ibm
|| !ibm
->write
)
815 if (count
> PAGE_SIZE
- 2)
818 kernbuf
= kmalloc(count
+ 2, GFP_KERNEL
);
822 if (copy_from_user(kernbuf
, userbuf
, count
)) {
828 strcat(kernbuf
, ",");
829 ret
= ibm
->write(kernbuf
);
838 static char *next_cmd(char **cmds
)
843 while ((end
= strchr(start
, ',')) && end
== start
)
855 /****************************************************************************
856 ****************************************************************************
858 * Device model: input, hwmon and platform
860 ****************************************************************************
861 ****************************************************************************/
863 static struct platform_device
*tpacpi_pdev
;
864 static struct platform_device
*tpacpi_sensors_pdev
;
865 static struct device
*tpacpi_hwmon
;
866 static struct input_dev
*tpacpi_inputdev
;
867 static struct mutex tpacpi_inputdev_send_mutex
;
868 static LIST_HEAD(tpacpi_all_drivers
);
870 static int tpacpi_suspend_handler(struct platform_device
*pdev
,
873 struct ibm_struct
*ibm
, *itmp
;
875 list_for_each_entry_safe(ibm
, itmp
,
879 (ibm
->suspend
)(state
);
885 static int tpacpi_resume_handler(struct platform_device
*pdev
)
887 struct ibm_struct
*ibm
, *itmp
;
889 list_for_each_entry_safe(ibm
, itmp
,
899 static void tpacpi_shutdown_handler(struct platform_device
*pdev
)
901 struct ibm_struct
*ibm
, *itmp
;
903 list_for_each_entry_safe(ibm
, itmp
,
911 static struct platform_driver tpacpi_pdriver
= {
913 .name
= TPACPI_DRVR_NAME
,
914 .owner
= THIS_MODULE
,
916 .suspend
= tpacpi_suspend_handler
,
917 .resume
= tpacpi_resume_handler
,
918 .shutdown
= tpacpi_shutdown_handler
,
921 static struct platform_driver tpacpi_hwmon_pdriver
= {
923 .name
= TPACPI_HWMON_DRVR_NAME
,
924 .owner
= THIS_MODULE
,
928 /*************************************************************************
929 * sysfs support helpers
932 struct attribute_set
{
933 unsigned int members
, max_members
;
934 struct attribute_group group
;
937 struct attribute_set_obj
{
938 struct attribute_set s
;
940 } __attribute__((packed
));
942 static struct attribute_set
*create_attr_set(unsigned int max_members
,
945 struct attribute_set_obj
*sobj
;
947 if (max_members
== 0)
950 /* Allocates space for implicit NULL at the end too */
951 sobj
= kzalloc(sizeof(struct attribute_set_obj
) +
952 max_members
* sizeof(struct attribute
*),
956 sobj
->s
.max_members
= max_members
;
957 sobj
->s
.group
.attrs
= &sobj
->a
;
958 sobj
->s
.group
.name
= name
;
963 #define destroy_attr_set(_set) \
966 /* not multi-threaded safe, use it in a single thread per set */
967 static int add_to_attr_set(struct attribute_set
*s
, struct attribute
*attr
)
972 if (s
->members
>= s
->max_members
)
975 s
->group
.attrs
[s
->members
] = attr
;
981 static int add_many_to_attr_set(struct attribute_set
*s
,
982 struct attribute
**attr
,
987 for (i
= 0; i
< count
; i
++) {
988 res
= add_to_attr_set(s
, attr
[i
]);
996 static void delete_attr_set(struct attribute_set
*s
, struct kobject
*kobj
)
998 sysfs_remove_group(kobj
, &s
->group
);
1002 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1003 sysfs_create_group(_kobj, &_attr_set->group)
1005 static int parse_strtoul(const char *buf
,
1006 unsigned long max
, unsigned long *value
)
1010 while (*buf
&& isspace(*buf
))
1012 *value
= simple_strtoul(buf
, &endp
, 0);
1013 while (*endp
&& isspace(*endp
))
1015 if (*endp
|| *value
> max
)
1021 static void tpacpi_disable_brightness_delay(void)
1023 if (acpi_evalf(hkey_handle
, NULL
, "PWMS", "qvd", 0))
1024 printk(TPACPI_NOTICE
1025 "ACPI backlight control delay disabled\n");
1028 static int __init
tpacpi_query_bcl_levels(acpi_handle handle
)
1030 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1031 union acpi_object
*obj
;
1034 if (ACPI_SUCCESS(acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
))) {
1035 obj
= (union acpi_object
*)buffer
.pointer
;
1036 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
1037 printk(TPACPI_ERR
"Unknown _BCL data, "
1038 "please report this to %s\n", TPACPI_MAIL
);
1041 rc
= obj
->package
.count
;
1047 kfree(buffer
.pointer
);
1051 static acpi_status __init
tpacpi_acpi_walk_find_bcl(acpi_handle handle
,
1052 u32 lvl
, void *context
, void **rv
)
1054 char name
[ACPI_PATH_SEGMENT_LENGTH
];
1055 struct acpi_buffer buffer
= { sizeof(name
), &name
};
1057 if (ACPI_SUCCESS(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
)) &&
1058 !strncmp("_BCL", name
, sizeof(name
) - 1)) {
1059 BUG_ON(!rv
|| !*rv
);
1060 **(int **)rv
= tpacpi_query_bcl_levels(handle
);
1061 return AE_CTRL_TERMINATE
;
1068 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1070 static int __init
tpacpi_check_std_acpi_brightness_support(void)
1074 void *bcl_ptr
= &bcl_levels
;
1077 TPACPI_ACPIHANDLE_INIT(vid
);
1083 * Search for a _BCL method, and execute it. This is safe on all
1084 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1085 * BIOS in ACPI backlight control mode. We do NOT have to care
1086 * about calling the _BCL method in an enabled video device, any
1087 * will do for our purposes.
1090 status
= acpi_walk_namespace(ACPI_TYPE_METHOD
, vid_handle
, 3,
1091 tpacpi_acpi_walk_find_bcl
, NULL
,
1094 if (ACPI_SUCCESS(status
) && bcl_levels
> 2) {
1095 tp_features
.bright_acpimode
= 1;
1096 return (bcl_levels
- 2);
1102 static void printk_deprecated_attribute(const char * const what
,
1103 const char * const details
)
1105 tpacpi_log_usertask("deprecated sysfs attribute");
1106 printk(TPACPI_WARN
"WARNING: sysfs attribute %s is deprecated and "
1107 "will be removed. %s\n",
1111 /*************************************************************************
1112 * rfkill and radio control support helpers
1116 * ThinkPad-ACPI firmware handling model:
1118 * WLSW (master wireless switch) is event-driven, and is common to all
1119 * firmware-controlled radios. It cannot be controlled, just monitored,
1120 * as expected. It overrides all radio state in firmware
1122 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1123 * (TODO: verify how WLSW interacts with the returned radio state).
1125 * The only time there are shadow radio state changes, is when
1126 * masked-off hotkeys are used.
1130 * Internal driver API for radio state:
1132 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1133 * bool: true means radio blocked (off)
1135 enum tpacpi_rfkill_state
{
1136 TPACPI_RFK_RADIO_OFF
= 0,
1140 /* rfkill switches */
1141 enum tpacpi_rfk_id
{
1142 TPACPI_RFK_BLUETOOTH_SW_ID
= 0,
1143 TPACPI_RFK_WWAN_SW_ID
,
1144 TPACPI_RFK_UWB_SW_ID
,
1148 static const char *tpacpi_rfkill_names
[] = {
1149 [TPACPI_RFK_BLUETOOTH_SW_ID
] = "bluetooth",
1150 [TPACPI_RFK_WWAN_SW_ID
] = "wwan",
1151 [TPACPI_RFK_UWB_SW_ID
] = "uwb",
1152 [TPACPI_RFK_SW_MAX
] = NULL
1155 /* ThinkPad-ACPI rfkill subdriver */
1157 struct rfkill
*rfkill
;
1158 enum tpacpi_rfk_id id
;
1159 const struct tpacpi_rfk_ops
*ops
;
1162 struct tpacpi_rfk_ops
{
1163 /* firmware interface */
1164 int (*get_status
)(void);
1165 int (*set_status
)(const enum tpacpi_rfkill_state
);
1168 static struct tpacpi_rfk
*tpacpi_rfkill_switches
[TPACPI_RFK_SW_MAX
];
1170 /* Query FW and update rfkill sw state for a given rfkill switch */
1171 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk
*tp_rfk
)
1178 status
= (tp_rfk
->ops
->get_status
)();
1182 rfkill_set_sw_state(tp_rfk
->rfkill
,
1183 (status
== TPACPI_RFK_RADIO_OFF
));
1188 /* Query FW and update rfkill sw state for all rfkill switches */
1189 static void tpacpi_rfk_update_swstate_all(void)
1193 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++)
1194 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[i
]);
1198 * Sync the HW-blocking state of all rfkill switches,
1199 * do notice it causes the rfkill core to schedule uevents
1201 static void tpacpi_rfk_update_hwblock_state(bool blocked
)
1204 struct tpacpi_rfk
*tp_rfk
;
1206 for (i
= 0; i
< TPACPI_RFK_SW_MAX
; i
++) {
1207 tp_rfk
= tpacpi_rfkill_switches
[i
];
1209 if (rfkill_set_hw_state(tp_rfk
->rfkill
,
1211 /* ignore -- we track sw block */
1217 /* Call to get the WLSW state from the firmware */
1218 static int hotkey_get_wlsw(void);
1220 /* Call to query WLSW state and update all rfkill switches */
1221 static bool tpacpi_rfk_check_hwblock_state(void)
1223 int res
= hotkey_get_wlsw();
1226 /* When unknown or unsupported, we have to assume it is unblocked */
1230 hw_blocked
= (res
== TPACPI_RFK_RADIO_OFF
);
1231 tpacpi_rfk_update_hwblock_state(hw_blocked
);
1236 static int tpacpi_rfk_hook_set_block(void *data
, bool blocked
)
1238 struct tpacpi_rfk
*tp_rfk
= data
;
1241 dbg_printk(TPACPI_DBG_RFKILL
,
1242 "request to change radio state to %s\n",
1243 blocked
? "blocked" : "unblocked");
1245 /* try to set radio state */
1246 res
= (tp_rfk
->ops
->set_status
)(blocked
?
1247 TPACPI_RFK_RADIO_OFF
: TPACPI_RFK_RADIO_ON
);
1249 /* and update the rfkill core with whatever the FW really did */
1250 tpacpi_rfk_update_swstate(tp_rfk
);
1252 return (res
< 0) ? res
: 0;
1255 static const struct rfkill_ops tpacpi_rfk_rfkill_ops
= {
1256 .set_block
= tpacpi_rfk_hook_set_block
,
1259 static int __init
tpacpi_new_rfkill(const enum tpacpi_rfk_id id
,
1260 const struct tpacpi_rfk_ops
*tp_rfkops
,
1261 const enum rfkill_type rfktype
,
1263 const bool set_default
)
1265 struct tpacpi_rfk
*atp_rfk
;
1267 bool sw_state
= false;
1271 BUG_ON(id
>= TPACPI_RFK_SW_MAX
|| tpacpi_rfkill_switches
[id
]);
1273 atp_rfk
= kzalloc(sizeof(struct tpacpi_rfk
), GFP_KERNEL
);
1275 atp_rfk
->rfkill
= rfkill_alloc(name
,
1278 &tpacpi_rfk_rfkill_ops
,
1280 if (!atp_rfk
|| !atp_rfk
->rfkill
) {
1282 "failed to allocate memory for rfkill class\n");
1288 atp_rfk
->ops
= tp_rfkops
;
1290 sw_status
= (tp_rfkops
->get_status
)();
1291 if (sw_status
< 0) {
1293 "failed to read initial state for %s, error %d\n",
1296 sw_state
= (sw_status
== TPACPI_RFK_RADIO_OFF
);
1298 /* try to keep the initial state, since we ask the
1299 * firmware to preserve it across S5 in NVRAM */
1300 rfkill_init_sw_state(atp_rfk
->rfkill
, sw_state
);
1303 hw_state
= tpacpi_rfk_check_hwblock_state();
1304 rfkill_set_hw_state(atp_rfk
->rfkill
, hw_state
);
1306 res
= rfkill_register(atp_rfk
->rfkill
);
1309 "failed to register %s rfkill switch: %d\n",
1311 rfkill_destroy(atp_rfk
->rfkill
);
1316 tpacpi_rfkill_switches
[id
] = atp_rfk
;
1318 printk(TPACPI_INFO
"rfkill switch %s: radio is %sblocked\n",
1319 name
, (sw_state
|| hw_state
) ? "" : "un");
1323 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id
)
1325 struct tpacpi_rfk
*tp_rfk
;
1327 BUG_ON(id
>= TPACPI_RFK_SW_MAX
);
1329 tp_rfk
= tpacpi_rfkill_switches
[id
];
1331 rfkill_unregister(tp_rfk
->rfkill
);
1332 rfkill_destroy(tp_rfk
->rfkill
);
1333 tpacpi_rfkill_switches
[id
] = NULL
;
1338 static void printk_deprecated_rfkill_attribute(const char * const what
)
1340 printk_deprecated_attribute(what
,
1341 "Please switch to generic rfkill before year 2010");
1344 /* sysfs <radio> enable ------------------------------------------------ */
1345 static ssize_t
tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id
,
1346 struct device_attribute
*attr
,
1351 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1353 /* This is in the ABI... */
1354 if (tpacpi_rfk_check_hwblock_state()) {
1355 status
= TPACPI_RFK_RADIO_OFF
;
1357 status
= tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1362 return snprintf(buf
, PAGE_SIZE
, "%d\n",
1363 (status
== TPACPI_RFK_RADIO_ON
) ? 1 : 0);
1366 static ssize_t
tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id
,
1367 struct device_attribute
*attr
,
1368 const char *buf
, size_t count
)
1373 printk_deprecated_rfkill_attribute(attr
->attr
.name
);
1375 if (parse_strtoul(buf
, 1, &t
))
1378 tpacpi_disclose_usertask(attr
->attr
.name
, "set to %ld\n", t
);
1380 /* This is in the ABI... */
1381 if (tpacpi_rfk_check_hwblock_state() && !!t
)
1384 res
= tpacpi_rfkill_switches
[id
]->ops
->set_status((!!t
) ?
1385 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
);
1386 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1388 return (res
< 0) ? res
: count
;
1391 /* procfs -------------------------------------------------------------- */
1392 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id
, char *p
)
1396 if (id
>= TPACPI_RFK_SW_MAX
)
1397 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
1401 /* This is in the ABI... */
1402 if (tpacpi_rfk_check_hwblock_state()) {
1403 status
= TPACPI_RFK_RADIO_OFF
;
1405 status
= tpacpi_rfk_update_swstate(
1406 tpacpi_rfkill_switches
[id
]);
1411 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
1412 (status
== TPACPI_RFK_RADIO_ON
) ?
1413 "enabled" : "disabled");
1414 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n");
1420 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id
, char *buf
)
1426 if (id
>= TPACPI_RFK_SW_MAX
)
1429 while ((cmd
= next_cmd(&buf
))) {
1430 if (strlencmp(cmd
, "enable") == 0)
1431 status
= TPACPI_RFK_RADIO_ON
;
1432 else if (strlencmp(cmd
, "disable") == 0)
1433 status
= TPACPI_RFK_RADIO_OFF
;
1439 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1440 (status
== TPACPI_RFK_RADIO_ON
) ?
1441 "enable" : "disable",
1442 tpacpi_rfkill_names
[id
]);
1443 res
= (tpacpi_rfkill_switches
[id
]->ops
->set_status
)(status
);
1444 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches
[id
]);
1450 /*************************************************************************
1451 * thinkpad-acpi driver attributes
1454 /* interface_version --------------------------------------------------- */
1455 static ssize_t
tpacpi_driver_interface_version_show(
1456 struct device_driver
*drv
,
1459 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", TPACPI_SYSFS_VERSION
);
1462 static DRIVER_ATTR(interface_version
, S_IRUGO
,
1463 tpacpi_driver_interface_version_show
, NULL
);
1465 /* debug_level --------------------------------------------------------- */
1466 static ssize_t
tpacpi_driver_debug_show(struct device_driver
*drv
,
1469 return snprintf(buf
, PAGE_SIZE
, "0x%04x\n", dbg_level
);
1472 static ssize_t
tpacpi_driver_debug_store(struct device_driver
*drv
,
1473 const char *buf
, size_t count
)
1477 if (parse_strtoul(buf
, 0xffff, &t
))
1485 static DRIVER_ATTR(debug_level
, S_IWUSR
| S_IRUGO
,
1486 tpacpi_driver_debug_show
, tpacpi_driver_debug_store
);
1488 /* version ------------------------------------------------------------- */
1489 static ssize_t
tpacpi_driver_version_show(struct device_driver
*drv
,
1492 return snprintf(buf
, PAGE_SIZE
, "%s v%s\n",
1493 TPACPI_DESC
, TPACPI_VERSION
);
1496 static DRIVER_ATTR(version
, S_IRUGO
,
1497 tpacpi_driver_version_show
, NULL
);
1499 /* --------------------------------------------------------------------- */
1501 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1503 /* wlsw_emulstate ------------------------------------------------------ */
1504 static ssize_t
tpacpi_driver_wlsw_emulstate_show(struct device_driver
*drv
,
1507 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wlsw_emulstate
);
1510 static ssize_t
tpacpi_driver_wlsw_emulstate_store(struct device_driver
*drv
,
1511 const char *buf
, size_t count
)
1515 if (parse_strtoul(buf
, 1, &t
))
1518 if (tpacpi_wlsw_emulstate
!= !!t
) {
1519 tpacpi_wlsw_emulstate
= !!t
;
1520 tpacpi_rfk_update_hwblock_state(!t
); /* negative logic */
1526 static DRIVER_ATTR(wlsw_emulstate
, S_IWUSR
| S_IRUGO
,
1527 tpacpi_driver_wlsw_emulstate_show
,
1528 tpacpi_driver_wlsw_emulstate_store
);
1530 /* bluetooth_emulstate ------------------------------------------------- */
1531 static ssize_t
tpacpi_driver_bluetooth_emulstate_show(
1532 struct device_driver
*drv
,
1535 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_bluetooth_emulstate
);
1538 static ssize_t
tpacpi_driver_bluetooth_emulstate_store(
1539 struct device_driver
*drv
,
1540 const char *buf
, size_t count
)
1544 if (parse_strtoul(buf
, 1, &t
))
1547 tpacpi_bluetooth_emulstate
= !!t
;
1552 static DRIVER_ATTR(bluetooth_emulstate
, S_IWUSR
| S_IRUGO
,
1553 tpacpi_driver_bluetooth_emulstate_show
,
1554 tpacpi_driver_bluetooth_emulstate_store
);
1556 /* wwan_emulstate ------------------------------------------------- */
1557 static ssize_t
tpacpi_driver_wwan_emulstate_show(
1558 struct device_driver
*drv
,
1561 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_wwan_emulstate
);
1564 static ssize_t
tpacpi_driver_wwan_emulstate_store(
1565 struct device_driver
*drv
,
1566 const char *buf
, size_t count
)
1570 if (parse_strtoul(buf
, 1, &t
))
1573 tpacpi_wwan_emulstate
= !!t
;
1578 static DRIVER_ATTR(wwan_emulstate
, S_IWUSR
| S_IRUGO
,
1579 tpacpi_driver_wwan_emulstate_show
,
1580 tpacpi_driver_wwan_emulstate_store
);
1582 /* uwb_emulstate ------------------------------------------------- */
1583 static ssize_t
tpacpi_driver_uwb_emulstate_show(
1584 struct device_driver
*drv
,
1587 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!tpacpi_uwb_emulstate
);
1590 static ssize_t
tpacpi_driver_uwb_emulstate_store(
1591 struct device_driver
*drv
,
1592 const char *buf
, size_t count
)
1596 if (parse_strtoul(buf
, 1, &t
))
1599 tpacpi_uwb_emulstate
= !!t
;
1604 static DRIVER_ATTR(uwb_emulstate
, S_IWUSR
| S_IRUGO
,
1605 tpacpi_driver_uwb_emulstate_show
,
1606 tpacpi_driver_uwb_emulstate_store
);
1609 /* --------------------------------------------------------------------- */
1611 static struct driver_attribute
*tpacpi_driver_attributes
[] = {
1612 &driver_attr_debug_level
, &driver_attr_version
,
1613 &driver_attr_interface_version
,
1616 static int __init
tpacpi_create_driver_attributes(struct device_driver
*drv
)
1622 while (!res
&& i
< ARRAY_SIZE(tpacpi_driver_attributes
)) {
1623 res
= driver_create_file(drv
, tpacpi_driver_attributes
[i
]);
1627 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1628 if (!res
&& dbg_wlswemul
)
1629 res
= driver_create_file(drv
, &driver_attr_wlsw_emulstate
);
1630 if (!res
&& dbg_bluetoothemul
)
1631 res
= driver_create_file(drv
, &driver_attr_bluetooth_emulstate
);
1632 if (!res
&& dbg_wwanemul
)
1633 res
= driver_create_file(drv
, &driver_attr_wwan_emulstate
);
1634 if (!res
&& dbg_uwbemul
)
1635 res
= driver_create_file(drv
, &driver_attr_uwb_emulstate
);
1641 static void tpacpi_remove_driver_attributes(struct device_driver
*drv
)
1645 for (i
= 0; i
< ARRAY_SIZE(tpacpi_driver_attributes
); i
++)
1646 driver_remove_file(drv
, tpacpi_driver_attributes
[i
]);
1648 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1649 driver_remove_file(drv
, &driver_attr_wlsw_emulstate
);
1650 driver_remove_file(drv
, &driver_attr_bluetooth_emulstate
);
1651 driver_remove_file(drv
, &driver_attr_wwan_emulstate
);
1652 driver_remove_file(drv
, &driver_attr_uwb_emulstate
);
1656 /*************************************************************************
1661 * Table of recommended minimum BIOS versions
1663 * Reasons for listing:
1664 * 1. Stable BIOS, listed because the unknown ammount of
1665 * bugs and bad ACPI behaviour on older versions
1667 * 2. BIOS or EC fw with known bugs that trigger on Linux
1669 * 3. BIOS with known reduced functionality in older versions
1671 * We recommend the latest BIOS and EC version.
1672 * We only support the latest BIOS and EC fw version as a rule.
1674 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1675 * Information from users in ThinkWiki
1677 * WARNING: we use this table also to detect that the machine is
1678 * a ThinkPad in some cases, so don't remove entries lightly.
1681 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1682 { .vendor = (__v), \
1683 .bios = TPID(__id1, __id2), \
1684 .ec = TPACPI_MATCH_ANY, \
1685 .quirks = TPACPI_MATCH_ANY << 16 \
1686 | (__bv1) << 8 | (__bv2) }
1688 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1689 __eid, __ev1, __ev2) \
1690 { .vendor = (__v), \
1691 .bios = TPID(__bid1, __bid2), \
1693 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1694 | (__bv1) << 8 | (__bv2) }
1696 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1697 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1699 /* Outdated IBM BIOSes often lack the EC id string */
1700 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1701 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1702 __bv1, __bv2, TPID(__id1, __id2), \
1704 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1705 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1708 /* Outdated IBM BIOSes often lack the EC id string */
1709 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1710 __eid1, __eid2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1712 __bv1, __bv2, TPID(__eid1, __eid2), \
1714 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1715 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1718 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1719 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1721 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1722 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1723 __bv1, __bv2, TPID(__id1, __id2), \
1726 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1727 __eid1, __eid2, __ev1, __ev2) \
1728 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1729 __bv1, __bv2, TPID(__eid1, __eid2), \
1732 static const struct tpacpi_quirk tpacpi_bios_version_qtable
[] __initconst
= {
1733 /* Numeric models ------------------ */
1734 /* FW MODEL BIOS VERS */
1735 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1736 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1737 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1738 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1739 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1740 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1741 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1742 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1743 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1745 /* A-series ------------------------- */
1746 /* FW MODEL BIOS VERS EC VERS */
1747 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1748 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1749 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1750 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1751 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1752 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1753 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1754 TPV_QI0('1', '3', '2', '0'), /* A22m */
1755 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1756 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1757 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1759 /* G-series ------------------------- */
1760 /* FW MODEL BIOS VERS */
1761 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1762 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1764 /* R-series, T-series --------------- */
1765 /* FW MODEL BIOS VERS EC VERS */
1766 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1767 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1768 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1769 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1770 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1771 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1772 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1773 T40/p, T41/p, T42/p (1) */
1774 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1775 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1776 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1777 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1779 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1780 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1781 TPV_QI0('1', '6', '3', '2'), /* T22 */
1782 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1783 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1784 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1786 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1787 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1788 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1790 /* BIOS FW BIOS VERS EC FW EC VERS */
1791 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1792 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1794 /* X-series ------------------------- */
1795 /* FW MODEL BIOS VERS EC VERS */
1796 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1797 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1798 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1799 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1800 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1801 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1802 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1804 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1805 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1807 /* (0) - older versions lack DMI EC fw string and functionality */
1808 /* (1) - older versions known to lack functionality */
1819 static void __init
tpacpi_check_outdated_fw(void)
1821 unsigned long fwvers
;
1822 u16 ec_version
, bios_version
;
1824 fwvers
= tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1825 ARRAY_SIZE(tpacpi_bios_version_qtable
));
1830 bios_version
= fwvers
& 0xffffU
;
1831 ec_version
= (fwvers
>> 16) & 0xffffU
;
1833 /* note that unknown versions are set to 0x0000 and we use that */
1834 if ((bios_version
> thinkpad_id
.bios_release
) ||
1835 (ec_version
> thinkpad_id
.ec_release
&&
1836 ec_version
!= TPACPI_MATCH_ANY
)) {
1838 * The changelogs would let us track down the exact
1839 * reason, but it is just too much of a pain to track
1840 * it. We only list BIOSes that are either really
1841 * broken, or really stable to begin with, so it is
1842 * best if the user upgrades the firmware anyway.
1845 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1847 "WARNING: This firmware may be missing critical bug "
1848 "fixes and/or important features\n");
1852 static bool __init
tpacpi_is_fw_known(void)
1854 return tpacpi_check_quirks(tpacpi_bios_version_qtable
,
1855 ARRAY_SIZE(tpacpi_bios_version_qtable
)) != 0;
1858 /****************************************************************************
1859 ****************************************************************************
1863 ****************************************************************************
1864 ****************************************************************************/
1866 /*************************************************************************
1867 * thinkpad-acpi init subdriver
1870 static int __init
thinkpad_acpi_driver_init(struct ibm_init_struct
*iibm
)
1872 printk(TPACPI_INFO
"%s v%s\n", TPACPI_DESC
, TPACPI_VERSION
);
1873 printk(TPACPI_INFO
"%s\n", TPACPI_URL
);
1875 printk(TPACPI_INFO
"ThinkPad BIOS %s, EC %s\n",
1876 (thinkpad_id
.bios_version_str
) ?
1877 thinkpad_id
.bios_version_str
: "unknown",
1878 (thinkpad_id
.ec_version_str
) ?
1879 thinkpad_id
.ec_version_str
: "unknown");
1881 if (thinkpad_id
.vendor
&& thinkpad_id
.model_str
)
1882 printk(TPACPI_INFO
"%s %s, model %s\n",
1883 (thinkpad_id
.vendor
== PCI_VENDOR_ID_IBM
) ?
1884 "IBM" : ((thinkpad_id
.vendor
==
1885 PCI_VENDOR_ID_LENOVO
) ?
1886 "Lenovo" : "Unknown vendor"),
1887 thinkpad_id
.model_str
,
1888 (thinkpad_id
.nummodel_str
) ?
1889 thinkpad_id
.nummodel_str
: "unknown");
1891 tpacpi_check_outdated_fw();
1895 static int thinkpad_acpi_driver_read(char *p
)
1899 len
+= sprintf(p
+ len
, "driver:\t\t%s\n", TPACPI_DESC
);
1900 len
+= sprintf(p
+ len
, "version:\t%s\n", TPACPI_VERSION
);
1905 static struct ibm_struct thinkpad_acpi_driver_data
= {
1907 .read
= thinkpad_acpi_driver_read
,
1910 /*************************************************************************
1915 * ThinkPad firmware event model
1917 * The ThinkPad firmware has two main event interfaces: normal ACPI
1918 * notifications (which follow the ACPI standard), and a private event
1921 * The private event interface also issues events for the hotkeys. As
1922 * the driver gained features, the event handling code ended up being
1923 * built around the hotkey subdriver. This will need to be refactored
1924 * to a more formal event API eventually.
1926 * Some "hotkeys" are actually supposed to be used as event reports,
1927 * such as "brightness has changed", "volume has changed", depending on
1928 * the ThinkPad model and how the firmware is operating.
1930 * Unlike other classes, hotkey-class events have mask/unmask control on
1931 * non-ancient firmware. However, how it behaves changes a lot with the
1932 * firmware model and version.
1935 enum { /* hot key scan codes (derived from ACPI DSDT) */
1936 TP_ACPI_HOTKEYSCAN_FNF1
= 0,
1937 TP_ACPI_HOTKEYSCAN_FNF2
,
1938 TP_ACPI_HOTKEYSCAN_FNF3
,
1939 TP_ACPI_HOTKEYSCAN_FNF4
,
1940 TP_ACPI_HOTKEYSCAN_FNF5
,
1941 TP_ACPI_HOTKEYSCAN_FNF6
,
1942 TP_ACPI_HOTKEYSCAN_FNF7
,
1943 TP_ACPI_HOTKEYSCAN_FNF8
,
1944 TP_ACPI_HOTKEYSCAN_FNF9
,
1945 TP_ACPI_HOTKEYSCAN_FNF10
,
1946 TP_ACPI_HOTKEYSCAN_FNF11
,
1947 TP_ACPI_HOTKEYSCAN_FNF12
,
1948 TP_ACPI_HOTKEYSCAN_FNBACKSPACE
,
1949 TP_ACPI_HOTKEYSCAN_FNINSERT
,
1950 TP_ACPI_HOTKEYSCAN_FNDELETE
,
1951 TP_ACPI_HOTKEYSCAN_FNHOME
,
1952 TP_ACPI_HOTKEYSCAN_FNEND
,
1953 TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1954 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN
,
1955 TP_ACPI_HOTKEYSCAN_FNSPACE
,
1956 TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1957 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1958 TP_ACPI_HOTKEYSCAN_MUTE
,
1959 TP_ACPI_HOTKEYSCAN_THINKPAD
,
1962 enum { /* Keys/events available through NVRAM polling */
1963 TPACPI_HKEY_NVRAM_KNOWN_MASK
= 0x00fb88c0U
,
1964 TPACPI_HKEY_NVRAM_GOOD_MASK
= 0x00fb8000U
,
1967 enum { /* Positions of some of the keys in hotkey masks */
1968 TP_ACPI_HKEY_DISPSWTCH_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF7
,
1969 TP_ACPI_HKEY_DISPXPAND_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF8
,
1970 TP_ACPI_HKEY_HIBERNATE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNF12
,
1971 TP_ACPI_HKEY_BRGHTUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNHOME
,
1972 TP_ACPI_HKEY_BRGHTDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNEND
,
1973 TP_ACPI_HKEY_THNKLGHT_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP
,
1974 TP_ACPI_HKEY_ZOOM_MASK
= 1 << TP_ACPI_HOTKEYSCAN_FNSPACE
,
1975 TP_ACPI_HKEY_VOLUP_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP
,
1976 TP_ACPI_HKEY_VOLDWN_MASK
= 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
,
1977 TP_ACPI_HKEY_MUTE_MASK
= 1 << TP_ACPI_HOTKEYSCAN_MUTE
,
1978 TP_ACPI_HKEY_THINKPAD_MASK
= 1 << TP_ACPI_HOTKEYSCAN_THINKPAD
,
1981 enum { /* NVRAM to ACPI HKEY group map */
1982 TP_NVRAM_HKEY_GROUP_HK2
= TP_ACPI_HKEY_THINKPAD_MASK
|
1983 TP_ACPI_HKEY_ZOOM_MASK
|
1984 TP_ACPI_HKEY_DISPSWTCH_MASK
|
1985 TP_ACPI_HKEY_HIBERNATE_MASK
,
1986 TP_NVRAM_HKEY_GROUP_BRIGHTNESS
= TP_ACPI_HKEY_BRGHTUP_MASK
|
1987 TP_ACPI_HKEY_BRGHTDWN_MASK
,
1988 TP_NVRAM_HKEY_GROUP_VOLUME
= TP_ACPI_HKEY_VOLUP_MASK
|
1989 TP_ACPI_HKEY_VOLDWN_MASK
|
1990 TP_ACPI_HKEY_MUTE_MASK
,
1993 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1994 struct tp_nvram_state
{
1995 u16 thinkpad_toggle
:1;
1997 u16 display_toggle
:1;
1998 u16 thinklight_toggle
:1;
1999 u16 hibernate_toggle
:1;
2000 u16 displayexp_toggle
:1;
2001 u16 display_state
:1;
2002 u16 brightness_toggle
:1;
2003 u16 volume_toggle
:1;
2006 u8 brightness_level
;
2010 /* kthread for the hotkey poller */
2011 static struct task_struct
*tpacpi_hotkey_task
;
2013 /* Acquired while the poller kthread is running, use to sync start/stop */
2014 static struct mutex hotkey_thread_mutex
;
2017 * Acquire mutex to write poller control variables as an
2020 * Increment hotkey_config_change when changing them if you
2021 * want the kthread to forget old state.
2023 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2025 static struct mutex hotkey_thread_data_mutex
;
2026 static unsigned int hotkey_config_change
;
2029 * hotkey poller control variables
2031 * Must be atomic or readers will also need to acquire mutex
2033 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2034 * should be used only when the changes need to be taken as
2035 * a block, OR when one needs to force the kthread to forget
2038 static u32 hotkey_source_mask
; /* bit mask 0=ACPI,1=NVRAM */
2039 static unsigned int hotkey_poll_freq
= 10; /* Hz */
2041 #define HOTKEY_CONFIG_CRITICAL_START \
2043 mutex_lock(&hotkey_thread_data_mutex); \
2044 hotkey_config_change++; \
2046 #define HOTKEY_CONFIG_CRITICAL_END \
2047 mutex_unlock(&hotkey_thread_data_mutex);
2049 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2051 #define hotkey_source_mask 0U
2052 #define HOTKEY_CONFIG_CRITICAL_START
2053 #define HOTKEY_CONFIG_CRITICAL_END
2055 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2057 static struct mutex hotkey_mutex
;
2059 static enum { /* Reasons for waking up */
2060 TP_ACPI_WAKEUP_NONE
= 0, /* None or unknown */
2061 TP_ACPI_WAKEUP_BAYEJ
, /* Bay ejection request */
2062 TP_ACPI_WAKEUP_UNDOCK
, /* Undock request */
2063 } hotkey_wakeup_reason
;
2065 static int hotkey_autosleep_ack
;
2067 static u32 hotkey_orig_mask
; /* events the BIOS had enabled */
2068 static u32 hotkey_all_mask
; /* all events supported in fw */
2069 static u32 hotkey_reserved_mask
; /* events better left disabled */
2070 static u32 hotkey_driver_mask
; /* events needed by the driver */
2071 static u32 hotkey_user_mask
; /* events visible to userspace */
2072 static u32 hotkey_acpi_mask
; /* events enabled in firmware */
2074 static unsigned int hotkey_report_mode
;
2076 static u16
*hotkey_keycode_map
;
2078 static struct attribute_set
*hotkey_dev_attributes
;
2080 static void tpacpi_driver_event(const unsigned int hkey_event
);
2081 static void hotkey_driver_event(const unsigned int scancode
);
2083 /* HKEY.MHKG() return bits */
2084 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2086 static int hotkey_get_wlsw(void)
2090 if (!tp_features
.hotkey_wlsw
)
2093 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2095 return (tpacpi_wlsw_emulstate
) ?
2096 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2099 if (!acpi_evalf(hkey_handle
, &status
, "WLSW", "d"))
2102 return (status
) ? TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
2105 static int hotkey_get_tablet_mode(int *status
)
2109 if (!acpi_evalf(hkey_handle
, &s
, "MHKG", "d"))
2112 *status
= ((s
& TP_HOTKEY_TABLET_MASK
) != 0);
2117 * Reads current event mask from firmware, and updates
2118 * hotkey_acpi_mask accordingly. Also resets any bits
2119 * from hotkey_user_mask that are unavailable to be
2120 * delivered (shadow requirement of the userspace ABI).
2122 * Call with hotkey_mutex held
2124 static int hotkey_mask_get(void)
2126 if (tp_features
.hotkey_mask
) {
2129 if (!acpi_evalf(hkey_handle
, &m
, "DHKN", "d"))
2132 hotkey_acpi_mask
= m
;
2134 /* no mask support doesn't mean no event support... */
2135 hotkey_acpi_mask
= hotkey_all_mask
;
2138 /* sync userspace-visible mask */
2139 hotkey_user_mask
&= (hotkey_acpi_mask
| hotkey_source_mask
);
2144 void static hotkey_mask_warn_incomplete_mask(void)
2146 /* log only what the user can fix... */
2147 const u32 wantedmask
= hotkey_driver_mask
&
2148 ~(hotkey_acpi_mask
| hotkey_source_mask
) &
2149 (hotkey_all_mask
| TPACPI_HKEY_NVRAM_KNOWN_MASK
);
2152 printk(TPACPI_NOTICE
2153 "required events 0x%08x not enabled!\n",
2158 * Set the firmware mask when supported
2160 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2162 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2164 * Call with hotkey_mutex held
2166 static int hotkey_mask_set(u32 mask
)
2171 const u32 fwmask
= mask
& ~hotkey_source_mask
;
2173 if (tp_features
.hotkey_mask
) {
2174 for (i
= 0; i
< 32; i
++) {
2175 if (!acpi_evalf(hkey_handle
,
2176 NULL
, "MHKM", "vdd", i
+ 1,
2177 !!(mask
& (1 << i
)))) {
2185 * We *must* make an inconditional call to hotkey_mask_get to
2186 * refresh hotkey_acpi_mask and update hotkey_user_mask
2188 * Take the opportunity to also log when we cannot _enable_
2191 if (!hotkey_mask_get() && !rc
&& (fwmask
& ~hotkey_acpi_mask
)) {
2192 printk(TPACPI_NOTICE
2193 "asked for hotkey mask 0x%08x, but "
2194 "firmware forced it to 0x%08x\n",
2195 fwmask
, hotkey_acpi_mask
);
2198 if (tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
)
2199 hotkey_mask_warn_incomplete_mask();
2205 * Sets hotkey_user_mask and tries to set the firmware mask
2207 * Call with hotkey_mutex held
2209 static int hotkey_user_mask_set(const u32 mask
)
2213 /* Give people a chance to notice they are doing something that
2214 * is bound to go boom on their users sooner or later */
2215 if (!tp_warned
.hotkey_mask_ff
&&
2216 (mask
== 0xffff || mask
== 0xffffff ||
2217 mask
== 0xffffffff)) {
2218 tp_warned
.hotkey_mask_ff
= 1;
2219 printk(TPACPI_NOTICE
2220 "setting the hotkey mask to 0x%08x is likely "
2221 "not the best way to go about it\n", mask
);
2222 printk(TPACPI_NOTICE
2223 "please consider using the driver defaults, "
2224 "and refer to up-to-date thinkpad-acpi "
2228 /* Try to enable what the user asked for, plus whatever we need.
2229 * this syncs everything but won't enable bits in hotkey_user_mask */
2230 rc
= hotkey_mask_set((mask
| hotkey_driver_mask
) & ~hotkey_source_mask
);
2232 /* Enable the available bits in hotkey_user_mask */
2233 hotkey_user_mask
= mask
& (hotkey_acpi_mask
| hotkey_source_mask
);
2239 * Sets the driver hotkey mask.
2241 * Can be called even if the hotkey subdriver is inactive
2243 static int tpacpi_hotkey_driver_mask_set(const u32 mask
)
2247 /* Do the right thing if hotkey_init has not been called yet */
2248 if (!tp_features
.hotkey
) {
2249 hotkey_driver_mask
= mask
;
2253 mutex_lock(&hotkey_mutex
);
2255 HOTKEY_CONFIG_CRITICAL_START
2256 hotkey_driver_mask
= mask
;
2257 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2258 hotkey_source_mask
|= (mask
& ~hotkey_all_mask
);
2260 HOTKEY_CONFIG_CRITICAL_END
2262 rc
= hotkey_mask_set((hotkey_acpi_mask
| hotkey_driver_mask
) &
2263 ~hotkey_source_mask
);
2264 mutex_unlock(&hotkey_mutex
);
2269 static int hotkey_status_get(int *status
)
2271 if (!acpi_evalf(hkey_handle
, status
, "DHKC", "d"))
2277 static int hotkey_status_set(bool enable
)
2279 if (!acpi_evalf(hkey_handle
, NULL
, "MHKC", "vd", enable
? 1 : 0))
2285 static void tpacpi_input_send_tabletsw(void)
2289 if (tp_features
.hotkey_tablet
&&
2290 !hotkey_get_tablet_mode(&state
)) {
2291 mutex_lock(&tpacpi_inputdev_send_mutex
);
2293 input_report_switch(tpacpi_inputdev
,
2294 SW_TABLET_MODE
, !!state
);
2295 input_sync(tpacpi_inputdev
);
2297 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2301 /* Do NOT call without validating scancode first */
2302 static void tpacpi_input_send_key(const unsigned int scancode
)
2304 const unsigned int keycode
= hotkey_keycode_map
[scancode
];
2306 if (keycode
!= KEY_RESERVED
) {
2307 mutex_lock(&tpacpi_inputdev_send_mutex
);
2309 input_report_key(tpacpi_inputdev
, keycode
, 1);
2310 if (keycode
== KEY_UNKNOWN
)
2311 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
2313 input_sync(tpacpi_inputdev
);
2315 input_report_key(tpacpi_inputdev
, keycode
, 0);
2316 if (keycode
== KEY_UNKNOWN
)
2317 input_event(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
,
2319 input_sync(tpacpi_inputdev
);
2321 mutex_unlock(&tpacpi_inputdev_send_mutex
);
2325 /* Do NOT call without validating scancode first */
2326 static void tpacpi_input_send_key_masked(const unsigned int scancode
)
2328 hotkey_driver_event(scancode
);
2329 if (hotkey_user_mask
& (1 << scancode
))
2330 tpacpi_input_send_key(scancode
);
2333 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2334 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
;
2336 /* Do NOT call without validating scancode first */
2337 static void tpacpi_hotkey_send_key(unsigned int scancode
)
2339 tpacpi_input_send_key_masked(scancode
);
2340 if (hotkey_report_mode
< 2) {
2341 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver
.device
,
2342 0x80, TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
2346 static void hotkey_read_nvram(struct tp_nvram_state
*n
, const u32 m
)
2350 if (m
& TP_NVRAM_HKEY_GROUP_HK2
) {
2351 d
= nvram_read_byte(TP_NVRAM_ADDR_HK2
);
2352 n
->thinkpad_toggle
= !!(d
& TP_NVRAM_MASK_HKT_THINKPAD
);
2353 n
->zoom_toggle
= !!(d
& TP_NVRAM_MASK_HKT_ZOOM
);
2354 n
->display_toggle
= !!(d
& TP_NVRAM_MASK_HKT_DISPLAY
);
2355 n
->hibernate_toggle
= !!(d
& TP_NVRAM_MASK_HKT_HIBERNATE
);
2357 if (m
& TP_ACPI_HKEY_THNKLGHT_MASK
) {
2358 d
= nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT
);
2359 n
->thinklight_toggle
= !!(d
& TP_NVRAM_MASK_THINKLIGHT
);
2361 if (m
& TP_ACPI_HKEY_DISPXPAND_MASK
) {
2362 d
= nvram_read_byte(TP_NVRAM_ADDR_VIDEO
);
2363 n
->displayexp_toggle
=
2364 !!(d
& TP_NVRAM_MASK_HKT_DISPEXPND
);
2366 if (m
& TP_NVRAM_HKEY_GROUP_BRIGHTNESS
) {
2367 d
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
2368 n
->brightness_level
= (d
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
2369 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
2370 n
->brightness_toggle
=
2371 !!(d
& TP_NVRAM_MASK_HKT_BRIGHTNESS
);
2373 if (m
& TP_NVRAM_HKEY_GROUP_VOLUME
) {
2374 d
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
2375 n
->volume_level
= (d
& TP_NVRAM_MASK_LEVEL_VOLUME
)
2376 >> TP_NVRAM_POS_LEVEL_VOLUME
;
2377 n
->mute
= !!(d
& TP_NVRAM_MASK_MUTE
);
2378 n
->volume_toggle
= !!(d
& TP_NVRAM_MASK_HKT_VOLUME
);
2382 static void hotkey_compare_and_issue_event(struct tp_nvram_state
*oldn
,
2383 struct tp_nvram_state
*newn
,
2384 const u32 event_mask
)
2387 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2389 if ((event_mask & (1 << __scancode)) && \
2390 oldn->__member != newn->__member) \
2391 tpacpi_hotkey_send_key(__scancode); \
2394 #define TPACPI_MAY_SEND_KEY(__scancode) \
2396 if (event_mask & (1 << __scancode)) \
2397 tpacpi_hotkey_send_key(__scancode); \
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD
, thinkpad_toggle
);
2401 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE
, zoom_toggle
);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7
, display_toggle
);
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12
, hibernate_toggle
);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP
, thinklight_toggle
);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8
, displayexp_toggle
);
2410 if (oldn
->volume_toggle
!= newn
->volume_toggle
) {
2411 if (oldn
->mute
!= newn
->mute
) {
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2414 if (oldn
->volume_level
> newn
->volume_level
) {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2416 } else if (oldn
->volume_level
< newn
->volume_level
) {
2417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2418 } else if (oldn
->mute
== newn
->mute
) {
2419 /* repeated key presses that didn't change state */
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE
);
2422 } else if (newn
->volume_level
!= 0) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP
);
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN
);
2430 /* handle brightness */
2431 if (oldn
->brightness_toggle
!= newn
->brightness_toggle
) {
2432 if (oldn
->brightness_level
< newn
->brightness_level
) {
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2434 } else if (oldn
->brightness_level
> newn
->brightness_level
) {
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2437 /* repeated key presses that didn't change state */
2438 if (newn
->brightness_level
!= 0) {
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME
);
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND
);
2446 #undef TPACPI_COMPARE_KEY
2447 #undef TPACPI_MAY_SEND_KEY
2453 * We track all events in hotkey_source_mask all the time, since
2454 * most of them are edge-based. We only issue those requested by
2455 * hotkey_user_mask or hotkey_driver_mask, though.
2457 static int hotkey_kthread(void *data
)
2459 struct tp_nvram_state s
[2];
2460 u32 poll_mask
, event_mask
;
2461 unsigned int si
, so
;
2463 unsigned int change_detector
, must_reset
;
2464 unsigned int poll_freq
;
2466 mutex_lock(&hotkey_thread_mutex
);
2468 if (tpacpi_lifecycle
== TPACPI_LIFE_EXITING
)
2477 /* Initial state for compares */
2478 mutex_lock(&hotkey_thread_data_mutex
);
2479 change_detector
= hotkey_config_change
;
2480 poll_mask
= hotkey_source_mask
;
2481 event_mask
= hotkey_source_mask
&
2482 (hotkey_driver_mask
| hotkey_user_mask
);
2483 poll_freq
= hotkey_poll_freq
;
2484 mutex_unlock(&hotkey_thread_data_mutex
);
2485 hotkey_read_nvram(&s
[so
], poll_mask
);
2487 while (!kthread_should_stop()) {
2489 if (likely(poll_freq
))
2492 t
= 100; /* should never happen... */
2494 t
= msleep_interruptible(t
);
2495 if (unlikely(kthread_should_stop()))
2497 must_reset
= try_to_freeze();
2498 if (t
> 0 && !must_reset
)
2501 mutex_lock(&hotkey_thread_data_mutex
);
2502 if (must_reset
|| hotkey_config_change
!= change_detector
) {
2503 /* forget old state on thaw or config change */
2506 change_detector
= hotkey_config_change
;
2508 poll_mask
= hotkey_source_mask
;
2509 event_mask
= hotkey_source_mask
&
2510 (hotkey_driver_mask
| hotkey_user_mask
);
2511 poll_freq
= hotkey_poll_freq
;
2512 mutex_unlock(&hotkey_thread_data_mutex
);
2514 if (likely(poll_mask
)) {
2515 hotkey_read_nvram(&s
[si
], poll_mask
);
2516 if (likely(si
!= so
)) {
2517 hotkey_compare_and_issue_event(&s
[so
], &s
[si
],
2527 mutex_unlock(&hotkey_thread_mutex
);
2531 /* call with hotkey_mutex held */
2532 static void hotkey_poll_stop_sync(void)
2534 if (tpacpi_hotkey_task
) {
2535 if (frozen(tpacpi_hotkey_task
) ||
2536 freezing(tpacpi_hotkey_task
))
2537 thaw_process(tpacpi_hotkey_task
);
2539 kthread_stop(tpacpi_hotkey_task
);
2540 tpacpi_hotkey_task
= NULL
;
2541 mutex_lock(&hotkey_thread_mutex
);
2542 /* at this point, the thread did exit */
2543 mutex_unlock(&hotkey_thread_mutex
);
2547 /* call with hotkey_mutex held */
2548 static void hotkey_poll_setup(bool may_warn
)
2550 const u32 poll_driver_mask
= hotkey_driver_mask
& hotkey_source_mask
;
2551 const u32 poll_user_mask
= hotkey_user_mask
& hotkey_source_mask
;
2553 if (hotkey_poll_freq
> 0 &&
2554 (poll_driver_mask
||
2555 (poll_user_mask
&& tpacpi_inputdev
->users
> 0))) {
2556 if (!tpacpi_hotkey_task
) {
2557 tpacpi_hotkey_task
= kthread_run(hotkey_kthread
,
2558 NULL
, TPACPI_NVRAM_KTHREAD_NAME
);
2559 if (IS_ERR(tpacpi_hotkey_task
)) {
2560 tpacpi_hotkey_task
= NULL
;
2562 "could not create kernel thread "
2563 "for hotkey polling\n");
2567 hotkey_poll_stop_sync();
2568 if (may_warn
&& (poll_driver_mask
|| poll_user_mask
) &&
2569 hotkey_poll_freq
== 0) {
2570 printk(TPACPI_NOTICE
2571 "hot keys 0x%08x and/or events 0x%08x "
2572 "require polling, which is currently "
2574 poll_user_mask
, poll_driver_mask
);
2579 static void hotkey_poll_setup_safe(bool may_warn
)
2581 mutex_lock(&hotkey_mutex
);
2582 hotkey_poll_setup(may_warn
);
2583 mutex_unlock(&hotkey_mutex
);
2586 /* call with hotkey_mutex held */
2587 static void hotkey_poll_set_freq(unsigned int freq
)
2590 hotkey_poll_stop_sync();
2592 hotkey_poll_freq
= freq
;
2595 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2597 static void hotkey_poll_setup_safe(bool __unused
)
2601 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603 static int hotkey_inputdev_open(struct input_dev
*dev
)
2605 switch (tpacpi_lifecycle
) {
2606 case TPACPI_LIFE_INIT
:
2608 * hotkey_init will call hotkey_poll_setup_safe
2609 * at the appropriate moment
2612 case TPACPI_LIFE_EXITING
:
2614 case TPACPI_LIFE_RUNNING
:
2615 hotkey_poll_setup_safe(false);
2619 /* Should only happen if tpacpi_lifecycle is corrupt */
2624 static void hotkey_inputdev_close(struct input_dev
*dev
)
2626 /* disable hotkey polling when possible */
2627 if (tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
&&
2628 !(hotkey_source_mask
& hotkey_driver_mask
))
2629 hotkey_poll_setup_safe(false);
2632 /* sysfs hotkey enable ------------------------------------------------- */
2633 static ssize_t
hotkey_enable_show(struct device
*dev
,
2634 struct device_attribute
*attr
,
2639 printk_deprecated_attribute("hotkey_enable",
2640 "Hotkey reporting is always enabled");
2642 res
= hotkey_status_get(&status
);
2646 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
2649 static ssize_t
hotkey_enable_store(struct device
*dev
,
2650 struct device_attribute
*attr
,
2651 const char *buf
, size_t count
)
2655 printk_deprecated_attribute("hotkey_enable",
2656 "Hotkeys can be disabled through hotkey_mask");
2658 if (parse_strtoul(buf
, 1, &t
))
2667 static struct device_attribute dev_attr_hotkey_enable
=
2668 __ATTR(hotkey_enable
, S_IWUSR
| S_IRUGO
,
2669 hotkey_enable_show
, hotkey_enable_store
);
2671 /* sysfs hotkey mask --------------------------------------------------- */
2672 static ssize_t
hotkey_mask_show(struct device
*dev
,
2673 struct device_attribute
*attr
,
2676 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_user_mask
);
2679 static ssize_t
hotkey_mask_store(struct device
*dev
,
2680 struct device_attribute
*attr
,
2681 const char *buf
, size_t count
)
2686 if (parse_strtoul(buf
, 0xffffffffUL
, &t
))
2689 if (mutex_lock_killable(&hotkey_mutex
))
2690 return -ERESTARTSYS
;
2692 res
= hotkey_user_mask_set(t
);
2694 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2695 hotkey_poll_setup(true);
2698 mutex_unlock(&hotkey_mutex
);
2700 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t
);
2702 return (res
) ? res
: count
;
2705 static struct device_attribute dev_attr_hotkey_mask
=
2706 __ATTR(hotkey_mask
, S_IWUSR
| S_IRUGO
,
2707 hotkey_mask_show
, hotkey_mask_store
);
2709 /* sysfs hotkey bios_enabled ------------------------------------------- */
2710 static ssize_t
hotkey_bios_enabled_show(struct device
*dev
,
2711 struct device_attribute
*attr
,
2714 return sprintf(buf
, "0\n");
2717 static struct device_attribute dev_attr_hotkey_bios_enabled
=
2718 __ATTR(hotkey_bios_enabled
, S_IRUGO
, hotkey_bios_enabled_show
, NULL
);
2720 /* sysfs hotkey bios_mask ---------------------------------------------- */
2721 static ssize_t
hotkey_bios_mask_show(struct device
*dev
,
2722 struct device_attribute
*attr
,
2725 printk_deprecated_attribute("hotkey_bios_mask",
2726 "This attribute is useless.");
2727 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_orig_mask
);
2730 static struct device_attribute dev_attr_hotkey_bios_mask
=
2731 __ATTR(hotkey_bios_mask
, S_IRUGO
, hotkey_bios_mask_show
, NULL
);
2733 /* sysfs hotkey all_mask ----------------------------------------------- */
2734 static ssize_t
hotkey_all_mask_show(struct device
*dev
,
2735 struct device_attribute
*attr
,
2738 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2739 hotkey_all_mask
| hotkey_source_mask
);
2742 static struct device_attribute dev_attr_hotkey_all_mask
=
2743 __ATTR(hotkey_all_mask
, S_IRUGO
, hotkey_all_mask_show
, NULL
);
2745 /* sysfs hotkey recommended_mask --------------------------------------- */
2746 static ssize_t
hotkey_recommended_mask_show(struct device
*dev
,
2747 struct device_attribute
*attr
,
2750 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n",
2751 (hotkey_all_mask
| hotkey_source_mask
)
2752 & ~hotkey_reserved_mask
);
2755 static struct device_attribute dev_attr_hotkey_recommended_mask
=
2756 __ATTR(hotkey_recommended_mask
, S_IRUGO
,
2757 hotkey_recommended_mask_show
, NULL
);
2759 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2761 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2762 static ssize_t
hotkey_source_mask_show(struct device
*dev
,
2763 struct device_attribute
*attr
,
2766 return snprintf(buf
, PAGE_SIZE
, "0x%08x\n", hotkey_source_mask
);
2769 static ssize_t
hotkey_source_mask_store(struct device
*dev
,
2770 struct device_attribute
*attr
,
2771 const char *buf
, size_t count
)
2777 if (parse_strtoul(buf
, 0xffffffffUL
, &t
) ||
2778 ((t
& ~TPACPI_HKEY_NVRAM_KNOWN_MASK
) != 0))
2781 if (mutex_lock_killable(&hotkey_mutex
))
2782 return -ERESTARTSYS
;
2784 HOTKEY_CONFIG_CRITICAL_START
2785 hotkey_source_mask
= t
;
2786 HOTKEY_CONFIG_CRITICAL_END
2788 rc
= hotkey_mask_set((hotkey_user_mask
| hotkey_driver_mask
) &
2789 ~hotkey_source_mask
);
2790 hotkey_poll_setup(true);
2792 /* check if events needed by the driver got disabled */
2793 r_ev
= hotkey_driver_mask
& ~(hotkey_acpi_mask
& hotkey_all_mask
)
2794 & ~hotkey_source_mask
& TPACPI_HKEY_NVRAM_KNOWN_MASK
;
2796 mutex_unlock(&hotkey_mutex
);
2799 printk(TPACPI_ERR
"hotkey_source_mask: failed to update the"
2800 "firmware event mask!\n");
2803 printk(TPACPI_NOTICE
"hotkey_source_mask: "
2804 "some important events were disabled: "
2807 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t
);
2809 return (rc
< 0) ? rc
: count
;
2812 static struct device_attribute dev_attr_hotkey_source_mask
=
2813 __ATTR(hotkey_source_mask
, S_IWUSR
| S_IRUGO
,
2814 hotkey_source_mask_show
, hotkey_source_mask_store
);
2816 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2817 static ssize_t
hotkey_poll_freq_show(struct device
*dev
,
2818 struct device_attribute
*attr
,
2821 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_poll_freq
);
2824 static ssize_t
hotkey_poll_freq_store(struct device
*dev
,
2825 struct device_attribute
*attr
,
2826 const char *buf
, size_t count
)
2830 if (parse_strtoul(buf
, 25, &t
))
2833 if (mutex_lock_killable(&hotkey_mutex
))
2834 return -ERESTARTSYS
;
2836 hotkey_poll_set_freq(t
);
2837 hotkey_poll_setup(true);
2839 mutex_unlock(&hotkey_mutex
);
2841 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t
);
2846 static struct device_attribute dev_attr_hotkey_poll_freq
=
2847 __ATTR(hotkey_poll_freq
, S_IWUSR
| S_IRUGO
,
2848 hotkey_poll_freq_show
, hotkey_poll_freq_store
);
2850 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2852 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2853 static ssize_t
hotkey_radio_sw_show(struct device
*dev
,
2854 struct device_attribute
*attr
,
2858 res
= hotkey_get_wlsw();
2862 /* Opportunistic update */
2863 tpacpi_rfk_update_hwblock_state((res
== TPACPI_RFK_RADIO_OFF
));
2865 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2866 (res
== TPACPI_RFK_RADIO_OFF
) ? 0 : 1);
2869 static struct device_attribute dev_attr_hotkey_radio_sw
=
2870 __ATTR(hotkey_radio_sw
, S_IRUGO
, hotkey_radio_sw_show
, NULL
);
2872 static void hotkey_radio_sw_notify_change(void)
2874 if (tp_features
.hotkey_wlsw
)
2875 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2879 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2880 static ssize_t
hotkey_tablet_mode_show(struct device
*dev
,
2881 struct device_attribute
*attr
,
2885 res
= hotkey_get_tablet_mode(&s
);
2889 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!s
);
2892 static struct device_attribute dev_attr_hotkey_tablet_mode
=
2893 __ATTR(hotkey_tablet_mode
, S_IRUGO
, hotkey_tablet_mode_show
, NULL
);
2895 static void hotkey_tablet_mode_notify_change(void)
2897 if (tp_features
.hotkey_tablet
)
2898 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2899 "hotkey_tablet_mode");
2902 /* sysfs hotkey report_mode -------------------------------------------- */
2903 static ssize_t
hotkey_report_mode_show(struct device
*dev
,
2904 struct device_attribute
*attr
,
2907 return snprintf(buf
, PAGE_SIZE
, "%d\n",
2908 (hotkey_report_mode
!= 0) ? hotkey_report_mode
: 1);
2911 static struct device_attribute dev_attr_hotkey_report_mode
=
2912 __ATTR(hotkey_report_mode
, S_IRUGO
, hotkey_report_mode_show
, NULL
);
2914 /* sysfs wakeup reason (pollable) -------------------------------------- */
2915 static ssize_t
hotkey_wakeup_reason_show(struct device
*dev
,
2916 struct device_attribute
*attr
,
2919 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_wakeup_reason
);
2922 static struct device_attribute dev_attr_hotkey_wakeup_reason
=
2923 __ATTR(wakeup_reason
, S_IRUGO
, hotkey_wakeup_reason_show
, NULL
);
2925 static void hotkey_wakeup_reason_notify_change(void)
2927 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2931 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2932 static ssize_t
hotkey_wakeup_hotunplug_complete_show(struct device
*dev
,
2933 struct device_attribute
*attr
,
2936 return snprintf(buf
, PAGE_SIZE
, "%d\n", hotkey_autosleep_ack
);
2939 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete
=
2940 __ATTR(wakeup_hotunplug_complete
, S_IRUGO
,
2941 hotkey_wakeup_hotunplug_complete_show
, NULL
);
2943 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2945 sysfs_notify(&tpacpi_pdev
->dev
.kobj
, NULL
,
2946 "wakeup_hotunplug_complete");
2949 /* --------------------------------------------------------------------- */
2951 static struct attribute
*hotkey_attributes
[] __initdata
= {
2952 &dev_attr_hotkey_enable
.attr
,
2953 &dev_attr_hotkey_bios_enabled
.attr
,
2954 &dev_attr_hotkey_bios_mask
.attr
,
2955 &dev_attr_hotkey_report_mode
.attr
,
2956 &dev_attr_hotkey_wakeup_reason
.attr
,
2957 &dev_attr_hotkey_wakeup_hotunplug_complete
.attr
,
2958 &dev_attr_hotkey_mask
.attr
,
2959 &dev_attr_hotkey_all_mask
.attr
,
2960 &dev_attr_hotkey_recommended_mask
.attr
,
2961 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2962 &dev_attr_hotkey_source_mask
.attr
,
2963 &dev_attr_hotkey_poll_freq
.attr
,
2968 * Sync both the hw and sw blocking state of all switches
2970 static void tpacpi_send_radiosw_update(void)
2975 * We must sync all rfkill controllers *before* issuing any
2976 * rfkill input events, or we will race the rfkill core input
2979 * tpacpi_inputdev_send_mutex works as a syncronization point
2982 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2985 wlsw
= hotkey_get_wlsw();
2987 /* Sync hw blocking state first if it is hw-blocked */
2988 if (wlsw
== TPACPI_RFK_RADIO_OFF
)
2989 tpacpi_rfk_update_hwblock_state(true);
2991 /* Sync sw blocking state */
2992 tpacpi_rfk_update_swstate_all();
2994 /* Sync hw blocking state last if it is hw-unblocked */
2995 if (wlsw
== TPACPI_RFK_RADIO_ON
)
2996 tpacpi_rfk_update_hwblock_state(false);
2998 /* Issue rfkill input event for WLSW switch */
3000 mutex_lock(&tpacpi_inputdev_send_mutex
);
3002 input_report_switch(tpacpi_inputdev
,
3003 SW_RFKILL_ALL
, (wlsw
> 0));
3004 input_sync(tpacpi_inputdev
);
3006 mutex_unlock(&tpacpi_inputdev_send_mutex
);
3010 * this can be unconditional, as we will poll state again
3011 * if userspace uses the notify to read data
3013 hotkey_radio_sw_notify_change();
3016 static void hotkey_exit(void)
3018 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3019 mutex_lock(&hotkey_mutex
);
3020 hotkey_poll_stop_sync();
3021 mutex_unlock(&hotkey_mutex
);
3024 if (hotkey_dev_attributes
)
3025 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3027 kfree(hotkey_keycode_map
);
3029 dbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_HKEY
,
3030 "restoring original HKEY status and mask\n");
3031 /* yes, there is a bitwise or below, we want the
3032 * functions to be called even if one of them fail */
3033 if (((tp_features
.hotkey_mask
&&
3034 hotkey_mask_set(hotkey_orig_mask
)) |
3035 hotkey_status_set(false)) != 0)
3037 "failed to restore hot key mask "
3038 "to BIOS defaults\n");
3041 static void __init
hotkey_unmap(const unsigned int scancode
)
3043 if (hotkey_keycode_map
[scancode
] != KEY_RESERVED
) {
3044 clear_bit(hotkey_keycode_map
[scancode
],
3045 tpacpi_inputdev
->keybit
);
3046 hotkey_keycode_map
[scancode
] = KEY_RESERVED
;
3052 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3055 #define TPACPI_HK_Q_INIMASK 0x0001
3057 static const struct tpacpi_quirk tpacpi_hotkey_qtable
[] __initconst
= {
3058 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK
), /* 600E */
3059 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK
), /* 600E */
3060 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK
), /* 770, 770E, 770ED */
3061 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK
), /* A20m */
3062 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK
), /* A20p */
3063 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK
), /* A21e, A22e */
3064 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK
), /* A21e */
3065 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK
), /* A21m, A22m */
3066 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK
), /* A21p, A22p */
3067 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK
), /* A22e */
3068 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK
), /* A22m */
3069 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK
), /* A30/p (0) */
3070 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK
), /* R30 */
3071 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK
), /* R31 */
3072 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK
), /* T20 */
3073 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK
), /* T21 */
3074 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK
), /* T22 */
3075 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK
), /* X20, X21 */
3076 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK
), /* X22, X23, X24 */
3079 static int __init
hotkey_init(struct ibm_init_struct
*iibm
)
3081 /* Requirements for changing the default keymaps:
3083 * 1. Many of the keys are mapped to KEY_RESERVED for very
3084 * good reasons. Do not change them unless you have deep
3085 * knowledge on the IBM and Lenovo ThinkPad firmware for
3086 * the various ThinkPad models. The driver behaves
3087 * differently for KEY_RESERVED: such keys have their
3088 * hot key mask *unset* in mask_recommended, and also
3089 * in the initial hot key mask programmed into the
3090 * firmware at driver load time, which means the firm-
3091 * ware may react very differently if you change them to
3094 * 2. You must be subscribed to the linux-thinkpad and
3095 * ibm-acpi-devel mailing lists, and you should read the
3096 * list archives since 2007 if you want to change the
3097 * keymaps. This requirement exists so that you will
3098 * know the past history of problems with the thinkpad-
3099 * acpi driver keymaps, and also that you will be
3100 * listening to any bug reports;
3102 * 3. Do not send thinkpad-acpi specific patches directly to
3103 * for merging, *ever*. Send them to the linux-acpi
3104 * mailinglist for comments. Merging is to be done only
3105 * through acpi-test and the ACPI maintainer.
3107 * If the above is too much to ask, don't change the keymap.
3108 * Ask the thinkpad-acpi maintainer to do it, instead.
3110 static u16 ibm_keycode_map
[] __initdata
= {
3111 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3112 KEY_FN_F1
, KEY_FN_F2
, KEY_COFFEE
, KEY_SLEEP
,
3113 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3114 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3116 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3117 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3118 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3119 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3121 /* brightness: firmware always reacts to them */
3122 KEY_RESERVED
, /* 0x0F: FN+HOME (brightness up) */
3123 KEY_RESERVED
, /* 0x10: FN+END (brightness down) */
3125 /* Thinklight: firmware always react to it */
3126 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3128 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3129 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3131 /* Volume: firmware always react to it and reprograms
3132 * the built-in *extra* mixer. Never map it to control
3133 * another mixer by default. */
3134 KEY_RESERVED
, /* 0x14: VOLUME UP */
3135 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3136 KEY_RESERVED
, /* 0x16: MUTE */
3138 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3140 /* (assignments unknown, please report if found) */
3141 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3142 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3144 static u16 lenovo_keycode_map
[] __initdata
= {
3145 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3146 KEY_FN_F1
, KEY_COFFEE
, KEY_BATTERY
, KEY_SLEEP
,
3147 KEY_WLAN
, KEY_FN_F6
, KEY_SWITCHVIDEOMODE
, KEY_FN_F8
,
3148 KEY_FN_F9
, KEY_FN_F10
, KEY_FN_F11
, KEY_SUSPEND
,
3150 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3151 KEY_UNKNOWN
, /* 0x0C: FN+BACKSPACE */
3152 KEY_UNKNOWN
, /* 0x0D: FN+INSERT */
3153 KEY_UNKNOWN
, /* 0x0E: FN+DELETE */
3155 /* These should be enabled --only-- when ACPI video
3156 * is disabled (i.e. in "vendor" mode), and are handled
3157 * in a special way by the init code */
3158 KEY_BRIGHTNESSUP
, /* 0x0F: FN+HOME (brightness up) */
3159 KEY_BRIGHTNESSDOWN
, /* 0x10: FN+END (brightness down) */
3161 KEY_RESERVED
, /* 0x11: FN+PGUP (thinklight toggle) */
3163 KEY_UNKNOWN
, /* 0x12: FN+PGDOWN */
3164 KEY_ZOOM
, /* 0x13: FN+SPACE (zoom) */
3166 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3167 * react to it and reprograms the built-in *extra* mixer.
3168 * Never map it to control another mixer by default.
3170 * T60?, T61, R60?, R61: firmware and EC tries to send
3171 * these over the regular keyboard, so these are no-ops,
3172 * but there are still weird bugs re. MUTE, so do not
3173 * change unless you get test reports from all Lenovo
3174 * models. May cause the BIOS to interfere with the
3177 KEY_RESERVED
, /* 0x14: VOLUME UP */
3178 KEY_RESERVED
, /* 0x15: VOLUME DOWN */
3179 KEY_RESERVED
, /* 0x16: MUTE */
3181 KEY_VENDOR
, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3183 /* (assignments unknown, please report if found) */
3184 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3185 KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
, KEY_UNKNOWN
,
3188 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3189 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3190 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3195 bool radiosw_state
= false;
3196 bool tabletsw_state
= false;
3198 unsigned long quirks
;
3200 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3201 "initializing hotkey subdriver\n");
3203 BUG_ON(!tpacpi_inputdev
);
3204 BUG_ON(tpacpi_inputdev
->open
!= NULL
||
3205 tpacpi_inputdev
->close
!= NULL
);
3207 TPACPI_ACPIHANDLE_INIT(hkey
);
3208 mutex_init(&hotkey_mutex
);
3210 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3211 mutex_init(&hotkey_thread_mutex
);
3212 mutex_init(&hotkey_thread_data_mutex
);
3215 /* hotkey not supported on 570 */
3216 tp_features
.hotkey
= hkey_handle
!= NULL
;
3218 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3220 str_supported(tp_features
.hotkey
));
3222 if (!tp_features
.hotkey
)
3225 quirks
= tpacpi_check_quirks(tpacpi_hotkey_qtable
,
3226 ARRAY_SIZE(tpacpi_hotkey_qtable
));
3228 tpacpi_disable_brightness_delay();
3230 /* MUST have enough space for all attributes to be added to
3231 * hotkey_dev_attributes */
3232 hotkey_dev_attributes
= create_attr_set(
3233 ARRAY_SIZE(hotkey_attributes
) + 2,
3235 if (!hotkey_dev_attributes
)
3237 res
= add_many_to_attr_set(hotkey_dev_attributes
,
3239 ARRAY_SIZE(hotkey_attributes
));
3243 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3244 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3245 for HKEY interface version 0x100 */
3246 if (acpi_evalf(hkey_handle
, &hkeyv
, "MHKV", "qd")) {
3247 if ((hkeyv
>> 8) != 1) {
3248 printk(TPACPI_ERR
"unknown version of the "
3249 "HKEY interface: 0x%x\n", hkeyv
);
3250 printk(TPACPI_ERR
"please report this to %s\n",
3254 * MHKV 0x100 in A31, R40, R40e,
3255 * T4x, X31, and later
3257 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3258 "firmware HKEY interface version: 0x%x\n",
3261 /* Paranoia check AND init hotkey_all_mask */
3262 if (!acpi_evalf(hkey_handle
, &hotkey_all_mask
,
3265 "missing MHKA handler, "
3266 "please report this to %s\n",
3268 /* Fallback: pre-init for FN+F3,F4,F12 */
3269 hotkey_all_mask
= 0x080cU
;
3271 tp_features
.hotkey_mask
= 1;
3276 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3277 "hotkey masks are %s\n",
3278 str_supported(tp_features
.hotkey_mask
));
3280 /* Init hotkey_all_mask if not initialized yet */
3281 if (!tp_features
.hotkey_mask
&& !hotkey_all_mask
&&
3282 (quirks
& TPACPI_HK_Q_INIMASK
))
3283 hotkey_all_mask
= 0x080cU
; /* FN+F12, FN+F4, FN+F3 */
3285 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3286 if (tp_features
.hotkey_mask
) {
3287 /* hotkey_source_mask *must* be zero for
3288 * the first hotkey_mask_get to return hotkey_orig_mask */
3289 res
= hotkey_mask_get();
3293 hotkey_orig_mask
= hotkey_acpi_mask
;
3295 hotkey_orig_mask
= hotkey_all_mask
;
3296 hotkey_acpi_mask
= hotkey_all_mask
;
3299 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3301 tp_features
.hotkey_wlsw
= 1;
3302 radiosw_state
= !!tpacpi_wlsw_emulstate
;
3304 "radio switch emulation enabled\n");
3307 /* Not all thinkpads have a hardware radio switch */
3308 if (acpi_evalf(hkey_handle
, &status
, "WLSW", "qd")) {
3309 tp_features
.hotkey_wlsw
= 1;
3310 radiosw_state
= !!status
;
3312 "radio switch found; radios are %s\n",
3313 enabled(status
, 0));
3315 if (tp_features
.hotkey_wlsw
)
3316 res
= add_to_attr_set(hotkey_dev_attributes
,
3317 &dev_attr_hotkey_radio_sw
.attr
);
3319 /* For X41t, X60t, X61t Tablets... */
3320 if (!res
&& acpi_evalf(hkey_handle
, &status
, "MHKG", "qd")) {
3321 tp_features
.hotkey_tablet
= 1;
3322 tabletsw_state
= !!(status
& TP_HOTKEY_TABLET_MASK
);
3324 "possible tablet mode switch found; "
3325 "ThinkPad in %s mode\n",
3326 (tabletsw_state
) ? "tablet" : "laptop");
3327 res
= add_to_attr_set(hotkey_dev_attributes
,
3328 &dev_attr_hotkey_tablet_mode
.attr
);
3332 res
= register_attr_set_with_sysfs(
3333 hotkey_dev_attributes
,
3334 &tpacpi_pdev
->dev
.kobj
);
3338 /* Set up key map */
3340 hotkey_keycode_map
= kmalloc(TPACPI_HOTKEY_MAP_SIZE
,
3342 if (!hotkey_keycode_map
) {
3344 "failed to allocate memory for key map\n");
3349 if (thinkpad_id
.vendor
== PCI_VENDOR_ID_LENOVO
) {
3350 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3351 "using Lenovo default hot key map\n");
3352 memcpy(hotkey_keycode_map
, &lenovo_keycode_map
,
3353 TPACPI_HOTKEY_MAP_SIZE
);
3355 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3356 "using IBM default hot key map\n");
3357 memcpy(hotkey_keycode_map
, &ibm_keycode_map
,
3358 TPACPI_HOTKEY_MAP_SIZE
);
3361 input_set_capability(tpacpi_inputdev
, EV_MSC
, MSC_SCAN
);
3362 tpacpi_inputdev
->keycodesize
= TPACPI_HOTKEY_MAP_TYPESIZE
;
3363 tpacpi_inputdev
->keycodemax
= TPACPI_HOTKEY_MAP_LEN
;
3364 tpacpi_inputdev
->keycode
= hotkey_keycode_map
;
3365 for (i
= 0; i
< TPACPI_HOTKEY_MAP_LEN
; i
++) {
3366 if (hotkey_keycode_map
[i
] != KEY_RESERVED
) {
3367 input_set_capability(tpacpi_inputdev
, EV_KEY
,
3368 hotkey_keycode_map
[i
]);
3370 if (i
< sizeof(hotkey_reserved_mask
)*8)
3371 hotkey_reserved_mask
|= 1 << i
;
3375 if (tp_features
.hotkey_wlsw
) {
3376 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_RFKILL_ALL
);
3377 input_report_switch(tpacpi_inputdev
,
3378 SW_RFKILL_ALL
, radiosw_state
);
3380 if (tp_features
.hotkey_tablet
) {
3381 input_set_capability(tpacpi_inputdev
, EV_SW
, SW_TABLET_MODE
);
3382 input_report_switch(tpacpi_inputdev
,
3383 SW_TABLET_MODE
, tabletsw_state
);
3386 /* Do not issue duplicate brightness change events to
3388 if (!tp_features
.bright_acpimode
)
3389 /* update bright_acpimode... */
3390 tpacpi_check_std_acpi_brightness_support();
3392 if (tp_features
.bright_acpimode
&& acpi_video_backlight_support()) {
3394 "This ThinkPad has standard ACPI backlight "
3395 "brightness control, supported by the ACPI "
3397 printk(TPACPI_NOTICE
3398 "Disabling thinkpad-acpi brightness events "
3401 /* Disable brightness up/down on Lenovo thinkpads when
3402 * ACPI is handling them, otherwise it is plain impossible
3403 * for userspace to do something even remotely sane */
3404 hotkey_reserved_mask
|=
3405 (1 << TP_ACPI_HOTKEYSCAN_FNHOME
)
3406 | (1 << TP_ACPI_HOTKEYSCAN_FNEND
);
3407 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME
);
3408 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND
);
3411 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3412 hotkey_source_mask
= TPACPI_HKEY_NVRAM_GOOD_MASK
3414 & ~hotkey_reserved_mask
;
3416 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3417 "hotkey source mask 0x%08x, polling freq %u\n",
3418 hotkey_source_mask
, hotkey_poll_freq
);
3421 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3422 "enabling firmware HKEY event interface...\n");
3423 res
= hotkey_status_set(true);
3428 res
= hotkey_mask_set(((hotkey_all_mask
& ~hotkey_reserved_mask
)
3429 | hotkey_driver_mask
)
3430 & ~hotkey_source_mask
);
3431 if (res
< 0 && res
!= -ENXIO
) {
3435 hotkey_user_mask
= (hotkey_acpi_mask
| hotkey_source_mask
)
3436 & ~hotkey_reserved_mask
;
3437 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3438 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3439 hotkey_user_mask
, hotkey_acpi_mask
, hotkey_source_mask
);
3441 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_HKEY
,
3442 "legacy ibm/hotkey event reporting over procfs %s\n",
3443 (hotkey_report_mode
< 2) ?
3444 "enabled" : "disabled");
3446 tpacpi_inputdev
->open
= &hotkey_inputdev_open
;
3447 tpacpi_inputdev
->close
= &hotkey_inputdev_close
;
3449 hotkey_poll_setup_safe(true);
3454 delete_attr_set(hotkey_dev_attributes
, &tpacpi_pdev
->dev
.kobj
);
3455 hotkey_dev_attributes
= NULL
;
3457 return (res
< 0)? res
: 1;
3460 static bool hotkey_notify_hotkey(const u32 hkey
,
3462 bool *ignore_acpi_ev
)
3464 /* 0x1000-0x1FFF: key presses */
3465 unsigned int scancode
= hkey
& 0xfff;
3466 *send_acpi_ev
= true;
3467 *ignore_acpi_ev
= false;
3469 if (scancode
> 0 && scancode
< 0x21) {
3471 if (!(hotkey_source_mask
& (1 << scancode
))) {
3472 tpacpi_input_send_key_masked(scancode
);
3473 *send_acpi_ev
= false;
3475 *ignore_acpi_ev
= true;
3482 static bool hotkey_notify_wakeup(const u32 hkey
,
3484 bool *ignore_acpi_ev
)
3486 /* 0x2000-0x2FFF: Wakeup reason */
3487 *send_acpi_ev
= true;
3488 *ignore_acpi_ev
= false;
3491 case TP_HKEY_EV_WKUP_S3_UNDOCK
: /* suspend, undock */
3492 case TP_HKEY_EV_WKUP_S4_UNDOCK
: /* hibernation, undock */
3493 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_UNDOCK
;
3494 *ignore_acpi_ev
= true;
3497 case TP_HKEY_EV_WKUP_S3_BAYEJ
: /* suspend, bay eject */
3498 case TP_HKEY_EV_WKUP_S4_BAYEJ
: /* hibernation, bay eject */
3499 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_BAYEJ
;
3500 *ignore_acpi_ev
= true;
3503 case TP_HKEY_EV_WKUP_S3_BATLOW
: /* Battery on critical low level/S3 */
3504 case TP_HKEY_EV_WKUP_S4_BATLOW
: /* Battery on critical low level/S4 */
3506 "EMERGENCY WAKEUP: battery almost empty\n");
3507 /* how to auto-heal: */
3508 /* 2313: woke up from S3, go to S4/S5 */
3509 /* 2413: woke up from S4, go to S5 */
3516 if (hotkey_wakeup_reason
!= TP_ACPI_WAKEUP_NONE
) {
3518 "woke up due to a hot-unplug "
3520 hotkey_wakeup_reason_notify_change();
3525 static bool hotkey_notify_usrevent(const u32 hkey
,
3527 bool *ignore_acpi_ev
)
3529 /* 0x5000-0x5FFF: human interface helpers */
3530 *send_acpi_ev
= true;
3531 *ignore_acpi_ev
= false;
3534 case TP_HKEY_EV_PEN_INSERTED
: /* X61t: tablet pen inserted into bay */
3535 case TP_HKEY_EV_PEN_REMOVED
: /* X61t: tablet pen removed from bay */
3538 case TP_HKEY_EV_TABLET_TABLET
: /* X41t-X61t: tablet mode */
3539 case TP_HKEY_EV_TABLET_NOTEBOOK
: /* X41t-X61t: normal mode */
3540 tpacpi_input_send_tabletsw();
3541 hotkey_tablet_mode_notify_change();
3542 *send_acpi_ev
= false;
3545 case TP_HKEY_EV_LID_CLOSE
: /* Lid closed */
3546 case TP_HKEY_EV_LID_OPEN
: /* Lid opened */
3547 case TP_HKEY_EV_BRGHT_CHANGED
: /* brightness changed */
3548 /* do not propagate these events */
3549 *ignore_acpi_ev
= true;
3557 static void thermal_dump_all_sensors(void);
3559 static bool hotkey_notify_thermal(const u32 hkey
,
3561 bool *ignore_acpi_ev
)
3565 /* 0x6000-0x6FFF: thermal alarms */
3566 *send_acpi_ev
= true;
3567 *ignore_acpi_ev
= false;
3570 case TP_HKEY_EV_THM_TABLE_CHANGED
:
3572 "EC reports that Thermal Table has changed\n");
3573 /* recommended action: do nothing, we don't have
3574 * Lenovo ATM information */
3576 case TP_HKEY_EV_ALARM_BAT_HOT
:
3578 "THERMAL ALARM: battery is too hot!\n");
3579 /* recommended action: warn user through gui */
3581 case TP_HKEY_EV_ALARM_BAT_XHOT
:
3583 "THERMAL EMERGENCY: battery is extremely hot!\n");
3584 /* recommended action: immediate sleep/hibernate */
3586 case TP_HKEY_EV_ALARM_SENSOR_HOT
:
3589 "a sensor reports something is too hot!\n");
3590 /* recommended action: warn user through gui, that */
3591 /* some internal component is too hot */
3593 case TP_HKEY_EV_ALARM_SENSOR_XHOT
:
3595 "THERMAL EMERGENCY: "
3596 "a sensor reports something is extremely hot!\n");
3597 /* recommended action: immediate sleep/hibernate */
3601 "THERMAL ALERT: unknown thermal alarm received\n");
3605 thermal_dump_all_sensors();
3610 static void hotkey_notify(struct ibm_struct
*ibm
, u32 event
)
3614 bool ignore_acpi_ev
;
3617 if (event
!= 0x80) {
3619 "unknown HKEY notification event %d\n", event
);
3620 /* forward it to userspace, maybe it knows how to handle it */
3621 acpi_bus_generate_netlink_event(
3622 ibm
->acpi
->device
->pnp
.device_class
,
3623 dev_name(&ibm
->acpi
->device
->dev
),
3629 if (!acpi_evalf(hkey_handle
, &hkey
, "MHKP", "d")) {
3630 printk(TPACPI_ERR
"failed to retrieve HKEY event\n");
3639 send_acpi_ev
= true;
3640 ignore_acpi_ev
= false;
3642 switch (hkey
>> 12) {
3644 /* 0x1000-0x1FFF: key presses */
3645 known_ev
= hotkey_notify_hotkey(hkey
, &send_acpi_ev
,
3649 /* 0x2000-0x2FFF: Wakeup reason */
3650 known_ev
= hotkey_notify_wakeup(hkey
, &send_acpi_ev
,
3654 /* 0x3000-0x3FFF: bay-related wakeups */
3655 if (hkey
== TP_HKEY_EV_BAYEJ_ACK
) {
3656 hotkey_autosleep_ack
= 1;
3659 hotkey_wakeup_hotunplug_complete_notify_change();
3666 /* 0x4000-0x4FFF: dock-related wakeups */
3667 if (hkey
== TP_HKEY_EV_UNDOCK_ACK
) {
3668 hotkey_autosleep_ack
= 1;
3671 hotkey_wakeup_hotunplug_complete_notify_change();
3678 /* 0x5000-0x5FFF: human interface helpers */
3679 known_ev
= hotkey_notify_usrevent(hkey
, &send_acpi_ev
,
3683 /* 0x6000-0x6FFF: thermal alarms */
3684 known_ev
= hotkey_notify_thermal(hkey
, &send_acpi_ev
,
3688 /* 0x7000-0x7FFF: misc */
3689 if (tp_features
.hotkey_wlsw
&&
3690 hkey
== TP_HKEY_EV_RFKILL_CHANGED
) {
3691 tpacpi_send_radiosw_update();
3696 /* fallthrough to default */
3701 printk(TPACPI_NOTICE
3702 "unhandled HKEY event 0x%04x\n", hkey
);
3703 printk(TPACPI_NOTICE
3704 "please report the conditions when this "
3705 "event happened to %s\n", TPACPI_MAIL
);
3709 if (!ignore_acpi_ev
&&
3710 (send_acpi_ev
|| hotkey_report_mode
< 2)) {
3711 acpi_bus_generate_proc_event(ibm
->acpi
->device
,
3715 /* netlink events */
3716 if (!ignore_acpi_ev
&& send_acpi_ev
) {
3717 acpi_bus_generate_netlink_event(
3718 ibm
->acpi
->device
->pnp
.device_class
,
3719 dev_name(&ibm
->acpi
->device
->dev
),
3725 static void hotkey_suspend(pm_message_t state
)
3727 /* Do these on suspend, we get the events on early resume! */
3728 hotkey_wakeup_reason
= TP_ACPI_WAKEUP_NONE
;
3729 hotkey_autosleep_ack
= 0;
3732 static void hotkey_resume(void)
3734 tpacpi_disable_brightness_delay();
3736 if (hotkey_status_set(true) < 0 ||
3737 hotkey_mask_set(hotkey_acpi_mask
) < 0)
3739 "error while attempting to reset the event "
3740 "firmware interface\n");
3742 tpacpi_send_radiosw_update();
3743 hotkey_tablet_mode_notify_change();
3744 hotkey_wakeup_reason_notify_change();
3745 hotkey_wakeup_hotunplug_complete_notify_change();
3746 hotkey_poll_setup_safe(false);
3749 /* procfs -------------------------------------------------------------- */
3750 static int hotkey_read(char *p
)
3755 if (!tp_features
.hotkey
) {
3756 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
3760 if (mutex_lock_killable(&hotkey_mutex
))
3761 return -ERESTARTSYS
;
3762 res
= hotkey_status_get(&status
);
3764 res
= hotkey_mask_get();
3765 mutex_unlock(&hotkey_mutex
);
3769 len
+= sprintf(p
+ len
, "status:\t\t%s\n", enabled(status
, 0));
3770 if (hotkey_all_mask
) {
3771 len
+= sprintf(p
+ len
, "mask:\t\t0x%08x\n", hotkey_user_mask
);
3772 len
+= sprintf(p
+ len
,
3773 "commands:\tenable, disable, reset, <mask>\n");
3775 len
+= sprintf(p
+ len
, "mask:\t\tnot supported\n");
3776 len
+= sprintf(p
+ len
, "commands:\tenable, disable, reset\n");
3782 static void hotkey_enabledisable_warn(bool enable
)
3784 tpacpi_log_usertask("procfs hotkey enable/disable");
3785 if (!WARN((tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
|| !enable
),
3787 "hotkey enable/disable functionality has been "
3788 "removed from the driver. Hotkeys are always "
3791 "Please remove the hotkey=enable module "
3792 "parameter, it is deprecated. Hotkeys are always "
3796 static int hotkey_write(char *buf
)
3802 if (!tp_features
.hotkey
)
3805 if (mutex_lock_killable(&hotkey_mutex
))
3806 return -ERESTARTSYS
;
3808 mask
= hotkey_user_mask
;
3811 while ((cmd
= next_cmd(&buf
))) {
3812 if (strlencmp(cmd
, "enable") == 0) {
3813 hotkey_enabledisable_warn(1);
3814 } else if (strlencmp(cmd
, "disable") == 0) {
3815 hotkey_enabledisable_warn(0);
3817 } else if (strlencmp(cmd
, "reset") == 0) {
3818 mask
= (hotkey_all_mask
| hotkey_source_mask
)
3819 & ~hotkey_reserved_mask
;
3820 } else if (sscanf(cmd
, "0x%x", &mask
) == 1) {
3822 } else if (sscanf(cmd
, "%x", &mask
) == 1) {
3831 tpacpi_disclose_usertask("procfs hotkey",
3832 "set mask to 0x%08x\n", mask
);
3833 res
= hotkey_user_mask_set(mask
);
3837 mutex_unlock(&hotkey_mutex
);
3841 static const struct acpi_device_id ibm_htk_device_ids
[] = {
3842 {TPACPI_ACPI_HKEY_HID
, 0},
3846 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver
= {
3847 .hid
= ibm_htk_device_ids
,
3848 .notify
= hotkey_notify
,
3849 .handle
= &hkey_handle
,
3850 .type
= ACPI_DEVICE_NOTIFY
,
3853 static struct ibm_struct hotkey_driver_data
= {
3855 .read
= hotkey_read
,
3856 .write
= hotkey_write
,
3857 .exit
= hotkey_exit
,
3858 .resume
= hotkey_resume
,
3859 .suspend
= hotkey_suspend
,
3860 .acpi
= &ibm_hotkey_acpidriver
,
3863 /*************************************************************************
3864 * Bluetooth subdriver
3868 /* ACPI GBDC/SBDC bits */
3869 TP_ACPI_BLUETOOTH_HWPRESENT
= 0x01, /* Bluetooth hw available */
3870 TP_ACPI_BLUETOOTH_RADIOSSW
= 0x02, /* Bluetooth radio enabled */
3871 TP_ACPI_BLUETOOTH_RESUMECTRL
= 0x04, /* Bluetooth state at resume:
3876 /* ACPI \BLTH commands */
3877 TP_ACPI_BLTH_GET_ULTRAPORT_ID
= 0x00, /* Get Ultraport BT ID */
3878 TP_ACPI_BLTH_GET_PWR_ON_RESUME
= 0x01, /* Get power-on-resume state */
3879 TP_ACPI_BLTH_PWR_ON_ON_RESUME
= 0x02, /* Resume powered on */
3880 TP_ACPI_BLTH_PWR_OFF_ON_RESUME
= 0x03, /* Resume powered off */
3881 TP_ACPI_BLTH_SAVE_STATE
= 0x05, /* Save state for S4/S5 */
3884 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3886 static int bluetooth_get_status(void)
3890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_bluetoothemul
)
3892 return (tpacpi_bluetooth_emulstate
) ?
3893 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3896 if (!acpi_evalf(hkey_handle
, &status
, "GBDC", "d"))
3899 return ((status
& TP_ACPI_BLUETOOTH_RADIOSSW
) != 0) ?
3900 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
3903 static int bluetooth_set_status(enum tpacpi_rfkill_state state
)
3907 vdbg_printk(TPACPI_DBG_RFKILL
,
3908 "will attempt to %s bluetooth\n",
3909 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
3911 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3912 if (dbg_bluetoothemul
) {
3913 tpacpi_bluetooth_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
3918 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3919 status
= TP_ACPI_BLUETOOTH_RESUMECTRL
;
3920 if (state
== TPACPI_RFK_RADIO_ON
)
3921 status
|= TP_ACPI_BLUETOOTH_RADIOSSW
;
3923 if (!acpi_evalf(hkey_handle
, NULL
, "SBDC", "vd", status
))
3929 /* sysfs bluetooth enable ---------------------------------------------- */
3930 static ssize_t
bluetooth_enable_show(struct device
*dev
,
3931 struct device_attribute
*attr
,
3934 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID
,
3938 static ssize_t
bluetooth_enable_store(struct device
*dev
,
3939 struct device_attribute
*attr
,
3940 const char *buf
, size_t count
)
3942 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID
,
3946 static struct device_attribute dev_attr_bluetooth_enable
=
3947 __ATTR(bluetooth_enable
, S_IWUSR
| S_IRUGO
,
3948 bluetooth_enable_show
, bluetooth_enable_store
);
3950 /* --------------------------------------------------------------------- */
3952 static struct attribute
*bluetooth_attributes
[] = {
3953 &dev_attr_bluetooth_enable
.attr
,
3957 static const struct attribute_group bluetooth_attr_group
= {
3958 .attrs
= bluetooth_attributes
,
3961 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops
= {
3962 .get_status
= bluetooth_get_status
,
3963 .set_status
= bluetooth_set_status
,
3966 static void bluetooth_shutdown(void)
3968 /* Order firmware to save current state to NVRAM */
3969 if (!acpi_evalf(NULL
, NULL
, "\\BLTH", "vd",
3970 TP_ACPI_BLTH_SAVE_STATE
))
3971 printk(TPACPI_NOTICE
3972 "failed to save bluetooth state to NVRAM\n");
3974 vdbg_printk(TPACPI_DBG_RFKILL
,
3975 "bluestooth state saved to NVRAM\n");
3978 static void bluetooth_exit(void)
3980 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
3981 &bluetooth_attr_group
);
3983 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
3985 bluetooth_shutdown();
3988 static int __init
bluetooth_init(struct ibm_init_struct
*iibm
)
3993 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
3994 "initializing bluetooth subdriver\n");
3996 TPACPI_ACPIHANDLE_INIT(hkey
);
3998 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3999 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4000 tp_features
.bluetooth
= hkey_handle
&&
4001 acpi_evalf(hkey_handle
, &status
, "GBDC", "qd");
4003 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4004 "bluetooth is %s, status 0x%02x\n",
4005 str_supported(tp_features
.bluetooth
),
4008 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4009 if (dbg_bluetoothemul
) {
4010 tp_features
.bluetooth
= 1;
4012 "bluetooth switch emulation enabled\n");
4015 if (tp_features
.bluetooth
&&
4016 !(status
& TP_ACPI_BLUETOOTH_HWPRESENT
)) {
4017 /* no bluetooth hardware present in system */
4018 tp_features
.bluetooth
= 0;
4019 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4020 "bluetooth hardware not installed\n");
4023 if (!tp_features
.bluetooth
)
4026 res
= tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
,
4027 &bluetooth_tprfk_ops
,
4028 RFKILL_TYPE_BLUETOOTH
,
4029 TPACPI_RFK_BLUETOOTH_SW_NAME
,
4034 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4035 &bluetooth_attr_group
);
4037 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID
);
4044 /* procfs -------------------------------------------------------------- */
4045 static int bluetooth_read(char *p
)
4047 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID
, p
);
4050 static int bluetooth_write(char *buf
)
4052 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID
, buf
);
4055 static struct ibm_struct bluetooth_driver_data
= {
4056 .name
= "bluetooth",
4057 .read
= bluetooth_read
,
4058 .write
= bluetooth_write
,
4059 .exit
= bluetooth_exit
,
4060 .shutdown
= bluetooth_shutdown
,
4063 /*************************************************************************
4068 /* ACPI GWAN/SWAN bits */
4069 TP_ACPI_WANCARD_HWPRESENT
= 0x01, /* Wan hw available */
4070 TP_ACPI_WANCARD_RADIOSSW
= 0x02, /* Wan radio enabled */
4071 TP_ACPI_WANCARD_RESUMECTRL
= 0x04, /* Wan state at resume:
4075 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4077 static int wan_get_status(void)
4081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4083 return (tpacpi_wwan_emulstate
) ?
4084 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4087 if (!acpi_evalf(hkey_handle
, &status
, "GWAN", "d"))
4090 return ((status
& TP_ACPI_WANCARD_RADIOSSW
) != 0) ?
4091 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4094 static int wan_set_status(enum tpacpi_rfkill_state state
)
4098 vdbg_printk(TPACPI_DBG_RFKILL
,
4099 "will attempt to %s wwan\n",
4100 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4102 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4104 tpacpi_wwan_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4109 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4110 status
= TP_ACPI_WANCARD_RESUMECTRL
;
4111 if (state
== TPACPI_RFK_RADIO_ON
)
4112 status
|= TP_ACPI_WANCARD_RADIOSSW
;
4114 if (!acpi_evalf(hkey_handle
, NULL
, "SWAN", "vd", status
))
4120 /* sysfs wan enable ---------------------------------------------------- */
4121 static ssize_t
wan_enable_show(struct device
*dev
,
4122 struct device_attribute
*attr
,
4125 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID
,
4129 static ssize_t
wan_enable_store(struct device
*dev
,
4130 struct device_attribute
*attr
,
4131 const char *buf
, size_t count
)
4133 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID
,
4137 static struct device_attribute dev_attr_wan_enable
=
4138 __ATTR(wwan_enable
, S_IWUSR
| S_IRUGO
,
4139 wan_enable_show
, wan_enable_store
);
4141 /* --------------------------------------------------------------------- */
4143 static struct attribute
*wan_attributes
[] = {
4144 &dev_attr_wan_enable
.attr
,
4148 static const struct attribute_group wan_attr_group
= {
4149 .attrs
= wan_attributes
,
4152 static const struct tpacpi_rfk_ops wan_tprfk_ops
= {
4153 .get_status
= wan_get_status
,
4154 .set_status
= wan_set_status
,
4157 static void wan_shutdown(void)
4159 /* Order firmware to save current state to NVRAM */
4160 if (!acpi_evalf(NULL
, NULL
, "\\WGSV", "vd",
4161 TP_ACPI_WGSV_SAVE_STATE
))
4162 printk(TPACPI_NOTICE
4163 "failed to save WWAN state to NVRAM\n");
4165 vdbg_printk(TPACPI_DBG_RFKILL
,
4166 "WWAN state saved to NVRAM\n");
4169 static void wan_exit(void)
4171 sysfs_remove_group(&tpacpi_pdev
->dev
.kobj
,
4174 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4179 static int __init
wan_init(struct ibm_init_struct
*iibm
)
4184 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4185 "initializing wan subdriver\n");
4187 TPACPI_ACPIHANDLE_INIT(hkey
);
4189 tp_features
.wan
= hkey_handle
&&
4190 acpi_evalf(hkey_handle
, &status
, "GWAN", "qd");
4192 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4193 "wan is %s, status 0x%02x\n",
4194 str_supported(tp_features
.wan
),
4197 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4199 tp_features
.wan
= 1;
4201 "wwan switch emulation enabled\n");
4204 if (tp_features
.wan
&&
4205 !(status
& TP_ACPI_WANCARD_HWPRESENT
)) {
4206 /* no wan hardware present in system */
4207 tp_features
.wan
= 0;
4208 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4209 "wan hardware not installed\n");
4212 if (!tp_features
.wan
)
4215 res
= tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID
,
4218 TPACPI_RFK_WWAN_SW_NAME
,
4223 res
= sysfs_create_group(&tpacpi_pdev
->dev
.kobj
,
4227 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID
);
4234 /* procfs -------------------------------------------------------------- */
4235 static int wan_read(char *p
)
4237 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID
, p
);
4240 static int wan_write(char *buf
)
4242 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID
, buf
);
4245 static struct ibm_struct wan_driver_data
= {
4250 .shutdown
= wan_shutdown
,
4253 /*************************************************************************
4258 /* ACPI GUWB/SUWB bits */
4259 TP_ACPI_UWB_HWPRESENT
= 0x01, /* UWB hw available */
4260 TP_ACPI_UWB_RADIOSSW
= 0x02, /* UWB radio enabled */
4263 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4265 static int uwb_get_status(void)
4269 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4271 return (tpacpi_uwb_emulstate
) ?
4272 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4275 if (!acpi_evalf(hkey_handle
, &status
, "GUWB", "d"))
4278 return ((status
& TP_ACPI_UWB_RADIOSSW
) != 0) ?
4279 TPACPI_RFK_RADIO_ON
: TPACPI_RFK_RADIO_OFF
;
4282 static int uwb_set_status(enum tpacpi_rfkill_state state
)
4286 vdbg_printk(TPACPI_DBG_RFKILL
,
4287 "will attempt to %s UWB\n",
4288 (state
== TPACPI_RFK_RADIO_ON
) ? "enable" : "disable");
4290 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4292 tpacpi_uwb_emulstate
= (state
== TPACPI_RFK_RADIO_ON
);
4297 if (state
== TPACPI_RFK_RADIO_ON
)
4298 status
= TP_ACPI_UWB_RADIOSSW
;
4302 if (!acpi_evalf(hkey_handle
, NULL
, "SUWB", "vd", status
))
4308 /* --------------------------------------------------------------------- */
4310 static const struct tpacpi_rfk_ops uwb_tprfk_ops
= {
4311 .get_status
= uwb_get_status
,
4312 .set_status
= uwb_set_status
,
4315 static void uwb_exit(void)
4317 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID
);
4320 static int __init
uwb_init(struct ibm_init_struct
*iibm
)
4325 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4326 "initializing uwb subdriver\n");
4328 TPACPI_ACPIHANDLE_INIT(hkey
);
4330 tp_features
.uwb
= hkey_handle
&&
4331 acpi_evalf(hkey_handle
, &status
, "GUWB", "qd");
4333 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_RFKILL
,
4334 "uwb is %s, status 0x%02x\n",
4335 str_supported(tp_features
.uwb
),
4338 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4340 tp_features
.uwb
= 1;
4342 "uwb switch emulation enabled\n");
4345 if (tp_features
.uwb
&&
4346 !(status
& TP_ACPI_UWB_HWPRESENT
)) {
4347 /* no uwb hardware present in system */
4348 tp_features
.uwb
= 0;
4349 dbg_printk(TPACPI_DBG_INIT
,
4350 "uwb hardware not installed\n");
4353 if (!tp_features
.uwb
)
4356 res
= tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID
,
4359 TPACPI_RFK_UWB_SW_NAME
,
4364 static struct ibm_struct uwb_driver_data
= {
4367 .flags
.experimental
= 1,
4370 /*************************************************************************
4374 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4376 enum video_access_mode
{
4377 TPACPI_VIDEO_NONE
= 0,
4378 TPACPI_VIDEO_570
, /* 570 */
4379 TPACPI_VIDEO_770
, /* 600e/x, 770e, 770x */
4380 TPACPI_VIDEO_NEW
, /* all others */
4383 enum { /* video status flags, based on VIDEO_570 */
4384 TP_ACPI_VIDEO_S_LCD
= 0x01, /* LCD output enabled */
4385 TP_ACPI_VIDEO_S_CRT
= 0x02, /* CRT output enabled */
4386 TP_ACPI_VIDEO_S_DVI
= 0x08, /* DVI output enabled */
4389 enum { /* TPACPI_VIDEO_570 constants */
4390 TP_ACPI_VIDEO_570_PHSCMD
= 0x87, /* unknown magic constant :( */
4391 TP_ACPI_VIDEO_570_PHSMASK
= 0x03, /* PHS bits that map to
4392 * video_status_flags */
4393 TP_ACPI_VIDEO_570_PHS2CMD
= 0x8b, /* unknown magic constant :( */
4394 TP_ACPI_VIDEO_570_PHS2SET
= 0x80, /* unknown magic constant :( */
4397 static enum video_access_mode video_supported
;
4398 static int video_orig_autosw
;
4400 static int video_autosw_get(void);
4401 static int video_autosw_set(int enable
);
4403 TPACPI_HANDLE(vid2
, root
, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4405 static int __init
video_init(struct ibm_init_struct
*iibm
)
4409 vdbg_printk(TPACPI_DBG_INIT
, "initializing video subdriver\n");
4411 TPACPI_ACPIHANDLE_INIT(vid
);
4412 TPACPI_ACPIHANDLE_INIT(vid2
);
4414 if (vid2_handle
&& acpi_evalf(NULL
, &ivga
, "\\IVGA", "d") && ivga
)
4415 /* G41, assume IVGA doesn't change */
4416 vid_handle
= vid2_handle
;
4419 /* video switching not supported on R30, R31 */
4420 video_supported
= TPACPI_VIDEO_NONE
;
4421 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "SWIT", "qd"))
4423 video_supported
= TPACPI_VIDEO_570
;
4424 else if (acpi_evalf(vid_handle
, &video_orig_autosw
, "^VADL", "qd"))
4425 /* 600e/x, 770e, 770x */
4426 video_supported
= TPACPI_VIDEO_770
;
4429 video_supported
= TPACPI_VIDEO_NEW
;
4431 vdbg_printk(TPACPI_DBG_INIT
, "video is %s, mode %d\n",
4432 str_supported(video_supported
!= TPACPI_VIDEO_NONE
),
4435 return (video_supported
!= TPACPI_VIDEO_NONE
)? 0 : 1;
4438 static void video_exit(void)
4440 dbg_printk(TPACPI_DBG_EXIT
,
4441 "restoring original video autoswitch mode\n");
4442 if (video_autosw_set(video_orig_autosw
))
4443 printk(TPACPI_ERR
"error while trying to restore original "
4444 "video autoswitch mode\n");
4447 static int video_outputsw_get(void)
4452 switch (video_supported
) {
4453 case TPACPI_VIDEO_570
:
4454 if (!acpi_evalf(NULL
, &i
, "\\_SB.PHS", "dd",
4455 TP_ACPI_VIDEO_570_PHSCMD
))
4457 status
= i
& TP_ACPI_VIDEO_570_PHSMASK
;
4459 case TPACPI_VIDEO_770
:
4460 if (!acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4463 status
|= TP_ACPI_VIDEO_S_LCD
;
4464 if (!acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4467 status
|= TP_ACPI_VIDEO_S_CRT
;
4469 case TPACPI_VIDEO_NEW
:
4470 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 1) ||
4471 !acpi_evalf(NULL
, &i
, "\\VCDC", "d"))
4474 status
|= TP_ACPI_VIDEO_S_CRT
;
4476 if (!acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0) ||
4477 !acpi_evalf(NULL
, &i
, "\\VCDL", "d"))
4480 status
|= TP_ACPI_VIDEO_S_LCD
;
4481 if (!acpi_evalf(NULL
, &i
, "\\VCDD", "d"))
4484 status
|= TP_ACPI_VIDEO_S_DVI
;
4493 static int video_outputsw_set(int status
)
4498 switch (video_supported
) {
4499 case TPACPI_VIDEO_570
:
4500 res
= acpi_evalf(NULL
, NULL
,
4501 "\\_SB.PHS2", "vdd",
4502 TP_ACPI_VIDEO_570_PHS2CMD
,
4503 status
| TP_ACPI_VIDEO_570_PHS2SET
);
4505 case TPACPI_VIDEO_770
:
4506 autosw
= video_autosw_get();
4510 res
= video_autosw_set(1);
4513 res
= acpi_evalf(vid_handle
, NULL
,
4514 "ASWT", "vdd", status
* 0x100, 0);
4515 if (!autosw
&& video_autosw_set(autosw
)) {
4517 "video auto-switch left enabled due to error\n");
4521 case TPACPI_VIDEO_NEW
:
4522 res
= acpi_evalf(NULL
, NULL
, "\\VUPS", "vd", 0x80) &&
4523 acpi_evalf(NULL
, NULL
, "\\VSDS", "vdd", status
, 1);
4529 return (res
)? 0 : -EIO
;
4532 static int video_autosw_get(void)
4536 switch (video_supported
) {
4537 case TPACPI_VIDEO_570
:
4538 if (!acpi_evalf(vid_handle
, &autosw
, "SWIT", "d"))
4541 case TPACPI_VIDEO_770
:
4542 case TPACPI_VIDEO_NEW
:
4543 if (!acpi_evalf(vid_handle
, &autosw
, "^VDEE", "d"))
4553 static int video_autosw_set(int enable
)
4555 if (!acpi_evalf(vid_handle
, NULL
, "_DOS", "vd", (enable
)? 1 : 0))
4560 static int video_outputsw_cycle(void)
4562 int autosw
= video_autosw_get();
4568 switch (video_supported
) {
4569 case TPACPI_VIDEO_570
:
4570 res
= video_autosw_set(1);
4573 res
= acpi_evalf(ec_handle
, NULL
, "_Q16", "v");
4575 case TPACPI_VIDEO_770
:
4576 case TPACPI_VIDEO_NEW
:
4577 res
= video_autosw_set(1);
4580 res
= acpi_evalf(vid_handle
, NULL
, "VSWT", "v");
4585 if (!autosw
&& video_autosw_set(autosw
)) {
4587 "video auto-switch left enabled due to error\n");
4591 return (res
)? 0 : -EIO
;
4594 static int video_expand_toggle(void)
4596 switch (video_supported
) {
4597 case TPACPI_VIDEO_570
:
4598 return acpi_evalf(ec_handle
, NULL
, "_Q17", "v")?
4600 case TPACPI_VIDEO_770
:
4601 return acpi_evalf(vid_handle
, NULL
, "VEXP", "v")?
4603 case TPACPI_VIDEO_NEW
:
4604 return acpi_evalf(NULL
, NULL
, "\\VEXP", "v")?
4612 static int video_read(char *p
)
4617 if (video_supported
== TPACPI_VIDEO_NONE
) {
4618 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4622 status
= video_outputsw_get();
4626 autosw
= video_autosw_get();
4630 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4631 len
+= sprintf(p
+ len
, "lcd:\t\t%s\n", enabled(status
, 0));
4632 len
+= sprintf(p
+ len
, "crt:\t\t%s\n", enabled(status
, 1));
4633 if (video_supported
== TPACPI_VIDEO_NEW
)
4634 len
+= sprintf(p
+ len
, "dvi:\t\t%s\n", enabled(status
, 3));
4635 len
+= sprintf(p
+ len
, "auto:\t\t%s\n", enabled(autosw
, 0));
4636 len
+= sprintf(p
+ len
, "commands:\tlcd_enable, lcd_disable\n");
4637 len
+= sprintf(p
+ len
, "commands:\tcrt_enable, crt_disable\n");
4638 if (video_supported
== TPACPI_VIDEO_NEW
)
4639 len
+= sprintf(p
+ len
, "commands:\tdvi_enable, dvi_disable\n");
4640 len
+= sprintf(p
+ len
, "commands:\tauto_enable, auto_disable\n");
4641 len
+= sprintf(p
+ len
, "commands:\tvideo_switch, expand_toggle\n");
4646 static int video_write(char *buf
)
4649 int enable
, disable
, status
;
4652 if (video_supported
== TPACPI_VIDEO_NONE
)
4658 while ((cmd
= next_cmd(&buf
))) {
4659 if (strlencmp(cmd
, "lcd_enable") == 0) {
4660 enable
|= TP_ACPI_VIDEO_S_LCD
;
4661 } else if (strlencmp(cmd
, "lcd_disable") == 0) {
4662 disable
|= TP_ACPI_VIDEO_S_LCD
;
4663 } else if (strlencmp(cmd
, "crt_enable") == 0) {
4664 enable
|= TP_ACPI_VIDEO_S_CRT
;
4665 } else if (strlencmp(cmd
, "crt_disable") == 0) {
4666 disable
|= TP_ACPI_VIDEO_S_CRT
;
4667 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4668 strlencmp(cmd
, "dvi_enable") == 0) {
4669 enable
|= TP_ACPI_VIDEO_S_DVI
;
4670 } else if (video_supported
== TPACPI_VIDEO_NEW
&&
4671 strlencmp(cmd
, "dvi_disable") == 0) {
4672 disable
|= TP_ACPI_VIDEO_S_DVI
;
4673 } else if (strlencmp(cmd
, "auto_enable") == 0) {
4674 res
= video_autosw_set(1);
4677 } else if (strlencmp(cmd
, "auto_disable") == 0) {
4678 res
= video_autosw_set(0);
4681 } else if (strlencmp(cmd
, "video_switch") == 0) {
4682 res
= video_outputsw_cycle();
4685 } else if (strlencmp(cmd
, "expand_toggle") == 0) {
4686 res
= video_expand_toggle();
4693 if (enable
|| disable
) {
4694 status
= video_outputsw_get();
4697 res
= video_outputsw_set((status
& ~disable
) | enable
);
4705 static struct ibm_struct video_driver_data
= {
4708 .write
= video_write
,
4712 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4714 /*************************************************************************
4715 * Light (thinklight) subdriver
4718 TPACPI_HANDLE(lght
, root
, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4719 TPACPI_HANDLE(ledb
, ec
, "LEDB"); /* G4x */
4721 static int light_get_status(void)
4725 if (tp_features
.light_status
) {
4726 if (!acpi_evalf(ec_handle
, &status
, "KBLT", "d"))
4734 static int light_set_status(int status
)
4738 if (tp_features
.light
) {
4740 rc
= acpi_evalf(cmos_handle
, NULL
, NULL
, "vd",
4742 TP_CMOS_THINKLIGHT_ON
:
4743 TP_CMOS_THINKLIGHT_OFF
);
4745 rc
= acpi_evalf(lght_handle
, NULL
, NULL
, "vd",
4748 return (rc
)? 0 : -EIO
;
4754 static void light_set_status_worker(struct work_struct
*work
)
4756 struct tpacpi_led_classdev
*data
=
4757 container_of(work
, struct tpacpi_led_classdev
, work
);
4759 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
4760 light_set_status((data
->new_state
!= TPACPI_LED_OFF
));
4763 static void light_sysfs_set(struct led_classdev
*led_cdev
,
4764 enum led_brightness brightness
)
4766 struct tpacpi_led_classdev
*data
=
4767 container_of(led_cdev
,
4768 struct tpacpi_led_classdev
,
4770 data
->new_state
= (brightness
!= LED_OFF
) ?
4771 TPACPI_LED_ON
: TPACPI_LED_OFF
;
4772 queue_work(tpacpi_wq
, &data
->work
);
4775 static enum led_brightness
light_sysfs_get(struct led_classdev
*led_cdev
)
4777 return (light_get_status() == 1)? LED_FULL
: LED_OFF
;
4780 static struct tpacpi_led_classdev tpacpi_led_thinklight
= {
4782 .name
= "tpacpi::thinklight",
4783 .brightness_set
= &light_sysfs_set
,
4784 .brightness_get
= &light_sysfs_get
,
4788 static int __init
light_init(struct ibm_init_struct
*iibm
)
4792 vdbg_printk(TPACPI_DBG_INIT
, "initializing light subdriver\n");
4794 TPACPI_ACPIHANDLE_INIT(ledb
);
4795 TPACPI_ACPIHANDLE_INIT(lght
);
4796 TPACPI_ACPIHANDLE_INIT(cmos
);
4797 INIT_WORK(&tpacpi_led_thinklight
.work
, light_set_status_worker
);
4799 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4800 tp_features
.light
= (cmos_handle
|| lght_handle
) && !ledb_handle
;
4802 if (tp_features
.light
)
4803 /* light status not supported on
4804 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4805 tp_features
.light_status
=
4806 acpi_evalf(ec_handle
, NULL
, "KBLT", "qv");
4808 vdbg_printk(TPACPI_DBG_INIT
, "light is %s, light status is %s\n",
4809 str_supported(tp_features
.light
),
4810 str_supported(tp_features
.light_status
));
4812 if (!tp_features
.light
)
4815 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
4816 &tpacpi_led_thinklight
.led_classdev
);
4819 tp_features
.light
= 0;
4820 tp_features
.light_status
= 0;
4828 static void light_exit(void)
4830 led_classdev_unregister(&tpacpi_led_thinklight
.led_classdev
);
4831 if (work_pending(&tpacpi_led_thinklight
.work
))
4832 flush_workqueue(tpacpi_wq
);
4835 static int light_read(char *p
)
4840 if (!tp_features
.light
) {
4841 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4842 } else if (!tp_features
.light_status
) {
4843 len
+= sprintf(p
+ len
, "status:\t\tunknown\n");
4844 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4846 status
= light_get_status();
4849 len
+= sprintf(p
+ len
, "status:\t\t%s\n", onoff(status
, 0));
4850 len
+= sprintf(p
+ len
, "commands:\ton, off\n");
4856 static int light_write(char *buf
)
4861 if (!tp_features
.light
)
4864 while ((cmd
= next_cmd(&buf
))) {
4865 if (strlencmp(cmd
, "on") == 0) {
4867 } else if (strlencmp(cmd
, "off") == 0) {
4873 return light_set_status(newstatus
);
4876 static struct ibm_struct light_driver_data
= {
4879 .write
= light_write
,
4883 /*************************************************************************
4887 /* sysfs cmos_command -------------------------------------------------- */
4888 static ssize_t
cmos_command_store(struct device
*dev
,
4889 struct device_attribute
*attr
,
4890 const char *buf
, size_t count
)
4892 unsigned long cmos_cmd
;
4895 if (parse_strtoul(buf
, 21, &cmos_cmd
))
4898 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4899 return (res
)? res
: count
;
4902 static struct device_attribute dev_attr_cmos_command
=
4903 __ATTR(cmos_command
, S_IWUSR
, NULL
, cmos_command_store
);
4905 /* --------------------------------------------------------------------- */
4907 static int __init
cmos_init(struct ibm_init_struct
*iibm
)
4911 vdbg_printk(TPACPI_DBG_INIT
,
4912 "initializing cmos commands subdriver\n");
4914 TPACPI_ACPIHANDLE_INIT(cmos
);
4916 vdbg_printk(TPACPI_DBG_INIT
, "cmos commands are %s\n",
4917 str_supported(cmos_handle
!= NULL
));
4919 res
= device_create_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4923 return (cmos_handle
)? 0 : 1;
4926 static void cmos_exit(void)
4928 device_remove_file(&tpacpi_pdev
->dev
, &dev_attr_cmos_command
);
4931 static int cmos_read(char *p
)
4935 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4936 R30, R31, T20-22, X20-21 */
4938 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
4940 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
4941 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-21)\n");
4947 static int cmos_write(char *buf
)
4952 while ((cmd
= next_cmd(&buf
))) {
4953 if (sscanf(cmd
, "%u", &cmos_cmd
) == 1 &&
4954 cmos_cmd
>= 0 && cmos_cmd
<= 21) {
4959 res
= issue_thinkpad_cmos_command(cmos_cmd
);
4967 static struct ibm_struct cmos_driver_data
= {
4970 .write
= cmos_write
,
4974 /*************************************************************************
4978 enum led_access_mode
{
4979 TPACPI_LED_NONE
= 0,
4980 TPACPI_LED_570
, /* 570 */
4981 TPACPI_LED_OLD
, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4982 TPACPI_LED_NEW
, /* all others */
4985 enum { /* For TPACPI_LED_OLD */
4986 TPACPI_LED_EC_HLCL
= 0x0c, /* EC reg to get led to power on */
4987 TPACPI_LED_EC_HLBL
= 0x0d, /* EC reg to blink a lit led */
4988 TPACPI_LED_EC_HLMS
= 0x0e, /* EC reg to select led to command */
4991 static enum led_access_mode led_supported
;
4993 TPACPI_HANDLE(led
, ec
, "SLED", /* 570 */
4994 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4995 /* T20-22, X20-21 */
4996 "LED", /* all others */
4999 #define TPACPI_LED_NUMLEDS 16
5000 static struct tpacpi_led_classdev
*tpacpi_leds
;
5001 static enum led_status_t tpacpi_led_state_cache
[TPACPI_LED_NUMLEDS
];
5002 static const char * const tpacpi_led_names
[TPACPI_LED_NUMLEDS
] = {
5003 /* there's a limit of 19 chars + NULL before 2.6.26 */
5005 "tpacpi:orange:batt",
5006 "tpacpi:green:batt",
5007 "tpacpi::dock_active",
5008 "tpacpi::bay_active",
5009 "tpacpi::dock_batt",
5010 "tpacpi::unknown_led",
5012 "tpacpi::dock_status1",
5013 "tpacpi::dock_status2",
5014 "tpacpi::unknown_led2",
5015 "tpacpi::unknown_led3",
5016 "tpacpi::thinkvantage",
5018 #define TPACPI_SAFE_LEDS 0x1081U
5020 static inline bool tpacpi_is_led_restricted(const unsigned int led
)
5022 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5025 return (1U & (TPACPI_SAFE_LEDS
>> led
)) == 0;
5029 static int led_get_status(const unsigned int led
)
5032 enum led_status_t led_s
;
5034 switch (led_supported
) {
5035 case TPACPI_LED_570
:
5036 if (!acpi_evalf(ec_handle
,
5037 &status
, "GLED", "dd", 1 << led
))
5039 led_s
= (status
== 0)?
5044 tpacpi_led_state_cache
[led
] = led_s
;
5053 static int led_set_status(const unsigned int led
,
5054 const enum led_status_t ledstatus
)
5056 /* off, on, blink. Index is led_status_t */
5057 static const unsigned int led_sled_arg1
[] = { 0, 1, 3 };
5058 static const unsigned int led_led_arg1
[] = { 0, 0x80, 0xc0 };
5062 switch (led_supported
) {
5063 case TPACPI_LED_570
:
5065 if (unlikely(led
> 7))
5067 if (unlikely(tpacpi_is_led_restricted(led
)))
5069 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5070 (1 << led
), led_sled_arg1
[ledstatus
]))
5073 case TPACPI_LED_OLD
:
5074 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5075 if (unlikely(led
> 7))
5077 if (unlikely(tpacpi_is_led_restricted(led
)))
5079 rc
= ec_write(TPACPI_LED_EC_HLMS
, (1 << led
));
5081 rc
= ec_write(TPACPI_LED_EC_HLBL
,
5082 (ledstatus
== TPACPI_LED_BLINK
) << led
);
5084 rc
= ec_write(TPACPI_LED_EC_HLCL
,
5085 (ledstatus
!= TPACPI_LED_OFF
) << led
);
5087 case TPACPI_LED_NEW
:
5089 if (unlikely(led
>= TPACPI_LED_NUMLEDS
))
5091 if (unlikely(tpacpi_is_led_restricted(led
)))
5093 if (!acpi_evalf(led_handle
, NULL
, NULL
, "vdd",
5094 led
, led_led_arg1
[ledstatus
]))
5102 tpacpi_led_state_cache
[led
] = ledstatus
;
5107 static void led_set_status_worker(struct work_struct
*work
)
5109 struct tpacpi_led_classdev
*data
=
5110 container_of(work
, struct tpacpi_led_classdev
, work
);
5112 if (likely(tpacpi_lifecycle
== TPACPI_LIFE_RUNNING
))
5113 led_set_status(data
->led
, data
->new_state
);
5116 static void led_sysfs_set(struct led_classdev
*led_cdev
,
5117 enum led_brightness brightness
)
5119 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5120 struct tpacpi_led_classdev
, led_classdev
);
5122 if (brightness
== LED_OFF
)
5123 data
->new_state
= TPACPI_LED_OFF
;
5124 else if (tpacpi_led_state_cache
[data
->led
] != TPACPI_LED_BLINK
)
5125 data
->new_state
= TPACPI_LED_ON
;
5127 data
->new_state
= TPACPI_LED_BLINK
;
5129 queue_work(tpacpi_wq
, &data
->work
);
5132 static int led_sysfs_blink_set(struct led_classdev
*led_cdev
,
5133 unsigned long *delay_on
, unsigned long *delay_off
)
5135 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5136 struct tpacpi_led_classdev
, led_classdev
);
5138 /* Can we choose the flash rate? */
5139 if (*delay_on
== 0 && *delay_off
== 0) {
5140 /* yes. set them to the hardware blink rate (1 Hz) */
5141 *delay_on
= 500; /* ms */
5142 *delay_off
= 500; /* ms */
5143 } else if ((*delay_on
!= 500) || (*delay_off
!= 500))
5146 data
->new_state
= TPACPI_LED_BLINK
;
5147 queue_work(tpacpi_wq
, &data
->work
);
5152 static enum led_brightness
led_sysfs_get(struct led_classdev
*led_cdev
)
5156 struct tpacpi_led_classdev
*data
= container_of(led_cdev
,
5157 struct tpacpi_led_classdev
, led_classdev
);
5159 rc
= led_get_status(data
->led
);
5161 if (rc
== TPACPI_LED_OFF
|| rc
< 0)
5162 rc
= LED_OFF
; /* no error handling in led class :( */
5169 static void led_exit(void)
5173 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5174 if (tpacpi_leds
[i
].led_classdev
.name
)
5175 led_classdev_unregister(&tpacpi_leds
[i
].led_classdev
);
5181 static int __init
tpacpi_init_led(unsigned int led
)
5185 tpacpi_leds
[led
].led
= led
;
5187 /* LEDs with no name don't get registered */
5188 if (!tpacpi_led_names
[led
])
5191 tpacpi_leds
[led
].led_classdev
.brightness_set
= &led_sysfs_set
;
5192 tpacpi_leds
[led
].led_classdev
.blink_set
= &led_sysfs_blink_set
;
5193 if (led_supported
== TPACPI_LED_570
)
5194 tpacpi_leds
[led
].led_classdev
.brightness_get
=
5197 tpacpi_leds
[led
].led_classdev
.name
= tpacpi_led_names
[led
];
5199 INIT_WORK(&tpacpi_leds
[led
].work
, led_set_status_worker
);
5201 rc
= led_classdev_register(&tpacpi_pdev
->dev
,
5202 &tpacpi_leds
[led
].led_classdev
);
5204 tpacpi_leds
[led
].led_classdev
.name
= NULL
;
5209 static const struct tpacpi_quirk led_useful_qtable
[] __initconst
= {
5210 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5211 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5212 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5214 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5215 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5216 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5217 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5218 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5219 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5220 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5221 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5223 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5224 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5225 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5226 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5227 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5229 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5230 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5231 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5232 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5234 /* (1) - may have excess leds enabled on MSB */
5236 /* Defaults (order matters, keep last, don't reorder!) */
5238 .vendor
= PCI_VENDOR_ID_LENOVO
,
5239 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5242 { /* IBM ThinkPads with no EC version string */
5243 .vendor
= PCI_VENDOR_ID_IBM
,
5244 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_UNKNOWN
,
5247 { /* IBM ThinkPads with EC version string */
5248 .vendor
= PCI_VENDOR_ID_IBM
,
5249 .bios
= TPACPI_MATCH_ANY
, .ec
= TPACPI_MATCH_ANY
,
5254 #undef TPACPI_LEDQ_IBM
5255 #undef TPACPI_LEDQ_LNV
5257 static int __init
led_init(struct ibm_init_struct
*iibm
)
5261 unsigned long useful_leds
;
5263 vdbg_printk(TPACPI_DBG_INIT
, "initializing LED subdriver\n");
5265 TPACPI_ACPIHANDLE_INIT(led
);
5268 /* led not supported on R30, R31 */
5269 led_supported
= TPACPI_LED_NONE
;
5270 else if (strlencmp(led_path
, "SLED") == 0)
5272 led_supported
= TPACPI_LED_570
;
5273 else if (strlencmp(led_path
, "SYSL") == 0)
5274 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5275 led_supported
= TPACPI_LED_OLD
;
5278 led_supported
= TPACPI_LED_NEW
;
5280 vdbg_printk(TPACPI_DBG_INIT
, "LED commands are %s, mode %d\n",
5281 str_supported(led_supported
), led_supported
);
5283 if (led_supported
== TPACPI_LED_NONE
)
5286 tpacpi_leds
= kzalloc(sizeof(*tpacpi_leds
) * TPACPI_LED_NUMLEDS
,
5289 printk(TPACPI_ERR
"Out of memory for LED data\n");
5293 useful_leds
= tpacpi_check_quirks(led_useful_qtable
,
5294 ARRAY_SIZE(led_useful_qtable
));
5296 for (i
= 0; i
< TPACPI_LED_NUMLEDS
; i
++) {
5297 if (!tpacpi_is_led_restricted(i
) &&
5298 test_bit(i
, &useful_leds
)) {
5299 rc
= tpacpi_init_led(i
);
5307 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5308 printk(TPACPI_NOTICE
5309 "warning: userspace override of important "
5310 "firmware LEDs is enabled\n");
5315 #define str_led_status(s) \
5316 ((s) == TPACPI_LED_OFF ? "off" : \
5317 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5319 static int led_read(char *p
)
5323 if (!led_supported
) {
5324 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5327 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5329 if (led_supported
== TPACPI_LED_570
) {
5332 for (i
= 0; i
< 8; i
++) {
5333 status
= led_get_status(i
);
5336 len
+= sprintf(p
+ len
, "%d:\t\t%s\n",
5337 i
, str_led_status(status
));
5341 len
+= sprintf(p
+ len
, "commands:\t"
5342 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5347 static int led_write(char *buf
)
5351 enum led_status_t s
;
5356 while ((cmd
= next_cmd(&buf
))) {
5357 if (sscanf(cmd
, "%d", &led
) != 1 || led
< 0 || led
> 15)
5360 if (strstr(cmd
, "off")) {
5362 } else if (strstr(cmd
, "on")) {
5364 } else if (strstr(cmd
, "blink")) {
5365 s
= TPACPI_LED_BLINK
;
5370 rc
= led_set_status(led
, s
);
5378 static struct ibm_struct led_driver_data
= {
5385 /*************************************************************************
5389 TPACPI_HANDLE(beep
, ec
, "BEEP"); /* all except R30, R31 */
5391 #define TPACPI_BEEP_Q1 0x0001
5393 static const struct tpacpi_quirk beep_quirk_table
[] __initconst
= {
5394 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1
), /* 570 */
5395 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1
), /* 570E - unverified */
5398 static int __init
beep_init(struct ibm_init_struct
*iibm
)
5400 unsigned long quirks
;
5402 vdbg_printk(TPACPI_DBG_INIT
, "initializing beep subdriver\n");
5404 TPACPI_ACPIHANDLE_INIT(beep
);
5406 vdbg_printk(TPACPI_DBG_INIT
, "beep is %s\n",
5407 str_supported(beep_handle
!= NULL
));
5409 quirks
= tpacpi_check_quirks(beep_quirk_table
,
5410 ARRAY_SIZE(beep_quirk_table
));
5412 tp_features
.beep_needs_two_args
= !!(quirks
& TPACPI_BEEP_Q1
);
5414 return (beep_handle
)? 0 : 1;
5417 static int beep_read(char *p
)
5422 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
5424 len
+= sprintf(p
+ len
, "status:\t\tsupported\n");
5425 len
+= sprintf(p
+ len
, "commands:\t<cmd> (<cmd> is 0-17)\n");
5431 static int beep_write(char *buf
)
5439 while ((cmd
= next_cmd(&buf
))) {
5440 if (sscanf(cmd
, "%u", &beep_cmd
) == 1 &&
5441 beep_cmd
>= 0 && beep_cmd
<= 17) {
5445 if (tp_features
.beep_needs_two_args
) {
5446 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vdd",
5450 if (!acpi_evalf(beep_handle
, NULL
, NULL
, "vd",
5459 static struct ibm_struct beep_driver_data
= {
5462 .write
= beep_write
,
5465 /*************************************************************************
5469 enum thermal_access_mode
{
5470 TPACPI_THERMAL_NONE
= 0, /* No thermal support */
5471 TPACPI_THERMAL_ACPI_TMP07
, /* Use ACPI TMP0-7 */
5472 TPACPI_THERMAL_ACPI_UPDT
, /* Use ACPI TMP0-7 with UPDT */
5473 TPACPI_THERMAL_TPEC_8
, /* Use ACPI EC regs, 8 sensors */
5474 TPACPI_THERMAL_TPEC_16
, /* Use ACPI EC regs, 16 sensors */
5477 enum { /* TPACPI_THERMAL_TPEC_* */
5478 TP_EC_THERMAL_TMP0
= 0x78, /* ACPI EC regs TMP 0..7 */
5479 TP_EC_THERMAL_TMP8
= 0xC0, /* ACPI EC regs TMP 8..15 */
5480 TP_EC_THERMAL_TMP_NA
= -128, /* ACPI EC sensor not available */
5482 TPACPI_THERMAL_SENSOR_NA
= -128000, /* Sensor not available */
5486 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5487 struct ibm_thermal_sensors_struct
{
5488 s32 temp
[TPACPI_MAX_THERMAL_SENSORS
];
5491 static enum thermal_access_mode thermal_read_mode
;
5493 /* idx is zero-based */
5494 static int thermal_get_sensor(int idx
, s32
*value
)
5500 t
= TP_EC_THERMAL_TMP0
;
5502 switch (thermal_read_mode
) {
5503 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5504 case TPACPI_THERMAL_TPEC_16
:
5505 if (idx
>= 8 && idx
<= 15) {
5506 t
= TP_EC_THERMAL_TMP8
;
5511 case TPACPI_THERMAL_TPEC_8
:
5513 if (!acpi_ec_read(t
+ idx
, &tmp
))
5515 *value
= tmp
* 1000;
5520 case TPACPI_THERMAL_ACPI_UPDT
:
5522 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5523 if (!acpi_evalf(ec_handle
, NULL
, "UPDT", "v"))
5525 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5527 *value
= (t
- 2732) * 100;
5532 case TPACPI_THERMAL_ACPI_TMP07
:
5534 snprintf(tmpi
, sizeof(tmpi
), "TMP%c", '0' + idx
);
5535 if (!acpi_evalf(ec_handle
, &t
, tmpi
, "d"))
5537 if (t
> 127 || t
< -127)
5538 t
= TP_EC_THERMAL_TMP_NA
;
5544 case TPACPI_THERMAL_NONE
:
5552 static int thermal_get_sensors(struct ibm_thermal_sensors_struct
*s
)
5563 if (thermal_read_mode
== TPACPI_THERMAL_TPEC_16
)
5566 for (i
= 0 ; i
< n
; i
++) {
5567 res
= thermal_get_sensor(i
, &s
->temp
[i
]);
5575 static void thermal_dump_all_sensors(void)
5578 struct ibm_thermal_sensors_struct t
;
5580 n
= thermal_get_sensors(&t
);
5584 printk(TPACPI_NOTICE
5585 "temperatures (Celsius):");
5587 for (i
= 0; i
< n
; i
++) {
5588 if (t
.temp
[i
] != TPACPI_THERMAL_SENSOR_NA
)
5589 printk(KERN_CONT
" %d", (int)(t
.temp
[i
] / 1000));
5591 printk(KERN_CONT
" N/A");
5594 printk(KERN_CONT
"\n");
5597 /* sysfs temp##_input -------------------------------------------------- */
5599 static ssize_t
thermal_temp_input_show(struct device
*dev
,
5600 struct device_attribute
*attr
,
5603 struct sensor_device_attribute
*sensor_attr
=
5604 to_sensor_dev_attr(attr
);
5605 int idx
= sensor_attr
->index
;
5609 res
= thermal_get_sensor(idx
, &value
);
5612 if (value
== TPACPI_THERMAL_SENSOR_NA
)
5615 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
5618 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5619 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5620 thermal_temp_input_show, NULL, _idxB)
5622 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input
[] = {
5623 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5624 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5625 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5626 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5627 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5628 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5629 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5630 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5631 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5632 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5633 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5634 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5635 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5636 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5637 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5638 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5641 #define THERMAL_ATTRS(X) \
5642 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5644 static struct attribute
*thermal_temp_input_attr
[] = {
5664 static const struct attribute_group thermal_temp_input16_group
= {
5665 .attrs
= thermal_temp_input_attr
5668 static const struct attribute_group thermal_temp_input8_group
= {
5669 .attrs
= &thermal_temp_input_attr
[8]
5672 #undef THERMAL_SENSOR_ATTR_TEMP
5673 #undef THERMAL_ATTRS
5675 /* --------------------------------------------------------------------- */
5677 static int __init
thermal_init(struct ibm_init_struct
*iibm
)
5684 vdbg_printk(TPACPI_DBG_INIT
, "initializing thermal subdriver\n");
5686 acpi_tmp7
= acpi_evalf(ec_handle
, NULL
, "TMP7", "qv");
5688 if (thinkpad_id
.ec_model
) {
5690 * Direct EC access mode: sensors at registers
5691 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5692 * non-implemented, thermal sensors return 0x80 when
5697 for (i
= 0; i
< 8; i
++) {
5698 if (acpi_ec_read(TP_EC_THERMAL_TMP0
+ i
, &t
)) {
5704 if (acpi_ec_read(TP_EC_THERMAL_TMP8
+ i
, &t
)) {
5712 /* This is sheer paranoia, but we handle it anyway */
5715 "ThinkPad ACPI EC access misbehaving, "
5716 "falling back to ACPI TMPx access "
5718 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5721 "ThinkPad ACPI EC access misbehaving, "
5722 "disabling thermal sensors access\n");
5723 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5728 TPACPI_THERMAL_TPEC_16
: TPACPI_THERMAL_TPEC_8
;
5730 } else if (acpi_tmp7
) {
5731 if (acpi_evalf(ec_handle
, NULL
, "UPDT", "qv")) {
5732 /* 600e/x, 770e, 770x */
5733 thermal_read_mode
= TPACPI_THERMAL_ACPI_UPDT
;
5735 /* Standard ACPI TMPx access, max 8 sensors */
5736 thermal_read_mode
= TPACPI_THERMAL_ACPI_TMP07
;
5739 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5740 thermal_read_mode
= TPACPI_THERMAL_NONE
;
5743 vdbg_printk(TPACPI_DBG_INIT
, "thermal is %s, mode %d\n",
5744 str_supported(thermal_read_mode
!= TPACPI_THERMAL_NONE
),
5747 switch (thermal_read_mode
) {
5748 case TPACPI_THERMAL_TPEC_16
:
5749 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5750 &thermal_temp_input16_group
);
5754 case TPACPI_THERMAL_TPEC_8
:
5755 case TPACPI_THERMAL_ACPI_TMP07
:
5756 case TPACPI_THERMAL_ACPI_UPDT
:
5757 res
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5758 &thermal_temp_input8_group
);
5762 case TPACPI_THERMAL_NONE
:
5770 static void thermal_exit(void)
5772 switch (thermal_read_mode
) {
5773 case TPACPI_THERMAL_TPEC_16
:
5774 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5775 &thermal_temp_input16_group
);
5777 case TPACPI_THERMAL_TPEC_8
:
5778 case TPACPI_THERMAL_ACPI_TMP07
:
5779 case TPACPI_THERMAL_ACPI_UPDT
:
5780 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
5781 &thermal_temp_input16_group
);
5783 case TPACPI_THERMAL_NONE
:
5789 static int thermal_read(char *p
)
5793 struct ibm_thermal_sensors_struct t
;
5795 n
= thermal_get_sensors(&t
);
5796 if (unlikely(n
< 0))
5799 len
+= sprintf(p
+ len
, "temperatures:\t");
5802 for (i
= 0; i
< (n
- 1); i
++)
5803 len
+= sprintf(p
+ len
, "%d ", t
.temp
[i
] / 1000);
5804 len
+= sprintf(p
+ len
, "%d\n", t
.temp
[i
] / 1000);
5806 len
+= sprintf(p
+ len
, "not supported\n");
5811 static struct ibm_struct thermal_driver_data
= {
5813 .read
= thermal_read
,
5814 .exit
= thermal_exit
,
5817 /*************************************************************************
5821 static u8 ecdump_regs
[256];
5823 static int ecdump_read(char *p
)
5829 len
+= sprintf(p
+ len
, "EC "
5830 " +00 +01 +02 +03 +04 +05 +06 +07"
5831 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5832 for (i
= 0; i
< 256; i
+= 16) {
5833 len
+= sprintf(p
+ len
, "EC 0x%02x:", i
);
5834 for (j
= 0; j
< 16; j
++) {
5835 if (!acpi_ec_read(i
+ j
, &v
))
5837 if (v
!= ecdump_regs
[i
+ j
])
5838 len
+= sprintf(p
+ len
, " *%02x", v
);
5840 len
+= sprintf(p
+ len
, " %02x", v
);
5841 ecdump_regs
[i
+ j
] = v
;
5843 len
+= sprintf(p
+ len
, "\n");
5848 /* These are way too dangerous to advertise openly... */
5850 len
+= sprintf(p
+ len
, "commands:\t0x<offset> 0x<value>"
5851 " (<offset> is 00-ff, <value> is 00-ff)\n");
5852 len
+= sprintf(p
+ len
, "commands:\t0x<offset> <value> "
5853 " (<offset> is 00-ff, <value> is 0-255)\n");
5858 static int ecdump_write(char *buf
)
5863 while ((cmd
= next_cmd(&buf
))) {
5864 if (sscanf(cmd
, "0x%x 0x%x", &i
, &v
) == 2) {
5866 } else if (sscanf(cmd
, "0x%x %u", &i
, &v
) == 2) {
5870 if (i
>= 0 && i
< 256 && v
>= 0 && v
< 256) {
5871 if (!acpi_ec_write(i
, v
))
5880 static struct ibm_struct ecdump_driver_data
= {
5882 .read
= ecdump_read
,
5883 .write
= ecdump_write
,
5884 .flags
.experimental
= 1,
5887 /*************************************************************************
5888 * Backlight/brightness subdriver
5891 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5894 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5895 * CMOS NVRAM byte 0x5E, bits 0-3.
5897 * EC HBRV (0x31) has the following layout
5898 * Bit 7: unknown function
5899 * Bit 6: unknown function
5900 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5901 * Bit 4: must be set to zero to avoid problems
5902 * Bit 3-0: backlight brightness level
5904 * brightness_get_raw returns status data in the HBRV layout
5906 * WARNING: The X61 has been verified to use HBRV for something else, so
5907 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5908 * testing on the very early *60 Lenovo models...
5912 TP_EC_BACKLIGHT
= 0x31,
5914 /* TP_EC_BACKLIGHT bitmasks */
5915 TP_EC_BACKLIGHT_LVLMSK
= 0x1F,
5916 TP_EC_BACKLIGHT_CMDMSK
= 0xE0,
5917 TP_EC_BACKLIGHT_MAPSW
= 0x20,
5920 enum tpacpi_brightness_access_mode
{
5921 TPACPI_BRGHT_MODE_AUTO
= 0, /* Not implemented yet */
5922 TPACPI_BRGHT_MODE_EC
, /* EC control */
5923 TPACPI_BRGHT_MODE_UCMS_STEP
, /* UCMS step-based control */
5924 TPACPI_BRGHT_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
5925 TPACPI_BRGHT_MODE_MAX
5928 static struct backlight_device
*ibm_backlight_device
;
5930 static enum tpacpi_brightness_access_mode brightness_mode
=
5931 TPACPI_BRGHT_MODE_MAX
;
5933 static unsigned int brightness_enable
= 2; /* 2 = auto, 0 = no, 1 = yes */
5935 static struct mutex brightness_mutex
;
5937 /* NVRAM brightness access,
5938 * call with brightness_mutex held! */
5939 static unsigned int tpacpi_brightness_nvram_get(void)
5943 lnvram
= (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
)
5944 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5945 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
;
5946 lnvram
&= (tp_features
.bright_16levels
) ? 0x0f : 0x07;
5951 static void tpacpi_brightness_checkpoint_nvram(void)
5956 if (brightness_mode
!= TPACPI_BRGHT_MODE_ECNVRAM
)
5959 vdbg_printk(TPACPI_DBG_BRGHT
,
5960 "trying to checkpoint backlight level to NVRAM...\n");
5962 if (mutex_lock_killable(&brightness_mutex
) < 0)
5965 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
5967 lec
&= TP_EC_BACKLIGHT_LVLMSK
;
5968 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS
);
5970 if (lec
!= ((b_nvram
& TP_NVRAM_MASK_LEVEL_BRIGHTNESS
)
5971 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS
)) {
5972 /* NVRAM needs update */
5973 b_nvram
&= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS
<<
5974 TP_NVRAM_POS_LEVEL_BRIGHTNESS
);
5976 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_BRIGHTNESS
);
5977 dbg_printk(TPACPI_DBG_BRGHT
,
5978 "updated NVRAM backlight level to %u (0x%02x)\n",
5979 (unsigned int) lec
, (unsigned int) b_nvram
);
5981 vdbg_printk(TPACPI_DBG_BRGHT
,
5982 "NVRAM backlight level already is %u (0x%02x)\n",
5983 (unsigned int) lec
, (unsigned int) b_nvram
);
5986 mutex_unlock(&brightness_mutex
);
5990 /* call with brightness_mutex held! */
5991 static int tpacpi_brightness_get_raw(int *status
)
5995 switch (brightness_mode
) {
5996 case TPACPI_BRGHT_MODE_UCMS_STEP
:
5997 *status
= tpacpi_brightness_nvram_get();
5999 case TPACPI_BRGHT_MODE_EC
:
6000 case TPACPI_BRGHT_MODE_ECNVRAM
:
6001 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6010 /* call with brightness_mutex held! */
6011 /* do NOT call with illegal backlight level value */
6012 static int tpacpi_brightness_set_ec(unsigned int value
)
6016 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT
, &lec
)))
6019 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT
,
6020 (lec
& TP_EC_BACKLIGHT_CMDMSK
) |
6021 (value
& TP_EC_BACKLIGHT_LVLMSK
))))
6027 /* call with brightness_mutex held! */
6028 static int tpacpi_brightness_set_ucmsstep(unsigned int value
)
6031 unsigned int current_value
, i
;
6033 current_value
= tpacpi_brightness_nvram_get();
6035 if (value
== current_value
)
6038 cmos_cmd
= (value
> current_value
) ?
6039 TP_CMOS_BRIGHTNESS_UP
:
6040 TP_CMOS_BRIGHTNESS_DOWN
;
6041 inc
= (value
> current_value
) ? 1 : -1;
6043 for (i
= current_value
; i
!= value
; i
+= inc
)
6044 if (issue_thinkpad_cmos_command(cmos_cmd
))
6050 /* May return EINTR which can always be mapped to ERESTARTSYS */
6051 static int brightness_set(unsigned int value
)
6055 if (value
> ((tp_features
.bright_16levels
)? 15 : 7) ||
6059 vdbg_printk(TPACPI_DBG_BRGHT
,
6060 "set backlight level to %d\n", value
);
6062 res
= mutex_lock_killable(&brightness_mutex
);
6066 switch (brightness_mode
) {
6067 case TPACPI_BRGHT_MODE_EC
:
6068 case TPACPI_BRGHT_MODE_ECNVRAM
:
6069 res
= tpacpi_brightness_set_ec(value
);
6071 case TPACPI_BRGHT_MODE_UCMS_STEP
:
6072 res
= tpacpi_brightness_set_ucmsstep(value
);
6078 mutex_unlock(&brightness_mutex
);
6082 /* sysfs backlight class ----------------------------------------------- */
6084 static int brightness_update_status(struct backlight_device
*bd
)
6086 unsigned int level
=
6087 (bd
->props
.fb_blank
== FB_BLANK_UNBLANK
&&
6088 bd
->props
.power
== FB_BLANK_UNBLANK
) ?
6089 bd
->props
.brightness
: 0;
6091 dbg_printk(TPACPI_DBG_BRGHT
,
6092 "backlight: attempt to set level to %d\n",
6095 /* it is the backlight class's job (caller) to handle
6096 * EINTR and other errors properly */
6097 return brightness_set(level
);
6100 static int brightness_get(struct backlight_device
*bd
)
6104 res
= mutex_lock_killable(&brightness_mutex
);
6108 res
= tpacpi_brightness_get_raw(&status
);
6110 mutex_unlock(&brightness_mutex
);
6115 return status
& TP_EC_BACKLIGHT_LVLMSK
;
6118 static void tpacpi_brightness_notify_change(void)
6120 backlight_force_update(ibm_backlight_device
,
6121 BACKLIGHT_UPDATE_HOTKEY
);
6124 static struct backlight_ops ibm_backlight_data
= {
6125 .get_brightness
= brightness_get
,
6126 .update_status
= brightness_update_status
,
6129 /* --------------------------------------------------------------------- */
6132 * These are only useful for models that have only one possibility
6133 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6136 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6137 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6138 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6140 static const struct tpacpi_quirk brightness_quirk_table
[] __initconst
= {
6141 /* Models with ATI GPUs known to require ECNVRAM mode */
6142 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC
), /* T43/p ATI */
6144 /* Models with ATI GPUs that can use ECNVRAM */
6145 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC
),
6146 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6147 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6148 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6150 /* Models with Intel Extreme Graphics 2 */
6151 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC
),
6152 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6153 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK
|TPACPI_BRGHT_Q_EC
),
6155 /* Models with Intel GMA900 */
6156 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC
), /* T43, R52 */
6157 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC
), /* X41 */
6158 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC
), /* X41 Tablet */
6161 static int __init
brightness_init(struct ibm_init_struct
*iibm
)
6164 unsigned long quirks
;
6166 vdbg_printk(TPACPI_DBG_INIT
, "initializing brightness subdriver\n");
6168 mutex_init(&brightness_mutex
);
6170 quirks
= tpacpi_check_quirks(brightness_quirk_table
,
6171 ARRAY_SIZE(brightness_quirk_table
));
6174 * We always attempt to detect acpi support, so as to switch
6175 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6176 * going to publish a backlight interface
6178 b
= tpacpi_check_std_acpi_brightness_support();
6181 if (acpi_video_backlight_support()) {
6182 if (brightness_enable
> 1) {
6183 printk(TPACPI_NOTICE
6184 "Standard ACPI backlight interface "
6185 "available, not loading native one.\n");
6187 } else if (brightness_enable
== 1) {
6188 printk(TPACPI_NOTICE
6189 "Backlight control force enabled, even if standard "
6190 "ACPI backlight interface is available\n");
6193 if (brightness_enable
> 1) {
6194 printk(TPACPI_NOTICE
6195 "Standard ACPI backlight interface not "
6196 "available, thinkpad_acpi native "
6197 "brightness control enabled\n");
6202 if (!brightness_enable
) {
6203 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6204 "brightness support disabled by "
6205 "module parameter\n");
6211 "Unsupported brightness interface, "
6212 "please contact %s\n", TPACPI_MAIL
);
6216 tp_features
.bright_16levels
= 1;
6219 * Check for module parameter bogosity, note that we
6220 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6221 * able to detect "unspecified"
6223 if (brightness_mode
> TPACPI_BRGHT_MODE_MAX
)
6226 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6227 if (brightness_mode
== TPACPI_BRGHT_MODE_AUTO
||
6228 brightness_mode
== TPACPI_BRGHT_MODE_MAX
) {
6229 if (quirks
& TPACPI_BRGHT_Q_EC
)
6230 brightness_mode
= TPACPI_BRGHT_MODE_ECNVRAM
;
6232 brightness_mode
= TPACPI_BRGHT_MODE_UCMS_STEP
;
6234 dbg_printk(TPACPI_DBG_BRGHT
,
6235 "driver auto-selected brightness_mode=%d\n",
6240 if (thinkpad_id
.vendor
!= PCI_VENDOR_ID_IBM
&&
6241 (brightness_mode
== TPACPI_BRGHT_MODE_ECNVRAM
||
6242 brightness_mode
== TPACPI_BRGHT_MODE_EC
))
6245 if (tpacpi_brightness_get_raw(&b
) < 0)
6248 if (tp_features
.bright_16levels
)
6250 "detected a 16-level brightness capable ThinkPad\n");
6252 ibm_backlight_device
= backlight_device_register(
6253 TPACPI_BACKLIGHT_DEV_NAME
, NULL
, NULL
,
6254 &ibm_backlight_data
);
6255 if (IS_ERR(ibm_backlight_device
)) {
6256 int rc
= PTR_ERR(ibm_backlight_device
);
6257 ibm_backlight_device
= NULL
;
6258 printk(TPACPI_ERR
"Could not register backlight device\n");
6261 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6262 "brightness is supported\n");
6264 if (quirks
& TPACPI_BRGHT_Q_ASK
) {
6265 printk(TPACPI_NOTICE
6266 "brightness: will use unverified default: "
6267 "brightness_mode=%d\n", brightness_mode
);
6268 printk(TPACPI_NOTICE
6269 "brightness: please report to %s whether it works well "
6270 "or not on your ThinkPad\n", TPACPI_MAIL
);
6273 ibm_backlight_device
->props
.max_brightness
=
6274 (tp_features
.bright_16levels
)? 15 : 7;
6275 ibm_backlight_device
->props
.brightness
= b
& TP_EC_BACKLIGHT_LVLMSK
;
6276 backlight_update_status(ibm_backlight_device
);
6278 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_BRGHT
,
6279 "brightness: registering brightness hotkeys "
6280 "as change notification\n");
6281 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6282 | TP_ACPI_HKEY_BRGHTUP_MASK
6283 | TP_ACPI_HKEY_BRGHTDWN_MASK
);;
6287 static void brightness_suspend(pm_message_t state
)
6289 tpacpi_brightness_checkpoint_nvram();
6292 static void brightness_shutdown(void)
6294 tpacpi_brightness_checkpoint_nvram();
6297 static void brightness_exit(void)
6299 if (ibm_backlight_device
) {
6300 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_BRGHT
,
6301 "calling backlight_device_unregister()\n");
6302 backlight_device_unregister(ibm_backlight_device
);
6305 tpacpi_brightness_checkpoint_nvram();
6308 static int brightness_read(char *p
)
6313 level
= brightness_get(NULL
);
6315 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6317 len
+= sprintf(p
+ len
, "level:\t\t%d\n", level
);
6318 len
+= sprintf(p
+ len
, "commands:\tup, down\n");
6319 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
6320 " (<level> is 0-%d)\n",
6321 (tp_features
.bright_16levels
) ? 15 : 7);
6327 static int brightness_write(char *buf
)
6332 int max_level
= (tp_features
.bright_16levels
) ? 15 : 7;
6334 level
= brightness_get(NULL
);
6338 while ((cmd
= next_cmd(&buf
))) {
6339 if (strlencmp(cmd
, "up") == 0) {
6340 if (level
< max_level
)
6342 } else if (strlencmp(cmd
, "down") == 0) {
6345 } else if (sscanf(cmd
, "level %d", &level
) == 1 &&
6346 level
>= 0 && level
<= max_level
) {
6352 tpacpi_disclose_usertask("procfs brightness",
6353 "set level to %d\n", level
);
6356 * Now we know what the final level should be, so we try to set it.
6357 * Doing it this way makes the syscall restartable in case of EINTR
6359 rc
= brightness_set(level
);
6360 if (!rc
&& ibm_backlight_device
)
6361 backlight_force_update(ibm_backlight_device
,
6362 BACKLIGHT_UPDATE_SYSFS
);
6363 return (rc
== -EINTR
)? -ERESTARTSYS
: rc
;
6366 static struct ibm_struct brightness_driver_data
= {
6367 .name
= "brightness",
6368 .read
= brightness_read
,
6369 .write
= brightness_write
,
6370 .exit
= brightness_exit
,
6371 .suspend
= brightness_suspend
,
6372 .shutdown
= brightness_shutdown
,
6375 /*************************************************************************
6380 * IBM ThinkPads have a simple volume controller with MUTE gating.
6381 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6383 * Since the *61 series (and probably also the later *60 series), Lenovo
6384 * ThinkPads only implement the MUTE gate.
6387 * Bit 6: MUTE (1 mutes sound)
6389 * Other bits should be zero as far as we know.
6391 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6392 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6393 * such as bit 7 which is used to detect repeated presses of MUTE,
6394 * and we leave them unchanged.
6400 /* TP_EC_AUDIO bits */
6401 TP_EC_AUDIO_MUTESW
= 6,
6403 /* TP_EC_AUDIO bitmasks */
6404 TP_EC_AUDIO_LVL_MSK
= 0x0F,
6405 TP_EC_AUDIO_MUTESW_MSK
= (1 << TP_EC_AUDIO_MUTESW
),
6407 /* Maximum volume */
6408 TP_EC_VOLUME_MAX
= 14,
6411 enum tpacpi_volume_access_mode
{
6412 TPACPI_VOL_MODE_AUTO
= 0, /* Not implemented yet */
6413 TPACPI_VOL_MODE_EC
, /* Pure EC control */
6414 TPACPI_VOL_MODE_UCMS_STEP
, /* UCMS step-based control: N/A */
6415 TPACPI_VOL_MODE_ECNVRAM
, /* EC control w/ NVRAM store */
6419 static enum tpacpi_volume_access_mode volume_mode
=
6420 TPACPI_VOL_MODE_MAX
;
6424 * Used to syncronize writers to TP_EC_AUDIO and
6425 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6427 static struct mutex volume_mutex
;
6429 static void tpacpi_volume_checkpoint_nvram(void)
6433 const u8 ec_mask
= TP_EC_AUDIO_LVL_MSK
| TP_EC_AUDIO_MUTESW_MSK
;
6435 if (volume_mode
!= TPACPI_VOL_MODE_ECNVRAM
)
6438 vdbg_printk(TPACPI_DBG_MIXER
,
6439 "trying to checkpoint mixer state to NVRAM...\n");
6441 if (mutex_lock_killable(&volume_mutex
) < 0)
6444 if (unlikely(!acpi_ec_read(TP_EC_AUDIO
, &lec
)))
6447 b_nvram
= nvram_read_byte(TP_NVRAM_ADDR_MIXER
);
6449 if (lec
!= (b_nvram
& ec_mask
)) {
6450 /* NVRAM needs update */
6451 b_nvram
&= ~ec_mask
;
6453 nvram_write_byte(b_nvram
, TP_NVRAM_ADDR_MIXER
);
6454 dbg_printk(TPACPI_DBG_MIXER
,
6455 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6456 (unsigned int) lec
, (unsigned int) b_nvram
);
6458 vdbg_printk(TPACPI_DBG_MIXER
,
6459 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6460 (unsigned int) lec
, (unsigned int) b_nvram
);
6464 mutex_unlock(&volume_mutex
);
6467 static int volume_get_status_ec(u8
*status
)
6471 if (!acpi_ec_read(TP_EC_AUDIO
, &s
))
6476 dbg_printk(TPACPI_DBG_MIXER
, "status 0x%02x\n", s
);
6481 static int volume_get_status(u8
*status
)
6483 return volume_get_status_ec(status
);
6486 static int volume_set_status_ec(const u8 status
)
6488 if (!acpi_ec_write(TP_EC_AUDIO
, status
))
6491 dbg_printk(TPACPI_DBG_MIXER
, "set EC mixer to 0x%02x\n", status
);
6496 static int volume_set_status(const u8 status
)
6498 return volume_set_status_ec(status
);
6501 static int volume_set_mute_ec(const bool mute
)
6506 if (mutex_lock_killable(&volume_mutex
) < 0)
6509 rc
= volume_get_status_ec(&s
);
6513 n
= (mute
) ? s
| TP_EC_AUDIO_MUTESW_MSK
:
6514 s
& ~TP_EC_AUDIO_MUTESW_MSK
;
6517 rc
= volume_set_status_ec(n
);
6520 mutex_unlock(&volume_mutex
);
6524 static int volume_set_mute(const bool mute
)
6526 dbg_printk(TPACPI_DBG_MIXER
, "trying to %smute\n",
6527 (mute
) ? "" : "un");
6528 return volume_set_mute_ec(mute
);
6531 static int volume_set_volume_ec(const u8 vol
)
6536 if (vol
> TP_EC_VOLUME_MAX
)
6539 if (mutex_lock_killable(&volume_mutex
) < 0)
6542 rc
= volume_get_status_ec(&s
);
6546 n
= (s
& ~TP_EC_AUDIO_LVL_MSK
) | vol
;
6549 rc
= volume_set_status_ec(n
);
6552 mutex_unlock(&volume_mutex
);
6556 static int volume_set_volume(const u8 vol
)
6558 dbg_printk(TPACPI_DBG_MIXER
,
6559 "trying to set volume level to %hu\n", vol
);
6560 return volume_set_volume_ec(vol
);
6563 static void volume_suspend(pm_message_t state
)
6565 tpacpi_volume_checkpoint_nvram();
6568 static void volume_shutdown(void)
6570 tpacpi_volume_checkpoint_nvram();
6573 static void volume_exit(void)
6575 tpacpi_volume_checkpoint_nvram();
6578 static int __init
volume_init(struct ibm_init_struct
*iibm
)
6580 vdbg_printk(TPACPI_DBG_INIT
, "initializing volume subdriver\n");
6582 mutex_init(&volume_mutex
);
6585 * Check for module parameter bogosity, note that we
6586 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6587 * able to detect "unspecified"
6589 if (volume_mode
> TPACPI_VOL_MODE_MAX
)
6592 if (volume_mode
== TPACPI_VOL_MODE_UCMS_STEP
) {
6594 "UCMS step volume mode not implemented, "
6595 "please contact %s\n", TPACPI_MAIL
);
6599 if (volume_mode
== TPACPI_VOL_MODE_AUTO
||
6600 volume_mode
== TPACPI_VOL_MODE_MAX
) {
6601 volume_mode
= TPACPI_VOL_MODE_ECNVRAM
;
6603 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6604 "driver auto-selected volume_mode=%d\n",
6607 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6608 "using user-supplied volume_mode=%d\n",
6612 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_MIXER
,
6613 "volume is supported\n");
6618 static int volume_read(char *p
)
6623 if (volume_get_status(&status
) < 0) {
6624 len
+= sprintf(p
+ len
, "level:\t\tunreadable\n");
6626 len
+= sprintf(p
+ len
, "level:\t\t%d\n",
6627 status
& TP_EC_AUDIO_LVL_MSK
);
6628 len
+= sprintf(p
+ len
, "mute:\t\t%s\n",
6629 onoff(status
, TP_EC_AUDIO_MUTESW
));
6630 len
+= sprintf(p
+ len
, "commands:\tup, down, mute\n");
6631 len
+= sprintf(p
+ len
, "commands:\tlevel <level>"
6632 " (<level> is 0-%d)\n", TP_EC_VOLUME_MAX
);
6638 static int volume_write(char *buf
)
6641 u8 new_level
, new_mute
;
6646 rc
= volume_get_status(&s
);
6650 new_level
= s
& TP_EC_AUDIO_LVL_MSK
;
6651 new_mute
= s
& TP_EC_AUDIO_MUTESW_MSK
;
6653 while ((cmd
= next_cmd(&buf
))) {
6654 if (strlencmp(cmd
, "up") == 0) {
6657 else if (new_level
< TP_EC_VOLUME_MAX
)
6659 } else if (strlencmp(cmd
, "down") == 0) {
6662 else if (new_level
> 0)
6664 } else if (sscanf(cmd
, "level %u", &l
) == 1 &&
6665 l
>= 0 && l
<= TP_EC_VOLUME_MAX
) {
6667 } else if (strlencmp(cmd
, "mute") == 0) {
6668 new_mute
= TP_EC_AUDIO_MUTESW_MSK
;
6673 tpacpi_disclose_usertask("procfs volume",
6674 "%smute and set level to %d\n",
6675 new_mute
? "" : "un", new_level
);
6677 rc
= volume_set_status(new_mute
| new_level
);
6679 return (rc
== -EINTR
) ? -ERESTARTSYS
: rc
;
6682 static struct ibm_struct volume_driver_data
= {
6684 .read
= volume_read
,
6685 .write
= volume_write
,
6686 .exit
= volume_exit
,
6687 .suspend
= volume_suspend
,
6688 .shutdown
= volume_shutdown
,
6691 /*************************************************************************
6698 * TPACPI_FAN_RD_ACPI_GFAN:
6699 * ACPI GFAN method: returns fan level
6701 * see TPACPI_FAN_WR_ACPI_SFAN
6702 * EC 0x2f (HFSP) not available if GFAN exists
6704 * TPACPI_FAN_WR_ACPI_SFAN:
6705 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6707 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6710 * TPACPI_FAN_WR_TPEC:
6711 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6712 * Supported on almost all ThinkPads
6714 * Fan speed changes of any sort (including those caused by the
6715 * disengaged mode) are usually done slowly by the firmware as the
6716 * maximum ammount of fan duty cycle change per second seems to be
6719 * Reading is not available if GFAN exists.
6720 * Writing is not available if SFAN exists.
6723 * 7 automatic mode engaged;
6724 * (default operation mode of the ThinkPad)
6725 * fan level is ignored in this mode.
6726 * 6 full speed mode (takes precedence over bit 7);
6727 * not available on all thinkpads. May disable
6728 * the tachometer while the fan controller ramps up
6729 * the speed (which can take up to a few *minutes*).
6730 * Speeds up fan to 100% duty-cycle, which is far above
6731 * the standard RPM levels. It is not impossible that
6732 * it could cause hardware damage.
6733 * 5-3 unused in some models. Extra bits for fan level
6734 * in others, but still useless as all values above
6735 * 7 map to the same speed as level 7 in these models.
6736 * 2-0 fan level (0..7 usually)
6738 * 0x07 = max (set when temperatures critical)
6739 * Some ThinkPads may have other levels, see
6740 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6742 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6743 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6744 * does so, its initial value is meaningless (0x07).
6746 * For firmware bugs, refer to:
6747 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6751 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6752 * Main fan tachometer reading (in RPM)
6754 * This register is present on all ThinkPads with a new-style EC, and
6755 * it is known not to be present on the A21m/e, and T22, as there is
6756 * something else in offset 0x84 according to the ACPI DSDT. Other
6757 * ThinkPads from this same time period (and earlier) probably lack the
6758 * tachometer as well.
6760 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6761 * was never fixed by IBM to report the EC firmware version string
6762 * probably support the tachometer (like the early X models), so
6763 * detecting it is quite hard. We need more data to know for sure.
6765 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6768 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6771 * For firmware bugs, refer to:
6772 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6776 * ThinkPad EC register 0x31 bit 0 (only on select models)
6778 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6779 * show the speed of the main fan. When bit 0 of EC register 0x31
6780 * is one, the tachometer registers show the speed of the auxiliary
6783 * Fan control seems to affect both fans, regardless of the state
6786 * So far, only the firmware for the X60/X61 non-tablet versions
6787 * seem to support this (firmware TP-7M).
6789 * TPACPI_FAN_WR_ACPI_FANS:
6790 * ThinkPad X31, X40, X41. Not available in the X60.
6792 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6793 * high speed. ACPI DSDT seems to map these three speeds to levels
6794 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6795 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6797 * The speeds are stored on handles
6798 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6800 * There are three default speed sets, acessible as handles:
6801 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6803 * ACPI DSDT switches which set is in use depending on various
6806 * TPACPI_FAN_WR_TPEC is also available and should be used to
6807 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6808 * but the ACPI tables just mention level 7.
6811 enum { /* Fan control constants */
6812 fan_status_offset
= 0x2f, /* EC register 0x2f */
6813 fan_rpm_offset
= 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6814 * 0x84 must be read before 0x85 */
6815 fan_select_offset
= 0x31, /* EC register 0x31 (Firmware 7M)
6816 bit 0 selects which fan is active */
6818 TP_EC_FAN_FULLSPEED
= 0x40, /* EC fan mode: full speed */
6819 TP_EC_FAN_AUTO
= 0x80, /* EC fan mode: auto fan control */
6821 TPACPI_FAN_LAST_LEVEL
= 0x100, /* Use cached last-seen fan level */
6824 enum fan_status_access_mode
{
6825 TPACPI_FAN_NONE
= 0, /* No fan status or control */
6826 TPACPI_FAN_RD_ACPI_GFAN
, /* Use ACPI GFAN */
6827 TPACPI_FAN_RD_TPEC
, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6830 enum fan_control_access_mode
{
6831 TPACPI_FAN_WR_NONE
= 0, /* No fan control */
6832 TPACPI_FAN_WR_ACPI_SFAN
, /* Use ACPI SFAN */
6833 TPACPI_FAN_WR_TPEC
, /* Use ACPI EC reg 0x2f */
6834 TPACPI_FAN_WR_ACPI_FANS
, /* Use ACPI FANS and EC reg 0x2f */
6837 enum fan_control_commands
{
6838 TPACPI_FAN_CMD_SPEED
= 0x0001, /* speed command */
6839 TPACPI_FAN_CMD_LEVEL
= 0x0002, /* level command */
6840 TPACPI_FAN_CMD_ENABLE
= 0x0004, /* enable/disable cmd,
6841 * and also watchdog cmd */
6844 static int fan_control_allowed
;
6846 static enum fan_status_access_mode fan_status_access_mode
;
6847 static enum fan_control_access_mode fan_control_access_mode
;
6848 static enum fan_control_commands fan_control_commands
;
6850 static u8 fan_control_initial_status
;
6851 static u8 fan_control_desired_level
;
6852 static u8 fan_control_resume_level
;
6853 static int fan_watchdog_maxinterval
;
6855 static struct mutex fan_mutex
;
6857 static void fan_watchdog_fire(struct work_struct
*ignored
);
6858 static DECLARE_DELAYED_WORK(fan_watchdog_task
, fan_watchdog_fire
);
6860 TPACPI_HANDLE(fans
, ec
, "FANS"); /* X31, X40, X41 */
6861 TPACPI_HANDLE(gfan
, ec
, "GFAN", /* 570 */
6862 "\\FSPD", /* 600e/x, 770e, 770x */
6864 TPACPI_HANDLE(sfan
, ec
, "SFAN", /* 570 */
6865 "JFNS", /* 770x-JL */
6869 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6870 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6871 * be in auto mode (0x80).
6873 * This is corrected by any write to HFSP either by the driver, or
6876 * We assume 0x07 really means auto mode while this quirk is active,
6877 * as this is far more likely than the ThinkPad being in level 7,
6878 * which is only used by the firmware during thermal emergencies.
6880 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6881 * TP-70 (T43, R52), which are known to be buggy.
6884 static void fan_quirk1_setup(void)
6886 if (fan_control_initial_status
== 0x07) {
6887 printk(TPACPI_NOTICE
6888 "fan_init: initial fan status is unknown, "
6889 "assuming it is in auto mode\n");
6890 tp_features
.fan_ctrl_status_undef
= 1;
6894 static void fan_quirk1_handle(u8
*fan_status
)
6896 if (unlikely(tp_features
.fan_ctrl_status_undef
)) {
6897 if (*fan_status
!= fan_control_initial_status
) {
6898 /* something changed the HFSP regisnter since
6899 * driver init time, so it is not undefined
6901 tp_features
.fan_ctrl_status_undef
= 0;
6903 /* Return most likely status. In fact, it
6904 * might be the only possible status */
6905 *fan_status
= TP_EC_FAN_AUTO
;
6910 /* Select main fan on X60/X61, NOOP on others */
6911 static bool fan_select_fan1(void)
6913 if (tp_features
.second_fan
) {
6916 if (ec_read(fan_select_offset
, &val
) < 0)
6919 if (ec_write(fan_select_offset
, val
) < 0)
6925 /* Select secondary fan on X60/X61 */
6926 static bool fan_select_fan2(void)
6930 if (!tp_features
.second_fan
)
6933 if (ec_read(fan_select_offset
, &val
) < 0)
6936 if (ec_write(fan_select_offset
, val
) < 0)
6943 * Call with fan_mutex held
6945 static void fan_update_desired_level(u8 status
)
6948 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
6950 fan_control_desired_level
= 7;
6952 fan_control_desired_level
= status
;
6956 static int fan_get_status(u8
*status
)
6961 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6963 switch (fan_status_access_mode
) {
6964 case TPACPI_FAN_RD_ACPI_GFAN
:
6965 /* 570, 600e/x, 770e, 770x */
6967 if (unlikely(!acpi_evalf(gfan_handle
, &s
, NULL
, "d")))
6975 case TPACPI_FAN_RD_TPEC
:
6976 /* all except 570, 600e/x, 770e, 770x */
6977 if (unlikely(!acpi_ec_read(fan_status_offset
, &s
)))
6980 if (likely(status
)) {
6982 fan_quirk1_handle(status
);
6994 static int fan_get_status_safe(u8
*status
)
6999 if (mutex_lock_killable(&fan_mutex
))
7000 return -ERESTARTSYS
;
7001 rc
= fan_get_status(&s
);
7003 fan_update_desired_level(s
);
7004 mutex_unlock(&fan_mutex
);
7012 static int fan_get_speed(unsigned int *speed
)
7016 switch (fan_status_access_mode
) {
7017 case TPACPI_FAN_RD_TPEC
:
7018 /* all except 570, 600e/x, 770e, 770x */
7019 if (unlikely(!fan_select_fan1()))
7021 if (unlikely(!acpi_ec_read(fan_rpm_offset
, &lo
) ||
7022 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
)))
7026 *speed
= (hi
<< 8) | lo
;
7037 static int fan2_get_speed(unsigned int *speed
)
7042 switch (fan_status_access_mode
) {
7043 case TPACPI_FAN_RD_TPEC
:
7044 /* all except 570, 600e/x, 770e, 770x */
7045 if (unlikely(!fan_select_fan2()))
7047 rc
= !acpi_ec_read(fan_rpm_offset
, &lo
) ||
7048 !acpi_ec_read(fan_rpm_offset
+ 1, &hi
);
7049 fan_select_fan1(); /* play it safe */
7054 *speed
= (hi
<< 8) | lo
;
7065 static int fan_set_level(int level
)
7067 if (!fan_control_allowed
)
7070 switch (fan_control_access_mode
) {
7071 case TPACPI_FAN_WR_ACPI_SFAN
:
7072 if (level
>= 0 && level
<= 7) {
7073 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", level
))
7079 case TPACPI_FAN_WR_ACPI_FANS
:
7080 case TPACPI_FAN_WR_TPEC
:
7081 if (!(level
& TP_EC_FAN_AUTO
) &&
7082 !(level
& TP_EC_FAN_FULLSPEED
) &&
7083 ((level
< 0) || (level
> 7)))
7086 /* safety net should the EC not support AUTO
7087 * or FULLSPEED mode bits and just ignore them */
7088 if (level
& TP_EC_FAN_FULLSPEED
)
7089 level
|= 7; /* safety min speed 7 */
7090 else if (level
& TP_EC_FAN_AUTO
)
7091 level
|= 4; /* safety min speed 4 */
7093 if (!acpi_ec_write(fan_status_offset
, level
))
7096 tp_features
.fan_ctrl_status_undef
= 0;
7103 vdbg_printk(TPACPI_DBG_FAN
,
7104 "fan control: set fan control register to 0x%02x\n", level
);
7108 static int fan_set_level_safe(int level
)
7112 if (!fan_control_allowed
)
7115 if (mutex_lock_killable(&fan_mutex
))
7116 return -ERESTARTSYS
;
7118 if (level
== TPACPI_FAN_LAST_LEVEL
)
7119 level
= fan_control_desired_level
;
7121 rc
= fan_set_level(level
);
7123 fan_update_desired_level(level
);
7125 mutex_unlock(&fan_mutex
);
7129 static int fan_set_enable(void)
7134 if (!fan_control_allowed
)
7137 if (mutex_lock_killable(&fan_mutex
))
7138 return -ERESTARTSYS
;
7140 switch (fan_control_access_mode
) {
7141 case TPACPI_FAN_WR_ACPI_FANS
:
7142 case TPACPI_FAN_WR_TPEC
:
7143 rc
= fan_get_status(&s
);
7147 /* Don't go out of emergency fan mode */
7150 s
|= TP_EC_FAN_AUTO
| 4; /* min fan speed 4 */
7153 if (!acpi_ec_write(fan_status_offset
, s
))
7156 tp_features
.fan_ctrl_status_undef
= 0;
7161 case TPACPI_FAN_WR_ACPI_SFAN
:
7162 rc
= fan_get_status(&s
);
7168 /* Set fan to at least level 4 */
7171 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", s
))
7181 mutex_unlock(&fan_mutex
);
7184 vdbg_printk(TPACPI_DBG_FAN
,
7185 "fan control: set fan control register to 0x%02x\n",
7190 static int fan_set_disable(void)
7194 if (!fan_control_allowed
)
7197 if (mutex_lock_killable(&fan_mutex
))
7198 return -ERESTARTSYS
;
7201 switch (fan_control_access_mode
) {
7202 case TPACPI_FAN_WR_ACPI_FANS
:
7203 case TPACPI_FAN_WR_TPEC
:
7204 if (!acpi_ec_write(fan_status_offset
, 0x00))
7207 fan_control_desired_level
= 0;
7208 tp_features
.fan_ctrl_status_undef
= 0;
7212 case TPACPI_FAN_WR_ACPI_SFAN
:
7213 if (!acpi_evalf(sfan_handle
, NULL
, NULL
, "vd", 0x00))
7216 fan_control_desired_level
= 0;
7224 vdbg_printk(TPACPI_DBG_FAN
,
7225 "fan control: set fan control register to 0\n");
7227 mutex_unlock(&fan_mutex
);
7231 static int fan_set_speed(int speed
)
7235 if (!fan_control_allowed
)
7238 if (mutex_lock_killable(&fan_mutex
))
7239 return -ERESTARTSYS
;
7242 switch (fan_control_access_mode
) {
7243 case TPACPI_FAN_WR_ACPI_FANS
:
7244 if (speed
>= 0 && speed
<= 65535) {
7245 if (!acpi_evalf(fans_handle
, NULL
, NULL
, "vddd",
7246 speed
, speed
, speed
))
7256 mutex_unlock(&fan_mutex
);
7260 static void fan_watchdog_reset(void)
7262 static int fan_watchdog_active
;
7264 if (fan_control_access_mode
== TPACPI_FAN_WR_NONE
)
7267 if (fan_watchdog_active
)
7268 cancel_delayed_work(&fan_watchdog_task
);
7270 if (fan_watchdog_maxinterval
> 0 &&
7271 tpacpi_lifecycle
!= TPACPI_LIFE_EXITING
) {
7272 fan_watchdog_active
= 1;
7273 if (!queue_delayed_work(tpacpi_wq
, &fan_watchdog_task
,
7274 msecs_to_jiffies(fan_watchdog_maxinterval
7277 "failed to queue the fan watchdog, "
7278 "watchdog will not trigger\n");
7281 fan_watchdog_active
= 0;
7284 static void fan_watchdog_fire(struct work_struct
*ignored
)
7288 if (tpacpi_lifecycle
!= TPACPI_LIFE_RUNNING
)
7291 printk(TPACPI_NOTICE
"fan watchdog: enabling fan\n");
7292 rc
= fan_set_enable();
7294 printk(TPACPI_ERR
"fan watchdog: error %d while enabling fan, "
7295 "will try again later...\n", -rc
);
7296 /* reschedule for later */
7297 fan_watchdog_reset();
7302 * SYSFS fan layout: hwmon compatible (device)
7305 * 0: "disengaged" mode
7307 * 2: native EC "auto" mode (recommended, hardware default)
7309 * pwm*: set speed in manual mode, ignored otherwise.
7310 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7313 * fan*_input: tachometer reading, RPM
7316 * SYSFS fan layout: extensions
7318 * fan_watchdog (driver):
7319 * fan watchdog interval in seconds, 0 disables (default), max 120
7322 /* sysfs fan pwm1_enable ----------------------------------------------- */
7323 static ssize_t
fan_pwm1_enable_show(struct device
*dev
,
7324 struct device_attribute
*attr
,
7330 res
= fan_get_status_safe(&status
);
7334 if (status
& TP_EC_FAN_FULLSPEED
) {
7336 } else if (status
& TP_EC_FAN_AUTO
) {
7341 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
7344 static ssize_t
fan_pwm1_enable_store(struct device
*dev
,
7345 struct device_attribute
*attr
,
7346 const char *buf
, size_t count
)
7351 if (parse_strtoul(buf
, 2, &t
))
7354 tpacpi_disclose_usertask("hwmon pwm1_enable",
7355 "set fan mode to %lu\n", t
);
7359 level
= TP_EC_FAN_FULLSPEED
;
7362 level
= TPACPI_FAN_LAST_LEVEL
;
7365 level
= TP_EC_FAN_AUTO
;
7368 /* reserved for software-controlled auto mode */
7374 res
= fan_set_level_safe(level
);
7380 fan_watchdog_reset();
7385 static struct device_attribute dev_attr_fan_pwm1_enable
=
7386 __ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
7387 fan_pwm1_enable_show
, fan_pwm1_enable_store
);
7389 /* sysfs fan pwm1 ------------------------------------------------------ */
7390 static ssize_t
fan_pwm1_show(struct device
*dev
,
7391 struct device_attribute
*attr
,
7397 res
= fan_get_status_safe(&status
);
7402 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) != 0)
7403 status
= fan_control_desired_level
;
7408 return snprintf(buf
, PAGE_SIZE
, "%u\n", (status
* 255) / 7);
7411 static ssize_t
fan_pwm1_store(struct device
*dev
,
7412 struct device_attribute
*attr
,
7413 const char *buf
, size_t count
)
7417 u8 status
, newlevel
;
7419 if (parse_strtoul(buf
, 255, &s
))
7422 tpacpi_disclose_usertask("hwmon pwm1",
7423 "set fan speed to %lu\n", s
);
7425 /* scale down from 0-255 to 0-7 */
7426 newlevel
= (s
>> 5) & 0x07;
7428 if (mutex_lock_killable(&fan_mutex
))
7429 return -ERESTARTSYS
;
7431 rc
= fan_get_status(&status
);
7432 if (!rc
&& (status
&
7433 (TP_EC_FAN_AUTO
| TP_EC_FAN_FULLSPEED
)) == 0) {
7434 rc
= fan_set_level(newlevel
);
7438 fan_update_desired_level(newlevel
);
7439 fan_watchdog_reset();
7443 mutex_unlock(&fan_mutex
);
7444 return (rc
)? rc
: count
;
7447 static struct device_attribute dev_attr_fan_pwm1
=
7448 __ATTR(pwm1
, S_IWUSR
| S_IRUGO
,
7449 fan_pwm1_show
, fan_pwm1_store
);
7451 /* sysfs fan fan1_input ------------------------------------------------ */
7452 static ssize_t
fan_fan1_input_show(struct device
*dev
,
7453 struct device_attribute
*attr
,
7459 res
= fan_get_speed(&speed
);
7463 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7466 static struct device_attribute dev_attr_fan_fan1_input
=
7467 __ATTR(fan1_input
, S_IRUGO
,
7468 fan_fan1_input_show
, NULL
);
7470 /* sysfs fan fan2_input ------------------------------------------------ */
7471 static ssize_t
fan_fan2_input_show(struct device
*dev
,
7472 struct device_attribute
*attr
,
7478 res
= fan2_get_speed(&speed
);
7482 return snprintf(buf
, PAGE_SIZE
, "%u\n", speed
);
7485 static struct device_attribute dev_attr_fan_fan2_input
=
7486 __ATTR(fan2_input
, S_IRUGO
,
7487 fan_fan2_input_show
, NULL
);
7489 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7490 static ssize_t
fan_fan_watchdog_show(struct device_driver
*drv
,
7493 return snprintf(buf
, PAGE_SIZE
, "%u\n", fan_watchdog_maxinterval
);
7496 static ssize_t
fan_fan_watchdog_store(struct device_driver
*drv
,
7497 const char *buf
, size_t count
)
7501 if (parse_strtoul(buf
, 120, &t
))
7504 if (!fan_control_allowed
)
7507 fan_watchdog_maxinterval
= t
;
7508 fan_watchdog_reset();
7510 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t
);
7515 static DRIVER_ATTR(fan_watchdog
, S_IWUSR
| S_IRUGO
,
7516 fan_fan_watchdog_show
, fan_fan_watchdog_store
);
7518 /* --------------------------------------------------------------------- */
7519 static struct attribute
*fan_attributes
[] = {
7520 &dev_attr_fan_pwm1_enable
.attr
, &dev_attr_fan_pwm1
.attr
,
7521 &dev_attr_fan_fan1_input
.attr
,
7522 NULL
, /* for fan2_input */
7526 static const struct attribute_group fan_attr_group
= {
7527 .attrs
= fan_attributes
,
7530 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7531 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7533 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7534 { .vendor = PCI_VENDOR_ID_IBM, \
7535 .bios = TPACPI_MATCH_ANY, \
7536 .ec = TPID(__id1, __id2), \
7537 .quirks = __quirks }
7539 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7540 { .vendor = PCI_VENDOR_ID_LENOVO, \
7541 .bios = TPACPI_MATCH_ANY, \
7542 .ec = TPID(__id1, __id2), \
7543 .quirks = __quirks }
7545 static const struct tpacpi_quirk fan_quirk_table
[] __initconst
= {
7546 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1
),
7547 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1
),
7548 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1
),
7549 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1
),
7550 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN
),
7553 #undef TPACPI_FAN_QL
7554 #undef TPACPI_FAN_QI
7556 static int __init
fan_init(struct ibm_init_struct
*iibm
)
7559 unsigned long quirks
;
7561 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7562 "initializing fan subdriver\n");
7564 mutex_init(&fan_mutex
);
7565 fan_status_access_mode
= TPACPI_FAN_NONE
;
7566 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7567 fan_control_commands
= 0;
7568 fan_watchdog_maxinterval
= 0;
7569 tp_features
.fan_ctrl_status_undef
= 0;
7570 tp_features
.second_fan
= 0;
7571 fan_control_desired_level
= 7;
7573 TPACPI_ACPIHANDLE_INIT(fans
);
7574 TPACPI_ACPIHANDLE_INIT(gfan
);
7575 TPACPI_ACPIHANDLE_INIT(sfan
);
7577 quirks
= tpacpi_check_quirks(fan_quirk_table
,
7578 ARRAY_SIZE(fan_quirk_table
));
7581 /* 570, 600e/x, 770e, 770x */
7582 fan_status_access_mode
= TPACPI_FAN_RD_ACPI_GFAN
;
7584 /* all other ThinkPads: note that even old-style
7585 * ThinkPad ECs supports the fan control register */
7586 if (likely(acpi_ec_read(fan_status_offset
,
7587 &fan_control_initial_status
))) {
7588 fan_status_access_mode
= TPACPI_FAN_RD_TPEC
;
7589 if (quirks
& TPACPI_FAN_Q1
)
7591 if (quirks
& TPACPI_FAN_2FAN
) {
7592 tp_features
.second_fan
= 1;
7593 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7594 "secondary fan support enabled\n");
7598 "ThinkPad ACPI EC access misbehaving, "
7599 "fan status and control unavailable\n");
7606 fan_control_access_mode
= TPACPI_FAN_WR_ACPI_SFAN
;
7607 fan_control_commands
|=
7608 TPACPI_FAN_CMD_LEVEL
| TPACPI_FAN_CMD_ENABLE
;
7611 /* gfan without sfan means no fan control */
7612 /* all other models implement TP EC 0x2f control */
7616 fan_control_access_mode
=
7617 TPACPI_FAN_WR_ACPI_FANS
;
7618 fan_control_commands
|=
7619 TPACPI_FAN_CMD_SPEED
|
7620 TPACPI_FAN_CMD_LEVEL
|
7621 TPACPI_FAN_CMD_ENABLE
;
7623 fan_control_access_mode
= TPACPI_FAN_WR_TPEC
;
7624 fan_control_commands
|=
7625 TPACPI_FAN_CMD_LEVEL
|
7626 TPACPI_FAN_CMD_ENABLE
;
7631 vdbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7632 "fan is %s, modes %d, %d\n",
7633 str_supported(fan_status_access_mode
!= TPACPI_FAN_NONE
||
7634 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
),
7635 fan_status_access_mode
, fan_control_access_mode
);
7637 /* fan control master switch */
7638 if (!fan_control_allowed
) {
7639 fan_control_access_mode
= TPACPI_FAN_WR_NONE
;
7640 fan_control_commands
= 0;
7641 dbg_printk(TPACPI_DBG_INIT
| TPACPI_DBG_FAN
,
7642 "fan control features disabled by parameter\n");
7645 /* update fan_control_desired_level */
7646 if (fan_status_access_mode
!= TPACPI_FAN_NONE
)
7647 fan_get_status_safe(NULL
);
7649 if (fan_status_access_mode
!= TPACPI_FAN_NONE
||
7650 fan_control_access_mode
!= TPACPI_FAN_WR_NONE
) {
7651 if (tp_features
.second_fan
) {
7652 /* attach second fan tachometer */
7653 fan_attributes
[ARRAY_SIZE(fan_attributes
)-2] =
7654 &dev_attr_fan_fan2_input
.attr
;
7656 rc
= sysfs_create_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7661 rc
= driver_create_file(&tpacpi_hwmon_pdriver
.driver
,
7662 &driver_attr_fan_watchdog
);
7664 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
,
7673 static void fan_exit(void)
7675 vdbg_printk(TPACPI_DBG_EXIT
| TPACPI_DBG_FAN
,
7676 "cancelling any pending fan watchdog tasks\n");
7678 /* FIXME: can we really do this unconditionally? */
7679 sysfs_remove_group(&tpacpi_sensors_pdev
->dev
.kobj
, &fan_attr_group
);
7680 driver_remove_file(&tpacpi_hwmon_pdriver
.driver
,
7681 &driver_attr_fan_watchdog
);
7683 cancel_delayed_work(&fan_watchdog_task
);
7684 flush_workqueue(tpacpi_wq
);
7687 static void fan_suspend(pm_message_t state
)
7691 if (!fan_control_allowed
)
7694 /* Store fan status in cache */
7695 fan_control_resume_level
= 0;
7696 rc
= fan_get_status_safe(&fan_control_resume_level
);
7698 printk(TPACPI_NOTICE
7699 "failed to read fan level for later "
7700 "restore during resume: %d\n", rc
);
7702 /* if it is undefined, don't attempt to restore it.
7704 if (tp_features
.fan_ctrl_status_undef
)
7705 fan_control_resume_level
= 0;
7708 static void fan_resume(void)
7710 u8 current_level
= 7;
7711 bool do_set
= false;
7714 /* DSDT *always* updates status on resume */
7715 tp_features
.fan_ctrl_status_undef
= 0;
7717 if (!fan_control_allowed
||
7718 !fan_control_resume_level
||
7719 (fan_get_status_safe(¤t_level
) < 0))
7722 switch (fan_control_access_mode
) {
7723 case TPACPI_FAN_WR_ACPI_SFAN
:
7724 /* never decrease fan level */
7725 do_set
= (fan_control_resume_level
> current_level
);
7727 case TPACPI_FAN_WR_ACPI_FANS
:
7728 case TPACPI_FAN_WR_TPEC
:
7729 /* never decrease fan level, scale is:
7730 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7732 * We expect the firmware to set either 7 or AUTO, but we
7733 * handle FULLSPEED out of paranoia.
7735 * So, we can safely only restore FULLSPEED or 7, anything
7736 * else could slow the fan. Restoring AUTO is useless, at
7737 * best that's exactly what the DSDT already set (it is the
7740 * Always keep in mind that the DSDT *will* have set the
7741 * fans to what the vendor supposes is the best level. We
7742 * muck with it only to speed the fan up.
7744 if (fan_control_resume_level
!= 7 &&
7745 !(fan_control_resume_level
& TP_EC_FAN_FULLSPEED
))
7748 do_set
= !(current_level
& TP_EC_FAN_FULLSPEED
) &&
7749 (current_level
!= fan_control_resume_level
);
7755 printk(TPACPI_NOTICE
7756 "restoring fan level to 0x%02x\n",
7757 fan_control_resume_level
);
7758 rc
= fan_set_level_safe(fan_control_resume_level
);
7760 printk(TPACPI_NOTICE
7761 "failed to restore fan level: %d\n", rc
);
7765 static int fan_read(char *p
)
7770 unsigned int speed
= 0;
7772 switch (fan_status_access_mode
) {
7773 case TPACPI_FAN_RD_ACPI_GFAN
:
7774 /* 570, 600e/x, 770e, 770x */
7775 rc
= fan_get_status_safe(&status
);
7779 len
+= sprintf(p
+ len
, "status:\t\t%s\n"
7781 (status
!= 0) ? "enabled" : "disabled", status
);
7784 case TPACPI_FAN_RD_TPEC
:
7785 /* all except 570, 600e/x, 770e, 770x */
7786 rc
= fan_get_status_safe(&status
);
7790 len
+= sprintf(p
+ len
, "status:\t\t%s\n",
7791 (status
!= 0) ? "enabled" : "disabled");
7793 rc
= fan_get_speed(&speed
);
7797 len
+= sprintf(p
+ len
, "speed:\t\t%d\n", speed
);
7799 if (status
& TP_EC_FAN_FULLSPEED
)
7800 /* Disengaged mode takes precedence */
7801 len
+= sprintf(p
+ len
, "level:\t\tdisengaged\n");
7802 else if (status
& TP_EC_FAN_AUTO
)
7803 len
+= sprintf(p
+ len
, "level:\t\tauto\n");
7805 len
+= sprintf(p
+ len
, "level:\t\t%d\n", status
);
7808 case TPACPI_FAN_NONE
:
7810 len
+= sprintf(p
+ len
, "status:\t\tnot supported\n");
7813 if (fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) {
7814 len
+= sprintf(p
+ len
, "commands:\tlevel <level>");
7816 switch (fan_control_access_mode
) {
7817 case TPACPI_FAN_WR_ACPI_SFAN
:
7818 len
+= sprintf(p
+ len
, " (<level> is 0-7)\n");
7822 len
+= sprintf(p
+ len
, " (<level> is 0-7, "
7823 "auto, disengaged, full-speed)\n");
7828 if (fan_control_commands
& TPACPI_FAN_CMD_ENABLE
)
7829 len
+= sprintf(p
+ len
, "commands:\tenable, disable\n"
7830 "commands:\twatchdog <timeout> (<timeout> "
7831 "is 0 (off), 1-120 (seconds))\n");
7833 if (fan_control_commands
& TPACPI_FAN_CMD_SPEED
)
7834 len
+= sprintf(p
+ len
, "commands:\tspeed <speed>"
7835 " (<speed> is 0-65535)\n");
7840 static int fan_write_cmd_level(const char *cmd
, int *rc
)
7844 if (strlencmp(cmd
, "level auto") == 0)
7845 level
= TP_EC_FAN_AUTO
;
7846 else if ((strlencmp(cmd
, "level disengaged") == 0) |
7847 (strlencmp(cmd
, "level full-speed") == 0))
7848 level
= TP_EC_FAN_FULLSPEED
;
7849 else if (sscanf(cmd
, "level %d", &level
) != 1)
7852 *rc
= fan_set_level_safe(level
);
7854 printk(TPACPI_ERR
"level command accepted for unsupported "
7855 "access mode %d", fan_control_access_mode
);
7857 tpacpi_disclose_usertask("procfs fan",
7858 "set level to %d\n", level
);
7863 static int fan_write_cmd_enable(const char *cmd
, int *rc
)
7865 if (strlencmp(cmd
, "enable") != 0)
7868 *rc
= fan_set_enable();
7870 printk(TPACPI_ERR
"enable command accepted for unsupported "
7871 "access mode %d", fan_control_access_mode
);
7873 tpacpi_disclose_usertask("procfs fan", "enable\n");
7878 static int fan_write_cmd_disable(const char *cmd
, int *rc
)
7880 if (strlencmp(cmd
, "disable") != 0)
7883 *rc
= fan_set_disable();
7885 printk(TPACPI_ERR
"disable command accepted for unsupported "
7886 "access mode %d", fan_control_access_mode
);
7888 tpacpi_disclose_usertask("procfs fan", "disable\n");
7893 static int fan_write_cmd_speed(const char *cmd
, int *rc
)
7898 * Support speed <low> <medium> <high> ? */
7900 if (sscanf(cmd
, "speed %d", &speed
) != 1)
7903 *rc
= fan_set_speed(speed
);
7905 printk(TPACPI_ERR
"speed command accepted for unsupported "
7906 "access mode %d", fan_control_access_mode
);
7908 tpacpi_disclose_usertask("procfs fan",
7909 "set speed to %d\n", speed
);
7914 static int fan_write_cmd_watchdog(const char *cmd
, int *rc
)
7918 if (sscanf(cmd
, "watchdog %d", &interval
) != 1)
7921 if (interval
< 0 || interval
> 120)
7924 fan_watchdog_maxinterval
= interval
;
7925 tpacpi_disclose_usertask("procfs fan",
7926 "set watchdog timer to %d\n",
7933 static int fan_write(char *buf
)
7938 while (!rc
&& (cmd
= next_cmd(&buf
))) {
7939 if (!((fan_control_commands
& TPACPI_FAN_CMD_LEVEL
) &&
7940 fan_write_cmd_level(cmd
, &rc
)) &&
7941 !((fan_control_commands
& TPACPI_FAN_CMD_ENABLE
) &&
7942 (fan_write_cmd_enable(cmd
, &rc
) ||
7943 fan_write_cmd_disable(cmd
, &rc
) ||
7944 fan_write_cmd_watchdog(cmd
, &rc
))) &&
7945 !((fan_control_commands
& TPACPI_FAN_CMD_SPEED
) &&
7946 fan_write_cmd_speed(cmd
, &rc
))
7950 fan_watchdog_reset();
7956 static struct ibm_struct fan_driver_data
= {
7961 .suspend
= fan_suspend
,
7962 .resume
= fan_resume
,
7965 /****************************************************************************
7966 ****************************************************************************
7970 ****************************************************************************
7971 ****************************************************************************/
7974 * HKEY event callout for other subdrivers go here
7975 * (yes, it is ugly, but it is quick, safe, and gets the job done
7977 static void tpacpi_driver_event(const unsigned int hkey_event
)
7979 if (ibm_backlight_device
) {
7980 switch (hkey_event
) {
7981 case TP_HKEY_EV_BRGHT_UP
:
7982 case TP_HKEY_EV_BRGHT_DOWN
:
7983 tpacpi_brightness_notify_change();
7990 static void hotkey_driver_event(const unsigned int scancode
)
7992 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE
+ scancode
);
7995 /* sysfs name ---------------------------------------------------------- */
7996 static ssize_t
thinkpad_acpi_pdev_name_show(struct device
*dev
,
7997 struct device_attribute
*attr
,
8000 return snprintf(buf
, PAGE_SIZE
, "%s\n", TPACPI_NAME
);
8003 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name
=
8004 __ATTR(name
, S_IRUGO
, thinkpad_acpi_pdev_name_show
, NULL
);
8006 /* --------------------------------------------------------------------- */
8009 static struct proc_dir_entry
*proc_dir
;
8012 * Module and infrastructure proble, init and exit handling
8015 static int force_load
;
8017 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8018 static const char * __init
str_supported(int is_supported
)
8020 static char text_unsupported
[] __initdata
= "not supported";
8022 return (is_supported
)? &text_unsupported
[4] : &text_unsupported
[0];
8024 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8026 static void ibm_exit(struct ibm_struct
*ibm
)
8028 dbg_printk(TPACPI_DBG_EXIT
, "removing %s\n", ibm
->name
);
8030 list_del_init(&ibm
->all_drivers
);
8032 if (ibm
->flags
.acpi_notify_installed
) {
8033 dbg_printk(TPACPI_DBG_EXIT
,
8034 "%s: acpi_remove_notify_handler\n", ibm
->name
);
8036 acpi_remove_notify_handler(*ibm
->acpi
->handle
,
8038 dispatch_acpi_notify
);
8039 ibm
->flags
.acpi_notify_installed
= 0;
8040 ibm
->flags
.acpi_notify_installed
= 0;
8043 if (ibm
->flags
.proc_created
) {
8044 dbg_printk(TPACPI_DBG_EXIT
,
8045 "%s: remove_proc_entry\n", ibm
->name
);
8046 remove_proc_entry(ibm
->name
, proc_dir
);
8047 ibm
->flags
.proc_created
= 0;
8050 if (ibm
->flags
.acpi_driver_registered
) {
8051 dbg_printk(TPACPI_DBG_EXIT
,
8052 "%s: acpi_bus_unregister_driver\n", ibm
->name
);
8054 acpi_bus_unregister_driver(ibm
->acpi
->driver
);
8055 kfree(ibm
->acpi
->driver
);
8056 ibm
->acpi
->driver
= NULL
;
8057 ibm
->flags
.acpi_driver_registered
= 0;
8060 if (ibm
->flags
.init_called
&& ibm
->exit
) {
8062 ibm
->flags
.init_called
= 0;
8065 dbg_printk(TPACPI_DBG_INIT
, "finished removing %s\n", ibm
->name
);
8068 static int __init
ibm_init(struct ibm_init_struct
*iibm
)
8071 struct ibm_struct
*ibm
= iibm
->data
;
8072 struct proc_dir_entry
*entry
;
8074 BUG_ON(ibm
== NULL
);
8076 INIT_LIST_HEAD(&ibm
->all_drivers
);
8078 if (ibm
->flags
.experimental
&& !experimental
)
8081 dbg_printk(TPACPI_DBG_INIT
,
8082 "probing for %s\n", ibm
->name
);
8085 ret
= iibm
->init(iibm
);
8087 return 0; /* probe failed */
8091 ibm
->flags
.init_called
= 1;
8095 if (ibm
->acpi
->hid
) {
8096 ret
= register_tpacpi_subdriver(ibm
);
8101 if (ibm
->acpi
->notify
) {
8102 ret
= setup_acpi_notify(ibm
);
8103 if (ret
== -ENODEV
) {
8104 printk(TPACPI_NOTICE
"disabling subdriver %s\n",
8114 dbg_printk(TPACPI_DBG_INIT
,
8115 "%s installed\n", ibm
->name
);
8118 entry
= create_proc_entry(ibm
->name
,
8119 S_IFREG
| S_IRUGO
| S_IWUSR
,
8122 printk(TPACPI_ERR
"unable to create proc entry %s\n",
8128 entry
->read_proc
= &dispatch_procfs_read
;
8130 entry
->write_proc
= &dispatch_procfs_write
;
8131 ibm
->flags
.proc_created
= 1;
8134 list_add_tail(&ibm
->all_drivers
, &tpacpi_all_drivers
);
8139 dbg_printk(TPACPI_DBG_INIT
,
8140 "%s: at error exit path with result %d\n",
8144 return (ret
< 0)? ret
: 0;
8149 static bool __pure __init
tpacpi_is_fw_digit(const char c
)
8151 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'Z');
8154 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8155 static bool __pure __init
tpacpi_is_valid_fw_id(const char* const s
,
8158 return s
&& strlen(s
) >= 8 &&
8159 tpacpi_is_fw_digit(s
[0]) &&
8160 tpacpi_is_fw_digit(s
[1]) &&
8161 s
[2] == t
&& s
[3] == 'T' &&
8162 tpacpi_is_fw_digit(s
[4]) &&
8163 tpacpi_is_fw_digit(s
[5]) &&
8164 s
[6] == 'W' && s
[7] == 'W';
8167 /* returns 0 - probe ok, or < 0 - probe error.
8168 * Probe ok doesn't mean thinkpad found.
8169 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8170 static int __must_check __init
get_thinkpad_model_data(
8171 struct thinkpad_id_data
*tp
)
8173 const struct dmi_device
*dev
= NULL
;
8174 char ec_fw_string
[18];
8180 memset(tp
, 0, sizeof(*tp
));
8182 if (dmi_name_in_vendors("IBM"))
8183 tp
->vendor
= PCI_VENDOR_ID_IBM
;
8184 else if (dmi_name_in_vendors("LENOVO"))
8185 tp
->vendor
= PCI_VENDOR_ID_LENOVO
;
8189 s
= dmi_get_system_info(DMI_BIOS_VERSION
);
8190 tp
->bios_version_str
= kstrdup(s
, GFP_KERNEL
);
8191 if (s
&& !tp
->bios_version_str
)
8194 /* Really ancient ThinkPad 240X will fail this, which is fine */
8195 if (!tpacpi_is_valid_fw_id(tp
->bios_version_str
, 'E'))
8198 tp
->bios_model
= tp
->bios_version_str
[0]
8199 | (tp
->bios_version_str
[1] << 8);
8200 tp
->bios_release
= (tp
->bios_version_str
[4] << 8)
8201 | tp
->bios_version_str
[5];
8204 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8205 * X32 or newer, all Z series; Some models must have an
8206 * up-to-date BIOS or they will not be detected.
8208 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8210 while ((dev
= dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, NULL
, dev
))) {
8211 if (sscanf(dev
->name
,
8212 "IBM ThinkPad Embedded Controller -[%17c",
8213 ec_fw_string
) == 1) {
8214 ec_fw_string
[sizeof(ec_fw_string
) - 1] = 0;
8215 ec_fw_string
[strcspn(ec_fw_string
, " ]")] = 0;
8217 tp
->ec_version_str
= kstrdup(ec_fw_string
, GFP_KERNEL
);
8218 if (!tp
->ec_version_str
)
8221 if (tpacpi_is_valid_fw_id(ec_fw_string
, 'H')) {
8222 tp
->ec_model
= ec_fw_string
[0]
8223 | (ec_fw_string
[1] << 8);
8224 tp
->ec_release
= (ec_fw_string
[4] << 8)
8227 printk(TPACPI_NOTICE
8228 "ThinkPad firmware release %s "
8229 "doesn't match the known patterns\n",
8231 printk(TPACPI_NOTICE
8232 "please report this to %s\n",
8239 s
= dmi_get_system_info(DMI_PRODUCT_VERSION
);
8240 if (s
&& !strnicmp(s
, "ThinkPad", 8)) {
8241 tp
->model_str
= kstrdup(s
, GFP_KERNEL
);
8246 s
= dmi_get_system_info(DMI_PRODUCT_NAME
);
8247 tp
->nummodel_str
= kstrdup(s
, GFP_KERNEL
);
8248 if (s
&& !tp
->nummodel_str
)
8254 static int __init
probe_for_thinkpad(void)
8262 * Non-ancient models have better DMI tagging, but very old models
8263 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8265 is_thinkpad
= (thinkpad_id
.model_str
!= NULL
) ||
8266 (thinkpad_id
.ec_model
!= 0) ||
8267 tpacpi_is_fw_known();
8269 /* ec is required because many other handles are relative to it */
8270 TPACPI_ACPIHANDLE_INIT(ec
);
8274 "Not yet supported ThinkPad detected!\n");
8278 if (!is_thinkpad
&& !force_load
)
8285 /* Module init, exit, parameters */
8287 static struct ibm_init_struct ibms_init
[] __initdata
= {
8289 .init
= thinkpad_acpi_driver_init
,
8290 .data
= &thinkpad_acpi_driver_data
,
8293 .init
= hotkey_init
,
8294 .data
= &hotkey_driver_data
,
8297 .init
= bluetooth_init
,
8298 .data
= &bluetooth_driver_data
,
8302 .data
= &wan_driver_data
,
8306 .data
= &uwb_driver_data
,
8308 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8311 .data
= &video_driver_data
,
8316 .data
= &light_driver_data
,
8320 .data
= &cmos_driver_data
,
8324 .data
= &led_driver_data
,
8328 .data
= &beep_driver_data
,
8331 .init
= thermal_init
,
8332 .data
= &thermal_driver_data
,
8335 .data
= &ecdump_driver_data
,
8338 .init
= brightness_init
,
8339 .data
= &brightness_driver_data
,
8342 .init
= volume_init
,
8343 .data
= &volume_driver_data
,
8347 .data
= &fan_driver_data
,
8351 static int __init
set_ibm_param(const char *val
, struct kernel_param
*kp
)
8354 struct ibm_struct
*ibm
;
8356 if (!kp
|| !kp
->name
|| !val
)
8359 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8360 ibm
= ibms_init
[i
].data
;
8361 WARN_ON(ibm
== NULL
);
8363 if (!ibm
|| !ibm
->name
)
8366 if (strcmp(ibm
->name
, kp
->name
) == 0 && ibm
->write
) {
8367 if (strlen(val
) > sizeof(ibms_init
[i
].param
) - 2)
8369 strcpy(ibms_init
[i
].param
, val
);
8370 strcat(ibms_init
[i
].param
, ",");
8378 module_param(experimental
, int, 0444);
8379 MODULE_PARM_DESC(experimental
,
8380 "Enables experimental features when non-zero");
8382 module_param_named(debug
, dbg_level
, uint
, 0);
8383 MODULE_PARM_DESC(debug
, "Sets debug level bit-mask");
8385 module_param(force_load
, bool, 0444);
8386 MODULE_PARM_DESC(force_load
,
8387 "Attempts to load the driver even on a "
8388 "mis-identified ThinkPad when true");
8390 module_param_named(fan_control
, fan_control_allowed
, bool, 0444);
8391 MODULE_PARM_DESC(fan_control
,
8392 "Enables setting fan parameters features when true");
8394 module_param_named(brightness_mode
, brightness_mode
, uint
, 0444);
8395 MODULE_PARM_DESC(brightness_mode
,
8396 "Selects brightness control strategy: "
8397 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8399 module_param(brightness_enable
, uint
, 0444);
8400 MODULE_PARM_DESC(brightness_enable
,
8401 "Enables backlight control when 1, disables when 0");
8403 module_param(hotkey_report_mode
, uint
, 0444);
8404 MODULE_PARM_DESC(hotkey_report_mode
,
8405 "used for backwards compatibility with userspace, "
8406 "see documentation");
8408 module_param_named(volume_mode
, volume_mode
, uint
, 0444);
8409 MODULE_PARM_DESC(volume_mode
,
8410 "Selects volume control strategy: "
8411 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8413 #define TPACPI_PARAM(feature) \
8414 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8415 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8416 "at module load, see documentation")
8418 TPACPI_PARAM(hotkey
);
8419 TPACPI_PARAM(bluetooth
);
8420 TPACPI_PARAM(video
);
8421 TPACPI_PARAM(light
);
8425 TPACPI_PARAM(ecdump
);
8426 TPACPI_PARAM(brightness
);
8427 TPACPI_PARAM(volume
);
8430 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8431 module_param(dbg_wlswemul
, uint
, 0444);
8432 MODULE_PARM_DESC(dbg_wlswemul
, "Enables WLSW emulation");
8433 module_param_named(wlsw_state
, tpacpi_wlsw_emulstate
, bool, 0);
8434 MODULE_PARM_DESC(wlsw_state
,
8435 "Initial state of the emulated WLSW switch");
8437 module_param(dbg_bluetoothemul
, uint
, 0444);
8438 MODULE_PARM_DESC(dbg_bluetoothemul
, "Enables bluetooth switch emulation");
8439 module_param_named(bluetooth_state
, tpacpi_bluetooth_emulstate
, bool, 0);
8440 MODULE_PARM_DESC(bluetooth_state
,
8441 "Initial state of the emulated bluetooth switch");
8443 module_param(dbg_wwanemul
, uint
, 0444);
8444 MODULE_PARM_DESC(dbg_wwanemul
, "Enables WWAN switch emulation");
8445 module_param_named(wwan_state
, tpacpi_wwan_emulstate
, bool, 0);
8446 MODULE_PARM_DESC(wwan_state
,
8447 "Initial state of the emulated WWAN switch");
8449 module_param(dbg_uwbemul
, uint
, 0444);
8450 MODULE_PARM_DESC(dbg_uwbemul
, "Enables UWB switch emulation");
8451 module_param_named(uwb_state
, tpacpi_uwb_emulstate
, bool, 0);
8452 MODULE_PARM_DESC(uwb_state
,
8453 "Initial state of the emulated UWB switch");
8456 static void thinkpad_acpi_module_exit(void)
8458 struct ibm_struct
*ibm
, *itmp
;
8460 tpacpi_lifecycle
= TPACPI_LIFE_EXITING
;
8462 list_for_each_entry_safe_reverse(ibm
, itmp
,
8463 &tpacpi_all_drivers
,
8468 dbg_printk(TPACPI_DBG_INIT
, "finished subdriver exit path...\n");
8470 if (tpacpi_inputdev
) {
8471 if (tp_features
.input_device_registered
)
8472 input_unregister_device(tpacpi_inputdev
);
8474 input_free_device(tpacpi_inputdev
);
8478 hwmon_device_unregister(tpacpi_hwmon
);
8480 if (tp_features
.sensors_pdev_attrs_registered
)
8481 device_remove_file(&tpacpi_sensors_pdev
->dev
,
8482 &dev_attr_thinkpad_acpi_pdev_name
);
8483 if (tpacpi_sensors_pdev
)
8484 platform_device_unregister(tpacpi_sensors_pdev
);
8486 platform_device_unregister(tpacpi_pdev
);
8488 if (tp_features
.sensors_pdrv_attrs_registered
)
8489 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver
.driver
);
8490 if (tp_features
.platform_drv_attrs_registered
)
8491 tpacpi_remove_driver_attributes(&tpacpi_pdriver
.driver
);
8493 if (tp_features
.sensors_pdrv_registered
)
8494 platform_driver_unregister(&tpacpi_hwmon_pdriver
);
8496 if (tp_features
.platform_drv_registered
)
8497 platform_driver_unregister(&tpacpi_pdriver
);
8500 remove_proc_entry(TPACPI_PROC_DIR
, acpi_root_dir
);
8503 destroy_workqueue(tpacpi_wq
);
8505 kfree(thinkpad_id
.bios_version_str
);
8506 kfree(thinkpad_id
.ec_version_str
);
8507 kfree(thinkpad_id
.model_str
);
8511 static int __init
thinkpad_acpi_module_init(void)
8515 tpacpi_lifecycle
= TPACPI_LIFE_INIT
;
8517 /* Parameter checking */
8518 if (hotkey_report_mode
> 2)
8521 /* Driver-level probe */
8523 ret
= get_thinkpad_model_data(&thinkpad_id
);
8526 "unable to get DMI data: %d\n", ret
);
8527 thinkpad_acpi_module_exit();
8530 ret
= probe_for_thinkpad();
8532 thinkpad_acpi_module_exit();
8536 /* Driver initialization */
8538 TPACPI_ACPIHANDLE_INIT(ecrd
);
8539 TPACPI_ACPIHANDLE_INIT(ecwr
);
8541 tpacpi_wq
= create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME
);
8543 thinkpad_acpi_module_exit();
8547 proc_dir
= proc_mkdir(TPACPI_PROC_DIR
, acpi_root_dir
);
8550 "unable to create proc dir " TPACPI_PROC_DIR
);
8551 thinkpad_acpi_module_exit();
8555 ret
= platform_driver_register(&tpacpi_pdriver
);
8558 "unable to register main platform driver\n");
8559 thinkpad_acpi_module_exit();
8562 tp_features
.platform_drv_registered
= 1;
8564 ret
= platform_driver_register(&tpacpi_hwmon_pdriver
);
8567 "unable to register hwmon platform driver\n");
8568 thinkpad_acpi_module_exit();
8571 tp_features
.sensors_pdrv_registered
= 1;
8573 ret
= tpacpi_create_driver_attributes(&tpacpi_pdriver
.driver
);
8575 tp_features
.platform_drv_attrs_registered
= 1;
8576 ret
= tpacpi_create_driver_attributes(
8577 &tpacpi_hwmon_pdriver
.driver
);
8581 "unable to create sysfs driver attributes\n");
8582 thinkpad_acpi_module_exit();
8585 tp_features
.sensors_pdrv_attrs_registered
= 1;
8588 /* Device initialization */
8589 tpacpi_pdev
= platform_device_register_simple(TPACPI_DRVR_NAME
, -1,
8591 if (IS_ERR(tpacpi_pdev
)) {
8592 ret
= PTR_ERR(tpacpi_pdev
);
8594 printk(TPACPI_ERR
"unable to register platform device\n");
8595 thinkpad_acpi_module_exit();
8598 tpacpi_sensors_pdev
= platform_device_register_simple(
8599 TPACPI_HWMON_DRVR_NAME
,
8601 if (IS_ERR(tpacpi_sensors_pdev
)) {
8602 ret
= PTR_ERR(tpacpi_sensors_pdev
);
8603 tpacpi_sensors_pdev
= NULL
;
8605 "unable to register hwmon platform device\n");
8606 thinkpad_acpi_module_exit();
8609 ret
= device_create_file(&tpacpi_sensors_pdev
->dev
,
8610 &dev_attr_thinkpad_acpi_pdev_name
);
8613 "unable to create sysfs hwmon device attributes\n");
8614 thinkpad_acpi_module_exit();
8617 tp_features
.sensors_pdev_attrs_registered
= 1;
8618 tpacpi_hwmon
= hwmon_device_register(&tpacpi_sensors_pdev
->dev
);
8619 if (IS_ERR(tpacpi_hwmon
)) {
8620 ret
= PTR_ERR(tpacpi_hwmon
);
8621 tpacpi_hwmon
= NULL
;
8622 printk(TPACPI_ERR
"unable to register hwmon device\n");
8623 thinkpad_acpi_module_exit();
8626 mutex_init(&tpacpi_inputdev_send_mutex
);
8627 tpacpi_inputdev
= input_allocate_device();
8628 if (!tpacpi_inputdev
) {
8629 printk(TPACPI_ERR
"unable to allocate input device\n");
8630 thinkpad_acpi_module_exit();
8633 /* Prepare input device, but don't register */
8634 tpacpi_inputdev
->name
= "ThinkPad Extra Buttons";
8635 tpacpi_inputdev
->phys
= TPACPI_DRVR_NAME
"/input0";
8636 tpacpi_inputdev
->id
.bustype
= BUS_HOST
;
8637 tpacpi_inputdev
->id
.vendor
= (thinkpad_id
.vendor
) ?
8638 thinkpad_id
.vendor
:
8640 tpacpi_inputdev
->id
.product
= TPACPI_HKEY_INPUT_PRODUCT
;
8641 tpacpi_inputdev
->id
.version
= TPACPI_HKEY_INPUT_VERSION
;
8642 tpacpi_inputdev
->dev
.parent
= &tpacpi_pdev
->dev
;
8644 for (i
= 0; i
< ARRAY_SIZE(ibms_init
); i
++) {
8645 ret
= ibm_init(&ibms_init
[i
]);
8646 if (ret
>= 0 && *ibms_init
[i
].param
)
8647 ret
= ibms_init
[i
].data
->write(ibms_init
[i
].param
);
8649 thinkpad_acpi_module_exit();
8653 ret
= input_register_device(tpacpi_inputdev
);
8655 printk(TPACPI_ERR
"unable to register input device\n");
8656 thinkpad_acpi_module_exit();
8659 tp_features
.input_device_registered
= 1;
8662 tpacpi_lifecycle
= TPACPI_LIFE_RUNNING
;
8666 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME
);
8669 * This will autoload the driver in almost every ThinkPad
8670 * in widespread use.
8672 * Only _VERY_ old models, like the 240, 240x and 570 lack
8673 * the HKEY event interface.
8675 MODULE_DEVICE_TABLE(acpi
, ibm_htk_device_ids
);
8678 * DMI matching for module autoloading
8680 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8681 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8683 * Only models listed in thinkwiki will be supported, so add yours
8684 * if it is not there yet.
8686 #define IBM_BIOS_MODULE_ALIAS(__type) \
8687 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8689 /* Ancient thinkpad BIOSes have to be identified by
8690 * BIOS type or model number, and there are far less
8691 * BIOS types than model numbers... */
8692 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8694 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8695 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8696 MODULE_DESCRIPTION(TPACPI_DESC
);
8697 MODULE_VERSION(TPACPI_VERSION
);
8698 MODULE_LICENSE("GPL");
8700 module_init(thinkpad_acpi_module_init
);
8701 module_exit(thinkpad_acpi_module_exit
);