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.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
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>
61 #include <linux/slab.h>
63 #include <linux/nvram.h>
64 #include <linux/proc_fs.h>
65 #include <linux/seq_file.h>
66 #include <linux/sysfs.h>
67 #include <linux/backlight.h>
69 #include <linux/platform_device.h>
70 #include <linux/hwmon.h>
71 #include <linux/hwmon-sysfs.h>
72 #include <linux/input.h>
73 #include <linux/leds.h>
74 #include <linux/rfkill.h>
75 #include <asm/uaccess.h>
77 #include <linux/dmi.h>
78 #include <linux/jiffies.h>
79 #include <linux/workqueue.h>
81 #include <sound/core.h>
82 #include <sound/control.h>
83 #include <sound/initval.h>
85 #include <acpi/acpi_drivers.h>
87 #include <linux/pci_ids.h>
90 /* ThinkPad CMOS commands */
91 #define TP_CMOS_VOLUME_DOWN 0
92 #define TP_CMOS_VOLUME_UP 1
93 #define TP_CMOS_VOLUME_MUTE 2
94 #define TP_CMOS_BRIGHTNESS_UP 4
95 #define TP_CMOS_BRIGHTNESS_DOWN 5
96 #define TP_CMOS_THINKLIGHT_ON 12
97 #define TP_CMOS_THINKLIGHT_OFF 13
101 TP_NVRAM_ADDR_HK2 = 0x57,
102 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
103 TP_NVRAM_ADDR_VIDEO = 0x59,
104 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
105 TP_NVRAM_ADDR_MIXER = 0x60,
108 /* NVRAM bit masks */
110 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
111 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
112 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
113 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
114 TP_NVRAM_MASK_THINKLIGHT = 0x10,
115 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
116 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
117 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
118 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
119 TP_NVRAM_MASK_MUTE = 0x40,
120 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
121 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
122 TP_NVRAM_POS_LEVEL_VOLUME = 0,
126 #define TPACPI_ACPI_HKEY_HID "IBM0068"
129 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
130 #define TPACPI_HKEY_INPUT_VERSION 0x4101
132 /* ACPI \WGSV commands */
134 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
135 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
136 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
137 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
140 /* TP_ACPI_WGSV_GET_STATE bits */
142 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
143 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
144 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
145 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
146 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
147 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
148 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
149 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
150 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
151 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
155 enum tpacpi_hkey_event_t {
157 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
158 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
159 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
160 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
161 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
162 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
164 /* Reasons for waking up from S3/S4 */
165 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
166 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
167 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
168 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
169 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
170 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
172 /* Auto-sleep after eject request */
173 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
174 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
176 /* Misc bay events */
177 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
179 /* User-interface events */
180 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
181 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
182 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
183 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
184 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
185 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
186 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
189 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
190 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
191 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
192 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
193 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
196 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
199 /****************************************************************************
203 #define TPACPI_NAME "thinkpad"
204 #define TPACPI_DESC "ThinkPad ACPI Extras"
205 #define TPACPI_FILE TPACPI_NAME "_acpi"
206 #define TPACPI_URL "http://ibm-acpi.sf.net/"
207 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
209 #define TPACPI_PROC_DIR "ibm"
210 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
211 #define TPACPI_DRVR_NAME TPACPI_FILE
212 #define TPACPI_DRVR_SHORTNAME "tpacpi"
213 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
215 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
216 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
218 #define TPACPI_MAX_ACPI_ARGS 3
221 #define TPACPI_LOG TPACPI_FILE ": "
222 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
223 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
224 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
225 #define TPACPI_ERR KERN_ERR TPACPI_LOG
226 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
227 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
228 #define TPACPI_INFO KERN_INFO TPACPI_LOG
229 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
231 /* Debugging printk groups */
232 #define TPACPI_DBG_ALL 0xffff
233 #define TPACPI_DBG_DISCLOSETASK 0x8000
234 #define TPACPI_DBG_INIT 0x0001
235 #define TPACPI_DBG_EXIT 0x0002
236 #define TPACPI_DBG_RFKILL 0x0004
237 #define TPACPI_DBG_HKEY 0x0008
238 #define TPACPI_DBG_FAN 0x0010
239 #define TPACPI_DBG_BRGHT 0x0020
240 #define TPACPI_DBG_MIXER 0x0040
242 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
243 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
244 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
247 /****************************************************************************
248 * Driver-wide structs and misc. variables
253 struct tp_acpi_drv_struct {
254 const struct acpi_device_id *hid;
255 struct acpi_driver *driver;
257 void (*notify) (struct ibm_struct *, u32);
260 struct acpi_device *device;
266 int (*read) (struct seq_file *);
267 int (*write) (char *);
269 void (*resume) (void);
270 void (*suspend) (pm_message_t state);
271 void (*shutdown) (void);
273 struct list_head all_drivers;
275 struct tp_acpi_drv_struct *acpi;
278 u8 acpi_driver_registered:1;
279 u8 acpi_notify_installed:1;
286 struct ibm_init_struct {
289 int (*init) (struct ibm_init_struct *);
290 mode_t base_procfs_mode;
291 struct ibm_struct *data;
302 u32 bright_16levels:1;
303 u32 bright_acpimode:1;
306 u32 fan_ctrl_status_undef:1;
308 u32 beep_needs_two_args:1;
309 u32 mixer_no_level_control:1;
310 u32 input_device_registered:1;
311 u32 platform_drv_registered:1;
312 u32 platform_drv_attrs_registered:1;
313 u32 sensors_pdrv_registered:1;
314 u32 sensors_pdrv_attrs_registered:1;
315 u32 sensors_pdev_attrs_registered:1;
316 u32 hotkey_poll_active:1;
320 u16 hotkey_mask_ff:1;
321 u16 volume_ctrl_forbidden:1;
324 struct thinkpad_id_data {
325 unsigned int vendor; /* ThinkPad vendor:
326 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
328 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
329 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
331 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
333 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
336 char *model_str; /* ThinkPad T43 */
337 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
339 static struct thinkpad_id_data thinkpad_id;
342 TPACPI_LIFE_INIT = 0,
347 static int experimental;
348 static u32 dbg_level;
350 static struct workqueue_struct *tpacpi_wq;
358 /* Special LED class that can defer work */
359 struct tpacpi_led_classdev {
360 struct led_classdev led_classdev;
361 struct work_struct work;
362 enum led_status_t new_state;
366 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
367 static int dbg_wlswemul;
368 static int tpacpi_wlsw_emulstate;
369 static int dbg_bluetoothemul;
370 static int tpacpi_bluetooth_emulstate;
371 static int dbg_wwanemul;
372 static int tpacpi_wwan_emulstate;
373 static int dbg_uwbemul;
374 static int tpacpi_uwb_emulstate;
378 /*************************************************************************
382 #define dbg_printk(a_dbg_level, format, arg...) \
383 do { if (dbg_level & (a_dbg_level)) \
384 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
387 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
388 #define vdbg_printk dbg_printk
389 static const char *str_supported(int is_supported);
391 #define vdbg_printk(a_dbg_level, format, arg...) \
395 static void tpacpi_log_usertask(const char * const what)
397 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
398 what, task_tgid_vnr(current));
401 #define tpacpi_disclose_usertask(what, format, arg...) \
404 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
405 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
406 printk(TPACPI_DEBUG "%s: PID %d: " format, \
407 what, task_tgid_vnr(current), ## arg); \
412 * Quirk handling helpers
414 * ThinkPad IDs and versions seen in the field so far
415 * are two-characters from the set [0-9A-Z], i.e. base 36.
417 * We use values well outside that range as specials.
420 #define TPACPI_MATCH_ANY 0xffffU
421 #define TPACPI_MATCH_UNKNOWN 0U
423 /* TPID('1', 'Y') == 0x5931 */
424 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
426 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
427 { .vendor = PCI_VENDOR_ID_IBM, \
428 .bios = TPID(__id1, __id2), \
429 .ec = TPACPI_MATCH_ANY, \
430 .quirks = (__quirk) }
432 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
433 { .vendor = PCI_VENDOR_ID_LENOVO, \
434 .bios = TPID(__id1, __id2), \
435 .ec = TPACPI_MATCH_ANY, \
436 .quirks = (__quirk) }
438 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
439 { .vendor = PCI_VENDOR_ID_LENOVO, \
440 .bios = TPACPI_MATCH_ANY, \
441 .ec = TPID(__id1, __id2), \
442 .quirks = (__quirk) }
444 struct tpacpi_quirk {
448 unsigned long quirks;
452 * tpacpi_check_quirks() - search BIOS/EC version on a list
453 * @qlist: array of &struct tpacpi_quirk
454 * @qlist_size: number of elements in @qlist
456 * Iterates over a quirks list until one is found that matches the
457 * ThinkPad's vendor, BIOS and EC model.
459 * Returns 0 if nothing matches, otherwise returns the quirks field of
460 * the matching &struct tpacpi_quirk entry.
462 * The match criteria is: vendor, ec and bios much match.
464 static unsigned long __init tpacpi_check_quirks(
465 const struct tpacpi_quirk *qlist,
466 unsigned int qlist_size)
469 if ((qlist->vendor == thinkpad_id.vendor ||
470 qlist->vendor == TPACPI_MATCH_ANY) &&
471 (qlist->bios == thinkpad_id.bios_model ||
472 qlist->bios == TPACPI_MATCH_ANY) &&
473 (qlist->ec == thinkpad_id.ec_model ||
474 qlist->ec == TPACPI_MATCH_ANY))
475 return qlist->quirks;
483 static inline bool __pure __init tpacpi_is_lenovo(void)
485 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
488 static inline bool __pure __init tpacpi_is_ibm(void)
490 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
493 /****************************************************************************
494 ****************************************************************************
496 * ACPI Helpers and device model
498 ****************************************************************************
499 ****************************************************************************/
501 /*************************************************************************
505 static acpi_handle root_handle;
507 #define TPACPI_HANDLE(object, parent, paths...) \
508 static acpi_handle object##_handle; \
509 static acpi_handle *object##_parent = &parent##_handle; \
510 static char *object##_path; \
511 static char *object##_paths[] = { paths }
513 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
514 "\\_SB.PCI.ISA.EC", /* 570 */
515 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
516 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
517 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
518 "\\_SB.PCI0.ICH3.EC0", /* R31 */
519 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
520 "\\_SB.PCI0.LPC.EC", /* all others */
523 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
526 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
532 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
537 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
538 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
539 "\\_SB.PCI0.VID0", /* 770e */
540 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
541 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
542 "\\_SB.PCI0.AGP.VID", /* all others */
546 /*************************************************************************
550 static int acpi_evalf(acpi_handle handle,
551 void *res, char *method, char *fmt, ...)
554 struct acpi_object_list params;
555 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
556 struct acpi_buffer result, *resultp;
557 union acpi_object out_obj;
565 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
578 params.pointer = &in_objs[0];
585 in_objs[params.count].integer.value = va_arg(ap, int);
586 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
588 /* add more types as needed */
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", c);
597 if (res_type != 'v') {
598 result.length = sizeof(out_obj);
599 result.pointer = &out_obj;
604 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
609 *(int *)res = out_obj.integer.value;
610 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
613 success = status == AE_OK;
615 /* add more types as needed */
617 printk(TPACPI_ERR "acpi_evalf() called "
618 "with invalid format character '%c'\n", res_type);
622 if (!success && !quiet)
623 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
624 method, fmt0, status);
629 static int acpi_ec_read(int i, u8 *p)
634 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
638 if (ec_read(i, p) < 0)
645 static int acpi_ec_write(int i, u8 v)
648 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
651 if (ec_write(i, v) < 0)
658 static int issue_thinkpad_cmos_command(int cmos_cmd)
663 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
669 /*************************************************************************
673 #define TPACPI_ACPIHANDLE_INIT(object) \
674 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
675 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
677 static void drv_acpi_handle_init(char *name,
678 acpi_handle *handle, acpi_handle parent,
679 char **paths, int num_paths, char **path)
684 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
687 for (i = 0; i < num_paths; i++) {
688 status = acpi_get_handle(parent, paths[i], handle);
689 if (ACPI_SUCCESS(status)) {
691 dbg_printk(TPACPI_DBG_INIT,
692 "Found ACPI handle %s for %s\n",
698 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
703 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
705 struct ibm_struct *ibm = data;
707 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
710 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
713 ibm->acpi->notify(ibm, event);
716 static int __init setup_acpi_notify(struct ibm_struct *ibm)
723 if (!*ibm->acpi->handle)
726 vdbg_printk(TPACPI_DBG_INIT,
727 "setting up ACPI notify for %s\n", ibm->name);
729 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
731 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
736 ibm->acpi->device->driver_data = ibm;
737 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
738 TPACPI_ACPI_EVENT_PREFIX,
741 status = acpi_install_notify_handler(*ibm->acpi->handle,
742 ibm->acpi->type, dispatch_acpi_notify, ibm);
743 if (ACPI_FAILURE(status)) {
744 if (status == AE_ALREADY_EXISTS) {
746 "another device driver is already "
747 "handling %s events\n", ibm->name);
750 "acpi_install_notify_handler(%s) failed: %d\n",
755 ibm->flags.acpi_notify_installed = 1;
759 static int __init tpacpi_device_add(struct acpi_device *device)
764 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
768 dbg_printk(TPACPI_DBG_INIT,
769 "registering %s as an ACPI driver\n", ibm->name);
773 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
774 if (!ibm->acpi->driver) {
776 "failed to allocate memory for ibm->acpi->driver\n");
780 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
781 ibm->acpi->driver->ids = ibm->acpi->hid;
783 ibm->acpi->driver->ops.add = &tpacpi_device_add;
785 rc = acpi_bus_register_driver(ibm->acpi->driver);
787 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
789 kfree(ibm->acpi->driver);
790 ibm->acpi->driver = NULL;
792 ibm->flags.acpi_driver_registered = 1;
798 /****************************************************************************
799 ****************************************************************************
803 ****************************************************************************
804 ****************************************************************************/
806 static int dispatch_proc_show(struct seq_file *m, void *v)
808 struct ibm_struct *ibm = m->private;
810 if (!ibm || !ibm->read)
815 static int dispatch_proc_open(struct inode *inode, struct file *file)
817 return single_open(file, dispatch_proc_show, PDE(inode)->data);
820 static ssize_t dispatch_proc_write(struct file *file,
821 const char __user *userbuf,
822 size_t count, loff_t *pos)
824 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
828 if (!ibm || !ibm->write)
830 if (count > PAGE_SIZE - 2)
833 kernbuf = kmalloc(count + 2, GFP_KERNEL);
837 if (copy_from_user(kernbuf, userbuf, count)) {
843 strcat(kernbuf, ",");
844 ret = ibm->write(kernbuf);
853 static const struct file_operations dispatch_proc_fops = {
854 .owner = THIS_MODULE,
855 .open = dispatch_proc_open,
858 .release = single_release,
859 .write = dispatch_proc_write,
862 static char *next_cmd(char **cmds)
867 while ((end = strchr(start, ',')) && end == start)
879 /****************************************************************************
880 ****************************************************************************
882 * Device model: input, hwmon and platform
884 ****************************************************************************
885 ****************************************************************************/
887 static struct platform_device *tpacpi_pdev;
888 static struct platform_device *tpacpi_sensors_pdev;
889 static struct device *tpacpi_hwmon;
890 static struct input_dev *tpacpi_inputdev;
891 static struct mutex tpacpi_inputdev_send_mutex;
892 static LIST_HEAD(tpacpi_all_drivers);
894 static int tpacpi_suspend_handler(struct platform_device *pdev,
897 struct ibm_struct *ibm, *itmp;
899 list_for_each_entry_safe(ibm, itmp,
903 (ibm->suspend)(state);
909 static int tpacpi_resume_handler(struct platform_device *pdev)
911 struct ibm_struct *ibm, *itmp;
913 list_for_each_entry_safe(ibm, itmp,
923 static void tpacpi_shutdown_handler(struct platform_device *pdev)
925 struct ibm_struct *ibm, *itmp;
927 list_for_each_entry_safe(ibm, itmp,
935 static struct platform_driver tpacpi_pdriver = {
937 .name = TPACPI_DRVR_NAME,
938 .owner = THIS_MODULE,
940 .suspend = tpacpi_suspend_handler,
941 .resume = tpacpi_resume_handler,
942 .shutdown = tpacpi_shutdown_handler,
945 static struct platform_driver tpacpi_hwmon_pdriver = {
947 .name = TPACPI_HWMON_DRVR_NAME,
948 .owner = THIS_MODULE,
952 /*************************************************************************
953 * sysfs support helpers
956 struct attribute_set {
957 unsigned int members, max_members;
958 struct attribute_group group;
961 struct attribute_set_obj {
962 struct attribute_set s;
964 } __attribute__((packed));
966 static struct attribute_set *create_attr_set(unsigned int max_members,
969 struct attribute_set_obj *sobj;
971 if (max_members == 0)
974 /* Allocates space for implicit NULL at the end too */
975 sobj = kzalloc(sizeof(struct attribute_set_obj) +
976 max_members * sizeof(struct attribute *),
980 sobj->s.max_members = max_members;
981 sobj->s.group.attrs = &sobj->a;
982 sobj->s.group.name = name;
987 #define destroy_attr_set(_set) \
990 /* not multi-threaded safe, use it in a single thread per set */
991 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
996 if (s->members >= s->max_members)
999 s->group.attrs[s->members] = attr;
1005 static int add_many_to_attr_set(struct attribute_set *s,
1006 struct attribute **attr,
1011 for (i = 0; i < count; i++) {
1012 res = add_to_attr_set(s, attr[i]);
1020 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1022 sysfs_remove_group(kobj, &s->group);
1023 destroy_attr_set(s);
1026 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1027 sysfs_create_group(_kobj, &_attr_set->group)
1029 static int parse_strtoul(const char *buf,
1030 unsigned long max, unsigned long *value)
1034 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1035 endp = skip_spaces(endp);
1036 if (*endp || *value > max)
1042 static void tpacpi_disable_brightness_delay(void)
1044 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1045 printk(TPACPI_NOTICE
1046 "ACPI backlight control delay disabled\n");
1049 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1051 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1052 union acpi_object *obj;
1055 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1056 obj = (union acpi_object *)buffer.pointer;
1057 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1058 printk(TPACPI_ERR "Unknown _BCL data, "
1059 "please report this to %s\n", TPACPI_MAIL);
1062 rc = obj->package.count;
1068 kfree(buffer.pointer);
1072 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1073 u32 lvl, void *context, void **rv)
1075 char name[ACPI_PATH_SEGMENT_LENGTH];
1076 struct acpi_buffer buffer = { sizeof(name), &name };
1078 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1079 !strncmp("_BCL", name, sizeof(name) - 1)) {
1080 BUG_ON(!rv || !*rv);
1081 **(int **)rv = tpacpi_query_bcl_levels(handle);
1082 return AE_CTRL_TERMINATE;
1089 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1091 static int __init tpacpi_check_std_acpi_brightness_support(void)
1095 void *bcl_ptr = &bcl_levels;
1098 TPACPI_ACPIHANDLE_INIT(vid);
1104 * Search for a _BCL method, and execute it. This is safe on all
1105 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1106 * BIOS in ACPI backlight control mode. We do NOT have to care
1107 * about calling the _BCL method in an enabled video device, any
1108 * will do for our purposes.
1111 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1112 tpacpi_acpi_walk_find_bcl, NULL, NULL,
1115 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1116 tp_features.bright_acpimode = 1;
1117 return (bcl_levels - 2);
1123 static void printk_deprecated_attribute(const char * const what,
1124 const char * const details)
1126 tpacpi_log_usertask("deprecated sysfs attribute");
1127 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1128 "will be removed. %s\n",
1132 /*************************************************************************
1133 * rfkill and radio control support helpers
1137 * ThinkPad-ACPI firmware handling model:
1139 * WLSW (master wireless switch) is event-driven, and is common to all
1140 * firmware-controlled radios. It cannot be controlled, just monitored,
1141 * as expected. It overrides all radio state in firmware
1143 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1144 * (TODO: verify how WLSW interacts with the returned radio state).
1146 * The only time there are shadow radio state changes, is when
1147 * masked-off hotkeys are used.
1151 * Internal driver API for radio state:
1153 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1154 * bool: true means radio blocked (off)
1156 enum tpacpi_rfkill_state {
1157 TPACPI_RFK_RADIO_OFF = 0,
1161 /* rfkill switches */
1162 enum tpacpi_rfk_id {
1163 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1164 TPACPI_RFK_WWAN_SW_ID,
1165 TPACPI_RFK_UWB_SW_ID,
1169 static const char *tpacpi_rfkill_names[] = {
1170 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1171 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1172 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1173 [TPACPI_RFK_SW_MAX] = NULL
1176 /* ThinkPad-ACPI rfkill subdriver */
1178 struct rfkill *rfkill;
1179 enum tpacpi_rfk_id id;
1180 const struct tpacpi_rfk_ops *ops;
1183 struct tpacpi_rfk_ops {
1184 /* firmware interface */
1185 int (*get_status)(void);
1186 int (*set_status)(const enum tpacpi_rfkill_state);
1189 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1191 /* Query FW and update rfkill sw state for a given rfkill switch */
1192 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1199 status = (tp_rfk->ops->get_status)();
1203 rfkill_set_sw_state(tp_rfk->rfkill,
1204 (status == TPACPI_RFK_RADIO_OFF));
1209 /* Query FW and update rfkill sw state for all rfkill switches */
1210 static void tpacpi_rfk_update_swstate_all(void)
1214 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1215 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1219 * Sync the HW-blocking state of all rfkill switches,
1220 * do notice it causes the rfkill core to schedule uevents
1222 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1225 struct tpacpi_rfk *tp_rfk;
1227 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1228 tp_rfk = tpacpi_rfkill_switches[i];
1230 if (rfkill_set_hw_state(tp_rfk->rfkill,
1232 /* ignore -- we track sw block */
1238 /* Call to get the WLSW state from the firmware */
1239 static int hotkey_get_wlsw(void);
1241 /* Call to query WLSW state and update all rfkill switches */
1242 static bool tpacpi_rfk_check_hwblock_state(void)
1244 int res = hotkey_get_wlsw();
1247 /* When unknown or unsupported, we have to assume it is unblocked */
1251 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1252 tpacpi_rfk_update_hwblock_state(hw_blocked);
1257 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1259 struct tpacpi_rfk *tp_rfk = data;
1262 dbg_printk(TPACPI_DBG_RFKILL,
1263 "request to change radio state to %s\n",
1264 blocked ? "blocked" : "unblocked");
1266 /* try to set radio state */
1267 res = (tp_rfk->ops->set_status)(blocked ?
1268 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1270 /* and update the rfkill core with whatever the FW really did */
1271 tpacpi_rfk_update_swstate(tp_rfk);
1273 return (res < 0) ? res : 0;
1276 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1277 .set_block = tpacpi_rfk_hook_set_block,
1280 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1281 const struct tpacpi_rfk_ops *tp_rfkops,
1282 const enum rfkill_type rfktype,
1284 const bool set_default)
1286 struct tpacpi_rfk *atp_rfk;
1288 bool sw_state = false;
1292 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1294 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1296 atp_rfk->rfkill = rfkill_alloc(name,
1299 &tpacpi_rfk_rfkill_ops,
1301 if (!atp_rfk || !atp_rfk->rfkill) {
1303 "failed to allocate memory for rfkill class\n");
1309 atp_rfk->ops = tp_rfkops;
1311 sw_status = (tp_rfkops->get_status)();
1312 if (sw_status < 0) {
1314 "failed to read initial state for %s, error %d\n",
1317 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1319 /* try to keep the initial state, since we ask the
1320 * firmware to preserve it across S5 in NVRAM */
1321 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1324 hw_state = tpacpi_rfk_check_hwblock_state();
1325 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1327 res = rfkill_register(atp_rfk->rfkill);
1330 "failed to register %s rfkill switch: %d\n",
1332 rfkill_destroy(atp_rfk->rfkill);
1337 tpacpi_rfkill_switches[id] = atp_rfk;
1339 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1340 name, (sw_state || hw_state) ? "" : "un");
1344 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1346 struct tpacpi_rfk *tp_rfk;
1348 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1350 tp_rfk = tpacpi_rfkill_switches[id];
1352 rfkill_unregister(tp_rfk->rfkill);
1353 rfkill_destroy(tp_rfk->rfkill);
1354 tpacpi_rfkill_switches[id] = NULL;
1359 static void printk_deprecated_rfkill_attribute(const char * const what)
1361 printk_deprecated_attribute(what,
1362 "Please switch to generic rfkill before year 2010");
1365 /* sysfs <radio> enable ------------------------------------------------ */
1366 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1367 struct device_attribute *attr,
1372 printk_deprecated_rfkill_attribute(attr->attr.name);
1374 /* This is in the ABI... */
1375 if (tpacpi_rfk_check_hwblock_state()) {
1376 status = TPACPI_RFK_RADIO_OFF;
1378 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1383 return snprintf(buf, PAGE_SIZE, "%d\n",
1384 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1387 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1388 struct device_attribute *attr,
1389 const char *buf, size_t count)
1394 printk_deprecated_rfkill_attribute(attr->attr.name);
1396 if (parse_strtoul(buf, 1, &t))
1399 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1401 /* This is in the ABI... */
1402 if (tpacpi_rfk_check_hwblock_state() && !!t)
1405 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1406 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1407 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1409 return (res < 0) ? res : count;
1412 /* procfs -------------------------------------------------------------- */
1413 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1415 if (id >= TPACPI_RFK_SW_MAX)
1416 seq_printf(m, "status:\t\tnot supported\n");
1420 /* This is in the ABI... */
1421 if (tpacpi_rfk_check_hwblock_state()) {
1422 status = TPACPI_RFK_RADIO_OFF;
1424 status = tpacpi_rfk_update_swstate(
1425 tpacpi_rfkill_switches[id]);
1430 seq_printf(m, "status:\t\t%s\n",
1431 (status == TPACPI_RFK_RADIO_ON) ?
1432 "enabled" : "disabled");
1433 seq_printf(m, "commands:\tenable, disable\n");
1439 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1445 if (id >= TPACPI_RFK_SW_MAX)
1448 while ((cmd = next_cmd(&buf))) {
1449 if (strlencmp(cmd, "enable") == 0)
1450 status = TPACPI_RFK_RADIO_ON;
1451 else if (strlencmp(cmd, "disable") == 0)
1452 status = TPACPI_RFK_RADIO_OFF;
1458 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1459 (status == TPACPI_RFK_RADIO_ON) ?
1460 "enable" : "disable",
1461 tpacpi_rfkill_names[id]);
1462 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1463 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1469 /*************************************************************************
1470 * thinkpad-acpi driver attributes
1473 /* interface_version --------------------------------------------------- */
1474 static ssize_t tpacpi_driver_interface_version_show(
1475 struct device_driver *drv,
1478 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1481 static DRIVER_ATTR(interface_version, S_IRUGO,
1482 tpacpi_driver_interface_version_show, NULL);
1484 /* debug_level --------------------------------------------------------- */
1485 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1488 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1491 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1492 const char *buf, size_t count)
1496 if (parse_strtoul(buf, 0xffff, &t))
1504 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1505 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1507 /* version ------------------------------------------------------------- */
1508 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1511 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1512 TPACPI_DESC, TPACPI_VERSION);
1515 static DRIVER_ATTR(version, S_IRUGO,
1516 tpacpi_driver_version_show, NULL);
1518 /* --------------------------------------------------------------------- */
1520 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1522 /* wlsw_emulstate ------------------------------------------------------ */
1523 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1526 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1529 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1530 const char *buf, size_t count)
1534 if (parse_strtoul(buf, 1, &t))
1537 if (tpacpi_wlsw_emulstate != !!t) {
1538 tpacpi_wlsw_emulstate = !!t;
1539 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1545 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1546 tpacpi_driver_wlsw_emulstate_show,
1547 tpacpi_driver_wlsw_emulstate_store);
1549 /* bluetooth_emulstate ------------------------------------------------- */
1550 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1551 struct device_driver *drv,
1554 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1557 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1558 struct device_driver *drv,
1559 const char *buf, size_t count)
1563 if (parse_strtoul(buf, 1, &t))
1566 tpacpi_bluetooth_emulstate = !!t;
1571 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1572 tpacpi_driver_bluetooth_emulstate_show,
1573 tpacpi_driver_bluetooth_emulstate_store);
1575 /* wwan_emulstate ------------------------------------------------- */
1576 static ssize_t tpacpi_driver_wwan_emulstate_show(
1577 struct device_driver *drv,
1580 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1583 static ssize_t tpacpi_driver_wwan_emulstate_store(
1584 struct device_driver *drv,
1585 const char *buf, size_t count)
1589 if (parse_strtoul(buf, 1, &t))
1592 tpacpi_wwan_emulstate = !!t;
1597 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1598 tpacpi_driver_wwan_emulstate_show,
1599 tpacpi_driver_wwan_emulstate_store);
1601 /* uwb_emulstate ------------------------------------------------- */
1602 static ssize_t tpacpi_driver_uwb_emulstate_show(
1603 struct device_driver *drv,
1606 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1609 static ssize_t tpacpi_driver_uwb_emulstate_store(
1610 struct device_driver *drv,
1611 const char *buf, size_t count)
1615 if (parse_strtoul(buf, 1, &t))
1618 tpacpi_uwb_emulstate = !!t;
1623 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1624 tpacpi_driver_uwb_emulstate_show,
1625 tpacpi_driver_uwb_emulstate_store);
1628 /* --------------------------------------------------------------------- */
1630 static struct driver_attribute *tpacpi_driver_attributes[] = {
1631 &driver_attr_debug_level, &driver_attr_version,
1632 &driver_attr_interface_version,
1635 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1641 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1642 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1646 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1647 if (!res && dbg_wlswemul)
1648 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1649 if (!res && dbg_bluetoothemul)
1650 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1651 if (!res && dbg_wwanemul)
1652 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1653 if (!res && dbg_uwbemul)
1654 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1660 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1664 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1665 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1667 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1668 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1669 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1670 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1671 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1675 /*************************************************************************
1680 * Table of recommended minimum BIOS versions
1682 * Reasons for listing:
1683 * 1. Stable BIOS, listed because the unknown amount of
1684 * bugs and bad ACPI behaviour on older versions
1686 * 2. BIOS or EC fw with known bugs that trigger on Linux
1688 * 3. BIOS with known reduced functionality in older versions
1690 * We recommend the latest BIOS and EC version.
1691 * We only support the latest BIOS and EC fw version as a rule.
1693 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1694 * Information from users in ThinkWiki
1696 * WARNING: we use this table also to detect that the machine is
1697 * a ThinkPad in some cases, so don't remove entries lightly.
1700 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1701 { .vendor = (__v), \
1702 .bios = TPID(__id1, __id2), \
1703 .ec = TPACPI_MATCH_ANY, \
1704 .quirks = TPACPI_MATCH_ANY << 16 \
1705 | (__bv1) << 8 | (__bv2) }
1707 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1708 __eid, __ev1, __ev2) \
1709 { .vendor = (__v), \
1710 .bios = TPID(__bid1, __bid2), \
1712 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1713 | (__bv1) << 8 | (__bv2) }
1715 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1716 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1718 /* Outdated IBM BIOSes often lack the EC id string */
1719 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1721 __bv1, __bv2, TPID(__id1, __id2), \
1723 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1724 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1727 /* Outdated IBM BIOSes often lack the EC id string */
1728 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1729 __eid1, __eid2, __ev1, __ev2) \
1730 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1731 __bv1, __bv2, TPID(__eid1, __eid2), \
1733 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1734 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1737 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1738 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1740 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1741 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1742 __bv1, __bv2, TPID(__id1, __id2), \
1745 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1746 __eid1, __eid2, __ev1, __ev2) \
1747 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1748 __bv1, __bv2, TPID(__eid1, __eid2), \
1751 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1752 /* Numeric models ------------------ */
1753 /* FW MODEL BIOS VERS */
1754 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1755 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1756 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1757 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1758 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1759 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1760 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1761 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1762 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1764 /* A-series ------------------------- */
1765 /* FW MODEL BIOS VERS EC VERS */
1766 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1767 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1768 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1769 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1770 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1771 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1772 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1773 TPV_QI0('1', '3', '2', '0'), /* A22m */
1774 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1775 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1776 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1778 /* G-series ------------------------- */
1779 /* FW MODEL BIOS VERS */
1780 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1781 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1783 /* R-series, T-series --------------- */
1784 /* FW MODEL BIOS VERS EC VERS */
1785 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1786 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1787 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1788 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1789 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1790 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1791 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1792 T40/p, T41/p, T42/p (1) */
1793 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1794 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1795 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1796 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1798 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1799 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1800 TPV_QI0('1', '6', '3', '2'), /* T22 */
1801 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1802 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1803 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1805 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1806 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1807 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1809 /* BIOS FW BIOS VERS EC FW EC VERS */
1810 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1811 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1813 /* X-series ------------------------- */
1814 /* FW MODEL BIOS VERS EC VERS */
1815 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1816 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1817 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1818 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1819 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1820 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1821 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1823 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1824 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1826 /* (0) - older versions lack DMI EC fw string and functionality */
1827 /* (1) - older versions known to lack functionality */
1838 static void __init tpacpi_check_outdated_fw(void)
1840 unsigned long fwvers;
1841 u16 ec_version, bios_version;
1843 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1844 ARRAY_SIZE(tpacpi_bios_version_qtable));
1849 bios_version = fwvers & 0xffffU;
1850 ec_version = (fwvers >> 16) & 0xffffU;
1852 /* note that unknown versions are set to 0x0000 and we use that */
1853 if ((bios_version > thinkpad_id.bios_release) ||
1854 (ec_version > thinkpad_id.ec_release &&
1855 ec_version != TPACPI_MATCH_ANY)) {
1857 * The changelogs would let us track down the exact
1858 * reason, but it is just too much of a pain to track
1859 * it. We only list BIOSes that are either really
1860 * broken, or really stable to begin with, so it is
1861 * best if the user upgrades the firmware anyway.
1864 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1866 "WARNING: This firmware may be missing critical bug "
1867 "fixes and/or important features\n");
1871 static bool __init tpacpi_is_fw_known(void)
1873 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1874 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1877 /****************************************************************************
1878 ****************************************************************************
1882 ****************************************************************************
1883 ****************************************************************************/
1885 /*************************************************************************
1886 * thinkpad-acpi init subdriver
1889 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1891 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1892 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1894 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1895 (thinkpad_id.bios_version_str) ?
1896 thinkpad_id.bios_version_str : "unknown",
1897 (thinkpad_id.ec_version_str) ?
1898 thinkpad_id.ec_version_str : "unknown");
1900 BUG_ON(!thinkpad_id.vendor);
1902 if (thinkpad_id.model_str)
1903 printk(TPACPI_INFO "%s %s, model %s\n",
1904 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1905 "IBM" : ((thinkpad_id.vendor ==
1906 PCI_VENDOR_ID_LENOVO) ?
1907 "Lenovo" : "Unknown vendor"),
1908 thinkpad_id.model_str,
1909 (thinkpad_id.nummodel_str) ?
1910 thinkpad_id.nummodel_str : "unknown");
1912 tpacpi_check_outdated_fw();
1916 static int thinkpad_acpi_driver_read(struct seq_file *m)
1918 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1919 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1923 static struct ibm_struct thinkpad_acpi_driver_data = {
1925 .read = thinkpad_acpi_driver_read,
1928 /*************************************************************************
1933 * ThinkPad firmware event model
1935 * The ThinkPad firmware has two main event interfaces: normal ACPI
1936 * notifications (which follow the ACPI standard), and a private event
1939 * The private event interface also issues events for the hotkeys. As
1940 * the driver gained features, the event handling code ended up being
1941 * built around the hotkey subdriver. This will need to be refactored
1942 * to a more formal event API eventually.
1944 * Some "hotkeys" are actually supposed to be used as event reports,
1945 * such as "brightness has changed", "volume has changed", depending on
1946 * the ThinkPad model and how the firmware is operating.
1948 * Unlike other classes, hotkey-class events have mask/unmask control on
1949 * non-ancient firmware. However, how it behaves changes a lot with the
1950 * firmware model and version.
1953 enum { /* hot key scan codes (derived from ACPI DSDT) */
1954 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1955 TP_ACPI_HOTKEYSCAN_FNF2,
1956 TP_ACPI_HOTKEYSCAN_FNF3,
1957 TP_ACPI_HOTKEYSCAN_FNF4,
1958 TP_ACPI_HOTKEYSCAN_FNF5,
1959 TP_ACPI_HOTKEYSCAN_FNF6,
1960 TP_ACPI_HOTKEYSCAN_FNF7,
1961 TP_ACPI_HOTKEYSCAN_FNF8,
1962 TP_ACPI_HOTKEYSCAN_FNF9,
1963 TP_ACPI_HOTKEYSCAN_FNF10,
1964 TP_ACPI_HOTKEYSCAN_FNF11,
1965 TP_ACPI_HOTKEYSCAN_FNF12,
1966 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1967 TP_ACPI_HOTKEYSCAN_FNINSERT,
1968 TP_ACPI_HOTKEYSCAN_FNDELETE,
1969 TP_ACPI_HOTKEYSCAN_FNHOME,
1970 TP_ACPI_HOTKEYSCAN_FNEND,
1971 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1972 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1973 TP_ACPI_HOTKEYSCAN_FNSPACE,
1974 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1975 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1976 TP_ACPI_HOTKEYSCAN_MUTE,
1977 TP_ACPI_HOTKEYSCAN_THINKPAD,
1980 enum { /* Keys/events available through NVRAM polling */
1981 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1982 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1985 enum { /* Positions of some of the keys in hotkey masks */
1986 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1987 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1988 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1989 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1990 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1991 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1992 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1993 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1994 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1995 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1996 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1999 enum { /* NVRAM to ACPI HKEY group map */
2000 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
2001 TP_ACPI_HKEY_ZOOM_MASK |
2002 TP_ACPI_HKEY_DISPSWTCH_MASK |
2003 TP_ACPI_HKEY_HIBERNATE_MASK,
2004 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
2005 TP_ACPI_HKEY_BRGHTDWN_MASK,
2006 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
2007 TP_ACPI_HKEY_VOLDWN_MASK |
2008 TP_ACPI_HKEY_MUTE_MASK,
2011 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2012 struct tp_nvram_state {
2013 u16 thinkpad_toggle:1;
2015 u16 display_toggle:1;
2016 u16 thinklight_toggle:1;
2017 u16 hibernate_toggle:1;
2018 u16 displayexp_toggle:1;
2019 u16 display_state:1;
2020 u16 brightness_toggle:1;
2021 u16 volume_toggle:1;
2024 u8 brightness_level;
2028 /* kthread for the hotkey poller */
2029 static struct task_struct *tpacpi_hotkey_task;
2031 /* Acquired while the poller kthread is running, use to sync start/stop */
2032 static struct mutex hotkey_thread_mutex;
2035 * Acquire mutex to write poller control variables as an
2038 * Increment hotkey_config_change when changing them if you
2039 * want the kthread to forget old state.
2041 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2043 static struct mutex hotkey_thread_data_mutex;
2044 static unsigned int hotkey_config_change;
2047 * hotkey poller control variables
2049 * Must be atomic or readers will also need to acquire mutex
2051 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2052 * should be used only when the changes need to be taken as
2053 * a block, OR when one needs to force the kthread to forget
2056 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2057 static unsigned int hotkey_poll_freq = 10; /* Hz */
2059 #define HOTKEY_CONFIG_CRITICAL_START \
2061 mutex_lock(&hotkey_thread_data_mutex); \
2062 hotkey_config_change++; \
2064 #define HOTKEY_CONFIG_CRITICAL_END \
2065 mutex_unlock(&hotkey_thread_data_mutex);
2067 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2069 #define hotkey_source_mask 0U
2070 #define HOTKEY_CONFIG_CRITICAL_START
2071 #define HOTKEY_CONFIG_CRITICAL_END
2073 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2075 static struct mutex hotkey_mutex;
2077 static enum { /* Reasons for waking up */
2078 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2079 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2080 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2081 } hotkey_wakeup_reason;
2083 static int hotkey_autosleep_ack;
2085 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2086 static u32 hotkey_all_mask; /* all events supported in fw */
2087 static u32 hotkey_reserved_mask; /* events better left disabled */
2088 static u32 hotkey_driver_mask; /* events needed by the driver */
2089 static u32 hotkey_user_mask; /* events visible to userspace */
2090 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2092 static unsigned int hotkey_report_mode;
2094 static u16 *hotkey_keycode_map;
2096 static struct attribute_set *hotkey_dev_attributes;
2098 static void tpacpi_driver_event(const unsigned int hkey_event);
2099 static void hotkey_driver_event(const unsigned int scancode);
2100 static void hotkey_poll_setup(const bool may_warn);
2102 /* HKEY.MHKG() return bits */
2103 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2105 static int hotkey_get_wlsw(void)
2109 if (!tp_features.hotkey_wlsw)
2112 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2114 return (tpacpi_wlsw_emulstate) ?
2115 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2118 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2121 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2124 static int hotkey_get_tablet_mode(int *status)
2128 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2131 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2136 * Reads current event mask from firmware, and updates
2137 * hotkey_acpi_mask accordingly. Also resets any bits
2138 * from hotkey_user_mask that are unavailable to be
2139 * delivered (shadow requirement of the userspace ABI).
2141 * Call with hotkey_mutex held
2143 static int hotkey_mask_get(void)
2145 if (tp_features.hotkey_mask) {
2148 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2151 hotkey_acpi_mask = m;
2153 /* no mask support doesn't mean no event support... */
2154 hotkey_acpi_mask = hotkey_all_mask;
2157 /* sync userspace-visible mask */
2158 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2163 void static hotkey_mask_warn_incomplete_mask(void)
2165 /* log only what the user can fix... */
2166 const u32 wantedmask = hotkey_driver_mask &
2167 ~(hotkey_acpi_mask | hotkey_source_mask) &
2168 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2171 printk(TPACPI_NOTICE
2172 "required events 0x%08x not enabled!\n",
2177 * Set the firmware mask when supported
2179 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2181 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2183 * Call with hotkey_mutex held
2185 static int hotkey_mask_set(u32 mask)
2190 const u32 fwmask = mask & ~hotkey_source_mask;
2192 if (tp_features.hotkey_mask) {
2193 for (i = 0; i < 32; i++) {
2194 if (!acpi_evalf(hkey_handle,
2195 NULL, "MHKM", "vdd", i + 1,
2196 !!(mask & (1 << i)))) {
2204 * We *must* make an inconditional call to hotkey_mask_get to
2205 * refresh hotkey_acpi_mask and update hotkey_user_mask
2207 * Take the opportunity to also log when we cannot _enable_
2210 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2211 printk(TPACPI_NOTICE
2212 "asked for hotkey mask 0x%08x, but "
2213 "firmware forced it to 0x%08x\n",
2214 fwmask, hotkey_acpi_mask);
2217 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2218 hotkey_mask_warn_incomplete_mask();
2224 * Sets hotkey_user_mask and tries to set the firmware mask
2226 * Call with hotkey_mutex held
2228 static int hotkey_user_mask_set(const u32 mask)
2232 /* Give people a chance to notice they are doing something that
2233 * is bound to go boom on their users sooner or later */
2234 if (!tp_warned.hotkey_mask_ff &&
2235 (mask == 0xffff || mask == 0xffffff ||
2236 mask == 0xffffffff)) {
2237 tp_warned.hotkey_mask_ff = 1;
2238 printk(TPACPI_NOTICE
2239 "setting the hotkey mask to 0x%08x is likely "
2240 "not the best way to go about it\n", mask);
2241 printk(TPACPI_NOTICE
2242 "please consider using the driver defaults, "
2243 "and refer to up-to-date thinkpad-acpi "
2247 /* Try to enable what the user asked for, plus whatever we need.
2248 * this syncs everything but won't enable bits in hotkey_user_mask */
2249 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2251 /* Enable the available bits in hotkey_user_mask */
2252 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2258 * Sets the driver hotkey mask.
2260 * Can be called even if the hotkey subdriver is inactive
2262 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2266 /* Do the right thing if hotkey_init has not been called yet */
2267 if (!tp_features.hotkey) {
2268 hotkey_driver_mask = mask;
2272 mutex_lock(&hotkey_mutex);
2274 HOTKEY_CONFIG_CRITICAL_START
2275 hotkey_driver_mask = mask;
2276 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2277 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2279 HOTKEY_CONFIG_CRITICAL_END
2281 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2282 ~hotkey_source_mask);
2283 hotkey_poll_setup(true);
2285 mutex_unlock(&hotkey_mutex);
2290 static int hotkey_status_get(int *status)
2292 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2298 static int hotkey_status_set(bool enable)
2300 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2306 static void tpacpi_input_send_tabletsw(void)
2310 if (tp_features.hotkey_tablet &&
2311 !hotkey_get_tablet_mode(&state)) {
2312 mutex_lock(&tpacpi_inputdev_send_mutex);
2314 input_report_switch(tpacpi_inputdev,
2315 SW_TABLET_MODE, !!state);
2316 input_sync(tpacpi_inputdev);
2318 mutex_unlock(&tpacpi_inputdev_send_mutex);
2322 /* Do NOT call without validating scancode first */
2323 static void tpacpi_input_send_key(const unsigned int scancode)
2325 const unsigned int keycode = hotkey_keycode_map[scancode];
2327 if (keycode != KEY_RESERVED) {
2328 mutex_lock(&tpacpi_inputdev_send_mutex);
2330 input_report_key(tpacpi_inputdev, keycode, 1);
2331 if (keycode == KEY_UNKNOWN)
2332 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2334 input_sync(tpacpi_inputdev);
2336 input_report_key(tpacpi_inputdev, keycode, 0);
2337 if (keycode == KEY_UNKNOWN)
2338 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2340 input_sync(tpacpi_inputdev);
2342 mutex_unlock(&tpacpi_inputdev_send_mutex);
2346 /* Do NOT call without validating scancode first */
2347 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2349 hotkey_driver_event(scancode);
2350 if (hotkey_user_mask & (1 << scancode))
2351 tpacpi_input_send_key(scancode);
2354 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2355 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2357 /* Do NOT call without validating scancode first */
2358 static void tpacpi_hotkey_send_key(unsigned int scancode)
2360 tpacpi_input_send_key_masked(scancode);
2361 if (hotkey_report_mode < 2) {
2362 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2363 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2367 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2371 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2372 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2373 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2374 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2375 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2376 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2378 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2379 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2380 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2382 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2383 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2384 n->displayexp_toggle =
2385 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2387 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2388 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2389 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2390 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2391 n->brightness_toggle =
2392 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2394 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2395 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2396 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2397 >> TP_NVRAM_POS_LEVEL_VOLUME;
2398 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2399 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2403 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2404 struct tp_nvram_state *newn,
2405 const u32 event_mask)
2408 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2410 if ((event_mask & (1 << __scancode)) && \
2411 oldn->__member != newn->__member) \
2412 tpacpi_hotkey_send_key(__scancode); \
2415 #define TPACPI_MAY_SEND_KEY(__scancode) \
2417 if (event_mask & (1 << __scancode)) \
2418 tpacpi_hotkey_send_key(__scancode); \
2421 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2422 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2423 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2424 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2426 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2428 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2431 if (oldn->volume_toggle != newn->volume_toggle) {
2432 if (oldn->mute != newn->mute) {
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2435 if (oldn->volume_level > newn->volume_level) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2437 } else if (oldn->volume_level < newn->volume_level) {
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2439 } else if (oldn->mute == newn->mute) {
2440 /* repeated key presses that didn't change state */
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2443 } else if (newn->volume_level != 0) {
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2451 /* handle brightness */
2452 if (oldn->brightness_toggle != newn->brightness_toggle) {
2453 if (oldn->brightness_level < newn->brightness_level) {
2454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2455 } else if (oldn->brightness_level > newn->brightness_level) {
2456 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2458 /* repeated key presses that didn't change state */
2459 if (newn->brightness_level != 0) {
2460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2467 #undef TPACPI_COMPARE_KEY
2468 #undef TPACPI_MAY_SEND_KEY
2474 * We track all events in hotkey_source_mask all the time, since
2475 * most of them are edge-based. We only issue those requested by
2476 * hotkey_user_mask or hotkey_driver_mask, though.
2478 static int hotkey_kthread(void *data)
2480 struct tp_nvram_state s[2];
2481 u32 poll_mask, event_mask;
2482 unsigned int si, so;
2484 unsigned int change_detector, must_reset;
2485 unsigned int poll_freq;
2487 mutex_lock(&hotkey_thread_mutex);
2489 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2498 /* Initial state for compares */
2499 mutex_lock(&hotkey_thread_data_mutex);
2500 change_detector = hotkey_config_change;
2501 poll_mask = hotkey_source_mask;
2502 event_mask = hotkey_source_mask &
2503 (hotkey_driver_mask | hotkey_user_mask);
2504 poll_freq = hotkey_poll_freq;
2505 mutex_unlock(&hotkey_thread_data_mutex);
2506 hotkey_read_nvram(&s[so], poll_mask);
2508 while (!kthread_should_stop()) {
2510 if (likely(poll_freq))
2513 t = 100; /* should never happen... */
2515 t = msleep_interruptible(t);
2516 if (unlikely(kthread_should_stop()))
2518 must_reset = try_to_freeze();
2519 if (t > 0 && !must_reset)
2522 mutex_lock(&hotkey_thread_data_mutex);
2523 if (must_reset || hotkey_config_change != change_detector) {
2524 /* forget old state on thaw or config change */
2527 change_detector = hotkey_config_change;
2529 poll_mask = hotkey_source_mask;
2530 event_mask = hotkey_source_mask &
2531 (hotkey_driver_mask | hotkey_user_mask);
2532 poll_freq = hotkey_poll_freq;
2533 mutex_unlock(&hotkey_thread_data_mutex);
2535 if (likely(poll_mask)) {
2536 hotkey_read_nvram(&s[si], poll_mask);
2537 if (likely(si != so)) {
2538 hotkey_compare_and_issue_event(&s[so], &s[si],
2548 mutex_unlock(&hotkey_thread_mutex);
2552 /* call with hotkey_mutex held */
2553 static void hotkey_poll_stop_sync(void)
2555 if (tpacpi_hotkey_task) {
2556 if (frozen(tpacpi_hotkey_task) ||
2557 freezing(tpacpi_hotkey_task))
2558 thaw_process(tpacpi_hotkey_task);
2560 kthread_stop(tpacpi_hotkey_task);
2561 tpacpi_hotkey_task = NULL;
2562 mutex_lock(&hotkey_thread_mutex);
2563 /* at this point, the thread did exit */
2564 mutex_unlock(&hotkey_thread_mutex);
2568 /* call with hotkey_mutex held */
2569 static void hotkey_poll_setup(const bool may_warn)
2571 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2572 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2574 if (hotkey_poll_freq > 0 &&
2575 (poll_driver_mask ||
2576 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2577 if (!tpacpi_hotkey_task) {
2578 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2579 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2580 if (IS_ERR(tpacpi_hotkey_task)) {
2581 tpacpi_hotkey_task = NULL;
2583 "could not create kernel thread "
2584 "for hotkey polling\n");
2588 hotkey_poll_stop_sync();
2589 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2590 hotkey_poll_freq == 0) {
2591 printk(TPACPI_NOTICE
2592 "hot keys 0x%08x and/or events 0x%08x "
2593 "require polling, which is currently "
2595 poll_user_mask, poll_driver_mask);
2600 static void hotkey_poll_setup_safe(const bool may_warn)
2602 mutex_lock(&hotkey_mutex);
2603 hotkey_poll_setup(may_warn);
2604 mutex_unlock(&hotkey_mutex);
2607 /* call with hotkey_mutex held */
2608 static void hotkey_poll_set_freq(unsigned int freq)
2611 hotkey_poll_stop_sync();
2613 hotkey_poll_freq = freq;
2616 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2618 static void hotkey_poll_setup(const bool __unused)
2622 static void hotkey_poll_setup_safe(const bool __unused)
2626 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2628 static int hotkey_inputdev_open(struct input_dev *dev)
2630 switch (tpacpi_lifecycle) {
2631 case TPACPI_LIFE_INIT:
2632 case TPACPI_LIFE_RUNNING:
2633 hotkey_poll_setup_safe(false);
2635 case TPACPI_LIFE_EXITING:
2639 /* Should only happen if tpacpi_lifecycle is corrupt */
2644 static void hotkey_inputdev_close(struct input_dev *dev)
2646 /* disable hotkey polling when possible */
2647 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2648 !(hotkey_source_mask & hotkey_driver_mask))
2649 hotkey_poll_setup_safe(false);
2652 /* sysfs hotkey enable ------------------------------------------------- */
2653 static ssize_t hotkey_enable_show(struct device *dev,
2654 struct device_attribute *attr,
2659 printk_deprecated_attribute("hotkey_enable",
2660 "Hotkey reporting is always enabled");
2662 res = hotkey_status_get(&status);
2666 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2669 static ssize_t hotkey_enable_store(struct device *dev,
2670 struct device_attribute *attr,
2671 const char *buf, size_t count)
2675 printk_deprecated_attribute("hotkey_enable",
2676 "Hotkeys can be disabled through hotkey_mask");
2678 if (parse_strtoul(buf, 1, &t))
2687 static struct device_attribute dev_attr_hotkey_enable =
2688 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2689 hotkey_enable_show, hotkey_enable_store);
2691 /* sysfs hotkey mask --------------------------------------------------- */
2692 static ssize_t hotkey_mask_show(struct device *dev,
2693 struct device_attribute *attr,
2696 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2699 static ssize_t hotkey_mask_store(struct device *dev,
2700 struct device_attribute *attr,
2701 const char *buf, size_t count)
2706 if (parse_strtoul(buf, 0xffffffffUL, &t))
2709 if (mutex_lock_killable(&hotkey_mutex))
2710 return -ERESTARTSYS;
2712 res = hotkey_user_mask_set(t);
2714 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2715 hotkey_poll_setup(true);
2718 mutex_unlock(&hotkey_mutex);
2720 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2722 return (res) ? res : count;
2725 static struct device_attribute dev_attr_hotkey_mask =
2726 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2727 hotkey_mask_show, hotkey_mask_store);
2729 /* sysfs hotkey bios_enabled ------------------------------------------- */
2730 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2731 struct device_attribute *attr,
2734 return sprintf(buf, "0\n");
2737 static struct device_attribute dev_attr_hotkey_bios_enabled =
2738 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2740 /* sysfs hotkey bios_mask ---------------------------------------------- */
2741 static ssize_t hotkey_bios_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2745 printk_deprecated_attribute("hotkey_bios_mask",
2746 "This attribute is useless.");
2747 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2750 static struct device_attribute dev_attr_hotkey_bios_mask =
2751 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2753 /* sysfs hotkey all_mask ----------------------------------------------- */
2754 static ssize_t hotkey_all_mask_show(struct device *dev,
2755 struct device_attribute *attr,
2758 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2759 hotkey_all_mask | hotkey_source_mask);
2762 static struct device_attribute dev_attr_hotkey_all_mask =
2763 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2765 /* sysfs hotkey recommended_mask --------------------------------------- */
2766 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2767 struct device_attribute *attr,
2770 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2771 (hotkey_all_mask | hotkey_source_mask)
2772 & ~hotkey_reserved_mask);
2775 static struct device_attribute dev_attr_hotkey_recommended_mask =
2776 __ATTR(hotkey_recommended_mask, S_IRUGO,
2777 hotkey_recommended_mask_show, NULL);
2779 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2781 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2782 static ssize_t hotkey_source_mask_show(struct device *dev,
2783 struct device_attribute *attr,
2786 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2789 static ssize_t hotkey_source_mask_store(struct device *dev,
2790 struct device_attribute *attr,
2791 const char *buf, size_t count)
2797 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2798 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2801 if (mutex_lock_killable(&hotkey_mutex))
2802 return -ERESTARTSYS;
2804 HOTKEY_CONFIG_CRITICAL_START
2805 hotkey_source_mask = t;
2806 HOTKEY_CONFIG_CRITICAL_END
2808 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2809 ~hotkey_source_mask);
2810 hotkey_poll_setup(true);
2812 /* check if events needed by the driver got disabled */
2813 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2814 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2816 mutex_unlock(&hotkey_mutex);
2819 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2820 "firmware event mask!\n");
2823 printk(TPACPI_NOTICE "hotkey_source_mask: "
2824 "some important events were disabled: "
2827 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2829 return (rc < 0) ? rc : count;
2832 static struct device_attribute dev_attr_hotkey_source_mask =
2833 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2834 hotkey_source_mask_show, hotkey_source_mask_store);
2836 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2837 static ssize_t hotkey_poll_freq_show(struct device *dev,
2838 struct device_attribute *attr,
2841 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2844 static ssize_t hotkey_poll_freq_store(struct device *dev,
2845 struct device_attribute *attr,
2846 const char *buf, size_t count)
2850 if (parse_strtoul(buf, 25, &t))
2853 if (mutex_lock_killable(&hotkey_mutex))
2854 return -ERESTARTSYS;
2856 hotkey_poll_set_freq(t);
2857 hotkey_poll_setup(true);
2859 mutex_unlock(&hotkey_mutex);
2861 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2866 static struct device_attribute dev_attr_hotkey_poll_freq =
2867 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2868 hotkey_poll_freq_show, hotkey_poll_freq_store);
2870 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2872 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2873 static ssize_t hotkey_radio_sw_show(struct device *dev,
2874 struct device_attribute *attr,
2878 res = hotkey_get_wlsw();
2882 /* Opportunistic update */
2883 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2885 return snprintf(buf, PAGE_SIZE, "%d\n",
2886 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2889 static struct device_attribute dev_attr_hotkey_radio_sw =
2890 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2892 static void hotkey_radio_sw_notify_change(void)
2894 if (tp_features.hotkey_wlsw)
2895 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2899 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2900 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2901 struct device_attribute *attr,
2905 res = hotkey_get_tablet_mode(&s);
2909 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2912 static struct device_attribute dev_attr_hotkey_tablet_mode =
2913 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2915 static void hotkey_tablet_mode_notify_change(void)
2917 if (tp_features.hotkey_tablet)
2918 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2919 "hotkey_tablet_mode");
2922 /* sysfs hotkey report_mode -------------------------------------------- */
2923 static ssize_t hotkey_report_mode_show(struct device *dev,
2924 struct device_attribute *attr,
2927 return snprintf(buf, PAGE_SIZE, "%d\n",
2928 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2931 static struct device_attribute dev_attr_hotkey_report_mode =
2932 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2934 /* sysfs wakeup reason (pollable) -------------------------------------- */
2935 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2936 struct device_attribute *attr,
2939 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2942 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2943 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2945 static void hotkey_wakeup_reason_notify_change(void)
2947 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2951 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2952 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2953 struct device_attribute *attr,
2956 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2959 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2960 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2961 hotkey_wakeup_hotunplug_complete_show, NULL);
2963 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2965 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2966 "wakeup_hotunplug_complete");
2969 /* --------------------------------------------------------------------- */
2971 static struct attribute *hotkey_attributes[] __initdata = {
2972 &dev_attr_hotkey_enable.attr,
2973 &dev_attr_hotkey_bios_enabled.attr,
2974 &dev_attr_hotkey_bios_mask.attr,
2975 &dev_attr_hotkey_report_mode.attr,
2976 &dev_attr_hotkey_wakeup_reason.attr,
2977 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2978 &dev_attr_hotkey_mask.attr,
2979 &dev_attr_hotkey_all_mask.attr,
2980 &dev_attr_hotkey_recommended_mask.attr,
2981 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2982 &dev_attr_hotkey_source_mask.attr,
2983 &dev_attr_hotkey_poll_freq.attr,
2988 * Sync both the hw and sw blocking state of all switches
2990 static void tpacpi_send_radiosw_update(void)
2995 * We must sync all rfkill controllers *before* issuing any
2996 * rfkill input events, or we will race the rfkill core input
2999 * tpacpi_inputdev_send_mutex works as a syncronization point
3002 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3005 wlsw = hotkey_get_wlsw();
3007 /* Sync hw blocking state first if it is hw-blocked */
3008 if (wlsw == TPACPI_RFK_RADIO_OFF)
3009 tpacpi_rfk_update_hwblock_state(true);
3011 /* Sync sw blocking state */
3012 tpacpi_rfk_update_swstate_all();
3014 /* Sync hw blocking state last if it is hw-unblocked */
3015 if (wlsw == TPACPI_RFK_RADIO_ON)
3016 tpacpi_rfk_update_hwblock_state(false);
3018 /* Issue rfkill input event for WLSW switch */
3020 mutex_lock(&tpacpi_inputdev_send_mutex);
3022 input_report_switch(tpacpi_inputdev,
3023 SW_RFKILL_ALL, (wlsw > 0));
3024 input_sync(tpacpi_inputdev);
3026 mutex_unlock(&tpacpi_inputdev_send_mutex);
3030 * this can be unconditional, as we will poll state again
3031 * if userspace uses the notify to read data
3033 hotkey_radio_sw_notify_change();
3036 static void hotkey_exit(void)
3038 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3039 mutex_lock(&hotkey_mutex);
3040 hotkey_poll_stop_sync();
3041 mutex_unlock(&hotkey_mutex);
3044 if (hotkey_dev_attributes)
3045 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3047 kfree(hotkey_keycode_map);
3049 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3050 "restoring original HKEY status and mask\n");
3051 /* yes, there is a bitwise or below, we want the
3052 * functions to be called even if one of them fail */
3053 if (((tp_features.hotkey_mask &&
3054 hotkey_mask_set(hotkey_orig_mask)) |
3055 hotkey_status_set(false)) != 0)
3057 "failed to restore hot key mask "
3058 "to BIOS defaults\n");
3061 static void __init hotkey_unmap(const unsigned int scancode)
3063 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3064 clear_bit(hotkey_keycode_map[scancode],
3065 tpacpi_inputdev->keybit);
3066 hotkey_keycode_map[scancode] = KEY_RESERVED;
3072 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3075 #define TPACPI_HK_Q_INIMASK 0x0001
3077 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3078 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3079 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3080 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3081 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3082 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3083 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3084 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3085 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3086 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3087 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3088 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3089 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3090 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3091 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3092 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3093 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3094 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3095 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3096 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3099 static int __init hotkey_init(struct ibm_init_struct *iibm)
3101 /* Requirements for changing the default keymaps:
3103 * 1. Many of the keys are mapped to KEY_RESERVED for very
3104 * good reasons. Do not change them unless you have deep
3105 * knowledge on the IBM and Lenovo ThinkPad firmware for
3106 * the various ThinkPad models. The driver behaves
3107 * differently for KEY_RESERVED: such keys have their
3108 * hot key mask *unset* in mask_recommended, and also
3109 * in the initial hot key mask programmed into the
3110 * firmware at driver load time, which means the firm-
3111 * ware may react very differently if you change them to
3114 * 2. You must be subscribed to the linux-thinkpad and
3115 * ibm-acpi-devel mailing lists, and you should read the
3116 * list archives since 2007 if you want to change the
3117 * keymaps. This requirement exists so that you will
3118 * know the past history of problems with the thinkpad-
3119 * acpi driver keymaps, and also that you will be
3120 * listening to any bug reports;
3122 * 3. Do not send thinkpad-acpi specific patches directly to
3123 * for merging, *ever*. Send them to the linux-acpi
3124 * mailinglist for comments. Merging is to be done only
3125 * through acpi-test and the ACPI maintainer.
3127 * If the above is too much to ask, don't change the keymap.
3128 * Ask the thinkpad-acpi maintainer to do it, instead.
3130 static u16 ibm_keycode_map[] __initdata = {
3131 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3132 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3133 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3134 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3136 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3137 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3138 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3139 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3141 /* brightness: firmware always reacts to them */
3142 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3143 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3145 /* Thinklight: firmware always react to it */
3146 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3148 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3149 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3151 /* Volume: firmware always react to it and reprograms
3152 * the built-in *extra* mixer. Never map it to control
3153 * another mixer by default. */
3154 KEY_RESERVED, /* 0x14: VOLUME UP */
3155 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3156 KEY_RESERVED, /* 0x16: MUTE */
3158 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3160 /* (assignments unknown, please report if found) */
3161 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3162 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3164 static u16 lenovo_keycode_map[] __initdata = {
3165 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3166 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3167 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3168 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3170 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3171 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3172 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3173 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3175 /* These should be enabled --only-- when ACPI video
3176 * is disabled (i.e. in "vendor" mode), and are handled
3177 * in a special way by the init code */
3178 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3179 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3181 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3183 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3184 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3186 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3187 * react to it and reprograms the built-in *extra* mixer.
3188 * Never map it to control another mixer by default.
3190 * T60?, T61, R60?, R61: firmware and EC tries to send
3191 * these over the regular keyboard, so these are no-ops,
3192 * but there are still weird bugs re. MUTE, so do not
3193 * change unless you get test reports from all Lenovo
3194 * models. May cause the BIOS to interfere with the
3197 KEY_RESERVED, /* 0x14: VOLUME UP */
3198 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3199 KEY_RESERVED, /* 0x16: MUTE */
3201 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3203 /* (assignments unknown, please report if found) */
3204 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3205 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3208 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3209 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3210 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3215 bool radiosw_state = false;
3216 bool tabletsw_state = false;
3218 unsigned long quirks;
3220 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3221 "initializing hotkey subdriver\n");
3223 BUG_ON(!tpacpi_inputdev);
3224 BUG_ON(tpacpi_inputdev->open != NULL ||
3225 tpacpi_inputdev->close != NULL);
3227 TPACPI_ACPIHANDLE_INIT(hkey);
3228 mutex_init(&hotkey_mutex);
3230 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3231 mutex_init(&hotkey_thread_mutex);
3232 mutex_init(&hotkey_thread_data_mutex);
3235 /* hotkey not supported on 570 */
3236 tp_features.hotkey = hkey_handle != NULL;
3238 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3240 str_supported(tp_features.hotkey));
3242 if (!tp_features.hotkey)
3245 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3246 ARRAY_SIZE(tpacpi_hotkey_qtable));
3248 tpacpi_disable_brightness_delay();
3250 /* MUST have enough space for all attributes to be added to
3251 * hotkey_dev_attributes */
3252 hotkey_dev_attributes = create_attr_set(
3253 ARRAY_SIZE(hotkey_attributes) + 2,
3255 if (!hotkey_dev_attributes)
3257 res = add_many_to_attr_set(hotkey_dev_attributes,
3259 ARRAY_SIZE(hotkey_attributes));
3263 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3264 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3265 for HKEY interface version 0x100 */
3266 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3267 if ((hkeyv >> 8) != 1) {
3268 printk(TPACPI_ERR "unknown version of the "
3269 "HKEY interface: 0x%x\n", hkeyv);
3270 printk(TPACPI_ERR "please report this to %s\n",
3274 * MHKV 0x100 in A31, R40, R40e,
3275 * T4x, X31, and later
3277 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3278 "firmware HKEY interface version: 0x%x\n",
3281 /* Paranoia check AND init hotkey_all_mask */
3282 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3285 "missing MHKA handler, "
3286 "please report this to %s\n",
3288 /* Fallback: pre-init for FN+F3,F4,F12 */
3289 hotkey_all_mask = 0x080cU;
3291 tp_features.hotkey_mask = 1;
3296 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3297 "hotkey masks are %s\n",
3298 str_supported(tp_features.hotkey_mask));
3300 /* Init hotkey_all_mask if not initialized yet */
3301 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3302 (quirks & TPACPI_HK_Q_INIMASK))
3303 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3305 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3306 if (tp_features.hotkey_mask) {
3307 /* hotkey_source_mask *must* be zero for
3308 * the first hotkey_mask_get to return hotkey_orig_mask */
3309 res = hotkey_mask_get();
3313 hotkey_orig_mask = hotkey_acpi_mask;
3315 hotkey_orig_mask = hotkey_all_mask;
3316 hotkey_acpi_mask = hotkey_all_mask;
3319 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3321 tp_features.hotkey_wlsw = 1;
3322 radiosw_state = !!tpacpi_wlsw_emulstate;
3324 "radio switch emulation enabled\n");
3327 /* Not all thinkpads have a hardware radio switch */
3328 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3329 tp_features.hotkey_wlsw = 1;
3330 radiosw_state = !!status;
3332 "radio switch found; radios are %s\n",
3333 enabled(status, 0));
3335 if (tp_features.hotkey_wlsw)
3336 res = add_to_attr_set(hotkey_dev_attributes,
3337 &dev_attr_hotkey_radio_sw.attr);
3339 /* For X41t, X60t, X61t Tablets... */
3340 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3341 tp_features.hotkey_tablet = 1;
3342 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3344 "possible tablet mode switch found; "
3345 "ThinkPad in %s mode\n",
3346 (tabletsw_state) ? "tablet" : "laptop");
3347 res = add_to_attr_set(hotkey_dev_attributes,
3348 &dev_attr_hotkey_tablet_mode.attr);
3352 res = register_attr_set_with_sysfs(
3353 hotkey_dev_attributes,
3354 &tpacpi_pdev->dev.kobj);
3358 /* Set up key map */
3360 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3362 if (!hotkey_keycode_map) {
3364 "failed to allocate memory for key map\n");
3369 if (tpacpi_is_lenovo()) {
3370 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3371 "using Lenovo default hot key map\n");
3372 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3373 TPACPI_HOTKEY_MAP_SIZE);
3375 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3376 "using IBM default hot key map\n");
3377 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3378 TPACPI_HOTKEY_MAP_SIZE);
3381 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3382 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3383 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3384 tpacpi_inputdev->keycode = hotkey_keycode_map;
3385 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3386 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3387 input_set_capability(tpacpi_inputdev, EV_KEY,
3388 hotkey_keycode_map[i]);
3390 if (i < sizeof(hotkey_reserved_mask)*8)
3391 hotkey_reserved_mask |= 1 << i;
3395 if (tp_features.hotkey_wlsw) {
3396 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3397 input_report_switch(tpacpi_inputdev,
3398 SW_RFKILL_ALL, radiosw_state);
3400 if (tp_features.hotkey_tablet) {
3401 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3402 input_report_switch(tpacpi_inputdev,
3403 SW_TABLET_MODE, tabletsw_state);
3406 /* Do not issue duplicate brightness change events to
3408 if (!tp_features.bright_acpimode)
3409 /* update bright_acpimode... */
3410 tpacpi_check_std_acpi_brightness_support();
3412 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3414 "This ThinkPad has standard ACPI backlight "
3415 "brightness control, supported by the ACPI "
3417 printk(TPACPI_NOTICE
3418 "Disabling thinkpad-acpi brightness events "
3421 /* Disable brightness up/down on Lenovo thinkpads when
3422 * ACPI is handling them, otherwise it is plain impossible
3423 * for userspace to do something even remotely sane */
3424 hotkey_reserved_mask |=
3425 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3426 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3427 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3428 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3431 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3432 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3434 & ~hotkey_reserved_mask;
3436 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3437 "hotkey source mask 0x%08x, polling freq %u\n",
3438 hotkey_source_mask, hotkey_poll_freq);
3441 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3442 "enabling firmware HKEY event interface...\n");
3443 res = hotkey_status_set(true);
3448 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3449 | hotkey_driver_mask)
3450 & ~hotkey_source_mask);
3451 if (res < 0 && res != -ENXIO) {
3455 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3456 & ~hotkey_reserved_mask;
3457 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3458 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3459 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3461 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3462 "legacy ibm/hotkey event reporting over procfs %s\n",
3463 (hotkey_report_mode < 2) ?
3464 "enabled" : "disabled");
3466 tpacpi_inputdev->open = &hotkey_inputdev_open;
3467 tpacpi_inputdev->close = &hotkey_inputdev_close;
3469 hotkey_poll_setup_safe(true);
3474 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3475 hotkey_dev_attributes = NULL;
3477 return (res < 0)? res : 1;
3480 static bool hotkey_notify_hotkey(const u32 hkey,
3482 bool *ignore_acpi_ev)
3484 /* 0x1000-0x1FFF: key presses */
3485 unsigned int scancode = hkey & 0xfff;
3486 *send_acpi_ev = true;
3487 *ignore_acpi_ev = false;
3489 if (scancode > 0 && scancode < 0x21) {
3491 if (!(hotkey_source_mask & (1 << scancode))) {
3492 tpacpi_input_send_key_masked(scancode);
3493 *send_acpi_ev = false;
3495 *ignore_acpi_ev = true;
3502 static bool hotkey_notify_wakeup(const u32 hkey,
3504 bool *ignore_acpi_ev)
3506 /* 0x2000-0x2FFF: Wakeup reason */
3507 *send_acpi_ev = true;
3508 *ignore_acpi_ev = false;
3511 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3512 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3513 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3514 *ignore_acpi_ev = true;
3517 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3518 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3519 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3520 *ignore_acpi_ev = true;
3523 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3524 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3526 "EMERGENCY WAKEUP: battery almost empty\n");
3527 /* how to auto-heal: */
3528 /* 2313: woke up from S3, go to S4/S5 */
3529 /* 2413: woke up from S4, go to S5 */
3536 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3538 "woke up due to a hot-unplug "
3540 hotkey_wakeup_reason_notify_change();
3545 static bool hotkey_notify_usrevent(const u32 hkey,
3547 bool *ignore_acpi_ev)
3549 /* 0x5000-0x5FFF: human interface helpers */
3550 *send_acpi_ev = true;
3551 *ignore_acpi_ev = false;
3554 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3555 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3558 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3559 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3560 tpacpi_input_send_tabletsw();
3561 hotkey_tablet_mode_notify_change();
3562 *send_acpi_ev = false;
3565 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3566 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3567 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3568 /* do not propagate these events */
3569 *ignore_acpi_ev = true;
3577 static void thermal_dump_all_sensors(void);
3579 static bool hotkey_notify_thermal(const u32 hkey,
3581 bool *ignore_acpi_ev)
3585 /* 0x6000-0x6FFF: thermal alarms */
3586 *send_acpi_ev = true;
3587 *ignore_acpi_ev = false;
3590 case TP_HKEY_EV_THM_TABLE_CHANGED:
3592 "EC reports that Thermal Table has changed\n");
3593 /* recommended action: do nothing, we don't have
3594 * Lenovo ATM information */
3596 case TP_HKEY_EV_ALARM_BAT_HOT:
3598 "THERMAL ALARM: battery is too hot!\n");
3599 /* recommended action: warn user through gui */
3601 case TP_HKEY_EV_ALARM_BAT_XHOT:
3603 "THERMAL EMERGENCY: battery is extremely hot!\n");
3604 /* recommended action: immediate sleep/hibernate */
3606 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3609 "a sensor reports something is too hot!\n");
3610 /* recommended action: warn user through gui, that */
3611 /* some internal component is too hot */
3613 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3615 "THERMAL EMERGENCY: "
3616 "a sensor reports something is extremely hot!\n");
3617 /* recommended action: immediate sleep/hibernate */
3621 "THERMAL ALERT: unknown thermal alarm received\n");
3625 thermal_dump_all_sensors();
3630 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3634 bool ignore_acpi_ev;
3637 if (event != 0x80) {
3639 "unknown HKEY notification event %d\n", event);
3640 /* forward it to userspace, maybe it knows how to handle it */
3641 acpi_bus_generate_netlink_event(
3642 ibm->acpi->device->pnp.device_class,
3643 dev_name(&ibm->acpi->device->dev),
3649 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3650 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3659 send_acpi_ev = true;
3660 ignore_acpi_ev = false;
3662 switch (hkey >> 12) {
3664 /* 0x1000-0x1FFF: key presses */
3665 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3669 /* 0x2000-0x2FFF: Wakeup reason */
3670 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3674 /* 0x3000-0x3FFF: bay-related wakeups */
3676 case TP_HKEY_EV_BAYEJ_ACK:
3677 hotkey_autosleep_ack = 1;
3680 hotkey_wakeup_hotunplug_complete_notify_change();
3683 case TP_HKEY_EV_OPTDRV_EJ:
3684 /* FIXME: kick libata if SATA link offline */
3692 /* 0x4000-0x4FFF: dock-related wakeups */
3693 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3694 hotkey_autosleep_ack = 1;
3697 hotkey_wakeup_hotunplug_complete_notify_change();
3704 /* 0x5000-0x5FFF: human interface helpers */
3705 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3709 /* 0x6000-0x6FFF: thermal alarms */
3710 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3714 /* 0x7000-0x7FFF: misc */
3715 if (tp_features.hotkey_wlsw &&
3716 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3717 tpacpi_send_radiosw_update();
3722 /* fallthrough to default */
3727 printk(TPACPI_NOTICE
3728 "unhandled HKEY event 0x%04x\n", hkey);
3729 printk(TPACPI_NOTICE
3730 "please report the conditions when this "
3731 "event happened to %s\n", TPACPI_MAIL);
3735 if (!ignore_acpi_ev &&
3736 (send_acpi_ev || hotkey_report_mode < 2)) {
3737 acpi_bus_generate_proc_event(ibm->acpi->device,
3741 /* netlink events */
3742 if (!ignore_acpi_ev && send_acpi_ev) {
3743 acpi_bus_generate_netlink_event(
3744 ibm->acpi->device->pnp.device_class,
3745 dev_name(&ibm->acpi->device->dev),
3751 static void hotkey_suspend(pm_message_t state)
3753 /* Do these on suspend, we get the events on early resume! */
3754 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3755 hotkey_autosleep_ack = 0;
3758 static void hotkey_resume(void)
3760 tpacpi_disable_brightness_delay();
3762 if (hotkey_status_set(true) < 0 ||
3763 hotkey_mask_set(hotkey_acpi_mask) < 0)
3765 "error while attempting to reset the event "
3766 "firmware interface\n");
3768 tpacpi_send_radiosw_update();
3769 hotkey_tablet_mode_notify_change();
3770 hotkey_wakeup_reason_notify_change();
3771 hotkey_wakeup_hotunplug_complete_notify_change();
3772 hotkey_poll_setup_safe(false);
3775 /* procfs -------------------------------------------------------------- */
3776 static int hotkey_read(struct seq_file *m)
3780 if (!tp_features.hotkey) {
3781 seq_printf(m, "status:\t\tnot supported\n");
3785 if (mutex_lock_killable(&hotkey_mutex))
3786 return -ERESTARTSYS;
3787 res = hotkey_status_get(&status);
3789 res = hotkey_mask_get();
3790 mutex_unlock(&hotkey_mutex);
3794 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3795 if (hotkey_all_mask) {
3796 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3797 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3799 seq_printf(m, "mask:\t\tnot supported\n");
3800 seq_printf(m, "commands:\tenable, disable, reset\n");
3806 static void hotkey_enabledisable_warn(bool enable)
3808 tpacpi_log_usertask("procfs hotkey enable/disable");
3809 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3811 "hotkey enable/disable functionality has been "
3812 "removed from the driver. Hotkeys are always "
3815 "Please remove the hotkey=enable module "
3816 "parameter, it is deprecated. Hotkeys are always "
3820 static int hotkey_write(char *buf)
3826 if (!tp_features.hotkey)
3829 if (mutex_lock_killable(&hotkey_mutex))
3830 return -ERESTARTSYS;
3832 mask = hotkey_user_mask;
3835 while ((cmd = next_cmd(&buf))) {
3836 if (strlencmp(cmd, "enable") == 0) {
3837 hotkey_enabledisable_warn(1);
3838 } else if (strlencmp(cmd, "disable") == 0) {
3839 hotkey_enabledisable_warn(0);
3841 } else if (strlencmp(cmd, "reset") == 0) {
3842 mask = (hotkey_all_mask | hotkey_source_mask)
3843 & ~hotkey_reserved_mask;
3844 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3846 } else if (sscanf(cmd, "%x", &mask) == 1) {
3855 tpacpi_disclose_usertask("procfs hotkey",
3856 "set mask to 0x%08x\n", mask);
3857 res = hotkey_user_mask_set(mask);
3861 mutex_unlock(&hotkey_mutex);
3865 static const struct acpi_device_id ibm_htk_device_ids[] = {
3866 {TPACPI_ACPI_HKEY_HID, 0},
3870 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3871 .hid = ibm_htk_device_ids,
3872 .notify = hotkey_notify,
3873 .handle = &hkey_handle,
3874 .type = ACPI_DEVICE_NOTIFY,
3877 static struct ibm_struct hotkey_driver_data = {
3879 .read = hotkey_read,
3880 .write = hotkey_write,
3881 .exit = hotkey_exit,
3882 .resume = hotkey_resume,
3883 .suspend = hotkey_suspend,
3884 .acpi = &ibm_hotkey_acpidriver,
3887 /*************************************************************************
3888 * Bluetooth subdriver
3892 /* ACPI GBDC/SBDC bits */
3893 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3894 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3895 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3896 0 = disable, 1 = enable */
3900 /* ACPI \BLTH commands */
3901 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3902 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3903 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3904 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3905 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3908 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3910 static int bluetooth_get_status(void)
3914 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3915 if (dbg_bluetoothemul)
3916 return (tpacpi_bluetooth_emulstate) ?
3917 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3920 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3923 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3924 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3927 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3931 vdbg_printk(TPACPI_DBG_RFKILL,
3932 "will attempt to %s bluetooth\n",
3933 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3935 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3936 if (dbg_bluetoothemul) {
3937 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3942 if (state == TPACPI_RFK_RADIO_ON)
3943 status = TP_ACPI_BLUETOOTH_RADIOSSW
3944 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3948 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3954 /* sysfs bluetooth enable ---------------------------------------------- */
3955 static ssize_t bluetooth_enable_show(struct device *dev,
3956 struct device_attribute *attr,
3959 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3963 static ssize_t bluetooth_enable_store(struct device *dev,
3964 struct device_attribute *attr,
3965 const char *buf, size_t count)
3967 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3971 static struct device_attribute dev_attr_bluetooth_enable =
3972 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3973 bluetooth_enable_show, bluetooth_enable_store);
3975 /* --------------------------------------------------------------------- */
3977 static struct attribute *bluetooth_attributes[] = {
3978 &dev_attr_bluetooth_enable.attr,
3982 static const struct attribute_group bluetooth_attr_group = {
3983 .attrs = bluetooth_attributes,
3986 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3987 .get_status = bluetooth_get_status,
3988 .set_status = bluetooth_set_status,
3991 static void bluetooth_shutdown(void)
3993 /* Order firmware to save current state to NVRAM */
3994 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3995 TP_ACPI_BLTH_SAVE_STATE))
3996 printk(TPACPI_NOTICE
3997 "failed to save bluetooth state to NVRAM\n");
3999 vdbg_printk(TPACPI_DBG_RFKILL,
4000 "bluestooth state saved to NVRAM\n");
4003 static void bluetooth_exit(void)
4005 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4006 &bluetooth_attr_group);
4008 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4010 bluetooth_shutdown();
4013 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4018 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4019 "initializing bluetooth subdriver\n");
4021 TPACPI_ACPIHANDLE_INIT(hkey);
4023 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4024 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4025 tp_features.bluetooth = hkey_handle &&
4026 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4028 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4029 "bluetooth is %s, status 0x%02x\n",
4030 str_supported(tp_features.bluetooth),
4033 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4034 if (dbg_bluetoothemul) {
4035 tp_features.bluetooth = 1;
4037 "bluetooth switch emulation enabled\n");
4040 if (tp_features.bluetooth &&
4041 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4042 /* no bluetooth hardware present in system */
4043 tp_features.bluetooth = 0;
4044 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4045 "bluetooth hardware not installed\n");
4048 if (!tp_features.bluetooth)
4051 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4052 &bluetooth_tprfk_ops,
4053 RFKILL_TYPE_BLUETOOTH,
4054 TPACPI_RFK_BLUETOOTH_SW_NAME,
4059 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4060 &bluetooth_attr_group);
4062 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4069 /* procfs -------------------------------------------------------------- */
4070 static int bluetooth_read(struct seq_file *m)
4072 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4075 static int bluetooth_write(char *buf)
4077 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4080 static struct ibm_struct bluetooth_driver_data = {
4081 .name = "bluetooth",
4082 .read = bluetooth_read,
4083 .write = bluetooth_write,
4084 .exit = bluetooth_exit,
4085 .shutdown = bluetooth_shutdown,
4088 /*************************************************************************
4093 /* ACPI GWAN/SWAN bits */
4094 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4095 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4096 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4097 0 = disable, 1 = enable */
4100 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4102 static int wan_get_status(void)
4106 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4108 return (tpacpi_wwan_emulstate) ?
4109 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4112 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4115 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4116 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4119 static int wan_set_status(enum tpacpi_rfkill_state state)
4123 vdbg_printk(TPACPI_DBG_RFKILL,
4124 "will attempt to %s wwan\n",
4125 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4127 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4129 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4134 if (state == TPACPI_RFK_RADIO_ON)
4135 status = TP_ACPI_WANCARD_RADIOSSW
4136 | TP_ACPI_WANCARD_RESUMECTRL;
4140 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4146 /* sysfs wan enable ---------------------------------------------------- */
4147 static ssize_t wan_enable_show(struct device *dev,
4148 struct device_attribute *attr,
4151 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4155 static ssize_t wan_enable_store(struct device *dev,
4156 struct device_attribute *attr,
4157 const char *buf, size_t count)
4159 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4163 static struct device_attribute dev_attr_wan_enable =
4164 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4165 wan_enable_show, wan_enable_store);
4167 /* --------------------------------------------------------------------- */
4169 static struct attribute *wan_attributes[] = {
4170 &dev_attr_wan_enable.attr,
4174 static const struct attribute_group wan_attr_group = {
4175 .attrs = wan_attributes,
4178 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4179 .get_status = wan_get_status,
4180 .set_status = wan_set_status,
4183 static void wan_shutdown(void)
4185 /* Order firmware to save current state to NVRAM */
4186 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4187 TP_ACPI_WGSV_SAVE_STATE))
4188 printk(TPACPI_NOTICE
4189 "failed to save WWAN state to NVRAM\n");
4191 vdbg_printk(TPACPI_DBG_RFKILL,
4192 "WWAN state saved to NVRAM\n");
4195 static void wan_exit(void)
4197 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4200 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4205 static int __init wan_init(struct ibm_init_struct *iibm)
4210 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4211 "initializing wan subdriver\n");
4213 TPACPI_ACPIHANDLE_INIT(hkey);
4215 tp_features.wan = hkey_handle &&
4216 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4218 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4219 "wan is %s, status 0x%02x\n",
4220 str_supported(tp_features.wan),
4223 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4225 tp_features.wan = 1;
4227 "wwan switch emulation enabled\n");
4230 if (tp_features.wan &&
4231 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4232 /* no wan hardware present in system */
4233 tp_features.wan = 0;
4234 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4235 "wan hardware not installed\n");
4238 if (!tp_features.wan)
4241 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4244 TPACPI_RFK_WWAN_SW_NAME,
4249 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4253 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4260 /* procfs -------------------------------------------------------------- */
4261 static int wan_read(struct seq_file *m)
4263 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4266 static int wan_write(char *buf)
4268 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4271 static struct ibm_struct wan_driver_data = {
4276 .shutdown = wan_shutdown,
4279 /*************************************************************************
4284 /* ACPI GUWB/SUWB bits */
4285 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4286 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4289 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4291 static int uwb_get_status(void)
4295 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4297 return (tpacpi_uwb_emulstate) ?
4298 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4301 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4304 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4305 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4308 static int uwb_set_status(enum tpacpi_rfkill_state state)
4312 vdbg_printk(TPACPI_DBG_RFKILL,
4313 "will attempt to %s UWB\n",
4314 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4316 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4318 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4323 if (state == TPACPI_RFK_RADIO_ON)
4324 status = TP_ACPI_UWB_RADIOSSW;
4328 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4334 /* --------------------------------------------------------------------- */
4336 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4337 .get_status = uwb_get_status,
4338 .set_status = uwb_set_status,
4341 static void uwb_exit(void)
4343 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4346 static int __init uwb_init(struct ibm_init_struct *iibm)
4351 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4352 "initializing uwb subdriver\n");
4354 TPACPI_ACPIHANDLE_INIT(hkey);
4356 tp_features.uwb = hkey_handle &&
4357 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4359 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4360 "uwb is %s, status 0x%02x\n",
4361 str_supported(tp_features.uwb),
4364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4366 tp_features.uwb = 1;
4368 "uwb switch emulation enabled\n");
4371 if (tp_features.uwb &&
4372 !(status & TP_ACPI_UWB_HWPRESENT)) {
4373 /* no uwb hardware present in system */
4374 tp_features.uwb = 0;
4375 dbg_printk(TPACPI_DBG_INIT,
4376 "uwb hardware not installed\n");
4379 if (!tp_features.uwb)
4382 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4385 TPACPI_RFK_UWB_SW_NAME,
4390 static struct ibm_struct uwb_driver_data = {
4393 .flags.experimental = 1,
4396 /*************************************************************************
4400 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4402 enum video_access_mode {
4403 TPACPI_VIDEO_NONE = 0,
4404 TPACPI_VIDEO_570, /* 570 */
4405 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4406 TPACPI_VIDEO_NEW, /* all others */
4409 enum { /* video status flags, based on VIDEO_570 */
4410 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4411 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4412 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4415 enum { /* TPACPI_VIDEO_570 constants */
4416 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4417 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4418 * video_status_flags */
4419 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4420 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4423 static enum video_access_mode video_supported;
4424 static int video_orig_autosw;
4426 static int video_autosw_get(void);
4427 static int video_autosw_set(int enable);
4429 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4431 static int __init video_init(struct ibm_init_struct *iibm)
4435 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4437 TPACPI_ACPIHANDLE_INIT(vid);
4438 if (tpacpi_is_ibm())
4439 TPACPI_ACPIHANDLE_INIT(vid2);
4441 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4442 /* G41, assume IVGA doesn't change */
4443 vid_handle = vid2_handle;
4446 /* video switching not supported on R30, R31 */
4447 video_supported = TPACPI_VIDEO_NONE;
4448 else if (tpacpi_is_ibm() &&
4449 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4451 video_supported = TPACPI_VIDEO_570;
4452 else if (tpacpi_is_ibm() &&
4453 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4454 /* 600e/x, 770e, 770x */
4455 video_supported = TPACPI_VIDEO_770;
4458 video_supported = TPACPI_VIDEO_NEW;
4460 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4461 str_supported(video_supported != TPACPI_VIDEO_NONE),
4464 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4467 static void video_exit(void)
4469 dbg_printk(TPACPI_DBG_EXIT,
4470 "restoring original video autoswitch mode\n");
4471 if (video_autosw_set(video_orig_autosw))
4472 printk(TPACPI_ERR "error while trying to restore original "
4473 "video autoswitch mode\n");
4476 static int video_outputsw_get(void)
4481 switch (video_supported) {
4482 case TPACPI_VIDEO_570:
4483 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4484 TP_ACPI_VIDEO_570_PHSCMD))
4486 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4488 case TPACPI_VIDEO_770:
4489 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4492 status |= TP_ACPI_VIDEO_S_LCD;
4493 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4496 status |= TP_ACPI_VIDEO_S_CRT;
4498 case TPACPI_VIDEO_NEW:
4499 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4500 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4503 status |= TP_ACPI_VIDEO_S_CRT;
4505 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4506 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4509 status |= TP_ACPI_VIDEO_S_LCD;
4510 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4513 status |= TP_ACPI_VIDEO_S_DVI;
4522 static int video_outputsw_set(int status)
4527 switch (video_supported) {
4528 case TPACPI_VIDEO_570:
4529 res = acpi_evalf(NULL, NULL,
4530 "\\_SB.PHS2", "vdd",
4531 TP_ACPI_VIDEO_570_PHS2CMD,
4532 status | TP_ACPI_VIDEO_570_PHS2SET);
4534 case TPACPI_VIDEO_770:
4535 autosw = video_autosw_get();
4539 res = video_autosw_set(1);
4542 res = acpi_evalf(vid_handle, NULL,
4543 "ASWT", "vdd", status * 0x100, 0);
4544 if (!autosw && video_autosw_set(autosw)) {
4546 "video auto-switch left enabled due to error\n");
4550 case TPACPI_VIDEO_NEW:
4551 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4552 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4558 return (res)? 0 : -EIO;
4561 static int video_autosw_get(void)
4565 switch (video_supported) {
4566 case TPACPI_VIDEO_570:
4567 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4570 case TPACPI_VIDEO_770:
4571 case TPACPI_VIDEO_NEW:
4572 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4582 static int video_autosw_set(int enable)
4584 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4589 static int video_outputsw_cycle(void)
4591 int autosw = video_autosw_get();
4597 switch (video_supported) {
4598 case TPACPI_VIDEO_570:
4599 res = video_autosw_set(1);
4602 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4604 case TPACPI_VIDEO_770:
4605 case TPACPI_VIDEO_NEW:
4606 res = video_autosw_set(1);
4609 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4614 if (!autosw && video_autosw_set(autosw)) {
4616 "video auto-switch left enabled due to error\n");
4620 return (res)? 0 : -EIO;
4623 static int video_expand_toggle(void)
4625 switch (video_supported) {
4626 case TPACPI_VIDEO_570:
4627 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4629 case TPACPI_VIDEO_770:
4630 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4632 case TPACPI_VIDEO_NEW:
4633 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4641 static int video_read(struct seq_file *m)
4645 if (video_supported == TPACPI_VIDEO_NONE) {
4646 seq_printf(m, "status:\t\tnot supported\n");
4650 /* Even reads can crash X.org, so... */
4651 if (!capable(CAP_SYS_ADMIN))
4654 status = video_outputsw_get();
4658 autosw = video_autosw_get();
4662 seq_printf(m, "status:\t\tsupported\n");
4663 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4664 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4665 if (video_supported == TPACPI_VIDEO_NEW)
4666 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4667 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4668 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4669 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4670 if (video_supported == TPACPI_VIDEO_NEW)
4671 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4672 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4673 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4678 static int video_write(char *buf)
4681 int enable, disable, status;
4684 if (video_supported == TPACPI_VIDEO_NONE)
4687 /* Even reads can crash X.org, let alone writes... */
4688 if (!capable(CAP_SYS_ADMIN))
4694 while ((cmd = next_cmd(&buf))) {
4695 if (strlencmp(cmd, "lcd_enable") == 0) {
4696 enable |= TP_ACPI_VIDEO_S_LCD;
4697 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4698 disable |= TP_ACPI_VIDEO_S_LCD;
4699 } else if (strlencmp(cmd, "crt_enable") == 0) {
4700 enable |= TP_ACPI_VIDEO_S_CRT;
4701 } else if (strlencmp(cmd, "crt_disable") == 0) {
4702 disable |= TP_ACPI_VIDEO_S_CRT;
4703 } else if (video_supported == TPACPI_VIDEO_NEW &&
4704 strlencmp(cmd, "dvi_enable") == 0) {
4705 enable |= TP_ACPI_VIDEO_S_DVI;
4706 } else if (video_supported == TPACPI_VIDEO_NEW &&
4707 strlencmp(cmd, "dvi_disable") == 0) {
4708 disable |= TP_ACPI_VIDEO_S_DVI;
4709 } else if (strlencmp(cmd, "auto_enable") == 0) {
4710 res = video_autosw_set(1);
4713 } else if (strlencmp(cmd, "auto_disable") == 0) {
4714 res = video_autosw_set(0);
4717 } else if (strlencmp(cmd, "video_switch") == 0) {
4718 res = video_outputsw_cycle();
4721 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4722 res = video_expand_toggle();
4729 if (enable || disable) {
4730 status = video_outputsw_get();
4733 res = video_outputsw_set((status & ~disable) | enable);
4741 static struct ibm_struct video_driver_data = {
4744 .write = video_write,
4748 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4750 /*************************************************************************
4751 * Light (thinklight) subdriver
4754 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4755 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4757 static int light_get_status(void)
4761 if (tp_features.light_status) {
4762 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4770 static int light_set_status(int status)
4774 if (tp_features.light) {
4776 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4778 TP_CMOS_THINKLIGHT_ON :
4779 TP_CMOS_THINKLIGHT_OFF);
4781 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4784 return (rc)? 0 : -EIO;
4790 static void light_set_status_worker(struct work_struct *work)
4792 struct tpacpi_led_classdev *data =
4793 container_of(work, struct tpacpi_led_classdev, work);
4795 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4796 light_set_status((data->new_state != TPACPI_LED_OFF));
4799 static void light_sysfs_set(struct led_classdev *led_cdev,
4800 enum led_brightness brightness)
4802 struct tpacpi_led_classdev *data =
4803 container_of(led_cdev,
4804 struct tpacpi_led_classdev,
4806 data->new_state = (brightness != LED_OFF) ?
4807 TPACPI_LED_ON : TPACPI_LED_OFF;
4808 queue_work(tpacpi_wq, &data->work);
4811 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4813 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4816 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4818 .name = "tpacpi::thinklight",
4819 .brightness_set = &light_sysfs_set,
4820 .brightness_get = &light_sysfs_get,
4824 static int __init light_init(struct ibm_init_struct *iibm)
4828 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4830 if (tpacpi_is_ibm()) {
4831 TPACPI_ACPIHANDLE_INIT(ledb);
4832 TPACPI_ACPIHANDLE_INIT(lght);
4834 TPACPI_ACPIHANDLE_INIT(cmos);
4835 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4837 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4838 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4840 if (tp_features.light)
4841 /* light status not supported on
4842 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4843 tp_features.light_status =
4844 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4846 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4847 str_supported(tp_features.light),
4848 str_supported(tp_features.light_status));
4850 if (!tp_features.light)
4853 rc = led_classdev_register(&tpacpi_pdev->dev,
4854 &tpacpi_led_thinklight.led_classdev);
4857 tp_features.light = 0;
4858 tp_features.light_status = 0;
4866 static void light_exit(void)
4868 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4869 if (work_pending(&tpacpi_led_thinklight.work))
4870 flush_workqueue(tpacpi_wq);
4873 static int light_read(struct seq_file *m)
4877 if (!tp_features.light) {
4878 seq_printf(m, "status:\t\tnot supported\n");
4879 } else if (!tp_features.light_status) {
4880 seq_printf(m, "status:\t\tunknown\n");
4881 seq_printf(m, "commands:\ton, off\n");
4883 status = light_get_status();
4886 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4887 seq_printf(m, "commands:\ton, off\n");
4893 static int light_write(char *buf)
4898 if (!tp_features.light)
4901 while ((cmd = next_cmd(&buf))) {
4902 if (strlencmp(cmd, "on") == 0) {
4904 } else if (strlencmp(cmd, "off") == 0) {
4910 return light_set_status(newstatus);
4913 static struct ibm_struct light_driver_data = {
4916 .write = light_write,
4920 /*************************************************************************
4924 /* sysfs cmos_command -------------------------------------------------- */
4925 static ssize_t cmos_command_store(struct device *dev,
4926 struct device_attribute *attr,
4927 const char *buf, size_t count)
4929 unsigned long cmos_cmd;
4932 if (parse_strtoul(buf, 21, &cmos_cmd))
4935 res = issue_thinkpad_cmos_command(cmos_cmd);
4936 return (res)? res : count;
4939 static struct device_attribute dev_attr_cmos_command =
4940 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4942 /* --------------------------------------------------------------------- */
4944 static int __init cmos_init(struct ibm_init_struct *iibm)
4948 vdbg_printk(TPACPI_DBG_INIT,
4949 "initializing cmos commands subdriver\n");
4951 TPACPI_ACPIHANDLE_INIT(cmos);
4953 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4954 str_supported(cmos_handle != NULL));
4956 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4960 return (cmos_handle)? 0 : 1;
4963 static void cmos_exit(void)
4965 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4968 static int cmos_read(struct seq_file *m)
4970 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4971 R30, R31, T20-22, X20-21 */
4973 seq_printf(m, "status:\t\tnot supported\n");
4975 seq_printf(m, "status:\t\tsupported\n");
4976 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4982 static int cmos_write(char *buf)
4987 while ((cmd = next_cmd(&buf))) {
4988 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4989 cmos_cmd >= 0 && cmos_cmd <= 21) {
4994 res = issue_thinkpad_cmos_command(cmos_cmd);
5002 static struct ibm_struct cmos_driver_data = {
5005 .write = cmos_write,
5009 /*************************************************************************
5013 enum led_access_mode {
5014 TPACPI_LED_NONE = 0,
5015 TPACPI_LED_570, /* 570 */
5016 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5017 TPACPI_LED_NEW, /* all others */
5020 enum { /* For TPACPI_LED_OLD */
5021 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5022 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5023 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5026 static enum led_access_mode led_supported;
5028 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5029 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5030 /* T20-22, X20-21 */
5031 "LED", /* all others */
5034 #define TPACPI_LED_NUMLEDS 16
5035 static struct tpacpi_led_classdev *tpacpi_leds;
5036 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5037 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5038 /* there's a limit of 19 chars + NULL before 2.6.26 */
5040 "tpacpi:orange:batt",
5041 "tpacpi:green:batt",
5042 "tpacpi::dock_active",
5043 "tpacpi::bay_active",
5044 "tpacpi::dock_batt",
5045 "tpacpi::unknown_led",
5047 "tpacpi::dock_status1",
5048 "tpacpi::dock_status2",
5049 "tpacpi::unknown_led2",
5050 "tpacpi::unknown_led3",
5051 "tpacpi::thinkvantage",
5053 #define TPACPI_SAFE_LEDS 0x1081U
5055 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5057 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5060 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5064 static int led_get_status(const unsigned int led)
5067 enum led_status_t led_s;
5069 switch (led_supported) {
5070 case TPACPI_LED_570:
5071 if (!acpi_evalf(ec_handle,
5072 &status, "GLED", "dd", 1 << led))
5074 led_s = (status == 0)?
5079 tpacpi_led_state_cache[led] = led_s;
5088 static int led_set_status(const unsigned int led,
5089 const enum led_status_t ledstatus)
5091 /* off, on, blink. Index is led_status_t */
5092 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5093 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5097 switch (led_supported) {
5098 case TPACPI_LED_570:
5100 if (unlikely(led > 7))
5102 if (unlikely(tpacpi_is_led_restricted(led)))
5104 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5105 (1 << led), led_sled_arg1[ledstatus]))
5108 case TPACPI_LED_OLD:
5109 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5110 if (unlikely(led > 7))
5112 if (unlikely(tpacpi_is_led_restricted(led)))
5114 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5116 rc = ec_write(TPACPI_LED_EC_HLBL,
5117 (ledstatus == TPACPI_LED_BLINK) << led);
5119 rc = ec_write(TPACPI_LED_EC_HLCL,
5120 (ledstatus != TPACPI_LED_OFF) << led);
5122 case TPACPI_LED_NEW:
5124 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5126 if (unlikely(tpacpi_is_led_restricted(led)))
5128 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5129 led, led_led_arg1[ledstatus]))
5137 tpacpi_led_state_cache[led] = ledstatus;
5142 static void led_set_status_worker(struct work_struct *work)
5144 struct tpacpi_led_classdev *data =
5145 container_of(work, struct tpacpi_led_classdev, work);
5147 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5148 led_set_status(data->led, data->new_state);
5151 static void led_sysfs_set(struct led_classdev *led_cdev,
5152 enum led_brightness brightness)
5154 struct tpacpi_led_classdev *data = container_of(led_cdev,
5155 struct tpacpi_led_classdev, led_classdev);
5157 if (brightness == LED_OFF)
5158 data->new_state = TPACPI_LED_OFF;
5159 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5160 data->new_state = TPACPI_LED_ON;
5162 data->new_state = TPACPI_LED_BLINK;
5164 queue_work(tpacpi_wq, &data->work);
5167 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5168 unsigned long *delay_on, unsigned long *delay_off)
5170 struct tpacpi_led_classdev *data = container_of(led_cdev,
5171 struct tpacpi_led_classdev, led_classdev);
5173 /* Can we choose the flash rate? */
5174 if (*delay_on == 0 && *delay_off == 0) {
5175 /* yes. set them to the hardware blink rate (1 Hz) */
5176 *delay_on = 500; /* ms */
5177 *delay_off = 500; /* ms */
5178 } else if ((*delay_on != 500) || (*delay_off != 500))
5181 data->new_state = TPACPI_LED_BLINK;
5182 queue_work(tpacpi_wq, &data->work);
5187 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5191 struct tpacpi_led_classdev *data = container_of(led_cdev,
5192 struct tpacpi_led_classdev, led_classdev);
5194 rc = led_get_status(data->led);
5196 if (rc == TPACPI_LED_OFF || rc < 0)
5197 rc = LED_OFF; /* no error handling in led class :( */
5204 static void led_exit(void)
5208 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5209 if (tpacpi_leds[i].led_classdev.name)
5210 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5216 static int __init tpacpi_init_led(unsigned int led)
5220 tpacpi_leds[led].led = led;
5222 /* LEDs with no name don't get registered */
5223 if (!tpacpi_led_names[led])
5226 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5227 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5228 if (led_supported == TPACPI_LED_570)
5229 tpacpi_leds[led].led_classdev.brightness_get =
5232 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5234 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5236 rc = led_classdev_register(&tpacpi_pdev->dev,
5237 &tpacpi_leds[led].led_classdev);
5239 tpacpi_leds[led].led_classdev.name = NULL;
5244 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5245 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5246 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5247 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5249 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5250 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5251 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5252 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5253 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5254 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5255 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5256 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5258 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5259 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5260 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5261 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5262 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5264 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5265 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5266 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5267 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5269 /* (1) - may have excess leds enabled on MSB */
5271 /* Defaults (order matters, keep last, don't reorder!) */
5273 .vendor = PCI_VENDOR_ID_LENOVO,
5274 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5277 { /* IBM ThinkPads with no EC version string */
5278 .vendor = PCI_VENDOR_ID_IBM,
5279 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5282 { /* IBM ThinkPads with EC version string */
5283 .vendor = PCI_VENDOR_ID_IBM,
5284 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5289 #undef TPACPI_LEDQ_IBM
5290 #undef TPACPI_LEDQ_LNV
5292 static int __init led_init(struct ibm_init_struct *iibm)
5296 unsigned long useful_leds;
5298 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5300 TPACPI_ACPIHANDLE_INIT(led);
5303 /* led not supported on R30, R31 */
5304 led_supported = TPACPI_LED_NONE;
5305 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5307 led_supported = TPACPI_LED_570;
5308 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5309 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5310 led_supported = TPACPI_LED_OLD;
5313 led_supported = TPACPI_LED_NEW;
5315 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5316 str_supported(led_supported), led_supported);
5318 if (led_supported == TPACPI_LED_NONE)
5321 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5324 printk(TPACPI_ERR "Out of memory for LED data\n");
5328 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5329 ARRAY_SIZE(led_useful_qtable));
5331 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5332 if (!tpacpi_is_led_restricted(i) &&
5333 test_bit(i, &useful_leds)) {
5334 rc = tpacpi_init_led(i);
5342 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5343 printk(TPACPI_NOTICE
5344 "warning: userspace override of important "
5345 "firmware LEDs is enabled\n");
5350 #define str_led_status(s) \
5351 ((s) == TPACPI_LED_OFF ? "off" : \
5352 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5354 static int led_read(struct seq_file *m)
5356 if (!led_supported) {
5357 seq_printf(m, "status:\t\tnot supported\n");
5360 seq_printf(m, "status:\t\tsupported\n");
5362 if (led_supported == TPACPI_LED_570) {
5365 for (i = 0; i < 8; i++) {
5366 status = led_get_status(i);
5369 seq_printf(m, "%d:\t\t%s\n",
5370 i, str_led_status(status));
5374 seq_printf(m, "commands:\t"
5375 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5380 static int led_write(char *buf)
5384 enum led_status_t s;
5389 while ((cmd = next_cmd(&buf))) {
5390 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5393 if (strstr(cmd, "off")) {
5395 } else if (strstr(cmd, "on")) {
5397 } else if (strstr(cmd, "blink")) {
5398 s = TPACPI_LED_BLINK;
5403 rc = led_set_status(led, s);
5411 static struct ibm_struct led_driver_data = {
5418 /*************************************************************************
5422 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5424 #define TPACPI_BEEP_Q1 0x0001
5426 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5427 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5428 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5431 static int __init beep_init(struct ibm_init_struct *iibm)
5433 unsigned long quirks;
5435 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5437 TPACPI_ACPIHANDLE_INIT(beep);
5439 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5440 str_supported(beep_handle != NULL));
5442 quirks = tpacpi_check_quirks(beep_quirk_table,
5443 ARRAY_SIZE(beep_quirk_table));
5445 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5447 return (beep_handle)? 0 : 1;
5450 static int beep_read(struct seq_file *m)
5453 seq_printf(m, "status:\t\tnot supported\n");
5455 seq_printf(m, "status:\t\tsupported\n");
5456 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5462 static int beep_write(char *buf)
5470 while ((cmd = next_cmd(&buf))) {
5471 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5472 beep_cmd >= 0 && beep_cmd <= 17) {
5476 if (tp_features.beep_needs_two_args) {
5477 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5481 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5490 static struct ibm_struct beep_driver_data = {
5493 .write = beep_write,
5496 /*************************************************************************
5500 enum thermal_access_mode {
5501 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5502 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5503 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5504 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5505 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5508 enum { /* TPACPI_THERMAL_TPEC_* */
5509 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5510 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5511 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5513 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5517 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5518 struct ibm_thermal_sensors_struct {
5519 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5522 static enum thermal_access_mode thermal_read_mode;
5524 /* idx is zero-based */
5525 static int thermal_get_sensor(int idx, s32 *value)
5531 t = TP_EC_THERMAL_TMP0;
5533 switch (thermal_read_mode) {
5534 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5535 case TPACPI_THERMAL_TPEC_16:
5536 if (idx >= 8 && idx <= 15) {
5537 t = TP_EC_THERMAL_TMP8;
5542 case TPACPI_THERMAL_TPEC_8:
5544 if (!acpi_ec_read(t + idx, &tmp))
5546 *value = tmp * 1000;
5551 case TPACPI_THERMAL_ACPI_UPDT:
5553 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5554 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5556 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5558 *value = (t - 2732) * 100;
5563 case TPACPI_THERMAL_ACPI_TMP07:
5565 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5566 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5568 if (t > 127 || t < -127)
5569 t = TP_EC_THERMAL_TMP_NA;
5575 case TPACPI_THERMAL_NONE:
5583 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5594 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5597 for (i = 0 ; i < n; i++) {
5598 res = thermal_get_sensor(i, &s->temp[i]);
5606 static void thermal_dump_all_sensors(void)
5609 struct ibm_thermal_sensors_struct t;
5611 n = thermal_get_sensors(&t);
5615 printk(TPACPI_NOTICE
5616 "temperatures (Celsius):");
5618 for (i = 0; i < n; i++) {
5619 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5620 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5622 printk(KERN_CONT " N/A");
5625 printk(KERN_CONT "\n");
5628 /* sysfs temp##_input -------------------------------------------------- */
5630 static ssize_t thermal_temp_input_show(struct device *dev,
5631 struct device_attribute *attr,
5634 struct sensor_device_attribute *sensor_attr =
5635 to_sensor_dev_attr(attr);
5636 int idx = sensor_attr->index;
5640 res = thermal_get_sensor(idx, &value);
5643 if (value == TPACPI_THERMAL_SENSOR_NA)
5646 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5649 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5650 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5651 thermal_temp_input_show, NULL, _idxB)
5653 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5654 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5655 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5656 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5657 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5658 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5659 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5660 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5661 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5662 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5663 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5664 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5665 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5666 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5667 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5668 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5669 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5672 #define THERMAL_ATTRS(X) \
5673 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5675 static struct attribute *thermal_temp_input_attr[] = {
5695 static const struct attribute_group thermal_temp_input16_group = {
5696 .attrs = thermal_temp_input_attr
5699 static const struct attribute_group thermal_temp_input8_group = {
5700 .attrs = &thermal_temp_input_attr[8]
5703 #undef THERMAL_SENSOR_ATTR_TEMP
5704 #undef THERMAL_ATTRS
5706 /* --------------------------------------------------------------------- */
5708 static int __init thermal_init(struct ibm_init_struct *iibm)
5715 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5717 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5719 if (thinkpad_id.ec_model) {
5721 * Direct EC access mode: sensors at registers
5722 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5723 * non-implemented, thermal sensors return 0x80 when
5728 for (i = 0; i < 8; i++) {
5729 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5735 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5743 /* This is sheer paranoia, but we handle it anyway */
5746 "ThinkPad ACPI EC access misbehaving, "
5747 "falling back to ACPI TMPx access "
5749 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5752 "ThinkPad ACPI EC access misbehaving, "
5753 "disabling thermal sensors access\n");
5754 thermal_read_mode = TPACPI_THERMAL_NONE;
5759 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5761 } else if (acpi_tmp7) {
5762 if (tpacpi_is_ibm() &&
5763 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5764 /* 600e/x, 770e, 770x */
5765 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5767 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5768 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5771 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5772 thermal_read_mode = TPACPI_THERMAL_NONE;
5775 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5776 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5779 switch (thermal_read_mode) {
5780 case TPACPI_THERMAL_TPEC_16:
5781 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5782 &thermal_temp_input16_group);
5786 case TPACPI_THERMAL_TPEC_8:
5787 case TPACPI_THERMAL_ACPI_TMP07:
5788 case TPACPI_THERMAL_ACPI_UPDT:
5789 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5790 &thermal_temp_input8_group);
5794 case TPACPI_THERMAL_NONE:
5802 static void thermal_exit(void)
5804 switch (thermal_read_mode) {
5805 case TPACPI_THERMAL_TPEC_16:
5806 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5807 &thermal_temp_input16_group);
5809 case TPACPI_THERMAL_TPEC_8:
5810 case TPACPI_THERMAL_ACPI_TMP07:
5811 case TPACPI_THERMAL_ACPI_UPDT:
5812 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5813 &thermal_temp_input8_group);
5815 case TPACPI_THERMAL_NONE:
5821 static int thermal_read(struct seq_file *m)
5824 struct ibm_thermal_sensors_struct t;
5826 n = thermal_get_sensors(&t);
5827 if (unlikely(n < 0))
5830 seq_printf(m, "temperatures:\t");
5833 for (i = 0; i < (n - 1); i++)
5834 seq_printf(m, "%d ", t.temp[i] / 1000);
5835 seq_printf(m, "%d\n", t.temp[i] / 1000);
5837 seq_printf(m, "not supported\n");
5842 static struct ibm_struct thermal_driver_data = {
5844 .read = thermal_read,
5845 .exit = thermal_exit,
5848 /*************************************************************************
5852 static u8 ecdump_regs[256];
5854 static int ecdump_read(struct seq_file *m)
5860 " +00 +01 +02 +03 +04 +05 +06 +07"
5861 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5862 for (i = 0; i < 256; i += 16) {
5863 seq_printf(m, "EC 0x%02x:", i);
5864 for (j = 0; j < 16; j++) {
5865 if (!acpi_ec_read(i + j, &v))
5867 if (v != ecdump_regs[i + j])
5868 seq_printf(m, " *%02x", v);
5870 seq_printf(m, " %02x", v);
5871 ecdump_regs[i + j] = v;
5878 /* These are way too dangerous to advertise openly... */
5880 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5881 " (<offset> is 00-ff, <value> is 00-ff)\n");
5882 seq_printf(m, "commands:\t0x<offset> <value> "
5883 " (<offset> is 00-ff, <value> is 0-255)\n");
5888 static int ecdump_write(char *buf)
5893 while ((cmd = next_cmd(&buf))) {
5894 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5896 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5900 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5901 if (!acpi_ec_write(i, v))
5910 static struct ibm_struct ecdump_driver_data = {
5912 .read = ecdump_read,
5913 .write = ecdump_write,
5914 .flags.experimental = 1,
5917 /*************************************************************************
5918 * Backlight/brightness subdriver
5921 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5924 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5925 * CMOS NVRAM byte 0x5E, bits 0-3.
5927 * EC HBRV (0x31) has the following layout
5928 * Bit 7: unknown function
5929 * Bit 6: unknown function
5930 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5931 * Bit 4: must be set to zero to avoid problems
5932 * Bit 3-0: backlight brightness level
5934 * brightness_get_raw returns status data in the HBRV layout
5936 * WARNING: The X61 has been verified to use HBRV for something else, so
5937 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5938 * testing on the very early *60 Lenovo models...
5942 TP_EC_BACKLIGHT = 0x31,
5944 /* TP_EC_BACKLIGHT bitmasks */
5945 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5946 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5947 TP_EC_BACKLIGHT_MAPSW = 0x20,
5950 enum tpacpi_brightness_access_mode {
5951 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5952 TPACPI_BRGHT_MODE_EC, /* EC control */
5953 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5954 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5955 TPACPI_BRGHT_MODE_MAX
5958 static struct backlight_device *ibm_backlight_device;
5960 static enum tpacpi_brightness_access_mode brightness_mode =
5961 TPACPI_BRGHT_MODE_MAX;
5963 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5965 static struct mutex brightness_mutex;
5967 /* NVRAM brightness access,
5968 * call with brightness_mutex held! */
5969 static unsigned int tpacpi_brightness_nvram_get(void)
5973 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5974 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5975 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5976 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5981 static void tpacpi_brightness_checkpoint_nvram(void)
5986 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5989 vdbg_printk(TPACPI_DBG_BRGHT,
5990 "trying to checkpoint backlight level to NVRAM...\n");
5992 if (mutex_lock_killable(&brightness_mutex) < 0)
5995 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5997 lec &= TP_EC_BACKLIGHT_LVLMSK;
5998 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6000 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6001 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6002 /* NVRAM needs update */
6003 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6004 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6006 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6007 dbg_printk(TPACPI_DBG_BRGHT,
6008 "updated NVRAM backlight level to %u (0x%02x)\n",
6009 (unsigned int) lec, (unsigned int) b_nvram);
6011 vdbg_printk(TPACPI_DBG_BRGHT,
6012 "NVRAM backlight level already is %u (0x%02x)\n",
6013 (unsigned int) lec, (unsigned int) b_nvram);
6016 mutex_unlock(&brightness_mutex);
6020 /* call with brightness_mutex held! */
6021 static int tpacpi_brightness_get_raw(int *status)
6025 switch (brightness_mode) {
6026 case TPACPI_BRGHT_MODE_UCMS_STEP:
6027 *status = tpacpi_brightness_nvram_get();
6029 case TPACPI_BRGHT_MODE_EC:
6030 case TPACPI_BRGHT_MODE_ECNVRAM:
6031 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6040 /* call with brightness_mutex held! */
6041 /* do NOT call with illegal backlight level value */
6042 static int tpacpi_brightness_set_ec(unsigned int value)
6046 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6049 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6050 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6051 (value & TP_EC_BACKLIGHT_LVLMSK))))
6057 /* call with brightness_mutex held! */
6058 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6061 unsigned int current_value, i;
6063 current_value = tpacpi_brightness_nvram_get();
6065 if (value == current_value)
6068 cmos_cmd = (value > current_value) ?
6069 TP_CMOS_BRIGHTNESS_UP :
6070 TP_CMOS_BRIGHTNESS_DOWN;
6071 inc = (value > current_value) ? 1 : -1;
6073 for (i = current_value; i != value; i += inc)
6074 if (issue_thinkpad_cmos_command(cmos_cmd))
6080 /* May return EINTR which can always be mapped to ERESTARTSYS */
6081 static int brightness_set(unsigned int value)
6085 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6089 vdbg_printk(TPACPI_DBG_BRGHT,
6090 "set backlight level to %d\n", value);
6092 res = mutex_lock_killable(&brightness_mutex);
6096 switch (brightness_mode) {
6097 case TPACPI_BRGHT_MODE_EC:
6098 case TPACPI_BRGHT_MODE_ECNVRAM:
6099 res = tpacpi_brightness_set_ec(value);
6101 case TPACPI_BRGHT_MODE_UCMS_STEP:
6102 res = tpacpi_brightness_set_ucmsstep(value);
6108 mutex_unlock(&brightness_mutex);
6112 /* sysfs backlight class ----------------------------------------------- */
6114 static int brightness_update_status(struct backlight_device *bd)
6116 unsigned int level =
6117 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6118 bd->props.power == FB_BLANK_UNBLANK) ?
6119 bd->props.brightness : 0;
6121 dbg_printk(TPACPI_DBG_BRGHT,
6122 "backlight: attempt to set level to %d\n",
6125 /* it is the backlight class's job (caller) to handle
6126 * EINTR and other errors properly */
6127 return brightness_set(level);
6130 static int brightness_get(struct backlight_device *bd)
6134 res = mutex_lock_killable(&brightness_mutex);
6138 res = tpacpi_brightness_get_raw(&status);
6140 mutex_unlock(&brightness_mutex);
6145 return status & TP_EC_BACKLIGHT_LVLMSK;
6148 static void tpacpi_brightness_notify_change(void)
6150 backlight_force_update(ibm_backlight_device,
6151 BACKLIGHT_UPDATE_HOTKEY);
6154 static struct backlight_ops ibm_backlight_data = {
6155 .get_brightness = brightness_get,
6156 .update_status = brightness_update_status,
6159 /* --------------------------------------------------------------------- */
6162 * These are only useful for models that have only one possibility
6163 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6166 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6167 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6168 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6170 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6171 /* Models with ATI GPUs known to require ECNVRAM mode */
6172 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6174 /* Models with ATI GPUs that can use ECNVRAM */
6175 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6176 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6177 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6178 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6180 /* Models with Intel Extreme Graphics 2 */
6181 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6182 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6183 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6185 /* Models with Intel GMA900 */
6186 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6187 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6188 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6191 static int __init brightness_init(struct ibm_init_struct *iibm)
6193 struct backlight_properties props;
6195 unsigned long quirks;
6197 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6199 mutex_init(&brightness_mutex);
6201 quirks = tpacpi_check_quirks(brightness_quirk_table,
6202 ARRAY_SIZE(brightness_quirk_table));
6205 * We always attempt to detect acpi support, so as to switch
6206 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6207 * going to publish a backlight interface
6209 b = tpacpi_check_std_acpi_brightness_support();
6212 if (acpi_video_backlight_support()) {
6213 if (brightness_enable > 1) {
6214 printk(TPACPI_NOTICE
6215 "Standard ACPI backlight interface "
6216 "available, not loading native one.\n");
6218 } else if (brightness_enable == 1) {
6219 printk(TPACPI_NOTICE
6220 "Backlight control force enabled, even if standard "
6221 "ACPI backlight interface is available\n");
6224 if (brightness_enable > 1) {
6225 printk(TPACPI_NOTICE
6226 "Standard ACPI backlight interface not "
6227 "available, thinkpad_acpi native "
6228 "brightness control enabled\n");
6233 if (!brightness_enable) {
6234 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6235 "brightness support disabled by "
6236 "module parameter\n");
6242 "Unsupported brightness interface, "
6243 "please contact %s\n", TPACPI_MAIL);
6247 tp_features.bright_16levels = 1;
6250 * Check for module parameter bogosity, note that we
6251 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6252 * able to detect "unspecified"
6254 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6257 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6258 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6259 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6260 if (quirks & TPACPI_BRGHT_Q_EC)
6261 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6263 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6265 dbg_printk(TPACPI_DBG_BRGHT,
6266 "driver auto-selected brightness_mode=%d\n",
6271 if (!tpacpi_is_ibm() &&
6272 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6273 brightness_mode == TPACPI_BRGHT_MODE_EC))
6276 if (tpacpi_brightness_get_raw(&b) < 0)
6279 if (tp_features.bright_16levels)
6281 "detected a 16-level brightness capable ThinkPad\n");
6283 memset(&props, 0, sizeof(struct backlight_properties));
6284 props.max_brightness = (tp_features.bright_16levels) ? 15 : 7;
6285 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6287 &ibm_backlight_data,
6289 if (IS_ERR(ibm_backlight_device)) {
6290 int rc = PTR_ERR(ibm_backlight_device);
6291 ibm_backlight_device = NULL;
6292 printk(TPACPI_ERR "Could not register backlight device\n");
6295 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6296 "brightness is supported\n");
6298 if (quirks & TPACPI_BRGHT_Q_ASK) {
6299 printk(TPACPI_NOTICE
6300 "brightness: will use unverified default: "
6301 "brightness_mode=%d\n", brightness_mode);
6302 printk(TPACPI_NOTICE
6303 "brightness: please report to %s whether it works well "
6304 "or not on your ThinkPad\n", TPACPI_MAIL);
6307 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6308 backlight_update_status(ibm_backlight_device);
6310 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6311 "brightness: registering brightness hotkeys "
6312 "as change notification\n");
6313 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6314 | TP_ACPI_HKEY_BRGHTUP_MASK
6315 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6319 static void brightness_suspend(pm_message_t state)
6321 tpacpi_brightness_checkpoint_nvram();
6324 static void brightness_shutdown(void)
6326 tpacpi_brightness_checkpoint_nvram();
6329 static void brightness_exit(void)
6331 if (ibm_backlight_device) {
6332 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6333 "calling backlight_device_unregister()\n");
6334 backlight_device_unregister(ibm_backlight_device);
6337 tpacpi_brightness_checkpoint_nvram();
6340 static int brightness_read(struct seq_file *m)
6344 level = brightness_get(NULL);
6346 seq_printf(m, "level:\t\tunreadable\n");
6348 seq_printf(m, "level:\t\t%d\n", level);
6349 seq_printf(m, "commands:\tup, down\n");
6350 seq_printf(m, "commands:\tlevel <level>"
6351 " (<level> is 0-%d)\n",
6352 (tp_features.bright_16levels) ? 15 : 7);
6358 static int brightness_write(char *buf)
6363 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6365 level = brightness_get(NULL);
6369 while ((cmd = next_cmd(&buf))) {
6370 if (strlencmp(cmd, "up") == 0) {
6371 if (level < max_level)
6373 } else if (strlencmp(cmd, "down") == 0) {
6376 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6377 level >= 0 && level <= max_level) {
6383 tpacpi_disclose_usertask("procfs brightness",
6384 "set level to %d\n", level);
6387 * Now we know what the final level should be, so we try to set it.
6388 * Doing it this way makes the syscall restartable in case of EINTR
6390 rc = brightness_set(level);
6391 if (!rc && ibm_backlight_device)
6392 backlight_force_update(ibm_backlight_device,
6393 BACKLIGHT_UPDATE_SYSFS);
6394 return (rc == -EINTR)? -ERESTARTSYS : rc;
6397 static struct ibm_struct brightness_driver_data = {
6398 .name = "brightness",
6399 .read = brightness_read,
6400 .write = brightness_write,
6401 .exit = brightness_exit,
6402 .suspend = brightness_suspend,
6403 .shutdown = brightness_shutdown,
6406 /*************************************************************************
6411 * IBM ThinkPads have a simple volume controller with MUTE gating.
6412 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6414 * Since the *61 series (and probably also the later *60 series), Lenovo
6415 * ThinkPads only implement the MUTE gate.
6418 * Bit 6: MUTE (1 mutes sound)
6420 * Other bits should be zero as far as we know.
6422 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6423 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6424 * such as bit 7 which is used to detect repeated presses of MUTE,
6425 * and we leave them unchanged.
6428 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6430 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6431 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6432 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6434 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6435 static char *alsa_id = "ThinkPadEC";
6436 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6438 struct tpacpi_alsa_data {
6439 struct snd_card *card;
6440 struct snd_ctl_elem_id *ctl_mute_id;
6441 struct snd_ctl_elem_id *ctl_vol_id;
6444 static struct snd_card *alsa_card;
6449 /* TP_EC_AUDIO bits */
6450 TP_EC_AUDIO_MUTESW = 6,
6452 /* TP_EC_AUDIO bitmasks */
6453 TP_EC_AUDIO_LVL_MSK = 0x0F,
6454 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6456 /* Maximum volume */
6457 TP_EC_VOLUME_MAX = 14,
6460 enum tpacpi_volume_access_mode {
6461 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6462 TPACPI_VOL_MODE_EC, /* Pure EC control */
6463 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6464 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6468 enum tpacpi_volume_capabilities {
6469 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6470 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6471 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6475 static enum tpacpi_volume_access_mode volume_mode =
6476 TPACPI_VOL_MODE_MAX;
6478 static enum tpacpi_volume_capabilities volume_capabilities;
6479 static int volume_control_allowed;
6482 * Used to syncronize writers to TP_EC_AUDIO and
6483 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6485 static struct mutex volume_mutex;
6487 static void tpacpi_volume_checkpoint_nvram(void)
6493 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6495 if (!volume_control_allowed)
6498 vdbg_printk(TPACPI_DBG_MIXER,
6499 "trying to checkpoint mixer state to NVRAM...\n");
6501 if (tp_features.mixer_no_level_control)
6502 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6504 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6506 if (mutex_lock_killable(&volume_mutex) < 0)
6509 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6512 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6514 if (lec != (b_nvram & ec_mask)) {
6515 /* NVRAM needs update */
6516 b_nvram &= ~ec_mask;
6518 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6519 dbg_printk(TPACPI_DBG_MIXER,
6520 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6521 (unsigned int) lec, (unsigned int) b_nvram);
6523 vdbg_printk(TPACPI_DBG_MIXER,
6524 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6525 (unsigned int) lec, (unsigned int) b_nvram);
6529 mutex_unlock(&volume_mutex);
6532 static int volume_get_status_ec(u8 *status)
6536 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6541 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6546 static int volume_get_status(u8 *status)
6548 return volume_get_status_ec(status);
6551 static int volume_set_status_ec(const u8 status)
6553 if (!acpi_ec_write(TP_EC_AUDIO, status))
6556 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6561 static int volume_set_status(const u8 status)
6563 return volume_set_status_ec(status);
6566 /* returns < 0 on error, 0 on no change, 1 on change */
6567 static int __volume_set_mute_ec(const bool mute)
6572 if (mutex_lock_killable(&volume_mutex) < 0)
6575 rc = volume_get_status_ec(&s);
6579 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6580 s & ~TP_EC_AUDIO_MUTESW_MSK;
6583 rc = volume_set_status_ec(n);
6589 mutex_unlock(&volume_mutex);
6593 static int volume_alsa_set_mute(const bool mute)
6595 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6596 (mute) ? "" : "un");
6597 return __volume_set_mute_ec(mute);
6600 static int volume_set_mute(const bool mute)
6604 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6605 (mute) ? "" : "un");
6607 rc = __volume_set_mute_ec(mute);
6608 return (rc < 0) ? rc : 0;
6611 /* returns < 0 on error, 0 on no change, 1 on change */
6612 static int __volume_set_volume_ec(const u8 vol)
6617 if (vol > TP_EC_VOLUME_MAX)
6620 if (mutex_lock_killable(&volume_mutex) < 0)
6623 rc = volume_get_status_ec(&s);
6627 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6630 rc = volume_set_status_ec(n);
6636 mutex_unlock(&volume_mutex);
6640 static int volume_alsa_set_volume(const u8 vol)
6642 dbg_printk(TPACPI_DBG_MIXER,
6643 "ALSA: trying to set volume level to %hu\n", vol);
6644 return __volume_set_volume_ec(vol);
6647 static void volume_alsa_notify_change(void)
6649 struct tpacpi_alsa_data *d;
6651 if (alsa_card && alsa_card->private_data) {
6652 d = alsa_card->private_data;
6654 snd_ctl_notify(alsa_card,
6655 SNDRV_CTL_EVENT_MASK_VALUE,
6658 snd_ctl_notify(alsa_card,
6659 SNDRV_CTL_EVENT_MASK_VALUE,
6664 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6665 struct snd_ctl_elem_info *uinfo)
6667 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6669 uinfo->value.integer.min = 0;
6670 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6674 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6675 struct snd_ctl_elem_value *ucontrol)
6680 rc = volume_get_status(&s);
6684 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6688 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6689 struct snd_ctl_elem_value *ucontrol)
6691 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6694 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6696 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6697 struct snd_ctl_elem_value *ucontrol)
6702 rc = volume_get_status(&s);
6706 ucontrol->value.integer.value[0] =
6707 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6711 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6712 struct snd_ctl_elem_value *ucontrol)
6714 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6717 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6718 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6719 .name = "Console Playback Volume",
6721 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6722 .info = volume_alsa_vol_info,
6723 .get = volume_alsa_vol_get,
6726 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6728 .name = "Console Playback Switch",
6730 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6731 .info = volume_alsa_mute_info,
6732 .get = volume_alsa_mute_get,
6735 static void volume_suspend(pm_message_t state)
6737 tpacpi_volume_checkpoint_nvram();
6740 static void volume_resume(void)
6742 volume_alsa_notify_change();
6745 static void volume_shutdown(void)
6747 tpacpi_volume_checkpoint_nvram();
6750 static void volume_exit(void)
6753 snd_card_free(alsa_card);
6757 tpacpi_volume_checkpoint_nvram();
6760 static int __init volume_create_alsa_mixer(void)
6762 struct snd_card *card;
6763 struct tpacpi_alsa_data *data;
6764 struct snd_kcontrol *ctl_vol;
6765 struct snd_kcontrol *ctl_mute;
6768 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6769 sizeof(struct tpacpi_alsa_data), &card);
6770 if (rc < 0 || !card) {
6772 "Failed to create ALSA card structures: %d\n", rc);
6776 BUG_ON(!card->private_data);
6777 data = card->private_data;
6780 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6781 sizeof(card->driver));
6782 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6783 sizeof(card->shortname));
6784 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6785 (thinkpad_id.ec_version_str) ?
6786 thinkpad_id.ec_version_str : "(unknown)");
6787 snprintf(card->longname, sizeof(card->longname),
6788 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6789 (thinkpad_id.ec_version_str) ?
6790 thinkpad_id.ec_version_str : "unknown");
6792 if (volume_control_allowed) {
6793 volume_alsa_control_vol.put = volume_alsa_vol_put;
6794 volume_alsa_control_vol.access =
6795 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6797 volume_alsa_control_mute.put = volume_alsa_mute_put;
6798 volume_alsa_control_mute.access =
6799 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6802 if (!tp_features.mixer_no_level_control) {
6803 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6804 rc = snd_ctl_add(card, ctl_vol);
6807 "Failed to create ALSA volume control: %d\n",
6811 data->ctl_vol_id = &ctl_vol->id;
6814 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6815 rc = snd_ctl_add(card, ctl_mute);
6817 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6821 data->ctl_mute_id = &ctl_mute->id;
6823 snd_card_set_dev(card, &tpacpi_pdev->dev);
6824 rc = snd_card_register(card);
6826 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6834 snd_card_free(card);
6838 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6839 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6841 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6842 /* Whitelist volume level on all IBM by default */
6843 { .vendor = PCI_VENDOR_ID_IBM,
6844 .bios = TPACPI_MATCH_ANY,
6845 .ec = TPACPI_MATCH_ANY,
6846 .quirks = TPACPI_VOL_Q_LEVEL },
6848 /* Lenovo models with volume control (needs confirmation) */
6849 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6850 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6851 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6852 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6853 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6854 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6855 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6857 /* Whitelist mute-only on all Lenovo by default */
6858 { .vendor = PCI_VENDOR_ID_LENOVO,
6859 .bios = TPACPI_MATCH_ANY,
6860 .ec = TPACPI_MATCH_ANY,
6861 .quirks = TPACPI_VOL_Q_MUTEONLY }
6864 static int __init volume_init(struct ibm_init_struct *iibm)
6866 unsigned long quirks;
6869 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6871 mutex_init(&volume_mutex);
6874 * Check for module parameter bogosity, note that we
6875 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6876 * able to detect "unspecified"
6878 if (volume_mode > TPACPI_VOL_MODE_MAX)
6881 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6883 "UCMS step volume mode not implemented, "
6884 "please contact %s\n", TPACPI_MAIL);
6888 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6892 * The ALSA mixer is our primary interface.
6893 * When disabled, don't install the subdriver at all
6896 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6897 "ALSA mixer disabled by parameter, "
6898 "not loading volume subdriver...\n");
6902 quirks = tpacpi_check_quirks(volume_quirk_table,
6903 ARRAY_SIZE(volume_quirk_table));
6905 switch (volume_capabilities) {
6906 case TPACPI_VOL_CAP_AUTO:
6907 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6908 tp_features.mixer_no_level_control = 1;
6909 else if (quirks & TPACPI_VOL_Q_LEVEL)
6910 tp_features.mixer_no_level_control = 0;
6912 return 1; /* no mixer */
6914 case TPACPI_VOL_CAP_VOLMUTE:
6915 tp_features.mixer_no_level_control = 0;
6917 case TPACPI_VOL_CAP_MUTEONLY:
6918 tp_features.mixer_no_level_control = 1;
6924 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6925 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6926 "using user-supplied volume_capabilities=%d\n",
6927 volume_capabilities);
6929 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6930 volume_mode == TPACPI_VOL_MODE_MAX) {
6931 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6933 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6934 "driver auto-selected volume_mode=%d\n",
6937 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6938 "using user-supplied volume_mode=%d\n",
6942 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6943 "mute is supported, volume control is %s\n",
6944 str_supported(!tp_features.mixer_no_level_control));
6946 rc = volume_create_alsa_mixer();
6949 "Could not create the ALSA mixer interface\n");
6954 "Console audio control enabled, mode: %s\n",
6955 (volume_control_allowed) ?
6956 "override (read/write)" :
6957 "monitor (read only)");
6959 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6960 "registering volume hotkeys as change notification\n");
6961 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6962 | TP_ACPI_HKEY_VOLUP_MASK
6963 | TP_ACPI_HKEY_VOLDWN_MASK
6964 | TP_ACPI_HKEY_MUTE_MASK);
6969 static int volume_read(struct seq_file *m)
6973 if (volume_get_status(&status) < 0) {
6974 seq_printf(m, "level:\t\tunreadable\n");
6976 if (tp_features.mixer_no_level_control)
6977 seq_printf(m, "level:\t\tunsupported\n");
6979 seq_printf(m, "level:\t\t%d\n",
6980 status & TP_EC_AUDIO_LVL_MSK);
6982 seq_printf(m, "mute:\t\t%s\n",
6983 onoff(status, TP_EC_AUDIO_MUTESW));
6985 if (volume_control_allowed) {
6986 seq_printf(m, "commands:\tunmute, mute\n");
6987 if (!tp_features.mixer_no_level_control) {
6989 "commands:\tup, down\n");
6991 "commands:\tlevel <level>"
6992 " (<level> is 0-%d)\n",
7001 static int volume_write(char *buf)
7004 u8 new_level, new_mute;
7010 * We do allow volume control at driver startup, so that the
7011 * user can set initial state through the volume=... parameter hack.
7013 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7014 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7015 tp_warned.volume_ctrl_forbidden = 1;
7016 printk(TPACPI_NOTICE
7017 "Console audio control in monitor mode, "
7018 "changes are not allowed.\n");
7019 printk(TPACPI_NOTICE
7020 "Use the volume_control=1 module parameter "
7021 "to enable volume control\n");
7026 rc = volume_get_status(&s);
7030 new_level = s & TP_EC_AUDIO_LVL_MSK;
7031 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7033 while ((cmd = next_cmd(&buf))) {
7034 if (!tp_features.mixer_no_level_control) {
7035 if (strlencmp(cmd, "up") == 0) {
7038 else if (new_level < TP_EC_VOLUME_MAX)
7041 } else if (strlencmp(cmd, "down") == 0) {
7044 else if (new_level > 0)
7047 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7048 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7053 if (strlencmp(cmd, "mute") == 0)
7054 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7055 else if (strlencmp(cmd, "unmute") == 0)
7061 if (tp_features.mixer_no_level_control) {
7062 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7063 new_mute ? "" : "un");
7064 rc = volume_set_mute(!!new_mute);
7066 tpacpi_disclose_usertask("procfs volume",
7067 "%smute and set level to %d\n",
7068 new_mute ? "" : "un", new_level);
7069 rc = volume_set_status(new_mute | new_level);
7071 volume_alsa_notify_change();
7073 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7076 static struct ibm_struct volume_driver_data = {
7078 .read = volume_read,
7079 .write = volume_write,
7080 .exit = volume_exit,
7081 .suspend = volume_suspend,
7082 .resume = volume_resume,
7083 .shutdown = volume_shutdown,
7086 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7088 #define alsa_card NULL
7090 static void inline volume_alsa_notify_change(void)
7094 static int __init volume_init(struct ibm_init_struct *iibm)
7097 "volume: disabled as there is no ALSA support in this kernel\n");
7102 static struct ibm_struct volume_driver_data = {
7106 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7108 /*************************************************************************
7115 * TPACPI_FAN_RD_ACPI_GFAN:
7116 * ACPI GFAN method: returns fan level
7118 * see TPACPI_FAN_WR_ACPI_SFAN
7119 * EC 0x2f (HFSP) not available if GFAN exists
7121 * TPACPI_FAN_WR_ACPI_SFAN:
7122 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7124 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7127 * TPACPI_FAN_WR_TPEC:
7128 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7129 * Supported on almost all ThinkPads
7131 * Fan speed changes of any sort (including those caused by the
7132 * disengaged mode) are usually done slowly by the firmware as the
7133 * maximum amount of fan duty cycle change per second seems to be
7136 * Reading is not available if GFAN exists.
7137 * Writing is not available if SFAN exists.
7140 * 7 automatic mode engaged;
7141 * (default operation mode of the ThinkPad)
7142 * fan level is ignored in this mode.
7143 * 6 full speed mode (takes precedence over bit 7);
7144 * not available on all thinkpads. May disable
7145 * the tachometer while the fan controller ramps up
7146 * the speed (which can take up to a few *minutes*).
7147 * Speeds up fan to 100% duty-cycle, which is far above
7148 * the standard RPM levels. It is not impossible that
7149 * it could cause hardware damage.
7150 * 5-3 unused in some models. Extra bits for fan level
7151 * in others, but still useless as all values above
7152 * 7 map to the same speed as level 7 in these models.
7153 * 2-0 fan level (0..7 usually)
7155 * 0x07 = max (set when temperatures critical)
7156 * Some ThinkPads may have other levels, see
7157 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7159 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7160 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7161 * does so, its initial value is meaningless (0x07).
7163 * For firmware bugs, refer to:
7164 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7168 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7169 * Main fan tachometer reading (in RPM)
7171 * This register is present on all ThinkPads with a new-style EC, and
7172 * it is known not to be present on the A21m/e, and T22, as there is
7173 * something else in offset 0x84 according to the ACPI DSDT. Other
7174 * ThinkPads from this same time period (and earlier) probably lack the
7175 * tachometer as well.
7177 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7178 * was never fixed by IBM to report the EC firmware version string
7179 * probably support the tachometer (like the early X models), so
7180 * detecting it is quite hard. We need more data to know for sure.
7182 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7185 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7188 * For firmware bugs, refer to:
7189 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7193 * ThinkPad EC register 0x31 bit 0 (only on select models)
7195 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7196 * show the speed of the main fan. When bit 0 of EC register 0x31
7197 * is one, the tachometer registers show the speed of the auxiliary
7200 * Fan control seems to affect both fans, regardless of the state
7203 * So far, only the firmware for the X60/X61 non-tablet versions
7204 * seem to support this (firmware TP-7M).
7206 * TPACPI_FAN_WR_ACPI_FANS:
7207 * ThinkPad X31, X40, X41. Not available in the X60.
7209 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7210 * high speed. ACPI DSDT seems to map these three speeds to levels
7211 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7212 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7214 * The speeds are stored on handles
7215 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7217 * There are three default speed sets, accessible as handles:
7218 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7220 * ACPI DSDT switches which set is in use depending on various
7223 * TPACPI_FAN_WR_TPEC is also available and should be used to
7224 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7225 * but the ACPI tables just mention level 7.
7228 enum { /* Fan control constants */
7229 fan_status_offset = 0x2f, /* EC register 0x2f */
7230 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7231 * 0x84 must be read before 0x85 */
7232 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7233 bit 0 selects which fan is active */
7235 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7236 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7238 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7241 enum fan_status_access_mode {
7242 TPACPI_FAN_NONE = 0, /* No fan status or control */
7243 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7244 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7247 enum fan_control_access_mode {
7248 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7249 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7250 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7251 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7254 enum fan_control_commands {
7255 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7256 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7257 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7258 * and also watchdog cmd */
7261 static int fan_control_allowed;
7263 static enum fan_status_access_mode fan_status_access_mode;
7264 static enum fan_control_access_mode fan_control_access_mode;
7265 static enum fan_control_commands fan_control_commands;
7267 static u8 fan_control_initial_status;
7268 static u8 fan_control_desired_level;
7269 static u8 fan_control_resume_level;
7270 static int fan_watchdog_maxinterval;
7272 static struct mutex fan_mutex;
7274 static void fan_watchdog_fire(struct work_struct *ignored);
7275 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7277 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7278 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7279 "\\FSPD", /* 600e/x, 770e, 770x */
7281 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7282 "JFNS", /* 770x-JL */
7286 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7287 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7288 * be in auto mode (0x80).
7290 * This is corrected by any write to HFSP either by the driver, or
7293 * We assume 0x07 really means auto mode while this quirk is active,
7294 * as this is far more likely than the ThinkPad being in level 7,
7295 * which is only used by the firmware during thermal emergencies.
7297 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7298 * TP-70 (T43, R52), which are known to be buggy.
7301 static void fan_quirk1_setup(void)
7303 if (fan_control_initial_status == 0x07) {
7304 printk(TPACPI_NOTICE
7305 "fan_init: initial fan status is unknown, "
7306 "assuming it is in auto mode\n");
7307 tp_features.fan_ctrl_status_undef = 1;
7311 static void fan_quirk1_handle(u8 *fan_status)
7313 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7314 if (*fan_status != fan_control_initial_status) {
7315 /* something changed the HFSP regisnter since
7316 * driver init time, so it is not undefined
7318 tp_features.fan_ctrl_status_undef = 0;
7320 /* Return most likely status. In fact, it
7321 * might be the only possible status */
7322 *fan_status = TP_EC_FAN_AUTO;
7327 /* Select main fan on X60/X61, NOOP on others */
7328 static bool fan_select_fan1(void)
7330 if (tp_features.second_fan) {
7333 if (ec_read(fan_select_offset, &val) < 0)
7336 if (ec_write(fan_select_offset, val) < 0)
7342 /* Select secondary fan on X60/X61 */
7343 static bool fan_select_fan2(void)
7347 if (!tp_features.second_fan)
7350 if (ec_read(fan_select_offset, &val) < 0)
7353 if (ec_write(fan_select_offset, val) < 0)
7360 * Call with fan_mutex held
7362 static void fan_update_desired_level(u8 status)
7365 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7367 fan_control_desired_level = 7;
7369 fan_control_desired_level = status;
7373 static int fan_get_status(u8 *status)
7378 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7380 switch (fan_status_access_mode) {
7381 case TPACPI_FAN_RD_ACPI_GFAN:
7382 /* 570, 600e/x, 770e, 770x */
7384 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7392 case TPACPI_FAN_RD_TPEC:
7393 /* all except 570, 600e/x, 770e, 770x */
7394 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7397 if (likely(status)) {
7399 fan_quirk1_handle(status);
7411 static int fan_get_status_safe(u8 *status)
7416 if (mutex_lock_killable(&fan_mutex))
7417 return -ERESTARTSYS;
7418 rc = fan_get_status(&s);
7420 fan_update_desired_level(s);
7421 mutex_unlock(&fan_mutex);
7429 static int fan_get_speed(unsigned int *speed)
7433 switch (fan_status_access_mode) {
7434 case TPACPI_FAN_RD_TPEC:
7435 /* all except 570, 600e/x, 770e, 770x */
7436 if (unlikely(!fan_select_fan1()))
7438 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7439 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7443 *speed = (hi << 8) | lo;
7454 static int fan2_get_speed(unsigned int *speed)
7459 switch (fan_status_access_mode) {
7460 case TPACPI_FAN_RD_TPEC:
7461 /* all except 570, 600e/x, 770e, 770x */
7462 if (unlikely(!fan_select_fan2()))
7464 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7465 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7466 fan_select_fan1(); /* play it safe */
7471 *speed = (hi << 8) | lo;
7482 static int fan_set_level(int level)
7484 if (!fan_control_allowed)
7487 switch (fan_control_access_mode) {
7488 case TPACPI_FAN_WR_ACPI_SFAN:
7489 if (level >= 0 && level <= 7) {
7490 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7496 case TPACPI_FAN_WR_ACPI_FANS:
7497 case TPACPI_FAN_WR_TPEC:
7498 if (!(level & TP_EC_FAN_AUTO) &&
7499 !(level & TP_EC_FAN_FULLSPEED) &&
7500 ((level < 0) || (level > 7)))
7503 /* safety net should the EC not support AUTO
7504 * or FULLSPEED mode bits and just ignore them */
7505 if (level & TP_EC_FAN_FULLSPEED)
7506 level |= 7; /* safety min speed 7 */
7507 else if (level & TP_EC_FAN_AUTO)
7508 level |= 4; /* safety min speed 4 */
7510 if (!acpi_ec_write(fan_status_offset, level))
7513 tp_features.fan_ctrl_status_undef = 0;
7520 vdbg_printk(TPACPI_DBG_FAN,
7521 "fan control: set fan control register to 0x%02x\n", level);
7525 static int fan_set_level_safe(int level)
7529 if (!fan_control_allowed)
7532 if (mutex_lock_killable(&fan_mutex))
7533 return -ERESTARTSYS;
7535 if (level == TPACPI_FAN_LAST_LEVEL)
7536 level = fan_control_desired_level;
7538 rc = fan_set_level(level);
7540 fan_update_desired_level(level);
7542 mutex_unlock(&fan_mutex);
7546 static int fan_set_enable(void)
7551 if (!fan_control_allowed)
7554 if (mutex_lock_killable(&fan_mutex))
7555 return -ERESTARTSYS;
7557 switch (fan_control_access_mode) {
7558 case TPACPI_FAN_WR_ACPI_FANS:
7559 case TPACPI_FAN_WR_TPEC:
7560 rc = fan_get_status(&s);
7564 /* Don't go out of emergency fan mode */
7567 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7570 if (!acpi_ec_write(fan_status_offset, s))
7573 tp_features.fan_ctrl_status_undef = 0;
7578 case TPACPI_FAN_WR_ACPI_SFAN:
7579 rc = fan_get_status(&s);
7585 /* Set fan to at least level 4 */
7588 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7598 mutex_unlock(&fan_mutex);
7601 vdbg_printk(TPACPI_DBG_FAN,
7602 "fan control: set fan control register to 0x%02x\n",
7607 static int fan_set_disable(void)
7611 if (!fan_control_allowed)
7614 if (mutex_lock_killable(&fan_mutex))
7615 return -ERESTARTSYS;
7618 switch (fan_control_access_mode) {
7619 case TPACPI_FAN_WR_ACPI_FANS:
7620 case TPACPI_FAN_WR_TPEC:
7621 if (!acpi_ec_write(fan_status_offset, 0x00))
7624 fan_control_desired_level = 0;
7625 tp_features.fan_ctrl_status_undef = 0;
7629 case TPACPI_FAN_WR_ACPI_SFAN:
7630 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7633 fan_control_desired_level = 0;
7641 vdbg_printk(TPACPI_DBG_FAN,
7642 "fan control: set fan control register to 0\n");
7644 mutex_unlock(&fan_mutex);
7648 static int fan_set_speed(int speed)
7652 if (!fan_control_allowed)
7655 if (mutex_lock_killable(&fan_mutex))
7656 return -ERESTARTSYS;
7659 switch (fan_control_access_mode) {
7660 case TPACPI_FAN_WR_ACPI_FANS:
7661 if (speed >= 0 && speed <= 65535) {
7662 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7663 speed, speed, speed))
7673 mutex_unlock(&fan_mutex);
7677 static void fan_watchdog_reset(void)
7679 static int fan_watchdog_active;
7681 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7684 if (fan_watchdog_active)
7685 cancel_delayed_work(&fan_watchdog_task);
7687 if (fan_watchdog_maxinterval > 0 &&
7688 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7689 fan_watchdog_active = 1;
7690 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7691 msecs_to_jiffies(fan_watchdog_maxinterval
7694 "failed to queue the fan watchdog, "
7695 "watchdog will not trigger\n");
7698 fan_watchdog_active = 0;
7701 static void fan_watchdog_fire(struct work_struct *ignored)
7705 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7708 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7709 rc = fan_set_enable();
7711 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7712 "will try again later...\n", -rc);
7713 /* reschedule for later */
7714 fan_watchdog_reset();
7719 * SYSFS fan layout: hwmon compatible (device)
7722 * 0: "disengaged" mode
7724 * 2: native EC "auto" mode (recommended, hardware default)
7726 * pwm*: set speed in manual mode, ignored otherwise.
7727 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7730 * fan*_input: tachometer reading, RPM
7733 * SYSFS fan layout: extensions
7735 * fan_watchdog (driver):
7736 * fan watchdog interval in seconds, 0 disables (default), max 120
7739 /* sysfs fan pwm1_enable ----------------------------------------------- */
7740 static ssize_t fan_pwm1_enable_show(struct device *dev,
7741 struct device_attribute *attr,
7747 res = fan_get_status_safe(&status);
7751 if (status & TP_EC_FAN_FULLSPEED) {
7753 } else if (status & TP_EC_FAN_AUTO) {
7758 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7761 static ssize_t fan_pwm1_enable_store(struct device *dev,
7762 struct device_attribute *attr,
7763 const char *buf, size_t count)
7768 if (parse_strtoul(buf, 2, &t))
7771 tpacpi_disclose_usertask("hwmon pwm1_enable",
7772 "set fan mode to %lu\n", t);
7776 level = TP_EC_FAN_FULLSPEED;
7779 level = TPACPI_FAN_LAST_LEVEL;
7782 level = TP_EC_FAN_AUTO;
7785 /* reserved for software-controlled auto mode */
7791 res = fan_set_level_safe(level);
7797 fan_watchdog_reset();
7802 static struct device_attribute dev_attr_fan_pwm1_enable =
7803 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7804 fan_pwm1_enable_show, fan_pwm1_enable_store);
7806 /* sysfs fan pwm1 ------------------------------------------------------ */
7807 static ssize_t fan_pwm1_show(struct device *dev,
7808 struct device_attribute *attr,
7814 res = fan_get_status_safe(&status);
7819 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7820 status = fan_control_desired_level;
7825 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7828 static ssize_t fan_pwm1_store(struct device *dev,
7829 struct device_attribute *attr,
7830 const char *buf, size_t count)
7834 u8 status, newlevel;
7836 if (parse_strtoul(buf, 255, &s))
7839 tpacpi_disclose_usertask("hwmon pwm1",
7840 "set fan speed to %lu\n", s);
7842 /* scale down from 0-255 to 0-7 */
7843 newlevel = (s >> 5) & 0x07;
7845 if (mutex_lock_killable(&fan_mutex))
7846 return -ERESTARTSYS;
7848 rc = fan_get_status(&status);
7849 if (!rc && (status &
7850 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7851 rc = fan_set_level(newlevel);
7855 fan_update_desired_level(newlevel);
7856 fan_watchdog_reset();
7860 mutex_unlock(&fan_mutex);
7861 return (rc)? rc : count;
7864 static struct device_attribute dev_attr_fan_pwm1 =
7865 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7866 fan_pwm1_show, fan_pwm1_store);
7868 /* sysfs fan fan1_input ------------------------------------------------ */
7869 static ssize_t fan_fan1_input_show(struct device *dev,
7870 struct device_attribute *attr,
7876 res = fan_get_speed(&speed);
7880 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7883 static struct device_attribute dev_attr_fan_fan1_input =
7884 __ATTR(fan1_input, S_IRUGO,
7885 fan_fan1_input_show, NULL);
7887 /* sysfs fan fan2_input ------------------------------------------------ */
7888 static ssize_t fan_fan2_input_show(struct device *dev,
7889 struct device_attribute *attr,
7895 res = fan2_get_speed(&speed);
7899 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7902 static struct device_attribute dev_attr_fan_fan2_input =
7903 __ATTR(fan2_input, S_IRUGO,
7904 fan_fan2_input_show, NULL);
7906 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7907 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7910 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7913 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7914 const char *buf, size_t count)
7918 if (parse_strtoul(buf, 120, &t))
7921 if (!fan_control_allowed)
7924 fan_watchdog_maxinterval = t;
7925 fan_watchdog_reset();
7927 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7932 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7933 fan_fan_watchdog_show, fan_fan_watchdog_store);
7935 /* --------------------------------------------------------------------- */
7936 static struct attribute *fan_attributes[] = {
7937 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7938 &dev_attr_fan_fan1_input.attr,
7939 NULL, /* for fan2_input */
7943 static const struct attribute_group fan_attr_group = {
7944 .attrs = fan_attributes,
7947 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7948 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7950 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7951 { .vendor = PCI_VENDOR_ID_IBM, \
7952 .bios = TPACPI_MATCH_ANY, \
7953 .ec = TPID(__id1, __id2), \
7954 .quirks = __quirks }
7956 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7957 { .vendor = PCI_VENDOR_ID_LENOVO, \
7958 .bios = TPACPI_MATCH_ANY, \
7959 .ec = TPID(__id1, __id2), \
7960 .quirks = __quirks }
7962 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7963 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7964 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7965 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7966 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7967 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7970 #undef TPACPI_FAN_QL
7971 #undef TPACPI_FAN_QI
7973 static int __init fan_init(struct ibm_init_struct *iibm)
7976 unsigned long quirks;
7978 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7979 "initializing fan subdriver\n");
7981 mutex_init(&fan_mutex);
7982 fan_status_access_mode = TPACPI_FAN_NONE;
7983 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7984 fan_control_commands = 0;
7985 fan_watchdog_maxinterval = 0;
7986 tp_features.fan_ctrl_status_undef = 0;
7987 tp_features.second_fan = 0;
7988 fan_control_desired_level = 7;
7990 if (tpacpi_is_ibm()) {
7991 TPACPI_ACPIHANDLE_INIT(fans);
7992 TPACPI_ACPIHANDLE_INIT(gfan);
7993 TPACPI_ACPIHANDLE_INIT(sfan);
7996 quirks = tpacpi_check_quirks(fan_quirk_table,
7997 ARRAY_SIZE(fan_quirk_table));
8000 /* 570, 600e/x, 770e, 770x */
8001 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8003 /* all other ThinkPads: note that even old-style
8004 * ThinkPad ECs supports the fan control register */
8005 if (likely(acpi_ec_read(fan_status_offset,
8006 &fan_control_initial_status))) {
8007 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8008 if (quirks & TPACPI_FAN_Q1)
8010 if (quirks & TPACPI_FAN_2FAN) {
8011 tp_features.second_fan = 1;
8012 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8013 "secondary fan support enabled\n");
8017 "ThinkPad ACPI EC access misbehaving, "
8018 "fan status and control unavailable\n");
8025 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8026 fan_control_commands |=
8027 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8030 /* gfan without sfan means no fan control */
8031 /* all other models implement TP EC 0x2f control */
8035 fan_control_access_mode =
8036 TPACPI_FAN_WR_ACPI_FANS;
8037 fan_control_commands |=
8038 TPACPI_FAN_CMD_SPEED |
8039 TPACPI_FAN_CMD_LEVEL |
8040 TPACPI_FAN_CMD_ENABLE;
8042 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8043 fan_control_commands |=
8044 TPACPI_FAN_CMD_LEVEL |
8045 TPACPI_FAN_CMD_ENABLE;
8050 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8051 "fan is %s, modes %d, %d\n",
8052 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8053 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8054 fan_status_access_mode, fan_control_access_mode);
8056 /* fan control master switch */
8057 if (!fan_control_allowed) {
8058 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8059 fan_control_commands = 0;
8060 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8061 "fan control features disabled by parameter\n");
8064 /* update fan_control_desired_level */
8065 if (fan_status_access_mode != TPACPI_FAN_NONE)
8066 fan_get_status_safe(NULL);
8068 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8069 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8070 if (tp_features.second_fan) {
8071 /* attach second fan tachometer */
8072 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8073 &dev_attr_fan_fan2_input.attr;
8075 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8080 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8081 &driver_attr_fan_watchdog);
8083 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8092 static void fan_exit(void)
8094 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8095 "cancelling any pending fan watchdog tasks\n");
8097 /* FIXME: can we really do this unconditionally? */
8098 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8099 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8100 &driver_attr_fan_watchdog);
8102 cancel_delayed_work(&fan_watchdog_task);
8103 flush_workqueue(tpacpi_wq);
8106 static void fan_suspend(pm_message_t state)
8110 if (!fan_control_allowed)
8113 /* Store fan status in cache */
8114 fan_control_resume_level = 0;
8115 rc = fan_get_status_safe(&fan_control_resume_level);
8117 printk(TPACPI_NOTICE
8118 "failed to read fan level for later "
8119 "restore during resume: %d\n", rc);
8121 /* if it is undefined, don't attempt to restore it.
8123 if (tp_features.fan_ctrl_status_undef)
8124 fan_control_resume_level = 0;
8127 static void fan_resume(void)
8129 u8 current_level = 7;
8130 bool do_set = false;
8133 /* DSDT *always* updates status on resume */
8134 tp_features.fan_ctrl_status_undef = 0;
8136 if (!fan_control_allowed ||
8137 !fan_control_resume_level ||
8138 (fan_get_status_safe(¤t_level) < 0))
8141 switch (fan_control_access_mode) {
8142 case TPACPI_FAN_WR_ACPI_SFAN:
8143 /* never decrease fan level */
8144 do_set = (fan_control_resume_level > current_level);
8146 case TPACPI_FAN_WR_ACPI_FANS:
8147 case TPACPI_FAN_WR_TPEC:
8148 /* never decrease fan level, scale is:
8149 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8151 * We expect the firmware to set either 7 or AUTO, but we
8152 * handle FULLSPEED out of paranoia.
8154 * So, we can safely only restore FULLSPEED or 7, anything
8155 * else could slow the fan. Restoring AUTO is useless, at
8156 * best that's exactly what the DSDT already set (it is the
8159 * Always keep in mind that the DSDT *will* have set the
8160 * fans to what the vendor supposes is the best level. We
8161 * muck with it only to speed the fan up.
8163 if (fan_control_resume_level != 7 &&
8164 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8167 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8168 (current_level != fan_control_resume_level);
8174 printk(TPACPI_NOTICE
8175 "restoring fan level to 0x%02x\n",
8176 fan_control_resume_level);
8177 rc = fan_set_level_safe(fan_control_resume_level);
8179 printk(TPACPI_NOTICE
8180 "failed to restore fan level: %d\n", rc);
8184 static int fan_read(struct seq_file *m)
8188 unsigned int speed = 0;
8190 switch (fan_status_access_mode) {
8191 case TPACPI_FAN_RD_ACPI_GFAN:
8192 /* 570, 600e/x, 770e, 770x */
8193 rc = fan_get_status_safe(&status);
8197 seq_printf(m, "status:\t\t%s\n"
8199 (status != 0) ? "enabled" : "disabled", status);
8202 case TPACPI_FAN_RD_TPEC:
8203 /* all except 570, 600e/x, 770e, 770x */
8204 rc = fan_get_status_safe(&status);
8208 seq_printf(m, "status:\t\t%s\n",
8209 (status != 0) ? "enabled" : "disabled");
8211 rc = fan_get_speed(&speed);
8215 seq_printf(m, "speed:\t\t%d\n", speed);
8217 if (status & TP_EC_FAN_FULLSPEED)
8218 /* Disengaged mode takes precedence */
8219 seq_printf(m, "level:\t\tdisengaged\n");
8220 else if (status & TP_EC_FAN_AUTO)
8221 seq_printf(m, "level:\t\tauto\n");
8223 seq_printf(m, "level:\t\t%d\n", status);
8226 case TPACPI_FAN_NONE:
8228 seq_printf(m, "status:\t\tnot supported\n");
8231 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8232 seq_printf(m, "commands:\tlevel <level>");
8234 switch (fan_control_access_mode) {
8235 case TPACPI_FAN_WR_ACPI_SFAN:
8236 seq_printf(m, " (<level> is 0-7)\n");
8240 seq_printf(m, " (<level> is 0-7, "
8241 "auto, disengaged, full-speed)\n");
8246 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8247 seq_printf(m, "commands:\tenable, disable\n"
8248 "commands:\twatchdog <timeout> (<timeout> "
8249 "is 0 (off), 1-120 (seconds))\n");
8251 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8252 seq_printf(m, "commands:\tspeed <speed>"
8253 " (<speed> is 0-65535)\n");
8258 static int fan_write_cmd_level(const char *cmd, int *rc)
8262 if (strlencmp(cmd, "level auto") == 0)
8263 level = TP_EC_FAN_AUTO;
8264 else if ((strlencmp(cmd, "level disengaged") == 0) |
8265 (strlencmp(cmd, "level full-speed") == 0))
8266 level = TP_EC_FAN_FULLSPEED;
8267 else if (sscanf(cmd, "level %d", &level) != 1)
8270 *rc = fan_set_level_safe(level);
8272 printk(TPACPI_ERR "level command accepted for unsupported "
8273 "access mode %d", fan_control_access_mode);
8275 tpacpi_disclose_usertask("procfs fan",
8276 "set level to %d\n", level);
8281 static int fan_write_cmd_enable(const char *cmd, int *rc)
8283 if (strlencmp(cmd, "enable") != 0)
8286 *rc = fan_set_enable();
8288 printk(TPACPI_ERR "enable command accepted for unsupported "
8289 "access mode %d", fan_control_access_mode);
8291 tpacpi_disclose_usertask("procfs fan", "enable\n");
8296 static int fan_write_cmd_disable(const char *cmd, int *rc)
8298 if (strlencmp(cmd, "disable") != 0)
8301 *rc = fan_set_disable();
8303 printk(TPACPI_ERR "disable command accepted for unsupported "
8304 "access mode %d", fan_control_access_mode);
8306 tpacpi_disclose_usertask("procfs fan", "disable\n");
8311 static int fan_write_cmd_speed(const char *cmd, int *rc)
8316 * Support speed <low> <medium> <high> ? */
8318 if (sscanf(cmd, "speed %d", &speed) != 1)
8321 *rc = fan_set_speed(speed);
8323 printk(TPACPI_ERR "speed command accepted for unsupported "
8324 "access mode %d", fan_control_access_mode);
8326 tpacpi_disclose_usertask("procfs fan",
8327 "set speed to %d\n", speed);
8332 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8336 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8339 if (interval < 0 || interval > 120)
8342 fan_watchdog_maxinterval = interval;
8343 tpacpi_disclose_usertask("procfs fan",
8344 "set watchdog timer to %d\n",
8351 static int fan_write(char *buf)
8356 while (!rc && (cmd = next_cmd(&buf))) {
8357 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8358 fan_write_cmd_level(cmd, &rc)) &&
8359 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8360 (fan_write_cmd_enable(cmd, &rc) ||
8361 fan_write_cmd_disable(cmd, &rc) ||
8362 fan_write_cmd_watchdog(cmd, &rc))) &&
8363 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8364 fan_write_cmd_speed(cmd, &rc))
8368 fan_watchdog_reset();
8374 static struct ibm_struct fan_driver_data = {
8379 .suspend = fan_suspend,
8380 .resume = fan_resume,
8383 /****************************************************************************
8384 ****************************************************************************
8388 ****************************************************************************
8389 ****************************************************************************/
8392 * HKEY event callout for other subdrivers go here
8393 * (yes, it is ugly, but it is quick, safe, and gets the job done
8395 static void tpacpi_driver_event(const unsigned int hkey_event)
8397 if (ibm_backlight_device) {
8398 switch (hkey_event) {
8399 case TP_HKEY_EV_BRGHT_UP:
8400 case TP_HKEY_EV_BRGHT_DOWN:
8401 tpacpi_brightness_notify_change();
8405 switch (hkey_event) {
8406 case TP_HKEY_EV_VOL_UP:
8407 case TP_HKEY_EV_VOL_DOWN:
8408 case TP_HKEY_EV_VOL_MUTE:
8409 volume_alsa_notify_change();
8414 static void hotkey_driver_event(const unsigned int scancode)
8416 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8419 /* sysfs name ---------------------------------------------------------- */
8420 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8421 struct device_attribute *attr,
8424 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8427 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8428 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8430 /* --------------------------------------------------------------------- */
8433 static struct proc_dir_entry *proc_dir;
8436 * Module and infrastructure proble, init and exit handling
8439 static int force_load;
8441 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8442 static const char * __init str_supported(int is_supported)
8444 static char text_unsupported[] __initdata = "not supported";
8446 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8448 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8450 static void ibm_exit(struct ibm_struct *ibm)
8452 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8454 list_del_init(&ibm->all_drivers);
8456 if (ibm->flags.acpi_notify_installed) {
8457 dbg_printk(TPACPI_DBG_EXIT,
8458 "%s: acpi_remove_notify_handler\n", ibm->name);
8460 acpi_remove_notify_handler(*ibm->acpi->handle,
8462 dispatch_acpi_notify);
8463 ibm->flags.acpi_notify_installed = 0;
8464 ibm->flags.acpi_notify_installed = 0;
8467 if (ibm->flags.proc_created) {
8468 dbg_printk(TPACPI_DBG_EXIT,
8469 "%s: remove_proc_entry\n", ibm->name);
8470 remove_proc_entry(ibm->name, proc_dir);
8471 ibm->flags.proc_created = 0;
8474 if (ibm->flags.acpi_driver_registered) {
8475 dbg_printk(TPACPI_DBG_EXIT,
8476 "%s: acpi_bus_unregister_driver\n", ibm->name);
8478 acpi_bus_unregister_driver(ibm->acpi->driver);
8479 kfree(ibm->acpi->driver);
8480 ibm->acpi->driver = NULL;
8481 ibm->flags.acpi_driver_registered = 0;
8484 if (ibm->flags.init_called && ibm->exit) {
8486 ibm->flags.init_called = 0;
8489 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8492 static int __init ibm_init(struct ibm_init_struct *iibm)
8495 struct ibm_struct *ibm = iibm->data;
8496 struct proc_dir_entry *entry;
8498 BUG_ON(ibm == NULL);
8500 INIT_LIST_HEAD(&ibm->all_drivers);
8502 if (ibm->flags.experimental && !experimental)
8505 dbg_printk(TPACPI_DBG_INIT,
8506 "probing for %s\n", ibm->name);
8509 ret = iibm->init(iibm);
8511 return 0; /* probe failed */
8515 ibm->flags.init_called = 1;
8519 if (ibm->acpi->hid) {
8520 ret = register_tpacpi_subdriver(ibm);
8525 if (ibm->acpi->notify) {
8526 ret = setup_acpi_notify(ibm);
8527 if (ret == -ENODEV) {
8528 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8538 dbg_printk(TPACPI_DBG_INIT,
8539 "%s installed\n", ibm->name);
8542 mode_t mode = iibm->base_procfs_mode;
8548 entry = proc_create_data(ibm->name, mode, proc_dir,
8549 &dispatch_proc_fops, ibm);
8551 printk(TPACPI_ERR "unable to create proc entry %s\n",
8556 ibm->flags.proc_created = 1;
8559 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8564 dbg_printk(TPACPI_DBG_INIT,
8565 "%s: at error exit path with result %d\n",
8569 return (ret < 0)? ret : 0;
8574 static bool __pure __init tpacpi_is_fw_digit(const char c)
8576 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8579 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8580 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8583 return s && strlen(s) >= 8 &&
8584 tpacpi_is_fw_digit(s[0]) &&
8585 tpacpi_is_fw_digit(s[1]) &&
8586 s[2] == t && s[3] == 'T' &&
8587 tpacpi_is_fw_digit(s[4]) &&
8588 tpacpi_is_fw_digit(s[5]) &&
8589 s[6] == 'W' && s[7] == 'W';
8592 /* returns 0 - probe ok, or < 0 - probe error.
8593 * Probe ok doesn't mean thinkpad found.
8594 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8595 static int __must_check __init get_thinkpad_model_data(
8596 struct thinkpad_id_data *tp)
8598 const struct dmi_device *dev = NULL;
8599 char ec_fw_string[18];
8605 memset(tp, 0, sizeof(*tp));
8607 if (dmi_name_in_vendors("IBM"))
8608 tp->vendor = PCI_VENDOR_ID_IBM;
8609 else if (dmi_name_in_vendors("LENOVO"))
8610 tp->vendor = PCI_VENDOR_ID_LENOVO;
8614 s = dmi_get_system_info(DMI_BIOS_VERSION);
8615 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8616 if (s && !tp->bios_version_str)
8619 /* Really ancient ThinkPad 240X will fail this, which is fine */
8620 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8623 tp->bios_model = tp->bios_version_str[0]
8624 | (tp->bios_version_str[1] << 8);
8625 tp->bios_release = (tp->bios_version_str[4] << 8)
8626 | tp->bios_version_str[5];
8629 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8630 * X32 or newer, all Z series; Some models must have an
8631 * up-to-date BIOS or they will not be detected.
8633 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8635 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8636 if (sscanf(dev->name,
8637 "IBM ThinkPad Embedded Controller -[%17c",
8638 ec_fw_string) == 1) {
8639 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8640 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8642 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8643 if (!tp->ec_version_str)
8646 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8647 tp->ec_model = ec_fw_string[0]
8648 | (ec_fw_string[1] << 8);
8649 tp->ec_release = (ec_fw_string[4] << 8)
8652 printk(TPACPI_NOTICE
8653 "ThinkPad firmware release %s "
8654 "doesn't match the known patterns\n",
8656 printk(TPACPI_NOTICE
8657 "please report this to %s\n",
8664 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8665 if (s && !strnicmp(s, "ThinkPad", 8)) {
8666 tp->model_str = kstrdup(s, GFP_KERNEL);
8671 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8672 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8673 if (s && !tp->nummodel_str)
8679 static int __init probe_for_thinkpad(void)
8686 /* It would be dangerous to run the driver in this case */
8687 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8691 * Non-ancient models have better DMI tagging, but very old models
8692 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8694 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8695 (thinkpad_id.ec_model != 0) ||
8696 tpacpi_is_fw_known();
8698 /* ec is required because many other handles are relative to it */
8699 TPACPI_ACPIHANDLE_INIT(ec);
8703 "Not yet supported ThinkPad detected!\n");
8707 if (!is_thinkpad && !force_load)
8714 /* Module init, exit, parameters */
8716 static struct ibm_init_struct ibms_init[] __initdata = {
8718 .init = thinkpad_acpi_driver_init,
8719 .data = &thinkpad_acpi_driver_data,
8722 .init = hotkey_init,
8723 .data = &hotkey_driver_data,
8726 .init = bluetooth_init,
8727 .data = &bluetooth_driver_data,
8731 .data = &wan_driver_data,
8735 .data = &uwb_driver_data,
8737 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8740 .base_procfs_mode = S_IRUSR,
8741 .data = &video_driver_data,
8746 .data = &light_driver_data,
8750 .data = &cmos_driver_data,
8754 .data = &led_driver_data,
8758 .data = &beep_driver_data,
8761 .init = thermal_init,
8762 .data = &thermal_driver_data,
8765 .data = &ecdump_driver_data,
8768 .init = brightness_init,
8769 .data = &brightness_driver_data,
8772 .init = volume_init,
8773 .data = &volume_driver_data,
8777 .data = &fan_driver_data,
8781 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8784 struct ibm_struct *ibm;
8786 if (!kp || !kp->name || !val)
8789 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8790 ibm = ibms_init[i].data;
8791 WARN_ON(ibm == NULL);
8793 if (!ibm || !ibm->name)
8796 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8797 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8799 strcpy(ibms_init[i].param, val);
8800 strcat(ibms_init[i].param, ",");
8808 module_param(experimental, int, 0444);
8809 MODULE_PARM_DESC(experimental,
8810 "Enables experimental features when non-zero");
8812 module_param_named(debug, dbg_level, uint, 0);
8813 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8815 module_param(force_load, bool, 0444);
8816 MODULE_PARM_DESC(force_load,
8817 "Attempts to load the driver even on a "
8818 "mis-identified ThinkPad when true");
8820 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8821 MODULE_PARM_DESC(fan_control,
8822 "Enables setting fan parameters features when true");
8824 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8825 MODULE_PARM_DESC(brightness_mode,
8826 "Selects brightness control strategy: "
8827 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8829 module_param(brightness_enable, uint, 0444);
8830 MODULE_PARM_DESC(brightness_enable,
8831 "Enables backlight control when 1, disables when 0");
8833 module_param(hotkey_report_mode, uint, 0444);
8834 MODULE_PARM_DESC(hotkey_report_mode,
8835 "used for backwards compatibility with userspace, "
8836 "see documentation");
8838 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8839 module_param_named(volume_mode, volume_mode, uint, 0444);
8840 MODULE_PARM_DESC(volume_mode,
8841 "Selects volume control strategy: "
8842 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8844 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8845 MODULE_PARM_DESC(volume_capabilities,
8846 "Selects the mixer capabilites: "
8847 "0=auto, 1=volume and mute, 2=mute only");
8849 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8850 MODULE_PARM_DESC(volume_control,
8851 "Enables software override for the console audio "
8852 "control when true");
8854 /* ALSA module API parameters */
8855 module_param_named(index, alsa_index, int, 0444);
8856 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8857 module_param_named(id, alsa_id, charp, 0444);
8858 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8859 module_param_named(enable, alsa_enable, bool, 0444);
8860 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8861 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8863 #define TPACPI_PARAM(feature) \
8864 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8865 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8866 "at module load, see documentation")
8868 TPACPI_PARAM(hotkey);
8869 TPACPI_PARAM(bluetooth);
8870 TPACPI_PARAM(video);
8871 TPACPI_PARAM(light);
8875 TPACPI_PARAM(ecdump);
8876 TPACPI_PARAM(brightness);
8877 TPACPI_PARAM(volume);
8880 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8881 module_param(dbg_wlswemul, uint, 0444);
8882 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8883 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8884 MODULE_PARM_DESC(wlsw_state,
8885 "Initial state of the emulated WLSW switch");
8887 module_param(dbg_bluetoothemul, uint, 0444);
8888 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8889 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8890 MODULE_PARM_DESC(bluetooth_state,
8891 "Initial state of the emulated bluetooth switch");
8893 module_param(dbg_wwanemul, uint, 0444);
8894 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8895 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8896 MODULE_PARM_DESC(wwan_state,
8897 "Initial state of the emulated WWAN switch");
8899 module_param(dbg_uwbemul, uint, 0444);
8900 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8901 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8902 MODULE_PARM_DESC(uwb_state,
8903 "Initial state of the emulated UWB switch");
8906 static void thinkpad_acpi_module_exit(void)
8908 struct ibm_struct *ibm, *itmp;
8910 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8912 list_for_each_entry_safe_reverse(ibm, itmp,
8913 &tpacpi_all_drivers,
8918 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8920 if (tpacpi_inputdev) {
8921 if (tp_features.input_device_registered)
8922 input_unregister_device(tpacpi_inputdev);
8924 input_free_device(tpacpi_inputdev);
8928 hwmon_device_unregister(tpacpi_hwmon);
8930 if (tp_features.sensors_pdev_attrs_registered)
8931 device_remove_file(&tpacpi_sensors_pdev->dev,
8932 &dev_attr_thinkpad_acpi_pdev_name);
8933 if (tpacpi_sensors_pdev)
8934 platform_device_unregister(tpacpi_sensors_pdev);
8936 platform_device_unregister(tpacpi_pdev);
8938 if (tp_features.sensors_pdrv_attrs_registered)
8939 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8940 if (tp_features.platform_drv_attrs_registered)
8941 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8943 if (tp_features.sensors_pdrv_registered)
8944 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8946 if (tp_features.platform_drv_registered)
8947 platform_driver_unregister(&tpacpi_pdriver);
8950 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8953 destroy_workqueue(tpacpi_wq);
8955 kfree(thinkpad_id.bios_version_str);
8956 kfree(thinkpad_id.ec_version_str);
8957 kfree(thinkpad_id.model_str);
8961 static int __init thinkpad_acpi_module_init(void)
8965 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8967 /* Parameter checking */
8968 if (hotkey_report_mode > 2)
8971 /* Driver-level probe */
8973 ret = get_thinkpad_model_data(&thinkpad_id);
8976 "unable to get DMI data: %d\n", ret);
8977 thinkpad_acpi_module_exit();
8980 ret = probe_for_thinkpad();
8982 thinkpad_acpi_module_exit();
8986 /* Driver initialization */
8988 TPACPI_ACPIHANDLE_INIT(ecrd);
8989 TPACPI_ACPIHANDLE_INIT(ecwr);
8991 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8993 thinkpad_acpi_module_exit();
8997 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9000 "unable to create proc dir " TPACPI_PROC_DIR);
9001 thinkpad_acpi_module_exit();
9005 ret = platform_driver_register(&tpacpi_pdriver);
9008 "unable to register main platform driver\n");
9009 thinkpad_acpi_module_exit();
9012 tp_features.platform_drv_registered = 1;
9014 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9017 "unable to register hwmon platform driver\n");
9018 thinkpad_acpi_module_exit();
9021 tp_features.sensors_pdrv_registered = 1;
9023 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9025 tp_features.platform_drv_attrs_registered = 1;
9026 ret = tpacpi_create_driver_attributes(
9027 &tpacpi_hwmon_pdriver.driver);
9031 "unable to create sysfs driver attributes\n");
9032 thinkpad_acpi_module_exit();
9035 tp_features.sensors_pdrv_attrs_registered = 1;
9038 /* Device initialization */
9039 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9041 if (IS_ERR(tpacpi_pdev)) {
9042 ret = PTR_ERR(tpacpi_pdev);
9044 printk(TPACPI_ERR "unable to register platform device\n");
9045 thinkpad_acpi_module_exit();
9048 tpacpi_sensors_pdev = platform_device_register_simple(
9049 TPACPI_HWMON_DRVR_NAME,
9051 if (IS_ERR(tpacpi_sensors_pdev)) {
9052 ret = PTR_ERR(tpacpi_sensors_pdev);
9053 tpacpi_sensors_pdev = NULL;
9055 "unable to register hwmon platform device\n");
9056 thinkpad_acpi_module_exit();
9059 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9060 &dev_attr_thinkpad_acpi_pdev_name);
9063 "unable to create sysfs hwmon device attributes\n");
9064 thinkpad_acpi_module_exit();
9067 tp_features.sensors_pdev_attrs_registered = 1;
9068 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9069 if (IS_ERR(tpacpi_hwmon)) {
9070 ret = PTR_ERR(tpacpi_hwmon);
9071 tpacpi_hwmon = NULL;
9072 printk(TPACPI_ERR "unable to register hwmon device\n");
9073 thinkpad_acpi_module_exit();
9076 mutex_init(&tpacpi_inputdev_send_mutex);
9077 tpacpi_inputdev = input_allocate_device();
9078 if (!tpacpi_inputdev) {
9079 printk(TPACPI_ERR "unable to allocate input device\n");
9080 thinkpad_acpi_module_exit();
9083 /* Prepare input device, but don't register */
9084 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9085 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9086 tpacpi_inputdev->id.bustype = BUS_HOST;
9087 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9088 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9089 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9090 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9092 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9093 ret = ibm_init(&ibms_init[i]);
9094 if (ret >= 0 && *ibms_init[i].param)
9095 ret = ibms_init[i].data->write(ibms_init[i].param);
9097 thinkpad_acpi_module_exit();
9102 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9104 ret = input_register_device(tpacpi_inputdev);
9106 printk(TPACPI_ERR "unable to register input device\n");
9107 thinkpad_acpi_module_exit();
9110 tp_features.input_device_registered = 1;
9116 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9119 * This will autoload the driver in almost every ThinkPad
9120 * in widespread use.
9122 * Only _VERY_ old models, like the 240, 240x and 570 lack
9123 * the HKEY event interface.
9125 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9128 * DMI matching for module autoloading
9130 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9131 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9133 * Only models listed in thinkwiki will be supported, so add yours
9134 * if it is not there yet.
9136 #define IBM_BIOS_MODULE_ALIAS(__type) \
9137 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9139 /* Ancient thinkpad BIOSes have to be identified by
9140 * BIOS type or model number, and there are far less
9141 * BIOS types than model numbers... */
9142 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9144 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9145 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9146 MODULE_DESCRIPTION(TPACPI_DESC);
9147 MODULE_VERSION(TPACPI_VERSION);
9148 MODULE_LICENSE("GPL");
9150 module_init(thinkpad_acpi_module_init);
9151 module_exit(thinkpad_acpi_module_exit);