thinkpad-acpi: firmware version checks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobe7f9f522279c919dae906d480b32949e32c400c9
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 acpi_driver_data(ibm->acpi->device) = 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
1363 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1364 { .vendor = (__v), \
1365 .bios = TPID(__id1, __id2), \
1366 .ec = TPACPI_MATCH_ANY, \
1367 .quirks = TPACPI_MATCH_ANY << 16 \
1368 | (__bv1) << 8 | (__bv2) }
1370 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1371 __eid1, __eid2, __ev1, __ev2) \
1372 { .vendor = (__v), \
1373 .bios = TPID(__bid1, __bid2), \
1374 .ec = TPID(__eid1, __eid2), \
1375 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1376 | (__bv1) << 8 | (__bv2) }
1378 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1379 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1381 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1382 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1383 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1385 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1386 __eid1, __eid2, __ev1, __ev2) \
1387 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1388 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1390 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1391 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1393 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1394 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1395 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1397 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1398 __eid1, __eid2, __ev1, __ev2) \
1399 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1400 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1402 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1403 /* Numeric models ------------------ */
1404 /* FW MODEL BIOS VERS */
1405 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1406 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1407 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1408 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1409 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1410 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1411 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1412 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1413 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1415 /* A-series ------------------------- */
1416 /* FW MODEL BIOS VERS EC VERS */
1417 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1418 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1419 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1420 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1421 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1422 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1423 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1424 TPV_QI0('1', '3', '2', '0'), /* A22m */
1425 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1426 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1427 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1429 /* G-series ------------------------- */
1430 /* FW MODEL BIOS VERS */
1431 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1432 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1434 /* R-series, T-series --------------- */
1435 /* FW MODEL BIOS VERS EC VERS */
1436 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1437 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1438 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1439 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1440 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1441 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1442 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1443 T40/p, T41/p, T42/p (1) */
1444 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1445 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1446 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1447 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1449 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1450 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1451 TPV_QI0('1', '6', '3', '2'), /* T22 */
1452 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1453 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1454 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1456 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1457 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1458 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1460 /* BIOS FW BIOS VERS EC FW EC VERS */
1461 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1462 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1464 /* X-series ------------------------- */
1465 /* FW MODEL BIOS VERS EC VERS */
1466 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1467 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1468 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1469 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1470 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1471 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1472 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1474 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1475 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1477 /* (0) - older versions lack DMI EC fw string and functionality */
1478 /* (1) - older versions known to lack functionality */
1481 #undef TPV_QL1
1482 #undef TPV_QL0
1483 #undef TPV_QI2
1484 #undef TPV_QI1
1485 #undef TPV_QI0
1486 #undef TPV_Q_X
1487 #undef TPV_Q
1489 static void __init tpacpi_check_outdated_fw(void)
1491 unsigned long fwvers;
1492 u16 ec_version, bios_version;
1494 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1495 ARRAY_SIZE(tpacpi_bios_version_qtable));
1497 if (!fwvers)
1498 return;
1500 bios_version = fwvers & 0xffffU;
1501 ec_version = (fwvers >> 16) & 0xffffU;
1503 /* note that unknown versions are set to 0x0000 and we use that */
1504 if ((bios_version > thinkpad_id.bios_release) ||
1505 (ec_version > thinkpad_id.ec_release &&
1506 ec_version != TPACPI_MATCH_ANY)) {
1508 * The changelogs would let us track down the exact
1509 * reason, but it is just too much of a pain to track
1510 * it. We only list BIOSes that are either really
1511 * broken, or really stable to begin with, so it is
1512 * best if the user upgrades the firmware anyway.
1514 printk(TPACPI_WARN
1515 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1516 printk(TPACPI_WARN
1517 "WARNING: This firmware may be missing critical bug "
1518 "fixes and/or important features\n");
1522 /****************************************************************************
1523 ****************************************************************************
1525 * Subdrivers
1527 ****************************************************************************
1528 ****************************************************************************/
1530 /*************************************************************************
1531 * thinkpad-acpi init subdriver
1534 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1536 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1537 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1539 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1540 (thinkpad_id.bios_version_str) ?
1541 thinkpad_id.bios_version_str : "unknown",
1542 (thinkpad_id.ec_version_str) ?
1543 thinkpad_id.ec_version_str : "unknown");
1545 if (thinkpad_id.vendor && thinkpad_id.model_str)
1546 printk(TPACPI_INFO "%s %s, model %s\n",
1547 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1548 "IBM" : ((thinkpad_id.vendor ==
1549 PCI_VENDOR_ID_LENOVO) ?
1550 "Lenovo" : "Unknown vendor"),
1551 thinkpad_id.model_str,
1552 (thinkpad_id.nummodel_str) ?
1553 thinkpad_id.nummodel_str : "unknown");
1555 tpacpi_check_outdated_fw();
1556 return 0;
1559 static int thinkpad_acpi_driver_read(char *p)
1561 int len = 0;
1563 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1564 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1566 return len;
1569 static struct ibm_struct thinkpad_acpi_driver_data = {
1570 .name = "driver",
1571 .read = thinkpad_acpi_driver_read,
1574 /*************************************************************************
1575 * Hotkey subdriver
1578 enum { /* hot key scan codes (derived from ACPI DSDT) */
1579 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1580 TP_ACPI_HOTKEYSCAN_FNF2,
1581 TP_ACPI_HOTKEYSCAN_FNF3,
1582 TP_ACPI_HOTKEYSCAN_FNF4,
1583 TP_ACPI_HOTKEYSCAN_FNF5,
1584 TP_ACPI_HOTKEYSCAN_FNF6,
1585 TP_ACPI_HOTKEYSCAN_FNF7,
1586 TP_ACPI_HOTKEYSCAN_FNF8,
1587 TP_ACPI_HOTKEYSCAN_FNF9,
1588 TP_ACPI_HOTKEYSCAN_FNF10,
1589 TP_ACPI_HOTKEYSCAN_FNF11,
1590 TP_ACPI_HOTKEYSCAN_FNF12,
1591 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1592 TP_ACPI_HOTKEYSCAN_FNINSERT,
1593 TP_ACPI_HOTKEYSCAN_FNDELETE,
1594 TP_ACPI_HOTKEYSCAN_FNHOME,
1595 TP_ACPI_HOTKEYSCAN_FNEND,
1596 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1597 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1598 TP_ACPI_HOTKEYSCAN_FNSPACE,
1599 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1600 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1601 TP_ACPI_HOTKEYSCAN_MUTE,
1602 TP_ACPI_HOTKEYSCAN_THINKPAD,
1605 enum { /* Keys available through NVRAM polling */
1606 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1607 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1610 enum { /* Positions of some of the keys in hotkey masks */
1611 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1612 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1613 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1614 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1615 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1616 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1617 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1618 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1619 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1620 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1621 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1624 enum { /* NVRAM to ACPI HKEY group map */
1625 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1626 TP_ACPI_HKEY_ZOOM_MASK |
1627 TP_ACPI_HKEY_DISPSWTCH_MASK |
1628 TP_ACPI_HKEY_HIBERNATE_MASK,
1629 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1630 TP_ACPI_HKEY_BRGHTDWN_MASK,
1631 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1632 TP_ACPI_HKEY_VOLDWN_MASK |
1633 TP_ACPI_HKEY_MUTE_MASK,
1636 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1637 struct tp_nvram_state {
1638 u16 thinkpad_toggle:1;
1639 u16 zoom_toggle:1;
1640 u16 display_toggle:1;
1641 u16 thinklight_toggle:1;
1642 u16 hibernate_toggle:1;
1643 u16 displayexp_toggle:1;
1644 u16 display_state:1;
1645 u16 brightness_toggle:1;
1646 u16 volume_toggle:1;
1647 u16 mute:1;
1649 u8 brightness_level;
1650 u8 volume_level;
1653 static struct task_struct *tpacpi_hotkey_task;
1654 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1655 static int hotkey_poll_freq = 10; /* Hz */
1656 static struct mutex hotkey_thread_mutex;
1657 static struct mutex hotkey_thread_data_mutex;
1658 static unsigned int hotkey_config_change;
1660 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1662 #define hotkey_source_mask 0U
1664 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1666 static struct mutex hotkey_mutex;
1668 static enum { /* Reasons for waking up */
1669 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1670 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1671 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1672 } hotkey_wakeup_reason;
1674 static int hotkey_autosleep_ack;
1676 static u32 hotkey_orig_mask;
1677 static u32 hotkey_all_mask;
1678 static u32 hotkey_reserved_mask;
1679 static u32 hotkey_mask;
1681 static unsigned int hotkey_report_mode;
1683 static u16 *hotkey_keycode_map;
1685 static struct attribute_set *hotkey_dev_attributes;
1687 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1688 #define HOTKEY_CONFIG_CRITICAL_START \
1689 do { \
1690 mutex_lock(&hotkey_thread_data_mutex); \
1691 hotkey_config_change++; \
1692 } while (0);
1693 #define HOTKEY_CONFIG_CRITICAL_END \
1694 mutex_unlock(&hotkey_thread_data_mutex);
1695 #else
1696 #define HOTKEY_CONFIG_CRITICAL_START
1697 #define HOTKEY_CONFIG_CRITICAL_END
1698 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1700 /* HKEY.MHKG() return bits */
1701 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1703 static int hotkey_get_wlsw(int *status)
1705 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1706 if (dbg_wlswemul) {
1707 *status = !!tpacpi_wlsw_emulstate;
1708 return 0;
1710 #endif
1711 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1712 return -EIO;
1713 return 0;
1716 static int hotkey_get_tablet_mode(int *status)
1718 int s;
1720 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1721 return -EIO;
1723 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1724 return 0;
1728 * Call with hotkey_mutex held
1730 static int hotkey_mask_get(void)
1732 u32 m = 0;
1734 if (tp_features.hotkey_mask) {
1735 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1736 return -EIO;
1738 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1740 return 0;
1744 * Call with hotkey_mutex held
1746 static int hotkey_mask_set(u32 mask)
1748 int i;
1749 int rc = 0;
1751 if (tp_features.hotkey_mask) {
1752 if (!tp_warned.hotkey_mask_ff &&
1753 (mask == 0xffff || mask == 0xffffff ||
1754 mask == 0xffffffff)) {
1755 tp_warned.hotkey_mask_ff = 1;
1756 printk(TPACPI_NOTICE
1757 "setting the hotkey mask to 0x%08x is likely "
1758 "not the best way to go about it\n", mask);
1759 printk(TPACPI_NOTICE
1760 "please consider using the driver defaults, "
1761 "and refer to up-to-date thinkpad-acpi "
1762 "documentation\n");
1765 HOTKEY_CONFIG_CRITICAL_START
1766 for (i = 0; i < 32; i++) {
1767 u32 m = 1 << i;
1768 /* enable in firmware mask only keys not in NVRAM
1769 * mode, but enable the key in the cached hotkey_mask
1770 * regardless of mode, or the key will end up
1771 * disabled by hotkey_mask_get() */
1772 if (!acpi_evalf(hkey_handle,
1773 NULL, "MHKM", "vdd", i + 1,
1774 !!((mask & ~hotkey_source_mask) & m))) {
1775 rc = -EIO;
1776 break;
1777 } else {
1778 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1781 HOTKEY_CONFIG_CRITICAL_END
1783 /* hotkey_mask_get must be called unconditionally below */
1784 if (!hotkey_mask_get() && !rc &&
1785 (hotkey_mask & ~hotkey_source_mask) !=
1786 (mask & ~hotkey_source_mask)) {
1787 printk(TPACPI_NOTICE
1788 "requested hot key mask 0x%08x, but "
1789 "firmware forced it to 0x%08x\n",
1790 mask, hotkey_mask);
1792 } else {
1793 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1794 HOTKEY_CONFIG_CRITICAL_START
1795 hotkey_mask = mask & hotkey_source_mask;
1796 HOTKEY_CONFIG_CRITICAL_END
1797 hotkey_mask_get();
1798 if (hotkey_mask != mask) {
1799 printk(TPACPI_NOTICE
1800 "requested hot key mask 0x%08x, "
1801 "forced to 0x%08x (NVRAM poll mask is "
1802 "0x%08x): no firmware mask support\n",
1803 mask, hotkey_mask, hotkey_source_mask);
1805 #else
1806 hotkey_mask_get();
1807 rc = -ENXIO;
1808 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1811 return rc;
1814 static int hotkey_status_get(int *status)
1816 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1817 return -EIO;
1819 return 0;
1822 static int hotkey_status_set(bool enable)
1824 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1825 return -EIO;
1827 return 0;
1830 static void tpacpi_input_send_tabletsw(void)
1832 int state;
1834 if (tp_features.hotkey_tablet &&
1835 !hotkey_get_tablet_mode(&state)) {
1836 mutex_lock(&tpacpi_inputdev_send_mutex);
1838 input_report_switch(tpacpi_inputdev,
1839 SW_TABLET_MODE, !!state);
1840 input_sync(tpacpi_inputdev);
1842 mutex_unlock(&tpacpi_inputdev_send_mutex);
1846 static void tpacpi_input_send_key(unsigned int scancode)
1848 unsigned int keycode;
1850 keycode = hotkey_keycode_map[scancode];
1852 if (keycode != KEY_RESERVED) {
1853 mutex_lock(&tpacpi_inputdev_send_mutex);
1855 input_report_key(tpacpi_inputdev, keycode, 1);
1856 if (keycode == KEY_UNKNOWN)
1857 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1858 scancode);
1859 input_sync(tpacpi_inputdev);
1861 input_report_key(tpacpi_inputdev, keycode, 0);
1862 if (keycode == KEY_UNKNOWN)
1863 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1864 scancode);
1865 input_sync(tpacpi_inputdev);
1867 mutex_unlock(&tpacpi_inputdev_send_mutex);
1871 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1872 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1874 static void tpacpi_hotkey_send_key(unsigned int scancode)
1876 tpacpi_input_send_key(scancode);
1877 if (hotkey_report_mode < 2) {
1878 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1879 0x80, 0x1001 + scancode);
1883 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1885 u8 d;
1887 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1888 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1889 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1890 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1891 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1892 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1894 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1895 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1896 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1898 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1899 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1900 n->displayexp_toggle =
1901 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1903 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1904 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1905 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1906 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1907 n->brightness_toggle =
1908 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1910 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1911 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1912 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1913 >> TP_NVRAM_POS_LEVEL_VOLUME;
1914 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1915 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1919 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1920 do { \
1921 if ((mask & (1 << __scancode)) && \
1922 oldn->__member != newn->__member) \
1923 tpacpi_hotkey_send_key(__scancode); \
1924 } while (0)
1926 #define TPACPI_MAY_SEND_KEY(__scancode) \
1927 do { if (mask & (1 << __scancode)) \
1928 tpacpi_hotkey_send_key(__scancode); } while (0)
1930 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1931 struct tp_nvram_state *newn,
1932 u32 mask)
1934 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1935 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1936 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1937 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1939 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1941 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1943 /* handle volume */
1944 if (oldn->volume_toggle != newn->volume_toggle) {
1945 if (oldn->mute != newn->mute) {
1946 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1948 if (oldn->volume_level > newn->volume_level) {
1949 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1950 } else if (oldn->volume_level < newn->volume_level) {
1951 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1952 } else if (oldn->mute == newn->mute) {
1953 /* repeated key presses that didn't change state */
1954 if (newn->mute) {
1955 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1956 } else if (newn->volume_level != 0) {
1957 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1958 } else {
1959 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1964 /* handle brightness */
1965 if (oldn->brightness_toggle != newn->brightness_toggle) {
1966 if (oldn->brightness_level < newn->brightness_level) {
1967 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1968 } else if (oldn->brightness_level > newn->brightness_level) {
1969 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1970 } else {
1971 /* repeated key presses that didn't change state */
1972 if (newn->brightness_level != 0) {
1973 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1974 } else {
1975 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1981 #undef TPACPI_COMPARE_KEY
1982 #undef TPACPI_MAY_SEND_KEY
1984 static int hotkey_kthread(void *data)
1986 struct tp_nvram_state s[2];
1987 u32 mask;
1988 unsigned int si, so;
1989 unsigned long t;
1990 unsigned int change_detector, must_reset;
1992 mutex_lock(&hotkey_thread_mutex);
1994 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1995 goto exit;
1997 set_freezable();
1999 so = 0;
2000 si = 1;
2001 t = 0;
2003 /* Initial state for compares */
2004 mutex_lock(&hotkey_thread_data_mutex);
2005 change_detector = hotkey_config_change;
2006 mask = hotkey_source_mask & hotkey_mask;
2007 mutex_unlock(&hotkey_thread_data_mutex);
2008 hotkey_read_nvram(&s[so], mask);
2010 while (!kthread_should_stop() && hotkey_poll_freq) {
2011 if (t == 0)
2012 t = 1000/hotkey_poll_freq;
2013 t = msleep_interruptible(t);
2014 if (unlikely(kthread_should_stop()))
2015 break;
2016 must_reset = try_to_freeze();
2017 if (t > 0 && !must_reset)
2018 continue;
2020 mutex_lock(&hotkey_thread_data_mutex);
2021 if (must_reset || hotkey_config_change != change_detector) {
2022 /* forget old state on thaw or config change */
2023 si = so;
2024 t = 0;
2025 change_detector = hotkey_config_change;
2027 mask = hotkey_source_mask & hotkey_mask;
2028 mutex_unlock(&hotkey_thread_data_mutex);
2030 if (likely(mask)) {
2031 hotkey_read_nvram(&s[si], mask);
2032 if (likely(si != so)) {
2033 hotkey_compare_and_issue_event(&s[so], &s[si],
2034 mask);
2038 so = si;
2039 si ^= 1;
2042 exit:
2043 mutex_unlock(&hotkey_thread_mutex);
2044 return 0;
2047 static void hotkey_poll_stop_sync(void)
2049 if (tpacpi_hotkey_task) {
2050 if (frozen(tpacpi_hotkey_task) ||
2051 freezing(tpacpi_hotkey_task))
2052 thaw_process(tpacpi_hotkey_task);
2054 kthread_stop(tpacpi_hotkey_task);
2055 tpacpi_hotkey_task = NULL;
2056 mutex_lock(&hotkey_thread_mutex);
2057 /* at this point, the thread did exit */
2058 mutex_unlock(&hotkey_thread_mutex);
2062 /* call with hotkey_mutex held */
2063 static void hotkey_poll_setup(int may_warn)
2065 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2066 hotkey_poll_freq > 0 &&
2067 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2068 if (!tpacpi_hotkey_task) {
2069 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2070 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2071 if (IS_ERR(tpacpi_hotkey_task)) {
2072 tpacpi_hotkey_task = NULL;
2073 printk(TPACPI_ERR
2074 "could not create kernel thread "
2075 "for hotkey polling\n");
2078 } else {
2079 hotkey_poll_stop_sync();
2080 if (may_warn &&
2081 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
2082 printk(TPACPI_NOTICE
2083 "hot keys 0x%08x require polling, "
2084 "which is currently disabled\n",
2085 hotkey_source_mask);
2090 static void hotkey_poll_setup_safe(int may_warn)
2092 mutex_lock(&hotkey_mutex);
2093 hotkey_poll_setup(may_warn);
2094 mutex_unlock(&hotkey_mutex);
2097 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2099 static void hotkey_poll_setup_safe(int __unused)
2103 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2105 static int hotkey_inputdev_open(struct input_dev *dev)
2107 switch (tpacpi_lifecycle) {
2108 case TPACPI_LIFE_INIT:
2110 * hotkey_init will call hotkey_poll_setup_safe
2111 * at the appropriate moment
2113 return 0;
2114 case TPACPI_LIFE_EXITING:
2115 return -EBUSY;
2116 case TPACPI_LIFE_RUNNING:
2117 hotkey_poll_setup_safe(0);
2118 return 0;
2121 /* Should only happen if tpacpi_lifecycle is corrupt */
2122 BUG();
2123 return -EBUSY;
2126 static void hotkey_inputdev_close(struct input_dev *dev)
2128 /* disable hotkey polling when possible */
2129 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2130 hotkey_poll_setup_safe(0);
2133 /* sysfs hotkey enable ------------------------------------------------- */
2134 static ssize_t hotkey_enable_show(struct device *dev,
2135 struct device_attribute *attr,
2136 char *buf)
2138 int res, status;
2140 printk_deprecated_attribute("hotkey_enable",
2141 "Hotkey reporting is always enabled");
2143 res = hotkey_status_get(&status);
2144 if (res)
2145 return res;
2147 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2150 static ssize_t hotkey_enable_store(struct device *dev,
2151 struct device_attribute *attr,
2152 const char *buf, size_t count)
2154 unsigned long t;
2156 printk_deprecated_attribute("hotkey_enable",
2157 "Hotkeys can be disabled through hotkey_mask");
2159 if (parse_strtoul(buf, 1, &t))
2160 return -EINVAL;
2162 if (t == 0)
2163 return -EPERM;
2165 return count;
2168 static struct device_attribute dev_attr_hotkey_enable =
2169 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2170 hotkey_enable_show, hotkey_enable_store);
2172 /* sysfs hotkey mask --------------------------------------------------- */
2173 static ssize_t hotkey_mask_show(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2177 int res;
2179 if (mutex_lock_killable(&hotkey_mutex))
2180 return -ERESTARTSYS;
2181 res = hotkey_mask_get();
2182 mutex_unlock(&hotkey_mutex);
2184 return (res)?
2185 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2188 static ssize_t hotkey_mask_store(struct device *dev,
2189 struct device_attribute *attr,
2190 const char *buf, size_t count)
2192 unsigned long t;
2193 int res;
2195 if (parse_strtoul(buf, 0xffffffffUL, &t))
2196 return -EINVAL;
2198 if (mutex_lock_killable(&hotkey_mutex))
2199 return -ERESTARTSYS;
2201 res = hotkey_mask_set(t);
2203 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2204 hotkey_poll_setup(1);
2205 #endif
2207 mutex_unlock(&hotkey_mutex);
2209 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2211 return (res) ? res : count;
2214 static struct device_attribute dev_attr_hotkey_mask =
2215 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2216 hotkey_mask_show, hotkey_mask_store);
2218 /* sysfs hotkey bios_enabled ------------------------------------------- */
2219 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2220 struct device_attribute *attr,
2221 char *buf)
2223 return sprintf(buf, "0\n");
2226 static struct device_attribute dev_attr_hotkey_bios_enabled =
2227 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2229 /* sysfs hotkey bios_mask ---------------------------------------------- */
2230 static ssize_t hotkey_bios_mask_show(struct device *dev,
2231 struct device_attribute *attr,
2232 char *buf)
2234 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2237 static struct device_attribute dev_attr_hotkey_bios_mask =
2238 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2240 /* sysfs hotkey all_mask ----------------------------------------------- */
2241 static ssize_t hotkey_all_mask_show(struct device *dev,
2242 struct device_attribute *attr,
2243 char *buf)
2245 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2246 hotkey_all_mask | hotkey_source_mask);
2249 static struct device_attribute dev_attr_hotkey_all_mask =
2250 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2252 /* sysfs hotkey recommended_mask --------------------------------------- */
2253 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2254 struct device_attribute *attr,
2255 char *buf)
2257 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2258 (hotkey_all_mask | hotkey_source_mask)
2259 & ~hotkey_reserved_mask);
2262 static struct device_attribute dev_attr_hotkey_recommended_mask =
2263 __ATTR(hotkey_recommended_mask, S_IRUGO,
2264 hotkey_recommended_mask_show, NULL);
2266 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2268 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2269 static ssize_t hotkey_source_mask_show(struct device *dev,
2270 struct device_attribute *attr,
2271 char *buf)
2273 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2276 static ssize_t hotkey_source_mask_store(struct device *dev,
2277 struct device_attribute *attr,
2278 const char *buf, size_t count)
2280 unsigned long t;
2282 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2283 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2284 return -EINVAL;
2286 if (mutex_lock_killable(&hotkey_mutex))
2287 return -ERESTARTSYS;
2289 HOTKEY_CONFIG_CRITICAL_START
2290 hotkey_source_mask = t;
2291 HOTKEY_CONFIG_CRITICAL_END
2293 hotkey_poll_setup(1);
2295 mutex_unlock(&hotkey_mutex);
2297 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2299 return count;
2302 static struct device_attribute dev_attr_hotkey_source_mask =
2303 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2304 hotkey_source_mask_show, hotkey_source_mask_store);
2306 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2307 static ssize_t hotkey_poll_freq_show(struct device *dev,
2308 struct device_attribute *attr,
2309 char *buf)
2311 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2314 static ssize_t hotkey_poll_freq_store(struct device *dev,
2315 struct device_attribute *attr,
2316 const char *buf, size_t count)
2318 unsigned long t;
2320 if (parse_strtoul(buf, 25, &t))
2321 return -EINVAL;
2323 if (mutex_lock_killable(&hotkey_mutex))
2324 return -ERESTARTSYS;
2326 hotkey_poll_freq = t;
2328 hotkey_poll_setup(1);
2329 mutex_unlock(&hotkey_mutex);
2331 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2333 return count;
2336 static struct device_attribute dev_attr_hotkey_poll_freq =
2337 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2338 hotkey_poll_freq_show, hotkey_poll_freq_store);
2340 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2342 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2343 static ssize_t hotkey_radio_sw_show(struct device *dev,
2344 struct device_attribute *attr,
2345 char *buf)
2347 int res, s;
2348 res = hotkey_get_wlsw(&s);
2349 if (res < 0)
2350 return res;
2352 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2355 static struct device_attribute dev_attr_hotkey_radio_sw =
2356 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2358 static void hotkey_radio_sw_notify_change(void)
2360 if (tp_features.hotkey_wlsw)
2361 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2362 "hotkey_radio_sw");
2365 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2366 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2367 struct device_attribute *attr,
2368 char *buf)
2370 int res, s;
2371 res = hotkey_get_tablet_mode(&s);
2372 if (res < 0)
2373 return res;
2375 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2378 static struct device_attribute dev_attr_hotkey_tablet_mode =
2379 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2381 static void hotkey_tablet_mode_notify_change(void)
2383 if (tp_features.hotkey_tablet)
2384 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2385 "hotkey_tablet_mode");
2388 /* sysfs hotkey report_mode -------------------------------------------- */
2389 static ssize_t hotkey_report_mode_show(struct device *dev,
2390 struct device_attribute *attr,
2391 char *buf)
2393 return snprintf(buf, PAGE_SIZE, "%d\n",
2394 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2397 static struct device_attribute dev_attr_hotkey_report_mode =
2398 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2400 /* sysfs wakeup reason (pollable) -------------------------------------- */
2401 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2402 struct device_attribute *attr,
2403 char *buf)
2405 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2408 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2409 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2411 static void hotkey_wakeup_reason_notify_change(void)
2413 if (tp_features.hotkey_mask)
2414 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2415 "wakeup_reason");
2418 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2419 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2420 struct device_attribute *attr,
2421 char *buf)
2423 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2426 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2427 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2428 hotkey_wakeup_hotunplug_complete_show, NULL);
2430 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2432 if (tp_features.hotkey_mask)
2433 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2434 "wakeup_hotunplug_complete");
2437 /* --------------------------------------------------------------------- */
2439 static struct attribute *hotkey_attributes[] __initdata = {
2440 &dev_attr_hotkey_enable.attr,
2441 &dev_attr_hotkey_bios_enabled.attr,
2442 &dev_attr_hotkey_report_mode.attr,
2443 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2444 &dev_attr_hotkey_mask.attr,
2445 &dev_attr_hotkey_all_mask.attr,
2446 &dev_attr_hotkey_recommended_mask.attr,
2447 &dev_attr_hotkey_source_mask.attr,
2448 &dev_attr_hotkey_poll_freq.attr,
2449 #endif
2452 static struct attribute *hotkey_mask_attributes[] __initdata = {
2453 &dev_attr_hotkey_bios_mask.attr,
2454 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2455 &dev_attr_hotkey_mask.attr,
2456 &dev_attr_hotkey_all_mask.attr,
2457 &dev_attr_hotkey_recommended_mask.attr,
2458 #endif
2459 &dev_attr_hotkey_wakeup_reason.attr,
2460 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2463 static void bluetooth_update_rfk(void);
2464 static void wan_update_rfk(void);
2465 static void uwb_update_rfk(void);
2466 static void tpacpi_send_radiosw_update(void)
2468 int wlsw;
2470 /* Sync these BEFORE sending any rfkill events */
2471 if (tp_features.bluetooth)
2472 bluetooth_update_rfk();
2473 if (tp_features.wan)
2474 wan_update_rfk();
2475 if (tp_features.uwb)
2476 uwb_update_rfk();
2478 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2479 mutex_lock(&tpacpi_inputdev_send_mutex);
2481 input_report_switch(tpacpi_inputdev,
2482 SW_RFKILL_ALL, !!wlsw);
2483 input_sync(tpacpi_inputdev);
2485 mutex_unlock(&tpacpi_inputdev_send_mutex);
2487 hotkey_radio_sw_notify_change();
2490 static void hotkey_exit(void)
2492 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2493 hotkey_poll_stop_sync();
2494 #endif
2496 if (hotkey_dev_attributes)
2497 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2499 kfree(hotkey_keycode_map);
2501 if (tp_features.hotkey) {
2502 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2503 "restoring original hot key mask\n");
2504 /* no short-circuit boolean operator below! */
2505 if ((hotkey_mask_set(hotkey_orig_mask) |
2506 hotkey_status_set(false)) != 0)
2507 printk(TPACPI_ERR
2508 "failed to restore hot key mask "
2509 "to BIOS defaults\n");
2513 static int __init hotkey_init(struct ibm_init_struct *iibm)
2515 /* Requirements for changing the default keymaps:
2517 * 1. Many of the keys are mapped to KEY_RESERVED for very
2518 * good reasons. Do not change them unless you have deep
2519 * knowledge on the IBM and Lenovo ThinkPad firmware for
2520 * the various ThinkPad models. The driver behaves
2521 * differently for KEY_RESERVED: such keys have their
2522 * hot key mask *unset* in mask_recommended, and also
2523 * in the initial hot key mask programmed into the
2524 * firmware at driver load time, which means the firm-
2525 * ware may react very differently if you change them to
2526 * something else;
2528 * 2. You must be subscribed to the linux-thinkpad and
2529 * ibm-acpi-devel mailing lists, and you should read the
2530 * list archives since 2007 if you want to change the
2531 * keymaps. This requirement exists so that you will
2532 * know the past history of problems with the thinkpad-
2533 * acpi driver keymaps, and also that you will be
2534 * listening to any bug reports;
2536 * 3. Do not send thinkpad-acpi specific patches directly to
2537 * for merging, *ever*. Send them to the linux-acpi
2538 * mailinglist for comments. Merging is to be done only
2539 * through acpi-test and the ACPI maintainer.
2541 * If the above is too much to ask, don't change the keymap.
2542 * Ask the thinkpad-acpi maintainer to do it, instead.
2544 static u16 ibm_keycode_map[] __initdata = {
2545 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2546 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2547 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2548 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2550 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2551 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2552 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2553 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2555 /* brightness: firmware always reacts to them, unless
2556 * X.org did some tricks in the radeon BIOS scratch
2557 * registers of *some* models */
2558 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2559 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2561 /* Thinklight: firmware always react to it */
2562 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2564 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2565 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2567 /* Volume: firmware always react to it and reprograms
2568 * the built-in *extra* mixer. Never map it to control
2569 * another mixer by default. */
2570 KEY_RESERVED, /* 0x14: VOLUME UP */
2571 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2572 KEY_RESERVED, /* 0x16: MUTE */
2574 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2576 /* (assignments unknown, please report if found) */
2577 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2578 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2580 static u16 lenovo_keycode_map[] __initdata = {
2581 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2582 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2583 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2584 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2586 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2587 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2588 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2589 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2591 /* These either have to go through ACPI video, or
2592 * act like in the IBM ThinkPads, so don't ever
2593 * enable them by default */
2594 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2595 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2597 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2599 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2600 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2602 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2603 * react to it and reprograms the built-in *extra* mixer.
2604 * Never map it to control another mixer by default.
2606 * T60?, T61, R60?, R61: firmware and EC tries to send
2607 * these over the regular keyboard, so these are no-ops,
2608 * but there are still weird bugs re. MUTE, so do not
2609 * change unless you get test reports from all Lenovo
2610 * models. May cause the BIOS to interfere with the
2611 * HDA mixer.
2613 KEY_RESERVED, /* 0x14: VOLUME UP */
2614 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2615 KEY_RESERVED, /* 0x16: MUTE */
2617 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2619 /* (assignments unknown, please report if found) */
2620 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2621 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2624 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2625 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2626 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2628 int res, i;
2629 int status;
2630 int hkeyv;
2632 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2633 "initializing hotkey subdriver\n");
2635 BUG_ON(!tpacpi_inputdev);
2636 BUG_ON(tpacpi_inputdev->open != NULL ||
2637 tpacpi_inputdev->close != NULL);
2639 TPACPI_ACPIHANDLE_INIT(hkey);
2640 mutex_init(&hotkey_mutex);
2642 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2643 mutex_init(&hotkey_thread_mutex);
2644 mutex_init(&hotkey_thread_data_mutex);
2645 #endif
2647 /* hotkey not supported on 570 */
2648 tp_features.hotkey = hkey_handle != NULL;
2650 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2651 "hotkeys are %s\n",
2652 str_supported(tp_features.hotkey));
2654 if (!tp_features.hotkey)
2655 return 1;
2657 tpacpi_disable_brightness_delay();
2659 hotkey_dev_attributes = create_attr_set(13, NULL);
2660 if (!hotkey_dev_attributes)
2661 return -ENOMEM;
2662 res = add_many_to_attr_set(hotkey_dev_attributes,
2663 hotkey_attributes,
2664 ARRAY_SIZE(hotkey_attributes));
2665 if (res)
2666 goto err_exit;
2668 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2669 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2670 for HKEY interface version 0x100 */
2671 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2672 if ((hkeyv >> 8) != 1) {
2673 printk(TPACPI_ERR "unknown version of the "
2674 "HKEY interface: 0x%x\n", hkeyv);
2675 printk(TPACPI_ERR "please report this to %s\n",
2676 TPACPI_MAIL);
2677 } else {
2679 * MHKV 0x100 in A31, R40, R40e,
2680 * T4x, X31, and later
2682 tp_features.hotkey_mask = 1;
2683 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2684 "firmware HKEY interface version: 0x%x\n",
2685 hkeyv);
2689 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2690 "hotkey masks are %s\n",
2691 str_supported(tp_features.hotkey_mask));
2693 if (tp_features.hotkey_mask) {
2694 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2695 "MHKA", "qd")) {
2696 printk(TPACPI_ERR
2697 "missing MHKA handler, "
2698 "please report this to %s\n",
2699 TPACPI_MAIL);
2700 /* FN+F12, FN+F4, FN+F3 */
2701 hotkey_all_mask = 0x080cU;
2705 /* hotkey_source_mask *must* be zero for
2706 * the first hotkey_mask_get */
2707 if (tp_features.hotkey_mask) {
2708 res = hotkey_mask_get();
2709 if (res)
2710 goto err_exit;
2712 hotkey_orig_mask = hotkey_mask;
2713 res = add_many_to_attr_set(
2714 hotkey_dev_attributes,
2715 hotkey_mask_attributes,
2716 ARRAY_SIZE(hotkey_mask_attributes));
2717 if (res)
2718 goto err_exit;
2721 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2722 if (tp_features.hotkey_mask) {
2723 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2724 & ~hotkey_all_mask;
2725 } else {
2726 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2729 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2730 "hotkey source mask 0x%08x, polling freq %d\n",
2731 hotkey_source_mask, hotkey_poll_freq);
2732 #endif
2734 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2735 if (dbg_wlswemul) {
2736 tp_features.hotkey_wlsw = 1;
2737 printk(TPACPI_INFO
2738 "radio switch emulation enabled\n");
2739 } else
2740 #endif
2741 /* Not all thinkpads have a hardware radio switch */
2742 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2743 tp_features.hotkey_wlsw = 1;
2744 printk(TPACPI_INFO
2745 "radio switch found; radios are %s\n",
2746 enabled(status, 0));
2748 if (tp_features.hotkey_wlsw)
2749 res = add_to_attr_set(hotkey_dev_attributes,
2750 &dev_attr_hotkey_radio_sw.attr);
2752 /* For X41t, X60t, X61t Tablets... */
2753 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2754 tp_features.hotkey_tablet = 1;
2755 printk(TPACPI_INFO
2756 "possible tablet mode switch found; "
2757 "ThinkPad in %s mode\n",
2758 (status & TP_HOTKEY_TABLET_MASK)?
2759 "tablet" : "laptop");
2760 res = add_to_attr_set(hotkey_dev_attributes,
2761 &dev_attr_hotkey_tablet_mode.attr);
2764 if (!res)
2765 res = register_attr_set_with_sysfs(
2766 hotkey_dev_attributes,
2767 &tpacpi_pdev->dev.kobj);
2768 if (res)
2769 goto err_exit;
2771 /* Set up key map */
2773 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2774 GFP_KERNEL);
2775 if (!hotkey_keycode_map) {
2776 printk(TPACPI_ERR
2777 "failed to allocate memory for key map\n");
2778 res = -ENOMEM;
2779 goto err_exit;
2782 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2783 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2784 "using Lenovo default hot key map\n");
2785 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2786 TPACPI_HOTKEY_MAP_SIZE);
2787 } else {
2788 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2789 "using IBM default hot key map\n");
2790 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2791 TPACPI_HOTKEY_MAP_SIZE);
2794 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2795 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2796 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2797 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2798 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2799 tpacpi_inputdev->keycode = hotkey_keycode_map;
2800 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2801 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2802 set_bit(hotkey_keycode_map[i],
2803 tpacpi_inputdev->keybit);
2804 } else {
2805 if (i < sizeof(hotkey_reserved_mask)*8)
2806 hotkey_reserved_mask |= 1 << i;
2810 if (tp_features.hotkey_wlsw) {
2811 set_bit(EV_SW, tpacpi_inputdev->evbit);
2812 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2814 if (tp_features.hotkey_tablet) {
2815 set_bit(EV_SW, tpacpi_inputdev->evbit);
2816 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2819 /* Do not issue duplicate brightness change events to
2820 * userspace */
2821 if (!tp_features.bright_acpimode)
2822 /* update bright_acpimode... */
2823 tpacpi_check_std_acpi_brightness_support();
2825 if (tp_features.bright_acpimode) {
2826 printk(TPACPI_INFO
2827 "This ThinkPad has standard ACPI backlight "
2828 "brightness control, supported by the ACPI "
2829 "video driver\n");
2830 printk(TPACPI_NOTICE
2831 "Disabling thinkpad-acpi brightness events "
2832 "by default...\n");
2834 /* The hotkey_reserved_mask change below is not
2835 * necessary while the keys are at KEY_RESERVED in the
2836 * default map, but better safe than sorry, leave it
2837 * here as a marker of what we have to do, especially
2838 * when we finally become able to set this at runtime
2839 * on response to X.org requests */
2840 hotkey_reserved_mask |=
2841 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2842 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2845 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2846 "enabling firmware HKEY event interface...\n");
2847 res = hotkey_status_set(true);
2848 if (res) {
2849 hotkey_exit();
2850 return res;
2852 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2853 & ~hotkey_reserved_mask)
2854 | hotkey_orig_mask);
2855 if (res < 0 && res != -ENXIO) {
2856 hotkey_exit();
2857 return res;
2860 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2861 "legacy ibm/hotkey event reporting over procfs %s\n",
2862 (hotkey_report_mode < 2) ?
2863 "enabled" : "disabled");
2865 tpacpi_inputdev->open = &hotkey_inputdev_open;
2866 tpacpi_inputdev->close = &hotkey_inputdev_close;
2868 hotkey_poll_setup_safe(1);
2869 tpacpi_send_radiosw_update();
2870 tpacpi_input_send_tabletsw();
2872 return 0;
2874 err_exit:
2875 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2876 hotkey_dev_attributes = NULL;
2878 return (res < 0)? res : 1;
2881 static bool hotkey_notify_hotkey(const u32 hkey,
2882 bool *send_acpi_ev,
2883 bool *ignore_acpi_ev)
2885 /* 0x1000-0x1FFF: key presses */
2886 unsigned int scancode = hkey & 0xfff;
2887 *send_acpi_ev = true;
2888 *ignore_acpi_ev = false;
2890 if (scancode > 0 && scancode < 0x21) {
2891 scancode--;
2892 if (!(hotkey_source_mask & (1 << scancode))) {
2893 tpacpi_input_send_key(scancode);
2894 *send_acpi_ev = false;
2895 } else {
2896 *ignore_acpi_ev = true;
2898 return true;
2900 return false;
2903 static bool hotkey_notify_wakeup(const u32 hkey,
2904 bool *send_acpi_ev,
2905 bool *ignore_acpi_ev)
2907 /* 0x2000-0x2FFF: Wakeup reason */
2908 *send_acpi_ev = true;
2909 *ignore_acpi_ev = false;
2911 switch (hkey) {
2912 case 0x2304: /* suspend, undock */
2913 case 0x2404: /* hibernation, undock */
2914 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2915 *ignore_acpi_ev = true;
2916 break;
2918 case 0x2305: /* suspend, bay eject */
2919 case 0x2405: /* hibernation, bay eject */
2920 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2921 *ignore_acpi_ev = true;
2922 break;
2924 case 0x2313: /* Battery on critical low level (S3) */
2925 case 0x2413: /* Battery on critical low level (S4) */
2926 printk(TPACPI_ALERT
2927 "EMERGENCY WAKEUP: battery almost empty\n");
2928 /* how to auto-heal: */
2929 /* 2313: woke up from S3, go to S4/S5 */
2930 /* 2413: woke up from S4, go to S5 */
2931 break;
2933 default:
2934 return false;
2937 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2938 printk(TPACPI_INFO
2939 "woke up due to a hot-unplug "
2940 "request...\n");
2941 hotkey_wakeup_reason_notify_change();
2943 return true;
2946 static bool hotkey_notify_usrevent(const u32 hkey,
2947 bool *send_acpi_ev,
2948 bool *ignore_acpi_ev)
2950 /* 0x5000-0x5FFF: human interface helpers */
2951 *send_acpi_ev = true;
2952 *ignore_acpi_ev = false;
2954 switch (hkey) {
2955 case 0x5010: /* Lenovo new BIOS: brightness changed */
2956 case 0x500b: /* X61t: tablet pen inserted into bay */
2957 case 0x500c: /* X61t: tablet pen removed from bay */
2958 return true;
2960 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2961 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2962 tpacpi_input_send_tabletsw();
2963 hotkey_tablet_mode_notify_change();
2964 *send_acpi_ev = false;
2965 return true;
2967 case 0x5001:
2968 case 0x5002:
2969 /* LID switch events. Do not propagate */
2970 *ignore_acpi_ev = true;
2971 return true;
2973 default:
2974 return false;
2978 static bool hotkey_notify_thermal(const u32 hkey,
2979 bool *send_acpi_ev,
2980 bool *ignore_acpi_ev)
2982 /* 0x6000-0x6FFF: thermal alarms */
2983 *send_acpi_ev = true;
2984 *ignore_acpi_ev = false;
2986 switch (hkey) {
2987 case 0x6011:
2988 printk(TPACPI_CRIT
2989 "THERMAL ALARM: battery is too hot!\n");
2990 /* recommended action: warn user through gui */
2991 return true;
2992 case 0x6012:
2993 printk(TPACPI_ALERT
2994 "THERMAL EMERGENCY: battery is extremely hot!\n");
2995 /* recommended action: immediate sleep/hibernate */
2996 return true;
2997 case 0x6021:
2998 printk(TPACPI_CRIT
2999 "THERMAL ALARM: "
3000 "a sensor reports something is too hot!\n");
3001 /* recommended action: warn user through gui, that */
3002 /* some internal component is too hot */
3003 return true;
3004 case 0x6022:
3005 printk(TPACPI_ALERT
3006 "THERMAL EMERGENCY: "
3007 "a sensor reports something is extremely hot!\n");
3008 /* recommended action: immediate sleep/hibernate */
3009 return true;
3010 case 0x6030:
3011 printk(TPACPI_INFO
3012 "EC reports that Thermal Table has changed\n");
3013 /* recommended action: do nothing, we don't have
3014 * Lenovo ATM information */
3015 return true;
3016 default:
3017 printk(TPACPI_ALERT
3018 "THERMAL ALERT: unknown thermal alarm received\n");
3019 return false;
3023 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3025 u32 hkey;
3026 bool send_acpi_ev;
3027 bool ignore_acpi_ev;
3028 bool known_ev;
3030 if (event != 0x80) {
3031 printk(TPACPI_ERR
3032 "unknown HKEY notification event %d\n", event);
3033 /* forward it to userspace, maybe it knows how to handle it */
3034 acpi_bus_generate_netlink_event(
3035 ibm->acpi->device->pnp.device_class,
3036 dev_name(&ibm->acpi->device->dev),
3037 event, 0);
3038 return;
3041 while (1) {
3042 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3043 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3044 return;
3047 if (hkey == 0) {
3048 /* queue empty */
3049 return;
3052 send_acpi_ev = true;
3053 ignore_acpi_ev = false;
3055 switch (hkey >> 12) {
3056 case 1:
3057 /* 0x1000-0x1FFF: key presses */
3058 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3059 &ignore_acpi_ev);
3060 break;
3061 case 2:
3062 /* 0x2000-0x2FFF: Wakeup reason */
3063 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3064 &ignore_acpi_ev);
3065 break;
3066 case 3:
3067 /* 0x3000-0x3FFF: bay-related wakeups */
3068 if (hkey == 0x3003) {
3069 hotkey_autosleep_ack = 1;
3070 printk(TPACPI_INFO
3071 "bay ejected\n");
3072 hotkey_wakeup_hotunplug_complete_notify_change();
3073 known_ev = true;
3074 } else {
3075 known_ev = false;
3077 break;
3078 case 4:
3079 /* 0x4000-0x4FFF: dock-related wakeups */
3080 if (hkey == 0x4003) {
3081 hotkey_autosleep_ack = 1;
3082 printk(TPACPI_INFO
3083 "undocked\n");
3084 hotkey_wakeup_hotunplug_complete_notify_change();
3085 known_ev = true;
3086 } else {
3087 known_ev = false;
3089 break;
3090 case 5:
3091 /* 0x5000-0x5FFF: human interface helpers */
3092 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3093 &ignore_acpi_ev);
3094 break;
3095 case 6:
3096 /* 0x6000-0x6FFF: thermal alarms */
3097 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3098 &ignore_acpi_ev);
3099 break;
3100 case 7:
3101 /* 0x7000-0x7FFF: misc */
3102 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3103 tpacpi_send_radiosw_update();
3104 send_acpi_ev = 0;
3105 known_ev = true;
3106 break;
3108 /* fallthrough to default */
3109 default:
3110 known_ev = false;
3112 if (!known_ev) {
3113 printk(TPACPI_NOTICE
3114 "unhandled HKEY event 0x%04x\n", hkey);
3115 printk(TPACPI_NOTICE
3116 "please report the conditions when this "
3117 "event happened to %s\n", TPACPI_MAIL);
3120 /* Legacy events */
3121 if (!ignore_acpi_ev &&
3122 (send_acpi_ev || hotkey_report_mode < 2)) {
3123 acpi_bus_generate_proc_event(ibm->acpi->device,
3124 event, hkey);
3127 /* netlink events */
3128 if (!ignore_acpi_ev && send_acpi_ev) {
3129 acpi_bus_generate_netlink_event(
3130 ibm->acpi->device->pnp.device_class,
3131 dev_name(&ibm->acpi->device->dev),
3132 event, hkey);
3137 static void hotkey_suspend(pm_message_t state)
3139 /* Do these on suspend, we get the events on early resume! */
3140 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3141 hotkey_autosleep_ack = 0;
3144 static void hotkey_resume(void)
3146 tpacpi_disable_brightness_delay();
3148 if (hotkey_mask_get())
3149 printk(TPACPI_ERR
3150 "error while trying to read hot key mask "
3151 "from firmware\n");
3152 tpacpi_send_radiosw_update();
3153 hotkey_tablet_mode_notify_change();
3154 hotkey_wakeup_reason_notify_change();
3155 hotkey_wakeup_hotunplug_complete_notify_change();
3156 hotkey_poll_setup_safe(0);
3159 /* procfs -------------------------------------------------------------- */
3160 static int hotkey_read(char *p)
3162 int res, status;
3163 int len = 0;
3165 if (!tp_features.hotkey) {
3166 len += sprintf(p + len, "status:\t\tnot supported\n");
3167 return len;
3170 if (mutex_lock_killable(&hotkey_mutex))
3171 return -ERESTARTSYS;
3172 res = hotkey_status_get(&status);
3173 if (!res)
3174 res = hotkey_mask_get();
3175 mutex_unlock(&hotkey_mutex);
3176 if (res)
3177 return res;
3179 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3180 if (tp_features.hotkey_mask) {
3181 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3182 len += sprintf(p + len,
3183 "commands:\tenable, disable, reset, <mask>\n");
3184 } else {
3185 len += sprintf(p + len, "mask:\t\tnot supported\n");
3186 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3189 return len;
3192 static void hotkey_enabledisable_warn(bool enable)
3194 tpacpi_log_usertask("procfs hotkey enable/disable");
3195 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3196 TPACPI_WARN
3197 "hotkey enable/disable functionality has been "
3198 "removed from the driver. Hotkeys are always "
3199 "enabled\n"))
3200 printk(TPACPI_ERR
3201 "Please remove the hotkey=enable module "
3202 "parameter, it is deprecated. Hotkeys are always "
3203 "enabled\n");
3206 static int hotkey_write(char *buf)
3208 int res;
3209 u32 mask;
3210 char *cmd;
3212 if (!tp_features.hotkey)
3213 return -ENODEV;
3215 if (mutex_lock_killable(&hotkey_mutex))
3216 return -ERESTARTSYS;
3218 mask = hotkey_mask;
3220 res = 0;
3221 while ((cmd = next_cmd(&buf))) {
3222 if (strlencmp(cmd, "enable") == 0) {
3223 hotkey_enabledisable_warn(1);
3224 } else if (strlencmp(cmd, "disable") == 0) {
3225 hotkey_enabledisable_warn(0);
3226 res = -EPERM;
3227 } else if (strlencmp(cmd, "reset") == 0) {
3228 mask = hotkey_orig_mask;
3229 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3230 /* mask set */
3231 } else if (sscanf(cmd, "%x", &mask) == 1) {
3232 /* mask set */
3233 } else {
3234 res = -EINVAL;
3235 goto errexit;
3239 if (!res)
3240 tpacpi_disclose_usertask("procfs hotkey",
3241 "set mask to 0x%08x\n", mask);
3243 if (!res && mask != hotkey_mask)
3244 res = hotkey_mask_set(mask);
3246 errexit:
3247 mutex_unlock(&hotkey_mutex);
3248 return res;
3251 static const struct acpi_device_id ibm_htk_device_ids[] = {
3252 {TPACPI_ACPI_HKEY_HID, 0},
3253 {"", 0},
3256 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3257 .hid = ibm_htk_device_ids,
3258 .notify = hotkey_notify,
3259 .handle = &hkey_handle,
3260 .type = ACPI_DEVICE_NOTIFY,
3263 static struct ibm_struct hotkey_driver_data = {
3264 .name = "hotkey",
3265 .read = hotkey_read,
3266 .write = hotkey_write,
3267 .exit = hotkey_exit,
3268 .resume = hotkey_resume,
3269 .suspend = hotkey_suspend,
3270 .acpi = &ibm_hotkey_acpidriver,
3273 /*************************************************************************
3274 * Bluetooth subdriver
3277 enum {
3278 /* ACPI GBDC/SBDC bits */
3279 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3280 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3281 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3282 off / last state */
3285 enum {
3286 /* ACPI \BLTH commands */
3287 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3288 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3289 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3290 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3291 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3294 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3296 static struct rfkill *tpacpi_bluetooth_rfkill;
3298 static void bluetooth_suspend(pm_message_t state)
3300 /* Try to make sure radio will resume powered off */
3301 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3302 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3303 vdbg_printk(TPACPI_DBG_RFKILL,
3304 "bluetooth power down on resume request failed\n");
3307 static int bluetooth_get_radiosw(void)
3309 int status;
3311 if (!tp_features.bluetooth)
3312 return -ENODEV;
3314 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3315 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3316 return RFKILL_STATE_HARD_BLOCKED;
3318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3319 if (dbg_bluetoothemul)
3320 return (tpacpi_bluetooth_emulstate) ?
3321 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3322 #endif
3324 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3325 return -EIO;
3327 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3328 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3331 static void bluetooth_update_rfk(void)
3333 int status;
3335 if (!tpacpi_bluetooth_rfkill)
3336 return;
3338 status = bluetooth_get_radiosw();
3339 if (status < 0)
3340 return;
3341 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3343 vdbg_printk(TPACPI_DBG_RFKILL,
3344 "forced rfkill state to %d\n",
3345 status);
3348 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3350 int status;
3352 if (!tp_features.bluetooth)
3353 return -ENODEV;
3355 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3356 * reason to risk weird behaviour. */
3357 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3358 && radio_on)
3359 return -EPERM;
3361 vdbg_printk(TPACPI_DBG_RFKILL,
3362 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3365 if (dbg_bluetoothemul) {
3366 tpacpi_bluetooth_emulstate = !!radio_on;
3367 if (update_rfk)
3368 bluetooth_update_rfk();
3369 return 0;
3371 #endif
3373 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3374 if (radio_on)
3375 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3376 else
3377 status = 0;
3378 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3379 return -EIO;
3381 if (update_rfk)
3382 bluetooth_update_rfk();
3384 return 0;
3387 /* sysfs bluetooth enable ---------------------------------------------- */
3388 static ssize_t bluetooth_enable_show(struct device *dev,
3389 struct device_attribute *attr,
3390 char *buf)
3392 int status;
3394 printk_deprecated_rfkill_attribute("bluetooth_enable");
3396 status = bluetooth_get_radiosw();
3397 if (status < 0)
3398 return status;
3400 return snprintf(buf, PAGE_SIZE, "%d\n",
3401 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3404 static ssize_t bluetooth_enable_store(struct device *dev,
3405 struct device_attribute *attr,
3406 const char *buf, size_t count)
3408 unsigned long t;
3409 int res;
3411 printk_deprecated_rfkill_attribute("bluetooth_enable");
3413 if (parse_strtoul(buf, 1, &t))
3414 return -EINVAL;
3416 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3418 res = bluetooth_set_radiosw(t, 1);
3420 return (res) ? res : count;
3423 static struct device_attribute dev_attr_bluetooth_enable =
3424 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3425 bluetooth_enable_show, bluetooth_enable_store);
3427 /* --------------------------------------------------------------------- */
3429 static struct attribute *bluetooth_attributes[] = {
3430 &dev_attr_bluetooth_enable.attr,
3431 NULL
3434 static const struct attribute_group bluetooth_attr_group = {
3435 .attrs = bluetooth_attributes,
3438 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3440 int bts = bluetooth_get_radiosw();
3442 if (bts < 0)
3443 return bts;
3445 *state = bts;
3446 return 0;
3449 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3451 dbg_printk(TPACPI_DBG_RFKILL,
3452 "request to change radio state to %d\n", state);
3453 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3456 static void bluetooth_shutdown(void)
3458 /* Order firmware to save current state to NVRAM */
3459 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3460 TP_ACPI_BLTH_SAVE_STATE))
3461 printk(TPACPI_NOTICE
3462 "failed to save bluetooth state to NVRAM\n");
3463 else
3464 vdbg_printk(TPACPI_DBG_RFKILL,
3465 "bluestooth state saved to NVRAM\n");
3468 static void bluetooth_exit(void)
3470 bluetooth_shutdown();
3472 if (tpacpi_bluetooth_rfkill)
3473 rfkill_unregister(tpacpi_bluetooth_rfkill);
3475 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3476 &bluetooth_attr_group);
3479 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3481 int res;
3482 int status = 0;
3484 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3485 "initializing bluetooth subdriver\n");
3487 TPACPI_ACPIHANDLE_INIT(hkey);
3489 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3490 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3491 tp_features.bluetooth = hkey_handle &&
3492 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3494 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3495 "bluetooth is %s, status 0x%02x\n",
3496 str_supported(tp_features.bluetooth),
3497 status);
3499 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3500 if (dbg_bluetoothemul) {
3501 tp_features.bluetooth = 1;
3502 printk(TPACPI_INFO
3503 "bluetooth switch emulation enabled\n");
3504 } else
3505 #endif
3506 if (tp_features.bluetooth &&
3507 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3508 /* no bluetooth hardware present in system */
3509 tp_features.bluetooth = 0;
3510 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3511 "bluetooth hardware not installed\n");
3514 if (!tp_features.bluetooth)
3515 return 1;
3517 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3518 &bluetooth_attr_group);
3519 if (res)
3520 return res;
3522 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3523 &tpacpi_bluetooth_rfkill,
3524 RFKILL_TYPE_BLUETOOTH,
3525 TPACPI_RFK_BLUETOOTH_SW_NAME,
3526 true,
3527 tpacpi_bluetooth_rfk_set,
3528 tpacpi_bluetooth_rfk_get);
3529 if (res) {
3530 bluetooth_exit();
3531 return res;
3534 return 0;
3537 /* procfs -------------------------------------------------------------- */
3538 static int bluetooth_read(char *p)
3540 int len = 0;
3541 int status = bluetooth_get_radiosw();
3543 if (!tp_features.bluetooth)
3544 len += sprintf(p + len, "status:\t\tnot supported\n");
3545 else {
3546 len += sprintf(p + len, "status:\t\t%s\n",
3547 (status == RFKILL_STATE_UNBLOCKED) ?
3548 "enabled" : "disabled");
3549 len += sprintf(p + len, "commands:\tenable, disable\n");
3552 return len;
3555 static int bluetooth_write(char *buf)
3557 char *cmd;
3558 int state = -1;
3560 if (!tp_features.bluetooth)
3561 return -ENODEV;
3563 while ((cmd = next_cmd(&buf))) {
3564 if (strlencmp(cmd, "enable") == 0) {
3565 state = 1;
3566 } else if (strlencmp(cmd, "disable") == 0) {
3567 state = 0;
3568 } else
3569 return -EINVAL;
3572 if (state != -1) {
3573 tpacpi_disclose_usertask("procfs bluetooth",
3574 "attempt to %s\n",
3575 state ? "enable" : "disable");
3576 bluetooth_set_radiosw(state, 1);
3579 return 0;
3582 static struct ibm_struct bluetooth_driver_data = {
3583 .name = "bluetooth",
3584 .read = bluetooth_read,
3585 .write = bluetooth_write,
3586 .exit = bluetooth_exit,
3587 .suspend = bluetooth_suspend,
3588 .shutdown = bluetooth_shutdown,
3591 /*************************************************************************
3592 * Wan subdriver
3595 enum {
3596 /* ACPI GWAN/SWAN bits */
3597 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3598 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3599 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3600 off / last state */
3603 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3605 static struct rfkill *tpacpi_wan_rfkill;
3607 static void wan_suspend(pm_message_t state)
3609 /* Try to make sure radio will resume powered off */
3610 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3611 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3612 vdbg_printk(TPACPI_DBG_RFKILL,
3613 "WWAN power down on resume request failed\n");
3616 static int wan_get_radiosw(void)
3618 int status;
3620 if (!tp_features.wan)
3621 return -ENODEV;
3623 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3624 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3625 return RFKILL_STATE_HARD_BLOCKED;
3627 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3628 if (dbg_wwanemul)
3629 return (tpacpi_wwan_emulstate) ?
3630 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3631 #endif
3633 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3634 return -EIO;
3636 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3637 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3640 static void wan_update_rfk(void)
3642 int status;
3644 if (!tpacpi_wan_rfkill)
3645 return;
3647 status = wan_get_radiosw();
3648 if (status < 0)
3649 return;
3650 rfkill_force_state(tpacpi_wan_rfkill, status);
3652 vdbg_printk(TPACPI_DBG_RFKILL,
3653 "forced rfkill state to %d\n",
3654 status);
3657 static int wan_set_radiosw(int radio_on, int update_rfk)
3659 int status;
3661 if (!tp_features.wan)
3662 return -ENODEV;
3664 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3665 * reason to risk weird behaviour. */
3666 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3667 && radio_on)
3668 return -EPERM;
3670 vdbg_printk(TPACPI_DBG_RFKILL,
3671 "will %s WWAN\n", radio_on ? "enable" : "disable");
3673 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3674 if (dbg_wwanemul) {
3675 tpacpi_wwan_emulstate = !!radio_on;
3676 if (update_rfk)
3677 wan_update_rfk();
3678 return 0;
3680 #endif
3682 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3683 if (radio_on)
3684 status = TP_ACPI_WANCARD_RADIOSSW;
3685 else
3686 status = 0;
3687 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3688 return -EIO;
3690 if (update_rfk)
3691 wan_update_rfk();
3693 return 0;
3696 /* sysfs wan enable ---------------------------------------------------- */
3697 static ssize_t wan_enable_show(struct device *dev,
3698 struct device_attribute *attr,
3699 char *buf)
3701 int status;
3703 printk_deprecated_rfkill_attribute("wwan_enable");
3705 status = wan_get_radiosw();
3706 if (status < 0)
3707 return status;
3709 return snprintf(buf, PAGE_SIZE, "%d\n",
3710 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3713 static ssize_t wan_enable_store(struct device *dev,
3714 struct device_attribute *attr,
3715 const char *buf, size_t count)
3717 unsigned long t;
3718 int res;
3720 printk_deprecated_rfkill_attribute("wwan_enable");
3722 if (parse_strtoul(buf, 1, &t))
3723 return -EINVAL;
3725 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3727 res = wan_set_radiosw(t, 1);
3729 return (res) ? res : count;
3732 static struct device_attribute dev_attr_wan_enable =
3733 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3734 wan_enable_show, wan_enable_store);
3736 /* --------------------------------------------------------------------- */
3738 static struct attribute *wan_attributes[] = {
3739 &dev_attr_wan_enable.attr,
3740 NULL
3743 static const struct attribute_group wan_attr_group = {
3744 .attrs = wan_attributes,
3747 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3749 int wans = wan_get_radiosw();
3751 if (wans < 0)
3752 return wans;
3754 *state = wans;
3755 return 0;
3758 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3760 dbg_printk(TPACPI_DBG_RFKILL,
3761 "request to change radio state to %d\n", state);
3762 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3765 static void wan_shutdown(void)
3767 /* Order firmware to save current state to NVRAM */
3768 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3769 TP_ACPI_WGSV_SAVE_STATE))
3770 printk(TPACPI_NOTICE
3771 "failed to save WWAN state to NVRAM\n");
3772 else
3773 vdbg_printk(TPACPI_DBG_RFKILL,
3774 "WWAN state saved to NVRAM\n");
3777 static void wan_exit(void)
3779 wan_shutdown();
3781 if (tpacpi_wan_rfkill)
3782 rfkill_unregister(tpacpi_wan_rfkill);
3784 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3785 &wan_attr_group);
3788 static int __init wan_init(struct ibm_init_struct *iibm)
3790 int res;
3791 int status = 0;
3793 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3794 "initializing wan subdriver\n");
3796 TPACPI_ACPIHANDLE_INIT(hkey);
3798 tp_features.wan = hkey_handle &&
3799 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3801 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3802 "wan is %s, status 0x%02x\n",
3803 str_supported(tp_features.wan),
3804 status);
3806 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3807 if (dbg_wwanemul) {
3808 tp_features.wan = 1;
3809 printk(TPACPI_INFO
3810 "wwan switch emulation enabled\n");
3811 } else
3812 #endif
3813 if (tp_features.wan &&
3814 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3815 /* no wan hardware present in system */
3816 tp_features.wan = 0;
3817 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3818 "wan hardware not installed\n");
3821 if (!tp_features.wan)
3822 return 1;
3824 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3825 &wan_attr_group);
3826 if (res)
3827 return res;
3829 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3830 &tpacpi_wan_rfkill,
3831 RFKILL_TYPE_WWAN,
3832 TPACPI_RFK_WWAN_SW_NAME,
3833 true,
3834 tpacpi_wan_rfk_set,
3835 tpacpi_wan_rfk_get);
3836 if (res) {
3837 wan_exit();
3838 return res;
3841 return 0;
3844 /* procfs -------------------------------------------------------------- */
3845 static int wan_read(char *p)
3847 int len = 0;
3848 int status = wan_get_radiosw();
3850 tpacpi_disclose_usertask("procfs wan", "read");
3852 if (!tp_features.wan)
3853 len += sprintf(p + len, "status:\t\tnot supported\n");
3854 else {
3855 len += sprintf(p + len, "status:\t\t%s\n",
3856 (status == RFKILL_STATE_UNBLOCKED) ?
3857 "enabled" : "disabled");
3858 len += sprintf(p + len, "commands:\tenable, disable\n");
3861 return len;
3864 static int wan_write(char *buf)
3866 char *cmd;
3867 int state = -1;
3869 if (!tp_features.wan)
3870 return -ENODEV;
3872 while ((cmd = next_cmd(&buf))) {
3873 if (strlencmp(cmd, "enable") == 0) {
3874 state = 1;
3875 } else if (strlencmp(cmd, "disable") == 0) {
3876 state = 0;
3877 } else
3878 return -EINVAL;
3881 if (state != -1) {
3882 tpacpi_disclose_usertask("procfs wan",
3883 "attempt to %s\n",
3884 state ? "enable" : "disable");
3885 wan_set_radiosw(state, 1);
3888 return 0;
3891 static struct ibm_struct wan_driver_data = {
3892 .name = "wan",
3893 .read = wan_read,
3894 .write = wan_write,
3895 .exit = wan_exit,
3896 .suspend = wan_suspend,
3897 .shutdown = wan_shutdown,
3900 /*************************************************************************
3901 * UWB subdriver
3904 enum {
3905 /* ACPI GUWB/SUWB bits */
3906 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3907 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3910 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3912 static struct rfkill *tpacpi_uwb_rfkill;
3914 static int uwb_get_radiosw(void)
3916 int status;
3918 if (!tp_features.uwb)
3919 return -ENODEV;
3921 /* WLSW overrides UWB in firmware/hardware, reflect that */
3922 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3923 return RFKILL_STATE_HARD_BLOCKED;
3925 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3926 if (dbg_uwbemul)
3927 return (tpacpi_uwb_emulstate) ?
3928 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3929 #endif
3931 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3932 return -EIO;
3934 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3935 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3938 static void uwb_update_rfk(void)
3940 int status;
3942 if (!tpacpi_uwb_rfkill)
3943 return;
3945 status = uwb_get_radiosw();
3946 if (status < 0)
3947 return;
3948 rfkill_force_state(tpacpi_uwb_rfkill, status);
3950 vdbg_printk(TPACPI_DBG_RFKILL,
3951 "forced rfkill state to %d\n",
3952 status);
3955 static int uwb_set_radiosw(int radio_on, int update_rfk)
3957 int status;
3959 if (!tp_features.uwb)
3960 return -ENODEV;
3962 /* WLSW overrides UWB in firmware/hardware, but there is no
3963 * reason to risk weird behaviour. */
3964 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3965 && radio_on)
3966 return -EPERM;
3968 vdbg_printk(TPACPI_DBG_RFKILL,
3969 "will %s UWB\n", radio_on ? "enable" : "disable");
3971 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3972 if (dbg_uwbemul) {
3973 tpacpi_uwb_emulstate = !!radio_on;
3974 if (update_rfk)
3975 uwb_update_rfk();
3976 return 0;
3978 #endif
3980 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3981 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3982 return -EIO;
3984 if (update_rfk)
3985 uwb_update_rfk();
3987 return 0;
3990 /* --------------------------------------------------------------------- */
3992 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3994 int uwbs = uwb_get_radiosw();
3996 if (uwbs < 0)
3997 return uwbs;
3999 *state = uwbs;
4000 return 0;
4003 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4005 dbg_printk(TPACPI_DBG_RFKILL,
4006 "request to change radio state to %d\n", state);
4007 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4010 static void uwb_exit(void)
4012 if (tpacpi_uwb_rfkill)
4013 rfkill_unregister(tpacpi_uwb_rfkill);
4016 static int __init uwb_init(struct ibm_init_struct *iibm)
4018 int res;
4019 int status = 0;
4021 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4022 "initializing uwb subdriver\n");
4024 TPACPI_ACPIHANDLE_INIT(hkey);
4026 tp_features.uwb = hkey_handle &&
4027 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4029 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4030 "uwb is %s, status 0x%02x\n",
4031 str_supported(tp_features.uwb),
4032 status);
4034 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4035 if (dbg_uwbemul) {
4036 tp_features.uwb = 1;
4037 printk(TPACPI_INFO
4038 "uwb switch emulation enabled\n");
4039 } else
4040 #endif
4041 if (tp_features.uwb &&
4042 !(status & TP_ACPI_UWB_HWPRESENT)) {
4043 /* no uwb hardware present in system */
4044 tp_features.uwb = 0;
4045 dbg_printk(TPACPI_DBG_INIT,
4046 "uwb hardware not installed\n");
4049 if (!tp_features.uwb)
4050 return 1;
4052 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4053 &tpacpi_uwb_rfkill,
4054 RFKILL_TYPE_UWB,
4055 TPACPI_RFK_UWB_SW_NAME,
4056 false,
4057 tpacpi_uwb_rfk_set,
4058 tpacpi_uwb_rfk_get);
4060 return res;
4063 static struct ibm_struct uwb_driver_data = {
4064 .name = "uwb",
4065 .exit = uwb_exit,
4066 .flags.experimental = 1,
4069 /*************************************************************************
4070 * Video subdriver
4073 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4075 enum video_access_mode {
4076 TPACPI_VIDEO_NONE = 0,
4077 TPACPI_VIDEO_570, /* 570 */
4078 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4079 TPACPI_VIDEO_NEW, /* all others */
4082 enum { /* video status flags, based on VIDEO_570 */
4083 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4084 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4085 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4088 enum { /* TPACPI_VIDEO_570 constants */
4089 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4090 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4091 * video_status_flags */
4092 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4093 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4096 static enum video_access_mode video_supported;
4097 static int video_orig_autosw;
4099 static int video_autosw_get(void);
4100 static int video_autosw_set(int enable);
4102 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4104 static int __init video_init(struct ibm_init_struct *iibm)
4106 int ivga;
4108 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4110 TPACPI_ACPIHANDLE_INIT(vid);
4111 TPACPI_ACPIHANDLE_INIT(vid2);
4113 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4114 /* G41, assume IVGA doesn't change */
4115 vid_handle = vid2_handle;
4117 if (!vid_handle)
4118 /* video switching not supported on R30, R31 */
4119 video_supported = TPACPI_VIDEO_NONE;
4120 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4121 /* 570 */
4122 video_supported = TPACPI_VIDEO_570;
4123 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4124 /* 600e/x, 770e, 770x */
4125 video_supported = TPACPI_VIDEO_770;
4126 else
4127 /* all others */
4128 video_supported = TPACPI_VIDEO_NEW;
4130 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4131 str_supported(video_supported != TPACPI_VIDEO_NONE),
4132 video_supported);
4134 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4137 static void video_exit(void)
4139 dbg_printk(TPACPI_DBG_EXIT,
4140 "restoring original video autoswitch mode\n");
4141 if (video_autosw_set(video_orig_autosw))
4142 printk(TPACPI_ERR "error while trying to restore original "
4143 "video autoswitch mode\n");
4146 static int video_outputsw_get(void)
4148 int status = 0;
4149 int i;
4151 switch (video_supported) {
4152 case TPACPI_VIDEO_570:
4153 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4154 TP_ACPI_VIDEO_570_PHSCMD))
4155 return -EIO;
4156 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4157 break;
4158 case TPACPI_VIDEO_770:
4159 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4160 return -EIO;
4161 if (i)
4162 status |= TP_ACPI_VIDEO_S_LCD;
4163 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4164 return -EIO;
4165 if (i)
4166 status |= TP_ACPI_VIDEO_S_CRT;
4167 break;
4168 case TPACPI_VIDEO_NEW:
4169 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4170 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4171 return -EIO;
4172 if (i)
4173 status |= TP_ACPI_VIDEO_S_CRT;
4175 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4176 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4177 return -EIO;
4178 if (i)
4179 status |= TP_ACPI_VIDEO_S_LCD;
4180 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4181 return -EIO;
4182 if (i)
4183 status |= TP_ACPI_VIDEO_S_DVI;
4184 break;
4185 default:
4186 return -ENOSYS;
4189 return status;
4192 static int video_outputsw_set(int status)
4194 int autosw;
4195 int res = 0;
4197 switch (video_supported) {
4198 case TPACPI_VIDEO_570:
4199 res = acpi_evalf(NULL, NULL,
4200 "\\_SB.PHS2", "vdd",
4201 TP_ACPI_VIDEO_570_PHS2CMD,
4202 status | TP_ACPI_VIDEO_570_PHS2SET);
4203 break;
4204 case TPACPI_VIDEO_770:
4205 autosw = video_autosw_get();
4206 if (autosw < 0)
4207 return autosw;
4209 res = video_autosw_set(1);
4210 if (res)
4211 return res;
4212 res = acpi_evalf(vid_handle, NULL,
4213 "ASWT", "vdd", status * 0x100, 0);
4214 if (!autosw && video_autosw_set(autosw)) {
4215 printk(TPACPI_ERR
4216 "video auto-switch left enabled due to error\n");
4217 return -EIO;
4219 break;
4220 case TPACPI_VIDEO_NEW:
4221 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4222 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4223 break;
4224 default:
4225 return -ENOSYS;
4228 return (res)? 0 : -EIO;
4231 static int video_autosw_get(void)
4233 int autosw = 0;
4235 switch (video_supported) {
4236 case TPACPI_VIDEO_570:
4237 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4238 return -EIO;
4239 break;
4240 case TPACPI_VIDEO_770:
4241 case TPACPI_VIDEO_NEW:
4242 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4243 return -EIO;
4244 break;
4245 default:
4246 return -ENOSYS;
4249 return autosw & 1;
4252 static int video_autosw_set(int enable)
4254 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4255 return -EIO;
4256 return 0;
4259 static int video_outputsw_cycle(void)
4261 int autosw = video_autosw_get();
4262 int res;
4264 if (autosw < 0)
4265 return autosw;
4267 switch (video_supported) {
4268 case TPACPI_VIDEO_570:
4269 res = video_autosw_set(1);
4270 if (res)
4271 return res;
4272 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4273 break;
4274 case TPACPI_VIDEO_770:
4275 case TPACPI_VIDEO_NEW:
4276 res = video_autosw_set(1);
4277 if (res)
4278 return res;
4279 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4280 break;
4281 default:
4282 return -ENOSYS;
4284 if (!autosw && video_autosw_set(autosw)) {
4285 printk(TPACPI_ERR
4286 "video auto-switch left enabled due to error\n");
4287 return -EIO;
4290 return (res)? 0 : -EIO;
4293 static int video_expand_toggle(void)
4295 switch (video_supported) {
4296 case TPACPI_VIDEO_570:
4297 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4298 0 : -EIO;
4299 case TPACPI_VIDEO_770:
4300 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4301 0 : -EIO;
4302 case TPACPI_VIDEO_NEW:
4303 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4304 0 : -EIO;
4305 default:
4306 return -ENOSYS;
4308 /* not reached */
4311 static int video_read(char *p)
4313 int status, autosw;
4314 int len = 0;
4316 if (video_supported == TPACPI_VIDEO_NONE) {
4317 len += sprintf(p + len, "status:\t\tnot supported\n");
4318 return len;
4321 status = video_outputsw_get();
4322 if (status < 0)
4323 return status;
4325 autosw = video_autosw_get();
4326 if (autosw < 0)
4327 return autosw;
4329 len += sprintf(p + len, "status:\t\tsupported\n");
4330 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4331 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4332 if (video_supported == TPACPI_VIDEO_NEW)
4333 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4334 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4335 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4336 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4337 if (video_supported == TPACPI_VIDEO_NEW)
4338 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4339 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4340 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4342 return len;
4345 static int video_write(char *buf)
4347 char *cmd;
4348 int enable, disable, status;
4349 int res;
4351 if (video_supported == TPACPI_VIDEO_NONE)
4352 return -ENODEV;
4354 enable = 0;
4355 disable = 0;
4357 while ((cmd = next_cmd(&buf))) {
4358 if (strlencmp(cmd, "lcd_enable") == 0) {
4359 enable |= TP_ACPI_VIDEO_S_LCD;
4360 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4361 disable |= TP_ACPI_VIDEO_S_LCD;
4362 } else if (strlencmp(cmd, "crt_enable") == 0) {
4363 enable |= TP_ACPI_VIDEO_S_CRT;
4364 } else if (strlencmp(cmd, "crt_disable") == 0) {
4365 disable |= TP_ACPI_VIDEO_S_CRT;
4366 } else if (video_supported == TPACPI_VIDEO_NEW &&
4367 strlencmp(cmd, "dvi_enable") == 0) {
4368 enable |= TP_ACPI_VIDEO_S_DVI;
4369 } else if (video_supported == TPACPI_VIDEO_NEW &&
4370 strlencmp(cmd, "dvi_disable") == 0) {
4371 disable |= TP_ACPI_VIDEO_S_DVI;
4372 } else if (strlencmp(cmd, "auto_enable") == 0) {
4373 res = video_autosw_set(1);
4374 if (res)
4375 return res;
4376 } else if (strlencmp(cmd, "auto_disable") == 0) {
4377 res = video_autosw_set(0);
4378 if (res)
4379 return res;
4380 } else if (strlencmp(cmd, "video_switch") == 0) {
4381 res = video_outputsw_cycle();
4382 if (res)
4383 return res;
4384 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4385 res = video_expand_toggle();
4386 if (res)
4387 return res;
4388 } else
4389 return -EINVAL;
4392 if (enable || disable) {
4393 status = video_outputsw_get();
4394 if (status < 0)
4395 return status;
4396 res = video_outputsw_set((status & ~disable) | enable);
4397 if (res)
4398 return res;
4401 return 0;
4404 static struct ibm_struct video_driver_data = {
4405 .name = "video",
4406 .read = video_read,
4407 .write = video_write,
4408 .exit = video_exit,
4411 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4413 /*************************************************************************
4414 * Light (thinklight) subdriver
4417 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4418 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4420 static int light_get_status(void)
4422 int status = 0;
4424 if (tp_features.light_status) {
4425 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4426 return -EIO;
4427 return (!!status);
4430 return -ENXIO;
4433 static int light_set_status(int status)
4435 int rc;
4437 if (tp_features.light) {
4438 if (cmos_handle) {
4439 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4440 (status)?
4441 TP_CMOS_THINKLIGHT_ON :
4442 TP_CMOS_THINKLIGHT_OFF);
4443 } else {
4444 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4445 (status)? 1 : 0);
4447 return (rc)? 0 : -EIO;
4450 return -ENXIO;
4453 static void light_set_status_worker(struct work_struct *work)
4455 struct tpacpi_led_classdev *data =
4456 container_of(work, struct tpacpi_led_classdev, work);
4458 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4459 light_set_status((data->new_state != TPACPI_LED_OFF));
4462 static void light_sysfs_set(struct led_classdev *led_cdev,
4463 enum led_brightness brightness)
4465 struct tpacpi_led_classdev *data =
4466 container_of(led_cdev,
4467 struct tpacpi_led_classdev,
4468 led_classdev);
4469 data->new_state = (brightness != LED_OFF) ?
4470 TPACPI_LED_ON : TPACPI_LED_OFF;
4471 queue_work(tpacpi_wq, &data->work);
4474 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4476 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4479 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4480 .led_classdev = {
4481 .name = "tpacpi::thinklight",
4482 .brightness_set = &light_sysfs_set,
4483 .brightness_get = &light_sysfs_get,
4487 static int __init light_init(struct ibm_init_struct *iibm)
4489 int rc;
4491 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4493 TPACPI_ACPIHANDLE_INIT(ledb);
4494 TPACPI_ACPIHANDLE_INIT(lght);
4495 TPACPI_ACPIHANDLE_INIT(cmos);
4496 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4498 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4499 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4501 if (tp_features.light)
4502 /* light status not supported on
4503 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4504 tp_features.light_status =
4505 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4507 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4508 str_supported(tp_features.light),
4509 str_supported(tp_features.light_status));
4511 if (!tp_features.light)
4512 return 1;
4514 rc = led_classdev_register(&tpacpi_pdev->dev,
4515 &tpacpi_led_thinklight.led_classdev);
4517 if (rc < 0) {
4518 tp_features.light = 0;
4519 tp_features.light_status = 0;
4520 } else {
4521 rc = 0;
4524 return rc;
4527 static void light_exit(void)
4529 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4530 if (work_pending(&tpacpi_led_thinklight.work))
4531 flush_workqueue(tpacpi_wq);
4534 static int light_read(char *p)
4536 int len = 0;
4537 int status;
4539 if (!tp_features.light) {
4540 len += sprintf(p + len, "status:\t\tnot supported\n");
4541 } else if (!tp_features.light_status) {
4542 len += sprintf(p + len, "status:\t\tunknown\n");
4543 len += sprintf(p + len, "commands:\ton, off\n");
4544 } else {
4545 status = light_get_status();
4546 if (status < 0)
4547 return status;
4548 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4549 len += sprintf(p + len, "commands:\ton, off\n");
4552 return len;
4555 static int light_write(char *buf)
4557 char *cmd;
4558 int newstatus = 0;
4560 if (!tp_features.light)
4561 return -ENODEV;
4563 while ((cmd = next_cmd(&buf))) {
4564 if (strlencmp(cmd, "on") == 0) {
4565 newstatus = 1;
4566 } else if (strlencmp(cmd, "off") == 0) {
4567 newstatus = 0;
4568 } else
4569 return -EINVAL;
4572 return light_set_status(newstatus);
4575 static struct ibm_struct light_driver_data = {
4576 .name = "light",
4577 .read = light_read,
4578 .write = light_write,
4579 .exit = light_exit,
4582 /*************************************************************************
4583 * CMOS subdriver
4586 /* sysfs cmos_command -------------------------------------------------- */
4587 static ssize_t cmos_command_store(struct device *dev,
4588 struct device_attribute *attr,
4589 const char *buf, size_t count)
4591 unsigned long cmos_cmd;
4592 int res;
4594 if (parse_strtoul(buf, 21, &cmos_cmd))
4595 return -EINVAL;
4597 res = issue_thinkpad_cmos_command(cmos_cmd);
4598 return (res)? res : count;
4601 static struct device_attribute dev_attr_cmos_command =
4602 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4604 /* --------------------------------------------------------------------- */
4606 static int __init cmos_init(struct ibm_init_struct *iibm)
4608 int res;
4610 vdbg_printk(TPACPI_DBG_INIT,
4611 "initializing cmos commands subdriver\n");
4613 TPACPI_ACPIHANDLE_INIT(cmos);
4615 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4616 str_supported(cmos_handle != NULL));
4618 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4619 if (res)
4620 return res;
4622 return (cmos_handle)? 0 : 1;
4625 static void cmos_exit(void)
4627 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4630 static int cmos_read(char *p)
4632 int len = 0;
4634 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4635 R30, R31, T20-22, X20-21 */
4636 if (!cmos_handle)
4637 len += sprintf(p + len, "status:\t\tnot supported\n");
4638 else {
4639 len += sprintf(p + len, "status:\t\tsupported\n");
4640 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4643 return len;
4646 static int cmos_write(char *buf)
4648 char *cmd;
4649 int cmos_cmd, res;
4651 while ((cmd = next_cmd(&buf))) {
4652 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4653 cmos_cmd >= 0 && cmos_cmd <= 21) {
4654 /* cmos_cmd set */
4655 } else
4656 return -EINVAL;
4658 res = issue_thinkpad_cmos_command(cmos_cmd);
4659 if (res)
4660 return res;
4663 return 0;
4666 static struct ibm_struct cmos_driver_data = {
4667 .name = "cmos",
4668 .read = cmos_read,
4669 .write = cmos_write,
4670 .exit = cmos_exit,
4673 /*************************************************************************
4674 * LED subdriver
4677 enum led_access_mode {
4678 TPACPI_LED_NONE = 0,
4679 TPACPI_LED_570, /* 570 */
4680 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4681 TPACPI_LED_NEW, /* all others */
4684 enum { /* For TPACPI_LED_OLD */
4685 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4686 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4687 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4690 static enum led_access_mode led_supported;
4692 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4693 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4694 /* T20-22, X20-21 */
4695 "LED", /* all others */
4696 ); /* R30, R31 */
4698 #define TPACPI_LED_NUMLEDS 16
4699 static struct tpacpi_led_classdev *tpacpi_leds;
4700 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4701 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4702 /* there's a limit of 19 chars + NULL before 2.6.26 */
4703 "tpacpi::power",
4704 "tpacpi:orange:batt",
4705 "tpacpi:green:batt",
4706 "tpacpi::dock_active",
4707 "tpacpi::bay_active",
4708 "tpacpi::dock_batt",
4709 "tpacpi::unknown_led",
4710 "tpacpi::standby",
4711 "tpacpi::dock_status1",
4712 "tpacpi::dock_status2",
4713 "tpacpi::unknown_led2",
4714 "tpacpi::unknown_led3",
4715 "tpacpi::thinkvantage",
4717 #define TPACPI_SAFE_LEDS 0x1081U
4719 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4721 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4722 return false;
4723 #else
4724 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4725 #endif
4728 static int led_get_status(const unsigned int led)
4730 int status;
4731 enum led_status_t led_s;
4733 switch (led_supported) {
4734 case TPACPI_LED_570:
4735 if (!acpi_evalf(ec_handle,
4736 &status, "GLED", "dd", 1 << led))
4737 return -EIO;
4738 led_s = (status == 0)?
4739 TPACPI_LED_OFF :
4740 ((status == 1)?
4741 TPACPI_LED_ON :
4742 TPACPI_LED_BLINK);
4743 tpacpi_led_state_cache[led] = led_s;
4744 return led_s;
4745 default:
4746 return -ENXIO;
4749 /* not reached */
4752 static int led_set_status(const unsigned int led,
4753 const enum led_status_t ledstatus)
4755 /* off, on, blink. Index is led_status_t */
4756 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4757 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4759 int rc = 0;
4761 switch (led_supported) {
4762 case TPACPI_LED_570:
4763 /* 570 */
4764 if (unlikely(led > 7))
4765 return -EINVAL;
4766 if (unlikely(tpacpi_is_led_restricted(led)))
4767 return -EPERM;
4768 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4769 (1 << led), led_sled_arg1[ledstatus]))
4770 rc = -EIO;
4771 break;
4772 case TPACPI_LED_OLD:
4773 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4774 if (unlikely(led > 7))
4775 return -EINVAL;
4776 if (unlikely(tpacpi_is_led_restricted(led)))
4777 return -EPERM;
4778 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4779 if (rc >= 0)
4780 rc = ec_write(TPACPI_LED_EC_HLBL,
4781 (ledstatus == TPACPI_LED_BLINK) << led);
4782 if (rc >= 0)
4783 rc = ec_write(TPACPI_LED_EC_HLCL,
4784 (ledstatus != TPACPI_LED_OFF) << led);
4785 break;
4786 case TPACPI_LED_NEW:
4787 /* all others */
4788 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4789 return -EINVAL;
4790 if (unlikely(tpacpi_is_led_restricted(led)))
4791 return -EPERM;
4792 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4793 led, led_led_arg1[ledstatus]))
4794 rc = -EIO;
4795 break;
4796 default:
4797 rc = -ENXIO;
4800 if (!rc)
4801 tpacpi_led_state_cache[led] = ledstatus;
4803 return rc;
4806 static void led_set_status_worker(struct work_struct *work)
4808 struct tpacpi_led_classdev *data =
4809 container_of(work, struct tpacpi_led_classdev, work);
4811 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4812 led_set_status(data->led, data->new_state);
4815 static void led_sysfs_set(struct led_classdev *led_cdev,
4816 enum led_brightness brightness)
4818 struct tpacpi_led_classdev *data = container_of(led_cdev,
4819 struct tpacpi_led_classdev, led_classdev);
4821 if (brightness == LED_OFF)
4822 data->new_state = TPACPI_LED_OFF;
4823 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4824 data->new_state = TPACPI_LED_ON;
4825 else
4826 data->new_state = TPACPI_LED_BLINK;
4828 queue_work(tpacpi_wq, &data->work);
4831 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4832 unsigned long *delay_on, unsigned long *delay_off)
4834 struct tpacpi_led_classdev *data = container_of(led_cdev,
4835 struct tpacpi_led_classdev, led_classdev);
4837 /* Can we choose the flash rate? */
4838 if (*delay_on == 0 && *delay_off == 0) {
4839 /* yes. set them to the hardware blink rate (1 Hz) */
4840 *delay_on = 500; /* ms */
4841 *delay_off = 500; /* ms */
4842 } else if ((*delay_on != 500) || (*delay_off != 500))
4843 return -EINVAL;
4845 data->new_state = TPACPI_LED_BLINK;
4846 queue_work(tpacpi_wq, &data->work);
4848 return 0;
4851 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4853 int rc;
4855 struct tpacpi_led_classdev *data = container_of(led_cdev,
4856 struct tpacpi_led_classdev, led_classdev);
4858 rc = led_get_status(data->led);
4860 if (rc == TPACPI_LED_OFF || rc < 0)
4861 rc = LED_OFF; /* no error handling in led class :( */
4862 else
4863 rc = LED_FULL;
4865 return rc;
4868 static void led_exit(void)
4870 unsigned int i;
4872 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4873 if (tpacpi_leds[i].led_classdev.name)
4874 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4877 kfree(tpacpi_leds);
4880 static int __init tpacpi_init_led(unsigned int led)
4882 int rc;
4884 tpacpi_leds[led].led = led;
4886 /* LEDs with no name don't get registered */
4887 if (!tpacpi_led_names[led])
4888 return 0;
4890 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4891 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4892 if (led_supported == TPACPI_LED_570)
4893 tpacpi_leds[led].led_classdev.brightness_get =
4894 &led_sysfs_get;
4896 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4898 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4900 rc = led_classdev_register(&tpacpi_pdev->dev,
4901 &tpacpi_leds[led].led_classdev);
4902 if (rc < 0)
4903 tpacpi_leds[led].led_classdev.name = NULL;
4905 return rc;
4908 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4909 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4910 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4911 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4913 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4914 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4915 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4916 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4917 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4918 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4919 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4920 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4922 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4923 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4924 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4925 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4926 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4928 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4929 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4930 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4931 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4933 /* (1) - may have excess leds enabled on MSB */
4935 /* Defaults (order matters, keep last, don't reorder!) */
4936 { /* Lenovo */
4937 .vendor = PCI_VENDOR_ID_LENOVO,
4938 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4939 .quirks = 0x1fffU,
4941 { /* IBM ThinkPads with no EC version string */
4942 .vendor = PCI_VENDOR_ID_IBM,
4943 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4944 .quirks = 0x00ffU,
4946 { /* IBM ThinkPads with EC version string */
4947 .vendor = PCI_VENDOR_ID_IBM,
4948 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4949 .quirks = 0x00bfU,
4953 #undef TPACPI_LEDQ_IBM
4954 #undef TPACPI_LEDQ_LNV
4956 static int __init led_init(struct ibm_init_struct *iibm)
4958 unsigned int i;
4959 int rc;
4960 unsigned long useful_leds;
4962 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4964 TPACPI_ACPIHANDLE_INIT(led);
4966 if (!led_handle)
4967 /* led not supported on R30, R31 */
4968 led_supported = TPACPI_LED_NONE;
4969 else if (strlencmp(led_path, "SLED") == 0)
4970 /* 570 */
4971 led_supported = TPACPI_LED_570;
4972 else if (strlencmp(led_path, "SYSL") == 0)
4973 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4974 led_supported = TPACPI_LED_OLD;
4975 else
4976 /* all others */
4977 led_supported = TPACPI_LED_NEW;
4979 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4980 str_supported(led_supported), led_supported);
4982 if (led_supported == TPACPI_LED_NONE)
4983 return 1;
4985 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4986 GFP_KERNEL);
4987 if (!tpacpi_leds) {
4988 printk(TPACPI_ERR "Out of memory for LED data\n");
4989 return -ENOMEM;
4992 useful_leds = tpacpi_check_quirks(led_useful_qtable,
4993 ARRAY_SIZE(led_useful_qtable));
4995 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4996 if (!tpacpi_is_led_restricted(i) &&
4997 test_bit(i, &useful_leds)) {
4998 rc = tpacpi_init_led(i);
4999 if (rc < 0) {
5000 led_exit();
5001 return rc;
5006 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5007 printk(TPACPI_NOTICE
5008 "warning: userspace override of important "
5009 "firmware LEDs is enabled\n");
5010 #endif
5011 return 0;
5014 #define str_led_status(s) \
5015 ((s) == TPACPI_LED_OFF ? "off" : \
5016 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5018 static int led_read(char *p)
5020 int len = 0;
5022 if (!led_supported) {
5023 len += sprintf(p + len, "status:\t\tnot supported\n");
5024 return len;
5026 len += sprintf(p + len, "status:\t\tsupported\n");
5028 if (led_supported == TPACPI_LED_570) {
5029 /* 570 */
5030 int i, status;
5031 for (i = 0; i < 8; i++) {
5032 status = led_get_status(i);
5033 if (status < 0)
5034 return -EIO;
5035 len += sprintf(p + len, "%d:\t\t%s\n",
5036 i, str_led_status(status));
5040 len += sprintf(p + len, "commands:\t"
5041 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5043 return len;
5046 static int led_write(char *buf)
5048 char *cmd;
5049 int led, rc;
5050 enum led_status_t s;
5052 if (!led_supported)
5053 return -ENODEV;
5055 while ((cmd = next_cmd(&buf))) {
5056 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5057 return -EINVAL;
5059 if (strstr(cmd, "off")) {
5060 s = TPACPI_LED_OFF;
5061 } else if (strstr(cmd, "on")) {
5062 s = TPACPI_LED_ON;
5063 } else if (strstr(cmd, "blink")) {
5064 s = TPACPI_LED_BLINK;
5065 } else {
5066 return -EINVAL;
5069 rc = led_set_status(led, s);
5070 if (rc < 0)
5071 return rc;
5074 return 0;
5077 static struct ibm_struct led_driver_data = {
5078 .name = "led",
5079 .read = led_read,
5080 .write = led_write,
5081 .exit = led_exit,
5084 /*************************************************************************
5085 * Beep subdriver
5088 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5090 #define TPACPI_BEEP_Q1 0x0001
5092 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5093 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5094 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5097 static int __init beep_init(struct ibm_init_struct *iibm)
5099 unsigned long quirks;
5101 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5103 TPACPI_ACPIHANDLE_INIT(beep);
5105 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5106 str_supported(beep_handle != NULL));
5108 quirks = tpacpi_check_quirks(beep_quirk_table,
5109 ARRAY_SIZE(beep_quirk_table));
5111 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5113 return (beep_handle)? 0 : 1;
5116 static int beep_read(char *p)
5118 int len = 0;
5120 if (!beep_handle)
5121 len += sprintf(p + len, "status:\t\tnot supported\n");
5122 else {
5123 len += sprintf(p + len, "status:\t\tsupported\n");
5124 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5127 return len;
5130 static int beep_write(char *buf)
5132 char *cmd;
5133 int beep_cmd;
5135 if (!beep_handle)
5136 return -ENODEV;
5138 while ((cmd = next_cmd(&buf))) {
5139 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5140 beep_cmd >= 0 && beep_cmd <= 17) {
5141 /* beep_cmd set */
5142 } else
5143 return -EINVAL;
5144 if (tp_features.beep_needs_two_args) {
5145 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5146 beep_cmd, 0))
5147 return -EIO;
5148 } else {
5149 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5150 beep_cmd))
5151 return -EIO;
5155 return 0;
5158 static struct ibm_struct beep_driver_data = {
5159 .name = "beep",
5160 .read = beep_read,
5161 .write = beep_write,
5164 /*************************************************************************
5165 * Thermal subdriver
5168 enum thermal_access_mode {
5169 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5170 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5171 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5172 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5173 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5176 enum { /* TPACPI_THERMAL_TPEC_* */
5177 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5178 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5179 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5182 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5183 struct ibm_thermal_sensors_struct {
5184 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5187 static enum thermal_access_mode thermal_read_mode;
5189 /* idx is zero-based */
5190 static int thermal_get_sensor(int idx, s32 *value)
5192 int t;
5193 s8 tmp;
5194 char tmpi[5];
5196 t = TP_EC_THERMAL_TMP0;
5198 switch (thermal_read_mode) {
5199 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5200 case TPACPI_THERMAL_TPEC_16:
5201 if (idx >= 8 && idx <= 15) {
5202 t = TP_EC_THERMAL_TMP8;
5203 idx -= 8;
5205 /* fallthrough */
5206 #endif
5207 case TPACPI_THERMAL_TPEC_8:
5208 if (idx <= 7) {
5209 if (!acpi_ec_read(t + idx, &tmp))
5210 return -EIO;
5211 *value = tmp * 1000;
5212 return 0;
5214 break;
5216 case TPACPI_THERMAL_ACPI_UPDT:
5217 if (idx <= 7) {
5218 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5219 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5220 return -EIO;
5221 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5222 return -EIO;
5223 *value = (t - 2732) * 100;
5224 return 0;
5226 break;
5228 case TPACPI_THERMAL_ACPI_TMP07:
5229 if (idx <= 7) {
5230 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5231 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5232 return -EIO;
5233 if (t > 127 || t < -127)
5234 t = TP_EC_THERMAL_TMP_NA;
5235 *value = t * 1000;
5236 return 0;
5238 break;
5240 case TPACPI_THERMAL_NONE:
5241 default:
5242 return -ENOSYS;
5245 return -EINVAL;
5248 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5250 int res, i;
5251 int n;
5253 n = 8;
5254 i = 0;
5256 if (!s)
5257 return -EINVAL;
5259 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5260 n = 16;
5262 for (i = 0 ; i < n; i++) {
5263 res = thermal_get_sensor(i, &s->temp[i]);
5264 if (res)
5265 return res;
5268 return n;
5271 /* sysfs temp##_input -------------------------------------------------- */
5273 static ssize_t thermal_temp_input_show(struct device *dev,
5274 struct device_attribute *attr,
5275 char *buf)
5277 struct sensor_device_attribute *sensor_attr =
5278 to_sensor_dev_attr(attr);
5279 int idx = sensor_attr->index;
5280 s32 value;
5281 int res;
5283 res = thermal_get_sensor(idx, &value);
5284 if (res)
5285 return res;
5286 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5287 return -ENXIO;
5289 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5292 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5293 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5294 thermal_temp_input_show, NULL, _idxB)
5296 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5297 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5298 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5299 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5300 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5301 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5302 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5303 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5304 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5305 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5306 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5307 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5308 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5309 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5310 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5311 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5312 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5315 #define THERMAL_ATTRS(X) \
5316 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5318 static struct attribute *thermal_temp_input_attr[] = {
5319 THERMAL_ATTRS(8),
5320 THERMAL_ATTRS(9),
5321 THERMAL_ATTRS(10),
5322 THERMAL_ATTRS(11),
5323 THERMAL_ATTRS(12),
5324 THERMAL_ATTRS(13),
5325 THERMAL_ATTRS(14),
5326 THERMAL_ATTRS(15),
5327 THERMAL_ATTRS(0),
5328 THERMAL_ATTRS(1),
5329 THERMAL_ATTRS(2),
5330 THERMAL_ATTRS(3),
5331 THERMAL_ATTRS(4),
5332 THERMAL_ATTRS(5),
5333 THERMAL_ATTRS(6),
5334 THERMAL_ATTRS(7),
5335 NULL
5338 static const struct attribute_group thermal_temp_input16_group = {
5339 .attrs = thermal_temp_input_attr
5342 static const struct attribute_group thermal_temp_input8_group = {
5343 .attrs = &thermal_temp_input_attr[8]
5346 #undef THERMAL_SENSOR_ATTR_TEMP
5347 #undef THERMAL_ATTRS
5349 /* --------------------------------------------------------------------- */
5351 static int __init thermal_init(struct ibm_init_struct *iibm)
5353 u8 t, ta1, ta2;
5354 int i;
5355 int acpi_tmp7;
5356 int res;
5358 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5360 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5362 if (thinkpad_id.ec_model) {
5364 * Direct EC access mode: sensors at registers
5365 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5366 * non-implemented, thermal sensors return 0x80 when
5367 * not available
5370 ta1 = ta2 = 0;
5371 for (i = 0; i < 8; i++) {
5372 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5373 ta1 |= t;
5374 } else {
5375 ta1 = 0;
5376 break;
5378 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5379 ta2 |= t;
5380 } else {
5381 ta1 = 0;
5382 break;
5385 if (ta1 == 0) {
5386 /* This is sheer paranoia, but we handle it anyway */
5387 if (acpi_tmp7) {
5388 printk(TPACPI_ERR
5389 "ThinkPad ACPI EC access misbehaving, "
5390 "falling back to ACPI TMPx access "
5391 "mode\n");
5392 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5393 } else {
5394 printk(TPACPI_ERR
5395 "ThinkPad ACPI EC access misbehaving, "
5396 "disabling thermal sensors access\n");
5397 thermal_read_mode = TPACPI_THERMAL_NONE;
5399 } else {
5400 thermal_read_mode =
5401 (ta2 != 0) ?
5402 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5404 } else if (acpi_tmp7) {
5405 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5406 /* 600e/x, 770e, 770x */
5407 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5408 } else {
5409 /* Standard ACPI TMPx access, max 8 sensors */
5410 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5412 } else {
5413 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5414 thermal_read_mode = TPACPI_THERMAL_NONE;
5417 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5418 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5419 thermal_read_mode);
5421 switch (thermal_read_mode) {
5422 case TPACPI_THERMAL_TPEC_16:
5423 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5424 &thermal_temp_input16_group);
5425 if (res)
5426 return res;
5427 break;
5428 case TPACPI_THERMAL_TPEC_8:
5429 case TPACPI_THERMAL_ACPI_TMP07:
5430 case TPACPI_THERMAL_ACPI_UPDT:
5431 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5432 &thermal_temp_input8_group);
5433 if (res)
5434 return res;
5435 break;
5436 case TPACPI_THERMAL_NONE:
5437 default:
5438 return 1;
5441 return 0;
5444 static void thermal_exit(void)
5446 switch (thermal_read_mode) {
5447 case TPACPI_THERMAL_TPEC_16:
5448 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5449 &thermal_temp_input16_group);
5450 break;
5451 case TPACPI_THERMAL_TPEC_8:
5452 case TPACPI_THERMAL_ACPI_TMP07:
5453 case TPACPI_THERMAL_ACPI_UPDT:
5454 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5455 &thermal_temp_input16_group);
5456 break;
5457 case TPACPI_THERMAL_NONE:
5458 default:
5459 break;
5463 static int thermal_read(char *p)
5465 int len = 0;
5466 int n, i;
5467 struct ibm_thermal_sensors_struct t;
5469 n = thermal_get_sensors(&t);
5470 if (unlikely(n < 0))
5471 return n;
5473 len += sprintf(p + len, "temperatures:\t");
5475 if (n > 0) {
5476 for (i = 0; i < (n - 1); i++)
5477 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5478 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5479 } else
5480 len += sprintf(p + len, "not supported\n");
5482 return len;
5485 static struct ibm_struct thermal_driver_data = {
5486 .name = "thermal",
5487 .read = thermal_read,
5488 .exit = thermal_exit,
5491 /*************************************************************************
5492 * EC Dump subdriver
5495 static u8 ecdump_regs[256];
5497 static int ecdump_read(char *p)
5499 int len = 0;
5500 int i, j;
5501 u8 v;
5503 len += sprintf(p + len, "EC "
5504 " +00 +01 +02 +03 +04 +05 +06 +07"
5505 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5506 for (i = 0; i < 256; i += 16) {
5507 len += sprintf(p + len, "EC 0x%02x:", i);
5508 for (j = 0; j < 16; j++) {
5509 if (!acpi_ec_read(i + j, &v))
5510 break;
5511 if (v != ecdump_regs[i + j])
5512 len += sprintf(p + len, " *%02x", v);
5513 else
5514 len += sprintf(p + len, " %02x", v);
5515 ecdump_regs[i + j] = v;
5517 len += sprintf(p + len, "\n");
5518 if (j != 16)
5519 break;
5522 /* These are way too dangerous to advertise openly... */
5523 #if 0
5524 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5525 " (<offset> is 00-ff, <value> is 00-ff)\n");
5526 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5527 " (<offset> is 00-ff, <value> is 0-255)\n");
5528 #endif
5529 return len;
5532 static int ecdump_write(char *buf)
5534 char *cmd;
5535 int i, v;
5537 while ((cmd = next_cmd(&buf))) {
5538 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5539 /* i and v set */
5540 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5541 /* i and v set */
5542 } else
5543 return -EINVAL;
5544 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5545 if (!acpi_ec_write(i, v))
5546 return -EIO;
5547 } else
5548 return -EINVAL;
5551 return 0;
5554 static struct ibm_struct ecdump_driver_data = {
5555 .name = "ecdump",
5556 .read = ecdump_read,
5557 .write = ecdump_write,
5558 .flags.experimental = 1,
5561 /*************************************************************************
5562 * Backlight/brightness subdriver
5565 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5568 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5569 * CMOS NVRAM byte 0x5E, bits 0-3.
5571 * EC HBRV (0x31) has the following layout
5572 * Bit 7: unknown function
5573 * Bit 6: unknown function
5574 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5575 * Bit 4: must be set to zero to avoid problems
5576 * Bit 3-0: backlight brightness level
5578 * brightness_get_raw returns status data in the HBRV layout
5580 * WARNING: The X61 has been verified to use HBRV for something else, so
5581 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5582 * testing on the very early *60 Lenovo models...
5585 enum {
5586 TP_EC_BACKLIGHT = 0x31,
5588 /* TP_EC_BACKLIGHT bitmasks */
5589 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5590 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5591 TP_EC_BACKLIGHT_MAPSW = 0x20,
5594 enum tpacpi_brightness_access_mode {
5595 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5596 TPACPI_BRGHT_MODE_EC, /* EC control */
5597 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5598 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5599 TPACPI_BRGHT_MODE_MAX
5602 static struct backlight_device *ibm_backlight_device;
5604 static enum tpacpi_brightness_access_mode brightness_mode =
5605 TPACPI_BRGHT_MODE_MAX;
5607 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5609 static struct mutex brightness_mutex;
5611 /* NVRAM brightness access,
5612 * call with brightness_mutex held! */
5613 static unsigned int tpacpi_brightness_nvram_get(void)
5615 u8 lnvram;
5617 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5618 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5619 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5620 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5622 return lnvram;
5625 static void tpacpi_brightness_checkpoint_nvram(void)
5627 u8 lec = 0;
5628 u8 b_nvram;
5630 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5631 return;
5633 vdbg_printk(TPACPI_DBG_BRGHT,
5634 "trying to checkpoint backlight level to NVRAM...\n");
5636 if (mutex_lock_killable(&brightness_mutex) < 0)
5637 return;
5639 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5640 goto unlock;
5641 lec &= TP_EC_BACKLIGHT_LVLMSK;
5642 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5644 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5645 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5646 /* NVRAM needs update */
5647 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5648 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5649 b_nvram |= lec;
5650 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5651 dbg_printk(TPACPI_DBG_BRGHT,
5652 "updated NVRAM backlight level to %u (0x%02x)\n",
5653 (unsigned int) lec, (unsigned int) b_nvram);
5654 } else
5655 vdbg_printk(TPACPI_DBG_BRGHT,
5656 "NVRAM backlight level already is %u (0x%02x)\n",
5657 (unsigned int) lec, (unsigned int) b_nvram);
5659 unlock:
5660 mutex_unlock(&brightness_mutex);
5664 /* call with brightness_mutex held! */
5665 static int tpacpi_brightness_get_raw(int *status)
5667 u8 lec = 0;
5669 switch (brightness_mode) {
5670 case TPACPI_BRGHT_MODE_UCMS_STEP:
5671 *status = tpacpi_brightness_nvram_get();
5672 return 0;
5673 case TPACPI_BRGHT_MODE_EC:
5674 case TPACPI_BRGHT_MODE_ECNVRAM:
5675 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5676 return -EIO;
5677 *status = lec;
5678 return 0;
5679 default:
5680 return -ENXIO;
5684 /* call with brightness_mutex held! */
5685 /* do NOT call with illegal backlight level value */
5686 static int tpacpi_brightness_set_ec(unsigned int value)
5688 u8 lec = 0;
5690 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5691 return -EIO;
5693 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5694 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5695 (value & TP_EC_BACKLIGHT_LVLMSK))))
5696 return -EIO;
5698 return 0;
5701 /* call with brightness_mutex held! */
5702 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5704 int cmos_cmd, inc;
5705 unsigned int current_value, i;
5707 current_value = tpacpi_brightness_nvram_get();
5709 if (value == current_value)
5710 return 0;
5712 cmos_cmd = (value > current_value) ?
5713 TP_CMOS_BRIGHTNESS_UP :
5714 TP_CMOS_BRIGHTNESS_DOWN;
5715 inc = (value > current_value) ? 1 : -1;
5717 for (i = current_value; i != value; i += inc)
5718 if (issue_thinkpad_cmos_command(cmos_cmd))
5719 return -EIO;
5721 return 0;
5724 /* May return EINTR which can always be mapped to ERESTARTSYS */
5725 static int brightness_set(unsigned int value)
5727 int res;
5729 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5730 value < 0)
5731 return -EINVAL;
5733 vdbg_printk(TPACPI_DBG_BRGHT,
5734 "set backlight level to %d\n", value);
5736 res = mutex_lock_killable(&brightness_mutex);
5737 if (res < 0)
5738 return res;
5740 switch (brightness_mode) {
5741 case TPACPI_BRGHT_MODE_EC:
5742 case TPACPI_BRGHT_MODE_ECNVRAM:
5743 res = tpacpi_brightness_set_ec(value);
5744 break;
5745 case TPACPI_BRGHT_MODE_UCMS_STEP:
5746 res = tpacpi_brightness_set_ucmsstep(value);
5747 break;
5748 default:
5749 res = -ENXIO;
5752 mutex_unlock(&brightness_mutex);
5753 return res;
5756 /* sysfs backlight class ----------------------------------------------- */
5758 static int brightness_update_status(struct backlight_device *bd)
5760 unsigned int level =
5761 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5762 bd->props.power == FB_BLANK_UNBLANK) ?
5763 bd->props.brightness : 0;
5765 dbg_printk(TPACPI_DBG_BRGHT,
5766 "backlight: attempt to set level to %d\n",
5767 level);
5769 /* it is the backlight class's job (caller) to handle
5770 * EINTR and other errors properly */
5771 return brightness_set(level);
5774 static int brightness_get(struct backlight_device *bd)
5776 int status, res;
5778 res = mutex_lock_killable(&brightness_mutex);
5779 if (res < 0)
5780 return 0;
5782 res = tpacpi_brightness_get_raw(&status);
5784 mutex_unlock(&brightness_mutex);
5786 if (res < 0)
5787 return 0;
5789 return status & TP_EC_BACKLIGHT_LVLMSK;
5792 static struct backlight_ops ibm_backlight_data = {
5793 .get_brightness = brightness_get,
5794 .update_status = brightness_update_status,
5797 /* --------------------------------------------------------------------- */
5800 * These are only useful for models that have only one possibility
5801 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5802 * these quirks.
5804 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5805 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5806 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5808 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5809 /* Models with ATI GPUs known to require ECNVRAM mode */
5810 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5812 /* Models with ATI GPUs that can use ECNVRAM */
5813 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
5814 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5815 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5816 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5818 /* Models with Intel Extreme Graphics 2 */
5819 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
5820 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5821 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5823 /* Models with Intel GMA900 */
5824 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5825 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5826 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5829 static int __init brightness_init(struct ibm_init_struct *iibm)
5831 int b;
5832 unsigned long quirks;
5834 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5836 mutex_init(&brightness_mutex);
5838 quirks = tpacpi_check_quirks(brightness_quirk_table,
5839 ARRAY_SIZE(brightness_quirk_table));
5842 * We always attempt to detect acpi support, so as to switch
5843 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5844 * going to publish a backlight interface
5846 b = tpacpi_check_std_acpi_brightness_support();
5847 if (b > 0) {
5848 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5849 printk(TPACPI_NOTICE
5850 "Lenovo BIOS switched to ACPI backlight "
5851 "control mode\n");
5853 if (brightness_enable > 1) {
5854 printk(TPACPI_NOTICE
5855 "standard ACPI backlight interface "
5856 "available, not loading native one...\n");
5857 return 1;
5861 if (!brightness_enable) {
5862 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5863 "brightness support disabled by "
5864 "module parameter\n");
5865 return 1;
5868 if (b > 16) {
5869 printk(TPACPI_ERR
5870 "Unsupported brightness interface, "
5871 "please contact %s\n", TPACPI_MAIL);
5872 return 1;
5874 if (b == 16)
5875 tp_features.bright_16levels = 1;
5878 * Check for module parameter bogosity, note that we
5879 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5880 * able to detect "unspecified"
5882 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5883 return -EINVAL;
5885 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5886 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5887 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5888 if (quirks & TPACPI_BRGHT_Q_EC)
5889 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5890 else
5891 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5893 dbg_printk(TPACPI_DBG_BRGHT,
5894 "driver auto-selected brightness_mode=%d\n",
5895 brightness_mode);
5898 /* Safety */
5899 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5900 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5901 brightness_mode == TPACPI_BRGHT_MODE_EC))
5902 return -EINVAL;
5904 if (tpacpi_brightness_get_raw(&b) < 0)
5905 return 1;
5907 if (tp_features.bright_16levels)
5908 printk(TPACPI_INFO
5909 "detected a 16-level brightness capable ThinkPad\n");
5911 ibm_backlight_device = backlight_device_register(
5912 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5913 &ibm_backlight_data);
5914 if (IS_ERR(ibm_backlight_device)) {
5915 printk(TPACPI_ERR "Could not register backlight device\n");
5916 return PTR_ERR(ibm_backlight_device);
5918 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5919 "brightness is supported\n");
5921 if (quirks & TPACPI_BRGHT_Q_ASK) {
5922 printk(TPACPI_NOTICE
5923 "brightness: will use unverified default: "
5924 "brightness_mode=%d\n", brightness_mode);
5925 printk(TPACPI_NOTICE
5926 "brightness: please report to %s whether it works well "
5927 "or not on your ThinkPad\n", TPACPI_MAIL);
5930 ibm_backlight_device->props.max_brightness =
5931 (tp_features.bright_16levels)? 15 : 7;
5932 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5933 backlight_update_status(ibm_backlight_device);
5935 return 0;
5938 static void brightness_suspend(pm_message_t state)
5940 tpacpi_brightness_checkpoint_nvram();
5943 static void brightness_shutdown(void)
5945 tpacpi_brightness_checkpoint_nvram();
5948 static void brightness_exit(void)
5950 if (ibm_backlight_device) {
5951 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5952 "calling backlight_device_unregister()\n");
5953 backlight_device_unregister(ibm_backlight_device);
5956 tpacpi_brightness_checkpoint_nvram();
5959 static int brightness_read(char *p)
5961 int len = 0;
5962 int level;
5964 level = brightness_get(NULL);
5965 if (level < 0) {
5966 len += sprintf(p + len, "level:\t\tunreadable\n");
5967 } else {
5968 len += sprintf(p + len, "level:\t\t%d\n", level);
5969 len += sprintf(p + len, "commands:\tup, down\n");
5970 len += sprintf(p + len, "commands:\tlevel <level>"
5971 " (<level> is 0-%d)\n",
5972 (tp_features.bright_16levels) ? 15 : 7);
5975 return len;
5978 static int brightness_write(char *buf)
5980 int level;
5981 int rc;
5982 char *cmd;
5983 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5985 level = brightness_get(NULL);
5986 if (level < 0)
5987 return level;
5989 while ((cmd = next_cmd(&buf))) {
5990 if (strlencmp(cmd, "up") == 0) {
5991 if (level < max_level)
5992 level++;
5993 } else if (strlencmp(cmd, "down") == 0) {
5994 if (level > 0)
5995 level--;
5996 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5997 level >= 0 && level <= max_level) {
5998 /* new level set */
5999 } else
6000 return -EINVAL;
6003 tpacpi_disclose_usertask("procfs brightness",
6004 "set level to %d\n", level);
6007 * Now we know what the final level should be, so we try to set it.
6008 * Doing it this way makes the syscall restartable in case of EINTR
6010 rc = brightness_set(level);
6011 return (rc == -EINTR)? -ERESTARTSYS : rc;
6014 static struct ibm_struct brightness_driver_data = {
6015 .name = "brightness",
6016 .read = brightness_read,
6017 .write = brightness_write,
6018 .exit = brightness_exit,
6019 .suspend = brightness_suspend,
6020 .shutdown = brightness_shutdown,
6023 /*************************************************************************
6024 * Volume subdriver
6027 static int volume_offset = 0x30;
6029 static int volume_read(char *p)
6031 int len = 0;
6032 u8 level;
6034 if (!acpi_ec_read(volume_offset, &level)) {
6035 len += sprintf(p + len, "level:\t\tunreadable\n");
6036 } else {
6037 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6038 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6039 len += sprintf(p + len, "commands:\tup, down, mute\n");
6040 len += sprintf(p + len, "commands:\tlevel <level>"
6041 " (<level> is 0-15)\n");
6044 return len;
6047 static int volume_write(char *buf)
6049 int cmos_cmd, inc, i;
6050 u8 level, mute;
6051 int new_level, new_mute;
6052 char *cmd;
6054 while ((cmd = next_cmd(&buf))) {
6055 if (!acpi_ec_read(volume_offset, &level))
6056 return -EIO;
6057 new_mute = mute = level & 0x40;
6058 new_level = level = level & 0xf;
6060 if (strlencmp(cmd, "up") == 0) {
6061 if (mute)
6062 new_mute = 0;
6063 else
6064 new_level = level == 15 ? 15 : level + 1;
6065 } else if (strlencmp(cmd, "down") == 0) {
6066 if (mute)
6067 new_mute = 0;
6068 else
6069 new_level = level == 0 ? 0 : level - 1;
6070 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6071 new_level >= 0 && new_level <= 15) {
6072 /* new_level set */
6073 } else if (strlencmp(cmd, "mute") == 0) {
6074 new_mute = 0x40;
6075 } else
6076 return -EINVAL;
6078 if (new_level != level) {
6079 /* mute doesn't change */
6081 cmos_cmd = (new_level > level) ?
6082 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6083 inc = new_level > level ? 1 : -1;
6085 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6086 !acpi_ec_write(volume_offset, level)))
6087 return -EIO;
6089 for (i = level; i != new_level; i += inc)
6090 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6091 !acpi_ec_write(volume_offset, i + inc))
6092 return -EIO;
6094 if (mute &&
6095 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6096 !acpi_ec_write(volume_offset, new_level + mute))) {
6097 return -EIO;
6101 if (new_mute != mute) {
6102 /* level doesn't change */
6104 cmos_cmd = (new_mute) ?
6105 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6107 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6108 !acpi_ec_write(volume_offset, level + new_mute))
6109 return -EIO;
6113 return 0;
6116 static struct ibm_struct volume_driver_data = {
6117 .name = "volume",
6118 .read = volume_read,
6119 .write = volume_write,
6122 /*************************************************************************
6123 * Fan subdriver
6127 * FAN ACCESS MODES
6129 * TPACPI_FAN_RD_ACPI_GFAN:
6130 * ACPI GFAN method: returns fan level
6132 * see TPACPI_FAN_WR_ACPI_SFAN
6133 * EC 0x2f (HFSP) not available if GFAN exists
6135 * TPACPI_FAN_WR_ACPI_SFAN:
6136 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6138 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6139 * it for writing.
6141 * TPACPI_FAN_WR_TPEC:
6142 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6143 * Supported on almost all ThinkPads
6145 * Fan speed changes of any sort (including those caused by the
6146 * disengaged mode) are usually done slowly by the firmware as the
6147 * maximum ammount of fan duty cycle change per second seems to be
6148 * limited.
6150 * Reading is not available if GFAN exists.
6151 * Writing is not available if SFAN exists.
6153 * Bits
6154 * 7 automatic mode engaged;
6155 * (default operation mode of the ThinkPad)
6156 * fan level is ignored in this mode.
6157 * 6 full speed mode (takes precedence over bit 7);
6158 * not available on all thinkpads. May disable
6159 * the tachometer while the fan controller ramps up
6160 * the speed (which can take up to a few *minutes*).
6161 * Speeds up fan to 100% duty-cycle, which is far above
6162 * the standard RPM levels. It is not impossible that
6163 * it could cause hardware damage.
6164 * 5-3 unused in some models. Extra bits for fan level
6165 * in others, but still useless as all values above
6166 * 7 map to the same speed as level 7 in these models.
6167 * 2-0 fan level (0..7 usually)
6168 * 0x00 = stop
6169 * 0x07 = max (set when temperatures critical)
6170 * Some ThinkPads may have other levels, see
6171 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6173 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6174 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6175 * does so, its initial value is meaningless (0x07).
6177 * For firmware bugs, refer to:
6178 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6180 * ----
6182 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6183 * Main fan tachometer reading (in RPM)
6185 * This register is present on all ThinkPads with a new-style EC, and
6186 * it is known not to be present on the A21m/e, and T22, as there is
6187 * something else in offset 0x84 according to the ACPI DSDT. Other
6188 * ThinkPads from this same time period (and earlier) probably lack the
6189 * tachometer as well.
6191 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6192 * was never fixed by IBM to report the EC firmware version string
6193 * probably support the tachometer (like the early X models), so
6194 * detecting it is quite hard. We need more data to know for sure.
6196 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6197 * might result.
6199 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6200 * mode.
6202 * For firmware bugs, refer to:
6203 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6205 * ----
6207 * ThinkPad EC register 0x31 bit 0 (only on select models)
6209 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6210 * show the speed of the main fan. When bit 0 of EC register 0x31
6211 * is one, the tachometer registers show the speed of the auxiliary
6212 * fan.
6214 * Fan control seems to affect both fans, regardless of the state
6215 * of this bit.
6217 * So far, only the firmware for the X60/X61 non-tablet versions
6218 * seem to support this (firmware TP-7M).
6220 * TPACPI_FAN_WR_ACPI_FANS:
6221 * ThinkPad X31, X40, X41. Not available in the X60.
6223 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6224 * high speed. ACPI DSDT seems to map these three speeds to levels
6225 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6226 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6228 * The speeds are stored on handles
6229 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6231 * There are three default speed sets, acessible as handles:
6232 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6234 * ACPI DSDT switches which set is in use depending on various
6235 * factors.
6237 * TPACPI_FAN_WR_TPEC is also available and should be used to
6238 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6239 * but the ACPI tables just mention level 7.
6242 enum { /* Fan control constants */
6243 fan_status_offset = 0x2f, /* EC register 0x2f */
6244 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6245 * 0x84 must be read before 0x85 */
6246 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6247 bit 0 selects which fan is active */
6249 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6250 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6252 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6255 enum fan_status_access_mode {
6256 TPACPI_FAN_NONE = 0, /* No fan status or control */
6257 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6258 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6261 enum fan_control_access_mode {
6262 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6263 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6264 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6265 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6268 enum fan_control_commands {
6269 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6270 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6271 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6272 * and also watchdog cmd */
6275 static int fan_control_allowed;
6277 static enum fan_status_access_mode fan_status_access_mode;
6278 static enum fan_control_access_mode fan_control_access_mode;
6279 static enum fan_control_commands fan_control_commands;
6281 static u8 fan_control_initial_status;
6282 static u8 fan_control_desired_level;
6283 static u8 fan_control_resume_level;
6284 static int fan_watchdog_maxinterval;
6286 static struct mutex fan_mutex;
6288 static void fan_watchdog_fire(struct work_struct *ignored);
6289 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6291 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6292 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6293 "\\FSPD", /* 600e/x, 770e, 770x */
6294 ); /* all others */
6295 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6296 "JFNS", /* 770x-JL */
6297 ); /* all others */
6300 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6301 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6302 * be in auto mode (0x80).
6304 * This is corrected by any write to HFSP either by the driver, or
6305 * by the firmware.
6307 * We assume 0x07 really means auto mode while this quirk is active,
6308 * as this is far more likely than the ThinkPad being in level 7,
6309 * which is only used by the firmware during thermal emergencies.
6311 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6312 * TP-70 (T43, R52), which are known to be buggy.
6315 static void fan_quirk1_setup(void)
6317 if (fan_control_initial_status == 0x07) {
6318 printk(TPACPI_NOTICE
6319 "fan_init: initial fan status is unknown, "
6320 "assuming it is in auto mode\n");
6321 tp_features.fan_ctrl_status_undef = 1;
6325 static void fan_quirk1_handle(u8 *fan_status)
6327 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6328 if (*fan_status != fan_control_initial_status) {
6329 /* something changed the HFSP regisnter since
6330 * driver init time, so it is not undefined
6331 * anymore */
6332 tp_features.fan_ctrl_status_undef = 0;
6333 } else {
6334 /* Return most likely status. In fact, it
6335 * might be the only possible status */
6336 *fan_status = TP_EC_FAN_AUTO;
6341 /* Select main fan on X60/X61, NOOP on others */
6342 static bool fan_select_fan1(void)
6344 if (tp_features.second_fan) {
6345 u8 val;
6347 if (ec_read(fan_select_offset, &val) < 0)
6348 return false;
6349 val &= 0xFEU;
6350 if (ec_write(fan_select_offset, val) < 0)
6351 return false;
6353 return true;
6356 /* Select secondary fan on X60/X61 */
6357 static bool fan_select_fan2(void)
6359 u8 val;
6361 if (!tp_features.second_fan)
6362 return false;
6364 if (ec_read(fan_select_offset, &val) < 0)
6365 return false;
6366 val |= 0x01U;
6367 if (ec_write(fan_select_offset, val) < 0)
6368 return false;
6370 return true;
6374 * Call with fan_mutex held
6376 static void fan_update_desired_level(u8 status)
6378 if ((status &
6379 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6380 if (status > 7)
6381 fan_control_desired_level = 7;
6382 else
6383 fan_control_desired_level = status;
6387 static int fan_get_status(u8 *status)
6389 u8 s;
6391 /* TODO:
6392 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6394 switch (fan_status_access_mode) {
6395 case TPACPI_FAN_RD_ACPI_GFAN:
6396 /* 570, 600e/x, 770e, 770x */
6398 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6399 return -EIO;
6401 if (likely(status))
6402 *status = s & 0x07;
6404 break;
6406 case TPACPI_FAN_RD_TPEC:
6407 /* all except 570, 600e/x, 770e, 770x */
6408 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6409 return -EIO;
6411 if (likely(status)) {
6412 *status = s;
6413 fan_quirk1_handle(status);
6416 break;
6418 default:
6419 return -ENXIO;
6422 return 0;
6425 static int fan_get_status_safe(u8 *status)
6427 int rc;
6428 u8 s;
6430 if (mutex_lock_killable(&fan_mutex))
6431 return -ERESTARTSYS;
6432 rc = fan_get_status(&s);
6433 if (!rc)
6434 fan_update_desired_level(s);
6435 mutex_unlock(&fan_mutex);
6437 if (status)
6438 *status = s;
6440 return rc;
6443 static int fan_get_speed(unsigned int *speed)
6445 u8 hi, lo;
6447 switch (fan_status_access_mode) {
6448 case TPACPI_FAN_RD_TPEC:
6449 /* all except 570, 600e/x, 770e, 770x */
6450 if (unlikely(!fan_select_fan1()))
6451 return -EIO;
6452 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6453 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6454 return -EIO;
6456 if (likely(speed))
6457 *speed = (hi << 8) | lo;
6459 break;
6461 default:
6462 return -ENXIO;
6465 return 0;
6468 static int fan2_get_speed(unsigned int *speed)
6470 u8 hi, lo;
6471 bool rc;
6473 switch (fan_status_access_mode) {
6474 case TPACPI_FAN_RD_TPEC:
6475 /* all except 570, 600e/x, 770e, 770x */
6476 if (unlikely(!fan_select_fan2()))
6477 return -EIO;
6478 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6479 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6480 fan_select_fan1(); /* play it safe */
6481 if (rc)
6482 return -EIO;
6484 if (likely(speed))
6485 *speed = (hi << 8) | lo;
6487 break;
6489 default:
6490 return -ENXIO;
6493 return 0;
6496 static int fan_set_level(int level)
6498 if (!fan_control_allowed)
6499 return -EPERM;
6501 switch (fan_control_access_mode) {
6502 case TPACPI_FAN_WR_ACPI_SFAN:
6503 if (level >= 0 && level <= 7) {
6504 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6505 return -EIO;
6506 } else
6507 return -EINVAL;
6508 break;
6510 case TPACPI_FAN_WR_ACPI_FANS:
6511 case TPACPI_FAN_WR_TPEC:
6512 if (!(level & TP_EC_FAN_AUTO) &&
6513 !(level & TP_EC_FAN_FULLSPEED) &&
6514 ((level < 0) || (level > 7)))
6515 return -EINVAL;
6517 /* safety net should the EC not support AUTO
6518 * or FULLSPEED mode bits and just ignore them */
6519 if (level & TP_EC_FAN_FULLSPEED)
6520 level |= 7; /* safety min speed 7 */
6521 else if (level & TP_EC_FAN_AUTO)
6522 level |= 4; /* safety min speed 4 */
6524 if (!acpi_ec_write(fan_status_offset, level))
6525 return -EIO;
6526 else
6527 tp_features.fan_ctrl_status_undef = 0;
6528 break;
6530 default:
6531 return -ENXIO;
6534 vdbg_printk(TPACPI_DBG_FAN,
6535 "fan control: set fan control register to 0x%02x\n", level);
6536 return 0;
6539 static int fan_set_level_safe(int level)
6541 int rc;
6543 if (!fan_control_allowed)
6544 return -EPERM;
6546 if (mutex_lock_killable(&fan_mutex))
6547 return -ERESTARTSYS;
6549 if (level == TPACPI_FAN_LAST_LEVEL)
6550 level = fan_control_desired_level;
6552 rc = fan_set_level(level);
6553 if (!rc)
6554 fan_update_desired_level(level);
6556 mutex_unlock(&fan_mutex);
6557 return rc;
6560 static int fan_set_enable(void)
6562 u8 s;
6563 int rc;
6565 if (!fan_control_allowed)
6566 return -EPERM;
6568 if (mutex_lock_killable(&fan_mutex))
6569 return -ERESTARTSYS;
6571 switch (fan_control_access_mode) {
6572 case TPACPI_FAN_WR_ACPI_FANS:
6573 case TPACPI_FAN_WR_TPEC:
6574 rc = fan_get_status(&s);
6575 if (rc < 0)
6576 break;
6578 /* Don't go out of emergency fan mode */
6579 if (s != 7) {
6580 s &= 0x07;
6581 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6584 if (!acpi_ec_write(fan_status_offset, s))
6585 rc = -EIO;
6586 else {
6587 tp_features.fan_ctrl_status_undef = 0;
6588 rc = 0;
6590 break;
6592 case TPACPI_FAN_WR_ACPI_SFAN:
6593 rc = fan_get_status(&s);
6594 if (rc < 0)
6595 break;
6597 s &= 0x07;
6599 /* Set fan to at least level 4 */
6600 s |= 4;
6602 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6603 rc = -EIO;
6604 else
6605 rc = 0;
6606 break;
6608 default:
6609 rc = -ENXIO;
6612 mutex_unlock(&fan_mutex);
6614 if (!rc)
6615 vdbg_printk(TPACPI_DBG_FAN,
6616 "fan control: set fan control register to 0x%02x\n",
6618 return rc;
6621 static int fan_set_disable(void)
6623 int rc;
6625 if (!fan_control_allowed)
6626 return -EPERM;
6628 if (mutex_lock_killable(&fan_mutex))
6629 return -ERESTARTSYS;
6631 rc = 0;
6632 switch (fan_control_access_mode) {
6633 case TPACPI_FAN_WR_ACPI_FANS:
6634 case TPACPI_FAN_WR_TPEC:
6635 if (!acpi_ec_write(fan_status_offset, 0x00))
6636 rc = -EIO;
6637 else {
6638 fan_control_desired_level = 0;
6639 tp_features.fan_ctrl_status_undef = 0;
6641 break;
6643 case TPACPI_FAN_WR_ACPI_SFAN:
6644 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6645 rc = -EIO;
6646 else
6647 fan_control_desired_level = 0;
6648 break;
6650 default:
6651 rc = -ENXIO;
6654 if (!rc)
6655 vdbg_printk(TPACPI_DBG_FAN,
6656 "fan control: set fan control register to 0\n");
6658 mutex_unlock(&fan_mutex);
6659 return rc;
6662 static int fan_set_speed(int speed)
6664 int rc;
6666 if (!fan_control_allowed)
6667 return -EPERM;
6669 if (mutex_lock_killable(&fan_mutex))
6670 return -ERESTARTSYS;
6672 rc = 0;
6673 switch (fan_control_access_mode) {
6674 case TPACPI_FAN_WR_ACPI_FANS:
6675 if (speed >= 0 && speed <= 65535) {
6676 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6677 speed, speed, speed))
6678 rc = -EIO;
6679 } else
6680 rc = -EINVAL;
6681 break;
6683 default:
6684 rc = -ENXIO;
6687 mutex_unlock(&fan_mutex);
6688 return rc;
6691 static void fan_watchdog_reset(void)
6693 static int fan_watchdog_active;
6695 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6696 return;
6698 if (fan_watchdog_active)
6699 cancel_delayed_work(&fan_watchdog_task);
6701 if (fan_watchdog_maxinterval > 0 &&
6702 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6703 fan_watchdog_active = 1;
6704 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6705 msecs_to_jiffies(fan_watchdog_maxinterval
6706 * 1000))) {
6707 printk(TPACPI_ERR
6708 "failed to queue the fan watchdog, "
6709 "watchdog will not trigger\n");
6711 } else
6712 fan_watchdog_active = 0;
6715 static void fan_watchdog_fire(struct work_struct *ignored)
6717 int rc;
6719 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6720 return;
6722 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6723 rc = fan_set_enable();
6724 if (rc < 0) {
6725 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6726 "will try again later...\n", -rc);
6727 /* reschedule for later */
6728 fan_watchdog_reset();
6733 * SYSFS fan layout: hwmon compatible (device)
6735 * pwm*_enable:
6736 * 0: "disengaged" mode
6737 * 1: manual mode
6738 * 2: native EC "auto" mode (recommended, hardware default)
6740 * pwm*: set speed in manual mode, ignored otherwise.
6741 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6742 * interpolation.
6744 * fan*_input: tachometer reading, RPM
6747 * SYSFS fan layout: extensions
6749 * fan_watchdog (driver):
6750 * fan watchdog interval in seconds, 0 disables (default), max 120
6753 /* sysfs fan pwm1_enable ----------------------------------------------- */
6754 static ssize_t fan_pwm1_enable_show(struct device *dev,
6755 struct device_attribute *attr,
6756 char *buf)
6758 int res, mode;
6759 u8 status;
6761 res = fan_get_status_safe(&status);
6762 if (res)
6763 return res;
6765 if (status & TP_EC_FAN_FULLSPEED) {
6766 mode = 0;
6767 } else if (status & TP_EC_FAN_AUTO) {
6768 mode = 2;
6769 } else
6770 mode = 1;
6772 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6775 static ssize_t fan_pwm1_enable_store(struct device *dev,
6776 struct device_attribute *attr,
6777 const char *buf, size_t count)
6779 unsigned long t;
6780 int res, level;
6782 if (parse_strtoul(buf, 2, &t))
6783 return -EINVAL;
6785 tpacpi_disclose_usertask("hwmon pwm1_enable",
6786 "set fan mode to %lu\n", t);
6788 switch (t) {
6789 case 0:
6790 level = TP_EC_FAN_FULLSPEED;
6791 break;
6792 case 1:
6793 level = TPACPI_FAN_LAST_LEVEL;
6794 break;
6795 case 2:
6796 level = TP_EC_FAN_AUTO;
6797 break;
6798 case 3:
6799 /* reserved for software-controlled auto mode */
6800 return -ENOSYS;
6801 default:
6802 return -EINVAL;
6805 res = fan_set_level_safe(level);
6806 if (res == -ENXIO)
6807 return -EINVAL;
6808 else if (res < 0)
6809 return res;
6811 fan_watchdog_reset();
6813 return count;
6816 static struct device_attribute dev_attr_fan_pwm1_enable =
6817 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6818 fan_pwm1_enable_show, fan_pwm1_enable_store);
6820 /* sysfs fan pwm1 ------------------------------------------------------ */
6821 static ssize_t fan_pwm1_show(struct device *dev,
6822 struct device_attribute *attr,
6823 char *buf)
6825 int res;
6826 u8 status;
6828 res = fan_get_status_safe(&status);
6829 if (res)
6830 return res;
6832 if ((status &
6833 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6834 status = fan_control_desired_level;
6836 if (status > 7)
6837 status = 7;
6839 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6842 static ssize_t fan_pwm1_store(struct device *dev,
6843 struct device_attribute *attr,
6844 const char *buf, size_t count)
6846 unsigned long s;
6847 int rc;
6848 u8 status, newlevel;
6850 if (parse_strtoul(buf, 255, &s))
6851 return -EINVAL;
6853 tpacpi_disclose_usertask("hwmon pwm1",
6854 "set fan speed to %lu\n", s);
6856 /* scale down from 0-255 to 0-7 */
6857 newlevel = (s >> 5) & 0x07;
6859 if (mutex_lock_killable(&fan_mutex))
6860 return -ERESTARTSYS;
6862 rc = fan_get_status(&status);
6863 if (!rc && (status &
6864 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6865 rc = fan_set_level(newlevel);
6866 if (rc == -ENXIO)
6867 rc = -EINVAL;
6868 else if (!rc) {
6869 fan_update_desired_level(newlevel);
6870 fan_watchdog_reset();
6874 mutex_unlock(&fan_mutex);
6875 return (rc)? rc : count;
6878 static struct device_attribute dev_attr_fan_pwm1 =
6879 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6880 fan_pwm1_show, fan_pwm1_store);
6882 /* sysfs fan fan1_input ------------------------------------------------ */
6883 static ssize_t fan_fan1_input_show(struct device *dev,
6884 struct device_attribute *attr,
6885 char *buf)
6887 int res;
6888 unsigned int speed;
6890 res = fan_get_speed(&speed);
6891 if (res < 0)
6892 return res;
6894 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6897 static struct device_attribute dev_attr_fan_fan1_input =
6898 __ATTR(fan1_input, S_IRUGO,
6899 fan_fan1_input_show, NULL);
6901 /* sysfs fan fan2_input ------------------------------------------------ */
6902 static ssize_t fan_fan2_input_show(struct device *dev,
6903 struct device_attribute *attr,
6904 char *buf)
6906 int res;
6907 unsigned int speed;
6909 res = fan2_get_speed(&speed);
6910 if (res < 0)
6911 return res;
6913 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6916 static struct device_attribute dev_attr_fan_fan2_input =
6917 __ATTR(fan2_input, S_IRUGO,
6918 fan_fan2_input_show, NULL);
6920 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6921 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6922 char *buf)
6924 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6927 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6928 const char *buf, size_t count)
6930 unsigned long t;
6932 if (parse_strtoul(buf, 120, &t))
6933 return -EINVAL;
6935 if (!fan_control_allowed)
6936 return -EPERM;
6938 fan_watchdog_maxinterval = t;
6939 fan_watchdog_reset();
6941 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6943 return count;
6946 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6947 fan_fan_watchdog_show, fan_fan_watchdog_store);
6949 /* --------------------------------------------------------------------- */
6950 static struct attribute *fan_attributes[] = {
6951 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6952 &dev_attr_fan_fan1_input.attr,
6953 NULL, /* for fan2_input */
6954 NULL
6957 static const struct attribute_group fan_attr_group = {
6958 .attrs = fan_attributes,
6961 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6962 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
6964 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6965 { .vendor = PCI_VENDOR_ID_IBM, \
6966 .bios = TPACPI_MATCH_ANY, \
6967 .ec = TPID(__id1, __id2), \
6968 .quirks = __quirks }
6970 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
6971 { .vendor = PCI_VENDOR_ID_LENOVO, \
6972 .bios = TPACPI_MATCH_ANY, \
6973 .ec = TPID(__id1, __id2), \
6974 .quirks = __quirks }
6976 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6977 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6978 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6979 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6980 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
6981 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
6984 #undef TPACPI_FAN_QL
6985 #undef TPACPI_FAN_QI
6987 static int __init fan_init(struct ibm_init_struct *iibm)
6989 int rc;
6990 unsigned long quirks;
6992 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6993 "initializing fan subdriver\n");
6995 mutex_init(&fan_mutex);
6996 fan_status_access_mode = TPACPI_FAN_NONE;
6997 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6998 fan_control_commands = 0;
6999 fan_watchdog_maxinterval = 0;
7000 tp_features.fan_ctrl_status_undef = 0;
7001 tp_features.second_fan = 0;
7002 fan_control_desired_level = 7;
7004 TPACPI_ACPIHANDLE_INIT(fans);
7005 TPACPI_ACPIHANDLE_INIT(gfan);
7006 TPACPI_ACPIHANDLE_INIT(sfan);
7008 quirks = tpacpi_check_quirks(fan_quirk_table,
7009 ARRAY_SIZE(fan_quirk_table));
7011 if (gfan_handle) {
7012 /* 570, 600e/x, 770e, 770x */
7013 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7014 } else {
7015 /* all other ThinkPads: note that even old-style
7016 * ThinkPad ECs supports the fan control register */
7017 if (likely(acpi_ec_read(fan_status_offset,
7018 &fan_control_initial_status))) {
7019 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7020 if (quirks & TPACPI_FAN_Q1)
7021 fan_quirk1_setup();
7022 if (quirks & TPACPI_FAN_2FAN) {
7023 tp_features.second_fan = 1;
7024 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7025 "secondary fan support enabled\n");
7027 } else {
7028 printk(TPACPI_ERR
7029 "ThinkPad ACPI EC access misbehaving, "
7030 "fan status and control unavailable\n");
7031 return 1;
7035 if (sfan_handle) {
7036 /* 570, 770x-JL */
7037 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7038 fan_control_commands |=
7039 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7040 } else {
7041 if (!gfan_handle) {
7042 /* gfan without sfan means no fan control */
7043 /* all other models implement TP EC 0x2f control */
7045 if (fans_handle) {
7046 /* X31, X40, X41 */
7047 fan_control_access_mode =
7048 TPACPI_FAN_WR_ACPI_FANS;
7049 fan_control_commands |=
7050 TPACPI_FAN_CMD_SPEED |
7051 TPACPI_FAN_CMD_LEVEL |
7052 TPACPI_FAN_CMD_ENABLE;
7053 } else {
7054 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7055 fan_control_commands |=
7056 TPACPI_FAN_CMD_LEVEL |
7057 TPACPI_FAN_CMD_ENABLE;
7062 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7063 "fan is %s, modes %d, %d\n",
7064 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7065 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7066 fan_status_access_mode, fan_control_access_mode);
7068 /* fan control master switch */
7069 if (!fan_control_allowed) {
7070 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7071 fan_control_commands = 0;
7072 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7073 "fan control features disabled by parameter\n");
7076 /* update fan_control_desired_level */
7077 if (fan_status_access_mode != TPACPI_FAN_NONE)
7078 fan_get_status_safe(NULL);
7080 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7081 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7082 if (tp_features.second_fan) {
7083 /* attach second fan tachometer */
7084 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7085 &dev_attr_fan_fan2_input.attr;
7087 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7088 &fan_attr_group);
7089 if (rc < 0)
7090 return rc;
7092 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7093 &driver_attr_fan_watchdog);
7094 if (rc < 0) {
7095 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7096 &fan_attr_group);
7097 return rc;
7099 return 0;
7100 } else
7101 return 1;
7104 static void fan_exit(void)
7106 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7107 "cancelling any pending fan watchdog tasks\n");
7109 /* FIXME: can we really do this unconditionally? */
7110 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7111 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7112 &driver_attr_fan_watchdog);
7114 cancel_delayed_work(&fan_watchdog_task);
7115 flush_workqueue(tpacpi_wq);
7118 static void fan_suspend(pm_message_t state)
7120 int rc;
7122 if (!fan_control_allowed)
7123 return;
7125 /* Store fan status in cache */
7126 fan_control_resume_level = 0;
7127 rc = fan_get_status_safe(&fan_control_resume_level);
7128 if (rc < 0)
7129 printk(TPACPI_NOTICE
7130 "failed to read fan level for later "
7131 "restore during resume: %d\n", rc);
7133 /* if it is undefined, don't attempt to restore it.
7134 * KEEP THIS LAST */
7135 if (tp_features.fan_ctrl_status_undef)
7136 fan_control_resume_level = 0;
7139 static void fan_resume(void)
7141 u8 current_level = 7;
7142 bool do_set = false;
7143 int rc;
7145 /* DSDT *always* updates status on resume */
7146 tp_features.fan_ctrl_status_undef = 0;
7148 if (!fan_control_allowed ||
7149 !fan_control_resume_level ||
7150 (fan_get_status_safe(&current_level) < 0))
7151 return;
7153 switch (fan_control_access_mode) {
7154 case TPACPI_FAN_WR_ACPI_SFAN:
7155 /* never decrease fan level */
7156 do_set = (fan_control_resume_level > current_level);
7157 break;
7158 case TPACPI_FAN_WR_ACPI_FANS:
7159 case TPACPI_FAN_WR_TPEC:
7160 /* never decrease fan level, scale is:
7161 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7163 * We expect the firmware to set either 7 or AUTO, but we
7164 * handle FULLSPEED out of paranoia.
7166 * So, we can safely only restore FULLSPEED or 7, anything
7167 * else could slow the fan. Restoring AUTO is useless, at
7168 * best that's exactly what the DSDT already set (it is the
7169 * slower it uses).
7171 * Always keep in mind that the DSDT *will* have set the
7172 * fans to what the vendor supposes is the best level. We
7173 * muck with it only to speed the fan up.
7175 if (fan_control_resume_level != 7 &&
7176 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7177 return;
7178 else
7179 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7180 (current_level != fan_control_resume_level);
7181 break;
7182 default:
7183 return;
7185 if (do_set) {
7186 printk(TPACPI_NOTICE
7187 "restoring fan level to 0x%02x\n",
7188 fan_control_resume_level);
7189 rc = fan_set_level_safe(fan_control_resume_level);
7190 if (rc < 0)
7191 printk(TPACPI_NOTICE
7192 "failed to restore fan level: %d\n", rc);
7196 static int fan_read(char *p)
7198 int len = 0;
7199 int rc;
7200 u8 status;
7201 unsigned int speed = 0;
7203 switch (fan_status_access_mode) {
7204 case TPACPI_FAN_RD_ACPI_GFAN:
7205 /* 570, 600e/x, 770e, 770x */
7206 rc = fan_get_status_safe(&status);
7207 if (rc < 0)
7208 return rc;
7210 len += sprintf(p + len, "status:\t\t%s\n"
7211 "level:\t\t%d\n",
7212 (status != 0) ? "enabled" : "disabled", status);
7213 break;
7215 case TPACPI_FAN_RD_TPEC:
7216 /* all except 570, 600e/x, 770e, 770x */
7217 rc = fan_get_status_safe(&status);
7218 if (rc < 0)
7219 return rc;
7221 len += sprintf(p + len, "status:\t\t%s\n",
7222 (status != 0) ? "enabled" : "disabled");
7224 rc = fan_get_speed(&speed);
7225 if (rc < 0)
7226 return rc;
7228 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7230 if (status & TP_EC_FAN_FULLSPEED)
7231 /* Disengaged mode takes precedence */
7232 len += sprintf(p + len, "level:\t\tdisengaged\n");
7233 else if (status & TP_EC_FAN_AUTO)
7234 len += sprintf(p + len, "level:\t\tauto\n");
7235 else
7236 len += sprintf(p + len, "level:\t\t%d\n", status);
7237 break;
7239 case TPACPI_FAN_NONE:
7240 default:
7241 len += sprintf(p + len, "status:\t\tnot supported\n");
7244 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7245 len += sprintf(p + len, "commands:\tlevel <level>");
7247 switch (fan_control_access_mode) {
7248 case TPACPI_FAN_WR_ACPI_SFAN:
7249 len += sprintf(p + len, " (<level> is 0-7)\n");
7250 break;
7252 default:
7253 len += sprintf(p + len, " (<level> is 0-7, "
7254 "auto, disengaged, full-speed)\n");
7255 break;
7259 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7260 len += sprintf(p + len, "commands:\tenable, disable\n"
7261 "commands:\twatchdog <timeout> (<timeout> "
7262 "is 0 (off), 1-120 (seconds))\n");
7264 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7265 len += sprintf(p + len, "commands:\tspeed <speed>"
7266 " (<speed> is 0-65535)\n");
7268 return len;
7271 static int fan_write_cmd_level(const char *cmd, int *rc)
7273 int level;
7275 if (strlencmp(cmd, "level auto") == 0)
7276 level = TP_EC_FAN_AUTO;
7277 else if ((strlencmp(cmd, "level disengaged") == 0) |
7278 (strlencmp(cmd, "level full-speed") == 0))
7279 level = TP_EC_FAN_FULLSPEED;
7280 else if (sscanf(cmd, "level %d", &level) != 1)
7281 return 0;
7283 *rc = fan_set_level_safe(level);
7284 if (*rc == -ENXIO)
7285 printk(TPACPI_ERR "level command accepted for unsupported "
7286 "access mode %d", fan_control_access_mode);
7287 else if (!*rc)
7288 tpacpi_disclose_usertask("procfs fan",
7289 "set level to %d\n", level);
7291 return 1;
7294 static int fan_write_cmd_enable(const char *cmd, int *rc)
7296 if (strlencmp(cmd, "enable") != 0)
7297 return 0;
7299 *rc = fan_set_enable();
7300 if (*rc == -ENXIO)
7301 printk(TPACPI_ERR "enable command accepted for unsupported "
7302 "access mode %d", fan_control_access_mode);
7303 else if (!*rc)
7304 tpacpi_disclose_usertask("procfs fan", "enable\n");
7306 return 1;
7309 static int fan_write_cmd_disable(const char *cmd, int *rc)
7311 if (strlencmp(cmd, "disable") != 0)
7312 return 0;
7314 *rc = fan_set_disable();
7315 if (*rc == -ENXIO)
7316 printk(TPACPI_ERR "disable command accepted for unsupported "
7317 "access mode %d", fan_control_access_mode);
7318 else if (!*rc)
7319 tpacpi_disclose_usertask("procfs fan", "disable\n");
7321 return 1;
7324 static int fan_write_cmd_speed(const char *cmd, int *rc)
7326 int speed;
7328 /* TODO:
7329 * Support speed <low> <medium> <high> ? */
7331 if (sscanf(cmd, "speed %d", &speed) != 1)
7332 return 0;
7334 *rc = fan_set_speed(speed);
7335 if (*rc == -ENXIO)
7336 printk(TPACPI_ERR "speed command accepted for unsupported "
7337 "access mode %d", fan_control_access_mode);
7338 else if (!*rc)
7339 tpacpi_disclose_usertask("procfs fan",
7340 "set speed to %d\n", speed);
7342 return 1;
7345 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7347 int interval;
7349 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7350 return 0;
7352 if (interval < 0 || interval > 120)
7353 *rc = -EINVAL;
7354 else {
7355 fan_watchdog_maxinterval = interval;
7356 tpacpi_disclose_usertask("procfs fan",
7357 "set watchdog timer to %d\n",
7358 interval);
7361 return 1;
7364 static int fan_write(char *buf)
7366 char *cmd;
7367 int rc = 0;
7369 while (!rc && (cmd = next_cmd(&buf))) {
7370 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7371 fan_write_cmd_level(cmd, &rc)) &&
7372 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7373 (fan_write_cmd_enable(cmd, &rc) ||
7374 fan_write_cmd_disable(cmd, &rc) ||
7375 fan_write_cmd_watchdog(cmd, &rc))) &&
7376 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7377 fan_write_cmd_speed(cmd, &rc))
7379 rc = -EINVAL;
7380 else if (!rc)
7381 fan_watchdog_reset();
7384 return rc;
7387 static struct ibm_struct fan_driver_data = {
7388 .name = "fan",
7389 .read = fan_read,
7390 .write = fan_write,
7391 .exit = fan_exit,
7392 .suspend = fan_suspend,
7393 .resume = fan_resume,
7396 /****************************************************************************
7397 ****************************************************************************
7399 * Infrastructure
7401 ****************************************************************************
7402 ****************************************************************************/
7404 /* sysfs name ---------------------------------------------------------- */
7405 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7406 struct device_attribute *attr,
7407 char *buf)
7409 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7412 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7413 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7415 /* --------------------------------------------------------------------- */
7417 /* /proc support */
7418 static struct proc_dir_entry *proc_dir;
7421 * Module and infrastructure proble, init and exit handling
7424 static int force_load;
7426 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7427 static const char * __init str_supported(int is_supported)
7429 static char text_unsupported[] __initdata = "not supported";
7431 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7433 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7435 static void ibm_exit(struct ibm_struct *ibm)
7437 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7439 list_del_init(&ibm->all_drivers);
7441 if (ibm->flags.acpi_notify_installed) {
7442 dbg_printk(TPACPI_DBG_EXIT,
7443 "%s: acpi_remove_notify_handler\n", ibm->name);
7444 BUG_ON(!ibm->acpi);
7445 acpi_remove_notify_handler(*ibm->acpi->handle,
7446 ibm->acpi->type,
7447 dispatch_acpi_notify);
7448 ibm->flags.acpi_notify_installed = 0;
7449 ibm->flags.acpi_notify_installed = 0;
7452 if (ibm->flags.proc_created) {
7453 dbg_printk(TPACPI_DBG_EXIT,
7454 "%s: remove_proc_entry\n", ibm->name);
7455 remove_proc_entry(ibm->name, proc_dir);
7456 ibm->flags.proc_created = 0;
7459 if (ibm->flags.acpi_driver_registered) {
7460 dbg_printk(TPACPI_DBG_EXIT,
7461 "%s: acpi_bus_unregister_driver\n", ibm->name);
7462 BUG_ON(!ibm->acpi);
7463 acpi_bus_unregister_driver(ibm->acpi->driver);
7464 kfree(ibm->acpi->driver);
7465 ibm->acpi->driver = NULL;
7466 ibm->flags.acpi_driver_registered = 0;
7469 if (ibm->flags.init_called && ibm->exit) {
7470 ibm->exit();
7471 ibm->flags.init_called = 0;
7474 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7477 static int __init ibm_init(struct ibm_init_struct *iibm)
7479 int ret;
7480 struct ibm_struct *ibm = iibm->data;
7481 struct proc_dir_entry *entry;
7483 BUG_ON(ibm == NULL);
7485 INIT_LIST_HEAD(&ibm->all_drivers);
7487 if (ibm->flags.experimental && !experimental)
7488 return 0;
7490 dbg_printk(TPACPI_DBG_INIT,
7491 "probing for %s\n", ibm->name);
7493 if (iibm->init) {
7494 ret = iibm->init(iibm);
7495 if (ret > 0)
7496 return 0; /* probe failed */
7497 if (ret)
7498 return ret;
7500 ibm->flags.init_called = 1;
7503 if (ibm->acpi) {
7504 if (ibm->acpi->hid) {
7505 ret = register_tpacpi_subdriver(ibm);
7506 if (ret)
7507 goto err_out;
7510 if (ibm->acpi->notify) {
7511 ret = setup_acpi_notify(ibm);
7512 if (ret == -ENODEV) {
7513 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7514 ibm->name);
7515 ret = 0;
7516 goto err_out;
7518 if (ret < 0)
7519 goto err_out;
7523 dbg_printk(TPACPI_DBG_INIT,
7524 "%s installed\n", ibm->name);
7526 if (ibm->read) {
7527 entry = create_proc_entry(ibm->name,
7528 S_IFREG | S_IRUGO | S_IWUSR,
7529 proc_dir);
7530 if (!entry) {
7531 printk(TPACPI_ERR "unable to create proc entry %s\n",
7532 ibm->name);
7533 ret = -ENODEV;
7534 goto err_out;
7536 entry->owner = THIS_MODULE;
7537 entry->data = ibm;
7538 entry->read_proc = &dispatch_procfs_read;
7539 if (ibm->write)
7540 entry->write_proc = &dispatch_procfs_write;
7541 ibm->flags.proc_created = 1;
7544 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7546 return 0;
7548 err_out:
7549 dbg_printk(TPACPI_DBG_INIT,
7550 "%s: at error exit path with result %d\n",
7551 ibm->name, ret);
7553 ibm_exit(ibm);
7554 return (ret < 0)? ret : 0;
7557 /* Probing */
7559 static bool __pure __init tpacpi_is_fw_digit(const char c)
7561 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7564 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7565 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7566 const char t)
7568 return s && strlen(s) >= 8 &&
7569 tpacpi_is_fw_digit(s[0]) &&
7570 tpacpi_is_fw_digit(s[1]) &&
7571 s[2] == t && s[3] == 'T' &&
7572 tpacpi_is_fw_digit(s[4]) &&
7573 tpacpi_is_fw_digit(s[5]) &&
7574 s[6] == 'W' && s[7] == 'W';
7577 /* returns 0 - probe ok, or < 0 - probe error.
7578 * Probe ok doesn't mean thinkpad found.
7579 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7580 static int __must_check __init get_thinkpad_model_data(
7581 struct thinkpad_id_data *tp)
7583 const struct dmi_device *dev = NULL;
7584 char ec_fw_string[18];
7585 char const *s;
7587 if (!tp)
7588 return -EINVAL;
7590 memset(tp, 0, sizeof(*tp));
7592 if (dmi_name_in_vendors("IBM"))
7593 tp->vendor = PCI_VENDOR_ID_IBM;
7594 else if (dmi_name_in_vendors("LENOVO"))
7595 tp->vendor = PCI_VENDOR_ID_LENOVO;
7596 else
7597 return 0;
7599 s = dmi_get_system_info(DMI_BIOS_VERSION);
7600 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7601 if (s && !tp->bios_version_str)
7602 return -ENOMEM;
7604 /* Really ancient ThinkPad 240X will fail this, which is fine */
7605 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7606 return 0;
7608 tp->bios_model = tp->bios_version_str[0]
7609 | (tp->bios_version_str[1] << 8);
7610 tp->bios_release = (tp->bios_version_str[4] << 8)
7611 | tp->bios_version_str[5];
7614 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7615 * X32 or newer, all Z series; Some models must have an
7616 * up-to-date BIOS or they will not be detected.
7618 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7620 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7621 if (sscanf(dev->name,
7622 "IBM ThinkPad Embedded Controller -[%17c",
7623 ec_fw_string) == 1) {
7624 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7625 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7627 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7628 if (!tp->ec_version_str)
7629 return -ENOMEM;
7631 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7632 tp->ec_model = ec_fw_string[0]
7633 | (ec_fw_string[1] << 8);
7634 tp->ec_release = (ec_fw_string[4] << 8)
7635 | ec_fw_string[5];
7636 } else {
7637 printk(TPACPI_NOTICE
7638 "ThinkPad firmware release %s "
7639 "doesn't match the known patterns\n",
7640 ec_fw_string);
7641 printk(TPACPI_NOTICE
7642 "please report this to %s\n",
7643 TPACPI_MAIL);
7645 break;
7649 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7650 if (s && !strnicmp(s, "ThinkPad", 8)) {
7651 tp->model_str = kstrdup(s, GFP_KERNEL);
7652 if (!tp->model_str)
7653 return -ENOMEM;
7656 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7657 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7658 if (s && !tp->nummodel_str)
7659 return -ENOMEM;
7661 return 0;
7664 static int __init probe_for_thinkpad(void)
7666 int is_thinkpad;
7668 if (acpi_disabled)
7669 return -ENODEV;
7672 * Non-ancient models have better DMI tagging, but very old models
7673 * don't.
7675 is_thinkpad = (thinkpad_id.model_str != NULL);
7677 /* ec is required because many other handles are relative to it */
7678 TPACPI_ACPIHANDLE_INIT(ec);
7679 if (!ec_handle) {
7680 if (is_thinkpad)
7681 printk(TPACPI_ERR
7682 "Not yet supported ThinkPad detected!\n");
7683 return -ENODEV;
7687 * Risks a regression on very old machines, but reduces potential
7688 * false positives a damn great deal
7690 if (!is_thinkpad)
7691 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7693 if (!is_thinkpad && !force_load)
7694 return -ENODEV;
7696 return 0;
7700 /* Module init, exit, parameters */
7702 static struct ibm_init_struct ibms_init[] __initdata = {
7704 .init = thinkpad_acpi_driver_init,
7705 .data = &thinkpad_acpi_driver_data,
7708 .init = hotkey_init,
7709 .data = &hotkey_driver_data,
7712 .init = bluetooth_init,
7713 .data = &bluetooth_driver_data,
7716 .init = wan_init,
7717 .data = &wan_driver_data,
7720 .init = uwb_init,
7721 .data = &uwb_driver_data,
7723 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7725 .init = video_init,
7726 .data = &video_driver_data,
7728 #endif
7730 .init = light_init,
7731 .data = &light_driver_data,
7734 .init = cmos_init,
7735 .data = &cmos_driver_data,
7738 .init = led_init,
7739 .data = &led_driver_data,
7742 .init = beep_init,
7743 .data = &beep_driver_data,
7746 .init = thermal_init,
7747 .data = &thermal_driver_data,
7750 .data = &ecdump_driver_data,
7753 .init = brightness_init,
7754 .data = &brightness_driver_data,
7757 .data = &volume_driver_data,
7760 .init = fan_init,
7761 .data = &fan_driver_data,
7765 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7767 unsigned int i;
7768 struct ibm_struct *ibm;
7770 if (!kp || !kp->name || !val)
7771 return -EINVAL;
7773 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7774 ibm = ibms_init[i].data;
7775 WARN_ON(ibm == NULL);
7777 if (!ibm || !ibm->name)
7778 continue;
7780 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7781 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7782 return -ENOSPC;
7783 strcpy(ibms_init[i].param, val);
7784 strcat(ibms_init[i].param, ",");
7785 return 0;
7789 return -EINVAL;
7792 module_param(experimental, int, 0);
7793 MODULE_PARM_DESC(experimental,
7794 "Enables experimental features when non-zero");
7796 module_param_named(debug, dbg_level, uint, 0);
7797 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7799 module_param(force_load, bool, 0);
7800 MODULE_PARM_DESC(force_load,
7801 "Attempts to load the driver even on a "
7802 "mis-identified ThinkPad when true");
7804 module_param_named(fan_control, fan_control_allowed, bool, 0);
7805 MODULE_PARM_DESC(fan_control,
7806 "Enables setting fan parameters features when true");
7808 module_param_named(brightness_mode, brightness_mode, uint, 0);
7809 MODULE_PARM_DESC(brightness_mode,
7810 "Selects brightness control strategy: "
7811 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7813 module_param(brightness_enable, uint, 0);
7814 MODULE_PARM_DESC(brightness_enable,
7815 "Enables backlight control when 1, disables when 0");
7817 module_param(hotkey_report_mode, uint, 0);
7818 MODULE_PARM_DESC(hotkey_report_mode,
7819 "used for backwards compatibility with userspace, "
7820 "see documentation");
7822 #define TPACPI_PARAM(feature) \
7823 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7824 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7825 "at module load, see documentation")
7827 TPACPI_PARAM(hotkey);
7828 TPACPI_PARAM(bluetooth);
7829 TPACPI_PARAM(video);
7830 TPACPI_PARAM(light);
7831 TPACPI_PARAM(cmos);
7832 TPACPI_PARAM(led);
7833 TPACPI_PARAM(beep);
7834 TPACPI_PARAM(ecdump);
7835 TPACPI_PARAM(brightness);
7836 TPACPI_PARAM(volume);
7837 TPACPI_PARAM(fan);
7839 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7840 module_param(dbg_wlswemul, uint, 0);
7841 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7842 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7843 MODULE_PARM_DESC(wlsw_state,
7844 "Initial state of the emulated WLSW switch");
7846 module_param(dbg_bluetoothemul, uint, 0);
7847 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7848 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7849 MODULE_PARM_DESC(bluetooth_state,
7850 "Initial state of the emulated bluetooth switch");
7852 module_param(dbg_wwanemul, uint, 0);
7853 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7854 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7855 MODULE_PARM_DESC(wwan_state,
7856 "Initial state of the emulated WWAN switch");
7858 module_param(dbg_uwbemul, uint, 0);
7859 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7860 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7861 MODULE_PARM_DESC(uwb_state,
7862 "Initial state of the emulated UWB switch");
7863 #endif
7865 static void thinkpad_acpi_module_exit(void)
7867 struct ibm_struct *ibm, *itmp;
7869 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7871 list_for_each_entry_safe_reverse(ibm, itmp,
7872 &tpacpi_all_drivers,
7873 all_drivers) {
7874 ibm_exit(ibm);
7877 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7879 if (tpacpi_inputdev) {
7880 if (tp_features.input_device_registered)
7881 input_unregister_device(tpacpi_inputdev);
7882 else
7883 input_free_device(tpacpi_inputdev);
7886 if (tpacpi_hwmon)
7887 hwmon_device_unregister(tpacpi_hwmon);
7889 if (tp_features.sensors_pdev_attrs_registered)
7890 device_remove_file(&tpacpi_sensors_pdev->dev,
7891 &dev_attr_thinkpad_acpi_pdev_name);
7892 if (tpacpi_sensors_pdev)
7893 platform_device_unregister(tpacpi_sensors_pdev);
7894 if (tpacpi_pdev)
7895 platform_device_unregister(tpacpi_pdev);
7897 if (tp_features.sensors_pdrv_attrs_registered)
7898 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7899 if (tp_features.platform_drv_attrs_registered)
7900 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7902 if (tp_features.sensors_pdrv_registered)
7903 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7905 if (tp_features.platform_drv_registered)
7906 platform_driver_unregister(&tpacpi_pdriver);
7908 if (proc_dir)
7909 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7911 if (tpacpi_wq)
7912 destroy_workqueue(tpacpi_wq);
7914 kfree(thinkpad_id.bios_version_str);
7915 kfree(thinkpad_id.ec_version_str);
7916 kfree(thinkpad_id.model_str);
7920 static int __init thinkpad_acpi_module_init(void)
7922 int ret, i;
7924 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7926 /* Parameter checking */
7927 if (hotkey_report_mode > 2)
7928 return -EINVAL;
7930 /* Driver-level probe */
7932 ret = get_thinkpad_model_data(&thinkpad_id);
7933 if (ret) {
7934 printk(TPACPI_ERR
7935 "unable to get DMI data: %d\n", ret);
7936 thinkpad_acpi_module_exit();
7937 return ret;
7939 ret = probe_for_thinkpad();
7940 if (ret) {
7941 thinkpad_acpi_module_exit();
7942 return ret;
7945 /* Driver initialization */
7947 TPACPI_ACPIHANDLE_INIT(ecrd);
7948 TPACPI_ACPIHANDLE_INIT(ecwr);
7950 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7951 if (!tpacpi_wq) {
7952 thinkpad_acpi_module_exit();
7953 return -ENOMEM;
7956 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7957 if (!proc_dir) {
7958 printk(TPACPI_ERR
7959 "unable to create proc dir " TPACPI_PROC_DIR);
7960 thinkpad_acpi_module_exit();
7961 return -ENODEV;
7963 proc_dir->owner = THIS_MODULE;
7965 ret = platform_driver_register(&tpacpi_pdriver);
7966 if (ret) {
7967 printk(TPACPI_ERR
7968 "unable to register main platform driver\n");
7969 thinkpad_acpi_module_exit();
7970 return ret;
7972 tp_features.platform_drv_registered = 1;
7974 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7975 if (ret) {
7976 printk(TPACPI_ERR
7977 "unable to register hwmon platform driver\n");
7978 thinkpad_acpi_module_exit();
7979 return ret;
7981 tp_features.sensors_pdrv_registered = 1;
7983 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7984 if (!ret) {
7985 tp_features.platform_drv_attrs_registered = 1;
7986 ret = tpacpi_create_driver_attributes(
7987 &tpacpi_hwmon_pdriver.driver);
7989 if (ret) {
7990 printk(TPACPI_ERR
7991 "unable to create sysfs driver attributes\n");
7992 thinkpad_acpi_module_exit();
7993 return ret;
7995 tp_features.sensors_pdrv_attrs_registered = 1;
7998 /* Device initialization */
7999 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8000 NULL, 0);
8001 if (IS_ERR(tpacpi_pdev)) {
8002 ret = PTR_ERR(tpacpi_pdev);
8003 tpacpi_pdev = NULL;
8004 printk(TPACPI_ERR "unable to register platform device\n");
8005 thinkpad_acpi_module_exit();
8006 return ret;
8008 tpacpi_sensors_pdev = platform_device_register_simple(
8009 TPACPI_HWMON_DRVR_NAME,
8010 -1, NULL, 0);
8011 if (IS_ERR(tpacpi_sensors_pdev)) {
8012 ret = PTR_ERR(tpacpi_sensors_pdev);
8013 tpacpi_sensors_pdev = NULL;
8014 printk(TPACPI_ERR
8015 "unable to register hwmon platform device\n");
8016 thinkpad_acpi_module_exit();
8017 return ret;
8019 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8020 &dev_attr_thinkpad_acpi_pdev_name);
8021 if (ret) {
8022 printk(TPACPI_ERR
8023 "unable to create sysfs hwmon device attributes\n");
8024 thinkpad_acpi_module_exit();
8025 return ret;
8027 tp_features.sensors_pdev_attrs_registered = 1;
8028 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8029 if (IS_ERR(tpacpi_hwmon)) {
8030 ret = PTR_ERR(tpacpi_hwmon);
8031 tpacpi_hwmon = NULL;
8032 printk(TPACPI_ERR "unable to register hwmon device\n");
8033 thinkpad_acpi_module_exit();
8034 return ret;
8036 mutex_init(&tpacpi_inputdev_send_mutex);
8037 tpacpi_inputdev = input_allocate_device();
8038 if (!tpacpi_inputdev) {
8039 printk(TPACPI_ERR "unable to allocate input device\n");
8040 thinkpad_acpi_module_exit();
8041 return -ENOMEM;
8042 } else {
8043 /* Prepare input device, but don't register */
8044 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8045 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8046 tpacpi_inputdev->id.bustype = BUS_HOST;
8047 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8048 thinkpad_id.vendor :
8049 PCI_VENDOR_ID_IBM;
8050 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8051 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8053 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8054 ret = ibm_init(&ibms_init[i]);
8055 if (ret >= 0 && *ibms_init[i].param)
8056 ret = ibms_init[i].data->write(ibms_init[i].param);
8057 if (ret < 0) {
8058 thinkpad_acpi_module_exit();
8059 return ret;
8062 ret = input_register_device(tpacpi_inputdev);
8063 if (ret < 0) {
8064 printk(TPACPI_ERR "unable to register input device\n");
8065 thinkpad_acpi_module_exit();
8066 return ret;
8067 } else {
8068 tp_features.input_device_registered = 1;
8071 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8072 return 0;
8075 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8078 * This will autoload the driver in almost every ThinkPad
8079 * in widespread use.
8081 * Only _VERY_ old models, like the 240, 240x and 570 lack
8082 * the HKEY event interface.
8084 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8087 * DMI matching for module autoloading
8089 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8090 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8092 * Only models listed in thinkwiki will be supported, so add yours
8093 * if it is not there yet.
8095 #define IBM_BIOS_MODULE_ALIAS(__type) \
8096 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8098 /* Ancient thinkpad BIOSes have to be identified by
8099 * BIOS type or model number, and there are far less
8100 * BIOS types than model numbers... */
8101 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8103 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8104 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8105 MODULE_DESCRIPTION(TPACPI_DESC);
8106 MODULE_VERSION(TPACPI_VERSION);
8107 MODULE_LICENSE("GPL");
8109 module_init(thinkpad_acpi_module_init);
8110 module_exit(thinkpad_acpi_module_exit);