thinkpad-acpi: report brightness events when required
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobe03359d897fe4c5fc5aefaf55fd259c123590324
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020400
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
194 #define TPACPI_DBG_FAN 0x0010
195 #define TPACPI_DBG_BRGHT 0x0020
197 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
198 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
199 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
202 /****************************************************************************
203 * Driver-wide structs and misc. variables
206 struct ibm_struct;
208 struct tp_acpi_drv_struct {
209 const struct acpi_device_id *hid;
210 struct acpi_driver *driver;
212 void (*notify) (struct ibm_struct *, u32);
213 acpi_handle *handle;
214 u32 type;
215 struct acpi_device *device;
218 struct ibm_struct {
219 char *name;
221 int (*read) (char *);
222 int (*write) (char *);
223 void (*exit) (void);
224 void (*resume) (void);
225 void (*suspend) (pm_message_t state);
226 void (*shutdown) (void);
228 struct list_head all_drivers;
230 struct tp_acpi_drv_struct *acpi;
232 struct {
233 u8 acpi_driver_registered:1;
234 u8 acpi_notify_installed:1;
235 u8 proc_created:1;
236 u8 init_called:1;
237 u8 experimental:1;
238 } flags;
241 struct ibm_init_struct {
242 char param[32];
244 int (*init) (struct ibm_init_struct *);
245 struct ibm_struct *data;
248 static struct {
249 u32 bluetooth:1;
250 u32 hotkey:1;
251 u32 hotkey_mask:1;
252 u32 hotkey_wlsw:1;
253 u32 hotkey_tablet:1;
254 u32 light:1;
255 u32 light_status:1;
256 u32 bright_16levels:1;
257 u32 bright_acpimode:1;
258 u32 wan:1;
259 u32 uwb:1;
260 u32 fan_ctrl_status_undef:1;
261 u32 second_fan:1;
262 u32 beep_needs_two_args:1;
263 u32 input_device_registered:1;
264 u32 platform_drv_registered:1;
265 u32 platform_drv_attrs_registered:1;
266 u32 sensors_pdrv_registered:1;
267 u32 sensors_pdrv_attrs_registered:1;
268 u32 sensors_pdev_attrs_registered:1;
269 u32 hotkey_poll_active:1;
270 } tp_features;
272 static struct {
273 u16 hotkey_mask_ff:1;
274 } tp_warned;
276 struct thinkpad_id_data {
277 unsigned int vendor; /* ThinkPad vendor:
278 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
280 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
281 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
283 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
284 u16 ec_model;
285 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
286 u16 ec_release;
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
293 static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 enum led_status_t {
305 TPACPI_LED_OFF = 0,
306 TPACPI_LED_ON,
307 TPACPI_LED_BLINK,
310 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev {
312 struct led_classdev led_classdev;
313 struct work_struct work;
314 enum led_status_t new_state;
315 unsigned int led;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul;
320 static int tpacpi_wlsw_emulstate;
321 static int dbg_bluetoothemul;
322 static int tpacpi_bluetooth_emulstate;
323 static int dbg_wwanemul;
324 static int tpacpi_wwan_emulstate;
325 static int dbg_uwbemul;
326 static int tpacpi_uwb_emulstate;
327 #endif
330 /*************************************************************************
331 * Debugging helpers
334 #define dbg_printk(a_dbg_level, format, arg...) \
335 do { if (dbg_level & (a_dbg_level)) \
336 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
337 } while (0)
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 static const char *str_supported(int is_supported);
342 #else
343 #define vdbg_printk(a_dbg_level, format, arg...) \
344 do { } while (0)
345 #endif
347 static void tpacpi_log_usertask(const char * const what)
349 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
350 what, task_tgid_vnr(current));
353 #define tpacpi_disclose_usertask(what, format, arg...) \
354 do { \
355 if (unlikely( \
356 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
357 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
358 printk(TPACPI_DEBUG "%s: PID %d: " format, \
359 what, task_tgid_vnr(current), ## arg); \
361 } while (0)
364 * Quirk handling helpers
366 * ThinkPad IDs and versions seen in the field so far
367 * are two-characters from the set [0-9A-Z], i.e. base 36.
369 * We use values well outside that range as specials.
372 #define TPACPI_MATCH_ANY 0xffffU
373 #define TPACPI_MATCH_UNKNOWN 0U
375 /* TPID('1', 'Y') == 0x5931 */
376 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
378 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
379 { .vendor = PCI_VENDOR_ID_IBM, \
380 .bios = TPID(__id1, __id2), \
381 .ec = TPACPI_MATCH_ANY, \
382 .quirks = (__quirk) }
384 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
385 { .vendor = PCI_VENDOR_ID_LENOVO, \
386 .bios = TPID(__id1, __id2), \
387 .ec = TPACPI_MATCH_ANY, \
388 .quirks = (__quirk) }
390 struct tpacpi_quirk {
391 unsigned int vendor;
392 u16 bios;
393 u16 ec;
394 unsigned long quirks;
398 * tpacpi_check_quirks() - search BIOS/EC version on a list
399 * @qlist: array of &struct tpacpi_quirk
400 * @qlist_size: number of elements in @qlist
402 * Iterates over a quirks list until one is found that matches the
403 * ThinkPad's vendor, BIOS and EC model.
405 * Returns 0 if nothing matches, otherwise returns the quirks field of
406 * the matching &struct tpacpi_quirk entry.
408 * The match criteria is: vendor, ec and bios much match.
410 static unsigned long __init tpacpi_check_quirks(
411 const struct tpacpi_quirk *qlist,
412 unsigned int qlist_size)
414 while (qlist_size) {
415 if ((qlist->vendor == thinkpad_id.vendor ||
416 qlist->vendor == TPACPI_MATCH_ANY) &&
417 (qlist->bios == thinkpad_id.bios_model ||
418 qlist->bios == TPACPI_MATCH_ANY) &&
419 (qlist->ec == thinkpad_id.ec_model ||
420 qlist->ec == TPACPI_MATCH_ANY))
421 return qlist->quirks;
423 qlist_size--;
424 qlist++;
426 return 0;
430 /****************************************************************************
431 ****************************************************************************
433 * ACPI Helpers and device model
435 ****************************************************************************
436 ****************************************************************************/
438 /*************************************************************************
439 * ACPI basic handles
442 static acpi_handle root_handle;
444 #define TPACPI_HANDLE(object, parent, paths...) \
445 static acpi_handle object##_handle; \
446 static acpi_handle *object##_parent = &parent##_handle; \
447 static char *object##_path; \
448 static char *object##_paths[] = { paths }
450 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
451 "\\_SB.PCI.ISA.EC", /* 570 */
452 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
453 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
454 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
455 "\\_SB.PCI0.ICH3.EC0", /* R31 */
456 "\\_SB.PCI0.LPC.EC", /* all others */
459 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
460 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
462 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
463 /* T4x, X31, X40 */
464 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
465 "\\CMS", /* R40, R40e */
466 ); /* all others */
468 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
469 "^HKEY", /* R30, R31 */
470 "HKEY", /* all others */
471 ); /* 570 */
473 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
474 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
475 "\\_SB.PCI0.VID0", /* 770e */
476 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
477 "\\_SB.PCI0.AGP.VID", /* all others */
478 ); /* R30, R31 */
481 /*************************************************************************
482 * ACPI helpers
485 static int acpi_evalf(acpi_handle handle,
486 void *res, char *method, char *fmt, ...)
488 char *fmt0 = fmt;
489 struct acpi_object_list params;
490 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
491 struct acpi_buffer result, *resultp;
492 union acpi_object out_obj;
493 acpi_status status;
494 va_list ap;
495 char res_type;
496 int success;
497 int quiet;
499 if (!*fmt) {
500 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
501 return 0;
504 if (*fmt == 'q') {
505 quiet = 1;
506 fmt++;
507 } else
508 quiet = 0;
510 res_type = *(fmt++);
512 params.count = 0;
513 params.pointer = &in_objs[0];
515 va_start(ap, fmt);
516 while (*fmt) {
517 char c = *(fmt++);
518 switch (c) {
519 case 'd': /* int */
520 in_objs[params.count].integer.value = va_arg(ap, int);
521 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
522 break;
523 /* add more types as needed */
524 default:
525 printk(TPACPI_ERR "acpi_evalf() called "
526 "with invalid format character '%c'\n", c);
527 return 0;
530 va_end(ap);
532 if (res_type != 'v') {
533 result.length = sizeof(out_obj);
534 result.pointer = &out_obj;
535 resultp = &result;
536 } else
537 resultp = NULL;
539 status = acpi_evaluate_object(handle, method, &params, resultp);
541 switch (res_type) {
542 case 'd': /* int */
543 if (res)
544 *(int *)res = out_obj.integer.value;
545 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
546 break;
547 case 'v': /* void */
548 success = status == AE_OK;
549 break;
550 /* add more types as needed */
551 default:
552 printk(TPACPI_ERR "acpi_evalf() called "
553 "with invalid format character '%c'\n", res_type);
554 return 0;
557 if (!success && !quiet)
558 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
559 method, fmt0, status);
561 return success;
564 static int acpi_ec_read(int i, u8 *p)
566 int v;
568 if (ecrd_handle) {
569 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
570 return 0;
571 *p = v;
572 } else {
573 if (ec_read(i, p) < 0)
574 return 0;
577 return 1;
580 static int acpi_ec_write(int i, u8 v)
582 if (ecwr_handle) {
583 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
584 return 0;
585 } else {
586 if (ec_write(i, v) < 0)
587 return 0;
590 return 1;
593 static int issue_thinkpad_cmos_command(int cmos_cmd)
595 if (!cmos_handle)
596 return -ENXIO;
598 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
599 return -EIO;
601 return 0;
604 /*************************************************************************
605 * ACPI device model
608 #define TPACPI_ACPIHANDLE_INIT(object) \
609 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
610 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
612 static void drv_acpi_handle_init(char *name,
613 acpi_handle *handle, acpi_handle parent,
614 char **paths, int num_paths, char **path)
616 int i;
617 acpi_status status;
619 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
620 name);
622 for (i = 0; i < num_paths; i++) {
623 status = acpi_get_handle(parent, paths[i], handle);
624 if (ACPI_SUCCESS(status)) {
625 *path = paths[i];
626 dbg_printk(TPACPI_DBG_INIT,
627 "Found ACPI handle %s for %s\n",
628 *path, name);
629 return;
633 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
634 name);
635 *handle = NULL;
638 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
640 struct ibm_struct *ibm = data;
642 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
643 return;
645 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
646 return;
648 ibm->acpi->notify(ibm, event);
651 static int __init setup_acpi_notify(struct ibm_struct *ibm)
653 acpi_status status;
654 int rc;
656 BUG_ON(!ibm->acpi);
658 if (!*ibm->acpi->handle)
659 return 0;
661 vdbg_printk(TPACPI_DBG_INIT,
662 "setting up ACPI notify for %s\n", ibm->name);
664 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
665 if (rc < 0) {
666 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
667 ibm->name, rc);
668 return -ENODEV;
671 ibm->acpi->device->driver_data = ibm;
672 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
673 TPACPI_ACPI_EVENT_PREFIX,
674 ibm->name);
676 status = acpi_install_notify_handler(*ibm->acpi->handle,
677 ibm->acpi->type, dispatch_acpi_notify, ibm);
678 if (ACPI_FAILURE(status)) {
679 if (status == AE_ALREADY_EXISTS) {
680 printk(TPACPI_NOTICE
681 "another device driver is already "
682 "handling %s events\n", ibm->name);
683 } else {
684 printk(TPACPI_ERR
685 "acpi_install_notify_handler(%s) failed: %d\n",
686 ibm->name, status);
688 return -ENODEV;
690 ibm->flags.acpi_notify_installed = 1;
691 return 0;
694 static int __init tpacpi_device_add(struct acpi_device *device)
696 return 0;
699 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
701 int rc;
703 dbg_printk(TPACPI_DBG_INIT,
704 "registering %s as an ACPI driver\n", ibm->name);
706 BUG_ON(!ibm->acpi);
708 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
709 if (!ibm->acpi->driver) {
710 printk(TPACPI_ERR
711 "failed to allocate memory for ibm->acpi->driver\n");
712 return -ENOMEM;
715 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
716 ibm->acpi->driver->ids = ibm->acpi->hid;
718 ibm->acpi->driver->ops.add = &tpacpi_device_add;
720 rc = acpi_bus_register_driver(ibm->acpi->driver);
721 if (rc < 0) {
722 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
723 ibm->name, rc);
724 kfree(ibm->acpi->driver);
725 ibm->acpi->driver = NULL;
726 } else if (!rc)
727 ibm->flags.acpi_driver_registered = 1;
729 return rc;
733 /****************************************************************************
734 ****************************************************************************
736 * Procfs Helpers
738 ****************************************************************************
739 ****************************************************************************/
741 static int dispatch_procfs_read(char *page, char **start, off_t off,
742 int count, int *eof, void *data)
744 struct ibm_struct *ibm = data;
745 int len;
747 if (!ibm || !ibm->read)
748 return -EINVAL;
750 len = ibm->read(page);
751 if (len < 0)
752 return len;
754 if (len <= off + count)
755 *eof = 1;
756 *start = page + off;
757 len -= off;
758 if (len > count)
759 len = count;
760 if (len < 0)
761 len = 0;
763 return len;
766 static int dispatch_procfs_write(struct file *file,
767 const char __user *userbuf,
768 unsigned long count, void *data)
770 struct ibm_struct *ibm = data;
771 char *kernbuf;
772 int ret;
774 if (!ibm || !ibm->write)
775 return -EINVAL;
776 if (count > PAGE_SIZE - 2)
777 return -EINVAL;
779 kernbuf = kmalloc(count + 2, GFP_KERNEL);
780 if (!kernbuf)
781 return -ENOMEM;
783 if (copy_from_user(kernbuf, userbuf, count)) {
784 kfree(kernbuf);
785 return -EFAULT;
788 kernbuf[count] = 0;
789 strcat(kernbuf, ",");
790 ret = ibm->write(kernbuf);
791 if (ret == 0)
792 ret = count;
794 kfree(kernbuf);
796 return ret;
799 static char *next_cmd(char **cmds)
801 char *start = *cmds;
802 char *end;
804 while ((end = strchr(start, ',')) && end == start)
805 start = end + 1;
807 if (!end)
808 return NULL;
810 *end = 0;
811 *cmds = end + 1;
812 return start;
816 /****************************************************************************
817 ****************************************************************************
819 * Device model: input, hwmon and platform
821 ****************************************************************************
822 ****************************************************************************/
824 static struct platform_device *tpacpi_pdev;
825 static struct platform_device *tpacpi_sensors_pdev;
826 static struct device *tpacpi_hwmon;
827 static struct input_dev *tpacpi_inputdev;
828 static struct mutex tpacpi_inputdev_send_mutex;
829 static LIST_HEAD(tpacpi_all_drivers);
831 static int tpacpi_suspend_handler(struct platform_device *pdev,
832 pm_message_t state)
834 struct ibm_struct *ibm, *itmp;
836 list_for_each_entry_safe(ibm, itmp,
837 &tpacpi_all_drivers,
838 all_drivers) {
839 if (ibm->suspend)
840 (ibm->suspend)(state);
843 return 0;
846 static int tpacpi_resume_handler(struct platform_device *pdev)
848 struct ibm_struct *ibm, *itmp;
850 list_for_each_entry_safe(ibm, itmp,
851 &tpacpi_all_drivers,
852 all_drivers) {
853 if (ibm->resume)
854 (ibm->resume)();
857 return 0;
860 static void tpacpi_shutdown_handler(struct platform_device *pdev)
862 struct ibm_struct *ibm, *itmp;
864 list_for_each_entry_safe(ibm, itmp,
865 &tpacpi_all_drivers,
866 all_drivers) {
867 if (ibm->shutdown)
868 (ibm->shutdown)();
872 static struct platform_driver tpacpi_pdriver = {
873 .driver = {
874 .name = TPACPI_DRVR_NAME,
875 .owner = THIS_MODULE,
877 .suspend = tpacpi_suspend_handler,
878 .resume = tpacpi_resume_handler,
879 .shutdown = tpacpi_shutdown_handler,
882 static struct platform_driver tpacpi_hwmon_pdriver = {
883 .driver = {
884 .name = TPACPI_HWMON_DRVR_NAME,
885 .owner = THIS_MODULE,
889 /*************************************************************************
890 * sysfs support helpers
893 struct attribute_set {
894 unsigned int members, max_members;
895 struct attribute_group group;
898 struct attribute_set_obj {
899 struct attribute_set s;
900 struct attribute *a;
901 } __attribute__((packed));
903 static struct attribute_set *create_attr_set(unsigned int max_members,
904 const char *name)
906 struct attribute_set_obj *sobj;
908 if (max_members == 0)
909 return NULL;
911 /* Allocates space for implicit NULL at the end too */
912 sobj = kzalloc(sizeof(struct attribute_set_obj) +
913 max_members * sizeof(struct attribute *),
914 GFP_KERNEL);
915 if (!sobj)
916 return NULL;
917 sobj->s.max_members = max_members;
918 sobj->s.group.attrs = &sobj->a;
919 sobj->s.group.name = name;
921 return &sobj->s;
924 #define destroy_attr_set(_set) \
925 kfree(_set);
927 /* not multi-threaded safe, use it in a single thread per set */
928 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
930 if (!s || !attr)
931 return -EINVAL;
933 if (s->members >= s->max_members)
934 return -ENOMEM;
936 s->group.attrs[s->members] = attr;
937 s->members++;
939 return 0;
942 static int add_many_to_attr_set(struct attribute_set *s,
943 struct attribute **attr,
944 unsigned int count)
946 int i, res;
948 for (i = 0; i < count; i++) {
949 res = add_to_attr_set(s, attr[i]);
950 if (res)
951 return res;
954 return 0;
957 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
959 sysfs_remove_group(kobj, &s->group);
960 destroy_attr_set(s);
963 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
964 sysfs_create_group(_kobj, &_attr_set->group)
966 static int parse_strtoul(const char *buf,
967 unsigned long max, unsigned long *value)
969 char *endp;
971 while (*buf && isspace(*buf))
972 buf++;
973 *value = simple_strtoul(buf, &endp, 0);
974 while (*endp && isspace(*endp))
975 endp++;
976 if (*endp || *value > max)
977 return -EINVAL;
979 return 0;
982 static void tpacpi_disable_brightness_delay(void)
984 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
985 printk(TPACPI_NOTICE
986 "ACPI backlight control delay disabled\n");
989 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
991 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
992 union acpi_object *obj;
993 int rc;
995 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
996 obj = (union acpi_object *)buffer.pointer;
997 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
998 printk(TPACPI_ERR "Unknown _BCL data, "
999 "please report this to %s\n", TPACPI_MAIL);
1000 rc = 0;
1001 } else {
1002 rc = obj->package.count;
1004 } else {
1005 return 0;
1008 kfree(buffer.pointer);
1009 return rc;
1012 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1013 u32 lvl, void *context, void **rv)
1015 char name[ACPI_PATH_SEGMENT_LENGTH];
1016 struct acpi_buffer buffer = { sizeof(name), &name };
1018 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1019 !strncmp("_BCL", name, sizeof(name) - 1)) {
1020 BUG_ON(!rv || !*rv);
1021 **(int **)rv = tpacpi_query_bcl_levels(handle);
1022 return AE_CTRL_TERMINATE;
1023 } else {
1024 return AE_OK;
1029 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1031 static int __init tpacpi_check_std_acpi_brightness_support(void)
1033 int status;
1034 int bcl_levels = 0;
1035 void *bcl_ptr = &bcl_levels;
1037 if (!vid_handle) {
1038 TPACPI_ACPIHANDLE_INIT(vid);
1040 if (!vid_handle)
1041 return 0;
1044 * Search for a _BCL method, and execute it. This is safe on all
1045 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1046 * BIOS in ACPI backlight control mode. We do NOT have to care
1047 * about calling the _BCL method in an enabled video device, any
1048 * will do for our purposes.
1051 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1052 tpacpi_acpi_walk_find_bcl, NULL,
1053 &bcl_ptr);
1055 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1056 tp_features.bright_acpimode = 1;
1057 return (bcl_levels - 2);
1060 return 0;
1063 static int __init tpacpi_new_rfkill(const unsigned int id,
1064 struct rfkill **rfk,
1065 const enum rfkill_type rfktype,
1066 const char *name,
1067 const bool set_default,
1068 int (*toggle_radio)(void *, enum rfkill_state),
1069 int (*get_state)(void *, enum rfkill_state *))
1071 int res;
1072 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1074 res = get_state(NULL, &initial_state);
1075 if (res < 0) {
1076 printk(TPACPI_ERR
1077 "failed to read initial state for %s, error %d; "
1078 "will turn radio off\n", name, res);
1079 } else if (set_default) {
1080 /* try to set the initial state as the default for the rfkill
1081 * type, since we ask the firmware to preserve it across S5 in
1082 * NVRAM */
1083 if (rfkill_set_default(rfktype,
1084 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1085 RFKILL_STATE_UNBLOCKED :
1086 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1087 vdbg_printk(TPACPI_DBG_RFKILL,
1088 "Default state for %s cannot be changed\n",
1089 name);
1092 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1093 if (!*rfk) {
1094 printk(TPACPI_ERR
1095 "failed to allocate memory for rfkill class\n");
1096 return -ENOMEM;
1099 (*rfk)->name = name;
1100 (*rfk)->get_state = get_state;
1101 (*rfk)->toggle_radio = toggle_radio;
1102 (*rfk)->state = initial_state;
1104 res = rfkill_register(*rfk);
1105 if (res < 0) {
1106 printk(TPACPI_ERR
1107 "failed to register %s rfkill switch: %d\n",
1108 name, res);
1109 rfkill_free(*rfk);
1110 *rfk = NULL;
1111 return res;
1114 return 0;
1117 static void printk_deprecated_attribute(const char * const what,
1118 const char * const details)
1120 tpacpi_log_usertask("deprecated sysfs attribute");
1121 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1122 "will be removed. %s\n",
1123 what, details);
1126 static void printk_deprecated_rfkill_attribute(const char * const what)
1128 printk_deprecated_attribute(what,
1129 "Please switch to generic rfkill before year 2010");
1132 /*************************************************************************
1133 * thinkpad-acpi driver attributes
1136 /* interface_version --------------------------------------------------- */
1137 static ssize_t tpacpi_driver_interface_version_show(
1138 struct device_driver *drv,
1139 char *buf)
1141 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1144 static DRIVER_ATTR(interface_version, S_IRUGO,
1145 tpacpi_driver_interface_version_show, NULL);
1147 /* debug_level --------------------------------------------------------- */
1148 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1149 char *buf)
1151 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1154 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1155 const char *buf, size_t count)
1157 unsigned long t;
1159 if (parse_strtoul(buf, 0xffff, &t))
1160 return -EINVAL;
1162 dbg_level = t;
1164 return count;
1167 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1168 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1170 /* version ------------------------------------------------------------- */
1171 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1172 char *buf)
1174 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1175 TPACPI_DESC, TPACPI_VERSION);
1178 static DRIVER_ATTR(version, S_IRUGO,
1179 tpacpi_driver_version_show, NULL);
1181 /* --------------------------------------------------------------------- */
1183 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1185 static void tpacpi_send_radiosw_update(void);
1187 /* wlsw_emulstate ------------------------------------------------------ */
1188 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1189 char *buf)
1191 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1194 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1195 const char *buf, size_t count)
1197 unsigned long t;
1199 if (parse_strtoul(buf, 1, &t))
1200 return -EINVAL;
1202 if (tpacpi_wlsw_emulstate != t) {
1203 tpacpi_wlsw_emulstate = !!t;
1204 tpacpi_send_radiosw_update();
1205 } else
1206 tpacpi_wlsw_emulstate = !!t;
1208 return count;
1211 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1212 tpacpi_driver_wlsw_emulstate_show,
1213 tpacpi_driver_wlsw_emulstate_store);
1215 /* bluetooth_emulstate ------------------------------------------------- */
1216 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1217 struct device_driver *drv,
1218 char *buf)
1220 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1223 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1224 struct device_driver *drv,
1225 const char *buf, size_t count)
1227 unsigned long t;
1229 if (parse_strtoul(buf, 1, &t))
1230 return -EINVAL;
1232 tpacpi_bluetooth_emulstate = !!t;
1234 return count;
1237 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1238 tpacpi_driver_bluetooth_emulstate_show,
1239 tpacpi_driver_bluetooth_emulstate_store);
1241 /* wwan_emulstate ------------------------------------------------- */
1242 static ssize_t tpacpi_driver_wwan_emulstate_show(
1243 struct device_driver *drv,
1244 char *buf)
1246 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1249 static ssize_t tpacpi_driver_wwan_emulstate_store(
1250 struct device_driver *drv,
1251 const char *buf, size_t count)
1253 unsigned long t;
1255 if (parse_strtoul(buf, 1, &t))
1256 return -EINVAL;
1258 tpacpi_wwan_emulstate = !!t;
1260 return count;
1263 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1264 tpacpi_driver_wwan_emulstate_show,
1265 tpacpi_driver_wwan_emulstate_store);
1267 /* uwb_emulstate ------------------------------------------------- */
1268 static ssize_t tpacpi_driver_uwb_emulstate_show(
1269 struct device_driver *drv,
1270 char *buf)
1272 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1275 static ssize_t tpacpi_driver_uwb_emulstate_store(
1276 struct device_driver *drv,
1277 const char *buf, size_t count)
1279 unsigned long t;
1281 if (parse_strtoul(buf, 1, &t))
1282 return -EINVAL;
1284 tpacpi_uwb_emulstate = !!t;
1286 return count;
1289 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1290 tpacpi_driver_uwb_emulstate_show,
1291 tpacpi_driver_uwb_emulstate_store);
1292 #endif
1294 /* --------------------------------------------------------------------- */
1296 static struct driver_attribute *tpacpi_driver_attributes[] = {
1297 &driver_attr_debug_level, &driver_attr_version,
1298 &driver_attr_interface_version,
1301 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1303 int i, res;
1305 i = 0;
1306 res = 0;
1307 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1308 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1309 i++;
1312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1313 if (!res && dbg_wlswemul)
1314 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1315 if (!res && dbg_bluetoothemul)
1316 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1317 if (!res && dbg_wwanemul)
1318 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1319 if (!res && dbg_uwbemul)
1320 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1321 #endif
1323 return res;
1326 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1328 int i;
1330 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1331 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1333 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1334 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1335 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1336 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1337 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1338 #endif
1341 /*************************************************************************
1342 * Firmware Data
1346 * Table of recommended minimum BIOS versions
1348 * Reasons for listing:
1349 * 1. Stable BIOS, listed because the unknown ammount of
1350 * bugs and bad ACPI behaviour on older versions
1352 * 2. BIOS or EC fw with known bugs that trigger on Linux
1354 * 3. BIOS with known reduced functionality in older versions
1356 * We recommend the latest BIOS and EC version.
1357 * We only support the latest BIOS and EC fw version as a rule.
1359 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1360 * Information from users in ThinkWiki
1362 * WARNING: we use this table also to detect that the machine is
1363 * a ThinkPad in some cases, so don't remove entries lightly.
1366 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1367 { .vendor = (__v), \
1368 .bios = TPID(__id1, __id2), \
1369 .ec = TPACPI_MATCH_ANY, \
1370 .quirks = TPACPI_MATCH_ANY << 16 \
1371 | (__bv1) << 8 | (__bv2) }
1373 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1374 __eid1, __eid2, __ev1, __ev2) \
1375 { .vendor = (__v), \
1376 .bios = TPID(__bid1, __bid2), \
1377 .ec = TPID(__eid1, __eid2), \
1378 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1379 | (__bv1) << 8 | (__bv2) }
1381 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1382 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1384 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1385 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1386 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1388 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1389 __eid1, __eid2, __ev1, __ev2) \
1390 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1391 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1393 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1394 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1396 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1397 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1398 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1400 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1401 __eid1, __eid2, __ev1, __ev2) \
1402 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1403 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1405 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1406 /* Numeric models ------------------ */
1407 /* FW MODEL BIOS VERS */
1408 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1409 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1410 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1411 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1412 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1413 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1414 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1415 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1416 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1418 /* A-series ------------------------- */
1419 /* FW MODEL BIOS VERS EC VERS */
1420 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1421 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1422 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1423 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1424 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1425 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1426 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1427 TPV_QI0('1', '3', '2', '0'), /* A22m */
1428 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1429 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1430 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1432 /* G-series ------------------------- */
1433 /* FW MODEL BIOS VERS */
1434 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1435 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1437 /* R-series, T-series --------------- */
1438 /* FW MODEL BIOS VERS EC VERS */
1439 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1440 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1441 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1442 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1443 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1444 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1445 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1446 T40/p, T41/p, T42/p (1) */
1447 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1448 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1449 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1450 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1452 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1453 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1454 TPV_QI0('1', '6', '3', '2'), /* T22 */
1455 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1456 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1457 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1459 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1460 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1461 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1463 /* BIOS FW BIOS VERS EC FW EC VERS */
1464 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1465 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1467 /* X-series ------------------------- */
1468 /* FW MODEL BIOS VERS EC VERS */
1469 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1470 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1471 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1472 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1473 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1474 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1475 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1477 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1478 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1480 /* (0) - older versions lack DMI EC fw string and functionality */
1481 /* (1) - older versions known to lack functionality */
1484 #undef TPV_QL1
1485 #undef TPV_QL0
1486 #undef TPV_QI2
1487 #undef TPV_QI1
1488 #undef TPV_QI0
1489 #undef TPV_Q_X
1490 #undef TPV_Q
1492 static void __init tpacpi_check_outdated_fw(void)
1494 unsigned long fwvers;
1495 u16 ec_version, bios_version;
1497 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1498 ARRAY_SIZE(tpacpi_bios_version_qtable));
1500 if (!fwvers)
1501 return;
1503 bios_version = fwvers & 0xffffU;
1504 ec_version = (fwvers >> 16) & 0xffffU;
1506 /* note that unknown versions are set to 0x0000 and we use that */
1507 if ((bios_version > thinkpad_id.bios_release) ||
1508 (ec_version > thinkpad_id.ec_release &&
1509 ec_version != TPACPI_MATCH_ANY)) {
1511 * The changelogs would let us track down the exact
1512 * reason, but it is just too much of a pain to track
1513 * it. We only list BIOSes that are either really
1514 * broken, or really stable to begin with, so it is
1515 * best if the user upgrades the firmware anyway.
1517 printk(TPACPI_WARN
1518 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1519 printk(TPACPI_WARN
1520 "WARNING: This firmware may be missing critical bug "
1521 "fixes and/or important features\n");
1525 static bool __init tpacpi_is_fw_known(void)
1527 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1528 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1531 /****************************************************************************
1532 ****************************************************************************
1534 * Subdrivers
1536 ****************************************************************************
1537 ****************************************************************************/
1539 /*************************************************************************
1540 * thinkpad-acpi init subdriver
1543 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1545 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1546 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1548 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1549 (thinkpad_id.bios_version_str) ?
1550 thinkpad_id.bios_version_str : "unknown",
1551 (thinkpad_id.ec_version_str) ?
1552 thinkpad_id.ec_version_str : "unknown");
1554 if (thinkpad_id.vendor && thinkpad_id.model_str)
1555 printk(TPACPI_INFO "%s %s, model %s\n",
1556 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1557 "IBM" : ((thinkpad_id.vendor ==
1558 PCI_VENDOR_ID_LENOVO) ?
1559 "Lenovo" : "Unknown vendor"),
1560 thinkpad_id.model_str,
1561 (thinkpad_id.nummodel_str) ?
1562 thinkpad_id.nummodel_str : "unknown");
1564 tpacpi_check_outdated_fw();
1565 return 0;
1568 static int thinkpad_acpi_driver_read(char *p)
1570 int len = 0;
1572 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1573 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1575 return len;
1578 static struct ibm_struct thinkpad_acpi_driver_data = {
1579 .name = "driver",
1580 .read = thinkpad_acpi_driver_read,
1583 /*************************************************************************
1584 * Hotkey subdriver
1587 enum { /* hot key scan codes (derived from ACPI DSDT) */
1588 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1589 TP_ACPI_HOTKEYSCAN_FNF2,
1590 TP_ACPI_HOTKEYSCAN_FNF3,
1591 TP_ACPI_HOTKEYSCAN_FNF4,
1592 TP_ACPI_HOTKEYSCAN_FNF5,
1593 TP_ACPI_HOTKEYSCAN_FNF6,
1594 TP_ACPI_HOTKEYSCAN_FNF7,
1595 TP_ACPI_HOTKEYSCAN_FNF8,
1596 TP_ACPI_HOTKEYSCAN_FNF9,
1597 TP_ACPI_HOTKEYSCAN_FNF10,
1598 TP_ACPI_HOTKEYSCAN_FNF11,
1599 TP_ACPI_HOTKEYSCAN_FNF12,
1600 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1601 TP_ACPI_HOTKEYSCAN_FNINSERT,
1602 TP_ACPI_HOTKEYSCAN_FNDELETE,
1603 TP_ACPI_HOTKEYSCAN_FNHOME,
1604 TP_ACPI_HOTKEYSCAN_FNEND,
1605 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1606 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1607 TP_ACPI_HOTKEYSCAN_FNSPACE,
1608 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1609 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1610 TP_ACPI_HOTKEYSCAN_MUTE,
1611 TP_ACPI_HOTKEYSCAN_THINKPAD,
1614 enum { /* Keys available through NVRAM polling */
1615 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1616 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1619 enum { /* Positions of some of the keys in hotkey masks */
1620 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1621 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1622 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1623 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1624 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1625 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1626 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1627 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1628 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1629 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1630 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1633 enum { /* NVRAM to ACPI HKEY group map */
1634 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1635 TP_ACPI_HKEY_ZOOM_MASK |
1636 TP_ACPI_HKEY_DISPSWTCH_MASK |
1637 TP_ACPI_HKEY_HIBERNATE_MASK,
1638 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1639 TP_ACPI_HKEY_BRGHTDWN_MASK,
1640 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1641 TP_ACPI_HKEY_VOLDWN_MASK |
1642 TP_ACPI_HKEY_MUTE_MASK,
1645 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1646 struct tp_nvram_state {
1647 u16 thinkpad_toggle:1;
1648 u16 zoom_toggle:1;
1649 u16 display_toggle:1;
1650 u16 thinklight_toggle:1;
1651 u16 hibernate_toggle:1;
1652 u16 displayexp_toggle:1;
1653 u16 display_state:1;
1654 u16 brightness_toggle:1;
1655 u16 volume_toggle:1;
1656 u16 mute:1;
1658 u8 brightness_level;
1659 u8 volume_level;
1662 /* kthread for the hotkey poller */
1663 static struct task_struct *tpacpi_hotkey_task;
1665 /* Acquired while the poller kthread is running, use to sync start/stop */
1666 static struct mutex hotkey_thread_mutex;
1669 * Acquire mutex to write poller control variables.
1670 * Increment hotkey_config_change when changing them.
1672 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1674 static struct mutex hotkey_thread_data_mutex;
1675 static unsigned int hotkey_config_change;
1678 * hotkey poller control variables
1680 * Must be atomic or readers will also need to acquire mutex
1682 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1683 static unsigned int hotkey_poll_freq = 10; /* Hz */
1685 #define HOTKEY_CONFIG_CRITICAL_START \
1686 do { \
1687 mutex_lock(&hotkey_thread_data_mutex); \
1688 hotkey_config_change++; \
1689 } while (0);
1690 #define HOTKEY_CONFIG_CRITICAL_END \
1691 mutex_unlock(&hotkey_thread_data_mutex);
1693 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1695 #define hotkey_source_mask 0U
1696 #define HOTKEY_CONFIG_CRITICAL_START
1697 #define HOTKEY_CONFIG_CRITICAL_END
1699 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1701 static struct mutex hotkey_mutex;
1703 static enum { /* Reasons for waking up */
1704 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1705 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1706 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1707 } hotkey_wakeup_reason;
1709 static int hotkey_autosleep_ack;
1711 static u32 hotkey_orig_mask;
1712 static u32 hotkey_all_mask;
1713 static u32 hotkey_reserved_mask;
1714 static u32 hotkey_mask;
1716 static unsigned int hotkey_report_mode;
1718 static u16 *hotkey_keycode_map;
1720 static struct attribute_set *hotkey_dev_attributes;
1722 /* HKEY.MHKG() return bits */
1723 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1725 static int hotkey_get_wlsw(int *status)
1727 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1728 if (dbg_wlswemul) {
1729 *status = !!tpacpi_wlsw_emulstate;
1730 return 0;
1732 #endif
1733 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1734 return -EIO;
1735 return 0;
1738 static int hotkey_get_tablet_mode(int *status)
1740 int s;
1742 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1743 return -EIO;
1745 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1746 return 0;
1750 * Call with hotkey_mutex held
1752 static int hotkey_mask_get(void)
1754 u32 m = 0;
1756 if (tp_features.hotkey_mask) {
1757 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1758 return -EIO;
1760 HOTKEY_CONFIG_CRITICAL_START
1761 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1762 HOTKEY_CONFIG_CRITICAL_END
1764 return 0;
1768 * Call with hotkey_mutex held
1770 static int hotkey_mask_set(u32 mask)
1772 int i;
1773 int rc = 0;
1775 if (tp_features.hotkey_mask) {
1776 if (!tp_warned.hotkey_mask_ff &&
1777 (mask == 0xffff || mask == 0xffffff ||
1778 mask == 0xffffffff)) {
1779 tp_warned.hotkey_mask_ff = 1;
1780 printk(TPACPI_NOTICE
1781 "setting the hotkey mask to 0x%08x is likely "
1782 "not the best way to go about it\n", mask);
1783 printk(TPACPI_NOTICE
1784 "please consider using the driver defaults, "
1785 "and refer to up-to-date thinkpad-acpi "
1786 "documentation\n");
1789 HOTKEY_CONFIG_CRITICAL_START
1790 for (i = 0; i < 32; i++) {
1791 u32 m = 1 << i;
1792 /* enable in firmware mask only keys not in NVRAM
1793 * mode, but enable the key in the cached hotkey_mask
1794 * regardless of mode, or the key will end up
1795 * disabled by hotkey_mask_get() */
1796 if (!acpi_evalf(hkey_handle,
1797 NULL, "MHKM", "vdd", i + 1,
1798 !!((mask & ~hotkey_source_mask) & m))) {
1799 rc = -EIO;
1800 break;
1801 } else {
1802 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1805 HOTKEY_CONFIG_CRITICAL_END
1807 /* hotkey_mask_get must be called unconditionally below */
1808 if (!hotkey_mask_get() && !rc &&
1809 (hotkey_mask & ~hotkey_source_mask) !=
1810 (mask & ~hotkey_source_mask)) {
1811 printk(TPACPI_NOTICE
1812 "requested hot key mask 0x%08x, but "
1813 "firmware forced it to 0x%08x\n",
1814 mask, hotkey_mask);
1816 } else {
1817 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1818 HOTKEY_CONFIG_CRITICAL_START
1819 hotkey_mask = mask & hotkey_source_mask;
1820 HOTKEY_CONFIG_CRITICAL_END
1821 hotkey_mask_get();
1822 if (hotkey_mask != mask) {
1823 printk(TPACPI_NOTICE
1824 "requested hot key mask 0x%08x, "
1825 "forced to 0x%08x (NVRAM poll mask is "
1826 "0x%08x): no firmware mask support\n",
1827 mask, hotkey_mask, hotkey_source_mask);
1829 #else
1830 hotkey_mask_get();
1831 rc = -ENXIO;
1832 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1835 return rc;
1838 static int hotkey_status_get(int *status)
1840 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1841 return -EIO;
1843 return 0;
1846 static int hotkey_status_set(bool enable)
1848 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1849 return -EIO;
1851 return 0;
1854 static void tpacpi_input_send_tabletsw(void)
1856 int state;
1858 if (tp_features.hotkey_tablet &&
1859 !hotkey_get_tablet_mode(&state)) {
1860 mutex_lock(&tpacpi_inputdev_send_mutex);
1862 input_report_switch(tpacpi_inputdev,
1863 SW_TABLET_MODE, !!state);
1864 input_sync(tpacpi_inputdev);
1866 mutex_unlock(&tpacpi_inputdev_send_mutex);
1870 static void tpacpi_input_send_key(unsigned int scancode)
1872 unsigned int keycode;
1874 keycode = hotkey_keycode_map[scancode];
1876 if (keycode != KEY_RESERVED) {
1877 mutex_lock(&tpacpi_inputdev_send_mutex);
1879 input_report_key(tpacpi_inputdev, keycode, 1);
1880 if (keycode == KEY_UNKNOWN)
1881 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1882 scancode);
1883 input_sync(tpacpi_inputdev);
1885 input_report_key(tpacpi_inputdev, keycode, 0);
1886 if (keycode == KEY_UNKNOWN)
1887 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1888 scancode);
1889 input_sync(tpacpi_inputdev);
1891 mutex_unlock(&tpacpi_inputdev_send_mutex);
1895 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1896 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1898 static void tpacpi_hotkey_send_key(unsigned int scancode)
1900 tpacpi_input_send_key(scancode);
1901 if (hotkey_report_mode < 2) {
1902 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1903 0x80, 0x1001 + scancode);
1907 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1909 u8 d;
1911 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1912 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1913 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1914 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1915 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1916 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1918 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1919 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1920 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1922 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1923 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1924 n->displayexp_toggle =
1925 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1927 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1928 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1929 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1930 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1931 n->brightness_toggle =
1932 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1934 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1935 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1936 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1937 >> TP_NVRAM_POS_LEVEL_VOLUME;
1938 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1939 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1943 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1944 do { \
1945 if ((mask & (1 << __scancode)) && \
1946 oldn->__member != newn->__member) \
1947 tpacpi_hotkey_send_key(__scancode); \
1948 } while (0)
1950 #define TPACPI_MAY_SEND_KEY(__scancode) \
1951 do { if (mask & (1 << __scancode)) \
1952 tpacpi_hotkey_send_key(__scancode); } while (0)
1954 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1955 struct tp_nvram_state *newn,
1956 u32 mask)
1958 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1959 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1960 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1961 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1963 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1965 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1967 /* handle volume */
1968 if (oldn->volume_toggle != newn->volume_toggle) {
1969 if (oldn->mute != newn->mute) {
1970 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1972 if (oldn->volume_level > newn->volume_level) {
1973 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1974 } else if (oldn->volume_level < newn->volume_level) {
1975 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1976 } else if (oldn->mute == newn->mute) {
1977 /* repeated key presses that didn't change state */
1978 if (newn->mute) {
1979 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1980 } else if (newn->volume_level != 0) {
1981 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1982 } else {
1983 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1988 /* handle brightness */
1989 if (oldn->brightness_toggle != newn->brightness_toggle) {
1990 if (oldn->brightness_level < newn->brightness_level) {
1991 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1992 } else if (oldn->brightness_level > newn->brightness_level) {
1993 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1994 } else {
1995 /* repeated key presses that didn't change state */
1996 if (newn->brightness_level != 0) {
1997 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1998 } else {
1999 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2005 #undef TPACPI_COMPARE_KEY
2006 #undef TPACPI_MAY_SEND_KEY
2008 static int hotkey_kthread(void *data)
2010 struct tp_nvram_state s[2];
2011 u32 mask;
2012 unsigned int si, so;
2013 unsigned long t;
2014 unsigned int change_detector, must_reset;
2015 unsigned int poll_freq;
2017 mutex_lock(&hotkey_thread_mutex);
2019 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2020 goto exit;
2022 set_freezable();
2024 so = 0;
2025 si = 1;
2026 t = 0;
2028 /* Initial state for compares */
2029 mutex_lock(&hotkey_thread_data_mutex);
2030 change_detector = hotkey_config_change;
2031 mask = hotkey_source_mask & hotkey_mask;
2032 poll_freq = hotkey_poll_freq;
2033 mutex_unlock(&hotkey_thread_data_mutex);
2034 hotkey_read_nvram(&s[so], mask);
2036 while (!kthread_should_stop()) {
2037 if (t == 0) {
2038 if (likely(poll_freq))
2039 t = 1000/poll_freq;
2040 else
2041 t = 100; /* should never happen... */
2043 t = msleep_interruptible(t);
2044 if (unlikely(kthread_should_stop()))
2045 break;
2046 must_reset = try_to_freeze();
2047 if (t > 0 && !must_reset)
2048 continue;
2050 mutex_lock(&hotkey_thread_data_mutex);
2051 if (must_reset || hotkey_config_change != change_detector) {
2052 /* forget old state on thaw or config change */
2053 si = so;
2054 t = 0;
2055 change_detector = hotkey_config_change;
2057 mask = hotkey_source_mask & hotkey_mask;
2058 poll_freq = hotkey_poll_freq;
2059 mutex_unlock(&hotkey_thread_data_mutex);
2061 if (likely(mask)) {
2062 hotkey_read_nvram(&s[si], mask);
2063 if (likely(si != so)) {
2064 hotkey_compare_and_issue_event(&s[so], &s[si],
2065 mask);
2069 so = si;
2070 si ^= 1;
2073 exit:
2074 mutex_unlock(&hotkey_thread_mutex);
2075 return 0;
2078 /* call with hotkey_mutex held */
2079 static void hotkey_poll_stop_sync(void)
2081 if (tpacpi_hotkey_task) {
2082 if (frozen(tpacpi_hotkey_task) ||
2083 freezing(tpacpi_hotkey_task))
2084 thaw_process(tpacpi_hotkey_task);
2086 kthread_stop(tpacpi_hotkey_task);
2087 tpacpi_hotkey_task = NULL;
2088 mutex_lock(&hotkey_thread_mutex);
2089 /* at this point, the thread did exit */
2090 mutex_unlock(&hotkey_thread_mutex);
2094 /* call with hotkey_mutex held */
2095 static void hotkey_poll_setup(bool may_warn)
2097 u32 hotkeys_to_poll = hotkey_source_mask & hotkey_mask;
2099 if (hotkeys_to_poll != 0 && hotkey_poll_freq > 0 &&
2100 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2101 if (!tpacpi_hotkey_task) {
2102 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2103 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2104 if (IS_ERR(tpacpi_hotkey_task)) {
2105 tpacpi_hotkey_task = NULL;
2106 printk(TPACPI_ERR
2107 "could not create kernel thread "
2108 "for hotkey polling\n");
2111 } else {
2112 hotkey_poll_stop_sync();
2113 if (may_warn && hotkeys_to_poll != 0 &&
2114 hotkey_poll_freq == 0) {
2115 printk(TPACPI_NOTICE
2116 "hot keys 0x%08x require polling, "
2117 "which is currently disabled\n",
2118 hotkeys_to_poll);
2123 static void hotkey_poll_setup_safe(bool may_warn)
2125 mutex_lock(&hotkey_mutex);
2126 hotkey_poll_setup(may_warn);
2127 mutex_unlock(&hotkey_mutex);
2130 /* call with hotkey_mutex held */
2131 static void hotkey_poll_set_freq(unsigned int freq)
2133 if (!freq)
2134 hotkey_poll_stop_sync();
2136 HOTKEY_CONFIG_CRITICAL_START
2137 hotkey_poll_freq = freq;
2138 HOTKEY_CONFIG_CRITICAL_END
2141 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2143 static void hotkey_poll_setup_safe(bool __unused)
2147 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2149 static int hotkey_inputdev_open(struct input_dev *dev)
2151 switch (tpacpi_lifecycle) {
2152 case TPACPI_LIFE_INIT:
2154 * hotkey_init will call hotkey_poll_setup_safe
2155 * at the appropriate moment
2157 return 0;
2158 case TPACPI_LIFE_EXITING:
2159 return -EBUSY;
2160 case TPACPI_LIFE_RUNNING:
2161 hotkey_poll_setup_safe(false);
2162 return 0;
2165 /* Should only happen if tpacpi_lifecycle is corrupt */
2166 BUG();
2167 return -EBUSY;
2170 static void hotkey_inputdev_close(struct input_dev *dev)
2172 /* disable hotkey polling when possible */
2173 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2174 hotkey_poll_setup_safe(false);
2177 /* sysfs hotkey enable ------------------------------------------------- */
2178 static ssize_t hotkey_enable_show(struct device *dev,
2179 struct device_attribute *attr,
2180 char *buf)
2182 int res, status;
2184 printk_deprecated_attribute("hotkey_enable",
2185 "Hotkey reporting is always enabled");
2187 res = hotkey_status_get(&status);
2188 if (res)
2189 return res;
2191 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2194 static ssize_t hotkey_enable_store(struct device *dev,
2195 struct device_attribute *attr,
2196 const char *buf, size_t count)
2198 unsigned long t;
2200 printk_deprecated_attribute("hotkey_enable",
2201 "Hotkeys can be disabled through hotkey_mask");
2203 if (parse_strtoul(buf, 1, &t))
2204 return -EINVAL;
2206 if (t == 0)
2207 return -EPERM;
2209 return count;
2212 static struct device_attribute dev_attr_hotkey_enable =
2213 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2214 hotkey_enable_show, hotkey_enable_store);
2216 /* sysfs hotkey mask --------------------------------------------------- */
2217 static ssize_t hotkey_mask_show(struct device *dev,
2218 struct device_attribute *attr,
2219 char *buf)
2221 int res;
2223 if (mutex_lock_killable(&hotkey_mutex))
2224 return -ERESTARTSYS;
2225 res = hotkey_mask_get();
2226 mutex_unlock(&hotkey_mutex);
2228 return (res)?
2229 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2232 static ssize_t hotkey_mask_store(struct device *dev,
2233 struct device_attribute *attr,
2234 const char *buf, size_t count)
2236 unsigned long t;
2237 int res;
2239 if (parse_strtoul(buf, 0xffffffffUL, &t))
2240 return -EINVAL;
2242 if (mutex_lock_killable(&hotkey_mutex))
2243 return -ERESTARTSYS;
2245 res = hotkey_mask_set(t);
2247 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2248 hotkey_poll_setup(true);
2249 #endif
2251 mutex_unlock(&hotkey_mutex);
2253 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2255 return (res) ? res : count;
2258 static struct device_attribute dev_attr_hotkey_mask =
2259 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2260 hotkey_mask_show, hotkey_mask_store);
2262 /* sysfs hotkey bios_enabled ------------------------------------------- */
2263 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2264 struct device_attribute *attr,
2265 char *buf)
2267 return sprintf(buf, "0\n");
2270 static struct device_attribute dev_attr_hotkey_bios_enabled =
2271 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2273 /* sysfs hotkey bios_mask ---------------------------------------------- */
2274 static ssize_t hotkey_bios_mask_show(struct device *dev,
2275 struct device_attribute *attr,
2276 char *buf)
2278 printk_deprecated_attribute("hotkey_bios_mask",
2279 "This attribute is useless.");
2280 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2283 static struct device_attribute dev_attr_hotkey_bios_mask =
2284 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2286 /* sysfs hotkey all_mask ----------------------------------------------- */
2287 static ssize_t hotkey_all_mask_show(struct device *dev,
2288 struct device_attribute *attr,
2289 char *buf)
2291 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2292 hotkey_all_mask | hotkey_source_mask);
2295 static struct device_attribute dev_attr_hotkey_all_mask =
2296 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2298 /* sysfs hotkey recommended_mask --------------------------------------- */
2299 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2300 struct device_attribute *attr,
2301 char *buf)
2303 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2304 (hotkey_all_mask | hotkey_source_mask)
2305 & ~hotkey_reserved_mask);
2308 static struct device_attribute dev_attr_hotkey_recommended_mask =
2309 __ATTR(hotkey_recommended_mask, S_IRUGO,
2310 hotkey_recommended_mask_show, NULL);
2312 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2314 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2315 static ssize_t hotkey_source_mask_show(struct device *dev,
2316 struct device_attribute *attr,
2317 char *buf)
2319 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2322 static ssize_t hotkey_source_mask_store(struct device *dev,
2323 struct device_attribute *attr,
2324 const char *buf, size_t count)
2326 unsigned long t;
2328 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2329 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2330 return -EINVAL;
2332 if (mutex_lock_killable(&hotkey_mutex))
2333 return -ERESTARTSYS;
2335 HOTKEY_CONFIG_CRITICAL_START
2336 hotkey_source_mask = t;
2337 HOTKEY_CONFIG_CRITICAL_END
2339 hotkey_poll_setup(true);
2340 hotkey_mask_set(hotkey_mask);
2342 mutex_unlock(&hotkey_mutex);
2344 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2346 return count;
2349 static struct device_attribute dev_attr_hotkey_source_mask =
2350 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2351 hotkey_source_mask_show, hotkey_source_mask_store);
2353 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2354 static ssize_t hotkey_poll_freq_show(struct device *dev,
2355 struct device_attribute *attr,
2356 char *buf)
2358 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2361 static ssize_t hotkey_poll_freq_store(struct device *dev,
2362 struct device_attribute *attr,
2363 const char *buf, size_t count)
2365 unsigned long t;
2367 if (parse_strtoul(buf, 25, &t))
2368 return -EINVAL;
2370 if (mutex_lock_killable(&hotkey_mutex))
2371 return -ERESTARTSYS;
2373 hotkey_poll_set_freq(t);
2374 hotkey_poll_setup(true);
2376 mutex_unlock(&hotkey_mutex);
2378 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2380 return count;
2383 static struct device_attribute dev_attr_hotkey_poll_freq =
2384 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2385 hotkey_poll_freq_show, hotkey_poll_freq_store);
2387 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2389 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2390 static ssize_t hotkey_radio_sw_show(struct device *dev,
2391 struct device_attribute *attr,
2392 char *buf)
2394 int res, s;
2395 res = hotkey_get_wlsw(&s);
2396 if (res < 0)
2397 return res;
2399 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2402 static struct device_attribute dev_attr_hotkey_radio_sw =
2403 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2405 static void hotkey_radio_sw_notify_change(void)
2407 if (tp_features.hotkey_wlsw)
2408 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2409 "hotkey_radio_sw");
2412 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2413 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2414 struct device_attribute *attr,
2415 char *buf)
2417 int res, s;
2418 res = hotkey_get_tablet_mode(&s);
2419 if (res < 0)
2420 return res;
2422 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2425 static struct device_attribute dev_attr_hotkey_tablet_mode =
2426 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2428 static void hotkey_tablet_mode_notify_change(void)
2430 if (tp_features.hotkey_tablet)
2431 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2432 "hotkey_tablet_mode");
2435 /* sysfs hotkey report_mode -------------------------------------------- */
2436 static ssize_t hotkey_report_mode_show(struct device *dev,
2437 struct device_attribute *attr,
2438 char *buf)
2440 return snprintf(buf, PAGE_SIZE, "%d\n",
2441 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2444 static struct device_attribute dev_attr_hotkey_report_mode =
2445 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2447 /* sysfs wakeup reason (pollable) -------------------------------------- */
2448 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2449 struct device_attribute *attr,
2450 char *buf)
2452 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2455 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2456 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2458 static void hotkey_wakeup_reason_notify_change(void)
2460 if (tp_features.hotkey_mask)
2461 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2462 "wakeup_reason");
2465 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2466 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2467 struct device_attribute *attr,
2468 char *buf)
2470 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2473 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2474 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2475 hotkey_wakeup_hotunplug_complete_show, NULL);
2477 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2479 if (tp_features.hotkey_mask)
2480 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2481 "wakeup_hotunplug_complete");
2484 /* --------------------------------------------------------------------- */
2486 static struct attribute *hotkey_attributes[] __initdata = {
2487 &dev_attr_hotkey_enable.attr,
2488 &dev_attr_hotkey_bios_enabled.attr,
2489 &dev_attr_hotkey_report_mode.attr,
2490 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2491 &dev_attr_hotkey_mask.attr,
2492 &dev_attr_hotkey_all_mask.attr,
2493 &dev_attr_hotkey_recommended_mask.attr,
2494 &dev_attr_hotkey_source_mask.attr,
2495 &dev_attr_hotkey_poll_freq.attr,
2496 #endif
2499 static struct attribute *hotkey_mask_attributes[] __initdata = {
2500 &dev_attr_hotkey_bios_mask.attr,
2501 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2502 &dev_attr_hotkey_mask.attr,
2503 &dev_attr_hotkey_all_mask.attr,
2504 &dev_attr_hotkey_recommended_mask.attr,
2505 #endif
2506 &dev_attr_hotkey_wakeup_reason.attr,
2507 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2510 static void bluetooth_update_rfk(void);
2511 static void wan_update_rfk(void);
2512 static void uwb_update_rfk(void);
2513 static void tpacpi_send_radiosw_update(void)
2515 int wlsw;
2517 /* Sync these BEFORE sending any rfkill events */
2518 if (tp_features.bluetooth)
2519 bluetooth_update_rfk();
2520 if (tp_features.wan)
2521 wan_update_rfk();
2522 if (tp_features.uwb)
2523 uwb_update_rfk();
2525 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2526 mutex_lock(&tpacpi_inputdev_send_mutex);
2528 input_report_switch(tpacpi_inputdev,
2529 SW_RFKILL_ALL, !!wlsw);
2530 input_sync(tpacpi_inputdev);
2532 mutex_unlock(&tpacpi_inputdev_send_mutex);
2534 hotkey_radio_sw_notify_change();
2537 static void hotkey_exit(void)
2539 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2540 mutex_lock(&hotkey_mutex);
2541 hotkey_poll_stop_sync();
2542 mutex_unlock(&hotkey_mutex);
2543 #endif
2545 if (hotkey_dev_attributes)
2546 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2548 kfree(hotkey_keycode_map);
2550 if (tp_features.hotkey) {
2551 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2552 "restoring original hot key mask\n");
2553 /* no short-circuit boolean operator below! */
2554 if ((hotkey_mask_set(hotkey_orig_mask) |
2555 hotkey_status_set(false)) != 0)
2556 printk(TPACPI_ERR
2557 "failed to restore hot key mask "
2558 "to BIOS defaults\n");
2562 static void __init hotkey_unmap(const unsigned int scancode)
2564 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
2565 clear_bit(hotkey_keycode_map[scancode],
2566 tpacpi_inputdev->keybit);
2567 hotkey_keycode_map[scancode] = KEY_RESERVED;
2571 static int __init hotkey_init(struct ibm_init_struct *iibm)
2573 /* Requirements for changing the default keymaps:
2575 * 1. Many of the keys are mapped to KEY_RESERVED for very
2576 * good reasons. Do not change them unless you have deep
2577 * knowledge on the IBM and Lenovo ThinkPad firmware for
2578 * the various ThinkPad models. The driver behaves
2579 * differently for KEY_RESERVED: such keys have their
2580 * hot key mask *unset* in mask_recommended, and also
2581 * in the initial hot key mask programmed into the
2582 * firmware at driver load time, which means the firm-
2583 * ware may react very differently if you change them to
2584 * something else;
2586 * 2. You must be subscribed to the linux-thinkpad and
2587 * ibm-acpi-devel mailing lists, and you should read the
2588 * list archives since 2007 if you want to change the
2589 * keymaps. This requirement exists so that you will
2590 * know the past history of problems with the thinkpad-
2591 * acpi driver keymaps, and also that you will be
2592 * listening to any bug reports;
2594 * 3. Do not send thinkpad-acpi specific patches directly to
2595 * for merging, *ever*. Send them to the linux-acpi
2596 * mailinglist for comments. Merging is to be done only
2597 * through acpi-test and the ACPI maintainer.
2599 * If the above is too much to ask, don't change the keymap.
2600 * Ask the thinkpad-acpi maintainer to do it, instead.
2602 static u16 ibm_keycode_map[] __initdata = {
2603 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2604 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2605 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2606 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2608 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2609 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2610 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2611 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2613 /* brightness: firmware always reacts to them, unless
2614 * X.org did some tricks in the radeon BIOS scratch
2615 * registers of *some* models */
2616 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2617 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2619 /* Thinklight: firmware always react to it */
2620 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2622 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2623 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2625 /* Volume: firmware always react to it and reprograms
2626 * the built-in *extra* mixer. Never map it to control
2627 * another mixer by default. */
2628 KEY_RESERVED, /* 0x14: VOLUME UP */
2629 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2630 KEY_RESERVED, /* 0x16: MUTE */
2632 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2634 /* (assignments unknown, please report if found) */
2635 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2636 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2638 static u16 lenovo_keycode_map[] __initdata = {
2639 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2640 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2641 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2642 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2644 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2645 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2646 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2647 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2649 /* These should be enabled --only-- when ACPI video
2650 * is disabled (i.e. in "vendor" mode), and are handled
2651 * in a special way by the init code */
2652 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2653 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2655 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2657 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2658 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2660 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2661 * react to it and reprograms the built-in *extra* mixer.
2662 * Never map it to control another mixer by default.
2664 * T60?, T61, R60?, R61: firmware and EC tries to send
2665 * these over the regular keyboard, so these are no-ops,
2666 * but there are still weird bugs re. MUTE, so do not
2667 * change unless you get test reports from all Lenovo
2668 * models. May cause the BIOS to interfere with the
2669 * HDA mixer.
2671 KEY_RESERVED, /* 0x14: VOLUME UP */
2672 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2673 KEY_RESERVED, /* 0x16: MUTE */
2675 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2677 /* (assignments unknown, please report if found) */
2678 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2679 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2682 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2683 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2684 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2686 int res, i;
2687 int status;
2688 int hkeyv;
2690 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2691 "initializing hotkey subdriver\n");
2693 BUG_ON(!tpacpi_inputdev);
2694 BUG_ON(tpacpi_inputdev->open != NULL ||
2695 tpacpi_inputdev->close != NULL);
2697 TPACPI_ACPIHANDLE_INIT(hkey);
2698 mutex_init(&hotkey_mutex);
2700 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2701 mutex_init(&hotkey_thread_mutex);
2702 mutex_init(&hotkey_thread_data_mutex);
2703 #endif
2705 /* hotkey not supported on 570 */
2706 tp_features.hotkey = hkey_handle != NULL;
2708 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2709 "hotkeys are %s\n",
2710 str_supported(tp_features.hotkey));
2712 if (!tp_features.hotkey)
2713 return 1;
2715 tpacpi_disable_brightness_delay();
2717 hotkey_dev_attributes = create_attr_set(13, NULL);
2718 if (!hotkey_dev_attributes)
2719 return -ENOMEM;
2720 res = add_many_to_attr_set(hotkey_dev_attributes,
2721 hotkey_attributes,
2722 ARRAY_SIZE(hotkey_attributes));
2723 if (res)
2724 goto err_exit;
2726 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2727 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2728 for HKEY interface version 0x100 */
2729 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2730 if ((hkeyv >> 8) != 1) {
2731 printk(TPACPI_ERR "unknown version of the "
2732 "HKEY interface: 0x%x\n", hkeyv);
2733 printk(TPACPI_ERR "please report this to %s\n",
2734 TPACPI_MAIL);
2735 } else {
2737 * MHKV 0x100 in A31, R40, R40e,
2738 * T4x, X31, and later
2740 tp_features.hotkey_mask = 1;
2741 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2742 "firmware HKEY interface version: 0x%x\n",
2743 hkeyv);
2747 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2748 "hotkey masks are %s\n",
2749 str_supported(tp_features.hotkey_mask));
2751 if (tp_features.hotkey_mask) {
2752 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2753 "MHKA", "qd")) {
2754 printk(TPACPI_ERR
2755 "missing MHKA handler, "
2756 "please report this to %s\n",
2757 TPACPI_MAIL);
2758 /* FN+F12, FN+F4, FN+F3 */
2759 hotkey_all_mask = 0x080cU;
2763 /* hotkey_source_mask *must* be zero for
2764 * the first hotkey_mask_get */
2765 if (tp_features.hotkey_mask) {
2766 res = hotkey_mask_get();
2767 if (res)
2768 goto err_exit;
2770 hotkey_orig_mask = hotkey_mask;
2771 res = add_many_to_attr_set(
2772 hotkey_dev_attributes,
2773 hotkey_mask_attributes,
2774 ARRAY_SIZE(hotkey_mask_attributes));
2775 if (res)
2776 goto err_exit;
2779 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2780 if (dbg_wlswemul) {
2781 tp_features.hotkey_wlsw = 1;
2782 printk(TPACPI_INFO
2783 "radio switch emulation enabled\n");
2784 } else
2785 #endif
2786 /* Not all thinkpads have a hardware radio switch */
2787 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2788 tp_features.hotkey_wlsw = 1;
2789 printk(TPACPI_INFO
2790 "radio switch found; radios are %s\n",
2791 enabled(status, 0));
2793 if (tp_features.hotkey_wlsw)
2794 res = add_to_attr_set(hotkey_dev_attributes,
2795 &dev_attr_hotkey_radio_sw.attr);
2797 /* For X41t, X60t, X61t Tablets... */
2798 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2799 tp_features.hotkey_tablet = 1;
2800 printk(TPACPI_INFO
2801 "possible tablet mode switch found; "
2802 "ThinkPad in %s mode\n",
2803 (status & TP_HOTKEY_TABLET_MASK)?
2804 "tablet" : "laptop");
2805 res = add_to_attr_set(hotkey_dev_attributes,
2806 &dev_attr_hotkey_tablet_mode.attr);
2809 if (!res)
2810 res = register_attr_set_with_sysfs(
2811 hotkey_dev_attributes,
2812 &tpacpi_pdev->dev.kobj);
2813 if (res)
2814 goto err_exit;
2816 /* Set up key map */
2818 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2819 GFP_KERNEL);
2820 if (!hotkey_keycode_map) {
2821 printk(TPACPI_ERR
2822 "failed to allocate memory for key map\n");
2823 res = -ENOMEM;
2824 goto err_exit;
2827 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2828 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2829 "using Lenovo default hot key map\n");
2830 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2831 TPACPI_HOTKEY_MAP_SIZE);
2832 } else {
2833 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2834 "using IBM default hot key map\n");
2835 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2836 TPACPI_HOTKEY_MAP_SIZE);
2839 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2840 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2841 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2842 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2843 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2844 tpacpi_inputdev->keycode = hotkey_keycode_map;
2845 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2846 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2847 set_bit(hotkey_keycode_map[i],
2848 tpacpi_inputdev->keybit);
2849 } else {
2850 if (i < sizeof(hotkey_reserved_mask)*8)
2851 hotkey_reserved_mask |= 1 << i;
2855 if (tp_features.hotkey_wlsw) {
2856 set_bit(EV_SW, tpacpi_inputdev->evbit);
2857 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2859 if (tp_features.hotkey_tablet) {
2860 set_bit(EV_SW, tpacpi_inputdev->evbit);
2861 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2864 /* Do not issue duplicate brightness change events to
2865 * userspace */
2866 if (!tp_features.bright_acpimode)
2867 /* update bright_acpimode... */
2868 tpacpi_check_std_acpi_brightness_support();
2870 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2871 printk(TPACPI_INFO
2872 "This ThinkPad has standard ACPI backlight "
2873 "brightness control, supported by the ACPI "
2874 "video driver\n");
2875 printk(TPACPI_NOTICE
2876 "Disabling thinkpad-acpi brightness events "
2877 "by default...\n");
2879 /* Disable brightness up/down on Lenovo thinkpads when
2880 * ACPI is handling them, otherwise it is plain impossible
2881 * for userspace to do something even remotely sane */
2882 hotkey_reserved_mask |=
2883 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2884 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2885 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
2886 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
2889 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2890 if (tp_features.hotkey_mask) {
2891 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2892 & ~hotkey_all_mask
2893 & ~hotkey_reserved_mask;
2894 } else {
2895 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2896 & ~hotkey_reserved_mask;
2899 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2900 "hotkey source mask 0x%08x, polling freq %u\n",
2901 hotkey_source_mask, hotkey_poll_freq);
2902 #endif
2904 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2905 "enabling firmware HKEY event interface...\n");
2906 res = hotkey_status_set(true);
2907 if (res) {
2908 hotkey_exit();
2909 return res;
2911 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2912 & ~hotkey_reserved_mask)
2913 | hotkey_orig_mask);
2914 if (res < 0 && res != -ENXIO) {
2915 hotkey_exit();
2916 return res;
2919 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2920 "legacy ibm/hotkey event reporting over procfs %s\n",
2921 (hotkey_report_mode < 2) ?
2922 "enabled" : "disabled");
2924 tpacpi_inputdev->open = &hotkey_inputdev_open;
2925 tpacpi_inputdev->close = &hotkey_inputdev_close;
2927 hotkey_poll_setup_safe(true);
2928 tpacpi_send_radiosw_update();
2929 tpacpi_input_send_tabletsw();
2931 return 0;
2933 err_exit:
2934 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2935 hotkey_dev_attributes = NULL;
2937 return (res < 0)? res : 1;
2940 static bool hotkey_notify_hotkey(const u32 hkey,
2941 bool *send_acpi_ev,
2942 bool *ignore_acpi_ev)
2944 /* 0x1000-0x1FFF: key presses */
2945 unsigned int scancode = hkey & 0xfff;
2946 *send_acpi_ev = true;
2947 *ignore_acpi_ev = false;
2949 if (scancode > 0 && scancode < 0x21) {
2950 scancode--;
2951 if (!(hotkey_source_mask & (1 << scancode))) {
2952 tpacpi_input_send_key(scancode);
2953 *send_acpi_ev = false;
2954 } else {
2955 *ignore_acpi_ev = true;
2957 return true;
2959 return false;
2962 static bool hotkey_notify_wakeup(const u32 hkey,
2963 bool *send_acpi_ev,
2964 bool *ignore_acpi_ev)
2966 /* 0x2000-0x2FFF: Wakeup reason */
2967 *send_acpi_ev = true;
2968 *ignore_acpi_ev = false;
2970 switch (hkey) {
2971 case 0x2304: /* suspend, undock */
2972 case 0x2404: /* hibernation, undock */
2973 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2974 *ignore_acpi_ev = true;
2975 break;
2977 case 0x2305: /* suspend, bay eject */
2978 case 0x2405: /* hibernation, bay eject */
2979 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2980 *ignore_acpi_ev = true;
2981 break;
2983 case 0x2313: /* Battery on critical low level (S3) */
2984 case 0x2413: /* Battery on critical low level (S4) */
2985 printk(TPACPI_ALERT
2986 "EMERGENCY WAKEUP: battery almost empty\n");
2987 /* how to auto-heal: */
2988 /* 2313: woke up from S3, go to S4/S5 */
2989 /* 2413: woke up from S4, go to S5 */
2990 break;
2992 default:
2993 return false;
2996 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2997 printk(TPACPI_INFO
2998 "woke up due to a hot-unplug "
2999 "request...\n");
3000 hotkey_wakeup_reason_notify_change();
3002 return true;
3005 static bool hotkey_notify_usrevent(const u32 hkey,
3006 bool *send_acpi_ev,
3007 bool *ignore_acpi_ev)
3009 /* 0x5000-0x5FFF: human interface helpers */
3010 *send_acpi_ev = true;
3011 *ignore_acpi_ev = false;
3013 switch (hkey) {
3014 case 0x5010: /* Lenovo new BIOS: brightness changed */
3015 case 0x500b: /* X61t: tablet pen inserted into bay */
3016 case 0x500c: /* X61t: tablet pen removed from bay */
3017 return true;
3019 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3020 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3021 tpacpi_input_send_tabletsw();
3022 hotkey_tablet_mode_notify_change();
3023 *send_acpi_ev = false;
3024 return true;
3026 case 0x5001:
3027 case 0x5002:
3028 /* LID switch events. Do not propagate */
3029 *ignore_acpi_ev = true;
3030 return true;
3032 default:
3033 return false;
3037 static bool hotkey_notify_thermal(const u32 hkey,
3038 bool *send_acpi_ev,
3039 bool *ignore_acpi_ev)
3041 /* 0x6000-0x6FFF: thermal alarms */
3042 *send_acpi_ev = true;
3043 *ignore_acpi_ev = false;
3045 switch (hkey) {
3046 case 0x6011:
3047 printk(TPACPI_CRIT
3048 "THERMAL ALARM: battery is too hot!\n");
3049 /* recommended action: warn user through gui */
3050 return true;
3051 case 0x6012:
3052 printk(TPACPI_ALERT
3053 "THERMAL EMERGENCY: battery is extremely hot!\n");
3054 /* recommended action: immediate sleep/hibernate */
3055 return true;
3056 case 0x6021:
3057 printk(TPACPI_CRIT
3058 "THERMAL ALARM: "
3059 "a sensor reports something is too hot!\n");
3060 /* recommended action: warn user through gui, that */
3061 /* some internal component is too hot */
3062 return true;
3063 case 0x6022:
3064 printk(TPACPI_ALERT
3065 "THERMAL EMERGENCY: "
3066 "a sensor reports something is extremely hot!\n");
3067 /* recommended action: immediate sleep/hibernate */
3068 return true;
3069 case 0x6030:
3070 printk(TPACPI_INFO
3071 "EC reports that Thermal Table has changed\n");
3072 /* recommended action: do nothing, we don't have
3073 * Lenovo ATM information */
3074 return true;
3075 default:
3076 printk(TPACPI_ALERT
3077 "THERMAL ALERT: unknown thermal alarm received\n");
3078 return false;
3082 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3084 u32 hkey;
3085 bool send_acpi_ev;
3086 bool ignore_acpi_ev;
3087 bool known_ev;
3089 if (event != 0x80) {
3090 printk(TPACPI_ERR
3091 "unknown HKEY notification event %d\n", event);
3092 /* forward it to userspace, maybe it knows how to handle it */
3093 acpi_bus_generate_netlink_event(
3094 ibm->acpi->device->pnp.device_class,
3095 dev_name(&ibm->acpi->device->dev),
3096 event, 0);
3097 return;
3100 while (1) {
3101 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3102 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3103 return;
3106 if (hkey == 0) {
3107 /* queue empty */
3108 return;
3111 send_acpi_ev = true;
3112 ignore_acpi_ev = false;
3114 switch (hkey >> 12) {
3115 case 1:
3116 /* 0x1000-0x1FFF: key presses */
3117 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3118 &ignore_acpi_ev);
3119 break;
3120 case 2:
3121 /* 0x2000-0x2FFF: Wakeup reason */
3122 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3123 &ignore_acpi_ev);
3124 break;
3125 case 3:
3126 /* 0x3000-0x3FFF: bay-related wakeups */
3127 if (hkey == 0x3003) {
3128 hotkey_autosleep_ack = 1;
3129 printk(TPACPI_INFO
3130 "bay ejected\n");
3131 hotkey_wakeup_hotunplug_complete_notify_change();
3132 known_ev = true;
3133 } else {
3134 known_ev = false;
3136 break;
3137 case 4:
3138 /* 0x4000-0x4FFF: dock-related wakeups */
3139 if (hkey == 0x4003) {
3140 hotkey_autosleep_ack = 1;
3141 printk(TPACPI_INFO
3142 "undocked\n");
3143 hotkey_wakeup_hotunplug_complete_notify_change();
3144 known_ev = true;
3145 } else {
3146 known_ev = false;
3148 break;
3149 case 5:
3150 /* 0x5000-0x5FFF: human interface helpers */
3151 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3152 &ignore_acpi_ev);
3153 break;
3154 case 6:
3155 /* 0x6000-0x6FFF: thermal alarms */
3156 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3157 &ignore_acpi_ev);
3158 break;
3159 case 7:
3160 /* 0x7000-0x7FFF: misc */
3161 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3162 tpacpi_send_radiosw_update();
3163 send_acpi_ev = 0;
3164 known_ev = true;
3165 break;
3167 /* fallthrough to default */
3168 default:
3169 known_ev = false;
3171 if (!known_ev) {
3172 printk(TPACPI_NOTICE
3173 "unhandled HKEY event 0x%04x\n", hkey);
3174 printk(TPACPI_NOTICE
3175 "please report the conditions when this "
3176 "event happened to %s\n", TPACPI_MAIL);
3179 /* Legacy events */
3180 if (!ignore_acpi_ev &&
3181 (send_acpi_ev || hotkey_report_mode < 2)) {
3182 acpi_bus_generate_proc_event(ibm->acpi->device,
3183 event, hkey);
3186 /* netlink events */
3187 if (!ignore_acpi_ev && send_acpi_ev) {
3188 acpi_bus_generate_netlink_event(
3189 ibm->acpi->device->pnp.device_class,
3190 dev_name(&ibm->acpi->device->dev),
3191 event, hkey);
3196 static void hotkey_suspend(pm_message_t state)
3198 /* Do these on suspend, we get the events on early resume! */
3199 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3200 hotkey_autosleep_ack = 0;
3203 static void hotkey_resume(void)
3205 tpacpi_disable_brightness_delay();
3207 if (hotkey_mask_get())
3208 printk(TPACPI_ERR
3209 "error while trying to read hot key mask "
3210 "from firmware\n");
3211 tpacpi_send_radiosw_update();
3212 hotkey_tablet_mode_notify_change();
3213 hotkey_wakeup_reason_notify_change();
3214 hotkey_wakeup_hotunplug_complete_notify_change();
3215 hotkey_poll_setup_safe(false);
3218 /* procfs -------------------------------------------------------------- */
3219 static int hotkey_read(char *p)
3221 int res, status;
3222 int len = 0;
3224 if (!tp_features.hotkey) {
3225 len += sprintf(p + len, "status:\t\tnot supported\n");
3226 return len;
3229 if (mutex_lock_killable(&hotkey_mutex))
3230 return -ERESTARTSYS;
3231 res = hotkey_status_get(&status);
3232 if (!res)
3233 res = hotkey_mask_get();
3234 mutex_unlock(&hotkey_mutex);
3235 if (res)
3236 return res;
3238 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3239 if (tp_features.hotkey_mask) {
3240 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3241 len += sprintf(p + len,
3242 "commands:\tenable, disable, reset, <mask>\n");
3243 } else {
3244 len += sprintf(p + len, "mask:\t\tnot supported\n");
3245 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3248 return len;
3251 static void hotkey_enabledisable_warn(bool enable)
3253 tpacpi_log_usertask("procfs hotkey enable/disable");
3254 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3255 TPACPI_WARN
3256 "hotkey enable/disable functionality has been "
3257 "removed from the driver. Hotkeys are always "
3258 "enabled\n"))
3259 printk(TPACPI_ERR
3260 "Please remove the hotkey=enable module "
3261 "parameter, it is deprecated. Hotkeys are always "
3262 "enabled\n");
3265 static int hotkey_write(char *buf)
3267 int res;
3268 u32 mask;
3269 char *cmd;
3271 if (!tp_features.hotkey)
3272 return -ENODEV;
3274 if (mutex_lock_killable(&hotkey_mutex))
3275 return -ERESTARTSYS;
3277 mask = hotkey_mask;
3279 res = 0;
3280 while ((cmd = next_cmd(&buf))) {
3281 if (strlencmp(cmd, "enable") == 0) {
3282 hotkey_enabledisable_warn(1);
3283 } else if (strlencmp(cmd, "disable") == 0) {
3284 hotkey_enabledisable_warn(0);
3285 res = -EPERM;
3286 } else if (strlencmp(cmd, "reset") == 0) {
3287 mask = (hotkey_all_mask | hotkey_source_mask)
3288 & ~hotkey_reserved_mask;
3289 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3290 /* mask set */
3291 } else if (sscanf(cmd, "%x", &mask) == 1) {
3292 /* mask set */
3293 } else {
3294 res = -EINVAL;
3295 goto errexit;
3299 if (!res)
3300 tpacpi_disclose_usertask("procfs hotkey",
3301 "set mask to 0x%08x\n", mask);
3303 if (!res && mask != hotkey_mask)
3304 res = hotkey_mask_set(mask);
3306 errexit:
3307 mutex_unlock(&hotkey_mutex);
3308 return res;
3311 static const struct acpi_device_id ibm_htk_device_ids[] = {
3312 {TPACPI_ACPI_HKEY_HID, 0},
3313 {"", 0},
3316 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3317 .hid = ibm_htk_device_ids,
3318 .notify = hotkey_notify,
3319 .handle = &hkey_handle,
3320 .type = ACPI_DEVICE_NOTIFY,
3323 static struct ibm_struct hotkey_driver_data = {
3324 .name = "hotkey",
3325 .read = hotkey_read,
3326 .write = hotkey_write,
3327 .exit = hotkey_exit,
3328 .resume = hotkey_resume,
3329 .suspend = hotkey_suspend,
3330 .acpi = &ibm_hotkey_acpidriver,
3333 /*************************************************************************
3334 * Bluetooth subdriver
3337 enum {
3338 /* ACPI GBDC/SBDC bits */
3339 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3340 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3341 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3342 off / last state */
3345 enum {
3346 /* ACPI \BLTH commands */
3347 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3348 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3349 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3350 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3351 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3354 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3356 static struct rfkill *tpacpi_bluetooth_rfkill;
3358 static void bluetooth_suspend(pm_message_t state)
3360 /* Try to make sure radio will resume powered off */
3361 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3362 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3363 vdbg_printk(TPACPI_DBG_RFKILL,
3364 "bluetooth power down on resume request failed\n");
3367 static int bluetooth_get_radiosw(void)
3369 int status;
3371 if (!tp_features.bluetooth)
3372 return -ENODEV;
3374 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3375 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3376 return RFKILL_STATE_HARD_BLOCKED;
3378 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3379 if (dbg_bluetoothemul)
3380 return (tpacpi_bluetooth_emulstate) ?
3381 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3382 #endif
3384 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3385 return -EIO;
3387 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3388 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3391 static void bluetooth_update_rfk(void)
3393 int status;
3395 if (!tpacpi_bluetooth_rfkill)
3396 return;
3398 status = bluetooth_get_radiosw();
3399 if (status < 0)
3400 return;
3401 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3403 vdbg_printk(TPACPI_DBG_RFKILL,
3404 "forced rfkill state to %d\n",
3405 status);
3408 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3410 int status;
3412 if (!tp_features.bluetooth)
3413 return -ENODEV;
3415 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3416 * reason to risk weird behaviour. */
3417 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3418 && radio_on)
3419 return -EPERM;
3421 vdbg_printk(TPACPI_DBG_RFKILL,
3422 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3424 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3425 if (dbg_bluetoothemul) {
3426 tpacpi_bluetooth_emulstate = !!radio_on;
3427 if (update_rfk)
3428 bluetooth_update_rfk();
3429 return 0;
3431 #endif
3433 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3434 if (radio_on)
3435 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3436 else
3437 status = 0;
3438 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3439 return -EIO;
3441 if (update_rfk)
3442 bluetooth_update_rfk();
3444 return 0;
3447 /* sysfs bluetooth enable ---------------------------------------------- */
3448 static ssize_t bluetooth_enable_show(struct device *dev,
3449 struct device_attribute *attr,
3450 char *buf)
3452 int status;
3454 printk_deprecated_rfkill_attribute("bluetooth_enable");
3456 status = bluetooth_get_radiosw();
3457 if (status < 0)
3458 return status;
3460 return snprintf(buf, PAGE_SIZE, "%d\n",
3461 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3464 static ssize_t bluetooth_enable_store(struct device *dev,
3465 struct device_attribute *attr,
3466 const char *buf, size_t count)
3468 unsigned long t;
3469 int res;
3471 printk_deprecated_rfkill_attribute("bluetooth_enable");
3473 if (parse_strtoul(buf, 1, &t))
3474 return -EINVAL;
3476 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3478 res = bluetooth_set_radiosw(t, 1);
3480 return (res) ? res : count;
3483 static struct device_attribute dev_attr_bluetooth_enable =
3484 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3485 bluetooth_enable_show, bluetooth_enable_store);
3487 /* --------------------------------------------------------------------- */
3489 static struct attribute *bluetooth_attributes[] = {
3490 &dev_attr_bluetooth_enable.attr,
3491 NULL
3494 static const struct attribute_group bluetooth_attr_group = {
3495 .attrs = bluetooth_attributes,
3498 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3500 int bts = bluetooth_get_radiosw();
3502 if (bts < 0)
3503 return bts;
3505 *state = bts;
3506 return 0;
3509 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3511 dbg_printk(TPACPI_DBG_RFKILL,
3512 "request to change radio state to %d\n", state);
3513 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3516 static void bluetooth_shutdown(void)
3518 /* Order firmware to save current state to NVRAM */
3519 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3520 TP_ACPI_BLTH_SAVE_STATE))
3521 printk(TPACPI_NOTICE
3522 "failed to save bluetooth state to NVRAM\n");
3523 else
3524 vdbg_printk(TPACPI_DBG_RFKILL,
3525 "bluestooth state saved to NVRAM\n");
3528 static void bluetooth_exit(void)
3530 bluetooth_shutdown();
3532 if (tpacpi_bluetooth_rfkill)
3533 rfkill_unregister(tpacpi_bluetooth_rfkill);
3535 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3536 &bluetooth_attr_group);
3539 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3541 int res;
3542 int status = 0;
3544 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3545 "initializing bluetooth subdriver\n");
3547 TPACPI_ACPIHANDLE_INIT(hkey);
3549 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3550 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3551 tp_features.bluetooth = hkey_handle &&
3552 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3554 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3555 "bluetooth is %s, status 0x%02x\n",
3556 str_supported(tp_features.bluetooth),
3557 status);
3559 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3560 if (dbg_bluetoothemul) {
3561 tp_features.bluetooth = 1;
3562 printk(TPACPI_INFO
3563 "bluetooth switch emulation enabled\n");
3564 } else
3565 #endif
3566 if (tp_features.bluetooth &&
3567 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3568 /* no bluetooth hardware present in system */
3569 tp_features.bluetooth = 0;
3570 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3571 "bluetooth hardware not installed\n");
3574 if (!tp_features.bluetooth)
3575 return 1;
3577 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3578 &bluetooth_attr_group);
3579 if (res)
3580 return res;
3582 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3583 &tpacpi_bluetooth_rfkill,
3584 RFKILL_TYPE_BLUETOOTH,
3585 TPACPI_RFK_BLUETOOTH_SW_NAME,
3586 true,
3587 tpacpi_bluetooth_rfk_set,
3588 tpacpi_bluetooth_rfk_get);
3589 if (res) {
3590 bluetooth_exit();
3591 return res;
3594 return 0;
3597 /* procfs -------------------------------------------------------------- */
3598 static int bluetooth_read(char *p)
3600 int len = 0;
3601 int status = bluetooth_get_radiosw();
3603 if (!tp_features.bluetooth)
3604 len += sprintf(p + len, "status:\t\tnot supported\n");
3605 else {
3606 len += sprintf(p + len, "status:\t\t%s\n",
3607 (status == RFKILL_STATE_UNBLOCKED) ?
3608 "enabled" : "disabled");
3609 len += sprintf(p + len, "commands:\tenable, disable\n");
3612 return len;
3615 static int bluetooth_write(char *buf)
3617 char *cmd;
3618 int state = -1;
3620 if (!tp_features.bluetooth)
3621 return -ENODEV;
3623 while ((cmd = next_cmd(&buf))) {
3624 if (strlencmp(cmd, "enable") == 0) {
3625 state = 1;
3626 } else if (strlencmp(cmd, "disable") == 0) {
3627 state = 0;
3628 } else
3629 return -EINVAL;
3632 if (state != -1) {
3633 tpacpi_disclose_usertask("procfs bluetooth",
3634 "attempt to %s\n",
3635 state ? "enable" : "disable");
3636 bluetooth_set_radiosw(state, 1);
3639 return 0;
3642 static struct ibm_struct bluetooth_driver_data = {
3643 .name = "bluetooth",
3644 .read = bluetooth_read,
3645 .write = bluetooth_write,
3646 .exit = bluetooth_exit,
3647 .suspend = bluetooth_suspend,
3648 .shutdown = bluetooth_shutdown,
3651 /*************************************************************************
3652 * Wan subdriver
3655 enum {
3656 /* ACPI GWAN/SWAN bits */
3657 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3658 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3659 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3660 off / last state */
3663 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3665 static struct rfkill *tpacpi_wan_rfkill;
3667 static void wan_suspend(pm_message_t state)
3669 /* Try to make sure radio will resume powered off */
3670 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3671 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3672 vdbg_printk(TPACPI_DBG_RFKILL,
3673 "WWAN power down on resume request failed\n");
3676 static int wan_get_radiosw(void)
3678 int status;
3680 if (!tp_features.wan)
3681 return -ENODEV;
3683 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3684 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3685 return RFKILL_STATE_HARD_BLOCKED;
3687 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3688 if (dbg_wwanemul)
3689 return (tpacpi_wwan_emulstate) ?
3690 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3691 #endif
3693 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3694 return -EIO;
3696 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3697 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3700 static void wan_update_rfk(void)
3702 int status;
3704 if (!tpacpi_wan_rfkill)
3705 return;
3707 status = wan_get_radiosw();
3708 if (status < 0)
3709 return;
3710 rfkill_force_state(tpacpi_wan_rfkill, status);
3712 vdbg_printk(TPACPI_DBG_RFKILL,
3713 "forced rfkill state to %d\n",
3714 status);
3717 static int wan_set_radiosw(int radio_on, int update_rfk)
3719 int status;
3721 if (!tp_features.wan)
3722 return -ENODEV;
3724 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3725 * reason to risk weird behaviour. */
3726 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3727 && radio_on)
3728 return -EPERM;
3730 vdbg_printk(TPACPI_DBG_RFKILL,
3731 "will %s WWAN\n", radio_on ? "enable" : "disable");
3733 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3734 if (dbg_wwanemul) {
3735 tpacpi_wwan_emulstate = !!radio_on;
3736 if (update_rfk)
3737 wan_update_rfk();
3738 return 0;
3740 #endif
3742 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3743 if (radio_on)
3744 status = TP_ACPI_WANCARD_RADIOSSW;
3745 else
3746 status = 0;
3747 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3748 return -EIO;
3750 if (update_rfk)
3751 wan_update_rfk();
3753 return 0;
3756 /* sysfs wan enable ---------------------------------------------------- */
3757 static ssize_t wan_enable_show(struct device *dev,
3758 struct device_attribute *attr,
3759 char *buf)
3761 int status;
3763 printk_deprecated_rfkill_attribute("wwan_enable");
3765 status = wan_get_radiosw();
3766 if (status < 0)
3767 return status;
3769 return snprintf(buf, PAGE_SIZE, "%d\n",
3770 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3773 static ssize_t wan_enable_store(struct device *dev,
3774 struct device_attribute *attr,
3775 const char *buf, size_t count)
3777 unsigned long t;
3778 int res;
3780 printk_deprecated_rfkill_attribute("wwan_enable");
3782 if (parse_strtoul(buf, 1, &t))
3783 return -EINVAL;
3785 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3787 res = wan_set_radiosw(t, 1);
3789 return (res) ? res : count;
3792 static struct device_attribute dev_attr_wan_enable =
3793 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3794 wan_enable_show, wan_enable_store);
3796 /* --------------------------------------------------------------------- */
3798 static struct attribute *wan_attributes[] = {
3799 &dev_attr_wan_enable.attr,
3800 NULL
3803 static const struct attribute_group wan_attr_group = {
3804 .attrs = wan_attributes,
3807 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3809 int wans = wan_get_radiosw();
3811 if (wans < 0)
3812 return wans;
3814 *state = wans;
3815 return 0;
3818 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3820 dbg_printk(TPACPI_DBG_RFKILL,
3821 "request to change radio state to %d\n", state);
3822 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3825 static void wan_shutdown(void)
3827 /* Order firmware to save current state to NVRAM */
3828 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3829 TP_ACPI_WGSV_SAVE_STATE))
3830 printk(TPACPI_NOTICE
3831 "failed to save WWAN state to NVRAM\n");
3832 else
3833 vdbg_printk(TPACPI_DBG_RFKILL,
3834 "WWAN state saved to NVRAM\n");
3837 static void wan_exit(void)
3839 wan_shutdown();
3841 if (tpacpi_wan_rfkill)
3842 rfkill_unregister(tpacpi_wan_rfkill);
3844 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3845 &wan_attr_group);
3848 static int __init wan_init(struct ibm_init_struct *iibm)
3850 int res;
3851 int status = 0;
3853 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3854 "initializing wan subdriver\n");
3856 TPACPI_ACPIHANDLE_INIT(hkey);
3858 tp_features.wan = hkey_handle &&
3859 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3861 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3862 "wan is %s, status 0x%02x\n",
3863 str_supported(tp_features.wan),
3864 status);
3866 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3867 if (dbg_wwanemul) {
3868 tp_features.wan = 1;
3869 printk(TPACPI_INFO
3870 "wwan switch emulation enabled\n");
3871 } else
3872 #endif
3873 if (tp_features.wan &&
3874 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3875 /* no wan hardware present in system */
3876 tp_features.wan = 0;
3877 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3878 "wan hardware not installed\n");
3881 if (!tp_features.wan)
3882 return 1;
3884 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3885 &wan_attr_group);
3886 if (res)
3887 return res;
3889 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3890 &tpacpi_wan_rfkill,
3891 RFKILL_TYPE_WWAN,
3892 TPACPI_RFK_WWAN_SW_NAME,
3893 true,
3894 tpacpi_wan_rfk_set,
3895 tpacpi_wan_rfk_get);
3896 if (res) {
3897 wan_exit();
3898 return res;
3901 return 0;
3904 /* procfs -------------------------------------------------------------- */
3905 static int wan_read(char *p)
3907 int len = 0;
3908 int status = wan_get_radiosw();
3910 tpacpi_disclose_usertask("procfs wan", "read");
3912 if (!tp_features.wan)
3913 len += sprintf(p + len, "status:\t\tnot supported\n");
3914 else {
3915 len += sprintf(p + len, "status:\t\t%s\n",
3916 (status == RFKILL_STATE_UNBLOCKED) ?
3917 "enabled" : "disabled");
3918 len += sprintf(p + len, "commands:\tenable, disable\n");
3921 return len;
3924 static int wan_write(char *buf)
3926 char *cmd;
3927 int state = -1;
3929 if (!tp_features.wan)
3930 return -ENODEV;
3932 while ((cmd = next_cmd(&buf))) {
3933 if (strlencmp(cmd, "enable") == 0) {
3934 state = 1;
3935 } else if (strlencmp(cmd, "disable") == 0) {
3936 state = 0;
3937 } else
3938 return -EINVAL;
3941 if (state != -1) {
3942 tpacpi_disclose_usertask("procfs wan",
3943 "attempt to %s\n",
3944 state ? "enable" : "disable");
3945 wan_set_radiosw(state, 1);
3948 return 0;
3951 static struct ibm_struct wan_driver_data = {
3952 .name = "wan",
3953 .read = wan_read,
3954 .write = wan_write,
3955 .exit = wan_exit,
3956 .suspend = wan_suspend,
3957 .shutdown = wan_shutdown,
3960 /*************************************************************************
3961 * UWB subdriver
3964 enum {
3965 /* ACPI GUWB/SUWB bits */
3966 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3967 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3970 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3972 static struct rfkill *tpacpi_uwb_rfkill;
3974 static int uwb_get_radiosw(void)
3976 int status;
3978 if (!tp_features.uwb)
3979 return -ENODEV;
3981 /* WLSW overrides UWB in firmware/hardware, reflect that */
3982 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3983 return RFKILL_STATE_HARD_BLOCKED;
3985 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3986 if (dbg_uwbemul)
3987 return (tpacpi_uwb_emulstate) ?
3988 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3989 #endif
3991 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3992 return -EIO;
3994 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3995 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3998 static void uwb_update_rfk(void)
4000 int status;
4002 if (!tpacpi_uwb_rfkill)
4003 return;
4005 status = uwb_get_radiosw();
4006 if (status < 0)
4007 return;
4008 rfkill_force_state(tpacpi_uwb_rfkill, status);
4010 vdbg_printk(TPACPI_DBG_RFKILL,
4011 "forced rfkill state to %d\n",
4012 status);
4015 static int uwb_set_radiosw(int radio_on, int update_rfk)
4017 int status;
4019 if (!tp_features.uwb)
4020 return -ENODEV;
4022 /* WLSW overrides UWB in firmware/hardware, but there is no
4023 * reason to risk weird behaviour. */
4024 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
4025 && radio_on)
4026 return -EPERM;
4028 vdbg_printk(TPACPI_DBG_RFKILL,
4029 "will %s UWB\n", radio_on ? "enable" : "disable");
4031 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4032 if (dbg_uwbemul) {
4033 tpacpi_uwb_emulstate = !!radio_on;
4034 if (update_rfk)
4035 uwb_update_rfk();
4036 return 0;
4038 #endif
4040 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
4041 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4042 return -EIO;
4044 if (update_rfk)
4045 uwb_update_rfk();
4047 return 0;
4050 /* --------------------------------------------------------------------- */
4052 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
4054 int uwbs = uwb_get_radiosw();
4056 if (uwbs < 0)
4057 return uwbs;
4059 *state = uwbs;
4060 return 0;
4063 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
4065 dbg_printk(TPACPI_DBG_RFKILL,
4066 "request to change radio state to %d\n", state);
4067 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
4070 static void uwb_exit(void)
4072 if (tpacpi_uwb_rfkill)
4073 rfkill_unregister(tpacpi_uwb_rfkill);
4076 static int __init uwb_init(struct ibm_init_struct *iibm)
4078 int res;
4079 int status = 0;
4081 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4082 "initializing uwb subdriver\n");
4084 TPACPI_ACPIHANDLE_INIT(hkey);
4086 tp_features.uwb = hkey_handle &&
4087 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4089 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4090 "uwb is %s, status 0x%02x\n",
4091 str_supported(tp_features.uwb),
4092 status);
4094 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4095 if (dbg_uwbemul) {
4096 tp_features.uwb = 1;
4097 printk(TPACPI_INFO
4098 "uwb switch emulation enabled\n");
4099 } else
4100 #endif
4101 if (tp_features.uwb &&
4102 !(status & TP_ACPI_UWB_HWPRESENT)) {
4103 /* no uwb hardware present in system */
4104 tp_features.uwb = 0;
4105 dbg_printk(TPACPI_DBG_INIT,
4106 "uwb hardware not installed\n");
4109 if (!tp_features.uwb)
4110 return 1;
4112 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4113 &tpacpi_uwb_rfkill,
4114 RFKILL_TYPE_UWB,
4115 TPACPI_RFK_UWB_SW_NAME,
4116 false,
4117 tpacpi_uwb_rfk_set,
4118 tpacpi_uwb_rfk_get);
4120 return res;
4123 static struct ibm_struct uwb_driver_data = {
4124 .name = "uwb",
4125 .exit = uwb_exit,
4126 .flags.experimental = 1,
4129 /*************************************************************************
4130 * Video subdriver
4133 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4135 enum video_access_mode {
4136 TPACPI_VIDEO_NONE = 0,
4137 TPACPI_VIDEO_570, /* 570 */
4138 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4139 TPACPI_VIDEO_NEW, /* all others */
4142 enum { /* video status flags, based on VIDEO_570 */
4143 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4144 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4145 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4148 enum { /* TPACPI_VIDEO_570 constants */
4149 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4150 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4151 * video_status_flags */
4152 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4153 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4156 static enum video_access_mode video_supported;
4157 static int video_orig_autosw;
4159 static int video_autosw_get(void);
4160 static int video_autosw_set(int enable);
4162 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4164 static int __init video_init(struct ibm_init_struct *iibm)
4166 int ivga;
4168 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4170 TPACPI_ACPIHANDLE_INIT(vid);
4171 TPACPI_ACPIHANDLE_INIT(vid2);
4173 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4174 /* G41, assume IVGA doesn't change */
4175 vid_handle = vid2_handle;
4177 if (!vid_handle)
4178 /* video switching not supported on R30, R31 */
4179 video_supported = TPACPI_VIDEO_NONE;
4180 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4181 /* 570 */
4182 video_supported = TPACPI_VIDEO_570;
4183 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4184 /* 600e/x, 770e, 770x */
4185 video_supported = TPACPI_VIDEO_770;
4186 else
4187 /* all others */
4188 video_supported = TPACPI_VIDEO_NEW;
4190 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4191 str_supported(video_supported != TPACPI_VIDEO_NONE),
4192 video_supported);
4194 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4197 static void video_exit(void)
4199 dbg_printk(TPACPI_DBG_EXIT,
4200 "restoring original video autoswitch mode\n");
4201 if (video_autosw_set(video_orig_autosw))
4202 printk(TPACPI_ERR "error while trying to restore original "
4203 "video autoswitch mode\n");
4206 static int video_outputsw_get(void)
4208 int status = 0;
4209 int i;
4211 switch (video_supported) {
4212 case TPACPI_VIDEO_570:
4213 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4214 TP_ACPI_VIDEO_570_PHSCMD))
4215 return -EIO;
4216 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4217 break;
4218 case TPACPI_VIDEO_770:
4219 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4220 return -EIO;
4221 if (i)
4222 status |= TP_ACPI_VIDEO_S_LCD;
4223 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4224 return -EIO;
4225 if (i)
4226 status |= TP_ACPI_VIDEO_S_CRT;
4227 break;
4228 case TPACPI_VIDEO_NEW:
4229 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4230 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4231 return -EIO;
4232 if (i)
4233 status |= TP_ACPI_VIDEO_S_CRT;
4235 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4236 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4237 return -EIO;
4238 if (i)
4239 status |= TP_ACPI_VIDEO_S_LCD;
4240 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4241 return -EIO;
4242 if (i)
4243 status |= TP_ACPI_VIDEO_S_DVI;
4244 break;
4245 default:
4246 return -ENOSYS;
4249 return status;
4252 static int video_outputsw_set(int status)
4254 int autosw;
4255 int res = 0;
4257 switch (video_supported) {
4258 case TPACPI_VIDEO_570:
4259 res = acpi_evalf(NULL, NULL,
4260 "\\_SB.PHS2", "vdd",
4261 TP_ACPI_VIDEO_570_PHS2CMD,
4262 status | TP_ACPI_VIDEO_570_PHS2SET);
4263 break;
4264 case TPACPI_VIDEO_770:
4265 autosw = video_autosw_get();
4266 if (autosw < 0)
4267 return autosw;
4269 res = video_autosw_set(1);
4270 if (res)
4271 return res;
4272 res = acpi_evalf(vid_handle, NULL,
4273 "ASWT", "vdd", status * 0x100, 0);
4274 if (!autosw && video_autosw_set(autosw)) {
4275 printk(TPACPI_ERR
4276 "video auto-switch left enabled due to error\n");
4277 return -EIO;
4279 break;
4280 case TPACPI_VIDEO_NEW:
4281 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4282 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4283 break;
4284 default:
4285 return -ENOSYS;
4288 return (res)? 0 : -EIO;
4291 static int video_autosw_get(void)
4293 int autosw = 0;
4295 switch (video_supported) {
4296 case TPACPI_VIDEO_570:
4297 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4298 return -EIO;
4299 break;
4300 case TPACPI_VIDEO_770:
4301 case TPACPI_VIDEO_NEW:
4302 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4303 return -EIO;
4304 break;
4305 default:
4306 return -ENOSYS;
4309 return autosw & 1;
4312 static int video_autosw_set(int enable)
4314 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4315 return -EIO;
4316 return 0;
4319 static int video_outputsw_cycle(void)
4321 int autosw = video_autosw_get();
4322 int res;
4324 if (autosw < 0)
4325 return autosw;
4327 switch (video_supported) {
4328 case TPACPI_VIDEO_570:
4329 res = video_autosw_set(1);
4330 if (res)
4331 return res;
4332 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4333 break;
4334 case TPACPI_VIDEO_770:
4335 case TPACPI_VIDEO_NEW:
4336 res = video_autosw_set(1);
4337 if (res)
4338 return res;
4339 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4340 break;
4341 default:
4342 return -ENOSYS;
4344 if (!autosw && video_autosw_set(autosw)) {
4345 printk(TPACPI_ERR
4346 "video auto-switch left enabled due to error\n");
4347 return -EIO;
4350 return (res)? 0 : -EIO;
4353 static int video_expand_toggle(void)
4355 switch (video_supported) {
4356 case TPACPI_VIDEO_570:
4357 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4358 0 : -EIO;
4359 case TPACPI_VIDEO_770:
4360 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4361 0 : -EIO;
4362 case TPACPI_VIDEO_NEW:
4363 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4364 0 : -EIO;
4365 default:
4366 return -ENOSYS;
4368 /* not reached */
4371 static int video_read(char *p)
4373 int status, autosw;
4374 int len = 0;
4376 if (video_supported == TPACPI_VIDEO_NONE) {
4377 len += sprintf(p + len, "status:\t\tnot supported\n");
4378 return len;
4381 status = video_outputsw_get();
4382 if (status < 0)
4383 return status;
4385 autosw = video_autosw_get();
4386 if (autosw < 0)
4387 return autosw;
4389 len += sprintf(p + len, "status:\t\tsupported\n");
4390 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4391 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4392 if (video_supported == TPACPI_VIDEO_NEW)
4393 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4394 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4395 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4396 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4397 if (video_supported == TPACPI_VIDEO_NEW)
4398 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4399 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4400 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4402 return len;
4405 static int video_write(char *buf)
4407 char *cmd;
4408 int enable, disable, status;
4409 int res;
4411 if (video_supported == TPACPI_VIDEO_NONE)
4412 return -ENODEV;
4414 enable = 0;
4415 disable = 0;
4417 while ((cmd = next_cmd(&buf))) {
4418 if (strlencmp(cmd, "lcd_enable") == 0) {
4419 enable |= TP_ACPI_VIDEO_S_LCD;
4420 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4421 disable |= TP_ACPI_VIDEO_S_LCD;
4422 } else if (strlencmp(cmd, "crt_enable") == 0) {
4423 enable |= TP_ACPI_VIDEO_S_CRT;
4424 } else if (strlencmp(cmd, "crt_disable") == 0) {
4425 disable |= TP_ACPI_VIDEO_S_CRT;
4426 } else if (video_supported == TPACPI_VIDEO_NEW &&
4427 strlencmp(cmd, "dvi_enable") == 0) {
4428 enable |= TP_ACPI_VIDEO_S_DVI;
4429 } else if (video_supported == TPACPI_VIDEO_NEW &&
4430 strlencmp(cmd, "dvi_disable") == 0) {
4431 disable |= TP_ACPI_VIDEO_S_DVI;
4432 } else if (strlencmp(cmd, "auto_enable") == 0) {
4433 res = video_autosw_set(1);
4434 if (res)
4435 return res;
4436 } else if (strlencmp(cmd, "auto_disable") == 0) {
4437 res = video_autosw_set(0);
4438 if (res)
4439 return res;
4440 } else if (strlencmp(cmd, "video_switch") == 0) {
4441 res = video_outputsw_cycle();
4442 if (res)
4443 return res;
4444 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4445 res = video_expand_toggle();
4446 if (res)
4447 return res;
4448 } else
4449 return -EINVAL;
4452 if (enable || disable) {
4453 status = video_outputsw_get();
4454 if (status < 0)
4455 return status;
4456 res = video_outputsw_set((status & ~disable) | enable);
4457 if (res)
4458 return res;
4461 return 0;
4464 static struct ibm_struct video_driver_data = {
4465 .name = "video",
4466 .read = video_read,
4467 .write = video_write,
4468 .exit = video_exit,
4471 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4473 /*************************************************************************
4474 * Light (thinklight) subdriver
4477 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4478 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4480 static int light_get_status(void)
4482 int status = 0;
4484 if (tp_features.light_status) {
4485 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4486 return -EIO;
4487 return (!!status);
4490 return -ENXIO;
4493 static int light_set_status(int status)
4495 int rc;
4497 if (tp_features.light) {
4498 if (cmos_handle) {
4499 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4500 (status)?
4501 TP_CMOS_THINKLIGHT_ON :
4502 TP_CMOS_THINKLIGHT_OFF);
4503 } else {
4504 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4505 (status)? 1 : 0);
4507 return (rc)? 0 : -EIO;
4510 return -ENXIO;
4513 static void light_set_status_worker(struct work_struct *work)
4515 struct tpacpi_led_classdev *data =
4516 container_of(work, struct tpacpi_led_classdev, work);
4518 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4519 light_set_status((data->new_state != TPACPI_LED_OFF));
4522 static void light_sysfs_set(struct led_classdev *led_cdev,
4523 enum led_brightness brightness)
4525 struct tpacpi_led_classdev *data =
4526 container_of(led_cdev,
4527 struct tpacpi_led_classdev,
4528 led_classdev);
4529 data->new_state = (brightness != LED_OFF) ?
4530 TPACPI_LED_ON : TPACPI_LED_OFF;
4531 queue_work(tpacpi_wq, &data->work);
4534 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4536 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4539 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4540 .led_classdev = {
4541 .name = "tpacpi::thinklight",
4542 .brightness_set = &light_sysfs_set,
4543 .brightness_get = &light_sysfs_get,
4547 static int __init light_init(struct ibm_init_struct *iibm)
4549 int rc;
4551 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4553 TPACPI_ACPIHANDLE_INIT(ledb);
4554 TPACPI_ACPIHANDLE_INIT(lght);
4555 TPACPI_ACPIHANDLE_INIT(cmos);
4556 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4558 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4559 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4561 if (tp_features.light)
4562 /* light status not supported on
4563 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4564 tp_features.light_status =
4565 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4567 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4568 str_supported(tp_features.light),
4569 str_supported(tp_features.light_status));
4571 if (!tp_features.light)
4572 return 1;
4574 rc = led_classdev_register(&tpacpi_pdev->dev,
4575 &tpacpi_led_thinklight.led_classdev);
4577 if (rc < 0) {
4578 tp_features.light = 0;
4579 tp_features.light_status = 0;
4580 } else {
4581 rc = 0;
4584 return rc;
4587 static void light_exit(void)
4589 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4590 if (work_pending(&tpacpi_led_thinklight.work))
4591 flush_workqueue(tpacpi_wq);
4594 static int light_read(char *p)
4596 int len = 0;
4597 int status;
4599 if (!tp_features.light) {
4600 len += sprintf(p + len, "status:\t\tnot supported\n");
4601 } else if (!tp_features.light_status) {
4602 len += sprintf(p + len, "status:\t\tunknown\n");
4603 len += sprintf(p + len, "commands:\ton, off\n");
4604 } else {
4605 status = light_get_status();
4606 if (status < 0)
4607 return status;
4608 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4609 len += sprintf(p + len, "commands:\ton, off\n");
4612 return len;
4615 static int light_write(char *buf)
4617 char *cmd;
4618 int newstatus = 0;
4620 if (!tp_features.light)
4621 return -ENODEV;
4623 while ((cmd = next_cmd(&buf))) {
4624 if (strlencmp(cmd, "on") == 0) {
4625 newstatus = 1;
4626 } else if (strlencmp(cmd, "off") == 0) {
4627 newstatus = 0;
4628 } else
4629 return -EINVAL;
4632 return light_set_status(newstatus);
4635 static struct ibm_struct light_driver_data = {
4636 .name = "light",
4637 .read = light_read,
4638 .write = light_write,
4639 .exit = light_exit,
4642 /*************************************************************************
4643 * CMOS subdriver
4646 /* sysfs cmos_command -------------------------------------------------- */
4647 static ssize_t cmos_command_store(struct device *dev,
4648 struct device_attribute *attr,
4649 const char *buf, size_t count)
4651 unsigned long cmos_cmd;
4652 int res;
4654 if (parse_strtoul(buf, 21, &cmos_cmd))
4655 return -EINVAL;
4657 res = issue_thinkpad_cmos_command(cmos_cmd);
4658 return (res)? res : count;
4661 static struct device_attribute dev_attr_cmos_command =
4662 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4664 /* --------------------------------------------------------------------- */
4666 static int __init cmos_init(struct ibm_init_struct *iibm)
4668 int res;
4670 vdbg_printk(TPACPI_DBG_INIT,
4671 "initializing cmos commands subdriver\n");
4673 TPACPI_ACPIHANDLE_INIT(cmos);
4675 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4676 str_supported(cmos_handle != NULL));
4678 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4679 if (res)
4680 return res;
4682 return (cmos_handle)? 0 : 1;
4685 static void cmos_exit(void)
4687 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4690 static int cmos_read(char *p)
4692 int len = 0;
4694 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4695 R30, R31, T20-22, X20-21 */
4696 if (!cmos_handle)
4697 len += sprintf(p + len, "status:\t\tnot supported\n");
4698 else {
4699 len += sprintf(p + len, "status:\t\tsupported\n");
4700 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4703 return len;
4706 static int cmos_write(char *buf)
4708 char *cmd;
4709 int cmos_cmd, res;
4711 while ((cmd = next_cmd(&buf))) {
4712 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4713 cmos_cmd >= 0 && cmos_cmd <= 21) {
4714 /* cmos_cmd set */
4715 } else
4716 return -EINVAL;
4718 res = issue_thinkpad_cmos_command(cmos_cmd);
4719 if (res)
4720 return res;
4723 return 0;
4726 static struct ibm_struct cmos_driver_data = {
4727 .name = "cmos",
4728 .read = cmos_read,
4729 .write = cmos_write,
4730 .exit = cmos_exit,
4733 /*************************************************************************
4734 * LED subdriver
4737 enum led_access_mode {
4738 TPACPI_LED_NONE = 0,
4739 TPACPI_LED_570, /* 570 */
4740 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4741 TPACPI_LED_NEW, /* all others */
4744 enum { /* For TPACPI_LED_OLD */
4745 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4746 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4747 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4750 static enum led_access_mode led_supported;
4752 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4753 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4754 /* T20-22, X20-21 */
4755 "LED", /* all others */
4756 ); /* R30, R31 */
4758 #define TPACPI_LED_NUMLEDS 16
4759 static struct tpacpi_led_classdev *tpacpi_leds;
4760 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4761 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4762 /* there's a limit of 19 chars + NULL before 2.6.26 */
4763 "tpacpi::power",
4764 "tpacpi:orange:batt",
4765 "tpacpi:green:batt",
4766 "tpacpi::dock_active",
4767 "tpacpi::bay_active",
4768 "tpacpi::dock_batt",
4769 "tpacpi::unknown_led",
4770 "tpacpi::standby",
4771 "tpacpi::dock_status1",
4772 "tpacpi::dock_status2",
4773 "tpacpi::unknown_led2",
4774 "tpacpi::unknown_led3",
4775 "tpacpi::thinkvantage",
4777 #define TPACPI_SAFE_LEDS 0x1081U
4779 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4781 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4782 return false;
4783 #else
4784 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4785 #endif
4788 static int led_get_status(const unsigned int led)
4790 int status;
4791 enum led_status_t led_s;
4793 switch (led_supported) {
4794 case TPACPI_LED_570:
4795 if (!acpi_evalf(ec_handle,
4796 &status, "GLED", "dd", 1 << led))
4797 return -EIO;
4798 led_s = (status == 0)?
4799 TPACPI_LED_OFF :
4800 ((status == 1)?
4801 TPACPI_LED_ON :
4802 TPACPI_LED_BLINK);
4803 tpacpi_led_state_cache[led] = led_s;
4804 return led_s;
4805 default:
4806 return -ENXIO;
4809 /* not reached */
4812 static int led_set_status(const unsigned int led,
4813 const enum led_status_t ledstatus)
4815 /* off, on, blink. Index is led_status_t */
4816 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4817 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4819 int rc = 0;
4821 switch (led_supported) {
4822 case TPACPI_LED_570:
4823 /* 570 */
4824 if (unlikely(led > 7))
4825 return -EINVAL;
4826 if (unlikely(tpacpi_is_led_restricted(led)))
4827 return -EPERM;
4828 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4829 (1 << led), led_sled_arg1[ledstatus]))
4830 rc = -EIO;
4831 break;
4832 case TPACPI_LED_OLD:
4833 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4834 if (unlikely(led > 7))
4835 return -EINVAL;
4836 if (unlikely(tpacpi_is_led_restricted(led)))
4837 return -EPERM;
4838 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4839 if (rc >= 0)
4840 rc = ec_write(TPACPI_LED_EC_HLBL,
4841 (ledstatus == TPACPI_LED_BLINK) << led);
4842 if (rc >= 0)
4843 rc = ec_write(TPACPI_LED_EC_HLCL,
4844 (ledstatus != TPACPI_LED_OFF) << led);
4845 break;
4846 case TPACPI_LED_NEW:
4847 /* all others */
4848 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4849 return -EINVAL;
4850 if (unlikely(tpacpi_is_led_restricted(led)))
4851 return -EPERM;
4852 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4853 led, led_led_arg1[ledstatus]))
4854 rc = -EIO;
4855 break;
4856 default:
4857 rc = -ENXIO;
4860 if (!rc)
4861 tpacpi_led_state_cache[led] = ledstatus;
4863 return rc;
4866 static void led_set_status_worker(struct work_struct *work)
4868 struct tpacpi_led_classdev *data =
4869 container_of(work, struct tpacpi_led_classdev, work);
4871 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4872 led_set_status(data->led, data->new_state);
4875 static void led_sysfs_set(struct led_classdev *led_cdev,
4876 enum led_brightness brightness)
4878 struct tpacpi_led_classdev *data = container_of(led_cdev,
4879 struct tpacpi_led_classdev, led_classdev);
4881 if (brightness == LED_OFF)
4882 data->new_state = TPACPI_LED_OFF;
4883 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4884 data->new_state = TPACPI_LED_ON;
4885 else
4886 data->new_state = TPACPI_LED_BLINK;
4888 queue_work(tpacpi_wq, &data->work);
4891 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4892 unsigned long *delay_on, unsigned long *delay_off)
4894 struct tpacpi_led_classdev *data = container_of(led_cdev,
4895 struct tpacpi_led_classdev, led_classdev);
4897 /* Can we choose the flash rate? */
4898 if (*delay_on == 0 && *delay_off == 0) {
4899 /* yes. set them to the hardware blink rate (1 Hz) */
4900 *delay_on = 500; /* ms */
4901 *delay_off = 500; /* ms */
4902 } else if ((*delay_on != 500) || (*delay_off != 500))
4903 return -EINVAL;
4905 data->new_state = TPACPI_LED_BLINK;
4906 queue_work(tpacpi_wq, &data->work);
4908 return 0;
4911 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4913 int rc;
4915 struct tpacpi_led_classdev *data = container_of(led_cdev,
4916 struct tpacpi_led_classdev, led_classdev);
4918 rc = led_get_status(data->led);
4920 if (rc == TPACPI_LED_OFF || rc < 0)
4921 rc = LED_OFF; /* no error handling in led class :( */
4922 else
4923 rc = LED_FULL;
4925 return rc;
4928 static void led_exit(void)
4930 unsigned int i;
4932 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4933 if (tpacpi_leds[i].led_classdev.name)
4934 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4937 kfree(tpacpi_leds);
4940 static int __init tpacpi_init_led(unsigned int led)
4942 int rc;
4944 tpacpi_leds[led].led = led;
4946 /* LEDs with no name don't get registered */
4947 if (!tpacpi_led_names[led])
4948 return 0;
4950 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4951 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4952 if (led_supported == TPACPI_LED_570)
4953 tpacpi_leds[led].led_classdev.brightness_get =
4954 &led_sysfs_get;
4956 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4958 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4960 rc = led_classdev_register(&tpacpi_pdev->dev,
4961 &tpacpi_leds[led].led_classdev);
4962 if (rc < 0)
4963 tpacpi_leds[led].led_classdev.name = NULL;
4965 return rc;
4968 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4969 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4970 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4971 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4973 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4974 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4975 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4976 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4977 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4978 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4979 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4980 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4982 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4983 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4984 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4985 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4986 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4988 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4989 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4990 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4991 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4993 /* (1) - may have excess leds enabled on MSB */
4995 /* Defaults (order matters, keep last, don't reorder!) */
4996 { /* Lenovo */
4997 .vendor = PCI_VENDOR_ID_LENOVO,
4998 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4999 .quirks = 0x1fffU,
5001 { /* IBM ThinkPads with no EC version string */
5002 .vendor = PCI_VENDOR_ID_IBM,
5003 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5004 .quirks = 0x00ffU,
5006 { /* IBM ThinkPads with EC version string */
5007 .vendor = PCI_VENDOR_ID_IBM,
5008 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5009 .quirks = 0x00bfU,
5013 #undef TPACPI_LEDQ_IBM
5014 #undef TPACPI_LEDQ_LNV
5016 static int __init led_init(struct ibm_init_struct *iibm)
5018 unsigned int i;
5019 int rc;
5020 unsigned long useful_leds;
5022 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5024 TPACPI_ACPIHANDLE_INIT(led);
5026 if (!led_handle)
5027 /* led not supported on R30, R31 */
5028 led_supported = TPACPI_LED_NONE;
5029 else if (strlencmp(led_path, "SLED") == 0)
5030 /* 570 */
5031 led_supported = TPACPI_LED_570;
5032 else if (strlencmp(led_path, "SYSL") == 0)
5033 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5034 led_supported = TPACPI_LED_OLD;
5035 else
5036 /* all others */
5037 led_supported = TPACPI_LED_NEW;
5039 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5040 str_supported(led_supported), led_supported);
5042 if (led_supported == TPACPI_LED_NONE)
5043 return 1;
5045 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5046 GFP_KERNEL);
5047 if (!tpacpi_leds) {
5048 printk(TPACPI_ERR "Out of memory for LED data\n");
5049 return -ENOMEM;
5052 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5053 ARRAY_SIZE(led_useful_qtable));
5055 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5056 if (!tpacpi_is_led_restricted(i) &&
5057 test_bit(i, &useful_leds)) {
5058 rc = tpacpi_init_led(i);
5059 if (rc < 0) {
5060 led_exit();
5061 return rc;
5066 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5067 printk(TPACPI_NOTICE
5068 "warning: userspace override of important "
5069 "firmware LEDs is enabled\n");
5070 #endif
5071 return 0;
5074 #define str_led_status(s) \
5075 ((s) == TPACPI_LED_OFF ? "off" : \
5076 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5078 static int led_read(char *p)
5080 int len = 0;
5082 if (!led_supported) {
5083 len += sprintf(p + len, "status:\t\tnot supported\n");
5084 return len;
5086 len += sprintf(p + len, "status:\t\tsupported\n");
5088 if (led_supported == TPACPI_LED_570) {
5089 /* 570 */
5090 int i, status;
5091 for (i = 0; i < 8; i++) {
5092 status = led_get_status(i);
5093 if (status < 0)
5094 return -EIO;
5095 len += sprintf(p + len, "%d:\t\t%s\n",
5096 i, str_led_status(status));
5100 len += sprintf(p + len, "commands:\t"
5101 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5103 return len;
5106 static int led_write(char *buf)
5108 char *cmd;
5109 int led, rc;
5110 enum led_status_t s;
5112 if (!led_supported)
5113 return -ENODEV;
5115 while ((cmd = next_cmd(&buf))) {
5116 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5117 return -EINVAL;
5119 if (strstr(cmd, "off")) {
5120 s = TPACPI_LED_OFF;
5121 } else if (strstr(cmd, "on")) {
5122 s = TPACPI_LED_ON;
5123 } else if (strstr(cmd, "blink")) {
5124 s = TPACPI_LED_BLINK;
5125 } else {
5126 return -EINVAL;
5129 rc = led_set_status(led, s);
5130 if (rc < 0)
5131 return rc;
5134 return 0;
5137 static struct ibm_struct led_driver_data = {
5138 .name = "led",
5139 .read = led_read,
5140 .write = led_write,
5141 .exit = led_exit,
5144 /*************************************************************************
5145 * Beep subdriver
5148 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5150 #define TPACPI_BEEP_Q1 0x0001
5152 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5153 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5154 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5157 static int __init beep_init(struct ibm_init_struct *iibm)
5159 unsigned long quirks;
5161 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5163 TPACPI_ACPIHANDLE_INIT(beep);
5165 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5166 str_supported(beep_handle != NULL));
5168 quirks = tpacpi_check_quirks(beep_quirk_table,
5169 ARRAY_SIZE(beep_quirk_table));
5171 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5173 return (beep_handle)? 0 : 1;
5176 static int beep_read(char *p)
5178 int len = 0;
5180 if (!beep_handle)
5181 len += sprintf(p + len, "status:\t\tnot supported\n");
5182 else {
5183 len += sprintf(p + len, "status:\t\tsupported\n");
5184 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5187 return len;
5190 static int beep_write(char *buf)
5192 char *cmd;
5193 int beep_cmd;
5195 if (!beep_handle)
5196 return -ENODEV;
5198 while ((cmd = next_cmd(&buf))) {
5199 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5200 beep_cmd >= 0 && beep_cmd <= 17) {
5201 /* beep_cmd set */
5202 } else
5203 return -EINVAL;
5204 if (tp_features.beep_needs_two_args) {
5205 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5206 beep_cmd, 0))
5207 return -EIO;
5208 } else {
5209 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5210 beep_cmd))
5211 return -EIO;
5215 return 0;
5218 static struct ibm_struct beep_driver_data = {
5219 .name = "beep",
5220 .read = beep_read,
5221 .write = beep_write,
5224 /*************************************************************************
5225 * Thermal subdriver
5228 enum thermal_access_mode {
5229 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5230 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5231 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5232 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5233 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5236 enum { /* TPACPI_THERMAL_TPEC_* */
5237 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5238 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5239 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5242 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5243 struct ibm_thermal_sensors_struct {
5244 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5247 static enum thermal_access_mode thermal_read_mode;
5249 /* idx is zero-based */
5250 static int thermal_get_sensor(int idx, s32 *value)
5252 int t;
5253 s8 tmp;
5254 char tmpi[5];
5256 t = TP_EC_THERMAL_TMP0;
5258 switch (thermal_read_mode) {
5259 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5260 case TPACPI_THERMAL_TPEC_16:
5261 if (idx >= 8 && idx <= 15) {
5262 t = TP_EC_THERMAL_TMP8;
5263 idx -= 8;
5265 /* fallthrough */
5266 #endif
5267 case TPACPI_THERMAL_TPEC_8:
5268 if (idx <= 7) {
5269 if (!acpi_ec_read(t + idx, &tmp))
5270 return -EIO;
5271 *value = tmp * 1000;
5272 return 0;
5274 break;
5276 case TPACPI_THERMAL_ACPI_UPDT:
5277 if (idx <= 7) {
5278 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5279 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5280 return -EIO;
5281 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5282 return -EIO;
5283 *value = (t - 2732) * 100;
5284 return 0;
5286 break;
5288 case TPACPI_THERMAL_ACPI_TMP07:
5289 if (idx <= 7) {
5290 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5291 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5292 return -EIO;
5293 if (t > 127 || t < -127)
5294 t = TP_EC_THERMAL_TMP_NA;
5295 *value = t * 1000;
5296 return 0;
5298 break;
5300 case TPACPI_THERMAL_NONE:
5301 default:
5302 return -ENOSYS;
5305 return -EINVAL;
5308 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5310 int res, i;
5311 int n;
5313 n = 8;
5314 i = 0;
5316 if (!s)
5317 return -EINVAL;
5319 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5320 n = 16;
5322 for (i = 0 ; i < n; i++) {
5323 res = thermal_get_sensor(i, &s->temp[i]);
5324 if (res)
5325 return res;
5328 return n;
5331 /* sysfs temp##_input -------------------------------------------------- */
5333 static ssize_t thermal_temp_input_show(struct device *dev,
5334 struct device_attribute *attr,
5335 char *buf)
5337 struct sensor_device_attribute *sensor_attr =
5338 to_sensor_dev_attr(attr);
5339 int idx = sensor_attr->index;
5340 s32 value;
5341 int res;
5343 res = thermal_get_sensor(idx, &value);
5344 if (res)
5345 return res;
5346 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5347 return -ENXIO;
5349 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5352 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5353 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5354 thermal_temp_input_show, NULL, _idxB)
5356 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5357 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5358 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5359 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5360 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5361 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5362 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5363 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5364 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5365 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5366 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5367 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5368 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5369 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5370 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5371 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5372 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5375 #define THERMAL_ATTRS(X) \
5376 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5378 static struct attribute *thermal_temp_input_attr[] = {
5379 THERMAL_ATTRS(8),
5380 THERMAL_ATTRS(9),
5381 THERMAL_ATTRS(10),
5382 THERMAL_ATTRS(11),
5383 THERMAL_ATTRS(12),
5384 THERMAL_ATTRS(13),
5385 THERMAL_ATTRS(14),
5386 THERMAL_ATTRS(15),
5387 THERMAL_ATTRS(0),
5388 THERMAL_ATTRS(1),
5389 THERMAL_ATTRS(2),
5390 THERMAL_ATTRS(3),
5391 THERMAL_ATTRS(4),
5392 THERMAL_ATTRS(5),
5393 THERMAL_ATTRS(6),
5394 THERMAL_ATTRS(7),
5395 NULL
5398 static const struct attribute_group thermal_temp_input16_group = {
5399 .attrs = thermal_temp_input_attr
5402 static const struct attribute_group thermal_temp_input8_group = {
5403 .attrs = &thermal_temp_input_attr[8]
5406 #undef THERMAL_SENSOR_ATTR_TEMP
5407 #undef THERMAL_ATTRS
5409 /* --------------------------------------------------------------------- */
5411 static int __init thermal_init(struct ibm_init_struct *iibm)
5413 u8 t, ta1, ta2;
5414 int i;
5415 int acpi_tmp7;
5416 int res;
5418 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5420 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5422 if (thinkpad_id.ec_model) {
5424 * Direct EC access mode: sensors at registers
5425 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5426 * non-implemented, thermal sensors return 0x80 when
5427 * not available
5430 ta1 = ta2 = 0;
5431 for (i = 0; i < 8; i++) {
5432 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5433 ta1 |= t;
5434 } else {
5435 ta1 = 0;
5436 break;
5438 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5439 ta2 |= t;
5440 } else {
5441 ta1 = 0;
5442 break;
5445 if (ta1 == 0) {
5446 /* This is sheer paranoia, but we handle it anyway */
5447 if (acpi_tmp7) {
5448 printk(TPACPI_ERR
5449 "ThinkPad ACPI EC access misbehaving, "
5450 "falling back to ACPI TMPx access "
5451 "mode\n");
5452 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5453 } else {
5454 printk(TPACPI_ERR
5455 "ThinkPad ACPI EC access misbehaving, "
5456 "disabling thermal sensors access\n");
5457 thermal_read_mode = TPACPI_THERMAL_NONE;
5459 } else {
5460 thermal_read_mode =
5461 (ta2 != 0) ?
5462 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5464 } else if (acpi_tmp7) {
5465 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5466 /* 600e/x, 770e, 770x */
5467 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5468 } else {
5469 /* Standard ACPI TMPx access, max 8 sensors */
5470 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5472 } else {
5473 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5474 thermal_read_mode = TPACPI_THERMAL_NONE;
5477 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5478 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5479 thermal_read_mode);
5481 switch (thermal_read_mode) {
5482 case TPACPI_THERMAL_TPEC_16:
5483 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5484 &thermal_temp_input16_group);
5485 if (res)
5486 return res;
5487 break;
5488 case TPACPI_THERMAL_TPEC_8:
5489 case TPACPI_THERMAL_ACPI_TMP07:
5490 case TPACPI_THERMAL_ACPI_UPDT:
5491 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5492 &thermal_temp_input8_group);
5493 if (res)
5494 return res;
5495 break;
5496 case TPACPI_THERMAL_NONE:
5497 default:
5498 return 1;
5501 return 0;
5504 static void thermal_exit(void)
5506 switch (thermal_read_mode) {
5507 case TPACPI_THERMAL_TPEC_16:
5508 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5509 &thermal_temp_input16_group);
5510 break;
5511 case TPACPI_THERMAL_TPEC_8:
5512 case TPACPI_THERMAL_ACPI_TMP07:
5513 case TPACPI_THERMAL_ACPI_UPDT:
5514 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5515 &thermal_temp_input16_group);
5516 break;
5517 case TPACPI_THERMAL_NONE:
5518 default:
5519 break;
5523 static int thermal_read(char *p)
5525 int len = 0;
5526 int n, i;
5527 struct ibm_thermal_sensors_struct t;
5529 n = thermal_get_sensors(&t);
5530 if (unlikely(n < 0))
5531 return n;
5533 len += sprintf(p + len, "temperatures:\t");
5535 if (n > 0) {
5536 for (i = 0; i < (n - 1); i++)
5537 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5538 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5539 } else
5540 len += sprintf(p + len, "not supported\n");
5542 return len;
5545 static struct ibm_struct thermal_driver_data = {
5546 .name = "thermal",
5547 .read = thermal_read,
5548 .exit = thermal_exit,
5551 /*************************************************************************
5552 * EC Dump subdriver
5555 static u8 ecdump_regs[256];
5557 static int ecdump_read(char *p)
5559 int len = 0;
5560 int i, j;
5561 u8 v;
5563 len += sprintf(p + len, "EC "
5564 " +00 +01 +02 +03 +04 +05 +06 +07"
5565 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5566 for (i = 0; i < 256; i += 16) {
5567 len += sprintf(p + len, "EC 0x%02x:", i);
5568 for (j = 0; j < 16; j++) {
5569 if (!acpi_ec_read(i + j, &v))
5570 break;
5571 if (v != ecdump_regs[i + j])
5572 len += sprintf(p + len, " *%02x", v);
5573 else
5574 len += sprintf(p + len, " %02x", v);
5575 ecdump_regs[i + j] = v;
5577 len += sprintf(p + len, "\n");
5578 if (j != 16)
5579 break;
5582 /* These are way too dangerous to advertise openly... */
5583 #if 0
5584 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5585 " (<offset> is 00-ff, <value> is 00-ff)\n");
5586 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5587 " (<offset> is 00-ff, <value> is 0-255)\n");
5588 #endif
5589 return len;
5592 static int ecdump_write(char *buf)
5594 char *cmd;
5595 int i, v;
5597 while ((cmd = next_cmd(&buf))) {
5598 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5599 /* i and v set */
5600 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5601 /* i and v set */
5602 } else
5603 return -EINVAL;
5604 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5605 if (!acpi_ec_write(i, v))
5606 return -EIO;
5607 } else
5608 return -EINVAL;
5611 return 0;
5614 static struct ibm_struct ecdump_driver_data = {
5615 .name = "ecdump",
5616 .read = ecdump_read,
5617 .write = ecdump_write,
5618 .flags.experimental = 1,
5621 /*************************************************************************
5622 * Backlight/brightness subdriver
5625 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5628 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5629 * CMOS NVRAM byte 0x5E, bits 0-3.
5631 * EC HBRV (0x31) has the following layout
5632 * Bit 7: unknown function
5633 * Bit 6: unknown function
5634 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5635 * Bit 4: must be set to zero to avoid problems
5636 * Bit 3-0: backlight brightness level
5638 * brightness_get_raw returns status data in the HBRV layout
5640 * WARNING: The X61 has been verified to use HBRV for something else, so
5641 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5642 * testing on the very early *60 Lenovo models...
5645 enum {
5646 TP_EC_BACKLIGHT = 0x31,
5648 /* TP_EC_BACKLIGHT bitmasks */
5649 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5650 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5651 TP_EC_BACKLIGHT_MAPSW = 0x20,
5654 enum tpacpi_brightness_access_mode {
5655 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5656 TPACPI_BRGHT_MODE_EC, /* EC control */
5657 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5658 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5659 TPACPI_BRGHT_MODE_MAX
5662 static struct backlight_device *ibm_backlight_device;
5664 static enum tpacpi_brightness_access_mode brightness_mode =
5665 TPACPI_BRGHT_MODE_MAX;
5667 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5669 static struct mutex brightness_mutex;
5671 /* NVRAM brightness access,
5672 * call with brightness_mutex held! */
5673 static unsigned int tpacpi_brightness_nvram_get(void)
5675 u8 lnvram;
5677 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5678 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5679 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5680 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5682 return lnvram;
5685 static void tpacpi_brightness_checkpoint_nvram(void)
5687 u8 lec = 0;
5688 u8 b_nvram;
5690 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5691 return;
5693 vdbg_printk(TPACPI_DBG_BRGHT,
5694 "trying to checkpoint backlight level to NVRAM...\n");
5696 if (mutex_lock_killable(&brightness_mutex) < 0)
5697 return;
5699 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5700 goto unlock;
5701 lec &= TP_EC_BACKLIGHT_LVLMSK;
5702 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5704 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5705 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5706 /* NVRAM needs update */
5707 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5708 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5709 b_nvram |= lec;
5710 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5711 dbg_printk(TPACPI_DBG_BRGHT,
5712 "updated NVRAM backlight level to %u (0x%02x)\n",
5713 (unsigned int) lec, (unsigned int) b_nvram);
5714 } else
5715 vdbg_printk(TPACPI_DBG_BRGHT,
5716 "NVRAM backlight level already is %u (0x%02x)\n",
5717 (unsigned int) lec, (unsigned int) b_nvram);
5719 unlock:
5720 mutex_unlock(&brightness_mutex);
5724 /* call with brightness_mutex held! */
5725 static int tpacpi_brightness_get_raw(int *status)
5727 u8 lec = 0;
5729 switch (brightness_mode) {
5730 case TPACPI_BRGHT_MODE_UCMS_STEP:
5731 *status = tpacpi_brightness_nvram_get();
5732 return 0;
5733 case TPACPI_BRGHT_MODE_EC:
5734 case TPACPI_BRGHT_MODE_ECNVRAM:
5735 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5736 return -EIO;
5737 *status = lec;
5738 return 0;
5739 default:
5740 return -ENXIO;
5744 /* call with brightness_mutex held! */
5745 /* do NOT call with illegal backlight level value */
5746 static int tpacpi_brightness_set_ec(unsigned int value)
5748 u8 lec = 0;
5750 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5751 return -EIO;
5753 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5754 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5755 (value & TP_EC_BACKLIGHT_LVLMSK))))
5756 return -EIO;
5758 return 0;
5761 /* call with brightness_mutex held! */
5762 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5764 int cmos_cmd, inc;
5765 unsigned int current_value, i;
5767 current_value = tpacpi_brightness_nvram_get();
5769 if (value == current_value)
5770 return 0;
5772 cmos_cmd = (value > current_value) ?
5773 TP_CMOS_BRIGHTNESS_UP :
5774 TP_CMOS_BRIGHTNESS_DOWN;
5775 inc = (value > current_value) ? 1 : -1;
5777 for (i = current_value; i != value; i += inc)
5778 if (issue_thinkpad_cmos_command(cmos_cmd))
5779 return -EIO;
5781 return 0;
5784 /* May return EINTR which can always be mapped to ERESTARTSYS */
5785 static int brightness_set(unsigned int value)
5787 int res;
5789 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5790 value < 0)
5791 return -EINVAL;
5793 vdbg_printk(TPACPI_DBG_BRGHT,
5794 "set backlight level to %d\n", value);
5796 res = mutex_lock_killable(&brightness_mutex);
5797 if (res < 0)
5798 return res;
5800 switch (brightness_mode) {
5801 case TPACPI_BRGHT_MODE_EC:
5802 case TPACPI_BRGHT_MODE_ECNVRAM:
5803 res = tpacpi_brightness_set_ec(value);
5804 break;
5805 case TPACPI_BRGHT_MODE_UCMS_STEP:
5806 res = tpacpi_brightness_set_ucmsstep(value);
5807 break;
5808 default:
5809 res = -ENXIO;
5812 mutex_unlock(&brightness_mutex);
5813 return res;
5816 /* sysfs backlight class ----------------------------------------------- */
5818 static int brightness_update_status(struct backlight_device *bd)
5820 unsigned int level =
5821 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5822 bd->props.power == FB_BLANK_UNBLANK) ?
5823 bd->props.brightness : 0;
5825 dbg_printk(TPACPI_DBG_BRGHT,
5826 "backlight: attempt to set level to %d\n",
5827 level);
5829 /* it is the backlight class's job (caller) to handle
5830 * EINTR and other errors properly */
5831 return brightness_set(level);
5834 static int brightness_get(struct backlight_device *bd)
5836 int status, res;
5838 res = mutex_lock_killable(&brightness_mutex);
5839 if (res < 0)
5840 return 0;
5842 res = tpacpi_brightness_get_raw(&status);
5844 mutex_unlock(&brightness_mutex);
5846 if (res < 0)
5847 return 0;
5849 return status & TP_EC_BACKLIGHT_LVLMSK;
5852 static struct backlight_ops ibm_backlight_data = {
5853 .get_brightness = brightness_get,
5854 .update_status = brightness_update_status,
5857 /* --------------------------------------------------------------------- */
5860 * These are only useful for models that have only one possibility
5861 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5862 * these quirks.
5864 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5865 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5866 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5868 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5869 /* Models with ATI GPUs known to require ECNVRAM mode */
5870 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5872 /* Models with ATI GPUs that can use ECNVRAM */
5873 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
5874 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5875 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5876 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5878 /* Models with Intel Extreme Graphics 2 */
5879 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
5880 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5881 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5883 /* Models with Intel GMA900 */
5884 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5885 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5886 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5889 static int __init brightness_init(struct ibm_init_struct *iibm)
5891 int b;
5892 unsigned long quirks;
5894 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5896 mutex_init(&brightness_mutex);
5898 quirks = tpacpi_check_quirks(brightness_quirk_table,
5899 ARRAY_SIZE(brightness_quirk_table));
5902 * We always attempt to detect acpi support, so as to switch
5903 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5904 * going to publish a backlight interface
5906 b = tpacpi_check_std_acpi_brightness_support();
5907 if (b > 0) {
5909 if (acpi_video_backlight_support()) {
5910 if (brightness_enable > 1) {
5911 printk(TPACPI_NOTICE
5912 "Standard ACPI backlight interface "
5913 "available, not loading native one.\n");
5914 return 1;
5915 } else if (brightness_enable == 1) {
5916 printk(TPACPI_NOTICE
5917 "Backlight control force enabled, even if standard "
5918 "ACPI backlight interface is available\n");
5920 } else {
5921 if (brightness_enable > 1) {
5922 printk(TPACPI_NOTICE
5923 "Standard ACPI backlight interface not "
5924 "available, thinkpad_acpi native "
5925 "brightness control enabled\n");
5930 if (!brightness_enable) {
5931 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5932 "brightness support disabled by "
5933 "module parameter\n");
5934 return 1;
5937 if (b > 16) {
5938 printk(TPACPI_ERR
5939 "Unsupported brightness interface, "
5940 "please contact %s\n", TPACPI_MAIL);
5941 return 1;
5943 if (b == 16)
5944 tp_features.bright_16levels = 1;
5947 * Check for module parameter bogosity, note that we
5948 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5949 * able to detect "unspecified"
5951 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5952 return -EINVAL;
5954 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5955 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5956 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5957 if (quirks & TPACPI_BRGHT_Q_EC)
5958 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5959 else
5960 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5962 dbg_printk(TPACPI_DBG_BRGHT,
5963 "driver auto-selected brightness_mode=%d\n",
5964 brightness_mode);
5967 /* Safety */
5968 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5969 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5970 brightness_mode == TPACPI_BRGHT_MODE_EC))
5971 return -EINVAL;
5973 if (tpacpi_brightness_get_raw(&b) < 0)
5974 return 1;
5976 if (tp_features.bright_16levels)
5977 printk(TPACPI_INFO
5978 "detected a 16-level brightness capable ThinkPad\n");
5980 ibm_backlight_device = backlight_device_register(
5981 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5982 &ibm_backlight_data);
5983 if (IS_ERR(ibm_backlight_device)) {
5984 printk(TPACPI_ERR "Could not register backlight device\n");
5985 return PTR_ERR(ibm_backlight_device);
5987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5988 "brightness is supported\n");
5990 if (quirks & TPACPI_BRGHT_Q_ASK) {
5991 printk(TPACPI_NOTICE
5992 "brightness: will use unverified default: "
5993 "brightness_mode=%d\n", brightness_mode);
5994 printk(TPACPI_NOTICE
5995 "brightness: please report to %s whether it works well "
5996 "or not on your ThinkPad\n", TPACPI_MAIL);
5999 ibm_backlight_device->props.max_brightness =
6000 (tp_features.bright_16levels)? 15 : 7;
6001 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6002 backlight_update_status(ibm_backlight_device);
6004 return 0;
6007 static void brightness_suspend(pm_message_t state)
6009 tpacpi_brightness_checkpoint_nvram();
6012 static void brightness_shutdown(void)
6014 tpacpi_brightness_checkpoint_nvram();
6017 static void brightness_exit(void)
6019 if (ibm_backlight_device) {
6020 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6021 "calling backlight_device_unregister()\n");
6022 backlight_device_unregister(ibm_backlight_device);
6025 tpacpi_brightness_checkpoint_nvram();
6028 static int brightness_read(char *p)
6030 int len = 0;
6031 int level;
6033 level = brightness_get(NULL);
6034 if (level < 0) {
6035 len += sprintf(p + len, "level:\t\tunreadable\n");
6036 } else {
6037 len += sprintf(p + len, "level:\t\t%d\n", level);
6038 len += sprintf(p + len, "commands:\tup, down\n");
6039 len += sprintf(p + len, "commands:\tlevel <level>"
6040 " (<level> is 0-%d)\n",
6041 (tp_features.bright_16levels) ? 15 : 7);
6044 return len;
6047 static int brightness_write(char *buf)
6049 int level;
6050 int rc;
6051 char *cmd;
6052 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6054 level = brightness_get(NULL);
6055 if (level < 0)
6056 return level;
6058 while ((cmd = next_cmd(&buf))) {
6059 if (strlencmp(cmd, "up") == 0) {
6060 if (level < max_level)
6061 level++;
6062 } else if (strlencmp(cmd, "down") == 0) {
6063 if (level > 0)
6064 level--;
6065 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6066 level >= 0 && level <= max_level) {
6067 /* new level set */
6068 } else
6069 return -EINVAL;
6072 tpacpi_disclose_usertask("procfs brightness",
6073 "set level to %d\n", level);
6076 * Now we know what the final level should be, so we try to set it.
6077 * Doing it this way makes the syscall restartable in case of EINTR
6079 rc = brightness_set(level);
6080 return (rc == -EINTR)? ERESTARTSYS : rc;
6083 static struct ibm_struct brightness_driver_data = {
6084 .name = "brightness",
6085 .read = brightness_read,
6086 .write = brightness_write,
6087 .exit = brightness_exit,
6088 .suspend = brightness_suspend,
6089 .shutdown = brightness_shutdown,
6092 /*************************************************************************
6093 * Volume subdriver
6096 static int volume_offset = 0x30;
6098 static int volume_read(char *p)
6100 int len = 0;
6101 u8 level;
6103 if (!acpi_ec_read(volume_offset, &level)) {
6104 len += sprintf(p + len, "level:\t\tunreadable\n");
6105 } else {
6106 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6107 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6108 len += sprintf(p + len, "commands:\tup, down, mute\n");
6109 len += sprintf(p + len, "commands:\tlevel <level>"
6110 " (<level> is 0-15)\n");
6113 return len;
6116 static int volume_write(char *buf)
6118 int cmos_cmd, inc, i;
6119 u8 level, mute;
6120 int new_level, new_mute;
6121 char *cmd;
6123 while ((cmd = next_cmd(&buf))) {
6124 if (!acpi_ec_read(volume_offset, &level))
6125 return -EIO;
6126 new_mute = mute = level & 0x40;
6127 new_level = level = level & 0xf;
6129 if (strlencmp(cmd, "up") == 0) {
6130 if (mute)
6131 new_mute = 0;
6132 else
6133 new_level = level == 15 ? 15 : level + 1;
6134 } else if (strlencmp(cmd, "down") == 0) {
6135 if (mute)
6136 new_mute = 0;
6137 else
6138 new_level = level == 0 ? 0 : level - 1;
6139 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6140 new_level >= 0 && new_level <= 15) {
6141 /* new_level set */
6142 } else if (strlencmp(cmd, "mute") == 0) {
6143 new_mute = 0x40;
6144 } else
6145 return -EINVAL;
6147 if (new_level != level) {
6148 /* mute doesn't change */
6150 cmos_cmd = (new_level > level) ?
6151 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6152 inc = new_level > level ? 1 : -1;
6154 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6155 !acpi_ec_write(volume_offset, level)))
6156 return -EIO;
6158 for (i = level; i != new_level; i += inc)
6159 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6160 !acpi_ec_write(volume_offset, i + inc))
6161 return -EIO;
6163 if (mute &&
6164 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6165 !acpi_ec_write(volume_offset, new_level + mute))) {
6166 return -EIO;
6170 if (new_mute != mute) {
6171 /* level doesn't change */
6173 cmos_cmd = (new_mute) ?
6174 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6176 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6177 !acpi_ec_write(volume_offset, level + new_mute))
6178 return -EIO;
6182 return 0;
6185 static struct ibm_struct volume_driver_data = {
6186 .name = "volume",
6187 .read = volume_read,
6188 .write = volume_write,
6191 /*************************************************************************
6192 * Fan subdriver
6196 * FAN ACCESS MODES
6198 * TPACPI_FAN_RD_ACPI_GFAN:
6199 * ACPI GFAN method: returns fan level
6201 * see TPACPI_FAN_WR_ACPI_SFAN
6202 * EC 0x2f (HFSP) not available if GFAN exists
6204 * TPACPI_FAN_WR_ACPI_SFAN:
6205 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6207 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6208 * it for writing.
6210 * TPACPI_FAN_WR_TPEC:
6211 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6212 * Supported on almost all ThinkPads
6214 * Fan speed changes of any sort (including those caused by the
6215 * disengaged mode) are usually done slowly by the firmware as the
6216 * maximum ammount of fan duty cycle change per second seems to be
6217 * limited.
6219 * Reading is not available if GFAN exists.
6220 * Writing is not available if SFAN exists.
6222 * Bits
6223 * 7 automatic mode engaged;
6224 * (default operation mode of the ThinkPad)
6225 * fan level is ignored in this mode.
6226 * 6 full speed mode (takes precedence over bit 7);
6227 * not available on all thinkpads. May disable
6228 * the tachometer while the fan controller ramps up
6229 * the speed (which can take up to a few *minutes*).
6230 * Speeds up fan to 100% duty-cycle, which is far above
6231 * the standard RPM levels. It is not impossible that
6232 * it could cause hardware damage.
6233 * 5-3 unused in some models. Extra bits for fan level
6234 * in others, but still useless as all values above
6235 * 7 map to the same speed as level 7 in these models.
6236 * 2-0 fan level (0..7 usually)
6237 * 0x00 = stop
6238 * 0x07 = max (set when temperatures critical)
6239 * Some ThinkPads may have other levels, see
6240 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6242 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6243 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6244 * does so, its initial value is meaningless (0x07).
6246 * For firmware bugs, refer to:
6247 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6249 * ----
6251 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6252 * Main fan tachometer reading (in RPM)
6254 * This register is present on all ThinkPads with a new-style EC, and
6255 * it is known not to be present on the A21m/e, and T22, as there is
6256 * something else in offset 0x84 according to the ACPI DSDT. Other
6257 * ThinkPads from this same time period (and earlier) probably lack the
6258 * tachometer as well.
6260 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6261 * was never fixed by IBM to report the EC firmware version string
6262 * probably support the tachometer (like the early X models), so
6263 * detecting it is quite hard. We need more data to know for sure.
6265 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6266 * might result.
6268 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6269 * mode.
6271 * For firmware bugs, refer to:
6272 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6274 * ----
6276 * ThinkPad EC register 0x31 bit 0 (only on select models)
6278 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6279 * show the speed of the main fan. When bit 0 of EC register 0x31
6280 * is one, the tachometer registers show the speed of the auxiliary
6281 * fan.
6283 * Fan control seems to affect both fans, regardless of the state
6284 * of this bit.
6286 * So far, only the firmware for the X60/X61 non-tablet versions
6287 * seem to support this (firmware TP-7M).
6289 * TPACPI_FAN_WR_ACPI_FANS:
6290 * ThinkPad X31, X40, X41. Not available in the X60.
6292 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6293 * high speed. ACPI DSDT seems to map these three speeds to levels
6294 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6295 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6297 * The speeds are stored on handles
6298 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6300 * There are three default speed sets, acessible as handles:
6301 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6303 * ACPI DSDT switches which set is in use depending on various
6304 * factors.
6306 * TPACPI_FAN_WR_TPEC is also available and should be used to
6307 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6308 * but the ACPI tables just mention level 7.
6311 enum { /* Fan control constants */
6312 fan_status_offset = 0x2f, /* EC register 0x2f */
6313 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6314 * 0x84 must be read before 0x85 */
6315 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6316 bit 0 selects which fan is active */
6318 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6319 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6321 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6324 enum fan_status_access_mode {
6325 TPACPI_FAN_NONE = 0, /* No fan status or control */
6326 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6327 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6330 enum fan_control_access_mode {
6331 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6332 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6333 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6334 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6337 enum fan_control_commands {
6338 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6339 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6340 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6341 * and also watchdog cmd */
6344 static int fan_control_allowed;
6346 static enum fan_status_access_mode fan_status_access_mode;
6347 static enum fan_control_access_mode fan_control_access_mode;
6348 static enum fan_control_commands fan_control_commands;
6350 static u8 fan_control_initial_status;
6351 static u8 fan_control_desired_level;
6352 static u8 fan_control_resume_level;
6353 static int fan_watchdog_maxinterval;
6355 static struct mutex fan_mutex;
6357 static void fan_watchdog_fire(struct work_struct *ignored);
6358 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6360 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6361 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6362 "\\FSPD", /* 600e/x, 770e, 770x */
6363 ); /* all others */
6364 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6365 "JFNS", /* 770x-JL */
6366 ); /* all others */
6369 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6370 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6371 * be in auto mode (0x80).
6373 * This is corrected by any write to HFSP either by the driver, or
6374 * by the firmware.
6376 * We assume 0x07 really means auto mode while this quirk is active,
6377 * as this is far more likely than the ThinkPad being in level 7,
6378 * which is only used by the firmware during thermal emergencies.
6380 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6381 * TP-70 (T43, R52), which are known to be buggy.
6384 static void fan_quirk1_setup(void)
6386 if (fan_control_initial_status == 0x07) {
6387 printk(TPACPI_NOTICE
6388 "fan_init: initial fan status is unknown, "
6389 "assuming it is in auto mode\n");
6390 tp_features.fan_ctrl_status_undef = 1;
6394 static void fan_quirk1_handle(u8 *fan_status)
6396 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6397 if (*fan_status != fan_control_initial_status) {
6398 /* something changed the HFSP regisnter since
6399 * driver init time, so it is not undefined
6400 * anymore */
6401 tp_features.fan_ctrl_status_undef = 0;
6402 } else {
6403 /* Return most likely status. In fact, it
6404 * might be the only possible status */
6405 *fan_status = TP_EC_FAN_AUTO;
6410 /* Select main fan on X60/X61, NOOP on others */
6411 static bool fan_select_fan1(void)
6413 if (tp_features.second_fan) {
6414 u8 val;
6416 if (ec_read(fan_select_offset, &val) < 0)
6417 return false;
6418 val &= 0xFEU;
6419 if (ec_write(fan_select_offset, val) < 0)
6420 return false;
6422 return true;
6425 /* Select secondary fan on X60/X61 */
6426 static bool fan_select_fan2(void)
6428 u8 val;
6430 if (!tp_features.second_fan)
6431 return false;
6433 if (ec_read(fan_select_offset, &val) < 0)
6434 return false;
6435 val |= 0x01U;
6436 if (ec_write(fan_select_offset, val) < 0)
6437 return false;
6439 return true;
6443 * Call with fan_mutex held
6445 static void fan_update_desired_level(u8 status)
6447 if ((status &
6448 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6449 if (status > 7)
6450 fan_control_desired_level = 7;
6451 else
6452 fan_control_desired_level = status;
6456 static int fan_get_status(u8 *status)
6458 u8 s;
6460 /* TODO:
6461 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6463 switch (fan_status_access_mode) {
6464 case TPACPI_FAN_RD_ACPI_GFAN:
6465 /* 570, 600e/x, 770e, 770x */
6467 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6468 return -EIO;
6470 if (likely(status))
6471 *status = s & 0x07;
6473 break;
6475 case TPACPI_FAN_RD_TPEC:
6476 /* all except 570, 600e/x, 770e, 770x */
6477 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6478 return -EIO;
6480 if (likely(status)) {
6481 *status = s;
6482 fan_quirk1_handle(status);
6485 break;
6487 default:
6488 return -ENXIO;
6491 return 0;
6494 static int fan_get_status_safe(u8 *status)
6496 int rc;
6497 u8 s;
6499 if (mutex_lock_killable(&fan_mutex))
6500 return -ERESTARTSYS;
6501 rc = fan_get_status(&s);
6502 if (!rc)
6503 fan_update_desired_level(s);
6504 mutex_unlock(&fan_mutex);
6506 if (status)
6507 *status = s;
6509 return rc;
6512 static int fan_get_speed(unsigned int *speed)
6514 u8 hi, lo;
6516 switch (fan_status_access_mode) {
6517 case TPACPI_FAN_RD_TPEC:
6518 /* all except 570, 600e/x, 770e, 770x */
6519 if (unlikely(!fan_select_fan1()))
6520 return -EIO;
6521 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6522 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6523 return -EIO;
6525 if (likely(speed))
6526 *speed = (hi << 8) | lo;
6528 break;
6530 default:
6531 return -ENXIO;
6534 return 0;
6537 static int fan2_get_speed(unsigned int *speed)
6539 u8 hi, lo;
6540 bool rc;
6542 switch (fan_status_access_mode) {
6543 case TPACPI_FAN_RD_TPEC:
6544 /* all except 570, 600e/x, 770e, 770x */
6545 if (unlikely(!fan_select_fan2()))
6546 return -EIO;
6547 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6548 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6549 fan_select_fan1(); /* play it safe */
6550 if (rc)
6551 return -EIO;
6553 if (likely(speed))
6554 *speed = (hi << 8) | lo;
6556 break;
6558 default:
6559 return -ENXIO;
6562 return 0;
6565 static int fan_set_level(int level)
6567 if (!fan_control_allowed)
6568 return -EPERM;
6570 switch (fan_control_access_mode) {
6571 case TPACPI_FAN_WR_ACPI_SFAN:
6572 if (level >= 0 && level <= 7) {
6573 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6574 return -EIO;
6575 } else
6576 return -EINVAL;
6577 break;
6579 case TPACPI_FAN_WR_ACPI_FANS:
6580 case TPACPI_FAN_WR_TPEC:
6581 if (!(level & TP_EC_FAN_AUTO) &&
6582 !(level & TP_EC_FAN_FULLSPEED) &&
6583 ((level < 0) || (level > 7)))
6584 return -EINVAL;
6586 /* safety net should the EC not support AUTO
6587 * or FULLSPEED mode bits and just ignore them */
6588 if (level & TP_EC_FAN_FULLSPEED)
6589 level |= 7; /* safety min speed 7 */
6590 else if (level & TP_EC_FAN_AUTO)
6591 level |= 4; /* safety min speed 4 */
6593 if (!acpi_ec_write(fan_status_offset, level))
6594 return -EIO;
6595 else
6596 tp_features.fan_ctrl_status_undef = 0;
6597 break;
6599 default:
6600 return -ENXIO;
6603 vdbg_printk(TPACPI_DBG_FAN,
6604 "fan control: set fan control register to 0x%02x\n", level);
6605 return 0;
6608 static int fan_set_level_safe(int level)
6610 int rc;
6612 if (!fan_control_allowed)
6613 return -EPERM;
6615 if (mutex_lock_killable(&fan_mutex))
6616 return -ERESTARTSYS;
6618 if (level == TPACPI_FAN_LAST_LEVEL)
6619 level = fan_control_desired_level;
6621 rc = fan_set_level(level);
6622 if (!rc)
6623 fan_update_desired_level(level);
6625 mutex_unlock(&fan_mutex);
6626 return rc;
6629 static int fan_set_enable(void)
6631 u8 s;
6632 int rc;
6634 if (!fan_control_allowed)
6635 return -EPERM;
6637 if (mutex_lock_killable(&fan_mutex))
6638 return -ERESTARTSYS;
6640 switch (fan_control_access_mode) {
6641 case TPACPI_FAN_WR_ACPI_FANS:
6642 case TPACPI_FAN_WR_TPEC:
6643 rc = fan_get_status(&s);
6644 if (rc < 0)
6645 break;
6647 /* Don't go out of emergency fan mode */
6648 if (s != 7) {
6649 s &= 0x07;
6650 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6653 if (!acpi_ec_write(fan_status_offset, s))
6654 rc = -EIO;
6655 else {
6656 tp_features.fan_ctrl_status_undef = 0;
6657 rc = 0;
6659 break;
6661 case TPACPI_FAN_WR_ACPI_SFAN:
6662 rc = fan_get_status(&s);
6663 if (rc < 0)
6664 break;
6666 s &= 0x07;
6668 /* Set fan to at least level 4 */
6669 s |= 4;
6671 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6672 rc = -EIO;
6673 else
6674 rc = 0;
6675 break;
6677 default:
6678 rc = -ENXIO;
6681 mutex_unlock(&fan_mutex);
6683 if (!rc)
6684 vdbg_printk(TPACPI_DBG_FAN,
6685 "fan control: set fan control register to 0x%02x\n",
6687 return rc;
6690 static int fan_set_disable(void)
6692 int rc;
6694 if (!fan_control_allowed)
6695 return -EPERM;
6697 if (mutex_lock_killable(&fan_mutex))
6698 return -ERESTARTSYS;
6700 rc = 0;
6701 switch (fan_control_access_mode) {
6702 case TPACPI_FAN_WR_ACPI_FANS:
6703 case TPACPI_FAN_WR_TPEC:
6704 if (!acpi_ec_write(fan_status_offset, 0x00))
6705 rc = -EIO;
6706 else {
6707 fan_control_desired_level = 0;
6708 tp_features.fan_ctrl_status_undef = 0;
6710 break;
6712 case TPACPI_FAN_WR_ACPI_SFAN:
6713 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6714 rc = -EIO;
6715 else
6716 fan_control_desired_level = 0;
6717 break;
6719 default:
6720 rc = -ENXIO;
6723 if (!rc)
6724 vdbg_printk(TPACPI_DBG_FAN,
6725 "fan control: set fan control register to 0\n");
6727 mutex_unlock(&fan_mutex);
6728 return rc;
6731 static int fan_set_speed(int speed)
6733 int rc;
6735 if (!fan_control_allowed)
6736 return -EPERM;
6738 if (mutex_lock_killable(&fan_mutex))
6739 return -ERESTARTSYS;
6741 rc = 0;
6742 switch (fan_control_access_mode) {
6743 case TPACPI_FAN_WR_ACPI_FANS:
6744 if (speed >= 0 && speed <= 65535) {
6745 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6746 speed, speed, speed))
6747 rc = -EIO;
6748 } else
6749 rc = -EINVAL;
6750 break;
6752 default:
6753 rc = -ENXIO;
6756 mutex_unlock(&fan_mutex);
6757 return rc;
6760 static void fan_watchdog_reset(void)
6762 static int fan_watchdog_active;
6764 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6765 return;
6767 if (fan_watchdog_active)
6768 cancel_delayed_work(&fan_watchdog_task);
6770 if (fan_watchdog_maxinterval > 0 &&
6771 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6772 fan_watchdog_active = 1;
6773 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6774 msecs_to_jiffies(fan_watchdog_maxinterval
6775 * 1000))) {
6776 printk(TPACPI_ERR
6777 "failed to queue the fan watchdog, "
6778 "watchdog will not trigger\n");
6780 } else
6781 fan_watchdog_active = 0;
6784 static void fan_watchdog_fire(struct work_struct *ignored)
6786 int rc;
6788 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6789 return;
6791 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6792 rc = fan_set_enable();
6793 if (rc < 0) {
6794 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6795 "will try again later...\n", -rc);
6796 /* reschedule for later */
6797 fan_watchdog_reset();
6802 * SYSFS fan layout: hwmon compatible (device)
6804 * pwm*_enable:
6805 * 0: "disengaged" mode
6806 * 1: manual mode
6807 * 2: native EC "auto" mode (recommended, hardware default)
6809 * pwm*: set speed in manual mode, ignored otherwise.
6810 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6811 * interpolation.
6813 * fan*_input: tachometer reading, RPM
6816 * SYSFS fan layout: extensions
6818 * fan_watchdog (driver):
6819 * fan watchdog interval in seconds, 0 disables (default), max 120
6822 /* sysfs fan pwm1_enable ----------------------------------------------- */
6823 static ssize_t fan_pwm1_enable_show(struct device *dev,
6824 struct device_attribute *attr,
6825 char *buf)
6827 int res, mode;
6828 u8 status;
6830 res = fan_get_status_safe(&status);
6831 if (res)
6832 return res;
6834 if (status & TP_EC_FAN_FULLSPEED) {
6835 mode = 0;
6836 } else if (status & TP_EC_FAN_AUTO) {
6837 mode = 2;
6838 } else
6839 mode = 1;
6841 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6844 static ssize_t fan_pwm1_enable_store(struct device *dev,
6845 struct device_attribute *attr,
6846 const char *buf, size_t count)
6848 unsigned long t;
6849 int res, level;
6851 if (parse_strtoul(buf, 2, &t))
6852 return -EINVAL;
6854 tpacpi_disclose_usertask("hwmon pwm1_enable",
6855 "set fan mode to %lu\n", t);
6857 switch (t) {
6858 case 0:
6859 level = TP_EC_FAN_FULLSPEED;
6860 break;
6861 case 1:
6862 level = TPACPI_FAN_LAST_LEVEL;
6863 break;
6864 case 2:
6865 level = TP_EC_FAN_AUTO;
6866 break;
6867 case 3:
6868 /* reserved for software-controlled auto mode */
6869 return -ENOSYS;
6870 default:
6871 return -EINVAL;
6874 res = fan_set_level_safe(level);
6875 if (res == -ENXIO)
6876 return -EINVAL;
6877 else if (res < 0)
6878 return res;
6880 fan_watchdog_reset();
6882 return count;
6885 static struct device_attribute dev_attr_fan_pwm1_enable =
6886 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6887 fan_pwm1_enable_show, fan_pwm1_enable_store);
6889 /* sysfs fan pwm1 ------------------------------------------------------ */
6890 static ssize_t fan_pwm1_show(struct device *dev,
6891 struct device_attribute *attr,
6892 char *buf)
6894 int res;
6895 u8 status;
6897 res = fan_get_status_safe(&status);
6898 if (res)
6899 return res;
6901 if ((status &
6902 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6903 status = fan_control_desired_level;
6905 if (status > 7)
6906 status = 7;
6908 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6911 static ssize_t fan_pwm1_store(struct device *dev,
6912 struct device_attribute *attr,
6913 const char *buf, size_t count)
6915 unsigned long s;
6916 int rc;
6917 u8 status, newlevel;
6919 if (parse_strtoul(buf, 255, &s))
6920 return -EINVAL;
6922 tpacpi_disclose_usertask("hwmon pwm1",
6923 "set fan speed to %lu\n", s);
6925 /* scale down from 0-255 to 0-7 */
6926 newlevel = (s >> 5) & 0x07;
6928 if (mutex_lock_killable(&fan_mutex))
6929 return -ERESTARTSYS;
6931 rc = fan_get_status(&status);
6932 if (!rc && (status &
6933 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6934 rc = fan_set_level(newlevel);
6935 if (rc == -ENXIO)
6936 rc = -EINVAL;
6937 else if (!rc) {
6938 fan_update_desired_level(newlevel);
6939 fan_watchdog_reset();
6943 mutex_unlock(&fan_mutex);
6944 return (rc)? rc : count;
6947 static struct device_attribute dev_attr_fan_pwm1 =
6948 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6949 fan_pwm1_show, fan_pwm1_store);
6951 /* sysfs fan fan1_input ------------------------------------------------ */
6952 static ssize_t fan_fan1_input_show(struct device *dev,
6953 struct device_attribute *attr,
6954 char *buf)
6956 int res;
6957 unsigned int speed;
6959 res = fan_get_speed(&speed);
6960 if (res < 0)
6961 return res;
6963 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6966 static struct device_attribute dev_attr_fan_fan1_input =
6967 __ATTR(fan1_input, S_IRUGO,
6968 fan_fan1_input_show, NULL);
6970 /* sysfs fan fan2_input ------------------------------------------------ */
6971 static ssize_t fan_fan2_input_show(struct device *dev,
6972 struct device_attribute *attr,
6973 char *buf)
6975 int res;
6976 unsigned int speed;
6978 res = fan2_get_speed(&speed);
6979 if (res < 0)
6980 return res;
6982 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6985 static struct device_attribute dev_attr_fan_fan2_input =
6986 __ATTR(fan2_input, S_IRUGO,
6987 fan_fan2_input_show, NULL);
6989 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6990 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6991 char *buf)
6993 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6996 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6997 const char *buf, size_t count)
6999 unsigned long t;
7001 if (parse_strtoul(buf, 120, &t))
7002 return -EINVAL;
7004 if (!fan_control_allowed)
7005 return -EPERM;
7007 fan_watchdog_maxinterval = t;
7008 fan_watchdog_reset();
7010 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7012 return count;
7015 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7016 fan_fan_watchdog_show, fan_fan_watchdog_store);
7018 /* --------------------------------------------------------------------- */
7019 static struct attribute *fan_attributes[] = {
7020 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7021 &dev_attr_fan_fan1_input.attr,
7022 NULL, /* for fan2_input */
7023 NULL
7026 static const struct attribute_group fan_attr_group = {
7027 .attrs = fan_attributes,
7030 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7031 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7033 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7034 { .vendor = PCI_VENDOR_ID_IBM, \
7035 .bios = TPACPI_MATCH_ANY, \
7036 .ec = TPID(__id1, __id2), \
7037 .quirks = __quirks }
7039 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7040 { .vendor = PCI_VENDOR_ID_LENOVO, \
7041 .bios = TPACPI_MATCH_ANY, \
7042 .ec = TPID(__id1, __id2), \
7043 .quirks = __quirks }
7045 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7046 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7047 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7048 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7049 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7050 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7053 #undef TPACPI_FAN_QL
7054 #undef TPACPI_FAN_QI
7056 static int __init fan_init(struct ibm_init_struct *iibm)
7058 int rc;
7059 unsigned long quirks;
7061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7062 "initializing fan subdriver\n");
7064 mutex_init(&fan_mutex);
7065 fan_status_access_mode = TPACPI_FAN_NONE;
7066 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7067 fan_control_commands = 0;
7068 fan_watchdog_maxinterval = 0;
7069 tp_features.fan_ctrl_status_undef = 0;
7070 tp_features.second_fan = 0;
7071 fan_control_desired_level = 7;
7073 TPACPI_ACPIHANDLE_INIT(fans);
7074 TPACPI_ACPIHANDLE_INIT(gfan);
7075 TPACPI_ACPIHANDLE_INIT(sfan);
7077 quirks = tpacpi_check_quirks(fan_quirk_table,
7078 ARRAY_SIZE(fan_quirk_table));
7080 if (gfan_handle) {
7081 /* 570, 600e/x, 770e, 770x */
7082 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7083 } else {
7084 /* all other ThinkPads: note that even old-style
7085 * ThinkPad ECs supports the fan control register */
7086 if (likely(acpi_ec_read(fan_status_offset,
7087 &fan_control_initial_status))) {
7088 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7089 if (quirks & TPACPI_FAN_Q1)
7090 fan_quirk1_setup();
7091 if (quirks & TPACPI_FAN_2FAN) {
7092 tp_features.second_fan = 1;
7093 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7094 "secondary fan support enabled\n");
7096 } else {
7097 printk(TPACPI_ERR
7098 "ThinkPad ACPI EC access misbehaving, "
7099 "fan status and control unavailable\n");
7100 return 1;
7104 if (sfan_handle) {
7105 /* 570, 770x-JL */
7106 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7107 fan_control_commands |=
7108 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7109 } else {
7110 if (!gfan_handle) {
7111 /* gfan without sfan means no fan control */
7112 /* all other models implement TP EC 0x2f control */
7114 if (fans_handle) {
7115 /* X31, X40, X41 */
7116 fan_control_access_mode =
7117 TPACPI_FAN_WR_ACPI_FANS;
7118 fan_control_commands |=
7119 TPACPI_FAN_CMD_SPEED |
7120 TPACPI_FAN_CMD_LEVEL |
7121 TPACPI_FAN_CMD_ENABLE;
7122 } else {
7123 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7124 fan_control_commands |=
7125 TPACPI_FAN_CMD_LEVEL |
7126 TPACPI_FAN_CMD_ENABLE;
7131 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7132 "fan is %s, modes %d, %d\n",
7133 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7134 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7135 fan_status_access_mode, fan_control_access_mode);
7137 /* fan control master switch */
7138 if (!fan_control_allowed) {
7139 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7140 fan_control_commands = 0;
7141 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7142 "fan control features disabled by parameter\n");
7145 /* update fan_control_desired_level */
7146 if (fan_status_access_mode != TPACPI_FAN_NONE)
7147 fan_get_status_safe(NULL);
7149 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7150 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7151 if (tp_features.second_fan) {
7152 /* attach second fan tachometer */
7153 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7154 &dev_attr_fan_fan2_input.attr;
7156 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7157 &fan_attr_group);
7158 if (rc < 0)
7159 return rc;
7161 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7162 &driver_attr_fan_watchdog);
7163 if (rc < 0) {
7164 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7165 &fan_attr_group);
7166 return rc;
7168 return 0;
7169 } else
7170 return 1;
7173 static void fan_exit(void)
7175 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7176 "cancelling any pending fan watchdog tasks\n");
7178 /* FIXME: can we really do this unconditionally? */
7179 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7180 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7181 &driver_attr_fan_watchdog);
7183 cancel_delayed_work(&fan_watchdog_task);
7184 flush_workqueue(tpacpi_wq);
7187 static void fan_suspend(pm_message_t state)
7189 int rc;
7191 if (!fan_control_allowed)
7192 return;
7194 /* Store fan status in cache */
7195 fan_control_resume_level = 0;
7196 rc = fan_get_status_safe(&fan_control_resume_level);
7197 if (rc < 0)
7198 printk(TPACPI_NOTICE
7199 "failed to read fan level for later "
7200 "restore during resume: %d\n", rc);
7202 /* if it is undefined, don't attempt to restore it.
7203 * KEEP THIS LAST */
7204 if (tp_features.fan_ctrl_status_undef)
7205 fan_control_resume_level = 0;
7208 static void fan_resume(void)
7210 u8 current_level = 7;
7211 bool do_set = false;
7212 int rc;
7214 /* DSDT *always* updates status on resume */
7215 tp_features.fan_ctrl_status_undef = 0;
7217 if (!fan_control_allowed ||
7218 !fan_control_resume_level ||
7219 (fan_get_status_safe(&current_level) < 0))
7220 return;
7222 switch (fan_control_access_mode) {
7223 case TPACPI_FAN_WR_ACPI_SFAN:
7224 /* never decrease fan level */
7225 do_set = (fan_control_resume_level > current_level);
7226 break;
7227 case TPACPI_FAN_WR_ACPI_FANS:
7228 case TPACPI_FAN_WR_TPEC:
7229 /* never decrease fan level, scale is:
7230 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7232 * We expect the firmware to set either 7 or AUTO, but we
7233 * handle FULLSPEED out of paranoia.
7235 * So, we can safely only restore FULLSPEED or 7, anything
7236 * else could slow the fan. Restoring AUTO is useless, at
7237 * best that's exactly what the DSDT already set (it is the
7238 * slower it uses).
7240 * Always keep in mind that the DSDT *will* have set the
7241 * fans to what the vendor supposes is the best level. We
7242 * muck with it only to speed the fan up.
7244 if (fan_control_resume_level != 7 &&
7245 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7246 return;
7247 else
7248 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7249 (current_level != fan_control_resume_level);
7250 break;
7251 default:
7252 return;
7254 if (do_set) {
7255 printk(TPACPI_NOTICE
7256 "restoring fan level to 0x%02x\n",
7257 fan_control_resume_level);
7258 rc = fan_set_level_safe(fan_control_resume_level);
7259 if (rc < 0)
7260 printk(TPACPI_NOTICE
7261 "failed to restore fan level: %d\n", rc);
7265 static int fan_read(char *p)
7267 int len = 0;
7268 int rc;
7269 u8 status;
7270 unsigned int speed = 0;
7272 switch (fan_status_access_mode) {
7273 case TPACPI_FAN_RD_ACPI_GFAN:
7274 /* 570, 600e/x, 770e, 770x */
7275 rc = fan_get_status_safe(&status);
7276 if (rc < 0)
7277 return rc;
7279 len += sprintf(p + len, "status:\t\t%s\n"
7280 "level:\t\t%d\n",
7281 (status != 0) ? "enabled" : "disabled", status);
7282 break;
7284 case TPACPI_FAN_RD_TPEC:
7285 /* all except 570, 600e/x, 770e, 770x */
7286 rc = fan_get_status_safe(&status);
7287 if (rc < 0)
7288 return rc;
7290 len += sprintf(p + len, "status:\t\t%s\n",
7291 (status != 0) ? "enabled" : "disabled");
7293 rc = fan_get_speed(&speed);
7294 if (rc < 0)
7295 return rc;
7297 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7299 if (status & TP_EC_FAN_FULLSPEED)
7300 /* Disengaged mode takes precedence */
7301 len += sprintf(p + len, "level:\t\tdisengaged\n");
7302 else if (status & TP_EC_FAN_AUTO)
7303 len += sprintf(p + len, "level:\t\tauto\n");
7304 else
7305 len += sprintf(p + len, "level:\t\t%d\n", status);
7306 break;
7308 case TPACPI_FAN_NONE:
7309 default:
7310 len += sprintf(p + len, "status:\t\tnot supported\n");
7313 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7314 len += sprintf(p + len, "commands:\tlevel <level>");
7316 switch (fan_control_access_mode) {
7317 case TPACPI_FAN_WR_ACPI_SFAN:
7318 len += sprintf(p + len, " (<level> is 0-7)\n");
7319 break;
7321 default:
7322 len += sprintf(p + len, " (<level> is 0-7, "
7323 "auto, disengaged, full-speed)\n");
7324 break;
7328 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7329 len += sprintf(p + len, "commands:\tenable, disable\n"
7330 "commands:\twatchdog <timeout> (<timeout> "
7331 "is 0 (off), 1-120 (seconds))\n");
7333 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7334 len += sprintf(p + len, "commands:\tspeed <speed>"
7335 " (<speed> is 0-65535)\n");
7337 return len;
7340 static int fan_write_cmd_level(const char *cmd, int *rc)
7342 int level;
7344 if (strlencmp(cmd, "level auto") == 0)
7345 level = TP_EC_FAN_AUTO;
7346 else if ((strlencmp(cmd, "level disengaged") == 0) |
7347 (strlencmp(cmd, "level full-speed") == 0))
7348 level = TP_EC_FAN_FULLSPEED;
7349 else if (sscanf(cmd, "level %d", &level) != 1)
7350 return 0;
7352 *rc = fan_set_level_safe(level);
7353 if (*rc == -ENXIO)
7354 printk(TPACPI_ERR "level command accepted for unsupported "
7355 "access mode %d", fan_control_access_mode);
7356 else if (!*rc)
7357 tpacpi_disclose_usertask("procfs fan",
7358 "set level to %d\n", level);
7360 return 1;
7363 static int fan_write_cmd_enable(const char *cmd, int *rc)
7365 if (strlencmp(cmd, "enable") != 0)
7366 return 0;
7368 *rc = fan_set_enable();
7369 if (*rc == -ENXIO)
7370 printk(TPACPI_ERR "enable command accepted for unsupported "
7371 "access mode %d", fan_control_access_mode);
7372 else if (!*rc)
7373 tpacpi_disclose_usertask("procfs fan", "enable\n");
7375 return 1;
7378 static int fan_write_cmd_disable(const char *cmd, int *rc)
7380 if (strlencmp(cmd, "disable") != 0)
7381 return 0;
7383 *rc = fan_set_disable();
7384 if (*rc == -ENXIO)
7385 printk(TPACPI_ERR "disable command accepted for unsupported "
7386 "access mode %d", fan_control_access_mode);
7387 else if (!*rc)
7388 tpacpi_disclose_usertask("procfs fan", "disable\n");
7390 return 1;
7393 static int fan_write_cmd_speed(const char *cmd, int *rc)
7395 int speed;
7397 /* TODO:
7398 * Support speed <low> <medium> <high> ? */
7400 if (sscanf(cmd, "speed %d", &speed) != 1)
7401 return 0;
7403 *rc = fan_set_speed(speed);
7404 if (*rc == -ENXIO)
7405 printk(TPACPI_ERR "speed command accepted for unsupported "
7406 "access mode %d", fan_control_access_mode);
7407 else if (!*rc)
7408 tpacpi_disclose_usertask("procfs fan",
7409 "set speed to %d\n", speed);
7411 return 1;
7414 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7416 int interval;
7418 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7419 return 0;
7421 if (interval < 0 || interval > 120)
7422 *rc = -EINVAL;
7423 else {
7424 fan_watchdog_maxinterval = interval;
7425 tpacpi_disclose_usertask("procfs fan",
7426 "set watchdog timer to %d\n",
7427 interval);
7430 return 1;
7433 static int fan_write(char *buf)
7435 char *cmd;
7436 int rc = 0;
7438 while (!rc && (cmd = next_cmd(&buf))) {
7439 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7440 fan_write_cmd_level(cmd, &rc)) &&
7441 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7442 (fan_write_cmd_enable(cmd, &rc) ||
7443 fan_write_cmd_disable(cmd, &rc) ||
7444 fan_write_cmd_watchdog(cmd, &rc))) &&
7445 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7446 fan_write_cmd_speed(cmd, &rc))
7448 rc = -EINVAL;
7449 else if (!rc)
7450 fan_watchdog_reset();
7453 return rc;
7456 static struct ibm_struct fan_driver_data = {
7457 .name = "fan",
7458 .read = fan_read,
7459 .write = fan_write,
7460 .exit = fan_exit,
7461 .suspend = fan_suspend,
7462 .resume = fan_resume,
7465 /****************************************************************************
7466 ****************************************************************************
7468 * Infrastructure
7470 ****************************************************************************
7471 ****************************************************************************/
7473 /* sysfs name ---------------------------------------------------------- */
7474 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7475 struct device_attribute *attr,
7476 char *buf)
7478 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7481 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7482 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7484 /* --------------------------------------------------------------------- */
7486 /* /proc support */
7487 static struct proc_dir_entry *proc_dir;
7490 * Module and infrastructure proble, init and exit handling
7493 static int force_load;
7495 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7496 static const char * __init str_supported(int is_supported)
7498 static char text_unsupported[] __initdata = "not supported";
7500 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7502 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7504 static void ibm_exit(struct ibm_struct *ibm)
7506 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7508 list_del_init(&ibm->all_drivers);
7510 if (ibm->flags.acpi_notify_installed) {
7511 dbg_printk(TPACPI_DBG_EXIT,
7512 "%s: acpi_remove_notify_handler\n", ibm->name);
7513 BUG_ON(!ibm->acpi);
7514 acpi_remove_notify_handler(*ibm->acpi->handle,
7515 ibm->acpi->type,
7516 dispatch_acpi_notify);
7517 ibm->flags.acpi_notify_installed = 0;
7518 ibm->flags.acpi_notify_installed = 0;
7521 if (ibm->flags.proc_created) {
7522 dbg_printk(TPACPI_DBG_EXIT,
7523 "%s: remove_proc_entry\n", ibm->name);
7524 remove_proc_entry(ibm->name, proc_dir);
7525 ibm->flags.proc_created = 0;
7528 if (ibm->flags.acpi_driver_registered) {
7529 dbg_printk(TPACPI_DBG_EXIT,
7530 "%s: acpi_bus_unregister_driver\n", ibm->name);
7531 BUG_ON(!ibm->acpi);
7532 acpi_bus_unregister_driver(ibm->acpi->driver);
7533 kfree(ibm->acpi->driver);
7534 ibm->acpi->driver = NULL;
7535 ibm->flags.acpi_driver_registered = 0;
7538 if (ibm->flags.init_called && ibm->exit) {
7539 ibm->exit();
7540 ibm->flags.init_called = 0;
7543 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7546 static int __init ibm_init(struct ibm_init_struct *iibm)
7548 int ret;
7549 struct ibm_struct *ibm = iibm->data;
7550 struct proc_dir_entry *entry;
7552 BUG_ON(ibm == NULL);
7554 INIT_LIST_HEAD(&ibm->all_drivers);
7556 if (ibm->flags.experimental && !experimental)
7557 return 0;
7559 dbg_printk(TPACPI_DBG_INIT,
7560 "probing for %s\n", ibm->name);
7562 if (iibm->init) {
7563 ret = iibm->init(iibm);
7564 if (ret > 0)
7565 return 0; /* probe failed */
7566 if (ret)
7567 return ret;
7569 ibm->flags.init_called = 1;
7572 if (ibm->acpi) {
7573 if (ibm->acpi->hid) {
7574 ret = register_tpacpi_subdriver(ibm);
7575 if (ret)
7576 goto err_out;
7579 if (ibm->acpi->notify) {
7580 ret = setup_acpi_notify(ibm);
7581 if (ret == -ENODEV) {
7582 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7583 ibm->name);
7584 ret = 0;
7585 goto err_out;
7587 if (ret < 0)
7588 goto err_out;
7592 dbg_printk(TPACPI_DBG_INIT,
7593 "%s installed\n", ibm->name);
7595 if (ibm->read) {
7596 entry = create_proc_entry(ibm->name,
7597 S_IFREG | S_IRUGO | S_IWUSR,
7598 proc_dir);
7599 if (!entry) {
7600 printk(TPACPI_ERR "unable to create proc entry %s\n",
7601 ibm->name);
7602 ret = -ENODEV;
7603 goto err_out;
7605 entry->data = ibm;
7606 entry->read_proc = &dispatch_procfs_read;
7607 if (ibm->write)
7608 entry->write_proc = &dispatch_procfs_write;
7609 ibm->flags.proc_created = 1;
7612 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7614 return 0;
7616 err_out:
7617 dbg_printk(TPACPI_DBG_INIT,
7618 "%s: at error exit path with result %d\n",
7619 ibm->name, ret);
7621 ibm_exit(ibm);
7622 return (ret < 0)? ret : 0;
7625 /* Probing */
7627 static bool __pure __init tpacpi_is_fw_digit(const char c)
7629 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7632 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7633 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7634 const char t)
7636 return s && strlen(s) >= 8 &&
7637 tpacpi_is_fw_digit(s[0]) &&
7638 tpacpi_is_fw_digit(s[1]) &&
7639 s[2] == t && s[3] == 'T' &&
7640 tpacpi_is_fw_digit(s[4]) &&
7641 tpacpi_is_fw_digit(s[5]) &&
7642 s[6] == 'W' && s[7] == 'W';
7645 /* returns 0 - probe ok, or < 0 - probe error.
7646 * Probe ok doesn't mean thinkpad found.
7647 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7648 static int __must_check __init get_thinkpad_model_data(
7649 struct thinkpad_id_data *tp)
7651 const struct dmi_device *dev = NULL;
7652 char ec_fw_string[18];
7653 char const *s;
7655 if (!tp)
7656 return -EINVAL;
7658 memset(tp, 0, sizeof(*tp));
7660 if (dmi_name_in_vendors("IBM"))
7661 tp->vendor = PCI_VENDOR_ID_IBM;
7662 else if (dmi_name_in_vendors("LENOVO"))
7663 tp->vendor = PCI_VENDOR_ID_LENOVO;
7664 else
7665 return 0;
7667 s = dmi_get_system_info(DMI_BIOS_VERSION);
7668 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7669 if (s && !tp->bios_version_str)
7670 return -ENOMEM;
7672 /* Really ancient ThinkPad 240X will fail this, which is fine */
7673 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7674 return 0;
7676 tp->bios_model = tp->bios_version_str[0]
7677 | (tp->bios_version_str[1] << 8);
7678 tp->bios_release = (tp->bios_version_str[4] << 8)
7679 | tp->bios_version_str[5];
7682 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7683 * X32 or newer, all Z series; Some models must have an
7684 * up-to-date BIOS or they will not be detected.
7686 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7688 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7689 if (sscanf(dev->name,
7690 "IBM ThinkPad Embedded Controller -[%17c",
7691 ec_fw_string) == 1) {
7692 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7693 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7695 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7696 if (!tp->ec_version_str)
7697 return -ENOMEM;
7699 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7700 tp->ec_model = ec_fw_string[0]
7701 | (ec_fw_string[1] << 8);
7702 tp->ec_release = (ec_fw_string[4] << 8)
7703 | ec_fw_string[5];
7704 } else {
7705 printk(TPACPI_NOTICE
7706 "ThinkPad firmware release %s "
7707 "doesn't match the known patterns\n",
7708 ec_fw_string);
7709 printk(TPACPI_NOTICE
7710 "please report this to %s\n",
7711 TPACPI_MAIL);
7713 break;
7717 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7718 if (s && !strnicmp(s, "ThinkPad", 8)) {
7719 tp->model_str = kstrdup(s, GFP_KERNEL);
7720 if (!tp->model_str)
7721 return -ENOMEM;
7724 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7725 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7726 if (s && !tp->nummodel_str)
7727 return -ENOMEM;
7729 return 0;
7732 static int __init probe_for_thinkpad(void)
7734 int is_thinkpad;
7736 if (acpi_disabled)
7737 return -ENODEV;
7740 * Non-ancient models have better DMI tagging, but very old models
7741 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7743 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7744 (thinkpad_id.ec_model != 0) ||
7745 tpacpi_is_fw_known();
7747 /* ec is required because many other handles are relative to it */
7748 TPACPI_ACPIHANDLE_INIT(ec);
7749 if (!ec_handle) {
7750 if (is_thinkpad)
7751 printk(TPACPI_ERR
7752 "Not yet supported ThinkPad detected!\n");
7753 return -ENODEV;
7756 if (!is_thinkpad && !force_load)
7757 return -ENODEV;
7759 return 0;
7763 /* Module init, exit, parameters */
7765 static struct ibm_init_struct ibms_init[] __initdata = {
7767 .init = thinkpad_acpi_driver_init,
7768 .data = &thinkpad_acpi_driver_data,
7771 .init = hotkey_init,
7772 .data = &hotkey_driver_data,
7775 .init = bluetooth_init,
7776 .data = &bluetooth_driver_data,
7779 .init = wan_init,
7780 .data = &wan_driver_data,
7783 .init = uwb_init,
7784 .data = &uwb_driver_data,
7786 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7788 .init = video_init,
7789 .data = &video_driver_data,
7791 #endif
7793 .init = light_init,
7794 .data = &light_driver_data,
7797 .init = cmos_init,
7798 .data = &cmos_driver_data,
7801 .init = led_init,
7802 .data = &led_driver_data,
7805 .init = beep_init,
7806 .data = &beep_driver_data,
7809 .init = thermal_init,
7810 .data = &thermal_driver_data,
7813 .data = &ecdump_driver_data,
7816 .init = brightness_init,
7817 .data = &brightness_driver_data,
7820 .data = &volume_driver_data,
7823 .init = fan_init,
7824 .data = &fan_driver_data,
7828 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7830 unsigned int i;
7831 struct ibm_struct *ibm;
7833 if (!kp || !kp->name || !val)
7834 return -EINVAL;
7836 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7837 ibm = ibms_init[i].data;
7838 WARN_ON(ibm == NULL);
7840 if (!ibm || !ibm->name)
7841 continue;
7843 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7844 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7845 return -ENOSPC;
7846 strcpy(ibms_init[i].param, val);
7847 strcat(ibms_init[i].param, ",");
7848 return 0;
7852 return -EINVAL;
7855 module_param(experimental, int, 0);
7856 MODULE_PARM_DESC(experimental,
7857 "Enables experimental features when non-zero");
7859 module_param_named(debug, dbg_level, uint, 0);
7860 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7862 module_param(force_load, bool, 0);
7863 MODULE_PARM_DESC(force_load,
7864 "Attempts to load the driver even on a "
7865 "mis-identified ThinkPad when true");
7867 module_param_named(fan_control, fan_control_allowed, bool, 0);
7868 MODULE_PARM_DESC(fan_control,
7869 "Enables setting fan parameters features when true");
7871 module_param_named(brightness_mode, brightness_mode, uint, 0);
7872 MODULE_PARM_DESC(brightness_mode,
7873 "Selects brightness control strategy: "
7874 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7876 module_param(brightness_enable, uint, 0);
7877 MODULE_PARM_DESC(brightness_enable,
7878 "Enables backlight control when 1, disables when 0");
7880 module_param(hotkey_report_mode, uint, 0);
7881 MODULE_PARM_DESC(hotkey_report_mode,
7882 "used for backwards compatibility with userspace, "
7883 "see documentation");
7885 #define TPACPI_PARAM(feature) \
7886 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7887 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7888 "at module load, see documentation")
7890 TPACPI_PARAM(hotkey);
7891 TPACPI_PARAM(bluetooth);
7892 TPACPI_PARAM(video);
7893 TPACPI_PARAM(light);
7894 TPACPI_PARAM(cmos);
7895 TPACPI_PARAM(led);
7896 TPACPI_PARAM(beep);
7897 TPACPI_PARAM(ecdump);
7898 TPACPI_PARAM(brightness);
7899 TPACPI_PARAM(volume);
7900 TPACPI_PARAM(fan);
7902 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7903 module_param(dbg_wlswemul, uint, 0);
7904 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7905 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7906 MODULE_PARM_DESC(wlsw_state,
7907 "Initial state of the emulated WLSW switch");
7909 module_param(dbg_bluetoothemul, uint, 0);
7910 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7911 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7912 MODULE_PARM_DESC(bluetooth_state,
7913 "Initial state of the emulated bluetooth switch");
7915 module_param(dbg_wwanemul, uint, 0);
7916 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7917 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7918 MODULE_PARM_DESC(wwan_state,
7919 "Initial state of the emulated WWAN switch");
7921 module_param(dbg_uwbemul, uint, 0);
7922 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7923 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7924 MODULE_PARM_DESC(uwb_state,
7925 "Initial state of the emulated UWB switch");
7926 #endif
7928 static void thinkpad_acpi_module_exit(void)
7930 struct ibm_struct *ibm, *itmp;
7932 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7934 list_for_each_entry_safe_reverse(ibm, itmp,
7935 &tpacpi_all_drivers,
7936 all_drivers) {
7937 ibm_exit(ibm);
7940 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7942 if (tpacpi_inputdev) {
7943 if (tp_features.input_device_registered)
7944 input_unregister_device(tpacpi_inputdev);
7945 else
7946 input_free_device(tpacpi_inputdev);
7949 if (tpacpi_hwmon)
7950 hwmon_device_unregister(tpacpi_hwmon);
7952 if (tp_features.sensors_pdev_attrs_registered)
7953 device_remove_file(&tpacpi_sensors_pdev->dev,
7954 &dev_attr_thinkpad_acpi_pdev_name);
7955 if (tpacpi_sensors_pdev)
7956 platform_device_unregister(tpacpi_sensors_pdev);
7957 if (tpacpi_pdev)
7958 platform_device_unregister(tpacpi_pdev);
7960 if (tp_features.sensors_pdrv_attrs_registered)
7961 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7962 if (tp_features.platform_drv_attrs_registered)
7963 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7965 if (tp_features.sensors_pdrv_registered)
7966 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7968 if (tp_features.platform_drv_registered)
7969 platform_driver_unregister(&tpacpi_pdriver);
7971 if (proc_dir)
7972 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7974 if (tpacpi_wq)
7975 destroy_workqueue(tpacpi_wq);
7977 kfree(thinkpad_id.bios_version_str);
7978 kfree(thinkpad_id.ec_version_str);
7979 kfree(thinkpad_id.model_str);
7983 static int __init thinkpad_acpi_module_init(void)
7985 int ret, i;
7987 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7989 /* Parameter checking */
7990 if (hotkey_report_mode > 2)
7991 return -EINVAL;
7993 /* Driver-level probe */
7995 ret = get_thinkpad_model_data(&thinkpad_id);
7996 if (ret) {
7997 printk(TPACPI_ERR
7998 "unable to get DMI data: %d\n", ret);
7999 thinkpad_acpi_module_exit();
8000 return ret;
8002 ret = probe_for_thinkpad();
8003 if (ret) {
8004 thinkpad_acpi_module_exit();
8005 return ret;
8008 /* Driver initialization */
8010 TPACPI_ACPIHANDLE_INIT(ecrd);
8011 TPACPI_ACPIHANDLE_INIT(ecwr);
8013 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8014 if (!tpacpi_wq) {
8015 thinkpad_acpi_module_exit();
8016 return -ENOMEM;
8019 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8020 if (!proc_dir) {
8021 printk(TPACPI_ERR
8022 "unable to create proc dir " TPACPI_PROC_DIR);
8023 thinkpad_acpi_module_exit();
8024 return -ENODEV;
8027 ret = platform_driver_register(&tpacpi_pdriver);
8028 if (ret) {
8029 printk(TPACPI_ERR
8030 "unable to register main platform driver\n");
8031 thinkpad_acpi_module_exit();
8032 return ret;
8034 tp_features.platform_drv_registered = 1;
8036 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8037 if (ret) {
8038 printk(TPACPI_ERR
8039 "unable to register hwmon platform driver\n");
8040 thinkpad_acpi_module_exit();
8041 return ret;
8043 tp_features.sensors_pdrv_registered = 1;
8045 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8046 if (!ret) {
8047 tp_features.platform_drv_attrs_registered = 1;
8048 ret = tpacpi_create_driver_attributes(
8049 &tpacpi_hwmon_pdriver.driver);
8051 if (ret) {
8052 printk(TPACPI_ERR
8053 "unable to create sysfs driver attributes\n");
8054 thinkpad_acpi_module_exit();
8055 return ret;
8057 tp_features.sensors_pdrv_attrs_registered = 1;
8060 /* Device initialization */
8061 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8062 NULL, 0);
8063 if (IS_ERR(tpacpi_pdev)) {
8064 ret = PTR_ERR(tpacpi_pdev);
8065 tpacpi_pdev = NULL;
8066 printk(TPACPI_ERR "unable to register platform device\n");
8067 thinkpad_acpi_module_exit();
8068 return ret;
8070 tpacpi_sensors_pdev = platform_device_register_simple(
8071 TPACPI_HWMON_DRVR_NAME,
8072 -1, NULL, 0);
8073 if (IS_ERR(tpacpi_sensors_pdev)) {
8074 ret = PTR_ERR(tpacpi_sensors_pdev);
8075 tpacpi_sensors_pdev = NULL;
8076 printk(TPACPI_ERR
8077 "unable to register hwmon platform device\n");
8078 thinkpad_acpi_module_exit();
8079 return ret;
8081 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8082 &dev_attr_thinkpad_acpi_pdev_name);
8083 if (ret) {
8084 printk(TPACPI_ERR
8085 "unable to create sysfs hwmon device attributes\n");
8086 thinkpad_acpi_module_exit();
8087 return ret;
8089 tp_features.sensors_pdev_attrs_registered = 1;
8090 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8091 if (IS_ERR(tpacpi_hwmon)) {
8092 ret = PTR_ERR(tpacpi_hwmon);
8093 tpacpi_hwmon = NULL;
8094 printk(TPACPI_ERR "unable to register hwmon device\n");
8095 thinkpad_acpi_module_exit();
8096 return ret;
8098 mutex_init(&tpacpi_inputdev_send_mutex);
8099 tpacpi_inputdev = input_allocate_device();
8100 if (!tpacpi_inputdev) {
8101 printk(TPACPI_ERR "unable to allocate input device\n");
8102 thinkpad_acpi_module_exit();
8103 return -ENOMEM;
8104 } else {
8105 /* Prepare input device, but don't register */
8106 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8107 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8108 tpacpi_inputdev->id.bustype = BUS_HOST;
8109 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8110 thinkpad_id.vendor :
8111 PCI_VENDOR_ID_IBM;
8112 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8113 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8115 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8116 ret = ibm_init(&ibms_init[i]);
8117 if (ret >= 0 && *ibms_init[i].param)
8118 ret = ibms_init[i].data->write(ibms_init[i].param);
8119 if (ret < 0) {
8120 thinkpad_acpi_module_exit();
8121 return ret;
8124 ret = input_register_device(tpacpi_inputdev);
8125 if (ret < 0) {
8126 printk(TPACPI_ERR "unable to register input device\n");
8127 thinkpad_acpi_module_exit();
8128 return ret;
8129 } else {
8130 tp_features.input_device_registered = 1;
8133 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8134 return 0;
8137 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8140 * This will autoload the driver in almost every ThinkPad
8141 * in widespread use.
8143 * Only _VERY_ old models, like the 240, 240x and 570 lack
8144 * the HKEY event interface.
8146 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8149 * DMI matching for module autoloading
8151 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8152 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8154 * Only models listed in thinkwiki will be supported, so add yours
8155 * if it is not there yet.
8157 #define IBM_BIOS_MODULE_ALIAS(__type) \
8158 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8160 /* Ancient thinkpad BIOSes have to be identified by
8161 * BIOS type or model number, and there are far less
8162 * BIOS types than model numbers... */
8163 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8165 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8166 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8167 MODULE_DESCRIPTION(TPACPI_DESC);
8168 MODULE_VERSION(TPACPI_VERSION);
8169 MODULE_LICENSE("GPL");
8171 module_init(thinkpad_acpi_module_init);
8172 module_exit(thinkpad_acpi_module_exit);