thinkpad-acpi: hotkey event driver update
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobda9cc2e77e1f9dfb265ddfd0cb044a42402f0328
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 /* HKEY.MHKG() return bits */
1754 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1756 static int hotkey_get_wlsw(int *status)
1758 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1759 if (dbg_wlswemul) {
1760 *status = !!tpacpi_wlsw_emulstate;
1761 return 0;
1763 #endif
1764 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1765 return -EIO;
1766 return 0;
1769 static int hotkey_get_tablet_mode(int *status)
1771 int s;
1773 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1774 return -EIO;
1776 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1777 return 0;
1781 * Reads current event mask from firmware, and updates
1782 * hotkey_acpi_mask accordingly. Also resets any bits
1783 * from hotkey_user_mask that are unavailable to be
1784 * delivered (shadow requirement of the userspace ABI).
1786 * Call with hotkey_mutex held
1788 static int hotkey_mask_get(void)
1790 if (tp_features.hotkey_mask) {
1791 u32 m = 0;
1793 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1794 return -EIO;
1796 hotkey_acpi_mask = m;
1797 } else {
1798 /* no mask support doesn't mean no event support... */
1799 hotkey_acpi_mask = hotkey_all_mask;
1802 /* sync userspace-visible mask */
1803 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
1805 return 0;
1808 void static hotkey_mask_warn_incomplete_mask(void)
1810 /* log only what the user can fix... */
1811 const u32 wantedmask = hotkey_driver_mask &
1812 ~(hotkey_acpi_mask | hotkey_source_mask) &
1813 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
1815 if (wantedmask)
1816 printk(TPACPI_NOTICE
1817 "required events 0x%08x not enabled!\n",
1818 wantedmask);
1822 * Set the firmware mask when supported
1824 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
1826 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
1828 * Call with hotkey_mutex held
1830 static int hotkey_mask_set(u32 mask)
1832 int i;
1833 int rc = 0;
1835 const u32 fwmask = mask & ~hotkey_source_mask;
1837 if (tp_features.hotkey_mask) {
1838 for (i = 0; i < 32; i++) {
1839 if (!acpi_evalf(hkey_handle,
1840 NULL, "MHKM", "vdd", i + 1,
1841 !!(mask & (1 << i)))) {
1842 rc = -EIO;
1843 break;
1849 * We *must* make an inconditional call to hotkey_mask_get to
1850 * refresh hotkey_acpi_mask and update hotkey_user_mask
1852 * Take the opportunity to also log when we cannot _enable_
1853 * a given event.
1855 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
1856 printk(TPACPI_NOTICE
1857 "asked for hotkey mask 0x%08x, but "
1858 "firmware forced it to 0x%08x\n",
1859 fwmask, hotkey_acpi_mask);
1862 hotkey_mask_warn_incomplete_mask();
1864 return rc;
1868 * Sets hotkey_user_mask and tries to set the firmware mask
1870 * Call with hotkey_mutex held
1872 static int hotkey_user_mask_set(const u32 mask)
1874 int rc;
1876 /* Give people a chance to notice they are doing something that
1877 * is bound to go boom on their users sooner or later */
1878 if (!tp_warned.hotkey_mask_ff &&
1879 (mask == 0xffff || mask == 0xffffff ||
1880 mask == 0xffffffff)) {
1881 tp_warned.hotkey_mask_ff = 1;
1882 printk(TPACPI_NOTICE
1883 "setting the hotkey mask to 0x%08x is likely "
1884 "not the best way to go about it\n", mask);
1885 printk(TPACPI_NOTICE
1886 "please consider using the driver defaults, "
1887 "and refer to up-to-date thinkpad-acpi "
1888 "documentation\n");
1891 /* Try to enable what the user asked for, plus whatever we need.
1892 * this syncs everything but won't enable bits in hotkey_user_mask */
1893 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
1895 /* Enable the available bits in hotkey_user_mask */
1896 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
1898 return rc;
1901 static int hotkey_status_get(int *status)
1903 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1904 return -EIO;
1906 return 0;
1909 static int hotkey_status_set(bool enable)
1911 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1912 return -EIO;
1914 return 0;
1917 static void tpacpi_input_send_tabletsw(void)
1919 int state;
1921 if (tp_features.hotkey_tablet &&
1922 !hotkey_get_tablet_mode(&state)) {
1923 mutex_lock(&tpacpi_inputdev_send_mutex);
1925 input_report_switch(tpacpi_inputdev,
1926 SW_TABLET_MODE, !!state);
1927 input_sync(tpacpi_inputdev);
1929 mutex_unlock(&tpacpi_inputdev_send_mutex);
1933 /* Do NOT call without validating scancode first */
1934 static void tpacpi_input_send_key(const unsigned int scancode)
1936 const unsigned int keycode = hotkey_keycode_map[scancode];
1938 if (keycode != KEY_RESERVED) {
1939 mutex_lock(&tpacpi_inputdev_send_mutex);
1941 input_report_key(tpacpi_inputdev, keycode, 1);
1942 if (keycode == KEY_UNKNOWN)
1943 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1944 scancode);
1945 input_sync(tpacpi_inputdev);
1947 input_report_key(tpacpi_inputdev, keycode, 0);
1948 if (keycode == KEY_UNKNOWN)
1949 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1950 scancode);
1951 input_sync(tpacpi_inputdev);
1953 mutex_unlock(&tpacpi_inputdev_send_mutex);
1957 /* Do NOT call without validating scancode first */
1958 static void tpacpi_input_send_key_masked(const unsigned int scancode)
1960 if (hotkey_user_mask & (1 << scancode))
1961 tpacpi_input_send_key(scancode);
1964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1965 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1967 /* Do NOT call without validating scancode first */
1968 static void tpacpi_hotkey_send_key(unsigned int scancode)
1970 tpacpi_input_send_key_masked(scancode);
1971 if (hotkey_report_mode < 2) {
1972 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1973 0x80, 0x1001 + scancode);
1977 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
1979 u8 d;
1981 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1982 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1983 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1984 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1985 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1986 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1988 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1989 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1990 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1992 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1993 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1994 n->displayexp_toggle =
1995 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1997 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1998 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1999 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2000 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2001 n->brightness_toggle =
2002 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2004 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2005 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2006 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2007 >> TP_NVRAM_POS_LEVEL_VOLUME;
2008 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2009 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2013 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2014 struct tp_nvram_state *newn,
2015 const u32 event_mask)
2018 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2019 do { \
2020 if ((event_mask & (1 << __scancode)) && \
2021 oldn->__member != newn->__member) \
2022 tpacpi_hotkey_send_key(__scancode); \
2023 } while (0)
2025 #define TPACPI_MAY_SEND_KEY(__scancode) \
2026 do { \
2027 if (event_mask & (1 << __scancode)) \
2028 tpacpi_hotkey_send_key(__scancode); \
2029 } while (0)
2031 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2032 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2033 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2034 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2036 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2038 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2040 /* handle volume */
2041 if (oldn->volume_toggle != newn->volume_toggle) {
2042 if (oldn->mute != newn->mute) {
2043 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2045 if (oldn->volume_level > newn->volume_level) {
2046 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2047 } else if (oldn->volume_level < newn->volume_level) {
2048 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2049 } else if (oldn->mute == newn->mute) {
2050 /* repeated key presses that didn't change state */
2051 if (newn->mute) {
2052 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2053 } else if (newn->volume_level != 0) {
2054 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2055 } else {
2056 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2061 /* handle brightness */
2062 if (oldn->brightness_toggle != newn->brightness_toggle) {
2063 if (oldn->brightness_level < newn->brightness_level) {
2064 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2065 } else if (oldn->brightness_level > newn->brightness_level) {
2066 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2067 } else {
2068 /* repeated key presses that didn't change state */
2069 if (newn->brightness_level != 0) {
2070 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2071 } else {
2072 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2077 #undef TPACPI_COMPARE_KEY
2078 #undef TPACPI_MAY_SEND_KEY
2082 * Polling driver
2084 * We track all events in hotkey_source_mask all the time, since
2085 * most of them are edge-based. We only issue those requested by
2086 * hotkey_user_mask or hotkey_driver_mask, though.
2088 static int hotkey_kthread(void *data)
2090 struct tp_nvram_state s[2];
2091 u32 poll_mask, event_mask;
2092 unsigned int si, so;
2093 unsigned long t;
2094 unsigned int change_detector, must_reset;
2095 unsigned int poll_freq;
2097 mutex_lock(&hotkey_thread_mutex);
2099 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2100 goto exit;
2102 set_freezable();
2104 so = 0;
2105 si = 1;
2106 t = 0;
2108 /* Initial state for compares */
2109 mutex_lock(&hotkey_thread_data_mutex);
2110 change_detector = hotkey_config_change;
2111 poll_mask = hotkey_source_mask;
2112 event_mask = hotkey_source_mask &
2113 (hotkey_driver_mask | hotkey_user_mask);
2114 poll_freq = hotkey_poll_freq;
2115 mutex_unlock(&hotkey_thread_data_mutex);
2116 hotkey_read_nvram(&s[so], poll_mask);
2118 while (!kthread_should_stop()) {
2119 if (t == 0) {
2120 if (likely(poll_freq))
2121 t = 1000/poll_freq;
2122 else
2123 t = 100; /* should never happen... */
2125 t = msleep_interruptible(t);
2126 if (unlikely(kthread_should_stop()))
2127 break;
2128 must_reset = try_to_freeze();
2129 if (t > 0 && !must_reset)
2130 continue;
2132 mutex_lock(&hotkey_thread_data_mutex);
2133 if (must_reset || hotkey_config_change != change_detector) {
2134 /* forget old state on thaw or config change */
2135 si = so;
2136 t = 0;
2137 change_detector = hotkey_config_change;
2139 poll_mask = hotkey_source_mask;
2140 event_mask = hotkey_source_mask &
2141 (hotkey_driver_mask | hotkey_user_mask);
2142 poll_freq = hotkey_poll_freq;
2143 mutex_unlock(&hotkey_thread_data_mutex);
2145 if (likely(poll_mask)) {
2146 hotkey_read_nvram(&s[si], poll_mask);
2147 if (likely(si != so)) {
2148 hotkey_compare_and_issue_event(&s[so], &s[si],
2149 event_mask);
2153 so = si;
2154 si ^= 1;
2157 exit:
2158 mutex_unlock(&hotkey_thread_mutex);
2159 return 0;
2162 /* call with hotkey_mutex held */
2163 static void hotkey_poll_stop_sync(void)
2165 if (tpacpi_hotkey_task) {
2166 if (frozen(tpacpi_hotkey_task) ||
2167 freezing(tpacpi_hotkey_task))
2168 thaw_process(tpacpi_hotkey_task);
2170 kthread_stop(tpacpi_hotkey_task);
2171 tpacpi_hotkey_task = NULL;
2172 mutex_lock(&hotkey_thread_mutex);
2173 /* at this point, the thread did exit */
2174 mutex_unlock(&hotkey_thread_mutex);
2178 /* call with hotkey_mutex held */
2179 static void hotkey_poll_setup(bool may_warn)
2181 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2182 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2184 if (hotkey_poll_freq > 0 &&
2185 (poll_driver_mask ||
2186 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2187 if (!tpacpi_hotkey_task) {
2188 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2189 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2190 if (IS_ERR(tpacpi_hotkey_task)) {
2191 tpacpi_hotkey_task = NULL;
2192 printk(TPACPI_ERR
2193 "could not create kernel thread "
2194 "for hotkey polling\n");
2197 } else {
2198 hotkey_poll_stop_sync();
2199 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2200 hotkey_poll_freq == 0) {
2201 printk(TPACPI_NOTICE
2202 "hot keys 0x%08x and/or events 0x%08x "
2203 "require polling, which is currently "
2204 "disabled\n",
2205 poll_user_mask, poll_driver_mask);
2210 static void hotkey_poll_setup_safe(bool may_warn)
2212 mutex_lock(&hotkey_mutex);
2213 hotkey_poll_setup(may_warn);
2214 mutex_unlock(&hotkey_mutex);
2217 /* call with hotkey_mutex held */
2218 static void hotkey_poll_set_freq(unsigned int freq)
2220 if (!freq)
2221 hotkey_poll_stop_sync();
2223 hotkey_poll_freq = freq;
2226 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2228 static void hotkey_poll_setup_safe(bool __unused)
2232 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2234 static int hotkey_inputdev_open(struct input_dev *dev)
2236 switch (tpacpi_lifecycle) {
2237 case TPACPI_LIFE_INIT:
2239 * hotkey_init will call hotkey_poll_setup_safe
2240 * at the appropriate moment
2242 return 0;
2243 case TPACPI_LIFE_EXITING:
2244 return -EBUSY;
2245 case TPACPI_LIFE_RUNNING:
2246 hotkey_poll_setup_safe(false);
2247 return 0;
2250 /* Should only happen if tpacpi_lifecycle is corrupt */
2251 BUG();
2252 return -EBUSY;
2255 static void hotkey_inputdev_close(struct input_dev *dev)
2257 /* disable hotkey polling when possible */
2258 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2259 !(hotkey_source_mask & hotkey_driver_mask))
2260 hotkey_poll_setup_safe(false);
2263 /* sysfs hotkey enable ------------------------------------------------- */
2264 static ssize_t hotkey_enable_show(struct device *dev,
2265 struct device_attribute *attr,
2266 char *buf)
2268 int res, status;
2270 printk_deprecated_attribute("hotkey_enable",
2271 "Hotkey reporting is always enabled");
2273 res = hotkey_status_get(&status);
2274 if (res)
2275 return res;
2277 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2280 static ssize_t hotkey_enable_store(struct device *dev,
2281 struct device_attribute *attr,
2282 const char *buf, size_t count)
2284 unsigned long t;
2286 printk_deprecated_attribute("hotkey_enable",
2287 "Hotkeys can be disabled through hotkey_mask");
2289 if (parse_strtoul(buf, 1, &t))
2290 return -EINVAL;
2292 if (t == 0)
2293 return -EPERM;
2295 return count;
2298 static struct device_attribute dev_attr_hotkey_enable =
2299 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2300 hotkey_enable_show, hotkey_enable_store);
2302 /* sysfs hotkey mask --------------------------------------------------- */
2303 static ssize_t hotkey_mask_show(struct device *dev,
2304 struct device_attribute *attr,
2305 char *buf)
2307 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2310 static ssize_t hotkey_mask_store(struct device *dev,
2311 struct device_attribute *attr,
2312 const char *buf, size_t count)
2314 unsigned long t;
2315 int res;
2317 if (parse_strtoul(buf, 0xffffffffUL, &t))
2318 return -EINVAL;
2320 if (mutex_lock_killable(&hotkey_mutex))
2321 return -ERESTARTSYS;
2323 res = hotkey_user_mask_set(t);
2325 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2326 hotkey_poll_setup(true);
2327 #endif
2329 mutex_unlock(&hotkey_mutex);
2331 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2333 return (res) ? res : count;
2336 static struct device_attribute dev_attr_hotkey_mask =
2337 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2338 hotkey_mask_show, hotkey_mask_store);
2340 /* sysfs hotkey bios_enabled ------------------------------------------- */
2341 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2342 struct device_attribute *attr,
2343 char *buf)
2345 return sprintf(buf, "0\n");
2348 static struct device_attribute dev_attr_hotkey_bios_enabled =
2349 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2351 /* sysfs hotkey bios_mask ---------------------------------------------- */
2352 static ssize_t hotkey_bios_mask_show(struct device *dev,
2353 struct device_attribute *attr,
2354 char *buf)
2356 printk_deprecated_attribute("hotkey_bios_mask",
2357 "This attribute is useless.");
2358 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2361 static struct device_attribute dev_attr_hotkey_bios_mask =
2362 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2364 /* sysfs hotkey all_mask ----------------------------------------------- */
2365 static ssize_t hotkey_all_mask_show(struct device *dev,
2366 struct device_attribute *attr,
2367 char *buf)
2369 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2370 hotkey_all_mask | hotkey_source_mask);
2373 static struct device_attribute dev_attr_hotkey_all_mask =
2374 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2376 /* sysfs hotkey recommended_mask --------------------------------------- */
2377 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2378 struct device_attribute *attr,
2379 char *buf)
2381 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2382 (hotkey_all_mask | hotkey_source_mask)
2383 & ~hotkey_reserved_mask);
2386 static struct device_attribute dev_attr_hotkey_recommended_mask =
2387 __ATTR(hotkey_recommended_mask, S_IRUGO,
2388 hotkey_recommended_mask_show, NULL);
2390 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2392 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2393 static ssize_t hotkey_source_mask_show(struct device *dev,
2394 struct device_attribute *attr,
2395 char *buf)
2397 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2400 static ssize_t hotkey_source_mask_store(struct device *dev,
2401 struct device_attribute *attr,
2402 const char *buf, size_t count)
2404 unsigned long t;
2405 u32 r_ev;
2406 int rc;
2408 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2409 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2410 return -EINVAL;
2412 if (mutex_lock_killable(&hotkey_mutex))
2413 return -ERESTARTSYS;
2415 HOTKEY_CONFIG_CRITICAL_START
2416 hotkey_source_mask = t;
2417 HOTKEY_CONFIG_CRITICAL_END
2419 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2420 ~hotkey_source_mask);
2421 hotkey_poll_setup(true);
2423 /* check if events needed by the driver got disabled */
2424 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2425 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2427 mutex_unlock(&hotkey_mutex);
2429 if (rc < 0)
2430 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2431 "firmware event mask!\n");
2433 if (r_ev)
2434 printk(TPACPI_NOTICE "hotkey_source_mask: "
2435 "some important events were disabled: "
2436 "0x%04x\n", r_ev);
2438 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2440 return (rc < 0) ? rc : count;
2443 static struct device_attribute dev_attr_hotkey_source_mask =
2444 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2445 hotkey_source_mask_show, hotkey_source_mask_store);
2447 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2448 static ssize_t hotkey_poll_freq_show(struct device *dev,
2449 struct device_attribute *attr,
2450 char *buf)
2452 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2455 static ssize_t hotkey_poll_freq_store(struct device *dev,
2456 struct device_attribute *attr,
2457 const char *buf, size_t count)
2459 unsigned long t;
2461 if (parse_strtoul(buf, 25, &t))
2462 return -EINVAL;
2464 if (mutex_lock_killable(&hotkey_mutex))
2465 return -ERESTARTSYS;
2467 hotkey_poll_set_freq(t);
2468 hotkey_poll_setup(true);
2470 mutex_unlock(&hotkey_mutex);
2472 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2474 return count;
2477 static struct device_attribute dev_attr_hotkey_poll_freq =
2478 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2479 hotkey_poll_freq_show, hotkey_poll_freq_store);
2481 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2483 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2484 static ssize_t hotkey_radio_sw_show(struct device *dev,
2485 struct device_attribute *attr,
2486 char *buf)
2488 int res, s;
2489 res = hotkey_get_wlsw(&s);
2490 if (res < 0)
2491 return res;
2493 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2496 static struct device_attribute dev_attr_hotkey_radio_sw =
2497 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2499 static void hotkey_radio_sw_notify_change(void)
2501 if (tp_features.hotkey_wlsw)
2502 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2503 "hotkey_radio_sw");
2506 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2507 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2508 struct device_attribute *attr,
2509 char *buf)
2511 int res, s;
2512 res = hotkey_get_tablet_mode(&s);
2513 if (res < 0)
2514 return res;
2516 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2519 static struct device_attribute dev_attr_hotkey_tablet_mode =
2520 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2522 static void hotkey_tablet_mode_notify_change(void)
2524 if (tp_features.hotkey_tablet)
2525 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2526 "hotkey_tablet_mode");
2529 /* sysfs hotkey report_mode -------------------------------------------- */
2530 static ssize_t hotkey_report_mode_show(struct device *dev,
2531 struct device_attribute *attr,
2532 char *buf)
2534 return snprintf(buf, PAGE_SIZE, "%d\n",
2535 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2538 static struct device_attribute dev_attr_hotkey_report_mode =
2539 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2541 /* sysfs wakeup reason (pollable) -------------------------------------- */
2542 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2543 struct device_attribute *attr,
2544 char *buf)
2546 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2549 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2550 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2552 static void hotkey_wakeup_reason_notify_change(void)
2554 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2555 "wakeup_reason");
2558 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2559 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2560 struct device_attribute *attr,
2561 char *buf)
2563 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2566 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2567 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2568 hotkey_wakeup_hotunplug_complete_show, NULL);
2570 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2572 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2573 "wakeup_hotunplug_complete");
2576 /* --------------------------------------------------------------------- */
2578 static struct attribute *hotkey_attributes[] __initdata = {
2579 &dev_attr_hotkey_enable.attr,
2580 &dev_attr_hotkey_bios_enabled.attr,
2581 &dev_attr_hotkey_bios_mask.attr,
2582 &dev_attr_hotkey_report_mode.attr,
2583 &dev_attr_hotkey_wakeup_reason.attr,
2584 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2585 &dev_attr_hotkey_mask.attr,
2586 &dev_attr_hotkey_all_mask.attr,
2587 &dev_attr_hotkey_recommended_mask.attr,
2588 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2589 &dev_attr_hotkey_source_mask.attr,
2590 &dev_attr_hotkey_poll_freq.attr,
2591 #endif
2594 static void bluetooth_update_rfk(void);
2595 static void wan_update_rfk(void);
2596 static void uwb_update_rfk(void);
2597 static void tpacpi_send_radiosw_update(void)
2599 int wlsw;
2601 /* Sync these BEFORE sending any rfkill events */
2602 if (tp_features.bluetooth)
2603 bluetooth_update_rfk();
2604 if (tp_features.wan)
2605 wan_update_rfk();
2606 if (tp_features.uwb)
2607 uwb_update_rfk();
2609 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2610 mutex_lock(&tpacpi_inputdev_send_mutex);
2612 input_report_switch(tpacpi_inputdev,
2613 SW_RFKILL_ALL, !!wlsw);
2614 input_sync(tpacpi_inputdev);
2616 mutex_unlock(&tpacpi_inputdev_send_mutex);
2618 hotkey_radio_sw_notify_change();
2621 static void hotkey_exit(void)
2623 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2624 mutex_lock(&hotkey_mutex);
2625 hotkey_poll_stop_sync();
2626 mutex_unlock(&hotkey_mutex);
2627 #endif
2629 if (hotkey_dev_attributes)
2630 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2632 kfree(hotkey_keycode_map);
2634 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2635 "restoring original HKEY status and mask\n");
2636 /* yes, there is a bitwise or below, we want the
2637 * functions to be called even if one of them fail */
2638 if (((tp_features.hotkey_mask &&
2639 hotkey_mask_set(hotkey_orig_mask)) |
2640 hotkey_status_set(false)) != 0)
2641 printk(TPACPI_ERR
2642 "failed to restore hot key mask "
2643 "to BIOS defaults\n");
2646 static void __init hotkey_unmap(const unsigned int scancode)
2648 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2649 clear_bit(hotkey_keycode_map[scancode],
2650 tpacpi_inputdev->keybit);
2651 hotkey_keycode_map[scancode] = KEY_RESERVED;
2656 * HKEY quirks:
2657 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
2660 #define TPACPI_HK_Q_INIMASK 0x0001
2662 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
2663 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
2664 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
2665 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
2666 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
2667 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
2668 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
2669 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
2670 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
2671 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
2672 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
2673 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
2674 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
2675 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
2676 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
2677 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
2678 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
2679 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
2680 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
2681 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
2684 static int __init hotkey_init(struct ibm_init_struct *iibm)
2686 /* Requirements for changing the default keymaps:
2688 * 1. Many of the keys are mapped to KEY_RESERVED for very
2689 * good reasons. Do not change them unless you have deep
2690 * knowledge on the IBM and Lenovo ThinkPad firmware for
2691 * the various ThinkPad models. The driver behaves
2692 * differently for KEY_RESERVED: such keys have their
2693 * hot key mask *unset* in mask_recommended, and also
2694 * in the initial hot key mask programmed into the
2695 * firmware at driver load time, which means the firm-
2696 * ware may react very differently if you change them to
2697 * something else;
2699 * 2. You must be subscribed to the linux-thinkpad and
2700 * ibm-acpi-devel mailing lists, and you should read the
2701 * list archives since 2007 if you want to change the
2702 * keymaps. This requirement exists so that you will
2703 * know the past history of problems with the thinkpad-
2704 * acpi driver keymaps, and also that you will be
2705 * listening to any bug reports;
2707 * 3. Do not send thinkpad-acpi specific patches directly to
2708 * for merging, *ever*. Send them to the linux-acpi
2709 * mailinglist for comments. Merging is to be done only
2710 * through acpi-test and the ACPI maintainer.
2712 * If the above is too much to ask, don't change the keymap.
2713 * Ask the thinkpad-acpi maintainer to do it, instead.
2715 static u16 ibm_keycode_map[] __initdata = {
2716 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2717 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2718 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2719 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2721 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2722 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2723 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2724 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2726 /* brightness: firmware always reacts to them */
2727 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2728 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2730 /* Thinklight: firmware always react to it */
2731 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2733 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2734 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2736 /* Volume: firmware always react to it and reprograms
2737 * the built-in *extra* mixer. Never map it to control
2738 * another mixer by default. */
2739 KEY_RESERVED, /* 0x14: VOLUME UP */
2740 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2741 KEY_RESERVED, /* 0x16: MUTE */
2743 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2745 /* (assignments unknown, please report if found) */
2746 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2747 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2749 static u16 lenovo_keycode_map[] __initdata = {
2750 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2751 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2752 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2753 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2755 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2756 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2757 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2758 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2760 /* These should be enabled --only-- when ACPI video
2761 * is disabled (i.e. in "vendor" mode), and are handled
2762 * in a special way by the init code */
2763 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2764 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2766 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2768 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2769 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2771 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2772 * react to it and reprograms the built-in *extra* mixer.
2773 * Never map it to control another mixer by default.
2775 * T60?, T61, R60?, R61: firmware and EC tries to send
2776 * these over the regular keyboard, so these are no-ops,
2777 * but there are still weird bugs re. MUTE, so do not
2778 * change unless you get test reports from all Lenovo
2779 * models. May cause the BIOS to interfere with the
2780 * HDA mixer.
2782 KEY_RESERVED, /* 0x14: VOLUME UP */
2783 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2784 KEY_RESERVED, /* 0x16: MUTE */
2786 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2788 /* (assignments unknown, please report if found) */
2789 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2790 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2793 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2794 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2795 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2797 int res, i;
2798 int status;
2799 int hkeyv;
2801 unsigned long quirks;
2803 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2804 "initializing hotkey subdriver\n");
2806 BUG_ON(!tpacpi_inputdev);
2807 BUG_ON(tpacpi_inputdev->open != NULL ||
2808 tpacpi_inputdev->close != NULL);
2810 TPACPI_ACPIHANDLE_INIT(hkey);
2811 mutex_init(&hotkey_mutex);
2813 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2814 mutex_init(&hotkey_thread_mutex);
2815 mutex_init(&hotkey_thread_data_mutex);
2816 #endif
2818 /* hotkey not supported on 570 */
2819 tp_features.hotkey = hkey_handle != NULL;
2821 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2822 "hotkeys are %s\n",
2823 str_supported(tp_features.hotkey));
2825 if (!tp_features.hotkey)
2826 return 1;
2828 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
2829 ARRAY_SIZE(tpacpi_hotkey_qtable));
2831 tpacpi_disable_brightness_delay();
2833 /* MUST have enough space for all attributes to be added to
2834 * hotkey_dev_attributes */
2835 hotkey_dev_attributes = create_attr_set(
2836 ARRAY_SIZE(hotkey_attributes) + 2,
2837 NULL);
2838 if (!hotkey_dev_attributes)
2839 return -ENOMEM;
2840 res = add_many_to_attr_set(hotkey_dev_attributes,
2841 hotkey_attributes,
2842 ARRAY_SIZE(hotkey_attributes));
2843 if (res)
2844 goto err_exit;
2846 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
2847 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2848 for HKEY interface version 0x100 */
2849 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2850 if ((hkeyv >> 8) != 1) {
2851 printk(TPACPI_ERR "unknown version of the "
2852 "HKEY interface: 0x%x\n", hkeyv);
2853 printk(TPACPI_ERR "please report this to %s\n",
2854 TPACPI_MAIL);
2855 } else {
2857 * MHKV 0x100 in A31, R40, R40e,
2858 * T4x, X31, and later
2860 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2861 "firmware HKEY interface version: 0x%x\n",
2862 hkeyv);
2864 /* Paranoia check AND init hotkey_all_mask */
2865 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2866 "MHKA", "qd")) {
2867 printk(TPACPI_ERR
2868 "missing MHKA handler, "
2869 "please report this to %s\n",
2870 TPACPI_MAIL);
2871 /* Fallback: pre-init for FN+F3,F4,F12 */
2872 hotkey_all_mask = 0x080cU;
2873 } else {
2874 tp_features.hotkey_mask = 1;
2879 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2880 "hotkey masks are %s\n",
2881 str_supported(tp_features.hotkey_mask));
2883 /* Init hotkey_all_mask if not initialized yet */
2884 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
2885 (quirks & TPACPI_HK_Q_INIMASK))
2886 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2888 /* Init hotkey_acpi_mask and hotkey_orig_mask */
2889 if (tp_features.hotkey_mask) {
2890 /* hotkey_source_mask *must* be zero for
2891 * the first hotkey_mask_get to return hotkey_orig_mask */
2892 res = hotkey_mask_get();
2893 if (res)
2894 goto err_exit;
2896 hotkey_orig_mask = hotkey_acpi_mask;
2897 } else {
2898 hotkey_orig_mask = hotkey_all_mask;
2899 hotkey_acpi_mask = hotkey_all_mask;
2902 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2903 if (dbg_wlswemul) {
2904 tp_features.hotkey_wlsw = 1;
2905 printk(TPACPI_INFO
2906 "radio switch emulation enabled\n");
2907 } else
2908 #endif
2909 /* Not all thinkpads have a hardware radio switch */
2910 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2911 tp_features.hotkey_wlsw = 1;
2912 printk(TPACPI_INFO
2913 "radio switch found; radios are %s\n",
2914 enabled(status, 0));
2916 if (tp_features.hotkey_wlsw)
2917 res = add_to_attr_set(hotkey_dev_attributes,
2918 &dev_attr_hotkey_radio_sw.attr);
2920 /* For X41t, X60t, X61t Tablets... */
2921 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2922 tp_features.hotkey_tablet = 1;
2923 printk(TPACPI_INFO
2924 "possible tablet mode switch found; "
2925 "ThinkPad in %s mode\n",
2926 (status & TP_HOTKEY_TABLET_MASK)?
2927 "tablet" : "laptop");
2928 res = add_to_attr_set(hotkey_dev_attributes,
2929 &dev_attr_hotkey_tablet_mode.attr);
2932 if (!res)
2933 res = register_attr_set_with_sysfs(
2934 hotkey_dev_attributes,
2935 &tpacpi_pdev->dev.kobj);
2936 if (res)
2937 goto err_exit;
2939 /* Set up key map */
2941 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2942 GFP_KERNEL);
2943 if (!hotkey_keycode_map) {
2944 printk(TPACPI_ERR
2945 "failed to allocate memory for key map\n");
2946 res = -ENOMEM;
2947 goto err_exit;
2950 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2951 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2952 "using Lenovo default hot key map\n");
2953 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2954 TPACPI_HOTKEY_MAP_SIZE);
2955 } else {
2956 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2957 "using IBM default hot key map\n");
2958 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2959 TPACPI_HOTKEY_MAP_SIZE);
2962 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2963 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2964 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2965 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2966 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2967 tpacpi_inputdev->keycode = hotkey_keycode_map;
2968 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2969 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2970 set_bit(hotkey_keycode_map[i],
2971 tpacpi_inputdev->keybit);
2972 } else {
2973 if (i < sizeof(hotkey_reserved_mask)*8)
2974 hotkey_reserved_mask |= 1 << i;
2978 if (tp_features.hotkey_wlsw) {
2979 set_bit(EV_SW, tpacpi_inputdev->evbit);
2980 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2982 if (tp_features.hotkey_tablet) {
2983 set_bit(EV_SW, tpacpi_inputdev->evbit);
2984 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2987 /* Do not issue duplicate brightness change events to
2988 * userspace */
2989 if (!tp_features.bright_acpimode)
2990 /* update bright_acpimode... */
2991 tpacpi_check_std_acpi_brightness_support();
2993 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2994 printk(TPACPI_INFO
2995 "This ThinkPad has standard ACPI backlight "
2996 "brightness control, supported by the ACPI "
2997 "video driver\n");
2998 printk(TPACPI_NOTICE
2999 "Disabling thinkpad-acpi brightness events "
3000 "by default...\n");
3002 /* Disable brightness up/down on Lenovo thinkpads when
3003 * ACPI is handling them, otherwise it is plain impossible
3004 * for userspace to do something even remotely sane */
3005 hotkey_reserved_mask |=
3006 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3007 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3008 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3009 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3012 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3013 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3014 & ~hotkey_all_mask
3015 & ~hotkey_reserved_mask;
3017 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3018 "hotkey source mask 0x%08x, polling freq %u\n",
3019 hotkey_source_mask, hotkey_poll_freq);
3020 #endif
3022 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3023 "enabling firmware HKEY event interface...\n");
3024 res = hotkey_status_set(true);
3025 if (res) {
3026 hotkey_exit();
3027 return res;
3029 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3030 | hotkey_driver_mask)
3031 & ~hotkey_source_mask);
3032 if (res < 0 && res != -ENXIO) {
3033 hotkey_exit();
3034 return res;
3036 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3037 & ~hotkey_reserved_mask;
3038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3039 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3040 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3042 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3043 "legacy ibm/hotkey event reporting over procfs %s\n",
3044 (hotkey_report_mode < 2) ?
3045 "enabled" : "disabled");
3047 tpacpi_inputdev->open = &hotkey_inputdev_open;
3048 tpacpi_inputdev->close = &hotkey_inputdev_close;
3050 hotkey_poll_setup_safe(true);
3051 tpacpi_send_radiosw_update();
3052 tpacpi_input_send_tabletsw();
3054 return 0;
3056 err_exit:
3057 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3058 hotkey_dev_attributes = NULL;
3060 return (res < 0)? res : 1;
3063 static bool hotkey_notify_hotkey(const u32 hkey,
3064 bool *send_acpi_ev,
3065 bool *ignore_acpi_ev)
3067 /* 0x1000-0x1FFF: key presses */
3068 unsigned int scancode = hkey & 0xfff;
3069 *send_acpi_ev = true;
3070 *ignore_acpi_ev = false;
3072 if (scancode > 0 && scancode < 0x21) {
3073 scancode--;
3074 if (!(hotkey_source_mask & (1 << scancode))) {
3075 tpacpi_input_send_key_masked(scancode);
3076 *send_acpi_ev = false;
3077 } else {
3078 *ignore_acpi_ev = true;
3080 return true;
3082 return false;
3085 static bool hotkey_notify_wakeup(const u32 hkey,
3086 bool *send_acpi_ev,
3087 bool *ignore_acpi_ev)
3089 /* 0x2000-0x2FFF: Wakeup reason */
3090 *send_acpi_ev = true;
3091 *ignore_acpi_ev = false;
3093 switch (hkey) {
3094 case 0x2304: /* suspend, undock */
3095 case 0x2404: /* hibernation, undock */
3096 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3097 *ignore_acpi_ev = true;
3098 break;
3100 case 0x2305: /* suspend, bay eject */
3101 case 0x2405: /* hibernation, bay eject */
3102 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3103 *ignore_acpi_ev = true;
3104 break;
3106 case 0x2313: /* Battery on critical low level (S3) */
3107 case 0x2413: /* Battery on critical low level (S4) */
3108 printk(TPACPI_ALERT
3109 "EMERGENCY WAKEUP: battery almost empty\n");
3110 /* how to auto-heal: */
3111 /* 2313: woke up from S3, go to S4/S5 */
3112 /* 2413: woke up from S4, go to S5 */
3113 break;
3115 default:
3116 return false;
3119 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3120 printk(TPACPI_INFO
3121 "woke up due to a hot-unplug "
3122 "request...\n");
3123 hotkey_wakeup_reason_notify_change();
3125 return true;
3128 static bool hotkey_notify_usrevent(const u32 hkey,
3129 bool *send_acpi_ev,
3130 bool *ignore_acpi_ev)
3132 /* 0x5000-0x5FFF: human interface helpers */
3133 *send_acpi_ev = true;
3134 *ignore_acpi_ev = false;
3136 switch (hkey) {
3137 case 0x500b: /* X61t: tablet pen inserted into bay */
3138 case 0x500c: /* X61t: tablet pen removed from bay */
3139 return true;
3141 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3142 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3143 tpacpi_input_send_tabletsw();
3144 hotkey_tablet_mode_notify_change();
3145 *send_acpi_ev = false;
3146 return true;
3148 case 0x5001: /* Lid close */
3149 case 0x5002: /* Lid open */
3150 case 0x5010: /* brightness control */
3151 /* do not propagate these events */
3152 *ignore_acpi_ev = true;
3153 return true;
3155 default:
3156 return false;
3160 static bool hotkey_notify_thermal(const u32 hkey,
3161 bool *send_acpi_ev,
3162 bool *ignore_acpi_ev)
3164 /* 0x6000-0x6FFF: thermal alarms */
3165 *send_acpi_ev = true;
3166 *ignore_acpi_ev = false;
3168 switch (hkey) {
3169 case 0x6011:
3170 printk(TPACPI_CRIT
3171 "THERMAL ALARM: battery is too hot!\n");
3172 /* recommended action: warn user through gui */
3173 return true;
3174 case 0x6012:
3175 printk(TPACPI_ALERT
3176 "THERMAL EMERGENCY: battery is extremely hot!\n");
3177 /* recommended action: immediate sleep/hibernate */
3178 return true;
3179 case 0x6021:
3180 printk(TPACPI_CRIT
3181 "THERMAL ALARM: "
3182 "a sensor reports something is too hot!\n");
3183 /* recommended action: warn user through gui, that */
3184 /* some internal component is too hot */
3185 return true;
3186 case 0x6022:
3187 printk(TPACPI_ALERT
3188 "THERMAL EMERGENCY: "
3189 "a sensor reports something is extremely hot!\n");
3190 /* recommended action: immediate sleep/hibernate */
3191 return true;
3192 case 0x6030:
3193 printk(TPACPI_INFO
3194 "EC reports that Thermal Table has changed\n");
3195 /* recommended action: do nothing, we don't have
3196 * Lenovo ATM information */
3197 return true;
3198 default:
3199 printk(TPACPI_ALERT
3200 "THERMAL ALERT: unknown thermal alarm received\n");
3201 return false;
3205 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3207 u32 hkey;
3208 bool send_acpi_ev;
3209 bool ignore_acpi_ev;
3210 bool known_ev;
3212 if (event != 0x80) {
3213 printk(TPACPI_ERR
3214 "unknown HKEY notification event %d\n", event);
3215 /* forward it to userspace, maybe it knows how to handle it */
3216 acpi_bus_generate_netlink_event(
3217 ibm->acpi->device->pnp.device_class,
3218 dev_name(&ibm->acpi->device->dev),
3219 event, 0);
3220 return;
3223 while (1) {
3224 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3225 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3226 return;
3229 if (hkey == 0) {
3230 /* queue empty */
3231 return;
3234 send_acpi_ev = true;
3235 ignore_acpi_ev = false;
3237 switch (hkey >> 12) {
3238 case 1:
3239 /* 0x1000-0x1FFF: key presses */
3240 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3241 &ignore_acpi_ev);
3242 break;
3243 case 2:
3244 /* 0x2000-0x2FFF: Wakeup reason */
3245 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3246 &ignore_acpi_ev);
3247 break;
3248 case 3:
3249 /* 0x3000-0x3FFF: bay-related wakeups */
3250 if (hkey == 0x3003) {
3251 hotkey_autosleep_ack = 1;
3252 printk(TPACPI_INFO
3253 "bay ejected\n");
3254 hotkey_wakeup_hotunplug_complete_notify_change();
3255 known_ev = true;
3256 } else {
3257 known_ev = false;
3259 break;
3260 case 4:
3261 /* 0x4000-0x4FFF: dock-related wakeups */
3262 if (hkey == 0x4003) {
3263 hotkey_autosleep_ack = 1;
3264 printk(TPACPI_INFO
3265 "undocked\n");
3266 hotkey_wakeup_hotunplug_complete_notify_change();
3267 known_ev = true;
3268 } else {
3269 known_ev = false;
3271 break;
3272 case 5:
3273 /* 0x5000-0x5FFF: human interface helpers */
3274 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3275 &ignore_acpi_ev);
3276 break;
3277 case 6:
3278 /* 0x6000-0x6FFF: thermal alarms */
3279 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3280 &ignore_acpi_ev);
3281 break;
3282 case 7:
3283 /* 0x7000-0x7FFF: misc */
3284 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3285 tpacpi_send_radiosw_update();
3286 send_acpi_ev = 0;
3287 known_ev = true;
3288 break;
3290 /* fallthrough to default */
3291 default:
3292 known_ev = false;
3294 if (!known_ev) {
3295 printk(TPACPI_NOTICE
3296 "unhandled HKEY event 0x%04x\n", hkey);
3297 printk(TPACPI_NOTICE
3298 "please report the conditions when this "
3299 "event happened to %s\n", TPACPI_MAIL);
3302 /* Legacy events */
3303 if (!ignore_acpi_ev &&
3304 (send_acpi_ev || hotkey_report_mode < 2)) {
3305 acpi_bus_generate_proc_event(ibm->acpi->device,
3306 event, hkey);
3309 /* netlink events */
3310 if (!ignore_acpi_ev && send_acpi_ev) {
3311 acpi_bus_generate_netlink_event(
3312 ibm->acpi->device->pnp.device_class,
3313 dev_name(&ibm->acpi->device->dev),
3314 event, hkey);
3319 static void hotkey_suspend(pm_message_t state)
3321 /* Do these on suspend, we get the events on early resume! */
3322 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3323 hotkey_autosleep_ack = 0;
3326 static void hotkey_resume(void)
3328 tpacpi_disable_brightness_delay();
3330 if (hotkey_status_set(true) < 0 ||
3331 hotkey_mask_set(hotkey_acpi_mask) < 0)
3332 printk(TPACPI_ERR
3333 "error while attempting to reset the event "
3334 "firmware interface\n");
3336 tpacpi_send_radiosw_update();
3337 hotkey_tablet_mode_notify_change();
3338 hotkey_wakeup_reason_notify_change();
3339 hotkey_wakeup_hotunplug_complete_notify_change();
3340 hotkey_poll_setup_safe(false);
3343 /* procfs -------------------------------------------------------------- */
3344 static int hotkey_read(char *p)
3346 int res, status;
3347 int len = 0;
3349 if (!tp_features.hotkey) {
3350 len += sprintf(p + len, "status:\t\tnot supported\n");
3351 return len;
3354 if (mutex_lock_killable(&hotkey_mutex))
3355 return -ERESTARTSYS;
3356 res = hotkey_status_get(&status);
3357 if (!res)
3358 res = hotkey_mask_get();
3359 mutex_unlock(&hotkey_mutex);
3360 if (res)
3361 return res;
3363 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3364 if (hotkey_all_mask) {
3365 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_user_mask);
3366 len += sprintf(p + len,
3367 "commands:\tenable, disable, reset, <mask>\n");
3368 } else {
3369 len += sprintf(p + len, "mask:\t\tnot supported\n");
3370 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3373 return len;
3376 static void hotkey_enabledisable_warn(bool enable)
3378 tpacpi_log_usertask("procfs hotkey enable/disable");
3379 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3380 TPACPI_WARN
3381 "hotkey enable/disable functionality has been "
3382 "removed from the driver. Hotkeys are always "
3383 "enabled\n"))
3384 printk(TPACPI_ERR
3385 "Please remove the hotkey=enable module "
3386 "parameter, it is deprecated. Hotkeys are always "
3387 "enabled\n");
3390 static int hotkey_write(char *buf)
3392 int res;
3393 u32 mask;
3394 char *cmd;
3396 if (!tp_features.hotkey)
3397 return -ENODEV;
3399 if (mutex_lock_killable(&hotkey_mutex))
3400 return -ERESTARTSYS;
3402 mask = hotkey_user_mask;
3404 res = 0;
3405 while ((cmd = next_cmd(&buf))) {
3406 if (strlencmp(cmd, "enable") == 0) {
3407 hotkey_enabledisable_warn(1);
3408 } else if (strlencmp(cmd, "disable") == 0) {
3409 hotkey_enabledisable_warn(0);
3410 res = -EPERM;
3411 } else if (strlencmp(cmd, "reset") == 0) {
3412 mask = (hotkey_all_mask | hotkey_source_mask)
3413 & ~hotkey_reserved_mask;
3414 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3415 /* mask set */
3416 } else if (sscanf(cmd, "%x", &mask) == 1) {
3417 /* mask set */
3418 } else {
3419 res = -EINVAL;
3420 goto errexit;
3424 if (!res) {
3425 tpacpi_disclose_usertask("procfs hotkey",
3426 "set mask to 0x%08x\n", mask);
3427 res = hotkey_user_mask_set(mask);
3430 errexit:
3431 mutex_unlock(&hotkey_mutex);
3432 return res;
3435 static const struct acpi_device_id ibm_htk_device_ids[] = {
3436 {TPACPI_ACPI_HKEY_HID, 0},
3437 {"", 0},
3440 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3441 .hid = ibm_htk_device_ids,
3442 .notify = hotkey_notify,
3443 .handle = &hkey_handle,
3444 .type = ACPI_DEVICE_NOTIFY,
3447 static struct ibm_struct hotkey_driver_data = {
3448 .name = "hotkey",
3449 .read = hotkey_read,
3450 .write = hotkey_write,
3451 .exit = hotkey_exit,
3452 .resume = hotkey_resume,
3453 .suspend = hotkey_suspend,
3454 .acpi = &ibm_hotkey_acpidriver,
3457 /*************************************************************************
3458 * Bluetooth subdriver
3461 enum {
3462 /* ACPI GBDC/SBDC bits */
3463 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3464 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3465 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3466 off / last state */
3469 enum {
3470 /* ACPI \BLTH commands */
3471 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3472 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3473 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3474 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3475 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3478 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3480 static struct rfkill *tpacpi_bluetooth_rfkill;
3482 static void bluetooth_suspend(pm_message_t state)
3484 /* Try to make sure radio will resume powered off */
3485 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3486 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3487 vdbg_printk(TPACPI_DBG_RFKILL,
3488 "bluetooth power down on resume request failed\n");
3491 static int bluetooth_get_radiosw(void)
3493 int status;
3495 if (!tp_features.bluetooth)
3496 return -ENODEV;
3498 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3499 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3500 return RFKILL_STATE_HARD_BLOCKED;
3502 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3503 if (dbg_bluetoothemul)
3504 return (tpacpi_bluetooth_emulstate) ?
3505 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3506 #endif
3508 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3509 return -EIO;
3511 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3512 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3515 static void bluetooth_update_rfk(void)
3517 int status;
3519 if (!tpacpi_bluetooth_rfkill)
3520 return;
3522 status = bluetooth_get_radiosw();
3523 if (status < 0)
3524 return;
3525 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3527 vdbg_printk(TPACPI_DBG_RFKILL,
3528 "forced rfkill state to %d\n",
3529 status);
3532 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3534 int status;
3536 if (!tp_features.bluetooth)
3537 return -ENODEV;
3539 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3540 * reason to risk weird behaviour. */
3541 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3542 && radio_on)
3543 return -EPERM;
3545 vdbg_printk(TPACPI_DBG_RFKILL,
3546 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3548 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3549 if (dbg_bluetoothemul) {
3550 tpacpi_bluetooth_emulstate = !!radio_on;
3551 if (update_rfk)
3552 bluetooth_update_rfk();
3553 return 0;
3555 #endif
3557 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3558 if (radio_on)
3559 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3560 else
3561 status = 0;
3562 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3563 return -EIO;
3565 if (update_rfk)
3566 bluetooth_update_rfk();
3568 return 0;
3571 /* sysfs bluetooth enable ---------------------------------------------- */
3572 static ssize_t bluetooth_enable_show(struct device *dev,
3573 struct device_attribute *attr,
3574 char *buf)
3576 int status;
3578 printk_deprecated_rfkill_attribute("bluetooth_enable");
3580 status = bluetooth_get_radiosw();
3581 if (status < 0)
3582 return status;
3584 return snprintf(buf, PAGE_SIZE, "%d\n",
3585 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3588 static ssize_t bluetooth_enable_store(struct device *dev,
3589 struct device_attribute *attr,
3590 const char *buf, size_t count)
3592 unsigned long t;
3593 int res;
3595 printk_deprecated_rfkill_attribute("bluetooth_enable");
3597 if (parse_strtoul(buf, 1, &t))
3598 return -EINVAL;
3600 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3602 res = bluetooth_set_radiosw(t, 1);
3604 return (res) ? res : count;
3607 static struct device_attribute dev_attr_bluetooth_enable =
3608 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3609 bluetooth_enable_show, bluetooth_enable_store);
3611 /* --------------------------------------------------------------------- */
3613 static struct attribute *bluetooth_attributes[] = {
3614 &dev_attr_bluetooth_enable.attr,
3615 NULL
3618 static const struct attribute_group bluetooth_attr_group = {
3619 .attrs = bluetooth_attributes,
3622 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3624 int bts = bluetooth_get_radiosw();
3626 if (bts < 0)
3627 return bts;
3629 *state = bts;
3630 return 0;
3633 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3635 dbg_printk(TPACPI_DBG_RFKILL,
3636 "request to change radio state to %d\n", state);
3637 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3640 static void bluetooth_shutdown(void)
3642 /* Order firmware to save current state to NVRAM */
3643 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3644 TP_ACPI_BLTH_SAVE_STATE))
3645 printk(TPACPI_NOTICE
3646 "failed to save bluetooth state to NVRAM\n");
3647 else
3648 vdbg_printk(TPACPI_DBG_RFKILL,
3649 "bluestooth state saved to NVRAM\n");
3652 static void bluetooth_exit(void)
3654 bluetooth_shutdown();
3656 if (tpacpi_bluetooth_rfkill)
3657 rfkill_unregister(tpacpi_bluetooth_rfkill);
3659 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3660 &bluetooth_attr_group);
3663 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3665 int res;
3666 int status = 0;
3668 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3669 "initializing bluetooth subdriver\n");
3671 TPACPI_ACPIHANDLE_INIT(hkey);
3673 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3674 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3675 tp_features.bluetooth = hkey_handle &&
3676 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3678 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3679 "bluetooth is %s, status 0x%02x\n",
3680 str_supported(tp_features.bluetooth),
3681 status);
3683 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3684 if (dbg_bluetoothemul) {
3685 tp_features.bluetooth = 1;
3686 printk(TPACPI_INFO
3687 "bluetooth switch emulation enabled\n");
3688 } else
3689 #endif
3690 if (tp_features.bluetooth &&
3691 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3692 /* no bluetooth hardware present in system */
3693 tp_features.bluetooth = 0;
3694 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3695 "bluetooth hardware not installed\n");
3698 if (!tp_features.bluetooth)
3699 return 1;
3701 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3702 &bluetooth_attr_group);
3703 if (res)
3704 return res;
3706 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3707 &tpacpi_bluetooth_rfkill,
3708 RFKILL_TYPE_BLUETOOTH,
3709 TPACPI_RFK_BLUETOOTH_SW_NAME,
3710 true,
3711 tpacpi_bluetooth_rfk_set,
3712 tpacpi_bluetooth_rfk_get);
3713 if (res) {
3714 bluetooth_exit();
3715 return res;
3718 return 0;
3721 /* procfs -------------------------------------------------------------- */
3722 static int bluetooth_read(char *p)
3724 int len = 0;
3725 int status = bluetooth_get_radiosw();
3727 if (!tp_features.bluetooth)
3728 len += sprintf(p + len, "status:\t\tnot supported\n");
3729 else {
3730 len += sprintf(p + len, "status:\t\t%s\n",
3731 (status == RFKILL_STATE_UNBLOCKED) ?
3732 "enabled" : "disabled");
3733 len += sprintf(p + len, "commands:\tenable, disable\n");
3736 return len;
3739 static int bluetooth_write(char *buf)
3741 char *cmd;
3742 int state = -1;
3744 if (!tp_features.bluetooth)
3745 return -ENODEV;
3747 while ((cmd = next_cmd(&buf))) {
3748 if (strlencmp(cmd, "enable") == 0) {
3749 state = 1;
3750 } else if (strlencmp(cmd, "disable") == 0) {
3751 state = 0;
3752 } else
3753 return -EINVAL;
3756 if (state != -1) {
3757 tpacpi_disclose_usertask("procfs bluetooth",
3758 "attempt to %s\n",
3759 state ? "enable" : "disable");
3760 bluetooth_set_radiosw(state, 1);
3763 return 0;
3766 static struct ibm_struct bluetooth_driver_data = {
3767 .name = "bluetooth",
3768 .read = bluetooth_read,
3769 .write = bluetooth_write,
3770 .exit = bluetooth_exit,
3771 .suspend = bluetooth_suspend,
3772 .shutdown = bluetooth_shutdown,
3775 /*************************************************************************
3776 * Wan subdriver
3779 enum {
3780 /* ACPI GWAN/SWAN bits */
3781 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3782 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3783 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3784 off / last state */
3787 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3789 static struct rfkill *tpacpi_wan_rfkill;
3791 static void wan_suspend(pm_message_t state)
3793 /* Try to make sure radio will resume powered off */
3794 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3795 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3796 vdbg_printk(TPACPI_DBG_RFKILL,
3797 "WWAN power down on resume request failed\n");
3800 static int wan_get_radiosw(void)
3802 int status;
3804 if (!tp_features.wan)
3805 return -ENODEV;
3807 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3808 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3809 return RFKILL_STATE_HARD_BLOCKED;
3811 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3812 if (dbg_wwanemul)
3813 return (tpacpi_wwan_emulstate) ?
3814 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3815 #endif
3817 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3818 return -EIO;
3820 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3821 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3824 static void wan_update_rfk(void)
3826 int status;
3828 if (!tpacpi_wan_rfkill)
3829 return;
3831 status = wan_get_radiosw();
3832 if (status < 0)
3833 return;
3834 rfkill_force_state(tpacpi_wan_rfkill, status);
3836 vdbg_printk(TPACPI_DBG_RFKILL,
3837 "forced rfkill state to %d\n",
3838 status);
3841 static int wan_set_radiosw(int radio_on, int update_rfk)
3843 int status;
3845 if (!tp_features.wan)
3846 return -ENODEV;
3848 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3849 * reason to risk weird behaviour. */
3850 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3851 && radio_on)
3852 return -EPERM;
3854 vdbg_printk(TPACPI_DBG_RFKILL,
3855 "will %s WWAN\n", radio_on ? "enable" : "disable");
3857 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3858 if (dbg_wwanemul) {
3859 tpacpi_wwan_emulstate = !!radio_on;
3860 if (update_rfk)
3861 wan_update_rfk();
3862 return 0;
3864 #endif
3866 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3867 if (radio_on)
3868 status = TP_ACPI_WANCARD_RADIOSSW;
3869 else
3870 status = 0;
3871 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3872 return -EIO;
3874 if (update_rfk)
3875 wan_update_rfk();
3877 return 0;
3880 /* sysfs wan enable ---------------------------------------------------- */
3881 static ssize_t wan_enable_show(struct device *dev,
3882 struct device_attribute *attr,
3883 char *buf)
3885 int status;
3887 printk_deprecated_rfkill_attribute("wwan_enable");
3889 status = wan_get_radiosw();
3890 if (status < 0)
3891 return status;
3893 return snprintf(buf, PAGE_SIZE, "%d\n",
3894 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3897 static ssize_t wan_enable_store(struct device *dev,
3898 struct device_attribute *attr,
3899 const char *buf, size_t count)
3901 unsigned long t;
3902 int res;
3904 printk_deprecated_rfkill_attribute("wwan_enable");
3906 if (parse_strtoul(buf, 1, &t))
3907 return -EINVAL;
3909 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3911 res = wan_set_radiosw(t, 1);
3913 return (res) ? res : count;
3916 static struct device_attribute dev_attr_wan_enable =
3917 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3918 wan_enable_show, wan_enable_store);
3920 /* --------------------------------------------------------------------- */
3922 static struct attribute *wan_attributes[] = {
3923 &dev_attr_wan_enable.attr,
3924 NULL
3927 static const struct attribute_group wan_attr_group = {
3928 .attrs = wan_attributes,
3931 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3933 int wans = wan_get_radiosw();
3935 if (wans < 0)
3936 return wans;
3938 *state = wans;
3939 return 0;
3942 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3944 dbg_printk(TPACPI_DBG_RFKILL,
3945 "request to change radio state to %d\n", state);
3946 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3949 static void wan_shutdown(void)
3951 /* Order firmware to save current state to NVRAM */
3952 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3953 TP_ACPI_WGSV_SAVE_STATE))
3954 printk(TPACPI_NOTICE
3955 "failed to save WWAN state to NVRAM\n");
3956 else
3957 vdbg_printk(TPACPI_DBG_RFKILL,
3958 "WWAN state saved to NVRAM\n");
3961 static void wan_exit(void)
3963 wan_shutdown();
3965 if (tpacpi_wan_rfkill)
3966 rfkill_unregister(tpacpi_wan_rfkill);
3968 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3969 &wan_attr_group);
3972 static int __init wan_init(struct ibm_init_struct *iibm)
3974 int res;
3975 int status = 0;
3977 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3978 "initializing wan subdriver\n");
3980 TPACPI_ACPIHANDLE_INIT(hkey);
3982 tp_features.wan = hkey_handle &&
3983 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3985 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3986 "wan is %s, status 0x%02x\n",
3987 str_supported(tp_features.wan),
3988 status);
3990 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3991 if (dbg_wwanemul) {
3992 tp_features.wan = 1;
3993 printk(TPACPI_INFO
3994 "wwan switch emulation enabled\n");
3995 } else
3996 #endif
3997 if (tp_features.wan &&
3998 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3999 /* no wan hardware present in system */
4000 tp_features.wan = 0;
4001 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4002 "wan hardware not installed\n");
4005 if (!tp_features.wan)
4006 return 1;
4008 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4009 &wan_attr_group);
4010 if (res)
4011 return res;
4013 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4014 &tpacpi_wan_rfkill,
4015 RFKILL_TYPE_WWAN,
4016 TPACPI_RFK_WWAN_SW_NAME,
4017 true,
4018 tpacpi_wan_rfk_set,
4019 tpacpi_wan_rfk_get);
4020 if (res) {
4021 wan_exit();
4022 return res;
4025 return 0;
4028 /* procfs -------------------------------------------------------------- */
4029 static int wan_read(char *p)
4031 int len = 0;
4032 int status = wan_get_radiosw();
4034 tpacpi_disclose_usertask("procfs wan", "read");
4036 if (!tp_features.wan)
4037 len += sprintf(p + len, "status:\t\tnot supported\n");
4038 else {
4039 len += sprintf(p + len, "status:\t\t%s\n",
4040 (status == RFKILL_STATE_UNBLOCKED) ?
4041 "enabled" : "disabled");
4042 len += sprintf(p + len, "commands:\tenable, disable\n");
4045 return len;
4048 static int wan_write(char *buf)
4050 char *cmd;
4051 int state = -1;
4053 if (!tp_features.wan)
4054 return -ENODEV;
4056 while ((cmd = next_cmd(&buf))) {
4057 if (strlencmp(cmd, "enable") == 0) {
4058 state = 1;
4059 } else if (strlencmp(cmd, "disable") == 0) {
4060 state = 0;
4061 } else
4062 return -EINVAL;
4065 if (state != -1) {
4066 tpacpi_disclose_usertask("procfs wan",
4067 "attempt to %s\n",
4068 state ? "enable" : "disable");
4069 wan_set_radiosw(state, 1);
4072 return 0;
4075 static struct ibm_struct wan_driver_data = {
4076 .name = "wan",
4077 .read = wan_read,
4078 .write = wan_write,
4079 .exit = wan_exit,
4080 .suspend = wan_suspend,
4081 .shutdown = wan_shutdown,
4084 /*************************************************************************
4085 * UWB subdriver
4088 enum {
4089 /* ACPI GUWB/SUWB bits */
4090 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4091 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4094 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4096 static struct rfkill *tpacpi_uwb_rfkill;
4098 static int uwb_get_radiosw(void)
4100 int status;
4102 if (!tp_features.uwb)
4103 return -ENODEV;
4105 /* WLSW overrides UWB in firmware/hardware, reflect that */
4106 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
4107 return RFKILL_STATE_HARD_BLOCKED;
4109 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4110 if (dbg_uwbemul)
4111 return (tpacpi_uwb_emulstate) ?
4112 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4113 #endif
4115 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4116 return -EIO;
4118 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4119 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
4122 static void uwb_update_rfk(void)
4124 int status;
4126 if (!tpacpi_uwb_rfkill)
4127 return;
4129 status = uwb_get_radiosw();
4130 if (status < 0)
4131 return;
4132 rfkill_force_state(tpacpi_uwb_rfkill, status);
4134 vdbg_printk(TPACPI_DBG_RFKILL,
4135 "forced rfkill state to %d\n",
4136 status);
4139 static int uwb_set_radiosw(int radio_on, int update_rfk)
4141 int status;
4143 if (!tp_features.uwb)
4144 return -ENODEV;
4146 /* WLSW overrides UWB in firmware/hardware, but there is no
4147 * reason to risk weird behaviour. */
4148 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4149 && radio_on)
4150 return -EPERM;
4152 vdbg_printk(TPACPI_DBG_RFKILL,
4153 "will %s UWB\n", radio_on ? "enable" : "disable");
4155 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4156 if (dbg_uwbemul) {
4157 tpacpi_uwb_emulstate = !!radio_on;
4158 if (update_rfk)
4159 uwb_update_rfk();
4160 return 0;
4162 #endif
4164 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4165 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4166 return -EIO;
4168 if (update_rfk)
4169 uwb_update_rfk();
4171 return 0;
4174 /* --------------------------------------------------------------------- */
4176 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4178 int uwbs = uwb_get_radiosw();
4180 if (uwbs < 0)
4181 return uwbs;
4183 *state = uwbs;
4184 return 0;
4187 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4189 dbg_printk(TPACPI_DBG_RFKILL,
4190 "request to change radio state to %d\n", state);
4191 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4194 static void uwb_exit(void)
4196 if (tpacpi_uwb_rfkill)
4197 rfkill_unregister(tpacpi_uwb_rfkill);
4200 static int __init uwb_init(struct ibm_init_struct *iibm)
4202 int res;
4203 int status = 0;
4205 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4206 "initializing uwb subdriver\n");
4208 TPACPI_ACPIHANDLE_INIT(hkey);
4210 tp_features.uwb = hkey_handle &&
4211 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4213 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4214 "uwb is %s, status 0x%02x\n",
4215 str_supported(tp_features.uwb),
4216 status);
4218 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4219 if (dbg_uwbemul) {
4220 tp_features.uwb = 1;
4221 printk(TPACPI_INFO
4222 "uwb switch emulation enabled\n");
4223 } else
4224 #endif
4225 if (tp_features.uwb &&
4226 !(status & TP_ACPI_UWB_HWPRESENT)) {
4227 /* no uwb hardware present in system */
4228 tp_features.uwb = 0;
4229 dbg_printk(TPACPI_DBG_INIT,
4230 "uwb hardware not installed\n");
4233 if (!tp_features.uwb)
4234 return 1;
4236 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4237 &tpacpi_uwb_rfkill,
4238 RFKILL_TYPE_UWB,
4239 TPACPI_RFK_UWB_SW_NAME,
4240 false,
4241 tpacpi_uwb_rfk_set,
4242 tpacpi_uwb_rfk_get);
4244 return res;
4247 static struct ibm_struct uwb_driver_data = {
4248 .name = "uwb",
4249 .exit = uwb_exit,
4250 .flags.experimental = 1,
4253 /*************************************************************************
4254 * Video subdriver
4257 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4259 enum video_access_mode {
4260 TPACPI_VIDEO_NONE = 0,
4261 TPACPI_VIDEO_570, /* 570 */
4262 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4263 TPACPI_VIDEO_NEW, /* all others */
4266 enum { /* video status flags, based on VIDEO_570 */
4267 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4268 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4269 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4272 enum { /* TPACPI_VIDEO_570 constants */
4273 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4274 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4275 * video_status_flags */
4276 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4277 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4280 static enum video_access_mode video_supported;
4281 static int video_orig_autosw;
4283 static int video_autosw_get(void);
4284 static int video_autosw_set(int enable);
4286 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4288 static int __init video_init(struct ibm_init_struct *iibm)
4290 int ivga;
4292 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4294 TPACPI_ACPIHANDLE_INIT(vid);
4295 TPACPI_ACPIHANDLE_INIT(vid2);
4297 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4298 /* G41, assume IVGA doesn't change */
4299 vid_handle = vid2_handle;
4301 if (!vid_handle)
4302 /* video switching not supported on R30, R31 */
4303 video_supported = TPACPI_VIDEO_NONE;
4304 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4305 /* 570 */
4306 video_supported = TPACPI_VIDEO_570;
4307 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4308 /* 600e/x, 770e, 770x */
4309 video_supported = TPACPI_VIDEO_770;
4310 else
4311 /* all others */
4312 video_supported = TPACPI_VIDEO_NEW;
4314 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4315 str_supported(video_supported != TPACPI_VIDEO_NONE),
4316 video_supported);
4318 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4321 static void video_exit(void)
4323 dbg_printk(TPACPI_DBG_EXIT,
4324 "restoring original video autoswitch mode\n");
4325 if (video_autosw_set(video_orig_autosw))
4326 printk(TPACPI_ERR "error while trying to restore original "
4327 "video autoswitch mode\n");
4330 static int video_outputsw_get(void)
4332 int status = 0;
4333 int i;
4335 switch (video_supported) {
4336 case TPACPI_VIDEO_570:
4337 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4338 TP_ACPI_VIDEO_570_PHSCMD))
4339 return -EIO;
4340 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4341 break;
4342 case TPACPI_VIDEO_770:
4343 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4344 return -EIO;
4345 if (i)
4346 status |= TP_ACPI_VIDEO_S_LCD;
4347 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4348 return -EIO;
4349 if (i)
4350 status |= TP_ACPI_VIDEO_S_CRT;
4351 break;
4352 case TPACPI_VIDEO_NEW:
4353 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4354 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4355 return -EIO;
4356 if (i)
4357 status |= TP_ACPI_VIDEO_S_CRT;
4359 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4360 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4361 return -EIO;
4362 if (i)
4363 status |= TP_ACPI_VIDEO_S_LCD;
4364 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4365 return -EIO;
4366 if (i)
4367 status |= TP_ACPI_VIDEO_S_DVI;
4368 break;
4369 default:
4370 return -ENOSYS;
4373 return status;
4376 static int video_outputsw_set(int status)
4378 int autosw;
4379 int res = 0;
4381 switch (video_supported) {
4382 case TPACPI_VIDEO_570:
4383 res = acpi_evalf(NULL, NULL,
4384 "\\_SB.PHS2", "vdd",
4385 TP_ACPI_VIDEO_570_PHS2CMD,
4386 status | TP_ACPI_VIDEO_570_PHS2SET);
4387 break;
4388 case TPACPI_VIDEO_770:
4389 autosw = video_autosw_get();
4390 if (autosw < 0)
4391 return autosw;
4393 res = video_autosw_set(1);
4394 if (res)
4395 return res;
4396 res = acpi_evalf(vid_handle, NULL,
4397 "ASWT", "vdd", status * 0x100, 0);
4398 if (!autosw && video_autosw_set(autosw)) {
4399 printk(TPACPI_ERR
4400 "video auto-switch left enabled due to error\n");
4401 return -EIO;
4403 break;
4404 case TPACPI_VIDEO_NEW:
4405 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4406 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4407 break;
4408 default:
4409 return -ENOSYS;
4412 return (res)? 0 : -EIO;
4415 static int video_autosw_get(void)
4417 int autosw = 0;
4419 switch (video_supported) {
4420 case TPACPI_VIDEO_570:
4421 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4422 return -EIO;
4423 break;
4424 case TPACPI_VIDEO_770:
4425 case TPACPI_VIDEO_NEW:
4426 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4427 return -EIO;
4428 break;
4429 default:
4430 return -ENOSYS;
4433 return autosw & 1;
4436 static int video_autosw_set(int enable)
4438 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4439 return -EIO;
4440 return 0;
4443 static int video_outputsw_cycle(void)
4445 int autosw = video_autosw_get();
4446 int res;
4448 if (autosw < 0)
4449 return autosw;
4451 switch (video_supported) {
4452 case TPACPI_VIDEO_570:
4453 res = video_autosw_set(1);
4454 if (res)
4455 return res;
4456 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4457 break;
4458 case TPACPI_VIDEO_770:
4459 case TPACPI_VIDEO_NEW:
4460 res = video_autosw_set(1);
4461 if (res)
4462 return res;
4463 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4464 break;
4465 default:
4466 return -ENOSYS;
4468 if (!autosw && video_autosw_set(autosw)) {
4469 printk(TPACPI_ERR
4470 "video auto-switch left enabled due to error\n");
4471 return -EIO;
4474 return (res)? 0 : -EIO;
4477 static int video_expand_toggle(void)
4479 switch (video_supported) {
4480 case TPACPI_VIDEO_570:
4481 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4482 0 : -EIO;
4483 case TPACPI_VIDEO_770:
4484 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4485 0 : -EIO;
4486 case TPACPI_VIDEO_NEW:
4487 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4488 0 : -EIO;
4489 default:
4490 return -ENOSYS;
4492 /* not reached */
4495 static int video_read(char *p)
4497 int status, autosw;
4498 int len = 0;
4500 if (video_supported == TPACPI_VIDEO_NONE) {
4501 len += sprintf(p + len, "status:\t\tnot supported\n");
4502 return len;
4505 status = video_outputsw_get();
4506 if (status < 0)
4507 return status;
4509 autosw = video_autosw_get();
4510 if (autosw < 0)
4511 return autosw;
4513 len += sprintf(p + len, "status:\t\tsupported\n");
4514 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4515 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4516 if (video_supported == TPACPI_VIDEO_NEW)
4517 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4518 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4519 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4520 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4521 if (video_supported == TPACPI_VIDEO_NEW)
4522 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4523 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4524 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4526 return len;
4529 static int video_write(char *buf)
4531 char *cmd;
4532 int enable, disable, status;
4533 int res;
4535 if (video_supported == TPACPI_VIDEO_NONE)
4536 return -ENODEV;
4538 enable = 0;
4539 disable = 0;
4541 while ((cmd = next_cmd(&buf))) {
4542 if (strlencmp(cmd, "lcd_enable") == 0) {
4543 enable |= TP_ACPI_VIDEO_S_LCD;
4544 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4545 disable |= TP_ACPI_VIDEO_S_LCD;
4546 } else if (strlencmp(cmd, "crt_enable") == 0) {
4547 enable |= TP_ACPI_VIDEO_S_CRT;
4548 } else if (strlencmp(cmd, "crt_disable") == 0) {
4549 disable |= TP_ACPI_VIDEO_S_CRT;
4550 } else if (video_supported == TPACPI_VIDEO_NEW &&
4551 strlencmp(cmd, "dvi_enable") == 0) {
4552 enable |= TP_ACPI_VIDEO_S_DVI;
4553 } else if (video_supported == TPACPI_VIDEO_NEW &&
4554 strlencmp(cmd, "dvi_disable") == 0) {
4555 disable |= TP_ACPI_VIDEO_S_DVI;
4556 } else if (strlencmp(cmd, "auto_enable") == 0) {
4557 res = video_autosw_set(1);
4558 if (res)
4559 return res;
4560 } else if (strlencmp(cmd, "auto_disable") == 0) {
4561 res = video_autosw_set(0);
4562 if (res)
4563 return res;
4564 } else if (strlencmp(cmd, "video_switch") == 0) {
4565 res = video_outputsw_cycle();
4566 if (res)
4567 return res;
4568 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4569 res = video_expand_toggle();
4570 if (res)
4571 return res;
4572 } else
4573 return -EINVAL;
4576 if (enable || disable) {
4577 status = video_outputsw_get();
4578 if (status < 0)
4579 return status;
4580 res = video_outputsw_set((status & ~disable) | enable);
4581 if (res)
4582 return res;
4585 return 0;
4588 static struct ibm_struct video_driver_data = {
4589 .name = "video",
4590 .read = video_read,
4591 .write = video_write,
4592 .exit = video_exit,
4595 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4597 /*************************************************************************
4598 * Light (thinklight) subdriver
4601 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4602 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4604 static int light_get_status(void)
4606 int status = 0;
4608 if (tp_features.light_status) {
4609 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4610 return -EIO;
4611 return (!!status);
4614 return -ENXIO;
4617 static int light_set_status(int status)
4619 int rc;
4621 if (tp_features.light) {
4622 if (cmos_handle) {
4623 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4624 (status)?
4625 TP_CMOS_THINKLIGHT_ON :
4626 TP_CMOS_THINKLIGHT_OFF);
4627 } else {
4628 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4629 (status)? 1 : 0);
4631 return (rc)? 0 : -EIO;
4634 return -ENXIO;
4637 static void light_set_status_worker(struct work_struct *work)
4639 struct tpacpi_led_classdev *data =
4640 container_of(work, struct tpacpi_led_classdev, work);
4642 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4643 light_set_status((data->new_state != TPACPI_LED_OFF));
4646 static void light_sysfs_set(struct led_classdev *led_cdev,
4647 enum led_brightness brightness)
4649 struct tpacpi_led_classdev *data =
4650 container_of(led_cdev,
4651 struct tpacpi_led_classdev,
4652 led_classdev);
4653 data->new_state = (brightness != LED_OFF) ?
4654 TPACPI_LED_ON : TPACPI_LED_OFF;
4655 queue_work(tpacpi_wq, &data->work);
4658 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4660 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4663 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4664 .led_classdev = {
4665 .name = "tpacpi::thinklight",
4666 .brightness_set = &light_sysfs_set,
4667 .brightness_get = &light_sysfs_get,
4671 static int __init light_init(struct ibm_init_struct *iibm)
4673 int rc;
4675 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4677 TPACPI_ACPIHANDLE_INIT(ledb);
4678 TPACPI_ACPIHANDLE_INIT(lght);
4679 TPACPI_ACPIHANDLE_INIT(cmos);
4680 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4682 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4683 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4685 if (tp_features.light)
4686 /* light status not supported on
4687 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4688 tp_features.light_status =
4689 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4691 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4692 str_supported(tp_features.light),
4693 str_supported(tp_features.light_status));
4695 if (!tp_features.light)
4696 return 1;
4698 rc = led_classdev_register(&tpacpi_pdev->dev,
4699 &tpacpi_led_thinklight.led_classdev);
4701 if (rc < 0) {
4702 tp_features.light = 0;
4703 tp_features.light_status = 0;
4704 } else {
4705 rc = 0;
4708 return rc;
4711 static void light_exit(void)
4713 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4714 if (work_pending(&tpacpi_led_thinklight.work))
4715 flush_workqueue(tpacpi_wq);
4718 static int light_read(char *p)
4720 int len = 0;
4721 int status;
4723 if (!tp_features.light) {
4724 len += sprintf(p + len, "status:\t\tnot supported\n");
4725 } else if (!tp_features.light_status) {
4726 len += sprintf(p + len, "status:\t\tunknown\n");
4727 len += sprintf(p + len, "commands:\ton, off\n");
4728 } else {
4729 status = light_get_status();
4730 if (status < 0)
4731 return status;
4732 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4733 len += sprintf(p + len, "commands:\ton, off\n");
4736 return len;
4739 static int light_write(char *buf)
4741 char *cmd;
4742 int newstatus = 0;
4744 if (!tp_features.light)
4745 return -ENODEV;
4747 while ((cmd = next_cmd(&buf))) {
4748 if (strlencmp(cmd, "on") == 0) {
4749 newstatus = 1;
4750 } else if (strlencmp(cmd, "off") == 0) {
4751 newstatus = 0;
4752 } else
4753 return -EINVAL;
4756 return light_set_status(newstatus);
4759 static struct ibm_struct light_driver_data = {
4760 .name = "light",
4761 .read = light_read,
4762 .write = light_write,
4763 .exit = light_exit,
4766 /*************************************************************************
4767 * CMOS subdriver
4770 /* sysfs cmos_command -------------------------------------------------- */
4771 static ssize_t cmos_command_store(struct device *dev,
4772 struct device_attribute *attr,
4773 const char *buf, size_t count)
4775 unsigned long cmos_cmd;
4776 int res;
4778 if (parse_strtoul(buf, 21, &cmos_cmd))
4779 return -EINVAL;
4781 res = issue_thinkpad_cmos_command(cmos_cmd);
4782 return (res)? res : count;
4785 static struct device_attribute dev_attr_cmos_command =
4786 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4788 /* --------------------------------------------------------------------- */
4790 static int __init cmos_init(struct ibm_init_struct *iibm)
4792 int res;
4794 vdbg_printk(TPACPI_DBG_INIT,
4795 "initializing cmos commands subdriver\n");
4797 TPACPI_ACPIHANDLE_INIT(cmos);
4799 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4800 str_supported(cmos_handle != NULL));
4802 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4803 if (res)
4804 return res;
4806 return (cmos_handle)? 0 : 1;
4809 static void cmos_exit(void)
4811 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4814 static int cmos_read(char *p)
4816 int len = 0;
4818 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4819 R30, R31, T20-22, X20-21 */
4820 if (!cmos_handle)
4821 len += sprintf(p + len, "status:\t\tnot supported\n");
4822 else {
4823 len += sprintf(p + len, "status:\t\tsupported\n");
4824 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4827 return len;
4830 static int cmos_write(char *buf)
4832 char *cmd;
4833 int cmos_cmd, res;
4835 while ((cmd = next_cmd(&buf))) {
4836 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4837 cmos_cmd >= 0 && cmos_cmd <= 21) {
4838 /* cmos_cmd set */
4839 } else
4840 return -EINVAL;
4842 res = issue_thinkpad_cmos_command(cmos_cmd);
4843 if (res)
4844 return res;
4847 return 0;
4850 static struct ibm_struct cmos_driver_data = {
4851 .name = "cmos",
4852 .read = cmos_read,
4853 .write = cmos_write,
4854 .exit = cmos_exit,
4857 /*************************************************************************
4858 * LED subdriver
4861 enum led_access_mode {
4862 TPACPI_LED_NONE = 0,
4863 TPACPI_LED_570, /* 570 */
4864 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4865 TPACPI_LED_NEW, /* all others */
4868 enum { /* For TPACPI_LED_OLD */
4869 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4870 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4871 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4874 static enum led_access_mode led_supported;
4876 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4877 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4878 /* T20-22, X20-21 */
4879 "LED", /* all others */
4880 ); /* R30, R31 */
4882 #define TPACPI_LED_NUMLEDS 16
4883 static struct tpacpi_led_classdev *tpacpi_leds;
4884 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4885 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4886 /* there's a limit of 19 chars + NULL before 2.6.26 */
4887 "tpacpi::power",
4888 "tpacpi:orange:batt",
4889 "tpacpi:green:batt",
4890 "tpacpi::dock_active",
4891 "tpacpi::bay_active",
4892 "tpacpi::dock_batt",
4893 "tpacpi::unknown_led",
4894 "tpacpi::standby",
4895 "tpacpi::dock_status1",
4896 "tpacpi::dock_status2",
4897 "tpacpi::unknown_led2",
4898 "tpacpi::unknown_led3",
4899 "tpacpi::thinkvantage",
4901 #define TPACPI_SAFE_LEDS 0x1081U
4903 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4905 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4906 return false;
4907 #else
4908 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4909 #endif
4912 static int led_get_status(const unsigned int led)
4914 int status;
4915 enum led_status_t led_s;
4917 switch (led_supported) {
4918 case TPACPI_LED_570:
4919 if (!acpi_evalf(ec_handle,
4920 &status, "GLED", "dd", 1 << led))
4921 return -EIO;
4922 led_s = (status == 0)?
4923 TPACPI_LED_OFF :
4924 ((status == 1)?
4925 TPACPI_LED_ON :
4926 TPACPI_LED_BLINK);
4927 tpacpi_led_state_cache[led] = led_s;
4928 return led_s;
4929 default:
4930 return -ENXIO;
4933 /* not reached */
4936 static int led_set_status(const unsigned int led,
4937 const enum led_status_t ledstatus)
4939 /* off, on, blink. Index is led_status_t */
4940 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4941 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4943 int rc = 0;
4945 switch (led_supported) {
4946 case TPACPI_LED_570:
4947 /* 570 */
4948 if (unlikely(led > 7))
4949 return -EINVAL;
4950 if (unlikely(tpacpi_is_led_restricted(led)))
4951 return -EPERM;
4952 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4953 (1 << led), led_sled_arg1[ledstatus]))
4954 rc = -EIO;
4955 break;
4956 case TPACPI_LED_OLD:
4957 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4958 if (unlikely(led > 7))
4959 return -EINVAL;
4960 if (unlikely(tpacpi_is_led_restricted(led)))
4961 return -EPERM;
4962 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4963 if (rc >= 0)
4964 rc = ec_write(TPACPI_LED_EC_HLBL,
4965 (ledstatus == TPACPI_LED_BLINK) << led);
4966 if (rc >= 0)
4967 rc = ec_write(TPACPI_LED_EC_HLCL,
4968 (ledstatus != TPACPI_LED_OFF) << led);
4969 break;
4970 case TPACPI_LED_NEW:
4971 /* all others */
4972 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4973 return -EINVAL;
4974 if (unlikely(tpacpi_is_led_restricted(led)))
4975 return -EPERM;
4976 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4977 led, led_led_arg1[ledstatus]))
4978 rc = -EIO;
4979 break;
4980 default:
4981 rc = -ENXIO;
4984 if (!rc)
4985 tpacpi_led_state_cache[led] = ledstatus;
4987 return rc;
4990 static void led_set_status_worker(struct work_struct *work)
4992 struct tpacpi_led_classdev *data =
4993 container_of(work, struct tpacpi_led_classdev, work);
4995 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4996 led_set_status(data->led, data->new_state);
4999 static void led_sysfs_set(struct led_classdev *led_cdev,
5000 enum led_brightness brightness)
5002 struct tpacpi_led_classdev *data = container_of(led_cdev,
5003 struct tpacpi_led_classdev, led_classdev);
5005 if (brightness == LED_OFF)
5006 data->new_state = TPACPI_LED_OFF;
5007 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5008 data->new_state = TPACPI_LED_ON;
5009 else
5010 data->new_state = TPACPI_LED_BLINK;
5012 queue_work(tpacpi_wq, &data->work);
5015 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5016 unsigned long *delay_on, unsigned long *delay_off)
5018 struct tpacpi_led_classdev *data = container_of(led_cdev,
5019 struct tpacpi_led_classdev, led_classdev);
5021 /* Can we choose the flash rate? */
5022 if (*delay_on == 0 && *delay_off == 0) {
5023 /* yes. set them to the hardware blink rate (1 Hz) */
5024 *delay_on = 500; /* ms */
5025 *delay_off = 500; /* ms */
5026 } else if ((*delay_on != 500) || (*delay_off != 500))
5027 return -EINVAL;
5029 data->new_state = TPACPI_LED_BLINK;
5030 queue_work(tpacpi_wq, &data->work);
5032 return 0;
5035 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5037 int rc;
5039 struct tpacpi_led_classdev *data = container_of(led_cdev,
5040 struct tpacpi_led_classdev, led_classdev);
5042 rc = led_get_status(data->led);
5044 if (rc == TPACPI_LED_OFF || rc < 0)
5045 rc = LED_OFF; /* no error handling in led class :( */
5046 else
5047 rc = LED_FULL;
5049 return rc;
5052 static void led_exit(void)
5054 unsigned int i;
5056 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5057 if (tpacpi_leds[i].led_classdev.name)
5058 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5061 kfree(tpacpi_leds);
5064 static int __init tpacpi_init_led(unsigned int led)
5066 int rc;
5068 tpacpi_leds[led].led = led;
5070 /* LEDs with no name don't get registered */
5071 if (!tpacpi_led_names[led])
5072 return 0;
5074 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5075 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5076 if (led_supported == TPACPI_LED_570)
5077 tpacpi_leds[led].led_classdev.brightness_get =
5078 &led_sysfs_get;
5080 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5082 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5084 rc = led_classdev_register(&tpacpi_pdev->dev,
5085 &tpacpi_leds[led].led_classdev);
5086 if (rc < 0)
5087 tpacpi_leds[led].led_classdev.name = NULL;
5089 return rc;
5092 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5093 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5094 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5095 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5097 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5098 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5099 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5100 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5101 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5102 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5103 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5104 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5106 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5107 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5108 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5109 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5110 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5112 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5113 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5114 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5115 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5117 /* (1) - may have excess leds enabled on MSB */
5119 /* Defaults (order matters, keep last, don't reorder!) */
5120 { /* Lenovo */
5121 .vendor = PCI_VENDOR_ID_LENOVO,
5122 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5123 .quirks = 0x1fffU,
5125 { /* IBM ThinkPads with no EC version string */
5126 .vendor = PCI_VENDOR_ID_IBM,
5127 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5128 .quirks = 0x00ffU,
5130 { /* IBM ThinkPads with EC version string */
5131 .vendor = PCI_VENDOR_ID_IBM,
5132 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5133 .quirks = 0x00bfU,
5137 #undef TPACPI_LEDQ_IBM
5138 #undef TPACPI_LEDQ_LNV
5140 static int __init led_init(struct ibm_init_struct *iibm)
5142 unsigned int i;
5143 int rc;
5144 unsigned long useful_leds;
5146 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5148 TPACPI_ACPIHANDLE_INIT(led);
5150 if (!led_handle)
5151 /* led not supported on R30, R31 */
5152 led_supported = TPACPI_LED_NONE;
5153 else if (strlencmp(led_path, "SLED") == 0)
5154 /* 570 */
5155 led_supported = TPACPI_LED_570;
5156 else if (strlencmp(led_path, "SYSL") == 0)
5157 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5158 led_supported = TPACPI_LED_OLD;
5159 else
5160 /* all others */
5161 led_supported = TPACPI_LED_NEW;
5163 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5164 str_supported(led_supported), led_supported);
5166 if (led_supported == TPACPI_LED_NONE)
5167 return 1;
5169 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5170 GFP_KERNEL);
5171 if (!tpacpi_leds) {
5172 printk(TPACPI_ERR "Out of memory for LED data\n");
5173 return -ENOMEM;
5176 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5177 ARRAY_SIZE(led_useful_qtable));
5179 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5180 if (!tpacpi_is_led_restricted(i) &&
5181 test_bit(i, &useful_leds)) {
5182 rc = tpacpi_init_led(i);
5183 if (rc < 0) {
5184 led_exit();
5185 return rc;
5190 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5191 printk(TPACPI_NOTICE
5192 "warning: userspace override of important "
5193 "firmware LEDs is enabled\n");
5194 #endif
5195 return 0;
5198 #define str_led_status(s) \
5199 ((s) == TPACPI_LED_OFF ? "off" : \
5200 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5202 static int led_read(char *p)
5204 int len = 0;
5206 if (!led_supported) {
5207 len += sprintf(p + len, "status:\t\tnot supported\n");
5208 return len;
5210 len += sprintf(p + len, "status:\t\tsupported\n");
5212 if (led_supported == TPACPI_LED_570) {
5213 /* 570 */
5214 int i, status;
5215 for (i = 0; i < 8; i++) {
5216 status = led_get_status(i);
5217 if (status < 0)
5218 return -EIO;
5219 len += sprintf(p + len, "%d:\t\t%s\n",
5220 i, str_led_status(status));
5224 len += sprintf(p + len, "commands:\t"
5225 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5227 return len;
5230 static int led_write(char *buf)
5232 char *cmd;
5233 int led, rc;
5234 enum led_status_t s;
5236 if (!led_supported)
5237 return -ENODEV;
5239 while ((cmd = next_cmd(&buf))) {
5240 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5241 return -EINVAL;
5243 if (strstr(cmd, "off")) {
5244 s = TPACPI_LED_OFF;
5245 } else if (strstr(cmd, "on")) {
5246 s = TPACPI_LED_ON;
5247 } else if (strstr(cmd, "blink")) {
5248 s = TPACPI_LED_BLINK;
5249 } else {
5250 return -EINVAL;
5253 rc = led_set_status(led, s);
5254 if (rc < 0)
5255 return rc;
5258 return 0;
5261 static struct ibm_struct led_driver_data = {
5262 .name = "led",
5263 .read = led_read,
5264 .write = led_write,
5265 .exit = led_exit,
5268 /*************************************************************************
5269 * Beep subdriver
5272 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5274 #define TPACPI_BEEP_Q1 0x0001
5276 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5277 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5278 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5281 static int __init beep_init(struct ibm_init_struct *iibm)
5283 unsigned long quirks;
5285 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5287 TPACPI_ACPIHANDLE_INIT(beep);
5289 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5290 str_supported(beep_handle != NULL));
5292 quirks = tpacpi_check_quirks(beep_quirk_table,
5293 ARRAY_SIZE(beep_quirk_table));
5295 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5297 return (beep_handle)? 0 : 1;
5300 static int beep_read(char *p)
5302 int len = 0;
5304 if (!beep_handle)
5305 len += sprintf(p + len, "status:\t\tnot supported\n");
5306 else {
5307 len += sprintf(p + len, "status:\t\tsupported\n");
5308 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5311 return len;
5314 static int beep_write(char *buf)
5316 char *cmd;
5317 int beep_cmd;
5319 if (!beep_handle)
5320 return -ENODEV;
5322 while ((cmd = next_cmd(&buf))) {
5323 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5324 beep_cmd >= 0 && beep_cmd <= 17) {
5325 /* beep_cmd set */
5326 } else
5327 return -EINVAL;
5328 if (tp_features.beep_needs_two_args) {
5329 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5330 beep_cmd, 0))
5331 return -EIO;
5332 } else {
5333 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5334 beep_cmd))
5335 return -EIO;
5339 return 0;
5342 static struct ibm_struct beep_driver_data = {
5343 .name = "beep",
5344 .read = beep_read,
5345 .write = beep_write,
5348 /*************************************************************************
5349 * Thermal subdriver
5352 enum thermal_access_mode {
5353 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5354 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5355 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5356 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5357 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5360 enum { /* TPACPI_THERMAL_TPEC_* */
5361 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5362 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5363 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5366 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5367 struct ibm_thermal_sensors_struct {
5368 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5371 static enum thermal_access_mode thermal_read_mode;
5373 /* idx is zero-based */
5374 static int thermal_get_sensor(int idx, s32 *value)
5376 int t;
5377 s8 tmp;
5378 char tmpi[5];
5380 t = TP_EC_THERMAL_TMP0;
5382 switch (thermal_read_mode) {
5383 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5384 case TPACPI_THERMAL_TPEC_16:
5385 if (idx >= 8 && idx <= 15) {
5386 t = TP_EC_THERMAL_TMP8;
5387 idx -= 8;
5389 /* fallthrough */
5390 #endif
5391 case TPACPI_THERMAL_TPEC_8:
5392 if (idx <= 7) {
5393 if (!acpi_ec_read(t + idx, &tmp))
5394 return -EIO;
5395 *value = tmp * 1000;
5396 return 0;
5398 break;
5400 case TPACPI_THERMAL_ACPI_UPDT:
5401 if (idx <= 7) {
5402 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5403 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5404 return -EIO;
5405 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5406 return -EIO;
5407 *value = (t - 2732) * 100;
5408 return 0;
5410 break;
5412 case TPACPI_THERMAL_ACPI_TMP07:
5413 if (idx <= 7) {
5414 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5415 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5416 return -EIO;
5417 if (t > 127 || t < -127)
5418 t = TP_EC_THERMAL_TMP_NA;
5419 *value = t * 1000;
5420 return 0;
5422 break;
5424 case TPACPI_THERMAL_NONE:
5425 default:
5426 return -ENOSYS;
5429 return -EINVAL;
5432 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5434 int res, i;
5435 int n;
5437 n = 8;
5438 i = 0;
5440 if (!s)
5441 return -EINVAL;
5443 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5444 n = 16;
5446 for (i = 0 ; i < n; i++) {
5447 res = thermal_get_sensor(i, &s->temp[i]);
5448 if (res)
5449 return res;
5452 return n;
5455 /* sysfs temp##_input -------------------------------------------------- */
5457 static ssize_t thermal_temp_input_show(struct device *dev,
5458 struct device_attribute *attr,
5459 char *buf)
5461 struct sensor_device_attribute *sensor_attr =
5462 to_sensor_dev_attr(attr);
5463 int idx = sensor_attr->index;
5464 s32 value;
5465 int res;
5467 res = thermal_get_sensor(idx, &value);
5468 if (res)
5469 return res;
5470 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5471 return -ENXIO;
5473 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5476 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5477 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5478 thermal_temp_input_show, NULL, _idxB)
5480 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5481 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5482 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5483 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5484 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5485 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5486 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5487 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5488 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5489 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5490 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5491 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5492 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5493 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5494 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5495 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5496 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5499 #define THERMAL_ATTRS(X) \
5500 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5502 static struct attribute *thermal_temp_input_attr[] = {
5503 THERMAL_ATTRS(8),
5504 THERMAL_ATTRS(9),
5505 THERMAL_ATTRS(10),
5506 THERMAL_ATTRS(11),
5507 THERMAL_ATTRS(12),
5508 THERMAL_ATTRS(13),
5509 THERMAL_ATTRS(14),
5510 THERMAL_ATTRS(15),
5511 THERMAL_ATTRS(0),
5512 THERMAL_ATTRS(1),
5513 THERMAL_ATTRS(2),
5514 THERMAL_ATTRS(3),
5515 THERMAL_ATTRS(4),
5516 THERMAL_ATTRS(5),
5517 THERMAL_ATTRS(6),
5518 THERMAL_ATTRS(7),
5519 NULL
5522 static const struct attribute_group thermal_temp_input16_group = {
5523 .attrs = thermal_temp_input_attr
5526 static const struct attribute_group thermal_temp_input8_group = {
5527 .attrs = &thermal_temp_input_attr[8]
5530 #undef THERMAL_SENSOR_ATTR_TEMP
5531 #undef THERMAL_ATTRS
5533 /* --------------------------------------------------------------------- */
5535 static int __init thermal_init(struct ibm_init_struct *iibm)
5537 u8 t, ta1, ta2;
5538 int i;
5539 int acpi_tmp7;
5540 int res;
5542 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5544 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5546 if (thinkpad_id.ec_model) {
5548 * Direct EC access mode: sensors at registers
5549 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5550 * non-implemented, thermal sensors return 0x80 when
5551 * not available
5554 ta1 = ta2 = 0;
5555 for (i = 0; i < 8; i++) {
5556 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5557 ta1 |= t;
5558 } else {
5559 ta1 = 0;
5560 break;
5562 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5563 ta2 |= t;
5564 } else {
5565 ta1 = 0;
5566 break;
5569 if (ta1 == 0) {
5570 /* This is sheer paranoia, but we handle it anyway */
5571 if (acpi_tmp7) {
5572 printk(TPACPI_ERR
5573 "ThinkPad ACPI EC access misbehaving, "
5574 "falling back to ACPI TMPx access "
5575 "mode\n");
5576 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5577 } else {
5578 printk(TPACPI_ERR
5579 "ThinkPad ACPI EC access misbehaving, "
5580 "disabling thermal sensors access\n");
5581 thermal_read_mode = TPACPI_THERMAL_NONE;
5583 } else {
5584 thermal_read_mode =
5585 (ta2 != 0) ?
5586 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5588 } else if (acpi_tmp7) {
5589 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5590 /* 600e/x, 770e, 770x */
5591 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5592 } else {
5593 /* Standard ACPI TMPx access, max 8 sensors */
5594 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5596 } else {
5597 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5598 thermal_read_mode = TPACPI_THERMAL_NONE;
5601 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5602 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5603 thermal_read_mode);
5605 switch (thermal_read_mode) {
5606 case TPACPI_THERMAL_TPEC_16:
5607 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5608 &thermal_temp_input16_group);
5609 if (res)
5610 return res;
5611 break;
5612 case TPACPI_THERMAL_TPEC_8:
5613 case TPACPI_THERMAL_ACPI_TMP07:
5614 case TPACPI_THERMAL_ACPI_UPDT:
5615 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5616 &thermal_temp_input8_group);
5617 if (res)
5618 return res;
5619 break;
5620 case TPACPI_THERMAL_NONE:
5621 default:
5622 return 1;
5625 return 0;
5628 static void thermal_exit(void)
5630 switch (thermal_read_mode) {
5631 case TPACPI_THERMAL_TPEC_16:
5632 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5633 &thermal_temp_input16_group);
5634 break;
5635 case TPACPI_THERMAL_TPEC_8:
5636 case TPACPI_THERMAL_ACPI_TMP07:
5637 case TPACPI_THERMAL_ACPI_UPDT:
5638 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5639 &thermal_temp_input16_group);
5640 break;
5641 case TPACPI_THERMAL_NONE:
5642 default:
5643 break;
5647 static int thermal_read(char *p)
5649 int len = 0;
5650 int n, i;
5651 struct ibm_thermal_sensors_struct t;
5653 n = thermal_get_sensors(&t);
5654 if (unlikely(n < 0))
5655 return n;
5657 len += sprintf(p + len, "temperatures:\t");
5659 if (n > 0) {
5660 for (i = 0; i < (n - 1); i++)
5661 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5662 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5663 } else
5664 len += sprintf(p + len, "not supported\n");
5666 return len;
5669 static struct ibm_struct thermal_driver_data = {
5670 .name = "thermal",
5671 .read = thermal_read,
5672 .exit = thermal_exit,
5675 /*************************************************************************
5676 * EC Dump subdriver
5679 static u8 ecdump_regs[256];
5681 static int ecdump_read(char *p)
5683 int len = 0;
5684 int i, j;
5685 u8 v;
5687 len += sprintf(p + len, "EC "
5688 " +00 +01 +02 +03 +04 +05 +06 +07"
5689 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5690 for (i = 0; i < 256; i += 16) {
5691 len += sprintf(p + len, "EC 0x%02x:", i);
5692 for (j = 0; j < 16; j++) {
5693 if (!acpi_ec_read(i + j, &v))
5694 break;
5695 if (v != ecdump_regs[i + j])
5696 len += sprintf(p + len, " *%02x", v);
5697 else
5698 len += sprintf(p + len, " %02x", v);
5699 ecdump_regs[i + j] = v;
5701 len += sprintf(p + len, "\n");
5702 if (j != 16)
5703 break;
5706 /* These are way too dangerous to advertise openly... */
5707 #if 0
5708 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5709 " (<offset> is 00-ff, <value> is 00-ff)\n");
5710 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5711 " (<offset> is 00-ff, <value> is 0-255)\n");
5712 #endif
5713 return len;
5716 static int ecdump_write(char *buf)
5718 char *cmd;
5719 int i, v;
5721 while ((cmd = next_cmd(&buf))) {
5722 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5723 /* i and v set */
5724 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5725 /* i and v set */
5726 } else
5727 return -EINVAL;
5728 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5729 if (!acpi_ec_write(i, v))
5730 return -EIO;
5731 } else
5732 return -EINVAL;
5735 return 0;
5738 static struct ibm_struct ecdump_driver_data = {
5739 .name = "ecdump",
5740 .read = ecdump_read,
5741 .write = ecdump_write,
5742 .flags.experimental = 1,
5745 /*************************************************************************
5746 * Backlight/brightness subdriver
5749 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5752 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5753 * CMOS NVRAM byte 0x5E, bits 0-3.
5755 * EC HBRV (0x31) has the following layout
5756 * Bit 7: unknown function
5757 * Bit 6: unknown function
5758 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5759 * Bit 4: must be set to zero to avoid problems
5760 * Bit 3-0: backlight brightness level
5762 * brightness_get_raw returns status data in the HBRV layout
5764 * WARNING: The X61 has been verified to use HBRV for something else, so
5765 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5766 * testing on the very early *60 Lenovo models...
5769 enum {
5770 TP_EC_BACKLIGHT = 0x31,
5772 /* TP_EC_BACKLIGHT bitmasks */
5773 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5774 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5775 TP_EC_BACKLIGHT_MAPSW = 0x20,
5778 enum tpacpi_brightness_access_mode {
5779 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5780 TPACPI_BRGHT_MODE_EC, /* EC control */
5781 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5782 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5783 TPACPI_BRGHT_MODE_MAX
5786 static struct backlight_device *ibm_backlight_device;
5788 static enum tpacpi_brightness_access_mode brightness_mode =
5789 TPACPI_BRGHT_MODE_MAX;
5791 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5793 static struct mutex brightness_mutex;
5795 /* NVRAM brightness access,
5796 * call with brightness_mutex held! */
5797 static unsigned int tpacpi_brightness_nvram_get(void)
5799 u8 lnvram;
5801 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5802 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5803 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5804 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5806 return lnvram;
5809 static void tpacpi_brightness_checkpoint_nvram(void)
5811 u8 lec = 0;
5812 u8 b_nvram;
5814 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5815 return;
5817 vdbg_printk(TPACPI_DBG_BRGHT,
5818 "trying to checkpoint backlight level to NVRAM...\n");
5820 if (mutex_lock_killable(&brightness_mutex) < 0)
5821 return;
5823 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5824 goto unlock;
5825 lec &= TP_EC_BACKLIGHT_LVLMSK;
5826 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5828 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5829 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5830 /* NVRAM needs update */
5831 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5832 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5833 b_nvram |= lec;
5834 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5835 dbg_printk(TPACPI_DBG_BRGHT,
5836 "updated NVRAM backlight level to %u (0x%02x)\n",
5837 (unsigned int) lec, (unsigned int) b_nvram);
5838 } else
5839 vdbg_printk(TPACPI_DBG_BRGHT,
5840 "NVRAM backlight level already is %u (0x%02x)\n",
5841 (unsigned int) lec, (unsigned int) b_nvram);
5843 unlock:
5844 mutex_unlock(&brightness_mutex);
5848 /* call with brightness_mutex held! */
5849 static int tpacpi_brightness_get_raw(int *status)
5851 u8 lec = 0;
5853 switch (brightness_mode) {
5854 case TPACPI_BRGHT_MODE_UCMS_STEP:
5855 *status = tpacpi_brightness_nvram_get();
5856 return 0;
5857 case TPACPI_BRGHT_MODE_EC:
5858 case TPACPI_BRGHT_MODE_ECNVRAM:
5859 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5860 return -EIO;
5861 *status = lec;
5862 return 0;
5863 default:
5864 return -ENXIO;
5868 /* call with brightness_mutex held! */
5869 /* do NOT call with illegal backlight level value */
5870 static int tpacpi_brightness_set_ec(unsigned int value)
5872 u8 lec = 0;
5874 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5875 return -EIO;
5877 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5878 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5879 (value & TP_EC_BACKLIGHT_LVLMSK))))
5880 return -EIO;
5882 return 0;
5885 /* call with brightness_mutex held! */
5886 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5888 int cmos_cmd, inc;
5889 unsigned int current_value, i;
5891 current_value = tpacpi_brightness_nvram_get();
5893 if (value == current_value)
5894 return 0;
5896 cmos_cmd = (value > current_value) ?
5897 TP_CMOS_BRIGHTNESS_UP :
5898 TP_CMOS_BRIGHTNESS_DOWN;
5899 inc = (value > current_value) ? 1 : -1;
5901 for (i = current_value; i != value; i += inc)
5902 if (issue_thinkpad_cmos_command(cmos_cmd))
5903 return -EIO;
5905 return 0;
5908 /* May return EINTR which can always be mapped to ERESTARTSYS */
5909 static int brightness_set(unsigned int value)
5911 int res;
5913 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5914 value < 0)
5915 return -EINVAL;
5917 vdbg_printk(TPACPI_DBG_BRGHT,
5918 "set backlight level to %d\n", value);
5920 res = mutex_lock_killable(&brightness_mutex);
5921 if (res < 0)
5922 return res;
5924 switch (brightness_mode) {
5925 case TPACPI_BRGHT_MODE_EC:
5926 case TPACPI_BRGHT_MODE_ECNVRAM:
5927 res = tpacpi_brightness_set_ec(value);
5928 break;
5929 case TPACPI_BRGHT_MODE_UCMS_STEP:
5930 res = tpacpi_brightness_set_ucmsstep(value);
5931 break;
5932 default:
5933 res = -ENXIO;
5936 mutex_unlock(&brightness_mutex);
5937 return res;
5940 /* sysfs backlight class ----------------------------------------------- */
5942 static int brightness_update_status(struct backlight_device *bd)
5944 unsigned int level =
5945 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5946 bd->props.power == FB_BLANK_UNBLANK) ?
5947 bd->props.brightness : 0;
5949 dbg_printk(TPACPI_DBG_BRGHT,
5950 "backlight: attempt to set level to %d\n",
5951 level);
5953 /* it is the backlight class's job (caller) to handle
5954 * EINTR and other errors properly */
5955 return brightness_set(level);
5958 static int brightness_get(struct backlight_device *bd)
5960 int status, res;
5962 res = mutex_lock_killable(&brightness_mutex);
5963 if (res < 0)
5964 return 0;
5966 res = tpacpi_brightness_get_raw(&status);
5968 mutex_unlock(&brightness_mutex);
5970 if (res < 0)
5971 return 0;
5973 return status & TP_EC_BACKLIGHT_LVLMSK;
5976 static struct backlight_ops ibm_backlight_data = {
5977 .get_brightness = brightness_get,
5978 .update_status = brightness_update_status,
5981 /* --------------------------------------------------------------------- */
5984 * These are only useful for models that have only one possibility
5985 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5986 * these quirks.
5988 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5989 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5990 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5992 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5993 /* Models with ATI GPUs known to require ECNVRAM mode */
5994 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5996 /* Models with ATI GPUs that can use ECNVRAM */
5997 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
5998 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5999 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6000 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6002 /* Models with Intel Extreme Graphics 2 */
6003 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6004 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6005 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
6007 /* Models with Intel GMA900 */
6008 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6009 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6010 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6013 static int __init brightness_init(struct ibm_init_struct *iibm)
6015 int b;
6016 unsigned long quirks;
6018 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6020 mutex_init(&brightness_mutex);
6022 quirks = tpacpi_check_quirks(brightness_quirk_table,
6023 ARRAY_SIZE(brightness_quirk_table));
6026 * We always attempt to detect acpi support, so as to switch
6027 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6028 * going to publish a backlight interface
6030 b = tpacpi_check_std_acpi_brightness_support();
6031 if (b > 0) {
6033 if (acpi_video_backlight_support()) {
6034 if (brightness_enable > 1) {
6035 printk(TPACPI_NOTICE
6036 "Standard ACPI backlight interface "
6037 "available, not loading native one.\n");
6038 return 1;
6039 } else if (brightness_enable == 1) {
6040 printk(TPACPI_NOTICE
6041 "Backlight control force enabled, even if standard "
6042 "ACPI backlight interface is available\n");
6044 } else {
6045 if (brightness_enable > 1) {
6046 printk(TPACPI_NOTICE
6047 "Standard ACPI backlight interface not "
6048 "available, thinkpad_acpi native "
6049 "brightness control enabled\n");
6054 if (!brightness_enable) {
6055 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6056 "brightness support disabled by "
6057 "module parameter\n");
6058 return 1;
6061 if (b > 16) {
6062 printk(TPACPI_ERR
6063 "Unsupported brightness interface, "
6064 "please contact %s\n", TPACPI_MAIL);
6065 return 1;
6067 if (b == 16)
6068 tp_features.bright_16levels = 1;
6071 * Check for module parameter bogosity, note that we
6072 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6073 * able to detect "unspecified"
6075 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6076 return -EINVAL;
6078 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6079 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6080 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6081 if (quirks & TPACPI_BRGHT_Q_EC)
6082 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6083 else
6084 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6086 dbg_printk(TPACPI_DBG_BRGHT,
6087 "driver auto-selected brightness_mode=%d\n",
6088 brightness_mode);
6091 /* Safety */
6092 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6093 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6094 brightness_mode == TPACPI_BRGHT_MODE_EC))
6095 return -EINVAL;
6097 if (tpacpi_brightness_get_raw(&b) < 0)
6098 return 1;
6100 if (tp_features.bright_16levels)
6101 printk(TPACPI_INFO
6102 "detected a 16-level brightness capable ThinkPad\n");
6104 ibm_backlight_device = backlight_device_register(
6105 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6106 &ibm_backlight_data);
6107 if (IS_ERR(ibm_backlight_device)) {
6108 int rc = PTR_ERR(ibm_backlight_device);
6109 ibm_backlight_device = NULL;
6110 printk(TPACPI_ERR "Could not register backlight device\n");
6111 return rc;
6113 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6114 "brightness is supported\n");
6116 if (quirks & TPACPI_BRGHT_Q_ASK) {
6117 printk(TPACPI_NOTICE
6118 "brightness: will use unverified default: "
6119 "brightness_mode=%d\n", brightness_mode);
6120 printk(TPACPI_NOTICE
6121 "brightness: please report to %s whether it works well "
6122 "or not on your ThinkPad\n", TPACPI_MAIL);
6125 ibm_backlight_device->props.max_brightness =
6126 (tp_features.bright_16levels)? 15 : 7;
6127 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6128 backlight_update_status(ibm_backlight_device);
6130 return 0;
6133 static void brightness_suspend(pm_message_t state)
6135 tpacpi_brightness_checkpoint_nvram();
6138 static void brightness_shutdown(void)
6140 tpacpi_brightness_checkpoint_nvram();
6143 static void brightness_exit(void)
6145 if (ibm_backlight_device) {
6146 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6147 "calling backlight_device_unregister()\n");
6148 backlight_device_unregister(ibm_backlight_device);
6151 tpacpi_brightness_checkpoint_nvram();
6154 static int brightness_read(char *p)
6156 int len = 0;
6157 int level;
6159 level = brightness_get(NULL);
6160 if (level < 0) {
6161 len += sprintf(p + len, "level:\t\tunreadable\n");
6162 } else {
6163 len += sprintf(p + len, "level:\t\t%d\n", level);
6164 len += sprintf(p + len, "commands:\tup, down\n");
6165 len += sprintf(p + len, "commands:\tlevel <level>"
6166 " (<level> is 0-%d)\n",
6167 (tp_features.bright_16levels) ? 15 : 7);
6170 return len;
6173 static int brightness_write(char *buf)
6175 int level;
6176 int rc;
6177 char *cmd;
6178 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6180 level = brightness_get(NULL);
6181 if (level < 0)
6182 return level;
6184 while ((cmd = next_cmd(&buf))) {
6185 if (strlencmp(cmd, "up") == 0) {
6186 if (level < max_level)
6187 level++;
6188 } else if (strlencmp(cmd, "down") == 0) {
6189 if (level > 0)
6190 level--;
6191 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6192 level >= 0 && level <= max_level) {
6193 /* new level set */
6194 } else
6195 return -EINVAL;
6198 tpacpi_disclose_usertask("procfs brightness",
6199 "set level to %d\n", level);
6202 * Now we know what the final level should be, so we try to set it.
6203 * Doing it this way makes the syscall restartable in case of EINTR
6205 rc = brightness_set(level);
6206 return (rc == -EINTR)? ERESTARTSYS : rc;
6209 static struct ibm_struct brightness_driver_data = {
6210 .name = "brightness",
6211 .read = brightness_read,
6212 .write = brightness_write,
6213 .exit = brightness_exit,
6214 .suspend = brightness_suspend,
6215 .shutdown = brightness_shutdown,
6218 /*************************************************************************
6219 * Volume subdriver
6222 static int volume_offset = 0x30;
6224 static int volume_read(char *p)
6226 int len = 0;
6227 u8 level;
6229 if (!acpi_ec_read(volume_offset, &level)) {
6230 len += sprintf(p + len, "level:\t\tunreadable\n");
6231 } else {
6232 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6233 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6234 len += sprintf(p + len, "commands:\tup, down, mute\n");
6235 len += sprintf(p + len, "commands:\tlevel <level>"
6236 " (<level> is 0-15)\n");
6239 return len;
6242 static int volume_write(char *buf)
6244 int cmos_cmd, inc, i;
6245 u8 level, mute;
6246 int new_level, new_mute;
6247 char *cmd;
6249 while ((cmd = next_cmd(&buf))) {
6250 if (!acpi_ec_read(volume_offset, &level))
6251 return -EIO;
6252 new_mute = mute = level & 0x40;
6253 new_level = level = level & 0xf;
6255 if (strlencmp(cmd, "up") == 0) {
6256 if (mute)
6257 new_mute = 0;
6258 else
6259 new_level = level == 15 ? 15 : level + 1;
6260 } else if (strlencmp(cmd, "down") == 0) {
6261 if (mute)
6262 new_mute = 0;
6263 else
6264 new_level = level == 0 ? 0 : level - 1;
6265 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6266 new_level >= 0 && new_level <= 15) {
6267 /* new_level set */
6268 } else if (strlencmp(cmd, "mute") == 0) {
6269 new_mute = 0x40;
6270 } else
6271 return -EINVAL;
6273 if (new_level != level) {
6274 /* mute doesn't change */
6276 cmos_cmd = (new_level > level) ?
6277 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6278 inc = new_level > level ? 1 : -1;
6280 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6281 !acpi_ec_write(volume_offset, level)))
6282 return -EIO;
6284 for (i = level; i != new_level; i += inc)
6285 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6286 !acpi_ec_write(volume_offset, i + inc))
6287 return -EIO;
6289 if (mute &&
6290 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6291 !acpi_ec_write(volume_offset, new_level + mute))) {
6292 return -EIO;
6296 if (new_mute != mute) {
6297 /* level doesn't change */
6299 cmos_cmd = (new_mute) ?
6300 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6302 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6303 !acpi_ec_write(volume_offset, level + new_mute))
6304 return -EIO;
6308 return 0;
6311 static struct ibm_struct volume_driver_data = {
6312 .name = "volume",
6313 .read = volume_read,
6314 .write = volume_write,
6317 /*************************************************************************
6318 * Fan subdriver
6322 * FAN ACCESS MODES
6324 * TPACPI_FAN_RD_ACPI_GFAN:
6325 * ACPI GFAN method: returns fan level
6327 * see TPACPI_FAN_WR_ACPI_SFAN
6328 * EC 0x2f (HFSP) not available if GFAN exists
6330 * TPACPI_FAN_WR_ACPI_SFAN:
6331 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6333 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6334 * it for writing.
6336 * TPACPI_FAN_WR_TPEC:
6337 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6338 * Supported on almost all ThinkPads
6340 * Fan speed changes of any sort (including those caused by the
6341 * disengaged mode) are usually done slowly by the firmware as the
6342 * maximum ammount of fan duty cycle change per second seems to be
6343 * limited.
6345 * Reading is not available if GFAN exists.
6346 * Writing is not available if SFAN exists.
6348 * Bits
6349 * 7 automatic mode engaged;
6350 * (default operation mode of the ThinkPad)
6351 * fan level is ignored in this mode.
6352 * 6 full speed mode (takes precedence over bit 7);
6353 * not available on all thinkpads. May disable
6354 * the tachometer while the fan controller ramps up
6355 * the speed (which can take up to a few *minutes*).
6356 * Speeds up fan to 100% duty-cycle, which is far above
6357 * the standard RPM levels. It is not impossible that
6358 * it could cause hardware damage.
6359 * 5-3 unused in some models. Extra bits for fan level
6360 * in others, but still useless as all values above
6361 * 7 map to the same speed as level 7 in these models.
6362 * 2-0 fan level (0..7 usually)
6363 * 0x00 = stop
6364 * 0x07 = max (set when temperatures critical)
6365 * Some ThinkPads may have other levels, see
6366 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6368 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6369 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6370 * does so, its initial value is meaningless (0x07).
6372 * For firmware bugs, refer to:
6373 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6375 * ----
6377 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6378 * Main fan tachometer reading (in RPM)
6380 * This register is present on all ThinkPads with a new-style EC, and
6381 * it is known not to be present on the A21m/e, and T22, as there is
6382 * something else in offset 0x84 according to the ACPI DSDT. Other
6383 * ThinkPads from this same time period (and earlier) probably lack the
6384 * tachometer as well.
6386 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6387 * was never fixed by IBM to report the EC firmware version string
6388 * probably support the tachometer (like the early X models), so
6389 * detecting it is quite hard. We need more data to know for sure.
6391 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6392 * might result.
6394 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6395 * mode.
6397 * For firmware bugs, refer to:
6398 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6400 * ----
6402 * ThinkPad EC register 0x31 bit 0 (only on select models)
6404 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6405 * show the speed of the main fan. When bit 0 of EC register 0x31
6406 * is one, the tachometer registers show the speed of the auxiliary
6407 * fan.
6409 * Fan control seems to affect both fans, regardless of the state
6410 * of this bit.
6412 * So far, only the firmware for the X60/X61 non-tablet versions
6413 * seem to support this (firmware TP-7M).
6415 * TPACPI_FAN_WR_ACPI_FANS:
6416 * ThinkPad X31, X40, X41. Not available in the X60.
6418 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6419 * high speed. ACPI DSDT seems to map these three speeds to levels
6420 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6421 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6423 * The speeds are stored on handles
6424 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6426 * There are three default speed sets, acessible as handles:
6427 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6429 * ACPI DSDT switches which set is in use depending on various
6430 * factors.
6432 * TPACPI_FAN_WR_TPEC is also available and should be used to
6433 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6434 * but the ACPI tables just mention level 7.
6437 enum { /* Fan control constants */
6438 fan_status_offset = 0x2f, /* EC register 0x2f */
6439 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6440 * 0x84 must be read before 0x85 */
6441 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6442 bit 0 selects which fan is active */
6444 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6445 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6447 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6450 enum fan_status_access_mode {
6451 TPACPI_FAN_NONE = 0, /* No fan status or control */
6452 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6453 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6456 enum fan_control_access_mode {
6457 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6458 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6459 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6460 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6463 enum fan_control_commands {
6464 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6465 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6466 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6467 * and also watchdog cmd */
6470 static int fan_control_allowed;
6472 static enum fan_status_access_mode fan_status_access_mode;
6473 static enum fan_control_access_mode fan_control_access_mode;
6474 static enum fan_control_commands fan_control_commands;
6476 static u8 fan_control_initial_status;
6477 static u8 fan_control_desired_level;
6478 static u8 fan_control_resume_level;
6479 static int fan_watchdog_maxinterval;
6481 static struct mutex fan_mutex;
6483 static void fan_watchdog_fire(struct work_struct *ignored);
6484 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6486 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6487 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6488 "\\FSPD", /* 600e/x, 770e, 770x */
6489 ); /* all others */
6490 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6491 "JFNS", /* 770x-JL */
6492 ); /* all others */
6495 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6496 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6497 * be in auto mode (0x80).
6499 * This is corrected by any write to HFSP either by the driver, or
6500 * by the firmware.
6502 * We assume 0x07 really means auto mode while this quirk is active,
6503 * as this is far more likely than the ThinkPad being in level 7,
6504 * which is only used by the firmware during thermal emergencies.
6506 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6507 * TP-70 (T43, R52), which are known to be buggy.
6510 static void fan_quirk1_setup(void)
6512 if (fan_control_initial_status == 0x07) {
6513 printk(TPACPI_NOTICE
6514 "fan_init: initial fan status is unknown, "
6515 "assuming it is in auto mode\n");
6516 tp_features.fan_ctrl_status_undef = 1;
6520 static void fan_quirk1_handle(u8 *fan_status)
6522 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6523 if (*fan_status != fan_control_initial_status) {
6524 /* something changed the HFSP regisnter since
6525 * driver init time, so it is not undefined
6526 * anymore */
6527 tp_features.fan_ctrl_status_undef = 0;
6528 } else {
6529 /* Return most likely status. In fact, it
6530 * might be the only possible status */
6531 *fan_status = TP_EC_FAN_AUTO;
6536 /* Select main fan on X60/X61, NOOP on others */
6537 static bool fan_select_fan1(void)
6539 if (tp_features.second_fan) {
6540 u8 val;
6542 if (ec_read(fan_select_offset, &val) < 0)
6543 return false;
6544 val &= 0xFEU;
6545 if (ec_write(fan_select_offset, val) < 0)
6546 return false;
6548 return true;
6551 /* Select secondary fan on X60/X61 */
6552 static bool fan_select_fan2(void)
6554 u8 val;
6556 if (!tp_features.second_fan)
6557 return false;
6559 if (ec_read(fan_select_offset, &val) < 0)
6560 return false;
6561 val |= 0x01U;
6562 if (ec_write(fan_select_offset, val) < 0)
6563 return false;
6565 return true;
6569 * Call with fan_mutex held
6571 static void fan_update_desired_level(u8 status)
6573 if ((status &
6574 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6575 if (status > 7)
6576 fan_control_desired_level = 7;
6577 else
6578 fan_control_desired_level = status;
6582 static int fan_get_status(u8 *status)
6584 u8 s;
6586 /* TODO:
6587 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6589 switch (fan_status_access_mode) {
6590 case TPACPI_FAN_RD_ACPI_GFAN:
6591 /* 570, 600e/x, 770e, 770x */
6593 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6594 return -EIO;
6596 if (likely(status))
6597 *status = s & 0x07;
6599 break;
6601 case TPACPI_FAN_RD_TPEC:
6602 /* all except 570, 600e/x, 770e, 770x */
6603 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6604 return -EIO;
6606 if (likely(status)) {
6607 *status = s;
6608 fan_quirk1_handle(status);
6611 break;
6613 default:
6614 return -ENXIO;
6617 return 0;
6620 static int fan_get_status_safe(u8 *status)
6622 int rc;
6623 u8 s;
6625 if (mutex_lock_killable(&fan_mutex))
6626 return -ERESTARTSYS;
6627 rc = fan_get_status(&s);
6628 if (!rc)
6629 fan_update_desired_level(s);
6630 mutex_unlock(&fan_mutex);
6632 if (status)
6633 *status = s;
6635 return rc;
6638 static int fan_get_speed(unsigned int *speed)
6640 u8 hi, lo;
6642 switch (fan_status_access_mode) {
6643 case TPACPI_FAN_RD_TPEC:
6644 /* all except 570, 600e/x, 770e, 770x */
6645 if (unlikely(!fan_select_fan1()))
6646 return -EIO;
6647 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6648 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6649 return -EIO;
6651 if (likely(speed))
6652 *speed = (hi << 8) | lo;
6654 break;
6656 default:
6657 return -ENXIO;
6660 return 0;
6663 static int fan2_get_speed(unsigned int *speed)
6665 u8 hi, lo;
6666 bool rc;
6668 switch (fan_status_access_mode) {
6669 case TPACPI_FAN_RD_TPEC:
6670 /* all except 570, 600e/x, 770e, 770x */
6671 if (unlikely(!fan_select_fan2()))
6672 return -EIO;
6673 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6674 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6675 fan_select_fan1(); /* play it safe */
6676 if (rc)
6677 return -EIO;
6679 if (likely(speed))
6680 *speed = (hi << 8) | lo;
6682 break;
6684 default:
6685 return -ENXIO;
6688 return 0;
6691 static int fan_set_level(int level)
6693 if (!fan_control_allowed)
6694 return -EPERM;
6696 switch (fan_control_access_mode) {
6697 case TPACPI_FAN_WR_ACPI_SFAN:
6698 if (level >= 0 && level <= 7) {
6699 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6700 return -EIO;
6701 } else
6702 return -EINVAL;
6703 break;
6705 case TPACPI_FAN_WR_ACPI_FANS:
6706 case TPACPI_FAN_WR_TPEC:
6707 if (!(level & TP_EC_FAN_AUTO) &&
6708 !(level & TP_EC_FAN_FULLSPEED) &&
6709 ((level < 0) || (level > 7)))
6710 return -EINVAL;
6712 /* safety net should the EC not support AUTO
6713 * or FULLSPEED mode bits and just ignore them */
6714 if (level & TP_EC_FAN_FULLSPEED)
6715 level |= 7; /* safety min speed 7 */
6716 else if (level & TP_EC_FAN_AUTO)
6717 level |= 4; /* safety min speed 4 */
6719 if (!acpi_ec_write(fan_status_offset, level))
6720 return -EIO;
6721 else
6722 tp_features.fan_ctrl_status_undef = 0;
6723 break;
6725 default:
6726 return -ENXIO;
6729 vdbg_printk(TPACPI_DBG_FAN,
6730 "fan control: set fan control register to 0x%02x\n", level);
6731 return 0;
6734 static int fan_set_level_safe(int level)
6736 int rc;
6738 if (!fan_control_allowed)
6739 return -EPERM;
6741 if (mutex_lock_killable(&fan_mutex))
6742 return -ERESTARTSYS;
6744 if (level == TPACPI_FAN_LAST_LEVEL)
6745 level = fan_control_desired_level;
6747 rc = fan_set_level(level);
6748 if (!rc)
6749 fan_update_desired_level(level);
6751 mutex_unlock(&fan_mutex);
6752 return rc;
6755 static int fan_set_enable(void)
6757 u8 s;
6758 int rc;
6760 if (!fan_control_allowed)
6761 return -EPERM;
6763 if (mutex_lock_killable(&fan_mutex))
6764 return -ERESTARTSYS;
6766 switch (fan_control_access_mode) {
6767 case TPACPI_FAN_WR_ACPI_FANS:
6768 case TPACPI_FAN_WR_TPEC:
6769 rc = fan_get_status(&s);
6770 if (rc < 0)
6771 break;
6773 /* Don't go out of emergency fan mode */
6774 if (s != 7) {
6775 s &= 0x07;
6776 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6779 if (!acpi_ec_write(fan_status_offset, s))
6780 rc = -EIO;
6781 else {
6782 tp_features.fan_ctrl_status_undef = 0;
6783 rc = 0;
6785 break;
6787 case TPACPI_FAN_WR_ACPI_SFAN:
6788 rc = fan_get_status(&s);
6789 if (rc < 0)
6790 break;
6792 s &= 0x07;
6794 /* Set fan to at least level 4 */
6795 s |= 4;
6797 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6798 rc = -EIO;
6799 else
6800 rc = 0;
6801 break;
6803 default:
6804 rc = -ENXIO;
6807 mutex_unlock(&fan_mutex);
6809 if (!rc)
6810 vdbg_printk(TPACPI_DBG_FAN,
6811 "fan control: set fan control register to 0x%02x\n",
6813 return rc;
6816 static int fan_set_disable(void)
6818 int rc;
6820 if (!fan_control_allowed)
6821 return -EPERM;
6823 if (mutex_lock_killable(&fan_mutex))
6824 return -ERESTARTSYS;
6826 rc = 0;
6827 switch (fan_control_access_mode) {
6828 case TPACPI_FAN_WR_ACPI_FANS:
6829 case TPACPI_FAN_WR_TPEC:
6830 if (!acpi_ec_write(fan_status_offset, 0x00))
6831 rc = -EIO;
6832 else {
6833 fan_control_desired_level = 0;
6834 tp_features.fan_ctrl_status_undef = 0;
6836 break;
6838 case TPACPI_FAN_WR_ACPI_SFAN:
6839 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6840 rc = -EIO;
6841 else
6842 fan_control_desired_level = 0;
6843 break;
6845 default:
6846 rc = -ENXIO;
6849 if (!rc)
6850 vdbg_printk(TPACPI_DBG_FAN,
6851 "fan control: set fan control register to 0\n");
6853 mutex_unlock(&fan_mutex);
6854 return rc;
6857 static int fan_set_speed(int speed)
6859 int rc;
6861 if (!fan_control_allowed)
6862 return -EPERM;
6864 if (mutex_lock_killable(&fan_mutex))
6865 return -ERESTARTSYS;
6867 rc = 0;
6868 switch (fan_control_access_mode) {
6869 case TPACPI_FAN_WR_ACPI_FANS:
6870 if (speed >= 0 && speed <= 65535) {
6871 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6872 speed, speed, speed))
6873 rc = -EIO;
6874 } else
6875 rc = -EINVAL;
6876 break;
6878 default:
6879 rc = -ENXIO;
6882 mutex_unlock(&fan_mutex);
6883 return rc;
6886 static void fan_watchdog_reset(void)
6888 static int fan_watchdog_active;
6890 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6891 return;
6893 if (fan_watchdog_active)
6894 cancel_delayed_work(&fan_watchdog_task);
6896 if (fan_watchdog_maxinterval > 0 &&
6897 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6898 fan_watchdog_active = 1;
6899 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6900 msecs_to_jiffies(fan_watchdog_maxinterval
6901 * 1000))) {
6902 printk(TPACPI_ERR
6903 "failed to queue the fan watchdog, "
6904 "watchdog will not trigger\n");
6906 } else
6907 fan_watchdog_active = 0;
6910 static void fan_watchdog_fire(struct work_struct *ignored)
6912 int rc;
6914 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6915 return;
6917 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6918 rc = fan_set_enable();
6919 if (rc < 0) {
6920 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6921 "will try again later...\n", -rc);
6922 /* reschedule for later */
6923 fan_watchdog_reset();
6928 * SYSFS fan layout: hwmon compatible (device)
6930 * pwm*_enable:
6931 * 0: "disengaged" mode
6932 * 1: manual mode
6933 * 2: native EC "auto" mode (recommended, hardware default)
6935 * pwm*: set speed in manual mode, ignored otherwise.
6936 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6937 * interpolation.
6939 * fan*_input: tachometer reading, RPM
6942 * SYSFS fan layout: extensions
6944 * fan_watchdog (driver):
6945 * fan watchdog interval in seconds, 0 disables (default), max 120
6948 /* sysfs fan pwm1_enable ----------------------------------------------- */
6949 static ssize_t fan_pwm1_enable_show(struct device *dev,
6950 struct device_attribute *attr,
6951 char *buf)
6953 int res, mode;
6954 u8 status;
6956 res = fan_get_status_safe(&status);
6957 if (res)
6958 return res;
6960 if (status & TP_EC_FAN_FULLSPEED) {
6961 mode = 0;
6962 } else if (status & TP_EC_FAN_AUTO) {
6963 mode = 2;
6964 } else
6965 mode = 1;
6967 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6970 static ssize_t fan_pwm1_enable_store(struct device *dev,
6971 struct device_attribute *attr,
6972 const char *buf, size_t count)
6974 unsigned long t;
6975 int res, level;
6977 if (parse_strtoul(buf, 2, &t))
6978 return -EINVAL;
6980 tpacpi_disclose_usertask("hwmon pwm1_enable",
6981 "set fan mode to %lu\n", t);
6983 switch (t) {
6984 case 0:
6985 level = TP_EC_FAN_FULLSPEED;
6986 break;
6987 case 1:
6988 level = TPACPI_FAN_LAST_LEVEL;
6989 break;
6990 case 2:
6991 level = TP_EC_FAN_AUTO;
6992 break;
6993 case 3:
6994 /* reserved for software-controlled auto mode */
6995 return -ENOSYS;
6996 default:
6997 return -EINVAL;
7000 res = fan_set_level_safe(level);
7001 if (res == -ENXIO)
7002 return -EINVAL;
7003 else if (res < 0)
7004 return res;
7006 fan_watchdog_reset();
7008 return count;
7011 static struct device_attribute dev_attr_fan_pwm1_enable =
7012 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7013 fan_pwm1_enable_show, fan_pwm1_enable_store);
7015 /* sysfs fan pwm1 ------------------------------------------------------ */
7016 static ssize_t fan_pwm1_show(struct device *dev,
7017 struct device_attribute *attr,
7018 char *buf)
7020 int res;
7021 u8 status;
7023 res = fan_get_status_safe(&status);
7024 if (res)
7025 return res;
7027 if ((status &
7028 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7029 status = fan_control_desired_level;
7031 if (status > 7)
7032 status = 7;
7034 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7037 static ssize_t fan_pwm1_store(struct device *dev,
7038 struct device_attribute *attr,
7039 const char *buf, size_t count)
7041 unsigned long s;
7042 int rc;
7043 u8 status, newlevel;
7045 if (parse_strtoul(buf, 255, &s))
7046 return -EINVAL;
7048 tpacpi_disclose_usertask("hwmon pwm1",
7049 "set fan speed to %lu\n", s);
7051 /* scale down from 0-255 to 0-7 */
7052 newlevel = (s >> 5) & 0x07;
7054 if (mutex_lock_killable(&fan_mutex))
7055 return -ERESTARTSYS;
7057 rc = fan_get_status(&status);
7058 if (!rc && (status &
7059 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7060 rc = fan_set_level(newlevel);
7061 if (rc == -ENXIO)
7062 rc = -EINVAL;
7063 else if (!rc) {
7064 fan_update_desired_level(newlevel);
7065 fan_watchdog_reset();
7069 mutex_unlock(&fan_mutex);
7070 return (rc)? rc : count;
7073 static struct device_attribute dev_attr_fan_pwm1 =
7074 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7075 fan_pwm1_show, fan_pwm1_store);
7077 /* sysfs fan fan1_input ------------------------------------------------ */
7078 static ssize_t fan_fan1_input_show(struct device *dev,
7079 struct device_attribute *attr,
7080 char *buf)
7082 int res;
7083 unsigned int speed;
7085 res = fan_get_speed(&speed);
7086 if (res < 0)
7087 return res;
7089 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7092 static struct device_attribute dev_attr_fan_fan1_input =
7093 __ATTR(fan1_input, S_IRUGO,
7094 fan_fan1_input_show, NULL);
7096 /* sysfs fan fan2_input ------------------------------------------------ */
7097 static ssize_t fan_fan2_input_show(struct device *dev,
7098 struct device_attribute *attr,
7099 char *buf)
7101 int res;
7102 unsigned int speed;
7104 res = fan2_get_speed(&speed);
7105 if (res < 0)
7106 return res;
7108 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7111 static struct device_attribute dev_attr_fan_fan2_input =
7112 __ATTR(fan2_input, S_IRUGO,
7113 fan_fan2_input_show, NULL);
7115 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7116 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7117 char *buf)
7119 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7122 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7123 const char *buf, size_t count)
7125 unsigned long t;
7127 if (parse_strtoul(buf, 120, &t))
7128 return -EINVAL;
7130 if (!fan_control_allowed)
7131 return -EPERM;
7133 fan_watchdog_maxinterval = t;
7134 fan_watchdog_reset();
7136 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7138 return count;
7141 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7142 fan_fan_watchdog_show, fan_fan_watchdog_store);
7144 /* --------------------------------------------------------------------- */
7145 static struct attribute *fan_attributes[] = {
7146 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7147 &dev_attr_fan_fan1_input.attr,
7148 NULL, /* for fan2_input */
7149 NULL
7152 static const struct attribute_group fan_attr_group = {
7153 .attrs = fan_attributes,
7156 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7157 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7159 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7160 { .vendor = PCI_VENDOR_ID_IBM, \
7161 .bios = TPACPI_MATCH_ANY, \
7162 .ec = TPID(__id1, __id2), \
7163 .quirks = __quirks }
7165 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7166 { .vendor = PCI_VENDOR_ID_LENOVO, \
7167 .bios = TPACPI_MATCH_ANY, \
7168 .ec = TPID(__id1, __id2), \
7169 .quirks = __quirks }
7171 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7172 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7173 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7174 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7175 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7176 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7179 #undef TPACPI_FAN_QL
7180 #undef TPACPI_FAN_QI
7182 static int __init fan_init(struct ibm_init_struct *iibm)
7184 int rc;
7185 unsigned long quirks;
7187 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7188 "initializing fan subdriver\n");
7190 mutex_init(&fan_mutex);
7191 fan_status_access_mode = TPACPI_FAN_NONE;
7192 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7193 fan_control_commands = 0;
7194 fan_watchdog_maxinterval = 0;
7195 tp_features.fan_ctrl_status_undef = 0;
7196 tp_features.second_fan = 0;
7197 fan_control_desired_level = 7;
7199 TPACPI_ACPIHANDLE_INIT(fans);
7200 TPACPI_ACPIHANDLE_INIT(gfan);
7201 TPACPI_ACPIHANDLE_INIT(sfan);
7203 quirks = tpacpi_check_quirks(fan_quirk_table,
7204 ARRAY_SIZE(fan_quirk_table));
7206 if (gfan_handle) {
7207 /* 570, 600e/x, 770e, 770x */
7208 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7209 } else {
7210 /* all other ThinkPads: note that even old-style
7211 * ThinkPad ECs supports the fan control register */
7212 if (likely(acpi_ec_read(fan_status_offset,
7213 &fan_control_initial_status))) {
7214 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7215 if (quirks & TPACPI_FAN_Q1)
7216 fan_quirk1_setup();
7217 if (quirks & TPACPI_FAN_2FAN) {
7218 tp_features.second_fan = 1;
7219 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7220 "secondary fan support enabled\n");
7222 } else {
7223 printk(TPACPI_ERR
7224 "ThinkPad ACPI EC access misbehaving, "
7225 "fan status and control unavailable\n");
7226 return 1;
7230 if (sfan_handle) {
7231 /* 570, 770x-JL */
7232 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7233 fan_control_commands |=
7234 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7235 } else {
7236 if (!gfan_handle) {
7237 /* gfan without sfan means no fan control */
7238 /* all other models implement TP EC 0x2f control */
7240 if (fans_handle) {
7241 /* X31, X40, X41 */
7242 fan_control_access_mode =
7243 TPACPI_FAN_WR_ACPI_FANS;
7244 fan_control_commands |=
7245 TPACPI_FAN_CMD_SPEED |
7246 TPACPI_FAN_CMD_LEVEL |
7247 TPACPI_FAN_CMD_ENABLE;
7248 } else {
7249 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7250 fan_control_commands |=
7251 TPACPI_FAN_CMD_LEVEL |
7252 TPACPI_FAN_CMD_ENABLE;
7257 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7258 "fan is %s, modes %d, %d\n",
7259 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7260 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7261 fan_status_access_mode, fan_control_access_mode);
7263 /* fan control master switch */
7264 if (!fan_control_allowed) {
7265 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7266 fan_control_commands = 0;
7267 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7268 "fan control features disabled by parameter\n");
7271 /* update fan_control_desired_level */
7272 if (fan_status_access_mode != TPACPI_FAN_NONE)
7273 fan_get_status_safe(NULL);
7275 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7276 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7277 if (tp_features.second_fan) {
7278 /* attach second fan tachometer */
7279 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7280 &dev_attr_fan_fan2_input.attr;
7282 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7283 &fan_attr_group);
7284 if (rc < 0)
7285 return rc;
7287 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7288 &driver_attr_fan_watchdog);
7289 if (rc < 0) {
7290 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7291 &fan_attr_group);
7292 return rc;
7294 return 0;
7295 } else
7296 return 1;
7299 static void fan_exit(void)
7301 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7302 "cancelling any pending fan watchdog tasks\n");
7304 /* FIXME: can we really do this unconditionally? */
7305 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7306 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7307 &driver_attr_fan_watchdog);
7309 cancel_delayed_work(&fan_watchdog_task);
7310 flush_workqueue(tpacpi_wq);
7313 static void fan_suspend(pm_message_t state)
7315 int rc;
7317 if (!fan_control_allowed)
7318 return;
7320 /* Store fan status in cache */
7321 fan_control_resume_level = 0;
7322 rc = fan_get_status_safe(&fan_control_resume_level);
7323 if (rc < 0)
7324 printk(TPACPI_NOTICE
7325 "failed to read fan level for later "
7326 "restore during resume: %d\n", rc);
7328 /* if it is undefined, don't attempt to restore it.
7329 * KEEP THIS LAST */
7330 if (tp_features.fan_ctrl_status_undef)
7331 fan_control_resume_level = 0;
7334 static void fan_resume(void)
7336 u8 current_level = 7;
7337 bool do_set = false;
7338 int rc;
7340 /* DSDT *always* updates status on resume */
7341 tp_features.fan_ctrl_status_undef = 0;
7343 if (!fan_control_allowed ||
7344 !fan_control_resume_level ||
7345 (fan_get_status_safe(&current_level) < 0))
7346 return;
7348 switch (fan_control_access_mode) {
7349 case TPACPI_FAN_WR_ACPI_SFAN:
7350 /* never decrease fan level */
7351 do_set = (fan_control_resume_level > current_level);
7352 break;
7353 case TPACPI_FAN_WR_ACPI_FANS:
7354 case TPACPI_FAN_WR_TPEC:
7355 /* never decrease fan level, scale is:
7356 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7358 * We expect the firmware to set either 7 or AUTO, but we
7359 * handle FULLSPEED out of paranoia.
7361 * So, we can safely only restore FULLSPEED or 7, anything
7362 * else could slow the fan. Restoring AUTO is useless, at
7363 * best that's exactly what the DSDT already set (it is the
7364 * slower it uses).
7366 * Always keep in mind that the DSDT *will* have set the
7367 * fans to what the vendor supposes is the best level. We
7368 * muck with it only to speed the fan up.
7370 if (fan_control_resume_level != 7 &&
7371 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7372 return;
7373 else
7374 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7375 (current_level != fan_control_resume_level);
7376 break;
7377 default:
7378 return;
7380 if (do_set) {
7381 printk(TPACPI_NOTICE
7382 "restoring fan level to 0x%02x\n",
7383 fan_control_resume_level);
7384 rc = fan_set_level_safe(fan_control_resume_level);
7385 if (rc < 0)
7386 printk(TPACPI_NOTICE
7387 "failed to restore fan level: %d\n", rc);
7391 static int fan_read(char *p)
7393 int len = 0;
7394 int rc;
7395 u8 status;
7396 unsigned int speed = 0;
7398 switch (fan_status_access_mode) {
7399 case TPACPI_FAN_RD_ACPI_GFAN:
7400 /* 570, 600e/x, 770e, 770x */
7401 rc = fan_get_status_safe(&status);
7402 if (rc < 0)
7403 return rc;
7405 len += sprintf(p + len, "status:\t\t%s\n"
7406 "level:\t\t%d\n",
7407 (status != 0) ? "enabled" : "disabled", status);
7408 break;
7410 case TPACPI_FAN_RD_TPEC:
7411 /* all except 570, 600e/x, 770e, 770x */
7412 rc = fan_get_status_safe(&status);
7413 if (rc < 0)
7414 return rc;
7416 len += sprintf(p + len, "status:\t\t%s\n",
7417 (status != 0) ? "enabled" : "disabled");
7419 rc = fan_get_speed(&speed);
7420 if (rc < 0)
7421 return rc;
7423 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7425 if (status & TP_EC_FAN_FULLSPEED)
7426 /* Disengaged mode takes precedence */
7427 len += sprintf(p + len, "level:\t\tdisengaged\n");
7428 else if (status & TP_EC_FAN_AUTO)
7429 len += sprintf(p + len, "level:\t\tauto\n");
7430 else
7431 len += sprintf(p + len, "level:\t\t%d\n", status);
7432 break;
7434 case TPACPI_FAN_NONE:
7435 default:
7436 len += sprintf(p + len, "status:\t\tnot supported\n");
7439 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7440 len += sprintf(p + len, "commands:\tlevel <level>");
7442 switch (fan_control_access_mode) {
7443 case TPACPI_FAN_WR_ACPI_SFAN:
7444 len += sprintf(p + len, " (<level> is 0-7)\n");
7445 break;
7447 default:
7448 len += sprintf(p + len, " (<level> is 0-7, "
7449 "auto, disengaged, full-speed)\n");
7450 break;
7454 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7455 len += sprintf(p + len, "commands:\tenable, disable\n"
7456 "commands:\twatchdog <timeout> (<timeout> "
7457 "is 0 (off), 1-120 (seconds))\n");
7459 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7460 len += sprintf(p + len, "commands:\tspeed <speed>"
7461 " (<speed> is 0-65535)\n");
7463 return len;
7466 static int fan_write_cmd_level(const char *cmd, int *rc)
7468 int level;
7470 if (strlencmp(cmd, "level auto") == 0)
7471 level = TP_EC_FAN_AUTO;
7472 else if ((strlencmp(cmd, "level disengaged") == 0) |
7473 (strlencmp(cmd, "level full-speed") == 0))
7474 level = TP_EC_FAN_FULLSPEED;
7475 else if (sscanf(cmd, "level %d", &level) != 1)
7476 return 0;
7478 *rc = fan_set_level_safe(level);
7479 if (*rc == -ENXIO)
7480 printk(TPACPI_ERR "level command accepted for unsupported "
7481 "access mode %d", fan_control_access_mode);
7482 else if (!*rc)
7483 tpacpi_disclose_usertask("procfs fan",
7484 "set level to %d\n", level);
7486 return 1;
7489 static int fan_write_cmd_enable(const char *cmd, int *rc)
7491 if (strlencmp(cmd, "enable") != 0)
7492 return 0;
7494 *rc = fan_set_enable();
7495 if (*rc == -ENXIO)
7496 printk(TPACPI_ERR "enable command accepted for unsupported "
7497 "access mode %d", fan_control_access_mode);
7498 else if (!*rc)
7499 tpacpi_disclose_usertask("procfs fan", "enable\n");
7501 return 1;
7504 static int fan_write_cmd_disable(const char *cmd, int *rc)
7506 if (strlencmp(cmd, "disable") != 0)
7507 return 0;
7509 *rc = fan_set_disable();
7510 if (*rc == -ENXIO)
7511 printk(TPACPI_ERR "disable command accepted for unsupported "
7512 "access mode %d", fan_control_access_mode);
7513 else if (!*rc)
7514 tpacpi_disclose_usertask("procfs fan", "disable\n");
7516 return 1;
7519 static int fan_write_cmd_speed(const char *cmd, int *rc)
7521 int speed;
7523 /* TODO:
7524 * Support speed <low> <medium> <high> ? */
7526 if (sscanf(cmd, "speed %d", &speed) != 1)
7527 return 0;
7529 *rc = fan_set_speed(speed);
7530 if (*rc == -ENXIO)
7531 printk(TPACPI_ERR "speed command accepted for unsupported "
7532 "access mode %d", fan_control_access_mode);
7533 else if (!*rc)
7534 tpacpi_disclose_usertask("procfs fan",
7535 "set speed to %d\n", speed);
7537 return 1;
7540 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7542 int interval;
7544 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7545 return 0;
7547 if (interval < 0 || interval > 120)
7548 *rc = -EINVAL;
7549 else {
7550 fan_watchdog_maxinterval = interval;
7551 tpacpi_disclose_usertask("procfs fan",
7552 "set watchdog timer to %d\n",
7553 interval);
7556 return 1;
7559 static int fan_write(char *buf)
7561 char *cmd;
7562 int rc = 0;
7564 while (!rc && (cmd = next_cmd(&buf))) {
7565 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7566 fan_write_cmd_level(cmd, &rc)) &&
7567 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7568 (fan_write_cmd_enable(cmd, &rc) ||
7569 fan_write_cmd_disable(cmd, &rc) ||
7570 fan_write_cmd_watchdog(cmd, &rc))) &&
7571 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7572 fan_write_cmd_speed(cmd, &rc))
7574 rc = -EINVAL;
7575 else if (!rc)
7576 fan_watchdog_reset();
7579 return rc;
7582 static struct ibm_struct fan_driver_data = {
7583 .name = "fan",
7584 .read = fan_read,
7585 .write = fan_write,
7586 .exit = fan_exit,
7587 .suspend = fan_suspend,
7588 .resume = fan_resume,
7591 /****************************************************************************
7592 ****************************************************************************
7594 * Infrastructure
7596 ****************************************************************************
7597 ****************************************************************************/
7599 /* sysfs name ---------------------------------------------------------- */
7600 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7601 struct device_attribute *attr,
7602 char *buf)
7604 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7607 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7608 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7610 /* --------------------------------------------------------------------- */
7612 /* /proc support */
7613 static struct proc_dir_entry *proc_dir;
7616 * Module and infrastructure proble, init and exit handling
7619 static int force_load;
7621 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7622 static const char * __init str_supported(int is_supported)
7624 static char text_unsupported[] __initdata = "not supported";
7626 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7628 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7630 static void ibm_exit(struct ibm_struct *ibm)
7632 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7634 list_del_init(&ibm->all_drivers);
7636 if (ibm->flags.acpi_notify_installed) {
7637 dbg_printk(TPACPI_DBG_EXIT,
7638 "%s: acpi_remove_notify_handler\n", ibm->name);
7639 BUG_ON(!ibm->acpi);
7640 acpi_remove_notify_handler(*ibm->acpi->handle,
7641 ibm->acpi->type,
7642 dispatch_acpi_notify);
7643 ibm->flags.acpi_notify_installed = 0;
7644 ibm->flags.acpi_notify_installed = 0;
7647 if (ibm->flags.proc_created) {
7648 dbg_printk(TPACPI_DBG_EXIT,
7649 "%s: remove_proc_entry\n", ibm->name);
7650 remove_proc_entry(ibm->name, proc_dir);
7651 ibm->flags.proc_created = 0;
7654 if (ibm->flags.acpi_driver_registered) {
7655 dbg_printk(TPACPI_DBG_EXIT,
7656 "%s: acpi_bus_unregister_driver\n", ibm->name);
7657 BUG_ON(!ibm->acpi);
7658 acpi_bus_unregister_driver(ibm->acpi->driver);
7659 kfree(ibm->acpi->driver);
7660 ibm->acpi->driver = NULL;
7661 ibm->flags.acpi_driver_registered = 0;
7664 if (ibm->flags.init_called && ibm->exit) {
7665 ibm->exit();
7666 ibm->flags.init_called = 0;
7669 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7672 static int __init ibm_init(struct ibm_init_struct *iibm)
7674 int ret;
7675 struct ibm_struct *ibm = iibm->data;
7676 struct proc_dir_entry *entry;
7678 BUG_ON(ibm == NULL);
7680 INIT_LIST_HEAD(&ibm->all_drivers);
7682 if (ibm->flags.experimental && !experimental)
7683 return 0;
7685 dbg_printk(TPACPI_DBG_INIT,
7686 "probing for %s\n", ibm->name);
7688 if (iibm->init) {
7689 ret = iibm->init(iibm);
7690 if (ret > 0)
7691 return 0; /* probe failed */
7692 if (ret)
7693 return ret;
7695 ibm->flags.init_called = 1;
7698 if (ibm->acpi) {
7699 if (ibm->acpi->hid) {
7700 ret = register_tpacpi_subdriver(ibm);
7701 if (ret)
7702 goto err_out;
7705 if (ibm->acpi->notify) {
7706 ret = setup_acpi_notify(ibm);
7707 if (ret == -ENODEV) {
7708 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7709 ibm->name);
7710 ret = 0;
7711 goto err_out;
7713 if (ret < 0)
7714 goto err_out;
7718 dbg_printk(TPACPI_DBG_INIT,
7719 "%s installed\n", ibm->name);
7721 if (ibm->read) {
7722 entry = create_proc_entry(ibm->name,
7723 S_IFREG | S_IRUGO | S_IWUSR,
7724 proc_dir);
7725 if (!entry) {
7726 printk(TPACPI_ERR "unable to create proc entry %s\n",
7727 ibm->name);
7728 ret = -ENODEV;
7729 goto err_out;
7731 entry->owner = THIS_MODULE;
7732 entry->data = ibm;
7733 entry->read_proc = &dispatch_procfs_read;
7734 if (ibm->write)
7735 entry->write_proc = &dispatch_procfs_write;
7736 ibm->flags.proc_created = 1;
7739 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7741 return 0;
7743 err_out:
7744 dbg_printk(TPACPI_DBG_INIT,
7745 "%s: at error exit path with result %d\n",
7746 ibm->name, ret);
7748 ibm_exit(ibm);
7749 return (ret < 0)? ret : 0;
7752 /* Probing */
7754 static bool __pure __init tpacpi_is_fw_digit(const char c)
7756 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7759 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7760 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7761 const char t)
7763 return s && strlen(s) >= 8 &&
7764 tpacpi_is_fw_digit(s[0]) &&
7765 tpacpi_is_fw_digit(s[1]) &&
7766 s[2] == t && s[3] == 'T' &&
7767 tpacpi_is_fw_digit(s[4]) &&
7768 tpacpi_is_fw_digit(s[5]) &&
7769 s[6] == 'W' && s[7] == 'W';
7772 /* returns 0 - probe ok, or < 0 - probe error.
7773 * Probe ok doesn't mean thinkpad found.
7774 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7775 static int __must_check __init get_thinkpad_model_data(
7776 struct thinkpad_id_data *tp)
7778 const struct dmi_device *dev = NULL;
7779 char ec_fw_string[18];
7780 char const *s;
7782 if (!tp)
7783 return -EINVAL;
7785 memset(tp, 0, sizeof(*tp));
7787 if (dmi_name_in_vendors("IBM"))
7788 tp->vendor = PCI_VENDOR_ID_IBM;
7789 else if (dmi_name_in_vendors("LENOVO"))
7790 tp->vendor = PCI_VENDOR_ID_LENOVO;
7791 else
7792 return 0;
7794 s = dmi_get_system_info(DMI_BIOS_VERSION);
7795 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7796 if (s && !tp->bios_version_str)
7797 return -ENOMEM;
7799 /* Really ancient ThinkPad 240X will fail this, which is fine */
7800 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7801 return 0;
7803 tp->bios_model = tp->bios_version_str[0]
7804 | (tp->bios_version_str[1] << 8);
7805 tp->bios_release = (tp->bios_version_str[4] << 8)
7806 | tp->bios_version_str[5];
7809 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7810 * X32 or newer, all Z series; Some models must have an
7811 * up-to-date BIOS or they will not be detected.
7813 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7815 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7816 if (sscanf(dev->name,
7817 "IBM ThinkPad Embedded Controller -[%17c",
7818 ec_fw_string) == 1) {
7819 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7820 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7822 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7823 if (!tp->ec_version_str)
7824 return -ENOMEM;
7826 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7827 tp->ec_model = ec_fw_string[0]
7828 | (ec_fw_string[1] << 8);
7829 tp->ec_release = (ec_fw_string[4] << 8)
7830 | ec_fw_string[5];
7831 } else {
7832 printk(TPACPI_NOTICE
7833 "ThinkPad firmware release %s "
7834 "doesn't match the known patterns\n",
7835 ec_fw_string);
7836 printk(TPACPI_NOTICE
7837 "please report this to %s\n",
7838 TPACPI_MAIL);
7840 break;
7844 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7845 if (s && !strnicmp(s, "ThinkPad", 8)) {
7846 tp->model_str = kstrdup(s, GFP_KERNEL);
7847 if (!tp->model_str)
7848 return -ENOMEM;
7851 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7852 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7853 if (s && !tp->nummodel_str)
7854 return -ENOMEM;
7856 return 0;
7859 static int __init probe_for_thinkpad(void)
7861 int is_thinkpad;
7863 if (acpi_disabled)
7864 return -ENODEV;
7867 * Non-ancient models have better DMI tagging, but very old models
7868 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7870 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7871 (thinkpad_id.ec_model != 0) ||
7872 tpacpi_is_fw_known();
7874 /* ec is required because many other handles are relative to it */
7875 TPACPI_ACPIHANDLE_INIT(ec);
7876 if (!ec_handle) {
7877 if (is_thinkpad)
7878 printk(TPACPI_ERR
7879 "Not yet supported ThinkPad detected!\n");
7880 return -ENODEV;
7883 if (!is_thinkpad && !force_load)
7884 return -ENODEV;
7886 return 0;
7890 /* Module init, exit, parameters */
7892 static struct ibm_init_struct ibms_init[] __initdata = {
7894 .init = thinkpad_acpi_driver_init,
7895 .data = &thinkpad_acpi_driver_data,
7898 .init = hotkey_init,
7899 .data = &hotkey_driver_data,
7902 .init = bluetooth_init,
7903 .data = &bluetooth_driver_data,
7906 .init = wan_init,
7907 .data = &wan_driver_data,
7910 .init = uwb_init,
7911 .data = &uwb_driver_data,
7913 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7915 .init = video_init,
7916 .data = &video_driver_data,
7918 #endif
7920 .init = light_init,
7921 .data = &light_driver_data,
7924 .init = cmos_init,
7925 .data = &cmos_driver_data,
7928 .init = led_init,
7929 .data = &led_driver_data,
7932 .init = beep_init,
7933 .data = &beep_driver_data,
7936 .init = thermal_init,
7937 .data = &thermal_driver_data,
7940 .data = &ecdump_driver_data,
7943 .init = brightness_init,
7944 .data = &brightness_driver_data,
7947 .data = &volume_driver_data,
7950 .init = fan_init,
7951 .data = &fan_driver_data,
7955 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7957 unsigned int i;
7958 struct ibm_struct *ibm;
7960 if (!kp || !kp->name || !val)
7961 return -EINVAL;
7963 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7964 ibm = ibms_init[i].data;
7965 WARN_ON(ibm == NULL);
7967 if (!ibm || !ibm->name)
7968 continue;
7970 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7971 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7972 return -ENOSPC;
7973 strcpy(ibms_init[i].param, val);
7974 strcat(ibms_init[i].param, ",");
7975 return 0;
7979 return -EINVAL;
7982 module_param(experimental, int, 0);
7983 MODULE_PARM_DESC(experimental,
7984 "Enables experimental features when non-zero");
7986 module_param_named(debug, dbg_level, uint, 0);
7987 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7989 module_param(force_load, bool, 0);
7990 MODULE_PARM_DESC(force_load,
7991 "Attempts to load the driver even on a "
7992 "mis-identified ThinkPad when true");
7994 module_param_named(fan_control, fan_control_allowed, bool, 0);
7995 MODULE_PARM_DESC(fan_control,
7996 "Enables setting fan parameters features when true");
7998 module_param_named(brightness_mode, brightness_mode, uint, 0);
7999 MODULE_PARM_DESC(brightness_mode,
8000 "Selects brightness control strategy: "
8001 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8003 module_param(brightness_enable, uint, 0);
8004 MODULE_PARM_DESC(brightness_enable,
8005 "Enables backlight control when 1, disables when 0");
8007 module_param(hotkey_report_mode, uint, 0);
8008 MODULE_PARM_DESC(hotkey_report_mode,
8009 "used for backwards compatibility with userspace, "
8010 "see documentation");
8012 #define TPACPI_PARAM(feature) \
8013 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8014 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8015 "at module load, see documentation")
8017 TPACPI_PARAM(hotkey);
8018 TPACPI_PARAM(bluetooth);
8019 TPACPI_PARAM(video);
8020 TPACPI_PARAM(light);
8021 TPACPI_PARAM(cmos);
8022 TPACPI_PARAM(led);
8023 TPACPI_PARAM(beep);
8024 TPACPI_PARAM(ecdump);
8025 TPACPI_PARAM(brightness);
8026 TPACPI_PARAM(volume);
8027 TPACPI_PARAM(fan);
8029 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8030 module_param(dbg_wlswemul, uint, 0);
8031 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8032 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8033 MODULE_PARM_DESC(wlsw_state,
8034 "Initial state of the emulated WLSW switch");
8036 module_param(dbg_bluetoothemul, uint, 0);
8037 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8038 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8039 MODULE_PARM_DESC(bluetooth_state,
8040 "Initial state of the emulated bluetooth switch");
8042 module_param(dbg_wwanemul, uint, 0);
8043 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8044 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8045 MODULE_PARM_DESC(wwan_state,
8046 "Initial state of the emulated WWAN switch");
8048 module_param(dbg_uwbemul, uint, 0);
8049 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8050 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8051 MODULE_PARM_DESC(uwb_state,
8052 "Initial state of the emulated UWB switch");
8053 #endif
8055 static void thinkpad_acpi_module_exit(void)
8057 struct ibm_struct *ibm, *itmp;
8059 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8061 list_for_each_entry_safe_reverse(ibm, itmp,
8062 &tpacpi_all_drivers,
8063 all_drivers) {
8064 ibm_exit(ibm);
8067 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8069 if (tpacpi_inputdev) {
8070 if (tp_features.input_device_registered)
8071 input_unregister_device(tpacpi_inputdev);
8072 else
8073 input_free_device(tpacpi_inputdev);
8076 if (tpacpi_hwmon)
8077 hwmon_device_unregister(tpacpi_hwmon);
8079 if (tp_features.sensors_pdev_attrs_registered)
8080 device_remove_file(&tpacpi_sensors_pdev->dev,
8081 &dev_attr_thinkpad_acpi_pdev_name);
8082 if (tpacpi_sensors_pdev)
8083 platform_device_unregister(tpacpi_sensors_pdev);
8084 if (tpacpi_pdev)
8085 platform_device_unregister(tpacpi_pdev);
8087 if (tp_features.sensors_pdrv_attrs_registered)
8088 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8089 if (tp_features.platform_drv_attrs_registered)
8090 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8092 if (tp_features.sensors_pdrv_registered)
8093 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8095 if (tp_features.platform_drv_registered)
8096 platform_driver_unregister(&tpacpi_pdriver);
8098 if (proc_dir)
8099 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8101 if (tpacpi_wq)
8102 destroy_workqueue(tpacpi_wq);
8104 kfree(thinkpad_id.bios_version_str);
8105 kfree(thinkpad_id.ec_version_str);
8106 kfree(thinkpad_id.model_str);
8110 static int __init thinkpad_acpi_module_init(void)
8112 int ret, i;
8114 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8116 /* Parameter checking */
8117 if (hotkey_report_mode > 2)
8118 return -EINVAL;
8120 /* Driver-level probe */
8122 ret = get_thinkpad_model_data(&thinkpad_id);
8123 if (ret) {
8124 printk(TPACPI_ERR
8125 "unable to get DMI data: %d\n", ret);
8126 thinkpad_acpi_module_exit();
8127 return ret;
8129 ret = probe_for_thinkpad();
8130 if (ret) {
8131 thinkpad_acpi_module_exit();
8132 return ret;
8135 /* Driver initialization */
8137 TPACPI_ACPIHANDLE_INIT(ecrd);
8138 TPACPI_ACPIHANDLE_INIT(ecwr);
8140 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8141 if (!tpacpi_wq) {
8142 thinkpad_acpi_module_exit();
8143 return -ENOMEM;
8146 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8147 if (!proc_dir) {
8148 printk(TPACPI_ERR
8149 "unable to create proc dir " TPACPI_PROC_DIR);
8150 thinkpad_acpi_module_exit();
8151 return -ENODEV;
8153 proc_dir->owner = THIS_MODULE;
8155 ret = platform_driver_register(&tpacpi_pdriver);
8156 if (ret) {
8157 printk(TPACPI_ERR
8158 "unable to register main platform driver\n");
8159 thinkpad_acpi_module_exit();
8160 return ret;
8162 tp_features.platform_drv_registered = 1;
8164 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8165 if (ret) {
8166 printk(TPACPI_ERR
8167 "unable to register hwmon platform driver\n");
8168 thinkpad_acpi_module_exit();
8169 return ret;
8171 tp_features.sensors_pdrv_registered = 1;
8173 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8174 if (!ret) {
8175 tp_features.platform_drv_attrs_registered = 1;
8176 ret = tpacpi_create_driver_attributes(
8177 &tpacpi_hwmon_pdriver.driver);
8179 if (ret) {
8180 printk(TPACPI_ERR
8181 "unable to create sysfs driver attributes\n");
8182 thinkpad_acpi_module_exit();
8183 return ret;
8185 tp_features.sensors_pdrv_attrs_registered = 1;
8188 /* Device initialization */
8189 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8190 NULL, 0);
8191 if (IS_ERR(tpacpi_pdev)) {
8192 ret = PTR_ERR(tpacpi_pdev);
8193 tpacpi_pdev = NULL;
8194 printk(TPACPI_ERR "unable to register platform device\n");
8195 thinkpad_acpi_module_exit();
8196 return ret;
8198 tpacpi_sensors_pdev = platform_device_register_simple(
8199 TPACPI_HWMON_DRVR_NAME,
8200 -1, NULL, 0);
8201 if (IS_ERR(tpacpi_sensors_pdev)) {
8202 ret = PTR_ERR(tpacpi_sensors_pdev);
8203 tpacpi_sensors_pdev = NULL;
8204 printk(TPACPI_ERR
8205 "unable to register hwmon platform device\n");
8206 thinkpad_acpi_module_exit();
8207 return ret;
8209 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8210 &dev_attr_thinkpad_acpi_pdev_name);
8211 if (ret) {
8212 printk(TPACPI_ERR
8213 "unable to create sysfs hwmon device attributes\n");
8214 thinkpad_acpi_module_exit();
8215 return ret;
8217 tp_features.sensors_pdev_attrs_registered = 1;
8218 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8219 if (IS_ERR(tpacpi_hwmon)) {
8220 ret = PTR_ERR(tpacpi_hwmon);
8221 tpacpi_hwmon = NULL;
8222 printk(TPACPI_ERR "unable to register hwmon device\n");
8223 thinkpad_acpi_module_exit();
8224 return ret;
8226 mutex_init(&tpacpi_inputdev_send_mutex);
8227 tpacpi_inputdev = input_allocate_device();
8228 if (!tpacpi_inputdev) {
8229 printk(TPACPI_ERR "unable to allocate input device\n");
8230 thinkpad_acpi_module_exit();
8231 return -ENOMEM;
8232 } else {
8233 /* Prepare input device, but don't register */
8234 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8235 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8236 tpacpi_inputdev->id.bustype = BUS_HOST;
8237 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8238 thinkpad_id.vendor :
8239 PCI_VENDOR_ID_IBM;
8240 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8241 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8243 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8244 ret = ibm_init(&ibms_init[i]);
8245 if (ret >= 0 && *ibms_init[i].param)
8246 ret = ibms_init[i].data->write(ibms_init[i].param);
8247 if (ret < 0) {
8248 thinkpad_acpi_module_exit();
8249 return ret;
8252 ret = input_register_device(tpacpi_inputdev);
8253 if (ret < 0) {
8254 printk(TPACPI_ERR "unable to register input device\n");
8255 thinkpad_acpi_module_exit();
8256 return ret;
8257 } else {
8258 tp_features.input_device_registered = 1;
8261 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8262 return 0;
8265 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8268 * This will autoload the driver in almost every ThinkPad
8269 * in widespread use.
8271 * Only _VERY_ old models, like the 240, 240x and 570 lack
8272 * the HKEY event interface.
8274 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8277 * DMI matching for module autoloading
8279 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8280 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8282 * Only models listed in thinkwiki will be supported, so add yours
8283 * if it is not there yet.
8285 #define IBM_BIOS_MODULE_ALIAS(__type) \
8286 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8288 /* Ancient thinkpad BIOSes have to be identified by
8289 * BIOS type or model number, and there are far less
8290 * BIOS types than model numbers... */
8291 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8293 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8294 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8295 MODULE_DESCRIPTION(TPACPI_DESC);
8296 MODULE_VERSION(TPACPI_VERSION);
8297 MODULE_LICENSE("GPL");
8299 module_init(thinkpad_acpi_module_init);
8300 module_exit(thinkpad_acpi_module_exit);