thinkpad-acpi: remove dock and bay subdrivers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob27d68e719e90df73305f9bc43c477507b353ba27
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;
770 kernbuf = kmalloc(count + 2, GFP_KERNEL);
771 if (!kernbuf)
772 return -ENOMEM;
774 if (copy_from_user(kernbuf, userbuf, count)) {
775 kfree(kernbuf);
776 return -EFAULT;
779 kernbuf[count] = 0;
780 strcat(kernbuf, ",");
781 ret = ibm->write(kernbuf);
782 if (ret == 0)
783 ret = count;
785 kfree(kernbuf);
787 return ret;
790 static char *next_cmd(char **cmds)
792 char *start = *cmds;
793 char *end;
795 while ((end = strchr(start, ',')) && end == start)
796 start = end + 1;
798 if (!end)
799 return NULL;
801 *end = 0;
802 *cmds = end + 1;
803 return start;
807 /****************************************************************************
808 ****************************************************************************
810 * Device model: input, hwmon and platform
812 ****************************************************************************
813 ****************************************************************************/
815 static struct platform_device *tpacpi_pdev;
816 static struct platform_device *tpacpi_sensors_pdev;
817 static struct device *tpacpi_hwmon;
818 static struct input_dev *tpacpi_inputdev;
819 static struct mutex tpacpi_inputdev_send_mutex;
820 static LIST_HEAD(tpacpi_all_drivers);
822 static int tpacpi_suspend_handler(struct platform_device *pdev,
823 pm_message_t state)
825 struct ibm_struct *ibm, *itmp;
827 list_for_each_entry_safe(ibm, itmp,
828 &tpacpi_all_drivers,
829 all_drivers) {
830 if (ibm->suspend)
831 (ibm->suspend)(state);
834 return 0;
837 static int tpacpi_resume_handler(struct platform_device *pdev)
839 struct ibm_struct *ibm, *itmp;
841 list_for_each_entry_safe(ibm, itmp,
842 &tpacpi_all_drivers,
843 all_drivers) {
844 if (ibm->resume)
845 (ibm->resume)();
848 return 0;
851 static void tpacpi_shutdown_handler(struct platform_device *pdev)
853 struct ibm_struct *ibm, *itmp;
855 list_for_each_entry_safe(ibm, itmp,
856 &tpacpi_all_drivers,
857 all_drivers) {
858 if (ibm->shutdown)
859 (ibm->shutdown)();
863 static struct platform_driver tpacpi_pdriver = {
864 .driver = {
865 .name = TPACPI_DRVR_NAME,
866 .owner = THIS_MODULE,
868 .suspend = tpacpi_suspend_handler,
869 .resume = tpacpi_resume_handler,
870 .shutdown = tpacpi_shutdown_handler,
873 static struct platform_driver tpacpi_hwmon_pdriver = {
874 .driver = {
875 .name = TPACPI_HWMON_DRVR_NAME,
876 .owner = THIS_MODULE,
880 /*************************************************************************
881 * sysfs support helpers
884 struct attribute_set {
885 unsigned int members, max_members;
886 struct attribute_group group;
889 struct attribute_set_obj {
890 struct attribute_set s;
891 struct attribute *a;
892 } __attribute__((packed));
894 static struct attribute_set *create_attr_set(unsigned int max_members,
895 const char *name)
897 struct attribute_set_obj *sobj;
899 if (max_members == 0)
900 return NULL;
902 /* Allocates space for implicit NULL at the end too */
903 sobj = kzalloc(sizeof(struct attribute_set_obj) +
904 max_members * sizeof(struct attribute *),
905 GFP_KERNEL);
906 if (!sobj)
907 return NULL;
908 sobj->s.max_members = max_members;
909 sobj->s.group.attrs = &sobj->a;
910 sobj->s.group.name = name;
912 return &sobj->s;
915 #define destroy_attr_set(_set) \
916 kfree(_set);
918 /* not multi-threaded safe, use it in a single thread per set */
919 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
921 if (!s || !attr)
922 return -EINVAL;
924 if (s->members >= s->max_members)
925 return -ENOMEM;
927 s->group.attrs[s->members] = attr;
928 s->members++;
930 return 0;
933 static int add_many_to_attr_set(struct attribute_set *s,
934 struct attribute **attr,
935 unsigned int count)
937 int i, res;
939 for (i = 0; i < count; i++) {
940 res = add_to_attr_set(s, attr[i]);
941 if (res)
942 return res;
945 return 0;
948 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
950 sysfs_remove_group(kobj, &s->group);
951 destroy_attr_set(s);
954 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
955 sysfs_create_group(_kobj, &_attr_set->group)
957 static int parse_strtoul(const char *buf,
958 unsigned long max, unsigned long *value)
960 char *endp;
962 while (*buf && isspace(*buf))
963 buf++;
964 *value = simple_strtoul(buf, &endp, 0);
965 while (*endp && isspace(*endp))
966 endp++;
967 if (*endp || *value > max)
968 return -EINVAL;
970 return 0;
973 static void tpacpi_disable_brightness_delay(void)
975 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
976 printk(TPACPI_NOTICE
977 "ACPI backlight control delay disabled\n");
980 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
982 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
983 union acpi_object *obj;
984 int rc;
986 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
987 obj = (union acpi_object *)buffer.pointer;
988 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
989 printk(TPACPI_ERR "Unknown _BCL data, "
990 "please report this to %s\n", TPACPI_MAIL);
991 rc = 0;
992 } else {
993 rc = obj->package.count;
995 } else {
996 return 0;
999 kfree(buffer.pointer);
1000 return rc;
1003 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1004 u32 lvl, void *context, void **rv)
1006 char name[ACPI_PATH_SEGMENT_LENGTH];
1007 struct acpi_buffer buffer = { sizeof(name), &name };
1009 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1010 !strncmp("_BCL", name, sizeof(name) - 1)) {
1011 BUG_ON(!rv || !*rv);
1012 **(int **)rv = tpacpi_query_bcl_levels(handle);
1013 return AE_CTRL_TERMINATE;
1014 } else {
1015 return AE_OK;
1020 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1022 static int __init tpacpi_check_std_acpi_brightness_support(void)
1024 int status;
1025 int bcl_levels = 0;
1026 void *bcl_ptr = &bcl_levels;
1028 if (!vid_handle) {
1029 TPACPI_ACPIHANDLE_INIT(vid);
1031 if (!vid_handle)
1032 return 0;
1035 * Search for a _BCL method, and execute it. This is safe on all
1036 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1037 * BIOS in ACPI backlight control mode. We do NOT have to care
1038 * about calling the _BCL method in an enabled video device, any
1039 * will do for our purposes.
1042 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1043 tpacpi_acpi_walk_find_bcl, NULL,
1044 &bcl_ptr);
1046 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1047 tp_features.bright_acpimode = 1;
1048 return (bcl_levels - 2);
1051 return 0;
1054 static void printk_deprecated_attribute(const char * const what,
1055 const char * const details)
1057 tpacpi_log_usertask("deprecated sysfs attribute");
1058 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1059 "will be removed. %s\n",
1060 what, details);
1063 /*************************************************************************
1064 * rfkill and radio control support helpers
1068 * ThinkPad-ACPI firmware handling model:
1070 * WLSW (master wireless switch) is event-driven, and is common to all
1071 * firmware-controlled radios. It cannot be controlled, just monitored,
1072 * as expected. It overrides all radio state in firmware
1074 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1075 * (TODO: verify how WLSW interacts with the returned radio state).
1077 * The only time there are shadow radio state changes, is when
1078 * masked-off hotkeys are used.
1082 * Internal driver API for radio state:
1084 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1085 * bool: true means radio blocked (off)
1087 enum tpacpi_rfkill_state {
1088 TPACPI_RFK_RADIO_OFF = 0,
1089 TPACPI_RFK_RADIO_ON
1092 /* rfkill switches */
1093 enum tpacpi_rfk_id {
1094 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1095 TPACPI_RFK_WWAN_SW_ID,
1096 TPACPI_RFK_UWB_SW_ID,
1097 TPACPI_RFK_SW_MAX
1100 static const char *tpacpi_rfkill_names[] = {
1101 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1102 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1103 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1104 [TPACPI_RFK_SW_MAX] = NULL
1107 /* ThinkPad-ACPI rfkill subdriver */
1108 struct tpacpi_rfk {
1109 struct rfkill *rfkill;
1110 enum tpacpi_rfk_id id;
1111 const struct tpacpi_rfk_ops *ops;
1114 struct tpacpi_rfk_ops {
1115 /* firmware interface */
1116 int (*get_status)(void);
1117 int (*set_status)(const enum tpacpi_rfkill_state);
1120 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1122 /* Query FW and update rfkill sw state for a given rfkill switch */
1123 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1125 int status;
1127 if (!tp_rfk)
1128 return -ENODEV;
1130 status = (tp_rfk->ops->get_status)();
1131 if (status < 0)
1132 return status;
1134 rfkill_set_sw_state(tp_rfk->rfkill,
1135 (status == TPACPI_RFK_RADIO_OFF));
1137 return status;
1140 /* Query FW and update rfkill sw state for all rfkill switches */
1141 static void tpacpi_rfk_update_swstate_all(void)
1143 unsigned int i;
1145 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1146 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1150 * Sync the HW-blocking state of all rfkill switches,
1151 * do notice it causes the rfkill core to schedule uevents
1153 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1155 unsigned int i;
1156 struct tpacpi_rfk *tp_rfk;
1158 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1159 tp_rfk = tpacpi_rfkill_switches[i];
1160 if (tp_rfk) {
1161 if (rfkill_set_hw_state(tp_rfk->rfkill,
1162 blocked)) {
1163 /* ignore -- we track sw block */
1169 /* Call to get the WLSW state from the firmware */
1170 static int hotkey_get_wlsw(void);
1172 /* Call to query WLSW state and update all rfkill switches */
1173 static bool tpacpi_rfk_check_hwblock_state(void)
1175 int res = hotkey_get_wlsw();
1176 int hw_blocked;
1178 /* When unknown or unsupported, we have to assume it is unblocked */
1179 if (res < 0)
1180 return false;
1182 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1183 tpacpi_rfk_update_hwblock_state(hw_blocked);
1185 return hw_blocked;
1188 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1190 struct tpacpi_rfk *tp_rfk = data;
1191 int res;
1193 dbg_printk(TPACPI_DBG_RFKILL,
1194 "request to change radio state to %s\n",
1195 blocked ? "blocked" : "unblocked");
1197 /* try to set radio state */
1198 res = (tp_rfk->ops->set_status)(blocked ?
1199 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1201 /* and update the rfkill core with whatever the FW really did */
1202 tpacpi_rfk_update_swstate(tp_rfk);
1204 return (res < 0) ? res : 0;
1207 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1208 .set_block = tpacpi_rfk_hook_set_block,
1211 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1212 const struct tpacpi_rfk_ops *tp_rfkops,
1213 const enum rfkill_type rfktype,
1214 const char *name,
1215 const bool set_default)
1217 struct tpacpi_rfk *atp_rfk;
1218 int res;
1219 bool sw_state = false;
1220 int sw_status;
1222 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1224 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1225 if (atp_rfk)
1226 atp_rfk->rfkill = rfkill_alloc(name,
1227 &tpacpi_pdev->dev,
1228 rfktype,
1229 &tpacpi_rfk_rfkill_ops,
1230 atp_rfk);
1231 if (!atp_rfk || !atp_rfk->rfkill) {
1232 printk(TPACPI_ERR
1233 "failed to allocate memory for rfkill class\n");
1234 kfree(atp_rfk);
1235 return -ENOMEM;
1238 atp_rfk->id = id;
1239 atp_rfk->ops = tp_rfkops;
1241 sw_status = (tp_rfkops->get_status)();
1242 if (sw_status < 0) {
1243 printk(TPACPI_ERR
1244 "failed to read initial state for %s, error %d\n",
1245 name, sw_status);
1246 } else {
1247 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1248 if (set_default) {
1249 /* try to keep the initial state, since we ask the
1250 * firmware to preserve it across S5 in NVRAM */
1251 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1254 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
1256 res = rfkill_register(atp_rfk->rfkill);
1257 if (res < 0) {
1258 printk(TPACPI_ERR
1259 "failed to register %s rfkill switch: %d\n",
1260 name, res);
1261 rfkill_destroy(atp_rfk->rfkill);
1262 kfree(atp_rfk);
1263 return res;
1266 tpacpi_rfkill_switches[id] = atp_rfk;
1267 return 0;
1270 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1272 struct tpacpi_rfk *tp_rfk;
1274 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1276 tp_rfk = tpacpi_rfkill_switches[id];
1277 if (tp_rfk) {
1278 rfkill_unregister(tp_rfk->rfkill);
1279 tpacpi_rfkill_switches[id] = NULL;
1280 kfree(tp_rfk);
1284 static void printk_deprecated_rfkill_attribute(const char * const what)
1286 printk_deprecated_attribute(what,
1287 "Please switch to generic rfkill before year 2010");
1290 /* sysfs <radio> enable ------------------------------------------------ */
1291 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1292 struct device_attribute *attr,
1293 char *buf)
1295 int status;
1297 printk_deprecated_rfkill_attribute(attr->attr.name);
1299 /* This is in the ABI... */
1300 if (tpacpi_rfk_check_hwblock_state()) {
1301 status = TPACPI_RFK_RADIO_OFF;
1302 } else {
1303 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1304 if (status < 0)
1305 return status;
1308 return snprintf(buf, PAGE_SIZE, "%d\n",
1309 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1312 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1313 struct device_attribute *attr,
1314 const char *buf, size_t count)
1316 unsigned long t;
1317 int res;
1319 printk_deprecated_rfkill_attribute(attr->attr.name);
1321 if (parse_strtoul(buf, 1, &t))
1322 return -EINVAL;
1324 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1326 /* This is in the ABI... */
1327 if (tpacpi_rfk_check_hwblock_state() && !!t)
1328 return -EPERM;
1330 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1331 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1332 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1334 return (res < 0) ? res : count;
1337 /* procfs -------------------------------------------------------------- */
1338 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1340 int len = 0;
1342 if (id >= TPACPI_RFK_SW_MAX)
1343 len += sprintf(p + len, "status:\t\tnot supported\n");
1344 else {
1345 int status;
1347 /* This is in the ABI... */
1348 if (tpacpi_rfk_check_hwblock_state()) {
1349 status = TPACPI_RFK_RADIO_OFF;
1350 } else {
1351 status = tpacpi_rfk_update_swstate(
1352 tpacpi_rfkill_switches[id]);
1353 if (status < 0)
1354 return status;
1357 len += sprintf(p + len, "status:\t\t%s\n",
1358 (status == TPACPI_RFK_RADIO_ON) ?
1359 "enabled" : "disabled");
1360 len += sprintf(p + len, "commands:\tenable, disable\n");
1363 return len;
1366 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1368 char *cmd;
1369 int status = -1;
1370 int res = 0;
1372 if (id >= TPACPI_RFK_SW_MAX)
1373 return -ENODEV;
1375 while ((cmd = next_cmd(&buf))) {
1376 if (strlencmp(cmd, "enable") == 0)
1377 status = TPACPI_RFK_RADIO_ON;
1378 else if (strlencmp(cmd, "disable") == 0)
1379 status = TPACPI_RFK_RADIO_OFF;
1380 else
1381 return -EINVAL;
1384 if (status != -1) {
1385 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1386 (status == TPACPI_RFK_RADIO_ON) ?
1387 "enable" : "disable",
1388 tpacpi_rfkill_names[id]);
1389 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1390 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1393 return res;
1396 /*************************************************************************
1397 * thinkpad-acpi driver attributes
1400 /* interface_version --------------------------------------------------- */
1401 static ssize_t tpacpi_driver_interface_version_show(
1402 struct device_driver *drv,
1403 char *buf)
1405 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1408 static DRIVER_ATTR(interface_version, S_IRUGO,
1409 tpacpi_driver_interface_version_show, NULL);
1411 /* debug_level --------------------------------------------------------- */
1412 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1413 char *buf)
1415 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1418 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1419 const char *buf, size_t count)
1421 unsigned long t;
1423 if (parse_strtoul(buf, 0xffff, &t))
1424 return -EINVAL;
1426 dbg_level = t;
1428 return count;
1431 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1432 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1434 /* version ------------------------------------------------------------- */
1435 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1436 char *buf)
1438 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1439 TPACPI_DESC, TPACPI_VERSION);
1442 static DRIVER_ATTR(version, S_IRUGO,
1443 tpacpi_driver_version_show, NULL);
1445 /* --------------------------------------------------------------------- */
1447 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1449 /* wlsw_emulstate ------------------------------------------------------ */
1450 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1451 char *buf)
1453 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1456 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1457 const char *buf, size_t count)
1459 unsigned long t;
1461 if (parse_strtoul(buf, 1, &t))
1462 return -EINVAL;
1464 if (tpacpi_wlsw_emulstate != !!t) {
1465 tpacpi_wlsw_emulstate = !!t;
1466 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1469 return count;
1472 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1473 tpacpi_driver_wlsw_emulstate_show,
1474 tpacpi_driver_wlsw_emulstate_store);
1476 /* bluetooth_emulstate ------------------------------------------------- */
1477 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1478 struct device_driver *drv,
1479 char *buf)
1481 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1484 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1485 struct device_driver *drv,
1486 const char *buf, size_t count)
1488 unsigned long t;
1490 if (parse_strtoul(buf, 1, &t))
1491 return -EINVAL;
1493 tpacpi_bluetooth_emulstate = !!t;
1495 return count;
1498 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1499 tpacpi_driver_bluetooth_emulstate_show,
1500 tpacpi_driver_bluetooth_emulstate_store);
1502 /* wwan_emulstate ------------------------------------------------- */
1503 static ssize_t tpacpi_driver_wwan_emulstate_show(
1504 struct device_driver *drv,
1505 char *buf)
1507 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1510 static ssize_t tpacpi_driver_wwan_emulstate_store(
1511 struct device_driver *drv,
1512 const char *buf, size_t count)
1514 unsigned long t;
1516 if (parse_strtoul(buf, 1, &t))
1517 return -EINVAL;
1519 tpacpi_wwan_emulstate = !!t;
1521 return count;
1524 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1525 tpacpi_driver_wwan_emulstate_show,
1526 tpacpi_driver_wwan_emulstate_store);
1528 /* uwb_emulstate ------------------------------------------------- */
1529 static ssize_t tpacpi_driver_uwb_emulstate_show(
1530 struct device_driver *drv,
1531 char *buf)
1533 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1536 static ssize_t tpacpi_driver_uwb_emulstate_store(
1537 struct device_driver *drv,
1538 const char *buf, size_t count)
1540 unsigned long t;
1542 if (parse_strtoul(buf, 1, &t))
1543 return -EINVAL;
1545 tpacpi_uwb_emulstate = !!t;
1547 return count;
1550 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1551 tpacpi_driver_uwb_emulstate_show,
1552 tpacpi_driver_uwb_emulstate_store);
1553 #endif
1555 /* --------------------------------------------------------------------- */
1557 static struct driver_attribute *tpacpi_driver_attributes[] = {
1558 &driver_attr_debug_level, &driver_attr_version,
1559 &driver_attr_interface_version,
1562 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1564 int i, res;
1566 i = 0;
1567 res = 0;
1568 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1569 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1570 i++;
1573 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1574 if (!res && dbg_wlswemul)
1575 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1576 if (!res && dbg_bluetoothemul)
1577 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1578 if (!res && dbg_wwanemul)
1579 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1580 if (!res && dbg_uwbemul)
1581 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1582 #endif
1584 return res;
1587 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1589 int i;
1591 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1592 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1594 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1595 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1596 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1597 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1598 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1599 #endif
1602 /****************************************************************************
1603 ****************************************************************************
1605 * Subdrivers
1607 ****************************************************************************
1608 ****************************************************************************/
1610 /*************************************************************************
1611 * thinkpad-acpi init subdriver
1614 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1616 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1617 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1619 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1620 (thinkpad_id.bios_version_str) ?
1621 thinkpad_id.bios_version_str : "unknown",
1622 (thinkpad_id.ec_version_str) ?
1623 thinkpad_id.ec_version_str : "unknown");
1625 if (thinkpad_id.vendor && thinkpad_id.model_str)
1626 printk(TPACPI_INFO "%s %s, model %s\n",
1627 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1628 "IBM" : ((thinkpad_id.vendor ==
1629 PCI_VENDOR_ID_LENOVO) ?
1630 "Lenovo" : "Unknown vendor"),
1631 thinkpad_id.model_str,
1632 (thinkpad_id.nummodel_str) ?
1633 thinkpad_id.nummodel_str : "unknown");
1635 return 0;
1638 static int thinkpad_acpi_driver_read(char *p)
1640 int len = 0;
1642 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1643 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1645 return len;
1648 static struct ibm_struct thinkpad_acpi_driver_data = {
1649 .name = "driver",
1650 .read = thinkpad_acpi_driver_read,
1653 /*************************************************************************
1654 * Hotkey subdriver
1657 enum { /* hot key scan codes (derived from ACPI DSDT) */
1658 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1659 TP_ACPI_HOTKEYSCAN_FNF2,
1660 TP_ACPI_HOTKEYSCAN_FNF3,
1661 TP_ACPI_HOTKEYSCAN_FNF4,
1662 TP_ACPI_HOTKEYSCAN_FNF5,
1663 TP_ACPI_HOTKEYSCAN_FNF6,
1664 TP_ACPI_HOTKEYSCAN_FNF7,
1665 TP_ACPI_HOTKEYSCAN_FNF8,
1666 TP_ACPI_HOTKEYSCAN_FNF9,
1667 TP_ACPI_HOTKEYSCAN_FNF10,
1668 TP_ACPI_HOTKEYSCAN_FNF11,
1669 TP_ACPI_HOTKEYSCAN_FNF12,
1670 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1671 TP_ACPI_HOTKEYSCAN_FNINSERT,
1672 TP_ACPI_HOTKEYSCAN_FNDELETE,
1673 TP_ACPI_HOTKEYSCAN_FNHOME,
1674 TP_ACPI_HOTKEYSCAN_FNEND,
1675 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1676 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1677 TP_ACPI_HOTKEYSCAN_FNSPACE,
1678 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1679 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1680 TP_ACPI_HOTKEYSCAN_MUTE,
1681 TP_ACPI_HOTKEYSCAN_THINKPAD,
1684 enum { /* Keys available through NVRAM polling */
1685 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1686 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1689 enum { /* Positions of some of the keys in hotkey masks */
1690 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1691 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1692 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1693 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1694 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1695 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1696 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1697 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1698 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1699 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1700 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1703 enum { /* NVRAM to ACPI HKEY group map */
1704 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1705 TP_ACPI_HKEY_ZOOM_MASK |
1706 TP_ACPI_HKEY_DISPSWTCH_MASK |
1707 TP_ACPI_HKEY_HIBERNATE_MASK,
1708 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1709 TP_ACPI_HKEY_BRGHTDWN_MASK,
1710 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1711 TP_ACPI_HKEY_VOLDWN_MASK |
1712 TP_ACPI_HKEY_MUTE_MASK,
1715 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1716 struct tp_nvram_state {
1717 u16 thinkpad_toggle:1;
1718 u16 zoom_toggle:1;
1719 u16 display_toggle:1;
1720 u16 thinklight_toggle:1;
1721 u16 hibernate_toggle:1;
1722 u16 displayexp_toggle:1;
1723 u16 display_state:1;
1724 u16 brightness_toggle:1;
1725 u16 volume_toggle:1;
1726 u16 mute:1;
1728 u8 brightness_level;
1729 u8 volume_level;
1732 static struct task_struct *tpacpi_hotkey_task;
1733 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1734 static int hotkey_poll_freq = 10; /* Hz */
1735 static struct mutex hotkey_thread_mutex;
1736 static struct mutex hotkey_thread_data_mutex;
1737 static unsigned int hotkey_config_change;
1739 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1741 #define hotkey_source_mask 0U
1743 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1745 static struct mutex hotkey_mutex;
1747 static enum { /* Reasons for waking up */
1748 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1749 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1750 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1751 } hotkey_wakeup_reason;
1753 static int hotkey_autosleep_ack;
1755 static u32 hotkey_orig_mask;
1756 static u32 hotkey_all_mask;
1757 static u32 hotkey_reserved_mask;
1758 static u32 hotkey_mask;
1760 static unsigned int hotkey_report_mode;
1762 static u16 *hotkey_keycode_map;
1764 static struct attribute_set *hotkey_dev_attributes;
1766 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1767 #define HOTKEY_CONFIG_CRITICAL_START \
1768 do { \
1769 mutex_lock(&hotkey_thread_data_mutex); \
1770 hotkey_config_change++; \
1771 } while (0);
1772 #define HOTKEY_CONFIG_CRITICAL_END \
1773 mutex_unlock(&hotkey_thread_data_mutex);
1774 #else
1775 #define HOTKEY_CONFIG_CRITICAL_START
1776 #define HOTKEY_CONFIG_CRITICAL_END
1777 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1779 /* HKEY.MHKG() return bits */
1780 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1782 static int hotkey_get_wlsw(void)
1784 int status;
1786 if (!tp_features.hotkey_wlsw)
1787 return -ENODEV;
1789 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1790 if (dbg_wlswemul)
1791 return (tpacpi_wlsw_emulstate) ?
1792 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1793 #endif
1795 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1796 return -EIO;
1798 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1801 static int hotkey_get_tablet_mode(int *status)
1803 int s;
1805 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1806 return -EIO;
1808 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1809 return 0;
1813 * Call with hotkey_mutex held
1815 static int hotkey_mask_get(void)
1817 u32 m = 0;
1819 if (tp_features.hotkey_mask) {
1820 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1821 return -EIO;
1823 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1825 return 0;
1829 * Call with hotkey_mutex held
1831 static int hotkey_mask_set(u32 mask)
1833 int i;
1834 int rc = 0;
1836 if (tp_features.hotkey_mask) {
1837 if (!tp_warned.hotkey_mask_ff &&
1838 (mask == 0xffff || mask == 0xffffff ||
1839 mask == 0xffffffff)) {
1840 tp_warned.hotkey_mask_ff = 1;
1841 printk(TPACPI_NOTICE
1842 "setting the hotkey mask to 0x%08x is likely "
1843 "not the best way to go about it\n", mask);
1844 printk(TPACPI_NOTICE
1845 "please consider using the driver defaults, "
1846 "and refer to up-to-date thinkpad-acpi "
1847 "documentation\n");
1850 HOTKEY_CONFIG_CRITICAL_START
1851 for (i = 0; i < 32; i++) {
1852 u32 m = 1 << i;
1853 /* enable in firmware mask only keys not in NVRAM
1854 * mode, but enable the key in the cached hotkey_mask
1855 * regardless of mode, or the key will end up
1856 * disabled by hotkey_mask_get() */
1857 if (!acpi_evalf(hkey_handle,
1858 NULL, "MHKM", "vdd", i + 1,
1859 !!((mask & ~hotkey_source_mask) & m))) {
1860 rc = -EIO;
1861 break;
1862 } else {
1863 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1866 HOTKEY_CONFIG_CRITICAL_END
1868 /* hotkey_mask_get must be called unconditionally below */
1869 if (!hotkey_mask_get() && !rc &&
1870 (hotkey_mask & ~hotkey_source_mask) !=
1871 (mask & ~hotkey_source_mask)) {
1872 printk(TPACPI_NOTICE
1873 "requested hot key mask 0x%08x, but "
1874 "firmware forced it to 0x%08x\n",
1875 mask, hotkey_mask);
1877 } else {
1878 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1879 HOTKEY_CONFIG_CRITICAL_START
1880 hotkey_mask = mask & hotkey_source_mask;
1881 HOTKEY_CONFIG_CRITICAL_END
1882 hotkey_mask_get();
1883 if (hotkey_mask != mask) {
1884 printk(TPACPI_NOTICE
1885 "requested hot key mask 0x%08x, "
1886 "forced to 0x%08x (NVRAM poll mask is "
1887 "0x%08x): no firmware mask support\n",
1888 mask, hotkey_mask, hotkey_source_mask);
1890 #else
1891 hotkey_mask_get();
1892 rc = -ENXIO;
1893 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1896 return rc;
1899 static int hotkey_status_get(int *status)
1901 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1902 return -EIO;
1904 return 0;
1907 static int hotkey_status_set(bool enable)
1909 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1910 return -EIO;
1912 return 0;
1915 static void tpacpi_input_send_tabletsw(void)
1917 int state;
1919 if (tp_features.hotkey_tablet &&
1920 !hotkey_get_tablet_mode(&state)) {
1921 mutex_lock(&tpacpi_inputdev_send_mutex);
1923 input_report_switch(tpacpi_inputdev,
1924 SW_TABLET_MODE, !!state);
1925 input_sync(tpacpi_inputdev);
1927 mutex_unlock(&tpacpi_inputdev_send_mutex);
1931 static void tpacpi_input_send_key(unsigned int scancode)
1933 unsigned int keycode;
1935 keycode = hotkey_keycode_map[scancode];
1937 if (keycode != KEY_RESERVED) {
1938 mutex_lock(&tpacpi_inputdev_send_mutex);
1940 input_report_key(tpacpi_inputdev, keycode, 1);
1941 if (keycode == KEY_UNKNOWN)
1942 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1943 scancode);
1944 input_sync(tpacpi_inputdev);
1946 input_report_key(tpacpi_inputdev, keycode, 0);
1947 if (keycode == KEY_UNKNOWN)
1948 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1949 scancode);
1950 input_sync(tpacpi_inputdev);
1952 mutex_unlock(&tpacpi_inputdev_send_mutex);
1956 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1957 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1959 static void tpacpi_hotkey_send_key(unsigned int scancode)
1961 tpacpi_input_send_key(scancode);
1962 if (hotkey_report_mode < 2) {
1963 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1964 0x80, 0x1001 + scancode);
1968 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1970 u8 d;
1972 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1973 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1974 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1975 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1976 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1977 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1979 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1980 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1981 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1983 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1984 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1985 n->displayexp_toggle =
1986 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1988 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1989 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1990 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1991 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1992 n->brightness_toggle =
1993 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1995 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1996 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1997 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1998 >> TP_NVRAM_POS_LEVEL_VOLUME;
1999 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2000 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2004 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2005 do { \
2006 if ((mask & (1 << __scancode)) && \
2007 oldn->__member != newn->__member) \
2008 tpacpi_hotkey_send_key(__scancode); \
2009 } while (0)
2011 #define TPACPI_MAY_SEND_KEY(__scancode) \
2012 do { if (mask & (1 << __scancode)) \
2013 tpacpi_hotkey_send_key(__scancode); } while (0)
2015 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2016 struct tp_nvram_state *newn,
2017 u32 mask)
2019 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2020 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2021 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2022 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2024 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2026 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2028 /* handle volume */
2029 if (oldn->volume_toggle != newn->volume_toggle) {
2030 if (oldn->mute != newn->mute) {
2031 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2033 if (oldn->volume_level > newn->volume_level) {
2034 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2035 } else if (oldn->volume_level < newn->volume_level) {
2036 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2037 } else if (oldn->mute == newn->mute) {
2038 /* repeated key presses that didn't change state */
2039 if (newn->mute) {
2040 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2041 } else if (newn->volume_level != 0) {
2042 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2043 } else {
2044 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2049 /* handle brightness */
2050 if (oldn->brightness_toggle != newn->brightness_toggle) {
2051 if (oldn->brightness_level < newn->brightness_level) {
2052 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2053 } else if (oldn->brightness_level > newn->brightness_level) {
2054 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2055 } else {
2056 /* repeated key presses that didn't change state */
2057 if (newn->brightness_level != 0) {
2058 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2059 } else {
2060 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2066 #undef TPACPI_COMPARE_KEY
2067 #undef TPACPI_MAY_SEND_KEY
2069 static int hotkey_kthread(void *data)
2071 struct tp_nvram_state s[2];
2072 u32 mask;
2073 unsigned int si, so;
2074 unsigned long t;
2075 unsigned int change_detector, must_reset;
2077 mutex_lock(&hotkey_thread_mutex);
2079 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2080 goto exit;
2082 set_freezable();
2084 so = 0;
2085 si = 1;
2086 t = 0;
2088 /* Initial state for compares */
2089 mutex_lock(&hotkey_thread_data_mutex);
2090 change_detector = hotkey_config_change;
2091 mask = hotkey_source_mask & hotkey_mask;
2092 mutex_unlock(&hotkey_thread_data_mutex);
2093 hotkey_read_nvram(&s[so], mask);
2095 while (!kthread_should_stop() && hotkey_poll_freq) {
2096 if (t == 0)
2097 t = 1000/hotkey_poll_freq;
2098 t = msleep_interruptible(t);
2099 if (unlikely(kthread_should_stop()))
2100 break;
2101 must_reset = try_to_freeze();
2102 if (t > 0 && !must_reset)
2103 continue;
2105 mutex_lock(&hotkey_thread_data_mutex);
2106 if (must_reset || hotkey_config_change != change_detector) {
2107 /* forget old state on thaw or config change */
2108 si = so;
2109 t = 0;
2110 change_detector = hotkey_config_change;
2112 mask = hotkey_source_mask & hotkey_mask;
2113 mutex_unlock(&hotkey_thread_data_mutex);
2115 if (likely(mask)) {
2116 hotkey_read_nvram(&s[si], mask);
2117 if (likely(si != so)) {
2118 hotkey_compare_and_issue_event(&s[so], &s[si],
2119 mask);
2123 so = si;
2124 si ^= 1;
2127 exit:
2128 mutex_unlock(&hotkey_thread_mutex);
2129 return 0;
2132 static void hotkey_poll_stop_sync(void)
2134 if (tpacpi_hotkey_task) {
2135 if (frozen(tpacpi_hotkey_task) ||
2136 freezing(tpacpi_hotkey_task))
2137 thaw_process(tpacpi_hotkey_task);
2139 kthread_stop(tpacpi_hotkey_task);
2140 tpacpi_hotkey_task = NULL;
2141 mutex_lock(&hotkey_thread_mutex);
2142 /* at this point, the thread did exit */
2143 mutex_unlock(&hotkey_thread_mutex);
2147 /* call with hotkey_mutex held */
2148 static void hotkey_poll_setup(int may_warn)
2150 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2151 hotkey_poll_freq > 0 &&
2152 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2153 if (!tpacpi_hotkey_task) {
2154 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2155 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2156 if (IS_ERR(tpacpi_hotkey_task)) {
2157 tpacpi_hotkey_task = NULL;
2158 printk(TPACPI_ERR
2159 "could not create kernel thread "
2160 "for hotkey polling\n");
2163 } else {
2164 hotkey_poll_stop_sync();
2165 if (may_warn &&
2166 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
2167 printk(TPACPI_NOTICE
2168 "hot keys 0x%08x require polling, "
2169 "which is currently disabled\n",
2170 hotkey_source_mask);
2175 static void hotkey_poll_setup_safe(int may_warn)
2177 mutex_lock(&hotkey_mutex);
2178 hotkey_poll_setup(may_warn);
2179 mutex_unlock(&hotkey_mutex);
2182 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2184 static void hotkey_poll_setup_safe(int __unused)
2188 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2190 static int hotkey_inputdev_open(struct input_dev *dev)
2192 switch (tpacpi_lifecycle) {
2193 case TPACPI_LIFE_INIT:
2195 * hotkey_init will call hotkey_poll_setup_safe
2196 * at the appropriate moment
2198 return 0;
2199 case TPACPI_LIFE_EXITING:
2200 return -EBUSY;
2201 case TPACPI_LIFE_RUNNING:
2202 hotkey_poll_setup_safe(0);
2203 return 0;
2206 /* Should only happen if tpacpi_lifecycle is corrupt */
2207 BUG();
2208 return -EBUSY;
2211 static void hotkey_inputdev_close(struct input_dev *dev)
2213 /* disable hotkey polling when possible */
2214 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2215 hotkey_poll_setup_safe(0);
2218 /* sysfs hotkey enable ------------------------------------------------- */
2219 static ssize_t hotkey_enable_show(struct device *dev,
2220 struct device_attribute *attr,
2221 char *buf)
2223 int res, status;
2225 printk_deprecated_attribute("hotkey_enable",
2226 "Hotkey reporting is always enabled");
2228 res = hotkey_status_get(&status);
2229 if (res)
2230 return res;
2232 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2235 static ssize_t hotkey_enable_store(struct device *dev,
2236 struct device_attribute *attr,
2237 const char *buf, size_t count)
2239 unsigned long t;
2241 printk_deprecated_attribute("hotkey_enable",
2242 "Hotkeys can be disabled through hotkey_mask");
2244 if (parse_strtoul(buf, 1, &t))
2245 return -EINVAL;
2247 if (t == 0)
2248 return -EPERM;
2250 return count;
2253 static struct device_attribute dev_attr_hotkey_enable =
2254 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2255 hotkey_enable_show, hotkey_enable_store);
2257 /* sysfs hotkey mask --------------------------------------------------- */
2258 static ssize_t hotkey_mask_show(struct device *dev,
2259 struct device_attribute *attr,
2260 char *buf)
2262 int res;
2264 if (mutex_lock_killable(&hotkey_mutex))
2265 return -ERESTARTSYS;
2266 res = hotkey_mask_get();
2267 mutex_unlock(&hotkey_mutex);
2269 return (res)?
2270 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2273 static ssize_t hotkey_mask_store(struct device *dev,
2274 struct device_attribute *attr,
2275 const char *buf, size_t count)
2277 unsigned long t;
2278 int res;
2280 if (parse_strtoul(buf, 0xffffffffUL, &t))
2281 return -EINVAL;
2283 if (mutex_lock_killable(&hotkey_mutex))
2284 return -ERESTARTSYS;
2286 res = hotkey_mask_set(t);
2288 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2289 hotkey_poll_setup(1);
2290 #endif
2292 mutex_unlock(&hotkey_mutex);
2294 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2296 return (res) ? res : count;
2299 static struct device_attribute dev_attr_hotkey_mask =
2300 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2301 hotkey_mask_show, hotkey_mask_store);
2303 /* sysfs hotkey bios_enabled ------------------------------------------- */
2304 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2305 struct device_attribute *attr,
2306 char *buf)
2308 return sprintf(buf, "0\n");
2311 static struct device_attribute dev_attr_hotkey_bios_enabled =
2312 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2314 /* sysfs hotkey bios_mask ---------------------------------------------- */
2315 static ssize_t hotkey_bios_mask_show(struct device *dev,
2316 struct device_attribute *attr,
2317 char *buf)
2319 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2322 static struct device_attribute dev_attr_hotkey_bios_mask =
2323 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2325 /* sysfs hotkey all_mask ----------------------------------------------- */
2326 static ssize_t hotkey_all_mask_show(struct device *dev,
2327 struct device_attribute *attr,
2328 char *buf)
2330 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2331 hotkey_all_mask | hotkey_source_mask);
2334 static struct device_attribute dev_attr_hotkey_all_mask =
2335 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2337 /* sysfs hotkey recommended_mask --------------------------------------- */
2338 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2339 struct device_attribute *attr,
2340 char *buf)
2342 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2343 (hotkey_all_mask | hotkey_source_mask)
2344 & ~hotkey_reserved_mask);
2347 static struct device_attribute dev_attr_hotkey_recommended_mask =
2348 __ATTR(hotkey_recommended_mask, S_IRUGO,
2349 hotkey_recommended_mask_show, NULL);
2351 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2353 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2354 static ssize_t hotkey_source_mask_show(struct device *dev,
2355 struct device_attribute *attr,
2356 char *buf)
2358 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2361 static ssize_t hotkey_source_mask_store(struct device *dev,
2362 struct device_attribute *attr,
2363 const char *buf, size_t count)
2365 unsigned long t;
2367 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2368 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2369 return -EINVAL;
2371 if (mutex_lock_killable(&hotkey_mutex))
2372 return -ERESTARTSYS;
2374 HOTKEY_CONFIG_CRITICAL_START
2375 hotkey_source_mask = t;
2376 HOTKEY_CONFIG_CRITICAL_END
2378 hotkey_poll_setup(1);
2380 mutex_unlock(&hotkey_mutex);
2382 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2384 return count;
2387 static struct device_attribute dev_attr_hotkey_source_mask =
2388 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2389 hotkey_source_mask_show, hotkey_source_mask_store);
2391 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2392 static ssize_t hotkey_poll_freq_show(struct device *dev,
2393 struct device_attribute *attr,
2394 char *buf)
2396 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2399 static ssize_t hotkey_poll_freq_store(struct device *dev,
2400 struct device_attribute *attr,
2401 const char *buf, size_t count)
2403 unsigned long t;
2405 if (parse_strtoul(buf, 25, &t))
2406 return -EINVAL;
2408 if (mutex_lock_killable(&hotkey_mutex))
2409 return -ERESTARTSYS;
2411 hotkey_poll_freq = t;
2413 hotkey_poll_setup(1);
2414 mutex_unlock(&hotkey_mutex);
2416 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2418 return count;
2421 static struct device_attribute dev_attr_hotkey_poll_freq =
2422 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2423 hotkey_poll_freq_show, hotkey_poll_freq_store);
2425 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2427 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2428 static ssize_t hotkey_radio_sw_show(struct device *dev,
2429 struct device_attribute *attr,
2430 char *buf)
2432 int res;
2433 res = hotkey_get_wlsw();
2434 if (res < 0)
2435 return res;
2437 /* Opportunistic update */
2438 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2440 return snprintf(buf, PAGE_SIZE, "%d\n",
2441 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2444 static struct device_attribute dev_attr_hotkey_radio_sw =
2445 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2447 static void hotkey_radio_sw_notify_change(void)
2449 if (tp_features.hotkey_wlsw)
2450 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2451 "hotkey_radio_sw");
2454 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2455 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2456 struct device_attribute *attr,
2457 char *buf)
2459 int res, s;
2460 res = hotkey_get_tablet_mode(&s);
2461 if (res < 0)
2462 return res;
2464 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2467 static struct device_attribute dev_attr_hotkey_tablet_mode =
2468 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2470 static void hotkey_tablet_mode_notify_change(void)
2472 if (tp_features.hotkey_tablet)
2473 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2474 "hotkey_tablet_mode");
2477 /* sysfs hotkey report_mode -------------------------------------------- */
2478 static ssize_t hotkey_report_mode_show(struct device *dev,
2479 struct device_attribute *attr,
2480 char *buf)
2482 return snprintf(buf, PAGE_SIZE, "%d\n",
2483 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2486 static struct device_attribute dev_attr_hotkey_report_mode =
2487 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2489 /* sysfs wakeup reason (pollable) -------------------------------------- */
2490 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2491 struct device_attribute *attr,
2492 char *buf)
2494 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2497 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2498 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2500 static void hotkey_wakeup_reason_notify_change(void)
2502 if (tp_features.hotkey_mask)
2503 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2504 "wakeup_reason");
2507 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2508 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2509 struct device_attribute *attr,
2510 char *buf)
2512 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2515 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2516 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2517 hotkey_wakeup_hotunplug_complete_show, NULL);
2519 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2521 if (tp_features.hotkey_mask)
2522 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2523 "wakeup_hotunplug_complete");
2526 /* --------------------------------------------------------------------- */
2528 static struct attribute *hotkey_attributes[] __initdata = {
2529 &dev_attr_hotkey_enable.attr,
2530 &dev_attr_hotkey_bios_enabled.attr,
2531 &dev_attr_hotkey_report_mode.attr,
2532 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2533 &dev_attr_hotkey_mask.attr,
2534 &dev_attr_hotkey_all_mask.attr,
2535 &dev_attr_hotkey_recommended_mask.attr,
2536 &dev_attr_hotkey_source_mask.attr,
2537 &dev_attr_hotkey_poll_freq.attr,
2538 #endif
2541 static struct attribute *hotkey_mask_attributes[] __initdata = {
2542 &dev_attr_hotkey_bios_mask.attr,
2543 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2544 &dev_attr_hotkey_mask.attr,
2545 &dev_attr_hotkey_all_mask.attr,
2546 &dev_attr_hotkey_recommended_mask.attr,
2547 #endif
2548 &dev_attr_hotkey_wakeup_reason.attr,
2549 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2553 * Sync both the hw and sw blocking state of all switches
2555 static void tpacpi_send_radiosw_update(void)
2557 int wlsw;
2560 * We must sync all rfkill controllers *before* issuing any
2561 * rfkill input events, or we will race the rfkill core input
2562 * handler.
2564 * tpacpi_inputdev_send_mutex works as a syncronization point
2565 * for the above.
2567 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2570 wlsw = hotkey_get_wlsw();
2572 /* Sync hw blocking state first if it is hw-blocked */
2573 if (wlsw == TPACPI_RFK_RADIO_OFF)
2574 tpacpi_rfk_update_hwblock_state(true);
2576 /* Sync sw blocking state */
2577 tpacpi_rfk_update_swstate_all();
2579 /* Sync hw blocking state last if it is hw-unblocked */
2580 if (wlsw == TPACPI_RFK_RADIO_ON)
2581 tpacpi_rfk_update_hwblock_state(false);
2583 /* Issue rfkill input event for WLSW switch */
2584 if (!(wlsw < 0)) {
2585 mutex_lock(&tpacpi_inputdev_send_mutex);
2587 input_report_switch(tpacpi_inputdev,
2588 SW_RFKILL_ALL, (wlsw > 0));
2589 input_sync(tpacpi_inputdev);
2591 mutex_unlock(&tpacpi_inputdev_send_mutex);
2595 * this can be unconditional, as we will poll state again
2596 * if userspace uses the notify to read data
2598 hotkey_radio_sw_notify_change();
2601 static void hotkey_exit(void)
2603 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2604 hotkey_poll_stop_sync();
2605 #endif
2607 if (hotkey_dev_attributes)
2608 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2610 kfree(hotkey_keycode_map);
2612 if (tp_features.hotkey) {
2613 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2614 "restoring original hot key mask\n");
2615 /* no short-circuit boolean operator below! */
2616 if ((hotkey_mask_set(hotkey_orig_mask) |
2617 hotkey_status_set(false)) != 0)
2618 printk(TPACPI_ERR
2619 "failed to restore hot key mask "
2620 "to BIOS defaults\n");
2624 static int __init hotkey_init(struct ibm_init_struct *iibm)
2626 /* Requirements for changing the default keymaps:
2628 * 1. Many of the keys are mapped to KEY_RESERVED for very
2629 * good reasons. Do not change them unless you have deep
2630 * knowledge on the IBM and Lenovo ThinkPad firmware for
2631 * the various ThinkPad models. The driver behaves
2632 * differently for KEY_RESERVED: such keys have their
2633 * hot key mask *unset* in mask_recommended, and also
2634 * in the initial hot key mask programmed into the
2635 * firmware at driver load time, which means the firm-
2636 * ware may react very differently if you change them to
2637 * something else;
2639 * 2. You must be subscribed to the linux-thinkpad and
2640 * ibm-acpi-devel mailing lists, and you should read the
2641 * list archives since 2007 if you want to change the
2642 * keymaps. This requirement exists so that you will
2643 * know the past history of problems with the thinkpad-
2644 * acpi driver keymaps, and also that you will be
2645 * listening to any bug reports;
2647 * 3. Do not send thinkpad-acpi specific patches directly to
2648 * for merging, *ever*. Send them to the linux-acpi
2649 * mailinglist for comments. Merging is to be done only
2650 * through acpi-test and the ACPI maintainer.
2652 * If the above is too much to ask, don't change the keymap.
2653 * Ask the thinkpad-acpi maintainer to do it, instead.
2655 static u16 ibm_keycode_map[] __initdata = {
2656 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2657 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2658 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2659 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2661 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2662 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2663 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2664 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2666 /* brightness: firmware always reacts to them, unless
2667 * X.org did some tricks in the radeon BIOS scratch
2668 * registers of *some* models */
2669 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2670 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2672 /* Thinklight: firmware always react to it */
2673 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2675 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2676 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2678 /* Volume: firmware always react to it and reprograms
2679 * the built-in *extra* mixer. Never map it to control
2680 * another mixer by default. */
2681 KEY_RESERVED, /* 0x14: VOLUME UP */
2682 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2683 KEY_RESERVED, /* 0x16: MUTE */
2685 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2687 /* (assignments unknown, please report if found) */
2688 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2689 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2691 static u16 lenovo_keycode_map[] __initdata = {
2692 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2693 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2694 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2695 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2697 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2698 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2699 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2700 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2702 /* These either have to go through ACPI video, or
2703 * act like in the IBM ThinkPads, so don't ever
2704 * enable them by default */
2705 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2706 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2708 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2710 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2711 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2713 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2714 * react to it and reprograms the built-in *extra* mixer.
2715 * Never map it to control another mixer by default.
2717 * T60?, T61, R60?, R61: firmware and EC tries to send
2718 * these over the regular keyboard, so these are no-ops,
2719 * but there are still weird bugs re. MUTE, so do not
2720 * change unless you get test reports from all Lenovo
2721 * models. May cause the BIOS to interfere with the
2722 * HDA mixer.
2724 KEY_RESERVED, /* 0x14: VOLUME UP */
2725 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2726 KEY_RESERVED, /* 0x16: MUTE */
2728 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2730 /* (assignments unknown, please report if found) */
2731 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2732 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2735 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2736 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2737 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2739 int res, i;
2740 int status;
2741 int hkeyv;
2743 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2744 "initializing hotkey subdriver\n");
2746 BUG_ON(!tpacpi_inputdev);
2747 BUG_ON(tpacpi_inputdev->open != NULL ||
2748 tpacpi_inputdev->close != NULL);
2750 TPACPI_ACPIHANDLE_INIT(hkey);
2751 mutex_init(&hotkey_mutex);
2753 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2754 mutex_init(&hotkey_thread_mutex);
2755 mutex_init(&hotkey_thread_data_mutex);
2756 #endif
2758 /* hotkey not supported on 570 */
2759 tp_features.hotkey = hkey_handle != NULL;
2761 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2762 "hotkeys are %s\n",
2763 str_supported(tp_features.hotkey));
2765 if (!tp_features.hotkey)
2766 return 1;
2768 tpacpi_disable_brightness_delay();
2770 hotkey_dev_attributes = create_attr_set(13, NULL);
2771 if (!hotkey_dev_attributes)
2772 return -ENOMEM;
2773 res = add_many_to_attr_set(hotkey_dev_attributes,
2774 hotkey_attributes,
2775 ARRAY_SIZE(hotkey_attributes));
2776 if (res)
2777 goto err_exit;
2779 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2780 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2781 for HKEY interface version 0x100 */
2782 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2783 if ((hkeyv >> 8) != 1) {
2784 printk(TPACPI_ERR "unknown version of the "
2785 "HKEY interface: 0x%x\n", hkeyv);
2786 printk(TPACPI_ERR "please report this to %s\n",
2787 TPACPI_MAIL);
2788 } else {
2790 * MHKV 0x100 in A31, R40, R40e,
2791 * T4x, X31, and later
2793 tp_features.hotkey_mask = 1;
2794 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2795 "firmware HKEY interface version: 0x%x\n",
2796 hkeyv);
2800 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2801 "hotkey masks are %s\n",
2802 str_supported(tp_features.hotkey_mask));
2804 if (tp_features.hotkey_mask) {
2805 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2806 "MHKA", "qd")) {
2807 printk(TPACPI_ERR
2808 "missing MHKA handler, "
2809 "please report this to %s\n",
2810 TPACPI_MAIL);
2811 /* FN+F12, FN+F4, FN+F3 */
2812 hotkey_all_mask = 0x080cU;
2816 /* hotkey_source_mask *must* be zero for
2817 * the first hotkey_mask_get */
2818 if (tp_features.hotkey_mask) {
2819 res = hotkey_mask_get();
2820 if (res)
2821 goto err_exit;
2823 hotkey_orig_mask = hotkey_mask;
2824 res = add_many_to_attr_set(
2825 hotkey_dev_attributes,
2826 hotkey_mask_attributes,
2827 ARRAY_SIZE(hotkey_mask_attributes));
2828 if (res)
2829 goto err_exit;
2832 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2833 if (tp_features.hotkey_mask) {
2834 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2835 & ~hotkey_all_mask;
2836 } else {
2837 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2840 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2841 "hotkey source mask 0x%08x, polling freq %d\n",
2842 hotkey_source_mask, hotkey_poll_freq);
2843 #endif
2845 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2846 if (dbg_wlswemul) {
2847 tp_features.hotkey_wlsw = 1;
2848 printk(TPACPI_INFO
2849 "radio switch emulation enabled\n");
2850 } else
2851 #endif
2852 /* Not all thinkpads have a hardware radio switch */
2853 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2854 tp_features.hotkey_wlsw = 1;
2855 printk(TPACPI_INFO
2856 "radio switch found; radios are %s\n",
2857 enabled(status, 0));
2859 if (tp_features.hotkey_wlsw)
2860 res = add_to_attr_set(hotkey_dev_attributes,
2861 &dev_attr_hotkey_radio_sw.attr);
2863 /* For X41t, X60t, X61t Tablets... */
2864 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2865 tp_features.hotkey_tablet = 1;
2866 printk(TPACPI_INFO
2867 "possible tablet mode switch found; "
2868 "ThinkPad in %s mode\n",
2869 (status & TP_HOTKEY_TABLET_MASK)?
2870 "tablet" : "laptop");
2871 res = add_to_attr_set(hotkey_dev_attributes,
2872 &dev_attr_hotkey_tablet_mode.attr);
2875 if (!res)
2876 res = register_attr_set_with_sysfs(
2877 hotkey_dev_attributes,
2878 &tpacpi_pdev->dev.kobj);
2879 if (res)
2880 goto err_exit;
2882 /* Set up key map */
2884 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2885 GFP_KERNEL);
2886 if (!hotkey_keycode_map) {
2887 printk(TPACPI_ERR
2888 "failed to allocate memory for key map\n");
2889 res = -ENOMEM;
2890 goto err_exit;
2893 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2894 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2895 "using Lenovo default hot key map\n");
2896 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2897 TPACPI_HOTKEY_MAP_SIZE);
2898 } else {
2899 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2900 "using IBM default hot key map\n");
2901 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2902 TPACPI_HOTKEY_MAP_SIZE);
2905 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2906 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2907 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2908 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2909 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2910 tpacpi_inputdev->keycode = hotkey_keycode_map;
2911 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2912 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2913 set_bit(hotkey_keycode_map[i],
2914 tpacpi_inputdev->keybit);
2915 } else {
2916 if (i < sizeof(hotkey_reserved_mask)*8)
2917 hotkey_reserved_mask |= 1 << i;
2921 if (tp_features.hotkey_wlsw) {
2922 set_bit(EV_SW, tpacpi_inputdev->evbit);
2923 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2925 if (tp_features.hotkey_tablet) {
2926 set_bit(EV_SW, tpacpi_inputdev->evbit);
2927 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2930 /* Do not issue duplicate brightness change events to
2931 * userspace */
2932 if (!tp_features.bright_acpimode)
2933 /* update bright_acpimode... */
2934 tpacpi_check_std_acpi_brightness_support();
2936 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2937 printk(TPACPI_INFO
2938 "This ThinkPad has standard ACPI backlight "
2939 "brightness control, supported by the ACPI "
2940 "video driver\n");
2941 printk(TPACPI_NOTICE
2942 "Disabling thinkpad-acpi brightness events "
2943 "by default...\n");
2945 /* The hotkey_reserved_mask change below is not
2946 * necessary while the keys are at KEY_RESERVED in the
2947 * default map, but better safe than sorry, leave it
2948 * here as a marker of what we have to do, especially
2949 * when we finally become able to set this at runtime
2950 * on response to X.org requests */
2951 hotkey_reserved_mask |=
2952 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2953 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2956 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2957 "enabling firmware HKEY event interface...\n");
2958 res = hotkey_status_set(true);
2959 if (res) {
2960 hotkey_exit();
2961 return res;
2963 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2964 & ~hotkey_reserved_mask)
2965 | hotkey_orig_mask);
2966 if (res < 0 && res != -ENXIO) {
2967 hotkey_exit();
2968 return res;
2971 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2972 "legacy ibm/hotkey event reporting over procfs %s\n",
2973 (hotkey_report_mode < 2) ?
2974 "enabled" : "disabled");
2976 tpacpi_inputdev->open = &hotkey_inputdev_open;
2977 tpacpi_inputdev->close = &hotkey_inputdev_close;
2979 hotkey_poll_setup_safe(1);
2980 tpacpi_send_radiosw_update();
2981 tpacpi_input_send_tabletsw();
2983 return 0;
2985 err_exit:
2986 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2987 hotkey_dev_attributes = NULL;
2989 return (res < 0)? res : 1;
2992 static bool hotkey_notify_hotkey(const u32 hkey,
2993 bool *send_acpi_ev,
2994 bool *ignore_acpi_ev)
2996 /* 0x1000-0x1FFF: key presses */
2997 unsigned int scancode = hkey & 0xfff;
2998 *send_acpi_ev = true;
2999 *ignore_acpi_ev = false;
3001 if (scancode > 0 && scancode < 0x21) {
3002 scancode--;
3003 if (!(hotkey_source_mask & (1 << scancode))) {
3004 tpacpi_input_send_key(scancode);
3005 *send_acpi_ev = false;
3006 } else {
3007 *ignore_acpi_ev = true;
3009 return true;
3011 return false;
3014 static bool hotkey_notify_wakeup(const u32 hkey,
3015 bool *send_acpi_ev,
3016 bool *ignore_acpi_ev)
3018 /* 0x2000-0x2FFF: Wakeup reason */
3019 *send_acpi_ev = true;
3020 *ignore_acpi_ev = false;
3022 switch (hkey) {
3023 case 0x2304: /* suspend, undock */
3024 case 0x2404: /* hibernation, undock */
3025 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3026 *ignore_acpi_ev = true;
3027 break;
3029 case 0x2305: /* suspend, bay eject */
3030 case 0x2405: /* hibernation, bay eject */
3031 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3032 *ignore_acpi_ev = true;
3033 break;
3035 case 0x2313: /* Battery on critical low level (S3) */
3036 case 0x2413: /* Battery on critical low level (S4) */
3037 printk(TPACPI_ALERT
3038 "EMERGENCY WAKEUP: battery almost empty\n");
3039 /* how to auto-heal: */
3040 /* 2313: woke up from S3, go to S4/S5 */
3041 /* 2413: woke up from S4, go to S5 */
3042 break;
3044 default:
3045 return false;
3048 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3049 printk(TPACPI_INFO
3050 "woke up due to a hot-unplug "
3051 "request...\n");
3052 hotkey_wakeup_reason_notify_change();
3054 return true;
3057 static bool hotkey_notify_usrevent(const u32 hkey,
3058 bool *send_acpi_ev,
3059 bool *ignore_acpi_ev)
3061 /* 0x5000-0x5FFF: human interface helpers */
3062 *send_acpi_ev = true;
3063 *ignore_acpi_ev = false;
3065 switch (hkey) {
3066 case 0x5010: /* Lenovo new BIOS: brightness changed */
3067 case 0x500b: /* X61t: tablet pen inserted into bay */
3068 case 0x500c: /* X61t: tablet pen removed from bay */
3069 return true;
3071 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3072 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3073 tpacpi_input_send_tabletsw();
3074 hotkey_tablet_mode_notify_change();
3075 *send_acpi_ev = false;
3076 return true;
3078 case 0x5001:
3079 case 0x5002:
3080 /* LID switch events. Do not propagate */
3081 *ignore_acpi_ev = true;
3082 return true;
3084 default:
3085 return false;
3089 static bool hotkey_notify_thermal(const u32 hkey,
3090 bool *send_acpi_ev,
3091 bool *ignore_acpi_ev)
3093 /* 0x6000-0x6FFF: thermal alarms */
3094 *send_acpi_ev = true;
3095 *ignore_acpi_ev = false;
3097 switch (hkey) {
3098 case 0x6011:
3099 printk(TPACPI_CRIT
3100 "THERMAL ALARM: battery is too hot!\n");
3101 /* recommended action: warn user through gui */
3102 return true;
3103 case 0x6012:
3104 printk(TPACPI_ALERT
3105 "THERMAL EMERGENCY: battery is extremely hot!\n");
3106 /* recommended action: immediate sleep/hibernate */
3107 return true;
3108 case 0x6021:
3109 printk(TPACPI_CRIT
3110 "THERMAL ALARM: "
3111 "a sensor reports something is too hot!\n");
3112 /* recommended action: warn user through gui, that */
3113 /* some internal component is too hot */
3114 return true;
3115 case 0x6022:
3116 printk(TPACPI_ALERT
3117 "THERMAL EMERGENCY: "
3118 "a sensor reports something is extremely hot!\n");
3119 /* recommended action: immediate sleep/hibernate */
3120 return true;
3121 case 0x6030:
3122 printk(TPACPI_INFO
3123 "EC reports that Thermal Table has changed\n");
3124 /* recommended action: do nothing, we don't have
3125 * Lenovo ATM information */
3126 return true;
3127 default:
3128 printk(TPACPI_ALERT
3129 "THERMAL ALERT: unknown thermal alarm received\n");
3130 return false;
3134 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3136 u32 hkey;
3137 bool send_acpi_ev;
3138 bool ignore_acpi_ev;
3139 bool known_ev;
3141 if (event != 0x80) {
3142 printk(TPACPI_ERR
3143 "unknown HKEY notification event %d\n", event);
3144 /* forward it to userspace, maybe it knows how to handle it */
3145 acpi_bus_generate_netlink_event(
3146 ibm->acpi->device->pnp.device_class,
3147 dev_name(&ibm->acpi->device->dev),
3148 event, 0);
3149 return;
3152 while (1) {
3153 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3154 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3155 return;
3158 if (hkey == 0) {
3159 /* queue empty */
3160 return;
3163 send_acpi_ev = true;
3164 ignore_acpi_ev = false;
3166 switch (hkey >> 12) {
3167 case 1:
3168 /* 0x1000-0x1FFF: key presses */
3169 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3170 &ignore_acpi_ev);
3171 break;
3172 case 2:
3173 /* 0x2000-0x2FFF: Wakeup reason */
3174 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3175 &ignore_acpi_ev);
3176 break;
3177 case 3:
3178 /* 0x3000-0x3FFF: bay-related wakeups */
3179 if (hkey == 0x3003) {
3180 hotkey_autosleep_ack = 1;
3181 printk(TPACPI_INFO
3182 "bay ejected\n");
3183 hotkey_wakeup_hotunplug_complete_notify_change();
3184 known_ev = true;
3185 } else {
3186 known_ev = false;
3188 break;
3189 case 4:
3190 /* 0x4000-0x4FFF: dock-related wakeups */
3191 if (hkey == 0x4003) {
3192 hotkey_autosleep_ack = 1;
3193 printk(TPACPI_INFO
3194 "undocked\n");
3195 hotkey_wakeup_hotunplug_complete_notify_change();
3196 known_ev = true;
3197 } else {
3198 known_ev = false;
3200 break;
3201 case 5:
3202 /* 0x5000-0x5FFF: human interface helpers */
3203 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3204 &ignore_acpi_ev);
3205 break;
3206 case 6:
3207 /* 0x6000-0x6FFF: thermal alarms */
3208 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3209 &ignore_acpi_ev);
3210 break;
3211 case 7:
3212 /* 0x7000-0x7FFF: misc */
3213 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3214 tpacpi_send_radiosw_update();
3215 send_acpi_ev = 0;
3216 known_ev = true;
3217 break;
3219 /* fallthrough to default */
3220 default:
3221 known_ev = false;
3223 if (!known_ev) {
3224 printk(TPACPI_NOTICE
3225 "unhandled HKEY event 0x%04x\n", hkey);
3226 printk(TPACPI_NOTICE
3227 "please report the conditions when this "
3228 "event happened to %s\n", TPACPI_MAIL);
3231 /* Legacy events */
3232 if (!ignore_acpi_ev &&
3233 (send_acpi_ev || hotkey_report_mode < 2)) {
3234 acpi_bus_generate_proc_event(ibm->acpi->device,
3235 event, hkey);
3238 /* netlink events */
3239 if (!ignore_acpi_ev && send_acpi_ev) {
3240 acpi_bus_generate_netlink_event(
3241 ibm->acpi->device->pnp.device_class,
3242 dev_name(&ibm->acpi->device->dev),
3243 event, hkey);
3248 static void hotkey_suspend(pm_message_t state)
3250 /* Do these on suspend, we get the events on early resume! */
3251 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3252 hotkey_autosleep_ack = 0;
3255 static void hotkey_resume(void)
3257 tpacpi_disable_brightness_delay();
3259 if (hotkey_mask_get())
3260 printk(TPACPI_ERR
3261 "error while trying to read hot key mask "
3262 "from firmware\n");
3263 tpacpi_send_radiosw_update();
3264 hotkey_tablet_mode_notify_change();
3265 hotkey_wakeup_reason_notify_change();
3266 hotkey_wakeup_hotunplug_complete_notify_change();
3267 hotkey_poll_setup_safe(0);
3270 /* procfs -------------------------------------------------------------- */
3271 static int hotkey_read(char *p)
3273 int res, status;
3274 int len = 0;
3276 if (!tp_features.hotkey) {
3277 len += sprintf(p + len, "status:\t\tnot supported\n");
3278 return len;
3281 if (mutex_lock_killable(&hotkey_mutex))
3282 return -ERESTARTSYS;
3283 res = hotkey_status_get(&status);
3284 if (!res)
3285 res = hotkey_mask_get();
3286 mutex_unlock(&hotkey_mutex);
3287 if (res)
3288 return res;
3290 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3291 if (tp_features.hotkey_mask) {
3292 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3293 len += sprintf(p + len,
3294 "commands:\tenable, disable, reset, <mask>\n");
3295 } else {
3296 len += sprintf(p + len, "mask:\t\tnot supported\n");
3297 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3300 return len;
3303 static void hotkey_enabledisable_warn(bool enable)
3305 tpacpi_log_usertask("procfs hotkey enable/disable");
3306 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3307 TPACPI_WARN
3308 "hotkey enable/disable functionality has been "
3309 "removed from the driver. Hotkeys are always "
3310 "enabled\n"))
3311 printk(TPACPI_ERR
3312 "Please remove the hotkey=enable module "
3313 "parameter, it is deprecated. Hotkeys are always "
3314 "enabled\n");
3317 static int hotkey_write(char *buf)
3319 int res;
3320 u32 mask;
3321 char *cmd;
3323 if (!tp_features.hotkey)
3324 return -ENODEV;
3326 if (mutex_lock_killable(&hotkey_mutex))
3327 return -ERESTARTSYS;
3329 mask = hotkey_mask;
3331 res = 0;
3332 while ((cmd = next_cmd(&buf))) {
3333 if (strlencmp(cmd, "enable") == 0) {
3334 hotkey_enabledisable_warn(1);
3335 } else if (strlencmp(cmd, "disable") == 0) {
3336 hotkey_enabledisable_warn(0);
3337 res = -EPERM;
3338 } else if (strlencmp(cmd, "reset") == 0) {
3339 mask = hotkey_orig_mask;
3340 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3341 /* mask set */
3342 } else if (sscanf(cmd, "%x", &mask) == 1) {
3343 /* mask set */
3344 } else {
3345 res = -EINVAL;
3346 goto errexit;
3350 if (!res)
3351 tpacpi_disclose_usertask("procfs hotkey",
3352 "set mask to 0x%08x\n", mask);
3354 if (!res && mask != hotkey_mask)
3355 res = hotkey_mask_set(mask);
3357 errexit:
3358 mutex_unlock(&hotkey_mutex);
3359 return res;
3362 static const struct acpi_device_id ibm_htk_device_ids[] = {
3363 {TPACPI_ACPI_HKEY_HID, 0},
3364 {"", 0},
3367 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3368 .hid = ibm_htk_device_ids,
3369 .notify = hotkey_notify,
3370 .handle = &hkey_handle,
3371 .type = ACPI_DEVICE_NOTIFY,
3374 static struct ibm_struct hotkey_driver_data = {
3375 .name = "hotkey",
3376 .read = hotkey_read,
3377 .write = hotkey_write,
3378 .exit = hotkey_exit,
3379 .resume = hotkey_resume,
3380 .suspend = hotkey_suspend,
3381 .acpi = &ibm_hotkey_acpidriver,
3384 /*************************************************************************
3385 * Bluetooth subdriver
3388 enum {
3389 /* ACPI GBDC/SBDC bits */
3390 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3391 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3392 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3393 off / last state */
3396 enum {
3397 /* ACPI \BLTH commands */
3398 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3399 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3400 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3401 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3402 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3405 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3407 static void bluetooth_suspend(pm_message_t state)
3409 /* Try to make sure radio will resume powered off */
3410 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3411 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3412 vdbg_printk(TPACPI_DBG_RFKILL,
3413 "bluetooth power down on resume request failed\n");
3416 static int bluetooth_get_status(void)
3418 int status;
3420 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3421 if (dbg_bluetoothemul)
3422 return (tpacpi_bluetooth_emulstate) ?
3423 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3424 #endif
3426 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3427 return -EIO;
3429 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3430 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3433 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3435 int status;
3437 vdbg_printk(TPACPI_DBG_RFKILL,
3438 "will attempt to %s bluetooth\n",
3439 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3441 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3442 if (dbg_bluetoothemul) {
3443 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3444 return 0;
3446 #endif
3448 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3449 if (state == TPACPI_RFK_RADIO_ON)
3450 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3451 else
3452 status = 0;
3454 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3455 return -EIO;
3457 return 0;
3460 /* sysfs bluetooth enable ---------------------------------------------- */
3461 static ssize_t bluetooth_enable_show(struct device *dev,
3462 struct device_attribute *attr,
3463 char *buf)
3465 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3466 attr, buf);
3469 static ssize_t bluetooth_enable_store(struct device *dev,
3470 struct device_attribute *attr,
3471 const char *buf, size_t count)
3473 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3474 attr, buf, count);
3477 static struct device_attribute dev_attr_bluetooth_enable =
3478 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3479 bluetooth_enable_show, bluetooth_enable_store);
3481 /* --------------------------------------------------------------------- */
3483 static struct attribute *bluetooth_attributes[] = {
3484 &dev_attr_bluetooth_enable.attr,
3485 NULL
3488 static const struct attribute_group bluetooth_attr_group = {
3489 .attrs = bluetooth_attributes,
3492 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3493 .get_status = bluetooth_get_status,
3494 .set_status = bluetooth_set_status,
3497 static void bluetooth_shutdown(void)
3499 /* Order firmware to save current state to NVRAM */
3500 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3501 TP_ACPI_BLTH_SAVE_STATE))
3502 printk(TPACPI_NOTICE
3503 "failed to save bluetooth state to NVRAM\n");
3504 else
3505 vdbg_printk(TPACPI_DBG_RFKILL,
3506 "bluestooth state saved to NVRAM\n");
3509 static void bluetooth_exit(void)
3511 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3512 &bluetooth_attr_group);
3514 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3516 bluetooth_shutdown();
3519 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3521 int res;
3522 int status = 0;
3524 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3525 "initializing bluetooth subdriver\n");
3527 TPACPI_ACPIHANDLE_INIT(hkey);
3529 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3530 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3531 tp_features.bluetooth = hkey_handle &&
3532 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3534 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3535 "bluetooth is %s, status 0x%02x\n",
3536 str_supported(tp_features.bluetooth),
3537 status);
3539 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3540 if (dbg_bluetoothemul) {
3541 tp_features.bluetooth = 1;
3542 printk(TPACPI_INFO
3543 "bluetooth switch emulation enabled\n");
3544 } else
3545 #endif
3546 if (tp_features.bluetooth &&
3547 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3548 /* no bluetooth hardware present in system */
3549 tp_features.bluetooth = 0;
3550 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3551 "bluetooth hardware not installed\n");
3554 if (!tp_features.bluetooth)
3555 return 1;
3557 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3558 &bluetooth_tprfk_ops,
3559 RFKILL_TYPE_BLUETOOTH,
3560 TPACPI_RFK_BLUETOOTH_SW_NAME,
3561 true);
3562 if (res)
3563 return res;
3565 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3566 &bluetooth_attr_group);
3567 if (res) {
3568 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3569 return res;
3572 return 0;
3575 /* procfs -------------------------------------------------------------- */
3576 static int bluetooth_read(char *p)
3578 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
3581 static int bluetooth_write(char *buf)
3583 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
3586 static struct ibm_struct bluetooth_driver_data = {
3587 .name = "bluetooth",
3588 .read = bluetooth_read,
3589 .write = bluetooth_write,
3590 .exit = bluetooth_exit,
3591 .suspend = bluetooth_suspend,
3592 .shutdown = bluetooth_shutdown,
3595 /*************************************************************************
3596 * Wan subdriver
3599 enum {
3600 /* ACPI GWAN/SWAN bits */
3601 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3602 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3603 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3604 off / last state */
3607 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3609 static void wan_suspend(pm_message_t state)
3611 /* Try to make sure radio will resume powered off */
3612 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3613 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3614 vdbg_printk(TPACPI_DBG_RFKILL,
3615 "WWAN power down on resume request failed\n");
3618 static int wan_get_status(void)
3620 int status;
3622 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3623 if (dbg_wwanemul)
3624 return (tpacpi_wwan_emulstate) ?
3625 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3626 #endif
3628 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3629 return -EIO;
3631 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3632 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3635 static int wan_set_status(enum tpacpi_rfkill_state state)
3637 int status;
3639 vdbg_printk(TPACPI_DBG_RFKILL,
3640 "will attempt to %s wwan\n",
3641 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3643 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3644 if (dbg_wwanemul) {
3645 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
3646 return 0;
3648 #endif
3650 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3651 if (state == TPACPI_RFK_RADIO_ON)
3652 status = TP_ACPI_WANCARD_RADIOSSW;
3653 else
3654 status = 0;
3656 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3657 return -EIO;
3659 return 0;
3662 /* sysfs wan enable ---------------------------------------------------- */
3663 static ssize_t wan_enable_show(struct device *dev,
3664 struct device_attribute *attr,
3665 char *buf)
3667 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3668 attr, buf);
3671 static ssize_t wan_enable_store(struct device *dev,
3672 struct device_attribute *attr,
3673 const char *buf, size_t count)
3675 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3676 attr, buf, count);
3679 static struct device_attribute dev_attr_wan_enable =
3680 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3681 wan_enable_show, wan_enable_store);
3683 /* --------------------------------------------------------------------- */
3685 static struct attribute *wan_attributes[] = {
3686 &dev_attr_wan_enable.attr,
3687 NULL
3690 static const struct attribute_group wan_attr_group = {
3691 .attrs = wan_attributes,
3694 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3695 .get_status = wan_get_status,
3696 .set_status = wan_set_status,
3699 static void wan_shutdown(void)
3701 /* Order firmware to save current state to NVRAM */
3702 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3703 TP_ACPI_WGSV_SAVE_STATE))
3704 printk(TPACPI_NOTICE
3705 "failed to save WWAN state to NVRAM\n");
3706 else
3707 vdbg_printk(TPACPI_DBG_RFKILL,
3708 "WWAN state saved to NVRAM\n");
3711 static void wan_exit(void)
3713 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3714 &wan_attr_group);
3716 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3718 wan_shutdown();
3721 static int __init wan_init(struct ibm_init_struct *iibm)
3723 int res;
3724 int status = 0;
3726 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3727 "initializing wan subdriver\n");
3729 TPACPI_ACPIHANDLE_INIT(hkey);
3731 tp_features.wan = hkey_handle &&
3732 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3734 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3735 "wan is %s, status 0x%02x\n",
3736 str_supported(tp_features.wan),
3737 status);
3739 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3740 if (dbg_wwanemul) {
3741 tp_features.wan = 1;
3742 printk(TPACPI_INFO
3743 "wwan switch emulation enabled\n");
3744 } else
3745 #endif
3746 if (tp_features.wan &&
3747 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3748 /* no wan hardware present in system */
3749 tp_features.wan = 0;
3750 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3751 "wan hardware not installed\n");
3754 if (!tp_features.wan)
3755 return 1;
3757 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3758 &wan_tprfk_ops,
3759 RFKILL_TYPE_WWAN,
3760 TPACPI_RFK_WWAN_SW_NAME,
3761 true);
3762 if (res)
3763 return res;
3765 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3766 &wan_attr_group);
3768 if (res) {
3769 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3770 return res;
3773 return 0;
3776 /* procfs -------------------------------------------------------------- */
3777 static int wan_read(char *p)
3779 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
3782 static int wan_write(char *buf)
3784 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
3787 static struct ibm_struct wan_driver_data = {
3788 .name = "wan",
3789 .read = wan_read,
3790 .write = wan_write,
3791 .exit = wan_exit,
3792 .suspend = wan_suspend,
3793 .shutdown = wan_shutdown,
3796 /*************************************************************************
3797 * UWB subdriver
3800 enum {
3801 /* ACPI GUWB/SUWB bits */
3802 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3803 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3806 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3808 static int uwb_get_status(void)
3810 int status;
3812 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3813 if (dbg_uwbemul)
3814 return (tpacpi_uwb_emulstate) ?
3815 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3816 #endif
3818 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3819 return -EIO;
3821 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3822 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3825 static int uwb_set_status(enum tpacpi_rfkill_state state)
3827 int status;
3829 vdbg_printk(TPACPI_DBG_RFKILL,
3830 "will attempt to %s UWB\n",
3831 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3833 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3834 if (dbg_uwbemul) {
3835 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
3836 return 0;
3838 #endif
3840 if (state == TPACPI_RFK_RADIO_ON)
3841 status = TP_ACPI_UWB_RADIOSSW;
3842 else
3843 status = 0;
3845 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3846 return -EIO;
3848 return 0;
3851 /* --------------------------------------------------------------------- */
3853 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
3854 .get_status = uwb_get_status,
3855 .set_status = uwb_set_status,
3858 static void uwb_exit(void)
3860 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
3863 static int __init uwb_init(struct ibm_init_struct *iibm)
3865 int res;
3866 int status = 0;
3868 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3869 "initializing uwb subdriver\n");
3871 TPACPI_ACPIHANDLE_INIT(hkey);
3873 tp_features.uwb = hkey_handle &&
3874 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3876 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3877 "uwb is %s, status 0x%02x\n",
3878 str_supported(tp_features.uwb),
3879 status);
3881 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3882 if (dbg_uwbemul) {
3883 tp_features.uwb = 1;
3884 printk(TPACPI_INFO
3885 "uwb switch emulation enabled\n");
3886 } else
3887 #endif
3888 if (tp_features.uwb &&
3889 !(status & TP_ACPI_UWB_HWPRESENT)) {
3890 /* no uwb hardware present in system */
3891 tp_features.uwb = 0;
3892 dbg_printk(TPACPI_DBG_INIT,
3893 "uwb hardware not installed\n");
3896 if (!tp_features.uwb)
3897 return 1;
3899 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3900 &uwb_tprfk_ops,
3901 RFKILL_TYPE_UWB,
3902 TPACPI_RFK_UWB_SW_NAME,
3903 false);
3904 return res;
3907 static struct ibm_struct uwb_driver_data = {
3908 .name = "uwb",
3909 .exit = uwb_exit,
3910 .flags.experimental = 1,
3913 /*************************************************************************
3914 * Video subdriver
3917 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3919 enum video_access_mode {
3920 TPACPI_VIDEO_NONE = 0,
3921 TPACPI_VIDEO_570, /* 570 */
3922 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3923 TPACPI_VIDEO_NEW, /* all others */
3926 enum { /* video status flags, based on VIDEO_570 */
3927 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3928 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3929 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3932 enum { /* TPACPI_VIDEO_570 constants */
3933 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3934 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3935 * video_status_flags */
3936 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3937 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3940 static enum video_access_mode video_supported;
3941 static int video_orig_autosw;
3943 static int video_autosw_get(void);
3944 static int video_autosw_set(int enable);
3946 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3948 static int __init video_init(struct ibm_init_struct *iibm)
3950 int ivga;
3952 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3954 TPACPI_ACPIHANDLE_INIT(vid);
3955 TPACPI_ACPIHANDLE_INIT(vid2);
3957 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3958 /* G41, assume IVGA doesn't change */
3959 vid_handle = vid2_handle;
3961 if (!vid_handle)
3962 /* video switching not supported on R30, R31 */
3963 video_supported = TPACPI_VIDEO_NONE;
3964 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3965 /* 570 */
3966 video_supported = TPACPI_VIDEO_570;
3967 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3968 /* 600e/x, 770e, 770x */
3969 video_supported = TPACPI_VIDEO_770;
3970 else
3971 /* all others */
3972 video_supported = TPACPI_VIDEO_NEW;
3974 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3975 str_supported(video_supported != TPACPI_VIDEO_NONE),
3976 video_supported);
3978 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3981 static void video_exit(void)
3983 dbg_printk(TPACPI_DBG_EXIT,
3984 "restoring original video autoswitch mode\n");
3985 if (video_autosw_set(video_orig_autosw))
3986 printk(TPACPI_ERR "error while trying to restore original "
3987 "video autoswitch mode\n");
3990 static int video_outputsw_get(void)
3992 int status = 0;
3993 int i;
3995 switch (video_supported) {
3996 case TPACPI_VIDEO_570:
3997 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3998 TP_ACPI_VIDEO_570_PHSCMD))
3999 return -EIO;
4000 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4001 break;
4002 case TPACPI_VIDEO_770:
4003 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4004 return -EIO;
4005 if (i)
4006 status |= TP_ACPI_VIDEO_S_LCD;
4007 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4008 return -EIO;
4009 if (i)
4010 status |= TP_ACPI_VIDEO_S_CRT;
4011 break;
4012 case TPACPI_VIDEO_NEW:
4013 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4014 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4015 return -EIO;
4016 if (i)
4017 status |= TP_ACPI_VIDEO_S_CRT;
4019 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4020 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4021 return -EIO;
4022 if (i)
4023 status |= TP_ACPI_VIDEO_S_LCD;
4024 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4025 return -EIO;
4026 if (i)
4027 status |= TP_ACPI_VIDEO_S_DVI;
4028 break;
4029 default:
4030 return -ENOSYS;
4033 return status;
4036 static int video_outputsw_set(int status)
4038 int autosw;
4039 int res = 0;
4041 switch (video_supported) {
4042 case TPACPI_VIDEO_570:
4043 res = acpi_evalf(NULL, NULL,
4044 "\\_SB.PHS2", "vdd",
4045 TP_ACPI_VIDEO_570_PHS2CMD,
4046 status | TP_ACPI_VIDEO_570_PHS2SET);
4047 break;
4048 case TPACPI_VIDEO_770:
4049 autosw = video_autosw_get();
4050 if (autosw < 0)
4051 return autosw;
4053 res = video_autosw_set(1);
4054 if (res)
4055 return res;
4056 res = acpi_evalf(vid_handle, NULL,
4057 "ASWT", "vdd", status * 0x100, 0);
4058 if (!autosw && video_autosw_set(autosw)) {
4059 printk(TPACPI_ERR
4060 "video auto-switch left enabled due to error\n");
4061 return -EIO;
4063 break;
4064 case TPACPI_VIDEO_NEW:
4065 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4066 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4067 break;
4068 default:
4069 return -ENOSYS;
4072 return (res)? 0 : -EIO;
4075 static int video_autosw_get(void)
4077 int autosw = 0;
4079 switch (video_supported) {
4080 case TPACPI_VIDEO_570:
4081 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4082 return -EIO;
4083 break;
4084 case TPACPI_VIDEO_770:
4085 case TPACPI_VIDEO_NEW:
4086 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4087 return -EIO;
4088 break;
4089 default:
4090 return -ENOSYS;
4093 return autosw & 1;
4096 static int video_autosw_set(int enable)
4098 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4099 return -EIO;
4100 return 0;
4103 static int video_outputsw_cycle(void)
4105 int autosw = video_autosw_get();
4106 int res;
4108 if (autosw < 0)
4109 return autosw;
4111 switch (video_supported) {
4112 case TPACPI_VIDEO_570:
4113 res = video_autosw_set(1);
4114 if (res)
4115 return res;
4116 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4117 break;
4118 case TPACPI_VIDEO_770:
4119 case TPACPI_VIDEO_NEW:
4120 res = video_autosw_set(1);
4121 if (res)
4122 return res;
4123 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4124 break;
4125 default:
4126 return -ENOSYS;
4128 if (!autosw && video_autosw_set(autosw)) {
4129 printk(TPACPI_ERR
4130 "video auto-switch left enabled due to error\n");
4131 return -EIO;
4134 return (res)? 0 : -EIO;
4137 static int video_expand_toggle(void)
4139 switch (video_supported) {
4140 case TPACPI_VIDEO_570:
4141 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4142 0 : -EIO;
4143 case TPACPI_VIDEO_770:
4144 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4145 0 : -EIO;
4146 case TPACPI_VIDEO_NEW:
4147 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4148 0 : -EIO;
4149 default:
4150 return -ENOSYS;
4152 /* not reached */
4155 static int video_read(char *p)
4157 int status, autosw;
4158 int len = 0;
4160 if (video_supported == TPACPI_VIDEO_NONE) {
4161 len += sprintf(p + len, "status:\t\tnot supported\n");
4162 return len;
4165 status = video_outputsw_get();
4166 if (status < 0)
4167 return status;
4169 autosw = video_autosw_get();
4170 if (autosw < 0)
4171 return autosw;
4173 len += sprintf(p + len, "status:\t\tsupported\n");
4174 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4175 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4176 if (video_supported == TPACPI_VIDEO_NEW)
4177 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4178 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4179 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4180 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4181 if (video_supported == TPACPI_VIDEO_NEW)
4182 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4183 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4184 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4186 return len;
4189 static int video_write(char *buf)
4191 char *cmd;
4192 int enable, disable, status;
4193 int res;
4195 if (video_supported == TPACPI_VIDEO_NONE)
4196 return -ENODEV;
4198 enable = 0;
4199 disable = 0;
4201 while ((cmd = next_cmd(&buf))) {
4202 if (strlencmp(cmd, "lcd_enable") == 0) {
4203 enable |= TP_ACPI_VIDEO_S_LCD;
4204 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4205 disable |= TP_ACPI_VIDEO_S_LCD;
4206 } else if (strlencmp(cmd, "crt_enable") == 0) {
4207 enable |= TP_ACPI_VIDEO_S_CRT;
4208 } else if (strlencmp(cmd, "crt_disable") == 0) {
4209 disable |= TP_ACPI_VIDEO_S_CRT;
4210 } else if (video_supported == TPACPI_VIDEO_NEW &&
4211 strlencmp(cmd, "dvi_enable") == 0) {
4212 enable |= TP_ACPI_VIDEO_S_DVI;
4213 } else if (video_supported == TPACPI_VIDEO_NEW &&
4214 strlencmp(cmd, "dvi_disable") == 0) {
4215 disable |= TP_ACPI_VIDEO_S_DVI;
4216 } else if (strlencmp(cmd, "auto_enable") == 0) {
4217 res = video_autosw_set(1);
4218 if (res)
4219 return res;
4220 } else if (strlencmp(cmd, "auto_disable") == 0) {
4221 res = video_autosw_set(0);
4222 if (res)
4223 return res;
4224 } else if (strlencmp(cmd, "video_switch") == 0) {
4225 res = video_outputsw_cycle();
4226 if (res)
4227 return res;
4228 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4229 res = video_expand_toggle();
4230 if (res)
4231 return res;
4232 } else
4233 return -EINVAL;
4236 if (enable || disable) {
4237 status = video_outputsw_get();
4238 if (status < 0)
4239 return status;
4240 res = video_outputsw_set((status & ~disable) | enable);
4241 if (res)
4242 return res;
4245 return 0;
4248 static struct ibm_struct video_driver_data = {
4249 .name = "video",
4250 .read = video_read,
4251 .write = video_write,
4252 .exit = video_exit,
4255 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4257 /*************************************************************************
4258 * Light (thinklight) subdriver
4261 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4262 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4264 static int light_get_status(void)
4266 int status = 0;
4268 if (tp_features.light_status) {
4269 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4270 return -EIO;
4271 return (!!status);
4274 return -ENXIO;
4277 static int light_set_status(int status)
4279 int rc;
4281 if (tp_features.light) {
4282 if (cmos_handle) {
4283 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4284 (status)?
4285 TP_CMOS_THINKLIGHT_ON :
4286 TP_CMOS_THINKLIGHT_OFF);
4287 } else {
4288 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4289 (status)? 1 : 0);
4291 return (rc)? 0 : -EIO;
4294 return -ENXIO;
4297 static void light_set_status_worker(struct work_struct *work)
4299 struct tpacpi_led_classdev *data =
4300 container_of(work, struct tpacpi_led_classdev, work);
4302 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4303 light_set_status((data->new_state != TPACPI_LED_OFF));
4306 static void light_sysfs_set(struct led_classdev *led_cdev,
4307 enum led_brightness brightness)
4309 struct tpacpi_led_classdev *data =
4310 container_of(led_cdev,
4311 struct tpacpi_led_classdev,
4312 led_classdev);
4313 data->new_state = (brightness != LED_OFF) ?
4314 TPACPI_LED_ON : TPACPI_LED_OFF;
4315 queue_work(tpacpi_wq, &data->work);
4318 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4320 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4323 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4324 .led_classdev = {
4325 .name = "tpacpi::thinklight",
4326 .brightness_set = &light_sysfs_set,
4327 .brightness_get = &light_sysfs_get,
4331 static int __init light_init(struct ibm_init_struct *iibm)
4333 int rc;
4335 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4337 TPACPI_ACPIHANDLE_INIT(ledb);
4338 TPACPI_ACPIHANDLE_INIT(lght);
4339 TPACPI_ACPIHANDLE_INIT(cmos);
4340 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4342 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4343 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4345 if (tp_features.light)
4346 /* light status not supported on
4347 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4348 tp_features.light_status =
4349 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4351 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4352 str_supported(tp_features.light),
4353 str_supported(tp_features.light_status));
4355 if (!tp_features.light)
4356 return 1;
4358 rc = led_classdev_register(&tpacpi_pdev->dev,
4359 &tpacpi_led_thinklight.led_classdev);
4361 if (rc < 0) {
4362 tp_features.light = 0;
4363 tp_features.light_status = 0;
4364 } else {
4365 rc = 0;
4368 return rc;
4371 static void light_exit(void)
4373 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4374 if (work_pending(&tpacpi_led_thinklight.work))
4375 flush_workqueue(tpacpi_wq);
4378 static int light_read(char *p)
4380 int len = 0;
4381 int status;
4383 if (!tp_features.light) {
4384 len += sprintf(p + len, "status:\t\tnot supported\n");
4385 } else if (!tp_features.light_status) {
4386 len += sprintf(p + len, "status:\t\tunknown\n");
4387 len += sprintf(p + len, "commands:\ton, off\n");
4388 } else {
4389 status = light_get_status();
4390 if (status < 0)
4391 return status;
4392 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4393 len += sprintf(p + len, "commands:\ton, off\n");
4396 return len;
4399 static int light_write(char *buf)
4401 char *cmd;
4402 int newstatus = 0;
4404 if (!tp_features.light)
4405 return -ENODEV;
4407 while ((cmd = next_cmd(&buf))) {
4408 if (strlencmp(cmd, "on") == 0) {
4409 newstatus = 1;
4410 } else if (strlencmp(cmd, "off") == 0) {
4411 newstatus = 0;
4412 } else
4413 return -EINVAL;
4416 return light_set_status(newstatus);
4419 static struct ibm_struct light_driver_data = {
4420 .name = "light",
4421 .read = light_read,
4422 .write = light_write,
4423 .exit = light_exit,
4426 /*************************************************************************
4427 * CMOS subdriver
4430 /* sysfs cmos_command -------------------------------------------------- */
4431 static ssize_t cmos_command_store(struct device *dev,
4432 struct device_attribute *attr,
4433 const char *buf, size_t count)
4435 unsigned long cmos_cmd;
4436 int res;
4438 if (parse_strtoul(buf, 21, &cmos_cmd))
4439 return -EINVAL;
4441 res = issue_thinkpad_cmos_command(cmos_cmd);
4442 return (res)? res : count;
4445 static struct device_attribute dev_attr_cmos_command =
4446 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4448 /* --------------------------------------------------------------------- */
4450 static int __init cmos_init(struct ibm_init_struct *iibm)
4452 int res;
4454 vdbg_printk(TPACPI_DBG_INIT,
4455 "initializing cmos commands subdriver\n");
4457 TPACPI_ACPIHANDLE_INIT(cmos);
4459 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4460 str_supported(cmos_handle != NULL));
4462 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4463 if (res)
4464 return res;
4466 return (cmos_handle)? 0 : 1;
4469 static void cmos_exit(void)
4471 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4474 static int cmos_read(char *p)
4476 int len = 0;
4478 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4479 R30, R31, T20-22, X20-21 */
4480 if (!cmos_handle)
4481 len += sprintf(p + len, "status:\t\tnot supported\n");
4482 else {
4483 len += sprintf(p + len, "status:\t\tsupported\n");
4484 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4487 return len;
4490 static int cmos_write(char *buf)
4492 char *cmd;
4493 int cmos_cmd, res;
4495 while ((cmd = next_cmd(&buf))) {
4496 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4497 cmos_cmd >= 0 && cmos_cmd <= 21) {
4498 /* cmos_cmd set */
4499 } else
4500 return -EINVAL;
4502 res = issue_thinkpad_cmos_command(cmos_cmd);
4503 if (res)
4504 return res;
4507 return 0;
4510 static struct ibm_struct cmos_driver_data = {
4511 .name = "cmos",
4512 .read = cmos_read,
4513 .write = cmos_write,
4514 .exit = cmos_exit,
4517 /*************************************************************************
4518 * LED subdriver
4521 enum led_access_mode {
4522 TPACPI_LED_NONE = 0,
4523 TPACPI_LED_570, /* 570 */
4524 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4525 TPACPI_LED_NEW, /* all others */
4528 enum { /* For TPACPI_LED_OLD */
4529 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4530 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4531 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4534 static enum led_access_mode led_supported;
4536 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4537 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4538 /* T20-22, X20-21 */
4539 "LED", /* all others */
4540 ); /* R30, R31 */
4542 #define TPACPI_LED_NUMLEDS 16
4543 static struct tpacpi_led_classdev *tpacpi_leds;
4544 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4545 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4546 /* there's a limit of 19 chars + NULL before 2.6.26 */
4547 "tpacpi::power",
4548 "tpacpi:orange:batt",
4549 "tpacpi:green:batt",
4550 "tpacpi::dock_active",
4551 "tpacpi::bay_active",
4552 "tpacpi::dock_batt",
4553 "tpacpi::unknown_led",
4554 "tpacpi::standby",
4555 "tpacpi::dock_status1",
4556 "tpacpi::dock_status2",
4557 "tpacpi::unknown_led2",
4558 "tpacpi::unknown_led3",
4559 "tpacpi::thinkvantage",
4561 #define TPACPI_SAFE_LEDS 0x1081U
4563 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4565 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4566 return false;
4567 #else
4568 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4569 #endif
4572 static int led_get_status(const unsigned int led)
4574 int status;
4575 enum led_status_t led_s;
4577 switch (led_supported) {
4578 case TPACPI_LED_570:
4579 if (!acpi_evalf(ec_handle,
4580 &status, "GLED", "dd", 1 << led))
4581 return -EIO;
4582 led_s = (status == 0)?
4583 TPACPI_LED_OFF :
4584 ((status == 1)?
4585 TPACPI_LED_ON :
4586 TPACPI_LED_BLINK);
4587 tpacpi_led_state_cache[led] = led_s;
4588 return led_s;
4589 default:
4590 return -ENXIO;
4593 /* not reached */
4596 static int led_set_status(const unsigned int led,
4597 const enum led_status_t ledstatus)
4599 /* off, on, blink. Index is led_status_t */
4600 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4601 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4603 int rc = 0;
4605 switch (led_supported) {
4606 case TPACPI_LED_570:
4607 /* 570 */
4608 if (unlikely(led > 7))
4609 return -EINVAL;
4610 if (unlikely(tpacpi_is_led_restricted(led)))
4611 return -EPERM;
4612 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4613 (1 << led), led_sled_arg1[ledstatus]))
4614 rc = -EIO;
4615 break;
4616 case TPACPI_LED_OLD:
4617 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4618 if (unlikely(led > 7))
4619 return -EINVAL;
4620 if (unlikely(tpacpi_is_led_restricted(led)))
4621 return -EPERM;
4622 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4623 if (rc >= 0)
4624 rc = ec_write(TPACPI_LED_EC_HLBL,
4625 (ledstatus == TPACPI_LED_BLINK) << led);
4626 if (rc >= 0)
4627 rc = ec_write(TPACPI_LED_EC_HLCL,
4628 (ledstatus != TPACPI_LED_OFF) << led);
4629 break;
4630 case TPACPI_LED_NEW:
4631 /* all others */
4632 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4633 return -EINVAL;
4634 if (unlikely(tpacpi_is_led_restricted(led)))
4635 return -EPERM;
4636 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4637 led, led_led_arg1[ledstatus]))
4638 rc = -EIO;
4639 break;
4640 default:
4641 rc = -ENXIO;
4644 if (!rc)
4645 tpacpi_led_state_cache[led] = ledstatus;
4647 return rc;
4650 static void led_set_status_worker(struct work_struct *work)
4652 struct tpacpi_led_classdev *data =
4653 container_of(work, struct tpacpi_led_classdev, work);
4655 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4656 led_set_status(data->led, data->new_state);
4659 static void led_sysfs_set(struct led_classdev *led_cdev,
4660 enum led_brightness brightness)
4662 struct tpacpi_led_classdev *data = container_of(led_cdev,
4663 struct tpacpi_led_classdev, led_classdev);
4665 if (brightness == LED_OFF)
4666 data->new_state = TPACPI_LED_OFF;
4667 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4668 data->new_state = TPACPI_LED_ON;
4669 else
4670 data->new_state = TPACPI_LED_BLINK;
4672 queue_work(tpacpi_wq, &data->work);
4675 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4676 unsigned long *delay_on, unsigned long *delay_off)
4678 struct tpacpi_led_classdev *data = container_of(led_cdev,
4679 struct tpacpi_led_classdev, led_classdev);
4681 /* Can we choose the flash rate? */
4682 if (*delay_on == 0 && *delay_off == 0) {
4683 /* yes. set them to the hardware blink rate (1 Hz) */
4684 *delay_on = 500; /* ms */
4685 *delay_off = 500; /* ms */
4686 } else if ((*delay_on != 500) || (*delay_off != 500))
4687 return -EINVAL;
4689 data->new_state = TPACPI_LED_BLINK;
4690 queue_work(tpacpi_wq, &data->work);
4692 return 0;
4695 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4697 int rc;
4699 struct tpacpi_led_classdev *data = container_of(led_cdev,
4700 struct tpacpi_led_classdev, led_classdev);
4702 rc = led_get_status(data->led);
4704 if (rc == TPACPI_LED_OFF || rc < 0)
4705 rc = LED_OFF; /* no error handling in led class :( */
4706 else
4707 rc = LED_FULL;
4709 return rc;
4712 static void led_exit(void)
4714 unsigned int i;
4716 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4717 if (tpacpi_leds[i].led_classdev.name)
4718 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4721 kfree(tpacpi_leds);
4724 static int __init tpacpi_init_led(unsigned int led)
4726 int rc;
4728 tpacpi_leds[led].led = led;
4730 /* LEDs with no name don't get registered */
4731 if (!tpacpi_led_names[led])
4732 return 0;
4734 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4735 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4736 if (led_supported == TPACPI_LED_570)
4737 tpacpi_leds[led].led_classdev.brightness_get =
4738 &led_sysfs_get;
4740 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4742 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4744 rc = led_classdev_register(&tpacpi_pdev->dev,
4745 &tpacpi_leds[led].led_classdev);
4746 if (rc < 0)
4747 tpacpi_leds[led].led_classdev.name = NULL;
4749 return rc;
4752 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4753 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4754 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4755 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4757 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4758 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4759 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4760 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4761 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4762 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4763 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4764 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4766 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4767 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4768 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4769 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4770 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4772 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4773 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4774 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4775 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4777 /* (1) - may have excess leds enabled on MSB */
4779 /* Defaults (order matters, keep last, don't reorder!) */
4780 { /* Lenovo */
4781 .vendor = PCI_VENDOR_ID_LENOVO,
4782 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4783 .quirks = 0x1fffU,
4785 { /* IBM ThinkPads with no EC version string */
4786 .vendor = PCI_VENDOR_ID_IBM,
4787 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4788 .quirks = 0x00ffU,
4790 { /* IBM ThinkPads with EC version string */
4791 .vendor = PCI_VENDOR_ID_IBM,
4792 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4793 .quirks = 0x00bfU,
4797 #undef TPACPI_LEDQ_IBM
4798 #undef TPACPI_LEDQ_LNV
4800 static int __init led_init(struct ibm_init_struct *iibm)
4802 unsigned int i;
4803 int rc;
4804 unsigned long useful_leds;
4806 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4808 TPACPI_ACPIHANDLE_INIT(led);
4810 if (!led_handle)
4811 /* led not supported on R30, R31 */
4812 led_supported = TPACPI_LED_NONE;
4813 else if (strlencmp(led_path, "SLED") == 0)
4814 /* 570 */
4815 led_supported = TPACPI_LED_570;
4816 else if (strlencmp(led_path, "SYSL") == 0)
4817 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4818 led_supported = TPACPI_LED_OLD;
4819 else
4820 /* all others */
4821 led_supported = TPACPI_LED_NEW;
4823 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4824 str_supported(led_supported), led_supported);
4826 if (led_supported == TPACPI_LED_NONE)
4827 return 1;
4829 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4830 GFP_KERNEL);
4831 if (!tpacpi_leds) {
4832 printk(TPACPI_ERR "Out of memory for LED data\n");
4833 return -ENOMEM;
4836 useful_leds = tpacpi_check_quirks(led_useful_qtable,
4837 ARRAY_SIZE(led_useful_qtable));
4839 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4840 if (!tpacpi_is_led_restricted(i) &&
4841 test_bit(i, &useful_leds)) {
4842 rc = tpacpi_init_led(i);
4843 if (rc < 0) {
4844 led_exit();
4845 return rc;
4850 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4851 printk(TPACPI_NOTICE
4852 "warning: userspace override of important "
4853 "firmware LEDs is enabled\n");
4854 #endif
4855 return 0;
4858 #define str_led_status(s) \
4859 ((s) == TPACPI_LED_OFF ? "off" : \
4860 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4862 static int led_read(char *p)
4864 int len = 0;
4866 if (!led_supported) {
4867 len += sprintf(p + len, "status:\t\tnot supported\n");
4868 return len;
4870 len += sprintf(p + len, "status:\t\tsupported\n");
4872 if (led_supported == TPACPI_LED_570) {
4873 /* 570 */
4874 int i, status;
4875 for (i = 0; i < 8; i++) {
4876 status = led_get_status(i);
4877 if (status < 0)
4878 return -EIO;
4879 len += sprintf(p + len, "%d:\t\t%s\n",
4880 i, str_led_status(status));
4884 len += sprintf(p + len, "commands:\t"
4885 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
4887 return len;
4890 static int led_write(char *buf)
4892 char *cmd;
4893 int led, rc;
4894 enum led_status_t s;
4896 if (!led_supported)
4897 return -ENODEV;
4899 while ((cmd = next_cmd(&buf))) {
4900 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
4901 return -EINVAL;
4903 if (strstr(cmd, "off")) {
4904 s = TPACPI_LED_OFF;
4905 } else if (strstr(cmd, "on")) {
4906 s = TPACPI_LED_ON;
4907 } else if (strstr(cmd, "blink")) {
4908 s = TPACPI_LED_BLINK;
4909 } else {
4910 return -EINVAL;
4913 rc = led_set_status(led, s);
4914 if (rc < 0)
4915 return rc;
4918 return 0;
4921 static struct ibm_struct led_driver_data = {
4922 .name = "led",
4923 .read = led_read,
4924 .write = led_write,
4925 .exit = led_exit,
4928 /*************************************************************************
4929 * Beep subdriver
4932 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4934 #define TPACPI_BEEP_Q1 0x0001
4936 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
4937 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
4938 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
4941 static int __init beep_init(struct ibm_init_struct *iibm)
4943 unsigned long quirks;
4945 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4947 TPACPI_ACPIHANDLE_INIT(beep);
4949 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4950 str_supported(beep_handle != NULL));
4952 quirks = tpacpi_check_quirks(beep_quirk_table,
4953 ARRAY_SIZE(beep_quirk_table));
4955 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
4957 return (beep_handle)? 0 : 1;
4960 static int beep_read(char *p)
4962 int len = 0;
4964 if (!beep_handle)
4965 len += sprintf(p + len, "status:\t\tnot supported\n");
4966 else {
4967 len += sprintf(p + len, "status:\t\tsupported\n");
4968 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4971 return len;
4974 static int beep_write(char *buf)
4976 char *cmd;
4977 int beep_cmd;
4979 if (!beep_handle)
4980 return -ENODEV;
4982 while ((cmd = next_cmd(&buf))) {
4983 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4984 beep_cmd >= 0 && beep_cmd <= 17) {
4985 /* beep_cmd set */
4986 } else
4987 return -EINVAL;
4988 if (tp_features.beep_needs_two_args) {
4989 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
4990 beep_cmd, 0))
4991 return -EIO;
4992 } else {
4993 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
4994 beep_cmd))
4995 return -EIO;
4999 return 0;
5002 static struct ibm_struct beep_driver_data = {
5003 .name = "beep",
5004 .read = beep_read,
5005 .write = beep_write,
5008 /*************************************************************************
5009 * Thermal subdriver
5012 enum thermal_access_mode {
5013 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5014 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5015 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5016 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5017 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5020 enum { /* TPACPI_THERMAL_TPEC_* */
5021 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5022 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5023 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5026 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5027 struct ibm_thermal_sensors_struct {
5028 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5031 static enum thermal_access_mode thermal_read_mode;
5033 /* idx is zero-based */
5034 static int thermal_get_sensor(int idx, s32 *value)
5036 int t;
5037 s8 tmp;
5038 char tmpi[5];
5040 t = TP_EC_THERMAL_TMP0;
5042 switch (thermal_read_mode) {
5043 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5044 case TPACPI_THERMAL_TPEC_16:
5045 if (idx >= 8 && idx <= 15) {
5046 t = TP_EC_THERMAL_TMP8;
5047 idx -= 8;
5049 /* fallthrough */
5050 #endif
5051 case TPACPI_THERMAL_TPEC_8:
5052 if (idx <= 7) {
5053 if (!acpi_ec_read(t + idx, &tmp))
5054 return -EIO;
5055 *value = tmp * 1000;
5056 return 0;
5058 break;
5060 case TPACPI_THERMAL_ACPI_UPDT:
5061 if (idx <= 7) {
5062 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5063 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5064 return -EIO;
5065 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5066 return -EIO;
5067 *value = (t - 2732) * 100;
5068 return 0;
5070 break;
5072 case TPACPI_THERMAL_ACPI_TMP07:
5073 if (idx <= 7) {
5074 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5075 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5076 return -EIO;
5077 if (t > 127 || t < -127)
5078 t = TP_EC_THERMAL_TMP_NA;
5079 *value = t * 1000;
5080 return 0;
5082 break;
5084 case TPACPI_THERMAL_NONE:
5085 default:
5086 return -ENOSYS;
5089 return -EINVAL;
5092 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5094 int res, i;
5095 int n;
5097 n = 8;
5098 i = 0;
5100 if (!s)
5101 return -EINVAL;
5103 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5104 n = 16;
5106 for (i = 0 ; i < n; i++) {
5107 res = thermal_get_sensor(i, &s->temp[i]);
5108 if (res)
5109 return res;
5112 return n;
5115 /* sysfs temp##_input -------------------------------------------------- */
5117 static ssize_t thermal_temp_input_show(struct device *dev,
5118 struct device_attribute *attr,
5119 char *buf)
5121 struct sensor_device_attribute *sensor_attr =
5122 to_sensor_dev_attr(attr);
5123 int idx = sensor_attr->index;
5124 s32 value;
5125 int res;
5127 res = thermal_get_sensor(idx, &value);
5128 if (res)
5129 return res;
5130 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5131 return -ENXIO;
5133 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5136 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5137 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5138 thermal_temp_input_show, NULL, _idxB)
5140 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5141 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5142 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5143 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5144 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5145 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5146 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5147 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5148 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5149 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5150 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5151 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5152 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5153 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5154 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5155 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5156 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5159 #define THERMAL_ATTRS(X) \
5160 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5162 static struct attribute *thermal_temp_input_attr[] = {
5163 THERMAL_ATTRS(8),
5164 THERMAL_ATTRS(9),
5165 THERMAL_ATTRS(10),
5166 THERMAL_ATTRS(11),
5167 THERMAL_ATTRS(12),
5168 THERMAL_ATTRS(13),
5169 THERMAL_ATTRS(14),
5170 THERMAL_ATTRS(15),
5171 THERMAL_ATTRS(0),
5172 THERMAL_ATTRS(1),
5173 THERMAL_ATTRS(2),
5174 THERMAL_ATTRS(3),
5175 THERMAL_ATTRS(4),
5176 THERMAL_ATTRS(5),
5177 THERMAL_ATTRS(6),
5178 THERMAL_ATTRS(7),
5179 NULL
5182 static const struct attribute_group thermal_temp_input16_group = {
5183 .attrs = thermal_temp_input_attr
5186 static const struct attribute_group thermal_temp_input8_group = {
5187 .attrs = &thermal_temp_input_attr[8]
5190 #undef THERMAL_SENSOR_ATTR_TEMP
5191 #undef THERMAL_ATTRS
5193 /* --------------------------------------------------------------------- */
5195 static int __init thermal_init(struct ibm_init_struct *iibm)
5197 u8 t, ta1, ta2;
5198 int i;
5199 int acpi_tmp7;
5200 int res;
5202 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5204 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5206 if (thinkpad_id.ec_model) {
5208 * Direct EC access mode: sensors at registers
5209 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5210 * non-implemented, thermal sensors return 0x80 when
5211 * not available
5214 ta1 = ta2 = 0;
5215 for (i = 0; i < 8; i++) {
5216 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5217 ta1 |= t;
5218 } else {
5219 ta1 = 0;
5220 break;
5222 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5223 ta2 |= t;
5224 } else {
5225 ta1 = 0;
5226 break;
5229 if (ta1 == 0) {
5230 /* This is sheer paranoia, but we handle it anyway */
5231 if (acpi_tmp7) {
5232 printk(TPACPI_ERR
5233 "ThinkPad ACPI EC access misbehaving, "
5234 "falling back to ACPI TMPx access "
5235 "mode\n");
5236 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5237 } else {
5238 printk(TPACPI_ERR
5239 "ThinkPad ACPI EC access misbehaving, "
5240 "disabling thermal sensors access\n");
5241 thermal_read_mode = TPACPI_THERMAL_NONE;
5243 } else {
5244 thermal_read_mode =
5245 (ta2 != 0) ?
5246 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5248 } else if (acpi_tmp7) {
5249 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5250 /* 600e/x, 770e, 770x */
5251 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5252 } else {
5253 /* Standard ACPI TMPx access, max 8 sensors */
5254 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5256 } else {
5257 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5258 thermal_read_mode = TPACPI_THERMAL_NONE;
5261 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5262 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5263 thermal_read_mode);
5265 switch (thermal_read_mode) {
5266 case TPACPI_THERMAL_TPEC_16:
5267 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5268 &thermal_temp_input16_group);
5269 if (res)
5270 return res;
5271 break;
5272 case TPACPI_THERMAL_TPEC_8:
5273 case TPACPI_THERMAL_ACPI_TMP07:
5274 case TPACPI_THERMAL_ACPI_UPDT:
5275 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5276 &thermal_temp_input8_group);
5277 if (res)
5278 return res;
5279 break;
5280 case TPACPI_THERMAL_NONE:
5281 default:
5282 return 1;
5285 return 0;
5288 static void thermal_exit(void)
5290 switch (thermal_read_mode) {
5291 case TPACPI_THERMAL_TPEC_16:
5292 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5293 &thermal_temp_input16_group);
5294 break;
5295 case TPACPI_THERMAL_TPEC_8:
5296 case TPACPI_THERMAL_ACPI_TMP07:
5297 case TPACPI_THERMAL_ACPI_UPDT:
5298 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5299 &thermal_temp_input16_group);
5300 break;
5301 case TPACPI_THERMAL_NONE:
5302 default:
5303 break;
5307 static int thermal_read(char *p)
5309 int len = 0;
5310 int n, i;
5311 struct ibm_thermal_sensors_struct t;
5313 n = thermal_get_sensors(&t);
5314 if (unlikely(n < 0))
5315 return n;
5317 len += sprintf(p + len, "temperatures:\t");
5319 if (n > 0) {
5320 for (i = 0; i < (n - 1); i++)
5321 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5322 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5323 } else
5324 len += sprintf(p + len, "not supported\n");
5326 return len;
5329 static struct ibm_struct thermal_driver_data = {
5330 .name = "thermal",
5331 .read = thermal_read,
5332 .exit = thermal_exit,
5335 /*************************************************************************
5336 * EC Dump subdriver
5339 static u8 ecdump_regs[256];
5341 static int ecdump_read(char *p)
5343 int len = 0;
5344 int i, j;
5345 u8 v;
5347 len += sprintf(p + len, "EC "
5348 " +00 +01 +02 +03 +04 +05 +06 +07"
5349 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5350 for (i = 0; i < 256; i += 16) {
5351 len += sprintf(p + len, "EC 0x%02x:", i);
5352 for (j = 0; j < 16; j++) {
5353 if (!acpi_ec_read(i + j, &v))
5354 break;
5355 if (v != ecdump_regs[i + j])
5356 len += sprintf(p + len, " *%02x", v);
5357 else
5358 len += sprintf(p + len, " %02x", v);
5359 ecdump_regs[i + j] = v;
5361 len += sprintf(p + len, "\n");
5362 if (j != 16)
5363 break;
5366 /* These are way too dangerous to advertise openly... */
5367 #if 0
5368 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5369 " (<offset> is 00-ff, <value> is 00-ff)\n");
5370 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5371 " (<offset> is 00-ff, <value> is 0-255)\n");
5372 #endif
5373 return len;
5376 static int ecdump_write(char *buf)
5378 char *cmd;
5379 int i, v;
5381 while ((cmd = next_cmd(&buf))) {
5382 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5383 /* i and v set */
5384 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5385 /* i and v set */
5386 } else
5387 return -EINVAL;
5388 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5389 if (!acpi_ec_write(i, v))
5390 return -EIO;
5391 } else
5392 return -EINVAL;
5395 return 0;
5398 static struct ibm_struct ecdump_driver_data = {
5399 .name = "ecdump",
5400 .read = ecdump_read,
5401 .write = ecdump_write,
5402 .flags.experimental = 1,
5405 /*************************************************************************
5406 * Backlight/brightness subdriver
5409 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5412 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5413 * CMOS NVRAM byte 0x5E, bits 0-3.
5415 * EC HBRV (0x31) has the following layout
5416 * Bit 7: unknown function
5417 * Bit 6: unknown function
5418 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5419 * Bit 4: must be set to zero to avoid problems
5420 * Bit 3-0: backlight brightness level
5422 * brightness_get_raw returns status data in the HBRV layout
5424 * WARNING: The X61 has been verified to use HBRV for something else, so
5425 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5426 * testing on the very early *60 Lenovo models...
5429 enum {
5430 TP_EC_BACKLIGHT = 0x31,
5432 /* TP_EC_BACKLIGHT bitmasks */
5433 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5434 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5435 TP_EC_BACKLIGHT_MAPSW = 0x20,
5438 enum tpacpi_brightness_access_mode {
5439 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5440 TPACPI_BRGHT_MODE_EC, /* EC control */
5441 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5442 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5443 TPACPI_BRGHT_MODE_MAX
5446 static struct backlight_device *ibm_backlight_device;
5448 static enum tpacpi_brightness_access_mode brightness_mode =
5449 TPACPI_BRGHT_MODE_MAX;
5451 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5453 static struct mutex brightness_mutex;
5455 /* NVRAM brightness access,
5456 * call with brightness_mutex held! */
5457 static unsigned int tpacpi_brightness_nvram_get(void)
5459 u8 lnvram;
5461 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5462 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5463 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5464 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5466 return lnvram;
5469 static void tpacpi_brightness_checkpoint_nvram(void)
5471 u8 lec = 0;
5472 u8 b_nvram;
5474 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5475 return;
5477 vdbg_printk(TPACPI_DBG_BRGHT,
5478 "trying to checkpoint backlight level to NVRAM...\n");
5480 if (mutex_lock_killable(&brightness_mutex) < 0)
5481 return;
5483 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5484 goto unlock;
5485 lec &= TP_EC_BACKLIGHT_LVLMSK;
5486 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5488 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5489 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5490 /* NVRAM needs update */
5491 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5492 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5493 b_nvram |= lec;
5494 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5495 dbg_printk(TPACPI_DBG_BRGHT,
5496 "updated NVRAM backlight level to %u (0x%02x)\n",
5497 (unsigned int) lec, (unsigned int) b_nvram);
5498 } else
5499 vdbg_printk(TPACPI_DBG_BRGHT,
5500 "NVRAM backlight level already is %u (0x%02x)\n",
5501 (unsigned int) lec, (unsigned int) b_nvram);
5503 unlock:
5504 mutex_unlock(&brightness_mutex);
5508 /* call with brightness_mutex held! */
5509 static int tpacpi_brightness_get_raw(int *status)
5511 u8 lec = 0;
5513 switch (brightness_mode) {
5514 case TPACPI_BRGHT_MODE_UCMS_STEP:
5515 *status = tpacpi_brightness_nvram_get();
5516 return 0;
5517 case TPACPI_BRGHT_MODE_EC:
5518 case TPACPI_BRGHT_MODE_ECNVRAM:
5519 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5520 return -EIO;
5521 *status = lec;
5522 return 0;
5523 default:
5524 return -ENXIO;
5528 /* call with brightness_mutex held! */
5529 /* do NOT call with illegal backlight level value */
5530 static int tpacpi_brightness_set_ec(unsigned int value)
5532 u8 lec = 0;
5534 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5535 return -EIO;
5537 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5538 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5539 (value & TP_EC_BACKLIGHT_LVLMSK))))
5540 return -EIO;
5542 return 0;
5545 /* call with brightness_mutex held! */
5546 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5548 int cmos_cmd, inc;
5549 unsigned int current_value, i;
5551 current_value = tpacpi_brightness_nvram_get();
5553 if (value == current_value)
5554 return 0;
5556 cmos_cmd = (value > current_value) ?
5557 TP_CMOS_BRIGHTNESS_UP :
5558 TP_CMOS_BRIGHTNESS_DOWN;
5559 inc = (value > current_value) ? 1 : -1;
5561 for (i = current_value; i != value; i += inc)
5562 if (issue_thinkpad_cmos_command(cmos_cmd))
5563 return -EIO;
5565 return 0;
5568 /* May return EINTR which can always be mapped to ERESTARTSYS */
5569 static int brightness_set(unsigned int value)
5571 int res;
5573 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5574 value < 0)
5575 return -EINVAL;
5577 vdbg_printk(TPACPI_DBG_BRGHT,
5578 "set backlight level to %d\n", value);
5580 res = mutex_lock_killable(&brightness_mutex);
5581 if (res < 0)
5582 return res;
5584 switch (brightness_mode) {
5585 case TPACPI_BRGHT_MODE_EC:
5586 case TPACPI_BRGHT_MODE_ECNVRAM:
5587 res = tpacpi_brightness_set_ec(value);
5588 break;
5589 case TPACPI_BRGHT_MODE_UCMS_STEP:
5590 res = tpacpi_brightness_set_ucmsstep(value);
5591 break;
5592 default:
5593 res = -ENXIO;
5596 mutex_unlock(&brightness_mutex);
5597 return res;
5600 /* sysfs backlight class ----------------------------------------------- */
5602 static int brightness_update_status(struct backlight_device *bd)
5604 unsigned int level =
5605 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5606 bd->props.power == FB_BLANK_UNBLANK) ?
5607 bd->props.brightness : 0;
5609 dbg_printk(TPACPI_DBG_BRGHT,
5610 "backlight: attempt to set level to %d\n",
5611 level);
5613 /* it is the backlight class's job (caller) to handle
5614 * EINTR and other errors properly */
5615 return brightness_set(level);
5618 static int brightness_get(struct backlight_device *bd)
5620 int status, res;
5622 res = mutex_lock_killable(&brightness_mutex);
5623 if (res < 0)
5624 return 0;
5626 res = tpacpi_brightness_get_raw(&status);
5628 mutex_unlock(&brightness_mutex);
5630 if (res < 0)
5631 return 0;
5633 return status & TP_EC_BACKLIGHT_LVLMSK;
5636 static struct backlight_ops ibm_backlight_data = {
5637 .get_brightness = brightness_get,
5638 .update_status = brightness_update_status,
5641 /* --------------------------------------------------------------------- */
5643 static int __init brightness_init(struct ibm_init_struct *iibm)
5645 int b;
5647 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5649 mutex_init(&brightness_mutex);
5652 * We always attempt to detect acpi support, so as to switch
5653 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5654 * going to publish a backlight interface
5656 b = tpacpi_check_std_acpi_brightness_support();
5657 if (b > 0) {
5659 if (acpi_video_backlight_support()) {
5660 if (brightness_enable > 1) {
5661 printk(TPACPI_NOTICE
5662 "Standard ACPI backlight interface "
5663 "available, not loading native one.\n");
5664 return 1;
5665 } else if (brightness_enable == 1) {
5666 printk(TPACPI_NOTICE
5667 "Backlight control force enabled, even if standard "
5668 "ACPI backlight interface is available\n");
5670 } else {
5671 if (brightness_enable > 1) {
5672 printk(TPACPI_NOTICE
5673 "Standard ACPI backlight interface not "
5674 "available, thinkpad_acpi native "
5675 "brightness control enabled\n");
5680 if (!brightness_enable) {
5681 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5682 "brightness support disabled by "
5683 "module parameter\n");
5684 return 1;
5687 if (b > 16) {
5688 printk(TPACPI_ERR
5689 "Unsupported brightness interface, "
5690 "please contact %s\n", TPACPI_MAIL);
5691 return 1;
5693 if (b == 16)
5694 tp_features.bright_16levels = 1;
5697 * Check for module parameter bogosity, note that we
5698 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5699 * able to detect "unspecified"
5701 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5702 return -EINVAL;
5704 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5705 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5706 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5707 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5709 * IBM models that define HBRV probably have
5710 * EC-based backlight level control
5712 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5713 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5714 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5715 else
5716 /* all other IBM ThinkPads */
5717 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5718 } else
5719 /* All Lenovo ThinkPads */
5720 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5722 dbg_printk(TPACPI_DBG_BRGHT,
5723 "selected brightness_mode=%d\n",
5724 brightness_mode);
5727 /* Safety */
5728 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5729 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5730 brightness_mode == TPACPI_BRGHT_MODE_EC))
5731 return -EINVAL;
5733 if (tpacpi_brightness_get_raw(&b) < 0)
5734 return 1;
5736 if (tp_features.bright_16levels)
5737 printk(TPACPI_INFO
5738 "detected a 16-level brightness capable ThinkPad\n");
5740 ibm_backlight_device = backlight_device_register(
5741 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5742 &ibm_backlight_data);
5743 if (IS_ERR(ibm_backlight_device)) {
5744 printk(TPACPI_ERR "Could not register backlight device\n");
5745 return PTR_ERR(ibm_backlight_device);
5747 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5748 "brightness is supported\n");
5750 ibm_backlight_device->props.max_brightness =
5751 (tp_features.bright_16levels)? 15 : 7;
5752 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5753 backlight_update_status(ibm_backlight_device);
5755 return 0;
5758 static void brightness_suspend(pm_message_t state)
5760 tpacpi_brightness_checkpoint_nvram();
5763 static void brightness_shutdown(void)
5765 tpacpi_brightness_checkpoint_nvram();
5768 static void brightness_exit(void)
5770 if (ibm_backlight_device) {
5771 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5772 "calling backlight_device_unregister()\n");
5773 backlight_device_unregister(ibm_backlight_device);
5776 tpacpi_brightness_checkpoint_nvram();
5779 static int brightness_read(char *p)
5781 int len = 0;
5782 int level;
5784 level = brightness_get(NULL);
5785 if (level < 0) {
5786 len += sprintf(p + len, "level:\t\tunreadable\n");
5787 } else {
5788 len += sprintf(p + len, "level:\t\t%d\n", level);
5789 len += sprintf(p + len, "commands:\tup, down\n");
5790 len += sprintf(p + len, "commands:\tlevel <level>"
5791 " (<level> is 0-%d)\n",
5792 (tp_features.bright_16levels) ? 15 : 7);
5795 return len;
5798 static int brightness_write(char *buf)
5800 int level;
5801 int rc;
5802 char *cmd;
5803 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5805 level = brightness_get(NULL);
5806 if (level < 0)
5807 return level;
5809 while ((cmd = next_cmd(&buf))) {
5810 if (strlencmp(cmd, "up") == 0) {
5811 if (level < max_level)
5812 level++;
5813 } else if (strlencmp(cmd, "down") == 0) {
5814 if (level > 0)
5815 level--;
5816 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5817 level >= 0 && level <= max_level) {
5818 /* new level set */
5819 } else
5820 return -EINVAL;
5823 tpacpi_disclose_usertask("procfs brightness",
5824 "set level to %d\n", level);
5827 * Now we know what the final level should be, so we try to set it.
5828 * Doing it this way makes the syscall restartable in case of EINTR
5830 rc = brightness_set(level);
5831 return (rc == -EINTR)? ERESTARTSYS : rc;
5834 static struct ibm_struct brightness_driver_data = {
5835 .name = "brightness",
5836 .read = brightness_read,
5837 .write = brightness_write,
5838 .exit = brightness_exit,
5839 .suspend = brightness_suspend,
5840 .shutdown = brightness_shutdown,
5843 /*************************************************************************
5844 * Volume subdriver
5847 static int volume_offset = 0x30;
5849 static int volume_read(char *p)
5851 int len = 0;
5852 u8 level;
5854 if (!acpi_ec_read(volume_offset, &level)) {
5855 len += sprintf(p + len, "level:\t\tunreadable\n");
5856 } else {
5857 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5858 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5859 len += sprintf(p + len, "commands:\tup, down, mute\n");
5860 len += sprintf(p + len, "commands:\tlevel <level>"
5861 " (<level> is 0-15)\n");
5864 return len;
5867 static int volume_write(char *buf)
5869 int cmos_cmd, inc, i;
5870 u8 level, mute;
5871 int new_level, new_mute;
5872 char *cmd;
5874 while ((cmd = next_cmd(&buf))) {
5875 if (!acpi_ec_read(volume_offset, &level))
5876 return -EIO;
5877 new_mute = mute = level & 0x40;
5878 new_level = level = level & 0xf;
5880 if (strlencmp(cmd, "up") == 0) {
5881 if (mute)
5882 new_mute = 0;
5883 else
5884 new_level = level == 15 ? 15 : level + 1;
5885 } else if (strlencmp(cmd, "down") == 0) {
5886 if (mute)
5887 new_mute = 0;
5888 else
5889 new_level = level == 0 ? 0 : level - 1;
5890 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5891 new_level >= 0 && new_level <= 15) {
5892 /* new_level set */
5893 } else if (strlencmp(cmd, "mute") == 0) {
5894 new_mute = 0x40;
5895 } else
5896 return -EINVAL;
5898 if (new_level != level) {
5899 /* mute doesn't change */
5901 cmos_cmd = (new_level > level) ?
5902 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5903 inc = new_level > level ? 1 : -1;
5905 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5906 !acpi_ec_write(volume_offset, level)))
5907 return -EIO;
5909 for (i = level; i != new_level; i += inc)
5910 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5911 !acpi_ec_write(volume_offset, i + inc))
5912 return -EIO;
5914 if (mute &&
5915 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5916 !acpi_ec_write(volume_offset, new_level + mute))) {
5917 return -EIO;
5921 if (new_mute != mute) {
5922 /* level doesn't change */
5924 cmos_cmd = (new_mute) ?
5925 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5927 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5928 !acpi_ec_write(volume_offset, level + new_mute))
5929 return -EIO;
5933 return 0;
5936 static struct ibm_struct volume_driver_data = {
5937 .name = "volume",
5938 .read = volume_read,
5939 .write = volume_write,
5942 /*************************************************************************
5943 * Fan subdriver
5947 * FAN ACCESS MODES
5949 * TPACPI_FAN_RD_ACPI_GFAN:
5950 * ACPI GFAN method: returns fan level
5952 * see TPACPI_FAN_WR_ACPI_SFAN
5953 * EC 0x2f (HFSP) not available if GFAN exists
5955 * TPACPI_FAN_WR_ACPI_SFAN:
5956 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5958 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5959 * it for writing.
5961 * TPACPI_FAN_WR_TPEC:
5962 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5963 * Supported on almost all ThinkPads
5965 * Fan speed changes of any sort (including those caused by the
5966 * disengaged mode) are usually done slowly by the firmware as the
5967 * maximum ammount of fan duty cycle change per second seems to be
5968 * limited.
5970 * Reading is not available if GFAN exists.
5971 * Writing is not available if SFAN exists.
5973 * Bits
5974 * 7 automatic mode engaged;
5975 * (default operation mode of the ThinkPad)
5976 * fan level is ignored in this mode.
5977 * 6 full speed mode (takes precedence over bit 7);
5978 * not available on all thinkpads. May disable
5979 * the tachometer while the fan controller ramps up
5980 * the speed (which can take up to a few *minutes*).
5981 * Speeds up fan to 100% duty-cycle, which is far above
5982 * the standard RPM levels. It is not impossible that
5983 * it could cause hardware damage.
5984 * 5-3 unused in some models. Extra bits for fan level
5985 * in others, but still useless as all values above
5986 * 7 map to the same speed as level 7 in these models.
5987 * 2-0 fan level (0..7 usually)
5988 * 0x00 = stop
5989 * 0x07 = max (set when temperatures critical)
5990 * Some ThinkPads may have other levels, see
5991 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5993 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5994 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5995 * does so, its initial value is meaningless (0x07).
5997 * For firmware bugs, refer to:
5998 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6000 * ----
6002 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6003 * Main fan tachometer reading (in RPM)
6005 * This register is present on all ThinkPads with a new-style EC, and
6006 * it is known not to be present on the A21m/e, and T22, as there is
6007 * something else in offset 0x84 according to the ACPI DSDT. Other
6008 * ThinkPads from this same time period (and earlier) probably lack the
6009 * tachometer as well.
6011 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6012 * was never fixed by IBM to report the EC firmware version string
6013 * probably support the tachometer (like the early X models), so
6014 * detecting it is quite hard. We need more data to know for sure.
6016 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6017 * might result.
6019 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6020 * mode.
6022 * For firmware bugs, refer to:
6023 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6025 * ----
6027 * ThinkPad EC register 0x31 bit 0 (only on select models)
6029 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6030 * show the speed of the main fan. When bit 0 of EC register 0x31
6031 * is one, the tachometer registers show the speed of the auxiliary
6032 * fan.
6034 * Fan control seems to affect both fans, regardless of the state
6035 * of this bit.
6037 * So far, only the firmware for the X60/X61 non-tablet versions
6038 * seem to support this (firmware TP-7M).
6040 * TPACPI_FAN_WR_ACPI_FANS:
6041 * ThinkPad X31, X40, X41. Not available in the X60.
6043 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6044 * high speed. ACPI DSDT seems to map these three speeds to levels
6045 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6046 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6048 * The speeds are stored on handles
6049 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6051 * There are three default speed sets, acessible as handles:
6052 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6054 * ACPI DSDT switches which set is in use depending on various
6055 * factors.
6057 * TPACPI_FAN_WR_TPEC is also available and should be used to
6058 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6059 * but the ACPI tables just mention level 7.
6062 enum { /* Fan control constants */
6063 fan_status_offset = 0x2f, /* EC register 0x2f */
6064 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6065 * 0x84 must be read before 0x85 */
6066 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6067 bit 0 selects which fan is active */
6069 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6070 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6072 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6075 enum fan_status_access_mode {
6076 TPACPI_FAN_NONE = 0, /* No fan status or control */
6077 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6078 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6081 enum fan_control_access_mode {
6082 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6083 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6084 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6085 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6088 enum fan_control_commands {
6089 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6090 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6091 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6092 * and also watchdog cmd */
6095 static int fan_control_allowed;
6097 static enum fan_status_access_mode fan_status_access_mode;
6098 static enum fan_control_access_mode fan_control_access_mode;
6099 static enum fan_control_commands fan_control_commands;
6101 static u8 fan_control_initial_status;
6102 static u8 fan_control_desired_level;
6103 static u8 fan_control_resume_level;
6104 static int fan_watchdog_maxinterval;
6106 static struct mutex fan_mutex;
6108 static void fan_watchdog_fire(struct work_struct *ignored);
6109 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6111 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6112 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6113 "\\FSPD", /* 600e/x, 770e, 770x */
6114 ); /* all others */
6115 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6116 "JFNS", /* 770x-JL */
6117 ); /* all others */
6120 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6121 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6122 * be in auto mode (0x80).
6124 * This is corrected by any write to HFSP either by the driver, or
6125 * by the firmware.
6127 * We assume 0x07 really means auto mode while this quirk is active,
6128 * as this is far more likely than the ThinkPad being in level 7,
6129 * which is only used by the firmware during thermal emergencies.
6131 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6132 * TP-70 (T43, R52), which are known to be buggy.
6135 static void fan_quirk1_setup(void)
6137 if (fan_control_initial_status == 0x07) {
6138 printk(TPACPI_NOTICE
6139 "fan_init: initial fan status is unknown, "
6140 "assuming it is in auto mode\n");
6141 tp_features.fan_ctrl_status_undef = 1;
6145 static void fan_quirk1_handle(u8 *fan_status)
6147 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6148 if (*fan_status != fan_control_initial_status) {
6149 /* something changed the HFSP regisnter since
6150 * driver init time, so it is not undefined
6151 * anymore */
6152 tp_features.fan_ctrl_status_undef = 0;
6153 } else {
6154 /* Return most likely status. In fact, it
6155 * might be the only possible status */
6156 *fan_status = TP_EC_FAN_AUTO;
6161 /* Select main fan on X60/X61, NOOP on others */
6162 static bool fan_select_fan1(void)
6164 if (tp_features.second_fan) {
6165 u8 val;
6167 if (ec_read(fan_select_offset, &val) < 0)
6168 return false;
6169 val &= 0xFEU;
6170 if (ec_write(fan_select_offset, val) < 0)
6171 return false;
6173 return true;
6176 /* Select secondary fan on X60/X61 */
6177 static bool fan_select_fan2(void)
6179 u8 val;
6181 if (!tp_features.second_fan)
6182 return false;
6184 if (ec_read(fan_select_offset, &val) < 0)
6185 return false;
6186 val |= 0x01U;
6187 if (ec_write(fan_select_offset, val) < 0)
6188 return false;
6190 return true;
6194 * Call with fan_mutex held
6196 static void fan_update_desired_level(u8 status)
6198 if ((status &
6199 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6200 if (status > 7)
6201 fan_control_desired_level = 7;
6202 else
6203 fan_control_desired_level = status;
6207 static int fan_get_status(u8 *status)
6209 u8 s;
6211 /* TODO:
6212 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6214 switch (fan_status_access_mode) {
6215 case TPACPI_FAN_RD_ACPI_GFAN:
6216 /* 570, 600e/x, 770e, 770x */
6218 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6219 return -EIO;
6221 if (likely(status))
6222 *status = s & 0x07;
6224 break;
6226 case TPACPI_FAN_RD_TPEC:
6227 /* all except 570, 600e/x, 770e, 770x */
6228 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6229 return -EIO;
6231 if (likely(status)) {
6232 *status = s;
6233 fan_quirk1_handle(status);
6236 break;
6238 default:
6239 return -ENXIO;
6242 return 0;
6245 static int fan_get_status_safe(u8 *status)
6247 int rc;
6248 u8 s;
6250 if (mutex_lock_killable(&fan_mutex))
6251 return -ERESTARTSYS;
6252 rc = fan_get_status(&s);
6253 if (!rc)
6254 fan_update_desired_level(s);
6255 mutex_unlock(&fan_mutex);
6257 if (status)
6258 *status = s;
6260 return rc;
6263 static int fan_get_speed(unsigned int *speed)
6265 u8 hi, lo;
6267 switch (fan_status_access_mode) {
6268 case TPACPI_FAN_RD_TPEC:
6269 /* all except 570, 600e/x, 770e, 770x */
6270 if (unlikely(!fan_select_fan1()))
6271 return -EIO;
6272 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6273 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6274 return -EIO;
6276 if (likely(speed))
6277 *speed = (hi << 8) | lo;
6279 break;
6281 default:
6282 return -ENXIO;
6285 return 0;
6288 static int fan2_get_speed(unsigned int *speed)
6290 u8 hi, lo;
6291 bool rc;
6293 switch (fan_status_access_mode) {
6294 case TPACPI_FAN_RD_TPEC:
6295 /* all except 570, 600e/x, 770e, 770x */
6296 if (unlikely(!fan_select_fan2()))
6297 return -EIO;
6298 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6299 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6300 fan_select_fan1(); /* play it safe */
6301 if (rc)
6302 return -EIO;
6304 if (likely(speed))
6305 *speed = (hi << 8) | lo;
6307 break;
6309 default:
6310 return -ENXIO;
6313 return 0;
6316 static int fan_set_level(int level)
6318 if (!fan_control_allowed)
6319 return -EPERM;
6321 switch (fan_control_access_mode) {
6322 case TPACPI_FAN_WR_ACPI_SFAN:
6323 if (level >= 0 && level <= 7) {
6324 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6325 return -EIO;
6326 } else
6327 return -EINVAL;
6328 break;
6330 case TPACPI_FAN_WR_ACPI_FANS:
6331 case TPACPI_FAN_WR_TPEC:
6332 if (!(level & TP_EC_FAN_AUTO) &&
6333 !(level & TP_EC_FAN_FULLSPEED) &&
6334 ((level < 0) || (level > 7)))
6335 return -EINVAL;
6337 /* safety net should the EC not support AUTO
6338 * or FULLSPEED mode bits and just ignore them */
6339 if (level & TP_EC_FAN_FULLSPEED)
6340 level |= 7; /* safety min speed 7 */
6341 else if (level & TP_EC_FAN_AUTO)
6342 level |= 4; /* safety min speed 4 */
6344 if (!acpi_ec_write(fan_status_offset, level))
6345 return -EIO;
6346 else
6347 tp_features.fan_ctrl_status_undef = 0;
6348 break;
6350 default:
6351 return -ENXIO;
6354 vdbg_printk(TPACPI_DBG_FAN,
6355 "fan control: set fan control register to 0x%02x\n", level);
6356 return 0;
6359 static int fan_set_level_safe(int level)
6361 int rc;
6363 if (!fan_control_allowed)
6364 return -EPERM;
6366 if (mutex_lock_killable(&fan_mutex))
6367 return -ERESTARTSYS;
6369 if (level == TPACPI_FAN_LAST_LEVEL)
6370 level = fan_control_desired_level;
6372 rc = fan_set_level(level);
6373 if (!rc)
6374 fan_update_desired_level(level);
6376 mutex_unlock(&fan_mutex);
6377 return rc;
6380 static int fan_set_enable(void)
6382 u8 s;
6383 int rc;
6385 if (!fan_control_allowed)
6386 return -EPERM;
6388 if (mutex_lock_killable(&fan_mutex))
6389 return -ERESTARTSYS;
6391 switch (fan_control_access_mode) {
6392 case TPACPI_FAN_WR_ACPI_FANS:
6393 case TPACPI_FAN_WR_TPEC:
6394 rc = fan_get_status(&s);
6395 if (rc < 0)
6396 break;
6398 /* Don't go out of emergency fan mode */
6399 if (s != 7) {
6400 s &= 0x07;
6401 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6404 if (!acpi_ec_write(fan_status_offset, s))
6405 rc = -EIO;
6406 else {
6407 tp_features.fan_ctrl_status_undef = 0;
6408 rc = 0;
6410 break;
6412 case TPACPI_FAN_WR_ACPI_SFAN:
6413 rc = fan_get_status(&s);
6414 if (rc < 0)
6415 break;
6417 s &= 0x07;
6419 /* Set fan to at least level 4 */
6420 s |= 4;
6422 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6423 rc = -EIO;
6424 else
6425 rc = 0;
6426 break;
6428 default:
6429 rc = -ENXIO;
6432 mutex_unlock(&fan_mutex);
6434 if (!rc)
6435 vdbg_printk(TPACPI_DBG_FAN,
6436 "fan control: set fan control register to 0x%02x\n",
6438 return rc;
6441 static int fan_set_disable(void)
6443 int rc;
6445 if (!fan_control_allowed)
6446 return -EPERM;
6448 if (mutex_lock_killable(&fan_mutex))
6449 return -ERESTARTSYS;
6451 rc = 0;
6452 switch (fan_control_access_mode) {
6453 case TPACPI_FAN_WR_ACPI_FANS:
6454 case TPACPI_FAN_WR_TPEC:
6455 if (!acpi_ec_write(fan_status_offset, 0x00))
6456 rc = -EIO;
6457 else {
6458 fan_control_desired_level = 0;
6459 tp_features.fan_ctrl_status_undef = 0;
6461 break;
6463 case TPACPI_FAN_WR_ACPI_SFAN:
6464 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6465 rc = -EIO;
6466 else
6467 fan_control_desired_level = 0;
6468 break;
6470 default:
6471 rc = -ENXIO;
6474 if (!rc)
6475 vdbg_printk(TPACPI_DBG_FAN,
6476 "fan control: set fan control register to 0\n");
6478 mutex_unlock(&fan_mutex);
6479 return rc;
6482 static int fan_set_speed(int speed)
6484 int rc;
6486 if (!fan_control_allowed)
6487 return -EPERM;
6489 if (mutex_lock_killable(&fan_mutex))
6490 return -ERESTARTSYS;
6492 rc = 0;
6493 switch (fan_control_access_mode) {
6494 case TPACPI_FAN_WR_ACPI_FANS:
6495 if (speed >= 0 && speed <= 65535) {
6496 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6497 speed, speed, speed))
6498 rc = -EIO;
6499 } else
6500 rc = -EINVAL;
6501 break;
6503 default:
6504 rc = -ENXIO;
6507 mutex_unlock(&fan_mutex);
6508 return rc;
6511 static void fan_watchdog_reset(void)
6513 static int fan_watchdog_active;
6515 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6516 return;
6518 if (fan_watchdog_active)
6519 cancel_delayed_work(&fan_watchdog_task);
6521 if (fan_watchdog_maxinterval > 0 &&
6522 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6523 fan_watchdog_active = 1;
6524 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6525 msecs_to_jiffies(fan_watchdog_maxinterval
6526 * 1000))) {
6527 printk(TPACPI_ERR
6528 "failed to queue the fan watchdog, "
6529 "watchdog will not trigger\n");
6531 } else
6532 fan_watchdog_active = 0;
6535 static void fan_watchdog_fire(struct work_struct *ignored)
6537 int rc;
6539 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6540 return;
6542 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6543 rc = fan_set_enable();
6544 if (rc < 0) {
6545 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6546 "will try again later...\n", -rc);
6547 /* reschedule for later */
6548 fan_watchdog_reset();
6553 * SYSFS fan layout: hwmon compatible (device)
6555 * pwm*_enable:
6556 * 0: "disengaged" mode
6557 * 1: manual mode
6558 * 2: native EC "auto" mode (recommended, hardware default)
6560 * pwm*: set speed in manual mode, ignored otherwise.
6561 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6562 * interpolation.
6564 * fan*_input: tachometer reading, RPM
6567 * SYSFS fan layout: extensions
6569 * fan_watchdog (driver):
6570 * fan watchdog interval in seconds, 0 disables (default), max 120
6573 /* sysfs fan pwm1_enable ----------------------------------------------- */
6574 static ssize_t fan_pwm1_enable_show(struct device *dev,
6575 struct device_attribute *attr,
6576 char *buf)
6578 int res, mode;
6579 u8 status;
6581 res = fan_get_status_safe(&status);
6582 if (res)
6583 return res;
6585 if (status & TP_EC_FAN_FULLSPEED) {
6586 mode = 0;
6587 } else if (status & TP_EC_FAN_AUTO) {
6588 mode = 2;
6589 } else
6590 mode = 1;
6592 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6595 static ssize_t fan_pwm1_enable_store(struct device *dev,
6596 struct device_attribute *attr,
6597 const char *buf, size_t count)
6599 unsigned long t;
6600 int res, level;
6602 if (parse_strtoul(buf, 2, &t))
6603 return -EINVAL;
6605 tpacpi_disclose_usertask("hwmon pwm1_enable",
6606 "set fan mode to %lu\n", t);
6608 switch (t) {
6609 case 0:
6610 level = TP_EC_FAN_FULLSPEED;
6611 break;
6612 case 1:
6613 level = TPACPI_FAN_LAST_LEVEL;
6614 break;
6615 case 2:
6616 level = TP_EC_FAN_AUTO;
6617 break;
6618 case 3:
6619 /* reserved for software-controlled auto mode */
6620 return -ENOSYS;
6621 default:
6622 return -EINVAL;
6625 res = fan_set_level_safe(level);
6626 if (res == -ENXIO)
6627 return -EINVAL;
6628 else if (res < 0)
6629 return res;
6631 fan_watchdog_reset();
6633 return count;
6636 static struct device_attribute dev_attr_fan_pwm1_enable =
6637 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6638 fan_pwm1_enable_show, fan_pwm1_enable_store);
6640 /* sysfs fan pwm1 ------------------------------------------------------ */
6641 static ssize_t fan_pwm1_show(struct device *dev,
6642 struct device_attribute *attr,
6643 char *buf)
6645 int res;
6646 u8 status;
6648 res = fan_get_status_safe(&status);
6649 if (res)
6650 return res;
6652 if ((status &
6653 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6654 status = fan_control_desired_level;
6656 if (status > 7)
6657 status = 7;
6659 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6662 static ssize_t fan_pwm1_store(struct device *dev,
6663 struct device_attribute *attr,
6664 const char *buf, size_t count)
6666 unsigned long s;
6667 int rc;
6668 u8 status, newlevel;
6670 if (parse_strtoul(buf, 255, &s))
6671 return -EINVAL;
6673 tpacpi_disclose_usertask("hwmon pwm1",
6674 "set fan speed to %lu\n", s);
6676 /* scale down from 0-255 to 0-7 */
6677 newlevel = (s >> 5) & 0x07;
6679 if (mutex_lock_killable(&fan_mutex))
6680 return -ERESTARTSYS;
6682 rc = fan_get_status(&status);
6683 if (!rc && (status &
6684 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6685 rc = fan_set_level(newlevel);
6686 if (rc == -ENXIO)
6687 rc = -EINVAL;
6688 else if (!rc) {
6689 fan_update_desired_level(newlevel);
6690 fan_watchdog_reset();
6694 mutex_unlock(&fan_mutex);
6695 return (rc)? rc : count;
6698 static struct device_attribute dev_attr_fan_pwm1 =
6699 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6700 fan_pwm1_show, fan_pwm1_store);
6702 /* sysfs fan fan1_input ------------------------------------------------ */
6703 static ssize_t fan_fan1_input_show(struct device *dev,
6704 struct device_attribute *attr,
6705 char *buf)
6707 int res;
6708 unsigned int speed;
6710 res = fan_get_speed(&speed);
6711 if (res < 0)
6712 return res;
6714 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6717 static struct device_attribute dev_attr_fan_fan1_input =
6718 __ATTR(fan1_input, S_IRUGO,
6719 fan_fan1_input_show, NULL);
6721 /* sysfs fan fan2_input ------------------------------------------------ */
6722 static ssize_t fan_fan2_input_show(struct device *dev,
6723 struct device_attribute *attr,
6724 char *buf)
6726 int res;
6727 unsigned int speed;
6729 res = fan2_get_speed(&speed);
6730 if (res < 0)
6731 return res;
6733 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6736 static struct device_attribute dev_attr_fan_fan2_input =
6737 __ATTR(fan2_input, S_IRUGO,
6738 fan_fan2_input_show, NULL);
6740 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6741 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6742 char *buf)
6744 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6747 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6748 const char *buf, size_t count)
6750 unsigned long t;
6752 if (parse_strtoul(buf, 120, &t))
6753 return -EINVAL;
6755 if (!fan_control_allowed)
6756 return -EPERM;
6758 fan_watchdog_maxinterval = t;
6759 fan_watchdog_reset();
6761 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6763 return count;
6766 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6767 fan_fan_watchdog_show, fan_fan_watchdog_store);
6769 /* --------------------------------------------------------------------- */
6770 static struct attribute *fan_attributes[] = {
6771 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6772 &dev_attr_fan_fan1_input.attr,
6773 NULL, /* for fan2_input */
6774 NULL
6777 static const struct attribute_group fan_attr_group = {
6778 .attrs = fan_attributes,
6781 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6782 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
6784 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6785 { .vendor = PCI_VENDOR_ID_IBM, \
6786 .bios = TPACPI_MATCH_ANY, \
6787 .ec = TPID(__id1, __id2), \
6788 .quirks = __quirks }
6790 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
6791 { .vendor = PCI_VENDOR_ID_LENOVO, \
6792 .bios = TPACPI_MATCH_ANY, \
6793 .ec = TPID(__id1, __id2), \
6794 .quirks = __quirks }
6796 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6797 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6798 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6799 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6800 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
6801 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
6804 #undef TPACPI_FAN_QL
6805 #undef TPACPI_FAN_QI
6807 static int __init fan_init(struct ibm_init_struct *iibm)
6809 int rc;
6810 unsigned long quirks;
6812 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6813 "initializing fan subdriver\n");
6815 mutex_init(&fan_mutex);
6816 fan_status_access_mode = TPACPI_FAN_NONE;
6817 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6818 fan_control_commands = 0;
6819 fan_watchdog_maxinterval = 0;
6820 tp_features.fan_ctrl_status_undef = 0;
6821 tp_features.second_fan = 0;
6822 fan_control_desired_level = 7;
6824 TPACPI_ACPIHANDLE_INIT(fans);
6825 TPACPI_ACPIHANDLE_INIT(gfan);
6826 TPACPI_ACPIHANDLE_INIT(sfan);
6828 quirks = tpacpi_check_quirks(fan_quirk_table,
6829 ARRAY_SIZE(fan_quirk_table));
6831 if (gfan_handle) {
6832 /* 570, 600e/x, 770e, 770x */
6833 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6834 } else {
6835 /* all other ThinkPads: note that even old-style
6836 * ThinkPad ECs supports the fan control register */
6837 if (likely(acpi_ec_read(fan_status_offset,
6838 &fan_control_initial_status))) {
6839 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6840 if (quirks & TPACPI_FAN_Q1)
6841 fan_quirk1_setup();
6842 if (quirks & TPACPI_FAN_2FAN) {
6843 tp_features.second_fan = 1;
6844 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6845 "secondary fan support enabled\n");
6847 } else {
6848 printk(TPACPI_ERR
6849 "ThinkPad ACPI EC access misbehaving, "
6850 "fan status and control unavailable\n");
6851 return 1;
6855 if (sfan_handle) {
6856 /* 570, 770x-JL */
6857 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6858 fan_control_commands |=
6859 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6860 } else {
6861 if (!gfan_handle) {
6862 /* gfan without sfan means no fan control */
6863 /* all other models implement TP EC 0x2f control */
6865 if (fans_handle) {
6866 /* X31, X40, X41 */
6867 fan_control_access_mode =
6868 TPACPI_FAN_WR_ACPI_FANS;
6869 fan_control_commands |=
6870 TPACPI_FAN_CMD_SPEED |
6871 TPACPI_FAN_CMD_LEVEL |
6872 TPACPI_FAN_CMD_ENABLE;
6873 } else {
6874 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6875 fan_control_commands |=
6876 TPACPI_FAN_CMD_LEVEL |
6877 TPACPI_FAN_CMD_ENABLE;
6882 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6883 "fan is %s, modes %d, %d\n",
6884 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6885 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6886 fan_status_access_mode, fan_control_access_mode);
6888 /* fan control master switch */
6889 if (!fan_control_allowed) {
6890 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6891 fan_control_commands = 0;
6892 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6893 "fan control features disabled by parameter\n");
6896 /* update fan_control_desired_level */
6897 if (fan_status_access_mode != TPACPI_FAN_NONE)
6898 fan_get_status_safe(NULL);
6900 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6901 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6902 if (tp_features.second_fan) {
6903 /* attach second fan tachometer */
6904 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
6905 &dev_attr_fan_fan2_input.attr;
6907 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6908 &fan_attr_group);
6909 if (rc < 0)
6910 return rc;
6912 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6913 &driver_attr_fan_watchdog);
6914 if (rc < 0) {
6915 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6916 &fan_attr_group);
6917 return rc;
6919 return 0;
6920 } else
6921 return 1;
6924 static void fan_exit(void)
6926 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
6927 "cancelling any pending fan watchdog tasks\n");
6929 /* FIXME: can we really do this unconditionally? */
6930 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6931 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6932 &driver_attr_fan_watchdog);
6934 cancel_delayed_work(&fan_watchdog_task);
6935 flush_workqueue(tpacpi_wq);
6938 static void fan_suspend(pm_message_t state)
6940 int rc;
6942 if (!fan_control_allowed)
6943 return;
6945 /* Store fan status in cache */
6946 fan_control_resume_level = 0;
6947 rc = fan_get_status_safe(&fan_control_resume_level);
6948 if (rc < 0)
6949 printk(TPACPI_NOTICE
6950 "failed to read fan level for later "
6951 "restore during resume: %d\n", rc);
6953 /* if it is undefined, don't attempt to restore it.
6954 * KEEP THIS LAST */
6955 if (tp_features.fan_ctrl_status_undef)
6956 fan_control_resume_level = 0;
6959 static void fan_resume(void)
6961 u8 current_level = 7;
6962 bool do_set = false;
6963 int rc;
6965 /* DSDT *always* updates status on resume */
6966 tp_features.fan_ctrl_status_undef = 0;
6968 if (!fan_control_allowed ||
6969 !fan_control_resume_level ||
6970 (fan_get_status_safe(&current_level) < 0))
6971 return;
6973 switch (fan_control_access_mode) {
6974 case TPACPI_FAN_WR_ACPI_SFAN:
6975 /* never decrease fan level */
6976 do_set = (fan_control_resume_level > current_level);
6977 break;
6978 case TPACPI_FAN_WR_ACPI_FANS:
6979 case TPACPI_FAN_WR_TPEC:
6980 /* never decrease fan level, scale is:
6981 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6983 * We expect the firmware to set either 7 or AUTO, but we
6984 * handle FULLSPEED out of paranoia.
6986 * So, we can safely only restore FULLSPEED or 7, anything
6987 * else could slow the fan. Restoring AUTO is useless, at
6988 * best that's exactly what the DSDT already set (it is the
6989 * slower it uses).
6991 * Always keep in mind that the DSDT *will* have set the
6992 * fans to what the vendor supposes is the best level. We
6993 * muck with it only to speed the fan up.
6995 if (fan_control_resume_level != 7 &&
6996 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6997 return;
6998 else
6999 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7000 (current_level != fan_control_resume_level);
7001 break;
7002 default:
7003 return;
7005 if (do_set) {
7006 printk(TPACPI_NOTICE
7007 "restoring fan level to 0x%02x\n",
7008 fan_control_resume_level);
7009 rc = fan_set_level_safe(fan_control_resume_level);
7010 if (rc < 0)
7011 printk(TPACPI_NOTICE
7012 "failed to restore fan level: %d\n", rc);
7016 static int fan_read(char *p)
7018 int len = 0;
7019 int rc;
7020 u8 status;
7021 unsigned int speed = 0;
7023 switch (fan_status_access_mode) {
7024 case TPACPI_FAN_RD_ACPI_GFAN:
7025 /* 570, 600e/x, 770e, 770x */
7026 rc = fan_get_status_safe(&status);
7027 if (rc < 0)
7028 return rc;
7030 len += sprintf(p + len, "status:\t\t%s\n"
7031 "level:\t\t%d\n",
7032 (status != 0) ? "enabled" : "disabled", status);
7033 break;
7035 case TPACPI_FAN_RD_TPEC:
7036 /* all except 570, 600e/x, 770e, 770x */
7037 rc = fan_get_status_safe(&status);
7038 if (rc < 0)
7039 return rc;
7041 len += sprintf(p + len, "status:\t\t%s\n",
7042 (status != 0) ? "enabled" : "disabled");
7044 rc = fan_get_speed(&speed);
7045 if (rc < 0)
7046 return rc;
7048 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7050 if (status & TP_EC_FAN_FULLSPEED)
7051 /* Disengaged mode takes precedence */
7052 len += sprintf(p + len, "level:\t\tdisengaged\n");
7053 else if (status & TP_EC_FAN_AUTO)
7054 len += sprintf(p + len, "level:\t\tauto\n");
7055 else
7056 len += sprintf(p + len, "level:\t\t%d\n", status);
7057 break;
7059 case TPACPI_FAN_NONE:
7060 default:
7061 len += sprintf(p + len, "status:\t\tnot supported\n");
7064 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7065 len += sprintf(p + len, "commands:\tlevel <level>");
7067 switch (fan_control_access_mode) {
7068 case TPACPI_FAN_WR_ACPI_SFAN:
7069 len += sprintf(p + len, " (<level> is 0-7)\n");
7070 break;
7072 default:
7073 len += sprintf(p + len, " (<level> is 0-7, "
7074 "auto, disengaged, full-speed)\n");
7075 break;
7079 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7080 len += sprintf(p + len, "commands:\tenable, disable\n"
7081 "commands:\twatchdog <timeout> (<timeout> "
7082 "is 0 (off), 1-120 (seconds))\n");
7084 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7085 len += sprintf(p + len, "commands:\tspeed <speed>"
7086 " (<speed> is 0-65535)\n");
7088 return len;
7091 static int fan_write_cmd_level(const char *cmd, int *rc)
7093 int level;
7095 if (strlencmp(cmd, "level auto") == 0)
7096 level = TP_EC_FAN_AUTO;
7097 else if ((strlencmp(cmd, "level disengaged") == 0) |
7098 (strlencmp(cmd, "level full-speed") == 0))
7099 level = TP_EC_FAN_FULLSPEED;
7100 else if (sscanf(cmd, "level %d", &level) != 1)
7101 return 0;
7103 *rc = fan_set_level_safe(level);
7104 if (*rc == -ENXIO)
7105 printk(TPACPI_ERR "level command accepted for unsupported "
7106 "access mode %d", fan_control_access_mode);
7107 else if (!*rc)
7108 tpacpi_disclose_usertask("procfs fan",
7109 "set level to %d\n", level);
7111 return 1;
7114 static int fan_write_cmd_enable(const char *cmd, int *rc)
7116 if (strlencmp(cmd, "enable") != 0)
7117 return 0;
7119 *rc = fan_set_enable();
7120 if (*rc == -ENXIO)
7121 printk(TPACPI_ERR "enable command accepted for unsupported "
7122 "access mode %d", fan_control_access_mode);
7123 else if (!*rc)
7124 tpacpi_disclose_usertask("procfs fan", "enable\n");
7126 return 1;
7129 static int fan_write_cmd_disable(const char *cmd, int *rc)
7131 if (strlencmp(cmd, "disable") != 0)
7132 return 0;
7134 *rc = fan_set_disable();
7135 if (*rc == -ENXIO)
7136 printk(TPACPI_ERR "disable command accepted for unsupported "
7137 "access mode %d", fan_control_access_mode);
7138 else if (!*rc)
7139 tpacpi_disclose_usertask("procfs fan", "disable\n");
7141 return 1;
7144 static int fan_write_cmd_speed(const char *cmd, int *rc)
7146 int speed;
7148 /* TODO:
7149 * Support speed <low> <medium> <high> ? */
7151 if (sscanf(cmd, "speed %d", &speed) != 1)
7152 return 0;
7154 *rc = fan_set_speed(speed);
7155 if (*rc == -ENXIO)
7156 printk(TPACPI_ERR "speed command accepted for unsupported "
7157 "access mode %d", fan_control_access_mode);
7158 else if (!*rc)
7159 tpacpi_disclose_usertask("procfs fan",
7160 "set speed to %d\n", speed);
7162 return 1;
7165 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7167 int interval;
7169 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7170 return 0;
7172 if (interval < 0 || interval > 120)
7173 *rc = -EINVAL;
7174 else {
7175 fan_watchdog_maxinterval = interval;
7176 tpacpi_disclose_usertask("procfs fan",
7177 "set watchdog timer to %d\n",
7178 interval);
7181 return 1;
7184 static int fan_write(char *buf)
7186 char *cmd;
7187 int rc = 0;
7189 while (!rc && (cmd = next_cmd(&buf))) {
7190 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7191 fan_write_cmd_level(cmd, &rc)) &&
7192 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7193 (fan_write_cmd_enable(cmd, &rc) ||
7194 fan_write_cmd_disable(cmd, &rc) ||
7195 fan_write_cmd_watchdog(cmd, &rc))) &&
7196 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7197 fan_write_cmd_speed(cmd, &rc))
7199 rc = -EINVAL;
7200 else if (!rc)
7201 fan_watchdog_reset();
7204 return rc;
7207 static struct ibm_struct fan_driver_data = {
7208 .name = "fan",
7209 .read = fan_read,
7210 .write = fan_write,
7211 .exit = fan_exit,
7212 .suspend = fan_suspend,
7213 .resume = fan_resume,
7216 /****************************************************************************
7217 ****************************************************************************
7219 * Infrastructure
7221 ****************************************************************************
7222 ****************************************************************************/
7224 /* sysfs name ---------------------------------------------------------- */
7225 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7226 struct device_attribute *attr,
7227 char *buf)
7229 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7232 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7233 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7235 /* --------------------------------------------------------------------- */
7237 /* /proc support */
7238 static struct proc_dir_entry *proc_dir;
7241 * Module and infrastructure proble, init and exit handling
7244 static int force_load;
7246 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7247 static const char * __init str_supported(int is_supported)
7249 static char text_unsupported[] __initdata = "not supported";
7251 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7253 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7255 static void ibm_exit(struct ibm_struct *ibm)
7257 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7259 list_del_init(&ibm->all_drivers);
7261 if (ibm->flags.acpi_notify_installed) {
7262 dbg_printk(TPACPI_DBG_EXIT,
7263 "%s: acpi_remove_notify_handler\n", ibm->name);
7264 BUG_ON(!ibm->acpi);
7265 acpi_remove_notify_handler(*ibm->acpi->handle,
7266 ibm->acpi->type,
7267 dispatch_acpi_notify);
7268 ibm->flags.acpi_notify_installed = 0;
7269 ibm->flags.acpi_notify_installed = 0;
7272 if (ibm->flags.proc_created) {
7273 dbg_printk(TPACPI_DBG_EXIT,
7274 "%s: remove_proc_entry\n", ibm->name);
7275 remove_proc_entry(ibm->name, proc_dir);
7276 ibm->flags.proc_created = 0;
7279 if (ibm->flags.acpi_driver_registered) {
7280 dbg_printk(TPACPI_DBG_EXIT,
7281 "%s: acpi_bus_unregister_driver\n", ibm->name);
7282 BUG_ON(!ibm->acpi);
7283 acpi_bus_unregister_driver(ibm->acpi->driver);
7284 kfree(ibm->acpi->driver);
7285 ibm->acpi->driver = NULL;
7286 ibm->flags.acpi_driver_registered = 0;
7289 if (ibm->flags.init_called && ibm->exit) {
7290 ibm->exit();
7291 ibm->flags.init_called = 0;
7294 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7297 static int __init ibm_init(struct ibm_init_struct *iibm)
7299 int ret;
7300 struct ibm_struct *ibm = iibm->data;
7301 struct proc_dir_entry *entry;
7303 BUG_ON(ibm == NULL);
7305 INIT_LIST_HEAD(&ibm->all_drivers);
7307 if (ibm->flags.experimental && !experimental)
7308 return 0;
7310 dbg_printk(TPACPI_DBG_INIT,
7311 "probing for %s\n", ibm->name);
7313 if (iibm->init) {
7314 ret = iibm->init(iibm);
7315 if (ret > 0)
7316 return 0; /* probe failed */
7317 if (ret)
7318 return ret;
7320 ibm->flags.init_called = 1;
7323 if (ibm->acpi) {
7324 if (ibm->acpi->hid) {
7325 ret = register_tpacpi_subdriver(ibm);
7326 if (ret)
7327 goto err_out;
7330 if (ibm->acpi->notify) {
7331 ret = setup_acpi_notify(ibm);
7332 if (ret == -ENODEV) {
7333 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7334 ibm->name);
7335 ret = 0;
7336 goto err_out;
7338 if (ret < 0)
7339 goto err_out;
7343 dbg_printk(TPACPI_DBG_INIT,
7344 "%s installed\n", ibm->name);
7346 if (ibm->read) {
7347 entry = create_proc_entry(ibm->name,
7348 S_IFREG | S_IRUGO | S_IWUSR,
7349 proc_dir);
7350 if (!entry) {
7351 printk(TPACPI_ERR "unable to create proc entry %s\n",
7352 ibm->name);
7353 ret = -ENODEV;
7354 goto err_out;
7356 entry->data = ibm;
7357 entry->read_proc = &dispatch_procfs_read;
7358 if (ibm->write)
7359 entry->write_proc = &dispatch_procfs_write;
7360 ibm->flags.proc_created = 1;
7363 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7365 return 0;
7367 err_out:
7368 dbg_printk(TPACPI_DBG_INIT,
7369 "%s: at error exit path with result %d\n",
7370 ibm->name, ret);
7372 ibm_exit(ibm);
7373 return (ret < 0)? ret : 0;
7376 /* Probing */
7378 static bool __pure __init tpacpi_is_fw_digit(const char c)
7380 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7383 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7384 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7385 const char t)
7387 return s && strlen(s) >= 8 &&
7388 tpacpi_is_fw_digit(s[0]) &&
7389 tpacpi_is_fw_digit(s[1]) &&
7390 s[2] == t && s[3] == 'T' &&
7391 tpacpi_is_fw_digit(s[4]) &&
7392 tpacpi_is_fw_digit(s[5]) &&
7393 s[6] == 'W' && s[7] == 'W';
7396 /* returns 0 - probe ok, or < 0 - probe error.
7397 * Probe ok doesn't mean thinkpad found.
7398 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7399 static int __must_check __init get_thinkpad_model_data(
7400 struct thinkpad_id_data *tp)
7402 const struct dmi_device *dev = NULL;
7403 char ec_fw_string[18];
7404 char const *s;
7406 if (!tp)
7407 return -EINVAL;
7409 memset(tp, 0, sizeof(*tp));
7411 if (dmi_name_in_vendors("IBM"))
7412 tp->vendor = PCI_VENDOR_ID_IBM;
7413 else if (dmi_name_in_vendors("LENOVO"))
7414 tp->vendor = PCI_VENDOR_ID_LENOVO;
7415 else
7416 return 0;
7418 s = dmi_get_system_info(DMI_BIOS_VERSION);
7419 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7420 if (s && !tp->bios_version_str)
7421 return -ENOMEM;
7423 /* Really ancient ThinkPad 240X will fail this, which is fine */
7424 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7425 return 0;
7427 tp->bios_model = tp->bios_version_str[0]
7428 | (tp->bios_version_str[1] << 8);
7429 tp->bios_release = (tp->bios_version_str[4] << 8)
7430 | tp->bios_version_str[5];
7433 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7434 * X32 or newer, all Z series; Some models must have an
7435 * up-to-date BIOS or they will not be detected.
7437 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7439 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7440 if (sscanf(dev->name,
7441 "IBM ThinkPad Embedded Controller -[%17c",
7442 ec_fw_string) == 1) {
7443 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7444 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7446 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7447 if (!tp->ec_version_str)
7448 return -ENOMEM;
7450 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7451 tp->ec_model = ec_fw_string[0]
7452 | (ec_fw_string[1] << 8);
7453 tp->ec_release = (ec_fw_string[4] << 8)
7454 | ec_fw_string[5];
7455 } else {
7456 printk(TPACPI_NOTICE
7457 "ThinkPad firmware release %s "
7458 "doesn't match the known patterns\n",
7459 ec_fw_string);
7460 printk(TPACPI_NOTICE
7461 "please report this to %s\n",
7462 TPACPI_MAIL);
7464 break;
7468 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7469 if (s && !strnicmp(s, "ThinkPad", 8)) {
7470 tp->model_str = kstrdup(s, GFP_KERNEL);
7471 if (!tp->model_str)
7472 return -ENOMEM;
7475 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7476 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7477 if (s && !tp->nummodel_str)
7478 return -ENOMEM;
7480 return 0;
7483 static int __init probe_for_thinkpad(void)
7485 int is_thinkpad;
7487 if (acpi_disabled)
7488 return -ENODEV;
7491 * Non-ancient models have better DMI tagging, but very old models
7492 * don't.
7494 is_thinkpad = (thinkpad_id.model_str != NULL);
7496 /* ec is required because many other handles are relative to it */
7497 TPACPI_ACPIHANDLE_INIT(ec);
7498 if (!ec_handle) {
7499 if (is_thinkpad)
7500 printk(TPACPI_ERR
7501 "Not yet supported ThinkPad detected!\n");
7502 return -ENODEV;
7506 * Risks a regression on very old machines, but reduces potential
7507 * false positives a damn great deal
7509 if (!is_thinkpad)
7510 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7512 if (!is_thinkpad && !force_load)
7513 return -ENODEV;
7515 return 0;
7519 /* Module init, exit, parameters */
7521 static struct ibm_init_struct ibms_init[] __initdata = {
7523 .init = thinkpad_acpi_driver_init,
7524 .data = &thinkpad_acpi_driver_data,
7527 .init = hotkey_init,
7528 .data = &hotkey_driver_data,
7531 .init = bluetooth_init,
7532 .data = &bluetooth_driver_data,
7535 .init = wan_init,
7536 .data = &wan_driver_data,
7539 .init = uwb_init,
7540 .data = &uwb_driver_data,
7542 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7544 .init = video_init,
7545 .data = &video_driver_data,
7547 #endif
7549 .init = light_init,
7550 .data = &light_driver_data,
7553 .init = cmos_init,
7554 .data = &cmos_driver_data,
7557 .init = led_init,
7558 .data = &led_driver_data,
7561 .init = beep_init,
7562 .data = &beep_driver_data,
7565 .init = thermal_init,
7566 .data = &thermal_driver_data,
7569 .data = &ecdump_driver_data,
7572 .init = brightness_init,
7573 .data = &brightness_driver_data,
7576 .data = &volume_driver_data,
7579 .init = fan_init,
7580 .data = &fan_driver_data,
7584 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7586 unsigned int i;
7587 struct ibm_struct *ibm;
7589 if (!kp || !kp->name || !val)
7590 return -EINVAL;
7592 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7593 ibm = ibms_init[i].data;
7594 WARN_ON(ibm == NULL);
7596 if (!ibm || !ibm->name)
7597 continue;
7599 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7600 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7601 return -ENOSPC;
7602 strcpy(ibms_init[i].param, val);
7603 strcat(ibms_init[i].param, ",");
7604 return 0;
7608 return -EINVAL;
7611 module_param(experimental, int, 0);
7612 MODULE_PARM_DESC(experimental,
7613 "Enables experimental features when non-zero");
7615 module_param_named(debug, dbg_level, uint, 0);
7616 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7618 module_param(force_load, bool, 0);
7619 MODULE_PARM_DESC(force_load,
7620 "Attempts to load the driver even on a "
7621 "mis-identified ThinkPad when true");
7623 module_param_named(fan_control, fan_control_allowed, bool, 0);
7624 MODULE_PARM_DESC(fan_control,
7625 "Enables setting fan parameters features when true");
7627 module_param_named(brightness_mode, brightness_mode, uint, 0);
7628 MODULE_PARM_DESC(brightness_mode,
7629 "Selects brightness control strategy: "
7630 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7632 module_param(brightness_enable, uint, 0);
7633 MODULE_PARM_DESC(brightness_enable,
7634 "Enables backlight control when 1, disables when 0");
7636 module_param(hotkey_report_mode, uint, 0);
7637 MODULE_PARM_DESC(hotkey_report_mode,
7638 "used for backwards compatibility with userspace, "
7639 "see documentation");
7641 #define TPACPI_PARAM(feature) \
7642 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7643 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7644 "at module load, see documentation")
7646 TPACPI_PARAM(hotkey);
7647 TPACPI_PARAM(bluetooth);
7648 TPACPI_PARAM(video);
7649 TPACPI_PARAM(light);
7650 TPACPI_PARAM(cmos);
7651 TPACPI_PARAM(led);
7652 TPACPI_PARAM(beep);
7653 TPACPI_PARAM(ecdump);
7654 TPACPI_PARAM(brightness);
7655 TPACPI_PARAM(volume);
7656 TPACPI_PARAM(fan);
7658 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7659 module_param(dbg_wlswemul, uint, 0);
7660 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7661 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7662 MODULE_PARM_DESC(wlsw_state,
7663 "Initial state of the emulated WLSW switch");
7665 module_param(dbg_bluetoothemul, uint, 0);
7666 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7667 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7668 MODULE_PARM_DESC(bluetooth_state,
7669 "Initial state of the emulated bluetooth switch");
7671 module_param(dbg_wwanemul, uint, 0);
7672 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7673 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7674 MODULE_PARM_DESC(wwan_state,
7675 "Initial state of the emulated WWAN switch");
7677 module_param(dbg_uwbemul, uint, 0);
7678 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7679 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7680 MODULE_PARM_DESC(uwb_state,
7681 "Initial state of the emulated UWB switch");
7682 #endif
7684 static void thinkpad_acpi_module_exit(void)
7686 struct ibm_struct *ibm, *itmp;
7688 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7690 list_for_each_entry_safe_reverse(ibm, itmp,
7691 &tpacpi_all_drivers,
7692 all_drivers) {
7693 ibm_exit(ibm);
7696 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7698 if (tpacpi_inputdev) {
7699 if (tp_features.input_device_registered)
7700 input_unregister_device(tpacpi_inputdev);
7701 else
7702 input_free_device(tpacpi_inputdev);
7705 if (tpacpi_hwmon)
7706 hwmon_device_unregister(tpacpi_hwmon);
7708 if (tp_features.sensors_pdev_attrs_registered)
7709 device_remove_file(&tpacpi_sensors_pdev->dev,
7710 &dev_attr_thinkpad_acpi_pdev_name);
7711 if (tpacpi_sensors_pdev)
7712 platform_device_unregister(tpacpi_sensors_pdev);
7713 if (tpacpi_pdev)
7714 platform_device_unregister(tpacpi_pdev);
7716 if (tp_features.sensors_pdrv_attrs_registered)
7717 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7718 if (tp_features.platform_drv_attrs_registered)
7719 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7721 if (tp_features.sensors_pdrv_registered)
7722 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7724 if (tp_features.platform_drv_registered)
7725 platform_driver_unregister(&tpacpi_pdriver);
7727 if (proc_dir)
7728 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7730 if (tpacpi_wq)
7731 destroy_workqueue(tpacpi_wq);
7733 kfree(thinkpad_id.bios_version_str);
7734 kfree(thinkpad_id.ec_version_str);
7735 kfree(thinkpad_id.model_str);
7739 static int __init thinkpad_acpi_module_init(void)
7741 int ret, i;
7743 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7745 /* Parameter checking */
7746 if (hotkey_report_mode > 2)
7747 return -EINVAL;
7749 /* Driver-level probe */
7751 ret = get_thinkpad_model_data(&thinkpad_id);
7752 if (ret) {
7753 printk(TPACPI_ERR
7754 "unable to get DMI data: %d\n", ret);
7755 thinkpad_acpi_module_exit();
7756 return ret;
7758 ret = probe_for_thinkpad();
7759 if (ret) {
7760 thinkpad_acpi_module_exit();
7761 return ret;
7764 /* Driver initialization */
7766 TPACPI_ACPIHANDLE_INIT(ecrd);
7767 TPACPI_ACPIHANDLE_INIT(ecwr);
7769 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7770 if (!tpacpi_wq) {
7771 thinkpad_acpi_module_exit();
7772 return -ENOMEM;
7775 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7776 if (!proc_dir) {
7777 printk(TPACPI_ERR
7778 "unable to create proc dir " TPACPI_PROC_DIR);
7779 thinkpad_acpi_module_exit();
7780 return -ENODEV;
7783 ret = platform_driver_register(&tpacpi_pdriver);
7784 if (ret) {
7785 printk(TPACPI_ERR
7786 "unable to register main platform driver\n");
7787 thinkpad_acpi_module_exit();
7788 return ret;
7790 tp_features.platform_drv_registered = 1;
7792 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7793 if (ret) {
7794 printk(TPACPI_ERR
7795 "unable to register hwmon platform driver\n");
7796 thinkpad_acpi_module_exit();
7797 return ret;
7799 tp_features.sensors_pdrv_registered = 1;
7801 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7802 if (!ret) {
7803 tp_features.platform_drv_attrs_registered = 1;
7804 ret = tpacpi_create_driver_attributes(
7805 &tpacpi_hwmon_pdriver.driver);
7807 if (ret) {
7808 printk(TPACPI_ERR
7809 "unable to create sysfs driver attributes\n");
7810 thinkpad_acpi_module_exit();
7811 return ret;
7813 tp_features.sensors_pdrv_attrs_registered = 1;
7816 /* Device initialization */
7817 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7818 NULL, 0);
7819 if (IS_ERR(tpacpi_pdev)) {
7820 ret = PTR_ERR(tpacpi_pdev);
7821 tpacpi_pdev = NULL;
7822 printk(TPACPI_ERR "unable to register platform device\n");
7823 thinkpad_acpi_module_exit();
7824 return ret;
7826 tpacpi_sensors_pdev = platform_device_register_simple(
7827 TPACPI_HWMON_DRVR_NAME,
7828 -1, NULL, 0);
7829 if (IS_ERR(tpacpi_sensors_pdev)) {
7830 ret = PTR_ERR(tpacpi_sensors_pdev);
7831 tpacpi_sensors_pdev = NULL;
7832 printk(TPACPI_ERR
7833 "unable to register hwmon platform device\n");
7834 thinkpad_acpi_module_exit();
7835 return ret;
7837 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7838 &dev_attr_thinkpad_acpi_pdev_name);
7839 if (ret) {
7840 printk(TPACPI_ERR
7841 "unable to create sysfs hwmon device attributes\n");
7842 thinkpad_acpi_module_exit();
7843 return ret;
7845 tp_features.sensors_pdev_attrs_registered = 1;
7846 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7847 if (IS_ERR(tpacpi_hwmon)) {
7848 ret = PTR_ERR(tpacpi_hwmon);
7849 tpacpi_hwmon = NULL;
7850 printk(TPACPI_ERR "unable to register hwmon device\n");
7851 thinkpad_acpi_module_exit();
7852 return ret;
7854 mutex_init(&tpacpi_inputdev_send_mutex);
7855 tpacpi_inputdev = input_allocate_device();
7856 if (!tpacpi_inputdev) {
7857 printk(TPACPI_ERR "unable to allocate input device\n");
7858 thinkpad_acpi_module_exit();
7859 return -ENOMEM;
7860 } else {
7861 /* Prepare input device, but don't register */
7862 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7863 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7864 tpacpi_inputdev->id.bustype = BUS_HOST;
7865 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7866 thinkpad_id.vendor :
7867 PCI_VENDOR_ID_IBM;
7868 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7869 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7871 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7872 ret = ibm_init(&ibms_init[i]);
7873 if (ret >= 0 && *ibms_init[i].param)
7874 ret = ibms_init[i].data->write(ibms_init[i].param);
7875 if (ret < 0) {
7876 thinkpad_acpi_module_exit();
7877 return ret;
7880 ret = input_register_device(tpacpi_inputdev);
7881 if (ret < 0) {
7882 printk(TPACPI_ERR "unable to register input device\n");
7883 thinkpad_acpi_module_exit();
7884 return ret;
7885 } else {
7886 tp_features.input_device_registered = 1;
7889 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7890 return 0;
7893 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7896 * This will autoload the driver in almost every ThinkPad
7897 * in widespread use.
7899 * Only _VERY_ old models, like the 240, 240x and 570 lack
7900 * the HKEY event interface.
7902 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7905 * DMI matching for module autoloading
7907 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7908 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7910 * Only models listed in thinkwiki will be supported, so add yours
7911 * if it is not there yet.
7913 #define IBM_BIOS_MODULE_ALIAS(__type) \
7914 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7916 /* Ancient thinkpad BIOSes have to be identified by
7917 * BIOS type or model number, and there are far less
7918 * BIOS types than model numbers... */
7919 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
7921 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7922 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
7923 MODULE_DESCRIPTION(TPACPI_DESC);
7924 MODULE_VERSION(TPACPI_VERSION);
7925 MODULE_LICENSE("GPL");
7927 module_init(thinkpad_acpi_module_init);
7928 module_exit(thinkpad_acpi_module_exit);