thinkpad-acpi: don't poll by default any of the reserved hotkeys
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob9c3bb0c498e14d9d67a9ce806cb079b4a495f791
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 /* printk headers */
170 #define TPACPI_LOG TPACPI_FILE ": "
171 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174 #define TPACPI_ERR KERN_ERR TPACPI_LOG
175 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
176 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177 #define TPACPI_INFO KERN_INFO TPACPI_LOG
178 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
180 /* Debugging printk groups */
181 #define TPACPI_DBG_ALL 0xffff
182 #define TPACPI_DBG_DISCLOSETASK 0x8000
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define TPACPI_DBG_RFKILL 0x0004
186 #define TPACPI_DBG_HKEY 0x0008
187 #define TPACPI_DBG_FAN 0x0010
188 #define TPACPI_DBG_BRGHT 0x0020
190 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
195 /****************************************************************************
196 * Driver-wide structs and misc. variables
199 struct ibm_struct;
201 struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
205 void (*notify) (struct ibm_struct *, u32);
206 acpi_handle *handle;
207 u32 type;
208 struct acpi_device *device;
211 struct ibm_struct {
212 char *name;
214 int (*read) (char *);
215 int (*write) (char *);
216 void (*exit) (void);
217 void (*resume) (void);
218 void (*suspend) (pm_message_t state);
219 void (*shutdown) (void);
221 struct list_head all_drivers;
223 struct tp_acpi_drv_struct *acpi;
225 struct {
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
228 u8 proc_created:1;
229 u8 init_called:1;
230 u8 experimental:1;
231 } flags;
234 struct ibm_init_struct {
235 char param[32];
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
241 static struct {
242 u32 bluetooth:1;
243 u32 hotkey:1;
244 u32 hotkey_mask:1;
245 u32 hotkey_wlsw:1;
246 u32 hotkey_tablet:1;
247 u32 light:1;
248 u32 light_status:1;
249 u32 bright_16levels:1;
250 u32 bright_acpimode:1;
251 u32 wan:1;
252 u32 uwb:1;
253 u32 fan_ctrl_status_undef:1;
254 u32 second_fan:1;
255 u32 beep_needs_two_args:1;
256 u32 input_device_registered:1;
257 u32 platform_drv_registered:1;
258 u32 platform_drv_attrs_registered:1;
259 u32 sensors_pdrv_registered:1;
260 u32 sensors_pdrv_attrs_registered:1;
261 u32 sensors_pdev_attrs_registered:1;
262 u32 hotkey_poll_active:1;
263 } tp_features;
265 static struct {
266 u16 hotkey_mask_ff:1;
267 } tp_warned;
269 struct thinkpad_id_data {
270 unsigned int vendor; /* ThinkPad vendor:
271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
273 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
274 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
276 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
277 u16 ec_model;
278 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
279 u16 ec_release;
281 char *model_str; /* ThinkPad T43 */
282 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
284 static struct thinkpad_id_data thinkpad_id;
286 static enum {
287 TPACPI_LIFE_INIT = 0,
288 TPACPI_LIFE_RUNNING,
289 TPACPI_LIFE_EXITING,
290 } tpacpi_lifecycle;
292 static int experimental;
293 static u32 dbg_level;
295 static struct workqueue_struct *tpacpi_wq;
297 enum led_status_t {
298 TPACPI_LED_OFF = 0,
299 TPACPI_LED_ON,
300 TPACPI_LED_BLINK,
303 /* Special LED class that can defer work */
304 struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
307 enum led_status_t new_state;
308 unsigned int led;
311 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312 static int dbg_wlswemul;
313 static int tpacpi_wlsw_emulstate;
314 static int dbg_bluetoothemul;
315 static int tpacpi_bluetooth_emulstate;
316 static int dbg_wwanemul;
317 static int tpacpi_wwan_emulstate;
318 static int dbg_uwbemul;
319 static int tpacpi_uwb_emulstate;
320 #endif
323 /*************************************************************************
324 * Debugging helpers
327 #define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
330 } while (0)
332 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
333 #define vdbg_printk dbg_printk
334 static const char *str_supported(int is_supported);
335 #else
336 #define vdbg_printk(a_dbg_level, format, arg...) \
337 do { } while (0)
338 #endif
340 static void tpacpi_log_usertask(const char * const what)
342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
343 what, task_tgid_vnr(current));
346 #define tpacpi_disclose_usertask(what, format, arg...) \
347 do { \
348 if (unlikely( \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
354 } while (0)
357 * Quirk handling helpers
359 * ThinkPad IDs and versions seen in the field so far
360 * are two-characters from the set [0-9A-Z], i.e. base 36.
362 * We use values well outside that range as specials.
365 #define TPACPI_MATCH_ANY 0xffffU
366 #define TPACPI_MATCH_UNKNOWN 0U
368 /* TPID('1', 'Y') == 0x5931 */
369 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
371 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
372 { .vendor = PCI_VENDOR_ID_IBM, \
373 .bios = TPID(__id1, __id2), \
374 .ec = TPACPI_MATCH_ANY, \
375 .quirks = (__quirk) }
377 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
378 { .vendor = PCI_VENDOR_ID_LENOVO, \
379 .bios = TPID(__id1, __id2), \
380 .ec = TPACPI_MATCH_ANY, \
381 .quirks = (__quirk) }
383 struct tpacpi_quirk {
384 unsigned int vendor;
385 u16 bios;
386 u16 ec;
387 unsigned long quirks;
391 * tpacpi_check_quirks() - search BIOS/EC version on a list
392 * @qlist: array of &struct tpacpi_quirk
393 * @qlist_size: number of elements in @qlist
395 * Iterates over a quirks list until one is found that matches the
396 * ThinkPad's vendor, BIOS and EC model.
398 * Returns 0 if nothing matches, otherwise returns the quirks field of
399 * the matching &struct tpacpi_quirk entry.
401 * The match criteria is: vendor, ec and bios much match.
403 static unsigned long __init tpacpi_check_quirks(
404 const struct tpacpi_quirk *qlist,
405 unsigned int qlist_size)
407 while (qlist_size) {
408 if ((qlist->vendor == thinkpad_id.vendor ||
409 qlist->vendor == TPACPI_MATCH_ANY) &&
410 (qlist->bios == thinkpad_id.bios_model ||
411 qlist->bios == TPACPI_MATCH_ANY) &&
412 (qlist->ec == thinkpad_id.ec_model ||
413 qlist->ec == TPACPI_MATCH_ANY))
414 return qlist->quirks;
416 qlist_size--;
417 qlist++;
419 return 0;
423 /****************************************************************************
424 ****************************************************************************
426 * ACPI Helpers and device model
428 ****************************************************************************
429 ****************************************************************************/
431 /*************************************************************************
432 * ACPI basic handles
435 static acpi_handle root_handle;
437 #define TPACPI_HANDLE(object, parent, paths...) \
438 static acpi_handle object##_handle; \
439 static acpi_handle *object##_parent = &parent##_handle; \
440 static char *object##_path; \
441 static char *object##_paths[] = { paths }
443 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
444 "\\_SB.PCI.ISA.EC", /* 570 */
445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
448 "\\_SB.PCI0.ICH3.EC0", /* R31 */
449 "\\_SB.PCI0.LPC.EC", /* all others */
452 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
453 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
455 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
456 /* T4x, X31, X40 */
457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
458 "\\CMS", /* R40, R40e */
459 ); /* all others */
461 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
462 "^HKEY", /* R30, R31 */
463 "HKEY", /* all others */
464 ); /* 570 */
466 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
468 "\\_SB.PCI0.VID0", /* 770e */
469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
470 "\\_SB.PCI0.AGP.VID", /* all others */
471 ); /* R30, R31 */
474 /*************************************************************************
475 * ACPI helpers
478 static int acpi_evalf(acpi_handle handle,
479 void *res, char *method, char *fmt, ...)
481 char *fmt0 = fmt;
482 struct acpi_object_list params;
483 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
484 struct acpi_buffer result, *resultp;
485 union acpi_object out_obj;
486 acpi_status status;
487 va_list ap;
488 char res_type;
489 int success;
490 int quiet;
492 if (!*fmt) {
493 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
494 return 0;
497 if (*fmt == 'q') {
498 quiet = 1;
499 fmt++;
500 } else
501 quiet = 0;
503 res_type = *(fmt++);
505 params.count = 0;
506 params.pointer = &in_objs[0];
508 va_start(ap, fmt);
509 while (*fmt) {
510 char c = *(fmt++);
511 switch (c) {
512 case 'd': /* int */
513 in_objs[params.count].integer.value = va_arg(ap, int);
514 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
515 break;
516 /* add more types as needed */
517 default:
518 printk(TPACPI_ERR "acpi_evalf() called "
519 "with invalid format character '%c'\n", c);
520 return 0;
523 va_end(ap);
525 if (res_type != 'v') {
526 result.length = sizeof(out_obj);
527 result.pointer = &out_obj;
528 resultp = &result;
529 } else
530 resultp = NULL;
532 status = acpi_evaluate_object(handle, method, &params, resultp);
534 switch (res_type) {
535 case 'd': /* int */
536 if (res)
537 *(int *)res = out_obj.integer.value;
538 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
539 break;
540 case 'v': /* void */
541 success = status == AE_OK;
542 break;
543 /* add more types as needed */
544 default:
545 printk(TPACPI_ERR "acpi_evalf() called "
546 "with invalid format character '%c'\n", res_type);
547 return 0;
550 if (!success && !quiet)
551 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
552 method, fmt0, status);
554 return success;
557 static int acpi_ec_read(int i, u8 *p)
559 int v;
561 if (ecrd_handle) {
562 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
563 return 0;
564 *p = v;
565 } else {
566 if (ec_read(i, p) < 0)
567 return 0;
570 return 1;
573 static int acpi_ec_write(int i, u8 v)
575 if (ecwr_handle) {
576 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
577 return 0;
578 } else {
579 if (ec_write(i, v) < 0)
580 return 0;
583 return 1;
586 static int issue_thinkpad_cmos_command(int cmos_cmd)
588 if (!cmos_handle)
589 return -ENXIO;
591 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
592 return -EIO;
594 return 0;
597 /*************************************************************************
598 * ACPI device model
601 #define TPACPI_ACPIHANDLE_INIT(object) \
602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
605 static void drv_acpi_handle_init(char *name,
606 acpi_handle *handle, acpi_handle parent,
607 char **paths, int num_paths, char **path)
609 int i;
610 acpi_status status;
612 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
613 name);
615 for (i = 0; i < num_paths; i++) {
616 status = acpi_get_handle(parent, paths[i], handle);
617 if (ACPI_SUCCESS(status)) {
618 *path = paths[i];
619 dbg_printk(TPACPI_DBG_INIT,
620 "Found ACPI handle %s for %s\n",
621 *path, name);
622 return;
626 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
627 name);
628 *handle = NULL;
631 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
633 struct ibm_struct *ibm = data;
635 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
636 return;
638 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
639 return;
641 ibm->acpi->notify(ibm, event);
644 static int __init setup_acpi_notify(struct ibm_struct *ibm)
646 acpi_status status;
647 int rc;
649 BUG_ON(!ibm->acpi);
651 if (!*ibm->acpi->handle)
652 return 0;
654 vdbg_printk(TPACPI_DBG_INIT,
655 "setting up ACPI notify for %s\n", ibm->name);
657 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
658 if (rc < 0) {
659 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
660 ibm->name, rc);
661 return -ENODEV;
664 ibm->acpi->device->driver_data = ibm;
665 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
666 TPACPI_ACPI_EVENT_PREFIX,
667 ibm->name);
669 status = acpi_install_notify_handler(*ibm->acpi->handle,
670 ibm->acpi->type, dispatch_acpi_notify, ibm);
671 if (ACPI_FAILURE(status)) {
672 if (status == AE_ALREADY_EXISTS) {
673 printk(TPACPI_NOTICE
674 "another device driver is already "
675 "handling %s events\n", ibm->name);
676 } else {
677 printk(TPACPI_ERR
678 "acpi_install_notify_handler(%s) failed: %d\n",
679 ibm->name, status);
681 return -ENODEV;
683 ibm->flags.acpi_notify_installed = 1;
684 return 0;
687 static int __init tpacpi_device_add(struct acpi_device *device)
689 return 0;
692 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
694 int rc;
696 dbg_printk(TPACPI_DBG_INIT,
697 "registering %s as an ACPI driver\n", ibm->name);
699 BUG_ON(!ibm->acpi);
701 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
702 if (!ibm->acpi->driver) {
703 printk(TPACPI_ERR
704 "failed to allocate memory for ibm->acpi->driver\n");
705 return -ENOMEM;
708 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
709 ibm->acpi->driver->ids = ibm->acpi->hid;
711 ibm->acpi->driver->ops.add = &tpacpi_device_add;
713 rc = acpi_bus_register_driver(ibm->acpi->driver);
714 if (rc < 0) {
715 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
716 ibm->name, rc);
717 kfree(ibm->acpi->driver);
718 ibm->acpi->driver = NULL;
719 } else if (!rc)
720 ibm->flags.acpi_driver_registered = 1;
722 return rc;
726 /****************************************************************************
727 ****************************************************************************
729 * Procfs Helpers
731 ****************************************************************************
732 ****************************************************************************/
734 static int dispatch_procfs_read(char *page, char **start, off_t off,
735 int count, int *eof, void *data)
737 struct ibm_struct *ibm = data;
738 int len;
740 if (!ibm || !ibm->read)
741 return -EINVAL;
743 len = ibm->read(page);
744 if (len < 0)
745 return len;
747 if (len <= off + count)
748 *eof = 1;
749 *start = page + off;
750 len -= off;
751 if (len > count)
752 len = count;
753 if (len < 0)
754 len = 0;
756 return len;
759 static int dispatch_procfs_write(struct file *file,
760 const char __user *userbuf,
761 unsigned long count, void *data)
763 struct ibm_struct *ibm = data;
764 char *kernbuf;
765 int ret;
767 if (!ibm || !ibm->write)
768 return -EINVAL;
769 if (count > PAGE_SIZE - 2)
770 return -EINVAL;
772 kernbuf = kmalloc(count + 2, GFP_KERNEL);
773 if (!kernbuf)
774 return -ENOMEM;
776 if (copy_from_user(kernbuf, userbuf, count)) {
777 kfree(kernbuf);
778 return -EFAULT;
781 kernbuf[count] = 0;
782 strcat(kernbuf, ",");
783 ret = ibm->write(kernbuf);
784 if (ret == 0)
785 ret = count;
787 kfree(kernbuf);
789 return ret;
792 static char *next_cmd(char **cmds)
794 char *start = *cmds;
795 char *end;
797 while ((end = strchr(start, ',')) && end == start)
798 start = end + 1;
800 if (!end)
801 return NULL;
803 *end = 0;
804 *cmds = end + 1;
805 return start;
809 /****************************************************************************
810 ****************************************************************************
812 * Device model: input, hwmon and platform
814 ****************************************************************************
815 ****************************************************************************/
817 static struct platform_device *tpacpi_pdev;
818 static struct platform_device *tpacpi_sensors_pdev;
819 static struct device *tpacpi_hwmon;
820 static struct input_dev *tpacpi_inputdev;
821 static struct mutex tpacpi_inputdev_send_mutex;
822 static LIST_HEAD(tpacpi_all_drivers);
824 static int tpacpi_suspend_handler(struct platform_device *pdev,
825 pm_message_t state)
827 struct ibm_struct *ibm, *itmp;
829 list_for_each_entry_safe(ibm, itmp,
830 &tpacpi_all_drivers,
831 all_drivers) {
832 if (ibm->suspend)
833 (ibm->suspend)(state);
836 return 0;
839 static int tpacpi_resume_handler(struct platform_device *pdev)
841 struct ibm_struct *ibm, *itmp;
843 list_for_each_entry_safe(ibm, itmp,
844 &tpacpi_all_drivers,
845 all_drivers) {
846 if (ibm->resume)
847 (ibm->resume)();
850 return 0;
853 static void tpacpi_shutdown_handler(struct platform_device *pdev)
855 struct ibm_struct *ibm, *itmp;
857 list_for_each_entry_safe(ibm, itmp,
858 &tpacpi_all_drivers,
859 all_drivers) {
860 if (ibm->shutdown)
861 (ibm->shutdown)();
865 static struct platform_driver tpacpi_pdriver = {
866 .driver = {
867 .name = TPACPI_DRVR_NAME,
868 .owner = THIS_MODULE,
870 .suspend = tpacpi_suspend_handler,
871 .resume = tpacpi_resume_handler,
872 .shutdown = tpacpi_shutdown_handler,
875 static struct platform_driver tpacpi_hwmon_pdriver = {
876 .driver = {
877 .name = TPACPI_HWMON_DRVR_NAME,
878 .owner = THIS_MODULE,
882 /*************************************************************************
883 * sysfs support helpers
886 struct attribute_set {
887 unsigned int members, max_members;
888 struct attribute_group group;
891 struct attribute_set_obj {
892 struct attribute_set s;
893 struct attribute *a;
894 } __attribute__((packed));
896 static struct attribute_set *create_attr_set(unsigned int max_members,
897 const char *name)
899 struct attribute_set_obj *sobj;
901 if (max_members == 0)
902 return NULL;
904 /* Allocates space for implicit NULL at the end too */
905 sobj = kzalloc(sizeof(struct attribute_set_obj) +
906 max_members * sizeof(struct attribute *),
907 GFP_KERNEL);
908 if (!sobj)
909 return NULL;
910 sobj->s.max_members = max_members;
911 sobj->s.group.attrs = &sobj->a;
912 sobj->s.group.name = name;
914 return &sobj->s;
917 #define destroy_attr_set(_set) \
918 kfree(_set);
920 /* not multi-threaded safe, use it in a single thread per set */
921 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
923 if (!s || !attr)
924 return -EINVAL;
926 if (s->members >= s->max_members)
927 return -ENOMEM;
929 s->group.attrs[s->members] = attr;
930 s->members++;
932 return 0;
935 static int add_many_to_attr_set(struct attribute_set *s,
936 struct attribute **attr,
937 unsigned int count)
939 int i, res;
941 for (i = 0; i < count; i++) {
942 res = add_to_attr_set(s, attr[i]);
943 if (res)
944 return res;
947 return 0;
950 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
952 sysfs_remove_group(kobj, &s->group);
953 destroy_attr_set(s);
956 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
959 static int parse_strtoul(const char *buf,
960 unsigned long max, unsigned long *value)
962 char *endp;
964 while (*buf && isspace(*buf))
965 buf++;
966 *value = simple_strtoul(buf, &endp, 0);
967 while (*endp && isspace(*endp))
968 endp++;
969 if (*endp || *value > max)
970 return -EINVAL;
972 return 0;
975 static void tpacpi_disable_brightness_delay(void)
977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
978 printk(TPACPI_NOTICE
979 "ACPI backlight control delay disabled\n");
982 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
985 union acpi_object *obj;
986 int rc;
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
989 obj = (union acpi_object *)buffer.pointer;
990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
991 printk(TPACPI_ERR "Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL);
993 rc = 0;
994 } else {
995 rc = obj->package.count;
997 } else {
998 return 0;
1001 kfree(buffer.pointer);
1002 return rc;
1005 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1006 u32 lvl, void *context, void **rv)
1008 char name[ACPI_PATH_SEGMENT_LENGTH];
1009 struct acpi_buffer buffer = { sizeof(name), &name };
1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1012 !strncmp("_BCL", name, sizeof(name) - 1)) {
1013 BUG_ON(!rv || !*rv);
1014 **(int **)rv = tpacpi_query_bcl_levels(handle);
1015 return AE_CTRL_TERMINATE;
1016 } else {
1017 return AE_OK;
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1024 static int __init tpacpi_check_std_acpi_brightness_support(void)
1026 int status;
1027 int bcl_levels = 0;
1028 void *bcl_ptr = &bcl_levels;
1030 if (!vid_handle) {
1031 TPACPI_ACPIHANDLE_INIT(vid);
1033 if (!vid_handle)
1034 return 0;
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1045 tpacpi_acpi_walk_find_bcl, NULL,
1046 &bcl_ptr);
1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1049 tp_features.bright_acpimode = 1;
1050 return (bcl_levels - 2);
1053 return 0;
1056 static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1065 /*************************************************************************
1066 * rfkill and radio control support helpers
1070 * ThinkPad-ACPI firmware handling model:
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1084 * Internal driver API for radio state:
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1089 enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1091 TPACPI_RFK_RADIO_ON
1094 /* rfkill switches */
1095 enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1099 TPACPI_RFK_SW_MAX
1102 static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1109 /* ThinkPad-ACPI rfkill subdriver */
1110 struct tpacpi_rfk {
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1116 struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1122 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1124 /* Query FW and update rfkill sw state for a given rfkill switch */
1125 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1127 int status;
1129 if (!tp_rfk)
1130 return -ENODEV;
1132 status = (tp_rfk->ops->get_status)();
1133 if (status < 0)
1134 return status;
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1139 return status;
1142 /* Query FW and update rfkill sw state for all rfkill switches */
1143 static void tpacpi_rfk_update_swstate_all(void)
1145 unsigned int i;
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1155 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1157 unsigned int i;
1158 struct tpacpi_rfk *tp_rfk;
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1162 if (tp_rfk) {
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1164 blocked)) {
1165 /* ignore -- we track sw block */
1171 /* Call to get the WLSW state from the firmware */
1172 static int hotkey_get_wlsw(void);
1174 /* Call to query WLSW state and update all rfkill switches */
1175 static bool tpacpi_rfk_check_hwblock_state(void)
1177 int res = hotkey_get_wlsw();
1178 int hw_blocked;
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1181 if (res < 0)
1182 return false;
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1187 return hw_blocked;
1190 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1192 struct tpacpi_rfk *tp_rfk = data;
1193 int res;
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1206 return (res < 0) ? res : 0;
1209 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1213 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1216 const char *name,
1217 const bool set_default)
1219 struct tpacpi_rfk *atp_rfk;
1220 int res;
1221 bool sw_state = false;
1222 int sw_status;
1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1227 if (atp_rfk)
1228 atp_rfk->rfkill = rfkill_alloc(name,
1229 &tpacpi_pdev->dev,
1230 rfktype,
1231 &tpacpi_rfk_rfkill_ops,
1232 atp_rfk);
1233 if (!atp_rfk || !atp_rfk->rfkill) {
1234 printk(TPACPI_ERR
1235 "failed to allocate memory for rfkill class\n");
1236 kfree(atp_rfk);
1237 return -ENOMEM;
1240 atp_rfk->id = id;
1241 atp_rfk->ops = tp_rfkops;
1243 sw_status = (tp_rfkops->get_status)();
1244 if (sw_status < 0) {
1245 printk(TPACPI_ERR
1246 "failed to read initial state for %s, error %d\n",
1247 name, sw_status);
1248 } else {
1249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1250 if (set_default) {
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
1253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
1258 res = rfkill_register(atp_rfk->rfkill);
1259 if (res < 0) {
1260 printk(TPACPI_ERR
1261 "failed to register %s rfkill switch: %d\n",
1262 name, res);
1263 rfkill_destroy(atp_rfk->rfkill);
1264 kfree(atp_rfk);
1265 return res;
1268 tpacpi_rfkill_switches[id] = atp_rfk;
1269 return 0;
1272 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1274 struct tpacpi_rfk *tp_rfk;
1276 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1278 tp_rfk = tpacpi_rfkill_switches[id];
1279 if (tp_rfk) {
1280 rfkill_unregister(tp_rfk->rfkill);
1281 tpacpi_rfkill_switches[id] = NULL;
1282 kfree(tp_rfk);
1286 static void printk_deprecated_rfkill_attribute(const char * const what)
1288 printk_deprecated_attribute(what,
1289 "Please switch to generic rfkill before year 2010");
1292 /* sysfs <radio> enable ------------------------------------------------ */
1293 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1294 struct device_attribute *attr,
1295 char *buf)
1297 int status;
1299 printk_deprecated_rfkill_attribute(attr->attr.name);
1301 /* This is in the ABI... */
1302 if (tpacpi_rfk_check_hwblock_state()) {
1303 status = TPACPI_RFK_RADIO_OFF;
1304 } else {
1305 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1306 if (status < 0)
1307 return status;
1310 return snprintf(buf, PAGE_SIZE, "%d\n",
1311 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1314 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 const char *buf, size_t count)
1318 unsigned long t;
1319 int res;
1321 printk_deprecated_rfkill_attribute(attr->attr.name);
1323 if (parse_strtoul(buf, 1, &t))
1324 return -EINVAL;
1326 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state() && !!t)
1330 return -EPERM;
1332 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1333 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1336 return (res < 0) ? res : count;
1339 /* procfs -------------------------------------------------------------- */
1340 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1342 int len = 0;
1344 if (id >= TPACPI_RFK_SW_MAX)
1345 len += sprintf(p + len, "status:\t\tnot supported\n");
1346 else {
1347 int status;
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state()) {
1351 status = TPACPI_RFK_RADIO_OFF;
1352 } else {
1353 status = tpacpi_rfk_update_swstate(
1354 tpacpi_rfkill_switches[id]);
1355 if (status < 0)
1356 return status;
1359 len += sprintf(p + len, "status:\t\t%s\n",
1360 (status == TPACPI_RFK_RADIO_ON) ?
1361 "enabled" : "disabled");
1362 len += sprintf(p + len, "commands:\tenable, disable\n");
1365 return len;
1368 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1370 char *cmd;
1371 int status = -1;
1372 int res = 0;
1374 if (id >= TPACPI_RFK_SW_MAX)
1375 return -ENODEV;
1377 while ((cmd = next_cmd(&buf))) {
1378 if (strlencmp(cmd, "enable") == 0)
1379 status = TPACPI_RFK_RADIO_ON;
1380 else if (strlencmp(cmd, "disable") == 0)
1381 status = TPACPI_RFK_RADIO_OFF;
1382 else
1383 return -EINVAL;
1386 if (status != -1) {
1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enable" : "disable",
1390 tpacpi_rfkill_names[id]);
1391 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1395 return res;
1398 /*************************************************************************
1399 * thinkpad-acpi driver attributes
1402 /* interface_version --------------------------------------------------- */
1403 static ssize_t tpacpi_driver_interface_version_show(
1404 struct device_driver *drv,
1405 char *buf)
1407 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1410 static DRIVER_ATTR(interface_version, S_IRUGO,
1411 tpacpi_driver_interface_version_show, NULL);
1413 /* debug_level --------------------------------------------------------- */
1414 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1415 char *buf)
1417 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1420 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1421 const char *buf, size_t count)
1423 unsigned long t;
1425 if (parse_strtoul(buf, 0xffff, &t))
1426 return -EINVAL;
1428 dbg_level = t;
1430 return count;
1433 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1434 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1436 /* version ------------------------------------------------------------- */
1437 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1438 char *buf)
1440 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1441 TPACPI_DESC, TPACPI_VERSION);
1444 static DRIVER_ATTR(version, S_IRUGO,
1445 tpacpi_driver_version_show, NULL);
1447 /* --------------------------------------------------------------------- */
1449 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1451 /* wlsw_emulstate ------------------------------------------------------ */
1452 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1453 char *buf)
1455 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1458 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1461 unsigned long t;
1463 if (parse_strtoul(buf, 1, &t))
1464 return -EINVAL;
1466 if (tpacpi_wlsw_emulstate != !!t) {
1467 tpacpi_wlsw_emulstate = !!t;
1468 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1471 return count;
1474 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1475 tpacpi_driver_wlsw_emulstate_show,
1476 tpacpi_driver_wlsw_emulstate_store);
1478 /* bluetooth_emulstate ------------------------------------------------- */
1479 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1480 struct device_driver *drv,
1481 char *buf)
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1486 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1487 struct device_driver *drv,
1488 const char *buf, size_t count)
1490 unsigned long t;
1492 if (parse_strtoul(buf, 1, &t))
1493 return -EINVAL;
1495 tpacpi_bluetooth_emulstate = !!t;
1497 return count;
1500 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1501 tpacpi_driver_bluetooth_emulstate_show,
1502 tpacpi_driver_bluetooth_emulstate_store);
1504 /* wwan_emulstate ------------------------------------------------- */
1505 static ssize_t tpacpi_driver_wwan_emulstate_show(
1506 struct device_driver *drv,
1507 char *buf)
1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1512 static ssize_t tpacpi_driver_wwan_emulstate_store(
1513 struct device_driver *drv,
1514 const char *buf, size_t count)
1516 unsigned long t;
1518 if (parse_strtoul(buf, 1, &t))
1519 return -EINVAL;
1521 tpacpi_wwan_emulstate = !!t;
1523 return count;
1526 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1527 tpacpi_driver_wwan_emulstate_show,
1528 tpacpi_driver_wwan_emulstate_store);
1530 /* uwb_emulstate ------------------------------------------------- */
1531 static ssize_t tpacpi_driver_uwb_emulstate_show(
1532 struct device_driver *drv,
1533 char *buf)
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1538 static ssize_t tpacpi_driver_uwb_emulstate_store(
1539 struct device_driver *drv,
1540 const char *buf, size_t count)
1542 unsigned long t;
1544 if (parse_strtoul(buf, 1, &t))
1545 return -EINVAL;
1547 tpacpi_uwb_emulstate = !!t;
1549 return count;
1552 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1553 tpacpi_driver_uwb_emulstate_show,
1554 tpacpi_driver_uwb_emulstate_store);
1555 #endif
1557 /* --------------------------------------------------------------------- */
1559 static struct driver_attribute *tpacpi_driver_attributes[] = {
1560 &driver_attr_debug_level, &driver_attr_version,
1561 &driver_attr_interface_version,
1564 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1566 int i, res;
1568 i = 0;
1569 res = 0;
1570 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1571 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1572 i++;
1575 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1576 if (!res && dbg_wlswemul)
1577 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1578 if (!res && dbg_bluetoothemul)
1579 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1580 if (!res && dbg_wwanemul)
1581 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1582 if (!res && dbg_uwbemul)
1583 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1584 #endif
1586 return res;
1589 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1591 int i;
1593 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1594 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1596 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1597 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1598 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1599 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1600 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1601 #endif
1604 /*************************************************************************
1605 * Firmware Data
1609 * Table of recommended minimum BIOS versions
1611 * Reasons for listing:
1612 * 1. Stable BIOS, listed because the unknown ammount of
1613 * bugs and bad ACPI behaviour on older versions
1615 * 2. BIOS or EC fw with known bugs that trigger on Linux
1617 * 3. BIOS with known reduced functionality in older versions
1619 * We recommend the latest BIOS and EC version.
1620 * We only support the latest BIOS and EC fw version as a rule.
1622 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1623 * Information from users in ThinkWiki
1625 * WARNING: we use this table also to detect that the machine is
1626 * a ThinkPad in some cases, so don't remove entries lightly.
1629 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1630 { .vendor = (__v), \
1631 .bios = TPID(__id1, __id2), \
1632 .ec = TPACPI_MATCH_ANY, \
1633 .quirks = TPACPI_MATCH_ANY << 16 \
1634 | (__bv1) << 8 | (__bv2) }
1636 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1637 __eid1, __eid2, __ev1, __ev2) \
1638 { .vendor = (__v), \
1639 .bios = TPID(__bid1, __bid2), \
1640 .ec = TPID(__eid1, __eid2), \
1641 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1642 | (__bv1) << 8 | (__bv2) }
1644 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1645 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1647 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1648 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1649 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1651 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1652 __eid1, __eid2, __ev1, __ev2) \
1653 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1654 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1656 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1657 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1659 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1660 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1661 __bv1, __bv2, __id1, __id2, __ev1, __ev2)
1663 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1664 __eid1, __eid2, __ev1, __ev2) \
1665 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1666 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2)
1668 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1669 /* Numeric models ------------------ */
1670 /* FW MODEL BIOS VERS */
1671 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1672 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1673 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1674 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1675 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1676 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1677 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1678 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1679 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1681 /* A-series ------------------------- */
1682 /* FW MODEL BIOS VERS EC VERS */
1683 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1684 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1685 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1686 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1687 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1688 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1689 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1690 TPV_QI0('1', '3', '2', '0'), /* A22m */
1691 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1692 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1693 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1695 /* G-series ------------------------- */
1696 /* FW MODEL BIOS VERS */
1697 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1698 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1700 /* R-series, T-series --------------- */
1701 /* FW MODEL BIOS VERS EC VERS */
1702 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1703 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1704 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1705 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1706 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1707 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1708 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1709 T40/p, T41/p, T42/p (1) */
1710 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1711 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1712 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1713 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1715 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1716 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1717 TPV_QI0('1', '6', '3', '2'), /* T22 */
1718 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1719 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1720 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1722 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1723 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1724 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */
1726 /* BIOS FW BIOS VERS EC FW EC VERS */
1727 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1728 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1730 /* X-series ------------------------- */
1731 /* FW MODEL BIOS VERS EC VERS */
1732 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1733 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1734 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1735 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1736 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1737 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1738 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1740 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */
1741 TPV_QL0('7', 'J', '3', '0'), /* X60t */
1743 /* (0) - older versions lack DMI EC fw string and functionality */
1744 /* (1) - older versions known to lack functionality */
1747 #undef TPV_QL1
1748 #undef TPV_QL0
1749 #undef TPV_QI2
1750 #undef TPV_QI1
1751 #undef TPV_QI0
1752 #undef TPV_Q_X
1753 #undef TPV_Q
1755 static void __init tpacpi_check_outdated_fw(void)
1757 unsigned long fwvers;
1758 u16 ec_version, bios_version;
1760 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1761 ARRAY_SIZE(tpacpi_bios_version_qtable));
1763 if (!fwvers)
1764 return;
1766 bios_version = fwvers & 0xffffU;
1767 ec_version = (fwvers >> 16) & 0xffffU;
1769 /* note that unknown versions are set to 0x0000 and we use that */
1770 if ((bios_version > thinkpad_id.bios_release) ||
1771 (ec_version > thinkpad_id.ec_release &&
1772 ec_version != TPACPI_MATCH_ANY)) {
1774 * The changelogs would let us track down the exact
1775 * reason, but it is just too much of a pain to track
1776 * it. We only list BIOSes that are either really
1777 * broken, or really stable to begin with, so it is
1778 * best if the user upgrades the firmware anyway.
1780 printk(TPACPI_WARN
1781 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1782 printk(TPACPI_WARN
1783 "WARNING: This firmware may be missing critical bug "
1784 "fixes and/or important features\n");
1788 static bool __init tpacpi_is_fw_known(void)
1790 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1791 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1794 /****************************************************************************
1795 ****************************************************************************
1797 * Subdrivers
1799 ****************************************************************************
1800 ****************************************************************************/
1802 /*************************************************************************
1803 * thinkpad-acpi init subdriver
1806 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1808 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1809 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1811 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1812 (thinkpad_id.bios_version_str) ?
1813 thinkpad_id.bios_version_str : "unknown",
1814 (thinkpad_id.ec_version_str) ?
1815 thinkpad_id.ec_version_str : "unknown");
1817 if (thinkpad_id.vendor && thinkpad_id.model_str)
1818 printk(TPACPI_INFO "%s %s, model %s\n",
1819 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1820 "IBM" : ((thinkpad_id.vendor ==
1821 PCI_VENDOR_ID_LENOVO) ?
1822 "Lenovo" : "Unknown vendor"),
1823 thinkpad_id.model_str,
1824 (thinkpad_id.nummodel_str) ?
1825 thinkpad_id.nummodel_str : "unknown");
1827 tpacpi_check_outdated_fw();
1828 return 0;
1831 static int thinkpad_acpi_driver_read(char *p)
1833 int len = 0;
1835 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1836 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1838 return len;
1841 static struct ibm_struct thinkpad_acpi_driver_data = {
1842 .name = "driver",
1843 .read = thinkpad_acpi_driver_read,
1846 /*************************************************************************
1847 * Hotkey subdriver
1850 enum { /* hot key scan codes (derived from ACPI DSDT) */
1851 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1852 TP_ACPI_HOTKEYSCAN_FNF2,
1853 TP_ACPI_HOTKEYSCAN_FNF3,
1854 TP_ACPI_HOTKEYSCAN_FNF4,
1855 TP_ACPI_HOTKEYSCAN_FNF5,
1856 TP_ACPI_HOTKEYSCAN_FNF6,
1857 TP_ACPI_HOTKEYSCAN_FNF7,
1858 TP_ACPI_HOTKEYSCAN_FNF8,
1859 TP_ACPI_HOTKEYSCAN_FNF9,
1860 TP_ACPI_HOTKEYSCAN_FNF10,
1861 TP_ACPI_HOTKEYSCAN_FNF11,
1862 TP_ACPI_HOTKEYSCAN_FNF12,
1863 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1864 TP_ACPI_HOTKEYSCAN_FNINSERT,
1865 TP_ACPI_HOTKEYSCAN_FNDELETE,
1866 TP_ACPI_HOTKEYSCAN_FNHOME,
1867 TP_ACPI_HOTKEYSCAN_FNEND,
1868 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1869 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1870 TP_ACPI_HOTKEYSCAN_FNSPACE,
1871 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1872 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1873 TP_ACPI_HOTKEYSCAN_MUTE,
1874 TP_ACPI_HOTKEYSCAN_THINKPAD,
1877 enum { /* Keys available through NVRAM polling */
1878 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1879 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1882 enum { /* Positions of some of the keys in hotkey masks */
1883 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1884 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1885 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1886 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1887 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1888 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1889 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1890 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1891 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1892 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1893 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1896 enum { /* NVRAM to ACPI HKEY group map */
1897 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1898 TP_ACPI_HKEY_ZOOM_MASK |
1899 TP_ACPI_HKEY_DISPSWTCH_MASK |
1900 TP_ACPI_HKEY_HIBERNATE_MASK,
1901 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1902 TP_ACPI_HKEY_BRGHTDWN_MASK,
1903 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1904 TP_ACPI_HKEY_VOLDWN_MASK |
1905 TP_ACPI_HKEY_MUTE_MASK,
1908 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1909 struct tp_nvram_state {
1910 u16 thinkpad_toggle:1;
1911 u16 zoom_toggle:1;
1912 u16 display_toggle:1;
1913 u16 thinklight_toggle:1;
1914 u16 hibernate_toggle:1;
1915 u16 displayexp_toggle:1;
1916 u16 display_state:1;
1917 u16 brightness_toggle:1;
1918 u16 volume_toggle:1;
1919 u16 mute:1;
1921 u8 brightness_level;
1922 u8 volume_level;
1925 /* kthread for the hotkey poller */
1926 static struct task_struct *tpacpi_hotkey_task;
1928 /* Acquired while the poller kthread is running, use to sync start/stop */
1929 static struct mutex hotkey_thread_mutex;
1932 * Acquire mutex to write poller control variables.
1933 * Increment hotkey_config_change when changing them.
1935 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1937 static struct mutex hotkey_thread_data_mutex;
1938 static unsigned int hotkey_config_change;
1941 * hotkey poller control variables
1943 * Must be atomic or readers will also need to acquire mutex
1945 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1946 static unsigned int hotkey_poll_freq = 10; /* Hz */
1948 #define HOTKEY_CONFIG_CRITICAL_START \
1949 do { \
1950 mutex_lock(&hotkey_thread_data_mutex); \
1951 hotkey_config_change++; \
1952 } while (0);
1953 #define HOTKEY_CONFIG_CRITICAL_END \
1954 mutex_unlock(&hotkey_thread_data_mutex);
1956 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1958 #define hotkey_source_mask 0U
1959 #define HOTKEY_CONFIG_CRITICAL_START
1960 #define HOTKEY_CONFIG_CRITICAL_END
1962 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1964 static struct mutex hotkey_mutex;
1966 static enum { /* Reasons for waking up */
1967 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1968 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1969 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1970 } hotkey_wakeup_reason;
1972 static int hotkey_autosleep_ack;
1974 static u32 hotkey_orig_mask;
1975 static u32 hotkey_all_mask;
1976 static u32 hotkey_reserved_mask;
1977 static u32 hotkey_mask;
1979 static unsigned int hotkey_report_mode;
1981 static u16 *hotkey_keycode_map;
1983 static struct attribute_set *hotkey_dev_attributes;
1985 /* HKEY.MHKG() return bits */
1986 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1988 static int hotkey_get_wlsw(void)
1990 int status;
1992 if (!tp_features.hotkey_wlsw)
1993 return -ENODEV;
1995 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1996 if (dbg_wlswemul)
1997 return (tpacpi_wlsw_emulstate) ?
1998 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1999 #endif
2001 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2002 return -EIO;
2004 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2007 static int hotkey_get_tablet_mode(int *status)
2009 int s;
2011 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2012 return -EIO;
2014 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2015 return 0;
2019 * Call with hotkey_mutex held
2021 static int hotkey_mask_get(void)
2023 u32 m = 0;
2025 if (tp_features.hotkey_mask) {
2026 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2027 return -EIO;
2029 HOTKEY_CONFIG_CRITICAL_START
2030 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
2031 HOTKEY_CONFIG_CRITICAL_END
2033 return 0;
2037 * Call with hotkey_mutex held
2039 static int hotkey_mask_set(u32 mask)
2041 int i;
2042 int rc = 0;
2044 if (tp_features.hotkey_mask) {
2045 if (!tp_warned.hotkey_mask_ff &&
2046 (mask == 0xffff || mask == 0xffffff ||
2047 mask == 0xffffffff)) {
2048 tp_warned.hotkey_mask_ff = 1;
2049 printk(TPACPI_NOTICE
2050 "setting the hotkey mask to 0x%08x is likely "
2051 "not the best way to go about it\n", mask);
2052 printk(TPACPI_NOTICE
2053 "please consider using the driver defaults, "
2054 "and refer to up-to-date thinkpad-acpi "
2055 "documentation\n");
2058 HOTKEY_CONFIG_CRITICAL_START
2059 for (i = 0; i < 32; i++) {
2060 u32 m = 1 << i;
2061 /* enable in firmware mask only keys not in NVRAM
2062 * mode, but enable the key in the cached hotkey_mask
2063 * regardless of mode, or the key will end up
2064 * disabled by hotkey_mask_get() */
2065 if (!acpi_evalf(hkey_handle,
2066 NULL, "MHKM", "vdd", i + 1,
2067 !!((mask & ~hotkey_source_mask) & m))) {
2068 rc = -EIO;
2069 break;
2070 } else {
2071 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
2074 HOTKEY_CONFIG_CRITICAL_END
2076 /* hotkey_mask_get must be called unconditionally below */
2077 if (!hotkey_mask_get() && !rc &&
2078 (hotkey_mask & ~hotkey_source_mask) !=
2079 (mask & ~hotkey_source_mask)) {
2080 printk(TPACPI_NOTICE
2081 "requested hot key mask 0x%08x, but "
2082 "firmware forced it to 0x%08x\n",
2083 mask, hotkey_mask);
2085 } else {
2086 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2087 HOTKEY_CONFIG_CRITICAL_START
2088 hotkey_mask = mask & hotkey_source_mask;
2089 HOTKEY_CONFIG_CRITICAL_END
2090 hotkey_mask_get();
2091 if (hotkey_mask != mask) {
2092 printk(TPACPI_NOTICE
2093 "requested hot key mask 0x%08x, "
2094 "forced to 0x%08x (NVRAM poll mask is "
2095 "0x%08x): no firmware mask support\n",
2096 mask, hotkey_mask, hotkey_source_mask);
2098 #else
2099 hotkey_mask_get();
2100 rc = -ENXIO;
2101 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2104 return rc;
2107 static int hotkey_status_get(int *status)
2109 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2110 return -EIO;
2112 return 0;
2115 static int hotkey_status_set(bool enable)
2117 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2118 return -EIO;
2120 return 0;
2123 static void tpacpi_input_send_tabletsw(void)
2125 int state;
2127 if (tp_features.hotkey_tablet &&
2128 !hotkey_get_tablet_mode(&state)) {
2129 mutex_lock(&tpacpi_inputdev_send_mutex);
2131 input_report_switch(tpacpi_inputdev,
2132 SW_TABLET_MODE, !!state);
2133 input_sync(tpacpi_inputdev);
2135 mutex_unlock(&tpacpi_inputdev_send_mutex);
2139 static void tpacpi_input_send_key(unsigned int scancode)
2141 unsigned int keycode;
2143 keycode = hotkey_keycode_map[scancode];
2145 if (keycode != KEY_RESERVED) {
2146 mutex_lock(&tpacpi_inputdev_send_mutex);
2148 input_report_key(tpacpi_inputdev, keycode, 1);
2149 if (keycode == KEY_UNKNOWN)
2150 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2151 scancode);
2152 input_sync(tpacpi_inputdev);
2154 input_report_key(tpacpi_inputdev, keycode, 0);
2155 if (keycode == KEY_UNKNOWN)
2156 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2157 scancode);
2158 input_sync(tpacpi_inputdev);
2160 mutex_unlock(&tpacpi_inputdev_send_mutex);
2164 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2165 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2167 static void tpacpi_hotkey_send_key(unsigned int scancode)
2169 tpacpi_input_send_key(scancode);
2170 if (hotkey_report_mode < 2) {
2171 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2172 0x80, 0x1001 + scancode);
2176 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
2178 u8 d;
2180 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2181 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2182 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2183 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2184 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2185 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2187 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2188 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2189 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2191 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2192 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2193 n->displayexp_toggle =
2194 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2196 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2197 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2198 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2199 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2200 n->brightness_toggle =
2201 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2203 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2204 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2205 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2206 >> TP_NVRAM_POS_LEVEL_VOLUME;
2207 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2208 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2212 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2213 do { \
2214 if ((mask & (1 << __scancode)) && \
2215 oldn->__member != newn->__member) \
2216 tpacpi_hotkey_send_key(__scancode); \
2217 } while (0)
2219 #define TPACPI_MAY_SEND_KEY(__scancode) \
2220 do { if (mask & (1 << __scancode)) \
2221 tpacpi_hotkey_send_key(__scancode); } while (0)
2223 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2224 struct tp_nvram_state *newn,
2225 u32 mask)
2227 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2228 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2229 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2230 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2232 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2234 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2236 /* handle volume */
2237 if (oldn->volume_toggle != newn->volume_toggle) {
2238 if (oldn->mute != newn->mute) {
2239 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2241 if (oldn->volume_level > newn->volume_level) {
2242 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2243 } else if (oldn->volume_level < newn->volume_level) {
2244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2245 } else if (oldn->mute == newn->mute) {
2246 /* repeated key presses that didn't change state */
2247 if (newn->mute) {
2248 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2249 } else if (newn->volume_level != 0) {
2250 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2251 } else {
2252 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2257 /* handle brightness */
2258 if (oldn->brightness_toggle != newn->brightness_toggle) {
2259 if (oldn->brightness_level < newn->brightness_level) {
2260 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2261 } else if (oldn->brightness_level > newn->brightness_level) {
2262 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2263 } else {
2264 /* repeated key presses that didn't change state */
2265 if (newn->brightness_level != 0) {
2266 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2267 } else {
2268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2274 #undef TPACPI_COMPARE_KEY
2275 #undef TPACPI_MAY_SEND_KEY
2277 static int hotkey_kthread(void *data)
2279 struct tp_nvram_state s[2];
2280 u32 mask;
2281 unsigned int si, so;
2282 unsigned long t;
2283 unsigned int change_detector, must_reset;
2284 unsigned int poll_freq;
2286 mutex_lock(&hotkey_thread_mutex);
2288 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2289 goto exit;
2291 set_freezable();
2293 so = 0;
2294 si = 1;
2295 t = 0;
2297 /* Initial state for compares */
2298 mutex_lock(&hotkey_thread_data_mutex);
2299 change_detector = hotkey_config_change;
2300 mask = hotkey_source_mask & hotkey_mask;
2301 poll_freq = hotkey_poll_freq;
2302 mutex_unlock(&hotkey_thread_data_mutex);
2303 hotkey_read_nvram(&s[so], mask);
2305 while (!kthread_should_stop()) {
2306 if (t == 0) {
2307 if (likely(poll_freq))
2308 t = 1000/poll_freq;
2309 else
2310 t = 100; /* should never happen... */
2312 t = msleep_interruptible(t);
2313 if (unlikely(kthread_should_stop()))
2314 break;
2315 must_reset = try_to_freeze();
2316 if (t > 0 && !must_reset)
2317 continue;
2319 mutex_lock(&hotkey_thread_data_mutex);
2320 if (must_reset || hotkey_config_change != change_detector) {
2321 /* forget old state on thaw or config change */
2322 si = so;
2323 t = 0;
2324 change_detector = hotkey_config_change;
2326 mask = hotkey_source_mask & hotkey_mask;
2327 poll_freq = hotkey_poll_freq;
2328 mutex_unlock(&hotkey_thread_data_mutex);
2330 if (likely(mask)) {
2331 hotkey_read_nvram(&s[si], mask);
2332 if (likely(si != so)) {
2333 hotkey_compare_and_issue_event(&s[so], &s[si],
2334 mask);
2338 so = si;
2339 si ^= 1;
2342 exit:
2343 mutex_unlock(&hotkey_thread_mutex);
2344 return 0;
2347 /* call with hotkey_mutex held */
2348 static void hotkey_poll_stop_sync(void)
2350 if (tpacpi_hotkey_task) {
2351 if (frozen(tpacpi_hotkey_task) ||
2352 freezing(tpacpi_hotkey_task))
2353 thaw_process(tpacpi_hotkey_task);
2355 kthread_stop(tpacpi_hotkey_task);
2356 tpacpi_hotkey_task = NULL;
2357 mutex_lock(&hotkey_thread_mutex);
2358 /* at this point, the thread did exit */
2359 mutex_unlock(&hotkey_thread_mutex);
2363 /* call with hotkey_mutex held */
2364 static void hotkey_poll_setup(bool may_warn)
2366 u32 hotkeys_to_poll = hotkey_source_mask & hotkey_mask;
2368 if (hotkeys_to_poll != 0 && hotkey_poll_freq > 0 &&
2369 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2370 if (!tpacpi_hotkey_task) {
2371 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2372 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2373 if (IS_ERR(tpacpi_hotkey_task)) {
2374 tpacpi_hotkey_task = NULL;
2375 printk(TPACPI_ERR
2376 "could not create kernel thread "
2377 "for hotkey polling\n");
2380 } else {
2381 hotkey_poll_stop_sync();
2382 if (may_warn && hotkeys_to_poll != 0 &&
2383 hotkey_poll_freq == 0) {
2384 printk(TPACPI_NOTICE
2385 "hot keys 0x%08x require polling, "
2386 "which is currently disabled\n",
2387 hotkeys_to_poll);
2392 static void hotkey_poll_setup_safe(bool may_warn)
2394 mutex_lock(&hotkey_mutex);
2395 hotkey_poll_setup(may_warn);
2396 mutex_unlock(&hotkey_mutex);
2399 /* call with hotkey_mutex held */
2400 static void hotkey_poll_set_freq(unsigned int freq)
2402 if (!freq)
2403 hotkey_poll_stop_sync();
2405 HOTKEY_CONFIG_CRITICAL_START
2406 hotkey_poll_freq = freq;
2407 HOTKEY_CONFIG_CRITICAL_END
2410 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2412 static void hotkey_poll_setup_safe(bool __unused)
2416 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2418 static int hotkey_inputdev_open(struct input_dev *dev)
2420 switch (tpacpi_lifecycle) {
2421 case TPACPI_LIFE_INIT:
2423 * hotkey_init will call hotkey_poll_setup_safe
2424 * at the appropriate moment
2426 return 0;
2427 case TPACPI_LIFE_EXITING:
2428 return -EBUSY;
2429 case TPACPI_LIFE_RUNNING:
2430 hotkey_poll_setup_safe(false);
2431 return 0;
2434 /* Should only happen if tpacpi_lifecycle is corrupt */
2435 BUG();
2436 return -EBUSY;
2439 static void hotkey_inputdev_close(struct input_dev *dev)
2441 /* disable hotkey polling when possible */
2442 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2443 hotkey_poll_setup_safe(false);
2446 /* sysfs hotkey enable ------------------------------------------------- */
2447 static ssize_t hotkey_enable_show(struct device *dev,
2448 struct device_attribute *attr,
2449 char *buf)
2451 int res, status;
2453 printk_deprecated_attribute("hotkey_enable",
2454 "Hotkey reporting is always enabled");
2456 res = hotkey_status_get(&status);
2457 if (res)
2458 return res;
2460 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2463 static ssize_t hotkey_enable_store(struct device *dev,
2464 struct device_attribute *attr,
2465 const char *buf, size_t count)
2467 unsigned long t;
2469 printk_deprecated_attribute("hotkey_enable",
2470 "Hotkeys can be disabled through hotkey_mask");
2472 if (parse_strtoul(buf, 1, &t))
2473 return -EINVAL;
2475 if (t == 0)
2476 return -EPERM;
2478 return count;
2481 static struct device_attribute dev_attr_hotkey_enable =
2482 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2483 hotkey_enable_show, hotkey_enable_store);
2485 /* sysfs hotkey mask --------------------------------------------------- */
2486 static ssize_t hotkey_mask_show(struct device *dev,
2487 struct device_attribute *attr,
2488 char *buf)
2490 int res;
2492 if (mutex_lock_killable(&hotkey_mutex))
2493 return -ERESTARTSYS;
2494 res = hotkey_mask_get();
2495 mutex_unlock(&hotkey_mutex);
2497 return (res)?
2498 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2501 static ssize_t hotkey_mask_store(struct device *dev,
2502 struct device_attribute *attr,
2503 const char *buf, size_t count)
2505 unsigned long t;
2506 int res;
2508 if (parse_strtoul(buf, 0xffffffffUL, &t))
2509 return -EINVAL;
2511 if (mutex_lock_killable(&hotkey_mutex))
2512 return -ERESTARTSYS;
2514 res = hotkey_mask_set(t);
2516 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2517 hotkey_poll_setup(true);
2518 #endif
2520 mutex_unlock(&hotkey_mutex);
2522 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2524 return (res) ? res : count;
2527 static struct device_attribute dev_attr_hotkey_mask =
2528 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2529 hotkey_mask_show, hotkey_mask_store);
2531 /* sysfs hotkey bios_enabled ------------------------------------------- */
2532 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2533 struct device_attribute *attr,
2534 char *buf)
2536 return sprintf(buf, "0\n");
2539 static struct device_attribute dev_attr_hotkey_bios_enabled =
2540 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2542 /* sysfs hotkey bios_mask ---------------------------------------------- */
2543 static ssize_t hotkey_bios_mask_show(struct device *dev,
2544 struct device_attribute *attr,
2545 char *buf)
2547 printk_deprecated_attribute("hotkey_bios_mask",
2548 "This attribute is useless.");
2549 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2552 static struct device_attribute dev_attr_hotkey_bios_mask =
2553 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2555 /* sysfs hotkey all_mask ----------------------------------------------- */
2556 static ssize_t hotkey_all_mask_show(struct device *dev,
2557 struct device_attribute *attr,
2558 char *buf)
2560 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2561 hotkey_all_mask | hotkey_source_mask);
2564 static struct device_attribute dev_attr_hotkey_all_mask =
2565 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2567 /* sysfs hotkey recommended_mask --------------------------------------- */
2568 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2569 struct device_attribute *attr,
2570 char *buf)
2572 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2573 (hotkey_all_mask | hotkey_source_mask)
2574 & ~hotkey_reserved_mask);
2577 static struct device_attribute dev_attr_hotkey_recommended_mask =
2578 __ATTR(hotkey_recommended_mask, S_IRUGO,
2579 hotkey_recommended_mask_show, NULL);
2581 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2583 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2584 static ssize_t hotkey_source_mask_show(struct device *dev,
2585 struct device_attribute *attr,
2586 char *buf)
2588 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2591 static ssize_t hotkey_source_mask_store(struct device *dev,
2592 struct device_attribute *attr,
2593 const char *buf, size_t count)
2595 unsigned long t;
2597 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2598 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2599 return -EINVAL;
2601 if (mutex_lock_killable(&hotkey_mutex))
2602 return -ERESTARTSYS;
2604 HOTKEY_CONFIG_CRITICAL_START
2605 hotkey_source_mask = t;
2606 HOTKEY_CONFIG_CRITICAL_END
2608 hotkey_poll_setup(true);
2609 hotkey_mask_set(hotkey_mask);
2611 mutex_unlock(&hotkey_mutex);
2613 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2615 return count;
2618 static struct device_attribute dev_attr_hotkey_source_mask =
2619 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2620 hotkey_source_mask_show, hotkey_source_mask_store);
2622 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2623 static ssize_t hotkey_poll_freq_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
2627 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2630 static ssize_t hotkey_poll_freq_store(struct device *dev,
2631 struct device_attribute *attr,
2632 const char *buf, size_t count)
2634 unsigned long t;
2636 if (parse_strtoul(buf, 25, &t))
2637 return -EINVAL;
2639 if (mutex_lock_killable(&hotkey_mutex))
2640 return -ERESTARTSYS;
2642 hotkey_poll_set_freq(t);
2643 hotkey_poll_setup(true);
2645 mutex_unlock(&hotkey_mutex);
2647 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2649 return count;
2652 static struct device_attribute dev_attr_hotkey_poll_freq =
2653 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2654 hotkey_poll_freq_show, hotkey_poll_freq_store);
2656 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2658 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2659 static ssize_t hotkey_radio_sw_show(struct device *dev,
2660 struct device_attribute *attr,
2661 char *buf)
2663 int res;
2664 res = hotkey_get_wlsw();
2665 if (res < 0)
2666 return res;
2668 /* Opportunistic update */
2669 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2671 return snprintf(buf, PAGE_SIZE, "%d\n",
2672 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2675 static struct device_attribute dev_attr_hotkey_radio_sw =
2676 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2678 static void hotkey_radio_sw_notify_change(void)
2680 if (tp_features.hotkey_wlsw)
2681 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2682 "hotkey_radio_sw");
2685 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2686 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2687 struct device_attribute *attr,
2688 char *buf)
2690 int res, s;
2691 res = hotkey_get_tablet_mode(&s);
2692 if (res < 0)
2693 return res;
2695 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2698 static struct device_attribute dev_attr_hotkey_tablet_mode =
2699 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2701 static void hotkey_tablet_mode_notify_change(void)
2703 if (tp_features.hotkey_tablet)
2704 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2705 "hotkey_tablet_mode");
2708 /* sysfs hotkey report_mode -------------------------------------------- */
2709 static ssize_t hotkey_report_mode_show(struct device *dev,
2710 struct device_attribute *attr,
2711 char *buf)
2713 return snprintf(buf, PAGE_SIZE, "%d\n",
2714 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2717 static struct device_attribute dev_attr_hotkey_report_mode =
2718 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2720 /* sysfs wakeup reason (pollable) -------------------------------------- */
2721 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2722 struct device_attribute *attr,
2723 char *buf)
2725 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2728 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2729 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2731 static void hotkey_wakeup_reason_notify_change(void)
2733 if (tp_features.hotkey_mask)
2734 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2735 "wakeup_reason");
2738 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2739 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2743 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2746 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2747 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2748 hotkey_wakeup_hotunplug_complete_show, NULL);
2750 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2752 if (tp_features.hotkey_mask)
2753 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2754 "wakeup_hotunplug_complete");
2757 /* --------------------------------------------------------------------- */
2759 static struct attribute *hotkey_attributes[] __initdata = {
2760 &dev_attr_hotkey_enable.attr,
2761 &dev_attr_hotkey_bios_enabled.attr,
2762 &dev_attr_hotkey_report_mode.attr,
2763 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2764 &dev_attr_hotkey_mask.attr,
2765 &dev_attr_hotkey_all_mask.attr,
2766 &dev_attr_hotkey_recommended_mask.attr,
2767 &dev_attr_hotkey_source_mask.attr,
2768 &dev_attr_hotkey_poll_freq.attr,
2769 #endif
2772 static struct attribute *hotkey_mask_attributes[] __initdata = {
2773 &dev_attr_hotkey_bios_mask.attr,
2774 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2775 &dev_attr_hotkey_mask.attr,
2776 &dev_attr_hotkey_all_mask.attr,
2777 &dev_attr_hotkey_recommended_mask.attr,
2778 #endif
2779 &dev_attr_hotkey_wakeup_reason.attr,
2780 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2784 * Sync both the hw and sw blocking state of all switches
2786 static void tpacpi_send_radiosw_update(void)
2788 int wlsw;
2791 * We must sync all rfkill controllers *before* issuing any
2792 * rfkill input events, or we will race the rfkill core input
2793 * handler.
2795 * tpacpi_inputdev_send_mutex works as a syncronization point
2796 * for the above.
2798 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2801 wlsw = hotkey_get_wlsw();
2803 /* Sync hw blocking state first if it is hw-blocked */
2804 if (wlsw == TPACPI_RFK_RADIO_OFF)
2805 tpacpi_rfk_update_hwblock_state(true);
2807 /* Sync sw blocking state */
2808 tpacpi_rfk_update_swstate_all();
2810 /* Sync hw blocking state last if it is hw-unblocked */
2811 if (wlsw == TPACPI_RFK_RADIO_ON)
2812 tpacpi_rfk_update_hwblock_state(false);
2814 /* Issue rfkill input event for WLSW switch */
2815 if (!(wlsw < 0)) {
2816 mutex_lock(&tpacpi_inputdev_send_mutex);
2818 input_report_switch(tpacpi_inputdev,
2819 SW_RFKILL_ALL, (wlsw > 0));
2820 input_sync(tpacpi_inputdev);
2822 mutex_unlock(&tpacpi_inputdev_send_mutex);
2826 * this can be unconditional, as we will poll state again
2827 * if userspace uses the notify to read data
2829 hotkey_radio_sw_notify_change();
2832 static void hotkey_exit(void)
2834 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2835 mutex_lock(&hotkey_mutex);
2836 hotkey_poll_stop_sync();
2837 mutex_unlock(&hotkey_mutex);
2838 #endif
2840 if (hotkey_dev_attributes)
2841 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2843 kfree(hotkey_keycode_map);
2845 if (tp_features.hotkey) {
2846 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2847 "restoring original hot key mask\n");
2848 /* no short-circuit boolean operator below! */
2849 if ((hotkey_mask_set(hotkey_orig_mask) |
2850 hotkey_status_set(false)) != 0)
2851 printk(TPACPI_ERR
2852 "failed to restore hot key mask "
2853 "to BIOS defaults\n");
2857 static int __init hotkey_init(struct ibm_init_struct *iibm)
2859 /* Requirements for changing the default keymaps:
2861 * 1. Many of the keys are mapped to KEY_RESERVED for very
2862 * good reasons. Do not change them unless you have deep
2863 * knowledge on the IBM and Lenovo ThinkPad firmware for
2864 * the various ThinkPad models. The driver behaves
2865 * differently for KEY_RESERVED: such keys have their
2866 * hot key mask *unset* in mask_recommended, and also
2867 * in the initial hot key mask programmed into the
2868 * firmware at driver load time, which means the firm-
2869 * ware may react very differently if you change them to
2870 * something else;
2872 * 2. You must be subscribed to the linux-thinkpad and
2873 * ibm-acpi-devel mailing lists, and you should read the
2874 * list archives since 2007 if you want to change the
2875 * keymaps. This requirement exists so that you will
2876 * know the past history of problems with the thinkpad-
2877 * acpi driver keymaps, and also that you will be
2878 * listening to any bug reports;
2880 * 3. Do not send thinkpad-acpi specific patches directly to
2881 * for merging, *ever*. Send them to the linux-acpi
2882 * mailinglist for comments. Merging is to be done only
2883 * through acpi-test and the ACPI maintainer.
2885 * If the above is too much to ask, don't change the keymap.
2886 * Ask the thinkpad-acpi maintainer to do it, instead.
2888 static u16 ibm_keycode_map[] __initdata = {
2889 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2890 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2891 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2892 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2894 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2895 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2896 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2897 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2899 /* brightness: firmware always reacts to them, unless
2900 * X.org did some tricks in the radeon BIOS scratch
2901 * registers of *some* models */
2902 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2903 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2905 /* Thinklight: firmware always react to it */
2906 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2908 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2909 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2911 /* Volume: firmware always react to it and reprograms
2912 * the built-in *extra* mixer. Never map it to control
2913 * another mixer by default. */
2914 KEY_RESERVED, /* 0x14: VOLUME UP */
2915 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2916 KEY_RESERVED, /* 0x16: MUTE */
2918 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2920 /* (assignments unknown, please report if found) */
2921 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2922 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2924 static u16 lenovo_keycode_map[] __initdata = {
2925 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2926 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2927 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2928 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2930 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2931 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2932 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2933 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2935 /* These either have to go through ACPI video, or
2936 * act like in the IBM ThinkPads, so don't ever
2937 * enable them by default */
2938 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2939 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2941 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2943 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2944 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2946 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2947 * react to it and reprograms the built-in *extra* mixer.
2948 * Never map it to control another mixer by default.
2950 * T60?, T61, R60?, R61: firmware and EC tries to send
2951 * these over the regular keyboard, so these are no-ops,
2952 * but there are still weird bugs re. MUTE, so do not
2953 * change unless you get test reports from all Lenovo
2954 * models. May cause the BIOS to interfere with the
2955 * HDA mixer.
2957 KEY_RESERVED, /* 0x14: VOLUME UP */
2958 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2959 KEY_RESERVED, /* 0x16: MUTE */
2961 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2963 /* (assignments unknown, please report if found) */
2964 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2965 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2968 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2969 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2970 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2972 int res, i;
2973 int status;
2974 int hkeyv;
2976 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2977 "initializing hotkey subdriver\n");
2979 BUG_ON(!tpacpi_inputdev);
2980 BUG_ON(tpacpi_inputdev->open != NULL ||
2981 tpacpi_inputdev->close != NULL);
2983 TPACPI_ACPIHANDLE_INIT(hkey);
2984 mutex_init(&hotkey_mutex);
2986 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2987 mutex_init(&hotkey_thread_mutex);
2988 mutex_init(&hotkey_thread_data_mutex);
2989 #endif
2991 /* hotkey not supported on 570 */
2992 tp_features.hotkey = hkey_handle != NULL;
2994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2995 "hotkeys are %s\n",
2996 str_supported(tp_features.hotkey));
2998 if (!tp_features.hotkey)
2999 return 1;
3001 tpacpi_disable_brightness_delay();
3003 hotkey_dev_attributes = create_attr_set(13, NULL);
3004 if (!hotkey_dev_attributes)
3005 return -ENOMEM;
3006 res = add_many_to_attr_set(hotkey_dev_attributes,
3007 hotkey_attributes,
3008 ARRAY_SIZE(hotkey_attributes));
3009 if (res)
3010 goto err_exit;
3012 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3013 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3014 for HKEY interface version 0x100 */
3015 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3016 if ((hkeyv >> 8) != 1) {
3017 printk(TPACPI_ERR "unknown version of the "
3018 "HKEY interface: 0x%x\n", hkeyv);
3019 printk(TPACPI_ERR "please report this to %s\n",
3020 TPACPI_MAIL);
3021 } else {
3023 * MHKV 0x100 in A31, R40, R40e,
3024 * T4x, X31, and later
3026 tp_features.hotkey_mask = 1;
3027 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3028 "firmware HKEY interface version: 0x%x\n",
3029 hkeyv);
3033 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3034 "hotkey masks are %s\n",
3035 str_supported(tp_features.hotkey_mask));
3037 if (tp_features.hotkey_mask) {
3038 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3039 "MHKA", "qd")) {
3040 printk(TPACPI_ERR
3041 "missing MHKA handler, "
3042 "please report this to %s\n",
3043 TPACPI_MAIL);
3044 /* FN+F12, FN+F4, FN+F3 */
3045 hotkey_all_mask = 0x080cU;
3049 /* hotkey_source_mask *must* be zero for
3050 * the first hotkey_mask_get */
3051 if (tp_features.hotkey_mask) {
3052 res = hotkey_mask_get();
3053 if (res)
3054 goto err_exit;
3056 hotkey_orig_mask = hotkey_mask;
3057 res = add_many_to_attr_set(
3058 hotkey_dev_attributes,
3059 hotkey_mask_attributes,
3060 ARRAY_SIZE(hotkey_mask_attributes));
3061 if (res)
3062 goto err_exit;
3065 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3066 if (dbg_wlswemul) {
3067 tp_features.hotkey_wlsw = 1;
3068 printk(TPACPI_INFO
3069 "radio switch emulation enabled\n");
3070 } else
3071 #endif
3072 /* Not all thinkpads have a hardware radio switch */
3073 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3074 tp_features.hotkey_wlsw = 1;
3075 printk(TPACPI_INFO
3076 "radio switch found; radios are %s\n",
3077 enabled(status, 0));
3079 if (tp_features.hotkey_wlsw)
3080 res = add_to_attr_set(hotkey_dev_attributes,
3081 &dev_attr_hotkey_radio_sw.attr);
3083 /* For X41t, X60t, X61t Tablets... */
3084 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3085 tp_features.hotkey_tablet = 1;
3086 printk(TPACPI_INFO
3087 "possible tablet mode switch found; "
3088 "ThinkPad in %s mode\n",
3089 (status & TP_HOTKEY_TABLET_MASK)?
3090 "tablet" : "laptop");
3091 res = add_to_attr_set(hotkey_dev_attributes,
3092 &dev_attr_hotkey_tablet_mode.attr);
3095 if (!res)
3096 res = register_attr_set_with_sysfs(
3097 hotkey_dev_attributes,
3098 &tpacpi_pdev->dev.kobj);
3099 if (res)
3100 goto err_exit;
3102 /* Set up key map */
3104 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3105 GFP_KERNEL);
3106 if (!hotkey_keycode_map) {
3107 printk(TPACPI_ERR
3108 "failed to allocate memory for key map\n");
3109 res = -ENOMEM;
3110 goto err_exit;
3113 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3114 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3115 "using Lenovo default hot key map\n");
3116 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3117 TPACPI_HOTKEY_MAP_SIZE);
3118 } else {
3119 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3120 "using IBM default hot key map\n");
3121 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3122 TPACPI_HOTKEY_MAP_SIZE);
3125 set_bit(EV_KEY, tpacpi_inputdev->evbit);
3126 set_bit(EV_MSC, tpacpi_inputdev->evbit);
3127 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
3128 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3129 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3130 tpacpi_inputdev->keycode = hotkey_keycode_map;
3131 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3132 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3133 set_bit(hotkey_keycode_map[i],
3134 tpacpi_inputdev->keybit);
3135 } else {
3136 if (i < sizeof(hotkey_reserved_mask)*8)
3137 hotkey_reserved_mask |= 1 << i;
3141 if (tp_features.hotkey_wlsw) {
3142 set_bit(EV_SW, tpacpi_inputdev->evbit);
3143 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
3145 if (tp_features.hotkey_tablet) {
3146 set_bit(EV_SW, tpacpi_inputdev->evbit);
3147 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
3150 /* Do not issue duplicate brightness change events to
3151 * userspace */
3152 if (!tp_features.bright_acpimode)
3153 /* update bright_acpimode... */
3154 tpacpi_check_std_acpi_brightness_support();
3156 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3157 printk(TPACPI_INFO
3158 "This ThinkPad has standard ACPI backlight "
3159 "brightness control, supported by the ACPI "
3160 "video driver\n");
3161 printk(TPACPI_NOTICE
3162 "Disabling thinkpad-acpi brightness events "
3163 "by default...\n");
3165 /* The hotkey_reserved_mask change below is not
3166 * necessary while the keys are at KEY_RESERVED in the
3167 * default map, but better safe than sorry, leave it
3168 * here as a marker of what we have to do, especially
3169 * when we finally become able to set this at runtime
3170 * on response to X.org requests */
3171 hotkey_reserved_mask |=
3172 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3173 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3176 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3177 if (tp_features.hotkey_mask) {
3178 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3179 & ~hotkey_all_mask
3180 & ~hotkey_reserved_mask;
3181 } else {
3182 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3183 & ~hotkey_reserved_mask;
3186 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3187 "hotkey source mask 0x%08x, polling freq %u\n",
3188 hotkey_source_mask, hotkey_poll_freq);
3189 #endif
3191 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3192 "enabling firmware HKEY event interface...\n");
3193 res = hotkey_status_set(true);
3194 if (res) {
3195 hotkey_exit();
3196 return res;
3198 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
3199 & ~hotkey_reserved_mask)
3200 | hotkey_orig_mask);
3201 if (res < 0 && res != -ENXIO) {
3202 hotkey_exit();
3203 return res;
3206 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "legacy ibm/hotkey event reporting over procfs %s\n",
3208 (hotkey_report_mode < 2) ?
3209 "enabled" : "disabled");
3211 tpacpi_inputdev->open = &hotkey_inputdev_open;
3212 tpacpi_inputdev->close = &hotkey_inputdev_close;
3214 hotkey_poll_setup_safe(true);
3215 tpacpi_send_radiosw_update();
3216 tpacpi_input_send_tabletsw();
3218 return 0;
3220 err_exit:
3221 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3222 hotkey_dev_attributes = NULL;
3224 return (res < 0)? res : 1;
3227 static bool hotkey_notify_hotkey(const u32 hkey,
3228 bool *send_acpi_ev,
3229 bool *ignore_acpi_ev)
3231 /* 0x1000-0x1FFF: key presses */
3232 unsigned int scancode = hkey & 0xfff;
3233 *send_acpi_ev = true;
3234 *ignore_acpi_ev = false;
3236 if (scancode > 0 && scancode < 0x21) {
3237 scancode--;
3238 if (!(hotkey_source_mask & (1 << scancode))) {
3239 tpacpi_input_send_key(scancode);
3240 *send_acpi_ev = false;
3241 } else {
3242 *ignore_acpi_ev = true;
3244 return true;
3246 return false;
3249 static bool hotkey_notify_wakeup(const u32 hkey,
3250 bool *send_acpi_ev,
3251 bool *ignore_acpi_ev)
3253 /* 0x2000-0x2FFF: Wakeup reason */
3254 *send_acpi_ev = true;
3255 *ignore_acpi_ev = false;
3257 switch (hkey) {
3258 case 0x2304: /* suspend, undock */
3259 case 0x2404: /* hibernation, undock */
3260 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3261 *ignore_acpi_ev = true;
3262 break;
3264 case 0x2305: /* suspend, bay eject */
3265 case 0x2405: /* hibernation, bay eject */
3266 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3267 *ignore_acpi_ev = true;
3268 break;
3270 case 0x2313: /* Battery on critical low level (S3) */
3271 case 0x2413: /* Battery on critical low level (S4) */
3272 printk(TPACPI_ALERT
3273 "EMERGENCY WAKEUP: battery almost empty\n");
3274 /* how to auto-heal: */
3275 /* 2313: woke up from S3, go to S4/S5 */
3276 /* 2413: woke up from S4, go to S5 */
3277 break;
3279 default:
3280 return false;
3283 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3284 printk(TPACPI_INFO
3285 "woke up due to a hot-unplug "
3286 "request...\n");
3287 hotkey_wakeup_reason_notify_change();
3289 return true;
3292 static bool hotkey_notify_usrevent(const u32 hkey,
3293 bool *send_acpi_ev,
3294 bool *ignore_acpi_ev)
3296 /* 0x5000-0x5FFF: human interface helpers */
3297 *send_acpi_ev = true;
3298 *ignore_acpi_ev = false;
3300 switch (hkey) {
3301 case 0x5010: /* Lenovo new BIOS: brightness changed */
3302 case 0x500b: /* X61t: tablet pen inserted into bay */
3303 case 0x500c: /* X61t: tablet pen removed from bay */
3304 return true;
3306 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3307 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3308 tpacpi_input_send_tabletsw();
3309 hotkey_tablet_mode_notify_change();
3310 *send_acpi_ev = false;
3311 return true;
3313 case 0x5001:
3314 case 0x5002:
3315 /* LID switch events. Do not propagate */
3316 *ignore_acpi_ev = true;
3317 return true;
3319 default:
3320 return false;
3324 static bool hotkey_notify_thermal(const u32 hkey,
3325 bool *send_acpi_ev,
3326 bool *ignore_acpi_ev)
3328 /* 0x6000-0x6FFF: thermal alarms */
3329 *send_acpi_ev = true;
3330 *ignore_acpi_ev = false;
3332 switch (hkey) {
3333 case 0x6011:
3334 printk(TPACPI_CRIT
3335 "THERMAL ALARM: battery is too hot!\n");
3336 /* recommended action: warn user through gui */
3337 return true;
3338 case 0x6012:
3339 printk(TPACPI_ALERT
3340 "THERMAL EMERGENCY: battery is extremely hot!\n");
3341 /* recommended action: immediate sleep/hibernate */
3342 return true;
3343 case 0x6021:
3344 printk(TPACPI_CRIT
3345 "THERMAL ALARM: "
3346 "a sensor reports something is too hot!\n");
3347 /* recommended action: warn user through gui, that */
3348 /* some internal component is too hot */
3349 return true;
3350 case 0x6022:
3351 printk(TPACPI_ALERT
3352 "THERMAL EMERGENCY: "
3353 "a sensor reports something is extremely hot!\n");
3354 /* recommended action: immediate sleep/hibernate */
3355 return true;
3356 case 0x6030:
3357 printk(TPACPI_INFO
3358 "EC reports that Thermal Table has changed\n");
3359 /* recommended action: do nothing, we don't have
3360 * Lenovo ATM information */
3361 return true;
3362 default:
3363 printk(TPACPI_ALERT
3364 "THERMAL ALERT: unknown thermal alarm received\n");
3365 return false;
3369 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3371 u32 hkey;
3372 bool send_acpi_ev;
3373 bool ignore_acpi_ev;
3374 bool known_ev;
3376 if (event != 0x80) {
3377 printk(TPACPI_ERR
3378 "unknown HKEY notification event %d\n", event);
3379 /* forward it to userspace, maybe it knows how to handle it */
3380 acpi_bus_generate_netlink_event(
3381 ibm->acpi->device->pnp.device_class,
3382 dev_name(&ibm->acpi->device->dev),
3383 event, 0);
3384 return;
3387 while (1) {
3388 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3389 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3390 return;
3393 if (hkey == 0) {
3394 /* queue empty */
3395 return;
3398 send_acpi_ev = true;
3399 ignore_acpi_ev = false;
3401 switch (hkey >> 12) {
3402 case 1:
3403 /* 0x1000-0x1FFF: key presses */
3404 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3405 &ignore_acpi_ev);
3406 break;
3407 case 2:
3408 /* 0x2000-0x2FFF: Wakeup reason */
3409 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3410 &ignore_acpi_ev);
3411 break;
3412 case 3:
3413 /* 0x3000-0x3FFF: bay-related wakeups */
3414 if (hkey == 0x3003) {
3415 hotkey_autosleep_ack = 1;
3416 printk(TPACPI_INFO
3417 "bay ejected\n");
3418 hotkey_wakeup_hotunplug_complete_notify_change();
3419 known_ev = true;
3420 } else {
3421 known_ev = false;
3423 break;
3424 case 4:
3425 /* 0x4000-0x4FFF: dock-related wakeups */
3426 if (hkey == 0x4003) {
3427 hotkey_autosleep_ack = 1;
3428 printk(TPACPI_INFO
3429 "undocked\n");
3430 hotkey_wakeup_hotunplug_complete_notify_change();
3431 known_ev = true;
3432 } else {
3433 known_ev = false;
3435 break;
3436 case 5:
3437 /* 0x5000-0x5FFF: human interface helpers */
3438 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3439 &ignore_acpi_ev);
3440 break;
3441 case 6:
3442 /* 0x6000-0x6FFF: thermal alarms */
3443 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3444 &ignore_acpi_ev);
3445 break;
3446 case 7:
3447 /* 0x7000-0x7FFF: misc */
3448 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3449 tpacpi_send_radiosw_update();
3450 send_acpi_ev = 0;
3451 known_ev = true;
3452 break;
3454 /* fallthrough to default */
3455 default:
3456 known_ev = false;
3458 if (!known_ev) {
3459 printk(TPACPI_NOTICE
3460 "unhandled HKEY event 0x%04x\n", hkey);
3461 printk(TPACPI_NOTICE
3462 "please report the conditions when this "
3463 "event happened to %s\n", TPACPI_MAIL);
3466 /* Legacy events */
3467 if (!ignore_acpi_ev &&
3468 (send_acpi_ev || hotkey_report_mode < 2)) {
3469 acpi_bus_generate_proc_event(ibm->acpi->device,
3470 event, hkey);
3473 /* netlink events */
3474 if (!ignore_acpi_ev && send_acpi_ev) {
3475 acpi_bus_generate_netlink_event(
3476 ibm->acpi->device->pnp.device_class,
3477 dev_name(&ibm->acpi->device->dev),
3478 event, hkey);
3483 static void hotkey_suspend(pm_message_t state)
3485 /* Do these on suspend, we get the events on early resume! */
3486 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3487 hotkey_autosleep_ack = 0;
3490 static void hotkey_resume(void)
3492 tpacpi_disable_brightness_delay();
3494 if (hotkey_mask_get())
3495 printk(TPACPI_ERR
3496 "error while trying to read hot key mask "
3497 "from firmware\n");
3498 tpacpi_send_radiosw_update();
3499 hotkey_tablet_mode_notify_change();
3500 hotkey_wakeup_reason_notify_change();
3501 hotkey_wakeup_hotunplug_complete_notify_change();
3502 hotkey_poll_setup_safe(false);
3505 /* procfs -------------------------------------------------------------- */
3506 static int hotkey_read(char *p)
3508 int res, status;
3509 int len = 0;
3511 if (!tp_features.hotkey) {
3512 len += sprintf(p + len, "status:\t\tnot supported\n");
3513 return len;
3516 if (mutex_lock_killable(&hotkey_mutex))
3517 return -ERESTARTSYS;
3518 res = hotkey_status_get(&status);
3519 if (!res)
3520 res = hotkey_mask_get();
3521 mutex_unlock(&hotkey_mutex);
3522 if (res)
3523 return res;
3525 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3526 if (tp_features.hotkey_mask) {
3527 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3528 len += sprintf(p + len,
3529 "commands:\tenable, disable, reset, <mask>\n");
3530 } else {
3531 len += sprintf(p + len, "mask:\t\tnot supported\n");
3532 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3535 return len;
3538 static void hotkey_enabledisable_warn(bool enable)
3540 tpacpi_log_usertask("procfs hotkey enable/disable");
3541 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3542 TPACPI_WARN
3543 "hotkey enable/disable functionality has been "
3544 "removed from the driver. Hotkeys are always "
3545 "enabled\n"))
3546 printk(TPACPI_ERR
3547 "Please remove the hotkey=enable module "
3548 "parameter, it is deprecated. Hotkeys are always "
3549 "enabled\n");
3552 static int hotkey_write(char *buf)
3554 int res;
3555 u32 mask;
3556 char *cmd;
3558 if (!tp_features.hotkey)
3559 return -ENODEV;
3561 if (mutex_lock_killable(&hotkey_mutex))
3562 return -ERESTARTSYS;
3564 mask = hotkey_mask;
3566 res = 0;
3567 while ((cmd = next_cmd(&buf))) {
3568 if (strlencmp(cmd, "enable") == 0) {
3569 hotkey_enabledisable_warn(1);
3570 } else if (strlencmp(cmd, "disable") == 0) {
3571 hotkey_enabledisable_warn(0);
3572 res = -EPERM;
3573 } else if (strlencmp(cmd, "reset") == 0) {
3574 mask = (hotkey_all_mask | hotkey_source_mask)
3575 & ~hotkey_reserved_mask;
3576 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3577 /* mask set */
3578 } else if (sscanf(cmd, "%x", &mask) == 1) {
3579 /* mask set */
3580 } else {
3581 res = -EINVAL;
3582 goto errexit;
3586 if (!res)
3587 tpacpi_disclose_usertask("procfs hotkey",
3588 "set mask to 0x%08x\n", mask);
3590 if (!res && mask != hotkey_mask)
3591 res = hotkey_mask_set(mask);
3593 errexit:
3594 mutex_unlock(&hotkey_mutex);
3595 return res;
3598 static const struct acpi_device_id ibm_htk_device_ids[] = {
3599 {TPACPI_ACPI_HKEY_HID, 0},
3600 {"", 0},
3603 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3604 .hid = ibm_htk_device_ids,
3605 .notify = hotkey_notify,
3606 .handle = &hkey_handle,
3607 .type = ACPI_DEVICE_NOTIFY,
3610 static struct ibm_struct hotkey_driver_data = {
3611 .name = "hotkey",
3612 .read = hotkey_read,
3613 .write = hotkey_write,
3614 .exit = hotkey_exit,
3615 .resume = hotkey_resume,
3616 .suspend = hotkey_suspend,
3617 .acpi = &ibm_hotkey_acpidriver,
3620 /*************************************************************************
3621 * Bluetooth subdriver
3624 enum {
3625 /* ACPI GBDC/SBDC bits */
3626 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3627 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3628 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3629 off / last state */
3632 enum {
3633 /* ACPI \BLTH commands */
3634 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3635 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3636 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3637 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3638 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3641 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3643 static void bluetooth_suspend(pm_message_t state)
3645 /* Try to make sure radio will resume powered off */
3646 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3647 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3648 vdbg_printk(TPACPI_DBG_RFKILL,
3649 "bluetooth power down on resume request failed\n");
3652 static int bluetooth_get_status(void)
3654 int status;
3656 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3657 if (dbg_bluetoothemul)
3658 return (tpacpi_bluetooth_emulstate) ?
3659 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3660 #endif
3662 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3663 return -EIO;
3665 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3666 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3669 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3671 int status;
3673 vdbg_printk(TPACPI_DBG_RFKILL,
3674 "will attempt to %s bluetooth\n",
3675 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3677 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3678 if (dbg_bluetoothemul) {
3679 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3680 return 0;
3682 #endif
3684 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3685 if (state == TPACPI_RFK_RADIO_ON)
3686 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3687 else
3688 status = 0;
3690 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3691 return -EIO;
3693 return 0;
3696 /* sysfs bluetooth enable ---------------------------------------------- */
3697 static ssize_t bluetooth_enable_show(struct device *dev,
3698 struct device_attribute *attr,
3699 char *buf)
3701 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3702 attr, buf);
3705 static ssize_t bluetooth_enable_store(struct device *dev,
3706 struct device_attribute *attr,
3707 const char *buf, size_t count)
3709 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3710 attr, buf, count);
3713 static struct device_attribute dev_attr_bluetooth_enable =
3714 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3715 bluetooth_enable_show, bluetooth_enable_store);
3717 /* --------------------------------------------------------------------- */
3719 static struct attribute *bluetooth_attributes[] = {
3720 &dev_attr_bluetooth_enable.attr,
3721 NULL
3724 static const struct attribute_group bluetooth_attr_group = {
3725 .attrs = bluetooth_attributes,
3728 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3729 .get_status = bluetooth_get_status,
3730 .set_status = bluetooth_set_status,
3733 static void bluetooth_shutdown(void)
3735 /* Order firmware to save current state to NVRAM */
3736 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3737 TP_ACPI_BLTH_SAVE_STATE))
3738 printk(TPACPI_NOTICE
3739 "failed to save bluetooth state to NVRAM\n");
3740 else
3741 vdbg_printk(TPACPI_DBG_RFKILL,
3742 "bluestooth state saved to NVRAM\n");
3745 static void bluetooth_exit(void)
3747 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3748 &bluetooth_attr_group);
3750 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3752 bluetooth_shutdown();
3755 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3757 int res;
3758 int status = 0;
3760 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3761 "initializing bluetooth subdriver\n");
3763 TPACPI_ACPIHANDLE_INIT(hkey);
3765 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3766 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3767 tp_features.bluetooth = hkey_handle &&
3768 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3770 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3771 "bluetooth is %s, status 0x%02x\n",
3772 str_supported(tp_features.bluetooth),
3773 status);
3775 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3776 if (dbg_bluetoothemul) {
3777 tp_features.bluetooth = 1;
3778 printk(TPACPI_INFO
3779 "bluetooth switch emulation enabled\n");
3780 } else
3781 #endif
3782 if (tp_features.bluetooth &&
3783 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3784 /* no bluetooth hardware present in system */
3785 tp_features.bluetooth = 0;
3786 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3787 "bluetooth hardware not installed\n");
3790 if (!tp_features.bluetooth)
3791 return 1;
3793 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3794 &bluetooth_tprfk_ops,
3795 RFKILL_TYPE_BLUETOOTH,
3796 TPACPI_RFK_BLUETOOTH_SW_NAME,
3797 true);
3798 if (res)
3799 return res;
3801 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3802 &bluetooth_attr_group);
3803 if (res) {
3804 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3805 return res;
3808 return 0;
3811 /* procfs -------------------------------------------------------------- */
3812 static int bluetooth_read(char *p)
3814 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
3817 static int bluetooth_write(char *buf)
3819 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
3822 static struct ibm_struct bluetooth_driver_data = {
3823 .name = "bluetooth",
3824 .read = bluetooth_read,
3825 .write = bluetooth_write,
3826 .exit = bluetooth_exit,
3827 .suspend = bluetooth_suspend,
3828 .shutdown = bluetooth_shutdown,
3831 /*************************************************************************
3832 * Wan subdriver
3835 enum {
3836 /* ACPI GWAN/SWAN bits */
3837 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3838 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3839 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3840 off / last state */
3843 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3845 static void wan_suspend(pm_message_t state)
3847 /* Try to make sure radio will resume powered off */
3848 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3849 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3850 vdbg_printk(TPACPI_DBG_RFKILL,
3851 "WWAN power down on resume request failed\n");
3854 static int wan_get_status(void)
3856 int status;
3858 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3859 if (dbg_wwanemul)
3860 return (tpacpi_wwan_emulstate) ?
3861 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3862 #endif
3864 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3865 return -EIO;
3867 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3868 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3871 static int wan_set_status(enum tpacpi_rfkill_state state)
3873 int status;
3875 vdbg_printk(TPACPI_DBG_RFKILL,
3876 "will attempt to %s wwan\n",
3877 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3879 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3880 if (dbg_wwanemul) {
3881 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
3882 return 0;
3884 #endif
3886 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3887 if (state == TPACPI_RFK_RADIO_ON)
3888 status = TP_ACPI_WANCARD_RADIOSSW;
3889 else
3890 status = 0;
3892 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3893 return -EIO;
3895 return 0;
3898 /* sysfs wan enable ---------------------------------------------------- */
3899 static ssize_t wan_enable_show(struct device *dev,
3900 struct device_attribute *attr,
3901 char *buf)
3903 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3904 attr, buf);
3907 static ssize_t wan_enable_store(struct device *dev,
3908 struct device_attribute *attr,
3909 const char *buf, size_t count)
3911 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3912 attr, buf, count);
3915 static struct device_attribute dev_attr_wan_enable =
3916 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3917 wan_enable_show, wan_enable_store);
3919 /* --------------------------------------------------------------------- */
3921 static struct attribute *wan_attributes[] = {
3922 &dev_attr_wan_enable.attr,
3923 NULL
3926 static const struct attribute_group wan_attr_group = {
3927 .attrs = wan_attributes,
3930 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3931 .get_status = wan_get_status,
3932 .set_status = wan_set_status,
3935 static void wan_shutdown(void)
3937 /* Order firmware to save current state to NVRAM */
3938 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3939 TP_ACPI_WGSV_SAVE_STATE))
3940 printk(TPACPI_NOTICE
3941 "failed to save WWAN state to NVRAM\n");
3942 else
3943 vdbg_printk(TPACPI_DBG_RFKILL,
3944 "WWAN state saved to NVRAM\n");
3947 static void wan_exit(void)
3949 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3950 &wan_attr_group);
3952 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3954 wan_shutdown();
3957 static int __init wan_init(struct ibm_init_struct *iibm)
3959 int res;
3960 int status = 0;
3962 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3963 "initializing wan subdriver\n");
3965 TPACPI_ACPIHANDLE_INIT(hkey);
3967 tp_features.wan = hkey_handle &&
3968 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3970 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3971 "wan is %s, status 0x%02x\n",
3972 str_supported(tp_features.wan),
3973 status);
3975 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3976 if (dbg_wwanemul) {
3977 tp_features.wan = 1;
3978 printk(TPACPI_INFO
3979 "wwan switch emulation enabled\n");
3980 } else
3981 #endif
3982 if (tp_features.wan &&
3983 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3984 /* no wan hardware present in system */
3985 tp_features.wan = 0;
3986 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3987 "wan hardware not installed\n");
3990 if (!tp_features.wan)
3991 return 1;
3993 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3994 &wan_tprfk_ops,
3995 RFKILL_TYPE_WWAN,
3996 TPACPI_RFK_WWAN_SW_NAME,
3997 true);
3998 if (res)
3999 return res;
4001 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4002 &wan_attr_group);
4004 if (res) {
4005 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4006 return res;
4009 return 0;
4012 /* procfs -------------------------------------------------------------- */
4013 static int wan_read(char *p)
4015 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
4018 static int wan_write(char *buf)
4020 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4023 static struct ibm_struct wan_driver_data = {
4024 .name = "wan",
4025 .read = wan_read,
4026 .write = wan_write,
4027 .exit = wan_exit,
4028 .suspend = wan_suspend,
4029 .shutdown = wan_shutdown,
4032 /*************************************************************************
4033 * UWB subdriver
4036 enum {
4037 /* ACPI GUWB/SUWB bits */
4038 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4039 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4042 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4044 static int uwb_get_status(void)
4046 int status;
4048 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4049 if (dbg_uwbemul)
4050 return (tpacpi_uwb_emulstate) ?
4051 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4052 #endif
4054 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4055 return -EIO;
4057 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4058 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4061 static int uwb_set_status(enum tpacpi_rfkill_state state)
4063 int status;
4065 vdbg_printk(TPACPI_DBG_RFKILL,
4066 "will attempt to %s UWB\n",
4067 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4069 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4070 if (dbg_uwbemul) {
4071 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4072 return 0;
4074 #endif
4076 if (state == TPACPI_RFK_RADIO_ON)
4077 status = TP_ACPI_UWB_RADIOSSW;
4078 else
4079 status = 0;
4081 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4082 return -EIO;
4084 return 0;
4087 /* --------------------------------------------------------------------- */
4089 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4090 .get_status = uwb_get_status,
4091 .set_status = uwb_set_status,
4094 static void uwb_exit(void)
4096 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4099 static int __init uwb_init(struct ibm_init_struct *iibm)
4101 int res;
4102 int status = 0;
4104 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4105 "initializing uwb subdriver\n");
4107 TPACPI_ACPIHANDLE_INIT(hkey);
4109 tp_features.uwb = hkey_handle &&
4110 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4112 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4113 "uwb is %s, status 0x%02x\n",
4114 str_supported(tp_features.uwb),
4115 status);
4117 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4118 if (dbg_uwbemul) {
4119 tp_features.uwb = 1;
4120 printk(TPACPI_INFO
4121 "uwb switch emulation enabled\n");
4122 } else
4123 #endif
4124 if (tp_features.uwb &&
4125 !(status & TP_ACPI_UWB_HWPRESENT)) {
4126 /* no uwb hardware present in system */
4127 tp_features.uwb = 0;
4128 dbg_printk(TPACPI_DBG_INIT,
4129 "uwb hardware not installed\n");
4132 if (!tp_features.uwb)
4133 return 1;
4135 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4136 &uwb_tprfk_ops,
4137 RFKILL_TYPE_UWB,
4138 TPACPI_RFK_UWB_SW_NAME,
4139 false);
4140 return res;
4143 static struct ibm_struct uwb_driver_data = {
4144 .name = "uwb",
4145 .exit = uwb_exit,
4146 .flags.experimental = 1,
4149 /*************************************************************************
4150 * Video subdriver
4153 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4155 enum video_access_mode {
4156 TPACPI_VIDEO_NONE = 0,
4157 TPACPI_VIDEO_570, /* 570 */
4158 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4159 TPACPI_VIDEO_NEW, /* all others */
4162 enum { /* video status flags, based on VIDEO_570 */
4163 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4164 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4165 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4168 enum { /* TPACPI_VIDEO_570 constants */
4169 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4170 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4171 * video_status_flags */
4172 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4173 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4176 static enum video_access_mode video_supported;
4177 static int video_orig_autosw;
4179 static int video_autosw_get(void);
4180 static int video_autosw_set(int enable);
4182 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4184 static int __init video_init(struct ibm_init_struct *iibm)
4186 int ivga;
4188 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4190 TPACPI_ACPIHANDLE_INIT(vid);
4191 TPACPI_ACPIHANDLE_INIT(vid2);
4193 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4194 /* G41, assume IVGA doesn't change */
4195 vid_handle = vid2_handle;
4197 if (!vid_handle)
4198 /* video switching not supported on R30, R31 */
4199 video_supported = TPACPI_VIDEO_NONE;
4200 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4201 /* 570 */
4202 video_supported = TPACPI_VIDEO_570;
4203 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4204 /* 600e/x, 770e, 770x */
4205 video_supported = TPACPI_VIDEO_770;
4206 else
4207 /* all others */
4208 video_supported = TPACPI_VIDEO_NEW;
4210 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4211 str_supported(video_supported != TPACPI_VIDEO_NONE),
4212 video_supported);
4214 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4217 static void video_exit(void)
4219 dbg_printk(TPACPI_DBG_EXIT,
4220 "restoring original video autoswitch mode\n");
4221 if (video_autosw_set(video_orig_autosw))
4222 printk(TPACPI_ERR "error while trying to restore original "
4223 "video autoswitch mode\n");
4226 static int video_outputsw_get(void)
4228 int status = 0;
4229 int i;
4231 switch (video_supported) {
4232 case TPACPI_VIDEO_570:
4233 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4234 TP_ACPI_VIDEO_570_PHSCMD))
4235 return -EIO;
4236 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4237 break;
4238 case TPACPI_VIDEO_770:
4239 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4240 return -EIO;
4241 if (i)
4242 status |= TP_ACPI_VIDEO_S_LCD;
4243 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4244 return -EIO;
4245 if (i)
4246 status |= TP_ACPI_VIDEO_S_CRT;
4247 break;
4248 case TPACPI_VIDEO_NEW:
4249 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4250 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4251 return -EIO;
4252 if (i)
4253 status |= TP_ACPI_VIDEO_S_CRT;
4255 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4256 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4257 return -EIO;
4258 if (i)
4259 status |= TP_ACPI_VIDEO_S_LCD;
4260 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4261 return -EIO;
4262 if (i)
4263 status |= TP_ACPI_VIDEO_S_DVI;
4264 break;
4265 default:
4266 return -ENOSYS;
4269 return status;
4272 static int video_outputsw_set(int status)
4274 int autosw;
4275 int res = 0;
4277 switch (video_supported) {
4278 case TPACPI_VIDEO_570:
4279 res = acpi_evalf(NULL, NULL,
4280 "\\_SB.PHS2", "vdd",
4281 TP_ACPI_VIDEO_570_PHS2CMD,
4282 status | TP_ACPI_VIDEO_570_PHS2SET);
4283 break;
4284 case TPACPI_VIDEO_770:
4285 autosw = video_autosw_get();
4286 if (autosw < 0)
4287 return autosw;
4289 res = video_autosw_set(1);
4290 if (res)
4291 return res;
4292 res = acpi_evalf(vid_handle, NULL,
4293 "ASWT", "vdd", status * 0x100, 0);
4294 if (!autosw && video_autosw_set(autosw)) {
4295 printk(TPACPI_ERR
4296 "video auto-switch left enabled due to error\n");
4297 return -EIO;
4299 break;
4300 case TPACPI_VIDEO_NEW:
4301 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4302 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4303 break;
4304 default:
4305 return -ENOSYS;
4308 return (res)? 0 : -EIO;
4311 static int video_autosw_get(void)
4313 int autosw = 0;
4315 switch (video_supported) {
4316 case TPACPI_VIDEO_570:
4317 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4318 return -EIO;
4319 break;
4320 case TPACPI_VIDEO_770:
4321 case TPACPI_VIDEO_NEW:
4322 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4323 return -EIO;
4324 break;
4325 default:
4326 return -ENOSYS;
4329 return autosw & 1;
4332 static int video_autosw_set(int enable)
4334 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4335 return -EIO;
4336 return 0;
4339 static int video_outputsw_cycle(void)
4341 int autosw = video_autosw_get();
4342 int res;
4344 if (autosw < 0)
4345 return autosw;
4347 switch (video_supported) {
4348 case TPACPI_VIDEO_570:
4349 res = video_autosw_set(1);
4350 if (res)
4351 return res;
4352 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4353 break;
4354 case TPACPI_VIDEO_770:
4355 case TPACPI_VIDEO_NEW:
4356 res = video_autosw_set(1);
4357 if (res)
4358 return res;
4359 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4360 break;
4361 default:
4362 return -ENOSYS;
4364 if (!autosw && video_autosw_set(autosw)) {
4365 printk(TPACPI_ERR
4366 "video auto-switch left enabled due to error\n");
4367 return -EIO;
4370 return (res)? 0 : -EIO;
4373 static int video_expand_toggle(void)
4375 switch (video_supported) {
4376 case TPACPI_VIDEO_570:
4377 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4378 0 : -EIO;
4379 case TPACPI_VIDEO_770:
4380 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4381 0 : -EIO;
4382 case TPACPI_VIDEO_NEW:
4383 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4384 0 : -EIO;
4385 default:
4386 return -ENOSYS;
4388 /* not reached */
4391 static int video_read(char *p)
4393 int status, autosw;
4394 int len = 0;
4396 if (video_supported == TPACPI_VIDEO_NONE) {
4397 len += sprintf(p + len, "status:\t\tnot supported\n");
4398 return len;
4401 status = video_outputsw_get();
4402 if (status < 0)
4403 return status;
4405 autosw = video_autosw_get();
4406 if (autosw < 0)
4407 return autosw;
4409 len += sprintf(p + len, "status:\t\tsupported\n");
4410 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4411 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4412 if (video_supported == TPACPI_VIDEO_NEW)
4413 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4414 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4415 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4416 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4417 if (video_supported == TPACPI_VIDEO_NEW)
4418 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4419 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4420 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4422 return len;
4425 static int video_write(char *buf)
4427 char *cmd;
4428 int enable, disable, status;
4429 int res;
4431 if (video_supported == TPACPI_VIDEO_NONE)
4432 return -ENODEV;
4434 enable = 0;
4435 disable = 0;
4437 while ((cmd = next_cmd(&buf))) {
4438 if (strlencmp(cmd, "lcd_enable") == 0) {
4439 enable |= TP_ACPI_VIDEO_S_LCD;
4440 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4441 disable |= TP_ACPI_VIDEO_S_LCD;
4442 } else if (strlencmp(cmd, "crt_enable") == 0) {
4443 enable |= TP_ACPI_VIDEO_S_CRT;
4444 } else if (strlencmp(cmd, "crt_disable") == 0) {
4445 disable |= TP_ACPI_VIDEO_S_CRT;
4446 } else if (video_supported == TPACPI_VIDEO_NEW &&
4447 strlencmp(cmd, "dvi_enable") == 0) {
4448 enable |= TP_ACPI_VIDEO_S_DVI;
4449 } else if (video_supported == TPACPI_VIDEO_NEW &&
4450 strlencmp(cmd, "dvi_disable") == 0) {
4451 disable |= TP_ACPI_VIDEO_S_DVI;
4452 } else if (strlencmp(cmd, "auto_enable") == 0) {
4453 res = video_autosw_set(1);
4454 if (res)
4455 return res;
4456 } else if (strlencmp(cmd, "auto_disable") == 0) {
4457 res = video_autosw_set(0);
4458 if (res)
4459 return res;
4460 } else if (strlencmp(cmd, "video_switch") == 0) {
4461 res = video_outputsw_cycle();
4462 if (res)
4463 return res;
4464 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4465 res = video_expand_toggle();
4466 if (res)
4467 return res;
4468 } else
4469 return -EINVAL;
4472 if (enable || disable) {
4473 status = video_outputsw_get();
4474 if (status < 0)
4475 return status;
4476 res = video_outputsw_set((status & ~disable) | enable);
4477 if (res)
4478 return res;
4481 return 0;
4484 static struct ibm_struct video_driver_data = {
4485 .name = "video",
4486 .read = video_read,
4487 .write = video_write,
4488 .exit = video_exit,
4491 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4493 /*************************************************************************
4494 * Light (thinklight) subdriver
4497 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4498 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4500 static int light_get_status(void)
4502 int status = 0;
4504 if (tp_features.light_status) {
4505 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4506 return -EIO;
4507 return (!!status);
4510 return -ENXIO;
4513 static int light_set_status(int status)
4515 int rc;
4517 if (tp_features.light) {
4518 if (cmos_handle) {
4519 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4520 (status)?
4521 TP_CMOS_THINKLIGHT_ON :
4522 TP_CMOS_THINKLIGHT_OFF);
4523 } else {
4524 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4525 (status)? 1 : 0);
4527 return (rc)? 0 : -EIO;
4530 return -ENXIO;
4533 static void light_set_status_worker(struct work_struct *work)
4535 struct tpacpi_led_classdev *data =
4536 container_of(work, struct tpacpi_led_classdev, work);
4538 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4539 light_set_status((data->new_state != TPACPI_LED_OFF));
4542 static void light_sysfs_set(struct led_classdev *led_cdev,
4543 enum led_brightness brightness)
4545 struct tpacpi_led_classdev *data =
4546 container_of(led_cdev,
4547 struct tpacpi_led_classdev,
4548 led_classdev);
4549 data->new_state = (brightness != LED_OFF) ?
4550 TPACPI_LED_ON : TPACPI_LED_OFF;
4551 queue_work(tpacpi_wq, &data->work);
4554 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4556 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4559 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4560 .led_classdev = {
4561 .name = "tpacpi::thinklight",
4562 .brightness_set = &light_sysfs_set,
4563 .brightness_get = &light_sysfs_get,
4567 static int __init light_init(struct ibm_init_struct *iibm)
4569 int rc;
4571 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4573 TPACPI_ACPIHANDLE_INIT(ledb);
4574 TPACPI_ACPIHANDLE_INIT(lght);
4575 TPACPI_ACPIHANDLE_INIT(cmos);
4576 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4578 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4579 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4581 if (tp_features.light)
4582 /* light status not supported on
4583 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4584 tp_features.light_status =
4585 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4587 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4588 str_supported(tp_features.light),
4589 str_supported(tp_features.light_status));
4591 if (!tp_features.light)
4592 return 1;
4594 rc = led_classdev_register(&tpacpi_pdev->dev,
4595 &tpacpi_led_thinklight.led_classdev);
4597 if (rc < 0) {
4598 tp_features.light = 0;
4599 tp_features.light_status = 0;
4600 } else {
4601 rc = 0;
4604 return rc;
4607 static void light_exit(void)
4609 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4610 if (work_pending(&tpacpi_led_thinklight.work))
4611 flush_workqueue(tpacpi_wq);
4614 static int light_read(char *p)
4616 int len = 0;
4617 int status;
4619 if (!tp_features.light) {
4620 len += sprintf(p + len, "status:\t\tnot supported\n");
4621 } else if (!tp_features.light_status) {
4622 len += sprintf(p + len, "status:\t\tunknown\n");
4623 len += sprintf(p + len, "commands:\ton, off\n");
4624 } else {
4625 status = light_get_status();
4626 if (status < 0)
4627 return status;
4628 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4629 len += sprintf(p + len, "commands:\ton, off\n");
4632 return len;
4635 static int light_write(char *buf)
4637 char *cmd;
4638 int newstatus = 0;
4640 if (!tp_features.light)
4641 return -ENODEV;
4643 while ((cmd = next_cmd(&buf))) {
4644 if (strlencmp(cmd, "on") == 0) {
4645 newstatus = 1;
4646 } else if (strlencmp(cmd, "off") == 0) {
4647 newstatus = 0;
4648 } else
4649 return -EINVAL;
4652 return light_set_status(newstatus);
4655 static struct ibm_struct light_driver_data = {
4656 .name = "light",
4657 .read = light_read,
4658 .write = light_write,
4659 .exit = light_exit,
4662 /*************************************************************************
4663 * CMOS subdriver
4666 /* sysfs cmos_command -------------------------------------------------- */
4667 static ssize_t cmos_command_store(struct device *dev,
4668 struct device_attribute *attr,
4669 const char *buf, size_t count)
4671 unsigned long cmos_cmd;
4672 int res;
4674 if (parse_strtoul(buf, 21, &cmos_cmd))
4675 return -EINVAL;
4677 res = issue_thinkpad_cmos_command(cmos_cmd);
4678 return (res)? res : count;
4681 static struct device_attribute dev_attr_cmos_command =
4682 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4684 /* --------------------------------------------------------------------- */
4686 static int __init cmos_init(struct ibm_init_struct *iibm)
4688 int res;
4690 vdbg_printk(TPACPI_DBG_INIT,
4691 "initializing cmos commands subdriver\n");
4693 TPACPI_ACPIHANDLE_INIT(cmos);
4695 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4696 str_supported(cmos_handle != NULL));
4698 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4699 if (res)
4700 return res;
4702 return (cmos_handle)? 0 : 1;
4705 static void cmos_exit(void)
4707 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4710 static int cmos_read(char *p)
4712 int len = 0;
4714 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4715 R30, R31, T20-22, X20-21 */
4716 if (!cmos_handle)
4717 len += sprintf(p + len, "status:\t\tnot supported\n");
4718 else {
4719 len += sprintf(p + len, "status:\t\tsupported\n");
4720 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4723 return len;
4726 static int cmos_write(char *buf)
4728 char *cmd;
4729 int cmos_cmd, res;
4731 while ((cmd = next_cmd(&buf))) {
4732 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4733 cmos_cmd >= 0 && cmos_cmd <= 21) {
4734 /* cmos_cmd set */
4735 } else
4736 return -EINVAL;
4738 res = issue_thinkpad_cmos_command(cmos_cmd);
4739 if (res)
4740 return res;
4743 return 0;
4746 static struct ibm_struct cmos_driver_data = {
4747 .name = "cmos",
4748 .read = cmos_read,
4749 .write = cmos_write,
4750 .exit = cmos_exit,
4753 /*************************************************************************
4754 * LED subdriver
4757 enum led_access_mode {
4758 TPACPI_LED_NONE = 0,
4759 TPACPI_LED_570, /* 570 */
4760 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4761 TPACPI_LED_NEW, /* all others */
4764 enum { /* For TPACPI_LED_OLD */
4765 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4766 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4767 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4770 static enum led_access_mode led_supported;
4772 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4773 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4774 /* T20-22, X20-21 */
4775 "LED", /* all others */
4776 ); /* R30, R31 */
4778 #define TPACPI_LED_NUMLEDS 16
4779 static struct tpacpi_led_classdev *tpacpi_leds;
4780 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4781 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4782 /* there's a limit of 19 chars + NULL before 2.6.26 */
4783 "tpacpi::power",
4784 "tpacpi:orange:batt",
4785 "tpacpi:green:batt",
4786 "tpacpi::dock_active",
4787 "tpacpi::bay_active",
4788 "tpacpi::dock_batt",
4789 "tpacpi::unknown_led",
4790 "tpacpi::standby",
4791 "tpacpi::dock_status1",
4792 "tpacpi::dock_status2",
4793 "tpacpi::unknown_led2",
4794 "tpacpi::unknown_led3",
4795 "tpacpi::thinkvantage",
4797 #define TPACPI_SAFE_LEDS 0x1081U
4799 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4801 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4802 return false;
4803 #else
4804 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4805 #endif
4808 static int led_get_status(const unsigned int led)
4810 int status;
4811 enum led_status_t led_s;
4813 switch (led_supported) {
4814 case TPACPI_LED_570:
4815 if (!acpi_evalf(ec_handle,
4816 &status, "GLED", "dd", 1 << led))
4817 return -EIO;
4818 led_s = (status == 0)?
4819 TPACPI_LED_OFF :
4820 ((status == 1)?
4821 TPACPI_LED_ON :
4822 TPACPI_LED_BLINK);
4823 tpacpi_led_state_cache[led] = led_s;
4824 return led_s;
4825 default:
4826 return -ENXIO;
4829 /* not reached */
4832 static int led_set_status(const unsigned int led,
4833 const enum led_status_t ledstatus)
4835 /* off, on, blink. Index is led_status_t */
4836 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4837 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4839 int rc = 0;
4841 switch (led_supported) {
4842 case TPACPI_LED_570:
4843 /* 570 */
4844 if (unlikely(led > 7))
4845 return -EINVAL;
4846 if (unlikely(tpacpi_is_led_restricted(led)))
4847 return -EPERM;
4848 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4849 (1 << led), led_sled_arg1[ledstatus]))
4850 rc = -EIO;
4851 break;
4852 case TPACPI_LED_OLD:
4853 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4854 if (unlikely(led > 7))
4855 return -EINVAL;
4856 if (unlikely(tpacpi_is_led_restricted(led)))
4857 return -EPERM;
4858 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4859 if (rc >= 0)
4860 rc = ec_write(TPACPI_LED_EC_HLBL,
4861 (ledstatus == TPACPI_LED_BLINK) << led);
4862 if (rc >= 0)
4863 rc = ec_write(TPACPI_LED_EC_HLCL,
4864 (ledstatus != TPACPI_LED_OFF) << led);
4865 break;
4866 case TPACPI_LED_NEW:
4867 /* all others */
4868 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4869 return -EINVAL;
4870 if (unlikely(tpacpi_is_led_restricted(led)))
4871 return -EPERM;
4872 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4873 led, led_led_arg1[ledstatus]))
4874 rc = -EIO;
4875 break;
4876 default:
4877 rc = -ENXIO;
4880 if (!rc)
4881 tpacpi_led_state_cache[led] = ledstatus;
4883 return rc;
4886 static void led_set_status_worker(struct work_struct *work)
4888 struct tpacpi_led_classdev *data =
4889 container_of(work, struct tpacpi_led_classdev, work);
4891 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4892 led_set_status(data->led, data->new_state);
4895 static void led_sysfs_set(struct led_classdev *led_cdev,
4896 enum led_brightness brightness)
4898 struct tpacpi_led_classdev *data = container_of(led_cdev,
4899 struct tpacpi_led_classdev, led_classdev);
4901 if (brightness == LED_OFF)
4902 data->new_state = TPACPI_LED_OFF;
4903 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4904 data->new_state = TPACPI_LED_ON;
4905 else
4906 data->new_state = TPACPI_LED_BLINK;
4908 queue_work(tpacpi_wq, &data->work);
4911 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4912 unsigned long *delay_on, unsigned long *delay_off)
4914 struct tpacpi_led_classdev *data = container_of(led_cdev,
4915 struct tpacpi_led_classdev, led_classdev);
4917 /* Can we choose the flash rate? */
4918 if (*delay_on == 0 && *delay_off == 0) {
4919 /* yes. set them to the hardware blink rate (1 Hz) */
4920 *delay_on = 500; /* ms */
4921 *delay_off = 500; /* ms */
4922 } else if ((*delay_on != 500) || (*delay_off != 500))
4923 return -EINVAL;
4925 data->new_state = TPACPI_LED_BLINK;
4926 queue_work(tpacpi_wq, &data->work);
4928 return 0;
4931 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4933 int rc;
4935 struct tpacpi_led_classdev *data = container_of(led_cdev,
4936 struct tpacpi_led_classdev, led_classdev);
4938 rc = led_get_status(data->led);
4940 if (rc == TPACPI_LED_OFF || rc < 0)
4941 rc = LED_OFF; /* no error handling in led class :( */
4942 else
4943 rc = LED_FULL;
4945 return rc;
4948 static void led_exit(void)
4950 unsigned int i;
4952 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4953 if (tpacpi_leds[i].led_classdev.name)
4954 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4957 kfree(tpacpi_leds);
4960 static int __init tpacpi_init_led(unsigned int led)
4962 int rc;
4964 tpacpi_leds[led].led = led;
4966 /* LEDs with no name don't get registered */
4967 if (!tpacpi_led_names[led])
4968 return 0;
4970 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4971 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4972 if (led_supported == TPACPI_LED_570)
4973 tpacpi_leds[led].led_classdev.brightness_get =
4974 &led_sysfs_get;
4976 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4978 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4980 rc = led_classdev_register(&tpacpi_pdev->dev,
4981 &tpacpi_leds[led].led_classdev);
4982 if (rc < 0)
4983 tpacpi_leds[led].led_classdev.name = NULL;
4985 return rc;
4988 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4989 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4990 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4991 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4993 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4994 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4995 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4996 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4997 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4998 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4999 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5000 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5002 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5003 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5004 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5005 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5006 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5008 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5009 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5010 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5011 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5013 /* (1) - may have excess leds enabled on MSB */
5015 /* Defaults (order matters, keep last, don't reorder!) */
5016 { /* Lenovo */
5017 .vendor = PCI_VENDOR_ID_LENOVO,
5018 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5019 .quirks = 0x1fffU,
5021 { /* IBM ThinkPads with no EC version string */
5022 .vendor = PCI_VENDOR_ID_IBM,
5023 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5024 .quirks = 0x00ffU,
5026 { /* IBM ThinkPads with EC version string */
5027 .vendor = PCI_VENDOR_ID_IBM,
5028 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5029 .quirks = 0x00bfU,
5033 #undef TPACPI_LEDQ_IBM
5034 #undef TPACPI_LEDQ_LNV
5036 static int __init led_init(struct ibm_init_struct *iibm)
5038 unsigned int i;
5039 int rc;
5040 unsigned long useful_leds;
5042 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5044 TPACPI_ACPIHANDLE_INIT(led);
5046 if (!led_handle)
5047 /* led not supported on R30, R31 */
5048 led_supported = TPACPI_LED_NONE;
5049 else if (strlencmp(led_path, "SLED") == 0)
5050 /* 570 */
5051 led_supported = TPACPI_LED_570;
5052 else if (strlencmp(led_path, "SYSL") == 0)
5053 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5054 led_supported = TPACPI_LED_OLD;
5055 else
5056 /* all others */
5057 led_supported = TPACPI_LED_NEW;
5059 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5060 str_supported(led_supported), led_supported);
5062 if (led_supported == TPACPI_LED_NONE)
5063 return 1;
5065 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5066 GFP_KERNEL);
5067 if (!tpacpi_leds) {
5068 printk(TPACPI_ERR "Out of memory for LED data\n");
5069 return -ENOMEM;
5072 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5073 ARRAY_SIZE(led_useful_qtable));
5075 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5076 if (!tpacpi_is_led_restricted(i) &&
5077 test_bit(i, &useful_leds)) {
5078 rc = tpacpi_init_led(i);
5079 if (rc < 0) {
5080 led_exit();
5081 return rc;
5086 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5087 printk(TPACPI_NOTICE
5088 "warning: userspace override of important "
5089 "firmware LEDs is enabled\n");
5090 #endif
5091 return 0;
5094 #define str_led_status(s) \
5095 ((s) == TPACPI_LED_OFF ? "off" : \
5096 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5098 static int led_read(char *p)
5100 int len = 0;
5102 if (!led_supported) {
5103 len += sprintf(p + len, "status:\t\tnot supported\n");
5104 return len;
5106 len += sprintf(p + len, "status:\t\tsupported\n");
5108 if (led_supported == TPACPI_LED_570) {
5109 /* 570 */
5110 int i, status;
5111 for (i = 0; i < 8; i++) {
5112 status = led_get_status(i);
5113 if (status < 0)
5114 return -EIO;
5115 len += sprintf(p + len, "%d:\t\t%s\n",
5116 i, str_led_status(status));
5120 len += sprintf(p + len, "commands:\t"
5121 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5123 return len;
5126 static int led_write(char *buf)
5128 char *cmd;
5129 int led, rc;
5130 enum led_status_t s;
5132 if (!led_supported)
5133 return -ENODEV;
5135 while ((cmd = next_cmd(&buf))) {
5136 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5137 return -EINVAL;
5139 if (strstr(cmd, "off")) {
5140 s = TPACPI_LED_OFF;
5141 } else if (strstr(cmd, "on")) {
5142 s = TPACPI_LED_ON;
5143 } else if (strstr(cmd, "blink")) {
5144 s = TPACPI_LED_BLINK;
5145 } else {
5146 return -EINVAL;
5149 rc = led_set_status(led, s);
5150 if (rc < 0)
5151 return rc;
5154 return 0;
5157 static struct ibm_struct led_driver_data = {
5158 .name = "led",
5159 .read = led_read,
5160 .write = led_write,
5161 .exit = led_exit,
5164 /*************************************************************************
5165 * Beep subdriver
5168 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5170 #define TPACPI_BEEP_Q1 0x0001
5172 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5173 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5174 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5177 static int __init beep_init(struct ibm_init_struct *iibm)
5179 unsigned long quirks;
5181 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5183 TPACPI_ACPIHANDLE_INIT(beep);
5185 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5186 str_supported(beep_handle != NULL));
5188 quirks = tpacpi_check_quirks(beep_quirk_table,
5189 ARRAY_SIZE(beep_quirk_table));
5191 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5193 return (beep_handle)? 0 : 1;
5196 static int beep_read(char *p)
5198 int len = 0;
5200 if (!beep_handle)
5201 len += sprintf(p + len, "status:\t\tnot supported\n");
5202 else {
5203 len += sprintf(p + len, "status:\t\tsupported\n");
5204 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5207 return len;
5210 static int beep_write(char *buf)
5212 char *cmd;
5213 int beep_cmd;
5215 if (!beep_handle)
5216 return -ENODEV;
5218 while ((cmd = next_cmd(&buf))) {
5219 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5220 beep_cmd >= 0 && beep_cmd <= 17) {
5221 /* beep_cmd set */
5222 } else
5223 return -EINVAL;
5224 if (tp_features.beep_needs_two_args) {
5225 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5226 beep_cmd, 0))
5227 return -EIO;
5228 } else {
5229 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5230 beep_cmd))
5231 return -EIO;
5235 return 0;
5238 static struct ibm_struct beep_driver_data = {
5239 .name = "beep",
5240 .read = beep_read,
5241 .write = beep_write,
5244 /*************************************************************************
5245 * Thermal subdriver
5248 enum thermal_access_mode {
5249 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5250 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5251 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5252 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5253 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5256 enum { /* TPACPI_THERMAL_TPEC_* */
5257 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5258 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5259 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5262 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5263 struct ibm_thermal_sensors_struct {
5264 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5267 static enum thermal_access_mode thermal_read_mode;
5269 /* idx is zero-based */
5270 static int thermal_get_sensor(int idx, s32 *value)
5272 int t;
5273 s8 tmp;
5274 char tmpi[5];
5276 t = TP_EC_THERMAL_TMP0;
5278 switch (thermal_read_mode) {
5279 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5280 case TPACPI_THERMAL_TPEC_16:
5281 if (idx >= 8 && idx <= 15) {
5282 t = TP_EC_THERMAL_TMP8;
5283 idx -= 8;
5285 /* fallthrough */
5286 #endif
5287 case TPACPI_THERMAL_TPEC_8:
5288 if (idx <= 7) {
5289 if (!acpi_ec_read(t + idx, &tmp))
5290 return -EIO;
5291 *value = tmp * 1000;
5292 return 0;
5294 break;
5296 case TPACPI_THERMAL_ACPI_UPDT:
5297 if (idx <= 7) {
5298 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5299 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5300 return -EIO;
5301 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5302 return -EIO;
5303 *value = (t - 2732) * 100;
5304 return 0;
5306 break;
5308 case TPACPI_THERMAL_ACPI_TMP07:
5309 if (idx <= 7) {
5310 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5311 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5312 return -EIO;
5313 if (t > 127 || t < -127)
5314 t = TP_EC_THERMAL_TMP_NA;
5315 *value = t * 1000;
5316 return 0;
5318 break;
5320 case TPACPI_THERMAL_NONE:
5321 default:
5322 return -ENOSYS;
5325 return -EINVAL;
5328 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5330 int res, i;
5331 int n;
5333 n = 8;
5334 i = 0;
5336 if (!s)
5337 return -EINVAL;
5339 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5340 n = 16;
5342 for (i = 0 ; i < n; i++) {
5343 res = thermal_get_sensor(i, &s->temp[i]);
5344 if (res)
5345 return res;
5348 return n;
5351 /* sysfs temp##_input -------------------------------------------------- */
5353 static ssize_t thermal_temp_input_show(struct device *dev,
5354 struct device_attribute *attr,
5355 char *buf)
5357 struct sensor_device_attribute *sensor_attr =
5358 to_sensor_dev_attr(attr);
5359 int idx = sensor_attr->index;
5360 s32 value;
5361 int res;
5363 res = thermal_get_sensor(idx, &value);
5364 if (res)
5365 return res;
5366 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5367 return -ENXIO;
5369 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5372 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5373 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5374 thermal_temp_input_show, NULL, _idxB)
5376 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5377 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5378 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5379 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5380 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5381 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5382 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5383 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5384 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5385 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5386 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5387 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5388 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5389 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5390 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5391 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5392 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5395 #define THERMAL_ATTRS(X) \
5396 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5398 static struct attribute *thermal_temp_input_attr[] = {
5399 THERMAL_ATTRS(8),
5400 THERMAL_ATTRS(9),
5401 THERMAL_ATTRS(10),
5402 THERMAL_ATTRS(11),
5403 THERMAL_ATTRS(12),
5404 THERMAL_ATTRS(13),
5405 THERMAL_ATTRS(14),
5406 THERMAL_ATTRS(15),
5407 THERMAL_ATTRS(0),
5408 THERMAL_ATTRS(1),
5409 THERMAL_ATTRS(2),
5410 THERMAL_ATTRS(3),
5411 THERMAL_ATTRS(4),
5412 THERMAL_ATTRS(5),
5413 THERMAL_ATTRS(6),
5414 THERMAL_ATTRS(7),
5415 NULL
5418 static const struct attribute_group thermal_temp_input16_group = {
5419 .attrs = thermal_temp_input_attr
5422 static const struct attribute_group thermal_temp_input8_group = {
5423 .attrs = &thermal_temp_input_attr[8]
5426 #undef THERMAL_SENSOR_ATTR_TEMP
5427 #undef THERMAL_ATTRS
5429 /* --------------------------------------------------------------------- */
5431 static int __init thermal_init(struct ibm_init_struct *iibm)
5433 u8 t, ta1, ta2;
5434 int i;
5435 int acpi_tmp7;
5436 int res;
5438 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5440 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5442 if (thinkpad_id.ec_model) {
5444 * Direct EC access mode: sensors at registers
5445 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5446 * non-implemented, thermal sensors return 0x80 when
5447 * not available
5450 ta1 = ta2 = 0;
5451 for (i = 0; i < 8; i++) {
5452 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5453 ta1 |= t;
5454 } else {
5455 ta1 = 0;
5456 break;
5458 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5459 ta2 |= t;
5460 } else {
5461 ta1 = 0;
5462 break;
5465 if (ta1 == 0) {
5466 /* This is sheer paranoia, but we handle it anyway */
5467 if (acpi_tmp7) {
5468 printk(TPACPI_ERR
5469 "ThinkPad ACPI EC access misbehaving, "
5470 "falling back to ACPI TMPx access "
5471 "mode\n");
5472 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5473 } else {
5474 printk(TPACPI_ERR
5475 "ThinkPad ACPI EC access misbehaving, "
5476 "disabling thermal sensors access\n");
5477 thermal_read_mode = TPACPI_THERMAL_NONE;
5479 } else {
5480 thermal_read_mode =
5481 (ta2 != 0) ?
5482 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5484 } else if (acpi_tmp7) {
5485 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5486 /* 600e/x, 770e, 770x */
5487 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5488 } else {
5489 /* Standard ACPI TMPx access, max 8 sensors */
5490 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5492 } else {
5493 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5494 thermal_read_mode = TPACPI_THERMAL_NONE;
5497 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5498 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5499 thermal_read_mode);
5501 switch (thermal_read_mode) {
5502 case TPACPI_THERMAL_TPEC_16:
5503 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5504 &thermal_temp_input16_group);
5505 if (res)
5506 return res;
5507 break;
5508 case TPACPI_THERMAL_TPEC_8:
5509 case TPACPI_THERMAL_ACPI_TMP07:
5510 case TPACPI_THERMAL_ACPI_UPDT:
5511 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5512 &thermal_temp_input8_group);
5513 if (res)
5514 return res;
5515 break;
5516 case TPACPI_THERMAL_NONE:
5517 default:
5518 return 1;
5521 return 0;
5524 static void thermal_exit(void)
5526 switch (thermal_read_mode) {
5527 case TPACPI_THERMAL_TPEC_16:
5528 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5529 &thermal_temp_input16_group);
5530 break;
5531 case TPACPI_THERMAL_TPEC_8:
5532 case TPACPI_THERMAL_ACPI_TMP07:
5533 case TPACPI_THERMAL_ACPI_UPDT:
5534 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5535 &thermal_temp_input16_group);
5536 break;
5537 case TPACPI_THERMAL_NONE:
5538 default:
5539 break;
5543 static int thermal_read(char *p)
5545 int len = 0;
5546 int n, i;
5547 struct ibm_thermal_sensors_struct t;
5549 n = thermal_get_sensors(&t);
5550 if (unlikely(n < 0))
5551 return n;
5553 len += sprintf(p + len, "temperatures:\t");
5555 if (n > 0) {
5556 for (i = 0; i < (n - 1); i++)
5557 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5558 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5559 } else
5560 len += sprintf(p + len, "not supported\n");
5562 return len;
5565 static struct ibm_struct thermal_driver_data = {
5566 .name = "thermal",
5567 .read = thermal_read,
5568 .exit = thermal_exit,
5571 /*************************************************************************
5572 * EC Dump subdriver
5575 static u8 ecdump_regs[256];
5577 static int ecdump_read(char *p)
5579 int len = 0;
5580 int i, j;
5581 u8 v;
5583 len += sprintf(p + len, "EC "
5584 " +00 +01 +02 +03 +04 +05 +06 +07"
5585 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5586 for (i = 0; i < 256; i += 16) {
5587 len += sprintf(p + len, "EC 0x%02x:", i);
5588 for (j = 0; j < 16; j++) {
5589 if (!acpi_ec_read(i + j, &v))
5590 break;
5591 if (v != ecdump_regs[i + j])
5592 len += sprintf(p + len, " *%02x", v);
5593 else
5594 len += sprintf(p + len, " %02x", v);
5595 ecdump_regs[i + j] = v;
5597 len += sprintf(p + len, "\n");
5598 if (j != 16)
5599 break;
5602 /* These are way too dangerous to advertise openly... */
5603 #if 0
5604 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5605 " (<offset> is 00-ff, <value> is 00-ff)\n");
5606 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5607 " (<offset> is 00-ff, <value> is 0-255)\n");
5608 #endif
5609 return len;
5612 static int ecdump_write(char *buf)
5614 char *cmd;
5615 int i, v;
5617 while ((cmd = next_cmd(&buf))) {
5618 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5619 /* i and v set */
5620 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5621 /* i and v set */
5622 } else
5623 return -EINVAL;
5624 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5625 if (!acpi_ec_write(i, v))
5626 return -EIO;
5627 } else
5628 return -EINVAL;
5631 return 0;
5634 static struct ibm_struct ecdump_driver_data = {
5635 .name = "ecdump",
5636 .read = ecdump_read,
5637 .write = ecdump_write,
5638 .flags.experimental = 1,
5641 /*************************************************************************
5642 * Backlight/brightness subdriver
5645 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5648 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5649 * CMOS NVRAM byte 0x5E, bits 0-3.
5651 * EC HBRV (0x31) has the following layout
5652 * Bit 7: unknown function
5653 * Bit 6: unknown function
5654 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5655 * Bit 4: must be set to zero to avoid problems
5656 * Bit 3-0: backlight brightness level
5658 * brightness_get_raw returns status data in the HBRV layout
5660 * WARNING: The X61 has been verified to use HBRV for something else, so
5661 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5662 * testing on the very early *60 Lenovo models...
5665 enum {
5666 TP_EC_BACKLIGHT = 0x31,
5668 /* TP_EC_BACKLIGHT bitmasks */
5669 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5670 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5671 TP_EC_BACKLIGHT_MAPSW = 0x20,
5674 enum tpacpi_brightness_access_mode {
5675 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5676 TPACPI_BRGHT_MODE_EC, /* EC control */
5677 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5678 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5679 TPACPI_BRGHT_MODE_MAX
5682 static struct backlight_device *ibm_backlight_device;
5684 static enum tpacpi_brightness_access_mode brightness_mode =
5685 TPACPI_BRGHT_MODE_MAX;
5687 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5689 static struct mutex brightness_mutex;
5691 /* NVRAM brightness access,
5692 * call with brightness_mutex held! */
5693 static unsigned int tpacpi_brightness_nvram_get(void)
5695 u8 lnvram;
5697 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5698 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5699 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5700 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5702 return lnvram;
5705 static void tpacpi_brightness_checkpoint_nvram(void)
5707 u8 lec = 0;
5708 u8 b_nvram;
5710 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5711 return;
5713 vdbg_printk(TPACPI_DBG_BRGHT,
5714 "trying to checkpoint backlight level to NVRAM...\n");
5716 if (mutex_lock_killable(&brightness_mutex) < 0)
5717 return;
5719 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5720 goto unlock;
5721 lec &= TP_EC_BACKLIGHT_LVLMSK;
5722 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5724 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5725 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5726 /* NVRAM needs update */
5727 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5728 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5729 b_nvram |= lec;
5730 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5731 dbg_printk(TPACPI_DBG_BRGHT,
5732 "updated NVRAM backlight level to %u (0x%02x)\n",
5733 (unsigned int) lec, (unsigned int) b_nvram);
5734 } else
5735 vdbg_printk(TPACPI_DBG_BRGHT,
5736 "NVRAM backlight level already is %u (0x%02x)\n",
5737 (unsigned int) lec, (unsigned int) b_nvram);
5739 unlock:
5740 mutex_unlock(&brightness_mutex);
5744 /* call with brightness_mutex held! */
5745 static int tpacpi_brightness_get_raw(int *status)
5747 u8 lec = 0;
5749 switch (brightness_mode) {
5750 case TPACPI_BRGHT_MODE_UCMS_STEP:
5751 *status = tpacpi_brightness_nvram_get();
5752 return 0;
5753 case TPACPI_BRGHT_MODE_EC:
5754 case TPACPI_BRGHT_MODE_ECNVRAM:
5755 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5756 return -EIO;
5757 *status = lec;
5758 return 0;
5759 default:
5760 return -ENXIO;
5764 /* call with brightness_mutex held! */
5765 /* do NOT call with illegal backlight level value */
5766 static int tpacpi_brightness_set_ec(unsigned int value)
5768 u8 lec = 0;
5770 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5771 return -EIO;
5773 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5774 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5775 (value & TP_EC_BACKLIGHT_LVLMSK))))
5776 return -EIO;
5778 return 0;
5781 /* call with brightness_mutex held! */
5782 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5784 int cmos_cmd, inc;
5785 unsigned int current_value, i;
5787 current_value = tpacpi_brightness_nvram_get();
5789 if (value == current_value)
5790 return 0;
5792 cmos_cmd = (value > current_value) ?
5793 TP_CMOS_BRIGHTNESS_UP :
5794 TP_CMOS_BRIGHTNESS_DOWN;
5795 inc = (value > current_value) ? 1 : -1;
5797 for (i = current_value; i != value; i += inc)
5798 if (issue_thinkpad_cmos_command(cmos_cmd))
5799 return -EIO;
5801 return 0;
5804 /* May return EINTR which can always be mapped to ERESTARTSYS */
5805 static int brightness_set(unsigned int value)
5807 int res;
5809 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5810 value < 0)
5811 return -EINVAL;
5813 vdbg_printk(TPACPI_DBG_BRGHT,
5814 "set backlight level to %d\n", value);
5816 res = mutex_lock_killable(&brightness_mutex);
5817 if (res < 0)
5818 return res;
5820 switch (brightness_mode) {
5821 case TPACPI_BRGHT_MODE_EC:
5822 case TPACPI_BRGHT_MODE_ECNVRAM:
5823 res = tpacpi_brightness_set_ec(value);
5824 break;
5825 case TPACPI_BRGHT_MODE_UCMS_STEP:
5826 res = tpacpi_brightness_set_ucmsstep(value);
5827 break;
5828 default:
5829 res = -ENXIO;
5832 mutex_unlock(&brightness_mutex);
5833 return res;
5836 /* sysfs backlight class ----------------------------------------------- */
5838 static int brightness_update_status(struct backlight_device *bd)
5840 unsigned int level =
5841 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5842 bd->props.power == FB_BLANK_UNBLANK) ?
5843 bd->props.brightness : 0;
5845 dbg_printk(TPACPI_DBG_BRGHT,
5846 "backlight: attempt to set level to %d\n",
5847 level);
5849 /* it is the backlight class's job (caller) to handle
5850 * EINTR and other errors properly */
5851 return brightness_set(level);
5854 static int brightness_get(struct backlight_device *bd)
5856 int status, res;
5858 res = mutex_lock_killable(&brightness_mutex);
5859 if (res < 0)
5860 return 0;
5862 res = tpacpi_brightness_get_raw(&status);
5864 mutex_unlock(&brightness_mutex);
5866 if (res < 0)
5867 return 0;
5869 return status & TP_EC_BACKLIGHT_LVLMSK;
5872 static struct backlight_ops ibm_backlight_data = {
5873 .get_brightness = brightness_get,
5874 .update_status = brightness_update_status,
5877 /* --------------------------------------------------------------------- */
5880 * These are only useful for models that have only one possibility
5881 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5882 * these quirks.
5884 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5885 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5886 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5888 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5889 /* Models with ATI GPUs known to require ECNVRAM mode */
5890 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5892 /* Models with ATI GPUs that can use ECNVRAM */
5893 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
5894 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5895 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5896 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5898 /* Models with Intel Extreme Graphics 2 */
5899 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
5900 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5901 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5903 /* Models with Intel GMA900 */
5904 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5905 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5906 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5909 static int __init brightness_init(struct ibm_init_struct *iibm)
5911 int b;
5912 unsigned long quirks;
5914 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5916 mutex_init(&brightness_mutex);
5918 quirks = tpacpi_check_quirks(brightness_quirk_table,
5919 ARRAY_SIZE(brightness_quirk_table));
5922 * We always attempt to detect acpi support, so as to switch
5923 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5924 * going to publish a backlight interface
5926 b = tpacpi_check_std_acpi_brightness_support();
5927 if (b > 0) {
5929 if (acpi_video_backlight_support()) {
5930 if (brightness_enable > 1) {
5931 printk(TPACPI_NOTICE
5932 "Standard ACPI backlight interface "
5933 "available, not loading native one.\n");
5934 return 1;
5935 } else if (brightness_enable == 1) {
5936 printk(TPACPI_NOTICE
5937 "Backlight control force enabled, even if standard "
5938 "ACPI backlight interface is available\n");
5940 } else {
5941 if (brightness_enable > 1) {
5942 printk(TPACPI_NOTICE
5943 "Standard ACPI backlight interface not "
5944 "available, thinkpad_acpi native "
5945 "brightness control enabled\n");
5950 if (!brightness_enable) {
5951 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5952 "brightness support disabled by "
5953 "module parameter\n");
5954 return 1;
5957 if (b > 16) {
5958 printk(TPACPI_ERR
5959 "Unsupported brightness interface, "
5960 "please contact %s\n", TPACPI_MAIL);
5961 return 1;
5963 if (b == 16)
5964 tp_features.bright_16levels = 1;
5967 * Check for module parameter bogosity, note that we
5968 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5969 * able to detect "unspecified"
5971 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5972 return -EINVAL;
5974 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5975 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5976 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5977 if (quirks & TPACPI_BRGHT_Q_EC)
5978 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5979 else
5980 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5982 dbg_printk(TPACPI_DBG_BRGHT,
5983 "driver auto-selected brightness_mode=%d\n",
5984 brightness_mode);
5987 /* Safety */
5988 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5989 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5990 brightness_mode == TPACPI_BRGHT_MODE_EC))
5991 return -EINVAL;
5993 if (tpacpi_brightness_get_raw(&b) < 0)
5994 return 1;
5996 if (tp_features.bright_16levels)
5997 printk(TPACPI_INFO
5998 "detected a 16-level brightness capable ThinkPad\n");
6000 ibm_backlight_device = backlight_device_register(
6001 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6002 &ibm_backlight_data);
6003 if (IS_ERR(ibm_backlight_device)) {
6004 printk(TPACPI_ERR "Could not register backlight device\n");
6005 return PTR_ERR(ibm_backlight_device);
6007 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6008 "brightness is supported\n");
6010 if (quirks & TPACPI_BRGHT_Q_ASK) {
6011 printk(TPACPI_NOTICE
6012 "brightness: will use unverified default: "
6013 "brightness_mode=%d\n", brightness_mode);
6014 printk(TPACPI_NOTICE
6015 "brightness: please report to %s whether it works well "
6016 "or not on your ThinkPad\n", TPACPI_MAIL);
6019 ibm_backlight_device->props.max_brightness =
6020 (tp_features.bright_16levels)? 15 : 7;
6021 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6022 backlight_update_status(ibm_backlight_device);
6024 return 0;
6027 static void brightness_suspend(pm_message_t state)
6029 tpacpi_brightness_checkpoint_nvram();
6032 static void brightness_shutdown(void)
6034 tpacpi_brightness_checkpoint_nvram();
6037 static void brightness_exit(void)
6039 if (ibm_backlight_device) {
6040 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6041 "calling backlight_device_unregister()\n");
6042 backlight_device_unregister(ibm_backlight_device);
6045 tpacpi_brightness_checkpoint_nvram();
6048 static int brightness_read(char *p)
6050 int len = 0;
6051 int level;
6053 level = brightness_get(NULL);
6054 if (level < 0) {
6055 len += sprintf(p + len, "level:\t\tunreadable\n");
6056 } else {
6057 len += sprintf(p + len, "level:\t\t%d\n", level);
6058 len += sprintf(p + len, "commands:\tup, down\n");
6059 len += sprintf(p + len, "commands:\tlevel <level>"
6060 " (<level> is 0-%d)\n",
6061 (tp_features.bright_16levels) ? 15 : 7);
6064 return len;
6067 static int brightness_write(char *buf)
6069 int level;
6070 int rc;
6071 char *cmd;
6072 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6074 level = brightness_get(NULL);
6075 if (level < 0)
6076 return level;
6078 while ((cmd = next_cmd(&buf))) {
6079 if (strlencmp(cmd, "up") == 0) {
6080 if (level < max_level)
6081 level++;
6082 } else if (strlencmp(cmd, "down") == 0) {
6083 if (level > 0)
6084 level--;
6085 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6086 level >= 0 && level <= max_level) {
6087 /* new level set */
6088 } else
6089 return -EINVAL;
6092 tpacpi_disclose_usertask("procfs brightness",
6093 "set level to %d\n", level);
6096 * Now we know what the final level should be, so we try to set it.
6097 * Doing it this way makes the syscall restartable in case of EINTR
6099 rc = brightness_set(level);
6100 return (rc == -EINTR)? ERESTARTSYS : rc;
6103 static struct ibm_struct brightness_driver_data = {
6104 .name = "brightness",
6105 .read = brightness_read,
6106 .write = brightness_write,
6107 .exit = brightness_exit,
6108 .suspend = brightness_suspend,
6109 .shutdown = brightness_shutdown,
6112 /*************************************************************************
6113 * Volume subdriver
6116 static int volume_offset = 0x30;
6118 static int volume_read(char *p)
6120 int len = 0;
6121 u8 level;
6123 if (!acpi_ec_read(volume_offset, &level)) {
6124 len += sprintf(p + len, "level:\t\tunreadable\n");
6125 } else {
6126 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
6127 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
6128 len += sprintf(p + len, "commands:\tup, down, mute\n");
6129 len += sprintf(p + len, "commands:\tlevel <level>"
6130 " (<level> is 0-15)\n");
6133 return len;
6136 static int volume_write(char *buf)
6138 int cmos_cmd, inc, i;
6139 u8 level, mute;
6140 int new_level, new_mute;
6141 char *cmd;
6143 while ((cmd = next_cmd(&buf))) {
6144 if (!acpi_ec_read(volume_offset, &level))
6145 return -EIO;
6146 new_mute = mute = level & 0x40;
6147 new_level = level = level & 0xf;
6149 if (strlencmp(cmd, "up") == 0) {
6150 if (mute)
6151 new_mute = 0;
6152 else
6153 new_level = level == 15 ? 15 : level + 1;
6154 } else if (strlencmp(cmd, "down") == 0) {
6155 if (mute)
6156 new_mute = 0;
6157 else
6158 new_level = level == 0 ? 0 : level - 1;
6159 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
6160 new_level >= 0 && new_level <= 15) {
6161 /* new_level set */
6162 } else if (strlencmp(cmd, "mute") == 0) {
6163 new_mute = 0x40;
6164 } else
6165 return -EINVAL;
6167 if (new_level != level) {
6168 /* mute doesn't change */
6170 cmos_cmd = (new_level > level) ?
6171 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
6172 inc = new_level > level ? 1 : -1;
6174 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
6175 !acpi_ec_write(volume_offset, level)))
6176 return -EIO;
6178 for (i = level; i != new_level; i += inc)
6179 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6180 !acpi_ec_write(volume_offset, i + inc))
6181 return -EIO;
6183 if (mute &&
6184 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
6185 !acpi_ec_write(volume_offset, new_level + mute))) {
6186 return -EIO;
6190 if (new_mute != mute) {
6191 /* level doesn't change */
6193 cmos_cmd = (new_mute) ?
6194 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
6196 if (issue_thinkpad_cmos_command(cmos_cmd) ||
6197 !acpi_ec_write(volume_offset, level + new_mute))
6198 return -EIO;
6202 return 0;
6205 static struct ibm_struct volume_driver_data = {
6206 .name = "volume",
6207 .read = volume_read,
6208 .write = volume_write,
6211 /*************************************************************************
6212 * Fan subdriver
6216 * FAN ACCESS MODES
6218 * TPACPI_FAN_RD_ACPI_GFAN:
6219 * ACPI GFAN method: returns fan level
6221 * see TPACPI_FAN_WR_ACPI_SFAN
6222 * EC 0x2f (HFSP) not available if GFAN exists
6224 * TPACPI_FAN_WR_ACPI_SFAN:
6225 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6227 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6228 * it for writing.
6230 * TPACPI_FAN_WR_TPEC:
6231 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6232 * Supported on almost all ThinkPads
6234 * Fan speed changes of any sort (including those caused by the
6235 * disengaged mode) are usually done slowly by the firmware as the
6236 * maximum ammount of fan duty cycle change per second seems to be
6237 * limited.
6239 * Reading is not available if GFAN exists.
6240 * Writing is not available if SFAN exists.
6242 * Bits
6243 * 7 automatic mode engaged;
6244 * (default operation mode of the ThinkPad)
6245 * fan level is ignored in this mode.
6246 * 6 full speed mode (takes precedence over bit 7);
6247 * not available on all thinkpads. May disable
6248 * the tachometer while the fan controller ramps up
6249 * the speed (which can take up to a few *minutes*).
6250 * Speeds up fan to 100% duty-cycle, which is far above
6251 * the standard RPM levels. It is not impossible that
6252 * it could cause hardware damage.
6253 * 5-3 unused in some models. Extra bits for fan level
6254 * in others, but still useless as all values above
6255 * 7 map to the same speed as level 7 in these models.
6256 * 2-0 fan level (0..7 usually)
6257 * 0x00 = stop
6258 * 0x07 = max (set when temperatures critical)
6259 * Some ThinkPads may have other levels, see
6260 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6262 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6263 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6264 * does so, its initial value is meaningless (0x07).
6266 * For firmware bugs, refer to:
6267 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6269 * ----
6271 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6272 * Main fan tachometer reading (in RPM)
6274 * This register is present on all ThinkPads with a new-style EC, and
6275 * it is known not to be present on the A21m/e, and T22, as there is
6276 * something else in offset 0x84 according to the ACPI DSDT. Other
6277 * ThinkPads from this same time period (and earlier) probably lack the
6278 * tachometer as well.
6280 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6281 * was never fixed by IBM to report the EC firmware version string
6282 * probably support the tachometer (like the early X models), so
6283 * detecting it is quite hard. We need more data to know for sure.
6285 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6286 * might result.
6288 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6289 * mode.
6291 * For firmware bugs, refer to:
6292 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6294 * ----
6296 * ThinkPad EC register 0x31 bit 0 (only on select models)
6298 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6299 * show the speed of the main fan. When bit 0 of EC register 0x31
6300 * is one, the tachometer registers show the speed of the auxiliary
6301 * fan.
6303 * Fan control seems to affect both fans, regardless of the state
6304 * of this bit.
6306 * So far, only the firmware for the X60/X61 non-tablet versions
6307 * seem to support this (firmware TP-7M).
6309 * TPACPI_FAN_WR_ACPI_FANS:
6310 * ThinkPad X31, X40, X41. Not available in the X60.
6312 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6313 * high speed. ACPI DSDT seems to map these three speeds to levels
6314 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6315 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6317 * The speeds are stored on handles
6318 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6320 * There are three default speed sets, acessible as handles:
6321 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6323 * ACPI DSDT switches which set is in use depending on various
6324 * factors.
6326 * TPACPI_FAN_WR_TPEC is also available and should be used to
6327 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6328 * but the ACPI tables just mention level 7.
6331 enum { /* Fan control constants */
6332 fan_status_offset = 0x2f, /* EC register 0x2f */
6333 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6334 * 0x84 must be read before 0x85 */
6335 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6336 bit 0 selects which fan is active */
6338 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6339 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6341 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6344 enum fan_status_access_mode {
6345 TPACPI_FAN_NONE = 0, /* No fan status or control */
6346 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6347 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6350 enum fan_control_access_mode {
6351 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6352 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6353 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6354 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6357 enum fan_control_commands {
6358 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6359 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6360 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6361 * and also watchdog cmd */
6364 static int fan_control_allowed;
6366 static enum fan_status_access_mode fan_status_access_mode;
6367 static enum fan_control_access_mode fan_control_access_mode;
6368 static enum fan_control_commands fan_control_commands;
6370 static u8 fan_control_initial_status;
6371 static u8 fan_control_desired_level;
6372 static u8 fan_control_resume_level;
6373 static int fan_watchdog_maxinterval;
6375 static struct mutex fan_mutex;
6377 static void fan_watchdog_fire(struct work_struct *ignored);
6378 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6380 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6381 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6382 "\\FSPD", /* 600e/x, 770e, 770x */
6383 ); /* all others */
6384 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6385 "JFNS", /* 770x-JL */
6386 ); /* all others */
6389 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6390 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6391 * be in auto mode (0x80).
6393 * This is corrected by any write to HFSP either by the driver, or
6394 * by the firmware.
6396 * We assume 0x07 really means auto mode while this quirk is active,
6397 * as this is far more likely than the ThinkPad being in level 7,
6398 * which is only used by the firmware during thermal emergencies.
6400 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6401 * TP-70 (T43, R52), which are known to be buggy.
6404 static void fan_quirk1_setup(void)
6406 if (fan_control_initial_status == 0x07) {
6407 printk(TPACPI_NOTICE
6408 "fan_init: initial fan status is unknown, "
6409 "assuming it is in auto mode\n");
6410 tp_features.fan_ctrl_status_undef = 1;
6414 static void fan_quirk1_handle(u8 *fan_status)
6416 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6417 if (*fan_status != fan_control_initial_status) {
6418 /* something changed the HFSP regisnter since
6419 * driver init time, so it is not undefined
6420 * anymore */
6421 tp_features.fan_ctrl_status_undef = 0;
6422 } else {
6423 /* Return most likely status. In fact, it
6424 * might be the only possible status */
6425 *fan_status = TP_EC_FAN_AUTO;
6430 /* Select main fan on X60/X61, NOOP on others */
6431 static bool fan_select_fan1(void)
6433 if (tp_features.second_fan) {
6434 u8 val;
6436 if (ec_read(fan_select_offset, &val) < 0)
6437 return false;
6438 val &= 0xFEU;
6439 if (ec_write(fan_select_offset, val) < 0)
6440 return false;
6442 return true;
6445 /* Select secondary fan on X60/X61 */
6446 static bool fan_select_fan2(void)
6448 u8 val;
6450 if (!tp_features.second_fan)
6451 return false;
6453 if (ec_read(fan_select_offset, &val) < 0)
6454 return false;
6455 val |= 0x01U;
6456 if (ec_write(fan_select_offset, val) < 0)
6457 return false;
6459 return true;
6463 * Call with fan_mutex held
6465 static void fan_update_desired_level(u8 status)
6467 if ((status &
6468 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6469 if (status > 7)
6470 fan_control_desired_level = 7;
6471 else
6472 fan_control_desired_level = status;
6476 static int fan_get_status(u8 *status)
6478 u8 s;
6480 /* TODO:
6481 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6483 switch (fan_status_access_mode) {
6484 case TPACPI_FAN_RD_ACPI_GFAN:
6485 /* 570, 600e/x, 770e, 770x */
6487 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6488 return -EIO;
6490 if (likely(status))
6491 *status = s & 0x07;
6493 break;
6495 case TPACPI_FAN_RD_TPEC:
6496 /* all except 570, 600e/x, 770e, 770x */
6497 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6498 return -EIO;
6500 if (likely(status)) {
6501 *status = s;
6502 fan_quirk1_handle(status);
6505 break;
6507 default:
6508 return -ENXIO;
6511 return 0;
6514 static int fan_get_status_safe(u8 *status)
6516 int rc;
6517 u8 s;
6519 if (mutex_lock_killable(&fan_mutex))
6520 return -ERESTARTSYS;
6521 rc = fan_get_status(&s);
6522 if (!rc)
6523 fan_update_desired_level(s);
6524 mutex_unlock(&fan_mutex);
6526 if (status)
6527 *status = s;
6529 return rc;
6532 static int fan_get_speed(unsigned int *speed)
6534 u8 hi, lo;
6536 switch (fan_status_access_mode) {
6537 case TPACPI_FAN_RD_TPEC:
6538 /* all except 570, 600e/x, 770e, 770x */
6539 if (unlikely(!fan_select_fan1()))
6540 return -EIO;
6541 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6542 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6543 return -EIO;
6545 if (likely(speed))
6546 *speed = (hi << 8) | lo;
6548 break;
6550 default:
6551 return -ENXIO;
6554 return 0;
6557 static int fan2_get_speed(unsigned int *speed)
6559 u8 hi, lo;
6560 bool rc;
6562 switch (fan_status_access_mode) {
6563 case TPACPI_FAN_RD_TPEC:
6564 /* all except 570, 600e/x, 770e, 770x */
6565 if (unlikely(!fan_select_fan2()))
6566 return -EIO;
6567 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6568 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6569 fan_select_fan1(); /* play it safe */
6570 if (rc)
6571 return -EIO;
6573 if (likely(speed))
6574 *speed = (hi << 8) | lo;
6576 break;
6578 default:
6579 return -ENXIO;
6582 return 0;
6585 static int fan_set_level(int level)
6587 if (!fan_control_allowed)
6588 return -EPERM;
6590 switch (fan_control_access_mode) {
6591 case TPACPI_FAN_WR_ACPI_SFAN:
6592 if (level >= 0 && level <= 7) {
6593 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6594 return -EIO;
6595 } else
6596 return -EINVAL;
6597 break;
6599 case TPACPI_FAN_WR_ACPI_FANS:
6600 case TPACPI_FAN_WR_TPEC:
6601 if (!(level & TP_EC_FAN_AUTO) &&
6602 !(level & TP_EC_FAN_FULLSPEED) &&
6603 ((level < 0) || (level > 7)))
6604 return -EINVAL;
6606 /* safety net should the EC not support AUTO
6607 * or FULLSPEED mode bits and just ignore them */
6608 if (level & TP_EC_FAN_FULLSPEED)
6609 level |= 7; /* safety min speed 7 */
6610 else if (level & TP_EC_FAN_AUTO)
6611 level |= 4; /* safety min speed 4 */
6613 if (!acpi_ec_write(fan_status_offset, level))
6614 return -EIO;
6615 else
6616 tp_features.fan_ctrl_status_undef = 0;
6617 break;
6619 default:
6620 return -ENXIO;
6623 vdbg_printk(TPACPI_DBG_FAN,
6624 "fan control: set fan control register to 0x%02x\n", level);
6625 return 0;
6628 static int fan_set_level_safe(int level)
6630 int rc;
6632 if (!fan_control_allowed)
6633 return -EPERM;
6635 if (mutex_lock_killable(&fan_mutex))
6636 return -ERESTARTSYS;
6638 if (level == TPACPI_FAN_LAST_LEVEL)
6639 level = fan_control_desired_level;
6641 rc = fan_set_level(level);
6642 if (!rc)
6643 fan_update_desired_level(level);
6645 mutex_unlock(&fan_mutex);
6646 return rc;
6649 static int fan_set_enable(void)
6651 u8 s;
6652 int rc;
6654 if (!fan_control_allowed)
6655 return -EPERM;
6657 if (mutex_lock_killable(&fan_mutex))
6658 return -ERESTARTSYS;
6660 switch (fan_control_access_mode) {
6661 case TPACPI_FAN_WR_ACPI_FANS:
6662 case TPACPI_FAN_WR_TPEC:
6663 rc = fan_get_status(&s);
6664 if (rc < 0)
6665 break;
6667 /* Don't go out of emergency fan mode */
6668 if (s != 7) {
6669 s &= 0x07;
6670 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6673 if (!acpi_ec_write(fan_status_offset, s))
6674 rc = -EIO;
6675 else {
6676 tp_features.fan_ctrl_status_undef = 0;
6677 rc = 0;
6679 break;
6681 case TPACPI_FAN_WR_ACPI_SFAN:
6682 rc = fan_get_status(&s);
6683 if (rc < 0)
6684 break;
6686 s &= 0x07;
6688 /* Set fan to at least level 4 */
6689 s |= 4;
6691 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6692 rc = -EIO;
6693 else
6694 rc = 0;
6695 break;
6697 default:
6698 rc = -ENXIO;
6701 mutex_unlock(&fan_mutex);
6703 if (!rc)
6704 vdbg_printk(TPACPI_DBG_FAN,
6705 "fan control: set fan control register to 0x%02x\n",
6707 return rc;
6710 static int fan_set_disable(void)
6712 int rc;
6714 if (!fan_control_allowed)
6715 return -EPERM;
6717 if (mutex_lock_killable(&fan_mutex))
6718 return -ERESTARTSYS;
6720 rc = 0;
6721 switch (fan_control_access_mode) {
6722 case TPACPI_FAN_WR_ACPI_FANS:
6723 case TPACPI_FAN_WR_TPEC:
6724 if (!acpi_ec_write(fan_status_offset, 0x00))
6725 rc = -EIO;
6726 else {
6727 fan_control_desired_level = 0;
6728 tp_features.fan_ctrl_status_undef = 0;
6730 break;
6732 case TPACPI_FAN_WR_ACPI_SFAN:
6733 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6734 rc = -EIO;
6735 else
6736 fan_control_desired_level = 0;
6737 break;
6739 default:
6740 rc = -ENXIO;
6743 if (!rc)
6744 vdbg_printk(TPACPI_DBG_FAN,
6745 "fan control: set fan control register to 0\n");
6747 mutex_unlock(&fan_mutex);
6748 return rc;
6751 static int fan_set_speed(int speed)
6753 int rc;
6755 if (!fan_control_allowed)
6756 return -EPERM;
6758 if (mutex_lock_killable(&fan_mutex))
6759 return -ERESTARTSYS;
6761 rc = 0;
6762 switch (fan_control_access_mode) {
6763 case TPACPI_FAN_WR_ACPI_FANS:
6764 if (speed >= 0 && speed <= 65535) {
6765 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6766 speed, speed, speed))
6767 rc = -EIO;
6768 } else
6769 rc = -EINVAL;
6770 break;
6772 default:
6773 rc = -ENXIO;
6776 mutex_unlock(&fan_mutex);
6777 return rc;
6780 static void fan_watchdog_reset(void)
6782 static int fan_watchdog_active;
6784 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6785 return;
6787 if (fan_watchdog_active)
6788 cancel_delayed_work(&fan_watchdog_task);
6790 if (fan_watchdog_maxinterval > 0 &&
6791 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6792 fan_watchdog_active = 1;
6793 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6794 msecs_to_jiffies(fan_watchdog_maxinterval
6795 * 1000))) {
6796 printk(TPACPI_ERR
6797 "failed to queue the fan watchdog, "
6798 "watchdog will not trigger\n");
6800 } else
6801 fan_watchdog_active = 0;
6804 static void fan_watchdog_fire(struct work_struct *ignored)
6806 int rc;
6808 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6809 return;
6811 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6812 rc = fan_set_enable();
6813 if (rc < 0) {
6814 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6815 "will try again later...\n", -rc);
6816 /* reschedule for later */
6817 fan_watchdog_reset();
6822 * SYSFS fan layout: hwmon compatible (device)
6824 * pwm*_enable:
6825 * 0: "disengaged" mode
6826 * 1: manual mode
6827 * 2: native EC "auto" mode (recommended, hardware default)
6829 * pwm*: set speed in manual mode, ignored otherwise.
6830 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6831 * interpolation.
6833 * fan*_input: tachometer reading, RPM
6836 * SYSFS fan layout: extensions
6838 * fan_watchdog (driver):
6839 * fan watchdog interval in seconds, 0 disables (default), max 120
6842 /* sysfs fan pwm1_enable ----------------------------------------------- */
6843 static ssize_t fan_pwm1_enable_show(struct device *dev,
6844 struct device_attribute *attr,
6845 char *buf)
6847 int res, mode;
6848 u8 status;
6850 res = fan_get_status_safe(&status);
6851 if (res)
6852 return res;
6854 if (status & TP_EC_FAN_FULLSPEED) {
6855 mode = 0;
6856 } else if (status & TP_EC_FAN_AUTO) {
6857 mode = 2;
6858 } else
6859 mode = 1;
6861 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6864 static ssize_t fan_pwm1_enable_store(struct device *dev,
6865 struct device_attribute *attr,
6866 const char *buf, size_t count)
6868 unsigned long t;
6869 int res, level;
6871 if (parse_strtoul(buf, 2, &t))
6872 return -EINVAL;
6874 tpacpi_disclose_usertask("hwmon pwm1_enable",
6875 "set fan mode to %lu\n", t);
6877 switch (t) {
6878 case 0:
6879 level = TP_EC_FAN_FULLSPEED;
6880 break;
6881 case 1:
6882 level = TPACPI_FAN_LAST_LEVEL;
6883 break;
6884 case 2:
6885 level = TP_EC_FAN_AUTO;
6886 break;
6887 case 3:
6888 /* reserved for software-controlled auto mode */
6889 return -ENOSYS;
6890 default:
6891 return -EINVAL;
6894 res = fan_set_level_safe(level);
6895 if (res == -ENXIO)
6896 return -EINVAL;
6897 else if (res < 0)
6898 return res;
6900 fan_watchdog_reset();
6902 return count;
6905 static struct device_attribute dev_attr_fan_pwm1_enable =
6906 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6907 fan_pwm1_enable_show, fan_pwm1_enable_store);
6909 /* sysfs fan pwm1 ------------------------------------------------------ */
6910 static ssize_t fan_pwm1_show(struct device *dev,
6911 struct device_attribute *attr,
6912 char *buf)
6914 int res;
6915 u8 status;
6917 res = fan_get_status_safe(&status);
6918 if (res)
6919 return res;
6921 if ((status &
6922 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6923 status = fan_control_desired_level;
6925 if (status > 7)
6926 status = 7;
6928 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6931 static ssize_t fan_pwm1_store(struct device *dev,
6932 struct device_attribute *attr,
6933 const char *buf, size_t count)
6935 unsigned long s;
6936 int rc;
6937 u8 status, newlevel;
6939 if (parse_strtoul(buf, 255, &s))
6940 return -EINVAL;
6942 tpacpi_disclose_usertask("hwmon pwm1",
6943 "set fan speed to %lu\n", s);
6945 /* scale down from 0-255 to 0-7 */
6946 newlevel = (s >> 5) & 0x07;
6948 if (mutex_lock_killable(&fan_mutex))
6949 return -ERESTARTSYS;
6951 rc = fan_get_status(&status);
6952 if (!rc && (status &
6953 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6954 rc = fan_set_level(newlevel);
6955 if (rc == -ENXIO)
6956 rc = -EINVAL;
6957 else if (!rc) {
6958 fan_update_desired_level(newlevel);
6959 fan_watchdog_reset();
6963 mutex_unlock(&fan_mutex);
6964 return (rc)? rc : count;
6967 static struct device_attribute dev_attr_fan_pwm1 =
6968 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6969 fan_pwm1_show, fan_pwm1_store);
6971 /* sysfs fan fan1_input ------------------------------------------------ */
6972 static ssize_t fan_fan1_input_show(struct device *dev,
6973 struct device_attribute *attr,
6974 char *buf)
6976 int res;
6977 unsigned int speed;
6979 res = fan_get_speed(&speed);
6980 if (res < 0)
6981 return res;
6983 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6986 static struct device_attribute dev_attr_fan_fan1_input =
6987 __ATTR(fan1_input, S_IRUGO,
6988 fan_fan1_input_show, NULL);
6990 /* sysfs fan fan2_input ------------------------------------------------ */
6991 static ssize_t fan_fan2_input_show(struct device *dev,
6992 struct device_attribute *attr,
6993 char *buf)
6995 int res;
6996 unsigned int speed;
6998 res = fan2_get_speed(&speed);
6999 if (res < 0)
7000 return res;
7002 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7005 static struct device_attribute dev_attr_fan_fan2_input =
7006 __ATTR(fan2_input, S_IRUGO,
7007 fan_fan2_input_show, NULL);
7009 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7010 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7011 char *buf)
7013 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7016 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7017 const char *buf, size_t count)
7019 unsigned long t;
7021 if (parse_strtoul(buf, 120, &t))
7022 return -EINVAL;
7024 if (!fan_control_allowed)
7025 return -EPERM;
7027 fan_watchdog_maxinterval = t;
7028 fan_watchdog_reset();
7030 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7032 return count;
7035 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7036 fan_fan_watchdog_show, fan_fan_watchdog_store);
7038 /* --------------------------------------------------------------------- */
7039 static struct attribute *fan_attributes[] = {
7040 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7041 &dev_attr_fan_fan1_input.attr,
7042 NULL, /* for fan2_input */
7043 NULL
7046 static const struct attribute_group fan_attr_group = {
7047 .attrs = fan_attributes,
7050 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7051 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7053 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7054 { .vendor = PCI_VENDOR_ID_IBM, \
7055 .bios = TPACPI_MATCH_ANY, \
7056 .ec = TPID(__id1, __id2), \
7057 .quirks = __quirks }
7059 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7060 { .vendor = PCI_VENDOR_ID_LENOVO, \
7061 .bios = TPACPI_MATCH_ANY, \
7062 .ec = TPID(__id1, __id2), \
7063 .quirks = __quirks }
7065 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7066 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7067 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7068 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7069 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7070 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7073 #undef TPACPI_FAN_QL
7074 #undef TPACPI_FAN_QI
7076 static int __init fan_init(struct ibm_init_struct *iibm)
7078 int rc;
7079 unsigned long quirks;
7081 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7082 "initializing fan subdriver\n");
7084 mutex_init(&fan_mutex);
7085 fan_status_access_mode = TPACPI_FAN_NONE;
7086 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7087 fan_control_commands = 0;
7088 fan_watchdog_maxinterval = 0;
7089 tp_features.fan_ctrl_status_undef = 0;
7090 tp_features.second_fan = 0;
7091 fan_control_desired_level = 7;
7093 TPACPI_ACPIHANDLE_INIT(fans);
7094 TPACPI_ACPIHANDLE_INIT(gfan);
7095 TPACPI_ACPIHANDLE_INIT(sfan);
7097 quirks = tpacpi_check_quirks(fan_quirk_table,
7098 ARRAY_SIZE(fan_quirk_table));
7100 if (gfan_handle) {
7101 /* 570, 600e/x, 770e, 770x */
7102 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7103 } else {
7104 /* all other ThinkPads: note that even old-style
7105 * ThinkPad ECs supports the fan control register */
7106 if (likely(acpi_ec_read(fan_status_offset,
7107 &fan_control_initial_status))) {
7108 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7109 if (quirks & TPACPI_FAN_Q1)
7110 fan_quirk1_setup();
7111 if (quirks & TPACPI_FAN_2FAN) {
7112 tp_features.second_fan = 1;
7113 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7114 "secondary fan support enabled\n");
7116 } else {
7117 printk(TPACPI_ERR
7118 "ThinkPad ACPI EC access misbehaving, "
7119 "fan status and control unavailable\n");
7120 return 1;
7124 if (sfan_handle) {
7125 /* 570, 770x-JL */
7126 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7127 fan_control_commands |=
7128 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7129 } else {
7130 if (!gfan_handle) {
7131 /* gfan without sfan means no fan control */
7132 /* all other models implement TP EC 0x2f control */
7134 if (fans_handle) {
7135 /* X31, X40, X41 */
7136 fan_control_access_mode =
7137 TPACPI_FAN_WR_ACPI_FANS;
7138 fan_control_commands |=
7139 TPACPI_FAN_CMD_SPEED |
7140 TPACPI_FAN_CMD_LEVEL |
7141 TPACPI_FAN_CMD_ENABLE;
7142 } else {
7143 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7144 fan_control_commands |=
7145 TPACPI_FAN_CMD_LEVEL |
7146 TPACPI_FAN_CMD_ENABLE;
7151 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7152 "fan is %s, modes %d, %d\n",
7153 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7154 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7155 fan_status_access_mode, fan_control_access_mode);
7157 /* fan control master switch */
7158 if (!fan_control_allowed) {
7159 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7160 fan_control_commands = 0;
7161 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7162 "fan control features disabled by parameter\n");
7165 /* update fan_control_desired_level */
7166 if (fan_status_access_mode != TPACPI_FAN_NONE)
7167 fan_get_status_safe(NULL);
7169 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7170 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7171 if (tp_features.second_fan) {
7172 /* attach second fan tachometer */
7173 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7174 &dev_attr_fan_fan2_input.attr;
7176 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7177 &fan_attr_group);
7178 if (rc < 0)
7179 return rc;
7181 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7182 &driver_attr_fan_watchdog);
7183 if (rc < 0) {
7184 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7185 &fan_attr_group);
7186 return rc;
7188 return 0;
7189 } else
7190 return 1;
7193 static void fan_exit(void)
7195 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7196 "cancelling any pending fan watchdog tasks\n");
7198 /* FIXME: can we really do this unconditionally? */
7199 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7200 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7201 &driver_attr_fan_watchdog);
7203 cancel_delayed_work(&fan_watchdog_task);
7204 flush_workqueue(tpacpi_wq);
7207 static void fan_suspend(pm_message_t state)
7209 int rc;
7211 if (!fan_control_allowed)
7212 return;
7214 /* Store fan status in cache */
7215 fan_control_resume_level = 0;
7216 rc = fan_get_status_safe(&fan_control_resume_level);
7217 if (rc < 0)
7218 printk(TPACPI_NOTICE
7219 "failed to read fan level for later "
7220 "restore during resume: %d\n", rc);
7222 /* if it is undefined, don't attempt to restore it.
7223 * KEEP THIS LAST */
7224 if (tp_features.fan_ctrl_status_undef)
7225 fan_control_resume_level = 0;
7228 static void fan_resume(void)
7230 u8 current_level = 7;
7231 bool do_set = false;
7232 int rc;
7234 /* DSDT *always* updates status on resume */
7235 tp_features.fan_ctrl_status_undef = 0;
7237 if (!fan_control_allowed ||
7238 !fan_control_resume_level ||
7239 (fan_get_status_safe(&current_level) < 0))
7240 return;
7242 switch (fan_control_access_mode) {
7243 case TPACPI_FAN_WR_ACPI_SFAN:
7244 /* never decrease fan level */
7245 do_set = (fan_control_resume_level > current_level);
7246 break;
7247 case TPACPI_FAN_WR_ACPI_FANS:
7248 case TPACPI_FAN_WR_TPEC:
7249 /* never decrease fan level, scale is:
7250 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7252 * We expect the firmware to set either 7 or AUTO, but we
7253 * handle FULLSPEED out of paranoia.
7255 * So, we can safely only restore FULLSPEED or 7, anything
7256 * else could slow the fan. Restoring AUTO is useless, at
7257 * best that's exactly what the DSDT already set (it is the
7258 * slower it uses).
7260 * Always keep in mind that the DSDT *will* have set the
7261 * fans to what the vendor supposes is the best level. We
7262 * muck with it only to speed the fan up.
7264 if (fan_control_resume_level != 7 &&
7265 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7266 return;
7267 else
7268 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7269 (current_level != fan_control_resume_level);
7270 break;
7271 default:
7272 return;
7274 if (do_set) {
7275 printk(TPACPI_NOTICE
7276 "restoring fan level to 0x%02x\n",
7277 fan_control_resume_level);
7278 rc = fan_set_level_safe(fan_control_resume_level);
7279 if (rc < 0)
7280 printk(TPACPI_NOTICE
7281 "failed to restore fan level: %d\n", rc);
7285 static int fan_read(char *p)
7287 int len = 0;
7288 int rc;
7289 u8 status;
7290 unsigned int speed = 0;
7292 switch (fan_status_access_mode) {
7293 case TPACPI_FAN_RD_ACPI_GFAN:
7294 /* 570, 600e/x, 770e, 770x */
7295 rc = fan_get_status_safe(&status);
7296 if (rc < 0)
7297 return rc;
7299 len += sprintf(p + len, "status:\t\t%s\n"
7300 "level:\t\t%d\n",
7301 (status != 0) ? "enabled" : "disabled", status);
7302 break;
7304 case TPACPI_FAN_RD_TPEC:
7305 /* all except 570, 600e/x, 770e, 770x */
7306 rc = fan_get_status_safe(&status);
7307 if (rc < 0)
7308 return rc;
7310 len += sprintf(p + len, "status:\t\t%s\n",
7311 (status != 0) ? "enabled" : "disabled");
7313 rc = fan_get_speed(&speed);
7314 if (rc < 0)
7315 return rc;
7317 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7319 if (status & TP_EC_FAN_FULLSPEED)
7320 /* Disengaged mode takes precedence */
7321 len += sprintf(p + len, "level:\t\tdisengaged\n");
7322 else if (status & TP_EC_FAN_AUTO)
7323 len += sprintf(p + len, "level:\t\tauto\n");
7324 else
7325 len += sprintf(p + len, "level:\t\t%d\n", status);
7326 break;
7328 case TPACPI_FAN_NONE:
7329 default:
7330 len += sprintf(p + len, "status:\t\tnot supported\n");
7333 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7334 len += sprintf(p + len, "commands:\tlevel <level>");
7336 switch (fan_control_access_mode) {
7337 case TPACPI_FAN_WR_ACPI_SFAN:
7338 len += sprintf(p + len, " (<level> is 0-7)\n");
7339 break;
7341 default:
7342 len += sprintf(p + len, " (<level> is 0-7, "
7343 "auto, disengaged, full-speed)\n");
7344 break;
7348 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7349 len += sprintf(p + len, "commands:\tenable, disable\n"
7350 "commands:\twatchdog <timeout> (<timeout> "
7351 "is 0 (off), 1-120 (seconds))\n");
7353 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7354 len += sprintf(p + len, "commands:\tspeed <speed>"
7355 " (<speed> is 0-65535)\n");
7357 return len;
7360 static int fan_write_cmd_level(const char *cmd, int *rc)
7362 int level;
7364 if (strlencmp(cmd, "level auto") == 0)
7365 level = TP_EC_FAN_AUTO;
7366 else if ((strlencmp(cmd, "level disengaged") == 0) |
7367 (strlencmp(cmd, "level full-speed") == 0))
7368 level = TP_EC_FAN_FULLSPEED;
7369 else if (sscanf(cmd, "level %d", &level) != 1)
7370 return 0;
7372 *rc = fan_set_level_safe(level);
7373 if (*rc == -ENXIO)
7374 printk(TPACPI_ERR "level command accepted for unsupported "
7375 "access mode %d", fan_control_access_mode);
7376 else if (!*rc)
7377 tpacpi_disclose_usertask("procfs fan",
7378 "set level to %d\n", level);
7380 return 1;
7383 static int fan_write_cmd_enable(const char *cmd, int *rc)
7385 if (strlencmp(cmd, "enable") != 0)
7386 return 0;
7388 *rc = fan_set_enable();
7389 if (*rc == -ENXIO)
7390 printk(TPACPI_ERR "enable command accepted for unsupported "
7391 "access mode %d", fan_control_access_mode);
7392 else if (!*rc)
7393 tpacpi_disclose_usertask("procfs fan", "enable\n");
7395 return 1;
7398 static int fan_write_cmd_disable(const char *cmd, int *rc)
7400 if (strlencmp(cmd, "disable") != 0)
7401 return 0;
7403 *rc = fan_set_disable();
7404 if (*rc == -ENXIO)
7405 printk(TPACPI_ERR "disable command accepted for unsupported "
7406 "access mode %d", fan_control_access_mode);
7407 else if (!*rc)
7408 tpacpi_disclose_usertask("procfs fan", "disable\n");
7410 return 1;
7413 static int fan_write_cmd_speed(const char *cmd, int *rc)
7415 int speed;
7417 /* TODO:
7418 * Support speed <low> <medium> <high> ? */
7420 if (sscanf(cmd, "speed %d", &speed) != 1)
7421 return 0;
7423 *rc = fan_set_speed(speed);
7424 if (*rc == -ENXIO)
7425 printk(TPACPI_ERR "speed command accepted for unsupported "
7426 "access mode %d", fan_control_access_mode);
7427 else if (!*rc)
7428 tpacpi_disclose_usertask("procfs fan",
7429 "set speed to %d\n", speed);
7431 return 1;
7434 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7436 int interval;
7438 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7439 return 0;
7441 if (interval < 0 || interval > 120)
7442 *rc = -EINVAL;
7443 else {
7444 fan_watchdog_maxinterval = interval;
7445 tpacpi_disclose_usertask("procfs fan",
7446 "set watchdog timer to %d\n",
7447 interval);
7450 return 1;
7453 static int fan_write(char *buf)
7455 char *cmd;
7456 int rc = 0;
7458 while (!rc && (cmd = next_cmd(&buf))) {
7459 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7460 fan_write_cmd_level(cmd, &rc)) &&
7461 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7462 (fan_write_cmd_enable(cmd, &rc) ||
7463 fan_write_cmd_disable(cmd, &rc) ||
7464 fan_write_cmd_watchdog(cmd, &rc))) &&
7465 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7466 fan_write_cmd_speed(cmd, &rc))
7468 rc = -EINVAL;
7469 else if (!rc)
7470 fan_watchdog_reset();
7473 return rc;
7476 static struct ibm_struct fan_driver_data = {
7477 .name = "fan",
7478 .read = fan_read,
7479 .write = fan_write,
7480 .exit = fan_exit,
7481 .suspend = fan_suspend,
7482 .resume = fan_resume,
7485 /****************************************************************************
7486 ****************************************************************************
7488 * Infrastructure
7490 ****************************************************************************
7491 ****************************************************************************/
7493 /* sysfs name ---------------------------------------------------------- */
7494 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7495 struct device_attribute *attr,
7496 char *buf)
7498 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7501 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7502 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7504 /* --------------------------------------------------------------------- */
7506 /* /proc support */
7507 static struct proc_dir_entry *proc_dir;
7510 * Module and infrastructure proble, init and exit handling
7513 static int force_load;
7515 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7516 static const char * __init str_supported(int is_supported)
7518 static char text_unsupported[] __initdata = "not supported";
7520 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7522 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7524 static void ibm_exit(struct ibm_struct *ibm)
7526 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7528 list_del_init(&ibm->all_drivers);
7530 if (ibm->flags.acpi_notify_installed) {
7531 dbg_printk(TPACPI_DBG_EXIT,
7532 "%s: acpi_remove_notify_handler\n", ibm->name);
7533 BUG_ON(!ibm->acpi);
7534 acpi_remove_notify_handler(*ibm->acpi->handle,
7535 ibm->acpi->type,
7536 dispatch_acpi_notify);
7537 ibm->flags.acpi_notify_installed = 0;
7538 ibm->flags.acpi_notify_installed = 0;
7541 if (ibm->flags.proc_created) {
7542 dbg_printk(TPACPI_DBG_EXIT,
7543 "%s: remove_proc_entry\n", ibm->name);
7544 remove_proc_entry(ibm->name, proc_dir);
7545 ibm->flags.proc_created = 0;
7548 if (ibm->flags.acpi_driver_registered) {
7549 dbg_printk(TPACPI_DBG_EXIT,
7550 "%s: acpi_bus_unregister_driver\n", ibm->name);
7551 BUG_ON(!ibm->acpi);
7552 acpi_bus_unregister_driver(ibm->acpi->driver);
7553 kfree(ibm->acpi->driver);
7554 ibm->acpi->driver = NULL;
7555 ibm->flags.acpi_driver_registered = 0;
7558 if (ibm->flags.init_called && ibm->exit) {
7559 ibm->exit();
7560 ibm->flags.init_called = 0;
7563 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7566 static int __init ibm_init(struct ibm_init_struct *iibm)
7568 int ret;
7569 struct ibm_struct *ibm = iibm->data;
7570 struct proc_dir_entry *entry;
7572 BUG_ON(ibm == NULL);
7574 INIT_LIST_HEAD(&ibm->all_drivers);
7576 if (ibm->flags.experimental && !experimental)
7577 return 0;
7579 dbg_printk(TPACPI_DBG_INIT,
7580 "probing for %s\n", ibm->name);
7582 if (iibm->init) {
7583 ret = iibm->init(iibm);
7584 if (ret > 0)
7585 return 0; /* probe failed */
7586 if (ret)
7587 return ret;
7589 ibm->flags.init_called = 1;
7592 if (ibm->acpi) {
7593 if (ibm->acpi->hid) {
7594 ret = register_tpacpi_subdriver(ibm);
7595 if (ret)
7596 goto err_out;
7599 if (ibm->acpi->notify) {
7600 ret = setup_acpi_notify(ibm);
7601 if (ret == -ENODEV) {
7602 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7603 ibm->name);
7604 ret = 0;
7605 goto err_out;
7607 if (ret < 0)
7608 goto err_out;
7612 dbg_printk(TPACPI_DBG_INIT,
7613 "%s installed\n", ibm->name);
7615 if (ibm->read) {
7616 entry = create_proc_entry(ibm->name,
7617 S_IFREG | S_IRUGO | S_IWUSR,
7618 proc_dir);
7619 if (!entry) {
7620 printk(TPACPI_ERR "unable to create proc entry %s\n",
7621 ibm->name);
7622 ret = -ENODEV;
7623 goto err_out;
7625 entry->data = ibm;
7626 entry->read_proc = &dispatch_procfs_read;
7627 if (ibm->write)
7628 entry->write_proc = &dispatch_procfs_write;
7629 ibm->flags.proc_created = 1;
7632 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7634 return 0;
7636 err_out:
7637 dbg_printk(TPACPI_DBG_INIT,
7638 "%s: at error exit path with result %d\n",
7639 ibm->name, ret);
7641 ibm_exit(ibm);
7642 return (ret < 0)? ret : 0;
7645 /* Probing */
7647 static bool __pure __init tpacpi_is_fw_digit(const char c)
7649 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7652 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7653 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7654 const char t)
7656 return s && strlen(s) >= 8 &&
7657 tpacpi_is_fw_digit(s[0]) &&
7658 tpacpi_is_fw_digit(s[1]) &&
7659 s[2] == t && s[3] == 'T' &&
7660 tpacpi_is_fw_digit(s[4]) &&
7661 tpacpi_is_fw_digit(s[5]) &&
7662 s[6] == 'W' && s[7] == 'W';
7665 /* returns 0 - probe ok, or < 0 - probe error.
7666 * Probe ok doesn't mean thinkpad found.
7667 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7668 static int __must_check __init get_thinkpad_model_data(
7669 struct thinkpad_id_data *tp)
7671 const struct dmi_device *dev = NULL;
7672 char ec_fw_string[18];
7673 char const *s;
7675 if (!tp)
7676 return -EINVAL;
7678 memset(tp, 0, sizeof(*tp));
7680 if (dmi_name_in_vendors("IBM"))
7681 tp->vendor = PCI_VENDOR_ID_IBM;
7682 else if (dmi_name_in_vendors("LENOVO"))
7683 tp->vendor = PCI_VENDOR_ID_LENOVO;
7684 else
7685 return 0;
7687 s = dmi_get_system_info(DMI_BIOS_VERSION);
7688 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7689 if (s && !tp->bios_version_str)
7690 return -ENOMEM;
7692 /* Really ancient ThinkPad 240X will fail this, which is fine */
7693 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7694 return 0;
7696 tp->bios_model = tp->bios_version_str[0]
7697 | (tp->bios_version_str[1] << 8);
7698 tp->bios_release = (tp->bios_version_str[4] << 8)
7699 | tp->bios_version_str[5];
7702 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7703 * X32 or newer, all Z series; Some models must have an
7704 * up-to-date BIOS or they will not be detected.
7706 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7708 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7709 if (sscanf(dev->name,
7710 "IBM ThinkPad Embedded Controller -[%17c",
7711 ec_fw_string) == 1) {
7712 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7713 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7715 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7716 if (!tp->ec_version_str)
7717 return -ENOMEM;
7719 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7720 tp->ec_model = ec_fw_string[0]
7721 | (ec_fw_string[1] << 8);
7722 tp->ec_release = (ec_fw_string[4] << 8)
7723 | ec_fw_string[5];
7724 } else {
7725 printk(TPACPI_NOTICE
7726 "ThinkPad firmware release %s "
7727 "doesn't match the known patterns\n",
7728 ec_fw_string);
7729 printk(TPACPI_NOTICE
7730 "please report this to %s\n",
7731 TPACPI_MAIL);
7733 break;
7737 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7738 if (s && !strnicmp(s, "ThinkPad", 8)) {
7739 tp->model_str = kstrdup(s, GFP_KERNEL);
7740 if (!tp->model_str)
7741 return -ENOMEM;
7744 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7745 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7746 if (s && !tp->nummodel_str)
7747 return -ENOMEM;
7749 return 0;
7752 static int __init probe_for_thinkpad(void)
7754 int is_thinkpad;
7756 if (acpi_disabled)
7757 return -ENODEV;
7760 * Non-ancient models have better DMI tagging, but very old models
7761 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
7763 is_thinkpad = (thinkpad_id.model_str != NULL) ||
7764 (thinkpad_id.ec_model != 0) ||
7765 tpacpi_is_fw_known();
7767 /* ec is required because many other handles are relative to it */
7768 TPACPI_ACPIHANDLE_INIT(ec);
7769 if (!ec_handle) {
7770 if (is_thinkpad)
7771 printk(TPACPI_ERR
7772 "Not yet supported ThinkPad detected!\n");
7773 return -ENODEV;
7776 if (!is_thinkpad && !force_load)
7777 return -ENODEV;
7779 return 0;
7783 /* Module init, exit, parameters */
7785 static struct ibm_init_struct ibms_init[] __initdata = {
7787 .init = thinkpad_acpi_driver_init,
7788 .data = &thinkpad_acpi_driver_data,
7791 .init = hotkey_init,
7792 .data = &hotkey_driver_data,
7795 .init = bluetooth_init,
7796 .data = &bluetooth_driver_data,
7799 .init = wan_init,
7800 .data = &wan_driver_data,
7803 .init = uwb_init,
7804 .data = &uwb_driver_data,
7806 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7808 .init = video_init,
7809 .data = &video_driver_data,
7811 #endif
7813 .init = light_init,
7814 .data = &light_driver_data,
7817 .init = cmos_init,
7818 .data = &cmos_driver_data,
7821 .init = led_init,
7822 .data = &led_driver_data,
7825 .init = beep_init,
7826 .data = &beep_driver_data,
7829 .init = thermal_init,
7830 .data = &thermal_driver_data,
7833 .data = &ecdump_driver_data,
7836 .init = brightness_init,
7837 .data = &brightness_driver_data,
7840 .data = &volume_driver_data,
7843 .init = fan_init,
7844 .data = &fan_driver_data,
7848 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7850 unsigned int i;
7851 struct ibm_struct *ibm;
7853 if (!kp || !kp->name || !val)
7854 return -EINVAL;
7856 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7857 ibm = ibms_init[i].data;
7858 WARN_ON(ibm == NULL);
7860 if (!ibm || !ibm->name)
7861 continue;
7863 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7864 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7865 return -ENOSPC;
7866 strcpy(ibms_init[i].param, val);
7867 strcat(ibms_init[i].param, ",");
7868 return 0;
7872 return -EINVAL;
7875 module_param(experimental, int, 0);
7876 MODULE_PARM_DESC(experimental,
7877 "Enables experimental features when non-zero");
7879 module_param_named(debug, dbg_level, uint, 0);
7880 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7882 module_param(force_load, bool, 0);
7883 MODULE_PARM_DESC(force_load,
7884 "Attempts to load the driver even on a "
7885 "mis-identified ThinkPad when true");
7887 module_param_named(fan_control, fan_control_allowed, bool, 0);
7888 MODULE_PARM_DESC(fan_control,
7889 "Enables setting fan parameters features when true");
7891 module_param_named(brightness_mode, brightness_mode, uint, 0);
7892 MODULE_PARM_DESC(brightness_mode,
7893 "Selects brightness control strategy: "
7894 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7896 module_param(brightness_enable, uint, 0);
7897 MODULE_PARM_DESC(brightness_enable,
7898 "Enables backlight control when 1, disables when 0");
7900 module_param(hotkey_report_mode, uint, 0);
7901 MODULE_PARM_DESC(hotkey_report_mode,
7902 "used for backwards compatibility with userspace, "
7903 "see documentation");
7905 #define TPACPI_PARAM(feature) \
7906 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7907 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7908 "at module load, see documentation")
7910 TPACPI_PARAM(hotkey);
7911 TPACPI_PARAM(bluetooth);
7912 TPACPI_PARAM(video);
7913 TPACPI_PARAM(light);
7914 TPACPI_PARAM(cmos);
7915 TPACPI_PARAM(led);
7916 TPACPI_PARAM(beep);
7917 TPACPI_PARAM(ecdump);
7918 TPACPI_PARAM(brightness);
7919 TPACPI_PARAM(volume);
7920 TPACPI_PARAM(fan);
7922 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7923 module_param(dbg_wlswemul, uint, 0);
7924 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7925 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7926 MODULE_PARM_DESC(wlsw_state,
7927 "Initial state of the emulated WLSW switch");
7929 module_param(dbg_bluetoothemul, uint, 0);
7930 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7931 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7932 MODULE_PARM_DESC(bluetooth_state,
7933 "Initial state of the emulated bluetooth switch");
7935 module_param(dbg_wwanemul, uint, 0);
7936 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7937 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7938 MODULE_PARM_DESC(wwan_state,
7939 "Initial state of the emulated WWAN switch");
7941 module_param(dbg_uwbemul, uint, 0);
7942 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7943 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7944 MODULE_PARM_DESC(uwb_state,
7945 "Initial state of the emulated UWB switch");
7946 #endif
7948 static void thinkpad_acpi_module_exit(void)
7950 struct ibm_struct *ibm, *itmp;
7952 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7954 list_for_each_entry_safe_reverse(ibm, itmp,
7955 &tpacpi_all_drivers,
7956 all_drivers) {
7957 ibm_exit(ibm);
7960 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7962 if (tpacpi_inputdev) {
7963 if (tp_features.input_device_registered)
7964 input_unregister_device(tpacpi_inputdev);
7965 else
7966 input_free_device(tpacpi_inputdev);
7969 if (tpacpi_hwmon)
7970 hwmon_device_unregister(tpacpi_hwmon);
7972 if (tp_features.sensors_pdev_attrs_registered)
7973 device_remove_file(&tpacpi_sensors_pdev->dev,
7974 &dev_attr_thinkpad_acpi_pdev_name);
7975 if (tpacpi_sensors_pdev)
7976 platform_device_unregister(tpacpi_sensors_pdev);
7977 if (tpacpi_pdev)
7978 platform_device_unregister(tpacpi_pdev);
7980 if (tp_features.sensors_pdrv_attrs_registered)
7981 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7982 if (tp_features.platform_drv_attrs_registered)
7983 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7985 if (tp_features.sensors_pdrv_registered)
7986 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7988 if (tp_features.platform_drv_registered)
7989 platform_driver_unregister(&tpacpi_pdriver);
7991 if (proc_dir)
7992 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7994 if (tpacpi_wq)
7995 destroy_workqueue(tpacpi_wq);
7997 kfree(thinkpad_id.bios_version_str);
7998 kfree(thinkpad_id.ec_version_str);
7999 kfree(thinkpad_id.model_str);
8003 static int __init thinkpad_acpi_module_init(void)
8005 int ret, i;
8007 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8009 /* Parameter checking */
8010 if (hotkey_report_mode > 2)
8011 return -EINVAL;
8013 /* Driver-level probe */
8015 ret = get_thinkpad_model_data(&thinkpad_id);
8016 if (ret) {
8017 printk(TPACPI_ERR
8018 "unable to get DMI data: %d\n", ret);
8019 thinkpad_acpi_module_exit();
8020 return ret;
8022 ret = probe_for_thinkpad();
8023 if (ret) {
8024 thinkpad_acpi_module_exit();
8025 return ret;
8028 /* Driver initialization */
8030 TPACPI_ACPIHANDLE_INIT(ecrd);
8031 TPACPI_ACPIHANDLE_INIT(ecwr);
8033 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8034 if (!tpacpi_wq) {
8035 thinkpad_acpi_module_exit();
8036 return -ENOMEM;
8039 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8040 if (!proc_dir) {
8041 printk(TPACPI_ERR
8042 "unable to create proc dir " TPACPI_PROC_DIR);
8043 thinkpad_acpi_module_exit();
8044 return -ENODEV;
8047 ret = platform_driver_register(&tpacpi_pdriver);
8048 if (ret) {
8049 printk(TPACPI_ERR
8050 "unable to register main platform driver\n");
8051 thinkpad_acpi_module_exit();
8052 return ret;
8054 tp_features.platform_drv_registered = 1;
8056 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8057 if (ret) {
8058 printk(TPACPI_ERR
8059 "unable to register hwmon platform driver\n");
8060 thinkpad_acpi_module_exit();
8061 return ret;
8063 tp_features.sensors_pdrv_registered = 1;
8065 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8066 if (!ret) {
8067 tp_features.platform_drv_attrs_registered = 1;
8068 ret = tpacpi_create_driver_attributes(
8069 &tpacpi_hwmon_pdriver.driver);
8071 if (ret) {
8072 printk(TPACPI_ERR
8073 "unable to create sysfs driver attributes\n");
8074 thinkpad_acpi_module_exit();
8075 return ret;
8077 tp_features.sensors_pdrv_attrs_registered = 1;
8080 /* Device initialization */
8081 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8082 NULL, 0);
8083 if (IS_ERR(tpacpi_pdev)) {
8084 ret = PTR_ERR(tpacpi_pdev);
8085 tpacpi_pdev = NULL;
8086 printk(TPACPI_ERR "unable to register platform device\n");
8087 thinkpad_acpi_module_exit();
8088 return ret;
8090 tpacpi_sensors_pdev = platform_device_register_simple(
8091 TPACPI_HWMON_DRVR_NAME,
8092 -1, NULL, 0);
8093 if (IS_ERR(tpacpi_sensors_pdev)) {
8094 ret = PTR_ERR(tpacpi_sensors_pdev);
8095 tpacpi_sensors_pdev = NULL;
8096 printk(TPACPI_ERR
8097 "unable to register hwmon platform device\n");
8098 thinkpad_acpi_module_exit();
8099 return ret;
8101 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8102 &dev_attr_thinkpad_acpi_pdev_name);
8103 if (ret) {
8104 printk(TPACPI_ERR
8105 "unable to create sysfs hwmon device attributes\n");
8106 thinkpad_acpi_module_exit();
8107 return ret;
8109 tp_features.sensors_pdev_attrs_registered = 1;
8110 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8111 if (IS_ERR(tpacpi_hwmon)) {
8112 ret = PTR_ERR(tpacpi_hwmon);
8113 tpacpi_hwmon = NULL;
8114 printk(TPACPI_ERR "unable to register hwmon device\n");
8115 thinkpad_acpi_module_exit();
8116 return ret;
8118 mutex_init(&tpacpi_inputdev_send_mutex);
8119 tpacpi_inputdev = input_allocate_device();
8120 if (!tpacpi_inputdev) {
8121 printk(TPACPI_ERR "unable to allocate input device\n");
8122 thinkpad_acpi_module_exit();
8123 return -ENOMEM;
8124 } else {
8125 /* Prepare input device, but don't register */
8126 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8127 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8128 tpacpi_inputdev->id.bustype = BUS_HOST;
8129 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8130 thinkpad_id.vendor :
8131 PCI_VENDOR_ID_IBM;
8132 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8133 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8135 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8136 ret = ibm_init(&ibms_init[i]);
8137 if (ret >= 0 && *ibms_init[i].param)
8138 ret = ibms_init[i].data->write(ibms_init[i].param);
8139 if (ret < 0) {
8140 thinkpad_acpi_module_exit();
8141 return ret;
8144 ret = input_register_device(tpacpi_inputdev);
8145 if (ret < 0) {
8146 printk(TPACPI_ERR "unable to register input device\n");
8147 thinkpad_acpi_module_exit();
8148 return ret;
8149 } else {
8150 tp_features.input_device_registered = 1;
8153 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8154 return 0;
8157 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8160 * This will autoload the driver in almost every ThinkPad
8161 * in widespread use.
8163 * Only _VERY_ old models, like the 240, 240x and 570 lack
8164 * the HKEY event interface.
8166 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8169 * DMI matching for module autoloading
8171 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8172 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8174 * Only models listed in thinkwiki will be supported, so add yours
8175 * if it is not there yet.
8177 #define IBM_BIOS_MODULE_ALIAS(__type) \
8178 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8180 /* Ancient thinkpad BIOSes have to be identified by
8181 * BIOS type or model number, and there are far less
8182 * BIOS types than model numbers... */
8183 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8185 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8186 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8187 MODULE_DESCRIPTION(TPACPI_DESC);
8188 MODULE_VERSION(TPACPI_VERSION);
8189 MODULE_LICENSE("GPL");
8191 module_init(thinkpad_acpi_module_init);
8192 module_exit(thinkpad_acpi_module_exit);