thinkpad-acpi: add internal hotkey event API
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob6d3bab2ccfa3aff96584b85b8b01cc8a9f94077a
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 0x020500
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
195 #define TPACPI_DBG_BRGHT 0x0020
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
226 void (*shutdown) (void);
228 struct list_head all_drivers;
230 struct tp_acpi_drv_struct *acpi;
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
241 struct ibm_init_struct {
242 char param[32];
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
248 static struct {
249 u32 bluetooth:1;
250 u32 hotkey:1;
251 u32 hotkey_mask:1;
252 u32 hotkey_wlsw:1;
253 u32 hotkey_tablet:1;
254 u32 light:1;
255 u32 light_status:1;
256 u32 bright_16levels:1;
257 u32 bright_acpimode:1;
258 u32 wan:1;
259 u32 uwb:1;
260 u32 fan_ctrl_status_undef:1;
261 u32 second_fan:1;
262 u32 beep_needs_two_args:1;
263 u32 input_device_registered:1;
264 u32 platform_drv_registered:1;
265 u32 platform_drv_attrs_registered:1;
266 u32 sensors_pdrv_registered:1;
267 u32 sensors_pdrv_attrs_registered:1;
268 u32 sensors_pdev_attrs_registered:1;
269 u32 hotkey_poll_active:1;
270 } tp_features;
272 static struct {
273 u16 hotkey_mask_ff:1;
274 } tp_warned;
276 struct thinkpad_id_data {
277 unsigned int vendor; /* ThinkPad vendor:
278 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
280 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
281 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
283 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
284 u16 ec_model;
285 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
286 u16 ec_release;
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
293 static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 enum led_status_t {
305 TPACPI_LED_OFF = 0,
306 TPACPI_LED_ON,
307 TPACPI_LED_BLINK,
310 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev {
312 struct led_classdev led_classdev;
313 struct work_struct work;
314 enum led_status_t new_state;
315 unsigned int led;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul;
320 static int tpacpi_wlsw_emulstate;
321 static int dbg_bluetoothemul;
322 static int tpacpi_bluetooth_emulstate;
323 static int dbg_wwanemul;
324 static int tpacpi_wwan_emulstate;
325 static int dbg_uwbemul;
326 static int tpacpi_uwb_emulstate;
327 #endif
330 /*************************************************************************
331 * Debugging helpers
334 #define dbg_printk(a_dbg_level, format, arg...) \
335 do { if (dbg_level & (a_dbg_level)) \
336 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
337 } while (0)
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 static const char *str_supported(int is_supported);
342 #else
343 #define vdbg_printk(a_dbg_level, format, arg...) \
344 do { } while (0)
345 #endif
347 static void tpacpi_log_usertask(const char * const what)
349 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
350 what, task_tgid_vnr(current));
353 #define tpacpi_disclose_usertask(what, format, arg...) \
354 do { \
355 if (unlikely( \
356 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
357 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
358 printk(TPACPI_DEBUG "%s: PID %d: " format, \
359 what, task_tgid_vnr(current), ## arg); \
361 } while (0)
364 * Quirk handling helpers
366 * ThinkPad IDs and versions seen in the field so far
367 * are two-characters from the set [0-9A-Z], i.e. base 36.
369 * We use values well outside that range as specials.
372 #define TPACPI_MATCH_ANY 0xffffU
373 #define TPACPI_MATCH_UNKNOWN 0U
375 /* TPID('1', 'Y') == 0x5931 */
376 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
378 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
379 { .vendor = PCI_VENDOR_ID_IBM, \
380 .bios = TPID(__id1, __id2), \
381 .ec = TPACPI_MATCH_ANY, \
382 .quirks = (__quirk) }
384 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_LENOVO, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
390 struct tpacpi_quirk {
391 unsigned int vendor;
392 u16 bios;
393 u16 ec;
394 unsigned long quirks;
398 * tpacpi_check_quirks() - search BIOS/EC version on a list
399 * @qlist: array of &struct tpacpi_quirk
400 * @qlist_size: number of elements in @qlist
402 * Iterates over a quirks list until one is found that matches the
403 * ThinkPad's vendor, BIOS and EC model.
405 * Returns 0 if nothing matches, otherwise returns the quirks field of
406 * the matching &struct tpacpi_quirk entry.
408 * The match criteria is: vendor, ec and bios much match.
410 static unsigned long __init tpacpi_check_quirks(
411 const struct tpacpi_quirk *qlist,
412 unsigned int qlist_size)
414 while (qlist_size) {
415 if ((qlist->vendor == thinkpad_id.vendor ||
416 qlist->vendor == TPACPI_MATCH_ANY) &&
417 (qlist->bios == thinkpad_id.bios_model ||
418 qlist->bios == TPACPI_MATCH_ANY) &&
419 (qlist->ec == thinkpad_id.ec_model ||
420 qlist->ec == TPACPI_MATCH_ANY))
421 return qlist->quirks;
423 qlist_size--;
424 qlist++;
426 return 0;
430 /****************************************************************************
431 ****************************************************************************
433 * ACPI Helpers and device model
435 ****************************************************************************
436 ****************************************************************************/
438 /*************************************************************************
439 * ACPI basic handles
442 static acpi_handle root_handle;
444 #define TPACPI_HANDLE(object, parent, paths...) \
445 static acpi_handle object##_handle; \
446 static acpi_handle *object##_parent = &parent##_handle; \
447 static char *object##_path; \
448 static char *object##_paths[] = { paths }
450 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
451 "\\_SB.PCI.ISA.EC", /* 570 */
452 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
453 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
454 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
455 "\\_SB.PCI0.ICH3.EC0", /* R31 */
456 "\\_SB.PCI0.LPC.EC", /* all others */
459 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
460 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
462 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
463 /* T4x, X31, X40 */
464 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
465 "\\CMS", /* R40, R40e */
466 ); /* all others */
468 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
469 "^HKEY", /* R30, R31 */
470 "HKEY", /* all others */
471 ); /* 570 */
473 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
474 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
475 "\\_SB.PCI0.VID0", /* 770e */
476 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
477 "\\_SB.PCI0.AGP.VID", /* all others */
478 ); /* R30, R31 */
481 /*************************************************************************
482 * ACPI helpers
485 static int acpi_evalf(acpi_handle handle,
486 void *res, char *method, char *fmt, ...)
488 char *fmt0 = fmt;
489 struct acpi_object_list params;
490 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
491 struct acpi_buffer result, *resultp;
492 union acpi_object out_obj;
493 acpi_status status;
494 va_list ap;
495 char res_type;
496 int success;
497 int quiet;
499 if (!*fmt) {
500 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
501 return 0;
504 if (*fmt == 'q') {
505 quiet = 1;
506 fmt++;
507 } else
508 quiet = 0;
510 res_type = *(fmt++);
512 params.count = 0;
513 params.pointer = &in_objs[0];
515 va_start(ap, fmt);
516 while (*fmt) {
517 char c = *(fmt++);
518 switch (c) {
519 case 'd': /* int */
520 in_objs[params.count].integer.value = va_arg(ap, int);
521 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
522 break;
523 /* add more types as needed */
524 default:
525 printk(TPACPI_ERR "acpi_evalf() called "
526 "with invalid format character '%c'\n", c);
527 return 0;
530 va_end(ap);
532 if (res_type != 'v') {
533 result.length = sizeof(out_obj);
534 result.pointer = &out_obj;
535 resultp = &result;
536 } else
537 resultp = NULL;
539 status = acpi_evaluate_object(handle, method, &params, resultp);
541 switch (res_type) {
542 case 'd': /* int */
543 if (res)
544 *(int *)res = out_obj.integer.value;
545 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
546 break;
547 case 'v': /* void */
548 success = status == AE_OK;
549 break;
550 /* add more types as needed */
551 default:
552 printk(TPACPI_ERR "acpi_evalf() called "
553 "with invalid format character '%c'\n", res_type);
554 return 0;
557 if (!success && !quiet)
558 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
559 method, fmt0, status);
561 return success;
564 static int acpi_ec_read(int i, u8 *p)
566 int v;
568 if (ecrd_handle) {
569 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
570 return 0;
571 *p = v;
572 } else {
573 if (ec_read(i, p) < 0)
574 return 0;
577 return 1;
580 static int acpi_ec_write(int i, u8 v)
582 if (ecwr_handle) {
583 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
584 return 0;
585 } else {
586 if (ec_write(i, v) < 0)
587 return 0;
590 return 1;
593 static int issue_thinkpad_cmos_command(int cmos_cmd)
595 if (!cmos_handle)
596 return -ENXIO;
598 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
599 return -EIO;
601 return 0;
604 /*************************************************************************
605 * ACPI device model
608 #define TPACPI_ACPIHANDLE_INIT(object) \
609 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
610 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
612 static void drv_acpi_handle_init(char *name,
613 acpi_handle *handle, acpi_handle parent,
614 char **paths, int num_paths, char **path)
616 int i;
617 acpi_status status;
619 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
620 name);
622 for (i = 0; i < num_paths; i++) {
623 status = acpi_get_handle(parent, paths[i], handle);
624 if (ACPI_SUCCESS(status)) {
625 *path = paths[i];
626 dbg_printk(TPACPI_DBG_INIT,
627 "Found ACPI handle %s for %s\n",
628 *path, name);
629 return;
633 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
634 name);
635 *handle = NULL;
638 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
640 struct ibm_struct *ibm = data;
642 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
643 return;
645 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
646 return;
648 ibm->acpi->notify(ibm, event);
651 static int __init setup_acpi_notify(struct ibm_struct *ibm)
653 acpi_status status;
654 int rc;
656 BUG_ON(!ibm->acpi);
658 if (!*ibm->acpi->handle)
659 return 0;
661 vdbg_printk(TPACPI_DBG_INIT,
662 "setting up ACPI notify for %s\n", ibm->name);
664 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
665 if (rc < 0) {
666 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
667 ibm->name, rc);
668 return -ENODEV;
671 ibm->acpi->device->driver_data = ibm;
672 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
673 TPACPI_ACPI_EVENT_PREFIX,
674 ibm->name);
676 status = acpi_install_notify_handler(*ibm->acpi->handle,
677 ibm->acpi->type, dispatch_acpi_notify, ibm);
678 if (ACPI_FAILURE(status)) {
679 if (status == AE_ALREADY_EXISTS) {
680 printk(TPACPI_NOTICE
681 "another device driver is already "
682 "handling %s events\n", ibm->name);
683 } else {
684 printk(TPACPI_ERR
685 "acpi_install_notify_handler(%s) failed: %d\n",
686 ibm->name, status);
688 return -ENODEV;
690 ibm->flags.acpi_notify_installed = 1;
691 return 0;
694 static int __init tpacpi_device_add(struct acpi_device *device)
696 return 0;
699 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
701 int rc;
703 dbg_printk(TPACPI_DBG_INIT,
704 "registering %s as an ACPI driver\n", ibm->name);
706 BUG_ON(!ibm->acpi);
708 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
709 if (!ibm->acpi->driver) {
710 printk(TPACPI_ERR
711 "failed to allocate memory for ibm->acpi->driver\n");
712 return -ENOMEM;
715 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
716 ibm->acpi->driver->ids = ibm->acpi->hid;
718 ibm->acpi->driver->ops.add = &tpacpi_device_add;
720 rc = acpi_bus_register_driver(ibm->acpi->driver);
721 if (rc < 0) {
722 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
723 ibm->name, rc);
724 kfree(ibm->acpi->driver);
725 ibm->acpi->driver = NULL;
726 } else if (!rc)
727 ibm->flags.acpi_driver_registered = 1;
729 return rc;
733 /****************************************************************************
734 ****************************************************************************
736 * Procfs Helpers
738 ****************************************************************************
739 ****************************************************************************/
741 static int dispatch_procfs_read(char *page, char **start, off_t off,
742 int count, int *eof, void *data)
744 struct ibm_struct *ibm = data;
745 int len;
747 if (!ibm || !ibm->read)
748 return -EINVAL;
750 len = ibm->read(page);
751 if (len < 0)
752 return len;
754 if (len <= off + count)
755 *eof = 1;
756 *start = page + off;
757 len -= off;
758 if (len > count)
759 len = count;
760 if (len < 0)
761 len = 0;
763 return len;
766 static int dispatch_procfs_write(struct file *file,
767 const char __user *userbuf,
768 unsigned long count, void *data)
770 struct ibm_struct *ibm = data;
771 char *kernbuf;
772 int ret;
774 if (!ibm || !ibm->write)
775 return -EINVAL;
776 if (count > PAGE_SIZE - 2)
777 return -EINVAL;
779 kernbuf = kmalloc(count + 2, GFP_KERNEL);
780 if (!kernbuf)
781 return -ENOMEM;
783 if (copy_from_user(kernbuf, userbuf, count)) {
784 kfree(kernbuf);
785 return -EFAULT;
788 kernbuf[count] = 0;
789 strcat(kernbuf, ",");
790 ret = ibm->write(kernbuf);
791 if (ret == 0)
792 ret = count;
794 kfree(kernbuf);
796 return ret;
799 static char *next_cmd(char **cmds)
801 char *start = *cmds;
802 char *end;
804 while ((end = strchr(start, ',')) && end == start)
805 start = end + 1;
807 if (!end)
808 return NULL;
810 *end = 0;
811 *cmds = end + 1;
812 return start;
816 /****************************************************************************
817 ****************************************************************************
819 * Device model: input, hwmon and platform
821 ****************************************************************************
822 ****************************************************************************/
824 static struct platform_device *tpacpi_pdev;
825 static struct platform_device *tpacpi_sensors_pdev;
826 static struct device *tpacpi_hwmon;
827 static struct input_dev *tpacpi_inputdev;
828 static struct mutex tpacpi_inputdev_send_mutex;
829 static LIST_HEAD(tpacpi_all_drivers);
831 static int tpacpi_suspend_handler(struct platform_device *pdev,
832 pm_message_t state)
834 struct ibm_struct *ibm, *itmp;
836 list_for_each_entry_safe(ibm, itmp,
837 &tpacpi_all_drivers,
838 all_drivers) {
839 if (ibm->suspend)
840 (ibm->suspend)(state);
843 return 0;
846 static int tpacpi_resume_handler(struct platform_device *pdev)
848 struct ibm_struct *ibm, *itmp;
850 list_for_each_entry_safe(ibm, itmp,
851 &tpacpi_all_drivers,
852 all_drivers) {
853 if (ibm->resume)
854 (ibm->resume)();
857 return 0;
860 static void tpacpi_shutdown_handler(struct platform_device *pdev)
862 struct ibm_struct *ibm, *itmp;
864 list_for_each_entry_safe(ibm, itmp,
865 &tpacpi_all_drivers,
866 all_drivers) {
867 if (ibm->shutdown)
868 (ibm->shutdown)();
872 static struct platform_driver tpacpi_pdriver = {
873 .driver = {
874 .name = TPACPI_DRVR_NAME,
875 .owner = THIS_MODULE,
877 .suspend = tpacpi_suspend_handler,
878 .resume = tpacpi_resume_handler,
879 .shutdown = tpacpi_shutdown_handler,
882 static struct platform_driver tpacpi_hwmon_pdriver = {
883 .driver = {
884 .name = TPACPI_HWMON_DRVR_NAME,
885 .owner = THIS_MODULE,
889 /*************************************************************************
890 * sysfs support helpers
893 struct attribute_set {
894 unsigned int members, max_members;
895 struct attribute_group group;
898 struct attribute_set_obj {
899 struct attribute_set s;
900 struct attribute *a;
901 } __attribute__((packed));
903 static struct attribute_set *create_attr_set(unsigned int max_members,
904 const char *name)
906 struct attribute_set_obj *sobj;
908 if (max_members == 0)
909 return NULL;
911 /* Allocates space for implicit NULL at the end too */
912 sobj = kzalloc(sizeof(struct attribute_set_obj) +
913 max_members * sizeof(struct attribute *),
914 GFP_KERNEL);
915 if (!sobj)
916 return NULL;
917 sobj->s.max_members = max_members;
918 sobj->s.group.attrs = &sobj->a;
919 sobj->s.group.name = name;
921 return &sobj->s;
924 #define destroy_attr_set(_set) \
925 kfree(_set);
927 /* not multi-threaded safe, use it in a single thread per set */
928 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
930 if (!s || !attr)
931 return -EINVAL;
933 if (s->members >= s->max_members)
934 return -ENOMEM;
936 s->group.attrs[s->members] = attr;
937 s->members++;
939 return 0;
942 static int add_many_to_attr_set(struct attribute_set *s,
943 struct attribute **attr,
944 unsigned int count)
946 int i, res;
948 for (i = 0; i < count; i++) {
949 res = add_to_attr_set(s, attr[i]);
950 if (res)
951 return res;
954 return 0;
957 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
959 sysfs_remove_group(kobj, &s->group);
960 destroy_attr_set(s);
963 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
964 sysfs_create_group(_kobj, &_attr_set->group)
966 static int parse_strtoul(const char *buf,
967 unsigned long max, unsigned long *value)
969 char *endp;
971 while (*buf && isspace(*buf))
972 buf++;
973 *value = simple_strtoul(buf, &endp, 0);
974 while (*endp && isspace(*endp))
975 endp++;
976 if (*endp || *value > max)
977 return -EINVAL;
979 return 0;
982 static void tpacpi_disable_brightness_delay(void)
984 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
985 printk(TPACPI_NOTICE
986 "ACPI backlight control delay disabled\n");
989 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
991 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
992 union acpi_object *obj;
993 int rc;
995 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
996 obj = (union acpi_object *)buffer.pointer;
997 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
998 printk(TPACPI_ERR "Unknown _BCL data, "
999 "please report this to %s\n", TPACPI_MAIL);
1000 rc = 0;
1001 } else {
1002 rc = obj->package.count;
1004 } else {
1005 return 0;
1008 kfree(buffer.pointer);
1009 return rc;
1012 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1013 u32 lvl, void *context, void **rv)
1015 char name[ACPI_PATH_SEGMENT_LENGTH];
1016 struct acpi_buffer buffer = { sizeof(name), &name };
1018 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1019 !strncmp("_BCL", name, sizeof(name) - 1)) {
1020 BUG_ON(!rv || !*rv);
1021 **(int **)rv = tpacpi_query_bcl_levels(handle);
1022 return AE_CTRL_TERMINATE;
1023 } else {
1024 return AE_OK;
1029 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1031 static int __init tpacpi_check_std_acpi_brightness_support(void)
1033 int status;
1034 int bcl_levels = 0;
1035 void *bcl_ptr = &bcl_levels;
1037 if (!vid_handle) {
1038 TPACPI_ACPIHANDLE_INIT(vid);
1040 if (!vid_handle)
1041 return 0;
1044 * Search for a _BCL method, and execute it. This is safe on all
1045 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1046 * BIOS in ACPI backlight control mode. We do NOT have to care
1047 * about calling the _BCL method in an enabled video device, any
1048 * will do for our purposes.
1051 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1052 tpacpi_acpi_walk_find_bcl, NULL,
1053 &bcl_ptr);
1055 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1056 tp_features.bright_acpimode = 1;
1057 return (bcl_levels - 2);
1060 return 0;
1063 static int __init tpacpi_new_rfkill(const unsigned int id,
1064 struct rfkill **rfk,
1065 const enum rfkill_type rfktype,
1066 const char *name,
1067 const bool set_default,
1068 int (*toggle_radio)(void *, enum rfkill_state),
1069 int (*get_state)(void *, enum rfkill_state *))
1071 int res;
1072 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1074 res = get_state(NULL, &initial_state);
1075 if (res < 0) {
1076 printk(TPACPI_ERR
1077 "failed to read initial state for %s, error %d; "
1078 "will turn radio off\n", name, res);
1079 } else if (set_default) {
1080 /* try to set the initial state as the default for the rfkill
1081 * type, since we ask the firmware to preserve it across S5 in
1082 * NVRAM */
1083 if (rfkill_set_default(rfktype,
1084 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1085 RFKILL_STATE_UNBLOCKED :
1086 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1087 vdbg_printk(TPACPI_DBG_RFKILL,
1088 "Default state for %s cannot be changed\n",
1089 name);
1092 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1093 if (!*rfk) {
1094 printk(TPACPI_ERR
1095 "failed to allocate memory for rfkill class\n");
1096 return -ENOMEM;
1099 (*rfk)->name = name;
1100 (*rfk)->get_state = get_state;
1101 (*rfk)->toggle_radio = toggle_radio;
1102 (*rfk)->state = initial_state;
1104 res = rfkill_register(*rfk);
1105 if (res < 0) {
1106 printk(TPACPI_ERR
1107 "failed to register %s rfkill switch: %d\n",
1108 name, res);
1109 rfkill_free(*rfk);
1110 *rfk = NULL;
1111 return res;
1114 return 0;
1117 static void printk_deprecated_attribute(const char * const what,
1118 const char * const details)
1120 tpacpi_log_usertask("deprecated sysfs attribute");
1121 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1122 "will be removed. %s\n",
1123 what, details);
1126 static void printk_deprecated_rfkill_attribute(const char * const what)
1128 printk_deprecated_attribute(what,
1129 "Please switch to generic rfkill before year 2010");
1132 /*************************************************************************
1133 * thinkpad-acpi driver attributes
1136 /* interface_version --------------------------------------------------- */
1137 static ssize_t tpacpi_driver_interface_version_show(
1138 struct device_driver *drv,
1139 char *buf)
1141 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1144 static DRIVER_ATTR(interface_version, S_IRUGO,
1145 tpacpi_driver_interface_version_show, NULL);
1147 /* debug_level --------------------------------------------------------- */
1148 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1149 char *buf)
1151 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1154 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1155 const char *buf, size_t count)
1157 unsigned long t;
1159 if (parse_strtoul(buf, 0xffff, &t))
1160 return -EINVAL;
1162 dbg_level = t;
1164 return count;
1167 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1168 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1170 /* version ------------------------------------------------------------- */
1171 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1172 char *buf)
1174 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1175 TPACPI_DESC, TPACPI_VERSION);
1178 static DRIVER_ATTR(version, S_IRUGO,
1179 tpacpi_driver_version_show, NULL);
1181 /* --------------------------------------------------------------------- */
1183 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1185 static void tpacpi_send_radiosw_update(void);
1187 /* wlsw_emulstate ------------------------------------------------------ */
1188 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1189 char *buf)
1191 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1194 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1195 const char *buf, size_t count)
1197 unsigned long t;
1199 if (parse_strtoul(buf, 1, &t))
1200 return -EINVAL;
1202 if (tpacpi_wlsw_emulstate != t) {
1203 tpacpi_wlsw_emulstate = !!t;
1204 tpacpi_send_radiosw_update();
1205 } else
1206 tpacpi_wlsw_emulstate = !!t;
1208 return count;
1211 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1212 tpacpi_driver_wlsw_emulstate_show,
1213 tpacpi_driver_wlsw_emulstate_store);
1215 /* bluetooth_emulstate ------------------------------------------------- */
1216 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1217 struct device_driver *drv,
1218 char *buf)
1220 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1223 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1224 struct device_driver *drv,
1225 const char *buf, size_t count)
1227 unsigned long t;
1229 if (parse_strtoul(buf, 1, &t))
1230 return -EINVAL;
1232 tpacpi_bluetooth_emulstate = !!t;
1234 return count;
1237 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1238 tpacpi_driver_bluetooth_emulstate_show,
1239 tpacpi_driver_bluetooth_emulstate_store);
1241 /* wwan_emulstate ------------------------------------------------- */
1242 static ssize_t tpacpi_driver_wwan_emulstate_show(
1243 struct device_driver *drv,
1244 char *buf)
1246 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1249 static ssize_t tpacpi_driver_wwan_emulstate_store(
1250 struct device_driver *drv,
1251 const char *buf, size_t count)
1253 unsigned long t;
1255 if (parse_strtoul(buf, 1, &t))
1256 return -EINVAL;
1258 tpacpi_wwan_emulstate = !!t;
1260 return count;
1263 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1264 tpacpi_driver_wwan_emulstate_show,
1265 tpacpi_driver_wwan_emulstate_store);
1267 /* uwb_emulstate ------------------------------------------------- */
1268 static ssize_t tpacpi_driver_uwb_emulstate_show(
1269 struct device_driver *drv,
1270 char *buf)
1272 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1275 static ssize_t tpacpi_driver_uwb_emulstate_store(
1276 struct device_driver *drv,
1277 const char *buf, size_t count)
1279 unsigned long t;
1281 if (parse_strtoul(buf, 1, &t))
1282 return -EINVAL;
1284 tpacpi_uwb_emulstate = !!t;
1286 return count;
1289 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1290 tpacpi_driver_uwb_emulstate_show,
1291 tpacpi_driver_uwb_emulstate_store);
1292 #endif
1294 /* --------------------------------------------------------------------- */
1296 static struct driver_attribute *tpacpi_driver_attributes[] = {
1297 &driver_attr_debug_level, &driver_attr_version,
1298 &driver_attr_interface_version,
1301 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1303 int i, res;
1305 i = 0;
1306 res = 0;
1307 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1308 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1309 i++;
1312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1313 if (!res && dbg_wlswemul)
1314 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1315 if (!res && dbg_bluetoothemul)
1316 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1317 if (!res && dbg_wwanemul)
1318 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1319 if (!res && dbg_uwbemul)
1320 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1321 #endif
1323 return res;
1326 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1328 int i;
1330 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1331 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1333 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1334 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1335 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1336 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1337 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1338 #endif
1341 /*************************************************************************
1342 * Firmware Data
1346 * Table of recommended minimum BIOS versions
1348 * Reasons for listing:
1349 * 1. Stable BIOS, listed because the unknown ammount of
1350 * bugs and bad ACPI behaviour on older versions
1352 * 2. BIOS or EC fw with known bugs that trigger on Linux
1354 * 3. BIOS with known reduced functionality in older versions
1356 * We recommend the latest BIOS and EC version.
1357 * We only support the latest BIOS and EC fw version as a rule.
1359 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1360 * Information from users in ThinkWiki
1362 * WARNING: we use this table also to detect that the machine is
1363 * a ThinkPad in some cases, so don't remove entries lightly.
1366 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1367 { .vendor = (__v), \
1368 .bios = TPID(__id1, __id2), \
1369 .ec = TPACPI_MATCH_ANY, \
1370 .quirks = TPACPI_MATCH_ANY << 16 \
1371 | (__bv1) << 8 | (__bv2) }
1373 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1374 __eid1, __eid2, __ev1, __ev2) \
1375 { .vendor = (__v), \
1376 .bios = TPID(__bid1, __bid2), \
1377 .ec = TPID(__eid1, __eid2), \
1378 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1379 | (__bv1) << 8 | (__bv2) }
1381 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1382 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1384 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1385 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1386 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1388 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1389 __eid1, __eid2, __ev1, __ev2) \
1390 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1391 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1393 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1394 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1396 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1397 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1398 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1400 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1401 __eid1, __eid2, __ev1, __ev2) \
1402 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1403 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1405 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1406 /* Numeric models ------------------ */
1407 /* FW MODEL BIOS VERS */
1408 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1409 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1410 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1411 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1412 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1413 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1414 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1415 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1416 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1418 /* A-series ------------------------- */
1419 /* FW MODEL BIOS VERS EC VERS */
1420 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1421 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1422 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1423 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1424 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1425 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1426 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1427 TPV_QI0('1', '3', '2', '0'), /* A22m */
1428 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1429 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1430 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1432 /* G-series ------------------------- */
1433 /* FW MODEL BIOS VERS */
1434 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1435 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1437 /* R-series, T-series --------------- */
1438 /* FW MODEL BIOS VERS EC VERS */
1439 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1440 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1441 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1442 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1443 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1444 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1445 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1446 T40/p, T41/p, T42/p (1) */
1447 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1448 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1449 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1450 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1452 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1453 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1454 TPV_QI0('1', '6', '3', '2'), /* T22 */
1455 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1456 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1457 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1459 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1460 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1461 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1463 /* BIOS FW BIOS VERS EC FW EC VERS */
1464 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1465 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1467 /* X-series ------------------------- */
1468 /* FW MODEL BIOS VERS EC VERS */
1469 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1470 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1471 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1472 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1473 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1474 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1475 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1477 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1478 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1480 /* (0) - older versions lack DMI EC fw string and functionality */
1481 /* (1) - older versions known to lack functionality */
1484 #undef TPV_QL1
1485 #undef TPV_QL0
1486 #undef TPV_QI2
1487 #undef TPV_QI1
1488 #undef TPV_QI0
1489 #undef TPV_Q_X
1490 #undef TPV_Q
1492 static void __init tpacpi_check_outdated_fw(void)
1494 unsigned long fwvers;
1495 u16 ec_version, bios_version;
1497 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1498 ARRAY_SIZE(tpacpi_bios_version_qtable));
1500 if (!fwvers)
1501 return;
1503 bios_version = fwvers & 0xffffU;
1504 ec_version = (fwvers >> 16) & 0xffffU;
1506 /* note that unknown versions are set to 0x0000 and we use that */
1507 if ((bios_version > thinkpad_id.bios_release) ||
1508 (ec_version > thinkpad_id.ec_release &&
1509 ec_version != TPACPI_MATCH_ANY)) {
1511 * The changelogs would let us track down the exact
1512 * reason, but it is just too much of a pain to track
1513 * it. We only list BIOSes that are either really
1514 * broken, or really stable to begin with, so it is
1515 * best if the user upgrades the firmware anyway.
1517 printk(TPACPI_WARN
1518 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1519 printk(TPACPI_WARN
1520 "WARNING: This firmware may be missing critical bug "
1521 "fixes and/or important features\n");
1525 static bool __init tpacpi_is_fw_known(void)
1527 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1528 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1531 /****************************************************************************
1532 ****************************************************************************
1534 * Subdrivers
1536 ****************************************************************************
1537 ****************************************************************************/
1539 /*************************************************************************
1540 * thinkpad-acpi init subdriver
1543 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1545 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1546 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1548 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1549 (thinkpad_id.bios_version_str) ?
1550 thinkpad_id.bios_version_str : "unknown",
1551 (thinkpad_id.ec_version_str) ?
1552 thinkpad_id.ec_version_str : "unknown");
1554 if (thinkpad_id.vendor && thinkpad_id.model_str)
1555 printk(TPACPI_INFO "%s %s, model %s\n",
1556 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1557 "IBM" : ((thinkpad_id.vendor ==
1558 PCI_VENDOR_ID_LENOVO) ?
1559 "Lenovo" : "Unknown vendor"),
1560 thinkpad_id.model_str,
1561 (thinkpad_id.nummodel_str) ?
1562 thinkpad_id.nummodel_str : "unknown");
1564 tpacpi_check_outdated_fw();
1565 return 0;
1568 static int thinkpad_acpi_driver_read(char *p)
1570 int len = 0;
1572 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1573 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1575 return len;
1578 static struct ibm_struct thinkpad_acpi_driver_data = {
1579 .name = "driver",
1580 .read = thinkpad_acpi_driver_read,
1583 /*************************************************************************
1584 * Hotkey subdriver
1588 * ThinkPad firmware event model
1590 * The ThinkPad firmware has two main event interfaces: normal ACPI
1591 * notifications (which follow the ACPI standard), and a private event
1592 * interface.
1594 * The private event interface also issues events for the hotkeys. As
1595 * the driver gained features, the event handling code ended up being
1596 * built around the hotkey subdriver. This will need to be refactored
1597 * to a more formal event API eventually.
1599 * Some "hotkeys" are actually supposed to be used as event reports,
1600 * such as "brightness has changed", "volume has changed", depending on
1601 * the ThinkPad model and how the firmware is operating.
1603 * Unlike other classes, hotkey-class events have mask/unmask control on
1604 * non-ancient firmware. However, how it behaves changes a lot with the
1605 * firmware model and version.
1608 enum { /* hot key scan codes (derived from ACPI DSDT) */
1609 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1610 TP_ACPI_HOTKEYSCAN_FNF2,
1611 TP_ACPI_HOTKEYSCAN_FNF3,
1612 TP_ACPI_HOTKEYSCAN_FNF4,
1613 TP_ACPI_HOTKEYSCAN_FNF5,
1614 TP_ACPI_HOTKEYSCAN_FNF6,
1615 TP_ACPI_HOTKEYSCAN_FNF7,
1616 TP_ACPI_HOTKEYSCAN_FNF8,
1617 TP_ACPI_HOTKEYSCAN_FNF9,
1618 TP_ACPI_HOTKEYSCAN_FNF10,
1619 TP_ACPI_HOTKEYSCAN_FNF11,
1620 TP_ACPI_HOTKEYSCAN_FNF12,
1621 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1622 TP_ACPI_HOTKEYSCAN_FNINSERT,
1623 TP_ACPI_HOTKEYSCAN_FNDELETE,
1624 TP_ACPI_HOTKEYSCAN_FNHOME,
1625 TP_ACPI_HOTKEYSCAN_FNEND,
1626 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1627 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1628 TP_ACPI_HOTKEYSCAN_FNSPACE,
1629 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1630 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1631 TP_ACPI_HOTKEYSCAN_MUTE,
1632 TP_ACPI_HOTKEYSCAN_THINKPAD,
1635 enum { /* Keys/events available through NVRAM polling */
1636 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1637 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1640 enum { /* Positions of some of the keys in hotkey masks */
1641 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1642 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1643 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1644 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1645 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1646 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1647 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1648 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1649 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1650 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1651 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1654 enum { /* NVRAM to ACPI HKEY group map */
1655 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1656 TP_ACPI_HKEY_ZOOM_MASK |
1657 TP_ACPI_HKEY_DISPSWTCH_MASK |
1658 TP_ACPI_HKEY_HIBERNATE_MASK,
1659 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1660 TP_ACPI_HKEY_BRGHTDWN_MASK,
1661 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1662 TP_ACPI_HKEY_VOLDWN_MASK |
1663 TP_ACPI_HKEY_MUTE_MASK,
1666 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1667 struct tp_nvram_state {
1668 u16 thinkpad_toggle:1;
1669 u16 zoom_toggle:1;
1670 u16 display_toggle:1;
1671 u16 thinklight_toggle:1;
1672 u16 hibernate_toggle:1;
1673 u16 displayexp_toggle:1;
1674 u16 display_state:1;
1675 u16 brightness_toggle:1;
1676 u16 volume_toggle:1;
1677 u16 mute:1;
1679 u8 brightness_level;
1680 u8 volume_level;
1683 /* kthread for the hotkey poller */
1684 static struct task_struct *tpacpi_hotkey_task;
1686 /* Acquired while the poller kthread is running, use to sync start/stop */
1687 static struct mutex hotkey_thread_mutex;
1690 * Acquire mutex to write poller control variables as an
1691 * atomic block.
1693 * Increment hotkey_config_change when changing them if you
1694 * want the kthread to forget old state.
1696 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1698 static struct mutex hotkey_thread_data_mutex;
1699 static unsigned int hotkey_config_change;
1702 * hotkey poller control variables
1704 * Must be atomic or readers will also need to acquire mutex
1706 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1707 * should be used only when the changes need to be taken as
1708 * a block, OR when one needs to force the kthread to forget
1709 * old state.
1711 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1712 static unsigned int hotkey_poll_freq = 10; /* Hz */
1714 #define HOTKEY_CONFIG_CRITICAL_START \
1715 do { \
1716 mutex_lock(&hotkey_thread_data_mutex); \
1717 hotkey_config_change++; \
1718 } while (0);
1719 #define HOTKEY_CONFIG_CRITICAL_END \
1720 mutex_unlock(&hotkey_thread_data_mutex);
1722 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1724 #define hotkey_source_mask 0U
1725 #define HOTKEY_CONFIG_CRITICAL_START
1726 #define HOTKEY_CONFIG_CRITICAL_END
1728 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1730 static struct mutex hotkey_mutex;
1732 static enum { /* Reasons for waking up */
1733 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1734 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1735 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1736 } hotkey_wakeup_reason;
1738 static int hotkey_autosleep_ack;
1740 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1741 static u32 hotkey_all_mask; /* all events supported in fw */
1742 static u32 hotkey_reserved_mask; /* events better left disabled */
1743 static u32 hotkey_driver_mask; /* events needed by the driver */
1744 static u32 hotkey_user_mask; /* events visible to userspace */
1745 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1747 static unsigned int hotkey_report_mode;
1749 static u16 *hotkey_keycode_map;
1751 static struct attribute_set *hotkey_dev_attributes;
1753 static void tpacpi_driver_event(const unsigned int hkey_event);
1754 static void hotkey_driver_event(const unsigned int scancode);
1756 /* HKEY.MHKG() return bits */
1757 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1759 static int hotkey_get_wlsw(int *status)
1761 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1762 if (dbg_wlswemul) {
1763 *status = !!tpacpi_wlsw_emulstate;
1764 return 0;
1766 #endif
1767 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1768 return -EIO;
1769 return 0;
1772 static int hotkey_get_tablet_mode(int *status)
1774 int s;
1776 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1777 return -EIO;
1779 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1780 return 0;
1784 * Reads current event mask from firmware, and updates
1785 * hotkey_acpi_mask accordingly. Also resets any bits
1786 * from hotkey_user_mask that are unavailable to be
1787 * delivered (shadow requirement of the userspace ABI).
1789 * Call with hotkey_mutex held
1791 static int hotkey_mask_get(void)
1793 if (tp_features.hotkey_mask) {
1794 u32 m = 0;
1796 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1797 return -EIO;
1799 hotkey_acpi_mask = m;
1800 } else {
1801 /* no mask support doesn't mean no event support... */
1802 hotkey_acpi_mask = hotkey_all_mask;
1805 /* sync userspace-visible mask */
1806 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1808 return 0;
1811 void static hotkey_mask_warn_incomplete_mask(void)
1813 /* log only what the user can fix... */
1814 const u32 wantedmask = hotkey_driver_mask &
1815 ~(hotkey_acpi_mask | hotkey_source_mask) &
1816 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1818 if (wantedmask)
1819 printk(TPACPI_NOTICE
1820 "required events 0x%08x not enabled!\n",
1821 wantedmask);
1825 * Set the firmware mask when supported
1827 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1829 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1831 * Call with hotkey_mutex held
1833 static int hotkey_mask_set(u32 mask)
1835 int i;
1836 int rc = 0;
1838 const u32 fwmask = mask & ~hotkey_source_mask;
1840 if (tp_features.hotkey_mask) {
1841 for (i = 0; i < 32; i++) {
1842 if (!acpi_evalf(hkey_handle,
1843 NULL, "MHKM", "vdd", i + 1,
1844 !!(mask & (1 << i)))) {
1845 rc = -EIO;
1846 break;
1852 * We *must* make an inconditional call to hotkey_mask_get to
1853 * refresh hotkey_acpi_mask and update hotkey_user_mask
1855 * Take the opportunity to also log when we cannot _enable_
1856 * a given event.
1858 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1859 printk(TPACPI_NOTICE
1860 "asked for hotkey mask 0x%08x, but "
1861 "firmware forced it to 0x%08x\n",
1862 fwmask, hotkey_acpi_mask);
1865 hotkey_mask_warn_incomplete_mask();
1867 return rc;
1871 * Sets hotkey_user_mask and tries to set the firmware mask
1873 * Call with hotkey_mutex held
1875 static int hotkey_user_mask_set(const u32 mask)
1877 int rc;
1879 /* Give people a chance to notice they are doing something that
1880 * is bound to go boom on their users sooner or later */
1881 if (!tp_warned.hotkey_mask_ff &&
1882 (mask == 0xffff || mask == 0xffffff ||
1883 mask == 0xffffffff)) {
1884 tp_warned.hotkey_mask_ff = 1;
1885 printk(TPACPI_NOTICE
1886 "setting the hotkey mask to 0x%08x is likely "
1887 "not the best way to go about it\n", mask);
1888 printk(TPACPI_NOTICE
1889 "please consider using the driver defaults, "
1890 "and refer to up-to-date thinkpad-acpi "
1891 "documentation\n");
1894 /* Try to enable what the user asked for, plus whatever we need.
1895 * this syncs everything but won't enable bits in hotkey_user_mask */
1896 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1898 /* Enable the available bits in hotkey_user_mask */
1899 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1901 return rc;
1905 * Sets the driver hotkey mask.
1907 * Can be called even if the hotkey subdriver is inactive
1909 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
1911 int rc;
1913 /* Do the right thing if hotkey_init has not been called yet */
1914 if (!tp_features.hotkey) {
1915 hotkey_driver_mask = mask;
1916 return 0;
1919 mutex_lock(&hotkey_mutex);
1921 HOTKEY_CONFIG_CRITICAL_START
1922 hotkey_driver_mask = mask;
1923 hotkey_source_mask |= (mask & ~hotkey_all_mask);
1924 HOTKEY_CONFIG_CRITICAL_END
1926 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
1927 ~hotkey_source_mask);
1928 mutex_unlock(&hotkey_mutex);
1930 return rc;
1933 static int hotkey_status_get(int *status)
1935 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1936 return -EIO;
1938 return 0;
1941 static int hotkey_status_set(bool enable)
1943 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1944 return -EIO;
1946 return 0;
1949 static void tpacpi_input_send_tabletsw(void)
1951 int state;
1953 if (tp_features.hotkey_tablet &&
1954 !hotkey_get_tablet_mode(&state)) {
1955 mutex_lock(&tpacpi_inputdev_send_mutex);
1957 input_report_switch(tpacpi_inputdev,
1958 SW_TABLET_MODE, !!state);
1959 input_sync(tpacpi_inputdev);
1961 mutex_unlock(&tpacpi_inputdev_send_mutex);
1965 /* Do NOT call without validating scancode first */
1966 static void tpacpi_input_send_key(const unsigned int scancode)
1968 const unsigned int keycode = hotkey_keycode_map[scancode];
1970 if (keycode != KEY_RESERVED) {
1971 mutex_lock(&tpacpi_inputdev_send_mutex);
1973 input_report_key(tpacpi_inputdev, keycode, 1);
1974 if (keycode == KEY_UNKNOWN)
1975 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1976 scancode);
1977 input_sync(tpacpi_inputdev);
1979 input_report_key(tpacpi_inputdev, keycode, 0);
1980 if (keycode == KEY_UNKNOWN)
1981 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1982 scancode);
1983 input_sync(tpacpi_inputdev);
1985 mutex_unlock(&tpacpi_inputdev_send_mutex);
1989 /* Do NOT call without validating scancode first */
1990 static void tpacpi_input_send_key_masked(const unsigned int scancode)
1992 hotkey_driver_event(scancode);
1993 if (hotkey_user_mask & (1 << scancode))
1994 tpacpi_input_send_key(scancode);
1997 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1998 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2000 /* Do NOT call without validating scancode first */
2001 static void tpacpi_hotkey_send_key(unsigned int scancode)
2003 tpacpi_input_send_key_masked(scancode);
2004 if (hotkey_report_mode < 2) {
2005 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2006 0x80, 0x1001 + scancode);
2010 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2012 u8 d;
2014 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2015 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2016 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2017 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2018 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2019 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2021 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2022 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2023 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2025 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2026 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2027 n->displayexp_toggle =
2028 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2030 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2031 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2032 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2033 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2034 n->brightness_toggle =
2035 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2037 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2038 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2039 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2040 >> TP_NVRAM_POS_LEVEL_VOLUME;
2041 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2042 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2046 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2047 struct tp_nvram_state *newn,
2048 const u32 event_mask)
2051 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2052 do { \
2053 if ((event_mask & (1 << __scancode)) && \
2054 oldn->__member != newn->__member) \
2055 tpacpi_hotkey_send_key(__scancode); \
2056 } while (0)
2058 #define TPACPI_MAY_SEND_KEY(__scancode) \
2059 do { \
2060 if (event_mask & (1 << __scancode)) \
2061 tpacpi_hotkey_send_key(__scancode); \
2062 } while (0)
2064 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2065 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2066 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2067 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2069 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2071 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2073 /* handle volume */
2074 if (oldn->volume_toggle != newn->volume_toggle) {
2075 if (oldn->mute != newn->mute) {
2076 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2078 if (oldn->volume_level > newn->volume_level) {
2079 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2080 } else if (oldn->volume_level < newn->volume_level) {
2081 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2082 } else if (oldn->mute == newn->mute) {
2083 /* repeated key presses that didn't change state */
2084 if (newn->mute) {
2085 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2086 } else if (newn->volume_level != 0) {
2087 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2088 } else {
2089 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2094 /* handle brightness */
2095 if (oldn->brightness_toggle != newn->brightness_toggle) {
2096 if (oldn->brightness_level < newn->brightness_level) {
2097 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2098 } else if (oldn->brightness_level > newn->brightness_level) {
2099 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2100 } else {
2101 /* repeated key presses that didn't change state */
2102 if (newn->brightness_level != 0) {
2103 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2104 } else {
2105 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2110 #undef TPACPI_COMPARE_KEY
2111 #undef TPACPI_MAY_SEND_KEY
2115 * Polling driver
2117 * We track all events in hotkey_source_mask all the time, since
2118 * most of them are edge-based. We only issue those requested by
2119 * hotkey_user_mask or hotkey_driver_mask, though.
2121 static int hotkey_kthread(void *data)
2123 struct tp_nvram_state s[2];
2124 u32 poll_mask, event_mask;
2125 unsigned int si, so;
2126 unsigned long t;
2127 unsigned int change_detector, must_reset;
2128 unsigned int poll_freq;
2130 mutex_lock(&hotkey_thread_mutex);
2132 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2133 goto exit;
2135 set_freezable();
2137 so = 0;
2138 si = 1;
2139 t = 0;
2141 /* Initial state for compares */
2142 mutex_lock(&hotkey_thread_data_mutex);
2143 change_detector = hotkey_config_change;
2144 poll_mask = hotkey_source_mask;
2145 event_mask = hotkey_source_mask &
2146 (hotkey_driver_mask | hotkey_user_mask);
2147 poll_freq = hotkey_poll_freq;
2148 mutex_unlock(&hotkey_thread_data_mutex);
2149 hotkey_read_nvram(&s[so], poll_mask);
2151 while (!kthread_should_stop()) {
2152 if (t == 0) {
2153 if (likely(poll_freq))
2154 t = 1000/poll_freq;
2155 else
2156 t = 100; /* should never happen... */
2158 t = msleep_interruptible(t);
2159 if (unlikely(kthread_should_stop()))
2160 break;
2161 must_reset = try_to_freeze();
2162 if (t > 0 && !must_reset)
2163 continue;
2165 mutex_lock(&hotkey_thread_data_mutex);
2166 if (must_reset || hotkey_config_change != change_detector) {
2167 /* forget old state on thaw or config change */
2168 si = so;
2169 t = 0;
2170 change_detector = hotkey_config_change;
2172 poll_mask = hotkey_source_mask;
2173 event_mask = hotkey_source_mask &
2174 (hotkey_driver_mask | hotkey_user_mask);
2175 poll_freq = hotkey_poll_freq;
2176 mutex_unlock(&hotkey_thread_data_mutex);
2178 if (likely(poll_mask)) {
2179 hotkey_read_nvram(&s[si], poll_mask);
2180 if (likely(si != so)) {
2181 hotkey_compare_and_issue_event(&s[so], &s[si],
2182 event_mask);
2186 so = si;
2187 si ^= 1;
2190 exit:
2191 mutex_unlock(&hotkey_thread_mutex);
2192 return 0;
2195 /* call with hotkey_mutex held */
2196 static void hotkey_poll_stop_sync(void)
2198 if (tpacpi_hotkey_task) {
2199 if (frozen(tpacpi_hotkey_task) ||
2200 freezing(tpacpi_hotkey_task))
2201 thaw_process(tpacpi_hotkey_task);
2203 kthread_stop(tpacpi_hotkey_task);
2204 tpacpi_hotkey_task = NULL;
2205 mutex_lock(&hotkey_thread_mutex);
2206 /* at this point, the thread did exit */
2207 mutex_unlock(&hotkey_thread_mutex);
2211 /* call with hotkey_mutex held */
2212 static void hotkey_poll_setup(bool may_warn)
2214 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2215 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2217 if (hotkey_poll_freq > 0 &&
2218 (poll_driver_mask ||
2219 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2220 if (!tpacpi_hotkey_task) {
2221 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2222 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2223 if (IS_ERR(tpacpi_hotkey_task)) {
2224 tpacpi_hotkey_task = NULL;
2225 printk(TPACPI_ERR
2226 "could not create kernel thread "
2227 "for hotkey polling\n");
2230 } else {
2231 hotkey_poll_stop_sync();
2232 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2233 hotkey_poll_freq == 0) {
2234 printk(TPACPI_NOTICE
2235 "hot keys 0x%08x and/or events 0x%08x "
2236 "require polling, which is currently "
2237 "disabled\n",
2238 poll_user_mask, poll_driver_mask);
2243 static void hotkey_poll_setup_safe(bool may_warn)
2245 mutex_lock(&hotkey_mutex);
2246 hotkey_poll_setup(may_warn);
2247 mutex_unlock(&hotkey_mutex);
2250 /* call with hotkey_mutex held */
2251 static void hotkey_poll_set_freq(unsigned int freq)
2253 if (!freq)
2254 hotkey_poll_stop_sync();
2256 hotkey_poll_freq = freq;
2259 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2261 static void hotkey_poll_setup_safe(bool __unused)
2265 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2267 static int hotkey_inputdev_open(struct input_dev *dev)
2269 switch (tpacpi_lifecycle) {
2270 case TPACPI_LIFE_INIT:
2272 * hotkey_init will call hotkey_poll_setup_safe
2273 * at the appropriate moment
2275 return 0;
2276 case TPACPI_LIFE_EXITING:
2277 return -EBUSY;
2278 case TPACPI_LIFE_RUNNING:
2279 hotkey_poll_setup_safe(false);
2280 return 0;
2283 /* Should only happen if tpacpi_lifecycle is corrupt */
2284 BUG();
2285 return -EBUSY;
2288 static void hotkey_inputdev_close(struct input_dev *dev)
2290 /* disable hotkey polling when possible */
2291 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2292 !(hotkey_source_mask & hotkey_driver_mask))
2293 hotkey_poll_setup_safe(false);
2296 /* sysfs hotkey enable ------------------------------------------------- */
2297 static ssize_t hotkey_enable_show(struct device *dev,
2298 struct device_attribute *attr,
2299 char *buf)
2301 int res, status;
2303 printk_deprecated_attribute("hotkey_enable",
2304 "Hotkey reporting is always enabled");
2306 res = hotkey_status_get(&status);
2307 if (res)
2308 return res;
2310 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2313 static ssize_t hotkey_enable_store(struct device *dev,
2314 struct device_attribute *attr,
2315 const char *buf, size_t count)
2317 unsigned long t;
2319 printk_deprecated_attribute("hotkey_enable",
2320 "Hotkeys can be disabled through hotkey_mask");
2322 if (parse_strtoul(buf, 1, &t))
2323 return -EINVAL;
2325 if (t == 0)
2326 return -EPERM;
2328 return count;
2331 static struct device_attribute dev_attr_hotkey_enable =
2332 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2333 hotkey_enable_show, hotkey_enable_store);
2335 /* sysfs hotkey mask --------------------------------------------------- */
2336 static ssize_t hotkey_mask_show(struct device *dev,
2337 struct device_attribute *attr,
2338 char *buf)
2340 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2343 static ssize_t hotkey_mask_store(struct device *dev,
2344 struct device_attribute *attr,
2345 const char *buf, size_t count)
2347 unsigned long t;
2348 int res;
2350 if (parse_strtoul(buf, 0xffffffffUL, &t))
2351 return -EINVAL;
2353 if (mutex_lock_killable(&hotkey_mutex))
2354 return -ERESTARTSYS;
2356 res = hotkey_user_mask_set(t);
2358 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2359 hotkey_poll_setup(true);
2360 #endif
2362 mutex_unlock(&hotkey_mutex);
2364 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2366 return (res) ? res : count;
2369 static struct device_attribute dev_attr_hotkey_mask =
2370 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2371 hotkey_mask_show, hotkey_mask_store);
2373 /* sysfs hotkey bios_enabled ------------------------------------------- */
2374 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2375 struct device_attribute *attr,
2376 char *buf)
2378 return sprintf(buf, "0\n");
2381 static struct device_attribute dev_attr_hotkey_bios_enabled =
2382 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2384 /* sysfs hotkey bios_mask ---------------------------------------------- */
2385 static ssize_t hotkey_bios_mask_show(struct device *dev,
2386 struct device_attribute *attr,
2387 char *buf)
2389 printk_deprecated_attribute("hotkey_bios_mask",
2390 "This attribute is useless.");
2391 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2394 static struct device_attribute dev_attr_hotkey_bios_mask =
2395 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2397 /* sysfs hotkey all_mask ----------------------------------------------- */
2398 static ssize_t hotkey_all_mask_show(struct device *dev,
2399 struct device_attribute *attr,
2400 char *buf)
2402 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2403 hotkey_all_mask | hotkey_source_mask);
2406 static struct device_attribute dev_attr_hotkey_all_mask =
2407 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2409 /* sysfs hotkey recommended_mask --------------------------------------- */
2410 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2411 struct device_attribute *attr,
2412 char *buf)
2414 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2415 (hotkey_all_mask | hotkey_source_mask)
2416 & ~hotkey_reserved_mask);
2419 static struct device_attribute dev_attr_hotkey_recommended_mask =
2420 __ATTR(hotkey_recommended_mask, S_IRUGO,
2421 hotkey_recommended_mask_show, NULL);
2423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2425 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2426 static ssize_t hotkey_source_mask_show(struct device *dev,
2427 struct device_attribute *attr,
2428 char *buf)
2430 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2433 static ssize_t hotkey_source_mask_store(struct device *dev,
2434 struct device_attribute *attr,
2435 const char *buf, size_t count)
2437 unsigned long t;
2438 u32 r_ev;
2439 int rc;
2441 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2442 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2443 return -EINVAL;
2445 if (mutex_lock_killable(&hotkey_mutex))
2446 return -ERESTARTSYS;
2448 HOTKEY_CONFIG_CRITICAL_START
2449 hotkey_source_mask = t;
2450 HOTKEY_CONFIG_CRITICAL_END
2452 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2453 ~hotkey_source_mask);
2454 hotkey_poll_setup(true);
2456 /* check if events needed by the driver got disabled */
2457 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2458 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2460 mutex_unlock(&hotkey_mutex);
2462 if (rc < 0)
2463 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2464 "firmware event mask!\n");
2466 if (r_ev)
2467 printk(TPACPI_NOTICE "hotkey_source_mask: "
2468 "some important events were disabled: "
2469 "0x%04x\n", r_ev);
2471 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2473 return (rc < 0) ? rc : count;
2476 static struct device_attribute dev_attr_hotkey_source_mask =
2477 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2478 hotkey_source_mask_show, hotkey_source_mask_store);
2480 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2481 static ssize_t hotkey_poll_freq_show(struct device *dev,
2482 struct device_attribute *attr,
2483 char *buf)
2485 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2488 static ssize_t hotkey_poll_freq_store(struct device *dev,
2489 struct device_attribute *attr,
2490 const char *buf, size_t count)
2492 unsigned long t;
2494 if (parse_strtoul(buf, 25, &t))
2495 return -EINVAL;
2497 if (mutex_lock_killable(&hotkey_mutex))
2498 return -ERESTARTSYS;
2500 hotkey_poll_set_freq(t);
2501 hotkey_poll_setup(true);
2503 mutex_unlock(&hotkey_mutex);
2505 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2507 return count;
2510 static struct device_attribute dev_attr_hotkey_poll_freq =
2511 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2512 hotkey_poll_freq_show, hotkey_poll_freq_store);
2514 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2516 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2517 static ssize_t hotkey_radio_sw_show(struct device *dev,
2518 struct device_attribute *attr,
2519 char *buf)
2521 int res, s;
2522 res = hotkey_get_wlsw(&s);
2523 if (res < 0)
2524 return res;
2526 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2529 static struct device_attribute dev_attr_hotkey_radio_sw =
2530 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2532 static void hotkey_radio_sw_notify_change(void)
2534 if (tp_features.hotkey_wlsw)
2535 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2536 "hotkey_radio_sw");
2539 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2540 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2541 struct device_attribute *attr,
2542 char *buf)
2544 int res, s;
2545 res = hotkey_get_tablet_mode(&s);
2546 if (res < 0)
2547 return res;
2549 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2552 static struct device_attribute dev_attr_hotkey_tablet_mode =
2553 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2555 static void hotkey_tablet_mode_notify_change(void)
2557 if (tp_features.hotkey_tablet)
2558 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2559 "hotkey_tablet_mode");
2562 /* sysfs hotkey report_mode -------------------------------------------- */
2563 static ssize_t hotkey_report_mode_show(struct device *dev,
2564 struct device_attribute *attr,
2565 char *buf)
2567 return snprintf(buf, PAGE_SIZE, "%d\n",
2568 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2571 static struct device_attribute dev_attr_hotkey_report_mode =
2572 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2574 /* sysfs wakeup reason (pollable) -------------------------------------- */
2575 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2576 struct device_attribute *attr,
2577 char *buf)
2579 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2582 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2583 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2585 static void hotkey_wakeup_reason_notify_change(void)
2587 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2588 "wakeup_reason");
2591 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2592 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2593 struct device_attribute *attr,
2594 char *buf)
2596 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2599 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2600 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2601 hotkey_wakeup_hotunplug_complete_show, NULL);
2603 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2605 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2606 "wakeup_hotunplug_complete");
2609 /* --------------------------------------------------------------------- */
2611 static struct attribute *hotkey_attributes[] __initdata = {
2612 &dev_attr_hotkey_enable.attr,
2613 &dev_attr_hotkey_bios_enabled.attr,
2614 &dev_attr_hotkey_bios_mask.attr,
2615 &dev_attr_hotkey_report_mode.attr,
2616 &dev_attr_hotkey_wakeup_reason.attr,
2617 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2618 &dev_attr_hotkey_mask.attr,
2619 &dev_attr_hotkey_all_mask.attr,
2620 &dev_attr_hotkey_recommended_mask.attr,
2621 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2622 &dev_attr_hotkey_source_mask.attr,
2623 &dev_attr_hotkey_poll_freq.attr,
2624 #endif
2627 static void bluetooth_update_rfk(void);
2628 static void wan_update_rfk(void);
2629 static void uwb_update_rfk(void);
2630 static void tpacpi_send_radiosw_update(void)
2632 int wlsw;
2634 /* Sync these BEFORE sending any rfkill events */
2635 if (tp_features.bluetooth)
2636 bluetooth_update_rfk();
2637 if (tp_features.wan)
2638 wan_update_rfk();
2639 if (tp_features.uwb)
2640 uwb_update_rfk();
2642 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2643 mutex_lock(&tpacpi_inputdev_send_mutex);
2645 input_report_switch(tpacpi_inputdev,
2646 SW_RFKILL_ALL, !!wlsw);
2647 input_sync(tpacpi_inputdev);
2649 mutex_unlock(&tpacpi_inputdev_send_mutex);
2651 hotkey_radio_sw_notify_change();
2654 static void hotkey_exit(void)
2656 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2657 mutex_lock(&hotkey_mutex);
2658 hotkey_poll_stop_sync();
2659 mutex_unlock(&hotkey_mutex);
2660 #endif
2662 if (hotkey_dev_attributes)
2663 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2665 kfree(hotkey_keycode_map);
2667 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2668 "restoring original HKEY status and mask\n");
2669 /* yes, there is a bitwise or below, we want the
2670 * functions to be called even if one of them fail */
2671 if (((tp_features.hotkey_mask &&
2672 hotkey_mask_set(hotkey_orig_mask)) |
2673 hotkey_status_set(false)) != 0)
2674 printk(TPACPI_ERR
2675 "failed to restore hot key mask "
2676 "to BIOS defaults\n");
2679 static void __init hotkey_unmap(const unsigned int scancode)
2681 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2682 clear_bit(hotkey_keycode_map[scancode],
2683 tpacpi_inputdev->keybit);
2684 hotkey_keycode_map[scancode] = KEY_RESERVED;
2689 * HKEY quirks:
2690 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2693 #define TPACPI_HK_Q_INIMASK 0x0001
2695 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2696 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2697 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2698 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2699 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2700 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2701 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2702 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2703 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2704 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2705 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2706 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2707 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2708 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2709 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2710 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2711 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2712 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2713 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2714 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2717 static int __init hotkey_init(struct ibm_init_struct *iibm)
2719 /* Requirements for changing the default keymaps:
2721 * 1. Many of the keys are mapped to KEY_RESERVED for very
2722 * good reasons. Do not change them unless you have deep
2723 * knowledge on the IBM and Lenovo ThinkPad firmware for
2724 * the various ThinkPad models. The driver behaves
2725 * differently for KEY_RESERVED: such keys have their
2726 * hot key mask *unset* in mask_recommended, and also
2727 * in the initial hot key mask programmed into the
2728 * firmware at driver load time, which means the firm-
2729 * ware may react very differently if you change them to
2730 * something else;
2732 * 2. You must be subscribed to the linux-thinkpad and
2733 * ibm-acpi-devel mailing lists, and you should read the
2734 * list archives since 2007 if you want to change the
2735 * keymaps. This requirement exists so that you will
2736 * know the past history of problems with the thinkpad-
2737 * acpi driver keymaps, and also that you will be
2738 * listening to any bug reports;
2740 * 3. Do not send thinkpad-acpi specific patches directly to
2741 * for merging, *ever*. Send them to the linux-acpi
2742 * mailinglist for comments. Merging is to be done only
2743 * through acpi-test and the ACPI maintainer.
2745 * If the above is too much to ask, don't change the keymap.
2746 * Ask the thinkpad-acpi maintainer to do it, instead.
2748 static u16 ibm_keycode_map[] __initdata = {
2749 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2750 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2751 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2752 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2754 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2755 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2756 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2757 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2759 /* brightness: firmware always reacts to them */
2760 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2761 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2763 /* Thinklight: firmware always react to it */
2764 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2766 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2767 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2769 /* Volume: firmware always react to it and reprograms
2770 * the built-in *extra* mixer. Never map it to control
2771 * another mixer by default. */
2772 KEY_RESERVED, /* 0x14: VOLUME UP */
2773 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2774 KEY_RESERVED, /* 0x16: MUTE */
2776 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2778 /* (assignments unknown, please report if found) */
2779 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2780 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2782 static u16 lenovo_keycode_map[] __initdata = {
2783 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2784 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2785 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2786 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2788 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2789 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2790 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2791 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2793 /* These should be enabled --only-- when ACPI video
2794 * is disabled (i.e. in "vendor" mode), and are handled
2795 * in a special way by the init code */
2796 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2797 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2799 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2801 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2802 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2804 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2805 * react to it and reprograms the built-in *extra* mixer.
2806 * Never map it to control another mixer by default.
2808 * T60?, T61, R60?, R61: firmware and EC tries to send
2809 * these over the regular keyboard, so these are no-ops,
2810 * but there are still weird bugs re. MUTE, so do not
2811 * change unless you get test reports from all Lenovo
2812 * models. May cause the BIOS to interfere with the
2813 * HDA mixer.
2815 KEY_RESERVED, /* 0x14: VOLUME UP */
2816 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2817 KEY_RESERVED, /* 0x16: MUTE */
2819 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2821 /* (assignments unknown, please report if found) */
2822 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2823 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2826 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2827 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2828 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2830 int res, i;
2831 int status;
2832 int hkeyv;
2834 unsigned long quirks;
2836 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2837 "initializing hotkey subdriver\n");
2839 BUG_ON(!tpacpi_inputdev);
2840 BUG_ON(tpacpi_inputdev->open != NULL ||
2841 tpacpi_inputdev->close != NULL);
2843 TPACPI_ACPIHANDLE_INIT(hkey);
2844 mutex_init(&hotkey_mutex);
2846 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2847 mutex_init(&hotkey_thread_mutex);
2848 mutex_init(&hotkey_thread_data_mutex);
2849 #endif
2851 /* hotkey not supported on 570 */
2852 tp_features.hotkey = hkey_handle != NULL;
2854 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2855 "hotkeys are %s\n",
2856 str_supported(tp_features.hotkey));
2858 if (!tp_features.hotkey)
2859 return 1;
2861 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2862 ARRAY_SIZE(tpacpi_hotkey_qtable));
2864 tpacpi_disable_brightness_delay();
2866 /* MUST have enough space for all attributes to be added to
2867 * hotkey_dev_attributes */
2868 hotkey_dev_attributes = create_attr_set(
2869 ARRAY_SIZE(hotkey_attributes) + 2,
2870 NULL);
2871 if (!hotkey_dev_attributes)
2872 return -ENOMEM;
2873 res = add_many_to_attr_set(hotkey_dev_attributes,
2874 hotkey_attributes,
2875 ARRAY_SIZE(hotkey_attributes));
2876 if (res)
2877 goto err_exit;
2879 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2880 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2881 for HKEY interface version 0x100 */
2882 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2883 if ((hkeyv >> 8) != 1) {
2884 printk(TPACPI_ERR "unknown version of the "
2885 "HKEY interface: 0x%x\n", hkeyv);
2886 printk(TPACPI_ERR "please report this to %s\n",
2887 TPACPI_MAIL);
2888 } else {
2890 * MHKV 0x100 in A31, R40, R40e,
2891 * T4x, X31, and later
2893 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2894 "firmware HKEY interface version: 0x%x\n",
2895 hkeyv);
2897 /* Paranoia check AND init hotkey_all_mask */
2898 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2899 "MHKA", "qd")) {
2900 printk(TPACPI_ERR
2901 "missing MHKA handler, "
2902 "please report this to %s\n",
2903 TPACPI_MAIL);
2904 /* Fallback: pre-init for FN+F3,F4,F12 */
2905 hotkey_all_mask = 0x080cU;
2906 } else {
2907 tp_features.hotkey_mask = 1;
2912 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2913 "hotkey masks are %s\n",
2914 str_supported(tp_features.hotkey_mask));
2916 /* Init hotkey_all_mask if not initialized yet */
2917 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2918 (quirks & TPACPI_HK_Q_INIMASK))
2919 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2921 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2922 if (tp_features.hotkey_mask) {
2923 /* hotkey_source_mask *must* be zero for
2924 * the first hotkey_mask_get to return hotkey_orig_mask */
2925 res = hotkey_mask_get();
2926 if (res)
2927 goto err_exit;
2929 hotkey_orig_mask = hotkey_acpi_mask;
2930 } else {
2931 hotkey_orig_mask = hotkey_all_mask;
2932 hotkey_acpi_mask = hotkey_all_mask;
2935 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2936 if (dbg_wlswemul) {
2937 tp_features.hotkey_wlsw = 1;
2938 printk(TPACPI_INFO
2939 "radio switch emulation enabled\n");
2940 } else
2941 #endif
2942 /* Not all thinkpads have a hardware radio switch */
2943 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2944 tp_features.hotkey_wlsw = 1;
2945 printk(TPACPI_INFO
2946 "radio switch found; radios are %s\n",
2947 enabled(status, 0));
2949 if (tp_features.hotkey_wlsw)
2950 res = add_to_attr_set(hotkey_dev_attributes,
2951 &dev_attr_hotkey_radio_sw.attr);
2953 /* For X41t, X60t, X61t Tablets... */
2954 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2955 tp_features.hotkey_tablet = 1;
2956 printk(TPACPI_INFO
2957 "possible tablet mode switch found; "
2958 "ThinkPad in %s mode\n",
2959 (status & TP_HOTKEY_TABLET_MASK)?
2960 "tablet" : "laptop");
2961 res = add_to_attr_set(hotkey_dev_attributes,
2962 &dev_attr_hotkey_tablet_mode.attr);
2965 if (!res)
2966 res = register_attr_set_with_sysfs(
2967 hotkey_dev_attributes,
2968 &tpacpi_pdev->dev.kobj);
2969 if (res)
2970 goto err_exit;
2972 /* Set up key map */
2974 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2975 GFP_KERNEL);
2976 if (!hotkey_keycode_map) {
2977 printk(TPACPI_ERR
2978 "failed to allocate memory for key map\n");
2979 res = -ENOMEM;
2980 goto err_exit;
2983 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2984 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2985 "using Lenovo default hot key map\n");
2986 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2987 TPACPI_HOTKEY_MAP_SIZE);
2988 } else {
2989 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2990 "using IBM default hot key map\n");
2991 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2992 TPACPI_HOTKEY_MAP_SIZE);
2995 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2996 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2997 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2998 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2999 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3000 tpacpi_inputdev->keycode = hotkey_keycode_map;
3001 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3002 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3003 set_bit(hotkey_keycode_map[i],
3004 tpacpi_inputdev->keybit);
3005 } else {
3006 if (i < sizeof(hotkey_reserved_mask)*8)
3007 hotkey_reserved_mask |= 1 << i;
3011 if (tp_features.hotkey_wlsw) {
3012 set_bit(EV_SW, tpacpi_inputdev->evbit);
3013 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3015 if (tp_features.hotkey_tablet) {
3016 set_bit(EV_SW, tpacpi_inputdev->evbit);
3017 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3020 /* Do not issue duplicate brightness change events to
3021 * userspace */
3022 if (!tp_features.bright_acpimode)
3023 /* update bright_acpimode... */
3024 tpacpi_check_std_acpi_brightness_support();
3026 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3027 printk(TPACPI_INFO
3028 "This ThinkPad has standard ACPI backlight "
3029 "brightness control, supported by the ACPI "
3030 "video driver\n");
3031 printk(TPACPI_NOTICE
3032 "Disabling thinkpad-acpi brightness events "
3033 "by default...\n");
3035 /* Disable brightness up/down on Lenovo thinkpads when
3036 * ACPI is handling them, otherwise it is plain impossible
3037 * for userspace to do something even remotely sane */
3038 hotkey_reserved_mask |=
3039 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3040 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3041 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3042 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3045 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3046 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3047 & ~hotkey_all_mask
3048 & ~hotkey_reserved_mask;
3050 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3051 "hotkey source mask 0x%08x, polling freq %u\n",
3052 hotkey_source_mask, hotkey_poll_freq);
3053 #endif
3055 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3056 "enabling firmware HKEY event interface...\n");
3057 res = hotkey_status_set(true);
3058 if (res) {
3059 hotkey_exit();
3060 return res;
3062 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3063 | hotkey_driver_mask)
3064 & ~hotkey_source_mask);
3065 if (res < 0 && res != -ENXIO) {
3066 hotkey_exit();
3067 return res;
3069 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3070 & ~hotkey_reserved_mask;
3071 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3072 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3073 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3075 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3076 "legacy ibm/hotkey event reporting over procfs %s\n",
3077 (hotkey_report_mode < 2) ?
3078 "enabled" : "disabled");
3080 tpacpi_inputdev->open = &hotkey_inputdev_open;
3081 tpacpi_inputdev->close = &hotkey_inputdev_close;
3083 hotkey_poll_setup_safe(true);
3084 tpacpi_send_radiosw_update();
3085 tpacpi_input_send_tabletsw();
3087 return 0;
3089 err_exit:
3090 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3091 hotkey_dev_attributes = NULL;
3093 return (res < 0)? res : 1;
3096 static bool hotkey_notify_hotkey(const u32 hkey,
3097 bool *send_acpi_ev,
3098 bool *ignore_acpi_ev)
3100 /* 0x1000-0x1FFF: key presses */
3101 unsigned int scancode = hkey & 0xfff;
3102 *send_acpi_ev = true;
3103 *ignore_acpi_ev = false;
3105 if (scancode > 0 && scancode < 0x21) {
3106 scancode--;
3107 if (!(hotkey_source_mask & (1 << scancode))) {
3108 tpacpi_input_send_key_masked(scancode);
3109 *send_acpi_ev = false;
3110 } else {
3111 *ignore_acpi_ev = true;
3113 return true;
3115 return false;
3118 static bool hotkey_notify_wakeup(const u32 hkey,
3119 bool *send_acpi_ev,
3120 bool *ignore_acpi_ev)
3122 /* 0x2000-0x2FFF: Wakeup reason */
3123 *send_acpi_ev = true;
3124 *ignore_acpi_ev = false;
3126 switch (hkey) {
3127 case 0x2304: /* suspend, undock */
3128 case 0x2404: /* hibernation, undock */
3129 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3130 *ignore_acpi_ev = true;
3131 break;
3133 case 0x2305: /* suspend, bay eject */
3134 case 0x2405: /* hibernation, bay eject */
3135 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3136 *ignore_acpi_ev = true;
3137 break;
3139 case 0x2313: /* Battery on critical low level (S3) */
3140 case 0x2413: /* Battery on critical low level (S4) */
3141 printk(TPACPI_ALERT
3142 "EMERGENCY WAKEUP: battery almost empty\n");
3143 /* how to auto-heal: */
3144 /* 2313: woke up from S3, go to S4/S5 */
3145 /* 2413: woke up from S4, go to S5 */
3146 break;
3148 default:
3149 return false;
3152 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3153 printk(TPACPI_INFO
3154 "woke up due to a hot-unplug "
3155 "request...\n");
3156 hotkey_wakeup_reason_notify_change();
3158 return true;
3161 static bool hotkey_notify_usrevent(const u32 hkey,
3162 bool *send_acpi_ev,
3163 bool *ignore_acpi_ev)
3165 /* 0x5000-0x5FFF: human interface helpers */
3166 *send_acpi_ev = true;
3167 *ignore_acpi_ev = false;
3169 switch (hkey) {
3170 case 0x500b: /* X61t: tablet pen inserted into bay */
3171 case 0x500c: /* X61t: tablet pen removed from bay */
3172 return true;
3174 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3175 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3176 tpacpi_input_send_tabletsw();
3177 hotkey_tablet_mode_notify_change();
3178 *send_acpi_ev = false;
3179 return true;
3181 case 0x5001: /* Lid close */
3182 case 0x5002: /* Lid open */
3183 case 0x5010: /* brightness control */
3184 /* do not propagate these events */
3185 *ignore_acpi_ev = true;
3186 return true;
3188 default:
3189 return false;
3193 static bool hotkey_notify_thermal(const u32 hkey,
3194 bool *send_acpi_ev,
3195 bool *ignore_acpi_ev)
3197 /* 0x6000-0x6FFF: thermal alarms */
3198 *send_acpi_ev = true;
3199 *ignore_acpi_ev = false;
3201 switch (hkey) {
3202 case 0x6011:
3203 printk(TPACPI_CRIT
3204 "THERMAL ALARM: battery is too hot!\n");
3205 /* recommended action: warn user through gui */
3206 return true;
3207 case 0x6012:
3208 printk(TPACPI_ALERT
3209 "THERMAL EMERGENCY: battery is extremely hot!\n");
3210 /* recommended action: immediate sleep/hibernate */
3211 return true;
3212 case 0x6021:
3213 printk(TPACPI_CRIT
3214 "THERMAL ALARM: "
3215 "a sensor reports something is too hot!\n");
3216 /* recommended action: warn user through gui, that */
3217 /* some internal component is too hot */
3218 return true;
3219 case 0x6022:
3220 printk(TPACPI_ALERT
3221 "THERMAL EMERGENCY: "
3222 "a sensor reports something is extremely hot!\n");
3223 /* recommended action: immediate sleep/hibernate */
3224 return true;
3225 case 0x6030:
3226 printk(TPACPI_INFO
3227 "EC reports that Thermal Table has changed\n");
3228 /* recommended action: do nothing, we don't have
3229 * Lenovo ATM information */
3230 return true;
3231 default:
3232 printk(TPACPI_ALERT
3233 "THERMAL ALERT: unknown thermal alarm received\n");
3234 return false;
3238 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3240 u32 hkey;
3241 bool send_acpi_ev;
3242 bool ignore_acpi_ev;
3243 bool known_ev;
3245 if (event != 0x80) {
3246 printk(TPACPI_ERR
3247 "unknown HKEY notification event %d\n", event);
3248 /* forward it to userspace, maybe it knows how to handle it */
3249 acpi_bus_generate_netlink_event(
3250 ibm->acpi->device->pnp.device_class,
3251 dev_name(&ibm->acpi->device->dev),
3252 event, 0);
3253 return;
3256 while (1) {
3257 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3258 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3259 return;
3262 if (hkey == 0) {
3263 /* queue empty */
3264 return;
3267 send_acpi_ev = true;
3268 ignore_acpi_ev = false;
3270 switch (hkey >> 12) {
3271 case 1:
3272 /* 0x1000-0x1FFF: key presses */
3273 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3274 &ignore_acpi_ev);
3275 break;
3276 case 2:
3277 /* 0x2000-0x2FFF: Wakeup reason */
3278 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3279 &ignore_acpi_ev);
3280 break;
3281 case 3:
3282 /* 0x3000-0x3FFF: bay-related wakeups */
3283 if (hkey == 0x3003) {
3284 hotkey_autosleep_ack = 1;
3285 printk(TPACPI_INFO
3286 "bay ejected\n");
3287 hotkey_wakeup_hotunplug_complete_notify_change();
3288 known_ev = true;
3289 } else {
3290 known_ev = false;
3292 break;
3293 case 4:
3294 /* 0x4000-0x4FFF: dock-related wakeups */
3295 if (hkey == 0x4003) {
3296 hotkey_autosleep_ack = 1;
3297 printk(TPACPI_INFO
3298 "undocked\n");
3299 hotkey_wakeup_hotunplug_complete_notify_change();
3300 known_ev = true;
3301 } else {
3302 known_ev = false;
3304 break;
3305 case 5:
3306 /* 0x5000-0x5FFF: human interface helpers */
3307 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3308 &ignore_acpi_ev);
3309 break;
3310 case 6:
3311 /* 0x6000-0x6FFF: thermal alarms */
3312 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3313 &ignore_acpi_ev);
3314 break;
3315 case 7:
3316 /* 0x7000-0x7FFF: misc */
3317 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3318 tpacpi_send_radiosw_update();
3319 send_acpi_ev = 0;
3320 known_ev = true;
3321 break;
3323 /* fallthrough to default */
3324 default:
3325 known_ev = false;
3327 if (!known_ev) {
3328 printk(TPACPI_NOTICE
3329 "unhandled HKEY event 0x%04x\n", hkey);
3330 printk(TPACPI_NOTICE
3331 "please report the conditions when this "
3332 "event happened to %s\n", TPACPI_MAIL);
3335 /* Legacy events */
3336 if (!ignore_acpi_ev &&
3337 (send_acpi_ev || hotkey_report_mode < 2)) {
3338 acpi_bus_generate_proc_event(ibm->acpi->device,
3339 event, hkey);
3342 /* netlink events */
3343 if (!ignore_acpi_ev && send_acpi_ev) {
3344 acpi_bus_generate_netlink_event(
3345 ibm->acpi->device->pnp.device_class,
3346 dev_name(&ibm->acpi->device->dev),
3347 event, hkey);
3352 static void hotkey_suspend(pm_message_t state)
3354 /* Do these on suspend, we get the events on early resume! */
3355 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3356 hotkey_autosleep_ack = 0;
3359 static void hotkey_resume(void)
3361 tpacpi_disable_brightness_delay();
3363 if (hotkey_status_set(true) < 0 ||
3364 hotkey_mask_set(hotkey_acpi_mask) < 0)
3365 printk(TPACPI_ERR
3366 "error while attempting to reset the event "
3367 "firmware interface\n");
3369 tpacpi_send_radiosw_update();
3370 hotkey_tablet_mode_notify_change();
3371 hotkey_wakeup_reason_notify_change();
3372 hotkey_wakeup_hotunplug_complete_notify_change();
3373 hotkey_poll_setup_safe(false);
3376 /* procfs -------------------------------------------------------------- */
3377 static int hotkey_read(char *p)
3379 int res, status;
3380 int len = 0;
3382 if (!tp_features.hotkey) {
3383 len += sprintf(p + len, "status:\t\tnot supported\n");
3384 return len;
3387 if (mutex_lock_killable(&hotkey_mutex))
3388 return -ERESTARTSYS;
3389 res = hotkey_status_get(&status);
3390 if (!res)
3391 res = hotkey_mask_get();
3392 mutex_unlock(&hotkey_mutex);
3393 if (res)
3394 return res;
3396 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3397 if (hotkey_all_mask) {
3398 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3399 len += sprintf(p + len,
3400 "commands:\tenable, disable, reset, <mask>\n");
3401 } else {
3402 len += sprintf(p + len, "mask:\t\tnot supported\n");
3403 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3406 return len;
3409 static void hotkey_enabledisable_warn(bool enable)
3411 tpacpi_log_usertask("procfs hotkey enable/disable");
3412 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3413 TPACPI_WARN
3414 "hotkey enable/disable functionality has been "
3415 "removed from the driver. Hotkeys are always "
3416 "enabled\n"))
3417 printk(TPACPI_ERR
3418 "Please remove the hotkey=enable module "
3419 "parameter, it is deprecated. Hotkeys are always "
3420 "enabled\n");
3423 static int hotkey_write(char *buf)
3425 int res;
3426 u32 mask;
3427 char *cmd;
3429 if (!tp_features.hotkey)
3430 return -ENODEV;
3432 if (mutex_lock_killable(&hotkey_mutex))
3433 return -ERESTARTSYS;
3435 mask = hotkey_user_mask;
3437 res = 0;
3438 while ((cmd = next_cmd(&buf))) {
3439 if (strlencmp(cmd, "enable") == 0) {
3440 hotkey_enabledisable_warn(1);
3441 } else if (strlencmp(cmd, "disable") == 0) {
3442 hotkey_enabledisable_warn(0);
3443 res = -EPERM;
3444 } else if (strlencmp(cmd, "reset") == 0) {
3445 mask = (hotkey_all_mask | hotkey_source_mask)
3446 & ~hotkey_reserved_mask;
3447 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3448 /* mask set */
3449 } else if (sscanf(cmd, "%x", &mask) == 1) {
3450 /* mask set */
3451 } else {
3452 res = -EINVAL;
3453 goto errexit;
3457 if (!res) {
3458 tpacpi_disclose_usertask("procfs hotkey",
3459 "set mask to 0x%08x\n", mask);
3460 res = hotkey_user_mask_set(mask);
3463 errexit:
3464 mutex_unlock(&hotkey_mutex);
3465 return res;
3468 static const struct acpi_device_id ibm_htk_device_ids[] = {
3469 {TPACPI_ACPI_HKEY_HID, 0},
3470 {"", 0},
3473 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3474 .hid = ibm_htk_device_ids,
3475 .notify = hotkey_notify,
3476 .handle = &hkey_handle,
3477 .type = ACPI_DEVICE_NOTIFY,
3480 static struct ibm_struct hotkey_driver_data = {
3481 .name = "hotkey",
3482 .read = hotkey_read,
3483 .write = hotkey_write,
3484 .exit = hotkey_exit,
3485 .resume = hotkey_resume,
3486 .suspend = hotkey_suspend,
3487 .acpi = &ibm_hotkey_acpidriver,
3490 /*************************************************************************
3491 * Bluetooth subdriver
3494 enum {
3495 /* ACPI GBDC/SBDC bits */
3496 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3497 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3498 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3499 off / last state */
3502 enum {
3503 /* ACPI \BLTH commands */
3504 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3505 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3506 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3507 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3508 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3511 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3513 static struct rfkill *tpacpi_bluetooth_rfkill;
3515 static void bluetooth_suspend(pm_message_t state)
3517 /* Try to make sure radio will resume powered off */
3518 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3519 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3520 vdbg_printk(TPACPI_DBG_RFKILL,
3521 "bluetooth power down on resume request failed\n");
3524 static int bluetooth_get_radiosw(void)
3526 int status;
3528 if (!tp_features.bluetooth)
3529 return -ENODEV;
3531 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3532 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3533 return RFKILL_STATE_HARD_BLOCKED;
3535 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3536 if (dbg_bluetoothemul)
3537 return (tpacpi_bluetooth_emulstate) ?
3538 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3539 #endif
3541 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3542 return -EIO;
3544 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3545 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3548 static void bluetooth_update_rfk(void)
3550 int status;
3552 if (!tpacpi_bluetooth_rfkill)
3553 return;
3555 status = bluetooth_get_radiosw();
3556 if (status < 0)
3557 return;
3558 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3560 vdbg_printk(TPACPI_DBG_RFKILL,
3561 "forced rfkill state to %d\n",
3562 status);
3565 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3567 int status;
3569 if (!tp_features.bluetooth)
3570 return -ENODEV;
3572 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3573 * reason to risk weird behaviour. */
3574 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3575 && radio_on)
3576 return -EPERM;
3578 vdbg_printk(TPACPI_DBG_RFKILL,
3579 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3581 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3582 if (dbg_bluetoothemul) {
3583 tpacpi_bluetooth_emulstate = !!radio_on;
3584 if (update_rfk)
3585 bluetooth_update_rfk();
3586 return 0;
3588 #endif
3590 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3591 if (radio_on)
3592 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3593 else
3594 status = 0;
3595 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3596 return -EIO;
3598 if (update_rfk)
3599 bluetooth_update_rfk();
3601 return 0;
3604 /* sysfs bluetooth enable ---------------------------------------------- */
3605 static ssize_t bluetooth_enable_show(struct device *dev,
3606 struct device_attribute *attr,
3607 char *buf)
3609 int status;
3611 printk_deprecated_rfkill_attribute("bluetooth_enable");
3613 status = bluetooth_get_radiosw();
3614 if (status < 0)
3615 return status;
3617 return snprintf(buf, PAGE_SIZE, "%d\n",
3618 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3621 static ssize_t bluetooth_enable_store(struct device *dev,
3622 struct device_attribute *attr,
3623 const char *buf, size_t count)
3625 unsigned long t;
3626 int res;
3628 printk_deprecated_rfkill_attribute("bluetooth_enable");
3630 if (parse_strtoul(buf, 1, &t))
3631 return -EINVAL;
3633 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3635 res = bluetooth_set_radiosw(t, 1);
3637 return (res) ? res : count;
3640 static struct device_attribute dev_attr_bluetooth_enable =
3641 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3642 bluetooth_enable_show, bluetooth_enable_store);
3644 /* --------------------------------------------------------------------- */
3646 static struct attribute *bluetooth_attributes[] = {
3647 &dev_attr_bluetooth_enable.attr,
3648 NULL
3651 static const struct attribute_group bluetooth_attr_group = {
3652 .attrs = bluetooth_attributes,
3655 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3657 int bts = bluetooth_get_radiosw();
3659 if (bts < 0)
3660 return bts;
3662 *state = bts;
3663 return 0;
3666 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3668 dbg_printk(TPACPI_DBG_RFKILL,
3669 "request to change radio state to %d\n", state);
3670 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3673 static void bluetooth_shutdown(void)
3675 /* Order firmware to save current state to NVRAM */
3676 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3677 TP_ACPI_BLTH_SAVE_STATE))
3678 printk(TPACPI_NOTICE
3679 "failed to save bluetooth state to NVRAM\n");
3680 else
3681 vdbg_printk(TPACPI_DBG_RFKILL,
3682 "bluestooth state saved to NVRAM\n");
3685 static void bluetooth_exit(void)
3687 bluetooth_shutdown();
3689 if (tpacpi_bluetooth_rfkill)
3690 rfkill_unregister(tpacpi_bluetooth_rfkill);
3692 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3693 &bluetooth_attr_group);
3696 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3698 int res;
3699 int status = 0;
3701 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3702 "initializing bluetooth subdriver\n");
3704 TPACPI_ACPIHANDLE_INIT(hkey);
3706 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3707 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3708 tp_features.bluetooth = hkey_handle &&
3709 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3711 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3712 "bluetooth is %s, status 0x%02x\n",
3713 str_supported(tp_features.bluetooth),
3714 status);
3716 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3717 if (dbg_bluetoothemul) {
3718 tp_features.bluetooth = 1;
3719 printk(TPACPI_INFO
3720 "bluetooth switch emulation enabled\n");
3721 } else
3722 #endif
3723 if (tp_features.bluetooth &&
3724 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3725 /* no bluetooth hardware present in system */
3726 tp_features.bluetooth = 0;
3727 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3728 "bluetooth hardware not installed\n");
3731 if (!tp_features.bluetooth)
3732 return 1;
3734 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3735 &bluetooth_attr_group);
3736 if (res)
3737 return res;
3739 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3740 &tpacpi_bluetooth_rfkill,
3741 RFKILL_TYPE_BLUETOOTH,
3742 TPACPI_RFK_BLUETOOTH_SW_NAME,
3743 true,
3744 tpacpi_bluetooth_rfk_set,
3745 tpacpi_bluetooth_rfk_get);
3746 if (res) {
3747 bluetooth_exit();
3748 return res;
3751 return 0;
3754 /* procfs -------------------------------------------------------------- */
3755 static int bluetooth_read(char *p)
3757 int len = 0;
3758 int status = bluetooth_get_radiosw();
3760 if (!tp_features.bluetooth)
3761 len += sprintf(p + len, "status:\t\tnot supported\n");
3762 else {
3763 len += sprintf(p + len, "status:\t\t%s\n",
3764 (status == RFKILL_STATE_UNBLOCKED) ?
3765 "enabled" : "disabled");
3766 len += sprintf(p + len, "commands:\tenable, disable\n");
3769 return len;
3772 static int bluetooth_write(char *buf)
3774 char *cmd;
3775 int state = -1;
3777 if (!tp_features.bluetooth)
3778 return -ENODEV;
3780 while ((cmd = next_cmd(&buf))) {
3781 if (strlencmp(cmd, "enable") == 0) {
3782 state = 1;
3783 } else if (strlencmp(cmd, "disable") == 0) {
3784 state = 0;
3785 } else
3786 return -EINVAL;
3789 if (state != -1) {
3790 tpacpi_disclose_usertask("procfs bluetooth",
3791 "attempt to %s\n",
3792 state ? "enable" : "disable");
3793 bluetooth_set_radiosw(state, 1);
3796 return 0;
3799 static struct ibm_struct bluetooth_driver_data = {
3800 .name = "bluetooth",
3801 .read = bluetooth_read,
3802 .write = bluetooth_write,
3803 .exit = bluetooth_exit,
3804 .suspend = bluetooth_suspend,
3805 .shutdown = bluetooth_shutdown,
3808 /*************************************************************************
3809 * Wan subdriver
3812 enum {
3813 /* ACPI GWAN/SWAN bits */
3814 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3815 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3816 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3817 off / last state */
3820 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3822 static struct rfkill *tpacpi_wan_rfkill;
3824 static void wan_suspend(pm_message_t state)
3826 /* Try to make sure radio will resume powered off */
3827 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3828 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3829 vdbg_printk(TPACPI_DBG_RFKILL,
3830 "WWAN power down on resume request failed\n");
3833 static int wan_get_radiosw(void)
3835 int status;
3837 if (!tp_features.wan)
3838 return -ENODEV;
3840 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3841 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3842 return RFKILL_STATE_HARD_BLOCKED;
3844 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3845 if (dbg_wwanemul)
3846 return (tpacpi_wwan_emulstate) ?
3847 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3848 #endif
3850 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3851 return -EIO;
3853 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3854 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3857 static void wan_update_rfk(void)
3859 int status;
3861 if (!tpacpi_wan_rfkill)
3862 return;
3864 status = wan_get_radiosw();
3865 if (status < 0)
3866 return;
3867 rfkill_force_state(tpacpi_wan_rfkill, status);
3869 vdbg_printk(TPACPI_DBG_RFKILL,
3870 "forced rfkill state to %d\n",
3871 status);
3874 static int wan_set_radiosw(int radio_on, int update_rfk)
3876 int status;
3878 if (!tp_features.wan)
3879 return -ENODEV;
3881 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3882 * reason to risk weird behaviour. */
3883 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3884 && radio_on)
3885 return -EPERM;
3887 vdbg_printk(TPACPI_DBG_RFKILL,
3888 "will %s WWAN\n", radio_on ? "enable" : "disable");
3890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_wwanemul) {
3892 tpacpi_wwan_emulstate = !!radio_on;
3893 if (update_rfk)
3894 wan_update_rfk();
3895 return 0;
3897 #endif
3899 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3900 if (radio_on)
3901 status = TP_ACPI_WANCARD_RADIOSSW;
3902 else
3903 status = 0;
3904 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3905 return -EIO;
3907 if (update_rfk)
3908 wan_update_rfk();
3910 return 0;
3913 /* sysfs wan enable ---------------------------------------------------- */
3914 static ssize_t wan_enable_show(struct device *dev,
3915 struct device_attribute *attr,
3916 char *buf)
3918 int status;
3920 printk_deprecated_rfkill_attribute("wwan_enable");
3922 status = wan_get_radiosw();
3923 if (status < 0)
3924 return status;
3926 return snprintf(buf, PAGE_SIZE, "%d\n",
3927 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3930 static ssize_t wan_enable_store(struct device *dev,
3931 struct device_attribute *attr,
3932 const char *buf, size_t count)
3934 unsigned long t;
3935 int res;
3937 printk_deprecated_rfkill_attribute("wwan_enable");
3939 if (parse_strtoul(buf, 1, &t))
3940 return -EINVAL;
3942 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3944 res = wan_set_radiosw(t, 1);
3946 return (res) ? res : count;
3949 static struct device_attribute dev_attr_wan_enable =
3950 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3951 wan_enable_show, wan_enable_store);
3953 /* --------------------------------------------------------------------- */
3955 static struct attribute *wan_attributes[] = {
3956 &dev_attr_wan_enable.attr,
3957 NULL
3960 static const struct attribute_group wan_attr_group = {
3961 .attrs = wan_attributes,
3964 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3966 int wans = wan_get_radiosw();
3968 if (wans < 0)
3969 return wans;
3971 *state = wans;
3972 return 0;
3975 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3977 dbg_printk(TPACPI_DBG_RFKILL,
3978 "request to change radio state to %d\n", state);
3979 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3982 static void wan_shutdown(void)
3984 /* Order firmware to save current state to NVRAM */
3985 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3986 TP_ACPI_WGSV_SAVE_STATE))
3987 printk(TPACPI_NOTICE
3988 "failed to save WWAN state to NVRAM\n");
3989 else
3990 vdbg_printk(TPACPI_DBG_RFKILL,
3991 "WWAN state saved to NVRAM\n");
3994 static void wan_exit(void)
3996 wan_shutdown();
3998 if (tpacpi_wan_rfkill)
3999 rfkill_unregister(tpacpi_wan_rfkill);
4001 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4002 &wan_attr_group);
4005 static int __init wan_init(struct ibm_init_struct *iibm)
4007 int res;
4008 int status = 0;
4010 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4011 "initializing wan subdriver\n");
4013 TPACPI_ACPIHANDLE_INIT(hkey);
4015 tp_features.wan = hkey_handle &&
4016 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4018 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4019 "wan is %s, status 0x%02x\n",
4020 str_supported(tp_features.wan),
4021 status);
4023 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4024 if (dbg_wwanemul) {
4025 tp_features.wan = 1;
4026 printk(TPACPI_INFO
4027 "wwan switch emulation enabled\n");
4028 } else
4029 #endif
4030 if (tp_features.wan &&
4031 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4032 /* no wan hardware present in system */
4033 tp_features.wan = 0;
4034 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4035 "wan hardware not installed\n");
4038 if (!tp_features.wan)
4039 return 1;
4041 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4042 &wan_attr_group);
4043 if (res)
4044 return res;
4046 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4047 &tpacpi_wan_rfkill,
4048 RFKILL_TYPE_WWAN,
4049 TPACPI_RFK_WWAN_SW_NAME,
4050 true,
4051 tpacpi_wan_rfk_set,
4052 tpacpi_wan_rfk_get);
4053 if (res) {
4054 wan_exit();
4055 return res;
4058 return 0;
4061 /* procfs -------------------------------------------------------------- */
4062 static int wan_read(char *p)
4064 int len = 0;
4065 int status = wan_get_radiosw();
4067 tpacpi_disclose_usertask("procfs wan", "read");
4069 if (!tp_features.wan)
4070 len += sprintf(p + len, "status:\t\tnot supported\n");
4071 else {
4072 len += sprintf(p + len, "status:\t\t%s\n",
4073 (status == RFKILL_STATE_UNBLOCKED) ?
4074 "enabled" : "disabled");
4075 len += sprintf(p + len, "commands:\tenable, disable\n");
4078 return len;
4081 static int wan_write(char *buf)
4083 char *cmd;
4084 int state = -1;
4086 if (!tp_features.wan)
4087 return -ENODEV;
4089 while ((cmd = next_cmd(&buf))) {
4090 if (strlencmp(cmd, "enable") == 0) {
4091 state = 1;
4092 } else if (strlencmp(cmd, "disable") == 0) {
4093 state = 0;
4094 } else
4095 return -EINVAL;
4098 if (state != -1) {
4099 tpacpi_disclose_usertask("procfs wan",
4100 "attempt to %s\n",
4101 state ? "enable" : "disable");
4102 wan_set_radiosw(state, 1);
4105 return 0;
4108 static struct ibm_struct wan_driver_data = {
4109 .name = "wan",
4110 .read = wan_read,
4111 .write = wan_write,
4112 .exit = wan_exit,
4113 .suspend = wan_suspend,
4114 .shutdown = wan_shutdown,
4117 /*************************************************************************
4118 * UWB subdriver
4121 enum {
4122 /* ACPI GUWB/SUWB bits */
4123 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4124 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4127 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4129 static struct rfkill *tpacpi_uwb_rfkill;
4131 static int uwb_get_radiosw(void)
4133 int status;
4135 if (!tp_features.uwb)
4136 return -ENODEV;
4138 /* WLSW overrides UWB in firmware/hardware, reflect that */
4139 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4140 return RFKILL_STATE_HARD_BLOCKED;
4142 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4143 if (dbg_uwbemul)
4144 return (tpacpi_uwb_emulstate) ?
4145 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4146 #endif
4148 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4149 return -EIO;
4151 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4152 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4155 static void uwb_update_rfk(void)
4157 int status;
4159 if (!tpacpi_uwb_rfkill)
4160 return;
4162 status = uwb_get_radiosw();
4163 if (status < 0)
4164 return;
4165 rfkill_force_state(tpacpi_uwb_rfkill, status);
4167 vdbg_printk(TPACPI_DBG_RFKILL,
4168 "forced rfkill state to %d\n",
4169 status);
4172 static int uwb_set_radiosw(int radio_on, int update_rfk)
4174 int status;
4176 if (!tp_features.uwb)
4177 return -ENODEV;
4179 /* WLSW overrides UWB in firmware/hardware, but there is no
4180 * reason to risk weird behaviour. */
4181 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4182 && radio_on)
4183 return -EPERM;
4185 vdbg_printk(TPACPI_DBG_RFKILL,
4186 "will %s UWB\n", radio_on ? "enable" : "disable");
4188 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4189 if (dbg_uwbemul) {
4190 tpacpi_uwb_emulstate = !!radio_on;
4191 if (update_rfk)
4192 uwb_update_rfk();
4193 return 0;
4195 #endif
4197 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4198 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4199 return -EIO;
4201 if (update_rfk)
4202 uwb_update_rfk();
4204 return 0;
4207 /* --------------------------------------------------------------------- */
4209 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4211 int uwbs = uwb_get_radiosw();
4213 if (uwbs < 0)
4214 return uwbs;
4216 *state = uwbs;
4217 return 0;
4220 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4222 dbg_printk(TPACPI_DBG_RFKILL,
4223 "request to change radio state to %d\n", state);
4224 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4227 static void uwb_exit(void)
4229 if (tpacpi_uwb_rfkill)
4230 rfkill_unregister(tpacpi_uwb_rfkill);
4233 static int __init uwb_init(struct ibm_init_struct *iibm)
4235 int res;
4236 int status = 0;
4238 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4239 "initializing uwb subdriver\n");
4241 TPACPI_ACPIHANDLE_INIT(hkey);
4243 tp_features.uwb = hkey_handle &&
4244 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4246 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4247 "uwb is %s, status 0x%02x\n",
4248 str_supported(tp_features.uwb),
4249 status);
4251 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4252 if (dbg_uwbemul) {
4253 tp_features.uwb = 1;
4254 printk(TPACPI_INFO
4255 "uwb switch emulation enabled\n");
4256 } else
4257 #endif
4258 if (tp_features.uwb &&
4259 !(status & TP_ACPI_UWB_HWPRESENT)) {
4260 /* no uwb hardware present in system */
4261 tp_features.uwb = 0;
4262 dbg_printk(TPACPI_DBG_INIT,
4263 "uwb hardware not installed\n");
4266 if (!tp_features.uwb)
4267 return 1;
4269 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4270 &tpacpi_uwb_rfkill,
4271 RFKILL_TYPE_UWB,
4272 TPACPI_RFK_UWB_SW_NAME,
4273 false,
4274 tpacpi_uwb_rfk_set,
4275 tpacpi_uwb_rfk_get);
4277 return res;
4280 static struct ibm_struct uwb_driver_data = {
4281 .name = "uwb",
4282 .exit = uwb_exit,
4283 .flags.experimental = 1,
4286 /*************************************************************************
4287 * Video subdriver
4290 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4292 enum video_access_mode {
4293 TPACPI_VIDEO_NONE = 0,
4294 TPACPI_VIDEO_570, /* 570 */
4295 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4296 TPACPI_VIDEO_NEW, /* all others */
4299 enum { /* video status flags, based on VIDEO_570 */
4300 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4301 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4302 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4305 enum { /* TPACPI_VIDEO_570 constants */
4306 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4307 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4308 * video_status_flags */
4309 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4310 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4313 static enum video_access_mode video_supported;
4314 static int video_orig_autosw;
4316 static int video_autosw_get(void);
4317 static int video_autosw_set(int enable);
4319 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4321 static int __init video_init(struct ibm_init_struct *iibm)
4323 int ivga;
4325 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4327 TPACPI_ACPIHANDLE_INIT(vid);
4328 TPACPI_ACPIHANDLE_INIT(vid2);
4330 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4331 /* G41, assume IVGA doesn't change */
4332 vid_handle = vid2_handle;
4334 if (!vid_handle)
4335 /* video switching not supported on R30, R31 */
4336 video_supported = TPACPI_VIDEO_NONE;
4337 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4338 /* 570 */
4339 video_supported = TPACPI_VIDEO_570;
4340 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4341 /* 600e/x, 770e, 770x */
4342 video_supported = TPACPI_VIDEO_770;
4343 else
4344 /* all others */
4345 video_supported = TPACPI_VIDEO_NEW;
4347 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4348 str_supported(video_supported != TPACPI_VIDEO_NONE),
4349 video_supported);
4351 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4354 static void video_exit(void)
4356 dbg_printk(TPACPI_DBG_EXIT,
4357 "restoring original video autoswitch mode\n");
4358 if (video_autosw_set(video_orig_autosw))
4359 printk(TPACPI_ERR "error while trying to restore original "
4360 "video autoswitch mode\n");
4363 static int video_outputsw_get(void)
4365 int status = 0;
4366 int i;
4368 switch (video_supported) {
4369 case TPACPI_VIDEO_570:
4370 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4371 TP_ACPI_VIDEO_570_PHSCMD))
4372 return -EIO;
4373 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4374 break;
4375 case TPACPI_VIDEO_770:
4376 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4377 return -EIO;
4378 if (i)
4379 status |= TP_ACPI_VIDEO_S_LCD;
4380 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4381 return -EIO;
4382 if (i)
4383 status |= TP_ACPI_VIDEO_S_CRT;
4384 break;
4385 case TPACPI_VIDEO_NEW:
4386 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4387 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4388 return -EIO;
4389 if (i)
4390 status |= TP_ACPI_VIDEO_S_CRT;
4392 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4393 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4394 return -EIO;
4395 if (i)
4396 status |= TP_ACPI_VIDEO_S_LCD;
4397 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4398 return -EIO;
4399 if (i)
4400 status |= TP_ACPI_VIDEO_S_DVI;
4401 break;
4402 default:
4403 return -ENOSYS;
4406 return status;
4409 static int video_outputsw_set(int status)
4411 int autosw;
4412 int res = 0;
4414 switch (video_supported) {
4415 case TPACPI_VIDEO_570:
4416 res = acpi_evalf(NULL, NULL,
4417 "\\_SB.PHS2", "vdd",
4418 TP_ACPI_VIDEO_570_PHS2CMD,
4419 status | TP_ACPI_VIDEO_570_PHS2SET);
4420 break;
4421 case TPACPI_VIDEO_770:
4422 autosw = video_autosw_get();
4423 if (autosw < 0)
4424 return autosw;
4426 res = video_autosw_set(1);
4427 if (res)
4428 return res;
4429 res = acpi_evalf(vid_handle, NULL,
4430 "ASWT", "vdd", status * 0x100, 0);
4431 if (!autosw && video_autosw_set(autosw)) {
4432 printk(TPACPI_ERR
4433 "video auto-switch left enabled due to error\n");
4434 return -EIO;
4436 break;
4437 case TPACPI_VIDEO_NEW:
4438 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4439 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4440 break;
4441 default:
4442 return -ENOSYS;
4445 return (res)? 0 : -EIO;
4448 static int video_autosw_get(void)
4450 int autosw = 0;
4452 switch (video_supported) {
4453 case TPACPI_VIDEO_570:
4454 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4455 return -EIO;
4456 break;
4457 case TPACPI_VIDEO_770:
4458 case TPACPI_VIDEO_NEW:
4459 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4460 return -EIO;
4461 break;
4462 default:
4463 return -ENOSYS;
4466 return autosw & 1;
4469 static int video_autosw_set(int enable)
4471 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4472 return -EIO;
4473 return 0;
4476 static int video_outputsw_cycle(void)
4478 int autosw = video_autosw_get();
4479 int res;
4481 if (autosw < 0)
4482 return autosw;
4484 switch (video_supported) {
4485 case TPACPI_VIDEO_570:
4486 res = video_autosw_set(1);
4487 if (res)
4488 return res;
4489 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4490 break;
4491 case TPACPI_VIDEO_770:
4492 case TPACPI_VIDEO_NEW:
4493 res = video_autosw_set(1);
4494 if (res)
4495 return res;
4496 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4497 break;
4498 default:
4499 return -ENOSYS;
4501 if (!autosw && video_autosw_set(autosw)) {
4502 printk(TPACPI_ERR
4503 "video auto-switch left enabled due to error\n");
4504 return -EIO;
4507 return (res)? 0 : -EIO;
4510 static int video_expand_toggle(void)
4512 switch (video_supported) {
4513 case TPACPI_VIDEO_570:
4514 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4515 0 : -EIO;
4516 case TPACPI_VIDEO_770:
4517 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4518 0 : -EIO;
4519 case TPACPI_VIDEO_NEW:
4520 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4521 0 : -EIO;
4522 default:
4523 return -ENOSYS;
4525 /* not reached */
4528 static int video_read(char *p)
4530 int status, autosw;
4531 int len = 0;
4533 if (video_supported == TPACPI_VIDEO_NONE) {
4534 len += sprintf(p + len, "status:\t\tnot supported\n");
4535 return len;
4538 status = video_outputsw_get();
4539 if (status < 0)
4540 return status;
4542 autosw = video_autosw_get();
4543 if (autosw < 0)
4544 return autosw;
4546 len += sprintf(p + len, "status:\t\tsupported\n");
4547 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4548 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4549 if (video_supported == TPACPI_VIDEO_NEW)
4550 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4551 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4552 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4553 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4554 if (video_supported == TPACPI_VIDEO_NEW)
4555 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4556 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4557 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4559 return len;
4562 static int video_write(char *buf)
4564 char *cmd;
4565 int enable, disable, status;
4566 int res;
4568 if (video_supported == TPACPI_VIDEO_NONE)
4569 return -ENODEV;
4571 enable = 0;
4572 disable = 0;
4574 while ((cmd = next_cmd(&buf))) {
4575 if (strlencmp(cmd, "lcd_enable") == 0) {
4576 enable |= TP_ACPI_VIDEO_S_LCD;
4577 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4578 disable |= TP_ACPI_VIDEO_S_LCD;
4579 } else if (strlencmp(cmd, "crt_enable") == 0) {
4580 enable |= TP_ACPI_VIDEO_S_CRT;
4581 } else if (strlencmp(cmd, "crt_disable") == 0) {
4582 disable |= TP_ACPI_VIDEO_S_CRT;
4583 } else if (video_supported == TPACPI_VIDEO_NEW &&
4584 strlencmp(cmd, "dvi_enable") == 0) {
4585 enable |= TP_ACPI_VIDEO_S_DVI;
4586 } else if (video_supported == TPACPI_VIDEO_NEW &&
4587 strlencmp(cmd, "dvi_disable") == 0) {
4588 disable |= TP_ACPI_VIDEO_S_DVI;
4589 } else if (strlencmp(cmd, "auto_enable") == 0) {
4590 res = video_autosw_set(1);
4591 if (res)
4592 return res;
4593 } else if (strlencmp(cmd, "auto_disable") == 0) {
4594 res = video_autosw_set(0);
4595 if (res)
4596 return res;
4597 } else if (strlencmp(cmd, "video_switch") == 0) {
4598 res = video_outputsw_cycle();
4599 if (res)
4600 return res;
4601 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4602 res = video_expand_toggle();
4603 if (res)
4604 return res;
4605 } else
4606 return -EINVAL;
4609 if (enable || disable) {
4610 status = video_outputsw_get();
4611 if (status < 0)
4612 return status;
4613 res = video_outputsw_set((status & ~disable) | enable);
4614 if (res)
4615 return res;
4618 return 0;
4621 static struct ibm_struct video_driver_data = {
4622 .name = "video",
4623 .read = video_read,
4624 .write = video_write,
4625 .exit = video_exit,
4628 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4630 /*************************************************************************
4631 * Light (thinklight) subdriver
4634 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4635 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4637 static int light_get_status(void)
4639 int status = 0;
4641 if (tp_features.light_status) {
4642 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4643 return -EIO;
4644 return (!!status);
4647 return -ENXIO;
4650 static int light_set_status(int status)
4652 int rc;
4654 if (tp_features.light) {
4655 if (cmos_handle) {
4656 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4657 (status)?
4658 TP_CMOS_THINKLIGHT_ON :
4659 TP_CMOS_THINKLIGHT_OFF);
4660 } else {
4661 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4662 (status)? 1 : 0);
4664 return (rc)? 0 : -EIO;
4667 return -ENXIO;
4670 static void light_set_status_worker(struct work_struct *work)
4672 struct tpacpi_led_classdev *data =
4673 container_of(work, struct tpacpi_led_classdev, work);
4675 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4676 light_set_status((data->new_state != TPACPI_LED_OFF));
4679 static void light_sysfs_set(struct led_classdev *led_cdev,
4680 enum led_brightness brightness)
4682 struct tpacpi_led_classdev *data =
4683 container_of(led_cdev,
4684 struct tpacpi_led_classdev,
4685 led_classdev);
4686 data->new_state = (brightness != LED_OFF) ?
4687 TPACPI_LED_ON : TPACPI_LED_OFF;
4688 queue_work(tpacpi_wq, &data->work);
4691 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4693 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4696 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4697 .led_classdev = {
4698 .name = "tpacpi::thinklight",
4699 .brightness_set = &light_sysfs_set,
4700 .brightness_get = &light_sysfs_get,
4704 static int __init light_init(struct ibm_init_struct *iibm)
4706 int rc;
4708 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4710 TPACPI_ACPIHANDLE_INIT(ledb);
4711 TPACPI_ACPIHANDLE_INIT(lght);
4712 TPACPI_ACPIHANDLE_INIT(cmos);
4713 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4715 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4716 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4718 if (tp_features.light)
4719 /* light status not supported on
4720 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4721 tp_features.light_status =
4722 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4724 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4725 str_supported(tp_features.light),
4726 str_supported(tp_features.light_status));
4728 if (!tp_features.light)
4729 return 1;
4731 rc = led_classdev_register(&tpacpi_pdev->dev,
4732 &tpacpi_led_thinklight.led_classdev);
4734 if (rc < 0) {
4735 tp_features.light = 0;
4736 tp_features.light_status = 0;
4737 } else {
4738 rc = 0;
4741 return rc;
4744 static void light_exit(void)
4746 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4747 if (work_pending(&tpacpi_led_thinklight.work))
4748 flush_workqueue(tpacpi_wq);
4751 static int light_read(char *p)
4753 int len = 0;
4754 int status;
4756 if (!tp_features.light) {
4757 len += sprintf(p + len, "status:\t\tnot supported\n");
4758 } else if (!tp_features.light_status) {
4759 len += sprintf(p + len, "status:\t\tunknown\n");
4760 len += sprintf(p + len, "commands:\ton, off\n");
4761 } else {
4762 status = light_get_status();
4763 if (status < 0)
4764 return status;
4765 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4766 len += sprintf(p + len, "commands:\ton, off\n");
4769 return len;
4772 static int light_write(char *buf)
4774 char *cmd;
4775 int newstatus = 0;
4777 if (!tp_features.light)
4778 return -ENODEV;
4780 while ((cmd = next_cmd(&buf))) {
4781 if (strlencmp(cmd, "on") == 0) {
4782 newstatus = 1;
4783 } else if (strlencmp(cmd, "off") == 0) {
4784 newstatus = 0;
4785 } else
4786 return -EINVAL;
4789 return light_set_status(newstatus);
4792 static struct ibm_struct light_driver_data = {
4793 .name = "light",
4794 .read = light_read,
4795 .write = light_write,
4796 .exit = light_exit,
4799 /*************************************************************************
4800 * CMOS subdriver
4803 /* sysfs cmos_command -------------------------------------------------- */
4804 static ssize_t cmos_command_store(struct device *dev,
4805 struct device_attribute *attr,
4806 const char *buf, size_t count)
4808 unsigned long cmos_cmd;
4809 int res;
4811 if (parse_strtoul(buf, 21, &cmos_cmd))
4812 return -EINVAL;
4814 res = issue_thinkpad_cmos_command(cmos_cmd);
4815 return (res)? res : count;
4818 static struct device_attribute dev_attr_cmos_command =
4819 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4821 /* --------------------------------------------------------------------- */
4823 static int __init cmos_init(struct ibm_init_struct *iibm)
4825 int res;
4827 vdbg_printk(TPACPI_DBG_INIT,
4828 "initializing cmos commands subdriver\n");
4830 TPACPI_ACPIHANDLE_INIT(cmos);
4832 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4833 str_supported(cmos_handle != NULL));
4835 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4836 if (res)
4837 return res;
4839 return (cmos_handle)? 0 : 1;
4842 static void cmos_exit(void)
4844 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4847 static int cmos_read(char *p)
4849 int len = 0;
4851 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4852 R30, R31, T20-22, X20-21 */
4853 if (!cmos_handle)
4854 len += sprintf(p + len, "status:\t\tnot supported\n");
4855 else {
4856 len += sprintf(p + len, "status:\t\tsupported\n");
4857 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4860 return len;
4863 static int cmos_write(char *buf)
4865 char *cmd;
4866 int cmos_cmd, res;
4868 while ((cmd = next_cmd(&buf))) {
4869 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4870 cmos_cmd >= 0 && cmos_cmd <= 21) {
4871 /* cmos_cmd set */
4872 } else
4873 return -EINVAL;
4875 res = issue_thinkpad_cmos_command(cmos_cmd);
4876 if (res)
4877 return res;
4880 return 0;
4883 static struct ibm_struct cmos_driver_data = {
4884 .name = "cmos",
4885 .read = cmos_read,
4886 .write = cmos_write,
4887 .exit = cmos_exit,
4890 /*************************************************************************
4891 * LED subdriver
4894 enum led_access_mode {
4895 TPACPI_LED_NONE = 0,
4896 TPACPI_LED_570, /* 570 */
4897 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4898 TPACPI_LED_NEW, /* all others */
4901 enum { /* For TPACPI_LED_OLD */
4902 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4903 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4904 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4907 static enum led_access_mode led_supported;
4909 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4910 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4911 /* T20-22, X20-21 */
4912 "LED", /* all others */
4913 ); /* R30, R31 */
4915 #define TPACPI_LED_NUMLEDS 16
4916 static struct tpacpi_led_classdev *tpacpi_leds;
4917 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4918 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4919 /* there's a limit of 19 chars + NULL before 2.6.26 */
4920 "tpacpi::power",
4921 "tpacpi:orange:batt",
4922 "tpacpi:green:batt",
4923 "tpacpi::dock_active",
4924 "tpacpi::bay_active",
4925 "tpacpi::dock_batt",
4926 "tpacpi::unknown_led",
4927 "tpacpi::standby",
4928 "tpacpi::dock_status1",
4929 "tpacpi::dock_status2",
4930 "tpacpi::unknown_led2",
4931 "tpacpi::unknown_led3",
4932 "tpacpi::thinkvantage",
4934 #define TPACPI_SAFE_LEDS 0x1081U
4936 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4938 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4939 return false;
4940 #else
4941 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4942 #endif
4945 static int led_get_status(const unsigned int led)
4947 int status;
4948 enum led_status_t led_s;
4950 switch (led_supported) {
4951 case TPACPI_LED_570:
4952 if (!acpi_evalf(ec_handle,
4953 &status, "GLED", "dd", 1 << led))
4954 return -EIO;
4955 led_s = (status == 0)?
4956 TPACPI_LED_OFF :
4957 ((status == 1)?
4958 TPACPI_LED_ON :
4959 TPACPI_LED_BLINK);
4960 tpacpi_led_state_cache[led] = led_s;
4961 return led_s;
4962 default:
4963 return -ENXIO;
4966 /* not reached */
4969 static int led_set_status(const unsigned int led,
4970 const enum led_status_t ledstatus)
4972 /* off, on, blink. Index is led_status_t */
4973 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4974 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4976 int rc = 0;
4978 switch (led_supported) {
4979 case TPACPI_LED_570:
4980 /* 570 */
4981 if (unlikely(led > 7))
4982 return -EINVAL;
4983 if (unlikely(tpacpi_is_led_restricted(led)))
4984 return -EPERM;
4985 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4986 (1 << led), led_sled_arg1[ledstatus]))
4987 rc = -EIO;
4988 break;
4989 case TPACPI_LED_OLD:
4990 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4991 if (unlikely(led > 7))
4992 return -EINVAL;
4993 if (unlikely(tpacpi_is_led_restricted(led)))
4994 return -EPERM;
4995 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4996 if (rc >= 0)
4997 rc = ec_write(TPACPI_LED_EC_HLBL,
4998 (ledstatus == TPACPI_LED_BLINK) << led);
4999 if (rc >= 0)
5000 rc = ec_write(TPACPI_LED_EC_HLCL,
5001 (ledstatus != TPACPI_LED_OFF) << led);
5002 break;
5003 case TPACPI_LED_NEW:
5004 /* all others */
5005 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5006 return -EINVAL;
5007 if (unlikely(tpacpi_is_led_restricted(led)))
5008 return -EPERM;
5009 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5010 led, led_led_arg1[ledstatus]))
5011 rc = -EIO;
5012 break;
5013 default:
5014 rc = -ENXIO;
5017 if (!rc)
5018 tpacpi_led_state_cache[led] = ledstatus;
5020 return rc;
5023 static void led_set_status_worker(struct work_struct *work)
5025 struct tpacpi_led_classdev *data =
5026 container_of(work, struct tpacpi_led_classdev, work);
5028 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5029 led_set_status(data->led, data->new_state);
5032 static void led_sysfs_set(struct led_classdev *led_cdev,
5033 enum led_brightness brightness)
5035 struct tpacpi_led_classdev *data = container_of(led_cdev,
5036 struct tpacpi_led_classdev, led_classdev);
5038 if (brightness == LED_OFF)
5039 data->new_state = TPACPI_LED_OFF;
5040 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5041 data->new_state = TPACPI_LED_ON;
5042 else
5043 data->new_state = TPACPI_LED_BLINK;
5045 queue_work(tpacpi_wq, &data->work);
5048 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5049 unsigned long *delay_on, unsigned long *delay_off)
5051 struct tpacpi_led_classdev *data = container_of(led_cdev,
5052 struct tpacpi_led_classdev, led_classdev);
5054 /* Can we choose the flash rate? */
5055 if (*delay_on == 0 && *delay_off == 0) {
5056 /* yes. set them to the hardware blink rate (1 Hz) */
5057 *delay_on = 500; /* ms */
5058 *delay_off = 500; /* ms */
5059 } else if ((*delay_on != 500) || (*delay_off != 500))
5060 return -EINVAL;
5062 data->new_state = TPACPI_LED_BLINK;
5063 queue_work(tpacpi_wq, &data->work);
5065 return 0;
5068 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5070 int rc;
5072 struct tpacpi_led_classdev *data = container_of(led_cdev,
5073 struct tpacpi_led_classdev, led_classdev);
5075 rc = led_get_status(data->led);
5077 if (rc == TPACPI_LED_OFF || rc < 0)
5078 rc = LED_OFF; /* no error handling in led class :( */
5079 else
5080 rc = LED_FULL;
5082 return rc;
5085 static void led_exit(void)
5087 unsigned int i;
5089 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5090 if (tpacpi_leds[i].led_classdev.name)
5091 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5094 kfree(tpacpi_leds);
5097 static int __init tpacpi_init_led(unsigned int led)
5099 int rc;
5101 tpacpi_leds[led].led = led;
5103 /* LEDs with no name don't get registered */
5104 if (!tpacpi_led_names[led])
5105 return 0;
5107 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5108 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5109 if (led_supported == TPACPI_LED_570)
5110 tpacpi_leds[led].led_classdev.brightness_get =
5111 &led_sysfs_get;
5113 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5115 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5117 rc = led_classdev_register(&tpacpi_pdev->dev,
5118 &tpacpi_leds[led].led_classdev);
5119 if (rc < 0)
5120 tpacpi_leds[led].led_classdev.name = NULL;
5122 return rc;
5125 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5126 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5127 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5128 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5130 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5131 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5132 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5133 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5134 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5135 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5136 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5137 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5139 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5140 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5141 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5142 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5143 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5145 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5146 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5147 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5148 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5150 /* (1) - may have excess leds enabled on MSB */
5152 /* Defaults (order matters, keep last, don't reorder!) */
5153 { /* Lenovo */
5154 .vendor = PCI_VENDOR_ID_LENOVO,
5155 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5156 .quirks = 0x1fffU,
5158 { /* IBM ThinkPads with no EC version string */
5159 .vendor = PCI_VENDOR_ID_IBM,
5160 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5161 .quirks = 0x00ffU,
5163 { /* IBM ThinkPads with EC version string */
5164 .vendor = PCI_VENDOR_ID_IBM,
5165 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5166 .quirks = 0x00bfU,
5170 #undef TPACPI_LEDQ_IBM
5171 #undef TPACPI_LEDQ_LNV
5173 static int __init led_init(struct ibm_init_struct *iibm)
5175 unsigned int i;
5176 int rc;
5177 unsigned long useful_leds;
5179 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5181 TPACPI_ACPIHANDLE_INIT(led);
5183 if (!led_handle)
5184 /* led not supported on R30, R31 */
5185 led_supported = TPACPI_LED_NONE;
5186 else if (strlencmp(led_path, "SLED") == 0)
5187 /* 570 */
5188 led_supported = TPACPI_LED_570;
5189 else if (strlencmp(led_path, "SYSL") == 0)
5190 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5191 led_supported = TPACPI_LED_OLD;
5192 else
5193 /* all others */
5194 led_supported = TPACPI_LED_NEW;
5196 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5197 str_supported(led_supported), led_supported);
5199 if (led_supported == TPACPI_LED_NONE)
5200 return 1;
5202 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5203 GFP_KERNEL);
5204 if (!tpacpi_leds) {
5205 printk(TPACPI_ERR "Out of memory for LED data\n");
5206 return -ENOMEM;
5209 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5210 ARRAY_SIZE(led_useful_qtable));
5212 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5213 if (!tpacpi_is_led_restricted(i) &&
5214 test_bit(i, &useful_leds)) {
5215 rc = tpacpi_init_led(i);
5216 if (rc < 0) {
5217 led_exit();
5218 return rc;
5223 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5224 printk(TPACPI_NOTICE
5225 "warning: userspace override of important "
5226 "firmware LEDs is enabled\n");
5227 #endif
5228 return 0;
5231 #define str_led_status(s) \
5232 ((s) == TPACPI_LED_OFF ? "off" : \
5233 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5235 static int led_read(char *p)
5237 int len = 0;
5239 if (!led_supported) {
5240 len += sprintf(p + len, "status:\t\tnot supported\n");
5241 return len;
5243 len += sprintf(p + len, "status:\t\tsupported\n");
5245 if (led_supported == TPACPI_LED_570) {
5246 /* 570 */
5247 int i, status;
5248 for (i = 0; i < 8; i++) {
5249 status = led_get_status(i);
5250 if (status < 0)
5251 return -EIO;
5252 len += sprintf(p + len, "%d:\t\t%s\n",
5253 i, str_led_status(status));
5257 len += sprintf(p + len, "commands:\t"
5258 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5260 return len;
5263 static int led_write(char *buf)
5265 char *cmd;
5266 int led, rc;
5267 enum led_status_t s;
5269 if (!led_supported)
5270 return -ENODEV;
5272 while ((cmd = next_cmd(&buf))) {
5273 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5274 return -EINVAL;
5276 if (strstr(cmd, "off")) {
5277 s = TPACPI_LED_OFF;
5278 } else if (strstr(cmd, "on")) {
5279 s = TPACPI_LED_ON;
5280 } else if (strstr(cmd, "blink")) {
5281 s = TPACPI_LED_BLINK;
5282 } else {
5283 return -EINVAL;
5286 rc = led_set_status(led, s);
5287 if (rc < 0)
5288 return rc;
5291 return 0;
5294 static struct ibm_struct led_driver_data = {
5295 .name = "led",
5296 .read = led_read,
5297 .write = led_write,
5298 .exit = led_exit,
5301 /*************************************************************************
5302 * Beep subdriver
5305 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5307 #define TPACPI_BEEP_Q1 0x0001
5309 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5310 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5311 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5314 static int __init beep_init(struct ibm_init_struct *iibm)
5316 unsigned long quirks;
5318 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5320 TPACPI_ACPIHANDLE_INIT(beep);
5322 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5323 str_supported(beep_handle != NULL));
5325 quirks = tpacpi_check_quirks(beep_quirk_table,
5326 ARRAY_SIZE(beep_quirk_table));
5328 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5330 return (beep_handle)? 0 : 1;
5333 static int beep_read(char *p)
5335 int len = 0;
5337 if (!beep_handle)
5338 len += sprintf(p + len, "status:\t\tnot supported\n");
5339 else {
5340 len += sprintf(p + len, "status:\t\tsupported\n");
5341 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5344 return len;
5347 static int beep_write(char *buf)
5349 char *cmd;
5350 int beep_cmd;
5352 if (!beep_handle)
5353 return -ENODEV;
5355 while ((cmd = next_cmd(&buf))) {
5356 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5357 beep_cmd >= 0 && beep_cmd <= 17) {
5358 /* beep_cmd set */
5359 } else
5360 return -EINVAL;
5361 if (tp_features.beep_needs_two_args) {
5362 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5363 beep_cmd, 0))
5364 return -EIO;
5365 } else {
5366 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5367 beep_cmd))
5368 return -EIO;
5372 return 0;
5375 static struct ibm_struct beep_driver_data = {
5376 .name = "beep",
5377 .read = beep_read,
5378 .write = beep_write,
5381 /*************************************************************************
5382 * Thermal subdriver
5385 enum thermal_access_mode {
5386 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5387 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5388 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5389 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5390 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5393 enum { /* TPACPI_THERMAL_TPEC_* */
5394 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5395 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5396 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5399 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5400 struct ibm_thermal_sensors_struct {
5401 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5404 static enum thermal_access_mode thermal_read_mode;
5406 /* idx is zero-based */
5407 static int thermal_get_sensor(int idx, s32 *value)
5409 int t;
5410 s8 tmp;
5411 char tmpi[5];
5413 t = TP_EC_THERMAL_TMP0;
5415 switch (thermal_read_mode) {
5416 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5417 case TPACPI_THERMAL_TPEC_16:
5418 if (idx >= 8 && idx <= 15) {
5419 t = TP_EC_THERMAL_TMP8;
5420 idx -= 8;
5422 /* fallthrough */
5423 #endif
5424 case TPACPI_THERMAL_TPEC_8:
5425 if (idx <= 7) {
5426 if (!acpi_ec_read(t + idx, &tmp))
5427 return -EIO;
5428 *value = tmp * 1000;
5429 return 0;
5431 break;
5433 case TPACPI_THERMAL_ACPI_UPDT:
5434 if (idx <= 7) {
5435 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5436 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5437 return -EIO;
5438 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5439 return -EIO;
5440 *value = (t - 2732) * 100;
5441 return 0;
5443 break;
5445 case TPACPI_THERMAL_ACPI_TMP07:
5446 if (idx <= 7) {
5447 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5448 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5449 return -EIO;
5450 if (t > 127 || t < -127)
5451 t = TP_EC_THERMAL_TMP_NA;
5452 *value = t * 1000;
5453 return 0;
5455 break;
5457 case TPACPI_THERMAL_NONE:
5458 default:
5459 return -ENOSYS;
5462 return -EINVAL;
5465 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5467 int res, i;
5468 int n;
5470 n = 8;
5471 i = 0;
5473 if (!s)
5474 return -EINVAL;
5476 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5477 n = 16;
5479 for (i = 0 ; i < n; i++) {
5480 res = thermal_get_sensor(i, &s->temp[i]);
5481 if (res)
5482 return res;
5485 return n;
5488 /* sysfs temp##_input -------------------------------------------------- */
5490 static ssize_t thermal_temp_input_show(struct device *dev,
5491 struct device_attribute *attr,
5492 char *buf)
5494 struct sensor_device_attribute *sensor_attr =
5495 to_sensor_dev_attr(attr);
5496 int idx = sensor_attr->index;
5497 s32 value;
5498 int res;
5500 res = thermal_get_sensor(idx, &value);
5501 if (res)
5502 return res;
5503 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5504 return -ENXIO;
5506 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5509 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5510 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5511 thermal_temp_input_show, NULL, _idxB)
5513 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5514 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5515 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5516 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5517 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5518 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5519 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5520 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5521 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5522 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5523 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5524 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5525 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5526 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5527 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5528 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5529 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5532 #define THERMAL_ATTRS(X) \
5533 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5535 static struct attribute *thermal_temp_input_attr[] = {
5536 THERMAL_ATTRS(8),
5537 THERMAL_ATTRS(9),
5538 THERMAL_ATTRS(10),
5539 THERMAL_ATTRS(11),
5540 THERMAL_ATTRS(12),
5541 THERMAL_ATTRS(13),
5542 THERMAL_ATTRS(14),
5543 THERMAL_ATTRS(15),
5544 THERMAL_ATTRS(0),
5545 THERMAL_ATTRS(1),
5546 THERMAL_ATTRS(2),
5547 THERMAL_ATTRS(3),
5548 THERMAL_ATTRS(4),
5549 THERMAL_ATTRS(5),
5550 THERMAL_ATTRS(6),
5551 THERMAL_ATTRS(7),
5552 NULL
5555 static const struct attribute_group thermal_temp_input16_group = {
5556 .attrs = thermal_temp_input_attr
5559 static const struct attribute_group thermal_temp_input8_group = {
5560 .attrs = &thermal_temp_input_attr[8]
5563 #undef THERMAL_SENSOR_ATTR_TEMP
5564 #undef THERMAL_ATTRS
5566 /* --------------------------------------------------------------------- */
5568 static int __init thermal_init(struct ibm_init_struct *iibm)
5570 u8 t, ta1, ta2;
5571 int i;
5572 int acpi_tmp7;
5573 int res;
5575 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5577 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5579 if (thinkpad_id.ec_model) {
5581 * Direct EC access mode: sensors at registers
5582 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5583 * non-implemented, thermal sensors return 0x80 when
5584 * not available
5587 ta1 = ta2 = 0;
5588 for (i = 0; i < 8; i++) {
5589 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5590 ta1 |= t;
5591 } else {
5592 ta1 = 0;
5593 break;
5595 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5596 ta2 |= t;
5597 } else {
5598 ta1 = 0;
5599 break;
5602 if (ta1 == 0) {
5603 /* This is sheer paranoia, but we handle it anyway */
5604 if (acpi_tmp7) {
5605 printk(TPACPI_ERR
5606 "ThinkPad ACPI EC access misbehaving, "
5607 "falling back to ACPI TMPx access "
5608 "mode\n");
5609 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5610 } else {
5611 printk(TPACPI_ERR
5612 "ThinkPad ACPI EC access misbehaving, "
5613 "disabling thermal sensors access\n");
5614 thermal_read_mode = TPACPI_THERMAL_NONE;
5616 } else {
5617 thermal_read_mode =
5618 (ta2 != 0) ?
5619 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5621 } else if (acpi_tmp7) {
5622 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5623 /* 600e/x, 770e, 770x */
5624 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5625 } else {
5626 /* Standard ACPI TMPx access, max 8 sensors */
5627 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5629 } else {
5630 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5631 thermal_read_mode = TPACPI_THERMAL_NONE;
5634 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5635 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5636 thermal_read_mode);
5638 switch (thermal_read_mode) {
5639 case TPACPI_THERMAL_TPEC_16:
5640 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5641 &thermal_temp_input16_group);
5642 if (res)
5643 return res;
5644 break;
5645 case TPACPI_THERMAL_TPEC_8:
5646 case TPACPI_THERMAL_ACPI_TMP07:
5647 case TPACPI_THERMAL_ACPI_UPDT:
5648 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5649 &thermal_temp_input8_group);
5650 if (res)
5651 return res;
5652 break;
5653 case TPACPI_THERMAL_NONE:
5654 default:
5655 return 1;
5658 return 0;
5661 static void thermal_exit(void)
5663 switch (thermal_read_mode) {
5664 case TPACPI_THERMAL_TPEC_16:
5665 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5666 &thermal_temp_input16_group);
5667 break;
5668 case TPACPI_THERMAL_TPEC_8:
5669 case TPACPI_THERMAL_ACPI_TMP07:
5670 case TPACPI_THERMAL_ACPI_UPDT:
5671 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5672 &thermal_temp_input16_group);
5673 break;
5674 case TPACPI_THERMAL_NONE:
5675 default:
5676 break;
5680 static int thermal_read(char *p)
5682 int len = 0;
5683 int n, i;
5684 struct ibm_thermal_sensors_struct t;
5686 n = thermal_get_sensors(&t);
5687 if (unlikely(n < 0))
5688 return n;
5690 len += sprintf(p + len, "temperatures:\t");
5692 if (n > 0) {
5693 for (i = 0; i < (n - 1); i++)
5694 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5695 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5696 } else
5697 len += sprintf(p + len, "not supported\n");
5699 return len;
5702 static struct ibm_struct thermal_driver_data = {
5703 .name = "thermal",
5704 .read = thermal_read,
5705 .exit = thermal_exit,
5708 /*************************************************************************
5709 * EC Dump subdriver
5712 static u8 ecdump_regs[256];
5714 static int ecdump_read(char *p)
5716 int len = 0;
5717 int i, j;
5718 u8 v;
5720 len += sprintf(p + len, "EC "
5721 " +00 +01 +02 +03 +04 +05 +06 +07"
5722 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5723 for (i = 0; i < 256; i += 16) {
5724 len += sprintf(p + len, "EC 0x%02x:", i);
5725 for (j = 0; j < 16; j++) {
5726 if (!acpi_ec_read(i + j, &v))
5727 break;
5728 if (v != ecdump_regs[i + j])
5729 len += sprintf(p + len, " *%02x", v);
5730 else
5731 len += sprintf(p + len, " %02x", v);
5732 ecdump_regs[i + j] = v;
5734 len += sprintf(p + len, "\n");
5735 if (j != 16)
5736 break;
5739 /* These are way too dangerous to advertise openly... */
5740 #if 0
5741 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5742 " (<offset> is 00-ff, <value> is 00-ff)\n");
5743 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5744 " (<offset> is 00-ff, <value> is 0-255)\n");
5745 #endif
5746 return len;
5749 static int ecdump_write(char *buf)
5751 char *cmd;
5752 int i, v;
5754 while ((cmd = next_cmd(&buf))) {
5755 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5756 /* i and v set */
5757 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5758 /* i and v set */
5759 } else
5760 return -EINVAL;
5761 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5762 if (!acpi_ec_write(i, v))
5763 return -EIO;
5764 } else
5765 return -EINVAL;
5768 return 0;
5771 static struct ibm_struct ecdump_driver_data = {
5772 .name = "ecdump",
5773 .read = ecdump_read,
5774 .write = ecdump_write,
5775 .flags.experimental = 1,
5778 /*************************************************************************
5779 * Backlight/brightness subdriver
5782 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5785 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5786 * CMOS NVRAM byte 0x5E, bits 0-3.
5788 * EC HBRV (0x31) has the following layout
5789 * Bit 7: unknown function
5790 * Bit 6: unknown function
5791 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5792 * Bit 4: must be set to zero to avoid problems
5793 * Bit 3-0: backlight brightness level
5795 * brightness_get_raw returns status data in the HBRV layout
5797 * WARNING: The X61 has been verified to use HBRV for something else, so
5798 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5799 * testing on the very early *60 Lenovo models...
5802 enum {
5803 TP_EC_BACKLIGHT = 0x31,
5805 /* TP_EC_BACKLIGHT bitmasks */
5806 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5807 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5808 TP_EC_BACKLIGHT_MAPSW = 0x20,
5811 enum tpacpi_brightness_access_mode {
5812 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5813 TPACPI_BRGHT_MODE_EC, /* EC control */
5814 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5815 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5816 TPACPI_BRGHT_MODE_MAX
5819 static struct backlight_device *ibm_backlight_device;
5821 static enum tpacpi_brightness_access_mode brightness_mode =
5822 TPACPI_BRGHT_MODE_MAX;
5824 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5826 static struct mutex brightness_mutex;
5828 /* NVRAM brightness access,
5829 * call with brightness_mutex held! */
5830 static unsigned int tpacpi_brightness_nvram_get(void)
5832 u8 lnvram;
5834 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5835 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5836 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5837 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5839 return lnvram;
5842 static void tpacpi_brightness_checkpoint_nvram(void)
5844 u8 lec = 0;
5845 u8 b_nvram;
5847 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5848 return;
5850 vdbg_printk(TPACPI_DBG_BRGHT,
5851 "trying to checkpoint backlight level to NVRAM...\n");
5853 if (mutex_lock_killable(&brightness_mutex) < 0)
5854 return;
5856 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5857 goto unlock;
5858 lec &= TP_EC_BACKLIGHT_LVLMSK;
5859 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5861 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5862 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5863 /* NVRAM needs update */
5864 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5865 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5866 b_nvram |= lec;
5867 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5868 dbg_printk(TPACPI_DBG_BRGHT,
5869 "updated NVRAM backlight level to %u (0x%02x)\n",
5870 (unsigned int) lec, (unsigned int) b_nvram);
5871 } else
5872 vdbg_printk(TPACPI_DBG_BRGHT,
5873 "NVRAM backlight level already is %u (0x%02x)\n",
5874 (unsigned int) lec, (unsigned int) b_nvram);
5876 unlock:
5877 mutex_unlock(&brightness_mutex);
5881 /* call with brightness_mutex held! */
5882 static int tpacpi_brightness_get_raw(int *status)
5884 u8 lec = 0;
5886 switch (brightness_mode) {
5887 case TPACPI_BRGHT_MODE_UCMS_STEP:
5888 *status = tpacpi_brightness_nvram_get();
5889 return 0;
5890 case TPACPI_BRGHT_MODE_EC:
5891 case TPACPI_BRGHT_MODE_ECNVRAM:
5892 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5893 return -EIO;
5894 *status = lec;
5895 return 0;
5896 default:
5897 return -ENXIO;
5901 /* call with brightness_mutex held! */
5902 /* do NOT call with illegal backlight level value */
5903 static int tpacpi_brightness_set_ec(unsigned int value)
5905 u8 lec = 0;
5907 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5908 return -EIO;
5910 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5911 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5912 (value & TP_EC_BACKLIGHT_LVLMSK))))
5913 return -EIO;
5915 return 0;
5918 /* call with brightness_mutex held! */
5919 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5921 int cmos_cmd, inc;
5922 unsigned int current_value, i;
5924 current_value = tpacpi_brightness_nvram_get();
5926 if (value == current_value)
5927 return 0;
5929 cmos_cmd = (value > current_value) ?
5930 TP_CMOS_BRIGHTNESS_UP :
5931 TP_CMOS_BRIGHTNESS_DOWN;
5932 inc = (value > current_value) ? 1 : -1;
5934 for (i = current_value; i != value; i += inc)
5935 if (issue_thinkpad_cmos_command(cmos_cmd))
5936 return -EIO;
5938 return 0;
5941 /* May return EINTR which can always be mapped to ERESTARTSYS */
5942 static int brightness_set(unsigned int value)
5944 int res;
5946 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5947 value < 0)
5948 return -EINVAL;
5950 vdbg_printk(TPACPI_DBG_BRGHT,
5951 "set backlight level to %d\n", value);
5953 res = mutex_lock_killable(&brightness_mutex);
5954 if (res < 0)
5955 return res;
5957 switch (brightness_mode) {
5958 case TPACPI_BRGHT_MODE_EC:
5959 case TPACPI_BRGHT_MODE_ECNVRAM:
5960 res = tpacpi_brightness_set_ec(value);
5961 break;
5962 case TPACPI_BRGHT_MODE_UCMS_STEP:
5963 res = tpacpi_brightness_set_ucmsstep(value);
5964 break;
5965 default:
5966 res = -ENXIO;
5969 mutex_unlock(&brightness_mutex);
5970 return res;
5973 /* sysfs backlight class ----------------------------------------------- */
5975 static int brightness_update_status(struct backlight_device *bd)
5977 unsigned int level =
5978 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5979 bd->props.power == FB_BLANK_UNBLANK) ?
5980 bd->props.brightness : 0;
5982 dbg_printk(TPACPI_DBG_BRGHT,
5983 "backlight: attempt to set level to %d\n",
5984 level);
5986 /* it is the backlight class's job (caller) to handle
5987 * EINTR and other errors properly */
5988 return brightness_set(level);
5991 static int brightness_get(struct backlight_device *bd)
5993 int status, res;
5995 res = mutex_lock_killable(&brightness_mutex);
5996 if (res < 0)
5997 return 0;
5999 res = tpacpi_brightness_get_raw(&status);
6001 mutex_unlock(&brightness_mutex);
6003 if (res < 0)
6004 return 0;
6006 return status & TP_EC_BACKLIGHT_LVLMSK;
6009 static struct backlight_ops ibm_backlight_data = {
6010 .get_brightness = brightness_get,
6011 .update_status = brightness_update_status,
6014 /* --------------------------------------------------------------------- */
6017 * These are only useful for models that have only one possibility
6018 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6019 * these quirks.
6021 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6022 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6023 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6025 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6026 /* Models with ATI GPUs known to require ECNVRAM mode */
6027 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6029 /* Models with ATI GPUs that can use ECNVRAM */
6030 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6031 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6032 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6033 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6035 /* Models with Intel Extreme Graphics 2 */
6036 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6037 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6038 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6040 /* Models with Intel GMA900 */
6041 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6042 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6043 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6046 static int __init brightness_init(struct ibm_init_struct *iibm)
6048 int b;
6049 unsigned long quirks;
6051 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6053 mutex_init(&brightness_mutex);
6055 quirks = tpacpi_check_quirks(brightness_quirk_table,
6056 ARRAY_SIZE(brightness_quirk_table));
6059 * We always attempt to detect acpi support, so as to switch
6060 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6061 * going to publish a backlight interface
6063 b = tpacpi_check_std_acpi_brightness_support();
6064 if (b > 0) {
6066 if (acpi_video_backlight_support()) {
6067 if (brightness_enable > 1) {
6068 printk(TPACPI_NOTICE
6069 "Standard ACPI backlight interface "
6070 "available, not loading native one.\n");
6071 return 1;
6072 } else if (brightness_enable == 1) {
6073 printk(TPACPI_NOTICE
6074 "Backlight control force enabled, even if standard "
6075 "ACPI backlight interface is available\n");
6077 } else {
6078 if (brightness_enable > 1) {
6079 printk(TPACPI_NOTICE
6080 "Standard ACPI backlight interface not "
6081 "available, thinkpad_acpi native "
6082 "brightness control enabled\n");
6087 if (!brightness_enable) {
6088 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6089 "brightness support disabled by "
6090 "module parameter\n");
6091 return 1;
6094 if (b > 16) {
6095 printk(TPACPI_ERR
6096 "Unsupported brightness interface, "
6097 "please contact %s\n", TPACPI_MAIL);
6098 return 1;
6100 if (b == 16)
6101 tp_features.bright_16levels = 1;
6104 * Check for module parameter bogosity, note that we
6105 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6106 * able to detect "unspecified"
6108 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6109 return -EINVAL;
6111 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6112 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6113 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6114 if (quirks & TPACPI_BRGHT_Q_EC)
6115 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6116 else
6117 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6119 dbg_printk(TPACPI_DBG_BRGHT,
6120 "driver auto-selected brightness_mode=%d\n",
6121 brightness_mode);
6124 /* Safety */
6125 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6126 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6127 brightness_mode == TPACPI_BRGHT_MODE_EC))
6128 return -EINVAL;
6130 if (tpacpi_brightness_get_raw(&b) < 0)
6131 return 1;
6133 if (tp_features.bright_16levels)
6134 printk(TPACPI_INFO
6135 "detected a 16-level brightness capable ThinkPad\n");
6137 ibm_backlight_device = backlight_device_register(
6138 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6139 &ibm_backlight_data);
6140 if (IS_ERR(ibm_backlight_device)) {
6141 int rc = PTR_ERR(ibm_backlight_device);
6142 ibm_backlight_device = NULL;
6143 printk(TPACPI_ERR "Could not register backlight device\n");
6144 return rc;
6146 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6147 "brightness is supported\n");
6149 if (quirks & TPACPI_BRGHT_Q_ASK) {
6150 printk(TPACPI_NOTICE
6151 "brightness: will use unverified default: "
6152 "brightness_mode=%d\n", brightness_mode);
6153 printk(TPACPI_NOTICE
6154 "brightness: please report to %s whether it works well "
6155 "or not on your ThinkPad\n", TPACPI_MAIL);
6158 ibm_backlight_device->props.max_brightness =
6159 (tp_features.bright_16levels)? 15 : 7;
6160 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6161 backlight_update_status(ibm_backlight_device);
6163 return 0;
6166 static void brightness_suspend(pm_message_t state)
6168 tpacpi_brightness_checkpoint_nvram();
6171 static void brightness_shutdown(void)
6173 tpacpi_brightness_checkpoint_nvram();
6176 static void brightness_exit(void)
6178 if (ibm_backlight_device) {
6179 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6180 "calling backlight_device_unregister()\n");
6181 backlight_device_unregister(ibm_backlight_device);
6184 tpacpi_brightness_checkpoint_nvram();
6187 static int brightness_read(char *p)
6189 int len = 0;
6190 int level;
6192 level = brightness_get(NULL);
6193 if (level < 0) {
6194 len += sprintf(p + len, "level:\t\tunreadable\n");
6195 } else {
6196 len += sprintf(p + len, "level:\t\t%d\n", level);
6197 len += sprintf(p + len, "commands:\tup, down\n");
6198 len += sprintf(p + len, "commands:\tlevel <level>"
6199 " (<level> is 0-%d)\n",
6200 (tp_features.bright_16levels) ? 15 : 7);
6203 return len;
6206 static int brightness_write(char *buf)
6208 int level;
6209 int rc;
6210 char *cmd;
6211 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6213 level = brightness_get(NULL);
6214 if (level < 0)
6215 return level;
6217 while ((cmd = next_cmd(&buf))) {
6218 if (strlencmp(cmd, "up") == 0) {
6219 if (level < max_level)
6220 level++;
6221 } else if (strlencmp(cmd, "down") == 0) {
6222 if (level > 0)
6223 level--;
6224 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6225 level >= 0 && level <= max_level) {
6226 /* new level set */
6227 } else
6228 return -EINVAL;
6231 tpacpi_disclose_usertask("procfs brightness",
6232 "set level to %d\n", level);
6235 * Now we know what the final level should be, so we try to set it.
6236 * Doing it this way makes the syscall restartable in case of EINTR
6238 rc = brightness_set(level);
6239 return (rc == -EINTR)? ERESTARTSYS : rc;
6242 static struct ibm_struct brightness_driver_data = {
6243 .name = "brightness",
6244 .read = brightness_read,
6245 .write = brightness_write,
6246 .exit = brightness_exit,
6247 .suspend = brightness_suspend,
6248 .shutdown = brightness_shutdown,
6251 /*************************************************************************
6252 * Volume subdriver
6255 static int volume_offset = 0x30;
6257 static int volume_read(char *p)
6259 int len = 0;
6260 u8 level;
6262 if (!acpi_ec_read(volume_offset, &level)) {
6263 len += sprintf(p + len, "level:\t\tunreadable\n");
6264 } else {
6265 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6266 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6267 len += sprintf(p + len, "commands:\tup, down, mute\n");
6268 len += sprintf(p + len, "commands:\tlevel <level>"
6269 " (<level> is 0-15)\n");
6272 return len;
6275 static int volume_write(char *buf)
6277 int cmos_cmd, inc, i;
6278 u8 level, mute;
6279 int new_level, new_mute;
6280 char *cmd;
6282 while ((cmd = next_cmd(&buf))) {
6283 if (!acpi_ec_read(volume_offset, &level))
6284 return -EIO;
6285 new_mute = mute = level & 0x40;
6286 new_level = level = level & 0xf;
6288 if (strlencmp(cmd, "up") == 0) {
6289 if (mute)
6290 new_mute = 0;
6291 else
6292 new_level = level == 15 ? 15 : level + 1;
6293 } else if (strlencmp(cmd, "down") == 0) {
6294 if (mute)
6295 new_mute = 0;
6296 else
6297 new_level = level == 0 ? 0 : level - 1;
6298 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6299 new_level >= 0 && new_level <= 15) {
6300 /* new_level set */
6301 } else if (strlencmp(cmd, "mute") == 0) {
6302 new_mute = 0x40;
6303 } else
6304 return -EINVAL;
6306 if (new_level != level) {
6307 /* mute doesn't change */
6309 cmos_cmd = (new_level > level) ?
6310 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6311 inc = new_level > level ? 1 : -1;
6313 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6314 !acpi_ec_write(volume_offset, level)))
6315 return -EIO;
6317 for (i = level; i != new_level; i += inc)
6318 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6319 !acpi_ec_write(volume_offset, i + inc))
6320 return -EIO;
6322 if (mute &&
6323 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6324 !acpi_ec_write(volume_offset, new_level + mute))) {
6325 return -EIO;
6329 if (new_mute != mute) {
6330 /* level doesn't change */
6332 cmos_cmd = (new_mute) ?
6333 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6335 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6336 !acpi_ec_write(volume_offset, level + new_mute))
6337 return -EIO;
6341 return 0;
6344 static struct ibm_struct volume_driver_data = {
6345 .name = "volume",
6346 .read = volume_read,
6347 .write = volume_write,
6350 /*************************************************************************
6351 * Fan subdriver
6355 * FAN ACCESS MODES
6357 * TPACPI_FAN_RD_ACPI_GFAN:
6358 * ACPI GFAN method: returns fan level
6360 * see TPACPI_FAN_WR_ACPI_SFAN
6361 * EC 0x2f (HFSP) not available if GFAN exists
6363 * TPACPI_FAN_WR_ACPI_SFAN:
6364 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6366 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6367 * it for writing.
6369 * TPACPI_FAN_WR_TPEC:
6370 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6371 * Supported on almost all ThinkPads
6373 * Fan speed changes of any sort (including those caused by the
6374 * disengaged mode) are usually done slowly by the firmware as the
6375 * maximum ammount of fan duty cycle change per second seems to be
6376 * limited.
6378 * Reading is not available if GFAN exists.
6379 * Writing is not available if SFAN exists.
6381 * Bits
6382 * 7 automatic mode engaged;
6383 * (default operation mode of the ThinkPad)
6384 * fan level is ignored in this mode.
6385 * 6 full speed mode (takes precedence over bit 7);
6386 * not available on all thinkpads. May disable
6387 * the tachometer while the fan controller ramps up
6388 * the speed (which can take up to a few *minutes*).
6389 * Speeds up fan to 100% duty-cycle, which is far above
6390 * the standard RPM levels. It is not impossible that
6391 * it could cause hardware damage.
6392 * 5-3 unused in some models. Extra bits for fan level
6393 * in others, but still useless as all values above
6394 * 7 map to the same speed as level 7 in these models.
6395 * 2-0 fan level (0..7 usually)
6396 * 0x00 = stop
6397 * 0x07 = max (set when temperatures critical)
6398 * Some ThinkPads may have other levels, see
6399 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6401 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6402 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6403 * does so, its initial value is meaningless (0x07).
6405 * For firmware bugs, refer to:
6406 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6408 * ----
6410 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6411 * Main fan tachometer reading (in RPM)
6413 * This register is present on all ThinkPads with a new-style EC, and
6414 * it is known not to be present on the A21m/e, and T22, as there is
6415 * something else in offset 0x84 according to the ACPI DSDT. Other
6416 * ThinkPads from this same time period (and earlier) probably lack the
6417 * tachometer as well.
6419 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6420 * was never fixed by IBM to report the EC firmware version string
6421 * probably support the tachometer (like the early X models), so
6422 * detecting it is quite hard. We need more data to know for sure.
6424 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6425 * might result.
6427 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6428 * mode.
6430 * For firmware bugs, refer to:
6431 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6433 * ----
6435 * ThinkPad EC register 0x31 bit 0 (only on select models)
6437 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6438 * show the speed of the main fan. When bit 0 of EC register 0x31
6439 * is one, the tachometer registers show the speed of the auxiliary
6440 * fan.
6442 * Fan control seems to affect both fans, regardless of the state
6443 * of this bit.
6445 * So far, only the firmware for the X60/X61 non-tablet versions
6446 * seem to support this (firmware TP-7M).
6448 * TPACPI_FAN_WR_ACPI_FANS:
6449 * ThinkPad X31, X40, X41. Not available in the X60.
6451 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6452 * high speed. ACPI DSDT seems to map these three speeds to levels
6453 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6454 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6456 * The speeds are stored on handles
6457 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6459 * There are three default speed sets, acessible as handles:
6460 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6462 * ACPI DSDT switches which set is in use depending on various
6463 * factors.
6465 * TPACPI_FAN_WR_TPEC is also available and should be used to
6466 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6467 * but the ACPI tables just mention level 7.
6470 enum { /* Fan control constants */
6471 fan_status_offset = 0x2f, /* EC register 0x2f */
6472 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6473 * 0x84 must be read before 0x85 */
6474 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6475 bit 0 selects which fan is active */
6477 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6478 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6480 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6483 enum fan_status_access_mode {
6484 TPACPI_FAN_NONE = 0, /* No fan status or control */
6485 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6486 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6489 enum fan_control_access_mode {
6490 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6491 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6492 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6493 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6496 enum fan_control_commands {
6497 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6498 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6499 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6500 * and also watchdog cmd */
6503 static int fan_control_allowed;
6505 static enum fan_status_access_mode fan_status_access_mode;
6506 static enum fan_control_access_mode fan_control_access_mode;
6507 static enum fan_control_commands fan_control_commands;
6509 static u8 fan_control_initial_status;
6510 static u8 fan_control_desired_level;
6511 static u8 fan_control_resume_level;
6512 static int fan_watchdog_maxinterval;
6514 static struct mutex fan_mutex;
6516 static void fan_watchdog_fire(struct work_struct *ignored);
6517 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6519 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6520 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6521 "\\FSPD", /* 600e/x, 770e, 770x */
6522 ); /* all others */
6523 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6524 "JFNS", /* 770x-JL */
6525 ); /* all others */
6528 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6529 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6530 * be in auto mode (0x80).
6532 * This is corrected by any write to HFSP either by the driver, or
6533 * by the firmware.
6535 * We assume 0x07 really means auto mode while this quirk is active,
6536 * as this is far more likely than the ThinkPad being in level 7,
6537 * which is only used by the firmware during thermal emergencies.
6539 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6540 * TP-70 (T43, R52), which are known to be buggy.
6543 static void fan_quirk1_setup(void)
6545 if (fan_control_initial_status == 0x07) {
6546 printk(TPACPI_NOTICE
6547 "fan_init: initial fan status is unknown, "
6548 "assuming it is in auto mode\n");
6549 tp_features.fan_ctrl_status_undef = 1;
6553 static void fan_quirk1_handle(u8 *fan_status)
6555 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6556 if (*fan_status != fan_control_initial_status) {
6557 /* something changed the HFSP regisnter since
6558 * driver init time, so it is not undefined
6559 * anymore */
6560 tp_features.fan_ctrl_status_undef = 0;
6561 } else {
6562 /* Return most likely status. In fact, it
6563 * might be the only possible status */
6564 *fan_status = TP_EC_FAN_AUTO;
6569 /* Select main fan on X60/X61, NOOP on others */
6570 static bool fan_select_fan1(void)
6572 if (tp_features.second_fan) {
6573 u8 val;
6575 if (ec_read(fan_select_offset, &val) < 0)
6576 return false;
6577 val &= 0xFEU;
6578 if (ec_write(fan_select_offset, val) < 0)
6579 return false;
6581 return true;
6584 /* Select secondary fan on X60/X61 */
6585 static bool fan_select_fan2(void)
6587 u8 val;
6589 if (!tp_features.second_fan)
6590 return false;
6592 if (ec_read(fan_select_offset, &val) < 0)
6593 return false;
6594 val |= 0x01U;
6595 if (ec_write(fan_select_offset, val) < 0)
6596 return false;
6598 return true;
6602 * Call with fan_mutex held
6604 static void fan_update_desired_level(u8 status)
6606 if ((status &
6607 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6608 if (status > 7)
6609 fan_control_desired_level = 7;
6610 else
6611 fan_control_desired_level = status;
6615 static int fan_get_status(u8 *status)
6617 u8 s;
6619 /* TODO:
6620 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6622 switch (fan_status_access_mode) {
6623 case TPACPI_FAN_RD_ACPI_GFAN:
6624 /* 570, 600e/x, 770e, 770x */
6626 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6627 return -EIO;
6629 if (likely(status))
6630 *status = s & 0x07;
6632 break;
6634 case TPACPI_FAN_RD_TPEC:
6635 /* all except 570, 600e/x, 770e, 770x */
6636 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6637 return -EIO;
6639 if (likely(status)) {
6640 *status = s;
6641 fan_quirk1_handle(status);
6644 break;
6646 default:
6647 return -ENXIO;
6650 return 0;
6653 static int fan_get_status_safe(u8 *status)
6655 int rc;
6656 u8 s;
6658 if (mutex_lock_killable(&fan_mutex))
6659 return -ERESTARTSYS;
6660 rc = fan_get_status(&s);
6661 if (!rc)
6662 fan_update_desired_level(s);
6663 mutex_unlock(&fan_mutex);
6665 if (status)
6666 *status = s;
6668 return rc;
6671 static int fan_get_speed(unsigned int *speed)
6673 u8 hi, lo;
6675 switch (fan_status_access_mode) {
6676 case TPACPI_FAN_RD_TPEC:
6677 /* all except 570, 600e/x, 770e, 770x */
6678 if (unlikely(!fan_select_fan1()))
6679 return -EIO;
6680 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6681 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6682 return -EIO;
6684 if (likely(speed))
6685 *speed = (hi << 8) | lo;
6687 break;
6689 default:
6690 return -ENXIO;
6693 return 0;
6696 static int fan2_get_speed(unsigned int *speed)
6698 u8 hi, lo;
6699 bool rc;
6701 switch (fan_status_access_mode) {
6702 case TPACPI_FAN_RD_TPEC:
6703 /* all except 570, 600e/x, 770e, 770x */
6704 if (unlikely(!fan_select_fan2()))
6705 return -EIO;
6706 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6707 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6708 fan_select_fan1(); /* play it safe */
6709 if (rc)
6710 return -EIO;
6712 if (likely(speed))
6713 *speed = (hi << 8) | lo;
6715 break;
6717 default:
6718 return -ENXIO;
6721 return 0;
6724 static int fan_set_level(int level)
6726 if (!fan_control_allowed)
6727 return -EPERM;
6729 switch (fan_control_access_mode) {
6730 case TPACPI_FAN_WR_ACPI_SFAN:
6731 if (level >= 0 && level <= 7) {
6732 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6733 return -EIO;
6734 } else
6735 return -EINVAL;
6736 break;
6738 case TPACPI_FAN_WR_ACPI_FANS:
6739 case TPACPI_FAN_WR_TPEC:
6740 if (!(level & TP_EC_FAN_AUTO) &&
6741 !(level & TP_EC_FAN_FULLSPEED) &&
6742 ((level < 0) || (level > 7)))
6743 return -EINVAL;
6745 /* safety net should the EC not support AUTO
6746 * or FULLSPEED mode bits and just ignore them */
6747 if (level & TP_EC_FAN_FULLSPEED)
6748 level |= 7; /* safety min speed 7 */
6749 else if (level & TP_EC_FAN_AUTO)
6750 level |= 4; /* safety min speed 4 */
6752 if (!acpi_ec_write(fan_status_offset, level))
6753 return -EIO;
6754 else
6755 tp_features.fan_ctrl_status_undef = 0;
6756 break;
6758 default:
6759 return -ENXIO;
6762 vdbg_printk(TPACPI_DBG_FAN,
6763 "fan control: set fan control register to 0x%02x\n", level);
6764 return 0;
6767 static int fan_set_level_safe(int level)
6769 int rc;
6771 if (!fan_control_allowed)
6772 return -EPERM;
6774 if (mutex_lock_killable(&fan_mutex))
6775 return -ERESTARTSYS;
6777 if (level == TPACPI_FAN_LAST_LEVEL)
6778 level = fan_control_desired_level;
6780 rc = fan_set_level(level);
6781 if (!rc)
6782 fan_update_desired_level(level);
6784 mutex_unlock(&fan_mutex);
6785 return rc;
6788 static int fan_set_enable(void)
6790 u8 s;
6791 int rc;
6793 if (!fan_control_allowed)
6794 return -EPERM;
6796 if (mutex_lock_killable(&fan_mutex))
6797 return -ERESTARTSYS;
6799 switch (fan_control_access_mode) {
6800 case TPACPI_FAN_WR_ACPI_FANS:
6801 case TPACPI_FAN_WR_TPEC:
6802 rc = fan_get_status(&s);
6803 if (rc < 0)
6804 break;
6806 /* Don't go out of emergency fan mode */
6807 if (s != 7) {
6808 s &= 0x07;
6809 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6812 if (!acpi_ec_write(fan_status_offset, s))
6813 rc = -EIO;
6814 else {
6815 tp_features.fan_ctrl_status_undef = 0;
6816 rc = 0;
6818 break;
6820 case TPACPI_FAN_WR_ACPI_SFAN:
6821 rc = fan_get_status(&s);
6822 if (rc < 0)
6823 break;
6825 s &= 0x07;
6827 /* Set fan to at least level 4 */
6828 s |= 4;
6830 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6831 rc = -EIO;
6832 else
6833 rc = 0;
6834 break;
6836 default:
6837 rc = -ENXIO;
6840 mutex_unlock(&fan_mutex);
6842 if (!rc)
6843 vdbg_printk(TPACPI_DBG_FAN,
6844 "fan control: set fan control register to 0x%02x\n",
6846 return rc;
6849 static int fan_set_disable(void)
6851 int rc;
6853 if (!fan_control_allowed)
6854 return -EPERM;
6856 if (mutex_lock_killable(&fan_mutex))
6857 return -ERESTARTSYS;
6859 rc = 0;
6860 switch (fan_control_access_mode) {
6861 case TPACPI_FAN_WR_ACPI_FANS:
6862 case TPACPI_FAN_WR_TPEC:
6863 if (!acpi_ec_write(fan_status_offset, 0x00))
6864 rc = -EIO;
6865 else {
6866 fan_control_desired_level = 0;
6867 tp_features.fan_ctrl_status_undef = 0;
6869 break;
6871 case TPACPI_FAN_WR_ACPI_SFAN:
6872 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6873 rc = -EIO;
6874 else
6875 fan_control_desired_level = 0;
6876 break;
6878 default:
6879 rc = -ENXIO;
6882 if (!rc)
6883 vdbg_printk(TPACPI_DBG_FAN,
6884 "fan control: set fan control register to 0\n");
6886 mutex_unlock(&fan_mutex);
6887 return rc;
6890 static int fan_set_speed(int speed)
6892 int rc;
6894 if (!fan_control_allowed)
6895 return -EPERM;
6897 if (mutex_lock_killable(&fan_mutex))
6898 return -ERESTARTSYS;
6900 rc = 0;
6901 switch (fan_control_access_mode) {
6902 case TPACPI_FAN_WR_ACPI_FANS:
6903 if (speed >= 0 && speed <= 65535) {
6904 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6905 speed, speed, speed))
6906 rc = -EIO;
6907 } else
6908 rc = -EINVAL;
6909 break;
6911 default:
6912 rc = -ENXIO;
6915 mutex_unlock(&fan_mutex);
6916 return rc;
6919 static void fan_watchdog_reset(void)
6921 static int fan_watchdog_active;
6923 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6924 return;
6926 if (fan_watchdog_active)
6927 cancel_delayed_work(&fan_watchdog_task);
6929 if (fan_watchdog_maxinterval > 0 &&
6930 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6931 fan_watchdog_active = 1;
6932 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6933 msecs_to_jiffies(fan_watchdog_maxinterval
6934 * 1000))) {
6935 printk(TPACPI_ERR
6936 "failed to queue the fan watchdog, "
6937 "watchdog will not trigger\n");
6939 } else
6940 fan_watchdog_active = 0;
6943 static void fan_watchdog_fire(struct work_struct *ignored)
6945 int rc;
6947 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6948 return;
6950 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6951 rc = fan_set_enable();
6952 if (rc < 0) {
6953 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6954 "will try again later...\n", -rc);
6955 /* reschedule for later */
6956 fan_watchdog_reset();
6961 * SYSFS fan layout: hwmon compatible (device)
6963 * pwm*_enable:
6964 * 0: "disengaged" mode
6965 * 1: manual mode
6966 * 2: native EC "auto" mode (recommended, hardware default)
6968 * pwm*: set speed in manual mode, ignored otherwise.
6969 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6970 * interpolation.
6972 * fan*_input: tachometer reading, RPM
6975 * SYSFS fan layout: extensions
6977 * fan_watchdog (driver):
6978 * fan watchdog interval in seconds, 0 disables (default), max 120
6981 /* sysfs fan pwm1_enable ----------------------------------------------- */
6982 static ssize_t fan_pwm1_enable_show(struct device *dev,
6983 struct device_attribute *attr,
6984 char *buf)
6986 int res, mode;
6987 u8 status;
6989 res = fan_get_status_safe(&status);
6990 if (res)
6991 return res;
6993 if (status & TP_EC_FAN_FULLSPEED) {
6994 mode = 0;
6995 } else if (status & TP_EC_FAN_AUTO) {
6996 mode = 2;
6997 } else
6998 mode = 1;
7000 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7003 static ssize_t fan_pwm1_enable_store(struct device *dev,
7004 struct device_attribute *attr,
7005 const char *buf, size_t count)
7007 unsigned long t;
7008 int res, level;
7010 if (parse_strtoul(buf, 2, &t))
7011 return -EINVAL;
7013 tpacpi_disclose_usertask("hwmon pwm1_enable",
7014 "set fan mode to %lu\n", t);
7016 switch (t) {
7017 case 0:
7018 level = TP_EC_FAN_FULLSPEED;
7019 break;
7020 case 1:
7021 level = TPACPI_FAN_LAST_LEVEL;
7022 break;
7023 case 2:
7024 level = TP_EC_FAN_AUTO;
7025 break;
7026 case 3:
7027 /* reserved for software-controlled auto mode */
7028 return -ENOSYS;
7029 default:
7030 return -EINVAL;
7033 res = fan_set_level_safe(level);
7034 if (res == -ENXIO)
7035 return -EINVAL;
7036 else if (res < 0)
7037 return res;
7039 fan_watchdog_reset();
7041 return count;
7044 static struct device_attribute dev_attr_fan_pwm1_enable =
7045 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7046 fan_pwm1_enable_show, fan_pwm1_enable_store);
7048 /* sysfs fan pwm1 ------------------------------------------------------ */
7049 static ssize_t fan_pwm1_show(struct device *dev,
7050 struct device_attribute *attr,
7051 char *buf)
7053 int res;
7054 u8 status;
7056 res = fan_get_status_safe(&status);
7057 if (res)
7058 return res;
7060 if ((status &
7061 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7062 status = fan_control_desired_level;
7064 if (status > 7)
7065 status = 7;
7067 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7070 static ssize_t fan_pwm1_store(struct device *dev,
7071 struct device_attribute *attr,
7072 const char *buf, size_t count)
7074 unsigned long s;
7075 int rc;
7076 u8 status, newlevel;
7078 if (parse_strtoul(buf, 255, &s))
7079 return -EINVAL;
7081 tpacpi_disclose_usertask("hwmon pwm1",
7082 "set fan speed to %lu\n", s);
7084 /* scale down from 0-255 to 0-7 */
7085 newlevel = (s >> 5) & 0x07;
7087 if (mutex_lock_killable(&fan_mutex))
7088 return -ERESTARTSYS;
7090 rc = fan_get_status(&status);
7091 if (!rc && (status &
7092 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7093 rc = fan_set_level(newlevel);
7094 if (rc == -ENXIO)
7095 rc = -EINVAL;
7096 else if (!rc) {
7097 fan_update_desired_level(newlevel);
7098 fan_watchdog_reset();
7102 mutex_unlock(&fan_mutex);
7103 return (rc)? rc : count;
7106 static struct device_attribute dev_attr_fan_pwm1 =
7107 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7108 fan_pwm1_show, fan_pwm1_store);
7110 /* sysfs fan fan1_input ------------------------------------------------ */
7111 static ssize_t fan_fan1_input_show(struct device *dev,
7112 struct device_attribute *attr,
7113 char *buf)
7115 int res;
7116 unsigned int speed;
7118 res = fan_get_speed(&speed);
7119 if (res < 0)
7120 return res;
7122 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7125 static struct device_attribute dev_attr_fan_fan1_input =
7126 __ATTR(fan1_input, S_IRUGO,
7127 fan_fan1_input_show, NULL);
7129 /* sysfs fan fan2_input ------------------------------------------------ */
7130 static ssize_t fan_fan2_input_show(struct device *dev,
7131 struct device_attribute *attr,
7132 char *buf)
7134 int res;
7135 unsigned int speed;
7137 res = fan2_get_speed(&speed);
7138 if (res < 0)
7139 return res;
7141 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7144 static struct device_attribute dev_attr_fan_fan2_input =
7145 __ATTR(fan2_input, S_IRUGO,
7146 fan_fan2_input_show, NULL);
7148 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7149 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7150 char *buf)
7152 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7155 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7156 const char *buf, size_t count)
7158 unsigned long t;
7160 if (parse_strtoul(buf, 120, &t))
7161 return -EINVAL;
7163 if (!fan_control_allowed)
7164 return -EPERM;
7166 fan_watchdog_maxinterval = t;
7167 fan_watchdog_reset();
7169 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7171 return count;
7174 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7175 fan_fan_watchdog_show, fan_fan_watchdog_store);
7177 /* --------------------------------------------------------------------- */
7178 static struct attribute *fan_attributes[] = {
7179 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7180 &dev_attr_fan_fan1_input.attr,
7181 NULL, /* for fan2_input */
7182 NULL
7185 static const struct attribute_group fan_attr_group = {
7186 .attrs = fan_attributes,
7189 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7190 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7192 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7193 { .vendor = PCI_VENDOR_ID_IBM, \
7194 .bios = TPACPI_MATCH_ANY, \
7195 .ec = TPID(__id1, __id2), \
7196 .quirks = __quirks }
7198 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7199 { .vendor = PCI_VENDOR_ID_LENOVO, \
7200 .bios = TPACPI_MATCH_ANY, \
7201 .ec = TPID(__id1, __id2), \
7202 .quirks = __quirks }
7204 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7205 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7206 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7207 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7208 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7209 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7212 #undef TPACPI_FAN_QL
7213 #undef TPACPI_FAN_QI
7215 static int __init fan_init(struct ibm_init_struct *iibm)
7217 int rc;
7218 unsigned long quirks;
7220 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7221 "initializing fan subdriver\n");
7223 mutex_init(&fan_mutex);
7224 fan_status_access_mode = TPACPI_FAN_NONE;
7225 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7226 fan_control_commands = 0;
7227 fan_watchdog_maxinterval = 0;
7228 tp_features.fan_ctrl_status_undef = 0;
7229 tp_features.second_fan = 0;
7230 fan_control_desired_level = 7;
7232 TPACPI_ACPIHANDLE_INIT(fans);
7233 TPACPI_ACPIHANDLE_INIT(gfan);
7234 TPACPI_ACPIHANDLE_INIT(sfan);
7236 quirks = tpacpi_check_quirks(fan_quirk_table,
7237 ARRAY_SIZE(fan_quirk_table));
7239 if (gfan_handle) {
7240 /* 570, 600e/x, 770e, 770x */
7241 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7242 } else {
7243 /* all other ThinkPads: note that even old-style
7244 * ThinkPad ECs supports the fan control register */
7245 if (likely(acpi_ec_read(fan_status_offset,
7246 &fan_control_initial_status))) {
7247 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7248 if (quirks & TPACPI_FAN_Q1)
7249 fan_quirk1_setup();
7250 if (quirks & TPACPI_FAN_2FAN) {
7251 tp_features.second_fan = 1;
7252 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7253 "secondary fan support enabled\n");
7255 } else {
7256 printk(TPACPI_ERR
7257 "ThinkPad ACPI EC access misbehaving, "
7258 "fan status and control unavailable\n");
7259 return 1;
7263 if (sfan_handle) {
7264 /* 570, 770x-JL */
7265 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7266 fan_control_commands |=
7267 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7268 } else {
7269 if (!gfan_handle) {
7270 /* gfan without sfan means no fan control */
7271 /* all other models implement TP EC 0x2f control */
7273 if (fans_handle) {
7274 /* X31, X40, X41 */
7275 fan_control_access_mode =
7276 TPACPI_FAN_WR_ACPI_FANS;
7277 fan_control_commands |=
7278 TPACPI_FAN_CMD_SPEED |
7279 TPACPI_FAN_CMD_LEVEL |
7280 TPACPI_FAN_CMD_ENABLE;
7281 } else {
7282 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7283 fan_control_commands |=
7284 TPACPI_FAN_CMD_LEVEL |
7285 TPACPI_FAN_CMD_ENABLE;
7290 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7291 "fan is %s, modes %d, %d\n",
7292 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7293 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7294 fan_status_access_mode, fan_control_access_mode);
7296 /* fan control master switch */
7297 if (!fan_control_allowed) {
7298 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7299 fan_control_commands = 0;
7300 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7301 "fan control features disabled by parameter\n");
7304 /* update fan_control_desired_level */
7305 if (fan_status_access_mode != TPACPI_FAN_NONE)
7306 fan_get_status_safe(NULL);
7308 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7309 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7310 if (tp_features.second_fan) {
7311 /* attach second fan tachometer */
7312 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7313 &dev_attr_fan_fan2_input.attr;
7315 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7316 &fan_attr_group);
7317 if (rc < 0)
7318 return rc;
7320 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7321 &driver_attr_fan_watchdog);
7322 if (rc < 0) {
7323 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7324 &fan_attr_group);
7325 return rc;
7327 return 0;
7328 } else
7329 return 1;
7332 static void fan_exit(void)
7334 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7335 "cancelling any pending fan watchdog tasks\n");
7337 /* FIXME: can we really do this unconditionally? */
7338 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7339 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7340 &driver_attr_fan_watchdog);
7342 cancel_delayed_work(&fan_watchdog_task);
7343 flush_workqueue(tpacpi_wq);
7346 static void fan_suspend(pm_message_t state)
7348 int rc;
7350 if (!fan_control_allowed)
7351 return;
7353 /* Store fan status in cache */
7354 fan_control_resume_level = 0;
7355 rc = fan_get_status_safe(&fan_control_resume_level);
7356 if (rc < 0)
7357 printk(TPACPI_NOTICE
7358 "failed to read fan level for later "
7359 "restore during resume: %d\n", rc);
7361 /* if it is undefined, don't attempt to restore it.
7362 * KEEP THIS LAST */
7363 if (tp_features.fan_ctrl_status_undef)
7364 fan_control_resume_level = 0;
7367 static void fan_resume(void)
7369 u8 current_level = 7;
7370 bool do_set = false;
7371 int rc;
7373 /* DSDT *always* updates status on resume */
7374 tp_features.fan_ctrl_status_undef = 0;
7376 if (!fan_control_allowed ||
7377 !fan_control_resume_level ||
7378 (fan_get_status_safe(&current_level) < 0))
7379 return;
7381 switch (fan_control_access_mode) {
7382 case TPACPI_FAN_WR_ACPI_SFAN:
7383 /* never decrease fan level */
7384 do_set = (fan_control_resume_level > current_level);
7385 break;
7386 case TPACPI_FAN_WR_ACPI_FANS:
7387 case TPACPI_FAN_WR_TPEC:
7388 /* never decrease fan level, scale is:
7389 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7391 * We expect the firmware to set either 7 or AUTO, but we
7392 * handle FULLSPEED out of paranoia.
7394 * So, we can safely only restore FULLSPEED or 7, anything
7395 * else could slow the fan. Restoring AUTO is useless, at
7396 * best that's exactly what the DSDT already set (it is the
7397 * slower it uses).
7399 * Always keep in mind that the DSDT *will* have set the
7400 * fans to what the vendor supposes is the best level. We
7401 * muck with it only to speed the fan up.
7403 if (fan_control_resume_level != 7 &&
7404 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7405 return;
7406 else
7407 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7408 (current_level != fan_control_resume_level);
7409 break;
7410 default:
7411 return;
7413 if (do_set) {
7414 printk(TPACPI_NOTICE
7415 "restoring fan level to 0x%02x\n",
7416 fan_control_resume_level);
7417 rc = fan_set_level_safe(fan_control_resume_level);
7418 if (rc < 0)
7419 printk(TPACPI_NOTICE
7420 "failed to restore fan level: %d\n", rc);
7424 static int fan_read(char *p)
7426 int len = 0;
7427 int rc;
7428 u8 status;
7429 unsigned int speed = 0;
7431 switch (fan_status_access_mode) {
7432 case TPACPI_FAN_RD_ACPI_GFAN:
7433 /* 570, 600e/x, 770e, 770x */
7434 rc = fan_get_status_safe(&status);
7435 if (rc < 0)
7436 return rc;
7438 len += sprintf(p + len, "status:\t\t%s\n"
7439 "level:\t\t%d\n",
7440 (status != 0) ? "enabled" : "disabled", status);
7441 break;
7443 case TPACPI_FAN_RD_TPEC:
7444 /* all except 570, 600e/x, 770e, 770x */
7445 rc = fan_get_status_safe(&status);
7446 if (rc < 0)
7447 return rc;
7449 len += sprintf(p + len, "status:\t\t%s\n",
7450 (status != 0) ? "enabled" : "disabled");
7452 rc = fan_get_speed(&speed);
7453 if (rc < 0)
7454 return rc;
7456 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7458 if (status & TP_EC_FAN_FULLSPEED)
7459 /* Disengaged mode takes precedence */
7460 len += sprintf(p + len, "level:\t\tdisengaged\n");
7461 else if (status & TP_EC_FAN_AUTO)
7462 len += sprintf(p + len, "level:\t\tauto\n");
7463 else
7464 len += sprintf(p + len, "level:\t\t%d\n", status);
7465 break;
7467 case TPACPI_FAN_NONE:
7468 default:
7469 len += sprintf(p + len, "status:\t\tnot supported\n");
7472 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7473 len += sprintf(p + len, "commands:\tlevel <level>");
7475 switch (fan_control_access_mode) {
7476 case TPACPI_FAN_WR_ACPI_SFAN:
7477 len += sprintf(p + len, " (<level> is 0-7)\n");
7478 break;
7480 default:
7481 len += sprintf(p + len, " (<level> is 0-7, "
7482 "auto, disengaged, full-speed)\n");
7483 break;
7487 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7488 len += sprintf(p + len, "commands:\tenable, disable\n"
7489 "commands:\twatchdog <timeout> (<timeout> "
7490 "is 0 (off), 1-120 (seconds))\n");
7492 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7493 len += sprintf(p + len, "commands:\tspeed <speed>"
7494 " (<speed> is 0-65535)\n");
7496 return len;
7499 static int fan_write_cmd_level(const char *cmd, int *rc)
7501 int level;
7503 if (strlencmp(cmd, "level auto") == 0)
7504 level = TP_EC_FAN_AUTO;
7505 else if ((strlencmp(cmd, "level disengaged") == 0) |
7506 (strlencmp(cmd, "level full-speed") == 0))
7507 level = TP_EC_FAN_FULLSPEED;
7508 else if (sscanf(cmd, "level %d", &level) != 1)
7509 return 0;
7511 *rc = fan_set_level_safe(level);
7512 if (*rc == -ENXIO)
7513 printk(TPACPI_ERR "level command accepted for unsupported "
7514 "access mode %d", fan_control_access_mode);
7515 else if (!*rc)
7516 tpacpi_disclose_usertask("procfs fan",
7517 "set level to %d\n", level);
7519 return 1;
7522 static int fan_write_cmd_enable(const char *cmd, int *rc)
7524 if (strlencmp(cmd, "enable") != 0)
7525 return 0;
7527 *rc = fan_set_enable();
7528 if (*rc == -ENXIO)
7529 printk(TPACPI_ERR "enable command accepted for unsupported "
7530 "access mode %d", fan_control_access_mode);
7531 else if (!*rc)
7532 tpacpi_disclose_usertask("procfs fan", "enable\n");
7534 return 1;
7537 static int fan_write_cmd_disable(const char *cmd, int *rc)
7539 if (strlencmp(cmd, "disable") != 0)
7540 return 0;
7542 *rc = fan_set_disable();
7543 if (*rc == -ENXIO)
7544 printk(TPACPI_ERR "disable command accepted for unsupported "
7545 "access mode %d", fan_control_access_mode);
7546 else if (!*rc)
7547 tpacpi_disclose_usertask("procfs fan", "disable\n");
7549 return 1;
7552 static int fan_write_cmd_speed(const char *cmd, int *rc)
7554 int speed;
7556 /* TODO:
7557 * Support speed <low> <medium> <high> ? */
7559 if (sscanf(cmd, "speed %d", &speed) != 1)
7560 return 0;
7562 *rc = fan_set_speed(speed);
7563 if (*rc == -ENXIO)
7564 printk(TPACPI_ERR "speed command accepted for unsupported "
7565 "access mode %d", fan_control_access_mode);
7566 else if (!*rc)
7567 tpacpi_disclose_usertask("procfs fan",
7568 "set speed to %d\n", speed);
7570 return 1;
7573 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7575 int interval;
7577 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7578 return 0;
7580 if (interval < 0 || interval > 120)
7581 *rc = -EINVAL;
7582 else {
7583 fan_watchdog_maxinterval = interval;
7584 tpacpi_disclose_usertask("procfs fan",
7585 "set watchdog timer to %d\n",
7586 interval);
7589 return 1;
7592 static int fan_write(char *buf)
7594 char *cmd;
7595 int rc = 0;
7597 while (!rc && (cmd = next_cmd(&buf))) {
7598 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7599 fan_write_cmd_level(cmd, &rc)) &&
7600 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7601 (fan_write_cmd_enable(cmd, &rc) ||
7602 fan_write_cmd_disable(cmd, &rc) ||
7603 fan_write_cmd_watchdog(cmd, &rc))) &&
7604 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7605 fan_write_cmd_speed(cmd, &rc))
7607 rc = -EINVAL;
7608 else if (!rc)
7609 fan_watchdog_reset();
7612 return rc;
7615 static struct ibm_struct fan_driver_data = {
7616 .name = "fan",
7617 .read = fan_read,
7618 .write = fan_write,
7619 .exit = fan_exit,
7620 .suspend = fan_suspend,
7621 .resume = fan_resume,
7624 /****************************************************************************
7625 ****************************************************************************
7627 * Infrastructure
7629 ****************************************************************************
7630 ****************************************************************************/
7633 * HKEY event callout for other subdrivers go here
7634 * (yes, it is ugly, but it is quick, safe, and gets the job done
7636 static void tpacpi_driver_event(const unsigned int hkey_event)
7642 static void hotkey_driver_event(const unsigned int scancode)
7644 tpacpi_driver_event(0x1001 + scancode);
7647 /* sysfs name ---------------------------------------------------------- */
7648 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7649 struct device_attribute *attr,
7650 char *buf)
7652 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7655 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7656 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7658 /* --------------------------------------------------------------------- */
7660 /* /proc support */
7661 static struct proc_dir_entry *proc_dir;
7664 * Module and infrastructure proble, init and exit handling
7667 static int force_load;
7669 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7670 static const char * __init str_supported(int is_supported)
7672 static char text_unsupported[] __initdata = "not supported";
7674 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7676 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7678 static void ibm_exit(struct ibm_struct *ibm)
7680 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7682 list_del_init(&ibm->all_drivers);
7684 if (ibm->flags.acpi_notify_installed) {
7685 dbg_printk(TPACPI_DBG_EXIT,
7686 "%s: acpi_remove_notify_handler\n", ibm->name);
7687 BUG_ON(!ibm->acpi);
7688 acpi_remove_notify_handler(*ibm->acpi->handle,
7689 ibm->acpi->type,
7690 dispatch_acpi_notify);
7691 ibm->flags.acpi_notify_installed = 0;
7692 ibm->flags.acpi_notify_installed = 0;
7695 if (ibm->flags.proc_created) {
7696 dbg_printk(TPACPI_DBG_EXIT,
7697 "%s: remove_proc_entry\n", ibm->name);
7698 remove_proc_entry(ibm->name, proc_dir);
7699 ibm->flags.proc_created = 0;
7702 if (ibm->flags.acpi_driver_registered) {
7703 dbg_printk(TPACPI_DBG_EXIT,
7704 "%s: acpi_bus_unregister_driver\n", ibm->name);
7705 BUG_ON(!ibm->acpi);
7706 acpi_bus_unregister_driver(ibm->acpi->driver);
7707 kfree(ibm->acpi->driver);
7708 ibm->acpi->driver = NULL;
7709 ibm->flags.acpi_driver_registered = 0;
7712 if (ibm->flags.init_called && ibm->exit) {
7713 ibm->exit();
7714 ibm->flags.init_called = 0;
7717 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7720 static int __init ibm_init(struct ibm_init_struct *iibm)
7722 int ret;
7723 struct ibm_struct *ibm = iibm->data;
7724 struct proc_dir_entry *entry;
7726 BUG_ON(ibm == NULL);
7728 INIT_LIST_HEAD(&ibm->all_drivers);
7730 if (ibm->flags.experimental && !experimental)
7731 return 0;
7733 dbg_printk(TPACPI_DBG_INIT,
7734 "probing for %s\n", ibm->name);
7736 if (iibm->init) {
7737 ret = iibm->init(iibm);
7738 if (ret > 0)
7739 return 0; /* probe failed */
7740 if (ret)
7741 return ret;
7743 ibm->flags.init_called = 1;
7746 if (ibm->acpi) {
7747 if (ibm->acpi->hid) {
7748 ret = register_tpacpi_subdriver(ibm);
7749 if (ret)
7750 goto err_out;
7753 if (ibm->acpi->notify) {
7754 ret = setup_acpi_notify(ibm);
7755 if (ret == -ENODEV) {
7756 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7757 ibm->name);
7758 ret = 0;
7759 goto err_out;
7761 if (ret < 0)
7762 goto err_out;
7766 dbg_printk(TPACPI_DBG_INIT,
7767 "%s installed\n", ibm->name);
7769 if (ibm->read) {
7770 entry = create_proc_entry(ibm->name,
7771 S_IFREG | S_IRUGO | S_IWUSR,
7772 proc_dir);
7773 if (!entry) {
7774 printk(TPACPI_ERR "unable to create proc entry %s\n",
7775 ibm->name);
7776 ret = -ENODEV;
7777 goto err_out;
7779 entry->owner = THIS_MODULE;
7780 entry->data = ibm;
7781 entry->read_proc = &dispatch_procfs_read;
7782 if (ibm->write)
7783 entry->write_proc = &dispatch_procfs_write;
7784 ibm->flags.proc_created = 1;
7787 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7789 return 0;
7791 err_out:
7792 dbg_printk(TPACPI_DBG_INIT,
7793 "%s: at error exit path with result %d\n",
7794 ibm->name, ret);
7796 ibm_exit(ibm);
7797 return (ret < 0)? ret : 0;
7800 /* Probing */
7802 static bool __pure __init tpacpi_is_fw_digit(const char c)
7804 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7807 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7808 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7809 const char t)
7811 return s && strlen(s) >= 8 &&
7812 tpacpi_is_fw_digit(s[0]) &&
7813 tpacpi_is_fw_digit(s[1]) &&
7814 s[2] == t && s[3] == 'T' &&
7815 tpacpi_is_fw_digit(s[4]) &&
7816 tpacpi_is_fw_digit(s[5]) &&
7817 s[6] == 'W' && s[7] == 'W';
7820 /* returns 0 - probe ok, or < 0 - probe error.
7821 * Probe ok doesn't mean thinkpad found.
7822 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7823 static int __must_check __init get_thinkpad_model_data(
7824 struct thinkpad_id_data *tp)
7826 const struct dmi_device *dev = NULL;
7827 char ec_fw_string[18];
7828 char const *s;
7830 if (!tp)
7831 return -EINVAL;
7833 memset(tp, 0, sizeof(*tp));
7835 if (dmi_name_in_vendors("IBM"))
7836 tp->vendor = PCI_VENDOR_ID_IBM;
7837 else if (dmi_name_in_vendors("LENOVO"))
7838 tp->vendor = PCI_VENDOR_ID_LENOVO;
7839 else
7840 return 0;
7842 s = dmi_get_system_info(DMI_BIOS_VERSION);
7843 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7844 if (s && !tp->bios_version_str)
7845 return -ENOMEM;
7847 /* Really ancient ThinkPad 240X will fail this, which is fine */
7848 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7849 return 0;
7851 tp->bios_model = tp->bios_version_str[0]
7852 | (tp->bios_version_str[1] << 8);
7853 tp->bios_release = (tp->bios_version_str[4] << 8)
7854 | tp->bios_version_str[5];
7857 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7858 * X32 or newer, all Z series; Some models must have an
7859 * up-to-date BIOS or they will not be detected.
7861 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7863 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7864 if (sscanf(dev->name,
7865 "IBM ThinkPad Embedded Controller -[%17c",
7866 ec_fw_string) == 1) {
7867 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7868 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7870 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7871 if (!tp->ec_version_str)
7872 return -ENOMEM;
7874 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7875 tp->ec_model = ec_fw_string[0]
7876 | (ec_fw_string[1] << 8);
7877 tp->ec_release = (ec_fw_string[4] << 8)
7878 | ec_fw_string[5];
7879 } else {
7880 printk(TPACPI_NOTICE
7881 "ThinkPad firmware release %s "
7882 "doesn't match the known patterns\n",
7883 ec_fw_string);
7884 printk(TPACPI_NOTICE
7885 "please report this to %s\n",
7886 TPACPI_MAIL);
7888 break;
7892 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7893 if (s && !strnicmp(s, "ThinkPad", 8)) {
7894 tp->model_str = kstrdup(s, GFP_KERNEL);
7895 if (!tp->model_str)
7896 return -ENOMEM;
7899 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7900 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7901 if (s && !tp->nummodel_str)
7902 return -ENOMEM;
7904 return 0;
7907 static int __init probe_for_thinkpad(void)
7909 int is_thinkpad;
7911 if (acpi_disabled)
7912 return -ENODEV;
7915 * Non-ancient models have better DMI tagging, but very old models
7916 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7918 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7919 (thinkpad_id.ec_model != 0) ||
7920 tpacpi_is_fw_known();
7922 /* ec is required because many other handles are relative to it */
7923 TPACPI_ACPIHANDLE_INIT(ec);
7924 if (!ec_handle) {
7925 if (is_thinkpad)
7926 printk(TPACPI_ERR
7927 "Not yet supported ThinkPad detected!\n");
7928 return -ENODEV;
7931 if (!is_thinkpad && !force_load)
7932 return -ENODEV;
7934 return 0;
7938 /* Module init, exit, parameters */
7940 static struct ibm_init_struct ibms_init[] __initdata = {
7942 .init = thinkpad_acpi_driver_init,
7943 .data = &thinkpad_acpi_driver_data,
7946 .init = hotkey_init,
7947 .data = &hotkey_driver_data,
7950 .init = bluetooth_init,
7951 .data = &bluetooth_driver_data,
7954 .init = wan_init,
7955 .data = &wan_driver_data,
7958 .init = uwb_init,
7959 .data = &uwb_driver_data,
7961 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7963 .init = video_init,
7964 .data = &video_driver_data,
7966 #endif
7968 .init = light_init,
7969 .data = &light_driver_data,
7972 .init = cmos_init,
7973 .data = &cmos_driver_data,
7976 .init = led_init,
7977 .data = &led_driver_data,
7980 .init = beep_init,
7981 .data = &beep_driver_data,
7984 .init = thermal_init,
7985 .data = &thermal_driver_data,
7988 .data = &ecdump_driver_data,
7991 .init = brightness_init,
7992 .data = &brightness_driver_data,
7995 .data = &volume_driver_data,
7998 .init = fan_init,
7999 .data = &fan_driver_data,
8003 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8005 unsigned int i;
8006 struct ibm_struct *ibm;
8008 if (!kp || !kp->name || !val)
8009 return -EINVAL;
8011 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8012 ibm = ibms_init[i].data;
8013 WARN_ON(ibm == NULL);
8015 if (!ibm || !ibm->name)
8016 continue;
8018 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8019 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8020 return -ENOSPC;
8021 strcpy(ibms_init[i].param, val);
8022 strcat(ibms_init[i].param, ",");
8023 return 0;
8027 return -EINVAL;
8030 module_param(experimental, int, 0);
8031 MODULE_PARM_DESC(experimental,
8032 "Enables experimental features when non-zero");
8034 module_param_named(debug, dbg_level, uint, 0);
8035 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8037 module_param(force_load, bool, 0);
8038 MODULE_PARM_DESC(force_load,
8039 "Attempts to load the driver even on a "
8040 "mis-identified ThinkPad when true");
8042 module_param_named(fan_control, fan_control_allowed, bool, 0);
8043 MODULE_PARM_DESC(fan_control,
8044 "Enables setting fan parameters features when true");
8046 module_param_named(brightness_mode, brightness_mode, uint, 0);
8047 MODULE_PARM_DESC(brightness_mode,
8048 "Selects brightness control strategy: "
8049 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8051 module_param(brightness_enable, uint, 0);
8052 MODULE_PARM_DESC(brightness_enable,
8053 "Enables backlight control when 1, disables when 0");
8055 module_param(hotkey_report_mode, uint, 0);
8056 MODULE_PARM_DESC(hotkey_report_mode,
8057 "used for backwards compatibility with userspace, "
8058 "see documentation");
8060 #define TPACPI_PARAM(feature) \
8061 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8062 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8063 "at module load, see documentation")
8065 TPACPI_PARAM(hotkey);
8066 TPACPI_PARAM(bluetooth);
8067 TPACPI_PARAM(video);
8068 TPACPI_PARAM(light);
8069 TPACPI_PARAM(cmos);
8070 TPACPI_PARAM(led);
8071 TPACPI_PARAM(beep);
8072 TPACPI_PARAM(ecdump);
8073 TPACPI_PARAM(brightness);
8074 TPACPI_PARAM(volume);
8075 TPACPI_PARAM(fan);
8077 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8078 module_param(dbg_wlswemul, uint, 0);
8079 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8080 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8081 MODULE_PARM_DESC(wlsw_state,
8082 "Initial state of the emulated WLSW switch");
8084 module_param(dbg_bluetoothemul, uint, 0);
8085 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8086 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8087 MODULE_PARM_DESC(bluetooth_state,
8088 "Initial state of the emulated bluetooth switch");
8090 module_param(dbg_wwanemul, uint, 0);
8091 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8092 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8093 MODULE_PARM_DESC(wwan_state,
8094 "Initial state of the emulated WWAN switch");
8096 module_param(dbg_uwbemul, uint, 0);
8097 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8098 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8099 MODULE_PARM_DESC(uwb_state,
8100 "Initial state of the emulated UWB switch");
8101 #endif
8103 static void thinkpad_acpi_module_exit(void)
8105 struct ibm_struct *ibm, *itmp;
8107 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8109 list_for_each_entry_safe_reverse(ibm, itmp,
8110 &tpacpi_all_drivers,
8111 all_drivers) {
8112 ibm_exit(ibm);
8115 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8117 if (tpacpi_inputdev) {
8118 if (tp_features.input_device_registered)
8119 input_unregister_device(tpacpi_inputdev);
8120 else
8121 input_free_device(tpacpi_inputdev);
8124 if (tpacpi_hwmon)
8125 hwmon_device_unregister(tpacpi_hwmon);
8127 if (tp_features.sensors_pdev_attrs_registered)
8128 device_remove_file(&tpacpi_sensors_pdev->dev,
8129 &dev_attr_thinkpad_acpi_pdev_name);
8130 if (tpacpi_sensors_pdev)
8131 platform_device_unregister(tpacpi_sensors_pdev);
8132 if (tpacpi_pdev)
8133 platform_device_unregister(tpacpi_pdev);
8135 if (tp_features.sensors_pdrv_attrs_registered)
8136 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8137 if (tp_features.platform_drv_attrs_registered)
8138 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8140 if (tp_features.sensors_pdrv_registered)
8141 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8143 if (tp_features.platform_drv_registered)
8144 platform_driver_unregister(&tpacpi_pdriver);
8146 if (proc_dir)
8147 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8149 if (tpacpi_wq)
8150 destroy_workqueue(tpacpi_wq);
8152 kfree(thinkpad_id.bios_version_str);
8153 kfree(thinkpad_id.ec_version_str);
8154 kfree(thinkpad_id.model_str);
8158 static int __init thinkpad_acpi_module_init(void)
8160 int ret, i;
8162 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8164 /* Parameter checking */
8165 if (hotkey_report_mode > 2)
8166 return -EINVAL;
8168 /* Driver-level probe */
8170 ret = get_thinkpad_model_data(&thinkpad_id);
8171 if (ret) {
8172 printk(TPACPI_ERR
8173 "unable to get DMI data: %d\n", ret);
8174 thinkpad_acpi_module_exit();
8175 return ret;
8177 ret = probe_for_thinkpad();
8178 if (ret) {
8179 thinkpad_acpi_module_exit();
8180 return ret;
8183 /* Driver initialization */
8185 TPACPI_ACPIHANDLE_INIT(ecrd);
8186 TPACPI_ACPIHANDLE_INIT(ecwr);
8188 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8189 if (!tpacpi_wq) {
8190 thinkpad_acpi_module_exit();
8191 return -ENOMEM;
8194 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8195 if (!proc_dir) {
8196 printk(TPACPI_ERR
8197 "unable to create proc dir " TPACPI_PROC_DIR);
8198 thinkpad_acpi_module_exit();
8199 return -ENODEV;
8201 proc_dir->owner = THIS_MODULE;
8203 ret = platform_driver_register(&tpacpi_pdriver);
8204 if (ret) {
8205 printk(TPACPI_ERR
8206 "unable to register main platform driver\n");
8207 thinkpad_acpi_module_exit();
8208 return ret;
8210 tp_features.platform_drv_registered = 1;
8212 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8213 if (ret) {
8214 printk(TPACPI_ERR
8215 "unable to register hwmon platform driver\n");
8216 thinkpad_acpi_module_exit();
8217 return ret;
8219 tp_features.sensors_pdrv_registered = 1;
8221 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8222 if (!ret) {
8223 tp_features.platform_drv_attrs_registered = 1;
8224 ret = tpacpi_create_driver_attributes(
8225 &tpacpi_hwmon_pdriver.driver);
8227 if (ret) {
8228 printk(TPACPI_ERR
8229 "unable to create sysfs driver attributes\n");
8230 thinkpad_acpi_module_exit();
8231 return ret;
8233 tp_features.sensors_pdrv_attrs_registered = 1;
8236 /* Device initialization */
8237 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8238 NULL, 0);
8239 if (IS_ERR(tpacpi_pdev)) {
8240 ret = PTR_ERR(tpacpi_pdev);
8241 tpacpi_pdev = NULL;
8242 printk(TPACPI_ERR "unable to register platform device\n");
8243 thinkpad_acpi_module_exit();
8244 return ret;
8246 tpacpi_sensors_pdev = platform_device_register_simple(
8247 TPACPI_HWMON_DRVR_NAME,
8248 -1, NULL, 0);
8249 if (IS_ERR(tpacpi_sensors_pdev)) {
8250 ret = PTR_ERR(tpacpi_sensors_pdev);
8251 tpacpi_sensors_pdev = NULL;
8252 printk(TPACPI_ERR
8253 "unable to register hwmon platform device\n");
8254 thinkpad_acpi_module_exit();
8255 return ret;
8257 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8258 &dev_attr_thinkpad_acpi_pdev_name);
8259 if (ret) {
8260 printk(TPACPI_ERR
8261 "unable to create sysfs hwmon device attributes\n");
8262 thinkpad_acpi_module_exit();
8263 return ret;
8265 tp_features.sensors_pdev_attrs_registered = 1;
8266 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8267 if (IS_ERR(tpacpi_hwmon)) {
8268 ret = PTR_ERR(tpacpi_hwmon);
8269 tpacpi_hwmon = NULL;
8270 printk(TPACPI_ERR "unable to register hwmon device\n");
8271 thinkpad_acpi_module_exit();
8272 return ret;
8274 mutex_init(&tpacpi_inputdev_send_mutex);
8275 tpacpi_inputdev = input_allocate_device();
8276 if (!tpacpi_inputdev) {
8277 printk(TPACPI_ERR "unable to allocate input device\n");
8278 thinkpad_acpi_module_exit();
8279 return -ENOMEM;
8280 } else {
8281 /* Prepare input device, but don't register */
8282 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8283 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8284 tpacpi_inputdev->id.bustype = BUS_HOST;
8285 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8286 thinkpad_id.vendor :
8287 PCI_VENDOR_ID_IBM;
8288 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8289 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8291 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8292 ret = ibm_init(&ibms_init[i]);
8293 if (ret >= 0 && *ibms_init[i].param)
8294 ret = ibms_init[i].data->write(ibms_init[i].param);
8295 if (ret < 0) {
8296 thinkpad_acpi_module_exit();
8297 return ret;
8300 ret = input_register_device(tpacpi_inputdev);
8301 if (ret < 0) {
8302 printk(TPACPI_ERR "unable to register input device\n");
8303 thinkpad_acpi_module_exit();
8304 return ret;
8305 } else {
8306 tp_features.input_device_registered = 1;
8309 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8310 return 0;
8313 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8316 * This will autoload the driver in almost every ThinkPad
8317 * in widespread use.
8319 * Only _VERY_ old models, like the 240, 240x and 570 lack
8320 * the HKEY event interface.
8322 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8325 * DMI matching for module autoloading
8327 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8328 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8330 * Only models listed in thinkwiki will be supported, so add yours
8331 * if it is not there yet.
8333 #define IBM_BIOS_MODULE_ALIAS(__type) \
8334 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8336 /* Ancient thinkpad BIOSes have to be identified by
8337 * BIOS type or model number, and there are far less
8338 * BIOS types than model numbers... */
8339 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8341 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8342 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8343 MODULE_DESCRIPTION(TPACPI_DESC);
8344 MODULE_VERSION(TPACPI_VERSION);
8345 MODULE_LICENSE("GPL");
8347 module_init(thinkpad_acpi_module_init);
8348 module_exit(thinkpad_acpi_module_exit);