thinkpad-acpi: make volume subdriver optional
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob6024db0eaeb23d64a7d88080cbd74cad6683a7fe
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 TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* ACPI HIDs */
125 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
126 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
127 #define TPACPI_ACPI_EC_HID "PNP0C09"
129 /* Input IDs */
130 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
131 #define TPACPI_HKEY_INPUT_VERSION 0x4101
133 /* ACPI \WGSV commands */
134 enum {
135 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
136 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
137 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
138 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
141 /* TP_ACPI_WGSV_GET_STATE bits */
142 enum {
143 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
144 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
145 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
146 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
147 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
148 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
149 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
150 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
151 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
152 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
155 /* HKEY events */
156 enum tpacpi_hkey_event_t {
157 /* Hotkey-related */
158 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
159 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
160 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
161 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
162 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
163 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
165 /* Reasons for waking up from S3/S4 */
166 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
167 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
168 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
169 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
170 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
171 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
173 /* Auto-sleep after eject request */
174 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
175 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
177 /* Misc bay events */
178 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
180 /* User-interface events */
181 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
182 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
183 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
184 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
185 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
186 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
187 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
189 /* Thermal events */
190 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
191 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
192 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
193 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
194 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
196 /* Misc */
197 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
200 /****************************************************************************
201 * Main driver
204 #define TPACPI_NAME "thinkpad"
205 #define TPACPI_DESC "ThinkPad ACPI Extras"
206 #define TPACPI_FILE TPACPI_NAME "_acpi"
207 #define TPACPI_URL "http://ibm-acpi.sf.net/"
208 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
210 #define TPACPI_PROC_DIR "ibm"
211 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
212 #define TPACPI_DRVR_NAME TPACPI_FILE
213 #define TPACPI_DRVR_SHORTNAME "tpacpi"
214 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
216 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
217 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
219 #define TPACPI_MAX_ACPI_ARGS 3
221 /* printk headers */
222 #define TPACPI_LOG TPACPI_FILE ": "
223 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
224 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
225 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
226 #define TPACPI_ERR KERN_ERR TPACPI_LOG
227 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
228 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
229 #define TPACPI_INFO KERN_INFO TPACPI_LOG
230 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
232 /* Debugging printk groups */
233 #define TPACPI_DBG_ALL 0xffff
234 #define TPACPI_DBG_DISCLOSETASK 0x8000
235 #define TPACPI_DBG_INIT 0x0001
236 #define TPACPI_DBG_EXIT 0x0002
237 #define TPACPI_DBG_RFKILL 0x0004
238 #define TPACPI_DBG_HKEY 0x0008
239 #define TPACPI_DBG_FAN 0x0010
240 #define TPACPI_DBG_BRGHT 0x0020
241 #define TPACPI_DBG_MIXER 0x0040
243 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
244 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
245 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
248 /****************************************************************************
249 * Driver-wide structs and misc. variables
252 struct ibm_struct;
254 struct tp_acpi_drv_struct {
255 const struct acpi_device_id *hid;
256 struct acpi_driver *driver;
258 void (*notify) (struct ibm_struct *, u32);
259 acpi_handle *handle;
260 u32 type;
261 struct acpi_device *device;
264 struct ibm_struct {
265 char *name;
267 int (*read) (struct seq_file *);
268 int (*write) (char *);
269 void (*exit) (void);
270 void (*resume) (void);
271 void (*suspend) (pm_message_t state);
272 void (*shutdown) (void);
274 struct list_head all_drivers;
276 struct tp_acpi_drv_struct *acpi;
278 struct {
279 u8 acpi_driver_registered:1;
280 u8 acpi_notify_installed:1;
281 u8 proc_created:1;
282 u8 init_called:1;
283 u8 experimental:1;
284 } flags;
287 struct ibm_init_struct {
288 char param[32];
290 int (*init) (struct ibm_init_struct *);
291 mode_t base_procfs_mode;
292 struct ibm_struct *data;
295 static struct {
296 u32 bluetooth:1;
297 u32 hotkey:1;
298 u32 hotkey_mask:1;
299 u32 hotkey_wlsw:1;
300 u32 hotkey_tablet:1;
301 u32 light:1;
302 u32 light_status:1;
303 u32 bright_16levels:1;
304 u32 bright_acpimode:1;
305 u32 wan:1;
306 u32 uwb:1;
307 u32 fan_ctrl_status_undef:1;
308 u32 second_fan:1;
309 u32 beep_needs_two_args:1;
310 u32 mixer_no_level_control:1;
311 u32 input_device_registered:1;
312 u32 platform_drv_registered:1;
313 u32 platform_drv_attrs_registered:1;
314 u32 sensors_pdrv_registered:1;
315 u32 sensors_pdrv_attrs_registered:1;
316 u32 sensors_pdev_attrs_registered:1;
317 u32 hotkey_poll_active:1;
318 } tp_features;
320 static struct {
321 u16 hotkey_mask_ff:1;
322 u16 volume_ctrl_forbidden:1;
323 } tp_warned;
325 struct thinkpad_id_data {
326 unsigned int vendor; /* ThinkPad vendor:
327 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
329 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
330 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
332 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
333 u16 ec_model;
334 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
335 u16 ec_release;
337 char *model_str; /* ThinkPad T43 */
338 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
340 static struct thinkpad_id_data thinkpad_id;
342 static enum {
343 TPACPI_LIFE_INIT = 0,
344 TPACPI_LIFE_RUNNING,
345 TPACPI_LIFE_EXITING,
346 } tpacpi_lifecycle;
348 static int experimental;
349 static u32 dbg_level;
351 static struct workqueue_struct *tpacpi_wq;
353 enum led_status_t {
354 TPACPI_LED_OFF = 0,
355 TPACPI_LED_ON,
356 TPACPI_LED_BLINK,
359 /* Special LED class that can defer work */
360 struct tpacpi_led_classdev {
361 struct led_classdev led_classdev;
362 struct work_struct work;
363 enum led_status_t new_state;
364 unsigned int led;
367 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
368 static int dbg_wlswemul;
369 static int tpacpi_wlsw_emulstate;
370 static int dbg_bluetoothemul;
371 static int tpacpi_bluetooth_emulstate;
372 static int dbg_wwanemul;
373 static int tpacpi_wwan_emulstate;
374 static int dbg_uwbemul;
375 static int tpacpi_uwb_emulstate;
376 #endif
379 /*************************************************************************
380 * Debugging helpers
383 #define dbg_printk(a_dbg_level, format, arg...) \
384 do { if (dbg_level & (a_dbg_level)) \
385 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
386 } while (0)
388 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
389 #define vdbg_printk dbg_printk
390 static const char *str_supported(int is_supported);
391 #else
392 #define vdbg_printk(a_dbg_level, format, arg...) \
393 do { } while (0)
394 #endif
396 static void tpacpi_log_usertask(const char * const what)
398 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
399 what, task_tgid_vnr(current));
402 #define tpacpi_disclose_usertask(what, format, arg...) \
403 do { \
404 if (unlikely( \
405 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
406 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
407 printk(TPACPI_DEBUG "%s: PID %d: " format, \
408 what, task_tgid_vnr(current), ## arg); \
410 } while (0)
413 * Quirk handling helpers
415 * ThinkPad IDs and versions seen in the field so far
416 * are two-characters from the set [0-9A-Z], i.e. base 36.
418 * We use values well outside that range as specials.
421 #define TPACPI_MATCH_ANY 0xffffU
422 #define TPACPI_MATCH_UNKNOWN 0U
424 /* TPID('1', 'Y') == 0x5931 */
425 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
427 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
428 { .vendor = PCI_VENDOR_ID_IBM, \
429 .bios = TPID(__id1, __id2), \
430 .ec = TPACPI_MATCH_ANY, \
431 .quirks = (__quirk) }
433 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
434 { .vendor = PCI_VENDOR_ID_LENOVO, \
435 .bios = TPID(__id1, __id2), \
436 .ec = TPACPI_MATCH_ANY, \
437 .quirks = (__quirk) }
439 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
440 { .vendor = PCI_VENDOR_ID_LENOVO, \
441 .bios = TPACPI_MATCH_ANY, \
442 .ec = TPID(__id1, __id2), \
443 .quirks = (__quirk) }
445 struct tpacpi_quirk {
446 unsigned int vendor;
447 u16 bios;
448 u16 ec;
449 unsigned long quirks;
453 * tpacpi_check_quirks() - search BIOS/EC version on a list
454 * @qlist: array of &struct tpacpi_quirk
455 * @qlist_size: number of elements in @qlist
457 * Iterates over a quirks list until one is found that matches the
458 * ThinkPad's vendor, BIOS and EC model.
460 * Returns 0 if nothing matches, otherwise returns the quirks field of
461 * the matching &struct tpacpi_quirk entry.
463 * The match criteria is: vendor, ec and bios much match.
465 static unsigned long __init tpacpi_check_quirks(
466 const struct tpacpi_quirk *qlist,
467 unsigned int qlist_size)
469 while (qlist_size) {
470 if ((qlist->vendor == thinkpad_id.vendor ||
471 qlist->vendor == TPACPI_MATCH_ANY) &&
472 (qlist->bios == thinkpad_id.bios_model ||
473 qlist->bios == TPACPI_MATCH_ANY) &&
474 (qlist->ec == thinkpad_id.ec_model ||
475 qlist->ec == TPACPI_MATCH_ANY))
476 return qlist->quirks;
478 qlist_size--;
479 qlist++;
481 return 0;
485 /****************************************************************************
486 ****************************************************************************
488 * ACPI Helpers and device model
490 ****************************************************************************
491 ****************************************************************************/
493 /*************************************************************************
494 * ACPI basic handles
497 static acpi_handle root_handle;
499 #define TPACPI_HANDLE(object, parent, paths...) \
500 static acpi_handle object##_handle; \
501 static acpi_handle *object##_parent = &parent##_handle; \
502 static char *object##_path; \
503 static char *object##_paths[] = { paths }
505 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
506 "\\_SB.PCI.ISA.EC", /* 570 */
507 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
508 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
509 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
510 "\\_SB.PCI0.ICH3.EC0", /* R31 */
511 "\\_SB.PCI0.LPC.EC", /* all others */
514 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
515 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
517 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
518 /* T4x, X31, X40 */
519 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
520 "\\CMS", /* R40, R40e */
521 ); /* all others */
523 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
524 "^HKEY", /* R30, R31 */
525 "HKEY", /* all others */
526 ); /* 570 */
528 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
529 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
530 "\\_SB.PCI0.VID0", /* 770e */
531 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
532 "\\_SB.PCI0.AGP.VID", /* all others */
533 ); /* R30, R31 */
536 /*************************************************************************
537 * ACPI helpers
540 static int acpi_evalf(acpi_handle handle,
541 void *res, char *method, char *fmt, ...)
543 char *fmt0 = fmt;
544 struct acpi_object_list params;
545 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
546 struct acpi_buffer result, *resultp;
547 union acpi_object out_obj;
548 acpi_status status;
549 va_list ap;
550 char res_type;
551 int success;
552 int quiet;
554 if (!*fmt) {
555 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
556 return 0;
559 if (*fmt == 'q') {
560 quiet = 1;
561 fmt++;
562 } else
563 quiet = 0;
565 res_type = *(fmt++);
567 params.count = 0;
568 params.pointer = &in_objs[0];
570 va_start(ap, fmt);
571 while (*fmt) {
572 char c = *(fmt++);
573 switch (c) {
574 case 'd': /* int */
575 in_objs[params.count].integer.value = va_arg(ap, int);
576 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
577 break;
578 /* add more types as needed */
579 default:
580 printk(TPACPI_ERR "acpi_evalf() called "
581 "with invalid format character '%c'\n", c);
582 return 0;
585 va_end(ap);
587 if (res_type != 'v') {
588 result.length = sizeof(out_obj);
589 result.pointer = &out_obj;
590 resultp = &result;
591 } else
592 resultp = NULL;
594 status = acpi_evaluate_object(handle, method, &params, resultp);
596 switch (res_type) {
597 case 'd': /* int */
598 if (res)
599 *(int *)res = out_obj.integer.value;
600 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
601 break;
602 case 'v': /* void */
603 success = status == AE_OK;
604 break;
605 /* add more types as needed */
606 default:
607 printk(TPACPI_ERR "acpi_evalf() called "
608 "with invalid format character '%c'\n", res_type);
609 return 0;
612 if (!success && !quiet)
613 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
614 method, fmt0, status);
616 return success;
619 static int acpi_ec_read(int i, u8 *p)
621 int v;
623 if (ecrd_handle) {
624 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
625 return 0;
626 *p = v;
627 } else {
628 if (ec_read(i, p) < 0)
629 return 0;
632 return 1;
635 static int acpi_ec_write(int i, u8 v)
637 if (ecwr_handle) {
638 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
639 return 0;
640 } else {
641 if (ec_write(i, v) < 0)
642 return 0;
645 return 1;
648 static int issue_thinkpad_cmos_command(int cmos_cmd)
650 if (!cmos_handle)
651 return -ENXIO;
653 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
654 return -EIO;
656 return 0;
659 /*************************************************************************
660 * ACPI device model
663 #define TPACPI_ACPIHANDLE_INIT(object) \
664 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
665 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
667 static void drv_acpi_handle_init(char *name,
668 acpi_handle *handle, acpi_handle parent,
669 char **paths, int num_paths, char **path)
671 int i;
672 acpi_status status;
674 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
675 name);
677 for (i = 0; i < num_paths; i++) {
678 status = acpi_get_handle(parent, paths[i], handle);
679 if (ACPI_SUCCESS(status)) {
680 *path = paths[i];
681 dbg_printk(TPACPI_DBG_INIT,
682 "Found ACPI handle %s for %s\n",
683 *path, name);
684 return;
688 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
689 name);
690 *handle = NULL;
693 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
695 struct ibm_struct *ibm = data;
697 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
698 return;
700 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
701 return;
703 ibm->acpi->notify(ibm, event);
706 static int __init setup_acpi_notify(struct ibm_struct *ibm)
708 acpi_status status;
709 int rc;
711 BUG_ON(!ibm->acpi);
713 if (!*ibm->acpi->handle)
714 return 0;
716 vdbg_printk(TPACPI_DBG_INIT,
717 "setting up ACPI notify for %s\n", ibm->name);
719 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
720 if (rc < 0) {
721 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
722 ibm->name, rc);
723 return -ENODEV;
726 ibm->acpi->device->driver_data = ibm;
727 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
728 TPACPI_ACPI_EVENT_PREFIX,
729 ibm->name);
731 status = acpi_install_notify_handler(*ibm->acpi->handle,
732 ibm->acpi->type, dispatch_acpi_notify, ibm);
733 if (ACPI_FAILURE(status)) {
734 if (status == AE_ALREADY_EXISTS) {
735 printk(TPACPI_NOTICE
736 "another device driver is already "
737 "handling %s events\n", ibm->name);
738 } else {
739 printk(TPACPI_ERR
740 "acpi_install_notify_handler(%s) failed: %d\n",
741 ibm->name, status);
743 return -ENODEV;
745 ibm->flags.acpi_notify_installed = 1;
746 return 0;
749 static int __init tpacpi_device_add(struct acpi_device *device)
751 return 0;
754 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
756 int rc;
758 dbg_printk(TPACPI_DBG_INIT,
759 "registering %s as an ACPI driver\n", ibm->name);
761 BUG_ON(!ibm->acpi);
763 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
764 if (!ibm->acpi->driver) {
765 printk(TPACPI_ERR
766 "failed to allocate memory for ibm->acpi->driver\n");
767 return -ENOMEM;
770 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
771 ibm->acpi->driver->ids = ibm->acpi->hid;
773 ibm->acpi->driver->ops.add = &tpacpi_device_add;
775 rc = acpi_bus_register_driver(ibm->acpi->driver);
776 if (rc < 0) {
777 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
778 ibm->name, rc);
779 kfree(ibm->acpi->driver);
780 ibm->acpi->driver = NULL;
781 } else if (!rc)
782 ibm->flags.acpi_driver_registered = 1;
784 return rc;
788 /****************************************************************************
789 ****************************************************************************
791 * Procfs Helpers
793 ****************************************************************************
794 ****************************************************************************/
796 static int dispatch_proc_show(struct seq_file *m, void *v)
798 struct ibm_struct *ibm = m->private;
800 if (!ibm || !ibm->read)
801 return -EINVAL;
802 return ibm->read(m);
805 static int dispatch_proc_open(struct inode *inode, struct file *file)
807 return single_open(file, dispatch_proc_show, PDE(inode)->data);
810 static ssize_t dispatch_proc_write(struct file *file,
811 const char __user *userbuf,
812 size_t count, loff_t *pos)
814 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
815 char *kernbuf;
816 int ret;
818 if (!ibm || !ibm->write)
819 return -EINVAL;
820 if (count > PAGE_SIZE - 2)
821 return -EINVAL;
823 kernbuf = kmalloc(count + 2, GFP_KERNEL);
824 if (!kernbuf)
825 return -ENOMEM;
827 if (copy_from_user(kernbuf, userbuf, count)) {
828 kfree(kernbuf);
829 return -EFAULT;
832 kernbuf[count] = 0;
833 strcat(kernbuf, ",");
834 ret = ibm->write(kernbuf);
835 if (ret == 0)
836 ret = count;
838 kfree(kernbuf);
840 return ret;
843 static const struct file_operations dispatch_proc_fops = {
844 .owner = THIS_MODULE,
845 .open = dispatch_proc_open,
846 .read = seq_read,
847 .llseek = seq_lseek,
848 .release = single_release,
849 .write = dispatch_proc_write,
852 static char *next_cmd(char **cmds)
854 char *start = *cmds;
855 char *end;
857 while ((end = strchr(start, ',')) && end == start)
858 start = end + 1;
860 if (!end)
861 return NULL;
863 *end = 0;
864 *cmds = end + 1;
865 return start;
869 /****************************************************************************
870 ****************************************************************************
872 * Device model: input, hwmon and platform
874 ****************************************************************************
875 ****************************************************************************/
877 static struct platform_device *tpacpi_pdev;
878 static struct platform_device *tpacpi_sensors_pdev;
879 static struct device *tpacpi_hwmon;
880 static struct input_dev *tpacpi_inputdev;
881 static struct mutex tpacpi_inputdev_send_mutex;
882 static LIST_HEAD(tpacpi_all_drivers);
884 static int tpacpi_suspend_handler(struct platform_device *pdev,
885 pm_message_t state)
887 struct ibm_struct *ibm, *itmp;
889 list_for_each_entry_safe(ibm, itmp,
890 &tpacpi_all_drivers,
891 all_drivers) {
892 if (ibm->suspend)
893 (ibm->suspend)(state);
896 return 0;
899 static int tpacpi_resume_handler(struct platform_device *pdev)
901 struct ibm_struct *ibm, *itmp;
903 list_for_each_entry_safe(ibm, itmp,
904 &tpacpi_all_drivers,
905 all_drivers) {
906 if (ibm->resume)
907 (ibm->resume)();
910 return 0;
913 static void tpacpi_shutdown_handler(struct platform_device *pdev)
915 struct ibm_struct *ibm, *itmp;
917 list_for_each_entry_safe(ibm, itmp,
918 &tpacpi_all_drivers,
919 all_drivers) {
920 if (ibm->shutdown)
921 (ibm->shutdown)();
925 static struct platform_driver tpacpi_pdriver = {
926 .driver = {
927 .name = TPACPI_DRVR_NAME,
928 .owner = THIS_MODULE,
930 .suspend = tpacpi_suspend_handler,
931 .resume = tpacpi_resume_handler,
932 .shutdown = tpacpi_shutdown_handler,
935 static struct platform_driver tpacpi_hwmon_pdriver = {
936 .driver = {
937 .name = TPACPI_HWMON_DRVR_NAME,
938 .owner = THIS_MODULE,
942 /*************************************************************************
943 * sysfs support helpers
946 struct attribute_set {
947 unsigned int members, max_members;
948 struct attribute_group group;
951 struct attribute_set_obj {
952 struct attribute_set s;
953 struct attribute *a;
954 } __attribute__((packed));
956 static struct attribute_set *create_attr_set(unsigned int max_members,
957 const char *name)
959 struct attribute_set_obj *sobj;
961 if (max_members == 0)
962 return NULL;
964 /* Allocates space for implicit NULL at the end too */
965 sobj = kzalloc(sizeof(struct attribute_set_obj) +
966 max_members * sizeof(struct attribute *),
967 GFP_KERNEL);
968 if (!sobj)
969 return NULL;
970 sobj->s.max_members = max_members;
971 sobj->s.group.attrs = &sobj->a;
972 sobj->s.group.name = name;
974 return &sobj->s;
977 #define destroy_attr_set(_set) \
978 kfree(_set);
980 /* not multi-threaded safe, use it in a single thread per set */
981 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
983 if (!s || !attr)
984 return -EINVAL;
986 if (s->members >= s->max_members)
987 return -ENOMEM;
989 s->group.attrs[s->members] = attr;
990 s->members++;
992 return 0;
995 static int add_many_to_attr_set(struct attribute_set *s,
996 struct attribute **attr,
997 unsigned int count)
999 int i, res;
1001 for (i = 0; i < count; i++) {
1002 res = add_to_attr_set(s, attr[i]);
1003 if (res)
1004 return res;
1007 return 0;
1010 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1012 sysfs_remove_group(kobj, &s->group);
1013 destroy_attr_set(s);
1016 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1017 sysfs_create_group(_kobj, &_attr_set->group)
1019 static int parse_strtoul(const char *buf,
1020 unsigned long max, unsigned long *value)
1022 char *endp;
1024 while (*buf && isspace(*buf))
1025 buf++;
1026 *value = simple_strtoul(buf, &endp, 0);
1027 while (*endp && isspace(*endp))
1028 endp++;
1029 if (*endp || *value > max)
1030 return -EINVAL;
1032 return 0;
1035 static void tpacpi_disable_brightness_delay(void)
1037 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1038 printk(TPACPI_NOTICE
1039 "ACPI backlight control delay disabled\n");
1042 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1044 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1045 union acpi_object *obj;
1046 int rc;
1048 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1049 obj = (union acpi_object *)buffer.pointer;
1050 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1051 printk(TPACPI_ERR "Unknown _BCL data, "
1052 "please report this to %s\n", TPACPI_MAIL);
1053 rc = 0;
1054 } else {
1055 rc = obj->package.count;
1057 } else {
1058 return 0;
1061 kfree(buffer.pointer);
1062 return rc;
1065 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1066 u32 lvl, void *context, void **rv)
1068 char name[ACPI_PATH_SEGMENT_LENGTH];
1069 struct acpi_buffer buffer = { sizeof(name), &name };
1071 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1072 !strncmp("_BCL", name, sizeof(name) - 1)) {
1073 BUG_ON(!rv || !*rv);
1074 **(int **)rv = tpacpi_query_bcl_levels(handle);
1075 return AE_CTRL_TERMINATE;
1076 } else {
1077 return AE_OK;
1082 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1084 static int __init tpacpi_check_std_acpi_brightness_support(void)
1086 int status;
1087 int bcl_levels = 0;
1088 void *bcl_ptr = &bcl_levels;
1090 if (!vid_handle) {
1091 TPACPI_ACPIHANDLE_INIT(vid);
1093 if (!vid_handle)
1094 return 0;
1097 * Search for a _BCL method, and execute it. This is safe on all
1098 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1099 * BIOS in ACPI backlight control mode. We do NOT have to care
1100 * about calling the _BCL method in an enabled video device, any
1101 * will do for our purposes.
1104 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1105 tpacpi_acpi_walk_find_bcl, NULL,
1106 &bcl_ptr);
1108 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1109 tp_features.bright_acpimode = 1;
1110 return (bcl_levels - 2);
1113 return 0;
1116 static void printk_deprecated_attribute(const char * const what,
1117 const char * const details)
1119 tpacpi_log_usertask("deprecated sysfs attribute");
1120 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1121 "will be removed. %s\n",
1122 what, details);
1125 /*************************************************************************
1126 * rfkill and radio control support helpers
1130 * ThinkPad-ACPI firmware handling model:
1132 * WLSW (master wireless switch) is event-driven, and is common to all
1133 * firmware-controlled radios. It cannot be controlled, just monitored,
1134 * as expected. It overrides all radio state in firmware
1136 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1137 * (TODO: verify how WLSW interacts with the returned radio state).
1139 * The only time there are shadow radio state changes, is when
1140 * masked-off hotkeys are used.
1144 * Internal driver API for radio state:
1146 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1147 * bool: true means radio blocked (off)
1149 enum tpacpi_rfkill_state {
1150 TPACPI_RFK_RADIO_OFF = 0,
1151 TPACPI_RFK_RADIO_ON
1154 /* rfkill switches */
1155 enum tpacpi_rfk_id {
1156 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1157 TPACPI_RFK_WWAN_SW_ID,
1158 TPACPI_RFK_UWB_SW_ID,
1159 TPACPI_RFK_SW_MAX
1162 static const char *tpacpi_rfkill_names[] = {
1163 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1164 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1165 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1166 [TPACPI_RFK_SW_MAX] = NULL
1169 /* ThinkPad-ACPI rfkill subdriver */
1170 struct tpacpi_rfk {
1171 struct rfkill *rfkill;
1172 enum tpacpi_rfk_id id;
1173 const struct tpacpi_rfk_ops *ops;
1176 struct tpacpi_rfk_ops {
1177 /* firmware interface */
1178 int (*get_status)(void);
1179 int (*set_status)(const enum tpacpi_rfkill_state);
1182 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1184 /* Query FW and update rfkill sw state for a given rfkill switch */
1185 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1187 int status;
1189 if (!tp_rfk)
1190 return -ENODEV;
1192 status = (tp_rfk->ops->get_status)();
1193 if (status < 0)
1194 return status;
1196 rfkill_set_sw_state(tp_rfk->rfkill,
1197 (status == TPACPI_RFK_RADIO_OFF));
1199 return status;
1202 /* Query FW and update rfkill sw state for all rfkill switches */
1203 static void tpacpi_rfk_update_swstate_all(void)
1205 unsigned int i;
1207 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1208 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1212 * Sync the HW-blocking state of all rfkill switches,
1213 * do notice it causes the rfkill core to schedule uevents
1215 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1217 unsigned int i;
1218 struct tpacpi_rfk *tp_rfk;
1220 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1221 tp_rfk = tpacpi_rfkill_switches[i];
1222 if (tp_rfk) {
1223 if (rfkill_set_hw_state(tp_rfk->rfkill,
1224 blocked)) {
1225 /* ignore -- we track sw block */
1231 /* Call to get the WLSW state from the firmware */
1232 static int hotkey_get_wlsw(void);
1234 /* Call to query WLSW state and update all rfkill switches */
1235 static bool tpacpi_rfk_check_hwblock_state(void)
1237 int res = hotkey_get_wlsw();
1238 int hw_blocked;
1240 /* When unknown or unsupported, we have to assume it is unblocked */
1241 if (res < 0)
1242 return false;
1244 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1245 tpacpi_rfk_update_hwblock_state(hw_blocked);
1247 return hw_blocked;
1250 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1252 struct tpacpi_rfk *tp_rfk = data;
1253 int res;
1255 dbg_printk(TPACPI_DBG_RFKILL,
1256 "request to change radio state to %s\n",
1257 blocked ? "blocked" : "unblocked");
1259 /* try to set radio state */
1260 res = (tp_rfk->ops->set_status)(blocked ?
1261 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1263 /* and update the rfkill core with whatever the FW really did */
1264 tpacpi_rfk_update_swstate(tp_rfk);
1266 return (res < 0) ? res : 0;
1269 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1270 .set_block = tpacpi_rfk_hook_set_block,
1273 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1274 const struct tpacpi_rfk_ops *tp_rfkops,
1275 const enum rfkill_type rfktype,
1276 const char *name,
1277 const bool set_default)
1279 struct tpacpi_rfk *atp_rfk;
1280 int res;
1281 bool sw_state = false;
1282 bool hw_state;
1283 int sw_status;
1285 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1287 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1288 if (atp_rfk)
1289 atp_rfk->rfkill = rfkill_alloc(name,
1290 &tpacpi_pdev->dev,
1291 rfktype,
1292 &tpacpi_rfk_rfkill_ops,
1293 atp_rfk);
1294 if (!atp_rfk || !atp_rfk->rfkill) {
1295 printk(TPACPI_ERR
1296 "failed to allocate memory for rfkill class\n");
1297 kfree(atp_rfk);
1298 return -ENOMEM;
1301 atp_rfk->id = id;
1302 atp_rfk->ops = tp_rfkops;
1304 sw_status = (tp_rfkops->get_status)();
1305 if (sw_status < 0) {
1306 printk(TPACPI_ERR
1307 "failed to read initial state for %s, error %d\n",
1308 name, sw_status);
1309 } else {
1310 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1311 if (set_default) {
1312 /* try to keep the initial state, since we ask the
1313 * firmware to preserve it across S5 in NVRAM */
1314 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1317 hw_state = tpacpi_rfk_check_hwblock_state();
1318 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1320 res = rfkill_register(atp_rfk->rfkill);
1321 if (res < 0) {
1322 printk(TPACPI_ERR
1323 "failed to register %s rfkill switch: %d\n",
1324 name, res);
1325 rfkill_destroy(atp_rfk->rfkill);
1326 kfree(atp_rfk);
1327 return res;
1330 tpacpi_rfkill_switches[id] = atp_rfk;
1332 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1333 name, (sw_state || hw_state) ? "" : "un");
1334 return 0;
1337 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1339 struct tpacpi_rfk *tp_rfk;
1341 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1343 tp_rfk = tpacpi_rfkill_switches[id];
1344 if (tp_rfk) {
1345 rfkill_unregister(tp_rfk->rfkill);
1346 rfkill_destroy(tp_rfk->rfkill);
1347 tpacpi_rfkill_switches[id] = NULL;
1348 kfree(tp_rfk);
1352 static void printk_deprecated_rfkill_attribute(const char * const what)
1354 printk_deprecated_attribute(what,
1355 "Please switch to generic rfkill before year 2010");
1358 /* sysfs <radio> enable ------------------------------------------------ */
1359 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1360 struct device_attribute *attr,
1361 char *buf)
1363 int status;
1365 printk_deprecated_rfkill_attribute(attr->attr.name);
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state()) {
1369 status = TPACPI_RFK_RADIO_OFF;
1370 } else {
1371 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1372 if (status < 0)
1373 return status;
1376 return snprintf(buf, PAGE_SIZE, "%d\n",
1377 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1380 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1381 struct device_attribute *attr,
1382 const char *buf, size_t count)
1384 unsigned long t;
1385 int res;
1387 printk_deprecated_rfkill_attribute(attr->attr.name);
1389 if (parse_strtoul(buf, 1, &t))
1390 return -EINVAL;
1392 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1394 /* This is in the ABI... */
1395 if (tpacpi_rfk_check_hwblock_state() && !!t)
1396 return -EPERM;
1398 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1399 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1400 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1402 return (res < 0) ? res : count;
1405 /* procfs -------------------------------------------------------------- */
1406 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1407 struct seq_file *m)
1409 if (id >= TPACPI_RFK_SW_MAX)
1410 seq_printf(m, "status:\t\tnot supported\n");
1411 else {
1412 int status;
1414 /* This is in the ABI... */
1415 if (tpacpi_rfk_check_hwblock_state()) {
1416 status = TPACPI_RFK_RADIO_OFF;
1417 } else {
1418 status = tpacpi_rfk_update_swstate(
1419 tpacpi_rfkill_switches[id]);
1420 if (status < 0)
1421 return status;
1424 seq_printf(m, "status:\t\t%s\n",
1425 (status == TPACPI_RFK_RADIO_ON) ?
1426 "enabled" : "disabled");
1427 seq_printf(m, "commands:\tenable, disable\n");
1430 return 0;
1433 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1435 char *cmd;
1436 int status = -1;
1437 int res = 0;
1439 if (id >= TPACPI_RFK_SW_MAX)
1440 return -ENODEV;
1442 while ((cmd = next_cmd(&buf))) {
1443 if (strlencmp(cmd, "enable") == 0)
1444 status = TPACPI_RFK_RADIO_ON;
1445 else if (strlencmp(cmd, "disable") == 0)
1446 status = TPACPI_RFK_RADIO_OFF;
1447 else
1448 return -EINVAL;
1451 if (status != -1) {
1452 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1453 (status == TPACPI_RFK_RADIO_ON) ?
1454 "enable" : "disable",
1455 tpacpi_rfkill_names[id]);
1456 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1457 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1460 return res;
1463 /*************************************************************************
1464 * thinkpad-acpi driver attributes
1467 /* interface_version --------------------------------------------------- */
1468 static ssize_t tpacpi_driver_interface_version_show(
1469 struct device_driver *drv,
1470 char *buf)
1472 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1475 static DRIVER_ATTR(interface_version, S_IRUGO,
1476 tpacpi_driver_interface_version_show, NULL);
1478 /* debug_level --------------------------------------------------------- */
1479 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1480 char *buf)
1482 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1485 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1486 const char *buf, size_t count)
1488 unsigned long t;
1490 if (parse_strtoul(buf, 0xffff, &t))
1491 return -EINVAL;
1493 dbg_level = t;
1495 return count;
1498 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1499 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1501 /* version ------------------------------------------------------------- */
1502 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1503 char *buf)
1505 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1506 TPACPI_DESC, TPACPI_VERSION);
1509 static DRIVER_ATTR(version, S_IRUGO,
1510 tpacpi_driver_version_show, NULL);
1512 /* --------------------------------------------------------------------- */
1514 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1516 /* wlsw_emulstate ------------------------------------------------------ */
1517 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1518 char *buf)
1520 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1523 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1524 const char *buf, size_t count)
1526 unsigned long t;
1528 if (parse_strtoul(buf, 1, &t))
1529 return -EINVAL;
1531 if (tpacpi_wlsw_emulstate != !!t) {
1532 tpacpi_wlsw_emulstate = !!t;
1533 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1536 return count;
1539 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1540 tpacpi_driver_wlsw_emulstate_show,
1541 tpacpi_driver_wlsw_emulstate_store);
1543 /* bluetooth_emulstate ------------------------------------------------- */
1544 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1545 struct device_driver *drv,
1546 char *buf)
1548 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1551 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1552 struct device_driver *drv,
1553 const char *buf, size_t count)
1555 unsigned long t;
1557 if (parse_strtoul(buf, 1, &t))
1558 return -EINVAL;
1560 tpacpi_bluetooth_emulstate = !!t;
1562 return count;
1565 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1566 tpacpi_driver_bluetooth_emulstate_show,
1567 tpacpi_driver_bluetooth_emulstate_store);
1569 /* wwan_emulstate ------------------------------------------------- */
1570 static ssize_t tpacpi_driver_wwan_emulstate_show(
1571 struct device_driver *drv,
1572 char *buf)
1574 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1577 static ssize_t tpacpi_driver_wwan_emulstate_store(
1578 struct device_driver *drv,
1579 const char *buf, size_t count)
1581 unsigned long t;
1583 if (parse_strtoul(buf, 1, &t))
1584 return -EINVAL;
1586 tpacpi_wwan_emulstate = !!t;
1588 return count;
1591 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1592 tpacpi_driver_wwan_emulstate_show,
1593 tpacpi_driver_wwan_emulstate_store);
1595 /* uwb_emulstate ------------------------------------------------- */
1596 static ssize_t tpacpi_driver_uwb_emulstate_show(
1597 struct device_driver *drv,
1598 char *buf)
1600 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1603 static ssize_t tpacpi_driver_uwb_emulstate_store(
1604 struct device_driver *drv,
1605 const char *buf, size_t count)
1607 unsigned long t;
1609 if (parse_strtoul(buf, 1, &t))
1610 return -EINVAL;
1612 tpacpi_uwb_emulstate = !!t;
1614 return count;
1617 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1618 tpacpi_driver_uwb_emulstate_show,
1619 tpacpi_driver_uwb_emulstate_store);
1620 #endif
1622 /* --------------------------------------------------------------------- */
1624 static struct driver_attribute *tpacpi_driver_attributes[] = {
1625 &driver_attr_debug_level, &driver_attr_version,
1626 &driver_attr_interface_version,
1629 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1631 int i, res;
1633 i = 0;
1634 res = 0;
1635 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1636 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1637 i++;
1640 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1641 if (!res && dbg_wlswemul)
1642 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1643 if (!res && dbg_bluetoothemul)
1644 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1645 if (!res && dbg_wwanemul)
1646 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1647 if (!res && dbg_uwbemul)
1648 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1649 #endif
1651 return res;
1654 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1656 int i;
1658 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1659 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1661 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1662 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1663 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1664 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1665 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1666 #endif
1669 /*************************************************************************
1670 * Firmware Data
1674 * Table of recommended minimum BIOS versions
1676 * Reasons for listing:
1677 * 1. Stable BIOS, listed because the unknown ammount of
1678 * bugs and bad ACPI behaviour on older versions
1680 * 2. BIOS or EC fw with known bugs that trigger on Linux
1682 * 3. BIOS with known reduced functionality in older versions
1684 * We recommend the latest BIOS and EC version.
1685 * We only support the latest BIOS and EC fw version as a rule.
1687 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1688 * Information from users in ThinkWiki
1690 * WARNING: we use this table also to detect that the machine is
1691 * a ThinkPad in some cases, so don't remove entries lightly.
1694 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1695 { .vendor = (__v), \
1696 .bios = TPID(__id1, __id2), \
1697 .ec = TPACPI_MATCH_ANY, \
1698 .quirks = TPACPI_MATCH_ANY << 16 \
1699 | (__bv1) << 8 | (__bv2) }
1701 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1702 __eid, __ev1, __ev2) \
1703 { .vendor = (__v), \
1704 .bios = TPID(__bid1, __bid2), \
1705 .ec = __eid, \
1706 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1707 | (__bv1) << 8 | (__bv2) }
1709 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1710 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1712 /* Outdated IBM BIOSes often lack the EC id string */
1713 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1715 __bv1, __bv2, TPID(__id1, __id2), \
1716 __ev1, __ev2), \
1717 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1718 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1719 __ev1, __ev2)
1721 /* Outdated IBM BIOSes often lack the EC id string */
1722 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1723 __eid1, __eid2, __ev1, __ev2) \
1724 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1725 __bv1, __bv2, TPID(__eid1, __eid2), \
1726 __ev1, __ev2), \
1727 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1728 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1729 __ev1, __ev2)
1731 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1732 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1734 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1735 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1736 __bv1, __bv2, TPID(__id1, __id2), \
1737 __ev1, __ev2)
1739 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1740 __eid1, __eid2, __ev1, __ev2) \
1741 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1742 __bv1, __bv2, TPID(__eid1, __eid2), \
1743 __ev1, __ev2)
1745 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1746 /* Numeric models ------------------ */
1747 /* FW MODEL BIOS VERS */
1748 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1749 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1750 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1751 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1752 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1753 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1754 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1755 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1756 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1758 /* A-series ------------------------- */
1759 /* FW MODEL BIOS VERS EC VERS */
1760 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1761 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1762 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1763 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1764 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1765 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1766 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1767 TPV_QI0('1', '3', '2', '0'), /* A22m */
1768 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1769 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1770 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1772 /* G-series ------------------------- */
1773 /* FW MODEL BIOS VERS */
1774 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1775 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1777 /* R-series, T-series --------------- */
1778 /* FW MODEL BIOS VERS EC VERS */
1779 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1780 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1781 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1782 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1783 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1784 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1785 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1786 T40/p, T41/p, T42/p (1) */
1787 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1788 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1789 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1790 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1792 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1793 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1794 TPV_QI0('1', '6', '3', '2'), /* T22 */
1795 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1796 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1797 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1799 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1800 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1801 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1803 /* BIOS FW BIOS VERS EC FW EC VERS */
1804 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1805 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1807 /* X-series ------------------------- */
1808 /* FW MODEL BIOS VERS EC VERS */
1809 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1810 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1811 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1812 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1813 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1814 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1815 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1817 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1818 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1820 /* (0) - older versions lack DMI EC fw string and functionality */
1821 /* (1) - older versions known to lack functionality */
1824 #undef TPV_QL1
1825 #undef TPV_QL0
1826 #undef TPV_QI2
1827 #undef TPV_QI1
1828 #undef TPV_QI0
1829 #undef TPV_Q_X
1830 #undef TPV_Q
1832 static void __init tpacpi_check_outdated_fw(void)
1834 unsigned long fwvers;
1835 u16 ec_version, bios_version;
1837 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1838 ARRAY_SIZE(tpacpi_bios_version_qtable));
1840 if (!fwvers)
1841 return;
1843 bios_version = fwvers & 0xffffU;
1844 ec_version = (fwvers >> 16) & 0xffffU;
1846 /* note that unknown versions are set to 0x0000 and we use that */
1847 if ((bios_version > thinkpad_id.bios_release) ||
1848 (ec_version > thinkpad_id.ec_release &&
1849 ec_version != TPACPI_MATCH_ANY)) {
1851 * The changelogs would let us track down the exact
1852 * reason, but it is just too much of a pain to track
1853 * it. We only list BIOSes that are either really
1854 * broken, or really stable to begin with, so it is
1855 * best if the user upgrades the firmware anyway.
1857 printk(TPACPI_WARN
1858 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1859 printk(TPACPI_WARN
1860 "WARNING: This firmware may be missing critical bug "
1861 "fixes and/or important features\n");
1865 static bool __init tpacpi_is_fw_known(void)
1867 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1868 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1871 /****************************************************************************
1872 ****************************************************************************
1874 * Subdrivers
1876 ****************************************************************************
1877 ****************************************************************************/
1879 /*************************************************************************
1880 * thinkpad-acpi init subdriver
1883 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1885 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1886 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1888 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1889 (thinkpad_id.bios_version_str) ?
1890 thinkpad_id.bios_version_str : "unknown",
1891 (thinkpad_id.ec_version_str) ?
1892 thinkpad_id.ec_version_str : "unknown");
1894 if (thinkpad_id.vendor && thinkpad_id.model_str)
1895 printk(TPACPI_INFO "%s %s, model %s\n",
1896 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1897 "IBM" : ((thinkpad_id.vendor ==
1898 PCI_VENDOR_ID_LENOVO) ?
1899 "Lenovo" : "Unknown vendor"),
1900 thinkpad_id.model_str,
1901 (thinkpad_id.nummodel_str) ?
1902 thinkpad_id.nummodel_str : "unknown");
1904 tpacpi_check_outdated_fw();
1905 return 0;
1908 static int thinkpad_acpi_driver_read(struct seq_file *m)
1910 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1911 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1912 return 0;
1915 static struct ibm_struct thinkpad_acpi_driver_data = {
1916 .name = "driver",
1917 .read = thinkpad_acpi_driver_read,
1920 /*************************************************************************
1921 * Hotkey subdriver
1925 * ThinkPad firmware event model
1927 * The ThinkPad firmware has two main event interfaces: normal ACPI
1928 * notifications (which follow the ACPI standard), and a private event
1929 * interface.
1931 * The private event interface also issues events for the hotkeys. As
1932 * the driver gained features, the event handling code ended up being
1933 * built around the hotkey subdriver. This will need to be refactored
1934 * to a more formal event API eventually.
1936 * Some "hotkeys" are actually supposed to be used as event reports,
1937 * such as "brightness has changed", "volume has changed", depending on
1938 * the ThinkPad model and how the firmware is operating.
1940 * Unlike other classes, hotkey-class events have mask/unmask control on
1941 * non-ancient firmware. However, how it behaves changes a lot with the
1942 * firmware model and version.
1945 enum { /* hot key scan codes (derived from ACPI DSDT) */
1946 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1947 TP_ACPI_HOTKEYSCAN_FNF2,
1948 TP_ACPI_HOTKEYSCAN_FNF3,
1949 TP_ACPI_HOTKEYSCAN_FNF4,
1950 TP_ACPI_HOTKEYSCAN_FNF5,
1951 TP_ACPI_HOTKEYSCAN_FNF6,
1952 TP_ACPI_HOTKEYSCAN_FNF7,
1953 TP_ACPI_HOTKEYSCAN_FNF8,
1954 TP_ACPI_HOTKEYSCAN_FNF9,
1955 TP_ACPI_HOTKEYSCAN_FNF10,
1956 TP_ACPI_HOTKEYSCAN_FNF11,
1957 TP_ACPI_HOTKEYSCAN_FNF12,
1958 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1959 TP_ACPI_HOTKEYSCAN_FNINSERT,
1960 TP_ACPI_HOTKEYSCAN_FNDELETE,
1961 TP_ACPI_HOTKEYSCAN_FNHOME,
1962 TP_ACPI_HOTKEYSCAN_FNEND,
1963 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1964 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1965 TP_ACPI_HOTKEYSCAN_FNSPACE,
1966 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1967 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1968 TP_ACPI_HOTKEYSCAN_MUTE,
1969 TP_ACPI_HOTKEYSCAN_THINKPAD,
1972 enum { /* Keys/events available through NVRAM polling */
1973 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1974 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1977 enum { /* Positions of some of the keys in hotkey masks */
1978 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1979 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1980 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1981 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1982 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1983 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1984 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1985 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1986 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1987 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1988 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1991 enum { /* NVRAM to ACPI HKEY group map */
1992 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1993 TP_ACPI_HKEY_ZOOM_MASK |
1994 TP_ACPI_HKEY_DISPSWTCH_MASK |
1995 TP_ACPI_HKEY_HIBERNATE_MASK,
1996 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1997 TP_ACPI_HKEY_BRGHTDWN_MASK,
1998 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1999 TP_ACPI_HKEY_VOLDWN_MASK |
2000 TP_ACPI_HKEY_MUTE_MASK,
2003 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2004 struct tp_nvram_state {
2005 u16 thinkpad_toggle:1;
2006 u16 zoom_toggle:1;
2007 u16 display_toggle:1;
2008 u16 thinklight_toggle:1;
2009 u16 hibernate_toggle:1;
2010 u16 displayexp_toggle:1;
2011 u16 display_state:1;
2012 u16 brightness_toggle:1;
2013 u16 volume_toggle:1;
2014 u16 mute:1;
2016 u8 brightness_level;
2017 u8 volume_level;
2020 /* kthread for the hotkey poller */
2021 static struct task_struct *tpacpi_hotkey_task;
2023 /* Acquired while the poller kthread is running, use to sync start/stop */
2024 static struct mutex hotkey_thread_mutex;
2027 * Acquire mutex to write poller control variables as an
2028 * atomic block.
2030 * Increment hotkey_config_change when changing them if you
2031 * want the kthread to forget old state.
2033 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2035 static struct mutex hotkey_thread_data_mutex;
2036 static unsigned int hotkey_config_change;
2039 * hotkey poller control variables
2041 * Must be atomic or readers will also need to acquire mutex
2043 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2044 * should be used only when the changes need to be taken as
2045 * a block, OR when one needs to force the kthread to forget
2046 * old state.
2048 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2049 static unsigned int hotkey_poll_freq = 10; /* Hz */
2051 #define HOTKEY_CONFIG_CRITICAL_START \
2052 do { \
2053 mutex_lock(&hotkey_thread_data_mutex); \
2054 hotkey_config_change++; \
2055 } while (0);
2056 #define HOTKEY_CONFIG_CRITICAL_END \
2057 mutex_unlock(&hotkey_thread_data_mutex);
2059 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2061 #define hotkey_source_mask 0U
2062 #define HOTKEY_CONFIG_CRITICAL_START
2063 #define HOTKEY_CONFIG_CRITICAL_END
2065 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2067 static struct mutex hotkey_mutex;
2069 static enum { /* Reasons for waking up */
2070 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2071 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2072 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2073 } hotkey_wakeup_reason;
2075 static int hotkey_autosleep_ack;
2077 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2078 static u32 hotkey_all_mask; /* all events supported in fw */
2079 static u32 hotkey_reserved_mask; /* events better left disabled */
2080 static u32 hotkey_driver_mask; /* events needed by the driver */
2081 static u32 hotkey_user_mask; /* events visible to userspace */
2082 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2084 static unsigned int hotkey_report_mode;
2086 static u16 *hotkey_keycode_map;
2088 static struct attribute_set *hotkey_dev_attributes;
2090 static void tpacpi_driver_event(const unsigned int hkey_event);
2091 static void hotkey_driver_event(const unsigned int scancode);
2092 static void hotkey_poll_setup(const bool may_warn);
2094 /* HKEY.MHKG() return bits */
2095 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2097 static int hotkey_get_wlsw(void)
2099 int status;
2101 if (!tp_features.hotkey_wlsw)
2102 return -ENODEV;
2104 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2105 if (dbg_wlswemul)
2106 return (tpacpi_wlsw_emulstate) ?
2107 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2108 #endif
2110 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2111 return -EIO;
2113 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2116 static int hotkey_get_tablet_mode(int *status)
2118 int s;
2120 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2121 return -EIO;
2123 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2124 return 0;
2128 * Reads current event mask from firmware, and updates
2129 * hotkey_acpi_mask accordingly. Also resets any bits
2130 * from hotkey_user_mask that are unavailable to be
2131 * delivered (shadow requirement of the userspace ABI).
2133 * Call with hotkey_mutex held
2135 static int hotkey_mask_get(void)
2137 if (tp_features.hotkey_mask) {
2138 u32 m = 0;
2140 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2141 return -EIO;
2143 hotkey_acpi_mask = m;
2144 } else {
2145 /* no mask support doesn't mean no event support... */
2146 hotkey_acpi_mask = hotkey_all_mask;
2149 /* sync userspace-visible mask */
2150 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2152 return 0;
2155 void static hotkey_mask_warn_incomplete_mask(void)
2157 /* log only what the user can fix... */
2158 const u32 wantedmask = hotkey_driver_mask &
2159 ~(hotkey_acpi_mask | hotkey_source_mask) &
2160 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2162 if (wantedmask)
2163 printk(TPACPI_NOTICE
2164 "required events 0x%08x not enabled!\n",
2165 wantedmask);
2169 * Set the firmware mask when supported
2171 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2173 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2175 * Call with hotkey_mutex held
2177 static int hotkey_mask_set(u32 mask)
2179 int i;
2180 int rc = 0;
2182 const u32 fwmask = mask & ~hotkey_source_mask;
2184 if (tp_features.hotkey_mask) {
2185 for (i = 0; i < 32; i++) {
2186 if (!acpi_evalf(hkey_handle,
2187 NULL, "MHKM", "vdd", i + 1,
2188 !!(mask & (1 << i)))) {
2189 rc = -EIO;
2190 break;
2196 * We *must* make an inconditional call to hotkey_mask_get to
2197 * refresh hotkey_acpi_mask and update hotkey_user_mask
2199 * Take the opportunity to also log when we cannot _enable_
2200 * a given event.
2202 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2203 printk(TPACPI_NOTICE
2204 "asked for hotkey mask 0x%08x, but "
2205 "firmware forced it to 0x%08x\n",
2206 fwmask, hotkey_acpi_mask);
2209 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2210 hotkey_mask_warn_incomplete_mask();
2212 return rc;
2216 * Sets hotkey_user_mask and tries to set the firmware mask
2218 * Call with hotkey_mutex held
2220 static int hotkey_user_mask_set(const u32 mask)
2222 int rc;
2224 /* Give people a chance to notice they are doing something that
2225 * is bound to go boom on their users sooner or later */
2226 if (!tp_warned.hotkey_mask_ff &&
2227 (mask == 0xffff || mask == 0xffffff ||
2228 mask == 0xffffffff)) {
2229 tp_warned.hotkey_mask_ff = 1;
2230 printk(TPACPI_NOTICE
2231 "setting the hotkey mask to 0x%08x is likely "
2232 "not the best way to go about it\n", mask);
2233 printk(TPACPI_NOTICE
2234 "please consider using the driver defaults, "
2235 "and refer to up-to-date thinkpad-acpi "
2236 "documentation\n");
2239 /* Try to enable what the user asked for, plus whatever we need.
2240 * this syncs everything but won't enable bits in hotkey_user_mask */
2241 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2243 /* Enable the available bits in hotkey_user_mask */
2244 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2246 return rc;
2250 * Sets the driver hotkey mask.
2252 * Can be called even if the hotkey subdriver is inactive
2254 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2256 int rc;
2258 /* Do the right thing if hotkey_init has not been called yet */
2259 if (!tp_features.hotkey) {
2260 hotkey_driver_mask = mask;
2261 return 0;
2264 mutex_lock(&hotkey_mutex);
2266 HOTKEY_CONFIG_CRITICAL_START
2267 hotkey_driver_mask = mask;
2268 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2269 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2270 #endif
2271 HOTKEY_CONFIG_CRITICAL_END
2273 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2274 ~hotkey_source_mask);
2275 hotkey_poll_setup(true);
2277 mutex_unlock(&hotkey_mutex);
2279 return rc;
2282 static int hotkey_status_get(int *status)
2284 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2285 return -EIO;
2287 return 0;
2290 static int hotkey_status_set(bool enable)
2292 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2293 return -EIO;
2295 return 0;
2298 static void tpacpi_input_send_tabletsw(void)
2300 int state;
2302 if (tp_features.hotkey_tablet &&
2303 !hotkey_get_tablet_mode(&state)) {
2304 mutex_lock(&tpacpi_inputdev_send_mutex);
2306 input_report_switch(tpacpi_inputdev,
2307 SW_TABLET_MODE, !!state);
2308 input_sync(tpacpi_inputdev);
2310 mutex_unlock(&tpacpi_inputdev_send_mutex);
2314 /* Do NOT call without validating scancode first */
2315 static void tpacpi_input_send_key(const unsigned int scancode)
2317 const unsigned int keycode = hotkey_keycode_map[scancode];
2319 if (keycode != KEY_RESERVED) {
2320 mutex_lock(&tpacpi_inputdev_send_mutex);
2322 input_report_key(tpacpi_inputdev, keycode, 1);
2323 if (keycode == KEY_UNKNOWN)
2324 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2325 scancode);
2326 input_sync(tpacpi_inputdev);
2328 input_report_key(tpacpi_inputdev, keycode, 0);
2329 if (keycode == KEY_UNKNOWN)
2330 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2331 scancode);
2332 input_sync(tpacpi_inputdev);
2334 mutex_unlock(&tpacpi_inputdev_send_mutex);
2338 /* Do NOT call without validating scancode first */
2339 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2341 hotkey_driver_event(scancode);
2342 if (hotkey_user_mask & (1 << scancode))
2343 tpacpi_input_send_key(scancode);
2346 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2347 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2349 /* Do NOT call without validating scancode first */
2350 static void tpacpi_hotkey_send_key(unsigned int scancode)
2352 tpacpi_input_send_key_masked(scancode);
2353 if (hotkey_report_mode < 2) {
2354 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2355 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2359 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2361 u8 d;
2363 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2365 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2366 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2367 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2368 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2370 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2371 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2372 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2374 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2375 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2376 n->displayexp_toggle =
2377 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2379 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2380 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2381 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2382 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2383 n->brightness_toggle =
2384 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2386 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2387 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2388 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2389 >> TP_NVRAM_POS_LEVEL_VOLUME;
2390 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2391 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2395 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2396 struct tp_nvram_state *newn,
2397 const u32 event_mask)
2400 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2401 do { \
2402 if ((event_mask & (1 << __scancode)) && \
2403 oldn->__member != newn->__member) \
2404 tpacpi_hotkey_send_key(__scancode); \
2405 } while (0)
2407 #define TPACPI_MAY_SEND_KEY(__scancode) \
2408 do { \
2409 if (event_mask & (1 << __scancode)) \
2410 tpacpi_hotkey_send_key(__scancode); \
2411 } while (0)
2413 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2414 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2415 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2416 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2418 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2420 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2422 /* handle volume */
2423 if (oldn->volume_toggle != newn->volume_toggle) {
2424 if (oldn->mute != newn->mute) {
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2427 if (oldn->volume_level > newn->volume_level) {
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2429 } else if (oldn->volume_level < newn->volume_level) {
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2431 } else if (oldn->mute == newn->mute) {
2432 /* repeated key presses that didn't change state */
2433 if (newn->mute) {
2434 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2435 } else if (newn->volume_level != 0) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2437 } else {
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2443 /* handle brightness */
2444 if (oldn->brightness_toggle != newn->brightness_toggle) {
2445 if (oldn->brightness_level < newn->brightness_level) {
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2447 } else if (oldn->brightness_level > newn->brightness_level) {
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2449 } else {
2450 /* repeated key presses that didn't change state */
2451 if (newn->brightness_level != 0) {
2452 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2453 } else {
2454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2459 #undef TPACPI_COMPARE_KEY
2460 #undef TPACPI_MAY_SEND_KEY
2464 * Polling driver
2466 * We track all events in hotkey_source_mask all the time, since
2467 * most of them are edge-based. We only issue those requested by
2468 * hotkey_user_mask or hotkey_driver_mask, though.
2470 static int hotkey_kthread(void *data)
2472 struct tp_nvram_state s[2];
2473 u32 poll_mask, event_mask;
2474 unsigned int si, so;
2475 unsigned long t;
2476 unsigned int change_detector, must_reset;
2477 unsigned int poll_freq;
2479 mutex_lock(&hotkey_thread_mutex);
2481 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2482 goto exit;
2484 set_freezable();
2486 so = 0;
2487 si = 1;
2488 t = 0;
2490 /* Initial state for compares */
2491 mutex_lock(&hotkey_thread_data_mutex);
2492 change_detector = hotkey_config_change;
2493 poll_mask = hotkey_source_mask;
2494 event_mask = hotkey_source_mask &
2495 (hotkey_driver_mask | hotkey_user_mask);
2496 poll_freq = hotkey_poll_freq;
2497 mutex_unlock(&hotkey_thread_data_mutex);
2498 hotkey_read_nvram(&s[so], poll_mask);
2500 while (!kthread_should_stop()) {
2501 if (t == 0) {
2502 if (likely(poll_freq))
2503 t = 1000/poll_freq;
2504 else
2505 t = 100; /* should never happen... */
2507 t = msleep_interruptible(t);
2508 if (unlikely(kthread_should_stop()))
2509 break;
2510 must_reset = try_to_freeze();
2511 if (t > 0 && !must_reset)
2512 continue;
2514 mutex_lock(&hotkey_thread_data_mutex);
2515 if (must_reset || hotkey_config_change != change_detector) {
2516 /* forget old state on thaw or config change */
2517 si = so;
2518 t = 0;
2519 change_detector = hotkey_config_change;
2521 poll_mask = hotkey_source_mask;
2522 event_mask = hotkey_source_mask &
2523 (hotkey_driver_mask | hotkey_user_mask);
2524 poll_freq = hotkey_poll_freq;
2525 mutex_unlock(&hotkey_thread_data_mutex);
2527 if (likely(poll_mask)) {
2528 hotkey_read_nvram(&s[si], poll_mask);
2529 if (likely(si != so)) {
2530 hotkey_compare_and_issue_event(&s[so], &s[si],
2531 event_mask);
2535 so = si;
2536 si ^= 1;
2539 exit:
2540 mutex_unlock(&hotkey_thread_mutex);
2541 return 0;
2544 /* call with hotkey_mutex held */
2545 static void hotkey_poll_stop_sync(void)
2547 if (tpacpi_hotkey_task) {
2548 if (frozen(tpacpi_hotkey_task) ||
2549 freezing(tpacpi_hotkey_task))
2550 thaw_process(tpacpi_hotkey_task);
2552 kthread_stop(tpacpi_hotkey_task);
2553 tpacpi_hotkey_task = NULL;
2554 mutex_lock(&hotkey_thread_mutex);
2555 /* at this point, the thread did exit */
2556 mutex_unlock(&hotkey_thread_mutex);
2560 /* call with hotkey_mutex held */
2561 static void hotkey_poll_setup(const bool may_warn)
2563 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2564 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2566 if (hotkey_poll_freq > 0 &&
2567 (poll_driver_mask ||
2568 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2569 if (!tpacpi_hotkey_task) {
2570 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2571 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2572 if (IS_ERR(tpacpi_hotkey_task)) {
2573 tpacpi_hotkey_task = NULL;
2574 printk(TPACPI_ERR
2575 "could not create kernel thread "
2576 "for hotkey polling\n");
2579 } else {
2580 hotkey_poll_stop_sync();
2581 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2582 hotkey_poll_freq == 0) {
2583 printk(TPACPI_NOTICE
2584 "hot keys 0x%08x and/or events 0x%08x "
2585 "require polling, which is currently "
2586 "disabled\n",
2587 poll_user_mask, poll_driver_mask);
2592 static void hotkey_poll_setup_safe(const bool may_warn)
2594 mutex_lock(&hotkey_mutex);
2595 hotkey_poll_setup(may_warn);
2596 mutex_unlock(&hotkey_mutex);
2599 /* call with hotkey_mutex held */
2600 static void hotkey_poll_set_freq(unsigned int freq)
2602 if (!freq)
2603 hotkey_poll_stop_sync();
2605 hotkey_poll_freq = freq;
2608 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2610 static void hotkey_poll_setup(const bool __unused)
2614 static void hotkey_poll_setup_safe(const bool __unused)
2618 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2620 static int hotkey_inputdev_open(struct input_dev *dev)
2622 switch (tpacpi_lifecycle) {
2623 case TPACPI_LIFE_INIT:
2624 case TPACPI_LIFE_RUNNING:
2625 hotkey_poll_setup_safe(false);
2626 return 0;
2627 case TPACPI_LIFE_EXITING:
2628 return -EBUSY;
2631 /* Should only happen if tpacpi_lifecycle is corrupt */
2632 BUG();
2633 return -EBUSY;
2636 static void hotkey_inputdev_close(struct input_dev *dev)
2638 /* disable hotkey polling when possible */
2639 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2640 !(hotkey_source_mask & hotkey_driver_mask))
2641 hotkey_poll_setup_safe(false);
2644 /* sysfs hotkey enable ------------------------------------------------- */
2645 static ssize_t hotkey_enable_show(struct device *dev,
2646 struct device_attribute *attr,
2647 char *buf)
2649 int res, status;
2651 printk_deprecated_attribute("hotkey_enable",
2652 "Hotkey reporting is always enabled");
2654 res = hotkey_status_get(&status);
2655 if (res)
2656 return res;
2658 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2661 static ssize_t hotkey_enable_store(struct device *dev,
2662 struct device_attribute *attr,
2663 const char *buf, size_t count)
2665 unsigned long t;
2667 printk_deprecated_attribute("hotkey_enable",
2668 "Hotkeys can be disabled through hotkey_mask");
2670 if (parse_strtoul(buf, 1, &t))
2671 return -EINVAL;
2673 if (t == 0)
2674 return -EPERM;
2676 return count;
2679 static struct device_attribute dev_attr_hotkey_enable =
2680 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2681 hotkey_enable_show, hotkey_enable_store);
2683 /* sysfs hotkey mask --------------------------------------------------- */
2684 static ssize_t hotkey_mask_show(struct device *dev,
2685 struct device_attribute *attr,
2686 char *buf)
2688 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2691 static ssize_t hotkey_mask_store(struct device *dev,
2692 struct device_attribute *attr,
2693 const char *buf, size_t count)
2695 unsigned long t;
2696 int res;
2698 if (parse_strtoul(buf, 0xffffffffUL, &t))
2699 return -EINVAL;
2701 if (mutex_lock_killable(&hotkey_mutex))
2702 return -ERESTARTSYS;
2704 res = hotkey_user_mask_set(t);
2706 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2707 hotkey_poll_setup(true);
2708 #endif
2710 mutex_unlock(&hotkey_mutex);
2712 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2714 return (res) ? res : count;
2717 static struct device_attribute dev_attr_hotkey_mask =
2718 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2719 hotkey_mask_show, hotkey_mask_store);
2721 /* sysfs hotkey bios_enabled ------------------------------------------- */
2722 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2723 struct device_attribute *attr,
2724 char *buf)
2726 return sprintf(buf, "0\n");
2729 static struct device_attribute dev_attr_hotkey_bios_enabled =
2730 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2732 /* sysfs hotkey bios_mask ---------------------------------------------- */
2733 static ssize_t hotkey_bios_mask_show(struct device *dev,
2734 struct device_attribute *attr,
2735 char *buf)
2737 printk_deprecated_attribute("hotkey_bios_mask",
2738 "This attribute is useless.");
2739 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2742 static struct device_attribute dev_attr_hotkey_bios_mask =
2743 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2745 /* sysfs hotkey all_mask ----------------------------------------------- */
2746 static ssize_t hotkey_all_mask_show(struct device *dev,
2747 struct device_attribute *attr,
2748 char *buf)
2750 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2751 hotkey_all_mask | hotkey_source_mask);
2754 static struct device_attribute dev_attr_hotkey_all_mask =
2755 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2757 /* sysfs hotkey recommended_mask --------------------------------------- */
2758 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2762 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2763 (hotkey_all_mask | hotkey_source_mask)
2764 & ~hotkey_reserved_mask);
2767 static struct device_attribute dev_attr_hotkey_recommended_mask =
2768 __ATTR(hotkey_recommended_mask, S_IRUGO,
2769 hotkey_recommended_mask_show, NULL);
2771 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2773 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2774 static ssize_t hotkey_source_mask_show(struct device *dev,
2775 struct device_attribute *attr,
2776 char *buf)
2778 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2781 static ssize_t hotkey_source_mask_store(struct device *dev,
2782 struct device_attribute *attr,
2783 const char *buf, size_t count)
2785 unsigned long t;
2786 u32 r_ev;
2787 int rc;
2789 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2790 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2791 return -EINVAL;
2793 if (mutex_lock_killable(&hotkey_mutex))
2794 return -ERESTARTSYS;
2796 HOTKEY_CONFIG_CRITICAL_START
2797 hotkey_source_mask = t;
2798 HOTKEY_CONFIG_CRITICAL_END
2800 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2801 ~hotkey_source_mask);
2802 hotkey_poll_setup(true);
2804 /* check if events needed by the driver got disabled */
2805 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2806 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2808 mutex_unlock(&hotkey_mutex);
2810 if (rc < 0)
2811 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2812 "firmware event mask!\n");
2814 if (r_ev)
2815 printk(TPACPI_NOTICE "hotkey_source_mask: "
2816 "some important events were disabled: "
2817 "0x%04x\n", r_ev);
2819 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2821 return (rc < 0) ? rc : count;
2824 static struct device_attribute dev_attr_hotkey_source_mask =
2825 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2826 hotkey_source_mask_show, hotkey_source_mask_store);
2828 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2829 static ssize_t hotkey_poll_freq_show(struct device *dev,
2830 struct device_attribute *attr,
2831 char *buf)
2833 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2836 static ssize_t hotkey_poll_freq_store(struct device *dev,
2837 struct device_attribute *attr,
2838 const char *buf, size_t count)
2840 unsigned long t;
2842 if (parse_strtoul(buf, 25, &t))
2843 return -EINVAL;
2845 if (mutex_lock_killable(&hotkey_mutex))
2846 return -ERESTARTSYS;
2848 hotkey_poll_set_freq(t);
2849 hotkey_poll_setup(true);
2851 mutex_unlock(&hotkey_mutex);
2853 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2855 return count;
2858 static struct device_attribute dev_attr_hotkey_poll_freq =
2859 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2860 hotkey_poll_freq_show, hotkey_poll_freq_store);
2862 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2864 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2865 static ssize_t hotkey_radio_sw_show(struct device *dev,
2866 struct device_attribute *attr,
2867 char *buf)
2869 int res;
2870 res = hotkey_get_wlsw();
2871 if (res < 0)
2872 return res;
2874 /* Opportunistic update */
2875 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2877 return snprintf(buf, PAGE_SIZE, "%d\n",
2878 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2881 static struct device_attribute dev_attr_hotkey_radio_sw =
2882 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2884 static void hotkey_radio_sw_notify_change(void)
2886 if (tp_features.hotkey_wlsw)
2887 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2888 "hotkey_radio_sw");
2891 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2892 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2893 struct device_attribute *attr,
2894 char *buf)
2896 int res, s;
2897 res = hotkey_get_tablet_mode(&s);
2898 if (res < 0)
2899 return res;
2901 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2904 static struct device_attribute dev_attr_hotkey_tablet_mode =
2905 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2907 static void hotkey_tablet_mode_notify_change(void)
2909 if (tp_features.hotkey_tablet)
2910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2911 "hotkey_tablet_mode");
2914 /* sysfs hotkey report_mode -------------------------------------------- */
2915 static ssize_t hotkey_report_mode_show(struct device *dev,
2916 struct device_attribute *attr,
2917 char *buf)
2919 return snprintf(buf, PAGE_SIZE, "%d\n",
2920 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2923 static struct device_attribute dev_attr_hotkey_report_mode =
2924 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2926 /* sysfs wakeup reason (pollable) -------------------------------------- */
2927 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2928 struct device_attribute *attr,
2929 char *buf)
2931 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2934 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2935 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2937 static void hotkey_wakeup_reason_notify_change(void)
2939 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2940 "wakeup_reason");
2943 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2944 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2945 struct device_attribute *attr,
2946 char *buf)
2948 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2951 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2952 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2953 hotkey_wakeup_hotunplug_complete_show, NULL);
2955 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2957 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2958 "wakeup_hotunplug_complete");
2961 /* --------------------------------------------------------------------- */
2963 static struct attribute *hotkey_attributes[] __initdata = {
2964 &dev_attr_hotkey_enable.attr,
2965 &dev_attr_hotkey_bios_enabled.attr,
2966 &dev_attr_hotkey_bios_mask.attr,
2967 &dev_attr_hotkey_report_mode.attr,
2968 &dev_attr_hotkey_wakeup_reason.attr,
2969 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2970 &dev_attr_hotkey_mask.attr,
2971 &dev_attr_hotkey_all_mask.attr,
2972 &dev_attr_hotkey_recommended_mask.attr,
2973 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2974 &dev_attr_hotkey_source_mask.attr,
2975 &dev_attr_hotkey_poll_freq.attr,
2976 #endif
2980 * Sync both the hw and sw blocking state of all switches
2982 static void tpacpi_send_radiosw_update(void)
2984 int wlsw;
2987 * We must sync all rfkill controllers *before* issuing any
2988 * rfkill input events, or we will race the rfkill core input
2989 * handler.
2991 * tpacpi_inputdev_send_mutex works as a syncronization point
2992 * for the above.
2994 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2997 wlsw = hotkey_get_wlsw();
2999 /* Sync hw blocking state first if it is hw-blocked */
3000 if (wlsw == TPACPI_RFK_RADIO_OFF)
3001 tpacpi_rfk_update_hwblock_state(true);
3003 /* Sync sw blocking state */
3004 tpacpi_rfk_update_swstate_all();
3006 /* Sync hw blocking state last if it is hw-unblocked */
3007 if (wlsw == TPACPI_RFK_RADIO_ON)
3008 tpacpi_rfk_update_hwblock_state(false);
3010 /* Issue rfkill input event for WLSW switch */
3011 if (!(wlsw < 0)) {
3012 mutex_lock(&tpacpi_inputdev_send_mutex);
3014 input_report_switch(tpacpi_inputdev,
3015 SW_RFKILL_ALL, (wlsw > 0));
3016 input_sync(tpacpi_inputdev);
3018 mutex_unlock(&tpacpi_inputdev_send_mutex);
3022 * this can be unconditional, as we will poll state again
3023 * if userspace uses the notify to read data
3025 hotkey_radio_sw_notify_change();
3028 static void hotkey_exit(void)
3030 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3031 mutex_lock(&hotkey_mutex);
3032 hotkey_poll_stop_sync();
3033 mutex_unlock(&hotkey_mutex);
3034 #endif
3036 if (hotkey_dev_attributes)
3037 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3039 kfree(hotkey_keycode_map);
3041 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3042 "restoring original HKEY status and mask\n");
3043 /* yes, there is a bitwise or below, we want the
3044 * functions to be called even if one of them fail */
3045 if (((tp_features.hotkey_mask &&
3046 hotkey_mask_set(hotkey_orig_mask)) |
3047 hotkey_status_set(false)) != 0)
3048 printk(TPACPI_ERR
3049 "failed to restore hot key mask "
3050 "to BIOS defaults\n");
3053 static void __init hotkey_unmap(const unsigned int scancode)
3055 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3056 clear_bit(hotkey_keycode_map[scancode],
3057 tpacpi_inputdev->keybit);
3058 hotkey_keycode_map[scancode] = KEY_RESERVED;
3063 * HKEY quirks:
3064 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3067 #define TPACPI_HK_Q_INIMASK 0x0001
3069 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3070 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3071 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3072 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3073 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3074 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3075 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3076 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3077 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3078 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3079 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3080 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3081 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3082 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3083 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3084 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3085 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3086 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3087 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3088 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3091 static int __init hotkey_init(struct ibm_init_struct *iibm)
3093 /* Requirements for changing the default keymaps:
3095 * 1. Many of the keys are mapped to KEY_RESERVED for very
3096 * good reasons. Do not change them unless you have deep
3097 * knowledge on the IBM and Lenovo ThinkPad firmware for
3098 * the various ThinkPad models. The driver behaves
3099 * differently for KEY_RESERVED: such keys have their
3100 * hot key mask *unset* in mask_recommended, and also
3101 * in the initial hot key mask programmed into the
3102 * firmware at driver load time, which means the firm-
3103 * ware may react very differently if you change them to
3104 * something else;
3106 * 2. You must be subscribed to the linux-thinkpad and
3107 * ibm-acpi-devel mailing lists, and you should read the
3108 * list archives since 2007 if you want to change the
3109 * keymaps. This requirement exists so that you will
3110 * know the past history of problems with the thinkpad-
3111 * acpi driver keymaps, and also that you will be
3112 * listening to any bug reports;
3114 * 3. Do not send thinkpad-acpi specific patches directly to
3115 * for merging, *ever*. Send them to the linux-acpi
3116 * mailinglist for comments. Merging is to be done only
3117 * through acpi-test and the ACPI maintainer.
3119 * If the above is too much to ask, don't change the keymap.
3120 * Ask the thinkpad-acpi maintainer to do it, instead.
3122 static u16 ibm_keycode_map[] __initdata = {
3123 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3124 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3125 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3126 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3128 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3129 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3130 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3131 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3133 /* brightness: firmware always reacts to them */
3134 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3135 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3137 /* Thinklight: firmware always react to it */
3138 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3140 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3141 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3143 /* Volume: firmware always react to it and reprograms
3144 * the built-in *extra* mixer. Never map it to control
3145 * another mixer by default. */
3146 KEY_RESERVED, /* 0x14: VOLUME UP */
3147 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3148 KEY_RESERVED, /* 0x16: MUTE */
3150 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3152 /* (assignments unknown, please report if found) */
3153 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3154 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3156 static u16 lenovo_keycode_map[] __initdata = {
3157 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3158 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3159 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3160 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3162 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3163 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3164 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3165 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3167 /* These should be enabled --only-- when ACPI video
3168 * is disabled (i.e. in "vendor" mode), and are handled
3169 * in a special way by the init code */
3170 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3171 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3173 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3175 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3176 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3178 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3179 * react to it and reprograms the built-in *extra* mixer.
3180 * Never map it to control another mixer by default.
3182 * T60?, T61, R60?, R61: firmware and EC tries to send
3183 * these over the regular keyboard, so these are no-ops,
3184 * but there are still weird bugs re. MUTE, so do not
3185 * change unless you get test reports from all Lenovo
3186 * models. May cause the BIOS to interfere with the
3187 * HDA mixer.
3189 KEY_RESERVED, /* 0x14: VOLUME UP */
3190 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3191 KEY_RESERVED, /* 0x16: MUTE */
3193 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3195 /* (assignments unknown, please report if found) */
3196 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3197 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3200 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3201 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3202 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3204 int res, i;
3205 int status;
3206 int hkeyv;
3207 bool radiosw_state = false;
3208 bool tabletsw_state = false;
3210 unsigned long quirks;
3212 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3213 "initializing hotkey subdriver\n");
3215 BUG_ON(!tpacpi_inputdev);
3216 BUG_ON(tpacpi_inputdev->open != NULL ||
3217 tpacpi_inputdev->close != NULL);
3219 TPACPI_ACPIHANDLE_INIT(hkey);
3220 mutex_init(&hotkey_mutex);
3222 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3223 mutex_init(&hotkey_thread_mutex);
3224 mutex_init(&hotkey_thread_data_mutex);
3225 #endif
3227 /* hotkey not supported on 570 */
3228 tp_features.hotkey = hkey_handle != NULL;
3230 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3231 "hotkeys are %s\n",
3232 str_supported(tp_features.hotkey));
3234 if (!tp_features.hotkey)
3235 return 1;
3237 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3238 ARRAY_SIZE(tpacpi_hotkey_qtable));
3240 tpacpi_disable_brightness_delay();
3242 /* MUST have enough space for all attributes to be added to
3243 * hotkey_dev_attributes */
3244 hotkey_dev_attributes = create_attr_set(
3245 ARRAY_SIZE(hotkey_attributes) + 2,
3246 NULL);
3247 if (!hotkey_dev_attributes)
3248 return -ENOMEM;
3249 res = add_many_to_attr_set(hotkey_dev_attributes,
3250 hotkey_attributes,
3251 ARRAY_SIZE(hotkey_attributes));
3252 if (res)
3253 goto err_exit;
3255 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3256 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3257 for HKEY interface version 0x100 */
3258 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3259 if ((hkeyv >> 8) != 1) {
3260 printk(TPACPI_ERR "unknown version of the "
3261 "HKEY interface: 0x%x\n", hkeyv);
3262 printk(TPACPI_ERR "please report this to %s\n",
3263 TPACPI_MAIL);
3264 } else {
3266 * MHKV 0x100 in A31, R40, R40e,
3267 * T4x, X31, and later
3269 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3270 "firmware HKEY interface version: 0x%x\n",
3271 hkeyv);
3273 /* Paranoia check AND init hotkey_all_mask */
3274 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3275 "MHKA", "qd")) {
3276 printk(TPACPI_ERR
3277 "missing MHKA handler, "
3278 "please report this to %s\n",
3279 TPACPI_MAIL);
3280 /* Fallback: pre-init for FN+F3,F4,F12 */
3281 hotkey_all_mask = 0x080cU;
3282 } else {
3283 tp_features.hotkey_mask = 1;
3288 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3289 "hotkey masks are %s\n",
3290 str_supported(tp_features.hotkey_mask));
3292 /* Init hotkey_all_mask if not initialized yet */
3293 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3294 (quirks & TPACPI_HK_Q_INIMASK))
3295 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3297 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3298 if (tp_features.hotkey_mask) {
3299 /* hotkey_source_mask *must* be zero for
3300 * the first hotkey_mask_get to return hotkey_orig_mask */
3301 res = hotkey_mask_get();
3302 if (res)
3303 goto err_exit;
3305 hotkey_orig_mask = hotkey_acpi_mask;
3306 } else {
3307 hotkey_orig_mask = hotkey_all_mask;
3308 hotkey_acpi_mask = hotkey_all_mask;
3311 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3312 if (dbg_wlswemul) {
3313 tp_features.hotkey_wlsw = 1;
3314 radiosw_state = !!tpacpi_wlsw_emulstate;
3315 printk(TPACPI_INFO
3316 "radio switch emulation enabled\n");
3317 } else
3318 #endif
3319 /* Not all thinkpads have a hardware radio switch */
3320 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3321 tp_features.hotkey_wlsw = 1;
3322 radiosw_state = !!status;
3323 printk(TPACPI_INFO
3324 "radio switch found; radios are %s\n",
3325 enabled(status, 0));
3327 if (tp_features.hotkey_wlsw)
3328 res = add_to_attr_set(hotkey_dev_attributes,
3329 &dev_attr_hotkey_radio_sw.attr);
3331 /* For X41t, X60t, X61t Tablets... */
3332 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3333 tp_features.hotkey_tablet = 1;
3334 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3335 printk(TPACPI_INFO
3336 "possible tablet mode switch found; "
3337 "ThinkPad in %s mode\n",
3338 (tabletsw_state) ? "tablet" : "laptop");
3339 res = add_to_attr_set(hotkey_dev_attributes,
3340 &dev_attr_hotkey_tablet_mode.attr);
3343 if (!res)
3344 res = register_attr_set_with_sysfs(
3345 hotkey_dev_attributes,
3346 &tpacpi_pdev->dev.kobj);
3347 if (res)
3348 goto err_exit;
3350 /* Set up key map */
3352 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3353 GFP_KERNEL);
3354 if (!hotkey_keycode_map) {
3355 printk(TPACPI_ERR
3356 "failed to allocate memory for key map\n");
3357 res = -ENOMEM;
3358 goto err_exit;
3361 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3362 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3363 "using Lenovo default hot key map\n");
3364 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3365 TPACPI_HOTKEY_MAP_SIZE);
3366 } else {
3367 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3368 "using IBM default hot key map\n");
3369 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3370 TPACPI_HOTKEY_MAP_SIZE);
3373 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3374 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3375 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3376 tpacpi_inputdev->keycode = hotkey_keycode_map;
3377 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3378 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3379 input_set_capability(tpacpi_inputdev, EV_KEY,
3380 hotkey_keycode_map[i]);
3381 } else {
3382 if (i < sizeof(hotkey_reserved_mask)*8)
3383 hotkey_reserved_mask |= 1 << i;
3387 if (tp_features.hotkey_wlsw) {
3388 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3389 input_report_switch(tpacpi_inputdev,
3390 SW_RFKILL_ALL, radiosw_state);
3392 if (tp_features.hotkey_tablet) {
3393 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3394 input_report_switch(tpacpi_inputdev,
3395 SW_TABLET_MODE, tabletsw_state);
3398 /* Do not issue duplicate brightness change events to
3399 * userspace */
3400 if (!tp_features.bright_acpimode)
3401 /* update bright_acpimode... */
3402 tpacpi_check_std_acpi_brightness_support();
3404 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3405 printk(TPACPI_INFO
3406 "This ThinkPad has standard ACPI backlight "
3407 "brightness control, supported by the ACPI "
3408 "video driver\n");
3409 printk(TPACPI_NOTICE
3410 "Disabling thinkpad-acpi brightness events "
3411 "by default...\n");
3413 /* Disable brightness up/down on Lenovo thinkpads when
3414 * ACPI is handling them, otherwise it is plain impossible
3415 * for userspace to do something even remotely sane */
3416 hotkey_reserved_mask |=
3417 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3418 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3419 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3420 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3424 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3425 & ~hotkey_all_mask
3426 & ~hotkey_reserved_mask;
3428 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3429 "hotkey source mask 0x%08x, polling freq %u\n",
3430 hotkey_source_mask, hotkey_poll_freq);
3431 #endif
3433 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3434 "enabling firmware HKEY event interface...\n");
3435 res = hotkey_status_set(true);
3436 if (res) {
3437 hotkey_exit();
3438 return res;
3440 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3441 | hotkey_driver_mask)
3442 & ~hotkey_source_mask);
3443 if (res < 0 && res != -ENXIO) {
3444 hotkey_exit();
3445 return res;
3447 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3448 & ~hotkey_reserved_mask;
3449 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3450 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3451 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3453 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3454 "legacy ibm/hotkey event reporting over procfs %s\n",
3455 (hotkey_report_mode < 2) ?
3456 "enabled" : "disabled");
3458 tpacpi_inputdev->open = &hotkey_inputdev_open;
3459 tpacpi_inputdev->close = &hotkey_inputdev_close;
3461 hotkey_poll_setup_safe(true);
3463 return 0;
3465 err_exit:
3466 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3467 hotkey_dev_attributes = NULL;
3469 return (res < 0)? res : 1;
3472 static bool hotkey_notify_hotkey(const u32 hkey,
3473 bool *send_acpi_ev,
3474 bool *ignore_acpi_ev)
3476 /* 0x1000-0x1FFF: key presses */
3477 unsigned int scancode = hkey & 0xfff;
3478 *send_acpi_ev = true;
3479 *ignore_acpi_ev = false;
3481 if (scancode > 0 && scancode < 0x21) {
3482 scancode--;
3483 if (!(hotkey_source_mask & (1 << scancode))) {
3484 tpacpi_input_send_key_masked(scancode);
3485 *send_acpi_ev = false;
3486 } else {
3487 *ignore_acpi_ev = true;
3489 return true;
3491 return false;
3494 static bool hotkey_notify_wakeup(const u32 hkey,
3495 bool *send_acpi_ev,
3496 bool *ignore_acpi_ev)
3498 /* 0x2000-0x2FFF: Wakeup reason */
3499 *send_acpi_ev = true;
3500 *ignore_acpi_ev = false;
3502 switch (hkey) {
3503 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3504 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3505 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3506 *ignore_acpi_ev = true;
3507 break;
3509 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3510 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3511 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3512 *ignore_acpi_ev = true;
3513 break;
3515 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3516 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3517 printk(TPACPI_ALERT
3518 "EMERGENCY WAKEUP: battery almost empty\n");
3519 /* how to auto-heal: */
3520 /* 2313: woke up from S3, go to S4/S5 */
3521 /* 2413: woke up from S4, go to S5 */
3522 break;
3524 default:
3525 return false;
3528 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3529 printk(TPACPI_INFO
3530 "woke up due to a hot-unplug "
3531 "request...\n");
3532 hotkey_wakeup_reason_notify_change();
3534 return true;
3537 static bool hotkey_notify_usrevent(const u32 hkey,
3538 bool *send_acpi_ev,
3539 bool *ignore_acpi_ev)
3541 /* 0x5000-0x5FFF: human interface helpers */
3542 *send_acpi_ev = true;
3543 *ignore_acpi_ev = false;
3545 switch (hkey) {
3546 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3547 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3548 return true;
3550 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3551 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3552 tpacpi_input_send_tabletsw();
3553 hotkey_tablet_mode_notify_change();
3554 *send_acpi_ev = false;
3555 return true;
3557 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3558 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3559 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3560 /* do not propagate these events */
3561 *ignore_acpi_ev = true;
3562 return true;
3564 default:
3565 return false;
3569 static void thermal_dump_all_sensors(void);
3571 static bool hotkey_notify_thermal(const u32 hkey,
3572 bool *send_acpi_ev,
3573 bool *ignore_acpi_ev)
3575 bool known = true;
3577 /* 0x6000-0x6FFF: thermal alarms */
3578 *send_acpi_ev = true;
3579 *ignore_acpi_ev = false;
3581 switch (hkey) {
3582 case TP_HKEY_EV_THM_TABLE_CHANGED:
3583 printk(TPACPI_INFO
3584 "EC reports that Thermal Table has changed\n");
3585 /* recommended action: do nothing, we don't have
3586 * Lenovo ATM information */
3587 return true;
3588 case TP_HKEY_EV_ALARM_BAT_HOT:
3589 printk(TPACPI_CRIT
3590 "THERMAL ALARM: battery is too hot!\n");
3591 /* recommended action: warn user through gui */
3592 break;
3593 case TP_HKEY_EV_ALARM_BAT_XHOT:
3594 printk(TPACPI_ALERT
3595 "THERMAL EMERGENCY: battery is extremely hot!\n");
3596 /* recommended action: immediate sleep/hibernate */
3597 break;
3598 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3599 printk(TPACPI_CRIT
3600 "THERMAL ALARM: "
3601 "a sensor reports something is too hot!\n");
3602 /* recommended action: warn user through gui, that */
3603 /* some internal component is too hot */
3604 break;
3605 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3606 printk(TPACPI_ALERT
3607 "THERMAL EMERGENCY: "
3608 "a sensor reports something is extremely hot!\n");
3609 /* recommended action: immediate sleep/hibernate */
3610 break;
3611 default:
3612 printk(TPACPI_ALERT
3613 "THERMAL ALERT: unknown thermal alarm received\n");
3614 known = false;
3617 thermal_dump_all_sensors();
3619 return known;
3622 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3624 u32 hkey;
3625 bool send_acpi_ev;
3626 bool ignore_acpi_ev;
3627 bool known_ev;
3629 if (event != 0x80) {
3630 printk(TPACPI_ERR
3631 "unknown HKEY notification event %d\n", event);
3632 /* forward it to userspace, maybe it knows how to handle it */
3633 acpi_bus_generate_netlink_event(
3634 ibm->acpi->device->pnp.device_class,
3635 dev_name(&ibm->acpi->device->dev),
3636 event, 0);
3637 return;
3640 while (1) {
3641 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3642 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3643 return;
3646 if (hkey == 0) {
3647 /* queue empty */
3648 return;
3651 send_acpi_ev = true;
3652 ignore_acpi_ev = false;
3654 switch (hkey >> 12) {
3655 case 1:
3656 /* 0x1000-0x1FFF: key presses */
3657 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3658 &ignore_acpi_ev);
3659 break;
3660 case 2:
3661 /* 0x2000-0x2FFF: Wakeup reason */
3662 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3663 &ignore_acpi_ev);
3664 break;
3665 case 3:
3666 /* 0x3000-0x3FFF: bay-related wakeups */
3667 switch (hkey) {
3668 case TP_HKEY_EV_BAYEJ_ACK:
3669 hotkey_autosleep_ack = 1;
3670 printk(TPACPI_INFO
3671 "bay ejected\n");
3672 hotkey_wakeup_hotunplug_complete_notify_change();
3673 known_ev = true;
3674 break;
3675 case TP_HKEY_EV_OPTDRV_EJ:
3676 /* FIXME: kick libata if SATA link offline */
3677 known_ev = true;
3678 break;
3679 default:
3680 known_ev = false;
3682 break;
3683 case 4:
3684 /* 0x4000-0x4FFF: dock-related wakeups */
3685 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3686 hotkey_autosleep_ack = 1;
3687 printk(TPACPI_INFO
3688 "undocked\n");
3689 hotkey_wakeup_hotunplug_complete_notify_change();
3690 known_ev = true;
3691 } else {
3692 known_ev = false;
3694 break;
3695 case 5:
3696 /* 0x5000-0x5FFF: human interface helpers */
3697 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3698 &ignore_acpi_ev);
3699 break;
3700 case 6:
3701 /* 0x6000-0x6FFF: thermal alarms */
3702 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3703 &ignore_acpi_ev);
3704 break;
3705 case 7:
3706 /* 0x7000-0x7FFF: misc */
3707 if (tp_features.hotkey_wlsw &&
3708 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3709 tpacpi_send_radiosw_update();
3710 send_acpi_ev = 0;
3711 known_ev = true;
3712 break;
3714 /* fallthrough to default */
3715 default:
3716 known_ev = false;
3718 if (!known_ev) {
3719 printk(TPACPI_NOTICE
3720 "unhandled HKEY event 0x%04x\n", hkey);
3721 printk(TPACPI_NOTICE
3722 "please report the conditions when this "
3723 "event happened to %s\n", TPACPI_MAIL);
3726 /* Legacy events */
3727 if (!ignore_acpi_ev &&
3728 (send_acpi_ev || hotkey_report_mode < 2)) {
3729 acpi_bus_generate_proc_event(ibm->acpi->device,
3730 event, hkey);
3733 /* netlink events */
3734 if (!ignore_acpi_ev && send_acpi_ev) {
3735 acpi_bus_generate_netlink_event(
3736 ibm->acpi->device->pnp.device_class,
3737 dev_name(&ibm->acpi->device->dev),
3738 event, hkey);
3743 static void hotkey_suspend(pm_message_t state)
3745 /* Do these on suspend, we get the events on early resume! */
3746 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3747 hotkey_autosleep_ack = 0;
3750 static void hotkey_resume(void)
3752 tpacpi_disable_brightness_delay();
3754 if (hotkey_status_set(true) < 0 ||
3755 hotkey_mask_set(hotkey_acpi_mask) < 0)
3756 printk(TPACPI_ERR
3757 "error while attempting to reset the event "
3758 "firmware interface\n");
3760 tpacpi_send_radiosw_update();
3761 hotkey_tablet_mode_notify_change();
3762 hotkey_wakeup_reason_notify_change();
3763 hotkey_wakeup_hotunplug_complete_notify_change();
3764 hotkey_poll_setup_safe(false);
3767 /* procfs -------------------------------------------------------------- */
3768 static int hotkey_read(struct seq_file *m)
3770 int res, status;
3772 if (!tp_features.hotkey) {
3773 seq_printf(m, "status:\t\tnot supported\n");
3774 return 0;
3777 if (mutex_lock_killable(&hotkey_mutex))
3778 return -ERESTARTSYS;
3779 res = hotkey_status_get(&status);
3780 if (!res)
3781 res = hotkey_mask_get();
3782 mutex_unlock(&hotkey_mutex);
3783 if (res)
3784 return res;
3786 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3787 if (hotkey_all_mask) {
3788 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3789 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3790 } else {
3791 seq_printf(m, "mask:\t\tnot supported\n");
3792 seq_printf(m, "commands:\tenable, disable, reset\n");
3795 return 0;
3798 static void hotkey_enabledisable_warn(bool enable)
3800 tpacpi_log_usertask("procfs hotkey enable/disable");
3801 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3802 TPACPI_WARN
3803 "hotkey enable/disable functionality has been "
3804 "removed from the driver. Hotkeys are always "
3805 "enabled\n"))
3806 printk(TPACPI_ERR
3807 "Please remove the hotkey=enable module "
3808 "parameter, it is deprecated. Hotkeys are always "
3809 "enabled\n");
3812 static int hotkey_write(char *buf)
3814 int res;
3815 u32 mask;
3816 char *cmd;
3818 if (!tp_features.hotkey)
3819 return -ENODEV;
3821 if (mutex_lock_killable(&hotkey_mutex))
3822 return -ERESTARTSYS;
3824 mask = hotkey_user_mask;
3826 res = 0;
3827 while ((cmd = next_cmd(&buf))) {
3828 if (strlencmp(cmd, "enable") == 0) {
3829 hotkey_enabledisable_warn(1);
3830 } else if (strlencmp(cmd, "disable") == 0) {
3831 hotkey_enabledisable_warn(0);
3832 res = -EPERM;
3833 } else if (strlencmp(cmd, "reset") == 0) {
3834 mask = (hotkey_all_mask | hotkey_source_mask)
3835 & ~hotkey_reserved_mask;
3836 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3837 /* mask set */
3838 } else if (sscanf(cmd, "%x", &mask) == 1) {
3839 /* mask set */
3840 } else {
3841 res = -EINVAL;
3842 goto errexit;
3846 if (!res) {
3847 tpacpi_disclose_usertask("procfs hotkey",
3848 "set mask to 0x%08x\n", mask);
3849 res = hotkey_user_mask_set(mask);
3852 errexit:
3853 mutex_unlock(&hotkey_mutex);
3854 return res;
3857 static const struct acpi_device_id ibm_htk_device_ids[] = {
3858 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3859 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3860 {"", 0},
3863 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3864 .hid = ibm_htk_device_ids,
3865 .notify = hotkey_notify,
3866 .handle = &hkey_handle,
3867 .type = ACPI_DEVICE_NOTIFY,
3870 static struct ibm_struct hotkey_driver_data = {
3871 .name = "hotkey",
3872 .read = hotkey_read,
3873 .write = hotkey_write,
3874 .exit = hotkey_exit,
3875 .resume = hotkey_resume,
3876 .suspend = hotkey_suspend,
3877 .acpi = &ibm_hotkey_acpidriver,
3880 /*************************************************************************
3881 * Bluetooth subdriver
3884 enum {
3885 /* ACPI GBDC/SBDC bits */
3886 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3887 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3888 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3889 0 = disable, 1 = enable */
3892 enum {
3893 /* ACPI \BLTH commands */
3894 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3895 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3896 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3897 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3898 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3901 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3903 static int bluetooth_get_status(void)
3905 int status;
3907 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3908 if (dbg_bluetoothemul)
3909 return (tpacpi_bluetooth_emulstate) ?
3910 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3911 #endif
3913 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3914 return -EIO;
3916 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3917 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3920 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3922 int status;
3924 vdbg_printk(TPACPI_DBG_RFKILL,
3925 "will attempt to %s bluetooth\n",
3926 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3928 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3929 if (dbg_bluetoothemul) {
3930 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3931 return 0;
3933 #endif
3935 if (state == TPACPI_RFK_RADIO_ON)
3936 status = TP_ACPI_BLUETOOTH_RADIOSSW
3937 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3938 else
3939 status = 0;
3941 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3942 return -EIO;
3944 return 0;
3947 /* sysfs bluetooth enable ---------------------------------------------- */
3948 static ssize_t bluetooth_enable_show(struct device *dev,
3949 struct device_attribute *attr,
3950 char *buf)
3952 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3953 attr, buf);
3956 static ssize_t bluetooth_enable_store(struct device *dev,
3957 struct device_attribute *attr,
3958 const char *buf, size_t count)
3960 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3961 attr, buf, count);
3964 static struct device_attribute dev_attr_bluetooth_enable =
3965 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3966 bluetooth_enable_show, bluetooth_enable_store);
3968 /* --------------------------------------------------------------------- */
3970 static struct attribute *bluetooth_attributes[] = {
3971 &dev_attr_bluetooth_enable.attr,
3972 NULL
3975 static const struct attribute_group bluetooth_attr_group = {
3976 .attrs = bluetooth_attributes,
3979 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3980 .get_status = bluetooth_get_status,
3981 .set_status = bluetooth_set_status,
3984 static void bluetooth_shutdown(void)
3986 /* Order firmware to save current state to NVRAM */
3987 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3988 TP_ACPI_BLTH_SAVE_STATE))
3989 printk(TPACPI_NOTICE
3990 "failed to save bluetooth state to NVRAM\n");
3991 else
3992 vdbg_printk(TPACPI_DBG_RFKILL,
3993 "bluestooth state saved to NVRAM\n");
3996 static void bluetooth_exit(void)
3998 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3999 &bluetooth_attr_group);
4001 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4003 bluetooth_shutdown();
4006 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4008 int res;
4009 int status = 0;
4011 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4012 "initializing bluetooth subdriver\n");
4014 TPACPI_ACPIHANDLE_INIT(hkey);
4016 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4017 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4018 tp_features.bluetooth = hkey_handle &&
4019 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4021 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4022 "bluetooth is %s, status 0x%02x\n",
4023 str_supported(tp_features.bluetooth),
4024 status);
4026 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4027 if (dbg_bluetoothemul) {
4028 tp_features.bluetooth = 1;
4029 printk(TPACPI_INFO
4030 "bluetooth switch emulation enabled\n");
4031 } else
4032 #endif
4033 if (tp_features.bluetooth &&
4034 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4035 /* no bluetooth hardware present in system */
4036 tp_features.bluetooth = 0;
4037 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4038 "bluetooth hardware not installed\n");
4041 if (!tp_features.bluetooth)
4042 return 1;
4044 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4045 &bluetooth_tprfk_ops,
4046 RFKILL_TYPE_BLUETOOTH,
4047 TPACPI_RFK_BLUETOOTH_SW_NAME,
4048 true);
4049 if (res)
4050 return res;
4052 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4053 &bluetooth_attr_group);
4054 if (res) {
4055 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4056 return res;
4059 return 0;
4062 /* procfs -------------------------------------------------------------- */
4063 static int bluetooth_read(struct seq_file *m)
4065 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4068 static int bluetooth_write(char *buf)
4070 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4073 static struct ibm_struct bluetooth_driver_data = {
4074 .name = "bluetooth",
4075 .read = bluetooth_read,
4076 .write = bluetooth_write,
4077 .exit = bluetooth_exit,
4078 .shutdown = bluetooth_shutdown,
4081 /*************************************************************************
4082 * Wan subdriver
4085 enum {
4086 /* ACPI GWAN/SWAN bits */
4087 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4088 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4089 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4090 0 = disable, 1 = enable */
4093 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4095 static int wan_get_status(void)
4097 int status;
4099 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4100 if (dbg_wwanemul)
4101 return (tpacpi_wwan_emulstate) ?
4102 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4103 #endif
4105 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4106 return -EIO;
4108 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4109 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4112 static int wan_set_status(enum tpacpi_rfkill_state state)
4114 int status;
4116 vdbg_printk(TPACPI_DBG_RFKILL,
4117 "will attempt to %s wwan\n",
4118 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4120 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4121 if (dbg_wwanemul) {
4122 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4123 return 0;
4125 #endif
4127 if (state == TPACPI_RFK_RADIO_ON)
4128 status = TP_ACPI_WANCARD_RADIOSSW
4129 | TP_ACPI_WANCARD_RESUMECTRL;
4130 else
4131 status = 0;
4133 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4134 return -EIO;
4136 return 0;
4139 /* sysfs wan enable ---------------------------------------------------- */
4140 static ssize_t wan_enable_show(struct device *dev,
4141 struct device_attribute *attr,
4142 char *buf)
4144 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4145 attr, buf);
4148 static ssize_t wan_enable_store(struct device *dev,
4149 struct device_attribute *attr,
4150 const char *buf, size_t count)
4152 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4153 attr, buf, count);
4156 static struct device_attribute dev_attr_wan_enable =
4157 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4158 wan_enable_show, wan_enable_store);
4160 /* --------------------------------------------------------------------- */
4162 static struct attribute *wan_attributes[] = {
4163 &dev_attr_wan_enable.attr,
4164 NULL
4167 static const struct attribute_group wan_attr_group = {
4168 .attrs = wan_attributes,
4171 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4172 .get_status = wan_get_status,
4173 .set_status = wan_set_status,
4176 static void wan_shutdown(void)
4178 /* Order firmware to save current state to NVRAM */
4179 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4180 TP_ACPI_WGSV_SAVE_STATE))
4181 printk(TPACPI_NOTICE
4182 "failed to save WWAN state to NVRAM\n");
4183 else
4184 vdbg_printk(TPACPI_DBG_RFKILL,
4185 "WWAN state saved to NVRAM\n");
4188 static void wan_exit(void)
4190 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4191 &wan_attr_group);
4193 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4195 wan_shutdown();
4198 static int __init wan_init(struct ibm_init_struct *iibm)
4200 int res;
4201 int status = 0;
4203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4204 "initializing wan subdriver\n");
4206 TPACPI_ACPIHANDLE_INIT(hkey);
4208 tp_features.wan = hkey_handle &&
4209 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4211 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4212 "wan is %s, status 0x%02x\n",
4213 str_supported(tp_features.wan),
4214 status);
4216 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4217 if (dbg_wwanemul) {
4218 tp_features.wan = 1;
4219 printk(TPACPI_INFO
4220 "wwan switch emulation enabled\n");
4221 } else
4222 #endif
4223 if (tp_features.wan &&
4224 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4225 /* no wan hardware present in system */
4226 tp_features.wan = 0;
4227 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4228 "wan hardware not installed\n");
4231 if (!tp_features.wan)
4232 return 1;
4234 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4235 &wan_tprfk_ops,
4236 RFKILL_TYPE_WWAN,
4237 TPACPI_RFK_WWAN_SW_NAME,
4238 true);
4239 if (res)
4240 return res;
4242 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4243 &wan_attr_group);
4245 if (res) {
4246 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4247 return res;
4250 return 0;
4253 /* procfs -------------------------------------------------------------- */
4254 static int wan_read(struct seq_file *m)
4256 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4259 static int wan_write(char *buf)
4261 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4264 static struct ibm_struct wan_driver_data = {
4265 .name = "wan",
4266 .read = wan_read,
4267 .write = wan_write,
4268 .exit = wan_exit,
4269 .shutdown = wan_shutdown,
4272 /*************************************************************************
4273 * UWB subdriver
4276 enum {
4277 /* ACPI GUWB/SUWB bits */
4278 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4279 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4282 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4284 static int uwb_get_status(void)
4286 int status;
4288 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4289 if (dbg_uwbemul)
4290 return (tpacpi_uwb_emulstate) ?
4291 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4292 #endif
4294 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4295 return -EIO;
4297 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4298 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4301 static int uwb_set_status(enum tpacpi_rfkill_state state)
4303 int status;
4305 vdbg_printk(TPACPI_DBG_RFKILL,
4306 "will attempt to %s UWB\n",
4307 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4309 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4310 if (dbg_uwbemul) {
4311 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4312 return 0;
4314 #endif
4316 if (state == TPACPI_RFK_RADIO_ON)
4317 status = TP_ACPI_UWB_RADIOSSW;
4318 else
4319 status = 0;
4321 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4322 return -EIO;
4324 return 0;
4327 /* --------------------------------------------------------------------- */
4329 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4330 .get_status = uwb_get_status,
4331 .set_status = uwb_set_status,
4334 static void uwb_exit(void)
4336 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4339 static int __init uwb_init(struct ibm_init_struct *iibm)
4341 int res;
4342 int status = 0;
4344 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4345 "initializing uwb subdriver\n");
4347 TPACPI_ACPIHANDLE_INIT(hkey);
4349 tp_features.uwb = hkey_handle &&
4350 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4352 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4353 "uwb is %s, status 0x%02x\n",
4354 str_supported(tp_features.uwb),
4355 status);
4357 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4358 if (dbg_uwbemul) {
4359 tp_features.uwb = 1;
4360 printk(TPACPI_INFO
4361 "uwb switch emulation enabled\n");
4362 } else
4363 #endif
4364 if (tp_features.uwb &&
4365 !(status & TP_ACPI_UWB_HWPRESENT)) {
4366 /* no uwb hardware present in system */
4367 tp_features.uwb = 0;
4368 dbg_printk(TPACPI_DBG_INIT,
4369 "uwb hardware not installed\n");
4372 if (!tp_features.uwb)
4373 return 1;
4375 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4376 &uwb_tprfk_ops,
4377 RFKILL_TYPE_UWB,
4378 TPACPI_RFK_UWB_SW_NAME,
4379 false);
4380 return res;
4383 static struct ibm_struct uwb_driver_data = {
4384 .name = "uwb",
4385 .exit = uwb_exit,
4386 .flags.experimental = 1,
4389 /*************************************************************************
4390 * Video subdriver
4393 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4395 enum video_access_mode {
4396 TPACPI_VIDEO_NONE = 0,
4397 TPACPI_VIDEO_570, /* 570 */
4398 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4399 TPACPI_VIDEO_NEW, /* all others */
4402 enum { /* video status flags, based on VIDEO_570 */
4403 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4404 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4405 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4408 enum { /* TPACPI_VIDEO_570 constants */
4409 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4410 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4411 * video_status_flags */
4412 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4413 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4416 static enum video_access_mode video_supported;
4417 static int video_orig_autosw;
4419 static int video_autosw_get(void);
4420 static int video_autosw_set(int enable);
4422 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4424 static int __init video_init(struct ibm_init_struct *iibm)
4426 int ivga;
4428 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4430 TPACPI_ACPIHANDLE_INIT(vid);
4431 TPACPI_ACPIHANDLE_INIT(vid2);
4433 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4434 /* G41, assume IVGA doesn't change */
4435 vid_handle = vid2_handle;
4437 if (!vid_handle)
4438 /* video switching not supported on R30, R31 */
4439 video_supported = TPACPI_VIDEO_NONE;
4440 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4441 /* 570 */
4442 video_supported = TPACPI_VIDEO_570;
4443 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4444 /* 600e/x, 770e, 770x */
4445 video_supported = TPACPI_VIDEO_770;
4446 else
4447 /* all others */
4448 video_supported = TPACPI_VIDEO_NEW;
4450 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4451 str_supported(video_supported != TPACPI_VIDEO_NONE),
4452 video_supported);
4454 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4457 static void video_exit(void)
4459 dbg_printk(TPACPI_DBG_EXIT,
4460 "restoring original video autoswitch mode\n");
4461 if (video_autosw_set(video_orig_autosw))
4462 printk(TPACPI_ERR "error while trying to restore original "
4463 "video autoswitch mode\n");
4466 static int video_outputsw_get(void)
4468 int status = 0;
4469 int i;
4471 switch (video_supported) {
4472 case TPACPI_VIDEO_570:
4473 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4474 TP_ACPI_VIDEO_570_PHSCMD))
4475 return -EIO;
4476 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4477 break;
4478 case TPACPI_VIDEO_770:
4479 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_LCD;
4483 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_CRT;
4487 break;
4488 case TPACPI_VIDEO_NEW:
4489 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4490 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4491 return -EIO;
4492 if (i)
4493 status |= TP_ACPI_VIDEO_S_CRT;
4495 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4496 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4497 return -EIO;
4498 if (i)
4499 status |= TP_ACPI_VIDEO_S_LCD;
4500 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4501 return -EIO;
4502 if (i)
4503 status |= TP_ACPI_VIDEO_S_DVI;
4504 break;
4505 default:
4506 return -ENOSYS;
4509 return status;
4512 static int video_outputsw_set(int status)
4514 int autosw;
4515 int res = 0;
4517 switch (video_supported) {
4518 case TPACPI_VIDEO_570:
4519 res = acpi_evalf(NULL, NULL,
4520 "\\_SB.PHS2", "vdd",
4521 TP_ACPI_VIDEO_570_PHS2CMD,
4522 status | TP_ACPI_VIDEO_570_PHS2SET);
4523 break;
4524 case TPACPI_VIDEO_770:
4525 autosw = video_autosw_get();
4526 if (autosw < 0)
4527 return autosw;
4529 res = video_autosw_set(1);
4530 if (res)
4531 return res;
4532 res = acpi_evalf(vid_handle, NULL,
4533 "ASWT", "vdd", status * 0x100, 0);
4534 if (!autosw && video_autosw_set(autosw)) {
4535 printk(TPACPI_ERR
4536 "video auto-switch left enabled due to error\n");
4537 return -EIO;
4539 break;
4540 case TPACPI_VIDEO_NEW:
4541 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4542 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4543 break;
4544 default:
4545 return -ENOSYS;
4548 return (res)? 0 : -EIO;
4551 static int video_autosw_get(void)
4553 int autosw = 0;
4555 switch (video_supported) {
4556 case TPACPI_VIDEO_570:
4557 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4558 return -EIO;
4559 break;
4560 case TPACPI_VIDEO_770:
4561 case TPACPI_VIDEO_NEW:
4562 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4563 return -EIO;
4564 break;
4565 default:
4566 return -ENOSYS;
4569 return autosw & 1;
4572 static int video_autosw_set(int enable)
4574 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4575 return -EIO;
4576 return 0;
4579 static int video_outputsw_cycle(void)
4581 int autosw = video_autosw_get();
4582 int res;
4584 if (autosw < 0)
4585 return autosw;
4587 switch (video_supported) {
4588 case TPACPI_VIDEO_570:
4589 res = video_autosw_set(1);
4590 if (res)
4591 return res;
4592 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4593 break;
4594 case TPACPI_VIDEO_770:
4595 case TPACPI_VIDEO_NEW:
4596 res = video_autosw_set(1);
4597 if (res)
4598 return res;
4599 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4600 break;
4601 default:
4602 return -ENOSYS;
4604 if (!autosw && video_autosw_set(autosw)) {
4605 printk(TPACPI_ERR
4606 "video auto-switch left enabled due to error\n");
4607 return -EIO;
4610 return (res)? 0 : -EIO;
4613 static int video_expand_toggle(void)
4615 switch (video_supported) {
4616 case TPACPI_VIDEO_570:
4617 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4618 0 : -EIO;
4619 case TPACPI_VIDEO_770:
4620 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4621 0 : -EIO;
4622 case TPACPI_VIDEO_NEW:
4623 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4624 0 : -EIO;
4625 default:
4626 return -ENOSYS;
4628 /* not reached */
4631 static int video_read(struct seq_file *m)
4633 int status, autosw;
4635 if (video_supported == TPACPI_VIDEO_NONE) {
4636 seq_printf(m, "status:\t\tnot supported\n");
4637 return 0;
4640 /* Even reads can crash X.org, so... */
4641 if (!capable(CAP_SYS_ADMIN))
4642 return -EPERM;
4644 status = video_outputsw_get();
4645 if (status < 0)
4646 return status;
4648 autosw = video_autosw_get();
4649 if (autosw < 0)
4650 return autosw;
4652 seq_printf(m, "status:\t\tsupported\n");
4653 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4654 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4655 if (video_supported == TPACPI_VIDEO_NEW)
4656 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4657 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4658 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4659 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4660 if (video_supported == TPACPI_VIDEO_NEW)
4661 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4662 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4663 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4665 return 0;
4668 static int video_write(char *buf)
4670 char *cmd;
4671 int enable, disable, status;
4672 int res;
4674 if (video_supported == TPACPI_VIDEO_NONE)
4675 return -ENODEV;
4677 /* Even reads can crash X.org, let alone writes... */
4678 if (!capable(CAP_SYS_ADMIN))
4679 return -EPERM;
4681 enable = 0;
4682 disable = 0;
4684 while ((cmd = next_cmd(&buf))) {
4685 if (strlencmp(cmd, "lcd_enable") == 0) {
4686 enable |= TP_ACPI_VIDEO_S_LCD;
4687 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4688 disable |= TP_ACPI_VIDEO_S_LCD;
4689 } else if (strlencmp(cmd, "crt_enable") == 0) {
4690 enable |= TP_ACPI_VIDEO_S_CRT;
4691 } else if (strlencmp(cmd, "crt_disable") == 0) {
4692 disable |= TP_ACPI_VIDEO_S_CRT;
4693 } else if (video_supported == TPACPI_VIDEO_NEW &&
4694 strlencmp(cmd, "dvi_enable") == 0) {
4695 enable |= TP_ACPI_VIDEO_S_DVI;
4696 } else if (video_supported == TPACPI_VIDEO_NEW &&
4697 strlencmp(cmd, "dvi_disable") == 0) {
4698 disable |= TP_ACPI_VIDEO_S_DVI;
4699 } else if (strlencmp(cmd, "auto_enable") == 0) {
4700 res = video_autosw_set(1);
4701 if (res)
4702 return res;
4703 } else if (strlencmp(cmd, "auto_disable") == 0) {
4704 res = video_autosw_set(0);
4705 if (res)
4706 return res;
4707 } else if (strlencmp(cmd, "video_switch") == 0) {
4708 res = video_outputsw_cycle();
4709 if (res)
4710 return res;
4711 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4712 res = video_expand_toggle();
4713 if (res)
4714 return res;
4715 } else
4716 return -EINVAL;
4719 if (enable || disable) {
4720 status = video_outputsw_get();
4721 if (status < 0)
4722 return status;
4723 res = video_outputsw_set((status & ~disable) | enable);
4724 if (res)
4725 return res;
4728 return 0;
4731 static struct ibm_struct video_driver_data = {
4732 .name = "video",
4733 .read = video_read,
4734 .write = video_write,
4735 .exit = video_exit,
4738 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4740 /*************************************************************************
4741 * Light (thinklight) subdriver
4744 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4745 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4747 static int light_get_status(void)
4749 int status = 0;
4751 if (tp_features.light_status) {
4752 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4753 return -EIO;
4754 return (!!status);
4757 return -ENXIO;
4760 static int light_set_status(int status)
4762 int rc;
4764 if (tp_features.light) {
4765 if (cmos_handle) {
4766 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4767 (status)?
4768 TP_CMOS_THINKLIGHT_ON :
4769 TP_CMOS_THINKLIGHT_OFF);
4770 } else {
4771 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4772 (status)? 1 : 0);
4774 return (rc)? 0 : -EIO;
4777 return -ENXIO;
4780 static void light_set_status_worker(struct work_struct *work)
4782 struct tpacpi_led_classdev *data =
4783 container_of(work, struct tpacpi_led_classdev, work);
4785 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4786 light_set_status((data->new_state != TPACPI_LED_OFF));
4789 static void light_sysfs_set(struct led_classdev *led_cdev,
4790 enum led_brightness brightness)
4792 struct tpacpi_led_classdev *data =
4793 container_of(led_cdev,
4794 struct tpacpi_led_classdev,
4795 led_classdev);
4796 data->new_state = (brightness != LED_OFF) ?
4797 TPACPI_LED_ON : TPACPI_LED_OFF;
4798 queue_work(tpacpi_wq, &data->work);
4801 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4803 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4806 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4807 .led_classdev = {
4808 .name = "tpacpi::thinklight",
4809 .brightness_set = &light_sysfs_set,
4810 .brightness_get = &light_sysfs_get,
4814 static int __init light_init(struct ibm_init_struct *iibm)
4816 int rc;
4818 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4820 TPACPI_ACPIHANDLE_INIT(ledb);
4821 TPACPI_ACPIHANDLE_INIT(lght);
4822 TPACPI_ACPIHANDLE_INIT(cmos);
4823 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4825 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4826 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4828 if (tp_features.light)
4829 /* light status not supported on
4830 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4831 tp_features.light_status =
4832 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4834 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4835 str_supported(tp_features.light),
4836 str_supported(tp_features.light_status));
4838 if (!tp_features.light)
4839 return 1;
4841 rc = led_classdev_register(&tpacpi_pdev->dev,
4842 &tpacpi_led_thinklight.led_classdev);
4844 if (rc < 0) {
4845 tp_features.light = 0;
4846 tp_features.light_status = 0;
4847 } else {
4848 rc = 0;
4851 return rc;
4854 static void light_exit(void)
4856 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4857 if (work_pending(&tpacpi_led_thinklight.work))
4858 flush_workqueue(tpacpi_wq);
4861 static int light_read(struct seq_file *m)
4863 int status;
4865 if (!tp_features.light) {
4866 seq_printf(m, "status:\t\tnot supported\n");
4867 } else if (!tp_features.light_status) {
4868 seq_printf(m, "status:\t\tunknown\n");
4869 seq_printf(m, "commands:\ton, off\n");
4870 } else {
4871 status = light_get_status();
4872 if (status < 0)
4873 return status;
4874 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4875 seq_printf(m, "commands:\ton, off\n");
4878 return 0;
4881 static int light_write(char *buf)
4883 char *cmd;
4884 int newstatus = 0;
4886 if (!tp_features.light)
4887 return -ENODEV;
4889 while ((cmd = next_cmd(&buf))) {
4890 if (strlencmp(cmd, "on") == 0) {
4891 newstatus = 1;
4892 } else if (strlencmp(cmd, "off") == 0) {
4893 newstatus = 0;
4894 } else
4895 return -EINVAL;
4898 return light_set_status(newstatus);
4901 static struct ibm_struct light_driver_data = {
4902 .name = "light",
4903 .read = light_read,
4904 .write = light_write,
4905 .exit = light_exit,
4908 /*************************************************************************
4909 * CMOS subdriver
4912 /* sysfs cmos_command -------------------------------------------------- */
4913 static ssize_t cmos_command_store(struct device *dev,
4914 struct device_attribute *attr,
4915 const char *buf, size_t count)
4917 unsigned long cmos_cmd;
4918 int res;
4920 if (parse_strtoul(buf, 21, &cmos_cmd))
4921 return -EINVAL;
4923 res = issue_thinkpad_cmos_command(cmos_cmd);
4924 return (res)? res : count;
4927 static struct device_attribute dev_attr_cmos_command =
4928 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4930 /* --------------------------------------------------------------------- */
4932 static int __init cmos_init(struct ibm_init_struct *iibm)
4934 int res;
4936 vdbg_printk(TPACPI_DBG_INIT,
4937 "initializing cmos commands subdriver\n");
4939 TPACPI_ACPIHANDLE_INIT(cmos);
4941 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4942 str_supported(cmos_handle != NULL));
4944 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4945 if (res)
4946 return res;
4948 return (cmos_handle)? 0 : 1;
4951 static void cmos_exit(void)
4953 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4956 static int cmos_read(struct seq_file *m)
4958 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4959 R30, R31, T20-22, X20-21 */
4960 if (!cmos_handle)
4961 seq_printf(m, "status:\t\tnot supported\n");
4962 else {
4963 seq_printf(m, "status:\t\tsupported\n");
4964 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4967 return 0;
4970 static int cmos_write(char *buf)
4972 char *cmd;
4973 int cmos_cmd, res;
4975 while ((cmd = next_cmd(&buf))) {
4976 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4977 cmos_cmd >= 0 && cmos_cmd <= 21) {
4978 /* cmos_cmd set */
4979 } else
4980 return -EINVAL;
4982 res = issue_thinkpad_cmos_command(cmos_cmd);
4983 if (res)
4984 return res;
4987 return 0;
4990 static struct ibm_struct cmos_driver_data = {
4991 .name = "cmos",
4992 .read = cmos_read,
4993 .write = cmos_write,
4994 .exit = cmos_exit,
4997 /*************************************************************************
4998 * LED subdriver
5001 enum led_access_mode {
5002 TPACPI_LED_NONE = 0,
5003 TPACPI_LED_570, /* 570 */
5004 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5005 TPACPI_LED_NEW, /* all others */
5008 enum { /* For TPACPI_LED_OLD */
5009 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5010 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5011 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5014 static enum led_access_mode led_supported;
5016 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5017 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5018 /* T20-22, X20-21 */
5019 "LED", /* all others */
5020 ); /* R30, R31 */
5022 #define TPACPI_LED_NUMLEDS 16
5023 static struct tpacpi_led_classdev *tpacpi_leds;
5024 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5025 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5026 /* there's a limit of 19 chars + NULL before 2.6.26 */
5027 "tpacpi::power",
5028 "tpacpi:orange:batt",
5029 "tpacpi:green:batt",
5030 "tpacpi::dock_active",
5031 "tpacpi::bay_active",
5032 "tpacpi::dock_batt",
5033 "tpacpi::unknown_led",
5034 "tpacpi::standby",
5035 "tpacpi::dock_status1",
5036 "tpacpi::dock_status2",
5037 "tpacpi::unknown_led2",
5038 "tpacpi::unknown_led3",
5039 "tpacpi::thinkvantage",
5041 #define TPACPI_SAFE_LEDS 0x1081U
5043 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5045 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5046 return false;
5047 #else
5048 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5049 #endif
5052 static int led_get_status(const unsigned int led)
5054 int status;
5055 enum led_status_t led_s;
5057 switch (led_supported) {
5058 case TPACPI_LED_570:
5059 if (!acpi_evalf(ec_handle,
5060 &status, "GLED", "dd", 1 << led))
5061 return -EIO;
5062 led_s = (status == 0)?
5063 TPACPI_LED_OFF :
5064 ((status == 1)?
5065 TPACPI_LED_ON :
5066 TPACPI_LED_BLINK);
5067 tpacpi_led_state_cache[led] = led_s;
5068 return led_s;
5069 default:
5070 return -ENXIO;
5073 /* not reached */
5076 static int led_set_status(const unsigned int led,
5077 const enum led_status_t ledstatus)
5079 /* off, on, blink. Index is led_status_t */
5080 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5081 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5083 int rc = 0;
5085 switch (led_supported) {
5086 case TPACPI_LED_570:
5087 /* 570 */
5088 if (unlikely(led > 7))
5089 return -EINVAL;
5090 if (unlikely(tpacpi_is_led_restricted(led)))
5091 return -EPERM;
5092 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5093 (1 << led), led_sled_arg1[ledstatus]))
5094 rc = -EIO;
5095 break;
5096 case TPACPI_LED_OLD:
5097 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5098 if (unlikely(led > 7))
5099 return -EINVAL;
5100 if (unlikely(tpacpi_is_led_restricted(led)))
5101 return -EPERM;
5102 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5103 if (rc >= 0)
5104 rc = ec_write(TPACPI_LED_EC_HLBL,
5105 (ledstatus == TPACPI_LED_BLINK) << led);
5106 if (rc >= 0)
5107 rc = ec_write(TPACPI_LED_EC_HLCL,
5108 (ledstatus != TPACPI_LED_OFF) << led);
5109 break;
5110 case TPACPI_LED_NEW:
5111 /* all others */
5112 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5113 return -EINVAL;
5114 if (unlikely(tpacpi_is_led_restricted(led)))
5115 return -EPERM;
5116 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5117 led, led_led_arg1[ledstatus]))
5118 rc = -EIO;
5119 break;
5120 default:
5121 rc = -ENXIO;
5124 if (!rc)
5125 tpacpi_led_state_cache[led] = ledstatus;
5127 return rc;
5130 static void led_set_status_worker(struct work_struct *work)
5132 struct tpacpi_led_classdev *data =
5133 container_of(work, struct tpacpi_led_classdev, work);
5135 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5136 led_set_status(data->led, data->new_state);
5139 static void led_sysfs_set(struct led_classdev *led_cdev,
5140 enum led_brightness brightness)
5142 struct tpacpi_led_classdev *data = container_of(led_cdev,
5143 struct tpacpi_led_classdev, led_classdev);
5145 if (brightness == LED_OFF)
5146 data->new_state = TPACPI_LED_OFF;
5147 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5148 data->new_state = TPACPI_LED_ON;
5149 else
5150 data->new_state = TPACPI_LED_BLINK;
5152 queue_work(tpacpi_wq, &data->work);
5155 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5156 unsigned long *delay_on, unsigned long *delay_off)
5158 struct tpacpi_led_classdev *data = container_of(led_cdev,
5159 struct tpacpi_led_classdev, led_classdev);
5161 /* Can we choose the flash rate? */
5162 if (*delay_on == 0 && *delay_off == 0) {
5163 /* yes. set them to the hardware blink rate (1 Hz) */
5164 *delay_on = 500; /* ms */
5165 *delay_off = 500; /* ms */
5166 } else if ((*delay_on != 500) || (*delay_off != 500))
5167 return -EINVAL;
5169 data->new_state = TPACPI_LED_BLINK;
5170 queue_work(tpacpi_wq, &data->work);
5172 return 0;
5175 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5177 int rc;
5179 struct tpacpi_led_classdev *data = container_of(led_cdev,
5180 struct tpacpi_led_classdev, led_classdev);
5182 rc = led_get_status(data->led);
5184 if (rc == TPACPI_LED_OFF || rc < 0)
5185 rc = LED_OFF; /* no error handling in led class :( */
5186 else
5187 rc = LED_FULL;
5189 return rc;
5192 static void led_exit(void)
5194 unsigned int i;
5196 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5197 if (tpacpi_leds[i].led_classdev.name)
5198 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5201 kfree(tpacpi_leds);
5204 static int __init tpacpi_init_led(unsigned int led)
5206 int rc;
5208 tpacpi_leds[led].led = led;
5210 /* LEDs with no name don't get registered */
5211 if (!tpacpi_led_names[led])
5212 return 0;
5214 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5215 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5216 if (led_supported == TPACPI_LED_570)
5217 tpacpi_leds[led].led_classdev.brightness_get =
5218 &led_sysfs_get;
5220 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5222 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5224 rc = led_classdev_register(&tpacpi_pdev->dev,
5225 &tpacpi_leds[led].led_classdev);
5226 if (rc < 0)
5227 tpacpi_leds[led].led_classdev.name = NULL;
5229 return rc;
5232 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5233 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5234 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5235 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5237 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5238 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5239 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5240 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5241 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5242 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5243 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5244 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5246 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5247 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5248 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5249 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5250 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5252 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5253 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5254 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5255 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5257 /* (1) - may have excess leds enabled on MSB */
5259 /* Defaults (order matters, keep last, don't reorder!) */
5260 { /* Lenovo */
5261 .vendor = PCI_VENDOR_ID_LENOVO,
5262 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5263 .quirks = 0x1fffU,
5265 { /* IBM ThinkPads with no EC version string */
5266 .vendor = PCI_VENDOR_ID_IBM,
5267 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5268 .quirks = 0x00ffU,
5270 { /* IBM ThinkPads with EC version string */
5271 .vendor = PCI_VENDOR_ID_IBM,
5272 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5273 .quirks = 0x00bfU,
5277 #undef TPACPI_LEDQ_IBM
5278 #undef TPACPI_LEDQ_LNV
5280 static int __init led_init(struct ibm_init_struct *iibm)
5282 unsigned int i;
5283 int rc;
5284 unsigned long useful_leds;
5286 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5288 TPACPI_ACPIHANDLE_INIT(led);
5290 if (!led_handle)
5291 /* led not supported on R30, R31 */
5292 led_supported = TPACPI_LED_NONE;
5293 else if (strlencmp(led_path, "SLED") == 0)
5294 /* 570 */
5295 led_supported = TPACPI_LED_570;
5296 else if (strlencmp(led_path, "SYSL") == 0)
5297 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5298 led_supported = TPACPI_LED_OLD;
5299 else
5300 /* all others */
5301 led_supported = TPACPI_LED_NEW;
5303 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5304 str_supported(led_supported), led_supported);
5306 if (led_supported == TPACPI_LED_NONE)
5307 return 1;
5309 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5310 GFP_KERNEL);
5311 if (!tpacpi_leds) {
5312 printk(TPACPI_ERR "Out of memory for LED data\n");
5313 return -ENOMEM;
5316 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5317 ARRAY_SIZE(led_useful_qtable));
5319 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5320 if (!tpacpi_is_led_restricted(i) &&
5321 test_bit(i, &useful_leds)) {
5322 rc = tpacpi_init_led(i);
5323 if (rc < 0) {
5324 led_exit();
5325 return rc;
5330 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5331 printk(TPACPI_NOTICE
5332 "warning: userspace override of important "
5333 "firmware LEDs is enabled\n");
5334 #endif
5335 return 0;
5338 #define str_led_status(s) \
5339 ((s) == TPACPI_LED_OFF ? "off" : \
5340 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5342 static int led_read(struct seq_file *m)
5344 if (!led_supported) {
5345 seq_printf(m, "status:\t\tnot supported\n");
5346 return 0;
5348 seq_printf(m, "status:\t\tsupported\n");
5350 if (led_supported == TPACPI_LED_570) {
5351 /* 570 */
5352 int i, status;
5353 for (i = 0; i < 8; i++) {
5354 status = led_get_status(i);
5355 if (status < 0)
5356 return -EIO;
5357 seq_printf(m, "%d:\t\t%s\n",
5358 i, str_led_status(status));
5362 seq_printf(m, "commands:\t"
5363 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5365 return 0;
5368 static int led_write(char *buf)
5370 char *cmd;
5371 int led, rc;
5372 enum led_status_t s;
5374 if (!led_supported)
5375 return -ENODEV;
5377 while ((cmd = next_cmd(&buf))) {
5378 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5379 return -EINVAL;
5381 if (strstr(cmd, "off")) {
5382 s = TPACPI_LED_OFF;
5383 } else if (strstr(cmd, "on")) {
5384 s = TPACPI_LED_ON;
5385 } else if (strstr(cmd, "blink")) {
5386 s = TPACPI_LED_BLINK;
5387 } else {
5388 return -EINVAL;
5391 rc = led_set_status(led, s);
5392 if (rc < 0)
5393 return rc;
5396 return 0;
5399 static struct ibm_struct led_driver_data = {
5400 .name = "led",
5401 .read = led_read,
5402 .write = led_write,
5403 .exit = led_exit,
5406 /*************************************************************************
5407 * Beep subdriver
5410 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5412 #define TPACPI_BEEP_Q1 0x0001
5414 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5415 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5416 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5419 static int __init beep_init(struct ibm_init_struct *iibm)
5421 unsigned long quirks;
5423 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5425 TPACPI_ACPIHANDLE_INIT(beep);
5427 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5428 str_supported(beep_handle != NULL));
5430 quirks = tpacpi_check_quirks(beep_quirk_table,
5431 ARRAY_SIZE(beep_quirk_table));
5433 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5435 return (beep_handle)? 0 : 1;
5438 static int beep_read(struct seq_file *m)
5440 if (!beep_handle)
5441 seq_printf(m, "status:\t\tnot supported\n");
5442 else {
5443 seq_printf(m, "status:\t\tsupported\n");
5444 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5447 return 0;
5450 static int beep_write(char *buf)
5452 char *cmd;
5453 int beep_cmd;
5455 if (!beep_handle)
5456 return -ENODEV;
5458 while ((cmd = next_cmd(&buf))) {
5459 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5460 beep_cmd >= 0 && beep_cmd <= 17) {
5461 /* beep_cmd set */
5462 } else
5463 return -EINVAL;
5464 if (tp_features.beep_needs_two_args) {
5465 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5466 beep_cmd, 0))
5467 return -EIO;
5468 } else {
5469 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5470 beep_cmd))
5471 return -EIO;
5475 return 0;
5478 static struct ibm_struct beep_driver_data = {
5479 .name = "beep",
5480 .read = beep_read,
5481 .write = beep_write,
5484 /*************************************************************************
5485 * Thermal subdriver
5488 enum thermal_access_mode {
5489 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5490 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5491 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5492 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5493 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5496 enum { /* TPACPI_THERMAL_TPEC_* */
5497 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5498 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5499 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5501 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5505 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5506 struct ibm_thermal_sensors_struct {
5507 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5510 static enum thermal_access_mode thermal_read_mode;
5512 /* idx is zero-based */
5513 static int thermal_get_sensor(int idx, s32 *value)
5515 int t;
5516 s8 tmp;
5517 char tmpi[5];
5519 t = TP_EC_THERMAL_TMP0;
5521 switch (thermal_read_mode) {
5522 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5523 case TPACPI_THERMAL_TPEC_16:
5524 if (idx >= 8 && idx <= 15) {
5525 t = TP_EC_THERMAL_TMP8;
5526 idx -= 8;
5528 /* fallthrough */
5529 #endif
5530 case TPACPI_THERMAL_TPEC_8:
5531 if (idx <= 7) {
5532 if (!acpi_ec_read(t + idx, &tmp))
5533 return -EIO;
5534 *value = tmp * 1000;
5535 return 0;
5537 break;
5539 case TPACPI_THERMAL_ACPI_UPDT:
5540 if (idx <= 7) {
5541 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5542 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5543 return -EIO;
5544 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5545 return -EIO;
5546 *value = (t - 2732) * 100;
5547 return 0;
5549 break;
5551 case TPACPI_THERMAL_ACPI_TMP07:
5552 if (idx <= 7) {
5553 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5554 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5555 return -EIO;
5556 if (t > 127 || t < -127)
5557 t = TP_EC_THERMAL_TMP_NA;
5558 *value = t * 1000;
5559 return 0;
5561 break;
5563 case TPACPI_THERMAL_NONE:
5564 default:
5565 return -ENOSYS;
5568 return -EINVAL;
5571 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5573 int res, i;
5574 int n;
5576 n = 8;
5577 i = 0;
5579 if (!s)
5580 return -EINVAL;
5582 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5583 n = 16;
5585 for (i = 0 ; i < n; i++) {
5586 res = thermal_get_sensor(i, &s->temp[i]);
5587 if (res)
5588 return res;
5591 return n;
5594 static void thermal_dump_all_sensors(void)
5596 int n, i;
5597 struct ibm_thermal_sensors_struct t;
5599 n = thermal_get_sensors(&t);
5600 if (n <= 0)
5601 return;
5603 printk(TPACPI_NOTICE
5604 "temperatures (Celsius):");
5606 for (i = 0; i < n; i++) {
5607 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5608 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5609 else
5610 printk(KERN_CONT " N/A");
5613 printk(KERN_CONT "\n");
5616 /* sysfs temp##_input -------------------------------------------------- */
5618 static ssize_t thermal_temp_input_show(struct device *dev,
5619 struct device_attribute *attr,
5620 char *buf)
5622 struct sensor_device_attribute *sensor_attr =
5623 to_sensor_dev_attr(attr);
5624 int idx = sensor_attr->index;
5625 s32 value;
5626 int res;
5628 res = thermal_get_sensor(idx, &value);
5629 if (res)
5630 return res;
5631 if (value == TPACPI_THERMAL_SENSOR_NA)
5632 return -ENXIO;
5634 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5637 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5638 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5639 thermal_temp_input_show, NULL, _idxB)
5641 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5642 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5643 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5644 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5645 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5646 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5647 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5648 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5649 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5650 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5651 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5652 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5653 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5654 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5655 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5656 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5657 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5660 #define THERMAL_ATTRS(X) \
5661 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5663 static struct attribute *thermal_temp_input_attr[] = {
5664 THERMAL_ATTRS(8),
5665 THERMAL_ATTRS(9),
5666 THERMAL_ATTRS(10),
5667 THERMAL_ATTRS(11),
5668 THERMAL_ATTRS(12),
5669 THERMAL_ATTRS(13),
5670 THERMAL_ATTRS(14),
5671 THERMAL_ATTRS(15),
5672 THERMAL_ATTRS(0),
5673 THERMAL_ATTRS(1),
5674 THERMAL_ATTRS(2),
5675 THERMAL_ATTRS(3),
5676 THERMAL_ATTRS(4),
5677 THERMAL_ATTRS(5),
5678 THERMAL_ATTRS(6),
5679 THERMAL_ATTRS(7),
5680 NULL
5683 static const struct attribute_group thermal_temp_input16_group = {
5684 .attrs = thermal_temp_input_attr
5687 static const struct attribute_group thermal_temp_input8_group = {
5688 .attrs = &thermal_temp_input_attr[8]
5691 #undef THERMAL_SENSOR_ATTR_TEMP
5692 #undef THERMAL_ATTRS
5694 /* --------------------------------------------------------------------- */
5696 static int __init thermal_init(struct ibm_init_struct *iibm)
5698 u8 t, ta1, ta2;
5699 int i;
5700 int acpi_tmp7;
5701 int res;
5703 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5705 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5707 if (thinkpad_id.ec_model) {
5709 * Direct EC access mode: sensors at registers
5710 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5711 * non-implemented, thermal sensors return 0x80 when
5712 * not available
5715 ta1 = ta2 = 0;
5716 for (i = 0; i < 8; i++) {
5717 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5718 ta1 |= t;
5719 } else {
5720 ta1 = 0;
5721 break;
5723 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5724 ta2 |= t;
5725 } else {
5726 ta1 = 0;
5727 break;
5730 if (ta1 == 0) {
5731 /* This is sheer paranoia, but we handle it anyway */
5732 if (acpi_tmp7) {
5733 printk(TPACPI_ERR
5734 "ThinkPad ACPI EC access misbehaving, "
5735 "falling back to ACPI TMPx access "
5736 "mode\n");
5737 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5738 } else {
5739 printk(TPACPI_ERR
5740 "ThinkPad ACPI EC access misbehaving, "
5741 "disabling thermal sensors access\n");
5742 thermal_read_mode = TPACPI_THERMAL_NONE;
5744 } else {
5745 thermal_read_mode =
5746 (ta2 != 0) ?
5747 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5749 } else if (acpi_tmp7) {
5750 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5751 /* 600e/x, 770e, 770x */
5752 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5753 } else {
5754 /* Standard ACPI TMPx access, max 8 sensors */
5755 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5757 } else {
5758 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5759 thermal_read_mode = TPACPI_THERMAL_NONE;
5762 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5763 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5764 thermal_read_mode);
5766 switch (thermal_read_mode) {
5767 case TPACPI_THERMAL_TPEC_16:
5768 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5769 &thermal_temp_input16_group);
5770 if (res)
5771 return res;
5772 break;
5773 case TPACPI_THERMAL_TPEC_8:
5774 case TPACPI_THERMAL_ACPI_TMP07:
5775 case TPACPI_THERMAL_ACPI_UPDT:
5776 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5777 &thermal_temp_input8_group);
5778 if (res)
5779 return res;
5780 break;
5781 case TPACPI_THERMAL_NONE:
5782 default:
5783 return 1;
5786 return 0;
5789 static void thermal_exit(void)
5791 switch (thermal_read_mode) {
5792 case TPACPI_THERMAL_TPEC_16:
5793 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5794 &thermal_temp_input16_group);
5795 break;
5796 case TPACPI_THERMAL_TPEC_8:
5797 case TPACPI_THERMAL_ACPI_TMP07:
5798 case TPACPI_THERMAL_ACPI_UPDT:
5799 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5800 &thermal_temp_input8_group);
5801 break;
5802 case TPACPI_THERMAL_NONE:
5803 default:
5804 break;
5808 static int thermal_read(struct seq_file *m)
5810 int n, i;
5811 struct ibm_thermal_sensors_struct t;
5813 n = thermal_get_sensors(&t);
5814 if (unlikely(n < 0))
5815 return n;
5817 seq_printf(m, "temperatures:\t");
5819 if (n > 0) {
5820 for (i = 0; i < (n - 1); i++)
5821 seq_printf(m, "%d ", t.temp[i] / 1000);
5822 seq_printf(m, "%d\n", t.temp[i] / 1000);
5823 } else
5824 seq_printf(m, "not supported\n");
5826 return 0;
5829 static struct ibm_struct thermal_driver_data = {
5830 .name = "thermal",
5831 .read = thermal_read,
5832 .exit = thermal_exit,
5835 /*************************************************************************
5836 * EC Dump subdriver
5839 static u8 ecdump_regs[256];
5841 static int ecdump_read(struct seq_file *m)
5843 int i, j;
5844 u8 v;
5846 seq_printf(m, "EC "
5847 " +00 +01 +02 +03 +04 +05 +06 +07"
5848 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5849 for (i = 0; i < 256; i += 16) {
5850 seq_printf(m, "EC 0x%02x:", i);
5851 for (j = 0; j < 16; j++) {
5852 if (!acpi_ec_read(i + j, &v))
5853 break;
5854 if (v != ecdump_regs[i + j])
5855 seq_printf(m, " *%02x", v);
5856 else
5857 seq_printf(m, " %02x", v);
5858 ecdump_regs[i + j] = v;
5860 seq_putc(m, '\n');
5861 if (j != 16)
5862 break;
5865 /* These are way too dangerous to advertise openly... */
5866 #if 0
5867 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5868 " (<offset> is 00-ff, <value> is 00-ff)\n");
5869 seq_printf(m, "commands:\t0x<offset> <value> "
5870 " (<offset> is 00-ff, <value> is 0-255)\n");
5871 #endif
5872 return 0;
5875 static int ecdump_write(char *buf)
5877 char *cmd;
5878 int i, v;
5880 while ((cmd = next_cmd(&buf))) {
5881 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5882 /* i and v set */
5883 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5884 /* i and v set */
5885 } else
5886 return -EINVAL;
5887 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5888 if (!acpi_ec_write(i, v))
5889 return -EIO;
5890 } else
5891 return -EINVAL;
5894 return 0;
5897 static struct ibm_struct ecdump_driver_data = {
5898 .name = "ecdump",
5899 .read = ecdump_read,
5900 .write = ecdump_write,
5901 .flags.experimental = 1,
5904 /*************************************************************************
5905 * Backlight/brightness subdriver
5908 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5911 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5912 * CMOS NVRAM byte 0x5E, bits 0-3.
5914 * EC HBRV (0x31) has the following layout
5915 * Bit 7: unknown function
5916 * Bit 6: unknown function
5917 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5918 * Bit 4: must be set to zero to avoid problems
5919 * Bit 3-0: backlight brightness level
5921 * brightness_get_raw returns status data in the HBRV layout
5923 * WARNING: The X61 has been verified to use HBRV for something else, so
5924 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5925 * testing on the very early *60 Lenovo models...
5928 enum {
5929 TP_EC_BACKLIGHT = 0x31,
5931 /* TP_EC_BACKLIGHT bitmasks */
5932 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5933 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5934 TP_EC_BACKLIGHT_MAPSW = 0x20,
5937 enum tpacpi_brightness_access_mode {
5938 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5939 TPACPI_BRGHT_MODE_EC, /* EC control */
5940 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5941 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5942 TPACPI_BRGHT_MODE_MAX
5945 static struct backlight_device *ibm_backlight_device;
5947 static enum tpacpi_brightness_access_mode brightness_mode =
5948 TPACPI_BRGHT_MODE_MAX;
5950 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5952 static struct mutex brightness_mutex;
5954 /* NVRAM brightness access,
5955 * call with brightness_mutex held! */
5956 static unsigned int tpacpi_brightness_nvram_get(void)
5958 u8 lnvram;
5960 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5961 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5962 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5963 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5965 return lnvram;
5968 static void tpacpi_brightness_checkpoint_nvram(void)
5970 u8 lec = 0;
5971 u8 b_nvram;
5973 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5974 return;
5976 vdbg_printk(TPACPI_DBG_BRGHT,
5977 "trying to checkpoint backlight level to NVRAM...\n");
5979 if (mutex_lock_killable(&brightness_mutex) < 0)
5980 return;
5982 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5983 goto unlock;
5984 lec &= TP_EC_BACKLIGHT_LVLMSK;
5985 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5987 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5988 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5989 /* NVRAM needs update */
5990 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5991 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5992 b_nvram |= lec;
5993 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5994 dbg_printk(TPACPI_DBG_BRGHT,
5995 "updated NVRAM backlight level to %u (0x%02x)\n",
5996 (unsigned int) lec, (unsigned int) b_nvram);
5997 } else
5998 vdbg_printk(TPACPI_DBG_BRGHT,
5999 "NVRAM backlight level already is %u (0x%02x)\n",
6000 (unsigned int) lec, (unsigned int) b_nvram);
6002 unlock:
6003 mutex_unlock(&brightness_mutex);
6007 /* call with brightness_mutex held! */
6008 static int tpacpi_brightness_get_raw(int *status)
6010 u8 lec = 0;
6012 switch (brightness_mode) {
6013 case TPACPI_BRGHT_MODE_UCMS_STEP:
6014 *status = tpacpi_brightness_nvram_get();
6015 return 0;
6016 case TPACPI_BRGHT_MODE_EC:
6017 case TPACPI_BRGHT_MODE_ECNVRAM:
6018 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6019 return -EIO;
6020 *status = lec;
6021 return 0;
6022 default:
6023 return -ENXIO;
6027 /* call with brightness_mutex held! */
6028 /* do NOT call with illegal backlight level value */
6029 static int tpacpi_brightness_set_ec(unsigned int value)
6031 u8 lec = 0;
6033 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6034 return -EIO;
6036 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6037 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6038 (value & TP_EC_BACKLIGHT_LVLMSK))))
6039 return -EIO;
6041 return 0;
6044 /* call with brightness_mutex held! */
6045 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6047 int cmos_cmd, inc;
6048 unsigned int current_value, i;
6050 current_value = tpacpi_brightness_nvram_get();
6052 if (value == current_value)
6053 return 0;
6055 cmos_cmd = (value > current_value) ?
6056 TP_CMOS_BRIGHTNESS_UP :
6057 TP_CMOS_BRIGHTNESS_DOWN;
6058 inc = (value > current_value) ? 1 : -1;
6060 for (i = current_value; i != value; i += inc)
6061 if (issue_thinkpad_cmos_command(cmos_cmd))
6062 return -EIO;
6064 return 0;
6067 /* May return EINTR which can always be mapped to ERESTARTSYS */
6068 static int brightness_set(unsigned int value)
6070 int res;
6072 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6073 value < 0)
6074 return -EINVAL;
6076 vdbg_printk(TPACPI_DBG_BRGHT,
6077 "set backlight level to %d\n", value);
6079 res = mutex_lock_killable(&brightness_mutex);
6080 if (res < 0)
6081 return res;
6083 switch (brightness_mode) {
6084 case TPACPI_BRGHT_MODE_EC:
6085 case TPACPI_BRGHT_MODE_ECNVRAM:
6086 res = tpacpi_brightness_set_ec(value);
6087 break;
6088 case TPACPI_BRGHT_MODE_UCMS_STEP:
6089 res = tpacpi_brightness_set_ucmsstep(value);
6090 break;
6091 default:
6092 res = -ENXIO;
6095 mutex_unlock(&brightness_mutex);
6096 return res;
6099 /* sysfs backlight class ----------------------------------------------- */
6101 static int brightness_update_status(struct backlight_device *bd)
6103 unsigned int level =
6104 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6105 bd->props.power == FB_BLANK_UNBLANK) ?
6106 bd->props.brightness : 0;
6108 dbg_printk(TPACPI_DBG_BRGHT,
6109 "backlight: attempt to set level to %d\n",
6110 level);
6112 /* it is the backlight class's job (caller) to handle
6113 * EINTR and other errors properly */
6114 return brightness_set(level);
6117 static int brightness_get(struct backlight_device *bd)
6119 int status, res;
6121 res = mutex_lock_killable(&brightness_mutex);
6122 if (res < 0)
6123 return 0;
6125 res = tpacpi_brightness_get_raw(&status);
6127 mutex_unlock(&brightness_mutex);
6129 if (res < 0)
6130 return 0;
6132 return status & TP_EC_BACKLIGHT_LVLMSK;
6135 static void tpacpi_brightness_notify_change(void)
6137 backlight_force_update(ibm_backlight_device,
6138 BACKLIGHT_UPDATE_HOTKEY);
6141 static struct backlight_ops ibm_backlight_data = {
6142 .get_brightness = brightness_get,
6143 .update_status = brightness_update_status,
6146 /* --------------------------------------------------------------------- */
6149 * These are only useful for models that have only one possibility
6150 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6151 * these quirks.
6153 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6154 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6155 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6157 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6158 /* Models with ATI GPUs known to require ECNVRAM mode */
6159 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6161 /* Models with ATI GPUs that can use ECNVRAM */
6162 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6163 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6164 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6165 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6167 /* Models with Intel Extreme Graphics 2 */
6168 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6169 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6170 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6172 /* Models with Intel GMA900 */
6173 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6174 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6175 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6178 static int __init brightness_init(struct ibm_init_struct *iibm)
6180 int b;
6181 unsigned long quirks;
6183 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6185 mutex_init(&brightness_mutex);
6187 quirks = tpacpi_check_quirks(brightness_quirk_table,
6188 ARRAY_SIZE(brightness_quirk_table));
6191 * We always attempt to detect acpi support, so as to switch
6192 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6193 * going to publish a backlight interface
6195 b = tpacpi_check_std_acpi_brightness_support();
6196 if (b > 0) {
6198 if (acpi_video_backlight_support()) {
6199 if (brightness_enable > 1) {
6200 printk(TPACPI_NOTICE
6201 "Standard ACPI backlight interface "
6202 "available, not loading native one.\n");
6203 return 1;
6204 } else if (brightness_enable == 1) {
6205 printk(TPACPI_NOTICE
6206 "Backlight control force enabled, even if standard "
6207 "ACPI backlight interface is available\n");
6209 } else {
6210 if (brightness_enable > 1) {
6211 printk(TPACPI_NOTICE
6212 "Standard ACPI backlight interface not "
6213 "available, thinkpad_acpi native "
6214 "brightness control enabled\n");
6219 if (!brightness_enable) {
6220 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6221 "brightness support disabled by "
6222 "module parameter\n");
6223 return 1;
6226 if (b > 16) {
6227 printk(TPACPI_ERR
6228 "Unsupported brightness interface, "
6229 "please contact %s\n", TPACPI_MAIL);
6230 return 1;
6232 if (b == 16)
6233 tp_features.bright_16levels = 1;
6236 * Check for module parameter bogosity, note that we
6237 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6238 * able to detect "unspecified"
6240 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6241 return -EINVAL;
6243 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6244 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6245 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6246 if (quirks & TPACPI_BRGHT_Q_EC)
6247 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6248 else
6249 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6251 dbg_printk(TPACPI_DBG_BRGHT,
6252 "driver auto-selected brightness_mode=%d\n",
6253 brightness_mode);
6256 /* Safety */
6257 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6258 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6259 brightness_mode == TPACPI_BRGHT_MODE_EC))
6260 return -EINVAL;
6262 if (tpacpi_brightness_get_raw(&b) < 0)
6263 return 1;
6265 if (tp_features.bright_16levels)
6266 printk(TPACPI_INFO
6267 "detected a 16-level brightness capable ThinkPad\n");
6269 ibm_backlight_device = backlight_device_register(
6270 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6271 &ibm_backlight_data);
6272 if (IS_ERR(ibm_backlight_device)) {
6273 int rc = PTR_ERR(ibm_backlight_device);
6274 ibm_backlight_device = NULL;
6275 printk(TPACPI_ERR "Could not register backlight device\n");
6276 return rc;
6278 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6279 "brightness is supported\n");
6281 if (quirks & TPACPI_BRGHT_Q_ASK) {
6282 printk(TPACPI_NOTICE
6283 "brightness: will use unverified default: "
6284 "brightness_mode=%d\n", brightness_mode);
6285 printk(TPACPI_NOTICE
6286 "brightness: please report to %s whether it works well "
6287 "or not on your ThinkPad\n", TPACPI_MAIL);
6290 ibm_backlight_device->props.max_brightness =
6291 (tp_features.bright_16levels)? 15 : 7;
6292 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6293 backlight_update_status(ibm_backlight_device);
6295 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6296 "brightness: registering brightness hotkeys "
6297 "as change notification\n");
6298 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6299 | TP_ACPI_HKEY_BRGHTUP_MASK
6300 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6301 return 0;
6304 static void brightness_suspend(pm_message_t state)
6306 tpacpi_brightness_checkpoint_nvram();
6309 static void brightness_shutdown(void)
6311 tpacpi_brightness_checkpoint_nvram();
6314 static void brightness_exit(void)
6316 if (ibm_backlight_device) {
6317 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6318 "calling backlight_device_unregister()\n");
6319 backlight_device_unregister(ibm_backlight_device);
6322 tpacpi_brightness_checkpoint_nvram();
6325 static int brightness_read(struct seq_file *m)
6327 int level;
6329 level = brightness_get(NULL);
6330 if (level < 0) {
6331 seq_printf(m, "level:\t\tunreadable\n");
6332 } else {
6333 seq_printf(m, "level:\t\t%d\n", level);
6334 seq_printf(m, "commands:\tup, down\n");
6335 seq_printf(m, "commands:\tlevel <level>"
6336 " (<level> is 0-%d)\n",
6337 (tp_features.bright_16levels) ? 15 : 7);
6340 return 0;
6343 static int brightness_write(char *buf)
6345 int level;
6346 int rc;
6347 char *cmd;
6348 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6350 level = brightness_get(NULL);
6351 if (level < 0)
6352 return level;
6354 while ((cmd = next_cmd(&buf))) {
6355 if (strlencmp(cmd, "up") == 0) {
6356 if (level < max_level)
6357 level++;
6358 } else if (strlencmp(cmd, "down") == 0) {
6359 if (level > 0)
6360 level--;
6361 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6362 level >= 0 && level <= max_level) {
6363 /* new level set */
6364 } else
6365 return -EINVAL;
6368 tpacpi_disclose_usertask("procfs brightness",
6369 "set level to %d\n", level);
6372 * Now we know what the final level should be, so we try to set it.
6373 * Doing it this way makes the syscall restartable in case of EINTR
6375 rc = brightness_set(level);
6376 if (!rc && ibm_backlight_device)
6377 backlight_force_update(ibm_backlight_device,
6378 BACKLIGHT_UPDATE_SYSFS);
6379 return (rc == -EINTR)? -ERESTARTSYS : rc;
6382 static struct ibm_struct brightness_driver_data = {
6383 .name = "brightness",
6384 .read = brightness_read,
6385 .write = brightness_write,
6386 .exit = brightness_exit,
6387 .suspend = brightness_suspend,
6388 .shutdown = brightness_shutdown,
6391 /*************************************************************************
6392 * Volume subdriver
6396 * IBM ThinkPads have a simple volume controller with MUTE gating.
6397 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6399 * Since the *61 series (and probably also the later *60 series), Lenovo
6400 * ThinkPads only implement the MUTE gate.
6402 * EC register 0x30
6403 * Bit 6: MUTE (1 mutes sound)
6404 * Bit 3-0: Volume
6405 * Other bits should be zero as far as we know.
6407 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6408 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6409 * such as bit 7 which is used to detect repeated presses of MUTE,
6410 * and we leave them unchanged.
6413 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6415 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6416 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6417 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6419 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6420 static char *alsa_id = "ThinkPadEC";
6421 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6423 struct tpacpi_alsa_data {
6424 struct snd_card *card;
6425 struct snd_ctl_elem_id *ctl_mute_id;
6426 struct snd_ctl_elem_id *ctl_vol_id;
6429 static struct snd_card *alsa_card;
6431 enum {
6432 TP_EC_AUDIO = 0x30,
6434 /* TP_EC_AUDIO bits */
6435 TP_EC_AUDIO_MUTESW = 6,
6437 /* TP_EC_AUDIO bitmasks */
6438 TP_EC_AUDIO_LVL_MSK = 0x0F,
6439 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6441 /* Maximum volume */
6442 TP_EC_VOLUME_MAX = 14,
6445 enum tpacpi_volume_access_mode {
6446 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6447 TPACPI_VOL_MODE_EC, /* Pure EC control */
6448 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6449 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6450 TPACPI_VOL_MODE_MAX
6453 enum tpacpi_volume_capabilities {
6454 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6455 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6456 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6457 TPACPI_VOL_CAP_MAX
6460 static enum tpacpi_volume_access_mode volume_mode =
6461 TPACPI_VOL_MODE_MAX;
6463 static enum tpacpi_volume_capabilities volume_capabilities;
6464 static int volume_control_allowed;
6467 * Used to syncronize writers to TP_EC_AUDIO and
6468 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6470 static struct mutex volume_mutex;
6472 static void tpacpi_volume_checkpoint_nvram(void)
6474 u8 lec = 0;
6475 u8 b_nvram;
6476 u8 ec_mask;
6478 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6479 return;
6480 if (!volume_control_allowed)
6481 return;
6483 vdbg_printk(TPACPI_DBG_MIXER,
6484 "trying to checkpoint mixer state to NVRAM...\n");
6486 if (tp_features.mixer_no_level_control)
6487 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6488 else
6489 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6491 if (mutex_lock_killable(&volume_mutex) < 0)
6492 return;
6494 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6495 goto unlock;
6496 lec &= ec_mask;
6497 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6499 if (lec != (b_nvram & ec_mask)) {
6500 /* NVRAM needs update */
6501 b_nvram &= ~ec_mask;
6502 b_nvram |= lec;
6503 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6504 dbg_printk(TPACPI_DBG_MIXER,
6505 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6506 (unsigned int) lec, (unsigned int) b_nvram);
6507 } else {
6508 vdbg_printk(TPACPI_DBG_MIXER,
6509 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6510 (unsigned int) lec, (unsigned int) b_nvram);
6513 unlock:
6514 mutex_unlock(&volume_mutex);
6517 static int volume_get_status_ec(u8 *status)
6519 u8 s;
6521 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6522 return -EIO;
6524 *status = s;
6526 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6528 return 0;
6531 static int volume_get_status(u8 *status)
6533 return volume_get_status_ec(status);
6536 static int volume_set_status_ec(const u8 status)
6538 if (!acpi_ec_write(TP_EC_AUDIO, status))
6539 return -EIO;
6541 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6543 return 0;
6546 static int volume_set_status(const u8 status)
6548 return volume_set_status_ec(status);
6551 static int volume_set_mute_ec(const bool mute)
6553 int rc;
6554 u8 s, n;
6556 if (mutex_lock_killable(&volume_mutex) < 0)
6557 return -EINTR;
6559 rc = volume_get_status_ec(&s);
6560 if (rc)
6561 goto unlock;
6563 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6564 s & ~TP_EC_AUDIO_MUTESW_MSK;
6566 if (n != s)
6567 rc = volume_set_status_ec(n);
6569 unlock:
6570 mutex_unlock(&volume_mutex);
6571 return rc;
6574 static int volume_set_mute(const bool mute)
6576 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6577 (mute) ? "" : "un");
6578 return volume_set_mute_ec(mute);
6581 static int volume_set_volume_ec(const u8 vol)
6583 int rc;
6584 u8 s, n;
6586 if (vol > TP_EC_VOLUME_MAX)
6587 return -EINVAL;
6589 if (mutex_lock_killable(&volume_mutex) < 0)
6590 return -EINTR;
6592 rc = volume_get_status_ec(&s);
6593 if (rc)
6594 goto unlock;
6596 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6598 if (n != s)
6599 rc = volume_set_status_ec(n);
6601 unlock:
6602 mutex_unlock(&volume_mutex);
6603 return rc;
6606 static int volume_set_volume(const u8 vol)
6608 dbg_printk(TPACPI_DBG_MIXER,
6609 "trying to set volume level to %hu\n", vol);
6610 return volume_set_volume_ec(vol);
6613 static void volume_alsa_notify_change(void)
6615 struct tpacpi_alsa_data *d;
6617 if (alsa_card && alsa_card->private_data) {
6618 d = alsa_card->private_data;
6619 if (d->ctl_mute_id)
6620 snd_ctl_notify(alsa_card,
6621 SNDRV_CTL_EVENT_MASK_VALUE,
6622 d->ctl_mute_id);
6623 if (d->ctl_vol_id)
6624 snd_ctl_notify(alsa_card,
6625 SNDRV_CTL_EVENT_MASK_VALUE,
6626 d->ctl_vol_id);
6630 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6631 struct snd_ctl_elem_info *uinfo)
6633 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6634 uinfo->count = 1;
6635 uinfo->value.integer.min = 0;
6636 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6637 return 0;
6640 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6641 struct snd_ctl_elem_value *ucontrol)
6643 u8 s;
6644 int rc;
6646 rc = volume_get_status(&s);
6647 if (rc < 0)
6648 return rc;
6650 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6651 return 0;
6654 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6655 struct snd_ctl_elem_value *ucontrol)
6657 return volume_set_volume(ucontrol->value.integer.value[0]);
6660 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6662 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6663 struct snd_ctl_elem_value *ucontrol)
6665 u8 s;
6666 int rc;
6668 rc = volume_get_status(&s);
6669 if (rc < 0)
6670 return rc;
6672 ucontrol->value.integer.value[0] =
6673 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6674 return 0;
6677 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6678 struct snd_ctl_elem_value *ucontrol)
6680 return volume_set_mute(!ucontrol->value.integer.value[0]);
6683 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6685 .name = "Console Playback Volume",
6686 .index = 0,
6687 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6688 .info = volume_alsa_vol_info,
6689 .get = volume_alsa_vol_get,
6692 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6693 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6694 .name = "Console Playback Switch",
6695 .index = 0,
6696 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6697 .info = volume_alsa_mute_info,
6698 .get = volume_alsa_mute_get,
6701 static void volume_suspend(pm_message_t state)
6703 tpacpi_volume_checkpoint_nvram();
6706 static void volume_resume(void)
6708 volume_alsa_notify_change();
6711 static void volume_shutdown(void)
6713 tpacpi_volume_checkpoint_nvram();
6716 static void volume_exit(void)
6718 if (alsa_card) {
6719 snd_card_free(alsa_card);
6720 alsa_card = NULL;
6723 tpacpi_volume_checkpoint_nvram();
6726 static int __init volume_create_alsa_mixer(void)
6728 struct snd_card *card;
6729 struct tpacpi_alsa_data *data;
6730 struct snd_kcontrol *ctl_vol;
6731 struct snd_kcontrol *ctl_mute;
6732 int rc;
6734 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6735 sizeof(struct tpacpi_alsa_data), &card);
6736 if (rc < 0 || !card) {
6737 printk(TPACPI_ERR
6738 "Failed to create ALSA card structures: %d\n", rc);
6739 return 1;
6742 BUG_ON(!card->private_data);
6743 data = card->private_data;
6744 data->card = card;
6746 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6747 sizeof(card->driver));
6748 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6749 sizeof(card->shortname));
6750 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6751 (thinkpad_id.ec_version_str) ?
6752 thinkpad_id.ec_version_str : "(unknown)");
6753 snprintf(card->longname, sizeof(card->longname),
6754 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6755 (thinkpad_id.ec_version_str) ?
6756 thinkpad_id.ec_version_str : "unknown");
6758 if (volume_control_allowed) {
6759 volume_alsa_control_vol.put = volume_alsa_vol_put;
6760 volume_alsa_control_vol.access =
6761 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6763 volume_alsa_control_mute.put = volume_alsa_mute_put;
6764 volume_alsa_control_mute.access =
6765 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6768 if (!tp_features.mixer_no_level_control) {
6769 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6770 rc = snd_ctl_add(card, ctl_vol);
6771 if (rc < 0) {
6772 printk(TPACPI_ERR
6773 "Failed to create ALSA volume control: %d\n",
6774 rc);
6775 goto err_exit;
6777 data->ctl_vol_id = &ctl_vol->id;
6780 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6781 rc = snd_ctl_add(card, ctl_mute);
6782 if (rc < 0) {
6783 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6784 rc);
6785 goto err_exit;
6787 data->ctl_mute_id = &ctl_mute->id;
6789 snd_card_set_dev(card, &tpacpi_pdev->dev);
6790 rc = snd_card_register(card);
6791 if (rc < 0) {
6792 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6793 goto err_exit;
6796 alsa_card = card;
6797 return 0;
6799 err_exit:
6800 snd_card_free(card);
6801 return 1;
6804 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6805 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6807 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6808 /* Whitelist volume level on all IBM by default */
6809 { .vendor = PCI_VENDOR_ID_IBM,
6810 .bios = TPACPI_MATCH_ANY,
6811 .ec = TPACPI_MATCH_ANY,
6812 .quirks = TPACPI_VOL_Q_LEVEL },
6814 /* Lenovo models with volume control (needs confirmation) */
6815 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6816 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6817 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6818 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6819 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6820 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6821 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6823 /* Whitelist mute-only on all Lenovo by default */
6824 { .vendor = PCI_VENDOR_ID_LENOVO,
6825 .bios = TPACPI_MATCH_ANY,
6826 .ec = TPACPI_MATCH_ANY,
6827 .quirks = TPACPI_VOL_Q_MUTEONLY }
6830 static int __init volume_init(struct ibm_init_struct *iibm)
6832 unsigned long quirks;
6833 int rc;
6835 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6837 mutex_init(&volume_mutex);
6840 * Check for module parameter bogosity, note that we
6841 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6842 * able to detect "unspecified"
6844 if (volume_mode > TPACPI_VOL_MODE_MAX)
6845 return -EINVAL;
6847 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6848 printk(TPACPI_ERR
6849 "UCMS step volume mode not implemented, "
6850 "please contact %s\n", TPACPI_MAIL);
6851 return 1;
6854 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6855 return -EINVAL;
6858 * The ALSA mixer is our primary interface.
6859 * When disabled, don't install the subdriver at all
6861 if (!alsa_enable) {
6862 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6863 "ALSA mixer disabled by parameter, "
6864 "not loading volume subdriver...\n");
6865 return 1;
6868 quirks = tpacpi_check_quirks(volume_quirk_table,
6869 ARRAY_SIZE(volume_quirk_table));
6871 switch (volume_capabilities) {
6872 case TPACPI_VOL_CAP_AUTO:
6873 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6874 tp_features.mixer_no_level_control = 1;
6875 else if (quirks & TPACPI_VOL_Q_LEVEL)
6876 tp_features.mixer_no_level_control = 0;
6877 else
6878 return 1; /* no mixer */
6879 break;
6880 case TPACPI_VOL_CAP_VOLMUTE:
6881 tp_features.mixer_no_level_control = 0;
6882 break;
6883 case TPACPI_VOL_CAP_MUTEONLY:
6884 tp_features.mixer_no_level_control = 1;
6885 break;
6886 default:
6887 return 1;
6890 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6891 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6892 "using user-supplied volume_capabilities=%d\n",
6893 volume_capabilities);
6895 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6896 volume_mode == TPACPI_VOL_MODE_MAX) {
6897 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6899 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6900 "driver auto-selected volume_mode=%d\n",
6901 volume_mode);
6902 } else {
6903 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6904 "using user-supplied volume_mode=%d\n",
6905 volume_mode);
6908 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6909 "mute is supported, volume control is %s\n",
6910 str_supported(!tp_features.mixer_no_level_control));
6912 rc = volume_create_alsa_mixer();
6913 if (rc) {
6914 printk(TPACPI_ERR
6915 "Could not create the ALSA mixer interface\n");
6916 return rc;
6919 printk(TPACPI_INFO
6920 "Console audio control enabled, mode: %s\n",
6921 (volume_control_allowed) ?
6922 "override (read/write)" :
6923 "monitor (read only)");
6925 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6926 "registering volume hotkeys as change notification\n");
6927 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6928 | TP_ACPI_HKEY_VOLUP_MASK
6929 | TP_ACPI_HKEY_VOLDWN_MASK
6930 | TP_ACPI_HKEY_MUTE_MASK);
6932 return 0;
6935 static int volume_read(struct seq_file *m)
6937 u8 status;
6939 if (volume_get_status(&status) < 0) {
6940 seq_printf(m, "level:\t\tunreadable\n");
6941 } else {
6942 if (tp_features.mixer_no_level_control)
6943 seq_printf(m, "level:\t\tunsupported\n");
6944 else
6945 seq_printf(m, "level:\t\t%d\n",
6946 status & TP_EC_AUDIO_LVL_MSK);
6948 seq_printf(m, "mute:\t\t%s\n",
6949 onoff(status, TP_EC_AUDIO_MUTESW));
6951 if (volume_control_allowed) {
6952 seq_printf(m, "commands:\tunmute, mute\n");
6953 if (!tp_features.mixer_no_level_control) {
6954 seq_printf(m,
6955 "commands:\tup, down\n");
6956 seq_printf(m,
6957 "commands:\tlevel <level>"
6958 " (<level> is 0-%d)\n",
6959 TP_EC_VOLUME_MAX);
6964 return 0;
6967 static int volume_write(char *buf)
6969 u8 s;
6970 u8 new_level, new_mute;
6971 int l;
6972 char *cmd;
6973 int rc;
6976 * We do allow volume control at driver startup, so that the
6977 * user can set initial state through the volume=... parameter hack.
6979 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6980 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6981 tp_warned.volume_ctrl_forbidden = 1;
6982 printk(TPACPI_NOTICE
6983 "Console audio control in monitor mode, "
6984 "changes are not allowed.\n");
6985 printk(TPACPI_NOTICE
6986 "Use the volume_control=1 module parameter "
6987 "to enable volume control\n");
6989 return -EPERM;
6992 rc = volume_get_status(&s);
6993 if (rc < 0)
6994 return rc;
6996 new_level = s & TP_EC_AUDIO_LVL_MSK;
6997 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6999 while ((cmd = next_cmd(&buf))) {
7000 if (!tp_features.mixer_no_level_control) {
7001 if (strlencmp(cmd, "up") == 0) {
7002 if (new_mute)
7003 new_mute = 0;
7004 else if (new_level < TP_EC_VOLUME_MAX)
7005 new_level++;
7006 continue;
7007 } else if (strlencmp(cmd, "down") == 0) {
7008 if (new_mute)
7009 new_mute = 0;
7010 else if (new_level > 0)
7011 new_level--;
7012 continue;
7013 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7014 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7015 new_level = l;
7016 continue;
7019 if (strlencmp(cmd, "mute") == 0)
7020 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7021 else if (strlencmp(cmd, "unmute") == 0)
7022 new_mute = 0;
7023 else
7024 return -EINVAL;
7027 if (tp_features.mixer_no_level_control) {
7028 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7029 new_mute ? "" : "un");
7030 rc = volume_set_mute(!!new_mute);
7031 } else {
7032 tpacpi_disclose_usertask("procfs volume",
7033 "%smute and set level to %d\n",
7034 new_mute ? "" : "un", new_level);
7035 rc = volume_set_status(new_mute | new_level);
7037 volume_alsa_notify_change();
7039 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7042 static struct ibm_struct volume_driver_data = {
7043 .name = "volume",
7044 .read = volume_read,
7045 .write = volume_write,
7046 .exit = volume_exit,
7047 .suspend = volume_suspend,
7048 .resume = volume_resume,
7049 .shutdown = volume_shutdown,
7052 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7054 #define alsa_card NULL
7056 static void inline volume_alsa_notify_change(void)
7060 static int __init volume_init(struct ibm_init_struct *iibm)
7062 printk(TPACPI_INFO
7063 "volume: disabled as there is no ALSA support in this kernel\n");
7065 return 1;
7068 static struct ibm_struct volume_driver_data = {
7069 .name = "volume",
7072 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7074 /*************************************************************************
7075 * Fan subdriver
7079 * FAN ACCESS MODES
7081 * TPACPI_FAN_RD_ACPI_GFAN:
7082 * ACPI GFAN method: returns fan level
7084 * see TPACPI_FAN_WR_ACPI_SFAN
7085 * EC 0x2f (HFSP) not available if GFAN exists
7087 * TPACPI_FAN_WR_ACPI_SFAN:
7088 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7090 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7091 * it for writing.
7093 * TPACPI_FAN_WR_TPEC:
7094 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7095 * Supported on almost all ThinkPads
7097 * Fan speed changes of any sort (including those caused by the
7098 * disengaged mode) are usually done slowly by the firmware as the
7099 * maximum ammount of fan duty cycle change per second seems to be
7100 * limited.
7102 * Reading is not available if GFAN exists.
7103 * Writing is not available if SFAN exists.
7105 * Bits
7106 * 7 automatic mode engaged;
7107 * (default operation mode of the ThinkPad)
7108 * fan level is ignored in this mode.
7109 * 6 full speed mode (takes precedence over bit 7);
7110 * not available on all thinkpads. May disable
7111 * the tachometer while the fan controller ramps up
7112 * the speed (which can take up to a few *minutes*).
7113 * Speeds up fan to 100% duty-cycle, which is far above
7114 * the standard RPM levels. It is not impossible that
7115 * it could cause hardware damage.
7116 * 5-3 unused in some models. Extra bits for fan level
7117 * in others, but still useless as all values above
7118 * 7 map to the same speed as level 7 in these models.
7119 * 2-0 fan level (0..7 usually)
7120 * 0x00 = stop
7121 * 0x07 = max (set when temperatures critical)
7122 * Some ThinkPads may have other levels, see
7123 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7125 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7126 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7127 * does so, its initial value is meaningless (0x07).
7129 * For firmware bugs, refer to:
7130 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7132 * ----
7134 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7135 * Main fan tachometer reading (in RPM)
7137 * This register is present on all ThinkPads with a new-style EC, and
7138 * it is known not to be present on the A21m/e, and T22, as there is
7139 * something else in offset 0x84 according to the ACPI DSDT. Other
7140 * ThinkPads from this same time period (and earlier) probably lack the
7141 * tachometer as well.
7143 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7144 * was never fixed by IBM to report the EC firmware version string
7145 * probably support the tachometer (like the early X models), so
7146 * detecting it is quite hard. We need more data to know for sure.
7148 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7149 * might result.
7151 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7152 * mode.
7154 * For firmware bugs, refer to:
7155 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7157 * ----
7159 * ThinkPad EC register 0x31 bit 0 (only on select models)
7161 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7162 * show the speed of the main fan. When bit 0 of EC register 0x31
7163 * is one, the tachometer registers show the speed of the auxiliary
7164 * fan.
7166 * Fan control seems to affect both fans, regardless of the state
7167 * of this bit.
7169 * So far, only the firmware for the X60/X61 non-tablet versions
7170 * seem to support this (firmware TP-7M).
7172 * TPACPI_FAN_WR_ACPI_FANS:
7173 * ThinkPad X31, X40, X41. Not available in the X60.
7175 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7176 * high speed. ACPI DSDT seems to map these three speeds to levels
7177 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7178 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7180 * The speeds are stored on handles
7181 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7183 * There are three default speed sets, acessible as handles:
7184 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7186 * ACPI DSDT switches which set is in use depending on various
7187 * factors.
7189 * TPACPI_FAN_WR_TPEC is also available and should be used to
7190 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7191 * but the ACPI tables just mention level 7.
7194 enum { /* Fan control constants */
7195 fan_status_offset = 0x2f, /* EC register 0x2f */
7196 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7197 * 0x84 must be read before 0x85 */
7198 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7199 bit 0 selects which fan is active */
7201 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7202 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7204 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7207 enum fan_status_access_mode {
7208 TPACPI_FAN_NONE = 0, /* No fan status or control */
7209 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7210 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7213 enum fan_control_access_mode {
7214 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7215 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7216 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7217 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7220 enum fan_control_commands {
7221 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7222 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7223 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7224 * and also watchdog cmd */
7227 static int fan_control_allowed;
7229 static enum fan_status_access_mode fan_status_access_mode;
7230 static enum fan_control_access_mode fan_control_access_mode;
7231 static enum fan_control_commands fan_control_commands;
7233 static u8 fan_control_initial_status;
7234 static u8 fan_control_desired_level;
7235 static u8 fan_control_resume_level;
7236 static int fan_watchdog_maxinterval;
7238 static struct mutex fan_mutex;
7240 static void fan_watchdog_fire(struct work_struct *ignored);
7241 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7243 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7244 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7245 "\\FSPD", /* 600e/x, 770e, 770x */
7246 ); /* all others */
7247 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7248 "JFNS", /* 770x-JL */
7249 ); /* all others */
7252 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7253 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7254 * be in auto mode (0x80).
7256 * This is corrected by any write to HFSP either by the driver, or
7257 * by the firmware.
7259 * We assume 0x07 really means auto mode while this quirk is active,
7260 * as this is far more likely than the ThinkPad being in level 7,
7261 * which is only used by the firmware during thermal emergencies.
7263 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7264 * TP-70 (T43, R52), which are known to be buggy.
7267 static void fan_quirk1_setup(void)
7269 if (fan_control_initial_status == 0x07) {
7270 printk(TPACPI_NOTICE
7271 "fan_init: initial fan status is unknown, "
7272 "assuming it is in auto mode\n");
7273 tp_features.fan_ctrl_status_undef = 1;
7277 static void fan_quirk1_handle(u8 *fan_status)
7279 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7280 if (*fan_status != fan_control_initial_status) {
7281 /* something changed the HFSP regisnter since
7282 * driver init time, so it is not undefined
7283 * anymore */
7284 tp_features.fan_ctrl_status_undef = 0;
7285 } else {
7286 /* Return most likely status. In fact, it
7287 * might be the only possible status */
7288 *fan_status = TP_EC_FAN_AUTO;
7293 /* Select main fan on X60/X61, NOOP on others */
7294 static bool fan_select_fan1(void)
7296 if (tp_features.second_fan) {
7297 u8 val;
7299 if (ec_read(fan_select_offset, &val) < 0)
7300 return false;
7301 val &= 0xFEU;
7302 if (ec_write(fan_select_offset, val) < 0)
7303 return false;
7305 return true;
7308 /* Select secondary fan on X60/X61 */
7309 static bool fan_select_fan2(void)
7311 u8 val;
7313 if (!tp_features.second_fan)
7314 return false;
7316 if (ec_read(fan_select_offset, &val) < 0)
7317 return false;
7318 val |= 0x01U;
7319 if (ec_write(fan_select_offset, val) < 0)
7320 return false;
7322 return true;
7326 * Call with fan_mutex held
7328 static void fan_update_desired_level(u8 status)
7330 if ((status &
7331 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7332 if (status > 7)
7333 fan_control_desired_level = 7;
7334 else
7335 fan_control_desired_level = status;
7339 static int fan_get_status(u8 *status)
7341 u8 s;
7343 /* TODO:
7344 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7346 switch (fan_status_access_mode) {
7347 case TPACPI_FAN_RD_ACPI_GFAN:
7348 /* 570, 600e/x, 770e, 770x */
7350 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7351 return -EIO;
7353 if (likely(status))
7354 *status = s & 0x07;
7356 break;
7358 case TPACPI_FAN_RD_TPEC:
7359 /* all except 570, 600e/x, 770e, 770x */
7360 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7361 return -EIO;
7363 if (likely(status)) {
7364 *status = s;
7365 fan_quirk1_handle(status);
7368 break;
7370 default:
7371 return -ENXIO;
7374 return 0;
7377 static int fan_get_status_safe(u8 *status)
7379 int rc;
7380 u8 s;
7382 if (mutex_lock_killable(&fan_mutex))
7383 return -ERESTARTSYS;
7384 rc = fan_get_status(&s);
7385 if (!rc)
7386 fan_update_desired_level(s);
7387 mutex_unlock(&fan_mutex);
7389 if (status)
7390 *status = s;
7392 return rc;
7395 static int fan_get_speed(unsigned int *speed)
7397 u8 hi, lo;
7399 switch (fan_status_access_mode) {
7400 case TPACPI_FAN_RD_TPEC:
7401 /* all except 570, 600e/x, 770e, 770x */
7402 if (unlikely(!fan_select_fan1()))
7403 return -EIO;
7404 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7405 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7406 return -EIO;
7408 if (likely(speed))
7409 *speed = (hi << 8) | lo;
7411 break;
7413 default:
7414 return -ENXIO;
7417 return 0;
7420 static int fan2_get_speed(unsigned int *speed)
7422 u8 hi, lo;
7423 bool rc;
7425 switch (fan_status_access_mode) {
7426 case TPACPI_FAN_RD_TPEC:
7427 /* all except 570, 600e/x, 770e, 770x */
7428 if (unlikely(!fan_select_fan2()))
7429 return -EIO;
7430 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7431 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7432 fan_select_fan1(); /* play it safe */
7433 if (rc)
7434 return -EIO;
7436 if (likely(speed))
7437 *speed = (hi << 8) | lo;
7439 break;
7441 default:
7442 return -ENXIO;
7445 return 0;
7448 static int fan_set_level(int level)
7450 if (!fan_control_allowed)
7451 return -EPERM;
7453 switch (fan_control_access_mode) {
7454 case TPACPI_FAN_WR_ACPI_SFAN:
7455 if (level >= 0 && level <= 7) {
7456 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7457 return -EIO;
7458 } else
7459 return -EINVAL;
7460 break;
7462 case TPACPI_FAN_WR_ACPI_FANS:
7463 case TPACPI_FAN_WR_TPEC:
7464 if (!(level & TP_EC_FAN_AUTO) &&
7465 !(level & TP_EC_FAN_FULLSPEED) &&
7466 ((level < 0) || (level > 7)))
7467 return -EINVAL;
7469 /* safety net should the EC not support AUTO
7470 * or FULLSPEED mode bits and just ignore them */
7471 if (level & TP_EC_FAN_FULLSPEED)
7472 level |= 7; /* safety min speed 7 */
7473 else if (level & TP_EC_FAN_AUTO)
7474 level |= 4; /* safety min speed 4 */
7476 if (!acpi_ec_write(fan_status_offset, level))
7477 return -EIO;
7478 else
7479 tp_features.fan_ctrl_status_undef = 0;
7480 break;
7482 default:
7483 return -ENXIO;
7486 vdbg_printk(TPACPI_DBG_FAN,
7487 "fan control: set fan control register to 0x%02x\n", level);
7488 return 0;
7491 static int fan_set_level_safe(int level)
7493 int rc;
7495 if (!fan_control_allowed)
7496 return -EPERM;
7498 if (mutex_lock_killable(&fan_mutex))
7499 return -ERESTARTSYS;
7501 if (level == TPACPI_FAN_LAST_LEVEL)
7502 level = fan_control_desired_level;
7504 rc = fan_set_level(level);
7505 if (!rc)
7506 fan_update_desired_level(level);
7508 mutex_unlock(&fan_mutex);
7509 return rc;
7512 static int fan_set_enable(void)
7514 u8 s;
7515 int rc;
7517 if (!fan_control_allowed)
7518 return -EPERM;
7520 if (mutex_lock_killable(&fan_mutex))
7521 return -ERESTARTSYS;
7523 switch (fan_control_access_mode) {
7524 case TPACPI_FAN_WR_ACPI_FANS:
7525 case TPACPI_FAN_WR_TPEC:
7526 rc = fan_get_status(&s);
7527 if (rc < 0)
7528 break;
7530 /* Don't go out of emergency fan mode */
7531 if (s != 7) {
7532 s &= 0x07;
7533 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7536 if (!acpi_ec_write(fan_status_offset, s))
7537 rc = -EIO;
7538 else {
7539 tp_features.fan_ctrl_status_undef = 0;
7540 rc = 0;
7542 break;
7544 case TPACPI_FAN_WR_ACPI_SFAN:
7545 rc = fan_get_status(&s);
7546 if (rc < 0)
7547 break;
7549 s &= 0x07;
7551 /* Set fan to at least level 4 */
7552 s |= 4;
7554 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7555 rc = -EIO;
7556 else
7557 rc = 0;
7558 break;
7560 default:
7561 rc = -ENXIO;
7564 mutex_unlock(&fan_mutex);
7566 if (!rc)
7567 vdbg_printk(TPACPI_DBG_FAN,
7568 "fan control: set fan control register to 0x%02x\n",
7570 return rc;
7573 static int fan_set_disable(void)
7575 int rc;
7577 if (!fan_control_allowed)
7578 return -EPERM;
7580 if (mutex_lock_killable(&fan_mutex))
7581 return -ERESTARTSYS;
7583 rc = 0;
7584 switch (fan_control_access_mode) {
7585 case TPACPI_FAN_WR_ACPI_FANS:
7586 case TPACPI_FAN_WR_TPEC:
7587 if (!acpi_ec_write(fan_status_offset, 0x00))
7588 rc = -EIO;
7589 else {
7590 fan_control_desired_level = 0;
7591 tp_features.fan_ctrl_status_undef = 0;
7593 break;
7595 case TPACPI_FAN_WR_ACPI_SFAN:
7596 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7597 rc = -EIO;
7598 else
7599 fan_control_desired_level = 0;
7600 break;
7602 default:
7603 rc = -ENXIO;
7606 if (!rc)
7607 vdbg_printk(TPACPI_DBG_FAN,
7608 "fan control: set fan control register to 0\n");
7610 mutex_unlock(&fan_mutex);
7611 return rc;
7614 static int fan_set_speed(int speed)
7616 int rc;
7618 if (!fan_control_allowed)
7619 return -EPERM;
7621 if (mutex_lock_killable(&fan_mutex))
7622 return -ERESTARTSYS;
7624 rc = 0;
7625 switch (fan_control_access_mode) {
7626 case TPACPI_FAN_WR_ACPI_FANS:
7627 if (speed >= 0 && speed <= 65535) {
7628 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7629 speed, speed, speed))
7630 rc = -EIO;
7631 } else
7632 rc = -EINVAL;
7633 break;
7635 default:
7636 rc = -ENXIO;
7639 mutex_unlock(&fan_mutex);
7640 return rc;
7643 static void fan_watchdog_reset(void)
7645 static int fan_watchdog_active;
7647 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7648 return;
7650 if (fan_watchdog_active)
7651 cancel_delayed_work(&fan_watchdog_task);
7653 if (fan_watchdog_maxinterval > 0 &&
7654 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7655 fan_watchdog_active = 1;
7656 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7657 msecs_to_jiffies(fan_watchdog_maxinterval
7658 * 1000))) {
7659 printk(TPACPI_ERR
7660 "failed to queue the fan watchdog, "
7661 "watchdog will not trigger\n");
7663 } else
7664 fan_watchdog_active = 0;
7667 static void fan_watchdog_fire(struct work_struct *ignored)
7669 int rc;
7671 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7672 return;
7674 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7675 rc = fan_set_enable();
7676 if (rc < 0) {
7677 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7678 "will try again later...\n", -rc);
7679 /* reschedule for later */
7680 fan_watchdog_reset();
7685 * SYSFS fan layout: hwmon compatible (device)
7687 * pwm*_enable:
7688 * 0: "disengaged" mode
7689 * 1: manual mode
7690 * 2: native EC "auto" mode (recommended, hardware default)
7692 * pwm*: set speed in manual mode, ignored otherwise.
7693 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7694 * interpolation.
7696 * fan*_input: tachometer reading, RPM
7699 * SYSFS fan layout: extensions
7701 * fan_watchdog (driver):
7702 * fan watchdog interval in seconds, 0 disables (default), max 120
7705 /* sysfs fan pwm1_enable ----------------------------------------------- */
7706 static ssize_t fan_pwm1_enable_show(struct device *dev,
7707 struct device_attribute *attr,
7708 char *buf)
7710 int res, mode;
7711 u8 status;
7713 res = fan_get_status_safe(&status);
7714 if (res)
7715 return res;
7717 if (status & TP_EC_FAN_FULLSPEED) {
7718 mode = 0;
7719 } else if (status & TP_EC_FAN_AUTO) {
7720 mode = 2;
7721 } else
7722 mode = 1;
7724 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7727 static ssize_t fan_pwm1_enable_store(struct device *dev,
7728 struct device_attribute *attr,
7729 const char *buf, size_t count)
7731 unsigned long t;
7732 int res, level;
7734 if (parse_strtoul(buf, 2, &t))
7735 return -EINVAL;
7737 tpacpi_disclose_usertask("hwmon pwm1_enable",
7738 "set fan mode to %lu\n", t);
7740 switch (t) {
7741 case 0:
7742 level = TP_EC_FAN_FULLSPEED;
7743 break;
7744 case 1:
7745 level = TPACPI_FAN_LAST_LEVEL;
7746 break;
7747 case 2:
7748 level = TP_EC_FAN_AUTO;
7749 break;
7750 case 3:
7751 /* reserved for software-controlled auto mode */
7752 return -ENOSYS;
7753 default:
7754 return -EINVAL;
7757 res = fan_set_level_safe(level);
7758 if (res == -ENXIO)
7759 return -EINVAL;
7760 else if (res < 0)
7761 return res;
7763 fan_watchdog_reset();
7765 return count;
7768 static struct device_attribute dev_attr_fan_pwm1_enable =
7769 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7770 fan_pwm1_enable_show, fan_pwm1_enable_store);
7772 /* sysfs fan pwm1 ------------------------------------------------------ */
7773 static ssize_t fan_pwm1_show(struct device *dev,
7774 struct device_attribute *attr,
7775 char *buf)
7777 int res;
7778 u8 status;
7780 res = fan_get_status_safe(&status);
7781 if (res)
7782 return res;
7784 if ((status &
7785 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7786 status = fan_control_desired_level;
7788 if (status > 7)
7789 status = 7;
7791 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7794 static ssize_t fan_pwm1_store(struct device *dev,
7795 struct device_attribute *attr,
7796 const char *buf, size_t count)
7798 unsigned long s;
7799 int rc;
7800 u8 status, newlevel;
7802 if (parse_strtoul(buf, 255, &s))
7803 return -EINVAL;
7805 tpacpi_disclose_usertask("hwmon pwm1",
7806 "set fan speed to %lu\n", s);
7808 /* scale down from 0-255 to 0-7 */
7809 newlevel = (s >> 5) & 0x07;
7811 if (mutex_lock_killable(&fan_mutex))
7812 return -ERESTARTSYS;
7814 rc = fan_get_status(&status);
7815 if (!rc && (status &
7816 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7817 rc = fan_set_level(newlevel);
7818 if (rc == -ENXIO)
7819 rc = -EINVAL;
7820 else if (!rc) {
7821 fan_update_desired_level(newlevel);
7822 fan_watchdog_reset();
7826 mutex_unlock(&fan_mutex);
7827 return (rc)? rc : count;
7830 static struct device_attribute dev_attr_fan_pwm1 =
7831 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7832 fan_pwm1_show, fan_pwm1_store);
7834 /* sysfs fan fan1_input ------------------------------------------------ */
7835 static ssize_t fan_fan1_input_show(struct device *dev,
7836 struct device_attribute *attr,
7837 char *buf)
7839 int res;
7840 unsigned int speed;
7842 res = fan_get_speed(&speed);
7843 if (res < 0)
7844 return res;
7846 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7849 static struct device_attribute dev_attr_fan_fan1_input =
7850 __ATTR(fan1_input, S_IRUGO,
7851 fan_fan1_input_show, NULL);
7853 /* sysfs fan fan2_input ------------------------------------------------ */
7854 static ssize_t fan_fan2_input_show(struct device *dev,
7855 struct device_attribute *attr,
7856 char *buf)
7858 int res;
7859 unsigned int speed;
7861 res = fan2_get_speed(&speed);
7862 if (res < 0)
7863 return res;
7865 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7868 static struct device_attribute dev_attr_fan_fan2_input =
7869 __ATTR(fan2_input, S_IRUGO,
7870 fan_fan2_input_show, NULL);
7872 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7873 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7874 char *buf)
7876 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7879 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7880 const char *buf, size_t count)
7882 unsigned long t;
7884 if (parse_strtoul(buf, 120, &t))
7885 return -EINVAL;
7887 if (!fan_control_allowed)
7888 return -EPERM;
7890 fan_watchdog_maxinterval = t;
7891 fan_watchdog_reset();
7893 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7895 return count;
7898 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7899 fan_fan_watchdog_show, fan_fan_watchdog_store);
7901 /* --------------------------------------------------------------------- */
7902 static struct attribute *fan_attributes[] = {
7903 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7904 &dev_attr_fan_fan1_input.attr,
7905 NULL, /* for fan2_input */
7906 NULL
7909 static const struct attribute_group fan_attr_group = {
7910 .attrs = fan_attributes,
7913 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7914 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7916 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7917 { .vendor = PCI_VENDOR_ID_IBM, \
7918 .bios = TPACPI_MATCH_ANY, \
7919 .ec = TPID(__id1, __id2), \
7920 .quirks = __quirks }
7922 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7923 { .vendor = PCI_VENDOR_ID_LENOVO, \
7924 .bios = TPACPI_MATCH_ANY, \
7925 .ec = TPID(__id1, __id2), \
7926 .quirks = __quirks }
7928 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7929 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7930 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7931 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7932 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7933 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7936 #undef TPACPI_FAN_QL
7937 #undef TPACPI_FAN_QI
7939 static int __init fan_init(struct ibm_init_struct *iibm)
7941 int rc;
7942 unsigned long quirks;
7944 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7945 "initializing fan subdriver\n");
7947 mutex_init(&fan_mutex);
7948 fan_status_access_mode = TPACPI_FAN_NONE;
7949 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7950 fan_control_commands = 0;
7951 fan_watchdog_maxinterval = 0;
7952 tp_features.fan_ctrl_status_undef = 0;
7953 tp_features.second_fan = 0;
7954 fan_control_desired_level = 7;
7956 TPACPI_ACPIHANDLE_INIT(fans);
7957 TPACPI_ACPIHANDLE_INIT(gfan);
7958 TPACPI_ACPIHANDLE_INIT(sfan);
7960 quirks = tpacpi_check_quirks(fan_quirk_table,
7961 ARRAY_SIZE(fan_quirk_table));
7963 if (gfan_handle) {
7964 /* 570, 600e/x, 770e, 770x */
7965 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7966 } else {
7967 /* all other ThinkPads: note that even old-style
7968 * ThinkPad ECs supports the fan control register */
7969 if (likely(acpi_ec_read(fan_status_offset,
7970 &fan_control_initial_status))) {
7971 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7972 if (quirks & TPACPI_FAN_Q1)
7973 fan_quirk1_setup();
7974 if (quirks & TPACPI_FAN_2FAN) {
7975 tp_features.second_fan = 1;
7976 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7977 "secondary fan support enabled\n");
7979 } else {
7980 printk(TPACPI_ERR
7981 "ThinkPad ACPI EC access misbehaving, "
7982 "fan status and control unavailable\n");
7983 return 1;
7987 if (sfan_handle) {
7988 /* 570, 770x-JL */
7989 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7990 fan_control_commands |=
7991 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7992 } else {
7993 if (!gfan_handle) {
7994 /* gfan without sfan means no fan control */
7995 /* all other models implement TP EC 0x2f control */
7997 if (fans_handle) {
7998 /* X31, X40, X41 */
7999 fan_control_access_mode =
8000 TPACPI_FAN_WR_ACPI_FANS;
8001 fan_control_commands |=
8002 TPACPI_FAN_CMD_SPEED |
8003 TPACPI_FAN_CMD_LEVEL |
8004 TPACPI_FAN_CMD_ENABLE;
8005 } else {
8006 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8007 fan_control_commands |=
8008 TPACPI_FAN_CMD_LEVEL |
8009 TPACPI_FAN_CMD_ENABLE;
8014 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8015 "fan is %s, modes %d, %d\n",
8016 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8017 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8018 fan_status_access_mode, fan_control_access_mode);
8020 /* fan control master switch */
8021 if (!fan_control_allowed) {
8022 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8023 fan_control_commands = 0;
8024 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8025 "fan control features disabled by parameter\n");
8028 /* update fan_control_desired_level */
8029 if (fan_status_access_mode != TPACPI_FAN_NONE)
8030 fan_get_status_safe(NULL);
8032 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8033 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8034 if (tp_features.second_fan) {
8035 /* attach second fan tachometer */
8036 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8037 &dev_attr_fan_fan2_input.attr;
8039 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8040 &fan_attr_group);
8041 if (rc < 0)
8042 return rc;
8044 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8045 &driver_attr_fan_watchdog);
8046 if (rc < 0) {
8047 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8048 &fan_attr_group);
8049 return rc;
8051 return 0;
8052 } else
8053 return 1;
8056 static void fan_exit(void)
8058 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8059 "cancelling any pending fan watchdog tasks\n");
8061 /* FIXME: can we really do this unconditionally? */
8062 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8063 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8064 &driver_attr_fan_watchdog);
8066 cancel_delayed_work(&fan_watchdog_task);
8067 flush_workqueue(tpacpi_wq);
8070 static void fan_suspend(pm_message_t state)
8072 int rc;
8074 if (!fan_control_allowed)
8075 return;
8077 /* Store fan status in cache */
8078 fan_control_resume_level = 0;
8079 rc = fan_get_status_safe(&fan_control_resume_level);
8080 if (rc < 0)
8081 printk(TPACPI_NOTICE
8082 "failed to read fan level for later "
8083 "restore during resume: %d\n", rc);
8085 /* if it is undefined, don't attempt to restore it.
8086 * KEEP THIS LAST */
8087 if (tp_features.fan_ctrl_status_undef)
8088 fan_control_resume_level = 0;
8091 static void fan_resume(void)
8093 u8 current_level = 7;
8094 bool do_set = false;
8095 int rc;
8097 /* DSDT *always* updates status on resume */
8098 tp_features.fan_ctrl_status_undef = 0;
8100 if (!fan_control_allowed ||
8101 !fan_control_resume_level ||
8102 (fan_get_status_safe(&current_level) < 0))
8103 return;
8105 switch (fan_control_access_mode) {
8106 case TPACPI_FAN_WR_ACPI_SFAN:
8107 /* never decrease fan level */
8108 do_set = (fan_control_resume_level > current_level);
8109 break;
8110 case TPACPI_FAN_WR_ACPI_FANS:
8111 case TPACPI_FAN_WR_TPEC:
8112 /* never decrease fan level, scale is:
8113 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8115 * We expect the firmware to set either 7 or AUTO, but we
8116 * handle FULLSPEED out of paranoia.
8118 * So, we can safely only restore FULLSPEED or 7, anything
8119 * else could slow the fan. Restoring AUTO is useless, at
8120 * best that's exactly what the DSDT already set (it is the
8121 * slower it uses).
8123 * Always keep in mind that the DSDT *will* have set the
8124 * fans to what the vendor supposes is the best level. We
8125 * muck with it only to speed the fan up.
8127 if (fan_control_resume_level != 7 &&
8128 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8129 return;
8130 else
8131 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8132 (current_level != fan_control_resume_level);
8133 break;
8134 default:
8135 return;
8137 if (do_set) {
8138 printk(TPACPI_NOTICE
8139 "restoring fan level to 0x%02x\n",
8140 fan_control_resume_level);
8141 rc = fan_set_level_safe(fan_control_resume_level);
8142 if (rc < 0)
8143 printk(TPACPI_NOTICE
8144 "failed to restore fan level: %d\n", rc);
8148 static int fan_read(struct seq_file *m)
8150 int rc;
8151 u8 status;
8152 unsigned int speed = 0;
8154 switch (fan_status_access_mode) {
8155 case TPACPI_FAN_RD_ACPI_GFAN:
8156 /* 570, 600e/x, 770e, 770x */
8157 rc = fan_get_status_safe(&status);
8158 if (rc < 0)
8159 return rc;
8161 seq_printf(m, "status:\t\t%s\n"
8162 "level:\t\t%d\n",
8163 (status != 0) ? "enabled" : "disabled", status);
8164 break;
8166 case TPACPI_FAN_RD_TPEC:
8167 /* all except 570, 600e/x, 770e, 770x */
8168 rc = fan_get_status_safe(&status);
8169 if (rc < 0)
8170 return rc;
8172 seq_printf(m, "status:\t\t%s\n",
8173 (status != 0) ? "enabled" : "disabled");
8175 rc = fan_get_speed(&speed);
8176 if (rc < 0)
8177 return rc;
8179 seq_printf(m, "speed:\t\t%d\n", speed);
8181 if (status & TP_EC_FAN_FULLSPEED)
8182 /* Disengaged mode takes precedence */
8183 seq_printf(m, "level:\t\tdisengaged\n");
8184 else if (status & TP_EC_FAN_AUTO)
8185 seq_printf(m, "level:\t\tauto\n");
8186 else
8187 seq_printf(m, "level:\t\t%d\n", status);
8188 break;
8190 case TPACPI_FAN_NONE:
8191 default:
8192 seq_printf(m, "status:\t\tnot supported\n");
8195 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8196 seq_printf(m, "commands:\tlevel <level>");
8198 switch (fan_control_access_mode) {
8199 case TPACPI_FAN_WR_ACPI_SFAN:
8200 seq_printf(m, " (<level> is 0-7)\n");
8201 break;
8203 default:
8204 seq_printf(m, " (<level> is 0-7, "
8205 "auto, disengaged, full-speed)\n");
8206 break;
8210 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8211 seq_printf(m, "commands:\tenable, disable\n"
8212 "commands:\twatchdog <timeout> (<timeout> "
8213 "is 0 (off), 1-120 (seconds))\n");
8215 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8216 seq_printf(m, "commands:\tspeed <speed>"
8217 " (<speed> is 0-65535)\n");
8219 return 0;
8222 static int fan_write_cmd_level(const char *cmd, int *rc)
8224 int level;
8226 if (strlencmp(cmd, "level auto") == 0)
8227 level = TP_EC_FAN_AUTO;
8228 else if ((strlencmp(cmd, "level disengaged") == 0) |
8229 (strlencmp(cmd, "level full-speed") == 0))
8230 level = TP_EC_FAN_FULLSPEED;
8231 else if (sscanf(cmd, "level %d", &level) != 1)
8232 return 0;
8234 *rc = fan_set_level_safe(level);
8235 if (*rc == -ENXIO)
8236 printk(TPACPI_ERR "level command accepted for unsupported "
8237 "access mode %d", fan_control_access_mode);
8238 else if (!*rc)
8239 tpacpi_disclose_usertask("procfs fan",
8240 "set level to %d\n", level);
8242 return 1;
8245 static int fan_write_cmd_enable(const char *cmd, int *rc)
8247 if (strlencmp(cmd, "enable") != 0)
8248 return 0;
8250 *rc = fan_set_enable();
8251 if (*rc == -ENXIO)
8252 printk(TPACPI_ERR "enable command accepted for unsupported "
8253 "access mode %d", fan_control_access_mode);
8254 else if (!*rc)
8255 tpacpi_disclose_usertask("procfs fan", "enable\n");
8257 return 1;
8260 static int fan_write_cmd_disable(const char *cmd, int *rc)
8262 if (strlencmp(cmd, "disable") != 0)
8263 return 0;
8265 *rc = fan_set_disable();
8266 if (*rc == -ENXIO)
8267 printk(TPACPI_ERR "disable command accepted for unsupported "
8268 "access mode %d", fan_control_access_mode);
8269 else if (!*rc)
8270 tpacpi_disclose_usertask("procfs fan", "disable\n");
8272 return 1;
8275 static int fan_write_cmd_speed(const char *cmd, int *rc)
8277 int speed;
8279 /* TODO:
8280 * Support speed <low> <medium> <high> ? */
8282 if (sscanf(cmd, "speed %d", &speed) != 1)
8283 return 0;
8285 *rc = fan_set_speed(speed);
8286 if (*rc == -ENXIO)
8287 printk(TPACPI_ERR "speed command accepted for unsupported "
8288 "access mode %d", fan_control_access_mode);
8289 else if (!*rc)
8290 tpacpi_disclose_usertask("procfs fan",
8291 "set speed to %d\n", speed);
8293 return 1;
8296 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8298 int interval;
8300 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8301 return 0;
8303 if (interval < 0 || interval > 120)
8304 *rc = -EINVAL;
8305 else {
8306 fan_watchdog_maxinterval = interval;
8307 tpacpi_disclose_usertask("procfs fan",
8308 "set watchdog timer to %d\n",
8309 interval);
8312 return 1;
8315 static int fan_write(char *buf)
8317 char *cmd;
8318 int rc = 0;
8320 while (!rc && (cmd = next_cmd(&buf))) {
8321 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8322 fan_write_cmd_level(cmd, &rc)) &&
8323 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8324 (fan_write_cmd_enable(cmd, &rc) ||
8325 fan_write_cmd_disable(cmd, &rc) ||
8326 fan_write_cmd_watchdog(cmd, &rc))) &&
8327 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8328 fan_write_cmd_speed(cmd, &rc))
8330 rc = -EINVAL;
8331 else if (!rc)
8332 fan_watchdog_reset();
8335 return rc;
8338 static struct ibm_struct fan_driver_data = {
8339 .name = "fan",
8340 .read = fan_read,
8341 .write = fan_write,
8342 .exit = fan_exit,
8343 .suspend = fan_suspend,
8344 .resume = fan_resume,
8347 /****************************************************************************
8348 ****************************************************************************
8350 * Infrastructure
8352 ****************************************************************************
8353 ****************************************************************************/
8356 * HKEY event callout for other subdrivers go here
8357 * (yes, it is ugly, but it is quick, safe, and gets the job done
8359 static void tpacpi_driver_event(const unsigned int hkey_event)
8361 if (ibm_backlight_device) {
8362 switch (hkey_event) {
8363 case TP_HKEY_EV_BRGHT_UP:
8364 case TP_HKEY_EV_BRGHT_DOWN:
8365 tpacpi_brightness_notify_change();
8368 if (alsa_card) {
8369 switch (hkey_event) {
8370 case TP_HKEY_EV_VOL_UP:
8371 case TP_HKEY_EV_VOL_DOWN:
8372 case TP_HKEY_EV_VOL_MUTE:
8373 volume_alsa_notify_change();
8378 static void hotkey_driver_event(const unsigned int scancode)
8380 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8383 /* sysfs name ---------------------------------------------------------- */
8384 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8385 struct device_attribute *attr,
8386 char *buf)
8388 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8391 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8392 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8394 /* --------------------------------------------------------------------- */
8396 /* /proc support */
8397 static struct proc_dir_entry *proc_dir;
8400 * Module and infrastructure proble, init and exit handling
8403 static int force_load;
8405 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8406 static const char * __init str_supported(int is_supported)
8408 static char text_unsupported[] __initdata = "not supported";
8410 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8412 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8414 static void ibm_exit(struct ibm_struct *ibm)
8416 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8418 list_del_init(&ibm->all_drivers);
8420 if (ibm->flags.acpi_notify_installed) {
8421 dbg_printk(TPACPI_DBG_EXIT,
8422 "%s: acpi_remove_notify_handler\n", ibm->name);
8423 BUG_ON(!ibm->acpi);
8424 acpi_remove_notify_handler(*ibm->acpi->handle,
8425 ibm->acpi->type,
8426 dispatch_acpi_notify);
8427 ibm->flags.acpi_notify_installed = 0;
8428 ibm->flags.acpi_notify_installed = 0;
8431 if (ibm->flags.proc_created) {
8432 dbg_printk(TPACPI_DBG_EXIT,
8433 "%s: remove_proc_entry\n", ibm->name);
8434 remove_proc_entry(ibm->name, proc_dir);
8435 ibm->flags.proc_created = 0;
8438 if (ibm->flags.acpi_driver_registered) {
8439 dbg_printk(TPACPI_DBG_EXIT,
8440 "%s: acpi_bus_unregister_driver\n", ibm->name);
8441 BUG_ON(!ibm->acpi);
8442 acpi_bus_unregister_driver(ibm->acpi->driver);
8443 kfree(ibm->acpi->driver);
8444 ibm->acpi->driver = NULL;
8445 ibm->flags.acpi_driver_registered = 0;
8448 if (ibm->flags.init_called && ibm->exit) {
8449 ibm->exit();
8450 ibm->flags.init_called = 0;
8453 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8456 static int __init ibm_init(struct ibm_init_struct *iibm)
8458 int ret;
8459 struct ibm_struct *ibm = iibm->data;
8460 struct proc_dir_entry *entry;
8462 BUG_ON(ibm == NULL);
8464 INIT_LIST_HEAD(&ibm->all_drivers);
8466 if (ibm->flags.experimental && !experimental)
8467 return 0;
8469 dbg_printk(TPACPI_DBG_INIT,
8470 "probing for %s\n", ibm->name);
8472 if (iibm->init) {
8473 ret = iibm->init(iibm);
8474 if (ret > 0)
8475 return 0; /* probe failed */
8476 if (ret)
8477 return ret;
8479 ibm->flags.init_called = 1;
8482 if (ibm->acpi) {
8483 if (ibm->acpi->hid) {
8484 ret = register_tpacpi_subdriver(ibm);
8485 if (ret)
8486 goto err_out;
8489 if (ibm->acpi->notify) {
8490 ret = setup_acpi_notify(ibm);
8491 if (ret == -ENODEV) {
8492 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8493 ibm->name);
8494 ret = 0;
8495 goto err_out;
8497 if (ret < 0)
8498 goto err_out;
8502 dbg_printk(TPACPI_DBG_INIT,
8503 "%s installed\n", ibm->name);
8505 if (ibm->read) {
8506 mode_t mode = iibm->base_procfs_mode;
8508 if (!mode)
8509 mode = S_IRUGO;
8510 if (ibm->write)
8511 mode |= S_IWUSR;
8512 entry = proc_create_data(ibm->name, mode, proc_dir,
8513 &dispatch_proc_fops, ibm);
8514 if (!entry) {
8515 printk(TPACPI_ERR "unable to create proc entry %s\n",
8516 ibm->name);
8517 ret = -ENODEV;
8518 goto err_out;
8520 ibm->flags.proc_created = 1;
8523 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8525 return 0;
8527 err_out:
8528 dbg_printk(TPACPI_DBG_INIT,
8529 "%s: at error exit path with result %d\n",
8530 ibm->name, ret);
8532 ibm_exit(ibm);
8533 return (ret < 0)? ret : 0;
8536 /* Probing */
8538 static bool __pure __init tpacpi_is_fw_digit(const char c)
8540 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8543 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8544 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8545 const char t)
8547 return s && strlen(s) >= 8 &&
8548 tpacpi_is_fw_digit(s[0]) &&
8549 tpacpi_is_fw_digit(s[1]) &&
8550 s[2] == t && s[3] == 'T' &&
8551 tpacpi_is_fw_digit(s[4]) &&
8552 tpacpi_is_fw_digit(s[5]) &&
8553 s[6] == 'W' && s[7] == 'W';
8556 /* returns 0 - probe ok, or < 0 - probe error.
8557 * Probe ok doesn't mean thinkpad found.
8558 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8559 static int __must_check __init get_thinkpad_model_data(
8560 struct thinkpad_id_data *tp)
8562 const struct dmi_device *dev = NULL;
8563 char ec_fw_string[18];
8564 char const *s;
8566 if (!tp)
8567 return -EINVAL;
8569 memset(tp, 0, sizeof(*tp));
8571 if (dmi_name_in_vendors("IBM"))
8572 tp->vendor = PCI_VENDOR_ID_IBM;
8573 else if (dmi_name_in_vendors("LENOVO"))
8574 tp->vendor = PCI_VENDOR_ID_LENOVO;
8575 else
8576 return 0;
8578 s = dmi_get_system_info(DMI_BIOS_VERSION);
8579 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8580 if (s && !tp->bios_version_str)
8581 return -ENOMEM;
8583 /* Really ancient ThinkPad 240X will fail this, which is fine */
8584 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8585 return 0;
8587 tp->bios_model = tp->bios_version_str[0]
8588 | (tp->bios_version_str[1] << 8);
8589 tp->bios_release = (tp->bios_version_str[4] << 8)
8590 | tp->bios_version_str[5];
8593 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8594 * X32 or newer, all Z series; Some models must have an
8595 * up-to-date BIOS or they will not be detected.
8597 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8599 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8600 if (sscanf(dev->name,
8601 "IBM ThinkPad Embedded Controller -[%17c",
8602 ec_fw_string) == 1) {
8603 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8604 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8606 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8607 if (!tp->ec_version_str)
8608 return -ENOMEM;
8610 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8611 tp->ec_model = ec_fw_string[0]
8612 | (ec_fw_string[1] << 8);
8613 tp->ec_release = (ec_fw_string[4] << 8)
8614 | ec_fw_string[5];
8615 } else {
8616 printk(TPACPI_NOTICE
8617 "ThinkPad firmware release %s "
8618 "doesn't match the known patterns\n",
8619 ec_fw_string);
8620 printk(TPACPI_NOTICE
8621 "please report this to %s\n",
8622 TPACPI_MAIL);
8624 break;
8628 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8629 if (s && !strnicmp(s, "ThinkPad", 8)) {
8630 tp->model_str = kstrdup(s, GFP_KERNEL);
8631 if (!tp->model_str)
8632 return -ENOMEM;
8635 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8636 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8637 if (s && !tp->nummodel_str)
8638 return -ENOMEM;
8640 return 0;
8643 static int __init probe_for_thinkpad(void)
8645 int is_thinkpad;
8647 if (acpi_disabled)
8648 return -ENODEV;
8651 * Non-ancient models have better DMI tagging, but very old models
8652 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8654 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8655 (thinkpad_id.ec_model != 0) ||
8656 tpacpi_is_fw_known();
8658 /* ec is required because many other handles are relative to it */
8659 TPACPI_ACPIHANDLE_INIT(ec);
8660 if (!ec_handle) {
8661 if (is_thinkpad)
8662 printk(TPACPI_ERR
8663 "Not yet supported ThinkPad detected!\n");
8664 return -ENODEV;
8667 if (!is_thinkpad && !force_load)
8668 return -ENODEV;
8670 return 0;
8674 /* Module init, exit, parameters */
8676 static struct ibm_init_struct ibms_init[] __initdata = {
8678 .init = thinkpad_acpi_driver_init,
8679 .data = &thinkpad_acpi_driver_data,
8682 .init = hotkey_init,
8683 .data = &hotkey_driver_data,
8686 .init = bluetooth_init,
8687 .data = &bluetooth_driver_data,
8690 .init = wan_init,
8691 .data = &wan_driver_data,
8694 .init = uwb_init,
8695 .data = &uwb_driver_data,
8697 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8699 .init = video_init,
8700 .base_procfs_mode = S_IRUSR,
8701 .data = &video_driver_data,
8703 #endif
8705 .init = light_init,
8706 .data = &light_driver_data,
8709 .init = cmos_init,
8710 .data = &cmos_driver_data,
8713 .init = led_init,
8714 .data = &led_driver_data,
8717 .init = beep_init,
8718 .data = &beep_driver_data,
8721 .init = thermal_init,
8722 .data = &thermal_driver_data,
8725 .data = &ecdump_driver_data,
8728 .init = brightness_init,
8729 .data = &brightness_driver_data,
8732 .init = volume_init,
8733 .data = &volume_driver_data,
8736 .init = fan_init,
8737 .data = &fan_driver_data,
8741 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8743 unsigned int i;
8744 struct ibm_struct *ibm;
8746 if (!kp || !kp->name || !val)
8747 return -EINVAL;
8749 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8750 ibm = ibms_init[i].data;
8751 WARN_ON(ibm == NULL);
8753 if (!ibm || !ibm->name)
8754 continue;
8756 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8757 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8758 return -ENOSPC;
8759 strcpy(ibms_init[i].param, val);
8760 strcat(ibms_init[i].param, ",");
8761 return 0;
8765 return -EINVAL;
8768 module_param(experimental, int, 0444);
8769 MODULE_PARM_DESC(experimental,
8770 "Enables experimental features when non-zero");
8772 module_param_named(debug, dbg_level, uint, 0);
8773 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8775 module_param(force_load, bool, 0444);
8776 MODULE_PARM_DESC(force_load,
8777 "Attempts to load the driver even on a "
8778 "mis-identified ThinkPad when true");
8780 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8781 MODULE_PARM_DESC(fan_control,
8782 "Enables setting fan parameters features when true");
8784 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8785 MODULE_PARM_DESC(brightness_mode,
8786 "Selects brightness control strategy: "
8787 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8789 module_param(brightness_enable, uint, 0444);
8790 MODULE_PARM_DESC(brightness_enable,
8791 "Enables backlight control when 1, disables when 0");
8793 module_param(hotkey_report_mode, uint, 0444);
8794 MODULE_PARM_DESC(hotkey_report_mode,
8795 "used for backwards compatibility with userspace, "
8796 "see documentation");
8798 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8799 module_param_named(volume_mode, volume_mode, uint, 0444);
8800 MODULE_PARM_DESC(volume_mode,
8801 "Selects volume control strategy: "
8802 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8804 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8805 MODULE_PARM_DESC(volume_capabilities,
8806 "Selects the mixer capabilites: "
8807 "0=auto, 1=volume and mute, 2=mute only");
8809 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8810 MODULE_PARM_DESC(volume_control,
8811 "Enables software override for the console audio "
8812 "control when true");
8814 /* ALSA module API parameters */
8815 module_param_named(index, alsa_index, int, 0444);
8816 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8817 module_param_named(id, alsa_id, charp, 0444);
8818 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8819 module_param_named(enable, alsa_enable, bool, 0444);
8820 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8821 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8823 #define TPACPI_PARAM(feature) \
8824 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8825 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8826 "at module load, see documentation")
8828 TPACPI_PARAM(hotkey);
8829 TPACPI_PARAM(bluetooth);
8830 TPACPI_PARAM(video);
8831 TPACPI_PARAM(light);
8832 TPACPI_PARAM(cmos);
8833 TPACPI_PARAM(led);
8834 TPACPI_PARAM(beep);
8835 TPACPI_PARAM(ecdump);
8836 TPACPI_PARAM(brightness);
8837 TPACPI_PARAM(volume);
8838 TPACPI_PARAM(fan);
8840 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8841 module_param(dbg_wlswemul, uint, 0444);
8842 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8843 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8844 MODULE_PARM_DESC(wlsw_state,
8845 "Initial state of the emulated WLSW switch");
8847 module_param(dbg_bluetoothemul, uint, 0444);
8848 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8849 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8850 MODULE_PARM_DESC(bluetooth_state,
8851 "Initial state of the emulated bluetooth switch");
8853 module_param(dbg_wwanemul, uint, 0444);
8854 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8855 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8856 MODULE_PARM_DESC(wwan_state,
8857 "Initial state of the emulated WWAN switch");
8859 module_param(dbg_uwbemul, uint, 0444);
8860 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8861 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8862 MODULE_PARM_DESC(uwb_state,
8863 "Initial state of the emulated UWB switch");
8864 #endif
8866 static void thinkpad_acpi_module_exit(void)
8868 struct ibm_struct *ibm, *itmp;
8870 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8872 list_for_each_entry_safe_reverse(ibm, itmp,
8873 &tpacpi_all_drivers,
8874 all_drivers) {
8875 ibm_exit(ibm);
8878 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8880 if (tpacpi_inputdev) {
8881 if (tp_features.input_device_registered)
8882 input_unregister_device(tpacpi_inputdev);
8883 else
8884 input_free_device(tpacpi_inputdev);
8887 if (tpacpi_hwmon)
8888 hwmon_device_unregister(tpacpi_hwmon);
8890 if (tp_features.sensors_pdev_attrs_registered)
8891 device_remove_file(&tpacpi_sensors_pdev->dev,
8892 &dev_attr_thinkpad_acpi_pdev_name);
8893 if (tpacpi_sensors_pdev)
8894 platform_device_unregister(tpacpi_sensors_pdev);
8895 if (tpacpi_pdev)
8896 platform_device_unregister(tpacpi_pdev);
8898 if (tp_features.sensors_pdrv_attrs_registered)
8899 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8900 if (tp_features.platform_drv_attrs_registered)
8901 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8903 if (tp_features.sensors_pdrv_registered)
8904 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8906 if (tp_features.platform_drv_registered)
8907 platform_driver_unregister(&tpacpi_pdriver);
8909 if (proc_dir)
8910 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8912 if (tpacpi_wq)
8913 destroy_workqueue(tpacpi_wq);
8915 kfree(thinkpad_id.bios_version_str);
8916 kfree(thinkpad_id.ec_version_str);
8917 kfree(thinkpad_id.model_str);
8921 static int __init thinkpad_acpi_module_init(void)
8923 int ret, i;
8925 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8927 /* Parameter checking */
8928 if (hotkey_report_mode > 2)
8929 return -EINVAL;
8931 /* Driver-level probe */
8933 ret = get_thinkpad_model_data(&thinkpad_id);
8934 if (ret) {
8935 printk(TPACPI_ERR
8936 "unable to get DMI data: %d\n", ret);
8937 thinkpad_acpi_module_exit();
8938 return ret;
8940 ret = probe_for_thinkpad();
8941 if (ret) {
8942 thinkpad_acpi_module_exit();
8943 return ret;
8946 /* Driver initialization */
8948 TPACPI_ACPIHANDLE_INIT(ecrd);
8949 TPACPI_ACPIHANDLE_INIT(ecwr);
8951 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8952 if (!tpacpi_wq) {
8953 thinkpad_acpi_module_exit();
8954 return -ENOMEM;
8957 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8958 if (!proc_dir) {
8959 printk(TPACPI_ERR
8960 "unable to create proc dir " TPACPI_PROC_DIR);
8961 thinkpad_acpi_module_exit();
8962 return -ENODEV;
8965 ret = platform_driver_register(&tpacpi_pdriver);
8966 if (ret) {
8967 printk(TPACPI_ERR
8968 "unable to register main platform driver\n");
8969 thinkpad_acpi_module_exit();
8970 return ret;
8972 tp_features.platform_drv_registered = 1;
8974 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8975 if (ret) {
8976 printk(TPACPI_ERR
8977 "unable to register hwmon platform driver\n");
8978 thinkpad_acpi_module_exit();
8979 return ret;
8981 tp_features.sensors_pdrv_registered = 1;
8983 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8984 if (!ret) {
8985 tp_features.platform_drv_attrs_registered = 1;
8986 ret = tpacpi_create_driver_attributes(
8987 &tpacpi_hwmon_pdriver.driver);
8989 if (ret) {
8990 printk(TPACPI_ERR
8991 "unable to create sysfs driver attributes\n");
8992 thinkpad_acpi_module_exit();
8993 return ret;
8995 tp_features.sensors_pdrv_attrs_registered = 1;
8998 /* Device initialization */
8999 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9000 NULL, 0);
9001 if (IS_ERR(tpacpi_pdev)) {
9002 ret = PTR_ERR(tpacpi_pdev);
9003 tpacpi_pdev = NULL;
9004 printk(TPACPI_ERR "unable to register platform device\n");
9005 thinkpad_acpi_module_exit();
9006 return ret;
9008 tpacpi_sensors_pdev = platform_device_register_simple(
9009 TPACPI_HWMON_DRVR_NAME,
9010 -1, NULL, 0);
9011 if (IS_ERR(tpacpi_sensors_pdev)) {
9012 ret = PTR_ERR(tpacpi_sensors_pdev);
9013 tpacpi_sensors_pdev = NULL;
9014 printk(TPACPI_ERR
9015 "unable to register hwmon platform device\n");
9016 thinkpad_acpi_module_exit();
9017 return ret;
9019 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9020 &dev_attr_thinkpad_acpi_pdev_name);
9021 if (ret) {
9022 printk(TPACPI_ERR
9023 "unable to create sysfs hwmon device attributes\n");
9024 thinkpad_acpi_module_exit();
9025 return ret;
9027 tp_features.sensors_pdev_attrs_registered = 1;
9028 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9029 if (IS_ERR(tpacpi_hwmon)) {
9030 ret = PTR_ERR(tpacpi_hwmon);
9031 tpacpi_hwmon = NULL;
9032 printk(TPACPI_ERR "unable to register hwmon device\n");
9033 thinkpad_acpi_module_exit();
9034 return ret;
9036 mutex_init(&tpacpi_inputdev_send_mutex);
9037 tpacpi_inputdev = input_allocate_device();
9038 if (!tpacpi_inputdev) {
9039 printk(TPACPI_ERR "unable to allocate input device\n");
9040 thinkpad_acpi_module_exit();
9041 return -ENOMEM;
9042 } else {
9043 /* Prepare input device, but don't register */
9044 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9045 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9046 tpacpi_inputdev->id.bustype = BUS_HOST;
9047 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9048 thinkpad_id.vendor :
9049 PCI_VENDOR_ID_IBM;
9050 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9051 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9052 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9054 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9055 ret = ibm_init(&ibms_init[i]);
9056 if (ret >= 0 && *ibms_init[i].param)
9057 ret = ibms_init[i].data->write(ibms_init[i].param);
9058 if (ret < 0) {
9059 thinkpad_acpi_module_exit();
9060 return ret;
9064 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9066 ret = input_register_device(tpacpi_inputdev);
9067 if (ret < 0) {
9068 printk(TPACPI_ERR "unable to register input device\n");
9069 thinkpad_acpi_module_exit();
9070 return ret;
9071 } else {
9072 tp_features.input_device_registered = 1;
9075 return 0;
9078 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9081 * This will autoload the driver in almost every ThinkPad
9082 * in widespread use.
9084 * Only _VERY_ old models, like the 240, 240x and 570 lack
9085 * the HKEY event interface.
9087 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9090 * DMI matching for module autoloading
9092 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9093 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9095 * Only models listed in thinkwiki will be supported, so add yours
9096 * if it is not there yet.
9098 #define IBM_BIOS_MODULE_ALIAS(__type) \
9099 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9101 /* Ancient thinkpad BIOSes have to be identified by
9102 * BIOS type or model number, and there are far less
9103 * BIOS types than model numbers... */
9104 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9106 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9107 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9108 MODULE_DESCRIPTION(TPACPI_DESC);
9109 MODULE_VERSION(TPACPI_VERSION);
9110 MODULE_LICENSE("GPL");
9112 module_init(thinkpad_acpi_module_init);
9113 module_exit(thinkpad_acpi_module_exit);