thinkpad-acpi: lock down size of hotkey keymap
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob97a037f46252231f5ebe018e8b4106f2e34693d4
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* Misc NVRAM-related */
125 enum {
126 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 /* ACPI HIDs */
130 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
191 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
192 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
194 /* Thermal events */
195 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
196 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
197 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
198 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
199 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
201 /* Misc */
202 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
205 /****************************************************************************
206 * Main driver
209 #define TPACPI_NAME "thinkpad"
210 #define TPACPI_DESC "ThinkPad ACPI Extras"
211 #define TPACPI_FILE TPACPI_NAME "_acpi"
212 #define TPACPI_URL "http://ibm-acpi.sf.net/"
213 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
215 #define TPACPI_PROC_DIR "ibm"
216 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
217 #define TPACPI_DRVR_NAME TPACPI_FILE
218 #define TPACPI_DRVR_SHORTNAME "tpacpi"
219 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
221 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
222 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
224 #define TPACPI_MAX_ACPI_ARGS 3
226 /* printk headers */
227 #define TPACPI_LOG TPACPI_FILE ": "
228 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231 #define TPACPI_ERR KERN_ERR TPACPI_LOG
232 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
233 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234 #define TPACPI_INFO KERN_INFO TPACPI_LOG
235 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_acpimode:1;
309 u32 bright_unkfw:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul;
377 static int tpacpi_wlsw_emulstate;
378 static int dbg_bluetoothemul;
379 static int tpacpi_bluetooth_emulstate;
380 static int dbg_wwanemul;
381 static int tpacpi_wwan_emulstate;
382 static int dbg_uwbemul;
383 static int tpacpi_uwb_emulstate;
384 #endif
387 /*************************************************************************
388 * Debugging helpers
391 #define dbg_printk(a_dbg_level, format, arg...) \
392 do { if (dbg_level & (a_dbg_level)) \
393 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
394 } while (0)
396 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
397 #define vdbg_printk dbg_printk
398 static const char *str_supported(int is_supported);
399 #else
400 #define vdbg_printk(a_dbg_level, format, arg...) \
401 do { } while (0)
402 #endif
404 static void tpacpi_log_usertask(const char * const what)
406 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
407 what, task_tgid_vnr(current));
410 #define tpacpi_disclose_usertask(what, format, arg...) \
411 do { \
412 if (unlikely( \
413 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(TPACPI_DEBUG "%s: PID %d: " format, \
416 what, task_tgid_vnr(current), ## arg); \
418 } while (0)
421 * Quirk handling helpers
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
426 * We use values well outside that range as specials.
429 #define TPACPI_MATCH_ANY 0xffffU
430 #define TPACPI_MATCH_UNKNOWN 0U
432 /* TPID('1', 'Y') == 0x5931 */
433 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
435 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
453 struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
471 * The match criteria is: vendor, ec and bios much match.
473 static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
486 qlist_size--;
487 qlist++;
489 return 0;
492 static inline bool __pure __init tpacpi_is_lenovo(void)
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497 static inline bool __pure __init tpacpi_is_ibm(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502 /****************************************************************************
503 ****************************************************************************
505 * ACPI Helpers and device model
507 ****************************************************************************
508 ****************************************************************************/
510 /*************************************************************************
511 * ACPI basic handles
514 static acpi_handle root_handle;
515 static acpi_handle ec_handle;
517 #define TPACPI_HANDLE(object, parent, paths...) \
518 static acpi_handle object##_handle; \
519 static const acpi_handle *object##_parent __initdata = \
520 &parent##_handle; \
521 static char *object##_paths[] __initdata = { paths }
523 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
526 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
527 /* T4x, X31, X40 */
528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
530 ); /* all others */
532 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
535 ); /* 570 */
537 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
538 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
539 "\\_SB.PCI0.VID0", /* 770e */
540 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
541 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
542 "\\_SB.PCI0.AGP.VID", /* all others */
543 ); /* R30, R31 */
546 /*************************************************************************
547 * ACPI helpers
550 static int acpi_evalf(acpi_handle handle,
551 void *res, char *method, char *fmt, ...)
553 char *fmt0 = fmt;
554 struct acpi_object_list params;
555 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
556 struct acpi_buffer result, *resultp;
557 union acpi_object out_obj;
558 acpi_status status;
559 va_list ap;
560 char res_type;
561 int success;
562 int quiet;
564 if (!*fmt) {
565 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
566 return 0;
569 if (*fmt == 'q') {
570 quiet = 1;
571 fmt++;
572 } else
573 quiet = 0;
575 res_type = *(fmt++);
577 params.count = 0;
578 params.pointer = &in_objs[0];
580 va_start(ap, fmt);
581 while (*fmt) {
582 char c = *(fmt++);
583 switch (c) {
584 case 'd': /* int */
585 in_objs[params.count].integer.value = va_arg(ap, int);
586 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
587 break;
588 /* add more types as needed */
589 default:
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", c);
592 return 0;
595 va_end(ap);
597 if (res_type != 'v') {
598 result.length = sizeof(out_obj);
599 result.pointer = &out_obj;
600 resultp = &result;
601 } else
602 resultp = NULL;
604 status = acpi_evaluate_object(handle, method, &params, resultp);
606 switch (res_type) {
607 case 'd': /* int */
608 success = (status == AE_OK &&
609 out_obj.type == ACPI_TYPE_INTEGER);
610 if (success && res)
611 *(int *)res = out_obj.integer.value;
612 break;
613 case 'v': /* void */
614 success = status == AE_OK;
615 break;
616 /* add more types as needed */
617 default:
618 printk(TPACPI_ERR "acpi_evalf() called "
619 "with invalid format character '%c'\n", res_type);
620 return 0;
623 if (!success && !quiet)
624 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
625 method, fmt0, acpi_format_exception(status));
627 return success;
630 static int acpi_ec_read(int i, u8 *p)
632 int v;
634 if (ecrd_handle) {
635 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
636 return 0;
637 *p = v;
638 } else {
639 if (ec_read(i, p) < 0)
640 return 0;
643 return 1;
646 static int acpi_ec_write(int i, u8 v)
648 if (ecwr_handle) {
649 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
650 return 0;
651 } else {
652 if (ec_write(i, v) < 0)
653 return 0;
656 return 1;
659 static int issue_thinkpad_cmos_command(int cmos_cmd)
661 if (!cmos_handle)
662 return -ENXIO;
664 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
665 return -EIO;
667 return 0;
670 /*************************************************************************
671 * ACPI device model
674 #define TPACPI_ACPIHANDLE_INIT(object) \
675 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
676 object##_paths, ARRAY_SIZE(object##_paths))
678 static void __init drv_acpi_handle_init(const char *name,
679 acpi_handle *handle, const acpi_handle parent,
680 char **paths, const int num_paths)
682 int i;
683 acpi_status status;
685 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
686 name);
688 for (i = 0; i < num_paths; i++) {
689 status = acpi_get_handle(parent, paths[i], handle);
690 if (ACPI_SUCCESS(status)) {
691 dbg_printk(TPACPI_DBG_INIT,
692 "Found ACPI handle %s for %s\n",
693 paths[i], name);
694 return;
698 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
699 name);
700 *handle = NULL;
703 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
704 u32 level, void *context, void **return_value)
706 *(acpi_handle *)return_value = handle;
708 return AE_CTRL_TERMINATE;
711 static void __init tpacpi_acpi_handle_locate(const char *name,
712 const char *hid,
713 acpi_handle *handle)
715 acpi_status status;
716 acpi_handle device_found;
718 BUG_ON(!name || !hid || !handle);
719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
721 name, hid);
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
727 *handle = NULL;
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
733 } else {
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
740 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
742 struct ibm_struct *ibm = data;
744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 return;
747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
748 return;
750 ibm->acpi->notify(ibm, event);
753 static int __init setup_acpi_notify(struct ibm_struct *ibm)
755 acpi_status status;
756 int rc;
758 BUG_ON(!ibm->acpi);
760 if (!*ibm->acpi->handle)
761 return 0;
763 vdbg_printk(TPACPI_DBG_INIT,
764 "setting up ACPI notify for %s\n", ibm->name);
766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
767 if (rc < 0) {
768 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
769 ibm->name, rc);
770 return -ENODEV;
773 ibm->acpi->device->driver_data = ibm;
774 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
775 TPACPI_ACPI_EVENT_PREFIX,
776 ibm->name);
778 status = acpi_install_notify_handler(*ibm->acpi->handle,
779 ibm->acpi->type, dispatch_acpi_notify, ibm);
780 if (ACPI_FAILURE(status)) {
781 if (status == AE_ALREADY_EXISTS) {
782 printk(TPACPI_NOTICE
783 "another device driver is already "
784 "handling %s events\n", ibm->name);
785 } else {
786 printk(TPACPI_ERR
787 "acpi_install_notify_handler(%s) failed: %s\n",
788 ibm->name, acpi_format_exception(status));
790 return -ENODEV;
792 ibm->flags.acpi_notify_installed = 1;
793 return 0;
796 static int __init tpacpi_device_add(struct acpi_device *device)
798 return 0;
801 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
803 int rc;
805 dbg_printk(TPACPI_DBG_INIT,
806 "registering %s as an ACPI driver\n", ibm->name);
808 BUG_ON(!ibm->acpi);
810 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
811 if (!ibm->acpi->driver) {
812 printk(TPACPI_ERR
813 "failed to allocate memory for ibm->acpi->driver\n");
814 return -ENOMEM;
817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
818 ibm->acpi->driver->ids = ibm->acpi->hid;
820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
822 rc = acpi_bus_register_driver(ibm->acpi->driver);
823 if (rc < 0) {
824 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
825 ibm->name, rc);
826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
828 } else if (!rc)
829 ibm->flags.acpi_driver_registered = 1;
831 return rc;
835 /****************************************************************************
836 ****************************************************************************
838 * Procfs Helpers
840 ****************************************************************************
841 ****************************************************************************/
843 static int dispatch_proc_show(struct seq_file *m, void *v)
845 struct ibm_struct *ibm = m->private;
847 if (!ibm || !ibm->read)
848 return -EINVAL;
849 return ibm->read(m);
852 static int dispatch_proc_open(struct inode *inode, struct file *file)
854 return single_open(file, dispatch_proc_show, PDE(inode)->data);
857 static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
861 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
862 char *kernbuf;
863 int ret;
865 if (!ibm || !ibm->write)
866 return -EINVAL;
867 if (count > PAGE_SIZE - 2)
868 return -EINVAL;
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
871 if (!kernbuf)
872 return -ENOMEM;
874 if (copy_from_user(kernbuf, userbuf, count)) {
875 kfree(kernbuf);
876 return -EFAULT;
879 kernbuf[count] = 0;
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
882 if (ret == 0)
883 ret = count;
885 kfree(kernbuf);
887 return ret;
890 static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = single_release,
896 .write = dispatch_proc_write,
899 static char *next_cmd(char **cmds)
901 char *start = *cmds;
902 char *end;
904 while ((end = strchr(start, ',')) && end == start)
905 start = end + 1;
907 if (!end)
908 return NULL;
910 *end = 0;
911 *cmds = end + 1;
912 return start;
916 /****************************************************************************
917 ****************************************************************************
919 * Device model: input, hwmon and platform
921 ****************************************************************************
922 ****************************************************************************/
924 static struct platform_device *tpacpi_pdev;
925 static struct platform_device *tpacpi_sensors_pdev;
926 static struct device *tpacpi_hwmon;
927 static struct input_dev *tpacpi_inputdev;
928 static struct mutex tpacpi_inputdev_send_mutex;
929 static LIST_HEAD(tpacpi_all_drivers);
931 static int tpacpi_suspend_handler(struct platform_device *pdev,
932 pm_message_t state)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->suspend)
940 (ibm->suspend)(state);
943 return 0;
946 static int tpacpi_resume_handler(struct platform_device *pdev)
948 struct ibm_struct *ibm, *itmp;
950 list_for_each_entry_safe(ibm, itmp,
951 &tpacpi_all_drivers,
952 all_drivers) {
953 if (ibm->resume)
954 (ibm->resume)();
957 return 0;
960 static void tpacpi_shutdown_handler(struct platform_device *pdev)
962 struct ibm_struct *ibm, *itmp;
964 list_for_each_entry_safe(ibm, itmp,
965 &tpacpi_all_drivers,
966 all_drivers) {
967 if (ibm->shutdown)
968 (ibm->shutdown)();
972 static struct platform_driver tpacpi_pdriver = {
973 .driver = {
974 .name = TPACPI_DRVR_NAME,
975 .owner = THIS_MODULE,
977 .suspend = tpacpi_suspend_handler,
978 .resume = tpacpi_resume_handler,
979 .shutdown = tpacpi_shutdown_handler,
982 static struct platform_driver tpacpi_hwmon_pdriver = {
983 .driver = {
984 .name = TPACPI_HWMON_DRVR_NAME,
985 .owner = THIS_MODULE,
989 /*************************************************************************
990 * sysfs support helpers
993 struct attribute_set {
994 unsigned int members, max_members;
995 struct attribute_group group;
998 struct attribute_set_obj {
999 struct attribute_set s;
1000 struct attribute *a;
1001 } __attribute__((packed));
1003 static struct attribute_set *create_attr_set(unsigned int max_members,
1004 const char *name)
1006 struct attribute_set_obj *sobj;
1008 if (max_members == 0)
1009 return NULL;
1011 /* Allocates space for implicit NULL at the end too */
1012 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1013 max_members * sizeof(struct attribute *),
1014 GFP_KERNEL);
1015 if (!sobj)
1016 return NULL;
1017 sobj->s.max_members = max_members;
1018 sobj->s.group.attrs = &sobj->a;
1019 sobj->s.group.name = name;
1021 return &sobj->s;
1024 #define destroy_attr_set(_set) \
1025 kfree(_set);
1027 /* not multi-threaded safe, use it in a single thread per set */
1028 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1030 if (!s || !attr)
1031 return -EINVAL;
1033 if (s->members >= s->max_members)
1034 return -ENOMEM;
1036 s->group.attrs[s->members] = attr;
1037 s->members++;
1039 return 0;
1042 static int add_many_to_attr_set(struct attribute_set *s,
1043 struct attribute **attr,
1044 unsigned int count)
1046 int i, res;
1048 for (i = 0; i < count; i++) {
1049 res = add_to_attr_set(s, attr[i]);
1050 if (res)
1051 return res;
1054 return 0;
1057 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1059 sysfs_remove_group(kobj, &s->group);
1060 destroy_attr_set(s);
1063 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1064 sysfs_create_group(_kobj, &_attr_set->group)
1066 static int parse_strtoul(const char *buf,
1067 unsigned long max, unsigned long *value)
1069 char *endp;
1071 while (*buf && isspace(*buf))
1072 buf++;
1073 *value = simple_strtoul(buf, &endp, 0);
1074 while (*endp && isspace(*endp))
1075 endp++;
1076 if (*endp || *value > max)
1077 return -EINVAL;
1079 return 0;
1082 static void tpacpi_disable_brightness_delay(void)
1084 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1085 printk(TPACPI_NOTICE
1086 "ACPI backlight control delay disabled\n");
1089 static void printk_deprecated_attribute(const char * const what,
1090 const char * const details)
1092 tpacpi_log_usertask("deprecated sysfs attribute");
1093 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1094 "will be removed. %s\n",
1095 what, details);
1098 /*************************************************************************
1099 * rfkill and radio control support helpers
1103 * ThinkPad-ACPI firmware handling model:
1105 * WLSW (master wireless switch) is event-driven, and is common to all
1106 * firmware-controlled radios. It cannot be controlled, just monitored,
1107 * as expected. It overrides all radio state in firmware
1109 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1110 * (TODO: verify how WLSW interacts with the returned radio state).
1112 * The only time there are shadow radio state changes, is when
1113 * masked-off hotkeys are used.
1117 * Internal driver API for radio state:
1119 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1120 * bool: true means radio blocked (off)
1122 enum tpacpi_rfkill_state {
1123 TPACPI_RFK_RADIO_OFF = 0,
1124 TPACPI_RFK_RADIO_ON
1127 /* rfkill switches */
1128 enum tpacpi_rfk_id {
1129 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1130 TPACPI_RFK_WWAN_SW_ID,
1131 TPACPI_RFK_UWB_SW_ID,
1132 TPACPI_RFK_SW_MAX
1135 static const char *tpacpi_rfkill_names[] = {
1136 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1137 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1138 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1139 [TPACPI_RFK_SW_MAX] = NULL
1142 /* ThinkPad-ACPI rfkill subdriver */
1143 struct tpacpi_rfk {
1144 struct rfkill *rfkill;
1145 enum tpacpi_rfk_id id;
1146 const struct tpacpi_rfk_ops *ops;
1149 struct tpacpi_rfk_ops {
1150 /* firmware interface */
1151 int (*get_status)(void);
1152 int (*set_status)(const enum tpacpi_rfkill_state);
1155 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1157 /* Query FW and update rfkill sw state for a given rfkill switch */
1158 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1160 int status;
1162 if (!tp_rfk)
1163 return -ENODEV;
1165 status = (tp_rfk->ops->get_status)();
1166 if (status < 0)
1167 return status;
1169 rfkill_set_sw_state(tp_rfk->rfkill,
1170 (status == TPACPI_RFK_RADIO_OFF));
1172 return status;
1175 /* Query FW and update rfkill sw state for all rfkill switches */
1176 static void tpacpi_rfk_update_swstate_all(void)
1178 unsigned int i;
1180 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1181 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1185 * Sync the HW-blocking state of all rfkill switches,
1186 * do notice it causes the rfkill core to schedule uevents
1188 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1190 unsigned int i;
1191 struct tpacpi_rfk *tp_rfk;
1193 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1194 tp_rfk = tpacpi_rfkill_switches[i];
1195 if (tp_rfk) {
1196 if (rfkill_set_hw_state(tp_rfk->rfkill,
1197 blocked)) {
1198 /* ignore -- we track sw block */
1204 /* Call to get the WLSW state from the firmware */
1205 static int hotkey_get_wlsw(void);
1207 /* Call to query WLSW state and update all rfkill switches */
1208 static bool tpacpi_rfk_check_hwblock_state(void)
1210 int res = hotkey_get_wlsw();
1211 int hw_blocked;
1213 /* When unknown or unsupported, we have to assume it is unblocked */
1214 if (res < 0)
1215 return false;
1217 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1218 tpacpi_rfk_update_hwblock_state(hw_blocked);
1220 return hw_blocked;
1223 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1225 struct tpacpi_rfk *tp_rfk = data;
1226 int res;
1228 dbg_printk(TPACPI_DBG_RFKILL,
1229 "request to change radio state to %s\n",
1230 blocked ? "blocked" : "unblocked");
1232 /* try to set radio state */
1233 res = (tp_rfk->ops->set_status)(blocked ?
1234 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1236 /* and update the rfkill core with whatever the FW really did */
1237 tpacpi_rfk_update_swstate(tp_rfk);
1239 return (res < 0) ? res : 0;
1242 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1243 .set_block = tpacpi_rfk_hook_set_block,
1246 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1247 const struct tpacpi_rfk_ops *tp_rfkops,
1248 const enum rfkill_type rfktype,
1249 const char *name,
1250 const bool set_default)
1252 struct tpacpi_rfk *atp_rfk;
1253 int res;
1254 bool sw_state = false;
1255 bool hw_state;
1256 int sw_status;
1258 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1260 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1261 if (atp_rfk)
1262 atp_rfk->rfkill = rfkill_alloc(name,
1263 &tpacpi_pdev->dev,
1264 rfktype,
1265 &tpacpi_rfk_rfkill_ops,
1266 atp_rfk);
1267 if (!atp_rfk || !atp_rfk->rfkill) {
1268 printk(TPACPI_ERR
1269 "failed to allocate memory for rfkill class\n");
1270 kfree(atp_rfk);
1271 return -ENOMEM;
1274 atp_rfk->id = id;
1275 atp_rfk->ops = tp_rfkops;
1277 sw_status = (tp_rfkops->get_status)();
1278 if (sw_status < 0) {
1279 printk(TPACPI_ERR
1280 "failed to read initial state for %s, error %d\n",
1281 name, sw_status);
1282 } else {
1283 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1284 if (set_default) {
1285 /* try to keep the initial state, since we ask the
1286 * firmware to preserve it across S5 in NVRAM */
1287 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1290 hw_state = tpacpi_rfk_check_hwblock_state();
1291 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1293 res = rfkill_register(atp_rfk->rfkill);
1294 if (res < 0) {
1295 printk(TPACPI_ERR
1296 "failed to register %s rfkill switch: %d\n",
1297 name, res);
1298 rfkill_destroy(atp_rfk->rfkill);
1299 kfree(atp_rfk);
1300 return res;
1303 tpacpi_rfkill_switches[id] = atp_rfk;
1305 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1306 name, (sw_state || hw_state) ? "" : "un");
1307 return 0;
1310 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312 struct tpacpi_rfk *tp_rfk;
1314 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316 tp_rfk = tpacpi_rfkill_switches[id];
1317 if (tp_rfk) {
1318 rfkill_unregister(tp_rfk->rfkill);
1319 rfkill_destroy(tp_rfk->rfkill);
1320 tpacpi_rfkill_switches[id] = NULL;
1321 kfree(tp_rfk);
1325 static void printk_deprecated_rfkill_attribute(const char * const what)
1327 printk_deprecated_attribute(what,
1328 "Please switch to generic rfkill before year 2010");
1331 /* sysfs <radio> enable ------------------------------------------------ */
1332 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1333 struct device_attribute *attr,
1334 char *buf)
1336 int status;
1338 printk_deprecated_rfkill_attribute(attr->attr.name);
1340 /* This is in the ABI... */
1341 if (tpacpi_rfk_check_hwblock_state()) {
1342 status = TPACPI_RFK_RADIO_OFF;
1343 } else {
1344 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1345 if (status < 0)
1346 return status;
1349 return snprintf(buf, PAGE_SIZE, "%d\n",
1350 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1353 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 const char *buf, size_t count)
1357 unsigned long t;
1358 int res;
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1362 if (parse_strtoul(buf, 1, &t))
1363 return -EINVAL;
1365 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state() && !!t)
1369 return -EPERM;
1371 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1373 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375 return (res < 0) ? res : count;
1378 /* procfs -------------------------------------------------------------- */
1379 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1380 struct seq_file *m)
1382 if (id >= TPACPI_RFK_SW_MAX)
1383 seq_printf(m, "status:\t\tnot supported\n");
1384 else {
1385 int status;
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1397 seq_printf(m, "status:\t\t%s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
1400 seq_printf(m, "commands:\tenable, disable\n");
1403 return 0;
1406 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1433 return res;
1436 /*************************************************************************
1437 * thinkpad-acpi driver attributes
1440 /* interface_version --------------------------------------------------- */
1441 static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1448 static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1451 /* debug_level --------------------------------------------------------- */
1452 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1458 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1461 unsigned long t;
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1466 dbg_level = t;
1468 return count;
1471 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1474 /* version ------------------------------------------------------------- */
1475 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
1482 static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1485 /* --------------------------------------------------------------------- */
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1496 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1499 unsigned long t;
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1504 if (tpacpi_wlsw_emulstate != !!t) {
1505 tpacpi_wlsw_emulstate = !!t;
1506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1509 return count;
1512 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1516 /* bluetooth_emulstate ------------------------------------------------- */
1517 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1524 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1528 unsigned long t;
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1533 tpacpi_bluetooth_emulstate = !!t;
1535 return count;
1538 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1542 /* wwan_emulstate ------------------------------------------------- */
1543 static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1550 static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1554 unsigned long t;
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1559 tpacpi_wwan_emulstate = !!t;
1561 return count;
1564 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1568 /* uwb_emulstate ------------------------------------------------- */
1569 static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1576 static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1580 unsigned long t;
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1585 tpacpi_uwb_emulstate = !!t;
1587 return count;
1590 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
1593 #endif
1595 /* --------------------------------------------------------------------- */
1597 static struct driver_attribute *tpacpi_driver_attributes[] = {
1598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1602 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1604 int i, res;
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1613 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1622 #endif
1624 return res;
1627 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1629 int i;
1631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1634 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1639 #endif
1642 /*************************************************************************
1643 * Firmware Data
1647 * Table of recommended minimum BIOS versions
1649 * Reasons for listing:
1650 * 1. Stable BIOS, listed because the unknown amount of
1651 * bugs and bad ACPI behaviour on older versions
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1655 * 3. BIOS with known reduced functionality in older versions
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
1667 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1674 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1675 __eid, __ev1, __ev2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
1678 .ec = __eid, \
1679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1682 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1685 /* Outdated IBM BIOSes often lack the EC id string */
1686 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
1694 /* Outdated IBM BIOSes often lack the EC id string */
1695 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
1704 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1707 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
1712 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
1718 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1797 #undef TPV_QL1
1798 #undef TPV_QL0
1799 #undef TPV_QI2
1800 #undef TPV_QI1
1801 #undef TPV_QI0
1802 #undef TPV_Q_X
1803 #undef TPV_Q
1805 static void __init tpacpi_check_outdated_fw(void)
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1813 if (!fwvers)
1814 return;
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1830 printk(TPACPI_WARN
1831 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1832 printk(TPACPI_WARN
1833 "WARNING: This firmware may be missing critical bug "
1834 "fixes and/or important features\n");
1838 static bool __init tpacpi_is_fw_known(void)
1840 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1841 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1844 /****************************************************************************
1845 ****************************************************************************
1847 * Subdrivers
1849 ****************************************************************************
1850 ****************************************************************************/
1852 /*************************************************************************
1853 * thinkpad-acpi metadata subdriver
1856 static int thinkpad_acpi_driver_read(struct seq_file *m)
1858 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1859 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1860 return 0;
1863 static struct ibm_struct thinkpad_acpi_driver_data = {
1864 .name = "driver",
1865 .read = thinkpad_acpi_driver_read,
1868 /*************************************************************************
1869 * Hotkey subdriver
1873 * ThinkPad firmware event model
1875 * The ThinkPad firmware has two main event interfaces: normal ACPI
1876 * notifications (which follow the ACPI standard), and a private event
1877 * interface.
1879 * The private event interface also issues events for the hotkeys. As
1880 * the driver gained features, the event handling code ended up being
1881 * built around the hotkey subdriver. This will need to be refactored
1882 * to a more formal event API eventually.
1884 * Some "hotkeys" are actually supposed to be used as event reports,
1885 * such as "brightness has changed", "volume has changed", depending on
1886 * the ThinkPad model and how the firmware is operating.
1888 * Unlike other classes, hotkey-class events have mask/unmask control on
1889 * non-ancient firmware. However, how it behaves changes a lot with the
1890 * firmware model and version.
1893 enum { /* hot key scan codes (derived from ACPI DSDT) */
1894 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1895 TP_ACPI_HOTKEYSCAN_FNF2,
1896 TP_ACPI_HOTKEYSCAN_FNF3,
1897 TP_ACPI_HOTKEYSCAN_FNF4,
1898 TP_ACPI_HOTKEYSCAN_FNF5,
1899 TP_ACPI_HOTKEYSCAN_FNF6,
1900 TP_ACPI_HOTKEYSCAN_FNF7,
1901 TP_ACPI_HOTKEYSCAN_FNF8,
1902 TP_ACPI_HOTKEYSCAN_FNF9,
1903 TP_ACPI_HOTKEYSCAN_FNF10,
1904 TP_ACPI_HOTKEYSCAN_FNF11,
1905 TP_ACPI_HOTKEYSCAN_FNF12,
1906 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1907 TP_ACPI_HOTKEYSCAN_FNINSERT,
1908 TP_ACPI_HOTKEYSCAN_FNDELETE,
1909 TP_ACPI_HOTKEYSCAN_FNHOME,
1910 TP_ACPI_HOTKEYSCAN_FNEND,
1911 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1912 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1913 TP_ACPI_HOTKEYSCAN_FNSPACE,
1914 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1915 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1916 TP_ACPI_HOTKEYSCAN_MUTE,
1917 TP_ACPI_HOTKEYSCAN_THINKPAD,
1918 TP_ACPI_HOTKEYSCAN_UNK1,
1919 TP_ACPI_HOTKEYSCAN_UNK2,
1920 TP_ACPI_HOTKEYSCAN_UNK3,
1921 TP_ACPI_HOTKEYSCAN_UNK4,
1922 TP_ACPI_HOTKEYSCAN_UNK5,
1923 TP_ACPI_HOTKEYSCAN_UNK6,
1924 TP_ACPI_HOTKEYSCAN_UNK7,
1925 TP_ACPI_HOTKEYSCAN_UNK8,
1927 /* Hotkey keymap size */
1928 TPACPI_HOTKEY_MAP_LEN
1931 enum { /* Keys/events available through NVRAM polling */
1932 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1933 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1936 enum { /* Positions of some of the keys in hotkey masks */
1937 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1938 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1939 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1940 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1941 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1942 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1943 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1944 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1945 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1946 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1947 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1950 enum { /* NVRAM to ACPI HKEY group map */
1951 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1952 TP_ACPI_HKEY_ZOOM_MASK |
1953 TP_ACPI_HKEY_DISPSWTCH_MASK |
1954 TP_ACPI_HKEY_HIBERNATE_MASK,
1955 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1956 TP_ACPI_HKEY_BRGHTDWN_MASK,
1957 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1958 TP_ACPI_HKEY_VOLDWN_MASK |
1959 TP_ACPI_HKEY_MUTE_MASK,
1962 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1963 struct tp_nvram_state {
1964 u16 thinkpad_toggle:1;
1965 u16 zoom_toggle:1;
1966 u16 display_toggle:1;
1967 u16 thinklight_toggle:1;
1968 u16 hibernate_toggle:1;
1969 u16 displayexp_toggle:1;
1970 u16 display_state:1;
1971 u16 brightness_toggle:1;
1972 u16 volume_toggle:1;
1973 u16 mute:1;
1975 u8 brightness_level;
1976 u8 volume_level;
1979 /* kthread for the hotkey poller */
1980 static struct task_struct *tpacpi_hotkey_task;
1982 /* Acquired while the poller kthread is running, use to sync start/stop */
1983 static struct mutex hotkey_thread_mutex;
1986 * Acquire mutex to write poller control variables as an
1987 * atomic block.
1989 * Increment hotkey_config_change when changing them if you
1990 * want the kthread to forget old state.
1992 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1994 static struct mutex hotkey_thread_data_mutex;
1995 static unsigned int hotkey_config_change;
1998 * hotkey poller control variables
2000 * Must be atomic or readers will also need to acquire mutex
2002 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2003 * should be used only when the changes need to be taken as
2004 * a block, OR when one needs to force the kthread to forget
2005 * old state.
2007 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2008 static unsigned int hotkey_poll_freq = 10; /* Hz */
2010 #define HOTKEY_CONFIG_CRITICAL_START \
2011 do { \
2012 mutex_lock(&hotkey_thread_data_mutex); \
2013 hotkey_config_change++; \
2014 } while (0);
2015 #define HOTKEY_CONFIG_CRITICAL_END \
2016 mutex_unlock(&hotkey_thread_data_mutex);
2018 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2020 #define hotkey_source_mask 0U
2021 #define HOTKEY_CONFIG_CRITICAL_START
2022 #define HOTKEY_CONFIG_CRITICAL_END
2024 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2026 static struct mutex hotkey_mutex;
2028 static enum { /* Reasons for waking up */
2029 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2030 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2031 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2032 } hotkey_wakeup_reason;
2034 static int hotkey_autosleep_ack;
2036 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2037 static u32 hotkey_all_mask; /* all events supported in fw */
2038 static u32 hotkey_reserved_mask; /* events better left disabled */
2039 static u32 hotkey_driver_mask; /* events needed by the driver */
2040 static u32 hotkey_user_mask; /* events visible to userspace */
2041 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2043 static unsigned int hotkey_report_mode;
2045 static u16 *hotkey_keycode_map;
2047 static struct attribute_set *hotkey_dev_attributes;
2049 static void tpacpi_driver_event(const unsigned int hkey_event);
2050 static void hotkey_driver_event(const unsigned int scancode);
2051 static void hotkey_poll_setup(const bool may_warn);
2053 /* HKEY.MHKG() return bits */
2054 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2056 static int hotkey_get_wlsw(void)
2058 int status;
2060 if (!tp_features.hotkey_wlsw)
2061 return -ENODEV;
2063 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2064 if (dbg_wlswemul)
2065 return (tpacpi_wlsw_emulstate) ?
2066 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2067 #endif
2069 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2070 return -EIO;
2072 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2075 static int hotkey_get_tablet_mode(int *status)
2077 int s;
2079 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2080 return -EIO;
2082 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2083 return 0;
2087 * Reads current event mask from firmware, and updates
2088 * hotkey_acpi_mask accordingly. Also resets any bits
2089 * from hotkey_user_mask that are unavailable to be
2090 * delivered (shadow requirement of the userspace ABI).
2092 * Call with hotkey_mutex held
2094 static int hotkey_mask_get(void)
2096 if (tp_features.hotkey_mask) {
2097 u32 m = 0;
2099 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2100 return -EIO;
2102 hotkey_acpi_mask = m;
2103 } else {
2104 /* no mask support doesn't mean no event support... */
2105 hotkey_acpi_mask = hotkey_all_mask;
2108 /* sync userspace-visible mask */
2109 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2111 return 0;
2114 void static hotkey_mask_warn_incomplete_mask(void)
2116 /* log only what the user can fix... */
2117 const u32 wantedmask = hotkey_driver_mask &
2118 ~(hotkey_acpi_mask | hotkey_source_mask) &
2119 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2121 if (wantedmask)
2122 printk(TPACPI_NOTICE
2123 "required events 0x%08x not enabled!\n",
2124 wantedmask);
2128 * Set the firmware mask when supported
2130 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2132 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2134 * Call with hotkey_mutex held
2136 static int hotkey_mask_set(u32 mask)
2138 int i;
2139 int rc = 0;
2141 const u32 fwmask = mask & ~hotkey_source_mask;
2143 if (tp_features.hotkey_mask) {
2144 for (i = 0; i < 32; i++) {
2145 if (!acpi_evalf(hkey_handle,
2146 NULL, "MHKM", "vdd", i + 1,
2147 !!(mask & (1 << i)))) {
2148 rc = -EIO;
2149 break;
2155 * We *must* make an inconditional call to hotkey_mask_get to
2156 * refresh hotkey_acpi_mask and update hotkey_user_mask
2158 * Take the opportunity to also log when we cannot _enable_
2159 * a given event.
2161 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2162 printk(TPACPI_NOTICE
2163 "asked for hotkey mask 0x%08x, but "
2164 "firmware forced it to 0x%08x\n",
2165 fwmask, hotkey_acpi_mask);
2168 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2169 hotkey_mask_warn_incomplete_mask();
2171 return rc;
2175 * Sets hotkey_user_mask and tries to set the firmware mask
2177 * Call with hotkey_mutex held
2179 static int hotkey_user_mask_set(const u32 mask)
2181 int rc;
2183 /* Give people a chance to notice they are doing something that
2184 * is bound to go boom on their users sooner or later */
2185 if (!tp_warned.hotkey_mask_ff &&
2186 (mask == 0xffff || mask == 0xffffff ||
2187 mask == 0xffffffff)) {
2188 tp_warned.hotkey_mask_ff = 1;
2189 printk(TPACPI_NOTICE
2190 "setting the hotkey mask to 0x%08x is likely "
2191 "not the best way to go about it\n", mask);
2192 printk(TPACPI_NOTICE
2193 "please consider using the driver defaults, "
2194 "and refer to up-to-date thinkpad-acpi "
2195 "documentation\n");
2198 /* Try to enable what the user asked for, plus whatever we need.
2199 * this syncs everything but won't enable bits in hotkey_user_mask */
2200 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2202 /* Enable the available bits in hotkey_user_mask */
2203 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2205 return rc;
2209 * Sets the driver hotkey mask.
2211 * Can be called even if the hotkey subdriver is inactive
2213 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2215 int rc;
2217 /* Do the right thing if hotkey_init has not been called yet */
2218 if (!tp_features.hotkey) {
2219 hotkey_driver_mask = mask;
2220 return 0;
2223 mutex_lock(&hotkey_mutex);
2225 HOTKEY_CONFIG_CRITICAL_START
2226 hotkey_driver_mask = mask;
2227 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2228 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2229 #endif
2230 HOTKEY_CONFIG_CRITICAL_END
2232 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2233 ~hotkey_source_mask);
2234 hotkey_poll_setup(true);
2236 mutex_unlock(&hotkey_mutex);
2238 return rc;
2241 static int hotkey_status_get(int *status)
2243 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2244 return -EIO;
2246 return 0;
2249 static int hotkey_status_set(bool enable)
2251 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2252 return -EIO;
2254 return 0;
2257 static void tpacpi_input_send_tabletsw(void)
2259 int state;
2261 if (tp_features.hotkey_tablet &&
2262 !hotkey_get_tablet_mode(&state)) {
2263 mutex_lock(&tpacpi_inputdev_send_mutex);
2265 input_report_switch(tpacpi_inputdev,
2266 SW_TABLET_MODE, !!state);
2267 input_sync(tpacpi_inputdev);
2269 mutex_unlock(&tpacpi_inputdev_send_mutex);
2273 /* Do NOT call without validating scancode first */
2274 static void tpacpi_input_send_key(const unsigned int scancode)
2276 const unsigned int keycode = hotkey_keycode_map[scancode];
2278 if (keycode != KEY_RESERVED) {
2279 mutex_lock(&tpacpi_inputdev_send_mutex);
2281 input_report_key(tpacpi_inputdev, keycode, 1);
2282 if (keycode == KEY_UNKNOWN)
2283 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2284 scancode);
2285 input_sync(tpacpi_inputdev);
2287 input_report_key(tpacpi_inputdev, keycode, 0);
2288 if (keycode == KEY_UNKNOWN)
2289 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2290 scancode);
2291 input_sync(tpacpi_inputdev);
2293 mutex_unlock(&tpacpi_inputdev_send_mutex);
2297 /* Do NOT call without validating scancode first */
2298 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2300 hotkey_driver_event(scancode);
2301 if (hotkey_user_mask & (1 << scancode))
2302 tpacpi_input_send_key(scancode);
2305 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2306 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2308 /* Do NOT call without validating scancode first */
2309 static void tpacpi_hotkey_send_key(unsigned int scancode)
2311 tpacpi_input_send_key_masked(scancode);
2312 if (hotkey_report_mode < 2) {
2313 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2314 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2318 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2320 u8 d;
2322 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2323 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2324 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2325 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2326 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2327 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2329 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2331 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2333 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2334 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2335 n->displayexp_toggle =
2336 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2338 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2339 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2340 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2341 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2342 n->brightness_toggle =
2343 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2345 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2346 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2347 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2348 >> TP_NVRAM_POS_LEVEL_VOLUME;
2349 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2350 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2354 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2355 struct tp_nvram_state *newn,
2356 const u32 event_mask)
2359 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2360 do { \
2361 if ((event_mask & (1 << __scancode)) && \
2362 oldn->__member != newn->__member) \
2363 tpacpi_hotkey_send_key(__scancode); \
2364 } while (0)
2366 #define TPACPI_MAY_SEND_KEY(__scancode) \
2367 do { \
2368 if (event_mask & (1 << __scancode)) \
2369 tpacpi_hotkey_send_key(__scancode); \
2370 } while (0)
2372 void issue_volchange(const unsigned int oldvol,
2373 const unsigned int newvol)
2375 unsigned int i = oldvol;
2377 while (i > newvol) {
2378 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2379 i--;
2381 while (i < newvol) {
2382 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2383 i++;
2387 void issue_brightnesschange(const unsigned int oldbrt,
2388 const unsigned int newbrt)
2390 unsigned int i = oldbrt;
2392 while (i > newbrt) {
2393 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2394 i--;
2396 while (i < newbrt) {
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2398 i++;
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2412 * Handle volume
2414 * This code is supposed to duplicate the IBM firmware behaviour:
2415 * - Pressing MUTE issues mute hotkey message, even when already mute
2416 * - Pressing Volume up/down issues volume up/down hotkey messages,
2417 * even when already at maximum or minumum volume
2418 * - The act of unmuting issues volume up/down notification,
2419 * depending which key was used to unmute
2421 * We are constrained to what the NVRAM can tell us, which is not much
2422 * and certainly not enough if more than one volume hotkey was pressed
2423 * since the last poll cycle.
2425 * Just to make our life interesting, some newer Lenovo ThinkPads have
2426 * bugs in the BIOS and may fail to update volume_toggle properly.
2428 if (newn->mute) {
2429 /* muted */
2430 if (!oldn->mute ||
2431 oldn->volume_toggle != newn->volume_toggle ||
2432 oldn->volume_level != newn->volume_level) {
2433 /* recently muted, or repeated mute keypress, or
2434 * multiple presses ending in mute */
2435 issue_volchange(oldn->volume_level, newn->volume_level);
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2438 } else {
2439 /* unmute */
2440 if (oldn->mute) {
2441 /* recently unmuted, issue 'unmute' keypress */
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2444 if (oldn->volume_level != newn->volume_level) {
2445 issue_volchange(oldn->volume_level, newn->volume_level);
2446 } else if (oldn->volume_toggle != newn->volume_toggle) {
2447 /* repeated vol up/down keypress at end of scale ? */
2448 if (newn->volume_level == 0)
2449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2450 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2455 /* handle brightness */
2456 if (oldn->brightness_level != newn->brightness_level) {
2457 issue_brightnesschange(oldn->brightness_level,
2458 newn->brightness_level);
2459 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2460 /* repeated key presses that didn't change state */
2461 if (newn->brightness_level == 0)
2462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2463 else if (newn->brightness_level >= bright_maxlvl
2464 && !tp_features.bright_unkfw)
2465 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2468 #undef TPACPI_COMPARE_KEY
2469 #undef TPACPI_MAY_SEND_KEY
2473 * Polling driver
2475 * We track all events in hotkey_source_mask all the time, since
2476 * most of them are edge-based. We only issue those requested by
2477 * hotkey_user_mask or hotkey_driver_mask, though.
2479 static int hotkey_kthread(void *data)
2481 struct tp_nvram_state s[2];
2482 u32 poll_mask, event_mask;
2483 unsigned int si, so;
2484 unsigned long t;
2485 unsigned int change_detector, must_reset;
2486 unsigned int poll_freq;
2488 mutex_lock(&hotkey_thread_mutex);
2490 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2491 goto exit;
2493 set_freezable();
2495 so = 0;
2496 si = 1;
2497 t = 0;
2499 /* Initial state for compares */
2500 mutex_lock(&hotkey_thread_data_mutex);
2501 change_detector = hotkey_config_change;
2502 poll_mask = hotkey_source_mask;
2503 event_mask = hotkey_source_mask &
2504 (hotkey_driver_mask | hotkey_user_mask);
2505 poll_freq = hotkey_poll_freq;
2506 mutex_unlock(&hotkey_thread_data_mutex);
2507 hotkey_read_nvram(&s[so], poll_mask);
2509 while (!kthread_should_stop()) {
2510 if (t == 0) {
2511 if (likely(poll_freq))
2512 t = 1000/poll_freq;
2513 else
2514 t = 100; /* should never happen... */
2516 t = msleep_interruptible(t);
2517 if (unlikely(kthread_should_stop()))
2518 break;
2519 must_reset = try_to_freeze();
2520 if (t > 0 && !must_reset)
2521 continue;
2523 mutex_lock(&hotkey_thread_data_mutex);
2524 if (must_reset || hotkey_config_change != change_detector) {
2525 /* forget old state on thaw or config change */
2526 si = so;
2527 t = 0;
2528 change_detector = hotkey_config_change;
2530 poll_mask = hotkey_source_mask;
2531 event_mask = hotkey_source_mask &
2532 (hotkey_driver_mask | hotkey_user_mask);
2533 poll_freq = hotkey_poll_freq;
2534 mutex_unlock(&hotkey_thread_data_mutex);
2536 if (likely(poll_mask)) {
2537 hotkey_read_nvram(&s[si], poll_mask);
2538 if (likely(si != so)) {
2539 hotkey_compare_and_issue_event(&s[so], &s[si],
2540 event_mask);
2544 so = si;
2545 si ^= 1;
2548 exit:
2549 mutex_unlock(&hotkey_thread_mutex);
2550 return 0;
2553 /* call with hotkey_mutex held */
2554 static void hotkey_poll_stop_sync(void)
2556 if (tpacpi_hotkey_task) {
2557 if (frozen(tpacpi_hotkey_task) ||
2558 freezing(tpacpi_hotkey_task))
2559 thaw_process(tpacpi_hotkey_task);
2561 kthread_stop(tpacpi_hotkey_task);
2562 tpacpi_hotkey_task = NULL;
2563 mutex_lock(&hotkey_thread_mutex);
2564 /* at this point, the thread did exit */
2565 mutex_unlock(&hotkey_thread_mutex);
2569 /* call with hotkey_mutex held */
2570 static void hotkey_poll_setup(const bool may_warn)
2572 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2573 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2575 if (hotkey_poll_freq > 0 &&
2576 (poll_driver_mask ||
2577 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2578 if (!tpacpi_hotkey_task) {
2579 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2580 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2581 if (IS_ERR(tpacpi_hotkey_task)) {
2582 tpacpi_hotkey_task = NULL;
2583 printk(TPACPI_ERR
2584 "could not create kernel thread "
2585 "for hotkey polling\n");
2588 } else {
2589 hotkey_poll_stop_sync();
2590 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2591 hotkey_poll_freq == 0) {
2592 printk(TPACPI_NOTICE
2593 "hot keys 0x%08x and/or events 0x%08x "
2594 "require polling, which is currently "
2595 "disabled\n",
2596 poll_user_mask, poll_driver_mask);
2601 static void hotkey_poll_setup_safe(const bool may_warn)
2603 mutex_lock(&hotkey_mutex);
2604 hotkey_poll_setup(may_warn);
2605 mutex_unlock(&hotkey_mutex);
2608 /* call with hotkey_mutex held */
2609 static void hotkey_poll_set_freq(unsigned int freq)
2611 if (!freq)
2612 hotkey_poll_stop_sync();
2614 hotkey_poll_freq = freq;
2617 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2619 static void hotkey_poll_setup(const bool __unused)
2623 static void hotkey_poll_setup_safe(const bool __unused)
2627 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2629 static int hotkey_inputdev_open(struct input_dev *dev)
2631 switch (tpacpi_lifecycle) {
2632 case TPACPI_LIFE_INIT:
2633 case TPACPI_LIFE_RUNNING:
2634 hotkey_poll_setup_safe(false);
2635 return 0;
2636 case TPACPI_LIFE_EXITING:
2637 return -EBUSY;
2640 /* Should only happen if tpacpi_lifecycle is corrupt */
2641 BUG();
2642 return -EBUSY;
2645 static void hotkey_inputdev_close(struct input_dev *dev)
2647 /* disable hotkey polling when possible */
2648 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2649 !(hotkey_source_mask & hotkey_driver_mask))
2650 hotkey_poll_setup_safe(false);
2653 /* sysfs hotkey enable ------------------------------------------------- */
2654 static ssize_t hotkey_enable_show(struct device *dev,
2655 struct device_attribute *attr,
2656 char *buf)
2658 int res, status;
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkey reporting is always enabled");
2663 res = hotkey_status_get(&status);
2664 if (res)
2665 return res;
2667 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2670 static ssize_t hotkey_enable_store(struct device *dev,
2671 struct device_attribute *attr,
2672 const char *buf, size_t count)
2674 unsigned long t;
2676 printk_deprecated_attribute("hotkey_enable",
2677 "Hotkeys can be disabled through hotkey_mask");
2679 if (parse_strtoul(buf, 1, &t))
2680 return -EINVAL;
2682 if (t == 0)
2683 return -EPERM;
2685 return count;
2688 static struct device_attribute dev_attr_hotkey_enable =
2689 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2690 hotkey_enable_show, hotkey_enable_store);
2692 /* sysfs hotkey mask --------------------------------------------------- */
2693 static ssize_t hotkey_mask_show(struct device *dev,
2694 struct device_attribute *attr,
2695 char *buf)
2697 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2700 static ssize_t hotkey_mask_store(struct device *dev,
2701 struct device_attribute *attr,
2702 const char *buf, size_t count)
2704 unsigned long t;
2705 int res;
2707 if (parse_strtoul(buf, 0xffffffffUL, &t))
2708 return -EINVAL;
2710 if (mutex_lock_killable(&hotkey_mutex))
2711 return -ERESTARTSYS;
2713 res = hotkey_user_mask_set(t);
2715 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2716 hotkey_poll_setup(true);
2717 #endif
2719 mutex_unlock(&hotkey_mutex);
2721 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2723 return (res) ? res : count;
2726 static struct device_attribute dev_attr_hotkey_mask =
2727 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2728 hotkey_mask_show, hotkey_mask_store);
2730 /* sysfs hotkey bios_enabled ------------------------------------------- */
2731 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2732 struct device_attribute *attr,
2733 char *buf)
2735 return sprintf(buf, "0\n");
2738 static struct device_attribute dev_attr_hotkey_bios_enabled =
2739 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2741 /* sysfs hotkey bios_mask ---------------------------------------------- */
2742 static ssize_t hotkey_bios_mask_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2746 printk_deprecated_attribute("hotkey_bios_mask",
2747 "This attribute is useless.");
2748 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2751 static struct device_attribute dev_attr_hotkey_bios_mask =
2752 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2754 /* sysfs hotkey all_mask ----------------------------------------------- */
2755 static ssize_t hotkey_all_mask_show(struct device *dev,
2756 struct device_attribute *attr,
2757 char *buf)
2759 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2760 hotkey_all_mask | hotkey_source_mask);
2763 static struct device_attribute dev_attr_hotkey_all_mask =
2764 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2766 /* sysfs hotkey recommended_mask --------------------------------------- */
2767 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2772 (hotkey_all_mask | hotkey_source_mask)
2773 & ~hotkey_reserved_mask);
2776 static struct device_attribute dev_attr_hotkey_recommended_mask =
2777 __ATTR(hotkey_recommended_mask, S_IRUGO,
2778 hotkey_recommended_mask_show, NULL);
2780 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2782 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2783 static ssize_t hotkey_source_mask_show(struct device *dev,
2784 struct device_attribute *attr,
2785 char *buf)
2787 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2790 static ssize_t hotkey_source_mask_store(struct device *dev,
2791 struct device_attribute *attr,
2792 const char *buf, size_t count)
2794 unsigned long t;
2795 u32 r_ev;
2796 int rc;
2798 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2799 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2800 return -EINVAL;
2802 if (mutex_lock_killable(&hotkey_mutex))
2803 return -ERESTARTSYS;
2805 HOTKEY_CONFIG_CRITICAL_START
2806 hotkey_source_mask = t;
2807 HOTKEY_CONFIG_CRITICAL_END
2809 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2810 ~hotkey_source_mask);
2811 hotkey_poll_setup(true);
2813 /* check if events needed by the driver got disabled */
2814 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2815 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2817 mutex_unlock(&hotkey_mutex);
2819 if (rc < 0)
2820 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2821 "firmware event mask!\n");
2823 if (r_ev)
2824 printk(TPACPI_NOTICE "hotkey_source_mask: "
2825 "some important events were disabled: "
2826 "0x%04x\n", r_ev);
2828 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2830 return (rc < 0) ? rc : count;
2833 static struct device_attribute dev_attr_hotkey_source_mask =
2834 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2835 hotkey_source_mask_show, hotkey_source_mask_store);
2837 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2838 static ssize_t hotkey_poll_freq_show(struct device *dev,
2839 struct device_attribute *attr,
2840 char *buf)
2842 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2845 static ssize_t hotkey_poll_freq_store(struct device *dev,
2846 struct device_attribute *attr,
2847 const char *buf, size_t count)
2849 unsigned long t;
2851 if (parse_strtoul(buf, 25, &t))
2852 return -EINVAL;
2854 if (mutex_lock_killable(&hotkey_mutex))
2855 return -ERESTARTSYS;
2857 hotkey_poll_set_freq(t);
2858 hotkey_poll_setup(true);
2860 mutex_unlock(&hotkey_mutex);
2862 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2864 return count;
2867 static struct device_attribute dev_attr_hotkey_poll_freq =
2868 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2869 hotkey_poll_freq_show, hotkey_poll_freq_store);
2871 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2873 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2874 static ssize_t hotkey_radio_sw_show(struct device *dev,
2875 struct device_attribute *attr,
2876 char *buf)
2878 int res;
2879 res = hotkey_get_wlsw();
2880 if (res < 0)
2881 return res;
2883 /* Opportunistic update */
2884 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2886 return snprintf(buf, PAGE_SIZE, "%d\n",
2887 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2890 static struct device_attribute dev_attr_hotkey_radio_sw =
2891 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2893 static void hotkey_radio_sw_notify_change(void)
2895 if (tp_features.hotkey_wlsw)
2896 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2897 "hotkey_radio_sw");
2900 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2901 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2902 struct device_attribute *attr,
2903 char *buf)
2905 int res, s;
2906 res = hotkey_get_tablet_mode(&s);
2907 if (res < 0)
2908 return res;
2910 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2913 static struct device_attribute dev_attr_hotkey_tablet_mode =
2914 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2916 static void hotkey_tablet_mode_notify_change(void)
2918 if (tp_features.hotkey_tablet)
2919 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2920 "hotkey_tablet_mode");
2923 /* sysfs hotkey report_mode -------------------------------------------- */
2924 static ssize_t hotkey_report_mode_show(struct device *dev,
2925 struct device_attribute *attr,
2926 char *buf)
2928 return snprintf(buf, PAGE_SIZE, "%d\n",
2929 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2932 static struct device_attribute dev_attr_hotkey_report_mode =
2933 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2935 /* sysfs wakeup reason (pollable) -------------------------------------- */
2936 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2937 struct device_attribute *attr,
2938 char *buf)
2940 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2943 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2944 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2946 static void hotkey_wakeup_reason_notify_change(void)
2948 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2949 "wakeup_reason");
2952 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2953 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2954 struct device_attribute *attr,
2955 char *buf)
2957 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2960 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2961 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2962 hotkey_wakeup_hotunplug_complete_show, NULL);
2964 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2966 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2967 "wakeup_hotunplug_complete");
2970 /* --------------------------------------------------------------------- */
2972 static struct attribute *hotkey_attributes[] __initdata = {
2973 &dev_attr_hotkey_enable.attr,
2974 &dev_attr_hotkey_bios_enabled.attr,
2975 &dev_attr_hotkey_bios_mask.attr,
2976 &dev_attr_hotkey_report_mode.attr,
2977 &dev_attr_hotkey_wakeup_reason.attr,
2978 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2979 &dev_attr_hotkey_mask.attr,
2980 &dev_attr_hotkey_all_mask.attr,
2981 &dev_attr_hotkey_recommended_mask.attr,
2982 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2983 &dev_attr_hotkey_source_mask.attr,
2984 &dev_attr_hotkey_poll_freq.attr,
2985 #endif
2989 * Sync both the hw and sw blocking state of all switches
2991 static void tpacpi_send_radiosw_update(void)
2993 int wlsw;
2996 * We must sync all rfkill controllers *before* issuing any
2997 * rfkill input events, or we will race the rfkill core input
2998 * handler.
3000 * tpacpi_inputdev_send_mutex works as a syncronization point
3001 * for the above.
3003 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3006 wlsw = hotkey_get_wlsw();
3008 /* Sync hw blocking state first if it is hw-blocked */
3009 if (wlsw == TPACPI_RFK_RADIO_OFF)
3010 tpacpi_rfk_update_hwblock_state(true);
3012 /* Sync sw blocking state */
3013 tpacpi_rfk_update_swstate_all();
3015 /* Sync hw blocking state last if it is hw-unblocked */
3016 if (wlsw == TPACPI_RFK_RADIO_ON)
3017 tpacpi_rfk_update_hwblock_state(false);
3019 /* Issue rfkill input event for WLSW switch */
3020 if (!(wlsw < 0)) {
3021 mutex_lock(&tpacpi_inputdev_send_mutex);
3023 input_report_switch(tpacpi_inputdev,
3024 SW_RFKILL_ALL, (wlsw > 0));
3025 input_sync(tpacpi_inputdev);
3027 mutex_unlock(&tpacpi_inputdev_send_mutex);
3031 * this can be unconditional, as we will poll state again
3032 * if userspace uses the notify to read data
3034 hotkey_radio_sw_notify_change();
3037 static void hotkey_exit(void)
3039 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3040 mutex_lock(&hotkey_mutex);
3041 hotkey_poll_stop_sync();
3042 mutex_unlock(&hotkey_mutex);
3043 #endif
3045 if (hotkey_dev_attributes)
3046 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3048 kfree(hotkey_keycode_map);
3050 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3051 "restoring original HKEY status and mask\n");
3052 /* yes, there is a bitwise or below, we want the
3053 * functions to be called even if one of them fail */
3054 if (((tp_features.hotkey_mask &&
3055 hotkey_mask_set(hotkey_orig_mask)) |
3056 hotkey_status_set(false)) != 0)
3057 printk(TPACPI_ERR
3058 "failed to restore hot key mask "
3059 "to BIOS defaults\n");
3062 static void __init hotkey_unmap(const unsigned int scancode)
3064 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3065 clear_bit(hotkey_keycode_map[scancode],
3066 tpacpi_inputdev->keybit);
3067 hotkey_keycode_map[scancode] = KEY_RESERVED;
3072 * HKEY quirks:
3073 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3076 #define TPACPI_HK_Q_INIMASK 0x0001
3078 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3079 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3080 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3081 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3082 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3083 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3084 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3085 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3086 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3087 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3088 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3089 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3090 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3091 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3092 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3093 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3094 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3095 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3096 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3097 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3100 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.
3131 static u16 ibm_keycode_map[TPACPI_HOTKEY_MAP_LEN] __initdata = {
3132 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3133 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3134 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3135 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3137 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3138 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3139 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3140 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3142 /* brightness: firmware always reacts to them */
3143 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3144 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3146 /* Thinklight: firmware always react to it */
3147 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3149 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3150 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3152 /* Volume: firmware always react to it and reprograms
3153 * the built-in *extra* mixer. Never map it to control
3154 * another mixer by default. */
3155 KEY_RESERVED, /* 0x14: VOLUME UP */
3156 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3157 KEY_RESERVED, /* 0x16: MUTE */
3159 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3161 /* (assignments unknown, please report if found) */
3162 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3163 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3165 static u16 lenovo_keycode_map[TPACPI_HOTKEY_MAP_LEN] __initdata = {
3166 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3167 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3168 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3169 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3171 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3172 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3173 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3174 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3176 /* These should be enabled --only-- when ACPI video
3177 * is disabled (i.e. in "vendor" mode), and are handled
3178 * in a special way by the init code */
3179 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3180 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3182 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3184 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3185 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3187 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3188 * react to it and reprograms the built-in *extra* mixer.
3189 * Never map it to control another mixer by default.
3191 * T60?, T61, R60?, R61: firmware and EC tries to send
3192 * these over the regular keyboard, so these are no-ops,
3193 * but there are still weird bugs re. MUTE, so do not
3194 * change unless you get test reports from all Lenovo
3195 * models. May cause the BIOS to interfere with the
3196 * HDA mixer.
3198 KEY_RESERVED, /* 0x14: VOLUME UP */
3199 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3200 KEY_RESERVED, /* 0x16: MUTE */
3202 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3204 /* (assignments unknown, please report if found) */
3205 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3206 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3209 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3210 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3212 int res, i;
3213 int status;
3214 int hkeyv;
3215 bool radiosw_state = false;
3216 bool tabletsw_state = false;
3218 unsigned long quirks;
3220 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3221 "initializing hotkey subdriver\n");
3223 BUG_ON(!tpacpi_inputdev);
3224 BUG_ON(tpacpi_inputdev->open != NULL ||
3225 tpacpi_inputdev->close != NULL);
3227 TPACPI_ACPIHANDLE_INIT(hkey);
3228 mutex_init(&hotkey_mutex);
3230 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3231 mutex_init(&hotkey_thread_mutex);
3232 mutex_init(&hotkey_thread_data_mutex);
3233 #endif
3235 /* hotkey not supported on 570 */
3236 tp_features.hotkey = hkey_handle != NULL;
3238 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3239 "hotkeys are %s\n",
3240 str_supported(tp_features.hotkey));
3242 if (!tp_features.hotkey)
3243 return 1;
3245 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3246 ARRAY_SIZE(tpacpi_hotkey_qtable));
3248 tpacpi_disable_brightness_delay();
3250 /* MUST have enough space for all attributes to be added to
3251 * hotkey_dev_attributes */
3252 hotkey_dev_attributes = create_attr_set(
3253 ARRAY_SIZE(hotkey_attributes) + 2,
3254 NULL);
3255 if (!hotkey_dev_attributes)
3256 return -ENOMEM;
3257 res = add_many_to_attr_set(hotkey_dev_attributes,
3258 hotkey_attributes,
3259 ARRAY_SIZE(hotkey_attributes));
3260 if (res)
3261 goto err_exit;
3263 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3264 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3265 for HKEY interface version 0x100 */
3266 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3267 if ((hkeyv >> 8) != 1) {
3268 printk(TPACPI_ERR "unknown version of the "
3269 "HKEY interface: 0x%x\n", hkeyv);
3270 printk(TPACPI_ERR "please report this to %s\n",
3271 TPACPI_MAIL);
3272 } else {
3274 * MHKV 0x100 in A31, R40, R40e,
3275 * T4x, X31, and later
3277 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3278 "firmware HKEY interface version: 0x%x\n",
3279 hkeyv);
3281 /* Paranoia check AND init hotkey_all_mask */
3282 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3283 "MHKA", "qd")) {
3284 printk(TPACPI_ERR
3285 "missing MHKA handler, "
3286 "please report this to %s\n",
3287 TPACPI_MAIL);
3288 /* Fallback: pre-init for FN+F3,F4,F12 */
3289 hotkey_all_mask = 0x080cU;
3290 } else {
3291 tp_features.hotkey_mask = 1;
3296 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3297 "hotkey masks are %s\n",
3298 str_supported(tp_features.hotkey_mask));
3300 /* Init hotkey_all_mask if not initialized yet */
3301 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3302 (quirks & TPACPI_HK_Q_INIMASK))
3303 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3305 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3306 if (tp_features.hotkey_mask) {
3307 /* hotkey_source_mask *must* be zero for
3308 * the first hotkey_mask_get to return hotkey_orig_mask */
3309 res = hotkey_mask_get();
3310 if (res)
3311 goto err_exit;
3313 hotkey_orig_mask = hotkey_acpi_mask;
3314 } else {
3315 hotkey_orig_mask = hotkey_all_mask;
3316 hotkey_acpi_mask = hotkey_all_mask;
3319 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3320 if (dbg_wlswemul) {
3321 tp_features.hotkey_wlsw = 1;
3322 radiosw_state = !!tpacpi_wlsw_emulstate;
3323 printk(TPACPI_INFO
3324 "radio switch emulation enabled\n");
3325 } else
3326 #endif
3327 /* Not all thinkpads have a hardware radio switch */
3328 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3329 tp_features.hotkey_wlsw = 1;
3330 radiosw_state = !!status;
3331 printk(TPACPI_INFO
3332 "radio switch found; radios are %s\n",
3333 enabled(status, 0));
3335 if (tp_features.hotkey_wlsw)
3336 res = add_to_attr_set(hotkey_dev_attributes,
3337 &dev_attr_hotkey_radio_sw.attr);
3339 /* For X41t, X60t, X61t Tablets... */
3340 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3341 tp_features.hotkey_tablet = 1;
3342 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3343 printk(TPACPI_INFO
3344 "possible tablet mode switch found; "
3345 "ThinkPad in %s mode\n",
3346 (tabletsw_state) ? "tablet" : "laptop");
3347 res = add_to_attr_set(hotkey_dev_attributes,
3348 &dev_attr_hotkey_tablet_mode.attr);
3351 if (!res)
3352 res = register_attr_set_with_sysfs(
3353 hotkey_dev_attributes,
3354 &tpacpi_pdev->dev.kobj);
3355 if (res)
3356 goto err_exit;
3358 /* Set up key map */
3360 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3361 GFP_KERNEL);
3362 if (!hotkey_keycode_map) {
3363 printk(TPACPI_ERR
3364 "failed to allocate memory for key map\n");
3365 res = -ENOMEM;
3366 goto err_exit;
3369 if (tpacpi_is_lenovo()) {
3370 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3371 "using Lenovo default hot key map\n");
3372 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3373 TPACPI_HOTKEY_MAP_SIZE);
3374 } else {
3375 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3376 "using IBM default hot key map\n");
3377 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3378 TPACPI_HOTKEY_MAP_SIZE);
3381 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3382 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3383 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3384 tpacpi_inputdev->keycode = hotkey_keycode_map;
3385 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3386 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3387 input_set_capability(tpacpi_inputdev, EV_KEY,
3388 hotkey_keycode_map[i]);
3389 } else {
3390 if (i < sizeof(hotkey_reserved_mask)*8)
3391 hotkey_reserved_mask |= 1 << i;
3395 if (tp_features.hotkey_wlsw) {
3396 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3397 input_report_switch(tpacpi_inputdev,
3398 SW_RFKILL_ALL, radiosw_state);
3400 if (tp_features.hotkey_tablet) {
3401 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3402 input_report_switch(tpacpi_inputdev,
3403 SW_TABLET_MODE, tabletsw_state);
3406 /* Do not issue duplicate brightness change events to
3407 * userspace. tpacpi_detect_brightness_capabilities() must have
3408 * been called before this point */
3409 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3410 printk(TPACPI_INFO
3411 "This ThinkPad has standard ACPI backlight "
3412 "brightness control, supported by the ACPI "
3413 "video driver\n");
3414 printk(TPACPI_NOTICE
3415 "Disabling thinkpad-acpi brightness events "
3416 "by default...\n");
3418 /* Disable brightness up/down on Lenovo thinkpads when
3419 * ACPI is handling them, otherwise it is plain impossible
3420 * for userspace to do something even remotely sane */
3421 hotkey_reserved_mask |=
3422 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3423 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3424 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3425 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3428 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3429 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3430 & ~hotkey_all_mask
3431 & ~hotkey_reserved_mask;
3433 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3434 "hotkey source mask 0x%08x, polling freq %u\n",
3435 hotkey_source_mask, hotkey_poll_freq);
3436 #endif
3438 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3439 "enabling firmware HKEY event interface...\n");
3440 res = hotkey_status_set(true);
3441 if (res) {
3442 hotkey_exit();
3443 return res;
3445 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3446 | hotkey_driver_mask)
3447 & ~hotkey_source_mask);
3448 if (res < 0 && res != -ENXIO) {
3449 hotkey_exit();
3450 return res;
3452 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3453 & ~hotkey_reserved_mask;
3454 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3455 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3456 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3458 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3459 "legacy ibm/hotkey event reporting over procfs %s\n",
3460 (hotkey_report_mode < 2) ?
3461 "enabled" : "disabled");
3463 tpacpi_inputdev->open = &hotkey_inputdev_open;
3464 tpacpi_inputdev->close = &hotkey_inputdev_close;
3466 hotkey_poll_setup_safe(true);
3468 return 0;
3470 err_exit:
3471 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3472 hotkey_dev_attributes = NULL;
3474 return (res < 0)? res : 1;
3477 static bool hotkey_notify_hotkey(const u32 hkey,
3478 bool *send_acpi_ev,
3479 bool *ignore_acpi_ev)
3481 /* 0x1000-0x1FFF: key presses */
3482 unsigned int scancode = hkey & 0xfff;
3483 *send_acpi_ev = true;
3484 *ignore_acpi_ev = false;
3486 /* HKEY event 0x1001 is scancode 0x00 */
3487 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3488 scancode--;
3489 if (!(hotkey_source_mask & (1 << scancode))) {
3490 tpacpi_input_send_key_masked(scancode);
3491 *send_acpi_ev = false;
3492 } else {
3493 *ignore_acpi_ev = true;
3495 return true;
3497 return false;
3500 static bool hotkey_notify_wakeup(const u32 hkey,
3501 bool *send_acpi_ev,
3502 bool *ignore_acpi_ev)
3504 /* 0x2000-0x2FFF: Wakeup reason */
3505 *send_acpi_ev = true;
3506 *ignore_acpi_ev = false;
3508 switch (hkey) {
3509 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3510 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3511 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3512 *ignore_acpi_ev = true;
3513 break;
3515 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3516 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3517 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3518 *ignore_acpi_ev = true;
3519 break;
3521 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3522 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3523 printk(TPACPI_ALERT
3524 "EMERGENCY WAKEUP: battery almost empty\n");
3525 /* how to auto-heal: */
3526 /* 2313: woke up from S3, go to S4/S5 */
3527 /* 2413: woke up from S4, go to S5 */
3528 break;
3530 default:
3531 return false;
3534 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3535 printk(TPACPI_INFO
3536 "woke up due to a hot-unplug "
3537 "request...\n");
3538 hotkey_wakeup_reason_notify_change();
3540 return true;
3543 static bool hotkey_notify_usrevent(const u32 hkey,
3544 bool *send_acpi_ev,
3545 bool *ignore_acpi_ev)
3547 /* 0x5000-0x5FFF: human interface helpers */
3548 *send_acpi_ev = true;
3549 *ignore_acpi_ev = false;
3551 switch (hkey) {
3552 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3553 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3554 return true;
3556 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3557 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3558 tpacpi_input_send_tabletsw();
3559 hotkey_tablet_mode_notify_change();
3560 *send_acpi_ev = false;
3561 return true;
3563 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3564 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3565 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3566 /* do not propagate these events */
3567 *ignore_acpi_ev = true;
3568 return true;
3570 default:
3571 return false;
3575 static void thermal_dump_all_sensors(void);
3577 static bool hotkey_notify_thermal(const u32 hkey,
3578 bool *send_acpi_ev,
3579 bool *ignore_acpi_ev)
3581 bool known = true;
3583 /* 0x6000-0x6FFF: thermal alarms */
3584 *send_acpi_ev = true;
3585 *ignore_acpi_ev = false;
3587 switch (hkey) {
3588 case TP_HKEY_EV_THM_TABLE_CHANGED:
3589 printk(TPACPI_INFO
3590 "EC reports that Thermal Table has changed\n");
3591 /* recommended action: do nothing, we don't have
3592 * Lenovo ATM information */
3593 return true;
3594 case TP_HKEY_EV_ALARM_BAT_HOT:
3595 printk(TPACPI_CRIT
3596 "THERMAL ALARM: battery is too hot!\n");
3597 /* recommended action: warn user through gui */
3598 break;
3599 case TP_HKEY_EV_ALARM_BAT_XHOT:
3600 printk(TPACPI_ALERT
3601 "THERMAL EMERGENCY: battery is extremely hot!\n");
3602 /* recommended action: immediate sleep/hibernate */
3603 break;
3604 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3605 printk(TPACPI_CRIT
3606 "THERMAL ALARM: "
3607 "a sensor reports something is too hot!\n");
3608 /* recommended action: warn user through gui, that */
3609 /* some internal component is too hot */
3610 break;
3611 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3612 printk(TPACPI_ALERT
3613 "THERMAL EMERGENCY: "
3614 "a sensor reports something is extremely hot!\n");
3615 /* recommended action: immediate sleep/hibernate */
3616 break;
3617 default:
3618 printk(TPACPI_ALERT
3619 "THERMAL ALERT: unknown thermal alarm received\n");
3620 known = false;
3623 thermal_dump_all_sensors();
3625 return known;
3628 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3630 u32 hkey;
3631 bool send_acpi_ev;
3632 bool ignore_acpi_ev;
3633 bool known_ev;
3635 if (event != 0x80) {
3636 printk(TPACPI_ERR
3637 "unknown HKEY notification event %d\n", event);
3638 /* forward it to userspace, maybe it knows how to handle it */
3639 acpi_bus_generate_netlink_event(
3640 ibm->acpi->device->pnp.device_class,
3641 dev_name(&ibm->acpi->device->dev),
3642 event, 0);
3643 return;
3646 while (1) {
3647 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3648 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3649 return;
3652 if (hkey == 0) {
3653 /* queue empty */
3654 return;
3657 send_acpi_ev = true;
3658 ignore_acpi_ev = false;
3660 switch (hkey >> 12) {
3661 case 1:
3662 /* 0x1000-0x1FFF: key presses */
3663 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3664 &ignore_acpi_ev);
3665 break;
3666 case 2:
3667 /* 0x2000-0x2FFF: Wakeup reason */
3668 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3669 &ignore_acpi_ev);
3670 break;
3671 case 3:
3672 /* 0x3000-0x3FFF: bay-related wakeups */
3673 switch (hkey) {
3674 case TP_HKEY_EV_BAYEJ_ACK:
3675 hotkey_autosleep_ack = 1;
3676 printk(TPACPI_INFO
3677 "bay ejected\n");
3678 hotkey_wakeup_hotunplug_complete_notify_change();
3679 known_ev = true;
3680 break;
3681 case TP_HKEY_EV_OPTDRV_EJ:
3682 /* FIXME: kick libata if SATA link offline */
3683 known_ev = true;
3684 break;
3685 default:
3686 known_ev = false;
3688 break;
3689 case 4:
3690 /* 0x4000-0x4FFF: dock-related wakeups */
3691 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3692 hotkey_autosleep_ack = 1;
3693 printk(TPACPI_INFO
3694 "undocked\n");
3695 hotkey_wakeup_hotunplug_complete_notify_change();
3696 known_ev = true;
3697 } else {
3698 known_ev = false;
3700 break;
3701 case 5:
3702 /* 0x5000-0x5FFF: human interface helpers */
3703 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3704 &ignore_acpi_ev);
3705 break;
3706 case 6:
3707 /* 0x6000-0x6FFF: thermal alarms */
3708 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3709 &ignore_acpi_ev);
3710 break;
3711 case 7:
3712 /* 0x7000-0x7FFF: misc */
3713 if (tp_features.hotkey_wlsw &&
3714 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3715 tpacpi_send_radiosw_update();
3716 send_acpi_ev = 0;
3717 known_ev = true;
3718 break;
3720 /* fallthrough to default */
3721 default:
3722 known_ev = false;
3724 if (!known_ev) {
3725 printk(TPACPI_NOTICE
3726 "unhandled HKEY event 0x%04x\n", hkey);
3727 printk(TPACPI_NOTICE
3728 "please report the conditions when this "
3729 "event happened to %s\n", TPACPI_MAIL);
3732 /* Legacy events */
3733 if (!ignore_acpi_ev &&
3734 (send_acpi_ev || hotkey_report_mode < 2)) {
3735 acpi_bus_generate_proc_event(ibm->acpi->device,
3736 event, hkey);
3739 /* netlink events */
3740 if (!ignore_acpi_ev && send_acpi_ev) {
3741 acpi_bus_generate_netlink_event(
3742 ibm->acpi->device->pnp.device_class,
3743 dev_name(&ibm->acpi->device->dev),
3744 event, hkey);
3749 static void hotkey_suspend(pm_message_t state)
3751 /* Do these on suspend, we get the events on early resume! */
3752 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3753 hotkey_autosleep_ack = 0;
3756 static void hotkey_resume(void)
3758 tpacpi_disable_brightness_delay();
3760 if (hotkey_status_set(true) < 0 ||
3761 hotkey_mask_set(hotkey_acpi_mask) < 0)
3762 printk(TPACPI_ERR
3763 "error while attempting to reset the event "
3764 "firmware interface\n");
3766 tpacpi_send_radiosw_update();
3767 hotkey_tablet_mode_notify_change();
3768 hotkey_wakeup_reason_notify_change();
3769 hotkey_wakeup_hotunplug_complete_notify_change();
3770 hotkey_poll_setup_safe(false);
3773 /* procfs -------------------------------------------------------------- */
3774 static int hotkey_read(struct seq_file *m)
3776 int res, status;
3778 if (!tp_features.hotkey) {
3779 seq_printf(m, "status:\t\tnot supported\n");
3780 return 0;
3783 if (mutex_lock_killable(&hotkey_mutex))
3784 return -ERESTARTSYS;
3785 res = hotkey_status_get(&status);
3786 if (!res)
3787 res = hotkey_mask_get();
3788 mutex_unlock(&hotkey_mutex);
3789 if (res)
3790 return res;
3792 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3793 if (hotkey_all_mask) {
3794 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3795 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3796 } else {
3797 seq_printf(m, "mask:\t\tnot supported\n");
3798 seq_printf(m, "commands:\tenable, disable, reset\n");
3801 return 0;
3804 static void hotkey_enabledisable_warn(bool enable)
3806 tpacpi_log_usertask("procfs hotkey enable/disable");
3807 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3808 TPACPI_WARN
3809 "hotkey enable/disable functionality has been "
3810 "removed from the driver. Hotkeys are always "
3811 "enabled\n"))
3812 printk(TPACPI_ERR
3813 "Please remove the hotkey=enable module "
3814 "parameter, it is deprecated. Hotkeys are always "
3815 "enabled\n");
3818 static int hotkey_write(char *buf)
3820 int res;
3821 u32 mask;
3822 char *cmd;
3824 if (!tp_features.hotkey)
3825 return -ENODEV;
3827 if (mutex_lock_killable(&hotkey_mutex))
3828 return -ERESTARTSYS;
3830 mask = hotkey_user_mask;
3832 res = 0;
3833 while ((cmd = next_cmd(&buf))) {
3834 if (strlencmp(cmd, "enable") == 0) {
3835 hotkey_enabledisable_warn(1);
3836 } else if (strlencmp(cmd, "disable") == 0) {
3837 hotkey_enabledisable_warn(0);
3838 res = -EPERM;
3839 } else if (strlencmp(cmd, "reset") == 0) {
3840 mask = (hotkey_all_mask | hotkey_source_mask)
3841 & ~hotkey_reserved_mask;
3842 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3843 /* mask set */
3844 } else if (sscanf(cmd, "%x", &mask) == 1) {
3845 /* mask set */
3846 } else {
3847 res = -EINVAL;
3848 goto errexit;
3852 if (!res) {
3853 tpacpi_disclose_usertask("procfs hotkey",
3854 "set mask to 0x%08x\n", mask);
3855 res = hotkey_user_mask_set(mask);
3858 errexit:
3859 mutex_unlock(&hotkey_mutex);
3860 return res;
3863 static const struct acpi_device_id ibm_htk_device_ids[] = {
3864 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3865 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3866 {"", 0},
3869 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3870 .hid = ibm_htk_device_ids,
3871 .notify = hotkey_notify,
3872 .handle = &hkey_handle,
3873 .type = ACPI_DEVICE_NOTIFY,
3876 static struct ibm_struct hotkey_driver_data = {
3877 .name = "hotkey",
3878 .read = hotkey_read,
3879 .write = hotkey_write,
3880 .exit = hotkey_exit,
3881 .resume = hotkey_resume,
3882 .suspend = hotkey_suspend,
3883 .acpi = &ibm_hotkey_acpidriver,
3886 /*************************************************************************
3887 * Bluetooth subdriver
3890 enum {
3891 /* ACPI GBDC/SBDC bits */
3892 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3893 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3894 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3895 0 = disable, 1 = enable */
3898 enum {
3899 /* ACPI \BLTH commands */
3900 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3901 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3902 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3903 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3904 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3907 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3909 static int bluetooth_get_status(void)
3911 int status;
3913 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3914 if (dbg_bluetoothemul)
3915 return (tpacpi_bluetooth_emulstate) ?
3916 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3917 #endif
3919 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3920 return -EIO;
3922 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3923 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3926 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3928 int status;
3930 vdbg_printk(TPACPI_DBG_RFKILL,
3931 "will attempt to %s bluetooth\n",
3932 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3934 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3935 if (dbg_bluetoothemul) {
3936 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3937 return 0;
3939 #endif
3941 if (state == TPACPI_RFK_RADIO_ON)
3942 status = TP_ACPI_BLUETOOTH_RADIOSSW
3943 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3944 else
3945 status = 0;
3947 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3948 return -EIO;
3950 return 0;
3953 /* sysfs bluetooth enable ---------------------------------------------- */
3954 static ssize_t bluetooth_enable_show(struct device *dev,
3955 struct device_attribute *attr,
3956 char *buf)
3958 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3959 attr, buf);
3962 static ssize_t bluetooth_enable_store(struct device *dev,
3963 struct device_attribute *attr,
3964 const char *buf, size_t count)
3966 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3967 attr, buf, count);
3970 static struct device_attribute dev_attr_bluetooth_enable =
3971 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3972 bluetooth_enable_show, bluetooth_enable_store);
3974 /* --------------------------------------------------------------------- */
3976 static struct attribute *bluetooth_attributes[] = {
3977 &dev_attr_bluetooth_enable.attr,
3978 NULL
3981 static const struct attribute_group bluetooth_attr_group = {
3982 .attrs = bluetooth_attributes,
3985 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3986 .get_status = bluetooth_get_status,
3987 .set_status = bluetooth_set_status,
3990 static void bluetooth_shutdown(void)
3992 /* Order firmware to save current state to NVRAM */
3993 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3994 TP_ACPI_BLTH_SAVE_STATE))
3995 printk(TPACPI_NOTICE
3996 "failed to save bluetooth state to NVRAM\n");
3997 else
3998 vdbg_printk(TPACPI_DBG_RFKILL,
3999 "bluestooth state saved to NVRAM\n");
4002 static void bluetooth_exit(void)
4004 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4005 &bluetooth_attr_group);
4007 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4009 bluetooth_shutdown();
4012 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4014 int res;
4015 int status = 0;
4017 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4018 "initializing bluetooth subdriver\n");
4020 TPACPI_ACPIHANDLE_INIT(hkey);
4022 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4023 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4024 tp_features.bluetooth = hkey_handle &&
4025 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4027 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4028 "bluetooth is %s, status 0x%02x\n",
4029 str_supported(tp_features.bluetooth),
4030 status);
4032 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4033 if (dbg_bluetoothemul) {
4034 tp_features.bluetooth = 1;
4035 printk(TPACPI_INFO
4036 "bluetooth switch emulation enabled\n");
4037 } else
4038 #endif
4039 if (tp_features.bluetooth &&
4040 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4041 /* no bluetooth hardware present in system */
4042 tp_features.bluetooth = 0;
4043 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4044 "bluetooth hardware not installed\n");
4047 if (!tp_features.bluetooth)
4048 return 1;
4050 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4051 &bluetooth_tprfk_ops,
4052 RFKILL_TYPE_BLUETOOTH,
4053 TPACPI_RFK_BLUETOOTH_SW_NAME,
4054 true);
4055 if (res)
4056 return res;
4058 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4059 &bluetooth_attr_group);
4060 if (res) {
4061 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4062 return res;
4065 return 0;
4068 /* procfs -------------------------------------------------------------- */
4069 static int bluetooth_read(struct seq_file *m)
4071 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4074 static int bluetooth_write(char *buf)
4076 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4079 static struct ibm_struct bluetooth_driver_data = {
4080 .name = "bluetooth",
4081 .read = bluetooth_read,
4082 .write = bluetooth_write,
4083 .exit = bluetooth_exit,
4084 .shutdown = bluetooth_shutdown,
4087 /*************************************************************************
4088 * Wan subdriver
4091 enum {
4092 /* ACPI GWAN/SWAN bits */
4093 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4094 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4095 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4096 0 = disable, 1 = enable */
4099 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4101 static int wan_get_status(void)
4103 int status;
4105 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4106 if (dbg_wwanemul)
4107 return (tpacpi_wwan_emulstate) ?
4108 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4109 #endif
4111 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4112 return -EIO;
4114 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4115 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4118 static int wan_set_status(enum tpacpi_rfkill_state state)
4120 int status;
4122 vdbg_printk(TPACPI_DBG_RFKILL,
4123 "will attempt to %s wwan\n",
4124 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4126 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4127 if (dbg_wwanemul) {
4128 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4129 return 0;
4131 #endif
4133 if (state == TPACPI_RFK_RADIO_ON)
4134 status = TP_ACPI_WANCARD_RADIOSSW
4135 | TP_ACPI_WANCARD_RESUMECTRL;
4136 else
4137 status = 0;
4139 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4140 return -EIO;
4142 return 0;
4145 /* sysfs wan enable ---------------------------------------------------- */
4146 static ssize_t wan_enable_show(struct device *dev,
4147 struct device_attribute *attr,
4148 char *buf)
4150 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4151 attr, buf);
4154 static ssize_t wan_enable_store(struct device *dev,
4155 struct device_attribute *attr,
4156 const char *buf, size_t count)
4158 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4159 attr, buf, count);
4162 static struct device_attribute dev_attr_wan_enable =
4163 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4164 wan_enable_show, wan_enable_store);
4166 /* --------------------------------------------------------------------- */
4168 static struct attribute *wan_attributes[] = {
4169 &dev_attr_wan_enable.attr,
4170 NULL
4173 static const struct attribute_group wan_attr_group = {
4174 .attrs = wan_attributes,
4177 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4178 .get_status = wan_get_status,
4179 .set_status = wan_set_status,
4182 static void wan_shutdown(void)
4184 /* Order firmware to save current state to NVRAM */
4185 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4186 TP_ACPI_WGSV_SAVE_STATE))
4187 printk(TPACPI_NOTICE
4188 "failed to save WWAN state to NVRAM\n");
4189 else
4190 vdbg_printk(TPACPI_DBG_RFKILL,
4191 "WWAN state saved to NVRAM\n");
4194 static void wan_exit(void)
4196 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4197 &wan_attr_group);
4199 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4201 wan_shutdown();
4204 static int __init wan_init(struct ibm_init_struct *iibm)
4206 int res;
4207 int status = 0;
4209 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4210 "initializing wan subdriver\n");
4212 TPACPI_ACPIHANDLE_INIT(hkey);
4214 tp_features.wan = hkey_handle &&
4215 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4217 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4218 "wan is %s, status 0x%02x\n",
4219 str_supported(tp_features.wan),
4220 status);
4222 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4223 if (dbg_wwanemul) {
4224 tp_features.wan = 1;
4225 printk(TPACPI_INFO
4226 "wwan switch emulation enabled\n");
4227 } else
4228 #endif
4229 if (tp_features.wan &&
4230 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4231 /* no wan hardware present in system */
4232 tp_features.wan = 0;
4233 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4234 "wan hardware not installed\n");
4237 if (!tp_features.wan)
4238 return 1;
4240 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4241 &wan_tprfk_ops,
4242 RFKILL_TYPE_WWAN,
4243 TPACPI_RFK_WWAN_SW_NAME,
4244 true);
4245 if (res)
4246 return res;
4248 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4249 &wan_attr_group);
4251 if (res) {
4252 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4253 return res;
4256 return 0;
4259 /* procfs -------------------------------------------------------------- */
4260 static int wan_read(struct seq_file *m)
4262 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4265 static int wan_write(char *buf)
4267 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4270 static struct ibm_struct wan_driver_data = {
4271 .name = "wan",
4272 .read = wan_read,
4273 .write = wan_write,
4274 .exit = wan_exit,
4275 .shutdown = wan_shutdown,
4278 /*************************************************************************
4279 * UWB subdriver
4282 enum {
4283 /* ACPI GUWB/SUWB bits */
4284 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4285 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4288 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4290 static int uwb_get_status(void)
4292 int status;
4294 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4295 if (dbg_uwbemul)
4296 return (tpacpi_uwb_emulstate) ?
4297 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4298 #endif
4300 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4301 return -EIO;
4303 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4304 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4307 static int uwb_set_status(enum tpacpi_rfkill_state state)
4309 int status;
4311 vdbg_printk(TPACPI_DBG_RFKILL,
4312 "will attempt to %s UWB\n",
4313 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4315 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4316 if (dbg_uwbemul) {
4317 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4318 return 0;
4320 #endif
4322 if (state == TPACPI_RFK_RADIO_ON)
4323 status = TP_ACPI_UWB_RADIOSSW;
4324 else
4325 status = 0;
4327 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4328 return -EIO;
4330 return 0;
4333 /* --------------------------------------------------------------------- */
4335 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4336 .get_status = uwb_get_status,
4337 .set_status = uwb_set_status,
4340 static void uwb_exit(void)
4342 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4345 static int __init uwb_init(struct ibm_init_struct *iibm)
4347 int res;
4348 int status = 0;
4350 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4351 "initializing uwb subdriver\n");
4353 TPACPI_ACPIHANDLE_INIT(hkey);
4355 tp_features.uwb = hkey_handle &&
4356 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4358 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4359 "uwb is %s, status 0x%02x\n",
4360 str_supported(tp_features.uwb),
4361 status);
4363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4364 if (dbg_uwbemul) {
4365 tp_features.uwb = 1;
4366 printk(TPACPI_INFO
4367 "uwb switch emulation enabled\n");
4368 } else
4369 #endif
4370 if (tp_features.uwb &&
4371 !(status & TP_ACPI_UWB_HWPRESENT)) {
4372 /* no uwb hardware present in system */
4373 tp_features.uwb = 0;
4374 dbg_printk(TPACPI_DBG_INIT,
4375 "uwb hardware not installed\n");
4378 if (!tp_features.uwb)
4379 return 1;
4381 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4382 &uwb_tprfk_ops,
4383 RFKILL_TYPE_UWB,
4384 TPACPI_RFK_UWB_SW_NAME,
4385 false);
4386 return res;
4389 static struct ibm_struct uwb_driver_data = {
4390 .name = "uwb",
4391 .exit = uwb_exit,
4392 .flags.experimental = 1,
4395 /*************************************************************************
4396 * Video subdriver
4399 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4401 enum video_access_mode {
4402 TPACPI_VIDEO_NONE = 0,
4403 TPACPI_VIDEO_570, /* 570 */
4404 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4405 TPACPI_VIDEO_NEW, /* all others */
4408 enum { /* video status flags, based on VIDEO_570 */
4409 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4410 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4411 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4414 enum { /* TPACPI_VIDEO_570 constants */
4415 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4416 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4417 * video_status_flags */
4418 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4419 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4422 static enum video_access_mode video_supported;
4423 static int video_orig_autosw;
4425 static int video_autosw_get(void);
4426 static int video_autosw_set(int enable);
4428 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4430 static int __init video_init(struct ibm_init_struct *iibm)
4432 int ivga;
4434 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4436 TPACPI_ACPIHANDLE_INIT(vid);
4437 if (tpacpi_is_ibm())
4438 TPACPI_ACPIHANDLE_INIT(vid2);
4440 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4441 /* G41, assume IVGA doesn't change */
4442 vid_handle = vid2_handle;
4444 if (!vid_handle)
4445 /* video switching not supported on R30, R31 */
4446 video_supported = TPACPI_VIDEO_NONE;
4447 else if (tpacpi_is_ibm() &&
4448 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4449 /* 570 */
4450 video_supported = TPACPI_VIDEO_570;
4451 else if (tpacpi_is_ibm() &&
4452 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4453 /* 600e/x, 770e, 770x */
4454 video_supported = TPACPI_VIDEO_770;
4455 else
4456 /* all others */
4457 video_supported = TPACPI_VIDEO_NEW;
4459 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4460 str_supported(video_supported != TPACPI_VIDEO_NONE),
4461 video_supported);
4463 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4466 static void video_exit(void)
4468 dbg_printk(TPACPI_DBG_EXIT,
4469 "restoring original video autoswitch mode\n");
4470 if (video_autosw_set(video_orig_autosw))
4471 printk(TPACPI_ERR "error while trying to restore original "
4472 "video autoswitch mode\n");
4475 static int video_outputsw_get(void)
4477 int status = 0;
4478 int i;
4480 switch (video_supported) {
4481 case TPACPI_VIDEO_570:
4482 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4483 TP_ACPI_VIDEO_570_PHSCMD))
4484 return -EIO;
4485 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4486 break;
4487 case TPACPI_VIDEO_770:
4488 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_LCD;
4492 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_CRT;
4496 break;
4497 case TPACPI_VIDEO_NEW:
4498 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4499 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4500 return -EIO;
4501 if (i)
4502 status |= TP_ACPI_VIDEO_S_CRT;
4504 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4505 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4506 return -EIO;
4507 if (i)
4508 status |= TP_ACPI_VIDEO_S_LCD;
4509 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4510 return -EIO;
4511 if (i)
4512 status |= TP_ACPI_VIDEO_S_DVI;
4513 break;
4514 default:
4515 return -ENOSYS;
4518 return status;
4521 static int video_outputsw_set(int status)
4523 int autosw;
4524 int res = 0;
4526 switch (video_supported) {
4527 case TPACPI_VIDEO_570:
4528 res = acpi_evalf(NULL, NULL,
4529 "\\_SB.PHS2", "vdd",
4530 TP_ACPI_VIDEO_570_PHS2CMD,
4531 status | TP_ACPI_VIDEO_570_PHS2SET);
4532 break;
4533 case TPACPI_VIDEO_770:
4534 autosw = video_autosw_get();
4535 if (autosw < 0)
4536 return autosw;
4538 res = video_autosw_set(1);
4539 if (res)
4540 return res;
4541 res = acpi_evalf(vid_handle, NULL,
4542 "ASWT", "vdd", status * 0x100, 0);
4543 if (!autosw && video_autosw_set(autosw)) {
4544 printk(TPACPI_ERR
4545 "video auto-switch left enabled due to error\n");
4546 return -EIO;
4548 break;
4549 case TPACPI_VIDEO_NEW:
4550 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4551 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4552 break;
4553 default:
4554 return -ENOSYS;
4557 return (res)? 0 : -EIO;
4560 static int video_autosw_get(void)
4562 int autosw = 0;
4564 switch (video_supported) {
4565 case TPACPI_VIDEO_570:
4566 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4567 return -EIO;
4568 break;
4569 case TPACPI_VIDEO_770:
4570 case TPACPI_VIDEO_NEW:
4571 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4572 return -EIO;
4573 break;
4574 default:
4575 return -ENOSYS;
4578 return autosw & 1;
4581 static int video_autosw_set(int enable)
4583 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4584 return -EIO;
4585 return 0;
4588 static int video_outputsw_cycle(void)
4590 int autosw = video_autosw_get();
4591 int res;
4593 if (autosw < 0)
4594 return autosw;
4596 switch (video_supported) {
4597 case TPACPI_VIDEO_570:
4598 res = video_autosw_set(1);
4599 if (res)
4600 return res;
4601 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4602 break;
4603 case TPACPI_VIDEO_770:
4604 case TPACPI_VIDEO_NEW:
4605 res = video_autosw_set(1);
4606 if (res)
4607 return res;
4608 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4609 break;
4610 default:
4611 return -ENOSYS;
4613 if (!autosw && video_autosw_set(autosw)) {
4614 printk(TPACPI_ERR
4615 "video auto-switch left enabled due to error\n");
4616 return -EIO;
4619 return (res)? 0 : -EIO;
4622 static int video_expand_toggle(void)
4624 switch (video_supported) {
4625 case TPACPI_VIDEO_570:
4626 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4627 0 : -EIO;
4628 case TPACPI_VIDEO_770:
4629 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4630 0 : -EIO;
4631 case TPACPI_VIDEO_NEW:
4632 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4633 0 : -EIO;
4634 default:
4635 return -ENOSYS;
4637 /* not reached */
4640 static int video_read(struct seq_file *m)
4642 int status, autosw;
4644 if (video_supported == TPACPI_VIDEO_NONE) {
4645 seq_printf(m, "status:\t\tnot supported\n");
4646 return 0;
4649 /* Even reads can crash X.org, so... */
4650 if (!capable(CAP_SYS_ADMIN))
4651 return -EPERM;
4653 status = video_outputsw_get();
4654 if (status < 0)
4655 return status;
4657 autosw = video_autosw_get();
4658 if (autosw < 0)
4659 return autosw;
4661 seq_printf(m, "status:\t\tsupported\n");
4662 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4663 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4664 if (video_supported == TPACPI_VIDEO_NEW)
4665 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4666 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4667 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4668 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4669 if (video_supported == TPACPI_VIDEO_NEW)
4670 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4671 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4672 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4674 return 0;
4677 static int video_write(char *buf)
4679 char *cmd;
4680 int enable, disable, status;
4681 int res;
4683 if (video_supported == TPACPI_VIDEO_NONE)
4684 return -ENODEV;
4686 /* Even reads can crash X.org, let alone writes... */
4687 if (!capable(CAP_SYS_ADMIN))
4688 return -EPERM;
4690 enable = 0;
4691 disable = 0;
4693 while ((cmd = next_cmd(&buf))) {
4694 if (strlencmp(cmd, "lcd_enable") == 0) {
4695 enable |= TP_ACPI_VIDEO_S_LCD;
4696 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4697 disable |= TP_ACPI_VIDEO_S_LCD;
4698 } else if (strlencmp(cmd, "crt_enable") == 0) {
4699 enable |= TP_ACPI_VIDEO_S_CRT;
4700 } else if (strlencmp(cmd, "crt_disable") == 0) {
4701 disable |= TP_ACPI_VIDEO_S_CRT;
4702 } else if (video_supported == TPACPI_VIDEO_NEW &&
4703 strlencmp(cmd, "dvi_enable") == 0) {
4704 enable |= TP_ACPI_VIDEO_S_DVI;
4705 } else if (video_supported == TPACPI_VIDEO_NEW &&
4706 strlencmp(cmd, "dvi_disable") == 0) {
4707 disable |= TP_ACPI_VIDEO_S_DVI;
4708 } else if (strlencmp(cmd, "auto_enable") == 0) {
4709 res = video_autosw_set(1);
4710 if (res)
4711 return res;
4712 } else if (strlencmp(cmd, "auto_disable") == 0) {
4713 res = video_autosw_set(0);
4714 if (res)
4715 return res;
4716 } else if (strlencmp(cmd, "video_switch") == 0) {
4717 res = video_outputsw_cycle();
4718 if (res)
4719 return res;
4720 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4721 res = video_expand_toggle();
4722 if (res)
4723 return res;
4724 } else
4725 return -EINVAL;
4728 if (enable || disable) {
4729 status = video_outputsw_get();
4730 if (status < 0)
4731 return status;
4732 res = video_outputsw_set((status & ~disable) | enable);
4733 if (res)
4734 return res;
4737 return 0;
4740 static struct ibm_struct video_driver_data = {
4741 .name = "video",
4742 .read = video_read,
4743 .write = video_write,
4744 .exit = video_exit,
4747 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4749 /*************************************************************************
4750 * Light (thinklight) subdriver
4753 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4754 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4756 static int light_get_status(void)
4758 int status = 0;
4760 if (tp_features.light_status) {
4761 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4762 return -EIO;
4763 return (!!status);
4766 return -ENXIO;
4769 static int light_set_status(int status)
4771 int rc;
4773 if (tp_features.light) {
4774 if (cmos_handle) {
4775 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4776 (status)?
4777 TP_CMOS_THINKLIGHT_ON :
4778 TP_CMOS_THINKLIGHT_OFF);
4779 } else {
4780 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4781 (status)? 1 : 0);
4783 return (rc)? 0 : -EIO;
4786 return -ENXIO;
4789 static void light_set_status_worker(struct work_struct *work)
4791 struct tpacpi_led_classdev *data =
4792 container_of(work, struct tpacpi_led_classdev, work);
4794 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4795 light_set_status((data->new_state != TPACPI_LED_OFF));
4798 static void light_sysfs_set(struct led_classdev *led_cdev,
4799 enum led_brightness brightness)
4801 struct tpacpi_led_classdev *data =
4802 container_of(led_cdev,
4803 struct tpacpi_led_classdev,
4804 led_classdev);
4805 data->new_state = (brightness != LED_OFF) ?
4806 TPACPI_LED_ON : TPACPI_LED_OFF;
4807 queue_work(tpacpi_wq, &data->work);
4810 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4812 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4815 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4816 .led_classdev = {
4817 .name = "tpacpi::thinklight",
4818 .brightness_set = &light_sysfs_set,
4819 .brightness_get = &light_sysfs_get,
4823 static int __init light_init(struct ibm_init_struct *iibm)
4825 int rc;
4827 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4829 if (tpacpi_is_ibm()) {
4830 TPACPI_ACPIHANDLE_INIT(ledb);
4831 TPACPI_ACPIHANDLE_INIT(lght);
4833 TPACPI_ACPIHANDLE_INIT(cmos);
4834 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4836 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4837 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4839 if (tp_features.light)
4840 /* light status not supported on
4841 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4842 tp_features.light_status =
4843 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4845 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4846 str_supported(tp_features.light),
4847 str_supported(tp_features.light_status));
4849 if (!tp_features.light)
4850 return 1;
4852 rc = led_classdev_register(&tpacpi_pdev->dev,
4853 &tpacpi_led_thinklight.led_classdev);
4855 if (rc < 0) {
4856 tp_features.light = 0;
4857 tp_features.light_status = 0;
4858 } else {
4859 rc = 0;
4862 return rc;
4865 static void light_exit(void)
4867 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4868 if (work_pending(&tpacpi_led_thinklight.work))
4869 flush_workqueue(tpacpi_wq);
4872 static int light_read(struct seq_file *m)
4874 int status;
4876 if (!tp_features.light) {
4877 seq_printf(m, "status:\t\tnot supported\n");
4878 } else if (!tp_features.light_status) {
4879 seq_printf(m, "status:\t\tunknown\n");
4880 seq_printf(m, "commands:\ton, off\n");
4881 } else {
4882 status = light_get_status();
4883 if (status < 0)
4884 return status;
4885 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4886 seq_printf(m, "commands:\ton, off\n");
4889 return 0;
4892 static int light_write(char *buf)
4894 char *cmd;
4895 int newstatus = 0;
4897 if (!tp_features.light)
4898 return -ENODEV;
4900 while ((cmd = next_cmd(&buf))) {
4901 if (strlencmp(cmd, "on") == 0) {
4902 newstatus = 1;
4903 } else if (strlencmp(cmd, "off") == 0) {
4904 newstatus = 0;
4905 } else
4906 return -EINVAL;
4909 return light_set_status(newstatus);
4912 static struct ibm_struct light_driver_data = {
4913 .name = "light",
4914 .read = light_read,
4915 .write = light_write,
4916 .exit = light_exit,
4919 /*************************************************************************
4920 * CMOS subdriver
4923 /* sysfs cmos_command -------------------------------------------------- */
4924 static ssize_t cmos_command_store(struct device *dev,
4925 struct device_attribute *attr,
4926 const char *buf, size_t count)
4928 unsigned long cmos_cmd;
4929 int res;
4931 if (parse_strtoul(buf, 21, &cmos_cmd))
4932 return -EINVAL;
4934 res = issue_thinkpad_cmos_command(cmos_cmd);
4935 return (res)? res : count;
4938 static struct device_attribute dev_attr_cmos_command =
4939 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4941 /* --------------------------------------------------------------------- */
4943 static int __init cmos_init(struct ibm_init_struct *iibm)
4945 int res;
4947 vdbg_printk(TPACPI_DBG_INIT,
4948 "initializing cmos commands subdriver\n");
4950 TPACPI_ACPIHANDLE_INIT(cmos);
4952 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4953 str_supported(cmos_handle != NULL));
4955 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4956 if (res)
4957 return res;
4959 return (cmos_handle)? 0 : 1;
4962 static void cmos_exit(void)
4964 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4967 static int cmos_read(struct seq_file *m)
4969 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4970 R30, R31, T20-22, X20-21 */
4971 if (!cmos_handle)
4972 seq_printf(m, "status:\t\tnot supported\n");
4973 else {
4974 seq_printf(m, "status:\t\tsupported\n");
4975 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4978 return 0;
4981 static int cmos_write(char *buf)
4983 char *cmd;
4984 int cmos_cmd, res;
4986 while ((cmd = next_cmd(&buf))) {
4987 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4988 cmos_cmd >= 0 && cmos_cmd <= 21) {
4989 /* cmos_cmd set */
4990 } else
4991 return -EINVAL;
4993 res = issue_thinkpad_cmos_command(cmos_cmd);
4994 if (res)
4995 return res;
4998 return 0;
5001 static struct ibm_struct cmos_driver_data = {
5002 .name = "cmos",
5003 .read = cmos_read,
5004 .write = cmos_write,
5005 .exit = cmos_exit,
5008 /*************************************************************************
5009 * LED subdriver
5012 enum led_access_mode {
5013 TPACPI_LED_NONE = 0,
5014 TPACPI_LED_570, /* 570 */
5015 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5016 TPACPI_LED_NEW, /* all others */
5019 enum { /* For TPACPI_LED_OLD */
5020 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5021 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5022 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5025 static enum led_access_mode led_supported;
5027 static acpi_handle led_handle;
5029 #define TPACPI_LED_NUMLEDS 16
5030 static struct tpacpi_led_classdev *tpacpi_leds;
5031 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5032 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5033 /* there's a limit of 19 chars + NULL before 2.6.26 */
5034 "tpacpi::power",
5035 "tpacpi:orange:batt",
5036 "tpacpi:green:batt",
5037 "tpacpi::dock_active",
5038 "tpacpi::bay_active",
5039 "tpacpi::dock_batt",
5040 "tpacpi::unknown_led",
5041 "tpacpi::standby",
5042 "tpacpi::dock_status1",
5043 "tpacpi::dock_status2",
5044 "tpacpi::unknown_led2",
5045 "tpacpi::unknown_led3",
5046 "tpacpi::thinkvantage",
5048 #define TPACPI_SAFE_LEDS 0x1081U
5050 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5052 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5053 return false;
5054 #else
5055 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5056 #endif
5059 static int led_get_status(const unsigned int led)
5061 int status;
5062 enum led_status_t led_s;
5064 switch (led_supported) {
5065 case TPACPI_LED_570:
5066 if (!acpi_evalf(ec_handle,
5067 &status, "GLED", "dd", 1 << led))
5068 return -EIO;
5069 led_s = (status == 0)?
5070 TPACPI_LED_OFF :
5071 ((status == 1)?
5072 TPACPI_LED_ON :
5073 TPACPI_LED_BLINK);
5074 tpacpi_led_state_cache[led] = led_s;
5075 return led_s;
5076 default:
5077 return -ENXIO;
5080 /* not reached */
5083 static int led_set_status(const unsigned int led,
5084 const enum led_status_t ledstatus)
5086 /* off, on, blink. Index is led_status_t */
5087 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5088 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5090 int rc = 0;
5092 switch (led_supported) {
5093 case TPACPI_LED_570:
5094 /* 570 */
5095 if (unlikely(led > 7))
5096 return -EINVAL;
5097 if (unlikely(tpacpi_is_led_restricted(led)))
5098 return -EPERM;
5099 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5100 (1 << led), led_sled_arg1[ledstatus]))
5101 rc = -EIO;
5102 break;
5103 case TPACPI_LED_OLD:
5104 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5105 if (unlikely(led > 7))
5106 return -EINVAL;
5107 if (unlikely(tpacpi_is_led_restricted(led)))
5108 return -EPERM;
5109 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5110 if (rc >= 0)
5111 rc = ec_write(TPACPI_LED_EC_HLBL,
5112 (ledstatus == TPACPI_LED_BLINK) << led);
5113 if (rc >= 0)
5114 rc = ec_write(TPACPI_LED_EC_HLCL,
5115 (ledstatus != TPACPI_LED_OFF) << led);
5116 break;
5117 case TPACPI_LED_NEW:
5118 /* all others */
5119 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5120 return -EINVAL;
5121 if (unlikely(tpacpi_is_led_restricted(led)))
5122 return -EPERM;
5123 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5124 led, led_led_arg1[ledstatus]))
5125 rc = -EIO;
5126 break;
5127 default:
5128 rc = -ENXIO;
5131 if (!rc)
5132 tpacpi_led_state_cache[led] = ledstatus;
5134 return rc;
5137 static void led_set_status_worker(struct work_struct *work)
5139 struct tpacpi_led_classdev *data =
5140 container_of(work, struct tpacpi_led_classdev, work);
5142 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5143 led_set_status(data->led, data->new_state);
5146 static void led_sysfs_set(struct led_classdev *led_cdev,
5147 enum led_brightness brightness)
5149 struct tpacpi_led_classdev *data = container_of(led_cdev,
5150 struct tpacpi_led_classdev, led_classdev);
5152 if (brightness == LED_OFF)
5153 data->new_state = TPACPI_LED_OFF;
5154 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5155 data->new_state = TPACPI_LED_ON;
5156 else
5157 data->new_state = TPACPI_LED_BLINK;
5159 queue_work(tpacpi_wq, &data->work);
5162 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5163 unsigned long *delay_on, unsigned long *delay_off)
5165 struct tpacpi_led_classdev *data = container_of(led_cdev,
5166 struct tpacpi_led_classdev, led_classdev);
5168 /* Can we choose the flash rate? */
5169 if (*delay_on == 0 && *delay_off == 0) {
5170 /* yes. set them to the hardware blink rate (1 Hz) */
5171 *delay_on = 500; /* ms */
5172 *delay_off = 500; /* ms */
5173 } else if ((*delay_on != 500) || (*delay_off != 500))
5174 return -EINVAL;
5176 data->new_state = TPACPI_LED_BLINK;
5177 queue_work(tpacpi_wq, &data->work);
5179 return 0;
5182 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5184 int rc;
5186 struct tpacpi_led_classdev *data = container_of(led_cdev,
5187 struct tpacpi_led_classdev, led_classdev);
5189 rc = led_get_status(data->led);
5191 if (rc == TPACPI_LED_OFF || rc < 0)
5192 rc = LED_OFF; /* no error handling in led class :( */
5193 else
5194 rc = LED_FULL;
5196 return rc;
5199 static void led_exit(void)
5201 unsigned int i;
5203 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5204 if (tpacpi_leds[i].led_classdev.name)
5205 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5208 kfree(tpacpi_leds);
5211 static int __init tpacpi_init_led(unsigned int led)
5213 int rc;
5215 tpacpi_leds[led].led = led;
5217 /* LEDs with no name don't get registered */
5218 if (!tpacpi_led_names[led])
5219 return 0;
5221 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5222 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5223 if (led_supported == TPACPI_LED_570)
5224 tpacpi_leds[led].led_classdev.brightness_get =
5225 &led_sysfs_get;
5227 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5229 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5231 rc = led_classdev_register(&tpacpi_pdev->dev,
5232 &tpacpi_leds[led].led_classdev);
5233 if (rc < 0)
5234 tpacpi_leds[led].led_classdev.name = NULL;
5236 return rc;
5239 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5240 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5241 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5242 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5244 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5245 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5246 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5247 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5248 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5249 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5250 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5251 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5253 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5254 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5255 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5256 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5257 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5259 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5260 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5261 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5262 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5264 /* (1) - may have excess leds enabled on MSB */
5266 /* Defaults (order matters, keep last, don't reorder!) */
5267 { /* Lenovo */
5268 .vendor = PCI_VENDOR_ID_LENOVO,
5269 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5270 .quirks = 0x1fffU,
5272 { /* IBM ThinkPads with no EC version string */
5273 .vendor = PCI_VENDOR_ID_IBM,
5274 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5275 .quirks = 0x00ffU,
5277 { /* IBM ThinkPads with EC version string */
5278 .vendor = PCI_VENDOR_ID_IBM,
5279 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5280 .quirks = 0x00bfU,
5284 #undef TPACPI_LEDQ_IBM
5285 #undef TPACPI_LEDQ_LNV
5287 static enum led_access_mode __init led_init_detect_mode(void)
5289 acpi_status status;
5291 if (tpacpi_is_ibm()) {
5292 /* 570 */
5293 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5294 if (ACPI_SUCCESS(status))
5295 return TPACPI_LED_570;
5297 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5298 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5299 if (ACPI_SUCCESS(status))
5300 return TPACPI_LED_OLD;
5303 /* most others */
5304 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5305 if (ACPI_SUCCESS(status))
5306 return TPACPI_LED_NEW;
5308 /* R30, R31, and unknown firmwares */
5309 led_handle = NULL;
5310 return TPACPI_LED_NONE;
5313 static int __init led_init(struct ibm_init_struct *iibm)
5315 unsigned int i;
5316 int rc;
5317 unsigned long useful_leds;
5319 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5321 led_supported = led_init_detect_mode();
5323 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5324 str_supported(led_supported), led_supported);
5326 if (led_supported == TPACPI_LED_NONE)
5327 return 1;
5329 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5330 GFP_KERNEL);
5331 if (!tpacpi_leds) {
5332 printk(TPACPI_ERR "Out of memory for LED data\n");
5333 return -ENOMEM;
5336 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5337 ARRAY_SIZE(led_useful_qtable));
5339 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5340 if (!tpacpi_is_led_restricted(i) &&
5341 test_bit(i, &useful_leds)) {
5342 rc = tpacpi_init_led(i);
5343 if (rc < 0) {
5344 led_exit();
5345 return rc;
5350 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5351 printk(TPACPI_NOTICE
5352 "warning: userspace override of important "
5353 "firmware LEDs is enabled\n");
5354 #endif
5355 return 0;
5358 #define str_led_status(s) \
5359 ((s) == TPACPI_LED_OFF ? "off" : \
5360 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5362 static int led_read(struct seq_file *m)
5364 if (!led_supported) {
5365 seq_printf(m, "status:\t\tnot supported\n");
5366 return 0;
5368 seq_printf(m, "status:\t\tsupported\n");
5370 if (led_supported == TPACPI_LED_570) {
5371 /* 570 */
5372 int i, status;
5373 for (i = 0; i < 8; i++) {
5374 status = led_get_status(i);
5375 if (status < 0)
5376 return -EIO;
5377 seq_printf(m, "%d:\t\t%s\n",
5378 i, str_led_status(status));
5382 seq_printf(m, "commands:\t"
5383 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5385 return 0;
5388 static int led_write(char *buf)
5390 char *cmd;
5391 int led, rc;
5392 enum led_status_t s;
5394 if (!led_supported)
5395 return -ENODEV;
5397 while ((cmd = next_cmd(&buf))) {
5398 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5399 return -EINVAL;
5401 if (strstr(cmd, "off")) {
5402 s = TPACPI_LED_OFF;
5403 } else if (strstr(cmd, "on")) {
5404 s = TPACPI_LED_ON;
5405 } else if (strstr(cmd, "blink")) {
5406 s = TPACPI_LED_BLINK;
5407 } else {
5408 return -EINVAL;
5411 rc = led_set_status(led, s);
5412 if (rc < 0)
5413 return rc;
5416 return 0;
5419 static struct ibm_struct led_driver_data = {
5420 .name = "led",
5421 .read = led_read,
5422 .write = led_write,
5423 .exit = led_exit,
5426 /*************************************************************************
5427 * Beep subdriver
5430 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5432 #define TPACPI_BEEP_Q1 0x0001
5434 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5435 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5436 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5439 static int __init beep_init(struct ibm_init_struct *iibm)
5441 unsigned long quirks;
5443 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5445 TPACPI_ACPIHANDLE_INIT(beep);
5447 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5448 str_supported(beep_handle != NULL));
5450 quirks = tpacpi_check_quirks(beep_quirk_table,
5451 ARRAY_SIZE(beep_quirk_table));
5453 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5455 return (beep_handle)? 0 : 1;
5458 static int beep_read(struct seq_file *m)
5460 if (!beep_handle)
5461 seq_printf(m, "status:\t\tnot supported\n");
5462 else {
5463 seq_printf(m, "status:\t\tsupported\n");
5464 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5467 return 0;
5470 static int beep_write(char *buf)
5472 char *cmd;
5473 int beep_cmd;
5475 if (!beep_handle)
5476 return -ENODEV;
5478 while ((cmd = next_cmd(&buf))) {
5479 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5480 beep_cmd >= 0 && beep_cmd <= 17) {
5481 /* beep_cmd set */
5482 } else
5483 return -EINVAL;
5484 if (tp_features.beep_needs_two_args) {
5485 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5486 beep_cmd, 0))
5487 return -EIO;
5488 } else {
5489 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5490 beep_cmd))
5491 return -EIO;
5495 return 0;
5498 static struct ibm_struct beep_driver_data = {
5499 .name = "beep",
5500 .read = beep_read,
5501 .write = beep_write,
5504 /*************************************************************************
5505 * Thermal subdriver
5508 enum thermal_access_mode {
5509 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5510 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5511 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5512 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5513 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5516 enum { /* TPACPI_THERMAL_TPEC_* */
5517 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5518 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5519 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5521 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5525 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5526 struct ibm_thermal_sensors_struct {
5527 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5530 static enum thermal_access_mode thermal_read_mode;
5532 /* idx is zero-based */
5533 static int thermal_get_sensor(int idx, s32 *value)
5535 int t;
5536 s8 tmp;
5537 char tmpi[5];
5539 t = TP_EC_THERMAL_TMP0;
5541 switch (thermal_read_mode) {
5542 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5543 case TPACPI_THERMAL_TPEC_16:
5544 if (idx >= 8 && idx <= 15) {
5545 t = TP_EC_THERMAL_TMP8;
5546 idx -= 8;
5548 /* fallthrough */
5549 #endif
5550 case TPACPI_THERMAL_TPEC_8:
5551 if (idx <= 7) {
5552 if (!acpi_ec_read(t + idx, &tmp))
5553 return -EIO;
5554 *value = tmp * 1000;
5555 return 0;
5557 break;
5559 case TPACPI_THERMAL_ACPI_UPDT:
5560 if (idx <= 7) {
5561 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5562 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5563 return -EIO;
5564 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5565 return -EIO;
5566 *value = (t - 2732) * 100;
5567 return 0;
5569 break;
5571 case TPACPI_THERMAL_ACPI_TMP07:
5572 if (idx <= 7) {
5573 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5574 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5575 return -EIO;
5576 if (t > 127 || t < -127)
5577 t = TP_EC_THERMAL_TMP_NA;
5578 *value = t * 1000;
5579 return 0;
5581 break;
5583 case TPACPI_THERMAL_NONE:
5584 default:
5585 return -ENOSYS;
5588 return -EINVAL;
5591 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5593 int res, i;
5594 int n;
5596 n = 8;
5597 i = 0;
5599 if (!s)
5600 return -EINVAL;
5602 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5603 n = 16;
5605 for (i = 0 ; i < n; i++) {
5606 res = thermal_get_sensor(i, &s->temp[i]);
5607 if (res)
5608 return res;
5611 return n;
5614 static void thermal_dump_all_sensors(void)
5616 int n, i;
5617 struct ibm_thermal_sensors_struct t;
5619 n = thermal_get_sensors(&t);
5620 if (n <= 0)
5621 return;
5623 printk(TPACPI_NOTICE
5624 "temperatures (Celsius):");
5626 for (i = 0; i < n; i++) {
5627 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5628 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5629 else
5630 printk(KERN_CONT " N/A");
5633 printk(KERN_CONT "\n");
5636 /* sysfs temp##_input -------------------------------------------------- */
5638 static ssize_t thermal_temp_input_show(struct device *dev,
5639 struct device_attribute *attr,
5640 char *buf)
5642 struct sensor_device_attribute *sensor_attr =
5643 to_sensor_dev_attr(attr);
5644 int idx = sensor_attr->index;
5645 s32 value;
5646 int res;
5648 res = thermal_get_sensor(idx, &value);
5649 if (res)
5650 return res;
5651 if (value == TPACPI_THERMAL_SENSOR_NA)
5652 return -ENXIO;
5654 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5657 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5658 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5659 thermal_temp_input_show, NULL, _idxB)
5661 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5662 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5663 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5664 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5665 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5666 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5667 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5668 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5669 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5670 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5671 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5672 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5673 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5674 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5675 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5676 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5677 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5680 #define THERMAL_ATTRS(X) \
5681 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5683 static struct attribute *thermal_temp_input_attr[] = {
5684 THERMAL_ATTRS(8),
5685 THERMAL_ATTRS(9),
5686 THERMAL_ATTRS(10),
5687 THERMAL_ATTRS(11),
5688 THERMAL_ATTRS(12),
5689 THERMAL_ATTRS(13),
5690 THERMAL_ATTRS(14),
5691 THERMAL_ATTRS(15),
5692 THERMAL_ATTRS(0),
5693 THERMAL_ATTRS(1),
5694 THERMAL_ATTRS(2),
5695 THERMAL_ATTRS(3),
5696 THERMAL_ATTRS(4),
5697 THERMAL_ATTRS(5),
5698 THERMAL_ATTRS(6),
5699 THERMAL_ATTRS(7),
5700 NULL
5703 static const struct attribute_group thermal_temp_input16_group = {
5704 .attrs = thermal_temp_input_attr
5707 static const struct attribute_group thermal_temp_input8_group = {
5708 .attrs = &thermal_temp_input_attr[8]
5711 #undef THERMAL_SENSOR_ATTR_TEMP
5712 #undef THERMAL_ATTRS
5714 /* --------------------------------------------------------------------- */
5716 static int __init thermal_init(struct ibm_init_struct *iibm)
5718 u8 t, ta1, ta2;
5719 int i;
5720 int acpi_tmp7;
5721 int res;
5723 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5725 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5727 if (thinkpad_id.ec_model) {
5729 * Direct EC access mode: sensors at registers
5730 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5731 * non-implemented, thermal sensors return 0x80 when
5732 * not available
5735 ta1 = ta2 = 0;
5736 for (i = 0; i < 8; i++) {
5737 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5738 ta1 |= t;
5739 } else {
5740 ta1 = 0;
5741 break;
5743 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5744 ta2 |= t;
5745 } else {
5746 ta1 = 0;
5747 break;
5750 if (ta1 == 0) {
5751 /* This is sheer paranoia, but we handle it anyway */
5752 if (acpi_tmp7) {
5753 printk(TPACPI_ERR
5754 "ThinkPad ACPI EC access misbehaving, "
5755 "falling back to ACPI TMPx access "
5756 "mode\n");
5757 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5758 } else {
5759 printk(TPACPI_ERR
5760 "ThinkPad ACPI EC access misbehaving, "
5761 "disabling thermal sensors access\n");
5762 thermal_read_mode = TPACPI_THERMAL_NONE;
5764 } else {
5765 thermal_read_mode =
5766 (ta2 != 0) ?
5767 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5769 } else if (acpi_tmp7) {
5770 if (tpacpi_is_ibm() &&
5771 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5772 /* 600e/x, 770e, 770x */
5773 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5774 } else {
5775 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5776 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5778 } else {
5779 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5780 thermal_read_mode = TPACPI_THERMAL_NONE;
5783 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5784 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5785 thermal_read_mode);
5787 switch (thermal_read_mode) {
5788 case TPACPI_THERMAL_TPEC_16:
5789 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5790 &thermal_temp_input16_group);
5791 if (res)
5792 return res;
5793 break;
5794 case TPACPI_THERMAL_TPEC_8:
5795 case TPACPI_THERMAL_ACPI_TMP07:
5796 case TPACPI_THERMAL_ACPI_UPDT:
5797 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5798 &thermal_temp_input8_group);
5799 if (res)
5800 return res;
5801 break;
5802 case TPACPI_THERMAL_NONE:
5803 default:
5804 return 1;
5807 return 0;
5810 static void thermal_exit(void)
5812 switch (thermal_read_mode) {
5813 case TPACPI_THERMAL_TPEC_16:
5814 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5815 &thermal_temp_input16_group);
5816 break;
5817 case TPACPI_THERMAL_TPEC_8:
5818 case TPACPI_THERMAL_ACPI_TMP07:
5819 case TPACPI_THERMAL_ACPI_UPDT:
5820 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5821 &thermal_temp_input8_group);
5822 break;
5823 case TPACPI_THERMAL_NONE:
5824 default:
5825 break;
5829 static int thermal_read(struct seq_file *m)
5831 int n, i;
5832 struct ibm_thermal_sensors_struct t;
5834 n = thermal_get_sensors(&t);
5835 if (unlikely(n < 0))
5836 return n;
5838 seq_printf(m, "temperatures:\t");
5840 if (n > 0) {
5841 for (i = 0; i < (n - 1); i++)
5842 seq_printf(m, "%d ", t.temp[i] / 1000);
5843 seq_printf(m, "%d\n", t.temp[i] / 1000);
5844 } else
5845 seq_printf(m, "not supported\n");
5847 return 0;
5850 static struct ibm_struct thermal_driver_data = {
5851 .name = "thermal",
5852 .read = thermal_read,
5853 .exit = thermal_exit,
5856 /*************************************************************************
5857 * EC Dump subdriver
5860 static u8 ecdump_regs[256];
5862 static int ecdump_read(struct seq_file *m)
5864 int i, j;
5865 u8 v;
5867 seq_printf(m, "EC "
5868 " +00 +01 +02 +03 +04 +05 +06 +07"
5869 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5870 for (i = 0; i < 256; i += 16) {
5871 seq_printf(m, "EC 0x%02x:", i);
5872 for (j = 0; j < 16; j++) {
5873 if (!acpi_ec_read(i + j, &v))
5874 break;
5875 if (v != ecdump_regs[i + j])
5876 seq_printf(m, " *%02x", v);
5877 else
5878 seq_printf(m, " %02x", v);
5879 ecdump_regs[i + j] = v;
5881 seq_putc(m, '\n');
5882 if (j != 16)
5883 break;
5886 /* These are way too dangerous to advertise openly... */
5887 #if 0
5888 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5889 " (<offset> is 00-ff, <value> is 00-ff)\n");
5890 seq_printf(m, "commands:\t0x<offset> <value> "
5891 " (<offset> is 00-ff, <value> is 0-255)\n");
5892 #endif
5893 return 0;
5896 static int ecdump_write(char *buf)
5898 char *cmd;
5899 int i, v;
5901 while ((cmd = next_cmd(&buf))) {
5902 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5903 /* i and v set */
5904 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5905 /* i and v set */
5906 } else
5907 return -EINVAL;
5908 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5909 if (!acpi_ec_write(i, v))
5910 return -EIO;
5911 } else
5912 return -EINVAL;
5915 return 0;
5918 static struct ibm_struct ecdump_driver_data = {
5919 .name = "ecdump",
5920 .read = ecdump_read,
5921 .write = ecdump_write,
5922 .flags.experimental = 1,
5925 /*************************************************************************
5926 * Backlight/brightness subdriver
5929 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5932 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5933 * CMOS NVRAM byte 0x5E, bits 0-3.
5935 * EC HBRV (0x31) has the following layout
5936 * Bit 7: unknown function
5937 * Bit 6: unknown function
5938 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5939 * Bit 4: must be set to zero to avoid problems
5940 * Bit 3-0: backlight brightness level
5942 * brightness_get_raw returns status data in the HBRV layout
5944 * WARNING: The X61 has been verified to use HBRV for something else, so
5945 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5946 * testing on the very early *60 Lenovo models...
5949 enum {
5950 TP_EC_BACKLIGHT = 0x31,
5952 /* TP_EC_BACKLIGHT bitmasks */
5953 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5954 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5955 TP_EC_BACKLIGHT_MAPSW = 0x20,
5958 enum tpacpi_brightness_access_mode {
5959 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5960 TPACPI_BRGHT_MODE_EC, /* EC control */
5961 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5962 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5963 TPACPI_BRGHT_MODE_MAX
5966 static struct backlight_device *ibm_backlight_device;
5968 static enum tpacpi_brightness_access_mode brightness_mode =
5969 TPACPI_BRGHT_MODE_MAX;
5971 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5973 static struct mutex brightness_mutex;
5975 /* NVRAM brightness access,
5976 * call with brightness_mutex held! */
5977 static unsigned int tpacpi_brightness_nvram_get(void)
5979 u8 lnvram;
5981 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5982 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5983 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5984 lnvram &= bright_maxlvl;
5986 return lnvram;
5989 static void tpacpi_brightness_checkpoint_nvram(void)
5991 u8 lec = 0;
5992 u8 b_nvram;
5994 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5995 return;
5997 vdbg_printk(TPACPI_DBG_BRGHT,
5998 "trying to checkpoint backlight level to NVRAM...\n");
6000 if (mutex_lock_killable(&brightness_mutex) < 0)
6001 return;
6003 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6004 goto unlock;
6005 lec &= TP_EC_BACKLIGHT_LVLMSK;
6006 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6008 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6009 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6010 /* NVRAM needs update */
6011 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6012 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6013 b_nvram |= lec;
6014 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6015 dbg_printk(TPACPI_DBG_BRGHT,
6016 "updated NVRAM backlight level to %u (0x%02x)\n",
6017 (unsigned int) lec, (unsigned int) b_nvram);
6018 } else
6019 vdbg_printk(TPACPI_DBG_BRGHT,
6020 "NVRAM backlight level already is %u (0x%02x)\n",
6021 (unsigned int) lec, (unsigned int) b_nvram);
6023 unlock:
6024 mutex_unlock(&brightness_mutex);
6028 /* call with brightness_mutex held! */
6029 static int tpacpi_brightness_get_raw(int *status)
6031 u8 lec = 0;
6033 switch (brightness_mode) {
6034 case TPACPI_BRGHT_MODE_UCMS_STEP:
6035 *status = tpacpi_brightness_nvram_get();
6036 return 0;
6037 case TPACPI_BRGHT_MODE_EC:
6038 case TPACPI_BRGHT_MODE_ECNVRAM:
6039 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6040 return -EIO;
6041 *status = lec;
6042 return 0;
6043 default:
6044 return -ENXIO;
6048 /* call with brightness_mutex held! */
6049 /* do NOT call with illegal backlight level value */
6050 static int tpacpi_brightness_set_ec(unsigned int value)
6052 u8 lec = 0;
6054 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6055 return -EIO;
6057 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6058 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6059 (value & TP_EC_BACKLIGHT_LVLMSK))))
6060 return -EIO;
6062 return 0;
6065 /* call with brightness_mutex held! */
6066 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6068 int cmos_cmd, inc;
6069 unsigned int current_value, i;
6071 current_value = tpacpi_brightness_nvram_get();
6073 if (value == current_value)
6074 return 0;
6076 cmos_cmd = (value > current_value) ?
6077 TP_CMOS_BRIGHTNESS_UP :
6078 TP_CMOS_BRIGHTNESS_DOWN;
6079 inc = (value > current_value) ? 1 : -1;
6081 for (i = current_value; i != value; i += inc)
6082 if (issue_thinkpad_cmos_command(cmos_cmd))
6083 return -EIO;
6085 return 0;
6088 /* May return EINTR which can always be mapped to ERESTARTSYS */
6089 static int brightness_set(unsigned int value)
6091 int res;
6093 if (value > bright_maxlvl || value < 0)
6094 return -EINVAL;
6096 vdbg_printk(TPACPI_DBG_BRGHT,
6097 "set backlight level to %d\n", value);
6099 res = mutex_lock_killable(&brightness_mutex);
6100 if (res < 0)
6101 return res;
6103 switch (brightness_mode) {
6104 case TPACPI_BRGHT_MODE_EC:
6105 case TPACPI_BRGHT_MODE_ECNVRAM:
6106 res = tpacpi_brightness_set_ec(value);
6107 break;
6108 case TPACPI_BRGHT_MODE_UCMS_STEP:
6109 res = tpacpi_brightness_set_ucmsstep(value);
6110 break;
6111 default:
6112 res = -ENXIO;
6115 mutex_unlock(&brightness_mutex);
6116 return res;
6119 /* sysfs backlight class ----------------------------------------------- */
6121 static int brightness_update_status(struct backlight_device *bd)
6123 unsigned int level =
6124 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6125 bd->props.power == FB_BLANK_UNBLANK) ?
6126 bd->props.brightness : 0;
6128 dbg_printk(TPACPI_DBG_BRGHT,
6129 "backlight: attempt to set level to %d\n",
6130 level);
6132 /* it is the backlight class's job (caller) to handle
6133 * EINTR and other errors properly */
6134 return brightness_set(level);
6137 static int brightness_get(struct backlight_device *bd)
6139 int status, res;
6141 res = mutex_lock_killable(&brightness_mutex);
6142 if (res < 0)
6143 return 0;
6145 res = tpacpi_brightness_get_raw(&status);
6147 mutex_unlock(&brightness_mutex);
6149 if (res < 0)
6150 return 0;
6152 return status & TP_EC_BACKLIGHT_LVLMSK;
6155 static void tpacpi_brightness_notify_change(void)
6157 backlight_force_update(ibm_backlight_device,
6158 BACKLIGHT_UPDATE_HOTKEY);
6161 static struct backlight_ops ibm_backlight_data = {
6162 .get_brightness = brightness_get,
6163 .update_status = brightness_update_status,
6166 /* --------------------------------------------------------------------- */
6169 * Call _BCL method of video device. On some ThinkPads this will
6170 * switch the firmware to the ACPI brightness control mode.
6173 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6175 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6176 union acpi_object *obj;
6177 int rc;
6179 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6180 obj = (union acpi_object *)buffer.pointer;
6181 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6182 printk(TPACPI_ERR "Unknown _BCL data, "
6183 "please report this to %s\n", TPACPI_MAIL);
6184 rc = 0;
6185 } else {
6186 rc = obj->package.count;
6188 } else {
6189 return 0;
6192 kfree(buffer.pointer);
6193 return rc;
6198 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6200 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6202 acpi_handle video_device;
6203 int bcl_levels = 0;
6205 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6206 if (video_device)
6207 bcl_levels = tpacpi_query_bcl_levels(video_device);
6209 tp_features.bright_acpimode = (bcl_levels > 0);
6211 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6215 * These are only useful for models that have only one possibility
6216 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6217 * these quirks.
6219 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6220 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6221 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6223 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6224 /* Models with ATI GPUs known to require ECNVRAM mode */
6225 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6227 /* Models with ATI GPUs that can use ECNVRAM */
6228 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6229 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6230 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6231 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6233 /* Models with Intel Extreme Graphics 2 */
6234 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6235 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6236 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6238 /* Models with Intel GMA900 */
6239 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6240 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6241 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6245 * Returns < 0 for error, otherwise sets tp_features.bright_*
6246 * and bright_maxlvl.
6248 static void __init tpacpi_detect_brightness_capabilities(void)
6250 unsigned int b;
6252 vdbg_printk(TPACPI_DBG_INIT,
6253 "detecting firmware brightness interface capabilities\n");
6255 /* we could run a quirks check here (same table used by
6256 * brightness_init) if needed */
6259 * We always attempt to detect acpi support, so as to switch
6260 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6261 * going to publish a backlight interface
6263 b = tpacpi_check_std_acpi_brightness_support();
6264 switch (b) {
6265 case 16:
6266 bright_maxlvl = 15;
6267 printk(TPACPI_INFO
6268 "detected a 16-level brightness capable ThinkPad\n");
6269 break;
6270 case 8:
6271 case 0:
6272 bright_maxlvl = 7;
6273 printk(TPACPI_INFO
6274 "detected a 8-level brightness capable ThinkPad\n");
6275 break;
6276 default:
6277 printk(TPACPI_ERR
6278 "Unsupported brightness interface, "
6279 "please contact %s\n", TPACPI_MAIL);
6280 tp_features.bright_unkfw = 1;
6281 bright_maxlvl = b - 1;
6285 static int __init brightness_init(struct ibm_init_struct *iibm)
6287 int b;
6288 unsigned long quirks;
6290 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6292 mutex_init(&brightness_mutex);
6294 quirks = tpacpi_check_quirks(brightness_quirk_table,
6295 ARRAY_SIZE(brightness_quirk_table));
6297 /* tpacpi_detect_brightness_capabilities() must have run already */
6299 /* if it is unknown, we don't handle it: it wouldn't be safe */
6300 if (tp_features.bright_unkfw)
6301 return 1;
6303 if (!brightness_enable) {
6304 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6305 "brightness support disabled by "
6306 "module parameter\n");
6307 return 1;
6310 if (acpi_video_backlight_support()) {
6311 if (brightness_enable > 1) {
6312 printk(TPACPI_INFO
6313 "Standard ACPI backlight interface "
6314 "available, not loading native one.\n");
6315 return 1;
6316 } else if (brightness_enable == 1) {
6317 printk(TPACPI_WARN
6318 "Cannot enable backlight brightness support, "
6319 "ACPI is already handling it. Refer to the "
6320 "acpi_backlight kernel parameter\n");
6321 return 1;
6323 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6324 printk(TPACPI_NOTICE
6325 "Standard ACPI backlight interface not "
6326 "available, thinkpad_acpi native "
6327 "brightness control enabled\n");
6331 * Check for module parameter bogosity, note that we
6332 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6333 * able to detect "unspecified"
6335 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6336 return -EINVAL;
6338 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6339 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6340 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6341 if (quirks & TPACPI_BRGHT_Q_EC)
6342 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6343 else
6344 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6346 dbg_printk(TPACPI_DBG_BRGHT,
6347 "driver auto-selected brightness_mode=%d\n",
6348 brightness_mode);
6351 /* Safety */
6352 if (!tpacpi_is_ibm() &&
6353 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6354 brightness_mode == TPACPI_BRGHT_MODE_EC))
6355 return -EINVAL;
6357 if (tpacpi_brightness_get_raw(&b) < 0)
6358 return 1;
6360 ibm_backlight_device = backlight_device_register(
6361 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6362 &ibm_backlight_data);
6363 if (IS_ERR(ibm_backlight_device)) {
6364 int rc = PTR_ERR(ibm_backlight_device);
6365 ibm_backlight_device = NULL;
6366 printk(TPACPI_ERR "Could not register backlight device\n");
6367 return rc;
6369 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6370 "brightness is supported\n");
6372 if (quirks & TPACPI_BRGHT_Q_ASK) {
6373 printk(TPACPI_NOTICE
6374 "brightness: will use unverified default: "
6375 "brightness_mode=%d\n", brightness_mode);
6376 printk(TPACPI_NOTICE
6377 "brightness: please report to %s whether it works well "
6378 "or not on your ThinkPad\n", TPACPI_MAIL);
6381 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6382 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6384 /* Added by mistake in early 2007. Probably useless, but it could
6385 * be working around some unknown firmware problem where the value
6386 * read at startup doesn't match the real hardware state... so leave
6387 * it in place just in case */
6388 backlight_update_status(ibm_backlight_device);
6390 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6391 "brightness: registering brightness hotkeys "
6392 "as change notification\n");
6393 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6394 | TP_ACPI_HKEY_BRGHTUP_MASK
6395 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6396 return 0;
6399 static void brightness_suspend(pm_message_t state)
6401 tpacpi_brightness_checkpoint_nvram();
6404 static void brightness_shutdown(void)
6406 tpacpi_brightness_checkpoint_nvram();
6409 static void brightness_exit(void)
6411 if (ibm_backlight_device) {
6412 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6413 "calling backlight_device_unregister()\n");
6414 backlight_device_unregister(ibm_backlight_device);
6417 tpacpi_brightness_checkpoint_nvram();
6420 static int brightness_read(struct seq_file *m)
6422 int level;
6424 level = brightness_get(NULL);
6425 if (level < 0) {
6426 seq_printf(m, "level:\t\tunreadable\n");
6427 } else {
6428 seq_printf(m, "level:\t\t%d\n", level);
6429 seq_printf(m, "commands:\tup, down\n");
6430 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6431 bright_maxlvl);
6434 return 0;
6437 static int brightness_write(char *buf)
6439 int level;
6440 int rc;
6441 char *cmd;
6443 level = brightness_get(NULL);
6444 if (level < 0)
6445 return level;
6447 while ((cmd = next_cmd(&buf))) {
6448 if (strlencmp(cmd, "up") == 0) {
6449 if (level < bright_maxlvl)
6450 level++;
6451 } else if (strlencmp(cmd, "down") == 0) {
6452 if (level > 0)
6453 level--;
6454 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6455 level >= 0 && level <= bright_maxlvl) {
6456 /* new level set */
6457 } else
6458 return -EINVAL;
6461 tpacpi_disclose_usertask("procfs brightness",
6462 "set level to %d\n", level);
6465 * Now we know what the final level should be, so we try to set it.
6466 * Doing it this way makes the syscall restartable in case of EINTR
6468 rc = brightness_set(level);
6469 if (!rc && ibm_backlight_device)
6470 backlight_force_update(ibm_backlight_device,
6471 BACKLIGHT_UPDATE_SYSFS);
6472 return (rc == -EINTR)? -ERESTARTSYS : rc;
6475 static struct ibm_struct brightness_driver_data = {
6476 .name = "brightness",
6477 .read = brightness_read,
6478 .write = brightness_write,
6479 .exit = brightness_exit,
6480 .suspend = brightness_suspend,
6481 .shutdown = brightness_shutdown,
6484 /*************************************************************************
6485 * Volume subdriver
6489 * IBM ThinkPads have a simple volume controller with MUTE gating.
6490 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6492 * Since the *61 series (and probably also the later *60 series), Lenovo
6493 * ThinkPads only implement the MUTE gate.
6495 * EC register 0x30
6496 * Bit 6: MUTE (1 mutes sound)
6497 * Bit 3-0: Volume
6498 * Other bits should be zero as far as we know.
6500 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6501 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6502 * such as bit 7 which is used to detect repeated presses of MUTE,
6503 * and we leave them unchanged.
6506 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6508 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6509 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6510 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6512 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6513 static char *alsa_id = "ThinkPadEC";
6514 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6516 struct tpacpi_alsa_data {
6517 struct snd_card *card;
6518 struct snd_ctl_elem_id *ctl_mute_id;
6519 struct snd_ctl_elem_id *ctl_vol_id;
6522 static struct snd_card *alsa_card;
6524 enum {
6525 TP_EC_AUDIO = 0x30,
6527 /* TP_EC_AUDIO bits */
6528 TP_EC_AUDIO_MUTESW = 6,
6530 /* TP_EC_AUDIO bitmasks */
6531 TP_EC_AUDIO_LVL_MSK = 0x0F,
6532 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6534 /* Maximum volume */
6535 TP_EC_VOLUME_MAX = 14,
6538 enum tpacpi_volume_access_mode {
6539 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6540 TPACPI_VOL_MODE_EC, /* Pure EC control */
6541 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6542 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6543 TPACPI_VOL_MODE_MAX
6546 enum tpacpi_volume_capabilities {
6547 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6548 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6549 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6550 TPACPI_VOL_CAP_MAX
6553 static enum tpacpi_volume_access_mode volume_mode =
6554 TPACPI_VOL_MODE_MAX;
6556 static enum tpacpi_volume_capabilities volume_capabilities;
6557 static int volume_control_allowed;
6560 * Used to syncronize writers to TP_EC_AUDIO and
6561 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6563 static struct mutex volume_mutex;
6565 static void tpacpi_volume_checkpoint_nvram(void)
6567 u8 lec = 0;
6568 u8 b_nvram;
6569 u8 ec_mask;
6571 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6572 return;
6573 if (!volume_control_allowed)
6574 return;
6576 vdbg_printk(TPACPI_DBG_MIXER,
6577 "trying to checkpoint mixer state to NVRAM...\n");
6579 if (tp_features.mixer_no_level_control)
6580 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6581 else
6582 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6584 if (mutex_lock_killable(&volume_mutex) < 0)
6585 return;
6587 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6588 goto unlock;
6589 lec &= ec_mask;
6590 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6592 if (lec != (b_nvram & ec_mask)) {
6593 /* NVRAM needs update */
6594 b_nvram &= ~ec_mask;
6595 b_nvram |= lec;
6596 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6597 dbg_printk(TPACPI_DBG_MIXER,
6598 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6599 (unsigned int) lec, (unsigned int) b_nvram);
6600 } else {
6601 vdbg_printk(TPACPI_DBG_MIXER,
6602 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6603 (unsigned int) lec, (unsigned int) b_nvram);
6606 unlock:
6607 mutex_unlock(&volume_mutex);
6610 static int volume_get_status_ec(u8 *status)
6612 u8 s;
6614 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6615 return -EIO;
6617 *status = s;
6619 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6621 return 0;
6624 static int volume_get_status(u8 *status)
6626 return volume_get_status_ec(status);
6629 static int volume_set_status_ec(const u8 status)
6631 if (!acpi_ec_write(TP_EC_AUDIO, status))
6632 return -EIO;
6634 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6636 return 0;
6639 static int volume_set_status(const u8 status)
6641 return volume_set_status_ec(status);
6644 /* returns < 0 on error, 0 on no change, 1 on change */
6645 static int __volume_set_mute_ec(const bool mute)
6647 int rc;
6648 u8 s, n;
6650 if (mutex_lock_killable(&volume_mutex) < 0)
6651 return -EINTR;
6653 rc = volume_get_status_ec(&s);
6654 if (rc)
6655 goto unlock;
6657 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6658 s & ~TP_EC_AUDIO_MUTESW_MSK;
6660 if (n != s) {
6661 rc = volume_set_status_ec(n);
6662 if (!rc)
6663 rc = 1;
6666 unlock:
6667 mutex_unlock(&volume_mutex);
6668 return rc;
6671 static int volume_alsa_set_mute(const bool mute)
6673 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6674 (mute) ? "" : "un");
6675 return __volume_set_mute_ec(mute);
6678 static int volume_set_mute(const bool mute)
6680 int rc;
6682 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6683 (mute) ? "" : "un");
6685 rc = __volume_set_mute_ec(mute);
6686 return (rc < 0) ? rc : 0;
6689 /* returns < 0 on error, 0 on no change, 1 on change */
6690 static int __volume_set_volume_ec(const u8 vol)
6692 int rc;
6693 u8 s, n;
6695 if (vol > TP_EC_VOLUME_MAX)
6696 return -EINVAL;
6698 if (mutex_lock_killable(&volume_mutex) < 0)
6699 return -EINTR;
6701 rc = volume_get_status_ec(&s);
6702 if (rc)
6703 goto unlock;
6705 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6707 if (n != s) {
6708 rc = volume_set_status_ec(n);
6709 if (!rc)
6710 rc = 1;
6713 unlock:
6714 mutex_unlock(&volume_mutex);
6715 return rc;
6718 static int volume_alsa_set_volume(const u8 vol)
6720 dbg_printk(TPACPI_DBG_MIXER,
6721 "ALSA: trying to set volume level to %hu\n", vol);
6722 return __volume_set_volume_ec(vol);
6725 static void volume_alsa_notify_change(void)
6727 struct tpacpi_alsa_data *d;
6729 if (alsa_card && alsa_card->private_data) {
6730 d = alsa_card->private_data;
6731 if (d->ctl_mute_id)
6732 snd_ctl_notify(alsa_card,
6733 SNDRV_CTL_EVENT_MASK_VALUE,
6734 d->ctl_mute_id);
6735 if (d->ctl_vol_id)
6736 snd_ctl_notify(alsa_card,
6737 SNDRV_CTL_EVENT_MASK_VALUE,
6738 d->ctl_vol_id);
6742 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6743 struct snd_ctl_elem_info *uinfo)
6745 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6746 uinfo->count = 1;
6747 uinfo->value.integer.min = 0;
6748 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6749 return 0;
6752 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6753 struct snd_ctl_elem_value *ucontrol)
6755 u8 s;
6756 int rc;
6758 rc = volume_get_status(&s);
6759 if (rc < 0)
6760 return rc;
6762 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6763 return 0;
6766 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6767 struct snd_ctl_elem_value *ucontrol)
6769 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6770 ucontrol->value.integer.value[0]);
6771 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6774 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6776 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6777 struct snd_ctl_elem_value *ucontrol)
6779 u8 s;
6780 int rc;
6782 rc = volume_get_status(&s);
6783 if (rc < 0)
6784 return rc;
6786 ucontrol->value.integer.value[0] =
6787 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6788 return 0;
6791 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6792 struct snd_ctl_elem_value *ucontrol)
6794 tpacpi_disclose_usertask("ALSA", "%smute\n",
6795 ucontrol->value.integer.value[0] ?
6796 "un" : "");
6797 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6800 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6801 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6802 .name = "Console Playback Volume",
6803 .index = 0,
6804 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6805 .info = volume_alsa_vol_info,
6806 .get = volume_alsa_vol_get,
6809 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6810 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6811 .name = "Console Playback Switch",
6812 .index = 0,
6813 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6814 .info = volume_alsa_mute_info,
6815 .get = volume_alsa_mute_get,
6818 static void volume_suspend(pm_message_t state)
6820 tpacpi_volume_checkpoint_nvram();
6823 static void volume_resume(void)
6825 volume_alsa_notify_change();
6828 static void volume_shutdown(void)
6830 tpacpi_volume_checkpoint_nvram();
6833 static void volume_exit(void)
6835 if (alsa_card) {
6836 snd_card_free(alsa_card);
6837 alsa_card = NULL;
6840 tpacpi_volume_checkpoint_nvram();
6843 static int __init volume_create_alsa_mixer(void)
6845 struct snd_card *card;
6846 struct tpacpi_alsa_data *data;
6847 struct snd_kcontrol *ctl_vol;
6848 struct snd_kcontrol *ctl_mute;
6849 int rc;
6851 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6852 sizeof(struct tpacpi_alsa_data), &card);
6853 if (rc < 0 || !card) {
6854 printk(TPACPI_ERR
6855 "Failed to create ALSA card structures: %d\n", rc);
6856 return 1;
6859 BUG_ON(!card->private_data);
6860 data = card->private_data;
6861 data->card = card;
6863 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6864 sizeof(card->driver));
6865 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6866 sizeof(card->shortname));
6867 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6868 (thinkpad_id.ec_version_str) ?
6869 thinkpad_id.ec_version_str : "(unknown)");
6870 snprintf(card->longname, sizeof(card->longname),
6871 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6872 (thinkpad_id.ec_version_str) ?
6873 thinkpad_id.ec_version_str : "unknown");
6875 if (volume_control_allowed) {
6876 volume_alsa_control_vol.put = volume_alsa_vol_put;
6877 volume_alsa_control_vol.access =
6878 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6880 volume_alsa_control_mute.put = volume_alsa_mute_put;
6881 volume_alsa_control_mute.access =
6882 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6885 if (!tp_features.mixer_no_level_control) {
6886 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6887 rc = snd_ctl_add(card, ctl_vol);
6888 if (rc < 0) {
6889 printk(TPACPI_ERR
6890 "Failed to create ALSA volume control: %d\n",
6891 rc);
6892 goto err_exit;
6894 data->ctl_vol_id = &ctl_vol->id;
6897 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6898 rc = snd_ctl_add(card, ctl_mute);
6899 if (rc < 0) {
6900 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6901 rc);
6902 goto err_exit;
6904 data->ctl_mute_id = &ctl_mute->id;
6906 snd_card_set_dev(card, &tpacpi_pdev->dev);
6907 rc = snd_card_register(card);
6908 if (rc < 0) {
6909 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6910 goto err_exit;
6913 alsa_card = card;
6914 return 0;
6916 err_exit:
6917 snd_card_free(card);
6918 return 1;
6921 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6922 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6924 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6925 /* Whitelist volume level on all IBM by default */
6926 { .vendor = PCI_VENDOR_ID_IBM,
6927 .bios = TPACPI_MATCH_ANY,
6928 .ec = TPACPI_MATCH_ANY,
6929 .quirks = TPACPI_VOL_Q_LEVEL },
6931 /* Lenovo models with volume control (needs confirmation) */
6932 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6933 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6934 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6935 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6936 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6937 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6938 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6940 /* Whitelist mute-only on all Lenovo by default */
6941 { .vendor = PCI_VENDOR_ID_LENOVO,
6942 .bios = TPACPI_MATCH_ANY,
6943 .ec = TPACPI_MATCH_ANY,
6944 .quirks = TPACPI_VOL_Q_MUTEONLY }
6947 static int __init volume_init(struct ibm_init_struct *iibm)
6949 unsigned long quirks;
6950 int rc;
6952 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6954 mutex_init(&volume_mutex);
6957 * Check for module parameter bogosity, note that we
6958 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6959 * able to detect "unspecified"
6961 if (volume_mode > TPACPI_VOL_MODE_MAX)
6962 return -EINVAL;
6964 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6965 printk(TPACPI_ERR
6966 "UCMS step volume mode not implemented, "
6967 "please contact %s\n", TPACPI_MAIL);
6968 return 1;
6971 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6972 return -EINVAL;
6975 * The ALSA mixer is our primary interface.
6976 * When disabled, don't install the subdriver at all
6978 if (!alsa_enable) {
6979 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6980 "ALSA mixer disabled by parameter, "
6981 "not loading volume subdriver...\n");
6982 return 1;
6985 quirks = tpacpi_check_quirks(volume_quirk_table,
6986 ARRAY_SIZE(volume_quirk_table));
6988 switch (volume_capabilities) {
6989 case TPACPI_VOL_CAP_AUTO:
6990 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6991 tp_features.mixer_no_level_control = 1;
6992 else if (quirks & TPACPI_VOL_Q_LEVEL)
6993 tp_features.mixer_no_level_control = 0;
6994 else
6995 return 1; /* no mixer */
6996 break;
6997 case TPACPI_VOL_CAP_VOLMUTE:
6998 tp_features.mixer_no_level_control = 0;
6999 break;
7000 case TPACPI_VOL_CAP_MUTEONLY:
7001 tp_features.mixer_no_level_control = 1;
7002 break;
7003 default:
7004 return 1;
7007 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7008 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7009 "using user-supplied volume_capabilities=%d\n",
7010 volume_capabilities);
7012 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7013 volume_mode == TPACPI_VOL_MODE_MAX) {
7014 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7016 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7017 "driver auto-selected volume_mode=%d\n",
7018 volume_mode);
7019 } else {
7020 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7021 "using user-supplied volume_mode=%d\n",
7022 volume_mode);
7025 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7026 "mute is supported, volume control is %s\n",
7027 str_supported(!tp_features.mixer_no_level_control));
7029 rc = volume_create_alsa_mixer();
7030 if (rc) {
7031 printk(TPACPI_ERR
7032 "Could not create the ALSA mixer interface\n");
7033 return rc;
7036 printk(TPACPI_INFO
7037 "Console audio control enabled, mode: %s\n",
7038 (volume_control_allowed) ?
7039 "override (read/write)" :
7040 "monitor (read only)");
7042 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7043 "registering volume hotkeys as change notification\n");
7044 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7045 | TP_ACPI_HKEY_VOLUP_MASK
7046 | TP_ACPI_HKEY_VOLDWN_MASK
7047 | TP_ACPI_HKEY_MUTE_MASK);
7049 return 0;
7052 static int volume_read(struct seq_file *m)
7054 u8 status;
7056 if (volume_get_status(&status) < 0) {
7057 seq_printf(m, "level:\t\tunreadable\n");
7058 } else {
7059 if (tp_features.mixer_no_level_control)
7060 seq_printf(m, "level:\t\tunsupported\n");
7061 else
7062 seq_printf(m, "level:\t\t%d\n",
7063 status & TP_EC_AUDIO_LVL_MSK);
7065 seq_printf(m, "mute:\t\t%s\n",
7066 onoff(status, TP_EC_AUDIO_MUTESW));
7068 if (volume_control_allowed) {
7069 seq_printf(m, "commands:\tunmute, mute\n");
7070 if (!tp_features.mixer_no_level_control) {
7071 seq_printf(m,
7072 "commands:\tup, down\n");
7073 seq_printf(m,
7074 "commands:\tlevel <level>"
7075 " (<level> is 0-%d)\n",
7076 TP_EC_VOLUME_MAX);
7081 return 0;
7084 static int volume_write(char *buf)
7086 u8 s;
7087 u8 new_level, new_mute;
7088 int l;
7089 char *cmd;
7090 int rc;
7093 * We do allow volume control at driver startup, so that the
7094 * user can set initial state through the volume=... parameter hack.
7096 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7097 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7098 tp_warned.volume_ctrl_forbidden = 1;
7099 printk(TPACPI_NOTICE
7100 "Console audio control in monitor mode, "
7101 "changes are not allowed.\n");
7102 printk(TPACPI_NOTICE
7103 "Use the volume_control=1 module parameter "
7104 "to enable volume control\n");
7106 return -EPERM;
7109 rc = volume_get_status(&s);
7110 if (rc < 0)
7111 return rc;
7113 new_level = s & TP_EC_AUDIO_LVL_MSK;
7114 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7116 while ((cmd = next_cmd(&buf))) {
7117 if (!tp_features.mixer_no_level_control) {
7118 if (strlencmp(cmd, "up") == 0) {
7119 if (new_mute)
7120 new_mute = 0;
7121 else if (new_level < TP_EC_VOLUME_MAX)
7122 new_level++;
7123 continue;
7124 } else if (strlencmp(cmd, "down") == 0) {
7125 if (new_mute)
7126 new_mute = 0;
7127 else if (new_level > 0)
7128 new_level--;
7129 continue;
7130 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7131 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7132 new_level = l;
7133 continue;
7136 if (strlencmp(cmd, "mute") == 0)
7137 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7138 else if (strlencmp(cmd, "unmute") == 0)
7139 new_mute = 0;
7140 else
7141 return -EINVAL;
7144 if (tp_features.mixer_no_level_control) {
7145 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7146 new_mute ? "" : "un");
7147 rc = volume_set_mute(!!new_mute);
7148 } else {
7149 tpacpi_disclose_usertask("procfs volume",
7150 "%smute and set level to %d\n",
7151 new_mute ? "" : "un", new_level);
7152 rc = volume_set_status(new_mute | new_level);
7154 volume_alsa_notify_change();
7156 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7159 static struct ibm_struct volume_driver_data = {
7160 .name = "volume",
7161 .read = volume_read,
7162 .write = volume_write,
7163 .exit = volume_exit,
7164 .suspend = volume_suspend,
7165 .resume = volume_resume,
7166 .shutdown = volume_shutdown,
7169 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7171 #define alsa_card NULL
7173 static void inline volume_alsa_notify_change(void)
7177 static int __init volume_init(struct ibm_init_struct *iibm)
7179 printk(TPACPI_INFO
7180 "volume: disabled as there is no ALSA support in this kernel\n");
7182 return 1;
7185 static struct ibm_struct volume_driver_data = {
7186 .name = "volume",
7189 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7191 /*************************************************************************
7192 * Fan subdriver
7196 * FAN ACCESS MODES
7198 * TPACPI_FAN_RD_ACPI_GFAN:
7199 * ACPI GFAN method: returns fan level
7201 * see TPACPI_FAN_WR_ACPI_SFAN
7202 * EC 0x2f (HFSP) not available if GFAN exists
7204 * TPACPI_FAN_WR_ACPI_SFAN:
7205 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7207 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7208 * it for writing.
7210 * TPACPI_FAN_WR_TPEC:
7211 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7212 * Supported on almost all ThinkPads
7214 * Fan speed changes of any sort (including those caused by the
7215 * disengaged mode) are usually done slowly by the firmware as the
7216 * maximum amount of fan duty cycle change per second seems to be
7217 * limited.
7219 * Reading is not available if GFAN exists.
7220 * Writing is not available if SFAN exists.
7222 * Bits
7223 * 7 automatic mode engaged;
7224 * (default operation mode of the ThinkPad)
7225 * fan level is ignored in this mode.
7226 * 6 full speed mode (takes precedence over bit 7);
7227 * not available on all thinkpads. May disable
7228 * the tachometer while the fan controller ramps up
7229 * the speed (which can take up to a few *minutes*).
7230 * Speeds up fan to 100% duty-cycle, which is far above
7231 * the standard RPM levels. It is not impossible that
7232 * it could cause hardware damage.
7233 * 5-3 unused in some models. Extra bits for fan level
7234 * in others, but still useless as all values above
7235 * 7 map to the same speed as level 7 in these models.
7236 * 2-0 fan level (0..7 usually)
7237 * 0x00 = stop
7238 * 0x07 = max (set when temperatures critical)
7239 * Some ThinkPads may have other levels, see
7240 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7242 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7243 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7244 * does so, its initial value is meaningless (0x07).
7246 * For firmware bugs, refer to:
7247 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7249 * ----
7251 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7252 * Main fan tachometer reading (in RPM)
7254 * This register is present on all ThinkPads with a new-style EC, and
7255 * it is known not to be present on the A21m/e, and T22, as there is
7256 * something else in offset 0x84 according to the ACPI DSDT. Other
7257 * ThinkPads from this same time period (and earlier) probably lack the
7258 * tachometer as well.
7260 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7261 * was never fixed by IBM to report the EC firmware version string
7262 * probably support the tachometer (like the early X models), so
7263 * detecting it is quite hard. We need more data to know for sure.
7265 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7266 * might result.
7268 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7269 * mode.
7271 * For firmware bugs, refer to:
7272 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7274 * ----
7276 * ThinkPad EC register 0x31 bit 0 (only on select models)
7278 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7279 * show the speed of the main fan. When bit 0 of EC register 0x31
7280 * is one, the tachometer registers show the speed of the auxiliary
7281 * fan.
7283 * Fan control seems to affect both fans, regardless of the state
7284 * of this bit.
7286 * So far, only the firmware for the X60/X61 non-tablet versions
7287 * seem to support this (firmware TP-7M).
7289 * TPACPI_FAN_WR_ACPI_FANS:
7290 * ThinkPad X31, X40, X41. Not available in the X60.
7292 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7293 * high speed. ACPI DSDT seems to map these three speeds to levels
7294 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7295 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7297 * The speeds are stored on handles
7298 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7300 * There are three default speed sets, accessible as handles:
7301 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7303 * ACPI DSDT switches which set is in use depending on various
7304 * factors.
7306 * TPACPI_FAN_WR_TPEC is also available and should be used to
7307 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7308 * but the ACPI tables just mention level 7.
7311 enum { /* Fan control constants */
7312 fan_status_offset = 0x2f, /* EC register 0x2f */
7313 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7314 * 0x84 must be read before 0x85 */
7315 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7316 bit 0 selects which fan is active */
7318 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7319 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7321 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7324 enum fan_status_access_mode {
7325 TPACPI_FAN_NONE = 0, /* No fan status or control */
7326 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7327 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7330 enum fan_control_access_mode {
7331 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7332 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7333 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7334 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7337 enum fan_control_commands {
7338 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7339 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7340 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7341 * and also watchdog cmd */
7344 static int fan_control_allowed;
7346 static enum fan_status_access_mode fan_status_access_mode;
7347 static enum fan_control_access_mode fan_control_access_mode;
7348 static enum fan_control_commands fan_control_commands;
7350 static u8 fan_control_initial_status;
7351 static u8 fan_control_desired_level;
7352 static u8 fan_control_resume_level;
7353 static int fan_watchdog_maxinterval;
7355 static struct mutex fan_mutex;
7357 static void fan_watchdog_fire(struct work_struct *ignored);
7358 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7360 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7361 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7362 "\\FSPD", /* 600e/x, 770e, 770x */
7363 ); /* all others */
7364 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7365 "JFNS", /* 770x-JL */
7366 ); /* all others */
7369 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7370 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7371 * be in auto mode (0x80).
7373 * This is corrected by any write to HFSP either by the driver, or
7374 * by the firmware.
7376 * We assume 0x07 really means auto mode while this quirk is active,
7377 * as this is far more likely than the ThinkPad being in level 7,
7378 * which is only used by the firmware during thermal emergencies.
7380 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7381 * TP-70 (T43, R52), which are known to be buggy.
7384 static void fan_quirk1_setup(void)
7386 if (fan_control_initial_status == 0x07) {
7387 printk(TPACPI_NOTICE
7388 "fan_init: initial fan status is unknown, "
7389 "assuming it is in auto mode\n");
7390 tp_features.fan_ctrl_status_undef = 1;
7394 static void fan_quirk1_handle(u8 *fan_status)
7396 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7397 if (*fan_status != fan_control_initial_status) {
7398 /* something changed the HFSP regisnter since
7399 * driver init time, so it is not undefined
7400 * anymore */
7401 tp_features.fan_ctrl_status_undef = 0;
7402 } else {
7403 /* Return most likely status. In fact, it
7404 * might be the only possible status */
7405 *fan_status = TP_EC_FAN_AUTO;
7410 /* Select main fan on X60/X61, NOOP on others */
7411 static bool fan_select_fan1(void)
7413 if (tp_features.second_fan) {
7414 u8 val;
7416 if (ec_read(fan_select_offset, &val) < 0)
7417 return false;
7418 val &= 0xFEU;
7419 if (ec_write(fan_select_offset, val) < 0)
7420 return false;
7422 return true;
7425 /* Select secondary fan on X60/X61 */
7426 static bool fan_select_fan2(void)
7428 u8 val;
7430 if (!tp_features.second_fan)
7431 return false;
7433 if (ec_read(fan_select_offset, &val) < 0)
7434 return false;
7435 val |= 0x01U;
7436 if (ec_write(fan_select_offset, val) < 0)
7437 return false;
7439 return true;
7443 * Call with fan_mutex held
7445 static void fan_update_desired_level(u8 status)
7447 if ((status &
7448 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7449 if (status > 7)
7450 fan_control_desired_level = 7;
7451 else
7452 fan_control_desired_level = status;
7456 static int fan_get_status(u8 *status)
7458 u8 s;
7460 /* TODO:
7461 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7463 switch (fan_status_access_mode) {
7464 case TPACPI_FAN_RD_ACPI_GFAN:
7465 /* 570, 600e/x, 770e, 770x */
7467 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7468 return -EIO;
7470 if (likely(status))
7471 *status = s & 0x07;
7473 break;
7475 case TPACPI_FAN_RD_TPEC:
7476 /* all except 570, 600e/x, 770e, 770x */
7477 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7478 return -EIO;
7480 if (likely(status)) {
7481 *status = s;
7482 fan_quirk1_handle(status);
7485 break;
7487 default:
7488 return -ENXIO;
7491 return 0;
7494 static int fan_get_status_safe(u8 *status)
7496 int rc;
7497 u8 s;
7499 if (mutex_lock_killable(&fan_mutex))
7500 return -ERESTARTSYS;
7501 rc = fan_get_status(&s);
7502 if (!rc)
7503 fan_update_desired_level(s);
7504 mutex_unlock(&fan_mutex);
7506 if (status)
7507 *status = s;
7509 return rc;
7512 static int fan_get_speed(unsigned int *speed)
7514 u8 hi, lo;
7516 switch (fan_status_access_mode) {
7517 case TPACPI_FAN_RD_TPEC:
7518 /* all except 570, 600e/x, 770e, 770x */
7519 if (unlikely(!fan_select_fan1()))
7520 return -EIO;
7521 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7522 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7523 return -EIO;
7525 if (likely(speed))
7526 *speed = (hi << 8) | lo;
7528 break;
7530 default:
7531 return -ENXIO;
7534 return 0;
7537 static int fan2_get_speed(unsigned int *speed)
7539 u8 hi, lo;
7540 bool rc;
7542 switch (fan_status_access_mode) {
7543 case TPACPI_FAN_RD_TPEC:
7544 /* all except 570, 600e/x, 770e, 770x */
7545 if (unlikely(!fan_select_fan2()))
7546 return -EIO;
7547 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7548 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7549 fan_select_fan1(); /* play it safe */
7550 if (rc)
7551 return -EIO;
7553 if (likely(speed))
7554 *speed = (hi << 8) | lo;
7556 break;
7558 default:
7559 return -ENXIO;
7562 return 0;
7565 static int fan_set_level(int level)
7567 if (!fan_control_allowed)
7568 return -EPERM;
7570 switch (fan_control_access_mode) {
7571 case TPACPI_FAN_WR_ACPI_SFAN:
7572 if (level >= 0 && level <= 7) {
7573 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7574 return -EIO;
7575 } else
7576 return -EINVAL;
7577 break;
7579 case TPACPI_FAN_WR_ACPI_FANS:
7580 case TPACPI_FAN_WR_TPEC:
7581 if (!(level & TP_EC_FAN_AUTO) &&
7582 !(level & TP_EC_FAN_FULLSPEED) &&
7583 ((level < 0) || (level > 7)))
7584 return -EINVAL;
7586 /* safety net should the EC not support AUTO
7587 * or FULLSPEED mode bits and just ignore them */
7588 if (level & TP_EC_FAN_FULLSPEED)
7589 level |= 7; /* safety min speed 7 */
7590 else if (level & TP_EC_FAN_AUTO)
7591 level |= 4; /* safety min speed 4 */
7593 if (!acpi_ec_write(fan_status_offset, level))
7594 return -EIO;
7595 else
7596 tp_features.fan_ctrl_status_undef = 0;
7597 break;
7599 default:
7600 return -ENXIO;
7603 vdbg_printk(TPACPI_DBG_FAN,
7604 "fan control: set fan control register to 0x%02x\n", level);
7605 return 0;
7608 static int fan_set_level_safe(int level)
7610 int rc;
7612 if (!fan_control_allowed)
7613 return -EPERM;
7615 if (mutex_lock_killable(&fan_mutex))
7616 return -ERESTARTSYS;
7618 if (level == TPACPI_FAN_LAST_LEVEL)
7619 level = fan_control_desired_level;
7621 rc = fan_set_level(level);
7622 if (!rc)
7623 fan_update_desired_level(level);
7625 mutex_unlock(&fan_mutex);
7626 return rc;
7629 static int fan_set_enable(void)
7631 u8 s;
7632 int rc;
7634 if (!fan_control_allowed)
7635 return -EPERM;
7637 if (mutex_lock_killable(&fan_mutex))
7638 return -ERESTARTSYS;
7640 switch (fan_control_access_mode) {
7641 case TPACPI_FAN_WR_ACPI_FANS:
7642 case TPACPI_FAN_WR_TPEC:
7643 rc = fan_get_status(&s);
7644 if (rc < 0)
7645 break;
7647 /* Don't go out of emergency fan mode */
7648 if (s != 7) {
7649 s &= 0x07;
7650 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7653 if (!acpi_ec_write(fan_status_offset, s))
7654 rc = -EIO;
7655 else {
7656 tp_features.fan_ctrl_status_undef = 0;
7657 rc = 0;
7659 break;
7661 case TPACPI_FAN_WR_ACPI_SFAN:
7662 rc = fan_get_status(&s);
7663 if (rc < 0)
7664 break;
7666 s &= 0x07;
7668 /* Set fan to at least level 4 */
7669 s |= 4;
7671 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7672 rc = -EIO;
7673 else
7674 rc = 0;
7675 break;
7677 default:
7678 rc = -ENXIO;
7681 mutex_unlock(&fan_mutex);
7683 if (!rc)
7684 vdbg_printk(TPACPI_DBG_FAN,
7685 "fan control: set fan control register to 0x%02x\n",
7687 return rc;
7690 static int fan_set_disable(void)
7692 int rc;
7694 if (!fan_control_allowed)
7695 return -EPERM;
7697 if (mutex_lock_killable(&fan_mutex))
7698 return -ERESTARTSYS;
7700 rc = 0;
7701 switch (fan_control_access_mode) {
7702 case TPACPI_FAN_WR_ACPI_FANS:
7703 case TPACPI_FAN_WR_TPEC:
7704 if (!acpi_ec_write(fan_status_offset, 0x00))
7705 rc = -EIO;
7706 else {
7707 fan_control_desired_level = 0;
7708 tp_features.fan_ctrl_status_undef = 0;
7710 break;
7712 case TPACPI_FAN_WR_ACPI_SFAN:
7713 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7714 rc = -EIO;
7715 else
7716 fan_control_desired_level = 0;
7717 break;
7719 default:
7720 rc = -ENXIO;
7723 if (!rc)
7724 vdbg_printk(TPACPI_DBG_FAN,
7725 "fan control: set fan control register to 0\n");
7727 mutex_unlock(&fan_mutex);
7728 return rc;
7731 static int fan_set_speed(int speed)
7733 int rc;
7735 if (!fan_control_allowed)
7736 return -EPERM;
7738 if (mutex_lock_killable(&fan_mutex))
7739 return -ERESTARTSYS;
7741 rc = 0;
7742 switch (fan_control_access_mode) {
7743 case TPACPI_FAN_WR_ACPI_FANS:
7744 if (speed >= 0 && speed <= 65535) {
7745 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7746 speed, speed, speed))
7747 rc = -EIO;
7748 } else
7749 rc = -EINVAL;
7750 break;
7752 default:
7753 rc = -ENXIO;
7756 mutex_unlock(&fan_mutex);
7757 return rc;
7760 static void fan_watchdog_reset(void)
7762 static int fan_watchdog_active;
7764 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7765 return;
7767 if (fan_watchdog_active)
7768 cancel_delayed_work(&fan_watchdog_task);
7770 if (fan_watchdog_maxinterval > 0 &&
7771 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7772 fan_watchdog_active = 1;
7773 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7774 msecs_to_jiffies(fan_watchdog_maxinterval
7775 * 1000))) {
7776 printk(TPACPI_ERR
7777 "failed to queue the fan watchdog, "
7778 "watchdog will not trigger\n");
7780 } else
7781 fan_watchdog_active = 0;
7784 static void fan_watchdog_fire(struct work_struct *ignored)
7786 int rc;
7788 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7789 return;
7791 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7792 rc = fan_set_enable();
7793 if (rc < 0) {
7794 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7795 "will try again later...\n", -rc);
7796 /* reschedule for later */
7797 fan_watchdog_reset();
7802 * SYSFS fan layout: hwmon compatible (device)
7804 * pwm*_enable:
7805 * 0: "disengaged" mode
7806 * 1: manual mode
7807 * 2: native EC "auto" mode (recommended, hardware default)
7809 * pwm*: set speed in manual mode, ignored otherwise.
7810 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7811 * interpolation.
7813 * fan*_input: tachometer reading, RPM
7816 * SYSFS fan layout: extensions
7818 * fan_watchdog (driver):
7819 * fan watchdog interval in seconds, 0 disables (default), max 120
7822 /* sysfs fan pwm1_enable ----------------------------------------------- */
7823 static ssize_t fan_pwm1_enable_show(struct device *dev,
7824 struct device_attribute *attr,
7825 char *buf)
7827 int res, mode;
7828 u8 status;
7830 res = fan_get_status_safe(&status);
7831 if (res)
7832 return res;
7834 if (status & TP_EC_FAN_FULLSPEED) {
7835 mode = 0;
7836 } else if (status & TP_EC_FAN_AUTO) {
7837 mode = 2;
7838 } else
7839 mode = 1;
7841 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7844 static ssize_t fan_pwm1_enable_store(struct device *dev,
7845 struct device_attribute *attr,
7846 const char *buf, size_t count)
7848 unsigned long t;
7849 int res, level;
7851 if (parse_strtoul(buf, 2, &t))
7852 return -EINVAL;
7854 tpacpi_disclose_usertask("hwmon pwm1_enable",
7855 "set fan mode to %lu\n", t);
7857 switch (t) {
7858 case 0:
7859 level = TP_EC_FAN_FULLSPEED;
7860 break;
7861 case 1:
7862 level = TPACPI_FAN_LAST_LEVEL;
7863 break;
7864 case 2:
7865 level = TP_EC_FAN_AUTO;
7866 break;
7867 case 3:
7868 /* reserved for software-controlled auto mode */
7869 return -ENOSYS;
7870 default:
7871 return -EINVAL;
7874 res = fan_set_level_safe(level);
7875 if (res == -ENXIO)
7876 return -EINVAL;
7877 else if (res < 0)
7878 return res;
7880 fan_watchdog_reset();
7882 return count;
7885 static struct device_attribute dev_attr_fan_pwm1_enable =
7886 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7887 fan_pwm1_enable_show, fan_pwm1_enable_store);
7889 /* sysfs fan pwm1 ------------------------------------------------------ */
7890 static ssize_t fan_pwm1_show(struct device *dev,
7891 struct device_attribute *attr,
7892 char *buf)
7894 int res;
7895 u8 status;
7897 res = fan_get_status_safe(&status);
7898 if (res)
7899 return res;
7901 if ((status &
7902 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7903 status = fan_control_desired_level;
7905 if (status > 7)
7906 status = 7;
7908 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7911 static ssize_t fan_pwm1_store(struct device *dev,
7912 struct device_attribute *attr,
7913 const char *buf, size_t count)
7915 unsigned long s;
7916 int rc;
7917 u8 status, newlevel;
7919 if (parse_strtoul(buf, 255, &s))
7920 return -EINVAL;
7922 tpacpi_disclose_usertask("hwmon pwm1",
7923 "set fan speed to %lu\n", s);
7925 /* scale down from 0-255 to 0-7 */
7926 newlevel = (s >> 5) & 0x07;
7928 if (mutex_lock_killable(&fan_mutex))
7929 return -ERESTARTSYS;
7931 rc = fan_get_status(&status);
7932 if (!rc && (status &
7933 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7934 rc = fan_set_level(newlevel);
7935 if (rc == -ENXIO)
7936 rc = -EINVAL;
7937 else if (!rc) {
7938 fan_update_desired_level(newlevel);
7939 fan_watchdog_reset();
7943 mutex_unlock(&fan_mutex);
7944 return (rc)? rc : count;
7947 static struct device_attribute dev_attr_fan_pwm1 =
7948 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7949 fan_pwm1_show, fan_pwm1_store);
7951 /* sysfs fan fan1_input ------------------------------------------------ */
7952 static ssize_t fan_fan1_input_show(struct device *dev,
7953 struct device_attribute *attr,
7954 char *buf)
7956 int res;
7957 unsigned int speed;
7959 res = fan_get_speed(&speed);
7960 if (res < 0)
7961 return res;
7963 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7966 static struct device_attribute dev_attr_fan_fan1_input =
7967 __ATTR(fan1_input, S_IRUGO,
7968 fan_fan1_input_show, NULL);
7970 /* sysfs fan fan2_input ------------------------------------------------ */
7971 static ssize_t fan_fan2_input_show(struct device *dev,
7972 struct device_attribute *attr,
7973 char *buf)
7975 int res;
7976 unsigned int speed;
7978 res = fan2_get_speed(&speed);
7979 if (res < 0)
7980 return res;
7982 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7985 static struct device_attribute dev_attr_fan_fan2_input =
7986 __ATTR(fan2_input, S_IRUGO,
7987 fan_fan2_input_show, NULL);
7989 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7990 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7991 char *buf)
7993 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7996 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7997 const char *buf, size_t count)
7999 unsigned long t;
8001 if (parse_strtoul(buf, 120, &t))
8002 return -EINVAL;
8004 if (!fan_control_allowed)
8005 return -EPERM;
8007 fan_watchdog_maxinterval = t;
8008 fan_watchdog_reset();
8010 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8012 return count;
8015 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8016 fan_fan_watchdog_show, fan_fan_watchdog_store);
8018 /* --------------------------------------------------------------------- */
8019 static struct attribute *fan_attributes[] = {
8020 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8021 &dev_attr_fan_fan1_input.attr,
8022 NULL, /* for fan2_input */
8023 NULL
8026 static const struct attribute_group fan_attr_group = {
8027 .attrs = fan_attributes,
8030 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8031 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8033 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8034 { .vendor = PCI_VENDOR_ID_IBM, \
8035 .bios = TPACPI_MATCH_ANY, \
8036 .ec = TPID(__id1, __id2), \
8037 .quirks = __quirks }
8039 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8040 { .vendor = PCI_VENDOR_ID_LENOVO, \
8041 .bios = TPACPI_MATCH_ANY, \
8042 .ec = TPID(__id1, __id2), \
8043 .quirks = __quirks }
8045 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8046 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8047 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8048 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8049 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8050 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8053 #undef TPACPI_FAN_QL
8054 #undef TPACPI_FAN_QI
8056 static int __init fan_init(struct ibm_init_struct *iibm)
8058 int rc;
8059 unsigned long quirks;
8061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8062 "initializing fan subdriver\n");
8064 mutex_init(&fan_mutex);
8065 fan_status_access_mode = TPACPI_FAN_NONE;
8066 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8067 fan_control_commands = 0;
8068 fan_watchdog_maxinterval = 0;
8069 tp_features.fan_ctrl_status_undef = 0;
8070 tp_features.second_fan = 0;
8071 fan_control_desired_level = 7;
8073 if (tpacpi_is_ibm()) {
8074 TPACPI_ACPIHANDLE_INIT(fans);
8075 TPACPI_ACPIHANDLE_INIT(gfan);
8076 TPACPI_ACPIHANDLE_INIT(sfan);
8079 quirks = tpacpi_check_quirks(fan_quirk_table,
8080 ARRAY_SIZE(fan_quirk_table));
8082 if (gfan_handle) {
8083 /* 570, 600e/x, 770e, 770x */
8084 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8085 } else {
8086 /* all other ThinkPads: note that even old-style
8087 * ThinkPad ECs supports the fan control register */
8088 if (likely(acpi_ec_read(fan_status_offset,
8089 &fan_control_initial_status))) {
8090 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8091 if (quirks & TPACPI_FAN_Q1)
8092 fan_quirk1_setup();
8093 if (quirks & TPACPI_FAN_2FAN) {
8094 tp_features.second_fan = 1;
8095 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8096 "secondary fan support enabled\n");
8098 } else {
8099 printk(TPACPI_ERR
8100 "ThinkPad ACPI EC access misbehaving, "
8101 "fan status and control unavailable\n");
8102 return 1;
8106 if (sfan_handle) {
8107 /* 570, 770x-JL */
8108 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8109 fan_control_commands |=
8110 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8111 } else {
8112 if (!gfan_handle) {
8113 /* gfan without sfan means no fan control */
8114 /* all other models implement TP EC 0x2f control */
8116 if (fans_handle) {
8117 /* X31, X40, X41 */
8118 fan_control_access_mode =
8119 TPACPI_FAN_WR_ACPI_FANS;
8120 fan_control_commands |=
8121 TPACPI_FAN_CMD_SPEED |
8122 TPACPI_FAN_CMD_LEVEL |
8123 TPACPI_FAN_CMD_ENABLE;
8124 } else {
8125 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8126 fan_control_commands |=
8127 TPACPI_FAN_CMD_LEVEL |
8128 TPACPI_FAN_CMD_ENABLE;
8133 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8134 "fan is %s, modes %d, %d\n",
8135 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8136 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8137 fan_status_access_mode, fan_control_access_mode);
8139 /* fan control master switch */
8140 if (!fan_control_allowed) {
8141 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8142 fan_control_commands = 0;
8143 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8144 "fan control features disabled by parameter\n");
8147 /* update fan_control_desired_level */
8148 if (fan_status_access_mode != TPACPI_FAN_NONE)
8149 fan_get_status_safe(NULL);
8151 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8152 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8153 if (tp_features.second_fan) {
8154 /* attach second fan tachometer */
8155 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8156 &dev_attr_fan_fan2_input.attr;
8158 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8159 &fan_attr_group);
8160 if (rc < 0)
8161 return rc;
8163 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8164 &driver_attr_fan_watchdog);
8165 if (rc < 0) {
8166 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8167 &fan_attr_group);
8168 return rc;
8170 return 0;
8171 } else
8172 return 1;
8175 static void fan_exit(void)
8177 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8178 "cancelling any pending fan watchdog tasks\n");
8180 /* FIXME: can we really do this unconditionally? */
8181 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8182 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8183 &driver_attr_fan_watchdog);
8185 cancel_delayed_work(&fan_watchdog_task);
8186 flush_workqueue(tpacpi_wq);
8189 static void fan_suspend(pm_message_t state)
8191 int rc;
8193 if (!fan_control_allowed)
8194 return;
8196 /* Store fan status in cache */
8197 fan_control_resume_level = 0;
8198 rc = fan_get_status_safe(&fan_control_resume_level);
8199 if (rc < 0)
8200 printk(TPACPI_NOTICE
8201 "failed to read fan level for later "
8202 "restore during resume: %d\n", rc);
8204 /* if it is undefined, don't attempt to restore it.
8205 * KEEP THIS LAST */
8206 if (tp_features.fan_ctrl_status_undef)
8207 fan_control_resume_level = 0;
8210 static void fan_resume(void)
8212 u8 current_level = 7;
8213 bool do_set = false;
8214 int rc;
8216 /* DSDT *always* updates status on resume */
8217 tp_features.fan_ctrl_status_undef = 0;
8219 if (!fan_control_allowed ||
8220 !fan_control_resume_level ||
8221 (fan_get_status_safe(&current_level) < 0))
8222 return;
8224 switch (fan_control_access_mode) {
8225 case TPACPI_FAN_WR_ACPI_SFAN:
8226 /* never decrease fan level */
8227 do_set = (fan_control_resume_level > current_level);
8228 break;
8229 case TPACPI_FAN_WR_ACPI_FANS:
8230 case TPACPI_FAN_WR_TPEC:
8231 /* never decrease fan level, scale is:
8232 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8234 * We expect the firmware to set either 7 or AUTO, but we
8235 * handle FULLSPEED out of paranoia.
8237 * So, we can safely only restore FULLSPEED or 7, anything
8238 * else could slow the fan. Restoring AUTO is useless, at
8239 * best that's exactly what the DSDT already set (it is the
8240 * slower it uses).
8242 * Always keep in mind that the DSDT *will* have set the
8243 * fans to what the vendor supposes is the best level. We
8244 * muck with it only to speed the fan up.
8246 if (fan_control_resume_level != 7 &&
8247 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8248 return;
8249 else
8250 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8251 (current_level != fan_control_resume_level);
8252 break;
8253 default:
8254 return;
8256 if (do_set) {
8257 printk(TPACPI_NOTICE
8258 "restoring fan level to 0x%02x\n",
8259 fan_control_resume_level);
8260 rc = fan_set_level_safe(fan_control_resume_level);
8261 if (rc < 0)
8262 printk(TPACPI_NOTICE
8263 "failed to restore fan level: %d\n", rc);
8267 static int fan_read(struct seq_file *m)
8269 int rc;
8270 u8 status;
8271 unsigned int speed = 0;
8273 switch (fan_status_access_mode) {
8274 case TPACPI_FAN_RD_ACPI_GFAN:
8275 /* 570, 600e/x, 770e, 770x */
8276 rc = fan_get_status_safe(&status);
8277 if (rc < 0)
8278 return rc;
8280 seq_printf(m, "status:\t\t%s\n"
8281 "level:\t\t%d\n",
8282 (status != 0) ? "enabled" : "disabled", status);
8283 break;
8285 case TPACPI_FAN_RD_TPEC:
8286 /* all except 570, 600e/x, 770e, 770x */
8287 rc = fan_get_status_safe(&status);
8288 if (rc < 0)
8289 return rc;
8291 seq_printf(m, "status:\t\t%s\n",
8292 (status != 0) ? "enabled" : "disabled");
8294 rc = fan_get_speed(&speed);
8295 if (rc < 0)
8296 return rc;
8298 seq_printf(m, "speed:\t\t%d\n", speed);
8300 if (status & TP_EC_FAN_FULLSPEED)
8301 /* Disengaged mode takes precedence */
8302 seq_printf(m, "level:\t\tdisengaged\n");
8303 else if (status & TP_EC_FAN_AUTO)
8304 seq_printf(m, "level:\t\tauto\n");
8305 else
8306 seq_printf(m, "level:\t\t%d\n", status);
8307 break;
8309 case TPACPI_FAN_NONE:
8310 default:
8311 seq_printf(m, "status:\t\tnot supported\n");
8314 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8315 seq_printf(m, "commands:\tlevel <level>");
8317 switch (fan_control_access_mode) {
8318 case TPACPI_FAN_WR_ACPI_SFAN:
8319 seq_printf(m, " (<level> is 0-7)\n");
8320 break;
8322 default:
8323 seq_printf(m, " (<level> is 0-7, "
8324 "auto, disengaged, full-speed)\n");
8325 break;
8329 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8330 seq_printf(m, "commands:\tenable, disable\n"
8331 "commands:\twatchdog <timeout> (<timeout> "
8332 "is 0 (off), 1-120 (seconds))\n");
8334 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8335 seq_printf(m, "commands:\tspeed <speed>"
8336 " (<speed> is 0-65535)\n");
8338 return 0;
8341 static int fan_write_cmd_level(const char *cmd, int *rc)
8343 int level;
8345 if (strlencmp(cmd, "level auto") == 0)
8346 level = TP_EC_FAN_AUTO;
8347 else if ((strlencmp(cmd, "level disengaged") == 0) |
8348 (strlencmp(cmd, "level full-speed") == 0))
8349 level = TP_EC_FAN_FULLSPEED;
8350 else if (sscanf(cmd, "level %d", &level) != 1)
8351 return 0;
8353 *rc = fan_set_level_safe(level);
8354 if (*rc == -ENXIO)
8355 printk(TPACPI_ERR "level command accepted for unsupported "
8356 "access mode %d", fan_control_access_mode);
8357 else if (!*rc)
8358 tpacpi_disclose_usertask("procfs fan",
8359 "set level to %d\n", level);
8361 return 1;
8364 static int fan_write_cmd_enable(const char *cmd, int *rc)
8366 if (strlencmp(cmd, "enable") != 0)
8367 return 0;
8369 *rc = fan_set_enable();
8370 if (*rc == -ENXIO)
8371 printk(TPACPI_ERR "enable command accepted for unsupported "
8372 "access mode %d", fan_control_access_mode);
8373 else if (!*rc)
8374 tpacpi_disclose_usertask("procfs fan", "enable\n");
8376 return 1;
8379 static int fan_write_cmd_disable(const char *cmd, int *rc)
8381 if (strlencmp(cmd, "disable") != 0)
8382 return 0;
8384 *rc = fan_set_disable();
8385 if (*rc == -ENXIO)
8386 printk(TPACPI_ERR "disable command accepted for unsupported "
8387 "access mode %d", fan_control_access_mode);
8388 else if (!*rc)
8389 tpacpi_disclose_usertask("procfs fan", "disable\n");
8391 return 1;
8394 static int fan_write_cmd_speed(const char *cmd, int *rc)
8396 int speed;
8398 /* TODO:
8399 * Support speed <low> <medium> <high> ? */
8401 if (sscanf(cmd, "speed %d", &speed) != 1)
8402 return 0;
8404 *rc = fan_set_speed(speed);
8405 if (*rc == -ENXIO)
8406 printk(TPACPI_ERR "speed command accepted for unsupported "
8407 "access mode %d", fan_control_access_mode);
8408 else if (!*rc)
8409 tpacpi_disclose_usertask("procfs fan",
8410 "set speed to %d\n", speed);
8412 return 1;
8415 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8417 int interval;
8419 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8420 return 0;
8422 if (interval < 0 || interval > 120)
8423 *rc = -EINVAL;
8424 else {
8425 fan_watchdog_maxinterval = interval;
8426 tpacpi_disclose_usertask("procfs fan",
8427 "set watchdog timer to %d\n",
8428 interval);
8431 return 1;
8434 static int fan_write(char *buf)
8436 char *cmd;
8437 int rc = 0;
8439 while (!rc && (cmd = next_cmd(&buf))) {
8440 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8441 fan_write_cmd_level(cmd, &rc)) &&
8442 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8443 (fan_write_cmd_enable(cmd, &rc) ||
8444 fan_write_cmd_disable(cmd, &rc) ||
8445 fan_write_cmd_watchdog(cmd, &rc))) &&
8446 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8447 fan_write_cmd_speed(cmd, &rc))
8449 rc = -EINVAL;
8450 else if (!rc)
8451 fan_watchdog_reset();
8454 return rc;
8457 static struct ibm_struct fan_driver_data = {
8458 .name = "fan",
8459 .read = fan_read,
8460 .write = fan_write,
8461 .exit = fan_exit,
8462 .suspend = fan_suspend,
8463 .resume = fan_resume,
8466 /****************************************************************************
8467 ****************************************************************************
8469 * Infrastructure
8471 ****************************************************************************
8472 ****************************************************************************/
8475 * HKEY event callout for other subdrivers go here
8476 * (yes, it is ugly, but it is quick, safe, and gets the job done
8478 static void tpacpi_driver_event(const unsigned int hkey_event)
8480 if (ibm_backlight_device) {
8481 switch (hkey_event) {
8482 case TP_HKEY_EV_BRGHT_UP:
8483 case TP_HKEY_EV_BRGHT_DOWN:
8484 tpacpi_brightness_notify_change();
8487 if (alsa_card) {
8488 switch (hkey_event) {
8489 case TP_HKEY_EV_VOL_UP:
8490 case TP_HKEY_EV_VOL_DOWN:
8491 case TP_HKEY_EV_VOL_MUTE:
8492 volume_alsa_notify_change();
8497 static void hotkey_driver_event(const unsigned int scancode)
8499 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8502 /* sysfs name ---------------------------------------------------------- */
8503 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8504 struct device_attribute *attr,
8505 char *buf)
8507 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8510 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8511 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8513 /* --------------------------------------------------------------------- */
8515 /* /proc support */
8516 static struct proc_dir_entry *proc_dir;
8519 * Module and infrastructure proble, init and exit handling
8522 static int force_load;
8524 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8525 static const char * __init str_supported(int is_supported)
8527 static char text_unsupported[] __initdata = "not supported";
8529 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8531 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8533 static void ibm_exit(struct ibm_struct *ibm)
8535 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8537 list_del_init(&ibm->all_drivers);
8539 if (ibm->flags.acpi_notify_installed) {
8540 dbg_printk(TPACPI_DBG_EXIT,
8541 "%s: acpi_remove_notify_handler\n", ibm->name);
8542 BUG_ON(!ibm->acpi);
8543 acpi_remove_notify_handler(*ibm->acpi->handle,
8544 ibm->acpi->type,
8545 dispatch_acpi_notify);
8546 ibm->flags.acpi_notify_installed = 0;
8547 ibm->flags.acpi_notify_installed = 0;
8550 if (ibm->flags.proc_created) {
8551 dbg_printk(TPACPI_DBG_EXIT,
8552 "%s: remove_proc_entry\n", ibm->name);
8553 remove_proc_entry(ibm->name, proc_dir);
8554 ibm->flags.proc_created = 0;
8557 if (ibm->flags.acpi_driver_registered) {
8558 dbg_printk(TPACPI_DBG_EXIT,
8559 "%s: acpi_bus_unregister_driver\n", ibm->name);
8560 BUG_ON(!ibm->acpi);
8561 acpi_bus_unregister_driver(ibm->acpi->driver);
8562 kfree(ibm->acpi->driver);
8563 ibm->acpi->driver = NULL;
8564 ibm->flags.acpi_driver_registered = 0;
8567 if (ibm->flags.init_called && ibm->exit) {
8568 ibm->exit();
8569 ibm->flags.init_called = 0;
8572 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8575 static int __init ibm_init(struct ibm_init_struct *iibm)
8577 int ret;
8578 struct ibm_struct *ibm = iibm->data;
8579 struct proc_dir_entry *entry;
8581 BUG_ON(ibm == NULL);
8583 INIT_LIST_HEAD(&ibm->all_drivers);
8585 if (ibm->flags.experimental && !experimental)
8586 return 0;
8588 dbg_printk(TPACPI_DBG_INIT,
8589 "probing for %s\n", ibm->name);
8591 if (iibm->init) {
8592 ret = iibm->init(iibm);
8593 if (ret > 0)
8594 return 0; /* probe failed */
8595 if (ret)
8596 return ret;
8598 ibm->flags.init_called = 1;
8601 if (ibm->acpi) {
8602 if (ibm->acpi->hid) {
8603 ret = register_tpacpi_subdriver(ibm);
8604 if (ret)
8605 goto err_out;
8608 if (ibm->acpi->notify) {
8609 ret = setup_acpi_notify(ibm);
8610 if (ret == -ENODEV) {
8611 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8612 ibm->name);
8613 ret = 0;
8614 goto err_out;
8616 if (ret < 0)
8617 goto err_out;
8621 dbg_printk(TPACPI_DBG_INIT,
8622 "%s installed\n", ibm->name);
8624 if (ibm->read) {
8625 mode_t mode = iibm->base_procfs_mode;
8627 if (!mode)
8628 mode = S_IRUGO;
8629 if (ibm->write)
8630 mode |= S_IWUSR;
8631 entry = proc_create_data(ibm->name, mode, proc_dir,
8632 &dispatch_proc_fops, ibm);
8633 if (!entry) {
8634 printk(TPACPI_ERR "unable to create proc entry %s\n",
8635 ibm->name);
8636 ret = -ENODEV;
8637 goto err_out;
8639 ibm->flags.proc_created = 1;
8642 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8644 return 0;
8646 err_out:
8647 dbg_printk(TPACPI_DBG_INIT,
8648 "%s: at error exit path with result %d\n",
8649 ibm->name, ret);
8651 ibm_exit(ibm);
8652 return (ret < 0)? ret : 0;
8655 /* Probing */
8657 static bool __pure __init tpacpi_is_fw_digit(const char c)
8659 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8662 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8663 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8664 const char t)
8666 return s && strlen(s) >= 8 &&
8667 tpacpi_is_fw_digit(s[0]) &&
8668 tpacpi_is_fw_digit(s[1]) &&
8669 s[2] == t && s[3] == 'T' &&
8670 tpacpi_is_fw_digit(s[4]) &&
8671 tpacpi_is_fw_digit(s[5]) &&
8672 s[6] == 'W' && s[7] == 'W';
8675 /* returns 0 - probe ok, or < 0 - probe error.
8676 * Probe ok doesn't mean thinkpad found.
8677 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8678 static int __must_check __init get_thinkpad_model_data(
8679 struct thinkpad_id_data *tp)
8681 const struct dmi_device *dev = NULL;
8682 char ec_fw_string[18];
8683 char const *s;
8685 if (!tp)
8686 return -EINVAL;
8688 memset(tp, 0, sizeof(*tp));
8690 if (dmi_name_in_vendors("IBM"))
8691 tp->vendor = PCI_VENDOR_ID_IBM;
8692 else if (dmi_name_in_vendors("LENOVO"))
8693 tp->vendor = PCI_VENDOR_ID_LENOVO;
8694 else
8695 return 0;
8697 s = dmi_get_system_info(DMI_BIOS_VERSION);
8698 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8699 if (s && !tp->bios_version_str)
8700 return -ENOMEM;
8702 /* Really ancient ThinkPad 240X will fail this, which is fine */
8703 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8704 return 0;
8706 tp->bios_model = tp->bios_version_str[0]
8707 | (tp->bios_version_str[1] << 8);
8708 tp->bios_release = (tp->bios_version_str[4] << 8)
8709 | tp->bios_version_str[5];
8712 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8713 * X32 or newer, all Z series; Some models must have an
8714 * up-to-date BIOS or they will not be detected.
8716 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8718 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8719 if (sscanf(dev->name,
8720 "IBM ThinkPad Embedded Controller -[%17c",
8721 ec_fw_string) == 1) {
8722 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8723 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8725 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8726 if (!tp->ec_version_str)
8727 return -ENOMEM;
8729 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8730 tp->ec_model = ec_fw_string[0]
8731 | (ec_fw_string[1] << 8);
8732 tp->ec_release = (ec_fw_string[4] << 8)
8733 | ec_fw_string[5];
8734 } else {
8735 printk(TPACPI_NOTICE
8736 "ThinkPad firmware release %s "
8737 "doesn't match the known patterns\n",
8738 ec_fw_string);
8739 printk(TPACPI_NOTICE
8740 "please report this to %s\n",
8741 TPACPI_MAIL);
8743 break;
8747 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8748 if (s && !strnicmp(s, "ThinkPad", 8)) {
8749 tp->model_str = kstrdup(s, GFP_KERNEL);
8750 if (!tp->model_str)
8751 return -ENOMEM;
8754 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8755 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8756 if (s && !tp->nummodel_str)
8757 return -ENOMEM;
8759 return 0;
8762 static int __init probe_for_thinkpad(void)
8764 int is_thinkpad;
8766 if (acpi_disabled)
8767 return -ENODEV;
8769 /* It would be dangerous to run the driver in this case */
8770 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8771 return -ENODEV;
8774 * Non-ancient models have better DMI tagging, but very old models
8775 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8777 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8778 (thinkpad_id.ec_model != 0) ||
8779 tpacpi_is_fw_known();
8781 /* The EC handler is required */
8782 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8783 if (!ec_handle) {
8784 if (is_thinkpad)
8785 printk(TPACPI_ERR
8786 "Not yet supported ThinkPad detected!\n");
8787 return -ENODEV;
8790 if (!is_thinkpad && !force_load)
8791 return -ENODEV;
8793 return 0;
8796 static void __init thinkpad_acpi_init_banner(void)
8798 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8799 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8801 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8802 (thinkpad_id.bios_version_str) ?
8803 thinkpad_id.bios_version_str : "unknown",
8804 (thinkpad_id.ec_version_str) ?
8805 thinkpad_id.ec_version_str : "unknown");
8807 BUG_ON(!thinkpad_id.vendor);
8809 if (thinkpad_id.model_str)
8810 printk(TPACPI_INFO "%s %s, model %s\n",
8811 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8812 "IBM" : ((thinkpad_id.vendor ==
8813 PCI_VENDOR_ID_LENOVO) ?
8814 "Lenovo" : "Unknown vendor"),
8815 thinkpad_id.model_str,
8816 (thinkpad_id.nummodel_str) ?
8817 thinkpad_id.nummodel_str : "unknown");
8820 /* Module init, exit, parameters */
8822 static struct ibm_init_struct ibms_init[] __initdata = {
8824 .data = &thinkpad_acpi_driver_data,
8827 .init = hotkey_init,
8828 .data = &hotkey_driver_data,
8831 .init = bluetooth_init,
8832 .data = &bluetooth_driver_data,
8835 .init = wan_init,
8836 .data = &wan_driver_data,
8839 .init = uwb_init,
8840 .data = &uwb_driver_data,
8842 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8844 .init = video_init,
8845 .base_procfs_mode = S_IRUSR,
8846 .data = &video_driver_data,
8848 #endif
8850 .init = light_init,
8851 .data = &light_driver_data,
8854 .init = cmos_init,
8855 .data = &cmos_driver_data,
8858 .init = led_init,
8859 .data = &led_driver_data,
8862 .init = beep_init,
8863 .data = &beep_driver_data,
8866 .init = thermal_init,
8867 .data = &thermal_driver_data,
8870 .data = &ecdump_driver_data,
8873 .init = brightness_init,
8874 .data = &brightness_driver_data,
8877 .init = volume_init,
8878 .data = &volume_driver_data,
8881 .init = fan_init,
8882 .data = &fan_driver_data,
8886 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8888 unsigned int i;
8889 struct ibm_struct *ibm;
8891 if (!kp || !kp->name || !val)
8892 return -EINVAL;
8894 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8895 ibm = ibms_init[i].data;
8896 WARN_ON(ibm == NULL);
8898 if (!ibm || !ibm->name)
8899 continue;
8901 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8902 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8903 return -ENOSPC;
8904 strcpy(ibms_init[i].param, val);
8905 strcat(ibms_init[i].param, ",");
8906 return 0;
8910 return -EINVAL;
8913 module_param(experimental, int, 0444);
8914 MODULE_PARM_DESC(experimental,
8915 "Enables experimental features when non-zero");
8917 module_param_named(debug, dbg_level, uint, 0);
8918 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8920 module_param(force_load, bool, 0444);
8921 MODULE_PARM_DESC(force_load,
8922 "Attempts to load the driver even on a "
8923 "mis-identified ThinkPad when true");
8925 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8926 MODULE_PARM_DESC(fan_control,
8927 "Enables setting fan parameters features when true");
8929 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8930 MODULE_PARM_DESC(brightness_mode,
8931 "Selects brightness control strategy: "
8932 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8934 module_param(brightness_enable, uint, 0444);
8935 MODULE_PARM_DESC(brightness_enable,
8936 "Enables backlight control when 1, disables when 0");
8938 module_param(hotkey_report_mode, uint, 0444);
8939 MODULE_PARM_DESC(hotkey_report_mode,
8940 "used for backwards compatibility with userspace, "
8941 "see documentation");
8943 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8944 module_param_named(volume_mode, volume_mode, uint, 0444);
8945 MODULE_PARM_DESC(volume_mode,
8946 "Selects volume control strategy: "
8947 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8949 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8950 MODULE_PARM_DESC(volume_capabilities,
8951 "Selects the mixer capabilites: "
8952 "0=auto, 1=volume and mute, 2=mute only");
8954 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8955 MODULE_PARM_DESC(volume_control,
8956 "Enables software override for the console audio "
8957 "control when true");
8959 /* ALSA module API parameters */
8960 module_param_named(index, alsa_index, int, 0444);
8961 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8962 module_param_named(id, alsa_id, charp, 0444);
8963 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8964 module_param_named(enable, alsa_enable, bool, 0444);
8965 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8966 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8968 #define TPACPI_PARAM(feature) \
8969 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8970 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8971 "at module load, see documentation")
8973 TPACPI_PARAM(hotkey);
8974 TPACPI_PARAM(bluetooth);
8975 TPACPI_PARAM(video);
8976 TPACPI_PARAM(light);
8977 TPACPI_PARAM(cmos);
8978 TPACPI_PARAM(led);
8979 TPACPI_PARAM(beep);
8980 TPACPI_PARAM(ecdump);
8981 TPACPI_PARAM(brightness);
8982 TPACPI_PARAM(volume);
8983 TPACPI_PARAM(fan);
8985 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8986 module_param(dbg_wlswemul, uint, 0444);
8987 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8988 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8989 MODULE_PARM_DESC(wlsw_state,
8990 "Initial state of the emulated WLSW switch");
8992 module_param(dbg_bluetoothemul, uint, 0444);
8993 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8994 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8995 MODULE_PARM_DESC(bluetooth_state,
8996 "Initial state of the emulated bluetooth switch");
8998 module_param(dbg_wwanemul, uint, 0444);
8999 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9000 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9001 MODULE_PARM_DESC(wwan_state,
9002 "Initial state of the emulated WWAN switch");
9004 module_param(dbg_uwbemul, uint, 0444);
9005 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9006 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9007 MODULE_PARM_DESC(uwb_state,
9008 "Initial state of the emulated UWB switch");
9009 #endif
9011 static void thinkpad_acpi_module_exit(void)
9013 struct ibm_struct *ibm, *itmp;
9015 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9017 list_for_each_entry_safe_reverse(ibm, itmp,
9018 &tpacpi_all_drivers,
9019 all_drivers) {
9020 ibm_exit(ibm);
9023 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9025 if (tpacpi_inputdev) {
9026 if (tp_features.input_device_registered)
9027 input_unregister_device(tpacpi_inputdev);
9028 else
9029 input_free_device(tpacpi_inputdev);
9032 if (tpacpi_hwmon)
9033 hwmon_device_unregister(tpacpi_hwmon);
9035 if (tp_features.sensors_pdev_attrs_registered)
9036 device_remove_file(&tpacpi_sensors_pdev->dev,
9037 &dev_attr_thinkpad_acpi_pdev_name);
9038 if (tpacpi_sensors_pdev)
9039 platform_device_unregister(tpacpi_sensors_pdev);
9040 if (tpacpi_pdev)
9041 platform_device_unregister(tpacpi_pdev);
9043 if (tp_features.sensors_pdrv_attrs_registered)
9044 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9045 if (tp_features.platform_drv_attrs_registered)
9046 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9048 if (tp_features.sensors_pdrv_registered)
9049 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9051 if (tp_features.platform_drv_registered)
9052 platform_driver_unregister(&tpacpi_pdriver);
9054 if (proc_dir)
9055 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9057 if (tpacpi_wq)
9058 destroy_workqueue(tpacpi_wq);
9060 kfree(thinkpad_id.bios_version_str);
9061 kfree(thinkpad_id.ec_version_str);
9062 kfree(thinkpad_id.model_str);
9066 static int __init thinkpad_acpi_module_init(void)
9068 int ret, i;
9070 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9072 /* Parameter checking */
9073 if (hotkey_report_mode > 2)
9074 return -EINVAL;
9076 /* Driver-level probe */
9078 ret = get_thinkpad_model_data(&thinkpad_id);
9079 if (ret) {
9080 printk(TPACPI_ERR
9081 "unable to get DMI data: %d\n", ret);
9082 thinkpad_acpi_module_exit();
9083 return ret;
9085 ret = probe_for_thinkpad();
9086 if (ret) {
9087 thinkpad_acpi_module_exit();
9088 return ret;
9091 /* Driver initialization */
9093 thinkpad_acpi_init_banner();
9094 tpacpi_check_outdated_fw();
9096 TPACPI_ACPIHANDLE_INIT(ecrd);
9097 TPACPI_ACPIHANDLE_INIT(ecwr);
9099 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9100 if (!tpacpi_wq) {
9101 thinkpad_acpi_module_exit();
9102 return -ENOMEM;
9105 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9106 if (!proc_dir) {
9107 printk(TPACPI_ERR
9108 "unable to create proc dir " TPACPI_PROC_DIR);
9109 thinkpad_acpi_module_exit();
9110 return -ENODEV;
9113 ret = platform_driver_register(&tpacpi_pdriver);
9114 if (ret) {
9115 printk(TPACPI_ERR
9116 "unable to register main platform driver\n");
9117 thinkpad_acpi_module_exit();
9118 return ret;
9120 tp_features.platform_drv_registered = 1;
9122 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9123 if (ret) {
9124 printk(TPACPI_ERR
9125 "unable to register hwmon platform driver\n");
9126 thinkpad_acpi_module_exit();
9127 return ret;
9129 tp_features.sensors_pdrv_registered = 1;
9131 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9132 if (!ret) {
9133 tp_features.platform_drv_attrs_registered = 1;
9134 ret = tpacpi_create_driver_attributes(
9135 &tpacpi_hwmon_pdriver.driver);
9137 if (ret) {
9138 printk(TPACPI_ERR
9139 "unable to create sysfs driver attributes\n");
9140 thinkpad_acpi_module_exit();
9141 return ret;
9143 tp_features.sensors_pdrv_attrs_registered = 1;
9146 /* Device initialization */
9147 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9148 NULL, 0);
9149 if (IS_ERR(tpacpi_pdev)) {
9150 ret = PTR_ERR(tpacpi_pdev);
9151 tpacpi_pdev = NULL;
9152 printk(TPACPI_ERR "unable to register platform device\n");
9153 thinkpad_acpi_module_exit();
9154 return ret;
9156 tpacpi_sensors_pdev = platform_device_register_simple(
9157 TPACPI_HWMON_DRVR_NAME,
9158 -1, NULL, 0);
9159 if (IS_ERR(tpacpi_sensors_pdev)) {
9160 ret = PTR_ERR(tpacpi_sensors_pdev);
9161 tpacpi_sensors_pdev = NULL;
9162 printk(TPACPI_ERR
9163 "unable to register hwmon platform device\n");
9164 thinkpad_acpi_module_exit();
9165 return ret;
9167 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9168 &dev_attr_thinkpad_acpi_pdev_name);
9169 if (ret) {
9170 printk(TPACPI_ERR
9171 "unable to create sysfs hwmon device attributes\n");
9172 thinkpad_acpi_module_exit();
9173 return ret;
9175 tp_features.sensors_pdev_attrs_registered = 1;
9176 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9177 if (IS_ERR(tpacpi_hwmon)) {
9178 ret = PTR_ERR(tpacpi_hwmon);
9179 tpacpi_hwmon = NULL;
9180 printk(TPACPI_ERR "unable to register hwmon device\n");
9181 thinkpad_acpi_module_exit();
9182 return ret;
9184 mutex_init(&tpacpi_inputdev_send_mutex);
9185 tpacpi_inputdev = input_allocate_device();
9186 if (!tpacpi_inputdev) {
9187 printk(TPACPI_ERR "unable to allocate input device\n");
9188 thinkpad_acpi_module_exit();
9189 return -ENOMEM;
9190 } else {
9191 /* Prepare input device, but don't register */
9192 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9193 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9194 tpacpi_inputdev->id.bustype = BUS_HOST;
9195 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9196 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9197 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9198 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9201 /* Init subdriver dependencies */
9202 tpacpi_detect_brightness_capabilities();
9204 /* Init subdrivers */
9205 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9206 ret = ibm_init(&ibms_init[i]);
9207 if (ret >= 0 && *ibms_init[i].param)
9208 ret = ibms_init[i].data->write(ibms_init[i].param);
9209 if (ret < 0) {
9210 thinkpad_acpi_module_exit();
9211 return ret;
9215 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9217 ret = input_register_device(tpacpi_inputdev);
9218 if (ret < 0) {
9219 printk(TPACPI_ERR "unable to register input device\n");
9220 thinkpad_acpi_module_exit();
9221 return ret;
9222 } else {
9223 tp_features.input_device_registered = 1;
9226 return 0;
9229 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9232 * This will autoload the driver in almost every ThinkPad
9233 * in widespread use.
9235 * Only _VERY_ old models, like the 240, 240x and 570 lack
9236 * the HKEY event interface.
9238 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9241 * DMI matching for module autoloading
9243 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9244 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9246 * Only models listed in thinkwiki will be supported, so add yours
9247 * if it is not there yet.
9249 #define IBM_BIOS_MODULE_ALIAS(__type) \
9250 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9252 /* Ancient thinkpad BIOSes have to be identified by
9253 * BIOS type or model number, and there are far less
9254 * BIOS types than model numbers... */
9255 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9257 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9258 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9259 MODULE_DESCRIPTION(TPACPI_DESC);
9260 MODULE_VERSION(TPACPI_VERSION);
9261 MODULE_LICENSE("GPL");
9263 module_init(thinkpad_acpi_module_init);
9264 module_exit(thinkpad_acpi_module_exit);