thinkpad-acpi: handle HKEY 0x4010, 0x4011 events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobe10dd34747bc52b6133f9beb8a7dff429a20b874
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 #ifndef no_printk
28 * Dummy printk for disabled debugging statements to use whilst maintaining
29 * gcc's format and side-effect checking.
31 static inline __attribute__ ((format (printf, 1, 2)))
32 int no_printk(const char *fmt, ...)
34 return 0;
36 #endif
38 #define TPACPI_VERSION "0.24"
39 #define TPACPI_SYSFS_VERSION 0x020700
42 * Changelog:
43 * 2007-10-20 changelog trimmed down
45 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
46 * drivers/misc.
48 * 2006-11-22 0.13 new maintainer
49 * changelog now lives in git commit history, and will
50 * not be updated further in-file.
52 * 2005-03-17 0.11 support for 600e, 770x
53 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
55 * 2005-01-16 0.9 use MODULE_VERSION
56 * thanks to Henrik Brix Andersen <brix@gentoo.org>
57 * fix parameter passing on module loading
58 * thanks to Rusty Russell <rusty@rustcorp.com.au>
59 * thanks to Jim Radford <radford@blackbean.org>
60 * 2004-11-08 0.8 fix init error case, don't return from a macro
61 * thanks to Chris Wright <chrisw@osdl.org>
64 #include <linux/kernel.h>
65 #include <linux/module.h>
66 #include <linux/init.h>
67 #include <linux/types.h>
68 #include <linux/string.h>
69 #include <linux/list.h>
70 #include <linux/mutex.h>
71 #include <linux/sched.h>
72 #include <linux/kthread.h>
73 #include <linux/freezer.h>
74 #include <linux/delay.h>
75 #include <linux/slab.h>
77 #include <linux/nvram.h>
78 #include <linux/proc_fs.h>
79 #include <linux/seq_file.h>
80 #include <linux/sysfs.h>
81 #include <linux/backlight.h>
82 #include <linux/fb.h>
83 #include <linux/platform_device.h>
84 #include <linux/hwmon.h>
85 #include <linux/hwmon-sysfs.h>
86 #include <linux/input.h>
87 #include <linux/leds.h>
88 #include <linux/rfkill.h>
89 #include <asm/uaccess.h>
91 #include <linux/dmi.h>
92 #include <linux/jiffies.h>
93 #include <linux/workqueue.h>
95 #include <sound/core.h>
96 #include <sound/control.h>
97 #include <sound/initval.h>
99 #include <acpi/acpi_drivers.h>
101 #include <linux/pci_ids.h>
104 /* ThinkPad CMOS commands */
105 #define TP_CMOS_VOLUME_DOWN 0
106 #define TP_CMOS_VOLUME_UP 1
107 #define TP_CMOS_VOLUME_MUTE 2
108 #define TP_CMOS_BRIGHTNESS_UP 4
109 #define TP_CMOS_BRIGHTNESS_DOWN 5
110 #define TP_CMOS_THINKLIGHT_ON 12
111 #define TP_CMOS_THINKLIGHT_OFF 13
113 /* NVRAM Addresses */
114 enum tp_nvram_addr {
115 TP_NVRAM_ADDR_HK2 = 0x57,
116 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
117 TP_NVRAM_ADDR_VIDEO = 0x59,
118 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
119 TP_NVRAM_ADDR_MIXER = 0x60,
122 /* NVRAM bit masks */
123 enum {
124 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
125 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
126 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
127 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
128 TP_NVRAM_MASK_THINKLIGHT = 0x10,
129 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
130 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
131 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
132 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
133 TP_NVRAM_MASK_MUTE = 0x40,
134 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
135 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
136 TP_NVRAM_POS_LEVEL_VOLUME = 0,
139 /* Misc NVRAM-related */
140 enum {
141 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
144 /* ACPI HIDs */
145 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
146 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
147 #define TPACPI_ACPI_EC_HID "PNP0C09"
149 /* Input IDs */
150 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
151 #define TPACPI_HKEY_INPUT_VERSION 0x4101
153 /* ACPI \WGSV commands */
154 enum {
155 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
156 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
157 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
158 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
161 /* TP_ACPI_WGSV_GET_STATE bits */
162 enum {
163 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
164 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
165 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
166 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
167 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
168 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
169 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
170 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
171 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
172 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
175 /* HKEY events */
176 enum tpacpi_hkey_event_t {
177 /* Hotkey-related */
178 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
179 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
180 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
181 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
182 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
183 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
185 /* Reasons for waking up from S3/S4 */
186 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
187 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
188 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
189 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
190 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
191 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
193 /* Auto-sleep after eject request */
194 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
195 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
197 /* Misc bay events */
198 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
199 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
200 or port replicator */
201 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
202 dock or port replicator */
204 /* User-interface events */
205 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
206 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
207 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
208 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
209 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
210 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
211 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
213 /* Key-related user-interface events */
214 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
215 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
217 /* Thermal events */
218 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
219 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
220 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
221 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
222 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
224 TP_HKEY_EV_UNK_6040 = 0x6040, /* Related to AC change?
225 some sort of APM hint,
226 W520 */
228 /* Misc */
229 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
232 /****************************************************************************
233 * Main driver
236 #define TPACPI_NAME "thinkpad"
237 #define TPACPI_DESC "ThinkPad ACPI Extras"
238 #define TPACPI_FILE TPACPI_NAME "_acpi"
239 #define TPACPI_URL "http://ibm-acpi.sf.net/"
240 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
242 #define TPACPI_PROC_DIR "ibm"
243 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
244 #define TPACPI_DRVR_NAME TPACPI_FILE
245 #define TPACPI_DRVR_SHORTNAME "tpacpi"
246 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
248 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
249 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
251 #define TPACPI_MAX_ACPI_ARGS 3
253 /* Debugging printk groups */
254 #define TPACPI_DBG_ALL 0xffff
255 #define TPACPI_DBG_DISCLOSETASK 0x8000
256 #define TPACPI_DBG_INIT 0x0001
257 #define TPACPI_DBG_EXIT 0x0002
258 #define TPACPI_DBG_RFKILL 0x0004
259 #define TPACPI_DBG_HKEY 0x0008
260 #define TPACPI_DBG_FAN 0x0010
261 #define TPACPI_DBG_BRGHT 0x0020
262 #define TPACPI_DBG_MIXER 0x0040
264 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
265 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
266 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
269 /****************************************************************************
270 * Driver-wide structs and misc. variables
273 struct ibm_struct;
275 struct tp_acpi_drv_struct {
276 const struct acpi_device_id *hid;
277 struct acpi_driver *driver;
279 void (*notify) (struct ibm_struct *, u32);
280 acpi_handle *handle;
281 u32 type;
282 struct acpi_device *device;
285 struct ibm_struct {
286 char *name;
288 int (*read) (struct seq_file *);
289 int (*write) (char *);
290 void (*exit) (void);
291 void (*resume) (void);
292 void (*suspend) (pm_message_t state);
293 void (*shutdown) (void);
295 struct list_head all_drivers;
297 struct tp_acpi_drv_struct *acpi;
299 struct {
300 u8 acpi_driver_registered:1;
301 u8 acpi_notify_installed:1;
302 u8 proc_created:1;
303 u8 init_called:1;
304 u8 experimental:1;
305 } flags;
308 struct ibm_init_struct {
309 char param[32];
311 int (*init) (struct ibm_init_struct *);
312 mode_t base_procfs_mode;
313 struct ibm_struct *data;
316 static struct {
317 u32 bluetooth:1;
318 u32 hotkey:1;
319 u32 hotkey_mask:1;
320 u32 hotkey_wlsw:1;
321 u32 hotkey_tablet:1;
322 u32 light:1;
323 u32 light_status:1;
324 u32 bright_acpimode:1;
325 u32 bright_unkfw:1;
326 u32 wan:1;
327 u32 uwb:1;
328 u32 fan_ctrl_status_undef:1;
329 u32 second_fan:1;
330 u32 beep_needs_two_args:1;
331 u32 mixer_no_level_control:1;
332 u32 input_device_registered:1;
333 u32 platform_drv_registered:1;
334 u32 platform_drv_attrs_registered:1;
335 u32 sensors_pdrv_registered:1;
336 u32 sensors_pdrv_attrs_registered:1;
337 u32 sensors_pdev_attrs_registered:1;
338 u32 hotkey_poll_active:1;
339 } tp_features;
341 static struct {
342 u16 hotkey_mask_ff:1;
343 u16 volume_ctrl_forbidden:1;
344 } tp_warned;
346 struct thinkpad_id_data {
347 unsigned int vendor; /* ThinkPad vendor:
348 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
350 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
351 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
353 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
354 u16 ec_model;
355 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
356 u16 ec_release;
358 char *model_str; /* ThinkPad T43 */
359 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
361 static struct thinkpad_id_data thinkpad_id;
363 static enum {
364 TPACPI_LIFE_INIT = 0,
365 TPACPI_LIFE_RUNNING,
366 TPACPI_LIFE_EXITING,
367 } tpacpi_lifecycle;
369 static int experimental;
370 static u32 dbg_level;
372 static struct workqueue_struct *tpacpi_wq;
374 enum led_status_t {
375 TPACPI_LED_OFF = 0,
376 TPACPI_LED_ON,
377 TPACPI_LED_BLINK,
380 /* Special LED class that can defer work */
381 struct tpacpi_led_classdev {
382 struct led_classdev led_classdev;
383 struct work_struct work;
384 enum led_status_t new_state;
385 unsigned int led;
388 /* brightness level capabilities */
389 static unsigned int bright_maxlvl; /* 0 = unknown */
391 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
392 static int dbg_wlswemul;
393 static int tpacpi_wlsw_emulstate;
394 static int dbg_bluetoothemul;
395 static int tpacpi_bluetooth_emulstate;
396 static int dbg_wwanemul;
397 static int tpacpi_wwan_emulstate;
398 static int dbg_uwbemul;
399 static int tpacpi_uwb_emulstate;
400 #endif
403 /*************************************************************************
404 * Debugging helpers
407 #define dbg_printk(a_dbg_level, format, arg...) \
408 do { \
409 if (dbg_level & (a_dbg_level)) \
410 printk(KERN_DEBUG pr_fmt("%s: " format), \
411 __func__, ##arg); \
412 } while (0)
414 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
415 #define vdbg_printk dbg_printk
416 static const char *str_supported(int is_supported);
417 #else
418 static inline const char *str_supported(int is_supported) { return ""; }
419 #define vdbg_printk(a_dbg_level, format, arg...) \
420 no_printk(format, ##arg)
421 #endif
423 static void tpacpi_log_usertask(const char * const what)
425 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
426 what, task_tgid_vnr(current));
429 #define tpacpi_disclose_usertask(what, format, arg...) \
430 do { \
431 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
432 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
433 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
434 what, task_tgid_vnr(current), ## arg); \
436 } while (0)
439 * Quirk handling helpers
441 * ThinkPad IDs and versions seen in the field so far
442 * are two-characters from the set [0-9A-Z], i.e. base 36.
444 * We use values well outside that range as specials.
447 #define TPACPI_MATCH_ANY 0xffffU
448 #define TPACPI_MATCH_UNKNOWN 0U
450 /* TPID('1', 'Y') == 0x5931 */
451 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
453 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_IBM, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
459 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPID(__id1, __id2), \
462 .ec = TPACPI_MATCH_ANY, \
463 .quirks = (__quirk) }
465 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
466 { .vendor = PCI_VENDOR_ID_LENOVO, \
467 .bios = TPACPI_MATCH_ANY, \
468 .ec = TPID(__id1, __id2), \
469 .quirks = (__quirk) }
471 struct tpacpi_quirk {
472 unsigned int vendor;
473 u16 bios;
474 u16 ec;
475 unsigned long quirks;
479 * tpacpi_check_quirks() - search BIOS/EC version on a list
480 * @qlist: array of &struct tpacpi_quirk
481 * @qlist_size: number of elements in @qlist
483 * Iterates over a quirks list until one is found that matches the
484 * ThinkPad's vendor, BIOS and EC model.
486 * Returns 0 if nothing matches, otherwise returns the quirks field of
487 * the matching &struct tpacpi_quirk entry.
489 * The match criteria is: vendor, ec and bios much match.
491 static unsigned long __init tpacpi_check_quirks(
492 const struct tpacpi_quirk *qlist,
493 unsigned int qlist_size)
495 while (qlist_size) {
496 if ((qlist->vendor == thinkpad_id.vendor ||
497 qlist->vendor == TPACPI_MATCH_ANY) &&
498 (qlist->bios == thinkpad_id.bios_model ||
499 qlist->bios == TPACPI_MATCH_ANY) &&
500 (qlist->ec == thinkpad_id.ec_model ||
501 qlist->ec == TPACPI_MATCH_ANY))
502 return qlist->quirks;
504 qlist_size--;
505 qlist++;
507 return 0;
510 static inline bool __pure __init tpacpi_is_lenovo(void)
512 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
515 static inline bool __pure __init tpacpi_is_ibm(void)
517 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
520 /****************************************************************************
521 ****************************************************************************
523 * ACPI Helpers and device model
525 ****************************************************************************
526 ****************************************************************************/
528 /*************************************************************************
529 * ACPI basic handles
532 static acpi_handle root_handle;
533 static acpi_handle ec_handle;
535 #define TPACPI_HANDLE(object, parent, paths...) \
536 static acpi_handle object##_handle; \
537 static const acpi_handle *object##_parent __initdata = \
538 &parent##_handle; \
539 static char *object##_paths[] __initdata = { paths }
541 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
542 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
544 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
545 /* T4x, X31, X40 */
546 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
547 "\\CMS", /* R40, R40e */
548 ); /* all others */
550 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
551 "^HKEY", /* R30, R31 */
552 "HKEY", /* all others */
553 ); /* 570 */
555 /*************************************************************************
556 * ACPI helpers
559 static int acpi_evalf(acpi_handle handle,
560 void *res, char *method, char *fmt, ...)
562 char *fmt0 = fmt;
563 struct acpi_object_list params;
564 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
565 struct acpi_buffer result, *resultp;
566 union acpi_object out_obj;
567 acpi_status status;
568 va_list ap;
569 char res_type;
570 int success;
571 int quiet;
573 if (!*fmt) {
574 pr_err("acpi_evalf() called with empty format\n");
575 return 0;
578 if (*fmt == 'q') {
579 quiet = 1;
580 fmt++;
581 } else
582 quiet = 0;
584 res_type = *(fmt++);
586 params.count = 0;
587 params.pointer = &in_objs[0];
589 va_start(ap, fmt);
590 while (*fmt) {
591 char c = *(fmt++);
592 switch (c) {
593 case 'd': /* int */
594 in_objs[params.count].integer.value = va_arg(ap, int);
595 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
596 break;
597 /* add more types as needed */
598 default:
599 pr_err("acpi_evalf() called "
600 "with invalid format character '%c'\n", c);
601 va_end(ap);
602 return 0;
605 va_end(ap);
607 if (res_type != 'v') {
608 result.length = sizeof(out_obj);
609 result.pointer = &out_obj;
610 resultp = &result;
611 } else
612 resultp = NULL;
614 status = acpi_evaluate_object(handle, method, &params, resultp);
616 switch (res_type) {
617 case 'd': /* int */
618 success = (status == AE_OK &&
619 out_obj.type == ACPI_TYPE_INTEGER);
620 if (success && res)
621 *(int *)res = out_obj.integer.value;
622 break;
623 case 'v': /* void */
624 success = status == AE_OK;
625 break;
626 /* add more types as needed */
627 default:
628 pr_err("acpi_evalf() called "
629 "with invalid format character '%c'\n", res_type);
630 return 0;
633 if (!success && !quiet)
634 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
635 method, fmt0, acpi_format_exception(status));
637 return success;
640 static int acpi_ec_read(int i, u8 *p)
642 int v;
644 if (ecrd_handle) {
645 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
646 return 0;
647 *p = v;
648 } else {
649 if (ec_read(i, p) < 0)
650 return 0;
653 return 1;
656 static int acpi_ec_write(int i, u8 v)
658 if (ecwr_handle) {
659 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
660 return 0;
661 } else {
662 if (ec_write(i, v) < 0)
663 return 0;
666 return 1;
669 static int issue_thinkpad_cmos_command(int cmos_cmd)
671 if (!cmos_handle)
672 return -ENXIO;
674 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
675 return -EIO;
677 return 0;
680 /*************************************************************************
681 * ACPI device model
684 #define TPACPI_ACPIHANDLE_INIT(object) \
685 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
686 object##_paths, ARRAY_SIZE(object##_paths))
688 static void __init drv_acpi_handle_init(const char *name,
689 acpi_handle *handle, const acpi_handle parent,
690 char **paths, const int num_paths)
692 int i;
693 acpi_status status;
695 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
696 name);
698 for (i = 0; i < num_paths; i++) {
699 status = acpi_get_handle(parent, paths[i], handle);
700 if (ACPI_SUCCESS(status)) {
701 dbg_printk(TPACPI_DBG_INIT,
702 "Found ACPI handle %s for %s\n",
703 paths[i], name);
704 return;
708 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
709 name);
710 *handle = NULL;
713 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
714 u32 level, void *context, void **return_value)
716 *(acpi_handle *)return_value = handle;
718 return AE_CTRL_TERMINATE;
721 static void __init tpacpi_acpi_handle_locate(const char *name,
722 const char *hid,
723 acpi_handle *handle)
725 acpi_status status;
726 acpi_handle device_found;
728 BUG_ON(!name || !hid || !handle);
729 vdbg_printk(TPACPI_DBG_INIT,
730 "trying to locate ACPI handle for %s, using HID %s\n",
731 name, hid);
733 memset(&device_found, 0, sizeof(device_found));
734 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
735 (void *)name, &device_found);
737 *handle = NULL;
739 if (ACPI_SUCCESS(status)) {
740 *handle = device_found;
741 dbg_printk(TPACPI_DBG_INIT,
742 "Found ACPI handle for %s\n", name);
743 } else {
744 vdbg_printk(TPACPI_DBG_INIT,
745 "Could not locate an ACPI handle for %s: %s\n",
746 name, acpi_format_exception(status));
750 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
752 struct ibm_struct *ibm = data;
754 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
755 return;
757 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
758 return;
760 ibm->acpi->notify(ibm, event);
763 static int __init setup_acpi_notify(struct ibm_struct *ibm)
765 acpi_status status;
766 int rc;
768 BUG_ON(!ibm->acpi);
770 if (!*ibm->acpi->handle)
771 return 0;
773 vdbg_printk(TPACPI_DBG_INIT,
774 "setting up ACPI notify for %s\n", ibm->name);
776 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
777 if (rc < 0) {
778 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
779 return -ENODEV;
782 ibm->acpi->device->driver_data = ibm;
783 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
784 TPACPI_ACPI_EVENT_PREFIX,
785 ibm->name);
787 status = acpi_install_notify_handler(*ibm->acpi->handle,
788 ibm->acpi->type, dispatch_acpi_notify, ibm);
789 if (ACPI_FAILURE(status)) {
790 if (status == AE_ALREADY_EXISTS) {
791 pr_notice("another device driver is already "
792 "handling %s events\n", ibm->name);
793 } else {
794 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
795 ibm->name, acpi_format_exception(status));
797 return -ENODEV;
799 ibm->flags.acpi_notify_installed = 1;
800 return 0;
803 static int __init tpacpi_device_add(struct acpi_device *device)
805 return 0;
808 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
810 int rc;
812 dbg_printk(TPACPI_DBG_INIT,
813 "registering %s as an ACPI driver\n", ibm->name);
815 BUG_ON(!ibm->acpi);
817 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
818 if (!ibm->acpi->driver) {
819 pr_err("failed to allocate memory for ibm->acpi->driver\n");
820 return -ENOMEM;
823 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
824 ibm->acpi->driver->ids = ibm->acpi->hid;
826 ibm->acpi->driver->ops.add = &tpacpi_device_add;
828 rc = acpi_bus_register_driver(ibm->acpi->driver);
829 if (rc < 0) {
830 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
831 ibm->name, rc);
832 kfree(ibm->acpi->driver);
833 ibm->acpi->driver = NULL;
834 } else if (!rc)
835 ibm->flags.acpi_driver_registered = 1;
837 return rc;
841 /****************************************************************************
842 ****************************************************************************
844 * Procfs Helpers
846 ****************************************************************************
847 ****************************************************************************/
849 static int dispatch_proc_show(struct seq_file *m, void *v)
851 struct ibm_struct *ibm = m->private;
853 if (!ibm || !ibm->read)
854 return -EINVAL;
855 return ibm->read(m);
858 static int dispatch_proc_open(struct inode *inode, struct file *file)
860 return single_open(file, dispatch_proc_show, PDE(inode)->data);
863 static ssize_t dispatch_proc_write(struct file *file,
864 const char __user *userbuf,
865 size_t count, loff_t *pos)
867 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
868 char *kernbuf;
869 int ret;
871 if (!ibm || !ibm->write)
872 return -EINVAL;
873 if (count > PAGE_SIZE - 2)
874 return -EINVAL;
876 kernbuf = kmalloc(count + 2, GFP_KERNEL);
877 if (!kernbuf)
878 return -ENOMEM;
880 if (copy_from_user(kernbuf, userbuf, count)) {
881 kfree(kernbuf);
882 return -EFAULT;
885 kernbuf[count] = 0;
886 strcat(kernbuf, ",");
887 ret = ibm->write(kernbuf);
888 if (ret == 0)
889 ret = count;
891 kfree(kernbuf);
893 return ret;
896 static const struct file_operations dispatch_proc_fops = {
897 .owner = THIS_MODULE,
898 .open = dispatch_proc_open,
899 .read = seq_read,
900 .llseek = seq_lseek,
901 .release = single_release,
902 .write = dispatch_proc_write,
905 static char *next_cmd(char **cmds)
907 char *start = *cmds;
908 char *end;
910 while ((end = strchr(start, ',')) && end == start)
911 start = end + 1;
913 if (!end)
914 return NULL;
916 *end = 0;
917 *cmds = end + 1;
918 return start;
922 /****************************************************************************
923 ****************************************************************************
925 * Device model: input, hwmon and platform
927 ****************************************************************************
928 ****************************************************************************/
930 static struct platform_device *tpacpi_pdev;
931 static struct platform_device *tpacpi_sensors_pdev;
932 static struct device *tpacpi_hwmon;
933 static struct input_dev *tpacpi_inputdev;
934 static struct mutex tpacpi_inputdev_send_mutex;
935 static LIST_HEAD(tpacpi_all_drivers);
937 static int tpacpi_suspend_handler(struct platform_device *pdev,
938 pm_message_t state)
940 struct ibm_struct *ibm, *itmp;
942 list_for_each_entry_safe(ibm, itmp,
943 &tpacpi_all_drivers,
944 all_drivers) {
945 if (ibm->suspend)
946 (ibm->suspend)(state);
949 return 0;
952 static int tpacpi_resume_handler(struct platform_device *pdev)
954 struct ibm_struct *ibm, *itmp;
956 list_for_each_entry_safe(ibm, itmp,
957 &tpacpi_all_drivers,
958 all_drivers) {
959 if (ibm->resume)
960 (ibm->resume)();
963 return 0;
966 static void tpacpi_shutdown_handler(struct platform_device *pdev)
968 struct ibm_struct *ibm, *itmp;
970 list_for_each_entry_safe(ibm, itmp,
971 &tpacpi_all_drivers,
972 all_drivers) {
973 if (ibm->shutdown)
974 (ibm->shutdown)();
978 static struct platform_driver tpacpi_pdriver = {
979 .driver = {
980 .name = TPACPI_DRVR_NAME,
981 .owner = THIS_MODULE,
983 .suspend = tpacpi_suspend_handler,
984 .resume = tpacpi_resume_handler,
985 .shutdown = tpacpi_shutdown_handler,
988 static struct platform_driver tpacpi_hwmon_pdriver = {
989 .driver = {
990 .name = TPACPI_HWMON_DRVR_NAME,
991 .owner = THIS_MODULE,
995 /*************************************************************************
996 * sysfs support helpers
999 struct attribute_set {
1000 unsigned int members, max_members;
1001 struct attribute_group group;
1004 struct attribute_set_obj {
1005 struct attribute_set s;
1006 struct attribute *a;
1007 } __attribute__((packed));
1009 static struct attribute_set *create_attr_set(unsigned int max_members,
1010 const char *name)
1012 struct attribute_set_obj *sobj;
1014 if (max_members == 0)
1015 return NULL;
1017 /* Allocates space for implicit NULL at the end too */
1018 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1019 max_members * sizeof(struct attribute *),
1020 GFP_KERNEL);
1021 if (!sobj)
1022 return NULL;
1023 sobj->s.max_members = max_members;
1024 sobj->s.group.attrs = &sobj->a;
1025 sobj->s.group.name = name;
1027 return &sobj->s;
1030 #define destroy_attr_set(_set) \
1031 kfree(_set);
1033 /* not multi-threaded safe, use it in a single thread per set */
1034 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1036 if (!s || !attr)
1037 return -EINVAL;
1039 if (s->members >= s->max_members)
1040 return -ENOMEM;
1042 s->group.attrs[s->members] = attr;
1043 s->members++;
1045 return 0;
1048 static int add_many_to_attr_set(struct attribute_set *s,
1049 struct attribute **attr,
1050 unsigned int count)
1052 int i, res;
1054 for (i = 0; i < count; i++) {
1055 res = add_to_attr_set(s, attr[i]);
1056 if (res)
1057 return res;
1060 return 0;
1063 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1065 sysfs_remove_group(kobj, &s->group);
1066 destroy_attr_set(s);
1069 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1070 sysfs_create_group(_kobj, &_attr_set->group)
1072 static int parse_strtoul(const char *buf,
1073 unsigned long max, unsigned long *value)
1075 char *endp;
1077 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1078 endp = skip_spaces(endp);
1079 if (*endp || *value > max)
1080 return -EINVAL;
1082 return 0;
1085 static void tpacpi_disable_brightness_delay(void)
1087 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1088 pr_notice("ACPI backlight control delay disabled\n");
1091 static void printk_deprecated_attribute(const char * const what,
1092 const char * const details)
1094 tpacpi_log_usertask("deprecated sysfs attribute");
1095 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1096 "will be removed. %s\n",
1097 what, details);
1100 /*************************************************************************
1101 * rfkill and radio control support helpers
1105 * ThinkPad-ACPI firmware handling model:
1107 * WLSW (master wireless switch) is event-driven, and is common to all
1108 * firmware-controlled radios. It cannot be controlled, just monitored,
1109 * as expected. It overrides all radio state in firmware
1111 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1112 * (TODO: verify how WLSW interacts with the returned radio state).
1114 * The only time there are shadow radio state changes, is when
1115 * masked-off hotkeys are used.
1119 * Internal driver API for radio state:
1121 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1122 * bool: true means radio blocked (off)
1124 enum tpacpi_rfkill_state {
1125 TPACPI_RFK_RADIO_OFF = 0,
1126 TPACPI_RFK_RADIO_ON
1129 /* rfkill switches */
1130 enum tpacpi_rfk_id {
1131 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1132 TPACPI_RFK_WWAN_SW_ID,
1133 TPACPI_RFK_UWB_SW_ID,
1134 TPACPI_RFK_SW_MAX
1137 static const char *tpacpi_rfkill_names[] = {
1138 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1139 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1140 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1141 [TPACPI_RFK_SW_MAX] = NULL
1144 /* ThinkPad-ACPI rfkill subdriver */
1145 struct tpacpi_rfk {
1146 struct rfkill *rfkill;
1147 enum tpacpi_rfk_id id;
1148 const struct tpacpi_rfk_ops *ops;
1151 struct tpacpi_rfk_ops {
1152 /* firmware interface */
1153 int (*get_status)(void);
1154 int (*set_status)(const enum tpacpi_rfkill_state);
1157 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1159 /* Query FW and update rfkill sw state for a given rfkill switch */
1160 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1162 int status;
1164 if (!tp_rfk)
1165 return -ENODEV;
1167 status = (tp_rfk->ops->get_status)();
1168 if (status < 0)
1169 return status;
1171 rfkill_set_sw_state(tp_rfk->rfkill,
1172 (status == TPACPI_RFK_RADIO_OFF));
1174 return status;
1177 /* Query FW and update rfkill sw state for all rfkill switches */
1178 static void tpacpi_rfk_update_swstate_all(void)
1180 unsigned int i;
1182 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1183 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1187 * Sync the HW-blocking state of all rfkill switches,
1188 * do notice it causes the rfkill core to schedule uevents
1190 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1192 unsigned int i;
1193 struct tpacpi_rfk *tp_rfk;
1195 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1196 tp_rfk = tpacpi_rfkill_switches[i];
1197 if (tp_rfk) {
1198 if (rfkill_set_hw_state(tp_rfk->rfkill,
1199 blocked)) {
1200 /* ignore -- we track sw block */
1206 /* Call to get the WLSW state from the firmware */
1207 static int hotkey_get_wlsw(void);
1209 /* Call to query WLSW state and update all rfkill switches */
1210 static bool tpacpi_rfk_check_hwblock_state(void)
1212 int res = hotkey_get_wlsw();
1213 int hw_blocked;
1215 /* When unknown or unsupported, we have to assume it is unblocked */
1216 if (res < 0)
1217 return false;
1219 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1220 tpacpi_rfk_update_hwblock_state(hw_blocked);
1222 return hw_blocked;
1225 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1227 struct tpacpi_rfk *tp_rfk = data;
1228 int res;
1230 dbg_printk(TPACPI_DBG_RFKILL,
1231 "request to change radio state to %s\n",
1232 blocked ? "blocked" : "unblocked");
1234 /* try to set radio state */
1235 res = (tp_rfk->ops->set_status)(blocked ?
1236 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1238 /* and update the rfkill core with whatever the FW really did */
1239 tpacpi_rfk_update_swstate(tp_rfk);
1241 return (res < 0) ? res : 0;
1244 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1245 .set_block = tpacpi_rfk_hook_set_block,
1248 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1249 const struct tpacpi_rfk_ops *tp_rfkops,
1250 const enum rfkill_type rfktype,
1251 const char *name,
1252 const bool set_default)
1254 struct tpacpi_rfk *atp_rfk;
1255 int res;
1256 bool sw_state = false;
1257 bool hw_state;
1258 int sw_status;
1260 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1262 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1263 if (atp_rfk)
1264 atp_rfk->rfkill = rfkill_alloc(name,
1265 &tpacpi_pdev->dev,
1266 rfktype,
1267 &tpacpi_rfk_rfkill_ops,
1268 atp_rfk);
1269 if (!atp_rfk || !atp_rfk->rfkill) {
1270 pr_err("failed to allocate memory for rfkill class\n");
1271 kfree(atp_rfk);
1272 return -ENOMEM;
1275 atp_rfk->id = id;
1276 atp_rfk->ops = tp_rfkops;
1278 sw_status = (tp_rfkops->get_status)();
1279 if (sw_status < 0) {
1280 pr_err("failed to read initial state for %s, error %d\n",
1281 name, sw_status);
1282 } else {
1283 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1284 if (set_default) {
1285 /* try to keep the initial state, since we ask the
1286 * firmware to preserve it across S5 in NVRAM */
1287 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1290 hw_state = tpacpi_rfk_check_hwblock_state();
1291 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1293 res = rfkill_register(atp_rfk->rfkill);
1294 if (res < 0) {
1295 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1296 rfkill_destroy(atp_rfk->rfkill);
1297 kfree(atp_rfk);
1298 return res;
1301 tpacpi_rfkill_switches[id] = atp_rfk;
1303 pr_info("rfkill switch %s: radio is %sblocked\n",
1304 name, (sw_state || hw_state) ? "" : "un");
1305 return 0;
1308 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1310 struct tpacpi_rfk *tp_rfk;
1312 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1314 tp_rfk = tpacpi_rfkill_switches[id];
1315 if (tp_rfk) {
1316 rfkill_unregister(tp_rfk->rfkill);
1317 rfkill_destroy(tp_rfk->rfkill);
1318 tpacpi_rfkill_switches[id] = NULL;
1319 kfree(tp_rfk);
1323 static void printk_deprecated_rfkill_attribute(const char * const what)
1325 printk_deprecated_attribute(what,
1326 "Please switch to generic rfkill before year 2010");
1329 /* sysfs <radio> enable ------------------------------------------------ */
1330 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1331 struct device_attribute *attr,
1332 char *buf)
1334 int status;
1336 printk_deprecated_rfkill_attribute(attr->attr.name);
1338 /* This is in the ABI... */
1339 if (tpacpi_rfk_check_hwblock_state()) {
1340 status = TPACPI_RFK_RADIO_OFF;
1341 } else {
1342 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1343 if (status < 0)
1344 return status;
1347 return snprintf(buf, PAGE_SIZE, "%d\n",
1348 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1351 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1352 struct device_attribute *attr,
1353 const char *buf, size_t count)
1355 unsigned long t;
1356 int res;
1358 printk_deprecated_rfkill_attribute(attr->attr.name);
1360 if (parse_strtoul(buf, 1, &t))
1361 return -EINVAL;
1363 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1365 /* This is in the ABI... */
1366 if (tpacpi_rfk_check_hwblock_state() && !!t)
1367 return -EPERM;
1369 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1370 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1371 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1373 return (res < 0) ? res : count;
1376 /* procfs -------------------------------------------------------------- */
1377 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1379 if (id >= TPACPI_RFK_SW_MAX)
1380 seq_printf(m, "status:\t\tnot supported\n");
1381 else {
1382 int status;
1384 /* This is in the ABI... */
1385 if (tpacpi_rfk_check_hwblock_state()) {
1386 status = TPACPI_RFK_RADIO_OFF;
1387 } else {
1388 status = tpacpi_rfk_update_swstate(
1389 tpacpi_rfkill_switches[id]);
1390 if (status < 0)
1391 return status;
1394 seq_printf(m, "status:\t\t%s\n",
1395 (status == TPACPI_RFK_RADIO_ON) ?
1396 "enabled" : "disabled");
1397 seq_printf(m, "commands:\tenable, disable\n");
1400 return 0;
1403 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1405 char *cmd;
1406 int status = -1;
1407 int res = 0;
1409 if (id >= TPACPI_RFK_SW_MAX)
1410 return -ENODEV;
1412 while ((cmd = next_cmd(&buf))) {
1413 if (strlencmp(cmd, "enable") == 0)
1414 status = TPACPI_RFK_RADIO_ON;
1415 else if (strlencmp(cmd, "disable") == 0)
1416 status = TPACPI_RFK_RADIO_OFF;
1417 else
1418 return -EINVAL;
1421 if (status != -1) {
1422 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1423 (status == TPACPI_RFK_RADIO_ON) ?
1424 "enable" : "disable",
1425 tpacpi_rfkill_names[id]);
1426 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1427 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1430 return res;
1433 /*************************************************************************
1434 * thinkpad-acpi driver attributes
1437 /* interface_version --------------------------------------------------- */
1438 static ssize_t tpacpi_driver_interface_version_show(
1439 struct device_driver *drv,
1440 char *buf)
1442 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1445 static DRIVER_ATTR(interface_version, S_IRUGO,
1446 tpacpi_driver_interface_version_show, NULL);
1448 /* debug_level --------------------------------------------------------- */
1449 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1450 char *buf)
1452 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1455 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1456 const char *buf, size_t count)
1458 unsigned long t;
1460 if (parse_strtoul(buf, 0xffff, &t))
1461 return -EINVAL;
1463 dbg_level = t;
1465 return count;
1468 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1469 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1471 /* version ------------------------------------------------------------- */
1472 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1473 char *buf)
1475 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1476 TPACPI_DESC, TPACPI_VERSION);
1479 static DRIVER_ATTR(version, S_IRUGO,
1480 tpacpi_driver_version_show, NULL);
1482 /* --------------------------------------------------------------------- */
1484 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1486 /* wlsw_emulstate ------------------------------------------------------ */
1487 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1488 char *buf)
1490 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1493 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1494 const char *buf, size_t count)
1496 unsigned long t;
1498 if (parse_strtoul(buf, 1, &t))
1499 return -EINVAL;
1501 if (tpacpi_wlsw_emulstate != !!t) {
1502 tpacpi_wlsw_emulstate = !!t;
1503 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1506 return count;
1509 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1510 tpacpi_driver_wlsw_emulstate_show,
1511 tpacpi_driver_wlsw_emulstate_store);
1513 /* bluetooth_emulstate ------------------------------------------------- */
1514 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1515 struct device_driver *drv,
1516 char *buf)
1518 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1521 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1522 struct device_driver *drv,
1523 const char *buf, size_t count)
1525 unsigned long t;
1527 if (parse_strtoul(buf, 1, &t))
1528 return -EINVAL;
1530 tpacpi_bluetooth_emulstate = !!t;
1532 return count;
1535 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1536 tpacpi_driver_bluetooth_emulstate_show,
1537 tpacpi_driver_bluetooth_emulstate_store);
1539 /* wwan_emulstate ------------------------------------------------- */
1540 static ssize_t tpacpi_driver_wwan_emulstate_show(
1541 struct device_driver *drv,
1542 char *buf)
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1547 static ssize_t tpacpi_driver_wwan_emulstate_store(
1548 struct device_driver *drv,
1549 const char *buf, size_t count)
1551 unsigned long t;
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1556 tpacpi_wwan_emulstate = !!t;
1558 return count;
1561 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1562 tpacpi_driver_wwan_emulstate_show,
1563 tpacpi_driver_wwan_emulstate_store);
1565 /* uwb_emulstate ------------------------------------------------- */
1566 static ssize_t tpacpi_driver_uwb_emulstate_show(
1567 struct device_driver *drv,
1568 char *buf)
1570 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1573 static ssize_t tpacpi_driver_uwb_emulstate_store(
1574 struct device_driver *drv,
1575 const char *buf, size_t count)
1577 unsigned long t;
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1582 tpacpi_uwb_emulstate = !!t;
1584 return count;
1587 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1588 tpacpi_driver_uwb_emulstate_show,
1589 tpacpi_driver_uwb_emulstate_store);
1590 #endif
1592 /* --------------------------------------------------------------------- */
1594 static struct driver_attribute *tpacpi_driver_attributes[] = {
1595 &driver_attr_debug_level, &driver_attr_version,
1596 &driver_attr_interface_version,
1599 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1601 int i, res;
1603 i = 0;
1604 res = 0;
1605 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1606 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1607 i++;
1610 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1611 if (!res && dbg_wlswemul)
1612 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1613 if (!res && dbg_bluetoothemul)
1614 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1615 if (!res && dbg_wwanemul)
1616 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1617 if (!res && dbg_uwbemul)
1618 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1619 #endif
1621 return res;
1624 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1626 int i;
1628 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1629 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1631 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1632 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1633 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1634 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1635 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1636 #endif
1639 /*************************************************************************
1640 * Firmware Data
1644 * Table of recommended minimum BIOS versions
1646 * Reasons for listing:
1647 * 1. Stable BIOS, listed because the unknown amount of
1648 * bugs and bad ACPI behaviour on older versions
1650 * 2. BIOS or EC fw with known bugs that trigger on Linux
1652 * 3. BIOS with known reduced functionality in older versions
1654 * We recommend the latest BIOS and EC version.
1655 * We only support the latest BIOS and EC fw version as a rule.
1657 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1658 * Information from users in ThinkWiki
1660 * WARNING: we use this table also to detect that the machine is
1661 * a ThinkPad in some cases, so don't remove entries lightly.
1664 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1665 { .vendor = (__v), \
1666 .bios = TPID(__id1, __id2), \
1667 .ec = TPACPI_MATCH_ANY, \
1668 .quirks = TPACPI_MATCH_ANY << 16 \
1669 | (__bv1) << 8 | (__bv2) }
1671 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1672 __eid, __ev1, __ev2) \
1673 { .vendor = (__v), \
1674 .bios = TPID(__bid1, __bid2), \
1675 .ec = __eid, \
1676 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1677 | (__bv1) << 8 | (__bv2) }
1679 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1680 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1682 /* Outdated IBM BIOSes often lack the EC id string */
1683 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1685 __bv1, __bv2, TPID(__id1, __id2), \
1686 __ev1, __ev2), \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1689 __ev1, __ev2)
1691 /* Outdated IBM BIOSes often lack the EC id string */
1692 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1693 __eid1, __eid2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1695 __bv1, __bv2, TPID(__eid1, __eid2), \
1696 __ev1, __ev2), \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1699 __ev1, __ev2)
1701 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1702 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1704 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1705 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1706 __bv1, __bv2, TPID(__id1, __id2), \
1707 __ev1, __ev2)
1709 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1710 __eid1, __eid2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1712 __bv1, __bv2, TPID(__eid1, __eid2), \
1713 __ev1, __ev2)
1715 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1716 /* Numeric models ------------------ */
1717 /* FW MODEL BIOS VERS */
1718 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1719 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1720 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1721 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1722 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1723 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1724 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1725 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1726 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1728 /* A-series ------------------------- */
1729 /* FW MODEL BIOS VERS EC VERS */
1730 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1731 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1732 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1733 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1734 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1735 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1736 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1737 TPV_QI0('1', '3', '2', '0'), /* A22m */
1738 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1739 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1740 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1742 /* G-series ------------------------- */
1743 /* FW MODEL BIOS VERS */
1744 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1745 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1747 /* R-series, T-series --------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1750 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1751 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1752 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1753 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1754 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1755 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1756 T40/p, T41/p, T42/p (1) */
1757 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1758 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1759 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1760 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1762 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1763 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1764 TPV_QI0('1', '6', '3', '2'), /* T22 */
1765 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1766 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1767 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1769 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1770 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1771 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1773 /* BIOS FW BIOS VERS EC FW EC VERS */
1774 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1775 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1777 /* X-series ------------------------- */
1778 /* FW MODEL BIOS VERS EC VERS */
1779 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1780 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1781 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1782 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1783 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1784 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1785 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1787 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1788 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1790 /* (0) - older versions lack DMI EC fw string and functionality */
1791 /* (1) - older versions known to lack functionality */
1794 #undef TPV_QL1
1795 #undef TPV_QL0
1796 #undef TPV_QI2
1797 #undef TPV_QI1
1798 #undef TPV_QI0
1799 #undef TPV_Q_X
1800 #undef TPV_Q
1802 static void __init tpacpi_check_outdated_fw(void)
1804 unsigned long fwvers;
1805 u16 ec_version, bios_version;
1807 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable));
1810 if (!fwvers)
1811 return;
1813 bios_version = fwvers & 0xffffU;
1814 ec_version = (fwvers >> 16) & 0xffffU;
1816 /* note that unknown versions are set to 0x0000 and we use that */
1817 if ((bios_version > thinkpad_id.bios_release) ||
1818 (ec_version > thinkpad_id.ec_release &&
1819 ec_version != TPACPI_MATCH_ANY)) {
1821 * The changelogs would let us track down the exact
1822 * reason, but it is just too much of a pain to track
1823 * it. We only list BIOSes that are either really
1824 * broken, or really stable to begin with, so it is
1825 * best if the user upgrades the firmware anyway.
1827 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1828 pr_warn("WARNING: This firmware may be missing critical bug "
1829 "fixes and/or important features\n");
1833 static bool __init tpacpi_is_fw_known(void)
1835 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1836 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1839 /****************************************************************************
1840 ****************************************************************************
1842 * Subdrivers
1844 ****************************************************************************
1845 ****************************************************************************/
1847 /*************************************************************************
1848 * thinkpad-acpi metadata subdriver
1851 static int thinkpad_acpi_driver_read(struct seq_file *m)
1853 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1854 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1855 return 0;
1858 static struct ibm_struct thinkpad_acpi_driver_data = {
1859 .name = "driver",
1860 .read = thinkpad_acpi_driver_read,
1863 /*************************************************************************
1864 * Hotkey subdriver
1868 * ThinkPad firmware event model
1870 * The ThinkPad firmware has two main event interfaces: normal ACPI
1871 * notifications (which follow the ACPI standard), and a private event
1872 * interface.
1874 * The private event interface also issues events for the hotkeys. As
1875 * the driver gained features, the event handling code ended up being
1876 * built around the hotkey subdriver. This will need to be refactored
1877 * to a more formal event API eventually.
1879 * Some "hotkeys" are actually supposed to be used as event reports,
1880 * such as "brightness has changed", "volume has changed", depending on
1881 * the ThinkPad model and how the firmware is operating.
1883 * Unlike other classes, hotkey-class events have mask/unmask control on
1884 * non-ancient firmware. However, how it behaves changes a lot with the
1885 * firmware model and version.
1888 enum { /* hot key scan codes (derived from ACPI DSDT) */
1889 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1890 TP_ACPI_HOTKEYSCAN_FNF2,
1891 TP_ACPI_HOTKEYSCAN_FNF3,
1892 TP_ACPI_HOTKEYSCAN_FNF4,
1893 TP_ACPI_HOTKEYSCAN_FNF5,
1894 TP_ACPI_HOTKEYSCAN_FNF6,
1895 TP_ACPI_HOTKEYSCAN_FNF7,
1896 TP_ACPI_HOTKEYSCAN_FNF8,
1897 TP_ACPI_HOTKEYSCAN_FNF9,
1898 TP_ACPI_HOTKEYSCAN_FNF10,
1899 TP_ACPI_HOTKEYSCAN_FNF11,
1900 TP_ACPI_HOTKEYSCAN_FNF12,
1901 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1902 TP_ACPI_HOTKEYSCAN_FNINSERT,
1903 TP_ACPI_HOTKEYSCAN_FNDELETE,
1904 TP_ACPI_HOTKEYSCAN_FNHOME,
1905 TP_ACPI_HOTKEYSCAN_FNEND,
1906 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1907 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1908 TP_ACPI_HOTKEYSCAN_FNSPACE,
1909 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1910 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1911 TP_ACPI_HOTKEYSCAN_MUTE,
1912 TP_ACPI_HOTKEYSCAN_THINKPAD,
1913 TP_ACPI_HOTKEYSCAN_UNK1,
1914 TP_ACPI_HOTKEYSCAN_UNK2,
1915 TP_ACPI_HOTKEYSCAN_UNK3,
1916 TP_ACPI_HOTKEYSCAN_UNK4,
1917 TP_ACPI_HOTKEYSCAN_UNK5,
1918 TP_ACPI_HOTKEYSCAN_UNK6,
1919 TP_ACPI_HOTKEYSCAN_UNK7,
1920 TP_ACPI_HOTKEYSCAN_UNK8,
1922 /* Hotkey keymap size */
1923 TPACPI_HOTKEY_MAP_LEN
1926 enum { /* Keys/events available through NVRAM polling */
1927 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1928 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1931 enum { /* Positions of some of the keys in hotkey masks */
1932 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1933 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1934 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1935 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1936 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1937 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1938 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1939 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1940 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1941 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1942 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1945 enum { /* NVRAM to ACPI HKEY group map */
1946 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1947 TP_ACPI_HKEY_ZOOM_MASK |
1948 TP_ACPI_HKEY_DISPSWTCH_MASK |
1949 TP_ACPI_HKEY_HIBERNATE_MASK,
1950 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1951 TP_ACPI_HKEY_BRGHTDWN_MASK,
1952 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1953 TP_ACPI_HKEY_VOLDWN_MASK |
1954 TP_ACPI_HKEY_MUTE_MASK,
1957 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1958 struct tp_nvram_state {
1959 u16 thinkpad_toggle:1;
1960 u16 zoom_toggle:1;
1961 u16 display_toggle:1;
1962 u16 thinklight_toggle:1;
1963 u16 hibernate_toggle:1;
1964 u16 displayexp_toggle:1;
1965 u16 display_state:1;
1966 u16 brightness_toggle:1;
1967 u16 volume_toggle:1;
1968 u16 mute:1;
1970 u8 brightness_level;
1971 u8 volume_level;
1974 /* kthread for the hotkey poller */
1975 static struct task_struct *tpacpi_hotkey_task;
1977 /* Acquired while the poller kthread is running, use to sync start/stop */
1978 static struct mutex hotkey_thread_mutex;
1981 * Acquire mutex to write poller control variables as an
1982 * atomic block.
1984 * Increment hotkey_config_change when changing them if you
1985 * want the kthread to forget old state.
1987 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1989 static struct mutex hotkey_thread_data_mutex;
1990 static unsigned int hotkey_config_change;
1993 * hotkey poller control variables
1995 * Must be atomic or readers will also need to acquire mutex
1997 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1998 * should be used only when the changes need to be taken as
1999 * a block, OR when one needs to force the kthread to forget
2000 * old state.
2002 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2003 static unsigned int hotkey_poll_freq = 10; /* Hz */
2005 #define HOTKEY_CONFIG_CRITICAL_START \
2006 do { \
2007 mutex_lock(&hotkey_thread_data_mutex); \
2008 hotkey_config_change++; \
2009 } while (0);
2010 #define HOTKEY_CONFIG_CRITICAL_END \
2011 mutex_unlock(&hotkey_thread_data_mutex);
2013 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2015 #define hotkey_source_mask 0U
2016 #define HOTKEY_CONFIG_CRITICAL_START
2017 #define HOTKEY_CONFIG_CRITICAL_END
2019 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2021 static struct mutex hotkey_mutex;
2023 static enum { /* Reasons for waking up */
2024 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2025 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2026 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2027 } hotkey_wakeup_reason;
2029 static int hotkey_autosleep_ack;
2031 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2032 static u32 hotkey_all_mask; /* all events supported in fw */
2033 static u32 hotkey_reserved_mask; /* events better left disabled */
2034 static u32 hotkey_driver_mask; /* events needed by the driver */
2035 static u32 hotkey_user_mask; /* events visible to userspace */
2036 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2038 static unsigned int hotkey_report_mode;
2040 static u16 *hotkey_keycode_map;
2042 static struct attribute_set *hotkey_dev_attributes;
2044 static void tpacpi_driver_event(const unsigned int hkey_event);
2045 static void hotkey_driver_event(const unsigned int scancode);
2046 static void hotkey_poll_setup(const bool may_warn);
2048 /* HKEY.MHKG() return bits */
2049 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2051 static int hotkey_get_wlsw(void)
2053 int status;
2055 if (!tp_features.hotkey_wlsw)
2056 return -ENODEV;
2058 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2059 if (dbg_wlswemul)
2060 return (tpacpi_wlsw_emulstate) ?
2061 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2062 #endif
2064 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2065 return -EIO;
2067 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2070 static int hotkey_get_tablet_mode(int *status)
2072 int s;
2074 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2075 return -EIO;
2077 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2078 return 0;
2082 * Reads current event mask from firmware, and updates
2083 * hotkey_acpi_mask accordingly. Also resets any bits
2084 * from hotkey_user_mask that are unavailable to be
2085 * delivered (shadow requirement of the userspace ABI).
2087 * Call with hotkey_mutex held
2089 static int hotkey_mask_get(void)
2091 if (tp_features.hotkey_mask) {
2092 u32 m = 0;
2094 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2095 return -EIO;
2097 hotkey_acpi_mask = m;
2098 } else {
2099 /* no mask support doesn't mean no event support... */
2100 hotkey_acpi_mask = hotkey_all_mask;
2103 /* sync userspace-visible mask */
2104 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2106 return 0;
2109 void static hotkey_mask_warn_incomplete_mask(void)
2111 /* log only what the user can fix... */
2112 const u32 wantedmask = hotkey_driver_mask &
2113 ~(hotkey_acpi_mask | hotkey_source_mask) &
2114 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2116 if (wantedmask)
2117 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2121 * Set the firmware mask when supported
2123 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2125 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2127 * Call with hotkey_mutex held
2129 static int hotkey_mask_set(u32 mask)
2131 int i;
2132 int rc = 0;
2134 const u32 fwmask = mask & ~hotkey_source_mask;
2136 if (tp_features.hotkey_mask) {
2137 for (i = 0; i < 32; i++) {
2138 if (!acpi_evalf(hkey_handle,
2139 NULL, "MHKM", "vdd", i + 1,
2140 !!(mask & (1 << i)))) {
2141 rc = -EIO;
2142 break;
2148 * We *must* make an inconditional call to hotkey_mask_get to
2149 * refresh hotkey_acpi_mask and update hotkey_user_mask
2151 * Take the opportunity to also log when we cannot _enable_
2152 * a given event.
2154 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2155 pr_notice("asked for hotkey mask 0x%08x, but "
2156 "firmware forced it to 0x%08x\n",
2157 fwmask, hotkey_acpi_mask);
2160 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2161 hotkey_mask_warn_incomplete_mask();
2163 return rc;
2167 * Sets hotkey_user_mask and tries to set the firmware mask
2169 * Call with hotkey_mutex held
2171 static int hotkey_user_mask_set(const u32 mask)
2173 int rc;
2175 /* Give people a chance to notice they are doing something that
2176 * is bound to go boom on their users sooner or later */
2177 if (!tp_warned.hotkey_mask_ff &&
2178 (mask == 0xffff || mask == 0xffffff ||
2179 mask == 0xffffffff)) {
2180 tp_warned.hotkey_mask_ff = 1;
2181 pr_notice("setting the hotkey mask to 0x%08x is likely "
2182 "not the best way to go about it\n", mask);
2183 pr_notice("please consider using the driver defaults, "
2184 "and refer to up-to-date thinkpad-acpi "
2185 "documentation\n");
2188 /* Try to enable what the user asked for, plus whatever we need.
2189 * this syncs everything but won't enable bits in hotkey_user_mask */
2190 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2192 /* Enable the available bits in hotkey_user_mask */
2193 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2195 return rc;
2199 * Sets the driver hotkey mask.
2201 * Can be called even if the hotkey subdriver is inactive
2203 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2205 int rc;
2207 /* Do the right thing if hotkey_init has not been called yet */
2208 if (!tp_features.hotkey) {
2209 hotkey_driver_mask = mask;
2210 return 0;
2213 mutex_lock(&hotkey_mutex);
2215 HOTKEY_CONFIG_CRITICAL_START
2216 hotkey_driver_mask = mask;
2217 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2218 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2219 #endif
2220 HOTKEY_CONFIG_CRITICAL_END
2222 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2223 ~hotkey_source_mask);
2224 hotkey_poll_setup(true);
2226 mutex_unlock(&hotkey_mutex);
2228 return rc;
2231 static int hotkey_status_get(int *status)
2233 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2234 return -EIO;
2236 return 0;
2239 static int hotkey_status_set(bool enable)
2241 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2242 return -EIO;
2244 return 0;
2247 static void tpacpi_input_send_tabletsw(void)
2249 int state;
2251 if (tp_features.hotkey_tablet &&
2252 !hotkey_get_tablet_mode(&state)) {
2253 mutex_lock(&tpacpi_inputdev_send_mutex);
2255 input_report_switch(tpacpi_inputdev,
2256 SW_TABLET_MODE, !!state);
2257 input_sync(tpacpi_inputdev);
2259 mutex_unlock(&tpacpi_inputdev_send_mutex);
2263 /* Do NOT call without validating scancode first */
2264 static void tpacpi_input_send_key(const unsigned int scancode)
2266 const unsigned int keycode = hotkey_keycode_map[scancode];
2268 if (keycode != KEY_RESERVED) {
2269 mutex_lock(&tpacpi_inputdev_send_mutex);
2271 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2272 input_report_key(tpacpi_inputdev, keycode, 1);
2273 input_sync(tpacpi_inputdev);
2275 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2276 input_report_key(tpacpi_inputdev, keycode, 0);
2277 input_sync(tpacpi_inputdev);
2279 mutex_unlock(&tpacpi_inputdev_send_mutex);
2283 /* Do NOT call without validating scancode first */
2284 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2286 hotkey_driver_event(scancode);
2287 if (hotkey_user_mask & (1 << scancode))
2288 tpacpi_input_send_key(scancode);
2291 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2292 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2294 /* Do NOT call without validating scancode first */
2295 static void tpacpi_hotkey_send_key(unsigned int scancode)
2297 tpacpi_input_send_key_masked(scancode);
2298 if (hotkey_report_mode < 2) {
2299 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2300 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2304 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2306 u8 d;
2308 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2310 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2311 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2312 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2313 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2315 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2317 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2319 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2320 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2321 n->displayexp_toggle =
2322 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2324 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2326 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2327 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2328 n->brightness_toggle =
2329 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2331 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2332 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2333 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2334 >> TP_NVRAM_POS_LEVEL_VOLUME;
2335 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2336 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2340 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2341 struct tp_nvram_state *newn,
2342 const u32 event_mask)
2345 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2346 do { \
2347 if ((event_mask & (1 << __scancode)) && \
2348 oldn->__member != newn->__member) \
2349 tpacpi_hotkey_send_key(__scancode); \
2350 } while (0)
2352 #define TPACPI_MAY_SEND_KEY(__scancode) \
2353 do { \
2354 if (event_mask & (1 << __scancode)) \
2355 tpacpi_hotkey_send_key(__scancode); \
2356 } while (0)
2358 void issue_volchange(const unsigned int oldvol,
2359 const unsigned int newvol)
2361 unsigned int i = oldvol;
2363 while (i > newvol) {
2364 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2365 i--;
2367 while (i < newvol) {
2368 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2369 i++;
2373 void issue_brightnesschange(const unsigned int oldbrt,
2374 const unsigned int newbrt)
2376 unsigned int i = oldbrt;
2378 while (i > newbrt) {
2379 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2380 i--;
2382 while (i < newbrt) {
2383 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2384 i++;
2388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2398 * Handle volume
2400 * This code is supposed to duplicate the IBM firmware behaviour:
2401 * - Pressing MUTE issues mute hotkey message, even when already mute
2402 * - Pressing Volume up/down issues volume up/down hotkey messages,
2403 * even when already at maximum or minimum volume
2404 * - The act of unmuting issues volume up/down notification,
2405 * depending which key was used to unmute
2407 * We are constrained to what the NVRAM can tell us, which is not much
2408 * and certainly not enough if more than one volume hotkey was pressed
2409 * since the last poll cycle.
2411 * Just to make our life interesting, some newer Lenovo ThinkPads have
2412 * bugs in the BIOS and may fail to update volume_toggle properly.
2414 if (newn->mute) {
2415 /* muted */
2416 if (!oldn->mute ||
2417 oldn->volume_toggle != newn->volume_toggle ||
2418 oldn->volume_level != newn->volume_level) {
2419 /* recently muted, or repeated mute keypress, or
2420 * multiple presses ending in mute */
2421 issue_volchange(oldn->volume_level, newn->volume_level);
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2424 } else {
2425 /* unmute */
2426 if (oldn->mute) {
2427 /* recently unmuted, issue 'unmute' keypress */
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 if (oldn->volume_level != newn->volume_level) {
2431 issue_volchange(oldn->volume_level, newn->volume_level);
2432 } else if (oldn->volume_toggle != newn->volume_toggle) {
2433 /* repeated vol up/down keypress at end of scale ? */
2434 if (newn->volume_level == 0)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2436 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2441 /* handle brightness */
2442 if (oldn->brightness_level != newn->brightness_level) {
2443 issue_brightnesschange(oldn->brightness_level,
2444 newn->brightness_level);
2445 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2446 /* repeated key presses that didn't change state */
2447 if (newn->brightness_level == 0)
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2449 else if (newn->brightness_level >= bright_maxlvl
2450 && !tp_features.bright_unkfw)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2454 #undef TPACPI_COMPARE_KEY
2455 #undef TPACPI_MAY_SEND_KEY
2459 * Polling driver
2461 * We track all events in hotkey_source_mask all the time, since
2462 * most of them are edge-based. We only issue those requested by
2463 * hotkey_user_mask or hotkey_driver_mask, though.
2465 static int hotkey_kthread(void *data)
2467 struct tp_nvram_state s[2];
2468 u32 poll_mask, event_mask;
2469 unsigned int si, so;
2470 unsigned long t;
2471 unsigned int change_detector, must_reset;
2472 unsigned int poll_freq;
2474 mutex_lock(&hotkey_thread_mutex);
2476 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2477 goto exit;
2479 set_freezable();
2481 so = 0;
2482 si = 1;
2483 t = 0;
2485 /* Initial state for compares */
2486 mutex_lock(&hotkey_thread_data_mutex);
2487 change_detector = hotkey_config_change;
2488 poll_mask = hotkey_source_mask;
2489 event_mask = hotkey_source_mask &
2490 (hotkey_driver_mask | hotkey_user_mask);
2491 poll_freq = hotkey_poll_freq;
2492 mutex_unlock(&hotkey_thread_data_mutex);
2493 hotkey_read_nvram(&s[so], poll_mask);
2495 while (!kthread_should_stop()) {
2496 if (t == 0) {
2497 if (likely(poll_freq))
2498 t = 1000/poll_freq;
2499 else
2500 t = 100; /* should never happen... */
2502 t = msleep_interruptible(t);
2503 if (unlikely(kthread_should_stop()))
2504 break;
2505 must_reset = try_to_freeze();
2506 if (t > 0 && !must_reset)
2507 continue;
2509 mutex_lock(&hotkey_thread_data_mutex);
2510 if (must_reset || hotkey_config_change != change_detector) {
2511 /* forget old state on thaw or config change */
2512 si = so;
2513 t = 0;
2514 change_detector = hotkey_config_change;
2516 poll_mask = hotkey_source_mask;
2517 event_mask = hotkey_source_mask &
2518 (hotkey_driver_mask | hotkey_user_mask);
2519 poll_freq = hotkey_poll_freq;
2520 mutex_unlock(&hotkey_thread_data_mutex);
2522 if (likely(poll_mask)) {
2523 hotkey_read_nvram(&s[si], poll_mask);
2524 if (likely(si != so)) {
2525 hotkey_compare_and_issue_event(&s[so], &s[si],
2526 event_mask);
2530 so = si;
2531 si ^= 1;
2534 exit:
2535 mutex_unlock(&hotkey_thread_mutex);
2536 return 0;
2539 /* call with hotkey_mutex held */
2540 static void hotkey_poll_stop_sync(void)
2542 if (tpacpi_hotkey_task) {
2543 if (frozen(tpacpi_hotkey_task) ||
2544 freezing(tpacpi_hotkey_task))
2545 thaw_process(tpacpi_hotkey_task);
2547 kthread_stop(tpacpi_hotkey_task);
2548 tpacpi_hotkey_task = NULL;
2549 mutex_lock(&hotkey_thread_mutex);
2550 /* at this point, the thread did exit */
2551 mutex_unlock(&hotkey_thread_mutex);
2555 /* call with hotkey_mutex held */
2556 static void hotkey_poll_setup(const bool may_warn)
2558 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2559 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2561 if (hotkey_poll_freq > 0 &&
2562 (poll_driver_mask ||
2563 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2564 if (!tpacpi_hotkey_task) {
2565 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2566 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2567 if (IS_ERR(tpacpi_hotkey_task)) {
2568 tpacpi_hotkey_task = NULL;
2569 pr_err("could not create kernel thread "
2570 "for hotkey polling\n");
2573 } else {
2574 hotkey_poll_stop_sync();
2575 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2576 hotkey_poll_freq == 0) {
2577 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2578 "require polling, which is currently "
2579 "disabled\n",
2580 poll_user_mask, poll_driver_mask);
2585 static void hotkey_poll_setup_safe(const bool may_warn)
2587 mutex_lock(&hotkey_mutex);
2588 hotkey_poll_setup(may_warn);
2589 mutex_unlock(&hotkey_mutex);
2592 /* call with hotkey_mutex held */
2593 static void hotkey_poll_set_freq(unsigned int freq)
2595 if (!freq)
2596 hotkey_poll_stop_sync();
2598 hotkey_poll_freq = freq;
2601 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603 static void hotkey_poll_setup(const bool __unused)
2607 static void hotkey_poll_setup_safe(const bool __unused)
2611 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2613 static int hotkey_inputdev_open(struct input_dev *dev)
2615 switch (tpacpi_lifecycle) {
2616 case TPACPI_LIFE_INIT:
2617 case TPACPI_LIFE_RUNNING:
2618 hotkey_poll_setup_safe(false);
2619 return 0;
2620 case TPACPI_LIFE_EXITING:
2621 return -EBUSY;
2624 /* Should only happen if tpacpi_lifecycle is corrupt */
2625 BUG();
2626 return -EBUSY;
2629 static void hotkey_inputdev_close(struct input_dev *dev)
2631 /* disable hotkey polling when possible */
2632 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2633 !(hotkey_source_mask & hotkey_driver_mask))
2634 hotkey_poll_setup_safe(false);
2637 /* sysfs hotkey enable ------------------------------------------------- */
2638 static ssize_t hotkey_enable_show(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2642 int res, status;
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkey reporting is always enabled");
2647 res = hotkey_status_get(&status);
2648 if (res)
2649 return res;
2651 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2654 static ssize_t hotkey_enable_store(struct device *dev,
2655 struct device_attribute *attr,
2656 const char *buf, size_t count)
2658 unsigned long t;
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkeys can be disabled through hotkey_mask");
2663 if (parse_strtoul(buf, 1, &t))
2664 return -EINVAL;
2666 if (t == 0)
2667 return -EPERM;
2669 return count;
2672 static struct device_attribute dev_attr_hotkey_enable =
2673 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2674 hotkey_enable_show, hotkey_enable_store);
2676 /* sysfs hotkey mask --------------------------------------------------- */
2677 static ssize_t hotkey_mask_show(struct device *dev,
2678 struct device_attribute *attr,
2679 char *buf)
2681 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2684 static ssize_t hotkey_mask_store(struct device *dev,
2685 struct device_attribute *attr,
2686 const char *buf, size_t count)
2688 unsigned long t;
2689 int res;
2691 if (parse_strtoul(buf, 0xffffffffUL, &t))
2692 return -EINVAL;
2694 if (mutex_lock_killable(&hotkey_mutex))
2695 return -ERESTARTSYS;
2697 res = hotkey_user_mask_set(t);
2699 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2700 hotkey_poll_setup(true);
2701 #endif
2703 mutex_unlock(&hotkey_mutex);
2705 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707 return (res) ? res : count;
2710 static struct device_attribute dev_attr_hotkey_mask =
2711 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2712 hotkey_mask_show, hotkey_mask_store);
2714 /* sysfs hotkey bios_enabled ------------------------------------------- */
2715 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2719 return sprintf(buf, "0\n");
2722 static struct device_attribute dev_attr_hotkey_bios_enabled =
2723 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2725 /* sysfs hotkey bios_mask ---------------------------------------------- */
2726 static ssize_t hotkey_bios_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2730 printk_deprecated_attribute("hotkey_bios_mask",
2731 "This attribute is useless.");
2732 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2735 static struct device_attribute dev_attr_hotkey_bios_mask =
2736 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2738 /* sysfs hotkey all_mask ----------------------------------------------- */
2739 static ssize_t hotkey_all_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2743 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2744 hotkey_all_mask | hotkey_source_mask);
2747 static struct device_attribute dev_attr_hotkey_all_mask =
2748 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750 /* sysfs hotkey recommended_mask --------------------------------------- */
2751 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2756 (hotkey_all_mask | hotkey_source_mask)
2757 & ~hotkey_reserved_mask);
2760 static struct device_attribute dev_attr_hotkey_recommended_mask =
2761 __ATTR(hotkey_recommended_mask, S_IRUGO,
2762 hotkey_recommended_mask_show, NULL);
2764 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2767 static ssize_t hotkey_source_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2774 static ssize_t hotkey_source_mask_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2778 unsigned long t;
2779 u32 r_ev;
2780 int rc;
2782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2784 return -EINVAL;
2786 if (mutex_lock_killable(&hotkey_mutex))
2787 return -ERESTARTSYS;
2789 HOTKEY_CONFIG_CRITICAL_START
2790 hotkey_source_mask = t;
2791 HOTKEY_CONFIG_CRITICAL_END
2793 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2794 ~hotkey_source_mask);
2795 hotkey_poll_setup(true);
2797 /* check if events needed by the driver got disabled */
2798 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2799 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2801 mutex_unlock(&hotkey_mutex);
2803 if (rc < 0)
2804 pr_err("hotkey_source_mask: "
2805 "failed to update the firmware event mask!\n");
2807 if (r_ev)
2808 pr_notice("hotkey_source_mask: "
2809 "some important events were disabled: 0x%04x\n",
2810 r_ev);
2812 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814 return (rc < 0) ? rc : count;
2817 static struct device_attribute dev_attr_hotkey_source_mask =
2818 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2819 hotkey_source_mask_show, hotkey_source_mask_store);
2821 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2822 static ssize_t hotkey_poll_freq_show(struct device *dev,
2823 struct device_attribute *attr,
2824 char *buf)
2826 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2829 static ssize_t hotkey_poll_freq_store(struct device *dev,
2830 struct device_attribute *attr,
2831 const char *buf, size_t count)
2833 unsigned long t;
2835 if (parse_strtoul(buf, 25, &t))
2836 return -EINVAL;
2838 if (mutex_lock_killable(&hotkey_mutex))
2839 return -ERESTARTSYS;
2841 hotkey_poll_set_freq(t);
2842 hotkey_poll_setup(true);
2844 mutex_unlock(&hotkey_mutex);
2846 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848 return count;
2851 static struct device_attribute dev_attr_hotkey_poll_freq =
2852 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2853 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2858 static ssize_t hotkey_radio_sw_show(struct device *dev,
2859 struct device_attribute *attr,
2860 char *buf)
2862 int res;
2863 res = hotkey_get_wlsw();
2864 if (res < 0)
2865 return res;
2867 /* Opportunistic update */
2868 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870 return snprintf(buf, PAGE_SIZE, "%d\n",
2871 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2874 static struct device_attribute dev_attr_hotkey_radio_sw =
2875 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877 static void hotkey_radio_sw_notify_change(void)
2879 if (tp_features.hotkey_wlsw)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_radio_sw");
2884 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2885 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2889 int res, s;
2890 res = hotkey_get_tablet_mode(&s);
2891 if (res < 0)
2892 return res;
2894 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897 static struct device_attribute dev_attr_hotkey_tablet_mode =
2898 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900 static void hotkey_tablet_mode_notify_change(void)
2902 if (tp_features.hotkey_tablet)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_tablet_mode");
2907 /* sysfs hotkey report_mode -------------------------------------------- */
2908 static ssize_t hotkey_report_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2912 return snprintf(buf, PAGE_SIZE, "%d\n",
2913 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2916 static struct device_attribute dev_attr_hotkey_report_mode =
2917 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919 /* sysfs wakeup reason (pollable) -------------------------------------- */
2920 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2927 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2928 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930 static void hotkey_wakeup_reason_notify_change(void)
2932 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2933 "wakeup_reason");
2936 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2937 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2938 struct device_attribute *attr,
2939 char *buf)
2941 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2944 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2945 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2946 hotkey_wakeup_hotunplug_complete_show, NULL);
2948 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2950 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2951 "wakeup_hotunplug_complete");
2954 /* --------------------------------------------------------------------- */
2956 static struct attribute *hotkey_attributes[] __initdata = {
2957 &dev_attr_hotkey_enable.attr,
2958 &dev_attr_hotkey_bios_enabled.attr,
2959 &dev_attr_hotkey_bios_mask.attr,
2960 &dev_attr_hotkey_report_mode.attr,
2961 &dev_attr_hotkey_wakeup_reason.attr,
2962 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2963 &dev_attr_hotkey_mask.attr,
2964 &dev_attr_hotkey_all_mask.attr,
2965 &dev_attr_hotkey_recommended_mask.attr,
2966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2969 #endif
2973 * Sync both the hw and sw blocking state of all switches
2975 static void tpacpi_send_radiosw_update(void)
2977 int wlsw;
2980 * We must sync all rfkill controllers *before* issuing any
2981 * rfkill input events, or we will race the rfkill core input
2982 * handler.
2984 * tpacpi_inputdev_send_mutex works as a synchronization point
2985 * for the above.
2987 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2990 wlsw = hotkey_get_wlsw();
2992 /* Sync hw blocking state first if it is hw-blocked */
2993 if (wlsw == TPACPI_RFK_RADIO_OFF)
2994 tpacpi_rfk_update_hwblock_state(true);
2996 /* Sync sw blocking state */
2997 tpacpi_rfk_update_swstate_all();
2999 /* Sync hw blocking state last if it is hw-unblocked */
3000 if (wlsw == TPACPI_RFK_RADIO_ON)
3001 tpacpi_rfk_update_hwblock_state(false);
3003 /* Issue rfkill input event for WLSW switch */
3004 if (!(wlsw < 0)) {
3005 mutex_lock(&tpacpi_inputdev_send_mutex);
3007 input_report_switch(tpacpi_inputdev,
3008 SW_RFKILL_ALL, (wlsw > 0));
3009 input_sync(tpacpi_inputdev);
3011 mutex_unlock(&tpacpi_inputdev_send_mutex);
3015 * this can be unconditional, as we will poll state again
3016 * if userspace uses the notify to read data
3018 hotkey_radio_sw_notify_change();
3021 static void hotkey_exit(void)
3023 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3024 mutex_lock(&hotkey_mutex);
3025 hotkey_poll_stop_sync();
3026 mutex_unlock(&hotkey_mutex);
3027 #endif
3029 if (hotkey_dev_attributes)
3030 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032 kfree(hotkey_keycode_map);
3034 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3035 "restoring original HKEY status and mask\n");
3036 /* yes, there is a bitwise or below, we want the
3037 * functions to be called even if one of them fail */
3038 if (((tp_features.hotkey_mask &&
3039 hotkey_mask_set(hotkey_orig_mask)) |
3040 hotkey_status_set(false)) != 0)
3041 pr_err("failed to restore hot key mask "
3042 "to BIOS defaults\n");
3045 static void __init hotkey_unmap(const unsigned int scancode)
3047 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3048 clear_bit(hotkey_keycode_map[scancode],
3049 tpacpi_inputdev->keybit);
3050 hotkey_keycode_map[scancode] = KEY_RESERVED;
3055 * HKEY quirks:
3056 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3059 #define TPACPI_HK_Q_INIMASK 0x0001
3061 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3062 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3063 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3064 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3065 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3066 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3067 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3068 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3069 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3070 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3071 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3072 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3073 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3074 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3075 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3076 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3077 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3078 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3079 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3080 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3083 typedef u16 tpacpi_keymap_entry_t;
3084 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3086 static int __init hotkey_init(struct ibm_init_struct *iibm)
3088 /* Requirements for changing the default keymaps:
3090 * 1. Many of the keys are mapped to KEY_RESERVED for very
3091 * good reasons. Do not change them unless you have deep
3092 * knowledge on the IBM and Lenovo ThinkPad firmware for
3093 * the various ThinkPad models. The driver behaves
3094 * differently for KEY_RESERVED: such keys have their
3095 * hot key mask *unset* in mask_recommended, and also
3096 * in the initial hot key mask programmed into the
3097 * firmware at driver load time, which means the firm-
3098 * ware may react very differently if you change them to
3099 * something else;
3101 * 2. You must be subscribed to the linux-thinkpad and
3102 * ibm-acpi-devel mailing lists, and you should read the
3103 * list archives since 2007 if you want to change the
3104 * keymaps. This requirement exists so that you will
3105 * know the past history of problems with the thinkpad-
3106 * acpi driver keymaps, and also that you will be
3107 * listening to any bug reports;
3109 * 3. Do not send thinkpad-acpi specific patches directly to
3110 * for merging, *ever*. Send them to the linux-acpi
3111 * mailinglist for comments. Merging is to be done only
3112 * through acpi-test and the ACPI maintainer.
3114 * If the above is too much to ask, don't change the keymap.
3115 * Ask the thinkpad-acpi maintainer to do it, instead.
3118 enum keymap_index {
3119 TPACPI_KEYMAP_IBM_GENERIC = 0,
3120 TPACPI_KEYMAP_LENOVO_GENERIC,
3123 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3124 /* Generic keymap for IBM ThinkPads */
3125 [TPACPI_KEYMAP_IBM_GENERIC] = {
3126 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3127 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3128 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3129 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3131 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3132 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3133 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3134 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3136 /* brightness: firmware always reacts to them */
3137 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3138 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3140 /* Thinklight: firmware always react to it */
3141 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3143 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3144 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3146 /* Volume: firmware always react to it and reprograms
3147 * the built-in *extra* mixer. Never map it to control
3148 * another mixer by default. */
3149 KEY_RESERVED, /* 0x14: VOLUME UP */
3150 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3151 KEY_RESERVED, /* 0x16: MUTE */
3153 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3155 /* (assignments unknown, please report if found) */
3156 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3157 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3160 /* Generic keymap for Lenovo ThinkPads */
3161 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3162 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3163 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3164 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3165 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3167 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3168 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3169 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3170 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3172 /* These should be enabled --only-- when ACPI video
3173 * is disabled (i.e. in "vendor" mode), and are handled
3174 * in a special way by the init code */
3175 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3176 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3178 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3180 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3181 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3183 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3184 * react to it and reprograms the built-in *extra* mixer.
3185 * Never map it to control another mixer by default.
3187 * T60?, T61, R60?, R61: firmware and EC tries to send
3188 * these over the regular keyboard, so these are no-ops,
3189 * but there are still weird bugs re. MUTE, so do not
3190 * change unless you get test reports from all Lenovo
3191 * models. May cause the BIOS to interfere with the
3192 * HDA mixer.
3194 KEY_RESERVED, /* 0x14: VOLUME UP */
3195 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3196 KEY_RESERVED, /* 0x16: MUTE */
3198 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3200 /* (assignments unknown, please report if found) */
3201 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3202 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3206 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3207 /* Generic maps (fallback) */
3209 .vendor = PCI_VENDOR_ID_IBM,
3210 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3211 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3214 .vendor = PCI_VENDOR_ID_LENOVO,
3215 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3216 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3220 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3221 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3223 int res, i;
3224 int status;
3225 int hkeyv;
3226 bool radiosw_state = false;
3227 bool tabletsw_state = false;
3229 unsigned long quirks;
3230 unsigned long keymap_id;
3232 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3233 "initializing hotkey subdriver\n");
3235 BUG_ON(!tpacpi_inputdev);
3236 BUG_ON(tpacpi_inputdev->open != NULL ||
3237 tpacpi_inputdev->close != NULL);
3239 TPACPI_ACPIHANDLE_INIT(hkey);
3240 mutex_init(&hotkey_mutex);
3242 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3243 mutex_init(&hotkey_thread_mutex);
3244 mutex_init(&hotkey_thread_data_mutex);
3245 #endif
3247 /* hotkey not supported on 570 */
3248 tp_features.hotkey = hkey_handle != NULL;
3250 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3251 "hotkeys are %s\n",
3252 str_supported(tp_features.hotkey));
3254 if (!tp_features.hotkey)
3255 return 1;
3257 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3258 ARRAY_SIZE(tpacpi_hotkey_qtable));
3260 tpacpi_disable_brightness_delay();
3262 /* MUST have enough space for all attributes to be added to
3263 * hotkey_dev_attributes */
3264 hotkey_dev_attributes = create_attr_set(
3265 ARRAY_SIZE(hotkey_attributes) + 2,
3266 NULL);
3267 if (!hotkey_dev_attributes)
3268 return -ENOMEM;
3269 res = add_many_to_attr_set(hotkey_dev_attributes,
3270 hotkey_attributes,
3271 ARRAY_SIZE(hotkey_attributes));
3272 if (res)
3273 goto err_exit;
3275 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3276 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3277 for HKEY interface version 0x100 */
3278 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3279 if ((hkeyv >> 8) != 1) {
3280 pr_err("unknown version of the HKEY interface: 0x%x\n",
3281 hkeyv);
3282 pr_err("please report this to %s\n", TPACPI_MAIL);
3283 } else {
3285 * MHKV 0x100 in A31, R40, R40e,
3286 * T4x, X31, and later
3288 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3289 "firmware HKEY interface version: 0x%x\n",
3290 hkeyv);
3292 /* Paranoia check AND init hotkey_all_mask */
3293 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3294 "MHKA", "qd")) {
3295 pr_err("missing MHKA handler, "
3296 "please report this to %s\n",
3297 TPACPI_MAIL);
3298 /* Fallback: pre-init for FN+F3,F4,F12 */
3299 hotkey_all_mask = 0x080cU;
3300 } else {
3301 tp_features.hotkey_mask = 1;
3306 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3307 "hotkey masks are %s\n",
3308 str_supported(tp_features.hotkey_mask));
3310 /* Init hotkey_all_mask if not initialized yet */
3311 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3312 (quirks & TPACPI_HK_Q_INIMASK))
3313 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3315 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3316 if (tp_features.hotkey_mask) {
3317 /* hotkey_source_mask *must* be zero for
3318 * the first hotkey_mask_get to return hotkey_orig_mask */
3319 res = hotkey_mask_get();
3320 if (res)
3321 goto err_exit;
3323 hotkey_orig_mask = hotkey_acpi_mask;
3324 } else {
3325 hotkey_orig_mask = hotkey_all_mask;
3326 hotkey_acpi_mask = hotkey_all_mask;
3329 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3330 if (dbg_wlswemul) {
3331 tp_features.hotkey_wlsw = 1;
3332 radiosw_state = !!tpacpi_wlsw_emulstate;
3333 pr_info("radio switch emulation enabled\n");
3334 } else
3335 #endif
3336 /* Not all thinkpads have a hardware radio switch */
3337 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3338 tp_features.hotkey_wlsw = 1;
3339 radiosw_state = !!status;
3340 pr_info("radio switch found; radios are %s\n",
3341 enabled(status, 0));
3343 if (tp_features.hotkey_wlsw)
3344 res = add_to_attr_set(hotkey_dev_attributes,
3345 &dev_attr_hotkey_radio_sw.attr);
3347 /* For X41t, X60t, X61t Tablets... */
3348 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3349 tp_features.hotkey_tablet = 1;
3350 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3351 pr_info("possible tablet mode switch found; "
3352 "ThinkPad in %s mode\n",
3353 (tabletsw_state) ? "tablet" : "laptop");
3354 res = add_to_attr_set(hotkey_dev_attributes,
3355 &dev_attr_hotkey_tablet_mode.attr);
3358 if (!res)
3359 res = register_attr_set_with_sysfs(
3360 hotkey_dev_attributes,
3361 &tpacpi_pdev->dev.kobj);
3362 if (res)
3363 goto err_exit;
3365 /* Set up key map */
3366 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3367 GFP_KERNEL);
3368 if (!hotkey_keycode_map) {
3369 pr_err("failed to allocate memory for key map\n");
3370 res = -ENOMEM;
3371 goto err_exit;
3374 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3375 ARRAY_SIZE(tpacpi_keymap_qtable));
3376 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3377 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3378 "using keymap number %lu\n", keymap_id);
3380 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3381 TPACPI_HOTKEY_MAP_SIZE);
3383 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3384 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3385 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3386 tpacpi_inputdev->keycode = hotkey_keycode_map;
3387 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3388 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3389 input_set_capability(tpacpi_inputdev, EV_KEY,
3390 hotkey_keycode_map[i]);
3391 } else {
3392 if (i < sizeof(hotkey_reserved_mask)*8)
3393 hotkey_reserved_mask |= 1 << i;
3397 if (tp_features.hotkey_wlsw) {
3398 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3399 input_report_switch(tpacpi_inputdev,
3400 SW_RFKILL_ALL, radiosw_state);
3402 if (tp_features.hotkey_tablet) {
3403 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3404 input_report_switch(tpacpi_inputdev,
3405 SW_TABLET_MODE, tabletsw_state);
3408 /* Do not issue duplicate brightness change events to
3409 * userspace. tpacpi_detect_brightness_capabilities() must have
3410 * been called before this point */
3411 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3412 pr_info("This ThinkPad has standard ACPI backlight "
3413 "brightness control, supported by the ACPI "
3414 "video driver\n");
3415 pr_notice("Disabling thinkpad-acpi brightness events "
3416 "by default...\n");
3418 /* Disable brightness up/down on Lenovo thinkpads when
3419 * ACPI is handling them, otherwise it is plain impossible
3420 * for userspace to do something even remotely sane */
3421 hotkey_reserved_mask |=
3422 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3423 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3424 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3425 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3428 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3429 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3430 & ~hotkey_all_mask
3431 & ~hotkey_reserved_mask;
3433 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3434 "hotkey source mask 0x%08x, polling freq %u\n",
3435 hotkey_source_mask, hotkey_poll_freq);
3436 #endif
3438 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3439 "enabling firmware HKEY event interface...\n");
3440 res = hotkey_status_set(true);
3441 if (res) {
3442 hotkey_exit();
3443 return res;
3445 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3446 | hotkey_driver_mask)
3447 & ~hotkey_source_mask);
3448 if (res < 0 && res != -ENXIO) {
3449 hotkey_exit();
3450 return res;
3452 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3453 & ~hotkey_reserved_mask;
3454 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3455 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3456 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3458 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3459 "legacy ibm/hotkey event reporting over procfs %s\n",
3460 (hotkey_report_mode < 2) ?
3461 "enabled" : "disabled");
3463 tpacpi_inputdev->open = &hotkey_inputdev_open;
3464 tpacpi_inputdev->close = &hotkey_inputdev_close;
3466 hotkey_poll_setup_safe(true);
3468 return 0;
3470 err_exit:
3471 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3472 hotkey_dev_attributes = NULL;
3474 return (res < 0)? res : 1;
3477 static bool hotkey_notify_hotkey(const u32 hkey,
3478 bool *send_acpi_ev,
3479 bool *ignore_acpi_ev)
3481 /* 0x1000-0x1FFF: key presses */
3482 unsigned int scancode = hkey & 0xfff;
3483 *send_acpi_ev = true;
3484 *ignore_acpi_ev = false;
3486 /* HKEY event 0x1001 is scancode 0x00 */
3487 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3488 scancode--;
3489 if (!(hotkey_source_mask & (1 << scancode))) {
3490 tpacpi_input_send_key_masked(scancode);
3491 *send_acpi_ev = false;
3492 } else {
3493 *ignore_acpi_ev = true;
3495 return true;
3497 return false;
3500 static bool hotkey_notify_wakeup(const u32 hkey,
3501 bool *send_acpi_ev,
3502 bool *ignore_acpi_ev)
3504 /* 0x2000-0x2FFF: Wakeup reason */
3505 *send_acpi_ev = true;
3506 *ignore_acpi_ev = false;
3508 switch (hkey) {
3509 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3510 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3511 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3512 *ignore_acpi_ev = true;
3513 break;
3515 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3516 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3517 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3518 *ignore_acpi_ev = true;
3519 break;
3521 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3522 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3523 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3524 /* how to auto-heal: */
3525 /* 2313: woke up from S3, go to S4/S5 */
3526 /* 2413: woke up from S4, go to S5 */
3527 break;
3529 default:
3530 return false;
3533 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3534 pr_info("woke up due to a hot-unplug request...\n");
3535 hotkey_wakeup_reason_notify_change();
3537 return true;
3540 static bool hotkey_notify_dockevent(const u32 hkey,
3541 bool *send_acpi_ev,
3542 bool *ignore_acpi_ev)
3544 /* 0x4000-0x4FFF: dock-related events */
3545 *send_acpi_ev = true;
3546 *ignore_acpi_ev = false;
3548 switch (hkey) {
3549 case TP_HKEY_EV_UNDOCK_ACK:
3550 /* ACPI undock operation completed after wakeup */
3551 hotkey_autosleep_ack = 1;
3552 pr_info("undocked\n");
3553 hotkey_wakeup_hotunplug_complete_notify_change();
3554 return true;
3556 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3557 pr_info("docked into hotplug port replicator\n");
3558 return true;
3559 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3560 pr_info("undocked from hotplug port replicator\n");
3561 return true;
3563 default:
3564 return false;
3568 static bool hotkey_notify_usrevent(const u32 hkey,
3569 bool *send_acpi_ev,
3570 bool *ignore_acpi_ev)
3572 /* 0x5000-0x5FFF: human interface helpers */
3573 *send_acpi_ev = true;
3574 *ignore_acpi_ev = false;
3576 switch (hkey) {
3577 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3578 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3579 return true;
3581 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3582 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3583 tpacpi_input_send_tabletsw();
3584 hotkey_tablet_mode_notify_change();
3585 *send_acpi_ev = false;
3586 return true;
3588 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3589 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3590 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3591 /* do not propagate these events */
3592 *ignore_acpi_ev = true;
3593 return true;
3595 default:
3596 return false;
3600 static void thermal_dump_all_sensors(void);
3602 static bool hotkey_notify_6xxx(const u32 hkey,
3603 bool *send_acpi_ev,
3604 bool *ignore_acpi_ev)
3606 bool known = true;
3608 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3609 *send_acpi_ev = true;
3610 *ignore_acpi_ev = false;
3612 switch (hkey) {
3613 case TP_HKEY_EV_THM_TABLE_CHANGED:
3614 pr_info("EC reports that Thermal Table has changed\n");
3615 /* recommended action: do nothing, we don't have
3616 * Lenovo ATM information */
3617 return true;
3618 case TP_HKEY_EV_ALARM_BAT_HOT:
3619 pr_crit("THERMAL ALARM: battery is too hot!\n");
3620 /* recommended action: warn user through gui */
3621 break;
3622 case TP_HKEY_EV_ALARM_BAT_XHOT:
3623 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3624 /* recommended action: immediate sleep/hibernate */
3625 break;
3626 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3627 pr_crit("THERMAL ALARM: "
3628 "a sensor reports something is too hot!\n");
3629 /* recommended action: warn user through gui, that */
3630 /* some internal component is too hot */
3631 break;
3632 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3633 pr_alert("THERMAL EMERGENCY: "
3634 "a sensor reports something is extremely hot!\n");
3635 /* recommended action: immediate sleep/hibernate */
3636 break;
3638 case TP_HKEY_EV_KEY_NUMLOCK:
3639 case TP_HKEY_EV_KEY_FN:
3640 /* key press events, we just ignore them as long as the EC
3641 * is still reporting them in the normal keyboard stream */
3642 *send_acpi_ev = false;
3643 *ignore_acpi_ev = true;
3644 return true;
3646 default:
3647 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3648 known = false;
3651 thermal_dump_all_sensors();
3653 return known;
3656 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3658 u32 hkey;
3659 bool send_acpi_ev;
3660 bool ignore_acpi_ev;
3661 bool known_ev;
3663 if (event != 0x80) {
3664 pr_err("unknown HKEY notification event %d\n", event);
3665 /* forward it to userspace, maybe it knows how to handle it */
3666 acpi_bus_generate_netlink_event(
3667 ibm->acpi->device->pnp.device_class,
3668 dev_name(&ibm->acpi->device->dev),
3669 event, 0);
3670 return;
3673 while (1) {
3674 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3675 pr_err("failed to retrieve HKEY event\n");
3676 return;
3679 if (hkey == 0) {
3680 /* queue empty */
3681 return;
3684 send_acpi_ev = true;
3685 ignore_acpi_ev = false;
3687 switch (hkey >> 12) {
3688 case 1:
3689 /* 0x1000-0x1FFF: key presses */
3690 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
3692 break;
3693 case 2:
3694 /* 0x2000-0x2FFF: Wakeup reason */
3695 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3696 &ignore_acpi_ev);
3697 break;
3698 case 3:
3699 /* 0x3000-0x3FFF: bay-related wakeups */
3700 switch (hkey) {
3701 case TP_HKEY_EV_BAYEJ_ACK:
3702 hotkey_autosleep_ack = 1;
3703 pr_info("bay ejected\n");
3704 hotkey_wakeup_hotunplug_complete_notify_change();
3705 known_ev = true;
3706 break;
3707 case TP_HKEY_EV_OPTDRV_EJ:
3708 /* FIXME: kick libata if SATA link offline */
3709 known_ev = true;
3710 break;
3711 default:
3712 known_ev = false;
3714 break;
3715 case 4:
3716 /* 0x4000-0x4FFF: dock-related events */
3717 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3718 &ignore_acpi_ev);
3719 break;
3720 case 5:
3721 /* 0x5000-0x5FFF: human interface helpers */
3722 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3723 &ignore_acpi_ev);
3724 break;
3725 case 6:
3726 /* 0x6000-0x6FFF: thermal alarms/notices and
3727 * keyboard events */
3728 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3729 &ignore_acpi_ev);
3730 break;
3731 case 7:
3732 /* 0x7000-0x7FFF: misc */
3733 if (tp_features.hotkey_wlsw &&
3734 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3735 tpacpi_send_radiosw_update();
3736 send_acpi_ev = 0;
3737 known_ev = true;
3738 break;
3740 /* fallthrough to default */
3741 default:
3742 known_ev = false;
3744 if (!known_ev) {
3745 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3746 pr_notice("please report the conditions when this "
3747 "event happened to %s\n", TPACPI_MAIL);
3750 /* Legacy events */
3751 if (!ignore_acpi_ev &&
3752 (send_acpi_ev || hotkey_report_mode < 2)) {
3753 acpi_bus_generate_proc_event(ibm->acpi->device,
3754 event, hkey);
3757 /* netlink events */
3758 if (!ignore_acpi_ev && send_acpi_ev) {
3759 acpi_bus_generate_netlink_event(
3760 ibm->acpi->device->pnp.device_class,
3761 dev_name(&ibm->acpi->device->dev),
3762 event, hkey);
3767 static void hotkey_suspend(pm_message_t state)
3769 /* Do these on suspend, we get the events on early resume! */
3770 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3771 hotkey_autosleep_ack = 0;
3774 static void hotkey_resume(void)
3776 tpacpi_disable_brightness_delay();
3778 if (hotkey_status_set(true) < 0 ||
3779 hotkey_mask_set(hotkey_acpi_mask) < 0)
3780 pr_err("error while attempting to reset the event "
3781 "firmware interface\n");
3783 tpacpi_send_radiosw_update();
3784 hotkey_tablet_mode_notify_change();
3785 hotkey_wakeup_reason_notify_change();
3786 hotkey_wakeup_hotunplug_complete_notify_change();
3787 hotkey_poll_setup_safe(false);
3790 /* procfs -------------------------------------------------------------- */
3791 static int hotkey_read(struct seq_file *m)
3793 int res, status;
3795 if (!tp_features.hotkey) {
3796 seq_printf(m, "status:\t\tnot supported\n");
3797 return 0;
3800 if (mutex_lock_killable(&hotkey_mutex))
3801 return -ERESTARTSYS;
3802 res = hotkey_status_get(&status);
3803 if (!res)
3804 res = hotkey_mask_get();
3805 mutex_unlock(&hotkey_mutex);
3806 if (res)
3807 return res;
3809 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3810 if (hotkey_all_mask) {
3811 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3812 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3813 } else {
3814 seq_printf(m, "mask:\t\tnot supported\n");
3815 seq_printf(m, "commands:\tenable, disable, reset\n");
3818 return 0;
3821 static void hotkey_enabledisable_warn(bool enable)
3823 tpacpi_log_usertask("procfs hotkey enable/disable");
3824 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3825 pr_fmt("hotkey enable/disable functionality has been "
3826 "removed from the driver. "
3827 "Hotkeys are always enabled.\n")))
3828 pr_err("Please remove the hotkey=enable module "
3829 "parameter, it is deprecated. "
3830 "Hotkeys are always enabled.\n");
3833 static int hotkey_write(char *buf)
3835 int res;
3836 u32 mask;
3837 char *cmd;
3839 if (!tp_features.hotkey)
3840 return -ENODEV;
3842 if (mutex_lock_killable(&hotkey_mutex))
3843 return -ERESTARTSYS;
3845 mask = hotkey_user_mask;
3847 res = 0;
3848 while ((cmd = next_cmd(&buf))) {
3849 if (strlencmp(cmd, "enable") == 0) {
3850 hotkey_enabledisable_warn(1);
3851 } else if (strlencmp(cmd, "disable") == 0) {
3852 hotkey_enabledisable_warn(0);
3853 res = -EPERM;
3854 } else if (strlencmp(cmd, "reset") == 0) {
3855 mask = (hotkey_all_mask | hotkey_source_mask)
3856 & ~hotkey_reserved_mask;
3857 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3858 /* mask set */
3859 } else if (sscanf(cmd, "%x", &mask) == 1) {
3860 /* mask set */
3861 } else {
3862 res = -EINVAL;
3863 goto errexit;
3867 if (!res) {
3868 tpacpi_disclose_usertask("procfs hotkey",
3869 "set mask to 0x%08x\n", mask);
3870 res = hotkey_user_mask_set(mask);
3873 errexit:
3874 mutex_unlock(&hotkey_mutex);
3875 return res;
3878 static const struct acpi_device_id ibm_htk_device_ids[] = {
3879 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3880 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3881 {"", 0},
3884 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3885 .hid = ibm_htk_device_ids,
3886 .notify = hotkey_notify,
3887 .handle = &hkey_handle,
3888 .type = ACPI_DEVICE_NOTIFY,
3891 static struct ibm_struct hotkey_driver_data = {
3892 .name = "hotkey",
3893 .read = hotkey_read,
3894 .write = hotkey_write,
3895 .exit = hotkey_exit,
3896 .resume = hotkey_resume,
3897 .suspend = hotkey_suspend,
3898 .acpi = &ibm_hotkey_acpidriver,
3901 /*************************************************************************
3902 * Bluetooth subdriver
3905 enum {
3906 /* ACPI GBDC/SBDC bits */
3907 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3908 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3909 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3910 0 = disable, 1 = enable */
3913 enum {
3914 /* ACPI \BLTH commands */
3915 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3916 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3917 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3918 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3919 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3922 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3924 static int bluetooth_get_status(void)
3926 int status;
3928 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3929 if (dbg_bluetoothemul)
3930 return (tpacpi_bluetooth_emulstate) ?
3931 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3932 #endif
3934 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3935 return -EIO;
3937 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3938 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3941 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3943 int status;
3945 vdbg_printk(TPACPI_DBG_RFKILL,
3946 "will attempt to %s bluetooth\n",
3947 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3949 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3950 if (dbg_bluetoothemul) {
3951 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3952 return 0;
3954 #endif
3956 if (state == TPACPI_RFK_RADIO_ON)
3957 status = TP_ACPI_BLUETOOTH_RADIOSSW
3958 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3959 else
3960 status = 0;
3962 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3963 return -EIO;
3965 return 0;
3968 /* sysfs bluetooth enable ---------------------------------------------- */
3969 static ssize_t bluetooth_enable_show(struct device *dev,
3970 struct device_attribute *attr,
3971 char *buf)
3973 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3974 attr, buf);
3977 static ssize_t bluetooth_enable_store(struct device *dev,
3978 struct device_attribute *attr,
3979 const char *buf, size_t count)
3981 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3982 attr, buf, count);
3985 static struct device_attribute dev_attr_bluetooth_enable =
3986 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3987 bluetooth_enable_show, bluetooth_enable_store);
3989 /* --------------------------------------------------------------------- */
3991 static struct attribute *bluetooth_attributes[] = {
3992 &dev_attr_bluetooth_enable.attr,
3993 NULL
3996 static const struct attribute_group bluetooth_attr_group = {
3997 .attrs = bluetooth_attributes,
4000 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4001 .get_status = bluetooth_get_status,
4002 .set_status = bluetooth_set_status,
4005 static void bluetooth_shutdown(void)
4007 /* Order firmware to save current state to NVRAM */
4008 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4009 TP_ACPI_BLTH_SAVE_STATE))
4010 pr_notice("failed to save bluetooth state to NVRAM\n");
4011 else
4012 vdbg_printk(TPACPI_DBG_RFKILL,
4013 "bluestooth state saved to NVRAM\n");
4016 static void bluetooth_exit(void)
4018 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4019 &bluetooth_attr_group);
4021 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4023 bluetooth_shutdown();
4026 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4028 int res;
4029 int status = 0;
4031 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4032 "initializing bluetooth subdriver\n");
4034 TPACPI_ACPIHANDLE_INIT(hkey);
4036 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4037 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4038 tp_features.bluetooth = hkey_handle &&
4039 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4041 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4042 "bluetooth is %s, status 0x%02x\n",
4043 str_supported(tp_features.bluetooth),
4044 status);
4046 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4047 if (dbg_bluetoothemul) {
4048 tp_features.bluetooth = 1;
4049 pr_info("bluetooth switch emulation enabled\n");
4050 } else
4051 #endif
4052 if (tp_features.bluetooth &&
4053 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4054 /* no bluetooth hardware present in system */
4055 tp_features.bluetooth = 0;
4056 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4057 "bluetooth hardware not installed\n");
4060 if (!tp_features.bluetooth)
4061 return 1;
4063 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4064 &bluetooth_tprfk_ops,
4065 RFKILL_TYPE_BLUETOOTH,
4066 TPACPI_RFK_BLUETOOTH_SW_NAME,
4067 true);
4068 if (res)
4069 return res;
4071 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4072 &bluetooth_attr_group);
4073 if (res) {
4074 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4075 return res;
4078 return 0;
4081 /* procfs -------------------------------------------------------------- */
4082 static int bluetooth_read(struct seq_file *m)
4084 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4087 static int bluetooth_write(char *buf)
4089 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4092 static struct ibm_struct bluetooth_driver_data = {
4093 .name = "bluetooth",
4094 .read = bluetooth_read,
4095 .write = bluetooth_write,
4096 .exit = bluetooth_exit,
4097 .shutdown = bluetooth_shutdown,
4100 /*************************************************************************
4101 * Wan subdriver
4104 enum {
4105 /* ACPI GWAN/SWAN bits */
4106 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4107 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4108 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4109 0 = disable, 1 = enable */
4112 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4114 static int wan_get_status(void)
4116 int status;
4118 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4119 if (dbg_wwanemul)
4120 return (tpacpi_wwan_emulstate) ?
4121 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4122 #endif
4124 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4125 return -EIO;
4127 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4128 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4131 static int wan_set_status(enum tpacpi_rfkill_state state)
4133 int status;
4135 vdbg_printk(TPACPI_DBG_RFKILL,
4136 "will attempt to %s wwan\n",
4137 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4139 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4140 if (dbg_wwanemul) {
4141 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4142 return 0;
4144 #endif
4146 if (state == TPACPI_RFK_RADIO_ON)
4147 status = TP_ACPI_WANCARD_RADIOSSW
4148 | TP_ACPI_WANCARD_RESUMECTRL;
4149 else
4150 status = 0;
4152 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4153 return -EIO;
4155 return 0;
4158 /* sysfs wan enable ---------------------------------------------------- */
4159 static ssize_t wan_enable_show(struct device *dev,
4160 struct device_attribute *attr,
4161 char *buf)
4163 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4164 attr, buf);
4167 static ssize_t wan_enable_store(struct device *dev,
4168 struct device_attribute *attr,
4169 const char *buf, size_t count)
4171 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4172 attr, buf, count);
4175 static struct device_attribute dev_attr_wan_enable =
4176 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4177 wan_enable_show, wan_enable_store);
4179 /* --------------------------------------------------------------------- */
4181 static struct attribute *wan_attributes[] = {
4182 &dev_attr_wan_enable.attr,
4183 NULL
4186 static const struct attribute_group wan_attr_group = {
4187 .attrs = wan_attributes,
4190 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4191 .get_status = wan_get_status,
4192 .set_status = wan_set_status,
4195 static void wan_shutdown(void)
4197 /* Order firmware to save current state to NVRAM */
4198 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4199 TP_ACPI_WGSV_SAVE_STATE))
4200 pr_notice("failed to save WWAN state to NVRAM\n");
4201 else
4202 vdbg_printk(TPACPI_DBG_RFKILL,
4203 "WWAN state saved to NVRAM\n");
4206 static void wan_exit(void)
4208 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4209 &wan_attr_group);
4211 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4213 wan_shutdown();
4216 static int __init wan_init(struct ibm_init_struct *iibm)
4218 int res;
4219 int status = 0;
4221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4222 "initializing wan subdriver\n");
4224 TPACPI_ACPIHANDLE_INIT(hkey);
4226 tp_features.wan = hkey_handle &&
4227 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4229 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4230 "wan is %s, status 0x%02x\n",
4231 str_supported(tp_features.wan),
4232 status);
4234 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4235 if (dbg_wwanemul) {
4236 tp_features.wan = 1;
4237 pr_info("wwan switch emulation enabled\n");
4238 } else
4239 #endif
4240 if (tp_features.wan &&
4241 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4242 /* no wan hardware present in system */
4243 tp_features.wan = 0;
4244 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4245 "wan hardware not installed\n");
4248 if (!tp_features.wan)
4249 return 1;
4251 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4252 &wan_tprfk_ops,
4253 RFKILL_TYPE_WWAN,
4254 TPACPI_RFK_WWAN_SW_NAME,
4255 true);
4256 if (res)
4257 return res;
4259 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4260 &wan_attr_group);
4262 if (res) {
4263 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4264 return res;
4267 return 0;
4270 /* procfs -------------------------------------------------------------- */
4271 static int wan_read(struct seq_file *m)
4273 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4276 static int wan_write(char *buf)
4278 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4281 static struct ibm_struct wan_driver_data = {
4282 .name = "wan",
4283 .read = wan_read,
4284 .write = wan_write,
4285 .exit = wan_exit,
4286 .shutdown = wan_shutdown,
4289 /*************************************************************************
4290 * UWB subdriver
4293 enum {
4294 /* ACPI GUWB/SUWB bits */
4295 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4296 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4299 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4301 static int uwb_get_status(void)
4303 int status;
4305 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4306 if (dbg_uwbemul)
4307 return (tpacpi_uwb_emulstate) ?
4308 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4309 #endif
4311 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4312 return -EIO;
4314 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4315 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4318 static int uwb_set_status(enum tpacpi_rfkill_state state)
4320 int status;
4322 vdbg_printk(TPACPI_DBG_RFKILL,
4323 "will attempt to %s UWB\n",
4324 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4326 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4327 if (dbg_uwbemul) {
4328 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4329 return 0;
4331 #endif
4333 if (state == TPACPI_RFK_RADIO_ON)
4334 status = TP_ACPI_UWB_RADIOSSW;
4335 else
4336 status = 0;
4338 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4339 return -EIO;
4341 return 0;
4344 /* --------------------------------------------------------------------- */
4346 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4347 .get_status = uwb_get_status,
4348 .set_status = uwb_set_status,
4351 static void uwb_exit(void)
4353 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4356 static int __init uwb_init(struct ibm_init_struct *iibm)
4358 int res;
4359 int status = 0;
4361 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4362 "initializing uwb subdriver\n");
4364 TPACPI_ACPIHANDLE_INIT(hkey);
4366 tp_features.uwb = hkey_handle &&
4367 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4369 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4370 "uwb is %s, status 0x%02x\n",
4371 str_supported(tp_features.uwb),
4372 status);
4374 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4375 if (dbg_uwbemul) {
4376 tp_features.uwb = 1;
4377 pr_info("uwb switch emulation enabled\n");
4378 } else
4379 #endif
4380 if (tp_features.uwb &&
4381 !(status & TP_ACPI_UWB_HWPRESENT)) {
4382 /* no uwb hardware present in system */
4383 tp_features.uwb = 0;
4384 dbg_printk(TPACPI_DBG_INIT,
4385 "uwb hardware not installed\n");
4388 if (!tp_features.uwb)
4389 return 1;
4391 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4392 &uwb_tprfk_ops,
4393 RFKILL_TYPE_UWB,
4394 TPACPI_RFK_UWB_SW_NAME,
4395 false);
4396 return res;
4399 static struct ibm_struct uwb_driver_data = {
4400 .name = "uwb",
4401 .exit = uwb_exit,
4402 .flags.experimental = 1,
4405 /*************************************************************************
4406 * Video subdriver
4409 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4411 enum video_access_mode {
4412 TPACPI_VIDEO_NONE = 0,
4413 TPACPI_VIDEO_570, /* 570 */
4414 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4415 TPACPI_VIDEO_NEW, /* all others */
4418 enum { /* video status flags, based on VIDEO_570 */
4419 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4420 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4421 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4424 enum { /* TPACPI_VIDEO_570 constants */
4425 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4426 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4427 * video_status_flags */
4428 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4429 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4432 static enum video_access_mode video_supported;
4433 static int video_orig_autosw;
4435 static int video_autosw_get(void);
4436 static int video_autosw_set(int enable);
4438 TPACPI_HANDLE(vid, root,
4439 "\\_SB.PCI.AGP.VGA", /* 570 */
4440 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4441 "\\_SB.PCI0.VID0", /* 770e */
4442 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4443 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4444 "\\_SB.PCI0.AGP.VID", /* all others */
4445 ); /* R30, R31 */
4447 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4449 static int __init video_init(struct ibm_init_struct *iibm)
4451 int ivga;
4453 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4455 TPACPI_ACPIHANDLE_INIT(vid);
4456 if (tpacpi_is_ibm())
4457 TPACPI_ACPIHANDLE_INIT(vid2);
4459 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4460 /* G41, assume IVGA doesn't change */
4461 vid_handle = vid2_handle;
4463 if (!vid_handle)
4464 /* video switching not supported on R30, R31 */
4465 video_supported = TPACPI_VIDEO_NONE;
4466 else if (tpacpi_is_ibm() &&
4467 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4468 /* 570 */
4469 video_supported = TPACPI_VIDEO_570;
4470 else if (tpacpi_is_ibm() &&
4471 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4472 /* 600e/x, 770e, 770x */
4473 video_supported = TPACPI_VIDEO_770;
4474 else
4475 /* all others */
4476 video_supported = TPACPI_VIDEO_NEW;
4478 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4479 str_supported(video_supported != TPACPI_VIDEO_NONE),
4480 video_supported);
4482 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4485 static void video_exit(void)
4487 dbg_printk(TPACPI_DBG_EXIT,
4488 "restoring original video autoswitch mode\n");
4489 if (video_autosw_set(video_orig_autosw))
4490 pr_err("error while trying to restore original "
4491 "video autoswitch mode\n");
4494 static int video_outputsw_get(void)
4496 int status = 0;
4497 int i;
4499 switch (video_supported) {
4500 case TPACPI_VIDEO_570:
4501 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4502 TP_ACPI_VIDEO_570_PHSCMD))
4503 return -EIO;
4504 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4505 break;
4506 case TPACPI_VIDEO_770:
4507 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4508 return -EIO;
4509 if (i)
4510 status |= TP_ACPI_VIDEO_S_LCD;
4511 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4512 return -EIO;
4513 if (i)
4514 status |= TP_ACPI_VIDEO_S_CRT;
4515 break;
4516 case TPACPI_VIDEO_NEW:
4517 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4518 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4519 return -EIO;
4520 if (i)
4521 status |= TP_ACPI_VIDEO_S_CRT;
4523 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4524 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4525 return -EIO;
4526 if (i)
4527 status |= TP_ACPI_VIDEO_S_LCD;
4528 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4529 return -EIO;
4530 if (i)
4531 status |= TP_ACPI_VIDEO_S_DVI;
4532 break;
4533 default:
4534 return -ENOSYS;
4537 return status;
4540 static int video_outputsw_set(int status)
4542 int autosw;
4543 int res = 0;
4545 switch (video_supported) {
4546 case TPACPI_VIDEO_570:
4547 res = acpi_evalf(NULL, NULL,
4548 "\\_SB.PHS2", "vdd",
4549 TP_ACPI_VIDEO_570_PHS2CMD,
4550 status | TP_ACPI_VIDEO_570_PHS2SET);
4551 break;
4552 case TPACPI_VIDEO_770:
4553 autosw = video_autosw_get();
4554 if (autosw < 0)
4555 return autosw;
4557 res = video_autosw_set(1);
4558 if (res)
4559 return res;
4560 res = acpi_evalf(vid_handle, NULL,
4561 "ASWT", "vdd", status * 0x100, 0);
4562 if (!autosw && video_autosw_set(autosw)) {
4563 pr_err("video auto-switch left enabled due to error\n");
4564 return -EIO;
4566 break;
4567 case TPACPI_VIDEO_NEW:
4568 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4569 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4570 break;
4571 default:
4572 return -ENOSYS;
4575 return (res)? 0 : -EIO;
4578 static int video_autosw_get(void)
4580 int autosw = 0;
4582 switch (video_supported) {
4583 case TPACPI_VIDEO_570:
4584 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4585 return -EIO;
4586 break;
4587 case TPACPI_VIDEO_770:
4588 case TPACPI_VIDEO_NEW:
4589 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4590 return -EIO;
4591 break;
4592 default:
4593 return -ENOSYS;
4596 return autosw & 1;
4599 static int video_autosw_set(int enable)
4601 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4602 return -EIO;
4603 return 0;
4606 static int video_outputsw_cycle(void)
4608 int autosw = video_autosw_get();
4609 int res;
4611 if (autosw < 0)
4612 return autosw;
4614 switch (video_supported) {
4615 case TPACPI_VIDEO_570:
4616 res = video_autosw_set(1);
4617 if (res)
4618 return res;
4619 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4620 break;
4621 case TPACPI_VIDEO_770:
4622 case TPACPI_VIDEO_NEW:
4623 res = video_autosw_set(1);
4624 if (res)
4625 return res;
4626 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4627 break;
4628 default:
4629 return -ENOSYS;
4631 if (!autosw && video_autosw_set(autosw)) {
4632 pr_err("video auto-switch left enabled due to error\n");
4633 return -EIO;
4636 return (res)? 0 : -EIO;
4639 static int video_expand_toggle(void)
4641 switch (video_supported) {
4642 case TPACPI_VIDEO_570:
4643 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4644 0 : -EIO;
4645 case TPACPI_VIDEO_770:
4646 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4647 0 : -EIO;
4648 case TPACPI_VIDEO_NEW:
4649 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4650 0 : -EIO;
4651 default:
4652 return -ENOSYS;
4654 /* not reached */
4657 static int video_read(struct seq_file *m)
4659 int status, autosw;
4661 if (video_supported == TPACPI_VIDEO_NONE) {
4662 seq_printf(m, "status:\t\tnot supported\n");
4663 return 0;
4666 /* Even reads can crash X.org, so... */
4667 if (!capable(CAP_SYS_ADMIN))
4668 return -EPERM;
4670 status = video_outputsw_get();
4671 if (status < 0)
4672 return status;
4674 autosw = video_autosw_get();
4675 if (autosw < 0)
4676 return autosw;
4678 seq_printf(m, "status:\t\tsupported\n");
4679 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4680 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4681 if (video_supported == TPACPI_VIDEO_NEW)
4682 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4683 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4684 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4685 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4686 if (video_supported == TPACPI_VIDEO_NEW)
4687 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4688 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4689 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4691 return 0;
4694 static int video_write(char *buf)
4696 char *cmd;
4697 int enable, disable, status;
4698 int res;
4700 if (video_supported == TPACPI_VIDEO_NONE)
4701 return -ENODEV;
4703 /* Even reads can crash X.org, let alone writes... */
4704 if (!capable(CAP_SYS_ADMIN))
4705 return -EPERM;
4707 enable = 0;
4708 disable = 0;
4710 while ((cmd = next_cmd(&buf))) {
4711 if (strlencmp(cmd, "lcd_enable") == 0) {
4712 enable |= TP_ACPI_VIDEO_S_LCD;
4713 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4714 disable |= TP_ACPI_VIDEO_S_LCD;
4715 } else if (strlencmp(cmd, "crt_enable") == 0) {
4716 enable |= TP_ACPI_VIDEO_S_CRT;
4717 } else if (strlencmp(cmd, "crt_disable") == 0) {
4718 disable |= TP_ACPI_VIDEO_S_CRT;
4719 } else if (video_supported == TPACPI_VIDEO_NEW &&
4720 strlencmp(cmd, "dvi_enable") == 0) {
4721 enable |= TP_ACPI_VIDEO_S_DVI;
4722 } else if (video_supported == TPACPI_VIDEO_NEW &&
4723 strlencmp(cmd, "dvi_disable") == 0) {
4724 disable |= TP_ACPI_VIDEO_S_DVI;
4725 } else if (strlencmp(cmd, "auto_enable") == 0) {
4726 res = video_autosw_set(1);
4727 if (res)
4728 return res;
4729 } else if (strlencmp(cmd, "auto_disable") == 0) {
4730 res = video_autosw_set(0);
4731 if (res)
4732 return res;
4733 } else if (strlencmp(cmd, "video_switch") == 0) {
4734 res = video_outputsw_cycle();
4735 if (res)
4736 return res;
4737 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4738 res = video_expand_toggle();
4739 if (res)
4740 return res;
4741 } else
4742 return -EINVAL;
4745 if (enable || disable) {
4746 status = video_outputsw_get();
4747 if (status < 0)
4748 return status;
4749 res = video_outputsw_set((status & ~disable) | enable);
4750 if (res)
4751 return res;
4754 return 0;
4757 static struct ibm_struct video_driver_data = {
4758 .name = "video",
4759 .read = video_read,
4760 .write = video_write,
4761 .exit = video_exit,
4764 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4766 /*************************************************************************
4767 * Light (thinklight) subdriver
4770 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4771 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4773 static int light_get_status(void)
4775 int status = 0;
4777 if (tp_features.light_status) {
4778 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4779 return -EIO;
4780 return (!!status);
4783 return -ENXIO;
4786 static int light_set_status(int status)
4788 int rc;
4790 if (tp_features.light) {
4791 if (cmos_handle) {
4792 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4793 (status)?
4794 TP_CMOS_THINKLIGHT_ON :
4795 TP_CMOS_THINKLIGHT_OFF);
4796 } else {
4797 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4798 (status)? 1 : 0);
4800 return (rc)? 0 : -EIO;
4803 return -ENXIO;
4806 static void light_set_status_worker(struct work_struct *work)
4808 struct tpacpi_led_classdev *data =
4809 container_of(work, struct tpacpi_led_classdev, work);
4811 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4812 light_set_status((data->new_state != TPACPI_LED_OFF));
4815 static void light_sysfs_set(struct led_classdev *led_cdev,
4816 enum led_brightness brightness)
4818 struct tpacpi_led_classdev *data =
4819 container_of(led_cdev,
4820 struct tpacpi_led_classdev,
4821 led_classdev);
4822 data->new_state = (brightness != LED_OFF) ?
4823 TPACPI_LED_ON : TPACPI_LED_OFF;
4824 queue_work(tpacpi_wq, &data->work);
4827 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4829 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4832 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4833 .led_classdev = {
4834 .name = "tpacpi::thinklight",
4835 .brightness_set = &light_sysfs_set,
4836 .brightness_get = &light_sysfs_get,
4840 static int __init light_init(struct ibm_init_struct *iibm)
4842 int rc;
4844 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4846 if (tpacpi_is_ibm()) {
4847 TPACPI_ACPIHANDLE_INIT(ledb);
4848 TPACPI_ACPIHANDLE_INIT(lght);
4850 TPACPI_ACPIHANDLE_INIT(cmos);
4851 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4853 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4854 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4856 if (tp_features.light)
4857 /* light status not supported on
4858 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4859 tp_features.light_status =
4860 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4862 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4863 str_supported(tp_features.light),
4864 str_supported(tp_features.light_status));
4866 if (!tp_features.light)
4867 return 1;
4869 rc = led_classdev_register(&tpacpi_pdev->dev,
4870 &tpacpi_led_thinklight.led_classdev);
4872 if (rc < 0) {
4873 tp_features.light = 0;
4874 tp_features.light_status = 0;
4875 } else {
4876 rc = 0;
4879 return rc;
4882 static void light_exit(void)
4884 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4885 if (work_pending(&tpacpi_led_thinklight.work))
4886 flush_workqueue(tpacpi_wq);
4889 static int light_read(struct seq_file *m)
4891 int status;
4893 if (!tp_features.light) {
4894 seq_printf(m, "status:\t\tnot supported\n");
4895 } else if (!tp_features.light_status) {
4896 seq_printf(m, "status:\t\tunknown\n");
4897 seq_printf(m, "commands:\ton, off\n");
4898 } else {
4899 status = light_get_status();
4900 if (status < 0)
4901 return status;
4902 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4903 seq_printf(m, "commands:\ton, off\n");
4906 return 0;
4909 static int light_write(char *buf)
4911 char *cmd;
4912 int newstatus = 0;
4914 if (!tp_features.light)
4915 return -ENODEV;
4917 while ((cmd = next_cmd(&buf))) {
4918 if (strlencmp(cmd, "on") == 0) {
4919 newstatus = 1;
4920 } else if (strlencmp(cmd, "off") == 0) {
4921 newstatus = 0;
4922 } else
4923 return -EINVAL;
4926 return light_set_status(newstatus);
4929 static struct ibm_struct light_driver_data = {
4930 .name = "light",
4931 .read = light_read,
4932 .write = light_write,
4933 .exit = light_exit,
4936 /*************************************************************************
4937 * CMOS subdriver
4940 /* sysfs cmos_command -------------------------------------------------- */
4941 static ssize_t cmos_command_store(struct device *dev,
4942 struct device_attribute *attr,
4943 const char *buf, size_t count)
4945 unsigned long cmos_cmd;
4946 int res;
4948 if (parse_strtoul(buf, 21, &cmos_cmd))
4949 return -EINVAL;
4951 res = issue_thinkpad_cmos_command(cmos_cmd);
4952 return (res)? res : count;
4955 static struct device_attribute dev_attr_cmos_command =
4956 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4958 /* --------------------------------------------------------------------- */
4960 static int __init cmos_init(struct ibm_init_struct *iibm)
4962 int res;
4964 vdbg_printk(TPACPI_DBG_INIT,
4965 "initializing cmos commands subdriver\n");
4967 TPACPI_ACPIHANDLE_INIT(cmos);
4969 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4970 str_supported(cmos_handle != NULL));
4972 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4973 if (res)
4974 return res;
4976 return (cmos_handle)? 0 : 1;
4979 static void cmos_exit(void)
4981 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4984 static int cmos_read(struct seq_file *m)
4986 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4987 R30, R31, T20-22, X20-21 */
4988 if (!cmos_handle)
4989 seq_printf(m, "status:\t\tnot supported\n");
4990 else {
4991 seq_printf(m, "status:\t\tsupported\n");
4992 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4995 return 0;
4998 static int cmos_write(char *buf)
5000 char *cmd;
5001 int cmos_cmd, res;
5003 while ((cmd = next_cmd(&buf))) {
5004 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5005 cmos_cmd >= 0 && cmos_cmd <= 21) {
5006 /* cmos_cmd set */
5007 } else
5008 return -EINVAL;
5010 res = issue_thinkpad_cmos_command(cmos_cmd);
5011 if (res)
5012 return res;
5015 return 0;
5018 static struct ibm_struct cmos_driver_data = {
5019 .name = "cmos",
5020 .read = cmos_read,
5021 .write = cmos_write,
5022 .exit = cmos_exit,
5025 /*************************************************************************
5026 * LED subdriver
5029 enum led_access_mode {
5030 TPACPI_LED_NONE = 0,
5031 TPACPI_LED_570, /* 570 */
5032 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5033 TPACPI_LED_NEW, /* all others */
5036 enum { /* For TPACPI_LED_OLD */
5037 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5038 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5039 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5042 static enum led_access_mode led_supported;
5044 static acpi_handle led_handle;
5046 #define TPACPI_LED_NUMLEDS 16
5047 static struct tpacpi_led_classdev *tpacpi_leds;
5048 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5049 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5050 /* there's a limit of 19 chars + NULL before 2.6.26 */
5051 "tpacpi::power",
5052 "tpacpi:orange:batt",
5053 "tpacpi:green:batt",
5054 "tpacpi::dock_active",
5055 "tpacpi::bay_active",
5056 "tpacpi::dock_batt",
5057 "tpacpi::unknown_led",
5058 "tpacpi::standby",
5059 "tpacpi::dock_status1",
5060 "tpacpi::dock_status2",
5061 "tpacpi::unknown_led2",
5062 "tpacpi::unknown_led3",
5063 "tpacpi::thinkvantage",
5065 #define TPACPI_SAFE_LEDS 0x1081U
5067 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5069 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5070 return false;
5071 #else
5072 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5073 #endif
5076 static int led_get_status(const unsigned int led)
5078 int status;
5079 enum led_status_t led_s;
5081 switch (led_supported) {
5082 case TPACPI_LED_570:
5083 if (!acpi_evalf(ec_handle,
5084 &status, "GLED", "dd", 1 << led))
5085 return -EIO;
5086 led_s = (status == 0)?
5087 TPACPI_LED_OFF :
5088 ((status == 1)?
5089 TPACPI_LED_ON :
5090 TPACPI_LED_BLINK);
5091 tpacpi_led_state_cache[led] = led_s;
5092 return led_s;
5093 default:
5094 return -ENXIO;
5097 /* not reached */
5100 static int led_set_status(const unsigned int led,
5101 const enum led_status_t ledstatus)
5103 /* off, on, blink. Index is led_status_t */
5104 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5105 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5107 int rc = 0;
5109 switch (led_supported) {
5110 case TPACPI_LED_570:
5111 /* 570 */
5112 if (unlikely(led > 7))
5113 return -EINVAL;
5114 if (unlikely(tpacpi_is_led_restricted(led)))
5115 return -EPERM;
5116 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5117 (1 << led), led_sled_arg1[ledstatus]))
5118 rc = -EIO;
5119 break;
5120 case TPACPI_LED_OLD:
5121 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5122 if (unlikely(led > 7))
5123 return -EINVAL;
5124 if (unlikely(tpacpi_is_led_restricted(led)))
5125 return -EPERM;
5126 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5127 if (rc >= 0)
5128 rc = ec_write(TPACPI_LED_EC_HLBL,
5129 (ledstatus == TPACPI_LED_BLINK) << led);
5130 if (rc >= 0)
5131 rc = ec_write(TPACPI_LED_EC_HLCL,
5132 (ledstatus != TPACPI_LED_OFF) << led);
5133 break;
5134 case TPACPI_LED_NEW:
5135 /* all others */
5136 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5137 return -EINVAL;
5138 if (unlikely(tpacpi_is_led_restricted(led)))
5139 return -EPERM;
5140 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5141 led, led_led_arg1[ledstatus]))
5142 rc = -EIO;
5143 break;
5144 default:
5145 rc = -ENXIO;
5148 if (!rc)
5149 tpacpi_led_state_cache[led] = ledstatus;
5151 return rc;
5154 static void led_set_status_worker(struct work_struct *work)
5156 struct tpacpi_led_classdev *data =
5157 container_of(work, struct tpacpi_led_classdev, work);
5159 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5160 led_set_status(data->led, data->new_state);
5163 static void led_sysfs_set(struct led_classdev *led_cdev,
5164 enum led_brightness brightness)
5166 struct tpacpi_led_classdev *data = container_of(led_cdev,
5167 struct tpacpi_led_classdev, led_classdev);
5169 if (brightness == LED_OFF)
5170 data->new_state = TPACPI_LED_OFF;
5171 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5172 data->new_state = TPACPI_LED_ON;
5173 else
5174 data->new_state = TPACPI_LED_BLINK;
5176 queue_work(tpacpi_wq, &data->work);
5179 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5180 unsigned long *delay_on, unsigned long *delay_off)
5182 struct tpacpi_led_classdev *data = container_of(led_cdev,
5183 struct tpacpi_led_classdev, led_classdev);
5185 /* Can we choose the flash rate? */
5186 if (*delay_on == 0 && *delay_off == 0) {
5187 /* yes. set them to the hardware blink rate (1 Hz) */
5188 *delay_on = 500; /* ms */
5189 *delay_off = 500; /* ms */
5190 } else if ((*delay_on != 500) || (*delay_off != 500))
5191 return -EINVAL;
5193 data->new_state = TPACPI_LED_BLINK;
5194 queue_work(tpacpi_wq, &data->work);
5196 return 0;
5199 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5201 int rc;
5203 struct tpacpi_led_classdev *data = container_of(led_cdev,
5204 struct tpacpi_led_classdev, led_classdev);
5206 rc = led_get_status(data->led);
5208 if (rc == TPACPI_LED_OFF || rc < 0)
5209 rc = LED_OFF; /* no error handling in led class :( */
5210 else
5211 rc = LED_FULL;
5213 return rc;
5216 static void led_exit(void)
5218 unsigned int i;
5220 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5221 if (tpacpi_leds[i].led_classdev.name)
5222 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5225 kfree(tpacpi_leds);
5228 static int __init tpacpi_init_led(unsigned int led)
5230 int rc;
5232 tpacpi_leds[led].led = led;
5234 /* LEDs with no name don't get registered */
5235 if (!tpacpi_led_names[led])
5236 return 0;
5238 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5239 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5240 if (led_supported == TPACPI_LED_570)
5241 tpacpi_leds[led].led_classdev.brightness_get =
5242 &led_sysfs_get;
5244 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5246 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5248 rc = led_classdev_register(&tpacpi_pdev->dev,
5249 &tpacpi_leds[led].led_classdev);
5250 if (rc < 0)
5251 tpacpi_leds[led].led_classdev.name = NULL;
5253 return rc;
5256 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5257 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5258 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5259 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5261 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5262 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5263 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5264 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5265 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5266 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5267 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5268 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5270 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5271 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5272 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5273 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5274 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5276 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5277 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5278 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5279 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5281 /* (1) - may have excess leds enabled on MSB */
5283 /* Defaults (order matters, keep last, don't reorder!) */
5284 { /* Lenovo */
5285 .vendor = PCI_VENDOR_ID_LENOVO,
5286 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5287 .quirks = 0x1fffU,
5289 { /* IBM ThinkPads with no EC version string */
5290 .vendor = PCI_VENDOR_ID_IBM,
5291 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5292 .quirks = 0x00ffU,
5294 { /* IBM ThinkPads with EC version string */
5295 .vendor = PCI_VENDOR_ID_IBM,
5296 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5297 .quirks = 0x00bfU,
5301 #undef TPACPI_LEDQ_IBM
5302 #undef TPACPI_LEDQ_LNV
5304 static enum led_access_mode __init led_init_detect_mode(void)
5306 acpi_status status;
5308 if (tpacpi_is_ibm()) {
5309 /* 570 */
5310 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5311 if (ACPI_SUCCESS(status))
5312 return TPACPI_LED_570;
5314 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5315 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5316 if (ACPI_SUCCESS(status))
5317 return TPACPI_LED_OLD;
5320 /* most others */
5321 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5322 if (ACPI_SUCCESS(status))
5323 return TPACPI_LED_NEW;
5325 /* R30, R31, and unknown firmwares */
5326 led_handle = NULL;
5327 return TPACPI_LED_NONE;
5330 static int __init led_init(struct ibm_init_struct *iibm)
5332 unsigned int i;
5333 int rc;
5334 unsigned long useful_leds;
5336 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5338 led_supported = led_init_detect_mode();
5340 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5341 str_supported(led_supported), led_supported);
5343 if (led_supported == TPACPI_LED_NONE)
5344 return 1;
5346 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5347 GFP_KERNEL);
5348 if (!tpacpi_leds) {
5349 pr_err("Out of memory for LED data\n");
5350 return -ENOMEM;
5353 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5354 ARRAY_SIZE(led_useful_qtable));
5356 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5357 if (!tpacpi_is_led_restricted(i) &&
5358 test_bit(i, &useful_leds)) {
5359 rc = tpacpi_init_led(i);
5360 if (rc < 0) {
5361 led_exit();
5362 return rc;
5367 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5368 pr_notice("warning: userspace override of important "
5369 "firmware LEDs is enabled\n");
5370 #endif
5371 return 0;
5374 #define str_led_status(s) \
5375 ((s) == TPACPI_LED_OFF ? "off" : \
5376 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5378 static int led_read(struct seq_file *m)
5380 if (!led_supported) {
5381 seq_printf(m, "status:\t\tnot supported\n");
5382 return 0;
5384 seq_printf(m, "status:\t\tsupported\n");
5386 if (led_supported == TPACPI_LED_570) {
5387 /* 570 */
5388 int i, status;
5389 for (i = 0; i < 8; i++) {
5390 status = led_get_status(i);
5391 if (status < 0)
5392 return -EIO;
5393 seq_printf(m, "%d:\t\t%s\n",
5394 i, str_led_status(status));
5398 seq_printf(m, "commands:\t"
5399 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5401 return 0;
5404 static int led_write(char *buf)
5406 char *cmd;
5407 int led, rc;
5408 enum led_status_t s;
5410 if (!led_supported)
5411 return -ENODEV;
5413 while ((cmd = next_cmd(&buf))) {
5414 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5415 return -EINVAL;
5417 if (strstr(cmd, "off")) {
5418 s = TPACPI_LED_OFF;
5419 } else if (strstr(cmd, "on")) {
5420 s = TPACPI_LED_ON;
5421 } else if (strstr(cmd, "blink")) {
5422 s = TPACPI_LED_BLINK;
5423 } else {
5424 return -EINVAL;
5427 rc = led_set_status(led, s);
5428 if (rc < 0)
5429 return rc;
5432 return 0;
5435 static struct ibm_struct led_driver_data = {
5436 .name = "led",
5437 .read = led_read,
5438 .write = led_write,
5439 .exit = led_exit,
5442 /*************************************************************************
5443 * Beep subdriver
5446 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5448 #define TPACPI_BEEP_Q1 0x0001
5450 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5451 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5452 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5455 static int __init beep_init(struct ibm_init_struct *iibm)
5457 unsigned long quirks;
5459 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5461 TPACPI_ACPIHANDLE_INIT(beep);
5463 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5464 str_supported(beep_handle != NULL));
5466 quirks = tpacpi_check_quirks(beep_quirk_table,
5467 ARRAY_SIZE(beep_quirk_table));
5469 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5471 return (beep_handle)? 0 : 1;
5474 static int beep_read(struct seq_file *m)
5476 if (!beep_handle)
5477 seq_printf(m, "status:\t\tnot supported\n");
5478 else {
5479 seq_printf(m, "status:\t\tsupported\n");
5480 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5483 return 0;
5486 static int beep_write(char *buf)
5488 char *cmd;
5489 int beep_cmd;
5491 if (!beep_handle)
5492 return -ENODEV;
5494 while ((cmd = next_cmd(&buf))) {
5495 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5496 beep_cmd >= 0 && beep_cmd <= 17) {
5497 /* beep_cmd set */
5498 } else
5499 return -EINVAL;
5500 if (tp_features.beep_needs_two_args) {
5501 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5502 beep_cmd, 0))
5503 return -EIO;
5504 } else {
5505 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5506 beep_cmd))
5507 return -EIO;
5511 return 0;
5514 static struct ibm_struct beep_driver_data = {
5515 .name = "beep",
5516 .read = beep_read,
5517 .write = beep_write,
5520 /*************************************************************************
5521 * Thermal subdriver
5524 enum thermal_access_mode {
5525 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5526 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5527 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5528 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5529 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5532 enum { /* TPACPI_THERMAL_TPEC_* */
5533 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5534 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5535 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5537 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5541 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5542 struct ibm_thermal_sensors_struct {
5543 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5546 static enum thermal_access_mode thermal_read_mode;
5548 /* idx is zero-based */
5549 static int thermal_get_sensor(int idx, s32 *value)
5551 int t;
5552 s8 tmp;
5553 char tmpi[5];
5555 t = TP_EC_THERMAL_TMP0;
5557 switch (thermal_read_mode) {
5558 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5559 case TPACPI_THERMAL_TPEC_16:
5560 if (idx >= 8 && idx <= 15) {
5561 t = TP_EC_THERMAL_TMP8;
5562 idx -= 8;
5564 /* fallthrough */
5565 #endif
5566 case TPACPI_THERMAL_TPEC_8:
5567 if (idx <= 7) {
5568 if (!acpi_ec_read(t + idx, &tmp))
5569 return -EIO;
5570 *value = tmp * 1000;
5571 return 0;
5573 break;
5575 case TPACPI_THERMAL_ACPI_UPDT:
5576 if (idx <= 7) {
5577 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5578 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5579 return -EIO;
5580 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5581 return -EIO;
5582 *value = (t - 2732) * 100;
5583 return 0;
5585 break;
5587 case TPACPI_THERMAL_ACPI_TMP07:
5588 if (idx <= 7) {
5589 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5590 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5591 return -EIO;
5592 if (t > 127 || t < -127)
5593 t = TP_EC_THERMAL_TMP_NA;
5594 *value = t * 1000;
5595 return 0;
5597 break;
5599 case TPACPI_THERMAL_NONE:
5600 default:
5601 return -ENOSYS;
5604 return -EINVAL;
5607 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5609 int res, i;
5610 int n;
5612 n = 8;
5613 i = 0;
5615 if (!s)
5616 return -EINVAL;
5618 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5619 n = 16;
5621 for (i = 0 ; i < n; i++) {
5622 res = thermal_get_sensor(i, &s->temp[i]);
5623 if (res)
5624 return res;
5627 return n;
5630 static void thermal_dump_all_sensors(void)
5632 int n, i;
5633 struct ibm_thermal_sensors_struct t;
5635 n = thermal_get_sensors(&t);
5636 if (n <= 0)
5637 return;
5639 pr_notice("temperatures (Celsius):");
5641 for (i = 0; i < n; i++) {
5642 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5643 pr_cont(" %d", (int)(t.temp[i] / 1000));
5644 else
5645 pr_cont(" N/A");
5648 pr_cont("\n");
5651 /* sysfs temp##_input -------------------------------------------------- */
5653 static ssize_t thermal_temp_input_show(struct device *dev,
5654 struct device_attribute *attr,
5655 char *buf)
5657 struct sensor_device_attribute *sensor_attr =
5658 to_sensor_dev_attr(attr);
5659 int idx = sensor_attr->index;
5660 s32 value;
5661 int res;
5663 res = thermal_get_sensor(idx, &value);
5664 if (res)
5665 return res;
5666 if (value == TPACPI_THERMAL_SENSOR_NA)
5667 return -ENXIO;
5669 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5672 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5673 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5674 thermal_temp_input_show, NULL, _idxB)
5676 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5677 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5678 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5679 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5680 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5681 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5682 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5683 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5684 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5685 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5686 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5687 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5688 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5689 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5690 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5691 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5692 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5695 #define THERMAL_ATTRS(X) \
5696 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5698 static struct attribute *thermal_temp_input_attr[] = {
5699 THERMAL_ATTRS(8),
5700 THERMAL_ATTRS(9),
5701 THERMAL_ATTRS(10),
5702 THERMAL_ATTRS(11),
5703 THERMAL_ATTRS(12),
5704 THERMAL_ATTRS(13),
5705 THERMAL_ATTRS(14),
5706 THERMAL_ATTRS(15),
5707 THERMAL_ATTRS(0),
5708 THERMAL_ATTRS(1),
5709 THERMAL_ATTRS(2),
5710 THERMAL_ATTRS(3),
5711 THERMAL_ATTRS(4),
5712 THERMAL_ATTRS(5),
5713 THERMAL_ATTRS(6),
5714 THERMAL_ATTRS(7),
5715 NULL
5718 static const struct attribute_group thermal_temp_input16_group = {
5719 .attrs = thermal_temp_input_attr
5722 static const struct attribute_group thermal_temp_input8_group = {
5723 .attrs = &thermal_temp_input_attr[8]
5726 #undef THERMAL_SENSOR_ATTR_TEMP
5727 #undef THERMAL_ATTRS
5729 /* --------------------------------------------------------------------- */
5731 static int __init thermal_init(struct ibm_init_struct *iibm)
5733 u8 t, ta1, ta2;
5734 int i;
5735 int acpi_tmp7;
5736 int res;
5738 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5740 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5742 if (thinkpad_id.ec_model) {
5744 * Direct EC access mode: sensors at registers
5745 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5746 * non-implemented, thermal sensors return 0x80 when
5747 * not available
5750 ta1 = ta2 = 0;
5751 for (i = 0; i < 8; i++) {
5752 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5753 ta1 |= t;
5754 } else {
5755 ta1 = 0;
5756 break;
5758 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5759 ta2 |= t;
5760 } else {
5761 ta1 = 0;
5762 break;
5765 if (ta1 == 0) {
5766 /* This is sheer paranoia, but we handle it anyway */
5767 if (acpi_tmp7) {
5768 pr_err("ThinkPad ACPI EC access misbehaving, "
5769 "falling back to ACPI TMPx access "
5770 "mode\n");
5771 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5772 } else {
5773 pr_err("ThinkPad ACPI EC access misbehaving, "
5774 "disabling thermal sensors access\n");
5775 thermal_read_mode = TPACPI_THERMAL_NONE;
5777 } else {
5778 thermal_read_mode =
5779 (ta2 != 0) ?
5780 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5782 } else if (acpi_tmp7) {
5783 if (tpacpi_is_ibm() &&
5784 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5785 /* 600e/x, 770e, 770x */
5786 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5787 } else {
5788 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5789 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5791 } else {
5792 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5793 thermal_read_mode = TPACPI_THERMAL_NONE;
5796 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5797 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5798 thermal_read_mode);
5800 switch (thermal_read_mode) {
5801 case TPACPI_THERMAL_TPEC_16:
5802 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5803 &thermal_temp_input16_group);
5804 if (res)
5805 return res;
5806 break;
5807 case TPACPI_THERMAL_TPEC_8:
5808 case TPACPI_THERMAL_ACPI_TMP07:
5809 case TPACPI_THERMAL_ACPI_UPDT:
5810 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5811 &thermal_temp_input8_group);
5812 if (res)
5813 return res;
5814 break;
5815 case TPACPI_THERMAL_NONE:
5816 default:
5817 return 1;
5820 return 0;
5823 static void thermal_exit(void)
5825 switch (thermal_read_mode) {
5826 case TPACPI_THERMAL_TPEC_16:
5827 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5828 &thermal_temp_input16_group);
5829 break;
5830 case TPACPI_THERMAL_TPEC_8:
5831 case TPACPI_THERMAL_ACPI_TMP07:
5832 case TPACPI_THERMAL_ACPI_UPDT:
5833 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5834 &thermal_temp_input8_group);
5835 break;
5836 case TPACPI_THERMAL_NONE:
5837 default:
5838 break;
5842 static int thermal_read(struct seq_file *m)
5844 int n, i;
5845 struct ibm_thermal_sensors_struct t;
5847 n = thermal_get_sensors(&t);
5848 if (unlikely(n < 0))
5849 return n;
5851 seq_printf(m, "temperatures:\t");
5853 if (n > 0) {
5854 for (i = 0; i < (n - 1); i++)
5855 seq_printf(m, "%d ", t.temp[i] / 1000);
5856 seq_printf(m, "%d\n", t.temp[i] / 1000);
5857 } else
5858 seq_printf(m, "not supported\n");
5860 return 0;
5863 static struct ibm_struct thermal_driver_data = {
5864 .name = "thermal",
5865 .read = thermal_read,
5866 .exit = thermal_exit,
5869 /*************************************************************************
5870 * EC Dump subdriver
5873 static u8 ecdump_regs[256];
5875 static int ecdump_read(struct seq_file *m)
5877 int i, j;
5878 u8 v;
5880 seq_printf(m, "EC "
5881 " +00 +01 +02 +03 +04 +05 +06 +07"
5882 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5883 for (i = 0; i < 256; i += 16) {
5884 seq_printf(m, "EC 0x%02x:", i);
5885 for (j = 0; j < 16; j++) {
5886 if (!acpi_ec_read(i + j, &v))
5887 break;
5888 if (v != ecdump_regs[i + j])
5889 seq_printf(m, " *%02x", v);
5890 else
5891 seq_printf(m, " %02x", v);
5892 ecdump_regs[i + j] = v;
5894 seq_putc(m, '\n');
5895 if (j != 16)
5896 break;
5899 /* These are way too dangerous to advertise openly... */
5900 #if 0
5901 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5902 " (<offset> is 00-ff, <value> is 00-ff)\n");
5903 seq_printf(m, "commands:\t0x<offset> <value> "
5904 " (<offset> is 00-ff, <value> is 0-255)\n");
5905 #endif
5906 return 0;
5909 static int ecdump_write(char *buf)
5911 char *cmd;
5912 int i, v;
5914 while ((cmd = next_cmd(&buf))) {
5915 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5916 /* i and v set */
5917 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5918 /* i and v set */
5919 } else
5920 return -EINVAL;
5921 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5922 if (!acpi_ec_write(i, v))
5923 return -EIO;
5924 } else
5925 return -EINVAL;
5928 return 0;
5931 static struct ibm_struct ecdump_driver_data = {
5932 .name = "ecdump",
5933 .read = ecdump_read,
5934 .write = ecdump_write,
5935 .flags.experimental = 1,
5938 /*************************************************************************
5939 * Backlight/brightness subdriver
5942 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5945 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5946 * CMOS NVRAM byte 0x5E, bits 0-3.
5948 * EC HBRV (0x31) has the following layout
5949 * Bit 7: unknown function
5950 * Bit 6: unknown function
5951 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5952 * Bit 4: must be set to zero to avoid problems
5953 * Bit 3-0: backlight brightness level
5955 * brightness_get_raw returns status data in the HBRV layout
5957 * WARNING: The X61 has been verified to use HBRV for something else, so
5958 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5959 * testing on the very early *60 Lenovo models...
5962 enum {
5963 TP_EC_BACKLIGHT = 0x31,
5965 /* TP_EC_BACKLIGHT bitmasks */
5966 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5967 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5968 TP_EC_BACKLIGHT_MAPSW = 0x20,
5971 enum tpacpi_brightness_access_mode {
5972 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5973 TPACPI_BRGHT_MODE_EC, /* EC control */
5974 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5975 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5976 TPACPI_BRGHT_MODE_MAX
5979 static struct backlight_device *ibm_backlight_device;
5981 static enum tpacpi_brightness_access_mode brightness_mode =
5982 TPACPI_BRGHT_MODE_MAX;
5984 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5986 static struct mutex brightness_mutex;
5988 /* NVRAM brightness access,
5989 * call with brightness_mutex held! */
5990 static unsigned int tpacpi_brightness_nvram_get(void)
5992 u8 lnvram;
5994 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5995 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5996 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5997 lnvram &= bright_maxlvl;
5999 return lnvram;
6002 static void tpacpi_brightness_checkpoint_nvram(void)
6004 u8 lec = 0;
6005 u8 b_nvram;
6007 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6008 return;
6010 vdbg_printk(TPACPI_DBG_BRGHT,
6011 "trying to checkpoint backlight level to NVRAM...\n");
6013 if (mutex_lock_killable(&brightness_mutex) < 0)
6014 return;
6016 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6017 goto unlock;
6018 lec &= TP_EC_BACKLIGHT_LVLMSK;
6019 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6021 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6022 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6023 /* NVRAM needs update */
6024 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6025 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6026 b_nvram |= lec;
6027 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6028 dbg_printk(TPACPI_DBG_BRGHT,
6029 "updated NVRAM backlight level to %u (0x%02x)\n",
6030 (unsigned int) lec, (unsigned int) b_nvram);
6031 } else
6032 vdbg_printk(TPACPI_DBG_BRGHT,
6033 "NVRAM backlight level already is %u (0x%02x)\n",
6034 (unsigned int) lec, (unsigned int) b_nvram);
6036 unlock:
6037 mutex_unlock(&brightness_mutex);
6041 /* call with brightness_mutex held! */
6042 static int tpacpi_brightness_get_raw(int *status)
6044 u8 lec = 0;
6046 switch (brightness_mode) {
6047 case TPACPI_BRGHT_MODE_UCMS_STEP:
6048 *status = tpacpi_brightness_nvram_get();
6049 return 0;
6050 case TPACPI_BRGHT_MODE_EC:
6051 case TPACPI_BRGHT_MODE_ECNVRAM:
6052 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6053 return -EIO;
6054 *status = lec;
6055 return 0;
6056 default:
6057 return -ENXIO;
6061 /* call with brightness_mutex held! */
6062 /* do NOT call with illegal backlight level value */
6063 static int tpacpi_brightness_set_ec(unsigned int value)
6065 u8 lec = 0;
6067 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6068 return -EIO;
6070 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6071 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6072 (value & TP_EC_BACKLIGHT_LVLMSK))))
6073 return -EIO;
6075 return 0;
6078 /* call with brightness_mutex held! */
6079 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6081 int cmos_cmd, inc;
6082 unsigned int current_value, i;
6084 current_value = tpacpi_brightness_nvram_get();
6086 if (value == current_value)
6087 return 0;
6089 cmos_cmd = (value > current_value) ?
6090 TP_CMOS_BRIGHTNESS_UP :
6091 TP_CMOS_BRIGHTNESS_DOWN;
6092 inc = (value > current_value) ? 1 : -1;
6094 for (i = current_value; i != value; i += inc)
6095 if (issue_thinkpad_cmos_command(cmos_cmd))
6096 return -EIO;
6098 return 0;
6101 /* May return EINTR which can always be mapped to ERESTARTSYS */
6102 static int brightness_set(unsigned int value)
6104 int res;
6106 if (value > bright_maxlvl || value < 0)
6107 return -EINVAL;
6109 vdbg_printk(TPACPI_DBG_BRGHT,
6110 "set backlight level to %d\n", value);
6112 res = mutex_lock_killable(&brightness_mutex);
6113 if (res < 0)
6114 return res;
6116 switch (brightness_mode) {
6117 case TPACPI_BRGHT_MODE_EC:
6118 case TPACPI_BRGHT_MODE_ECNVRAM:
6119 res = tpacpi_brightness_set_ec(value);
6120 break;
6121 case TPACPI_BRGHT_MODE_UCMS_STEP:
6122 res = tpacpi_brightness_set_ucmsstep(value);
6123 break;
6124 default:
6125 res = -ENXIO;
6128 mutex_unlock(&brightness_mutex);
6129 return res;
6132 /* sysfs backlight class ----------------------------------------------- */
6134 static int brightness_update_status(struct backlight_device *bd)
6136 unsigned int level =
6137 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6138 bd->props.power == FB_BLANK_UNBLANK) ?
6139 bd->props.brightness : 0;
6141 dbg_printk(TPACPI_DBG_BRGHT,
6142 "backlight: attempt to set level to %d\n",
6143 level);
6145 /* it is the backlight class's job (caller) to handle
6146 * EINTR and other errors properly */
6147 return brightness_set(level);
6150 static int brightness_get(struct backlight_device *bd)
6152 int status, res;
6154 res = mutex_lock_killable(&brightness_mutex);
6155 if (res < 0)
6156 return 0;
6158 res = tpacpi_brightness_get_raw(&status);
6160 mutex_unlock(&brightness_mutex);
6162 if (res < 0)
6163 return 0;
6165 return status & TP_EC_BACKLIGHT_LVLMSK;
6168 static void tpacpi_brightness_notify_change(void)
6170 backlight_force_update(ibm_backlight_device,
6171 BACKLIGHT_UPDATE_HOTKEY);
6174 static const struct backlight_ops ibm_backlight_data = {
6175 .get_brightness = brightness_get,
6176 .update_status = brightness_update_status,
6179 /* --------------------------------------------------------------------- */
6182 * Call _BCL method of video device. On some ThinkPads this will
6183 * switch the firmware to the ACPI brightness control mode.
6186 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6188 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6189 union acpi_object *obj;
6190 int rc;
6192 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6193 obj = (union acpi_object *)buffer.pointer;
6194 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6195 pr_err("Unknown _BCL data, please report this to %s\n",
6196 TPACPI_MAIL);
6197 rc = 0;
6198 } else {
6199 rc = obj->package.count;
6201 } else {
6202 return 0;
6205 kfree(buffer.pointer);
6206 return rc;
6211 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6213 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6215 acpi_handle video_device;
6216 int bcl_levels = 0;
6218 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6219 if (video_device)
6220 bcl_levels = tpacpi_query_bcl_levels(video_device);
6222 tp_features.bright_acpimode = (bcl_levels > 0);
6224 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6228 * These are only useful for models that have only one possibility
6229 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6230 * these quirks.
6232 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6233 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6234 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6236 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6237 /* Models with ATI GPUs known to require ECNVRAM mode */
6238 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6240 /* Models with ATI GPUs that can use ECNVRAM */
6241 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6242 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6243 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6244 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6246 /* Models with Intel Extreme Graphics 2 */
6247 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6248 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6249 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6251 /* Models with Intel GMA900 */
6252 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6253 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6254 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6258 * Returns < 0 for error, otherwise sets tp_features.bright_*
6259 * and bright_maxlvl.
6261 static void __init tpacpi_detect_brightness_capabilities(void)
6263 unsigned int b;
6265 vdbg_printk(TPACPI_DBG_INIT,
6266 "detecting firmware brightness interface capabilities\n");
6268 /* we could run a quirks check here (same table used by
6269 * brightness_init) if needed */
6272 * We always attempt to detect acpi support, so as to switch
6273 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6274 * going to publish a backlight interface
6276 b = tpacpi_check_std_acpi_brightness_support();
6277 switch (b) {
6278 case 16:
6279 bright_maxlvl = 15;
6280 pr_info("detected a 16-level brightness capable ThinkPad\n");
6281 break;
6282 case 8:
6283 case 0:
6284 bright_maxlvl = 7;
6285 pr_info("detected a 8-level brightness capable ThinkPad\n");
6286 break;
6287 default:
6288 pr_err("Unsupported brightness interface, "
6289 "please contact %s\n", TPACPI_MAIL);
6290 tp_features.bright_unkfw = 1;
6291 bright_maxlvl = b - 1;
6295 static int __init brightness_init(struct ibm_init_struct *iibm)
6297 struct backlight_properties props;
6298 int b;
6299 unsigned long quirks;
6301 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6303 mutex_init(&brightness_mutex);
6305 quirks = tpacpi_check_quirks(brightness_quirk_table,
6306 ARRAY_SIZE(brightness_quirk_table));
6308 /* tpacpi_detect_brightness_capabilities() must have run already */
6310 /* if it is unknown, we don't handle it: it wouldn't be safe */
6311 if (tp_features.bright_unkfw)
6312 return 1;
6314 if (!brightness_enable) {
6315 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6316 "brightness support disabled by "
6317 "module parameter\n");
6318 return 1;
6321 if (acpi_video_backlight_support()) {
6322 if (brightness_enable > 1) {
6323 pr_info("Standard ACPI backlight interface "
6324 "available, not loading native one\n");
6325 return 1;
6326 } else if (brightness_enable == 1) {
6327 pr_warn("Cannot enable backlight brightness support, "
6328 "ACPI is already handling it. Refer to the "
6329 "acpi_backlight kernel parameter.\n");
6330 return 1;
6332 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6333 pr_notice("Standard ACPI backlight interface not "
6334 "available, thinkpad_acpi native "
6335 "brightness control enabled\n");
6339 * Check for module parameter bogosity, note that we
6340 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6341 * able to detect "unspecified"
6343 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6344 return -EINVAL;
6346 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6347 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6348 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6349 if (quirks & TPACPI_BRGHT_Q_EC)
6350 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6351 else
6352 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6354 dbg_printk(TPACPI_DBG_BRGHT,
6355 "driver auto-selected brightness_mode=%d\n",
6356 brightness_mode);
6359 /* Safety */
6360 if (!tpacpi_is_ibm() &&
6361 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6362 brightness_mode == TPACPI_BRGHT_MODE_EC))
6363 return -EINVAL;
6365 if (tpacpi_brightness_get_raw(&b) < 0)
6366 return 1;
6368 memset(&props, 0, sizeof(struct backlight_properties));
6369 props.max_brightness = bright_maxlvl;
6370 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6371 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6372 NULL, NULL,
6373 &ibm_backlight_data,
6374 &props);
6375 if (IS_ERR(ibm_backlight_device)) {
6376 int rc = PTR_ERR(ibm_backlight_device);
6377 ibm_backlight_device = NULL;
6378 pr_err("Could not register backlight device\n");
6379 return rc;
6381 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6382 "brightness is supported\n");
6384 if (quirks & TPACPI_BRGHT_Q_ASK) {
6385 pr_notice("brightness: will use unverified default: "
6386 "brightness_mode=%d\n", brightness_mode);
6387 pr_notice("brightness: please report to %s whether it works well "
6388 "or not on your ThinkPad\n", TPACPI_MAIL);
6391 /* Added by mistake in early 2007. Probably useless, but it could
6392 * be working around some unknown firmware problem where the value
6393 * read at startup doesn't match the real hardware state... so leave
6394 * it in place just in case */
6395 backlight_update_status(ibm_backlight_device);
6397 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6398 "brightness: registering brightness hotkeys "
6399 "as change notification\n");
6400 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6401 | TP_ACPI_HKEY_BRGHTUP_MASK
6402 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6403 return 0;
6406 static void brightness_suspend(pm_message_t state)
6408 tpacpi_brightness_checkpoint_nvram();
6411 static void brightness_shutdown(void)
6413 tpacpi_brightness_checkpoint_nvram();
6416 static void brightness_exit(void)
6418 if (ibm_backlight_device) {
6419 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6420 "calling backlight_device_unregister()\n");
6421 backlight_device_unregister(ibm_backlight_device);
6424 tpacpi_brightness_checkpoint_nvram();
6427 static int brightness_read(struct seq_file *m)
6429 int level;
6431 level = brightness_get(NULL);
6432 if (level < 0) {
6433 seq_printf(m, "level:\t\tunreadable\n");
6434 } else {
6435 seq_printf(m, "level:\t\t%d\n", level);
6436 seq_printf(m, "commands:\tup, down\n");
6437 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6438 bright_maxlvl);
6441 return 0;
6444 static int brightness_write(char *buf)
6446 int level;
6447 int rc;
6448 char *cmd;
6450 level = brightness_get(NULL);
6451 if (level < 0)
6452 return level;
6454 while ((cmd = next_cmd(&buf))) {
6455 if (strlencmp(cmd, "up") == 0) {
6456 if (level < bright_maxlvl)
6457 level++;
6458 } else if (strlencmp(cmd, "down") == 0) {
6459 if (level > 0)
6460 level--;
6461 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6462 level >= 0 && level <= bright_maxlvl) {
6463 /* new level set */
6464 } else
6465 return -EINVAL;
6468 tpacpi_disclose_usertask("procfs brightness",
6469 "set level to %d\n", level);
6472 * Now we know what the final level should be, so we try to set it.
6473 * Doing it this way makes the syscall restartable in case of EINTR
6475 rc = brightness_set(level);
6476 if (!rc && ibm_backlight_device)
6477 backlight_force_update(ibm_backlight_device,
6478 BACKLIGHT_UPDATE_SYSFS);
6479 return (rc == -EINTR)? -ERESTARTSYS : rc;
6482 static struct ibm_struct brightness_driver_data = {
6483 .name = "brightness",
6484 .read = brightness_read,
6485 .write = brightness_write,
6486 .exit = brightness_exit,
6487 .suspend = brightness_suspend,
6488 .shutdown = brightness_shutdown,
6491 /*************************************************************************
6492 * Volume subdriver
6496 * IBM ThinkPads have a simple volume controller with MUTE gating.
6497 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6499 * Since the *61 series (and probably also the later *60 series), Lenovo
6500 * ThinkPads only implement the MUTE gate.
6502 * EC register 0x30
6503 * Bit 6: MUTE (1 mutes sound)
6504 * Bit 3-0: Volume
6505 * Other bits should be zero as far as we know.
6507 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6508 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6509 * such as bit 7 which is used to detect repeated presses of MUTE,
6510 * and we leave them unchanged.
6513 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6515 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6516 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6517 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6519 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6520 static char *alsa_id = "ThinkPadEC";
6521 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6523 struct tpacpi_alsa_data {
6524 struct snd_card *card;
6525 struct snd_ctl_elem_id *ctl_mute_id;
6526 struct snd_ctl_elem_id *ctl_vol_id;
6529 static struct snd_card *alsa_card;
6531 enum {
6532 TP_EC_AUDIO = 0x30,
6534 /* TP_EC_AUDIO bits */
6535 TP_EC_AUDIO_MUTESW = 6,
6537 /* TP_EC_AUDIO bitmasks */
6538 TP_EC_AUDIO_LVL_MSK = 0x0F,
6539 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6541 /* Maximum volume */
6542 TP_EC_VOLUME_MAX = 14,
6545 enum tpacpi_volume_access_mode {
6546 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6547 TPACPI_VOL_MODE_EC, /* Pure EC control */
6548 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6549 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6550 TPACPI_VOL_MODE_MAX
6553 enum tpacpi_volume_capabilities {
6554 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6555 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6556 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6557 TPACPI_VOL_CAP_MAX
6560 static enum tpacpi_volume_access_mode volume_mode =
6561 TPACPI_VOL_MODE_MAX;
6563 static enum tpacpi_volume_capabilities volume_capabilities;
6564 static int volume_control_allowed;
6567 * Used to syncronize writers to TP_EC_AUDIO and
6568 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6570 static struct mutex volume_mutex;
6572 static void tpacpi_volume_checkpoint_nvram(void)
6574 u8 lec = 0;
6575 u8 b_nvram;
6576 u8 ec_mask;
6578 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6579 return;
6580 if (!volume_control_allowed)
6581 return;
6583 vdbg_printk(TPACPI_DBG_MIXER,
6584 "trying to checkpoint mixer state to NVRAM...\n");
6586 if (tp_features.mixer_no_level_control)
6587 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6588 else
6589 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6591 if (mutex_lock_killable(&volume_mutex) < 0)
6592 return;
6594 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6595 goto unlock;
6596 lec &= ec_mask;
6597 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6599 if (lec != (b_nvram & ec_mask)) {
6600 /* NVRAM needs update */
6601 b_nvram &= ~ec_mask;
6602 b_nvram |= lec;
6603 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6604 dbg_printk(TPACPI_DBG_MIXER,
6605 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6606 (unsigned int) lec, (unsigned int) b_nvram);
6607 } else {
6608 vdbg_printk(TPACPI_DBG_MIXER,
6609 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6610 (unsigned int) lec, (unsigned int) b_nvram);
6613 unlock:
6614 mutex_unlock(&volume_mutex);
6617 static int volume_get_status_ec(u8 *status)
6619 u8 s;
6621 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6622 return -EIO;
6624 *status = s;
6626 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6628 return 0;
6631 static int volume_get_status(u8 *status)
6633 return volume_get_status_ec(status);
6636 static int volume_set_status_ec(const u8 status)
6638 if (!acpi_ec_write(TP_EC_AUDIO, status))
6639 return -EIO;
6641 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6643 return 0;
6646 static int volume_set_status(const u8 status)
6648 return volume_set_status_ec(status);
6651 /* returns < 0 on error, 0 on no change, 1 on change */
6652 static int __volume_set_mute_ec(const bool mute)
6654 int rc;
6655 u8 s, n;
6657 if (mutex_lock_killable(&volume_mutex) < 0)
6658 return -EINTR;
6660 rc = volume_get_status_ec(&s);
6661 if (rc)
6662 goto unlock;
6664 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6665 s & ~TP_EC_AUDIO_MUTESW_MSK;
6667 if (n != s) {
6668 rc = volume_set_status_ec(n);
6669 if (!rc)
6670 rc = 1;
6673 unlock:
6674 mutex_unlock(&volume_mutex);
6675 return rc;
6678 static int volume_alsa_set_mute(const bool mute)
6680 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6681 (mute) ? "" : "un");
6682 return __volume_set_mute_ec(mute);
6685 static int volume_set_mute(const bool mute)
6687 int rc;
6689 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6690 (mute) ? "" : "un");
6692 rc = __volume_set_mute_ec(mute);
6693 return (rc < 0) ? rc : 0;
6696 /* returns < 0 on error, 0 on no change, 1 on change */
6697 static int __volume_set_volume_ec(const u8 vol)
6699 int rc;
6700 u8 s, n;
6702 if (vol > TP_EC_VOLUME_MAX)
6703 return -EINVAL;
6705 if (mutex_lock_killable(&volume_mutex) < 0)
6706 return -EINTR;
6708 rc = volume_get_status_ec(&s);
6709 if (rc)
6710 goto unlock;
6712 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6714 if (n != s) {
6715 rc = volume_set_status_ec(n);
6716 if (!rc)
6717 rc = 1;
6720 unlock:
6721 mutex_unlock(&volume_mutex);
6722 return rc;
6725 static int volume_alsa_set_volume(const u8 vol)
6727 dbg_printk(TPACPI_DBG_MIXER,
6728 "ALSA: trying to set volume level to %hu\n", vol);
6729 return __volume_set_volume_ec(vol);
6732 static void volume_alsa_notify_change(void)
6734 struct tpacpi_alsa_data *d;
6736 if (alsa_card && alsa_card->private_data) {
6737 d = alsa_card->private_data;
6738 if (d->ctl_mute_id)
6739 snd_ctl_notify(alsa_card,
6740 SNDRV_CTL_EVENT_MASK_VALUE,
6741 d->ctl_mute_id);
6742 if (d->ctl_vol_id)
6743 snd_ctl_notify(alsa_card,
6744 SNDRV_CTL_EVENT_MASK_VALUE,
6745 d->ctl_vol_id);
6749 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6750 struct snd_ctl_elem_info *uinfo)
6752 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6753 uinfo->count = 1;
6754 uinfo->value.integer.min = 0;
6755 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6756 return 0;
6759 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6760 struct snd_ctl_elem_value *ucontrol)
6762 u8 s;
6763 int rc;
6765 rc = volume_get_status(&s);
6766 if (rc < 0)
6767 return rc;
6769 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6770 return 0;
6773 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6774 struct snd_ctl_elem_value *ucontrol)
6776 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6777 ucontrol->value.integer.value[0]);
6778 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6781 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6783 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6784 struct snd_ctl_elem_value *ucontrol)
6786 u8 s;
6787 int rc;
6789 rc = volume_get_status(&s);
6790 if (rc < 0)
6791 return rc;
6793 ucontrol->value.integer.value[0] =
6794 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6795 return 0;
6798 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6799 struct snd_ctl_elem_value *ucontrol)
6801 tpacpi_disclose_usertask("ALSA", "%smute\n",
6802 ucontrol->value.integer.value[0] ?
6803 "un" : "");
6804 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6807 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6809 .name = "Console Playback Volume",
6810 .index = 0,
6811 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6812 .info = volume_alsa_vol_info,
6813 .get = volume_alsa_vol_get,
6816 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6817 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6818 .name = "Console Playback Switch",
6819 .index = 0,
6820 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6821 .info = volume_alsa_mute_info,
6822 .get = volume_alsa_mute_get,
6825 static void volume_suspend(pm_message_t state)
6827 tpacpi_volume_checkpoint_nvram();
6830 static void volume_resume(void)
6832 volume_alsa_notify_change();
6835 static void volume_shutdown(void)
6837 tpacpi_volume_checkpoint_nvram();
6840 static void volume_exit(void)
6842 if (alsa_card) {
6843 snd_card_free(alsa_card);
6844 alsa_card = NULL;
6847 tpacpi_volume_checkpoint_nvram();
6850 static int __init volume_create_alsa_mixer(void)
6852 struct snd_card *card;
6853 struct tpacpi_alsa_data *data;
6854 struct snd_kcontrol *ctl_vol;
6855 struct snd_kcontrol *ctl_mute;
6856 int rc;
6858 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6859 sizeof(struct tpacpi_alsa_data), &card);
6860 if (rc < 0 || !card) {
6861 pr_err("Failed to create ALSA card structures: %d\n", rc);
6862 return 1;
6865 BUG_ON(!card->private_data);
6866 data = card->private_data;
6867 data->card = card;
6869 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6870 sizeof(card->driver));
6871 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6872 sizeof(card->shortname));
6873 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6874 (thinkpad_id.ec_version_str) ?
6875 thinkpad_id.ec_version_str : "(unknown)");
6876 snprintf(card->longname, sizeof(card->longname),
6877 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6878 (thinkpad_id.ec_version_str) ?
6879 thinkpad_id.ec_version_str : "unknown");
6881 if (volume_control_allowed) {
6882 volume_alsa_control_vol.put = volume_alsa_vol_put;
6883 volume_alsa_control_vol.access =
6884 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6886 volume_alsa_control_mute.put = volume_alsa_mute_put;
6887 volume_alsa_control_mute.access =
6888 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6891 if (!tp_features.mixer_no_level_control) {
6892 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6893 rc = snd_ctl_add(card, ctl_vol);
6894 if (rc < 0) {
6895 pr_err("Failed to create ALSA volume control: %d\n",
6896 rc);
6897 goto err_exit;
6899 data->ctl_vol_id = &ctl_vol->id;
6902 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6903 rc = snd_ctl_add(card, ctl_mute);
6904 if (rc < 0) {
6905 pr_err("Failed to create ALSA mute control: %d\n", rc);
6906 goto err_exit;
6908 data->ctl_mute_id = &ctl_mute->id;
6910 snd_card_set_dev(card, &tpacpi_pdev->dev);
6911 rc = snd_card_register(card);
6912 if (rc < 0) {
6913 pr_err("Failed to register ALSA card: %d\n", rc);
6914 goto err_exit;
6917 alsa_card = card;
6918 return 0;
6920 err_exit:
6921 snd_card_free(card);
6922 return 1;
6925 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6926 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6928 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6929 /* Whitelist volume level on all IBM by default */
6930 { .vendor = PCI_VENDOR_ID_IBM,
6931 .bios = TPACPI_MATCH_ANY,
6932 .ec = TPACPI_MATCH_ANY,
6933 .quirks = TPACPI_VOL_Q_LEVEL },
6935 /* Lenovo models with volume control (needs confirmation) */
6936 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6937 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6938 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6939 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6940 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6941 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6942 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6944 /* Whitelist mute-only on all Lenovo by default */
6945 { .vendor = PCI_VENDOR_ID_LENOVO,
6946 .bios = TPACPI_MATCH_ANY,
6947 .ec = TPACPI_MATCH_ANY,
6948 .quirks = TPACPI_VOL_Q_MUTEONLY }
6951 static int __init volume_init(struct ibm_init_struct *iibm)
6953 unsigned long quirks;
6954 int rc;
6956 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6958 mutex_init(&volume_mutex);
6961 * Check for module parameter bogosity, note that we
6962 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6963 * able to detect "unspecified"
6965 if (volume_mode > TPACPI_VOL_MODE_MAX)
6966 return -EINVAL;
6968 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6969 pr_err("UCMS step volume mode not implemented, "
6970 "please contact %s\n", TPACPI_MAIL);
6971 return 1;
6974 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6975 return -EINVAL;
6978 * The ALSA mixer is our primary interface.
6979 * When disabled, don't install the subdriver at all
6981 if (!alsa_enable) {
6982 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6983 "ALSA mixer disabled by parameter, "
6984 "not loading volume subdriver...\n");
6985 return 1;
6988 quirks = tpacpi_check_quirks(volume_quirk_table,
6989 ARRAY_SIZE(volume_quirk_table));
6991 switch (volume_capabilities) {
6992 case TPACPI_VOL_CAP_AUTO:
6993 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6994 tp_features.mixer_no_level_control = 1;
6995 else if (quirks & TPACPI_VOL_Q_LEVEL)
6996 tp_features.mixer_no_level_control = 0;
6997 else
6998 return 1; /* no mixer */
6999 break;
7000 case TPACPI_VOL_CAP_VOLMUTE:
7001 tp_features.mixer_no_level_control = 0;
7002 break;
7003 case TPACPI_VOL_CAP_MUTEONLY:
7004 tp_features.mixer_no_level_control = 1;
7005 break;
7006 default:
7007 return 1;
7010 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7011 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7012 "using user-supplied volume_capabilities=%d\n",
7013 volume_capabilities);
7015 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7016 volume_mode == TPACPI_VOL_MODE_MAX) {
7017 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7019 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7020 "driver auto-selected volume_mode=%d\n",
7021 volume_mode);
7022 } else {
7023 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7024 "using user-supplied volume_mode=%d\n",
7025 volume_mode);
7028 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7029 "mute is supported, volume control is %s\n",
7030 str_supported(!tp_features.mixer_no_level_control));
7032 rc = volume_create_alsa_mixer();
7033 if (rc) {
7034 pr_err("Could not create the ALSA mixer interface\n");
7035 return rc;
7038 pr_info("Console audio control enabled, mode: %s\n",
7039 (volume_control_allowed) ?
7040 "override (read/write)" :
7041 "monitor (read only)");
7043 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7044 "registering volume hotkeys as change notification\n");
7045 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7046 | TP_ACPI_HKEY_VOLUP_MASK
7047 | TP_ACPI_HKEY_VOLDWN_MASK
7048 | TP_ACPI_HKEY_MUTE_MASK);
7050 return 0;
7053 static int volume_read(struct seq_file *m)
7055 u8 status;
7057 if (volume_get_status(&status) < 0) {
7058 seq_printf(m, "level:\t\tunreadable\n");
7059 } else {
7060 if (tp_features.mixer_no_level_control)
7061 seq_printf(m, "level:\t\tunsupported\n");
7062 else
7063 seq_printf(m, "level:\t\t%d\n",
7064 status & TP_EC_AUDIO_LVL_MSK);
7066 seq_printf(m, "mute:\t\t%s\n",
7067 onoff(status, TP_EC_AUDIO_MUTESW));
7069 if (volume_control_allowed) {
7070 seq_printf(m, "commands:\tunmute, mute\n");
7071 if (!tp_features.mixer_no_level_control) {
7072 seq_printf(m,
7073 "commands:\tup, down\n");
7074 seq_printf(m,
7075 "commands:\tlevel <level>"
7076 " (<level> is 0-%d)\n",
7077 TP_EC_VOLUME_MAX);
7082 return 0;
7085 static int volume_write(char *buf)
7087 u8 s;
7088 u8 new_level, new_mute;
7089 int l;
7090 char *cmd;
7091 int rc;
7094 * We do allow volume control at driver startup, so that the
7095 * user can set initial state through the volume=... parameter hack.
7097 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7098 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7099 tp_warned.volume_ctrl_forbidden = 1;
7100 pr_notice("Console audio control in monitor mode, "
7101 "changes are not allowed\n");
7102 pr_notice("Use the volume_control=1 module parameter "
7103 "to enable volume control\n");
7105 return -EPERM;
7108 rc = volume_get_status(&s);
7109 if (rc < 0)
7110 return rc;
7112 new_level = s & TP_EC_AUDIO_LVL_MSK;
7113 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7115 while ((cmd = next_cmd(&buf))) {
7116 if (!tp_features.mixer_no_level_control) {
7117 if (strlencmp(cmd, "up") == 0) {
7118 if (new_mute)
7119 new_mute = 0;
7120 else if (new_level < TP_EC_VOLUME_MAX)
7121 new_level++;
7122 continue;
7123 } else if (strlencmp(cmd, "down") == 0) {
7124 if (new_mute)
7125 new_mute = 0;
7126 else if (new_level > 0)
7127 new_level--;
7128 continue;
7129 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7130 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7131 new_level = l;
7132 continue;
7135 if (strlencmp(cmd, "mute") == 0)
7136 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7137 else if (strlencmp(cmd, "unmute") == 0)
7138 new_mute = 0;
7139 else
7140 return -EINVAL;
7143 if (tp_features.mixer_no_level_control) {
7144 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7145 new_mute ? "" : "un");
7146 rc = volume_set_mute(!!new_mute);
7147 } else {
7148 tpacpi_disclose_usertask("procfs volume",
7149 "%smute and set level to %d\n",
7150 new_mute ? "" : "un", new_level);
7151 rc = volume_set_status(new_mute | new_level);
7153 volume_alsa_notify_change();
7155 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7158 static struct ibm_struct volume_driver_data = {
7159 .name = "volume",
7160 .read = volume_read,
7161 .write = volume_write,
7162 .exit = volume_exit,
7163 .suspend = volume_suspend,
7164 .resume = volume_resume,
7165 .shutdown = volume_shutdown,
7168 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7170 #define alsa_card NULL
7172 static void inline volume_alsa_notify_change(void)
7176 static int __init volume_init(struct ibm_init_struct *iibm)
7178 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7180 return 1;
7183 static struct ibm_struct volume_driver_data = {
7184 .name = "volume",
7187 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7189 /*************************************************************************
7190 * Fan subdriver
7194 * FAN ACCESS MODES
7196 * TPACPI_FAN_RD_ACPI_GFAN:
7197 * ACPI GFAN method: returns fan level
7199 * see TPACPI_FAN_WR_ACPI_SFAN
7200 * EC 0x2f (HFSP) not available if GFAN exists
7202 * TPACPI_FAN_WR_ACPI_SFAN:
7203 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7205 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7206 * it for writing.
7208 * TPACPI_FAN_WR_TPEC:
7209 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7210 * Supported on almost all ThinkPads
7212 * Fan speed changes of any sort (including those caused by the
7213 * disengaged mode) are usually done slowly by the firmware as the
7214 * maximum amount of fan duty cycle change per second seems to be
7215 * limited.
7217 * Reading is not available if GFAN exists.
7218 * Writing is not available if SFAN exists.
7220 * Bits
7221 * 7 automatic mode engaged;
7222 * (default operation mode of the ThinkPad)
7223 * fan level is ignored in this mode.
7224 * 6 full speed mode (takes precedence over bit 7);
7225 * not available on all thinkpads. May disable
7226 * the tachometer while the fan controller ramps up
7227 * the speed (which can take up to a few *minutes*).
7228 * Speeds up fan to 100% duty-cycle, which is far above
7229 * the standard RPM levels. It is not impossible that
7230 * it could cause hardware damage.
7231 * 5-3 unused in some models. Extra bits for fan level
7232 * in others, but still useless as all values above
7233 * 7 map to the same speed as level 7 in these models.
7234 * 2-0 fan level (0..7 usually)
7235 * 0x00 = stop
7236 * 0x07 = max (set when temperatures critical)
7237 * Some ThinkPads may have other levels, see
7238 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7240 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7241 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7242 * does so, its initial value is meaningless (0x07).
7244 * For firmware bugs, refer to:
7245 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7247 * ----
7249 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7250 * Main fan tachometer reading (in RPM)
7252 * This register is present on all ThinkPads with a new-style EC, and
7253 * it is known not to be present on the A21m/e, and T22, as there is
7254 * something else in offset 0x84 according to the ACPI DSDT. Other
7255 * ThinkPads from this same time period (and earlier) probably lack the
7256 * tachometer as well.
7258 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7259 * was never fixed by IBM to report the EC firmware version string
7260 * probably support the tachometer (like the early X models), so
7261 * detecting it is quite hard. We need more data to know for sure.
7263 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7264 * might result.
7266 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7267 * mode.
7269 * For firmware bugs, refer to:
7270 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7272 * ----
7274 * ThinkPad EC register 0x31 bit 0 (only on select models)
7276 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7277 * show the speed of the main fan. When bit 0 of EC register 0x31
7278 * is one, the tachometer registers show the speed of the auxiliary
7279 * fan.
7281 * Fan control seems to affect both fans, regardless of the state
7282 * of this bit.
7284 * So far, only the firmware for the X60/X61 non-tablet versions
7285 * seem to support this (firmware TP-7M).
7287 * TPACPI_FAN_WR_ACPI_FANS:
7288 * ThinkPad X31, X40, X41. Not available in the X60.
7290 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7291 * high speed. ACPI DSDT seems to map these three speeds to levels
7292 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7293 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7295 * The speeds are stored on handles
7296 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7298 * There are three default speed sets, accessible as handles:
7299 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7301 * ACPI DSDT switches which set is in use depending on various
7302 * factors.
7304 * TPACPI_FAN_WR_TPEC is also available and should be used to
7305 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7306 * but the ACPI tables just mention level 7.
7309 enum { /* Fan control constants */
7310 fan_status_offset = 0x2f, /* EC register 0x2f */
7311 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7312 * 0x84 must be read before 0x85 */
7313 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7314 bit 0 selects which fan is active */
7316 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7317 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7319 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7322 enum fan_status_access_mode {
7323 TPACPI_FAN_NONE = 0, /* No fan status or control */
7324 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7325 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7328 enum fan_control_access_mode {
7329 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7330 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7331 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7332 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7335 enum fan_control_commands {
7336 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7337 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7338 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7339 * and also watchdog cmd */
7342 static int fan_control_allowed;
7344 static enum fan_status_access_mode fan_status_access_mode;
7345 static enum fan_control_access_mode fan_control_access_mode;
7346 static enum fan_control_commands fan_control_commands;
7348 static u8 fan_control_initial_status;
7349 static u8 fan_control_desired_level;
7350 static u8 fan_control_resume_level;
7351 static int fan_watchdog_maxinterval;
7353 static struct mutex fan_mutex;
7355 static void fan_watchdog_fire(struct work_struct *ignored);
7356 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7358 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7359 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7360 "\\FSPD", /* 600e/x, 770e, 770x */
7361 ); /* all others */
7362 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7363 "JFNS", /* 770x-JL */
7364 ); /* all others */
7367 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7368 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7369 * be in auto mode (0x80).
7371 * This is corrected by any write to HFSP either by the driver, or
7372 * by the firmware.
7374 * We assume 0x07 really means auto mode while this quirk is active,
7375 * as this is far more likely than the ThinkPad being in level 7,
7376 * which is only used by the firmware during thermal emergencies.
7378 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7379 * TP-70 (T43, R52), which are known to be buggy.
7382 static void fan_quirk1_setup(void)
7384 if (fan_control_initial_status == 0x07) {
7385 pr_notice("fan_init: initial fan status is unknown, "
7386 "assuming it is in auto mode\n");
7387 tp_features.fan_ctrl_status_undef = 1;
7391 static void fan_quirk1_handle(u8 *fan_status)
7393 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7394 if (*fan_status != fan_control_initial_status) {
7395 /* something changed the HFSP regisnter since
7396 * driver init time, so it is not undefined
7397 * anymore */
7398 tp_features.fan_ctrl_status_undef = 0;
7399 } else {
7400 /* Return most likely status. In fact, it
7401 * might be the only possible status */
7402 *fan_status = TP_EC_FAN_AUTO;
7407 /* Select main fan on X60/X61, NOOP on others */
7408 static bool fan_select_fan1(void)
7410 if (tp_features.second_fan) {
7411 u8 val;
7413 if (ec_read(fan_select_offset, &val) < 0)
7414 return false;
7415 val &= 0xFEU;
7416 if (ec_write(fan_select_offset, val) < 0)
7417 return false;
7419 return true;
7422 /* Select secondary fan on X60/X61 */
7423 static bool fan_select_fan2(void)
7425 u8 val;
7427 if (!tp_features.second_fan)
7428 return false;
7430 if (ec_read(fan_select_offset, &val) < 0)
7431 return false;
7432 val |= 0x01U;
7433 if (ec_write(fan_select_offset, val) < 0)
7434 return false;
7436 return true;
7440 * Call with fan_mutex held
7442 static void fan_update_desired_level(u8 status)
7444 if ((status &
7445 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7446 if (status > 7)
7447 fan_control_desired_level = 7;
7448 else
7449 fan_control_desired_level = status;
7453 static int fan_get_status(u8 *status)
7455 u8 s;
7457 /* TODO:
7458 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7460 switch (fan_status_access_mode) {
7461 case TPACPI_FAN_RD_ACPI_GFAN:
7462 /* 570, 600e/x, 770e, 770x */
7464 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7465 return -EIO;
7467 if (likely(status))
7468 *status = s & 0x07;
7470 break;
7472 case TPACPI_FAN_RD_TPEC:
7473 /* all except 570, 600e/x, 770e, 770x */
7474 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7475 return -EIO;
7477 if (likely(status)) {
7478 *status = s;
7479 fan_quirk1_handle(status);
7482 break;
7484 default:
7485 return -ENXIO;
7488 return 0;
7491 static int fan_get_status_safe(u8 *status)
7493 int rc;
7494 u8 s;
7496 if (mutex_lock_killable(&fan_mutex))
7497 return -ERESTARTSYS;
7498 rc = fan_get_status(&s);
7499 if (!rc)
7500 fan_update_desired_level(s);
7501 mutex_unlock(&fan_mutex);
7503 if (status)
7504 *status = s;
7506 return rc;
7509 static int fan_get_speed(unsigned int *speed)
7511 u8 hi, lo;
7513 switch (fan_status_access_mode) {
7514 case TPACPI_FAN_RD_TPEC:
7515 /* all except 570, 600e/x, 770e, 770x */
7516 if (unlikely(!fan_select_fan1()))
7517 return -EIO;
7518 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7519 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7520 return -EIO;
7522 if (likely(speed))
7523 *speed = (hi << 8) | lo;
7525 break;
7527 default:
7528 return -ENXIO;
7531 return 0;
7534 static int fan2_get_speed(unsigned int *speed)
7536 u8 hi, lo;
7537 bool rc;
7539 switch (fan_status_access_mode) {
7540 case TPACPI_FAN_RD_TPEC:
7541 /* all except 570, 600e/x, 770e, 770x */
7542 if (unlikely(!fan_select_fan2()))
7543 return -EIO;
7544 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7545 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7546 fan_select_fan1(); /* play it safe */
7547 if (rc)
7548 return -EIO;
7550 if (likely(speed))
7551 *speed = (hi << 8) | lo;
7553 break;
7555 default:
7556 return -ENXIO;
7559 return 0;
7562 static int fan_set_level(int level)
7564 if (!fan_control_allowed)
7565 return -EPERM;
7567 switch (fan_control_access_mode) {
7568 case TPACPI_FAN_WR_ACPI_SFAN:
7569 if (level >= 0 && level <= 7) {
7570 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7571 return -EIO;
7572 } else
7573 return -EINVAL;
7574 break;
7576 case TPACPI_FAN_WR_ACPI_FANS:
7577 case TPACPI_FAN_WR_TPEC:
7578 if (!(level & TP_EC_FAN_AUTO) &&
7579 !(level & TP_EC_FAN_FULLSPEED) &&
7580 ((level < 0) || (level > 7)))
7581 return -EINVAL;
7583 /* safety net should the EC not support AUTO
7584 * or FULLSPEED mode bits and just ignore them */
7585 if (level & TP_EC_FAN_FULLSPEED)
7586 level |= 7; /* safety min speed 7 */
7587 else if (level & TP_EC_FAN_AUTO)
7588 level |= 4; /* safety min speed 4 */
7590 if (!acpi_ec_write(fan_status_offset, level))
7591 return -EIO;
7592 else
7593 tp_features.fan_ctrl_status_undef = 0;
7594 break;
7596 default:
7597 return -ENXIO;
7600 vdbg_printk(TPACPI_DBG_FAN,
7601 "fan control: set fan control register to 0x%02x\n", level);
7602 return 0;
7605 static int fan_set_level_safe(int level)
7607 int rc;
7609 if (!fan_control_allowed)
7610 return -EPERM;
7612 if (mutex_lock_killable(&fan_mutex))
7613 return -ERESTARTSYS;
7615 if (level == TPACPI_FAN_LAST_LEVEL)
7616 level = fan_control_desired_level;
7618 rc = fan_set_level(level);
7619 if (!rc)
7620 fan_update_desired_level(level);
7622 mutex_unlock(&fan_mutex);
7623 return rc;
7626 static int fan_set_enable(void)
7628 u8 s;
7629 int rc;
7631 if (!fan_control_allowed)
7632 return -EPERM;
7634 if (mutex_lock_killable(&fan_mutex))
7635 return -ERESTARTSYS;
7637 switch (fan_control_access_mode) {
7638 case TPACPI_FAN_WR_ACPI_FANS:
7639 case TPACPI_FAN_WR_TPEC:
7640 rc = fan_get_status(&s);
7641 if (rc < 0)
7642 break;
7644 /* Don't go out of emergency fan mode */
7645 if (s != 7) {
7646 s &= 0x07;
7647 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7650 if (!acpi_ec_write(fan_status_offset, s))
7651 rc = -EIO;
7652 else {
7653 tp_features.fan_ctrl_status_undef = 0;
7654 rc = 0;
7656 break;
7658 case TPACPI_FAN_WR_ACPI_SFAN:
7659 rc = fan_get_status(&s);
7660 if (rc < 0)
7661 break;
7663 s &= 0x07;
7665 /* Set fan to at least level 4 */
7666 s |= 4;
7668 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7669 rc = -EIO;
7670 else
7671 rc = 0;
7672 break;
7674 default:
7675 rc = -ENXIO;
7678 mutex_unlock(&fan_mutex);
7680 if (!rc)
7681 vdbg_printk(TPACPI_DBG_FAN,
7682 "fan control: set fan control register to 0x%02x\n",
7684 return rc;
7687 static int fan_set_disable(void)
7689 int rc;
7691 if (!fan_control_allowed)
7692 return -EPERM;
7694 if (mutex_lock_killable(&fan_mutex))
7695 return -ERESTARTSYS;
7697 rc = 0;
7698 switch (fan_control_access_mode) {
7699 case TPACPI_FAN_WR_ACPI_FANS:
7700 case TPACPI_FAN_WR_TPEC:
7701 if (!acpi_ec_write(fan_status_offset, 0x00))
7702 rc = -EIO;
7703 else {
7704 fan_control_desired_level = 0;
7705 tp_features.fan_ctrl_status_undef = 0;
7707 break;
7709 case TPACPI_FAN_WR_ACPI_SFAN:
7710 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7711 rc = -EIO;
7712 else
7713 fan_control_desired_level = 0;
7714 break;
7716 default:
7717 rc = -ENXIO;
7720 if (!rc)
7721 vdbg_printk(TPACPI_DBG_FAN,
7722 "fan control: set fan control register to 0\n");
7724 mutex_unlock(&fan_mutex);
7725 return rc;
7728 static int fan_set_speed(int speed)
7730 int rc;
7732 if (!fan_control_allowed)
7733 return -EPERM;
7735 if (mutex_lock_killable(&fan_mutex))
7736 return -ERESTARTSYS;
7738 rc = 0;
7739 switch (fan_control_access_mode) {
7740 case TPACPI_FAN_WR_ACPI_FANS:
7741 if (speed >= 0 && speed <= 65535) {
7742 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7743 speed, speed, speed))
7744 rc = -EIO;
7745 } else
7746 rc = -EINVAL;
7747 break;
7749 default:
7750 rc = -ENXIO;
7753 mutex_unlock(&fan_mutex);
7754 return rc;
7757 static void fan_watchdog_reset(void)
7759 static int fan_watchdog_active;
7761 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7762 return;
7764 if (fan_watchdog_active)
7765 cancel_delayed_work(&fan_watchdog_task);
7767 if (fan_watchdog_maxinterval > 0 &&
7768 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7769 fan_watchdog_active = 1;
7770 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7771 msecs_to_jiffies(fan_watchdog_maxinterval
7772 * 1000))) {
7773 pr_err("failed to queue the fan watchdog, "
7774 "watchdog will not trigger\n");
7776 } else
7777 fan_watchdog_active = 0;
7780 static void fan_watchdog_fire(struct work_struct *ignored)
7782 int rc;
7784 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7785 return;
7787 pr_notice("fan watchdog: enabling fan\n");
7788 rc = fan_set_enable();
7789 if (rc < 0) {
7790 pr_err("fan watchdog: error %d while enabling fan, "
7791 "will try again later...\n", -rc);
7792 /* reschedule for later */
7793 fan_watchdog_reset();
7798 * SYSFS fan layout: hwmon compatible (device)
7800 * pwm*_enable:
7801 * 0: "disengaged" mode
7802 * 1: manual mode
7803 * 2: native EC "auto" mode (recommended, hardware default)
7805 * pwm*: set speed in manual mode, ignored otherwise.
7806 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7807 * interpolation.
7809 * fan*_input: tachometer reading, RPM
7812 * SYSFS fan layout: extensions
7814 * fan_watchdog (driver):
7815 * fan watchdog interval in seconds, 0 disables (default), max 120
7818 /* sysfs fan pwm1_enable ----------------------------------------------- */
7819 static ssize_t fan_pwm1_enable_show(struct device *dev,
7820 struct device_attribute *attr,
7821 char *buf)
7823 int res, mode;
7824 u8 status;
7826 res = fan_get_status_safe(&status);
7827 if (res)
7828 return res;
7830 if (status & TP_EC_FAN_FULLSPEED) {
7831 mode = 0;
7832 } else if (status & TP_EC_FAN_AUTO) {
7833 mode = 2;
7834 } else
7835 mode = 1;
7837 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7840 static ssize_t fan_pwm1_enable_store(struct device *dev,
7841 struct device_attribute *attr,
7842 const char *buf, size_t count)
7844 unsigned long t;
7845 int res, level;
7847 if (parse_strtoul(buf, 2, &t))
7848 return -EINVAL;
7850 tpacpi_disclose_usertask("hwmon pwm1_enable",
7851 "set fan mode to %lu\n", t);
7853 switch (t) {
7854 case 0:
7855 level = TP_EC_FAN_FULLSPEED;
7856 break;
7857 case 1:
7858 level = TPACPI_FAN_LAST_LEVEL;
7859 break;
7860 case 2:
7861 level = TP_EC_FAN_AUTO;
7862 break;
7863 case 3:
7864 /* reserved for software-controlled auto mode */
7865 return -ENOSYS;
7866 default:
7867 return -EINVAL;
7870 res = fan_set_level_safe(level);
7871 if (res == -ENXIO)
7872 return -EINVAL;
7873 else if (res < 0)
7874 return res;
7876 fan_watchdog_reset();
7878 return count;
7881 static struct device_attribute dev_attr_fan_pwm1_enable =
7882 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7883 fan_pwm1_enable_show, fan_pwm1_enable_store);
7885 /* sysfs fan pwm1 ------------------------------------------------------ */
7886 static ssize_t fan_pwm1_show(struct device *dev,
7887 struct device_attribute *attr,
7888 char *buf)
7890 int res;
7891 u8 status;
7893 res = fan_get_status_safe(&status);
7894 if (res)
7895 return res;
7897 if ((status &
7898 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7899 status = fan_control_desired_level;
7901 if (status > 7)
7902 status = 7;
7904 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7907 static ssize_t fan_pwm1_store(struct device *dev,
7908 struct device_attribute *attr,
7909 const char *buf, size_t count)
7911 unsigned long s;
7912 int rc;
7913 u8 status, newlevel;
7915 if (parse_strtoul(buf, 255, &s))
7916 return -EINVAL;
7918 tpacpi_disclose_usertask("hwmon pwm1",
7919 "set fan speed to %lu\n", s);
7921 /* scale down from 0-255 to 0-7 */
7922 newlevel = (s >> 5) & 0x07;
7924 if (mutex_lock_killable(&fan_mutex))
7925 return -ERESTARTSYS;
7927 rc = fan_get_status(&status);
7928 if (!rc && (status &
7929 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7930 rc = fan_set_level(newlevel);
7931 if (rc == -ENXIO)
7932 rc = -EINVAL;
7933 else if (!rc) {
7934 fan_update_desired_level(newlevel);
7935 fan_watchdog_reset();
7939 mutex_unlock(&fan_mutex);
7940 return (rc)? rc : count;
7943 static struct device_attribute dev_attr_fan_pwm1 =
7944 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7945 fan_pwm1_show, fan_pwm1_store);
7947 /* sysfs fan fan1_input ------------------------------------------------ */
7948 static ssize_t fan_fan1_input_show(struct device *dev,
7949 struct device_attribute *attr,
7950 char *buf)
7952 int res;
7953 unsigned int speed;
7955 res = fan_get_speed(&speed);
7956 if (res < 0)
7957 return res;
7959 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7962 static struct device_attribute dev_attr_fan_fan1_input =
7963 __ATTR(fan1_input, S_IRUGO,
7964 fan_fan1_input_show, NULL);
7966 /* sysfs fan fan2_input ------------------------------------------------ */
7967 static ssize_t fan_fan2_input_show(struct device *dev,
7968 struct device_attribute *attr,
7969 char *buf)
7971 int res;
7972 unsigned int speed;
7974 res = fan2_get_speed(&speed);
7975 if (res < 0)
7976 return res;
7978 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7981 static struct device_attribute dev_attr_fan_fan2_input =
7982 __ATTR(fan2_input, S_IRUGO,
7983 fan_fan2_input_show, NULL);
7985 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7986 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7987 char *buf)
7989 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7992 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7993 const char *buf, size_t count)
7995 unsigned long t;
7997 if (parse_strtoul(buf, 120, &t))
7998 return -EINVAL;
8000 if (!fan_control_allowed)
8001 return -EPERM;
8003 fan_watchdog_maxinterval = t;
8004 fan_watchdog_reset();
8006 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8008 return count;
8011 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8012 fan_fan_watchdog_show, fan_fan_watchdog_store);
8014 /* --------------------------------------------------------------------- */
8015 static struct attribute *fan_attributes[] = {
8016 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8017 &dev_attr_fan_fan1_input.attr,
8018 NULL, /* for fan2_input */
8019 NULL
8022 static const struct attribute_group fan_attr_group = {
8023 .attrs = fan_attributes,
8026 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8027 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8029 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8030 { .vendor = PCI_VENDOR_ID_IBM, \
8031 .bios = TPACPI_MATCH_ANY, \
8032 .ec = TPID(__id1, __id2), \
8033 .quirks = __quirks }
8035 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8036 { .vendor = PCI_VENDOR_ID_LENOVO, \
8037 .bios = TPACPI_MATCH_ANY, \
8038 .ec = TPID(__id1, __id2), \
8039 .quirks = __quirks }
8041 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8042 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8043 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8044 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8045 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8046 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8049 #undef TPACPI_FAN_QL
8050 #undef TPACPI_FAN_QI
8052 static int __init fan_init(struct ibm_init_struct *iibm)
8054 int rc;
8055 unsigned long quirks;
8057 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8058 "initializing fan subdriver\n");
8060 mutex_init(&fan_mutex);
8061 fan_status_access_mode = TPACPI_FAN_NONE;
8062 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8063 fan_control_commands = 0;
8064 fan_watchdog_maxinterval = 0;
8065 tp_features.fan_ctrl_status_undef = 0;
8066 tp_features.second_fan = 0;
8067 fan_control_desired_level = 7;
8069 if (tpacpi_is_ibm()) {
8070 TPACPI_ACPIHANDLE_INIT(fans);
8071 TPACPI_ACPIHANDLE_INIT(gfan);
8072 TPACPI_ACPIHANDLE_INIT(sfan);
8075 quirks = tpacpi_check_quirks(fan_quirk_table,
8076 ARRAY_SIZE(fan_quirk_table));
8078 if (gfan_handle) {
8079 /* 570, 600e/x, 770e, 770x */
8080 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8081 } else {
8082 /* all other ThinkPads: note that even old-style
8083 * ThinkPad ECs supports the fan control register */
8084 if (likely(acpi_ec_read(fan_status_offset,
8085 &fan_control_initial_status))) {
8086 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8087 if (quirks & TPACPI_FAN_Q1)
8088 fan_quirk1_setup();
8089 if (quirks & TPACPI_FAN_2FAN) {
8090 tp_features.second_fan = 1;
8091 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8092 "secondary fan support enabled\n");
8094 } else {
8095 pr_err("ThinkPad ACPI EC access misbehaving, "
8096 "fan status and control unavailable\n");
8097 return 1;
8101 if (sfan_handle) {
8102 /* 570, 770x-JL */
8103 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8104 fan_control_commands |=
8105 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8106 } else {
8107 if (!gfan_handle) {
8108 /* gfan without sfan means no fan control */
8109 /* all other models implement TP EC 0x2f control */
8111 if (fans_handle) {
8112 /* X31, X40, X41 */
8113 fan_control_access_mode =
8114 TPACPI_FAN_WR_ACPI_FANS;
8115 fan_control_commands |=
8116 TPACPI_FAN_CMD_SPEED |
8117 TPACPI_FAN_CMD_LEVEL |
8118 TPACPI_FAN_CMD_ENABLE;
8119 } else {
8120 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8121 fan_control_commands |=
8122 TPACPI_FAN_CMD_LEVEL |
8123 TPACPI_FAN_CMD_ENABLE;
8128 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8129 "fan is %s, modes %d, %d\n",
8130 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8131 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8132 fan_status_access_mode, fan_control_access_mode);
8134 /* fan control master switch */
8135 if (!fan_control_allowed) {
8136 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8137 fan_control_commands = 0;
8138 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8139 "fan control features disabled by parameter\n");
8142 /* update fan_control_desired_level */
8143 if (fan_status_access_mode != TPACPI_FAN_NONE)
8144 fan_get_status_safe(NULL);
8146 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8147 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8148 if (tp_features.second_fan) {
8149 /* attach second fan tachometer */
8150 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8151 &dev_attr_fan_fan2_input.attr;
8153 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8154 &fan_attr_group);
8155 if (rc < 0)
8156 return rc;
8158 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8159 &driver_attr_fan_watchdog);
8160 if (rc < 0) {
8161 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8162 &fan_attr_group);
8163 return rc;
8165 return 0;
8166 } else
8167 return 1;
8170 static void fan_exit(void)
8172 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8173 "cancelling any pending fan watchdog tasks\n");
8175 /* FIXME: can we really do this unconditionally? */
8176 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8177 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8178 &driver_attr_fan_watchdog);
8180 cancel_delayed_work(&fan_watchdog_task);
8181 flush_workqueue(tpacpi_wq);
8184 static void fan_suspend(pm_message_t state)
8186 int rc;
8188 if (!fan_control_allowed)
8189 return;
8191 /* Store fan status in cache */
8192 fan_control_resume_level = 0;
8193 rc = fan_get_status_safe(&fan_control_resume_level);
8194 if (rc < 0)
8195 pr_notice("failed to read fan level for later "
8196 "restore during resume: %d\n", rc);
8198 /* if it is undefined, don't attempt to restore it.
8199 * KEEP THIS LAST */
8200 if (tp_features.fan_ctrl_status_undef)
8201 fan_control_resume_level = 0;
8204 static void fan_resume(void)
8206 u8 current_level = 7;
8207 bool do_set = false;
8208 int rc;
8210 /* DSDT *always* updates status on resume */
8211 tp_features.fan_ctrl_status_undef = 0;
8213 if (!fan_control_allowed ||
8214 !fan_control_resume_level ||
8215 (fan_get_status_safe(&current_level) < 0))
8216 return;
8218 switch (fan_control_access_mode) {
8219 case TPACPI_FAN_WR_ACPI_SFAN:
8220 /* never decrease fan level */
8221 do_set = (fan_control_resume_level > current_level);
8222 break;
8223 case TPACPI_FAN_WR_ACPI_FANS:
8224 case TPACPI_FAN_WR_TPEC:
8225 /* never decrease fan level, scale is:
8226 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8228 * We expect the firmware to set either 7 or AUTO, but we
8229 * handle FULLSPEED out of paranoia.
8231 * So, we can safely only restore FULLSPEED or 7, anything
8232 * else could slow the fan. Restoring AUTO is useless, at
8233 * best that's exactly what the DSDT already set (it is the
8234 * slower it uses).
8236 * Always keep in mind that the DSDT *will* have set the
8237 * fans to what the vendor supposes is the best level. We
8238 * muck with it only to speed the fan up.
8240 if (fan_control_resume_level != 7 &&
8241 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8242 return;
8243 else
8244 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8245 (current_level != fan_control_resume_level);
8246 break;
8247 default:
8248 return;
8250 if (do_set) {
8251 pr_notice("restoring fan level to 0x%02x\n",
8252 fan_control_resume_level);
8253 rc = fan_set_level_safe(fan_control_resume_level);
8254 if (rc < 0)
8255 pr_notice("failed to restore fan level: %d\n", rc);
8259 static int fan_read(struct seq_file *m)
8261 int rc;
8262 u8 status;
8263 unsigned int speed = 0;
8265 switch (fan_status_access_mode) {
8266 case TPACPI_FAN_RD_ACPI_GFAN:
8267 /* 570, 600e/x, 770e, 770x */
8268 rc = fan_get_status_safe(&status);
8269 if (rc < 0)
8270 return rc;
8272 seq_printf(m, "status:\t\t%s\n"
8273 "level:\t\t%d\n",
8274 (status != 0) ? "enabled" : "disabled", status);
8275 break;
8277 case TPACPI_FAN_RD_TPEC:
8278 /* all except 570, 600e/x, 770e, 770x */
8279 rc = fan_get_status_safe(&status);
8280 if (rc < 0)
8281 return rc;
8283 seq_printf(m, "status:\t\t%s\n",
8284 (status != 0) ? "enabled" : "disabled");
8286 rc = fan_get_speed(&speed);
8287 if (rc < 0)
8288 return rc;
8290 seq_printf(m, "speed:\t\t%d\n", speed);
8292 if (status & TP_EC_FAN_FULLSPEED)
8293 /* Disengaged mode takes precedence */
8294 seq_printf(m, "level:\t\tdisengaged\n");
8295 else if (status & TP_EC_FAN_AUTO)
8296 seq_printf(m, "level:\t\tauto\n");
8297 else
8298 seq_printf(m, "level:\t\t%d\n", status);
8299 break;
8301 case TPACPI_FAN_NONE:
8302 default:
8303 seq_printf(m, "status:\t\tnot supported\n");
8306 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8307 seq_printf(m, "commands:\tlevel <level>");
8309 switch (fan_control_access_mode) {
8310 case TPACPI_FAN_WR_ACPI_SFAN:
8311 seq_printf(m, " (<level> is 0-7)\n");
8312 break;
8314 default:
8315 seq_printf(m, " (<level> is 0-7, "
8316 "auto, disengaged, full-speed)\n");
8317 break;
8321 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8322 seq_printf(m, "commands:\tenable, disable\n"
8323 "commands:\twatchdog <timeout> (<timeout> "
8324 "is 0 (off), 1-120 (seconds))\n");
8326 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8327 seq_printf(m, "commands:\tspeed <speed>"
8328 " (<speed> is 0-65535)\n");
8330 return 0;
8333 static int fan_write_cmd_level(const char *cmd, int *rc)
8335 int level;
8337 if (strlencmp(cmd, "level auto") == 0)
8338 level = TP_EC_FAN_AUTO;
8339 else if ((strlencmp(cmd, "level disengaged") == 0) |
8340 (strlencmp(cmd, "level full-speed") == 0))
8341 level = TP_EC_FAN_FULLSPEED;
8342 else if (sscanf(cmd, "level %d", &level) != 1)
8343 return 0;
8345 *rc = fan_set_level_safe(level);
8346 if (*rc == -ENXIO)
8347 pr_err("level command accepted for unsupported access mode %d\n",
8348 fan_control_access_mode);
8349 else if (!*rc)
8350 tpacpi_disclose_usertask("procfs fan",
8351 "set level to %d\n", level);
8353 return 1;
8356 static int fan_write_cmd_enable(const char *cmd, int *rc)
8358 if (strlencmp(cmd, "enable") != 0)
8359 return 0;
8361 *rc = fan_set_enable();
8362 if (*rc == -ENXIO)
8363 pr_err("enable command accepted for unsupported access mode %d\n",
8364 fan_control_access_mode);
8365 else if (!*rc)
8366 tpacpi_disclose_usertask("procfs fan", "enable\n");
8368 return 1;
8371 static int fan_write_cmd_disable(const char *cmd, int *rc)
8373 if (strlencmp(cmd, "disable") != 0)
8374 return 0;
8376 *rc = fan_set_disable();
8377 if (*rc == -ENXIO)
8378 pr_err("disable command accepted for unsupported access mode %d\n",
8379 fan_control_access_mode);
8380 else if (!*rc)
8381 tpacpi_disclose_usertask("procfs fan", "disable\n");
8383 return 1;
8386 static int fan_write_cmd_speed(const char *cmd, int *rc)
8388 int speed;
8390 /* TODO:
8391 * Support speed <low> <medium> <high> ? */
8393 if (sscanf(cmd, "speed %d", &speed) != 1)
8394 return 0;
8396 *rc = fan_set_speed(speed);
8397 if (*rc == -ENXIO)
8398 pr_err("speed command accepted for unsupported access mode %d\n",
8399 fan_control_access_mode);
8400 else if (!*rc)
8401 tpacpi_disclose_usertask("procfs fan",
8402 "set speed to %d\n", speed);
8404 return 1;
8407 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8409 int interval;
8411 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8412 return 0;
8414 if (interval < 0 || interval > 120)
8415 *rc = -EINVAL;
8416 else {
8417 fan_watchdog_maxinterval = interval;
8418 tpacpi_disclose_usertask("procfs fan",
8419 "set watchdog timer to %d\n",
8420 interval);
8423 return 1;
8426 static int fan_write(char *buf)
8428 char *cmd;
8429 int rc = 0;
8431 while (!rc && (cmd = next_cmd(&buf))) {
8432 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8433 fan_write_cmd_level(cmd, &rc)) &&
8434 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8435 (fan_write_cmd_enable(cmd, &rc) ||
8436 fan_write_cmd_disable(cmd, &rc) ||
8437 fan_write_cmd_watchdog(cmd, &rc))) &&
8438 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8439 fan_write_cmd_speed(cmd, &rc))
8441 rc = -EINVAL;
8442 else if (!rc)
8443 fan_watchdog_reset();
8446 return rc;
8449 static struct ibm_struct fan_driver_data = {
8450 .name = "fan",
8451 .read = fan_read,
8452 .write = fan_write,
8453 .exit = fan_exit,
8454 .suspend = fan_suspend,
8455 .resume = fan_resume,
8458 /****************************************************************************
8459 ****************************************************************************
8461 * Infrastructure
8463 ****************************************************************************
8464 ****************************************************************************/
8467 * HKEY event callout for other subdrivers go here
8468 * (yes, it is ugly, but it is quick, safe, and gets the job done
8470 static void tpacpi_driver_event(const unsigned int hkey_event)
8472 if (ibm_backlight_device) {
8473 switch (hkey_event) {
8474 case TP_HKEY_EV_BRGHT_UP:
8475 case TP_HKEY_EV_BRGHT_DOWN:
8476 tpacpi_brightness_notify_change();
8479 if (alsa_card) {
8480 switch (hkey_event) {
8481 case TP_HKEY_EV_VOL_UP:
8482 case TP_HKEY_EV_VOL_DOWN:
8483 case TP_HKEY_EV_VOL_MUTE:
8484 volume_alsa_notify_change();
8489 static void hotkey_driver_event(const unsigned int scancode)
8491 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8494 /* sysfs name ---------------------------------------------------------- */
8495 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8496 struct device_attribute *attr,
8497 char *buf)
8499 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8502 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8503 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8505 /* --------------------------------------------------------------------- */
8507 /* /proc support */
8508 static struct proc_dir_entry *proc_dir;
8511 * Module and infrastructure proble, init and exit handling
8514 static int force_load;
8516 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8517 static const char * __init str_supported(int is_supported)
8519 static char text_unsupported[] __initdata = "not supported";
8521 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8523 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8525 static void ibm_exit(struct ibm_struct *ibm)
8527 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8529 list_del_init(&ibm->all_drivers);
8531 if (ibm->flags.acpi_notify_installed) {
8532 dbg_printk(TPACPI_DBG_EXIT,
8533 "%s: acpi_remove_notify_handler\n", ibm->name);
8534 BUG_ON(!ibm->acpi);
8535 acpi_remove_notify_handler(*ibm->acpi->handle,
8536 ibm->acpi->type,
8537 dispatch_acpi_notify);
8538 ibm->flags.acpi_notify_installed = 0;
8541 if (ibm->flags.proc_created) {
8542 dbg_printk(TPACPI_DBG_EXIT,
8543 "%s: remove_proc_entry\n", ibm->name);
8544 remove_proc_entry(ibm->name, proc_dir);
8545 ibm->flags.proc_created = 0;
8548 if (ibm->flags.acpi_driver_registered) {
8549 dbg_printk(TPACPI_DBG_EXIT,
8550 "%s: acpi_bus_unregister_driver\n", ibm->name);
8551 BUG_ON(!ibm->acpi);
8552 acpi_bus_unregister_driver(ibm->acpi->driver);
8553 kfree(ibm->acpi->driver);
8554 ibm->acpi->driver = NULL;
8555 ibm->flags.acpi_driver_registered = 0;
8558 if (ibm->flags.init_called && ibm->exit) {
8559 ibm->exit();
8560 ibm->flags.init_called = 0;
8563 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8566 static int __init ibm_init(struct ibm_init_struct *iibm)
8568 int ret;
8569 struct ibm_struct *ibm = iibm->data;
8570 struct proc_dir_entry *entry;
8572 BUG_ON(ibm == NULL);
8574 INIT_LIST_HEAD(&ibm->all_drivers);
8576 if (ibm->flags.experimental && !experimental)
8577 return 0;
8579 dbg_printk(TPACPI_DBG_INIT,
8580 "probing for %s\n", ibm->name);
8582 if (iibm->init) {
8583 ret = iibm->init(iibm);
8584 if (ret > 0)
8585 return 0; /* probe failed */
8586 if (ret)
8587 return ret;
8589 ibm->flags.init_called = 1;
8592 if (ibm->acpi) {
8593 if (ibm->acpi->hid) {
8594 ret = register_tpacpi_subdriver(ibm);
8595 if (ret)
8596 goto err_out;
8599 if (ibm->acpi->notify) {
8600 ret = setup_acpi_notify(ibm);
8601 if (ret == -ENODEV) {
8602 pr_notice("disabling subdriver %s\n",
8603 ibm->name);
8604 ret = 0;
8605 goto err_out;
8607 if (ret < 0)
8608 goto err_out;
8612 dbg_printk(TPACPI_DBG_INIT,
8613 "%s installed\n", ibm->name);
8615 if (ibm->read) {
8616 mode_t mode = iibm->base_procfs_mode;
8618 if (!mode)
8619 mode = S_IRUGO;
8620 if (ibm->write)
8621 mode |= S_IWUSR;
8622 entry = proc_create_data(ibm->name, mode, proc_dir,
8623 &dispatch_proc_fops, ibm);
8624 if (!entry) {
8625 pr_err("unable to create proc entry %s\n", ibm->name);
8626 ret = -ENODEV;
8627 goto err_out;
8629 ibm->flags.proc_created = 1;
8632 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8634 return 0;
8636 err_out:
8637 dbg_printk(TPACPI_DBG_INIT,
8638 "%s: at error exit path with result %d\n",
8639 ibm->name, ret);
8641 ibm_exit(ibm);
8642 return (ret < 0)? ret : 0;
8645 /* Probing */
8647 static bool __pure __init tpacpi_is_fw_digit(const char c)
8649 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8652 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8653 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8654 const char t)
8656 return s && strlen(s) >= 8 &&
8657 tpacpi_is_fw_digit(s[0]) &&
8658 tpacpi_is_fw_digit(s[1]) &&
8659 s[2] == t && s[3] == 'T' &&
8660 tpacpi_is_fw_digit(s[4]) &&
8661 tpacpi_is_fw_digit(s[5]);
8664 /* returns 0 - probe ok, or < 0 - probe error.
8665 * Probe ok doesn't mean thinkpad found.
8666 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8667 static int __must_check __init get_thinkpad_model_data(
8668 struct thinkpad_id_data *tp)
8670 const struct dmi_device *dev = NULL;
8671 char ec_fw_string[18];
8672 char const *s;
8674 if (!tp)
8675 return -EINVAL;
8677 memset(tp, 0, sizeof(*tp));
8679 if (dmi_name_in_vendors("IBM"))
8680 tp->vendor = PCI_VENDOR_ID_IBM;
8681 else if (dmi_name_in_vendors("LENOVO"))
8682 tp->vendor = PCI_VENDOR_ID_LENOVO;
8683 else
8684 return 0;
8686 s = dmi_get_system_info(DMI_BIOS_VERSION);
8687 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8688 if (s && !tp->bios_version_str)
8689 return -ENOMEM;
8691 /* Really ancient ThinkPad 240X will fail this, which is fine */
8692 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8693 return 0;
8695 tp->bios_model = tp->bios_version_str[0]
8696 | (tp->bios_version_str[1] << 8);
8697 tp->bios_release = (tp->bios_version_str[4] << 8)
8698 | tp->bios_version_str[5];
8701 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8702 * X32 or newer, all Z series; Some models must have an
8703 * up-to-date BIOS or they will not be detected.
8705 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8707 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8708 if (sscanf(dev->name,
8709 "IBM ThinkPad Embedded Controller -[%17c",
8710 ec_fw_string) == 1) {
8711 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8712 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8714 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8715 if (!tp->ec_version_str)
8716 return -ENOMEM;
8718 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8719 tp->ec_model = ec_fw_string[0]
8720 | (ec_fw_string[1] << 8);
8721 tp->ec_release = (ec_fw_string[4] << 8)
8722 | ec_fw_string[5];
8723 } else {
8724 pr_notice("ThinkPad firmware release %s "
8725 "doesn't match the known patterns\n",
8726 ec_fw_string);
8727 pr_notice("please report this to %s\n",
8728 TPACPI_MAIL);
8730 break;
8734 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8735 if (s && !strnicmp(s, "ThinkPad", 8)) {
8736 tp->model_str = kstrdup(s, GFP_KERNEL);
8737 if (!tp->model_str)
8738 return -ENOMEM;
8741 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8742 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8743 if (s && !tp->nummodel_str)
8744 return -ENOMEM;
8746 return 0;
8749 static int __init probe_for_thinkpad(void)
8751 int is_thinkpad;
8753 if (acpi_disabled)
8754 return -ENODEV;
8756 /* It would be dangerous to run the driver in this case */
8757 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8758 return -ENODEV;
8761 * Non-ancient models have better DMI tagging, but very old models
8762 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8764 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8765 (thinkpad_id.ec_model != 0) ||
8766 tpacpi_is_fw_known();
8768 /* The EC handler is required */
8769 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8770 if (!ec_handle) {
8771 if (is_thinkpad)
8772 pr_err("Not yet supported ThinkPad detected!\n");
8773 return -ENODEV;
8776 if (!is_thinkpad && !force_load)
8777 return -ENODEV;
8779 return 0;
8782 static void __init thinkpad_acpi_init_banner(void)
8784 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8785 pr_info("%s\n", TPACPI_URL);
8787 pr_info("ThinkPad BIOS %s, EC %s\n",
8788 (thinkpad_id.bios_version_str) ?
8789 thinkpad_id.bios_version_str : "unknown",
8790 (thinkpad_id.ec_version_str) ?
8791 thinkpad_id.ec_version_str : "unknown");
8793 BUG_ON(!thinkpad_id.vendor);
8795 if (thinkpad_id.model_str)
8796 pr_info("%s %s, model %s\n",
8797 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8798 "IBM" : ((thinkpad_id.vendor ==
8799 PCI_VENDOR_ID_LENOVO) ?
8800 "Lenovo" : "Unknown vendor"),
8801 thinkpad_id.model_str,
8802 (thinkpad_id.nummodel_str) ?
8803 thinkpad_id.nummodel_str : "unknown");
8806 /* Module init, exit, parameters */
8808 static struct ibm_init_struct ibms_init[] __initdata = {
8810 .data = &thinkpad_acpi_driver_data,
8813 .init = hotkey_init,
8814 .data = &hotkey_driver_data,
8817 .init = bluetooth_init,
8818 .data = &bluetooth_driver_data,
8821 .init = wan_init,
8822 .data = &wan_driver_data,
8825 .init = uwb_init,
8826 .data = &uwb_driver_data,
8828 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8830 .init = video_init,
8831 .base_procfs_mode = S_IRUSR,
8832 .data = &video_driver_data,
8834 #endif
8836 .init = light_init,
8837 .data = &light_driver_data,
8840 .init = cmos_init,
8841 .data = &cmos_driver_data,
8844 .init = led_init,
8845 .data = &led_driver_data,
8848 .init = beep_init,
8849 .data = &beep_driver_data,
8852 .init = thermal_init,
8853 .data = &thermal_driver_data,
8856 .data = &ecdump_driver_data,
8859 .init = brightness_init,
8860 .data = &brightness_driver_data,
8863 .init = volume_init,
8864 .data = &volume_driver_data,
8867 .init = fan_init,
8868 .data = &fan_driver_data,
8872 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8874 unsigned int i;
8875 struct ibm_struct *ibm;
8877 if (!kp || !kp->name || !val)
8878 return -EINVAL;
8880 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8881 ibm = ibms_init[i].data;
8882 WARN_ON(ibm == NULL);
8884 if (!ibm || !ibm->name)
8885 continue;
8887 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8888 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8889 return -ENOSPC;
8890 strcpy(ibms_init[i].param, val);
8891 strcat(ibms_init[i].param, ",");
8892 return 0;
8896 return -EINVAL;
8899 module_param(experimental, int, 0444);
8900 MODULE_PARM_DESC(experimental,
8901 "Enables experimental features when non-zero");
8903 module_param_named(debug, dbg_level, uint, 0);
8904 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8906 module_param(force_load, bool, 0444);
8907 MODULE_PARM_DESC(force_load,
8908 "Attempts to load the driver even on a "
8909 "mis-identified ThinkPad when true");
8911 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8912 MODULE_PARM_DESC(fan_control,
8913 "Enables setting fan parameters features when true");
8915 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8916 MODULE_PARM_DESC(brightness_mode,
8917 "Selects brightness control strategy: "
8918 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8920 module_param(brightness_enable, uint, 0444);
8921 MODULE_PARM_DESC(brightness_enable,
8922 "Enables backlight control when 1, disables when 0");
8924 module_param(hotkey_report_mode, uint, 0444);
8925 MODULE_PARM_DESC(hotkey_report_mode,
8926 "used for backwards compatibility with userspace, "
8927 "see documentation");
8929 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8930 module_param_named(volume_mode, volume_mode, uint, 0444);
8931 MODULE_PARM_DESC(volume_mode,
8932 "Selects volume control strategy: "
8933 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8935 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8936 MODULE_PARM_DESC(volume_capabilities,
8937 "Selects the mixer capabilites: "
8938 "0=auto, 1=volume and mute, 2=mute only");
8940 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8941 MODULE_PARM_DESC(volume_control,
8942 "Enables software override for the console audio "
8943 "control when true");
8945 /* ALSA module API parameters */
8946 module_param_named(index, alsa_index, int, 0444);
8947 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8948 module_param_named(id, alsa_id, charp, 0444);
8949 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8950 module_param_named(enable, alsa_enable, bool, 0444);
8951 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8952 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8954 #define TPACPI_PARAM(feature) \
8955 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8956 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8957 "at module load, see documentation")
8959 TPACPI_PARAM(hotkey);
8960 TPACPI_PARAM(bluetooth);
8961 TPACPI_PARAM(video);
8962 TPACPI_PARAM(light);
8963 TPACPI_PARAM(cmos);
8964 TPACPI_PARAM(led);
8965 TPACPI_PARAM(beep);
8966 TPACPI_PARAM(ecdump);
8967 TPACPI_PARAM(brightness);
8968 TPACPI_PARAM(volume);
8969 TPACPI_PARAM(fan);
8971 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8972 module_param(dbg_wlswemul, uint, 0444);
8973 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8974 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8975 MODULE_PARM_DESC(wlsw_state,
8976 "Initial state of the emulated WLSW switch");
8978 module_param(dbg_bluetoothemul, uint, 0444);
8979 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8980 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8981 MODULE_PARM_DESC(bluetooth_state,
8982 "Initial state of the emulated bluetooth switch");
8984 module_param(dbg_wwanemul, uint, 0444);
8985 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8986 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8987 MODULE_PARM_DESC(wwan_state,
8988 "Initial state of the emulated WWAN switch");
8990 module_param(dbg_uwbemul, uint, 0444);
8991 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8992 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8993 MODULE_PARM_DESC(uwb_state,
8994 "Initial state of the emulated UWB switch");
8995 #endif
8997 static void thinkpad_acpi_module_exit(void)
8999 struct ibm_struct *ibm, *itmp;
9001 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9003 list_for_each_entry_safe_reverse(ibm, itmp,
9004 &tpacpi_all_drivers,
9005 all_drivers) {
9006 ibm_exit(ibm);
9009 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9011 if (tpacpi_inputdev) {
9012 if (tp_features.input_device_registered)
9013 input_unregister_device(tpacpi_inputdev);
9014 else
9015 input_free_device(tpacpi_inputdev);
9018 if (tpacpi_hwmon)
9019 hwmon_device_unregister(tpacpi_hwmon);
9021 if (tp_features.sensors_pdev_attrs_registered)
9022 device_remove_file(&tpacpi_sensors_pdev->dev,
9023 &dev_attr_thinkpad_acpi_pdev_name);
9024 if (tpacpi_sensors_pdev)
9025 platform_device_unregister(tpacpi_sensors_pdev);
9026 if (tpacpi_pdev)
9027 platform_device_unregister(tpacpi_pdev);
9029 if (tp_features.sensors_pdrv_attrs_registered)
9030 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9031 if (tp_features.platform_drv_attrs_registered)
9032 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9034 if (tp_features.sensors_pdrv_registered)
9035 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9037 if (tp_features.platform_drv_registered)
9038 platform_driver_unregister(&tpacpi_pdriver);
9040 if (proc_dir)
9041 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9043 if (tpacpi_wq)
9044 destroy_workqueue(tpacpi_wq);
9046 kfree(thinkpad_id.bios_version_str);
9047 kfree(thinkpad_id.ec_version_str);
9048 kfree(thinkpad_id.model_str);
9052 static int __init thinkpad_acpi_module_init(void)
9054 int ret, i;
9056 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9058 /* Parameter checking */
9059 if (hotkey_report_mode > 2)
9060 return -EINVAL;
9062 /* Driver-level probe */
9064 ret = get_thinkpad_model_data(&thinkpad_id);
9065 if (ret) {
9066 pr_err("unable to get DMI data: %d\n", ret);
9067 thinkpad_acpi_module_exit();
9068 return ret;
9070 ret = probe_for_thinkpad();
9071 if (ret) {
9072 thinkpad_acpi_module_exit();
9073 return ret;
9076 /* Driver initialization */
9078 thinkpad_acpi_init_banner();
9079 tpacpi_check_outdated_fw();
9081 TPACPI_ACPIHANDLE_INIT(ecrd);
9082 TPACPI_ACPIHANDLE_INIT(ecwr);
9084 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9085 if (!tpacpi_wq) {
9086 thinkpad_acpi_module_exit();
9087 return -ENOMEM;
9090 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9091 if (!proc_dir) {
9092 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9093 thinkpad_acpi_module_exit();
9094 return -ENODEV;
9097 ret = platform_driver_register(&tpacpi_pdriver);
9098 if (ret) {
9099 pr_err("unable to register main platform driver\n");
9100 thinkpad_acpi_module_exit();
9101 return ret;
9103 tp_features.platform_drv_registered = 1;
9105 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9106 if (ret) {
9107 pr_err("unable to register hwmon platform driver\n");
9108 thinkpad_acpi_module_exit();
9109 return ret;
9111 tp_features.sensors_pdrv_registered = 1;
9113 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9114 if (!ret) {
9115 tp_features.platform_drv_attrs_registered = 1;
9116 ret = tpacpi_create_driver_attributes(
9117 &tpacpi_hwmon_pdriver.driver);
9119 if (ret) {
9120 pr_err("unable to create sysfs driver attributes\n");
9121 thinkpad_acpi_module_exit();
9122 return ret;
9124 tp_features.sensors_pdrv_attrs_registered = 1;
9127 /* Device initialization */
9128 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9129 NULL, 0);
9130 if (IS_ERR(tpacpi_pdev)) {
9131 ret = PTR_ERR(tpacpi_pdev);
9132 tpacpi_pdev = NULL;
9133 pr_err("unable to register platform device\n");
9134 thinkpad_acpi_module_exit();
9135 return ret;
9137 tpacpi_sensors_pdev = platform_device_register_simple(
9138 TPACPI_HWMON_DRVR_NAME,
9139 -1, NULL, 0);
9140 if (IS_ERR(tpacpi_sensors_pdev)) {
9141 ret = PTR_ERR(tpacpi_sensors_pdev);
9142 tpacpi_sensors_pdev = NULL;
9143 pr_err("unable to register hwmon platform device\n");
9144 thinkpad_acpi_module_exit();
9145 return ret;
9147 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9148 &dev_attr_thinkpad_acpi_pdev_name);
9149 if (ret) {
9150 pr_err("unable to create sysfs hwmon device attributes\n");
9151 thinkpad_acpi_module_exit();
9152 return ret;
9154 tp_features.sensors_pdev_attrs_registered = 1;
9155 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9156 if (IS_ERR(tpacpi_hwmon)) {
9157 ret = PTR_ERR(tpacpi_hwmon);
9158 tpacpi_hwmon = NULL;
9159 pr_err("unable to register hwmon device\n");
9160 thinkpad_acpi_module_exit();
9161 return ret;
9163 mutex_init(&tpacpi_inputdev_send_mutex);
9164 tpacpi_inputdev = input_allocate_device();
9165 if (!tpacpi_inputdev) {
9166 pr_err("unable to allocate input device\n");
9167 thinkpad_acpi_module_exit();
9168 return -ENOMEM;
9169 } else {
9170 /* Prepare input device, but don't register */
9171 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9172 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9173 tpacpi_inputdev->id.bustype = BUS_HOST;
9174 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9175 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9176 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9177 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9180 /* Init subdriver dependencies */
9181 tpacpi_detect_brightness_capabilities();
9183 /* Init subdrivers */
9184 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9185 ret = ibm_init(&ibms_init[i]);
9186 if (ret >= 0 && *ibms_init[i].param)
9187 ret = ibms_init[i].data->write(ibms_init[i].param);
9188 if (ret < 0) {
9189 thinkpad_acpi_module_exit();
9190 return ret;
9194 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9196 ret = input_register_device(tpacpi_inputdev);
9197 if (ret < 0) {
9198 pr_err("unable to register input device\n");
9199 thinkpad_acpi_module_exit();
9200 return ret;
9201 } else {
9202 tp_features.input_device_registered = 1;
9205 return 0;
9208 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9211 * This will autoload the driver in almost every ThinkPad
9212 * in widespread use.
9214 * Only _VERY_ old models, like the 240, 240x and 570 lack
9215 * the HKEY event interface.
9217 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9220 * DMI matching for module autoloading
9222 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9223 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9225 * Only models listed in thinkwiki will be supported, so add yours
9226 * if it is not there yet.
9228 #define IBM_BIOS_MODULE_ALIAS(__type) \
9229 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9231 /* Ancient thinkpad BIOSes have to be identified by
9232 * BIOS type or model number, and there are far less
9233 * BIOS types than model numbers... */
9234 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9236 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9237 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9238 MODULE_DESCRIPTION(TPACPI_DESC);
9239 MODULE_VERSION(TPACPI_VERSION);
9240 MODULE_LICENSE("GPL");
9242 module_init(thinkpad_acpi_module_init);
9243 module_exit(thinkpad_acpi_module_exit);