tree-wide: fix comment/printk typos
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
bloba6289c2b7927169ef75ba31fed35d97946921b79
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 /* Misc NVRAM-related */
125 enum {
126 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 /* ACPI HIDs */
130 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
191 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
192 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
194 /* Thermal events */
195 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
196 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
197 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
198 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
199 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
201 /* Misc */
202 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
205 /****************************************************************************
206 * Main driver
209 #define TPACPI_NAME "thinkpad"
210 #define TPACPI_DESC "ThinkPad ACPI Extras"
211 #define TPACPI_FILE TPACPI_NAME "_acpi"
212 #define TPACPI_URL "http://ibm-acpi.sf.net/"
213 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
215 #define TPACPI_PROC_DIR "ibm"
216 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
217 #define TPACPI_DRVR_NAME TPACPI_FILE
218 #define TPACPI_DRVR_SHORTNAME "tpacpi"
219 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
221 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
222 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
224 #define TPACPI_MAX_ACPI_ARGS 3
226 /* printk headers */
227 #define TPACPI_LOG TPACPI_FILE ": "
228 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231 #define TPACPI_ERR KERN_ERR TPACPI_LOG
232 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
233 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234 #define TPACPI_INFO KERN_INFO TPACPI_LOG
235 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_acpimode:1;
309 u32 bright_unkfw:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul;
377 static int tpacpi_wlsw_emulstate;
378 static int dbg_bluetoothemul;
379 static int tpacpi_bluetooth_emulstate;
380 static int dbg_wwanemul;
381 static int tpacpi_wwan_emulstate;
382 static int dbg_uwbemul;
383 static int tpacpi_uwb_emulstate;
384 #endif
387 /*************************************************************************
388 * Debugging helpers
391 #define dbg_printk(a_dbg_level, format, arg...) \
392 do { if (dbg_level & (a_dbg_level)) \
393 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
394 } while (0)
396 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
397 #define vdbg_printk dbg_printk
398 static const char *str_supported(int is_supported);
399 #else
400 #define vdbg_printk(a_dbg_level, format, arg...) \
401 do { } while (0)
402 #endif
404 static void tpacpi_log_usertask(const char * const what)
406 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
407 what, task_tgid_vnr(current));
410 #define tpacpi_disclose_usertask(what, format, arg...) \
411 do { \
412 if (unlikely( \
413 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(TPACPI_DEBUG "%s: PID %d: " format, \
416 what, task_tgid_vnr(current), ## arg); \
418 } while (0)
421 * Quirk handling helpers
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
426 * We use values well outside that range as specials.
429 #define TPACPI_MATCH_ANY 0xffffU
430 #define TPACPI_MATCH_UNKNOWN 0U
432 /* TPID('1', 'Y') == 0x5931 */
433 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
435 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
453 struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
471 * The match criteria is: vendor, ec and bios much match.
473 static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
486 qlist_size--;
487 qlist++;
489 return 0;
492 static inline bool __pure __init tpacpi_is_lenovo(void)
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497 static inline bool __pure __init tpacpi_is_ibm(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502 /****************************************************************************
503 ****************************************************************************
505 * ACPI Helpers and device model
507 ****************************************************************************
508 ****************************************************************************/
510 /*************************************************************************
511 * ACPI basic handles
514 static acpi_handle root_handle;
515 static acpi_handle ec_handle;
517 #define TPACPI_HANDLE(object, parent, paths...) \
518 static acpi_handle object##_handle; \
519 static const acpi_handle *object##_parent __initdata = \
520 &parent##_handle; \
521 static char *object##_paths[] __initdata = { paths }
523 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
526 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
527 /* T4x, X31, X40 */
528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
530 ); /* all others */
532 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
535 ); /* 570 */
537 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
538 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
539 "\\_SB.PCI0.VID0", /* 770e */
540 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
541 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
542 "\\_SB.PCI0.AGP.VID", /* all others */
543 ); /* R30, R31 */
546 /*************************************************************************
547 * ACPI helpers
550 static int acpi_evalf(acpi_handle handle,
551 void *res, char *method, char *fmt, ...)
553 char *fmt0 = fmt;
554 struct acpi_object_list params;
555 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
556 struct acpi_buffer result, *resultp;
557 union acpi_object out_obj;
558 acpi_status status;
559 va_list ap;
560 char res_type;
561 int success;
562 int quiet;
564 if (!*fmt) {
565 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
566 return 0;
569 if (*fmt == 'q') {
570 quiet = 1;
571 fmt++;
572 } else
573 quiet = 0;
575 res_type = *(fmt++);
577 params.count = 0;
578 params.pointer = &in_objs[0];
580 va_start(ap, fmt);
581 while (*fmt) {
582 char c = *(fmt++);
583 switch (c) {
584 case 'd': /* int */
585 in_objs[params.count].integer.value = va_arg(ap, int);
586 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
587 break;
588 /* add more types as needed */
589 default:
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", c);
592 return 0;
595 va_end(ap);
597 if (res_type != 'v') {
598 result.length = sizeof(out_obj);
599 result.pointer = &out_obj;
600 resultp = &result;
601 } else
602 resultp = NULL;
604 status = acpi_evaluate_object(handle, method, &params, resultp);
606 switch (res_type) {
607 case 'd': /* int */
608 success = (status == AE_OK &&
609 out_obj.type == ACPI_TYPE_INTEGER);
610 if (success && res)
611 *(int *)res = out_obj.integer.value;
612 break;
613 case 'v': /* void */
614 success = status == AE_OK;
615 break;
616 /* add more types as needed */
617 default:
618 printk(TPACPI_ERR "acpi_evalf() called "
619 "with invalid format character '%c'\n", res_type);
620 return 0;
623 if (!success && !quiet)
624 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
625 method, fmt0, acpi_format_exception(status));
627 return success;
630 static int acpi_ec_read(int i, u8 *p)
632 int v;
634 if (ecrd_handle) {
635 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
636 return 0;
637 *p = v;
638 } else {
639 if (ec_read(i, p) < 0)
640 return 0;
643 return 1;
646 static int acpi_ec_write(int i, u8 v)
648 if (ecwr_handle) {
649 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
650 return 0;
651 } else {
652 if (ec_write(i, v) < 0)
653 return 0;
656 return 1;
659 static int issue_thinkpad_cmos_command(int cmos_cmd)
661 if (!cmos_handle)
662 return -ENXIO;
664 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
665 return -EIO;
667 return 0;
670 /*************************************************************************
671 * ACPI device model
674 #define TPACPI_ACPIHANDLE_INIT(object) \
675 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
676 object##_paths, ARRAY_SIZE(object##_paths))
678 static void __init drv_acpi_handle_init(const char *name,
679 acpi_handle *handle, const acpi_handle parent,
680 char **paths, const int num_paths)
682 int i;
683 acpi_status status;
685 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
686 name);
688 for (i = 0; i < num_paths; i++) {
689 status = acpi_get_handle(parent, paths[i], handle);
690 if (ACPI_SUCCESS(status)) {
691 dbg_printk(TPACPI_DBG_INIT,
692 "Found ACPI handle %s for %s\n",
693 paths[i], name);
694 return;
698 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
699 name);
700 *handle = NULL;
703 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
704 u32 level, void *context, void **return_value)
706 *(acpi_handle *)return_value = handle;
708 return AE_CTRL_TERMINATE;
711 static void __init tpacpi_acpi_handle_locate(const char *name,
712 const char *hid,
713 acpi_handle *handle)
715 acpi_status status;
716 acpi_handle device_found;
718 BUG_ON(!name || !hid || !handle);
719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
721 name, hid);
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
727 *handle = NULL;
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
733 } else {
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
740 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
742 struct ibm_struct *ibm = data;
744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 return;
747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
748 return;
750 ibm->acpi->notify(ibm, event);
753 static int __init setup_acpi_notify(struct ibm_struct *ibm)
755 acpi_status status;
756 int rc;
758 BUG_ON(!ibm->acpi);
760 if (!*ibm->acpi->handle)
761 return 0;
763 vdbg_printk(TPACPI_DBG_INIT,
764 "setting up ACPI notify for %s\n", ibm->name);
766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
767 if (rc < 0) {
768 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
769 ibm->name, rc);
770 return -ENODEV;
773 ibm->acpi->device->driver_data = ibm;
774 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
775 TPACPI_ACPI_EVENT_PREFIX,
776 ibm->name);
778 status = acpi_install_notify_handler(*ibm->acpi->handle,
779 ibm->acpi->type, dispatch_acpi_notify, ibm);
780 if (ACPI_FAILURE(status)) {
781 if (status == AE_ALREADY_EXISTS) {
782 printk(TPACPI_NOTICE
783 "another device driver is already "
784 "handling %s events\n", ibm->name);
785 } else {
786 printk(TPACPI_ERR
787 "acpi_install_notify_handler(%s) failed: %s\n",
788 ibm->name, acpi_format_exception(status));
790 return -ENODEV;
792 ibm->flags.acpi_notify_installed = 1;
793 return 0;
796 static int __init tpacpi_device_add(struct acpi_device *device)
798 return 0;
801 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
803 int rc;
805 dbg_printk(TPACPI_DBG_INIT,
806 "registering %s as an ACPI driver\n", ibm->name);
808 BUG_ON(!ibm->acpi);
810 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
811 if (!ibm->acpi->driver) {
812 printk(TPACPI_ERR
813 "failed to allocate memory for ibm->acpi->driver\n");
814 return -ENOMEM;
817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
818 ibm->acpi->driver->ids = ibm->acpi->hid;
820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
822 rc = acpi_bus_register_driver(ibm->acpi->driver);
823 if (rc < 0) {
824 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
825 ibm->name, rc);
826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
828 } else if (!rc)
829 ibm->flags.acpi_driver_registered = 1;
831 return rc;
835 /****************************************************************************
836 ****************************************************************************
838 * Procfs Helpers
840 ****************************************************************************
841 ****************************************************************************/
843 static int dispatch_proc_show(struct seq_file *m, void *v)
845 struct ibm_struct *ibm = m->private;
847 if (!ibm || !ibm->read)
848 return -EINVAL;
849 return ibm->read(m);
852 static int dispatch_proc_open(struct inode *inode, struct file *file)
854 return single_open(file, dispatch_proc_show, PDE(inode)->data);
857 static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
861 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
862 char *kernbuf;
863 int ret;
865 if (!ibm || !ibm->write)
866 return -EINVAL;
867 if (count > PAGE_SIZE - 2)
868 return -EINVAL;
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
871 if (!kernbuf)
872 return -ENOMEM;
874 if (copy_from_user(kernbuf, userbuf, count)) {
875 kfree(kernbuf);
876 return -EFAULT;
879 kernbuf[count] = 0;
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
882 if (ret == 0)
883 ret = count;
885 kfree(kernbuf);
887 return ret;
890 static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = single_release,
896 .write = dispatch_proc_write,
899 static char *next_cmd(char **cmds)
901 char *start = *cmds;
902 char *end;
904 while ((end = strchr(start, ',')) && end == start)
905 start = end + 1;
907 if (!end)
908 return NULL;
910 *end = 0;
911 *cmds = end + 1;
912 return start;
916 /****************************************************************************
917 ****************************************************************************
919 * Device model: input, hwmon and platform
921 ****************************************************************************
922 ****************************************************************************/
924 static struct platform_device *tpacpi_pdev;
925 static struct platform_device *tpacpi_sensors_pdev;
926 static struct device *tpacpi_hwmon;
927 static struct input_dev *tpacpi_inputdev;
928 static struct mutex tpacpi_inputdev_send_mutex;
929 static LIST_HEAD(tpacpi_all_drivers);
931 static int tpacpi_suspend_handler(struct platform_device *pdev,
932 pm_message_t state)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->suspend)
940 (ibm->suspend)(state);
943 return 0;
946 static int tpacpi_resume_handler(struct platform_device *pdev)
948 struct ibm_struct *ibm, *itmp;
950 list_for_each_entry_safe(ibm, itmp,
951 &tpacpi_all_drivers,
952 all_drivers) {
953 if (ibm->resume)
954 (ibm->resume)();
957 return 0;
960 static void tpacpi_shutdown_handler(struct platform_device *pdev)
962 struct ibm_struct *ibm, *itmp;
964 list_for_each_entry_safe(ibm, itmp,
965 &tpacpi_all_drivers,
966 all_drivers) {
967 if (ibm->shutdown)
968 (ibm->shutdown)();
972 static struct platform_driver tpacpi_pdriver = {
973 .driver = {
974 .name = TPACPI_DRVR_NAME,
975 .owner = THIS_MODULE,
977 .suspend = tpacpi_suspend_handler,
978 .resume = tpacpi_resume_handler,
979 .shutdown = tpacpi_shutdown_handler,
982 static struct platform_driver tpacpi_hwmon_pdriver = {
983 .driver = {
984 .name = TPACPI_HWMON_DRVR_NAME,
985 .owner = THIS_MODULE,
989 /*************************************************************************
990 * sysfs support helpers
993 struct attribute_set {
994 unsigned int members, max_members;
995 struct attribute_group group;
998 struct attribute_set_obj {
999 struct attribute_set s;
1000 struct attribute *a;
1001 } __attribute__((packed));
1003 static struct attribute_set *create_attr_set(unsigned int max_members,
1004 const char *name)
1006 struct attribute_set_obj *sobj;
1008 if (max_members == 0)
1009 return NULL;
1011 /* Allocates space for implicit NULL at the end too */
1012 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1013 max_members * sizeof(struct attribute *),
1014 GFP_KERNEL);
1015 if (!sobj)
1016 return NULL;
1017 sobj->s.max_members = max_members;
1018 sobj->s.group.attrs = &sobj->a;
1019 sobj->s.group.name = name;
1021 return &sobj->s;
1024 #define destroy_attr_set(_set) \
1025 kfree(_set);
1027 /* not multi-threaded safe, use it in a single thread per set */
1028 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1030 if (!s || !attr)
1031 return -EINVAL;
1033 if (s->members >= s->max_members)
1034 return -ENOMEM;
1036 s->group.attrs[s->members] = attr;
1037 s->members++;
1039 return 0;
1042 static int add_many_to_attr_set(struct attribute_set *s,
1043 struct attribute **attr,
1044 unsigned int count)
1046 int i, res;
1048 for (i = 0; i < count; i++) {
1049 res = add_to_attr_set(s, attr[i]);
1050 if (res)
1051 return res;
1054 return 0;
1057 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1059 sysfs_remove_group(kobj, &s->group);
1060 destroy_attr_set(s);
1063 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1064 sysfs_create_group(_kobj, &_attr_set->group)
1066 static int parse_strtoul(const char *buf,
1067 unsigned long max, unsigned long *value)
1069 char *endp;
1071 while (*buf && isspace(*buf))
1072 buf++;
1073 *value = simple_strtoul(buf, &endp, 0);
1074 while (*endp && isspace(*endp))
1075 endp++;
1076 if (*endp || *value > max)
1077 return -EINVAL;
1079 return 0;
1082 static void tpacpi_disable_brightness_delay(void)
1084 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1085 printk(TPACPI_NOTICE
1086 "ACPI backlight control delay disabled\n");
1089 static void printk_deprecated_attribute(const char * const what,
1090 const char * const details)
1092 tpacpi_log_usertask("deprecated sysfs attribute");
1093 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1094 "will be removed. %s\n",
1095 what, details);
1098 /*************************************************************************
1099 * rfkill and radio control support helpers
1103 * ThinkPad-ACPI firmware handling model:
1105 * WLSW (master wireless switch) is event-driven, and is common to all
1106 * firmware-controlled radios. It cannot be controlled, just monitored,
1107 * as expected. It overrides all radio state in firmware
1109 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1110 * (TODO: verify how WLSW interacts with the returned radio state).
1112 * The only time there are shadow radio state changes, is when
1113 * masked-off hotkeys are used.
1117 * Internal driver API for radio state:
1119 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1120 * bool: true means radio blocked (off)
1122 enum tpacpi_rfkill_state {
1123 TPACPI_RFK_RADIO_OFF = 0,
1124 TPACPI_RFK_RADIO_ON
1127 /* rfkill switches */
1128 enum tpacpi_rfk_id {
1129 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1130 TPACPI_RFK_WWAN_SW_ID,
1131 TPACPI_RFK_UWB_SW_ID,
1132 TPACPI_RFK_SW_MAX
1135 static const char *tpacpi_rfkill_names[] = {
1136 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1137 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1138 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1139 [TPACPI_RFK_SW_MAX] = NULL
1142 /* ThinkPad-ACPI rfkill subdriver */
1143 struct tpacpi_rfk {
1144 struct rfkill *rfkill;
1145 enum tpacpi_rfk_id id;
1146 const struct tpacpi_rfk_ops *ops;
1149 struct tpacpi_rfk_ops {
1150 /* firmware interface */
1151 int (*get_status)(void);
1152 int (*set_status)(const enum tpacpi_rfkill_state);
1155 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1157 /* Query FW and update rfkill sw state for a given rfkill switch */
1158 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1160 int status;
1162 if (!tp_rfk)
1163 return -ENODEV;
1165 status = (tp_rfk->ops->get_status)();
1166 if (status < 0)
1167 return status;
1169 rfkill_set_sw_state(tp_rfk->rfkill,
1170 (status == TPACPI_RFK_RADIO_OFF));
1172 return status;
1175 /* Query FW and update rfkill sw state for all rfkill switches */
1176 static void tpacpi_rfk_update_swstate_all(void)
1178 unsigned int i;
1180 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1181 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1185 * Sync the HW-blocking state of all rfkill switches,
1186 * do notice it causes the rfkill core to schedule uevents
1188 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1190 unsigned int i;
1191 struct tpacpi_rfk *tp_rfk;
1193 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1194 tp_rfk = tpacpi_rfkill_switches[i];
1195 if (tp_rfk) {
1196 if (rfkill_set_hw_state(tp_rfk->rfkill,
1197 blocked)) {
1198 /* ignore -- we track sw block */
1204 /* Call to get the WLSW state from the firmware */
1205 static int hotkey_get_wlsw(void);
1207 /* Call to query WLSW state and update all rfkill switches */
1208 static bool tpacpi_rfk_check_hwblock_state(void)
1210 int res = hotkey_get_wlsw();
1211 int hw_blocked;
1213 /* When unknown or unsupported, we have to assume it is unblocked */
1214 if (res < 0)
1215 return false;
1217 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1218 tpacpi_rfk_update_hwblock_state(hw_blocked);
1220 return hw_blocked;
1223 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1225 struct tpacpi_rfk *tp_rfk = data;
1226 int res;
1228 dbg_printk(TPACPI_DBG_RFKILL,
1229 "request to change radio state to %s\n",
1230 blocked ? "blocked" : "unblocked");
1232 /* try to set radio state */
1233 res = (tp_rfk->ops->set_status)(blocked ?
1234 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1236 /* and update the rfkill core with whatever the FW really did */
1237 tpacpi_rfk_update_swstate(tp_rfk);
1239 return (res < 0) ? res : 0;
1242 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1243 .set_block = tpacpi_rfk_hook_set_block,
1246 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1247 const struct tpacpi_rfk_ops *tp_rfkops,
1248 const enum rfkill_type rfktype,
1249 const char *name,
1250 const bool set_default)
1252 struct tpacpi_rfk *atp_rfk;
1253 int res;
1254 bool sw_state = false;
1255 bool hw_state;
1256 int sw_status;
1258 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1260 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1261 if (atp_rfk)
1262 atp_rfk->rfkill = rfkill_alloc(name,
1263 &tpacpi_pdev->dev,
1264 rfktype,
1265 &tpacpi_rfk_rfkill_ops,
1266 atp_rfk);
1267 if (!atp_rfk || !atp_rfk->rfkill) {
1268 printk(TPACPI_ERR
1269 "failed to allocate memory for rfkill class\n");
1270 kfree(atp_rfk);
1271 return -ENOMEM;
1274 atp_rfk->id = id;
1275 atp_rfk->ops = tp_rfkops;
1277 sw_status = (tp_rfkops->get_status)();
1278 if (sw_status < 0) {
1279 printk(TPACPI_ERR
1280 "failed to read initial state for %s, error %d\n",
1281 name, sw_status);
1282 } else {
1283 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1284 if (set_default) {
1285 /* try to keep the initial state, since we ask the
1286 * firmware to preserve it across S5 in NVRAM */
1287 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1290 hw_state = tpacpi_rfk_check_hwblock_state();
1291 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1293 res = rfkill_register(atp_rfk->rfkill);
1294 if (res < 0) {
1295 printk(TPACPI_ERR
1296 "failed to register %s rfkill switch: %d\n",
1297 name, res);
1298 rfkill_destroy(atp_rfk->rfkill);
1299 kfree(atp_rfk);
1300 return res;
1303 tpacpi_rfkill_switches[id] = atp_rfk;
1305 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1306 name, (sw_state || hw_state) ? "" : "un");
1307 return 0;
1310 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312 struct tpacpi_rfk *tp_rfk;
1314 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316 tp_rfk = tpacpi_rfkill_switches[id];
1317 if (tp_rfk) {
1318 rfkill_unregister(tp_rfk->rfkill);
1319 rfkill_destroy(tp_rfk->rfkill);
1320 tpacpi_rfkill_switches[id] = NULL;
1321 kfree(tp_rfk);
1325 static void printk_deprecated_rfkill_attribute(const char * const what)
1327 printk_deprecated_attribute(what,
1328 "Please switch to generic rfkill before year 2010");
1331 /* sysfs <radio> enable ------------------------------------------------ */
1332 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1333 struct device_attribute *attr,
1334 char *buf)
1336 int status;
1338 printk_deprecated_rfkill_attribute(attr->attr.name);
1340 /* This is in the ABI... */
1341 if (tpacpi_rfk_check_hwblock_state()) {
1342 status = TPACPI_RFK_RADIO_OFF;
1343 } else {
1344 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1345 if (status < 0)
1346 return status;
1349 return snprintf(buf, PAGE_SIZE, "%d\n",
1350 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1353 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 const char *buf, size_t count)
1357 unsigned long t;
1358 int res;
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1362 if (parse_strtoul(buf, 1, &t))
1363 return -EINVAL;
1365 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state() && !!t)
1369 return -EPERM;
1371 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1373 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375 return (res < 0) ? res : count;
1378 /* procfs -------------------------------------------------------------- */
1379 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1380 struct seq_file *m)
1382 if (id >= TPACPI_RFK_SW_MAX)
1383 seq_printf(m, "status:\t\tnot supported\n");
1384 else {
1385 int status;
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1397 seq_printf(m, "status:\t\t%s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
1400 seq_printf(m, "commands:\tenable, disable\n");
1403 return 0;
1406 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1433 return res;
1436 /*************************************************************************
1437 * thinkpad-acpi driver attributes
1440 /* interface_version --------------------------------------------------- */
1441 static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1448 static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1451 /* debug_level --------------------------------------------------------- */
1452 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1458 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1461 unsigned long t;
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1466 dbg_level = t;
1468 return count;
1471 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1474 /* version ------------------------------------------------------------- */
1475 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
1482 static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1485 /* --------------------------------------------------------------------- */
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1496 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1499 unsigned long t;
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1504 if (tpacpi_wlsw_emulstate != !!t) {
1505 tpacpi_wlsw_emulstate = !!t;
1506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1509 return count;
1512 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1516 /* bluetooth_emulstate ------------------------------------------------- */
1517 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1524 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1528 unsigned long t;
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1533 tpacpi_bluetooth_emulstate = !!t;
1535 return count;
1538 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1542 /* wwan_emulstate ------------------------------------------------- */
1543 static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1550 static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1554 unsigned long t;
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1559 tpacpi_wwan_emulstate = !!t;
1561 return count;
1564 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1568 /* uwb_emulstate ------------------------------------------------- */
1569 static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1576 static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1580 unsigned long t;
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1585 tpacpi_uwb_emulstate = !!t;
1587 return count;
1590 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
1593 #endif
1595 /* --------------------------------------------------------------------- */
1597 static struct driver_attribute *tpacpi_driver_attributes[] = {
1598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1602 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1604 int i, res;
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1613 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1622 #endif
1624 return res;
1627 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1629 int i;
1631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1634 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1639 #endif
1642 /*************************************************************************
1643 * Firmware Data
1647 * Table of recommended minimum BIOS versions
1649 * Reasons for listing:
1650 * 1. Stable BIOS, listed because the unknown amount of
1651 * bugs and bad ACPI behaviour on older versions
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1655 * 3. BIOS with known reduced functionality in older versions
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
1667 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1674 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1675 __eid, __ev1, __ev2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
1678 .ec = __eid, \
1679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1682 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1685 /* Outdated IBM BIOSes often lack the EC id string */
1686 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
1694 /* Outdated IBM BIOSes often lack the EC id string */
1695 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
1704 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1707 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
1712 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
1718 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1797 #undef TPV_QL1
1798 #undef TPV_QL0
1799 #undef TPV_QI2
1800 #undef TPV_QI1
1801 #undef TPV_QI0
1802 #undef TPV_Q_X
1803 #undef TPV_Q
1805 static void __init tpacpi_check_outdated_fw(void)
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1813 if (!fwvers)
1814 return;
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1830 printk(TPACPI_WARN
1831 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1832 printk(TPACPI_WARN
1833 "WARNING: This firmware may be missing critical bug "
1834 "fixes and/or important features\n");
1838 static bool __init tpacpi_is_fw_known(void)
1840 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1841 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1844 /****************************************************************************
1845 ****************************************************************************
1847 * Subdrivers
1849 ****************************************************************************
1850 ****************************************************************************/
1852 /*************************************************************************
1853 * thinkpad-acpi metadata subdriver
1856 static int thinkpad_acpi_driver_read(struct seq_file *m)
1858 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1859 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1860 return 0;
1863 static struct ibm_struct thinkpad_acpi_driver_data = {
1864 .name = "driver",
1865 .read = thinkpad_acpi_driver_read,
1868 /*************************************************************************
1869 * Hotkey subdriver
1873 * ThinkPad firmware event model
1875 * The ThinkPad firmware has two main event interfaces: normal ACPI
1876 * notifications (which follow the ACPI standard), and a private event
1877 * interface.
1879 * The private event interface also issues events for the hotkeys. As
1880 * the driver gained features, the event handling code ended up being
1881 * built around the hotkey subdriver. This will need to be refactored
1882 * to a more formal event API eventually.
1884 * Some "hotkeys" are actually supposed to be used as event reports,
1885 * such as "brightness has changed", "volume has changed", depending on
1886 * the ThinkPad model and how the firmware is operating.
1888 * Unlike other classes, hotkey-class events have mask/unmask control on
1889 * non-ancient firmware. However, how it behaves changes a lot with the
1890 * firmware model and version.
1893 enum { /* hot key scan codes (derived from ACPI DSDT) */
1894 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1895 TP_ACPI_HOTKEYSCAN_FNF2,
1896 TP_ACPI_HOTKEYSCAN_FNF3,
1897 TP_ACPI_HOTKEYSCAN_FNF4,
1898 TP_ACPI_HOTKEYSCAN_FNF5,
1899 TP_ACPI_HOTKEYSCAN_FNF6,
1900 TP_ACPI_HOTKEYSCAN_FNF7,
1901 TP_ACPI_HOTKEYSCAN_FNF8,
1902 TP_ACPI_HOTKEYSCAN_FNF9,
1903 TP_ACPI_HOTKEYSCAN_FNF10,
1904 TP_ACPI_HOTKEYSCAN_FNF11,
1905 TP_ACPI_HOTKEYSCAN_FNF12,
1906 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1907 TP_ACPI_HOTKEYSCAN_FNINSERT,
1908 TP_ACPI_HOTKEYSCAN_FNDELETE,
1909 TP_ACPI_HOTKEYSCAN_FNHOME,
1910 TP_ACPI_HOTKEYSCAN_FNEND,
1911 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1912 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1913 TP_ACPI_HOTKEYSCAN_FNSPACE,
1914 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1915 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1916 TP_ACPI_HOTKEYSCAN_MUTE,
1917 TP_ACPI_HOTKEYSCAN_THINKPAD,
1918 TP_ACPI_HOTKEYSCAN_UNK1,
1919 TP_ACPI_HOTKEYSCAN_UNK2,
1920 TP_ACPI_HOTKEYSCAN_UNK3,
1921 TP_ACPI_HOTKEYSCAN_UNK4,
1922 TP_ACPI_HOTKEYSCAN_UNK5,
1923 TP_ACPI_HOTKEYSCAN_UNK6,
1924 TP_ACPI_HOTKEYSCAN_UNK7,
1925 TP_ACPI_HOTKEYSCAN_UNK8,
1927 /* Hotkey keymap size */
1928 TPACPI_HOTKEY_MAP_LEN
1931 enum { /* Keys/events available through NVRAM polling */
1932 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1933 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1936 enum { /* Positions of some of the keys in hotkey masks */
1937 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1938 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1939 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1940 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1941 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1942 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1943 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1944 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1945 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1946 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1947 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1950 enum { /* NVRAM to ACPI HKEY group map */
1951 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1952 TP_ACPI_HKEY_ZOOM_MASK |
1953 TP_ACPI_HKEY_DISPSWTCH_MASK |
1954 TP_ACPI_HKEY_HIBERNATE_MASK,
1955 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1956 TP_ACPI_HKEY_BRGHTDWN_MASK,
1957 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1958 TP_ACPI_HKEY_VOLDWN_MASK |
1959 TP_ACPI_HKEY_MUTE_MASK,
1962 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1963 struct tp_nvram_state {
1964 u16 thinkpad_toggle:1;
1965 u16 zoom_toggle:1;
1966 u16 display_toggle:1;
1967 u16 thinklight_toggle:1;
1968 u16 hibernate_toggle:1;
1969 u16 displayexp_toggle:1;
1970 u16 display_state:1;
1971 u16 brightness_toggle:1;
1972 u16 volume_toggle:1;
1973 u16 mute:1;
1975 u8 brightness_level;
1976 u8 volume_level;
1979 /* kthread for the hotkey poller */
1980 static struct task_struct *tpacpi_hotkey_task;
1982 /* Acquired while the poller kthread is running, use to sync start/stop */
1983 static struct mutex hotkey_thread_mutex;
1986 * Acquire mutex to write poller control variables as an
1987 * atomic block.
1989 * Increment hotkey_config_change when changing them if you
1990 * want the kthread to forget old state.
1992 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1994 static struct mutex hotkey_thread_data_mutex;
1995 static unsigned int hotkey_config_change;
1998 * hotkey poller control variables
2000 * Must be atomic or readers will also need to acquire mutex
2002 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2003 * should be used only when the changes need to be taken as
2004 * a block, OR when one needs to force the kthread to forget
2005 * old state.
2007 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2008 static unsigned int hotkey_poll_freq = 10; /* Hz */
2010 #define HOTKEY_CONFIG_CRITICAL_START \
2011 do { \
2012 mutex_lock(&hotkey_thread_data_mutex); \
2013 hotkey_config_change++; \
2014 } while (0);
2015 #define HOTKEY_CONFIG_CRITICAL_END \
2016 mutex_unlock(&hotkey_thread_data_mutex);
2018 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2020 #define hotkey_source_mask 0U
2021 #define HOTKEY_CONFIG_CRITICAL_START
2022 #define HOTKEY_CONFIG_CRITICAL_END
2024 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2026 static struct mutex hotkey_mutex;
2028 static enum { /* Reasons for waking up */
2029 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2030 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2031 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2032 } hotkey_wakeup_reason;
2034 static int hotkey_autosleep_ack;
2036 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2037 static u32 hotkey_all_mask; /* all events supported in fw */
2038 static u32 hotkey_reserved_mask; /* events better left disabled */
2039 static u32 hotkey_driver_mask; /* events needed by the driver */
2040 static u32 hotkey_user_mask; /* events visible to userspace */
2041 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2043 static unsigned int hotkey_report_mode;
2045 static u16 *hotkey_keycode_map;
2047 static struct attribute_set *hotkey_dev_attributes;
2049 static void tpacpi_driver_event(const unsigned int hkey_event);
2050 static void hotkey_driver_event(const unsigned int scancode);
2051 static void hotkey_poll_setup(const bool may_warn);
2053 /* HKEY.MHKG() return bits */
2054 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2056 static int hotkey_get_wlsw(void)
2058 int status;
2060 if (!tp_features.hotkey_wlsw)
2061 return -ENODEV;
2063 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2064 if (dbg_wlswemul)
2065 return (tpacpi_wlsw_emulstate) ?
2066 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2067 #endif
2069 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2070 return -EIO;
2072 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2075 static int hotkey_get_tablet_mode(int *status)
2077 int s;
2079 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2080 return -EIO;
2082 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2083 return 0;
2087 * Reads current event mask from firmware, and updates
2088 * hotkey_acpi_mask accordingly. Also resets any bits
2089 * from hotkey_user_mask that are unavailable to be
2090 * delivered (shadow requirement of the userspace ABI).
2092 * Call with hotkey_mutex held
2094 static int hotkey_mask_get(void)
2096 if (tp_features.hotkey_mask) {
2097 u32 m = 0;
2099 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2100 return -EIO;
2102 hotkey_acpi_mask = m;
2103 } else {
2104 /* no mask support doesn't mean no event support... */
2105 hotkey_acpi_mask = hotkey_all_mask;
2108 /* sync userspace-visible mask */
2109 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2111 return 0;
2114 void static hotkey_mask_warn_incomplete_mask(void)
2116 /* log only what the user can fix... */
2117 const u32 wantedmask = hotkey_driver_mask &
2118 ~(hotkey_acpi_mask | hotkey_source_mask) &
2119 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2121 if (wantedmask)
2122 printk(TPACPI_NOTICE
2123 "required events 0x%08x not enabled!\n",
2124 wantedmask);
2128 * Set the firmware mask when supported
2130 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2132 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2134 * Call with hotkey_mutex held
2136 static int hotkey_mask_set(u32 mask)
2138 int i;
2139 int rc = 0;
2141 const u32 fwmask = mask & ~hotkey_source_mask;
2143 if (tp_features.hotkey_mask) {
2144 for (i = 0; i < 32; i++) {
2145 if (!acpi_evalf(hkey_handle,
2146 NULL, "MHKM", "vdd", i + 1,
2147 !!(mask & (1 << i)))) {
2148 rc = -EIO;
2149 break;
2155 * We *must* make an inconditional call to hotkey_mask_get to
2156 * refresh hotkey_acpi_mask and update hotkey_user_mask
2158 * Take the opportunity to also log when we cannot _enable_
2159 * a given event.
2161 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2162 printk(TPACPI_NOTICE
2163 "asked for hotkey mask 0x%08x, but "
2164 "firmware forced it to 0x%08x\n",
2165 fwmask, hotkey_acpi_mask);
2168 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2169 hotkey_mask_warn_incomplete_mask();
2171 return rc;
2175 * Sets hotkey_user_mask and tries to set the firmware mask
2177 * Call with hotkey_mutex held
2179 static int hotkey_user_mask_set(const u32 mask)
2181 int rc;
2183 /* Give people a chance to notice they are doing something that
2184 * is bound to go boom on their users sooner or later */
2185 if (!tp_warned.hotkey_mask_ff &&
2186 (mask == 0xffff || mask == 0xffffff ||
2187 mask == 0xffffffff)) {
2188 tp_warned.hotkey_mask_ff = 1;
2189 printk(TPACPI_NOTICE
2190 "setting the hotkey mask to 0x%08x is likely "
2191 "not the best way to go about it\n", mask);
2192 printk(TPACPI_NOTICE
2193 "please consider using the driver defaults, "
2194 "and refer to up-to-date thinkpad-acpi "
2195 "documentation\n");
2198 /* Try to enable what the user asked for, plus whatever we need.
2199 * this syncs everything but won't enable bits in hotkey_user_mask */
2200 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2202 /* Enable the available bits in hotkey_user_mask */
2203 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2205 return rc;
2209 * Sets the driver hotkey mask.
2211 * Can be called even if the hotkey subdriver is inactive
2213 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2215 int rc;
2217 /* Do the right thing if hotkey_init has not been called yet */
2218 if (!tp_features.hotkey) {
2219 hotkey_driver_mask = mask;
2220 return 0;
2223 mutex_lock(&hotkey_mutex);
2225 HOTKEY_CONFIG_CRITICAL_START
2226 hotkey_driver_mask = mask;
2227 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2228 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2229 #endif
2230 HOTKEY_CONFIG_CRITICAL_END
2232 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2233 ~hotkey_source_mask);
2234 hotkey_poll_setup(true);
2236 mutex_unlock(&hotkey_mutex);
2238 return rc;
2241 static int hotkey_status_get(int *status)
2243 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2244 return -EIO;
2246 return 0;
2249 static int hotkey_status_set(bool enable)
2251 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2252 return -EIO;
2254 return 0;
2257 static void tpacpi_input_send_tabletsw(void)
2259 int state;
2261 if (tp_features.hotkey_tablet &&
2262 !hotkey_get_tablet_mode(&state)) {
2263 mutex_lock(&tpacpi_inputdev_send_mutex);
2265 input_report_switch(tpacpi_inputdev,
2266 SW_TABLET_MODE, !!state);
2267 input_sync(tpacpi_inputdev);
2269 mutex_unlock(&tpacpi_inputdev_send_mutex);
2273 /* Do NOT call without validating scancode first */
2274 static void tpacpi_input_send_key(const unsigned int scancode)
2276 const unsigned int keycode = hotkey_keycode_map[scancode];
2278 if (keycode != KEY_RESERVED) {
2279 mutex_lock(&tpacpi_inputdev_send_mutex);
2281 input_report_key(tpacpi_inputdev, keycode, 1);
2282 if (keycode == KEY_UNKNOWN)
2283 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2284 scancode);
2285 input_sync(tpacpi_inputdev);
2287 input_report_key(tpacpi_inputdev, keycode, 0);
2288 if (keycode == KEY_UNKNOWN)
2289 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2290 scancode);
2291 input_sync(tpacpi_inputdev);
2293 mutex_unlock(&tpacpi_inputdev_send_mutex);
2297 /* Do NOT call without validating scancode first */
2298 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2300 hotkey_driver_event(scancode);
2301 if (hotkey_user_mask & (1 << scancode))
2302 tpacpi_input_send_key(scancode);
2305 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2306 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2308 /* Do NOT call without validating scancode first */
2309 static void tpacpi_hotkey_send_key(unsigned int scancode)
2311 tpacpi_input_send_key_masked(scancode);
2312 if (hotkey_report_mode < 2) {
2313 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2314 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2318 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2320 u8 d;
2322 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2323 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2324 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2325 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2326 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2327 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2329 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2331 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2333 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2334 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2335 n->displayexp_toggle =
2336 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2338 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2339 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2340 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2341 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2342 n->brightness_toggle =
2343 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2345 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2346 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2347 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2348 >> TP_NVRAM_POS_LEVEL_VOLUME;
2349 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2350 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2354 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2355 struct tp_nvram_state *newn,
2356 const u32 event_mask)
2359 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2360 do { \
2361 if ((event_mask & (1 << __scancode)) && \
2362 oldn->__member != newn->__member) \
2363 tpacpi_hotkey_send_key(__scancode); \
2364 } while (0)
2366 #define TPACPI_MAY_SEND_KEY(__scancode) \
2367 do { \
2368 if (event_mask & (1 << __scancode)) \
2369 tpacpi_hotkey_send_key(__scancode); \
2370 } while (0)
2372 void issue_volchange(const unsigned int oldvol,
2373 const unsigned int newvol)
2375 unsigned int i = oldvol;
2377 while (i > newvol) {
2378 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2379 i--;
2381 while (i < newvol) {
2382 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2383 i++;
2387 void issue_brightnesschange(const unsigned int oldbrt,
2388 const unsigned int newbrt)
2390 unsigned int i = oldbrt;
2392 while (i > newbrt) {
2393 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2394 i--;
2396 while (i < newbrt) {
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2398 i++;
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2412 * Handle volume
2414 * This code is supposed to duplicate the IBM firmware behaviour:
2415 * - Pressing MUTE issues mute hotkey message, even when already mute
2416 * - Pressing Volume up/down issues volume up/down hotkey messages,
2417 * even when already at maximum or minumum volume
2418 * - The act of unmuting issues volume up/down notification,
2419 * depending which key was used to unmute
2421 * We are constrained to what the NVRAM can tell us, which is not much
2422 * and certainly not enough if more than one volume hotkey was pressed
2423 * since the last poll cycle.
2425 * Just to make our life interesting, some newer Lenovo ThinkPads have
2426 * bugs in the BIOS and may fail to update volume_toggle properly.
2428 if (newn->mute) {
2429 /* muted */
2430 if (!oldn->mute ||
2431 oldn->volume_toggle != newn->volume_toggle ||
2432 oldn->volume_level != newn->volume_level) {
2433 /* recently muted, or repeated mute keypress, or
2434 * multiple presses ending in mute */
2435 issue_volchange(oldn->volume_level, newn->volume_level);
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2438 } else {
2439 /* unmute */
2440 if (oldn->mute) {
2441 /* recently unmuted, issue 'unmute' keypress */
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2444 if (oldn->volume_level != newn->volume_level) {
2445 issue_volchange(oldn->volume_level, newn->volume_level);
2446 } else if (oldn->volume_toggle != newn->volume_toggle) {
2447 /* repeated vol up/down keypress at end of scale ? */
2448 if (newn->volume_level == 0)
2449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2450 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2455 /* handle brightness */
2456 if (oldn->brightness_level != newn->brightness_level) {
2457 issue_brightnesschange(oldn->brightness_level,
2458 newn->brightness_level);
2459 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2460 /* repeated key presses that didn't change state */
2461 if (newn->brightness_level == 0)
2462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2463 else if (newn->brightness_level >= bright_maxlvl
2464 && !tp_features.bright_unkfw)
2465 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2468 #undef TPACPI_COMPARE_KEY
2469 #undef TPACPI_MAY_SEND_KEY
2473 * Polling driver
2475 * We track all events in hotkey_source_mask all the time, since
2476 * most of them are edge-based. We only issue those requested by
2477 * hotkey_user_mask or hotkey_driver_mask, though.
2479 static int hotkey_kthread(void *data)
2481 struct tp_nvram_state s[2];
2482 u32 poll_mask, event_mask;
2483 unsigned int si, so;
2484 unsigned long t;
2485 unsigned int change_detector, must_reset;
2486 unsigned int poll_freq;
2488 mutex_lock(&hotkey_thread_mutex);
2490 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2491 goto exit;
2493 set_freezable();
2495 so = 0;
2496 si = 1;
2497 t = 0;
2499 /* Initial state for compares */
2500 mutex_lock(&hotkey_thread_data_mutex);
2501 change_detector = hotkey_config_change;
2502 poll_mask = hotkey_source_mask;
2503 event_mask = hotkey_source_mask &
2504 (hotkey_driver_mask | hotkey_user_mask);
2505 poll_freq = hotkey_poll_freq;
2506 mutex_unlock(&hotkey_thread_data_mutex);
2507 hotkey_read_nvram(&s[so], poll_mask);
2509 while (!kthread_should_stop()) {
2510 if (t == 0) {
2511 if (likely(poll_freq))
2512 t = 1000/poll_freq;
2513 else
2514 t = 100; /* should never happen... */
2516 t = msleep_interruptible(t);
2517 if (unlikely(kthread_should_stop()))
2518 break;
2519 must_reset = try_to_freeze();
2520 if (t > 0 && !must_reset)
2521 continue;
2523 mutex_lock(&hotkey_thread_data_mutex);
2524 if (must_reset || hotkey_config_change != change_detector) {
2525 /* forget old state on thaw or config change */
2526 si = so;
2527 t = 0;
2528 change_detector = hotkey_config_change;
2530 poll_mask = hotkey_source_mask;
2531 event_mask = hotkey_source_mask &
2532 (hotkey_driver_mask | hotkey_user_mask);
2533 poll_freq = hotkey_poll_freq;
2534 mutex_unlock(&hotkey_thread_data_mutex);
2536 if (likely(poll_mask)) {
2537 hotkey_read_nvram(&s[si], poll_mask);
2538 if (likely(si != so)) {
2539 hotkey_compare_and_issue_event(&s[so], &s[si],
2540 event_mask);
2544 so = si;
2545 si ^= 1;
2548 exit:
2549 mutex_unlock(&hotkey_thread_mutex);
2550 return 0;
2553 /* call with hotkey_mutex held */
2554 static void hotkey_poll_stop_sync(void)
2556 if (tpacpi_hotkey_task) {
2557 if (frozen(tpacpi_hotkey_task) ||
2558 freezing(tpacpi_hotkey_task))
2559 thaw_process(tpacpi_hotkey_task);
2561 kthread_stop(tpacpi_hotkey_task);
2562 tpacpi_hotkey_task = NULL;
2563 mutex_lock(&hotkey_thread_mutex);
2564 /* at this point, the thread did exit */
2565 mutex_unlock(&hotkey_thread_mutex);
2569 /* call with hotkey_mutex held */
2570 static void hotkey_poll_setup(const bool may_warn)
2572 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2573 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2575 if (hotkey_poll_freq > 0 &&
2576 (poll_driver_mask ||
2577 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2578 if (!tpacpi_hotkey_task) {
2579 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2580 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2581 if (IS_ERR(tpacpi_hotkey_task)) {
2582 tpacpi_hotkey_task = NULL;
2583 printk(TPACPI_ERR
2584 "could not create kernel thread "
2585 "for hotkey polling\n");
2588 } else {
2589 hotkey_poll_stop_sync();
2590 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2591 hotkey_poll_freq == 0) {
2592 printk(TPACPI_NOTICE
2593 "hot keys 0x%08x and/or events 0x%08x "
2594 "require polling, which is currently "
2595 "disabled\n",
2596 poll_user_mask, poll_driver_mask);
2601 static void hotkey_poll_setup_safe(const bool may_warn)
2603 mutex_lock(&hotkey_mutex);
2604 hotkey_poll_setup(may_warn);
2605 mutex_unlock(&hotkey_mutex);
2608 /* call with hotkey_mutex held */
2609 static void hotkey_poll_set_freq(unsigned int freq)
2611 if (!freq)
2612 hotkey_poll_stop_sync();
2614 hotkey_poll_freq = freq;
2617 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2619 static void hotkey_poll_setup(const bool __unused)
2623 static void hotkey_poll_setup_safe(const bool __unused)
2627 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2629 static int hotkey_inputdev_open(struct input_dev *dev)
2631 switch (tpacpi_lifecycle) {
2632 case TPACPI_LIFE_INIT:
2633 case TPACPI_LIFE_RUNNING:
2634 hotkey_poll_setup_safe(false);
2635 return 0;
2636 case TPACPI_LIFE_EXITING:
2637 return -EBUSY;
2640 /* Should only happen if tpacpi_lifecycle is corrupt */
2641 BUG();
2642 return -EBUSY;
2645 static void hotkey_inputdev_close(struct input_dev *dev)
2647 /* disable hotkey polling when possible */
2648 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2649 !(hotkey_source_mask & hotkey_driver_mask))
2650 hotkey_poll_setup_safe(false);
2653 /* sysfs hotkey enable ------------------------------------------------- */
2654 static ssize_t hotkey_enable_show(struct device *dev,
2655 struct device_attribute *attr,
2656 char *buf)
2658 int res, status;
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkey reporting is always enabled");
2663 res = hotkey_status_get(&status);
2664 if (res)
2665 return res;
2667 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2670 static ssize_t hotkey_enable_store(struct device *dev,
2671 struct device_attribute *attr,
2672 const char *buf, size_t count)
2674 unsigned long t;
2676 printk_deprecated_attribute("hotkey_enable",
2677 "Hotkeys can be disabled through hotkey_mask");
2679 if (parse_strtoul(buf, 1, &t))
2680 return -EINVAL;
2682 if (t == 0)
2683 return -EPERM;
2685 return count;
2688 static struct device_attribute dev_attr_hotkey_enable =
2689 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2690 hotkey_enable_show, hotkey_enable_store);
2692 /* sysfs hotkey mask --------------------------------------------------- */
2693 static ssize_t hotkey_mask_show(struct device *dev,
2694 struct device_attribute *attr,
2695 char *buf)
2697 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2700 static ssize_t hotkey_mask_store(struct device *dev,
2701 struct device_attribute *attr,
2702 const char *buf, size_t count)
2704 unsigned long t;
2705 int res;
2707 if (parse_strtoul(buf, 0xffffffffUL, &t))
2708 return -EINVAL;
2710 if (mutex_lock_killable(&hotkey_mutex))
2711 return -ERESTARTSYS;
2713 res = hotkey_user_mask_set(t);
2715 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2716 hotkey_poll_setup(true);
2717 #endif
2719 mutex_unlock(&hotkey_mutex);
2721 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2723 return (res) ? res : count;
2726 static struct device_attribute dev_attr_hotkey_mask =
2727 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2728 hotkey_mask_show, hotkey_mask_store);
2730 /* sysfs hotkey bios_enabled ------------------------------------------- */
2731 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2732 struct device_attribute *attr,
2733 char *buf)
2735 return sprintf(buf, "0\n");
2738 static struct device_attribute dev_attr_hotkey_bios_enabled =
2739 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2741 /* sysfs hotkey bios_mask ---------------------------------------------- */
2742 static ssize_t hotkey_bios_mask_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2746 printk_deprecated_attribute("hotkey_bios_mask",
2747 "This attribute is useless.");
2748 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2751 static struct device_attribute dev_attr_hotkey_bios_mask =
2752 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2754 /* sysfs hotkey all_mask ----------------------------------------------- */
2755 static ssize_t hotkey_all_mask_show(struct device *dev,
2756 struct device_attribute *attr,
2757 char *buf)
2759 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2760 hotkey_all_mask | hotkey_source_mask);
2763 static struct device_attribute dev_attr_hotkey_all_mask =
2764 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2766 /* sysfs hotkey recommended_mask --------------------------------------- */
2767 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2772 (hotkey_all_mask | hotkey_source_mask)
2773 & ~hotkey_reserved_mask);
2776 static struct device_attribute dev_attr_hotkey_recommended_mask =
2777 __ATTR(hotkey_recommended_mask, S_IRUGO,
2778 hotkey_recommended_mask_show, NULL);
2780 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2782 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2783 static ssize_t hotkey_source_mask_show(struct device *dev,
2784 struct device_attribute *attr,
2785 char *buf)
2787 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2790 static ssize_t hotkey_source_mask_store(struct device *dev,
2791 struct device_attribute *attr,
2792 const char *buf, size_t count)
2794 unsigned long t;
2795 u32 r_ev;
2796 int rc;
2798 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2799 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2800 return -EINVAL;
2802 if (mutex_lock_killable(&hotkey_mutex))
2803 return -ERESTARTSYS;
2805 HOTKEY_CONFIG_CRITICAL_START
2806 hotkey_source_mask = t;
2807 HOTKEY_CONFIG_CRITICAL_END
2809 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2810 ~hotkey_source_mask);
2811 hotkey_poll_setup(true);
2813 /* check if events needed by the driver got disabled */
2814 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2815 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2817 mutex_unlock(&hotkey_mutex);
2819 if (rc < 0)
2820 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2821 "firmware event mask!\n");
2823 if (r_ev)
2824 printk(TPACPI_NOTICE "hotkey_source_mask: "
2825 "some important events were disabled: "
2826 "0x%04x\n", r_ev);
2828 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2830 return (rc < 0) ? rc : count;
2833 static struct device_attribute dev_attr_hotkey_source_mask =
2834 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2835 hotkey_source_mask_show, hotkey_source_mask_store);
2837 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2838 static ssize_t hotkey_poll_freq_show(struct device *dev,
2839 struct device_attribute *attr,
2840 char *buf)
2842 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2845 static ssize_t hotkey_poll_freq_store(struct device *dev,
2846 struct device_attribute *attr,
2847 const char *buf, size_t count)
2849 unsigned long t;
2851 if (parse_strtoul(buf, 25, &t))
2852 return -EINVAL;
2854 if (mutex_lock_killable(&hotkey_mutex))
2855 return -ERESTARTSYS;
2857 hotkey_poll_set_freq(t);
2858 hotkey_poll_setup(true);
2860 mutex_unlock(&hotkey_mutex);
2862 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2864 return count;
2867 static struct device_attribute dev_attr_hotkey_poll_freq =
2868 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2869 hotkey_poll_freq_show, hotkey_poll_freq_store);
2871 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2873 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2874 static ssize_t hotkey_radio_sw_show(struct device *dev,
2875 struct device_attribute *attr,
2876 char *buf)
2878 int res;
2879 res = hotkey_get_wlsw();
2880 if (res < 0)
2881 return res;
2883 /* Opportunistic update */
2884 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2886 return snprintf(buf, PAGE_SIZE, "%d\n",
2887 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2890 static struct device_attribute dev_attr_hotkey_radio_sw =
2891 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2893 static void hotkey_radio_sw_notify_change(void)
2895 if (tp_features.hotkey_wlsw)
2896 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2897 "hotkey_radio_sw");
2900 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2901 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2902 struct device_attribute *attr,
2903 char *buf)
2905 int res, s;
2906 res = hotkey_get_tablet_mode(&s);
2907 if (res < 0)
2908 return res;
2910 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2913 static struct device_attribute dev_attr_hotkey_tablet_mode =
2914 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2916 static void hotkey_tablet_mode_notify_change(void)
2918 if (tp_features.hotkey_tablet)
2919 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2920 "hotkey_tablet_mode");
2923 /* sysfs hotkey report_mode -------------------------------------------- */
2924 static ssize_t hotkey_report_mode_show(struct device *dev,
2925 struct device_attribute *attr,
2926 char *buf)
2928 return snprintf(buf, PAGE_SIZE, "%d\n",
2929 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2932 static struct device_attribute dev_attr_hotkey_report_mode =
2933 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2935 /* sysfs wakeup reason (pollable) -------------------------------------- */
2936 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2937 struct device_attribute *attr,
2938 char *buf)
2940 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2943 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2944 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2946 static void hotkey_wakeup_reason_notify_change(void)
2948 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2949 "wakeup_reason");
2952 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2953 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2954 struct device_attribute *attr,
2955 char *buf)
2957 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2960 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2961 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2962 hotkey_wakeup_hotunplug_complete_show, NULL);
2964 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2966 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2967 "wakeup_hotunplug_complete");
2970 /* --------------------------------------------------------------------- */
2972 static struct attribute *hotkey_attributes[] __initdata = {
2973 &dev_attr_hotkey_enable.attr,
2974 &dev_attr_hotkey_bios_enabled.attr,
2975 &dev_attr_hotkey_bios_mask.attr,
2976 &dev_attr_hotkey_report_mode.attr,
2977 &dev_attr_hotkey_wakeup_reason.attr,
2978 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2979 &dev_attr_hotkey_mask.attr,
2980 &dev_attr_hotkey_all_mask.attr,
2981 &dev_attr_hotkey_recommended_mask.attr,
2982 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2983 &dev_attr_hotkey_source_mask.attr,
2984 &dev_attr_hotkey_poll_freq.attr,
2985 #endif
2989 * Sync both the hw and sw blocking state of all switches
2991 static void tpacpi_send_radiosw_update(void)
2993 int wlsw;
2996 * We must sync all rfkill controllers *before* issuing any
2997 * rfkill input events, or we will race the rfkill core input
2998 * handler.
3000 * tpacpi_inputdev_send_mutex works as a syncronization point
3001 * for the above.
3003 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3006 wlsw = hotkey_get_wlsw();
3008 /* Sync hw blocking state first if it is hw-blocked */
3009 if (wlsw == TPACPI_RFK_RADIO_OFF)
3010 tpacpi_rfk_update_hwblock_state(true);
3012 /* Sync sw blocking state */
3013 tpacpi_rfk_update_swstate_all();
3015 /* Sync hw blocking state last if it is hw-unblocked */
3016 if (wlsw == TPACPI_RFK_RADIO_ON)
3017 tpacpi_rfk_update_hwblock_state(false);
3019 /* Issue rfkill input event for WLSW switch */
3020 if (!(wlsw < 0)) {
3021 mutex_lock(&tpacpi_inputdev_send_mutex);
3023 input_report_switch(tpacpi_inputdev,
3024 SW_RFKILL_ALL, (wlsw > 0));
3025 input_sync(tpacpi_inputdev);
3027 mutex_unlock(&tpacpi_inputdev_send_mutex);
3031 * this can be unconditional, as we will poll state again
3032 * if userspace uses the notify to read data
3034 hotkey_radio_sw_notify_change();
3037 static void hotkey_exit(void)
3039 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3040 mutex_lock(&hotkey_mutex);
3041 hotkey_poll_stop_sync();
3042 mutex_unlock(&hotkey_mutex);
3043 #endif
3045 if (hotkey_dev_attributes)
3046 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3048 kfree(hotkey_keycode_map);
3050 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3051 "restoring original HKEY status and mask\n");
3052 /* yes, there is a bitwise or below, we want the
3053 * functions to be called even if one of them fail */
3054 if (((tp_features.hotkey_mask &&
3055 hotkey_mask_set(hotkey_orig_mask)) |
3056 hotkey_status_set(false)) != 0)
3057 printk(TPACPI_ERR
3058 "failed to restore hot key mask "
3059 "to BIOS defaults\n");
3062 static void __init hotkey_unmap(const unsigned int scancode)
3064 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3065 clear_bit(hotkey_keycode_map[scancode],
3066 tpacpi_inputdev->keybit);
3067 hotkey_keycode_map[scancode] = KEY_RESERVED;
3072 * HKEY quirks:
3073 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3076 #define TPACPI_HK_Q_INIMASK 0x0001
3078 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3079 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3080 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3081 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3082 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3083 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3084 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3085 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3086 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3087 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3088 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3089 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3090 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3091 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3092 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3093 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3094 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3095 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3096 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3097 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3100 typedef u16 tpacpi_keymap_entry_t;
3101 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3103 static int __init hotkey_init(struct ibm_init_struct *iibm)
3105 /* Requirements for changing the default keymaps:
3107 * 1. Many of the keys are mapped to KEY_RESERVED for very
3108 * good reasons. Do not change them unless you have deep
3109 * knowledge on the IBM and Lenovo ThinkPad firmware for
3110 * the various ThinkPad models. The driver behaves
3111 * differently for KEY_RESERVED: such keys have their
3112 * hot key mask *unset* in mask_recommended, and also
3113 * in the initial hot key mask programmed into the
3114 * firmware at driver load time, which means the firm-
3115 * ware may react very differently if you change them to
3116 * something else;
3118 * 2. You must be subscribed to the linux-thinkpad and
3119 * ibm-acpi-devel mailing lists, and you should read the
3120 * list archives since 2007 if you want to change the
3121 * keymaps. This requirement exists so that you will
3122 * know the past history of problems with the thinkpad-
3123 * acpi driver keymaps, and also that you will be
3124 * listening to any bug reports;
3126 * 3. Do not send thinkpad-acpi specific patches directly to
3127 * for merging, *ever*. Send them to the linux-acpi
3128 * mailinglist for comments. Merging is to be done only
3129 * through acpi-test and the ACPI maintainer.
3131 * If the above is too much to ask, don't change the keymap.
3132 * Ask the thinkpad-acpi maintainer to do it, instead.
3135 enum keymap_index {
3136 TPACPI_KEYMAP_IBM_GENERIC = 0,
3137 TPACPI_KEYMAP_LENOVO_GENERIC,
3140 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3141 /* Generic keymap for IBM ThinkPads */
3142 [TPACPI_KEYMAP_IBM_GENERIC] = {
3143 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3144 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3145 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3146 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3148 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3149 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3150 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3151 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3153 /* brightness: firmware always reacts to them */
3154 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3155 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3157 /* Thinklight: firmware always react to it */
3158 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3160 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3161 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3163 /* Volume: firmware always react to it and reprograms
3164 * the built-in *extra* mixer. Never map it to control
3165 * another mixer by default. */
3166 KEY_RESERVED, /* 0x14: VOLUME UP */
3167 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3168 KEY_RESERVED, /* 0x16: MUTE */
3170 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3172 /* (assignments unknown, please report if found) */
3173 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3174 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3177 /* Generic keymap for Lenovo ThinkPads */
3178 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3179 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3180 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3181 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3182 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3184 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3185 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3186 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3187 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3189 /* These should be enabled --only-- when ACPI video
3190 * is disabled (i.e. in "vendor" mode), and are handled
3191 * in a special way by the init code */
3192 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3193 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3195 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3197 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3198 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3200 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3201 * react to it and reprograms the built-in *extra* mixer.
3202 * Never map it to control another mixer by default.
3204 * T60?, T61, R60?, R61: firmware and EC tries to send
3205 * these over the regular keyboard, so these are no-ops,
3206 * but there are still weird bugs re. MUTE, so do not
3207 * change unless you get test reports from all Lenovo
3208 * models. May cause the BIOS to interfere with the
3209 * HDA mixer.
3211 KEY_RESERVED, /* 0x14: VOLUME UP */
3212 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3213 KEY_RESERVED, /* 0x16: MUTE */
3215 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3217 /* (assignments unknown, please report if found) */
3218 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3219 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3223 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3224 /* Generic maps (fallback) */
3226 .vendor = PCI_VENDOR_ID_IBM,
3227 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3228 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3231 .vendor = PCI_VENDOR_ID_LENOVO,
3232 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3233 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3237 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3238 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3240 int res, i;
3241 int status;
3242 int hkeyv;
3243 bool radiosw_state = false;
3244 bool tabletsw_state = false;
3246 unsigned long quirks;
3247 unsigned long keymap_id;
3249 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3250 "initializing hotkey subdriver\n");
3252 BUG_ON(!tpacpi_inputdev);
3253 BUG_ON(tpacpi_inputdev->open != NULL ||
3254 tpacpi_inputdev->close != NULL);
3256 TPACPI_ACPIHANDLE_INIT(hkey);
3257 mutex_init(&hotkey_mutex);
3259 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3260 mutex_init(&hotkey_thread_mutex);
3261 mutex_init(&hotkey_thread_data_mutex);
3262 #endif
3264 /* hotkey not supported on 570 */
3265 tp_features.hotkey = hkey_handle != NULL;
3267 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3268 "hotkeys are %s\n",
3269 str_supported(tp_features.hotkey));
3271 if (!tp_features.hotkey)
3272 return 1;
3274 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3275 ARRAY_SIZE(tpacpi_hotkey_qtable));
3277 tpacpi_disable_brightness_delay();
3279 /* MUST have enough space for all attributes to be added to
3280 * hotkey_dev_attributes */
3281 hotkey_dev_attributes = create_attr_set(
3282 ARRAY_SIZE(hotkey_attributes) + 2,
3283 NULL);
3284 if (!hotkey_dev_attributes)
3285 return -ENOMEM;
3286 res = add_many_to_attr_set(hotkey_dev_attributes,
3287 hotkey_attributes,
3288 ARRAY_SIZE(hotkey_attributes));
3289 if (res)
3290 goto err_exit;
3292 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3293 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3294 for HKEY interface version 0x100 */
3295 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3296 if ((hkeyv >> 8) != 1) {
3297 printk(TPACPI_ERR "unknown version of the "
3298 "HKEY interface: 0x%x\n", hkeyv);
3299 printk(TPACPI_ERR "please report this to %s\n",
3300 TPACPI_MAIL);
3301 } else {
3303 * MHKV 0x100 in A31, R40, R40e,
3304 * T4x, X31, and later
3306 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3307 "firmware HKEY interface version: 0x%x\n",
3308 hkeyv);
3310 /* Paranoia check AND init hotkey_all_mask */
3311 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3312 "MHKA", "qd")) {
3313 printk(TPACPI_ERR
3314 "missing MHKA handler, "
3315 "please report this to %s\n",
3316 TPACPI_MAIL);
3317 /* Fallback: pre-init for FN+F3,F4,F12 */
3318 hotkey_all_mask = 0x080cU;
3319 } else {
3320 tp_features.hotkey_mask = 1;
3325 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3326 "hotkey masks are %s\n",
3327 str_supported(tp_features.hotkey_mask));
3329 /* Init hotkey_all_mask if not initialized yet */
3330 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3331 (quirks & TPACPI_HK_Q_INIMASK))
3332 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3334 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3335 if (tp_features.hotkey_mask) {
3336 /* hotkey_source_mask *must* be zero for
3337 * the first hotkey_mask_get to return hotkey_orig_mask */
3338 res = hotkey_mask_get();
3339 if (res)
3340 goto err_exit;
3342 hotkey_orig_mask = hotkey_acpi_mask;
3343 } else {
3344 hotkey_orig_mask = hotkey_all_mask;
3345 hotkey_acpi_mask = hotkey_all_mask;
3348 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3349 if (dbg_wlswemul) {
3350 tp_features.hotkey_wlsw = 1;
3351 radiosw_state = !!tpacpi_wlsw_emulstate;
3352 printk(TPACPI_INFO
3353 "radio switch emulation enabled\n");
3354 } else
3355 #endif
3356 /* Not all thinkpads have a hardware radio switch */
3357 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3358 tp_features.hotkey_wlsw = 1;
3359 radiosw_state = !!status;
3360 printk(TPACPI_INFO
3361 "radio switch found; radios are %s\n",
3362 enabled(status, 0));
3364 if (tp_features.hotkey_wlsw)
3365 res = add_to_attr_set(hotkey_dev_attributes,
3366 &dev_attr_hotkey_radio_sw.attr);
3368 /* For X41t, X60t, X61t Tablets... */
3369 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3370 tp_features.hotkey_tablet = 1;
3371 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3372 printk(TPACPI_INFO
3373 "possible tablet mode switch found; "
3374 "ThinkPad in %s mode\n",
3375 (tabletsw_state) ? "tablet" : "laptop");
3376 res = add_to_attr_set(hotkey_dev_attributes,
3377 &dev_attr_hotkey_tablet_mode.attr);
3380 if (!res)
3381 res = register_attr_set_with_sysfs(
3382 hotkey_dev_attributes,
3383 &tpacpi_pdev->dev.kobj);
3384 if (res)
3385 goto err_exit;
3387 /* Set up key map */
3388 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3389 GFP_KERNEL);
3390 if (!hotkey_keycode_map) {
3391 printk(TPACPI_ERR
3392 "failed to allocate memory for key map\n");
3393 res = -ENOMEM;
3394 goto err_exit;
3397 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3398 ARRAY_SIZE(tpacpi_keymap_qtable));
3399 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3400 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3401 "using keymap number %lu\n", keymap_id);
3403 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3404 TPACPI_HOTKEY_MAP_SIZE);
3406 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3407 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3408 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3409 tpacpi_inputdev->keycode = hotkey_keycode_map;
3410 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3411 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3412 input_set_capability(tpacpi_inputdev, EV_KEY,
3413 hotkey_keycode_map[i]);
3414 } else {
3415 if (i < sizeof(hotkey_reserved_mask)*8)
3416 hotkey_reserved_mask |= 1 << i;
3420 if (tp_features.hotkey_wlsw) {
3421 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3422 input_report_switch(tpacpi_inputdev,
3423 SW_RFKILL_ALL, radiosw_state);
3425 if (tp_features.hotkey_tablet) {
3426 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3427 input_report_switch(tpacpi_inputdev,
3428 SW_TABLET_MODE, tabletsw_state);
3431 /* Do not issue duplicate brightness change events to
3432 * userspace. tpacpi_detect_brightness_capabilities() must have
3433 * been called before this point */
3434 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3435 printk(TPACPI_INFO
3436 "This ThinkPad has standard ACPI backlight "
3437 "brightness control, supported by the ACPI "
3438 "video driver\n");
3439 printk(TPACPI_NOTICE
3440 "Disabling thinkpad-acpi brightness events "
3441 "by default...\n");
3443 /* Disable brightness up/down on Lenovo thinkpads when
3444 * ACPI is handling them, otherwise it is plain impossible
3445 * for userspace to do something even remotely sane */
3446 hotkey_reserved_mask |=
3447 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3448 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3449 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3450 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3453 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3454 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3455 & ~hotkey_all_mask
3456 & ~hotkey_reserved_mask;
3458 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3459 "hotkey source mask 0x%08x, polling freq %u\n",
3460 hotkey_source_mask, hotkey_poll_freq);
3461 #endif
3463 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3464 "enabling firmware HKEY event interface...\n");
3465 res = hotkey_status_set(true);
3466 if (res) {
3467 hotkey_exit();
3468 return res;
3470 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3471 | hotkey_driver_mask)
3472 & ~hotkey_source_mask);
3473 if (res < 0 && res != -ENXIO) {
3474 hotkey_exit();
3475 return res;
3477 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3478 & ~hotkey_reserved_mask;
3479 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3480 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3481 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3483 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3484 "legacy ibm/hotkey event reporting over procfs %s\n",
3485 (hotkey_report_mode < 2) ?
3486 "enabled" : "disabled");
3488 tpacpi_inputdev->open = &hotkey_inputdev_open;
3489 tpacpi_inputdev->close = &hotkey_inputdev_close;
3491 hotkey_poll_setup_safe(true);
3493 return 0;
3495 err_exit:
3496 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3497 hotkey_dev_attributes = NULL;
3499 return (res < 0)? res : 1;
3502 static bool hotkey_notify_hotkey(const u32 hkey,
3503 bool *send_acpi_ev,
3504 bool *ignore_acpi_ev)
3506 /* 0x1000-0x1FFF: key presses */
3507 unsigned int scancode = hkey & 0xfff;
3508 *send_acpi_ev = true;
3509 *ignore_acpi_ev = false;
3511 /* HKEY event 0x1001 is scancode 0x00 */
3512 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3513 scancode--;
3514 if (!(hotkey_source_mask & (1 << scancode))) {
3515 tpacpi_input_send_key_masked(scancode);
3516 *send_acpi_ev = false;
3517 } else {
3518 *ignore_acpi_ev = true;
3520 return true;
3522 return false;
3525 static bool hotkey_notify_wakeup(const u32 hkey,
3526 bool *send_acpi_ev,
3527 bool *ignore_acpi_ev)
3529 /* 0x2000-0x2FFF: Wakeup reason */
3530 *send_acpi_ev = true;
3531 *ignore_acpi_ev = false;
3533 switch (hkey) {
3534 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3535 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3536 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3537 *ignore_acpi_ev = true;
3538 break;
3540 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3541 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3542 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3543 *ignore_acpi_ev = true;
3544 break;
3546 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3547 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3548 printk(TPACPI_ALERT
3549 "EMERGENCY WAKEUP: battery almost empty\n");
3550 /* how to auto-heal: */
3551 /* 2313: woke up from S3, go to S4/S5 */
3552 /* 2413: woke up from S4, go to S5 */
3553 break;
3555 default:
3556 return false;
3559 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3560 printk(TPACPI_INFO
3561 "woke up due to a hot-unplug "
3562 "request...\n");
3563 hotkey_wakeup_reason_notify_change();
3565 return true;
3568 static bool hotkey_notify_usrevent(const u32 hkey,
3569 bool *send_acpi_ev,
3570 bool *ignore_acpi_ev)
3572 /* 0x5000-0x5FFF: human interface helpers */
3573 *send_acpi_ev = true;
3574 *ignore_acpi_ev = false;
3576 switch (hkey) {
3577 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3578 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3579 return true;
3581 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3582 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3583 tpacpi_input_send_tabletsw();
3584 hotkey_tablet_mode_notify_change();
3585 *send_acpi_ev = false;
3586 return true;
3588 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3589 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3590 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3591 /* do not propagate these events */
3592 *ignore_acpi_ev = true;
3593 return true;
3595 default:
3596 return false;
3600 static void thermal_dump_all_sensors(void);
3602 static bool hotkey_notify_thermal(const u32 hkey,
3603 bool *send_acpi_ev,
3604 bool *ignore_acpi_ev)
3606 bool known = true;
3608 /* 0x6000-0x6FFF: thermal alarms */
3609 *send_acpi_ev = true;
3610 *ignore_acpi_ev = false;
3612 switch (hkey) {
3613 case TP_HKEY_EV_THM_TABLE_CHANGED:
3614 printk(TPACPI_INFO
3615 "EC reports that Thermal Table has changed\n");
3616 /* recommended action: do nothing, we don't have
3617 * Lenovo ATM information */
3618 return true;
3619 case TP_HKEY_EV_ALARM_BAT_HOT:
3620 printk(TPACPI_CRIT
3621 "THERMAL ALARM: battery is too hot!\n");
3622 /* recommended action: warn user through gui */
3623 break;
3624 case TP_HKEY_EV_ALARM_BAT_XHOT:
3625 printk(TPACPI_ALERT
3626 "THERMAL EMERGENCY: battery is extremely hot!\n");
3627 /* recommended action: immediate sleep/hibernate */
3628 break;
3629 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3630 printk(TPACPI_CRIT
3631 "THERMAL ALARM: "
3632 "a sensor reports something is too hot!\n");
3633 /* recommended action: warn user through gui, that */
3634 /* some internal component is too hot */
3635 break;
3636 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3637 printk(TPACPI_ALERT
3638 "THERMAL EMERGENCY: "
3639 "a sensor reports something is extremely hot!\n");
3640 /* recommended action: immediate sleep/hibernate */
3641 break;
3642 default:
3643 printk(TPACPI_ALERT
3644 "THERMAL ALERT: unknown thermal alarm received\n");
3645 known = false;
3648 thermal_dump_all_sensors();
3650 return known;
3653 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3655 u32 hkey;
3656 bool send_acpi_ev;
3657 bool ignore_acpi_ev;
3658 bool known_ev;
3660 if (event != 0x80) {
3661 printk(TPACPI_ERR
3662 "unknown HKEY notification event %d\n", event);
3663 /* forward it to userspace, maybe it knows how to handle it */
3664 acpi_bus_generate_netlink_event(
3665 ibm->acpi->device->pnp.device_class,
3666 dev_name(&ibm->acpi->device->dev),
3667 event, 0);
3668 return;
3671 while (1) {
3672 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3673 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3674 return;
3677 if (hkey == 0) {
3678 /* queue empty */
3679 return;
3682 send_acpi_ev = true;
3683 ignore_acpi_ev = false;
3685 switch (hkey >> 12) {
3686 case 1:
3687 /* 0x1000-0x1FFF: key presses */
3688 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
3690 break;
3691 case 2:
3692 /* 0x2000-0x2FFF: Wakeup reason */
3693 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3694 &ignore_acpi_ev);
3695 break;
3696 case 3:
3697 /* 0x3000-0x3FFF: bay-related wakeups */
3698 switch (hkey) {
3699 case TP_HKEY_EV_BAYEJ_ACK:
3700 hotkey_autosleep_ack = 1;
3701 printk(TPACPI_INFO
3702 "bay ejected\n");
3703 hotkey_wakeup_hotunplug_complete_notify_change();
3704 known_ev = true;
3705 break;
3706 case TP_HKEY_EV_OPTDRV_EJ:
3707 /* FIXME: kick libata if SATA link offline */
3708 known_ev = true;
3709 break;
3710 default:
3711 known_ev = false;
3713 break;
3714 case 4:
3715 /* 0x4000-0x4FFF: dock-related wakeups */
3716 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3717 hotkey_autosleep_ack = 1;
3718 printk(TPACPI_INFO
3719 "undocked\n");
3720 hotkey_wakeup_hotunplug_complete_notify_change();
3721 known_ev = true;
3722 } else {
3723 known_ev = false;
3725 break;
3726 case 5:
3727 /* 0x5000-0x5FFF: human interface helpers */
3728 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3729 &ignore_acpi_ev);
3730 break;
3731 case 6:
3732 /* 0x6000-0x6FFF: thermal alarms */
3733 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3734 &ignore_acpi_ev);
3735 break;
3736 case 7:
3737 /* 0x7000-0x7FFF: misc */
3738 if (tp_features.hotkey_wlsw &&
3739 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3740 tpacpi_send_radiosw_update();
3741 send_acpi_ev = 0;
3742 known_ev = true;
3743 break;
3745 /* fallthrough to default */
3746 default:
3747 known_ev = false;
3749 if (!known_ev) {
3750 printk(TPACPI_NOTICE
3751 "unhandled HKEY event 0x%04x\n", hkey);
3752 printk(TPACPI_NOTICE
3753 "please report the conditions when this "
3754 "event happened to %s\n", TPACPI_MAIL);
3757 /* Legacy events */
3758 if (!ignore_acpi_ev &&
3759 (send_acpi_ev || hotkey_report_mode < 2)) {
3760 acpi_bus_generate_proc_event(ibm->acpi->device,
3761 event, hkey);
3764 /* netlink events */
3765 if (!ignore_acpi_ev && send_acpi_ev) {
3766 acpi_bus_generate_netlink_event(
3767 ibm->acpi->device->pnp.device_class,
3768 dev_name(&ibm->acpi->device->dev),
3769 event, hkey);
3774 static void hotkey_suspend(pm_message_t state)
3776 /* Do these on suspend, we get the events on early resume! */
3777 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3778 hotkey_autosleep_ack = 0;
3781 static void hotkey_resume(void)
3783 tpacpi_disable_brightness_delay();
3785 if (hotkey_status_set(true) < 0 ||
3786 hotkey_mask_set(hotkey_acpi_mask) < 0)
3787 printk(TPACPI_ERR
3788 "error while attempting to reset the event "
3789 "firmware interface\n");
3791 tpacpi_send_radiosw_update();
3792 hotkey_tablet_mode_notify_change();
3793 hotkey_wakeup_reason_notify_change();
3794 hotkey_wakeup_hotunplug_complete_notify_change();
3795 hotkey_poll_setup_safe(false);
3798 /* procfs -------------------------------------------------------------- */
3799 static int hotkey_read(struct seq_file *m)
3801 int res, status;
3803 if (!tp_features.hotkey) {
3804 seq_printf(m, "status:\t\tnot supported\n");
3805 return 0;
3808 if (mutex_lock_killable(&hotkey_mutex))
3809 return -ERESTARTSYS;
3810 res = hotkey_status_get(&status);
3811 if (!res)
3812 res = hotkey_mask_get();
3813 mutex_unlock(&hotkey_mutex);
3814 if (res)
3815 return res;
3817 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3818 if (hotkey_all_mask) {
3819 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3820 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3821 } else {
3822 seq_printf(m, "mask:\t\tnot supported\n");
3823 seq_printf(m, "commands:\tenable, disable, reset\n");
3826 return 0;
3829 static void hotkey_enabledisable_warn(bool enable)
3831 tpacpi_log_usertask("procfs hotkey enable/disable");
3832 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3833 TPACPI_WARN
3834 "hotkey enable/disable functionality has been "
3835 "removed from the driver. Hotkeys are always "
3836 "enabled\n"))
3837 printk(TPACPI_ERR
3838 "Please remove the hotkey=enable module "
3839 "parameter, it is deprecated. Hotkeys are always "
3840 "enabled\n");
3843 static int hotkey_write(char *buf)
3845 int res;
3846 u32 mask;
3847 char *cmd;
3849 if (!tp_features.hotkey)
3850 return -ENODEV;
3852 if (mutex_lock_killable(&hotkey_mutex))
3853 return -ERESTARTSYS;
3855 mask = hotkey_user_mask;
3857 res = 0;
3858 while ((cmd = next_cmd(&buf))) {
3859 if (strlencmp(cmd, "enable") == 0) {
3860 hotkey_enabledisable_warn(1);
3861 } else if (strlencmp(cmd, "disable") == 0) {
3862 hotkey_enabledisable_warn(0);
3863 res = -EPERM;
3864 } else if (strlencmp(cmd, "reset") == 0) {
3865 mask = (hotkey_all_mask | hotkey_source_mask)
3866 & ~hotkey_reserved_mask;
3867 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3868 /* mask set */
3869 } else if (sscanf(cmd, "%x", &mask) == 1) {
3870 /* mask set */
3871 } else {
3872 res = -EINVAL;
3873 goto errexit;
3877 if (!res) {
3878 tpacpi_disclose_usertask("procfs hotkey",
3879 "set mask to 0x%08x\n", mask);
3880 res = hotkey_user_mask_set(mask);
3883 errexit:
3884 mutex_unlock(&hotkey_mutex);
3885 return res;
3888 static const struct acpi_device_id ibm_htk_device_ids[] = {
3889 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3890 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3891 {"", 0},
3894 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3895 .hid = ibm_htk_device_ids,
3896 .notify = hotkey_notify,
3897 .handle = &hkey_handle,
3898 .type = ACPI_DEVICE_NOTIFY,
3901 static struct ibm_struct hotkey_driver_data = {
3902 .name = "hotkey",
3903 .read = hotkey_read,
3904 .write = hotkey_write,
3905 .exit = hotkey_exit,
3906 .resume = hotkey_resume,
3907 .suspend = hotkey_suspend,
3908 .acpi = &ibm_hotkey_acpidriver,
3911 /*************************************************************************
3912 * Bluetooth subdriver
3915 enum {
3916 /* ACPI GBDC/SBDC bits */
3917 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3918 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3919 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3920 0 = disable, 1 = enable */
3923 enum {
3924 /* ACPI \BLTH commands */
3925 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3926 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3927 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3928 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3929 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3932 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3934 static int bluetooth_get_status(void)
3936 int status;
3938 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3939 if (dbg_bluetoothemul)
3940 return (tpacpi_bluetooth_emulstate) ?
3941 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3942 #endif
3944 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3945 return -EIO;
3947 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3948 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3951 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3953 int status;
3955 vdbg_printk(TPACPI_DBG_RFKILL,
3956 "will attempt to %s bluetooth\n",
3957 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3959 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3960 if (dbg_bluetoothemul) {
3961 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3962 return 0;
3964 #endif
3966 if (state == TPACPI_RFK_RADIO_ON)
3967 status = TP_ACPI_BLUETOOTH_RADIOSSW
3968 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3969 else
3970 status = 0;
3972 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3973 return -EIO;
3975 return 0;
3978 /* sysfs bluetooth enable ---------------------------------------------- */
3979 static ssize_t bluetooth_enable_show(struct device *dev,
3980 struct device_attribute *attr,
3981 char *buf)
3983 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3984 attr, buf);
3987 static ssize_t bluetooth_enable_store(struct device *dev,
3988 struct device_attribute *attr,
3989 const char *buf, size_t count)
3991 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3992 attr, buf, count);
3995 static struct device_attribute dev_attr_bluetooth_enable =
3996 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3997 bluetooth_enable_show, bluetooth_enable_store);
3999 /* --------------------------------------------------------------------- */
4001 static struct attribute *bluetooth_attributes[] = {
4002 &dev_attr_bluetooth_enable.attr,
4003 NULL
4006 static const struct attribute_group bluetooth_attr_group = {
4007 .attrs = bluetooth_attributes,
4010 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4011 .get_status = bluetooth_get_status,
4012 .set_status = bluetooth_set_status,
4015 static void bluetooth_shutdown(void)
4017 /* Order firmware to save current state to NVRAM */
4018 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4019 TP_ACPI_BLTH_SAVE_STATE))
4020 printk(TPACPI_NOTICE
4021 "failed to save bluetooth state to NVRAM\n");
4022 else
4023 vdbg_printk(TPACPI_DBG_RFKILL,
4024 "bluestooth state saved to NVRAM\n");
4027 static void bluetooth_exit(void)
4029 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4030 &bluetooth_attr_group);
4032 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4034 bluetooth_shutdown();
4037 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4039 int res;
4040 int status = 0;
4042 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4043 "initializing bluetooth subdriver\n");
4045 TPACPI_ACPIHANDLE_INIT(hkey);
4047 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4048 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4049 tp_features.bluetooth = hkey_handle &&
4050 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4052 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4053 "bluetooth is %s, status 0x%02x\n",
4054 str_supported(tp_features.bluetooth),
4055 status);
4057 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4058 if (dbg_bluetoothemul) {
4059 tp_features.bluetooth = 1;
4060 printk(TPACPI_INFO
4061 "bluetooth switch emulation enabled\n");
4062 } else
4063 #endif
4064 if (tp_features.bluetooth &&
4065 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4066 /* no bluetooth hardware present in system */
4067 tp_features.bluetooth = 0;
4068 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4069 "bluetooth hardware not installed\n");
4072 if (!tp_features.bluetooth)
4073 return 1;
4075 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4076 &bluetooth_tprfk_ops,
4077 RFKILL_TYPE_BLUETOOTH,
4078 TPACPI_RFK_BLUETOOTH_SW_NAME,
4079 true);
4080 if (res)
4081 return res;
4083 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4084 &bluetooth_attr_group);
4085 if (res) {
4086 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4087 return res;
4090 return 0;
4093 /* procfs -------------------------------------------------------------- */
4094 static int bluetooth_read(struct seq_file *m)
4096 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4099 static int bluetooth_write(char *buf)
4101 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4104 static struct ibm_struct bluetooth_driver_data = {
4105 .name = "bluetooth",
4106 .read = bluetooth_read,
4107 .write = bluetooth_write,
4108 .exit = bluetooth_exit,
4109 .shutdown = bluetooth_shutdown,
4112 /*************************************************************************
4113 * Wan subdriver
4116 enum {
4117 /* ACPI GWAN/SWAN bits */
4118 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4119 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4120 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4121 0 = disable, 1 = enable */
4124 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4126 static int wan_get_status(void)
4128 int status;
4130 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4131 if (dbg_wwanemul)
4132 return (tpacpi_wwan_emulstate) ?
4133 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4134 #endif
4136 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4137 return -EIO;
4139 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4140 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4143 static int wan_set_status(enum tpacpi_rfkill_state state)
4145 int status;
4147 vdbg_printk(TPACPI_DBG_RFKILL,
4148 "will attempt to %s wwan\n",
4149 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4151 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4152 if (dbg_wwanemul) {
4153 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4154 return 0;
4156 #endif
4158 if (state == TPACPI_RFK_RADIO_ON)
4159 status = TP_ACPI_WANCARD_RADIOSSW
4160 | TP_ACPI_WANCARD_RESUMECTRL;
4161 else
4162 status = 0;
4164 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4165 return -EIO;
4167 return 0;
4170 /* sysfs wan enable ---------------------------------------------------- */
4171 static ssize_t wan_enable_show(struct device *dev,
4172 struct device_attribute *attr,
4173 char *buf)
4175 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4176 attr, buf);
4179 static ssize_t wan_enable_store(struct device *dev,
4180 struct device_attribute *attr,
4181 const char *buf, size_t count)
4183 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4184 attr, buf, count);
4187 static struct device_attribute dev_attr_wan_enable =
4188 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4189 wan_enable_show, wan_enable_store);
4191 /* --------------------------------------------------------------------- */
4193 static struct attribute *wan_attributes[] = {
4194 &dev_attr_wan_enable.attr,
4195 NULL
4198 static const struct attribute_group wan_attr_group = {
4199 .attrs = wan_attributes,
4202 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4203 .get_status = wan_get_status,
4204 .set_status = wan_set_status,
4207 static void wan_shutdown(void)
4209 /* Order firmware to save current state to NVRAM */
4210 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4211 TP_ACPI_WGSV_SAVE_STATE))
4212 printk(TPACPI_NOTICE
4213 "failed to save WWAN state to NVRAM\n");
4214 else
4215 vdbg_printk(TPACPI_DBG_RFKILL,
4216 "WWAN state saved to NVRAM\n");
4219 static void wan_exit(void)
4221 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4222 &wan_attr_group);
4224 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4226 wan_shutdown();
4229 static int __init wan_init(struct ibm_init_struct *iibm)
4231 int res;
4232 int status = 0;
4234 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4235 "initializing wan subdriver\n");
4237 TPACPI_ACPIHANDLE_INIT(hkey);
4239 tp_features.wan = hkey_handle &&
4240 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4242 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4243 "wan is %s, status 0x%02x\n",
4244 str_supported(tp_features.wan),
4245 status);
4247 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4248 if (dbg_wwanemul) {
4249 tp_features.wan = 1;
4250 printk(TPACPI_INFO
4251 "wwan switch emulation enabled\n");
4252 } else
4253 #endif
4254 if (tp_features.wan &&
4255 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4256 /* no wan hardware present in system */
4257 tp_features.wan = 0;
4258 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4259 "wan hardware not installed\n");
4262 if (!tp_features.wan)
4263 return 1;
4265 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4266 &wan_tprfk_ops,
4267 RFKILL_TYPE_WWAN,
4268 TPACPI_RFK_WWAN_SW_NAME,
4269 true);
4270 if (res)
4271 return res;
4273 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4274 &wan_attr_group);
4276 if (res) {
4277 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4278 return res;
4281 return 0;
4284 /* procfs -------------------------------------------------------------- */
4285 static int wan_read(struct seq_file *m)
4287 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4290 static int wan_write(char *buf)
4292 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4295 static struct ibm_struct wan_driver_data = {
4296 .name = "wan",
4297 .read = wan_read,
4298 .write = wan_write,
4299 .exit = wan_exit,
4300 .shutdown = wan_shutdown,
4303 /*************************************************************************
4304 * UWB subdriver
4307 enum {
4308 /* ACPI GUWB/SUWB bits */
4309 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4310 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4313 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4315 static int uwb_get_status(void)
4317 int status;
4319 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4320 if (dbg_uwbemul)
4321 return (tpacpi_uwb_emulstate) ?
4322 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4323 #endif
4325 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4326 return -EIO;
4328 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4329 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4332 static int uwb_set_status(enum tpacpi_rfkill_state state)
4334 int status;
4336 vdbg_printk(TPACPI_DBG_RFKILL,
4337 "will attempt to %s UWB\n",
4338 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4340 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4341 if (dbg_uwbemul) {
4342 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4343 return 0;
4345 #endif
4347 if (state == TPACPI_RFK_RADIO_ON)
4348 status = TP_ACPI_UWB_RADIOSSW;
4349 else
4350 status = 0;
4352 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4353 return -EIO;
4355 return 0;
4358 /* --------------------------------------------------------------------- */
4360 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4361 .get_status = uwb_get_status,
4362 .set_status = uwb_set_status,
4365 static void uwb_exit(void)
4367 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4370 static int __init uwb_init(struct ibm_init_struct *iibm)
4372 int res;
4373 int status = 0;
4375 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4376 "initializing uwb subdriver\n");
4378 TPACPI_ACPIHANDLE_INIT(hkey);
4380 tp_features.uwb = hkey_handle &&
4381 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4383 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4384 "uwb is %s, status 0x%02x\n",
4385 str_supported(tp_features.uwb),
4386 status);
4388 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4389 if (dbg_uwbemul) {
4390 tp_features.uwb = 1;
4391 printk(TPACPI_INFO
4392 "uwb switch emulation enabled\n");
4393 } else
4394 #endif
4395 if (tp_features.uwb &&
4396 !(status & TP_ACPI_UWB_HWPRESENT)) {
4397 /* no uwb hardware present in system */
4398 tp_features.uwb = 0;
4399 dbg_printk(TPACPI_DBG_INIT,
4400 "uwb hardware not installed\n");
4403 if (!tp_features.uwb)
4404 return 1;
4406 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4407 &uwb_tprfk_ops,
4408 RFKILL_TYPE_UWB,
4409 TPACPI_RFK_UWB_SW_NAME,
4410 false);
4411 return res;
4414 static struct ibm_struct uwb_driver_data = {
4415 .name = "uwb",
4416 .exit = uwb_exit,
4417 .flags.experimental = 1,
4420 /*************************************************************************
4421 * Video subdriver
4424 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4426 enum video_access_mode {
4427 TPACPI_VIDEO_NONE = 0,
4428 TPACPI_VIDEO_570, /* 570 */
4429 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4430 TPACPI_VIDEO_NEW, /* all others */
4433 enum { /* video status flags, based on VIDEO_570 */
4434 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4435 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4436 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4439 enum { /* TPACPI_VIDEO_570 constants */
4440 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4441 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4442 * video_status_flags */
4443 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4444 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4447 static enum video_access_mode video_supported;
4448 static int video_orig_autosw;
4450 static int video_autosw_get(void);
4451 static int video_autosw_set(int enable);
4453 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4455 static int __init video_init(struct ibm_init_struct *iibm)
4457 int ivga;
4459 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4461 TPACPI_ACPIHANDLE_INIT(vid);
4462 if (tpacpi_is_ibm())
4463 TPACPI_ACPIHANDLE_INIT(vid2);
4465 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4466 /* G41, assume IVGA doesn't change */
4467 vid_handle = vid2_handle;
4469 if (!vid_handle)
4470 /* video switching not supported on R30, R31 */
4471 video_supported = TPACPI_VIDEO_NONE;
4472 else if (tpacpi_is_ibm() &&
4473 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4474 /* 570 */
4475 video_supported = TPACPI_VIDEO_570;
4476 else if (tpacpi_is_ibm() &&
4477 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4478 /* 600e/x, 770e, 770x */
4479 video_supported = TPACPI_VIDEO_770;
4480 else
4481 /* all others */
4482 video_supported = TPACPI_VIDEO_NEW;
4484 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4485 str_supported(video_supported != TPACPI_VIDEO_NONE),
4486 video_supported);
4488 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4491 static void video_exit(void)
4493 dbg_printk(TPACPI_DBG_EXIT,
4494 "restoring original video autoswitch mode\n");
4495 if (video_autosw_set(video_orig_autosw))
4496 printk(TPACPI_ERR "error while trying to restore original "
4497 "video autoswitch mode\n");
4500 static int video_outputsw_get(void)
4502 int status = 0;
4503 int i;
4505 switch (video_supported) {
4506 case TPACPI_VIDEO_570:
4507 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4508 TP_ACPI_VIDEO_570_PHSCMD))
4509 return -EIO;
4510 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4511 break;
4512 case TPACPI_VIDEO_770:
4513 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4514 return -EIO;
4515 if (i)
4516 status |= TP_ACPI_VIDEO_S_LCD;
4517 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4518 return -EIO;
4519 if (i)
4520 status |= TP_ACPI_VIDEO_S_CRT;
4521 break;
4522 case TPACPI_VIDEO_NEW:
4523 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4524 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4525 return -EIO;
4526 if (i)
4527 status |= TP_ACPI_VIDEO_S_CRT;
4529 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4530 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4531 return -EIO;
4532 if (i)
4533 status |= TP_ACPI_VIDEO_S_LCD;
4534 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4535 return -EIO;
4536 if (i)
4537 status |= TP_ACPI_VIDEO_S_DVI;
4538 break;
4539 default:
4540 return -ENOSYS;
4543 return status;
4546 static int video_outputsw_set(int status)
4548 int autosw;
4549 int res = 0;
4551 switch (video_supported) {
4552 case TPACPI_VIDEO_570:
4553 res = acpi_evalf(NULL, NULL,
4554 "\\_SB.PHS2", "vdd",
4555 TP_ACPI_VIDEO_570_PHS2CMD,
4556 status | TP_ACPI_VIDEO_570_PHS2SET);
4557 break;
4558 case TPACPI_VIDEO_770:
4559 autosw = video_autosw_get();
4560 if (autosw < 0)
4561 return autosw;
4563 res = video_autosw_set(1);
4564 if (res)
4565 return res;
4566 res = acpi_evalf(vid_handle, NULL,
4567 "ASWT", "vdd", status * 0x100, 0);
4568 if (!autosw && video_autosw_set(autosw)) {
4569 printk(TPACPI_ERR
4570 "video auto-switch left enabled due to error\n");
4571 return -EIO;
4573 break;
4574 case TPACPI_VIDEO_NEW:
4575 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4576 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4577 break;
4578 default:
4579 return -ENOSYS;
4582 return (res)? 0 : -EIO;
4585 static int video_autosw_get(void)
4587 int autosw = 0;
4589 switch (video_supported) {
4590 case TPACPI_VIDEO_570:
4591 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4592 return -EIO;
4593 break;
4594 case TPACPI_VIDEO_770:
4595 case TPACPI_VIDEO_NEW:
4596 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4597 return -EIO;
4598 break;
4599 default:
4600 return -ENOSYS;
4603 return autosw & 1;
4606 static int video_autosw_set(int enable)
4608 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4609 return -EIO;
4610 return 0;
4613 static int video_outputsw_cycle(void)
4615 int autosw = video_autosw_get();
4616 int res;
4618 if (autosw < 0)
4619 return autosw;
4621 switch (video_supported) {
4622 case TPACPI_VIDEO_570:
4623 res = video_autosw_set(1);
4624 if (res)
4625 return res;
4626 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4627 break;
4628 case TPACPI_VIDEO_770:
4629 case TPACPI_VIDEO_NEW:
4630 res = video_autosw_set(1);
4631 if (res)
4632 return res;
4633 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4634 break;
4635 default:
4636 return -ENOSYS;
4638 if (!autosw && video_autosw_set(autosw)) {
4639 printk(TPACPI_ERR
4640 "video auto-switch left enabled due to error\n");
4641 return -EIO;
4644 return (res)? 0 : -EIO;
4647 static int video_expand_toggle(void)
4649 switch (video_supported) {
4650 case TPACPI_VIDEO_570:
4651 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4652 0 : -EIO;
4653 case TPACPI_VIDEO_770:
4654 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4655 0 : -EIO;
4656 case TPACPI_VIDEO_NEW:
4657 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4658 0 : -EIO;
4659 default:
4660 return -ENOSYS;
4662 /* not reached */
4665 static int video_read(struct seq_file *m)
4667 int status, autosw;
4669 if (video_supported == TPACPI_VIDEO_NONE) {
4670 seq_printf(m, "status:\t\tnot supported\n");
4671 return 0;
4674 /* Even reads can crash X.org, so... */
4675 if (!capable(CAP_SYS_ADMIN))
4676 return -EPERM;
4678 status = video_outputsw_get();
4679 if (status < 0)
4680 return status;
4682 autosw = video_autosw_get();
4683 if (autosw < 0)
4684 return autosw;
4686 seq_printf(m, "status:\t\tsupported\n");
4687 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4688 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4689 if (video_supported == TPACPI_VIDEO_NEW)
4690 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4691 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4692 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4693 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4694 if (video_supported == TPACPI_VIDEO_NEW)
4695 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4696 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4697 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4699 return 0;
4702 static int video_write(char *buf)
4704 char *cmd;
4705 int enable, disable, status;
4706 int res;
4708 if (video_supported == TPACPI_VIDEO_NONE)
4709 return -ENODEV;
4711 /* Even reads can crash X.org, let alone writes... */
4712 if (!capable(CAP_SYS_ADMIN))
4713 return -EPERM;
4715 enable = 0;
4716 disable = 0;
4718 while ((cmd = next_cmd(&buf))) {
4719 if (strlencmp(cmd, "lcd_enable") == 0) {
4720 enable |= TP_ACPI_VIDEO_S_LCD;
4721 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4722 disable |= TP_ACPI_VIDEO_S_LCD;
4723 } else if (strlencmp(cmd, "crt_enable") == 0) {
4724 enable |= TP_ACPI_VIDEO_S_CRT;
4725 } else if (strlencmp(cmd, "crt_disable") == 0) {
4726 disable |= TP_ACPI_VIDEO_S_CRT;
4727 } else if (video_supported == TPACPI_VIDEO_NEW &&
4728 strlencmp(cmd, "dvi_enable") == 0) {
4729 enable |= TP_ACPI_VIDEO_S_DVI;
4730 } else if (video_supported == TPACPI_VIDEO_NEW &&
4731 strlencmp(cmd, "dvi_disable") == 0) {
4732 disable |= TP_ACPI_VIDEO_S_DVI;
4733 } else if (strlencmp(cmd, "auto_enable") == 0) {
4734 res = video_autosw_set(1);
4735 if (res)
4736 return res;
4737 } else if (strlencmp(cmd, "auto_disable") == 0) {
4738 res = video_autosw_set(0);
4739 if (res)
4740 return res;
4741 } else if (strlencmp(cmd, "video_switch") == 0) {
4742 res = video_outputsw_cycle();
4743 if (res)
4744 return res;
4745 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4746 res = video_expand_toggle();
4747 if (res)
4748 return res;
4749 } else
4750 return -EINVAL;
4753 if (enable || disable) {
4754 status = video_outputsw_get();
4755 if (status < 0)
4756 return status;
4757 res = video_outputsw_set((status & ~disable) | enable);
4758 if (res)
4759 return res;
4762 return 0;
4765 static struct ibm_struct video_driver_data = {
4766 .name = "video",
4767 .read = video_read,
4768 .write = video_write,
4769 .exit = video_exit,
4772 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4774 /*************************************************************************
4775 * Light (thinklight) subdriver
4778 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4779 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4781 static int light_get_status(void)
4783 int status = 0;
4785 if (tp_features.light_status) {
4786 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4787 return -EIO;
4788 return (!!status);
4791 return -ENXIO;
4794 static int light_set_status(int status)
4796 int rc;
4798 if (tp_features.light) {
4799 if (cmos_handle) {
4800 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4801 (status)?
4802 TP_CMOS_THINKLIGHT_ON :
4803 TP_CMOS_THINKLIGHT_OFF);
4804 } else {
4805 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4806 (status)? 1 : 0);
4808 return (rc)? 0 : -EIO;
4811 return -ENXIO;
4814 static void light_set_status_worker(struct work_struct *work)
4816 struct tpacpi_led_classdev *data =
4817 container_of(work, struct tpacpi_led_classdev, work);
4819 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4820 light_set_status((data->new_state != TPACPI_LED_OFF));
4823 static void light_sysfs_set(struct led_classdev *led_cdev,
4824 enum led_brightness brightness)
4826 struct tpacpi_led_classdev *data =
4827 container_of(led_cdev,
4828 struct tpacpi_led_classdev,
4829 led_classdev);
4830 data->new_state = (brightness != LED_OFF) ?
4831 TPACPI_LED_ON : TPACPI_LED_OFF;
4832 queue_work(tpacpi_wq, &data->work);
4835 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4837 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4840 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4841 .led_classdev = {
4842 .name = "tpacpi::thinklight",
4843 .brightness_set = &light_sysfs_set,
4844 .brightness_get = &light_sysfs_get,
4848 static int __init light_init(struct ibm_init_struct *iibm)
4850 int rc;
4852 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4854 if (tpacpi_is_ibm()) {
4855 TPACPI_ACPIHANDLE_INIT(ledb);
4856 TPACPI_ACPIHANDLE_INIT(lght);
4858 TPACPI_ACPIHANDLE_INIT(cmos);
4859 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4861 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4862 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4864 if (tp_features.light)
4865 /* light status not supported on
4866 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4867 tp_features.light_status =
4868 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4870 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4871 str_supported(tp_features.light),
4872 str_supported(tp_features.light_status));
4874 if (!tp_features.light)
4875 return 1;
4877 rc = led_classdev_register(&tpacpi_pdev->dev,
4878 &tpacpi_led_thinklight.led_classdev);
4880 if (rc < 0) {
4881 tp_features.light = 0;
4882 tp_features.light_status = 0;
4883 } else {
4884 rc = 0;
4887 return rc;
4890 static void light_exit(void)
4892 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4893 if (work_pending(&tpacpi_led_thinklight.work))
4894 flush_workqueue(tpacpi_wq);
4897 static int light_read(struct seq_file *m)
4899 int status;
4901 if (!tp_features.light) {
4902 seq_printf(m, "status:\t\tnot supported\n");
4903 } else if (!tp_features.light_status) {
4904 seq_printf(m, "status:\t\tunknown\n");
4905 seq_printf(m, "commands:\ton, off\n");
4906 } else {
4907 status = light_get_status();
4908 if (status < 0)
4909 return status;
4910 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4911 seq_printf(m, "commands:\ton, off\n");
4914 return 0;
4917 static int light_write(char *buf)
4919 char *cmd;
4920 int newstatus = 0;
4922 if (!tp_features.light)
4923 return -ENODEV;
4925 while ((cmd = next_cmd(&buf))) {
4926 if (strlencmp(cmd, "on") == 0) {
4927 newstatus = 1;
4928 } else if (strlencmp(cmd, "off") == 0) {
4929 newstatus = 0;
4930 } else
4931 return -EINVAL;
4934 return light_set_status(newstatus);
4937 static struct ibm_struct light_driver_data = {
4938 .name = "light",
4939 .read = light_read,
4940 .write = light_write,
4941 .exit = light_exit,
4944 /*************************************************************************
4945 * CMOS subdriver
4948 /* sysfs cmos_command -------------------------------------------------- */
4949 static ssize_t cmos_command_store(struct device *dev,
4950 struct device_attribute *attr,
4951 const char *buf, size_t count)
4953 unsigned long cmos_cmd;
4954 int res;
4956 if (parse_strtoul(buf, 21, &cmos_cmd))
4957 return -EINVAL;
4959 res = issue_thinkpad_cmos_command(cmos_cmd);
4960 return (res)? res : count;
4963 static struct device_attribute dev_attr_cmos_command =
4964 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4966 /* --------------------------------------------------------------------- */
4968 static int __init cmos_init(struct ibm_init_struct *iibm)
4970 int res;
4972 vdbg_printk(TPACPI_DBG_INIT,
4973 "initializing cmos commands subdriver\n");
4975 TPACPI_ACPIHANDLE_INIT(cmos);
4977 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4978 str_supported(cmos_handle != NULL));
4980 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4981 if (res)
4982 return res;
4984 return (cmos_handle)? 0 : 1;
4987 static void cmos_exit(void)
4989 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4992 static int cmos_read(struct seq_file *m)
4994 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4995 R30, R31, T20-22, X20-21 */
4996 if (!cmos_handle)
4997 seq_printf(m, "status:\t\tnot supported\n");
4998 else {
4999 seq_printf(m, "status:\t\tsupported\n");
5000 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5003 return 0;
5006 static int cmos_write(char *buf)
5008 char *cmd;
5009 int cmos_cmd, res;
5011 while ((cmd = next_cmd(&buf))) {
5012 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5013 cmos_cmd >= 0 && cmos_cmd <= 21) {
5014 /* cmos_cmd set */
5015 } else
5016 return -EINVAL;
5018 res = issue_thinkpad_cmos_command(cmos_cmd);
5019 if (res)
5020 return res;
5023 return 0;
5026 static struct ibm_struct cmos_driver_data = {
5027 .name = "cmos",
5028 .read = cmos_read,
5029 .write = cmos_write,
5030 .exit = cmos_exit,
5033 /*************************************************************************
5034 * LED subdriver
5037 enum led_access_mode {
5038 TPACPI_LED_NONE = 0,
5039 TPACPI_LED_570, /* 570 */
5040 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5041 TPACPI_LED_NEW, /* all others */
5044 enum { /* For TPACPI_LED_OLD */
5045 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5046 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5047 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5050 static enum led_access_mode led_supported;
5052 static acpi_handle led_handle;
5054 #define TPACPI_LED_NUMLEDS 16
5055 static struct tpacpi_led_classdev *tpacpi_leds;
5056 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5057 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5058 /* there's a limit of 19 chars + NULL before 2.6.26 */
5059 "tpacpi::power",
5060 "tpacpi:orange:batt",
5061 "tpacpi:green:batt",
5062 "tpacpi::dock_active",
5063 "tpacpi::bay_active",
5064 "tpacpi::dock_batt",
5065 "tpacpi::unknown_led",
5066 "tpacpi::standby",
5067 "tpacpi::dock_status1",
5068 "tpacpi::dock_status2",
5069 "tpacpi::unknown_led2",
5070 "tpacpi::unknown_led3",
5071 "tpacpi::thinkvantage",
5073 #define TPACPI_SAFE_LEDS 0x1081U
5075 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5077 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5078 return false;
5079 #else
5080 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5081 #endif
5084 static int led_get_status(const unsigned int led)
5086 int status;
5087 enum led_status_t led_s;
5089 switch (led_supported) {
5090 case TPACPI_LED_570:
5091 if (!acpi_evalf(ec_handle,
5092 &status, "GLED", "dd", 1 << led))
5093 return -EIO;
5094 led_s = (status == 0)?
5095 TPACPI_LED_OFF :
5096 ((status == 1)?
5097 TPACPI_LED_ON :
5098 TPACPI_LED_BLINK);
5099 tpacpi_led_state_cache[led] = led_s;
5100 return led_s;
5101 default:
5102 return -ENXIO;
5105 /* not reached */
5108 static int led_set_status(const unsigned int led,
5109 const enum led_status_t ledstatus)
5111 /* off, on, blink. Index is led_status_t */
5112 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5113 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5115 int rc = 0;
5117 switch (led_supported) {
5118 case TPACPI_LED_570:
5119 /* 570 */
5120 if (unlikely(led > 7))
5121 return -EINVAL;
5122 if (unlikely(tpacpi_is_led_restricted(led)))
5123 return -EPERM;
5124 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5125 (1 << led), led_sled_arg1[ledstatus]))
5126 rc = -EIO;
5127 break;
5128 case TPACPI_LED_OLD:
5129 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5130 if (unlikely(led > 7))
5131 return -EINVAL;
5132 if (unlikely(tpacpi_is_led_restricted(led)))
5133 return -EPERM;
5134 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5135 if (rc >= 0)
5136 rc = ec_write(TPACPI_LED_EC_HLBL,
5137 (ledstatus == TPACPI_LED_BLINK) << led);
5138 if (rc >= 0)
5139 rc = ec_write(TPACPI_LED_EC_HLCL,
5140 (ledstatus != TPACPI_LED_OFF) << led);
5141 break;
5142 case TPACPI_LED_NEW:
5143 /* all others */
5144 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5145 return -EINVAL;
5146 if (unlikely(tpacpi_is_led_restricted(led)))
5147 return -EPERM;
5148 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5149 led, led_led_arg1[ledstatus]))
5150 rc = -EIO;
5151 break;
5152 default:
5153 rc = -ENXIO;
5156 if (!rc)
5157 tpacpi_led_state_cache[led] = ledstatus;
5159 return rc;
5162 static void led_set_status_worker(struct work_struct *work)
5164 struct tpacpi_led_classdev *data =
5165 container_of(work, struct tpacpi_led_classdev, work);
5167 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5168 led_set_status(data->led, data->new_state);
5171 static void led_sysfs_set(struct led_classdev *led_cdev,
5172 enum led_brightness brightness)
5174 struct tpacpi_led_classdev *data = container_of(led_cdev,
5175 struct tpacpi_led_classdev, led_classdev);
5177 if (brightness == LED_OFF)
5178 data->new_state = TPACPI_LED_OFF;
5179 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5180 data->new_state = TPACPI_LED_ON;
5181 else
5182 data->new_state = TPACPI_LED_BLINK;
5184 queue_work(tpacpi_wq, &data->work);
5187 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5188 unsigned long *delay_on, unsigned long *delay_off)
5190 struct tpacpi_led_classdev *data = container_of(led_cdev,
5191 struct tpacpi_led_classdev, led_classdev);
5193 /* Can we choose the flash rate? */
5194 if (*delay_on == 0 && *delay_off == 0) {
5195 /* yes. set them to the hardware blink rate (1 Hz) */
5196 *delay_on = 500; /* ms */
5197 *delay_off = 500; /* ms */
5198 } else if ((*delay_on != 500) || (*delay_off != 500))
5199 return -EINVAL;
5201 data->new_state = TPACPI_LED_BLINK;
5202 queue_work(tpacpi_wq, &data->work);
5204 return 0;
5207 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5209 int rc;
5211 struct tpacpi_led_classdev *data = container_of(led_cdev,
5212 struct tpacpi_led_classdev, led_classdev);
5214 rc = led_get_status(data->led);
5216 if (rc == TPACPI_LED_OFF || rc < 0)
5217 rc = LED_OFF; /* no error handling in led class :( */
5218 else
5219 rc = LED_FULL;
5221 return rc;
5224 static void led_exit(void)
5226 unsigned int i;
5228 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5229 if (tpacpi_leds[i].led_classdev.name)
5230 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5233 kfree(tpacpi_leds);
5236 static int __init tpacpi_init_led(unsigned int led)
5238 int rc;
5240 tpacpi_leds[led].led = led;
5242 /* LEDs with no name don't get registered */
5243 if (!tpacpi_led_names[led])
5244 return 0;
5246 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5247 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5248 if (led_supported == TPACPI_LED_570)
5249 tpacpi_leds[led].led_classdev.brightness_get =
5250 &led_sysfs_get;
5252 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5254 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5256 rc = led_classdev_register(&tpacpi_pdev->dev,
5257 &tpacpi_leds[led].led_classdev);
5258 if (rc < 0)
5259 tpacpi_leds[led].led_classdev.name = NULL;
5261 return rc;
5264 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5265 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5266 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5267 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5269 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5270 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5271 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5272 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5273 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5274 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5275 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5276 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5278 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5279 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5280 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5281 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5282 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5284 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5285 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5286 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5287 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5289 /* (1) - may have excess leds enabled on MSB */
5291 /* Defaults (order matters, keep last, don't reorder!) */
5292 { /* Lenovo */
5293 .vendor = PCI_VENDOR_ID_LENOVO,
5294 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5295 .quirks = 0x1fffU,
5297 { /* IBM ThinkPads with no EC version string */
5298 .vendor = PCI_VENDOR_ID_IBM,
5299 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5300 .quirks = 0x00ffU,
5302 { /* IBM ThinkPads with EC version string */
5303 .vendor = PCI_VENDOR_ID_IBM,
5304 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5305 .quirks = 0x00bfU,
5309 #undef TPACPI_LEDQ_IBM
5310 #undef TPACPI_LEDQ_LNV
5312 static enum led_access_mode __init led_init_detect_mode(void)
5314 acpi_status status;
5316 if (tpacpi_is_ibm()) {
5317 /* 570 */
5318 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5319 if (ACPI_SUCCESS(status))
5320 return TPACPI_LED_570;
5322 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5323 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5324 if (ACPI_SUCCESS(status))
5325 return TPACPI_LED_OLD;
5328 /* most others */
5329 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5330 if (ACPI_SUCCESS(status))
5331 return TPACPI_LED_NEW;
5333 /* R30, R31, and unknown firmwares */
5334 led_handle = NULL;
5335 return TPACPI_LED_NONE;
5338 static int __init led_init(struct ibm_init_struct *iibm)
5340 unsigned int i;
5341 int rc;
5342 unsigned long useful_leds;
5344 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5346 led_supported = led_init_detect_mode();
5348 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5349 str_supported(led_supported), led_supported);
5351 if (led_supported == TPACPI_LED_NONE)
5352 return 1;
5354 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5355 GFP_KERNEL);
5356 if (!tpacpi_leds) {
5357 printk(TPACPI_ERR "Out of memory for LED data\n");
5358 return -ENOMEM;
5361 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5362 ARRAY_SIZE(led_useful_qtable));
5364 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5365 if (!tpacpi_is_led_restricted(i) &&
5366 test_bit(i, &useful_leds)) {
5367 rc = tpacpi_init_led(i);
5368 if (rc < 0) {
5369 led_exit();
5370 return rc;
5375 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5376 printk(TPACPI_NOTICE
5377 "warning: userspace override of important "
5378 "firmware LEDs is enabled\n");
5379 #endif
5380 return 0;
5383 #define str_led_status(s) \
5384 ((s) == TPACPI_LED_OFF ? "off" : \
5385 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5387 static int led_read(struct seq_file *m)
5389 if (!led_supported) {
5390 seq_printf(m, "status:\t\tnot supported\n");
5391 return 0;
5393 seq_printf(m, "status:\t\tsupported\n");
5395 if (led_supported == TPACPI_LED_570) {
5396 /* 570 */
5397 int i, status;
5398 for (i = 0; i < 8; i++) {
5399 status = led_get_status(i);
5400 if (status < 0)
5401 return -EIO;
5402 seq_printf(m, "%d:\t\t%s\n",
5403 i, str_led_status(status));
5407 seq_printf(m, "commands:\t"
5408 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5410 return 0;
5413 static int led_write(char *buf)
5415 char *cmd;
5416 int led, rc;
5417 enum led_status_t s;
5419 if (!led_supported)
5420 return -ENODEV;
5422 while ((cmd = next_cmd(&buf))) {
5423 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5424 return -EINVAL;
5426 if (strstr(cmd, "off")) {
5427 s = TPACPI_LED_OFF;
5428 } else if (strstr(cmd, "on")) {
5429 s = TPACPI_LED_ON;
5430 } else if (strstr(cmd, "blink")) {
5431 s = TPACPI_LED_BLINK;
5432 } else {
5433 return -EINVAL;
5436 rc = led_set_status(led, s);
5437 if (rc < 0)
5438 return rc;
5441 return 0;
5444 static struct ibm_struct led_driver_data = {
5445 .name = "led",
5446 .read = led_read,
5447 .write = led_write,
5448 .exit = led_exit,
5451 /*************************************************************************
5452 * Beep subdriver
5455 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5457 #define TPACPI_BEEP_Q1 0x0001
5459 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5460 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5461 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5464 static int __init beep_init(struct ibm_init_struct *iibm)
5466 unsigned long quirks;
5468 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5470 TPACPI_ACPIHANDLE_INIT(beep);
5472 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5473 str_supported(beep_handle != NULL));
5475 quirks = tpacpi_check_quirks(beep_quirk_table,
5476 ARRAY_SIZE(beep_quirk_table));
5478 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5480 return (beep_handle)? 0 : 1;
5483 static int beep_read(struct seq_file *m)
5485 if (!beep_handle)
5486 seq_printf(m, "status:\t\tnot supported\n");
5487 else {
5488 seq_printf(m, "status:\t\tsupported\n");
5489 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5492 return 0;
5495 static int beep_write(char *buf)
5497 char *cmd;
5498 int beep_cmd;
5500 if (!beep_handle)
5501 return -ENODEV;
5503 while ((cmd = next_cmd(&buf))) {
5504 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5505 beep_cmd >= 0 && beep_cmd <= 17) {
5506 /* beep_cmd set */
5507 } else
5508 return -EINVAL;
5509 if (tp_features.beep_needs_two_args) {
5510 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5511 beep_cmd, 0))
5512 return -EIO;
5513 } else {
5514 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5515 beep_cmd))
5516 return -EIO;
5520 return 0;
5523 static struct ibm_struct beep_driver_data = {
5524 .name = "beep",
5525 .read = beep_read,
5526 .write = beep_write,
5529 /*************************************************************************
5530 * Thermal subdriver
5533 enum thermal_access_mode {
5534 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5535 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5536 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5537 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5538 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5541 enum { /* TPACPI_THERMAL_TPEC_* */
5542 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5543 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5544 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5546 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5550 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5551 struct ibm_thermal_sensors_struct {
5552 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5555 static enum thermal_access_mode thermal_read_mode;
5557 /* idx is zero-based */
5558 static int thermal_get_sensor(int idx, s32 *value)
5560 int t;
5561 s8 tmp;
5562 char tmpi[5];
5564 t = TP_EC_THERMAL_TMP0;
5566 switch (thermal_read_mode) {
5567 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5568 case TPACPI_THERMAL_TPEC_16:
5569 if (idx >= 8 && idx <= 15) {
5570 t = TP_EC_THERMAL_TMP8;
5571 idx -= 8;
5573 /* fallthrough */
5574 #endif
5575 case TPACPI_THERMAL_TPEC_8:
5576 if (idx <= 7) {
5577 if (!acpi_ec_read(t + idx, &tmp))
5578 return -EIO;
5579 *value = tmp * 1000;
5580 return 0;
5582 break;
5584 case TPACPI_THERMAL_ACPI_UPDT:
5585 if (idx <= 7) {
5586 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5587 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5588 return -EIO;
5589 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5590 return -EIO;
5591 *value = (t - 2732) * 100;
5592 return 0;
5594 break;
5596 case TPACPI_THERMAL_ACPI_TMP07:
5597 if (idx <= 7) {
5598 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5599 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5600 return -EIO;
5601 if (t > 127 || t < -127)
5602 t = TP_EC_THERMAL_TMP_NA;
5603 *value = t * 1000;
5604 return 0;
5606 break;
5608 case TPACPI_THERMAL_NONE:
5609 default:
5610 return -ENOSYS;
5613 return -EINVAL;
5616 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5618 int res, i;
5619 int n;
5621 n = 8;
5622 i = 0;
5624 if (!s)
5625 return -EINVAL;
5627 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5628 n = 16;
5630 for (i = 0 ; i < n; i++) {
5631 res = thermal_get_sensor(i, &s->temp[i]);
5632 if (res)
5633 return res;
5636 return n;
5639 static void thermal_dump_all_sensors(void)
5641 int n, i;
5642 struct ibm_thermal_sensors_struct t;
5644 n = thermal_get_sensors(&t);
5645 if (n <= 0)
5646 return;
5648 printk(TPACPI_NOTICE
5649 "temperatures (Celsius):");
5651 for (i = 0; i < n; i++) {
5652 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5653 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5654 else
5655 printk(KERN_CONT " N/A");
5658 printk(KERN_CONT "\n");
5661 /* sysfs temp##_input -------------------------------------------------- */
5663 static ssize_t thermal_temp_input_show(struct device *dev,
5664 struct device_attribute *attr,
5665 char *buf)
5667 struct sensor_device_attribute *sensor_attr =
5668 to_sensor_dev_attr(attr);
5669 int idx = sensor_attr->index;
5670 s32 value;
5671 int res;
5673 res = thermal_get_sensor(idx, &value);
5674 if (res)
5675 return res;
5676 if (value == TPACPI_THERMAL_SENSOR_NA)
5677 return -ENXIO;
5679 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5682 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5683 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5684 thermal_temp_input_show, NULL, _idxB)
5686 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5687 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5688 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5689 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5690 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5691 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5692 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5693 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5694 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5695 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5696 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5697 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5698 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5699 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5700 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5701 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5702 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5705 #define THERMAL_ATTRS(X) \
5706 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5708 static struct attribute *thermal_temp_input_attr[] = {
5709 THERMAL_ATTRS(8),
5710 THERMAL_ATTRS(9),
5711 THERMAL_ATTRS(10),
5712 THERMAL_ATTRS(11),
5713 THERMAL_ATTRS(12),
5714 THERMAL_ATTRS(13),
5715 THERMAL_ATTRS(14),
5716 THERMAL_ATTRS(15),
5717 THERMAL_ATTRS(0),
5718 THERMAL_ATTRS(1),
5719 THERMAL_ATTRS(2),
5720 THERMAL_ATTRS(3),
5721 THERMAL_ATTRS(4),
5722 THERMAL_ATTRS(5),
5723 THERMAL_ATTRS(6),
5724 THERMAL_ATTRS(7),
5725 NULL
5728 static const struct attribute_group thermal_temp_input16_group = {
5729 .attrs = thermal_temp_input_attr
5732 static const struct attribute_group thermal_temp_input8_group = {
5733 .attrs = &thermal_temp_input_attr[8]
5736 #undef THERMAL_SENSOR_ATTR_TEMP
5737 #undef THERMAL_ATTRS
5739 /* --------------------------------------------------------------------- */
5741 static int __init thermal_init(struct ibm_init_struct *iibm)
5743 u8 t, ta1, ta2;
5744 int i;
5745 int acpi_tmp7;
5746 int res;
5748 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5750 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5752 if (thinkpad_id.ec_model) {
5754 * Direct EC access mode: sensors at registers
5755 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5756 * non-implemented, thermal sensors return 0x80 when
5757 * not available
5760 ta1 = ta2 = 0;
5761 for (i = 0; i < 8; i++) {
5762 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5763 ta1 |= t;
5764 } else {
5765 ta1 = 0;
5766 break;
5768 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5769 ta2 |= t;
5770 } else {
5771 ta1 = 0;
5772 break;
5775 if (ta1 == 0) {
5776 /* This is sheer paranoia, but we handle it anyway */
5777 if (acpi_tmp7) {
5778 printk(TPACPI_ERR
5779 "ThinkPad ACPI EC access misbehaving, "
5780 "falling back to ACPI TMPx access "
5781 "mode\n");
5782 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5783 } else {
5784 printk(TPACPI_ERR
5785 "ThinkPad ACPI EC access misbehaving, "
5786 "disabling thermal sensors access\n");
5787 thermal_read_mode = TPACPI_THERMAL_NONE;
5789 } else {
5790 thermal_read_mode =
5791 (ta2 != 0) ?
5792 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5794 } else if (acpi_tmp7) {
5795 if (tpacpi_is_ibm() &&
5796 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5797 /* 600e/x, 770e, 770x */
5798 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5799 } else {
5800 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5801 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5803 } else {
5804 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5805 thermal_read_mode = TPACPI_THERMAL_NONE;
5808 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5809 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5810 thermal_read_mode);
5812 switch (thermal_read_mode) {
5813 case TPACPI_THERMAL_TPEC_16:
5814 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5815 &thermal_temp_input16_group);
5816 if (res)
5817 return res;
5818 break;
5819 case TPACPI_THERMAL_TPEC_8:
5820 case TPACPI_THERMAL_ACPI_TMP07:
5821 case TPACPI_THERMAL_ACPI_UPDT:
5822 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5823 &thermal_temp_input8_group);
5824 if (res)
5825 return res;
5826 break;
5827 case TPACPI_THERMAL_NONE:
5828 default:
5829 return 1;
5832 return 0;
5835 static void thermal_exit(void)
5837 switch (thermal_read_mode) {
5838 case TPACPI_THERMAL_TPEC_16:
5839 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5840 &thermal_temp_input16_group);
5841 break;
5842 case TPACPI_THERMAL_TPEC_8:
5843 case TPACPI_THERMAL_ACPI_TMP07:
5844 case TPACPI_THERMAL_ACPI_UPDT:
5845 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5846 &thermal_temp_input8_group);
5847 break;
5848 case TPACPI_THERMAL_NONE:
5849 default:
5850 break;
5854 static int thermal_read(struct seq_file *m)
5856 int n, i;
5857 struct ibm_thermal_sensors_struct t;
5859 n = thermal_get_sensors(&t);
5860 if (unlikely(n < 0))
5861 return n;
5863 seq_printf(m, "temperatures:\t");
5865 if (n > 0) {
5866 for (i = 0; i < (n - 1); i++)
5867 seq_printf(m, "%d ", t.temp[i] / 1000);
5868 seq_printf(m, "%d\n", t.temp[i] / 1000);
5869 } else
5870 seq_printf(m, "not supported\n");
5872 return 0;
5875 static struct ibm_struct thermal_driver_data = {
5876 .name = "thermal",
5877 .read = thermal_read,
5878 .exit = thermal_exit,
5881 /*************************************************************************
5882 * EC Dump subdriver
5885 static u8 ecdump_regs[256];
5887 static int ecdump_read(struct seq_file *m)
5889 int i, j;
5890 u8 v;
5892 seq_printf(m, "EC "
5893 " +00 +01 +02 +03 +04 +05 +06 +07"
5894 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5895 for (i = 0; i < 256; i += 16) {
5896 seq_printf(m, "EC 0x%02x:", i);
5897 for (j = 0; j < 16; j++) {
5898 if (!acpi_ec_read(i + j, &v))
5899 break;
5900 if (v != ecdump_regs[i + j])
5901 seq_printf(m, " *%02x", v);
5902 else
5903 seq_printf(m, " %02x", v);
5904 ecdump_regs[i + j] = v;
5906 seq_putc(m, '\n');
5907 if (j != 16)
5908 break;
5911 /* These are way too dangerous to advertise openly... */
5912 #if 0
5913 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5914 " (<offset> is 00-ff, <value> is 00-ff)\n");
5915 seq_printf(m, "commands:\t0x<offset> <value> "
5916 " (<offset> is 00-ff, <value> is 0-255)\n");
5917 #endif
5918 return 0;
5921 static int ecdump_write(char *buf)
5923 char *cmd;
5924 int i, v;
5926 while ((cmd = next_cmd(&buf))) {
5927 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5928 /* i and v set */
5929 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5930 /* i and v set */
5931 } else
5932 return -EINVAL;
5933 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5934 if (!acpi_ec_write(i, v))
5935 return -EIO;
5936 } else
5937 return -EINVAL;
5940 return 0;
5943 static struct ibm_struct ecdump_driver_data = {
5944 .name = "ecdump",
5945 .read = ecdump_read,
5946 .write = ecdump_write,
5947 .flags.experimental = 1,
5950 /*************************************************************************
5951 * Backlight/brightness subdriver
5954 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5957 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5958 * CMOS NVRAM byte 0x5E, bits 0-3.
5960 * EC HBRV (0x31) has the following layout
5961 * Bit 7: unknown function
5962 * Bit 6: unknown function
5963 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5964 * Bit 4: must be set to zero to avoid problems
5965 * Bit 3-0: backlight brightness level
5967 * brightness_get_raw returns status data in the HBRV layout
5969 * WARNING: The X61 has been verified to use HBRV for something else, so
5970 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5971 * testing on the very early *60 Lenovo models...
5974 enum {
5975 TP_EC_BACKLIGHT = 0x31,
5977 /* TP_EC_BACKLIGHT bitmasks */
5978 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5979 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5980 TP_EC_BACKLIGHT_MAPSW = 0x20,
5983 enum tpacpi_brightness_access_mode {
5984 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5985 TPACPI_BRGHT_MODE_EC, /* EC control */
5986 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5987 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5988 TPACPI_BRGHT_MODE_MAX
5991 static struct backlight_device *ibm_backlight_device;
5993 static enum tpacpi_brightness_access_mode brightness_mode =
5994 TPACPI_BRGHT_MODE_MAX;
5996 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5998 static struct mutex brightness_mutex;
6000 /* NVRAM brightness access,
6001 * call with brightness_mutex held! */
6002 static unsigned int tpacpi_brightness_nvram_get(void)
6004 u8 lnvram;
6006 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6007 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6008 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6009 lnvram &= bright_maxlvl;
6011 return lnvram;
6014 static void tpacpi_brightness_checkpoint_nvram(void)
6016 u8 lec = 0;
6017 u8 b_nvram;
6019 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6020 return;
6022 vdbg_printk(TPACPI_DBG_BRGHT,
6023 "trying to checkpoint backlight level to NVRAM...\n");
6025 if (mutex_lock_killable(&brightness_mutex) < 0)
6026 return;
6028 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6029 goto unlock;
6030 lec &= TP_EC_BACKLIGHT_LVLMSK;
6031 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6033 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6034 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6035 /* NVRAM needs update */
6036 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6037 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6038 b_nvram |= lec;
6039 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6040 dbg_printk(TPACPI_DBG_BRGHT,
6041 "updated NVRAM backlight level to %u (0x%02x)\n",
6042 (unsigned int) lec, (unsigned int) b_nvram);
6043 } else
6044 vdbg_printk(TPACPI_DBG_BRGHT,
6045 "NVRAM backlight level already is %u (0x%02x)\n",
6046 (unsigned int) lec, (unsigned int) b_nvram);
6048 unlock:
6049 mutex_unlock(&brightness_mutex);
6053 /* call with brightness_mutex held! */
6054 static int tpacpi_brightness_get_raw(int *status)
6056 u8 lec = 0;
6058 switch (brightness_mode) {
6059 case TPACPI_BRGHT_MODE_UCMS_STEP:
6060 *status = tpacpi_brightness_nvram_get();
6061 return 0;
6062 case TPACPI_BRGHT_MODE_EC:
6063 case TPACPI_BRGHT_MODE_ECNVRAM:
6064 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6065 return -EIO;
6066 *status = lec;
6067 return 0;
6068 default:
6069 return -ENXIO;
6073 /* call with brightness_mutex held! */
6074 /* do NOT call with illegal backlight level value */
6075 static int tpacpi_brightness_set_ec(unsigned int value)
6077 u8 lec = 0;
6079 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6080 return -EIO;
6082 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6083 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6084 (value & TP_EC_BACKLIGHT_LVLMSK))))
6085 return -EIO;
6087 return 0;
6090 /* call with brightness_mutex held! */
6091 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6093 int cmos_cmd, inc;
6094 unsigned int current_value, i;
6096 current_value = tpacpi_brightness_nvram_get();
6098 if (value == current_value)
6099 return 0;
6101 cmos_cmd = (value > current_value) ?
6102 TP_CMOS_BRIGHTNESS_UP :
6103 TP_CMOS_BRIGHTNESS_DOWN;
6104 inc = (value > current_value) ? 1 : -1;
6106 for (i = current_value; i != value; i += inc)
6107 if (issue_thinkpad_cmos_command(cmos_cmd))
6108 return -EIO;
6110 return 0;
6113 /* May return EINTR which can always be mapped to ERESTARTSYS */
6114 static int brightness_set(unsigned int value)
6116 int res;
6118 if (value > bright_maxlvl || value < 0)
6119 return -EINVAL;
6121 vdbg_printk(TPACPI_DBG_BRGHT,
6122 "set backlight level to %d\n", value);
6124 res = mutex_lock_killable(&brightness_mutex);
6125 if (res < 0)
6126 return res;
6128 switch (brightness_mode) {
6129 case TPACPI_BRGHT_MODE_EC:
6130 case TPACPI_BRGHT_MODE_ECNVRAM:
6131 res = tpacpi_brightness_set_ec(value);
6132 break;
6133 case TPACPI_BRGHT_MODE_UCMS_STEP:
6134 res = tpacpi_brightness_set_ucmsstep(value);
6135 break;
6136 default:
6137 res = -ENXIO;
6140 mutex_unlock(&brightness_mutex);
6141 return res;
6144 /* sysfs backlight class ----------------------------------------------- */
6146 static int brightness_update_status(struct backlight_device *bd)
6148 unsigned int level =
6149 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6150 bd->props.power == FB_BLANK_UNBLANK) ?
6151 bd->props.brightness : 0;
6153 dbg_printk(TPACPI_DBG_BRGHT,
6154 "backlight: attempt to set level to %d\n",
6155 level);
6157 /* it is the backlight class's job (caller) to handle
6158 * EINTR and other errors properly */
6159 return brightness_set(level);
6162 static int brightness_get(struct backlight_device *bd)
6164 int status, res;
6166 res = mutex_lock_killable(&brightness_mutex);
6167 if (res < 0)
6168 return 0;
6170 res = tpacpi_brightness_get_raw(&status);
6172 mutex_unlock(&brightness_mutex);
6174 if (res < 0)
6175 return 0;
6177 return status & TP_EC_BACKLIGHT_LVLMSK;
6180 static void tpacpi_brightness_notify_change(void)
6182 backlight_force_update(ibm_backlight_device,
6183 BACKLIGHT_UPDATE_HOTKEY);
6186 static struct backlight_ops ibm_backlight_data = {
6187 .get_brightness = brightness_get,
6188 .update_status = brightness_update_status,
6191 /* --------------------------------------------------------------------- */
6194 * Call _BCL method of video device. On some ThinkPads this will
6195 * switch the firmware to the ACPI brightness control mode.
6198 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6200 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6201 union acpi_object *obj;
6202 int rc;
6204 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6205 obj = (union acpi_object *)buffer.pointer;
6206 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6207 printk(TPACPI_ERR "Unknown _BCL data, "
6208 "please report this to %s\n", TPACPI_MAIL);
6209 rc = 0;
6210 } else {
6211 rc = obj->package.count;
6213 } else {
6214 return 0;
6217 kfree(buffer.pointer);
6218 return rc;
6223 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6225 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6227 acpi_handle video_device;
6228 int bcl_levels = 0;
6230 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6231 if (video_device)
6232 bcl_levels = tpacpi_query_bcl_levels(video_device);
6234 tp_features.bright_acpimode = (bcl_levels > 0);
6236 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6240 * These are only useful for models that have only one possibility
6241 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6242 * these quirks.
6244 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6245 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6246 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6248 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6249 /* Models with ATI GPUs known to require ECNVRAM mode */
6250 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6252 /* Models with ATI GPUs that can use ECNVRAM */
6253 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6254 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6255 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6256 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6258 /* Models with Intel Extreme Graphics 2 */
6259 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6260 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6261 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6263 /* Models with Intel GMA900 */
6264 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6265 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6266 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6270 * Returns < 0 for error, otherwise sets tp_features.bright_*
6271 * and bright_maxlvl.
6273 static void __init tpacpi_detect_brightness_capabilities(void)
6275 unsigned int b;
6277 vdbg_printk(TPACPI_DBG_INIT,
6278 "detecting firmware brightness interface capabilities\n");
6280 /* we could run a quirks check here (same table used by
6281 * brightness_init) if needed */
6284 * We always attempt to detect acpi support, so as to switch
6285 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6286 * going to publish a backlight interface
6288 b = tpacpi_check_std_acpi_brightness_support();
6289 switch (b) {
6290 case 16:
6291 bright_maxlvl = 15;
6292 printk(TPACPI_INFO
6293 "detected a 16-level brightness capable ThinkPad\n");
6294 break;
6295 case 8:
6296 case 0:
6297 bright_maxlvl = 7;
6298 printk(TPACPI_INFO
6299 "detected a 8-level brightness capable ThinkPad\n");
6300 break;
6301 default:
6302 printk(TPACPI_ERR
6303 "Unsupported brightness interface, "
6304 "please contact %s\n", TPACPI_MAIL);
6305 tp_features.bright_unkfw = 1;
6306 bright_maxlvl = b - 1;
6310 static int __init brightness_init(struct ibm_init_struct *iibm)
6312 int b;
6313 unsigned long quirks;
6315 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6317 mutex_init(&brightness_mutex);
6319 quirks = tpacpi_check_quirks(brightness_quirk_table,
6320 ARRAY_SIZE(brightness_quirk_table));
6322 /* tpacpi_detect_brightness_capabilities() must have run already */
6324 /* if it is unknown, we don't handle it: it wouldn't be safe */
6325 if (tp_features.bright_unkfw)
6326 return 1;
6328 if (!brightness_enable) {
6329 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6330 "brightness support disabled by "
6331 "module parameter\n");
6332 return 1;
6335 if (acpi_video_backlight_support()) {
6336 if (brightness_enable > 1) {
6337 printk(TPACPI_INFO
6338 "Standard ACPI backlight interface "
6339 "available, not loading native one.\n");
6340 return 1;
6341 } else if (brightness_enable == 1) {
6342 printk(TPACPI_WARN
6343 "Cannot enable backlight brightness support, "
6344 "ACPI is already handling it. Refer to the "
6345 "acpi_backlight kernel parameter\n");
6346 return 1;
6348 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6349 printk(TPACPI_NOTICE
6350 "Standard ACPI backlight interface not "
6351 "available, thinkpad_acpi native "
6352 "brightness control enabled\n");
6356 * Check for module parameter bogosity, note that we
6357 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6358 * able to detect "unspecified"
6360 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6361 return -EINVAL;
6363 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6364 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6365 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6366 if (quirks & TPACPI_BRGHT_Q_EC)
6367 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6368 else
6369 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6371 dbg_printk(TPACPI_DBG_BRGHT,
6372 "driver auto-selected brightness_mode=%d\n",
6373 brightness_mode);
6376 /* Safety */
6377 if (!tpacpi_is_ibm() &&
6378 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6379 brightness_mode == TPACPI_BRGHT_MODE_EC))
6380 return -EINVAL;
6382 if (tpacpi_brightness_get_raw(&b) < 0)
6383 return 1;
6385 ibm_backlight_device = backlight_device_register(
6386 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6387 &ibm_backlight_data);
6388 if (IS_ERR(ibm_backlight_device)) {
6389 int rc = PTR_ERR(ibm_backlight_device);
6390 ibm_backlight_device = NULL;
6391 printk(TPACPI_ERR "Could not register backlight device\n");
6392 return rc;
6394 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6395 "brightness is supported\n");
6397 if (quirks & TPACPI_BRGHT_Q_ASK) {
6398 printk(TPACPI_NOTICE
6399 "brightness: will use unverified default: "
6400 "brightness_mode=%d\n", brightness_mode);
6401 printk(TPACPI_NOTICE
6402 "brightness: please report to %s whether it works well "
6403 "or not on your ThinkPad\n", TPACPI_MAIL);
6406 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6407 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6409 /* Added by mistake in early 2007. Probably useless, but it could
6410 * be working around some unknown firmware problem where the value
6411 * read at startup doesn't match the real hardware state... so leave
6412 * it in place just in case */
6413 backlight_update_status(ibm_backlight_device);
6415 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6416 "brightness: registering brightness hotkeys "
6417 "as change notification\n");
6418 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6419 | TP_ACPI_HKEY_BRGHTUP_MASK
6420 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6421 return 0;
6424 static void brightness_suspend(pm_message_t state)
6426 tpacpi_brightness_checkpoint_nvram();
6429 static void brightness_shutdown(void)
6431 tpacpi_brightness_checkpoint_nvram();
6434 static void brightness_exit(void)
6436 if (ibm_backlight_device) {
6437 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6438 "calling backlight_device_unregister()\n");
6439 backlight_device_unregister(ibm_backlight_device);
6442 tpacpi_brightness_checkpoint_nvram();
6445 static int brightness_read(struct seq_file *m)
6447 int level;
6449 level = brightness_get(NULL);
6450 if (level < 0) {
6451 seq_printf(m, "level:\t\tunreadable\n");
6452 } else {
6453 seq_printf(m, "level:\t\t%d\n", level);
6454 seq_printf(m, "commands:\tup, down\n");
6455 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6456 bright_maxlvl);
6459 return 0;
6462 static int brightness_write(char *buf)
6464 int level;
6465 int rc;
6466 char *cmd;
6468 level = brightness_get(NULL);
6469 if (level < 0)
6470 return level;
6472 while ((cmd = next_cmd(&buf))) {
6473 if (strlencmp(cmd, "up") == 0) {
6474 if (level < bright_maxlvl)
6475 level++;
6476 } else if (strlencmp(cmd, "down") == 0) {
6477 if (level > 0)
6478 level--;
6479 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6480 level >= 0 && level <= bright_maxlvl) {
6481 /* new level set */
6482 } else
6483 return -EINVAL;
6486 tpacpi_disclose_usertask("procfs brightness",
6487 "set level to %d\n", level);
6490 * Now we know what the final level should be, so we try to set it.
6491 * Doing it this way makes the syscall restartable in case of EINTR
6493 rc = brightness_set(level);
6494 if (!rc && ibm_backlight_device)
6495 backlight_force_update(ibm_backlight_device,
6496 BACKLIGHT_UPDATE_SYSFS);
6497 return (rc == -EINTR)? -ERESTARTSYS : rc;
6500 static struct ibm_struct brightness_driver_data = {
6501 .name = "brightness",
6502 .read = brightness_read,
6503 .write = brightness_write,
6504 .exit = brightness_exit,
6505 .suspend = brightness_suspend,
6506 .shutdown = brightness_shutdown,
6509 /*************************************************************************
6510 * Volume subdriver
6514 * IBM ThinkPads have a simple volume controller with MUTE gating.
6515 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6517 * Since the *61 series (and probably also the later *60 series), Lenovo
6518 * ThinkPads only implement the MUTE gate.
6520 * EC register 0x30
6521 * Bit 6: MUTE (1 mutes sound)
6522 * Bit 3-0: Volume
6523 * Other bits should be zero as far as we know.
6525 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6526 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6527 * such as bit 7 which is used to detect repeated presses of MUTE,
6528 * and we leave them unchanged.
6531 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6533 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6534 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6535 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6537 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6538 static char *alsa_id = "ThinkPadEC";
6539 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6541 struct tpacpi_alsa_data {
6542 struct snd_card *card;
6543 struct snd_ctl_elem_id *ctl_mute_id;
6544 struct snd_ctl_elem_id *ctl_vol_id;
6547 static struct snd_card *alsa_card;
6549 enum {
6550 TP_EC_AUDIO = 0x30,
6552 /* TP_EC_AUDIO bits */
6553 TP_EC_AUDIO_MUTESW = 6,
6555 /* TP_EC_AUDIO bitmasks */
6556 TP_EC_AUDIO_LVL_MSK = 0x0F,
6557 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6559 /* Maximum volume */
6560 TP_EC_VOLUME_MAX = 14,
6563 enum tpacpi_volume_access_mode {
6564 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6565 TPACPI_VOL_MODE_EC, /* Pure EC control */
6566 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6567 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6568 TPACPI_VOL_MODE_MAX
6571 enum tpacpi_volume_capabilities {
6572 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6573 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6574 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6575 TPACPI_VOL_CAP_MAX
6578 static enum tpacpi_volume_access_mode volume_mode =
6579 TPACPI_VOL_MODE_MAX;
6581 static enum tpacpi_volume_capabilities volume_capabilities;
6582 static int volume_control_allowed;
6585 * Used to syncronize writers to TP_EC_AUDIO and
6586 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6588 static struct mutex volume_mutex;
6590 static void tpacpi_volume_checkpoint_nvram(void)
6592 u8 lec = 0;
6593 u8 b_nvram;
6594 u8 ec_mask;
6596 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6597 return;
6598 if (!volume_control_allowed)
6599 return;
6601 vdbg_printk(TPACPI_DBG_MIXER,
6602 "trying to checkpoint mixer state to NVRAM...\n");
6604 if (tp_features.mixer_no_level_control)
6605 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6606 else
6607 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6609 if (mutex_lock_killable(&volume_mutex) < 0)
6610 return;
6612 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6613 goto unlock;
6614 lec &= ec_mask;
6615 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6617 if (lec != (b_nvram & ec_mask)) {
6618 /* NVRAM needs update */
6619 b_nvram &= ~ec_mask;
6620 b_nvram |= lec;
6621 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6622 dbg_printk(TPACPI_DBG_MIXER,
6623 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6624 (unsigned int) lec, (unsigned int) b_nvram);
6625 } else {
6626 vdbg_printk(TPACPI_DBG_MIXER,
6627 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6628 (unsigned int) lec, (unsigned int) b_nvram);
6631 unlock:
6632 mutex_unlock(&volume_mutex);
6635 static int volume_get_status_ec(u8 *status)
6637 u8 s;
6639 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6640 return -EIO;
6642 *status = s;
6644 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6646 return 0;
6649 static int volume_get_status(u8 *status)
6651 return volume_get_status_ec(status);
6654 static int volume_set_status_ec(const u8 status)
6656 if (!acpi_ec_write(TP_EC_AUDIO, status))
6657 return -EIO;
6659 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6661 return 0;
6664 static int volume_set_status(const u8 status)
6666 return volume_set_status_ec(status);
6669 /* returns < 0 on error, 0 on no change, 1 on change */
6670 static int __volume_set_mute_ec(const bool mute)
6672 int rc;
6673 u8 s, n;
6675 if (mutex_lock_killable(&volume_mutex) < 0)
6676 return -EINTR;
6678 rc = volume_get_status_ec(&s);
6679 if (rc)
6680 goto unlock;
6682 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6683 s & ~TP_EC_AUDIO_MUTESW_MSK;
6685 if (n != s) {
6686 rc = volume_set_status_ec(n);
6687 if (!rc)
6688 rc = 1;
6691 unlock:
6692 mutex_unlock(&volume_mutex);
6693 return rc;
6696 static int volume_alsa_set_mute(const bool mute)
6698 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6699 (mute) ? "" : "un");
6700 return __volume_set_mute_ec(mute);
6703 static int volume_set_mute(const bool mute)
6705 int rc;
6707 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6708 (mute) ? "" : "un");
6710 rc = __volume_set_mute_ec(mute);
6711 return (rc < 0) ? rc : 0;
6714 /* returns < 0 on error, 0 on no change, 1 on change */
6715 static int __volume_set_volume_ec(const u8 vol)
6717 int rc;
6718 u8 s, n;
6720 if (vol > TP_EC_VOLUME_MAX)
6721 return -EINVAL;
6723 if (mutex_lock_killable(&volume_mutex) < 0)
6724 return -EINTR;
6726 rc = volume_get_status_ec(&s);
6727 if (rc)
6728 goto unlock;
6730 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6732 if (n != s) {
6733 rc = volume_set_status_ec(n);
6734 if (!rc)
6735 rc = 1;
6738 unlock:
6739 mutex_unlock(&volume_mutex);
6740 return rc;
6743 static int volume_alsa_set_volume(const u8 vol)
6745 dbg_printk(TPACPI_DBG_MIXER,
6746 "ALSA: trying to set volume level to %hu\n", vol);
6747 return __volume_set_volume_ec(vol);
6750 static void volume_alsa_notify_change(void)
6752 struct tpacpi_alsa_data *d;
6754 if (alsa_card && alsa_card->private_data) {
6755 d = alsa_card->private_data;
6756 if (d->ctl_mute_id)
6757 snd_ctl_notify(alsa_card,
6758 SNDRV_CTL_EVENT_MASK_VALUE,
6759 d->ctl_mute_id);
6760 if (d->ctl_vol_id)
6761 snd_ctl_notify(alsa_card,
6762 SNDRV_CTL_EVENT_MASK_VALUE,
6763 d->ctl_vol_id);
6767 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6768 struct snd_ctl_elem_info *uinfo)
6770 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6771 uinfo->count = 1;
6772 uinfo->value.integer.min = 0;
6773 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6774 return 0;
6777 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6778 struct snd_ctl_elem_value *ucontrol)
6780 u8 s;
6781 int rc;
6783 rc = volume_get_status(&s);
6784 if (rc < 0)
6785 return rc;
6787 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6788 return 0;
6791 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6792 struct snd_ctl_elem_value *ucontrol)
6794 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6795 ucontrol->value.integer.value[0]);
6796 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6799 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6801 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6802 struct snd_ctl_elem_value *ucontrol)
6804 u8 s;
6805 int rc;
6807 rc = volume_get_status(&s);
6808 if (rc < 0)
6809 return rc;
6811 ucontrol->value.integer.value[0] =
6812 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6813 return 0;
6816 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6817 struct snd_ctl_elem_value *ucontrol)
6819 tpacpi_disclose_usertask("ALSA", "%smute\n",
6820 ucontrol->value.integer.value[0] ?
6821 "un" : "");
6822 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6825 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6826 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6827 .name = "Console Playback Volume",
6828 .index = 0,
6829 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6830 .info = volume_alsa_vol_info,
6831 .get = volume_alsa_vol_get,
6834 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6836 .name = "Console Playback Switch",
6837 .index = 0,
6838 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6839 .info = volume_alsa_mute_info,
6840 .get = volume_alsa_mute_get,
6843 static void volume_suspend(pm_message_t state)
6845 tpacpi_volume_checkpoint_nvram();
6848 static void volume_resume(void)
6850 volume_alsa_notify_change();
6853 static void volume_shutdown(void)
6855 tpacpi_volume_checkpoint_nvram();
6858 static void volume_exit(void)
6860 if (alsa_card) {
6861 snd_card_free(alsa_card);
6862 alsa_card = NULL;
6865 tpacpi_volume_checkpoint_nvram();
6868 static int __init volume_create_alsa_mixer(void)
6870 struct snd_card *card;
6871 struct tpacpi_alsa_data *data;
6872 struct snd_kcontrol *ctl_vol;
6873 struct snd_kcontrol *ctl_mute;
6874 int rc;
6876 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6877 sizeof(struct tpacpi_alsa_data), &card);
6878 if (rc < 0 || !card) {
6879 printk(TPACPI_ERR
6880 "Failed to create ALSA card structures: %d\n", rc);
6881 return 1;
6884 BUG_ON(!card->private_data);
6885 data = card->private_data;
6886 data->card = card;
6888 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6889 sizeof(card->driver));
6890 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6891 sizeof(card->shortname));
6892 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6893 (thinkpad_id.ec_version_str) ?
6894 thinkpad_id.ec_version_str : "(unknown)");
6895 snprintf(card->longname, sizeof(card->longname),
6896 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6897 (thinkpad_id.ec_version_str) ?
6898 thinkpad_id.ec_version_str : "unknown");
6900 if (volume_control_allowed) {
6901 volume_alsa_control_vol.put = volume_alsa_vol_put;
6902 volume_alsa_control_vol.access =
6903 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6905 volume_alsa_control_mute.put = volume_alsa_mute_put;
6906 volume_alsa_control_mute.access =
6907 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6910 if (!tp_features.mixer_no_level_control) {
6911 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6912 rc = snd_ctl_add(card, ctl_vol);
6913 if (rc < 0) {
6914 printk(TPACPI_ERR
6915 "Failed to create ALSA volume control: %d\n",
6916 rc);
6917 goto err_exit;
6919 data->ctl_vol_id = &ctl_vol->id;
6922 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6923 rc = snd_ctl_add(card, ctl_mute);
6924 if (rc < 0) {
6925 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6926 rc);
6927 goto err_exit;
6929 data->ctl_mute_id = &ctl_mute->id;
6931 snd_card_set_dev(card, &tpacpi_pdev->dev);
6932 rc = snd_card_register(card);
6933 if (rc < 0) {
6934 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6935 goto err_exit;
6938 alsa_card = card;
6939 return 0;
6941 err_exit:
6942 snd_card_free(card);
6943 return 1;
6946 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6947 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6949 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6950 /* Whitelist volume level on all IBM by default */
6951 { .vendor = PCI_VENDOR_ID_IBM,
6952 .bios = TPACPI_MATCH_ANY,
6953 .ec = TPACPI_MATCH_ANY,
6954 .quirks = TPACPI_VOL_Q_LEVEL },
6956 /* Lenovo models with volume control (needs confirmation) */
6957 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6958 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6959 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6960 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6961 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6962 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6963 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6965 /* Whitelist mute-only on all Lenovo by default */
6966 { .vendor = PCI_VENDOR_ID_LENOVO,
6967 .bios = TPACPI_MATCH_ANY,
6968 .ec = TPACPI_MATCH_ANY,
6969 .quirks = TPACPI_VOL_Q_MUTEONLY }
6972 static int __init volume_init(struct ibm_init_struct *iibm)
6974 unsigned long quirks;
6975 int rc;
6977 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6979 mutex_init(&volume_mutex);
6982 * Check for module parameter bogosity, note that we
6983 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6984 * able to detect "unspecified"
6986 if (volume_mode > TPACPI_VOL_MODE_MAX)
6987 return -EINVAL;
6989 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6990 printk(TPACPI_ERR
6991 "UCMS step volume mode not implemented, "
6992 "please contact %s\n", TPACPI_MAIL);
6993 return 1;
6996 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6997 return -EINVAL;
7000 * The ALSA mixer is our primary interface.
7001 * When disabled, don't install the subdriver at all
7003 if (!alsa_enable) {
7004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7005 "ALSA mixer disabled by parameter, "
7006 "not loading volume subdriver...\n");
7007 return 1;
7010 quirks = tpacpi_check_quirks(volume_quirk_table,
7011 ARRAY_SIZE(volume_quirk_table));
7013 switch (volume_capabilities) {
7014 case TPACPI_VOL_CAP_AUTO:
7015 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7016 tp_features.mixer_no_level_control = 1;
7017 else if (quirks & TPACPI_VOL_Q_LEVEL)
7018 tp_features.mixer_no_level_control = 0;
7019 else
7020 return 1; /* no mixer */
7021 break;
7022 case TPACPI_VOL_CAP_VOLMUTE:
7023 tp_features.mixer_no_level_control = 0;
7024 break;
7025 case TPACPI_VOL_CAP_MUTEONLY:
7026 tp_features.mixer_no_level_control = 1;
7027 break;
7028 default:
7029 return 1;
7032 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7033 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7034 "using user-supplied volume_capabilities=%d\n",
7035 volume_capabilities);
7037 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7038 volume_mode == TPACPI_VOL_MODE_MAX) {
7039 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7041 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7042 "driver auto-selected volume_mode=%d\n",
7043 volume_mode);
7044 } else {
7045 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7046 "using user-supplied volume_mode=%d\n",
7047 volume_mode);
7050 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7051 "mute is supported, volume control is %s\n",
7052 str_supported(!tp_features.mixer_no_level_control));
7054 rc = volume_create_alsa_mixer();
7055 if (rc) {
7056 printk(TPACPI_ERR
7057 "Could not create the ALSA mixer interface\n");
7058 return rc;
7061 printk(TPACPI_INFO
7062 "Console audio control enabled, mode: %s\n",
7063 (volume_control_allowed) ?
7064 "override (read/write)" :
7065 "monitor (read only)");
7067 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7068 "registering volume hotkeys as change notification\n");
7069 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7070 | TP_ACPI_HKEY_VOLUP_MASK
7071 | TP_ACPI_HKEY_VOLDWN_MASK
7072 | TP_ACPI_HKEY_MUTE_MASK);
7074 return 0;
7077 static int volume_read(struct seq_file *m)
7079 u8 status;
7081 if (volume_get_status(&status) < 0) {
7082 seq_printf(m, "level:\t\tunreadable\n");
7083 } else {
7084 if (tp_features.mixer_no_level_control)
7085 seq_printf(m, "level:\t\tunsupported\n");
7086 else
7087 seq_printf(m, "level:\t\t%d\n",
7088 status & TP_EC_AUDIO_LVL_MSK);
7090 seq_printf(m, "mute:\t\t%s\n",
7091 onoff(status, TP_EC_AUDIO_MUTESW));
7093 if (volume_control_allowed) {
7094 seq_printf(m, "commands:\tunmute, mute\n");
7095 if (!tp_features.mixer_no_level_control) {
7096 seq_printf(m,
7097 "commands:\tup, down\n");
7098 seq_printf(m,
7099 "commands:\tlevel <level>"
7100 " (<level> is 0-%d)\n",
7101 TP_EC_VOLUME_MAX);
7106 return 0;
7109 static int volume_write(char *buf)
7111 u8 s;
7112 u8 new_level, new_mute;
7113 int l;
7114 char *cmd;
7115 int rc;
7118 * We do allow volume control at driver startup, so that the
7119 * user can set initial state through the volume=... parameter hack.
7121 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7122 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7123 tp_warned.volume_ctrl_forbidden = 1;
7124 printk(TPACPI_NOTICE
7125 "Console audio control in monitor mode, "
7126 "changes are not allowed.\n");
7127 printk(TPACPI_NOTICE
7128 "Use the volume_control=1 module parameter "
7129 "to enable volume control\n");
7131 return -EPERM;
7134 rc = volume_get_status(&s);
7135 if (rc < 0)
7136 return rc;
7138 new_level = s & TP_EC_AUDIO_LVL_MSK;
7139 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7141 while ((cmd = next_cmd(&buf))) {
7142 if (!tp_features.mixer_no_level_control) {
7143 if (strlencmp(cmd, "up") == 0) {
7144 if (new_mute)
7145 new_mute = 0;
7146 else if (new_level < TP_EC_VOLUME_MAX)
7147 new_level++;
7148 continue;
7149 } else if (strlencmp(cmd, "down") == 0) {
7150 if (new_mute)
7151 new_mute = 0;
7152 else if (new_level > 0)
7153 new_level--;
7154 continue;
7155 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7156 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7157 new_level = l;
7158 continue;
7161 if (strlencmp(cmd, "mute") == 0)
7162 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7163 else if (strlencmp(cmd, "unmute") == 0)
7164 new_mute = 0;
7165 else
7166 return -EINVAL;
7169 if (tp_features.mixer_no_level_control) {
7170 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7171 new_mute ? "" : "un");
7172 rc = volume_set_mute(!!new_mute);
7173 } else {
7174 tpacpi_disclose_usertask("procfs volume",
7175 "%smute and set level to %d\n",
7176 new_mute ? "" : "un", new_level);
7177 rc = volume_set_status(new_mute | new_level);
7179 volume_alsa_notify_change();
7181 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7184 static struct ibm_struct volume_driver_data = {
7185 .name = "volume",
7186 .read = volume_read,
7187 .write = volume_write,
7188 .exit = volume_exit,
7189 .suspend = volume_suspend,
7190 .resume = volume_resume,
7191 .shutdown = volume_shutdown,
7194 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7196 #define alsa_card NULL
7198 static void inline volume_alsa_notify_change(void)
7202 static int __init volume_init(struct ibm_init_struct *iibm)
7204 printk(TPACPI_INFO
7205 "volume: disabled as there is no ALSA support in this kernel\n");
7207 return 1;
7210 static struct ibm_struct volume_driver_data = {
7211 .name = "volume",
7214 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7216 /*************************************************************************
7217 * Fan subdriver
7221 * FAN ACCESS MODES
7223 * TPACPI_FAN_RD_ACPI_GFAN:
7224 * ACPI GFAN method: returns fan level
7226 * see TPACPI_FAN_WR_ACPI_SFAN
7227 * EC 0x2f (HFSP) not available if GFAN exists
7229 * TPACPI_FAN_WR_ACPI_SFAN:
7230 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7232 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7233 * it for writing.
7235 * TPACPI_FAN_WR_TPEC:
7236 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7237 * Supported on almost all ThinkPads
7239 * Fan speed changes of any sort (including those caused by the
7240 * disengaged mode) are usually done slowly by the firmware as the
7241 * maximum amount of fan duty cycle change per second seems to be
7242 * limited.
7244 * Reading is not available if GFAN exists.
7245 * Writing is not available if SFAN exists.
7247 * Bits
7248 * 7 automatic mode engaged;
7249 * (default operation mode of the ThinkPad)
7250 * fan level is ignored in this mode.
7251 * 6 full speed mode (takes precedence over bit 7);
7252 * not available on all thinkpads. May disable
7253 * the tachometer while the fan controller ramps up
7254 * the speed (which can take up to a few *minutes*).
7255 * Speeds up fan to 100% duty-cycle, which is far above
7256 * the standard RPM levels. It is not impossible that
7257 * it could cause hardware damage.
7258 * 5-3 unused in some models. Extra bits for fan level
7259 * in others, but still useless as all values above
7260 * 7 map to the same speed as level 7 in these models.
7261 * 2-0 fan level (0..7 usually)
7262 * 0x00 = stop
7263 * 0x07 = max (set when temperatures critical)
7264 * Some ThinkPads may have other levels, see
7265 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7267 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7268 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7269 * does so, its initial value is meaningless (0x07).
7271 * For firmware bugs, refer to:
7272 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7274 * ----
7276 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7277 * Main fan tachometer reading (in RPM)
7279 * This register is present on all ThinkPads with a new-style EC, and
7280 * it is known not to be present on the A21m/e, and T22, as there is
7281 * something else in offset 0x84 according to the ACPI DSDT. Other
7282 * ThinkPads from this same time period (and earlier) probably lack the
7283 * tachometer as well.
7285 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7286 * was never fixed by IBM to report the EC firmware version string
7287 * probably support the tachometer (like the early X models), so
7288 * detecting it is quite hard. We need more data to know for sure.
7290 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7291 * might result.
7293 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7294 * mode.
7296 * For firmware bugs, refer to:
7297 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7299 * ----
7301 * ThinkPad EC register 0x31 bit 0 (only on select models)
7303 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7304 * show the speed of the main fan. When bit 0 of EC register 0x31
7305 * is one, the tachometer registers show the speed of the auxiliary
7306 * fan.
7308 * Fan control seems to affect both fans, regardless of the state
7309 * of this bit.
7311 * So far, only the firmware for the X60/X61 non-tablet versions
7312 * seem to support this (firmware TP-7M).
7314 * TPACPI_FAN_WR_ACPI_FANS:
7315 * ThinkPad X31, X40, X41. Not available in the X60.
7317 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7318 * high speed. ACPI DSDT seems to map these three speeds to levels
7319 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7320 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7322 * The speeds are stored on handles
7323 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7325 * There are three default speed sets, accessible as handles:
7326 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7328 * ACPI DSDT switches which set is in use depending on various
7329 * factors.
7331 * TPACPI_FAN_WR_TPEC is also available and should be used to
7332 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7333 * but the ACPI tables just mention level 7.
7336 enum { /* Fan control constants */
7337 fan_status_offset = 0x2f, /* EC register 0x2f */
7338 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7339 * 0x84 must be read before 0x85 */
7340 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7341 bit 0 selects which fan is active */
7343 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7344 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7346 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7349 enum fan_status_access_mode {
7350 TPACPI_FAN_NONE = 0, /* No fan status or control */
7351 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7352 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7355 enum fan_control_access_mode {
7356 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7357 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7358 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7359 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7362 enum fan_control_commands {
7363 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7364 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7365 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7366 * and also watchdog cmd */
7369 static int fan_control_allowed;
7371 static enum fan_status_access_mode fan_status_access_mode;
7372 static enum fan_control_access_mode fan_control_access_mode;
7373 static enum fan_control_commands fan_control_commands;
7375 static u8 fan_control_initial_status;
7376 static u8 fan_control_desired_level;
7377 static u8 fan_control_resume_level;
7378 static int fan_watchdog_maxinterval;
7380 static struct mutex fan_mutex;
7382 static void fan_watchdog_fire(struct work_struct *ignored);
7383 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7385 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7386 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7387 "\\FSPD", /* 600e/x, 770e, 770x */
7388 ); /* all others */
7389 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7390 "JFNS", /* 770x-JL */
7391 ); /* all others */
7394 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7395 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7396 * be in auto mode (0x80).
7398 * This is corrected by any write to HFSP either by the driver, or
7399 * by the firmware.
7401 * We assume 0x07 really means auto mode while this quirk is active,
7402 * as this is far more likely than the ThinkPad being in level 7,
7403 * which is only used by the firmware during thermal emergencies.
7405 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7406 * TP-70 (T43, R52), which are known to be buggy.
7409 static void fan_quirk1_setup(void)
7411 if (fan_control_initial_status == 0x07) {
7412 printk(TPACPI_NOTICE
7413 "fan_init: initial fan status is unknown, "
7414 "assuming it is in auto mode\n");
7415 tp_features.fan_ctrl_status_undef = 1;
7419 static void fan_quirk1_handle(u8 *fan_status)
7421 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7422 if (*fan_status != fan_control_initial_status) {
7423 /* something changed the HFSP regisnter since
7424 * driver init time, so it is not undefined
7425 * anymore */
7426 tp_features.fan_ctrl_status_undef = 0;
7427 } else {
7428 /* Return most likely status. In fact, it
7429 * might be the only possible status */
7430 *fan_status = TP_EC_FAN_AUTO;
7435 /* Select main fan on X60/X61, NOOP on others */
7436 static bool fan_select_fan1(void)
7438 if (tp_features.second_fan) {
7439 u8 val;
7441 if (ec_read(fan_select_offset, &val) < 0)
7442 return false;
7443 val &= 0xFEU;
7444 if (ec_write(fan_select_offset, val) < 0)
7445 return false;
7447 return true;
7450 /* Select secondary fan on X60/X61 */
7451 static bool fan_select_fan2(void)
7453 u8 val;
7455 if (!tp_features.second_fan)
7456 return false;
7458 if (ec_read(fan_select_offset, &val) < 0)
7459 return false;
7460 val |= 0x01U;
7461 if (ec_write(fan_select_offset, val) < 0)
7462 return false;
7464 return true;
7468 * Call with fan_mutex held
7470 static void fan_update_desired_level(u8 status)
7472 if ((status &
7473 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7474 if (status > 7)
7475 fan_control_desired_level = 7;
7476 else
7477 fan_control_desired_level = status;
7481 static int fan_get_status(u8 *status)
7483 u8 s;
7485 /* TODO:
7486 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7488 switch (fan_status_access_mode) {
7489 case TPACPI_FAN_RD_ACPI_GFAN:
7490 /* 570, 600e/x, 770e, 770x */
7492 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7493 return -EIO;
7495 if (likely(status))
7496 *status = s & 0x07;
7498 break;
7500 case TPACPI_FAN_RD_TPEC:
7501 /* all except 570, 600e/x, 770e, 770x */
7502 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7503 return -EIO;
7505 if (likely(status)) {
7506 *status = s;
7507 fan_quirk1_handle(status);
7510 break;
7512 default:
7513 return -ENXIO;
7516 return 0;
7519 static int fan_get_status_safe(u8 *status)
7521 int rc;
7522 u8 s;
7524 if (mutex_lock_killable(&fan_mutex))
7525 return -ERESTARTSYS;
7526 rc = fan_get_status(&s);
7527 if (!rc)
7528 fan_update_desired_level(s);
7529 mutex_unlock(&fan_mutex);
7531 if (status)
7532 *status = s;
7534 return rc;
7537 static int fan_get_speed(unsigned int *speed)
7539 u8 hi, lo;
7541 switch (fan_status_access_mode) {
7542 case TPACPI_FAN_RD_TPEC:
7543 /* all except 570, 600e/x, 770e, 770x */
7544 if (unlikely(!fan_select_fan1()))
7545 return -EIO;
7546 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7547 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7548 return -EIO;
7550 if (likely(speed))
7551 *speed = (hi << 8) | lo;
7553 break;
7555 default:
7556 return -ENXIO;
7559 return 0;
7562 static int fan2_get_speed(unsigned int *speed)
7564 u8 hi, lo;
7565 bool rc;
7567 switch (fan_status_access_mode) {
7568 case TPACPI_FAN_RD_TPEC:
7569 /* all except 570, 600e/x, 770e, 770x */
7570 if (unlikely(!fan_select_fan2()))
7571 return -EIO;
7572 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7573 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7574 fan_select_fan1(); /* play it safe */
7575 if (rc)
7576 return -EIO;
7578 if (likely(speed))
7579 *speed = (hi << 8) | lo;
7581 break;
7583 default:
7584 return -ENXIO;
7587 return 0;
7590 static int fan_set_level(int level)
7592 if (!fan_control_allowed)
7593 return -EPERM;
7595 switch (fan_control_access_mode) {
7596 case TPACPI_FAN_WR_ACPI_SFAN:
7597 if (level >= 0 && level <= 7) {
7598 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7599 return -EIO;
7600 } else
7601 return -EINVAL;
7602 break;
7604 case TPACPI_FAN_WR_ACPI_FANS:
7605 case TPACPI_FAN_WR_TPEC:
7606 if (!(level & TP_EC_FAN_AUTO) &&
7607 !(level & TP_EC_FAN_FULLSPEED) &&
7608 ((level < 0) || (level > 7)))
7609 return -EINVAL;
7611 /* safety net should the EC not support AUTO
7612 * or FULLSPEED mode bits and just ignore them */
7613 if (level & TP_EC_FAN_FULLSPEED)
7614 level |= 7; /* safety min speed 7 */
7615 else if (level & TP_EC_FAN_AUTO)
7616 level |= 4; /* safety min speed 4 */
7618 if (!acpi_ec_write(fan_status_offset, level))
7619 return -EIO;
7620 else
7621 tp_features.fan_ctrl_status_undef = 0;
7622 break;
7624 default:
7625 return -ENXIO;
7628 vdbg_printk(TPACPI_DBG_FAN,
7629 "fan control: set fan control register to 0x%02x\n", level);
7630 return 0;
7633 static int fan_set_level_safe(int level)
7635 int rc;
7637 if (!fan_control_allowed)
7638 return -EPERM;
7640 if (mutex_lock_killable(&fan_mutex))
7641 return -ERESTARTSYS;
7643 if (level == TPACPI_FAN_LAST_LEVEL)
7644 level = fan_control_desired_level;
7646 rc = fan_set_level(level);
7647 if (!rc)
7648 fan_update_desired_level(level);
7650 mutex_unlock(&fan_mutex);
7651 return rc;
7654 static int fan_set_enable(void)
7656 u8 s;
7657 int rc;
7659 if (!fan_control_allowed)
7660 return -EPERM;
7662 if (mutex_lock_killable(&fan_mutex))
7663 return -ERESTARTSYS;
7665 switch (fan_control_access_mode) {
7666 case TPACPI_FAN_WR_ACPI_FANS:
7667 case TPACPI_FAN_WR_TPEC:
7668 rc = fan_get_status(&s);
7669 if (rc < 0)
7670 break;
7672 /* Don't go out of emergency fan mode */
7673 if (s != 7) {
7674 s &= 0x07;
7675 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7678 if (!acpi_ec_write(fan_status_offset, s))
7679 rc = -EIO;
7680 else {
7681 tp_features.fan_ctrl_status_undef = 0;
7682 rc = 0;
7684 break;
7686 case TPACPI_FAN_WR_ACPI_SFAN:
7687 rc = fan_get_status(&s);
7688 if (rc < 0)
7689 break;
7691 s &= 0x07;
7693 /* Set fan to at least level 4 */
7694 s |= 4;
7696 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7697 rc = -EIO;
7698 else
7699 rc = 0;
7700 break;
7702 default:
7703 rc = -ENXIO;
7706 mutex_unlock(&fan_mutex);
7708 if (!rc)
7709 vdbg_printk(TPACPI_DBG_FAN,
7710 "fan control: set fan control register to 0x%02x\n",
7712 return rc;
7715 static int fan_set_disable(void)
7717 int rc;
7719 if (!fan_control_allowed)
7720 return -EPERM;
7722 if (mutex_lock_killable(&fan_mutex))
7723 return -ERESTARTSYS;
7725 rc = 0;
7726 switch (fan_control_access_mode) {
7727 case TPACPI_FAN_WR_ACPI_FANS:
7728 case TPACPI_FAN_WR_TPEC:
7729 if (!acpi_ec_write(fan_status_offset, 0x00))
7730 rc = -EIO;
7731 else {
7732 fan_control_desired_level = 0;
7733 tp_features.fan_ctrl_status_undef = 0;
7735 break;
7737 case TPACPI_FAN_WR_ACPI_SFAN:
7738 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7739 rc = -EIO;
7740 else
7741 fan_control_desired_level = 0;
7742 break;
7744 default:
7745 rc = -ENXIO;
7748 if (!rc)
7749 vdbg_printk(TPACPI_DBG_FAN,
7750 "fan control: set fan control register to 0\n");
7752 mutex_unlock(&fan_mutex);
7753 return rc;
7756 static int fan_set_speed(int speed)
7758 int rc;
7760 if (!fan_control_allowed)
7761 return -EPERM;
7763 if (mutex_lock_killable(&fan_mutex))
7764 return -ERESTARTSYS;
7766 rc = 0;
7767 switch (fan_control_access_mode) {
7768 case TPACPI_FAN_WR_ACPI_FANS:
7769 if (speed >= 0 && speed <= 65535) {
7770 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7771 speed, speed, speed))
7772 rc = -EIO;
7773 } else
7774 rc = -EINVAL;
7775 break;
7777 default:
7778 rc = -ENXIO;
7781 mutex_unlock(&fan_mutex);
7782 return rc;
7785 static void fan_watchdog_reset(void)
7787 static int fan_watchdog_active;
7789 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7790 return;
7792 if (fan_watchdog_active)
7793 cancel_delayed_work(&fan_watchdog_task);
7795 if (fan_watchdog_maxinterval > 0 &&
7796 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7797 fan_watchdog_active = 1;
7798 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7799 msecs_to_jiffies(fan_watchdog_maxinterval
7800 * 1000))) {
7801 printk(TPACPI_ERR
7802 "failed to queue the fan watchdog, "
7803 "watchdog will not trigger\n");
7805 } else
7806 fan_watchdog_active = 0;
7809 static void fan_watchdog_fire(struct work_struct *ignored)
7811 int rc;
7813 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7814 return;
7816 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7817 rc = fan_set_enable();
7818 if (rc < 0) {
7819 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7820 "will try again later...\n", -rc);
7821 /* reschedule for later */
7822 fan_watchdog_reset();
7827 * SYSFS fan layout: hwmon compatible (device)
7829 * pwm*_enable:
7830 * 0: "disengaged" mode
7831 * 1: manual mode
7832 * 2: native EC "auto" mode (recommended, hardware default)
7834 * pwm*: set speed in manual mode, ignored otherwise.
7835 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7836 * interpolation.
7838 * fan*_input: tachometer reading, RPM
7841 * SYSFS fan layout: extensions
7843 * fan_watchdog (driver):
7844 * fan watchdog interval in seconds, 0 disables (default), max 120
7847 /* sysfs fan pwm1_enable ----------------------------------------------- */
7848 static ssize_t fan_pwm1_enable_show(struct device *dev,
7849 struct device_attribute *attr,
7850 char *buf)
7852 int res, mode;
7853 u8 status;
7855 res = fan_get_status_safe(&status);
7856 if (res)
7857 return res;
7859 if (status & TP_EC_FAN_FULLSPEED) {
7860 mode = 0;
7861 } else if (status & TP_EC_FAN_AUTO) {
7862 mode = 2;
7863 } else
7864 mode = 1;
7866 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7869 static ssize_t fan_pwm1_enable_store(struct device *dev,
7870 struct device_attribute *attr,
7871 const char *buf, size_t count)
7873 unsigned long t;
7874 int res, level;
7876 if (parse_strtoul(buf, 2, &t))
7877 return -EINVAL;
7879 tpacpi_disclose_usertask("hwmon pwm1_enable",
7880 "set fan mode to %lu\n", t);
7882 switch (t) {
7883 case 0:
7884 level = TP_EC_FAN_FULLSPEED;
7885 break;
7886 case 1:
7887 level = TPACPI_FAN_LAST_LEVEL;
7888 break;
7889 case 2:
7890 level = TP_EC_FAN_AUTO;
7891 break;
7892 case 3:
7893 /* reserved for software-controlled auto mode */
7894 return -ENOSYS;
7895 default:
7896 return -EINVAL;
7899 res = fan_set_level_safe(level);
7900 if (res == -ENXIO)
7901 return -EINVAL;
7902 else if (res < 0)
7903 return res;
7905 fan_watchdog_reset();
7907 return count;
7910 static struct device_attribute dev_attr_fan_pwm1_enable =
7911 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7912 fan_pwm1_enable_show, fan_pwm1_enable_store);
7914 /* sysfs fan pwm1 ------------------------------------------------------ */
7915 static ssize_t fan_pwm1_show(struct device *dev,
7916 struct device_attribute *attr,
7917 char *buf)
7919 int res;
7920 u8 status;
7922 res = fan_get_status_safe(&status);
7923 if (res)
7924 return res;
7926 if ((status &
7927 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7928 status = fan_control_desired_level;
7930 if (status > 7)
7931 status = 7;
7933 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7936 static ssize_t fan_pwm1_store(struct device *dev,
7937 struct device_attribute *attr,
7938 const char *buf, size_t count)
7940 unsigned long s;
7941 int rc;
7942 u8 status, newlevel;
7944 if (parse_strtoul(buf, 255, &s))
7945 return -EINVAL;
7947 tpacpi_disclose_usertask("hwmon pwm1",
7948 "set fan speed to %lu\n", s);
7950 /* scale down from 0-255 to 0-7 */
7951 newlevel = (s >> 5) & 0x07;
7953 if (mutex_lock_killable(&fan_mutex))
7954 return -ERESTARTSYS;
7956 rc = fan_get_status(&status);
7957 if (!rc && (status &
7958 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7959 rc = fan_set_level(newlevel);
7960 if (rc == -ENXIO)
7961 rc = -EINVAL;
7962 else if (!rc) {
7963 fan_update_desired_level(newlevel);
7964 fan_watchdog_reset();
7968 mutex_unlock(&fan_mutex);
7969 return (rc)? rc : count;
7972 static struct device_attribute dev_attr_fan_pwm1 =
7973 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7974 fan_pwm1_show, fan_pwm1_store);
7976 /* sysfs fan fan1_input ------------------------------------------------ */
7977 static ssize_t fan_fan1_input_show(struct device *dev,
7978 struct device_attribute *attr,
7979 char *buf)
7981 int res;
7982 unsigned int speed;
7984 res = fan_get_speed(&speed);
7985 if (res < 0)
7986 return res;
7988 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7991 static struct device_attribute dev_attr_fan_fan1_input =
7992 __ATTR(fan1_input, S_IRUGO,
7993 fan_fan1_input_show, NULL);
7995 /* sysfs fan fan2_input ------------------------------------------------ */
7996 static ssize_t fan_fan2_input_show(struct device *dev,
7997 struct device_attribute *attr,
7998 char *buf)
8000 int res;
8001 unsigned int speed;
8003 res = fan2_get_speed(&speed);
8004 if (res < 0)
8005 return res;
8007 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8010 static struct device_attribute dev_attr_fan_fan2_input =
8011 __ATTR(fan2_input, S_IRUGO,
8012 fan_fan2_input_show, NULL);
8014 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8015 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8016 char *buf)
8018 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8021 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8022 const char *buf, size_t count)
8024 unsigned long t;
8026 if (parse_strtoul(buf, 120, &t))
8027 return -EINVAL;
8029 if (!fan_control_allowed)
8030 return -EPERM;
8032 fan_watchdog_maxinterval = t;
8033 fan_watchdog_reset();
8035 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8037 return count;
8040 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8041 fan_fan_watchdog_show, fan_fan_watchdog_store);
8043 /* --------------------------------------------------------------------- */
8044 static struct attribute *fan_attributes[] = {
8045 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8046 &dev_attr_fan_fan1_input.attr,
8047 NULL, /* for fan2_input */
8048 NULL
8051 static const struct attribute_group fan_attr_group = {
8052 .attrs = fan_attributes,
8055 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8056 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8058 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8059 { .vendor = PCI_VENDOR_ID_IBM, \
8060 .bios = TPACPI_MATCH_ANY, \
8061 .ec = TPID(__id1, __id2), \
8062 .quirks = __quirks }
8064 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8065 { .vendor = PCI_VENDOR_ID_LENOVO, \
8066 .bios = TPACPI_MATCH_ANY, \
8067 .ec = TPID(__id1, __id2), \
8068 .quirks = __quirks }
8070 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8071 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8072 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8073 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8074 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8075 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8078 #undef TPACPI_FAN_QL
8079 #undef TPACPI_FAN_QI
8081 static int __init fan_init(struct ibm_init_struct *iibm)
8083 int rc;
8084 unsigned long quirks;
8086 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8087 "initializing fan subdriver\n");
8089 mutex_init(&fan_mutex);
8090 fan_status_access_mode = TPACPI_FAN_NONE;
8091 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8092 fan_control_commands = 0;
8093 fan_watchdog_maxinterval = 0;
8094 tp_features.fan_ctrl_status_undef = 0;
8095 tp_features.second_fan = 0;
8096 fan_control_desired_level = 7;
8098 if (tpacpi_is_ibm()) {
8099 TPACPI_ACPIHANDLE_INIT(fans);
8100 TPACPI_ACPIHANDLE_INIT(gfan);
8101 TPACPI_ACPIHANDLE_INIT(sfan);
8104 quirks = tpacpi_check_quirks(fan_quirk_table,
8105 ARRAY_SIZE(fan_quirk_table));
8107 if (gfan_handle) {
8108 /* 570, 600e/x, 770e, 770x */
8109 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8110 } else {
8111 /* all other ThinkPads: note that even old-style
8112 * ThinkPad ECs supports the fan control register */
8113 if (likely(acpi_ec_read(fan_status_offset,
8114 &fan_control_initial_status))) {
8115 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8116 if (quirks & TPACPI_FAN_Q1)
8117 fan_quirk1_setup();
8118 if (quirks & TPACPI_FAN_2FAN) {
8119 tp_features.second_fan = 1;
8120 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8121 "secondary fan support enabled\n");
8123 } else {
8124 printk(TPACPI_ERR
8125 "ThinkPad ACPI EC access misbehaving, "
8126 "fan status and control unavailable\n");
8127 return 1;
8131 if (sfan_handle) {
8132 /* 570, 770x-JL */
8133 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8134 fan_control_commands |=
8135 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8136 } else {
8137 if (!gfan_handle) {
8138 /* gfan without sfan means no fan control */
8139 /* all other models implement TP EC 0x2f control */
8141 if (fans_handle) {
8142 /* X31, X40, X41 */
8143 fan_control_access_mode =
8144 TPACPI_FAN_WR_ACPI_FANS;
8145 fan_control_commands |=
8146 TPACPI_FAN_CMD_SPEED |
8147 TPACPI_FAN_CMD_LEVEL |
8148 TPACPI_FAN_CMD_ENABLE;
8149 } else {
8150 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8151 fan_control_commands |=
8152 TPACPI_FAN_CMD_LEVEL |
8153 TPACPI_FAN_CMD_ENABLE;
8158 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8159 "fan is %s, modes %d, %d\n",
8160 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8161 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8162 fan_status_access_mode, fan_control_access_mode);
8164 /* fan control master switch */
8165 if (!fan_control_allowed) {
8166 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8167 fan_control_commands = 0;
8168 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8169 "fan control features disabled by parameter\n");
8172 /* update fan_control_desired_level */
8173 if (fan_status_access_mode != TPACPI_FAN_NONE)
8174 fan_get_status_safe(NULL);
8176 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8177 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8178 if (tp_features.second_fan) {
8179 /* attach second fan tachometer */
8180 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8181 &dev_attr_fan_fan2_input.attr;
8183 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8184 &fan_attr_group);
8185 if (rc < 0)
8186 return rc;
8188 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8189 &driver_attr_fan_watchdog);
8190 if (rc < 0) {
8191 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8192 &fan_attr_group);
8193 return rc;
8195 return 0;
8196 } else
8197 return 1;
8200 static void fan_exit(void)
8202 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8203 "cancelling any pending fan watchdog tasks\n");
8205 /* FIXME: can we really do this unconditionally? */
8206 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8207 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8208 &driver_attr_fan_watchdog);
8210 cancel_delayed_work(&fan_watchdog_task);
8211 flush_workqueue(tpacpi_wq);
8214 static void fan_suspend(pm_message_t state)
8216 int rc;
8218 if (!fan_control_allowed)
8219 return;
8221 /* Store fan status in cache */
8222 fan_control_resume_level = 0;
8223 rc = fan_get_status_safe(&fan_control_resume_level);
8224 if (rc < 0)
8225 printk(TPACPI_NOTICE
8226 "failed to read fan level for later "
8227 "restore during resume: %d\n", rc);
8229 /* if it is undefined, don't attempt to restore it.
8230 * KEEP THIS LAST */
8231 if (tp_features.fan_ctrl_status_undef)
8232 fan_control_resume_level = 0;
8235 static void fan_resume(void)
8237 u8 current_level = 7;
8238 bool do_set = false;
8239 int rc;
8241 /* DSDT *always* updates status on resume */
8242 tp_features.fan_ctrl_status_undef = 0;
8244 if (!fan_control_allowed ||
8245 !fan_control_resume_level ||
8246 (fan_get_status_safe(&current_level) < 0))
8247 return;
8249 switch (fan_control_access_mode) {
8250 case TPACPI_FAN_WR_ACPI_SFAN:
8251 /* never decrease fan level */
8252 do_set = (fan_control_resume_level > current_level);
8253 break;
8254 case TPACPI_FAN_WR_ACPI_FANS:
8255 case TPACPI_FAN_WR_TPEC:
8256 /* never decrease fan level, scale is:
8257 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8259 * We expect the firmware to set either 7 or AUTO, but we
8260 * handle FULLSPEED out of paranoia.
8262 * So, we can safely only restore FULLSPEED or 7, anything
8263 * else could slow the fan. Restoring AUTO is useless, at
8264 * best that's exactly what the DSDT already set (it is the
8265 * slower it uses).
8267 * Always keep in mind that the DSDT *will* have set the
8268 * fans to what the vendor supposes is the best level. We
8269 * muck with it only to speed the fan up.
8271 if (fan_control_resume_level != 7 &&
8272 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8273 return;
8274 else
8275 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8276 (current_level != fan_control_resume_level);
8277 break;
8278 default:
8279 return;
8281 if (do_set) {
8282 printk(TPACPI_NOTICE
8283 "restoring fan level to 0x%02x\n",
8284 fan_control_resume_level);
8285 rc = fan_set_level_safe(fan_control_resume_level);
8286 if (rc < 0)
8287 printk(TPACPI_NOTICE
8288 "failed to restore fan level: %d\n", rc);
8292 static int fan_read(struct seq_file *m)
8294 int rc;
8295 u8 status;
8296 unsigned int speed = 0;
8298 switch (fan_status_access_mode) {
8299 case TPACPI_FAN_RD_ACPI_GFAN:
8300 /* 570, 600e/x, 770e, 770x */
8301 rc = fan_get_status_safe(&status);
8302 if (rc < 0)
8303 return rc;
8305 seq_printf(m, "status:\t\t%s\n"
8306 "level:\t\t%d\n",
8307 (status != 0) ? "enabled" : "disabled", status);
8308 break;
8310 case TPACPI_FAN_RD_TPEC:
8311 /* all except 570, 600e/x, 770e, 770x */
8312 rc = fan_get_status_safe(&status);
8313 if (rc < 0)
8314 return rc;
8316 seq_printf(m, "status:\t\t%s\n",
8317 (status != 0) ? "enabled" : "disabled");
8319 rc = fan_get_speed(&speed);
8320 if (rc < 0)
8321 return rc;
8323 seq_printf(m, "speed:\t\t%d\n", speed);
8325 if (status & TP_EC_FAN_FULLSPEED)
8326 /* Disengaged mode takes precedence */
8327 seq_printf(m, "level:\t\tdisengaged\n");
8328 else if (status & TP_EC_FAN_AUTO)
8329 seq_printf(m, "level:\t\tauto\n");
8330 else
8331 seq_printf(m, "level:\t\t%d\n", status);
8332 break;
8334 case TPACPI_FAN_NONE:
8335 default:
8336 seq_printf(m, "status:\t\tnot supported\n");
8339 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8340 seq_printf(m, "commands:\tlevel <level>");
8342 switch (fan_control_access_mode) {
8343 case TPACPI_FAN_WR_ACPI_SFAN:
8344 seq_printf(m, " (<level> is 0-7)\n");
8345 break;
8347 default:
8348 seq_printf(m, " (<level> is 0-7, "
8349 "auto, disengaged, full-speed)\n");
8350 break;
8354 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8355 seq_printf(m, "commands:\tenable, disable\n"
8356 "commands:\twatchdog <timeout> (<timeout> "
8357 "is 0 (off), 1-120 (seconds))\n");
8359 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8360 seq_printf(m, "commands:\tspeed <speed>"
8361 " (<speed> is 0-65535)\n");
8363 return 0;
8366 static int fan_write_cmd_level(const char *cmd, int *rc)
8368 int level;
8370 if (strlencmp(cmd, "level auto") == 0)
8371 level = TP_EC_FAN_AUTO;
8372 else if ((strlencmp(cmd, "level disengaged") == 0) |
8373 (strlencmp(cmd, "level full-speed") == 0))
8374 level = TP_EC_FAN_FULLSPEED;
8375 else if (sscanf(cmd, "level %d", &level) != 1)
8376 return 0;
8378 *rc = fan_set_level_safe(level);
8379 if (*rc == -ENXIO)
8380 printk(TPACPI_ERR "level command accepted for unsupported "
8381 "access mode %d", fan_control_access_mode);
8382 else if (!*rc)
8383 tpacpi_disclose_usertask("procfs fan",
8384 "set level to %d\n", level);
8386 return 1;
8389 static int fan_write_cmd_enable(const char *cmd, int *rc)
8391 if (strlencmp(cmd, "enable") != 0)
8392 return 0;
8394 *rc = fan_set_enable();
8395 if (*rc == -ENXIO)
8396 printk(TPACPI_ERR "enable command accepted for unsupported "
8397 "access mode %d", fan_control_access_mode);
8398 else if (!*rc)
8399 tpacpi_disclose_usertask("procfs fan", "enable\n");
8401 return 1;
8404 static int fan_write_cmd_disable(const char *cmd, int *rc)
8406 if (strlencmp(cmd, "disable") != 0)
8407 return 0;
8409 *rc = fan_set_disable();
8410 if (*rc == -ENXIO)
8411 printk(TPACPI_ERR "disable command accepted for unsupported "
8412 "access mode %d", fan_control_access_mode);
8413 else if (!*rc)
8414 tpacpi_disclose_usertask("procfs fan", "disable\n");
8416 return 1;
8419 static int fan_write_cmd_speed(const char *cmd, int *rc)
8421 int speed;
8423 /* TODO:
8424 * Support speed <low> <medium> <high> ? */
8426 if (sscanf(cmd, "speed %d", &speed) != 1)
8427 return 0;
8429 *rc = fan_set_speed(speed);
8430 if (*rc == -ENXIO)
8431 printk(TPACPI_ERR "speed command accepted for unsupported "
8432 "access mode %d", fan_control_access_mode);
8433 else if (!*rc)
8434 tpacpi_disclose_usertask("procfs fan",
8435 "set speed to %d\n", speed);
8437 return 1;
8440 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8442 int interval;
8444 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8445 return 0;
8447 if (interval < 0 || interval > 120)
8448 *rc = -EINVAL;
8449 else {
8450 fan_watchdog_maxinterval = interval;
8451 tpacpi_disclose_usertask("procfs fan",
8452 "set watchdog timer to %d\n",
8453 interval);
8456 return 1;
8459 static int fan_write(char *buf)
8461 char *cmd;
8462 int rc = 0;
8464 while (!rc && (cmd = next_cmd(&buf))) {
8465 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8466 fan_write_cmd_level(cmd, &rc)) &&
8467 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8468 (fan_write_cmd_enable(cmd, &rc) ||
8469 fan_write_cmd_disable(cmd, &rc) ||
8470 fan_write_cmd_watchdog(cmd, &rc))) &&
8471 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8472 fan_write_cmd_speed(cmd, &rc))
8474 rc = -EINVAL;
8475 else if (!rc)
8476 fan_watchdog_reset();
8479 return rc;
8482 static struct ibm_struct fan_driver_data = {
8483 .name = "fan",
8484 .read = fan_read,
8485 .write = fan_write,
8486 .exit = fan_exit,
8487 .suspend = fan_suspend,
8488 .resume = fan_resume,
8491 /****************************************************************************
8492 ****************************************************************************
8494 * Infrastructure
8496 ****************************************************************************
8497 ****************************************************************************/
8500 * HKEY event callout for other subdrivers go here
8501 * (yes, it is ugly, but it is quick, safe, and gets the job done
8503 static void tpacpi_driver_event(const unsigned int hkey_event)
8505 if (ibm_backlight_device) {
8506 switch (hkey_event) {
8507 case TP_HKEY_EV_BRGHT_UP:
8508 case TP_HKEY_EV_BRGHT_DOWN:
8509 tpacpi_brightness_notify_change();
8512 if (alsa_card) {
8513 switch (hkey_event) {
8514 case TP_HKEY_EV_VOL_UP:
8515 case TP_HKEY_EV_VOL_DOWN:
8516 case TP_HKEY_EV_VOL_MUTE:
8517 volume_alsa_notify_change();
8522 static void hotkey_driver_event(const unsigned int scancode)
8524 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8527 /* sysfs name ---------------------------------------------------------- */
8528 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8529 struct device_attribute *attr,
8530 char *buf)
8532 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8535 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8536 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8538 /* --------------------------------------------------------------------- */
8540 /* /proc support */
8541 static struct proc_dir_entry *proc_dir;
8544 * Module and infrastructure proble, init and exit handling
8547 static int force_load;
8549 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8550 static const char * __init str_supported(int is_supported)
8552 static char text_unsupported[] __initdata = "not supported";
8554 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8556 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8558 static void ibm_exit(struct ibm_struct *ibm)
8560 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8562 list_del_init(&ibm->all_drivers);
8564 if (ibm->flags.acpi_notify_installed) {
8565 dbg_printk(TPACPI_DBG_EXIT,
8566 "%s: acpi_remove_notify_handler\n", ibm->name);
8567 BUG_ON(!ibm->acpi);
8568 acpi_remove_notify_handler(*ibm->acpi->handle,
8569 ibm->acpi->type,
8570 dispatch_acpi_notify);
8571 ibm->flags.acpi_notify_installed = 0;
8572 ibm->flags.acpi_notify_installed = 0;
8575 if (ibm->flags.proc_created) {
8576 dbg_printk(TPACPI_DBG_EXIT,
8577 "%s: remove_proc_entry\n", ibm->name);
8578 remove_proc_entry(ibm->name, proc_dir);
8579 ibm->flags.proc_created = 0;
8582 if (ibm->flags.acpi_driver_registered) {
8583 dbg_printk(TPACPI_DBG_EXIT,
8584 "%s: acpi_bus_unregister_driver\n", ibm->name);
8585 BUG_ON(!ibm->acpi);
8586 acpi_bus_unregister_driver(ibm->acpi->driver);
8587 kfree(ibm->acpi->driver);
8588 ibm->acpi->driver = NULL;
8589 ibm->flags.acpi_driver_registered = 0;
8592 if (ibm->flags.init_called && ibm->exit) {
8593 ibm->exit();
8594 ibm->flags.init_called = 0;
8597 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8600 static int __init ibm_init(struct ibm_init_struct *iibm)
8602 int ret;
8603 struct ibm_struct *ibm = iibm->data;
8604 struct proc_dir_entry *entry;
8606 BUG_ON(ibm == NULL);
8608 INIT_LIST_HEAD(&ibm->all_drivers);
8610 if (ibm->flags.experimental && !experimental)
8611 return 0;
8613 dbg_printk(TPACPI_DBG_INIT,
8614 "probing for %s\n", ibm->name);
8616 if (iibm->init) {
8617 ret = iibm->init(iibm);
8618 if (ret > 0)
8619 return 0; /* probe failed */
8620 if (ret)
8621 return ret;
8623 ibm->flags.init_called = 1;
8626 if (ibm->acpi) {
8627 if (ibm->acpi->hid) {
8628 ret = register_tpacpi_subdriver(ibm);
8629 if (ret)
8630 goto err_out;
8633 if (ibm->acpi->notify) {
8634 ret = setup_acpi_notify(ibm);
8635 if (ret == -ENODEV) {
8636 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8637 ibm->name);
8638 ret = 0;
8639 goto err_out;
8641 if (ret < 0)
8642 goto err_out;
8646 dbg_printk(TPACPI_DBG_INIT,
8647 "%s installed\n", ibm->name);
8649 if (ibm->read) {
8650 mode_t mode = iibm->base_procfs_mode;
8652 if (!mode)
8653 mode = S_IRUGO;
8654 if (ibm->write)
8655 mode |= S_IWUSR;
8656 entry = proc_create_data(ibm->name, mode, proc_dir,
8657 &dispatch_proc_fops, ibm);
8658 if (!entry) {
8659 printk(TPACPI_ERR "unable to create proc entry %s\n",
8660 ibm->name);
8661 ret = -ENODEV;
8662 goto err_out;
8664 ibm->flags.proc_created = 1;
8667 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8669 return 0;
8671 err_out:
8672 dbg_printk(TPACPI_DBG_INIT,
8673 "%s: at error exit path with result %d\n",
8674 ibm->name, ret);
8676 ibm_exit(ibm);
8677 return (ret < 0)? ret : 0;
8680 /* Probing */
8682 static bool __pure __init tpacpi_is_fw_digit(const char c)
8684 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8687 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8688 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8689 const char t)
8691 return s && strlen(s) >= 8 &&
8692 tpacpi_is_fw_digit(s[0]) &&
8693 tpacpi_is_fw_digit(s[1]) &&
8694 s[2] == t && s[3] == 'T' &&
8695 tpacpi_is_fw_digit(s[4]) &&
8696 tpacpi_is_fw_digit(s[5]) &&
8697 s[6] == 'W' && s[7] == 'W';
8700 /* returns 0 - probe ok, or < 0 - probe error.
8701 * Probe ok doesn't mean thinkpad found.
8702 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8703 static int __must_check __init get_thinkpad_model_data(
8704 struct thinkpad_id_data *tp)
8706 const struct dmi_device *dev = NULL;
8707 char ec_fw_string[18];
8708 char const *s;
8710 if (!tp)
8711 return -EINVAL;
8713 memset(tp, 0, sizeof(*tp));
8715 if (dmi_name_in_vendors("IBM"))
8716 tp->vendor = PCI_VENDOR_ID_IBM;
8717 else if (dmi_name_in_vendors("LENOVO"))
8718 tp->vendor = PCI_VENDOR_ID_LENOVO;
8719 else
8720 return 0;
8722 s = dmi_get_system_info(DMI_BIOS_VERSION);
8723 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8724 if (s && !tp->bios_version_str)
8725 return -ENOMEM;
8727 /* Really ancient ThinkPad 240X will fail this, which is fine */
8728 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8729 return 0;
8731 tp->bios_model = tp->bios_version_str[0]
8732 | (tp->bios_version_str[1] << 8);
8733 tp->bios_release = (tp->bios_version_str[4] << 8)
8734 | tp->bios_version_str[5];
8737 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8738 * X32 or newer, all Z series; Some models must have an
8739 * up-to-date BIOS or they will not be detected.
8741 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8743 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8744 if (sscanf(dev->name,
8745 "IBM ThinkPad Embedded Controller -[%17c",
8746 ec_fw_string) == 1) {
8747 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8748 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8750 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8751 if (!tp->ec_version_str)
8752 return -ENOMEM;
8754 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8755 tp->ec_model = ec_fw_string[0]
8756 | (ec_fw_string[1] << 8);
8757 tp->ec_release = (ec_fw_string[4] << 8)
8758 | ec_fw_string[5];
8759 } else {
8760 printk(TPACPI_NOTICE
8761 "ThinkPad firmware release %s "
8762 "doesn't match the known patterns\n",
8763 ec_fw_string);
8764 printk(TPACPI_NOTICE
8765 "please report this to %s\n",
8766 TPACPI_MAIL);
8768 break;
8772 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8773 if (s && !strnicmp(s, "ThinkPad", 8)) {
8774 tp->model_str = kstrdup(s, GFP_KERNEL);
8775 if (!tp->model_str)
8776 return -ENOMEM;
8779 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8780 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8781 if (s && !tp->nummodel_str)
8782 return -ENOMEM;
8784 return 0;
8787 static int __init probe_for_thinkpad(void)
8789 int is_thinkpad;
8791 if (acpi_disabled)
8792 return -ENODEV;
8794 /* It would be dangerous to run the driver in this case */
8795 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8796 return -ENODEV;
8799 * Non-ancient models have better DMI tagging, but very old models
8800 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8802 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8803 (thinkpad_id.ec_model != 0) ||
8804 tpacpi_is_fw_known();
8806 /* The EC handler is required */
8807 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8808 if (!ec_handle) {
8809 if (is_thinkpad)
8810 printk(TPACPI_ERR
8811 "Not yet supported ThinkPad detected!\n");
8812 return -ENODEV;
8815 if (!is_thinkpad && !force_load)
8816 return -ENODEV;
8818 return 0;
8821 static void __init thinkpad_acpi_init_banner(void)
8823 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8824 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8826 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8827 (thinkpad_id.bios_version_str) ?
8828 thinkpad_id.bios_version_str : "unknown",
8829 (thinkpad_id.ec_version_str) ?
8830 thinkpad_id.ec_version_str : "unknown");
8832 BUG_ON(!thinkpad_id.vendor);
8834 if (thinkpad_id.model_str)
8835 printk(TPACPI_INFO "%s %s, model %s\n",
8836 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8837 "IBM" : ((thinkpad_id.vendor ==
8838 PCI_VENDOR_ID_LENOVO) ?
8839 "Lenovo" : "Unknown vendor"),
8840 thinkpad_id.model_str,
8841 (thinkpad_id.nummodel_str) ?
8842 thinkpad_id.nummodel_str : "unknown");
8845 /* Module init, exit, parameters */
8847 static struct ibm_init_struct ibms_init[] __initdata = {
8849 .data = &thinkpad_acpi_driver_data,
8852 .init = hotkey_init,
8853 .data = &hotkey_driver_data,
8856 .init = bluetooth_init,
8857 .data = &bluetooth_driver_data,
8860 .init = wan_init,
8861 .data = &wan_driver_data,
8864 .init = uwb_init,
8865 .data = &uwb_driver_data,
8867 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8869 .init = video_init,
8870 .base_procfs_mode = S_IRUSR,
8871 .data = &video_driver_data,
8873 #endif
8875 .init = light_init,
8876 .data = &light_driver_data,
8879 .init = cmos_init,
8880 .data = &cmos_driver_data,
8883 .init = led_init,
8884 .data = &led_driver_data,
8887 .init = beep_init,
8888 .data = &beep_driver_data,
8891 .init = thermal_init,
8892 .data = &thermal_driver_data,
8895 .data = &ecdump_driver_data,
8898 .init = brightness_init,
8899 .data = &brightness_driver_data,
8902 .init = volume_init,
8903 .data = &volume_driver_data,
8906 .init = fan_init,
8907 .data = &fan_driver_data,
8911 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8913 unsigned int i;
8914 struct ibm_struct *ibm;
8916 if (!kp || !kp->name || !val)
8917 return -EINVAL;
8919 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8920 ibm = ibms_init[i].data;
8921 WARN_ON(ibm == NULL);
8923 if (!ibm || !ibm->name)
8924 continue;
8926 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8927 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8928 return -ENOSPC;
8929 strcpy(ibms_init[i].param, val);
8930 strcat(ibms_init[i].param, ",");
8931 return 0;
8935 return -EINVAL;
8938 module_param(experimental, int, 0444);
8939 MODULE_PARM_DESC(experimental,
8940 "Enables experimental features when non-zero");
8942 module_param_named(debug, dbg_level, uint, 0);
8943 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8945 module_param(force_load, bool, 0444);
8946 MODULE_PARM_DESC(force_load,
8947 "Attempts to load the driver even on a "
8948 "mis-identified ThinkPad when true");
8950 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8951 MODULE_PARM_DESC(fan_control,
8952 "Enables setting fan parameters features when true");
8954 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8955 MODULE_PARM_DESC(brightness_mode,
8956 "Selects brightness control strategy: "
8957 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8959 module_param(brightness_enable, uint, 0444);
8960 MODULE_PARM_DESC(brightness_enable,
8961 "Enables backlight control when 1, disables when 0");
8963 module_param(hotkey_report_mode, uint, 0444);
8964 MODULE_PARM_DESC(hotkey_report_mode,
8965 "used for backwards compatibility with userspace, "
8966 "see documentation");
8968 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8969 module_param_named(volume_mode, volume_mode, uint, 0444);
8970 MODULE_PARM_DESC(volume_mode,
8971 "Selects volume control strategy: "
8972 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8974 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8975 MODULE_PARM_DESC(volume_capabilities,
8976 "Selects the mixer capabilites: "
8977 "0=auto, 1=volume and mute, 2=mute only");
8979 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8980 MODULE_PARM_DESC(volume_control,
8981 "Enables software override for the console audio "
8982 "control when true");
8984 /* ALSA module API parameters */
8985 module_param_named(index, alsa_index, int, 0444);
8986 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8987 module_param_named(id, alsa_id, charp, 0444);
8988 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8989 module_param_named(enable, alsa_enable, bool, 0444);
8990 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8991 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8993 #define TPACPI_PARAM(feature) \
8994 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8995 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8996 "at module load, see documentation")
8998 TPACPI_PARAM(hotkey);
8999 TPACPI_PARAM(bluetooth);
9000 TPACPI_PARAM(video);
9001 TPACPI_PARAM(light);
9002 TPACPI_PARAM(cmos);
9003 TPACPI_PARAM(led);
9004 TPACPI_PARAM(beep);
9005 TPACPI_PARAM(ecdump);
9006 TPACPI_PARAM(brightness);
9007 TPACPI_PARAM(volume);
9008 TPACPI_PARAM(fan);
9010 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9011 module_param(dbg_wlswemul, uint, 0444);
9012 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9013 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9014 MODULE_PARM_DESC(wlsw_state,
9015 "Initial state of the emulated WLSW switch");
9017 module_param(dbg_bluetoothemul, uint, 0444);
9018 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9019 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9020 MODULE_PARM_DESC(bluetooth_state,
9021 "Initial state of the emulated bluetooth switch");
9023 module_param(dbg_wwanemul, uint, 0444);
9024 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9025 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9026 MODULE_PARM_DESC(wwan_state,
9027 "Initial state of the emulated WWAN switch");
9029 module_param(dbg_uwbemul, uint, 0444);
9030 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9031 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9032 MODULE_PARM_DESC(uwb_state,
9033 "Initial state of the emulated UWB switch");
9034 #endif
9036 static void thinkpad_acpi_module_exit(void)
9038 struct ibm_struct *ibm, *itmp;
9040 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9042 list_for_each_entry_safe_reverse(ibm, itmp,
9043 &tpacpi_all_drivers,
9044 all_drivers) {
9045 ibm_exit(ibm);
9048 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9050 if (tpacpi_inputdev) {
9051 if (tp_features.input_device_registered)
9052 input_unregister_device(tpacpi_inputdev);
9053 else
9054 input_free_device(tpacpi_inputdev);
9057 if (tpacpi_hwmon)
9058 hwmon_device_unregister(tpacpi_hwmon);
9060 if (tp_features.sensors_pdev_attrs_registered)
9061 device_remove_file(&tpacpi_sensors_pdev->dev,
9062 &dev_attr_thinkpad_acpi_pdev_name);
9063 if (tpacpi_sensors_pdev)
9064 platform_device_unregister(tpacpi_sensors_pdev);
9065 if (tpacpi_pdev)
9066 platform_device_unregister(tpacpi_pdev);
9068 if (tp_features.sensors_pdrv_attrs_registered)
9069 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9070 if (tp_features.platform_drv_attrs_registered)
9071 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9073 if (tp_features.sensors_pdrv_registered)
9074 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9076 if (tp_features.platform_drv_registered)
9077 platform_driver_unregister(&tpacpi_pdriver);
9079 if (proc_dir)
9080 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9082 if (tpacpi_wq)
9083 destroy_workqueue(tpacpi_wq);
9085 kfree(thinkpad_id.bios_version_str);
9086 kfree(thinkpad_id.ec_version_str);
9087 kfree(thinkpad_id.model_str);
9091 static int __init thinkpad_acpi_module_init(void)
9093 int ret, i;
9095 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9097 /* Parameter checking */
9098 if (hotkey_report_mode > 2)
9099 return -EINVAL;
9101 /* Driver-level probe */
9103 ret = get_thinkpad_model_data(&thinkpad_id);
9104 if (ret) {
9105 printk(TPACPI_ERR
9106 "unable to get DMI data: %d\n", ret);
9107 thinkpad_acpi_module_exit();
9108 return ret;
9110 ret = probe_for_thinkpad();
9111 if (ret) {
9112 thinkpad_acpi_module_exit();
9113 return ret;
9116 /* Driver initialization */
9118 thinkpad_acpi_init_banner();
9119 tpacpi_check_outdated_fw();
9121 TPACPI_ACPIHANDLE_INIT(ecrd);
9122 TPACPI_ACPIHANDLE_INIT(ecwr);
9124 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9125 if (!tpacpi_wq) {
9126 thinkpad_acpi_module_exit();
9127 return -ENOMEM;
9130 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9131 if (!proc_dir) {
9132 printk(TPACPI_ERR
9133 "unable to create proc dir " TPACPI_PROC_DIR);
9134 thinkpad_acpi_module_exit();
9135 return -ENODEV;
9138 ret = platform_driver_register(&tpacpi_pdriver);
9139 if (ret) {
9140 printk(TPACPI_ERR
9141 "unable to register main platform driver\n");
9142 thinkpad_acpi_module_exit();
9143 return ret;
9145 tp_features.platform_drv_registered = 1;
9147 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9148 if (ret) {
9149 printk(TPACPI_ERR
9150 "unable to register hwmon platform driver\n");
9151 thinkpad_acpi_module_exit();
9152 return ret;
9154 tp_features.sensors_pdrv_registered = 1;
9156 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9157 if (!ret) {
9158 tp_features.platform_drv_attrs_registered = 1;
9159 ret = tpacpi_create_driver_attributes(
9160 &tpacpi_hwmon_pdriver.driver);
9162 if (ret) {
9163 printk(TPACPI_ERR
9164 "unable to create sysfs driver attributes\n");
9165 thinkpad_acpi_module_exit();
9166 return ret;
9168 tp_features.sensors_pdrv_attrs_registered = 1;
9171 /* Device initialization */
9172 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9173 NULL, 0);
9174 if (IS_ERR(tpacpi_pdev)) {
9175 ret = PTR_ERR(tpacpi_pdev);
9176 tpacpi_pdev = NULL;
9177 printk(TPACPI_ERR "unable to register platform device\n");
9178 thinkpad_acpi_module_exit();
9179 return ret;
9181 tpacpi_sensors_pdev = platform_device_register_simple(
9182 TPACPI_HWMON_DRVR_NAME,
9183 -1, NULL, 0);
9184 if (IS_ERR(tpacpi_sensors_pdev)) {
9185 ret = PTR_ERR(tpacpi_sensors_pdev);
9186 tpacpi_sensors_pdev = NULL;
9187 printk(TPACPI_ERR
9188 "unable to register hwmon platform device\n");
9189 thinkpad_acpi_module_exit();
9190 return ret;
9192 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9193 &dev_attr_thinkpad_acpi_pdev_name);
9194 if (ret) {
9195 printk(TPACPI_ERR
9196 "unable to create sysfs hwmon device attributes\n");
9197 thinkpad_acpi_module_exit();
9198 return ret;
9200 tp_features.sensors_pdev_attrs_registered = 1;
9201 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9202 if (IS_ERR(tpacpi_hwmon)) {
9203 ret = PTR_ERR(tpacpi_hwmon);
9204 tpacpi_hwmon = NULL;
9205 printk(TPACPI_ERR "unable to register hwmon device\n");
9206 thinkpad_acpi_module_exit();
9207 return ret;
9209 mutex_init(&tpacpi_inputdev_send_mutex);
9210 tpacpi_inputdev = input_allocate_device();
9211 if (!tpacpi_inputdev) {
9212 printk(TPACPI_ERR "unable to allocate input device\n");
9213 thinkpad_acpi_module_exit();
9214 return -ENOMEM;
9215 } else {
9216 /* Prepare input device, but don't register */
9217 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9218 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9219 tpacpi_inputdev->id.bustype = BUS_HOST;
9220 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9221 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9222 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9223 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9226 /* Init subdriver dependencies */
9227 tpacpi_detect_brightness_capabilities();
9229 /* Init subdrivers */
9230 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9231 ret = ibm_init(&ibms_init[i]);
9232 if (ret >= 0 && *ibms_init[i].param)
9233 ret = ibms_init[i].data->write(ibms_init[i].param);
9234 if (ret < 0) {
9235 thinkpad_acpi_module_exit();
9236 return ret;
9240 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9242 ret = input_register_device(tpacpi_inputdev);
9243 if (ret < 0) {
9244 printk(TPACPI_ERR "unable to register input device\n");
9245 thinkpad_acpi_module_exit();
9246 return ret;
9247 } else {
9248 tp_features.input_device_registered = 1;
9251 return 0;
9254 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9257 * This will autoload the driver in almost every ThinkPad
9258 * in widespread use.
9260 * Only _VERY_ old models, like the 240, 240x and 570 lack
9261 * the HKEY event interface.
9263 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9266 * DMI matching for module autoloading
9268 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9269 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9271 * Only models listed in thinkwiki will be supported, so add yours
9272 * if it is not there yet.
9274 #define IBM_BIOS_MODULE_ALIAS(__type) \
9275 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9277 /* Ancient thinkpad BIOSes have to be identified by
9278 * BIOS type or model number, and there are far less
9279 * BIOS types than model numbers... */
9280 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9282 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9283 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9284 MODULE_DESCRIPTION(TPACPI_DESC);
9285 MODULE_VERSION(TPACPI_VERSION);
9286 MODULE_LICENSE("GPL");
9288 module_init(thinkpad_acpi_module_init);
9289 module_exit(thinkpad_acpi_module_exit);