thinkpad-acpi: hotkey poll fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob2e4c146a156497402ace5a6974c16bf8ad208d82
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.23"
25 #define TPACPI_SYSFS_VERSION 0x020400
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/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
195 #define TPACPI_DBG_BRGHT 0x0020
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
226 void (*shutdown) (void);
228 struct list_head all_drivers;
230 struct tp_acpi_drv_struct *acpi;
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
241 struct ibm_init_struct {
242 char param[32];
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
248 static struct {
249 u32 bluetooth:1;
250 u32 hotkey:1;
251 u32 hotkey_mask:1;
252 u32 hotkey_wlsw:1;
253 u32 hotkey_tablet:1;
254 u32 light:1;
255 u32 light_status:1;
256 u32 bright_16levels:1;
257 u32 bright_acpimode:1;
258 u32 wan:1;
259 u32 uwb:1;
260 u32 fan_ctrl_status_undef:1;
261 u32 second_fan:1;
262 u32 beep_needs_two_args:1;
263 u32 input_device_registered:1;
264 u32 platform_drv_registered:1;
265 u32 platform_drv_attrs_registered:1;
266 u32 sensors_pdrv_registered:1;
267 u32 sensors_pdrv_attrs_registered:1;
268 u32 sensors_pdev_attrs_registered:1;
269 u32 hotkey_poll_active:1;
270 } tp_features;
272 static struct {
273 u16 hotkey_mask_ff:1;
274 } tp_warned;
276 struct thinkpad_id_data {
277 unsigned int vendor; /* ThinkPad vendor:
278 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
280 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
281 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
283 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
284 u16 ec_model;
285 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
286 u16 ec_release;
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
293 static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 enum led_status_t {
305 TPACPI_LED_OFF = 0,
306 TPACPI_LED_ON,
307 TPACPI_LED_BLINK,
310 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev {
312 struct led_classdev led_classdev;
313 struct work_struct work;
314 enum led_status_t new_state;
315 unsigned int led;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul;
320 static int tpacpi_wlsw_emulstate;
321 static int dbg_bluetoothemul;
322 static int tpacpi_bluetooth_emulstate;
323 static int dbg_wwanemul;
324 static int tpacpi_wwan_emulstate;
325 static int dbg_uwbemul;
326 static int tpacpi_uwb_emulstate;
327 #endif
330 /*************************************************************************
331 * Debugging helpers
334 #define dbg_printk(a_dbg_level, format, arg...) \
335 do { if (dbg_level & (a_dbg_level)) \
336 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
337 } while (0)
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 static const char *str_supported(int is_supported);
342 #else
343 #define vdbg_printk(a_dbg_level, format, arg...) \
344 do { } while (0)
345 #endif
347 static void tpacpi_log_usertask(const char * const what)
349 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
350 what, task_tgid_vnr(current));
353 #define tpacpi_disclose_usertask(what, format, arg...) \
354 do { \
355 if (unlikely( \
356 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
357 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
358 printk(TPACPI_DEBUG "%s: PID %d: " format, \
359 what, task_tgid_vnr(current), ## arg); \
361 } while (0)
364 * Quirk handling helpers
366 * ThinkPad IDs and versions seen in the field so far
367 * are two-characters from the set [0-9A-Z], i.e. base 36.
369 * We use values well outside that range as specials.
372 #define TPACPI_MATCH_ANY 0xffffU
373 #define TPACPI_MATCH_UNKNOWN 0U
375 /* TPID('1', 'Y') == 0x5931 */
376 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
378 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
379 { .vendor = PCI_VENDOR_ID_IBM, \
380 .bios = TPID(__id1, __id2), \
381 .ec = TPACPI_MATCH_ANY, \
382 .quirks = (__quirk) }
384 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_LENOVO, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
390 struct tpacpi_quirk {
391 unsigned int vendor;
392 u16 bios;
393 u16 ec;
394 unsigned long quirks;
398 * tpacpi_check_quirks() - search BIOS/EC version on a list
399 * @qlist: array of &struct tpacpi_quirk
400 * @qlist_size: number of elements in @qlist
402 * Iterates over a quirks list until one is found that matches the
403 * ThinkPad's vendor, BIOS and EC model.
405 * Returns 0 if nothing matches, otherwise returns the quirks field of
406 * the matching &struct tpacpi_quirk entry.
408 * The match criteria is: vendor, ec and bios much match.
410 static unsigned long __init tpacpi_check_quirks(
411 const struct tpacpi_quirk *qlist,
412 unsigned int qlist_size)
414 while (qlist_size) {
415 if ((qlist->vendor == thinkpad_id.vendor ||
416 qlist->vendor == TPACPI_MATCH_ANY) &&
417 (qlist->bios == thinkpad_id.bios_model ||
418 qlist->bios == TPACPI_MATCH_ANY) &&
419 (qlist->ec == thinkpad_id.ec_model ||
420 qlist->ec == TPACPI_MATCH_ANY))
421 return qlist->quirks;
423 qlist_size--;
424 qlist++;
426 return 0;
430 /****************************************************************************
431 ****************************************************************************
433 * ACPI Helpers and device model
435 ****************************************************************************
436 ****************************************************************************/
438 /*************************************************************************
439 * ACPI basic handles
442 static acpi_handle root_handle;
444 #define TPACPI_HANDLE(object, parent, paths...) \
445 static acpi_handle object##_handle; \
446 static acpi_handle *object##_parent = &parent##_handle; \
447 static char *object##_path; \
448 static char *object##_paths[] = { paths }
450 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
451 "\\_SB.PCI.ISA.EC", /* 570 */
452 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
453 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
454 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
455 "\\_SB.PCI0.ICH3.EC0", /* R31 */
456 "\\_SB.PCI0.LPC.EC", /* all others */
459 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
460 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
462 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
463 /* T4x, X31, X40 */
464 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
465 "\\CMS", /* R40, R40e */
466 ); /* all others */
468 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
469 "^HKEY", /* R30, R31 */
470 "HKEY", /* all others */
471 ); /* 570 */
473 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
474 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
475 "\\_SB.PCI0.VID0", /* 770e */
476 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
477 "\\_SB.PCI0.AGP.VID", /* all others */
478 ); /* R30, R31 */
481 /*************************************************************************
482 * ACPI helpers
485 static int acpi_evalf(acpi_handle handle,
486 void *res, char *method, char *fmt, ...)
488 char *fmt0 = fmt;
489 struct acpi_object_list params;
490 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
491 struct acpi_buffer result, *resultp;
492 union acpi_object out_obj;
493 acpi_status status;
494 va_list ap;
495 char res_type;
496 int success;
497 int quiet;
499 if (!*fmt) {
500 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
501 return 0;
504 if (*fmt == 'q') {
505 quiet = 1;
506 fmt++;
507 } else
508 quiet = 0;
510 res_type = *(fmt++);
512 params.count = 0;
513 params.pointer = &in_objs[0];
515 va_start(ap, fmt);
516 while (*fmt) {
517 char c = *(fmt++);
518 switch (c) {
519 case 'd': /* int */
520 in_objs[params.count].integer.value = va_arg(ap, int);
521 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
522 break;
523 /* add more types as needed */
524 default:
525 printk(TPACPI_ERR "acpi_evalf() called "
526 "with invalid format character '%c'\n", c);
527 return 0;
530 va_end(ap);
532 if (res_type != 'v') {
533 result.length = sizeof(out_obj);
534 result.pointer = &out_obj;
535 resultp = &result;
536 } else
537 resultp = NULL;
539 status = acpi_evaluate_object(handle, method, &params, resultp);
541 switch (res_type) {
542 case 'd': /* int */
543 if (res)
544 *(int *)res = out_obj.integer.value;
545 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
546 break;
547 case 'v': /* void */
548 success = status == AE_OK;
549 break;
550 /* add more types as needed */
551 default:
552 printk(TPACPI_ERR "acpi_evalf() called "
553 "with invalid format character '%c'\n", res_type);
554 return 0;
557 if (!success && !quiet)
558 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
559 method, fmt0, status);
561 return success;
564 static int acpi_ec_read(int i, u8 *p)
566 int v;
568 if (ecrd_handle) {
569 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
570 return 0;
571 *p = v;
572 } else {
573 if (ec_read(i, p) < 0)
574 return 0;
577 return 1;
580 static int acpi_ec_write(int i, u8 v)
582 if (ecwr_handle) {
583 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
584 return 0;
585 } else {
586 if (ec_write(i, v) < 0)
587 return 0;
590 return 1;
593 static int issue_thinkpad_cmos_command(int cmos_cmd)
595 if (!cmos_handle)
596 return -ENXIO;
598 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
599 return -EIO;
601 return 0;
604 /*************************************************************************
605 * ACPI device model
608 #define TPACPI_ACPIHANDLE_INIT(object) \
609 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
610 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
612 static void drv_acpi_handle_init(char *name,
613 acpi_handle *handle, acpi_handle parent,
614 char **paths, int num_paths, char **path)
616 int i;
617 acpi_status status;
619 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
620 name);
622 for (i = 0; i < num_paths; i++) {
623 status = acpi_get_handle(parent, paths[i], handle);
624 if (ACPI_SUCCESS(status)) {
625 *path = paths[i];
626 dbg_printk(TPACPI_DBG_INIT,
627 "Found ACPI handle %s for %s\n",
628 *path, name);
629 return;
633 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
634 name);
635 *handle = NULL;
638 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
640 struct ibm_struct *ibm = data;
642 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
643 return;
645 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
646 return;
648 ibm->acpi->notify(ibm, event);
651 static int __init setup_acpi_notify(struct ibm_struct *ibm)
653 acpi_status status;
654 int rc;
656 BUG_ON(!ibm->acpi);
658 if (!*ibm->acpi->handle)
659 return 0;
661 vdbg_printk(TPACPI_DBG_INIT,
662 "setting up ACPI notify for %s\n", ibm->name);
664 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
665 if (rc < 0) {
666 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
667 ibm->name, rc);
668 return -ENODEV;
671 ibm->acpi->device->driver_data = ibm;
672 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
673 TPACPI_ACPI_EVENT_PREFIX,
674 ibm->name);
676 status = acpi_install_notify_handler(*ibm->acpi->handle,
677 ibm->acpi->type, dispatch_acpi_notify, ibm);
678 if (ACPI_FAILURE(status)) {
679 if (status == AE_ALREADY_EXISTS) {
680 printk(TPACPI_NOTICE
681 "another device driver is already "
682 "handling %s events\n", ibm->name);
683 } else {
684 printk(TPACPI_ERR
685 "acpi_install_notify_handler(%s) failed: %d\n",
686 ibm->name, status);
688 return -ENODEV;
690 ibm->flags.acpi_notify_installed = 1;
691 return 0;
694 static int __init tpacpi_device_add(struct acpi_device *device)
696 return 0;
699 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
701 int rc;
703 dbg_printk(TPACPI_DBG_INIT,
704 "registering %s as an ACPI driver\n", ibm->name);
706 BUG_ON(!ibm->acpi);
708 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
709 if (!ibm->acpi->driver) {
710 printk(TPACPI_ERR
711 "failed to allocate memory for ibm->acpi->driver\n");
712 return -ENOMEM;
715 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
716 ibm->acpi->driver->ids = ibm->acpi->hid;
718 ibm->acpi->driver->ops.add = &tpacpi_device_add;
720 rc = acpi_bus_register_driver(ibm->acpi->driver);
721 if (rc < 0) {
722 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
723 ibm->name, rc);
724 kfree(ibm->acpi->driver);
725 ibm->acpi->driver = NULL;
726 } else if (!rc)
727 ibm->flags.acpi_driver_registered = 1;
729 return rc;
733 /****************************************************************************
734 ****************************************************************************
736 * Procfs Helpers
738 ****************************************************************************
739 ****************************************************************************/
741 static int dispatch_procfs_read(char *page, char **start, off_t off,
742 int count, int *eof, void *data)
744 struct ibm_struct *ibm = data;
745 int len;
747 if (!ibm || !ibm->read)
748 return -EINVAL;
750 len = ibm->read(page);
751 if (len < 0)
752 return len;
754 if (len <= off + count)
755 *eof = 1;
756 *start = page + off;
757 len -= off;
758 if (len > count)
759 len = count;
760 if (len < 0)
761 len = 0;
763 return len;
766 static int dispatch_procfs_write(struct file *file,
767 const char __user *userbuf,
768 unsigned long count, void *data)
770 struct ibm_struct *ibm = data;
771 char *kernbuf;
772 int ret;
774 if (!ibm || !ibm->write)
775 return -EINVAL;
776 if (count > PAGE_SIZE - 2)
777 return -EINVAL;
779 kernbuf = kmalloc(count + 2, GFP_KERNEL);
780 if (!kernbuf)
781 return -ENOMEM;
783 if (copy_from_user(kernbuf, userbuf, count)) {
784 kfree(kernbuf);
785 return -EFAULT;
788 kernbuf[count] = 0;
789 strcat(kernbuf, ",");
790 ret = ibm->write(kernbuf);
791 if (ret == 0)
792 ret = count;
794 kfree(kernbuf);
796 return ret;
799 static char *next_cmd(char **cmds)
801 char *start = *cmds;
802 char *end;
804 while ((end = strchr(start, ',')) && end == start)
805 start = end + 1;
807 if (!end)
808 return NULL;
810 *end = 0;
811 *cmds = end + 1;
812 return start;
816 /****************************************************************************
817 ****************************************************************************
819 * Device model: input, hwmon and platform
821 ****************************************************************************
822 ****************************************************************************/
824 static struct platform_device *tpacpi_pdev;
825 static struct platform_device *tpacpi_sensors_pdev;
826 static struct device *tpacpi_hwmon;
827 static struct input_dev *tpacpi_inputdev;
828 static struct mutex tpacpi_inputdev_send_mutex;
829 static LIST_HEAD(tpacpi_all_drivers);
831 static int tpacpi_suspend_handler(struct platform_device *pdev,
832 pm_message_t state)
834 struct ibm_struct *ibm, *itmp;
836 list_for_each_entry_safe(ibm, itmp,
837 &tpacpi_all_drivers,
838 all_drivers) {
839 if (ibm->suspend)
840 (ibm->suspend)(state);
843 return 0;
846 static int tpacpi_resume_handler(struct platform_device *pdev)
848 struct ibm_struct *ibm, *itmp;
850 list_for_each_entry_safe(ibm, itmp,
851 &tpacpi_all_drivers,
852 all_drivers) {
853 if (ibm->resume)
854 (ibm->resume)();
857 return 0;
860 static void tpacpi_shutdown_handler(struct platform_device *pdev)
862 struct ibm_struct *ibm, *itmp;
864 list_for_each_entry_safe(ibm, itmp,
865 &tpacpi_all_drivers,
866 all_drivers) {
867 if (ibm->shutdown)
868 (ibm->shutdown)();
872 static struct platform_driver tpacpi_pdriver = {
873 .driver = {
874 .name = TPACPI_DRVR_NAME,
875 .owner = THIS_MODULE,
877 .suspend = tpacpi_suspend_handler,
878 .resume = tpacpi_resume_handler,
879 .shutdown = tpacpi_shutdown_handler,
882 static struct platform_driver tpacpi_hwmon_pdriver = {
883 .driver = {
884 .name = TPACPI_HWMON_DRVR_NAME,
885 .owner = THIS_MODULE,
889 /*************************************************************************
890 * sysfs support helpers
893 struct attribute_set {
894 unsigned int members, max_members;
895 struct attribute_group group;
898 struct attribute_set_obj {
899 struct attribute_set s;
900 struct attribute *a;
901 } __attribute__((packed));
903 static struct attribute_set *create_attr_set(unsigned int max_members,
904 const char *name)
906 struct attribute_set_obj *sobj;
908 if (max_members == 0)
909 return NULL;
911 /* Allocates space for implicit NULL at the end too */
912 sobj = kzalloc(sizeof(struct attribute_set_obj) +
913 max_members * sizeof(struct attribute *),
914 GFP_KERNEL);
915 if (!sobj)
916 return NULL;
917 sobj->s.max_members = max_members;
918 sobj->s.group.attrs = &sobj->a;
919 sobj->s.group.name = name;
921 return &sobj->s;
924 #define destroy_attr_set(_set) \
925 kfree(_set);
927 /* not multi-threaded safe, use it in a single thread per set */
928 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
930 if (!s || !attr)
931 return -EINVAL;
933 if (s->members >= s->max_members)
934 return -ENOMEM;
936 s->group.attrs[s->members] = attr;
937 s->members++;
939 return 0;
942 static int add_many_to_attr_set(struct attribute_set *s,
943 struct attribute **attr,
944 unsigned int count)
946 int i, res;
948 for (i = 0; i < count; i++) {
949 res = add_to_attr_set(s, attr[i]);
950 if (res)
951 return res;
954 return 0;
957 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
959 sysfs_remove_group(kobj, &s->group);
960 destroy_attr_set(s);
963 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
964 sysfs_create_group(_kobj, &_attr_set->group)
966 static int parse_strtoul(const char *buf,
967 unsigned long max, unsigned long *value)
969 char *endp;
971 while (*buf && isspace(*buf))
972 buf++;
973 *value = simple_strtoul(buf, &endp, 0);
974 while (*endp && isspace(*endp))
975 endp++;
976 if (*endp || *value > max)
977 return -EINVAL;
979 return 0;
982 static void tpacpi_disable_brightness_delay(void)
984 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
985 printk(TPACPI_NOTICE
986 "ACPI backlight control delay disabled\n");
989 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
991 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
992 union acpi_object *obj;
993 int rc;
995 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
996 obj = (union acpi_object *)buffer.pointer;
997 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
998 printk(TPACPI_ERR "Unknown _BCL data, "
999 "please report this to %s\n", TPACPI_MAIL);
1000 rc = 0;
1001 } else {
1002 rc = obj->package.count;
1004 } else {
1005 return 0;
1008 kfree(buffer.pointer);
1009 return rc;
1012 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1013 u32 lvl, void *context, void **rv)
1015 char name[ACPI_PATH_SEGMENT_LENGTH];
1016 struct acpi_buffer buffer = { sizeof(name), &name };
1018 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1019 !strncmp("_BCL", name, sizeof(name) - 1)) {
1020 BUG_ON(!rv || !*rv);
1021 **(int **)rv = tpacpi_query_bcl_levels(handle);
1022 return AE_CTRL_TERMINATE;
1023 } else {
1024 return AE_OK;
1029 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1031 static int __init tpacpi_check_std_acpi_brightness_support(void)
1033 int status;
1034 int bcl_levels = 0;
1035 void *bcl_ptr = &bcl_levels;
1037 if (!vid_handle) {
1038 TPACPI_ACPIHANDLE_INIT(vid);
1040 if (!vid_handle)
1041 return 0;
1044 * Search for a _BCL method, and execute it. This is safe on all
1045 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1046 * BIOS in ACPI backlight control mode. We do NOT have to care
1047 * about calling the _BCL method in an enabled video device, any
1048 * will do for our purposes.
1051 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1052 tpacpi_acpi_walk_find_bcl, NULL,
1053 &bcl_ptr);
1055 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1056 tp_features.bright_acpimode = 1;
1057 return (bcl_levels - 2);
1060 return 0;
1063 static int __init tpacpi_new_rfkill(const unsigned int id,
1064 struct rfkill **rfk,
1065 const enum rfkill_type rfktype,
1066 const char *name,
1067 const bool set_default,
1068 int (*toggle_radio)(void *, enum rfkill_state),
1069 int (*get_state)(void *, enum rfkill_state *))
1071 int res;
1072 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1074 res = get_state(NULL, &initial_state);
1075 if (res < 0) {
1076 printk(TPACPI_ERR
1077 "failed to read initial state for %s, error %d; "
1078 "will turn radio off\n", name, res);
1079 } else if (set_default) {
1080 /* try to set the initial state as the default for the rfkill
1081 * type, since we ask the firmware to preserve it across S5 in
1082 * NVRAM */
1083 if (rfkill_set_default(rfktype,
1084 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1085 RFKILL_STATE_UNBLOCKED :
1086 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1087 vdbg_printk(TPACPI_DBG_RFKILL,
1088 "Default state for %s cannot be changed\n",
1089 name);
1092 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1093 if (!*rfk) {
1094 printk(TPACPI_ERR
1095 "failed to allocate memory for rfkill class\n");
1096 return -ENOMEM;
1099 (*rfk)->name = name;
1100 (*rfk)->get_state = get_state;
1101 (*rfk)->toggle_radio = toggle_radio;
1102 (*rfk)->state = initial_state;
1104 res = rfkill_register(*rfk);
1105 if (res < 0) {
1106 printk(TPACPI_ERR
1107 "failed to register %s rfkill switch: %d\n",
1108 name, res);
1109 rfkill_free(*rfk);
1110 *rfk = NULL;
1111 return res;
1114 return 0;
1117 static void printk_deprecated_attribute(const char * const what,
1118 const char * const details)
1120 tpacpi_log_usertask("deprecated sysfs attribute");
1121 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1122 "will be removed. %s\n",
1123 what, details);
1126 static void printk_deprecated_rfkill_attribute(const char * const what)
1128 printk_deprecated_attribute(what,
1129 "Please switch to generic rfkill before year 2010");
1132 /*************************************************************************
1133 * thinkpad-acpi driver attributes
1136 /* interface_version --------------------------------------------------- */
1137 static ssize_t tpacpi_driver_interface_version_show(
1138 struct device_driver *drv,
1139 char *buf)
1141 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1144 static DRIVER_ATTR(interface_version, S_IRUGO,
1145 tpacpi_driver_interface_version_show, NULL);
1147 /* debug_level --------------------------------------------------------- */
1148 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1149 char *buf)
1151 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1154 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1155 const char *buf, size_t count)
1157 unsigned long t;
1159 if (parse_strtoul(buf, 0xffff, &t))
1160 return -EINVAL;
1162 dbg_level = t;
1164 return count;
1167 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1168 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1170 /* version ------------------------------------------------------------- */
1171 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1172 char *buf)
1174 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1175 TPACPI_DESC, TPACPI_VERSION);
1178 static DRIVER_ATTR(version, S_IRUGO,
1179 tpacpi_driver_version_show, NULL);
1181 /* --------------------------------------------------------------------- */
1183 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1185 static void tpacpi_send_radiosw_update(void);
1187 /* wlsw_emulstate ------------------------------------------------------ */
1188 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1189 char *buf)
1191 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1194 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1195 const char *buf, size_t count)
1197 unsigned long t;
1199 if (parse_strtoul(buf, 1, &t))
1200 return -EINVAL;
1202 if (tpacpi_wlsw_emulstate != t) {
1203 tpacpi_wlsw_emulstate = !!t;
1204 tpacpi_send_radiosw_update();
1205 } else
1206 tpacpi_wlsw_emulstate = !!t;
1208 return count;
1211 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1212 tpacpi_driver_wlsw_emulstate_show,
1213 tpacpi_driver_wlsw_emulstate_store);
1215 /* bluetooth_emulstate ------------------------------------------------- */
1216 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1217 struct device_driver *drv,
1218 char *buf)
1220 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1223 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1224 struct device_driver *drv,
1225 const char *buf, size_t count)
1227 unsigned long t;
1229 if (parse_strtoul(buf, 1, &t))
1230 return -EINVAL;
1232 tpacpi_bluetooth_emulstate = !!t;
1234 return count;
1237 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1238 tpacpi_driver_bluetooth_emulstate_show,
1239 tpacpi_driver_bluetooth_emulstate_store);
1241 /* wwan_emulstate ------------------------------------------------- */
1242 static ssize_t tpacpi_driver_wwan_emulstate_show(
1243 struct device_driver *drv,
1244 char *buf)
1246 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1249 static ssize_t tpacpi_driver_wwan_emulstate_store(
1250 struct device_driver *drv,
1251 const char *buf, size_t count)
1253 unsigned long t;
1255 if (parse_strtoul(buf, 1, &t))
1256 return -EINVAL;
1258 tpacpi_wwan_emulstate = !!t;
1260 return count;
1263 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1264 tpacpi_driver_wwan_emulstate_show,
1265 tpacpi_driver_wwan_emulstate_store);
1267 /* uwb_emulstate ------------------------------------------------- */
1268 static ssize_t tpacpi_driver_uwb_emulstate_show(
1269 struct device_driver *drv,
1270 char *buf)
1272 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1275 static ssize_t tpacpi_driver_uwb_emulstate_store(
1276 struct device_driver *drv,
1277 const char *buf, size_t count)
1279 unsigned long t;
1281 if (parse_strtoul(buf, 1, &t))
1282 return -EINVAL;
1284 tpacpi_uwb_emulstate = !!t;
1286 return count;
1289 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1290 tpacpi_driver_uwb_emulstate_show,
1291 tpacpi_driver_uwb_emulstate_store);
1292 #endif
1294 /* --------------------------------------------------------------------- */
1296 static struct driver_attribute *tpacpi_driver_attributes[] = {
1297 &driver_attr_debug_level, &driver_attr_version,
1298 &driver_attr_interface_version,
1301 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1303 int i, res;
1305 i = 0;
1306 res = 0;
1307 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1308 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1309 i++;
1312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1313 if (!res && dbg_wlswemul)
1314 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1315 if (!res && dbg_bluetoothemul)
1316 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1317 if (!res && dbg_wwanemul)
1318 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1319 if (!res && dbg_uwbemul)
1320 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1321 #endif
1323 return res;
1326 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1328 int i;
1330 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1331 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1333 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1334 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1335 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1336 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1337 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1338 #endif
1341 /*************************************************************************
1342 * Firmware Data
1346 * Table of recommended minimum BIOS versions
1348 * Reasons for listing:
1349 * 1. Stable BIOS, listed because the unknown ammount of
1350 * bugs and bad ACPI behaviour on older versions
1352 * 2. BIOS or EC fw with known bugs that trigger on Linux
1354 * 3. BIOS with known reduced functionality in older versions
1356 * We recommend the latest BIOS and EC version.
1357 * We only support the latest BIOS and EC fw version as a rule.
1359 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1360 * Information from users in ThinkWiki
1362 * WARNING: we use this table also to detect that the machine is
1363 * a ThinkPad in some cases, so don't remove entries lightly.
1366 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1367 { .vendor = (__v), \
1368 .bios = TPID(__id1, __id2), \
1369 .ec = TPACPI_MATCH_ANY, \
1370 .quirks = TPACPI_MATCH_ANY << 16 \
1371 | (__bv1) << 8 | (__bv2) }
1373 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1374 __eid1, __eid2, __ev1, __ev2) \
1375 { .vendor = (__v), \
1376 .bios = TPID(__bid1, __bid2), \
1377 .ec = TPID(__eid1, __eid2), \
1378 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1379 | (__bv1) << 8 | (__bv2) }
1381 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1382 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1384 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1385 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1386 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1388 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1389 __eid1, __eid2, __ev1, __ev2) \
1390 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1391 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1393 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1394 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1396 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1397 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1398 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1400 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1401 __eid1, __eid2, __ev1, __ev2) \
1402 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1403 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1405 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1406 /* Numeric models ------------------ */
1407 /* FW MODEL BIOS VERS */
1408 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1409 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1410 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1411 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1412 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1413 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1414 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1415 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1416 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1418 /* A-series ------------------------- */
1419 /* FW MODEL BIOS VERS EC VERS */
1420 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1421 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1422 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1423 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1424 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1425 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1426 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1427 TPV_QI0('1', '3', '2', '0'), /* A22m */
1428 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1429 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1430 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1432 /* G-series ------------------------- */
1433 /* FW MODEL BIOS VERS */
1434 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1435 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1437 /* R-series, T-series --------------- */
1438 /* FW MODEL BIOS VERS EC VERS */
1439 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1440 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1441 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1442 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1443 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1444 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1445 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1446 T40/p, T41/p, T42/p (1) */
1447 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1448 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1449 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1450 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1452 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1453 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1454 TPV_QI0('1', '6', '3', '2'), /* T22 */
1455 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1456 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1457 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1459 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1460 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1461 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1463 /* BIOS FW BIOS VERS EC FW EC VERS */
1464 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1465 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1467 /* X-series ------------------------- */
1468 /* FW MODEL BIOS VERS EC VERS */
1469 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1470 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1471 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1472 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1473 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1474 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1475 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1477 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1478 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1480 /* (0) - older versions lack DMI EC fw string and functionality */
1481 /* (1) - older versions known to lack functionality */
1484 #undef TPV_QL1
1485 #undef TPV_QL0
1486 #undef TPV_QI2
1487 #undef TPV_QI1
1488 #undef TPV_QI0
1489 #undef TPV_Q_X
1490 #undef TPV_Q
1492 static void __init tpacpi_check_outdated_fw(void)
1494 unsigned long fwvers;
1495 u16 ec_version, bios_version;
1497 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1498 ARRAY_SIZE(tpacpi_bios_version_qtable));
1500 if (!fwvers)
1501 return;
1503 bios_version = fwvers & 0xffffU;
1504 ec_version = (fwvers >> 16) & 0xffffU;
1506 /* note that unknown versions are set to 0x0000 and we use that */
1507 if ((bios_version > thinkpad_id.bios_release) ||
1508 (ec_version > thinkpad_id.ec_release &&
1509 ec_version != TPACPI_MATCH_ANY)) {
1511 * The changelogs would let us track down the exact
1512 * reason, but it is just too much of a pain to track
1513 * it. We only list BIOSes that are either really
1514 * broken, or really stable to begin with, so it is
1515 * best if the user upgrades the firmware anyway.
1517 printk(TPACPI_WARN
1518 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1519 printk(TPACPI_WARN
1520 "WARNING: This firmware may be missing critical bug "
1521 "fixes and/or important features\n");
1525 static bool __init tpacpi_is_fw_known(void)
1527 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1528 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1531 /****************************************************************************
1532 ****************************************************************************
1534 * Subdrivers
1536 ****************************************************************************
1537 ****************************************************************************/
1539 /*************************************************************************
1540 * thinkpad-acpi init subdriver
1543 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1545 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1546 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1548 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1549 (thinkpad_id.bios_version_str) ?
1550 thinkpad_id.bios_version_str : "unknown",
1551 (thinkpad_id.ec_version_str) ?
1552 thinkpad_id.ec_version_str : "unknown");
1554 if (thinkpad_id.vendor && thinkpad_id.model_str)
1555 printk(TPACPI_INFO "%s %s, model %s\n",
1556 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1557 "IBM" : ((thinkpad_id.vendor ==
1558 PCI_VENDOR_ID_LENOVO) ?
1559 "Lenovo" : "Unknown vendor"),
1560 thinkpad_id.model_str,
1561 (thinkpad_id.nummodel_str) ?
1562 thinkpad_id.nummodel_str : "unknown");
1564 tpacpi_check_outdated_fw();
1565 return 0;
1568 static int thinkpad_acpi_driver_read(char *p)
1570 int len = 0;
1572 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1573 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1575 return len;
1578 static struct ibm_struct thinkpad_acpi_driver_data = {
1579 .name = "driver",
1580 .read = thinkpad_acpi_driver_read,
1583 /*************************************************************************
1584 * Hotkey subdriver
1587 enum { /* hot key scan codes (derived from ACPI DSDT) */
1588 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1589 TP_ACPI_HOTKEYSCAN_FNF2,
1590 TP_ACPI_HOTKEYSCAN_FNF3,
1591 TP_ACPI_HOTKEYSCAN_FNF4,
1592 TP_ACPI_HOTKEYSCAN_FNF5,
1593 TP_ACPI_HOTKEYSCAN_FNF6,
1594 TP_ACPI_HOTKEYSCAN_FNF7,
1595 TP_ACPI_HOTKEYSCAN_FNF8,
1596 TP_ACPI_HOTKEYSCAN_FNF9,
1597 TP_ACPI_HOTKEYSCAN_FNF10,
1598 TP_ACPI_HOTKEYSCAN_FNF11,
1599 TP_ACPI_HOTKEYSCAN_FNF12,
1600 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1601 TP_ACPI_HOTKEYSCAN_FNINSERT,
1602 TP_ACPI_HOTKEYSCAN_FNDELETE,
1603 TP_ACPI_HOTKEYSCAN_FNHOME,
1604 TP_ACPI_HOTKEYSCAN_FNEND,
1605 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1606 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1607 TP_ACPI_HOTKEYSCAN_FNSPACE,
1608 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1609 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1610 TP_ACPI_HOTKEYSCAN_MUTE,
1611 TP_ACPI_HOTKEYSCAN_THINKPAD,
1614 enum { /* Keys available through NVRAM polling */
1615 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1616 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1619 enum { /* Positions of some of the keys in hotkey masks */
1620 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1621 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1622 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1623 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1624 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1625 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1626 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1627 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1628 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1629 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1630 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1633 enum { /* NVRAM to ACPI HKEY group map */
1634 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1635 TP_ACPI_HKEY_ZOOM_MASK |
1636 TP_ACPI_HKEY_DISPSWTCH_MASK |
1637 TP_ACPI_HKEY_HIBERNATE_MASK,
1638 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1639 TP_ACPI_HKEY_BRGHTDWN_MASK,
1640 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1641 TP_ACPI_HKEY_VOLDWN_MASK |
1642 TP_ACPI_HKEY_MUTE_MASK,
1645 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1646 struct tp_nvram_state {
1647 u16 thinkpad_toggle:1;
1648 u16 zoom_toggle:1;
1649 u16 display_toggle:1;
1650 u16 thinklight_toggle:1;
1651 u16 hibernate_toggle:1;
1652 u16 displayexp_toggle:1;
1653 u16 display_state:1;
1654 u16 brightness_toggle:1;
1655 u16 volume_toggle:1;
1656 u16 mute:1;
1658 u8 brightness_level;
1659 u8 volume_level;
1662 /* kthread for the hotkey poller */
1663 static struct task_struct *tpacpi_hotkey_task;
1665 /* Acquired while the poller kthread is running, use to sync start/stop */
1666 static struct mutex hotkey_thread_mutex;
1669 * Acquire mutex to write poller control variables.
1670 * Increment hotkey_config_change when changing them.
1672 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1674 static struct mutex hotkey_thread_data_mutex;
1675 static unsigned int hotkey_config_change;
1678 * hotkey poller control variables
1680 * Must be atomic or readers will also need to acquire mutex
1682 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1683 static unsigned int hotkey_poll_freq = 10; /* Hz */
1685 #define HOTKEY_CONFIG_CRITICAL_START \
1686 do { \
1687 mutex_lock(&hotkey_thread_data_mutex); \
1688 hotkey_config_change++; \
1689 } while (0);
1690 #define HOTKEY_CONFIG_CRITICAL_END \
1691 mutex_unlock(&hotkey_thread_data_mutex);
1693 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1695 #define hotkey_source_mask 0U
1696 #define HOTKEY_CONFIG_CRITICAL_START
1697 #define HOTKEY_CONFIG_CRITICAL_END
1699 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1701 static struct mutex hotkey_mutex;
1703 static enum { /* Reasons for waking up */
1704 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1705 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1706 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1707 } hotkey_wakeup_reason;
1709 static int hotkey_autosleep_ack;
1711 static u32 hotkey_orig_mask;
1712 static u32 hotkey_all_mask;
1713 static u32 hotkey_reserved_mask;
1714 static u32 hotkey_mask;
1716 static unsigned int hotkey_report_mode;
1718 static u16 *hotkey_keycode_map;
1720 static struct attribute_set *hotkey_dev_attributes;
1722 /* HKEY.MHKG() return bits */
1723 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1725 static int hotkey_get_wlsw(int *status)
1727 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1728 if (dbg_wlswemul) {
1729 *status = !!tpacpi_wlsw_emulstate;
1730 return 0;
1732 #endif
1733 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1734 return -EIO;
1735 return 0;
1738 static int hotkey_get_tablet_mode(int *status)
1740 int s;
1742 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1743 return -EIO;
1745 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1746 return 0;
1750 * Call with hotkey_mutex held
1752 static int hotkey_mask_get(void)
1754 u32 m = 0;
1756 if (tp_features.hotkey_mask) {
1757 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1758 return -EIO;
1760 HOTKEY_CONFIG_CRITICAL_START
1761 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1762 HOTKEY_CONFIG_CRITICAL_END
1764 return 0;
1768 * Call with hotkey_mutex held
1770 static int hotkey_mask_set(u32 mask)
1772 int i;
1773 int rc = 0;
1775 if (tp_features.hotkey_mask) {
1776 if (!tp_warned.hotkey_mask_ff &&
1777 (mask == 0xffff || mask == 0xffffff ||
1778 mask == 0xffffffff)) {
1779 tp_warned.hotkey_mask_ff = 1;
1780 printk(TPACPI_NOTICE
1781 "setting the hotkey mask to 0x%08x is likely "
1782 "not the best way to go about it\n", mask);
1783 printk(TPACPI_NOTICE
1784 "please consider using the driver defaults, "
1785 "and refer to up-to-date thinkpad-acpi "
1786 "documentation\n");
1789 HOTKEY_CONFIG_CRITICAL_START
1790 for (i = 0; i < 32; i++) {
1791 u32 m = 1 << i;
1792 /* enable in firmware mask only keys not in NVRAM
1793 * mode, but enable the key in the cached hotkey_mask
1794 * regardless of mode, or the key will end up
1795 * disabled by hotkey_mask_get() */
1796 if (!acpi_evalf(hkey_handle,
1797 NULL, "MHKM", "vdd", i + 1,
1798 !!((mask & ~hotkey_source_mask) & m))) {
1799 rc = -EIO;
1800 break;
1801 } else {
1802 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1805 HOTKEY_CONFIG_CRITICAL_END
1807 /* hotkey_mask_get must be called unconditionally below */
1808 if (!hotkey_mask_get() && !rc &&
1809 (hotkey_mask & ~hotkey_source_mask) !=
1810 (mask & ~hotkey_source_mask)) {
1811 printk(TPACPI_NOTICE
1812 "requested hot key mask 0x%08x, but "
1813 "firmware forced it to 0x%08x\n",
1814 mask, hotkey_mask);
1816 } else {
1817 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1818 HOTKEY_CONFIG_CRITICAL_START
1819 hotkey_mask = mask & hotkey_source_mask;
1820 HOTKEY_CONFIG_CRITICAL_END
1821 hotkey_mask_get();
1822 if (hotkey_mask != mask) {
1823 printk(TPACPI_NOTICE
1824 "requested hot key mask 0x%08x, "
1825 "forced to 0x%08x (NVRAM poll mask is "
1826 "0x%08x): no firmware mask support\n",
1827 mask, hotkey_mask, hotkey_source_mask);
1829 #else
1830 hotkey_mask_get();
1831 rc = -ENXIO;
1832 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1835 return rc;
1838 static int hotkey_status_get(int *status)
1840 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1841 return -EIO;
1843 return 0;
1846 static int hotkey_status_set(bool enable)
1848 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1849 return -EIO;
1851 return 0;
1854 static void tpacpi_input_send_tabletsw(void)
1856 int state;
1858 if (tp_features.hotkey_tablet &&
1859 !hotkey_get_tablet_mode(&state)) {
1860 mutex_lock(&tpacpi_inputdev_send_mutex);
1862 input_report_switch(tpacpi_inputdev,
1863 SW_TABLET_MODE, !!state);
1864 input_sync(tpacpi_inputdev);
1866 mutex_unlock(&tpacpi_inputdev_send_mutex);
1870 static void tpacpi_input_send_key(unsigned int scancode)
1872 unsigned int keycode;
1874 keycode = hotkey_keycode_map[scancode];
1876 if (keycode != KEY_RESERVED) {
1877 mutex_lock(&tpacpi_inputdev_send_mutex);
1879 input_report_key(tpacpi_inputdev, keycode, 1);
1880 if (keycode == KEY_UNKNOWN)
1881 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1882 scancode);
1883 input_sync(tpacpi_inputdev);
1885 input_report_key(tpacpi_inputdev, keycode, 0);
1886 if (keycode == KEY_UNKNOWN)
1887 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1888 scancode);
1889 input_sync(tpacpi_inputdev);
1891 mutex_unlock(&tpacpi_inputdev_send_mutex);
1895 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1896 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1898 static void tpacpi_hotkey_send_key(unsigned int scancode)
1900 tpacpi_input_send_key(scancode);
1901 if (hotkey_report_mode < 2) {
1902 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1903 0x80, 0x1001 + scancode);
1907 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1909 u8 d;
1911 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1912 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1913 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1914 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1915 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1916 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1918 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1919 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1920 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1922 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1923 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1924 n->displayexp_toggle =
1925 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1927 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1928 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1929 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1930 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1931 n->brightness_toggle =
1932 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1934 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1935 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1936 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1937 >> TP_NVRAM_POS_LEVEL_VOLUME;
1938 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1939 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1943 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1944 do { \
1945 if ((mask & (1 << __scancode)) && \
1946 oldn->__member != newn->__member) \
1947 tpacpi_hotkey_send_key(__scancode); \
1948 } while (0)
1950 #define TPACPI_MAY_SEND_KEY(__scancode) \
1951 do { if (mask & (1 << __scancode)) \
1952 tpacpi_hotkey_send_key(__scancode); } while (0)
1954 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1955 struct tp_nvram_state *newn,
1956 u32 mask)
1958 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1959 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1960 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1961 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1963 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1965 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1967 /* handle volume */
1968 if (oldn->volume_toggle != newn->volume_toggle) {
1969 if (oldn->mute != newn->mute) {
1970 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1972 if (oldn->volume_level > newn->volume_level) {
1973 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1974 } else if (oldn->volume_level < newn->volume_level) {
1975 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1976 } else if (oldn->mute == newn->mute) {
1977 /* repeated key presses that didn't change state */
1978 if (newn->mute) {
1979 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1980 } else if (newn->volume_level != 0) {
1981 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1982 } else {
1983 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1988 /* handle brightness */
1989 if (oldn->brightness_toggle != newn->brightness_toggle) {
1990 if (oldn->brightness_level < newn->brightness_level) {
1991 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1992 } else if (oldn->brightness_level > newn->brightness_level) {
1993 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1994 } else {
1995 /* repeated key presses that didn't change state */
1996 if (newn->brightness_level != 0) {
1997 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1998 } else {
1999 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2005 #undef TPACPI_COMPARE_KEY
2006 #undef TPACPI_MAY_SEND_KEY
2008 static int hotkey_kthread(void *data)
2010 struct tp_nvram_state s[2];
2011 u32 mask;
2012 unsigned int si, so;
2013 unsigned long t;
2014 unsigned int change_detector, must_reset;
2015 unsigned int poll_freq;
2017 mutex_lock(&hotkey_thread_mutex);
2019 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2020 goto exit;
2022 set_freezable();
2024 so = 0;
2025 si = 1;
2026 t = 0;
2028 /* Initial state for compares */
2029 mutex_lock(&hotkey_thread_data_mutex);
2030 change_detector = hotkey_config_change;
2031 mask = hotkey_source_mask & hotkey_mask;
2032 poll_freq = hotkey_poll_freq;
2033 mutex_unlock(&hotkey_thread_data_mutex);
2034 hotkey_read_nvram(&s[so], mask);
2036 while (!kthread_should_stop()) {
2037 if (t == 0) {
2038 if (likely(poll_freq))
2039 t = 1000/poll_freq;
2040 else
2041 t = 100; /* should never happen... */
2043 t = msleep_interruptible(t);
2044 if (unlikely(kthread_should_stop()))
2045 break;
2046 must_reset = try_to_freeze();
2047 if (t > 0 && !must_reset)
2048 continue;
2050 mutex_lock(&hotkey_thread_data_mutex);
2051 if (must_reset || hotkey_config_change != change_detector) {
2052 /* forget old state on thaw or config change */
2053 si = so;
2054 t = 0;
2055 change_detector = hotkey_config_change;
2057 mask = hotkey_source_mask & hotkey_mask;
2058 poll_freq = hotkey_poll_freq;
2059 mutex_unlock(&hotkey_thread_data_mutex);
2061 if (likely(mask)) {
2062 hotkey_read_nvram(&s[si], mask);
2063 if (likely(si != so)) {
2064 hotkey_compare_and_issue_event(&s[so], &s[si],
2065 mask);
2069 so = si;
2070 si ^= 1;
2073 exit:
2074 mutex_unlock(&hotkey_thread_mutex);
2075 return 0;
2078 /* call with hotkey_mutex held */
2079 static void hotkey_poll_stop_sync(void)
2081 if (tpacpi_hotkey_task) {
2082 if (frozen(tpacpi_hotkey_task) ||
2083 freezing(tpacpi_hotkey_task))
2084 thaw_process(tpacpi_hotkey_task);
2086 kthread_stop(tpacpi_hotkey_task);
2087 tpacpi_hotkey_task = NULL;
2088 mutex_lock(&hotkey_thread_mutex);
2089 /* at this point, the thread did exit */
2090 mutex_unlock(&hotkey_thread_mutex);
2094 /* call with hotkey_mutex held */
2095 static void hotkey_poll_setup(bool may_warn)
2097 u32 hotkeys_to_poll = hotkey_source_mask & hotkey_mask;
2099 if (hotkeys_to_poll != 0 && hotkey_poll_freq > 0 &&
2100 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2101 if (!tpacpi_hotkey_task) {
2102 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2103 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2104 if (IS_ERR(tpacpi_hotkey_task)) {
2105 tpacpi_hotkey_task = NULL;
2106 printk(TPACPI_ERR
2107 "could not create kernel thread "
2108 "for hotkey polling\n");
2111 } else {
2112 hotkey_poll_stop_sync();
2113 if (may_warn && hotkeys_to_poll != 0 &&
2114 hotkey_poll_freq == 0) {
2115 printk(TPACPI_NOTICE
2116 "hot keys 0x%08x require polling, "
2117 "which is currently disabled\n",
2118 hotkeys_to_poll);
2123 static void hotkey_poll_setup_safe(bool may_warn)
2125 mutex_lock(&hotkey_mutex);
2126 hotkey_poll_setup(may_warn);
2127 mutex_unlock(&hotkey_mutex);
2130 /* call with hotkey_mutex held */
2131 static void hotkey_poll_set_freq(unsigned int freq)
2133 if (!freq)
2134 hotkey_poll_stop_sync();
2136 HOTKEY_CONFIG_CRITICAL_START
2137 hotkey_poll_freq = freq;
2138 HOTKEY_CONFIG_CRITICAL_END
2141 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2143 static void hotkey_poll_setup_safe(bool __unused)
2147 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2149 static int hotkey_inputdev_open(struct input_dev *dev)
2151 switch (tpacpi_lifecycle) {
2152 case TPACPI_LIFE_INIT:
2154 * hotkey_init will call hotkey_poll_setup_safe
2155 * at the appropriate moment
2157 return 0;
2158 case TPACPI_LIFE_EXITING:
2159 return -EBUSY;
2160 case TPACPI_LIFE_RUNNING:
2161 hotkey_poll_setup_safe(false);
2162 return 0;
2165 /* Should only happen if tpacpi_lifecycle is corrupt */
2166 BUG();
2167 return -EBUSY;
2170 static void hotkey_inputdev_close(struct input_dev *dev)
2172 /* disable hotkey polling when possible */
2173 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2174 hotkey_poll_setup_safe(false);
2177 /* sysfs hotkey enable ------------------------------------------------- */
2178 static ssize_t hotkey_enable_show(struct device *dev,
2179 struct device_attribute *attr,
2180 char *buf)
2182 int res, status;
2184 printk_deprecated_attribute("hotkey_enable",
2185 "Hotkey reporting is always enabled");
2187 res = hotkey_status_get(&status);
2188 if (res)
2189 return res;
2191 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2194 static ssize_t hotkey_enable_store(struct device *dev,
2195 struct device_attribute *attr,
2196 const char *buf, size_t count)
2198 unsigned long t;
2200 printk_deprecated_attribute("hotkey_enable",
2201 "Hotkeys can be disabled through hotkey_mask");
2203 if (parse_strtoul(buf, 1, &t))
2204 return -EINVAL;
2206 if (t == 0)
2207 return -EPERM;
2209 return count;
2212 static struct device_attribute dev_attr_hotkey_enable =
2213 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2214 hotkey_enable_show, hotkey_enable_store);
2216 /* sysfs hotkey mask --------------------------------------------------- */
2217 static ssize_t hotkey_mask_show(struct device *dev,
2218 struct device_attribute *attr,
2219 char *buf)
2221 int res;
2223 if (mutex_lock_killable(&hotkey_mutex))
2224 return -ERESTARTSYS;
2225 res = hotkey_mask_get();
2226 mutex_unlock(&hotkey_mutex);
2228 return (res)?
2229 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2232 static ssize_t hotkey_mask_store(struct device *dev,
2233 struct device_attribute *attr,
2234 const char *buf, size_t count)
2236 unsigned long t;
2237 int res;
2239 if (parse_strtoul(buf, 0xffffffffUL, &t))
2240 return -EINVAL;
2242 if (mutex_lock_killable(&hotkey_mutex))
2243 return -ERESTARTSYS;
2245 res = hotkey_mask_set(t);
2247 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2248 hotkey_poll_setup(true);
2249 #endif
2251 mutex_unlock(&hotkey_mutex);
2253 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2255 return (res) ? res : count;
2258 static struct device_attribute dev_attr_hotkey_mask =
2259 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2260 hotkey_mask_show, hotkey_mask_store);
2262 /* sysfs hotkey bios_enabled ------------------------------------------- */
2263 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2264 struct device_attribute *attr,
2265 char *buf)
2267 return sprintf(buf, "0\n");
2270 static struct device_attribute dev_attr_hotkey_bios_enabled =
2271 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2273 /* sysfs hotkey bios_mask ---------------------------------------------- */
2274 static ssize_t hotkey_bios_mask_show(struct device *dev,
2275 struct device_attribute *attr,
2276 char *buf)
2278 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2281 static struct device_attribute dev_attr_hotkey_bios_mask =
2282 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2284 /* sysfs hotkey all_mask ----------------------------------------------- */
2285 static ssize_t hotkey_all_mask_show(struct device *dev,
2286 struct device_attribute *attr,
2287 char *buf)
2289 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2290 hotkey_all_mask | hotkey_source_mask);
2293 static struct device_attribute dev_attr_hotkey_all_mask =
2294 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2296 /* sysfs hotkey recommended_mask --------------------------------------- */
2297 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2298 struct device_attribute *attr,
2299 char *buf)
2301 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2302 (hotkey_all_mask | hotkey_source_mask)
2303 & ~hotkey_reserved_mask);
2306 static struct device_attribute dev_attr_hotkey_recommended_mask =
2307 __ATTR(hotkey_recommended_mask, S_IRUGO,
2308 hotkey_recommended_mask_show, NULL);
2310 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2312 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2313 static ssize_t hotkey_source_mask_show(struct device *dev,
2314 struct device_attribute *attr,
2315 char *buf)
2317 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2320 static ssize_t hotkey_source_mask_store(struct device *dev,
2321 struct device_attribute *attr,
2322 const char *buf, size_t count)
2324 unsigned long t;
2326 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2327 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2328 return -EINVAL;
2330 if (mutex_lock_killable(&hotkey_mutex))
2331 return -ERESTARTSYS;
2333 HOTKEY_CONFIG_CRITICAL_START
2334 hotkey_source_mask = t;
2335 HOTKEY_CONFIG_CRITICAL_END
2337 hotkey_poll_setup(true);
2338 hotkey_mask_set(hotkey_mask);
2340 mutex_unlock(&hotkey_mutex);
2342 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2344 return count;
2347 static struct device_attribute dev_attr_hotkey_source_mask =
2348 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2349 hotkey_source_mask_show, hotkey_source_mask_store);
2351 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2352 static ssize_t hotkey_poll_freq_show(struct device *dev,
2353 struct device_attribute *attr,
2354 char *buf)
2356 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2359 static ssize_t hotkey_poll_freq_store(struct device *dev,
2360 struct device_attribute *attr,
2361 const char *buf, size_t count)
2363 unsigned long t;
2365 if (parse_strtoul(buf, 25, &t))
2366 return -EINVAL;
2368 if (mutex_lock_killable(&hotkey_mutex))
2369 return -ERESTARTSYS;
2371 hotkey_poll_set_freq(t);
2372 hotkey_poll_setup(true);
2374 mutex_unlock(&hotkey_mutex);
2376 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2378 return count;
2381 static struct device_attribute dev_attr_hotkey_poll_freq =
2382 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2383 hotkey_poll_freq_show, hotkey_poll_freq_store);
2385 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2387 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2388 static ssize_t hotkey_radio_sw_show(struct device *dev,
2389 struct device_attribute *attr,
2390 char *buf)
2392 int res, s;
2393 res = hotkey_get_wlsw(&s);
2394 if (res < 0)
2395 return res;
2397 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2400 static struct device_attribute dev_attr_hotkey_radio_sw =
2401 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2403 static void hotkey_radio_sw_notify_change(void)
2405 if (tp_features.hotkey_wlsw)
2406 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2407 "hotkey_radio_sw");
2410 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2411 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2412 struct device_attribute *attr,
2413 char *buf)
2415 int res, s;
2416 res = hotkey_get_tablet_mode(&s);
2417 if (res < 0)
2418 return res;
2420 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2423 static struct device_attribute dev_attr_hotkey_tablet_mode =
2424 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2426 static void hotkey_tablet_mode_notify_change(void)
2428 if (tp_features.hotkey_tablet)
2429 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2430 "hotkey_tablet_mode");
2433 /* sysfs hotkey report_mode -------------------------------------------- */
2434 static ssize_t hotkey_report_mode_show(struct device *dev,
2435 struct device_attribute *attr,
2436 char *buf)
2438 return snprintf(buf, PAGE_SIZE, "%d\n",
2439 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2442 static struct device_attribute dev_attr_hotkey_report_mode =
2443 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2445 /* sysfs wakeup reason (pollable) -------------------------------------- */
2446 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2447 struct device_attribute *attr,
2448 char *buf)
2450 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2453 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2454 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2456 static void hotkey_wakeup_reason_notify_change(void)
2458 if (tp_features.hotkey_mask)
2459 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2460 "wakeup_reason");
2463 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2464 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2465 struct device_attribute *attr,
2466 char *buf)
2468 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2471 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2472 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2473 hotkey_wakeup_hotunplug_complete_show, NULL);
2475 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2477 if (tp_features.hotkey_mask)
2478 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2479 "wakeup_hotunplug_complete");
2482 /* --------------------------------------------------------------------- */
2484 static struct attribute *hotkey_attributes[] __initdata = {
2485 &dev_attr_hotkey_enable.attr,
2486 &dev_attr_hotkey_bios_enabled.attr,
2487 &dev_attr_hotkey_report_mode.attr,
2488 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2489 &dev_attr_hotkey_mask.attr,
2490 &dev_attr_hotkey_all_mask.attr,
2491 &dev_attr_hotkey_recommended_mask.attr,
2492 &dev_attr_hotkey_source_mask.attr,
2493 &dev_attr_hotkey_poll_freq.attr,
2494 #endif
2497 static struct attribute *hotkey_mask_attributes[] __initdata = {
2498 &dev_attr_hotkey_bios_mask.attr,
2499 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2500 &dev_attr_hotkey_mask.attr,
2501 &dev_attr_hotkey_all_mask.attr,
2502 &dev_attr_hotkey_recommended_mask.attr,
2503 #endif
2504 &dev_attr_hotkey_wakeup_reason.attr,
2505 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2508 static void bluetooth_update_rfk(void);
2509 static void wan_update_rfk(void);
2510 static void uwb_update_rfk(void);
2511 static void tpacpi_send_radiosw_update(void)
2513 int wlsw;
2515 /* Sync these BEFORE sending any rfkill events */
2516 if (tp_features.bluetooth)
2517 bluetooth_update_rfk();
2518 if (tp_features.wan)
2519 wan_update_rfk();
2520 if (tp_features.uwb)
2521 uwb_update_rfk();
2523 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2524 mutex_lock(&tpacpi_inputdev_send_mutex);
2526 input_report_switch(tpacpi_inputdev,
2527 SW_RFKILL_ALL, !!wlsw);
2528 input_sync(tpacpi_inputdev);
2530 mutex_unlock(&tpacpi_inputdev_send_mutex);
2532 hotkey_radio_sw_notify_change();
2535 static void hotkey_exit(void)
2537 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2538 mutex_lock(&hotkey_mutex);
2539 hotkey_poll_stop_sync();
2540 mutex_unlock(&hotkey_mutex);
2541 #endif
2543 if (hotkey_dev_attributes)
2544 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2546 kfree(hotkey_keycode_map);
2548 if (tp_features.hotkey) {
2549 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2550 "restoring original hot key mask\n");
2551 /* no short-circuit boolean operator below! */
2552 if ((hotkey_mask_set(hotkey_orig_mask) |
2553 hotkey_status_set(false)) != 0)
2554 printk(TPACPI_ERR
2555 "failed to restore hot key mask "
2556 "to BIOS defaults\n");
2560 static int __init hotkey_init(struct ibm_init_struct *iibm)
2562 /* Requirements for changing the default keymaps:
2564 * 1. Many of the keys are mapped to KEY_RESERVED for very
2565 * good reasons. Do not change them unless you have deep
2566 * knowledge on the IBM and Lenovo ThinkPad firmware for
2567 * the various ThinkPad models. The driver behaves
2568 * differently for KEY_RESERVED: such keys have their
2569 * hot key mask *unset* in mask_recommended, and also
2570 * in the initial hot key mask programmed into the
2571 * firmware at driver load time, which means the firm-
2572 * ware may react very differently if you change them to
2573 * something else;
2575 * 2. You must be subscribed to the linux-thinkpad and
2576 * ibm-acpi-devel mailing lists, and you should read the
2577 * list archives since 2007 if you want to change the
2578 * keymaps. This requirement exists so that you will
2579 * know the past history of problems with the thinkpad-
2580 * acpi driver keymaps, and also that you will be
2581 * listening to any bug reports;
2583 * 3. Do not send thinkpad-acpi specific patches directly to
2584 * for merging, *ever*. Send them to the linux-acpi
2585 * mailinglist for comments. Merging is to be done only
2586 * through acpi-test and the ACPI maintainer.
2588 * If the above is too much to ask, don't change the keymap.
2589 * Ask the thinkpad-acpi maintainer to do it, instead.
2591 static u16 ibm_keycode_map[] __initdata = {
2592 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2593 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2594 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2595 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2597 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2598 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2599 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2600 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2602 /* brightness: firmware always reacts to them, unless
2603 * X.org did some tricks in the radeon BIOS scratch
2604 * registers of *some* models */
2605 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2606 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2608 /* Thinklight: firmware always react to it */
2609 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2611 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2612 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2614 /* Volume: firmware always react to it and reprograms
2615 * the built-in *extra* mixer. Never map it to control
2616 * another mixer by default. */
2617 KEY_RESERVED, /* 0x14: VOLUME UP */
2618 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2619 KEY_RESERVED, /* 0x16: MUTE */
2621 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2623 /* (assignments unknown, please report if found) */
2624 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2625 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2627 static u16 lenovo_keycode_map[] __initdata = {
2628 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2629 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2630 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2631 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2633 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2634 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2635 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2636 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2638 /* These either have to go through ACPI video, or
2639 * act like in the IBM ThinkPads, so don't ever
2640 * enable them by default */
2641 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2642 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2644 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2646 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2647 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2649 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2650 * react to it and reprograms the built-in *extra* mixer.
2651 * Never map it to control another mixer by default.
2653 * T60?, T61, R60?, R61: firmware and EC tries to send
2654 * these over the regular keyboard, so these are no-ops,
2655 * but there are still weird bugs re. MUTE, so do not
2656 * change unless you get test reports from all Lenovo
2657 * models. May cause the BIOS to interfere with the
2658 * HDA mixer.
2660 KEY_RESERVED, /* 0x14: VOLUME UP */
2661 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2662 KEY_RESERVED, /* 0x16: MUTE */
2664 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2666 /* (assignments unknown, please report if found) */
2667 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2668 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2671 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2672 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2673 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2675 int res, i;
2676 int status;
2677 int hkeyv;
2679 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2680 "initializing hotkey subdriver\n");
2682 BUG_ON(!tpacpi_inputdev);
2683 BUG_ON(tpacpi_inputdev->open != NULL ||
2684 tpacpi_inputdev->close != NULL);
2686 TPACPI_ACPIHANDLE_INIT(hkey);
2687 mutex_init(&hotkey_mutex);
2689 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2690 mutex_init(&hotkey_thread_mutex);
2691 mutex_init(&hotkey_thread_data_mutex);
2692 #endif
2694 /* hotkey not supported on 570 */
2695 tp_features.hotkey = hkey_handle != NULL;
2697 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2698 "hotkeys are %s\n",
2699 str_supported(tp_features.hotkey));
2701 if (!tp_features.hotkey)
2702 return 1;
2704 tpacpi_disable_brightness_delay();
2706 hotkey_dev_attributes = create_attr_set(13, NULL);
2707 if (!hotkey_dev_attributes)
2708 return -ENOMEM;
2709 res = add_many_to_attr_set(hotkey_dev_attributes,
2710 hotkey_attributes,
2711 ARRAY_SIZE(hotkey_attributes));
2712 if (res)
2713 goto err_exit;
2715 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2716 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2717 for HKEY interface version 0x100 */
2718 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2719 if ((hkeyv >> 8) != 1) {
2720 printk(TPACPI_ERR "unknown version of the "
2721 "HKEY interface: 0x%x\n", hkeyv);
2722 printk(TPACPI_ERR "please report this to %s\n",
2723 TPACPI_MAIL);
2724 } else {
2726 * MHKV 0x100 in A31, R40, R40e,
2727 * T4x, X31, and later
2729 tp_features.hotkey_mask = 1;
2730 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2731 "firmware HKEY interface version: 0x%x\n",
2732 hkeyv);
2736 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2737 "hotkey masks are %s\n",
2738 str_supported(tp_features.hotkey_mask));
2740 if (tp_features.hotkey_mask) {
2741 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2742 "MHKA", "qd")) {
2743 printk(TPACPI_ERR
2744 "missing MHKA handler, "
2745 "please report this to %s\n",
2746 TPACPI_MAIL);
2747 /* FN+F12, FN+F4, FN+F3 */
2748 hotkey_all_mask = 0x080cU;
2752 /* hotkey_source_mask *must* be zero for
2753 * the first hotkey_mask_get */
2754 if (tp_features.hotkey_mask) {
2755 res = hotkey_mask_get();
2756 if (res)
2757 goto err_exit;
2759 hotkey_orig_mask = hotkey_mask;
2760 res = add_many_to_attr_set(
2761 hotkey_dev_attributes,
2762 hotkey_mask_attributes,
2763 ARRAY_SIZE(hotkey_mask_attributes));
2764 if (res)
2765 goto err_exit;
2768 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2769 if (tp_features.hotkey_mask) {
2770 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2771 & ~hotkey_all_mask;
2772 } else {
2773 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2776 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2777 "hotkey source mask 0x%08x, polling freq %u\n",
2778 hotkey_source_mask, hotkey_poll_freq);
2779 #endif
2781 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2782 if (dbg_wlswemul) {
2783 tp_features.hotkey_wlsw = 1;
2784 printk(TPACPI_INFO
2785 "radio switch emulation enabled\n");
2786 } else
2787 #endif
2788 /* Not all thinkpads have a hardware radio switch */
2789 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2790 tp_features.hotkey_wlsw = 1;
2791 printk(TPACPI_INFO
2792 "radio switch found; radios are %s\n",
2793 enabled(status, 0));
2795 if (tp_features.hotkey_wlsw)
2796 res = add_to_attr_set(hotkey_dev_attributes,
2797 &dev_attr_hotkey_radio_sw.attr);
2799 /* For X41t, X60t, X61t Tablets... */
2800 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2801 tp_features.hotkey_tablet = 1;
2802 printk(TPACPI_INFO
2803 "possible tablet mode switch found; "
2804 "ThinkPad in %s mode\n",
2805 (status & TP_HOTKEY_TABLET_MASK)?
2806 "tablet" : "laptop");
2807 res = add_to_attr_set(hotkey_dev_attributes,
2808 &dev_attr_hotkey_tablet_mode.attr);
2811 if (!res)
2812 res = register_attr_set_with_sysfs(
2813 hotkey_dev_attributes,
2814 &tpacpi_pdev->dev.kobj);
2815 if (res)
2816 goto err_exit;
2818 /* Set up key map */
2820 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2821 GFP_KERNEL);
2822 if (!hotkey_keycode_map) {
2823 printk(TPACPI_ERR
2824 "failed to allocate memory for key map\n");
2825 res = -ENOMEM;
2826 goto err_exit;
2829 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2830 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2831 "using Lenovo default hot key map\n");
2832 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2833 TPACPI_HOTKEY_MAP_SIZE);
2834 } else {
2835 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2836 "using IBM default hot key map\n");
2837 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2838 TPACPI_HOTKEY_MAP_SIZE);
2841 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2842 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2843 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2844 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2845 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2846 tpacpi_inputdev->keycode = hotkey_keycode_map;
2847 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2848 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2849 set_bit(hotkey_keycode_map[i],
2850 tpacpi_inputdev->keybit);
2851 } else {
2852 if (i < sizeof(hotkey_reserved_mask)*8)
2853 hotkey_reserved_mask |= 1 << i;
2857 if (tp_features.hotkey_wlsw) {
2858 set_bit(EV_SW, tpacpi_inputdev->evbit);
2859 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2861 if (tp_features.hotkey_tablet) {
2862 set_bit(EV_SW, tpacpi_inputdev->evbit);
2863 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2866 /* Do not issue duplicate brightness change events to
2867 * userspace */
2868 if (!tp_features.bright_acpimode)
2869 /* update bright_acpimode... */
2870 tpacpi_check_std_acpi_brightness_support();
2872 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2873 printk(TPACPI_INFO
2874 "This ThinkPad has standard ACPI backlight "
2875 "brightness control, supported by the ACPI "
2876 "video driver\n");
2877 printk(TPACPI_NOTICE
2878 "Disabling thinkpad-acpi brightness events "
2879 "by default...\n");
2881 /* The hotkey_reserved_mask change below is not
2882 * necessary while the keys are at KEY_RESERVED in the
2883 * default map, but better safe than sorry, leave it
2884 * here as a marker of what we have to do, especially
2885 * when we finally become able to set this at runtime
2886 * on response to X.org requests */
2887 hotkey_reserved_mask |=
2888 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2889 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2892 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2893 "enabling firmware HKEY event interface...\n");
2894 res = hotkey_status_set(true);
2895 if (res) {
2896 hotkey_exit();
2897 return res;
2899 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2900 & ~hotkey_reserved_mask)
2901 | hotkey_orig_mask);
2902 if (res < 0 && res != -ENXIO) {
2903 hotkey_exit();
2904 return res;
2907 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2908 "legacy ibm/hotkey event reporting over procfs %s\n",
2909 (hotkey_report_mode < 2) ?
2910 "enabled" : "disabled");
2912 tpacpi_inputdev->open = &hotkey_inputdev_open;
2913 tpacpi_inputdev->close = &hotkey_inputdev_close;
2915 hotkey_poll_setup_safe(true);
2916 tpacpi_send_radiosw_update();
2917 tpacpi_input_send_tabletsw();
2919 return 0;
2921 err_exit:
2922 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2923 hotkey_dev_attributes = NULL;
2925 return (res < 0)? res : 1;
2928 static bool hotkey_notify_hotkey(const u32 hkey,
2929 bool *send_acpi_ev,
2930 bool *ignore_acpi_ev)
2932 /* 0x1000-0x1FFF: key presses */
2933 unsigned int scancode = hkey & 0xfff;
2934 *send_acpi_ev = true;
2935 *ignore_acpi_ev = false;
2937 if (scancode > 0 && scancode < 0x21) {
2938 scancode--;
2939 if (!(hotkey_source_mask & (1 << scancode))) {
2940 tpacpi_input_send_key(scancode);
2941 *send_acpi_ev = false;
2942 } else {
2943 *ignore_acpi_ev = true;
2945 return true;
2947 return false;
2950 static bool hotkey_notify_wakeup(const u32 hkey,
2951 bool *send_acpi_ev,
2952 bool *ignore_acpi_ev)
2954 /* 0x2000-0x2FFF: Wakeup reason */
2955 *send_acpi_ev = true;
2956 *ignore_acpi_ev = false;
2958 switch (hkey) {
2959 case 0x2304: /* suspend, undock */
2960 case 0x2404: /* hibernation, undock */
2961 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2962 *ignore_acpi_ev = true;
2963 break;
2965 case 0x2305: /* suspend, bay eject */
2966 case 0x2405: /* hibernation, bay eject */
2967 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2968 *ignore_acpi_ev = true;
2969 break;
2971 case 0x2313: /* Battery on critical low level (S3) */
2972 case 0x2413: /* Battery on critical low level (S4) */
2973 printk(TPACPI_ALERT
2974 "EMERGENCY WAKEUP: battery almost empty\n");
2975 /* how to auto-heal: */
2976 /* 2313: woke up from S3, go to S4/S5 */
2977 /* 2413: woke up from S4, go to S5 */
2978 break;
2980 default:
2981 return false;
2984 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2985 printk(TPACPI_INFO
2986 "woke up due to a hot-unplug "
2987 "request...\n");
2988 hotkey_wakeup_reason_notify_change();
2990 return true;
2993 static bool hotkey_notify_usrevent(const u32 hkey,
2994 bool *send_acpi_ev,
2995 bool *ignore_acpi_ev)
2997 /* 0x5000-0x5FFF: human interface helpers */
2998 *send_acpi_ev = true;
2999 *ignore_acpi_ev = false;
3001 switch (hkey) {
3002 case 0x5010: /* Lenovo new BIOS: brightness changed */
3003 case 0x500b: /* X61t: tablet pen inserted into bay */
3004 case 0x500c: /* X61t: tablet pen removed from bay */
3005 return true;
3007 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3008 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3009 tpacpi_input_send_tabletsw();
3010 hotkey_tablet_mode_notify_change();
3011 *send_acpi_ev = false;
3012 return true;
3014 case 0x5001:
3015 case 0x5002:
3016 /* LID switch events. Do not propagate */
3017 *ignore_acpi_ev = true;
3018 return true;
3020 default:
3021 return false;
3025 static bool hotkey_notify_thermal(const u32 hkey,
3026 bool *send_acpi_ev,
3027 bool *ignore_acpi_ev)
3029 /* 0x6000-0x6FFF: thermal alarms */
3030 *send_acpi_ev = true;
3031 *ignore_acpi_ev = false;
3033 switch (hkey) {
3034 case 0x6011:
3035 printk(TPACPI_CRIT
3036 "THERMAL ALARM: battery is too hot!\n");
3037 /* recommended action: warn user through gui */
3038 return true;
3039 case 0x6012:
3040 printk(TPACPI_ALERT
3041 "THERMAL EMERGENCY: battery is extremely hot!\n");
3042 /* recommended action: immediate sleep/hibernate */
3043 return true;
3044 case 0x6021:
3045 printk(TPACPI_CRIT
3046 "THERMAL ALARM: "
3047 "a sensor reports something is too hot!\n");
3048 /* recommended action: warn user through gui, that */
3049 /* some internal component is too hot */
3050 return true;
3051 case 0x6022:
3052 printk(TPACPI_ALERT
3053 "THERMAL EMERGENCY: "
3054 "a sensor reports something is extremely hot!\n");
3055 /* recommended action: immediate sleep/hibernate */
3056 return true;
3057 case 0x6030:
3058 printk(TPACPI_INFO
3059 "EC reports that Thermal Table has changed\n");
3060 /* recommended action: do nothing, we don't have
3061 * Lenovo ATM information */
3062 return true;
3063 default:
3064 printk(TPACPI_ALERT
3065 "THERMAL ALERT: unknown thermal alarm received\n");
3066 return false;
3070 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3072 u32 hkey;
3073 bool send_acpi_ev;
3074 bool ignore_acpi_ev;
3075 bool known_ev;
3077 if (event != 0x80) {
3078 printk(TPACPI_ERR
3079 "unknown HKEY notification event %d\n", event);
3080 /* forward it to userspace, maybe it knows how to handle it */
3081 acpi_bus_generate_netlink_event(
3082 ibm->acpi->device->pnp.device_class,
3083 dev_name(&ibm->acpi->device->dev),
3084 event, 0);
3085 return;
3088 while (1) {
3089 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3090 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3091 return;
3094 if (hkey == 0) {
3095 /* queue empty */
3096 return;
3099 send_acpi_ev = true;
3100 ignore_acpi_ev = false;
3102 switch (hkey >> 12) {
3103 case 1:
3104 /* 0x1000-0x1FFF: key presses */
3105 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3106 &ignore_acpi_ev);
3107 break;
3108 case 2:
3109 /* 0x2000-0x2FFF: Wakeup reason */
3110 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3111 &ignore_acpi_ev);
3112 break;
3113 case 3:
3114 /* 0x3000-0x3FFF: bay-related wakeups */
3115 if (hkey == 0x3003) {
3116 hotkey_autosleep_ack = 1;
3117 printk(TPACPI_INFO
3118 "bay ejected\n");
3119 hotkey_wakeup_hotunplug_complete_notify_change();
3120 known_ev = true;
3121 } else {
3122 known_ev = false;
3124 break;
3125 case 4:
3126 /* 0x4000-0x4FFF: dock-related wakeups */
3127 if (hkey == 0x4003) {
3128 hotkey_autosleep_ack = 1;
3129 printk(TPACPI_INFO
3130 "undocked\n");
3131 hotkey_wakeup_hotunplug_complete_notify_change();
3132 known_ev = true;
3133 } else {
3134 known_ev = false;
3136 break;
3137 case 5:
3138 /* 0x5000-0x5FFF: human interface helpers */
3139 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3140 &ignore_acpi_ev);
3141 break;
3142 case 6:
3143 /* 0x6000-0x6FFF: thermal alarms */
3144 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3145 &ignore_acpi_ev);
3146 break;
3147 case 7:
3148 /* 0x7000-0x7FFF: misc */
3149 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3150 tpacpi_send_radiosw_update();
3151 send_acpi_ev = 0;
3152 known_ev = true;
3153 break;
3155 /* fallthrough to default */
3156 default:
3157 known_ev = false;
3159 if (!known_ev) {
3160 printk(TPACPI_NOTICE
3161 "unhandled HKEY event 0x%04x\n", hkey);
3162 printk(TPACPI_NOTICE
3163 "please report the conditions when this "
3164 "event happened to %s\n", TPACPI_MAIL);
3167 /* Legacy events */
3168 if (!ignore_acpi_ev &&
3169 (send_acpi_ev || hotkey_report_mode < 2)) {
3170 acpi_bus_generate_proc_event(ibm->acpi->device,
3171 event, hkey);
3174 /* netlink events */
3175 if (!ignore_acpi_ev && send_acpi_ev) {
3176 acpi_bus_generate_netlink_event(
3177 ibm->acpi->device->pnp.device_class,
3178 dev_name(&ibm->acpi->device->dev),
3179 event, hkey);
3184 static void hotkey_suspend(pm_message_t state)
3186 /* Do these on suspend, we get the events on early resume! */
3187 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3188 hotkey_autosleep_ack = 0;
3191 static void hotkey_resume(void)
3193 tpacpi_disable_brightness_delay();
3195 if (hotkey_mask_get())
3196 printk(TPACPI_ERR
3197 "error while trying to read hot key mask "
3198 "from firmware\n");
3199 tpacpi_send_radiosw_update();
3200 hotkey_tablet_mode_notify_change();
3201 hotkey_wakeup_reason_notify_change();
3202 hotkey_wakeup_hotunplug_complete_notify_change();
3203 hotkey_poll_setup_safe(false);
3206 /* procfs -------------------------------------------------------------- */
3207 static int hotkey_read(char *p)
3209 int res, status;
3210 int len = 0;
3212 if (!tp_features.hotkey) {
3213 len += sprintf(p + len, "status:\t\tnot supported\n");
3214 return len;
3217 if (mutex_lock_killable(&hotkey_mutex))
3218 return -ERESTARTSYS;
3219 res = hotkey_status_get(&status);
3220 if (!res)
3221 res = hotkey_mask_get();
3222 mutex_unlock(&hotkey_mutex);
3223 if (res)
3224 return res;
3226 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3227 if (tp_features.hotkey_mask) {
3228 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3229 len += sprintf(p + len,
3230 "commands:\tenable, disable, reset, <mask>\n");
3231 } else {
3232 len += sprintf(p + len, "mask:\t\tnot supported\n");
3233 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3236 return len;
3239 static void hotkey_enabledisable_warn(bool enable)
3241 tpacpi_log_usertask("procfs hotkey enable/disable");
3242 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3243 TPACPI_WARN
3244 "hotkey enable/disable functionality has been "
3245 "removed from the driver. Hotkeys are always "
3246 "enabled\n"))
3247 printk(TPACPI_ERR
3248 "Please remove the hotkey=enable module "
3249 "parameter, it is deprecated. Hotkeys are always "
3250 "enabled\n");
3253 static int hotkey_write(char *buf)
3255 int res;
3256 u32 mask;
3257 char *cmd;
3259 if (!tp_features.hotkey)
3260 return -ENODEV;
3262 if (mutex_lock_killable(&hotkey_mutex))
3263 return -ERESTARTSYS;
3265 mask = hotkey_mask;
3267 res = 0;
3268 while ((cmd = next_cmd(&buf))) {
3269 if (strlencmp(cmd, "enable") == 0) {
3270 hotkey_enabledisable_warn(1);
3271 } else if (strlencmp(cmd, "disable") == 0) {
3272 hotkey_enabledisable_warn(0);
3273 res = -EPERM;
3274 } else if (strlencmp(cmd, "reset") == 0) {
3275 mask = hotkey_orig_mask;
3276 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3277 /* mask set */
3278 } else if (sscanf(cmd, "%x", &mask) == 1) {
3279 /* mask set */
3280 } else {
3281 res = -EINVAL;
3282 goto errexit;
3286 if (!res)
3287 tpacpi_disclose_usertask("procfs hotkey",
3288 "set mask to 0x%08x\n", mask);
3290 if (!res && mask != hotkey_mask)
3291 res = hotkey_mask_set(mask);
3293 errexit:
3294 mutex_unlock(&hotkey_mutex);
3295 return res;
3298 static const struct acpi_device_id ibm_htk_device_ids[] = {
3299 {TPACPI_ACPI_HKEY_HID, 0},
3300 {"", 0},
3303 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3304 .hid = ibm_htk_device_ids,
3305 .notify = hotkey_notify,
3306 .handle = &hkey_handle,
3307 .type = ACPI_DEVICE_NOTIFY,
3310 static struct ibm_struct hotkey_driver_data = {
3311 .name = "hotkey",
3312 .read = hotkey_read,
3313 .write = hotkey_write,
3314 .exit = hotkey_exit,
3315 .resume = hotkey_resume,
3316 .suspend = hotkey_suspend,
3317 .acpi = &ibm_hotkey_acpidriver,
3320 /*************************************************************************
3321 * Bluetooth subdriver
3324 enum {
3325 /* ACPI GBDC/SBDC bits */
3326 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3327 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3328 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3329 off / last state */
3332 enum {
3333 /* ACPI \BLTH commands */
3334 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3335 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3336 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3337 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3338 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3341 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3343 static struct rfkill *tpacpi_bluetooth_rfkill;
3345 static void bluetooth_suspend(pm_message_t state)
3347 /* Try to make sure radio will resume powered off */
3348 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3349 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3350 vdbg_printk(TPACPI_DBG_RFKILL,
3351 "bluetooth power down on resume request failed\n");
3354 static int bluetooth_get_radiosw(void)
3356 int status;
3358 if (!tp_features.bluetooth)
3359 return -ENODEV;
3361 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3362 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3363 return RFKILL_STATE_HARD_BLOCKED;
3365 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3366 if (dbg_bluetoothemul)
3367 return (tpacpi_bluetooth_emulstate) ?
3368 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3369 #endif
3371 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3372 return -EIO;
3374 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3375 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3378 static void bluetooth_update_rfk(void)
3380 int status;
3382 if (!tpacpi_bluetooth_rfkill)
3383 return;
3385 status = bluetooth_get_radiosw();
3386 if (status < 0)
3387 return;
3388 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3390 vdbg_printk(TPACPI_DBG_RFKILL,
3391 "forced rfkill state to %d\n",
3392 status);
3395 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3397 int status;
3399 if (!tp_features.bluetooth)
3400 return -ENODEV;
3402 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3403 * reason to risk weird behaviour. */
3404 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3405 && radio_on)
3406 return -EPERM;
3408 vdbg_printk(TPACPI_DBG_RFKILL,
3409 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3411 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3412 if (dbg_bluetoothemul) {
3413 tpacpi_bluetooth_emulstate = !!radio_on;
3414 if (update_rfk)
3415 bluetooth_update_rfk();
3416 return 0;
3418 #endif
3420 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3421 if (radio_on)
3422 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3423 else
3424 status = 0;
3425 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3426 return -EIO;
3428 if (update_rfk)
3429 bluetooth_update_rfk();
3431 return 0;
3434 /* sysfs bluetooth enable ---------------------------------------------- */
3435 static ssize_t bluetooth_enable_show(struct device *dev,
3436 struct device_attribute *attr,
3437 char *buf)
3439 int status;
3441 printk_deprecated_rfkill_attribute("bluetooth_enable");
3443 status = bluetooth_get_radiosw();
3444 if (status < 0)
3445 return status;
3447 return snprintf(buf, PAGE_SIZE, "%d\n",
3448 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3451 static ssize_t bluetooth_enable_store(struct device *dev,
3452 struct device_attribute *attr,
3453 const char *buf, size_t count)
3455 unsigned long t;
3456 int res;
3458 printk_deprecated_rfkill_attribute("bluetooth_enable");
3460 if (parse_strtoul(buf, 1, &t))
3461 return -EINVAL;
3463 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3465 res = bluetooth_set_radiosw(t, 1);
3467 return (res) ? res : count;
3470 static struct device_attribute dev_attr_bluetooth_enable =
3471 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3472 bluetooth_enable_show, bluetooth_enable_store);
3474 /* --------------------------------------------------------------------- */
3476 static struct attribute *bluetooth_attributes[] = {
3477 &dev_attr_bluetooth_enable.attr,
3478 NULL
3481 static const struct attribute_group bluetooth_attr_group = {
3482 .attrs = bluetooth_attributes,
3485 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3487 int bts = bluetooth_get_radiosw();
3489 if (bts < 0)
3490 return bts;
3492 *state = bts;
3493 return 0;
3496 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3498 dbg_printk(TPACPI_DBG_RFKILL,
3499 "request to change radio state to %d\n", state);
3500 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3503 static void bluetooth_shutdown(void)
3505 /* Order firmware to save current state to NVRAM */
3506 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3507 TP_ACPI_BLTH_SAVE_STATE))
3508 printk(TPACPI_NOTICE
3509 "failed to save bluetooth state to NVRAM\n");
3510 else
3511 vdbg_printk(TPACPI_DBG_RFKILL,
3512 "bluestooth state saved to NVRAM\n");
3515 static void bluetooth_exit(void)
3517 bluetooth_shutdown();
3519 if (tpacpi_bluetooth_rfkill)
3520 rfkill_unregister(tpacpi_bluetooth_rfkill);
3522 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3523 &bluetooth_attr_group);
3526 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3528 int res;
3529 int status = 0;
3531 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3532 "initializing bluetooth subdriver\n");
3534 TPACPI_ACPIHANDLE_INIT(hkey);
3536 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3537 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3538 tp_features.bluetooth = hkey_handle &&
3539 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3541 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3542 "bluetooth is %s, status 0x%02x\n",
3543 str_supported(tp_features.bluetooth),
3544 status);
3546 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3547 if (dbg_bluetoothemul) {
3548 tp_features.bluetooth = 1;
3549 printk(TPACPI_INFO
3550 "bluetooth switch emulation enabled\n");
3551 } else
3552 #endif
3553 if (tp_features.bluetooth &&
3554 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3555 /* no bluetooth hardware present in system */
3556 tp_features.bluetooth = 0;
3557 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3558 "bluetooth hardware not installed\n");
3561 if (!tp_features.bluetooth)
3562 return 1;
3564 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3565 &bluetooth_attr_group);
3566 if (res)
3567 return res;
3569 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3570 &tpacpi_bluetooth_rfkill,
3571 RFKILL_TYPE_BLUETOOTH,
3572 TPACPI_RFK_BLUETOOTH_SW_NAME,
3573 true,
3574 tpacpi_bluetooth_rfk_set,
3575 tpacpi_bluetooth_rfk_get);
3576 if (res) {
3577 bluetooth_exit();
3578 return res;
3581 return 0;
3584 /* procfs -------------------------------------------------------------- */
3585 static int bluetooth_read(char *p)
3587 int len = 0;
3588 int status = bluetooth_get_radiosw();
3590 if (!tp_features.bluetooth)
3591 len += sprintf(p + len, "status:\t\tnot supported\n");
3592 else {
3593 len += sprintf(p + len, "status:\t\t%s\n",
3594 (status == RFKILL_STATE_UNBLOCKED) ?
3595 "enabled" : "disabled");
3596 len += sprintf(p + len, "commands:\tenable, disable\n");
3599 return len;
3602 static int bluetooth_write(char *buf)
3604 char *cmd;
3605 int state = -1;
3607 if (!tp_features.bluetooth)
3608 return -ENODEV;
3610 while ((cmd = next_cmd(&buf))) {
3611 if (strlencmp(cmd, "enable") == 0) {
3612 state = 1;
3613 } else if (strlencmp(cmd, "disable") == 0) {
3614 state = 0;
3615 } else
3616 return -EINVAL;
3619 if (state != -1) {
3620 tpacpi_disclose_usertask("procfs bluetooth",
3621 "attempt to %s\n",
3622 state ? "enable" : "disable");
3623 bluetooth_set_radiosw(state, 1);
3626 return 0;
3629 static struct ibm_struct bluetooth_driver_data = {
3630 .name = "bluetooth",
3631 .read = bluetooth_read,
3632 .write = bluetooth_write,
3633 .exit = bluetooth_exit,
3634 .suspend = bluetooth_suspend,
3635 .shutdown = bluetooth_shutdown,
3638 /*************************************************************************
3639 * Wan subdriver
3642 enum {
3643 /* ACPI GWAN/SWAN bits */
3644 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3645 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3646 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3647 off / last state */
3650 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3652 static struct rfkill *tpacpi_wan_rfkill;
3654 static void wan_suspend(pm_message_t state)
3656 /* Try to make sure radio will resume powered off */
3657 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3658 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3659 vdbg_printk(TPACPI_DBG_RFKILL,
3660 "WWAN power down on resume request failed\n");
3663 static int wan_get_radiosw(void)
3665 int status;
3667 if (!tp_features.wan)
3668 return -ENODEV;
3670 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3671 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3672 return RFKILL_STATE_HARD_BLOCKED;
3674 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3675 if (dbg_wwanemul)
3676 return (tpacpi_wwan_emulstate) ?
3677 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3678 #endif
3680 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3681 return -EIO;
3683 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3684 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3687 static void wan_update_rfk(void)
3689 int status;
3691 if (!tpacpi_wan_rfkill)
3692 return;
3694 status = wan_get_radiosw();
3695 if (status < 0)
3696 return;
3697 rfkill_force_state(tpacpi_wan_rfkill, status);
3699 vdbg_printk(TPACPI_DBG_RFKILL,
3700 "forced rfkill state to %d\n",
3701 status);
3704 static int wan_set_radiosw(int radio_on, int update_rfk)
3706 int status;
3708 if (!tp_features.wan)
3709 return -ENODEV;
3711 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3712 * reason to risk weird behaviour. */
3713 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3714 && radio_on)
3715 return -EPERM;
3717 vdbg_printk(TPACPI_DBG_RFKILL,
3718 "will %s WWAN\n", radio_on ? "enable" : "disable");
3720 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3721 if (dbg_wwanemul) {
3722 tpacpi_wwan_emulstate = !!radio_on;
3723 if (update_rfk)
3724 wan_update_rfk();
3725 return 0;
3727 #endif
3729 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3730 if (radio_on)
3731 status = TP_ACPI_WANCARD_RADIOSSW;
3732 else
3733 status = 0;
3734 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3735 return -EIO;
3737 if (update_rfk)
3738 wan_update_rfk();
3740 return 0;
3743 /* sysfs wan enable ---------------------------------------------------- */
3744 static ssize_t wan_enable_show(struct device *dev,
3745 struct device_attribute *attr,
3746 char *buf)
3748 int status;
3750 printk_deprecated_rfkill_attribute("wwan_enable");
3752 status = wan_get_radiosw();
3753 if (status < 0)
3754 return status;
3756 return snprintf(buf, PAGE_SIZE, "%d\n",
3757 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3760 static ssize_t wan_enable_store(struct device *dev,
3761 struct device_attribute *attr,
3762 const char *buf, size_t count)
3764 unsigned long t;
3765 int res;
3767 printk_deprecated_rfkill_attribute("wwan_enable");
3769 if (parse_strtoul(buf, 1, &t))
3770 return -EINVAL;
3772 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3774 res = wan_set_radiosw(t, 1);
3776 return (res) ? res : count;
3779 static struct device_attribute dev_attr_wan_enable =
3780 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3781 wan_enable_show, wan_enable_store);
3783 /* --------------------------------------------------------------------- */
3785 static struct attribute *wan_attributes[] = {
3786 &dev_attr_wan_enable.attr,
3787 NULL
3790 static const struct attribute_group wan_attr_group = {
3791 .attrs = wan_attributes,
3794 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3796 int wans = wan_get_radiosw();
3798 if (wans < 0)
3799 return wans;
3801 *state = wans;
3802 return 0;
3805 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3807 dbg_printk(TPACPI_DBG_RFKILL,
3808 "request to change radio state to %d\n", state);
3809 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3812 static void wan_shutdown(void)
3814 /* Order firmware to save current state to NVRAM */
3815 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3816 TP_ACPI_WGSV_SAVE_STATE))
3817 printk(TPACPI_NOTICE
3818 "failed to save WWAN state to NVRAM\n");
3819 else
3820 vdbg_printk(TPACPI_DBG_RFKILL,
3821 "WWAN state saved to NVRAM\n");
3824 static void wan_exit(void)
3826 wan_shutdown();
3828 if (tpacpi_wan_rfkill)
3829 rfkill_unregister(tpacpi_wan_rfkill);
3831 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3832 &wan_attr_group);
3835 static int __init wan_init(struct ibm_init_struct *iibm)
3837 int res;
3838 int status = 0;
3840 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3841 "initializing wan subdriver\n");
3843 TPACPI_ACPIHANDLE_INIT(hkey);
3845 tp_features.wan = hkey_handle &&
3846 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3848 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3849 "wan is %s, status 0x%02x\n",
3850 str_supported(tp_features.wan),
3851 status);
3853 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3854 if (dbg_wwanemul) {
3855 tp_features.wan = 1;
3856 printk(TPACPI_INFO
3857 "wwan switch emulation enabled\n");
3858 } else
3859 #endif
3860 if (tp_features.wan &&
3861 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3862 /* no wan hardware present in system */
3863 tp_features.wan = 0;
3864 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3865 "wan hardware not installed\n");
3868 if (!tp_features.wan)
3869 return 1;
3871 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3872 &wan_attr_group);
3873 if (res)
3874 return res;
3876 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3877 &tpacpi_wan_rfkill,
3878 RFKILL_TYPE_WWAN,
3879 TPACPI_RFK_WWAN_SW_NAME,
3880 true,
3881 tpacpi_wan_rfk_set,
3882 tpacpi_wan_rfk_get);
3883 if (res) {
3884 wan_exit();
3885 return res;
3888 return 0;
3891 /* procfs -------------------------------------------------------------- */
3892 static int wan_read(char *p)
3894 int len = 0;
3895 int status = wan_get_radiosw();
3897 tpacpi_disclose_usertask("procfs wan", "read");
3899 if (!tp_features.wan)
3900 len += sprintf(p + len, "status:\t\tnot supported\n");
3901 else {
3902 len += sprintf(p + len, "status:\t\t%s\n",
3903 (status == RFKILL_STATE_UNBLOCKED) ?
3904 "enabled" : "disabled");
3905 len += sprintf(p + len, "commands:\tenable, disable\n");
3908 return len;
3911 static int wan_write(char *buf)
3913 char *cmd;
3914 int state = -1;
3916 if (!tp_features.wan)
3917 return -ENODEV;
3919 while ((cmd = next_cmd(&buf))) {
3920 if (strlencmp(cmd, "enable") == 0) {
3921 state = 1;
3922 } else if (strlencmp(cmd, "disable") == 0) {
3923 state = 0;
3924 } else
3925 return -EINVAL;
3928 if (state != -1) {
3929 tpacpi_disclose_usertask("procfs wan",
3930 "attempt to %s\n",
3931 state ? "enable" : "disable");
3932 wan_set_radiosw(state, 1);
3935 return 0;
3938 static struct ibm_struct wan_driver_data = {
3939 .name = "wan",
3940 .read = wan_read,
3941 .write = wan_write,
3942 .exit = wan_exit,
3943 .suspend = wan_suspend,
3944 .shutdown = wan_shutdown,
3947 /*************************************************************************
3948 * UWB subdriver
3951 enum {
3952 /* ACPI GUWB/SUWB bits */
3953 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3954 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3957 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3959 static struct rfkill *tpacpi_uwb_rfkill;
3961 static int uwb_get_radiosw(void)
3963 int status;
3965 if (!tp_features.uwb)
3966 return -ENODEV;
3968 /* WLSW overrides UWB in firmware/hardware, reflect that */
3969 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3970 return RFKILL_STATE_HARD_BLOCKED;
3972 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3973 if (dbg_uwbemul)
3974 return (tpacpi_uwb_emulstate) ?
3975 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3976 #endif
3978 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3979 return -EIO;
3981 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3982 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3985 static void uwb_update_rfk(void)
3987 int status;
3989 if (!tpacpi_uwb_rfkill)
3990 return;
3992 status = uwb_get_radiosw();
3993 if (status < 0)
3994 return;
3995 rfkill_force_state(tpacpi_uwb_rfkill, status);
3997 vdbg_printk(TPACPI_DBG_RFKILL,
3998 "forced rfkill state to %d\n",
3999 status);
4002 static int uwb_set_radiosw(int radio_on, int update_rfk)
4004 int status;
4006 if (!tp_features.uwb)
4007 return -ENODEV;
4009 /* WLSW overrides UWB in firmware/hardware, but there is no
4010 * reason to risk weird behaviour. */
4011 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4012 && radio_on)
4013 return -EPERM;
4015 vdbg_printk(TPACPI_DBG_RFKILL,
4016 "will %s UWB\n", radio_on ? "enable" : "disable");
4018 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4019 if (dbg_uwbemul) {
4020 tpacpi_uwb_emulstate = !!radio_on;
4021 if (update_rfk)
4022 uwb_update_rfk();
4023 return 0;
4025 #endif
4027 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4028 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4029 return -EIO;
4031 if (update_rfk)
4032 uwb_update_rfk();
4034 return 0;
4037 /* --------------------------------------------------------------------- */
4039 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4041 int uwbs = uwb_get_radiosw();
4043 if (uwbs < 0)
4044 return uwbs;
4046 *state = uwbs;
4047 return 0;
4050 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4052 dbg_printk(TPACPI_DBG_RFKILL,
4053 "request to change radio state to %d\n", state);
4054 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4057 static void uwb_exit(void)
4059 if (tpacpi_uwb_rfkill)
4060 rfkill_unregister(tpacpi_uwb_rfkill);
4063 static int __init uwb_init(struct ibm_init_struct *iibm)
4065 int res;
4066 int status = 0;
4068 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4069 "initializing uwb subdriver\n");
4071 TPACPI_ACPIHANDLE_INIT(hkey);
4073 tp_features.uwb = hkey_handle &&
4074 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4076 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4077 "uwb is %s, status 0x%02x\n",
4078 str_supported(tp_features.uwb),
4079 status);
4081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4082 if (dbg_uwbemul) {
4083 tp_features.uwb = 1;
4084 printk(TPACPI_INFO
4085 "uwb switch emulation enabled\n");
4086 } else
4087 #endif
4088 if (tp_features.uwb &&
4089 !(status & TP_ACPI_UWB_HWPRESENT)) {
4090 /* no uwb hardware present in system */
4091 tp_features.uwb = 0;
4092 dbg_printk(TPACPI_DBG_INIT,
4093 "uwb hardware not installed\n");
4096 if (!tp_features.uwb)
4097 return 1;
4099 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4100 &tpacpi_uwb_rfkill,
4101 RFKILL_TYPE_UWB,
4102 TPACPI_RFK_UWB_SW_NAME,
4103 false,
4104 tpacpi_uwb_rfk_set,
4105 tpacpi_uwb_rfk_get);
4107 return res;
4110 static struct ibm_struct uwb_driver_data = {
4111 .name = "uwb",
4112 .exit = uwb_exit,
4113 .flags.experimental = 1,
4116 /*************************************************************************
4117 * Video subdriver
4120 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4122 enum video_access_mode {
4123 TPACPI_VIDEO_NONE = 0,
4124 TPACPI_VIDEO_570, /* 570 */
4125 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4126 TPACPI_VIDEO_NEW, /* all others */
4129 enum { /* video status flags, based on VIDEO_570 */
4130 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4131 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4132 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4135 enum { /* TPACPI_VIDEO_570 constants */
4136 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4137 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4138 * video_status_flags */
4139 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4140 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4143 static enum video_access_mode video_supported;
4144 static int video_orig_autosw;
4146 static int video_autosw_get(void);
4147 static int video_autosw_set(int enable);
4149 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4151 static int __init video_init(struct ibm_init_struct *iibm)
4153 int ivga;
4155 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4157 TPACPI_ACPIHANDLE_INIT(vid);
4158 TPACPI_ACPIHANDLE_INIT(vid2);
4160 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4161 /* G41, assume IVGA doesn't change */
4162 vid_handle = vid2_handle;
4164 if (!vid_handle)
4165 /* video switching not supported on R30, R31 */
4166 video_supported = TPACPI_VIDEO_NONE;
4167 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4168 /* 570 */
4169 video_supported = TPACPI_VIDEO_570;
4170 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4171 /* 600e/x, 770e, 770x */
4172 video_supported = TPACPI_VIDEO_770;
4173 else
4174 /* all others */
4175 video_supported = TPACPI_VIDEO_NEW;
4177 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4178 str_supported(video_supported != TPACPI_VIDEO_NONE),
4179 video_supported);
4181 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4184 static void video_exit(void)
4186 dbg_printk(TPACPI_DBG_EXIT,
4187 "restoring original video autoswitch mode\n");
4188 if (video_autosw_set(video_orig_autosw))
4189 printk(TPACPI_ERR "error while trying to restore original "
4190 "video autoswitch mode\n");
4193 static int video_outputsw_get(void)
4195 int status = 0;
4196 int i;
4198 switch (video_supported) {
4199 case TPACPI_VIDEO_570:
4200 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4201 TP_ACPI_VIDEO_570_PHSCMD))
4202 return -EIO;
4203 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4204 break;
4205 case TPACPI_VIDEO_770:
4206 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4207 return -EIO;
4208 if (i)
4209 status |= TP_ACPI_VIDEO_S_LCD;
4210 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4211 return -EIO;
4212 if (i)
4213 status |= TP_ACPI_VIDEO_S_CRT;
4214 break;
4215 case TPACPI_VIDEO_NEW:
4216 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4217 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4218 return -EIO;
4219 if (i)
4220 status |= TP_ACPI_VIDEO_S_CRT;
4222 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4223 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4224 return -EIO;
4225 if (i)
4226 status |= TP_ACPI_VIDEO_S_LCD;
4227 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4228 return -EIO;
4229 if (i)
4230 status |= TP_ACPI_VIDEO_S_DVI;
4231 break;
4232 default:
4233 return -ENOSYS;
4236 return status;
4239 static int video_outputsw_set(int status)
4241 int autosw;
4242 int res = 0;
4244 switch (video_supported) {
4245 case TPACPI_VIDEO_570:
4246 res = acpi_evalf(NULL, NULL,
4247 "\\_SB.PHS2", "vdd",
4248 TP_ACPI_VIDEO_570_PHS2CMD,
4249 status | TP_ACPI_VIDEO_570_PHS2SET);
4250 break;
4251 case TPACPI_VIDEO_770:
4252 autosw = video_autosw_get();
4253 if (autosw < 0)
4254 return autosw;
4256 res = video_autosw_set(1);
4257 if (res)
4258 return res;
4259 res = acpi_evalf(vid_handle, NULL,
4260 "ASWT", "vdd", status * 0x100, 0);
4261 if (!autosw && video_autosw_set(autosw)) {
4262 printk(TPACPI_ERR
4263 "video auto-switch left enabled due to error\n");
4264 return -EIO;
4266 break;
4267 case TPACPI_VIDEO_NEW:
4268 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4269 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4270 break;
4271 default:
4272 return -ENOSYS;
4275 return (res)? 0 : -EIO;
4278 static int video_autosw_get(void)
4280 int autosw = 0;
4282 switch (video_supported) {
4283 case TPACPI_VIDEO_570:
4284 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4285 return -EIO;
4286 break;
4287 case TPACPI_VIDEO_770:
4288 case TPACPI_VIDEO_NEW:
4289 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4290 return -EIO;
4291 break;
4292 default:
4293 return -ENOSYS;
4296 return autosw & 1;
4299 static int video_autosw_set(int enable)
4301 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4302 return -EIO;
4303 return 0;
4306 static int video_outputsw_cycle(void)
4308 int autosw = video_autosw_get();
4309 int res;
4311 if (autosw < 0)
4312 return autosw;
4314 switch (video_supported) {
4315 case TPACPI_VIDEO_570:
4316 res = video_autosw_set(1);
4317 if (res)
4318 return res;
4319 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4320 break;
4321 case TPACPI_VIDEO_770:
4322 case TPACPI_VIDEO_NEW:
4323 res = video_autosw_set(1);
4324 if (res)
4325 return res;
4326 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4327 break;
4328 default:
4329 return -ENOSYS;
4331 if (!autosw && video_autosw_set(autosw)) {
4332 printk(TPACPI_ERR
4333 "video auto-switch left enabled due to error\n");
4334 return -EIO;
4337 return (res)? 0 : -EIO;
4340 static int video_expand_toggle(void)
4342 switch (video_supported) {
4343 case TPACPI_VIDEO_570:
4344 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4345 0 : -EIO;
4346 case TPACPI_VIDEO_770:
4347 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4348 0 : -EIO;
4349 case TPACPI_VIDEO_NEW:
4350 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4351 0 : -EIO;
4352 default:
4353 return -ENOSYS;
4355 /* not reached */
4358 static int video_read(char *p)
4360 int status, autosw;
4361 int len = 0;
4363 if (video_supported == TPACPI_VIDEO_NONE) {
4364 len += sprintf(p + len, "status:\t\tnot supported\n");
4365 return len;
4368 status = video_outputsw_get();
4369 if (status < 0)
4370 return status;
4372 autosw = video_autosw_get();
4373 if (autosw < 0)
4374 return autosw;
4376 len += sprintf(p + len, "status:\t\tsupported\n");
4377 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4378 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4379 if (video_supported == TPACPI_VIDEO_NEW)
4380 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4381 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4382 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4383 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4384 if (video_supported == TPACPI_VIDEO_NEW)
4385 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4386 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4387 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4389 return len;
4392 static int video_write(char *buf)
4394 char *cmd;
4395 int enable, disable, status;
4396 int res;
4398 if (video_supported == TPACPI_VIDEO_NONE)
4399 return -ENODEV;
4401 enable = 0;
4402 disable = 0;
4404 while ((cmd = next_cmd(&buf))) {
4405 if (strlencmp(cmd, "lcd_enable") == 0) {
4406 enable |= TP_ACPI_VIDEO_S_LCD;
4407 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4408 disable |= TP_ACPI_VIDEO_S_LCD;
4409 } else if (strlencmp(cmd, "crt_enable") == 0) {
4410 enable |= TP_ACPI_VIDEO_S_CRT;
4411 } else if (strlencmp(cmd, "crt_disable") == 0) {
4412 disable |= TP_ACPI_VIDEO_S_CRT;
4413 } else if (video_supported == TPACPI_VIDEO_NEW &&
4414 strlencmp(cmd, "dvi_enable") == 0) {
4415 enable |= TP_ACPI_VIDEO_S_DVI;
4416 } else if (video_supported == TPACPI_VIDEO_NEW &&
4417 strlencmp(cmd, "dvi_disable") == 0) {
4418 disable |= TP_ACPI_VIDEO_S_DVI;
4419 } else if (strlencmp(cmd, "auto_enable") == 0) {
4420 res = video_autosw_set(1);
4421 if (res)
4422 return res;
4423 } else if (strlencmp(cmd, "auto_disable") == 0) {
4424 res = video_autosw_set(0);
4425 if (res)
4426 return res;
4427 } else if (strlencmp(cmd, "video_switch") == 0) {
4428 res = video_outputsw_cycle();
4429 if (res)
4430 return res;
4431 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4432 res = video_expand_toggle();
4433 if (res)
4434 return res;
4435 } else
4436 return -EINVAL;
4439 if (enable || disable) {
4440 status = video_outputsw_get();
4441 if (status < 0)
4442 return status;
4443 res = video_outputsw_set((status & ~disable) | enable);
4444 if (res)
4445 return res;
4448 return 0;
4451 static struct ibm_struct video_driver_data = {
4452 .name = "video",
4453 .read = video_read,
4454 .write = video_write,
4455 .exit = video_exit,
4458 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4460 /*************************************************************************
4461 * Light (thinklight) subdriver
4464 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4465 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4467 static int light_get_status(void)
4469 int status = 0;
4471 if (tp_features.light_status) {
4472 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4473 return -EIO;
4474 return (!!status);
4477 return -ENXIO;
4480 static int light_set_status(int status)
4482 int rc;
4484 if (tp_features.light) {
4485 if (cmos_handle) {
4486 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4487 (status)?
4488 TP_CMOS_THINKLIGHT_ON :
4489 TP_CMOS_THINKLIGHT_OFF);
4490 } else {
4491 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4492 (status)? 1 : 0);
4494 return (rc)? 0 : -EIO;
4497 return -ENXIO;
4500 static void light_set_status_worker(struct work_struct *work)
4502 struct tpacpi_led_classdev *data =
4503 container_of(work, struct tpacpi_led_classdev, work);
4505 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4506 light_set_status((data->new_state != TPACPI_LED_OFF));
4509 static void light_sysfs_set(struct led_classdev *led_cdev,
4510 enum led_brightness brightness)
4512 struct tpacpi_led_classdev *data =
4513 container_of(led_cdev,
4514 struct tpacpi_led_classdev,
4515 led_classdev);
4516 data->new_state = (brightness != LED_OFF) ?
4517 TPACPI_LED_ON : TPACPI_LED_OFF;
4518 queue_work(tpacpi_wq, &data->work);
4521 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4523 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4526 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4527 .led_classdev = {
4528 .name = "tpacpi::thinklight",
4529 .brightness_set = &light_sysfs_set,
4530 .brightness_get = &light_sysfs_get,
4534 static int __init light_init(struct ibm_init_struct *iibm)
4536 int rc;
4538 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4540 TPACPI_ACPIHANDLE_INIT(ledb);
4541 TPACPI_ACPIHANDLE_INIT(lght);
4542 TPACPI_ACPIHANDLE_INIT(cmos);
4543 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4545 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4546 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4548 if (tp_features.light)
4549 /* light status not supported on
4550 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4551 tp_features.light_status =
4552 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4554 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4555 str_supported(tp_features.light),
4556 str_supported(tp_features.light_status));
4558 if (!tp_features.light)
4559 return 1;
4561 rc = led_classdev_register(&tpacpi_pdev->dev,
4562 &tpacpi_led_thinklight.led_classdev);
4564 if (rc < 0) {
4565 tp_features.light = 0;
4566 tp_features.light_status = 0;
4567 } else {
4568 rc = 0;
4571 return rc;
4574 static void light_exit(void)
4576 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4577 if (work_pending(&tpacpi_led_thinklight.work))
4578 flush_workqueue(tpacpi_wq);
4581 static int light_read(char *p)
4583 int len = 0;
4584 int status;
4586 if (!tp_features.light) {
4587 len += sprintf(p + len, "status:\t\tnot supported\n");
4588 } else if (!tp_features.light_status) {
4589 len += sprintf(p + len, "status:\t\tunknown\n");
4590 len += sprintf(p + len, "commands:\ton, off\n");
4591 } else {
4592 status = light_get_status();
4593 if (status < 0)
4594 return status;
4595 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4596 len += sprintf(p + len, "commands:\ton, off\n");
4599 return len;
4602 static int light_write(char *buf)
4604 char *cmd;
4605 int newstatus = 0;
4607 if (!tp_features.light)
4608 return -ENODEV;
4610 while ((cmd = next_cmd(&buf))) {
4611 if (strlencmp(cmd, "on") == 0) {
4612 newstatus = 1;
4613 } else if (strlencmp(cmd, "off") == 0) {
4614 newstatus = 0;
4615 } else
4616 return -EINVAL;
4619 return light_set_status(newstatus);
4622 static struct ibm_struct light_driver_data = {
4623 .name = "light",
4624 .read = light_read,
4625 .write = light_write,
4626 .exit = light_exit,
4629 /*************************************************************************
4630 * CMOS subdriver
4633 /* sysfs cmos_command -------------------------------------------------- */
4634 static ssize_t cmos_command_store(struct device *dev,
4635 struct device_attribute *attr,
4636 const char *buf, size_t count)
4638 unsigned long cmos_cmd;
4639 int res;
4641 if (parse_strtoul(buf, 21, &cmos_cmd))
4642 return -EINVAL;
4644 res = issue_thinkpad_cmos_command(cmos_cmd);
4645 return (res)? res : count;
4648 static struct device_attribute dev_attr_cmos_command =
4649 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4651 /* --------------------------------------------------------------------- */
4653 static int __init cmos_init(struct ibm_init_struct *iibm)
4655 int res;
4657 vdbg_printk(TPACPI_DBG_INIT,
4658 "initializing cmos commands subdriver\n");
4660 TPACPI_ACPIHANDLE_INIT(cmos);
4662 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4663 str_supported(cmos_handle != NULL));
4665 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4666 if (res)
4667 return res;
4669 return (cmos_handle)? 0 : 1;
4672 static void cmos_exit(void)
4674 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4677 static int cmos_read(char *p)
4679 int len = 0;
4681 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4682 R30, R31, T20-22, X20-21 */
4683 if (!cmos_handle)
4684 len += sprintf(p + len, "status:\t\tnot supported\n");
4685 else {
4686 len += sprintf(p + len, "status:\t\tsupported\n");
4687 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4690 return len;
4693 static int cmos_write(char *buf)
4695 char *cmd;
4696 int cmos_cmd, res;
4698 while ((cmd = next_cmd(&buf))) {
4699 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4700 cmos_cmd >= 0 && cmos_cmd <= 21) {
4701 /* cmos_cmd set */
4702 } else
4703 return -EINVAL;
4705 res = issue_thinkpad_cmos_command(cmos_cmd);
4706 if (res)
4707 return res;
4710 return 0;
4713 static struct ibm_struct cmos_driver_data = {
4714 .name = "cmos",
4715 .read = cmos_read,
4716 .write = cmos_write,
4717 .exit = cmos_exit,
4720 /*************************************************************************
4721 * LED subdriver
4724 enum led_access_mode {
4725 TPACPI_LED_NONE = 0,
4726 TPACPI_LED_570, /* 570 */
4727 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4728 TPACPI_LED_NEW, /* all others */
4731 enum { /* For TPACPI_LED_OLD */
4732 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4733 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4734 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4737 static enum led_access_mode led_supported;
4739 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4740 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4741 /* T20-22, X20-21 */
4742 "LED", /* all others */
4743 ); /* R30, R31 */
4745 #define TPACPI_LED_NUMLEDS 16
4746 static struct tpacpi_led_classdev *tpacpi_leds;
4747 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4748 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4749 /* there's a limit of 19 chars + NULL before 2.6.26 */
4750 "tpacpi::power",
4751 "tpacpi:orange:batt",
4752 "tpacpi:green:batt",
4753 "tpacpi::dock_active",
4754 "tpacpi::bay_active",
4755 "tpacpi::dock_batt",
4756 "tpacpi::unknown_led",
4757 "tpacpi::standby",
4758 "tpacpi::dock_status1",
4759 "tpacpi::dock_status2",
4760 "tpacpi::unknown_led2",
4761 "tpacpi::unknown_led3",
4762 "tpacpi::thinkvantage",
4764 #define TPACPI_SAFE_LEDS 0x1081U
4766 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4768 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4769 return false;
4770 #else
4771 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4772 #endif
4775 static int led_get_status(const unsigned int led)
4777 int status;
4778 enum led_status_t led_s;
4780 switch (led_supported) {
4781 case TPACPI_LED_570:
4782 if (!acpi_evalf(ec_handle,
4783 &status, "GLED", "dd", 1 << led))
4784 return -EIO;
4785 led_s = (status == 0)?
4786 TPACPI_LED_OFF :
4787 ((status == 1)?
4788 TPACPI_LED_ON :
4789 TPACPI_LED_BLINK);
4790 tpacpi_led_state_cache[led] = led_s;
4791 return led_s;
4792 default:
4793 return -ENXIO;
4796 /* not reached */
4799 static int led_set_status(const unsigned int led,
4800 const enum led_status_t ledstatus)
4802 /* off, on, blink. Index is led_status_t */
4803 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4804 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4806 int rc = 0;
4808 switch (led_supported) {
4809 case TPACPI_LED_570:
4810 /* 570 */
4811 if (unlikely(led > 7))
4812 return -EINVAL;
4813 if (unlikely(tpacpi_is_led_restricted(led)))
4814 return -EPERM;
4815 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4816 (1 << led), led_sled_arg1[ledstatus]))
4817 rc = -EIO;
4818 break;
4819 case TPACPI_LED_OLD:
4820 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4821 if (unlikely(led > 7))
4822 return -EINVAL;
4823 if (unlikely(tpacpi_is_led_restricted(led)))
4824 return -EPERM;
4825 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4826 if (rc >= 0)
4827 rc = ec_write(TPACPI_LED_EC_HLBL,
4828 (ledstatus == TPACPI_LED_BLINK) << led);
4829 if (rc >= 0)
4830 rc = ec_write(TPACPI_LED_EC_HLCL,
4831 (ledstatus != TPACPI_LED_OFF) << led);
4832 break;
4833 case TPACPI_LED_NEW:
4834 /* all others */
4835 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4836 return -EINVAL;
4837 if (unlikely(tpacpi_is_led_restricted(led)))
4838 return -EPERM;
4839 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4840 led, led_led_arg1[ledstatus]))
4841 rc = -EIO;
4842 break;
4843 default:
4844 rc = -ENXIO;
4847 if (!rc)
4848 tpacpi_led_state_cache[led] = ledstatus;
4850 return rc;
4853 static void led_set_status_worker(struct work_struct *work)
4855 struct tpacpi_led_classdev *data =
4856 container_of(work, struct tpacpi_led_classdev, work);
4858 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4859 led_set_status(data->led, data->new_state);
4862 static void led_sysfs_set(struct led_classdev *led_cdev,
4863 enum led_brightness brightness)
4865 struct tpacpi_led_classdev *data = container_of(led_cdev,
4866 struct tpacpi_led_classdev, led_classdev);
4868 if (brightness == LED_OFF)
4869 data->new_state = TPACPI_LED_OFF;
4870 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4871 data->new_state = TPACPI_LED_ON;
4872 else
4873 data->new_state = TPACPI_LED_BLINK;
4875 queue_work(tpacpi_wq, &data->work);
4878 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4879 unsigned long *delay_on, unsigned long *delay_off)
4881 struct tpacpi_led_classdev *data = container_of(led_cdev,
4882 struct tpacpi_led_classdev, led_classdev);
4884 /* Can we choose the flash rate? */
4885 if (*delay_on == 0 && *delay_off == 0) {
4886 /* yes. set them to the hardware blink rate (1 Hz) */
4887 *delay_on = 500; /* ms */
4888 *delay_off = 500; /* ms */
4889 } else if ((*delay_on != 500) || (*delay_off != 500))
4890 return -EINVAL;
4892 data->new_state = TPACPI_LED_BLINK;
4893 queue_work(tpacpi_wq, &data->work);
4895 return 0;
4898 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4900 int rc;
4902 struct tpacpi_led_classdev *data = container_of(led_cdev,
4903 struct tpacpi_led_classdev, led_classdev);
4905 rc = led_get_status(data->led);
4907 if (rc == TPACPI_LED_OFF || rc < 0)
4908 rc = LED_OFF; /* no error handling in led class :( */
4909 else
4910 rc = LED_FULL;
4912 return rc;
4915 static void led_exit(void)
4917 unsigned int i;
4919 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4920 if (tpacpi_leds[i].led_classdev.name)
4921 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4924 kfree(tpacpi_leds);
4927 static int __init tpacpi_init_led(unsigned int led)
4929 int rc;
4931 tpacpi_leds[led].led = led;
4933 /* LEDs with no name don't get registered */
4934 if (!tpacpi_led_names[led])
4935 return 0;
4937 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4938 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4939 if (led_supported == TPACPI_LED_570)
4940 tpacpi_leds[led].led_classdev.brightness_get =
4941 &led_sysfs_get;
4943 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4945 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4947 rc = led_classdev_register(&tpacpi_pdev->dev,
4948 &tpacpi_leds[led].led_classdev);
4949 if (rc < 0)
4950 tpacpi_leds[led].led_classdev.name = NULL;
4952 return rc;
4955 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4956 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4957 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4958 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4960 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4961 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4962 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4963 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4964 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4965 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4966 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4967 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4969 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4970 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4971 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4972 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4973 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4975 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4976 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4977 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4978 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4980 /* (1) - may have excess leds enabled on MSB */
4982 /* Defaults (order matters, keep last, don't reorder!) */
4983 { /* Lenovo */
4984 .vendor = PCI_VENDOR_ID_LENOVO,
4985 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4986 .quirks = 0x1fffU,
4988 { /* IBM ThinkPads with no EC version string */
4989 .vendor = PCI_VENDOR_ID_IBM,
4990 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4991 .quirks = 0x00ffU,
4993 { /* IBM ThinkPads with EC version string */
4994 .vendor = PCI_VENDOR_ID_IBM,
4995 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4996 .quirks = 0x00bfU,
5000 #undef TPACPI_LEDQ_IBM
5001 #undef TPACPI_LEDQ_LNV
5003 static int __init led_init(struct ibm_init_struct *iibm)
5005 unsigned int i;
5006 int rc;
5007 unsigned long useful_leds;
5009 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5011 TPACPI_ACPIHANDLE_INIT(led);
5013 if (!led_handle)
5014 /* led not supported on R30, R31 */
5015 led_supported = TPACPI_LED_NONE;
5016 else if (strlencmp(led_path, "SLED") == 0)
5017 /* 570 */
5018 led_supported = TPACPI_LED_570;
5019 else if (strlencmp(led_path, "SYSL") == 0)
5020 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5021 led_supported = TPACPI_LED_OLD;
5022 else
5023 /* all others */
5024 led_supported = TPACPI_LED_NEW;
5026 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5027 str_supported(led_supported), led_supported);
5029 if (led_supported == TPACPI_LED_NONE)
5030 return 1;
5032 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5033 GFP_KERNEL);
5034 if (!tpacpi_leds) {
5035 printk(TPACPI_ERR "Out of memory for LED data\n");
5036 return -ENOMEM;
5039 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5040 ARRAY_SIZE(led_useful_qtable));
5042 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5043 if (!tpacpi_is_led_restricted(i) &&
5044 test_bit(i, &useful_leds)) {
5045 rc = tpacpi_init_led(i);
5046 if (rc < 0) {
5047 led_exit();
5048 return rc;
5053 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5054 printk(TPACPI_NOTICE
5055 "warning: userspace override of important "
5056 "firmware LEDs is enabled\n");
5057 #endif
5058 return 0;
5061 #define str_led_status(s) \
5062 ((s) == TPACPI_LED_OFF ? "off" : \
5063 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5065 static int led_read(char *p)
5067 int len = 0;
5069 if (!led_supported) {
5070 len += sprintf(p + len, "status:\t\tnot supported\n");
5071 return len;
5073 len += sprintf(p + len, "status:\t\tsupported\n");
5075 if (led_supported == TPACPI_LED_570) {
5076 /* 570 */
5077 int i, status;
5078 for (i = 0; i < 8; i++) {
5079 status = led_get_status(i);
5080 if (status < 0)
5081 return -EIO;
5082 len += sprintf(p + len, "%d:\t\t%s\n",
5083 i, str_led_status(status));
5087 len += sprintf(p + len, "commands:\t"
5088 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5090 return len;
5093 static int led_write(char *buf)
5095 char *cmd;
5096 int led, rc;
5097 enum led_status_t s;
5099 if (!led_supported)
5100 return -ENODEV;
5102 while ((cmd = next_cmd(&buf))) {
5103 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5104 return -EINVAL;
5106 if (strstr(cmd, "off")) {
5107 s = TPACPI_LED_OFF;
5108 } else if (strstr(cmd, "on")) {
5109 s = TPACPI_LED_ON;
5110 } else if (strstr(cmd, "blink")) {
5111 s = TPACPI_LED_BLINK;
5112 } else {
5113 return -EINVAL;
5116 rc = led_set_status(led, s);
5117 if (rc < 0)
5118 return rc;
5121 return 0;
5124 static struct ibm_struct led_driver_data = {
5125 .name = "led",
5126 .read = led_read,
5127 .write = led_write,
5128 .exit = led_exit,
5131 /*************************************************************************
5132 * Beep subdriver
5135 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5137 #define TPACPI_BEEP_Q1 0x0001
5139 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5140 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5141 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5144 static int __init beep_init(struct ibm_init_struct *iibm)
5146 unsigned long quirks;
5148 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5150 TPACPI_ACPIHANDLE_INIT(beep);
5152 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5153 str_supported(beep_handle != NULL));
5155 quirks = tpacpi_check_quirks(beep_quirk_table,
5156 ARRAY_SIZE(beep_quirk_table));
5158 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5160 return (beep_handle)? 0 : 1;
5163 static int beep_read(char *p)
5165 int len = 0;
5167 if (!beep_handle)
5168 len += sprintf(p + len, "status:\t\tnot supported\n");
5169 else {
5170 len += sprintf(p + len, "status:\t\tsupported\n");
5171 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5174 return len;
5177 static int beep_write(char *buf)
5179 char *cmd;
5180 int beep_cmd;
5182 if (!beep_handle)
5183 return -ENODEV;
5185 while ((cmd = next_cmd(&buf))) {
5186 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5187 beep_cmd >= 0 && beep_cmd <= 17) {
5188 /* beep_cmd set */
5189 } else
5190 return -EINVAL;
5191 if (tp_features.beep_needs_two_args) {
5192 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5193 beep_cmd, 0))
5194 return -EIO;
5195 } else {
5196 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5197 beep_cmd))
5198 return -EIO;
5202 return 0;
5205 static struct ibm_struct beep_driver_data = {
5206 .name = "beep",
5207 .read = beep_read,
5208 .write = beep_write,
5211 /*************************************************************************
5212 * Thermal subdriver
5215 enum thermal_access_mode {
5216 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5217 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5218 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5219 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5220 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5223 enum { /* TPACPI_THERMAL_TPEC_* */
5224 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5225 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5226 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5229 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5230 struct ibm_thermal_sensors_struct {
5231 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5234 static enum thermal_access_mode thermal_read_mode;
5236 /* idx is zero-based */
5237 static int thermal_get_sensor(int idx, s32 *value)
5239 int t;
5240 s8 tmp;
5241 char tmpi[5];
5243 t = TP_EC_THERMAL_TMP0;
5245 switch (thermal_read_mode) {
5246 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5247 case TPACPI_THERMAL_TPEC_16:
5248 if (idx >= 8 && idx <= 15) {
5249 t = TP_EC_THERMAL_TMP8;
5250 idx -= 8;
5252 /* fallthrough */
5253 #endif
5254 case TPACPI_THERMAL_TPEC_8:
5255 if (idx <= 7) {
5256 if (!acpi_ec_read(t + idx, &tmp))
5257 return -EIO;
5258 *value = tmp * 1000;
5259 return 0;
5261 break;
5263 case TPACPI_THERMAL_ACPI_UPDT:
5264 if (idx <= 7) {
5265 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5266 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5267 return -EIO;
5268 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5269 return -EIO;
5270 *value = (t - 2732) * 100;
5271 return 0;
5273 break;
5275 case TPACPI_THERMAL_ACPI_TMP07:
5276 if (idx <= 7) {
5277 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5278 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5279 return -EIO;
5280 if (t > 127 || t < -127)
5281 t = TP_EC_THERMAL_TMP_NA;
5282 *value = t * 1000;
5283 return 0;
5285 break;
5287 case TPACPI_THERMAL_NONE:
5288 default:
5289 return -ENOSYS;
5292 return -EINVAL;
5295 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5297 int res, i;
5298 int n;
5300 n = 8;
5301 i = 0;
5303 if (!s)
5304 return -EINVAL;
5306 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5307 n = 16;
5309 for (i = 0 ; i < n; i++) {
5310 res = thermal_get_sensor(i, &s->temp[i]);
5311 if (res)
5312 return res;
5315 return n;
5318 /* sysfs temp##_input -------------------------------------------------- */
5320 static ssize_t thermal_temp_input_show(struct device *dev,
5321 struct device_attribute *attr,
5322 char *buf)
5324 struct sensor_device_attribute *sensor_attr =
5325 to_sensor_dev_attr(attr);
5326 int idx = sensor_attr->index;
5327 s32 value;
5328 int res;
5330 res = thermal_get_sensor(idx, &value);
5331 if (res)
5332 return res;
5333 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5334 return -ENXIO;
5336 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5339 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5340 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5341 thermal_temp_input_show, NULL, _idxB)
5343 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5344 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5345 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5346 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5347 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5348 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5349 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5350 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5351 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5352 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5353 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5354 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5355 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5356 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5357 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5358 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5359 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5362 #define THERMAL_ATTRS(X) \
5363 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5365 static struct attribute *thermal_temp_input_attr[] = {
5366 THERMAL_ATTRS(8),
5367 THERMAL_ATTRS(9),
5368 THERMAL_ATTRS(10),
5369 THERMAL_ATTRS(11),
5370 THERMAL_ATTRS(12),
5371 THERMAL_ATTRS(13),
5372 THERMAL_ATTRS(14),
5373 THERMAL_ATTRS(15),
5374 THERMAL_ATTRS(0),
5375 THERMAL_ATTRS(1),
5376 THERMAL_ATTRS(2),
5377 THERMAL_ATTRS(3),
5378 THERMAL_ATTRS(4),
5379 THERMAL_ATTRS(5),
5380 THERMAL_ATTRS(6),
5381 THERMAL_ATTRS(7),
5382 NULL
5385 static const struct attribute_group thermal_temp_input16_group = {
5386 .attrs = thermal_temp_input_attr
5389 static const struct attribute_group thermal_temp_input8_group = {
5390 .attrs = &thermal_temp_input_attr[8]
5393 #undef THERMAL_SENSOR_ATTR_TEMP
5394 #undef THERMAL_ATTRS
5396 /* --------------------------------------------------------------------- */
5398 static int __init thermal_init(struct ibm_init_struct *iibm)
5400 u8 t, ta1, ta2;
5401 int i;
5402 int acpi_tmp7;
5403 int res;
5405 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5407 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5409 if (thinkpad_id.ec_model) {
5411 * Direct EC access mode: sensors at registers
5412 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5413 * non-implemented, thermal sensors return 0x80 when
5414 * not available
5417 ta1 = ta2 = 0;
5418 for (i = 0; i < 8; i++) {
5419 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5420 ta1 |= t;
5421 } else {
5422 ta1 = 0;
5423 break;
5425 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5426 ta2 |= t;
5427 } else {
5428 ta1 = 0;
5429 break;
5432 if (ta1 == 0) {
5433 /* This is sheer paranoia, but we handle it anyway */
5434 if (acpi_tmp7) {
5435 printk(TPACPI_ERR
5436 "ThinkPad ACPI EC access misbehaving, "
5437 "falling back to ACPI TMPx access "
5438 "mode\n");
5439 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5440 } else {
5441 printk(TPACPI_ERR
5442 "ThinkPad ACPI EC access misbehaving, "
5443 "disabling thermal sensors access\n");
5444 thermal_read_mode = TPACPI_THERMAL_NONE;
5446 } else {
5447 thermal_read_mode =
5448 (ta2 != 0) ?
5449 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5451 } else if (acpi_tmp7) {
5452 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5453 /* 600e/x, 770e, 770x */
5454 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5455 } else {
5456 /* Standard ACPI TMPx access, max 8 sensors */
5457 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5459 } else {
5460 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5461 thermal_read_mode = TPACPI_THERMAL_NONE;
5464 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5465 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5466 thermal_read_mode);
5468 switch (thermal_read_mode) {
5469 case TPACPI_THERMAL_TPEC_16:
5470 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5471 &thermal_temp_input16_group);
5472 if (res)
5473 return res;
5474 break;
5475 case TPACPI_THERMAL_TPEC_8:
5476 case TPACPI_THERMAL_ACPI_TMP07:
5477 case TPACPI_THERMAL_ACPI_UPDT:
5478 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5479 &thermal_temp_input8_group);
5480 if (res)
5481 return res;
5482 break;
5483 case TPACPI_THERMAL_NONE:
5484 default:
5485 return 1;
5488 return 0;
5491 static void thermal_exit(void)
5493 switch (thermal_read_mode) {
5494 case TPACPI_THERMAL_TPEC_16:
5495 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5496 &thermal_temp_input16_group);
5497 break;
5498 case TPACPI_THERMAL_TPEC_8:
5499 case TPACPI_THERMAL_ACPI_TMP07:
5500 case TPACPI_THERMAL_ACPI_UPDT:
5501 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5502 &thermal_temp_input16_group);
5503 break;
5504 case TPACPI_THERMAL_NONE:
5505 default:
5506 break;
5510 static int thermal_read(char *p)
5512 int len = 0;
5513 int n, i;
5514 struct ibm_thermal_sensors_struct t;
5516 n = thermal_get_sensors(&t);
5517 if (unlikely(n < 0))
5518 return n;
5520 len += sprintf(p + len, "temperatures:\t");
5522 if (n > 0) {
5523 for (i = 0; i < (n - 1); i++)
5524 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5525 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5526 } else
5527 len += sprintf(p + len, "not supported\n");
5529 return len;
5532 static struct ibm_struct thermal_driver_data = {
5533 .name = "thermal",
5534 .read = thermal_read,
5535 .exit = thermal_exit,
5538 /*************************************************************************
5539 * EC Dump subdriver
5542 static u8 ecdump_regs[256];
5544 static int ecdump_read(char *p)
5546 int len = 0;
5547 int i, j;
5548 u8 v;
5550 len += sprintf(p + len, "EC "
5551 " +00 +01 +02 +03 +04 +05 +06 +07"
5552 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5553 for (i = 0; i < 256; i += 16) {
5554 len += sprintf(p + len, "EC 0x%02x:", i);
5555 for (j = 0; j < 16; j++) {
5556 if (!acpi_ec_read(i + j, &v))
5557 break;
5558 if (v != ecdump_regs[i + j])
5559 len += sprintf(p + len, " *%02x", v);
5560 else
5561 len += sprintf(p + len, " %02x", v);
5562 ecdump_regs[i + j] = v;
5564 len += sprintf(p + len, "\n");
5565 if (j != 16)
5566 break;
5569 /* These are way too dangerous to advertise openly... */
5570 #if 0
5571 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5572 " (<offset> is 00-ff, <value> is 00-ff)\n");
5573 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5574 " (<offset> is 00-ff, <value> is 0-255)\n");
5575 #endif
5576 return len;
5579 static int ecdump_write(char *buf)
5581 char *cmd;
5582 int i, v;
5584 while ((cmd = next_cmd(&buf))) {
5585 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5586 /* i and v set */
5587 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5588 /* i and v set */
5589 } else
5590 return -EINVAL;
5591 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5592 if (!acpi_ec_write(i, v))
5593 return -EIO;
5594 } else
5595 return -EINVAL;
5598 return 0;
5601 static struct ibm_struct ecdump_driver_data = {
5602 .name = "ecdump",
5603 .read = ecdump_read,
5604 .write = ecdump_write,
5605 .flags.experimental = 1,
5608 /*************************************************************************
5609 * Backlight/brightness subdriver
5612 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5615 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5616 * CMOS NVRAM byte 0x5E, bits 0-3.
5618 * EC HBRV (0x31) has the following layout
5619 * Bit 7: unknown function
5620 * Bit 6: unknown function
5621 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5622 * Bit 4: must be set to zero to avoid problems
5623 * Bit 3-0: backlight brightness level
5625 * brightness_get_raw returns status data in the HBRV layout
5627 * WARNING: The X61 has been verified to use HBRV for something else, so
5628 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5629 * testing on the very early *60 Lenovo models...
5632 enum {
5633 TP_EC_BACKLIGHT = 0x31,
5635 /* TP_EC_BACKLIGHT bitmasks */
5636 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5637 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5638 TP_EC_BACKLIGHT_MAPSW = 0x20,
5641 enum tpacpi_brightness_access_mode {
5642 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5643 TPACPI_BRGHT_MODE_EC, /* EC control */
5644 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5645 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5646 TPACPI_BRGHT_MODE_MAX
5649 static struct backlight_device *ibm_backlight_device;
5651 static enum tpacpi_brightness_access_mode brightness_mode =
5652 TPACPI_BRGHT_MODE_MAX;
5654 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5656 static struct mutex brightness_mutex;
5658 /* NVRAM brightness access,
5659 * call with brightness_mutex held! */
5660 static unsigned int tpacpi_brightness_nvram_get(void)
5662 u8 lnvram;
5664 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5665 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5666 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5667 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5669 return lnvram;
5672 static void tpacpi_brightness_checkpoint_nvram(void)
5674 u8 lec = 0;
5675 u8 b_nvram;
5677 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5678 return;
5680 vdbg_printk(TPACPI_DBG_BRGHT,
5681 "trying to checkpoint backlight level to NVRAM...\n");
5683 if (mutex_lock_killable(&brightness_mutex) < 0)
5684 return;
5686 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5687 goto unlock;
5688 lec &= TP_EC_BACKLIGHT_LVLMSK;
5689 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5691 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5692 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5693 /* NVRAM needs update */
5694 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5695 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5696 b_nvram |= lec;
5697 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5698 dbg_printk(TPACPI_DBG_BRGHT,
5699 "updated NVRAM backlight level to %u (0x%02x)\n",
5700 (unsigned int) lec, (unsigned int) b_nvram);
5701 } else
5702 vdbg_printk(TPACPI_DBG_BRGHT,
5703 "NVRAM backlight level already is %u (0x%02x)\n",
5704 (unsigned int) lec, (unsigned int) b_nvram);
5706 unlock:
5707 mutex_unlock(&brightness_mutex);
5711 /* call with brightness_mutex held! */
5712 static int tpacpi_brightness_get_raw(int *status)
5714 u8 lec = 0;
5716 switch (brightness_mode) {
5717 case TPACPI_BRGHT_MODE_UCMS_STEP:
5718 *status = tpacpi_brightness_nvram_get();
5719 return 0;
5720 case TPACPI_BRGHT_MODE_EC:
5721 case TPACPI_BRGHT_MODE_ECNVRAM:
5722 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5723 return -EIO;
5724 *status = lec;
5725 return 0;
5726 default:
5727 return -ENXIO;
5731 /* call with brightness_mutex held! */
5732 /* do NOT call with illegal backlight level value */
5733 static int tpacpi_brightness_set_ec(unsigned int value)
5735 u8 lec = 0;
5737 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5738 return -EIO;
5740 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5741 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5742 (value & TP_EC_BACKLIGHT_LVLMSK))))
5743 return -EIO;
5745 return 0;
5748 /* call with brightness_mutex held! */
5749 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5751 int cmos_cmd, inc;
5752 unsigned int current_value, i;
5754 current_value = tpacpi_brightness_nvram_get();
5756 if (value == current_value)
5757 return 0;
5759 cmos_cmd = (value > current_value) ?
5760 TP_CMOS_BRIGHTNESS_UP :
5761 TP_CMOS_BRIGHTNESS_DOWN;
5762 inc = (value > current_value) ? 1 : -1;
5764 for (i = current_value; i != value; i += inc)
5765 if (issue_thinkpad_cmos_command(cmos_cmd))
5766 return -EIO;
5768 return 0;
5771 /* May return EINTR which can always be mapped to ERESTARTSYS */
5772 static int brightness_set(unsigned int value)
5774 int res;
5776 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5777 value < 0)
5778 return -EINVAL;
5780 vdbg_printk(TPACPI_DBG_BRGHT,
5781 "set backlight level to %d\n", value);
5783 res = mutex_lock_killable(&brightness_mutex);
5784 if (res < 0)
5785 return res;
5787 switch (brightness_mode) {
5788 case TPACPI_BRGHT_MODE_EC:
5789 case TPACPI_BRGHT_MODE_ECNVRAM:
5790 res = tpacpi_brightness_set_ec(value);
5791 break;
5792 case TPACPI_BRGHT_MODE_UCMS_STEP:
5793 res = tpacpi_brightness_set_ucmsstep(value);
5794 break;
5795 default:
5796 res = -ENXIO;
5799 mutex_unlock(&brightness_mutex);
5800 return res;
5803 /* sysfs backlight class ----------------------------------------------- */
5805 static int brightness_update_status(struct backlight_device *bd)
5807 unsigned int level =
5808 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5809 bd->props.power == FB_BLANK_UNBLANK) ?
5810 bd->props.brightness : 0;
5812 dbg_printk(TPACPI_DBG_BRGHT,
5813 "backlight: attempt to set level to %d\n",
5814 level);
5816 /* it is the backlight class's job (caller) to handle
5817 * EINTR and other errors properly */
5818 return brightness_set(level);
5821 static int brightness_get(struct backlight_device *bd)
5823 int status, res;
5825 res = mutex_lock_killable(&brightness_mutex);
5826 if (res < 0)
5827 return 0;
5829 res = tpacpi_brightness_get_raw(&status);
5831 mutex_unlock(&brightness_mutex);
5833 if (res < 0)
5834 return 0;
5836 return status & TP_EC_BACKLIGHT_LVLMSK;
5839 static struct backlight_ops ibm_backlight_data = {
5840 .get_brightness = brightness_get,
5841 .update_status = brightness_update_status,
5844 /* --------------------------------------------------------------------- */
5847 * These are only useful for models that have only one possibility
5848 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5849 * these quirks.
5851 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5852 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5853 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5855 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5856 /* Models with ATI GPUs known to require ECNVRAM mode */
5857 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5859 /* Models with ATI GPUs that can use ECNVRAM */
5860 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
5861 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5862 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5863 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5865 /* Models with Intel Extreme Graphics 2 */
5866 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
5867 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5868 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5870 /* Models with Intel GMA900 */
5871 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5872 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5873 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5876 static int __init brightness_init(struct ibm_init_struct *iibm)
5878 int b;
5879 unsigned long quirks;
5881 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5883 mutex_init(&brightness_mutex);
5885 quirks = tpacpi_check_quirks(brightness_quirk_table,
5886 ARRAY_SIZE(brightness_quirk_table));
5889 * We always attempt to detect acpi support, so as to switch
5890 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5891 * going to publish a backlight interface
5893 b = tpacpi_check_std_acpi_brightness_support();
5894 if (b > 0) {
5896 if (acpi_video_backlight_support()) {
5897 if (brightness_enable > 1) {
5898 printk(TPACPI_NOTICE
5899 "Standard ACPI backlight interface "
5900 "available, not loading native one.\n");
5901 return 1;
5902 } else if (brightness_enable == 1) {
5903 printk(TPACPI_NOTICE
5904 "Backlight control force enabled, even if standard "
5905 "ACPI backlight interface is available\n");
5907 } else {
5908 if (brightness_enable > 1) {
5909 printk(TPACPI_NOTICE
5910 "Standard ACPI backlight interface not "
5911 "available, thinkpad_acpi native "
5912 "brightness control enabled\n");
5917 if (!brightness_enable) {
5918 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5919 "brightness support disabled by "
5920 "module parameter\n");
5921 return 1;
5924 if (b > 16) {
5925 printk(TPACPI_ERR
5926 "Unsupported brightness interface, "
5927 "please contact %s\n", TPACPI_MAIL);
5928 return 1;
5930 if (b == 16)
5931 tp_features.bright_16levels = 1;
5934 * Check for module parameter bogosity, note that we
5935 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5936 * able to detect "unspecified"
5938 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5939 return -EINVAL;
5941 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5942 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5943 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5944 if (quirks & TPACPI_BRGHT_Q_EC)
5945 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5946 else
5947 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5949 dbg_printk(TPACPI_DBG_BRGHT,
5950 "driver auto-selected brightness_mode=%d\n",
5951 brightness_mode);
5954 /* Safety */
5955 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5956 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5957 brightness_mode == TPACPI_BRGHT_MODE_EC))
5958 return -EINVAL;
5960 if (tpacpi_brightness_get_raw(&b) < 0)
5961 return 1;
5963 if (tp_features.bright_16levels)
5964 printk(TPACPI_INFO
5965 "detected a 16-level brightness capable ThinkPad\n");
5967 ibm_backlight_device = backlight_device_register(
5968 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5969 &ibm_backlight_data);
5970 if (IS_ERR(ibm_backlight_device)) {
5971 printk(TPACPI_ERR "Could not register backlight device\n");
5972 return PTR_ERR(ibm_backlight_device);
5974 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5975 "brightness is supported\n");
5977 if (quirks & TPACPI_BRGHT_Q_ASK) {
5978 printk(TPACPI_NOTICE
5979 "brightness: will use unverified default: "
5980 "brightness_mode=%d\n", brightness_mode);
5981 printk(TPACPI_NOTICE
5982 "brightness: please report to %s whether it works well "
5983 "or not on your ThinkPad\n", TPACPI_MAIL);
5986 ibm_backlight_device->props.max_brightness =
5987 (tp_features.bright_16levels)? 15 : 7;
5988 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5989 backlight_update_status(ibm_backlight_device);
5991 return 0;
5994 static void brightness_suspend(pm_message_t state)
5996 tpacpi_brightness_checkpoint_nvram();
5999 static void brightness_shutdown(void)
6001 tpacpi_brightness_checkpoint_nvram();
6004 static void brightness_exit(void)
6006 if (ibm_backlight_device) {
6007 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6008 "calling backlight_device_unregister()\n");
6009 backlight_device_unregister(ibm_backlight_device);
6012 tpacpi_brightness_checkpoint_nvram();
6015 static int brightness_read(char *p)
6017 int len = 0;
6018 int level;
6020 level = brightness_get(NULL);
6021 if (level < 0) {
6022 len += sprintf(p + len, "level:\t\tunreadable\n");
6023 } else {
6024 len += sprintf(p + len, "level:\t\t%d\n", level);
6025 len += sprintf(p + len, "commands:\tup, down\n");
6026 len += sprintf(p + len, "commands:\tlevel <level>"
6027 " (<level> is 0-%d)\n",
6028 (tp_features.bright_16levels) ? 15 : 7);
6031 return len;
6034 static int brightness_write(char *buf)
6036 int level;
6037 int rc;
6038 char *cmd;
6039 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6041 level = brightness_get(NULL);
6042 if (level < 0)
6043 return level;
6045 while ((cmd = next_cmd(&buf))) {
6046 if (strlencmp(cmd, "up") == 0) {
6047 if (level < max_level)
6048 level++;
6049 } else if (strlencmp(cmd, "down") == 0) {
6050 if (level > 0)
6051 level--;
6052 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6053 level >= 0 && level <= max_level) {
6054 /* new level set */
6055 } else
6056 return -EINVAL;
6059 tpacpi_disclose_usertask("procfs brightness",
6060 "set level to %d\n", level);
6063 * Now we know what the final level should be, so we try to set it.
6064 * Doing it this way makes the syscall restartable in case of EINTR
6066 rc = brightness_set(level);
6067 return (rc == -EINTR)? ERESTARTSYS : rc;
6070 static struct ibm_struct brightness_driver_data = {
6071 .name = "brightness",
6072 .read = brightness_read,
6073 .write = brightness_write,
6074 .exit = brightness_exit,
6075 .suspend = brightness_suspend,
6076 .shutdown = brightness_shutdown,
6079 /*************************************************************************
6080 * Volume subdriver
6083 static int volume_offset = 0x30;
6085 static int volume_read(char *p)
6087 int len = 0;
6088 u8 level;
6090 if (!acpi_ec_read(volume_offset, &level)) {
6091 len += sprintf(p + len, "level:\t\tunreadable\n");
6092 } else {
6093 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6094 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6095 len += sprintf(p + len, "commands:\tup, down, mute\n");
6096 len += sprintf(p + len, "commands:\tlevel <level>"
6097 " (<level> is 0-15)\n");
6100 return len;
6103 static int volume_write(char *buf)
6105 int cmos_cmd, inc, i;
6106 u8 level, mute;
6107 int new_level, new_mute;
6108 char *cmd;
6110 while ((cmd = next_cmd(&buf))) {
6111 if (!acpi_ec_read(volume_offset, &level))
6112 return -EIO;
6113 new_mute = mute = level & 0x40;
6114 new_level = level = level & 0xf;
6116 if (strlencmp(cmd, "up") == 0) {
6117 if (mute)
6118 new_mute = 0;
6119 else
6120 new_level = level == 15 ? 15 : level + 1;
6121 } else if (strlencmp(cmd, "down") == 0) {
6122 if (mute)
6123 new_mute = 0;
6124 else
6125 new_level = level == 0 ? 0 : level - 1;
6126 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6127 new_level >= 0 && new_level <= 15) {
6128 /* new_level set */
6129 } else if (strlencmp(cmd, "mute") == 0) {
6130 new_mute = 0x40;
6131 } else
6132 return -EINVAL;
6134 if (new_level != level) {
6135 /* mute doesn't change */
6137 cmos_cmd = (new_level > level) ?
6138 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6139 inc = new_level > level ? 1 : -1;
6141 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6142 !acpi_ec_write(volume_offset, level)))
6143 return -EIO;
6145 for (i = level; i != new_level; i += inc)
6146 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6147 !acpi_ec_write(volume_offset, i + inc))
6148 return -EIO;
6150 if (mute &&
6151 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6152 !acpi_ec_write(volume_offset, new_level + mute))) {
6153 return -EIO;
6157 if (new_mute != mute) {
6158 /* level doesn't change */
6160 cmos_cmd = (new_mute) ?
6161 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6163 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6164 !acpi_ec_write(volume_offset, level + new_mute))
6165 return -EIO;
6169 return 0;
6172 static struct ibm_struct volume_driver_data = {
6173 .name = "volume",
6174 .read = volume_read,
6175 .write = volume_write,
6178 /*************************************************************************
6179 * Fan subdriver
6183 * FAN ACCESS MODES
6185 * TPACPI_FAN_RD_ACPI_GFAN:
6186 * ACPI GFAN method: returns fan level
6188 * see TPACPI_FAN_WR_ACPI_SFAN
6189 * EC 0x2f (HFSP) not available if GFAN exists
6191 * TPACPI_FAN_WR_ACPI_SFAN:
6192 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6194 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6195 * it for writing.
6197 * TPACPI_FAN_WR_TPEC:
6198 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6199 * Supported on almost all ThinkPads
6201 * Fan speed changes of any sort (including those caused by the
6202 * disengaged mode) are usually done slowly by the firmware as the
6203 * maximum ammount of fan duty cycle change per second seems to be
6204 * limited.
6206 * Reading is not available if GFAN exists.
6207 * Writing is not available if SFAN exists.
6209 * Bits
6210 * 7 automatic mode engaged;
6211 * (default operation mode of the ThinkPad)
6212 * fan level is ignored in this mode.
6213 * 6 full speed mode (takes precedence over bit 7);
6214 * not available on all thinkpads. May disable
6215 * the tachometer while the fan controller ramps up
6216 * the speed (which can take up to a few *minutes*).
6217 * Speeds up fan to 100% duty-cycle, which is far above
6218 * the standard RPM levels. It is not impossible that
6219 * it could cause hardware damage.
6220 * 5-3 unused in some models. Extra bits for fan level
6221 * in others, but still useless as all values above
6222 * 7 map to the same speed as level 7 in these models.
6223 * 2-0 fan level (0..7 usually)
6224 * 0x00 = stop
6225 * 0x07 = max (set when temperatures critical)
6226 * Some ThinkPads may have other levels, see
6227 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6229 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6230 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6231 * does so, its initial value is meaningless (0x07).
6233 * For firmware bugs, refer to:
6234 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6236 * ----
6238 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6239 * Main fan tachometer reading (in RPM)
6241 * This register is present on all ThinkPads with a new-style EC, and
6242 * it is known not to be present on the A21m/e, and T22, as there is
6243 * something else in offset 0x84 according to the ACPI DSDT. Other
6244 * ThinkPads from this same time period (and earlier) probably lack the
6245 * tachometer as well.
6247 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6248 * was never fixed by IBM to report the EC firmware version string
6249 * probably support the tachometer (like the early X models), so
6250 * detecting it is quite hard. We need more data to know for sure.
6252 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6253 * might result.
6255 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6256 * mode.
6258 * For firmware bugs, refer to:
6259 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6261 * ----
6263 * ThinkPad EC register 0x31 bit 0 (only on select models)
6265 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6266 * show the speed of the main fan. When bit 0 of EC register 0x31
6267 * is one, the tachometer registers show the speed of the auxiliary
6268 * fan.
6270 * Fan control seems to affect both fans, regardless of the state
6271 * of this bit.
6273 * So far, only the firmware for the X60/X61 non-tablet versions
6274 * seem to support this (firmware TP-7M).
6276 * TPACPI_FAN_WR_ACPI_FANS:
6277 * ThinkPad X31, X40, X41. Not available in the X60.
6279 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6280 * high speed. ACPI DSDT seems to map these three speeds to levels
6281 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6282 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6284 * The speeds are stored on handles
6285 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6287 * There are three default speed sets, acessible as handles:
6288 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6290 * ACPI DSDT switches which set is in use depending on various
6291 * factors.
6293 * TPACPI_FAN_WR_TPEC is also available and should be used to
6294 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6295 * but the ACPI tables just mention level 7.
6298 enum { /* Fan control constants */
6299 fan_status_offset = 0x2f, /* EC register 0x2f */
6300 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6301 * 0x84 must be read before 0x85 */
6302 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6303 bit 0 selects which fan is active */
6305 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6306 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6308 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6311 enum fan_status_access_mode {
6312 TPACPI_FAN_NONE = 0, /* No fan status or control */
6313 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6314 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6317 enum fan_control_access_mode {
6318 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6319 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6320 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6321 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6324 enum fan_control_commands {
6325 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6326 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6327 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6328 * and also watchdog cmd */
6331 static int fan_control_allowed;
6333 static enum fan_status_access_mode fan_status_access_mode;
6334 static enum fan_control_access_mode fan_control_access_mode;
6335 static enum fan_control_commands fan_control_commands;
6337 static u8 fan_control_initial_status;
6338 static u8 fan_control_desired_level;
6339 static u8 fan_control_resume_level;
6340 static int fan_watchdog_maxinterval;
6342 static struct mutex fan_mutex;
6344 static void fan_watchdog_fire(struct work_struct *ignored);
6345 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6347 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6348 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6349 "\\FSPD", /* 600e/x, 770e, 770x */
6350 ); /* all others */
6351 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6352 "JFNS", /* 770x-JL */
6353 ); /* all others */
6356 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6357 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6358 * be in auto mode (0x80).
6360 * This is corrected by any write to HFSP either by the driver, or
6361 * by the firmware.
6363 * We assume 0x07 really means auto mode while this quirk is active,
6364 * as this is far more likely than the ThinkPad being in level 7,
6365 * which is only used by the firmware during thermal emergencies.
6367 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6368 * TP-70 (T43, R52), which are known to be buggy.
6371 static void fan_quirk1_setup(void)
6373 if (fan_control_initial_status == 0x07) {
6374 printk(TPACPI_NOTICE
6375 "fan_init: initial fan status is unknown, "
6376 "assuming it is in auto mode\n");
6377 tp_features.fan_ctrl_status_undef = 1;
6381 static void fan_quirk1_handle(u8 *fan_status)
6383 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6384 if (*fan_status != fan_control_initial_status) {
6385 /* something changed the HFSP regisnter since
6386 * driver init time, so it is not undefined
6387 * anymore */
6388 tp_features.fan_ctrl_status_undef = 0;
6389 } else {
6390 /* Return most likely status. In fact, it
6391 * might be the only possible status */
6392 *fan_status = TP_EC_FAN_AUTO;
6397 /* Select main fan on X60/X61, NOOP on others */
6398 static bool fan_select_fan1(void)
6400 if (tp_features.second_fan) {
6401 u8 val;
6403 if (ec_read(fan_select_offset, &val) < 0)
6404 return false;
6405 val &= 0xFEU;
6406 if (ec_write(fan_select_offset, val) < 0)
6407 return false;
6409 return true;
6412 /* Select secondary fan on X60/X61 */
6413 static bool fan_select_fan2(void)
6415 u8 val;
6417 if (!tp_features.second_fan)
6418 return false;
6420 if (ec_read(fan_select_offset, &val) < 0)
6421 return false;
6422 val |= 0x01U;
6423 if (ec_write(fan_select_offset, val) < 0)
6424 return false;
6426 return true;
6430 * Call with fan_mutex held
6432 static void fan_update_desired_level(u8 status)
6434 if ((status &
6435 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6436 if (status > 7)
6437 fan_control_desired_level = 7;
6438 else
6439 fan_control_desired_level = status;
6443 static int fan_get_status(u8 *status)
6445 u8 s;
6447 /* TODO:
6448 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6450 switch (fan_status_access_mode) {
6451 case TPACPI_FAN_RD_ACPI_GFAN:
6452 /* 570, 600e/x, 770e, 770x */
6454 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6455 return -EIO;
6457 if (likely(status))
6458 *status = s & 0x07;
6460 break;
6462 case TPACPI_FAN_RD_TPEC:
6463 /* all except 570, 600e/x, 770e, 770x */
6464 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6465 return -EIO;
6467 if (likely(status)) {
6468 *status = s;
6469 fan_quirk1_handle(status);
6472 break;
6474 default:
6475 return -ENXIO;
6478 return 0;
6481 static int fan_get_status_safe(u8 *status)
6483 int rc;
6484 u8 s;
6486 if (mutex_lock_killable(&fan_mutex))
6487 return -ERESTARTSYS;
6488 rc = fan_get_status(&s);
6489 if (!rc)
6490 fan_update_desired_level(s);
6491 mutex_unlock(&fan_mutex);
6493 if (status)
6494 *status = s;
6496 return rc;
6499 static int fan_get_speed(unsigned int *speed)
6501 u8 hi, lo;
6503 switch (fan_status_access_mode) {
6504 case TPACPI_FAN_RD_TPEC:
6505 /* all except 570, 600e/x, 770e, 770x */
6506 if (unlikely(!fan_select_fan1()))
6507 return -EIO;
6508 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6509 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6510 return -EIO;
6512 if (likely(speed))
6513 *speed = (hi << 8) | lo;
6515 break;
6517 default:
6518 return -ENXIO;
6521 return 0;
6524 static int fan2_get_speed(unsigned int *speed)
6526 u8 hi, lo;
6527 bool rc;
6529 switch (fan_status_access_mode) {
6530 case TPACPI_FAN_RD_TPEC:
6531 /* all except 570, 600e/x, 770e, 770x */
6532 if (unlikely(!fan_select_fan2()))
6533 return -EIO;
6534 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6535 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6536 fan_select_fan1(); /* play it safe */
6537 if (rc)
6538 return -EIO;
6540 if (likely(speed))
6541 *speed = (hi << 8) | lo;
6543 break;
6545 default:
6546 return -ENXIO;
6549 return 0;
6552 static int fan_set_level(int level)
6554 if (!fan_control_allowed)
6555 return -EPERM;
6557 switch (fan_control_access_mode) {
6558 case TPACPI_FAN_WR_ACPI_SFAN:
6559 if (level >= 0 && level <= 7) {
6560 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6561 return -EIO;
6562 } else
6563 return -EINVAL;
6564 break;
6566 case TPACPI_FAN_WR_ACPI_FANS:
6567 case TPACPI_FAN_WR_TPEC:
6568 if (!(level & TP_EC_FAN_AUTO) &&
6569 !(level & TP_EC_FAN_FULLSPEED) &&
6570 ((level < 0) || (level > 7)))
6571 return -EINVAL;
6573 /* safety net should the EC not support AUTO
6574 * or FULLSPEED mode bits and just ignore them */
6575 if (level & TP_EC_FAN_FULLSPEED)
6576 level |= 7; /* safety min speed 7 */
6577 else if (level & TP_EC_FAN_AUTO)
6578 level |= 4; /* safety min speed 4 */
6580 if (!acpi_ec_write(fan_status_offset, level))
6581 return -EIO;
6582 else
6583 tp_features.fan_ctrl_status_undef = 0;
6584 break;
6586 default:
6587 return -ENXIO;
6590 vdbg_printk(TPACPI_DBG_FAN,
6591 "fan control: set fan control register to 0x%02x\n", level);
6592 return 0;
6595 static int fan_set_level_safe(int level)
6597 int rc;
6599 if (!fan_control_allowed)
6600 return -EPERM;
6602 if (mutex_lock_killable(&fan_mutex))
6603 return -ERESTARTSYS;
6605 if (level == TPACPI_FAN_LAST_LEVEL)
6606 level = fan_control_desired_level;
6608 rc = fan_set_level(level);
6609 if (!rc)
6610 fan_update_desired_level(level);
6612 mutex_unlock(&fan_mutex);
6613 return rc;
6616 static int fan_set_enable(void)
6618 u8 s;
6619 int rc;
6621 if (!fan_control_allowed)
6622 return -EPERM;
6624 if (mutex_lock_killable(&fan_mutex))
6625 return -ERESTARTSYS;
6627 switch (fan_control_access_mode) {
6628 case TPACPI_FAN_WR_ACPI_FANS:
6629 case TPACPI_FAN_WR_TPEC:
6630 rc = fan_get_status(&s);
6631 if (rc < 0)
6632 break;
6634 /* Don't go out of emergency fan mode */
6635 if (s != 7) {
6636 s &= 0x07;
6637 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6640 if (!acpi_ec_write(fan_status_offset, s))
6641 rc = -EIO;
6642 else {
6643 tp_features.fan_ctrl_status_undef = 0;
6644 rc = 0;
6646 break;
6648 case TPACPI_FAN_WR_ACPI_SFAN:
6649 rc = fan_get_status(&s);
6650 if (rc < 0)
6651 break;
6653 s &= 0x07;
6655 /* Set fan to at least level 4 */
6656 s |= 4;
6658 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6659 rc = -EIO;
6660 else
6661 rc = 0;
6662 break;
6664 default:
6665 rc = -ENXIO;
6668 mutex_unlock(&fan_mutex);
6670 if (!rc)
6671 vdbg_printk(TPACPI_DBG_FAN,
6672 "fan control: set fan control register to 0x%02x\n",
6674 return rc;
6677 static int fan_set_disable(void)
6679 int rc;
6681 if (!fan_control_allowed)
6682 return -EPERM;
6684 if (mutex_lock_killable(&fan_mutex))
6685 return -ERESTARTSYS;
6687 rc = 0;
6688 switch (fan_control_access_mode) {
6689 case TPACPI_FAN_WR_ACPI_FANS:
6690 case TPACPI_FAN_WR_TPEC:
6691 if (!acpi_ec_write(fan_status_offset, 0x00))
6692 rc = -EIO;
6693 else {
6694 fan_control_desired_level = 0;
6695 tp_features.fan_ctrl_status_undef = 0;
6697 break;
6699 case TPACPI_FAN_WR_ACPI_SFAN:
6700 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6701 rc = -EIO;
6702 else
6703 fan_control_desired_level = 0;
6704 break;
6706 default:
6707 rc = -ENXIO;
6710 if (!rc)
6711 vdbg_printk(TPACPI_DBG_FAN,
6712 "fan control: set fan control register to 0\n");
6714 mutex_unlock(&fan_mutex);
6715 return rc;
6718 static int fan_set_speed(int speed)
6720 int rc;
6722 if (!fan_control_allowed)
6723 return -EPERM;
6725 if (mutex_lock_killable(&fan_mutex))
6726 return -ERESTARTSYS;
6728 rc = 0;
6729 switch (fan_control_access_mode) {
6730 case TPACPI_FAN_WR_ACPI_FANS:
6731 if (speed >= 0 && speed <= 65535) {
6732 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6733 speed, speed, speed))
6734 rc = -EIO;
6735 } else
6736 rc = -EINVAL;
6737 break;
6739 default:
6740 rc = -ENXIO;
6743 mutex_unlock(&fan_mutex);
6744 return rc;
6747 static void fan_watchdog_reset(void)
6749 static int fan_watchdog_active;
6751 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6752 return;
6754 if (fan_watchdog_active)
6755 cancel_delayed_work(&fan_watchdog_task);
6757 if (fan_watchdog_maxinterval > 0 &&
6758 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6759 fan_watchdog_active = 1;
6760 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6761 msecs_to_jiffies(fan_watchdog_maxinterval
6762 * 1000))) {
6763 printk(TPACPI_ERR
6764 "failed to queue the fan watchdog, "
6765 "watchdog will not trigger\n");
6767 } else
6768 fan_watchdog_active = 0;
6771 static void fan_watchdog_fire(struct work_struct *ignored)
6773 int rc;
6775 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6776 return;
6778 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6779 rc = fan_set_enable();
6780 if (rc < 0) {
6781 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6782 "will try again later...\n", -rc);
6783 /* reschedule for later */
6784 fan_watchdog_reset();
6789 * SYSFS fan layout: hwmon compatible (device)
6791 * pwm*_enable:
6792 * 0: "disengaged" mode
6793 * 1: manual mode
6794 * 2: native EC "auto" mode (recommended, hardware default)
6796 * pwm*: set speed in manual mode, ignored otherwise.
6797 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6798 * interpolation.
6800 * fan*_input: tachometer reading, RPM
6803 * SYSFS fan layout: extensions
6805 * fan_watchdog (driver):
6806 * fan watchdog interval in seconds, 0 disables (default), max 120
6809 /* sysfs fan pwm1_enable ----------------------------------------------- */
6810 static ssize_t fan_pwm1_enable_show(struct device *dev,
6811 struct device_attribute *attr,
6812 char *buf)
6814 int res, mode;
6815 u8 status;
6817 res = fan_get_status_safe(&status);
6818 if (res)
6819 return res;
6821 if (status & TP_EC_FAN_FULLSPEED) {
6822 mode = 0;
6823 } else if (status & TP_EC_FAN_AUTO) {
6824 mode = 2;
6825 } else
6826 mode = 1;
6828 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6831 static ssize_t fan_pwm1_enable_store(struct device *dev,
6832 struct device_attribute *attr,
6833 const char *buf, size_t count)
6835 unsigned long t;
6836 int res, level;
6838 if (parse_strtoul(buf, 2, &t))
6839 return -EINVAL;
6841 tpacpi_disclose_usertask("hwmon pwm1_enable",
6842 "set fan mode to %lu\n", t);
6844 switch (t) {
6845 case 0:
6846 level = TP_EC_FAN_FULLSPEED;
6847 break;
6848 case 1:
6849 level = TPACPI_FAN_LAST_LEVEL;
6850 break;
6851 case 2:
6852 level = TP_EC_FAN_AUTO;
6853 break;
6854 case 3:
6855 /* reserved for software-controlled auto mode */
6856 return -ENOSYS;
6857 default:
6858 return -EINVAL;
6861 res = fan_set_level_safe(level);
6862 if (res == -ENXIO)
6863 return -EINVAL;
6864 else if (res < 0)
6865 return res;
6867 fan_watchdog_reset();
6869 return count;
6872 static struct device_attribute dev_attr_fan_pwm1_enable =
6873 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6874 fan_pwm1_enable_show, fan_pwm1_enable_store);
6876 /* sysfs fan pwm1 ------------------------------------------------------ */
6877 static ssize_t fan_pwm1_show(struct device *dev,
6878 struct device_attribute *attr,
6879 char *buf)
6881 int res;
6882 u8 status;
6884 res = fan_get_status_safe(&status);
6885 if (res)
6886 return res;
6888 if ((status &
6889 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6890 status = fan_control_desired_level;
6892 if (status > 7)
6893 status = 7;
6895 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6898 static ssize_t fan_pwm1_store(struct device *dev,
6899 struct device_attribute *attr,
6900 const char *buf, size_t count)
6902 unsigned long s;
6903 int rc;
6904 u8 status, newlevel;
6906 if (parse_strtoul(buf, 255, &s))
6907 return -EINVAL;
6909 tpacpi_disclose_usertask("hwmon pwm1",
6910 "set fan speed to %lu\n", s);
6912 /* scale down from 0-255 to 0-7 */
6913 newlevel = (s >> 5) & 0x07;
6915 if (mutex_lock_killable(&fan_mutex))
6916 return -ERESTARTSYS;
6918 rc = fan_get_status(&status);
6919 if (!rc && (status &
6920 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6921 rc = fan_set_level(newlevel);
6922 if (rc == -ENXIO)
6923 rc = -EINVAL;
6924 else if (!rc) {
6925 fan_update_desired_level(newlevel);
6926 fan_watchdog_reset();
6930 mutex_unlock(&fan_mutex);
6931 return (rc)? rc : count;
6934 static struct device_attribute dev_attr_fan_pwm1 =
6935 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6936 fan_pwm1_show, fan_pwm1_store);
6938 /* sysfs fan fan1_input ------------------------------------------------ */
6939 static ssize_t fan_fan1_input_show(struct device *dev,
6940 struct device_attribute *attr,
6941 char *buf)
6943 int res;
6944 unsigned int speed;
6946 res = fan_get_speed(&speed);
6947 if (res < 0)
6948 return res;
6950 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6953 static struct device_attribute dev_attr_fan_fan1_input =
6954 __ATTR(fan1_input, S_IRUGO,
6955 fan_fan1_input_show, NULL);
6957 /* sysfs fan fan2_input ------------------------------------------------ */
6958 static ssize_t fan_fan2_input_show(struct device *dev,
6959 struct device_attribute *attr,
6960 char *buf)
6962 int res;
6963 unsigned int speed;
6965 res = fan2_get_speed(&speed);
6966 if (res < 0)
6967 return res;
6969 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6972 static struct device_attribute dev_attr_fan_fan2_input =
6973 __ATTR(fan2_input, S_IRUGO,
6974 fan_fan2_input_show, NULL);
6976 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6977 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6978 char *buf)
6980 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6983 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6984 const char *buf, size_t count)
6986 unsigned long t;
6988 if (parse_strtoul(buf, 120, &t))
6989 return -EINVAL;
6991 if (!fan_control_allowed)
6992 return -EPERM;
6994 fan_watchdog_maxinterval = t;
6995 fan_watchdog_reset();
6997 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6999 return count;
7002 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7003 fan_fan_watchdog_show, fan_fan_watchdog_store);
7005 /* --------------------------------------------------------------------- */
7006 static struct attribute *fan_attributes[] = {
7007 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7008 &dev_attr_fan_fan1_input.attr,
7009 NULL, /* for fan2_input */
7010 NULL
7013 static const struct attribute_group fan_attr_group = {
7014 .attrs = fan_attributes,
7017 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7018 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7020 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7021 { .vendor = PCI_VENDOR_ID_IBM, \
7022 .bios = TPACPI_MATCH_ANY, \
7023 .ec = TPID(__id1, __id2), \
7024 .quirks = __quirks }
7026 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7027 { .vendor = PCI_VENDOR_ID_LENOVO, \
7028 .bios = TPACPI_MATCH_ANY, \
7029 .ec = TPID(__id1, __id2), \
7030 .quirks = __quirks }
7032 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7033 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7034 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7035 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7036 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7037 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7040 #undef TPACPI_FAN_QL
7041 #undef TPACPI_FAN_QI
7043 static int __init fan_init(struct ibm_init_struct *iibm)
7045 int rc;
7046 unsigned long quirks;
7048 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7049 "initializing fan subdriver\n");
7051 mutex_init(&fan_mutex);
7052 fan_status_access_mode = TPACPI_FAN_NONE;
7053 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7054 fan_control_commands = 0;
7055 fan_watchdog_maxinterval = 0;
7056 tp_features.fan_ctrl_status_undef = 0;
7057 tp_features.second_fan = 0;
7058 fan_control_desired_level = 7;
7060 TPACPI_ACPIHANDLE_INIT(fans);
7061 TPACPI_ACPIHANDLE_INIT(gfan);
7062 TPACPI_ACPIHANDLE_INIT(sfan);
7064 quirks = tpacpi_check_quirks(fan_quirk_table,
7065 ARRAY_SIZE(fan_quirk_table));
7067 if (gfan_handle) {
7068 /* 570, 600e/x, 770e, 770x */
7069 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7070 } else {
7071 /* all other ThinkPads: note that even old-style
7072 * ThinkPad ECs supports the fan control register */
7073 if (likely(acpi_ec_read(fan_status_offset,
7074 &fan_control_initial_status))) {
7075 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7076 if (quirks & TPACPI_FAN_Q1)
7077 fan_quirk1_setup();
7078 if (quirks & TPACPI_FAN_2FAN) {
7079 tp_features.second_fan = 1;
7080 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7081 "secondary fan support enabled\n");
7083 } else {
7084 printk(TPACPI_ERR
7085 "ThinkPad ACPI EC access misbehaving, "
7086 "fan status and control unavailable\n");
7087 return 1;
7091 if (sfan_handle) {
7092 /* 570, 770x-JL */
7093 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7094 fan_control_commands |=
7095 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7096 } else {
7097 if (!gfan_handle) {
7098 /* gfan without sfan means no fan control */
7099 /* all other models implement TP EC 0x2f control */
7101 if (fans_handle) {
7102 /* X31, X40, X41 */
7103 fan_control_access_mode =
7104 TPACPI_FAN_WR_ACPI_FANS;
7105 fan_control_commands |=
7106 TPACPI_FAN_CMD_SPEED |
7107 TPACPI_FAN_CMD_LEVEL |
7108 TPACPI_FAN_CMD_ENABLE;
7109 } else {
7110 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7111 fan_control_commands |=
7112 TPACPI_FAN_CMD_LEVEL |
7113 TPACPI_FAN_CMD_ENABLE;
7118 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7119 "fan is %s, modes %d, %d\n",
7120 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7121 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7122 fan_status_access_mode, fan_control_access_mode);
7124 /* fan control master switch */
7125 if (!fan_control_allowed) {
7126 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7127 fan_control_commands = 0;
7128 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7129 "fan control features disabled by parameter\n");
7132 /* update fan_control_desired_level */
7133 if (fan_status_access_mode != TPACPI_FAN_NONE)
7134 fan_get_status_safe(NULL);
7136 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7137 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7138 if (tp_features.second_fan) {
7139 /* attach second fan tachometer */
7140 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7141 &dev_attr_fan_fan2_input.attr;
7143 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7144 &fan_attr_group);
7145 if (rc < 0)
7146 return rc;
7148 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7149 &driver_attr_fan_watchdog);
7150 if (rc < 0) {
7151 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7152 &fan_attr_group);
7153 return rc;
7155 return 0;
7156 } else
7157 return 1;
7160 static void fan_exit(void)
7162 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7163 "cancelling any pending fan watchdog tasks\n");
7165 /* FIXME: can we really do this unconditionally? */
7166 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7167 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7168 &driver_attr_fan_watchdog);
7170 cancel_delayed_work(&fan_watchdog_task);
7171 flush_workqueue(tpacpi_wq);
7174 static void fan_suspend(pm_message_t state)
7176 int rc;
7178 if (!fan_control_allowed)
7179 return;
7181 /* Store fan status in cache */
7182 fan_control_resume_level = 0;
7183 rc = fan_get_status_safe(&fan_control_resume_level);
7184 if (rc < 0)
7185 printk(TPACPI_NOTICE
7186 "failed to read fan level for later "
7187 "restore during resume: %d\n", rc);
7189 /* if it is undefined, don't attempt to restore it.
7190 * KEEP THIS LAST */
7191 if (tp_features.fan_ctrl_status_undef)
7192 fan_control_resume_level = 0;
7195 static void fan_resume(void)
7197 u8 current_level = 7;
7198 bool do_set = false;
7199 int rc;
7201 /* DSDT *always* updates status on resume */
7202 tp_features.fan_ctrl_status_undef = 0;
7204 if (!fan_control_allowed ||
7205 !fan_control_resume_level ||
7206 (fan_get_status_safe(&current_level) < 0))
7207 return;
7209 switch (fan_control_access_mode) {
7210 case TPACPI_FAN_WR_ACPI_SFAN:
7211 /* never decrease fan level */
7212 do_set = (fan_control_resume_level > current_level);
7213 break;
7214 case TPACPI_FAN_WR_ACPI_FANS:
7215 case TPACPI_FAN_WR_TPEC:
7216 /* never decrease fan level, scale is:
7217 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7219 * We expect the firmware to set either 7 or AUTO, but we
7220 * handle FULLSPEED out of paranoia.
7222 * So, we can safely only restore FULLSPEED or 7, anything
7223 * else could slow the fan. Restoring AUTO is useless, at
7224 * best that's exactly what the DSDT already set (it is the
7225 * slower it uses).
7227 * Always keep in mind that the DSDT *will* have set the
7228 * fans to what the vendor supposes is the best level. We
7229 * muck with it only to speed the fan up.
7231 if (fan_control_resume_level != 7 &&
7232 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7233 return;
7234 else
7235 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7236 (current_level != fan_control_resume_level);
7237 break;
7238 default:
7239 return;
7241 if (do_set) {
7242 printk(TPACPI_NOTICE
7243 "restoring fan level to 0x%02x\n",
7244 fan_control_resume_level);
7245 rc = fan_set_level_safe(fan_control_resume_level);
7246 if (rc < 0)
7247 printk(TPACPI_NOTICE
7248 "failed to restore fan level: %d\n", rc);
7252 static int fan_read(char *p)
7254 int len = 0;
7255 int rc;
7256 u8 status;
7257 unsigned int speed = 0;
7259 switch (fan_status_access_mode) {
7260 case TPACPI_FAN_RD_ACPI_GFAN:
7261 /* 570, 600e/x, 770e, 770x */
7262 rc = fan_get_status_safe(&status);
7263 if (rc < 0)
7264 return rc;
7266 len += sprintf(p + len, "status:\t\t%s\n"
7267 "level:\t\t%d\n",
7268 (status != 0) ? "enabled" : "disabled", status);
7269 break;
7271 case TPACPI_FAN_RD_TPEC:
7272 /* all except 570, 600e/x, 770e, 770x */
7273 rc = fan_get_status_safe(&status);
7274 if (rc < 0)
7275 return rc;
7277 len += sprintf(p + len, "status:\t\t%s\n",
7278 (status != 0) ? "enabled" : "disabled");
7280 rc = fan_get_speed(&speed);
7281 if (rc < 0)
7282 return rc;
7284 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7286 if (status & TP_EC_FAN_FULLSPEED)
7287 /* Disengaged mode takes precedence */
7288 len += sprintf(p + len, "level:\t\tdisengaged\n");
7289 else if (status & TP_EC_FAN_AUTO)
7290 len += sprintf(p + len, "level:\t\tauto\n");
7291 else
7292 len += sprintf(p + len, "level:\t\t%d\n", status);
7293 break;
7295 case TPACPI_FAN_NONE:
7296 default:
7297 len += sprintf(p + len, "status:\t\tnot supported\n");
7300 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7301 len += sprintf(p + len, "commands:\tlevel <level>");
7303 switch (fan_control_access_mode) {
7304 case TPACPI_FAN_WR_ACPI_SFAN:
7305 len += sprintf(p + len, " (<level> is 0-7)\n");
7306 break;
7308 default:
7309 len += sprintf(p + len, " (<level> is 0-7, "
7310 "auto, disengaged, full-speed)\n");
7311 break;
7315 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7316 len += sprintf(p + len, "commands:\tenable, disable\n"
7317 "commands:\twatchdog <timeout> (<timeout> "
7318 "is 0 (off), 1-120 (seconds))\n");
7320 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7321 len += sprintf(p + len, "commands:\tspeed <speed>"
7322 " (<speed> is 0-65535)\n");
7324 return len;
7327 static int fan_write_cmd_level(const char *cmd, int *rc)
7329 int level;
7331 if (strlencmp(cmd, "level auto") == 0)
7332 level = TP_EC_FAN_AUTO;
7333 else if ((strlencmp(cmd, "level disengaged") == 0) |
7334 (strlencmp(cmd, "level full-speed") == 0))
7335 level = TP_EC_FAN_FULLSPEED;
7336 else if (sscanf(cmd, "level %d", &level) != 1)
7337 return 0;
7339 *rc = fan_set_level_safe(level);
7340 if (*rc == -ENXIO)
7341 printk(TPACPI_ERR "level command accepted for unsupported "
7342 "access mode %d", fan_control_access_mode);
7343 else if (!*rc)
7344 tpacpi_disclose_usertask("procfs fan",
7345 "set level to %d\n", level);
7347 return 1;
7350 static int fan_write_cmd_enable(const char *cmd, int *rc)
7352 if (strlencmp(cmd, "enable") != 0)
7353 return 0;
7355 *rc = fan_set_enable();
7356 if (*rc == -ENXIO)
7357 printk(TPACPI_ERR "enable command accepted for unsupported "
7358 "access mode %d", fan_control_access_mode);
7359 else if (!*rc)
7360 tpacpi_disclose_usertask("procfs fan", "enable\n");
7362 return 1;
7365 static int fan_write_cmd_disable(const char *cmd, int *rc)
7367 if (strlencmp(cmd, "disable") != 0)
7368 return 0;
7370 *rc = fan_set_disable();
7371 if (*rc == -ENXIO)
7372 printk(TPACPI_ERR "disable command accepted for unsupported "
7373 "access mode %d", fan_control_access_mode);
7374 else if (!*rc)
7375 tpacpi_disclose_usertask("procfs fan", "disable\n");
7377 return 1;
7380 static int fan_write_cmd_speed(const char *cmd, int *rc)
7382 int speed;
7384 /* TODO:
7385 * Support speed <low> <medium> <high> ? */
7387 if (sscanf(cmd, "speed %d", &speed) != 1)
7388 return 0;
7390 *rc = fan_set_speed(speed);
7391 if (*rc == -ENXIO)
7392 printk(TPACPI_ERR "speed command accepted for unsupported "
7393 "access mode %d", fan_control_access_mode);
7394 else if (!*rc)
7395 tpacpi_disclose_usertask("procfs fan",
7396 "set speed to %d\n", speed);
7398 return 1;
7401 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7403 int interval;
7405 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7406 return 0;
7408 if (interval < 0 || interval > 120)
7409 *rc = -EINVAL;
7410 else {
7411 fan_watchdog_maxinterval = interval;
7412 tpacpi_disclose_usertask("procfs fan",
7413 "set watchdog timer to %d\n",
7414 interval);
7417 return 1;
7420 static int fan_write(char *buf)
7422 char *cmd;
7423 int rc = 0;
7425 while (!rc && (cmd = next_cmd(&buf))) {
7426 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7427 fan_write_cmd_level(cmd, &rc)) &&
7428 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7429 (fan_write_cmd_enable(cmd, &rc) ||
7430 fan_write_cmd_disable(cmd, &rc) ||
7431 fan_write_cmd_watchdog(cmd, &rc))) &&
7432 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7433 fan_write_cmd_speed(cmd, &rc))
7435 rc = -EINVAL;
7436 else if (!rc)
7437 fan_watchdog_reset();
7440 return rc;
7443 static struct ibm_struct fan_driver_data = {
7444 .name = "fan",
7445 .read = fan_read,
7446 .write = fan_write,
7447 .exit = fan_exit,
7448 .suspend = fan_suspend,
7449 .resume = fan_resume,
7452 /****************************************************************************
7453 ****************************************************************************
7455 * Infrastructure
7457 ****************************************************************************
7458 ****************************************************************************/
7460 /* sysfs name ---------------------------------------------------------- */
7461 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7462 struct device_attribute *attr,
7463 char *buf)
7465 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7468 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7469 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7471 /* --------------------------------------------------------------------- */
7473 /* /proc support */
7474 static struct proc_dir_entry *proc_dir;
7477 * Module and infrastructure proble, init and exit handling
7480 static int force_load;
7482 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7483 static const char * __init str_supported(int is_supported)
7485 static char text_unsupported[] __initdata = "not supported";
7487 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7489 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7491 static void ibm_exit(struct ibm_struct *ibm)
7493 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7495 list_del_init(&ibm->all_drivers);
7497 if (ibm->flags.acpi_notify_installed) {
7498 dbg_printk(TPACPI_DBG_EXIT,
7499 "%s: acpi_remove_notify_handler\n", ibm->name);
7500 BUG_ON(!ibm->acpi);
7501 acpi_remove_notify_handler(*ibm->acpi->handle,
7502 ibm->acpi->type,
7503 dispatch_acpi_notify);
7504 ibm->flags.acpi_notify_installed = 0;
7505 ibm->flags.acpi_notify_installed = 0;
7508 if (ibm->flags.proc_created) {
7509 dbg_printk(TPACPI_DBG_EXIT,
7510 "%s: remove_proc_entry\n", ibm->name);
7511 remove_proc_entry(ibm->name, proc_dir);
7512 ibm->flags.proc_created = 0;
7515 if (ibm->flags.acpi_driver_registered) {
7516 dbg_printk(TPACPI_DBG_EXIT,
7517 "%s: acpi_bus_unregister_driver\n", ibm->name);
7518 BUG_ON(!ibm->acpi);
7519 acpi_bus_unregister_driver(ibm->acpi->driver);
7520 kfree(ibm->acpi->driver);
7521 ibm->acpi->driver = NULL;
7522 ibm->flags.acpi_driver_registered = 0;
7525 if (ibm->flags.init_called && ibm->exit) {
7526 ibm->exit();
7527 ibm->flags.init_called = 0;
7530 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7533 static int __init ibm_init(struct ibm_init_struct *iibm)
7535 int ret;
7536 struct ibm_struct *ibm = iibm->data;
7537 struct proc_dir_entry *entry;
7539 BUG_ON(ibm == NULL);
7541 INIT_LIST_HEAD(&ibm->all_drivers);
7543 if (ibm->flags.experimental && !experimental)
7544 return 0;
7546 dbg_printk(TPACPI_DBG_INIT,
7547 "probing for %s\n", ibm->name);
7549 if (iibm->init) {
7550 ret = iibm->init(iibm);
7551 if (ret > 0)
7552 return 0; /* probe failed */
7553 if (ret)
7554 return ret;
7556 ibm->flags.init_called = 1;
7559 if (ibm->acpi) {
7560 if (ibm->acpi->hid) {
7561 ret = register_tpacpi_subdriver(ibm);
7562 if (ret)
7563 goto err_out;
7566 if (ibm->acpi->notify) {
7567 ret = setup_acpi_notify(ibm);
7568 if (ret == -ENODEV) {
7569 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7570 ibm->name);
7571 ret = 0;
7572 goto err_out;
7574 if (ret < 0)
7575 goto err_out;
7579 dbg_printk(TPACPI_DBG_INIT,
7580 "%s installed\n", ibm->name);
7582 if (ibm->read) {
7583 entry = create_proc_entry(ibm->name,
7584 S_IFREG | S_IRUGO | S_IWUSR,
7585 proc_dir);
7586 if (!entry) {
7587 printk(TPACPI_ERR "unable to create proc entry %s\n",
7588 ibm->name);
7589 ret = -ENODEV;
7590 goto err_out;
7592 entry->owner = THIS_MODULE;
7593 entry->data = ibm;
7594 entry->read_proc = &dispatch_procfs_read;
7595 if (ibm->write)
7596 entry->write_proc = &dispatch_procfs_write;
7597 ibm->flags.proc_created = 1;
7600 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7602 return 0;
7604 err_out:
7605 dbg_printk(TPACPI_DBG_INIT,
7606 "%s: at error exit path with result %d\n",
7607 ibm->name, ret);
7609 ibm_exit(ibm);
7610 return (ret < 0)? ret : 0;
7613 /* Probing */
7615 static bool __pure __init tpacpi_is_fw_digit(const char c)
7617 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7620 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7621 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7622 const char t)
7624 return s && strlen(s) >= 8 &&
7625 tpacpi_is_fw_digit(s[0]) &&
7626 tpacpi_is_fw_digit(s[1]) &&
7627 s[2] == t && s[3] == 'T' &&
7628 tpacpi_is_fw_digit(s[4]) &&
7629 tpacpi_is_fw_digit(s[5]) &&
7630 s[6] == 'W' && s[7] == 'W';
7633 /* returns 0 - probe ok, or < 0 - probe error.
7634 * Probe ok doesn't mean thinkpad found.
7635 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7636 static int __must_check __init get_thinkpad_model_data(
7637 struct thinkpad_id_data *tp)
7639 const struct dmi_device *dev = NULL;
7640 char ec_fw_string[18];
7641 char const *s;
7643 if (!tp)
7644 return -EINVAL;
7646 memset(tp, 0, sizeof(*tp));
7648 if (dmi_name_in_vendors("IBM"))
7649 tp->vendor = PCI_VENDOR_ID_IBM;
7650 else if (dmi_name_in_vendors("LENOVO"))
7651 tp->vendor = PCI_VENDOR_ID_LENOVO;
7652 else
7653 return 0;
7655 s = dmi_get_system_info(DMI_BIOS_VERSION);
7656 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7657 if (s && !tp->bios_version_str)
7658 return -ENOMEM;
7660 /* Really ancient ThinkPad 240X will fail this, which is fine */
7661 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7662 return 0;
7664 tp->bios_model = tp->bios_version_str[0]
7665 | (tp->bios_version_str[1] << 8);
7666 tp->bios_release = (tp->bios_version_str[4] << 8)
7667 | tp->bios_version_str[5];
7670 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7671 * X32 or newer, all Z series; Some models must have an
7672 * up-to-date BIOS or they will not be detected.
7674 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7676 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7677 if (sscanf(dev->name,
7678 "IBM ThinkPad Embedded Controller -[%17c",
7679 ec_fw_string) == 1) {
7680 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7681 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7683 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7684 if (!tp->ec_version_str)
7685 return -ENOMEM;
7687 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7688 tp->ec_model = ec_fw_string[0]
7689 | (ec_fw_string[1] << 8);
7690 tp->ec_release = (ec_fw_string[4] << 8)
7691 | ec_fw_string[5];
7692 } else {
7693 printk(TPACPI_NOTICE
7694 "ThinkPad firmware release %s "
7695 "doesn't match the known patterns\n",
7696 ec_fw_string);
7697 printk(TPACPI_NOTICE
7698 "please report this to %s\n",
7699 TPACPI_MAIL);
7701 break;
7705 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7706 if (s && !strnicmp(s, "ThinkPad", 8)) {
7707 tp->model_str = kstrdup(s, GFP_KERNEL);
7708 if (!tp->model_str)
7709 return -ENOMEM;
7712 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7713 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7714 if (s && !tp->nummodel_str)
7715 return -ENOMEM;
7717 return 0;
7720 static int __init probe_for_thinkpad(void)
7722 int is_thinkpad;
7724 if (acpi_disabled)
7725 return -ENODEV;
7728 * Non-ancient models have better DMI tagging, but very old models
7729 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7731 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7732 (thinkpad_id.ec_model != 0) ||
7733 tpacpi_is_fw_known();
7735 /* ec is required because many other handles are relative to it */
7736 TPACPI_ACPIHANDLE_INIT(ec);
7737 if (!ec_handle) {
7738 if (is_thinkpad)
7739 printk(TPACPI_ERR
7740 "Not yet supported ThinkPad detected!\n");
7741 return -ENODEV;
7744 if (!is_thinkpad && !force_load)
7745 return -ENODEV;
7747 return 0;
7751 /* Module init, exit, parameters */
7753 static struct ibm_init_struct ibms_init[] __initdata = {
7755 .init = thinkpad_acpi_driver_init,
7756 .data = &thinkpad_acpi_driver_data,
7759 .init = hotkey_init,
7760 .data = &hotkey_driver_data,
7763 .init = bluetooth_init,
7764 .data = &bluetooth_driver_data,
7767 .init = wan_init,
7768 .data = &wan_driver_data,
7771 .init = uwb_init,
7772 .data = &uwb_driver_data,
7774 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7776 .init = video_init,
7777 .data = &video_driver_data,
7779 #endif
7781 .init = light_init,
7782 .data = &light_driver_data,
7785 .init = cmos_init,
7786 .data = &cmos_driver_data,
7789 .init = led_init,
7790 .data = &led_driver_data,
7793 .init = beep_init,
7794 .data = &beep_driver_data,
7797 .init = thermal_init,
7798 .data = &thermal_driver_data,
7801 .data = &ecdump_driver_data,
7804 .init = brightness_init,
7805 .data = &brightness_driver_data,
7808 .data = &volume_driver_data,
7811 .init = fan_init,
7812 .data = &fan_driver_data,
7816 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7818 unsigned int i;
7819 struct ibm_struct *ibm;
7821 if (!kp || !kp->name || !val)
7822 return -EINVAL;
7824 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7825 ibm = ibms_init[i].data;
7826 WARN_ON(ibm == NULL);
7828 if (!ibm || !ibm->name)
7829 continue;
7831 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7832 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7833 return -ENOSPC;
7834 strcpy(ibms_init[i].param, val);
7835 strcat(ibms_init[i].param, ",");
7836 return 0;
7840 return -EINVAL;
7843 module_param(experimental, int, 0);
7844 MODULE_PARM_DESC(experimental,
7845 "Enables experimental features when non-zero");
7847 module_param_named(debug, dbg_level, uint, 0);
7848 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7850 module_param(force_load, bool, 0);
7851 MODULE_PARM_DESC(force_load,
7852 "Attempts to load the driver even on a "
7853 "mis-identified ThinkPad when true");
7855 module_param_named(fan_control, fan_control_allowed, bool, 0);
7856 MODULE_PARM_DESC(fan_control,
7857 "Enables setting fan parameters features when true");
7859 module_param_named(brightness_mode, brightness_mode, uint, 0);
7860 MODULE_PARM_DESC(brightness_mode,
7861 "Selects brightness control strategy: "
7862 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7864 module_param(brightness_enable, uint, 0);
7865 MODULE_PARM_DESC(brightness_enable,
7866 "Enables backlight control when 1, disables when 0");
7868 module_param(hotkey_report_mode, uint, 0);
7869 MODULE_PARM_DESC(hotkey_report_mode,
7870 "used for backwards compatibility with userspace, "
7871 "see documentation");
7873 #define TPACPI_PARAM(feature) \
7874 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7875 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7876 "at module load, see documentation")
7878 TPACPI_PARAM(hotkey);
7879 TPACPI_PARAM(bluetooth);
7880 TPACPI_PARAM(video);
7881 TPACPI_PARAM(light);
7882 TPACPI_PARAM(cmos);
7883 TPACPI_PARAM(led);
7884 TPACPI_PARAM(beep);
7885 TPACPI_PARAM(ecdump);
7886 TPACPI_PARAM(brightness);
7887 TPACPI_PARAM(volume);
7888 TPACPI_PARAM(fan);
7890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7891 module_param(dbg_wlswemul, uint, 0);
7892 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7893 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7894 MODULE_PARM_DESC(wlsw_state,
7895 "Initial state of the emulated WLSW switch");
7897 module_param(dbg_bluetoothemul, uint, 0);
7898 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7899 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7900 MODULE_PARM_DESC(bluetooth_state,
7901 "Initial state of the emulated bluetooth switch");
7903 module_param(dbg_wwanemul, uint, 0);
7904 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7905 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7906 MODULE_PARM_DESC(wwan_state,
7907 "Initial state of the emulated WWAN switch");
7909 module_param(dbg_uwbemul, uint, 0);
7910 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7911 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7912 MODULE_PARM_DESC(uwb_state,
7913 "Initial state of the emulated UWB switch");
7914 #endif
7916 static void thinkpad_acpi_module_exit(void)
7918 struct ibm_struct *ibm, *itmp;
7920 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7922 list_for_each_entry_safe_reverse(ibm, itmp,
7923 &tpacpi_all_drivers,
7924 all_drivers) {
7925 ibm_exit(ibm);
7928 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7930 if (tpacpi_inputdev) {
7931 if (tp_features.input_device_registered)
7932 input_unregister_device(tpacpi_inputdev);
7933 else
7934 input_free_device(tpacpi_inputdev);
7937 if (tpacpi_hwmon)
7938 hwmon_device_unregister(tpacpi_hwmon);
7940 if (tp_features.sensors_pdev_attrs_registered)
7941 device_remove_file(&tpacpi_sensors_pdev->dev,
7942 &dev_attr_thinkpad_acpi_pdev_name);
7943 if (tpacpi_sensors_pdev)
7944 platform_device_unregister(tpacpi_sensors_pdev);
7945 if (tpacpi_pdev)
7946 platform_device_unregister(tpacpi_pdev);
7948 if (tp_features.sensors_pdrv_attrs_registered)
7949 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7950 if (tp_features.platform_drv_attrs_registered)
7951 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7953 if (tp_features.sensors_pdrv_registered)
7954 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7956 if (tp_features.platform_drv_registered)
7957 platform_driver_unregister(&tpacpi_pdriver);
7959 if (proc_dir)
7960 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7962 if (tpacpi_wq)
7963 destroy_workqueue(tpacpi_wq);
7965 kfree(thinkpad_id.bios_version_str);
7966 kfree(thinkpad_id.ec_version_str);
7967 kfree(thinkpad_id.model_str);
7971 static int __init thinkpad_acpi_module_init(void)
7973 int ret, i;
7975 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7977 /* Parameter checking */
7978 if (hotkey_report_mode > 2)
7979 return -EINVAL;
7981 /* Driver-level probe */
7983 ret = get_thinkpad_model_data(&thinkpad_id);
7984 if (ret) {
7985 printk(TPACPI_ERR
7986 "unable to get DMI data: %d\n", ret);
7987 thinkpad_acpi_module_exit();
7988 return ret;
7990 ret = probe_for_thinkpad();
7991 if (ret) {
7992 thinkpad_acpi_module_exit();
7993 return ret;
7996 /* Driver initialization */
7998 TPACPI_ACPIHANDLE_INIT(ecrd);
7999 TPACPI_ACPIHANDLE_INIT(ecwr);
8001 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8002 if (!tpacpi_wq) {
8003 thinkpad_acpi_module_exit();
8004 return -ENOMEM;
8007 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8008 if (!proc_dir) {
8009 printk(TPACPI_ERR
8010 "unable to create proc dir " TPACPI_PROC_DIR);
8011 thinkpad_acpi_module_exit();
8012 return -ENODEV;
8014 proc_dir->owner = THIS_MODULE;
8016 ret = platform_driver_register(&tpacpi_pdriver);
8017 if (ret) {
8018 printk(TPACPI_ERR
8019 "unable to register main platform driver\n");
8020 thinkpad_acpi_module_exit();
8021 return ret;
8023 tp_features.platform_drv_registered = 1;
8025 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8026 if (ret) {
8027 printk(TPACPI_ERR
8028 "unable to register hwmon platform driver\n");
8029 thinkpad_acpi_module_exit();
8030 return ret;
8032 tp_features.sensors_pdrv_registered = 1;
8034 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8035 if (!ret) {
8036 tp_features.platform_drv_attrs_registered = 1;
8037 ret = tpacpi_create_driver_attributes(
8038 &tpacpi_hwmon_pdriver.driver);
8040 if (ret) {
8041 printk(TPACPI_ERR
8042 "unable to create sysfs driver attributes\n");
8043 thinkpad_acpi_module_exit();
8044 return ret;
8046 tp_features.sensors_pdrv_attrs_registered = 1;
8049 /* Device initialization */
8050 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8051 NULL, 0);
8052 if (IS_ERR(tpacpi_pdev)) {
8053 ret = PTR_ERR(tpacpi_pdev);
8054 tpacpi_pdev = NULL;
8055 printk(TPACPI_ERR "unable to register platform device\n");
8056 thinkpad_acpi_module_exit();
8057 return ret;
8059 tpacpi_sensors_pdev = platform_device_register_simple(
8060 TPACPI_HWMON_DRVR_NAME,
8061 -1, NULL, 0);
8062 if (IS_ERR(tpacpi_sensors_pdev)) {
8063 ret = PTR_ERR(tpacpi_sensors_pdev);
8064 tpacpi_sensors_pdev = NULL;
8065 printk(TPACPI_ERR
8066 "unable to register hwmon platform device\n");
8067 thinkpad_acpi_module_exit();
8068 return ret;
8070 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8071 &dev_attr_thinkpad_acpi_pdev_name);
8072 if (ret) {
8073 printk(TPACPI_ERR
8074 "unable to create sysfs hwmon device attributes\n");
8075 thinkpad_acpi_module_exit();
8076 return ret;
8078 tp_features.sensors_pdev_attrs_registered = 1;
8079 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8080 if (IS_ERR(tpacpi_hwmon)) {
8081 ret = PTR_ERR(tpacpi_hwmon);
8082 tpacpi_hwmon = NULL;
8083 printk(TPACPI_ERR "unable to register hwmon device\n");
8084 thinkpad_acpi_module_exit();
8085 return ret;
8087 mutex_init(&tpacpi_inputdev_send_mutex);
8088 tpacpi_inputdev = input_allocate_device();
8089 if (!tpacpi_inputdev) {
8090 printk(TPACPI_ERR "unable to allocate input device\n");
8091 thinkpad_acpi_module_exit();
8092 return -ENOMEM;
8093 } else {
8094 /* Prepare input device, but don't register */
8095 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8096 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8097 tpacpi_inputdev->id.bustype = BUS_HOST;
8098 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8099 thinkpad_id.vendor :
8100 PCI_VENDOR_ID_IBM;
8101 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8102 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8104 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8105 ret = ibm_init(&ibms_init[i]);
8106 if (ret >= 0 && *ibms_init[i].param)
8107 ret = ibms_init[i].data->write(ibms_init[i].param);
8108 if (ret < 0) {
8109 thinkpad_acpi_module_exit();
8110 return ret;
8113 ret = input_register_device(tpacpi_inputdev);
8114 if (ret < 0) {
8115 printk(TPACPI_ERR "unable to register input device\n");
8116 thinkpad_acpi_module_exit();
8117 return ret;
8118 } else {
8119 tp_features.input_device_registered = 1;
8122 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8123 return 0;
8126 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8129 * This will autoload the driver in almost every ThinkPad
8130 * in widespread use.
8132 * Only _VERY_ old models, like the 240, 240x and 570 lack
8133 * the HKEY event interface.
8135 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8138 * DMI matching for module autoloading
8140 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8141 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8143 * Only models listed in thinkwiki will be supported, so add yours
8144 * if it is not there yet.
8146 #define IBM_BIOS_MODULE_ALIAS(__type) \
8147 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8149 /* Ancient thinkpad BIOSes have to be identified by
8150 * BIOS type or model number, and there are far less
8151 * BIOS types than model numbers... */
8152 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8154 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8155 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8156 MODULE_DESCRIPTION(TPACPI_DESC);
8157 MODULE_VERSION(TPACPI_VERSION);
8158 MODULE_LICENSE("GPL");
8160 module_init(thinkpad_acpi_module_init);
8161 module_exit(thinkpad_acpi_module_exit);