thinkpad-acpi: handle some new HKEY 0x60xx events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobcdcd2388af2b5fa849bf9a099b30b24b887a8ac6
1 /*
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
21 * 02110-1301, USA.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #define TPACPI_VERSION "0.24"
27 #define TPACPI_SYSFS_VERSION 0x020700
30 * Changelog:
31 * 2007-10-20 changelog trimmed down
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
65 #include <linux/nvram.h>
66 #include <linux/proc_fs.h>
67 #include <linux/seq_file.h>
68 #include <linux/sysfs.h>
69 #include <linux/backlight.h>
70 #include <linux/fb.h>
71 #include <linux/platform_device.h>
72 #include <linux/hwmon.h>
73 #include <linux/hwmon-sysfs.h>
74 #include <linux/input.h>
75 #include <linux/leds.h>
76 #include <linux/rfkill.h>
77 #include <asm/uaccess.h>
79 #include <linux/dmi.h>
80 #include <linux/jiffies.h>
81 #include <linux/workqueue.h>
83 #include <sound/core.h>
84 #include <sound/control.h>
85 #include <sound/initval.h>
87 #include <acpi/acpi_drivers.h>
89 #include <linux/pci_ids.h>
92 /* ThinkPad CMOS commands */
93 #define TP_CMOS_VOLUME_DOWN 0
94 #define TP_CMOS_VOLUME_UP 1
95 #define TP_CMOS_VOLUME_MUTE 2
96 #define TP_CMOS_BRIGHTNESS_UP 4
97 #define TP_CMOS_BRIGHTNESS_DOWN 5
98 #define TP_CMOS_THINKLIGHT_ON 12
99 #define TP_CMOS_THINKLIGHT_OFF 13
101 /* NVRAM Addresses */
102 enum tp_nvram_addr {
103 TP_NVRAM_ADDR_HK2 = 0x57,
104 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
105 TP_NVRAM_ADDR_VIDEO = 0x59,
106 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
107 TP_NVRAM_ADDR_MIXER = 0x60,
110 /* NVRAM bit masks */
111 enum {
112 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
113 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
114 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
115 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
116 TP_NVRAM_MASK_THINKLIGHT = 0x10,
117 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
118 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
119 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
120 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
121 TP_NVRAM_MASK_MUTE = 0x40,
122 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
123 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
124 TP_NVRAM_POS_LEVEL_VOLUME = 0,
127 /* Misc NVRAM-related */
128 enum {
129 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
132 /* ACPI HIDs */
133 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
134 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
135 #define TPACPI_ACPI_EC_HID "PNP0C09"
137 /* Input IDs */
138 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
139 #define TPACPI_HKEY_INPUT_VERSION 0x4101
141 /* ACPI \WGSV commands */
142 enum {
143 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
144 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
145 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
146 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
149 /* TP_ACPI_WGSV_GET_STATE bits */
150 enum {
151 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
152 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
153 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
154 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
155 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
156 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
157 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
158 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
159 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
160 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
163 /* HKEY events */
164 enum tpacpi_hkey_event_t {
165 /* Hotkey-related */
166 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
167 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
168 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
169 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
170 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
171 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
173 /* Reasons for waking up from S3/S4 */
174 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
175 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
176 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
177 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
178 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
179 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
181 /* Auto-sleep after eject request */
182 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
183 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
185 /* Misc bay events */
186 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
188 /* User-interface events */
189 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
190 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
191 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
192 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
193 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
194 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
195 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
197 /* Key-related user-interface events */
198 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
199 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
201 /* Thermal events */
202 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
203 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
204 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
205 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
206 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
208 TP_HKEY_EV_UNK_6040 = 0x6040, /* Related to AC change?
209 some sort of APM hint,
210 W520 */
212 /* Misc */
213 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
216 /****************************************************************************
217 * Main driver
220 #define TPACPI_NAME "thinkpad"
221 #define TPACPI_DESC "ThinkPad ACPI Extras"
222 #define TPACPI_FILE TPACPI_NAME "_acpi"
223 #define TPACPI_URL "http://ibm-acpi.sf.net/"
224 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
226 #define TPACPI_PROC_DIR "ibm"
227 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
228 #define TPACPI_DRVR_NAME TPACPI_FILE
229 #define TPACPI_DRVR_SHORTNAME "tpacpi"
230 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
232 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
233 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
235 #define TPACPI_MAX_ACPI_ARGS 3
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_acpimode:1;
309 u32 bright_unkfw:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul;
377 static int tpacpi_wlsw_emulstate;
378 static int dbg_bluetoothemul;
379 static int tpacpi_bluetooth_emulstate;
380 static int dbg_wwanemul;
381 static int tpacpi_wwan_emulstate;
382 static int dbg_uwbemul;
383 static int tpacpi_uwb_emulstate;
384 #endif
387 /*************************************************************************
388 * Debugging helpers
391 #define dbg_printk(a_dbg_level, format, arg...) \
392 do { \
393 if (dbg_level & (a_dbg_level)) \
394 printk(KERN_DEBUG pr_fmt("%s: " format), \
395 __func__, ##arg); \
396 } while (0)
398 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
399 #define vdbg_printk dbg_printk
400 static const char *str_supported(int is_supported);
401 #else
402 static inline const char *str_supported(int is_supported) { return ""; }
403 #define vdbg_printk(a_dbg_level, format, arg...) \
404 no_printk(format, ##arg)
405 #endif
407 static void tpacpi_log_usertask(const char * const what)
409 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
410 what, task_tgid_vnr(current));
413 #define tpacpi_disclose_usertask(what, format, arg...) \
414 do { \
415 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
416 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
417 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
418 what, task_tgid_vnr(current), ## arg); \
420 } while (0)
423 * Quirk handling helpers
425 * ThinkPad IDs and versions seen in the field so far
426 * are two-characters from the set [0-9A-Z], i.e. base 36.
428 * We use values well outside that range as specials.
431 #define TPACPI_MATCH_ANY 0xffffU
432 #define TPACPI_MATCH_UNKNOWN 0U
434 /* TPID('1', 'Y') == 0x5931 */
435 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
437 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_IBM, \
439 .bios = TPID(__id1, __id2), \
440 .ec = TPACPI_MATCH_ANY, \
441 .quirks = (__quirk) }
443 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
444 { .vendor = PCI_VENDOR_ID_LENOVO, \
445 .bios = TPID(__id1, __id2), \
446 .ec = TPACPI_MATCH_ANY, \
447 .quirks = (__quirk) }
449 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
450 { .vendor = PCI_VENDOR_ID_LENOVO, \
451 .bios = TPACPI_MATCH_ANY, \
452 .ec = TPID(__id1, __id2), \
453 .quirks = (__quirk) }
455 struct tpacpi_quirk {
456 unsigned int vendor;
457 u16 bios;
458 u16 ec;
459 unsigned long quirks;
463 * tpacpi_check_quirks() - search BIOS/EC version on a list
464 * @qlist: array of &struct tpacpi_quirk
465 * @qlist_size: number of elements in @qlist
467 * Iterates over a quirks list until one is found that matches the
468 * ThinkPad's vendor, BIOS and EC model.
470 * Returns 0 if nothing matches, otherwise returns the quirks field of
471 * the matching &struct tpacpi_quirk entry.
473 * The match criteria is: vendor, ec and bios much match.
475 static unsigned long __init tpacpi_check_quirks(
476 const struct tpacpi_quirk *qlist,
477 unsigned int qlist_size)
479 while (qlist_size) {
480 if ((qlist->vendor == thinkpad_id.vendor ||
481 qlist->vendor == TPACPI_MATCH_ANY) &&
482 (qlist->bios == thinkpad_id.bios_model ||
483 qlist->bios == TPACPI_MATCH_ANY) &&
484 (qlist->ec == thinkpad_id.ec_model ||
485 qlist->ec == TPACPI_MATCH_ANY))
486 return qlist->quirks;
488 qlist_size--;
489 qlist++;
491 return 0;
494 static inline bool __pure __init tpacpi_is_lenovo(void)
496 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
499 static inline bool __pure __init tpacpi_is_ibm(void)
501 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
504 /****************************************************************************
505 ****************************************************************************
507 * ACPI Helpers and device model
509 ****************************************************************************
510 ****************************************************************************/
512 /*************************************************************************
513 * ACPI basic handles
516 static acpi_handle root_handle;
517 static acpi_handle ec_handle;
519 #define TPACPI_HANDLE(object, parent, paths...) \
520 static acpi_handle object##_handle; \
521 static const acpi_handle *object##_parent __initdata = \
522 &parent##_handle; \
523 static char *object##_paths[] __initdata = { paths }
525 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
526 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
528 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
529 /* T4x, X31, X40 */
530 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
531 "\\CMS", /* R40, R40e */
532 ); /* all others */
534 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
535 "^HKEY", /* R30, R31 */
536 "HKEY", /* all others */
537 ); /* 570 */
539 /*************************************************************************
540 * ACPI helpers
543 static int acpi_evalf(acpi_handle handle,
544 void *res, char *method, char *fmt, ...)
546 char *fmt0 = fmt;
547 struct acpi_object_list params;
548 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
549 struct acpi_buffer result, *resultp;
550 union acpi_object out_obj;
551 acpi_status status;
552 va_list ap;
553 char res_type;
554 int success;
555 int quiet;
557 if (!*fmt) {
558 pr_err("acpi_evalf() called with empty format\n");
559 return 0;
562 if (*fmt == 'q') {
563 quiet = 1;
564 fmt++;
565 } else
566 quiet = 0;
568 res_type = *(fmt++);
570 params.count = 0;
571 params.pointer = &in_objs[0];
573 va_start(ap, fmt);
574 while (*fmt) {
575 char c = *(fmt++);
576 switch (c) {
577 case 'd': /* int */
578 in_objs[params.count].integer.value = va_arg(ap, int);
579 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
580 break;
581 /* add more types as needed */
582 default:
583 pr_err("acpi_evalf() called "
584 "with invalid format character '%c'\n", c);
585 va_end(ap);
586 return 0;
589 va_end(ap);
591 if (res_type != 'v') {
592 result.length = sizeof(out_obj);
593 result.pointer = &out_obj;
594 resultp = &result;
595 } else
596 resultp = NULL;
598 status = acpi_evaluate_object(handle, method, &params, resultp);
600 switch (res_type) {
601 case 'd': /* int */
602 success = (status == AE_OK &&
603 out_obj.type == ACPI_TYPE_INTEGER);
604 if (success && res)
605 *(int *)res = out_obj.integer.value;
606 break;
607 case 'v': /* void */
608 success = status == AE_OK;
609 break;
610 /* add more types as needed */
611 default:
612 pr_err("acpi_evalf() called "
613 "with invalid format character '%c'\n", res_type);
614 return 0;
617 if (!success && !quiet)
618 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
619 method, fmt0, acpi_format_exception(status));
621 return success;
624 static int acpi_ec_read(int i, u8 *p)
626 int v;
628 if (ecrd_handle) {
629 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
630 return 0;
631 *p = v;
632 } else {
633 if (ec_read(i, p) < 0)
634 return 0;
637 return 1;
640 static int acpi_ec_write(int i, u8 v)
642 if (ecwr_handle) {
643 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
644 return 0;
645 } else {
646 if (ec_write(i, v) < 0)
647 return 0;
650 return 1;
653 static int issue_thinkpad_cmos_command(int cmos_cmd)
655 if (!cmos_handle)
656 return -ENXIO;
658 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
659 return -EIO;
661 return 0;
664 /*************************************************************************
665 * ACPI device model
668 #define TPACPI_ACPIHANDLE_INIT(object) \
669 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
670 object##_paths, ARRAY_SIZE(object##_paths))
672 static void __init drv_acpi_handle_init(const char *name,
673 acpi_handle *handle, const acpi_handle parent,
674 char **paths, const int num_paths)
676 int i;
677 acpi_status status;
679 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
680 name);
682 for (i = 0; i < num_paths; i++) {
683 status = acpi_get_handle(parent, paths[i], handle);
684 if (ACPI_SUCCESS(status)) {
685 dbg_printk(TPACPI_DBG_INIT,
686 "Found ACPI handle %s for %s\n",
687 paths[i], name);
688 return;
692 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
693 name);
694 *handle = NULL;
697 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
698 u32 level, void *context, void **return_value)
700 *(acpi_handle *)return_value = handle;
702 return AE_CTRL_TERMINATE;
705 static void __init tpacpi_acpi_handle_locate(const char *name,
706 const char *hid,
707 acpi_handle *handle)
709 acpi_status status;
710 acpi_handle device_found;
712 BUG_ON(!name || !hid || !handle);
713 vdbg_printk(TPACPI_DBG_INIT,
714 "trying to locate ACPI handle for %s, using HID %s\n",
715 name, hid);
717 memset(&device_found, 0, sizeof(device_found));
718 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
719 (void *)name, &device_found);
721 *handle = NULL;
723 if (ACPI_SUCCESS(status)) {
724 *handle = device_found;
725 dbg_printk(TPACPI_DBG_INIT,
726 "Found ACPI handle for %s\n", name);
727 } else {
728 vdbg_printk(TPACPI_DBG_INIT,
729 "Could not locate an ACPI handle for %s: %s\n",
730 name, acpi_format_exception(status));
734 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
736 struct ibm_struct *ibm = data;
738 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
739 return;
741 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
742 return;
744 ibm->acpi->notify(ibm, event);
747 static int __init setup_acpi_notify(struct ibm_struct *ibm)
749 acpi_status status;
750 int rc;
752 BUG_ON(!ibm->acpi);
754 if (!*ibm->acpi->handle)
755 return 0;
757 vdbg_printk(TPACPI_DBG_INIT,
758 "setting up ACPI notify for %s\n", ibm->name);
760 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
761 if (rc < 0) {
762 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
763 return -ENODEV;
766 ibm->acpi->device->driver_data = ibm;
767 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
768 TPACPI_ACPI_EVENT_PREFIX,
769 ibm->name);
771 status = acpi_install_notify_handler(*ibm->acpi->handle,
772 ibm->acpi->type, dispatch_acpi_notify, ibm);
773 if (ACPI_FAILURE(status)) {
774 if (status == AE_ALREADY_EXISTS) {
775 pr_notice("another device driver is already "
776 "handling %s events\n", ibm->name);
777 } else {
778 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
779 ibm->name, acpi_format_exception(status));
781 return -ENODEV;
783 ibm->flags.acpi_notify_installed = 1;
784 return 0;
787 static int __init tpacpi_device_add(struct acpi_device *device)
789 return 0;
792 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
794 int rc;
796 dbg_printk(TPACPI_DBG_INIT,
797 "registering %s as an ACPI driver\n", ibm->name);
799 BUG_ON(!ibm->acpi);
801 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
802 if (!ibm->acpi->driver) {
803 pr_err("failed to allocate memory for ibm->acpi->driver\n");
804 return -ENOMEM;
807 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
808 ibm->acpi->driver->ids = ibm->acpi->hid;
810 ibm->acpi->driver->ops.add = &tpacpi_device_add;
812 rc = acpi_bus_register_driver(ibm->acpi->driver);
813 if (rc < 0) {
814 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
815 ibm->name, rc);
816 kfree(ibm->acpi->driver);
817 ibm->acpi->driver = NULL;
818 } else if (!rc)
819 ibm->flags.acpi_driver_registered = 1;
821 return rc;
825 /****************************************************************************
826 ****************************************************************************
828 * Procfs Helpers
830 ****************************************************************************
831 ****************************************************************************/
833 static int dispatch_proc_show(struct seq_file *m, void *v)
835 struct ibm_struct *ibm = m->private;
837 if (!ibm || !ibm->read)
838 return -EINVAL;
839 return ibm->read(m);
842 static int dispatch_proc_open(struct inode *inode, struct file *file)
844 return single_open(file, dispatch_proc_show, PDE(inode)->data);
847 static ssize_t dispatch_proc_write(struct file *file,
848 const char __user *userbuf,
849 size_t count, loff_t *pos)
851 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
852 char *kernbuf;
853 int ret;
855 if (!ibm || !ibm->write)
856 return -EINVAL;
857 if (count > PAGE_SIZE - 2)
858 return -EINVAL;
860 kernbuf = kmalloc(count + 2, GFP_KERNEL);
861 if (!kernbuf)
862 return -ENOMEM;
864 if (copy_from_user(kernbuf, userbuf, count)) {
865 kfree(kernbuf);
866 return -EFAULT;
869 kernbuf[count] = 0;
870 strcat(kernbuf, ",");
871 ret = ibm->write(kernbuf);
872 if (ret == 0)
873 ret = count;
875 kfree(kernbuf);
877 return ret;
880 static const struct file_operations dispatch_proc_fops = {
881 .owner = THIS_MODULE,
882 .open = dispatch_proc_open,
883 .read = seq_read,
884 .llseek = seq_lseek,
885 .release = single_release,
886 .write = dispatch_proc_write,
889 static char *next_cmd(char **cmds)
891 char *start = *cmds;
892 char *end;
894 while ((end = strchr(start, ',')) && end == start)
895 start = end + 1;
897 if (!end)
898 return NULL;
900 *end = 0;
901 *cmds = end + 1;
902 return start;
906 /****************************************************************************
907 ****************************************************************************
909 * Device model: input, hwmon and platform
911 ****************************************************************************
912 ****************************************************************************/
914 static struct platform_device *tpacpi_pdev;
915 static struct platform_device *tpacpi_sensors_pdev;
916 static struct device *tpacpi_hwmon;
917 static struct input_dev *tpacpi_inputdev;
918 static struct mutex tpacpi_inputdev_send_mutex;
919 static LIST_HEAD(tpacpi_all_drivers);
921 static int tpacpi_suspend_handler(struct platform_device *pdev,
922 pm_message_t state)
924 struct ibm_struct *ibm, *itmp;
926 list_for_each_entry_safe(ibm, itmp,
927 &tpacpi_all_drivers,
928 all_drivers) {
929 if (ibm->suspend)
930 (ibm->suspend)(state);
933 return 0;
936 static int tpacpi_resume_handler(struct platform_device *pdev)
938 struct ibm_struct *ibm, *itmp;
940 list_for_each_entry_safe(ibm, itmp,
941 &tpacpi_all_drivers,
942 all_drivers) {
943 if (ibm->resume)
944 (ibm->resume)();
947 return 0;
950 static void tpacpi_shutdown_handler(struct platform_device *pdev)
952 struct ibm_struct *ibm, *itmp;
954 list_for_each_entry_safe(ibm, itmp,
955 &tpacpi_all_drivers,
956 all_drivers) {
957 if (ibm->shutdown)
958 (ibm->shutdown)();
962 static struct platform_driver tpacpi_pdriver = {
963 .driver = {
964 .name = TPACPI_DRVR_NAME,
965 .owner = THIS_MODULE,
967 .suspend = tpacpi_suspend_handler,
968 .resume = tpacpi_resume_handler,
969 .shutdown = tpacpi_shutdown_handler,
972 static struct platform_driver tpacpi_hwmon_pdriver = {
973 .driver = {
974 .name = TPACPI_HWMON_DRVR_NAME,
975 .owner = THIS_MODULE,
979 /*************************************************************************
980 * sysfs support helpers
983 struct attribute_set {
984 unsigned int members, max_members;
985 struct attribute_group group;
988 struct attribute_set_obj {
989 struct attribute_set s;
990 struct attribute *a;
991 } __attribute__((packed));
993 static struct attribute_set *create_attr_set(unsigned int max_members,
994 const char *name)
996 struct attribute_set_obj *sobj;
998 if (max_members == 0)
999 return NULL;
1001 /* Allocates space for implicit NULL at the end too */
1002 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1003 max_members * sizeof(struct attribute *),
1004 GFP_KERNEL);
1005 if (!sobj)
1006 return NULL;
1007 sobj->s.max_members = max_members;
1008 sobj->s.group.attrs = &sobj->a;
1009 sobj->s.group.name = name;
1011 return &sobj->s;
1014 #define destroy_attr_set(_set) \
1015 kfree(_set);
1017 /* not multi-threaded safe, use it in a single thread per set */
1018 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1020 if (!s || !attr)
1021 return -EINVAL;
1023 if (s->members >= s->max_members)
1024 return -ENOMEM;
1026 s->group.attrs[s->members] = attr;
1027 s->members++;
1029 return 0;
1032 static int add_many_to_attr_set(struct attribute_set *s,
1033 struct attribute **attr,
1034 unsigned int count)
1036 int i, res;
1038 for (i = 0; i < count; i++) {
1039 res = add_to_attr_set(s, attr[i]);
1040 if (res)
1041 return res;
1044 return 0;
1047 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1049 sysfs_remove_group(kobj, &s->group);
1050 destroy_attr_set(s);
1053 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1054 sysfs_create_group(_kobj, &_attr_set->group)
1056 static int parse_strtoul(const char *buf,
1057 unsigned long max, unsigned long *value)
1059 char *endp;
1061 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1062 endp = skip_spaces(endp);
1063 if (*endp || *value > max)
1064 return -EINVAL;
1066 return 0;
1069 static void tpacpi_disable_brightness_delay(void)
1071 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1072 pr_notice("ACPI backlight control delay disabled\n");
1075 static void printk_deprecated_attribute(const char * const what,
1076 const char * const details)
1078 tpacpi_log_usertask("deprecated sysfs attribute");
1079 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1080 "will be removed. %s\n",
1081 what, details);
1084 /*************************************************************************
1085 * rfkill and radio control support helpers
1089 * ThinkPad-ACPI firmware handling model:
1091 * WLSW (master wireless switch) is event-driven, and is common to all
1092 * firmware-controlled radios. It cannot be controlled, just monitored,
1093 * as expected. It overrides all radio state in firmware
1095 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1096 * (TODO: verify how WLSW interacts with the returned radio state).
1098 * The only time there are shadow radio state changes, is when
1099 * masked-off hotkeys are used.
1103 * Internal driver API for radio state:
1105 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1106 * bool: true means radio blocked (off)
1108 enum tpacpi_rfkill_state {
1109 TPACPI_RFK_RADIO_OFF = 0,
1110 TPACPI_RFK_RADIO_ON
1113 /* rfkill switches */
1114 enum tpacpi_rfk_id {
1115 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1116 TPACPI_RFK_WWAN_SW_ID,
1117 TPACPI_RFK_UWB_SW_ID,
1118 TPACPI_RFK_SW_MAX
1121 static const char *tpacpi_rfkill_names[] = {
1122 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1123 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1124 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1125 [TPACPI_RFK_SW_MAX] = NULL
1128 /* ThinkPad-ACPI rfkill subdriver */
1129 struct tpacpi_rfk {
1130 struct rfkill *rfkill;
1131 enum tpacpi_rfk_id id;
1132 const struct tpacpi_rfk_ops *ops;
1135 struct tpacpi_rfk_ops {
1136 /* firmware interface */
1137 int (*get_status)(void);
1138 int (*set_status)(const enum tpacpi_rfkill_state);
1141 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1143 /* Query FW and update rfkill sw state for a given rfkill switch */
1144 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1146 int status;
1148 if (!tp_rfk)
1149 return -ENODEV;
1151 status = (tp_rfk->ops->get_status)();
1152 if (status < 0)
1153 return status;
1155 rfkill_set_sw_state(tp_rfk->rfkill,
1156 (status == TPACPI_RFK_RADIO_OFF));
1158 return status;
1161 /* Query FW and update rfkill sw state for all rfkill switches */
1162 static void tpacpi_rfk_update_swstate_all(void)
1164 unsigned int i;
1166 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1167 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1171 * Sync the HW-blocking state of all rfkill switches,
1172 * do notice it causes the rfkill core to schedule uevents
1174 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1176 unsigned int i;
1177 struct tpacpi_rfk *tp_rfk;
1179 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1180 tp_rfk = tpacpi_rfkill_switches[i];
1181 if (tp_rfk) {
1182 if (rfkill_set_hw_state(tp_rfk->rfkill,
1183 blocked)) {
1184 /* ignore -- we track sw block */
1190 /* Call to get the WLSW state from the firmware */
1191 static int hotkey_get_wlsw(void);
1193 /* Call to query WLSW state and update all rfkill switches */
1194 static bool tpacpi_rfk_check_hwblock_state(void)
1196 int res = hotkey_get_wlsw();
1197 int hw_blocked;
1199 /* When unknown or unsupported, we have to assume it is unblocked */
1200 if (res < 0)
1201 return false;
1203 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1204 tpacpi_rfk_update_hwblock_state(hw_blocked);
1206 return hw_blocked;
1209 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1211 struct tpacpi_rfk *tp_rfk = data;
1212 int res;
1214 dbg_printk(TPACPI_DBG_RFKILL,
1215 "request to change radio state to %s\n",
1216 blocked ? "blocked" : "unblocked");
1218 /* try to set radio state */
1219 res = (tp_rfk->ops->set_status)(blocked ?
1220 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1222 /* and update the rfkill core with whatever the FW really did */
1223 tpacpi_rfk_update_swstate(tp_rfk);
1225 return (res < 0) ? res : 0;
1228 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1229 .set_block = tpacpi_rfk_hook_set_block,
1232 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1233 const struct tpacpi_rfk_ops *tp_rfkops,
1234 const enum rfkill_type rfktype,
1235 const char *name,
1236 const bool set_default)
1238 struct tpacpi_rfk *atp_rfk;
1239 int res;
1240 bool sw_state = false;
1241 bool hw_state;
1242 int sw_status;
1244 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1246 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1247 if (atp_rfk)
1248 atp_rfk->rfkill = rfkill_alloc(name,
1249 &tpacpi_pdev->dev,
1250 rfktype,
1251 &tpacpi_rfk_rfkill_ops,
1252 atp_rfk);
1253 if (!atp_rfk || !atp_rfk->rfkill) {
1254 pr_err("failed to allocate memory for rfkill class\n");
1255 kfree(atp_rfk);
1256 return -ENOMEM;
1259 atp_rfk->id = id;
1260 atp_rfk->ops = tp_rfkops;
1262 sw_status = (tp_rfkops->get_status)();
1263 if (sw_status < 0) {
1264 pr_err("failed to read initial state for %s, error %d\n",
1265 name, sw_status);
1266 } else {
1267 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1268 if (set_default) {
1269 /* try to keep the initial state, since we ask the
1270 * firmware to preserve it across S5 in NVRAM */
1271 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1274 hw_state = tpacpi_rfk_check_hwblock_state();
1275 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1277 res = rfkill_register(atp_rfk->rfkill);
1278 if (res < 0) {
1279 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1280 rfkill_destroy(atp_rfk->rfkill);
1281 kfree(atp_rfk);
1282 return res;
1285 tpacpi_rfkill_switches[id] = atp_rfk;
1287 pr_info("rfkill switch %s: radio is %sblocked\n",
1288 name, (sw_state || hw_state) ? "" : "un");
1289 return 0;
1292 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1294 struct tpacpi_rfk *tp_rfk;
1296 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1298 tp_rfk = tpacpi_rfkill_switches[id];
1299 if (tp_rfk) {
1300 rfkill_unregister(tp_rfk->rfkill);
1301 rfkill_destroy(tp_rfk->rfkill);
1302 tpacpi_rfkill_switches[id] = NULL;
1303 kfree(tp_rfk);
1307 static void printk_deprecated_rfkill_attribute(const char * const what)
1309 printk_deprecated_attribute(what,
1310 "Please switch to generic rfkill before year 2010");
1313 /* sysfs <radio> enable ------------------------------------------------ */
1314 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 char *buf)
1318 int status;
1320 printk_deprecated_rfkill_attribute(attr->attr.name);
1322 /* This is in the ABI... */
1323 if (tpacpi_rfk_check_hwblock_state()) {
1324 status = TPACPI_RFK_RADIO_OFF;
1325 } else {
1326 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1327 if (status < 0)
1328 return status;
1331 return snprintf(buf, PAGE_SIZE, "%d\n",
1332 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1335 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1336 struct device_attribute *attr,
1337 const char *buf, size_t count)
1339 unsigned long t;
1340 int res;
1342 printk_deprecated_rfkill_attribute(attr->attr.name);
1344 if (parse_strtoul(buf, 1, &t))
1345 return -EINVAL;
1347 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state() && !!t)
1351 return -EPERM;
1353 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1354 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1355 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1357 return (res < 0) ? res : count;
1360 /* procfs -------------------------------------------------------------- */
1361 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1363 if (id >= TPACPI_RFK_SW_MAX)
1364 seq_printf(m, "status:\t\tnot supported\n");
1365 else {
1366 int status;
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state()) {
1370 status = TPACPI_RFK_RADIO_OFF;
1371 } else {
1372 status = tpacpi_rfk_update_swstate(
1373 tpacpi_rfkill_switches[id]);
1374 if (status < 0)
1375 return status;
1378 seq_printf(m, "status:\t\t%s\n",
1379 (status == TPACPI_RFK_RADIO_ON) ?
1380 "enabled" : "disabled");
1381 seq_printf(m, "commands:\tenable, disable\n");
1384 return 0;
1387 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1389 char *cmd;
1390 int status = -1;
1391 int res = 0;
1393 if (id >= TPACPI_RFK_SW_MAX)
1394 return -ENODEV;
1396 while ((cmd = next_cmd(&buf))) {
1397 if (strlencmp(cmd, "enable") == 0)
1398 status = TPACPI_RFK_RADIO_ON;
1399 else if (strlencmp(cmd, "disable") == 0)
1400 status = TPACPI_RFK_RADIO_OFF;
1401 else
1402 return -EINVAL;
1405 if (status != -1) {
1406 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1407 (status == TPACPI_RFK_RADIO_ON) ?
1408 "enable" : "disable",
1409 tpacpi_rfkill_names[id]);
1410 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1411 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1414 return res;
1417 /*************************************************************************
1418 * thinkpad-acpi driver attributes
1421 /* interface_version --------------------------------------------------- */
1422 static ssize_t tpacpi_driver_interface_version_show(
1423 struct device_driver *drv,
1424 char *buf)
1426 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1429 static DRIVER_ATTR(interface_version, S_IRUGO,
1430 tpacpi_driver_interface_version_show, NULL);
1432 /* debug_level --------------------------------------------------------- */
1433 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1434 char *buf)
1436 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1439 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1440 const char *buf, size_t count)
1442 unsigned long t;
1444 if (parse_strtoul(buf, 0xffff, &t))
1445 return -EINVAL;
1447 dbg_level = t;
1449 return count;
1452 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1453 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1455 /* version ------------------------------------------------------------- */
1456 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1457 char *buf)
1459 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1460 TPACPI_DESC, TPACPI_VERSION);
1463 static DRIVER_ATTR(version, S_IRUGO,
1464 tpacpi_driver_version_show, NULL);
1466 /* --------------------------------------------------------------------- */
1468 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1470 /* wlsw_emulstate ------------------------------------------------------ */
1471 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1472 char *buf)
1474 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1477 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1478 const char *buf, size_t count)
1480 unsigned long t;
1482 if (parse_strtoul(buf, 1, &t))
1483 return -EINVAL;
1485 if (tpacpi_wlsw_emulstate != !!t) {
1486 tpacpi_wlsw_emulstate = !!t;
1487 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1490 return count;
1493 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1494 tpacpi_driver_wlsw_emulstate_show,
1495 tpacpi_driver_wlsw_emulstate_store);
1497 /* bluetooth_emulstate ------------------------------------------------- */
1498 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1499 struct device_driver *drv,
1500 char *buf)
1502 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1505 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1506 struct device_driver *drv,
1507 const char *buf, size_t count)
1509 unsigned long t;
1511 if (parse_strtoul(buf, 1, &t))
1512 return -EINVAL;
1514 tpacpi_bluetooth_emulstate = !!t;
1516 return count;
1519 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1520 tpacpi_driver_bluetooth_emulstate_show,
1521 tpacpi_driver_bluetooth_emulstate_store);
1523 /* wwan_emulstate ------------------------------------------------- */
1524 static ssize_t tpacpi_driver_wwan_emulstate_show(
1525 struct device_driver *drv,
1526 char *buf)
1528 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1531 static ssize_t tpacpi_driver_wwan_emulstate_store(
1532 struct device_driver *drv,
1533 const char *buf, size_t count)
1535 unsigned long t;
1537 if (parse_strtoul(buf, 1, &t))
1538 return -EINVAL;
1540 tpacpi_wwan_emulstate = !!t;
1542 return count;
1545 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1546 tpacpi_driver_wwan_emulstate_show,
1547 tpacpi_driver_wwan_emulstate_store);
1549 /* uwb_emulstate ------------------------------------------------- */
1550 static ssize_t tpacpi_driver_uwb_emulstate_show(
1551 struct device_driver *drv,
1552 char *buf)
1554 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1557 static ssize_t tpacpi_driver_uwb_emulstate_store(
1558 struct device_driver *drv,
1559 const char *buf, size_t count)
1561 unsigned long t;
1563 if (parse_strtoul(buf, 1, &t))
1564 return -EINVAL;
1566 tpacpi_uwb_emulstate = !!t;
1568 return count;
1571 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1572 tpacpi_driver_uwb_emulstate_show,
1573 tpacpi_driver_uwb_emulstate_store);
1574 #endif
1576 /* --------------------------------------------------------------------- */
1578 static struct driver_attribute *tpacpi_driver_attributes[] = {
1579 &driver_attr_debug_level, &driver_attr_version,
1580 &driver_attr_interface_version,
1583 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1585 int i, res;
1587 i = 0;
1588 res = 0;
1589 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1590 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1591 i++;
1594 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1595 if (!res && dbg_wlswemul)
1596 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1597 if (!res && dbg_bluetoothemul)
1598 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1599 if (!res && dbg_wwanemul)
1600 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1601 if (!res && dbg_uwbemul)
1602 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1603 #endif
1605 return res;
1608 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1610 int i;
1612 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1613 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1615 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1616 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1617 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1618 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1619 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1620 #endif
1623 /*************************************************************************
1624 * Firmware Data
1628 * Table of recommended minimum BIOS versions
1630 * Reasons for listing:
1631 * 1. Stable BIOS, listed because the unknown amount of
1632 * bugs and bad ACPI behaviour on older versions
1634 * 2. BIOS or EC fw with known bugs that trigger on Linux
1636 * 3. BIOS with known reduced functionality in older versions
1638 * We recommend the latest BIOS and EC version.
1639 * We only support the latest BIOS and EC fw version as a rule.
1641 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1642 * Information from users in ThinkWiki
1644 * WARNING: we use this table also to detect that the machine is
1645 * a ThinkPad in some cases, so don't remove entries lightly.
1648 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1649 { .vendor = (__v), \
1650 .bios = TPID(__id1, __id2), \
1651 .ec = TPACPI_MATCH_ANY, \
1652 .quirks = TPACPI_MATCH_ANY << 16 \
1653 | (__bv1) << 8 | (__bv2) }
1655 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1656 __eid, __ev1, __ev2) \
1657 { .vendor = (__v), \
1658 .bios = TPID(__bid1, __bid2), \
1659 .ec = __eid, \
1660 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1661 | (__bv1) << 8 | (__bv2) }
1663 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1664 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1666 /* Outdated IBM BIOSes often lack the EC id string */
1667 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1668 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1669 __bv1, __bv2, TPID(__id1, __id2), \
1670 __ev1, __ev2), \
1671 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1672 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1673 __ev1, __ev2)
1675 /* Outdated IBM BIOSes often lack the EC id string */
1676 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1677 __eid1, __eid2, __ev1, __ev2) \
1678 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1679 __bv1, __bv2, TPID(__eid1, __eid2), \
1680 __ev1, __ev2), \
1681 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1682 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1683 __ev1, __ev2)
1685 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1686 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1688 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1689 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1690 __bv1, __bv2, TPID(__id1, __id2), \
1691 __ev1, __ev2)
1693 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1694 __eid1, __eid2, __ev1, __ev2) \
1695 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1696 __bv1, __bv2, TPID(__eid1, __eid2), \
1697 __ev1, __ev2)
1699 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1700 /* Numeric models ------------------ */
1701 /* FW MODEL BIOS VERS */
1702 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1703 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1704 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1705 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1706 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1707 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1708 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1709 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1710 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1712 /* A-series ------------------------- */
1713 /* FW MODEL BIOS VERS EC VERS */
1714 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1715 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1716 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1717 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1718 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1719 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1720 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1721 TPV_QI0('1', '3', '2', '0'), /* A22m */
1722 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1723 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1724 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1726 /* G-series ------------------------- */
1727 /* FW MODEL BIOS VERS */
1728 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1729 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1731 /* R-series, T-series --------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1734 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1735 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1736 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1737 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1738 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1739 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1740 T40/p, T41/p, T42/p (1) */
1741 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1742 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1743 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1744 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1746 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1747 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1748 TPV_QI0('1', '6', '3', '2'), /* T22 */
1749 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1750 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1751 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1753 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1754 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1755 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1757 /* BIOS FW BIOS VERS EC FW EC VERS */
1758 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1759 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1761 /* X-series ------------------------- */
1762 /* FW MODEL BIOS VERS EC VERS */
1763 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1764 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1765 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1766 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1767 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1768 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1769 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1771 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1772 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1774 /* (0) - older versions lack DMI EC fw string and functionality */
1775 /* (1) - older versions known to lack functionality */
1778 #undef TPV_QL1
1779 #undef TPV_QL0
1780 #undef TPV_QI2
1781 #undef TPV_QI1
1782 #undef TPV_QI0
1783 #undef TPV_Q_X
1784 #undef TPV_Q
1786 static void __init tpacpi_check_outdated_fw(void)
1788 unsigned long fwvers;
1789 u16 ec_version, bios_version;
1791 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1792 ARRAY_SIZE(tpacpi_bios_version_qtable));
1794 if (!fwvers)
1795 return;
1797 bios_version = fwvers & 0xffffU;
1798 ec_version = (fwvers >> 16) & 0xffffU;
1800 /* note that unknown versions are set to 0x0000 and we use that */
1801 if ((bios_version > thinkpad_id.bios_release) ||
1802 (ec_version > thinkpad_id.ec_release &&
1803 ec_version != TPACPI_MATCH_ANY)) {
1805 * The changelogs would let us track down the exact
1806 * reason, but it is just too much of a pain to track
1807 * it. We only list BIOSes that are either really
1808 * broken, or really stable to begin with, so it is
1809 * best if the user upgrades the firmware anyway.
1811 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1812 pr_warn("WARNING: This firmware may be missing critical bug "
1813 "fixes and/or important features\n");
1817 static bool __init tpacpi_is_fw_known(void)
1819 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1820 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1823 /****************************************************************************
1824 ****************************************************************************
1826 * Subdrivers
1828 ****************************************************************************
1829 ****************************************************************************/
1831 /*************************************************************************
1832 * thinkpad-acpi metadata subdriver
1835 static int thinkpad_acpi_driver_read(struct seq_file *m)
1837 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1838 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1839 return 0;
1842 static struct ibm_struct thinkpad_acpi_driver_data = {
1843 .name = "driver",
1844 .read = thinkpad_acpi_driver_read,
1847 /*************************************************************************
1848 * Hotkey subdriver
1852 * ThinkPad firmware event model
1854 * The ThinkPad firmware has two main event interfaces: normal ACPI
1855 * notifications (which follow the ACPI standard), and a private event
1856 * interface.
1858 * The private event interface also issues events for the hotkeys. As
1859 * the driver gained features, the event handling code ended up being
1860 * built around the hotkey subdriver. This will need to be refactored
1861 * to a more formal event API eventually.
1863 * Some "hotkeys" are actually supposed to be used as event reports,
1864 * such as "brightness has changed", "volume has changed", depending on
1865 * the ThinkPad model and how the firmware is operating.
1867 * Unlike other classes, hotkey-class events have mask/unmask control on
1868 * non-ancient firmware. However, how it behaves changes a lot with the
1869 * firmware model and version.
1872 enum { /* hot key scan codes (derived from ACPI DSDT) */
1873 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1874 TP_ACPI_HOTKEYSCAN_FNF2,
1875 TP_ACPI_HOTKEYSCAN_FNF3,
1876 TP_ACPI_HOTKEYSCAN_FNF4,
1877 TP_ACPI_HOTKEYSCAN_FNF5,
1878 TP_ACPI_HOTKEYSCAN_FNF6,
1879 TP_ACPI_HOTKEYSCAN_FNF7,
1880 TP_ACPI_HOTKEYSCAN_FNF8,
1881 TP_ACPI_HOTKEYSCAN_FNF9,
1882 TP_ACPI_HOTKEYSCAN_FNF10,
1883 TP_ACPI_HOTKEYSCAN_FNF11,
1884 TP_ACPI_HOTKEYSCAN_FNF12,
1885 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1886 TP_ACPI_HOTKEYSCAN_FNINSERT,
1887 TP_ACPI_HOTKEYSCAN_FNDELETE,
1888 TP_ACPI_HOTKEYSCAN_FNHOME,
1889 TP_ACPI_HOTKEYSCAN_FNEND,
1890 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1891 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1892 TP_ACPI_HOTKEYSCAN_FNSPACE,
1893 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1894 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1895 TP_ACPI_HOTKEYSCAN_MUTE,
1896 TP_ACPI_HOTKEYSCAN_THINKPAD,
1897 TP_ACPI_HOTKEYSCAN_UNK1,
1898 TP_ACPI_HOTKEYSCAN_UNK2,
1899 TP_ACPI_HOTKEYSCAN_UNK3,
1900 TP_ACPI_HOTKEYSCAN_UNK4,
1901 TP_ACPI_HOTKEYSCAN_UNK5,
1902 TP_ACPI_HOTKEYSCAN_UNK6,
1903 TP_ACPI_HOTKEYSCAN_UNK7,
1904 TP_ACPI_HOTKEYSCAN_UNK8,
1906 /* Hotkey keymap size */
1907 TPACPI_HOTKEY_MAP_LEN
1910 enum { /* Keys/events available through NVRAM polling */
1911 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1912 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1915 enum { /* Positions of some of the keys in hotkey masks */
1916 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1917 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1918 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1919 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1920 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1921 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1922 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1923 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1924 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1925 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1926 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1929 enum { /* NVRAM to ACPI HKEY group map */
1930 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1931 TP_ACPI_HKEY_ZOOM_MASK |
1932 TP_ACPI_HKEY_DISPSWTCH_MASK |
1933 TP_ACPI_HKEY_HIBERNATE_MASK,
1934 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1935 TP_ACPI_HKEY_BRGHTDWN_MASK,
1936 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1937 TP_ACPI_HKEY_VOLDWN_MASK |
1938 TP_ACPI_HKEY_MUTE_MASK,
1941 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1942 struct tp_nvram_state {
1943 u16 thinkpad_toggle:1;
1944 u16 zoom_toggle:1;
1945 u16 display_toggle:1;
1946 u16 thinklight_toggle:1;
1947 u16 hibernate_toggle:1;
1948 u16 displayexp_toggle:1;
1949 u16 display_state:1;
1950 u16 brightness_toggle:1;
1951 u16 volume_toggle:1;
1952 u16 mute:1;
1954 u8 brightness_level;
1955 u8 volume_level;
1958 /* kthread for the hotkey poller */
1959 static struct task_struct *tpacpi_hotkey_task;
1961 /* Acquired while the poller kthread is running, use to sync start/stop */
1962 static struct mutex hotkey_thread_mutex;
1965 * Acquire mutex to write poller control variables as an
1966 * atomic block.
1968 * Increment hotkey_config_change when changing them if you
1969 * want the kthread to forget old state.
1971 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1973 static struct mutex hotkey_thread_data_mutex;
1974 static unsigned int hotkey_config_change;
1977 * hotkey poller control variables
1979 * Must be atomic or readers will also need to acquire mutex
1981 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1982 * should be used only when the changes need to be taken as
1983 * a block, OR when one needs to force the kthread to forget
1984 * old state.
1986 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1987 static unsigned int hotkey_poll_freq = 10; /* Hz */
1989 #define HOTKEY_CONFIG_CRITICAL_START \
1990 do { \
1991 mutex_lock(&hotkey_thread_data_mutex); \
1992 hotkey_config_change++; \
1993 } while (0);
1994 #define HOTKEY_CONFIG_CRITICAL_END \
1995 mutex_unlock(&hotkey_thread_data_mutex);
1997 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1999 #define hotkey_source_mask 0U
2000 #define HOTKEY_CONFIG_CRITICAL_START
2001 #define HOTKEY_CONFIG_CRITICAL_END
2003 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2005 static struct mutex hotkey_mutex;
2007 static enum { /* Reasons for waking up */
2008 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2009 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2010 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2011 } hotkey_wakeup_reason;
2013 static int hotkey_autosleep_ack;
2015 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2016 static u32 hotkey_all_mask; /* all events supported in fw */
2017 static u32 hotkey_reserved_mask; /* events better left disabled */
2018 static u32 hotkey_driver_mask; /* events needed by the driver */
2019 static u32 hotkey_user_mask; /* events visible to userspace */
2020 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2022 static unsigned int hotkey_report_mode;
2024 static u16 *hotkey_keycode_map;
2026 static struct attribute_set *hotkey_dev_attributes;
2028 static void tpacpi_driver_event(const unsigned int hkey_event);
2029 static void hotkey_driver_event(const unsigned int scancode);
2030 static void hotkey_poll_setup(const bool may_warn);
2032 /* HKEY.MHKG() return bits */
2033 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2035 static int hotkey_get_wlsw(void)
2037 int status;
2039 if (!tp_features.hotkey_wlsw)
2040 return -ENODEV;
2042 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2043 if (dbg_wlswemul)
2044 return (tpacpi_wlsw_emulstate) ?
2045 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2046 #endif
2048 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2049 return -EIO;
2051 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2054 static int hotkey_get_tablet_mode(int *status)
2056 int s;
2058 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2059 return -EIO;
2061 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2062 return 0;
2066 * Reads current event mask from firmware, and updates
2067 * hotkey_acpi_mask accordingly. Also resets any bits
2068 * from hotkey_user_mask that are unavailable to be
2069 * delivered (shadow requirement of the userspace ABI).
2071 * Call with hotkey_mutex held
2073 static int hotkey_mask_get(void)
2075 if (tp_features.hotkey_mask) {
2076 u32 m = 0;
2078 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2079 return -EIO;
2081 hotkey_acpi_mask = m;
2082 } else {
2083 /* no mask support doesn't mean no event support... */
2084 hotkey_acpi_mask = hotkey_all_mask;
2087 /* sync userspace-visible mask */
2088 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2090 return 0;
2093 void static hotkey_mask_warn_incomplete_mask(void)
2095 /* log only what the user can fix... */
2096 const u32 wantedmask = hotkey_driver_mask &
2097 ~(hotkey_acpi_mask | hotkey_source_mask) &
2098 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2100 if (wantedmask)
2101 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2105 * Set the firmware mask when supported
2107 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2109 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2111 * Call with hotkey_mutex held
2113 static int hotkey_mask_set(u32 mask)
2115 int i;
2116 int rc = 0;
2118 const u32 fwmask = mask & ~hotkey_source_mask;
2120 if (tp_features.hotkey_mask) {
2121 for (i = 0; i < 32; i++) {
2122 if (!acpi_evalf(hkey_handle,
2123 NULL, "MHKM", "vdd", i + 1,
2124 !!(mask & (1 << i)))) {
2125 rc = -EIO;
2126 break;
2132 * We *must* make an inconditional call to hotkey_mask_get to
2133 * refresh hotkey_acpi_mask and update hotkey_user_mask
2135 * Take the opportunity to also log when we cannot _enable_
2136 * a given event.
2138 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2139 pr_notice("asked for hotkey mask 0x%08x, but "
2140 "firmware forced it to 0x%08x\n",
2141 fwmask, hotkey_acpi_mask);
2144 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2145 hotkey_mask_warn_incomplete_mask();
2147 return rc;
2151 * Sets hotkey_user_mask and tries to set the firmware mask
2153 * Call with hotkey_mutex held
2155 static int hotkey_user_mask_set(const u32 mask)
2157 int rc;
2159 /* Give people a chance to notice they are doing something that
2160 * is bound to go boom on their users sooner or later */
2161 if (!tp_warned.hotkey_mask_ff &&
2162 (mask == 0xffff || mask == 0xffffff ||
2163 mask == 0xffffffff)) {
2164 tp_warned.hotkey_mask_ff = 1;
2165 pr_notice("setting the hotkey mask to 0x%08x is likely "
2166 "not the best way to go about it\n", mask);
2167 pr_notice("please consider using the driver defaults, "
2168 "and refer to up-to-date thinkpad-acpi "
2169 "documentation\n");
2172 /* Try to enable what the user asked for, plus whatever we need.
2173 * this syncs everything but won't enable bits in hotkey_user_mask */
2174 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2176 /* Enable the available bits in hotkey_user_mask */
2177 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2179 return rc;
2183 * Sets the driver hotkey mask.
2185 * Can be called even if the hotkey subdriver is inactive
2187 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2189 int rc;
2191 /* Do the right thing if hotkey_init has not been called yet */
2192 if (!tp_features.hotkey) {
2193 hotkey_driver_mask = mask;
2194 return 0;
2197 mutex_lock(&hotkey_mutex);
2199 HOTKEY_CONFIG_CRITICAL_START
2200 hotkey_driver_mask = mask;
2201 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2202 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2203 #endif
2204 HOTKEY_CONFIG_CRITICAL_END
2206 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2207 ~hotkey_source_mask);
2208 hotkey_poll_setup(true);
2210 mutex_unlock(&hotkey_mutex);
2212 return rc;
2215 static int hotkey_status_get(int *status)
2217 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2218 return -EIO;
2220 return 0;
2223 static int hotkey_status_set(bool enable)
2225 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2226 return -EIO;
2228 return 0;
2231 static void tpacpi_input_send_tabletsw(void)
2233 int state;
2235 if (tp_features.hotkey_tablet &&
2236 !hotkey_get_tablet_mode(&state)) {
2237 mutex_lock(&tpacpi_inputdev_send_mutex);
2239 input_report_switch(tpacpi_inputdev,
2240 SW_TABLET_MODE, !!state);
2241 input_sync(tpacpi_inputdev);
2243 mutex_unlock(&tpacpi_inputdev_send_mutex);
2247 /* Do NOT call without validating scancode first */
2248 static void tpacpi_input_send_key(const unsigned int scancode)
2250 const unsigned int keycode = hotkey_keycode_map[scancode];
2252 if (keycode != KEY_RESERVED) {
2253 mutex_lock(&tpacpi_inputdev_send_mutex);
2255 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2256 input_report_key(tpacpi_inputdev, keycode, 1);
2257 input_sync(tpacpi_inputdev);
2259 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2260 input_report_key(tpacpi_inputdev, keycode, 0);
2261 input_sync(tpacpi_inputdev);
2263 mutex_unlock(&tpacpi_inputdev_send_mutex);
2267 /* Do NOT call without validating scancode first */
2268 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2270 hotkey_driver_event(scancode);
2271 if (hotkey_user_mask & (1 << scancode))
2272 tpacpi_input_send_key(scancode);
2275 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2276 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2278 /* Do NOT call without validating scancode first */
2279 static void tpacpi_hotkey_send_key(unsigned int scancode)
2281 tpacpi_input_send_key_masked(scancode);
2282 if (hotkey_report_mode < 2) {
2283 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2284 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2288 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2290 u8 d;
2292 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2293 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2294 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2295 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2296 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2297 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2299 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2301 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2303 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2304 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2305 n->displayexp_toggle =
2306 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2308 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2310 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2311 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2312 n->brightness_toggle =
2313 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2315 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2317 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2318 >> TP_NVRAM_POS_LEVEL_VOLUME;
2319 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2320 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2324 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2325 struct tp_nvram_state *newn,
2326 const u32 event_mask)
2329 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2330 do { \
2331 if ((event_mask & (1 << __scancode)) && \
2332 oldn->__member != newn->__member) \
2333 tpacpi_hotkey_send_key(__scancode); \
2334 } while (0)
2336 #define TPACPI_MAY_SEND_KEY(__scancode) \
2337 do { \
2338 if (event_mask & (1 << __scancode)) \
2339 tpacpi_hotkey_send_key(__scancode); \
2340 } while (0)
2342 void issue_volchange(const unsigned int oldvol,
2343 const unsigned int newvol)
2345 unsigned int i = oldvol;
2347 while (i > newvol) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2349 i--;
2351 while (i < newvol) {
2352 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2353 i++;
2357 void issue_brightnesschange(const unsigned int oldbrt,
2358 const unsigned int newbrt)
2360 unsigned int i = oldbrt;
2362 while (i > newbrt) {
2363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2364 i--;
2366 while (i < newbrt) {
2367 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2368 i++;
2372 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2373 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2374 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2375 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2377 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2379 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2382 * Handle volume
2384 * This code is supposed to duplicate the IBM firmware behaviour:
2385 * - Pressing MUTE issues mute hotkey message, even when already mute
2386 * - Pressing Volume up/down issues volume up/down hotkey messages,
2387 * even when already at maximum or minimum volume
2388 * - The act of unmuting issues volume up/down notification,
2389 * depending which key was used to unmute
2391 * We are constrained to what the NVRAM can tell us, which is not much
2392 * and certainly not enough if more than one volume hotkey was pressed
2393 * since the last poll cycle.
2395 * Just to make our life interesting, some newer Lenovo ThinkPads have
2396 * bugs in the BIOS and may fail to update volume_toggle properly.
2398 if (newn->mute) {
2399 /* muted */
2400 if (!oldn->mute ||
2401 oldn->volume_toggle != newn->volume_toggle ||
2402 oldn->volume_level != newn->volume_level) {
2403 /* recently muted, or repeated mute keypress, or
2404 * multiple presses ending in mute */
2405 issue_volchange(oldn->volume_level, newn->volume_level);
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2408 } else {
2409 /* unmute */
2410 if (oldn->mute) {
2411 /* recently unmuted, issue 'unmute' keypress */
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2414 if (oldn->volume_level != newn->volume_level) {
2415 issue_volchange(oldn->volume_level, newn->volume_level);
2416 } else if (oldn->volume_toggle != newn->volume_toggle) {
2417 /* repeated vol up/down keypress at end of scale ? */
2418 if (newn->volume_level == 0)
2419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2420 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2425 /* handle brightness */
2426 if (oldn->brightness_level != newn->brightness_level) {
2427 issue_brightnesschange(oldn->brightness_level,
2428 newn->brightness_level);
2429 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2430 /* repeated key presses that didn't change state */
2431 if (newn->brightness_level == 0)
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2433 else if (newn->brightness_level >= bright_maxlvl
2434 && !tp_features.bright_unkfw)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2438 #undef TPACPI_COMPARE_KEY
2439 #undef TPACPI_MAY_SEND_KEY
2443 * Polling driver
2445 * We track all events in hotkey_source_mask all the time, since
2446 * most of them are edge-based. We only issue those requested by
2447 * hotkey_user_mask or hotkey_driver_mask, though.
2449 static int hotkey_kthread(void *data)
2451 struct tp_nvram_state s[2];
2452 u32 poll_mask, event_mask;
2453 unsigned int si, so;
2454 unsigned long t;
2455 unsigned int change_detector, must_reset;
2456 unsigned int poll_freq;
2458 mutex_lock(&hotkey_thread_mutex);
2460 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2461 goto exit;
2463 set_freezable();
2465 so = 0;
2466 si = 1;
2467 t = 0;
2469 /* Initial state for compares */
2470 mutex_lock(&hotkey_thread_data_mutex);
2471 change_detector = hotkey_config_change;
2472 poll_mask = hotkey_source_mask;
2473 event_mask = hotkey_source_mask &
2474 (hotkey_driver_mask | hotkey_user_mask);
2475 poll_freq = hotkey_poll_freq;
2476 mutex_unlock(&hotkey_thread_data_mutex);
2477 hotkey_read_nvram(&s[so], poll_mask);
2479 while (!kthread_should_stop()) {
2480 if (t == 0) {
2481 if (likely(poll_freq))
2482 t = 1000/poll_freq;
2483 else
2484 t = 100; /* should never happen... */
2486 t = msleep_interruptible(t);
2487 if (unlikely(kthread_should_stop()))
2488 break;
2489 must_reset = try_to_freeze();
2490 if (t > 0 && !must_reset)
2491 continue;
2493 mutex_lock(&hotkey_thread_data_mutex);
2494 if (must_reset || hotkey_config_change != change_detector) {
2495 /* forget old state on thaw or config change */
2496 si = so;
2497 t = 0;
2498 change_detector = hotkey_config_change;
2500 poll_mask = hotkey_source_mask;
2501 event_mask = hotkey_source_mask &
2502 (hotkey_driver_mask | hotkey_user_mask);
2503 poll_freq = hotkey_poll_freq;
2504 mutex_unlock(&hotkey_thread_data_mutex);
2506 if (likely(poll_mask)) {
2507 hotkey_read_nvram(&s[si], poll_mask);
2508 if (likely(si != so)) {
2509 hotkey_compare_and_issue_event(&s[so], &s[si],
2510 event_mask);
2514 so = si;
2515 si ^= 1;
2518 exit:
2519 mutex_unlock(&hotkey_thread_mutex);
2520 return 0;
2523 /* call with hotkey_mutex held */
2524 static void hotkey_poll_stop_sync(void)
2526 if (tpacpi_hotkey_task) {
2527 if (frozen(tpacpi_hotkey_task) ||
2528 freezing(tpacpi_hotkey_task))
2529 thaw_process(tpacpi_hotkey_task);
2531 kthread_stop(tpacpi_hotkey_task);
2532 tpacpi_hotkey_task = NULL;
2533 mutex_lock(&hotkey_thread_mutex);
2534 /* at this point, the thread did exit */
2535 mutex_unlock(&hotkey_thread_mutex);
2539 /* call with hotkey_mutex held */
2540 static void hotkey_poll_setup(const bool may_warn)
2542 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2543 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2545 if (hotkey_poll_freq > 0 &&
2546 (poll_driver_mask ||
2547 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2548 if (!tpacpi_hotkey_task) {
2549 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2550 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2551 if (IS_ERR(tpacpi_hotkey_task)) {
2552 tpacpi_hotkey_task = NULL;
2553 pr_err("could not create kernel thread "
2554 "for hotkey polling\n");
2557 } else {
2558 hotkey_poll_stop_sync();
2559 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2560 hotkey_poll_freq == 0) {
2561 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2562 "require polling, which is currently "
2563 "disabled\n",
2564 poll_user_mask, poll_driver_mask);
2569 static void hotkey_poll_setup_safe(const bool may_warn)
2571 mutex_lock(&hotkey_mutex);
2572 hotkey_poll_setup(may_warn);
2573 mutex_unlock(&hotkey_mutex);
2576 /* call with hotkey_mutex held */
2577 static void hotkey_poll_set_freq(unsigned int freq)
2579 if (!freq)
2580 hotkey_poll_stop_sync();
2582 hotkey_poll_freq = freq;
2585 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2587 static void hotkey_poll_setup(const bool __unused)
2591 static void hotkey_poll_setup_safe(const bool __unused)
2595 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2597 static int hotkey_inputdev_open(struct input_dev *dev)
2599 switch (tpacpi_lifecycle) {
2600 case TPACPI_LIFE_INIT:
2601 case TPACPI_LIFE_RUNNING:
2602 hotkey_poll_setup_safe(false);
2603 return 0;
2604 case TPACPI_LIFE_EXITING:
2605 return -EBUSY;
2608 /* Should only happen if tpacpi_lifecycle is corrupt */
2609 BUG();
2610 return -EBUSY;
2613 static void hotkey_inputdev_close(struct input_dev *dev)
2615 /* disable hotkey polling when possible */
2616 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2617 !(hotkey_source_mask & hotkey_driver_mask))
2618 hotkey_poll_setup_safe(false);
2621 /* sysfs hotkey enable ------------------------------------------------- */
2622 static ssize_t hotkey_enable_show(struct device *dev,
2623 struct device_attribute *attr,
2624 char *buf)
2626 int res, status;
2628 printk_deprecated_attribute("hotkey_enable",
2629 "Hotkey reporting is always enabled");
2631 res = hotkey_status_get(&status);
2632 if (res)
2633 return res;
2635 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2638 static ssize_t hotkey_enable_store(struct device *dev,
2639 struct device_attribute *attr,
2640 const char *buf, size_t count)
2642 unsigned long t;
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkeys can be disabled through hotkey_mask");
2647 if (parse_strtoul(buf, 1, &t))
2648 return -EINVAL;
2650 if (t == 0)
2651 return -EPERM;
2653 return count;
2656 static struct device_attribute dev_attr_hotkey_enable =
2657 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2658 hotkey_enable_show, hotkey_enable_store);
2660 /* sysfs hotkey mask --------------------------------------------------- */
2661 static ssize_t hotkey_mask_show(struct device *dev,
2662 struct device_attribute *attr,
2663 char *buf)
2665 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2668 static ssize_t hotkey_mask_store(struct device *dev,
2669 struct device_attribute *attr,
2670 const char *buf, size_t count)
2672 unsigned long t;
2673 int res;
2675 if (parse_strtoul(buf, 0xffffffffUL, &t))
2676 return -EINVAL;
2678 if (mutex_lock_killable(&hotkey_mutex))
2679 return -ERESTARTSYS;
2681 res = hotkey_user_mask_set(t);
2683 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2684 hotkey_poll_setup(true);
2685 #endif
2687 mutex_unlock(&hotkey_mutex);
2689 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2691 return (res) ? res : count;
2694 static struct device_attribute dev_attr_hotkey_mask =
2695 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2696 hotkey_mask_show, hotkey_mask_store);
2698 /* sysfs hotkey bios_enabled ------------------------------------------- */
2699 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2700 struct device_attribute *attr,
2701 char *buf)
2703 return sprintf(buf, "0\n");
2706 static struct device_attribute dev_attr_hotkey_bios_enabled =
2707 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2709 /* sysfs hotkey bios_mask ---------------------------------------------- */
2710 static ssize_t hotkey_bios_mask_show(struct device *dev,
2711 struct device_attribute *attr,
2712 char *buf)
2714 printk_deprecated_attribute("hotkey_bios_mask",
2715 "This attribute is useless.");
2716 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2719 static struct device_attribute dev_attr_hotkey_bios_mask =
2720 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2722 /* sysfs hotkey all_mask ----------------------------------------------- */
2723 static ssize_t hotkey_all_mask_show(struct device *dev,
2724 struct device_attribute *attr,
2725 char *buf)
2727 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2728 hotkey_all_mask | hotkey_source_mask);
2731 static struct device_attribute dev_attr_hotkey_all_mask =
2732 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2734 /* sysfs hotkey recommended_mask --------------------------------------- */
2735 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2736 struct device_attribute *attr,
2737 char *buf)
2739 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2740 (hotkey_all_mask | hotkey_source_mask)
2741 & ~hotkey_reserved_mask);
2744 static struct device_attribute dev_attr_hotkey_recommended_mask =
2745 __ATTR(hotkey_recommended_mask, S_IRUGO,
2746 hotkey_recommended_mask_show, NULL);
2748 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2750 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2751 static ssize_t hotkey_source_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2758 static ssize_t hotkey_source_mask_store(struct device *dev,
2759 struct device_attribute *attr,
2760 const char *buf, size_t count)
2762 unsigned long t;
2763 u32 r_ev;
2764 int rc;
2766 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2767 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2768 return -EINVAL;
2770 if (mutex_lock_killable(&hotkey_mutex))
2771 return -ERESTARTSYS;
2773 HOTKEY_CONFIG_CRITICAL_START
2774 hotkey_source_mask = t;
2775 HOTKEY_CONFIG_CRITICAL_END
2777 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2778 ~hotkey_source_mask);
2779 hotkey_poll_setup(true);
2781 /* check if events needed by the driver got disabled */
2782 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2783 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2785 mutex_unlock(&hotkey_mutex);
2787 if (rc < 0)
2788 pr_err("hotkey_source_mask: "
2789 "failed to update the firmware event mask!\n");
2791 if (r_ev)
2792 pr_notice("hotkey_source_mask: "
2793 "some important events were disabled: 0x%04x\n",
2794 r_ev);
2796 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2798 return (rc < 0) ? rc : count;
2801 static struct device_attribute dev_attr_hotkey_source_mask =
2802 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2803 hotkey_source_mask_show, hotkey_source_mask_store);
2805 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2806 static ssize_t hotkey_poll_freq_show(struct device *dev,
2807 struct device_attribute *attr,
2808 char *buf)
2810 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2813 static ssize_t hotkey_poll_freq_store(struct device *dev,
2814 struct device_attribute *attr,
2815 const char *buf, size_t count)
2817 unsigned long t;
2819 if (parse_strtoul(buf, 25, &t))
2820 return -EINVAL;
2822 if (mutex_lock_killable(&hotkey_mutex))
2823 return -ERESTARTSYS;
2825 hotkey_poll_set_freq(t);
2826 hotkey_poll_setup(true);
2828 mutex_unlock(&hotkey_mutex);
2830 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2832 return count;
2835 static struct device_attribute dev_attr_hotkey_poll_freq =
2836 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2837 hotkey_poll_freq_show, hotkey_poll_freq_store);
2839 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2841 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2842 static ssize_t hotkey_radio_sw_show(struct device *dev,
2843 struct device_attribute *attr,
2844 char *buf)
2846 int res;
2847 res = hotkey_get_wlsw();
2848 if (res < 0)
2849 return res;
2851 /* Opportunistic update */
2852 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2854 return snprintf(buf, PAGE_SIZE, "%d\n",
2855 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2858 static struct device_attribute dev_attr_hotkey_radio_sw =
2859 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2861 static void hotkey_radio_sw_notify_change(void)
2863 if (tp_features.hotkey_wlsw)
2864 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2865 "hotkey_radio_sw");
2868 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2869 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2870 struct device_attribute *attr,
2871 char *buf)
2873 int res, s;
2874 res = hotkey_get_tablet_mode(&s);
2875 if (res < 0)
2876 return res;
2878 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2881 static struct device_attribute dev_attr_hotkey_tablet_mode =
2882 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2884 static void hotkey_tablet_mode_notify_change(void)
2886 if (tp_features.hotkey_tablet)
2887 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2888 "hotkey_tablet_mode");
2891 /* sysfs hotkey report_mode -------------------------------------------- */
2892 static ssize_t hotkey_report_mode_show(struct device *dev,
2893 struct device_attribute *attr,
2894 char *buf)
2896 return snprintf(buf, PAGE_SIZE, "%d\n",
2897 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2900 static struct device_attribute dev_attr_hotkey_report_mode =
2901 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2903 /* sysfs wakeup reason (pollable) -------------------------------------- */
2904 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2905 struct device_attribute *attr,
2906 char *buf)
2908 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2911 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2912 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2914 static void hotkey_wakeup_reason_notify_change(void)
2916 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2917 "wakeup_reason");
2920 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2921 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2925 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2928 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2929 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2930 hotkey_wakeup_hotunplug_complete_show, NULL);
2932 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2934 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2935 "wakeup_hotunplug_complete");
2938 /* --------------------------------------------------------------------- */
2940 static struct attribute *hotkey_attributes[] __initdata = {
2941 &dev_attr_hotkey_enable.attr,
2942 &dev_attr_hotkey_bios_enabled.attr,
2943 &dev_attr_hotkey_bios_mask.attr,
2944 &dev_attr_hotkey_report_mode.attr,
2945 &dev_attr_hotkey_wakeup_reason.attr,
2946 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2947 &dev_attr_hotkey_mask.attr,
2948 &dev_attr_hotkey_all_mask.attr,
2949 &dev_attr_hotkey_recommended_mask.attr,
2950 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2951 &dev_attr_hotkey_source_mask.attr,
2952 &dev_attr_hotkey_poll_freq.attr,
2953 #endif
2957 * Sync both the hw and sw blocking state of all switches
2959 static void tpacpi_send_radiosw_update(void)
2961 int wlsw;
2964 * We must sync all rfkill controllers *before* issuing any
2965 * rfkill input events, or we will race the rfkill core input
2966 * handler.
2968 * tpacpi_inputdev_send_mutex works as a synchronization point
2969 * for the above.
2971 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2974 wlsw = hotkey_get_wlsw();
2976 /* Sync hw blocking state first if it is hw-blocked */
2977 if (wlsw == TPACPI_RFK_RADIO_OFF)
2978 tpacpi_rfk_update_hwblock_state(true);
2980 /* Sync sw blocking state */
2981 tpacpi_rfk_update_swstate_all();
2983 /* Sync hw blocking state last if it is hw-unblocked */
2984 if (wlsw == TPACPI_RFK_RADIO_ON)
2985 tpacpi_rfk_update_hwblock_state(false);
2987 /* Issue rfkill input event for WLSW switch */
2988 if (!(wlsw < 0)) {
2989 mutex_lock(&tpacpi_inputdev_send_mutex);
2991 input_report_switch(tpacpi_inputdev,
2992 SW_RFKILL_ALL, (wlsw > 0));
2993 input_sync(tpacpi_inputdev);
2995 mutex_unlock(&tpacpi_inputdev_send_mutex);
2999 * this can be unconditional, as we will poll state again
3000 * if userspace uses the notify to read data
3002 hotkey_radio_sw_notify_change();
3005 static void hotkey_exit(void)
3007 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3008 mutex_lock(&hotkey_mutex);
3009 hotkey_poll_stop_sync();
3010 mutex_unlock(&hotkey_mutex);
3011 #endif
3013 if (hotkey_dev_attributes)
3014 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3016 kfree(hotkey_keycode_map);
3018 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3019 "restoring original HKEY status and mask\n");
3020 /* yes, there is a bitwise or below, we want the
3021 * functions to be called even if one of them fail */
3022 if (((tp_features.hotkey_mask &&
3023 hotkey_mask_set(hotkey_orig_mask)) |
3024 hotkey_status_set(false)) != 0)
3025 pr_err("failed to restore hot key mask "
3026 "to BIOS defaults\n");
3029 static void __init hotkey_unmap(const unsigned int scancode)
3031 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3032 clear_bit(hotkey_keycode_map[scancode],
3033 tpacpi_inputdev->keybit);
3034 hotkey_keycode_map[scancode] = KEY_RESERVED;
3039 * HKEY quirks:
3040 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3043 #define TPACPI_HK_Q_INIMASK 0x0001
3045 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3046 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3047 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3048 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3049 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3050 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3051 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3052 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3053 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3054 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3055 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3056 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3057 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3058 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3059 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3060 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3061 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3062 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3063 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3064 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3067 typedef u16 tpacpi_keymap_entry_t;
3068 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3070 static int __init hotkey_init(struct ibm_init_struct *iibm)
3072 /* Requirements for changing the default keymaps:
3074 * 1. Many of the keys are mapped to KEY_RESERVED for very
3075 * good reasons. Do not change them unless you have deep
3076 * knowledge on the IBM and Lenovo ThinkPad firmware for
3077 * the various ThinkPad models. The driver behaves
3078 * differently for KEY_RESERVED: such keys have their
3079 * hot key mask *unset* in mask_recommended, and also
3080 * in the initial hot key mask programmed into the
3081 * firmware at driver load time, which means the firm-
3082 * ware may react very differently if you change them to
3083 * something else;
3085 * 2. You must be subscribed to the linux-thinkpad and
3086 * ibm-acpi-devel mailing lists, and you should read the
3087 * list archives since 2007 if you want to change the
3088 * keymaps. This requirement exists so that you will
3089 * know the past history of problems with the thinkpad-
3090 * acpi driver keymaps, and also that you will be
3091 * listening to any bug reports;
3093 * 3. Do not send thinkpad-acpi specific patches directly to
3094 * for merging, *ever*. Send them to the linux-acpi
3095 * mailinglist for comments. Merging is to be done only
3096 * through acpi-test and the ACPI maintainer.
3098 * If the above is too much to ask, don't change the keymap.
3099 * Ask the thinkpad-acpi maintainer to do it, instead.
3102 enum keymap_index {
3103 TPACPI_KEYMAP_IBM_GENERIC = 0,
3104 TPACPI_KEYMAP_LENOVO_GENERIC,
3107 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3108 /* Generic keymap for IBM ThinkPads */
3109 [TPACPI_KEYMAP_IBM_GENERIC] = {
3110 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3111 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3112 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3113 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3115 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3116 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3117 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3118 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3120 /* brightness: firmware always reacts to them */
3121 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3122 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3124 /* Thinklight: firmware always react to it */
3125 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3127 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3128 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3130 /* Volume: firmware always react to it and reprograms
3131 * the built-in *extra* mixer. Never map it to control
3132 * another mixer by default. */
3133 KEY_RESERVED, /* 0x14: VOLUME UP */
3134 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3135 KEY_RESERVED, /* 0x16: MUTE */
3137 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3139 /* (assignments unknown, please report if found) */
3140 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3141 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3144 /* Generic keymap for Lenovo ThinkPads */
3145 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3146 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3147 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3148 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3149 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3151 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3152 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3153 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3154 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3156 /* These should be enabled --only-- when ACPI video
3157 * is disabled (i.e. in "vendor" mode), and are handled
3158 * in a special way by the init code */
3159 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3160 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3162 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3164 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3165 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3167 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3168 * react to it and reprograms the built-in *extra* mixer.
3169 * Never map it to control another mixer by default.
3171 * T60?, T61, R60?, R61: firmware and EC tries to send
3172 * these over the regular keyboard, so these are no-ops,
3173 * but there are still weird bugs re. MUTE, so do not
3174 * change unless you get test reports from all Lenovo
3175 * models. May cause the BIOS to interfere with the
3176 * HDA mixer.
3178 KEY_RESERVED, /* 0x14: VOLUME UP */
3179 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3180 KEY_RESERVED, /* 0x16: MUTE */
3182 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3184 /* (assignments unknown, please report if found) */
3185 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3186 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3190 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3191 /* Generic maps (fallback) */
3193 .vendor = PCI_VENDOR_ID_IBM,
3194 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3195 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3198 .vendor = PCI_VENDOR_ID_LENOVO,
3199 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3200 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3204 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3205 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3207 int res, i;
3208 int status;
3209 int hkeyv;
3210 bool radiosw_state = false;
3211 bool tabletsw_state = false;
3213 unsigned long quirks;
3214 unsigned long keymap_id;
3216 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3217 "initializing hotkey subdriver\n");
3219 BUG_ON(!tpacpi_inputdev);
3220 BUG_ON(tpacpi_inputdev->open != NULL ||
3221 tpacpi_inputdev->close != NULL);
3223 TPACPI_ACPIHANDLE_INIT(hkey);
3224 mutex_init(&hotkey_mutex);
3226 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3227 mutex_init(&hotkey_thread_mutex);
3228 mutex_init(&hotkey_thread_data_mutex);
3229 #endif
3231 /* hotkey not supported on 570 */
3232 tp_features.hotkey = hkey_handle != NULL;
3234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3235 "hotkeys are %s\n",
3236 str_supported(tp_features.hotkey));
3238 if (!tp_features.hotkey)
3239 return 1;
3241 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3242 ARRAY_SIZE(tpacpi_hotkey_qtable));
3244 tpacpi_disable_brightness_delay();
3246 /* MUST have enough space for all attributes to be added to
3247 * hotkey_dev_attributes */
3248 hotkey_dev_attributes = create_attr_set(
3249 ARRAY_SIZE(hotkey_attributes) + 2,
3250 NULL);
3251 if (!hotkey_dev_attributes)
3252 return -ENOMEM;
3253 res = add_many_to_attr_set(hotkey_dev_attributes,
3254 hotkey_attributes,
3255 ARRAY_SIZE(hotkey_attributes));
3256 if (res)
3257 goto err_exit;
3259 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3260 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3261 for HKEY interface version 0x100 */
3262 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3263 if ((hkeyv >> 8) != 1) {
3264 pr_err("unknown version of the HKEY interface: 0x%x\n",
3265 hkeyv);
3266 pr_err("please report this to %s\n", TPACPI_MAIL);
3267 } else {
3269 * MHKV 0x100 in A31, R40, R40e,
3270 * T4x, X31, and later
3272 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3273 "firmware HKEY interface version: 0x%x\n",
3274 hkeyv);
3276 /* Paranoia check AND init hotkey_all_mask */
3277 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3278 "MHKA", "qd")) {
3279 pr_err("missing MHKA handler, "
3280 "please report this to %s\n",
3281 TPACPI_MAIL);
3282 /* Fallback: pre-init for FN+F3,F4,F12 */
3283 hotkey_all_mask = 0x080cU;
3284 } else {
3285 tp_features.hotkey_mask = 1;
3290 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3291 "hotkey masks are %s\n",
3292 str_supported(tp_features.hotkey_mask));
3294 /* Init hotkey_all_mask if not initialized yet */
3295 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3296 (quirks & TPACPI_HK_Q_INIMASK))
3297 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3299 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3300 if (tp_features.hotkey_mask) {
3301 /* hotkey_source_mask *must* be zero for
3302 * the first hotkey_mask_get to return hotkey_orig_mask */
3303 res = hotkey_mask_get();
3304 if (res)
3305 goto err_exit;
3307 hotkey_orig_mask = hotkey_acpi_mask;
3308 } else {
3309 hotkey_orig_mask = hotkey_all_mask;
3310 hotkey_acpi_mask = hotkey_all_mask;
3313 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3314 if (dbg_wlswemul) {
3315 tp_features.hotkey_wlsw = 1;
3316 radiosw_state = !!tpacpi_wlsw_emulstate;
3317 pr_info("radio switch emulation enabled\n");
3318 } else
3319 #endif
3320 /* Not all thinkpads have a hardware radio switch */
3321 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3322 tp_features.hotkey_wlsw = 1;
3323 radiosw_state = !!status;
3324 pr_info("radio switch found; radios are %s\n",
3325 enabled(status, 0));
3327 if (tp_features.hotkey_wlsw)
3328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_radio_sw.attr);
3331 /* For X41t, X60t, X61t Tablets... */
3332 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3333 tp_features.hotkey_tablet = 1;
3334 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3335 pr_info("possible tablet mode switch found; "
3336 "ThinkPad in %s mode\n",
3337 (tabletsw_state) ? "tablet" : "laptop");
3338 res = add_to_attr_set(hotkey_dev_attributes,
3339 &dev_attr_hotkey_tablet_mode.attr);
3342 if (!res)
3343 res = register_attr_set_with_sysfs(
3344 hotkey_dev_attributes,
3345 &tpacpi_pdev->dev.kobj);
3346 if (res)
3347 goto err_exit;
3349 /* Set up key map */
3350 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3351 GFP_KERNEL);
3352 if (!hotkey_keycode_map) {
3353 pr_err("failed to allocate memory for key map\n");
3354 res = -ENOMEM;
3355 goto err_exit;
3358 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3359 ARRAY_SIZE(tpacpi_keymap_qtable));
3360 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3362 "using keymap number %lu\n", keymap_id);
3364 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3365 TPACPI_HOTKEY_MAP_SIZE);
3367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
3375 } else {
3376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
3381 if (tp_features.hotkey_wlsw) {
3382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
3386 if (tp_features.hotkey_tablet) {
3387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
3392 /* Do not issue duplicate brightness change events to
3393 * userspace. tpacpi_detect_brightness_capabilities() must have
3394 * been called before this point */
3395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3396 pr_info("This ThinkPad has standard ACPI backlight "
3397 "brightness control, supported by the ACPI "
3398 "video driver\n");
3399 pr_notice("Disabling thinkpad-acpi brightness events "
3400 "by default...\n");
3402 /* Disable brightness up/down on Lenovo thinkpads when
3403 * ACPI is handling them, otherwise it is plain impossible
3404 * for userspace to do something even remotely sane */
3405 hotkey_reserved_mask |=
3406 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3407 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3408 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3409 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3412 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3413 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3414 & ~hotkey_all_mask
3415 & ~hotkey_reserved_mask;
3417 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "hotkey source mask 0x%08x, polling freq %u\n",
3419 hotkey_source_mask, hotkey_poll_freq);
3420 #endif
3422 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3423 "enabling firmware HKEY event interface...\n");
3424 res = hotkey_status_set(true);
3425 if (res) {
3426 hotkey_exit();
3427 return res;
3429 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3430 | hotkey_driver_mask)
3431 & ~hotkey_source_mask);
3432 if (res < 0 && res != -ENXIO) {
3433 hotkey_exit();
3434 return res;
3436 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3437 & ~hotkey_reserved_mask;
3438 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3439 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3440 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3442 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "legacy ibm/hotkey event reporting over procfs %s\n",
3444 (hotkey_report_mode < 2) ?
3445 "enabled" : "disabled");
3447 tpacpi_inputdev->open = &hotkey_inputdev_open;
3448 tpacpi_inputdev->close = &hotkey_inputdev_close;
3450 hotkey_poll_setup_safe(true);
3452 return 0;
3454 err_exit:
3455 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3456 hotkey_dev_attributes = NULL;
3458 return (res < 0)? res : 1;
3461 static bool hotkey_notify_hotkey(const u32 hkey,
3462 bool *send_acpi_ev,
3463 bool *ignore_acpi_ev)
3465 /* 0x1000-0x1FFF: key presses */
3466 unsigned int scancode = hkey & 0xfff;
3467 *send_acpi_ev = true;
3468 *ignore_acpi_ev = false;
3470 /* HKEY event 0x1001 is scancode 0x00 */
3471 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3472 scancode--;
3473 if (!(hotkey_source_mask & (1 << scancode))) {
3474 tpacpi_input_send_key_masked(scancode);
3475 *send_acpi_ev = false;
3476 } else {
3477 *ignore_acpi_ev = true;
3479 return true;
3481 return false;
3484 static bool hotkey_notify_wakeup(const u32 hkey,
3485 bool *send_acpi_ev,
3486 bool *ignore_acpi_ev)
3488 /* 0x2000-0x2FFF: Wakeup reason */
3489 *send_acpi_ev = true;
3490 *ignore_acpi_ev = false;
3492 switch (hkey) {
3493 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3494 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3495 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3496 *ignore_acpi_ev = true;
3497 break;
3499 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3500 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3501 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3502 *ignore_acpi_ev = true;
3503 break;
3505 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3506 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3507 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3508 /* how to auto-heal: */
3509 /* 2313: woke up from S3, go to S4/S5 */
3510 /* 2413: woke up from S4, go to S5 */
3511 break;
3513 default:
3514 return false;
3517 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3518 pr_info("woke up due to a hot-unplug request...\n");
3519 hotkey_wakeup_reason_notify_change();
3521 return true;
3524 static bool hotkey_notify_usrevent(const u32 hkey,
3525 bool *send_acpi_ev,
3526 bool *ignore_acpi_ev)
3528 /* 0x5000-0x5FFF: human interface helpers */
3529 *send_acpi_ev = true;
3530 *ignore_acpi_ev = false;
3532 switch (hkey) {
3533 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3534 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3535 return true;
3537 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3538 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3539 tpacpi_input_send_tabletsw();
3540 hotkey_tablet_mode_notify_change();
3541 *send_acpi_ev = false;
3542 return true;
3544 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3545 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3546 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3547 /* do not propagate these events */
3548 *ignore_acpi_ev = true;
3549 return true;
3551 default:
3552 return false;
3556 static void thermal_dump_all_sensors(void);
3558 static bool hotkey_notify_6xxx(const u32 hkey,
3559 bool *send_acpi_ev,
3560 bool *ignore_acpi_ev)
3562 bool known = true;
3564 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3565 *send_acpi_ev = true;
3566 *ignore_acpi_ev = false;
3568 switch (hkey) {
3569 case TP_HKEY_EV_THM_TABLE_CHANGED:
3570 pr_info("EC reports that Thermal Table has changed\n");
3571 /* recommended action: do nothing, we don't have
3572 * Lenovo ATM information */
3573 return true;
3574 case TP_HKEY_EV_ALARM_BAT_HOT:
3575 pr_crit("THERMAL ALARM: battery is too hot!\n");
3576 /* recommended action: warn user through gui */
3577 break;
3578 case TP_HKEY_EV_ALARM_BAT_XHOT:
3579 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3580 /* recommended action: immediate sleep/hibernate */
3581 break;
3582 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3583 pr_crit("THERMAL ALARM: "
3584 "a sensor reports something is too hot!\n");
3585 /* recommended action: warn user through gui, that */
3586 /* some internal component is too hot */
3587 break;
3588 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3589 pr_alert("THERMAL EMERGENCY: "
3590 "a sensor reports something is extremely hot!\n");
3591 /* recommended action: immediate sleep/hibernate */
3592 break;
3594 case TP_HKEY_EV_KEY_NUMLOCK:
3595 case TP_HKEY_EV_KEY_FN:
3596 /* key press events, we just ignore them as long as the EC
3597 * is still reporting them in the normal keyboard stream */
3598 *send_acpi_ev = false;
3599 *ignore_acpi_ev = true;
3600 return true;
3602 default:
3603 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3604 known = false;
3607 thermal_dump_all_sensors();
3609 return known;
3612 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3614 u32 hkey;
3615 bool send_acpi_ev;
3616 bool ignore_acpi_ev;
3617 bool known_ev;
3619 if (event != 0x80) {
3620 pr_err("unknown HKEY notification event %d\n", event);
3621 /* forward it to userspace, maybe it knows how to handle it */
3622 acpi_bus_generate_netlink_event(
3623 ibm->acpi->device->pnp.device_class,
3624 dev_name(&ibm->acpi->device->dev),
3625 event, 0);
3626 return;
3629 while (1) {
3630 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3631 pr_err("failed to retrieve HKEY event\n");
3632 return;
3635 if (hkey == 0) {
3636 /* queue empty */
3637 return;
3640 send_acpi_ev = true;
3641 ignore_acpi_ev = false;
3643 switch (hkey >> 12) {
3644 case 1:
3645 /* 0x1000-0x1FFF: key presses */
3646 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3647 &ignore_acpi_ev);
3648 break;
3649 case 2:
3650 /* 0x2000-0x2FFF: Wakeup reason */
3651 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3652 &ignore_acpi_ev);
3653 break;
3654 case 3:
3655 /* 0x3000-0x3FFF: bay-related wakeups */
3656 switch (hkey) {
3657 case TP_HKEY_EV_BAYEJ_ACK:
3658 hotkey_autosleep_ack = 1;
3659 pr_info("bay ejected\n");
3660 hotkey_wakeup_hotunplug_complete_notify_change();
3661 known_ev = true;
3662 break;
3663 case TP_HKEY_EV_OPTDRV_EJ:
3664 /* FIXME: kick libata if SATA link offline */
3665 known_ev = true;
3666 break;
3667 default:
3668 known_ev = false;
3670 break;
3671 case 4:
3672 /* 0x4000-0x4FFF: dock-related wakeups */
3673 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3674 hotkey_autosleep_ack = 1;
3675 pr_info("undocked\n");
3676 hotkey_wakeup_hotunplug_complete_notify_change();
3677 known_ev = true;
3678 } else {
3679 known_ev = false;
3681 break;
3682 case 5:
3683 /* 0x5000-0x5FFF: human interface helpers */
3684 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3685 &ignore_acpi_ev);
3686 break;
3687 case 6:
3688 /* 0x6000-0x6FFF: thermal alarms/notices and
3689 * keyboard events */
3690 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
3692 break;
3693 case 7:
3694 /* 0x7000-0x7FFF: misc */
3695 if (tp_features.hotkey_wlsw &&
3696 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3697 tpacpi_send_radiosw_update();
3698 send_acpi_ev = 0;
3699 known_ev = true;
3700 break;
3702 /* fallthrough to default */
3703 default:
3704 known_ev = false;
3706 if (!known_ev) {
3707 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3708 pr_notice("please report the conditions when this "
3709 "event happened to %s\n", TPACPI_MAIL);
3712 /* Legacy events */
3713 if (!ignore_acpi_ev &&
3714 (send_acpi_ev || hotkey_report_mode < 2)) {
3715 acpi_bus_generate_proc_event(ibm->acpi->device,
3716 event, hkey);
3719 /* netlink events */
3720 if (!ignore_acpi_ev && send_acpi_ev) {
3721 acpi_bus_generate_netlink_event(
3722 ibm->acpi->device->pnp.device_class,
3723 dev_name(&ibm->acpi->device->dev),
3724 event, hkey);
3729 static void hotkey_suspend(pm_message_t state)
3731 /* Do these on suspend, we get the events on early resume! */
3732 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3733 hotkey_autosleep_ack = 0;
3736 static void hotkey_resume(void)
3738 tpacpi_disable_brightness_delay();
3740 if (hotkey_status_set(true) < 0 ||
3741 hotkey_mask_set(hotkey_acpi_mask) < 0)
3742 pr_err("error while attempting to reset the event "
3743 "firmware interface\n");
3745 tpacpi_send_radiosw_update();
3746 hotkey_tablet_mode_notify_change();
3747 hotkey_wakeup_reason_notify_change();
3748 hotkey_wakeup_hotunplug_complete_notify_change();
3749 hotkey_poll_setup_safe(false);
3752 /* procfs -------------------------------------------------------------- */
3753 static int hotkey_read(struct seq_file *m)
3755 int res, status;
3757 if (!tp_features.hotkey) {
3758 seq_printf(m, "status:\t\tnot supported\n");
3759 return 0;
3762 if (mutex_lock_killable(&hotkey_mutex))
3763 return -ERESTARTSYS;
3764 res = hotkey_status_get(&status);
3765 if (!res)
3766 res = hotkey_mask_get();
3767 mutex_unlock(&hotkey_mutex);
3768 if (res)
3769 return res;
3771 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3772 if (hotkey_all_mask) {
3773 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3774 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3775 } else {
3776 seq_printf(m, "mask:\t\tnot supported\n");
3777 seq_printf(m, "commands:\tenable, disable, reset\n");
3780 return 0;
3783 static void hotkey_enabledisable_warn(bool enable)
3785 tpacpi_log_usertask("procfs hotkey enable/disable");
3786 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3787 pr_fmt("hotkey enable/disable functionality has been "
3788 "removed from the driver. "
3789 "Hotkeys are always enabled.\n")))
3790 pr_err("Please remove the hotkey=enable module "
3791 "parameter, it is deprecated. "
3792 "Hotkeys are always enabled.\n");
3795 static int hotkey_write(char *buf)
3797 int res;
3798 u32 mask;
3799 char *cmd;
3801 if (!tp_features.hotkey)
3802 return -ENODEV;
3804 if (mutex_lock_killable(&hotkey_mutex))
3805 return -ERESTARTSYS;
3807 mask = hotkey_user_mask;
3809 res = 0;
3810 while ((cmd = next_cmd(&buf))) {
3811 if (strlencmp(cmd, "enable") == 0) {
3812 hotkey_enabledisable_warn(1);
3813 } else if (strlencmp(cmd, "disable") == 0) {
3814 hotkey_enabledisable_warn(0);
3815 res = -EPERM;
3816 } else if (strlencmp(cmd, "reset") == 0) {
3817 mask = (hotkey_all_mask | hotkey_source_mask)
3818 & ~hotkey_reserved_mask;
3819 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3820 /* mask set */
3821 } else if (sscanf(cmd, "%x", &mask) == 1) {
3822 /* mask set */
3823 } else {
3824 res = -EINVAL;
3825 goto errexit;
3829 if (!res) {
3830 tpacpi_disclose_usertask("procfs hotkey",
3831 "set mask to 0x%08x\n", mask);
3832 res = hotkey_user_mask_set(mask);
3835 errexit:
3836 mutex_unlock(&hotkey_mutex);
3837 return res;
3840 static const struct acpi_device_id ibm_htk_device_ids[] = {
3841 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3842 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3843 {"", 0},
3846 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3847 .hid = ibm_htk_device_ids,
3848 .notify = hotkey_notify,
3849 .handle = &hkey_handle,
3850 .type = ACPI_DEVICE_NOTIFY,
3853 static struct ibm_struct hotkey_driver_data = {
3854 .name = "hotkey",
3855 .read = hotkey_read,
3856 .write = hotkey_write,
3857 .exit = hotkey_exit,
3858 .resume = hotkey_resume,
3859 .suspend = hotkey_suspend,
3860 .acpi = &ibm_hotkey_acpidriver,
3863 /*************************************************************************
3864 * Bluetooth subdriver
3867 enum {
3868 /* ACPI GBDC/SBDC bits */
3869 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3870 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3871 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3872 0 = disable, 1 = enable */
3875 enum {
3876 /* ACPI \BLTH commands */
3877 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3878 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3879 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3880 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3881 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3884 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3886 static int bluetooth_get_status(void)
3888 int status;
3890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_bluetoothemul)
3892 return (tpacpi_bluetooth_emulstate) ?
3893 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3894 #endif
3896 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3897 return -EIO;
3899 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3903 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3905 int status;
3907 vdbg_printk(TPACPI_DBG_RFKILL,
3908 "will attempt to %s bluetooth\n",
3909 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3911 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3912 if (dbg_bluetoothemul) {
3913 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3914 return 0;
3916 #endif
3918 if (state == TPACPI_RFK_RADIO_ON)
3919 status = TP_ACPI_BLUETOOTH_RADIOSSW
3920 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3921 else
3922 status = 0;
3924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3925 return -EIO;
3927 return 0;
3930 /* sysfs bluetooth enable ---------------------------------------------- */
3931 static ssize_t bluetooth_enable_show(struct device *dev,
3932 struct device_attribute *attr,
3933 char *buf)
3935 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3936 attr, buf);
3939 static ssize_t bluetooth_enable_store(struct device *dev,
3940 struct device_attribute *attr,
3941 const char *buf, size_t count)
3943 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf, count);
3947 static struct device_attribute dev_attr_bluetooth_enable =
3948 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3949 bluetooth_enable_show, bluetooth_enable_store);
3951 /* --------------------------------------------------------------------- */
3953 static struct attribute *bluetooth_attributes[] = {
3954 &dev_attr_bluetooth_enable.attr,
3955 NULL
3958 static const struct attribute_group bluetooth_attr_group = {
3959 .attrs = bluetooth_attributes,
3962 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3963 .get_status = bluetooth_get_status,
3964 .set_status = bluetooth_set_status,
3967 static void bluetooth_shutdown(void)
3969 /* Order firmware to save current state to NVRAM */
3970 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3971 TP_ACPI_BLTH_SAVE_STATE))
3972 pr_notice("failed to save bluetooth state to NVRAM\n");
3973 else
3974 vdbg_printk(TPACPI_DBG_RFKILL,
3975 "bluestooth state saved to NVRAM\n");
3978 static void bluetooth_exit(void)
3980 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3981 &bluetooth_attr_group);
3983 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3985 bluetooth_shutdown();
3988 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3990 int res;
3991 int status = 0;
3993 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3994 "initializing bluetooth subdriver\n");
3996 TPACPI_ACPIHANDLE_INIT(hkey);
3998 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3999 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4000 tp_features.bluetooth = hkey_handle &&
4001 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4003 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4004 "bluetooth is %s, status 0x%02x\n",
4005 str_supported(tp_features.bluetooth),
4006 status);
4008 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4009 if (dbg_bluetoothemul) {
4010 tp_features.bluetooth = 1;
4011 pr_info("bluetooth switch emulation enabled\n");
4012 } else
4013 #endif
4014 if (tp_features.bluetooth &&
4015 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4016 /* no bluetooth hardware present in system */
4017 tp_features.bluetooth = 0;
4018 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4019 "bluetooth hardware not installed\n");
4022 if (!tp_features.bluetooth)
4023 return 1;
4025 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4026 &bluetooth_tprfk_ops,
4027 RFKILL_TYPE_BLUETOOTH,
4028 TPACPI_RFK_BLUETOOTH_SW_NAME,
4029 true);
4030 if (res)
4031 return res;
4033 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4034 &bluetooth_attr_group);
4035 if (res) {
4036 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4037 return res;
4040 return 0;
4043 /* procfs -------------------------------------------------------------- */
4044 static int bluetooth_read(struct seq_file *m)
4046 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4049 static int bluetooth_write(char *buf)
4051 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4054 static struct ibm_struct bluetooth_driver_data = {
4055 .name = "bluetooth",
4056 .read = bluetooth_read,
4057 .write = bluetooth_write,
4058 .exit = bluetooth_exit,
4059 .shutdown = bluetooth_shutdown,
4062 /*************************************************************************
4063 * Wan subdriver
4066 enum {
4067 /* ACPI GWAN/SWAN bits */
4068 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4069 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4070 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4071 0 = disable, 1 = enable */
4074 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4076 static int wan_get_status(void)
4078 int status;
4080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4081 if (dbg_wwanemul)
4082 return (tpacpi_wwan_emulstate) ?
4083 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4084 #endif
4086 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4087 return -EIO;
4089 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4090 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4093 static int wan_set_status(enum tpacpi_rfkill_state state)
4095 int status;
4097 vdbg_printk(TPACPI_DBG_RFKILL,
4098 "will attempt to %s wwan\n",
4099 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4101 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4102 if (dbg_wwanemul) {
4103 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4104 return 0;
4106 #endif
4108 if (state == TPACPI_RFK_RADIO_ON)
4109 status = TP_ACPI_WANCARD_RADIOSSW
4110 | TP_ACPI_WANCARD_RESUMECTRL;
4111 else
4112 status = 0;
4114 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4115 return -EIO;
4117 return 0;
4120 /* sysfs wan enable ---------------------------------------------------- */
4121 static ssize_t wan_enable_show(struct device *dev,
4122 struct device_attribute *attr,
4123 char *buf)
4125 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4126 attr, buf);
4129 static ssize_t wan_enable_store(struct device *dev,
4130 struct device_attribute *attr,
4131 const char *buf, size_t count)
4133 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4134 attr, buf, count);
4137 static struct device_attribute dev_attr_wan_enable =
4138 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4139 wan_enable_show, wan_enable_store);
4141 /* --------------------------------------------------------------------- */
4143 static struct attribute *wan_attributes[] = {
4144 &dev_attr_wan_enable.attr,
4145 NULL
4148 static const struct attribute_group wan_attr_group = {
4149 .attrs = wan_attributes,
4152 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4153 .get_status = wan_get_status,
4154 .set_status = wan_set_status,
4157 static void wan_shutdown(void)
4159 /* Order firmware to save current state to NVRAM */
4160 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4161 TP_ACPI_WGSV_SAVE_STATE))
4162 pr_notice("failed to save WWAN state to NVRAM\n");
4163 else
4164 vdbg_printk(TPACPI_DBG_RFKILL,
4165 "WWAN state saved to NVRAM\n");
4168 static void wan_exit(void)
4170 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4171 &wan_attr_group);
4173 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4175 wan_shutdown();
4178 static int __init wan_init(struct ibm_init_struct *iibm)
4180 int res;
4181 int status = 0;
4183 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4184 "initializing wan subdriver\n");
4186 TPACPI_ACPIHANDLE_INIT(hkey);
4188 tp_features.wan = hkey_handle &&
4189 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4191 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4192 "wan is %s, status 0x%02x\n",
4193 str_supported(tp_features.wan),
4194 status);
4196 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4197 if (dbg_wwanemul) {
4198 tp_features.wan = 1;
4199 pr_info("wwan switch emulation enabled\n");
4200 } else
4201 #endif
4202 if (tp_features.wan &&
4203 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4204 /* no wan hardware present in system */
4205 tp_features.wan = 0;
4206 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4207 "wan hardware not installed\n");
4210 if (!tp_features.wan)
4211 return 1;
4213 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4214 &wan_tprfk_ops,
4215 RFKILL_TYPE_WWAN,
4216 TPACPI_RFK_WWAN_SW_NAME,
4217 true);
4218 if (res)
4219 return res;
4221 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4222 &wan_attr_group);
4224 if (res) {
4225 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4226 return res;
4229 return 0;
4232 /* procfs -------------------------------------------------------------- */
4233 static int wan_read(struct seq_file *m)
4235 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4238 static int wan_write(char *buf)
4240 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4243 static struct ibm_struct wan_driver_data = {
4244 .name = "wan",
4245 .read = wan_read,
4246 .write = wan_write,
4247 .exit = wan_exit,
4248 .shutdown = wan_shutdown,
4251 /*************************************************************************
4252 * UWB subdriver
4255 enum {
4256 /* ACPI GUWB/SUWB bits */
4257 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4258 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4261 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4263 static int uwb_get_status(void)
4265 int status;
4267 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4268 if (dbg_uwbemul)
4269 return (tpacpi_uwb_emulstate) ?
4270 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4271 #endif
4273 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4274 return -EIO;
4276 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4277 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4280 static int uwb_set_status(enum tpacpi_rfkill_state state)
4282 int status;
4284 vdbg_printk(TPACPI_DBG_RFKILL,
4285 "will attempt to %s UWB\n",
4286 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4288 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4289 if (dbg_uwbemul) {
4290 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4291 return 0;
4293 #endif
4295 if (state == TPACPI_RFK_RADIO_ON)
4296 status = TP_ACPI_UWB_RADIOSSW;
4297 else
4298 status = 0;
4300 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4301 return -EIO;
4303 return 0;
4306 /* --------------------------------------------------------------------- */
4308 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4309 .get_status = uwb_get_status,
4310 .set_status = uwb_set_status,
4313 static void uwb_exit(void)
4315 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4318 static int __init uwb_init(struct ibm_init_struct *iibm)
4320 int res;
4321 int status = 0;
4323 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4324 "initializing uwb subdriver\n");
4326 TPACPI_ACPIHANDLE_INIT(hkey);
4328 tp_features.uwb = hkey_handle &&
4329 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4331 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4332 "uwb is %s, status 0x%02x\n",
4333 str_supported(tp_features.uwb),
4334 status);
4336 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4337 if (dbg_uwbemul) {
4338 tp_features.uwb = 1;
4339 pr_info("uwb switch emulation enabled\n");
4340 } else
4341 #endif
4342 if (tp_features.uwb &&
4343 !(status & TP_ACPI_UWB_HWPRESENT)) {
4344 /* no uwb hardware present in system */
4345 tp_features.uwb = 0;
4346 dbg_printk(TPACPI_DBG_INIT,
4347 "uwb hardware not installed\n");
4350 if (!tp_features.uwb)
4351 return 1;
4353 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4354 &uwb_tprfk_ops,
4355 RFKILL_TYPE_UWB,
4356 TPACPI_RFK_UWB_SW_NAME,
4357 false);
4358 return res;
4361 static struct ibm_struct uwb_driver_data = {
4362 .name = "uwb",
4363 .exit = uwb_exit,
4364 .flags.experimental = 1,
4367 /*************************************************************************
4368 * Video subdriver
4371 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4373 enum video_access_mode {
4374 TPACPI_VIDEO_NONE = 0,
4375 TPACPI_VIDEO_570, /* 570 */
4376 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4377 TPACPI_VIDEO_NEW, /* all others */
4380 enum { /* video status flags, based on VIDEO_570 */
4381 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4382 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4383 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4386 enum { /* TPACPI_VIDEO_570 constants */
4387 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4388 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4389 * video_status_flags */
4390 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4391 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4394 static enum video_access_mode video_supported;
4395 static int video_orig_autosw;
4397 static int video_autosw_get(void);
4398 static int video_autosw_set(int enable);
4400 TPACPI_HANDLE(vid, root,
4401 "\\_SB.PCI.AGP.VGA", /* 570 */
4402 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4403 "\\_SB.PCI0.VID0", /* 770e */
4404 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4405 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4406 "\\_SB.PCI0.AGP.VID", /* all others */
4407 ); /* R30, R31 */
4409 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4411 static int __init video_init(struct ibm_init_struct *iibm)
4413 int ivga;
4415 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4417 TPACPI_ACPIHANDLE_INIT(vid);
4418 if (tpacpi_is_ibm())
4419 TPACPI_ACPIHANDLE_INIT(vid2);
4421 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4422 /* G41, assume IVGA doesn't change */
4423 vid_handle = vid2_handle;
4425 if (!vid_handle)
4426 /* video switching not supported on R30, R31 */
4427 video_supported = TPACPI_VIDEO_NONE;
4428 else if (tpacpi_is_ibm() &&
4429 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4430 /* 570 */
4431 video_supported = TPACPI_VIDEO_570;
4432 else if (tpacpi_is_ibm() &&
4433 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4434 /* 600e/x, 770e, 770x */
4435 video_supported = TPACPI_VIDEO_770;
4436 else
4437 /* all others */
4438 video_supported = TPACPI_VIDEO_NEW;
4440 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4441 str_supported(video_supported != TPACPI_VIDEO_NONE),
4442 video_supported);
4444 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4447 static void video_exit(void)
4449 dbg_printk(TPACPI_DBG_EXIT,
4450 "restoring original video autoswitch mode\n");
4451 if (video_autosw_set(video_orig_autosw))
4452 pr_err("error while trying to restore original "
4453 "video autoswitch mode\n");
4456 static int video_outputsw_get(void)
4458 int status = 0;
4459 int i;
4461 switch (video_supported) {
4462 case TPACPI_VIDEO_570:
4463 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4464 TP_ACPI_VIDEO_570_PHSCMD))
4465 return -EIO;
4466 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4467 break;
4468 case TPACPI_VIDEO_770:
4469 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4470 return -EIO;
4471 if (i)
4472 status |= TP_ACPI_VIDEO_S_LCD;
4473 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
4477 break;
4478 case TPACPI_VIDEO_NEW:
4479 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4480 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4481 return -EIO;
4482 if (i)
4483 status |= TP_ACPI_VIDEO_S_CRT;
4485 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4486 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4487 return -EIO;
4488 if (i)
4489 status |= TP_ACPI_VIDEO_S_LCD;
4490 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4491 return -EIO;
4492 if (i)
4493 status |= TP_ACPI_VIDEO_S_DVI;
4494 break;
4495 default:
4496 return -ENOSYS;
4499 return status;
4502 static int video_outputsw_set(int status)
4504 int autosw;
4505 int res = 0;
4507 switch (video_supported) {
4508 case TPACPI_VIDEO_570:
4509 res = acpi_evalf(NULL, NULL,
4510 "\\_SB.PHS2", "vdd",
4511 TP_ACPI_VIDEO_570_PHS2CMD,
4512 status | TP_ACPI_VIDEO_570_PHS2SET);
4513 break;
4514 case TPACPI_VIDEO_770:
4515 autosw = video_autosw_get();
4516 if (autosw < 0)
4517 return autosw;
4519 res = video_autosw_set(1);
4520 if (res)
4521 return res;
4522 res = acpi_evalf(vid_handle, NULL,
4523 "ASWT", "vdd", status * 0x100, 0);
4524 if (!autosw && video_autosw_set(autosw)) {
4525 pr_err("video auto-switch left enabled due to error\n");
4526 return -EIO;
4528 break;
4529 case TPACPI_VIDEO_NEW:
4530 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4531 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4532 break;
4533 default:
4534 return -ENOSYS;
4537 return (res)? 0 : -EIO;
4540 static int video_autosw_get(void)
4542 int autosw = 0;
4544 switch (video_supported) {
4545 case TPACPI_VIDEO_570:
4546 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4547 return -EIO;
4548 break;
4549 case TPACPI_VIDEO_770:
4550 case TPACPI_VIDEO_NEW:
4551 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4552 return -EIO;
4553 break;
4554 default:
4555 return -ENOSYS;
4558 return autosw & 1;
4561 static int video_autosw_set(int enable)
4563 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4564 return -EIO;
4565 return 0;
4568 static int video_outputsw_cycle(void)
4570 int autosw = video_autosw_get();
4571 int res;
4573 if (autosw < 0)
4574 return autosw;
4576 switch (video_supported) {
4577 case TPACPI_VIDEO_570:
4578 res = video_autosw_set(1);
4579 if (res)
4580 return res;
4581 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4582 break;
4583 case TPACPI_VIDEO_770:
4584 case TPACPI_VIDEO_NEW:
4585 res = video_autosw_set(1);
4586 if (res)
4587 return res;
4588 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4589 break;
4590 default:
4591 return -ENOSYS;
4593 if (!autosw && video_autosw_set(autosw)) {
4594 pr_err("video auto-switch left enabled due to error\n");
4595 return -EIO;
4598 return (res)? 0 : -EIO;
4601 static int video_expand_toggle(void)
4603 switch (video_supported) {
4604 case TPACPI_VIDEO_570:
4605 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4606 0 : -EIO;
4607 case TPACPI_VIDEO_770:
4608 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4609 0 : -EIO;
4610 case TPACPI_VIDEO_NEW:
4611 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4612 0 : -EIO;
4613 default:
4614 return -ENOSYS;
4616 /* not reached */
4619 static int video_read(struct seq_file *m)
4621 int status, autosw;
4623 if (video_supported == TPACPI_VIDEO_NONE) {
4624 seq_printf(m, "status:\t\tnot supported\n");
4625 return 0;
4628 /* Even reads can crash X.org, so... */
4629 if (!capable(CAP_SYS_ADMIN))
4630 return -EPERM;
4632 status = video_outputsw_get();
4633 if (status < 0)
4634 return status;
4636 autosw = video_autosw_get();
4637 if (autosw < 0)
4638 return autosw;
4640 seq_printf(m, "status:\t\tsupported\n");
4641 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4642 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4643 if (video_supported == TPACPI_VIDEO_NEW)
4644 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4645 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4646 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4647 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4648 if (video_supported == TPACPI_VIDEO_NEW)
4649 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4650 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4651 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4653 return 0;
4656 static int video_write(char *buf)
4658 char *cmd;
4659 int enable, disable, status;
4660 int res;
4662 if (video_supported == TPACPI_VIDEO_NONE)
4663 return -ENODEV;
4665 /* Even reads can crash X.org, let alone writes... */
4666 if (!capable(CAP_SYS_ADMIN))
4667 return -EPERM;
4669 enable = 0;
4670 disable = 0;
4672 while ((cmd = next_cmd(&buf))) {
4673 if (strlencmp(cmd, "lcd_enable") == 0) {
4674 enable |= TP_ACPI_VIDEO_S_LCD;
4675 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4676 disable |= TP_ACPI_VIDEO_S_LCD;
4677 } else if (strlencmp(cmd, "crt_enable") == 0) {
4678 enable |= TP_ACPI_VIDEO_S_CRT;
4679 } else if (strlencmp(cmd, "crt_disable") == 0) {
4680 disable |= TP_ACPI_VIDEO_S_CRT;
4681 } else if (video_supported == TPACPI_VIDEO_NEW &&
4682 strlencmp(cmd, "dvi_enable") == 0) {
4683 enable |= TP_ACPI_VIDEO_S_DVI;
4684 } else if (video_supported == TPACPI_VIDEO_NEW &&
4685 strlencmp(cmd, "dvi_disable") == 0) {
4686 disable |= TP_ACPI_VIDEO_S_DVI;
4687 } else if (strlencmp(cmd, "auto_enable") == 0) {
4688 res = video_autosw_set(1);
4689 if (res)
4690 return res;
4691 } else if (strlencmp(cmd, "auto_disable") == 0) {
4692 res = video_autosw_set(0);
4693 if (res)
4694 return res;
4695 } else if (strlencmp(cmd, "video_switch") == 0) {
4696 res = video_outputsw_cycle();
4697 if (res)
4698 return res;
4699 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4700 res = video_expand_toggle();
4701 if (res)
4702 return res;
4703 } else
4704 return -EINVAL;
4707 if (enable || disable) {
4708 status = video_outputsw_get();
4709 if (status < 0)
4710 return status;
4711 res = video_outputsw_set((status & ~disable) | enable);
4712 if (res)
4713 return res;
4716 return 0;
4719 static struct ibm_struct video_driver_data = {
4720 .name = "video",
4721 .read = video_read,
4722 .write = video_write,
4723 .exit = video_exit,
4726 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4728 /*************************************************************************
4729 * Light (thinklight) subdriver
4732 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4733 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4735 static int light_get_status(void)
4737 int status = 0;
4739 if (tp_features.light_status) {
4740 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4741 return -EIO;
4742 return (!!status);
4745 return -ENXIO;
4748 static int light_set_status(int status)
4750 int rc;
4752 if (tp_features.light) {
4753 if (cmos_handle) {
4754 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4755 (status)?
4756 TP_CMOS_THINKLIGHT_ON :
4757 TP_CMOS_THINKLIGHT_OFF);
4758 } else {
4759 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4760 (status)? 1 : 0);
4762 return (rc)? 0 : -EIO;
4765 return -ENXIO;
4768 static void light_set_status_worker(struct work_struct *work)
4770 struct tpacpi_led_classdev *data =
4771 container_of(work, struct tpacpi_led_classdev, work);
4773 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4774 light_set_status((data->new_state != TPACPI_LED_OFF));
4777 static void light_sysfs_set(struct led_classdev *led_cdev,
4778 enum led_brightness brightness)
4780 struct tpacpi_led_classdev *data =
4781 container_of(led_cdev,
4782 struct tpacpi_led_classdev,
4783 led_classdev);
4784 data->new_state = (brightness != LED_OFF) ?
4785 TPACPI_LED_ON : TPACPI_LED_OFF;
4786 queue_work(tpacpi_wq, &data->work);
4789 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4791 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4794 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4795 .led_classdev = {
4796 .name = "tpacpi::thinklight",
4797 .brightness_set = &light_sysfs_set,
4798 .brightness_get = &light_sysfs_get,
4802 static int __init light_init(struct ibm_init_struct *iibm)
4804 int rc;
4806 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4808 if (tpacpi_is_ibm()) {
4809 TPACPI_ACPIHANDLE_INIT(ledb);
4810 TPACPI_ACPIHANDLE_INIT(lght);
4812 TPACPI_ACPIHANDLE_INIT(cmos);
4813 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4815 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4816 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4818 if (tp_features.light)
4819 /* light status not supported on
4820 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4821 tp_features.light_status =
4822 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4824 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4825 str_supported(tp_features.light),
4826 str_supported(tp_features.light_status));
4828 if (!tp_features.light)
4829 return 1;
4831 rc = led_classdev_register(&tpacpi_pdev->dev,
4832 &tpacpi_led_thinklight.led_classdev);
4834 if (rc < 0) {
4835 tp_features.light = 0;
4836 tp_features.light_status = 0;
4837 } else {
4838 rc = 0;
4841 return rc;
4844 static void light_exit(void)
4846 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4847 if (work_pending(&tpacpi_led_thinklight.work))
4848 flush_workqueue(tpacpi_wq);
4851 static int light_read(struct seq_file *m)
4853 int status;
4855 if (!tp_features.light) {
4856 seq_printf(m, "status:\t\tnot supported\n");
4857 } else if (!tp_features.light_status) {
4858 seq_printf(m, "status:\t\tunknown\n");
4859 seq_printf(m, "commands:\ton, off\n");
4860 } else {
4861 status = light_get_status();
4862 if (status < 0)
4863 return status;
4864 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4865 seq_printf(m, "commands:\ton, off\n");
4868 return 0;
4871 static int light_write(char *buf)
4873 char *cmd;
4874 int newstatus = 0;
4876 if (!tp_features.light)
4877 return -ENODEV;
4879 while ((cmd = next_cmd(&buf))) {
4880 if (strlencmp(cmd, "on") == 0) {
4881 newstatus = 1;
4882 } else if (strlencmp(cmd, "off") == 0) {
4883 newstatus = 0;
4884 } else
4885 return -EINVAL;
4888 return light_set_status(newstatus);
4891 static struct ibm_struct light_driver_data = {
4892 .name = "light",
4893 .read = light_read,
4894 .write = light_write,
4895 .exit = light_exit,
4898 /*************************************************************************
4899 * CMOS subdriver
4902 /* sysfs cmos_command -------------------------------------------------- */
4903 static ssize_t cmos_command_store(struct device *dev,
4904 struct device_attribute *attr,
4905 const char *buf, size_t count)
4907 unsigned long cmos_cmd;
4908 int res;
4910 if (parse_strtoul(buf, 21, &cmos_cmd))
4911 return -EINVAL;
4913 res = issue_thinkpad_cmos_command(cmos_cmd);
4914 return (res)? res : count;
4917 static struct device_attribute dev_attr_cmos_command =
4918 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4920 /* --------------------------------------------------------------------- */
4922 static int __init cmos_init(struct ibm_init_struct *iibm)
4924 int res;
4926 vdbg_printk(TPACPI_DBG_INIT,
4927 "initializing cmos commands subdriver\n");
4929 TPACPI_ACPIHANDLE_INIT(cmos);
4931 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4932 str_supported(cmos_handle != NULL));
4934 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4935 if (res)
4936 return res;
4938 return (cmos_handle)? 0 : 1;
4941 static void cmos_exit(void)
4943 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4946 static int cmos_read(struct seq_file *m)
4948 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4949 R30, R31, T20-22, X20-21 */
4950 if (!cmos_handle)
4951 seq_printf(m, "status:\t\tnot supported\n");
4952 else {
4953 seq_printf(m, "status:\t\tsupported\n");
4954 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4957 return 0;
4960 static int cmos_write(char *buf)
4962 char *cmd;
4963 int cmos_cmd, res;
4965 while ((cmd = next_cmd(&buf))) {
4966 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4967 cmos_cmd >= 0 && cmos_cmd <= 21) {
4968 /* cmos_cmd set */
4969 } else
4970 return -EINVAL;
4972 res = issue_thinkpad_cmos_command(cmos_cmd);
4973 if (res)
4974 return res;
4977 return 0;
4980 static struct ibm_struct cmos_driver_data = {
4981 .name = "cmos",
4982 .read = cmos_read,
4983 .write = cmos_write,
4984 .exit = cmos_exit,
4987 /*************************************************************************
4988 * LED subdriver
4991 enum led_access_mode {
4992 TPACPI_LED_NONE = 0,
4993 TPACPI_LED_570, /* 570 */
4994 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4995 TPACPI_LED_NEW, /* all others */
4998 enum { /* For TPACPI_LED_OLD */
4999 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5000 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5001 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5004 static enum led_access_mode led_supported;
5006 static acpi_handle led_handle;
5008 #define TPACPI_LED_NUMLEDS 16
5009 static struct tpacpi_led_classdev *tpacpi_leds;
5010 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5011 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5012 /* there's a limit of 19 chars + NULL before 2.6.26 */
5013 "tpacpi::power",
5014 "tpacpi:orange:batt",
5015 "tpacpi:green:batt",
5016 "tpacpi::dock_active",
5017 "tpacpi::bay_active",
5018 "tpacpi::dock_batt",
5019 "tpacpi::unknown_led",
5020 "tpacpi::standby",
5021 "tpacpi::dock_status1",
5022 "tpacpi::dock_status2",
5023 "tpacpi::unknown_led2",
5024 "tpacpi::unknown_led3",
5025 "tpacpi::thinkvantage",
5027 #define TPACPI_SAFE_LEDS 0x1081U
5029 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5031 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5032 return false;
5033 #else
5034 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5035 #endif
5038 static int led_get_status(const unsigned int led)
5040 int status;
5041 enum led_status_t led_s;
5043 switch (led_supported) {
5044 case TPACPI_LED_570:
5045 if (!acpi_evalf(ec_handle,
5046 &status, "GLED", "dd", 1 << led))
5047 return -EIO;
5048 led_s = (status == 0)?
5049 TPACPI_LED_OFF :
5050 ((status == 1)?
5051 TPACPI_LED_ON :
5052 TPACPI_LED_BLINK);
5053 tpacpi_led_state_cache[led] = led_s;
5054 return led_s;
5055 default:
5056 return -ENXIO;
5059 /* not reached */
5062 static int led_set_status(const unsigned int led,
5063 const enum led_status_t ledstatus)
5065 /* off, on, blink. Index is led_status_t */
5066 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5067 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5069 int rc = 0;
5071 switch (led_supported) {
5072 case TPACPI_LED_570:
5073 /* 570 */
5074 if (unlikely(led > 7))
5075 return -EINVAL;
5076 if (unlikely(tpacpi_is_led_restricted(led)))
5077 return -EPERM;
5078 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5079 (1 << led), led_sled_arg1[ledstatus]))
5080 rc = -EIO;
5081 break;
5082 case TPACPI_LED_OLD:
5083 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5084 if (unlikely(led > 7))
5085 return -EINVAL;
5086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
5088 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5089 if (rc >= 0)
5090 rc = ec_write(TPACPI_LED_EC_HLBL,
5091 (ledstatus == TPACPI_LED_BLINK) << led);
5092 if (rc >= 0)
5093 rc = ec_write(TPACPI_LED_EC_HLCL,
5094 (ledstatus != TPACPI_LED_OFF) << led);
5095 break;
5096 case TPACPI_LED_NEW:
5097 /* all others */
5098 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5099 return -EINVAL;
5100 if (unlikely(tpacpi_is_led_restricted(led)))
5101 return -EPERM;
5102 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5103 led, led_led_arg1[ledstatus]))
5104 rc = -EIO;
5105 break;
5106 default:
5107 rc = -ENXIO;
5110 if (!rc)
5111 tpacpi_led_state_cache[led] = ledstatus;
5113 return rc;
5116 static void led_set_status_worker(struct work_struct *work)
5118 struct tpacpi_led_classdev *data =
5119 container_of(work, struct tpacpi_led_classdev, work);
5121 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5122 led_set_status(data->led, data->new_state);
5125 static void led_sysfs_set(struct led_classdev *led_cdev,
5126 enum led_brightness brightness)
5128 struct tpacpi_led_classdev *data = container_of(led_cdev,
5129 struct tpacpi_led_classdev, led_classdev);
5131 if (brightness == LED_OFF)
5132 data->new_state = TPACPI_LED_OFF;
5133 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5134 data->new_state = TPACPI_LED_ON;
5135 else
5136 data->new_state = TPACPI_LED_BLINK;
5138 queue_work(tpacpi_wq, &data->work);
5141 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5142 unsigned long *delay_on, unsigned long *delay_off)
5144 struct tpacpi_led_classdev *data = container_of(led_cdev,
5145 struct tpacpi_led_classdev, led_classdev);
5147 /* Can we choose the flash rate? */
5148 if (*delay_on == 0 && *delay_off == 0) {
5149 /* yes. set them to the hardware blink rate (1 Hz) */
5150 *delay_on = 500; /* ms */
5151 *delay_off = 500; /* ms */
5152 } else if ((*delay_on != 500) || (*delay_off != 500))
5153 return -EINVAL;
5155 data->new_state = TPACPI_LED_BLINK;
5156 queue_work(tpacpi_wq, &data->work);
5158 return 0;
5161 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5163 int rc;
5165 struct tpacpi_led_classdev *data = container_of(led_cdev,
5166 struct tpacpi_led_classdev, led_classdev);
5168 rc = led_get_status(data->led);
5170 if (rc == TPACPI_LED_OFF || rc < 0)
5171 rc = LED_OFF; /* no error handling in led class :( */
5172 else
5173 rc = LED_FULL;
5175 return rc;
5178 static void led_exit(void)
5180 unsigned int i;
5182 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5183 if (tpacpi_leds[i].led_classdev.name)
5184 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5187 kfree(tpacpi_leds);
5190 static int __init tpacpi_init_led(unsigned int led)
5192 int rc;
5194 tpacpi_leds[led].led = led;
5196 /* LEDs with no name don't get registered */
5197 if (!tpacpi_led_names[led])
5198 return 0;
5200 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5201 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5202 if (led_supported == TPACPI_LED_570)
5203 tpacpi_leds[led].led_classdev.brightness_get =
5204 &led_sysfs_get;
5206 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5208 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5210 rc = led_classdev_register(&tpacpi_pdev->dev,
5211 &tpacpi_leds[led].led_classdev);
5212 if (rc < 0)
5213 tpacpi_leds[led].led_classdev.name = NULL;
5215 return rc;
5218 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5219 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5220 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5221 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5223 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5224 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5225 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5226 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5227 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5228 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5229 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5230 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5232 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5233 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5234 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5235 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5236 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5238 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5239 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5240 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5241 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5243 /* (1) - may have excess leds enabled on MSB */
5245 /* Defaults (order matters, keep last, don't reorder!) */
5246 { /* Lenovo */
5247 .vendor = PCI_VENDOR_ID_LENOVO,
5248 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5249 .quirks = 0x1fffU,
5251 { /* IBM ThinkPads with no EC version string */
5252 .vendor = PCI_VENDOR_ID_IBM,
5253 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5254 .quirks = 0x00ffU,
5256 { /* IBM ThinkPads with EC version string */
5257 .vendor = PCI_VENDOR_ID_IBM,
5258 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5259 .quirks = 0x00bfU,
5263 #undef TPACPI_LEDQ_IBM
5264 #undef TPACPI_LEDQ_LNV
5266 static enum led_access_mode __init led_init_detect_mode(void)
5268 acpi_status status;
5270 if (tpacpi_is_ibm()) {
5271 /* 570 */
5272 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5273 if (ACPI_SUCCESS(status))
5274 return TPACPI_LED_570;
5276 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5277 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5278 if (ACPI_SUCCESS(status))
5279 return TPACPI_LED_OLD;
5282 /* most others */
5283 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5284 if (ACPI_SUCCESS(status))
5285 return TPACPI_LED_NEW;
5287 /* R30, R31, and unknown firmwares */
5288 led_handle = NULL;
5289 return TPACPI_LED_NONE;
5292 static int __init led_init(struct ibm_init_struct *iibm)
5294 unsigned int i;
5295 int rc;
5296 unsigned long useful_leds;
5298 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5300 led_supported = led_init_detect_mode();
5302 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5303 str_supported(led_supported), led_supported);
5305 if (led_supported == TPACPI_LED_NONE)
5306 return 1;
5308 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5309 GFP_KERNEL);
5310 if (!tpacpi_leds) {
5311 pr_err("Out of memory for LED data\n");
5312 return -ENOMEM;
5315 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5316 ARRAY_SIZE(led_useful_qtable));
5318 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5319 if (!tpacpi_is_led_restricted(i) &&
5320 test_bit(i, &useful_leds)) {
5321 rc = tpacpi_init_led(i);
5322 if (rc < 0) {
5323 led_exit();
5324 return rc;
5329 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5330 pr_notice("warning: userspace override of important "
5331 "firmware LEDs is enabled\n");
5332 #endif
5333 return 0;
5336 #define str_led_status(s) \
5337 ((s) == TPACPI_LED_OFF ? "off" : \
5338 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5340 static int led_read(struct seq_file *m)
5342 if (!led_supported) {
5343 seq_printf(m, "status:\t\tnot supported\n");
5344 return 0;
5346 seq_printf(m, "status:\t\tsupported\n");
5348 if (led_supported == TPACPI_LED_570) {
5349 /* 570 */
5350 int i, status;
5351 for (i = 0; i < 8; i++) {
5352 status = led_get_status(i);
5353 if (status < 0)
5354 return -EIO;
5355 seq_printf(m, "%d:\t\t%s\n",
5356 i, str_led_status(status));
5360 seq_printf(m, "commands:\t"
5361 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5363 return 0;
5366 static int led_write(char *buf)
5368 char *cmd;
5369 int led, rc;
5370 enum led_status_t s;
5372 if (!led_supported)
5373 return -ENODEV;
5375 while ((cmd = next_cmd(&buf))) {
5376 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5377 return -EINVAL;
5379 if (strstr(cmd, "off")) {
5380 s = TPACPI_LED_OFF;
5381 } else if (strstr(cmd, "on")) {
5382 s = TPACPI_LED_ON;
5383 } else if (strstr(cmd, "blink")) {
5384 s = TPACPI_LED_BLINK;
5385 } else {
5386 return -EINVAL;
5389 rc = led_set_status(led, s);
5390 if (rc < 0)
5391 return rc;
5394 return 0;
5397 static struct ibm_struct led_driver_data = {
5398 .name = "led",
5399 .read = led_read,
5400 .write = led_write,
5401 .exit = led_exit,
5404 /*************************************************************************
5405 * Beep subdriver
5408 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5410 #define TPACPI_BEEP_Q1 0x0001
5412 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5413 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5414 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5417 static int __init beep_init(struct ibm_init_struct *iibm)
5419 unsigned long quirks;
5421 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5423 TPACPI_ACPIHANDLE_INIT(beep);
5425 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5426 str_supported(beep_handle != NULL));
5428 quirks = tpacpi_check_quirks(beep_quirk_table,
5429 ARRAY_SIZE(beep_quirk_table));
5431 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5433 return (beep_handle)? 0 : 1;
5436 static int beep_read(struct seq_file *m)
5438 if (!beep_handle)
5439 seq_printf(m, "status:\t\tnot supported\n");
5440 else {
5441 seq_printf(m, "status:\t\tsupported\n");
5442 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5445 return 0;
5448 static int beep_write(char *buf)
5450 char *cmd;
5451 int beep_cmd;
5453 if (!beep_handle)
5454 return -ENODEV;
5456 while ((cmd = next_cmd(&buf))) {
5457 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5458 beep_cmd >= 0 && beep_cmd <= 17) {
5459 /* beep_cmd set */
5460 } else
5461 return -EINVAL;
5462 if (tp_features.beep_needs_two_args) {
5463 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5464 beep_cmd, 0))
5465 return -EIO;
5466 } else {
5467 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5468 beep_cmd))
5469 return -EIO;
5473 return 0;
5476 static struct ibm_struct beep_driver_data = {
5477 .name = "beep",
5478 .read = beep_read,
5479 .write = beep_write,
5482 /*************************************************************************
5483 * Thermal subdriver
5486 enum thermal_access_mode {
5487 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5488 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5489 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5490 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5491 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5494 enum { /* TPACPI_THERMAL_TPEC_* */
5495 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5496 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5497 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5499 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5503 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5504 struct ibm_thermal_sensors_struct {
5505 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5508 static enum thermal_access_mode thermal_read_mode;
5510 /* idx is zero-based */
5511 static int thermal_get_sensor(int idx, s32 *value)
5513 int t;
5514 s8 tmp;
5515 char tmpi[5];
5517 t = TP_EC_THERMAL_TMP0;
5519 switch (thermal_read_mode) {
5520 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5521 case TPACPI_THERMAL_TPEC_16:
5522 if (idx >= 8 && idx <= 15) {
5523 t = TP_EC_THERMAL_TMP8;
5524 idx -= 8;
5526 /* fallthrough */
5527 #endif
5528 case TPACPI_THERMAL_TPEC_8:
5529 if (idx <= 7) {
5530 if (!acpi_ec_read(t + idx, &tmp))
5531 return -EIO;
5532 *value = tmp * 1000;
5533 return 0;
5535 break;
5537 case TPACPI_THERMAL_ACPI_UPDT:
5538 if (idx <= 7) {
5539 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5540 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5541 return -EIO;
5542 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5543 return -EIO;
5544 *value = (t - 2732) * 100;
5545 return 0;
5547 break;
5549 case TPACPI_THERMAL_ACPI_TMP07:
5550 if (idx <= 7) {
5551 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5552 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5553 return -EIO;
5554 if (t > 127 || t < -127)
5555 t = TP_EC_THERMAL_TMP_NA;
5556 *value = t * 1000;
5557 return 0;
5559 break;
5561 case TPACPI_THERMAL_NONE:
5562 default:
5563 return -ENOSYS;
5566 return -EINVAL;
5569 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5571 int res, i;
5572 int n;
5574 n = 8;
5575 i = 0;
5577 if (!s)
5578 return -EINVAL;
5580 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5581 n = 16;
5583 for (i = 0 ; i < n; i++) {
5584 res = thermal_get_sensor(i, &s->temp[i]);
5585 if (res)
5586 return res;
5589 return n;
5592 static void thermal_dump_all_sensors(void)
5594 int n, i;
5595 struct ibm_thermal_sensors_struct t;
5597 n = thermal_get_sensors(&t);
5598 if (n <= 0)
5599 return;
5601 pr_notice("temperatures (Celsius):");
5603 for (i = 0; i < n; i++) {
5604 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5605 pr_cont(" %d", (int)(t.temp[i] / 1000));
5606 else
5607 pr_cont(" N/A");
5610 pr_cont("\n");
5613 /* sysfs temp##_input -------------------------------------------------- */
5615 static ssize_t thermal_temp_input_show(struct device *dev,
5616 struct device_attribute *attr,
5617 char *buf)
5619 struct sensor_device_attribute *sensor_attr =
5620 to_sensor_dev_attr(attr);
5621 int idx = sensor_attr->index;
5622 s32 value;
5623 int res;
5625 res = thermal_get_sensor(idx, &value);
5626 if (res)
5627 return res;
5628 if (value == TPACPI_THERMAL_SENSOR_NA)
5629 return -ENXIO;
5631 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5634 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5635 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5636 thermal_temp_input_show, NULL, _idxB)
5638 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5639 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5640 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5641 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5642 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5643 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5644 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5645 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5646 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5647 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5648 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5649 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5650 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5651 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5652 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5653 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5654 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5657 #define THERMAL_ATTRS(X) \
5658 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5660 static struct attribute *thermal_temp_input_attr[] = {
5661 THERMAL_ATTRS(8),
5662 THERMAL_ATTRS(9),
5663 THERMAL_ATTRS(10),
5664 THERMAL_ATTRS(11),
5665 THERMAL_ATTRS(12),
5666 THERMAL_ATTRS(13),
5667 THERMAL_ATTRS(14),
5668 THERMAL_ATTRS(15),
5669 THERMAL_ATTRS(0),
5670 THERMAL_ATTRS(1),
5671 THERMAL_ATTRS(2),
5672 THERMAL_ATTRS(3),
5673 THERMAL_ATTRS(4),
5674 THERMAL_ATTRS(5),
5675 THERMAL_ATTRS(6),
5676 THERMAL_ATTRS(7),
5677 NULL
5680 static const struct attribute_group thermal_temp_input16_group = {
5681 .attrs = thermal_temp_input_attr
5684 static const struct attribute_group thermal_temp_input8_group = {
5685 .attrs = &thermal_temp_input_attr[8]
5688 #undef THERMAL_SENSOR_ATTR_TEMP
5689 #undef THERMAL_ATTRS
5691 /* --------------------------------------------------------------------- */
5693 static int __init thermal_init(struct ibm_init_struct *iibm)
5695 u8 t, ta1, ta2;
5696 int i;
5697 int acpi_tmp7;
5698 int res;
5700 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5702 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5704 if (thinkpad_id.ec_model) {
5706 * Direct EC access mode: sensors at registers
5707 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5708 * non-implemented, thermal sensors return 0x80 when
5709 * not available
5712 ta1 = ta2 = 0;
5713 for (i = 0; i < 8; i++) {
5714 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5715 ta1 |= t;
5716 } else {
5717 ta1 = 0;
5718 break;
5720 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5721 ta2 |= t;
5722 } else {
5723 ta1 = 0;
5724 break;
5727 if (ta1 == 0) {
5728 /* This is sheer paranoia, but we handle it anyway */
5729 if (acpi_tmp7) {
5730 pr_err("ThinkPad ACPI EC access misbehaving, "
5731 "falling back to ACPI TMPx access "
5732 "mode\n");
5733 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5734 } else {
5735 pr_err("ThinkPad ACPI EC access misbehaving, "
5736 "disabling thermal sensors access\n");
5737 thermal_read_mode = TPACPI_THERMAL_NONE;
5739 } else {
5740 thermal_read_mode =
5741 (ta2 != 0) ?
5742 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5744 } else if (acpi_tmp7) {
5745 if (tpacpi_is_ibm() &&
5746 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5747 /* 600e/x, 770e, 770x */
5748 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5749 } else {
5750 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5751 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5753 } else {
5754 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5755 thermal_read_mode = TPACPI_THERMAL_NONE;
5758 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5759 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5760 thermal_read_mode);
5762 switch (thermal_read_mode) {
5763 case TPACPI_THERMAL_TPEC_16:
5764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5765 &thermal_temp_input16_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
5772 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5773 &thermal_temp_input8_group);
5774 if (res)
5775 return res;
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 return 1;
5782 return 0;
5785 static void thermal_exit(void)
5787 switch (thermal_read_mode) {
5788 case TPACPI_THERMAL_TPEC_16:
5789 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5790 &thermal_temp_input16_group);
5791 break;
5792 case TPACPI_THERMAL_TPEC_8:
5793 case TPACPI_THERMAL_ACPI_TMP07:
5794 case TPACPI_THERMAL_ACPI_UPDT:
5795 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5796 &thermal_temp_input8_group);
5797 break;
5798 case TPACPI_THERMAL_NONE:
5799 default:
5800 break;
5804 static int thermal_read(struct seq_file *m)
5806 int n, i;
5807 struct ibm_thermal_sensors_struct t;
5809 n = thermal_get_sensors(&t);
5810 if (unlikely(n < 0))
5811 return n;
5813 seq_printf(m, "temperatures:\t");
5815 if (n > 0) {
5816 for (i = 0; i < (n - 1); i++)
5817 seq_printf(m, "%d ", t.temp[i] / 1000);
5818 seq_printf(m, "%d\n", t.temp[i] / 1000);
5819 } else
5820 seq_printf(m, "not supported\n");
5822 return 0;
5825 static struct ibm_struct thermal_driver_data = {
5826 .name = "thermal",
5827 .read = thermal_read,
5828 .exit = thermal_exit,
5831 /*************************************************************************
5832 * Backlight/brightness subdriver
5835 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5838 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5839 * CMOS NVRAM byte 0x5E, bits 0-3.
5841 * EC HBRV (0x31) has the following layout
5842 * Bit 7: unknown function
5843 * Bit 6: unknown function
5844 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5845 * Bit 4: must be set to zero to avoid problems
5846 * Bit 3-0: backlight brightness level
5848 * brightness_get_raw returns status data in the HBRV layout
5850 * WARNING: The X61 has been verified to use HBRV for something else, so
5851 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5852 * testing on the very early *60 Lenovo models...
5855 enum {
5856 TP_EC_BACKLIGHT = 0x31,
5858 /* TP_EC_BACKLIGHT bitmasks */
5859 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5860 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5861 TP_EC_BACKLIGHT_MAPSW = 0x20,
5864 enum tpacpi_brightness_access_mode {
5865 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5866 TPACPI_BRGHT_MODE_EC, /* EC control */
5867 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5868 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5869 TPACPI_BRGHT_MODE_MAX
5872 static struct backlight_device *ibm_backlight_device;
5874 static enum tpacpi_brightness_access_mode brightness_mode =
5875 TPACPI_BRGHT_MODE_MAX;
5877 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5879 static struct mutex brightness_mutex;
5881 /* NVRAM brightness access,
5882 * call with brightness_mutex held! */
5883 static unsigned int tpacpi_brightness_nvram_get(void)
5885 u8 lnvram;
5887 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5888 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5889 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5890 lnvram &= bright_maxlvl;
5892 return lnvram;
5895 static void tpacpi_brightness_checkpoint_nvram(void)
5897 u8 lec = 0;
5898 u8 b_nvram;
5900 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5901 return;
5903 vdbg_printk(TPACPI_DBG_BRGHT,
5904 "trying to checkpoint backlight level to NVRAM...\n");
5906 if (mutex_lock_killable(&brightness_mutex) < 0)
5907 return;
5909 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5910 goto unlock;
5911 lec &= TP_EC_BACKLIGHT_LVLMSK;
5912 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5914 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5915 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5916 /* NVRAM needs update */
5917 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5918 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5919 b_nvram |= lec;
5920 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5921 dbg_printk(TPACPI_DBG_BRGHT,
5922 "updated NVRAM backlight level to %u (0x%02x)\n",
5923 (unsigned int) lec, (unsigned int) b_nvram);
5924 } else
5925 vdbg_printk(TPACPI_DBG_BRGHT,
5926 "NVRAM backlight level already is %u (0x%02x)\n",
5927 (unsigned int) lec, (unsigned int) b_nvram);
5929 unlock:
5930 mutex_unlock(&brightness_mutex);
5934 /* call with brightness_mutex held! */
5935 static int tpacpi_brightness_get_raw(int *status)
5937 u8 lec = 0;
5939 switch (brightness_mode) {
5940 case TPACPI_BRGHT_MODE_UCMS_STEP:
5941 *status = tpacpi_brightness_nvram_get();
5942 return 0;
5943 case TPACPI_BRGHT_MODE_EC:
5944 case TPACPI_BRGHT_MODE_ECNVRAM:
5945 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5946 return -EIO;
5947 *status = lec;
5948 return 0;
5949 default:
5950 return -ENXIO;
5954 /* call with brightness_mutex held! */
5955 /* do NOT call with illegal backlight level value */
5956 static int tpacpi_brightness_set_ec(unsigned int value)
5958 u8 lec = 0;
5960 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5961 return -EIO;
5963 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5964 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5965 (value & TP_EC_BACKLIGHT_LVLMSK))))
5966 return -EIO;
5968 return 0;
5971 /* call with brightness_mutex held! */
5972 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5974 int cmos_cmd, inc;
5975 unsigned int current_value, i;
5977 current_value = tpacpi_brightness_nvram_get();
5979 if (value == current_value)
5980 return 0;
5982 cmos_cmd = (value > current_value) ?
5983 TP_CMOS_BRIGHTNESS_UP :
5984 TP_CMOS_BRIGHTNESS_DOWN;
5985 inc = (value > current_value) ? 1 : -1;
5987 for (i = current_value; i != value; i += inc)
5988 if (issue_thinkpad_cmos_command(cmos_cmd))
5989 return -EIO;
5991 return 0;
5994 /* May return EINTR which can always be mapped to ERESTARTSYS */
5995 static int brightness_set(unsigned int value)
5997 int res;
5999 if (value > bright_maxlvl || value < 0)
6000 return -EINVAL;
6002 vdbg_printk(TPACPI_DBG_BRGHT,
6003 "set backlight level to %d\n", value);
6005 res = mutex_lock_killable(&brightness_mutex);
6006 if (res < 0)
6007 return res;
6009 switch (brightness_mode) {
6010 case TPACPI_BRGHT_MODE_EC:
6011 case TPACPI_BRGHT_MODE_ECNVRAM:
6012 res = tpacpi_brightness_set_ec(value);
6013 break;
6014 case TPACPI_BRGHT_MODE_UCMS_STEP:
6015 res = tpacpi_brightness_set_ucmsstep(value);
6016 break;
6017 default:
6018 res = -ENXIO;
6021 mutex_unlock(&brightness_mutex);
6022 return res;
6025 /* sysfs backlight class ----------------------------------------------- */
6027 static int brightness_update_status(struct backlight_device *bd)
6029 unsigned int level =
6030 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6031 bd->props.power == FB_BLANK_UNBLANK) ?
6032 bd->props.brightness : 0;
6034 dbg_printk(TPACPI_DBG_BRGHT,
6035 "backlight: attempt to set level to %d\n",
6036 level);
6038 /* it is the backlight class's job (caller) to handle
6039 * EINTR and other errors properly */
6040 return brightness_set(level);
6043 static int brightness_get(struct backlight_device *bd)
6045 int status, res;
6047 res = mutex_lock_killable(&brightness_mutex);
6048 if (res < 0)
6049 return 0;
6051 res = tpacpi_brightness_get_raw(&status);
6053 mutex_unlock(&brightness_mutex);
6055 if (res < 0)
6056 return 0;
6058 return status & TP_EC_BACKLIGHT_LVLMSK;
6061 static void tpacpi_brightness_notify_change(void)
6063 backlight_force_update(ibm_backlight_device,
6064 BACKLIGHT_UPDATE_HOTKEY);
6067 static const struct backlight_ops ibm_backlight_data = {
6068 .get_brightness = brightness_get,
6069 .update_status = brightness_update_status,
6072 /* --------------------------------------------------------------------- */
6075 * Call _BCL method of video device. On some ThinkPads this will
6076 * switch the firmware to the ACPI brightness control mode.
6079 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6081 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6082 union acpi_object *obj;
6083 int rc;
6085 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6086 obj = (union acpi_object *)buffer.pointer;
6087 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6088 pr_err("Unknown _BCL data, please report this to %s\n",
6089 TPACPI_MAIL);
6090 rc = 0;
6091 } else {
6092 rc = obj->package.count;
6094 } else {
6095 return 0;
6098 kfree(buffer.pointer);
6099 return rc;
6104 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6106 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6108 acpi_handle video_device;
6109 int bcl_levels = 0;
6111 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6112 if (video_device)
6113 bcl_levels = tpacpi_query_bcl_levels(video_device);
6115 tp_features.bright_acpimode = (bcl_levels > 0);
6117 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6121 * These are only useful for models that have only one possibility
6122 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6123 * these quirks.
6125 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6126 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6127 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6129 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6130 /* Models with ATI GPUs known to require ECNVRAM mode */
6131 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6133 /* Models with ATI GPUs that can use ECNVRAM */
6134 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6135 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6136 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6137 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6139 /* Models with Intel Extreme Graphics 2 */
6140 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6141 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6142 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6144 /* Models with Intel GMA900 */
6145 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6146 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6147 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6151 * Returns < 0 for error, otherwise sets tp_features.bright_*
6152 * and bright_maxlvl.
6154 static void __init tpacpi_detect_brightness_capabilities(void)
6156 unsigned int b;
6158 vdbg_printk(TPACPI_DBG_INIT,
6159 "detecting firmware brightness interface capabilities\n");
6161 /* we could run a quirks check here (same table used by
6162 * brightness_init) if needed */
6165 * We always attempt to detect acpi support, so as to switch
6166 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6167 * going to publish a backlight interface
6169 b = tpacpi_check_std_acpi_brightness_support();
6170 switch (b) {
6171 case 16:
6172 bright_maxlvl = 15;
6173 pr_info("detected a 16-level brightness capable ThinkPad\n");
6174 break;
6175 case 8:
6176 case 0:
6177 bright_maxlvl = 7;
6178 pr_info("detected a 8-level brightness capable ThinkPad\n");
6179 break;
6180 default:
6181 pr_err("Unsupported brightness interface, "
6182 "please contact %s\n", TPACPI_MAIL);
6183 tp_features.bright_unkfw = 1;
6184 bright_maxlvl = b - 1;
6188 static int __init brightness_init(struct ibm_init_struct *iibm)
6190 struct backlight_properties props;
6191 int b;
6192 unsigned long quirks;
6194 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6196 mutex_init(&brightness_mutex);
6198 quirks = tpacpi_check_quirks(brightness_quirk_table,
6199 ARRAY_SIZE(brightness_quirk_table));
6201 /* tpacpi_detect_brightness_capabilities() must have run already */
6203 /* if it is unknown, we don't handle it: it wouldn't be safe */
6204 if (tp_features.bright_unkfw)
6205 return 1;
6207 if (!brightness_enable) {
6208 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6209 "brightness support disabled by "
6210 "module parameter\n");
6211 return 1;
6214 if (acpi_video_backlight_support()) {
6215 if (brightness_enable > 1) {
6216 pr_info("Standard ACPI backlight interface "
6217 "available, not loading native one\n");
6218 return 1;
6219 } else if (brightness_enable == 1) {
6220 pr_warn("Cannot enable backlight brightness support, "
6221 "ACPI is already handling it. Refer to the "
6222 "acpi_backlight kernel parameter.\n");
6223 return 1;
6225 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6226 pr_notice("Standard ACPI backlight interface not "
6227 "available, thinkpad_acpi native "
6228 "brightness control enabled\n");
6232 * Check for module parameter bogosity, note that we
6233 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6234 * able to detect "unspecified"
6236 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6237 return -EINVAL;
6239 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6240 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6241 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6242 if (quirks & TPACPI_BRGHT_Q_EC)
6243 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6244 else
6245 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6247 dbg_printk(TPACPI_DBG_BRGHT,
6248 "driver auto-selected brightness_mode=%d\n",
6249 brightness_mode);
6252 /* Safety */
6253 if (!tpacpi_is_ibm() &&
6254 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6255 brightness_mode == TPACPI_BRGHT_MODE_EC))
6256 return -EINVAL;
6258 if (tpacpi_brightness_get_raw(&b) < 0)
6259 return 1;
6261 memset(&props, 0, sizeof(struct backlight_properties));
6262 props.type = BACKLIGHT_PLATFORM;
6263 props.max_brightness = bright_maxlvl;
6264 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6265 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6266 NULL, NULL,
6267 &ibm_backlight_data,
6268 &props);
6269 if (IS_ERR(ibm_backlight_device)) {
6270 int rc = PTR_ERR(ibm_backlight_device);
6271 ibm_backlight_device = NULL;
6272 pr_err("Could not register backlight device\n");
6273 return rc;
6275 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6276 "brightness is supported\n");
6278 if (quirks & TPACPI_BRGHT_Q_ASK) {
6279 pr_notice("brightness: will use unverified default: "
6280 "brightness_mode=%d\n", brightness_mode);
6281 pr_notice("brightness: please report to %s whether it works well "
6282 "or not on your ThinkPad\n", TPACPI_MAIL);
6285 /* Added by mistake in early 2007. Probably useless, but it could
6286 * be working around some unknown firmware problem where the value
6287 * read at startup doesn't match the real hardware state... so leave
6288 * it in place just in case */
6289 backlight_update_status(ibm_backlight_device);
6291 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6292 "brightness: registering brightness hotkeys "
6293 "as change notification\n");
6294 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6295 | TP_ACPI_HKEY_BRGHTUP_MASK
6296 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6297 return 0;
6300 static void brightness_suspend(pm_message_t state)
6302 tpacpi_brightness_checkpoint_nvram();
6305 static void brightness_shutdown(void)
6307 tpacpi_brightness_checkpoint_nvram();
6310 static void brightness_exit(void)
6312 if (ibm_backlight_device) {
6313 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6314 "calling backlight_device_unregister()\n");
6315 backlight_device_unregister(ibm_backlight_device);
6318 tpacpi_brightness_checkpoint_nvram();
6321 static int brightness_read(struct seq_file *m)
6323 int level;
6325 level = brightness_get(NULL);
6326 if (level < 0) {
6327 seq_printf(m, "level:\t\tunreadable\n");
6328 } else {
6329 seq_printf(m, "level:\t\t%d\n", level);
6330 seq_printf(m, "commands:\tup, down\n");
6331 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6332 bright_maxlvl);
6335 return 0;
6338 static int brightness_write(char *buf)
6340 int level;
6341 int rc;
6342 char *cmd;
6344 level = brightness_get(NULL);
6345 if (level < 0)
6346 return level;
6348 while ((cmd = next_cmd(&buf))) {
6349 if (strlencmp(cmd, "up") == 0) {
6350 if (level < bright_maxlvl)
6351 level++;
6352 } else if (strlencmp(cmd, "down") == 0) {
6353 if (level > 0)
6354 level--;
6355 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6356 level >= 0 && level <= bright_maxlvl) {
6357 /* new level set */
6358 } else
6359 return -EINVAL;
6362 tpacpi_disclose_usertask("procfs brightness",
6363 "set level to %d\n", level);
6366 * Now we know what the final level should be, so we try to set it.
6367 * Doing it this way makes the syscall restartable in case of EINTR
6369 rc = brightness_set(level);
6370 if (!rc && ibm_backlight_device)
6371 backlight_force_update(ibm_backlight_device,
6372 BACKLIGHT_UPDATE_SYSFS);
6373 return (rc == -EINTR)? -ERESTARTSYS : rc;
6376 static struct ibm_struct brightness_driver_data = {
6377 .name = "brightness",
6378 .read = brightness_read,
6379 .write = brightness_write,
6380 .exit = brightness_exit,
6381 .suspend = brightness_suspend,
6382 .shutdown = brightness_shutdown,
6385 /*************************************************************************
6386 * Volume subdriver
6390 * IBM ThinkPads have a simple volume controller with MUTE gating.
6391 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6393 * Since the *61 series (and probably also the later *60 series), Lenovo
6394 * ThinkPads only implement the MUTE gate.
6396 * EC register 0x30
6397 * Bit 6: MUTE (1 mutes sound)
6398 * Bit 3-0: Volume
6399 * Other bits should be zero as far as we know.
6401 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6402 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6403 * such as bit 7 which is used to detect repeated presses of MUTE,
6404 * and we leave them unchanged.
6407 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6409 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6410 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6411 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6413 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6414 static char *alsa_id = "ThinkPadEC";
6415 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6417 struct tpacpi_alsa_data {
6418 struct snd_card *card;
6419 struct snd_ctl_elem_id *ctl_mute_id;
6420 struct snd_ctl_elem_id *ctl_vol_id;
6423 static struct snd_card *alsa_card;
6425 enum {
6426 TP_EC_AUDIO = 0x30,
6428 /* TP_EC_AUDIO bits */
6429 TP_EC_AUDIO_MUTESW = 6,
6431 /* TP_EC_AUDIO bitmasks */
6432 TP_EC_AUDIO_LVL_MSK = 0x0F,
6433 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6435 /* Maximum volume */
6436 TP_EC_VOLUME_MAX = 14,
6439 enum tpacpi_volume_access_mode {
6440 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6441 TPACPI_VOL_MODE_EC, /* Pure EC control */
6442 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6443 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6444 TPACPI_VOL_MODE_MAX
6447 enum tpacpi_volume_capabilities {
6448 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6449 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6450 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6451 TPACPI_VOL_CAP_MAX
6454 static enum tpacpi_volume_access_mode volume_mode =
6455 TPACPI_VOL_MODE_MAX;
6457 static enum tpacpi_volume_capabilities volume_capabilities;
6458 static int volume_control_allowed;
6461 * Used to syncronize writers to TP_EC_AUDIO and
6462 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6464 static struct mutex volume_mutex;
6466 static void tpacpi_volume_checkpoint_nvram(void)
6468 u8 lec = 0;
6469 u8 b_nvram;
6470 u8 ec_mask;
6472 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6473 return;
6474 if (!volume_control_allowed)
6475 return;
6477 vdbg_printk(TPACPI_DBG_MIXER,
6478 "trying to checkpoint mixer state to NVRAM...\n");
6480 if (tp_features.mixer_no_level_control)
6481 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6482 else
6483 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6485 if (mutex_lock_killable(&volume_mutex) < 0)
6486 return;
6488 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6489 goto unlock;
6490 lec &= ec_mask;
6491 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6493 if (lec != (b_nvram & ec_mask)) {
6494 /* NVRAM needs update */
6495 b_nvram &= ~ec_mask;
6496 b_nvram |= lec;
6497 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6498 dbg_printk(TPACPI_DBG_MIXER,
6499 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6500 (unsigned int) lec, (unsigned int) b_nvram);
6501 } else {
6502 vdbg_printk(TPACPI_DBG_MIXER,
6503 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6504 (unsigned int) lec, (unsigned int) b_nvram);
6507 unlock:
6508 mutex_unlock(&volume_mutex);
6511 static int volume_get_status_ec(u8 *status)
6513 u8 s;
6515 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6516 return -EIO;
6518 *status = s;
6520 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6522 return 0;
6525 static int volume_get_status(u8 *status)
6527 return volume_get_status_ec(status);
6530 static int volume_set_status_ec(const u8 status)
6532 if (!acpi_ec_write(TP_EC_AUDIO, status))
6533 return -EIO;
6535 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6537 return 0;
6540 static int volume_set_status(const u8 status)
6542 return volume_set_status_ec(status);
6545 /* returns < 0 on error, 0 on no change, 1 on change */
6546 static int __volume_set_mute_ec(const bool mute)
6548 int rc;
6549 u8 s, n;
6551 if (mutex_lock_killable(&volume_mutex) < 0)
6552 return -EINTR;
6554 rc = volume_get_status_ec(&s);
6555 if (rc)
6556 goto unlock;
6558 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6559 s & ~TP_EC_AUDIO_MUTESW_MSK;
6561 if (n != s) {
6562 rc = volume_set_status_ec(n);
6563 if (!rc)
6564 rc = 1;
6567 unlock:
6568 mutex_unlock(&volume_mutex);
6569 return rc;
6572 static int volume_alsa_set_mute(const bool mute)
6574 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6575 (mute) ? "" : "un");
6576 return __volume_set_mute_ec(mute);
6579 static int volume_set_mute(const bool mute)
6581 int rc;
6583 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6584 (mute) ? "" : "un");
6586 rc = __volume_set_mute_ec(mute);
6587 return (rc < 0) ? rc : 0;
6590 /* returns < 0 on error, 0 on no change, 1 on change */
6591 static int __volume_set_volume_ec(const u8 vol)
6593 int rc;
6594 u8 s, n;
6596 if (vol > TP_EC_VOLUME_MAX)
6597 return -EINVAL;
6599 if (mutex_lock_killable(&volume_mutex) < 0)
6600 return -EINTR;
6602 rc = volume_get_status_ec(&s);
6603 if (rc)
6604 goto unlock;
6606 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6608 if (n != s) {
6609 rc = volume_set_status_ec(n);
6610 if (!rc)
6611 rc = 1;
6614 unlock:
6615 mutex_unlock(&volume_mutex);
6616 return rc;
6619 static int volume_alsa_set_volume(const u8 vol)
6621 dbg_printk(TPACPI_DBG_MIXER,
6622 "ALSA: trying to set volume level to %hu\n", vol);
6623 return __volume_set_volume_ec(vol);
6626 static void volume_alsa_notify_change(void)
6628 struct tpacpi_alsa_data *d;
6630 if (alsa_card && alsa_card->private_data) {
6631 d = alsa_card->private_data;
6632 if (d->ctl_mute_id)
6633 snd_ctl_notify(alsa_card,
6634 SNDRV_CTL_EVENT_MASK_VALUE,
6635 d->ctl_mute_id);
6636 if (d->ctl_vol_id)
6637 snd_ctl_notify(alsa_card,
6638 SNDRV_CTL_EVENT_MASK_VALUE,
6639 d->ctl_vol_id);
6643 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6644 struct snd_ctl_elem_info *uinfo)
6646 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6647 uinfo->count = 1;
6648 uinfo->value.integer.min = 0;
6649 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6650 return 0;
6653 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6654 struct snd_ctl_elem_value *ucontrol)
6656 u8 s;
6657 int rc;
6659 rc = volume_get_status(&s);
6660 if (rc < 0)
6661 return rc;
6663 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6664 return 0;
6667 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6668 struct snd_ctl_elem_value *ucontrol)
6670 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6671 ucontrol->value.integer.value[0]);
6672 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6675 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6677 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6678 struct snd_ctl_elem_value *ucontrol)
6680 u8 s;
6681 int rc;
6683 rc = volume_get_status(&s);
6684 if (rc < 0)
6685 return rc;
6687 ucontrol->value.integer.value[0] =
6688 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6689 return 0;
6692 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6693 struct snd_ctl_elem_value *ucontrol)
6695 tpacpi_disclose_usertask("ALSA", "%smute\n",
6696 ucontrol->value.integer.value[0] ?
6697 "un" : "");
6698 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6701 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6702 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6703 .name = "Console Playback Volume",
6704 .index = 0,
6705 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6706 .info = volume_alsa_vol_info,
6707 .get = volume_alsa_vol_get,
6710 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6711 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6712 .name = "Console Playback Switch",
6713 .index = 0,
6714 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6715 .info = volume_alsa_mute_info,
6716 .get = volume_alsa_mute_get,
6719 static void volume_suspend(pm_message_t state)
6721 tpacpi_volume_checkpoint_nvram();
6724 static void volume_resume(void)
6726 volume_alsa_notify_change();
6729 static void volume_shutdown(void)
6731 tpacpi_volume_checkpoint_nvram();
6734 static void volume_exit(void)
6736 if (alsa_card) {
6737 snd_card_free(alsa_card);
6738 alsa_card = NULL;
6741 tpacpi_volume_checkpoint_nvram();
6744 static int __init volume_create_alsa_mixer(void)
6746 struct snd_card *card;
6747 struct tpacpi_alsa_data *data;
6748 struct snd_kcontrol *ctl_vol;
6749 struct snd_kcontrol *ctl_mute;
6750 int rc;
6752 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6753 sizeof(struct tpacpi_alsa_data), &card);
6754 if (rc < 0 || !card) {
6755 pr_err("Failed to create ALSA card structures: %d\n", rc);
6756 return 1;
6759 BUG_ON(!card->private_data);
6760 data = card->private_data;
6761 data->card = card;
6763 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6764 sizeof(card->driver));
6765 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6766 sizeof(card->shortname));
6767 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6768 (thinkpad_id.ec_version_str) ?
6769 thinkpad_id.ec_version_str : "(unknown)");
6770 snprintf(card->longname, sizeof(card->longname),
6771 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6772 (thinkpad_id.ec_version_str) ?
6773 thinkpad_id.ec_version_str : "unknown");
6775 if (volume_control_allowed) {
6776 volume_alsa_control_vol.put = volume_alsa_vol_put;
6777 volume_alsa_control_vol.access =
6778 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6780 volume_alsa_control_mute.put = volume_alsa_mute_put;
6781 volume_alsa_control_mute.access =
6782 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6785 if (!tp_features.mixer_no_level_control) {
6786 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6787 rc = snd_ctl_add(card, ctl_vol);
6788 if (rc < 0) {
6789 pr_err("Failed to create ALSA volume control: %d\n",
6790 rc);
6791 goto err_exit;
6793 data->ctl_vol_id = &ctl_vol->id;
6796 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6797 rc = snd_ctl_add(card, ctl_mute);
6798 if (rc < 0) {
6799 pr_err("Failed to create ALSA mute control: %d\n", rc);
6800 goto err_exit;
6802 data->ctl_mute_id = &ctl_mute->id;
6804 snd_card_set_dev(card, &tpacpi_pdev->dev);
6805 rc = snd_card_register(card);
6806 if (rc < 0) {
6807 pr_err("Failed to register ALSA card: %d\n", rc);
6808 goto err_exit;
6811 alsa_card = card;
6812 return 0;
6814 err_exit:
6815 snd_card_free(card);
6816 return 1;
6819 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6820 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6822 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6823 /* Whitelist volume level on all IBM by default */
6824 { .vendor = PCI_VENDOR_ID_IBM,
6825 .bios = TPACPI_MATCH_ANY,
6826 .ec = TPACPI_MATCH_ANY,
6827 .quirks = TPACPI_VOL_Q_LEVEL },
6829 /* Lenovo models with volume control (needs confirmation) */
6830 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6831 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6832 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6833 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6834 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6835 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6836 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6838 /* Whitelist mute-only on all Lenovo by default */
6839 { .vendor = PCI_VENDOR_ID_LENOVO,
6840 .bios = TPACPI_MATCH_ANY,
6841 .ec = TPACPI_MATCH_ANY,
6842 .quirks = TPACPI_VOL_Q_MUTEONLY }
6845 static int __init volume_init(struct ibm_init_struct *iibm)
6847 unsigned long quirks;
6848 int rc;
6850 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6852 mutex_init(&volume_mutex);
6855 * Check for module parameter bogosity, note that we
6856 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6857 * able to detect "unspecified"
6859 if (volume_mode > TPACPI_VOL_MODE_MAX)
6860 return -EINVAL;
6862 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6863 pr_err("UCMS step volume mode not implemented, "
6864 "please contact %s\n", TPACPI_MAIL);
6865 return 1;
6868 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6869 return -EINVAL;
6872 * The ALSA mixer is our primary interface.
6873 * When disabled, don't install the subdriver at all
6875 if (!alsa_enable) {
6876 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6877 "ALSA mixer disabled by parameter, "
6878 "not loading volume subdriver...\n");
6879 return 1;
6882 quirks = tpacpi_check_quirks(volume_quirk_table,
6883 ARRAY_SIZE(volume_quirk_table));
6885 switch (volume_capabilities) {
6886 case TPACPI_VOL_CAP_AUTO:
6887 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6888 tp_features.mixer_no_level_control = 1;
6889 else if (quirks & TPACPI_VOL_Q_LEVEL)
6890 tp_features.mixer_no_level_control = 0;
6891 else
6892 return 1; /* no mixer */
6893 break;
6894 case TPACPI_VOL_CAP_VOLMUTE:
6895 tp_features.mixer_no_level_control = 0;
6896 break;
6897 case TPACPI_VOL_CAP_MUTEONLY:
6898 tp_features.mixer_no_level_control = 1;
6899 break;
6900 default:
6901 return 1;
6904 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6905 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6906 "using user-supplied volume_capabilities=%d\n",
6907 volume_capabilities);
6909 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6910 volume_mode == TPACPI_VOL_MODE_MAX) {
6911 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6913 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6914 "driver auto-selected volume_mode=%d\n",
6915 volume_mode);
6916 } else {
6917 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6918 "using user-supplied volume_mode=%d\n",
6919 volume_mode);
6922 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6923 "mute is supported, volume control is %s\n",
6924 str_supported(!tp_features.mixer_no_level_control));
6926 rc = volume_create_alsa_mixer();
6927 if (rc) {
6928 pr_err("Could not create the ALSA mixer interface\n");
6929 return rc;
6932 pr_info("Console audio control enabled, mode: %s\n",
6933 (volume_control_allowed) ?
6934 "override (read/write)" :
6935 "monitor (read only)");
6937 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6938 "registering volume hotkeys as change notification\n");
6939 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6940 | TP_ACPI_HKEY_VOLUP_MASK
6941 | TP_ACPI_HKEY_VOLDWN_MASK
6942 | TP_ACPI_HKEY_MUTE_MASK);
6944 return 0;
6947 static int volume_read(struct seq_file *m)
6949 u8 status;
6951 if (volume_get_status(&status) < 0) {
6952 seq_printf(m, "level:\t\tunreadable\n");
6953 } else {
6954 if (tp_features.mixer_no_level_control)
6955 seq_printf(m, "level:\t\tunsupported\n");
6956 else
6957 seq_printf(m, "level:\t\t%d\n",
6958 status & TP_EC_AUDIO_LVL_MSK);
6960 seq_printf(m, "mute:\t\t%s\n",
6961 onoff(status, TP_EC_AUDIO_MUTESW));
6963 if (volume_control_allowed) {
6964 seq_printf(m, "commands:\tunmute, mute\n");
6965 if (!tp_features.mixer_no_level_control) {
6966 seq_printf(m,
6967 "commands:\tup, down\n");
6968 seq_printf(m,
6969 "commands:\tlevel <level>"
6970 " (<level> is 0-%d)\n",
6971 TP_EC_VOLUME_MAX);
6976 return 0;
6979 static int volume_write(char *buf)
6981 u8 s;
6982 u8 new_level, new_mute;
6983 int l;
6984 char *cmd;
6985 int rc;
6988 * We do allow volume control at driver startup, so that the
6989 * user can set initial state through the volume=... parameter hack.
6991 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6992 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6993 tp_warned.volume_ctrl_forbidden = 1;
6994 pr_notice("Console audio control in monitor mode, "
6995 "changes are not allowed\n");
6996 pr_notice("Use the volume_control=1 module parameter "
6997 "to enable volume control\n");
6999 return -EPERM;
7002 rc = volume_get_status(&s);
7003 if (rc < 0)
7004 return rc;
7006 new_level = s & TP_EC_AUDIO_LVL_MSK;
7007 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7009 while ((cmd = next_cmd(&buf))) {
7010 if (!tp_features.mixer_no_level_control) {
7011 if (strlencmp(cmd, "up") == 0) {
7012 if (new_mute)
7013 new_mute = 0;
7014 else if (new_level < TP_EC_VOLUME_MAX)
7015 new_level++;
7016 continue;
7017 } else if (strlencmp(cmd, "down") == 0) {
7018 if (new_mute)
7019 new_mute = 0;
7020 else if (new_level > 0)
7021 new_level--;
7022 continue;
7023 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7024 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7025 new_level = l;
7026 continue;
7029 if (strlencmp(cmd, "mute") == 0)
7030 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7031 else if (strlencmp(cmd, "unmute") == 0)
7032 new_mute = 0;
7033 else
7034 return -EINVAL;
7037 if (tp_features.mixer_no_level_control) {
7038 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7039 new_mute ? "" : "un");
7040 rc = volume_set_mute(!!new_mute);
7041 } else {
7042 tpacpi_disclose_usertask("procfs volume",
7043 "%smute and set level to %d\n",
7044 new_mute ? "" : "un", new_level);
7045 rc = volume_set_status(new_mute | new_level);
7047 volume_alsa_notify_change();
7049 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7052 static struct ibm_struct volume_driver_data = {
7053 .name = "volume",
7054 .read = volume_read,
7055 .write = volume_write,
7056 .exit = volume_exit,
7057 .suspend = volume_suspend,
7058 .resume = volume_resume,
7059 .shutdown = volume_shutdown,
7062 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7064 #define alsa_card NULL
7066 static void inline volume_alsa_notify_change(void)
7070 static int __init volume_init(struct ibm_init_struct *iibm)
7072 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7074 return 1;
7077 static struct ibm_struct volume_driver_data = {
7078 .name = "volume",
7081 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7083 /*************************************************************************
7084 * Fan subdriver
7088 * FAN ACCESS MODES
7090 * TPACPI_FAN_RD_ACPI_GFAN:
7091 * ACPI GFAN method: returns fan level
7093 * see TPACPI_FAN_WR_ACPI_SFAN
7094 * EC 0x2f (HFSP) not available if GFAN exists
7096 * TPACPI_FAN_WR_ACPI_SFAN:
7097 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7099 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7100 * it for writing.
7102 * TPACPI_FAN_WR_TPEC:
7103 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7104 * Supported on almost all ThinkPads
7106 * Fan speed changes of any sort (including those caused by the
7107 * disengaged mode) are usually done slowly by the firmware as the
7108 * maximum amount of fan duty cycle change per second seems to be
7109 * limited.
7111 * Reading is not available if GFAN exists.
7112 * Writing is not available if SFAN exists.
7114 * Bits
7115 * 7 automatic mode engaged;
7116 * (default operation mode of the ThinkPad)
7117 * fan level is ignored in this mode.
7118 * 6 full speed mode (takes precedence over bit 7);
7119 * not available on all thinkpads. May disable
7120 * the tachometer while the fan controller ramps up
7121 * the speed (which can take up to a few *minutes*).
7122 * Speeds up fan to 100% duty-cycle, which is far above
7123 * the standard RPM levels. It is not impossible that
7124 * it could cause hardware damage.
7125 * 5-3 unused in some models. Extra bits for fan level
7126 * in others, but still useless as all values above
7127 * 7 map to the same speed as level 7 in these models.
7128 * 2-0 fan level (0..7 usually)
7129 * 0x00 = stop
7130 * 0x07 = max (set when temperatures critical)
7131 * Some ThinkPads may have other levels, see
7132 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7134 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7135 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7136 * does so, its initial value is meaningless (0x07).
7138 * For firmware bugs, refer to:
7139 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7141 * ----
7143 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7144 * Main fan tachometer reading (in RPM)
7146 * This register is present on all ThinkPads with a new-style EC, and
7147 * it is known not to be present on the A21m/e, and T22, as there is
7148 * something else in offset 0x84 according to the ACPI DSDT. Other
7149 * ThinkPads from this same time period (and earlier) probably lack the
7150 * tachometer as well.
7152 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7153 * was never fixed by IBM to report the EC firmware version string
7154 * probably support the tachometer (like the early X models), so
7155 * detecting it is quite hard. We need more data to know for sure.
7157 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7158 * might result.
7160 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7161 * mode.
7163 * For firmware bugs, refer to:
7164 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7166 * ----
7168 * ThinkPad EC register 0x31 bit 0 (only on select models)
7170 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7171 * show the speed of the main fan. When bit 0 of EC register 0x31
7172 * is one, the tachometer registers show the speed of the auxiliary
7173 * fan.
7175 * Fan control seems to affect both fans, regardless of the state
7176 * of this bit.
7178 * So far, only the firmware for the X60/X61 non-tablet versions
7179 * seem to support this (firmware TP-7M).
7181 * TPACPI_FAN_WR_ACPI_FANS:
7182 * ThinkPad X31, X40, X41. Not available in the X60.
7184 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7185 * high speed. ACPI DSDT seems to map these three speeds to levels
7186 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7187 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7189 * The speeds are stored on handles
7190 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7192 * There are three default speed sets, accessible as handles:
7193 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7195 * ACPI DSDT switches which set is in use depending on various
7196 * factors.
7198 * TPACPI_FAN_WR_TPEC is also available and should be used to
7199 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7200 * but the ACPI tables just mention level 7.
7203 enum { /* Fan control constants */
7204 fan_status_offset = 0x2f, /* EC register 0x2f */
7205 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7206 * 0x84 must be read before 0x85 */
7207 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7208 bit 0 selects which fan is active */
7210 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7211 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7213 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7216 enum fan_status_access_mode {
7217 TPACPI_FAN_NONE = 0, /* No fan status or control */
7218 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7219 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7222 enum fan_control_access_mode {
7223 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7224 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7225 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7226 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7229 enum fan_control_commands {
7230 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7231 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7232 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7233 * and also watchdog cmd */
7236 static int fan_control_allowed;
7238 static enum fan_status_access_mode fan_status_access_mode;
7239 static enum fan_control_access_mode fan_control_access_mode;
7240 static enum fan_control_commands fan_control_commands;
7242 static u8 fan_control_initial_status;
7243 static u8 fan_control_desired_level;
7244 static u8 fan_control_resume_level;
7245 static int fan_watchdog_maxinterval;
7247 static struct mutex fan_mutex;
7249 static void fan_watchdog_fire(struct work_struct *ignored);
7250 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7252 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7253 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7254 "\\FSPD", /* 600e/x, 770e, 770x */
7255 ); /* all others */
7256 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7257 "JFNS", /* 770x-JL */
7258 ); /* all others */
7261 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7262 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7263 * be in auto mode (0x80).
7265 * This is corrected by any write to HFSP either by the driver, or
7266 * by the firmware.
7268 * We assume 0x07 really means auto mode while this quirk is active,
7269 * as this is far more likely than the ThinkPad being in level 7,
7270 * which is only used by the firmware during thermal emergencies.
7272 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7273 * TP-70 (T43, R52), which are known to be buggy.
7276 static void fan_quirk1_setup(void)
7278 if (fan_control_initial_status == 0x07) {
7279 pr_notice("fan_init: initial fan status is unknown, "
7280 "assuming it is in auto mode\n");
7281 tp_features.fan_ctrl_status_undef = 1;
7285 static void fan_quirk1_handle(u8 *fan_status)
7287 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7288 if (*fan_status != fan_control_initial_status) {
7289 /* something changed the HFSP regisnter since
7290 * driver init time, so it is not undefined
7291 * anymore */
7292 tp_features.fan_ctrl_status_undef = 0;
7293 } else {
7294 /* Return most likely status. In fact, it
7295 * might be the only possible status */
7296 *fan_status = TP_EC_FAN_AUTO;
7301 /* Select main fan on X60/X61, NOOP on others */
7302 static bool fan_select_fan1(void)
7304 if (tp_features.second_fan) {
7305 u8 val;
7307 if (ec_read(fan_select_offset, &val) < 0)
7308 return false;
7309 val &= 0xFEU;
7310 if (ec_write(fan_select_offset, val) < 0)
7311 return false;
7313 return true;
7316 /* Select secondary fan on X60/X61 */
7317 static bool fan_select_fan2(void)
7319 u8 val;
7321 if (!tp_features.second_fan)
7322 return false;
7324 if (ec_read(fan_select_offset, &val) < 0)
7325 return false;
7326 val |= 0x01U;
7327 if (ec_write(fan_select_offset, val) < 0)
7328 return false;
7330 return true;
7334 * Call with fan_mutex held
7336 static void fan_update_desired_level(u8 status)
7338 if ((status &
7339 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7340 if (status > 7)
7341 fan_control_desired_level = 7;
7342 else
7343 fan_control_desired_level = status;
7347 static int fan_get_status(u8 *status)
7349 u8 s;
7351 /* TODO:
7352 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7354 switch (fan_status_access_mode) {
7355 case TPACPI_FAN_RD_ACPI_GFAN:
7356 /* 570, 600e/x, 770e, 770x */
7358 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7359 return -EIO;
7361 if (likely(status))
7362 *status = s & 0x07;
7364 break;
7366 case TPACPI_FAN_RD_TPEC:
7367 /* all except 570, 600e/x, 770e, 770x */
7368 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7369 return -EIO;
7371 if (likely(status)) {
7372 *status = s;
7373 fan_quirk1_handle(status);
7376 break;
7378 default:
7379 return -ENXIO;
7382 return 0;
7385 static int fan_get_status_safe(u8 *status)
7387 int rc;
7388 u8 s;
7390 if (mutex_lock_killable(&fan_mutex))
7391 return -ERESTARTSYS;
7392 rc = fan_get_status(&s);
7393 if (!rc)
7394 fan_update_desired_level(s);
7395 mutex_unlock(&fan_mutex);
7397 if (status)
7398 *status = s;
7400 return rc;
7403 static int fan_get_speed(unsigned int *speed)
7405 u8 hi, lo;
7407 switch (fan_status_access_mode) {
7408 case TPACPI_FAN_RD_TPEC:
7409 /* all except 570, 600e/x, 770e, 770x */
7410 if (unlikely(!fan_select_fan1()))
7411 return -EIO;
7412 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7413 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7414 return -EIO;
7416 if (likely(speed))
7417 *speed = (hi << 8) | lo;
7419 break;
7421 default:
7422 return -ENXIO;
7425 return 0;
7428 static int fan2_get_speed(unsigned int *speed)
7430 u8 hi, lo;
7431 bool rc;
7433 switch (fan_status_access_mode) {
7434 case TPACPI_FAN_RD_TPEC:
7435 /* all except 570, 600e/x, 770e, 770x */
7436 if (unlikely(!fan_select_fan2()))
7437 return -EIO;
7438 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7439 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7440 fan_select_fan1(); /* play it safe */
7441 if (rc)
7442 return -EIO;
7444 if (likely(speed))
7445 *speed = (hi << 8) | lo;
7447 break;
7449 default:
7450 return -ENXIO;
7453 return 0;
7456 static int fan_set_level(int level)
7458 if (!fan_control_allowed)
7459 return -EPERM;
7461 switch (fan_control_access_mode) {
7462 case TPACPI_FAN_WR_ACPI_SFAN:
7463 if (level >= 0 && level <= 7) {
7464 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7465 return -EIO;
7466 } else
7467 return -EINVAL;
7468 break;
7470 case TPACPI_FAN_WR_ACPI_FANS:
7471 case TPACPI_FAN_WR_TPEC:
7472 if (!(level & TP_EC_FAN_AUTO) &&
7473 !(level & TP_EC_FAN_FULLSPEED) &&
7474 ((level < 0) || (level > 7)))
7475 return -EINVAL;
7477 /* safety net should the EC not support AUTO
7478 * or FULLSPEED mode bits and just ignore them */
7479 if (level & TP_EC_FAN_FULLSPEED)
7480 level |= 7; /* safety min speed 7 */
7481 else if (level & TP_EC_FAN_AUTO)
7482 level |= 4; /* safety min speed 4 */
7484 if (!acpi_ec_write(fan_status_offset, level))
7485 return -EIO;
7486 else
7487 tp_features.fan_ctrl_status_undef = 0;
7488 break;
7490 default:
7491 return -ENXIO;
7494 vdbg_printk(TPACPI_DBG_FAN,
7495 "fan control: set fan control register to 0x%02x\n", level);
7496 return 0;
7499 static int fan_set_level_safe(int level)
7501 int rc;
7503 if (!fan_control_allowed)
7504 return -EPERM;
7506 if (mutex_lock_killable(&fan_mutex))
7507 return -ERESTARTSYS;
7509 if (level == TPACPI_FAN_LAST_LEVEL)
7510 level = fan_control_desired_level;
7512 rc = fan_set_level(level);
7513 if (!rc)
7514 fan_update_desired_level(level);
7516 mutex_unlock(&fan_mutex);
7517 return rc;
7520 static int fan_set_enable(void)
7522 u8 s;
7523 int rc;
7525 if (!fan_control_allowed)
7526 return -EPERM;
7528 if (mutex_lock_killable(&fan_mutex))
7529 return -ERESTARTSYS;
7531 switch (fan_control_access_mode) {
7532 case TPACPI_FAN_WR_ACPI_FANS:
7533 case TPACPI_FAN_WR_TPEC:
7534 rc = fan_get_status(&s);
7535 if (rc < 0)
7536 break;
7538 /* Don't go out of emergency fan mode */
7539 if (s != 7) {
7540 s &= 0x07;
7541 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7544 if (!acpi_ec_write(fan_status_offset, s))
7545 rc = -EIO;
7546 else {
7547 tp_features.fan_ctrl_status_undef = 0;
7548 rc = 0;
7550 break;
7552 case TPACPI_FAN_WR_ACPI_SFAN:
7553 rc = fan_get_status(&s);
7554 if (rc < 0)
7555 break;
7557 s &= 0x07;
7559 /* Set fan to at least level 4 */
7560 s |= 4;
7562 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7563 rc = -EIO;
7564 else
7565 rc = 0;
7566 break;
7568 default:
7569 rc = -ENXIO;
7572 mutex_unlock(&fan_mutex);
7574 if (!rc)
7575 vdbg_printk(TPACPI_DBG_FAN,
7576 "fan control: set fan control register to 0x%02x\n",
7578 return rc;
7581 static int fan_set_disable(void)
7583 int rc;
7585 if (!fan_control_allowed)
7586 return -EPERM;
7588 if (mutex_lock_killable(&fan_mutex))
7589 return -ERESTARTSYS;
7591 rc = 0;
7592 switch (fan_control_access_mode) {
7593 case TPACPI_FAN_WR_ACPI_FANS:
7594 case TPACPI_FAN_WR_TPEC:
7595 if (!acpi_ec_write(fan_status_offset, 0x00))
7596 rc = -EIO;
7597 else {
7598 fan_control_desired_level = 0;
7599 tp_features.fan_ctrl_status_undef = 0;
7601 break;
7603 case TPACPI_FAN_WR_ACPI_SFAN:
7604 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7605 rc = -EIO;
7606 else
7607 fan_control_desired_level = 0;
7608 break;
7610 default:
7611 rc = -ENXIO;
7614 if (!rc)
7615 vdbg_printk(TPACPI_DBG_FAN,
7616 "fan control: set fan control register to 0\n");
7618 mutex_unlock(&fan_mutex);
7619 return rc;
7622 static int fan_set_speed(int speed)
7624 int rc;
7626 if (!fan_control_allowed)
7627 return -EPERM;
7629 if (mutex_lock_killable(&fan_mutex))
7630 return -ERESTARTSYS;
7632 rc = 0;
7633 switch (fan_control_access_mode) {
7634 case TPACPI_FAN_WR_ACPI_FANS:
7635 if (speed >= 0 && speed <= 65535) {
7636 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7637 speed, speed, speed))
7638 rc = -EIO;
7639 } else
7640 rc = -EINVAL;
7641 break;
7643 default:
7644 rc = -ENXIO;
7647 mutex_unlock(&fan_mutex);
7648 return rc;
7651 static void fan_watchdog_reset(void)
7653 static int fan_watchdog_active;
7655 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7656 return;
7658 if (fan_watchdog_active)
7659 cancel_delayed_work(&fan_watchdog_task);
7661 if (fan_watchdog_maxinterval > 0 &&
7662 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7663 fan_watchdog_active = 1;
7664 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7665 msecs_to_jiffies(fan_watchdog_maxinterval
7666 * 1000))) {
7667 pr_err("failed to queue the fan watchdog, "
7668 "watchdog will not trigger\n");
7670 } else
7671 fan_watchdog_active = 0;
7674 static void fan_watchdog_fire(struct work_struct *ignored)
7676 int rc;
7678 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7679 return;
7681 pr_notice("fan watchdog: enabling fan\n");
7682 rc = fan_set_enable();
7683 if (rc < 0) {
7684 pr_err("fan watchdog: error %d while enabling fan, "
7685 "will try again later...\n", -rc);
7686 /* reschedule for later */
7687 fan_watchdog_reset();
7692 * SYSFS fan layout: hwmon compatible (device)
7694 * pwm*_enable:
7695 * 0: "disengaged" mode
7696 * 1: manual mode
7697 * 2: native EC "auto" mode (recommended, hardware default)
7699 * pwm*: set speed in manual mode, ignored otherwise.
7700 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7701 * interpolation.
7703 * fan*_input: tachometer reading, RPM
7706 * SYSFS fan layout: extensions
7708 * fan_watchdog (driver):
7709 * fan watchdog interval in seconds, 0 disables (default), max 120
7712 /* sysfs fan pwm1_enable ----------------------------------------------- */
7713 static ssize_t fan_pwm1_enable_show(struct device *dev,
7714 struct device_attribute *attr,
7715 char *buf)
7717 int res, mode;
7718 u8 status;
7720 res = fan_get_status_safe(&status);
7721 if (res)
7722 return res;
7724 if (status & TP_EC_FAN_FULLSPEED) {
7725 mode = 0;
7726 } else if (status & TP_EC_FAN_AUTO) {
7727 mode = 2;
7728 } else
7729 mode = 1;
7731 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7734 static ssize_t fan_pwm1_enable_store(struct device *dev,
7735 struct device_attribute *attr,
7736 const char *buf, size_t count)
7738 unsigned long t;
7739 int res, level;
7741 if (parse_strtoul(buf, 2, &t))
7742 return -EINVAL;
7744 tpacpi_disclose_usertask("hwmon pwm1_enable",
7745 "set fan mode to %lu\n", t);
7747 switch (t) {
7748 case 0:
7749 level = TP_EC_FAN_FULLSPEED;
7750 break;
7751 case 1:
7752 level = TPACPI_FAN_LAST_LEVEL;
7753 break;
7754 case 2:
7755 level = TP_EC_FAN_AUTO;
7756 break;
7757 case 3:
7758 /* reserved for software-controlled auto mode */
7759 return -ENOSYS;
7760 default:
7761 return -EINVAL;
7764 res = fan_set_level_safe(level);
7765 if (res == -ENXIO)
7766 return -EINVAL;
7767 else if (res < 0)
7768 return res;
7770 fan_watchdog_reset();
7772 return count;
7775 static struct device_attribute dev_attr_fan_pwm1_enable =
7776 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7777 fan_pwm1_enable_show, fan_pwm1_enable_store);
7779 /* sysfs fan pwm1 ------------------------------------------------------ */
7780 static ssize_t fan_pwm1_show(struct device *dev,
7781 struct device_attribute *attr,
7782 char *buf)
7784 int res;
7785 u8 status;
7787 res = fan_get_status_safe(&status);
7788 if (res)
7789 return res;
7791 if ((status &
7792 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7793 status = fan_control_desired_level;
7795 if (status > 7)
7796 status = 7;
7798 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7801 static ssize_t fan_pwm1_store(struct device *dev,
7802 struct device_attribute *attr,
7803 const char *buf, size_t count)
7805 unsigned long s;
7806 int rc;
7807 u8 status, newlevel;
7809 if (parse_strtoul(buf, 255, &s))
7810 return -EINVAL;
7812 tpacpi_disclose_usertask("hwmon pwm1",
7813 "set fan speed to %lu\n", s);
7815 /* scale down from 0-255 to 0-7 */
7816 newlevel = (s >> 5) & 0x07;
7818 if (mutex_lock_killable(&fan_mutex))
7819 return -ERESTARTSYS;
7821 rc = fan_get_status(&status);
7822 if (!rc && (status &
7823 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7824 rc = fan_set_level(newlevel);
7825 if (rc == -ENXIO)
7826 rc = -EINVAL;
7827 else if (!rc) {
7828 fan_update_desired_level(newlevel);
7829 fan_watchdog_reset();
7833 mutex_unlock(&fan_mutex);
7834 return (rc)? rc : count;
7837 static struct device_attribute dev_attr_fan_pwm1 =
7838 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7839 fan_pwm1_show, fan_pwm1_store);
7841 /* sysfs fan fan1_input ------------------------------------------------ */
7842 static ssize_t fan_fan1_input_show(struct device *dev,
7843 struct device_attribute *attr,
7844 char *buf)
7846 int res;
7847 unsigned int speed;
7849 res = fan_get_speed(&speed);
7850 if (res < 0)
7851 return res;
7853 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7856 static struct device_attribute dev_attr_fan_fan1_input =
7857 __ATTR(fan1_input, S_IRUGO,
7858 fan_fan1_input_show, NULL);
7860 /* sysfs fan fan2_input ------------------------------------------------ */
7861 static ssize_t fan_fan2_input_show(struct device *dev,
7862 struct device_attribute *attr,
7863 char *buf)
7865 int res;
7866 unsigned int speed;
7868 res = fan2_get_speed(&speed);
7869 if (res < 0)
7870 return res;
7872 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7875 static struct device_attribute dev_attr_fan_fan2_input =
7876 __ATTR(fan2_input, S_IRUGO,
7877 fan_fan2_input_show, NULL);
7879 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7880 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7881 char *buf)
7883 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7886 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7887 const char *buf, size_t count)
7889 unsigned long t;
7891 if (parse_strtoul(buf, 120, &t))
7892 return -EINVAL;
7894 if (!fan_control_allowed)
7895 return -EPERM;
7897 fan_watchdog_maxinterval = t;
7898 fan_watchdog_reset();
7900 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7902 return count;
7905 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7906 fan_fan_watchdog_show, fan_fan_watchdog_store);
7908 /* --------------------------------------------------------------------- */
7909 static struct attribute *fan_attributes[] = {
7910 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7911 &dev_attr_fan_fan1_input.attr,
7912 NULL, /* for fan2_input */
7913 NULL
7916 static const struct attribute_group fan_attr_group = {
7917 .attrs = fan_attributes,
7920 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7921 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7923 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7924 { .vendor = PCI_VENDOR_ID_IBM, \
7925 .bios = TPACPI_MATCH_ANY, \
7926 .ec = TPID(__id1, __id2), \
7927 .quirks = __quirks }
7929 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7930 { .vendor = PCI_VENDOR_ID_LENOVO, \
7931 .bios = TPACPI_MATCH_ANY, \
7932 .ec = TPID(__id1, __id2), \
7933 .quirks = __quirks }
7935 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7936 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7937 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7938 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7939 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7940 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7943 #undef TPACPI_FAN_QL
7944 #undef TPACPI_FAN_QI
7946 static int __init fan_init(struct ibm_init_struct *iibm)
7948 int rc;
7949 unsigned long quirks;
7951 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7952 "initializing fan subdriver\n");
7954 mutex_init(&fan_mutex);
7955 fan_status_access_mode = TPACPI_FAN_NONE;
7956 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7957 fan_control_commands = 0;
7958 fan_watchdog_maxinterval = 0;
7959 tp_features.fan_ctrl_status_undef = 0;
7960 tp_features.second_fan = 0;
7961 fan_control_desired_level = 7;
7963 if (tpacpi_is_ibm()) {
7964 TPACPI_ACPIHANDLE_INIT(fans);
7965 TPACPI_ACPIHANDLE_INIT(gfan);
7966 TPACPI_ACPIHANDLE_INIT(sfan);
7969 quirks = tpacpi_check_quirks(fan_quirk_table,
7970 ARRAY_SIZE(fan_quirk_table));
7972 if (gfan_handle) {
7973 /* 570, 600e/x, 770e, 770x */
7974 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7975 } else {
7976 /* all other ThinkPads: note that even old-style
7977 * ThinkPad ECs supports the fan control register */
7978 if (likely(acpi_ec_read(fan_status_offset,
7979 &fan_control_initial_status))) {
7980 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7981 if (quirks & TPACPI_FAN_Q1)
7982 fan_quirk1_setup();
7983 if (quirks & TPACPI_FAN_2FAN) {
7984 tp_features.second_fan = 1;
7985 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7986 "secondary fan support enabled\n");
7988 } else {
7989 pr_err("ThinkPad ACPI EC access misbehaving, "
7990 "fan status and control unavailable\n");
7991 return 1;
7995 if (sfan_handle) {
7996 /* 570, 770x-JL */
7997 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7998 fan_control_commands |=
7999 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8000 } else {
8001 if (!gfan_handle) {
8002 /* gfan without sfan means no fan control */
8003 /* all other models implement TP EC 0x2f control */
8005 if (fans_handle) {
8006 /* X31, X40, X41 */
8007 fan_control_access_mode =
8008 TPACPI_FAN_WR_ACPI_FANS;
8009 fan_control_commands |=
8010 TPACPI_FAN_CMD_SPEED |
8011 TPACPI_FAN_CMD_LEVEL |
8012 TPACPI_FAN_CMD_ENABLE;
8013 } else {
8014 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8015 fan_control_commands |=
8016 TPACPI_FAN_CMD_LEVEL |
8017 TPACPI_FAN_CMD_ENABLE;
8022 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8023 "fan is %s, modes %d, %d\n",
8024 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8025 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8026 fan_status_access_mode, fan_control_access_mode);
8028 /* fan control master switch */
8029 if (!fan_control_allowed) {
8030 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8031 fan_control_commands = 0;
8032 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8033 "fan control features disabled by parameter\n");
8036 /* update fan_control_desired_level */
8037 if (fan_status_access_mode != TPACPI_FAN_NONE)
8038 fan_get_status_safe(NULL);
8040 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8041 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8042 if (tp_features.second_fan) {
8043 /* attach second fan tachometer */
8044 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8045 &dev_attr_fan_fan2_input.attr;
8047 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8048 &fan_attr_group);
8049 if (rc < 0)
8050 return rc;
8052 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8053 &driver_attr_fan_watchdog);
8054 if (rc < 0) {
8055 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8056 &fan_attr_group);
8057 return rc;
8059 return 0;
8060 } else
8061 return 1;
8064 static void fan_exit(void)
8066 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8067 "cancelling any pending fan watchdog tasks\n");
8069 /* FIXME: can we really do this unconditionally? */
8070 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8071 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8072 &driver_attr_fan_watchdog);
8074 cancel_delayed_work(&fan_watchdog_task);
8075 flush_workqueue(tpacpi_wq);
8078 static void fan_suspend(pm_message_t state)
8080 int rc;
8082 if (!fan_control_allowed)
8083 return;
8085 /* Store fan status in cache */
8086 fan_control_resume_level = 0;
8087 rc = fan_get_status_safe(&fan_control_resume_level);
8088 if (rc < 0)
8089 pr_notice("failed to read fan level for later "
8090 "restore during resume: %d\n", rc);
8092 /* if it is undefined, don't attempt to restore it.
8093 * KEEP THIS LAST */
8094 if (tp_features.fan_ctrl_status_undef)
8095 fan_control_resume_level = 0;
8098 static void fan_resume(void)
8100 u8 current_level = 7;
8101 bool do_set = false;
8102 int rc;
8104 /* DSDT *always* updates status on resume */
8105 tp_features.fan_ctrl_status_undef = 0;
8107 if (!fan_control_allowed ||
8108 !fan_control_resume_level ||
8109 (fan_get_status_safe(&current_level) < 0))
8110 return;
8112 switch (fan_control_access_mode) {
8113 case TPACPI_FAN_WR_ACPI_SFAN:
8114 /* never decrease fan level */
8115 do_set = (fan_control_resume_level > current_level);
8116 break;
8117 case TPACPI_FAN_WR_ACPI_FANS:
8118 case TPACPI_FAN_WR_TPEC:
8119 /* never decrease fan level, scale is:
8120 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8122 * We expect the firmware to set either 7 or AUTO, but we
8123 * handle FULLSPEED out of paranoia.
8125 * So, we can safely only restore FULLSPEED or 7, anything
8126 * else could slow the fan. Restoring AUTO is useless, at
8127 * best that's exactly what the DSDT already set (it is the
8128 * slower it uses).
8130 * Always keep in mind that the DSDT *will* have set the
8131 * fans to what the vendor supposes is the best level. We
8132 * muck with it only to speed the fan up.
8134 if (fan_control_resume_level != 7 &&
8135 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8136 return;
8137 else
8138 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8139 (current_level != fan_control_resume_level);
8140 break;
8141 default:
8142 return;
8144 if (do_set) {
8145 pr_notice("restoring fan level to 0x%02x\n",
8146 fan_control_resume_level);
8147 rc = fan_set_level_safe(fan_control_resume_level);
8148 if (rc < 0)
8149 pr_notice("failed to restore fan level: %d\n", rc);
8153 static int fan_read(struct seq_file *m)
8155 int rc;
8156 u8 status;
8157 unsigned int speed = 0;
8159 switch (fan_status_access_mode) {
8160 case TPACPI_FAN_RD_ACPI_GFAN:
8161 /* 570, 600e/x, 770e, 770x */
8162 rc = fan_get_status_safe(&status);
8163 if (rc < 0)
8164 return rc;
8166 seq_printf(m, "status:\t\t%s\n"
8167 "level:\t\t%d\n",
8168 (status != 0) ? "enabled" : "disabled", status);
8169 break;
8171 case TPACPI_FAN_RD_TPEC:
8172 /* all except 570, 600e/x, 770e, 770x */
8173 rc = fan_get_status_safe(&status);
8174 if (rc < 0)
8175 return rc;
8177 seq_printf(m, "status:\t\t%s\n",
8178 (status != 0) ? "enabled" : "disabled");
8180 rc = fan_get_speed(&speed);
8181 if (rc < 0)
8182 return rc;
8184 seq_printf(m, "speed:\t\t%d\n", speed);
8186 if (status & TP_EC_FAN_FULLSPEED)
8187 /* Disengaged mode takes precedence */
8188 seq_printf(m, "level:\t\tdisengaged\n");
8189 else if (status & TP_EC_FAN_AUTO)
8190 seq_printf(m, "level:\t\tauto\n");
8191 else
8192 seq_printf(m, "level:\t\t%d\n", status);
8193 break;
8195 case TPACPI_FAN_NONE:
8196 default:
8197 seq_printf(m, "status:\t\tnot supported\n");
8200 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8201 seq_printf(m, "commands:\tlevel <level>");
8203 switch (fan_control_access_mode) {
8204 case TPACPI_FAN_WR_ACPI_SFAN:
8205 seq_printf(m, " (<level> is 0-7)\n");
8206 break;
8208 default:
8209 seq_printf(m, " (<level> is 0-7, "
8210 "auto, disengaged, full-speed)\n");
8211 break;
8215 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8216 seq_printf(m, "commands:\tenable, disable\n"
8217 "commands:\twatchdog <timeout> (<timeout> "
8218 "is 0 (off), 1-120 (seconds))\n");
8220 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8221 seq_printf(m, "commands:\tspeed <speed>"
8222 " (<speed> is 0-65535)\n");
8224 return 0;
8227 static int fan_write_cmd_level(const char *cmd, int *rc)
8229 int level;
8231 if (strlencmp(cmd, "level auto") == 0)
8232 level = TP_EC_FAN_AUTO;
8233 else if ((strlencmp(cmd, "level disengaged") == 0) |
8234 (strlencmp(cmd, "level full-speed") == 0))
8235 level = TP_EC_FAN_FULLSPEED;
8236 else if (sscanf(cmd, "level %d", &level) != 1)
8237 return 0;
8239 *rc = fan_set_level_safe(level);
8240 if (*rc == -ENXIO)
8241 pr_err("level command accepted for unsupported access mode %d\n",
8242 fan_control_access_mode);
8243 else if (!*rc)
8244 tpacpi_disclose_usertask("procfs fan",
8245 "set level to %d\n", level);
8247 return 1;
8250 static int fan_write_cmd_enable(const char *cmd, int *rc)
8252 if (strlencmp(cmd, "enable") != 0)
8253 return 0;
8255 *rc = fan_set_enable();
8256 if (*rc == -ENXIO)
8257 pr_err("enable command accepted for unsupported access mode %d\n",
8258 fan_control_access_mode);
8259 else if (!*rc)
8260 tpacpi_disclose_usertask("procfs fan", "enable\n");
8262 return 1;
8265 static int fan_write_cmd_disable(const char *cmd, int *rc)
8267 if (strlencmp(cmd, "disable") != 0)
8268 return 0;
8270 *rc = fan_set_disable();
8271 if (*rc == -ENXIO)
8272 pr_err("disable command accepted for unsupported access mode %d\n",
8273 fan_control_access_mode);
8274 else if (!*rc)
8275 tpacpi_disclose_usertask("procfs fan", "disable\n");
8277 return 1;
8280 static int fan_write_cmd_speed(const char *cmd, int *rc)
8282 int speed;
8284 /* TODO:
8285 * Support speed <low> <medium> <high> ? */
8287 if (sscanf(cmd, "speed %d", &speed) != 1)
8288 return 0;
8290 *rc = fan_set_speed(speed);
8291 if (*rc == -ENXIO)
8292 pr_err("speed command accepted for unsupported access mode %d\n",
8293 fan_control_access_mode);
8294 else if (!*rc)
8295 tpacpi_disclose_usertask("procfs fan",
8296 "set speed to %d\n", speed);
8298 return 1;
8301 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8303 int interval;
8305 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8306 return 0;
8308 if (interval < 0 || interval > 120)
8309 *rc = -EINVAL;
8310 else {
8311 fan_watchdog_maxinterval = interval;
8312 tpacpi_disclose_usertask("procfs fan",
8313 "set watchdog timer to %d\n",
8314 interval);
8317 return 1;
8320 static int fan_write(char *buf)
8322 char *cmd;
8323 int rc = 0;
8325 while (!rc && (cmd = next_cmd(&buf))) {
8326 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8327 fan_write_cmd_level(cmd, &rc)) &&
8328 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8329 (fan_write_cmd_enable(cmd, &rc) ||
8330 fan_write_cmd_disable(cmd, &rc) ||
8331 fan_write_cmd_watchdog(cmd, &rc))) &&
8332 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8333 fan_write_cmd_speed(cmd, &rc))
8335 rc = -EINVAL;
8336 else if (!rc)
8337 fan_watchdog_reset();
8340 return rc;
8343 static struct ibm_struct fan_driver_data = {
8344 .name = "fan",
8345 .read = fan_read,
8346 .write = fan_write,
8347 .exit = fan_exit,
8348 .suspend = fan_suspend,
8349 .resume = fan_resume,
8352 /****************************************************************************
8353 ****************************************************************************
8355 * Infrastructure
8357 ****************************************************************************
8358 ****************************************************************************/
8361 * HKEY event callout for other subdrivers go here
8362 * (yes, it is ugly, but it is quick, safe, and gets the job done
8364 static void tpacpi_driver_event(const unsigned int hkey_event)
8366 if (ibm_backlight_device) {
8367 switch (hkey_event) {
8368 case TP_HKEY_EV_BRGHT_UP:
8369 case TP_HKEY_EV_BRGHT_DOWN:
8370 tpacpi_brightness_notify_change();
8373 if (alsa_card) {
8374 switch (hkey_event) {
8375 case TP_HKEY_EV_VOL_UP:
8376 case TP_HKEY_EV_VOL_DOWN:
8377 case TP_HKEY_EV_VOL_MUTE:
8378 volume_alsa_notify_change();
8383 static void hotkey_driver_event(const unsigned int scancode)
8385 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8388 /* sysfs name ---------------------------------------------------------- */
8389 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8390 struct device_attribute *attr,
8391 char *buf)
8393 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8396 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8397 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8399 /* --------------------------------------------------------------------- */
8401 /* /proc support */
8402 static struct proc_dir_entry *proc_dir;
8405 * Module and infrastructure proble, init and exit handling
8408 static int force_load;
8410 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8411 static const char * __init str_supported(int is_supported)
8413 static char text_unsupported[] __initdata = "not supported";
8415 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8417 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8419 static void ibm_exit(struct ibm_struct *ibm)
8421 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8423 list_del_init(&ibm->all_drivers);
8425 if (ibm->flags.acpi_notify_installed) {
8426 dbg_printk(TPACPI_DBG_EXIT,
8427 "%s: acpi_remove_notify_handler\n", ibm->name);
8428 BUG_ON(!ibm->acpi);
8429 acpi_remove_notify_handler(*ibm->acpi->handle,
8430 ibm->acpi->type,
8431 dispatch_acpi_notify);
8432 ibm->flags.acpi_notify_installed = 0;
8435 if (ibm->flags.proc_created) {
8436 dbg_printk(TPACPI_DBG_EXIT,
8437 "%s: remove_proc_entry\n", ibm->name);
8438 remove_proc_entry(ibm->name, proc_dir);
8439 ibm->flags.proc_created = 0;
8442 if (ibm->flags.acpi_driver_registered) {
8443 dbg_printk(TPACPI_DBG_EXIT,
8444 "%s: acpi_bus_unregister_driver\n", ibm->name);
8445 BUG_ON(!ibm->acpi);
8446 acpi_bus_unregister_driver(ibm->acpi->driver);
8447 kfree(ibm->acpi->driver);
8448 ibm->acpi->driver = NULL;
8449 ibm->flags.acpi_driver_registered = 0;
8452 if (ibm->flags.init_called && ibm->exit) {
8453 ibm->exit();
8454 ibm->flags.init_called = 0;
8457 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8460 static int __init ibm_init(struct ibm_init_struct *iibm)
8462 int ret;
8463 struct ibm_struct *ibm = iibm->data;
8464 struct proc_dir_entry *entry;
8466 BUG_ON(ibm == NULL);
8468 INIT_LIST_HEAD(&ibm->all_drivers);
8470 if (ibm->flags.experimental && !experimental)
8471 return 0;
8473 dbg_printk(TPACPI_DBG_INIT,
8474 "probing for %s\n", ibm->name);
8476 if (iibm->init) {
8477 ret = iibm->init(iibm);
8478 if (ret > 0)
8479 return 0; /* probe failed */
8480 if (ret)
8481 return ret;
8483 ibm->flags.init_called = 1;
8486 if (ibm->acpi) {
8487 if (ibm->acpi->hid) {
8488 ret = register_tpacpi_subdriver(ibm);
8489 if (ret)
8490 goto err_out;
8493 if (ibm->acpi->notify) {
8494 ret = setup_acpi_notify(ibm);
8495 if (ret == -ENODEV) {
8496 pr_notice("disabling subdriver %s\n",
8497 ibm->name);
8498 ret = 0;
8499 goto err_out;
8501 if (ret < 0)
8502 goto err_out;
8506 dbg_printk(TPACPI_DBG_INIT,
8507 "%s installed\n", ibm->name);
8509 if (ibm->read) {
8510 mode_t mode = iibm->base_procfs_mode;
8512 if (!mode)
8513 mode = S_IRUGO;
8514 if (ibm->write)
8515 mode |= S_IWUSR;
8516 entry = proc_create_data(ibm->name, mode, proc_dir,
8517 &dispatch_proc_fops, ibm);
8518 if (!entry) {
8519 pr_err("unable to create proc entry %s\n", ibm->name);
8520 ret = -ENODEV;
8521 goto err_out;
8523 ibm->flags.proc_created = 1;
8526 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8528 return 0;
8530 err_out:
8531 dbg_printk(TPACPI_DBG_INIT,
8532 "%s: at error exit path with result %d\n",
8533 ibm->name, ret);
8535 ibm_exit(ibm);
8536 return (ret < 0)? ret : 0;
8539 /* Probing */
8541 static bool __pure __init tpacpi_is_fw_digit(const char c)
8543 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8546 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8547 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8548 const char t)
8550 return s && strlen(s) >= 8 &&
8551 tpacpi_is_fw_digit(s[0]) &&
8552 tpacpi_is_fw_digit(s[1]) &&
8553 s[2] == t && s[3] == 'T' &&
8554 tpacpi_is_fw_digit(s[4]) &&
8555 tpacpi_is_fw_digit(s[5]);
8558 /* returns 0 - probe ok, or < 0 - probe error.
8559 * Probe ok doesn't mean thinkpad found.
8560 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8561 static int __must_check __init get_thinkpad_model_data(
8562 struct thinkpad_id_data *tp)
8564 const struct dmi_device *dev = NULL;
8565 char ec_fw_string[18];
8566 char const *s;
8568 if (!tp)
8569 return -EINVAL;
8571 memset(tp, 0, sizeof(*tp));
8573 if (dmi_name_in_vendors("IBM"))
8574 tp->vendor = PCI_VENDOR_ID_IBM;
8575 else if (dmi_name_in_vendors("LENOVO"))
8576 tp->vendor = PCI_VENDOR_ID_LENOVO;
8577 else
8578 return 0;
8580 s = dmi_get_system_info(DMI_BIOS_VERSION);
8581 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8582 if (s && !tp->bios_version_str)
8583 return -ENOMEM;
8585 /* Really ancient ThinkPad 240X will fail this, which is fine */
8586 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8587 return 0;
8589 tp->bios_model = tp->bios_version_str[0]
8590 | (tp->bios_version_str[1] << 8);
8591 tp->bios_release = (tp->bios_version_str[4] << 8)
8592 | tp->bios_version_str[5];
8595 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8596 * X32 or newer, all Z series; Some models must have an
8597 * up-to-date BIOS or they will not be detected.
8599 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8601 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8602 if (sscanf(dev->name,
8603 "IBM ThinkPad Embedded Controller -[%17c",
8604 ec_fw_string) == 1) {
8605 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8606 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8608 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8609 if (!tp->ec_version_str)
8610 return -ENOMEM;
8612 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8613 tp->ec_model = ec_fw_string[0]
8614 | (ec_fw_string[1] << 8);
8615 tp->ec_release = (ec_fw_string[4] << 8)
8616 | ec_fw_string[5];
8617 } else {
8618 pr_notice("ThinkPad firmware release %s "
8619 "doesn't match the known patterns\n",
8620 ec_fw_string);
8621 pr_notice("please report this to %s\n",
8622 TPACPI_MAIL);
8624 break;
8628 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8629 if (s && !strnicmp(s, "ThinkPad", 8)) {
8630 tp->model_str = kstrdup(s, GFP_KERNEL);
8631 if (!tp->model_str)
8632 return -ENOMEM;
8635 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8636 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8637 if (s && !tp->nummodel_str)
8638 return -ENOMEM;
8640 return 0;
8643 static int __init probe_for_thinkpad(void)
8645 int is_thinkpad;
8647 if (acpi_disabled)
8648 return -ENODEV;
8650 /* It would be dangerous to run the driver in this case */
8651 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8652 return -ENODEV;
8655 * Non-ancient models have better DMI tagging, but very old models
8656 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8658 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8659 (thinkpad_id.ec_model != 0) ||
8660 tpacpi_is_fw_known();
8662 /* The EC handler is required */
8663 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8664 if (!ec_handle) {
8665 if (is_thinkpad)
8666 pr_err("Not yet supported ThinkPad detected!\n");
8667 return -ENODEV;
8670 if (!is_thinkpad && !force_load)
8671 return -ENODEV;
8673 return 0;
8676 static void __init thinkpad_acpi_init_banner(void)
8678 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8679 pr_info("%s\n", TPACPI_URL);
8681 pr_info("ThinkPad BIOS %s, EC %s\n",
8682 (thinkpad_id.bios_version_str) ?
8683 thinkpad_id.bios_version_str : "unknown",
8684 (thinkpad_id.ec_version_str) ?
8685 thinkpad_id.ec_version_str : "unknown");
8687 BUG_ON(!thinkpad_id.vendor);
8689 if (thinkpad_id.model_str)
8690 pr_info("%s %s, model %s\n",
8691 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8692 "IBM" : ((thinkpad_id.vendor ==
8693 PCI_VENDOR_ID_LENOVO) ?
8694 "Lenovo" : "Unknown vendor"),
8695 thinkpad_id.model_str,
8696 (thinkpad_id.nummodel_str) ?
8697 thinkpad_id.nummodel_str : "unknown");
8700 /* Module init, exit, parameters */
8702 static struct ibm_init_struct ibms_init[] __initdata = {
8704 .data = &thinkpad_acpi_driver_data,
8707 .init = hotkey_init,
8708 .data = &hotkey_driver_data,
8711 .init = bluetooth_init,
8712 .data = &bluetooth_driver_data,
8715 .init = wan_init,
8716 .data = &wan_driver_data,
8719 .init = uwb_init,
8720 .data = &uwb_driver_data,
8722 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8724 .init = video_init,
8725 .base_procfs_mode = S_IRUSR,
8726 .data = &video_driver_data,
8728 #endif
8730 .init = light_init,
8731 .data = &light_driver_data,
8734 .init = cmos_init,
8735 .data = &cmos_driver_data,
8738 .init = led_init,
8739 .data = &led_driver_data,
8742 .init = beep_init,
8743 .data = &beep_driver_data,
8746 .init = thermal_init,
8747 .data = &thermal_driver_data,
8750 .init = brightness_init,
8751 .data = &brightness_driver_data,
8754 .init = volume_init,
8755 .data = &volume_driver_data,
8758 .init = fan_init,
8759 .data = &fan_driver_data,
8763 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8765 unsigned int i;
8766 struct ibm_struct *ibm;
8768 if (!kp || !kp->name || !val)
8769 return -EINVAL;
8771 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8772 ibm = ibms_init[i].data;
8773 WARN_ON(ibm == NULL);
8775 if (!ibm || !ibm->name)
8776 continue;
8778 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8779 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8780 return -ENOSPC;
8781 strcpy(ibms_init[i].param, val);
8782 strcat(ibms_init[i].param, ",");
8783 return 0;
8787 return -EINVAL;
8790 module_param(experimental, int, 0444);
8791 MODULE_PARM_DESC(experimental,
8792 "Enables experimental features when non-zero");
8794 module_param_named(debug, dbg_level, uint, 0);
8795 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8797 module_param(force_load, bool, 0444);
8798 MODULE_PARM_DESC(force_load,
8799 "Attempts to load the driver even on a "
8800 "mis-identified ThinkPad when true");
8802 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8803 MODULE_PARM_DESC(fan_control,
8804 "Enables setting fan parameters features when true");
8806 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8807 MODULE_PARM_DESC(brightness_mode,
8808 "Selects brightness control strategy: "
8809 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8811 module_param(brightness_enable, uint, 0444);
8812 MODULE_PARM_DESC(brightness_enable,
8813 "Enables backlight control when 1, disables when 0");
8815 module_param(hotkey_report_mode, uint, 0444);
8816 MODULE_PARM_DESC(hotkey_report_mode,
8817 "used for backwards compatibility with userspace, "
8818 "see documentation");
8820 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8821 module_param_named(volume_mode, volume_mode, uint, 0444);
8822 MODULE_PARM_DESC(volume_mode,
8823 "Selects volume control strategy: "
8824 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8826 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8827 MODULE_PARM_DESC(volume_capabilities,
8828 "Selects the mixer capabilites: "
8829 "0=auto, 1=volume and mute, 2=mute only");
8831 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8832 MODULE_PARM_DESC(volume_control,
8833 "Enables software override for the console audio "
8834 "control when true");
8836 /* ALSA module API parameters */
8837 module_param_named(index, alsa_index, int, 0444);
8838 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8839 module_param_named(id, alsa_id, charp, 0444);
8840 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8841 module_param_named(enable, alsa_enable, bool, 0444);
8842 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8843 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8845 #define TPACPI_PARAM(feature) \
8846 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8847 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8848 "at module load, see documentation")
8850 TPACPI_PARAM(hotkey);
8851 TPACPI_PARAM(bluetooth);
8852 TPACPI_PARAM(video);
8853 TPACPI_PARAM(light);
8854 TPACPI_PARAM(cmos);
8855 TPACPI_PARAM(led);
8856 TPACPI_PARAM(beep);
8857 TPACPI_PARAM(brightness);
8858 TPACPI_PARAM(volume);
8859 TPACPI_PARAM(fan);
8861 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8862 module_param(dbg_wlswemul, uint, 0444);
8863 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8864 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8865 MODULE_PARM_DESC(wlsw_state,
8866 "Initial state of the emulated WLSW switch");
8868 module_param(dbg_bluetoothemul, uint, 0444);
8869 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8870 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8871 MODULE_PARM_DESC(bluetooth_state,
8872 "Initial state of the emulated bluetooth switch");
8874 module_param(dbg_wwanemul, uint, 0444);
8875 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8876 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8877 MODULE_PARM_DESC(wwan_state,
8878 "Initial state of the emulated WWAN switch");
8880 module_param(dbg_uwbemul, uint, 0444);
8881 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8882 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8883 MODULE_PARM_DESC(uwb_state,
8884 "Initial state of the emulated UWB switch");
8885 #endif
8887 static void thinkpad_acpi_module_exit(void)
8889 struct ibm_struct *ibm, *itmp;
8891 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8893 list_for_each_entry_safe_reverse(ibm, itmp,
8894 &tpacpi_all_drivers,
8895 all_drivers) {
8896 ibm_exit(ibm);
8899 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8901 if (tpacpi_inputdev) {
8902 if (tp_features.input_device_registered)
8903 input_unregister_device(tpacpi_inputdev);
8904 else
8905 input_free_device(tpacpi_inputdev);
8908 if (tpacpi_hwmon)
8909 hwmon_device_unregister(tpacpi_hwmon);
8911 if (tp_features.sensors_pdev_attrs_registered)
8912 device_remove_file(&tpacpi_sensors_pdev->dev,
8913 &dev_attr_thinkpad_acpi_pdev_name);
8914 if (tpacpi_sensors_pdev)
8915 platform_device_unregister(tpacpi_sensors_pdev);
8916 if (tpacpi_pdev)
8917 platform_device_unregister(tpacpi_pdev);
8919 if (tp_features.sensors_pdrv_attrs_registered)
8920 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8921 if (tp_features.platform_drv_attrs_registered)
8922 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8924 if (tp_features.sensors_pdrv_registered)
8925 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8927 if (tp_features.platform_drv_registered)
8928 platform_driver_unregister(&tpacpi_pdriver);
8930 if (proc_dir)
8931 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8933 if (tpacpi_wq)
8934 destroy_workqueue(tpacpi_wq);
8936 kfree(thinkpad_id.bios_version_str);
8937 kfree(thinkpad_id.ec_version_str);
8938 kfree(thinkpad_id.model_str);
8942 static int __init thinkpad_acpi_module_init(void)
8944 int ret, i;
8946 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8948 /* Parameter checking */
8949 if (hotkey_report_mode > 2)
8950 return -EINVAL;
8952 /* Driver-level probe */
8954 ret = get_thinkpad_model_data(&thinkpad_id);
8955 if (ret) {
8956 pr_err("unable to get DMI data: %d\n", ret);
8957 thinkpad_acpi_module_exit();
8958 return ret;
8960 ret = probe_for_thinkpad();
8961 if (ret) {
8962 thinkpad_acpi_module_exit();
8963 return ret;
8966 /* Driver initialization */
8968 thinkpad_acpi_init_banner();
8969 tpacpi_check_outdated_fw();
8971 TPACPI_ACPIHANDLE_INIT(ecrd);
8972 TPACPI_ACPIHANDLE_INIT(ecwr);
8974 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8975 if (!tpacpi_wq) {
8976 thinkpad_acpi_module_exit();
8977 return -ENOMEM;
8980 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8981 if (!proc_dir) {
8982 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
8983 thinkpad_acpi_module_exit();
8984 return -ENODEV;
8987 ret = platform_driver_register(&tpacpi_pdriver);
8988 if (ret) {
8989 pr_err("unable to register main platform driver\n");
8990 thinkpad_acpi_module_exit();
8991 return ret;
8993 tp_features.platform_drv_registered = 1;
8995 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8996 if (ret) {
8997 pr_err("unable to register hwmon platform driver\n");
8998 thinkpad_acpi_module_exit();
8999 return ret;
9001 tp_features.sensors_pdrv_registered = 1;
9003 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9004 if (!ret) {
9005 tp_features.platform_drv_attrs_registered = 1;
9006 ret = tpacpi_create_driver_attributes(
9007 &tpacpi_hwmon_pdriver.driver);
9009 if (ret) {
9010 pr_err("unable to create sysfs driver attributes\n");
9011 thinkpad_acpi_module_exit();
9012 return ret;
9014 tp_features.sensors_pdrv_attrs_registered = 1;
9017 /* Device initialization */
9018 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9019 NULL, 0);
9020 if (IS_ERR(tpacpi_pdev)) {
9021 ret = PTR_ERR(tpacpi_pdev);
9022 tpacpi_pdev = NULL;
9023 pr_err("unable to register platform device\n");
9024 thinkpad_acpi_module_exit();
9025 return ret;
9027 tpacpi_sensors_pdev = platform_device_register_simple(
9028 TPACPI_HWMON_DRVR_NAME,
9029 -1, NULL, 0);
9030 if (IS_ERR(tpacpi_sensors_pdev)) {
9031 ret = PTR_ERR(tpacpi_sensors_pdev);
9032 tpacpi_sensors_pdev = NULL;
9033 pr_err("unable to register hwmon platform device\n");
9034 thinkpad_acpi_module_exit();
9035 return ret;
9037 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9038 &dev_attr_thinkpad_acpi_pdev_name);
9039 if (ret) {
9040 pr_err("unable to create sysfs hwmon device attributes\n");
9041 thinkpad_acpi_module_exit();
9042 return ret;
9044 tp_features.sensors_pdev_attrs_registered = 1;
9045 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9046 if (IS_ERR(tpacpi_hwmon)) {
9047 ret = PTR_ERR(tpacpi_hwmon);
9048 tpacpi_hwmon = NULL;
9049 pr_err("unable to register hwmon device\n");
9050 thinkpad_acpi_module_exit();
9051 return ret;
9053 mutex_init(&tpacpi_inputdev_send_mutex);
9054 tpacpi_inputdev = input_allocate_device();
9055 if (!tpacpi_inputdev) {
9056 pr_err("unable to allocate input device\n");
9057 thinkpad_acpi_module_exit();
9058 return -ENOMEM;
9059 } else {
9060 /* Prepare input device, but don't register */
9061 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9062 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9063 tpacpi_inputdev->id.bustype = BUS_HOST;
9064 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9065 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9066 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9067 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9070 /* Init subdriver dependencies */
9071 tpacpi_detect_brightness_capabilities();
9073 /* Init subdrivers */
9074 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9075 ret = ibm_init(&ibms_init[i]);
9076 if (ret >= 0 && *ibms_init[i].param)
9077 ret = ibms_init[i].data->write(ibms_init[i].param);
9078 if (ret < 0) {
9079 thinkpad_acpi_module_exit();
9080 return ret;
9084 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9086 ret = input_register_device(tpacpi_inputdev);
9087 if (ret < 0) {
9088 pr_err("unable to register input device\n");
9089 thinkpad_acpi_module_exit();
9090 return ret;
9091 } else {
9092 tp_features.input_device_registered = 1;
9095 return 0;
9098 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9101 * This will autoload the driver in almost every ThinkPad
9102 * in widespread use.
9104 * Only _VERY_ old models, like the 240, 240x and 570 lack
9105 * the HKEY event interface.
9107 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9110 * DMI matching for module autoloading
9112 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9113 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9115 * Only models listed in thinkwiki will be supported, so add yours
9116 * if it is not there yet.
9118 #define IBM_BIOS_MODULE_ALIAS(__type) \
9119 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9121 /* Ancient thinkpad BIOSes have to be identified by
9122 * BIOS type or model number, and there are far less
9123 * BIOS types than model numbers... */
9124 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9126 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9127 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9128 MODULE_DESCRIPTION(TPACPI_DESC);
9129 MODULE_VERSION(TPACPI_VERSION);
9130 MODULE_LICENSE("GPL");
9132 module_init(thinkpad_acpi_module_init);
9133 module_exit(thinkpad_acpi_module_exit);