ACPI Thinkpad: We must always call va_end() after va_start() but do not do so in...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob43caf8d24c491ac8c442c996a702854cff2a49ff
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>
61 #include <linux/slab.h>
63 #include <linux/nvram.h>
64 #include <linux/proc_fs.h>
65 #include <linux/seq_file.h>
66 #include <linux/sysfs.h>
67 #include <linux/backlight.h>
68 #include <linux/fb.h>
69 #include <linux/platform_device.h>
70 #include <linux/hwmon.h>
71 #include <linux/hwmon-sysfs.h>
72 #include <linux/input.h>
73 #include <linux/leds.h>
74 #include <linux/rfkill.h>
75 #include <asm/uaccess.h>
77 #include <linux/dmi.h>
78 #include <linux/jiffies.h>
79 #include <linux/workqueue.h>
81 #include <sound/core.h>
82 #include <sound/control.h>
83 #include <sound/initval.h>
85 #include <acpi/acpi_drivers.h>
87 #include <linux/pci_ids.h>
90 /* ThinkPad CMOS commands */
91 #define TP_CMOS_VOLUME_DOWN 0
92 #define TP_CMOS_VOLUME_UP 1
93 #define TP_CMOS_VOLUME_MUTE 2
94 #define TP_CMOS_BRIGHTNESS_UP 4
95 #define TP_CMOS_BRIGHTNESS_DOWN 5
96 #define TP_CMOS_THINKLIGHT_ON 12
97 #define TP_CMOS_THINKLIGHT_OFF 13
99 /* NVRAM Addresses */
100 enum tp_nvram_addr {
101 TP_NVRAM_ADDR_HK2 = 0x57,
102 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
103 TP_NVRAM_ADDR_VIDEO = 0x59,
104 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
105 TP_NVRAM_ADDR_MIXER = 0x60,
108 /* NVRAM bit masks */
109 enum {
110 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
111 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
112 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
113 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
114 TP_NVRAM_MASK_THINKLIGHT = 0x10,
115 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
116 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
117 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
118 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
119 TP_NVRAM_MASK_MUTE = 0x40,
120 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
121 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
122 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125 /* Misc NVRAM-related */
126 enum {
127 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130 /* ACPI HIDs */
131 #define TPACPI_ACPI_HKEY_HID "IBM0068"
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 va_end(ap);
593 return 0;
596 va_end(ap);
598 if (res_type != 'v') {
599 result.length = sizeof(out_obj);
600 result.pointer = &out_obj;
601 resultp = &result;
602 } else
603 resultp = NULL;
605 status = acpi_evaluate_object(handle, method, &params, resultp);
607 switch (res_type) {
608 case 'd': /* int */
609 success = (status == AE_OK &&
610 out_obj.type == ACPI_TYPE_INTEGER);
611 if (success && res)
612 *(int *)res = out_obj.integer.value;
613 break;
614 case 'v': /* void */
615 success = status == AE_OK;
616 break;
617 /* add more types as needed */
618 default:
619 printk(TPACPI_ERR "acpi_evalf() called "
620 "with invalid format character '%c'\n", res_type);
621 return 0;
624 if (!success && !quiet)
625 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
626 method, fmt0, acpi_format_exception(status));
628 return success;
631 static int acpi_ec_read(int i, u8 *p)
633 int v;
635 if (ecrd_handle) {
636 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
637 return 0;
638 *p = v;
639 } else {
640 if (ec_read(i, p) < 0)
641 return 0;
644 return 1;
647 static int acpi_ec_write(int i, u8 v)
649 if (ecwr_handle) {
650 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
651 return 0;
652 } else {
653 if (ec_write(i, v) < 0)
654 return 0;
657 return 1;
660 static int issue_thinkpad_cmos_command(int cmos_cmd)
662 if (!cmos_handle)
663 return -ENXIO;
665 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
666 return -EIO;
668 return 0;
671 /*************************************************************************
672 * ACPI device model
675 #define TPACPI_ACPIHANDLE_INIT(object) \
676 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
677 object##_paths, ARRAY_SIZE(object##_paths))
679 static void __init drv_acpi_handle_init(const char *name,
680 acpi_handle *handle, const acpi_handle parent,
681 char **paths, const int num_paths)
683 int i;
684 acpi_status status;
686 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
687 name);
689 for (i = 0; i < num_paths; i++) {
690 status = acpi_get_handle(parent, paths[i], handle);
691 if (ACPI_SUCCESS(status)) {
692 dbg_printk(TPACPI_DBG_INIT,
693 "Found ACPI handle %s for %s\n",
694 paths[i], name);
695 return;
699 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
700 name);
701 *handle = NULL;
704 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
705 u32 level, void *context, void **return_value)
707 *(acpi_handle *)return_value = handle;
709 return AE_CTRL_TERMINATE;
712 static void __init tpacpi_acpi_handle_locate(const char *name,
713 const char *hid,
714 acpi_handle *handle)
716 acpi_status status;
717 acpi_handle device_found;
719 BUG_ON(!name || !hid || !handle);
720 vdbg_printk(TPACPI_DBG_INIT,
721 "trying to locate ACPI handle for %s, using HID %s\n",
722 name, hid);
724 memset(&device_found, 0, sizeof(device_found));
725 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
726 (void *)name, &device_found);
728 *handle = NULL;
730 if (ACPI_SUCCESS(status)) {
731 *handle = device_found;
732 dbg_printk(TPACPI_DBG_INIT,
733 "Found ACPI handle for %s\n", name);
734 } else {
735 vdbg_printk(TPACPI_DBG_INIT,
736 "Could not locate an ACPI handle for %s: %s\n",
737 name, acpi_format_exception(status));
741 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
743 struct ibm_struct *ibm = data;
745 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
746 return;
748 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
749 return;
751 ibm->acpi->notify(ibm, event);
754 static int __init setup_acpi_notify(struct ibm_struct *ibm)
756 acpi_status status;
757 int rc;
759 BUG_ON(!ibm->acpi);
761 if (!*ibm->acpi->handle)
762 return 0;
764 vdbg_printk(TPACPI_DBG_INIT,
765 "setting up ACPI notify for %s\n", ibm->name);
767 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
768 if (rc < 0) {
769 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
770 ibm->name, rc);
771 return -ENODEV;
774 ibm->acpi->device->driver_data = ibm;
775 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
776 TPACPI_ACPI_EVENT_PREFIX,
777 ibm->name);
779 status = acpi_install_notify_handler(*ibm->acpi->handle,
780 ibm->acpi->type, dispatch_acpi_notify, ibm);
781 if (ACPI_FAILURE(status)) {
782 if (status == AE_ALREADY_EXISTS) {
783 printk(TPACPI_NOTICE
784 "another device driver is already "
785 "handling %s events\n", ibm->name);
786 } else {
787 printk(TPACPI_ERR
788 "acpi_install_notify_handler(%s) failed: %s\n",
789 ibm->name, acpi_format_exception(status));
791 return -ENODEV;
793 ibm->flags.acpi_notify_installed = 1;
794 return 0;
797 static int __init tpacpi_device_add(struct acpi_device *device)
799 return 0;
802 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
804 int rc;
806 dbg_printk(TPACPI_DBG_INIT,
807 "registering %s as an ACPI driver\n", ibm->name);
809 BUG_ON(!ibm->acpi);
811 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
812 if (!ibm->acpi->driver) {
813 printk(TPACPI_ERR
814 "failed to allocate memory for ibm->acpi->driver\n");
815 return -ENOMEM;
818 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
819 ibm->acpi->driver->ids = ibm->acpi->hid;
821 ibm->acpi->driver->ops.add = &tpacpi_device_add;
823 rc = acpi_bus_register_driver(ibm->acpi->driver);
824 if (rc < 0) {
825 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
826 ibm->name, rc);
827 kfree(ibm->acpi->driver);
828 ibm->acpi->driver = NULL;
829 } else if (!rc)
830 ibm->flags.acpi_driver_registered = 1;
832 return rc;
836 /****************************************************************************
837 ****************************************************************************
839 * Procfs Helpers
841 ****************************************************************************
842 ****************************************************************************/
844 static int dispatch_proc_show(struct seq_file *m, void *v)
846 struct ibm_struct *ibm = m->private;
848 if (!ibm || !ibm->read)
849 return -EINVAL;
850 return ibm->read(m);
853 static int dispatch_proc_open(struct inode *inode, struct file *file)
855 return single_open(file, dispatch_proc_show, PDE(inode)->data);
858 static ssize_t dispatch_proc_write(struct file *file,
859 const char __user *userbuf,
860 size_t count, loff_t *pos)
862 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
863 char *kernbuf;
864 int ret;
866 if (!ibm || !ibm->write)
867 return -EINVAL;
868 if (count > PAGE_SIZE - 2)
869 return -EINVAL;
871 kernbuf = kmalloc(count + 2, GFP_KERNEL);
872 if (!kernbuf)
873 return -ENOMEM;
875 if (copy_from_user(kernbuf, userbuf, count)) {
876 kfree(kernbuf);
877 return -EFAULT;
880 kernbuf[count] = 0;
881 strcat(kernbuf, ",");
882 ret = ibm->write(kernbuf);
883 if (ret == 0)
884 ret = count;
886 kfree(kernbuf);
888 return ret;
891 static const struct file_operations dispatch_proc_fops = {
892 .owner = THIS_MODULE,
893 .open = dispatch_proc_open,
894 .read = seq_read,
895 .llseek = seq_lseek,
896 .release = single_release,
897 .write = dispatch_proc_write,
900 static char *next_cmd(char **cmds)
902 char *start = *cmds;
903 char *end;
905 while ((end = strchr(start, ',')) && end == start)
906 start = end + 1;
908 if (!end)
909 return NULL;
911 *end = 0;
912 *cmds = end + 1;
913 return start;
917 /****************************************************************************
918 ****************************************************************************
920 * Device model: input, hwmon and platform
922 ****************************************************************************
923 ****************************************************************************/
925 static struct platform_device *tpacpi_pdev;
926 static struct platform_device *tpacpi_sensors_pdev;
927 static struct device *tpacpi_hwmon;
928 static struct input_dev *tpacpi_inputdev;
929 static struct mutex tpacpi_inputdev_send_mutex;
930 static LIST_HEAD(tpacpi_all_drivers);
932 static int tpacpi_suspend_handler(struct platform_device *pdev,
933 pm_message_t state)
935 struct ibm_struct *ibm, *itmp;
937 list_for_each_entry_safe(ibm, itmp,
938 &tpacpi_all_drivers,
939 all_drivers) {
940 if (ibm->suspend)
941 (ibm->suspend)(state);
944 return 0;
947 static int tpacpi_resume_handler(struct platform_device *pdev)
949 struct ibm_struct *ibm, *itmp;
951 list_for_each_entry_safe(ibm, itmp,
952 &tpacpi_all_drivers,
953 all_drivers) {
954 if (ibm->resume)
955 (ibm->resume)();
958 return 0;
961 static void tpacpi_shutdown_handler(struct platform_device *pdev)
963 struct ibm_struct *ibm, *itmp;
965 list_for_each_entry_safe(ibm, itmp,
966 &tpacpi_all_drivers,
967 all_drivers) {
968 if (ibm->shutdown)
969 (ibm->shutdown)();
973 static struct platform_driver tpacpi_pdriver = {
974 .driver = {
975 .name = TPACPI_DRVR_NAME,
976 .owner = THIS_MODULE,
978 .suspend = tpacpi_suspend_handler,
979 .resume = tpacpi_resume_handler,
980 .shutdown = tpacpi_shutdown_handler,
983 static struct platform_driver tpacpi_hwmon_pdriver = {
984 .driver = {
985 .name = TPACPI_HWMON_DRVR_NAME,
986 .owner = THIS_MODULE,
990 /*************************************************************************
991 * sysfs support helpers
994 struct attribute_set {
995 unsigned int members, max_members;
996 struct attribute_group group;
999 struct attribute_set_obj {
1000 struct attribute_set s;
1001 struct attribute *a;
1002 } __attribute__((packed));
1004 static struct attribute_set *create_attr_set(unsigned int max_members,
1005 const char *name)
1007 struct attribute_set_obj *sobj;
1009 if (max_members == 0)
1010 return NULL;
1012 /* Allocates space for implicit NULL at the end too */
1013 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1014 max_members * sizeof(struct attribute *),
1015 GFP_KERNEL);
1016 if (!sobj)
1017 return NULL;
1018 sobj->s.max_members = max_members;
1019 sobj->s.group.attrs = &sobj->a;
1020 sobj->s.group.name = name;
1022 return &sobj->s;
1025 #define destroy_attr_set(_set) \
1026 kfree(_set);
1028 /* not multi-threaded safe, use it in a single thread per set */
1029 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1031 if (!s || !attr)
1032 return -EINVAL;
1034 if (s->members >= s->max_members)
1035 return -ENOMEM;
1037 s->group.attrs[s->members] = attr;
1038 s->members++;
1040 return 0;
1043 static int add_many_to_attr_set(struct attribute_set *s,
1044 struct attribute **attr,
1045 unsigned int count)
1047 int i, res;
1049 for (i = 0; i < count; i++) {
1050 res = add_to_attr_set(s, attr[i]);
1051 if (res)
1052 return res;
1055 return 0;
1058 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1060 sysfs_remove_group(kobj, &s->group);
1061 destroy_attr_set(s);
1064 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1065 sysfs_create_group(_kobj, &_attr_set->group)
1067 static int parse_strtoul(const char *buf,
1068 unsigned long max, unsigned long *value)
1070 char *endp;
1072 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1073 endp = skip_spaces(endp);
1074 if (*endp || *value > max)
1075 return -EINVAL;
1077 return 0;
1080 static void tpacpi_disable_brightness_delay(void)
1082 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1083 printk(TPACPI_NOTICE
1084 "ACPI backlight control delay disabled\n");
1087 static void printk_deprecated_attribute(const char * const what,
1088 const char * const details)
1090 tpacpi_log_usertask("deprecated sysfs attribute");
1091 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1092 "will be removed. %s\n",
1093 what, details);
1096 /*************************************************************************
1097 * rfkill and radio control support helpers
1101 * ThinkPad-ACPI firmware handling model:
1103 * WLSW (master wireless switch) is event-driven, and is common to all
1104 * firmware-controlled radios. It cannot be controlled, just monitored,
1105 * as expected. It overrides all radio state in firmware
1107 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1108 * (TODO: verify how WLSW interacts with the returned radio state).
1110 * The only time there are shadow radio state changes, is when
1111 * masked-off hotkeys are used.
1115 * Internal driver API for radio state:
1117 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1118 * bool: true means radio blocked (off)
1120 enum tpacpi_rfkill_state {
1121 TPACPI_RFK_RADIO_OFF = 0,
1122 TPACPI_RFK_RADIO_ON
1125 /* rfkill switches */
1126 enum tpacpi_rfk_id {
1127 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1128 TPACPI_RFK_WWAN_SW_ID,
1129 TPACPI_RFK_UWB_SW_ID,
1130 TPACPI_RFK_SW_MAX
1133 static const char *tpacpi_rfkill_names[] = {
1134 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1135 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1136 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1137 [TPACPI_RFK_SW_MAX] = NULL
1140 /* ThinkPad-ACPI rfkill subdriver */
1141 struct tpacpi_rfk {
1142 struct rfkill *rfkill;
1143 enum tpacpi_rfk_id id;
1144 const struct tpacpi_rfk_ops *ops;
1147 struct tpacpi_rfk_ops {
1148 /* firmware interface */
1149 int (*get_status)(void);
1150 int (*set_status)(const enum tpacpi_rfkill_state);
1153 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1155 /* Query FW and update rfkill sw state for a given rfkill switch */
1156 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1158 int status;
1160 if (!tp_rfk)
1161 return -ENODEV;
1163 status = (tp_rfk->ops->get_status)();
1164 if (status < 0)
1165 return status;
1167 rfkill_set_sw_state(tp_rfk->rfkill,
1168 (status == TPACPI_RFK_RADIO_OFF));
1170 return status;
1173 /* Query FW and update rfkill sw state for all rfkill switches */
1174 static void tpacpi_rfk_update_swstate_all(void)
1176 unsigned int i;
1178 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1179 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1183 * Sync the HW-blocking state of all rfkill switches,
1184 * do notice it causes the rfkill core to schedule uevents
1186 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1188 unsigned int i;
1189 struct tpacpi_rfk *tp_rfk;
1191 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1192 tp_rfk = tpacpi_rfkill_switches[i];
1193 if (tp_rfk) {
1194 if (rfkill_set_hw_state(tp_rfk->rfkill,
1195 blocked)) {
1196 /* ignore -- we track sw block */
1202 /* Call to get the WLSW state from the firmware */
1203 static int hotkey_get_wlsw(void);
1205 /* Call to query WLSW state and update all rfkill switches */
1206 static bool tpacpi_rfk_check_hwblock_state(void)
1208 int res = hotkey_get_wlsw();
1209 int hw_blocked;
1211 /* When unknown or unsupported, we have to assume it is unblocked */
1212 if (res < 0)
1213 return false;
1215 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1216 tpacpi_rfk_update_hwblock_state(hw_blocked);
1218 return hw_blocked;
1221 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1223 struct tpacpi_rfk *tp_rfk = data;
1224 int res;
1226 dbg_printk(TPACPI_DBG_RFKILL,
1227 "request to change radio state to %s\n",
1228 blocked ? "blocked" : "unblocked");
1230 /* try to set radio state */
1231 res = (tp_rfk->ops->set_status)(blocked ?
1232 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1234 /* and update the rfkill core with whatever the FW really did */
1235 tpacpi_rfk_update_swstate(tp_rfk);
1237 return (res < 0) ? res : 0;
1240 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1241 .set_block = tpacpi_rfk_hook_set_block,
1244 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1245 const struct tpacpi_rfk_ops *tp_rfkops,
1246 const enum rfkill_type rfktype,
1247 const char *name,
1248 const bool set_default)
1250 struct tpacpi_rfk *atp_rfk;
1251 int res;
1252 bool sw_state = false;
1253 bool hw_state;
1254 int sw_status;
1256 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1258 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1259 if (atp_rfk)
1260 atp_rfk->rfkill = rfkill_alloc(name,
1261 &tpacpi_pdev->dev,
1262 rfktype,
1263 &tpacpi_rfk_rfkill_ops,
1264 atp_rfk);
1265 if (!atp_rfk || !atp_rfk->rfkill) {
1266 printk(TPACPI_ERR
1267 "failed to allocate memory for rfkill class\n");
1268 kfree(atp_rfk);
1269 return -ENOMEM;
1272 atp_rfk->id = id;
1273 atp_rfk->ops = tp_rfkops;
1275 sw_status = (tp_rfkops->get_status)();
1276 if (sw_status < 0) {
1277 printk(TPACPI_ERR
1278 "failed to read initial state for %s, error %d\n",
1279 name, sw_status);
1280 } else {
1281 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1282 if (set_default) {
1283 /* try to keep the initial state, since we ask the
1284 * firmware to preserve it across S5 in NVRAM */
1285 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1288 hw_state = tpacpi_rfk_check_hwblock_state();
1289 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1291 res = rfkill_register(atp_rfk->rfkill);
1292 if (res < 0) {
1293 printk(TPACPI_ERR
1294 "failed to register %s rfkill switch: %d\n",
1295 name, res);
1296 rfkill_destroy(atp_rfk->rfkill);
1297 kfree(atp_rfk);
1298 return res;
1301 tpacpi_rfkill_switches[id] = atp_rfk;
1303 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1304 name, (sw_state || hw_state) ? "" : "un");
1305 return 0;
1308 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1310 struct tpacpi_rfk *tp_rfk;
1312 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1314 tp_rfk = tpacpi_rfkill_switches[id];
1315 if (tp_rfk) {
1316 rfkill_unregister(tp_rfk->rfkill);
1317 rfkill_destroy(tp_rfk->rfkill);
1318 tpacpi_rfkill_switches[id] = NULL;
1319 kfree(tp_rfk);
1323 static void printk_deprecated_rfkill_attribute(const char * const what)
1325 printk_deprecated_attribute(what,
1326 "Please switch to generic rfkill before year 2010");
1329 /* sysfs <radio> enable ------------------------------------------------ */
1330 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1331 struct device_attribute *attr,
1332 char *buf)
1334 int status;
1336 printk_deprecated_rfkill_attribute(attr->attr.name);
1338 /* This is in the ABI... */
1339 if (tpacpi_rfk_check_hwblock_state()) {
1340 status = TPACPI_RFK_RADIO_OFF;
1341 } else {
1342 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1343 if (status < 0)
1344 return status;
1347 return snprintf(buf, PAGE_SIZE, "%d\n",
1348 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1351 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1352 struct device_attribute *attr,
1353 const char *buf, size_t count)
1355 unsigned long t;
1356 int res;
1358 printk_deprecated_rfkill_attribute(attr->attr.name);
1360 if (parse_strtoul(buf, 1, &t))
1361 return -EINVAL;
1363 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1365 /* This is in the ABI... */
1366 if (tpacpi_rfk_check_hwblock_state() && !!t)
1367 return -EPERM;
1369 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1370 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1371 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1373 return (res < 0) ? res : count;
1376 /* procfs -------------------------------------------------------------- */
1377 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1379 if (id >= TPACPI_RFK_SW_MAX)
1380 seq_printf(m, "status:\t\tnot supported\n");
1381 else {
1382 int status;
1384 /* This is in the ABI... */
1385 if (tpacpi_rfk_check_hwblock_state()) {
1386 status = TPACPI_RFK_RADIO_OFF;
1387 } else {
1388 status = tpacpi_rfk_update_swstate(
1389 tpacpi_rfkill_switches[id]);
1390 if (status < 0)
1391 return status;
1394 seq_printf(m, "status:\t\t%s\n",
1395 (status == TPACPI_RFK_RADIO_ON) ?
1396 "enabled" : "disabled");
1397 seq_printf(m, "commands:\tenable, disable\n");
1400 return 0;
1403 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1405 char *cmd;
1406 int status = -1;
1407 int res = 0;
1409 if (id >= TPACPI_RFK_SW_MAX)
1410 return -ENODEV;
1412 while ((cmd = next_cmd(&buf))) {
1413 if (strlencmp(cmd, "enable") == 0)
1414 status = TPACPI_RFK_RADIO_ON;
1415 else if (strlencmp(cmd, "disable") == 0)
1416 status = TPACPI_RFK_RADIO_OFF;
1417 else
1418 return -EINVAL;
1421 if (status != -1) {
1422 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1423 (status == TPACPI_RFK_RADIO_ON) ?
1424 "enable" : "disable",
1425 tpacpi_rfkill_names[id]);
1426 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1427 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1430 return res;
1433 /*************************************************************************
1434 * thinkpad-acpi driver attributes
1437 /* interface_version --------------------------------------------------- */
1438 static ssize_t tpacpi_driver_interface_version_show(
1439 struct device_driver *drv,
1440 char *buf)
1442 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1445 static DRIVER_ATTR(interface_version, S_IRUGO,
1446 tpacpi_driver_interface_version_show, NULL);
1448 /* debug_level --------------------------------------------------------- */
1449 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1450 char *buf)
1452 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1455 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1456 const char *buf, size_t count)
1458 unsigned long t;
1460 if (parse_strtoul(buf, 0xffff, &t))
1461 return -EINVAL;
1463 dbg_level = t;
1465 return count;
1468 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1469 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1471 /* version ------------------------------------------------------------- */
1472 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1473 char *buf)
1475 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1476 TPACPI_DESC, TPACPI_VERSION);
1479 static DRIVER_ATTR(version, S_IRUGO,
1480 tpacpi_driver_version_show, NULL);
1482 /* --------------------------------------------------------------------- */
1484 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1486 /* wlsw_emulstate ------------------------------------------------------ */
1487 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1488 char *buf)
1490 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1493 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1494 const char *buf, size_t count)
1496 unsigned long t;
1498 if (parse_strtoul(buf, 1, &t))
1499 return -EINVAL;
1501 if (tpacpi_wlsw_emulstate != !!t) {
1502 tpacpi_wlsw_emulstate = !!t;
1503 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1506 return count;
1509 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1510 tpacpi_driver_wlsw_emulstate_show,
1511 tpacpi_driver_wlsw_emulstate_store);
1513 /* bluetooth_emulstate ------------------------------------------------- */
1514 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1515 struct device_driver *drv,
1516 char *buf)
1518 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1521 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1522 struct device_driver *drv,
1523 const char *buf, size_t count)
1525 unsigned long t;
1527 if (parse_strtoul(buf, 1, &t))
1528 return -EINVAL;
1530 tpacpi_bluetooth_emulstate = !!t;
1532 return count;
1535 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1536 tpacpi_driver_bluetooth_emulstate_show,
1537 tpacpi_driver_bluetooth_emulstate_store);
1539 /* wwan_emulstate ------------------------------------------------- */
1540 static ssize_t tpacpi_driver_wwan_emulstate_show(
1541 struct device_driver *drv,
1542 char *buf)
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1547 static ssize_t tpacpi_driver_wwan_emulstate_store(
1548 struct device_driver *drv,
1549 const char *buf, size_t count)
1551 unsigned long t;
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1556 tpacpi_wwan_emulstate = !!t;
1558 return count;
1561 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1562 tpacpi_driver_wwan_emulstate_show,
1563 tpacpi_driver_wwan_emulstate_store);
1565 /* uwb_emulstate ------------------------------------------------- */
1566 static ssize_t tpacpi_driver_uwb_emulstate_show(
1567 struct device_driver *drv,
1568 char *buf)
1570 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1573 static ssize_t tpacpi_driver_uwb_emulstate_store(
1574 struct device_driver *drv,
1575 const char *buf, size_t count)
1577 unsigned long t;
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1582 tpacpi_uwb_emulstate = !!t;
1584 return count;
1587 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1588 tpacpi_driver_uwb_emulstate_show,
1589 tpacpi_driver_uwb_emulstate_store);
1590 #endif
1592 /* --------------------------------------------------------------------- */
1594 static struct driver_attribute *tpacpi_driver_attributes[] = {
1595 &driver_attr_debug_level, &driver_attr_version,
1596 &driver_attr_interface_version,
1599 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1601 int i, res;
1603 i = 0;
1604 res = 0;
1605 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1606 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1607 i++;
1610 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1611 if (!res && dbg_wlswemul)
1612 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1613 if (!res && dbg_bluetoothemul)
1614 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1615 if (!res && dbg_wwanemul)
1616 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1617 if (!res && dbg_uwbemul)
1618 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1619 #endif
1621 return res;
1624 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1626 int i;
1628 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1629 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1631 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1632 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1633 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1634 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1635 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1636 #endif
1639 /*************************************************************************
1640 * Firmware Data
1644 * Table of recommended minimum BIOS versions
1646 * Reasons for listing:
1647 * 1. Stable BIOS, listed because the unknown amount of
1648 * bugs and bad ACPI behaviour on older versions
1650 * 2. BIOS or EC fw with known bugs that trigger on Linux
1652 * 3. BIOS with known reduced functionality in older versions
1654 * We recommend the latest BIOS and EC version.
1655 * We only support the latest BIOS and EC fw version as a rule.
1657 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1658 * Information from users in ThinkWiki
1660 * WARNING: we use this table also to detect that the machine is
1661 * a ThinkPad in some cases, so don't remove entries lightly.
1664 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1665 { .vendor = (__v), \
1666 .bios = TPID(__id1, __id2), \
1667 .ec = TPACPI_MATCH_ANY, \
1668 .quirks = TPACPI_MATCH_ANY << 16 \
1669 | (__bv1) << 8 | (__bv2) }
1671 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1672 __eid, __ev1, __ev2) \
1673 { .vendor = (__v), \
1674 .bios = TPID(__bid1, __bid2), \
1675 .ec = __eid, \
1676 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1677 | (__bv1) << 8 | (__bv2) }
1679 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1680 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1682 /* Outdated IBM BIOSes often lack the EC id string */
1683 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1685 __bv1, __bv2, TPID(__id1, __id2), \
1686 __ev1, __ev2), \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1689 __ev1, __ev2)
1691 /* Outdated IBM BIOSes often lack the EC id string */
1692 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1693 __eid1, __eid2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1695 __bv1, __bv2, TPID(__eid1, __eid2), \
1696 __ev1, __ev2), \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1699 __ev1, __ev2)
1701 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1702 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1704 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1705 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1706 __bv1, __bv2, TPID(__id1, __id2), \
1707 __ev1, __ev2)
1709 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1710 __eid1, __eid2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1712 __bv1, __bv2, TPID(__eid1, __eid2), \
1713 __ev1, __ev2)
1715 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1716 /* Numeric models ------------------ */
1717 /* FW MODEL BIOS VERS */
1718 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1719 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1720 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1721 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1722 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1723 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1724 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1725 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1726 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1728 /* A-series ------------------------- */
1729 /* FW MODEL BIOS VERS EC VERS */
1730 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1731 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1732 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1733 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1734 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1735 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1736 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1737 TPV_QI0('1', '3', '2', '0'), /* A22m */
1738 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1739 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1740 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1742 /* G-series ------------------------- */
1743 /* FW MODEL BIOS VERS */
1744 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1745 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1747 /* R-series, T-series --------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1750 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1751 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1752 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1753 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1754 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1755 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1756 T40/p, T41/p, T42/p (1) */
1757 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1758 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1759 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1760 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1762 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1763 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1764 TPV_QI0('1', '6', '3', '2'), /* T22 */
1765 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1766 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1767 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1769 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1770 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1771 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1773 /* BIOS FW BIOS VERS EC FW EC VERS */
1774 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1775 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1777 /* X-series ------------------------- */
1778 /* FW MODEL BIOS VERS EC VERS */
1779 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1780 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1781 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1782 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1783 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1784 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1785 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1787 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1788 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1790 /* (0) - older versions lack DMI EC fw string and functionality */
1791 /* (1) - older versions known to lack functionality */
1794 #undef TPV_QL1
1795 #undef TPV_QL0
1796 #undef TPV_QI2
1797 #undef TPV_QI1
1798 #undef TPV_QI0
1799 #undef TPV_Q_X
1800 #undef TPV_Q
1802 static void __init tpacpi_check_outdated_fw(void)
1804 unsigned long fwvers;
1805 u16 ec_version, bios_version;
1807 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable));
1810 if (!fwvers)
1811 return;
1813 bios_version = fwvers & 0xffffU;
1814 ec_version = (fwvers >> 16) & 0xffffU;
1816 /* note that unknown versions are set to 0x0000 and we use that */
1817 if ((bios_version > thinkpad_id.bios_release) ||
1818 (ec_version > thinkpad_id.ec_release &&
1819 ec_version != TPACPI_MATCH_ANY)) {
1821 * The changelogs would let us track down the exact
1822 * reason, but it is just too much of a pain to track
1823 * it. We only list BIOSes that are either really
1824 * broken, or really stable to begin with, so it is
1825 * best if the user upgrades the firmware anyway.
1827 printk(TPACPI_WARN
1828 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1829 printk(TPACPI_WARN
1830 "WARNING: This firmware may be missing critical bug "
1831 "fixes and/or important features\n");
1835 static bool __init tpacpi_is_fw_known(void)
1837 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1838 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1841 /****************************************************************************
1842 ****************************************************************************
1844 * Subdrivers
1846 ****************************************************************************
1847 ****************************************************************************/
1849 /*************************************************************************
1850 * thinkpad-acpi metadata subdriver
1853 static int thinkpad_acpi_driver_read(struct seq_file *m)
1855 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1856 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1857 return 0;
1860 static struct ibm_struct thinkpad_acpi_driver_data = {
1861 .name = "driver",
1862 .read = thinkpad_acpi_driver_read,
1865 /*************************************************************************
1866 * Hotkey subdriver
1870 * ThinkPad firmware event model
1872 * The ThinkPad firmware has two main event interfaces: normal ACPI
1873 * notifications (which follow the ACPI standard), and a private event
1874 * interface.
1876 * The private event interface also issues events for the hotkeys. As
1877 * the driver gained features, the event handling code ended up being
1878 * built around the hotkey subdriver. This will need to be refactored
1879 * to a more formal event API eventually.
1881 * Some "hotkeys" are actually supposed to be used as event reports,
1882 * such as "brightness has changed", "volume has changed", depending on
1883 * the ThinkPad model and how the firmware is operating.
1885 * Unlike other classes, hotkey-class events have mask/unmask control on
1886 * non-ancient firmware. However, how it behaves changes a lot with the
1887 * firmware model and version.
1890 enum { /* hot key scan codes (derived from ACPI DSDT) */
1891 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1892 TP_ACPI_HOTKEYSCAN_FNF2,
1893 TP_ACPI_HOTKEYSCAN_FNF3,
1894 TP_ACPI_HOTKEYSCAN_FNF4,
1895 TP_ACPI_HOTKEYSCAN_FNF5,
1896 TP_ACPI_HOTKEYSCAN_FNF6,
1897 TP_ACPI_HOTKEYSCAN_FNF7,
1898 TP_ACPI_HOTKEYSCAN_FNF8,
1899 TP_ACPI_HOTKEYSCAN_FNF9,
1900 TP_ACPI_HOTKEYSCAN_FNF10,
1901 TP_ACPI_HOTKEYSCAN_FNF11,
1902 TP_ACPI_HOTKEYSCAN_FNF12,
1903 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1904 TP_ACPI_HOTKEYSCAN_FNINSERT,
1905 TP_ACPI_HOTKEYSCAN_FNDELETE,
1906 TP_ACPI_HOTKEYSCAN_FNHOME,
1907 TP_ACPI_HOTKEYSCAN_FNEND,
1908 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1910 TP_ACPI_HOTKEYSCAN_FNSPACE,
1911 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1913 TP_ACPI_HOTKEYSCAN_MUTE,
1914 TP_ACPI_HOTKEYSCAN_THINKPAD,
1915 TP_ACPI_HOTKEYSCAN_UNK1,
1916 TP_ACPI_HOTKEYSCAN_UNK2,
1917 TP_ACPI_HOTKEYSCAN_UNK3,
1918 TP_ACPI_HOTKEYSCAN_UNK4,
1919 TP_ACPI_HOTKEYSCAN_UNK5,
1920 TP_ACPI_HOTKEYSCAN_UNK6,
1921 TP_ACPI_HOTKEYSCAN_UNK7,
1922 TP_ACPI_HOTKEYSCAN_UNK8,
1924 /* Hotkey keymap size */
1925 TPACPI_HOTKEY_MAP_LEN
1928 enum { /* Keys/events available through NVRAM polling */
1929 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1930 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1933 enum { /* Positions of some of the keys in hotkey masks */
1934 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1935 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1936 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1937 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1938 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1939 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1940 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1941 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1942 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1943 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1944 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1947 enum { /* NVRAM to ACPI HKEY group map */
1948 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1949 TP_ACPI_HKEY_ZOOM_MASK |
1950 TP_ACPI_HKEY_DISPSWTCH_MASK |
1951 TP_ACPI_HKEY_HIBERNATE_MASK,
1952 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1953 TP_ACPI_HKEY_BRGHTDWN_MASK,
1954 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1955 TP_ACPI_HKEY_VOLDWN_MASK |
1956 TP_ACPI_HKEY_MUTE_MASK,
1959 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1960 struct tp_nvram_state {
1961 u16 thinkpad_toggle:1;
1962 u16 zoom_toggle:1;
1963 u16 display_toggle:1;
1964 u16 thinklight_toggle:1;
1965 u16 hibernate_toggle:1;
1966 u16 displayexp_toggle:1;
1967 u16 display_state:1;
1968 u16 brightness_toggle:1;
1969 u16 volume_toggle:1;
1970 u16 mute:1;
1972 u8 brightness_level;
1973 u8 volume_level;
1976 /* kthread for the hotkey poller */
1977 static struct task_struct *tpacpi_hotkey_task;
1979 /* Acquired while the poller kthread is running, use to sync start/stop */
1980 static struct mutex hotkey_thread_mutex;
1983 * Acquire mutex to write poller control variables as an
1984 * atomic block.
1986 * Increment hotkey_config_change when changing them if you
1987 * want the kthread to forget old state.
1989 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1991 static struct mutex hotkey_thread_data_mutex;
1992 static unsigned int hotkey_config_change;
1995 * hotkey poller control variables
1997 * Must be atomic or readers will also need to acquire mutex
1999 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2000 * should be used only when the changes need to be taken as
2001 * a block, OR when one needs to force the kthread to forget
2002 * old state.
2004 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2005 static unsigned int hotkey_poll_freq = 10; /* Hz */
2007 #define HOTKEY_CONFIG_CRITICAL_START \
2008 do { \
2009 mutex_lock(&hotkey_thread_data_mutex); \
2010 hotkey_config_change++; \
2011 } while (0);
2012 #define HOTKEY_CONFIG_CRITICAL_END \
2013 mutex_unlock(&hotkey_thread_data_mutex);
2015 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2017 #define hotkey_source_mask 0U
2018 #define HOTKEY_CONFIG_CRITICAL_START
2019 #define HOTKEY_CONFIG_CRITICAL_END
2021 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2023 static struct mutex hotkey_mutex;
2025 static enum { /* Reasons for waking up */
2026 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2027 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2028 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2029 } hotkey_wakeup_reason;
2031 static int hotkey_autosleep_ack;
2033 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2034 static u32 hotkey_all_mask; /* all events supported in fw */
2035 static u32 hotkey_reserved_mask; /* events better left disabled */
2036 static u32 hotkey_driver_mask; /* events needed by the driver */
2037 static u32 hotkey_user_mask; /* events visible to userspace */
2038 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2040 static unsigned int hotkey_report_mode;
2042 static u16 *hotkey_keycode_map;
2044 static struct attribute_set *hotkey_dev_attributes;
2046 static void tpacpi_driver_event(const unsigned int hkey_event);
2047 static void hotkey_driver_event(const unsigned int scancode);
2048 static void hotkey_poll_setup(const bool may_warn);
2050 /* HKEY.MHKG() return bits */
2051 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2053 static int hotkey_get_wlsw(void)
2055 int status;
2057 if (!tp_features.hotkey_wlsw)
2058 return -ENODEV;
2060 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2061 if (dbg_wlswemul)
2062 return (tpacpi_wlsw_emulstate) ?
2063 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2064 #endif
2066 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2067 return -EIO;
2069 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2072 static int hotkey_get_tablet_mode(int *status)
2074 int s;
2076 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2077 return -EIO;
2079 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2080 return 0;
2084 * Reads current event mask from firmware, and updates
2085 * hotkey_acpi_mask accordingly. Also resets any bits
2086 * from hotkey_user_mask that are unavailable to be
2087 * delivered (shadow requirement of the userspace ABI).
2089 * Call with hotkey_mutex held
2091 static int hotkey_mask_get(void)
2093 if (tp_features.hotkey_mask) {
2094 u32 m = 0;
2096 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2097 return -EIO;
2099 hotkey_acpi_mask = m;
2100 } else {
2101 /* no mask support doesn't mean no event support... */
2102 hotkey_acpi_mask = hotkey_all_mask;
2105 /* sync userspace-visible mask */
2106 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2108 return 0;
2111 void static hotkey_mask_warn_incomplete_mask(void)
2113 /* log only what the user can fix... */
2114 const u32 wantedmask = hotkey_driver_mask &
2115 ~(hotkey_acpi_mask | hotkey_source_mask) &
2116 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2118 if (wantedmask)
2119 printk(TPACPI_NOTICE
2120 "required events 0x%08x not enabled!\n",
2121 wantedmask);
2125 * Set the firmware mask when supported
2127 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2129 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2131 * Call with hotkey_mutex held
2133 static int hotkey_mask_set(u32 mask)
2135 int i;
2136 int rc = 0;
2138 const u32 fwmask = mask & ~hotkey_source_mask;
2140 if (tp_features.hotkey_mask) {
2141 for (i = 0; i < 32; i++) {
2142 if (!acpi_evalf(hkey_handle,
2143 NULL, "MHKM", "vdd", i + 1,
2144 !!(mask & (1 << i)))) {
2145 rc = -EIO;
2146 break;
2152 * We *must* make an inconditional call to hotkey_mask_get to
2153 * refresh hotkey_acpi_mask and update hotkey_user_mask
2155 * Take the opportunity to also log when we cannot _enable_
2156 * a given event.
2158 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2159 printk(TPACPI_NOTICE
2160 "asked for hotkey mask 0x%08x, but "
2161 "firmware forced it to 0x%08x\n",
2162 fwmask, hotkey_acpi_mask);
2165 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2166 hotkey_mask_warn_incomplete_mask();
2168 return rc;
2172 * Sets hotkey_user_mask and tries to set the firmware mask
2174 * Call with hotkey_mutex held
2176 static int hotkey_user_mask_set(const u32 mask)
2178 int rc;
2180 /* Give people a chance to notice they are doing something that
2181 * is bound to go boom on their users sooner or later */
2182 if (!tp_warned.hotkey_mask_ff &&
2183 (mask == 0xffff || mask == 0xffffff ||
2184 mask == 0xffffffff)) {
2185 tp_warned.hotkey_mask_ff = 1;
2186 printk(TPACPI_NOTICE
2187 "setting the hotkey mask to 0x%08x is likely "
2188 "not the best way to go about it\n", mask);
2189 printk(TPACPI_NOTICE
2190 "please consider using the driver defaults, "
2191 "and refer to up-to-date thinkpad-acpi "
2192 "documentation\n");
2195 /* Try to enable what the user asked for, plus whatever we need.
2196 * this syncs everything but won't enable bits in hotkey_user_mask */
2197 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2199 /* Enable the available bits in hotkey_user_mask */
2200 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2202 return rc;
2206 * Sets the driver hotkey mask.
2208 * Can be called even if the hotkey subdriver is inactive
2210 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2212 int rc;
2214 /* Do the right thing if hotkey_init has not been called yet */
2215 if (!tp_features.hotkey) {
2216 hotkey_driver_mask = mask;
2217 return 0;
2220 mutex_lock(&hotkey_mutex);
2222 HOTKEY_CONFIG_CRITICAL_START
2223 hotkey_driver_mask = mask;
2224 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2225 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2226 #endif
2227 HOTKEY_CONFIG_CRITICAL_END
2229 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2230 ~hotkey_source_mask);
2231 hotkey_poll_setup(true);
2233 mutex_unlock(&hotkey_mutex);
2235 return rc;
2238 static int hotkey_status_get(int *status)
2240 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2241 return -EIO;
2243 return 0;
2246 static int hotkey_status_set(bool enable)
2248 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2249 return -EIO;
2251 return 0;
2254 static void tpacpi_input_send_tabletsw(void)
2256 int state;
2258 if (tp_features.hotkey_tablet &&
2259 !hotkey_get_tablet_mode(&state)) {
2260 mutex_lock(&tpacpi_inputdev_send_mutex);
2262 input_report_switch(tpacpi_inputdev,
2263 SW_TABLET_MODE, !!state);
2264 input_sync(tpacpi_inputdev);
2266 mutex_unlock(&tpacpi_inputdev_send_mutex);
2270 /* Do NOT call without validating scancode first */
2271 static void tpacpi_input_send_key(const unsigned int scancode)
2273 const unsigned int keycode = hotkey_keycode_map[scancode];
2275 if (keycode != KEY_RESERVED) {
2276 mutex_lock(&tpacpi_inputdev_send_mutex);
2278 input_report_key(tpacpi_inputdev, keycode, 1);
2279 if (keycode == KEY_UNKNOWN)
2280 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2281 scancode);
2282 input_sync(tpacpi_inputdev);
2284 input_report_key(tpacpi_inputdev, keycode, 0);
2285 if (keycode == KEY_UNKNOWN)
2286 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2287 scancode);
2288 input_sync(tpacpi_inputdev);
2290 mutex_unlock(&tpacpi_inputdev_send_mutex);
2294 /* Do NOT call without validating scancode first */
2295 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2297 hotkey_driver_event(scancode);
2298 if (hotkey_user_mask & (1 << scancode))
2299 tpacpi_input_send_key(scancode);
2302 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2303 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2305 /* Do NOT call without validating scancode first */
2306 static void tpacpi_hotkey_send_key(unsigned int scancode)
2308 tpacpi_input_send_key_masked(scancode);
2309 if (hotkey_report_mode < 2) {
2310 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2311 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2315 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2317 u8 d;
2319 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2320 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2321 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2322 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2323 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2324 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2326 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2327 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2328 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2330 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2331 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2332 n->displayexp_toggle =
2333 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2335 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2336 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2337 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2338 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2339 n->brightness_toggle =
2340 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2342 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2343 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2344 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2345 >> TP_NVRAM_POS_LEVEL_VOLUME;
2346 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2347 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2351 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2352 struct tp_nvram_state *newn,
2353 const u32 event_mask)
2356 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2357 do { \
2358 if ((event_mask & (1 << __scancode)) && \
2359 oldn->__member != newn->__member) \
2360 tpacpi_hotkey_send_key(__scancode); \
2361 } while (0)
2363 #define TPACPI_MAY_SEND_KEY(__scancode) \
2364 do { \
2365 if (event_mask & (1 << __scancode)) \
2366 tpacpi_hotkey_send_key(__scancode); \
2367 } while (0)
2369 void issue_volchange(const unsigned int oldvol,
2370 const unsigned int newvol)
2372 unsigned int i = oldvol;
2374 while (i > newvol) {
2375 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2376 i--;
2378 while (i < newvol) {
2379 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2380 i++;
2384 void issue_brightnesschange(const unsigned int oldbrt,
2385 const unsigned int newbrt)
2387 unsigned int i = oldbrt;
2389 while (i > newbrt) {
2390 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2391 i--;
2393 while (i < newbrt) {
2394 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2395 i++;
2399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2401 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2409 * Handle volume
2411 * This code is supposed to duplicate the IBM firmware behaviour:
2412 * - Pressing MUTE issues mute hotkey message, even when already mute
2413 * - Pressing Volume up/down issues volume up/down hotkey messages,
2414 * even when already at maximum or minumum volume
2415 * - The act of unmuting issues volume up/down notification,
2416 * depending which key was used to unmute
2418 * We are constrained to what the NVRAM can tell us, which is not much
2419 * and certainly not enough if more than one volume hotkey was pressed
2420 * since the last poll cycle.
2422 * Just to make our life interesting, some newer Lenovo ThinkPads have
2423 * bugs in the BIOS and may fail to update volume_toggle properly.
2425 if (newn->mute) {
2426 /* muted */
2427 if (!oldn->mute ||
2428 oldn->volume_toggle != newn->volume_toggle ||
2429 oldn->volume_level != newn->volume_level) {
2430 /* recently muted, or repeated mute keypress, or
2431 * multiple presses ending in mute */
2432 issue_volchange(oldn->volume_level, newn->volume_level);
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2435 } else {
2436 /* unmute */
2437 if (oldn->mute) {
2438 /* recently unmuted, issue 'unmute' keypress */
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2441 if (oldn->volume_level != newn->volume_level) {
2442 issue_volchange(oldn->volume_level, newn->volume_level);
2443 } else if (oldn->volume_toggle != newn->volume_toggle) {
2444 /* repeated vol up/down keypress at end of scale ? */
2445 if (newn->volume_level == 0)
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2447 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2452 /* handle brightness */
2453 if (oldn->brightness_level != newn->brightness_level) {
2454 issue_brightnesschange(oldn->brightness_level,
2455 newn->brightness_level);
2456 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2457 /* repeated key presses that didn't change state */
2458 if (newn->brightness_level == 0)
2459 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2460 else if (newn->brightness_level >= bright_maxlvl
2461 && !tp_features.bright_unkfw)
2462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2465 #undef TPACPI_COMPARE_KEY
2466 #undef TPACPI_MAY_SEND_KEY
2470 * Polling driver
2472 * We track all events in hotkey_source_mask all the time, since
2473 * most of them are edge-based. We only issue those requested by
2474 * hotkey_user_mask or hotkey_driver_mask, though.
2476 static int hotkey_kthread(void *data)
2478 struct tp_nvram_state s[2];
2479 u32 poll_mask, event_mask;
2480 unsigned int si, so;
2481 unsigned long t;
2482 unsigned int change_detector, must_reset;
2483 unsigned int poll_freq;
2485 mutex_lock(&hotkey_thread_mutex);
2487 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2488 goto exit;
2490 set_freezable();
2492 so = 0;
2493 si = 1;
2494 t = 0;
2496 /* Initial state for compares */
2497 mutex_lock(&hotkey_thread_data_mutex);
2498 change_detector = hotkey_config_change;
2499 poll_mask = hotkey_source_mask;
2500 event_mask = hotkey_source_mask &
2501 (hotkey_driver_mask | hotkey_user_mask);
2502 poll_freq = hotkey_poll_freq;
2503 mutex_unlock(&hotkey_thread_data_mutex);
2504 hotkey_read_nvram(&s[so], poll_mask);
2506 while (!kthread_should_stop()) {
2507 if (t == 0) {
2508 if (likely(poll_freq))
2509 t = 1000/poll_freq;
2510 else
2511 t = 100; /* should never happen... */
2513 t = msleep_interruptible(t);
2514 if (unlikely(kthread_should_stop()))
2515 break;
2516 must_reset = try_to_freeze();
2517 if (t > 0 && !must_reset)
2518 continue;
2520 mutex_lock(&hotkey_thread_data_mutex);
2521 if (must_reset || hotkey_config_change != change_detector) {
2522 /* forget old state on thaw or config change */
2523 si = so;
2524 t = 0;
2525 change_detector = hotkey_config_change;
2527 poll_mask = hotkey_source_mask;
2528 event_mask = hotkey_source_mask &
2529 (hotkey_driver_mask | hotkey_user_mask);
2530 poll_freq = hotkey_poll_freq;
2531 mutex_unlock(&hotkey_thread_data_mutex);
2533 if (likely(poll_mask)) {
2534 hotkey_read_nvram(&s[si], poll_mask);
2535 if (likely(si != so)) {
2536 hotkey_compare_and_issue_event(&s[so], &s[si],
2537 event_mask);
2541 so = si;
2542 si ^= 1;
2545 exit:
2546 mutex_unlock(&hotkey_thread_mutex);
2547 return 0;
2550 /* call with hotkey_mutex held */
2551 static void hotkey_poll_stop_sync(void)
2553 if (tpacpi_hotkey_task) {
2554 if (frozen(tpacpi_hotkey_task) ||
2555 freezing(tpacpi_hotkey_task))
2556 thaw_process(tpacpi_hotkey_task);
2558 kthread_stop(tpacpi_hotkey_task);
2559 tpacpi_hotkey_task = NULL;
2560 mutex_lock(&hotkey_thread_mutex);
2561 /* at this point, the thread did exit */
2562 mutex_unlock(&hotkey_thread_mutex);
2566 /* call with hotkey_mutex held */
2567 static void hotkey_poll_setup(const bool may_warn)
2569 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2570 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2572 if (hotkey_poll_freq > 0 &&
2573 (poll_driver_mask ||
2574 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2575 if (!tpacpi_hotkey_task) {
2576 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2577 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2578 if (IS_ERR(tpacpi_hotkey_task)) {
2579 tpacpi_hotkey_task = NULL;
2580 printk(TPACPI_ERR
2581 "could not create kernel thread "
2582 "for hotkey polling\n");
2585 } else {
2586 hotkey_poll_stop_sync();
2587 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2588 hotkey_poll_freq == 0) {
2589 printk(TPACPI_NOTICE
2590 "hot keys 0x%08x and/or events 0x%08x "
2591 "require polling, which is currently "
2592 "disabled\n",
2593 poll_user_mask, poll_driver_mask);
2598 static void hotkey_poll_setup_safe(const bool may_warn)
2600 mutex_lock(&hotkey_mutex);
2601 hotkey_poll_setup(may_warn);
2602 mutex_unlock(&hotkey_mutex);
2605 /* call with hotkey_mutex held */
2606 static void hotkey_poll_set_freq(unsigned int freq)
2608 if (!freq)
2609 hotkey_poll_stop_sync();
2611 hotkey_poll_freq = freq;
2614 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2616 static void hotkey_poll_setup(const bool __unused)
2620 static void hotkey_poll_setup_safe(const bool __unused)
2624 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2626 static int hotkey_inputdev_open(struct input_dev *dev)
2628 switch (tpacpi_lifecycle) {
2629 case TPACPI_LIFE_INIT:
2630 case TPACPI_LIFE_RUNNING:
2631 hotkey_poll_setup_safe(false);
2632 return 0;
2633 case TPACPI_LIFE_EXITING:
2634 return -EBUSY;
2637 /* Should only happen if tpacpi_lifecycle is corrupt */
2638 BUG();
2639 return -EBUSY;
2642 static void hotkey_inputdev_close(struct input_dev *dev)
2644 /* disable hotkey polling when possible */
2645 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2646 !(hotkey_source_mask & hotkey_driver_mask))
2647 hotkey_poll_setup_safe(false);
2650 /* sysfs hotkey enable ------------------------------------------------- */
2651 static ssize_t hotkey_enable_show(struct device *dev,
2652 struct device_attribute *attr,
2653 char *buf)
2655 int res, status;
2657 printk_deprecated_attribute("hotkey_enable",
2658 "Hotkey reporting is always enabled");
2660 res = hotkey_status_get(&status);
2661 if (res)
2662 return res;
2664 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2667 static ssize_t hotkey_enable_store(struct device *dev,
2668 struct device_attribute *attr,
2669 const char *buf, size_t count)
2671 unsigned long t;
2673 printk_deprecated_attribute("hotkey_enable",
2674 "Hotkeys can be disabled through hotkey_mask");
2676 if (parse_strtoul(buf, 1, &t))
2677 return -EINVAL;
2679 if (t == 0)
2680 return -EPERM;
2682 return count;
2685 static struct device_attribute dev_attr_hotkey_enable =
2686 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2687 hotkey_enable_show, hotkey_enable_store);
2689 /* sysfs hotkey mask --------------------------------------------------- */
2690 static ssize_t hotkey_mask_show(struct device *dev,
2691 struct device_attribute *attr,
2692 char *buf)
2694 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2697 static ssize_t hotkey_mask_store(struct device *dev,
2698 struct device_attribute *attr,
2699 const char *buf, size_t count)
2701 unsigned long t;
2702 int res;
2704 if (parse_strtoul(buf, 0xffffffffUL, &t))
2705 return -EINVAL;
2707 if (mutex_lock_killable(&hotkey_mutex))
2708 return -ERESTARTSYS;
2710 res = hotkey_user_mask_set(t);
2712 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2713 hotkey_poll_setup(true);
2714 #endif
2716 mutex_unlock(&hotkey_mutex);
2718 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2720 return (res) ? res : count;
2723 static struct device_attribute dev_attr_hotkey_mask =
2724 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2725 hotkey_mask_show, hotkey_mask_store);
2727 /* sysfs hotkey bios_enabled ------------------------------------------- */
2728 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2732 return sprintf(buf, "0\n");
2735 static struct device_attribute dev_attr_hotkey_bios_enabled =
2736 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2738 /* sysfs hotkey bios_mask ---------------------------------------------- */
2739 static ssize_t hotkey_bios_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2743 printk_deprecated_attribute("hotkey_bios_mask",
2744 "This attribute is useless.");
2745 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2748 static struct device_attribute dev_attr_hotkey_bios_mask =
2749 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2751 /* sysfs hotkey all_mask ----------------------------------------------- */
2752 static ssize_t hotkey_all_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2757 hotkey_all_mask | hotkey_source_mask);
2760 static struct device_attribute dev_attr_hotkey_all_mask =
2761 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2763 /* sysfs hotkey recommended_mask --------------------------------------- */
2764 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2765 struct device_attribute *attr,
2766 char *buf)
2768 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2769 (hotkey_all_mask | hotkey_source_mask)
2770 & ~hotkey_reserved_mask);
2773 static struct device_attribute dev_attr_hotkey_recommended_mask =
2774 __ATTR(hotkey_recommended_mask, S_IRUGO,
2775 hotkey_recommended_mask_show, NULL);
2777 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2779 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2780 static ssize_t hotkey_source_mask_show(struct device *dev,
2781 struct device_attribute *attr,
2782 char *buf)
2784 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2787 static ssize_t hotkey_source_mask_store(struct device *dev,
2788 struct device_attribute *attr,
2789 const char *buf, size_t count)
2791 unsigned long t;
2792 u32 r_ev;
2793 int rc;
2795 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2796 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2797 return -EINVAL;
2799 if (mutex_lock_killable(&hotkey_mutex))
2800 return -ERESTARTSYS;
2802 HOTKEY_CONFIG_CRITICAL_START
2803 hotkey_source_mask = t;
2804 HOTKEY_CONFIG_CRITICAL_END
2806 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2807 ~hotkey_source_mask);
2808 hotkey_poll_setup(true);
2810 /* check if events needed by the driver got disabled */
2811 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2812 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2814 mutex_unlock(&hotkey_mutex);
2816 if (rc < 0)
2817 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2818 "firmware event mask!\n");
2820 if (r_ev)
2821 printk(TPACPI_NOTICE "hotkey_source_mask: "
2822 "some important events were disabled: "
2823 "0x%04x\n", r_ev);
2825 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2827 return (rc < 0) ? rc : count;
2830 static struct device_attribute dev_attr_hotkey_source_mask =
2831 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2832 hotkey_source_mask_show, hotkey_source_mask_store);
2834 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2835 static ssize_t hotkey_poll_freq_show(struct device *dev,
2836 struct device_attribute *attr,
2837 char *buf)
2839 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2842 static ssize_t hotkey_poll_freq_store(struct device *dev,
2843 struct device_attribute *attr,
2844 const char *buf, size_t count)
2846 unsigned long t;
2848 if (parse_strtoul(buf, 25, &t))
2849 return -EINVAL;
2851 if (mutex_lock_killable(&hotkey_mutex))
2852 return -ERESTARTSYS;
2854 hotkey_poll_set_freq(t);
2855 hotkey_poll_setup(true);
2857 mutex_unlock(&hotkey_mutex);
2859 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2861 return count;
2864 static struct device_attribute dev_attr_hotkey_poll_freq =
2865 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2866 hotkey_poll_freq_show, hotkey_poll_freq_store);
2868 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2870 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2871 static ssize_t hotkey_radio_sw_show(struct device *dev,
2872 struct device_attribute *attr,
2873 char *buf)
2875 int res;
2876 res = hotkey_get_wlsw();
2877 if (res < 0)
2878 return res;
2880 /* Opportunistic update */
2881 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2883 return snprintf(buf, PAGE_SIZE, "%d\n",
2884 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2887 static struct device_attribute dev_attr_hotkey_radio_sw =
2888 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2890 static void hotkey_radio_sw_notify_change(void)
2892 if (tp_features.hotkey_wlsw)
2893 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2894 "hotkey_radio_sw");
2897 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2898 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2899 struct device_attribute *attr,
2900 char *buf)
2902 int res, s;
2903 res = hotkey_get_tablet_mode(&s);
2904 if (res < 0)
2905 return res;
2907 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2910 static struct device_attribute dev_attr_hotkey_tablet_mode =
2911 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2913 static void hotkey_tablet_mode_notify_change(void)
2915 if (tp_features.hotkey_tablet)
2916 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2917 "hotkey_tablet_mode");
2920 /* sysfs hotkey report_mode -------------------------------------------- */
2921 static ssize_t hotkey_report_mode_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2925 return snprintf(buf, PAGE_SIZE, "%d\n",
2926 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2929 static struct device_attribute dev_attr_hotkey_report_mode =
2930 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2932 /* sysfs wakeup reason (pollable) -------------------------------------- */
2933 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2934 struct device_attribute *attr,
2935 char *buf)
2937 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2940 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2941 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2943 static void hotkey_wakeup_reason_notify_change(void)
2945 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2946 "wakeup_reason");
2949 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2950 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2951 struct device_attribute *attr,
2952 char *buf)
2954 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2957 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2958 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2959 hotkey_wakeup_hotunplug_complete_show, NULL);
2961 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2963 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2964 "wakeup_hotunplug_complete");
2967 /* --------------------------------------------------------------------- */
2969 static struct attribute *hotkey_attributes[] __initdata = {
2970 &dev_attr_hotkey_enable.attr,
2971 &dev_attr_hotkey_bios_enabled.attr,
2972 &dev_attr_hotkey_bios_mask.attr,
2973 &dev_attr_hotkey_report_mode.attr,
2974 &dev_attr_hotkey_wakeup_reason.attr,
2975 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2976 &dev_attr_hotkey_mask.attr,
2977 &dev_attr_hotkey_all_mask.attr,
2978 &dev_attr_hotkey_recommended_mask.attr,
2979 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2980 &dev_attr_hotkey_source_mask.attr,
2981 &dev_attr_hotkey_poll_freq.attr,
2982 #endif
2986 * Sync both the hw and sw blocking state of all switches
2988 static void tpacpi_send_radiosw_update(void)
2990 int wlsw;
2993 * We must sync all rfkill controllers *before* issuing any
2994 * rfkill input events, or we will race the rfkill core input
2995 * handler.
2997 * tpacpi_inputdev_send_mutex works as a syncronization point
2998 * for the above.
3000 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3003 wlsw = hotkey_get_wlsw();
3005 /* Sync hw blocking state first if it is hw-blocked */
3006 if (wlsw == TPACPI_RFK_RADIO_OFF)
3007 tpacpi_rfk_update_hwblock_state(true);
3009 /* Sync sw blocking state */
3010 tpacpi_rfk_update_swstate_all();
3012 /* Sync hw blocking state last if it is hw-unblocked */
3013 if (wlsw == TPACPI_RFK_RADIO_ON)
3014 tpacpi_rfk_update_hwblock_state(false);
3016 /* Issue rfkill input event for WLSW switch */
3017 if (!(wlsw < 0)) {
3018 mutex_lock(&tpacpi_inputdev_send_mutex);
3020 input_report_switch(tpacpi_inputdev,
3021 SW_RFKILL_ALL, (wlsw > 0));
3022 input_sync(tpacpi_inputdev);
3024 mutex_unlock(&tpacpi_inputdev_send_mutex);
3028 * this can be unconditional, as we will poll state again
3029 * if userspace uses the notify to read data
3031 hotkey_radio_sw_notify_change();
3034 static void hotkey_exit(void)
3036 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3037 mutex_lock(&hotkey_mutex);
3038 hotkey_poll_stop_sync();
3039 mutex_unlock(&hotkey_mutex);
3040 #endif
3042 if (hotkey_dev_attributes)
3043 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3045 kfree(hotkey_keycode_map);
3047 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3048 "restoring original HKEY status and mask\n");
3049 /* yes, there is a bitwise or below, we want the
3050 * functions to be called even if one of them fail */
3051 if (((tp_features.hotkey_mask &&
3052 hotkey_mask_set(hotkey_orig_mask)) |
3053 hotkey_status_set(false)) != 0)
3054 printk(TPACPI_ERR
3055 "failed to restore hot key mask "
3056 "to BIOS defaults\n");
3059 static void __init hotkey_unmap(const unsigned int scancode)
3061 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3062 clear_bit(hotkey_keycode_map[scancode],
3063 tpacpi_inputdev->keybit);
3064 hotkey_keycode_map[scancode] = KEY_RESERVED;
3069 * HKEY quirks:
3070 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3073 #define TPACPI_HK_Q_INIMASK 0x0001
3075 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3076 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3077 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3078 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3079 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3080 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3081 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3082 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3083 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3084 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3085 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3086 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3087 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3088 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3089 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3090 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3091 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3092 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3093 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3094 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3097 typedef u16 tpacpi_keymap_entry_t;
3098 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3100 static int __init hotkey_init(struct ibm_init_struct *iibm)
3102 /* Requirements for changing the default keymaps:
3104 * 1. Many of the keys are mapped to KEY_RESERVED for very
3105 * good reasons. Do not change them unless you have deep
3106 * knowledge on the IBM and Lenovo ThinkPad firmware for
3107 * the various ThinkPad models. The driver behaves
3108 * differently for KEY_RESERVED: such keys have their
3109 * hot key mask *unset* in mask_recommended, and also
3110 * in the initial hot key mask programmed into the
3111 * firmware at driver load time, which means the firm-
3112 * ware may react very differently if you change them to
3113 * something else;
3115 * 2. You must be subscribed to the linux-thinkpad and
3116 * ibm-acpi-devel mailing lists, and you should read the
3117 * list archives since 2007 if you want to change the
3118 * keymaps. This requirement exists so that you will
3119 * know the past history of problems with the thinkpad-
3120 * acpi driver keymaps, and also that you will be
3121 * listening to any bug reports;
3123 * 3. Do not send thinkpad-acpi specific patches directly to
3124 * for merging, *ever*. Send them to the linux-acpi
3125 * mailinglist for comments. Merging is to be done only
3126 * through acpi-test and the ACPI maintainer.
3128 * If the above is too much to ask, don't change the keymap.
3129 * Ask the thinkpad-acpi maintainer to do it, instead.
3132 enum keymap_index {
3133 TPACPI_KEYMAP_IBM_GENERIC = 0,
3134 TPACPI_KEYMAP_LENOVO_GENERIC,
3137 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3138 /* Generic keymap for IBM ThinkPads */
3139 [TPACPI_KEYMAP_IBM_GENERIC] = {
3140 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3141 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3142 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3143 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3145 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3146 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3147 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3148 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3150 /* brightness: firmware always reacts to them */
3151 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3152 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3154 /* Thinklight: firmware always react to it */
3155 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3157 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3158 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3160 /* Volume: firmware always react to it and reprograms
3161 * the built-in *extra* mixer. Never map it to control
3162 * another mixer by default. */
3163 KEY_RESERVED, /* 0x14: VOLUME UP */
3164 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3165 KEY_RESERVED, /* 0x16: MUTE */
3167 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3169 /* (assignments unknown, please report if found) */
3170 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3171 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3174 /* Generic keymap for Lenovo ThinkPads */
3175 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3176 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3177 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3178 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3179 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3181 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3182 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3183 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3184 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3186 /* These should be enabled --only-- when ACPI video
3187 * is disabled (i.e. in "vendor" mode), and are handled
3188 * in a special way by the init code */
3189 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3190 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3192 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3194 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3195 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3197 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3198 * react to it and reprograms the built-in *extra* mixer.
3199 * Never map it to control another mixer by default.
3201 * T60?, T61, R60?, R61: firmware and EC tries to send
3202 * these over the regular keyboard, so these are no-ops,
3203 * but there are still weird bugs re. MUTE, so do not
3204 * change unless you get test reports from all Lenovo
3205 * models. May cause the BIOS to interfere with the
3206 * HDA mixer.
3208 KEY_RESERVED, /* 0x14: VOLUME UP */
3209 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3210 KEY_RESERVED, /* 0x16: MUTE */
3212 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3214 /* (assignments unknown, please report if found) */
3215 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3216 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3220 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3221 /* Generic maps (fallback) */
3223 .vendor = PCI_VENDOR_ID_IBM,
3224 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3225 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3228 .vendor = PCI_VENDOR_ID_LENOVO,
3229 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3230 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3234 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3235 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3237 int res, i;
3238 int status;
3239 int hkeyv;
3240 bool radiosw_state = false;
3241 bool tabletsw_state = false;
3243 unsigned long quirks;
3244 unsigned long keymap_id;
3246 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3247 "initializing hotkey subdriver\n");
3249 BUG_ON(!tpacpi_inputdev);
3250 BUG_ON(tpacpi_inputdev->open != NULL ||
3251 tpacpi_inputdev->close != NULL);
3253 TPACPI_ACPIHANDLE_INIT(hkey);
3254 mutex_init(&hotkey_mutex);
3256 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3257 mutex_init(&hotkey_thread_mutex);
3258 mutex_init(&hotkey_thread_data_mutex);
3259 #endif
3261 /* hotkey not supported on 570 */
3262 tp_features.hotkey = hkey_handle != NULL;
3264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "hotkeys are %s\n",
3266 str_supported(tp_features.hotkey));
3268 if (!tp_features.hotkey)
3269 return 1;
3271 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3272 ARRAY_SIZE(tpacpi_hotkey_qtable));
3274 tpacpi_disable_brightness_delay();
3276 /* MUST have enough space for all attributes to be added to
3277 * hotkey_dev_attributes */
3278 hotkey_dev_attributes = create_attr_set(
3279 ARRAY_SIZE(hotkey_attributes) + 2,
3280 NULL);
3281 if (!hotkey_dev_attributes)
3282 return -ENOMEM;
3283 res = add_many_to_attr_set(hotkey_dev_attributes,
3284 hotkey_attributes,
3285 ARRAY_SIZE(hotkey_attributes));
3286 if (res)
3287 goto err_exit;
3289 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3290 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3291 for HKEY interface version 0x100 */
3292 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3293 if ((hkeyv >> 8) != 1) {
3294 printk(TPACPI_ERR "unknown version of the "
3295 "HKEY interface: 0x%x\n", hkeyv);
3296 printk(TPACPI_ERR "please report this to %s\n",
3297 TPACPI_MAIL);
3298 } else {
3300 * MHKV 0x100 in A31, R40, R40e,
3301 * T4x, X31, and later
3303 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3304 "firmware HKEY interface version: 0x%x\n",
3305 hkeyv);
3307 /* Paranoia check AND init hotkey_all_mask */
3308 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3309 "MHKA", "qd")) {
3310 printk(TPACPI_ERR
3311 "missing MHKA handler, "
3312 "please report this to %s\n",
3313 TPACPI_MAIL);
3314 /* Fallback: pre-init for FN+F3,F4,F12 */
3315 hotkey_all_mask = 0x080cU;
3316 } else {
3317 tp_features.hotkey_mask = 1;
3322 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3323 "hotkey masks are %s\n",
3324 str_supported(tp_features.hotkey_mask));
3326 /* Init hotkey_all_mask if not initialized yet */
3327 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3328 (quirks & TPACPI_HK_Q_INIMASK))
3329 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3331 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3332 if (tp_features.hotkey_mask) {
3333 /* hotkey_source_mask *must* be zero for
3334 * the first hotkey_mask_get to return hotkey_orig_mask */
3335 res = hotkey_mask_get();
3336 if (res)
3337 goto err_exit;
3339 hotkey_orig_mask = hotkey_acpi_mask;
3340 } else {
3341 hotkey_orig_mask = hotkey_all_mask;
3342 hotkey_acpi_mask = hotkey_all_mask;
3345 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3346 if (dbg_wlswemul) {
3347 tp_features.hotkey_wlsw = 1;
3348 radiosw_state = !!tpacpi_wlsw_emulstate;
3349 printk(TPACPI_INFO
3350 "radio switch emulation enabled\n");
3351 } else
3352 #endif
3353 /* Not all thinkpads have a hardware radio switch */
3354 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3355 tp_features.hotkey_wlsw = 1;
3356 radiosw_state = !!status;
3357 printk(TPACPI_INFO
3358 "radio switch found; radios are %s\n",
3359 enabled(status, 0));
3361 if (tp_features.hotkey_wlsw)
3362 res = add_to_attr_set(hotkey_dev_attributes,
3363 &dev_attr_hotkey_radio_sw.attr);
3365 /* For X41t, X60t, X61t Tablets... */
3366 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3367 tp_features.hotkey_tablet = 1;
3368 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3369 printk(TPACPI_INFO
3370 "possible tablet mode switch found; "
3371 "ThinkPad in %s mode\n",
3372 (tabletsw_state) ? "tablet" : "laptop");
3373 res = add_to_attr_set(hotkey_dev_attributes,
3374 &dev_attr_hotkey_tablet_mode.attr);
3377 if (!res)
3378 res = register_attr_set_with_sysfs(
3379 hotkey_dev_attributes,
3380 &tpacpi_pdev->dev.kobj);
3381 if (res)
3382 goto err_exit;
3384 /* Set up key map */
3385 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3386 GFP_KERNEL);
3387 if (!hotkey_keycode_map) {
3388 printk(TPACPI_ERR
3389 "failed to allocate memory for key map\n");
3390 res = -ENOMEM;
3391 goto err_exit;
3394 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3395 ARRAY_SIZE(tpacpi_keymap_qtable));
3396 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3397 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3398 "using keymap number %lu\n", keymap_id);
3400 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3401 TPACPI_HOTKEY_MAP_SIZE);
3403 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3404 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3405 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3406 tpacpi_inputdev->keycode = hotkey_keycode_map;
3407 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3408 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3409 input_set_capability(tpacpi_inputdev, EV_KEY,
3410 hotkey_keycode_map[i]);
3411 } else {
3412 if (i < sizeof(hotkey_reserved_mask)*8)
3413 hotkey_reserved_mask |= 1 << i;
3417 if (tp_features.hotkey_wlsw) {
3418 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3419 input_report_switch(tpacpi_inputdev,
3420 SW_RFKILL_ALL, radiosw_state);
3422 if (tp_features.hotkey_tablet) {
3423 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3424 input_report_switch(tpacpi_inputdev,
3425 SW_TABLET_MODE, tabletsw_state);
3428 /* Do not issue duplicate brightness change events to
3429 * userspace. tpacpi_detect_brightness_capabilities() must have
3430 * been called before this point */
3431 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3432 printk(TPACPI_INFO
3433 "This ThinkPad has standard ACPI backlight "
3434 "brightness control, supported by the ACPI "
3435 "video driver\n");
3436 printk(TPACPI_NOTICE
3437 "Disabling thinkpad-acpi brightness events "
3438 "by default...\n");
3440 /* Disable brightness up/down on Lenovo thinkpads when
3441 * ACPI is handling them, otherwise it is plain impossible
3442 * for userspace to do something even remotely sane */
3443 hotkey_reserved_mask |=
3444 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3445 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3446 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3447 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3450 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3451 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3452 & ~hotkey_all_mask
3453 & ~hotkey_reserved_mask;
3455 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3456 "hotkey source mask 0x%08x, polling freq %u\n",
3457 hotkey_source_mask, hotkey_poll_freq);
3458 #endif
3460 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3461 "enabling firmware HKEY event interface...\n");
3462 res = hotkey_status_set(true);
3463 if (res) {
3464 hotkey_exit();
3465 return res;
3467 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3468 | hotkey_driver_mask)
3469 & ~hotkey_source_mask);
3470 if (res < 0 && res != -ENXIO) {
3471 hotkey_exit();
3472 return res;
3474 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3475 & ~hotkey_reserved_mask;
3476 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3477 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3478 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3480 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3481 "legacy ibm/hotkey event reporting over procfs %s\n",
3482 (hotkey_report_mode < 2) ?
3483 "enabled" : "disabled");
3485 tpacpi_inputdev->open = &hotkey_inputdev_open;
3486 tpacpi_inputdev->close = &hotkey_inputdev_close;
3488 hotkey_poll_setup_safe(true);
3490 return 0;
3492 err_exit:
3493 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3494 hotkey_dev_attributes = NULL;
3496 return (res < 0)? res : 1;
3499 static bool hotkey_notify_hotkey(const u32 hkey,
3500 bool *send_acpi_ev,
3501 bool *ignore_acpi_ev)
3503 /* 0x1000-0x1FFF: key presses */
3504 unsigned int scancode = hkey & 0xfff;
3505 *send_acpi_ev = true;
3506 *ignore_acpi_ev = false;
3508 /* HKEY event 0x1001 is scancode 0x00 */
3509 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3510 scancode--;
3511 if (!(hotkey_source_mask & (1 << scancode))) {
3512 tpacpi_input_send_key_masked(scancode);
3513 *send_acpi_ev = false;
3514 } else {
3515 *ignore_acpi_ev = true;
3517 return true;
3519 return false;
3522 static bool hotkey_notify_wakeup(const u32 hkey,
3523 bool *send_acpi_ev,
3524 bool *ignore_acpi_ev)
3526 /* 0x2000-0x2FFF: Wakeup reason */
3527 *send_acpi_ev = true;
3528 *ignore_acpi_ev = false;
3530 switch (hkey) {
3531 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3532 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3533 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3534 *ignore_acpi_ev = true;
3535 break;
3537 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3538 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3539 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3540 *ignore_acpi_ev = true;
3541 break;
3543 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3544 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3545 printk(TPACPI_ALERT
3546 "EMERGENCY WAKEUP: battery almost empty\n");
3547 /* how to auto-heal: */
3548 /* 2313: woke up from S3, go to S4/S5 */
3549 /* 2413: woke up from S4, go to S5 */
3550 break;
3552 default:
3553 return false;
3556 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3557 printk(TPACPI_INFO
3558 "woke up due to a hot-unplug "
3559 "request...\n");
3560 hotkey_wakeup_reason_notify_change();
3562 return true;
3565 static bool hotkey_notify_usrevent(const u32 hkey,
3566 bool *send_acpi_ev,
3567 bool *ignore_acpi_ev)
3569 /* 0x5000-0x5FFF: human interface helpers */
3570 *send_acpi_ev = true;
3571 *ignore_acpi_ev = false;
3573 switch (hkey) {
3574 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3575 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3576 return true;
3578 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3579 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3580 tpacpi_input_send_tabletsw();
3581 hotkey_tablet_mode_notify_change();
3582 *send_acpi_ev = false;
3583 return true;
3585 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3586 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3587 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3588 /* do not propagate these events */
3589 *ignore_acpi_ev = true;
3590 return true;
3592 default:
3593 return false;
3597 static void thermal_dump_all_sensors(void);
3599 static bool hotkey_notify_thermal(const u32 hkey,
3600 bool *send_acpi_ev,
3601 bool *ignore_acpi_ev)
3603 bool known = true;
3605 /* 0x6000-0x6FFF: thermal alarms */
3606 *send_acpi_ev = true;
3607 *ignore_acpi_ev = false;
3609 switch (hkey) {
3610 case TP_HKEY_EV_THM_TABLE_CHANGED:
3611 printk(TPACPI_INFO
3612 "EC reports that Thermal Table has changed\n");
3613 /* recommended action: do nothing, we don't have
3614 * Lenovo ATM information */
3615 return true;
3616 case TP_HKEY_EV_ALARM_BAT_HOT:
3617 printk(TPACPI_CRIT
3618 "THERMAL ALARM: battery is too hot!\n");
3619 /* recommended action: warn user through gui */
3620 break;
3621 case TP_HKEY_EV_ALARM_BAT_XHOT:
3622 printk(TPACPI_ALERT
3623 "THERMAL EMERGENCY: battery is extremely hot!\n");
3624 /* recommended action: immediate sleep/hibernate */
3625 break;
3626 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3627 printk(TPACPI_CRIT
3628 "THERMAL ALARM: "
3629 "a sensor reports something is too hot!\n");
3630 /* recommended action: warn user through gui, that */
3631 /* some internal component is too hot */
3632 break;
3633 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3634 printk(TPACPI_ALERT
3635 "THERMAL EMERGENCY: "
3636 "a sensor reports something is extremely hot!\n");
3637 /* recommended action: immediate sleep/hibernate */
3638 break;
3639 default:
3640 printk(TPACPI_ALERT
3641 "THERMAL ALERT: unknown thermal alarm received\n");
3642 known = false;
3645 thermal_dump_all_sensors();
3647 return known;
3650 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3652 u32 hkey;
3653 bool send_acpi_ev;
3654 bool ignore_acpi_ev;
3655 bool known_ev;
3657 if (event != 0x80) {
3658 printk(TPACPI_ERR
3659 "unknown HKEY notification event %d\n", event);
3660 /* forward it to userspace, maybe it knows how to handle it */
3661 acpi_bus_generate_netlink_event(
3662 ibm->acpi->device->pnp.device_class,
3663 dev_name(&ibm->acpi->device->dev),
3664 event, 0);
3665 return;
3668 while (1) {
3669 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3670 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3671 return;
3674 if (hkey == 0) {
3675 /* queue empty */
3676 return;
3679 send_acpi_ev = true;
3680 ignore_acpi_ev = false;
3682 switch (hkey >> 12) {
3683 case 1:
3684 /* 0x1000-0x1FFF: key presses */
3685 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3686 &ignore_acpi_ev);
3687 break;
3688 case 2:
3689 /* 0x2000-0x2FFF: Wakeup reason */
3690 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
3692 break;
3693 case 3:
3694 /* 0x3000-0x3FFF: bay-related wakeups */
3695 switch (hkey) {
3696 case TP_HKEY_EV_BAYEJ_ACK:
3697 hotkey_autosleep_ack = 1;
3698 printk(TPACPI_INFO
3699 "bay ejected\n");
3700 hotkey_wakeup_hotunplug_complete_notify_change();
3701 known_ev = true;
3702 break;
3703 case TP_HKEY_EV_OPTDRV_EJ:
3704 /* FIXME: kick libata if SATA link offline */
3705 known_ev = true;
3706 break;
3707 default:
3708 known_ev = false;
3710 break;
3711 case 4:
3712 /* 0x4000-0x4FFF: dock-related wakeups */
3713 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3714 hotkey_autosleep_ack = 1;
3715 printk(TPACPI_INFO
3716 "undocked\n");
3717 hotkey_wakeup_hotunplug_complete_notify_change();
3718 known_ev = true;
3719 } else {
3720 known_ev = false;
3722 break;
3723 case 5:
3724 /* 0x5000-0x5FFF: human interface helpers */
3725 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3726 &ignore_acpi_ev);
3727 break;
3728 case 6:
3729 /* 0x6000-0x6FFF: thermal alarms */
3730 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3731 &ignore_acpi_ev);
3732 break;
3733 case 7:
3734 /* 0x7000-0x7FFF: misc */
3735 if (tp_features.hotkey_wlsw &&
3736 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3737 tpacpi_send_radiosw_update();
3738 send_acpi_ev = 0;
3739 known_ev = true;
3740 break;
3742 /* fallthrough to default */
3743 default:
3744 known_ev = false;
3746 if (!known_ev) {
3747 printk(TPACPI_NOTICE
3748 "unhandled HKEY event 0x%04x\n", hkey);
3749 printk(TPACPI_NOTICE
3750 "please report the conditions when this "
3751 "event happened to %s\n", TPACPI_MAIL);
3754 /* Legacy events */
3755 if (!ignore_acpi_ev &&
3756 (send_acpi_ev || hotkey_report_mode < 2)) {
3757 acpi_bus_generate_proc_event(ibm->acpi->device,
3758 event, hkey);
3761 /* netlink events */
3762 if (!ignore_acpi_ev && send_acpi_ev) {
3763 acpi_bus_generate_netlink_event(
3764 ibm->acpi->device->pnp.device_class,
3765 dev_name(&ibm->acpi->device->dev),
3766 event, hkey);
3771 static void hotkey_suspend(pm_message_t state)
3773 /* Do these on suspend, we get the events on early resume! */
3774 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3775 hotkey_autosleep_ack = 0;
3778 static void hotkey_resume(void)
3780 tpacpi_disable_brightness_delay();
3782 if (hotkey_status_set(true) < 0 ||
3783 hotkey_mask_set(hotkey_acpi_mask) < 0)
3784 printk(TPACPI_ERR
3785 "error while attempting to reset the event "
3786 "firmware interface\n");
3788 tpacpi_send_radiosw_update();
3789 hotkey_tablet_mode_notify_change();
3790 hotkey_wakeup_reason_notify_change();
3791 hotkey_wakeup_hotunplug_complete_notify_change();
3792 hotkey_poll_setup_safe(false);
3795 /* procfs -------------------------------------------------------------- */
3796 static int hotkey_read(struct seq_file *m)
3798 int res, status;
3800 if (!tp_features.hotkey) {
3801 seq_printf(m, "status:\t\tnot supported\n");
3802 return 0;
3805 if (mutex_lock_killable(&hotkey_mutex))
3806 return -ERESTARTSYS;
3807 res = hotkey_status_get(&status);
3808 if (!res)
3809 res = hotkey_mask_get();
3810 mutex_unlock(&hotkey_mutex);
3811 if (res)
3812 return res;
3814 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3815 if (hotkey_all_mask) {
3816 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3817 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3818 } else {
3819 seq_printf(m, "mask:\t\tnot supported\n");
3820 seq_printf(m, "commands:\tenable, disable, reset\n");
3823 return 0;
3826 static void hotkey_enabledisable_warn(bool enable)
3828 tpacpi_log_usertask("procfs hotkey enable/disable");
3829 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3830 TPACPI_WARN
3831 "hotkey enable/disable functionality has been "
3832 "removed from the driver. Hotkeys are always "
3833 "enabled\n"))
3834 printk(TPACPI_ERR
3835 "Please remove the hotkey=enable module "
3836 "parameter, it is deprecated. Hotkeys are always "
3837 "enabled\n");
3840 static int hotkey_write(char *buf)
3842 int res;
3843 u32 mask;
3844 char *cmd;
3846 if (!tp_features.hotkey)
3847 return -ENODEV;
3849 if (mutex_lock_killable(&hotkey_mutex))
3850 return -ERESTARTSYS;
3852 mask = hotkey_user_mask;
3854 res = 0;
3855 while ((cmd = next_cmd(&buf))) {
3856 if (strlencmp(cmd, "enable") == 0) {
3857 hotkey_enabledisable_warn(1);
3858 } else if (strlencmp(cmd, "disable") == 0) {
3859 hotkey_enabledisable_warn(0);
3860 res = -EPERM;
3861 } else if (strlencmp(cmd, "reset") == 0) {
3862 mask = (hotkey_all_mask | hotkey_source_mask)
3863 & ~hotkey_reserved_mask;
3864 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3865 /* mask set */
3866 } else if (sscanf(cmd, "%x", &mask) == 1) {
3867 /* mask set */
3868 } else {
3869 res = -EINVAL;
3870 goto errexit;
3874 if (!res) {
3875 tpacpi_disclose_usertask("procfs hotkey",
3876 "set mask to 0x%08x\n", mask);
3877 res = hotkey_user_mask_set(mask);
3880 errexit:
3881 mutex_unlock(&hotkey_mutex);
3882 return res;
3885 static const struct acpi_device_id ibm_htk_device_ids[] = {
3886 {TPACPI_ACPI_HKEY_HID, 0},
3887 {"", 0},
3890 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3891 .hid = ibm_htk_device_ids,
3892 .notify = hotkey_notify,
3893 .handle = &hkey_handle,
3894 .type = ACPI_DEVICE_NOTIFY,
3897 static struct ibm_struct hotkey_driver_data = {
3898 .name = "hotkey",
3899 .read = hotkey_read,
3900 .write = hotkey_write,
3901 .exit = hotkey_exit,
3902 .resume = hotkey_resume,
3903 .suspend = hotkey_suspend,
3904 .acpi = &ibm_hotkey_acpidriver,
3907 /*************************************************************************
3908 * Bluetooth subdriver
3911 enum {
3912 /* ACPI GBDC/SBDC bits */
3913 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3914 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3915 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3916 0 = disable, 1 = enable */
3919 enum {
3920 /* ACPI \BLTH commands */
3921 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3922 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3923 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3924 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3925 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3928 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3930 static int bluetooth_get_status(void)
3932 int status;
3934 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3935 if (dbg_bluetoothemul)
3936 return (tpacpi_bluetooth_emulstate) ?
3937 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3938 #endif
3940 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3941 return -EIO;
3943 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3944 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3947 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3949 int status;
3951 vdbg_printk(TPACPI_DBG_RFKILL,
3952 "will attempt to %s bluetooth\n",
3953 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3955 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3956 if (dbg_bluetoothemul) {
3957 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3958 return 0;
3960 #endif
3962 if (state == TPACPI_RFK_RADIO_ON)
3963 status = TP_ACPI_BLUETOOTH_RADIOSSW
3964 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3965 else
3966 status = 0;
3968 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3969 return -EIO;
3971 return 0;
3974 /* sysfs bluetooth enable ---------------------------------------------- */
3975 static ssize_t bluetooth_enable_show(struct device *dev,
3976 struct device_attribute *attr,
3977 char *buf)
3979 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3980 attr, buf);
3983 static ssize_t bluetooth_enable_store(struct device *dev,
3984 struct device_attribute *attr,
3985 const char *buf, size_t count)
3987 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3988 attr, buf, count);
3991 static struct device_attribute dev_attr_bluetooth_enable =
3992 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3993 bluetooth_enable_show, bluetooth_enable_store);
3995 /* --------------------------------------------------------------------- */
3997 static struct attribute *bluetooth_attributes[] = {
3998 &dev_attr_bluetooth_enable.attr,
3999 NULL
4002 static const struct attribute_group bluetooth_attr_group = {
4003 .attrs = bluetooth_attributes,
4006 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4007 .get_status = bluetooth_get_status,
4008 .set_status = bluetooth_set_status,
4011 static void bluetooth_shutdown(void)
4013 /* Order firmware to save current state to NVRAM */
4014 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4015 TP_ACPI_BLTH_SAVE_STATE))
4016 printk(TPACPI_NOTICE
4017 "failed to save bluetooth state to NVRAM\n");
4018 else
4019 vdbg_printk(TPACPI_DBG_RFKILL,
4020 "bluestooth state saved to NVRAM\n");
4023 static void bluetooth_exit(void)
4025 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4026 &bluetooth_attr_group);
4028 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4030 bluetooth_shutdown();
4033 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4035 int res;
4036 int status = 0;
4038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4039 "initializing bluetooth subdriver\n");
4041 TPACPI_ACPIHANDLE_INIT(hkey);
4043 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4044 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4045 tp_features.bluetooth = hkey_handle &&
4046 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4048 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4049 "bluetooth is %s, status 0x%02x\n",
4050 str_supported(tp_features.bluetooth),
4051 status);
4053 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4054 if (dbg_bluetoothemul) {
4055 tp_features.bluetooth = 1;
4056 printk(TPACPI_INFO
4057 "bluetooth switch emulation enabled\n");
4058 } else
4059 #endif
4060 if (tp_features.bluetooth &&
4061 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4062 /* no bluetooth hardware present in system */
4063 tp_features.bluetooth = 0;
4064 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4065 "bluetooth hardware not installed\n");
4068 if (!tp_features.bluetooth)
4069 return 1;
4071 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4072 &bluetooth_tprfk_ops,
4073 RFKILL_TYPE_BLUETOOTH,
4074 TPACPI_RFK_BLUETOOTH_SW_NAME,
4075 true);
4076 if (res)
4077 return res;
4079 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4080 &bluetooth_attr_group);
4081 if (res) {
4082 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4083 return res;
4086 return 0;
4089 /* procfs -------------------------------------------------------------- */
4090 static int bluetooth_read(struct seq_file *m)
4092 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4095 static int bluetooth_write(char *buf)
4097 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4100 static struct ibm_struct bluetooth_driver_data = {
4101 .name = "bluetooth",
4102 .read = bluetooth_read,
4103 .write = bluetooth_write,
4104 .exit = bluetooth_exit,
4105 .shutdown = bluetooth_shutdown,
4108 /*************************************************************************
4109 * Wan subdriver
4112 enum {
4113 /* ACPI GWAN/SWAN bits */
4114 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4115 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4116 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4117 0 = disable, 1 = enable */
4120 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4122 static int wan_get_status(void)
4124 int status;
4126 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4127 if (dbg_wwanemul)
4128 return (tpacpi_wwan_emulstate) ?
4129 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4130 #endif
4132 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4133 return -EIO;
4135 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4136 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4139 static int wan_set_status(enum tpacpi_rfkill_state state)
4141 int status;
4143 vdbg_printk(TPACPI_DBG_RFKILL,
4144 "will attempt to %s wwan\n",
4145 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4147 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4148 if (dbg_wwanemul) {
4149 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4150 return 0;
4152 #endif
4154 if (state == TPACPI_RFK_RADIO_ON)
4155 status = TP_ACPI_WANCARD_RADIOSSW
4156 | TP_ACPI_WANCARD_RESUMECTRL;
4157 else
4158 status = 0;
4160 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4161 return -EIO;
4163 return 0;
4166 /* sysfs wan enable ---------------------------------------------------- */
4167 static ssize_t wan_enable_show(struct device *dev,
4168 struct device_attribute *attr,
4169 char *buf)
4171 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4172 attr, buf);
4175 static ssize_t wan_enable_store(struct device *dev,
4176 struct device_attribute *attr,
4177 const char *buf, size_t count)
4179 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4180 attr, buf, count);
4183 static struct device_attribute dev_attr_wan_enable =
4184 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4185 wan_enable_show, wan_enable_store);
4187 /* --------------------------------------------------------------------- */
4189 static struct attribute *wan_attributes[] = {
4190 &dev_attr_wan_enable.attr,
4191 NULL
4194 static const struct attribute_group wan_attr_group = {
4195 .attrs = wan_attributes,
4198 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4199 .get_status = wan_get_status,
4200 .set_status = wan_set_status,
4203 static void wan_shutdown(void)
4205 /* Order firmware to save current state to NVRAM */
4206 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4207 TP_ACPI_WGSV_SAVE_STATE))
4208 printk(TPACPI_NOTICE
4209 "failed to save WWAN state to NVRAM\n");
4210 else
4211 vdbg_printk(TPACPI_DBG_RFKILL,
4212 "WWAN state saved to NVRAM\n");
4215 static void wan_exit(void)
4217 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4218 &wan_attr_group);
4220 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4222 wan_shutdown();
4225 static int __init wan_init(struct ibm_init_struct *iibm)
4227 int res;
4228 int status = 0;
4230 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4231 "initializing wan subdriver\n");
4233 TPACPI_ACPIHANDLE_INIT(hkey);
4235 tp_features.wan = hkey_handle &&
4236 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4238 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4239 "wan is %s, status 0x%02x\n",
4240 str_supported(tp_features.wan),
4241 status);
4243 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4244 if (dbg_wwanemul) {
4245 tp_features.wan = 1;
4246 printk(TPACPI_INFO
4247 "wwan switch emulation enabled\n");
4248 } else
4249 #endif
4250 if (tp_features.wan &&
4251 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4252 /* no wan hardware present in system */
4253 tp_features.wan = 0;
4254 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4255 "wan hardware not installed\n");
4258 if (!tp_features.wan)
4259 return 1;
4261 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4262 &wan_tprfk_ops,
4263 RFKILL_TYPE_WWAN,
4264 TPACPI_RFK_WWAN_SW_NAME,
4265 true);
4266 if (res)
4267 return res;
4269 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4270 &wan_attr_group);
4272 if (res) {
4273 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4274 return res;
4277 return 0;
4280 /* procfs -------------------------------------------------------------- */
4281 static int wan_read(struct seq_file *m)
4283 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4286 static int wan_write(char *buf)
4288 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4291 static struct ibm_struct wan_driver_data = {
4292 .name = "wan",
4293 .read = wan_read,
4294 .write = wan_write,
4295 .exit = wan_exit,
4296 .shutdown = wan_shutdown,
4299 /*************************************************************************
4300 * UWB subdriver
4303 enum {
4304 /* ACPI GUWB/SUWB bits */
4305 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4306 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4309 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4311 static int uwb_get_status(void)
4313 int status;
4315 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4316 if (dbg_uwbemul)
4317 return (tpacpi_uwb_emulstate) ?
4318 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4319 #endif
4321 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4322 return -EIO;
4324 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4325 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4328 static int uwb_set_status(enum tpacpi_rfkill_state state)
4330 int status;
4332 vdbg_printk(TPACPI_DBG_RFKILL,
4333 "will attempt to %s UWB\n",
4334 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4336 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4337 if (dbg_uwbemul) {
4338 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4339 return 0;
4341 #endif
4343 if (state == TPACPI_RFK_RADIO_ON)
4344 status = TP_ACPI_UWB_RADIOSSW;
4345 else
4346 status = 0;
4348 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4349 return -EIO;
4351 return 0;
4354 /* --------------------------------------------------------------------- */
4356 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4357 .get_status = uwb_get_status,
4358 .set_status = uwb_set_status,
4361 static void uwb_exit(void)
4363 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4366 static int __init uwb_init(struct ibm_init_struct *iibm)
4368 int res;
4369 int status = 0;
4371 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4372 "initializing uwb subdriver\n");
4374 TPACPI_ACPIHANDLE_INIT(hkey);
4376 tp_features.uwb = hkey_handle &&
4377 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4379 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4380 "uwb is %s, status 0x%02x\n",
4381 str_supported(tp_features.uwb),
4382 status);
4384 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4385 if (dbg_uwbemul) {
4386 tp_features.uwb = 1;
4387 printk(TPACPI_INFO
4388 "uwb switch emulation enabled\n");
4389 } else
4390 #endif
4391 if (tp_features.uwb &&
4392 !(status & TP_ACPI_UWB_HWPRESENT)) {
4393 /* no uwb hardware present in system */
4394 tp_features.uwb = 0;
4395 dbg_printk(TPACPI_DBG_INIT,
4396 "uwb hardware not installed\n");
4399 if (!tp_features.uwb)
4400 return 1;
4402 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4403 &uwb_tprfk_ops,
4404 RFKILL_TYPE_UWB,
4405 TPACPI_RFK_UWB_SW_NAME,
4406 false);
4407 return res;
4410 static struct ibm_struct uwb_driver_data = {
4411 .name = "uwb",
4412 .exit = uwb_exit,
4413 .flags.experimental = 1,
4416 /*************************************************************************
4417 * Video subdriver
4420 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4422 enum video_access_mode {
4423 TPACPI_VIDEO_NONE = 0,
4424 TPACPI_VIDEO_570, /* 570 */
4425 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4426 TPACPI_VIDEO_NEW, /* all others */
4429 enum { /* video status flags, based on VIDEO_570 */
4430 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4431 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4432 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4435 enum { /* TPACPI_VIDEO_570 constants */
4436 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4437 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4438 * video_status_flags */
4439 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4440 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4443 static enum video_access_mode video_supported;
4444 static int video_orig_autosw;
4446 static int video_autosw_get(void);
4447 static int video_autosw_set(int enable);
4449 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4451 static int __init video_init(struct ibm_init_struct *iibm)
4453 int ivga;
4455 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4457 TPACPI_ACPIHANDLE_INIT(vid);
4458 if (tpacpi_is_ibm())
4459 TPACPI_ACPIHANDLE_INIT(vid2);
4461 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4462 /* G41, assume IVGA doesn't change */
4463 vid_handle = vid2_handle;
4465 if (!vid_handle)
4466 /* video switching not supported on R30, R31 */
4467 video_supported = TPACPI_VIDEO_NONE;
4468 else if (tpacpi_is_ibm() &&
4469 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4470 /* 570 */
4471 video_supported = TPACPI_VIDEO_570;
4472 else if (tpacpi_is_ibm() &&
4473 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4474 /* 600e/x, 770e, 770x */
4475 video_supported = TPACPI_VIDEO_770;
4476 else
4477 /* all others */
4478 video_supported = TPACPI_VIDEO_NEW;
4480 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4481 str_supported(video_supported != TPACPI_VIDEO_NONE),
4482 video_supported);
4484 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4487 static void video_exit(void)
4489 dbg_printk(TPACPI_DBG_EXIT,
4490 "restoring original video autoswitch mode\n");
4491 if (video_autosw_set(video_orig_autosw))
4492 printk(TPACPI_ERR "error while trying to restore original "
4493 "video autoswitch mode\n");
4496 static int video_outputsw_get(void)
4498 int status = 0;
4499 int i;
4501 switch (video_supported) {
4502 case TPACPI_VIDEO_570:
4503 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4504 TP_ACPI_VIDEO_570_PHSCMD))
4505 return -EIO;
4506 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4507 break;
4508 case TPACPI_VIDEO_770:
4509 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4510 return -EIO;
4511 if (i)
4512 status |= TP_ACPI_VIDEO_S_LCD;
4513 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4514 return -EIO;
4515 if (i)
4516 status |= TP_ACPI_VIDEO_S_CRT;
4517 break;
4518 case TPACPI_VIDEO_NEW:
4519 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4520 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4521 return -EIO;
4522 if (i)
4523 status |= TP_ACPI_VIDEO_S_CRT;
4525 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4526 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4527 return -EIO;
4528 if (i)
4529 status |= TP_ACPI_VIDEO_S_LCD;
4530 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4531 return -EIO;
4532 if (i)
4533 status |= TP_ACPI_VIDEO_S_DVI;
4534 break;
4535 default:
4536 return -ENOSYS;
4539 return status;
4542 static int video_outputsw_set(int status)
4544 int autosw;
4545 int res = 0;
4547 switch (video_supported) {
4548 case TPACPI_VIDEO_570:
4549 res = acpi_evalf(NULL, NULL,
4550 "\\_SB.PHS2", "vdd",
4551 TP_ACPI_VIDEO_570_PHS2CMD,
4552 status | TP_ACPI_VIDEO_570_PHS2SET);
4553 break;
4554 case TPACPI_VIDEO_770:
4555 autosw = video_autosw_get();
4556 if (autosw < 0)
4557 return autosw;
4559 res = video_autosw_set(1);
4560 if (res)
4561 return res;
4562 res = acpi_evalf(vid_handle, NULL,
4563 "ASWT", "vdd", status * 0x100, 0);
4564 if (!autosw && video_autosw_set(autosw)) {
4565 printk(TPACPI_ERR
4566 "video auto-switch left enabled due to error\n");
4567 return -EIO;
4569 break;
4570 case TPACPI_VIDEO_NEW:
4571 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4572 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4573 break;
4574 default:
4575 return -ENOSYS;
4578 return (res)? 0 : -EIO;
4581 static int video_autosw_get(void)
4583 int autosw = 0;
4585 switch (video_supported) {
4586 case TPACPI_VIDEO_570:
4587 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4588 return -EIO;
4589 break;
4590 case TPACPI_VIDEO_770:
4591 case TPACPI_VIDEO_NEW:
4592 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4593 return -EIO;
4594 break;
4595 default:
4596 return -ENOSYS;
4599 return autosw & 1;
4602 static int video_autosw_set(int enable)
4604 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4605 return -EIO;
4606 return 0;
4609 static int video_outputsw_cycle(void)
4611 int autosw = video_autosw_get();
4612 int res;
4614 if (autosw < 0)
4615 return autosw;
4617 switch (video_supported) {
4618 case TPACPI_VIDEO_570:
4619 res = video_autosw_set(1);
4620 if (res)
4621 return res;
4622 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4623 break;
4624 case TPACPI_VIDEO_770:
4625 case TPACPI_VIDEO_NEW:
4626 res = video_autosw_set(1);
4627 if (res)
4628 return res;
4629 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4630 break;
4631 default:
4632 return -ENOSYS;
4634 if (!autosw && video_autosw_set(autosw)) {
4635 printk(TPACPI_ERR
4636 "video auto-switch left enabled due to error\n");
4637 return -EIO;
4640 return (res)? 0 : -EIO;
4643 static int video_expand_toggle(void)
4645 switch (video_supported) {
4646 case TPACPI_VIDEO_570:
4647 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4648 0 : -EIO;
4649 case TPACPI_VIDEO_770:
4650 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4651 0 : -EIO;
4652 case TPACPI_VIDEO_NEW:
4653 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4654 0 : -EIO;
4655 default:
4656 return -ENOSYS;
4658 /* not reached */
4661 static int video_read(struct seq_file *m)
4663 int status, autosw;
4665 if (video_supported == TPACPI_VIDEO_NONE) {
4666 seq_printf(m, "status:\t\tnot supported\n");
4667 return 0;
4670 /* Even reads can crash X.org, so... */
4671 if (!capable(CAP_SYS_ADMIN))
4672 return -EPERM;
4674 status = video_outputsw_get();
4675 if (status < 0)
4676 return status;
4678 autosw = video_autosw_get();
4679 if (autosw < 0)
4680 return autosw;
4682 seq_printf(m, "status:\t\tsupported\n");
4683 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4684 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4685 if (video_supported == TPACPI_VIDEO_NEW)
4686 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4687 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4688 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4689 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4690 if (video_supported == TPACPI_VIDEO_NEW)
4691 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4692 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4693 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4695 return 0;
4698 static int video_write(char *buf)
4700 char *cmd;
4701 int enable, disable, status;
4702 int res;
4704 if (video_supported == TPACPI_VIDEO_NONE)
4705 return -ENODEV;
4707 /* Even reads can crash X.org, let alone writes... */
4708 if (!capable(CAP_SYS_ADMIN))
4709 return -EPERM;
4711 enable = 0;
4712 disable = 0;
4714 while ((cmd = next_cmd(&buf))) {
4715 if (strlencmp(cmd, "lcd_enable") == 0) {
4716 enable |= TP_ACPI_VIDEO_S_LCD;
4717 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4718 disable |= TP_ACPI_VIDEO_S_LCD;
4719 } else if (strlencmp(cmd, "crt_enable") == 0) {
4720 enable |= TP_ACPI_VIDEO_S_CRT;
4721 } else if (strlencmp(cmd, "crt_disable") == 0) {
4722 disable |= TP_ACPI_VIDEO_S_CRT;
4723 } else if (video_supported == TPACPI_VIDEO_NEW &&
4724 strlencmp(cmd, "dvi_enable") == 0) {
4725 enable |= TP_ACPI_VIDEO_S_DVI;
4726 } else if (video_supported == TPACPI_VIDEO_NEW &&
4727 strlencmp(cmd, "dvi_disable") == 0) {
4728 disable |= TP_ACPI_VIDEO_S_DVI;
4729 } else if (strlencmp(cmd, "auto_enable") == 0) {
4730 res = video_autosw_set(1);
4731 if (res)
4732 return res;
4733 } else if (strlencmp(cmd, "auto_disable") == 0) {
4734 res = video_autosw_set(0);
4735 if (res)
4736 return res;
4737 } else if (strlencmp(cmd, "video_switch") == 0) {
4738 res = video_outputsw_cycle();
4739 if (res)
4740 return res;
4741 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4742 res = video_expand_toggle();
4743 if (res)
4744 return res;
4745 } else
4746 return -EINVAL;
4749 if (enable || disable) {
4750 status = video_outputsw_get();
4751 if (status < 0)
4752 return status;
4753 res = video_outputsw_set((status & ~disable) | enable);
4754 if (res)
4755 return res;
4758 return 0;
4761 static struct ibm_struct video_driver_data = {
4762 .name = "video",
4763 .read = video_read,
4764 .write = video_write,
4765 .exit = video_exit,
4768 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4770 /*************************************************************************
4771 * Light (thinklight) subdriver
4774 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4775 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4777 static int light_get_status(void)
4779 int status = 0;
4781 if (tp_features.light_status) {
4782 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4783 return -EIO;
4784 return (!!status);
4787 return -ENXIO;
4790 static int light_set_status(int status)
4792 int rc;
4794 if (tp_features.light) {
4795 if (cmos_handle) {
4796 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4797 (status)?
4798 TP_CMOS_THINKLIGHT_ON :
4799 TP_CMOS_THINKLIGHT_OFF);
4800 } else {
4801 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4802 (status)? 1 : 0);
4804 return (rc)? 0 : -EIO;
4807 return -ENXIO;
4810 static void light_set_status_worker(struct work_struct *work)
4812 struct tpacpi_led_classdev *data =
4813 container_of(work, struct tpacpi_led_classdev, work);
4815 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4816 light_set_status((data->new_state != TPACPI_LED_OFF));
4819 static void light_sysfs_set(struct led_classdev *led_cdev,
4820 enum led_brightness brightness)
4822 struct tpacpi_led_classdev *data =
4823 container_of(led_cdev,
4824 struct tpacpi_led_classdev,
4825 led_classdev);
4826 data->new_state = (brightness != LED_OFF) ?
4827 TPACPI_LED_ON : TPACPI_LED_OFF;
4828 queue_work(tpacpi_wq, &data->work);
4831 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4833 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4836 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4837 .led_classdev = {
4838 .name = "tpacpi::thinklight",
4839 .brightness_set = &light_sysfs_set,
4840 .brightness_get = &light_sysfs_get,
4844 static int __init light_init(struct ibm_init_struct *iibm)
4846 int rc;
4848 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4850 if (tpacpi_is_ibm()) {
4851 TPACPI_ACPIHANDLE_INIT(ledb);
4852 TPACPI_ACPIHANDLE_INIT(lght);
4854 TPACPI_ACPIHANDLE_INIT(cmos);
4855 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4857 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4858 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4860 if (tp_features.light)
4861 /* light status not supported on
4862 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4863 tp_features.light_status =
4864 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4866 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4867 str_supported(tp_features.light),
4868 str_supported(tp_features.light_status));
4870 if (!tp_features.light)
4871 return 1;
4873 rc = led_classdev_register(&tpacpi_pdev->dev,
4874 &tpacpi_led_thinklight.led_classdev);
4876 if (rc < 0) {
4877 tp_features.light = 0;
4878 tp_features.light_status = 0;
4879 } else {
4880 rc = 0;
4883 return rc;
4886 static void light_exit(void)
4888 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4889 if (work_pending(&tpacpi_led_thinklight.work))
4890 flush_workqueue(tpacpi_wq);
4893 static int light_read(struct seq_file *m)
4895 int status;
4897 if (!tp_features.light) {
4898 seq_printf(m, "status:\t\tnot supported\n");
4899 } else if (!tp_features.light_status) {
4900 seq_printf(m, "status:\t\tunknown\n");
4901 seq_printf(m, "commands:\ton, off\n");
4902 } else {
4903 status = light_get_status();
4904 if (status < 0)
4905 return status;
4906 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4907 seq_printf(m, "commands:\ton, off\n");
4910 return 0;
4913 static int light_write(char *buf)
4915 char *cmd;
4916 int newstatus = 0;
4918 if (!tp_features.light)
4919 return -ENODEV;
4921 while ((cmd = next_cmd(&buf))) {
4922 if (strlencmp(cmd, "on") == 0) {
4923 newstatus = 1;
4924 } else if (strlencmp(cmd, "off") == 0) {
4925 newstatus = 0;
4926 } else
4927 return -EINVAL;
4930 return light_set_status(newstatus);
4933 static struct ibm_struct light_driver_data = {
4934 .name = "light",
4935 .read = light_read,
4936 .write = light_write,
4937 .exit = light_exit,
4940 /*************************************************************************
4941 * CMOS subdriver
4944 /* sysfs cmos_command -------------------------------------------------- */
4945 static ssize_t cmos_command_store(struct device *dev,
4946 struct device_attribute *attr,
4947 const char *buf, size_t count)
4949 unsigned long cmos_cmd;
4950 int res;
4952 if (parse_strtoul(buf, 21, &cmos_cmd))
4953 return -EINVAL;
4955 res = issue_thinkpad_cmos_command(cmos_cmd);
4956 return (res)? res : count;
4959 static struct device_attribute dev_attr_cmos_command =
4960 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4962 /* --------------------------------------------------------------------- */
4964 static int __init cmos_init(struct ibm_init_struct *iibm)
4966 int res;
4968 vdbg_printk(TPACPI_DBG_INIT,
4969 "initializing cmos commands subdriver\n");
4971 TPACPI_ACPIHANDLE_INIT(cmos);
4973 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4974 str_supported(cmos_handle != NULL));
4976 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4977 if (res)
4978 return res;
4980 return (cmos_handle)? 0 : 1;
4983 static void cmos_exit(void)
4985 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4988 static int cmos_read(struct seq_file *m)
4990 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4991 R30, R31, T20-22, X20-21 */
4992 if (!cmos_handle)
4993 seq_printf(m, "status:\t\tnot supported\n");
4994 else {
4995 seq_printf(m, "status:\t\tsupported\n");
4996 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4999 return 0;
5002 static int cmos_write(char *buf)
5004 char *cmd;
5005 int cmos_cmd, res;
5007 while ((cmd = next_cmd(&buf))) {
5008 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5009 cmos_cmd >= 0 && cmos_cmd <= 21) {
5010 /* cmos_cmd set */
5011 } else
5012 return -EINVAL;
5014 res = issue_thinkpad_cmos_command(cmos_cmd);
5015 if (res)
5016 return res;
5019 return 0;
5022 static struct ibm_struct cmos_driver_data = {
5023 .name = "cmos",
5024 .read = cmos_read,
5025 .write = cmos_write,
5026 .exit = cmos_exit,
5029 /*************************************************************************
5030 * LED subdriver
5033 enum led_access_mode {
5034 TPACPI_LED_NONE = 0,
5035 TPACPI_LED_570, /* 570 */
5036 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5037 TPACPI_LED_NEW, /* all others */
5040 enum { /* For TPACPI_LED_OLD */
5041 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5042 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5043 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5046 static enum led_access_mode led_supported;
5048 static acpi_handle led_handle;
5050 #define TPACPI_LED_NUMLEDS 16
5051 static struct tpacpi_led_classdev *tpacpi_leds;
5052 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5053 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5054 /* there's a limit of 19 chars + NULL before 2.6.26 */
5055 "tpacpi::power",
5056 "tpacpi:orange:batt",
5057 "tpacpi:green:batt",
5058 "tpacpi::dock_active",
5059 "tpacpi::bay_active",
5060 "tpacpi::dock_batt",
5061 "tpacpi::unknown_led",
5062 "tpacpi::standby",
5063 "tpacpi::dock_status1",
5064 "tpacpi::dock_status2",
5065 "tpacpi::unknown_led2",
5066 "tpacpi::unknown_led3",
5067 "tpacpi::thinkvantage",
5069 #define TPACPI_SAFE_LEDS 0x1081U
5071 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5073 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5074 return false;
5075 #else
5076 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5077 #endif
5080 static int led_get_status(const unsigned int led)
5082 int status;
5083 enum led_status_t led_s;
5085 switch (led_supported) {
5086 case TPACPI_LED_570:
5087 if (!acpi_evalf(ec_handle,
5088 &status, "GLED", "dd", 1 << led))
5089 return -EIO;
5090 led_s = (status == 0)?
5091 TPACPI_LED_OFF :
5092 ((status == 1)?
5093 TPACPI_LED_ON :
5094 TPACPI_LED_BLINK);
5095 tpacpi_led_state_cache[led] = led_s;
5096 return led_s;
5097 default:
5098 return -ENXIO;
5101 /* not reached */
5104 static int led_set_status(const unsigned int led,
5105 const enum led_status_t ledstatus)
5107 /* off, on, blink. Index is led_status_t */
5108 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5109 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5111 int rc = 0;
5113 switch (led_supported) {
5114 case TPACPI_LED_570:
5115 /* 570 */
5116 if (unlikely(led > 7))
5117 return -EINVAL;
5118 if (unlikely(tpacpi_is_led_restricted(led)))
5119 return -EPERM;
5120 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5121 (1 << led), led_sled_arg1[ledstatus]))
5122 rc = -EIO;
5123 break;
5124 case TPACPI_LED_OLD:
5125 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5126 if (unlikely(led > 7))
5127 return -EINVAL;
5128 if (unlikely(tpacpi_is_led_restricted(led)))
5129 return -EPERM;
5130 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5131 if (rc >= 0)
5132 rc = ec_write(TPACPI_LED_EC_HLBL,
5133 (ledstatus == TPACPI_LED_BLINK) << led);
5134 if (rc >= 0)
5135 rc = ec_write(TPACPI_LED_EC_HLCL,
5136 (ledstatus != TPACPI_LED_OFF) << led);
5137 break;
5138 case TPACPI_LED_NEW:
5139 /* all others */
5140 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5141 return -EINVAL;
5142 if (unlikely(tpacpi_is_led_restricted(led)))
5143 return -EPERM;
5144 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5145 led, led_led_arg1[ledstatus]))
5146 rc = -EIO;
5147 break;
5148 default:
5149 rc = -ENXIO;
5152 if (!rc)
5153 tpacpi_led_state_cache[led] = ledstatus;
5155 return rc;
5158 static void led_set_status_worker(struct work_struct *work)
5160 struct tpacpi_led_classdev *data =
5161 container_of(work, struct tpacpi_led_classdev, work);
5163 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5164 led_set_status(data->led, data->new_state);
5167 static void led_sysfs_set(struct led_classdev *led_cdev,
5168 enum led_brightness brightness)
5170 struct tpacpi_led_classdev *data = container_of(led_cdev,
5171 struct tpacpi_led_classdev, led_classdev);
5173 if (brightness == LED_OFF)
5174 data->new_state = TPACPI_LED_OFF;
5175 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5176 data->new_state = TPACPI_LED_ON;
5177 else
5178 data->new_state = TPACPI_LED_BLINK;
5180 queue_work(tpacpi_wq, &data->work);
5183 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5184 unsigned long *delay_on, unsigned long *delay_off)
5186 struct tpacpi_led_classdev *data = container_of(led_cdev,
5187 struct tpacpi_led_classdev, led_classdev);
5189 /* Can we choose the flash rate? */
5190 if (*delay_on == 0 && *delay_off == 0) {
5191 /* yes. set them to the hardware blink rate (1 Hz) */
5192 *delay_on = 500; /* ms */
5193 *delay_off = 500; /* ms */
5194 } else if ((*delay_on != 500) || (*delay_off != 500))
5195 return -EINVAL;
5197 data->new_state = TPACPI_LED_BLINK;
5198 queue_work(tpacpi_wq, &data->work);
5200 return 0;
5203 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5205 int rc;
5207 struct tpacpi_led_classdev *data = container_of(led_cdev,
5208 struct tpacpi_led_classdev, led_classdev);
5210 rc = led_get_status(data->led);
5212 if (rc == TPACPI_LED_OFF || rc < 0)
5213 rc = LED_OFF; /* no error handling in led class :( */
5214 else
5215 rc = LED_FULL;
5217 return rc;
5220 static void led_exit(void)
5222 unsigned int i;
5224 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5225 if (tpacpi_leds[i].led_classdev.name)
5226 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5229 kfree(tpacpi_leds);
5232 static int __init tpacpi_init_led(unsigned int led)
5234 int rc;
5236 tpacpi_leds[led].led = led;
5238 /* LEDs with no name don't get registered */
5239 if (!tpacpi_led_names[led])
5240 return 0;
5242 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5243 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5244 if (led_supported == TPACPI_LED_570)
5245 tpacpi_leds[led].led_classdev.brightness_get =
5246 &led_sysfs_get;
5248 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5250 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5252 rc = led_classdev_register(&tpacpi_pdev->dev,
5253 &tpacpi_leds[led].led_classdev);
5254 if (rc < 0)
5255 tpacpi_leds[led].led_classdev.name = NULL;
5257 return rc;
5260 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5261 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5262 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5263 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5265 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5266 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5267 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5268 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5269 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5270 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5271 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5272 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5274 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5275 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5276 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5277 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5278 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5280 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5281 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5282 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5283 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5285 /* (1) - may have excess leds enabled on MSB */
5287 /* Defaults (order matters, keep last, don't reorder!) */
5288 { /* Lenovo */
5289 .vendor = PCI_VENDOR_ID_LENOVO,
5290 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5291 .quirks = 0x1fffU,
5293 { /* IBM ThinkPads with no EC version string */
5294 .vendor = PCI_VENDOR_ID_IBM,
5295 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5296 .quirks = 0x00ffU,
5298 { /* IBM ThinkPads with EC version string */
5299 .vendor = PCI_VENDOR_ID_IBM,
5300 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5301 .quirks = 0x00bfU,
5305 #undef TPACPI_LEDQ_IBM
5306 #undef TPACPI_LEDQ_LNV
5308 static enum led_access_mode __init led_init_detect_mode(void)
5310 acpi_status status;
5312 if (tpacpi_is_ibm()) {
5313 /* 570 */
5314 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5315 if (ACPI_SUCCESS(status))
5316 return TPACPI_LED_570;
5318 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5319 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5320 if (ACPI_SUCCESS(status))
5321 return TPACPI_LED_OLD;
5324 /* most others */
5325 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5326 if (ACPI_SUCCESS(status))
5327 return TPACPI_LED_NEW;
5329 /* R30, R31, and unknown firmwares */
5330 led_handle = NULL;
5331 return TPACPI_LED_NONE;
5334 static int __init led_init(struct ibm_init_struct *iibm)
5336 unsigned int i;
5337 int rc;
5338 unsigned long useful_leds;
5340 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5342 led_supported = led_init_detect_mode();
5344 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5345 str_supported(led_supported), led_supported);
5347 if (led_supported == TPACPI_LED_NONE)
5348 return 1;
5350 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5351 GFP_KERNEL);
5352 if (!tpacpi_leds) {
5353 printk(TPACPI_ERR "Out of memory for LED data\n");
5354 return -ENOMEM;
5357 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5358 ARRAY_SIZE(led_useful_qtable));
5360 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5361 if (!tpacpi_is_led_restricted(i) &&
5362 test_bit(i, &useful_leds)) {
5363 rc = tpacpi_init_led(i);
5364 if (rc < 0) {
5365 led_exit();
5366 return rc;
5371 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5372 printk(TPACPI_NOTICE
5373 "warning: userspace override of important "
5374 "firmware LEDs is enabled\n");
5375 #endif
5376 return 0;
5379 #define str_led_status(s) \
5380 ((s) == TPACPI_LED_OFF ? "off" : \
5381 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5383 static int led_read(struct seq_file *m)
5385 if (!led_supported) {
5386 seq_printf(m, "status:\t\tnot supported\n");
5387 return 0;
5389 seq_printf(m, "status:\t\tsupported\n");
5391 if (led_supported == TPACPI_LED_570) {
5392 /* 570 */
5393 int i, status;
5394 for (i = 0; i < 8; i++) {
5395 status = led_get_status(i);
5396 if (status < 0)
5397 return -EIO;
5398 seq_printf(m, "%d:\t\t%s\n",
5399 i, str_led_status(status));
5403 seq_printf(m, "commands:\t"
5404 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5406 return 0;
5409 static int led_write(char *buf)
5411 char *cmd;
5412 int led, rc;
5413 enum led_status_t s;
5415 if (!led_supported)
5416 return -ENODEV;
5418 while ((cmd = next_cmd(&buf))) {
5419 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5420 return -EINVAL;
5422 if (strstr(cmd, "off")) {
5423 s = TPACPI_LED_OFF;
5424 } else if (strstr(cmd, "on")) {
5425 s = TPACPI_LED_ON;
5426 } else if (strstr(cmd, "blink")) {
5427 s = TPACPI_LED_BLINK;
5428 } else {
5429 return -EINVAL;
5432 rc = led_set_status(led, s);
5433 if (rc < 0)
5434 return rc;
5437 return 0;
5440 static struct ibm_struct led_driver_data = {
5441 .name = "led",
5442 .read = led_read,
5443 .write = led_write,
5444 .exit = led_exit,
5447 /*************************************************************************
5448 * Beep subdriver
5451 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5453 #define TPACPI_BEEP_Q1 0x0001
5455 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5456 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5457 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5460 static int __init beep_init(struct ibm_init_struct *iibm)
5462 unsigned long quirks;
5464 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5466 TPACPI_ACPIHANDLE_INIT(beep);
5468 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5469 str_supported(beep_handle != NULL));
5471 quirks = tpacpi_check_quirks(beep_quirk_table,
5472 ARRAY_SIZE(beep_quirk_table));
5474 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5476 return (beep_handle)? 0 : 1;
5479 static int beep_read(struct seq_file *m)
5481 if (!beep_handle)
5482 seq_printf(m, "status:\t\tnot supported\n");
5483 else {
5484 seq_printf(m, "status:\t\tsupported\n");
5485 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5488 return 0;
5491 static int beep_write(char *buf)
5493 char *cmd;
5494 int beep_cmd;
5496 if (!beep_handle)
5497 return -ENODEV;
5499 while ((cmd = next_cmd(&buf))) {
5500 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5501 beep_cmd >= 0 && beep_cmd <= 17) {
5502 /* beep_cmd set */
5503 } else
5504 return -EINVAL;
5505 if (tp_features.beep_needs_two_args) {
5506 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5507 beep_cmd, 0))
5508 return -EIO;
5509 } else {
5510 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5511 beep_cmd))
5512 return -EIO;
5516 return 0;
5519 static struct ibm_struct beep_driver_data = {
5520 .name = "beep",
5521 .read = beep_read,
5522 .write = beep_write,
5525 /*************************************************************************
5526 * Thermal subdriver
5529 enum thermal_access_mode {
5530 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5531 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5532 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5533 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5534 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5537 enum { /* TPACPI_THERMAL_TPEC_* */
5538 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5539 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5540 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5542 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5546 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5547 struct ibm_thermal_sensors_struct {
5548 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5551 static enum thermal_access_mode thermal_read_mode;
5553 /* idx is zero-based */
5554 static int thermal_get_sensor(int idx, s32 *value)
5556 int t;
5557 s8 tmp;
5558 char tmpi[5];
5560 t = TP_EC_THERMAL_TMP0;
5562 switch (thermal_read_mode) {
5563 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5564 case TPACPI_THERMAL_TPEC_16:
5565 if (idx >= 8 && idx <= 15) {
5566 t = TP_EC_THERMAL_TMP8;
5567 idx -= 8;
5569 /* fallthrough */
5570 #endif
5571 case TPACPI_THERMAL_TPEC_8:
5572 if (idx <= 7) {
5573 if (!acpi_ec_read(t + idx, &tmp))
5574 return -EIO;
5575 *value = tmp * 1000;
5576 return 0;
5578 break;
5580 case TPACPI_THERMAL_ACPI_UPDT:
5581 if (idx <= 7) {
5582 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5583 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5584 return -EIO;
5585 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5586 return -EIO;
5587 *value = (t - 2732) * 100;
5588 return 0;
5590 break;
5592 case TPACPI_THERMAL_ACPI_TMP07:
5593 if (idx <= 7) {
5594 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5595 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5596 return -EIO;
5597 if (t > 127 || t < -127)
5598 t = TP_EC_THERMAL_TMP_NA;
5599 *value = t * 1000;
5600 return 0;
5602 break;
5604 case TPACPI_THERMAL_NONE:
5605 default:
5606 return -ENOSYS;
5609 return -EINVAL;
5612 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5614 int res, i;
5615 int n;
5617 n = 8;
5618 i = 0;
5620 if (!s)
5621 return -EINVAL;
5623 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5624 n = 16;
5626 for (i = 0 ; i < n; i++) {
5627 res = thermal_get_sensor(i, &s->temp[i]);
5628 if (res)
5629 return res;
5632 return n;
5635 static void thermal_dump_all_sensors(void)
5637 int n, i;
5638 struct ibm_thermal_sensors_struct t;
5640 n = thermal_get_sensors(&t);
5641 if (n <= 0)
5642 return;
5644 printk(TPACPI_NOTICE
5645 "temperatures (Celsius):");
5647 for (i = 0; i < n; i++) {
5648 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5649 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5650 else
5651 printk(KERN_CONT " N/A");
5654 printk(KERN_CONT "\n");
5657 /* sysfs temp##_input -------------------------------------------------- */
5659 static ssize_t thermal_temp_input_show(struct device *dev,
5660 struct device_attribute *attr,
5661 char *buf)
5663 struct sensor_device_attribute *sensor_attr =
5664 to_sensor_dev_attr(attr);
5665 int idx = sensor_attr->index;
5666 s32 value;
5667 int res;
5669 res = thermal_get_sensor(idx, &value);
5670 if (res)
5671 return res;
5672 if (value == TPACPI_THERMAL_SENSOR_NA)
5673 return -ENXIO;
5675 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5678 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5679 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5680 thermal_temp_input_show, NULL, _idxB)
5682 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5683 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5684 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5685 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5686 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5687 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5688 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5689 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5690 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5691 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5692 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5693 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5694 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5695 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5696 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5697 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5698 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5701 #define THERMAL_ATTRS(X) \
5702 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5704 static struct attribute *thermal_temp_input_attr[] = {
5705 THERMAL_ATTRS(8),
5706 THERMAL_ATTRS(9),
5707 THERMAL_ATTRS(10),
5708 THERMAL_ATTRS(11),
5709 THERMAL_ATTRS(12),
5710 THERMAL_ATTRS(13),
5711 THERMAL_ATTRS(14),
5712 THERMAL_ATTRS(15),
5713 THERMAL_ATTRS(0),
5714 THERMAL_ATTRS(1),
5715 THERMAL_ATTRS(2),
5716 THERMAL_ATTRS(3),
5717 THERMAL_ATTRS(4),
5718 THERMAL_ATTRS(5),
5719 THERMAL_ATTRS(6),
5720 THERMAL_ATTRS(7),
5721 NULL
5724 static const struct attribute_group thermal_temp_input16_group = {
5725 .attrs = thermal_temp_input_attr
5728 static const struct attribute_group thermal_temp_input8_group = {
5729 .attrs = &thermal_temp_input_attr[8]
5732 #undef THERMAL_SENSOR_ATTR_TEMP
5733 #undef THERMAL_ATTRS
5735 /* --------------------------------------------------------------------- */
5737 static int __init thermal_init(struct ibm_init_struct *iibm)
5739 u8 t, ta1, ta2;
5740 int i;
5741 int acpi_tmp7;
5742 int res;
5744 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5746 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5748 if (thinkpad_id.ec_model) {
5750 * Direct EC access mode: sensors at registers
5751 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5752 * non-implemented, thermal sensors return 0x80 when
5753 * not available
5756 ta1 = ta2 = 0;
5757 for (i = 0; i < 8; i++) {
5758 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5759 ta1 |= t;
5760 } else {
5761 ta1 = 0;
5762 break;
5764 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5765 ta2 |= t;
5766 } else {
5767 ta1 = 0;
5768 break;
5771 if (ta1 == 0) {
5772 /* This is sheer paranoia, but we handle it anyway */
5773 if (acpi_tmp7) {
5774 printk(TPACPI_ERR
5775 "ThinkPad ACPI EC access misbehaving, "
5776 "falling back to ACPI TMPx access "
5777 "mode\n");
5778 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5779 } else {
5780 printk(TPACPI_ERR
5781 "ThinkPad ACPI EC access misbehaving, "
5782 "disabling thermal sensors access\n");
5783 thermal_read_mode = TPACPI_THERMAL_NONE;
5785 } else {
5786 thermal_read_mode =
5787 (ta2 != 0) ?
5788 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5790 } else if (acpi_tmp7) {
5791 if (tpacpi_is_ibm() &&
5792 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5793 /* 600e/x, 770e, 770x */
5794 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5795 } else {
5796 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5797 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5799 } else {
5800 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5801 thermal_read_mode = TPACPI_THERMAL_NONE;
5804 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5805 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5806 thermal_read_mode);
5808 switch (thermal_read_mode) {
5809 case TPACPI_THERMAL_TPEC_16:
5810 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5811 &thermal_temp_input16_group);
5812 if (res)
5813 return res;
5814 break;
5815 case TPACPI_THERMAL_TPEC_8:
5816 case TPACPI_THERMAL_ACPI_TMP07:
5817 case TPACPI_THERMAL_ACPI_UPDT:
5818 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5819 &thermal_temp_input8_group);
5820 if (res)
5821 return res;
5822 break;
5823 case TPACPI_THERMAL_NONE:
5824 default:
5825 return 1;
5828 return 0;
5831 static void thermal_exit(void)
5833 switch (thermal_read_mode) {
5834 case TPACPI_THERMAL_TPEC_16:
5835 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5836 &thermal_temp_input16_group);
5837 break;
5838 case TPACPI_THERMAL_TPEC_8:
5839 case TPACPI_THERMAL_ACPI_TMP07:
5840 case TPACPI_THERMAL_ACPI_UPDT:
5841 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5842 &thermal_temp_input8_group);
5843 break;
5844 case TPACPI_THERMAL_NONE:
5845 default:
5846 break;
5850 static int thermal_read(struct seq_file *m)
5852 int n, i;
5853 struct ibm_thermal_sensors_struct t;
5855 n = thermal_get_sensors(&t);
5856 if (unlikely(n < 0))
5857 return n;
5859 seq_printf(m, "temperatures:\t");
5861 if (n > 0) {
5862 for (i = 0; i < (n - 1); i++)
5863 seq_printf(m, "%d ", t.temp[i] / 1000);
5864 seq_printf(m, "%d\n", t.temp[i] / 1000);
5865 } else
5866 seq_printf(m, "not supported\n");
5868 return 0;
5871 static struct ibm_struct thermal_driver_data = {
5872 .name = "thermal",
5873 .read = thermal_read,
5874 .exit = thermal_exit,
5877 /*************************************************************************
5878 * EC Dump subdriver
5881 static u8 ecdump_regs[256];
5883 static int ecdump_read(struct seq_file *m)
5885 int i, j;
5886 u8 v;
5888 seq_printf(m, "EC "
5889 " +00 +01 +02 +03 +04 +05 +06 +07"
5890 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5891 for (i = 0; i < 256; i += 16) {
5892 seq_printf(m, "EC 0x%02x:", i);
5893 for (j = 0; j < 16; j++) {
5894 if (!acpi_ec_read(i + j, &v))
5895 break;
5896 if (v != ecdump_regs[i + j])
5897 seq_printf(m, " *%02x", v);
5898 else
5899 seq_printf(m, " %02x", v);
5900 ecdump_regs[i + j] = v;
5902 seq_putc(m, '\n');
5903 if (j != 16)
5904 break;
5907 /* These are way too dangerous to advertise openly... */
5908 #if 0
5909 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5910 " (<offset> is 00-ff, <value> is 00-ff)\n");
5911 seq_printf(m, "commands:\t0x<offset> <value> "
5912 " (<offset> is 00-ff, <value> is 0-255)\n");
5913 #endif
5914 return 0;
5917 static int ecdump_write(char *buf)
5919 char *cmd;
5920 int i, v;
5922 while ((cmd = next_cmd(&buf))) {
5923 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5924 /* i and v set */
5925 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5926 /* i and v set */
5927 } else
5928 return -EINVAL;
5929 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5930 if (!acpi_ec_write(i, v))
5931 return -EIO;
5932 } else
5933 return -EINVAL;
5936 return 0;
5939 static struct ibm_struct ecdump_driver_data = {
5940 .name = "ecdump",
5941 .read = ecdump_read,
5942 .write = ecdump_write,
5943 .flags.experimental = 1,
5946 /*************************************************************************
5947 * Backlight/brightness subdriver
5950 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5953 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5954 * CMOS NVRAM byte 0x5E, bits 0-3.
5956 * EC HBRV (0x31) has the following layout
5957 * Bit 7: unknown function
5958 * Bit 6: unknown function
5959 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5960 * Bit 4: must be set to zero to avoid problems
5961 * Bit 3-0: backlight brightness level
5963 * brightness_get_raw returns status data in the HBRV layout
5965 * WARNING: The X61 has been verified to use HBRV for something else, so
5966 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5967 * testing on the very early *60 Lenovo models...
5970 enum {
5971 TP_EC_BACKLIGHT = 0x31,
5973 /* TP_EC_BACKLIGHT bitmasks */
5974 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5975 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5976 TP_EC_BACKLIGHT_MAPSW = 0x20,
5979 enum tpacpi_brightness_access_mode {
5980 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5981 TPACPI_BRGHT_MODE_EC, /* EC control */
5982 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5983 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5984 TPACPI_BRGHT_MODE_MAX
5987 static struct backlight_device *ibm_backlight_device;
5989 static enum tpacpi_brightness_access_mode brightness_mode =
5990 TPACPI_BRGHT_MODE_MAX;
5992 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5994 static struct mutex brightness_mutex;
5996 /* NVRAM brightness access,
5997 * call with brightness_mutex held! */
5998 static unsigned int tpacpi_brightness_nvram_get(void)
6000 u8 lnvram;
6002 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6003 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6004 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6005 lnvram &= bright_maxlvl;
6007 return lnvram;
6010 static void tpacpi_brightness_checkpoint_nvram(void)
6012 u8 lec = 0;
6013 u8 b_nvram;
6015 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6016 return;
6018 vdbg_printk(TPACPI_DBG_BRGHT,
6019 "trying to checkpoint backlight level to NVRAM...\n");
6021 if (mutex_lock_killable(&brightness_mutex) < 0)
6022 return;
6024 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6025 goto unlock;
6026 lec &= TP_EC_BACKLIGHT_LVLMSK;
6027 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6029 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6030 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6031 /* NVRAM needs update */
6032 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6033 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6034 b_nvram |= lec;
6035 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6036 dbg_printk(TPACPI_DBG_BRGHT,
6037 "updated NVRAM backlight level to %u (0x%02x)\n",
6038 (unsigned int) lec, (unsigned int) b_nvram);
6039 } else
6040 vdbg_printk(TPACPI_DBG_BRGHT,
6041 "NVRAM backlight level already is %u (0x%02x)\n",
6042 (unsigned int) lec, (unsigned int) b_nvram);
6044 unlock:
6045 mutex_unlock(&brightness_mutex);
6049 /* call with brightness_mutex held! */
6050 static int tpacpi_brightness_get_raw(int *status)
6052 u8 lec = 0;
6054 switch (brightness_mode) {
6055 case TPACPI_BRGHT_MODE_UCMS_STEP:
6056 *status = tpacpi_brightness_nvram_get();
6057 return 0;
6058 case TPACPI_BRGHT_MODE_EC:
6059 case TPACPI_BRGHT_MODE_ECNVRAM:
6060 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6061 return -EIO;
6062 *status = lec;
6063 return 0;
6064 default:
6065 return -ENXIO;
6069 /* call with brightness_mutex held! */
6070 /* do NOT call with illegal backlight level value */
6071 static int tpacpi_brightness_set_ec(unsigned int value)
6073 u8 lec = 0;
6075 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6076 return -EIO;
6078 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6079 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6080 (value & TP_EC_BACKLIGHT_LVLMSK))))
6081 return -EIO;
6083 return 0;
6086 /* call with brightness_mutex held! */
6087 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6089 int cmos_cmd, inc;
6090 unsigned int current_value, i;
6092 current_value = tpacpi_brightness_nvram_get();
6094 if (value == current_value)
6095 return 0;
6097 cmos_cmd = (value > current_value) ?
6098 TP_CMOS_BRIGHTNESS_UP :
6099 TP_CMOS_BRIGHTNESS_DOWN;
6100 inc = (value > current_value) ? 1 : -1;
6102 for (i = current_value; i != value; i += inc)
6103 if (issue_thinkpad_cmos_command(cmos_cmd))
6104 return -EIO;
6106 return 0;
6109 /* May return EINTR which can always be mapped to ERESTARTSYS */
6110 static int brightness_set(unsigned int value)
6112 int res;
6114 if (value > bright_maxlvl || value < 0)
6115 return -EINVAL;
6117 vdbg_printk(TPACPI_DBG_BRGHT,
6118 "set backlight level to %d\n", value);
6120 res = mutex_lock_killable(&brightness_mutex);
6121 if (res < 0)
6122 return res;
6124 switch (brightness_mode) {
6125 case TPACPI_BRGHT_MODE_EC:
6126 case TPACPI_BRGHT_MODE_ECNVRAM:
6127 res = tpacpi_brightness_set_ec(value);
6128 break;
6129 case TPACPI_BRGHT_MODE_UCMS_STEP:
6130 res = tpacpi_brightness_set_ucmsstep(value);
6131 break;
6132 default:
6133 res = -ENXIO;
6136 mutex_unlock(&brightness_mutex);
6137 return res;
6140 /* sysfs backlight class ----------------------------------------------- */
6142 static int brightness_update_status(struct backlight_device *bd)
6144 unsigned int level =
6145 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6146 bd->props.power == FB_BLANK_UNBLANK) ?
6147 bd->props.brightness : 0;
6149 dbg_printk(TPACPI_DBG_BRGHT,
6150 "backlight: attempt to set level to %d\n",
6151 level);
6153 /* it is the backlight class's job (caller) to handle
6154 * EINTR and other errors properly */
6155 return brightness_set(level);
6158 static int brightness_get(struct backlight_device *bd)
6160 int status, res;
6162 res = mutex_lock_killable(&brightness_mutex);
6163 if (res < 0)
6164 return 0;
6166 res = tpacpi_brightness_get_raw(&status);
6168 mutex_unlock(&brightness_mutex);
6170 if (res < 0)
6171 return 0;
6173 return status & TP_EC_BACKLIGHT_LVLMSK;
6176 static void tpacpi_brightness_notify_change(void)
6178 backlight_force_update(ibm_backlight_device,
6179 BACKLIGHT_UPDATE_HOTKEY);
6182 static const struct backlight_ops ibm_backlight_data = {
6183 .get_brightness = brightness_get,
6184 .update_status = brightness_update_status,
6187 /* --------------------------------------------------------------------- */
6190 * Call _BCL method of video device. On some ThinkPads this will
6191 * switch the firmware to the ACPI brightness control mode.
6194 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6196 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6197 union acpi_object *obj;
6198 int rc;
6200 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6201 obj = (union acpi_object *)buffer.pointer;
6202 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6203 printk(TPACPI_ERR "Unknown _BCL data, "
6204 "please report this to %s\n", TPACPI_MAIL);
6205 rc = 0;
6206 } else {
6207 rc = obj->package.count;
6209 } else {
6210 return 0;
6213 kfree(buffer.pointer);
6214 return rc;
6219 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6221 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6223 acpi_handle video_device;
6224 int bcl_levels = 0;
6226 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6227 if (video_device)
6228 bcl_levels = tpacpi_query_bcl_levels(video_device);
6230 tp_features.bright_acpimode = (bcl_levels > 0);
6232 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6236 * These are only useful for models that have only one possibility
6237 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6238 * these quirks.
6240 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6241 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6242 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6244 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6245 /* Models with ATI GPUs known to require ECNVRAM mode */
6246 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6248 /* Models with ATI GPUs that can use ECNVRAM */
6249 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6250 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6251 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6252 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6254 /* Models with Intel Extreme Graphics 2 */
6255 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6256 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6257 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6259 /* Models with Intel GMA900 */
6260 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6261 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6262 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6266 * Returns < 0 for error, otherwise sets tp_features.bright_*
6267 * and bright_maxlvl.
6269 static void __init tpacpi_detect_brightness_capabilities(void)
6271 unsigned int b;
6273 vdbg_printk(TPACPI_DBG_INIT,
6274 "detecting firmware brightness interface capabilities\n");
6276 /* we could run a quirks check here (same table used by
6277 * brightness_init) if needed */
6280 * We always attempt to detect acpi support, so as to switch
6281 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6282 * going to publish a backlight interface
6284 b = tpacpi_check_std_acpi_brightness_support();
6285 switch (b) {
6286 case 16:
6287 bright_maxlvl = 15;
6288 printk(TPACPI_INFO
6289 "detected a 16-level brightness capable ThinkPad\n");
6290 break;
6291 case 8:
6292 case 0:
6293 bright_maxlvl = 7;
6294 printk(TPACPI_INFO
6295 "detected a 8-level brightness capable ThinkPad\n");
6296 break;
6297 default:
6298 printk(TPACPI_ERR
6299 "Unsupported brightness interface, "
6300 "please contact %s\n", TPACPI_MAIL);
6301 tp_features.bright_unkfw = 1;
6302 bright_maxlvl = b - 1;
6306 static int __init brightness_init(struct ibm_init_struct *iibm)
6308 struct backlight_properties props;
6309 int b;
6310 unsigned long quirks;
6312 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6314 mutex_init(&brightness_mutex);
6316 quirks = tpacpi_check_quirks(brightness_quirk_table,
6317 ARRAY_SIZE(brightness_quirk_table));
6319 /* tpacpi_detect_brightness_capabilities() must have run already */
6321 /* if it is unknown, we don't handle it: it wouldn't be safe */
6322 if (tp_features.bright_unkfw)
6323 return 1;
6325 if (!brightness_enable) {
6326 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6327 "brightness support disabled by "
6328 "module parameter\n");
6329 return 1;
6332 if (acpi_video_backlight_support()) {
6333 if (brightness_enable > 1) {
6334 printk(TPACPI_INFO
6335 "Standard ACPI backlight interface "
6336 "available, not loading native one.\n");
6337 return 1;
6338 } else if (brightness_enable == 1) {
6339 printk(TPACPI_WARN
6340 "Cannot enable backlight brightness support, "
6341 "ACPI is already handling it. Refer to the "
6342 "acpi_backlight kernel parameter\n");
6343 return 1;
6345 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6346 printk(TPACPI_NOTICE
6347 "Standard ACPI backlight interface not "
6348 "available, thinkpad_acpi native "
6349 "brightness control enabled\n");
6353 * Check for module parameter bogosity, note that we
6354 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6355 * able to detect "unspecified"
6357 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6358 return -EINVAL;
6360 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6361 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6362 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6363 if (quirks & TPACPI_BRGHT_Q_EC)
6364 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6365 else
6366 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6368 dbg_printk(TPACPI_DBG_BRGHT,
6369 "driver auto-selected brightness_mode=%d\n",
6370 brightness_mode);
6373 /* Safety */
6374 if (!tpacpi_is_ibm() &&
6375 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6376 brightness_mode == TPACPI_BRGHT_MODE_EC))
6377 return -EINVAL;
6379 if (tpacpi_brightness_get_raw(&b) < 0)
6380 return 1;
6382 memset(&props, 0, sizeof(struct backlight_properties));
6383 props.max_brightness = bright_maxlvl;
6384 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6385 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6386 NULL, NULL,
6387 &ibm_backlight_data,
6388 &props);
6389 if (IS_ERR(ibm_backlight_device)) {
6390 int rc = PTR_ERR(ibm_backlight_device);
6391 ibm_backlight_device = NULL;
6392 printk(TPACPI_ERR "Could not register backlight device\n");
6393 return rc;
6395 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6396 "brightness is supported\n");
6398 if (quirks & TPACPI_BRGHT_Q_ASK) {
6399 printk(TPACPI_NOTICE
6400 "brightness: will use unverified default: "
6401 "brightness_mode=%d\n", brightness_mode);
6402 printk(TPACPI_NOTICE
6403 "brightness: please report to %s whether it works well "
6404 "or not on your ThinkPad\n", TPACPI_MAIL);
6407 /* Added by mistake in early 2007. Probably useless, but it could
6408 * be working around some unknown firmware problem where the value
6409 * read at startup doesn't match the real hardware state... so leave
6410 * it in place just in case */
6411 backlight_update_status(ibm_backlight_device);
6413 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6414 "brightness: registering brightness hotkeys "
6415 "as change notification\n");
6416 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6417 | TP_ACPI_HKEY_BRGHTUP_MASK
6418 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6419 return 0;
6422 static void brightness_suspend(pm_message_t state)
6424 tpacpi_brightness_checkpoint_nvram();
6427 static void brightness_shutdown(void)
6429 tpacpi_brightness_checkpoint_nvram();
6432 static void brightness_exit(void)
6434 if (ibm_backlight_device) {
6435 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6436 "calling backlight_device_unregister()\n");
6437 backlight_device_unregister(ibm_backlight_device);
6440 tpacpi_brightness_checkpoint_nvram();
6443 static int brightness_read(struct seq_file *m)
6445 int level;
6447 level = brightness_get(NULL);
6448 if (level < 0) {
6449 seq_printf(m, "level:\t\tunreadable\n");
6450 } else {
6451 seq_printf(m, "level:\t\t%d\n", level);
6452 seq_printf(m, "commands:\tup, down\n");
6453 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6454 bright_maxlvl);
6457 return 0;
6460 static int brightness_write(char *buf)
6462 int level;
6463 int rc;
6464 char *cmd;
6466 level = brightness_get(NULL);
6467 if (level < 0)
6468 return level;
6470 while ((cmd = next_cmd(&buf))) {
6471 if (strlencmp(cmd, "up") == 0) {
6472 if (level < bright_maxlvl)
6473 level++;
6474 } else if (strlencmp(cmd, "down") == 0) {
6475 if (level > 0)
6476 level--;
6477 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6478 level >= 0 && level <= bright_maxlvl) {
6479 /* new level set */
6480 } else
6481 return -EINVAL;
6484 tpacpi_disclose_usertask("procfs brightness",
6485 "set level to %d\n", level);
6488 * Now we know what the final level should be, so we try to set it.
6489 * Doing it this way makes the syscall restartable in case of EINTR
6491 rc = brightness_set(level);
6492 if (!rc && ibm_backlight_device)
6493 backlight_force_update(ibm_backlight_device,
6494 BACKLIGHT_UPDATE_SYSFS);
6495 return (rc == -EINTR)? -ERESTARTSYS : rc;
6498 static struct ibm_struct brightness_driver_data = {
6499 .name = "brightness",
6500 .read = brightness_read,
6501 .write = brightness_write,
6502 .exit = brightness_exit,
6503 .suspend = brightness_suspend,
6504 .shutdown = brightness_shutdown,
6507 /*************************************************************************
6508 * Volume subdriver
6512 * IBM ThinkPads have a simple volume controller with MUTE gating.
6513 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6515 * Since the *61 series (and probably also the later *60 series), Lenovo
6516 * ThinkPads only implement the MUTE gate.
6518 * EC register 0x30
6519 * Bit 6: MUTE (1 mutes sound)
6520 * Bit 3-0: Volume
6521 * Other bits should be zero as far as we know.
6523 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6524 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6525 * such as bit 7 which is used to detect repeated presses of MUTE,
6526 * and we leave them unchanged.
6529 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6531 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6532 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6533 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6535 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6536 static char *alsa_id = "ThinkPadEC";
6537 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6539 struct tpacpi_alsa_data {
6540 struct snd_card *card;
6541 struct snd_ctl_elem_id *ctl_mute_id;
6542 struct snd_ctl_elem_id *ctl_vol_id;
6545 static struct snd_card *alsa_card;
6547 enum {
6548 TP_EC_AUDIO = 0x30,
6550 /* TP_EC_AUDIO bits */
6551 TP_EC_AUDIO_MUTESW = 6,
6553 /* TP_EC_AUDIO bitmasks */
6554 TP_EC_AUDIO_LVL_MSK = 0x0F,
6555 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6557 /* Maximum volume */
6558 TP_EC_VOLUME_MAX = 14,
6561 enum tpacpi_volume_access_mode {
6562 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6563 TPACPI_VOL_MODE_EC, /* Pure EC control */
6564 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6565 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6566 TPACPI_VOL_MODE_MAX
6569 enum tpacpi_volume_capabilities {
6570 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6571 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6572 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6573 TPACPI_VOL_CAP_MAX
6576 static enum tpacpi_volume_access_mode volume_mode =
6577 TPACPI_VOL_MODE_MAX;
6579 static enum tpacpi_volume_capabilities volume_capabilities;
6580 static int volume_control_allowed;
6583 * Used to syncronize writers to TP_EC_AUDIO and
6584 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6586 static struct mutex volume_mutex;
6588 static void tpacpi_volume_checkpoint_nvram(void)
6590 u8 lec = 0;
6591 u8 b_nvram;
6592 u8 ec_mask;
6594 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6595 return;
6596 if (!volume_control_allowed)
6597 return;
6599 vdbg_printk(TPACPI_DBG_MIXER,
6600 "trying to checkpoint mixer state to NVRAM...\n");
6602 if (tp_features.mixer_no_level_control)
6603 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6604 else
6605 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6607 if (mutex_lock_killable(&volume_mutex) < 0)
6608 return;
6610 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6611 goto unlock;
6612 lec &= ec_mask;
6613 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6615 if (lec != (b_nvram & ec_mask)) {
6616 /* NVRAM needs update */
6617 b_nvram &= ~ec_mask;
6618 b_nvram |= lec;
6619 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6620 dbg_printk(TPACPI_DBG_MIXER,
6621 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6622 (unsigned int) lec, (unsigned int) b_nvram);
6623 } else {
6624 vdbg_printk(TPACPI_DBG_MIXER,
6625 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6626 (unsigned int) lec, (unsigned int) b_nvram);
6629 unlock:
6630 mutex_unlock(&volume_mutex);
6633 static int volume_get_status_ec(u8 *status)
6635 u8 s;
6637 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6638 return -EIO;
6640 *status = s;
6642 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6644 return 0;
6647 static int volume_get_status(u8 *status)
6649 return volume_get_status_ec(status);
6652 static int volume_set_status_ec(const u8 status)
6654 if (!acpi_ec_write(TP_EC_AUDIO, status))
6655 return -EIO;
6657 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6659 return 0;
6662 static int volume_set_status(const u8 status)
6664 return volume_set_status_ec(status);
6667 /* returns < 0 on error, 0 on no change, 1 on change */
6668 static int __volume_set_mute_ec(const bool mute)
6670 int rc;
6671 u8 s, n;
6673 if (mutex_lock_killable(&volume_mutex) < 0)
6674 return -EINTR;
6676 rc = volume_get_status_ec(&s);
6677 if (rc)
6678 goto unlock;
6680 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6681 s & ~TP_EC_AUDIO_MUTESW_MSK;
6683 if (n != s) {
6684 rc = volume_set_status_ec(n);
6685 if (!rc)
6686 rc = 1;
6689 unlock:
6690 mutex_unlock(&volume_mutex);
6691 return rc;
6694 static int volume_alsa_set_mute(const bool mute)
6696 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6697 (mute) ? "" : "un");
6698 return __volume_set_mute_ec(mute);
6701 static int volume_set_mute(const bool mute)
6703 int rc;
6705 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6706 (mute) ? "" : "un");
6708 rc = __volume_set_mute_ec(mute);
6709 return (rc < 0) ? rc : 0;
6712 /* returns < 0 on error, 0 on no change, 1 on change */
6713 static int __volume_set_volume_ec(const u8 vol)
6715 int rc;
6716 u8 s, n;
6718 if (vol > TP_EC_VOLUME_MAX)
6719 return -EINVAL;
6721 if (mutex_lock_killable(&volume_mutex) < 0)
6722 return -EINTR;
6724 rc = volume_get_status_ec(&s);
6725 if (rc)
6726 goto unlock;
6728 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6730 if (n != s) {
6731 rc = volume_set_status_ec(n);
6732 if (!rc)
6733 rc = 1;
6736 unlock:
6737 mutex_unlock(&volume_mutex);
6738 return rc;
6741 static int volume_alsa_set_volume(const u8 vol)
6743 dbg_printk(TPACPI_DBG_MIXER,
6744 "ALSA: trying to set volume level to %hu\n", vol);
6745 return __volume_set_volume_ec(vol);
6748 static void volume_alsa_notify_change(void)
6750 struct tpacpi_alsa_data *d;
6752 if (alsa_card && alsa_card->private_data) {
6753 d = alsa_card->private_data;
6754 if (d->ctl_mute_id)
6755 snd_ctl_notify(alsa_card,
6756 SNDRV_CTL_EVENT_MASK_VALUE,
6757 d->ctl_mute_id);
6758 if (d->ctl_vol_id)
6759 snd_ctl_notify(alsa_card,
6760 SNDRV_CTL_EVENT_MASK_VALUE,
6761 d->ctl_vol_id);
6765 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6766 struct snd_ctl_elem_info *uinfo)
6768 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6769 uinfo->count = 1;
6770 uinfo->value.integer.min = 0;
6771 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6772 return 0;
6775 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6776 struct snd_ctl_elem_value *ucontrol)
6778 u8 s;
6779 int rc;
6781 rc = volume_get_status(&s);
6782 if (rc < 0)
6783 return rc;
6785 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6786 return 0;
6789 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6790 struct snd_ctl_elem_value *ucontrol)
6792 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6793 ucontrol->value.integer.value[0]);
6794 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6797 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6799 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6800 struct snd_ctl_elem_value *ucontrol)
6802 u8 s;
6803 int rc;
6805 rc = volume_get_status(&s);
6806 if (rc < 0)
6807 return rc;
6809 ucontrol->value.integer.value[0] =
6810 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6811 return 0;
6814 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6815 struct snd_ctl_elem_value *ucontrol)
6817 tpacpi_disclose_usertask("ALSA", "%smute\n",
6818 ucontrol->value.integer.value[0] ?
6819 "un" : "");
6820 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6823 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6824 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6825 .name = "Console Playback Volume",
6826 .index = 0,
6827 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6828 .info = volume_alsa_vol_info,
6829 .get = volume_alsa_vol_get,
6832 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6833 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6834 .name = "Console Playback Switch",
6835 .index = 0,
6836 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6837 .info = volume_alsa_mute_info,
6838 .get = volume_alsa_mute_get,
6841 static void volume_suspend(pm_message_t state)
6843 tpacpi_volume_checkpoint_nvram();
6846 static void volume_resume(void)
6848 volume_alsa_notify_change();
6851 static void volume_shutdown(void)
6853 tpacpi_volume_checkpoint_nvram();
6856 static void volume_exit(void)
6858 if (alsa_card) {
6859 snd_card_free(alsa_card);
6860 alsa_card = NULL;
6863 tpacpi_volume_checkpoint_nvram();
6866 static int __init volume_create_alsa_mixer(void)
6868 struct snd_card *card;
6869 struct tpacpi_alsa_data *data;
6870 struct snd_kcontrol *ctl_vol;
6871 struct snd_kcontrol *ctl_mute;
6872 int rc;
6874 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6875 sizeof(struct tpacpi_alsa_data), &card);
6876 if (rc < 0 || !card) {
6877 printk(TPACPI_ERR
6878 "Failed to create ALSA card structures: %d\n", rc);
6879 return 1;
6882 BUG_ON(!card->private_data);
6883 data = card->private_data;
6884 data->card = card;
6886 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6887 sizeof(card->driver));
6888 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6889 sizeof(card->shortname));
6890 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6891 (thinkpad_id.ec_version_str) ?
6892 thinkpad_id.ec_version_str : "(unknown)");
6893 snprintf(card->longname, sizeof(card->longname),
6894 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6895 (thinkpad_id.ec_version_str) ?
6896 thinkpad_id.ec_version_str : "unknown");
6898 if (volume_control_allowed) {
6899 volume_alsa_control_vol.put = volume_alsa_vol_put;
6900 volume_alsa_control_vol.access =
6901 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6903 volume_alsa_control_mute.put = volume_alsa_mute_put;
6904 volume_alsa_control_mute.access =
6905 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6908 if (!tp_features.mixer_no_level_control) {
6909 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6910 rc = snd_ctl_add(card, ctl_vol);
6911 if (rc < 0) {
6912 printk(TPACPI_ERR
6913 "Failed to create ALSA volume control: %d\n",
6914 rc);
6915 goto err_exit;
6917 data->ctl_vol_id = &ctl_vol->id;
6920 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6921 rc = snd_ctl_add(card, ctl_mute);
6922 if (rc < 0) {
6923 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6924 rc);
6925 goto err_exit;
6927 data->ctl_mute_id = &ctl_mute->id;
6929 snd_card_set_dev(card, &tpacpi_pdev->dev);
6930 rc = snd_card_register(card);
6931 if (rc < 0) {
6932 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6933 goto err_exit;
6936 alsa_card = card;
6937 return 0;
6939 err_exit:
6940 snd_card_free(card);
6941 return 1;
6944 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6945 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6947 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6948 /* Whitelist volume level on all IBM by default */
6949 { .vendor = PCI_VENDOR_ID_IBM,
6950 .bios = TPACPI_MATCH_ANY,
6951 .ec = TPACPI_MATCH_ANY,
6952 .quirks = TPACPI_VOL_Q_LEVEL },
6954 /* Lenovo models with volume control (needs confirmation) */
6955 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6956 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6957 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6958 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6959 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6960 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6961 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6963 /* Whitelist mute-only on all Lenovo by default */
6964 { .vendor = PCI_VENDOR_ID_LENOVO,
6965 .bios = TPACPI_MATCH_ANY,
6966 .ec = TPACPI_MATCH_ANY,
6967 .quirks = TPACPI_VOL_Q_MUTEONLY }
6970 static int __init volume_init(struct ibm_init_struct *iibm)
6972 unsigned long quirks;
6973 int rc;
6975 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6977 mutex_init(&volume_mutex);
6980 * Check for module parameter bogosity, note that we
6981 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6982 * able to detect "unspecified"
6984 if (volume_mode > TPACPI_VOL_MODE_MAX)
6985 return -EINVAL;
6987 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6988 printk(TPACPI_ERR
6989 "UCMS step volume mode not implemented, "
6990 "please contact %s\n", TPACPI_MAIL);
6991 return 1;
6994 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6995 return -EINVAL;
6998 * The ALSA mixer is our primary interface.
6999 * When disabled, don't install the subdriver at all
7001 if (!alsa_enable) {
7002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7003 "ALSA mixer disabled by parameter, "
7004 "not loading volume subdriver...\n");
7005 return 1;
7008 quirks = tpacpi_check_quirks(volume_quirk_table,
7009 ARRAY_SIZE(volume_quirk_table));
7011 switch (volume_capabilities) {
7012 case TPACPI_VOL_CAP_AUTO:
7013 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7014 tp_features.mixer_no_level_control = 1;
7015 else if (quirks & TPACPI_VOL_Q_LEVEL)
7016 tp_features.mixer_no_level_control = 0;
7017 else
7018 return 1; /* no mixer */
7019 break;
7020 case TPACPI_VOL_CAP_VOLMUTE:
7021 tp_features.mixer_no_level_control = 0;
7022 break;
7023 case TPACPI_VOL_CAP_MUTEONLY:
7024 tp_features.mixer_no_level_control = 1;
7025 break;
7026 default:
7027 return 1;
7030 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7031 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7032 "using user-supplied volume_capabilities=%d\n",
7033 volume_capabilities);
7035 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7036 volume_mode == TPACPI_VOL_MODE_MAX) {
7037 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7039 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7040 "driver auto-selected volume_mode=%d\n",
7041 volume_mode);
7042 } else {
7043 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7044 "using user-supplied volume_mode=%d\n",
7045 volume_mode);
7048 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7049 "mute is supported, volume control is %s\n",
7050 str_supported(!tp_features.mixer_no_level_control));
7052 rc = volume_create_alsa_mixer();
7053 if (rc) {
7054 printk(TPACPI_ERR
7055 "Could not create the ALSA mixer interface\n");
7056 return rc;
7059 printk(TPACPI_INFO
7060 "Console audio control enabled, mode: %s\n",
7061 (volume_control_allowed) ?
7062 "override (read/write)" :
7063 "monitor (read only)");
7065 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7066 "registering volume hotkeys as change notification\n");
7067 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7068 | TP_ACPI_HKEY_VOLUP_MASK
7069 | TP_ACPI_HKEY_VOLDWN_MASK
7070 | TP_ACPI_HKEY_MUTE_MASK);
7072 return 0;
7075 static int volume_read(struct seq_file *m)
7077 u8 status;
7079 if (volume_get_status(&status) < 0) {
7080 seq_printf(m, "level:\t\tunreadable\n");
7081 } else {
7082 if (tp_features.mixer_no_level_control)
7083 seq_printf(m, "level:\t\tunsupported\n");
7084 else
7085 seq_printf(m, "level:\t\t%d\n",
7086 status & TP_EC_AUDIO_LVL_MSK);
7088 seq_printf(m, "mute:\t\t%s\n",
7089 onoff(status, TP_EC_AUDIO_MUTESW));
7091 if (volume_control_allowed) {
7092 seq_printf(m, "commands:\tunmute, mute\n");
7093 if (!tp_features.mixer_no_level_control) {
7094 seq_printf(m,
7095 "commands:\tup, down\n");
7096 seq_printf(m,
7097 "commands:\tlevel <level>"
7098 " (<level> is 0-%d)\n",
7099 TP_EC_VOLUME_MAX);
7104 return 0;
7107 static int volume_write(char *buf)
7109 u8 s;
7110 u8 new_level, new_mute;
7111 int l;
7112 char *cmd;
7113 int rc;
7116 * We do allow volume control at driver startup, so that the
7117 * user can set initial state through the volume=... parameter hack.
7119 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7120 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7121 tp_warned.volume_ctrl_forbidden = 1;
7122 printk(TPACPI_NOTICE
7123 "Console audio control in monitor mode, "
7124 "changes are not allowed.\n");
7125 printk(TPACPI_NOTICE
7126 "Use the volume_control=1 module parameter "
7127 "to enable volume control\n");
7129 return -EPERM;
7132 rc = volume_get_status(&s);
7133 if (rc < 0)
7134 return rc;
7136 new_level = s & TP_EC_AUDIO_LVL_MSK;
7137 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7139 while ((cmd = next_cmd(&buf))) {
7140 if (!tp_features.mixer_no_level_control) {
7141 if (strlencmp(cmd, "up") == 0) {
7142 if (new_mute)
7143 new_mute = 0;
7144 else if (new_level < TP_EC_VOLUME_MAX)
7145 new_level++;
7146 continue;
7147 } else if (strlencmp(cmd, "down") == 0) {
7148 if (new_mute)
7149 new_mute = 0;
7150 else if (new_level > 0)
7151 new_level--;
7152 continue;
7153 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7154 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7155 new_level = l;
7156 continue;
7159 if (strlencmp(cmd, "mute") == 0)
7160 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7161 else if (strlencmp(cmd, "unmute") == 0)
7162 new_mute = 0;
7163 else
7164 return -EINVAL;
7167 if (tp_features.mixer_no_level_control) {
7168 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7169 new_mute ? "" : "un");
7170 rc = volume_set_mute(!!new_mute);
7171 } else {
7172 tpacpi_disclose_usertask("procfs volume",
7173 "%smute and set level to %d\n",
7174 new_mute ? "" : "un", new_level);
7175 rc = volume_set_status(new_mute | new_level);
7177 volume_alsa_notify_change();
7179 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7182 static struct ibm_struct volume_driver_data = {
7183 .name = "volume",
7184 .read = volume_read,
7185 .write = volume_write,
7186 .exit = volume_exit,
7187 .suspend = volume_suspend,
7188 .resume = volume_resume,
7189 .shutdown = volume_shutdown,
7192 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7194 #define alsa_card NULL
7196 static void inline volume_alsa_notify_change(void)
7200 static int __init volume_init(struct ibm_init_struct *iibm)
7202 printk(TPACPI_INFO
7203 "volume: disabled as there is no ALSA support in this kernel\n");
7205 return 1;
7208 static struct ibm_struct volume_driver_data = {
7209 .name = "volume",
7212 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7214 /*************************************************************************
7215 * Fan subdriver
7219 * FAN ACCESS MODES
7221 * TPACPI_FAN_RD_ACPI_GFAN:
7222 * ACPI GFAN method: returns fan level
7224 * see TPACPI_FAN_WR_ACPI_SFAN
7225 * EC 0x2f (HFSP) not available if GFAN exists
7227 * TPACPI_FAN_WR_ACPI_SFAN:
7228 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7230 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7231 * it for writing.
7233 * TPACPI_FAN_WR_TPEC:
7234 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7235 * Supported on almost all ThinkPads
7237 * Fan speed changes of any sort (including those caused by the
7238 * disengaged mode) are usually done slowly by the firmware as the
7239 * maximum amount of fan duty cycle change per second seems to be
7240 * limited.
7242 * Reading is not available if GFAN exists.
7243 * Writing is not available if SFAN exists.
7245 * Bits
7246 * 7 automatic mode engaged;
7247 * (default operation mode of the ThinkPad)
7248 * fan level is ignored in this mode.
7249 * 6 full speed mode (takes precedence over bit 7);
7250 * not available on all thinkpads. May disable
7251 * the tachometer while the fan controller ramps up
7252 * the speed (which can take up to a few *minutes*).
7253 * Speeds up fan to 100% duty-cycle, which is far above
7254 * the standard RPM levels. It is not impossible that
7255 * it could cause hardware damage.
7256 * 5-3 unused in some models. Extra bits for fan level
7257 * in others, but still useless as all values above
7258 * 7 map to the same speed as level 7 in these models.
7259 * 2-0 fan level (0..7 usually)
7260 * 0x00 = stop
7261 * 0x07 = max (set when temperatures critical)
7262 * Some ThinkPads may have other levels, see
7263 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7265 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7266 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7267 * does so, its initial value is meaningless (0x07).
7269 * For firmware bugs, refer to:
7270 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7272 * ----
7274 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7275 * Main fan tachometer reading (in RPM)
7277 * This register is present on all ThinkPads with a new-style EC, and
7278 * it is known not to be present on the A21m/e, and T22, as there is
7279 * something else in offset 0x84 according to the ACPI DSDT. Other
7280 * ThinkPads from this same time period (and earlier) probably lack the
7281 * tachometer as well.
7283 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7284 * was never fixed by IBM to report the EC firmware version string
7285 * probably support the tachometer (like the early X models), so
7286 * detecting it is quite hard. We need more data to know for sure.
7288 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7289 * might result.
7291 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7292 * mode.
7294 * For firmware bugs, refer to:
7295 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7297 * ----
7299 * ThinkPad EC register 0x31 bit 0 (only on select models)
7301 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7302 * show the speed of the main fan. When bit 0 of EC register 0x31
7303 * is one, the tachometer registers show the speed of the auxiliary
7304 * fan.
7306 * Fan control seems to affect both fans, regardless of the state
7307 * of this bit.
7309 * So far, only the firmware for the X60/X61 non-tablet versions
7310 * seem to support this (firmware TP-7M).
7312 * TPACPI_FAN_WR_ACPI_FANS:
7313 * ThinkPad X31, X40, X41. Not available in the X60.
7315 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7316 * high speed. ACPI DSDT seems to map these three speeds to levels
7317 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7318 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7320 * The speeds are stored on handles
7321 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7323 * There are three default speed sets, accessible as handles:
7324 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7326 * ACPI DSDT switches which set is in use depending on various
7327 * factors.
7329 * TPACPI_FAN_WR_TPEC is also available and should be used to
7330 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7331 * but the ACPI tables just mention level 7.
7334 enum { /* Fan control constants */
7335 fan_status_offset = 0x2f, /* EC register 0x2f */
7336 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7337 * 0x84 must be read before 0x85 */
7338 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7339 bit 0 selects which fan is active */
7341 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7342 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7344 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7347 enum fan_status_access_mode {
7348 TPACPI_FAN_NONE = 0, /* No fan status or control */
7349 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7350 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7353 enum fan_control_access_mode {
7354 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7355 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7356 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7357 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7360 enum fan_control_commands {
7361 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7362 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7363 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7364 * and also watchdog cmd */
7367 static int fan_control_allowed;
7369 static enum fan_status_access_mode fan_status_access_mode;
7370 static enum fan_control_access_mode fan_control_access_mode;
7371 static enum fan_control_commands fan_control_commands;
7373 static u8 fan_control_initial_status;
7374 static u8 fan_control_desired_level;
7375 static u8 fan_control_resume_level;
7376 static int fan_watchdog_maxinterval;
7378 static struct mutex fan_mutex;
7380 static void fan_watchdog_fire(struct work_struct *ignored);
7381 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7383 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7384 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7385 "\\FSPD", /* 600e/x, 770e, 770x */
7386 ); /* all others */
7387 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7388 "JFNS", /* 770x-JL */
7389 ); /* all others */
7392 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7393 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7394 * be in auto mode (0x80).
7396 * This is corrected by any write to HFSP either by the driver, or
7397 * by the firmware.
7399 * We assume 0x07 really means auto mode while this quirk is active,
7400 * as this is far more likely than the ThinkPad being in level 7,
7401 * which is only used by the firmware during thermal emergencies.
7403 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7404 * TP-70 (T43, R52), which are known to be buggy.
7407 static void fan_quirk1_setup(void)
7409 if (fan_control_initial_status == 0x07) {
7410 printk(TPACPI_NOTICE
7411 "fan_init: initial fan status is unknown, "
7412 "assuming it is in auto mode\n");
7413 tp_features.fan_ctrl_status_undef = 1;
7417 static void fan_quirk1_handle(u8 *fan_status)
7419 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7420 if (*fan_status != fan_control_initial_status) {
7421 /* something changed the HFSP regisnter since
7422 * driver init time, so it is not undefined
7423 * anymore */
7424 tp_features.fan_ctrl_status_undef = 0;
7425 } else {
7426 /* Return most likely status. In fact, it
7427 * might be the only possible status */
7428 *fan_status = TP_EC_FAN_AUTO;
7433 /* Select main fan on X60/X61, NOOP on others */
7434 static bool fan_select_fan1(void)
7436 if (tp_features.second_fan) {
7437 u8 val;
7439 if (ec_read(fan_select_offset, &val) < 0)
7440 return false;
7441 val &= 0xFEU;
7442 if (ec_write(fan_select_offset, val) < 0)
7443 return false;
7445 return true;
7448 /* Select secondary fan on X60/X61 */
7449 static bool fan_select_fan2(void)
7451 u8 val;
7453 if (!tp_features.second_fan)
7454 return false;
7456 if (ec_read(fan_select_offset, &val) < 0)
7457 return false;
7458 val |= 0x01U;
7459 if (ec_write(fan_select_offset, val) < 0)
7460 return false;
7462 return true;
7466 * Call with fan_mutex held
7468 static void fan_update_desired_level(u8 status)
7470 if ((status &
7471 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7472 if (status > 7)
7473 fan_control_desired_level = 7;
7474 else
7475 fan_control_desired_level = status;
7479 static int fan_get_status(u8 *status)
7481 u8 s;
7483 /* TODO:
7484 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7486 switch (fan_status_access_mode) {
7487 case TPACPI_FAN_RD_ACPI_GFAN:
7488 /* 570, 600e/x, 770e, 770x */
7490 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7491 return -EIO;
7493 if (likely(status))
7494 *status = s & 0x07;
7496 break;
7498 case TPACPI_FAN_RD_TPEC:
7499 /* all except 570, 600e/x, 770e, 770x */
7500 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7501 return -EIO;
7503 if (likely(status)) {
7504 *status = s;
7505 fan_quirk1_handle(status);
7508 break;
7510 default:
7511 return -ENXIO;
7514 return 0;
7517 static int fan_get_status_safe(u8 *status)
7519 int rc;
7520 u8 s;
7522 if (mutex_lock_killable(&fan_mutex))
7523 return -ERESTARTSYS;
7524 rc = fan_get_status(&s);
7525 if (!rc)
7526 fan_update_desired_level(s);
7527 mutex_unlock(&fan_mutex);
7529 if (status)
7530 *status = s;
7532 return rc;
7535 static int fan_get_speed(unsigned int *speed)
7537 u8 hi, lo;
7539 switch (fan_status_access_mode) {
7540 case TPACPI_FAN_RD_TPEC:
7541 /* all except 570, 600e/x, 770e, 770x */
7542 if (unlikely(!fan_select_fan1()))
7543 return -EIO;
7544 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7545 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7546 return -EIO;
7548 if (likely(speed))
7549 *speed = (hi << 8) | lo;
7551 break;
7553 default:
7554 return -ENXIO;
7557 return 0;
7560 static int fan2_get_speed(unsigned int *speed)
7562 u8 hi, lo;
7563 bool rc;
7565 switch (fan_status_access_mode) {
7566 case TPACPI_FAN_RD_TPEC:
7567 /* all except 570, 600e/x, 770e, 770x */
7568 if (unlikely(!fan_select_fan2()))
7569 return -EIO;
7570 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7571 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7572 fan_select_fan1(); /* play it safe */
7573 if (rc)
7574 return -EIO;
7576 if (likely(speed))
7577 *speed = (hi << 8) | lo;
7579 break;
7581 default:
7582 return -ENXIO;
7585 return 0;
7588 static int fan_set_level(int level)
7590 if (!fan_control_allowed)
7591 return -EPERM;
7593 switch (fan_control_access_mode) {
7594 case TPACPI_FAN_WR_ACPI_SFAN:
7595 if (level >= 0 && level <= 7) {
7596 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7597 return -EIO;
7598 } else
7599 return -EINVAL;
7600 break;
7602 case TPACPI_FAN_WR_ACPI_FANS:
7603 case TPACPI_FAN_WR_TPEC:
7604 if (!(level & TP_EC_FAN_AUTO) &&
7605 !(level & TP_EC_FAN_FULLSPEED) &&
7606 ((level < 0) || (level > 7)))
7607 return -EINVAL;
7609 /* safety net should the EC not support AUTO
7610 * or FULLSPEED mode bits and just ignore them */
7611 if (level & TP_EC_FAN_FULLSPEED)
7612 level |= 7; /* safety min speed 7 */
7613 else if (level & TP_EC_FAN_AUTO)
7614 level |= 4; /* safety min speed 4 */
7616 if (!acpi_ec_write(fan_status_offset, level))
7617 return -EIO;
7618 else
7619 tp_features.fan_ctrl_status_undef = 0;
7620 break;
7622 default:
7623 return -ENXIO;
7626 vdbg_printk(TPACPI_DBG_FAN,
7627 "fan control: set fan control register to 0x%02x\n", level);
7628 return 0;
7631 static int fan_set_level_safe(int level)
7633 int rc;
7635 if (!fan_control_allowed)
7636 return -EPERM;
7638 if (mutex_lock_killable(&fan_mutex))
7639 return -ERESTARTSYS;
7641 if (level == TPACPI_FAN_LAST_LEVEL)
7642 level = fan_control_desired_level;
7644 rc = fan_set_level(level);
7645 if (!rc)
7646 fan_update_desired_level(level);
7648 mutex_unlock(&fan_mutex);
7649 return rc;
7652 static int fan_set_enable(void)
7654 u8 s;
7655 int rc;
7657 if (!fan_control_allowed)
7658 return -EPERM;
7660 if (mutex_lock_killable(&fan_mutex))
7661 return -ERESTARTSYS;
7663 switch (fan_control_access_mode) {
7664 case TPACPI_FAN_WR_ACPI_FANS:
7665 case TPACPI_FAN_WR_TPEC:
7666 rc = fan_get_status(&s);
7667 if (rc < 0)
7668 break;
7670 /* Don't go out of emergency fan mode */
7671 if (s != 7) {
7672 s &= 0x07;
7673 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7676 if (!acpi_ec_write(fan_status_offset, s))
7677 rc = -EIO;
7678 else {
7679 tp_features.fan_ctrl_status_undef = 0;
7680 rc = 0;
7682 break;
7684 case TPACPI_FAN_WR_ACPI_SFAN:
7685 rc = fan_get_status(&s);
7686 if (rc < 0)
7687 break;
7689 s &= 0x07;
7691 /* Set fan to at least level 4 */
7692 s |= 4;
7694 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7695 rc = -EIO;
7696 else
7697 rc = 0;
7698 break;
7700 default:
7701 rc = -ENXIO;
7704 mutex_unlock(&fan_mutex);
7706 if (!rc)
7707 vdbg_printk(TPACPI_DBG_FAN,
7708 "fan control: set fan control register to 0x%02x\n",
7710 return rc;
7713 static int fan_set_disable(void)
7715 int rc;
7717 if (!fan_control_allowed)
7718 return -EPERM;
7720 if (mutex_lock_killable(&fan_mutex))
7721 return -ERESTARTSYS;
7723 rc = 0;
7724 switch (fan_control_access_mode) {
7725 case TPACPI_FAN_WR_ACPI_FANS:
7726 case TPACPI_FAN_WR_TPEC:
7727 if (!acpi_ec_write(fan_status_offset, 0x00))
7728 rc = -EIO;
7729 else {
7730 fan_control_desired_level = 0;
7731 tp_features.fan_ctrl_status_undef = 0;
7733 break;
7735 case TPACPI_FAN_WR_ACPI_SFAN:
7736 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7737 rc = -EIO;
7738 else
7739 fan_control_desired_level = 0;
7740 break;
7742 default:
7743 rc = -ENXIO;
7746 if (!rc)
7747 vdbg_printk(TPACPI_DBG_FAN,
7748 "fan control: set fan control register to 0\n");
7750 mutex_unlock(&fan_mutex);
7751 return rc;
7754 static int fan_set_speed(int speed)
7756 int rc;
7758 if (!fan_control_allowed)
7759 return -EPERM;
7761 if (mutex_lock_killable(&fan_mutex))
7762 return -ERESTARTSYS;
7764 rc = 0;
7765 switch (fan_control_access_mode) {
7766 case TPACPI_FAN_WR_ACPI_FANS:
7767 if (speed >= 0 && speed <= 65535) {
7768 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7769 speed, speed, speed))
7770 rc = -EIO;
7771 } else
7772 rc = -EINVAL;
7773 break;
7775 default:
7776 rc = -ENXIO;
7779 mutex_unlock(&fan_mutex);
7780 return rc;
7783 static void fan_watchdog_reset(void)
7785 static int fan_watchdog_active;
7787 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7788 return;
7790 if (fan_watchdog_active)
7791 cancel_delayed_work(&fan_watchdog_task);
7793 if (fan_watchdog_maxinterval > 0 &&
7794 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7795 fan_watchdog_active = 1;
7796 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7797 msecs_to_jiffies(fan_watchdog_maxinterval
7798 * 1000))) {
7799 printk(TPACPI_ERR
7800 "failed to queue the fan watchdog, "
7801 "watchdog will not trigger\n");
7803 } else
7804 fan_watchdog_active = 0;
7807 static void fan_watchdog_fire(struct work_struct *ignored)
7809 int rc;
7811 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7812 return;
7814 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7815 rc = fan_set_enable();
7816 if (rc < 0) {
7817 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7818 "will try again later...\n", -rc);
7819 /* reschedule for later */
7820 fan_watchdog_reset();
7825 * SYSFS fan layout: hwmon compatible (device)
7827 * pwm*_enable:
7828 * 0: "disengaged" mode
7829 * 1: manual mode
7830 * 2: native EC "auto" mode (recommended, hardware default)
7832 * pwm*: set speed in manual mode, ignored otherwise.
7833 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7834 * interpolation.
7836 * fan*_input: tachometer reading, RPM
7839 * SYSFS fan layout: extensions
7841 * fan_watchdog (driver):
7842 * fan watchdog interval in seconds, 0 disables (default), max 120
7845 /* sysfs fan pwm1_enable ----------------------------------------------- */
7846 static ssize_t fan_pwm1_enable_show(struct device *dev,
7847 struct device_attribute *attr,
7848 char *buf)
7850 int res, mode;
7851 u8 status;
7853 res = fan_get_status_safe(&status);
7854 if (res)
7855 return res;
7857 if (status & TP_EC_FAN_FULLSPEED) {
7858 mode = 0;
7859 } else if (status & TP_EC_FAN_AUTO) {
7860 mode = 2;
7861 } else
7862 mode = 1;
7864 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7867 static ssize_t fan_pwm1_enable_store(struct device *dev,
7868 struct device_attribute *attr,
7869 const char *buf, size_t count)
7871 unsigned long t;
7872 int res, level;
7874 if (parse_strtoul(buf, 2, &t))
7875 return -EINVAL;
7877 tpacpi_disclose_usertask("hwmon pwm1_enable",
7878 "set fan mode to %lu\n", t);
7880 switch (t) {
7881 case 0:
7882 level = TP_EC_FAN_FULLSPEED;
7883 break;
7884 case 1:
7885 level = TPACPI_FAN_LAST_LEVEL;
7886 break;
7887 case 2:
7888 level = TP_EC_FAN_AUTO;
7889 break;
7890 case 3:
7891 /* reserved for software-controlled auto mode */
7892 return -ENOSYS;
7893 default:
7894 return -EINVAL;
7897 res = fan_set_level_safe(level);
7898 if (res == -ENXIO)
7899 return -EINVAL;
7900 else if (res < 0)
7901 return res;
7903 fan_watchdog_reset();
7905 return count;
7908 static struct device_attribute dev_attr_fan_pwm1_enable =
7909 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7910 fan_pwm1_enable_show, fan_pwm1_enable_store);
7912 /* sysfs fan pwm1 ------------------------------------------------------ */
7913 static ssize_t fan_pwm1_show(struct device *dev,
7914 struct device_attribute *attr,
7915 char *buf)
7917 int res;
7918 u8 status;
7920 res = fan_get_status_safe(&status);
7921 if (res)
7922 return res;
7924 if ((status &
7925 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7926 status = fan_control_desired_level;
7928 if (status > 7)
7929 status = 7;
7931 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7934 static ssize_t fan_pwm1_store(struct device *dev,
7935 struct device_attribute *attr,
7936 const char *buf, size_t count)
7938 unsigned long s;
7939 int rc;
7940 u8 status, newlevel;
7942 if (parse_strtoul(buf, 255, &s))
7943 return -EINVAL;
7945 tpacpi_disclose_usertask("hwmon pwm1",
7946 "set fan speed to %lu\n", s);
7948 /* scale down from 0-255 to 0-7 */
7949 newlevel = (s >> 5) & 0x07;
7951 if (mutex_lock_killable(&fan_mutex))
7952 return -ERESTARTSYS;
7954 rc = fan_get_status(&status);
7955 if (!rc && (status &
7956 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7957 rc = fan_set_level(newlevel);
7958 if (rc == -ENXIO)
7959 rc = -EINVAL;
7960 else if (!rc) {
7961 fan_update_desired_level(newlevel);
7962 fan_watchdog_reset();
7966 mutex_unlock(&fan_mutex);
7967 return (rc)? rc : count;
7970 static struct device_attribute dev_attr_fan_pwm1 =
7971 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7972 fan_pwm1_show, fan_pwm1_store);
7974 /* sysfs fan fan1_input ------------------------------------------------ */
7975 static ssize_t fan_fan1_input_show(struct device *dev,
7976 struct device_attribute *attr,
7977 char *buf)
7979 int res;
7980 unsigned int speed;
7982 res = fan_get_speed(&speed);
7983 if (res < 0)
7984 return res;
7986 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7989 static struct device_attribute dev_attr_fan_fan1_input =
7990 __ATTR(fan1_input, S_IRUGO,
7991 fan_fan1_input_show, NULL);
7993 /* sysfs fan fan2_input ------------------------------------------------ */
7994 static ssize_t fan_fan2_input_show(struct device *dev,
7995 struct device_attribute *attr,
7996 char *buf)
7998 int res;
7999 unsigned int speed;
8001 res = fan2_get_speed(&speed);
8002 if (res < 0)
8003 return res;
8005 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8008 static struct device_attribute dev_attr_fan_fan2_input =
8009 __ATTR(fan2_input, S_IRUGO,
8010 fan_fan2_input_show, NULL);
8012 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8013 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8014 char *buf)
8016 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8019 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8020 const char *buf, size_t count)
8022 unsigned long t;
8024 if (parse_strtoul(buf, 120, &t))
8025 return -EINVAL;
8027 if (!fan_control_allowed)
8028 return -EPERM;
8030 fan_watchdog_maxinterval = t;
8031 fan_watchdog_reset();
8033 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8035 return count;
8038 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8039 fan_fan_watchdog_show, fan_fan_watchdog_store);
8041 /* --------------------------------------------------------------------- */
8042 static struct attribute *fan_attributes[] = {
8043 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8044 &dev_attr_fan_fan1_input.attr,
8045 NULL, /* for fan2_input */
8046 NULL
8049 static const struct attribute_group fan_attr_group = {
8050 .attrs = fan_attributes,
8053 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8054 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8056 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8057 { .vendor = PCI_VENDOR_ID_IBM, \
8058 .bios = TPACPI_MATCH_ANY, \
8059 .ec = TPID(__id1, __id2), \
8060 .quirks = __quirks }
8062 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8063 { .vendor = PCI_VENDOR_ID_LENOVO, \
8064 .bios = TPACPI_MATCH_ANY, \
8065 .ec = TPID(__id1, __id2), \
8066 .quirks = __quirks }
8068 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8069 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8070 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8071 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8072 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8073 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8076 #undef TPACPI_FAN_QL
8077 #undef TPACPI_FAN_QI
8079 static int __init fan_init(struct ibm_init_struct *iibm)
8081 int rc;
8082 unsigned long quirks;
8084 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8085 "initializing fan subdriver\n");
8087 mutex_init(&fan_mutex);
8088 fan_status_access_mode = TPACPI_FAN_NONE;
8089 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8090 fan_control_commands = 0;
8091 fan_watchdog_maxinterval = 0;
8092 tp_features.fan_ctrl_status_undef = 0;
8093 tp_features.second_fan = 0;
8094 fan_control_desired_level = 7;
8096 if (tpacpi_is_ibm()) {
8097 TPACPI_ACPIHANDLE_INIT(fans);
8098 TPACPI_ACPIHANDLE_INIT(gfan);
8099 TPACPI_ACPIHANDLE_INIT(sfan);
8102 quirks = tpacpi_check_quirks(fan_quirk_table,
8103 ARRAY_SIZE(fan_quirk_table));
8105 if (gfan_handle) {
8106 /* 570, 600e/x, 770e, 770x */
8107 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8108 } else {
8109 /* all other ThinkPads: note that even old-style
8110 * ThinkPad ECs supports the fan control register */
8111 if (likely(acpi_ec_read(fan_status_offset,
8112 &fan_control_initial_status))) {
8113 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8114 if (quirks & TPACPI_FAN_Q1)
8115 fan_quirk1_setup();
8116 if (quirks & TPACPI_FAN_2FAN) {
8117 tp_features.second_fan = 1;
8118 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8119 "secondary fan support enabled\n");
8121 } else {
8122 printk(TPACPI_ERR
8123 "ThinkPad ACPI EC access misbehaving, "
8124 "fan status and control unavailable\n");
8125 return 1;
8129 if (sfan_handle) {
8130 /* 570, 770x-JL */
8131 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8132 fan_control_commands |=
8133 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8134 } else {
8135 if (!gfan_handle) {
8136 /* gfan without sfan means no fan control */
8137 /* all other models implement TP EC 0x2f control */
8139 if (fans_handle) {
8140 /* X31, X40, X41 */
8141 fan_control_access_mode =
8142 TPACPI_FAN_WR_ACPI_FANS;
8143 fan_control_commands |=
8144 TPACPI_FAN_CMD_SPEED |
8145 TPACPI_FAN_CMD_LEVEL |
8146 TPACPI_FAN_CMD_ENABLE;
8147 } else {
8148 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8149 fan_control_commands |=
8150 TPACPI_FAN_CMD_LEVEL |
8151 TPACPI_FAN_CMD_ENABLE;
8156 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8157 "fan is %s, modes %d, %d\n",
8158 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8159 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8160 fan_status_access_mode, fan_control_access_mode);
8162 /* fan control master switch */
8163 if (!fan_control_allowed) {
8164 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8165 fan_control_commands = 0;
8166 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8167 "fan control features disabled by parameter\n");
8170 /* update fan_control_desired_level */
8171 if (fan_status_access_mode != TPACPI_FAN_NONE)
8172 fan_get_status_safe(NULL);
8174 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8175 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8176 if (tp_features.second_fan) {
8177 /* attach second fan tachometer */
8178 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8179 &dev_attr_fan_fan2_input.attr;
8181 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8182 &fan_attr_group);
8183 if (rc < 0)
8184 return rc;
8186 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8187 &driver_attr_fan_watchdog);
8188 if (rc < 0) {
8189 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8190 &fan_attr_group);
8191 return rc;
8193 return 0;
8194 } else
8195 return 1;
8198 static void fan_exit(void)
8200 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8201 "cancelling any pending fan watchdog tasks\n");
8203 /* FIXME: can we really do this unconditionally? */
8204 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8205 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8206 &driver_attr_fan_watchdog);
8208 cancel_delayed_work(&fan_watchdog_task);
8209 flush_workqueue(tpacpi_wq);
8212 static void fan_suspend(pm_message_t state)
8214 int rc;
8216 if (!fan_control_allowed)
8217 return;
8219 /* Store fan status in cache */
8220 fan_control_resume_level = 0;
8221 rc = fan_get_status_safe(&fan_control_resume_level);
8222 if (rc < 0)
8223 printk(TPACPI_NOTICE
8224 "failed to read fan level for later "
8225 "restore during resume: %d\n", rc);
8227 /* if it is undefined, don't attempt to restore it.
8228 * KEEP THIS LAST */
8229 if (tp_features.fan_ctrl_status_undef)
8230 fan_control_resume_level = 0;
8233 static void fan_resume(void)
8235 u8 current_level = 7;
8236 bool do_set = false;
8237 int rc;
8239 /* DSDT *always* updates status on resume */
8240 tp_features.fan_ctrl_status_undef = 0;
8242 if (!fan_control_allowed ||
8243 !fan_control_resume_level ||
8244 (fan_get_status_safe(&current_level) < 0))
8245 return;
8247 switch (fan_control_access_mode) {
8248 case TPACPI_FAN_WR_ACPI_SFAN:
8249 /* never decrease fan level */
8250 do_set = (fan_control_resume_level > current_level);
8251 break;
8252 case TPACPI_FAN_WR_ACPI_FANS:
8253 case TPACPI_FAN_WR_TPEC:
8254 /* never decrease fan level, scale is:
8255 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8257 * We expect the firmware to set either 7 or AUTO, but we
8258 * handle FULLSPEED out of paranoia.
8260 * So, we can safely only restore FULLSPEED or 7, anything
8261 * else could slow the fan. Restoring AUTO is useless, at
8262 * best that's exactly what the DSDT already set (it is the
8263 * slower it uses).
8265 * Always keep in mind that the DSDT *will* have set the
8266 * fans to what the vendor supposes is the best level. We
8267 * muck with it only to speed the fan up.
8269 if (fan_control_resume_level != 7 &&
8270 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8271 return;
8272 else
8273 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8274 (current_level != fan_control_resume_level);
8275 break;
8276 default:
8277 return;
8279 if (do_set) {
8280 printk(TPACPI_NOTICE
8281 "restoring fan level to 0x%02x\n",
8282 fan_control_resume_level);
8283 rc = fan_set_level_safe(fan_control_resume_level);
8284 if (rc < 0)
8285 printk(TPACPI_NOTICE
8286 "failed to restore fan level: %d\n", rc);
8290 static int fan_read(struct seq_file *m)
8292 int rc;
8293 u8 status;
8294 unsigned int speed = 0;
8296 switch (fan_status_access_mode) {
8297 case TPACPI_FAN_RD_ACPI_GFAN:
8298 /* 570, 600e/x, 770e, 770x */
8299 rc = fan_get_status_safe(&status);
8300 if (rc < 0)
8301 return rc;
8303 seq_printf(m, "status:\t\t%s\n"
8304 "level:\t\t%d\n",
8305 (status != 0) ? "enabled" : "disabled", status);
8306 break;
8308 case TPACPI_FAN_RD_TPEC:
8309 /* all except 570, 600e/x, 770e, 770x */
8310 rc = fan_get_status_safe(&status);
8311 if (rc < 0)
8312 return rc;
8314 seq_printf(m, "status:\t\t%s\n",
8315 (status != 0) ? "enabled" : "disabled");
8317 rc = fan_get_speed(&speed);
8318 if (rc < 0)
8319 return rc;
8321 seq_printf(m, "speed:\t\t%d\n", speed);
8323 if (status & TP_EC_FAN_FULLSPEED)
8324 /* Disengaged mode takes precedence */
8325 seq_printf(m, "level:\t\tdisengaged\n");
8326 else if (status & TP_EC_FAN_AUTO)
8327 seq_printf(m, "level:\t\tauto\n");
8328 else
8329 seq_printf(m, "level:\t\t%d\n", status);
8330 break;
8332 case TPACPI_FAN_NONE:
8333 default:
8334 seq_printf(m, "status:\t\tnot supported\n");
8337 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8338 seq_printf(m, "commands:\tlevel <level>");
8340 switch (fan_control_access_mode) {
8341 case TPACPI_FAN_WR_ACPI_SFAN:
8342 seq_printf(m, " (<level> is 0-7)\n");
8343 break;
8345 default:
8346 seq_printf(m, " (<level> is 0-7, "
8347 "auto, disengaged, full-speed)\n");
8348 break;
8352 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8353 seq_printf(m, "commands:\tenable, disable\n"
8354 "commands:\twatchdog <timeout> (<timeout> "
8355 "is 0 (off), 1-120 (seconds))\n");
8357 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8358 seq_printf(m, "commands:\tspeed <speed>"
8359 " (<speed> is 0-65535)\n");
8361 return 0;
8364 static int fan_write_cmd_level(const char *cmd, int *rc)
8366 int level;
8368 if (strlencmp(cmd, "level auto") == 0)
8369 level = TP_EC_FAN_AUTO;
8370 else if ((strlencmp(cmd, "level disengaged") == 0) |
8371 (strlencmp(cmd, "level full-speed") == 0))
8372 level = TP_EC_FAN_FULLSPEED;
8373 else if (sscanf(cmd, "level %d", &level) != 1)
8374 return 0;
8376 *rc = fan_set_level_safe(level);
8377 if (*rc == -ENXIO)
8378 printk(TPACPI_ERR "level command accepted for unsupported "
8379 "access mode %d", fan_control_access_mode);
8380 else if (!*rc)
8381 tpacpi_disclose_usertask("procfs fan",
8382 "set level to %d\n", level);
8384 return 1;
8387 static int fan_write_cmd_enable(const char *cmd, int *rc)
8389 if (strlencmp(cmd, "enable") != 0)
8390 return 0;
8392 *rc = fan_set_enable();
8393 if (*rc == -ENXIO)
8394 printk(TPACPI_ERR "enable command accepted for unsupported "
8395 "access mode %d", fan_control_access_mode);
8396 else if (!*rc)
8397 tpacpi_disclose_usertask("procfs fan", "enable\n");
8399 return 1;
8402 static int fan_write_cmd_disable(const char *cmd, int *rc)
8404 if (strlencmp(cmd, "disable") != 0)
8405 return 0;
8407 *rc = fan_set_disable();
8408 if (*rc == -ENXIO)
8409 printk(TPACPI_ERR "disable command accepted for unsupported "
8410 "access mode %d", fan_control_access_mode);
8411 else if (!*rc)
8412 tpacpi_disclose_usertask("procfs fan", "disable\n");
8414 return 1;
8417 static int fan_write_cmd_speed(const char *cmd, int *rc)
8419 int speed;
8421 /* TODO:
8422 * Support speed <low> <medium> <high> ? */
8424 if (sscanf(cmd, "speed %d", &speed) != 1)
8425 return 0;
8427 *rc = fan_set_speed(speed);
8428 if (*rc == -ENXIO)
8429 printk(TPACPI_ERR "speed command accepted for unsupported "
8430 "access mode %d", fan_control_access_mode);
8431 else if (!*rc)
8432 tpacpi_disclose_usertask("procfs fan",
8433 "set speed to %d\n", speed);
8435 return 1;
8438 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8440 int interval;
8442 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8443 return 0;
8445 if (interval < 0 || interval > 120)
8446 *rc = -EINVAL;
8447 else {
8448 fan_watchdog_maxinterval = interval;
8449 tpacpi_disclose_usertask("procfs fan",
8450 "set watchdog timer to %d\n",
8451 interval);
8454 return 1;
8457 static int fan_write(char *buf)
8459 char *cmd;
8460 int rc = 0;
8462 while (!rc && (cmd = next_cmd(&buf))) {
8463 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8464 fan_write_cmd_level(cmd, &rc)) &&
8465 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8466 (fan_write_cmd_enable(cmd, &rc) ||
8467 fan_write_cmd_disable(cmd, &rc) ||
8468 fan_write_cmd_watchdog(cmd, &rc))) &&
8469 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8470 fan_write_cmd_speed(cmd, &rc))
8472 rc = -EINVAL;
8473 else if (!rc)
8474 fan_watchdog_reset();
8477 return rc;
8480 static struct ibm_struct fan_driver_data = {
8481 .name = "fan",
8482 .read = fan_read,
8483 .write = fan_write,
8484 .exit = fan_exit,
8485 .suspend = fan_suspend,
8486 .resume = fan_resume,
8489 /****************************************************************************
8490 ****************************************************************************
8492 * Infrastructure
8494 ****************************************************************************
8495 ****************************************************************************/
8498 * HKEY event callout for other subdrivers go here
8499 * (yes, it is ugly, but it is quick, safe, and gets the job done
8501 static void tpacpi_driver_event(const unsigned int hkey_event)
8503 if (ibm_backlight_device) {
8504 switch (hkey_event) {
8505 case TP_HKEY_EV_BRGHT_UP:
8506 case TP_HKEY_EV_BRGHT_DOWN:
8507 tpacpi_brightness_notify_change();
8510 if (alsa_card) {
8511 switch (hkey_event) {
8512 case TP_HKEY_EV_VOL_UP:
8513 case TP_HKEY_EV_VOL_DOWN:
8514 case TP_HKEY_EV_VOL_MUTE:
8515 volume_alsa_notify_change();
8520 static void hotkey_driver_event(const unsigned int scancode)
8522 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8525 /* sysfs name ---------------------------------------------------------- */
8526 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8527 struct device_attribute *attr,
8528 char *buf)
8530 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8533 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8534 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8536 /* --------------------------------------------------------------------- */
8538 /* /proc support */
8539 static struct proc_dir_entry *proc_dir;
8542 * Module and infrastructure proble, init and exit handling
8545 static int force_load;
8547 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8548 static const char * __init str_supported(int is_supported)
8550 static char text_unsupported[] __initdata = "not supported";
8552 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8554 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8556 static void ibm_exit(struct ibm_struct *ibm)
8558 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8560 list_del_init(&ibm->all_drivers);
8562 if (ibm->flags.acpi_notify_installed) {
8563 dbg_printk(TPACPI_DBG_EXIT,
8564 "%s: acpi_remove_notify_handler\n", ibm->name);
8565 BUG_ON(!ibm->acpi);
8566 acpi_remove_notify_handler(*ibm->acpi->handle,
8567 ibm->acpi->type,
8568 dispatch_acpi_notify);
8569 ibm->flags.acpi_notify_installed = 0;
8572 if (ibm->flags.proc_created) {
8573 dbg_printk(TPACPI_DBG_EXIT,
8574 "%s: remove_proc_entry\n", ibm->name);
8575 remove_proc_entry(ibm->name, proc_dir);
8576 ibm->flags.proc_created = 0;
8579 if (ibm->flags.acpi_driver_registered) {
8580 dbg_printk(TPACPI_DBG_EXIT,
8581 "%s: acpi_bus_unregister_driver\n", ibm->name);
8582 BUG_ON(!ibm->acpi);
8583 acpi_bus_unregister_driver(ibm->acpi->driver);
8584 kfree(ibm->acpi->driver);
8585 ibm->acpi->driver = NULL;
8586 ibm->flags.acpi_driver_registered = 0;
8589 if (ibm->flags.init_called && ibm->exit) {
8590 ibm->exit();
8591 ibm->flags.init_called = 0;
8594 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8597 static int __init ibm_init(struct ibm_init_struct *iibm)
8599 int ret;
8600 struct ibm_struct *ibm = iibm->data;
8601 struct proc_dir_entry *entry;
8603 BUG_ON(ibm == NULL);
8605 INIT_LIST_HEAD(&ibm->all_drivers);
8607 if (ibm->flags.experimental && !experimental)
8608 return 0;
8610 dbg_printk(TPACPI_DBG_INIT,
8611 "probing for %s\n", ibm->name);
8613 if (iibm->init) {
8614 ret = iibm->init(iibm);
8615 if (ret > 0)
8616 return 0; /* probe failed */
8617 if (ret)
8618 return ret;
8620 ibm->flags.init_called = 1;
8623 if (ibm->acpi) {
8624 if (ibm->acpi->hid) {
8625 ret = register_tpacpi_subdriver(ibm);
8626 if (ret)
8627 goto err_out;
8630 if (ibm->acpi->notify) {
8631 ret = setup_acpi_notify(ibm);
8632 if (ret == -ENODEV) {
8633 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8634 ibm->name);
8635 ret = 0;
8636 goto err_out;
8638 if (ret < 0)
8639 goto err_out;
8643 dbg_printk(TPACPI_DBG_INIT,
8644 "%s installed\n", ibm->name);
8646 if (ibm->read) {
8647 mode_t mode = iibm->base_procfs_mode;
8649 if (!mode)
8650 mode = S_IRUGO;
8651 if (ibm->write)
8652 mode |= S_IWUSR;
8653 entry = proc_create_data(ibm->name, mode, proc_dir,
8654 &dispatch_proc_fops, ibm);
8655 if (!entry) {
8656 printk(TPACPI_ERR "unable to create proc entry %s\n",
8657 ibm->name);
8658 ret = -ENODEV;
8659 goto err_out;
8661 ibm->flags.proc_created = 1;
8664 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8666 return 0;
8668 err_out:
8669 dbg_printk(TPACPI_DBG_INIT,
8670 "%s: at error exit path with result %d\n",
8671 ibm->name, ret);
8673 ibm_exit(ibm);
8674 return (ret < 0)? ret : 0;
8677 /* Probing */
8679 static bool __pure __init tpacpi_is_fw_digit(const char c)
8681 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8684 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8685 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8686 const char t)
8688 return s && strlen(s) >= 8 &&
8689 tpacpi_is_fw_digit(s[0]) &&
8690 tpacpi_is_fw_digit(s[1]) &&
8691 s[2] == t && s[3] == 'T' &&
8692 tpacpi_is_fw_digit(s[4]) &&
8693 tpacpi_is_fw_digit(s[5]) &&
8694 s[6] == 'W' && s[7] == 'W';
8697 /* returns 0 - probe ok, or < 0 - probe error.
8698 * Probe ok doesn't mean thinkpad found.
8699 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8700 static int __must_check __init get_thinkpad_model_data(
8701 struct thinkpad_id_data *tp)
8703 const struct dmi_device *dev = NULL;
8704 char ec_fw_string[18];
8705 char const *s;
8707 if (!tp)
8708 return -EINVAL;
8710 memset(tp, 0, sizeof(*tp));
8712 if (dmi_name_in_vendors("IBM"))
8713 tp->vendor = PCI_VENDOR_ID_IBM;
8714 else if (dmi_name_in_vendors("LENOVO"))
8715 tp->vendor = PCI_VENDOR_ID_LENOVO;
8716 else
8717 return 0;
8719 s = dmi_get_system_info(DMI_BIOS_VERSION);
8720 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8721 if (s && !tp->bios_version_str)
8722 return -ENOMEM;
8724 /* Really ancient ThinkPad 240X will fail this, which is fine */
8725 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8726 return 0;
8728 tp->bios_model = tp->bios_version_str[0]
8729 | (tp->bios_version_str[1] << 8);
8730 tp->bios_release = (tp->bios_version_str[4] << 8)
8731 | tp->bios_version_str[5];
8734 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8735 * X32 or newer, all Z series; Some models must have an
8736 * up-to-date BIOS or they will not be detected.
8738 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8740 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8741 if (sscanf(dev->name,
8742 "IBM ThinkPad Embedded Controller -[%17c",
8743 ec_fw_string) == 1) {
8744 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8745 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8747 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8748 if (!tp->ec_version_str)
8749 return -ENOMEM;
8751 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8752 tp->ec_model = ec_fw_string[0]
8753 | (ec_fw_string[1] << 8);
8754 tp->ec_release = (ec_fw_string[4] << 8)
8755 | ec_fw_string[5];
8756 } else {
8757 printk(TPACPI_NOTICE
8758 "ThinkPad firmware release %s "
8759 "doesn't match the known patterns\n",
8760 ec_fw_string);
8761 printk(TPACPI_NOTICE
8762 "please report this to %s\n",
8763 TPACPI_MAIL);
8765 break;
8769 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8770 if (s && !strnicmp(s, "ThinkPad", 8)) {
8771 tp->model_str = kstrdup(s, GFP_KERNEL);
8772 if (!tp->model_str)
8773 return -ENOMEM;
8776 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8777 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8778 if (s && !tp->nummodel_str)
8779 return -ENOMEM;
8781 return 0;
8784 static int __init probe_for_thinkpad(void)
8786 int is_thinkpad;
8788 if (acpi_disabled)
8789 return -ENODEV;
8791 /* It would be dangerous to run the driver in this case */
8792 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8793 return -ENODEV;
8796 * Non-ancient models have better DMI tagging, but very old models
8797 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8799 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8800 (thinkpad_id.ec_model != 0) ||
8801 tpacpi_is_fw_known();
8803 /* The EC handler is required */
8804 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8805 if (!ec_handle) {
8806 if (is_thinkpad)
8807 printk(TPACPI_ERR
8808 "Not yet supported ThinkPad detected!\n");
8809 return -ENODEV;
8812 if (!is_thinkpad && !force_load)
8813 return -ENODEV;
8815 return 0;
8818 static void __init thinkpad_acpi_init_banner(void)
8820 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8821 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8823 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8824 (thinkpad_id.bios_version_str) ?
8825 thinkpad_id.bios_version_str : "unknown",
8826 (thinkpad_id.ec_version_str) ?
8827 thinkpad_id.ec_version_str : "unknown");
8829 BUG_ON(!thinkpad_id.vendor);
8831 if (thinkpad_id.model_str)
8832 printk(TPACPI_INFO "%s %s, model %s\n",
8833 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8834 "IBM" : ((thinkpad_id.vendor ==
8835 PCI_VENDOR_ID_LENOVO) ?
8836 "Lenovo" : "Unknown vendor"),
8837 thinkpad_id.model_str,
8838 (thinkpad_id.nummodel_str) ?
8839 thinkpad_id.nummodel_str : "unknown");
8842 /* Module init, exit, parameters */
8844 static struct ibm_init_struct ibms_init[] __initdata = {
8846 .data = &thinkpad_acpi_driver_data,
8849 .init = hotkey_init,
8850 .data = &hotkey_driver_data,
8853 .init = bluetooth_init,
8854 .data = &bluetooth_driver_data,
8857 .init = wan_init,
8858 .data = &wan_driver_data,
8861 .init = uwb_init,
8862 .data = &uwb_driver_data,
8864 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8866 .init = video_init,
8867 .base_procfs_mode = S_IRUSR,
8868 .data = &video_driver_data,
8870 #endif
8872 .init = light_init,
8873 .data = &light_driver_data,
8876 .init = cmos_init,
8877 .data = &cmos_driver_data,
8880 .init = led_init,
8881 .data = &led_driver_data,
8884 .init = beep_init,
8885 .data = &beep_driver_data,
8888 .init = thermal_init,
8889 .data = &thermal_driver_data,
8892 .data = &ecdump_driver_data,
8895 .init = brightness_init,
8896 .data = &brightness_driver_data,
8899 .init = volume_init,
8900 .data = &volume_driver_data,
8903 .init = fan_init,
8904 .data = &fan_driver_data,
8908 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8910 unsigned int i;
8911 struct ibm_struct *ibm;
8913 if (!kp || !kp->name || !val)
8914 return -EINVAL;
8916 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8917 ibm = ibms_init[i].data;
8918 WARN_ON(ibm == NULL);
8920 if (!ibm || !ibm->name)
8921 continue;
8923 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8924 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8925 return -ENOSPC;
8926 strcpy(ibms_init[i].param, val);
8927 strcat(ibms_init[i].param, ",");
8928 return 0;
8932 return -EINVAL;
8935 module_param(experimental, int, 0444);
8936 MODULE_PARM_DESC(experimental,
8937 "Enables experimental features when non-zero");
8939 module_param_named(debug, dbg_level, uint, 0);
8940 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8942 module_param(force_load, bool, 0444);
8943 MODULE_PARM_DESC(force_load,
8944 "Attempts to load the driver even on a "
8945 "mis-identified ThinkPad when true");
8947 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8948 MODULE_PARM_DESC(fan_control,
8949 "Enables setting fan parameters features when true");
8951 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8952 MODULE_PARM_DESC(brightness_mode,
8953 "Selects brightness control strategy: "
8954 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8956 module_param(brightness_enable, uint, 0444);
8957 MODULE_PARM_DESC(brightness_enable,
8958 "Enables backlight control when 1, disables when 0");
8960 module_param(hotkey_report_mode, uint, 0444);
8961 MODULE_PARM_DESC(hotkey_report_mode,
8962 "used for backwards compatibility with userspace, "
8963 "see documentation");
8965 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8966 module_param_named(volume_mode, volume_mode, uint, 0444);
8967 MODULE_PARM_DESC(volume_mode,
8968 "Selects volume control strategy: "
8969 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8971 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8972 MODULE_PARM_DESC(volume_capabilities,
8973 "Selects the mixer capabilites: "
8974 "0=auto, 1=volume and mute, 2=mute only");
8976 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8977 MODULE_PARM_DESC(volume_control,
8978 "Enables software override for the console audio "
8979 "control when true");
8981 /* ALSA module API parameters */
8982 module_param_named(index, alsa_index, int, 0444);
8983 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8984 module_param_named(id, alsa_id, charp, 0444);
8985 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8986 module_param_named(enable, alsa_enable, bool, 0444);
8987 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8988 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8990 #define TPACPI_PARAM(feature) \
8991 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8992 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8993 "at module load, see documentation")
8995 TPACPI_PARAM(hotkey);
8996 TPACPI_PARAM(bluetooth);
8997 TPACPI_PARAM(video);
8998 TPACPI_PARAM(light);
8999 TPACPI_PARAM(cmos);
9000 TPACPI_PARAM(led);
9001 TPACPI_PARAM(beep);
9002 TPACPI_PARAM(ecdump);
9003 TPACPI_PARAM(brightness);
9004 TPACPI_PARAM(volume);
9005 TPACPI_PARAM(fan);
9007 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9008 module_param(dbg_wlswemul, uint, 0444);
9009 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9010 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9011 MODULE_PARM_DESC(wlsw_state,
9012 "Initial state of the emulated WLSW switch");
9014 module_param(dbg_bluetoothemul, uint, 0444);
9015 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9016 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9017 MODULE_PARM_DESC(bluetooth_state,
9018 "Initial state of the emulated bluetooth switch");
9020 module_param(dbg_wwanemul, uint, 0444);
9021 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9022 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9023 MODULE_PARM_DESC(wwan_state,
9024 "Initial state of the emulated WWAN switch");
9026 module_param(dbg_uwbemul, uint, 0444);
9027 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9028 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9029 MODULE_PARM_DESC(uwb_state,
9030 "Initial state of the emulated UWB switch");
9031 #endif
9033 static void thinkpad_acpi_module_exit(void)
9035 struct ibm_struct *ibm, *itmp;
9037 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9039 list_for_each_entry_safe_reverse(ibm, itmp,
9040 &tpacpi_all_drivers,
9041 all_drivers) {
9042 ibm_exit(ibm);
9045 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9047 if (tpacpi_inputdev) {
9048 if (tp_features.input_device_registered)
9049 input_unregister_device(tpacpi_inputdev);
9050 else
9051 input_free_device(tpacpi_inputdev);
9054 if (tpacpi_hwmon)
9055 hwmon_device_unregister(tpacpi_hwmon);
9057 if (tp_features.sensors_pdev_attrs_registered)
9058 device_remove_file(&tpacpi_sensors_pdev->dev,
9059 &dev_attr_thinkpad_acpi_pdev_name);
9060 if (tpacpi_sensors_pdev)
9061 platform_device_unregister(tpacpi_sensors_pdev);
9062 if (tpacpi_pdev)
9063 platform_device_unregister(tpacpi_pdev);
9065 if (tp_features.sensors_pdrv_attrs_registered)
9066 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9067 if (tp_features.platform_drv_attrs_registered)
9068 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9070 if (tp_features.sensors_pdrv_registered)
9071 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9073 if (tp_features.platform_drv_registered)
9074 platform_driver_unregister(&tpacpi_pdriver);
9076 if (proc_dir)
9077 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9079 if (tpacpi_wq)
9080 destroy_workqueue(tpacpi_wq);
9082 kfree(thinkpad_id.bios_version_str);
9083 kfree(thinkpad_id.ec_version_str);
9084 kfree(thinkpad_id.model_str);
9088 static int __init thinkpad_acpi_module_init(void)
9090 int ret, i;
9092 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9094 /* Parameter checking */
9095 if (hotkey_report_mode > 2)
9096 return -EINVAL;
9098 /* Driver-level probe */
9100 ret = get_thinkpad_model_data(&thinkpad_id);
9101 if (ret) {
9102 printk(TPACPI_ERR
9103 "unable to get DMI data: %d\n", ret);
9104 thinkpad_acpi_module_exit();
9105 return ret;
9107 ret = probe_for_thinkpad();
9108 if (ret) {
9109 thinkpad_acpi_module_exit();
9110 return ret;
9113 /* Driver initialization */
9115 thinkpad_acpi_init_banner();
9116 tpacpi_check_outdated_fw();
9118 TPACPI_ACPIHANDLE_INIT(ecrd);
9119 TPACPI_ACPIHANDLE_INIT(ecwr);
9121 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9122 if (!tpacpi_wq) {
9123 thinkpad_acpi_module_exit();
9124 return -ENOMEM;
9127 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9128 if (!proc_dir) {
9129 printk(TPACPI_ERR
9130 "unable to create proc dir " TPACPI_PROC_DIR);
9131 thinkpad_acpi_module_exit();
9132 return -ENODEV;
9135 ret = platform_driver_register(&tpacpi_pdriver);
9136 if (ret) {
9137 printk(TPACPI_ERR
9138 "unable to register main platform driver\n");
9139 thinkpad_acpi_module_exit();
9140 return ret;
9142 tp_features.platform_drv_registered = 1;
9144 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9145 if (ret) {
9146 printk(TPACPI_ERR
9147 "unable to register hwmon platform driver\n");
9148 thinkpad_acpi_module_exit();
9149 return ret;
9151 tp_features.sensors_pdrv_registered = 1;
9153 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9154 if (!ret) {
9155 tp_features.platform_drv_attrs_registered = 1;
9156 ret = tpacpi_create_driver_attributes(
9157 &tpacpi_hwmon_pdriver.driver);
9159 if (ret) {
9160 printk(TPACPI_ERR
9161 "unable to create sysfs driver attributes\n");
9162 thinkpad_acpi_module_exit();
9163 return ret;
9165 tp_features.sensors_pdrv_attrs_registered = 1;
9168 /* Device initialization */
9169 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9170 NULL, 0);
9171 if (IS_ERR(tpacpi_pdev)) {
9172 ret = PTR_ERR(tpacpi_pdev);
9173 tpacpi_pdev = NULL;
9174 printk(TPACPI_ERR "unable to register platform device\n");
9175 thinkpad_acpi_module_exit();
9176 return ret;
9178 tpacpi_sensors_pdev = platform_device_register_simple(
9179 TPACPI_HWMON_DRVR_NAME,
9180 -1, NULL, 0);
9181 if (IS_ERR(tpacpi_sensors_pdev)) {
9182 ret = PTR_ERR(tpacpi_sensors_pdev);
9183 tpacpi_sensors_pdev = NULL;
9184 printk(TPACPI_ERR
9185 "unable to register hwmon platform device\n");
9186 thinkpad_acpi_module_exit();
9187 return ret;
9189 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9190 &dev_attr_thinkpad_acpi_pdev_name);
9191 if (ret) {
9192 printk(TPACPI_ERR
9193 "unable to create sysfs hwmon device attributes\n");
9194 thinkpad_acpi_module_exit();
9195 return ret;
9197 tp_features.sensors_pdev_attrs_registered = 1;
9198 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9199 if (IS_ERR(tpacpi_hwmon)) {
9200 ret = PTR_ERR(tpacpi_hwmon);
9201 tpacpi_hwmon = NULL;
9202 printk(TPACPI_ERR "unable to register hwmon device\n");
9203 thinkpad_acpi_module_exit();
9204 return ret;
9206 mutex_init(&tpacpi_inputdev_send_mutex);
9207 tpacpi_inputdev = input_allocate_device();
9208 if (!tpacpi_inputdev) {
9209 printk(TPACPI_ERR "unable to allocate input device\n");
9210 thinkpad_acpi_module_exit();
9211 return -ENOMEM;
9212 } else {
9213 /* Prepare input device, but don't register */
9214 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9215 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9216 tpacpi_inputdev->id.bustype = BUS_HOST;
9217 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9218 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9219 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9220 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9223 /* Init subdriver dependencies */
9224 tpacpi_detect_brightness_capabilities();
9226 /* Init subdrivers */
9227 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9228 ret = ibm_init(&ibms_init[i]);
9229 if (ret >= 0 && *ibms_init[i].param)
9230 ret = ibms_init[i].data->write(ibms_init[i].param);
9231 if (ret < 0) {
9232 thinkpad_acpi_module_exit();
9233 return ret;
9237 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9239 ret = input_register_device(tpacpi_inputdev);
9240 if (ret < 0) {
9241 printk(TPACPI_ERR "unable to register input device\n");
9242 thinkpad_acpi_module_exit();
9243 return ret;
9244 } else {
9245 tp_features.input_device_registered = 1;
9248 return 0;
9251 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9254 * This will autoload the driver in almost every ThinkPad
9255 * in widespread use.
9257 * Only _VERY_ old models, like the 240, 240x and 570 lack
9258 * the HKEY event interface.
9260 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9263 * DMI matching for module autoloading
9265 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9266 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9268 * Only models listed in thinkwiki will be supported, so add yours
9269 * if it is not there yet.
9271 #define IBM_BIOS_MODULE_ALIAS(__type) \
9272 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9274 /* Ancient thinkpad BIOSes have to be identified by
9275 * BIOS type or model number, and there are far less
9276 * BIOS types than model numbers... */
9277 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9279 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9280 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9281 MODULE_DESCRIPTION(TPACPI_DESC);
9282 MODULE_VERSION(TPACPI_VERSION);
9283 MODULE_LICENSE("GPL");
9285 module_init(thinkpad_acpi_module_init);
9286 module_exit(thinkpad_acpi_module_exit);