ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
bloba21dbb1c8872bb1dc51c79bc90d2743cbdcb3d57
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 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.21-20081019"
25 #define TPACPI_SYSFS_VERSION 0x020200
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/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
79 #include <acpi/acnamesp.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* Debugging */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_ERR KERN_ERR TPACPI_LOG
179 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
180 #define TPACPI_INFO KERN_INFO TPACPI_LOG
181 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
183 #define TPACPI_DBG_ALL 0xffff
184 #define TPACPI_DBG_INIT 0x0001
185 #define TPACPI_DBG_EXIT 0x0002
186 #define dbg_printk(a_dbg_level, format, arg...) \
187 do { if (dbg_level & a_dbg_level) \
188 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
189 } while (0)
190 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
191 #define vdbg_printk(a_dbg_level, format, arg...) \
192 dbg_printk(a_dbg_level, format, ## arg)
193 static const char *str_supported(int is_supported);
194 #else
195 #define vdbg_printk(a_dbg_level, format, arg...)
196 #endif
198 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
199 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
200 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
203 /****************************************************************************
204 * Driver-wide structs and misc. variables
207 struct ibm_struct;
209 struct tp_acpi_drv_struct {
210 const struct acpi_device_id *hid;
211 struct acpi_driver *driver;
213 void (*notify) (struct ibm_struct *, u32);
214 acpi_handle *handle;
215 u32 type;
216 struct acpi_device *device;
219 struct ibm_struct {
220 char *name;
222 int (*read) (char *);
223 int (*write) (char *);
224 void (*exit) (void);
225 void (*resume) (void);
226 void (*suspend) (pm_message_t state);
227 void (*shutdown) (void);
229 struct list_head all_drivers;
231 struct tp_acpi_drv_struct *acpi;
233 struct {
234 u8 acpi_driver_registered:1;
235 u8 acpi_notify_installed:1;
236 u8 proc_created:1;
237 u8 init_called:1;
238 u8 experimental:1;
239 } flags;
242 struct ibm_init_struct {
243 char param[32];
245 int (*init) (struct ibm_init_struct *);
246 struct ibm_struct *data;
249 static struct {
250 #ifdef CONFIG_THINKPAD_ACPI_BAY
251 u32 bay_status:1;
252 u32 bay_eject:1;
253 u32 bay_status2:1;
254 u32 bay_eject2:1;
255 #endif
256 u32 bluetooth:1;
257 u32 hotkey:1;
258 u32 hotkey_mask:1;
259 u32 hotkey_wlsw:1;
260 u32 hotkey_tablet:1;
261 u32 light:1;
262 u32 light_status:1;
263 u32 bright_16levels:1;
264 u32 bright_acpimode:1;
265 u32 wan:1;
266 u32 uwb:1;
267 u32 fan_ctrl_status_undef:1;
268 u32 input_device_registered:1;
269 u32 platform_drv_registered:1;
270 u32 platform_drv_attrs_registered:1;
271 u32 sensors_pdrv_registered:1;
272 u32 sensors_pdrv_attrs_registered:1;
273 u32 sensors_pdev_attrs_registered:1;
274 u32 hotkey_poll_active:1;
275 } tp_features;
277 static struct {
278 u16 hotkey_mask_ff:1;
279 u16 bright_cmos_ec_unsync:1;
280 } tp_warned;
282 struct thinkpad_id_data {
283 unsigned int vendor; /* ThinkPad vendor:
284 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
286 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
287 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
289 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
290 u16 ec_model;
292 char *model_str; /* ThinkPad T43 */
293 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
295 static struct thinkpad_id_data thinkpad_id;
297 static enum {
298 TPACPI_LIFE_INIT = 0,
299 TPACPI_LIFE_RUNNING,
300 TPACPI_LIFE_EXITING,
301 } tpacpi_lifecycle;
303 static int experimental;
304 static u32 dbg_level;
306 static struct workqueue_struct *tpacpi_wq;
308 /* Special LED class that can defer work */
309 struct tpacpi_led_classdev {
310 struct led_classdev led_classdev;
311 struct work_struct work;
312 enum led_brightness new_brightness;
313 unsigned int led;
316 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
317 static int dbg_wlswemul;
318 static int tpacpi_wlsw_emulstate;
319 static int dbg_bluetoothemul;
320 static int tpacpi_bluetooth_emulstate;
321 static int dbg_wwanemul;
322 static int tpacpi_wwan_emulstate;
323 static int dbg_uwbemul;
324 static int tpacpi_uwb_emulstate;
325 #endif
328 /****************************************************************************
329 ****************************************************************************
331 * ACPI Helpers and device model
333 ****************************************************************************
334 ****************************************************************************/
336 /*************************************************************************
337 * ACPI basic handles
340 static acpi_handle root_handle;
342 #define TPACPI_HANDLE(object, parent, paths...) \
343 static acpi_handle object##_handle; \
344 static acpi_handle *object##_parent = &parent##_handle; \
345 static char *object##_path; \
346 static char *object##_paths[] = { paths }
348 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
349 "\\_SB.PCI.ISA.EC", /* 570 */
350 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
351 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
352 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
353 "\\_SB.PCI0.ICH3.EC0", /* R31 */
354 "\\_SB.PCI0.LPC.EC", /* all others */
357 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
358 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
360 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
361 /* T4x, X31, X40 */
362 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
363 "\\CMS", /* R40, R40e */
364 ); /* all others */
366 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
367 "^HKEY", /* R30, R31 */
368 "HKEY", /* all others */
369 ); /* 570 */
371 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
372 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
373 "\\_SB.PCI0.VID0", /* 770e */
374 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
375 "\\_SB.PCI0.AGP.VID", /* all others */
376 ); /* R30, R31 */
379 /*************************************************************************
380 * ACPI helpers
383 static int acpi_evalf(acpi_handle handle,
384 void *res, char *method, char *fmt, ...)
386 char *fmt0 = fmt;
387 struct acpi_object_list params;
388 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
389 struct acpi_buffer result, *resultp;
390 union acpi_object out_obj;
391 acpi_status status;
392 va_list ap;
393 char res_type;
394 int success;
395 int quiet;
397 if (!*fmt) {
398 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
399 return 0;
402 if (*fmt == 'q') {
403 quiet = 1;
404 fmt++;
405 } else
406 quiet = 0;
408 res_type = *(fmt++);
410 params.count = 0;
411 params.pointer = &in_objs[0];
413 va_start(ap, fmt);
414 while (*fmt) {
415 char c = *(fmt++);
416 switch (c) {
417 case 'd': /* int */
418 in_objs[params.count].integer.value = va_arg(ap, int);
419 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
420 break;
421 /* add more types as needed */
422 default:
423 printk(TPACPI_ERR "acpi_evalf() called "
424 "with invalid format character '%c'\n", c);
425 return 0;
428 va_end(ap);
430 if (res_type != 'v') {
431 result.length = sizeof(out_obj);
432 result.pointer = &out_obj;
433 resultp = &result;
434 } else
435 resultp = NULL;
437 status = acpi_evaluate_object(handle, method, &params, resultp);
439 switch (res_type) {
440 case 'd': /* int */
441 if (res)
442 *(int *)res = out_obj.integer.value;
443 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
444 break;
445 case 'v': /* void */
446 success = status == AE_OK;
447 break;
448 /* add more types as needed */
449 default:
450 printk(TPACPI_ERR "acpi_evalf() called "
451 "with invalid format character '%c'\n", res_type);
452 return 0;
455 if (!success && !quiet)
456 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
457 method, fmt0, status);
459 return success;
462 static int acpi_ec_read(int i, u8 *p)
464 int v;
466 if (ecrd_handle) {
467 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
468 return 0;
469 *p = v;
470 } else {
471 if (ec_read(i, p) < 0)
472 return 0;
475 return 1;
478 static int acpi_ec_write(int i, u8 v)
480 if (ecwr_handle) {
481 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
482 return 0;
483 } else {
484 if (ec_write(i, v) < 0)
485 return 0;
488 return 1;
491 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
492 static int _sta(acpi_handle handle)
494 int status;
496 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
497 status = 0;
499 return status;
501 #endif
503 static int issue_thinkpad_cmos_command(int cmos_cmd)
505 if (!cmos_handle)
506 return -ENXIO;
508 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
509 return -EIO;
511 return 0;
514 /*************************************************************************
515 * ACPI device model
518 #define TPACPI_ACPIHANDLE_INIT(object) \
519 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
520 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
522 static void drv_acpi_handle_init(char *name,
523 acpi_handle *handle, acpi_handle parent,
524 char **paths, int num_paths, char **path)
526 int i;
527 acpi_status status;
529 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
530 name);
532 for (i = 0; i < num_paths; i++) {
533 status = acpi_get_handle(parent, paths[i], handle);
534 if (ACPI_SUCCESS(status)) {
535 *path = paths[i];
536 dbg_printk(TPACPI_DBG_INIT,
537 "Found ACPI handle %s for %s\n",
538 *path, name);
539 return;
543 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
544 name);
545 *handle = NULL;
548 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
550 struct ibm_struct *ibm = data;
552 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
553 return;
555 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
556 return;
558 ibm->acpi->notify(ibm, event);
561 static int __init setup_acpi_notify(struct ibm_struct *ibm)
563 acpi_status status;
564 int rc;
566 BUG_ON(!ibm->acpi);
568 if (!*ibm->acpi->handle)
569 return 0;
571 vdbg_printk(TPACPI_DBG_INIT,
572 "setting up ACPI notify for %s\n", ibm->name);
574 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
575 if (rc < 0) {
576 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
577 ibm->name, rc);
578 return -ENODEV;
581 acpi_driver_data(ibm->acpi->device) = ibm;
582 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
583 TPACPI_ACPI_EVENT_PREFIX,
584 ibm->name);
586 status = acpi_install_notify_handler(*ibm->acpi->handle,
587 ibm->acpi->type, dispatch_acpi_notify, ibm);
588 if (ACPI_FAILURE(status)) {
589 if (status == AE_ALREADY_EXISTS) {
590 printk(TPACPI_NOTICE
591 "another device driver is already "
592 "handling %s events\n", ibm->name);
593 } else {
594 printk(TPACPI_ERR
595 "acpi_install_notify_handler(%s) failed: %d\n",
596 ibm->name, status);
598 return -ENODEV;
600 ibm->flags.acpi_notify_installed = 1;
601 return 0;
604 static int __init tpacpi_device_add(struct acpi_device *device)
606 return 0;
609 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
611 int rc;
613 dbg_printk(TPACPI_DBG_INIT,
614 "registering %s as an ACPI driver\n", ibm->name);
616 BUG_ON(!ibm->acpi);
618 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
619 if (!ibm->acpi->driver) {
620 printk(TPACPI_ERR
621 "failed to allocate memory for ibm->acpi->driver\n");
622 return -ENOMEM;
625 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
626 ibm->acpi->driver->ids = ibm->acpi->hid;
628 ibm->acpi->driver->ops.add = &tpacpi_device_add;
630 rc = acpi_bus_register_driver(ibm->acpi->driver);
631 if (rc < 0) {
632 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
633 ibm->name, rc);
634 kfree(ibm->acpi->driver);
635 ibm->acpi->driver = NULL;
636 } else if (!rc)
637 ibm->flags.acpi_driver_registered = 1;
639 return rc;
643 /****************************************************************************
644 ****************************************************************************
646 * Procfs Helpers
648 ****************************************************************************
649 ****************************************************************************/
651 static int dispatch_procfs_read(char *page, char **start, off_t off,
652 int count, int *eof, void *data)
654 struct ibm_struct *ibm = data;
655 int len;
657 if (!ibm || !ibm->read)
658 return -EINVAL;
660 len = ibm->read(page);
661 if (len < 0)
662 return len;
664 if (len <= off + count)
665 *eof = 1;
666 *start = page + off;
667 len -= off;
668 if (len > count)
669 len = count;
670 if (len < 0)
671 len = 0;
673 return len;
676 static int dispatch_procfs_write(struct file *file,
677 const char __user *userbuf,
678 unsigned long count, void *data)
680 struct ibm_struct *ibm = data;
681 char *kernbuf;
682 int ret;
684 if (!ibm || !ibm->write)
685 return -EINVAL;
687 kernbuf = kmalloc(count + 2, GFP_KERNEL);
688 if (!kernbuf)
689 return -ENOMEM;
691 if (copy_from_user(kernbuf, userbuf, count)) {
692 kfree(kernbuf);
693 return -EFAULT;
696 kernbuf[count] = 0;
697 strcat(kernbuf, ",");
698 ret = ibm->write(kernbuf);
699 if (ret == 0)
700 ret = count;
702 kfree(kernbuf);
704 return ret;
707 static char *next_cmd(char **cmds)
709 char *start = *cmds;
710 char *end;
712 while ((end = strchr(start, ',')) && end == start)
713 start = end + 1;
715 if (!end)
716 return NULL;
718 *end = 0;
719 *cmds = end + 1;
720 return start;
724 /****************************************************************************
725 ****************************************************************************
727 * Device model: input, hwmon and platform
729 ****************************************************************************
730 ****************************************************************************/
732 static struct platform_device *tpacpi_pdev;
733 static struct platform_device *tpacpi_sensors_pdev;
734 static struct device *tpacpi_hwmon;
735 static struct input_dev *tpacpi_inputdev;
736 static struct mutex tpacpi_inputdev_send_mutex;
737 static LIST_HEAD(tpacpi_all_drivers);
739 static int tpacpi_suspend_handler(struct platform_device *pdev,
740 pm_message_t state)
742 struct ibm_struct *ibm, *itmp;
744 list_for_each_entry_safe(ibm, itmp,
745 &tpacpi_all_drivers,
746 all_drivers) {
747 if (ibm->suspend)
748 (ibm->suspend)(state);
751 return 0;
754 static int tpacpi_resume_handler(struct platform_device *pdev)
756 struct ibm_struct *ibm, *itmp;
758 list_for_each_entry_safe(ibm, itmp,
759 &tpacpi_all_drivers,
760 all_drivers) {
761 if (ibm->resume)
762 (ibm->resume)();
765 return 0;
768 static void tpacpi_shutdown_handler(struct platform_device *pdev)
770 struct ibm_struct *ibm, *itmp;
772 list_for_each_entry_safe(ibm, itmp,
773 &tpacpi_all_drivers,
774 all_drivers) {
775 if (ibm->shutdown)
776 (ibm->shutdown)();
780 static struct platform_driver tpacpi_pdriver = {
781 .driver = {
782 .name = TPACPI_DRVR_NAME,
783 .owner = THIS_MODULE,
785 .suspend = tpacpi_suspend_handler,
786 .resume = tpacpi_resume_handler,
787 .shutdown = tpacpi_shutdown_handler,
790 static struct platform_driver tpacpi_hwmon_pdriver = {
791 .driver = {
792 .name = TPACPI_HWMON_DRVR_NAME,
793 .owner = THIS_MODULE,
797 /*************************************************************************
798 * sysfs support helpers
801 struct attribute_set {
802 unsigned int members, max_members;
803 struct attribute_group group;
806 struct attribute_set_obj {
807 struct attribute_set s;
808 struct attribute *a;
809 } __attribute__((packed));
811 static struct attribute_set *create_attr_set(unsigned int max_members,
812 const char *name)
814 struct attribute_set_obj *sobj;
816 if (max_members == 0)
817 return NULL;
819 /* Allocates space for implicit NULL at the end too */
820 sobj = kzalloc(sizeof(struct attribute_set_obj) +
821 max_members * sizeof(struct attribute *),
822 GFP_KERNEL);
823 if (!sobj)
824 return NULL;
825 sobj->s.max_members = max_members;
826 sobj->s.group.attrs = &sobj->a;
827 sobj->s.group.name = name;
829 return &sobj->s;
832 #define destroy_attr_set(_set) \
833 kfree(_set);
835 /* not multi-threaded safe, use it in a single thread per set */
836 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
838 if (!s || !attr)
839 return -EINVAL;
841 if (s->members >= s->max_members)
842 return -ENOMEM;
844 s->group.attrs[s->members] = attr;
845 s->members++;
847 return 0;
850 static int add_many_to_attr_set(struct attribute_set *s,
851 struct attribute **attr,
852 unsigned int count)
854 int i, res;
856 for (i = 0; i < count; i++) {
857 res = add_to_attr_set(s, attr[i]);
858 if (res)
859 return res;
862 return 0;
865 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
867 sysfs_remove_group(kobj, &s->group);
868 destroy_attr_set(s);
871 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
872 sysfs_create_group(_kobj, &_attr_set->group)
874 static int parse_strtoul(const char *buf,
875 unsigned long max, unsigned long *value)
877 char *endp;
879 while (*buf && isspace(*buf))
880 buf++;
881 *value = simple_strtoul(buf, &endp, 0);
882 while (*endp && isspace(*endp))
883 endp++;
884 if (*endp || *value > max)
885 return -EINVAL;
887 return 0;
890 static void tpacpi_disable_brightness_delay(void)
892 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
893 printk(TPACPI_NOTICE
894 "ACPI backlight control delay disabled\n");
897 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
899 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
900 union acpi_object *obj;
901 int rc;
903 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
904 obj = (union acpi_object *)buffer.pointer;
905 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
906 printk(TPACPI_ERR "Unknown _BCL data, "
907 "please report this to %s\n", TPACPI_MAIL);
908 rc = 0;
909 } else {
910 rc = obj->package.count;
912 } else {
913 return 0;
916 kfree(buffer.pointer);
917 return rc;
920 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
921 u32 lvl, void *context, void **rv)
923 char name[ACPI_PATH_SEGMENT_LENGTH];
924 struct acpi_buffer buffer = { sizeof(name), &name };
926 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
927 !strncmp("_BCL", name, sizeof(name) - 1)) {
928 BUG_ON(!rv || !*rv);
929 **(int **)rv = tpacpi_query_bcl_levels(handle);
930 return AE_CTRL_TERMINATE;
931 } else {
932 return AE_OK;
937 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
939 static int __init tpacpi_check_std_acpi_brightness_support(void)
941 int status;
942 int bcl_levels = 0;
943 void *bcl_ptr = &bcl_levels;
945 if (!vid_handle) {
946 TPACPI_ACPIHANDLE_INIT(vid);
948 if (!vid_handle)
949 return 0;
952 * Search for a _BCL method, and execute it. This is safe on all
953 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
954 * BIOS in ACPI backlight control mode. We do NOT have to care
955 * about calling the _BCL method in an enabled video device, any
956 * will do for our purposes.
959 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
960 tpacpi_acpi_walk_find_bcl, NULL,
961 &bcl_ptr);
963 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
964 tp_features.bright_acpimode = 1;
965 return (bcl_levels - 2);
968 return 0;
971 static int __init tpacpi_new_rfkill(const unsigned int id,
972 struct rfkill **rfk,
973 const enum rfkill_type rfktype,
974 const char *name,
975 const bool set_default,
976 int (*toggle_radio)(void *, enum rfkill_state),
977 int (*get_state)(void *, enum rfkill_state *))
979 int res;
980 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
982 res = get_state(NULL, &initial_state);
983 if (res < 0) {
984 printk(TPACPI_ERR
985 "failed to read initial state for %s, error %d; "
986 "will turn radio off\n", name, res);
987 } else if (set_default) {
988 /* try to set the initial state as the default for the rfkill
989 * type, since we ask the firmware to preserve it across S5 in
990 * NVRAM */
991 rfkill_set_default(rfktype,
992 (initial_state == RFKILL_STATE_UNBLOCKED) ?
993 RFKILL_STATE_UNBLOCKED :
994 RFKILL_STATE_SOFT_BLOCKED);
997 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
998 if (!*rfk) {
999 printk(TPACPI_ERR
1000 "failed to allocate memory for rfkill class\n");
1001 return -ENOMEM;
1004 (*rfk)->name = name;
1005 (*rfk)->get_state = get_state;
1006 (*rfk)->toggle_radio = toggle_radio;
1007 (*rfk)->state = initial_state;
1009 res = rfkill_register(*rfk);
1010 if (res < 0) {
1011 printk(TPACPI_ERR
1012 "failed to register %s rfkill switch: %d\n",
1013 name, res);
1014 rfkill_free(*rfk);
1015 *rfk = NULL;
1016 return res;
1019 return 0;
1022 /*************************************************************************
1023 * thinkpad-acpi driver attributes
1026 /* interface_version --------------------------------------------------- */
1027 static ssize_t tpacpi_driver_interface_version_show(
1028 struct device_driver *drv,
1029 char *buf)
1031 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1034 static DRIVER_ATTR(interface_version, S_IRUGO,
1035 tpacpi_driver_interface_version_show, NULL);
1037 /* debug_level --------------------------------------------------------- */
1038 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1039 char *buf)
1041 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1044 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1045 const char *buf, size_t count)
1047 unsigned long t;
1049 if (parse_strtoul(buf, 0xffff, &t))
1050 return -EINVAL;
1052 dbg_level = t;
1054 return count;
1057 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1058 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1060 /* version ------------------------------------------------------------- */
1061 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1062 char *buf)
1064 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1065 TPACPI_DESC, TPACPI_VERSION);
1068 static DRIVER_ATTR(version, S_IRUGO,
1069 tpacpi_driver_version_show, NULL);
1071 /* --------------------------------------------------------------------- */
1073 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1075 static void tpacpi_send_radiosw_update(void);
1077 /* wlsw_emulstate ------------------------------------------------------ */
1078 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1079 char *buf)
1081 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1084 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1085 const char *buf, size_t count)
1087 unsigned long t;
1089 if (parse_strtoul(buf, 1, &t))
1090 return -EINVAL;
1092 if (tpacpi_wlsw_emulstate != t) {
1093 tpacpi_wlsw_emulstate = !!t;
1094 tpacpi_send_radiosw_update();
1095 } else
1096 tpacpi_wlsw_emulstate = !!t;
1098 return count;
1101 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1102 tpacpi_driver_wlsw_emulstate_show,
1103 tpacpi_driver_wlsw_emulstate_store);
1105 /* bluetooth_emulstate ------------------------------------------------- */
1106 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1107 struct device_driver *drv,
1108 char *buf)
1110 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1113 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1114 struct device_driver *drv,
1115 const char *buf, size_t count)
1117 unsigned long t;
1119 if (parse_strtoul(buf, 1, &t))
1120 return -EINVAL;
1122 tpacpi_bluetooth_emulstate = !!t;
1124 return count;
1127 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1128 tpacpi_driver_bluetooth_emulstate_show,
1129 tpacpi_driver_bluetooth_emulstate_store);
1131 /* wwan_emulstate ------------------------------------------------- */
1132 static ssize_t tpacpi_driver_wwan_emulstate_show(
1133 struct device_driver *drv,
1134 char *buf)
1136 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1139 static ssize_t tpacpi_driver_wwan_emulstate_store(
1140 struct device_driver *drv,
1141 const char *buf, size_t count)
1143 unsigned long t;
1145 if (parse_strtoul(buf, 1, &t))
1146 return -EINVAL;
1148 tpacpi_wwan_emulstate = !!t;
1150 return count;
1153 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1154 tpacpi_driver_wwan_emulstate_show,
1155 tpacpi_driver_wwan_emulstate_store);
1157 /* uwb_emulstate ------------------------------------------------- */
1158 static ssize_t tpacpi_driver_uwb_emulstate_show(
1159 struct device_driver *drv,
1160 char *buf)
1162 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1165 static ssize_t tpacpi_driver_uwb_emulstate_store(
1166 struct device_driver *drv,
1167 const char *buf, size_t count)
1169 unsigned long t;
1171 if (parse_strtoul(buf, 1, &t))
1172 return -EINVAL;
1174 tpacpi_uwb_emulstate = !!t;
1176 return count;
1179 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1180 tpacpi_driver_uwb_emulstate_show,
1181 tpacpi_driver_uwb_emulstate_store);
1182 #endif
1184 /* --------------------------------------------------------------------- */
1186 static struct driver_attribute *tpacpi_driver_attributes[] = {
1187 &driver_attr_debug_level, &driver_attr_version,
1188 &driver_attr_interface_version,
1191 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1193 int i, res;
1195 i = 0;
1196 res = 0;
1197 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1198 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1199 i++;
1202 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1203 if (!res && dbg_wlswemul)
1204 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1205 if (!res && dbg_bluetoothemul)
1206 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1207 if (!res && dbg_wwanemul)
1208 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1209 if (!res && dbg_uwbemul)
1210 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1211 #endif
1213 return res;
1216 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1218 int i;
1220 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1221 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1223 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1224 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1225 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1226 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1227 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1228 #endif
1231 /****************************************************************************
1232 ****************************************************************************
1234 * Subdrivers
1236 ****************************************************************************
1237 ****************************************************************************/
1239 /*************************************************************************
1240 * thinkpad-acpi init subdriver
1243 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1245 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1246 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1248 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1249 (thinkpad_id.bios_version_str) ?
1250 thinkpad_id.bios_version_str : "unknown",
1251 (thinkpad_id.ec_version_str) ?
1252 thinkpad_id.ec_version_str : "unknown");
1254 if (thinkpad_id.vendor && thinkpad_id.model_str)
1255 printk(TPACPI_INFO "%s %s, model %s\n",
1256 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1257 "IBM" : ((thinkpad_id.vendor ==
1258 PCI_VENDOR_ID_LENOVO) ?
1259 "Lenovo" : "Unknown vendor"),
1260 thinkpad_id.model_str,
1261 (thinkpad_id.nummodel_str) ?
1262 thinkpad_id.nummodel_str : "unknown");
1264 return 0;
1267 static int thinkpad_acpi_driver_read(char *p)
1269 int len = 0;
1271 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1272 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1274 return len;
1277 static struct ibm_struct thinkpad_acpi_driver_data = {
1278 .name = "driver",
1279 .read = thinkpad_acpi_driver_read,
1282 /*************************************************************************
1283 * Hotkey subdriver
1286 enum { /* hot key scan codes (derived from ACPI DSDT) */
1287 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1288 TP_ACPI_HOTKEYSCAN_FNF2,
1289 TP_ACPI_HOTKEYSCAN_FNF3,
1290 TP_ACPI_HOTKEYSCAN_FNF4,
1291 TP_ACPI_HOTKEYSCAN_FNF5,
1292 TP_ACPI_HOTKEYSCAN_FNF6,
1293 TP_ACPI_HOTKEYSCAN_FNF7,
1294 TP_ACPI_HOTKEYSCAN_FNF8,
1295 TP_ACPI_HOTKEYSCAN_FNF9,
1296 TP_ACPI_HOTKEYSCAN_FNF10,
1297 TP_ACPI_HOTKEYSCAN_FNF11,
1298 TP_ACPI_HOTKEYSCAN_FNF12,
1299 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1300 TP_ACPI_HOTKEYSCAN_FNINSERT,
1301 TP_ACPI_HOTKEYSCAN_FNDELETE,
1302 TP_ACPI_HOTKEYSCAN_FNHOME,
1303 TP_ACPI_HOTKEYSCAN_FNEND,
1304 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1305 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1306 TP_ACPI_HOTKEYSCAN_FNSPACE,
1307 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1308 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1309 TP_ACPI_HOTKEYSCAN_MUTE,
1310 TP_ACPI_HOTKEYSCAN_THINKPAD,
1313 enum { /* Keys available through NVRAM polling */
1314 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1315 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1318 enum { /* Positions of some of the keys in hotkey masks */
1319 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1320 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1321 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1322 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1323 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1324 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1325 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1326 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1327 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1328 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1329 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1332 enum { /* NVRAM to ACPI HKEY group map */
1333 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1334 TP_ACPI_HKEY_ZOOM_MASK |
1335 TP_ACPI_HKEY_DISPSWTCH_MASK |
1336 TP_ACPI_HKEY_HIBERNATE_MASK,
1337 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1338 TP_ACPI_HKEY_BRGHTDWN_MASK,
1339 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1340 TP_ACPI_HKEY_VOLDWN_MASK |
1341 TP_ACPI_HKEY_MUTE_MASK,
1344 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1345 struct tp_nvram_state {
1346 u16 thinkpad_toggle:1;
1347 u16 zoom_toggle:1;
1348 u16 display_toggle:1;
1349 u16 thinklight_toggle:1;
1350 u16 hibernate_toggle:1;
1351 u16 displayexp_toggle:1;
1352 u16 display_state:1;
1353 u16 brightness_toggle:1;
1354 u16 volume_toggle:1;
1355 u16 mute:1;
1357 u8 brightness_level;
1358 u8 volume_level;
1361 static struct task_struct *tpacpi_hotkey_task;
1362 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1363 static int hotkey_poll_freq = 10; /* Hz */
1364 static struct mutex hotkey_thread_mutex;
1365 static struct mutex hotkey_thread_data_mutex;
1366 static unsigned int hotkey_config_change;
1368 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1370 #define hotkey_source_mask 0U
1372 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1374 static struct mutex hotkey_mutex;
1376 static enum { /* Reasons for waking up */
1377 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1378 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1379 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1380 } hotkey_wakeup_reason;
1382 static int hotkey_autosleep_ack;
1384 static int hotkey_orig_status;
1385 static u32 hotkey_orig_mask;
1386 static u32 hotkey_all_mask;
1387 static u32 hotkey_reserved_mask;
1388 static u32 hotkey_mask;
1390 static unsigned int hotkey_report_mode;
1392 static u16 *hotkey_keycode_map;
1394 static struct attribute_set *hotkey_dev_attributes;
1396 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1397 #define HOTKEY_CONFIG_CRITICAL_START \
1398 do { \
1399 mutex_lock(&hotkey_thread_data_mutex); \
1400 hotkey_config_change++; \
1401 } while (0);
1402 #define HOTKEY_CONFIG_CRITICAL_END \
1403 mutex_unlock(&hotkey_thread_data_mutex);
1404 #else
1405 #define HOTKEY_CONFIG_CRITICAL_START
1406 #define HOTKEY_CONFIG_CRITICAL_END
1407 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1409 /* HKEY.MHKG() return bits */
1410 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1412 static int hotkey_get_wlsw(int *status)
1414 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1415 if (dbg_wlswemul) {
1416 *status = !!tpacpi_wlsw_emulstate;
1417 return 0;
1419 #endif
1420 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1421 return -EIO;
1422 return 0;
1425 static int hotkey_get_tablet_mode(int *status)
1427 int s;
1429 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1430 return -EIO;
1432 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1433 return 0;
1437 * Call with hotkey_mutex held
1439 static int hotkey_mask_get(void)
1441 u32 m = 0;
1443 if (tp_features.hotkey_mask) {
1444 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1445 return -EIO;
1447 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1449 return 0;
1453 * Call with hotkey_mutex held
1455 static int hotkey_mask_set(u32 mask)
1457 int i;
1458 int rc = 0;
1460 if (tp_features.hotkey_mask) {
1461 if (!tp_warned.hotkey_mask_ff &&
1462 (mask == 0xffff || mask == 0xffffff ||
1463 mask == 0xffffffff)) {
1464 tp_warned.hotkey_mask_ff = 1;
1465 printk(TPACPI_NOTICE
1466 "setting the hotkey mask to 0x%08x is likely "
1467 "not the best way to go about it\n", mask);
1468 printk(TPACPI_NOTICE
1469 "please consider using the driver defaults, "
1470 "and refer to up-to-date thinkpad-acpi "
1471 "documentation\n");
1474 HOTKEY_CONFIG_CRITICAL_START
1475 for (i = 0; i < 32; i++) {
1476 u32 m = 1 << i;
1477 /* enable in firmware mask only keys not in NVRAM
1478 * mode, but enable the key in the cached hotkey_mask
1479 * regardless of mode, or the key will end up
1480 * disabled by hotkey_mask_get() */
1481 if (!acpi_evalf(hkey_handle,
1482 NULL, "MHKM", "vdd", i + 1,
1483 !!((mask & ~hotkey_source_mask) & m))) {
1484 rc = -EIO;
1485 break;
1486 } else {
1487 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1490 HOTKEY_CONFIG_CRITICAL_END
1492 /* hotkey_mask_get must be called unconditionally below */
1493 if (!hotkey_mask_get() && !rc &&
1494 (hotkey_mask & ~hotkey_source_mask) !=
1495 (mask & ~hotkey_source_mask)) {
1496 printk(TPACPI_NOTICE
1497 "requested hot key mask 0x%08x, but "
1498 "firmware forced it to 0x%08x\n",
1499 mask, hotkey_mask);
1501 } else {
1502 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1503 HOTKEY_CONFIG_CRITICAL_START
1504 hotkey_mask = mask & hotkey_source_mask;
1505 HOTKEY_CONFIG_CRITICAL_END
1506 hotkey_mask_get();
1507 if (hotkey_mask != mask) {
1508 printk(TPACPI_NOTICE
1509 "requested hot key mask 0x%08x, "
1510 "forced to 0x%08x (NVRAM poll mask is "
1511 "0x%08x): no firmware mask support\n",
1512 mask, hotkey_mask, hotkey_source_mask);
1514 #else
1515 hotkey_mask_get();
1516 rc = -ENXIO;
1517 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1520 return rc;
1523 static int hotkey_status_get(int *status)
1525 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1526 return -EIO;
1528 return 0;
1531 static int hotkey_status_set(int status)
1533 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1534 return -EIO;
1536 return 0;
1539 static void tpacpi_input_send_tabletsw(void)
1541 int state;
1543 if (tp_features.hotkey_tablet &&
1544 !hotkey_get_tablet_mode(&state)) {
1545 mutex_lock(&tpacpi_inputdev_send_mutex);
1547 input_report_switch(tpacpi_inputdev,
1548 SW_TABLET_MODE, !!state);
1549 input_sync(tpacpi_inputdev);
1551 mutex_unlock(&tpacpi_inputdev_send_mutex);
1555 static void tpacpi_input_send_key(unsigned int scancode)
1557 unsigned int keycode;
1559 keycode = hotkey_keycode_map[scancode];
1561 if (keycode != KEY_RESERVED) {
1562 mutex_lock(&tpacpi_inputdev_send_mutex);
1564 input_report_key(tpacpi_inputdev, keycode, 1);
1565 if (keycode == KEY_UNKNOWN)
1566 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1567 scancode);
1568 input_sync(tpacpi_inputdev);
1570 input_report_key(tpacpi_inputdev, keycode, 0);
1571 if (keycode == KEY_UNKNOWN)
1572 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1573 scancode);
1574 input_sync(tpacpi_inputdev);
1576 mutex_unlock(&tpacpi_inputdev_send_mutex);
1580 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1581 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1583 static void tpacpi_hotkey_send_key(unsigned int scancode)
1585 tpacpi_input_send_key(scancode);
1586 if (hotkey_report_mode < 2) {
1587 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1588 0x80, 0x1001 + scancode);
1592 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1594 u8 d;
1596 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1597 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1598 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1599 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1600 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1601 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1603 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1604 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1605 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1607 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1608 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1609 n->displayexp_toggle =
1610 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1612 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1613 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1614 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1615 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1616 n->brightness_toggle =
1617 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1619 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1620 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1621 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1622 >> TP_NVRAM_POS_LEVEL_VOLUME;
1623 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1624 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1628 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1629 do { \
1630 if ((mask & (1 << __scancode)) && \
1631 oldn->__member != newn->__member) \
1632 tpacpi_hotkey_send_key(__scancode); \
1633 } while (0)
1635 #define TPACPI_MAY_SEND_KEY(__scancode) \
1636 do { if (mask & (1 << __scancode)) \
1637 tpacpi_hotkey_send_key(__scancode); } while (0)
1639 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1640 struct tp_nvram_state *newn,
1641 u32 mask)
1643 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1644 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1645 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1646 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1648 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1650 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1652 /* handle volume */
1653 if (oldn->volume_toggle != newn->volume_toggle) {
1654 if (oldn->mute != newn->mute) {
1655 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1657 if (oldn->volume_level > newn->volume_level) {
1658 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1659 } else if (oldn->volume_level < newn->volume_level) {
1660 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1661 } else if (oldn->mute == newn->mute) {
1662 /* repeated key presses that didn't change state */
1663 if (newn->mute) {
1664 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1665 } else if (newn->volume_level != 0) {
1666 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1667 } else {
1668 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1673 /* handle brightness */
1674 if (oldn->brightness_toggle != newn->brightness_toggle) {
1675 if (oldn->brightness_level < newn->brightness_level) {
1676 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1677 } else if (oldn->brightness_level > newn->brightness_level) {
1678 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1679 } else {
1680 /* repeated key presses that didn't change state */
1681 if (newn->brightness_level != 0) {
1682 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1683 } else {
1684 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1690 #undef TPACPI_COMPARE_KEY
1691 #undef TPACPI_MAY_SEND_KEY
1693 static int hotkey_kthread(void *data)
1695 struct tp_nvram_state s[2];
1696 u32 mask;
1697 unsigned int si, so;
1698 unsigned long t;
1699 unsigned int change_detector, must_reset;
1701 mutex_lock(&hotkey_thread_mutex);
1703 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1704 goto exit;
1706 set_freezable();
1708 so = 0;
1709 si = 1;
1710 t = 0;
1712 /* Initial state for compares */
1713 mutex_lock(&hotkey_thread_data_mutex);
1714 change_detector = hotkey_config_change;
1715 mask = hotkey_source_mask & hotkey_mask;
1716 mutex_unlock(&hotkey_thread_data_mutex);
1717 hotkey_read_nvram(&s[so], mask);
1719 while (!kthread_should_stop() && hotkey_poll_freq) {
1720 if (t == 0)
1721 t = 1000/hotkey_poll_freq;
1722 t = msleep_interruptible(t);
1723 if (unlikely(kthread_should_stop()))
1724 break;
1725 must_reset = try_to_freeze();
1726 if (t > 0 && !must_reset)
1727 continue;
1729 mutex_lock(&hotkey_thread_data_mutex);
1730 if (must_reset || hotkey_config_change != change_detector) {
1731 /* forget old state on thaw or config change */
1732 si = so;
1733 t = 0;
1734 change_detector = hotkey_config_change;
1736 mask = hotkey_source_mask & hotkey_mask;
1737 mutex_unlock(&hotkey_thread_data_mutex);
1739 if (likely(mask)) {
1740 hotkey_read_nvram(&s[si], mask);
1741 if (likely(si != so)) {
1742 hotkey_compare_and_issue_event(&s[so], &s[si],
1743 mask);
1747 so = si;
1748 si ^= 1;
1751 exit:
1752 mutex_unlock(&hotkey_thread_mutex);
1753 return 0;
1756 static void hotkey_poll_stop_sync(void)
1758 if (tpacpi_hotkey_task) {
1759 if (frozen(tpacpi_hotkey_task) ||
1760 freezing(tpacpi_hotkey_task))
1761 thaw_process(tpacpi_hotkey_task);
1763 kthread_stop(tpacpi_hotkey_task);
1764 tpacpi_hotkey_task = NULL;
1765 mutex_lock(&hotkey_thread_mutex);
1766 /* at this point, the thread did exit */
1767 mutex_unlock(&hotkey_thread_mutex);
1771 /* call with hotkey_mutex held */
1772 static void hotkey_poll_setup(int may_warn)
1774 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1775 hotkey_poll_freq > 0 &&
1776 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1777 if (!tpacpi_hotkey_task) {
1778 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1779 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1780 if (IS_ERR(tpacpi_hotkey_task)) {
1781 tpacpi_hotkey_task = NULL;
1782 printk(TPACPI_ERR
1783 "could not create kernel thread "
1784 "for hotkey polling\n");
1787 } else {
1788 hotkey_poll_stop_sync();
1789 if (may_warn &&
1790 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1791 printk(TPACPI_NOTICE
1792 "hot keys 0x%08x require polling, "
1793 "which is currently disabled\n",
1794 hotkey_source_mask);
1799 static void hotkey_poll_setup_safe(int may_warn)
1801 mutex_lock(&hotkey_mutex);
1802 hotkey_poll_setup(may_warn);
1803 mutex_unlock(&hotkey_mutex);
1806 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1808 static void hotkey_poll_setup_safe(int __unused)
1812 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1814 static int hotkey_inputdev_open(struct input_dev *dev)
1816 switch (tpacpi_lifecycle) {
1817 case TPACPI_LIFE_INIT:
1819 * hotkey_init will call hotkey_poll_setup_safe
1820 * at the appropriate moment
1822 return 0;
1823 case TPACPI_LIFE_EXITING:
1824 return -EBUSY;
1825 case TPACPI_LIFE_RUNNING:
1826 hotkey_poll_setup_safe(0);
1827 return 0;
1830 /* Should only happen if tpacpi_lifecycle is corrupt */
1831 BUG();
1832 return -EBUSY;
1835 static void hotkey_inputdev_close(struct input_dev *dev)
1837 /* disable hotkey polling when possible */
1838 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1839 hotkey_poll_setup_safe(0);
1842 /* sysfs hotkey enable ------------------------------------------------- */
1843 static ssize_t hotkey_enable_show(struct device *dev,
1844 struct device_attribute *attr,
1845 char *buf)
1847 int res, status;
1849 res = hotkey_status_get(&status);
1850 if (res)
1851 return res;
1853 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1856 static ssize_t hotkey_enable_store(struct device *dev,
1857 struct device_attribute *attr,
1858 const char *buf, size_t count)
1860 unsigned long t;
1861 int res;
1863 if (parse_strtoul(buf, 1, &t))
1864 return -EINVAL;
1866 res = hotkey_status_set(t);
1868 return (res) ? res : count;
1871 static struct device_attribute dev_attr_hotkey_enable =
1872 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1873 hotkey_enable_show, hotkey_enable_store);
1875 /* sysfs hotkey mask --------------------------------------------------- */
1876 static ssize_t hotkey_mask_show(struct device *dev,
1877 struct device_attribute *attr,
1878 char *buf)
1880 int res;
1882 if (mutex_lock_killable(&hotkey_mutex))
1883 return -ERESTARTSYS;
1884 res = hotkey_mask_get();
1885 mutex_unlock(&hotkey_mutex);
1887 return (res)?
1888 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1891 static ssize_t hotkey_mask_store(struct device *dev,
1892 struct device_attribute *attr,
1893 const char *buf, size_t count)
1895 unsigned long t;
1896 int res;
1898 if (parse_strtoul(buf, 0xffffffffUL, &t))
1899 return -EINVAL;
1901 if (mutex_lock_killable(&hotkey_mutex))
1902 return -ERESTARTSYS;
1904 res = hotkey_mask_set(t);
1906 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1907 hotkey_poll_setup(1);
1908 #endif
1910 mutex_unlock(&hotkey_mutex);
1912 return (res) ? res : count;
1915 static struct device_attribute dev_attr_hotkey_mask =
1916 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1917 hotkey_mask_show, hotkey_mask_store);
1919 /* sysfs hotkey bios_enabled ------------------------------------------- */
1920 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1921 struct device_attribute *attr,
1922 char *buf)
1924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1927 static struct device_attribute dev_attr_hotkey_bios_enabled =
1928 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1930 /* sysfs hotkey bios_mask ---------------------------------------------- */
1931 static ssize_t hotkey_bios_mask_show(struct device *dev,
1932 struct device_attribute *attr,
1933 char *buf)
1935 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1938 static struct device_attribute dev_attr_hotkey_bios_mask =
1939 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1941 /* sysfs hotkey all_mask ----------------------------------------------- */
1942 static ssize_t hotkey_all_mask_show(struct device *dev,
1943 struct device_attribute *attr,
1944 char *buf)
1946 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1947 hotkey_all_mask | hotkey_source_mask);
1950 static struct device_attribute dev_attr_hotkey_all_mask =
1951 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1953 /* sysfs hotkey recommended_mask --------------------------------------- */
1954 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1955 struct device_attribute *attr,
1956 char *buf)
1958 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1959 (hotkey_all_mask | hotkey_source_mask)
1960 & ~hotkey_reserved_mask);
1963 static struct device_attribute dev_attr_hotkey_recommended_mask =
1964 __ATTR(hotkey_recommended_mask, S_IRUGO,
1965 hotkey_recommended_mask_show, NULL);
1967 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1969 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1970 static ssize_t hotkey_source_mask_show(struct device *dev,
1971 struct device_attribute *attr,
1972 char *buf)
1974 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1977 static ssize_t hotkey_source_mask_store(struct device *dev,
1978 struct device_attribute *attr,
1979 const char *buf, size_t count)
1981 unsigned long t;
1983 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1984 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1985 return -EINVAL;
1987 if (mutex_lock_killable(&hotkey_mutex))
1988 return -ERESTARTSYS;
1990 HOTKEY_CONFIG_CRITICAL_START
1991 hotkey_source_mask = t;
1992 HOTKEY_CONFIG_CRITICAL_END
1994 hotkey_poll_setup(1);
1996 mutex_unlock(&hotkey_mutex);
1998 return count;
2001 static struct device_attribute dev_attr_hotkey_source_mask =
2002 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2003 hotkey_source_mask_show, hotkey_source_mask_store);
2005 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2006 static ssize_t hotkey_poll_freq_show(struct device *dev,
2007 struct device_attribute *attr,
2008 char *buf)
2010 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2013 static ssize_t hotkey_poll_freq_store(struct device *dev,
2014 struct device_attribute *attr,
2015 const char *buf, size_t count)
2017 unsigned long t;
2019 if (parse_strtoul(buf, 25, &t))
2020 return -EINVAL;
2022 if (mutex_lock_killable(&hotkey_mutex))
2023 return -ERESTARTSYS;
2025 hotkey_poll_freq = t;
2027 hotkey_poll_setup(1);
2028 mutex_unlock(&hotkey_mutex);
2030 return count;
2033 static struct device_attribute dev_attr_hotkey_poll_freq =
2034 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2035 hotkey_poll_freq_show, hotkey_poll_freq_store);
2037 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2039 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2040 static ssize_t hotkey_radio_sw_show(struct device *dev,
2041 struct device_attribute *attr,
2042 char *buf)
2044 int res, s;
2045 res = hotkey_get_wlsw(&s);
2046 if (res < 0)
2047 return res;
2049 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2052 static struct device_attribute dev_attr_hotkey_radio_sw =
2053 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2055 static void hotkey_radio_sw_notify_change(void)
2057 if (tp_features.hotkey_wlsw)
2058 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2059 "hotkey_radio_sw");
2062 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2063 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2064 struct device_attribute *attr,
2065 char *buf)
2067 int res, s;
2068 res = hotkey_get_tablet_mode(&s);
2069 if (res < 0)
2070 return res;
2072 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2075 static struct device_attribute dev_attr_hotkey_tablet_mode =
2076 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2078 static void hotkey_tablet_mode_notify_change(void)
2080 if (tp_features.hotkey_tablet)
2081 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2082 "hotkey_tablet_mode");
2085 /* sysfs hotkey report_mode -------------------------------------------- */
2086 static ssize_t hotkey_report_mode_show(struct device *dev,
2087 struct device_attribute *attr,
2088 char *buf)
2090 return snprintf(buf, PAGE_SIZE, "%d\n",
2091 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2094 static struct device_attribute dev_attr_hotkey_report_mode =
2095 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2097 /* sysfs wakeup reason (pollable) -------------------------------------- */
2098 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2099 struct device_attribute *attr,
2100 char *buf)
2102 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2105 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2106 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2108 static void hotkey_wakeup_reason_notify_change(void)
2110 if (tp_features.hotkey_mask)
2111 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2112 "wakeup_reason");
2115 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2116 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2117 struct device_attribute *attr,
2118 char *buf)
2120 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2123 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2124 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2125 hotkey_wakeup_hotunplug_complete_show, NULL);
2127 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2129 if (tp_features.hotkey_mask)
2130 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2131 "wakeup_hotunplug_complete");
2134 /* --------------------------------------------------------------------- */
2136 static struct attribute *hotkey_attributes[] __initdata = {
2137 &dev_attr_hotkey_enable.attr,
2138 &dev_attr_hotkey_bios_enabled.attr,
2139 &dev_attr_hotkey_report_mode.attr,
2140 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2141 &dev_attr_hotkey_mask.attr,
2142 &dev_attr_hotkey_all_mask.attr,
2143 &dev_attr_hotkey_recommended_mask.attr,
2144 &dev_attr_hotkey_source_mask.attr,
2145 &dev_attr_hotkey_poll_freq.attr,
2146 #endif
2149 static struct attribute *hotkey_mask_attributes[] __initdata = {
2150 &dev_attr_hotkey_bios_mask.attr,
2151 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2152 &dev_attr_hotkey_mask.attr,
2153 &dev_attr_hotkey_all_mask.attr,
2154 &dev_attr_hotkey_recommended_mask.attr,
2155 #endif
2156 &dev_attr_hotkey_wakeup_reason.attr,
2157 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2160 static void bluetooth_update_rfk(void);
2161 static void wan_update_rfk(void);
2162 static void uwb_update_rfk(void);
2163 static void tpacpi_send_radiosw_update(void)
2165 int wlsw;
2167 /* Sync these BEFORE sending any rfkill events */
2168 if (tp_features.bluetooth)
2169 bluetooth_update_rfk();
2170 if (tp_features.wan)
2171 wan_update_rfk();
2172 if (tp_features.uwb)
2173 uwb_update_rfk();
2175 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2176 mutex_lock(&tpacpi_inputdev_send_mutex);
2178 input_report_switch(tpacpi_inputdev,
2179 SW_RFKILL_ALL, !!wlsw);
2180 input_sync(tpacpi_inputdev);
2182 mutex_unlock(&tpacpi_inputdev_send_mutex);
2184 hotkey_radio_sw_notify_change();
2187 static void hotkey_exit(void)
2189 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2190 hotkey_poll_stop_sync();
2191 #endif
2193 if (hotkey_dev_attributes)
2194 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2196 kfree(hotkey_keycode_map);
2198 if (tp_features.hotkey) {
2199 dbg_printk(TPACPI_DBG_EXIT,
2200 "restoring original hot key mask\n");
2201 /* no short-circuit boolean operator below! */
2202 if ((hotkey_mask_set(hotkey_orig_mask) |
2203 hotkey_status_set(hotkey_orig_status)) != 0)
2204 printk(TPACPI_ERR
2205 "failed to restore hot key mask "
2206 "to BIOS defaults\n");
2210 static int __init hotkey_init(struct ibm_init_struct *iibm)
2212 /* Requirements for changing the default keymaps:
2214 * 1. Many of the keys are mapped to KEY_RESERVED for very
2215 * good reasons. Do not change them unless you have deep
2216 * knowledge on the IBM and Lenovo ThinkPad firmware for
2217 * the various ThinkPad models. The driver behaves
2218 * differently for KEY_RESERVED: such keys have their
2219 * hot key mask *unset* in mask_recommended, and also
2220 * in the initial hot key mask programmed into the
2221 * firmware at driver load time, which means the firm-
2222 * ware may react very differently if you change them to
2223 * something else;
2225 * 2. You must be subscribed to the linux-thinkpad and
2226 * ibm-acpi-devel mailing lists, and you should read the
2227 * list archives since 2007 if you want to change the
2228 * keymaps. This requirement exists so that you will
2229 * know the past history of problems with the thinkpad-
2230 * acpi driver keymaps, and also that you will be
2231 * listening to any bug reports;
2233 * 3. Do not send thinkpad-acpi specific patches directly to
2234 * for merging, *ever*. Send them to the linux-acpi
2235 * mailinglist for comments. Merging is to be done only
2236 * through acpi-test and the ACPI maintainer.
2238 * If the above is too much to ask, don't change the keymap.
2239 * Ask the thinkpad-acpi maintainer to do it, instead.
2241 static u16 ibm_keycode_map[] __initdata = {
2242 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2243 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2244 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2245 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2247 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2248 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2249 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2250 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2252 /* brightness: firmware always reacts to them, unless
2253 * X.org did some tricks in the radeon BIOS scratch
2254 * registers of *some* models */
2255 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2256 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2258 /* Thinklight: firmware always react to it */
2259 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2261 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2262 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2264 /* Volume: firmware always react to it and reprograms
2265 * the built-in *extra* mixer. Never map it to control
2266 * another mixer by default. */
2267 KEY_RESERVED, /* 0x14: VOLUME UP */
2268 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2269 KEY_RESERVED, /* 0x16: MUTE */
2271 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2273 /* (assignments unknown, please report if found) */
2274 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2275 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2277 static u16 lenovo_keycode_map[] __initdata = {
2278 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2279 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2280 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2281 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2283 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2284 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2285 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2286 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2288 /* These either have to go through ACPI video, or
2289 * act like in the IBM ThinkPads, so don't ever
2290 * enable them by default */
2291 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2292 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2294 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2296 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2297 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2299 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2300 * react to it and reprograms the built-in *extra* mixer.
2301 * Never map it to control another mixer by default.
2303 * T60?, T61, R60?, R61: firmware and EC tries to send
2304 * these over the regular keyboard, so these are no-ops,
2305 * but there are still weird bugs re. MUTE, so do not
2306 * change unless you get test reports from all Lenovo
2307 * models. May cause the BIOS to interfere with the
2308 * HDA mixer.
2310 KEY_RESERVED, /* 0x14: VOLUME UP */
2311 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2312 KEY_RESERVED, /* 0x16: MUTE */
2314 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2316 /* (assignments unknown, please report if found) */
2317 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2318 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2321 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2322 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2323 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2325 int res, i;
2326 int status;
2327 int hkeyv;
2329 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2331 BUG_ON(!tpacpi_inputdev);
2332 BUG_ON(tpacpi_inputdev->open != NULL ||
2333 tpacpi_inputdev->close != NULL);
2335 TPACPI_ACPIHANDLE_INIT(hkey);
2336 mutex_init(&hotkey_mutex);
2338 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2339 mutex_init(&hotkey_thread_mutex);
2340 mutex_init(&hotkey_thread_data_mutex);
2341 #endif
2343 /* hotkey not supported on 570 */
2344 tp_features.hotkey = hkey_handle != NULL;
2346 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2347 str_supported(tp_features.hotkey));
2349 if (!tp_features.hotkey)
2350 return 1;
2352 tpacpi_disable_brightness_delay();
2354 hotkey_dev_attributes = create_attr_set(13, NULL);
2355 if (!hotkey_dev_attributes)
2356 return -ENOMEM;
2357 res = add_many_to_attr_set(hotkey_dev_attributes,
2358 hotkey_attributes,
2359 ARRAY_SIZE(hotkey_attributes));
2360 if (res)
2361 goto err_exit;
2363 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2364 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2365 for HKEY interface version 0x100 */
2366 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2367 if ((hkeyv >> 8) != 1) {
2368 printk(TPACPI_ERR "unknown version of the "
2369 "HKEY interface: 0x%x\n", hkeyv);
2370 printk(TPACPI_ERR "please report this to %s\n",
2371 TPACPI_MAIL);
2372 } else {
2374 * MHKV 0x100 in A31, R40, R40e,
2375 * T4x, X31, and later
2377 tp_features.hotkey_mask = 1;
2381 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2382 str_supported(tp_features.hotkey_mask));
2384 if (tp_features.hotkey_mask) {
2385 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2386 "MHKA", "qd")) {
2387 printk(TPACPI_ERR
2388 "missing MHKA handler, "
2389 "please report this to %s\n",
2390 TPACPI_MAIL);
2391 /* FN+F12, FN+F4, FN+F3 */
2392 hotkey_all_mask = 0x080cU;
2396 /* hotkey_source_mask *must* be zero for
2397 * the first hotkey_mask_get */
2398 res = hotkey_status_get(&hotkey_orig_status);
2399 if (res)
2400 goto err_exit;
2402 if (tp_features.hotkey_mask) {
2403 res = hotkey_mask_get();
2404 if (res)
2405 goto err_exit;
2407 hotkey_orig_mask = hotkey_mask;
2408 res = add_many_to_attr_set(
2409 hotkey_dev_attributes,
2410 hotkey_mask_attributes,
2411 ARRAY_SIZE(hotkey_mask_attributes));
2412 if (res)
2413 goto err_exit;
2416 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2417 if (tp_features.hotkey_mask) {
2418 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2419 & ~hotkey_all_mask;
2420 } else {
2421 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2424 vdbg_printk(TPACPI_DBG_INIT,
2425 "hotkey source mask 0x%08x, polling freq %d\n",
2426 hotkey_source_mask, hotkey_poll_freq);
2427 #endif
2429 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2430 if (dbg_wlswemul) {
2431 tp_features.hotkey_wlsw = 1;
2432 printk(TPACPI_INFO
2433 "radio switch emulation enabled\n");
2434 } else
2435 #endif
2436 /* Not all thinkpads have a hardware radio switch */
2437 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2438 tp_features.hotkey_wlsw = 1;
2439 printk(TPACPI_INFO
2440 "radio switch found; radios are %s\n",
2441 enabled(status, 0));
2443 if (tp_features.hotkey_wlsw)
2444 res = add_to_attr_set(hotkey_dev_attributes,
2445 &dev_attr_hotkey_radio_sw.attr);
2447 /* For X41t, X60t, X61t Tablets... */
2448 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2449 tp_features.hotkey_tablet = 1;
2450 printk(TPACPI_INFO
2451 "possible tablet mode switch found; "
2452 "ThinkPad in %s mode\n",
2453 (status & TP_HOTKEY_TABLET_MASK)?
2454 "tablet" : "laptop");
2455 res = add_to_attr_set(hotkey_dev_attributes,
2456 &dev_attr_hotkey_tablet_mode.attr);
2459 if (!res)
2460 res = register_attr_set_with_sysfs(
2461 hotkey_dev_attributes,
2462 &tpacpi_pdev->dev.kobj);
2463 if (res)
2464 goto err_exit;
2466 /* Set up key map */
2468 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2469 GFP_KERNEL);
2470 if (!hotkey_keycode_map) {
2471 printk(TPACPI_ERR
2472 "failed to allocate memory for key map\n");
2473 res = -ENOMEM;
2474 goto err_exit;
2477 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2478 dbg_printk(TPACPI_DBG_INIT,
2479 "using Lenovo default hot key map\n");
2480 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2481 TPACPI_HOTKEY_MAP_SIZE);
2482 } else {
2483 dbg_printk(TPACPI_DBG_INIT,
2484 "using IBM default hot key map\n");
2485 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2486 TPACPI_HOTKEY_MAP_SIZE);
2489 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2490 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2491 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2492 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2493 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2494 tpacpi_inputdev->keycode = hotkey_keycode_map;
2495 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2496 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2497 set_bit(hotkey_keycode_map[i],
2498 tpacpi_inputdev->keybit);
2499 } else {
2500 if (i < sizeof(hotkey_reserved_mask)*8)
2501 hotkey_reserved_mask |= 1 << i;
2505 if (tp_features.hotkey_wlsw) {
2506 set_bit(EV_SW, tpacpi_inputdev->evbit);
2507 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2509 if (tp_features.hotkey_tablet) {
2510 set_bit(EV_SW, tpacpi_inputdev->evbit);
2511 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2514 /* Do not issue duplicate brightness change events to
2515 * userspace */
2516 if (!tp_features.bright_acpimode)
2517 /* update bright_acpimode... */
2518 tpacpi_check_std_acpi_brightness_support();
2520 if (tp_features.bright_acpimode) {
2521 printk(TPACPI_INFO
2522 "This ThinkPad has standard ACPI backlight "
2523 "brightness control, supported by the ACPI "
2524 "video driver\n");
2525 printk(TPACPI_NOTICE
2526 "Disabling thinkpad-acpi brightness events "
2527 "by default...\n");
2529 /* The hotkey_reserved_mask change below is not
2530 * necessary while the keys are at KEY_RESERVED in the
2531 * default map, but better safe than sorry, leave it
2532 * here as a marker of what we have to do, especially
2533 * when we finally become able to set this at runtime
2534 * on response to X.org requests */
2535 hotkey_reserved_mask |=
2536 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2537 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2540 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2541 res = hotkey_status_set(1);
2542 if (res) {
2543 hotkey_exit();
2544 return res;
2546 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2547 & ~hotkey_reserved_mask)
2548 | hotkey_orig_mask);
2549 if (res < 0 && res != -ENXIO) {
2550 hotkey_exit();
2551 return res;
2554 dbg_printk(TPACPI_DBG_INIT,
2555 "legacy hot key reporting over procfs %s\n",
2556 (hotkey_report_mode < 2) ?
2557 "enabled" : "disabled");
2559 tpacpi_inputdev->open = &hotkey_inputdev_open;
2560 tpacpi_inputdev->close = &hotkey_inputdev_close;
2562 hotkey_poll_setup_safe(1);
2563 tpacpi_send_radiosw_update();
2564 tpacpi_input_send_tabletsw();
2566 return 0;
2568 err_exit:
2569 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2570 hotkey_dev_attributes = NULL;
2572 return (res < 0)? res : 1;
2575 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2577 u32 hkey;
2578 unsigned int scancode;
2579 int send_acpi_ev;
2580 int ignore_acpi_ev;
2581 int unk_ev;
2583 if (event != 0x80) {
2584 printk(TPACPI_ERR
2585 "unknown HKEY notification event %d\n", event);
2586 /* forward it to userspace, maybe it knows how to handle it */
2587 acpi_bus_generate_netlink_event(
2588 ibm->acpi->device->pnp.device_class,
2589 ibm->acpi->device->dev.bus_id,
2590 event, 0);
2591 return;
2594 while (1) {
2595 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2596 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2597 return;
2600 if (hkey == 0) {
2601 /* queue empty */
2602 return;
2605 send_acpi_ev = 1;
2606 ignore_acpi_ev = 0;
2607 unk_ev = 0;
2609 switch (hkey >> 12) {
2610 case 1:
2611 /* 0x1000-0x1FFF: key presses */
2612 scancode = hkey & 0xfff;
2613 if (scancode > 0 && scancode < 0x21) {
2614 scancode--;
2615 if (!(hotkey_source_mask & (1 << scancode))) {
2616 tpacpi_input_send_key(scancode);
2617 send_acpi_ev = 0;
2618 } else {
2619 ignore_acpi_ev = 1;
2621 } else {
2622 unk_ev = 1;
2624 break;
2625 case 2:
2626 /* Wakeup reason */
2627 switch (hkey) {
2628 case 0x2304: /* suspend, undock */
2629 case 0x2404: /* hibernation, undock */
2630 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2631 ignore_acpi_ev = 1;
2632 break;
2633 case 0x2305: /* suspend, bay eject */
2634 case 0x2405: /* hibernation, bay eject */
2635 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2636 ignore_acpi_ev = 1;
2637 break;
2638 default:
2639 unk_ev = 1;
2641 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2642 printk(TPACPI_INFO
2643 "woke up due to a hot-unplug "
2644 "request...\n");
2645 hotkey_wakeup_reason_notify_change();
2647 break;
2648 case 3:
2649 /* bay-related wakeups */
2650 if (hkey == 0x3003) {
2651 hotkey_autosleep_ack = 1;
2652 printk(TPACPI_INFO
2653 "bay ejected\n");
2654 hotkey_wakeup_hotunplug_complete_notify_change();
2655 } else {
2656 unk_ev = 1;
2658 break;
2659 case 4:
2660 /* dock-related wakeups */
2661 if (hkey == 0x4003) {
2662 hotkey_autosleep_ack = 1;
2663 printk(TPACPI_INFO
2664 "undocked\n");
2665 hotkey_wakeup_hotunplug_complete_notify_change();
2666 } else {
2667 unk_ev = 1;
2669 break;
2670 case 5:
2671 /* 0x5000-0x5FFF: human interface helpers */
2672 switch (hkey) {
2673 case 0x5010: /* Lenovo new BIOS: brightness changed */
2674 case 0x500b: /* X61t: tablet pen inserted into bay */
2675 case 0x500c: /* X61t: tablet pen removed from bay */
2676 break;
2677 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2678 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2679 tpacpi_input_send_tabletsw();
2680 hotkey_tablet_mode_notify_change();
2681 send_acpi_ev = 0;
2682 break;
2683 case 0x5001:
2684 case 0x5002:
2685 /* LID switch events. Do not propagate */
2686 ignore_acpi_ev = 1;
2687 break;
2688 default:
2689 unk_ev = 1;
2691 break;
2692 case 7:
2693 /* 0x7000-0x7FFF: misc */
2694 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2695 tpacpi_send_radiosw_update();
2696 send_acpi_ev = 0;
2697 break;
2699 /* fallthrough to default */
2700 default:
2701 unk_ev = 1;
2703 if (unk_ev) {
2704 printk(TPACPI_NOTICE
2705 "unhandled HKEY event 0x%04x\n", hkey);
2708 /* Legacy events */
2709 if (!ignore_acpi_ev &&
2710 (send_acpi_ev || hotkey_report_mode < 2)) {
2711 acpi_bus_generate_proc_event(ibm->acpi->device,
2712 event, hkey);
2715 /* netlink events */
2716 if (!ignore_acpi_ev && send_acpi_ev) {
2717 acpi_bus_generate_netlink_event(
2718 ibm->acpi->device->pnp.device_class,
2719 ibm->acpi->device->dev.bus_id,
2720 event, hkey);
2725 static void hotkey_suspend(pm_message_t state)
2727 /* Do these on suspend, we get the events on early resume! */
2728 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2729 hotkey_autosleep_ack = 0;
2732 static void hotkey_resume(void)
2734 tpacpi_disable_brightness_delay();
2736 if (hotkey_mask_get())
2737 printk(TPACPI_ERR
2738 "error while trying to read hot key mask "
2739 "from firmware\n");
2740 tpacpi_send_radiosw_update();
2741 hotkey_tablet_mode_notify_change();
2742 hotkey_wakeup_reason_notify_change();
2743 hotkey_wakeup_hotunplug_complete_notify_change();
2744 hotkey_poll_setup_safe(0);
2747 /* procfs -------------------------------------------------------------- */
2748 static int hotkey_read(char *p)
2750 int res, status;
2751 int len = 0;
2753 if (!tp_features.hotkey) {
2754 len += sprintf(p + len, "status:\t\tnot supported\n");
2755 return len;
2758 if (mutex_lock_killable(&hotkey_mutex))
2759 return -ERESTARTSYS;
2760 res = hotkey_status_get(&status);
2761 if (!res)
2762 res = hotkey_mask_get();
2763 mutex_unlock(&hotkey_mutex);
2764 if (res)
2765 return res;
2767 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2768 if (tp_features.hotkey_mask) {
2769 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2770 len += sprintf(p + len,
2771 "commands:\tenable, disable, reset, <mask>\n");
2772 } else {
2773 len += sprintf(p + len, "mask:\t\tnot supported\n");
2774 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2777 return len;
2780 static int hotkey_write(char *buf)
2782 int res, status;
2783 u32 mask;
2784 char *cmd;
2786 if (!tp_features.hotkey)
2787 return -ENODEV;
2789 if (mutex_lock_killable(&hotkey_mutex))
2790 return -ERESTARTSYS;
2792 status = -1;
2793 mask = hotkey_mask;
2795 res = 0;
2796 while ((cmd = next_cmd(&buf))) {
2797 if (strlencmp(cmd, "enable") == 0) {
2798 status = 1;
2799 } else if (strlencmp(cmd, "disable") == 0) {
2800 status = 0;
2801 } else if (strlencmp(cmd, "reset") == 0) {
2802 status = hotkey_orig_status;
2803 mask = hotkey_orig_mask;
2804 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2805 /* mask set */
2806 } else if (sscanf(cmd, "%x", &mask) == 1) {
2807 /* mask set */
2808 } else {
2809 res = -EINVAL;
2810 goto errexit;
2813 if (status != -1)
2814 res = hotkey_status_set(status);
2816 if (!res && mask != hotkey_mask)
2817 res = hotkey_mask_set(mask);
2819 errexit:
2820 mutex_unlock(&hotkey_mutex);
2821 return res;
2824 static const struct acpi_device_id ibm_htk_device_ids[] = {
2825 {TPACPI_ACPI_HKEY_HID, 0},
2826 {"", 0},
2829 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2830 .hid = ibm_htk_device_ids,
2831 .notify = hotkey_notify,
2832 .handle = &hkey_handle,
2833 .type = ACPI_DEVICE_NOTIFY,
2836 static struct ibm_struct hotkey_driver_data = {
2837 .name = "hotkey",
2838 .read = hotkey_read,
2839 .write = hotkey_write,
2840 .exit = hotkey_exit,
2841 .resume = hotkey_resume,
2842 .suspend = hotkey_suspend,
2843 .acpi = &ibm_hotkey_acpidriver,
2846 /*************************************************************************
2847 * Bluetooth subdriver
2850 enum {
2851 /* ACPI GBDC/SBDC bits */
2852 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2853 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2854 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2855 off / last state */
2858 enum {
2859 /* ACPI \BLTH commands */
2860 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2861 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2862 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2863 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2864 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2867 static struct rfkill *tpacpi_bluetooth_rfkill;
2869 static void bluetooth_suspend(pm_message_t state)
2871 /* Try to make sure radio will resume powered off */
2872 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2873 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2876 static int bluetooth_get_radiosw(void)
2878 int status;
2880 if (!tp_features.bluetooth)
2881 return -ENODEV;
2883 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2884 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2885 return RFKILL_STATE_HARD_BLOCKED;
2887 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2888 if (dbg_bluetoothemul)
2889 return (tpacpi_bluetooth_emulstate) ?
2890 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2891 #endif
2893 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2894 return -EIO;
2896 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2897 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2900 static void bluetooth_update_rfk(void)
2902 int status;
2904 if (!tpacpi_bluetooth_rfkill)
2905 return;
2907 status = bluetooth_get_radiosw();
2908 if (status < 0)
2909 return;
2910 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2913 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2915 int status;
2917 if (!tp_features.bluetooth)
2918 return -ENODEV;
2920 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2921 * reason to risk weird behaviour. */
2922 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2923 && radio_on)
2924 return -EPERM;
2926 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2927 if (dbg_bluetoothemul) {
2928 tpacpi_bluetooth_emulstate = !!radio_on;
2929 if (update_rfk)
2930 bluetooth_update_rfk();
2931 return 0;
2933 #endif
2935 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
2936 if (radio_on)
2937 status = TP_ACPI_BLUETOOTH_RADIOSSW;
2938 else
2939 status = 0;
2940 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2941 return -EIO;
2943 if (update_rfk)
2944 bluetooth_update_rfk();
2946 return 0;
2949 /* sysfs bluetooth enable ---------------------------------------------- */
2950 static ssize_t bluetooth_enable_show(struct device *dev,
2951 struct device_attribute *attr,
2952 char *buf)
2954 int status;
2956 status = bluetooth_get_radiosw();
2957 if (status < 0)
2958 return status;
2960 return snprintf(buf, PAGE_SIZE, "%d\n",
2961 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2964 static ssize_t bluetooth_enable_store(struct device *dev,
2965 struct device_attribute *attr,
2966 const char *buf, size_t count)
2968 unsigned long t;
2969 int res;
2971 if (parse_strtoul(buf, 1, &t))
2972 return -EINVAL;
2974 res = bluetooth_set_radiosw(t, 1);
2976 return (res) ? res : count;
2979 static struct device_attribute dev_attr_bluetooth_enable =
2980 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2981 bluetooth_enable_show, bluetooth_enable_store);
2983 /* --------------------------------------------------------------------- */
2985 static struct attribute *bluetooth_attributes[] = {
2986 &dev_attr_bluetooth_enable.attr,
2987 NULL
2990 static const struct attribute_group bluetooth_attr_group = {
2991 .attrs = bluetooth_attributes,
2994 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2996 int bts = bluetooth_get_radiosw();
2998 if (bts < 0)
2999 return bts;
3001 *state = bts;
3002 return 0;
3005 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3007 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3010 static void bluetooth_shutdown(void)
3012 /* Order firmware to save current state to NVRAM */
3013 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3014 TP_ACPI_BLTH_SAVE_STATE))
3015 printk(TPACPI_NOTICE
3016 "failed to save bluetooth state to NVRAM\n");
3019 static void bluetooth_exit(void)
3021 bluetooth_shutdown();
3023 if (tpacpi_bluetooth_rfkill)
3024 rfkill_unregister(tpacpi_bluetooth_rfkill);
3026 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3027 &bluetooth_attr_group);
3030 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3032 int res;
3033 int status = 0;
3035 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3037 TPACPI_ACPIHANDLE_INIT(hkey);
3039 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3040 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3041 tp_features.bluetooth = hkey_handle &&
3042 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3044 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3045 str_supported(tp_features.bluetooth),
3046 status);
3048 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3049 if (dbg_bluetoothemul) {
3050 tp_features.bluetooth = 1;
3051 printk(TPACPI_INFO
3052 "bluetooth switch emulation enabled\n");
3053 } else
3054 #endif
3055 if (tp_features.bluetooth &&
3056 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3057 /* no bluetooth hardware present in system */
3058 tp_features.bluetooth = 0;
3059 dbg_printk(TPACPI_DBG_INIT,
3060 "bluetooth hardware not installed\n");
3063 if (!tp_features.bluetooth)
3064 return 1;
3066 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3067 &bluetooth_attr_group);
3068 if (res)
3069 return res;
3071 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3072 &tpacpi_bluetooth_rfkill,
3073 RFKILL_TYPE_BLUETOOTH,
3074 "tpacpi_bluetooth_sw",
3075 true,
3076 tpacpi_bluetooth_rfk_set,
3077 tpacpi_bluetooth_rfk_get);
3078 if (res) {
3079 bluetooth_exit();
3080 return res;
3083 return 0;
3086 /* procfs -------------------------------------------------------------- */
3087 static int bluetooth_read(char *p)
3089 int len = 0;
3090 int status = bluetooth_get_radiosw();
3092 if (!tp_features.bluetooth)
3093 len += sprintf(p + len, "status:\t\tnot supported\n");
3094 else {
3095 len += sprintf(p + len, "status:\t\t%s\n",
3096 (status == RFKILL_STATE_UNBLOCKED) ?
3097 "enabled" : "disabled");
3098 len += sprintf(p + len, "commands:\tenable, disable\n");
3101 return len;
3104 static int bluetooth_write(char *buf)
3106 char *cmd;
3108 if (!tp_features.bluetooth)
3109 return -ENODEV;
3111 while ((cmd = next_cmd(&buf))) {
3112 if (strlencmp(cmd, "enable") == 0) {
3113 bluetooth_set_radiosw(1, 1);
3114 } else if (strlencmp(cmd, "disable") == 0) {
3115 bluetooth_set_radiosw(0, 1);
3116 } else
3117 return -EINVAL;
3120 return 0;
3123 static struct ibm_struct bluetooth_driver_data = {
3124 .name = "bluetooth",
3125 .read = bluetooth_read,
3126 .write = bluetooth_write,
3127 .exit = bluetooth_exit,
3128 .suspend = bluetooth_suspend,
3129 .shutdown = bluetooth_shutdown,
3132 /*************************************************************************
3133 * Wan subdriver
3136 enum {
3137 /* ACPI GWAN/SWAN bits */
3138 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3139 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3140 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3141 off / last state */
3144 static struct rfkill *tpacpi_wan_rfkill;
3146 static void wan_suspend(pm_message_t state)
3148 /* Try to make sure radio will resume powered off */
3149 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3150 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3153 static int wan_get_radiosw(void)
3155 int status;
3157 if (!tp_features.wan)
3158 return -ENODEV;
3160 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3161 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3162 return RFKILL_STATE_HARD_BLOCKED;
3164 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3165 if (dbg_wwanemul)
3166 return (tpacpi_wwan_emulstate) ?
3167 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3168 #endif
3170 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3171 return -EIO;
3173 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3174 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3177 static void wan_update_rfk(void)
3179 int status;
3181 if (!tpacpi_wan_rfkill)
3182 return;
3184 status = wan_get_radiosw();
3185 if (status < 0)
3186 return;
3187 rfkill_force_state(tpacpi_wan_rfkill, status);
3190 static int wan_set_radiosw(int radio_on, int update_rfk)
3192 int status;
3194 if (!tp_features.wan)
3195 return -ENODEV;
3197 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3198 * reason to risk weird behaviour. */
3199 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3200 && radio_on)
3201 return -EPERM;
3203 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3204 if (dbg_wwanemul) {
3205 tpacpi_wwan_emulstate = !!radio_on;
3206 if (update_rfk)
3207 wan_update_rfk();
3208 return 0;
3210 #endif
3212 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3213 if (radio_on)
3214 status = TP_ACPI_WANCARD_RADIOSSW;
3215 else
3216 status = 0;
3217 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3218 return -EIO;
3220 if (update_rfk)
3221 wan_update_rfk();
3223 return 0;
3226 /* sysfs wan enable ---------------------------------------------------- */
3227 static ssize_t wan_enable_show(struct device *dev,
3228 struct device_attribute *attr,
3229 char *buf)
3231 int status;
3233 status = wan_get_radiosw();
3234 if (status < 0)
3235 return status;
3237 return snprintf(buf, PAGE_SIZE, "%d\n",
3238 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3241 static ssize_t wan_enable_store(struct device *dev,
3242 struct device_attribute *attr,
3243 const char *buf, size_t count)
3245 unsigned long t;
3246 int res;
3248 if (parse_strtoul(buf, 1, &t))
3249 return -EINVAL;
3251 res = wan_set_radiosw(t, 1);
3253 return (res) ? res : count;
3256 static struct device_attribute dev_attr_wan_enable =
3257 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3258 wan_enable_show, wan_enable_store);
3260 /* --------------------------------------------------------------------- */
3262 static struct attribute *wan_attributes[] = {
3263 &dev_attr_wan_enable.attr,
3264 NULL
3267 static const struct attribute_group wan_attr_group = {
3268 .attrs = wan_attributes,
3271 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3273 int wans = wan_get_radiosw();
3275 if (wans < 0)
3276 return wans;
3278 *state = wans;
3279 return 0;
3282 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3284 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3287 static void wan_shutdown(void)
3289 /* Order firmware to save current state to NVRAM */
3290 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3291 TP_ACPI_WGSV_SAVE_STATE))
3292 printk(TPACPI_NOTICE
3293 "failed to save WWAN state to NVRAM\n");
3296 static void wan_exit(void)
3298 wan_shutdown();
3300 if (tpacpi_wan_rfkill)
3301 rfkill_unregister(tpacpi_wan_rfkill);
3303 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3304 &wan_attr_group);
3307 static int __init wan_init(struct ibm_init_struct *iibm)
3309 int res;
3310 int status = 0;
3312 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3314 TPACPI_ACPIHANDLE_INIT(hkey);
3316 tp_features.wan = hkey_handle &&
3317 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3319 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3320 str_supported(tp_features.wan),
3321 status);
3323 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3324 if (dbg_wwanemul) {
3325 tp_features.wan = 1;
3326 printk(TPACPI_INFO
3327 "wwan switch emulation enabled\n");
3328 } else
3329 #endif
3330 if (tp_features.wan &&
3331 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3332 /* no wan hardware present in system */
3333 tp_features.wan = 0;
3334 dbg_printk(TPACPI_DBG_INIT,
3335 "wan hardware not installed\n");
3338 if (!tp_features.wan)
3339 return 1;
3341 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3342 &wan_attr_group);
3343 if (res)
3344 return res;
3346 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3347 &tpacpi_wan_rfkill,
3348 RFKILL_TYPE_WWAN,
3349 "tpacpi_wwan_sw",
3350 true,
3351 tpacpi_wan_rfk_set,
3352 tpacpi_wan_rfk_get);
3353 if (res) {
3354 wan_exit();
3355 return res;
3358 return 0;
3361 /* procfs -------------------------------------------------------------- */
3362 static int wan_read(char *p)
3364 int len = 0;
3365 int status = wan_get_radiosw();
3367 if (!tp_features.wan)
3368 len += sprintf(p + len, "status:\t\tnot supported\n");
3369 else {
3370 len += sprintf(p + len, "status:\t\t%s\n",
3371 (status == RFKILL_STATE_UNBLOCKED) ?
3372 "enabled" : "disabled");
3373 len += sprintf(p + len, "commands:\tenable, disable\n");
3376 return len;
3379 static int wan_write(char *buf)
3381 char *cmd;
3383 if (!tp_features.wan)
3384 return -ENODEV;
3386 while ((cmd = next_cmd(&buf))) {
3387 if (strlencmp(cmd, "enable") == 0) {
3388 wan_set_radiosw(1, 1);
3389 } else if (strlencmp(cmd, "disable") == 0) {
3390 wan_set_radiosw(0, 1);
3391 } else
3392 return -EINVAL;
3395 return 0;
3398 static struct ibm_struct wan_driver_data = {
3399 .name = "wan",
3400 .read = wan_read,
3401 .write = wan_write,
3402 .exit = wan_exit,
3403 .suspend = wan_suspend,
3404 .shutdown = wan_shutdown,
3407 /*************************************************************************
3408 * UWB subdriver
3411 enum {
3412 /* ACPI GUWB/SUWB bits */
3413 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3414 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3417 static struct rfkill *tpacpi_uwb_rfkill;
3419 static int uwb_get_radiosw(void)
3421 int status;
3423 if (!tp_features.uwb)
3424 return -ENODEV;
3426 /* WLSW overrides UWB in firmware/hardware, reflect that */
3427 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3428 return RFKILL_STATE_HARD_BLOCKED;
3430 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3431 if (dbg_uwbemul)
3432 return (tpacpi_uwb_emulstate) ?
3433 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3434 #endif
3436 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3437 return -EIO;
3439 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3440 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3443 static void uwb_update_rfk(void)
3445 int status;
3447 if (!tpacpi_uwb_rfkill)
3448 return;
3450 status = uwb_get_radiosw();
3451 if (status < 0)
3452 return;
3453 rfkill_force_state(tpacpi_uwb_rfkill, status);
3456 static int uwb_set_radiosw(int radio_on, int update_rfk)
3458 int status;
3460 if (!tp_features.uwb)
3461 return -ENODEV;
3463 /* WLSW overrides UWB in firmware/hardware, but there is no
3464 * reason to risk weird behaviour. */
3465 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3466 && radio_on)
3467 return -EPERM;
3469 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3470 if (dbg_uwbemul) {
3471 tpacpi_uwb_emulstate = !!radio_on;
3472 if (update_rfk)
3473 uwb_update_rfk();
3474 return 0;
3476 #endif
3478 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3479 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3480 return -EIO;
3482 if (update_rfk)
3483 uwb_update_rfk();
3485 return 0;
3488 /* --------------------------------------------------------------------- */
3490 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3492 int uwbs = uwb_get_radiosw();
3494 if (uwbs < 0)
3495 return uwbs;
3497 *state = uwbs;
3498 return 0;
3501 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3503 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3506 static void uwb_exit(void)
3508 if (tpacpi_uwb_rfkill)
3509 rfkill_unregister(tpacpi_uwb_rfkill);
3512 static int __init uwb_init(struct ibm_init_struct *iibm)
3514 int res;
3515 int status = 0;
3517 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3519 TPACPI_ACPIHANDLE_INIT(hkey);
3521 tp_features.uwb = hkey_handle &&
3522 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3524 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3525 str_supported(tp_features.uwb),
3526 status);
3528 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3529 if (dbg_uwbemul) {
3530 tp_features.uwb = 1;
3531 printk(TPACPI_INFO
3532 "uwb switch emulation enabled\n");
3533 } else
3534 #endif
3535 if (tp_features.uwb &&
3536 !(status & TP_ACPI_UWB_HWPRESENT)) {
3537 /* no uwb hardware present in system */
3538 tp_features.uwb = 0;
3539 dbg_printk(TPACPI_DBG_INIT,
3540 "uwb hardware not installed\n");
3543 if (!tp_features.uwb)
3544 return 1;
3546 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3547 &tpacpi_uwb_rfkill,
3548 RFKILL_TYPE_UWB,
3549 "tpacpi_uwb_sw",
3550 false,
3551 tpacpi_uwb_rfk_set,
3552 tpacpi_uwb_rfk_get);
3554 return res;
3557 static struct ibm_struct uwb_driver_data = {
3558 .name = "uwb",
3559 .exit = uwb_exit,
3560 .flags.experimental = 1,
3563 /*************************************************************************
3564 * Video subdriver
3567 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3569 enum video_access_mode {
3570 TPACPI_VIDEO_NONE = 0,
3571 TPACPI_VIDEO_570, /* 570 */
3572 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3573 TPACPI_VIDEO_NEW, /* all others */
3576 enum { /* video status flags, based on VIDEO_570 */
3577 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3578 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3579 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3582 enum { /* TPACPI_VIDEO_570 constants */
3583 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3584 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3585 * video_status_flags */
3586 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3587 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3590 static enum video_access_mode video_supported;
3591 static int video_orig_autosw;
3593 static int video_autosw_get(void);
3594 static int video_autosw_set(int enable);
3596 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3598 static int __init video_init(struct ibm_init_struct *iibm)
3600 int ivga;
3602 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3604 TPACPI_ACPIHANDLE_INIT(vid);
3605 TPACPI_ACPIHANDLE_INIT(vid2);
3607 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3608 /* G41, assume IVGA doesn't change */
3609 vid_handle = vid2_handle;
3611 if (!vid_handle)
3612 /* video switching not supported on R30, R31 */
3613 video_supported = TPACPI_VIDEO_NONE;
3614 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3615 /* 570 */
3616 video_supported = TPACPI_VIDEO_570;
3617 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3618 /* 600e/x, 770e, 770x */
3619 video_supported = TPACPI_VIDEO_770;
3620 else
3621 /* all others */
3622 video_supported = TPACPI_VIDEO_NEW;
3624 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3625 str_supported(video_supported != TPACPI_VIDEO_NONE),
3626 video_supported);
3628 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3631 static void video_exit(void)
3633 dbg_printk(TPACPI_DBG_EXIT,
3634 "restoring original video autoswitch mode\n");
3635 if (video_autosw_set(video_orig_autosw))
3636 printk(TPACPI_ERR "error while trying to restore original "
3637 "video autoswitch mode\n");
3640 static int video_outputsw_get(void)
3642 int status = 0;
3643 int i;
3645 switch (video_supported) {
3646 case TPACPI_VIDEO_570:
3647 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3648 TP_ACPI_VIDEO_570_PHSCMD))
3649 return -EIO;
3650 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3651 break;
3652 case TPACPI_VIDEO_770:
3653 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3654 return -EIO;
3655 if (i)
3656 status |= TP_ACPI_VIDEO_S_LCD;
3657 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3658 return -EIO;
3659 if (i)
3660 status |= TP_ACPI_VIDEO_S_CRT;
3661 break;
3662 case TPACPI_VIDEO_NEW:
3663 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3664 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3665 return -EIO;
3666 if (i)
3667 status |= TP_ACPI_VIDEO_S_CRT;
3669 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3670 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3671 return -EIO;
3672 if (i)
3673 status |= TP_ACPI_VIDEO_S_LCD;
3674 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3675 return -EIO;
3676 if (i)
3677 status |= TP_ACPI_VIDEO_S_DVI;
3678 break;
3679 default:
3680 return -ENOSYS;
3683 return status;
3686 static int video_outputsw_set(int status)
3688 int autosw;
3689 int res = 0;
3691 switch (video_supported) {
3692 case TPACPI_VIDEO_570:
3693 res = acpi_evalf(NULL, NULL,
3694 "\\_SB.PHS2", "vdd",
3695 TP_ACPI_VIDEO_570_PHS2CMD,
3696 status | TP_ACPI_VIDEO_570_PHS2SET);
3697 break;
3698 case TPACPI_VIDEO_770:
3699 autosw = video_autosw_get();
3700 if (autosw < 0)
3701 return autosw;
3703 res = video_autosw_set(1);
3704 if (res)
3705 return res;
3706 res = acpi_evalf(vid_handle, NULL,
3707 "ASWT", "vdd", status * 0x100, 0);
3708 if (!autosw && video_autosw_set(autosw)) {
3709 printk(TPACPI_ERR
3710 "video auto-switch left enabled due to error\n");
3711 return -EIO;
3713 break;
3714 case TPACPI_VIDEO_NEW:
3715 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3716 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3717 break;
3718 default:
3719 return -ENOSYS;
3722 return (res)? 0 : -EIO;
3725 static int video_autosw_get(void)
3727 int autosw = 0;
3729 switch (video_supported) {
3730 case TPACPI_VIDEO_570:
3731 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3732 return -EIO;
3733 break;
3734 case TPACPI_VIDEO_770:
3735 case TPACPI_VIDEO_NEW:
3736 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3737 return -EIO;
3738 break;
3739 default:
3740 return -ENOSYS;
3743 return autosw & 1;
3746 static int video_autosw_set(int enable)
3748 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3749 return -EIO;
3750 return 0;
3753 static int video_outputsw_cycle(void)
3755 int autosw = video_autosw_get();
3756 int res;
3758 if (autosw < 0)
3759 return autosw;
3761 switch (video_supported) {
3762 case TPACPI_VIDEO_570:
3763 res = video_autosw_set(1);
3764 if (res)
3765 return res;
3766 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3767 break;
3768 case TPACPI_VIDEO_770:
3769 case TPACPI_VIDEO_NEW:
3770 res = video_autosw_set(1);
3771 if (res)
3772 return res;
3773 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3774 break;
3775 default:
3776 return -ENOSYS;
3778 if (!autosw && video_autosw_set(autosw)) {
3779 printk(TPACPI_ERR
3780 "video auto-switch left enabled due to error\n");
3781 return -EIO;
3784 return (res)? 0 : -EIO;
3787 static int video_expand_toggle(void)
3789 switch (video_supported) {
3790 case TPACPI_VIDEO_570:
3791 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3792 0 : -EIO;
3793 case TPACPI_VIDEO_770:
3794 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3795 0 : -EIO;
3796 case TPACPI_VIDEO_NEW:
3797 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3798 0 : -EIO;
3799 default:
3800 return -ENOSYS;
3802 /* not reached */
3805 static int video_read(char *p)
3807 int status, autosw;
3808 int len = 0;
3810 if (video_supported == TPACPI_VIDEO_NONE) {
3811 len += sprintf(p + len, "status:\t\tnot supported\n");
3812 return len;
3815 status = video_outputsw_get();
3816 if (status < 0)
3817 return status;
3819 autosw = video_autosw_get();
3820 if (autosw < 0)
3821 return autosw;
3823 len += sprintf(p + len, "status:\t\tsupported\n");
3824 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3825 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3826 if (video_supported == TPACPI_VIDEO_NEW)
3827 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3828 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3829 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3830 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3831 if (video_supported == TPACPI_VIDEO_NEW)
3832 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3833 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3834 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3836 return len;
3839 static int video_write(char *buf)
3841 char *cmd;
3842 int enable, disable, status;
3843 int res;
3845 if (video_supported == TPACPI_VIDEO_NONE)
3846 return -ENODEV;
3848 enable = 0;
3849 disable = 0;
3851 while ((cmd = next_cmd(&buf))) {
3852 if (strlencmp(cmd, "lcd_enable") == 0) {
3853 enable |= TP_ACPI_VIDEO_S_LCD;
3854 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3855 disable |= TP_ACPI_VIDEO_S_LCD;
3856 } else if (strlencmp(cmd, "crt_enable") == 0) {
3857 enable |= TP_ACPI_VIDEO_S_CRT;
3858 } else if (strlencmp(cmd, "crt_disable") == 0) {
3859 disable |= TP_ACPI_VIDEO_S_CRT;
3860 } else if (video_supported == TPACPI_VIDEO_NEW &&
3861 strlencmp(cmd, "dvi_enable") == 0) {
3862 enable |= TP_ACPI_VIDEO_S_DVI;
3863 } else if (video_supported == TPACPI_VIDEO_NEW &&
3864 strlencmp(cmd, "dvi_disable") == 0) {
3865 disable |= TP_ACPI_VIDEO_S_DVI;
3866 } else if (strlencmp(cmd, "auto_enable") == 0) {
3867 res = video_autosw_set(1);
3868 if (res)
3869 return res;
3870 } else if (strlencmp(cmd, "auto_disable") == 0) {
3871 res = video_autosw_set(0);
3872 if (res)
3873 return res;
3874 } else if (strlencmp(cmd, "video_switch") == 0) {
3875 res = video_outputsw_cycle();
3876 if (res)
3877 return res;
3878 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3879 res = video_expand_toggle();
3880 if (res)
3881 return res;
3882 } else
3883 return -EINVAL;
3886 if (enable || disable) {
3887 status = video_outputsw_get();
3888 if (status < 0)
3889 return status;
3890 res = video_outputsw_set((status & ~disable) | enable);
3891 if (res)
3892 return res;
3895 return 0;
3898 static struct ibm_struct video_driver_data = {
3899 .name = "video",
3900 .read = video_read,
3901 .write = video_write,
3902 .exit = video_exit,
3905 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3907 /*************************************************************************
3908 * Light (thinklight) subdriver
3911 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3912 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3914 static int light_get_status(void)
3916 int status = 0;
3918 if (tp_features.light_status) {
3919 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3920 return -EIO;
3921 return (!!status);
3924 return -ENXIO;
3927 static int light_set_status(int status)
3929 int rc;
3931 if (tp_features.light) {
3932 if (cmos_handle) {
3933 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3934 (status)?
3935 TP_CMOS_THINKLIGHT_ON :
3936 TP_CMOS_THINKLIGHT_OFF);
3937 } else {
3938 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3939 (status)? 1 : 0);
3941 return (rc)? 0 : -EIO;
3944 return -ENXIO;
3947 static void light_set_status_worker(struct work_struct *work)
3949 struct tpacpi_led_classdev *data =
3950 container_of(work, struct tpacpi_led_classdev, work);
3952 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3953 light_set_status((data->new_brightness != LED_OFF));
3956 static void light_sysfs_set(struct led_classdev *led_cdev,
3957 enum led_brightness brightness)
3959 struct tpacpi_led_classdev *data =
3960 container_of(led_cdev,
3961 struct tpacpi_led_classdev,
3962 led_classdev);
3963 data->new_brightness = brightness;
3964 queue_work(tpacpi_wq, &data->work);
3967 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3969 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3972 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3973 .led_classdev = {
3974 .name = "tpacpi::thinklight",
3975 .brightness_set = &light_sysfs_set,
3976 .brightness_get = &light_sysfs_get,
3980 static int __init light_init(struct ibm_init_struct *iibm)
3982 int rc;
3984 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3986 TPACPI_ACPIHANDLE_INIT(ledb);
3987 TPACPI_ACPIHANDLE_INIT(lght);
3988 TPACPI_ACPIHANDLE_INIT(cmos);
3989 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3991 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3992 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3994 if (tp_features.light)
3995 /* light status not supported on
3996 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3997 tp_features.light_status =
3998 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4000 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4001 str_supported(tp_features.light),
4002 str_supported(tp_features.light_status));
4004 if (!tp_features.light)
4005 return 1;
4007 rc = led_classdev_register(&tpacpi_pdev->dev,
4008 &tpacpi_led_thinklight.led_classdev);
4010 if (rc < 0) {
4011 tp_features.light = 0;
4012 tp_features.light_status = 0;
4013 } else {
4014 rc = 0;
4017 return rc;
4020 static void light_exit(void)
4022 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4023 if (work_pending(&tpacpi_led_thinklight.work))
4024 flush_workqueue(tpacpi_wq);
4027 static int light_read(char *p)
4029 int len = 0;
4030 int status;
4032 if (!tp_features.light) {
4033 len += sprintf(p + len, "status:\t\tnot supported\n");
4034 } else if (!tp_features.light_status) {
4035 len += sprintf(p + len, "status:\t\tunknown\n");
4036 len += sprintf(p + len, "commands:\ton, off\n");
4037 } else {
4038 status = light_get_status();
4039 if (status < 0)
4040 return status;
4041 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4042 len += sprintf(p + len, "commands:\ton, off\n");
4045 return len;
4048 static int light_write(char *buf)
4050 char *cmd;
4051 int newstatus = 0;
4053 if (!tp_features.light)
4054 return -ENODEV;
4056 while ((cmd = next_cmd(&buf))) {
4057 if (strlencmp(cmd, "on") == 0) {
4058 newstatus = 1;
4059 } else if (strlencmp(cmd, "off") == 0) {
4060 newstatus = 0;
4061 } else
4062 return -EINVAL;
4065 return light_set_status(newstatus);
4068 static struct ibm_struct light_driver_data = {
4069 .name = "light",
4070 .read = light_read,
4071 .write = light_write,
4072 .exit = light_exit,
4075 /*************************************************************************
4076 * Dock subdriver
4079 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4081 static void dock_notify(struct ibm_struct *ibm, u32 event);
4082 static int dock_read(char *p);
4083 static int dock_write(char *buf);
4085 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4086 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4087 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4088 "\\_SB.PCI.ISA.SLCE", /* 570 */
4089 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4091 /* don't list other alternatives as we install a notify handler on the 570 */
4092 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4094 static const struct acpi_device_id ibm_pci_device_ids[] = {
4095 {PCI_ROOT_HID_STRING, 0},
4096 {"", 0},
4099 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4101 .notify = dock_notify,
4102 .handle = &dock_handle,
4103 .type = ACPI_SYSTEM_NOTIFY,
4106 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4107 * We just use it to get notifications of dock hotplug
4108 * in very old thinkpads */
4109 .hid = ibm_pci_device_ids,
4110 .notify = dock_notify,
4111 .handle = &pci_handle,
4112 .type = ACPI_SYSTEM_NOTIFY,
4116 static struct ibm_struct dock_driver_data[2] = {
4118 .name = "dock",
4119 .read = dock_read,
4120 .write = dock_write,
4121 .acpi = &ibm_dock_acpidriver[0],
4124 .name = "dock",
4125 .acpi = &ibm_dock_acpidriver[1],
4129 #define dock_docked() (_sta(dock_handle) & 1)
4131 static int __init dock_init(struct ibm_init_struct *iibm)
4133 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4135 TPACPI_ACPIHANDLE_INIT(dock);
4137 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4138 str_supported(dock_handle != NULL));
4140 return (dock_handle)? 0 : 1;
4143 static int __init dock_init2(struct ibm_init_struct *iibm)
4145 int dock2_needed;
4147 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4149 if (dock_driver_data[0].flags.acpi_driver_registered &&
4150 dock_driver_data[0].flags.acpi_notify_installed) {
4151 TPACPI_ACPIHANDLE_INIT(pci);
4152 dock2_needed = (pci_handle != NULL);
4153 vdbg_printk(TPACPI_DBG_INIT,
4154 "dock PCI handler for the TP 570 is %s\n",
4155 str_supported(dock2_needed));
4156 } else {
4157 vdbg_printk(TPACPI_DBG_INIT,
4158 "dock subdriver part 2 not required\n");
4159 dock2_needed = 0;
4162 return (dock2_needed)? 0 : 1;
4165 static void dock_notify(struct ibm_struct *ibm, u32 event)
4167 int docked = dock_docked();
4168 int pci = ibm->acpi->hid && ibm->acpi->device &&
4169 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4170 int data;
4172 if (event == 1 && !pci) /* 570 */
4173 data = 1; /* button */
4174 else if (event == 1 && pci) /* 570 */
4175 data = 3; /* dock */
4176 else if (event == 3 && docked)
4177 data = 1; /* button */
4178 else if (event == 3 && !docked)
4179 data = 2; /* undock */
4180 else if (event == 0 && docked)
4181 data = 3; /* dock */
4182 else {
4183 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4184 event, _sta(dock_handle));
4185 data = 0; /* unknown */
4187 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4188 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4189 ibm->acpi->device->dev.bus_id,
4190 event, data);
4193 static int dock_read(char *p)
4195 int len = 0;
4196 int docked = dock_docked();
4198 if (!dock_handle)
4199 len += sprintf(p + len, "status:\t\tnot supported\n");
4200 else if (!docked)
4201 len += sprintf(p + len, "status:\t\tundocked\n");
4202 else {
4203 len += sprintf(p + len, "status:\t\tdocked\n");
4204 len += sprintf(p + len, "commands:\tdock, undock\n");
4207 return len;
4210 static int dock_write(char *buf)
4212 char *cmd;
4214 if (!dock_docked())
4215 return -ENODEV;
4217 while ((cmd = next_cmd(&buf))) {
4218 if (strlencmp(cmd, "undock") == 0) {
4219 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4220 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4221 return -EIO;
4222 } else if (strlencmp(cmd, "dock") == 0) {
4223 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4224 return -EIO;
4225 } else
4226 return -EINVAL;
4229 return 0;
4232 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4234 /*************************************************************************
4235 * Bay subdriver
4238 #ifdef CONFIG_THINKPAD_ACPI_BAY
4240 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4241 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4242 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4243 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4244 ); /* A21e, R30, R31 */
4245 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4246 "_EJ0", /* all others */
4247 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4248 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4249 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4250 ); /* all others */
4251 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4252 "_EJ0", /* 770x */
4253 ); /* all others */
4255 static int __init bay_init(struct ibm_init_struct *iibm)
4257 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4259 TPACPI_ACPIHANDLE_INIT(bay);
4260 if (bay_handle)
4261 TPACPI_ACPIHANDLE_INIT(bay_ej);
4262 TPACPI_ACPIHANDLE_INIT(bay2);
4263 if (bay2_handle)
4264 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4266 tp_features.bay_status = bay_handle &&
4267 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4268 tp_features.bay_status2 = bay2_handle &&
4269 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4271 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4272 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4273 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4274 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4276 vdbg_printk(TPACPI_DBG_INIT,
4277 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4278 str_supported(tp_features.bay_status),
4279 str_supported(tp_features.bay_eject),
4280 str_supported(tp_features.bay_status2),
4281 str_supported(tp_features.bay_eject2));
4283 return (tp_features.bay_status || tp_features.bay_eject ||
4284 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4287 static void bay_notify(struct ibm_struct *ibm, u32 event)
4289 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4290 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4291 ibm->acpi->device->dev.bus_id,
4292 event, 0);
4295 #define bay_occupied(b) (_sta(b##_handle) & 1)
4297 static int bay_read(char *p)
4299 int len = 0;
4300 int occupied = bay_occupied(bay);
4301 int occupied2 = bay_occupied(bay2);
4302 int eject, eject2;
4304 len += sprintf(p + len, "status:\t\t%s\n",
4305 tp_features.bay_status ?
4306 (occupied ? "occupied" : "unoccupied") :
4307 "not supported");
4308 if (tp_features.bay_status2)
4309 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4310 "occupied" : "unoccupied");
4312 eject = tp_features.bay_eject && occupied;
4313 eject2 = tp_features.bay_eject2 && occupied2;
4315 if (eject && eject2)
4316 len += sprintf(p + len, "commands:\teject, eject2\n");
4317 else if (eject)
4318 len += sprintf(p + len, "commands:\teject\n");
4319 else if (eject2)
4320 len += sprintf(p + len, "commands:\teject2\n");
4322 return len;
4325 static int bay_write(char *buf)
4327 char *cmd;
4329 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4330 return -ENODEV;
4332 while ((cmd = next_cmd(&buf))) {
4333 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4334 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4335 return -EIO;
4336 } else if (tp_features.bay_eject2 &&
4337 strlencmp(cmd, "eject2") == 0) {
4338 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4339 return -EIO;
4340 } else
4341 return -EINVAL;
4344 return 0;
4347 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4348 .notify = bay_notify,
4349 .handle = &bay_handle,
4350 .type = ACPI_SYSTEM_NOTIFY,
4353 static struct ibm_struct bay_driver_data = {
4354 .name = "bay",
4355 .read = bay_read,
4356 .write = bay_write,
4357 .acpi = &ibm_bay_acpidriver,
4360 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4362 /*************************************************************************
4363 * CMOS subdriver
4366 /* sysfs cmos_command -------------------------------------------------- */
4367 static ssize_t cmos_command_store(struct device *dev,
4368 struct device_attribute *attr,
4369 const char *buf, size_t count)
4371 unsigned long cmos_cmd;
4372 int res;
4374 if (parse_strtoul(buf, 21, &cmos_cmd))
4375 return -EINVAL;
4377 res = issue_thinkpad_cmos_command(cmos_cmd);
4378 return (res)? res : count;
4381 static struct device_attribute dev_attr_cmos_command =
4382 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4384 /* --------------------------------------------------------------------- */
4386 static int __init cmos_init(struct ibm_init_struct *iibm)
4388 int res;
4390 vdbg_printk(TPACPI_DBG_INIT,
4391 "initializing cmos commands subdriver\n");
4393 TPACPI_ACPIHANDLE_INIT(cmos);
4395 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4396 str_supported(cmos_handle != NULL));
4398 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4399 if (res)
4400 return res;
4402 return (cmos_handle)? 0 : 1;
4405 static void cmos_exit(void)
4407 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4410 static int cmos_read(char *p)
4412 int len = 0;
4414 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4415 R30, R31, T20-22, X20-21 */
4416 if (!cmos_handle)
4417 len += sprintf(p + len, "status:\t\tnot supported\n");
4418 else {
4419 len += sprintf(p + len, "status:\t\tsupported\n");
4420 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4423 return len;
4426 static int cmos_write(char *buf)
4428 char *cmd;
4429 int cmos_cmd, res;
4431 while ((cmd = next_cmd(&buf))) {
4432 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4433 cmos_cmd >= 0 && cmos_cmd <= 21) {
4434 /* cmos_cmd set */
4435 } else
4436 return -EINVAL;
4438 res = issue_thinkpad_cmos_command(cmos_cmd);
4439 if (res)
4440 return res;
4443 return 0;
4446 static struct ibm_struct cmos_driver_data = {
4447 .name = "cmos",
4448 .read = cmos_read,
4449 .write = cmos_write,
4450 .exit = cmos_exit,
4453 /*************************************************************************
4454 * LED subdriver
4457 enum led_access_mode {
4458 TPACPI_LED_NONE = 0,
4459 TPACPI_LED_570, /* 570 */
4460 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4461 TPACPI_LED_NEW, /* all others */
4464 enum { /* For TPACPI_LED_OLD */
4465 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4466 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4467 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4470 enum led_status_t {
4471 TPACPI_LED_OFF = 0,
4472 TPACPI_LED_ON,
4473 TPACPI_LED_BLINK,
4476 static enum led_access_mode led_supported;
4478 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4479 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4480 /* T20-22, X20-21 */
4481 "LED", /* all others */
4482 ); /* R30, R31 */
4484 #define TPACPI_LED_NUMLEDS 8
4485 static struct tpacpi_led_classdev *tpacpi_leds;
4486 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4487 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4488 /* there's a limit of 19 chars + NULL before 2.6.26 */
4489 "tpacpi::power",
4490 "tpacpi:orange:batt",
4491 "tpacpi:green:batt",
4492 "tpacpi::dock_active",
4493 "tpacpi::bay_active",
4494 "tpacpi::dock_batt",
4495 "tpacpi::unknown_led",
4496 "tpacpi::standby",
4499 static int led_get_status(const unsigned int led)
4501 int status;
4502 enum led_status_t led_s;
4504 switch (led_supported) {
4505 case TPACPI_LED_570:
4506 if (!acpi_evalf(ec_handle,
4507 &status, "GLED", "dd", 1 << led))
4508 return -EIO;
4509 led_s = (status == 0)?
4510 TPACPI_LED_OFF :
4511 ((status == 1)?
4512 TPACPI_LED_ON :
4513 TPACPI_LED_BLINK);
4514 tpacpi_led_state_cache[led] = led_s;
4515 return led_s;
4516 default:
4517 return -ENXIO;
4520 /* not reached */
4523 static int led_set_status(const unsigned int led,
4524 const enum led_status_t ledstatus)
4526 /* off, on, blink. Index is led_status_t */
4527 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4528 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4530 int rc = 0;
4532 switch (led_supported) {
4533 case TPACPI_LED_570:
4534 /* 570 */
4535 if (led > 7)
4536 return -EINVAL;
4537 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4538 (1 << led), led_sled_arg1[ledstatus]))
4539 rc = -EIO;
4540 break;
4541 case TPACPI_LED_OLD:
4542 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4543 if (led > 7)
4544 return -EINVAL;
4545 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4546 if (rc >= 0)
4547 rc = ec_write(TPACPI_LED_EC_HLBL,
4548 (ledstatus == TPACPI_LED_BLINK) << led);
4549 if (rc >= 0)
4550 rc = ec_write(TPACPI_LED_EC_HLCL,
4551 (ledstatus != TPACPI_LED_OFF) << led);
4552 break;
4553 case TPACPI_LED_NEW:
4554 /* all others */
4555 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4556 led, led_led_arg1[ledstatus]))
4557 rc = -EIO;
4558 break;
4559 default:
4560 rc = -ENXIO;
4563 if (!rc)
4564 tpacpi_led_state_cache[led] = ledstatus;
4566 return rc;
4569 static void led_sysfs_set_status(unsigned int led,
4570 enum led_brightness brightness)
4572 led_set_status(led,
4573 (brightness == LED_OFF) ?
4574 TPACPI_LED_OFF :
4575 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4576 TPACPI_LED_BLINK : TPACPI_LED_ON);
4579 static void led_set_status_worker(struct work_struct *work)
4581 struct tpacpi_led_classdev *data =
4582 container_of(work, struct tpacpi_led_classdev, work);
4584 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4585 led_sysfs_set_status(data->led, data->new_brightness);
4588 static void led_sysfs_set(struct led_classdev *led_cdev,
4589 enum led_brightness brightness)
4591 struct tpacpi_led_classdev *data = container_of(led_cdev,
4592 struct tpacpi_led_classdev, led_classdev);
4594 data->new_brightness = brightness;
4595 queue_work(tpacpi_wq, &data->work);
4598 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4599 unsigned long *delay_on, unsigned long *delay_off)
4601 struct tpacpi_led_classdev *data = container_of(led_cdev,
4602 struct tpacpi_led_classdev, led_classdev);
4604 /* Can we choose the flash rate? */
4605 if (*delay_on == 0 && *delay_off == 0) {
4606 /* yes. set them to the hardware blink rate (1 Hz) */
4607 *delay_on = 500; /* ms */
4608 *delay_off = 500; /* ms */
4609 } else if ((*delay_on != 500) || (*delay_off != 500))
4610 return -EINVAL;
4612 data->new_brightness = TPACPI_LED_BLINK;
4613 queue_work(tpacpi_wq, &data->work);
4615 return 0;
4618 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4620 int rc;
4622 struct tpacpi_led_classdev *data = container_of(led_cdev,
4623 struct tpacpi_led_classdev, led_classdev);
4625 rc = led_get_status(data->led);
4627 if (rc == TPACPI_LED_OFF || rc < 0)
4628 rc = LED_OFF; /* no error handling in led class :( */
4629 else
4630 rc = LED_FULL;
4632 return rc;
4635 static void led_exit(void)
4637 unsigned int i;
4639 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4640 if (tpacpi_leds[i].led_classdev.name)
4641 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4644 kfree(tpacpi_leds);
4647 static int __init led_init(struct ibm_init_struct *iibm)
4649 unsigned int i;
4650 int rc;
4652 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4654 TPACPI_ACPIHANDLE_INIT(led);
4656 if (!led_handle)
4657 /* led not supported on R30, R31 */
4658 led_supported = TPACPI_LED_NONE;
4659 else if (strlencmp(led_path, "SLED") == 0)
4660 /* 570 */
4661 led_supported = TPACPI_LED_570;
4662 else if (strlencmp(led_path, "SYSL") == 0)
4663 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4664 led_supported = TPACPI_LED_OLD;
4665 else
4666 /* all others */
4667 led_supported = TPACPI_LED_NEW;
4669 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4670 str_supported(led_supported), led_supported);
4672 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4673 GFP_KERNEL);
4674 if (!tpacpi_leds) {
4675 printk(TPACPI_ERR "Out of memory for LED data\n");
4676 return -ENOMEM;
4679 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4680 tpacpi_leds[i].led = i;
4682 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4683 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4684 if (led_supported == TPACPI_LED_570)
4685 tpacpi_leds[i].led_classdev.brightness_get =
4686 &led_sysfs_get;
4688 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4690 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4692 rc = led_classdev_register(&tpacpi_pdev->dev,
4693 &tpacpi_leds[i].led_classdev);
4694 if (rc < 0) {
4695 tpacpi_leds[i].led_classdev.name = NULL;
4696 led_exit();
4697 return rc;
4701 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4704 #define str_led_status(s) \
4705 ((s) == TPACPI_LED_OFF ? "off" : \
4706 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4708 static int led_read(char *p)
4710 int len = 0;
4712 if (!led_supported) {
4713 len += sprintf(p + len, "status:\t\tnot supported\n");
4714 return len;
4716 len += sprintf(p + len, "status:\t\tsupported\n");
4718 if (led_supported == TPACPI_LED_570) {
4719 /* 570 */
4720 int i, status;
4721 for (i = 0; i < 8; i++) {
4722 status = led_get_status(i);
4723 if (status < 0)
4724 return -EIO;
4725 len += sprintf(p + len, "%d:\t\t%s\n",
4726 i, str_led_status(status));
4730 len += sprintf(p + len, "commands:\t"
4731 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4733 return len;
4736 static int led_write(char *buf)
4738 char *cmd;
4739 int led, rc;
4740 enum led_status_t s;
4742 if (!led_supported)
4743 return -ENODEV;
4745 while ((cmd = next_cmd(&buf))) {
4746 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4747 return -EINVAL;
4749 if (strstr(cmd, "off")) {
4750 s = TPACPI_LED_OFF;
4751 } else if (strstr(cmd, "on")) {
4752 s = TPACPI_LED_ON;
4753 } else if (strstr(cmd, "blink")) {
4754 s = TPACPI_LED_BLINK;
4755 } else {
4756 return -EINVAL;
4759 rc = led_set_status(led, s);
4760 if (rc < 0)
4761 return rc;
4764 return 0;
4767 static struct ibm_struct led_driver_data = {
4768 .name = "led",
4769 .read = led_read,
4770 .write = led_write,
4771 .exit = led_exit,
4774 /*************************************************************************
4775 * Beep subdriver
4778 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4780 static int __init beep_init(struct ibm_init_struct *iibm)
4782 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4784 TPACPI_ACPIHANDLE_INIT(beep);
4786 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4787 str_supported(beep_handle != NULL));
4789 return (beep_handle)? 0 : 1;
4792 static int beep_read(char *p)
4794 int len = 0;
4796 if (!beep_handle)
4797 len += sprintf(p + len, "status:\t\tnot supported\n");
4798 else {
4799 len += sprintf(p + len, "status:\t\tsupported\n");
4800 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4803 return len;
4806 static int beep_write(char *buf)
4808 char *cmd;
4809 int beep_cmd;
4811 if (!beep_handle)
4812 return -ENODEV;
4814 while ((cmd = next_cmd(&buf))) {
4815 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4816 beep_cmd >= 0 && beep_cmd <= 17) {
4817 /* beep_cmd set */
4818 } else
4819 return -EINVAL;
4820 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4821 return -EIO;
4824 return 0;
4827 static struct ibm_struct beep_driver_data = {
4828 .name = "beep",
4829 .read = beep_read,
4830 .write = beep_write,
4833 /*************************************************************************
4834 * Thermal subdriver
4837 enum thermal_access_mode {
4838 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4839 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4840 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4841 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4842 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4845 enum { /* TPACPI_THERMAL_TPEC_* */
4846 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4847 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4848 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4851 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4852 struct ibm_thermal_sensors_struct {
4853 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4856 static enum thermal_access_mode thermal_read_mode;
4858 /* idx is zero-based */
4859 static int thermal_get_sensor(int idx, s32 *value)
4861 int t;
4862 s8 tmp;
4863 char tmpi[5];
4865 t = TP_EC_THERMAL_TMP0;
4867 switch (thermal_read_mode) {
4868 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4869 case TPACPI_THERMAL_TPEC_16:
4870 if (idx >= 8 && idx <= 15) {
4871 t = TP_EC_THERMAL_TMP8;
4872 idx -= 8;
4874 /* fallthrough */
4875 #endif
4876 case TPACPI_THERMAL_TPEC_8:
4877 if (idx <= 7) {
4878 if (!acpi_ec_read(t + idx, &tmp))
4879 return -EIO;
4880 *value = tmp * 1000;
4881 return 0;
4883 break;
4885 case TPACPI_THERMAL_ACPI_UPDT:
4886 if (idx <= 7) {
4887 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4888 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4889 return -EIO;
4890 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4891 return -EIO;
4892 *value = (t - 2732) * 100;
4893 return 0;
4895 break;
4897 case TPACPI_THERMAL_ACPI_TMP07:
4898 if (idx <= 7) {
4899 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4900 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4901 return -EIO;
4902 if (t > 127 || t < -127)
4903 t = TP_EC_THERMAL_TMP_NA;
4904 *value = t * 1000;
4905 return 0;
4907 break;
4909 case TPACPI_THERMAL_NONE:
4910 default:
4911 return -ENOSYS;
4914 return -EINVAL;
4917 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4919 int res, i;
4920 int n;
4922 n = 8;
4923 i = 0;
4925 if (!s)
4926 return -EINVAL;
4928 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4929 n = 16;
4931 for (i = 0 ; i < n; i++) {
4932 res = thermal_get_sensor(i, &s->temp[i]);
4933 if (res)
4934 return res;
4937 return n;
4940 /* sysfs temp##_input -------------------------------------------------- */
4942 static ssize_t thermal_temp_input_show(struct device *dev,
4943 struct device_attribute *attr,
4944 char *buf)
4946 struct sensor_device_attribute *sensor_attr =
4947 to_sensor_dev_attr(attr);
4948 int idx = sensor_attr->index;
4949 s32 value;
4950 int res;
4952 res = thermal_get_sensor(idx, &value);
4953 if (res)
4954 return res;
4955 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4956 return -ENXIO;
4958 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4961 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4962 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4963 thermal_temp_input_show, NULL, _idxB)
4965 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4966 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4967 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4968 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4969 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4970 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4971 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4972 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4973 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4974 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4975 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4976 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4977 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4978 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4979 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4980 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4981 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4984 #define THERMAL_ATTRS(X) \
4985 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4987 static struct attribute *thermal_temp_input_attr[] = {
4988 THERMAL_ATTRS(8),
4989 THERMAL_ATTRS(9),
4990 THERMAL_ATTRS(10),
4991 THERMAL_ATTRS(11),
4992 THERMAL_ATTRS(12),
4993 THERMAL_ATTRS(13),
4994 THERMAL_ATTRS(14),
4995 THERMAL_ATTRS(15),
4996 THERMAL_ATTRS(0),
4997 THERMAL_ATTRS(1),
4998 THERMAL_ATTRS(2),
4999 THERMAL_ATTRS(3),
5000 THERMAL_ATTRS(4),
5001 THERMAL_ATTRS(5),
5002 THERMAL_ATTRS(6),
5003 THERMAL_ATTRS(7),
5004 NULL
5007 static const struct attribute_group thermal_temp_input16_group = {
5008 .attrs = thermal_temp_input_attr
5011 static const struct attribute_group thermal_temp_input8_group = {
5012 .attrs = &thermal_temp_input_attr[8]
5015 #undef THERMAL_SENSOR_ATTR_TEMP
5016 #undef THERMAL_ATTRS
5018 /* --------------------------------------------------------------------- */
5020 static int __init thermal_init(struct ibm_init_struct *iibm)
5022 u8 t, ta1, ta2;
5023 int i;
5024 int acpi_tmp7;
5025 int res;
5027 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5029 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5031 if (thinkpad_id.ec_model) {
5033 * Direct EC access mode: sensors at registers
5034 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5035 * non-implemented, thermal sensors return 0x80 when
5036 * not available
5039 ta1 = ta2 = 0;
5040 for (i = 0; i < 8; i++) {
5041 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5042 ta1 |= t;
5043 } else {
5044 ta1 = 0;
5045 break;
5047 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5048 ta2 |= t;
5049 } else {
5050 ta1 = 0;
5051 break;
5054 if (ta1 == 0) {
5055 /* This is sheer paranoia, but we handle it anyway */
5056 if (acpi_tmp7) {
5057 printk(TPACPI_ERR
5058 "ThinkPad ACPI EC access misbehaving, "
5059 "falling back to ACPI TMPx access "
5060 "mode\n");
5061 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5062 } else {
5063 printk(TPACPI_ERR
5064 "ThinkPad ACPI EC access misbehaving, "
5065 "disabling thermal sensors access\n");
5066 thermal_read_mode = TPACPI_THERMAL_NONE;
5068 } else {
5069 thermal_read_mode =
5070 (ta2 != 0) ?
5071 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5073 } else if (acpi_tmp7) {
5074 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5075 /* 600e/x, 770e, 770x */
5076 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5077 } else {
5078 /* Standard ACPI TMPx access, max 8 sensors */
5079 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5081 } else {
5082 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5083 thermal_read_mode = TPACPI_THERMAL_NONE;
5086 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5087 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5088 thermal_read_mode);
5090 switch (thermal_read_mode) {
5091 case TPACPI_THERMAL_TPEC_16:
5092 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5093 &thermal_temp_input16_group);
5094 if (res)
5095 return res;
5096 break;
5097 case TPACPI_THERMAL_TPEC_8:
5098 case TPACPI_THERMAL_ACPI_TMP07:
5099 case TPACPI_THERMAL_ACPI_UPDT:
5100 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5101 &thermal_temp_input8_group);
5102 if (res)
5103 return res;
5104 break;
5105 case TPACPI_THERMAL_NONE:
5106 default:
5107 return 1;
5110 return 0;
5113 static void thermal_exit(void)
5115 switch (thermal_read_mode) {
5116 case TPACPI_THERMAL_TPEC_16:
5117 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5118 &thermal_temp_input16_group);
5119 break;
5120 case TPACPI_THERMAL_TPEC_8:
5121 case TPACPI_THERMAL_ACPI_TMP07:
5122 case TPACPI_THERMAL_ACPI_UPDT:
5123 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5124 &thermal_temp_input16_group);
5125 break;
5126 case TPACPI_THERMAL_NONE:
5127 default:
5128 break;
5132 static int thermal_read(char *p)
5134 int len = 0;
5135 int n, i;
5136 struct ibm_thermal_sensors_struct t;
5138 n = thermal_get_sensors(&t);
5139 if (unlikely(n < 0))
5140 return n;
5142 len += sprintf(p + len, "temperatures:\t");
5144 if (n > 0) {
5145 for (i = 0; i < (n - 1); i++)
5146 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5147 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5148 } else
5149 len += sprintf(p + len, "not supported\n");
5151 return len;
5154 static struct ibm_struct thermal_driver_data = {
5155 .name = "thermal",
5156 .read = thermal_read,
5157 .exit = thermal_exit,
5160 /*************************************************************************
5161 * EC Dump subdriver
5164 static u8 ecdump_regs[256];
5166 static int ecdump_read(char *p)
5168 int len = 0;
5169 int i, j;
5170 u8 v;
5172 len += sprintf(p + len, "EC "
5173 " +00 +01 +02 +03 +04 +05 +06 +07"
5174 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5175 for (i = 0; i < 256; i += 16) {
5176 len += sprintf(p + len, "EC 0x%02x:", i);
5177 for (j = 0; j < 16; j++) {
5178 if (!acpi_ec_read(i + j, &v))
5179 break;
5180 if (v != ecdump_regs[i + j])
5181 len += sprintf(p + len, " *%02x", v);
5182 else
5183 len += sprintf(p + len, " %02x", v);
5184 ecdump_regs[i + j] = v;
5186 len += sprintf(p + len, "\n");
5187 if (j != 16)
5188 break;
5191 /* These are way too dangerous to advertise openly... */
5192 #if 0
5193 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5194 " (<offset> is 00-ff, <value> is 00-ff)\n");
5195 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5196 " (<offset> is 00-ff, <value> is 0-255)\n");
5197 #endif
5198 return len;
5201 static int ecdump_write(char *buf)
5203 char *cmd;
5204 int i, v;
5206 while ((cmd = next_cmd(&buf))) {
5207 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5208 /* i and v set */
5209 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5210 /* i and v set */
5211 } else
5212 return -EINVAL;
5213 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5214 if (!acpi_ec_write(i, v))
5215 return -EIO;
5216 } else
5217 return -EINVAL;
5220 return 0;
5223 static struct ibm_struct ecdump_driver_data = {
5224 .name = "ecdump",
5225 .read = ecdump_read,
5226 .write = ecdump_write,
5227 .flags.experimental = 1,
5230 /*************************************************************************
5231 * Backlight/brightness subdriver
5234 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5236 enum {
5237 TP_EC_BACKLIGHT = 0x31,
5239 /* TP_EC_BACKLIGHT bitmasks */
5240 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5241 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5242 TP_EC_BACKLIGHT_MAPSW = 0x20,
5245 static struct backlight_device *ibm_backlight_device;
5246 static int brightness_mode;
5247 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5249 static struct mutex brightness_mutex;
5252 * ThinkPads can read brightness from two places: EC 0x31, or
5253 * CMOS NVRAM byte 0x5E, bits 0-3.
5255 * EC 0x31 has the following layout
5256 * Bit 7: unknown function
5257 * Bit 6: unknown function
5258 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5259 * Bit 4: must be set to zero to avoid problems
5260 * Bit 3-0: backlight brightness level
5262 * brightness_get_raw returns status data in the EC 0x31 layout
5264 static int brightness_get_raw(int *status)
5266 u8 lec = 0, lcmos = 0, level = 0;
5268 if (brightness_mode & 1) {
5269 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5270 return -EIO;
5271 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5273 if (brightness_mode & 2) {
5274 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5275 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5276 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5277 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5278 level = lcmos;
5281 if (brightness_mode == 3) {
5282 *status = lec; /* Prefer EC, CMOS is just a backing store */
5283 lec &= TP_EC_BACKLIGHT_LVLMSK;
5284 if (lec == lcmos)
5285 tp_warned.bright_cmos_ec_unsync = 0;
5286 else {
5287 if (!tp_warned.bright_cmos_ec_unsync) {
5288 printk(TPACPI_ERR
5289 "CMOS NVRAM (%u) and EC (%u) do not "
5290 "agree on display brightness level\n",
5291 (unsigned int) lcmos,
5292 (unsigned int) lec);
5293 tp_warned.bright_cmos_ec_unsync = 1;
5295 return -EIO;
5297 } else {
5298 *status = level;
5301 return 0;
5304 /* May return EINTR which can always be mapped to ERESTARTSYS */
5305 static int brightness_set(int value)
5307 int cmos_cmd, inc, i, res;
5308 int current_value;
5309 int command_bits;
5311 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5312 value < 0)
5313 return -EINVAL;
5315 res = mutex_lock_killable(&brightness_mutex);
5316 if (res < 0)
5317 return res;
5319 res = brightness_get_raw(&current_value);
5320 if (res < 0)
5321 goto errout;
5323 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5324 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5326 cmos_cmd = value > current_value ?
5327 TP_CMOS_BRIGHTNESS_UP :
5328 TP_CMOS_BRIGHTNESS_DOWN;
5329 inc = (value > current_value)? 1 : -1;
5331 res = 0;
5332 for (i = current_value; i != value; i += inc) {
5333 if ((brightness_mode & 2) &&
5334 issue_thinkpad_cmos_command(cmos_cmd)) {
5335 res = -EIO;
5336 goto errout;
5338 if ((brightness_mode & 1) &&
5339 !acpi_ec_write(TP_EC_BACKLIGHT,
5340 (i + inc) | command_bits)) {
5341 res = -EIO;
5342 goto errout;;
5346 errout:
5347 mutex_unlock(&brightness_mutex);
5348 return res;
5351 /* sysfs backlight class ----------------------------------------------- */
5353 static int brightness_update_status(struct backlight_device *bd)
5355 /* it is the backlight class's job (caller) to handle
5356 * EINTR and other errors properly */
5357 return brightness_set(
5358 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5359 bd->props.power == FB_BLANK_UNBLANK) ?
5360 bd->props.brightness : 0);
5363 static int brightness_get(struct backlight_device *bd)
5365 int status, res;
5367 res = brightness_get_raw(&status);
5368 if (res < 0)
5369 return 0; /* FIXME: teach backlight about error handling */
5371 return status & TP_EC_BACKLIGHT_LVLMSK;
5374 static struct backlight_ops ibm_backlight_data = {
5375 .get_brightness = brightness_get,
5376 .update_status = brightness_update_status,
5379 /* --------------------------------------------------------------------- */
5381 static int __init brightness_init(struct ibm_init_struct *iibm)
5383 int b;
5385 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5387 mutex_init(&brightness_mutex);
5390 * We always attempt to detect acpi support, so as to switch
5391 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5392 * going to publish a backlight interface
5394 b = tpacpi_check_std_acpi_brightness_support();
5395 if (b > 0) {
5396 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5397 printk(TPACPI_NOTICE
5398 "Lenovo BIOS switched to ACPI backlight "
5399 "control mode\n");
5401 if (brightness_enable > 1) {
5402 printk(TPACPI_NOTICE
5403 "standard ACPI backlight interface "
5404 "available, not loading native one...\n");
5405 return 1;
5409 if (!brightness_enable) {
5410 dbg_printk(TPACPI_DBG_INIT,
5411 "brightness support disabled by "
5412 "module parameter\n");
5413 return 1;
5416 if (b > 16) {
5417 printk(TPACPI_ERR
5418 "Unsupported brightness interface, "
5419 "please contact %s\n", TPACPI_MAIL);
5420 return 1;
5422 if (b == 16)
5423 tp_features.bright_16levels = 1;
5425 if (!brightness_mode) {
5426 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5427 brightness_mode = 2;
5428 else
5429 brightness_mode = 3;
5431 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5432 brightness_mode);
5435 if (brightness_mode > 3)
5436 return -EINVAL;
5438 if (brightness_get_raw(&b) < 0)
5439 return 1;
5441 if (tp_features.bright_16levels)
5442 printk(TPACPI_INFO
5443 "detected a 16-level brightness capable ThinkPad\n");
5445 ibm_backlight_device = backlight_device_register(
5446 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5447 &ibm_backlight_data);
5448 if (IS_ERR(ibm_backlight_device)) {
5449 printk(TPACPI_ERR "Could not register backlight device\n");
5450 return PTR_ERR(ibm_backlight_device);
5452 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5454 ibm_backlight_device->props.max_brightness =
5455 (tp_features.bright_16levels)? 15 : 7;
5456 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5457 backlight_update_status(ibm_backlight_device);
5459 return 0;
5462 static void brightness_exit(void)
5464 if (ibm_backlight_device) {
5465 vdbg_printk(TPACPI_DBG_EXIT,
5466 "calling backlight_device_unregister()\n");
5467 backlight_device_unregister(ibm_backlight_device);
5471 static int brightness_read(char *p)
5473 int len = 0;
5474 int level;
5476 level = brightness_get(NULL);
5477 if (level < 0) {
5478 len += sprintf(p + len, "level:\t\tunreadable\n");
5479 } else {
5480 len += sprintf(p + len, "level:\t\t%d\n", level);
5481 len += sprintf(p + len, "commands:\tup, down\n");
5482 len += sprintf(p + len, "commands:\tlevel <level>"
5483 " (<level> is 0-%d)\n",
5484 (tp_features.bright_16levels) ? 15 : 7);
5487 return len;
5490 static int brightness_write(char *buf)
5492 int level;
5493 int rc;
5494 char *cmd;
5495 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5497 level = brightness_get(NULL);
5498 if (level < 0)
5499 return level;
5501 while ((cmd = next_cmd(&buf))) {
5502 if (strlencmp(cmd, "up") == 0) {
5503 if (level < max_level)
5504 level++;
5505 } else if (strlencmp(cmd, "down") == 0) {
5506 if (level > 0)
5507 level--;
5508 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5509 level >= 0 && level <= max_level) {
5510 /* new level set */
5511 } else
5512 return -EINVAL;
5516 * Now we know what the final level should be, so we try to set it.
5517 * Doing it this way makes the syscall restartable in case of EINTR
5519 rc = brightness_set(level);
5520 return (rc == -EINTR)? ERESTARTSYS : rc;
5523 static struct ibm_struct brightness_driver_data = {
5524 .name = "brightness",
5525 .read = brightness_read,
5526 .write = brightness_write,
5527 .exit = brightness_exit,
5530 /*************************************************************************
5531 * Volume subdriver
5534 static int volume_offset = 0x30;
5536 static int volume_read(char *p)
5538 int len = 0;
5539 u8 level;
5541 if (!acpi_ec_read(volume_offset, &level)) {
5542 len += sprintf(p + len, "level:\t\tunreadable\n");
5543 } else {
5544 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5545 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5546 len += sprintf(p + len, "commands:\tup, down, mute\n");
5547 len += sprintf(p + len, "commands:\tlevel <level>"
5548 " (<level> is 0-15)\n");
5551 return len;
5554 static int volume_write(char *buf)
5556 int cmos_cmd, inc, i;
5557 u8 level, mute;
5558 int new_level, new_mute;
5559 char *cmd;
5561 while ((cmd = next_cmd(&buf))) {
5562 if (!acpi_ec_read(volume_offset, &level))
5563 return -EIO;
5564 new_mute = mute = level & 0x40;
5565 new_level = level = level & 0xf;
5567 if (strlencmp(cmd, "up") == 0) {
5568 if (mute)
5569 new_mute = 0;
5570 else
5571 new_level = level == 15 ? 15 : level + 1;
5572 } else if (strlencmp(cmd, "down") == 0) {
5573 if (mute)
5574 new_mute = 0;
5575 else
5576 new_level = level == 0 ? 0 : level - 1;
5577 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5578 new_level >= 0 && new_level <= 15) {
5579 /* new_level set */
5580 } else if (strlencmp(cmd, "mute") == 0) {
5581 new_mute = 0x40;
5582 } else
5583 return -EINVAL;
5585 if (new_level != level) {
5586 /* mute doesn't change */
5588 cmos_cmd = (new_level > level) ?
5589 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5590 inc = new_level > level ? 1 : -1;
5592 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5593 !acpi_ec_write(volume_offset, level)))
5594 return -EIO;
5596 for (i = level; i != new_level; i += inc)
5597 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5598 !acpi_ec_write(volume_offset, i + inc))
5599 return -EIO;
5601 if (mute &&
5602 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5603 !acpi_ec_write(volume_offset, new_level + mute))) {
5604 return -EIO;
5608 if (new_mute != mute) {
5609 /* level doesn't change */
5611 cmos_cmd = (new_mute) ?
5612 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5614 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5615 !acpi_ec_write(volume_offset, level + new_mute))
5616 return -EIO;
5620 return 0;
5623 static struct ibm_struct volume_driver_data = {
5624 .name = "volume",
5625 .read = volume_read,
5626 .write = volume_write,
5629 /*************************************************************************
5630 * Fan subdriver
5634 * FAN ACCESS MODES
5636 * TPACPI_FAN_RD_ACPI_GFAN:
5637 * ACPI GFAN method: returns fan level
5639 * see TPACPI_FAN_WR_ACPI_SFAN
5640 * EC 0x2f (HFSP) not available if GFAN exists
5642 * TPACPI_FAN_WR_ACPI_SFAN:
5643 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5645 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5646 * it for writing.
5648 * TPACPI_FAN_WR_TPEC:
5649 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5650 * Supported on almost all ThinkPads
5652 * Fan speed changes of any sort (including those caused by the
5653 * disengaged mode) are usually done slowly by the firmware as the
5654 * maximum ammount of fan duty cycle change per second seems to be
5655 * limited.
5657 * Reading is not available if GFAN exists.
5658 * Writing is not available if SFAN exists.
5660 * Bits
5661 * 7 automatic mode engaged;
5662 * (default operation mode of the ThinkPad)
5663 * fan level is ignored in this mode.
5664 * 6 full speed mode (takes precedence over bit 7);
5665 * not available on all thinkpads. May disable
5666 * the tachometer while the fan controller ramps up
5667 * the speed (which can take up to a few *minutes*).
5668 * Speeds up fan to 100% duty-cycle, which is far above
5669 * the standard RPM levels. It is not impossible that
5670 * it could cause hardware damage.
5671 * 5-3 unused in some models. Extra bits for fan level
5672 * in others, but still useless as all values above
5673 * 7 map to the same speed as level 7 in these models.
5674 * 2-0 fan level (0..7 usually)
5675 * 0x00 = stop
5676 * 0x07 = max (set when temperatures critical)
5677 * Some ThinkPads may have other levels, see
5678 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5680 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5681 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5682 * does so, its initial value is meaningless (0x07).
5684 * For firmware bugs, refer to:
5685 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5687 * ----
5689 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5690 * Main fan tachometer reading (in RPM)
5692 * This register is present on all ThinkPads with a new-style EC, and
5693 * it is known not to be present on the A21m/e, and T22, as there is
5694 * something else in offset 0x84 according to the ACPI DSDT. Other
5695 * ThinkPads from this same time period (and earlier) probably lack the
5696 * tachometer as well.
5698 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5699 * was never fixed by IBM to report the EC firmware version string
5700 * probably support the tachometer (like the early X models), so
5701 * detecting it is quite hard. We need more data to know for sure.
5703 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5704 * might result.
5706 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5707 * mode.
5709 * For firmware bugs, refer to:
5710 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5712 * TPACPI_FAN_WR_ACPI_FANS:
5713 * ThinkPad X31, X40, X41. Not available in the X60.
5715 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5716 * high speed. ACPI DSDT seems to map these three speeds to levels
5717 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5718 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5720 * The speeds are stored on handles
5721 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5723 * There are three default speed sets, acessible as handles:
5724 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5726 * ACPI DSDT switches which set is in use depending on various
5727 * factors.
5729 * TPACPI_FAN_WR_TPEC is also available and should be used to
5730 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5731 * but the ACPI tables just mention level 7.
5734 enum { /* Fan control constants */
5735 fan_status_offset = 0x2f, /* EC register 0x2f */
5736 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5737 * 0x84 must be read before 0x85 */
5739 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5740 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5742 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5745 enum fan_status_access_mode {
5746 TPACPI_FAN_NONE = 0, /* No fan status or control */
5747 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5748 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5751 enum fan_control_access_mode {
5752 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5753 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5754 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5755 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5758 enum fan_control_commands {
5759 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5760 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5761 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5762 * and also watchdog cmd */
5765 static int fan_control_allowed;
5767 static enum fan_status_access_mode fan_status_access_mode;
5768 static enum fan_control_access_mode fan_control_access_mode;
5769 static enum fan_control_commands fan_control_commands;
5771 static u8 fan_control_initial_status;
5772 static u8 fan_control_desired_level;
5773 static int fan_watchdog_maxinterval;
5775 static struct mutex fan_mutex;
5777 static void fan_watchdog_fire(struct work_struct *ignored);
5778 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5780 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5781 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5782 "\\FSPD", /* 600e/x, 770e, 770x */
5783 ); /* all others */
5784 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5785 "JFNS", /* 770x-JL */
5786 ); /* all others */
5789 * Call with fan_mutex held
5791 static void fan_update_desired_level(u8 status)
5793 if ((status &
5794 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5795 if (status > 7)
5796 fan_control_desired_level = 7;
5797 else
5798 fan_control_desired_level = status;
5802 static int fan_get_status(u8 *status)
5804 u8 s;
5806 /* TODO:
5807 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5809 switch (fan_status_access_mode) {
5810 case TPACPI_FAN_RD_ACPI_GFAN:
5811 /* 570, 600e/x, 770e, 770x */
5813 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5814 return -EIO;
5816 if (likely(status))
5817 *status = s & 0x07;
5819 break;
5821 case TPACPI_FAN_RD_TPEC:
5822 /* all except 570, 600e/x, 770e, 770x */
5823 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5824 return -EIO;
5826 if (likely(status))
5827 *status = s;
5829 break;
5831 default:
5832 return -ENXIO;
5835 return 0;
5838 static int fan_get_status_safe(u8 *status)
5840 int rc;
5841 u8 s;
5843 if (mutex_lock_killable(&fan_mutex))
5844 return -ERESTARTSYS;
5845 rc = fan_get_status(&s);
5846 if (!rc)
5847 fan_update_desired_level(s);
5848 mutex_unlock(&fan_mutex);
5850 if (status)
5851 *status = s;
5853 return rc;
5856 static int fan_get_speed(unsigned int *speed)
5858 u8 hi, lo;
5860 switch (fan_status_access_mode) {
5861 case TPACPI_FAN_RD_TPEC:
5862 /* all except 570, 600e/x, 770e, 770x */
5863 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5864 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5865 return -EIO;
5867 if (likely(speed))
5868 *speed = (hi << 8) | lo;
5870 break;
5872 default:
5873 return -ENXIO;
5876 return 0;
5879 static int fan_set_level(int level)
5881 if (!fan_control_allowed)
5882 return -EPERM;
5884 switch (fan_control_access_mode) {
5885 case TPACPI_FAN_WR_ACPI_SFAN:
5886 if (level >= 0 && level <= 7) {
5887 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5888 return -EIO;
5889 } else
5890 return -EINVAL;
5891 break;
5893 case TPACPI_FAN_WR_ACPI_FANS:
5894 case TPACPI_FAN_WR_TPEC:
5895 if ((level != TP_EC_FAN_AUTO) &&
5896 (level != TP_EC_FAN_FULLSPEED) &&
5897 ((level < 0) || (level > 7)))
5898 return -EINVAL;
5900 /* safety net should the EC not support AUTO
5901 * or FULLSPEED mode bits and just ignore them */
5902 if (level & TP_EC_FAN_FULLSPEED)
5903 level |= 7; /* safety min speed 7 */
5904 else if (level & TP_EC_FAN_AUTO)
5905 level |= 4; /* safety min speed 4 */
5907 if (!acpi_ec_write(fan_status_offset, level))
5908 return -EIO;
5909 else
5910 tp_features.fan_ctrl_status_undef = 0;
5911 break;
5913 default:
5914 return -ENXIO;
5916 return 0;
5919 static int fan_set_level_safe(int level)
5921 int rc;
5923 if (!fan_control_allowed)
5924 return -EPERM;
5926 if (mutex_lock_killable(&fan_mutex))
5927 return -ERESTARTSYS;
5929 if (level == TPACPI_FAN_LAST_LEVEL)
5930 level = fan_control_desired_level;
5932 rc = fan_set_level(level);
5933 if (!rc)
5934 fan_update_desired_level(level);
5936 mutex_unlock(&fan_mutex);
5937 return rc;
5940 static int fan_set_enable(void)
5942 u8 s;
5943 int rc;
5945 if (!fan_control_allowed)
5946 return -EPERM;
5948 if (mutex_lock_killable(&fan_mutex))
5949 return -ERESTARTSYS;
5951 switch (fan_control_access_mode) {
5952 case TPACPI_FAN_WR_ACPI_FANS:
5953 case TPACPI_FAN_WR_TPEC:
5954 rc = fan_get_status(&s);
5955 if (rc < 0)
5956 break;
5958 /* Don't go out of emergency fan mode */
5959 if (s != 7) {
5960 s &= 0x07;
5961 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5964 if (!acpi_ec_write(fan_status_offset, s))
5965 rc = -EIO;
5966 else {
5967 tp_features.fan_ctrl_status_undef = 0;
5968 rc = 0;
5970 break;
5972 case TPACPI_FAN_WR_ACPI_SFAN:
5973 rc = fan_get_status(&s);
5974 if (rc < 0)
5975 break;
5977 s &= 0x07;
5979 /* Set fan to at least level 4 */
5980 s |= 4;
5982 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5983 rc = -EIO;
5984 else
5985 rc = 0;
5986 break;
5988 default:
5989 rc = -ENXIO;
5992 mutex_unlock(&fan_mutex);
5993 return rc;
5996 static int fan_set_disable(void)
5998 int rc;
6000 if (!fan_control_allowed)
6001 return -EPERM;
6003 if (mutex_lock_killable(&fan_mutex))
6004 return -ERESTARTSYS;
6006 rc = 0;
6007 switch (fan_control_access_mode) {
6008 case TPACPI_FAN_WR_ACPI_FANS:
6009 case TPACPI_FAN_WR_TPEC:
6010 if (!acpi_ec_write(fan_status_offset, 0x00))
6011 rc = -EIO;
6012 else {
6013 fan_control_desired_level = 0;
6014 tp_features.fan_ctrl_status_undef = 0;
6016 break;
6018 case TPACPI_FAN_WR_ACPI_SFAN:
6019 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6020 rc = -EIO;
6021 else
6022 fan_control_desired_level = 0;
6023 break;
6025 default:
6026 rc = -ENXIO;
6030 mutex_unlock(&fan_mutex);
6031 return rc;
6034 static int fan_set_speed(int speed)
6036 int rc;
6038 if (!fan_control_allowed)
6039 return -EPERM;
6041 if (mutex_lock_killable(&fan_mutex))
6042 return -ERESTARTSYS;
6044 rc = 0;
6045 switch (fan_control_access_mode) {
6046 case TPACPI_FAN_WR_ACPI_FANS:
6047 if (speed >= 0 && speed <= 65535) {
6048 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6049 speed, speed, speed))
6050 rc = -EIO;
6051 } else
6052 rc = -EINVAL;
6053 break;
6055 default:
6056 rc = -ENXIO;
6059 mutex_unlock(&fan_mutex);
6060 return rc;
6063 static void fan_watchdog_reset(void)
6065 static int fan_watchdog_active;
6067 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6068 return;
6070 if (fan_watchdog_active)
6071 cancel_delayed_work(&fan_watchdog_task);
6073 if (fan_watchdog_maxinterval > 0 &&
6074 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6075 fan_watchdog_active = 1;
6076 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6077 msecs_to_jiffies(fan_watchdog_maxinterval
6078 * 1000))) {
6079 printk(TPACPI_ERR
6080 "failed to queue the fan watchdog, "
6081 "watchdog will not trigger\n");
6083 } else
6084 fan_watchdog_active = 0;
6087 static void fan_watchdog_fire(struct work_struct *ignored)
6089 int rc;
6091 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6092 return;
6094 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6095 rc = fan_set_enable();
6096 if (rc < 0) {
6097 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6098 "will try again later...\n", -rc);
6099 /* reschedule for later */
6100 fan_watchdog_reset();
6105 * SYSFS fan layout: hwmon compatible (device)
6107 * pwm*_enable:
6108 * 0: "disengaged" mode
6109 * 1: manual mode
6110 * 2: native EC "auto" mode (recommended, hardware default)
6112 * pwm*: set speed in manual mode, ignored otherwise.
6113 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6114 * interpolation.
6116 * fan*_input: tachometer reading, RPM
6119 * SYSFS fan layout: extensions
6121 * fan_watchdog (driver):
6122 * fan watchdog interval in seconds, 0 disables (default), max 120
6125 /* sysfs fan pwm1_enable ----------------------------------------------- */
6126 static ssize_t fan_pwm1_enable_show(struct device *dev,
6127 struct device_attribute *attr,
6128 char *buf)
6130 int res, mode;
6131 u8 status;
6133 res = fan_get_status_safe(&status);
6134 if (res)
6135 return res;
6137 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6138 if (status != fan_control_initial_status) {
6139 tp_features.fan_ctrl_status_undef = 0;
6140 } else {
6141 /* Return most likely status. In fact, it
6142 * might be the only possible status */
6143 status = TP_EC_FAN_AUTO;
6147 if (status & TP_EC_FAN_FULLSPEED) {
6148 mode = 0;
6149 } else if (status & TP_EC_FAN_AUTO) {
6150 mode = 2;
6151 } else
6152 mode = 1;
6154 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6157 static ssize_t fan_pwm1_enable_store(struct device *dev,
6158 struct device_attribute *attr,
6159 const char *buf, size_t count)
6161 unsigned long t;
6162 int res, level;
6164 if (parse_strtoul(buf, 2, &t))
6165 return -EINVAL;
6167 switch (t) {
6168 case 0:
6169 level = TP_EC_FAN_FULLSPEED;
6170 break;
6171 case 1:
6172 level = TPACPI_FAN_LAST_LEVEL;
6173 break;
6174 case 2:
6175 level = TP_EC_FAN_AUTO;
6176 break;
6177 case 3:
6178 /* reserved for software-controlled auto mode */
6179 return -ENOSYS;
6180 default:
6181 return -EINVAL;
6184 res = fan_set_level_safe(level);
6185 if (res == -ENXIO)
6186 return -EINVAL;
6187 else if (res < 0)
6188 return res;
6190 fan_watchdog_reset();
6192 return count;
6195 static struct device_attribute dev_attr_fan_pwm1_enable =
6196 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6197 fan_pwm1_enable_show, fan_pwm1_enable_store);
6199 /* sysfs fan pwm1 ------------------------------------------------------ */
6200 static ssize_t fan_pwm1_show(struct device *dev,
6201 struct device_attribute *attr,
6202 char *buf)
6204 int res;
6205 u8 status;
6207 res = fan_get_status_safe(&status);
6208 if (res)
6209 return res;
6211 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6212 if (status != fan_control_initial_status) {
6213 tp_features.fan_ctrl_status_undef = 0;
6214 } else {
6215 status = TP_EC_FAN_AUTO;
6219 if ((status &
6220 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6221 status = fan_control_desired_level;
6223 if (status > 7)
6224 status = 7;
6226 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6229 static ssize_t fan_pwm1_store(struct device *dev,
6230 struct device_attribute *attr,
6231 const char *buf, size_t count)
6233 unsigned long s;
6234 int rc;
6235 u8 status, newlevel;
6237 if (parse_strtoul(buf, 255, &s))
6238 return -EINVAL;
6240 /* scale down from 0-255 to 0-7 */
6241 newlevel = (s >> 5) & 0x07;
6243 if (mutex_lock_killable(&fan_mutex))
6244 return -ERESTARTSYS;
6246 rc = fan_get_status(&status);
6247 if (!rc && (status &
6248 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6249 rc = fan_set_level(newlevel);
6250 if (rc == -ENXIO)
6251 rc = -EINVAL;
6252 else if (!rc) {
6253 fan_update_desired_level(newlevel);
6254 fan_watchdog_reset();
6258 mutex_unlock(&fan_mutex);
6259 return (rc)? rc : count;
6262 static struct device_attribute dev_attr_fan_pwm1 =
6263 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6264 fan_pwm1_show, fan_pwm1_store);
6266 /* sysfs fan fan1_input ------------------------------------------------ */
6267 static ssize_t fan_fan1_input_show(struct device *dev,
6268 struct device_attribute *attr,
6269 char *buf)
6271 int res;
6272 unsigned int speed;
6274 res = fan_get_speed(&speed);
6275 if (res < 0)
6276 return res;
6278 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6281 static struct device_attribute dev_attr_fan_fan1_input =
6282 __ATTR(fan1_input, S_IRUGO,
6283 fan_fan1_input_show, NULL);
6285 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6286 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6287 char *buf)
6289 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6292 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6293 const char *buf, size_t count)
6295 unsigned long t;
6297 if (parse_strtoul(buf, 120, &t))
6298 return -EINVAL;
6300 if (!fan_control_allowed)
6301 return -EPERM;
6303 fan_watchdog_maxinterval = t;
6304 fan_watchdog_reset();
6306 return count;
6309 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6310 fan_fan_watchdog_show, fan_fan_watchdog_store);
6312 /* --------------------------------------------------------------------- */
6313 static struct attribute *fan_attributes[] = {
6314 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6315 &dev_attr_fan_fan1_input.attr,
6316 NULL
6319 static const struct attribute_group fan_attr_group = {
6320 .attrs = fan_attributes,
6323 static int __init fan_init(struct ibm_init_struct *iibm)
6325 int rc;
6327 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6329 mutex_init(&fan_mutex);
6330 fan_status_access_mode = TPACPI_FAN_NONE;
6331 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6332 fan_control_commands = 0;
6333 fan_watchdog_maxinterval = 0;
6334 tp_features.fan_ctrl_status_undef = 0;
6335 fan_control_desired_level = 7;
6337 TPACPI_ACPIHANDLE_INIT(fans);
6338 TPACPI_ACPIHANDLE_INIT(gfan);
6339 TPACPI_ACPIHANDLE_INIT(sfan);
6341 if (gfan_handle) {
6342 /* 570, 600e/x, 770e, 770x */
6343 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6344 } else {
6345 /* all other ThinkPads: note that even old-style
6346 * ThinkPad ECs supports the fan control register */
6347 if (likely(acpi_ec_read(fan_status_offset,
6348 &fan_control_initial_status))) {
6349 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6351 /* In some ThinkPads, neither the EC nor the ACPI
6352 * DSDT initialize the fan status, and it ends up
6353 * being set to 0x07 when it *could* be either
6354 * 0x07 or 0x80.
6356 * Enable for TP-1Y (T43), TP-78 (R51e),
6357 * TP-76 (R52), TP-70 (T43, R52), which are known
6358 * to be buggy. */
6359 if (fan_control_initial_status == 0x07) {
6360 switch (thinkpad_id.ec_model) {
6361 case 0x5931: /* TP-1Y */
6362 case 0x3837: /* TP-78 */
6363 case 0x3637: /* TP-76 */
6364 case 0x3037: /* TP-70 */
6365 printk(TPACPI_NOTICE
6366 "fan_init: initial fan status "
6367 "is unknown, assuming it is "
6368 "in auto mode\n");
6369 tp_features.fan_ctrl_status_undef = 1;
6373 } else {
6374 printk(TPACPI_ERR
6375 "ThinkPad ACPI EC access misbehaving, "
6376 "fan status and control unavailable\n");
6377 return 1;
6381 if (sfan_handle) {
6382 /* 570, 770x-JL */
6383 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6384 fan_control_commands |=
6385 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6386 } else {
6387 if (!gfan_handle) {
6388 /* gfan without sfan means no fan control */
6389 /* all other models implement TP EC 0x2f control */
6391 if (fans_handle) {
6392 /* X31, X40, X41 */
6393 fan_control_access_mode =
6394 TPACPI_FAN_WR_ACPI_FANS;
6395 fan_control_commands |=
6396 TPACPI_FAN_CMD_SPEED |
6397 TPACPI_FAN_CMD_LEVEL |
6398 TPACPI_FAN_CMD_ENABLE;
6399 } else {
6400 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6401 fan_control_commands |=
6402 TPACPI_FAN_CMD_LEVEL |
6403 TPACPI_FAN_CMD_ENABLE;
6408 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6409 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6410 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6411 fan_status_access_mode, fan_control_access_mode);
6413 /* fan control master switch */
6414 if (!fan_control_allowed) {
6415 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6416 fan_control_commands = 0;
6417 dbg_printk(TPACPI_DBG_INIT,
6418 "fan control features disabled by parameter\n");
6421 /* update fan_control_desired_level */
6422 if (fan_status_access_mode != TPACPI_FAN_NONE)
6423 fan_get_status_safe(NULL);
6425 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6426 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6427 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6428 &fan_attr_group);
6429 if (rc < 0)
6430 return rc;
6432 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6433 &driver_attr_fan_watchdog);
6434 if (rc < 0) {
6435 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6436 &fan_attr_group);
6437 return rc;
6439 return 0;
6440 } else
6441 return 1;
6444 static void fan_exit(void)
6446 vdbg_printk(TPACPI_DBG_EXIT,
6447 "cancelling any pending fan watchdog tasks\n");
6449 /* FIXME: can we really do this unconditionally? */
6450 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6451 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6452 &driver_attr_fan_watchdog);
6454 cancel_delayed_work(&fan_watchdog_task);
6455 flush_workqueue(tpacpi_wq);
6458 static void fan_suspend(pm_message_t state)
6460 if (!fan_control_allowed)
6461 return;
6463 /* Store fan status in cache */
6464 fan_get_status_safe(NULL);
6465 if (tp_features.fan_ctrl_status_undef)
6466 fan_control_desired_level = TP_EC_FAN_AUTO;
6469 static void fan_resume(void)
6471 u8 saved_fan_level;
6472 u8 current_level = 7;
6473 bool do_set = false;
6475 /* DSDT *always* updates status on resume */
6476 tp_features.fan_ctrl_status_undef = 0;
6478 saved_fan_level = fan_control_desired_level;
6479 if (!fan_control_allowed ||
6480 (fan_get_status_safe(&current_level) < 0))
6481 return;
6483 switch (fan_control_access_mode) {
6484 case TPACPI_FAN_WR_ACPI_SFAN:
6485 do_set = (saved_fan_level > current_level);
6486 break;
6487 case TPACPI_FAN_WR_ACPI_FANS:
6488 case TPACPI_FAN_WR_TPEC:
6489 do_set = ((saved_fan_level & TP_EC_FAN_FULLSPEED) ||
6490 (saved_fan_level == 7 &&
6491 !(current_level & TP_EC_FAN_FULLSPEED)));
6492 break;
6493 default:
6494 return;
6496 if (do_set) {
6497 printk(TPACPI_NOTICE
6498 "restoring fan level to 0x%02x\n",
6499 saved_fan_level);
6500 fan_set_level_safe(saved_fan_level);
6504 static int fan_read(char *p)
6506 int len = 0;
6507 int rc;
6508 u8 status;
6509 unsigned int speed = 0;
6511 switch (fan_status_access_mode) {
6512 case TPACPI_FAN_RD_ACPI_GFAN:
6513 /* 570, 600e/x, 770e, 770x */
6514 rc = fan_get_status_safe(&status);
6515 if (rc < 0)
6516 return rc;
6518 len += sprintf(p + len, "status:\t\t%s\n"
6519 "level:\t\t%d\n",
6520 (status != 0) ? "enabled" : "disabled", status);
6521 break;
6523 case TPACPI_FAN_RD_TPEC:
6524 /* all except 570, 600e/x, 770e, 770x */
6525 rc = fan_get_status_safe(&status);
6526 if (rc < 0)
6527 return rc;
6529 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6530 if (status != fan_control_initial_status)
6531 tp_features.fan_ctrl_status_undef = 0;
6532 else
6533 /* Return most likely status. In fact, it
6534 * might be the only possible status */
6535 status = TP_EC_FAN_AUTO;
6538 len += sprintf(p + len, "status:\t\t%s\n",
6539 (status != 0) ? "enabled" : "disabled");
6541 rc = fan_get_speed(&speed);
6542 if (rc < 0)
6543 return rc;
6545 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6547 if (status & TP_EC_FAN_FULLSPEED)
6548 /* Disengaged mode takes precedence */
6549 len += sprintf(p + len, "level:\t\tdisengaged\n");
6550 else if (status & TP_EC_FAN_AUTO)
6551 len += sprintf(p + len, "level:\t\tauto\n");
6552 else
6553 len += sprintf(p + len, "level:\t\t%d\n", status);
6554 break;
6556 case TPACPI_FAN_NONE:
6557 default:
6558 len += sprintf(p + len, "status:\t\tnot supported\n");
6561 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6562 len += sprintf(p + len, "commands:\tlevel <level>");
6564 switch (fan_control_access_mode) {
6565 case TPACPI_FAN_WR_ACPI_SFAN:
6566 len += sprintf(p + len, " (<level> is 0-7)\n");
6567 break;
6569 default:
6570 len += sprintf(p + len, " (<level> is 0-7, "
6571 "auto, disengaged, full-speed)\n");
6572 break;
6576 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6577 len += sprintf(p + len, "commands:\tenable, disable\n"
6578 "commands:\twatchdog <timeout> (<timeout> "
6579 "is 0 (off), 1-120 (seconds))\n");
6581 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6582 len += sprintf(p + len, "commands:\tspeed <speed>"
6583 " (<speed> is 0-65535)\n");
6585 return len;
6588 static int fan_write_cmd_level(const char *cmd, int *rc)
6590 int level;
6592 if (strlencmp(cmd, "level auto") == 0)
6593 level = TP_EC_FAN_AUTO;
6594 else if ((strlencmp(cmd, "level disengaged") == 0) |
6595 (strlencmp(cmd, "level full-speed") == 0))
6596 level = TP_EC_FAN_FULLSPEED;
6597 else if (sscanf(cmd, "level %d", &level) != 1)
6598 return 0;
6600 *rc = fan_set_level_safe(level);
6601 if (*rc == -ENXIO)
6602 printk(TPACPI_ERR "level command accepted for unsupported "
6603 "access mode %d", fan_control_access_mode);
6605 return 1;
6608 static int fan_write_cmd_enable(const char *cmd, int *rc)
6610 if (strlencmp(cmd, "enable") != 0)
6611 return 0;
6613 *rc = fan_set_enable();
6614 if (*rc == -ENXIO)
6615 printk(TPACPI_ERR "enable command accepted for unsupported "
6616 "access mode %d", fan_control_access_mode);
6618 return 1;
6621 static int fan_write_cmd_disable(const char *cmd, int *rc)
6623 if (strlencmp(cmd, "disable") != 0)
6624 return 0;
6626 *rc = fan_set_disable();
6627 if (*rc == -ENXIO)
6628 printk(TPACPI_ERR "disable command accepted for unsupported "
6629 "access mode %d", fan_control_access_mode);
6631 return 1;
6634 static int fan_write_cmd_speed(const char *cmd, int *rc)
6636 int speed;
6638 /* TODO:
6639 * Support speed <low> <medium> <high> ? */
6641 if (sscanf(cmd, "speed %d", &speed) != 1)
6642 return 0;
6644 *rc = fan_set_speed(speed);
6645 if (*rc == -ENXIO)
6646 printk(TPACPI_ERR "speed command accepted for unsupported "
6647 "access mode %d", fan_control_access_mode);
6649 return 1;
6652 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6654 int interval;
6656 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6657 return 0;
6659 if (interval < 0 || interval > 120)
6660 *rc = -EINVAL;
6661 else
6662 fan_watchdog_maxinterval = interval;
6664 return 1;
6667 static int fan_write(char *buf)
6669 char *cmd;
6670 int rc = 0;
6672 while (!rc && (cmd = next_cmd(&buf))) {
6673 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6674 fan_write_cmd_level(cmd, &rc)) &&
6675 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6676 (fan_write_cmd_enable(cmd, &rc) ||
6677 fan_write_cmd_disable(cmd, &rc) ||
6678 fan_write_cmd_watchdog(cmd, &rc))) &&
6679 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6680 fan_write_cmd_speed(cmd, &rc))
6682 rc = -EINVAL;
6683 else if (!rc)
6684 fan_watchdog_reset();
6687 return rc;
6690 static struct ibm_struct fan_driver_data = {
6691 .name = "fan",
6692 .read = fan_read,
6693 .write = fan_write,
6694 .exit = fan_exit,
6695 .suspend = fan_suspend,
6696 .resume = fan_resume,
6699 /****************************************************************************
6700 ****************************************************************************
6702 * Infrastructure
6704 ****************************************************************************
6705 ****************************************************************************/
6707 /* sysfs name ---------------------------------------------------------- */
6708 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6709 struct device_attribute *attr,
6710 char *buf)
6712 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6715 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6716 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6718 /* --------------------------------------------------------------------- */
6720 /* /proc support */
6721 static struct proc_dir_entry *proc_dir;
6724 * Module and infrastructure proble, init and exit handling
6727 static int force_load;
6729 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6730 static const char * __init str_supported(int is_supported)
6732 static char text_unsupported[] __initdata = "not supported";
6734 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6736 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6738 static void ibm_exit(struct ibm_struct *ibm)
6740 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6742 list_del_init(&ibm->all_drivers);
6744 if (ibm->flags.acpi_notify_installed) {
6745 dbg_printk(TPACPI_DBG_EXIT,
6746 "%s: acpi_remove_notify_handler\n", ibm->name);
6747 BUG_ON(!ibm->acpi);
6748 acpi_remove_notify_handler(*ibm->acpi->handle,
6749 ibm->acpi->type,
6750 dispatch_acpi_notify);
6751 ibm->flags.acpi_notify_installed = 0;
6752 ibm->flags.acpi_notify_installed = 0;
6755 if (ibm->flags.proc_created) {
6756 dbg_printk(TPACPI_DBG_EXIT,
6757 "%s: remove_proc_entry\n", ibm->name);
6758 remove_proc_entry(ibm->name, proc_dir);
6759 ibm->flags.proc_created = 0;
6762 if (ibm->flags.acpi_driver_registered) {
6763 dbg_printk(TPACPI_DBG_EXIT,
6764 "%s: acpi_bus_unregister_driver\n", ibm->name);
6765 BUG_ON(!ibm->acpi);
6766 acpi_bus_unregister_driver(ibm->acpi->driver);
6767 kfree(ibm->acpi->driver);
6768 ibm->acpi->driver = NULL;
6769 ibm->flags.acpi_driver_registered = 0;
6772 if (ibm->flags.init_called && ibm->exit) {
6773 ibm->exit();
6774 ibm->flags.init_called = 0;
6777 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6780 static int __init ibm_init(struct ibm_init_struct *iibm)
6782 int ret;
6783 struct ibm_struct *ibm = iibm->data;
6784 struct proc_dir_entry *entry;
6786 BUG_ON(ibm == NULL);
6788 INIT_LIST_HEAD(&ibm->all_drivers);
6790 if (ibm->flags.experimental && !experimental)
6791 return 0;
6793 dbg_printk(TPACPI_DBG_INIT,
6794 "probing for %s\n", ibm->name);
6796 if (iibm->init) {
6797 ret = iibm->init(iibm);
6798 if (ret > 0)
6799 return 0; /* probe failed */
6800 if (ret)
6801 return ret;
6803 ibm->flags.init_called = 1;
6806 if (ibm->acpi) {
6807 if (ibm->acpi->hid) {
6808 ret = register_tpacpi_subdriver(ibm);
6809 if (ret)
6810 goto err_out;
6813 if (ibm->acpi->notify) {
6814 ret = setup_acpi_notify(ibm);
6815 if (ret == -ENODEV) {
6816 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6817 ibm->name);
6818 ret = 0;
6819 goto err_out;
6821 if (ret < 0)
6822 goto err_out;
6826 dbg_printk(TPACPI_DBG_INIT,
6827 "%s installed\n", ibm->name);
6829 if (ibm->read) {
6830 entry = create_proc_entry(ibm->name,
6831 S_IFREG | S_IRUGO | S_IWUSR,
6832 proc_dir);
6833 if (!entry) {
6834 printk(TPACPI_ERR "unable to create proc entry %s\n",
6835 ibm->name);
6836 ret = -ENODEV;
6837 goto err_out;
6839 entry->owner = THIS_MODULE;
6840 entry->data = ibm;
6841 entry->read_proc = &dispatch_procfs_read;
6842 if (ibm->write)
6843 entry->write_proc = &dispatch_procfs_write;
6844 ibm->flags.proc_created = 1;
6847 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6849 return 0;
6851 err_out:
6852 dbg_printk(TPACPI_DBG_INIT,
6853 "%s: at error exit path with result %d\n",
6854 ibm->name, ret);
6856 ibm_exit(ibm);
6857 return (ret < 0)? ret : 0;
6860 /* Probing */
6862 /* returns 0 - probe ok, or < 0 - probe error.
6863 * Probe ok doesn't mean thinkpad found.
6864 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6865 static int __must_check __init get_thinkpad_model_data(
6866 struct thinkpad_id_data *tp)
6868 const struct dmi_device *dev = NULL;
6869 char ec_fw_string[18];
6870 char const *s;
6872 if (!tp)
6873 return -EINVAL;
6875 memset(tp, 0, sizeof(*tp));
6877 if (dmi_name_in_vendors("IBM"))
6878 tp->vendor = PCI_VENDOR_ID_IBM;
6879 else if (dmi_name_in_vendors("LENOVO"))
6880 tp->vendor = PCI_VENDOR_ID_LENOVO;
6881 else
6882 return 0;
6884 s = dmi_get_system_info(DMI_BIOS_VERSION);
6885 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6886 if (s && !tp->bios_version_str)
6887 return -ENOMEM;
6888 if (!tp->bios_version_str)
6889 return 0;
6890 tp->bios_model = tp->bios_version_str[0]
6891 | (tp->bios_version_str[1] << 8);
6894 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6895 * X32 or newer, all Z series; Some models must have an
6896 * up-to-date BIOS or they will not be detected.
6898 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6900 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6901 if (sscanf(dev->name,
6902 "IBM ThinkPad Embedded Controller -[%17c",
6903 ec_fw_string) == 1) {
6904 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6905 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6907 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6908 if (!tp->ec_version_str)
6909 return -ENOMEM;
6910 tp->ec_model = ec_fw_string[0]
6911 | (ec_fw_string[1] << 8);
6912 break;
6916 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6917 if (s && !strnicmp(s, "ThinkPad", 8)) {
6918 tp->model_str = kstrdup(s, GFP_KERNEL);
6919 if (!tp->model_str)
6920 return -ENOMEM;
6923 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6924 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6925 if (s && !tp->nummodel_str)
6926 return -ENOMEM;
6928 return 0;
6931 static int __init probe_for_thinkpad(void)
6933 int is_thinkpad;
6935 if (acpi_disabled)
6936 return -ENODEV;
6939 * Non-ancient models have better DMI tagging, but very old models
6940 * don't.
6942 is_thinkpad = (thinkpad_id.model_str != NULL);
6944 /* ec is required because many other handles are relative to it */
6945 TPACPI_ACPIHANDLE_INIT(ec);
6946 if (!ec_handle) {
6947 if (is_thinkpad)
6948 printk(TPACPI_ERR
6949 "Not yet supported ThinkPad detected!\n");
6950 return -ENODEV;
6954 * Risks a regression on very old machines, but reduces potential
6955 * false positives a damn great deal
6957 if (!is_thinkpad)
6958 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6960 if (!is_thinkpad && !force_load)
6961 return -ENODEV;
6963 return 0;
6967 /* Module init, exit, parameters */
6969 static struct ibm_init_struct ibms_init[] __initdata = {
6971 .init = thinkpad_acpi_driver_init,
6972 .data = &thinkpad_acpi_driver_data,
6975 .init = hotkey_init,
6976 .data = &hotkey_driver_data,
6979 .init = bluetooth_init,
6980 .data = &bluetooth_driver_data,
6983 .init = wan_init,
6984 .data = &wan_driver_data,
6987 .init = uwb_init,
6988 .data = &uwb_driver_data,
6990 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6992 .init = video_init,
6993 .data = &video_driver_data,
6995 #endif
6997 .init = light_init,
6998 .data = &light_driver_data,
7000 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7002 .init = dock_init,
7003 .data = &dock_driver_data[0],
7006 .init = dock_init2,
7007 .data = &dock_driver_data[1],
7009 #endif
7010 #ifdef CONFIG_THINKPAD_ACPI_BAY
7012 .init = bay_init,
7013 .data = &bay_driver_data,
7015 #endif
7017 .init = cmos_init,
7018 .data = &cmos_driver_data,
7021 .init = led_init,
7022 .data = &led_driver_data,
7025 .init = beep_init,
7026 .data = &beep_driver_data,
7029 .init = thermal_init,
7030 .data = &thermal_driver_data,
7033 .data = &ecdump_driver_data,
7036 .init = brightness_init,
7037 .data = &brightness_driver_data,
7040 .data = &volume_driver_data,
7043 .init = fan_init,
7044 .data = &fan_driver_data,
7048 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7050 unsigned int i;
7051 struct ibm_struct *ibm;
7053 if (!kp || !kp->name || !val)
7054 return -EINVAL;
7056 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7057 ibm = ibms_init[i].data;
7058 WARN_ON(ibm == NULL);
7060 if (!ibm || !ibm->name)
7061 continue;
7063 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7064 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7065 return -ENOSPC;
7066 strcpy(ibms_init[i].param, val);
7067 strcat(ibms_init[i].param, ",");
7068 return 0;
7072 return -EINVAL;
7075 module_param(experimental, int, 0);
7076 MODULE_PARM_DESC(experimental,
7077 "Enables experimental features when non-zero");
7079 module_param_named(debug, dbg_level, uint, 0);
7080 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7082 module_param(force_load, bool, 0);
7083 MODULE_PARM_DESC(force_load,
7084 "Attempts to load the driver even on a "
7085 "mis-identified ThinkPad when true");
7087 module_param_named(fan_control, fan_control_allowed, bool, 0);
7088 MODULE_PARM_DESC(fan_control,
7089 "Enables setting fan parameters features when true");
7091 module_param_named(brightness_mode, brightness_mode, int, 0);
7092 MODULE_PARM_DESC(brightness_mode,
7093 "Selects brightness control strategy: "
7094 "0=auto, 1=EC, 2=CMOS, 3=both");
7096 module_param(brightness_enable, uint, 0);
7097 MODULE_PARM_DESC(brightness_enable,
7098 "Enables backlight control when 1, disables when 0");
7100 module_param(hotkey_report_mode, uint, 0);
7101 MODULE_PARM_DESC(hotkey_report_mode,
7102 "used for backwards compatibility with userspace, "
7103 "see documentation");
7105 #define TPACPI_PARAM(feature) \
7106 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7107 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7108 "at module load, see documentation")
7110 TPACPI_PARAM(hotkey);
7111 TPACPI_PARAM(bluetooth);
7112 TPACPI_PARAM(video);
7113 TPACPI_PARAM(light);
7114 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7115 TPACPI_PARAM(dock);
7116 #endif
7117 #ifdef CONFIG_THINKPAD_ACPI_BAY
7118 TPACPI_PARAM(bay);
7119 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7120 TPACPI_PARAM(cmos);
7121 TPACPI_PARAM(led);
7122 TPACPI_PARAM(beep);
7123 TPACPI_PARAM(ecdump);
7124 TPACPI_PARAM(brightness);
7125 TPACPI_PARAM(volume);
7126 TPACPI_PARAM(fan);
7128 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7129 module_param(dbg_wlswemul, uint, 0);
7130 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7131 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7132 MODULE_PARM_DESC(wlsw_state,
7133 "Initial state of the emulated WLSW switch");
7135 module_param(dbg_bluetoothemul, uint, 0);
7136 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7137 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7138 MODULE_PARM_DESC(bluetooth_state,
7139 "Initial state of the emulated bluetooth switch");
7141 module_param(dbg_wwanemul, uint, 0);
7142 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7143 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7144 MODULE_PARM_DESC(wwan_state,
7145 "Initial state of the emulated WWAN switch");
7147 module_param(dbg_uwbemul, uint, 0);
7148 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7149 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7150 MODULE_PARM_DESC(uwb_state,
7151 "Initial state of the emulated UWB switch");
7152 #endif
7154 static void thinkpad_acpi_module_exit(void)
7156 struct ibm_struct *ibm, *itmp;
7158 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7160 list_for_each_entry_safe_reverse(ibm, itmp,
7161 &tpacpi_all_drivers,
7162 all_drivers) {
7163 ibm_exit(ibm);
7166 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7168 if (tpacpi_inputdev) {
7169 if (tp_features.input_device_registered)
7170 input_unregister_device(tpacpi_inputdev);
7171 else
7172 input_free_device(tpacpi_inputdev);
7175 if (tpacpi_hwmon)
7176 hwmon_device_unregister(tpacpi_hwmon);
7178 if (tp_features.sensors_pdev_attrs_registered)
7179 device_remove_file(&tpacpi_sensors_pdev->dev,
7180 &dev_attr_thinkpad_acpi_pdev_name);
7181 if (tpacpi_sensors_pdev)
7182 platform_device_unregister(tpacpi_sensors_pdev);
7183 if (tpacpi_pdev)
7184 platform_device_unregister(tpacpi_pdev);
7186 if (tp_features.sensors_pdrv_attrs_registered)
7187 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7188 if (tp_features.platform_drv_attrs_registered)
7189 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7191 if (tp_features.sensors_pdrv_registered)
7192 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7194 if (tp_features.platform_drv_registered)
7195 platform_driver_unregister(&tpacpi_pdriver);
7197 if (proc_dir)
7198 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7200 if (tpacpi_wq)
7201 destroy_workqueue(tpacpi_wq);
7203 kfree(thinkpad_id.bios_version_str);
7204 kfree(thinkpad_id.ec_version_str);
7205 kfree(thinkpad_id.model_str);
7209 static int __init thinkpad_acpi_module_init(void)
7211 int ret, i;
7213 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7215 /* Parameter checking */
7216 if (hotkey_report_mode > 2)
7217 return -EINVAL;
7219 /* Driver-level probe */
7221 ret = get_thinkpad_model_data(&thinkpad_id);
7222 if (ret) {
7223 printk(TPACPI_ERR
7224 "unable to get DMI data: %d\n", ret);
7225 thinkpad_acpi_module_exit();
7226 return ret;
7228 ret = probe_for_thinkpad();
7229 if (ret) {
7230 thinkpad_acpi_module_exit();
7231 return ret;
7234 /* Driver initialization */
7236 TPACPI_ACPIHANDLE_INIT(ecrd);
7237 TPACPI_ACPIHANDLE_INIT(ecwr);
7239 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7240 if (!tpacpi_wq) {
7241 thinkpad_acpi_module_exit();
7242 return -ENOMEM;
7245 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7246 if (!proc_dir) {
7247 printk(TPACPI_ERR
7248 "unable to create proc dir " TPACPI_PROC_DIR);
7249 thinkpad_acpi_module_exit();
7250 return -ENODEV;
7252 proc_dir->owner = THIS_MODULE;
7254 ret = platform_driver_register(&tpacpi_pdriver);
7255 if (ret) {
7256 printk(TPACPI_ERR
7257 "unable to register main platform driver\n");
7258 thinkpad_acpi_module_exit();
7259 return ret;
7261 tp_features.platform_drv_registered = 1;
7263 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7264 if (ret) {
7265 printk(TPACPI_ERR
7266 "unable to register hwmon platform driver\n");
7267 thinkpad_acpi_module_exit();
7268 return ret;
7270 tp_features.sensors_pdrv_registered = 1;
7272 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7273 if (!ret) {
7274 tp_features.platform_drv_attrs_registered = 1;
7275 ret = tpacpi_create_driver_attributes(
7276 &tpacpi_hwmon_pdriver.driver);
7278 if (ret) {
7279 printk(TPACPI_ERR
7280 "unable to create sysfs driver attributes\n");
7281 thinkpad_acpi_module_exit();
7282 return ret;
7284 tp_features.sensors_pdrv_attrs_registered = 1;
7287 /* Device initialization */
7288 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7289 NULL, 0);
7290 if (IS_ERR(tpacpi_pdev)) {
7291 ret = PTR_ERR(tpacpi_pdev);
7292 tpacpi_pdev = NULL;
7293 printk(TPACPI_ERR "unable to register platform device\n");
7294 thinkpad_acpi_module_exit();
7295 return ret;
7297 tpacpi_sensors_pdev = platform_device_register_simple(
7298 TPACPI_HWMON_DRVR_NAME,
7299 -1, NULL, 0);
7300 if (IS_ERR(tpacpi_sensors_pdev)) {
7301 ret = PTR_ERR(tpacpi_sensors_pdev);
7302 tpacpi_sensors_pdev = NULL;
7303 printk(TPACPI_ERR
7304 "unable to register hwmon platform device\n");
7305 thinkpad_acpi_module_exit();
7306 return ret;
7308 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7309 &dev_attr_thinkpad_acpi_pdev_name);
7310 if (ret) {
7311 printk(TPACPI_ERR
7312 "unable to create sysfs hwmon device attributes\n");
7313 thinkpad_acpi_module_exit();
7314 return ret;
7316 tp_features.sensors_pdev_attrs_registered = 1;
7317 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7318 if (IS_ERR(tpacpi_hwmon)) {
7319 ret = PTR_ERR(tpacpi_hwmon);
7320 tpacpi_hwmon = NULL;
7321 printk(TPACPI_ERR "unable to register hwmon device\n");
7322 thinkpad_acpi_module_exit();
7323 return ret;
7325 mutex_init(&tpacpi_inputdev_send_mutex);
7326 tpacpi_inputdev = input_allocate_device();
7327 if (!tpacpi_inputdev) {
7328 printk(TPACPI_ERR "unable to allocate input device\n");
7329 thinkpad_acpi_module_exit();
7330 return -ENOMEM;
7331 } else {
7332 /* Prepare input device, but don't register */
7333 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7334 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7335 tpacpi_inputdev->id.bustype = BUS_HOST;
7336 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7337 thinkpad_id.vendor :
7338 PCI_VENDOR_ID_IBM;
7339 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7340 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7342 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7343 ret = ibm_init(&ibms_init[i]);
7344 if (ret >= 0 && *ibms_init[i].param)
7345 ret = ibms_init[i].data->write(ibms_init[i].param);
7346 if (ret < 0) {
7347 thinkpad_acpi_module_exit();
7348 return ret;
7351 ret = input_register_device(tpacpi_inputdev);
7352 if (ret < 0) {
7353 printk(TPACPI_ERR "unable to register input device\n");
7354 thinkpad_acpi_module_exit();
7355 return ret;
7356 } else {
7357 tp_features.input_device_registered = 1;
7360 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7361 return 0;
7364 /* Please remove this in year 2009 */
7365 MODULE_ALIAS("ibm_acpi");
7367 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7370 * DMI matching for module autoloading
7372 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7373 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7375 * Only models listed in thinkwiki will be supported, so add yours
7376 * if it is not there yet.
7378 #define IBM_BIOS_MODULE_ALIAS(__type) \
7379 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7381 /* Non-ancient thinkpads */
7382 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7383 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7385 /* Ancient thinkpad BIOSes have to be identified by
7386 * BIOS type or model number, and there are far less
7387 * BIOS types than model numbers... */
7388 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7389 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7390 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7392 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7393 MODULE_DESCRIPTION(TPACPI_DESC);
7394 MODULE_VERSION(TPACPI_VERSION);
7395 MODULE_LICENSE("GPL");
7397 module_init(thinkpad_acpi_module_init);
7398 module_exit(thinkpad_acpi_module_exit);