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,
125 /* Misc NVRAM-related */
127 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
131 #define TPACPI_ACPI_HKEY_HID "IBM0068"
134 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
135 #define TPACPI_HKEY_INPUT_VERSION 0x4101
137 /* ACPI \WGSV commands */
139 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
140 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
141 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
142 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
145 /* TP_ACPI_WGSV_GET_STATE bits */
147 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
148 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
149 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
150 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
151 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
152 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
153 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
154 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
155 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
156 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 enum tpacpi_hkey_event_t {
162 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
163 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
164 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
184 /* User-interface events */
185 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
186 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
187 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
188 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
189 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
190 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
191 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
194 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
195 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
196 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
197 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
198 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
201 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
204 /****************************************************************************
208 #define TPACPI_NAME "thinkpad"
209 #define TPACPI_DESC "ThinkPad ACPI Extras"
210 #define TPACPI_FILE TPACPI_NAME "_acpi"
211 #define TPACPI_URL "http://ibm-acpi.sf.net/"
212 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
214 #define TPACPI_PROC_DIR "ibm"
215 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
216 #define TPACPI_DRVR_NAME TPACPI_FILE
217 #define TPACPI_DRVR_SHORTNAME "tpacpi"
218 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
220 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
221 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
223 #define TPACPI_MAX_ACPI_ARGS 3
226 #define TPACPI_LOG TPACPI_FILE ": "
227 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
228 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
229 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
230 #define TPACPI_ERR KERN_ERR TPACPI_LOG
231 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
232 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
233 #define TPACPI_INFO KERN_INFO TPACPI_LOG
234 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
236 /* Debugging printk groups */
237 #define TPACPI_DBG_ALL 0xffff
238 #define TPACPI_DBG_DISCLOSETASK 0x8000
239 #define TPACPI_DBG_INIT 0x0001
240 #define TPACPI_DBG_EXIT 0x0002
241 #define TPACPI_DBG_RFKILL 0x0004
242 #define TPACPI_DBG_HKEY 0x0008
243 #define TPACPI_DBG_FAN 0x0010
244 #define TPACPI_DBG_BRGHT 0x0020
245 #define TPACPI_DBG_MIXER 0x0040
247 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
248 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
249 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
252 /****************************************************************************
253 * Driver-wide structs and misc. variables
258 struct tp_acpi_drv_struct {
259 const struct acpi_device_id *hid;
260 struct acpi_driver *driver;
262 void (*notify) (struct ibm_struct *, u32);
265 struct acpi_device *device;
271 int (*read) (struct seq_file *);
272 int (*write) (char *);
274 void (*resume) (void);
275 void (*suspend) (pm_message_t state);
276 void (*shutdown) (void);
278 struct list_head all_drivers;
280 struct tp_acpi_drv_struct *acpi;
283 u8 acpi_driver_registered:1;
284 u8 acpi_notify_installed:1;
291 struct ibm_init_struct {
294 int (*init) (struct ibm_init_struct *);
295 mode_t base_procfs_mode;
296 struct ibm_struct *data;
307 u32 bright_acpimode:1;
311 u32 fan_ctrl_status_undef:1;
313 u32 beep_needs_two_args:1;
314 u32 mixer_no_level_control:1;
315 u32 input_device_registered:1;
316 u32 platform_drv_registered:1;
317 u32 platform_drv_attrs_registered:1;
318 u32 sensors_pdrv_registered:1;
319 u32 sensors_pdrv_attrs_registered:1;
320 u32 sensors_pdev_attrs_registered:1;
321 u32 hotkey_poll_active:1;
325 u16 hotkey_mask_ff:1;
326 u16 volume_ctrl_forbidden:1;
329 struct thinkpad_id_data {
330 unsigned int vendor; /* ThinkPad vendor:
331 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
333 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
334 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
336 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
341 char *model_str; /* ThinkPad T43 */
342 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
344 static struct thinkpad_id_data thinkpad_id;
347 TPACPI_LIFE_INIT = 0,
352 static int experimental;
353 static u32 dbg_level;
355 static struct workqueue_struct *tpacpi_wq;
363 /* Special LED class that can defer work */
364 struct tpacpi_led_classdev {
365 struct led_classdev led_classdev;
366 struct work_struct work;
367 enum led_status_t new_state;
371 /* brightness level capabilities */
372 static unsigned int bright_maxlvl; /* 0 = unknown */
374 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
375 static int dbg_wlswemul;
376 static int tpacpi_wlsw_emulstate;
377 static int dbg_bluetoothemul;
378 static int tpacpi_bluetooth_emulstate;
379 static int dbg_wwanemul;
380 static int tpacpi_wwan_emulstate;
381 static int dbg_uwbemul;
382 static int tpacpi_uwb_emulstate;
386 /*************************************************************************
390 #define dbg_printk(a_dbg_level, format, arg...) \
391 do { if (dbg_level & (a_dbg_level)) \
392 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
395 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
396 #define vdbg_printk dbg_printk
397 static const char *str_supported(int is_supported);
399 #define vdbg_printk(a_dbg_level, format, arg...) \
403 static void tpacpi_log_usertask(const char * const what)
405 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
406 what, task_tgid_vnr(current));
409 #define tpacpi_disclose_usertask(what, format, arg...) \
412 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
413 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
414 printk(TPACPI_DEBUG "%s: PID %d: " format, \
415 what, task_tgid_vnr(current), ## arg); \
420 * Quirk handling helpers
422 * ThinkPad IDs and versions seen in the field so far
423 * are two-characters from the set [0-9A-Z], i.e. base 36.
425 * We use values well outside that range as specials.
428 #define TPACPI_MATCH_ANY 0xffffU
429 #define TPACPI_MATCH_UNKNOWN 0U
431 /* TPID('1', 'Y') == 0x5931 */
432 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
434 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
435 { .vendor = PCI_VENDOR_ID_IBM, \
436 .bios = TPID(__id1, __id2), \
437 .ec = TPACPI_MATCH_ANY, \
438 .quirks = (__quirk) }
440 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
441 { .vendor = PCI_VENDOR_ID_LENOVO, \
442 .bios = TPID(__id1, __id2), \
443 .ec = TPACPI_MATCH_ANY, \
444 .quirks = (__quirk) }
446 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_LENOVO, \
448 .bios = TPACPI_MATCH_ANY, \
449 .ec = TPID(__id1, __id2), \
450 .quirks = (__quirk) }
452 struct tpacpi_quirk {
456 unsigned long quirks;
460 * tpacpi_check_quirks() - search BIOS/EC version on a list
461 * @qlist: array of &struct tpacpi_quirk
462 * @qlist_size: number of elements in @qlist
464 * Iterates over a quirks list until one is found that matches the
465 * ThinkPad's vendor, BIOS and EC model.
467 * Returns 0 if nothing matches, otherwise returns the quirks field of
468 * the matching &struct tpacpi_quirk entry.
470 * The match criteria is: vendor, ec and bios much match.
472 static unsigned long __init tpacpi_check_quirks(
473 const struct tpacpi_quirk *qlist,
474 unsigned int qlist_size)
477 if ((qlist->vendor == thinkpad_id.vendor ||
478 qlist->vendor == TPACPI_MATCH_ANY) &&
479 (qlist->bios == thinkpad_id.bios_model ||
480 qlist->bios == TPACPI_MATCH_ANY) &&
481 (qlist->ec == thinkpad_id.ec_model ||
482 qlist->ec == TPACPI_MATCH_ANY))
483 return qlist->quirks;
491 static inline bool __pure __init tpacpi_is_lenovo(void)
493 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
496 static inline bool __pure __init tpacpi_is_ibm(void)
498 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
501 /****************************************************************************
502 ****************************************************************************
504 * ACPI Helpers and device model
506 ****************************************************************************
507 ****************************************************************************/
509 /*************************************************************************
513 static acpi_handle root_handle;
515 #define TPACPI_HANDLE(object, parent, paths...) \
516 static acpi_handle object##_handle; \
517 static acpi_handle *object##_parent = &parent##_handle; \
518 static char *object##_path; \
519 static char *object##_paths[] = { paths }
521 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
522 "\\_SB.PCI.ISA.EC", /* 570 */
523 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
524 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
525 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
526 "\\_SB.PCI0.ICH3.EC0", /* R31 */
527 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
528 "\\_SB.PCI0.LPC.EC", /* all others */
531 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
532 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
534 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
536 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
537 "\\CMS", /* R40, R40e */
540 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
541 "^HKEY", /* R30, R31 */
542 "HKEY", /* all others */
545 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
546 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
547 "\\_SB.PCI0.VID0", /* 770e */
548 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
549 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
550 "\\_SB.PCI0.AGP.VID", /* all others */
554 /*************************************************************************
558 static int acpi_evalf(acpi_handle handle,
559 void *res, char *method, char *fmt, ...)
562 struct acpi_object_list params;
563 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
564 struct acpi_buffer result, *resultp;
565 union acpi_object out_obj;
573 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
586 params.pointer = &in_objs[0];
593 in_objs[params.count].integer.value = va_arg(ap, int);
594 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
596 /* add more types as needed */
598 printk(TPACPI_ERR "acpi_evalf() called "
599 "with invalid format character '%c'\n", c);
605 if (res_type != 'v') {
606 result.length = sizeof(out_obj);
607 result.pointer = &out_obj;
612 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
617 *(int *)res = out_obj.integer.value;
618 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
621 success = status == AE_OK;
623 /* add more types as needed */
625 printk(TPACPI_ERR "acpi_evalf() called "
626 "with invalid format character '%c'\n", res_type);
630 if (!success && !quiet)
631 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
632 method, fmt0, status);
637 static int acpi_ec_read(int i, u8 *p)
642 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
646 if (ec_read(i, p) < 0)
653 static int acpi_ec_write(int i, u8 v)
656 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
659 if (ec_write(i, v) < 0)
666 static int issue_thinkpad_cmos_command(int cmos_cmd)
671 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
677 /*************************************************************************
681 #define TPACPI_ACPIHANDLE_INIT(object) \
682 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
683 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
685 static void drv_acpi_handle_init(char *name,
686 acpi_handle *handle, acpi_handle parent,
687 char **paths, int num_paths, char **path)
692 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
695 for (i = 0; i < num_paths; i++) {
696 status = acpi_get_handle(parent, paths[i], handle);
697 if (ACPI_SUCCESS(status)) {
699 dbg_printk(TPACPI_DBG_INIT,
700 "Found ACPI handle %s for %s\n",
706 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
711 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
713 struct ibm_struct *ibm = data;
715 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
718 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
721 ibm->acpi->notify(ibm, event);
724 static int __init setup_acpi_notify(struct ibm_struct *ibm)
731 if (!*ibm->acpi->handle)
734 vdbg_printk(TPACPI_DBG_INIT,
735 "setting up ACPI notify for %s\n", ibm->name);
737 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
739 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
744 ibm->acpi->device->driver_data = ibm;
745 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
746 TPACPI_ACPI_EVENT_PREFIX,
749 status = acpi_install_notify_handler(*ibm->acpi->handle,
750 ibm->acpi->type, dispatch_acpi_notify, ibm);
751 if (ACPI_FAILURE(status)) {
752 if (status == AE_ALREADY_EXISTS) {
754 "another device driver is already "
755 "handling %s events\n", ibm->name);
758 "acpi_install_notify_handler(%s) failed: %d\n",
763 ibm->flags.acpi_notify_installed = 1;
767 static int __init tpacpi_device_add(struct acpi_device *device)
772 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
776 dbg_printk(TPACPI_DBG_INIT,
777 "registering %s as an ACPI driver\n", ibm->name);
781 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
782 if (!ibm->acpi->driver) {
784 "failed to allocate memory for ibm->acpi->driver\n");
788 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
789 ibm->acpi->driver->ids = ibm->acpi->hid;
791 ibm->acpi->driver->ops.add = &tpacpi_device_add;
793 rc = acpi_bus_register_driver(ibm->acpi->driver);
795 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
797 kfree(ibm->acpi->driver);
798 ibm->acpi->driver = NULL;
800 ibm->flags.acpi_driver_registered = 1;
806 /****************************************************************************
807 ****************************************************************************
811 ****************************************************************************
812 ****************************************************************************/
814 static int dispatch_proc_show(struct seq_file *m, void *v)
816 struct ibm_struct *ibm = m->private;
818 if (!ibm || !ibm->read)
823 static int dispatch_proc_open(struct inode *inode, struct file *file)
825 return single_open(file, dispatch_proc_show, PDE(inode)->data);
828 static ssize_t dispatch_proc_write(struct file *file,
829 const char __user *userbuf,
830 size_t count, loff_t *pos)
832 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
836 if (!ibm || !ibm->write)
838 if (count > PAGE_SIZE - 2)
841 kernbuf = kmalloc(count + 2, GFP_KERNEL);
845 if (copy_from_user(kernbuf, userbuf, count)) {
851 strcat(kernbuf, ",");
852 ret = ibm->write(kernbuf);
861 static const struct file_operations dispatch_proc_fops = {
862 .owner = THIS_MODULE,
863 .open = dispatch_proc_open,
866 .release = single_release,
867 .write = dispatch_proc_write,
870 static char *next_cmd(char **cmds)
875 while ((end = strchr(start, ',')) && end == start)
887 /****************************************************************************
888 ****************************************************************************
890 * Device model: input, hwmon and platform
892 ****************************************************************************
893 ****************************************************************************/
895 static struct platform_device *tpacpi_pdev;
896 static struct platform_device *tpacpi_sensors_pdev;
897 static struct device *tpacpi_hwmon;
898 static struct input_dev *tpacpi_inputdev;
899 static struct mutex tpacpi_inputdev_send_mutex;
900 static LIST_HEAD(tpacpi_all_drivers);
902 static int tpacpi_suspend_handler(struct platform_device *pdev,
905 struct ibm_struct *ibm, *itmp;
907 list_for_each_entry_safe(ibm, itmp,
911 (ibm->suspend)(state);
917 static int tpacpi_resume_handler(struct platform_device *pdev)
919 struct ibm_struct *ibm, *itmp;
921 list_for_each_entry_safe(ibm, itmp,
931 static void tpacpi_shutdown_handler(struct platform_device *pdev)
933 struct ibm_struct *ibm, *itmp;
935 list_for_each_entry_safe(ibm, itmp,
943 static struct platform_driver tpacpi_pdriver = {
945 .name = TPACPI_DRVR_NAME,
946 .owner = THIS_MODULE,
948 .suspend = tpacpi_suspend_handler,
949 .resume = tpacpi_resume_handler,
950 .shutdown = tpacpi_shutdown_handler,
953 static struct platform_driver tpacpi_hwmon_pdriver = {
955 .name = TPACPI_HWMON_DRVR_NAME,
956 .owner = THIS_MODULE,
960 /*************************************************************************
961 * sysfs support helpers
964 struct attribute_set {
965 unsigned int members, max_members;
966 struct attribute_group group;
969 struct attribute_set_obj {
970 struct attribute_set s;
972 } __attribute__((packed));
974 static struct attribute_set *create_attr_set(unsigned int max_members,
977 struct attribute_set_obj *sobj;
979 if (max_members == 0)
982 /* Allocates space for implicit NULL at the end too */
983 sobj = kzalloc(sizeof(struct attribute_set_obj) +
984 max_members * sizeof(struct attribute *),
988 sobj->s.max_members = max_members;
989 sobj->s.group.attrs = &sobj->a;
990 sobj->s.group.name = name;
995 #define destroy_attr_set(_set) \
998 /* not multi-threaded safe, use it in a single thread per set */
999 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1004 if (s->members >= s->max_members)
1007 s->group.attrs[s->members] = attr;
1013 static int add_many_to_attr_set(struct attribute_set *s,
1014 struct attribute **attr,
1019 for (i = 0; i < count; i++) {
1020 res = add_to_attr_set(s, attr[i]);
1028 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1030 sysfs_remove_group(kobj, &s->group);
1031 destroy_attr_set(s);
1034 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1035 sysfs_create_group(_kobj, &_attr_set->group)
1037 static int parse_strtoul(const char *buf,
1038 unsigned long max, unsigned long *value)
1042 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1043 endp = skip_spaces(endp);
1044 if (*endp || *value > max)
1050 static void tpacpi_disable_brightness_delay(void)
1052 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1053 printk(TPACPI_NOTICE
1054 "ACPI backlight control delay disabled\n");
1057 static void printk_deprecated_attribute(const char * const what,
1058 const char * const details)
1060 tpacpi_log_usertask("deprecated sysfs attribute");
1061 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1062 "will be removed. %s\n",
1066 /*************************************************************************
1067 * rfkill and radio control support helpers
1071 * ThinkPad-ACPI firmware handling model:
1073 * WLSW (master wireless switch) is event-driven, and is common to all
1074 * firmware-controlled radios. It cannot be controlled, just monitored,
1075 * as expected. It overrides all radio state in firmware
1077 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1078 * (TODO: verify how WLSW interacts with the returned radio state).
1080 * The only time there are shadow radio state changes, is when
1081 * masked-off hotkeys are used.
1085 * Internal driver API for radio state:
1087 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1088 * bool: true means radio blocked (off)
1090 enum tpacpi_rfkill_state {
1091 TPACPI_RFK_RADIO_OFF = 0,
1095 /* rfkill switches */
1096 enum tpacpi_rfk_id {
1097 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1098 TPACPI_RFK_WWAN_SW_ID,
1099 TPACPI_RFK_UWB_SW_ID,
1103 static const char *tpacpi_rfkill_names[] = {
1104 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1105 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1106 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1107 [TPACPI_RFK_SW_MAX] = NULL
1110 /* ThinkPad-ACPI rfkill subdriver */
1112 struct rfkill *rfkill;
1113 enum tpacpi_rfk_id id;
1114 const struct tpacpi_rfk_ops *ops;
1117 struct tpacpi_rfk_ops {
1118 /* firmware interface */
1119 int (*get_status)(void);
1120 int (*set_status)(const enum tpacpi_rfkill_state);
1123 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1125 /* Query FW and update rfkill sw state for a given rfkill switch */
1126 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1133 status = (tp_rfk->ops->get_status)();
1137 rfkill_set_sw_state(tp_rfk->rfkill,
1138 (status == TPACPI_RFK_RADIO_OFF));
1143 /* Query FW and update rfkill sw state for all rfkill switches */
1144 static void tpacpi_rfk_update_swstate_all(void)
1148 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1149 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1153 * Sync the HW-blocking state of all rfkill switches,
1154 * do notice it causes the rfkill core to schedule uevents
1156 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1159 struct tpacpi_rfk *tp_rfk;
1161 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1162 tp_rfk = tpacpi_rfkill_switches[i];
1164 if (rfkill_set_hw_state(tp_rfk->rfkill,
1166 /* ignore -- we track sw block */
1172 /* Call to get the WLSW state from the firmware */
1173 static int hotkey_get_wlsw(void);
1175 /* Call to query WLSW state and update all rfkill switches */
1176 static bool tpacpi_rfk_check_hwblock_state(void)
1178 int res = hotkey_get_wlsw();
1181 /* When unknown or unsupported, we have to assume it is unblocked */
1185 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1186 tpacpi_rfk_update_hwblock_state(hw_blocked);
1191 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1193 struct tpacpi_rfk *tp_rfk = data;
1196 dbg_printk(TPACPI_DBG_RFKILL,
1197 "request to change radio state to %s\n",
1198 blocked ? "blocked" : "unblocked");
1200 /* try to set radio state */
1201 res = (tp_rfk->ops->set_status)(blocked ?
1202 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1204 /* and update the rfkill core with whatever the FW really did */
1205 tpacpi_rfk_update_swstate(tp_rfk);
1207 return (res < 0) ? res : 0;
1210 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1211 .set_block = tpacpi_rfk_hook_set_block,
1214 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1215 const struct tpacpi_rfk_ops *tp_rfkops,
1216 const enum rfkill_type rfktype,
1218 const bool set_default)
1220 struct tpacpi_rfk *atp_rfk;
1222 bool sw_state = false;
1226 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1228 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1230 atp_rfk->rfkill = rfkill_alloc(name,
1233 &tpacpi_rfk_rfkill_ops,
1235 if (!atp_rfk || !atp_rfk->rfkill) {
1237 "failed to allocate memory for rfkill class\n");
1243 atp_rfk->ops = tp_rfkops;
1245 sw_status = (tp_rfkops->get_status)();
1246 if (sw_status < 0) {
1248 "failed to read initial state for %s, error %d\n",
1251 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1253 /* try to keep the initial state, since we ask the
1254 * firmware to preserve it across S5 in NVRAM */
1255 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1258 hw_state = tpacpi_rfk_check_hwblock_state();
1259 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1261 res = rfkill_register(atp_rfk->rfkill);
1264 "failed to register %s rfkill switch: %d\n",
1266 rfkill_destroy(atp_rfk->rfkill);
1271 tpacpi_rfkill_switches[id] = atp_rfk;
1273 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1274 name, (sw_state || hw_state) ? "" : "un");
1278 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1280 struct tpacpi_rfk *tp_rfk;
1282 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1284 tp_rfk = tpacpi_rfkill_switches[id];
1286 rfkill_unregister(tp_rfk->rfkill);
1287 rfkill_destroy(tp_rfk->rfkill);
1288 tpacpi_rfkill_switches[id] = NULL;
1293 static void printk_deprecated_rfkill_attribute(const char * const what)
1295 printk_deprecated_attribute(what,
1296 "Please switch to generic rfkill before year 2010");
1299 /* sysfs <radio> enable ------------------------------------------------ */
1300 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1301 struct device_attribute *attr,
1306 printk_deprecated_rfkill_attribute(attr->attr.name);
1308 /* This is in the ABI... */
1309 if (tpacpi_rfk_check_hwblock_state()) {
1310 status = TPACPI_RFK_RADIO_OFF;
1312 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1317 return snprintf(buf, PAGE_SIZE, "%d\n",
1318 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1321 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1322 struct device_attribute *attr,
1323 const char *buf, size_t count)
1328 printk_deprecated_rfkill_attribute(attr->attr.name);
1330 if (parse_strtoul(buf, 1, &t))
1333 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1335 /* This is in the ABI... */
1336 if (tpacpi_rfk_check_hwblock_state() && !!t)
1339 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1340 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1341 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1343 return (res < 0) ? res : count;
1346 /* procfs -------------------------------------------------------------- */
1347 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1349 if (id >= TPACPI_RFK_SW_MAX)
1350 seq_printf(m, "status:\t\tnot supported\n");
1354 /* This is in the ABI... */
1355 if (tpacpi_rfk_check_hwblock_state()) {
1356 status = TPACPI_RFK_RADIO_OFF;
1358 status = tpacpi_rfk_update_swstate(
1359 tpacpi_rfkill_switches[id]);
1364 seq_printf(m, "status:\t\t%s\n",
1365 (status == TPACPI_RFK_RADIO_ON) ?
1366 "enabled" : "disabled");
1367 seq_printf(m, "commands:\tenable, disable\n");
1373 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1379 if (id >= TPACPI_RFK_SW_MAX)
1382 while ((cmd = next_cmd(&buf))) {
1383 if (strlencmp(cmd, "enable") == 0)
1384 status = TPACPI_RFK_RADIO_ON;
1385 else if (strlencmp(cmd, "disable") == 0)
1386 status = TPACPI_RFK_RADIO_OFF;
1392 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1393 (status == TPACPI_RFK_RADIO_ON) ?
1394 "enable" : "disable",
1395 tpacpi_rfkill_names[id]);
1396 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1397 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1403 /*************************************************************************
1404 * thinkpad-acpi driver attributes
1407 /* interface_version --------------------------------------------------- */
1408 static ssize_t tpacpi_driver_interface_version_show(
1409 struct device_driver *drv,
1412 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1415 static DRIVER_ATTR(interface_version, S_IRUGO,
1416 tpacpi_driver_interface_version_show, NULL);
1418 /* debug_level --------------------------------------------------------- */
1419 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1422 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1425 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1426 const char *buf, size_t count)
1430 if (parse_strtoul(buf, 0xffff, &t))
1438 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1439 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1441 /* version ------------------------------------------------------------- */
1442 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1445 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1446 TPACPI_DESC, TPACPI_VERSION);
1449 static DRIVER_ATTR(version, S_IRUGO,
1450 tpacpi_driver_version_show, NULL);
1452 /* --------------------------------------------------------------------- */
1454 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1456 /* wlsw_emulstate ------------------------------------------------------ */
1457 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1460 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1463 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1464 const char *buf, size_t count)
1468 if (parse_strtoul(buf, 1, &t))
1471 if (tpacpi_wlsw_emulstate != !!t) {
1472 tpacpi_wlsw_emulstate = !!t;
1473 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1479 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1480 tpacpi_driver_wlsw_emulstate_show,
1481 tpacpi_driver_wlsw_emulstate_store);
1483 /* bluetooth_emulstate ------------------------------------------------- */
1484 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1485 struct device_driver *drv,
1488 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1491 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1492 struct device_driver *drv,
1493 const char *buf, size_t count)
1497 if (parse_strtoul(buf, 1, &t))
1500 tpacpi_bluetooth_emulstate = !!t;
1505 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1506 tpacpi_driver_bluetooth_emulstate_show,
1507 tpacpi_driver_bluetooth_emulstate_store);
1509 /* wwan_emulstate ------------------------------------------------- */
1510 static ssize_t tpacpi_driver_wwan_emulstate_show(
1511 struct device_driver *drv,
1514 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1517 static ssize_t tpacpi_driver_wwan_emulstate_store(
1518 struct device_driver *drv,
1519 const char *buf, size_t count)
1523 if (parse_strtoul(buf, 1, &t))
1526 tpacpi_wwan_emulstate = !!t;
1531 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1532 tpacpi_driver_wwan_emulstate_show,
1533 tpacpi_driver_wwan_emulstate_store);
1535 /* uwb_emulstate ------------------------------------------------- */
1536 static ssize_t tpacpi_driver_uwb_emulstate_show(
1537 struct device_driver *drv,
1540 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1543 static ssize_t tpacpi_driver_uwb_emulstate_store(
1544 struct device_driver *drv,
1545 const char *buf, size_t count)
1549 if (parse_strtoul(buf, 1, &t))
1552 tpacpi_uwb_emulstate = !!t;
1557 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1558 tpacpi_driver_uwb_emulstate_show,
1559 tpacpi_driver_uwb_emulstate_store);
1562 /* --------------------------------------------------------------------- */
1564 static struct driver_attribute *tpacpi_driver_attributes[] = {
1565 &driver_attr_debug_level, &driver_attr_version,
1566 &driver_attr_interface_version,
1569 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1575 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1576 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1580 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1581 if (!res && dbg_wlswemul)
1582 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1583 if (!res && dbg_bluetoothemul)
1584 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1585 if (!res && dbg_wwanemul)
1586 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1587 if (!res && dbg_uwbemul)
1588 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1594 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1598 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1599 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1601 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1602 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1603 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1604 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1605 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1609 /*************************************************************************
1614 * Table of recommended minimum BIOS versions
1616 * Reasons for listing:
1617 * 1. Stable BIOS, listed because the unknown amount of
1618 * bugs and bad ACPI behaviour on older versions
1620 * 2. BIOS or EC fw with known bugs that trigger on Linux
1622 * 3. BIOS with known reduced functionality in older versions
1624 * We recommend the latest BIOS and EC version.
1625 * We only support the latest BIOS and EC fw version as a rule.
1627 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1628 * Information from users in ThinkWiki
1630 * WARNING: we use this table also to detect that the machine is
1631 * a ThinkPad in some cases, so don't remove entries lightly.
1634 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1635 { .vendor = (__v), \
1636 .bios = TPID(__id1, __id2), \
1637 .ec = TPACPI_MATCH_ANY, \
1638 .quirks = TPACPI_MATCH_ANY << 16 \
1639 | (__bv1) << 8 | (__bv2) }
1641 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1642 __eid, __ev1, __ev2) \
1643 { .vendor = (__v), \
1644 .bios = TPID(__bid1, __bid2), \
1646 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1647 | (__bv1) << 8 | (__bv2) }
1649 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1650 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1652 /* Outdated IBM BIOSes often lack the EC id string */
1653 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1654 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1655 __bv1, __bv2, TPID(__id1, __id2), \
1657 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1658 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1661 /* Outdated IBM BIOSes often lack the EC id string */
1662 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1663 __eid1, __eid2, __ev1, __ev2) \
1664 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1665 __bv1, __bv2, TPID(__eid1, __eid2), \
1667 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1668 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1671 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1672 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1674 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1675 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1676 __bv1, __bv2, TPID(__id1, __id2), \
1679 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1680 __eid1, __eid2, __ev1, __ev2) \
1681 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1682 __bv1, __bv2, TPID(__eid1, __eid2), \
1685 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1686 /* Numeric models ------------------ */
1687 /* FW MODEL BIOS VERS */
1688 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1689 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1690 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1691 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1692 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1693 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1694 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1695 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1696 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1698 /* A-series ------------------------- */
1699 /* FW MODEL BIOS VERS EC VERS */
1700 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1701 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1702 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1703 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1704 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1705 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1706 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1707 TPV_QI0('1', '3', '2', '0'), /* A22m */
1708 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1709 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1710 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1712 /* G-series ------------------------- */
1713 /* FW MODEL BIOS VERS */
1714 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1715 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1717 /* R-series, T-series --------------- */
1718 /* FW MODEL BIOS VERS EC VERS */
1719 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1720 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1721 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1722 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1723 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1724 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1725 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1726 T40/p, T41/p, T42/p (1) */
1727 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1728 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1729 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1730 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1732 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1733 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1734 TPV_QI0('1', '6', '3', '2'), /* T22 */
1735 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1736 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1737 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1739 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1740 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1741 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1743 /* BIOS FW BIOS VERS EC FW EC VERS */
1744 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1745 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1747 /* X-series ------------------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1750 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1751 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1752 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1753 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1754 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1755 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1757 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1758 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1760 /* (0) - older versions lack DMI EC fw string and functionality */
1761 /* (1) - older versions known to lack functionality */
1772 static void __init tpacpi_check_outdated_fw(void)
1774 unsigned long fwvers;
1775 u16 ec_version, bios_version;
1777 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1778 ARRAY_SIZE(tpacpi_bios_version_qtable));
1783 bios_version = fwvers & 0xffffU;
1784 ec_version = (fwvers >> 16) & 0xffffU;
1786 /* note that unknown versions are set to 0x0000 and we use that */
1787 if ((bios_version > thinkpad_id.bios_release) ||
1788 (ec_version > thinkpad_id.ec_release &&
1789 ec_version != TPACPI_MATCH_ANY)) {
1791 * The changelogs would let us track down the exact
1792 * reason, but it is just too much of a pain to track
1793 * it. We only list BIOSes that are either really
1794 * broken, or really stable to begin with, so it is
1795 * best if the user upgrades the firmware anyway.
1798 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1800 "WARNING: This firmware may be missing critical bug "
1801 "fixes and/or important features\n");
1805 static bool __init tpacpi_is_fw_known(void)
1807 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1811 /****************************************************************************
1812 ****************************************************************************
1816 ****************************************************************************
1817 ****************************************************************************/
1819 /*************************************************************************
1820 * thinkpad-acpi metadata subdriver
1823 static int thinkpad_acpi_driver_read(struct seq_file *m)
1825 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1826 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1830 static struct ibm_struct thinkpad_acpi_driver_data = {
1832 .read = thinkpad_acpi_driver_read,
1835 /*************************************************************************
1840 * ThinkPad firmware event model
1842 * The ThinkPad firmware has two main event interfaces: normal ACPI
1843 * notifications (which follow the ACPI standard), and a private event
1846 * The private event interface also issues events for the hotkeys. As
1847 * the driver gained features, the event handling code ended up being
1848 * built around the hotkey subdriver. This will need to be refactored
1849 * to a more formal event API eventually.
1851 * Some "hotkeys" are actually supposed to be used as event reports,
1852 * such as "brightness has changed", "volume has changed", depending on
1853 * the ThinkPad model and how the firmware is operating.
1855 * Unlike other classes, hotkey-class events have mask/unmask control on
1856 * non-ancient firmware. However, how it behaves changes a lot with the
1857 * firmware model and version.
1860 enum { /* hot key scan codes (derived from ACPI DSDT) */
1861 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1862 TP_ACPI_HOTKEYSCAN_FNF2,
1863 TP_ACPI_HOTKEYSCAN_FNF3,
1864 TP_ACPI_HOTKEYSCAN_FNF4,
1865 TP_ACPI_HOTKEYSCAN_FNF5,
1866 TP_ACPI_HOTKEYSCAN_FNF6,
1867 TP_ACPI_HOTKEYSCAN_FNF7,
1868 TP_ACPI_HOTKEYSCAN_FNF8,
1869 TP_ACPI_HOTKEYSCAN_FNF9,
1870 TP_ACPI_HOTKEYSCAN_FNF10,
1871 TP_ACPI_HOTKEYSCAN_FNF11,
1872 TP_ACPI_HOTKEYSCAN_FNF12,
1873 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1874 TP_ACPI_HOTKEYSCAN_FNINSERT,
1875 TP_ACPI_HOTKEYSCAN_FNDELETE,
1876 TP_ACPI_HOTKEYSCAN_FNHOME,
1877 TP_ACPI_HOTKEYSCAN_FNEND,
1878 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1879 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1880 TP_ACPI_HOTKEYSCAN_FNSPACE,
1881 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1882 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1883 TP_ACPI_HOTKEYSCAN_MUTE,
1884 TP_ACPI_HOTKEYSCAN_THINKPAD,
1887 enum { /* Keys/events available through NVRAM polling */
1888 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1889 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1892 enum { /* Positions of some of the keys in hotkey masks */
1893 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1894 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1895 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1896 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1897 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1898 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1899 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1900 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1901 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1902 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1903 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1906 enum { /* NVRAM to ACPI HKEY group map */
1907 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1908 TP_ACPI_HKEY_ZOOM_MASK |
1909 TP_ACPI_HKEY_DISPSWTCH_MASK |
1910 TP_ACPI_HKEY_HIBERNATE_MASK,
1911 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1912 TP_ACPI_HKEY_BRGHTDWN_MASK,
1913 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1914 TP_ACPI_HKEY_VOLDWN_MASK |
1915 TP_ACPI_HKEY_MUTE_MASK,
1918 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1919 struct tp_nvram_state {
1920 u16 thinkpad_toggle:1;
1922 u16 display_toggle:1;
1923 u16 thinklight_toggle:1;
1924 u16 hibernate_toggle:1;
1925 u16 displayexp_toggle:1;
1926 u16 display_state:1;
1927 u16 brightness_toggle:1;
1928 u16 volume_toggle:1;
1931 u8 brightness_level;
1935 /* kthread for the hotkey poller */
1936 static struct task_struct *tpacpi_hotkey_task;
1938 /* Acquired while the poller kthread is running, use to sync start/stop */
1939 static struct mutex hotkey_thread_mutex;
1942 * Acquire mutex to write poller control variables as an
1945 * Increment hotkey_config_change when changing them if you
1946 * want the kthread to forget old state.
1948 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1950 static struct mutex hotkey_thread_data_mutex;
1951 static unsigned int hotkey_config_change;
1954 * hotkey poller control variables
1956 * Must be atomic or readers will also need to acquire mutex
1958 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1959 * should be used only when the changes need to be taken as
1960 * a block, OR when one needs to force the kthread to forget
1963 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1964 static unsigned int hotkey_poll_freq = 10; /* Hz */
1966 #define HOTKEY_CONFIG_CRITICAL_START \
1968 mutex_lock(&hotkey_thread_data_mutex); \
1969 hotkey_config_change++; \
1971 #define HOTKEY_CONFIG_CRITICAL_END \
1972 mutex_unlock(&hotkey_thread_data_mutex);
1974 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1976 #define hotkey_source_mask 0U
1977 #define HOTKEY_CONFIG_CRITICAL_START
1978 #define HOTKEY_CONFIG_CRITICAL_END
1980 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1982 static struct mutex hotkey_mutex;
1984 static enum { /* Reasons for waking up */
1985 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1986 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1987 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1988 } hotkey_wakeup_reason;
1990 static int hotkey_autosleep_ack;
1992 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1993 static u32 hotkey_all_mask; /* all events supported in fw */
1994 static u32 hotkey_reserved_mask; /* events better left disabled */
1995 static u32 hotkey_driver_mask; /* events needed by the driver */
1996 static u32 hotkey_user_mask; /* events visible to userspace */
1997 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1999 static unsigned int hotkey_report_mode;
2001 static u16 *hotkey_keycode_map;
2003 static struct attribute_set *hotkey_dev_attributes;
2005 static void tpacpi_driver_event(const unsigned int hkey_event);
2006 static void hotkey_driver_event(const unsigned int scancode);
2007 static void hotkey_poll_setup(const bool may_warn);
2009 /* HKEY.MHKG() return bits */
2010 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2012 static int hotkey_get_wlsw(void)
2016 if (!tp_features.hotkey_wlsw)
2019 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2021 return (tpacpi_wlsw_emulstate) ?
2022 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2025 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2028 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2031 static int hotkey_get_tablet_mode(int *status)
2035 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2038 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2043 * Reads current event mask from firmware, and updates
2044 * hotkey_acpi_mask accordingly. Also resets any bits
2045 * from hotkey_user_mask that are unavailable to be
2046 * delivered (shadow requirement of the userspace ABI).
2048 * Call with hotkey_mutex held
2050 static int hotkey_mask_get(void)
2052 if (tp_features.hotkey_mask) {
2055 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2058 hotkey_acpi_mask = m;
2060 /* no mask support doesn't mean no event support... */
2061 hotkey_acpi_mask = hotkey_all_mask;
2064 /* sync userspace-visible mask */
2065 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2070 void static hotkey_mask_warn_incomplete_mask(void)
2072 /* log only what the user can fix... */
2073 const u32 wantedmask = hotkey_driver_mask &
2074 ~(hotkey_acpi_mask | hotkey_source_mask) &
2075 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2078 printk(TPACPI_NOTICE
2079 "required events 0x%08x not enabled!\n",
2084 * Set the firmware mask when supported
2086 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2088 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2090 * Call with hotkey_mutex held
2092 static int hotkey_mask_set(u32 mask)
2097 const u32 fwmask = mask & ~hotkey_source_mask;
2099 if (tp_features.hotkey_mask) {
2100 for (i = 0; i < 32; i++) {
2101 if (!acpi_evalf(hkey_handle,
2102 NULL, "MHKM", "vdd", i + 1,
2103 !!(mask & (1 << i)))) {
2111 * We *must* make an inconditional call to hotkey_mask_get to
2112 * refresh hotkey_acpi_mask and update hotkey_user_mask
2114 * Take the opportunity to also log when we cannot _enable_
2117 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2118 printk(TPACPI_NOTICE
2119 "asked for hotkey mask 0x%08x, but "
2120 "firmware forced it to 0x%08x\n",
2121 fwmask, hotkey_acpi_mask);
2124 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2125 hotkey_mask_warn_incomplete_mask();
2131 * Sets hotkey_user_mask and tries to set the firmware mask
2133 * Call with hotkey_mutex held
2135 static int hotkey_user_mask_set(const u32 mask)
2139 /* Give people a chance to notice they are doing something that
2140 * is bound to go boom on their users sooner or later */
2141 if (!tp_warned.hotkey_mask_ff &&
2142 (mask == 0xffff || mask == 0xffffff ||
2143 mask == 0xffffffff)) {
2144 tp_warned.hotkey_mask_ff = 1;
2145 printk(TPACPI_NOTICE
2146 "setting the hotkey mask to 0x%08x is likely "
2147 "not the best way to go about it\n", mask);
2148 printk(TPACPI_NOTICE
2149 "please consider using the driver defaults, "
2150 "and refer to up-to-date thinkpad-acpi "
2154 /* Try to enable what the user asked for, plus whatever we need.
2155 * this syncs everything but won't enable bits in hotkey_user_mask */
2156 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2158 /* Enable the available bits in hotkey_user_mask */
2159 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2165 * Sets the driver hotkey mask.
2167 * Can be called even if the hotkey subdriver is inactive
2169 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2173 /* Do the right thing if hotkey_init has not been called yet */
2174 if (!tp_features.hotkey) {
2175 hotkey_driver_mask = mask;
2179 mutex_lock(&hotkey_mutex);
2181 HOTKEY_CONFIG_CRITICAL_START
2182 hotkey_driver_mask = mask;
2183 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2184 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2186 HOTKEY_CONFIG_CRITICAL_END
2188 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2189 ~hotkey_source_mask);
2190 hotkey_poll_setup(true);
2192 mutex_unlock(&hotkey_mutex);
2197 static int hotkey_status_get(int *status)
2199 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2205 static int hotkey_status_set(bool enable)
2207 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2213 static void tpacpi_input_send_tabletsw(void)
2217 if (tp_features.hotkey_tablet &&
2218 !hotkey_get_tablet_mode(&state)) {
2219 mutex_lock(&tpacpi_inputdev_send_mutex);
2221 input_report_switch(tpacpi_inputdev,
2222 SW_TABLET_MODE, !!state);
2223 input_sync(tpacpi_inputdev);
2225 mutex_unlock(&tpacpi_inputdev_send_mutex);
2229 /* Do NOT call without validating scancode first */
2230 static void tpacpi_input_send_key(const unsigned int scancode)
2232 const unsigned int keycode = hotkey_keycode_map[scancode];
2234 if (keycode != KEY_RESERVED) {
2235 mutex_lock(&tpacpi_inputdev_send_mutex);
2237 input_report_key(tpacpi_inputdev, keycode, 1);
2238 if (keycode == KEY_UNKNOWN)
2239 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2241 input_sync(tpacpi_inputdev);
2243 input_report_key(tpacpi_inputdev, keycode, 0);
2244 if (keycode == KEY_UNKNOWN)
2245 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2247 input_sync(tpacpi_inputdev);
2249 mutex_unlock(&tpacpi_inputdev_send_mutex);
2253 /* Do NOT call without validating scancode first */
2254 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2256 hotkey_driver_event(scancode);
2257 if (hotkey_user_mask & (1 << scancode))
2258 tpacpi_input_send_key(scancode);
2261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2264 /* Do NOT call without validating scancode first */
2265 static void tpacpi_hotkey_send_key(unsigned int scancode)
2267 tpacpi_input_send_key_masked(scancode);
2268 if (hotkey_report_mode < 2) {
2269 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2270 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2274 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2278 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2279 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2280 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2281 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2282 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2283 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2285 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2286 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2287 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2289 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2290 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2291 n->displayexp_toggle =
2292 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2294 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2295 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2296 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2297 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2298 n->brightness_toggle =
2299 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2301 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2302 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2303 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2304 >> TP_NVRAM_POS_LEVEL_VOLUME;
2305 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2306 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2310 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2311 struct tp_nvram_state *newn,
2312 const u32 event_mask)
2315 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2317 if ((event_mask & (1 << __scancode)) && \
2318 oldn->__member != newn->__member) \
2319 tpacpi_hotkey_send_key(__scancode); \
2322 #define TPACPI_MAY_SEND_KEY(__scancode) \
2324 if (event_mask & (1 << __scancode)) \
2325 tpacpi_hotkey_send_key(__scancode); \
2328 void issue_volchange(const unsigned int oldvol,
2329 const unsigned int newvol)
2331 unsigned int i = oldvol;
2333 while (i > newvol) {
2334 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2337 while (i < newvol) {
2338 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2343 void issue_brightnesschange(const unsigned int oldbrt,
2344 const unsigned int newbrt)
2346 unsigned int i = oldbrt;
2348 while (i > newbrt) {
2349 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2352 while (i < newbrt) {
2353 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2358 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2359 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2360 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2361 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2363 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2370 * This code is supposed to duplicate the IBM firmware behaviour:
2371 * - Pressing MUTE issues mute hotkey message, even when already mute
2372 * - Pressing Volume up/down issues volume up/down hotkey messages,
2373 * even when already at maximum or minumum volume
2374 * - The act of unmuting issues volume up/down notification,
2375 * depending which key was used to unmute
2377 * We are constrained to what the NVRAM can tell us, which is not much
2378 * and certainly not enough if more than one volume hotkey was pressed
2379 * since the last poll cycle.
2381 * Just to make our life interesting, some newer Lenovo ThinkPads have
2382 * bugs in the BIOS and may fail to update volume_toggle properly.
2387 oldn->volume_toggle != newn->volume_toggle ||
2388 oldn->volume_level != newn->volume_level) {
2389 /* recently muted, or repeated mute keypress, or
2390 * multiple presses ending in mute */
2391 issue_volchange(oldn->volume_level, newn->volume_level);
2392 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2397 /* recently unmuted, issue 'unmute' keypress */
2398 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2400 if (oldn->volume_level != newn->volume_level) {
2401 issue_volchange(oldn->volume_level, newn->volume_level);
2402 } else if (oldn->volume_toggle != newn->volume_toggle) {
2403 /* repeated vol up/down keypress at end of scale ? */
2404 if (newn->volume_level == 0)
2405 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2406 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2411 /* handle brightness */
2412 if (oldn->brightness_level != newn->brightness_level) {
2413 issue_brightnesschange(oldn->brightness_level,
2414 newn->brightness_level);
2415 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2416 /* repeated key presses that didn't change state */
2417 if (newn->brightness_level == 0)
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2419 else if (newn->brightness_level >= bright_maxlvl
2420 && !tp_features.bright_unkfw)
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2424 #undef TPACPI_COMPARE_KEY
2425 #undef TPACPI_MAY_SEND_KEY
2431 * We track all events in hotkey_source_mask all the time, since
2432 * most of them are edge-based. We only issue those requested by
2433 * hotkey_user_mask or hotkey_driver_mask, though.
2435 static int hotkey_kthread(void *data)
2437 struct tp_nvram_state s[2];
2438 u32 poll_mask, event_mask;
2439 unsigned int si, so;
2441 unsigned int change_detector, must_reset;
2442 unsigned int poll_freq;
2444 mutex_lock(&hotkey_thread_mutex);
2446 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2455 /* Initial state for compares */
2456 mutex_lock(&hotkey_thread_data_mutex);
2457 change_detector = hotkey_config_change;
2458 poll_mask = hotkey_source_mask;
2459 event_mask = hotkey_source_mask &
2460 (hotkey_driver_mask | hotkey_user_mask);
2461 poll_freq = hotkey_poll_freq;
2462 mutex_unlock(&hotkey_thread_data_mutex);
2463 hotkey_read_nvram(&s[so], poll_mask);
2465 while (!kthread_should_stop()) {
2467 if (likely(poll_freq))
2470 t = 100; /* should never happen... */
2472 t = msleep_interruptible(t);
2473 if (unlikely(kthread_should_stop()))
2475 must_reset = try_to_freeze();
2476 if (t > 0 && !must_reset)
2479 mutex_lock(&hotkey_thread_data_mutex);
2480 if (must_reset || hotkey_config_change != change_detector) {
2481 /* forget old state on thaw or config change */
2484 change_detector = hotkey_config_change;
2486 poll_mask = hotkey_source_mask;
2487 event_mask = hotkey_source_mask &
2488 (hotkey_driver_mask | hotkey_user_mask);
2489 poll_freq = hotkey_poll_freq;
2490 mutex_unlock(&hotkey_thread_data_mutex);
2492 if (likely(poll_mask)) {
2493 hotkey_read_nvram(&s[si], poll_mask);
2494 if (likely(si != so)) {
2495 hotkey_compare_and_issue_event(&s[so], &s[si],
2505 mutex_unlock(&hotkey_thread_mutex);
2509 /* call with hotkey_mutex held */
2510 static void hotkey_poll_stop_sync(void)
2512 if (tpacpi_hotkey_task) {
2513 if (frozen(tpacpi_hotkey_task) ||
2514 freezing(tpacpi_hotkey_task))
2515 thaw_process(tpacpi_hotkey_task);
2517 kthread_stop(tpacpi_hotkey_task);
2518 tpacpi_hotkey_task = NULL;
2519 mutex_lock(&hotkey_thread_mutex);
2520 /* at this point, the thread did exit */
2521 mutex_unlock(&hotkey_thread_mutex);
2525 /* call with hotkey_mutex held */
2526 static void hotkey_poll_setup(const bool may_warn)
2528 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2529 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2531 if (hotkey_poll_freq > 0 &&
2532 (poll_driver_mask ||
2533 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2534 if (!tpacpi_hotkey_task) {
2535 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2536 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2537 if (IS_ERR(tpacpi_hotkey_task)) {
2538 tpacpi_hotkey_task = NULL;
2540 "could not create kernel thread "
2541 "for hotkey polling\n");
2545 hotkey_poll_stop_sync();
2546 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2547 hotkey_poll_freq == 0) {
2548 printk(TPACPI_NOTICE
2549 "hot keys 0x%08x and/or events 0x%08x "
2550 "require polling, which is currently "
2552 poll_user_mask, poll_driver_mask);
2557 static void hotkey_poll_setup_safe(const bool may_warn)
2559 mutex_lock(&hotkey_mutex);
2560 hotkey_poll_setup(may_warn);
2561 mutex_unlock(&hotkey_mutex);
2564 /* call with hotkey_mutex held */
2565 static void hotkey_poll_set_freq(unsigned int freq)
2568 hotkey_poll_stop_sync();
2570 hotkey_poll_freq = freq;
2573 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2575 static void hotkey_poll_setup(const bool __unused)
2579 static void hotkey_poll_setup_safe(const bool __unused)
2583 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2585 static int hotkey_inputdev_open(struct input_dev *dev)
2587 switch (tpacpi_lifecycle) {
2588 case TPACPI_LIFE_INIT:
2589 case TPACPI_LIFE_RUNNING:
2590 hotkey_poll_setup_safe(false);
2592 case TPACPI_LIFE_EXITING:
2596 /* Should only happen if tpacpi_lifecycle is corrupt */
2601 static void hotkey_inputdev_close(struct input_dev *dev)
2603 /* disable hotkey polling when possible */
2604 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2605 !(hotkey_source_mask & hotkey_driver_mask))
2606 hotkey_poll_setup_safe(false);
2609 /* sysfs hotkey enable ------------------------------------------------- */
2610 static ssize_t hotkey_enable_show(struct device *dev,
2611 struct device_attribute *attr,
2616 printk_deprecated_attribute("hotkey_enable",
2617 "Hotkey reporting is always enabled");
2619 res = hotkey_status_get(&status);
2623 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2626 static ssize_t hotkey_enable_store(struct device *dev,
2627 struct device_attribute *attr,
2628 const char *buf, size_t count)
2632 printk_deprecated_attribute("hotkey_enable",
2633 "Hotkeys can be disabled through hotkey_mask");
2635 if (parse_strtoul(buf, 1, &t))
2644 static struct device_attribute dev_attr_hotkey_enable =
2645 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2646 hotkey_enable_show, hotkey_enable_store);
2648 /* sysfs hotkey mask --------------------------------------------------- */
2649 static ssize_t hotkey_mask_show(struct device *dev,
2650 struct device_attribute *attr,
2653 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2656 static ssize_t hotkey_mask_store(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2663 if (parse_strtoul(buf, 0xffffffffUL, &t))
2666 if (mutex_lock_killable(&hotkey_mutex))
2667 return -ERESTARTSYS;
2669 res = hotkey_user_mask_set(t);
2671 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2672 hotkey_poll_setup(true);
2675 mutex_unlock(&hotkey_mutex);
2677 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2679 return (res) ? res : count;
2682 static struct device_attribute dev_attr_hotkey_mask =
2683 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2684 hotkey_mask_show, hotkey_mask_store);
2686 /* sysfs hotkey bios_enabled ------------------------------------------- */
2687 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2688 struct device_attribute *attr,
2691 return sprintf(buf, "0\n");
2694 static struct device_attribute dev_attr_hotkey_bios_enabled =
2695 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2697 /* sysfs hotkey bios_mask ---------------------------------------------- */
2698 static ssize_t hotkey_bios_mask_show(struct device *dev,
2699 struct device_attribute *attr,
2702 printk_deprecated_attribute("hotkey_bios_mask",
2703 "This attribute is useless.");
2704 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2707 static struct device_attribute dev_attr_hotkey_bios_mask =
2708 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2710 /* sysfs hotkey all_mask ----------------------------------------------- */
2711 static ssize_t hotkey_all_mask_show(struct device *dev,
2712 struct device_attribute *attr,
2715 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2716 hotkey_all_mask | hotkey_source_mask);
2719 static struct device_attribute dev_attr_hotkey_all_mask =
2720 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2722 /* sysfs hotkey recommended_mask --------------------------------------- */
2723 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2724 struct device_attribute *attr,
2727 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2728 (hotkey_all_mask | hotkey_source_mask)
2729 & ~hotkey_reserved_mask);
2732 static struct device_attribute dev_attr_hotkey_recommended_mask =
2733 __ATTR(hotkey_recommended_mask, S_IRUGO,
2734 hotkey_recommended_mask_show, NULL);
2736 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2738 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2739 static ssize_t hotkey_source_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2743 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2746 static ssize_t hotkey_source_mask_store(struct device *dev,
2747 struct device_attribute *attr,
2748 const char *buf, size_t count)
2754 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2755 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2758 if (mutex_lock_killable(&hotkey_mutex))
2759 return -ERESTARTSYS;
2761 HOTKEY_CONFIG_CRITICAL_START
2762 hotkey_source_mask = t;
2763 HOTKEY_CONFIG_CRITICAL_END
2765 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2766 ~hotkey_source_mask);
2767 hotkey_poll_setup(true);
2769 /* check if events needed by the driver got disabled */
2770 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2771 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2773 mutex_unlock(&hotkey_mutex);
2776 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2777 "firmware event mask!\n");
2780 printk(TPACPI_NOTICE "hotkey_source_mask: "
2781 "some important events were disabled: "
2784 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2786 return (rc < 0) ? rc : count;
2789 static struct device_attribute dev_attr_hotkey_source_mask =
2790 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2791 hotkey_source_mask_show, hotkey_source_mask_store);
2793 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2794 static ssize_t hotkey_poll_freq_show(struct device *dev,
2795 struct device_attribute *attr,
2798 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2801 static ssize_t hotkey_poll_freq_store(struct device *dev,
2802 struct device_attribute *attr,
2803 const char *buf, size_t count)
2807 if (parse_strtoul(buf, 25, &t))
2810 if (mutex_lock_killable(&hotkey_mutex))
2811 return -ERESTARTSYS;
2813 hotkey_poll_set_freq(t);
2814 hotkey_poll_setup(true);
2816 mutex_unlock(&hotkey_mutex);
2818 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2823 static struct device_attribute dev_attr_hotkey_poll_freq =
2824 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2825 hotkey_poll_freq_show, hotkey_poll_freq_store);
2827 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2829 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2830 static ssize_t hotkey_radio_sw_show(struct device *dev,
2831 struct device_attribute *attr,
2835 res = hotkey_get_wlsw();
2839 /* Opportunistic update */
2840 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2842 return snprintf(buf, PAGE_SIZE, "%d\n",
2843 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2846 static struct device_attribute dev_attr_hotkey_radio_sw =
2847 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2849 static void hotkey_radio_sw_notify_change(void)
2851 if (tp_features.hotkey_wlsw)
2852 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2856 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2857 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2858 struct device_attribute *attr,
2862 res = hotkey_get_tablet_mode(&s);
2866 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2869 static struct device_attribute dev_attr_hotkey_tablet_mode =
2870 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2872 static void hotkey_tablet_mode_notify_change(void)
2874 if (tp_features.hotkey_tablet)
2875 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2876 "hotkey_tablet_mode");
2879 /* sysfs hotkey report_mode -------------------------------------------- */
2880 static ssize_t hotkey_report_mode_show(struct device *dev,
2881 struct device_attribute *attr,
2884 return snprintf(buf, PAGE_SIZE, "%d\n",
2885 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2888 static struct device_attribute dev_attr_hotkey_report_mode =
2889 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2891 /* sysfs wakeup reason (pollable) -------------------------------------- */
2892 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2893 struct device_attribute *attr,
2896 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2899 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2900 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2902 static void hotkey_wakeup_reason_notify_change(void)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2908 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2909 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2910 struct device_attribute *attr,
2913 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2916 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2917 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2918 hotkey_wakeup_hotunplug_complete_show, NULL);
2920 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2922 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2923 "wakeup_hotunplug_complete");
2926 /* --------------------------------------------------------------------- */
2928 static struct attribute *hotkey_attributes[] __initdata = {
2929 &dev_attr_hotkey_enable.attr,
2930 &dev_attr_hotkey_bios_enabled.attr,
2931 &dev_attr_hotkey_bios_mask.attr,
2932 &dev_attr_hotkey_report_mode.attr,
2933 &dev_attr_hotkey_wakeup_reason.attr,
2934 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2935 &dev_attr_hotkey_mask.attr,
2936 &dev_attr_hotkey_all_mask.attr,
2937 &dev_attr_hotkey_recommended_mask.attr,
2938 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2939 &dev_attr_hotkey_source_mask.attr,
2940 &dev_attr_hotkey_poll_freq.attr,
2945 * Sync both the hw and sw blocking state of all switches
2947 static void tpacpi_send_radiosw_update(void)
2952 * We must sync all rfkill controllers *before* issuing any
2953 * rfkill input events, or we will race the rfkill core input
2956 * tpacpi_inputdev_send_mutex works as a syncronization point
2959 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2962 wlsw = hotkey_get_wlsw();
2964 /* Sync hw blocking state first if it is hw-blocked */
2965 if (wlsw == TPACPI_RFK_RADIO_OFF)
2966 tpacpi_rfk_update_hwblock_state(true);
2968 /* Sync sw blocking state */
2969 tpacpi_rfk_update_swstate_all();
2971 /* Sync hw blocking state last if it is hw-unblocked */
2972 if (wlsw == TPACPI_RFK_RADIO_ON)
2973 tpacpi_rfk_update_hwblock_state(false);
2975 /* Issue rfkill input event for WLSW switch */
2977 mutex_lock(&tpacpi_inputdev_send_mutex);
2979 input_report_switch(tpacpi_inputdev,
2980 SW_RFKILL_ALL, (wlsw > 0));
2981 input_sync(tpacpi_inputdev);
2983 mutex_unlock(&tpacpi_inputdev_send_mutex);
2987 * this can be unconditional, as we will poll state again
2988 * if userspace uses the notify to read data
2990 hotkey_radio_sw_notify_change();
2993 static void hotkey_exit(void)
2995 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2996 mutex_lock(&hotkey_mutex);
2997 hotkey_poll_stop_sync();
2998 mutex_unlock(&hotkey_mutex);
3001 if (hotkey_dev_attributes)
3002 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3004 kfree(hotkey_keycode_map);
3006 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3007 "restoring original HKEY status and mask\n");
3008 /* yes, there is a bitwise or below, we want the
3009 * functions to be called even if one of them fail */
3010 if (((tp_features.hotkey_mask &&
3011 hotkey_mask_set(hotkey_orig_mask)) |
3012 hotkey_status_set(false)) != 0)
3014 "failed to restore hot key mask "
3015 "to BIOS defaults\n");
3018 static void __init hotkey_unmap(const unsigned int scancode)
3020 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3021 clear_bit(hotkey_keycode_map[scancode],
3022 tpacpi_inputdev->keybit);
3023 hotkey_keycode_map[scancode] = KEY_RESERVED;
3029 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3032 #define TPACPI_HK_Q_INIMASK 0x0001
3034 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3035 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3036 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3037 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3038 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3039 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3040 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3041 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3042 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3043 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3044 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3045 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3046 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3047 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3048 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3049 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3050 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3051 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3052 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3053 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3056 static int __init hotkey_init(struct ibm_init_struct *iibm)
3058 /* Requirements for changing the default keymaps:
3060 * 1. Many of the keys are mapped to KEY_RESERVED for very
3061 * good reasons. Do not change them unless you have deep
3062 * knowledge on the IBM and Lenovo ThinkPad firmware for
3063 * the various ThinkPad models. The driver behaves
3064 * differently for KEY_RESERVED: such keys have their
3065 * hot key mask *unset* in mask_recommended, and also
3066 * in the initial hot key mask programmed into the
3067 * firmware at driver load time, which means the firm-
3068 * ware may react very differently if you change them to
3071 * 2. You must be subscribed to the linux-thinkpad and
3072 * ibm-acpi-devel mailing lists, and you should read the
3073 * list archives since 2007 if you want to change the
3074 * keymaps. This requirement exists so that you will
3075 * know the past history of problems with the thinkpad-
3076 * acpi driver keymaps, and also that you will be
3077 * listening to any bug reports;
3079 * 3. Do not send thinkpad-acpi specific patches directly to
3080 * for merging, *ever*. Send them to the linux-acpi
3081 * mailinglist for comments. Merging is to be done only
3082 * through acpi-test and the ACPI maintainer.
3084 * If the above is too much to ask, don't change the keymap.
3085 * Ask the thinkpad-acpi maintainer to do it, instead.
3087 static u16 ibm_keycode_map[] __initdata = {
3088 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3089 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3090 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3091 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3093 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3094 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3095 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3096 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3098 /* brightness: firmware always reacts to them */
3099 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3100 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3102 /* Thinklight: firmware always react to it */
3103 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3105 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3106 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3108 /* Volume: firmware always react to it and reprograms
3109 * the built-in *extra* mixer. Never map it to control
3110 * another mixer by default. */
3111 KEY_RESERVED, /* 0x14: VOLUME UP */
3112 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3113 KEY_RESERVED, /* 0x16: MUTE */
3115 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3117 /* (assignments unknown, please report if found) */
3118 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3119 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3121 static u16 lenovo_keycode_map[] __initdata = {
3122 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3123 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3124 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3125 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3127 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3128 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3129 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3130 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3132 /* These should be enabled --only-- when ACPI video
3133 * is disabled (i.e. in "vendor" mode), and are handled
3134 * in a special way by the init code */
3135 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3136 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3138 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3140 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3141 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3143 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3144 * react to it and reprograms the built-in *extra* mixer.
3145 * Never map it to control another mixer by default.
3147 * T60?, T61, R60?, R61: firmware and EC tries to send
3148 * these over the regular keyboard, so these are no-ops,
3149 * but there are still weird bugs re. MUTE, so do not
3150 * change unless you get test reports from all Lenovo
3151 * models. May cause the BIOS to interfere with the
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,
3165 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3166 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3167 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3172 bool radiosw_state = false;
3173 bool tabletsw_state = false;
3175 unsigned long quirks;
3177 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3178 "initializing hotkey subdriver\n");
3180 BUG_ON(!tpacpi_inputdev);
3181 BUG_ON(tpacpi_inputdev->open != NULL ||
3182 tpacpi_inputdev->close != NULL);
3184 TPACPI_ACPIHANDLE_INIT(hkey);
3185 mutex_init(&hotkey_mutex);
3187 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3188 mutex_init(&hotkey_thread_mutex);
3189 mutex_init(&hotkey_thread_data_mutex);
3192 /* hotkey not supported on 570 */
3193 tp_features.hotkey = hkey_handle != NULL;
3195 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3197 str_supported(tp_features.hotkey));
3199 if (!tp_features.hotkey)
3202 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3203 ARRAY_SIZE(tpacpi_hotkey_qtable));
3205 tpacpi_disable_brightness_delay();
3207 /* MUST have enough space for all attributes to be added to
3208 * hotkey_dev_attributes */
3209 hotkey_dev_attributes = create_attr_set(
3210 ARRAY_SIZE(hotkey_attributes) + 2,
3212 if (!hotkey_dev_attributes)
3214 res = add_many_to_attr_set(hotkey_dev_attributes,
3216 ARRAY_SIZE(hotkey_attributes));
3220 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3221 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3222 for HKEY interface version 0x100 */
3223 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3224 if ((hkeyv >> 8) != 1) {
3225 printk(TPACPI_ERR "unknown version of the "
3226 "HKEY interface: 0x%x\n", hkeyv);
3227 printk(TPACPI_ERR "please report this to %s\n",
3231 * MHKV 0x100 in A31, R40, R40e,
3232 * T4x, X31, and later
3234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3235 "firmware HKEY interface version: 0x%x\n",
3238 /* Paranoia check AND init hotkey_all_mask */
3239 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3242 "missing MHKA handler, "
3243 "please report this to %s\n",
3245 /* Fallback: pre-init for FN+F3,F4,F12 */
3246 hotkey_all_mask = 0x080cU;
3248 tp_features.hotkey_mask = 1;
3253 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3254 "hotkey masks are %s\n",
3255 str_supported(tp_features.hotkey_mask));
3257 /* Init hotkey_all_mask if not initialized yet */
3258 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3259 (quirks & TPACPI_HK_Q_INIMASK))
3260 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3262 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3263 if (tp_features.hotkey_mask) {
3264 /* hotkey_source_mask *must* be zero for
3265 * the first hotkey_mask_get to return hotkey_orig_mask */
3266 res = hotkey_mask_get();
3270 hotkey_orig_mask = hotkey_acpi_mask;
3272 hotkey_orig_mask = hotkey_all_mask;
3273 hotkey_acpi_mask = hotkey_all_mask;
3276 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3278 tp_features.hotkey_wlsw = 1;
3279 radiosw_state = !!tpacpi_wlsw_emulstate;
3281 "radio switch emulation enabled\n");
3284 /* Not all thinkpads have a hardware radio switch */
3285 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3286 tp_features.hotkey_wlsw = 1;
3287 radiosw_state = !!status;
3289 "radio switch found; radios are %s\n",
3290 enabled(status, 0));
3292 if (tp_features.hotkey_wlsw)
3293 res = add_to_attr_set(hotkey_dev_attributes,
3294 &dev_attr_hotkey_radio_sw.attr);
3296 /* For X41t, X60t, X61t Tablets... */
3297 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3298 tp_features.hotkey_tablet = 1;
3299 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3301 "possible tablet mode switch found; "
3302 "ThinkPad in %s mode\n",
3303 (tabletsw_state) ? "tablet" : "laptop");
3304 res = add_to_attr_set(hotkey_dev_attributes,
3305 &dev_attr_hotkey_tablet_mode.attr);
3309 res = register_attr_set_with_sysfs(
3310 hotkey_dev_attributes,
3311 &tpacpi_pdev->dev.kobj);
3315 /* Set up key map */
3317 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3319 if (!hotkey_keycode_map) {
3321 "failed to allocate memory for key map\n");
3326 if (tpacpi_is_lenovo()) {
3327 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3328 "using Lenovo default hot key map\n");
3329 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3330 TPACPI_HOTKEY_MAP_SIZE);
3332 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3333 "using IBM default hot key map\n");
3334 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3335 TPACPI_HOTKEY_MAP_SIZE);
3338 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3339 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3340 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3341 tpacpi_inputdev->keycode = hotkey_keycode_map;
3342 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3343 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3344 input_set_capability(tpacpi_inputdev, EV_KEY,
3345 hotkey_keycode_map[i]);
3347 if (i < sizeof(hotkey_reserved_mask)*8)
3348 hotkey_reserved_mask |= 1 << i;
3352 if (tp_features.hotkey_wlsw) {
3353 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3354 input_report_switch(tpacpi_inputdev,
3355 SW_RFKILL_ALL, radiosw_state);
3357 if (tp_features.hotkey_tablet) {
3358 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3359 input_report_switch(tpacpi_inputdev,
3360 SW_TABLET_MODE, tabletsw_state);
3363 /* Do not issue duplicate brightness change events to
3364 * userspace. tpacpi_detect_brightness_capabilities() must have
3365 * been called before this point */
3366 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3368 "This ThinkPad has standard ACPI backlight "
3369 "brightness control, supported by the ACPI "
3371 printk(TPACPI_NOTICE
3372 "Disabling thinkpad-acpi brightness events "
3375 /* Disable brightness up/down on Lenovo thinkpads when
3376 * ACPI is handling them, otherwise it is plain impossible
3377 * for userspace to do something even remotely sane */
3378 hotkey_reserved_mask |=
3379 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3380 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3381 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3382 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3385 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3386 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3388 & ~hotkey_reserved_mask;
3390 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3391 "hotkey source mask 0x%08x, polling freq %u\n",
3392 hotkey_source_mask, hotkey_poll_freq);
3395 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3396 "enabling firmware HKEY event interface...\n");
3397 res = hotkey_status_set(true);
3402 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3403 | hotkey_driver_mask)
3404 & ~hotkey_source_mask);
3405 if (res < 0 && res != -ENXIO) {
3409 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3410 & ~hotkey_reserved_mask;
3411 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3412 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3413 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3415 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3416 "legacy ibm/hotkey event reporting over procfs %s\n",
3417 (hotkey_report_mode < 2) ?
3418 "enabled" : "disabled");
3420 tpacpi_inputdev->open = &hotkey_inputdev_open;
3421 tpacpi_inputdev->close = &hotkey_inputdev_close;
3423 hotkey_poll_setup_safe(true);
3428 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3429 hotkey_dev_attributes = NULL;
3431 return (res < 0)? res : 1;
3434 static bool hotkey_notify_hotkey(const u32 hkey,
3436 bool *ignore_acpi_ev)
3438 /* 0x1000-0x1FFF: key presses */
3439 unsigned int scancode = hkey & 0xfff;
3440 *send_acpi_ev = true;
3441 *ignore_acpi_ev = false;
3443 if (scancode > 0 && scancode < 0x21) {
3445 if (!(hotkey_source_mask & (1 << scancode))) {
3446 tpacpi_input_send_key_masked(scancode);
3447 *send_acpi_ev = false;
3449 *ignore_acpi_ev = true;
3456 static bool hotkey_notify_wakeup(const u32 hkey,
3458 bool *ignore_acpi_ev)
3460 /* 0x2000-0x2FFF: Wakeup reason */
3461 *send_acpi_ev = true;
3462 *ignore_acpi_ev = false;
3465 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3466 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3467 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3468 *ignore_acpi_ev = true;
3471 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3472 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3473 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3474 *ignore_acpi_ev = true;
3477 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3478 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3480 "EMERGENCY WAKEUP: battery almost empty\n");
3481 /* how to auto-heal: */
3482 /* 2313: woke up from S3, go to S4/S5 */
3483 /* 2413: woke up from S4, go to S5 */
3490 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3492 "woke up due to a hot-unplug "
3494 hotkey_wakeup_reason_notify_change();
3499 static bool hotkey_notify_usrevent(const u32 hkey,
3501 bool *ignore_acpi_ev)
3503 /* 0x5000-0x5FFF: human interface helpers */
3504 *send_acpi_ev = true;
3505 *ignore_acpi_ev = false;
3508 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3509 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3512 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3513 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3514 tpacpi_input_send_tabletsw();
3515 hotkey_tablet_mode_notify_change();
3516 *send_acpi_ev = false;
3519 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3520 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3521 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3522 /* do not propagate these events */
3523 *ignore_acpi_ev = true;
3531 static void thermal_dump_all_sensors(void);
3533 static bool hotkey_notify_thermal(const u32 hkey,
3535 bool *ignore_acpi_ev)
3539 /* 0x6000-0x6FFF: thermal alarms */
3540 *send_acpi_ev = true;
3541 *ignore_acpi_ev = false;
3544 case TP_HKEY_EV_THM_TABLE_CHANGED:
3546 "EC reports that Thermal Table has changed\n");
3547 /* recommended action: do nothing, we don't have
3548 * Lenovo ATM information */
3550 case TP_HKEY_EV_ALARM_BAT_HOT:
3552 "THERMAL ALARM: battery is too hot!\n");
3553 /* recommended action: warn user through gui */
3555 case TP_HKEY_EV_ALARM_BAT_XHOT:
3557 "THERMAL EMERGENCY: battery is extremely hot!\n");
3558 /* recommended action: immediate sleep/hibernate */
3560 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3563 "a sensor reports something is too hot!\n");
3564 /* recommended action: warn user through gui, that */
3565 /* some internal component is too hot */
3567 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3569 "THERMAL EMERGENCY: "
3570 "a sensor reports something is extremely hot!\n");
3571 /* recommended action: immediate sleep/hibernate */
3575 "THERMAL ALERT: unknown thermal alarm received\n");
3579 thermal_dump_all_sensors();
3584 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3588 bool ignore_acpi_ev;
3591 if (event != 0x80) {
3593 "unknown HKEY notification event %d\n", event);
3594 /* forward it to userspace, maybe it knows how to handle it */
3595 acpi_bus_generate_netlink_event(
3596 ibm->acpi->device->pnp.device_class,
3597 dev_name(&ibm->acpi->device->dev),
3603 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3604 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3613 send_acpi_ev = true;
3614 ignore_acpi_ev = false;
3616 switch (hkey >> 12) {
3618 /* 0x1000-0x1FFF: key presses */
3619 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3623 /* 0x2000-0x2FFF: Wakeup reason */
3624 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3628 /* 0x3000-0x3FFF: bay-related wakeups */
3630 case TP_HKEY_EV_BAYEJ_ACK:
3631 hotkey_autosleep_ack = 1;
3634 hotkey_wakeup_hotunplug_complete_notify_change();
3637 case TP_HKEY_EV_OPTDRV_EJ:
3638 /* FIXME: kick libata if SATA link offline */
3646 /* 0x4000-0x4FFF: dock-related wakeups */
3647 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3648 hotkey_autosleep_ack = 1;
3651 hotkey_wakeup_hotunplug_complete_notify_change();
3658 /* 0x5000-0x5FFF: human interface helpers */
3659 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3663 /* 0x6000-0x6FFF: thermal alarms */
3664 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3668 /* 0x7000-0x7FFF: misc */
3669 if (tp_features.hotkey_wlsw &&
3670 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3671 tpacpi_send_radiosw_update();
3676 /* fallthrough to default */
3681 printk(TPACPI_NOTICE
3682 "unhandled HKEY event 0x%04x\n", hkey);
3683 printk(TPACPI_NOTICE
3684 "please report the conditions when this "
3685 "event happened to %s\n", TPACPI_MAIL);
3689 if (!ignore_acpi_ev &&
3690 (send_acpi_ev || hotkey_report_mode < 2)) {
3691 acpi_bus_generate_proc_event(ibm->acpi->device,
3695 /* netlink events */
3696 if (!ignore_acpi_ev && send_acpi_ev) {
3697 acpi_bus_generate_netlink_event(
3698 ibm->acpi->device->pnp.device_class,
3699 dev_name(&ibm->acpi->device->dev),
3705 static void hotkey_suspend(pm_message_t state)
3707 /* Do these on suspend, we get the events on early resume! */
3708 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3709 hotkey_autosleep_ack = 0;
3712 static void hotkey_resume(void)
3714 tpacpi_disable_brightness_delay();
3716 if (hotkey_status_set(true) < 0 ||
3717 hotkey_mask_set(hotkey_acpi_mask) < 0)
3719 "error while attempting to reset the event "
3720 "firmware interface\n");
3722 tpacpi_send_radiosw_update();
3723 hotkey_tablet_mode_notify_change();
3724 hotkey_wakeup_reason_notify_change();
3725 hotkey_wakeup_hotunplug_complete_notify_change();
3726 hotkey_poll_setup_safe(false);
3729 /* procfs -------------------------------------------------------------- */
3730 static int hotkey_read(struct seq_file *m)
3734 if (!tp_features.hotkey) {
3735 seq_printf(m, "status:\t\tnot supported\n");
3739 if (mutex_lock_killable(&hotkey_mutex))
3740 return -ERESTARTSYS;
3741 res = hotkey_status_get(&status);
3743 res = hotkey_mask_get();
3744 mutex_unlock(&hotkey_mutex);
3748 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3749 if (hotkey_all_mask) {
3750 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3751 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3753 seq_printf(m, "mask:\t\tnot supported\n");
3754 seq_printf(m, "commands:\tenable, disable, reset\n");
3760 static void hotkey_enabledisable_warn(bool enable)
3762 tpacpi_log_usertask("procfs hotkey enable/disable");
3763 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3765 "hotkey enable/disable functionality has been "
3766 "removed from the driver. Hotkeys are always "
3769 "Please remove the hotkey=enable module "
3770 "parameter, it is deprecated. Hotkeys are always "
3774 static int hotkey_write(char *buf)
3780 if (!tp_features.hotkey)
3783 if (mutex_lock_killable(&hotkey_mutex))
3784 return -ERESTARTSYS;
3786 mask = hotkey_user_mask;
3789 while ((cmd = next_cmd(&buf))) {
3790 if (strlencmp(cmd, "enable") == 0) {
3791 hotkey_enabledisable_warn(1);
3792 } else if (strlencmp(cmd, "disable") == 0) {
3793 hotkey_enabledisable_warn(0);
3795 } else if (strlencmp(cmd, "reset") == 0) {
3796 mask = (hotkey_all_mask | hotkey_source_mask)
3797 & ~hotkey_reserved_mask;
3798 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3800 } else if (sscanf(cmd, "%x", &mask) == 1) {
3809 tpacpi_disclose_usertask("procfs hotkey",
3810 "set mask to 0x%08x\n", mask);
3811 res = hotkey_user_mask_set(mask);
3815 mutex_unlock(&hotkey_mutex);
3819 static const struct acpi_device_id ibm_htk_device_ids[] = {
3820 {TPACPI_ACPI_HKEY_HID, 0},
3824 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3825 .hid = ibm_htk_device_ids,
3826 .notify = hotkey_notify,
3827 .handle = &hkey_handle,
3828 .type = ACPI_DEVICE_NOTIFY,
3831 static struct ibm_struct hotkey_driver_data = {
3833 .read = hotkey_read,
3834 .write = hotkey_write,
3835 .exit = hotkey_exit,
3836 .resume = hotkey_resume,
3837 .suspend = hotkey_suspend,
3838 .acpi = &ibm_hotkey_acpidriver,
3841 /*************************************************************************
3842 * Bluetooth subdriver
3846 /* ACPI GBDC/SBDC bits */
3847 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3848 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3849 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3850 0 = disable, 1 = enable */
3854 /* ACPI \BLTH commands */
3855 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3856 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3857 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3858 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3859 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3862 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3864 static int bluetooth_get_status(void)
3868 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3869 if (dbg_bluetoothemul)
3870 return (tpacpi_bluetooth_emulstate) ?
3871 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3874 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3877 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3878 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3881 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3885 vdbg_printk(TPACPI_DBG_RFKILL,
3886 "will attempt to %s bluetooth\n",
3887 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3889 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3890 if (dbg_bluetoothemul) {
3891 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3896 if (state == TPACPI_RFK_RADIO_ON)
3897 status = TP_ACPI_BLUETOOTH_RADIOSSW
3898 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3902 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3908 /* sysfs bluetooth enable ---------------------------------------------- */
3909 static ssize_t bluetooth_enable_show(struct device *dev,
3910 struct device_attribute *attr,
3913 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3917 static ssize_t bluetooth_enable_store(struct device *dev,
3918 struct device_attribute *attr,
3919 const char *buf, size_t count)
3921 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3925 static struct device_attribute dev_attr_bluetooth_enable =
3926 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3927 bluetooth_enable_show, bluetooth_enable_store);
3929 /* --------------------------------------------------------------------- */
3931 static struct attribute *bluetooth_attributes[] = {
3932 &dev_attr_bluetooth_enable.attr,
3936 static const struct attribute_group bluetooth_attr_group = {
3937 .attrs = bluetooth_attributes,
3940 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3941 .get_status = bluetooth_get_status,
3942 .set_status = bluetooth_set_status,
3945 static void bluetooth_shutdown(void)
3947 /* Order firmware to save current state to NVRAM */
3948 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3949 TP_ACPI_BLTH_SAVE_STATE))
3950 printk(TPACPI_NOTICE
3951 "failed to save bluetooth state to NVRAM\n");
3953 vdbg_printk(TPACPI_DBG_RFKILL,
3954 "bluestooth state saved to NVRAM\n");
3957 static void bluetooth_exit(void)
3959 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3960 &bluetooth_attr_group);
3962 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3964 bluetooth_shutdown();
3967 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3972 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3973 "initializing bluetooth subdriver\n");
3975 TPACPI_ACPIHANDLE_INIT(hkey);
3977 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3978 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3979 tp_features.bluetooth = hkey_handle &&
3980 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3982 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3983 "bluetooth is %s, status 0x%02x\n",
3984 str_supported(tp_features.bluetooth),
3987 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3988 if (dbg_bluetoothemul) {
3989 tp_features.bluetooth = 1;
3991 "bluetooth switch emulation enabled\n");
3994 if (tp_features.bluetooth &&
3995 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3996 /* no bluetooth hardware present in system */
3997 tp_features.bluetooth = 0;
3998 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3999 "bluetooth hardware not installed\n");
4002 if (!tp_features.bluetooth)
4005 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4006 &bluetooth_tprfk_ops,
4007 RFKILL_TYPE_BLUETOOTH,
4008 TPACPI_RFK_BLUETOOTH_SW_NAME,
4013 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4014 &bluetooth_attr_group);
4016 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4023 /* procfs -------------------------------------------------------------- */
4024 static int bluetooth_read(struct seq_file *m)
4026 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4029 static int bluetooth_write(char *buf)
4031 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4034 static struct ibm_struct bluetooth_driver_data = {
4035 .name = "bluetooth",
4036 .read = bluetooth_read,
4037 .write = bluetooth_write,
4038 .exit = bluetooth_exit,
4039 .shutdown = bluetooth_shutdown,
4042 /*************************************************************************
4047 /* ACPI GWAN/SWAN bits */
4048 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4049 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4050 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4051 0 = disable, 1 = enable */
4054 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4056 static int wan_get_status(void)
4060 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4062 return (tpacpi_wwan_emulstate) ?
4063 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4066 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4069 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4070 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4073 static int wan_set_status(enum tpacpi_rfkill_state state)
4077 vdbg_printk(TPACPI_DBG_RFKILL,
4078 "will attempt to %s wwan\n",
4079 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4083 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4088 if (state == TPACPI_RFK_RADIO_ON)
4089 status = TP_ACPI_WANCARD_RADIOSSW
4090 | TP_ACPI_WANCARD_RESUMECTRL;
4094 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4100 /* sysfs wan enable ---------------------------------------------------- */
4101 static ssize_t wan_enable_show(struct device *dev,
4102 struct device_attribute *attr,
4105 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4109 static ssize_t wan_enable_store(struct device *dev,
4110 struct device_attribute *attr,
4111 const char *buf, size_t count)
4113 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4117 static struct device_attribute dev_attr_wan_enable =
4118 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4119 wan_enable_show, wan_enable_store);
4121 /* --------------------------------------------------------------------- */
4123 static struct attribute *wan_attributes[] = {
4124 &dev_attr_wan_enable.attr,
4128 static const struct attribute_group wan_attr_group = {
4129 .attrs = wan_attributes,
4132 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4133 .get_status = wan_get_status,
4134 .set_status = wan_set_status,
4137 static void wan_shutdown(void)
4139 /* Order firmware to save current state to NVRAM */
4140 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4141 TP_ACPI_WGSV_SAVE_STATE))
4142 printk(TPACPI_NOTICE
4143 "failed to save WWAN state to NVRAM\n");
4145 vdbg_printk(TPACPI_DBG_RFKILL,
4146 "WWAN state saved to NVRAM\n");
4149 static void wan_exit(void)
4151 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4154 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4159 static int __init wan_init(struct ibm_init_struct *iibm)
4164 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4165 "initializing wan subdriver\n");
4167 TPACPI_ACPIHANDLE_INIT(hkey);
4169 tp_features.wan = hkey_handle &&
4170 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4172 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4173 "wan is %s, status 0x%02x\n",
4174 str_supported(tp_features.wan),
4177 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4179 tp_features.wan = 1;
4181 "wwan switch emulation enabled\n");
4184 if (tp_features.wan &&
4185 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4186 /* no wan hardware present in system */
4187 tp_features.wan = 0;
4188 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4189 "wan hardware not installed\n");
4192 if (!tp_features.wan)
4195 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4198 TPACPI_RFK_WWAN_SW_NAME,
4203 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4207 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4214 /* procfs -------------------------------------------------------------- */
4215 static int wan_read(struct seq_file *m)
4217 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4220 static int wan_write(char *buf)
4222 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4225 static struct ibm_struct wan_driver_data = {
4230 .shutdown = wan_shutdown,
4233 /*************************************************************************
4238 /* ACPI GUWB/SUWB bits */
4239 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4240 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4243 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4245 static int uwb_get_status(void)
4249 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4251 return (tpacpi_uwb_emulstate) ?
4252 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4255 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4258 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4259 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4262 static int uwb_set_status(enum tpacpi_rfkill_state state)
4266 vdbg_printk(TPACPI_DBG_RFKILL,
4267 "will attempt to %s UWB\n",
4268 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4270 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4272 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4277 if (state == TPACPI_RFK_RADIO_ON)
4278 status = TP_ACPI_UWB_RADIOSSW;
4282 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4288 /* --------------------------------------------------------------------- */
4290 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4291 .get_status = uwb_get_status,
4292 .set_status = uwb_set_status,
4295 static void uwb_exit(void)
4297 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4300 static int __init uwb_init(struct ibm_init_struct *iibm)
4305 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4306 "initializing uwb subdriver\n");
4308 TPACPI_ACPIHANDLE_INIT(hkey);
4310 tp_features.uwb = hkey_handle &&
4311 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4314 "uwb is %s, status 0x%02x\n",
4315 str_supported(tp_features.uwb),
4318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4320 tp_features.uwb = 1;
4322 "uwb switch emulation enabled\n");
4325 if (tp_features.uwb &&
4326 !(status & TP_ACPI_UWB_HWPRESENT)) {
4327 /* no uwb hardware present in system */
4328 tp_features.uwb = 0;
4329 dbg_printk(TPACPI_DBG_INIT,
4330 "uwb hardware not installed\n");
4333 if (!tp_features.uwb)
4336 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4339 TPACPI_RFK_UWB_SW_NAME,
4344 static struct ibm_struct uwb_driver_data = {
4347 .flags.experimental = 1,
4350 /*************************************************************************
4354 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4356 enum video_access_mode {
4357 TPACPI_VIDEO_NONE = 0,
4358 TPACPI_VIDEO_570, /* 570 */
4359 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4360 TPACPI_VIDEO_NEW, /* all others */
4363 enum { /* video status flags, based on VIDEO_570 */
4364 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4365 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4366 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4369 enum { /* TPACPI_VIDEO_570 constants */
4370 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4371 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4372 * video_status_flags */
4373 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4374 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4377 static enum video_access_mode video_supported;
4378 static int video_orig_autosw;
4380 static int video_autosw_get(void);
4381 static int video_autosw_set(int enable);
4383 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4385 static int __init video_init(struct ibm_init_struct *iibm)
4389 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4391 TPACPI_ACPIHANDLE_INIT(vid);
4392 if (tpacpi_is_ibm())
4393 TPACPI_ACPIHANDLE_INIT(vid2);
4395 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4396 /* G41, assume IVGA doesn't change */
4397 vid_handle = vid2_handle;
4400 /* video switching not supported on R30, R31 */
4401 video_supported = TPACPI_VIDEO_NONE;
4402 else if (tpacpi_is_ibm() &&
4403 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4405 video_supported = TPACPI_VIDEO_570;
4406 else if (tpacpi_is_ibm() &&
4407 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4408 /* 600e/x, 770e, 770x */
4409 video_supported = TPACPI_VIDEO_770;
4412 video_supported = TPACPI_VIDEO_NEW;
4414 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4415 str_supported(video_supported != TPACPI_VIDEO_NONE),
4418 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4421 static void video_exit(void)
4423 dbg_printk(TPACPI_DBG_EXIT,
4424 "restoring original video autoswitch mode\n");
4425 if (video_autosw_set(video_orig_autosw))
4426 printk(TPACPI_ERR "error while trying to restore original "
4427 "video autoswitch mode\n");
4430 static int video_outputsw_get(void)
4435 switch (video_supported) {
4436 case TPACPI_VIDEO_570:
4437 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4438 TP_ACPI_VIDEO_570_PHSCMD))
4440 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4442 case TPACPI_VIDEO_770:
4443 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4446 status |= TP_ACPI_VIDEO_S_LCD;
4447 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4450 status |= TP_ACPI_VIDEO_S_CRT;
4452 case TPACPI_VIDEO_NEW:
4453 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4454 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4457 status |= TP_ACPI_VIDEO_S_CRT;
4459 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4460 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4463 status |= TP_ACPI_VIDEO_S_LCD;
4464 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4467 status |= TP_ACPI_VIDEO_S_DVI;
4476 static int video_outputsw_set(int status)
4481 switch (video_supported) {
4482 case TPACPI_VIDEO_570:
4483 res = acpi_evalf(NULL, NULL,
4484 "\\_SB.PHS2", "vdd",
4485 TP_ACPI_VIDEO_570_PHS2CMD,
4486 status | TP_ACPI_VIDEO_570_PHS2SET);
4488 case TPACPI_VIDEO_770:
4489 autosw = video_autosw_get();
4493 res = video_autosw_set(1);
4496 res = acpi_evalf(vid_handle, NULL,
4497 "ASWT", "vdd", status * 0x100, 0);
4498 if (!autosw && video_autosw_set(autosw)) {
4500 "video auto-switch left enabled due to error\n");
4504 case TPACPI_VIDEO_NEW:
4505 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4506 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4512 return (res)? 0 : -EIO;
4515 static int video_autosw_get(void)
4519 switch (video_supported) {
4520 case TPACPI_VIDEO_570:
4521 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4524 case TPACPI_VIDEO_770:
4525 case TPACPI_VIDEO_NEW:
4526 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4536 static int video_autosw_set(int enable)
4538 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4543 static int video_outputsw_cycle(void)
4545 int autosw = video_autosw_get();
4551 switch (video_supported) {
4552 case TPACPI_VIDEO_570:
4553 res = video_autosw_set(1);
4556 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4558 case TPACPI_VIDEO_770:
4559 case TPACPI_VIDEO_NEW:
4560 res = video_autosw_set(1);
4563 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4568 if (!autosw && video_autosw_set(autosw)) {
4570 "video auto-switch left enabled due to error\n");
4574 return (res)? 0 : -EIO;
4577 static int video_expand_toggle(void)
4579 switch (video_supported) {
4580 case TPACPI_VIDEO_570:
4581 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4583 case TPACPI_VIDEO_770:
4584 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4586 case TPACPI_VIDEO_NEW:
4587 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4595 static int video_read(struct seq_file *m)
4599 if (video_supported == TPACPI_VIDEO_NONE) {
4600 seq_printf(m, "status:\t\tnot supported\n");
4604 /* Even reads can crash X.org, so... */
4605 if (!capable(CAP_SYS_ADMIN))
4608 status = video_outputsw_get();
4612 autosw = video_autosw_get();
4616 seq_printf(m, "status:\t\tsupported\n");
4617 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4618 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4619 if (video_supported == TPACPI_VIDEO_NEW)
4620 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4621 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4622 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4623 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4624 if (video_supported == TPACPI_VIDEO_NEW)
4625 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4626 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4627 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4632 static int video_write(char *buf)
4635 int enable, disable, status;
4638 if (video_supported == TPACPI_VIDEO_NONE)
4641 /* Even reads can crash X.org, let alone writes... */
4642 if (!capable(CAP_SYS_ADMIN))
4648 while ((cmd = next_cmd(&buf))) {
4649 if (strlencmp(cmd, "lcd_enable") == 0) {
4650 enable |= TP_ACPI_VIDEO_S_LCD;
4651 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4652 disable |= TP_ACPI_VIDEO_S_LCD;
4653 } else if (strlencmp(cmd, "crt_enable") == 0) {
4654 enable |= TP_ACPI_VIDEO_S_CRT;
4655 } else if (strlencmp(cmd, "crt_disable") == 0) {
4656 disable |= TP_ACPI_VIDEO_S_CRT;
4657 } else if (video_supported == TPACPI_VIDEO_NEW &&
4658 strlencmp(cmd, "dvi_enable") == 0) {
4659 enable |= TP_ACPI_VIDEO_S_DVI;
4660 } else if (video_supported == TPACPI_VIDEO_NEW &&
4661 strlencmp(cmd, "dvi_disable") == 0) {
4662 disable |= TP_ACPI_VIDEO_S_DVI;
4663 } else if (strlencmp(cmd, "auto_enable") == 0) {
4664 res = video_autosw_set(1);
4667 } else if (strlencmp(cmd, "auto_disable") == 0) {
4668 res = video_autosw_set(0);
4671 } else if (strlencmp(cmd, "video_switch") == 0) {
4672 res = video_outputsw_cycle();
4675 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4676 res = video_expand_toggle();
4683 if (enable || disable) {
4684 status = video_outputsw_get();
4687 res = video_outputsw_set((status & ~disable) | enable);
4695 static struct ibm_struct video_driver_data = {
4698 .write = video_write,
4702 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4704 /*************************************************************************
4705 * Light (thinklight) subdriver
4708 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4709 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4711 static int light_get_status(void)
4715 if (tp_features.light_status) {
4716 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4724 static int light_set_status(int status)
4728 if (tp_features.light) {
4730 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4732 TP_CMOS_THINKLIGHT_ON :
4733 TP_CMOS_THINKLIGHT_OFF);
4735 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4738 return (rc)? 0 : -EIO;
4744 static void light_set_status_worker(struct work_struct *work)
4746 struct tpacpi_led_classdev *data =
4747 container_of(work, struct tpacpi_led_classdev, work);
4749 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4750 light_set_status((data->new_state != TPACPI_LED_OFF));
4753 static void light_sysfs_set(struct led_classdev *led_cdev,
4754 enum led_brightness brightness)
4756 struct tpacpi_led_classdev *data =
4757 container_of(led_cdev,
4758 struct tpacpi_led_classdev,
4760 data->new_state = (brightness != LED_OFF) ?
4761 TPACPI_LED_ON : TPACPI_LED_OFF;
4762 queue_work(tpacpi_wq, &data->work);
4765 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4767 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4770 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4772 .name = "tpacpi::thinklight",
4773 .brightness_set = &light_sysfs_set,
4774 .brightness_get = &light_sysfs_get,
4778 static int __init light_init(struct ibm_init_struct *iibm)
4782 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4784 if (tpacpi_is_ibm()) {
4785 TPACPI_ACPIHANDLE_INIT(ledb);
4786 TPACPI_ACPIHANDLE_INIT(lght);
4788 TPACPI_ACPIHANDLE_INIT(cmos);
4789 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4791 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4792 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4794 if (tp_features.light)
4795 /* light status not supported on
4796 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4797 tp_features.light_status =
4798 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4800 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4801 str_supported(tp_features.light),
4802 str_supported(tp_features.light_status));
4804 if (!tp_features.light)
4807 rc = led_classdev_register(&tpacpi_pdev->dev,
4808 &tpacpi_led_thinklight.led_classdev);
4811 tp_features.light = 0;
4812 tp_features.light_status = 0;
4820 static void light_exit(void)
4822 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4823 if (work_pending(&tpacpi_led_thinklight.work))
4824 flush_workqueue(tpacpi_wq);
4827 static int light_read(struct seq_file *m)
4831 if (!tp_features.light) {
4832 seq_printf(m, "status:\t\tnot supported\n");
4833 } else if (!tp_features.light_status) {
4834 seq_printf(m, "status:\t\tunknown\n");
4835 seq_printf(m, "commands:\ton, off\n");
4837 status = light_get_status();
4840 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4841 seq_printf(m, "commands:\ton, off\n");
4847 static int light_write(char *buf)
4852 if (!tp_features.light)
4855 while ((cmd = next_cmd(&buf))) {
4856 if (strlencmp(cmd, "on") == 0) {
4858 } else if (strlencmp(cmd, "off") == 0) {
4864 return light_set_status(newstatus);
4867 static struct ibm_struct light_driver_data = {
4870 .write = light_write,
4874 /*************************************************************************
4878 /* sysfs cmos_command -------------------------------------------------- */
4879 static ssize_t cmos_command_store(struct device *dev,
4880 struct device_attribute *attr,
4881 const char *buf, size_t count)
4883 unsigned long cmos_cmd;
4886 if (parse_strtoul(buf, 21, &cmos_cmd))
4889 res = issue_thinkpad_cmos_command(cmos_cmd);
4890 return (res)? res : count;
4893 static struct device_attribute dev_attr_cmos_command =
4894 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4896 /* --------------------------------------------------------------------- */
4898 static int __init cmos_init(struct ibm_init_struct *iibm)
4902 vdbg_printk(TPACPI_DBG_INIT,
4903 "initializing cmos commands subdriver\n");
4905 TPACPI_ACPIHANDLE_INIT(cmos);
4907 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4908 str_supported(cmos_handle != NULL));
4910 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4914 return (cmos_handle)? 0 : 1;
4917 static void cmos_exit(void)
4919 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4922 static int cmos_read(struct seq_file *m)
4924 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4925 R30, R31, T20-22, X20-21 */
4927 seq_printf(m, "status:\t\tnot supported\n");
4929 seq_printf(m, "status:\t\tsupported\n");
4930 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4936 static int cmos_write(char *buf)
4941 while ((cmd = next_cmd(&buf))) {
4942 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4943 cmos_cmd >= 0 && cmos_cmd <= 21) {
4948 res = issue_thinkpad_cmos_command(cmos_cmd);
4956 static struct ibm_struct cmos_driver_data = {
4959 .write = cmos_write,
4963 /*************************************************************************
4967 enum led_access_mode {
4968 TPACPI_LED_NONE = 0,
4969 TPACPI_LED_570, /* 570 */
4970 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4971 TPACPI_LED_NEW, /* all others */
4974 enum { /* For TPACPI_LED_OLD */
4975 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4976 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4977 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4980 static enum led_access_mode led_supported;
4982 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4983 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4984 /* T20-22, X20-21 */
4985 "LED", /* all others */
4988 #define TPACPI_LED_NUMLEDS 16
4989 static struct tpacpi_led_classdev *tpacpi_leds;
4990 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4991 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4992 /* there's a limit of 19 chars + NULL before 2.6.26 */
4994 "tpacpi:orange:batt",
4995 "tpacpi:green:batt",
4996 "tpacpi::dock_active",
4997 "tpacpi::bay_active",
4998 "tpacpi::dock_batt",
4999 "tpacpi::unknown_led",
5001 "tpacpi::dock_status1",
5002 "tpacpi::dock_status2",
5003 "tpacpi::unknown_led2",
5004 "tpacpi::unknown_led3",
5005 "tpacpi::thinkvantage",
5007 #define TPACPI_SAFE_LEDS 0x1081U
5009 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5011 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5014 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5018 static int led_get_status(const unsigned int led)
5021 enum led_status_t led_s;
5023 switch (led_supported) {
5024 case TPACPI_LED_570:
5025 if (!acpi_evalf(ec_handle,
5026 &status, "GLED", "dd", 1 << led))
5028 led_s = (status == 0)?
5033 tpacpi_led_state_cache[led] = led_s;
5042 static int led_set_status(const unsigned int led,
5043 const enum led_status_t ledstatus)
5045 /* off, on, blink. Index is led_status_t */
5046 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5047 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5051 switch (led_supported) {
5052 case TPACPI_LED_570:
5054 if (unlikely(led > 7))
5056 if (unlikely(tpacpi_is_led_restricted(led)))
5058 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5059 (1 << led), led_sled_arg1[ledstatus]))
5062 case TPACPI_LED_OLD:
5063 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5064 if (unlikely(led > 7))
5066 if (unlikely(tpacpi_is_led_restricted(led)))
5068 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5070 rc = ec_write(TPACPI_LED_EC_HLBL,
5071 (ledstatus == TPACPI_LED_BLINK) << led);
5073 rc = ec_write(TPACPI_LED_EC_HLCL,
5074 (ledstatus != TPACPI_LED_OFF) << led);
5076 case TPACPI_LED_NEW:
5078 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5080 if (unlikely(tpacpi_is_led_restricted(led)))
5082 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5083 led, led_led_arg1[ledstatus]))
5091 tpacpi_led_state_cache[led] = ledstatus;
5096 static void led_set_status_worker(struct work_struct *work)
5098 struct tpacpi_led_classdev *data =
5099 container_of(work, struct tpacpi_led_classdev, work);
5101 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5102 led_set_status(data->led, data->new_state);
5105 static void led_sysfs_set(struct led_classdev *led_cdev,
5106 enum led_brightness brightness)
5108 struct tpacpi_led_classdev *data = container_of(led_cdev,
5109 struct tpacpi_led_classdev, led_classdev);
5111 if (brightness == LED_OFF)
5112 data->new_state = TPACPI_LED_OFF;
5113 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5114 data->new_state = TPACPI_LED_ON;
5116 data->new_state = TPACPI_LED_BLINK;
5118 queue_work(tpacpi_wq, &data->work);
5121 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5122 unsigned long *delay_on, unsigned long *delay_off)
5124 struct tpacpi_led_classdev *data = container_of(led_cdev,
5125 struct tpacpi_led_classdev, led_classdev);
5127 /* Can we choose the flash rate? */
5128 if (*delay_on == 0 && *delay_off == 0) {
5129 /* yes. set them to the hardware blink rate (1 Hz) */
5130 *delay_on = 500; /* ms */
5131 *delay_off = 500; /* ms */
5132 } else if ((*delay_on != 500) || (*delay_off != 500))
5135 data->new_state = TPACPI_LED_BLINK;
5136 queue_work(tpacpi_wq, &data->work);
5141 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5145 struct tpacpi_led_classdev *data = container_of(led_cdev,
5146 struct tpacpi_led_classdev, led_classdev);
5148 rc = led_get_status(data->led);
5150 if (rc == TPACPI_LED_OFF || rc < 0)
5151 rc = LED_OFF; /* no error handling in led class :( */
5158 static void led_exit(void)
5162 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5163 if (tpacpi_leds[i].led_classdev.name)
5164 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5170 static int __init tpacpi_init_led(unsigned int led)
5174 tpacpi_leds[led].led = led;
5176 /* LEDs with no name don't get registered */
5177 if (!tpacpi_led_names[led])
5180 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5181 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5182 if (led_supported == TPACPI_LED_570)
5183 tpacpi_leds[led].led_classdev.brightness_get =
5186 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5188 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5190 rc = led_classdev_register(&tpacpi_pdev->dev,
5191 &tpacpi_leds[led].led_classdev);
5193 tpacpi_leds[led].led_classdev.name = NULL;
5198 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5199 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5200 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5201 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5203 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5204 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5205 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5206 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5207 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5208 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5209 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5210 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5212 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5213 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5214 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5215 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5216 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5218 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5219 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5220 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5221 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5223 /* (1) - may have excess leds enabled on MSB */
5225 /* Defaults (order matters, keep last, don't reorder!) */
5227 .vendor = PCI_VENDOR_ID_LENOVO,
5228 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5231 { /* IBM ThinkPads with no EC version string */
5232 .vendor = PCI_VENDOR_ID_IBM,
5233 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5236 { /* IBM ThinkPads with EC version string */
5237 .vendor = PCI_VENDOR_ID_IBM,
5238 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5243 #undef TPACPI_LEDQ_IBM
5244 #undef TPACPI_LEDQ_LNV
5246 static int __init led_init(struct ibm_init_struct *iibm)
5250 unsigned long useful_leds;
5252 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5254 TPACPI_ACPIHANDLE_INIT(led);
5257 /* led not supported on R30, R31 */
5258 led_supported = TPACPI_LED_NONE;
5259 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5261 led_supported = TPACPI_LED_570;
5262 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5263 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5264 led_supported = TPACPI_LED_OLD;
5267 led_supported = TPACPI_LED_NEW;
5269 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5270 str_supported(led_supported), led_supported);
5272 if (led_supported == TPACPI_LED_NONE)
5275 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5278 printk(TPACPI_ERR "Out of memory for LED data\n");
5282 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5283 ARRAY_SIZE(led_useful_qtable));
5285 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5286 if (!tpacpi_is_led_restricted(i) &&
5287 test_bit(i, &useful_leds)) {
5288 rc = tpacpi_init_led(i);
5296 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5297 printk(TPACPI_NOTICE
5298 "warning: userspace override of important "
5299 "firmware LEDs is enabled\n");
5304 #define str_led_status(s) \
5305 ((s) == TPACPI_LED_OFF ? "off" : \
5306 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5308 static int led_read(struct seq_file *m)
5310 if (!led_supported) {
5311 seq_printf(m, "status:\t\tnot supported\n");
5314 seq_printf(m, "status:\t\tsupported\n");
5316 if (led_supported == TPACPI_LED_570) {
5319 for (i = 0; i < 8; i++) {
5320 status = led_get_status(i);
5323 seq_printf(m, "%d:\t\t%s\n",
5324 i, str_led_status(status));
5328 seq_printf(m, "commands:\t"
5329 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5334 static int led_write(char *buf)
5338 enum led_status_t s;
5343 while ((cmd = next_cmd(&buf))) {
5344 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5347 if (strstr(cmd, "off")) {
5349 } else if (strstr(cmd, "on")) {
5351 } else if (strstr(cmd, "blink")) {
5352 s = TPACPI_LED_BLINK;
5357 rc = led_set_status(led, s);
5365 static struct ibm_struct led_driver_data = {
5372 /*************************************************************************
5376 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5378 #define TPACPI_BEEP_Q1 0x0001
5380 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5381 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5382 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5385 static int __init beep_init(struct ibm_init_struct *iibm)
5387 unsigned long quirks;
5389 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5391 TPACPI_ACPIHANDLE_INIT(beep);
5393 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5394 str_supported(beep_handle != NULL));
5396 quirks = tpacpi_check_quirks(beep_quirk_table,
5397 ARRAY_SIZE(beep_quirk_table));
5399 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5401 return (beep_handle)? 0 : 1;
5404 static int beep_read(struct seq_file *m)
5407 seq_printf(m, "status:\t\tnot supported\n");
5409 seq_printf(m, "status:\t\tsupported\n");
5410 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5416 static int beep_write(char *buf)
5424 while ((cmd = next_cmd(&buf))) {
5425 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5426 beep_cmd >= 0 && beep_cmd <= 17) {
5430 if (tp_features.beep_needs_two_args) {
5431 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5435 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5444 static struct ibm_struct beep_driver_data = {
5447 .write = beep_write,
5450 /*************************************************************************
5454 enum thermal_access_mode {
5455 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5456 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5457 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5458 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5459 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5462 enum { /* TPACPI_THERMAL_TPEC_* */
5463 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5464 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5465 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5467 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5471 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5472 struct ibm_thermal_sensors_struct {
5473 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5476 static enum thermal_access_mode thermal_read_mode;
5478 /* idx is zero-based */
5479 static int thermal_get_sensor(int idx, s32 *value)
5485 t = TP_EC_THERMAL_TMP0;
5487 switch (thermal_read_mode) {
5488 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5489 case TPACPI_THERMAL_TPEC_16:
5490 if (idx >= 8 && idx <= 15) {
5491 t = TP_EC_THERMAL_TMP8;
5496 case TPACPI_THERMAL_TPEC_8:
5498 if (!acpi_ec_read(t + idx, &tmp))
5500 *value = tmp * 1000;
5505 case TPACPI_THERMAL_ACPI_UPDT:
5507 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5508 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5510 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5512 *value = (t - 2732) * 100;
5517 case TPACPI_THERMAL_ACPI_TMP07:
5519 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5520 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5522 if (t > 127 || t < -127)
5523 t = TP_EC_THERMAL_TMP_NA;
5529 case TPACPI_THERMAL_NONE:
5537 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5548 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5551 for (i = 0 ; i < n; i++) {
5552 res = thermal_get_sensor(i, &s->temp[i]);
5560 static void thermal_dump_all_sensors(void)
5563 struct ibm_thermal_sensors_struct t;
5565 n = thermal_get_sensors(&t);
5569 printk(TPACPI_NOTICE
5570 "temperatures (Celsius):");
5572 for (i = 0; i < n; i++) {
5573 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5574 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5576 printk(KERN_CONT " N/A");
5579 printk(KERN_CONT "\n");
5582 /* sysfs temp##_input -------------------------------------------------- */
5584 static ssize_t thermal_temp_input_show(struct device *dev,
5585 struct device_attribute *attr,
5588 struct sensor_device_attribute *sensor_attr =
5589 to_sensor_dev_attr(attr);
5590 int idx = sensor_attr->index;
5594 res = thermal_get_sensor(idx, &value);
5597 if (value == TPACPI_THERMAL_SENSOR_NA)
5600 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5603 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5604 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5605 thermal_temp_input_show, NULL, _idxB)
5607 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5608 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5609 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5610 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5611 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5612 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5613 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5614 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5615 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5616 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5617 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5618 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5619 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5620 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5621 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5622 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5623 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5626 #define THERMAL_ATTRS(X) \
5627 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5629 static struct attribute *thermal_temp_input_attr[] = {
5649 static const struct attribute_group thermal_temp_input16_group = {
5650 .attrs = thermal_temp_input_attr
5653 static const struct attribute_group thermal_temp_input8_group = {
5654 .attrs = &thermal_temp_input_attr[8]
5657 #undef THERMAL_SENSOR_ATTR_TEMP
5658 #undef THERMAL_ATTRS
5660 /* --------------------------------------------------------------------- */
5662 static int __init thermal_init(struct ibm_init_struct *iibm)
5669 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5671 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5673 if (thinkpad_id.ec_model) {
5675 * Direct EC access mode: sensors at registers
5676 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5677 * non-implemented, thermal sensors return 0x80 when
5682 for (i = 0; i < 8; i++) {
5683 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5689 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5697 /* This is sheer paranoia, but we handle it anyway */
5700 "ThinkPad ACPI EC access misbehaving, "
5701 "falling back to ACPI TMPx access "
5703 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5706 "ThinkPad ACPI EC access misbehaving, "
5707 "disabling thermal sensors access\n");
5708 thermal_read_mode = TPACPI_THERMAL_NONE;
5713 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5715 } else if (acpi_tmp7) {
5716 if (tpacpi_is_ibm() &&
5717 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5718 /* 600e/x, 770e, 770x */
5719 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5721 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5722 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5725 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5726 thermal_read_mode = TPACPI_THERMAL_NONE;
5729 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5730 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5733 switch (thermal_read_mode) {
5734 case TPACPI_THERMAL_TPEC_16:
5735 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5736 &thermal_temp_input16_group);
5740 case TPACPI_THERMAL_TPEC_8:
5741 case TPACPI_THERMAL_ACPI_TMP07:
5742 case TPACPI_THERMAL_ACPI_UPDT:
5743 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5744 &thermal_temp_input8_group);
5748 case TPACPI_THERMAL_NONE:
5756 static void thermal_exit(void)
5758 switch (thermal_read_mode) {
5759 case TPACPI_THERMAL_TPEC_16:
5760 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5761 &thermal_temp_input16_group);
5763 case TPACPI_THERMAL_TPEC_8:
5764 case TPACPI_THERMAL_ACPI_TMP07:
5765 case TPACPI_THERMAL_ACPI_UPDT:
5766 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5767 &thermal_temp_input8_group);
5769 case TPACPI_THERMAL_NONE:
5775 static int thermal_read(struct seq_file *m)
5778 struct ibm_thermal_sensors_struct t;
5780 n = thermal_get_sensors(&t);
5781 if (unlikely(n < 0))
5784 seq_printf(m, "temperatures:\t");
5787 for (i = 0; i < (n - 1); i++)
5788 seq_printf(m, "%d ", t.temp[i] / 1000);
5789 seq_printf(m, "%d\n", t.temp[i] / 1000);
5791 seq_printf(m, "not supported\n");
5796 static struct ibm_struct thermal_driver_data = {
5798 .read = thermal_read,
5799 .exit = thermal_exit,
5802 /*************************************************************************
5806 static u8 ecdump_regs[256];
5808 static int ecdump_read(struct seq_file *m)
5814 " +00 +01 +02 +03 +04 +05 +06 +07"
5815 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5816 for (i = 0; i < 256; i += 16) {
5817 seq_printf(m, "EC 0x%02x:", i);
5818 for (j = 0; j < 16; j++) {
5819 if (!acpi_ec_read(i + j, &v))
5821 if (v != ecdump_regs[i + j])
5822 seq_printf(m, " *%02x", v);
5824 seq_printf(m, " %02x", v);
5825 ecdump_regs[i + j] = v;
5832 /* These are way too dangerous to advertise openly... */
5834 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5835 " (<offset> is 00-ff, <value> is 00-ff)\n");
5836 seq_printf(m, "commands:\t0x<offset> <value> "
5837 " (<offset> is 00-ff, <value> is 0-255)\n");
5842 static int ecdump_write(char *buf)
5847 while ((cmd = next_cmd(&buf))) {
5848 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5850 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5854 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5855 if (!acpi_ec_write(i, v))
5864 static struct ibm_struct ecdump_driver_data = {
5866 .read = ecdump_read,
5867 .write = ecdump_write,
5868 .flags.experimental = 1,
5871 /*************************************************************************
5872 * Backlight/brightness subdriver
5875 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5878 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5879 * CMOS NVRAM byte 0x5E, bits 0-3.
5881 * EC HBRV (0x31) has the following layout
5882 * Bit 7: unknown function
5883 * Bit 6: unknown function
5884 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5885 * Bit 4: must be set to zero to avoid problems
5886 * Bit 3-0: backlight brightness level
5888 * brightness_get_raw returns status data in the HBRV layout
5890 * WARNING: The X61 has been verified to use HBRV for something else, so
5891 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5892 * testing on the very early *60 Lenovo models...
5896 TP_EC_BACKLIGHT = 0x31,
5898 /* TP_EC_BACKLIGHT bitmasks */
5899 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5900 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5901 TP_EC_BACKLIGHT_MAPSW = 0x20,
5904 enum tpacpi_brightness_access_mode {
5905 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5906 TPACPI_BRGHT_MODE_EC, /* EC control */
5907 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5908 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5909 TPACPI_BRGHT_MODE_MAX
5912 static struct backlight_device *ibm_backlight_device;
5914 static enum tpacpi_brightness_access_mode brightness_mode =
5915 TPACPI_BRGHT_MODE_MAX;
5917 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5919 static struct mutex brightness_mutex;
5921 /* NVRAM brightness access,
5922 * call with brightness_mutex held! */
5923 static unsigned int tpacpi_brightness_nvram_get(void)
5927 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5928 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5929 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5930 lnvram &= bright_maxlvl;
5935 static void tpacpi_brightness_checkpoint_nvram(void)
5940 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5943 vdbg_printk(TPACPI_DBG_BRGHT,
5944 "trying to checkpoint backlight level to NVRAM...\n");
5946 if (mutex_lock_killable(&brightness_mutex) < 0)
5949 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5951 lec &= TP_EC_BACKLIGHT_LVLMSK;
5952 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5954 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5955 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5956 /* NVRAM needs update */
5957 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5958 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5960 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5961 dbg_printk(TPACPI_DBG_BRGHT,
5962 "updated NVRAM backlight level to %u (0x%02x)\n",
5963 (unsigned int) lec, (unsigned int) b_nvram);
5965 vdbg_printk(TPACPI_DBG_BRGHT,
5966 "NVRAM backlight level already is %u (0x%02x)\n",
5967 (unsigned int) lec, (unsigned int) b_nvram);
5970 mutex_unlock(&brightness_mutex);
5974 /* call with brightness_mutex held! */
5975 static int tpacpi_brightness_get_raw(int *status)
5979 switch (brightness_mode) {
5980 case TPACPI_BRGHT_MODE_UCMS_STEP:
5981 *status = tpacpi_brightness_nvram_get();
5983 case TPACPI_BRGHT_MODE_EC:
5984 case TPACPI_BRGHT_MODE_ECNVRAM:
5985 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5994 /* call with brightness_mutex held! */
5995 /* do NOT call with illegal backlight level value */
5996 static int tpacpi_brightness_set_ec(unsigned int value)
6000 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6003 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6004 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6005 (value & TP_EC_BACKLIGHT_LVLMSK))))
6011 /* call with brightness_mutex held! */
6012 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6015 unsigned int current_value, i;
6017 current_value = tpacpi_brightness_nvram_get();
6019 if (value == current_value)
6022 cmos_cmd = (value > current_value) ?
6023 TP_CMOS_BRIGHTNESS_UP :
6024 TP_CMOS_BRIGHTNESS_DOWN;
6025 inc = (value > current_value) ? 1 : -1;
6027 for (i = current_value; i != value; i += inc)
6028 if (issue_thinkpad_cmos_command(cmos_cmd))
6034 /* May return EINTR which can always be mapped to ERESTARTSYS */
6035 static int brightness_set(unsigned int value)
6039 if (value > bright_maxlvl || value < 0)
6042 vdbg_printk(TPACPI_DBG_BRGHT,
6043 "set backlight level to %d\n", value);
6045 res = mutex_lock_killable(&brightness_mutex);
6049 switch (brightness_mode) {
6050 case TPACPI_BRGHT_MODE_EC:
6051 case TPACPI_BRGHT_MODE_ECNVRAM:
6052 res = tpacpi_brightness_set_ec(value);
6054 case TPACPI_BRGHT_MODE_UCMS_STEP:
6055 res = tpacpi_brightness_set_ucmsstep(value);
6061 mutex_unlock(&brightness_mutex);
6065 /* sysfs backlight class ----------------------------------------------- */
6067 static int brightness_update_status(struct backlight_device *bd)
6069 unsigned int level =
6070 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6071 bd->props.power == FB_BLANK_UNBLANK) ?
6072 bd->props.brightness : 0;
6074 dbg_printk(TPACPI_DBG_BRGHT,
6075 "backlight: attempt to set level to %d\n",
6078 /* it is the backlight class's job (caller) to handle
6079 * EINTR and other errors properly */
6080 return brightness_set(level);
6083 static int brightness_get(struct backlight_device *bd)
6087 res = mutex_lock_killable(&brightness_mutex);
6091 res = tpacpi_brightness_get_raw(&status);
6093 mutex_unlock(&brightness_mutex);
6098 return status & TP_EC_BACKLIGHT_LVLMSK;
6101 static void tpacpi_brightness_notify_change(void)
6103 backlight_force_update(ibm_backlight_device,
6104 BACKLIGHT_UPDATE_HOTKEY);
6107 static struct backlight_ops ibm_backlight_data = {
6108 .get_brightness = brightness_get,
6109 .update_status = brightness_update_status,
6112 /* --------------------------------------------------------------------- */
6114 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6116 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6117 union acpi_object *obj;
6120 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6121 obj = (union acpi_object *)buffer.pointer;
6122 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6123 printk(TPACPI_ERR "Unknown _BCL data, "
6124 "please report this to %s\n", TPACPI_MAIL);
6127 rc = obj->package.count;
6133 kfree(buffer.pointer);
6137 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6138 u32 lvl, void *context, void **rv)
6140 char name[ACPI_PATH_SEGMENT_LENGTH];
6141 struct acpi_buffer buffer = { sizeof(name), &name };
6143 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6144 !strncmp("_BCL", name, sizeof(name) - 1)) {
6145 BUG_ON(!rv || !*rv);
6146 **(int **)rv = tpacpi_query_bcl_levels(handle);
6147 return AE_CTRL_TERMINATE;
6154 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6156 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6160 void *bcl_ptr = &bcl_levels;
6163 TPACPI_ACPIHANDLE_INIT(vid);
6169 * Search for a _BCL method, and execute it. This is safe on all
6170 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6171 * BIOS in ACPI backlight control mode. We do NOT have to care
6172 * about calling the _BCL method in an enabled video device, any
6173 * will do for our purposes.
6176 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6177 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6180 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6181 tp_features.bright_acpimode = 1;
6182 return bcl_levels - 2;
6189 * These are only useful for models that have only one possibility
6190 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6193 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6194 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6195 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6197 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6198 /* Models with ATI GPUs known to require ECNVRAM mode */
6199 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6201 /* Models with ATI GPUs that can use ECNVRAM */
6202 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6203 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6204 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6205 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6207 /* Models with Intel Extreme Graphics 2 */
6208 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6209 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6210 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6212 /* Models with Intel GMA900 */
6213 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6214 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6215 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6219 * Returns < 0 for error, otherwise sets tp_features.bright_*
6220 * and bright_maxlvl.
6222 static void __init tpacpi_detect_brightness_capabilities(void)
6226 vdbg_printk(TPACPI_DBG_INIT,
6227 "detecting firmware brightness interface capabilities\n");
6229 /* we could run a quirks check here (same table used by
6230 * brightness_init) if needed */
6233 * We always attempt to detect acpi support, so as to switch
6234 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6235 * going to publish a backlight interface
6237 b = tpacpi_check_std_acpi_brightness_support();
6242 "detected a 16-level brightness capable ThinkPad\n");
6248 "detected a 8-level brightness capable ThinkPad\n");
6252 "Unsupported brightness interface, "
6253 "please contact %s\n", TPACPI_MAIL);
6254 tp_features.bright_unkfw = 1;
6255 bright_maxlvl = b - 1;
6259 static int __init brightness_init(struct ibm_init_struct *iibm)
6261 struct backlight_properties props;
6263 unsigned long quirks;
6265 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6267 mutex_init(&brightness_mutex);
6269 quirks = tpacpi_check_quirks(brightness_quirk_table,
6270 ARRAY_SIZE(brightness_quirk_table));
6272 /* tpacpi_detect_brightness_capabilities() must have run already */
6274 /* if it is unknown, we don't handle it: it wouldn't be safe */
6275 if (tp_features.bright_unkfw)
6278 if (tp_features.bright_acpimode) {
6279 if (acpi_video_backlight_support()) {
6280 if (brightness_enable > 1) {
6281 printk(TPACPI_NOTICE
6282 "Standard ACPI backlight interface "
6283 "available, not loading native one.\n");
6285 } else if (brightness_enable == 1) {
6286 printk(TPACPI_NOTICE
6287 "Backlight control force enabled, even if standard "
6288 "ACPI backlight interface is available\n");
6291 if (brightness_enable > 1) {
6292 printk(TPACPI_NOTICE
6293 "Standard ACPI backlight interface not "
6294 "available, thinkpad_acpi native "
6295 "brightness control enabled\n");
6300 if (!brightness_enable) {
6301 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6302 "brightness support disabled by "
6303 "module parameter\n");
6308 * Check for module parameter bogosity, note that we
6309 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6310 * able to detect "unspecified"
6312 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6315 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6316 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6317 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6318 if (quirks & TPACPI_BRGHT_Q_EC)
6319 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6321 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6323 dbg_printk(TPACPI_DBG_BRGHT,
6324 "driver auto-selected brightness_mode=%d\n",
6329 if (!tpacpi_is_ibm() &&
6330 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6331 brightness_mode == TPACPI_BRGHT_MODE_EC))
6334 if (tpacpi_brightness_get_raw(&b) < 0)
6337 memset(&props, 0, sizeof(struct backlight_properties));
6338 props.max_brightness = bright_maxlvl;
6339 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6340 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6342 &ibm_backlight_data,
6344 if (IS_ERR(ibm_backlight_device)) {
6345 int rc = PTR_ERR(ibm_backlight_device);
6346 ibm_backlight_device = NULL;
6347 printk(TPACPI_ERR "Could not register backlight device\n");
6350 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6351 "brightness is supported\n");
6353 if (quirks & TPACPI_BRGHT_Q_ASK) {
6354 printk(TPACPI_NOTICE
6355 "brightness: will use unverified default: "
6356 "brightness_mode=%d\n", brightness_mode);
6357 printk(TPACPI_NOTICE
6358 "brightness: please report to %s whether it works well "
6359 "or not on your ThinkPad\n", TPACPI_MAIL);
6362 backlight_update_status(ibm_backlight_device);
6364 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6365 "brightness: registering brightness hotkeys "
6366 "as change notification\n");
6367 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6368 | TP_ACPI_HKEY_BRGHTUP_MASK
6369 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6373 static void brightness_suspend(pm_message_t state)
6375 tpacpi_brightness_checkpoint_nvram();
6378 static void brightness_shutdown(void)
6380 tpacpi_brightness_checkpoint_nvram();
6383 static void brightness_exit(void)
6385 if (ibm_backlight_device) {
6386 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6387 "calling backlight_device_unregister()\n");
6388 backlight_device_unregister(ibm_backlight_device);
6391 tpacpi_brightness_checkpoint_nvram();
6394 static int brightness_read(struct seq_file *m)
6398 level = brightness_get(NULL);
6400 seq_printf(m, "level:\t\tunreadable\n");
6402 seq_printf(m, "level:\t\t%d\n", level);
6403 seq_printf(m, "commands:\tup, down\n");
6404 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6411 static int brightness_write(char *buf)
6417 level = brightness_get(NULL);
6421 while ((cmd = next_cmd(&buf))) {
6422 if (strlencmp(cmd, "up") == 0) {
6423 if (level < bright_maxlvl)
6425 } else if (strlencmp(cmd, "down") == 0) {
6428 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6429 level >= 0 && level <= bright_maxlvl) {
6435 tpacpi_disclose_usertask("procfs brightness",
6436 "set level to %d\n", level);
6439 * Now we know what the final level should be, so we try to set it.
6440 * Doing it this way makes the syscall restartable in case of EINTR
6442 rc = brightness_set(level);
6443 if (!rc && ibm_backlight_device)
6444 backlight_force_update(ibm_backlight_device,
6445 BACKLIGHT_UPDATE_SYSFS);
6446 return (rc == -EINTR)? -ERESTARTSYS : rc;
6449 static struct ibm_struct brightness_driver_data = {
6450 .name = "brightness",
6451 .read = brightness_read,
6452 .write = brightness_write,
6453 .exit = brightness_exit,
6454 .suspend = brightness_suspend,
6455 .shutdown = brightness_shutdown,
6458 /*************************************************************************
6463 * IBM ThinkPads have a simple volume controller with MUTE gating.
6464 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6466 * Since the *61 series (and probably also the later *60 series), Lenovo
6467 * ThinkPads only implement the MUTE gate.
6470 * Bit 6: MUTE (1 mutes sound)
6472 * Other bits should be zero as far as we know.
6474 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6475 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6476 * such as bit 7 which is used to detect repeated presses of MUTE,
6477 * and we leave them unchanged.
6480 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6482 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6483 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6484 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6486 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6487 static char *alsa_id = "ThinkPadEC";
6488 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6490 struct tpacpi_alsa_data {
6491 struct snd_card *card;
6492 struct snd_ctl_elem_id *ctl_mute_id;
6493 struct snd_ctl_elem_id *ctl_vol_id;
6496 static struct snd_card *alsa_card;
6501 /* TP_EC_AUDIO bits */
6502 TP_EC_AUDIO_MUTESW = 6,
6504 /* TP_EC_AUDIO bitmasks */
6505 TP_EC_AUDIO_LVL_MSK = 0x0F,
6506 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6508 /* Maximum volume */
6509 TP_EC_VOLUME_MAX = 14,
6512 enum tpacpi_volume_access_mode {
6513 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6514 TPACPI_VOL_MODE_EC, /* Pure EC control */
6515 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6516 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6520 enum tpacpi_volume_capabilities {
6521 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6522 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6523 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6527 static enum tpacpi_volume_access_mode volume_mode =
6528 TPACPI_VOL_MODE_MAX;
6530 static enum tpacpi_volume_capabilities volume_capabilities;
6531 static int volume_control_allowed;
6534 * Used to syncronize writers to TP_EC_AUDIO and
6535 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6537 static struct mutex volume_mutex;
6539 static void tpacpi_volume_checkpoint_nvram(void)
6545 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6547 if (!volume_control_allowed)
6550 vdbg_printk(TPACPI_DBG_MIXER,
6551 "trying to checkpoint mixer state to NVRAM...\n");
6553 if (tp_features.mixer_no_level_control)
6554 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6556 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6558 if (mutex_lock_killable(&volume_mutex) < 0)
6561 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6564 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6566 if (lec != (b_nvram & ec_mask)) {
6567 /* NVRAM needs update */
6568 b_nvram &= ~ec_mask;
6570 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6571 dbg_printk(TPACPI_DBG_MIXER,
6572 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6573 (unsigned int) lec, (unsigned int) b_nvram);
6575 vdbg_printk(TPACPI_DBG_MIXER,
6576 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6577 (unsigned int) lec, (unsigned int) b_nvram);
6581 mutex_unlock(&volume_mutex);
6584 static int volume_get_status_ec(u8 *status)
6588 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6593 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6598 static int volume_get_status(u8 *status)
6600 return volume_get_status_ec(status);
6603 static int volume_set_status_ec(const u8 status)
6605 if (!acpi_ec_write(TP_EC_AUDIO, status))
6608 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6613 static int volume_set_status(const u8 status)
6615 return volume_set_status_ec(status);
6618 /* returns < 0 on error, 0 on no change, 1 on change */
6619 static int __volume_set_mute_ec(const bool mute)
6624 if (mutex_lock_killable(&volume_mutex) < 0)
6627 rc = volume_get_status_ec(&s);
6631 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6632 s & ~TP_EC_AUDIO_MUTESW_MSK;
6635 rc = volume_set_status_ec(n);
6641 mutex_unlock(&volume_mutex);
6645 static int volume_alsa_set_mute(const bool mute)
6647 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6648 (mute) ? "" : "un");
6649 return __volume_set_mute_ec(mute);
6652 static int volume_set_mute(const bool mute)
6656 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6657 (mute) ? "" : "un");
6659 rc = __volume_set_mute_ec(mute);
6660 return (rc < 0) ? rc : 0;
6663 /* returns < 0 on error, 0 on no change, 1 on change */
6664 static int __volume_set_volume_ec(const u8 vol)
6669 if (vol > TP_EC_VOLUME_MAX)
6672 if (mutex_lock_killable(&volume_mutex) < 0)
6675 rc = volume_get_status_ec(&s);
6679 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6682 rc = volume_set_status_ec(n);
6688 mutex_unlock(&volume_mutex);
6692 static int volume_alsa_set_volume(const u8 vol)
6694 dbg_printk(TPACPI_DBG_MIXER,
6695 "ALSA: trying to set volume level to %hu\n", vol);
6696 return __volume_set_volume_ec(vol);
6699 static void volume_alsa_notify_change(void)
6701 struct tpacpi_alsa_data *d;
6703 if (alsa_card && alsa_card->private_data) {
6704 d = alsa_card->private_data;
6706 snd_ctl_notify(alsa_card,
6707 SNDRV_CTL_EVENT_MASK_VALUE,
6710 snd_ctl_notify(alsa_card,
6711 SNDRV_CTL_EVENT_MASK_VALUE,
6716 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6717 struct snd_ctl_elem_info *uinfo)
6719 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6721 uinfo->value.integer.min = 0;
6722 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6726 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6727 struct snd_ctl_elem_value *ucontrol)
6732 rc = volume_get_status(&s);
6736 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6740 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6741 struct snd_ctl_elem_value *ucontrol)
6743 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6744 ucontrol->value.integer.value[0]);
6745 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6748 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6750 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6751 struct snd_ctl_elem_value *ucontrol)
6756 rc = volume_get_status(&s);
6760 ucontrol->value.integer.value[0] =
6761 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6765 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6766 struct snd_ctl_elem_value *ucontrol)
6768 tpacpi_disclose_usertask("ALSA", "%smute\n",
6769 ucontrol->value.integer.value[0] ?
6771 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6774 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6776 .name = "Console Playback Volume",
6778 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6779 .info = volume_alsa_vol_info,
6780 .get = volume_alsa_vol_get,
6783 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6785 .name = "Console Playback Switch",
6787 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6788 .info = volume_alsa_mute_info,
6789 .get = volume_alsa_mute_get,
6792 static void volume_suspend(pm_message_t state)
6794 tpacpi_volume_checkpoint_nvram();
6797 static void volume_resume(void)
6799 volume_alsa_notify_change();
6802 static void volume_shutdown(void)
6804 tpacpi_volume_checkpoint_nvram();
6807 static void volume_exit(void)
6810 snd_card_free(alsa_card);
6814 tpacpi_volume_checkpoint_nvram();
6817 static int __init volume_create_alsa_mixer(void)
6819 struct snd_card *card;
6820 struct tpacpi_alsa_data *data;
6821 struct snd_kcontrol *ctl_vol;
6822 struct snd_kcontrol *ctl_mute;
6825 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6826 sizeof(struct tpacpi_alsa_data), &card);
6827 if (rc < 0 || !card) {
6829 "Failed to create ALSA card structures: %d\n", rc);
6833 BUG_ON(!card->private_data);
6834 data = card->private_data;
6837 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6838 sizeof(card->driver));
6839 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6840 sizeof(card->shortname));
6841 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6842 (thinkpad_id.ec_version_str) ?
6843 thinkpad_id.ec_version_str : "(unknown)");
6844 snprintf(card->longname, sizeof(card->longname),
6845 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6846 (thinkpad_id.ec_version_str) ?
6847 thinkpad_id.ec_version_str : "unknown");
6849 if (volume_control_allowed) {
6850 volume_alsa_control_vol.put = volume_alsa_vol_put;
6851 volume_alsa_control_vol.access =
6852 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6854 volume_alsa_control_mute.put = volume_alsa_mute_put;
6855 volume_alsa_control_mute.access =
6856 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6859 if (!tp_features.mixer_no_level_control) {
6860 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6861 rc = snd_ctl_add(card, ctl_vol);
6864 "Failed to create ALSA volume control: %d\n",
6868 data->ctl_vol_id = &ctl_vol->id;
6871 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6872 rc = snd_ctl_add(card, ctl_mute);
6874 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6878 data->ctl_mute_id = &ctl_mute->id;
6880 snd_card_set_dev(card, &tpacpi_pdev->dev);
6881 rc = snd_card_register(card);
6883 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6891 snd_card_free(card);
6895 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6896 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6898 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6899 /* Whitelist volume level on all IBM by default */
6900 { .vendor = PCI_VENDOR_ID_IBM,
6901 .bios = TPACPI_MATCH_ANY,
6902 .ec = TPACPI_MATCH_ANY,
6903 .quirks = TPACPI_VOL_Q_LEVEL },
6905 /* Lenovo models with volume control (needs confirmation) */
6906 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6907 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6908 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6909 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6910 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6911 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6912 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6914 /* Whitelist mute-only on all Lenovo by default */
6915 { .vendor = PCI_VENDOR_ID_LENOVO,
6916 .bios = TPACPI_MATCH_ANY,
6917 .ec = TPACPI_MATCH_ANY,
6918 .quirks = TPACPI_VOL_Q_MUTEONLY }
6921 static int __init volume_init(struct ibm_init_struct *iibm)
6923 unsigned long quirks;
6926 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6928 mutex_init(&volume_mutex);
6931 * Check for module parameter bogosity, note that we
6932 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6933 * able to detect "unspecified"
6935 if (volume_mode > TPACPI_VOL_MODE_MAX)
6938 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6940 "UCMS step volume mode not implemented, "
6941 "please contact %s\n", TPACPI_MAIL);
6945 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6949 * The ALSA mixer is our primary interface.
6950 * When disabled, don't install the subdriver at all
6953 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6954 "ALSA mixer disabled by parameter, "
6955 "not loading volume subdriver...\n");
6959 quirks = tpacpi_check_quirks(volume_quirk_table,
6960 ARRAY_SIZE(volume_quirk_table));
6962 switch (volume_capabilities) {
6963 case TPACPI_VOL_CAP_AUTO:
6964 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6965 tp_features.mixer_no_level_control = 1;
6966 else if (quirks & TPACPI_VOL_Q_LEVEL)
6967 tp_features.mixer_no_level_control = 0;
6969 return 1; /* no mixer */
6971 case TPACPI_VOL_CAP_VOLMUTE:
6972 tp_features.mixer_no_level_control = 0;
6974 case TPACPI_VOL_CAP_MUTEONLY:
6975 tp_features.mixer_no_level_control = 1;
6981 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6982 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6983 "using user-supplied volume_capabilities=%d\n",
6984 volume_capabilities);
6986 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6987 volume_mode == TPACPI_VOL_MODE_MAX) {
6988 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6990 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6991 "driver auto-selected volume_mode=%d\n",
6994 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6995 "using user-supplied volume_mode=%d\n",
6999 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7000 "mute is supported, volume control is %s\n",
7001 str_supported(!tp_features.mixer_no_level_control));
7003 rc = volume_create_alsa_mixer();
7006 "Could not create the ALSA mixer interface\n");
7011 "Console audio control enabled, mode: %s\n",
7012 (volume_control_allowed) ?
7013 "override (read/write)" :
7014 "monitor (read only)");
7016 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7017 "registering volume hotkeys as change notification\n");
7018 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7019 | TP_ACPI_HKEY_VOLUP_MASK
7020 | TP_ACPI_HKEY_VOLDWN_MASK
7021 | TP_ACPI_HKEY_MUTE_MASK);
7026 static int volume_read(struct seq_file *m)
7030 if (volume_get_status(&status) < 0) {
7031 seq_printf(m, "level:\t\tunreadable\n");
7033 if (tp_features.mixer_no_level_control)
7034 seq_printf(m, "level:\t\tunsupported\n");
7036 seq_printf(m, "level:\t\t%d\n",
7037 status & TP_EC_AUDIO_LVL_MSK);
7039 seq_printf(m, "mute:\t\t%s\n",
7040 onoff(status, TP_EC_AUDIO_MUTESW));
7042 if (volume_control_allowed) {
7043 seq_printf(m, "commands:\tunmute, mute\n");
7044 if (!tp_features.mixer_no_level_control) {
7046 "commands:\tup, down\n");
7048 "commands:\tlevel <level>"
7049 " (<level> is 0-%d)\n",
7058 static int volume_write(char *buf)
7061 u8 new_level, new_mute;
7067 * We do allow volume control at driver startup, so that the
7068 * user can set initial state through the volume=... parameter hack.
7070 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7071 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7072 tp_warned.volume_ctrl_forbidden = 1;
7073 printk(TPACPI_NOTICE
7074 "Console audio control in monitor mode, "
7075 "changes are not allowed.\n");
7076 printk(TPACPI_NOTICE
7077 "Use the volume_control=1 module parameter "
7078 "to enable volume control\n");
7083 rc = volume_get_status(&s);
7087 new_level = s & TP_EC_AUDIO_LVL_MSK;
7088 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7090 while ((cmd = next_cmd(&buf))) {
7091 if (!tp_features.mixer_no_level_control) {
7092 if (strlencmp(cmd, "up") == 0) {
7095 else if (new_level < TP_EC_VOLUME_MAX)
7098 } else if (strlencmp(cmd, "down") == 0) {
7101 else if (new_level > 0)
7104 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7105 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7110 if (strlencmp(cmd, "mute") == 0)
7111 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7112 else if (strlencmp(cmd, "unmute") == 0)
7118 if (tp_features.mixer_no_level_control) {
7119 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7120 new_mute ? "" : "un");
7121 rc = volume_set_mute(!!new_mute);
7123 tpacpi_disclose_usertask("procfs volume",
7124 "%smute and set level to %d\n",
7125 new_mute ? "" : "un", new_level);
7126 rc = volume_set_status(new_mute | new_level);
7128 volume_alsa_notify_change();
7130 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7133 static struct ibm_struct volume_driver_data = {
7135 .read = volume_read,
7136 .write = volume_write,
7137 .exit = volume_exit,
7138 .suspend = volume_suspend,
7139 .resume = volume_resume,
7140 .shutdown = volume_shutdown,
7143 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7145 #define alsa_card NULL
7147 static void inline volume_alsa_notify_change(void)
7151 static int __init volume_init(struct ibm_init_struct *iibm)
7154 "volume: disabled as there is no ALSA support in this kernel\n");
7159 static struct ibm_struct volume_driver_data = {
7163 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7165 /*************************************************************************
7172 * TPACPI_FAN_RD_ACPI_GFAN:
7173 * ACPI GFAN method: returns fan level
7175 * see TPACPI_FAN_WR_ACPI_SFAN
7176 * EC 0x2f (HFSP) not available if GFAN exists
7178 * TPACPI_FAN_WR_ACPI_SFAN:
7179 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7181 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7184 * TPACPI_FAN_WR_TPEC:
7185 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7186 * Supported on almost all ThinkPads
7188 * Fan speed changes of any sort (including those caused by the
7189 * disengaged mode) are usually done slowly by the firmware as the
7190 * maximum amount of fan duty cycle change per second seems to be
7193 * Reading is not available if GFAN exists.
7194 * Writing is not available if SFAN exists.
7197 * 7 automatic mode engaged;
7198 * (default operation mode of the ThinkPad)
7199 * fan level is ignored in this mode.
7200 * 6 full speed mode (takes precedence over bit 7);
7201 * not available on all thinkpads. May disable
7202 * the tachometer while the fan controller ramps up
7203 * the speed (which can take up to a few *minutes*).
7204 * Speeds up fan to 100% duty-cycle, which is far above
7205 * the standard RPM levels. It is not impossible that
7206 * it could cause hardware damage.
7207 * 5-3 unused in some models. Extra bits for fan level
7208 * in others, but still useless as all values above
7209 * 7 map to the same speed as level 7 in these models.
7210 * 2-0 fan level (0..7 usually)
7212 * 0x07 = max (set when temperatures critical)
7213 * Some ThinkPads may have other levels, see
7214 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7216 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7217 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7218 * does so, its initial value is meaningless (0x07).
7220 * For firmware bugs, refer to:
7221 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7225 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7226 * Main fan tachometer reading (in RPM)
7228 * This register is present on all ThinkPads with a new-style EC, and
7229 * it is known not to be present on the A21m/e, and T22, as there is
7230 * something else in offset 0x84 according to the ACPI DSDT. Other
7231 * ThinkPads from this same time period (and earlier) probably lack the
7232 * tachometer as well.
7234 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7235 * was never fixed by IBM to report the EC firmware version string
7236 * probably support the tachometer (like the early X models), so
7237 * detecting it is quite hard. We need more data to know for sure.
7239 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7242 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7245 * For firmware bugs, refer to:
7246 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7250 * ThinkPad EC register 0x31 bit 0 (only on select models)
7252 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7253 * show the speed of the main fan. When bit 0 of EC register 0x31
7254 * is one, the tachometer registers show the speed of the auxiliary
7257 * Fan control seems to affect both fans, regardless of the state
7260 * So far, only the firmware for the X60/X61 non-tablet versions
7261 * seem to support this (firmware TP-7M).
7263 * TPACPI_FAN_WR_ACPI_FANS:
7264 * ThinkPad X31, X40, X41. Not available in the X60.
7266 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7267 * high speed. ACPI DSDT seems to map these three speeds to levels
7268 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7269 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7271 * The speeds are stored on handles
7272 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7274 * There are three default speed sets, accessible as handles:
7275 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7277 * ACPI DSDT switches which set is in use depending on various
7280 * TPACPI_FAN_WR_TPEC is also available and should be used to
7281 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7282 * but the ACPI tables just mention level 7.
7285 enum { /* Fan control constants */
7286 fan_status_offset = 0x2f, /* EC register 0x2f */
7287 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7288 * 0x84 must be read before 0x85 */
7289 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7290 bit 0 selects which fan is active */
7292 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7293 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7295 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7298 enum fan_status_access_mode {
7299 TPACPI_FAN_NONE = 0, /* No fan status or control */
7300 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7301 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7304 enum fan_control_access_mode {
7305 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7306 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7307 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7308 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7311 enum fan_control_commands {
7312 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7313 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7314 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7315 * and also watchdog cmd */
7318 static int fan_control_allowed;
7320 static enum fan_status_access_mode fan_status_access_mode;
7321 static enum fan_control_access_mode fan_control_access_mode;
7322 static enum fan_control_commands fan_control_commands;
7324 static u8 fan_control_initial_status;
7325 static u8 fan_control_desired_level;
7326 static u8 fan_control_resume_level;
7327 static int fan_watchdog_maxinterval;
7329 static struct mutex fan_mutex;
7331 static void fan_watchdog_fire(struct work_struct *ignored);
7332 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7334 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7335 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7336 "\\FSPD", /* 600e/x, 770e, 770x */
7338 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7339 "JFNS", /* 770x-JL */
7343 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7344 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7345 * be in auto mode (0x80).
7347 * This is corrected by any write to HFSP either by the driver, or
7350 * We assume 0x07 really means auto mode while this quirk is active,
7351 * as this is far more likely than the ThinkPad being in level 7,
7352 * which is only used by the firmware during thermal emergencies.
7354 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7355 * TP-70 (T43, R52), which are known to be buggy.
7358 static void fan_quirk1_setup(void)
7360 if (fan_control_initial_status == 0x07) {
7361 printk(TPACPI_NOTICE
7362 "fan_init: initial fan status is unknown, "
7363 "assuming it is in auto mode\n");
7364 tp_features.fan_ctrl_status_undef = 1;
7368 static void fan_quirk1_handle(u8 *fan_status)
7370 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7371 if (*fan_status != fan_control_initial_status) {
7372 /* something changed the HFSP regisnter since
7373 * driver init time, so it is not undefined
7375 tp_features.fan_ctrl_status_undef = 0;
7377 /* Return most likely status. In fact, it
7378 * might be the only possible status */
7379 *fan_status = TP_EC_FAN_AUTO;
7384 /* Select main fan on X60/X61, NOOP on others */
7385 static bool fan_select_fan1(void)
7387 if (tp_features.second_fan) {
7390 if (ec_read(fan_select_offset, &val) < 0)
7393 if (ec_write(fan_select_offset, val) < 0)
7399 /* Select secondary fan on X60/X61 */
7400 static bool fan_select_fan2(void)
7404 if (!tp_features.second_fan)
7407 if (ec_read(fan_select_offset, &val) < 0)
7410 if (ec_write(fan_select_offset, val) < 0)
7417 * Call with fan_mutex held
7419 static void fan_update_desired_level(u8 status)
7422 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7424 fan_control_desired_level = 7;
7426 fan_control_desired_level = status;
7430 static int fan_get_status(u8 *status)
7435 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7437 switch (fan_status_access_mode) {
7438 case TPACPI_FAN_RD_ACPI_GFAN:
7439 /* 570, 600e/x, 770e, 770x */
7441 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7449 case TPACPI_FAN_RD_TPEC:
7450 /* all except 570, 600e/x, 770e, 770x */
7451 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7454 if (likely(status)) {
7456 fan_quirk1_handle(status);
7468 static int fan_get_status_safe(u8 *status)
7473 if (mutex_lock_killable(&fan_mutex))
7474 return -ERESTARTSYS;
7475 rc = fan_get_status(&s);
7477 fan_update_desired_level(s);
7478 mutex_unlock(&fan_mutex);
7486 static int fan_get_speed(unsigned int *speed)
7490 switch (fan_status_access_mode) {
7491 case TPACPI_FAN_RD_TPEC:
7492 /* all except 570, 600e/x, 770e, 770x */
7493 if (unlikely(!fan_select_fan1()))
7495 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7496 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7500 *speed = (hi << 8) | lo;
7511 static int fan2_get_speed(unsigned int *speed)
7516 switch (fan_status_access_mode) {
7517 case TPACPI_FAN_RD_TPEC:
7518 /* all except 570, 600e/x, 770e, 770x */
7519 if (unlikely(!fan_select_fan2()))
7521 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7522 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7523 fan_select_fan1(); /* play it safe */
7528 *speed = (hi << 8) | lo;
7539 static int fan_set_level(int level)
7541 if (!fan_control_allowed)
7544 switch (fan_control_access_mode) {
7545 case TPACPI_FAN_WR_ACPI_SFAN:
7546 if (level >= 0 && level <= 7) {
7547 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7553 case TPACPI_FAN_WR_ACPI_FANS:
7554 case TPACPI_FAN_WR_TPEC:
7555 if (!(level & TP_EC_FAN_AUTO) &&
7556 !(level & TP_EC_FAN_FULLSPEED) &&
7557 ((level < 0) || (level > 7)))
7560 /* safety net should the EC not support AUTO
7561 * or FULLSPEED mode bits and just ignore them */
7562 if (level & TP_EC_FAN_FULLSPEED)
7563 level |= 7; /* safety min speed 7 */
7564 else if (level & TP_EC_FAN_AUTO)
7565 level |= 4; /* safety min speed 4 */
7567 if (!acpi_ec_write(fan_status_offset, level))
7570 tp_features.fan_ctrl_status_undef = 0;
7577 vdbg_printk(TPACPI_DBG_FAN,
7578 "fan control: set fan control register to 0x%02x\n", level);
7582 static int fan_set_level_safe(int level)
7586 if (!fan_control_allowed)
7589 if (mutex_lock_killable(&fan_mutex))
7590 return -ERESTARTSYS;
7592 if (level == TPACPI_FAN_LAST_LEVEL)
7593 level = fan_control_desired_level;
7595 rc = fan_set_level(level);
7597 fan_update_desired_level(level);
7599 mutex_unlock(&fan_mutex);
7603 static int fan_set_enable(void)
7608 if (!fan_control_allowed)
7611 if (mutex_lock_killable(&fan_mutex))
7612 return -ERESTARTSYS;
7614 switch (fan_control_access_mode) {
7615 case TPACPI_FAN_WR_ACPI_FANS:
7616 case TPACPI_FAN_WR_TPEC:
7617 rc = fan_get_status(&s);
7621 /* Don't go out of emergency fan mode */
7624 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7627 if (!acpi_ec_write(fan_status_offset, s))
7630 tp_features.fan_ctrl_status_undef = 0;
7635 case TPACPI_FAN_WR_ACPI_SFAN:
7636 rc = fan_get_status(&s);
7642 /* Set fan to at least level 4 */
7645 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7655 mutex_unlock(&fan_mutex);
7658 vdbg_printk(TPACPI_DBG_FAN,
7659 "fan control: set fan control register to 0x%02x\n",
7664 static int fan_set_disable(void)
7668 if (!fan_control_allowed)
7671 if (mutex_lock_killable(&fan_mutex))
7672 return -ERESTARTSYS;
7675 switch (fan_control_access_mode) {
7676 case TPACPI_FAN_WR_ACPI_FANS:
7677 case TPACPI_FAN_WR_TPEC:
7678 if (!acpi_ec_write(fan_status_offset, 0x00))
7681 fan_control_desired_level = 0;
7682 tp_features.fan_ctrl_status_undef = 0;
7686 case TPACPI_FAN_WR_ACPI_SFAN:
7687 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7690 fan_control_desired_level = 0;
7698 vdbg_printk(TPACPI_DBG_FAN,
7699 "fan control: set fan control register to 0\n");
7701 mutex_unlock(&fan_mutex);
7705 static int fan_set_speed(int speed)
7709 if (!fan_control_allowed)
7712 if (mutex_lock_killable(&fan_mutex))
7713 return -ERESTARTSYS;
7716 switch (fan_control_access_mode) {
7717 case TPACPI_FAN_WR_ACPI_FANS:
7718 if (speed >= 0 && speed <= 65535) {
7719 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7720 speed, speed, speed))
7730 mutex_unlock(&fan_mutex);
7734 static void fan_watchdog_reset(void)
7736 static int fan_watchdog_active;
7738 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7741 if (fan_watchdog_active)
7742 cancel_delayed_work(&fan_watchdog_task);
7744 if (fan_watchdog_maxinterval > 0 &&
7745 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7746 fan_watchdog_active = 1;
7747 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7748 msecs_to_jiffies(fan_watchdog_maxinterval
7751 "failed to queue the fan watchdog, "
7752 "watchdog will not trigger\n");
7755 fan_watchdog_active = 0;
7758 static void fan_watchdog_fire(struct work_struct *ignored)
7762 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7765 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7766 rc = fan_set_enable();
7768 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7769 "will try again later...\n", -rc);
7770 /* reschedule for later */
7771 fan_watchdog_reset();
7776 * SYSFS fan layout: hwmon compatible (device)
7779 * 0: "disengaged" mode
7781 * 2: native EC "auto" mode (recommended, hardware default)
7783 * pwm*: set speed in manual mode, ignored otherwise.
7784 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7787 * fan*_input: tachometer reading, RPM
7790 * SYSFS fan layout: extensions
7792 * fan_watchdog (driver):
7793 * fan watchdog interval in seconds, 0 disables (default), max 120
7796 /* sysfs fan pwm1_enable ----------------------------------------------- */
7797 static ssize_t fan_pwm1_enable_show(struct device *dev,
7798 struct device_attribute *attr,
7804 res = fan_get_status_safe(&status);
7808 if (status & TP_EC_FAN_FULLSPEED) {
7810 } else if (status & TP_EC_FAN_AUTO) {
7815 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7818 static ssize_t fan_pwm1_enable_store(struct device *dev,
7819 struct device_attribute *attr,
7820 const char *buf, size_t count)
7825 if (parse_strtoul(buf, 2, &t))
7828 tpacpi_disclose_usertask("hwmon pwm1_enable",
7829 "set fan mode to %lu\n", t);
7833 level = TP_EC_FAN_FULLSPEED;
7836 level = TPACPI_FAN_LAST_LEVEL;
7839 level = TP_EC_FAN_AUTO;
7842 /* reserved for software-controlled auto mode */
7848 res = fan_set_level_safe(level);
7854 fan_watchdog_reset();
7859 static struct device_attribute dev_attr_fan_pwm1_enable =
7860 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7861 fan_pwm1_enable_show, fan_pwm1_enable_store);
7863 /* sysfs fan pwm1 ------------------------------------------------------ */
7864 static ssize_t fan_pwm1_show(struct device *dev,
7865 struct device_attribute *attr,
7871 res = fan_get_status_safe(&status);
7876 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7877 status = fan_control_desired_level;
7882 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7885 static ssize_t fan_pwm1_store(struct device *dev,
7886 struct device_attribute *attr,
7887 const char *buf, size_t count)
7891 u8 status, newlevel;
7893 if (parse_strtoul(buf, 255, &s))
7896 tpacpi_disclose_usertask("hwmon pwm1",
7897 "set fan speed to %lu\n", s);
7899 /* scale down from 0-255 to 0-7 */
7900 newlevel = (s >> 5) & 0x07;
7902 if (mutex_lock_killable(&fan_mutex))
7903 return -ERESTARTSYS;
7905 rc = fan_get_status(&status);
7906 if (!rc && (status &
7907 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7908 rc = fan_set_level(newlevel);
7912 fan_update_desired_level(newlevel);
7913 fan_watchdog_reset();
7917 mutex_unlock(&fan_mutex);
7918 return (rc)? rc : count;
7921 static struct device_attribute dev_attr_fan_pwm1 =
7922 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7923 fan_pwm1_show, fan_pwm1_store);
7925 /* sysfs fan fan1_input ------------------------------------------------ */
7926 static ssize_t fan_fan1_input_show(struct device *dev,
7927 struct device_attribute *attr,
7933 res = fan_get_speed(&speed);
7937 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7940 static struct device_attribute dev_attr_fan_fan1_input =
7941 __ATTR(fan1_input, S_IRUGO,
7942 fan_fan1_input_show, NULL);
7944 /* sysfs fan fan2_input ------------------------------------------------ */
7945 static ssize_t fan_fan2_input_show(struct device *dev,
7946 struct device_attribute *attr,
7952 res = fan2_get_speed(&speed);
7956 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7959 static struct device_attribute dev_attr_fan_fan2_input =
7960 __ATTR(fan2_input, S_IRUGO,
7961 fan_fan2_input_show, NULL);
7963 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7964 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7967 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7970 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7971 const char *buf, size_t count)
7975 if (parse_strtoul(buf, 120, &t))
7978 if (!fan_control_allowed)
7981 fan_watchdog_maxinterval = t;
7982 fan_watchdog_reset();
7984 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7989 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7990 fan_fan_watchdog_show, fan_fan_watchdog_store);
7992 /* --------------------------------------------------------------------- */
7993 static struct attribute *fan_attributes[] = {
7994 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7995 &dev_attr_fan_fan1_input.attr,
7996 NULL, /* for fan2_input */
8000 static const struct attribute_group fan_attr_group = {
8001 .attrs = fan_attributes,
8004 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8005 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8007 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8008 { .vendor = PCI_VENDOR_ID_IBM, \
8009 .bios = TPACPI_MATCH_ANY, \
8010 .ec = TPID(__id1, __id2), \
8011 .quirks = __quirks }
8013 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8014 { .vendor = PCI_VENDOR_ID_LENOVO, \
8015 .bios = TPACPI_MATCH_ANY, \
8016 .ec = TPID(__id1, __id2), \
8017 .quirks = __quirks }
8019 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8020 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8021 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8022 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8023 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8024 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8027 #undef TPACPI_FAN_QL
8028 #undef TPACPI_FAN_QI
8030 static int __init fan_init(struct ibm_init_struct *iibm)
8033 unsigned long quirks;
8035 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8036 "initializing fan subdriver\n");
8038 mutex_init(&fan_mutex);
8039 fan_status_access_mode = TPACPI_FAN_NONE;
8040 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8041 fan_control_commands = 0;
8042 fan_watchdog_maxinterval = 0;
8043 tp_features.fan_ctrl_status_undef = 0;
8044 tp_features.second_fan = 0;
8045 fan_control_desired_level = 7;
8047 if (tpacpi_is_ibm()) {
8048 TPACPI_ACPIHANDLE_INIT(fans);
8049 TPACPI_ACPIHANDLE_INIT(gfan);
8050 TPACPI_ACPIHANDLE_INIT(sfan);
8053 quirks = tpacpi_check_quirks(fan_quirk_table,
8054 ARRAY_SIZE(fan_quirk_table));
8057 /* 570, 600e/x, 770e, 770x */
8058 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8060 /* all other ThinkPads: note that even old-style
8061 * ThinkPad ECs supports the fan control register */
8062 if (likely(acpi_ec_read(fan_status_offset,
8063 &fan_control_initial_status))) {
8064 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8065 if (quirks & TPACPI_FAN_Q1)
8067 if (quirks & TPACPI_FAN_2FAN) {
8068 tp_features.second_fan = 1;
8069 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8070 "secondary fan support enabled\n");
8074 "ThinkPad ACPI EC access misbehaving, "
8075 "fan status and control unavailable\n");
8082 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8083 fan_control_commands |=
8084 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8087 /* gfan without sfan means no fan control */
8088 /* all other models implement TP EC 0x2f control */
8092 fan_control_access_mode =
8093 TPACPI_FAN_WR_ACPI_FANS;
8094 fan_control_commands |=
8095 TPACPI_FAN_CMD_SPEED |
8096 TPACPI_FAN_CMD_LEVEL |
8097 TPACPI_FAN_CMD_ENABLE;
8099 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8100 fan_control_commands |=
8101 TPACPI_FAN_CMD_LEVEL |
8102 TPACPI_FAN_CMD_ENABLE;
8107 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8108 "fan is %s, modes %d, %d\n",
8109 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8110 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8111 fan_status_access_mode, fan_control_access_mode);
8113 /* fan control master switch */
8114 if (!fan_control_allowed) {
8115 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8116 fan_control_commands = 0;
8117 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8118 "fan control features disabled by parameter\n");
8121 /* update fan_control_desired_level */
8122 if (fan_status_access_mode != TPACPI_FAN_NONE)
8123 fan_get_status_safe(NULL);
8125 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8126 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8127 if (tp_features.second_fan) {
8128 /* attach second fan tachometer */
8129 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8130 &dev_attr_fan_fan2_input.attr;
8132 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8137 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8138 &driver_attr_fan_watchdog);
8140 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8149 static void fan_exit(void)
8151 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8152 "cancelling any pending fan watchdog tasks\n");
8154 /* FIXME: can we really do this unconditionally? */
8155 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8156 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8157 &driver_attr_fan_watchdog);
8159 cancel_delayed_work(&fan_watchdog_task);
8160 flush_workqueue(tpacpi_wq);
8163 static void fan_suspend(pm_message_t state)
8167 if (!fan_control_allowed)
8170 /* Store fan status in cache */
8171 fan_control_resume_level = 0;
8172 rc = fan_get_status_safe(&fan_control_resume_level);
8174 printk(TPACPI_NOTICE
8175 "failed to read fan level for later "
8176 "restore during resume: %d\n", rc);
8178 /* if it is undefined, don't attempt to restore it.
8180 if (tp_features.fan_ctrl_status_undef)
8181 fan_control_resume_level = 0;
8184 static void fan_resume(void)
8186 u8 current_level = 7;
8187 bool do_set = false;
8190 /* DSDT *always* updates status on resume */
8191 tp_features.fan_ctrl_status_undef = 0;
8193 if (!fan_control_allowed ||
8194 !fan_control_resume_level ||
8195 (fan_get_status_safe(¤t_level) < 0))
8198 switch (fan_control_access_mode) {
8199 case TPACPI_FAN_WR_ACPI_SFAN:
8200 /* never decrease fan level */
8201 do_set = (fan_control_resume_level > current_level);
8203 case TPACPI_FAN_WR_ACPI_FANS:
8204 case TPACPI_FAN_WR_TPEC:
8205 /* never decrease fan level, scale is:
8206 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8208 * We expect the firmware to set either 7 or AUTO, but we
8209 * handle FULLSPEED out of paranoia.
8211 * So, we can safely only restore FULLSPEED or 7, anything
8212 * else could slow the fan. Restoring AUTO is useless, at
8213 * best that's exactly what the DSDT already set (it is the
8216 * Always keep in mind that the DSDT *will* have set the
8217 * fans to what the vendor supposes is the best level. We
8218 * muck with it only to speed the fan up.
8220 if (fan_control_resume_level != 7 &&
8221 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8224 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8225 (current_level != fan_control_resume_level);
8231 printk(TPACPI_NOTICE
8232 "restoring fan level to 0x%02x\n",
8233 fan_control_resume_level);
8234 rc = fan_set_level_safe(fan_control_resume_level);
8236 printk(TPACPI_NOTICE
8237 "failed to restore fan level: %d\n", rc);
8241 static int fan_read(struct seq_file *m)
8245 unsigned int speed = 0;
8247 switch (fan_status_access_mode) {
8248 case TPACPI_FAN_RD_ACPI_GFAN:
8249 /* 570, 600e/x, 770e, 770x */
8250 rc = fan_get_status_safe(&status);
8254 seq_printf(m, "status:\t\t%s\n"
8256 (status != 0) ? "enabled" : "disabled", status);
8259 case TPACPI_FAN_RD_TPEC:
8260 /* all except 570, 600e/x, 770e, 770x */
8261 rc = fan_get_status_safe(&status);
8265 seq_printf(m, "status:\t\t%s\n",
8266 (status != 0) ? "enabled" : "disabled");
8268 rc = fan_get_speed(&speed);
8272 seq_printf(m, "speed:\t\t%d\n", speed);
8274 if (status & TP_EC_FAN_FULLSPEED)
8275 /* Disengaged mode takes precedence */
8276 seq_printf(m, "level:\t\tdisengaged\n");
8277 else if (status & TP_EC_FAN_AUTO)
8278 seq_printf(m, "level:\t\tauto\n");
8280 seq_printf(m, "level:\t\t%d\n", status);
8283 case TPACPI_FAN_NONE:
8285 seq_printf(m, "status:\t\tnot supported\n");
8288 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8289 seq_printf(m, "commands:\tlevel <level>");
8291 switch (fan_control_access_mode) {
8292 case TPACPI_FAN_WR_ACPI_SFAN:
8293 seq_printf(m, " (<level> is 0-7)\n");
8297 seq_printf(m, " (<level> is 0-7, "
8298 "auto, disengaged, full-speed)\n");
8303 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8304 seq_printf(m, "commands:\tenable, disable\n"
8305 "commands:\twatchdog <timeout> (<timeout> "
8306 "is 0 (off), 1-120 (seconds))\n");
8308 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8309 seq_printf(m, "commands:\tspeed <speed>"
8310 " (<speed> is 0-65535)\n");
8315 static int fan_write_cmd_level(const char *cmd, int *rc)
8319 if (strlencmp(cmd, "level auto") == 0)
8320 level = TP_EC_FAN_AUTO;
8321 else if ((strlencmp(cmd, "level disengaged") == 0) |
8322 (strlencmp(cmd, "level full-speed") == 0))
8323 level = TP_EC_FAN_FULLSPEED;
8324 else if (sscanf(cmd, "level %d", &level) != 1)
8327 *rc = fan_set_level_safe(level);
8329 printk(TPACPI_ERR "level command accepted for unsupported "
8330 "access mode %d", fan_control_access_mode);
8332 tpacpi_disclose_usertask("procfs fan",
8333 "set level to %d\n", level);
8338 static int fan_write_cmd_enable(const char *cmd, int *rc)
8340 if (strlencmp(cmd, "enable") != 0)
8343 *rc = fan_set_enable();
8345 printk(TPACPI_ERR "enable command accepted for unsupported "
8346 "access mode %d", fan_control_access_mode);
8348 tpacpi_disclose_usertask("procfs fan", "enable\n");
8353 static int fan_write_cmd_disable(const char *cmd, int *rc)
8355 if (strlencmp(cmd, "disable") != 0)
8358 *rc = fan_set_disable();
8360 printk(TPACPI_ERR "disable command accepted for unsupported "
8361 "access mode %d", fan_control_access_mode);
8363 tpacpi_disclose_usertask("procfs fan", "disable\n");
8368 static int fan_write_cmd_speed(const char *cmd, int *rc)
8373 * Support speed <low> <medium> <high> ? */
8375 if (sscanf(cmd, "speed %d", &speed) != 1)
8378 *rc = fan_set_speed(speed);
8380 printk(TPACPI_ERR "speed command accepted for unsupported "
8381 "access mode %d", fan_control_access_mode);
8383 tpacpi_disclose_usertask("procfs fan",
8384 "set speed to %d\n", speed);
8389 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8393 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8396 if (interval < 0 || interval > 120)
8399 fan_watchdog_maxinterval = interval;
8400 tpacpi_disclose_usertask("procfs fan",
8401 "set watchdog timer to %d\n",
8408 static int fan_write(char *buf)
8413 while (!rc && (cmd = next_cmd(&buf))) {
8414 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8415 fan_write_cmd_level(cmd, &rc)) &&
8416 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8417 (fan_write_cmd_enable(cmd, &rc) ||
8418 fan_write_cmd_disable(cmd, &rc) ||
8419 fan_write_cmd_watchdog(cmd, &rc))) &&
8420 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8421 fan_write_cmd_speed(cmd, &rc))
8425 fan_watchdog_reset();
8431 static struct ibm_struct fan_driver_data = {
8436 .suspend = fan_suspend,
8437 .resume = fan_resume,
8440 /****************************************************************************
8441 ****************************************************************************
8445 ****************************************************************************
8446 ****************************************************************************/
8449 * HKEY event callout for other subdrivers go here
8450 * (yes, it is ugly, but it is quick, safe, and gets the job done
8452 static void tpacpi_driver_event(const unsigned int hkey_event)
8454 if (ibm_backlight_device) {
8455 switch (hkey_event) {
8456 case TP_HKEY_EV_BRGHT_UP:
8457 case TP_HKEY_EV_BRGHT_DOWN:
8458 tpacpi_brightness_notify_change();
8462 switch (hkey_event) {
8463 case TP_HKEY_EV_VOL_UP:
8464 case TP_HKEY_EV_VOL_DOWN:
8465 case TP_HKEY_EV_VOL_MUTE:
8466 volume_alsa_notify_change();
8471 static void hotkey_driver_event(const unsigned int scancode)
8473 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8476 /* sysfs name ---------------------------------------------------------- */
8477 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8478 struct device_attribute *attr,
8481 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8484 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8485 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8487 /* --------------------------------------------------------------------- */
8490 static struct proc_dir_entry *proc_dir;
8493 * Module and infrastructure proble, init and exit handling
8496 static int force_load;
8498 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8499 static const char * __init str_supported(int is_supported)
8501 static char text_unsupported[] __initdata = "not supported";
8503 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8505 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8507 static void ibm_exit(struct ibm_struct *ibm)
8509 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8511 list_del_init(&ibm->all_drivers);
8513 if (ibm->flags.acpi_notify_installed) {
8514 dbg_printk(TPACPI_DBG_EXIT,
8515 "%s: acpi_remove_notify_handler\n", ibm->name);
8517 acpi_remove_notify_handler(*ibm->acpi->handle,
8519 dispatch_acpi_notify);
8520 ibm->flags.acpi_notify_installed = 0;
8521 ibm->flags.acpi_notify_installed = 0;
8524 if (ibm->flags.proc_created) {
8525 dbg_printk(TPACPI_DBG_EXIT,
8526 "%s: remove_proc_entry\n", ibm->name);
8527 remove_proc_entry(ibm->name, proc_dir);
8528 ibm->flags.proc_created = 0;
8531 if (ibm->flags.acpi_driver_registered) {
8532 dbg_printk(TPACPI_DBG_EXIT,
8533 "%s: acpi_bus_unregister_driver\n", ibm->name);
8535 acpi_bus_unregister_driver(ibm->acpi->driver);
8536 kfree(ibm->acpi->driver);
8537 ibm->acpi->driver = NULL;
8538 ibm->flags.acpi_driver_registered = 0;
8541 if (ibm->flags.init_called && ibm->exit) {
8543 ibm->flags.init_called = 0;
8546 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8549 static int __init ibm_init(struct ibm_init_struct *iibm)
8552 struct ibm_struct *ibm = iibm->data;
8553 struct proc_dir_entry *entry;
8555 BUG_ON(ibm == NULL);
8557 INIT_LIST_HEAD(&ibm->all_drivers);
8559 if (ibm->flags.experimental && !experimental)
8562 dbg_printk(TPACPI_DBG_INIT,
8563 "probing for %s\n", ibm->name);
8566 ret = iibm->init(iibm);
8568 return 0; /* probe failed */
8572 ibm->flags.init_called = 1;
8576 if (ibm->acpi->hid) {
8577 ret = register_tpacpi_subdriver(ibm);
8582 if (ibm->acpi->notify) {
8583 ret = setup_acpi_notify(ibm);
8584 if (ret == -ENODEV) {
8585 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8595 dbg_printk(TPACPI_DBG_INIT,
8596 "%s installed\n", ibm->name);
8599 mode_t mode = iibm->base_procfs_mode;
8605 entry = proc_create_data(ibm->name, mode, proc_dir,
8606 &dispatch_proc_fops, ibm);
8608 printk(TPACPI_ERR "unable to create proc entry %s\n",
8613 ibm->flags.proc_created = 1;
8616 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8621 dbg_printk(TPACPI_DBG_INIT,
8622 "%s: at error exit path with result %d\n",
8626 return (ret < 0)? ret : 0;
8631 static bool __pure __init tpacpi_is_fw_digit(const char c)
8633 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8636 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8637 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8640 return s && strlen(s) >= 8 &&
8641 tpacpi_is_fw_digit(s[0]) &&
8642 tpacpi_is_fw_digit(s[1]) &&
8643 s[2] == t && s[3] == 'T' &&
8644 tpacpi_is_fw_digit(s[4]) &&
8645 tpacpi_is_fw_digit(s[5]) &&
8646 s[6] == 'W' && s[7] == 'W';
8649 /* returns 0 - probe ok, or < 0 - probe error.
8650 * Probe ok doesn't mean thinkpad found.
8651 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8652 static int __must_check __init get_thinkpad_model_data(
8653 struct thinkpad_id_data *tp)
8655 const struct dmi_device *dev = NULL;
8656 char ec_fw_string[18];
8662 memset(tp, 0, sizeof(*tp));
8664 if (dmi_name_in_vendors("IBM"))
8665 tp->vendor = PCI_VENDOR_ID_IBM;
8666 else if (dmi_name_in_vendors("LENOVO"))
8667 tp->vendor = PCI_VENDOR_ID_LENOVO;
8671 s = dmi_get_system_info(DMI_BIOS_VERSION);
8672 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8673 if (s && !tp->bios_version_str)
8676 /* Really ancient ThinkPad 240X will fail this, which is fine */
8677 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8680 tp->bios_model = tp->bios_version_str[0]
8681 | (tp->bios_version_str[1] << 8);
8682 tp->bios_release = (tp->bios_version_str[4] << 8)
8683 | tp->bios_version_str[5];
8686 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8687 * X32 or newer, all Z series; Some models must have an
8688 * up-to-date BIOS or they will not be detected.
8690 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8692 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8693 if (sscanf(dev->name,
8694 "IBM ThinkPad Embedded Controller -[%17c",
8695 ec_fw_string) == 1) {
8696 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8697 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8699 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8700 if (!tp->ec_version_str)
8703 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8704 tp->ec_model = ec_fw_string[0]
8705 | (ec_fw_string[1] << 8);
8706 tp->ec_release = (ec_fw_string[4] << 8)
8709 printk(TPACPI_NOTICE
8710 "ThinkPad firmware release %s "
8711 "doesn't match the known patterns\n",
8713 printk(TPACPI_NOTICE
8714 "please report this to %s\n",
8721 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8722 if (s && !strnicmp(s, "ThinkPad", 8)) {
8723 tp->model_str = kstrdup(s, GFP_KERNEL);
8728 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8729 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8730 if (s && !tp->nummodel_str)
8736 static int __init probe_for_thinkpad(void)
8743 /* It would be dangerous to run the driver in this case */
8744 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8748 * Non-ancient models have better DMI tagging, but very old models
8749 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8751 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8752 (thinkpad_id.ec_model != 0) ||
8753 tpacpi_is_fw_known();
8755 /* ec is required because many other handles are relative to it */
8756 TPACPI_ACPIHANDLE_INIT(ec);
8760 "Not yet supported ThinkPad detected!\n");
8764 if (!is_thinkpad && !force_load)
8770 static void __init thinkpad_acpi_init_banner(void)
8772 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8773 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8775 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8776 (thinkpad_id.bios_version_str) ?
8777 thinkpad_id.bios_version_str : "unknown",
8778 (thinkpad_id.ec_version_str) ?
8779 thinkpad_id.ec_version_str : "unknown");
8781 BUG_ON(!thinkpad_id.vendor);
8783 if (thinkpad_id.model_str)
8784 printk(TPACPI_INFO "%s %s, model %s\n",
8785 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8786 "IBM" : ((thinkpad_id.vendor ==
8787 PCI_VENDOR_ID_LENOVO) ?
8788 "Lenovo" : "Unknown vendor"),
8789 thinkpad_id.model_str,
8790 (thinkpad_id.nummodel_str) ?
8791 thinkpad_id.nummodel_str : "unknown");
8794 /* Module init, exit, parameters */
8796 static struct ibm_init_struct ibms_init[] __initdata = {
8798 .data = &thinkpad_acpi_driver_data,
8801 .init = hotkey_init,
8802 .data = &hotkey_driver_data,
8805 .init = bluetooth_init,
8806 .data = &bluetooth_driver_data,
8810 .data = &wan_driver_data,
8814 .data = &uwb_driver_data,
8816 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8819 .base_procfs_mode = S_IRUSR,
8820 .data = &video_driver_data,
8825 .data = &light_driver_data,
8829 .data = &cmos_driver_data,
8833 .data = &led_driver_data,
8837 .data = &beep_driver_data,
8840 .init = thermal_init,
8841 .data = &thermal_driver_data,
8844 .data = &ecdump_driver_data,
8847 .init = brightness_init,
8848 .data = &brightness_driver_data,
8851 .init = volume_init,
8852 .data = &volume_driver_data,
8856 .data = &fan_driver_data,
8860 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8863 struct ibm_struct *ibm;
8865 if (!kp || !kp->name || !val)
8868 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8869 ibm = ibms_init[i].data;
8870 WARN_ON(ibm == NULL);
8872 if (!ibm || !ibm->name)
8875 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8876 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8878 strcpy(ibms_init[i].param, val);
8879 strcat(ibms_init[i].param, ",");
8887 module_param(experimental, int, 0444);
8888 MODULE_PARM_DESC(experimental,
8889 "Enables experimental features when non-zero");
8891 module_param_named(debug, dbg_level, uint, 0);
8892 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8894 module_param(force_load, bool, 0444);
8895 MODULE_PARM_DESC(force_load,
8896 "Attempts to load the driver even on a "
8897 "mis-identified ThinkPad when true");
8899 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8900 MODULE_PARM_DESC(fan_control,
8901 "Enables setting fan parameters features when true");
8903 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8904 MODULE_PARM_DESC(brightness_mode,
8905 "Selects brightness control strategy: "
8906 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8908 module_param(brightness_enable, uint, 0444);
8909 MODULE_PARM_DESC(brightness_enable,
8910 "Enables backlight control when 1, disables when 0");
8912 module_param(hotkey_report_mode, uint, 0444);
8913 MODULE_PARM_DESC(hotkey_report_mode,
8914 "used for backwards compatibility with userspace, "
8915 "see documentation");
8917 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8918 module_param_named(volume_mode, volume_mode, uint, 0444);
8919 MODULE_PARM_DESC(volume_mode,
8920 "Selects volume control strategy: "
8921 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8923 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8924 MODULE_PARM_DESC(volume_capabilities,
8925 "Selects the mixer capabilites: "
8926 "0=auto, 1=volume and mute, 2=mute only");
8928 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8929 MODULE_PARM_DESC(volume_control,
8930 "Enables software override for the console audio "
8931 "control when true");
8933 /* ALSA module API parameters */
8934 module_param_named(index, alsa_index, int, 0444);
8935 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8936 module_param_named(id, alsa_id, charp, 0444);
8937 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8938 module_param_named(enable, alsa_enable, bool, 0444);
8939 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8940 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8942 #define TPACPI_PARAM(feature) \
8943 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8944 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8945 "at module load, see documentation")
8947 TPACPI_PARAM(hotkey);
8948 TPACPI_PARAM(bluetooth);
8949 TPACPI_PARAM(video);
8950 TPACPI_PARAM(light);
8954 TPACPI_PARAM(ecdump);
8955 TPACPI_PARAM(brightness);
8956 TPACPI_PARAM(volume);
8959 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8960 module_param(dbg_wlswemul, uint, 0444);
8961 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8962 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8963 MODULE_PARM_DESC(wlsw_state,
8964 "Initial state of the emulated WLSW switch");
8966 module_param(dbg_bluetoothemul, uint, 0444);
8967 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8968 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8969 MODULE_PARM_DESC(bluetooth_state,
8970 "Initial state of the emulated bluetooth switch");
8972 module_param(dbg_wwanemul, uint, 0444);
8973 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8974 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8975 MODULE_PARM_DESC(wwan_state,
8976 "Initial state of the emulated WWAN switch");
8978 module_param(dbg_uwbemul, uint, 0444);
8979 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8980 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8981 MODULE_PARM_DESC(uwb_state,
8982 "Initial state of the emulated UWB switch");
8985 static void thinkpad_acpi_module_exit(void)
8987 struct ibm_struct *ibm, *itmp;
8989 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8991 list_for_each_entry_safe_reverse(ibm, itmp,
8992 &tpacpi_all_drivers,
8997 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8999 if (tpacpi_inputdev) {
9000 if (tp_features.input_device_registered)
9001 input_unregister_device(tpacpi_inputdev);
9003 input_free_device(tpacpi_inputdev);
9007 hwmon_device_unregister(tpacpi_hwmon);
9009 if (tp_features.sensors_pdev_attrs_registered)
9010 device_remove_file(&tpacpi_sensors_pdev->dev,
9011 &dev_attr_thinkpad_acpi_pdev_name);
9012 if (tpacpi_sensors_pdev)
9013 platform_device_unregister(tpacpi_sensors_pdev);
9015 platform_device_unregister(tpacpi_pdev);
9017 if (tp_features.sensors_pdrv_attrs_registered)
9018 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9019 if (tp_features.platform_drv_attrs_registered)
9020 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9022 if (tp_features.sensors_pdrv_registered)
9023 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9025 if (tp_features.platform_drv_registered)
9026 platform_driver_unregister(&tpacpi_pdriver);
9029 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9032 destroy_workqueue(tpacpi_wq);
9034 kfree(thinkpad_id.bios_version_str);
9035 kfree(thinkpad_id.ec_version_str);
9036 kfree(thinkpad_id.model_str);
9040 static int __init thinkpad_acpi_module_init(void)
9044 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9046 /* Parameter checking */
9047 if (hotkey_report_mode > 2)
9050 /* Driver-level probe */
9052 ret = get_thinkpad_model_data(&thinkpad_id);
9055 "unable to get DMI data: %d\n", ret);
9056 thinkpad_acpi_module_exit();
9059 ret = probe_for_thinkpad();
9061 thinkpad_acpi_module_exit();
9065 /* Driver initialization */
9067 thinkpad_acpi_init_banner();
9068 tpacpi_check_outdated_fw();
9070 TPACPI_ACPIHANDLE_INIT(ecrd);
9071 TPACPI_ACPIHANDLE_INIT(ecwr);
9073 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9075 thinkpad_acpi_module_exit();
9079 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9082 "unable to create proc dir " TPACPI_PROC_DIR);
9083 thinkpad_acpi_module_exit();
9087 ret = platform_driver_register(&tpacpi_pdriver);
9090 "unable to register main platform driver\n");
9091 thinkpad_acpi_module_exit();
9094 tp_features.platform_drv_registered = 1;
9096 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9099 "unable to register hwmon platform driver\n");
9100 thinkpad_acpi_module_exit();
9103 tp_features.sensors_pdrv_registered = 1;
9105 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9107 tp_features.platform_drv_attrs_registered = 1;
9108 ret = tpacpi_create_driver_attributes(
9109 &tpacpi_hwmon_pdriver.driver);
9113 "unable to create sysfs driver attributes\n");
9114 thinkpad_acpi_module_exit();
9117 tp_features.sensors_pdrv_attrs_registered = 1;
9120 /* Device initialization */
9121 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9123 if (IS_ERR(tpacpi_pdev)) {
9124 ret = PTR_ERR(tpacpi_pdev);
9126 printk(TPACPI_ERR "unable to register platform device\n");
9127 thinkpad_acpi_module_exit();
9130 tpacpi_sensors_pdev = platform_device_register_simple(
9131 TPACPI_HWMON_DRVR_NAME,
9133 if (IS_ERR(tpacpi_sensors_pdev)) {
9134 ret = PTR_ERR(tpacpi_sensors_pdev);
9135 tpacpi_sensors_pdev = NULL;
9137 "unable to register hwmon platform device\n");
9138 thinkpad_acpi_module_exit();
9141 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9142 &dev_attr_thinkpad_acpi_pdev_name);
9145 "unable to create sysfs hwmon device attributes\n");
9146 thinkpad_acpi_module_exit();
9149 tp_features.sensors_pdev_attrs_registered = 1;
9150 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9151 if (IS_ERR(tpacpi_hwmon)) {
9152 ret = PTR_ERR(tpacpi_hwmon);
9153 tpacpi_hwmon = NULL;
9154 printk(TPACPI_ERR "unable to register hwmon device\n");
9155 thinkpad_acpi_module_exit();
9158 mutex_init(&tpacpi_inputdev_send_mutex);
9159 tpacpi_inputdev = input_allocate_device();
9160 if (!tpacpi_inputdev) {
9161 printk(TPACPI_ERR "unable to allocate input device\n");
9162 thinkpad_acpi_module_exit();
9165 /* Prepare input device, but don't register */
9166 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9167 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9168 tpacpi_inputdev->id.bustype = BUS_HOST;
9169 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9170 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9171 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9172 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9175 /* Init subdriver dependencies */
9176 tpacpi_detect_brightness_capabilities();
9178 /* Init subdrivers */
9179 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9180 ret = ibm_init(&ibms_init[i]);
9181 if (ret >= 0 && *ibms_init[i].param)
9182 ret = ibms_init[i].data->write(ibms_init[i].param);
9184 thinkpad_acpi_module_exit();
9189 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9191 ret = input_register_device(tpacpi_inputdev);
9193 printk(TPACPI_ERR "unable to register input device\n");
9194 thinkpad_acpi_module_exit();
9197 tp_features.input_device_registered = 1;
9203 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9206 * This will autoload the driver in almost every ThinkPad
9207 * in widespread use.
9209 * Only _VERY_ old models, like the 240, 240x and 570 lack
9210 * the HKEY event interface.
9212 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9215 * DMI matching for module autoloading
9217 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9218 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9220 * Only models listed in thinkwiki will be supported, so add yours
9221 * if it is not there yet.
9223 #define IBM_BIOS_MODULE_ALIAS(__type) \
9224 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9226 /* Ancient thinkpad BIOSes have to be identified by
9227 * BIOS type or model number, and there are far less
9228 * BIOS types than model numbers... */
9229 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9231 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9232 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9233 MODULE_DESCRIPTION(TPACPI_DESC);
9234 MODULE_VERSION(TPACPI_VERSION);
9235 MODULE_LICENSE("GPL");
9237 module_init(thinkpad_acpi_module_init);
9238 module_exit(thinkpad_acpi_module_exit);