ACPI: thinkpad-acpi: debug facility to emulate the rf switches
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob8cd78526d15a1de3c779a60697dae10dc9739b88
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"
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
127 /****************************************************************************
128 * Main driver
131 #define TPACPI_NAME "thinkpad"
132 #define TPACPI_DESC "ThinkPad ACPI Extras"
133 #define TPACPI_FILE TPACPI_NAME "_acpi"
134 #define TPACPI_URL "http://ibm-acpi.sf.net/"
135 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
137 #define TPACPI_PROC_DIR "ibm"
138 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
139 #define TPACPI_DRVR_NAME TPACPI_FILE
140 #define TPACPI_DRVR_SHORTNAME "tpacpi"
141 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
143 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
146 #define TPACPI_MAX_ACPI_ARGS 3
148 /* rfkill switches */
149 enum {
150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
151 TPACPI_RFK_WWAN_SW_ID,
154 /* Debugging */
155 #define TPACPI_LOG TPACPI_FILE ": "
156 #define TPACPI_ERR KERN_ERR TPACPI_LOG
157 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
158 #define TPACPI_INFO KERN_INFO TPACPI_LOG
159 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
161 #define TPACPI_DBG_ALL 0xffff
162 #define TPACPI_DBG_INIT 0x0001
163 #define TPACPI_DBG_EXIT 0x0002
164 #define dbg_printk(a_dbg_level, format, arg...) \
165 do { if (dbg_level & a_dbg_level) \
166 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
167 } while (0)
168 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
169 #define vdbg_printk(a_dbg_level, format, arg...) \
170 dbg_printk(a_dbg_level, format, ## arg)
171 static const char *str_supported(int is_supported);
172 #else
173 #define vdbg_printk(a_dbg_level, format, arg...)
174 #endif
176 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
177 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
178 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
181 /****************************************************************************
182 * Driver-wide structs and misc. variables
185 struct ibm_struct;
187 struct tp_acpi_drv_struct {
188 const struct acpi_device_id *hid;
189 struct acpi_driver *driver;
191 void (*notify) (struct ibm_struct *, u32);
192 acpi_handle *handle;
193 u32 type;
194 struct acpi_device *device;
197 struct ibm_struct {
198 char *name;
200 int (*read) (char *);
201 int (*write) (char *);
202 void (*exit) (void);
203 void (*resume) (void);
204 void (*suspend) (pm_message_t state);
206 struct list_head all_drivers;
208 struct tp_acpi_drv_struct *acpi;
210 struct {
211 u8 acpi_driver_registered:1;
212 u8 acpi_notify_installed:1;
213 u8 proc_created:1;
214 u8 init_called:1;
215 u8 experimental:1;
216 } flags;
219 struct ibm_init_struct {
220 char param[32];
222 int (*init) (struct ibm_init_struct *);
223 struct ibm_struct *data;
226 static struct {
227 #ifdef CONFIG_THINKPAD_ACPI_BAY
228 u32 bay_status:1;
229 u32 bay_eject:1;
230 u32 bay_status2:1;
231 u32 bay_eject2:1;
232 #endif
233 u32 bluetooth:1;
234 u32 hotkey:1;
235 u32 hotkey_mask:1;
236 u32 hotkey_wlsw:1;
237 u32 hotkey_tablet:1;
238 u32 light:1;
239 u32 light_status:1;
240 u32 bright_16levels:1;
241 u32 bright_acpimode:1;
242 u32 wan:1;
243 u32 fan_ctrl_status_undef:1;
244 u32 input_device_registered:1;
245 u32 platform_drv_registered:1;
246 u32 platform_drv_attrs_registered:1;
247 u32 sensors_pdrv_registered:1;
248 u32 sensors_pdrv_attrs_registered:1;
249 u32 sensors_pdev_attrs_registered:1;
250 u32 hotkey_poll_active:1;
251 } tp_features;
253 static struct {
254 u16 hotkey_mask_ff:1;
255 u16 bright_cmos_ec_unsync:1;
256 } tp_warned;
258 struct thinkpad_id_data {
259 unsigned int vendor; /* ThinkPad vendor:
260 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
262 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
263 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
265 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
266 u16 ec_model;
268 char *model_str; /* ThinkPad T43 */
269 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
271 static struct thinkpad_id_data thinkpad_id;
273 static enum {
274 TPACPI_LIFE_INIT = 0,
275 TPACPI_LIFE_RUNNING,
276 TPACPI_LIFE_EXITING,
277 } tpacpi_lifecycle;
279 static int experimental;
280 static u32 dbg_level;
282 static struct workqueue_struct *tpacpi_wq;
284 /* Special LED class that can defer work */
285 struct tpacpi_led_classdev {
286 struct led_classdev led_classdev;
287 struct work_struct work;
288 enum led_brightness new_brightness;
289 unsigned int led;
292 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
293 static int dbg_wlswemul;
294 static int tpacpi_wlsw_emulstate;
295 static int dbg_bluetoothemul;
296 static int tpacpi_bluetooth_emulstate;
297 static int dbg_wwanemul;
298 static int tpacpi_wwan_emulstate;
299 #endif
302 /****************************************************************************
303 ****************************************************************************
305 * ACPI Helpers and device model
307 ****************************************************************************
308 ****************************************************************************/
310 /*************************************************************************
311 * ACPI basic handles
314 static acpi_handle root_handle;
316 #define TPACPI_HANDLE(object, parent, paths...) \
317 static acpi_handle object##_handle; \
318 static acpi_handle *object##_parent = &parent##_handle; \
319 static char *object##_path; \
320 static char *object##_paths[] = { paths }
322 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
323 "\\_SB.PCI.ISA.EC", /* 570 */
324 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
325 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
326 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
327 "\\_SB.PCI0.ICH3.EC0", /* R31 */
328 "\\_SB.PCI0.LPC.EC", /* all others */
331 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
332 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
334 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
335 /* T4x, X31, X40 */
336 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
337 "\\CMS", /* R40, R40e */
338 ); /* all others */
340 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
341 "^HKEY", /* R30, R31 */
342 "HKEY", /* all others */
343 ); /* 570 */
345 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
346 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
347 "\\_SB.PCI0.VID0", /* 770e */
348 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
349 "\\_SB.PCI0.AGP.VID", /* all others */
350 ); /* R30, R31 */
353 /*************************************************************************
354 * ACPI helpers
357 static int acpi_evalf(acpi_handle handle,
358 void *res, char *method, char *fmt, ...)
360 char *fmt0 = fmt;
361 struct acpi_object_list params;
362 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
363 struct acpi_buffer result, *resultp;
364 union acpi_object out_obj;
365 acpi_status status;
366 va_list ap;
367 char res_type;
368 int success;
369 int quiet;
371 if (!*fmt) {
372 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
373 return 0;
376 if (*fmt == 'q') {
377 quiet = 1;
378 fmt++;
379 } else
380 quiet = 0;
382 res_type = *(fmt++);
384 params.count = 0;
385 params.pointer = &in_objs[0];
387 va_start(ap, fmt);
388 while (*fmt) {
389 char c = *(fmt++);
390 switch (c) {
391 case 'd': /* int */
392 in_objs[params.count].integer.value = va_arg(ap, int);
393 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
394 break;
395 /* add more types as needed */
396 default:
397 printk(TPACPI_ERR "acpi_evalf() called "
398 "with invalid format character '%c'\n", c);
399 return 0;
402 va_end(ap);
404 if (res_type != 'v') {
405 result.length = sizeof(out_obj);
406 result.pointer = &out_obj;
407 resultp = &result;
408 } else
409 resultp = NULL;
411 status = acpi_evaluate_object(handle, method, &params, resultp);
413 switch (res_type) {
414 case 'd': /* int */
415 if (res)
416 *(int *)res = out_obj.integer.value;
417 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
418 break;
419 case 'v': /* void */
420 success = status == AE_OK;
421 break;
422 /* add more types as needed */
423 default:
424 printk(TPACPI_ERR "acpi_evalf() called "
425 "with invalid format character '%c'\n", res_type);
426 return 0;
429 if (!success && !quiet)
430 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
431 method, fmt0, status);
433 return success;
436 static int acpi_ec_read(int i, u8 *p)
438 int v;
440 if (ecrd_handle) {
441 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
442 return 0;
443 *p = v;
444 } else {
445 if (ec_read(i, p) < 0)
446 return 0;
449 return 1;
452 static int acpi_ec_write(int i, u8 v)
454 if (ecwr_handle) {
455 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
456 return 0;
457 } else {
458 if (ec_write(i, v) < 0)
459 return 0;
462 return 1;
465 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
466 static int _sta(acpi_handle handle)
468 int status;
470 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
471 status = 0;
473 return status;
475 #endif
477 static int issue_thinkpad_cmos_command(int cmos_cmd)
479 if (!cmos_handle)
480 return -ENXIO;
482 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
483 return -EIO;
485 return 0;
488 /*************************************************************************
489 * ACPI device model
492 #define TPACPI_ACPIHANDLE_INIT(object) \
493 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
494 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
496 static void drv_acpi_handle_init(char *name,
497 acpi_handle *handle, acpi_handle parent,
498 char **paths, int num_paths, char **path)
500 int i;
501 acpi_status status;
503 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
504 name);
506 for (i = 0; i < num_paths; i++) {
507 status = acpi_get_handle(parent, paths[i], handle);
508 if (ACPI_SUCCESS(status)) {
509 *path = paths[i];
510 dbg_printk(TPACPI_DBG_INIT,
511 "Found ACPI handle %s for %s\n",
512 *path, name);
513 return;
517 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
518 name);
519 *handle = NULL;
522 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
524 struct ibm_struct *ibm = data;
526 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
527 return;
529 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
530 return;
532 ibm->acpi->notify(ibm, event);
535 static int __init setup_acpi_notify(struct ibm_struct *ibm)
537 acpi_status status;
538 int rc;
540 BUG_ON(!ibm->acpi);
542 if (!*ibm->acpi->handle)
543 return 0;
545 vdbg_printk(TPACPI_DBG_INIT,
546 "setting up ACPI notify for %s\n", ibm->name);
548 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
549 if (rc < 0) {
550 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
551 ibm->name, rc);
552 return -ENODEV;
555 ibm->acpi->device->driver_data = ibm;
556 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
557 TPACPI_ACPI_EVENT_PREFIX,
558 ibm->name);
560 status = acpi_install_notify_handler(*ibm->acpi->handle,
561 ibm->acpi->type, dispatch_acpi_notify, ibm);
562 if (ACPI_FAILURE(status)) {
563 if (status == AE_ALREADY_EXISTS) {
564 printk(TPACPI_NOTICE
565 "another device driver is already "
566 "handling %s events\n", ibm->name);
567 } else {
568 printk(TPACPI_ERR
569 "acpi_install_notify_handler(%s) failed: %d\n",
570 ibm->name, status);
572 return -ENODEV;
574 ibm->flags.acpi_notify_installed = 1;
575 return 0;
578 static int __init tpacpi_device_add(struct acpi_device *device)
580 return 0;
583 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
585 int rc;
587 dbg_printk(TPACPI_DBG_INIT,
588 "registering %s as an ACPI driver\n", ibm->name);
590 BUG_ON(!ibm->acpi);
592 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
593 if (!ibm->acpi->driver) {
594 printk(TPACPI_ERR
595 "failed to allocate memory for ibm->acpi->driver\n");
596 return -ENOMEM;
599 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
600 ibm->acpi->driver->ids = ibm->acpi->hid;
602 ibm->acpi->driver->ops.add = &tpacpi_device_add;
604 rc = acpi_bus_register_driver(ibm->acpi->driver);
605 if (rc < 0) {
606 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
607 ibm->name, rc);
608 kfree(ibm->acpi->driver);
609 ibm->acpi->driver = NULL;
610 } else if (!rc)
611 ibm->flags.acpi_driver_registered = 1;
613 return rc;
617 /****************************************************************************
618 ****************************************************************************
620 * Procfs Helpers
622 ****************************************************************************
623 ****************************************************************************/
625 static int dispatch_procfs_read(char *page, char **start, off_t off,
626 int count, int *eof, void *data)
628 struct ibm_struct *ibm = data;
629 int len;
631 if (!ibm || !ibm->read)
632 return -EINVAL;
634 len = ibm->read(page);
635 if (len < 0)
636 return len;
638 if (len <= off + count)
639 *eof = 1;
640 *start = page + off;
641 len -= off;
642 if (len > count)
643 len = count;
644 if (len < 0)
645 len = 0;
647 return len;
650 static int dispatch_procfs_write(struct file *file,
651 const char __user *userbuf,
652 unsigned long count, void *data)
654 struct ibm_struct *ibm = data;
655 char *kernbuf;
656 int ret;
658 if (!ibm || !ibm->write)
659 return -EINVAL;
661 kernbuf = kmalloc(count + 2, GFP_KERNEL);
662 if (!kernbuf)
663 return -ENOMEM;
665 if (copy_from_user(kernbuf, userbuf, count)) {
666 kfree(kernbuf);
667 return -EFAULT;
670 kernbuf[count] = 0;
671 strcat(kernbuf, ",");
672 ret = ibm->write(kernbuf);
673 if (ret == 0)
674 ret = count;
676 kfree(kernbuf);
678 return ret;
681 static char *next_cmd(char **cmds)
683 char *start = *cmds;
684 char *end;
686 while ((end = strchr(start, ',')) && end == start)
687 start = end + 1;
689 if (!end)
690 return NULL;
692 *end = 0;
693 *cmds = end + 1;
694 return start;
698 /****************************************************************************
699 ****************************************************************************
701 * Device model: input, hwmon and platform
703 ****************************************************************************
704 ****************************************************************************/
706 static struct platform_device *tpacpi_pdev;
707 static struct platform_device *tpacpi_sensors_pdev;
708 static struct device *tpacpi_hwmon;
709 static struct input_dev *tpacpi_inputdev;
710 static struct mutex tpacpi_inputdev_send_mutex;
711 static LIST_HEAD(tpacpi_all_drivers);
713 static int tpacpi_suspend_handler(struct platform_device *pdev,
714 pm_message_t state)
716 struct ibm_struct *ibm, *itmp;
718 list_for_each_entry_safe(ibm, itmp,
719 &tpacpi_all_drivers,
720 all_drivers) {
721 if (ibm->suspend)
722 (ibm->suspend)(state);
725 return 0;
728 static int tpacpi_resume_handler(struct platform_device *pdev)
730 struct ibm_struct *ibm, *itmp;
732 list_for_each_entry_safe(ibm, itmp,
733 &tpacpi_all_drivers,
734 all_drivers) {
735 if (ibm->resume)
736 (ibm->resume)();
739 return 0;
742 static struct platform_driver tpacpi_pdriver = {
743 .driver = {
744 .name = TPACPI_DRVR_NAME,
745 .owner = THIS_MODULE,
747 .suspend = tpacpi_suspend_handler,
748 .resume = tpacpi_resume_handler,
751 static struct platform_driver tpacpi_hwmon_pdriver = {
752 .driver = {
753 .name = TPACPI_HWMON_DRVR_NAME,
754 .owner = THIS_MODULE,
758 /*************************************************************************
759 * sysfs support helpers
762 struct attribute_set {
763 unsigned int members, max_members;
764 struct attribute_group group;
767 struct attribute_set_obj {
768 struct attribute_set s;
769 struct attribute *a;
770 } __attribute__((packed));
772 static struct attribute_set *create_attr_set(unsigned int max_members,
773 const char *name)
775 struct attribute_set_obj *sobj;
777 if (max_members == 0)
778 return NULL;
780 /* Allocates space for implicit NULL at the end too */
781 sobj = kzalloc(sizeof(struct attribute_set_obj) +
782 max_members * sizeof(struct attribute *),
783 GFP_KERNEL);
784 if (!sobj)
785 return NULL;
786 sobj->s.max_members = max_members;
787 sobj->s.group.attrs = &sobj->a;
788 sobj->s.group.name = name;
790 return &sobj->s;
793 #define destroy_attr_set(_set) \
794 kfree(_set);
796 /* not multi-threaded safe, use it in a single thread per set */
797 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
799 if (!s || !attr)
800 return -EINVAL;
802 if (s->members >= s->max_members)
803 return -ENOMEM;
805 s->group.attrs[s->members] = attr;
806 s->members++;
808 return 0;
811 static int add_many_to_attr_set(struct attribute_set *s,
812 struct attribute **attr,
813 unsigned int count)
815 int i, res;
817 for (i = 0; i < count; i++) {
818 res = add_to_attr_set(s, attr[i]);
819 if (res)
820 return res;
823 return 0;
826 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
828 sysfs_remove_group(kobj, &s->group);
829 destroy_attr_set(s);
832 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
833 sysfs_create_group(_kobj, &_attr_set->group)
835 static int parse_strtoul(const char *buf,
836 unsigned long max, unsigned long *value)
838 char *endp;
840 while (*buf && isspace(*buf))
841 buf++;
842 *value = simple_strtoul(buf, &endp, 0);
843 while (*endp && isspace(*endp))
844 endp++;
845 if (*endp || *value > max)
846 return -EINVAL;
848 return 0;
851 static void tpacpi_disable_brightness_delay(void)
853 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
854 printk(TPACPI_NOTICE
855 "ACPI backlight control delay disabled\n");
858 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
860 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
861 union acpi_object *obj;
862 int rc;
864 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
865 obj = (union acpi_object *)buffer.pointer;
866 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
867 printk(TPACPI_ERR "Unknown _BCL data, "
868 "please report this to %s\n", TPACPI_MAIL);
869 rc = 0;
870 } else {
871 rc = obj->package.count;
873 } else {
874 return 0;
877 kfree(buffer.pointer);
878 return rc;
881 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
882 u32 lvl, void *context, void **rv)
884 char name[ACPI_PATH_SEGMENT_LENGTH];
885 struct acpi_buffer buffer = { sizeof(name), &name };
887 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
888 !strncmp("_BCL", name, sizeof(name) - 1)) {
889 BUG_ON(!rv || !*rv);
890 **(int **)rv = tpacpi_query_bcl_levels(handle);
891 return AE_CTRL_TERMINATE;
892 } else {
893 return AE_OK;
898 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
900 static int __init tpacpi_check_std_acpi_brightness_support(void)
902 int status;
903 int bcl_levels = 0;
904 void *bcl_ptr = &bcl_levels;
906 if (!vid_handle) {
907 TPACPI_ACPIHANDLE_INIT(vid);
909 if (!vid_handle)
910 return 0;
913 * Search for a _BCL method, and execute it. This is safe on all
914 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
915 * BIOS in ACPI backlight control mode. We do NOT have to care
916 * about calling the _BCL method in an enabled video device, any
917 * will do for our purposes.
920 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
921 tpacpi_acpi_walk_find_bcl, NULL,
922 &bcl_ptr);
924 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
925 tp_features.bright_acpimode = 1;
926 return (bcl_levels - 2);
929 return 0;
932 static int __init tpacpi_new_rfkill(const unsigned int id,
933 struct rfkill **rfk,
934 const enum rfkill_type rfktype,
935 const char *name,
936 int (*toggle_radio)(void *, enum rfkill_state),
937 int (*get_state)(void *, enum rfkill_state *))
939 int res;
940 enum rfkill_state initial_state;
942 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
943 if (!*rfk) {
944 printk(TPACPI_ERR
945 "failed to allocate memory for rfkill class\n");
946 return -ENOMEM;
949 (*rfk)->name = name;
950 (*rfk)->get_state = get_state;
951 (*rfk)->toggle_radio = toggle_radio;
953 if (!get_state(NULL, &initial_state))
954 (*rfk)->state = initial_state;
956 res = rfkill_register(*rfk);
957 if (res < 0) {
958 printk(TPACPI_ERR
959 "failed to register %s rfkill switch: %d\n",
960 name, res);
961 rfkill_free(*rfk);
962 *rfk = NULL;
963 return res;
966 return 0;
969 /*************************************************************************
970 * thinkpad-acpi driver attributes
973 /* interface_version --------------------------------------------------- */
974 static ssize_t tpacpi_driver_interface_version_show(
975 struct device_driver *drv,
976 char *buf)
978 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
981 static DRIVER_ATTR(interface_version, S_IRUGO,
982 tpacpi_driver_interface_version_show, NULL);
984 /* debug_level --------------------------------------------------------- */
985 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
986 char *buf)
988 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
991 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
992 const char *buf, size_t count)
994 unsigned long t;
996 if (parse_strtoul(buf, 0xffff, &t))
997 return -EINVAL;
999 dbg_level = t;
1001 return count;
1004 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1005 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1007 /* version ------------------------------------------------------------- */
1008 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1009 char *buf)
1011 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1012 TPACPI_DESC, TPACPI_VERSION);
1015 static DRIVER_ATTR(version, S_IRUGO,
1016 tpacpi_driver_version_show, NULL);
1018 /* --------------------------------------------------------------------- */
1020 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1022 static void tpacpi_send_radiosw_update(void);
1024 /* wlsw_emulstate ------------------------------------------------------ */
1025 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1026 char *buf)
1028 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1031 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1032 const char *buf, size_t count)
1034 unsigned long t;
1036 if (parse_strtoul(buf, 1, &t))
1037 return -EINVAL;
1039 if (tpacpi_wlsw_emulstate != t) {
1040 tpacpi_wlsw_emulstate = !!t;
1041 tpacpi_send_radiosw_update();
1042 } else
1043 tpacpi_wlsw_emulstate = !!t;
1045 return count;
1048 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1049 tpacpi_driver_wlsw_emulstate_show,
1050 tpacpi_driver_wlsw_emulstate_store);
1052 /* bluetooth_emulstate ------------------------------------------------- */
1053 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1054 struct device_driver *drv,
1055 char *buf)
1057 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1060 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1061 struct device_driver *drv,
1062 const char *buf, size_t count)
1064 unsigned long t;
1066 if (parse_strtoul(buf, 1, &t))
1067 return -EINVAL;
1069 tpacpi_bluetooth_emulstate = !!t;
1071 return count;
1074 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1075 tpacpi_driver_bluetooth_emulstate_show,
1076 tpacpi_driver_bluetooth_emulstate_store);
1078 /* wwan_emulstate ------------------------------------------------- */
1079 static ssize_t tpacpi_driver_wwan_emulstate_show(
1080 struct device_driver *drv,
1081 char *buf)
1083 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1086 static ssize_t tpacpi_driver_wwan_emulstate_store(
1087 struct device_driver *drv,
1088 const char *buf, size_t count)
1090 unsigned long t;
1092 if (parse_strtoul(buf, 1, &t))
1093 return -EINVAL;
1095 tpacpi_wwan_emulstate = !!t;
1097 return count;
1100 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1101 tpacpi_driver_wwan_emulstate_show,
1102 tpacpi_driver_wwan_emulstate_store);
1104 #endif
1106 /* --------------------------------------------------------------------- */
1108 static struct driver_attribute *tpacpi_driver_attributes[] = {
1109 &driver_attr_debug_level, &driver_attr_version,
1110 &driver_attr_interface_version,
1113 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1115 int i, res;
1117 i = 0;
1118 res = 0;
1119 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1120 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1121 i++;
1124 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1125 if (!res && dbg_wlswemul)
1126 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1127 if (!res && dbg_bluetoothemul)
1128 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1129 if (!res && dbg_wwanemul)
1130 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1131 #endif
1133 return res;
1136 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1138 int i;
1140 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1141 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1143 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1144 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1145 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1146 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1147 #endif
1150 /****************************************************************************
1151 ****************************************************************************
1153 * Subdrivers
1155 ****************************************************************************
1156 ****************************************************************************/
1158 /*************************************************************************
1159 * thinkpad-acpi init subdriver
1162 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1164 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1165 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1167 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1168 (thinkpad_id.bios_version_str) ?
1169 thinkpad_id.bios_version_str : "unknown",
1170 (thinkpad_id.ec_version_str) ?
1171 thinkpad_id.ec_version_str : "unknown");
1173 if (thinkpad_id.vendor && thinkpad_id.model_str)
1174 printk(TPACPI_INFO "%s %s, model %s\n",
1175 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1176 "IBM" : ((thinkpad_id.vendor ==
1177 PCI_VENDOR_ID_LENOVO) ?
1178 "Lenovo" : "Unknown vendor"),
1179 thinkpad_id.model_str,
1180 (thinkpad_id.nummodel_str) ?
1181 thinkpad_id.nummodel_str : "unknown");
1183 return 0;
1186 static int thinkpad_acpi_driver_read(char *p)
1188 int len = 0;
1190 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1191 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1193 return len;
1196 static struct ibm_struct thinkpad_acpi_driver_data = {
1197 .name = "driver",
1198 .read = thinkpad_acpi_driver_read,
1201 /*************************************************************************
1202 * Hotkey subdriver
1205 enum { /* hot key scan codes (derived from ACPI DSDT) */
1206 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1207 TP_ACPI_HOTKEYSCAN_FNF2,
1208 TP_ACPI_HOTKEYSCAN_FNF3,
1209 TP_ACPI_HOTKEYSCAN_FNF4,
1210 TP_ACPI_HOTKEYSCAN_FNF5,
1211 TP_ACPI_HOTKEYSCAN_FNF6,
1212 TP_ACPI_HOTKEYSCAN_FNF7,
1213 TP_ACPI_HOTKEYSCAN_FNF8,
1214 TP_ACPI_HOTKEYSCAN_FNF9,
1215 TP_ACPI_HOTKEYSCAN_FNF10,
1216 TP_ACPI_HOTKEYSCAN_FNF11,
1217 TP_ACPI_HOTKEYSCAN_FNF12,
1218 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1219 TP_ACPI_HOTKEYSCAN_FNINSERT,
1220 TP_ACPI_HOTKEYSCAN_FNDELETE,
1221 TP_ACPI_HOTKEYSCAN_FNHOME,
1222 TP_ACPI_HOTKEYSCAN_FNEND,
1223 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1224 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1225 TP_ACPI_HOTKEYSCAN_FNSPACE,
1226 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1227 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1228 TP_ACPI_HOTKEYSCAN_MUTE,
1229 TP_ACPI_HOTKEYSCAN_THINKPAD,
1232 enum { /* Keys available through NVRAM polling */
1233 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1234 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1237 enum { /* Positions of some of the keys in hotkey masks */
1238 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1239 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1240 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1241 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1242 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1243 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1244 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1245 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1246 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1247 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1248 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1251 enum { /* NVRAM to ACPI HKEY group map */
1252 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1253 TP_ACPI_HKEY_ZOOM_MASK |
1254 TP_ACPI_HKEY_DISPSWTCH_MASK |
1255 TP_ACPI_HKEY_HIBERNATE_MASK,
1256 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1257 TP_ACPI_HKEY_BRGHTDWN_MASK,
1258 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1259 TP_ACPI_HKEY_VOLDWN_MASK |
1260 TP_ACPI_HKEY_MUTE_MASK,
1263 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1264 struct tp_nvram_state {
1265 u16 thinkpad_toggle:1;
1266 u16 zoom_toggle:1;
1267 u16 display_toggle:1;
1268 u16 thinklight_toggle:1;
1269 u16 hibernate_toggle:1;
1270 u16 displayexp_toggle:1;
1271 u16 display_state:1;
1272 u16 brightness_toggle:1;
1273 u16 volume_toggle:1;
1274 u16 mute:1;
1276 u8 brightness_level;
1277 u8 volume_level;
1280 static struct task_struct *tpacpi_hotkey_task;
1281 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1282 static int hotkey_poll_freq = 10; /* Hz */
1283 static struct mutex hotkey_thread_mutex;
1284 static struct mutex hotkey_thread_data_mutex;
1285 static unsigned int hotkey_config_change;
1287 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1289 #define hotkey_source_mask 0U
1291 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1293 static struct mutex hotkey_mutex;
1295 static enum { /* Reasons for waking up */
1296 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1297 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1298 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1299 } hotkey_wakeup_reason;
1301 static int hotkey_autosleep_ack;
1303 static int hotkey_orig_status;
1304 static u32 hotkey_orig_mask;
1305 static u32 hotkey_all_mask;
1306 static u32 hotkey_reserved_mask;
1307 static u32 hotkey_mask;
1309 static unsigned int hotkey_report_mode;
1311 static u16 *hotkey_keycode_map;
1313 static struct attribute_set *hotkey_dev_attributes;
1315 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1316 #define HOTKEY_CONFIG_CRITICAL_START \
1317 do { \
1318 mutex_lock(&hotkey_thread_data_mutex); \
1319 hotkey_config_change++; \
1320 } while (0);
1321 #define HOTKEY_CONFIG_CRITICAL_END \
1322 mutex_unlock(&hotkey_thread_data_mutex);
1323 #else
1324 #define HOTKEY_CONFIG_CRITICAL_START
1325 #define HOTKEY_CONFIG_CRITICAL_END
1326 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1328 /* HKEY.MHKG() return bits */
1329 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1331 static int hotkey_get_wlsw(int *status)
1333 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1334 if (dbg_wlswemul) {
1335 *status = !!tpacpi_wlsw_emulstate;
1336 return 0;
1338 #endif
1339 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1340 return -EIO;
1341 return 0;
1344 static int hotkey_get_tablet_mode(int *status)
1346 int s;
1348 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1349 return -EIO;
1351 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1352 return 0;
1356 * Call with hotkey_mutex held
1358 static int hotkey_mask_get(void)
1360 u32 m = 0;
1362 if (tp_features.hotkey_mask) {
1363 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1364 return -EIO;
1366 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1368 return 0;
1372 * Call with hotkey_mutex held
1374 static int hotkey_mask_set(u32 mask)
1376 int i;
1377 int rc = 0;
1379 if (tp_features.hotkey_mask) {
1380 if (!tp_warned.hotkey_mask_ff &&
1381 (mask == 0xffff || mask == 0xffffff ||
1382 mask == 0xffffffff)) {
1383 tp_warned.hotkey_mask_ff = 1;
1384 printk(TPACPI_NOTICE
1385 "setting the hotkey mask to 0x%08x is likely "
1386 "not the best way to go about it\n", mask);
1387 printk(TPACPI_NOTICE
1388 "please consider using the driver defaults, "
1389 "and refer to up-to-date thinkpad-acpi "
1390 "documentation\n");
1393 HOTKEY_CONFIG_CRITICAL_START
1394 for (i = 0; i < 32; i++) {
1395 u32 m = 1 << i;
1396 /* enable in firmware mask only keys not in NVRAM
1397 * mode, but enable the key in the cached hotkey_mask
1398 * regardless of mode, or the key will end up
1399 * disabled by hotkey_mask_get() */
1400 if (!acpi_evalf(hkey_handle,
1401 NULL, "MHKM", "vdd", i + 1,
1402 !!((mask & ~hotkey_source_mask) & m))) {
1403 rc = -EIO;
1404 break;
1405 } else {
1406 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1409 HOTKEY_CONFIG_CRITICAL_END
1411 /* hotkey_mask_get must be called unconditionally below */
1412 if (!hotkey_mask_get() && !rc &&
1413 (hotkey_mask & ~hotkey_source_mask) !=
1414 (mask & ~hotkey_source_mask)) {
1415 printk(TPACPI_NOTICE
1416 "requested hot key mask 0x%08x, but "
1417 "firmware forced it to 0x%08x\n",
1418 mask, hotkey_mask);
1420 } else {
1421 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1422 HOTKEY_CONFIG_CRITICAL_START
1423 hotkey_mask = mask & hotkey_source_mask;
1424 HOTKEY_CONFIG_CRITICAL_END
1425 hotkey_mask_get();
1426 if (hotkey_mask != mask) {
1427 printk(TPACPI_NOTICE
1428 "requested hot key mask 0x%08x, "
1429 "forced to 0x%08x (NVRAM poll mask is "
1430 "0x%08x): no firmware mask support\n",
1431 mask, hotkey_mask, hotkey_source_mask);
1433 #else
1434 hotkey_mask_get();
1435 rc = -ENXIO;
1436 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1439 return rc;
1442 static int hotkey_status_get(int *status)
1444 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1445 return -EIO;
1447 return 0;
1450 static int hotkey_status_set(int status)
1452 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1453 return -EIO;
1455 return 0;
1458 static void tpacpi_input_send_tabletsw(void)
1460 int state;
1462 if (tp_features.hotkey_tablet &&
1463 !hotkey_get_tablet_mode(&state)) {
1464 mutex_lock(&tpacpi_inputdev_send_mutex);
1466 input_report_switch(tpacpi_inputdev,
1467 SW_TABLET_MODE, !!state);
1468 input_sync(tpacpi_inputdev);
1470 mutex_unlock(&tpacpi_inputdev_send_mutex);
1474 static void tpacpi_input_send_key(unsigned int scancode)
1476 unsigned int keycode;
1478 keycode = hotkey_keycode_map[scancode];
1480 if (keycode != KEY_RESERVED) {
1481 mutex_lock(&tpacpi_inputdev_send_mutex);
1483 input_report_key(tpacpi_inputdev, keycode, 1);
1484 if (keycode == KEY_UNKNOWN)
1485 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1486 scancode);
1487 input_sync(tpacpi_inputdev);
1489 input_report_key(tpacpi_inputdev, keycode, 0);
1490 if (keycode == KEY_UNKNOWN)
1491 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1492 scancode);
1493 input_sync(tpacpi_inputdev);
1495 mutex_unlock(&tpacpi_inputdev_send_mutex);
1499 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1500 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1502 static void tpacpi_hotkey_send_key(unsigned int scancode)
1504 tpacpi_input_send_key(scancode);
1505 if (hotkey_report_mode < 2) {
1506 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1507 0x80, 0x1001 + scancode);
1511 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1513 u8 d;
1515 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1516 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1517 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1518 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1519 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1520 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1522 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1523 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1524 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1526 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1527 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1528 n->displayexp_toggle =
1529 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1531 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1532 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1533 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1534 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1535 n->brightness_toggle =
1536 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1538 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1539 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1540 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1541 >> TP_NVRAM_POS_LEVEL_VOLUME;
1542 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1543 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1547 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1548 do { \
1549 if ((mask & (1 << __scancode)) && \
1550 oldn->__member != newn->__member) \
1551 tpacpi_hotkey_send_key(__scancode); \
1552 } while (0)
1554 #define TPACPI_MAY_SEND_KEY(__scancode) \
1555 do { if (mask & (1 << __scancode)) \
1556 tpacpi_hotkey_send_key(__scancode); } while (0)
1558 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1559 struct tp_nvram_state *newn,
1560 u32 mask)
1562 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1563 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1564 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1565 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1567 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1569 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1571 /* handle volume */
1572 if (oldn->volume_toggle != newn->volume_toggle) {
1573 if (oldn->mute != newn->mute) {
1574 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1576 if (oldn->volume_level > newn->volume_level) {
1577 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1578 } else if (oldn->volume_level < newn->volume_level) {
1579 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1580 } else if (oldn->mute == newn->mute) {
1581 /* repeated key presses that didn't change state */
1582 if (newn->mute) {
1583 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1584 } else if (newn->volume_level != 0) {
1585 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1586 } else {
1587 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1592 /* handle brightness */
1593 if (oldn->brightness_toggle != newn->brightness_toggle) {
1594 if (oldn->brightness_level < newn->brightness_level) {
1595 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1596 } else if (oldn->brightness_level > newn->brightness_level) {
1597 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1598 } else {
1599 /* repeated key presses that didn't change state */
1600 if (newn->brightness_level != 0) {
1601 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1602 } else {
1603 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1609 #undef TPACPI_COMPARE_KEY
1610 #undef TPACPI_MAY_SEND_KEY
1612 static int hotkey_kthread(void *data)
1614 struct tp_nvram_state s[2];
1615 u32 mask;
1616 unsigned int si, so;
1617 unsigned long t;
1618 unsigned int change_detector, must_reset;
1620 mutex_lock(&hotkey_thread_mutex);
1622 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1623 goto exit;
1625 set_freezable();
1627 so = 0;
1628 si = 1;
1629 t = 0;
1631 /* Initial state for compares */
1632 mutex_lock(&hotkey_thread_data_mutex);
1633 change_detector = hotkey_config_change;
1634 mask = hotkey_source_mask & hotkey_mask;
1635 mutex_unlock(&hotkey_thread_data_mutex);
1636 hotkey_read_nvram(&s[so], mask);
1638 while (!kthread_should_stop() && hotkey_poll_freq) {
1639 if (t == 0)
1640 t = 1000/hotkey_poll_freq;
1641 t = msleep_interruptible(t);
1642 if (unlikely(kthread_should_stop()))
1643 break;
1644 must_reset = try_to_freeze();
1645 if (t > 0 && !must_reset)
1646 continue;
1648 mutex_lock(&hotkey_thread_data_mutex);
1649 if (must_reset || hotkey_config_change != change_detector) {
1650 /* forget old state on thaw or config change */
1651 si = so;
1652 t = 0;
1653 change_detector = hotkey_config_change;
1655 mask = hotkey_source_mask & hotkey_mask;
1656 mutex_unlock(&hotkey_thread_data_mutex);
1658 if (likely(mask)) {
1659 hotkey_read_nvram(&s[si], mask);
1660 if (likely(si != so)) {
1661 hotkey_compare_and_issue_event(&s[so], &s[si],
1662 mask);
1666 so = si;
1667 si ^= 1;
1670 exit:
1671 mutex_unlock(&hotkey_thread_mutex);
1672 return 0;
1675 static void hotkey_poll_stop_sync(void)
1677 if (tpacpi_hotkey_task) {
1678 if (frozen(tpacpi_hotkey_task) ||
1679 freezing(tpacpi_hotkey_task))
1680 thaw_process(tpacpi_hotkey_task);
1682 kthread_stop(tpacpi_hotkey_task);
1683 tpacpi_hotkey_task = NULL;
1684 mutex_lock(&hotkey_thread_mutex);
1685 /* at this point, the thread did exit */
1686 mutex_unlock(&hotkey_thread_mutex);
1690 /* call with hotkey_mutex held */
1691 static void hotkey_poll_setup(int may_warn)
1693 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1694 hotkey_poll_freq > 0 &&
1695 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1696 if (!tpacpi_hotkey_task) {
1697 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1698 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1699 if (IS_ERR(tpacpi_hotkey_task)) {
1700 tpacpi_hotkey_task = NULL;
1701 printk(TPACPI_ERR
1702 "could not create kernel thread "
1703 "for hotkey polling\n");
1706 } else {
1707 hotkey_poll_stop_sync();
1708 if (may_warn &&
1709 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1710 printk(TPACPI_NOTICE
1711 "hot keys 0x%08x require polling, "
1712 "which is currently disabled\n",
1713 hotkey_source_mask);
1718 static void hotkey_poll_setup_safe(int may_warn)
1720 mutex_lock(&hotkey_mutex);
1721 hotkey_poll_setup(may_warn);
1722 mutex_unlock(&hotkey_mutex);
1725 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1727 static void hotkey_poll_setup_safe(int __unused)
1731 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1733 static int hotkey_inputdev_open(struct input_dev *dev)
1735 switch (tpacpi_lifecycle) {
1736 case TPACPI_LIFE_INIT:
1738 * hotkey_init will call hotkey_poll_setup_safe
1739 * at the appropriate moment
1741 return 0;
1742 case TPACPI_LIFE_EXITING:
1743 return -EBUSY;
1744 case TPACPI_LIFE_RUNNING:
1745 hotkey_poll_setup_safe(0);
1746 return 0;
1749 /* Should only happen if tpacpi_lifecycle is corrupt */
1750 BUG();
1751 return -EBUSY;
1754 static void hotkey_inputdev_close(struct input_dev *dev)
1756 /* disable hotkey polling when possible */
1757 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1758 hotkey_poll_setup_safe(0);
1761 /* sysfs hotkey enable ------------------------------------------------- */
1762 static ssize_t hotkey_enable_show(struct device *dev,
1763 struct device_attribute *attr,
1764 char *buf)
1766 int res, status;
1768 res = hotkey_status_get(&status);
1769 if (res)
1770 return res;
1772 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1775 static ssize_t hotkey_enable_store(struct device *dev,
1776 struct device_attribute *attr,
1777 const char *buf, size_t count)
1779 unsigned long t;
1780 int res;
1782 if (parse_strtoul(buf, 1, &t))
1783 return -EINVAL;
1785 res = hotkey_status_set(t);
1787 return (res) ? res : count;
1790 static struct device_attribute dev_attr_hotkey_enable =
1791 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1792 hotkey_enable_show, hotkey_enable_store);
1794 /* sysfs hotkey mask --------------------------------------------------- */
1795 static ssize_t hotkey_mask_show(struct device *dev,
1796 struct device_attribute *attr,
1797 char *buf)
1799 int res;
1801 if (mutex_lock_interruptible(&hotkey_mutex))
1802 return -ERESTARTSYS;
1803 res = hotkey_mask_get();
1804 mutex_unlock(&hotkey_mutex);
1806 return (res)?
1807 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1810 static ssize_t hotkey_mask_store(struct device *dev,
1811 struct device_attribute *attr,
1812 const char *buf, size_t count)
1814 unsigned long t;
1815 int res;
1817 if (parse_strtoul(buf, 0xffffffffUL, &t))
1818 return -EINVAL;
1820 if (mutex_lock_interruptible(&hotkey_mutex))
1821 return -ERESTARTSYS;
1823 res = hotkey_mask_set(t);
1825 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1826 hotkey_poll_setup(1);
1827 #endif
1829 mutex_unlock(&hotkey_mutex);
1831 return (res) ? res : count;
1834 static struct device_attribute dev_attr_hotkey_mask =
1835 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1836 hotkey_mask_show, hotkey_mask_store);
1838 /* sysfs hotkey bios_enabled ------------------------------------------- */
1839 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1840 struct device_attribute *attr,
1841 char *buf)
1843 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1846 static struct device_attribute dev_attr_hotkey_bios_enabled =
1847 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1849 /* sysfs hotkey bios_mask ---------------------------------------------- */
1850 static ssize_t hotkey_bios_mask_show(struct device *dev,
1851 struct device_attribute *attr,
1852 char *buf)
1854 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1857 static struct device_attribute dev_attr_hotkey_bios_mask =
1858 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1860 /* sysfs hotkey all_mask ----------------------------------------------- */
1861 static ssize_t hotkey_all_mask_show(struct device *dev,
1862 struct device_attribute *attr,
1863 char *buf)
1865 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1866 hotkey_all_mask | hotkey_source_mask);
1869 static struct device_attribute dev_attr_hotkey_all_mask =
1870 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1872 /* sysfs hotkey recommended_mask --------------------------------------- */
1873 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1874 struct device_attribute *attr,
1875 char *buf)
1877 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1878 (hotkey_all_mask | hotkey_source_mask)
1879 & ~hotkey_reserved_mask);
1882 static struct device_attribute dev_attr_hotkey_recommended_mask =
1883 __ATTR(hotkey_recommended_mask, S_IRUGO,
1884 hotkey_recommended_mask_show, NULL);
1886 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1888 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1889 static ssize_t hotkey_source_mask_show(struct device *dev,
1890 struct device_attribute *attr,
1891 char *buf)
1893 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1896 static ssize_t hotkey_source_mask_store(struct device *dev,
1897 struct device_attribute *attr,
1898 const char *buf, size_t count)
1900 unsigned long t;
1902 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1903 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1904 return -EINVAL;
1906 if (mutex_lock_interruptible(&hotkey_mutex))
1907 return -ERESTARTSYS;
1909 HOTKEY_CONFIG_CRITICAL_START
1910 hotkey_source_mask = t;
1911 HOTKEY_CONFIG_CRITICAL_END
1913 hotkey_poll_setup(1);
1915 mutex_unlock(&hotkey_mutex);
1917 return count;
1920 static struct device_attribute dev_attr_hotkey_source_mask =
1921 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1922 hotkey_source_mask_show, hotkey_source_mask_store);
1924 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1925 static ssize_t hotkey_poll_freq_show(struct device *dev,
1926 struct device_attribute *attr,
1927 char *buf)
1929 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1932 static ssize_t hotkey_poll_freq_store(struct device *dev,
1933 struct device_attribute *attr,
1934 const char *buf, size_t count)
1936 unsigned long t;
1938 if (parse_strtoul(buf, 25, &t))
1939 return -EINVAL;
1941 if (mutex_lock_interruptible(&hotkey_mutex))
1942 return -ERESTARTSYS;
1944 hotkey_poll_freq = t;
1946 hotkey_poll_setup(1);
1947 mutex_unlock(&hotkey_mutex);
1949 return count;
1952 static struct device_attribute dev_attr_hotkey_poll_freq =
1953 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1954 hotkey_poll_freq_show, hotkey_poll_freq_store);
1956 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1958 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1959 static ssize_t hotkey_radio_sw_show(struct device *dev,
1960 struct device_attribute *attr,
1961 char *buf)
1963 int res, s;
1964 res = hotkey_get_wlsw(&s);
1965 if (res < 0)
1966 return res;
1968 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1971 static struct device_attribute dev_attr_hotkey_radio_sw =
1972 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1974 static void hotkey_radio_sw_notify_change(void)
1976 if (tp_features.hotkey_wlsw)
1977 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1978 "hotkey_radio_sw");
1981 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1982 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1983 struct device_attribute *attr,
1984 char *buf)
1986 int res, s;
1987 res = hotkey_get_tablet_mode(&s);
1988 if (res < 0)
1989 return res;
1991 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1994 static struct device_attribute dev_attr_hotkey_tablet_mode =
1995 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1997 static void hotkey_tablet_mode_notify_change(void)
1999 if (tp_features.hotkey_tablet)
2000 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2001 "hotkey_tablet_mode");
2004 /* sysfs hotkey report_mode -------------------------------------------- */
2005 static ssize_t hotkey_report_mode_show(struct device *dev,
2006 struct device_attribute *attr,
2007 char *buf)
2009 return snprintf(buf, PAGE_SIZE, "%d\n",
2010 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2013 static struct device_attribute dev_attr_hotkey_report_mode =
2014 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2016 /* sysfs wakeup reason (pollable) -------------------------------------- */
2017 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2018 struct device_attribute *attr,
2019 char *buf)
2021 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2024 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2025 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2027 static void hotkey_wakeup_reason_notify_change(void)
2029 if (tp_features.hotkey_mask)
2030 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2031 "wakeup_reason");
2034 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2035 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2036 struct device_attribute *attr,
2037 char *buf)
2039 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2042 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2043 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2044 hotkey_wakeup_hotunplug_complete_show, NULL);
2046 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2048 if (tp_features.hotkey_mask)
2049 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2050 "wakeup_hotunplug_complete");
2053 /* --------------------------------------------------------------------- */
2055 static struct attribute *hotkey_attributes[] __initdata = {
2056 &dev_attr_hotkey_enable.attr,
2057 &dev_attr_hotkey_bios_enabled.attr,
2058 &dev_attr_hotkey_report_mode.attr,
2059 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2060 &dev_attr_hotkey_mask.attr,
2061 &dev_attr_hotkey_all_mask.attr,
2062 &dev_attr_hotkey_recommended_mask.attr,
2063 &dev_attr_hotkey_source_mask.attr,
2064 &dev_attr_hotkey_poll_freq.attr,
2065 #endif
2068 static struct attribute *hotkey_mask_attributes[] __initdata = {
2069 &dev_attr_hotkey_bios_mask.attr,
2070 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2071 &dev_attr_hotkey_mask.attr,
2072 &dev_attr_hotkey_all_mask.attr,
2073 &dev_attr_hotkey_recommended_mask.attr,
2074 #endif
2075 &dev_attr_hotkey_wakeup_reason.attr,
2076 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2079 static void bluetooth_update_rfk(void);
2080 static void wan_update_rfk(void);
2081 static void tpacpi_send_radiosw_update(void)
2083 int wlsw;
2085 /* Sync these BEFORE sending any rfkill events */
2086 if (tp_features.bluetooth)
2087 bluetooth_update_rfk();
2088 if (tp_features.wan)
2089 wan_update_rfk();
2091 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2092 mutex_lock(&tpacpi_inputdev_send_mutex);
2094 input_report_switch(tpacpi_inputdev,
2095 SW_RFKILL_ALL, !!wlsw);
2096 input_sync(tpacpi_inputdev);
2098 mutex_unlock(&tpacpi_inputdev_send_mutex);
2100 hotkey_radio_sw_notify_change();
2103 static void hotkey_exit(void)
2105 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2106 hotkey_poll_stop_sync();
2107 #endif
2109 if (hotkey_dev_attributes)
2110 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2112 kfree(hotkey_keycode_map);
2114 if (tp_features.hotkey) {
2115 dbg_printk(TPACPI_DBG_EXIT,
2116 "restoring original hot key mask\n");
2117 /* no short-circuit boolean operator below! */
2118 if ((hotkey_mask_set(hotkey_orig_mask) |
2119 hotkey_status_set(hotkey_orig_status)) != 0)
2120 printk(TPACPI_ERR
2121 "failed to restore hot key mask "
2122 "to BIOS defaults\n");
2126 static int __init hotkey_init(struct ibm_init_struct *iibm)
2128 /* Requirements for changing the default keymaps:
2130 * 1. Many of the keys are mapped to KEY_RESERVED for very
2131 * good reasons. Do not change them unless you have deep
2132 * knowledge on the IBM and Lenovo ThinkPad firmware for
2133 * the various ThinkPad models. The driver behaves
2134 * differently for KEY_RESERVED: such keys have their
2135 * hot key mask *unset* in mask_recommended, and also
2136 * in the initial hot key mask programmed into the
2137 * firmware at driver load time, which means the firm-
2138 * ware may react very differently if you change them to
2139 * something else;
2141 * 2. You must be subscribed to the linux-thinkpad and
2142 * ibm-acpi-devel mailing lists, and you should read the
2143 * list archives since 2007 if you want to change the
2144 * keymaps. This requirement exists so that you will
2145 * know the past history of problems with the thinkpad-
2146 * acpi driver keymaps, and also that you will be
2147 * listening to any bug reports;
2149 * 3. Do not send thinkpad-acpi specific patches directly to
2150 * for merging, *ever*. Send them to the linux-acpi
2151 * mailinglist for comments. Merging is to be done only
2152 * through acpi-test and the ACPI maintainer.
2154 * If the above is too much to ask, don't change the keymap.
2155 * Ask the thinkpad-acpi maintainer to do it, instead.
2157 static u16 ibm_keycode_map[] __initdata = {
2158 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2159 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2160 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2161 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2163 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2164 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2165 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2166 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2168 /* brightness: firmware always reacts to them, unless
2169 * X.org did some tricks in the radeon BIOS scratch
2170 * registers of *some* models */
2171 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2172 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2174 /* Thinklight: firmware always react to it */
2175 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2177 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2178 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2180 /* Volume: firmware always react to it and reprograms
2181 * the built-in *extra* mixer. Never map it to control
2182 * another mixer by default. */
2183 KEY_RESERVED, /* 0x14: VOLUME UP */
2184 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2185 KEY_RESERVED, /* 0x16: MUTE */
2187 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2189 /* (assignments unknown, please report if found) */
2190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2193 static u16 lenovo_keycode_map[] __initdata = {
2194 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2195 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2196 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2197 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2199 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2200 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2201 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2202 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2204 /* These either have to go through ACPI video, or
2205 * act like in the IBM ThinkPads, so don't ever
2206 * enable them by default */
2207 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2208 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2210 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2212 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2213 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2215 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2216 * react to it and reprograms the built-in *extra* mixer.
2217 * Never map it to control another mixer by default.
2219 * T60?, T61, R60?, R61: firmware and EC tries to send
2220 * these over the regular keyboard, so these are no-ops,
2221 * but there are still weird bugs re. MUTE, so do not
2222 * change unless you get test reports from all Lenovo
2223 * models. May cause the BIOS to interfere with the
2224 * HDA mixer.
2226 KEY_RESERVED, /* 0x14: VOLUME UP */
2227 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2228 KEY_RESERVED, /* 0x16: MUTE */
2230 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2232 /* (assignments unknown, please report if found) */
2233 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2234 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2237 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2238 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2239 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2241 int res, i;
2242 int status;
2243 int hkeyv;
2245 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2247 BUG_ON(!tpacpi_inputdev);
2248 BUG_ON(tpacpi_inputdev->open != NULL ||
2249 tpacpi_inputdev->close != NULL);
2251 TPACPI_ACPIHANDLE_INIT(hkey);
2252 mutex_init(&hotkey_mutex);
2254 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2255 mutex_init(&hotkey_thread_mutex);
2256 mutex_init(&hotkey_thread_data_mutex);
2257 #endif
2259 /* hotkey not supported on 570 */
2260 tp_features.hotkey = hkey_handle != NULL;
2262 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2263 str_supported(tp_features.hotkey));
2265 if (!tp_features.hotkey)
2266 return 1;
2268 tpacpi_disable_brightness_delay();
2270 hotkey_dev_attributes = create_attr_set(13, NULL);
2271 if (!hotkey_dev_attributes)
2272 return -ENOMEM;
2273 res = add_many_to_attr_set(hotkey_dev_attributes,
2274 hotkey_attributes,
2275 ARRAY_SIZE(hotkey_attributes));
2276 if (res)
2277 goto err_exit;
2279 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2280 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2281 for HKEY interface version 0x100 */
2282 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2283 if ((hkeyv >> 8) != 1) {
2284 printk(TPACPI_ERR "unknown version of the "
2285 "HKEY interface: 0x%x\n", hkeyv);
2286 printk(TPACPI_ERR "please report this to %s\n",
2287 TPACPI_MAIL);
2288 } else {
2290 * MHKV 0x100 in A31, R40, R40e,
2291 * T4x, X31, and later
2293 tp_features.hotkey_mask = 1;
2297 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2298 str_supported(tp_features.hotkey_mask));
2300 if (tp_features.hotkey_mask) {
2301 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2302 "MHKA", "qd")) {
2303 printk(TPACPI_ERR
2304 "missing MHKA handler, "
2305 "please report this to %s\n",
2306 TPACPI_MAIL);
2307 /* FN+F12, FN+F4, FN+F3 */
2308 hotkey_all_mask = 0x080cU;
2312 /* hotkey_source_mask *must* be zero for
2313 * the first hotkey_mask_get */
2314 res = hotkey_status_get(&hotkey_orig_status);
2315 if (res)
2316 goto err_exit;
2318 if (tp_features.hotkey_mask) {
2319 res = hotkey_mask_get();
2320 if (res)
2321 goto err_exit;
2323 hotkey_orig_mask = hotkey_mask;
2324 res = add_many_to_attr_set(
2325 hotkey_dev_attributes,
2326 hotkey_mask_attributes,
2327 ARRAY_SIZE(hotkey_mask_attributes));
2328 if (res)
2329 goto err_exit;
2332 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2333 if (tp_features.hotkey_mask) {
2334 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2335 & ~hotkey_all_mask;
2336 } else {
2337 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2340 vdbg_printk(TPACPI_DBG_INIT,
2341 "hotkey source mask 0x%08x, polling freq %d\n",
2342 hotkey_source_mask, hotkey_poll_freq);
2343 #endif
2345 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2346 if (dbg_wlswemul) {
2347 tp_features.hotkey_wlsw = 1;
2348 printk(TPACPI_INFO
2349 "radio switch emulation enabled\n");
2350 } else
2351 #endif
2352 /* Not all thinkpads have a hardware radio switch */
2353 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2354 tp_features.hotkey_wlsw = 1;
2355 printk(TPACPI_INFO
2356 "radio switch found; radios are %s\n",
2357 enabled(status, 0));
2359 if (tp_features.hotkey_wlsw)
2360 res = add_to_attr_set(hotkey_dev_attributes,
2361 &dev_attr_hotkey_radio_sw.attr);
2363 /* For X41t, X60t, X61t Tablets... */
2364 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2365 tp_features.hotkey_tablet = 1;
2366 printk(TPACPI_INFO
2367 "possible tablet mode switch found; "
2368 "ThinkPad in %s mode\n",
2369 (status & TP_HOTKEY_TABLET_MASK)?
2370 "tablet" : "laptop");
2371 res = add_to_attr_set(hotkey_dev_attributes,
2372 &dev_attr_hotkey_tablet_mode.attr);
2375 if (!res)
2376 res = register_attr_set_with_sysfs(
2377 hotkey_dev_attributes,
2378 &tpacpi_pdev->dev.kobj);
2379 if (res)
2380 goto err_exit;
2382 /* Set up key map */
2384 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2385 GFP_KERNEL);
2386 if (!hotkey_keycode_map) {
2387 printk(TPACPI_ERR
2388 "failed to allocate memory for key map\n");
2389 res = -ENOMEM;
2390 goto err_exit;
2393 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2394 dbg_printk(TPACPI_DBG_INIT,
2395 "using Lenovo default hot key map\n");
2396 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2397 TPACPI_HOTKEY_MAP_SIZE);
2398 } else {
2399 dbg_printk(TPACPI_DBG_INIT,
2400 "using IBM default hot key map\n");
2401 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2402 TPACPI_HOTKEY_MAP_SIZE);
2405 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2406 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2407 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2408 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2409 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2410 tpacpi_inputdev->keycode = hotkey_keycode_map;
2411 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2412 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2413 set_bit(hotkey_keycode_map[i],
2414 tpacpi_inputdev->keybit);
2415 } else {
2416 if (i < sizeof(hotkey_reserved_mask)*8)
2417 hotkey_reserved_mask |= 1 << i;
2421 if (tp_features.hotkey_wlsw) {
2422 set_bit(EV_SW, tpacpi_inputdev->evbit);
2423 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2425 if (tp_features.hotkey_tablet) {
2426 set_bit(EV_SW, tpacpi_inputdev->evbit);
2427 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2430 /* Do not issue duplicate brightness change events to
2431 * userspace */
2432 if (!tp_features.bright_acpimode)
2433 /* update bright_acpimode... */
2434 tpacpi_check_std_acpi_brightness_support();
2436 if (tp_features.bright_acpimode) {
2437 printk(TPACPI_INFO
2438 "This ThinkPad has standard ACPI backlight "
2439 "brightness control, supported by the ACPI "
2440 "video driver\n");
2441 printk(TPACPI_NOTICE
2442 "Disabling thinkpad-acpi brightness events "
2443 "by default...\n");
2445 /* The hotkey_reserved_mask change below is not
2446 * necessary while the keys are at KEY_RESERVED in the
2447 * default map, but better safe than sorry, leave it
2448 * here as a marker of what we have to do, especially
2449 * when we finally become able to set this at runtime
2450 * on response to X.org requests */
2451 hotkey_reserved_mask |=
2452 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2453 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2456 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2457 res = hotkey_status_set(1);
2458 if (res) {
2459 hotkey_exit();
2460 return res;
2462 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2463 & ~hotkey_reserved_mask)
2464 | hotkey_orig_mask);
2465 if (res < 0 && res != -ENXIO) {
2466 hotkey_exit();
2467 return res;
2470 dbg_printk(TPACPI_DBG_INIT,
2471 "legacy hot key reporting over procfs %s\n",
2472 (hotkey_report_mode < 2) ?
2473 "enabled" : "disabled");
2475 tpacpi_inputdev->open = &hotkey_inputdev_open;
2476 tpacpi_inputdev->close = &hotkey_inputdev_close;
2478 hotkey_poll_setup_safe(1);
2479 tpacpi_send_radiosw_update();
2480 tpacpi_input_send_tabletsw();
2482 return 0;
2484 err_exit:
2485 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2486 hotkey_dev_attributes = NULL;
2488 return (res < 0)? res : 1;
2491 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2493 u32 hkey;
2494 unsigned int scancode;
2495 int send_acpi_ev;
2496 int ignore_acpi_ev;
2497 int unk_ev;
2499 if (event != 0x80) {
2500 printk(TPACPI_ERR
2501 "unknown HKEY notification event %d\n", event);
2502 /* forward it to userspace, maybe it knows how to handle it */
2503 acpi_bus_generate_netlink_event(
2504 ibm->acpi->device->pnp.device_class,
2505 dev_name(&ibm->acpi->device->dev),
2506 event, 0);
2507 return;
2510 while (1) {
2511 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2512 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2513 return;
2516 if (hkey == 0) {
2517 /* queue empty */
2518 return;
2521 send_acpi_ev = 1;
2522 ignore_acpi_ev = 0;
2523 unk_ev = 0;
2525 switch (hkey >> 12) {
2526 case 1:
2527 /* 0x1000-0x1FFF: key presses */
2528 scancode = hkey & 0xfff;
2529 if (scancode > 0 && scancode < 0x21) {
2530 scancode--;
2531 if (!(hotkey_source_mask & (1 << scancode))) {
2532 tpacpi_input_send_key(scancode);
2533 send_acpi_ev = 0;
2534 } else {
2535 ignore_acpi_ev = 1;
2537 } else {
2538 unk_ev = 1;
2540 break;
2541 case 2:
2542 /* Wakeup reason */
2543 switch (hkey) {
2544 case 0x2304: /* suspend, undock */
2545 case 0x2404: /* hibernation, undock */
2546 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2547 ignore_acpi_ev = 1;
2548 break;
2549 case 0x2305: /* suspend, bay eject */
2550 case 0x2405: /* hibernation, bay eject */
2551 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2552 ignore_acpi_ev = 1;
2553 break;
2554 default:
2555 unk_ev = 1;
2557 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2558 printk(TPACPI_INFO
2559 "woke up due to a hot-unplug "
2560 "request...\n");
2561 hotkey_wakeup_reason_notify_change();
2563 break;
2564 case 3:
2565 /* bay-related wakeups */
2566 if (hkey == 0x3003) {
2567 hotkey_autosleep_ack = 1;
2568 printk(TPACPI_INFO
2569 "bay ejected\n");
2570 hotkey_wakeup_hotunplug_complete_notify_change();
2571 } else {
2572 unk_ev = 1;
2574 break;
2575 case 4:
2576 /* dock-related wakeups */
2577 if (hkey == 0x4003) {
2578 hotkey_autosleep_ack = 1;
2579 printk(TPACPI_INFO
2580 "undocked\n");
2581 hotkey_wakeup_hotunplug_complete_notify_change();
2582 } else {
2583 unk_ev = 1;
2585 break;
2586 case 5:
2587 /* 0x5000-0x5FFF: human interface helpers */
2588 switch (hkey) {
2589 case 0x5010: /* Lenovo new BIOS: brightness changed */
2590 case 0x500b: /* X61t: tablet pen inserted into bay */
2591 case 0x500c: /* X61t: tablet pen removed from bay */
2592 break;
2593 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2594 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2595 tpacpi_input_send_tabletsw();
2596 hotkey_tablet_mode_notify_change();
2597 send_acpi_ev = 0;
2598 break;
2599 case 0x5001:
2600 case 0x5002:
2601 /* LID switch events. Do not propagate */
2602 ignore_acpi_ev = 1;
2603 break;
2604 default:
2605 unk_ev = 1;
2607 break;
2608 case 7:
2609 /* 0x7000-0x7FFF: misc */
2610 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2611 tpacpi_send_radiosw_update();
2612 send_acpi_ev = 0;
2613 break;
2615 /* fallthrough to default */
2616 default:
2617 unk_ev = 1;
2619 if (unk_ev) {
2620 printk(TPACPI_NOTICE
2621 "unhandled HKEY event 0x%04x\n", hkey);
2624 /* Legacy events */
2625 if (!ignore_acpi_ev &&
2626 (send_acpi_ev || hotkey_report_mode < 2)) {
2627 acpi_bus_generate_proc_event(ibm->acpi->device,
2628 event, hkey);
2631 /* netlink events */
2632 if (!ignore_acpi_ev && send_acpi_ev) {
2633 acpi_bus_generate_netlink_event(
2634 ibm->acpi->device->pnp.device_class,
2635 dev_name(&ibm->acpi->device->dev),
2636 event, hkey);
2641 static void hotkey_suspend(pm_message_t state)
2643 /* Do these on suspend, we get the events on early resume! */
2644 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2645 hotkey_autosleep_ack = 0;
2648 static void hotkey_resume(void)
2650 tpacpi_disable_brightness_delay();
2652 if (hotkey_mask_get())
2653 printk(TPACPI_ERR
2654 "error while trying to read hot key mask "
2655 "from firmware\n");
2656 tpacpi_send_radiosw_update();
2657 hotkey_tablet_mode_notify_change();
2658 hotkey_wakeup_reason_notify_change();
2659 hotkey_wakeup_hotunplug_complete_notify_change();
2660 hotkey_poll_setup_safe(0);
2663 /* procfs -------------------------------------------------------------- */
2664 static int hotkey_read(char *p)
2666 int res, status;
2667 int len = 0;
2669 if (!tp_features.hotkey) {
2670 len += sprintf(p + len, "status:\t\tnot supported\n");
2671 return len;
2674 if (mutex_lock_interruptible(&hotkey_mutex))
2675 return -ERESTARTSYS;
2676 res = hotkey_status_get(&status);
2677 if (!res)
2678 res = hotkey_mask_get();
2679 mutex_unlock(&hotkey_mutex);
2680 if (res)
2681 return res;
2683 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2684 if (tp_features.hotkey_mask) {
2685 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2686 len += sprintf(p + len,
2687 "commands:\tenable, disable, reset, <mask>\n");
2688 } else {
2689 len += sprintf(p + len, "mask:\t\tnot supported\n");
2690 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2693 return len;
2696 static int hotkey_write(char *buf)
2698 int res, status;
2699 u32 mask;
2700 char *cmd;
2702 if (!tp_features.hotkey)
2703 return -ENODEV;
2705 if (mutex_lock_interruptible(&hotkey_mutex))
2706 return -ERESTARTSYS;
2708 status = -1;
2709 mask = hotkey_mask;
2711 res = 0;
2712 while ((cmd = next_cmd(&buf))) {
2713 if (strlencmp(cmd, "enable") == 0) {
2714 status = 1;
2715 } else if (strlencmp(cmd, "disable") == 0) {
2716 status = 0;
2717 } else if (strlencmp(cmd, "reset") == 0) {
2718 status = hotkey_orig_status;
2719 mask = hotkey_orig_mask;
2720 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2721 /* mask set */
2722 } else if (sscanf(cmd, "%x", &mask) == 1) {
2723 /* mask set */
2724 } else {
2725 res = -EINVAL;
2726 goto errexit;
2729 if (status != -1)
2730 res = hotkey_status_set(status);
2732 if (!res && mask != hotkey_mask)
2733 res = hotkey_mask_set(mask);
2735 errexit:
2736 mutex_unlock(&hotkey_mutex);
2737 return res;
2740 static const struct acpi_device_id ibm_htk_device_ids[] = {
2741 {TPACPI_ACPI_HKEY_HID, 0},
2742 {"", 0},
2745 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2746 .hid = ibm_htk_device_ids,
2747 .notify = hotkey_notify,
2748 .handle = &hkey_handle,
2749 .type = ACPI_DEVICE_NOTIFY,
2752 static struct ibm_struct hotkey_driver_data = {
2753 .name = "hotkey",
2754 .read = hotkey_read,
2755 .write = hotkey_write,
2756 .exit = hotkey_exit,
2757 .resume = hotkey_resume,
2758 .suspend = hotkey_suspend,
2759 .acpi = &ibm_hotkey_acpidriver,
2762 /*************************************************************************
2763 * Bluetooth subdriver
2766 enum {
2767 /* ACPI GBDC/SBDC bits */
2768 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2769 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2770 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2773 static struct rfkill *tpacpi_bluetooth_rfkill;
2775 static int bluetooth_get_radiosw(void)
2777 int status;
2779 if (!tp_features.bluetooth)
2780 return -ENODEV;
2782 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2783 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2784 return RFKILL_STATE_HARD_BLOCKED;
2786 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2787 if (dbg_bluetoothemul)
2788 return (tpacpi_bluetooth_emulstate) ?
2789 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2790 #endif
2792 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2793 return -EIO;
2795 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2796 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2799 static void bluetooth_update_rfk(void)
2801 int status;
2803 if (!tpacpi_bluetooth_rfkill)
2804 return;
2806 status = bluetooth_get_radiosw();
2807 if (status < 0)
2808 return;
2809 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2812 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2814 int status;
2816 if (!tp_features.bluetooth)
2817 return -ENODEV;
2819 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2820 * reason to risk weird behaviour. */
2821 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2822 && radio_on)
2823 return -EPERM;
2825 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2826 if (dbg_bluetoothemul) {
2827 tpacpi_bluetooth_emulstate = !!radio_on;
2828 if (update_rfk)
2829 bluetooth_update_rfk();
2830 return 0;
2832 #endif
2834 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2835 return -EIO;
2836 if (radio_on)
2837 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2838 else
2839 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2840 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2841 return -EIO;
2843 if (update_rfk)
2844 bluetooth_update_rfk();
2846 return 0;
2849 /* sysfs bluetooth enable ---------------------------------------------- */
2850 static ssize_t bluetooth_enable_show(struct device *dev,
2851 struct device_attribute *attr,
2852 char *buf)
2854 int status;
2856 status = bluetooth_get_radiosw();
2857 if (status < 0)
2858 return status;
2860 return snprintf(buf, PAGE_SIZE, "%d\n",
2861 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2864 static ssize_t bluetooth_enable_store(struct device *dev,
2865 struct device_attribute *attr,
2866 const char *buf, size_t count)
2868 unsigned long t;
2869 int res;
2871 if (parse_strtoul(buf, 1, &t))
2872 return -EINVAL;
2874 res = bluetooth_set_radiosw(t, 1);
2876 return (res) ? res : count;
2879 static struct device_attribute dev_attr_bluetooth_enable =
2880 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2881 bluetooth_enable_show, bluetooth_enable_store);
2883 /* --------------------------------------------------------------------- */
2885 static struct attribute *bluetooth_attributes[] = {
2886 &dev_attr_bluetooth_enable.attr,
2887 NULL
2890 static const struct attribute_group bluetooth_attr_group = {
2891 .attrs = bluetooth_attributes,
2894 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2896 int bts = bluetooth_get_radiosw();
2898 if (bts < 0)
2899 return bts;
2901 *state = bts;
2902 return 0;
2905 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2907 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2910 static void bluetooth_exit(void)
2912 if (tpacpi_bluetooth_rfkill)
2913 rfkill_unregister(tpacpi_bluetooth_rfkill);
2915 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2916 &bluetooth_attr_group);
2919 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2921 int res;
2922 int status = 0;
2924 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2926 TPACPI_ACPIHANDLE_INIT(hkey);
2928 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2929 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2930 tp_features.bluetooth = hkey_handle &&
2931 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2933 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2934 str_supported(tp_features.bluetooth),
2935 status);
2937 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2938 if (dbg_bluetoothemul) {
2939 tp_features.bluetooth = 1;
2940 printk(TPACPI_INFO
2941 "bluetooth switch emulation enabled\n");
2942 } else
2943 #endif
2944 if (tp_features.bluetooth &&
2945 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2946 /* no bluetooth hardware present in system */
2947 tp_features.bluetooth = 0;
2948 dbg_printk(TPACPI_DBG_INIT,
2949 "bluetooth hardware not installed\n");
2952 if (!tp_features.bluetooth)
2953 return 1;
2955 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2956 &bluetooth_attr_group);
2957 if (res)
2958 return res;
2960 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2961 &tpacpi_bluetooth_rfkill,
2962 RFKILL_TYPE_BLUETOOTH,
2963 "tpacpi_bluetooth_sw",
2964 tpacpi_bluetooth_rfk_set,
2965 tpacpi_bluetooth_rfk_get);
2966 if (res) {
2967 bluetooth_exit();
2968 return res;
2971 return 0;
2974 /* procfs -------------------------------------------------------------- */
2975 static int bluetooth_read(char *p)
2977 int len = 0;
2978 int status = bluetooth_get_radiosw();
2980 if (!tp_features.bluetooth)
2981 len += sprintf(p + len, "status:\t\tnot supported\n");
2982 else {
2983 len += sprintf(p + len, "status:\t\t%s\n",
2984 (status == RFKILL_STATE_UNBLOCKED) ?
2985 "enabled" : "disabled");
2986 len += sprintf(p + len, "commands:\tenable, disable\n");
2989 return len;
2992 static int bluetooth_write(char *buf)
2994 char *cmd;
2996 if (!tp_features.bluetooth)
2997 return -ENODEV;
2999 while ((cmd = next_cmd(&buf))) {
3000 if (strlencmp(cmd, "enable") == 0) {
3001 bluetooth_set_radiosw(1, 1);
3002 } else if (strlencmp(cmd, "disable") == 0) {
3003 bluetooth_set_radiosw(0, 1);
3004 } else
3005 return -EINVAL;
3008 return 0;
3011 static struct ibm_struct bluetooth_driver_data = {
3012 .name = "bluetooth",
3013 .read = bluetooth_read,
3014 .write = bluetooth_write,
3015 .exit = bluetooth_exit,
3018 /*************************************************************************
3019 * Wan subdriver
3022 enum {
3023 /* ACPI GWAN/SWAN bits */
3024 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3025 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3026 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
3029 static struct rfkill *tpacpi_wan_rfkill;
3031 static int wan_get_radiosw(void)
3033 int status;
3035 if (!tp_features.wan)
3036 return -ENODEV;
3038 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3039 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3040 return RFKILL_STATE_HARD_BLOCKED;
3042 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3043 if (dbg_wwanemul)
3044 return (tpacpi_wwan_emulstate) ?
3045 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3046 #endif
3048 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3049 return -EIO;
3051 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3052 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3055 static void wan_update_rfk(void)
3057 int status;
3059 if (!tpacpi_wan_rfkill)
3060 return;
3062 status = wan_get_radiosw();
3063 if (status < 0)
3064 return;
3065 rfkill_force_state(tpacpi_wan_rfkill, status);
3068 static int wan_set_radiosw(int radio_on, int update_rfk)
3070 int status;
3072 if (!tp_features.wan)
3073 return -ENODEV;
3075 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3076 * reason to risk weird behaviour. */
3077 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3078 && radio_on)
3079 return -EPERM;
3081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3082 if (dbg_wwanemul) {
3083 tpacpi_wwan_emulstate = !!radio_on;
3084 if (update_rfk)
3085 wan_update_rfk();
3086 return 0;
3088 #endif
3090 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3091 return -EIO;
3092 if (radio_on)
3093 status |= TP_ACPI_WANCARD_RADIOSSW;
3094 else
3095 status &= ~TP_ACPI_WANCARD_RADIOSSW;
3096 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3097 return -EIO;
3099 if (update_rfk)
3100 wan_update_rfk();
3102 return 0;
3105 /* sysfs wan enable ---------------------------------------------------- */
3106 static ssize_t wan_enable_show(struct device *dev,
3107 struct device_attribute *attr,
3108 char *buf)
3110 int status;
3112 status = wan_get_radiosw();
3113 if (status < 0)
3114 return status;
3116 return snprintf(buf, PAGE_SIZE, "%d\n",
3117 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3120 static ssize_t wan_enable_store(struct device *dev,
3121 struct device_attribute *attr,
3122 const char *buf, size_t count)
3124 unsigned long t;
3125 int res;
3127 if (parse_strtoul(buf, 1, &t))
3128 return -EINVAL;
3130 res = wan_set_radiosw(t, 1);
3132 return (res) ? res : count;
3135 static struct device_attribute dev_attr_wan_enable =
3136 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3137 wan_enable_show, wan_enable_store);
3139 /* --------------------------------------------------------------------- */
3141 static struct attribute *wan_attributes[] = {
3142 &dev_attr_wan_enable.attr,
3143 NULL
3146 static const struct attribute_group wan_attr_group = {
3147 .attrs = wan_attributes,
3150 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3152 int wans = wan_get_radiosw();
3154 if (wans < 0)
3155 return wans;
3157 *state = wans;
3158 return 0;
3161 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3163 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3166 static void wan_exit(void)
3168 if (tpacpi_wan_rfkill)
3169 rfkill_unregister(tpacpi_wan_rfkill);
3171 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3172 &wan_attr_group);
3175 static int __init wan_init(struct ibm_init_struct *iibm)
3177 int res;
3178 int status = 0;
3180 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3182 TPACPI_ACPIHANDLE_INIT(hkey);
3184 tp_features.wan = hkey_handle &&
3185 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3187 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3188 str_supported(tp_features.wan),
3189 status);
3191 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3192 if (dbg_wwanemul) {
3193 tp_features.wan = 1;
3194 printk(TPACPI_INFO
3195 "wwan switch emulation enabled\n");
3196 } else
3197 #endif
3198 if (tp_features.wan &&
3199 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3200 /* no wan hardware present in system */
3201 tp_features.wan = 0;
3202 dbg_printk(TPACPI_DBG_INIT,
3203 "wan hardware not installed\n");
3206 if (!tp_features.wan)
3207 return 1;
3209 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3210 &wan_attr_group);
3211 if (res)
3212 return res;
3214 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3215 &tpacpi_wan_rfkill,
3216 RFKILL_TYPE_WWAN,
3217 "tpacpi_wwan_sw",
3218 tpacpi_wan_rfk_set,
3219 tpacpi_wan_rfk_get);
3220 if (res) {
3221 wan_exit();
3222 return res;
3225 return 0;
3228 /* procfs -------------------------------------------------------------- */
3229 static int wan_read(char *p)
3231 int len = 0;
3232 int status = wan_get_radiosw();
3234 if (!tp_features.wan)
3235 len += sprintf(p + len, "status:\t\tnot supported\n");
3236 else {
3237 len += sprintf(p + len, "status:\t\t%s\n",
3238 (status == RFKILL_STATE_UNBLOCKED) ?
3239 "enabled" : "disabled");
3240 len += sprintf(p + len, "commands:\tenable, disable\n");
3243 return len;
3246 static int wan_write(char *buf)
3248 char *cmd;
3250 if (!tp_features.wan)
3251 return -ENODEV;
3253 while ((cmd = next_cmd(&buf))) {
3254 if (strlencmp(cmd, "enable") == 0) {
3255 wan_set_radiosw(1, 1);
3256 } else if (strlencmp(cmd, "disable") == 0) {
3257 wan_set_radiosw(0, 1);
3258 } else
3259 return -EINVAL;
3262 return 0;
3265 static struct ibm_struct wan_driver_data = {
3266 .name = "wan",
3267 .read = wan_read,
3268 .write = wan_write,
3269 .exit = wan_exit,
3272 /*************************************************************************
3273 * Video subdriver
3276 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3278 enum video_access_mode {
3279 TPACPI_VIDEO_NONE = 0,
3280 TPACPI_VIDEO_570, /* 570 */
3281 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3282 TPACPI_VIDEO_NEW, /* all others */
3285 enum { /* video status flags, based on VIDEO_570 */
3286 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3287 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3288 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3291 enum { /* TPACPI_VIDEO_570 constants */
3292 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3293 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3294 * video_status_flags */
3295 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3296 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3299 static enum video_access_mode video_supported;
3300 static int video_orig_autosw;
3302 static int video_autosw_get(void);
3303 static int video_autosw_set(int enable);
3305 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3307 static int __init video_init(struct ibm_init_struct *iibm)
3309 int ivga;
3311 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3313 TPACPI_ACPIHANDLE_INIT(vid);
3314 TPACPI_ACPIHANDLE_INIT(vid2);
3316 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3317 /* G41, assume IVGA doesn't change */
3318 vid_handle = vid2_handle;
3320 if (!vid_handle)
3321 /* video switching not supported on R30, R31 */
3322 video_supported = TPACPI_VIDEO_NONE;
3323 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3324 /* 570 */
3325 video_supported = TPACPI_VIDEO_570;
3326 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3327 /* 600e/x, 770e, 770x */
3328 video_supported = TPACPI_VIDEO_770;
3329 else
3330 /* all others */
3331 video_supported = TPACPI_VIDEO_NEW;
3333 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3334 str_supported(video_supported != TPACPI_VIDEO_NONE),
3335 video_supported);
3337 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3340 static void video_exit(void)
3342 dbg_printk(TPACPI_DBG_EXIT,
3343 "restoring original video autoswitch mode\n");
3344 if (video_autosw_set(video_orig_autosw))
3345 printk(TPACPI_ERR "error while trying to restore original "
3346 "video autoswitch mode\n");
3349 static int video_outputsw_get(void)
3351 int status = 0;
3352 int i;
3354 switch (video_supported) {
3355 case TPACPI_VIDEO_570:
3356 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3357 TP_ACPI_VIDEO_570_PHSCMD))
3358 return -EIO;
3359 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3360 break;
3361 case TPACPI_VIDEO_770:
3362 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3363 return -EIO;
3364 if (i)
3365 status |= TP_ACPI_VIDEO_S_LCD;
3366 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3367 return -EIO;
3368 if (i)
3369 status |= TP_ACPI_VIDEO_S_CRT;
3370 break;
3371 case TPACPI_VIDEO_NEW:
3372 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3373 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3374 return -EIO;
3375 if (i)
3376 status |= TP_ACPI_VIDEO_S_CRT;
3378 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3379 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3380 return -EIO;
3381 if (i)
3382 status |= TP_ACPI_VIDEO_S_LCD;
3383 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3384 return -EIO;
3385 if (i)
3386 status |= TP_ACPI_VIDEO_S_DVI;
3387 break;
3388 default:
3389 return -ENOSYS;
3392 return status;
3395 static int video_outputsw_set(int status)
3397 int autosw;
3398 int res = 0;
3400 switch (video_supported) {
3401 case TPACPI_VIDEO_570:
3402 res = acpi_evalf(NULL, NULL,
3403 "\\_SB.PHS2", "vdd",
3404 TP_ACPI_VIDEO_570_PHS2CMD,
3405 status | TP_ACPI_VIDEO_570_PHS2SET);
3406 break;
3407 case TPACPI_VIDEO_770:
3408 autosw = video_autosw_get();
3409 if (autosw < 0)
3410 return autosw;
3412 res = video_autosw_set(1);
3413 if (res)
3414 return res;
3415 res = acpi_evalf(vid_handle, NULL,
3416 "ASWT", "vdd", status * 0x100, 0);
3417 if (!autosw && video_autosw_set(autosw)) {
3418 printk(TPACPI_ERR
3419 "video auto-switch left enabled due to error\n");
3420 return -EIO;
3422 break;
3423 case TPACPI_VIDEO_NEW:
3424 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3425 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3426 break;
3427 default:
3428 return -ENOSYS;
3431 return (res)? 0 : -EIO;
3434 static int video_autosw_get(void)
3436 int autosw = 0;
3438 switch (video_supported) {
3439 case TPACPI_VIDEO_570:
3440 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3441 return -EIO;
3442 break;
3443 case TPACPI_VIDEO_770:
3444 case TPACPI_VIDEO_NEW:
3445 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3446 return -EIO;
3447 break;
3448 default:
3449 return -ENOSYS;
3452 return autosw & 1;
3455 static int video_autosw_set(int enable)
3457 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3458 return -EIO;
3459 return 0;
3462 static int video_outputsw_cycle(void)
3464 int autosw = video_autosw_get();
3465 int res;
3467 if (autosw < 0)
3468 return autosw;
3470 switch (video_supported) {
3471 case TPACPI_VIDEO_570:
3472 res = video_autosw_set(1);
3473 if (res)
3474 return res;
3475 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3476 break;
3477 case TPACPI_VIDEO_770:
3478 case TPACPI_VIDEO_NEW:
3479 res = video_autosw_set(1);
3480 if (res)
3481 return res;
3482 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3483 break;
3484 default:
3485 return -ENOSYS;
3487 if (!autosw && video_autosw_set(autosw)) {
3488 printk(TPACPI_ERR
3489 "video auto-switch left enabled due to error\n");
3490 return -EIO;
3493 return (res)? 0 : -EIO;
3496 static int video_expand_toggle(void)
3498 switch (video_supported) {
3499 case TPACPI_VIDEO_570:
3500 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3501 0 : -EIO;
3502 case TPACPI_VIDEO_770:
3503 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3504 0 : -EIO;
3505 case TPACPI_VIDEO_NEW:
3506 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3507 0 : -EIO;
3508 default:
3509 return -ENOSYS;
3511 /* not reached */
3514 static int video_read(char *p)
3516 int status, autosw;
3517 int len = 0;
3519 if (video_supported == TPACPI_VIDEO_NONE) {
3520 len += sprintf(p + len, "status:\t\tnot supported\n");
3521 return len;
3524 status = video_outputsw_get();
3525 if (status < 0)
3526 return status;
3528 autosw = video_autosw_get();
3529 if (autosw < 0)
3530 return autosw;
3532 len += sprintf(p + len, "status:\t\tsupported\n");
3533 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3534 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3535 if (video_supported == TPACPI_VIDEO_NEW)
3536 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3537 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3538 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3539 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3540 if (video_supported == TPACPI_VIDEO_NEW)
3541 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3542 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3543 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3545 return len;
3548 static int video_write(char *buf)
3550 char *cmd;
3551 int enable, disable, status;
3552 int res;
3554 if (video_supported == TPACPI_VIDEO_NONE)
3555 return -ENODEV;
3557 enable = 0;
3558 disable = 0;
3560 while ((cmd = next_cmd(&buf))) {
3561 if (strlencmp(cmd, "lcd_enable") == 0) {
3562 enable |= TP_ACPI_VIDEO_S_LCD;
3563 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3564 disable |= TP_ACPI_VIDEO_S_LCD;
3565 } else if (strlencmp(cmd, "crt_enable") == 0) {
3566 enable |= TP_ACPI_VIDEO_S_CRT;
3567 } else if (strlencmp(cmd, "crt_disable") == 0) {
3568 disable |= TP_ACPI_VIDEO_S_CRT;
3569 } else if (video_supported == TPACPI_VIDEO_NEW &&
3570 strlencmp(cmd, "dvi_enable") == 0) {
3571 enable |= TP_ACPI_VIDEO_S_DVI;
3572 } else if (video_supported == TPACPI_VIDEO_NEW &&
3573 strlencmp(cmd, "dvi_disable") == 0) {
3574 disable |= TP_ACPI_VIDEO_S_DVI;
3575 } else if (strlencmp(cmd, "auto_enable") == 0) {
3576 res = video_autosw_set(1);
3577 if (res)
3578 return res;
3579 } else if (strlencmp(cmd, "auto_disable") == 0) {
3580 res = video_autosw_set(0);
3581 if (res)
3582 return res;
3583 } else if (strlencmp(cmd, "video_switch") == 0) {
3584 res = video_outputsw_cycle();
3585 if (res)
3586 return res;
3587 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3588 res = video_expand_toggle();
3589 if (res)
3590 return res;
3591 } else
3592 return -EINVAL;
3595 if (enable || disable) {
3596 status = video_outputsw_get();
3597 if (status < 0)
3598 return status;
3599 res = video_outputsw_set((status & ~disable) | enable);
3600 if (res)
3601 return res;
3604 return 0;
3607 static struct ibm_struct video_driver_data = {
3608 .name = "video",
3609 .read = video_read,
3610 .write = video_write,
3611 .exit = video_exit,
3614 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3616 /*************************************************************************
3617 * Light (thinklight) subdriver
3620 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3621 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3623 static int light_get_status(void)
3625 int status = 0;
3627 if (tp_features.light_status) {
3628 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3629 return -EIO;
3630 return (!!status);
3633 return -ENXIO;
3636 static int light_set_status(int status)
3638 int rc;
3640 if (tp_features.light) {
3641 if (cmos_handle) {
3642 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3643 (status)?
3644 TP_CMOS_THINKLIGHT_ON :
3645 TP_CMOS_THINKLIGHT_OFF);
3646 } else {
3647 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3648 (status)? 1 : 0);
3650 return (rc)? 0 : -EIO;
3653 return -ENXIO;
3656 static void light_set_status_worker(struct work_struct *work)
3658 struct tpacpi_led_classdev *data =
3659 container_of(work, struct tpacpi_led_classdev, work);
3661 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3662 light_set_status((data->new_brightness != LED_OFF));
3665 static void light_sysfs_set(struct led_classdev *led_cdev,
3666 enum led_brightness brightness)
3668 struct tpacpi_led_classdev *data =
3669 container_of(led_cdev,
3670 struct tpacpi_led_classdev,
3671 led_classdev);
3672 data->new_brightness = brightness;
3673 queue_work(tpacpi_wq, &data->work);
3676 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3678 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3681 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3682 .led_classdev = {
3683 .name = "tpacpi::thinklight",
3684 .brightness_set = &light_sysfs_set,
3685 .brightness_get = &light_sysfs_get,
3689 static int __init light_init(struct ibm_init_struct *iibm)
3691 int rc;
3693 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3695 TPACPI_ACPIHANDLE_INIT(ledb);
3696 TPACPI_ACPIHANDLE_INIT(lght);
3697 TPACPI_ACPIHANDLE_INIT(cmos);
3698 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3700 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3701 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3703 if (tp_features.light)
3704 /* light status not supported on
3705 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3706 tp_features.light_status =
3707 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3709 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3710 str_supported(tp_features.light),
3711 str_supported(tp_features.light_status));
3713 if (!tp_features.light)
3714 return 1;
3716 rc = led_classdev_register(&tpacpi_pdev->dev,
3717 &tpacpi_led_thinklight.led_classdev);
3719 if (rc < 0) {
3720 tp_features.light = 0;
3721 tp_features.light_status = 0;
3722 } else {
3723 rc = 0;
3726 return rc;
3729 static void light_exit(void)
3731 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3732 if (work_pending(&tpacpi_led_thinklight.work))
3733 flush_workqueue(tpacpi_wq);
3736 static int light_read(char *p)
3738 int len = 0;
3739 int status;
3741 if (!tp_features.light) {
3742 len += sprintf(p + len, "status:\t\tnot supported\n");
3743 } else if (!tp_features.light_status) {
3744 len += sprintf(p + len, "status:\t\tunknown\n");
3745 len += sprintf(p + len, "commands:\ton, off\n");
3746 } else {
3747 status = light_get_status();
3748 if (status < 0)
3749 return status;
3750 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3751 len += sprintf(p + len, "commands:\ton, off\n");
3754 return len;
3757 static int light_write(char *buf)
3759 char *cmd;
3760 int newstatus = 0;
3762 if (!tp_features.light)
3763 return -ENODEV;
3765 while ((cmd = next_cmd(&buf))) {
3766 if (strlencmp(cmd, "on") == 0) {
3767 newstatus = 1;
3768 } else if (strlencmp(cmd, "off") == 0) {
3769 newstatus = 0;
3770 } else
3771 return -EINVAL;
3774 return light_set_status(newstatus);
3777 static struct ibm_struct light_driver_data = {
3778 .name = "light",
3779 .read = light_read,
3780 .write = light_write,
3781 .exit = light_exit,
3784 /*************************************************************************
3785 * Dock subdriver
3788 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3790 static void dock_notify(struct ibm_struct *ibm, u32 event);
3791 static int dock_read(char *p);
3792 static int dock_write(char *buf);
3794 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3795 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3796 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3797 "\\_SB.PCI.ISA.SLCE", /* 570 */
3798 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3800 /* don't list other alternatives as we install a notify handler on the 570 */
3801 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3803 static const struct acpi_device_id ibm_pci_device_ids[] = {
3804 {PCI_ROOT_HID_STRING, 0},
3805 {"", 0},
3808 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3810 .notify = dock_notify,
3811 .handle = &dock_handle,
3812 .type = ACPI_SYSTEM_NOTIFY,
3815 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3816 * We just use it to get notifications of dock hotplug
3817 * in very old thinkpads */
3818 .hid = ibm_pci_device_ids,
3819 .notify = dock_notify,
3820 .handle = &pci_handle,
3821 .type = ACPI_SYSTEM_NOTIFY,
3825 static struct ibm_struct dock_driver_data[2] = {
3827 .name = "dock",
3828 .read = dock_read,
3829 .write = dock_write,
3830 .acpi = &ibm_dock_acpidriver[0],
3833 .name = "dock",
3834 .acpi = &ibm_dock_acpidriver[1],
3838 #define dock_docked() (_sta(dock_handle) & 1)
3840 static int __init dock_init(struct ibm_init_struct *iibm)
3842 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3844 TPACPI_ACPIHANDLE_INIT(dock);
3846 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3847 str_supported(dock_handle != NULL));
3849 return (dock_handle)? 0 : 1;
3852 static int __init dock_init2(struct ibm_init_struct *iibm)
3854 int dock2_needed;
3856 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3858 if (dock_driver_data[0].flags.acpi_driver_registered &&
3859 dock_driver_data[0].flags.acpi_notify_installed) {
3860 TPACPI_ACPIHANDLE_INIT(pci);
3861 dock2_needed = (pci_handle != NULL);
3862 vdbg_printk(TPACPI_DBG_INIT,
3863 "dock PCI handler for the TP 570 is %s\n",
3864 str_supported(dock2_needed));
3865 } else {
3866 vdbg_printk(TPACPI_DBG_INIT,
3867 "dock subdriver part 2 not required\n");
3868 dock2_needed = 0;
3871 return (dock2_needed)? 0 : 1;
3874 static void dock_notify(struct ibm_struct *ibm, u32 event)
3876 int docked = dock_docked();
3877 int pci = ibm->acpi->hid && ibm->acpi->device &&
3878 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3879 int data;
3881 if (event == 1 && !pci) /* 570 */
3882 data = 1; /* button */
3883 else if (event == 1 && pci) /* 570 */
3884 data = 3; /* dock */
3885 else if (event == 3 && docked)
3886 data = 1; /* button */
3887 else if (event == 3 && !docked)
3888 data = 2; /* undock */
3889 else if (event == 0 && docked)
3890 data = 3; /* dock */
3891 else {
3892 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3893 event, _sta(dock_handle));
3894 data = 0; /* unknown */
3896 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3897 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3898 dev_name(&ibm->acpi->device->dev),
3899 event, data);
3902 static int dock_read(char *p)
3904 int len = 0;
3905 int docked = dock_docked();
3907 if (!dock_handle)
3908 len += sprintf(p + len, "status:\t\tnot supported\n");
3909 else if (!docked)
3910 len += sprintf(p + len, "status:\t\tundocked\n");
3911 else {
3912 len += sprintf(p + len, "status:\t\tdocked\n");
3913 len += sprintf(p + len, "commands:\tdock, undock\n");
3916 return len;
3919 static int dock_write(char *buf)
3921 char *cmd;
3923 if (!dock_docked())
3924 return -ENODEV;
3926 while ((cmd = next_cmd(&buf))) {
3927 if (strlencmp(cmd, "undock") == 0) {
3928 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3929 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3930 return -EIO;
3931 } else if (strlencmp(cmd, "dock") == 0) {
3932 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3933 return -EIO;
3934 } else
3935 return -EINVAL;
3938 return 0;
3941 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3943 /*************************************************************************
3944 * Bay subdriver
3947 #ifdef CONFIG_THINKPAD_ACPI_BAY
3949 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3950 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3951 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3952 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3953 ); /* A21e, R30, R31 */
3954 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3955 "_EJ0", /* all others */
3956 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3957 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3958 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3959 ); /* all others */
3960 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3961 "_EJ0", /* 770x */
3962 ); /* all others */
3964 static int __init bay_init(struct ibm_init_struct *iibm)
3966 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3968 TPACPI_ACPIHANDLE_INIT(bay);
3969 if (bay_handle)
3970 TPACPI_ACPIHANDLE_INIT(bay_ej);
3971 TPACPI_ACPIHANDLE_INIT(bay2);
3972 if (bay2_handle)
3973 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3975 tp_features.bay_status = bay_handle &&
3976 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3977 tp_features.bay_status2 = bay2_handle &&
3978 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3980 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3981 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3982 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3983 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3985 vdbg_printk(TPACPI_DBG_INIT,
3986 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3987 str_supported(tp_features.bay_status),
3988 str_supported(tp_features.bay_eject),
3989 str_supported(tp_features.bay_status2),
3990 str_supported(tp_features.bay_eject2));
3992 return (tp_features.bay_status || tp_features.bay_eject ||
3993 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3996 static void bay_notify(struct ibm_struct *ibm, u32 event)
3998 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3999 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4000 dev_name(&ibm->acpi->device->dev),
4001 event, 0);
4004 #define bay_occupied(b) (_sta(b##_handle) & 1)
4006 static int bay_read(char *p)
4008 int len = 0;
4009 int occupied = bay_occupied(bay);
4010 int occupied2 = bay_occupied(bay2);
4011 int eject, eject2;
4013 len += sprintf(p + len, "status:\t\t%s\n",
4014 tp_features.bay_status ?
4015 (occupied ? "occupied" : "unoccupied") :
4016 "not supported");
4017 if (tp_features.bay_status2)
4018 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4019 "occupied" : "unoccupied");
4021 eject = tp_features.bay_eject && occupied;
4022 eject2 = tp_features.bay_eject2 && occupied2;
4024 if (eject && eject2)
4025 len += sprintf(p + len, "commands:\teject, eject2\n");
4026 else if (eject)
4027 len += sprintf(p + len, "commands:\teject\n");
4028 else if (eject2)
4029 len += sprintf(p + len, "commands:\teject2\n");
4031 return len;
4034 static int bay_write(char *buf)
4036 char *cmd;
4038 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4039 return -ENODEV;
4041 while ((cmd = next_cmd(&buf))) {
4042 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4043 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4044 return -EIO;
4045 } else if (tp_features.bay_eject2 &&
4046 strlencmp(cmd, "eject2") == 0) {
4047 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4048 return -EIO;
4049 } else
4050 return -EINVAL;
4053 return 0;
4056 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4057 .notify = bay_notify,
4058 .handle = &bay_handle,
4059 .type = ACPI_SYSTEM_NOTIFY,
4062 static struct ibm_struct bay_driver_data = {
4063 .name = "bay",
4064 .read = bay_read,
4065 .write = bay_write,
4066 .acpi = &ibm_bay_acpidriver,
4069 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4071 /*************************************************************************
4072 * CMOS subdriver
4075 /* sysfs cmos_command -------------------------------------------------- */
4076 static ssize_t cmos_command_store(struct device *dev,
4077 struct device_attribute *attr,
4078 const char *buf, size_t count)
4080 unsigned long cmos_cmd;
4081 int res;
4083 if (parse_strtoul(buf, 21, &cmos_cmd))
4084 return -EINVAL;
4086 res = issue_thinkpad_cmos_command(cmos_cmd);
4087 return (res)? res : count;
4090 static struct device_attribute dev_attr_cmos_command =
4091 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4093 /* --------------------------------------------------------------------- */
4095 static int __init cmos_init(struct ibm_init_struct *iibm)
4097 int res;
4099 vdbg_printk(TPACPI_DBG_INIT,
4100 "initializing cmos commands subdriver\n");
4102 TPACPI_ACPIHANDLE_INIT(cmos);
4104 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4105 str_supported(cmos_handle != NULL));
4107 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4108 if (res)
4109 return res;
4111 return (cmos_handle)? 0 : 1;
4114 static void cmos_exit(void)
4116 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4119 static int cmos_read(char *p)
4121 int len = 0;
4123 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4124 R30, R31, T20-22, X20-21 */
4125 if (!cmos_handle)
4126 len += sprintf(p + len, "status:\t\tnot supported\n");
4127 else {
4128 len += sprintf(p + len, "status:\t\tsupported\n");
4129 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4132 return len;
4135 static int cmos_write(char *buf)
4137 char *cmd;
4138 int cmos_cmd, res;
4140 while ((cmd = next_cmd(&buf))) {
4141 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4142 cmos_cmd >= 0 && cmos_cmd <= 21) {
4143 /* cmos_cmd set */
4144 } else
4145 return -EINVAL;
4147 res = issue_thinkpad_cmos_command(cmos_cmd);
4148 if (res)
4149 return res;
4152 return 0;
4155 static struct ibm_struct cmos_driver_data = {
4156 .name = "cmos",
4157 .read = cmos_read,
4158 .write = cmos_write,
4159 .exit = cmos_exit,
4162 /*************************************************************************
4163 * LED subdriver
4166 enum led_access_mode {
4167 TPACPI_LED_NONE = 0,
4168 TPACPI_LED_570, /* 570 */
4169 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4170 TPACPI_LED_NEW, /* all others */
4173 enum { /* For TPACPI_LED_OLD */
4174 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4175 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4176 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4179 enum led_status_t {
4180 TPACPI_LED_OFF = 0,
4181 TPACPI_LED_ON,
4182 TPACPI_LED_BLINK,
4185 static enum led_access_mode led_supported;
4187 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4188 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4189 /* T20-22, X20-21 */
4190 "LED", /* all others */
4191 ); /* R30, R31 */
4193 #define TPACPI_LED_NUMLEDS 8
4194 static struct tpacpi_led_classdev *tpacpi_leds;
4195 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4196 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4197 /* there's a limit of 19 chars + NULL before 2.6.26 */
4198 "tpacpi::power",
4199 "tpacpi:orange:batt",
4200 "tpacpi:green:batt",
4201 "tpacpi::dock_active",
4202 "tpacpi::bay_active",
4203 "tpacpi::dock_batt",
4204 "tpacpi::unknown_led",
4205 "tpacpi::standby",
4208 static int led_get_status(const unsigned int led)
4210 int status;
4211 enum led_status_t led_s;
4213 switch (led_supported) {
4214 case TPACPI_LED_570:
4215 if (!acpi_evalf(ec_handle,
4216 &status, "GLED", "dd", 1 << led))
4217 return -EIO;
4218 led_s = (status == 0)?
4219 TPACPI_LED_OFF :
4220 ((status == 1)?
4221 TPACPI_LED_ON :
4222 TPACPI_LED_BLINK);
4223 tpacpi_led_state_cache[led] = led_s;
4224 return led_s;
4225 default:
4226 return -ENXIO;
4229 /* not reached */
4232 static int led_set_status(const unsigned int led,
4233 const enum led_status_t ledstatus)
4235 /* off, on, blink. Index is led_status_t */
4236 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4237 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4239 int rc = 0;
4241 switch (led_supported) {
4242 case TPACPI_LED_570:
4243 /* 570 */
4244 if (led > 7)
4245 return -EINVAL;
4246 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4247 (1 << led), led_sled_arg1[ledstatus]))
4248 rc = -EIO;
4249 break;
4250 case TPACPI_LED_OLD:
4251 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4252 if (led > 7)
4253 return -EINVAL;
4254 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4255 if (rc >= 0)
4256 rc = ec_write(TPACPI_LED_EC_HLBL,
4257 (ledstatus == TPACPI_LED_BLINK) << led);
4258 if (rc >= 0)
4259 rc = ec_write(TPACPI_LED_EC_HLCL,
4260 (ledstatus != TPACPI_LED_OFF) << led);
4261 break;
4262 case TPACPI_LED_NEW:
4263 /* all others */
4264 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4265 led, led_led_arg1[ledstatus]))
4266 rc = -EIO;
4267 break;
4268 default:
4269 rc = -ENXIO;
4272 if (!rc)
4273 tpacpi_led_state_cache[led] = ledstatus;
4275 return rc;
4278 static void led_sysfs_set_status(unsigned int led,
4279 enum led_brightness brightness)
4281 led_set_status(led,
4282 (brightness == LED_OFF) ?
4283 TPACPI_LED_OFF :
4284 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4285 TPACPI_LED_BLINK : TPACPI_LED_ON);
4288 static void led_set_status_worker(struct work_struct *work)
4290 struct tpacpi_led_classdev *data =
4291 container_of(work, struct tpacpi_led_classdev, work);
4293 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4294 led_sysfs_set_status(data->led, data->new_brightness);
4297 static void led_sysfs_set(struct led_classdev *led_cdev,
4298 enum led_brightness brightness)
4300 struct tpacpi_led_classdev *data = container_of(led_cdev,
4301 struct tpacpi_led_classdev, led_classdev);
4303 data->new_brightness = brightness;
4304 queue_work(tpacpi_wq, &data->work);
4307 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4308 unsigned long *delay_on, unsigned long *delay_off)
4310 struct tpacpi_led_classdev *data = container_of(led_cdev,
4311 struct tpacpi_led_classdev, led_classdev);
4313 /* Can we choose the flash rate? */
4314 if (*delay_on == 0 && *delay_off == 0) {
4315 /* yes. set them to the hardware blink rate (1 Hz) */
4316 *delay_on = 500; /* ms */
4317 *delay_off = 500; /* ms */
4318 } else if ((*delay_on != 500) || (*delay_off != 500))
4319 return -EINVAL;
4321 data->new_brightness = TPACPI_LED_BLINK;
4322 queue_work(tpacpi_wq, &data->work);
4324 return 0;
4327 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4329 int rc;
4331 struct tpacpi_led_classdev *data = container_of(led_cdev,
4332 struct tpacpi_led_classdev, led_classdev);
4334 rc = led_get_status(data->led);
4336 if (rc == TPACPI_LED_OFF || rc < 0)
4337 rc = LED_OFF; /* no error handling in led class :( */
4338 else
4339 rc = LED_FULL;
4341 return rc;
4344 static void led_exit(void)
4346 unsigned int i;
4348 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4349 if (tpacpi_leds[i].led_classdev.name)
4350 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4353 kfree(tpacpi_leds);
4356 static int __init led_init(struct ibm_init_struct *iibm)
4358 unsigned int i;
4359 int rc;
4361 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4363 TPACPI_ACPIHANDLE_INIT(led);
4365 if (!led_handle)
4366 /* led not supported on R30, R31 */
4367 led_supported = TPACPI_LED_NONE;
4368 else if (strlencmp(led_path, "SLED") == 0)
4369 /* 570 */
4370 led_supported = TPACPI_LED_570;
4371 else if (strlencmp(led_path, "SYSL") == 0)
4372 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4373 led_supported = TPACPI_LED_OLD;
4374 else
4375 /* all others */
4376 led_supported = TPACPI_LED_NEW;
4378 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4379 str_supported(led_supported), led_supported);
4381 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4382 GFP_KERNEL);
4383 if (!tpacpi_leds) {
4384 printk(TPACPI_ERR "Out of memory for LED data\n");
4385 return -ENOMEM;
4388 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4389 tpacpi_leds[i].led = i;
4391 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4392 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4393 if (led_supported == TPACPI_LED_570)
4394 tpacpi_leds[i].led_classdev.brightness_get =
4395 &led_sysfs_get;
4397 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4399 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4401 rc = led_classdev_register(&tpacpi_pdev->dev,
4402 &tpacpi_leds[i].led_classdev);
4403 if (rc < 0) {
4404 tpacpi_leds[i].led_classdev.name = NULL;
4405 led_exit();
4406 return rc;
4410 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4413 #define str_led_status(s) \
4414 ((s) == TPACPI_LED_OFF ? "off" : \
4415 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4417 static int led_read(char *p)
4419 int len = 0;
4421 if (!led_supported) {
4422 len += sprintf(p + len, "status:\t\tnot supported\n");
4423 return len;
4425 len += sprintf(p + len, "status:\t\tsupported\n");
4427 if (led_supported == TPACPI_LED_570) {
4428 /* 570 */
4429 int i, status;
4430 for (i = 0; i < 8; i++) {
4431 status = led_get_status(i);
4432 if (status < 0)
4433 return -EIO;
4434 len += sprintf(p + len, "%d:\t\t%s\n",
4435 i, str_led_status(status));
4439 len += sprintf(p + len, "commands:\t"
4440 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4442 return len;
4445 static int led_write(char *buf)
4447 char *cmd;
4448 int led, rc;
4449 enum led_status_t s;
4451 if (!led_supported)
4452 return -ENODEV;
4454 while ((cmd = next_cmd(&buf))) {
4455 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4456 return -EINVAL;
4458 if (strstr(cmd, "off")) {
4459 s = TPACPI_LED_OFF;
4460 } else if (strstr(cmd, "on")) {
4461 s = TPACPI_LED_ON;
4462 } else if (strstr(cmd, "blink")) {
4463 s = TPACPI_LED_BLINK;
4464 } else {
4465 return -EINVAL;
4468 rc = led_set_status(led, s);
4469 if (rc < 0)
4470 return rc;
4473 return 0;
4476 static struct ibm_struct led_driver_data = {
4477 .name = "led",
4478 .read = led_read,
4479 .write = led_write,
4480 .exit = led_exit,
4483 /*************************************************************************
4484 * Beep subdriver
4487 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4489 static int __init beep_init(struct ibm_init_struct *iibm)
4491 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4493 TPACPI_ACPIHANDLE_INIT(beep);
4495 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4496 str_supported(beep_handle != NULL));
4498 return (beep_handle)? 0 : 1;
4501 static int beep_read(char *p)
4503 int len = 0;
4505 if (!beep_handle)
4506 len += sprintf(p + len, "status:\t\tnot supported\n");
4507 else {
4508 len += sprintf(p + len, "status:\t\tsupported\n");
4509 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4512 return len;
4515 static int beep_write(char *buf)
4517 char *cmd;
4518 int beep_cmd;
4520 if (!beep_handle)
4521 return -ENODEV;
4523 while ((cmd = next_cmd(&buf))) {
4524 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4525 beep_cmd >= 0 && beep_cmd <= 17) {
4526 /* beep_cmd set */
4527 } else
4528 return -EINVAL;
4529 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4530 return -EIO;
4533 return 0;
4536 static struct ibm_struct beep_driver_data = {
4537 .name = "beep",
4538 .read = beep_read,
4539 .write = beep_write,
4542 /*************************************************************************
4543 * Thermal subdriver
4546 enum thermal_access_mode {
4547 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4548 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4549 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4550 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4551 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4554 enum { /* TPACPI_THERMAL_TPEC_* */
4555 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4556 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4557 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4560 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4561 struct ibm_thermal_sensors_struct {
4562 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4565 static enum thermal_access_mode thermal_read_mode;
4567 /* idx is zero-based */
4568 static int thermal_get_sensor(int idx, s32 *value)
4570 int t;
4571 s8 tmp;
4572 char tmpi[5];
4574 t = TP_EC_THERMAL_TMP0;
4576 switch (thermal_read_mode) {
4577 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4578 case TPACPI_THERMAL_TPEC_16:
4579 if (idx >= 8 && idx <= 15) {
4580 t = TP_EC_THERMAL_TMP8;
4581 idx -= 8;
4583 /* fallthrough */
4584 #endif
4585 case TPACPI_THERMAL_TPEC_8:
4586 if (idx <= 7) {
4587 if (!acpi_ec_read(t + idx, &tmp))
4588 return -EIO;
4589 *value = tmp * 1000;
4590 return 0;
4592 break;
4594 case TPACPI_THERMAL_ACPI_UPDT:
4595 if (idx <= 7) {
4596 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4597 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4598 return -EIO;
4599 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4600 return -EIO;
4601 *value = (t - 2732) * 100;
4602 return 0;
4604 break;
4606 case TPACPI_THERMAL_ACPI_TMP07:
4607 if (idx <= 7) {
4608 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4609 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4610 return -EIO;
4611 if (t > 127 || t < -127)
4612 t = TP_EC_THERMAL_TMP_NA;
4613 *value = t * 1000;
4614 return 0;
4616 break;
4618 case TPACPI_THERMAL_NONE:
4619 default:
4620 return -ENOSYS;
4623 return -EINVAL;
4626 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4628 int res, i;
4629 int n;
4631 n = 8;
4632 i = 0;
4634 if (!s)
4635 return -EINVAL;
4637 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4638 n = 16;
4640 for (i = 0 ; i < n; i++) {
4641 res = thermal_get_sensor(i, &s->temp[i]);
4642 if (res)
4643 return res;
4646 return n;
4649 /* sysfs temp##_input -------------------------------------------------- */
4651 static ssize_t thermal_temp_input_show(struct device *dev,
4652 struct device_attribute *attr,
4653 char *buf)
4655 struct sensor_device_attribute *sensor_attr =
4656 to_sensor_dev_attr(attr);
4657 int idx = sensor_attr->index;
4658 s32 value;
4659 int res;
4661 res = thermal_get_sensor(idx, &value);
4662 if (res)
4663 return res;
4664 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4665 return -ENXIO;
4667 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4670 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4671 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4672 thermal_temp_input_show, NULL, _idxB)
4674 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4675 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4676 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4677 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4678 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4679 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4680 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4681 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4682 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4683 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4684 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4685 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4686 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4687 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4688 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4689 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4690 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4693 #define THERMAL_ATTRS(X) \
4694 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4696 static struct attribute *thermal_temp_input_attr[] = {
4697 THERMAL_ATTRS(8),
4698 THERMAL_ATTRS(9),
4699 THERMAL_ATTRS(10),
4700 THERMAL_ATTRS(11),
4701 THERMAL_ATTRS(12),
4702 THERMAL_ATTRS(13),
4703 THERMAL_ATTRS(14),
4704 THERMAL_ATTRS(15),
4705 THERMAL_ATTRS(0),
4706 THERMAL_ATTRS(1),
4707 THERMAL_ATTRS(2),
4708 THERMAL_ATTRS(3),
4709 THERMAL_ATTRS(4),
4710 THERMAL_ATTRS(5),
4711 THERMAL_ATTRS(6),
4712 THERMAL_ATTRS(7),
4713 NULL
4716 static const struct attribute_group thermal_temp_input16_group = {
4717 .attrs = thermal_temp_input_attr
4720 static const struct attribute_group thermal_temp_input8_group = {
4721 .attrs = &thermal_temp_input_attr[8]
4724 #undef THERMAL_SENSOR_ATTR_TEMP
4725 #undef THERMAL_ATTRS
4727 /* --------------------------------------------------------------------- */
4729 static int __init thermal_init(struct ibm_init_struct *iibm)
4731 u8 t, ta1, ta2;
4732 int i;
4733 int acpi_tmp7;
4734 int res;
4736 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4738 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4740 if (thinkpad_id.ec_model) {
4742 * Direct EC access mode: sensors at registers
4743 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4744 * non-implemented, thermal sensors return 0x80 when
4745 * not available
4748 ta1 = ta2 = 0;
4749 for (i = 0; i < 8; i++) {
4750 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4751 ta1 |= t;
4752 } else {
4753 ta1 = 0;
4754 break;
4756 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4757 ta2 |= t;
4758 } else {
4759 ta1 = 0;
4760 break;
4763 if (ta1 == 0) {
4764 /* This is sheer paranoia, but we handle it anyway */
4765 if (acpi_tmp7) {
4766 printk(TPACPI_ERR
4767 "ThinkPad ACPI EC access misbehaving, "
4768 "falling back to ACPI TMPx access "
4769 "mode\n");
4770 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4771 } else {
4772 printk(TPACPI_ERR
4773 "ThinkPad ACPI EC access misbehaving, "
4774 "disabling thermal sensors access\n");
4775 thermal_read_mode = TPACPI_THERMAL_NONE;
4777 } else {
4778 thermal_read_mode =
4779 (ta2 != 0) ?
4780 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4782 } else if (acpi_tmp7) {
4783 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4784 /* 600e/x, 770e, 770x */
4785 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4786 } else {
4787 /* Standard ACPI TMPx access, max 8 sensors */
4788 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4790 } else {
4791 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4792 thermal_read_mode = TPACPI_THERMAL_NONE;
4795 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4796 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4797 thermal_read_mode);
4799 switch (thermal_read_mode) {
4800 case TPACPI_THERMAL_TPEC_16:
4801 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4802 &thermal_temp_input16_group);
4803 if (res)
4804 return res;
4805 break;
4806 case TPACPI_THERMAL_TPEC_8:
4807 case TPACPI_THERMAL_ACPI_TMP07:
4808 case TPACPI_THERMAL_ACPI_UPDT:
4809 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4810 &thermal_temp_input8_group);
4811 if (res)
4812 return res;
4813 break;
4814 case TPACPI_THERMAL_NONE:
4815 default:
4816 return 1;
4819 return 0;
4822 static void thermal_exit(void)
4824 switch (thermal_read_mode) {
4825 case TPACPI_THERMAL_TPEC_16:
4826 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4827 &thermal_temp_input16_group);
4828 break;
4829 case TPACPI_THERMAL_TPEC_8:
4830 case TPACPI_THERMAL_ACPI_TMP07:
4831 case TPACPI_THERMAL_ACPI_UPDT:
4832 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4833 &thermal_temp_input16_group);
4834 break;
4835 case TPACPI_THERMAL_NONE:
4836 default:
4837 break;
4841 static int thermal_read(char *p)
4843 int len = 0;
4844 int n, i;
4845 struct ibm_thermal_sensors_struct t;
4847 n = thermal_get_sensors(&t);
4848 if (unlikely(n < 0))
4849 return n;
4851 len += sprintf(p + len, "temperatures:\t");
4853 if (n > 0) {
4854 for (i = 0; i < (n - 1); i++)
4855 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4856 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4857 } else
4858 len += sprintf(p + len, "not supported\n");
4860 return len;
4863 static struct ibm_struct thermal_driver_data = {
4864 .name = "thermal",
4865 .read = thermal_read,
4866 .exit = thermal_exit,
4869 /*************************************************************************
4870 * EC Dump subdriver
4873 static u8 ecdump_regs[256];
4875 static int ecdump_read(char *p)
4877 int len = 0;
4878 int i, j;
4879 u8 v;
4881 len += sprintf(p + len, "EC "
4882 " +00 +01 +02 +03 +04 +05 +06 +07"
4883 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4884 for (i = 0; i < 256; i += 16) {
4885 len += sprintf(p + len, "EC 0x%02x:", i);
4886 for (j = 0; j < 16; j++) {
4887 if (!acpi_ec_read(i + j, &v))
4888 break;
4889 if (v != ecdump_regs[i + j])
4890 len += sprintf(p + len, " *%02x", v);
4891 else
4892 len += sprintf(p + len, " %02x", v);
4893 ecdump_regs[i + j] = v;
4895 len += sprintf(p + len, "\n");
4896 if (j != 16)
4897 break;
4900 /* These are way too dangerous to advertise openly... */
4901 #if 0
4902 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4903 " (<offset> is 00-ff, <value> is 00-ff)\n");
4904 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4905 " (<offset> is 00-ff, <value> is 0-255)\n");
4906 #endif
4907 return len;
4910 static int ecdump_write(char *buf)
4912 char *cmd;
4913 int i, v;
4915 while ((cmd = next_cmd(&buf))) {
4916 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4917 /* i and v set */
4918 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4919 /* i and v set */
4920 } else
4921 return -EINVAL;
4922 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4923 if (!acpi_ec_write(i, v))
4924 return -EIO;
4925 } else
4926 return -EINVAL;
4929 return 0;
4932 static struct ibm_struct ecdump_driver_data = {
4933 .name = "ecdump",
4934 .read = ecdump_read,
4935 .write = ecdump_write,
4936 .flags.experimental = 1,
4939 /*************************************************************************
4940 * Backlight/brightness subdriver
4943 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4945 enum {
4946 TP_EC_BACKLIGHT = 0x31,
4948 /* TP_EC_BACKLIGHT bitmasks */
4949 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4950 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4951 TP_EC_BACKLIGHT_MAPSW = 0x20,
4954 static struct backlight_device *ibm_backlight_device;
4955 static int brightness_mode;
4956 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4958 static struct mutex brightness_mutex;
4961 * ThinkPads can read brightness from two places: EC 0x31, or
4962 * CMOS NVRAM byte 0x5E, bits 0-3.
4964 * EC 0x31 has the following layout
4965 * Bit 7: unknown function
4966 * Bit 6: unknown function
4967 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4968 * Bit 4: must be set to zero to avoid problems
4969 * Bit 3-0: backlight brightness level
4971 * brightness_get_raw returns status data in the EC 0x31 layout
4973 static int brightness_get_raw(int *status)
4975 u8 lec = 0, lcmos = 0, level = 0;
4977 if (brightness_mode & 1) {
4978 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4979 return -EIO;
4980 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4982 if (brightness_mode & 2) {
4983 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4984 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4985 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4986 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4987 level = lcmos;
4990 if (brightness_mode == 3) {
4991 *status = lec; /* Prefer EC, CMOS is just a backing store */
4992 lec &= TP_EC_BACKLIGHT_LVLMSK;
4993 if (lec == lcmos)
4994 tp_warned.bright_cmos_ec_unsync = 0;
4995 else {
4996 if (!tp_warned.bright_cmos_ec_unsync) {
4997 printk(TPACPI_ERR
4998 "CMOS NVRAM (%u) and EC (%u) do not "
4999 "agree on display brightness level\n",
5000 (unsigned int) lcmos,
5001 (unsigned int) lec);
5002 tp_warned.bright_cmos_ec_unsync = 1;
5004 return -EIO;
5006 } else {
5007 *status = level;
5010 return 0;
5013 /* May return EINTR which can always be mapped to ERESTARTSYS */
5014 static int brightness_set(int value)
5016 int cmos_cmd, inc, i, res;
5017 int current_value;
5018 int command_bits;
5020 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5021 value < 0)
5022 return -EINVAL;
5024 res = mutex_lock_interruptible(&brightness_mutex);
5025 if (res < 0)
5026 return res;
5028 res = brightness_get_raw(&current_value);
5029 if (res < 0)
5030 goto errout;
5032 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5033 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5035 cmos_cmd = value > current_value ?
5036 TP_CMOS_BRIGHTNESS_UP :
5037 TP_CMOS_BRIGHTNESS_DOWN;
5038 inc = (value > current_value)? 1 : -1;
5040 res = 0;
5041 for (i = current_value; i != value; i += inc) {
5042 if ((brightness_mode & 2) &&
5043 issue_thinkpad_cmos_command(cmos_cmd)) {
5044 res = -EIO;
5045 goto errout;
5047 if ((brightness_mode & 1) &&
5048 !acpi_ec_write(TP_EC_BACKLIGHT,
5049 (i + inc) | command_bits)) {
5050 res = -EIO;
5051 goto errout;;
5055 errout:
5056 mutex_unlock(&brightness_mutex);
5057 return res;
5060 /* sysfs backlight class ----------------------------------------------- */
5062 static int brightness_update_status(struct backlight_device *bd)
5064 /* it is the backlight class's job (caller) to handle
5065 * EINTR and other errors properly */
5066 return brightness_set(
5067 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5068 bd->props.power == FB_BLANK_UNBLANK) ?
5069 bd->props.brightness : 0);
5072 static int brightness_get(struct backlight_device *bd)
5074 int status, res;
5076 res = brightness_get_raw(&status);
5077 if (res < 0)
5078 return 0; /* FIXME: teach backlight about error handling */
5080 return status & TP_EC_BACKLIGHT_LVLMSK;
5083 static struct backlight_ops ibm_backlight_data = {
5084 .get_brightness = brightness_get,
5085 .update_status = brightness_update_status,
5088 /* --------------------------------------------------------------------- */
5090 static int __init brightness_init(struct ibm_init_struct *iibm)
5092 int b;
5094 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5096 mutex_init(&brightness_mutex);
5099 * We always attempt to detect acpi support, so as to switch
5100 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5101 * going to publish a backlight interface
5103 b = tpacpi_check_std_acpi_brightness_support();
5104 if (b > 0) {
5105 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5106 printk(TPACPI_NOTICE
5107 "Lenovo BIOS switched to ACPI backlight "
5108 "control mode\n");
5110 if (brightness_enable > 1) {
5111 printk(TPACPI_NOTICE
5112 "standard ACPI backlight interface "
5113 "available, not loading native one...\n");
5114 return 1;
5118 if (!brightness_enable) {
5119 dbg_printk(TPACPI_DBG_INIT,
5120 "brightness support disabled by "
5121 "module parameter\n");
5122 return 1;
5125 if (b > 16) {
5126 printk(TPACPI_ERR
5127 "Unsupported brightness interface, "
5128 "please contact %s\n", TPACPI_MAIL);
5129 return 1;
5131 if (b == 16)
5132 tp_features.bright_16levels = 1;
5134 if (!brightness_mode) {
5135 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5136 brightness_mode = 2;
5137 else
5138 brightness_mode = 3;
5140 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5141 brightness_mode);
5144 if (brightness_mode > 3)
5145 return -EINVAL;
5147 if (brightness_get_raw(&b) < 0)
5148 return 1;
5150 if (tp_features.bright_16levels)
5151 printk(TPACPI_INFO
5152 "detected a 16-level brightness capable ThinkPad\n");
5154 ibm_backlight_device = backlight_device_register(
5155 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5156 &ibm_backlight_data);
5157 if (IS_ERR(ibm_backlight_device)) {
5158 printk(TPACPI_ERR "Could not register backlight device\n");
5159 return PTR_ERR(ibm_backlight_device);
5161 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5163 ibm_backlight_device->props.max_brightness =
5164 (tp_features.bright_16levels)? 15 : 7;
5165 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5166 backlight_update_status(ibm_backlight_device);
5168 return 0;
5171 static void brightness_exit(void)
5173 if (ibm_backlight_device) {
5174 vdbg_printk(TPACPI_DBG_EXIT,
5175 "calling backlight_device_unregister()\n");
5176 backlight_device_unregister(ibm_backlight_device);
5180 static int brightness_read(char *p)
5182 int len = 0;
5183 int level;
5185 level = brightness_get(NULL);
5186 if (level < 0) {
5187 len += sprintf(p + len, "level:\t\tunreadable\n");
5188 } else {
5189 len += sprintf(p + len, "level:\t\t%d\n", level);
5190 len += sprintf(p + len, "commands:\tup, down\n");
5191 len += sprintf(p + len, "commands:\tlevel <level>"
5192 " (<level> is 0-%d)\n",
5193 (tp_features.bright_16levels) ? 15 : 7);
5196 return len;
5199 static int brightness_write(char *buf)
5201 int level;
5202 int rc;
5203 char *cmd;
5204 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5206 level = brightness_get(NULL);
5207 if (level < 0)
5208 return level;
5210 while ((cmd = next_cmd(&buf))) {
5211 if (strlencmp(cmd, "up") == 0) {
5212 if (level < max_level)
5213 level++;
5214 } else if (strlencmp(cmd, "down") == 0) {
5215 if (level > 0)
5216 level--;
5217 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5218 level >= 0 && level <= max_level) {
5219 /* new level set */
5220 } else
5221 return -EINVAL;
5225 * Now we know what the final level should be, so we try to set it.
5226 * Doing it this way makes the syscall restartable in case of EINTR
5228 rc = brightness_set(level);
5229 return (rc == -EINTR)? ERESTARTSYS : rc;
5232 static struct ibm_struct brightness_driver_data = {
5233 .name = "brightness",
5234 .read = brightness_read,
5235 .write = brightness_write,
5236 .exit = brightness_exit,
5239 /*************************************************************************
5240 * Volume subdriver
5243 static int volume_offset = 0x30;
5245 static int volume_read(char *p)
5247 int len = 0;
5248 u8 level;
5250 if (!acpi_ec_read(volume_offset, &level)) {
5251 len += sprintf(p + len, "level:\t\tunreadable\n");
5252 } else {
5253 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5254 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5255 len += sprintf(p + len, "commands:\tup, down, mute\n");
5256 len += sprintf(p + len, "commands:\tlevel <level>"
5257 " (<level> is 0-15)\n");
5260 return len;
5263 static int volume_write(char *buf)
5265 int cmos_cmd, inc, i;
5266 u8 level, mute;
5267 int new_level, new_mute;
5268 char *cmd;
5270 while ((cmd = next_cmd(&buf))) {
5271 if (!acpi_ec_read(volume_offset, &level))
5272 return -EIO;
5273 new_mute = mute = level & 0x40;
5274 new_level = level = level & 0xf;
5276 if (strlencmp(cmd, "up") == 0) {
5277 if (mute)
5278 new_mute = 0;
5279 else
5280 new_level = level == 15 ? 15 : level + 1;
5281 } else if (strlencmp(cmd, "down") == 0) {
5282 if (mute)
5283 new_mute = 0;
5284 else
5285 new_level = level == 0 ? 0 : level - 1;
5286 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5287 new_level >= 0 && new_level <= 15) {
5288 /* new_level set */
5289 } else if (strlencmp(cmd, "mute") == 0) {
5290 new_mute = 0x40;
5291 } else
5292 return -EINVAL;
5294 if (new_level != level) {
5295 /* mute doesn't change */
5297 cmos_cmd = (new_level > level) ?
5298 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5299 inc = new_level > level ? 1 : -1;
5301 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5302 !acpi_ec_write(volume_offset, level)))
5303 return -EIO;
5305 for (i = level; i != new_level; i += inc)
5306 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5307 !acpi_ec_write(volume_offset, i + inc))
5308 return -EIO;
5310 if (mute &&
5311 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5312 !acpi_ec_write(volume_offset, new_level + mute))) {
5313 return -EIO;
5317 if (new_mute != mute) {
5318 /* level doesn't change */
5320 cmos_cmd = (new_mute) ?
5321 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5323 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5324 !acpi_ec_write(volume_offset, level + new_mute))
5325 return -EIO;
5329 return 0;
5332 static struct ibm_struct volume_driver_data = {
5333 .name = "volume",
5334 .read = volume_read,
5335 .write = volume_write,
5338 /*************************************************************************
5339 * Fan subdriver
5343 * FAN ACCESS MODES
5345 * TPACPI_FAN_RD_ACPI_GFAN:
5346 * ACPI GFAN method: returns fan level
5348 * see TPACPI_FAN_WR_ACPI_SFAN
5349 * EC 0x2f (HFSP) not available if GFAN exists
5351 * TPACPI_FAN_WR_ACPI_SFAN:
5352 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5354 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5355 * it for writing.
5357 * TPACPI_FAN_WR_TPEC:
5358 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5359 * Supported on almost all ThinkPads
5361 * Fan speed changes of any sort (including those caused by the
5362 * disengaged mode) are usually done slowly by the firmware as the
5363 * maximum ammount of fan duty cycle change per second seems to be
5364 * limited.
5366 * Reading is not available if GFAN exists.
5367 * Writing is not available if SFAN exists.
5369 * Bits
5370 * 7 automatic mode engaged;
5371 * (default operation mode of the ThinkPad)
5372 * fan level is ignored in this mode.
5373 * 6 full speed mode (takes precedence over bit 7);
5374 * not available on all thinkpads. May disable
5375 * the tachometer while the fan controller ramps up
5376 * the speed (which can take up to a few *minutes*).
5377 * Speeds up fan to 100% duty-cycle, which is far above
5378 * the standard RPM levels. It is not impossible that
5379 * it could cause hardware damage.
5380 * 5-3 unused in some models. Extra bits for fan level
5381 * in others, but still useless as all values above
5382 * 7 map to the same speed as level 7 in these models.
5383 * 2-0 fan level (0..7 usually)
5384 * 0x00 = stop
5385 * 0x07 = max (set when temperatures critical)
5386 * Some ThinkPads may have other levels, see
5387 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5389 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5390 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5391 * does so, its initial value is meaningless (0x07).
5393 * For firmware bugs, refer to:
5394 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5396 * ----
5398 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5399 * Main fan tachometer reading (in RPM)
5401 * This register is present on all ThinkPads with a new-style EC, and
5402 * it is known not to be present on the A21m/e, and T22, as there is
5403 * something else in offset 0x84 according to the ACPI DSDT. Other
5404 * ThinkPads from this same time period (and earlier) probably lack the
5405 * tachometer as well.
5407 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5408 * was never fixed by IBM to report the EC firmware version string
5409 * probably support the tachometer (like the early X models), so
5410 * detecting it is quite hard. We need more data to know for sure.
5412 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5413 * might result.
5415 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5416 * mode.
5418 * For firmware bugs, refer to:
5419 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5421 * TPACPI_FAN_WR_ACPI_FANS:
5422 * ThinkPad X31, X40, X41. Not available in the X60.
5424 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5425 * high speed. ACPI DSDT seems to map these three speeds to levels
5426 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5427 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5429 * The speeds are stored on handles
5430 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5432 * There are three default speed sets, acessible as handles:
5433 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5435 * ACPI DSDT switches which set is in use depending on various
5436 * factors.
5438 * TPACPI_FAN_WR_TPEC is also available and should be used to
5439 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5440 * but the ACPI tables just mention level 7.
5443 enum { /* Fan control constants */
5444 fan_status_offset = 0x2f, /* EC register 0x2f */
5445 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5446 * 0x84 must be read before 0x85 */
5448 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5449 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5451 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5454 enum fan_status_access_mode {
5455 TPACPI_FAN_NONE = 0, /* No fan status or control */
5456 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5457 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5460 enum fan_control_access_mode {
5461 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5462 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5463 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5464 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5467 enum fan_control_commands {
5468 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5469 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5470 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5471 * and also watchdog cmd */
5474 static int fan_control_allowed;
5476 static enum fan_status_access_mode fan_status_access_mode;
5477 static enum fan_control_access_mode fan_control_access_mode;
5478 static enum fan_control_commands fan_control_commands;
5480 static u8 fan_control_initial_status;
5481 static u8 fan_control_desired_level;
5482 static u8 fan_control_resume_level;
5483 static int fan_watchdog_maxinterval;
5485 static struct mutex fan_mutex;
5487 static void fan_watchdog_fire(struct work_struct *ignored);
5488 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5490 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5491 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5492 "\\FSPD", /* 600e/x, 770e, 770x */
5493 ); /* all others */
5494 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5495 "JFNS", /* 770x-JL */
5496 ); /* all others */
5499 * Call with fan_mutex held
5501 static void fan_update_desired_level(u8 status)
5503 if ((status &
5504 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5505 if (status > 7)
5506 fan_control_desired_level = 7;
5507 else
5508 fan_control_desired_level = status;
5512 static int fan_get_status(u8 *status)
5514 u8 s;
5516 /* TODO:
5517 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5519 switch (fan_status_access_mode) {
5520 case TPACPI_FAN_RD_ACPI_GFAN:
5521 /* 570, 600e/x, 770e, 770x */
5523 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5524 return -EIO;
5526 if (likely(status))
5527 *status = s & 0x07;
5529 break;
5531 case TPACPI_FAN_RD_TPEC:
5532 /* all except 570, 600e/x, 770e, 770x */
5533 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5534 return -EIO;
5536 if (likely(status))
5537 *status = s;
5539 break;
5541 default:
5542 return -ENXIO;
5545 return 0;
5548 static int fan_get_status_safe(u8 *status)
5550 int rc;
5551 u8 s;
5553 if (mutex_lock_interruptible(&fan_mutex))
5554 return -ERESTARTSYS;
5555 rc = fan_get_status(&s);
5556 if (!rc)
5557 fan_update_desired_level(s);
5558 mutex_unlock(&fan_mutex);
5560 if (status)
5561 *status = s;
5563 return rc;
5566 static int fan_get_speed(unsigned int *speed)
5568 u8 hi, lo;
5570 switch (fan_status_access_mode) {
5571 case TPACPI_FAN_RD_TPEC:
5572 /* all except 570, 600e/x, 770e, 770x */
5573 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5574 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5575 return -EIO;
5577 if (likely(speed))
5578 *speed = (hi << 8) | lo;
5580 break;
5582 default:
5583 return -ENXIO;
5586 return 0;
5589 static int fan_set_level(int level)
5591 if (!fan_control_allowed)
5592 return -EPERM;
5594 switch (fan_control_access_mode) {
5595 case TPACPI_FAN_WR_ACPI_SFAN:
5596 if (level >= 0 && level <= 7) {
5597 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5598 return -EIO;
5599 } else
5600 return -EINVAL;
5601 break;
5603 case TPACPI_FAN_WR_ACPI_FANS:
5604 case TPACPI_FAN_WR_TPEC:
5605 if (!(level & TP_EC_FAN_AUTO) &&
5606 !(level & TP_EC_FAN_FULLSPEED) &&
5607 ((level < 0) || (level > 7)))
5608 return -EINVAL;
5610 /* safety net should the EC not support AUTO
5611 * or FULLSPEED mode bits and just ignore them */
5612 if (level & TP_EC_FAN_FULLSPEED)
5613 level |= 7; /* safety min speed 7 */
5614 else if (level & TP_EC_FAN_AUTO)
5615 level |= 4; /* safety min speed 4 */
5617 if (!acpi_ec_write(fan_status_offset, level))
5618 return -EIO;
5619 else
5620 tp_features.fan_ctrl_status_undef = 0;
5621 break;
5623 default:
5624 return -ENXIO;
5626 return 0;
5629 static int fan_set_level_safe(int level)
5631 int rc;
5633 if (!fan_control_allowed)
5634 return -EPERM;
5636 if (mutex_lock_interruptible(&fan_mutex))
5637 return -ERESTARTSYS;
5639 if (level == TPACPI_FAN_LAST_LEVEL)
5640 level = fan_control_desired_level;
5642 rc = fan_set_level(level);
5643 if (!rc)
5644 fan_update_desired_level(level);
5646 mutex_unlock(&fan_mutex);
5647 return rc;
5650 static int fan_set_enable(void)
5652 u8 s;
5653 int rc;
5655 if (!fan_control_allowed)
5656 return -EPERM;
5658 if (mutex_lock_interruptible(&fan_mutex))
5659 return -ERESTARTSYS;
5661 switch (fan_control_access_mode) {
5662 case TPACPI_FAN_WR_ACPI_FANS:
5663 case TPACPI_FAN_WR_TPEC:
5664 rc = fan_get_status(&s);
5665 if (rc < 0)
5666 break;
5668 /* Don't go out of emergency fan mode */
5669 if (s != 7) {
5670 s &= 0x07;
5671 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5674 if (!acpi_ec_write(fan_status_offset, s))
5675 rc = -EIO;
5676 else {
5677 tp_features.fan_ctrl_status_undef = 0;
5678 rc = 0;
5680 break;
5682 case TPACPI_FAN_WR_ACPI_SFAN:
5683 rc = fan_get_status(&s);
5684 if (rc < 0)
5685 break;
5687 s &= 0x07;
5689 /* Set fan to at least level 4 */
5690 s |= 4;
5692 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5693 rc = -EIO;
5694 else
5695 rc = 0;
5696 break;
5698 default:
5699 rc = -ENXIO;
5702 mutex_unlock(&fan_mutex);
5703 return rc;
5706 static int fan_set_disable(void)
5708 int rc;
5710 if (!fan_control_allowed)
5711 return -EPERM;
5713 if (mutex_lock_interruptible(&fan_mutex))
5714 return -ERESTARTSYS;
5716 rc = 0;
5717 switch (fan_control_access_mode) {
5718 case TPACPI_FAN_WR_ACPI_FANS:
5719 case TPACPI_FAN_WR_TPEC:
5720 if (!acpi_ec_write(fan_status_offset, 0x00))
5721 rc = -EIO;
5722 else {
5723 fan_control_desired_level = 0;
5724 tp_features.fan_ctrl_status_undef = 0;
5726 break;
5728 case TPACPI_FAN_WR_ACPI_SFAN:
5729 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5730 rc = -EIO;
5731 else
5732 fan_control_desired_level = 0;
5733 break;
5735 default:
5736 rc = -ENXIO;
5740 mutex_unlock(&fan_mutex);
5741 return rc;
5744 static int fan_set_speed(int speed)
5746 int rc;
5748 if (!fan_control_allowed)
5749 return -EPERM;
5751 if (mutex_lock_interruptible(&fan_mutex))
5752 return -ERESTARTSYS;
5754 rc = 0;
5755 switch (fan_control_access_mode) {
5756 case TPACPI_FAN_WR_ACPI_FANS:
5757 if (speed >= 0 && speed <= 65535) {
5758 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5759 speed, speed, speed))
5760 rc = -EIO;
5761 } else
5762 rc = -EINVAL;
5763 break;
5765 default:
5766 rc = -ENXIO;
5769 mutex_unlock(&fan_mutex);
5770 return rc;
5773 static void fan_watchdog_reset(void)
5775 static int fan_watchdog_active;
5777 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5778 return;
5780 if (fan_watchdog_active)
5781 cancel_delayed_work(&fan_watchdog_task);
5783 if (fan_watchdog_maxinterval > 0 &&
5784 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5785 fan_watchdog_active = 1;
5786 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5787 msecs_to_jiffies(fan_watchdog_maxinterval
5788 * 1000))) {
5789 printk(TPACPI_ERR
5790 "failed to queue the fan watchdog, "
5791 "watchdog will not trigger\n");
5793 } else
5794 fan_watchdog_active = 0;
5797 static void fan_watchdog_fire(struct work_struct *ignored)
5799 int rc;
5801 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5802 return;
5804 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5805 rc = fan_set_enable();
5806 if (rc < 0) {
5807 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5808 "will try again later...\n", -rc);
5809 /* reschedule for later */
5810 fan_watchdog_reset();
5815 * SYSFS fan layout: hwmon compatible (device)
5817 * pwm*_enable:
5818 * 0: "disengaged" mode
5819 * 1: manual mode
5820 * 2: native EC "auto" mode (recommended, hardware default)
5822 * pwm*: set speed in manual mode, ignored otherwise.
5823 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5824 * interpolation.
5826 * fan*_input: tachometer reading, RPM
5829 * SYSFS fan layout: extensions
5831 * fan_watchdog (driver):
5832 * fan watchdog interval in seconds, 0 disables (default), max 120
5835 /* sysfs fan pwm1_enable ----------------------------------------------- */
5836 static ssize_t fan_pwm1_enable_show(struct device *dev,
5837 struct device_attribute *attr,
5838 char *buf)
5840 int res, mode;
5841 u8 status;
5843 res = fan_get_status_safe(&status);
5844 if (res)
5845 return res;
5847 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5848 if (status != fan_control_initial_status) {
5849 tp_features.fan_ctrl_status_undef = 0;
5850 } else {
5851 /* Return most likely status. In fact, it
5852 * might be the only possible status */
5853 status = TP_EC_FAN_AUTO;
5857 if (status & TP_EC_FAN_FULLSPEED) {
5858 mode = 0;
5859 } else if (status & TP_EC_FAN_AUTO) {
5860 mode = 2;
5861 } else
5862 mode = 1;
5864 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5867 static ssize_t fan_pwm1_enable_store(struct device *dev,
5868 struct device_attribute *attr,
5869 const char *buf, size_t count)
5871 unsigned long t;
5872 int res, level;
5874 if (parse_strtoul(buf, 2, &t))
5875 return -EINVAL;
5877 switch (t) {
5878 case 0:
5879 level = TP_EC_FAN_FULLSPEED;
5880 break;
5881 case 1:
5882 level = TPACPI_FAN_LAST_LEVEL;
5883 break;
5884 case 2:
5885 level = TP_EC_FAN_AUTO;
5886 break;
5887 case 3:
5888 /* reserved for software-controlled auto mode */
5889 return -ENOSYS;
5890 default:
5891 return -EINVAL;
5894 res = fan_set_level_safe(level);
5895 if (res == -ENXIO)
5896 return -EINVAL;
5897 else if (res < 0)
5898 return res;
5900 fan_watchdog_reset();
5902 return count;
5905 static struct device_attribute dev_attr_fan_pwm1_enable =
5906 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5907 fan_pwm1_enable_show, fan_pwm1_enable_store);
5909 /* sysfs fan pwm1 ------------------------------------------------------ */
5910 static ssize_t fan_pwm1_show(struct device *dev,
5911 struct device_attribute *attr,
5912 char *buf)
5914 int res;
5915 u8 status;
5917 res = fan_get_status_safe(&status);
5918 if (res)
5919 return res;
5921 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5922 if (status != fan_control_initial_status) {
5923 tp_features.fan_ctrl_status_undef = 0;
5924 } else {
5925 status = TP_EC_FAN_AUTO;
5929 if ((status &
5930 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5931 status = fan_control_desired_level;
5933 if (status > 7)
5934 status = 7;
5936 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5939 static ssize_t fan_pwm1_store(struct device *dev,
5940 struct device_attribute *attr,
5941 const char *buf, size_t count)
5943 unsigned long s;
5944 int rc;
5945 u8 status, newlevel;
5947 if (parse_strtoul(buf, 255, &s))
5948 return -EINVAL;
5950 /* scale down from 0-255 to 0-7 */
5951 newlevel = (s >> 5) & 0x07;
5953 if (mutex_lock_interruptible(&fan_mutex))
5954 return -ERESTARTSYS;
5956 rc = fan_get_status(&status);
5957 if (!rc && (status &
5958 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5959 rc = fan_set_level(newlevel);
5960 if (rc == -ENXIO)
5961 rc = -EINVAL;
5962 else if (!rc) {
5963 fan_update_desired_level(newlevel);
5964 fan_watchdog_reset();
5968 mutex_unlock(&fan_mutex);
5969 return (rc)? rc : count;
5972 static struct device_attribute dev_attr_fan_pwm1 =
5973 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5974 fan_pwm1_show, fan_pwm1_store);
5976 /* sysfs fan fan1_input ------------------------------------------------ */
5977 static ssize_t fan_fan1_input_show(struct device *dev,
5978 struct device_attribute *attr,
5979 char *buf)
5981 int res;
5982 unsigned int speed;
5984 res = fan_get_speed(&speed);
5985 if (res < 0)
5986 return res;
5988 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5991 static struct device_attribute dev_attr_fan_fan1_input =
5992 __ATTR(fan1_input, S_IRUGO,
5993 fan_fan1_input_show, NULL);
5995 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5996 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5997 char *buf)
5999 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6002 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6003 const char *buf, size_t count)
6005 unsigned long t;
6007 if (parse_strtoul(buf, 120, &t))
6008 return -EINVAL;
6010 if (!fan_control_allowed)
6011 return -EPERM;
6013 fan_watchdog_maxinterval = t;
6014 fan_watchdog_reset();
6016 return count;
6019 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6020 fan_fan_watchdog_show, fan_fan_watchdog_store);
6022 /* --------------------------------------------------------------------- */
6023 static struct attribute *fan_attributes[] = {
6024 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6025 &dev_attr_fan_fan1_input.attr,
6026 NULL
6029 static const struct attribute_group fan_attr_group = {
6030 .attrs = fan_attributes,
6033 static int __init fan_init(struct ibm_init_struct *iibm)
6035 int rc;
6037 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6039 mutex_init(&fan_mutex);
6040 fan_status_access_mode = TPACPI_FAN_NONE;
6041 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6042 fan_control_commands = 0;
6043 fan_watchdog_maxinterval = 0;
6044 tp_features.fan_ctrl_status_undef = 0;
6045 fan_control_desired_level = 7;
6047 TPACPI_ACPIHANDLE_INIT(fans);
6048 TPACPI_ACPIHANDLE_INIT(gfan);
6049 TPACPI_ACPIHANDLE_INIT(sfan);
6051 if (gfan_handle) {
6052 /* 570, 600e/x, 770e, 770x */
6053 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6054 } else {
6055 /* all other ThinkPads: note that even old-style
6056 * ThinkPad ECs supports the fan control register */
6057 if (likely(acpi_ec_read(fan_status_offset,
6058 &fan_control_initial_status))) {
6059 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6061 /* In some ThinkPads, neither the EC nor the ACPI
6062 * DSDT initialize the fan status, and it ends up
6063 * being set to 0x07 when it *could* be either
6064 * 0x07 or 0x80.
6066 * Enable for TP-1Y (T43), TP-78 (R51e),
6067 * TP-76 (R52), TP-70 (T43, R52), which are known
6068 * to be buggy. */
6069 if (fan_control_initial_status == 0x07) {
6070 switch (thinkpad_id.ec_model) {
6071 case 0x5931: /* TP-1Y */
6072 case 0x3837: /* TP-78 */
6073 case 0x3637: /* TP-76 */
6074 case 0x3037: /* TP-70 */
6075 printk(TPACPI_NOTICE
6076 "fan_init: initial fan status "
6077 "is unknown, assuming it is "
6078 "in auto mode\n");
6079 tp_features.fan_ctrl_status_undef = 1;
6083 } else {
6084 printk(TPACPI_ERR
6085 "ThinkPad ACPI EC access misbehaving, "
6086 "fan status and control unavailable\n");
6087 return 1;
6091 if (sfan_handle) {
6092 /* 570, 770x-JL */
6093 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6094 fan_control_commands |=
6095 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6096 } else {
6097 if (!gfan_handle) {
6098 /* gfan without sfan means no fan control */
6099 /* all other models implement TP EC 0x2f control */
6101 if (fans_handle) {
6102 /* X31, X40, X41 */
6103 fan_control_access_mode =
6104 TPACPI_FAN_WR_ACPI_FANS;
6105 fan_control_commands |=
6106 TPACPI_FAN_CMD_SPEED |
6107 TPACPI_FAN_CMD_LEVEL |
6108 TPACPI_FAN_CMD_ENABLE;
6109 } else {
6110 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6111 fan_control_commands |=
6112 TPACPI_FAN_CMD_LEVEL |
6113 TPACPI_FAN_CMD_ENABLE;
6118 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6119 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6120 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6121 fan_status_access_mode, fan_control_access_mode);
6123 /* fan control master switch */
6124 if (!fan_control_allowed) {
6125 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6126 fan_control_commands = 0;
6127 dbg_printk(TPACPI_DBG_INIT,
6128 "fan control features disabled by parameter\n");
6131 /* update fan_control_desired_level */
6132 if (fan_status_access_mode != TPACPI_FAN_NONE)
6133 fan_get_status_safe(NULL);
6135 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6136 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6137 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6138 &fan_attr_group);
6139 if (rc < 0)
6140 return rc;
6142 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6143 &driver_attr_fan_watchdog);
6144 if (rc < 0) {
6145 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6146 &fan_attr_group);
6147 return rc;
6149 return 0;
6150 } else
6151 return 1;
6154 static void fan_exit(void)
6156 vdbg_printk(TPACPI_DBG_EXIT,
6157 "cancelling any pending fan watchdog tasks\n");
6159 /* FIXME: can we really do this unconditionally? */
6160 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6161 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6162 &driver_attr_fan_watchdog);
6164 cancel_delayed_work(&fan_watchdog_task);
6165 flush_workqueue(tpacpi_wq);
6168 static void fan_suspend(pm_message_t state)
6170 int rc;
6172 if (!fan_control_allowed)
6173 return;
6175 /* Store fan status in cache */
6176 fan_control_resume_level = 0;
6177 rc = fan_get_status_safe(&fan_control_resume_level);
6178 if (rc < 0)
6179 printk(TPACPI_NOTICE
6180 "failed to read fan level for later "
6181 "restore during resume: %d\n", rc);
6183 /* if it is undefined, don't attempt to restore it.
6184 * KEEP THIS LAST */
6185 if (tp_features.fan_ctrl_status_undef)
6186 fan_control_resume_level = 0;
6189 static void fan_resume(void)
6191 u8 current_level = 7;
6192 bool do_set = false;
6193 int rc;
6195 /* DSDT *always* updates status on resume */
6196 tp_features.fan_ctrl_status_undef = 0;
6198 if (!fan_control_allowed ||
6199 !fan_control_resume_level ||
6200 (fan_get_status_safe(&current_level) < 0))
6201 return;
6203 switch (fan_control_access_mode) {
6204 case TPACPI_FAN_WR_ACPI_SFAN:
6205 /* never decrease fan level */
6206 do_set = (fan_control_resume_level > current_level);
6207 break;
6208 case TPACPI_FAN_WR_ACPI_FANS:
6209 case TPACPI_FAN_WR_TPEC:
6210 /* never decrease fan level, scale is:
6211 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6213 * We expect the firmware to set either 7 or AUTO, but we
6214 * handle FULLSPEED out of paranoia.
6216 * So, we can safely only restore FULLSPEED or 7, anything
6217 * else could slow the fan. Restoring AUTO is useless, at
6218 * best that's exactly what the DSDT already set (it is the
6219 * slower it uses).
6221 * Always keep in mind that the DSDT *will* have set the
6222 * fans to what the vendor supposes is the best level. We
6223 * muck with it only to speed the fan up.
6225 if (fan_control_resume_level != 7 &&
6226 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6227 return;
6228 else
6229 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6230 (current_level != fan_control_resume_level);
6231 break;
6232 default:
6233 return;
6235 if (do_set) {
6236 printk(TPACPI_NOTICE
6237 "restoring fan level to 0x%02x\n",
6238 fan_control_resume_level);
6239 rc = fan_set_level_safe(fan_control_resume_level);
6240 if (rc < 0)
6241 printk(TPACPI_NOTICE
6242 "failed to restore fan level: %d\n", rc);
6246 static int fan_read(char *p)
6248 int len = 0;
6249 int rc;
6250 u8 status;
6251 unsigned int speed = 0;
6253 switch (fan_status_access_mode) {
6254 case TPACPI_FAN_RD_ACPI_GFAN:
6255 /* 570, 600e/x, 770e, 770x */
6256 rc = fan_get_status_safe(&status);
6257 if (rc < 0)
6258 return rc;
6260 len += sprintf(p + len, "status:\t\t%s\n"
6261 "level:\t\t%d\n",
6262 (status != 0) ? "enabled" : "disabled", status);
6263 break;
6265 case TPACPI_FAN_RD_TPEC:
6266 /* all except 570, 600e/x, 770e, 770x */
6267 rc = fan_get_status_safe(&status);
6268 if (rc < 0)
6269 return rc;
6271 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6272 if (status != fan_control_initial_status)
6273 tp_features.fan_ctrl_status_undef = 0;
6274 else
6275 /* Return most likely status. In fact, it
6276 * might be the only possible status */
6277 status = TP_EC_FAN_AUTO;
6280 len += sprintf(p + len, "status:\t\t%s\n",
6281 (status != 0) ? "enabled" : "disabled");
6283 rc = fan_get_speed(&speed);
6284 if (rc < 0)
6285 return rc;
6287 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6289 if (status & TP_EC_FAN_FULLSPEED)
6290 /* Disengaged mode takes precedence */
6291 len += sprintf(p + len, "level:\t\tdisengaged\n");
6292 else if (status & TP_EC_FAN_AUTO)
6293 len += sprintf(p + len, "level:\t\tauto\n");
6294 else
6295 len += sprintf(p + len, "level:\t\t%d\n", status);
6296 break;
6298 case TPACPI_FAN_NONE:
6299 default:
6300 len += sprintf(p + len, "status:\t\tnot supported\n");
6303 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6304 len += sprintf(p + len, "commands:\tlevel <level>");
6306 switch (fan_control_access_mode) {
6307 case TPACPI_FAN_WR_ACPI_SFAN:
6308 len += sprintf(p + len, " (<level> is 0-7)\n");
6309 break;
6311 default:
6312 len += sprintf(p + len, " (<level> is 0-7, "
6313 "auto, disengaged, full-speed)\n");
6314 break;
6318 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6319 len += sprintf(p + len, "commands:\tenable, disable\n"
6320 "commands:\twatchdog <timeout> (<timeout> "
6321 "is 0 (off), 1-120 (seconds))\n");
6323 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6324 len += sprintf(p + len, "commands:\tspeed <speed>"
6325 " (<speed> is 0-65535)\n");
6327 return len;
6330 static int fan_write_cmd_level(const char *cmd, int *rc)
6332 int level;
6334 if (strlencmp(cmd, "level auto") == 0)
6335 level = TP_EC_FAN_AUTO;
6336 else if ((strlencmp(cmd, "level disengaged") == 0) |
6337 (strlencmp(cmd, "level full-speed") == 0))
6338 level = TP_EC_FAN_FULLSPEED;
6339 else if (sscanf(cmd, "level %d", &level) != 1)
6340 return 0;
6342 *rc = fan_set_level_safe(level);
6343 if (*rc == -ENXIO)
6344 printk(TPACPI_ERR "level command accepted for unsupported "
6345 "access mode %d", fan_control_access_mode);
6347 return 1;
6350 static int fan_write_cmd_enable(const char *cmd, int *rc)
6352 if (strlencmp(cmd, "enable") != 0)
6353 return 0;
6355 *rc = fan_set_enable();
6356 if (*rc == -ENXIO)
6357 printk(TPACPI_ERR "enable command accepted for unsupported "
6358 "access mode %d", fan_control_access_mode);
6360 return 1;
6363 static int fan_write_cmd_disable(const char *cmd, int *rc)
6365 if (strlencmp(cmd, "disable") != 0)
6366 return 0;
6368 *rc = fan_set_disable();
6369 if (*rc == -ENXIO)
6370 printk(TPACPI_ERR "disable command accepted for unsupported "
6371 "access mode %d", fan_control_access_mode);
6373 return 1;
6376 static int fan_write_cmd_speed(const char *cmd, int *rc)
6378 int speed;
6380 /* TODO:
6381 * Support speed <low> <medium> <high> ? */
6383 if (sscanf(cmd, "speed %d", &speed) != 1)
6384 return 0;
6386 *rc = fan_set_speed(speed);
6387 if (*rc == -ENXIO)
6388 printk(TPACPI_ERR "speed command accepted for unsupported "
6389 "access mode %d", fan_control_access_mode);
6391 return 1;
6394 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6396 int interval;
6398 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6399 return 0;
6401 if (interval < 0 || interval > 120)
6402 *rc = -EINVAL;
6403 else
6404 fan_watchdog_maxinterval = interval;
6406 return 1;
6409 static int fan_write(char *buf)
6411 char *cmd;
6412 int rc = 0;
6414 while (!rc && (cmd = next_cmd(&buf))) {
6415 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6416 fan_write_cmd_level(cmd, &rc)) &&
6417 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6418 (fan_write_cmd_enable(cmd, &rc) ||
6419 fan_write_cmd_disable(cmd, &rc) ||
6420 fan_write_cmd_watchdog(cmd, &rc))) &&
6421 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6422 fan_write_cmd_speed(cmd, &rc))
6424 rc = -EINVAL;
6425 else if (!rc)
6426 fan_watchdog_reset();
6429 return rc;
6432 static struct ibm_struct fan_driver_data = {
6433 .name = "fan",
6434 .read = fan_read,
6435 .write = fan_write,
6436 .exit = fan_exit,
6437 .suspend = fan_suspend,
6438 .resume = fan_resume,
6441 /****************************************************************************
6442 ****************************************************************************
6444 * Infrastructure
6446 ****************************************************************************
6447 ****************************************************************************/
6449 /* sysfs name ---------------------------------------------------------- */
6450 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6451 struct device_attribute *attr,
6452 char *buf)
6454 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6457 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6458 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6460 /* --------------------------------------------------------------------- */
6462 /* /proc support */
6463 static struct proc_dir_entry *proc_dir;
6466 * Module and infrastructure proble, init and exit handling
6469 static int force_load;
6471 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6472 static const char * __init str_supported(int is_supported)
6474 static char text_unsupported[] __initdata = "not supported";
6476 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6478 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6480 static void ibm_exit(struct ibm_struct *ibm)
6482 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6484 list_del_init(&ibm->all_drivers);
6486 if (ibm->flags.acpi_notify_installed) {
6487 dbg_printk(TPACPI_DBG_EXIT,
6488 "%s: acpi_remove_notify_handler\n", ibm->name);
6489 BUG_ON(!ibm->acpi);
6490 acpi_remove_notify_handler(*ibm->acpi->handle,
6491 ibm->acpi->type,
6492 dispatch_acpi_notify);
6493 ibm->flags.acpi_notify_installed = 0;
6494 ibm->flags.acpi_notify_installed = 0;
6497 if (ibm->flags.proc_created) {
6498 dbg_printk(TPACPI_DBG_EXIT,
6499 "%s: remove_proc_entry\n", ibm->name);
6500 remove_proc_entry(ibm->name, proc_dir);
6501 ibm->flags.proc_created = 0;
6504 if (ibm->flags.acpi_driver_registered) {
6505 dbg_printk(TPACPI_DBG_EXIT,
6506 "%s: acpi_bus_unregister_driver\n", ibm->name);
6507 BUG_ON(!ibm->acpi);
6508 acpi_bus_unregister_driver(ibm->acpi->driver);
6509 kfree(ibm->acpi->driver);
6510 ibm->acpi->driver = NULL;
6511 ibm->flags.acpi_driver_registered = 0;
6514 if (ibm->flags.init_called && ibm->exit) {
6515 ibm->exit();
6516 ibm->flags.init_called = 0;
6519 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6522 static int __init ibm_init(struct ibm_init_struct *iibm)
6524 int ret;
6525 struct ibm_struct *ibm = iibm->data;
6526 struct proc_dir_entry *entry;
6528 BUG_ON(ibm == NULL);
6530 INIT_LIST_HEAD(&ibm->all_drivers);
6532 if (ibm->flags.experimental && !experimental)
6533 return 0;
6535 dbg_printk(TPACPI_DBG_INIT,
6536 "probing for %s\n", ibm->name);
6538 if (iibm->init) {
6539 ret = iibm->init(iibm);
6540 if (ret > 0)
6541 return 0; /* probe failed */
6542 if (ret)
6543 return ret;
6545 ibm->flags.init_called = 1;
6548 if (ibm->acpi) {
6549 if (ibm->acpi->hid) {
6550 ret = register_tpacpi_subdriver(ibm);
6551 if (ret)
6552 goto err_out;
6555 if (ibm->acpi->notify) {
6556 ret = setup_acpi_notify(ibm);
6557 if (ret == -ENODEV) {
6558 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6559 ibm->name);
6560 ret = 0;
6561 goto err_out;
6563 if (ret < 0)
6564 goto err_out;
6568 dbg_printk(TPACPI_DBG_INIT,
6569 "%s installed\n", ibm->name);
6571 if (ibm->read) {
6572 entry = create_proc_entry(ibm->name,
6573 S_IFREG | S_IRUGO | S_IWUSR,
6574 proc_dir);
6575 if (!entry) {
6576 printk(TPACPI_ERR "unable to create proc entry %s\n",
6577 ibm->name);
6578 ret = -ENODEV;
6579 goto err_out;
6581 entry->owner = THIS_MODULE;
6582 entry->data = ibm;
6583 entry->read_proc = &dispatch_procfs_read;
6584 if (ibm->write)
6585 entry->write_proc = &dispatch_procfs_write;
6586 ibm->flags.proc_created = 1;
6589 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6591 return 0;
6593 err_out:
6594 dbg_printk(TPACPI_DBG_INIT,
6595 "%s: at error exit path with result %d\n",
6596 ibm->name, ret);
6598 ibm_exit(ibm);
6599 return (ret < 0)? ret : 0;
6602 /* Probing */
6604 /* returns 0 - probe ok, or < 0 - probe error.
6605 * Probe ok doesn't mean thinkpad found.
6606 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6607 static int __must_check __init get_thinkpad_model_data(
6608 struct thinkpad_id_data *tp)
6610 const struct dmi_device *dev = NULL;
6611 char ec_fw_string[18];
6612 char const *s;
6614 if (!tp)
6615 return -EINVAL;
6617 memset(tp, 0, sizeof(*tp));
6619 if (dmi_name_in_vendors("IBM"))
6620 tp->vendor = PCI_VENDOR_ID_IBM;
6621 else if (dmi_name_in_vendors("LENOVO"))
6622 tp->vendor = PCI_VENDOR_ID_LENOVO;
6623 else
6624 return 0;
6626 s = dmi_get_system_info(DMI_BIOS_VERSION);
6627 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6628 if (s && !tp->bios_version_str)
6629 return -ENOMEM;
6630 if (!tp->bios_version_str)
6631 return 0;
6632 tp->bios_model = tp->bios_version_str[0]
6633 | (tp->bios_version_str[1] << 8);
6636 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6637 * X32 or newer, all Z series; Some models must have an
6638 * up-to-date BIOS or they will not be detected.
6640 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6642 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6643 if (sscanf(dev->name,
6644 "IBM ThinkPad Embedded Controller -[%17c",
6645 ec_fw_string) == 1) {
6646 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6647 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6649 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6650 if (!tp->ec_version_str)
6651 return -ENOMEM;
6652 tp->ec_model = ec_fw_string[0]
6653 | (ec_fw_string[1] << 8);
6654 break;
6658 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6659 if (s && !strnicmp(s, "ThinkPad", 8)) {
6660 tp->model_str = kstrdup(s, GFP_KERNEL);
6661 if (!tp->model_str)
6662 return -ENOMEM;
6665 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6666 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6667 if (s && !tp->nummodel_str)
6668 return -ENOMEM;
6670 return 0;
6673 static int __init probe_for_thinkpad(void)
6675 int is_thinkpad;
6677 if (acpi_disabled)
6678 return -ENODEV;
6681 * Non-ancient models have better DMI tagging, but very old models
6682 * don't.
6684 is_thinkpad = (thinkpad_id.model_str != NULL);
6686 /* ec is required because many other handles are relative to it */
6687 TPACPI_ACPIHANDLE_INIT(ec);
6688 if (!ec_handle) {
6689 if (is_thinkpad)
6690 printk(TPACPI_ERR
6691 "Not yet supported ThinkPad detected!\n");
6692 return -ENODEV;
6696 * Risks a regression on very old machines, but reduces potential
6697 * false positives a damn great deal
6699 if (!is_thinkpad)
6700 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6702 if (!is_thinkpad && !force_load)
6703 return -ENODEV;
6705 return 0;
6709 /* Module init, exit, parameters */
6711 static struct ibm_init_struct ibms_init[] __initdata = {
6713 .init = thinkpad_acpi_driver_init,
6714 .data = &thinkpad_acpi_driver_data,
6717 .init = hotkey_init,
6718 .data = &hotkey_driver_data,
6721 .init = bluetooth_init,
6722 .data = &bluetooth_driver_data,
6725 .init = wan_init,
6726 .data = &wan_driver_data,
6728 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6730 .init = video_init,
6731 .data = &video_driver_data,
6733 #endif
6735 .init = light_init,
6736 .data = &light_driver_data,
6738 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6740 .init = dock_init,
6741 .data = &dock_driver_data[0],
6744 .init = dock_init2,
6745 .data = &dock_driver_data[1],
6747 #endif
6748 #ifdef CONFIG_THINKPAD_ACPI_BAY
6750 .init = bay_init,
6751 .data = &bay_driver_data,
6753 #endif
6755 .init = cmos_init,
6756 .data = &cmos_driver_data,
6759 .init = led_init,
6760 .data = &led_driver_data,
6763 .init = beep_init,
6764 .data = &beep_driver_data,
6767 .init = thermal_init,
6768 .data = &thermal_driver_data,
6771 .data = &ecdump_driver_data,
6774 .init = brightness_init,
6775 .data = &brightness_driver_data,
6778 .data = &volume_driver_data,
6781 .init = fan_init,
6782 .data = &fan_driver_data,
6786 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6788 unsigned int i;
6789 struct ibm_struct *ibm;
6791 if (!kp || !kp->name || !val)
6792 return -EINVAL;
6794 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6795 ibm = ibms_init[i].data;
6796 WARN_ON(ibm == NULL);
6798 if (!ibm || !ibm->name)
6799 continue;
6801 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6802 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6803 return -ENOSPC;
6804 strcpy(ibms_init[i].param, val);
6805 strcat(ibms_init[i].param, ",");
6806 return 0;
6810 return -EINVAL;
6813 module_param(experimental, int, 0);
6814 MODULE_PARM_DESC(experimental,
6815 "Enables experimental features when non-zero");
6817 module_param_named(debug, dbg_level, uint, 0);
6818 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6820 module_param(force_load, bool, 0);
6821 MODULE_PARM_DESC(force_load,
6822 "Attempts to load the driver even on a "
6823 "mis-identified ThinkPad when true");
6825 module_param_named(fan_control, fan_control_allowed, bool, 0);
6826 MODULE_PARM_DESC(fan_control,
6827 "Enables setting fan parameters features when true");
6829 module_param_named(brightness_mode, brightness_mode, int, 0);
6830 MODULE_PARM_DESC(brightness_mode,
6831 "Selects brightness control strategy: "
6832 "0=auto, 1=EC, 2=CMOS, 3=both");
6834 module_param(brightness_enable, uint, 0);
6835 MODULE_PARM_DESC(brightness_enable,
6836 "Enables backlight control when 1, disables when 0");
6838 module_param(hotkey_report_mode, uint, 0);
6839 MODULE_PARM_DESC(hotkey_report_mode,
6840 "used for backwards compatibility with userspace, "
6841 "see documentation");
6843 #define TPACPI_PARAM(feature) \
6844 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6845 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6846 "at module load, see documentation")
6848 TPACPI_PARAM(hotkey);
6849 TPACPI_PARAM(bluetooth);
6850 TPACPI_PARAM(video);
6851 TPACPI_PARAM(light);
6852 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6853 TPACPI_PARAM(dock);
6854 #endif
6855 #ifdef CONFIG_THINKPAD_ACPI_BAY
6856 TPACPI_PARAM(bay);
6857 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6858 TPACPI_PARAM(cmos);
6859 TPACPI_PARAM(led);
6860 TPACPI_PARAM(beep);
6861 TPACPI_PARAM(ecdump);
6862 TPACPI_PARAM(brightness);
6863 TPACPI_PARAM(volume);
6864 TPACPI_PARAM(fan);
6866 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
6867 module_param(dbg_wlswemul, uint, 0);
6868 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
6869 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
6870 MODULE_PARM_DESC(wlsw_state,
6871 "Initial state of the emulated WLSW switch");
6873 module_param(dbg_bluetoothemul, uint, 0);
6874 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
6875 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
6876 MODULE_PARM_DESC(bluetooth_state,
6877 "Initial state of the emulated bluetooth switch");
6879 module_param(dbg_wwanemul, uint, 0);
6880 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
6881 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
6882 MODULE_PARM_DESC(wwan_state,
6883 "Initial state of the emulated WWAN switch");
6884 #endif
6886 static void thinkpad_acpi_module_exit(void)
6888 struct ibm_struct *ibm, *itmp;
6890 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6892 list_for_each_entry_safe_reverse(ibm, itmp,
6893 &tpacpi_all_drivers,
6894 all_drivers) {
6895 ibm_exit(ibm);
6898 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6900 if (tpacpi_inputdev) {
6901 if (tp_features.input_device_registered)
6902 input_unregister_device(tpacpi_inputdev);
6903 else
6904 input_free_device(tpacpi_inputdev);
6907 if (tpacpi_hwmon)
6908 hwmon_device_unregister(tpacpi_hwmon);
6910 if (tp_features.sensors_pdev_attrs_registered)
6911 device_remove_file(&tpacpi_sensors_pdev->dev,
6912 &dev_attr_thinkpad_acpi_pdev_name);
6913 if (tpacpi_sensors_pdev)
6914 platform_device_unregister(tpacpi_sensors_pdev);
6915 if (tpacpi_pdev)
6916 platform_device_unregister(tpacpi_pdev);
6918 if (tp_features.sensors_pdrv_attrs_registered)
6919 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6920 if (tp_features.platform_drv_attrs_registered)
6921 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6923 if (tp_features.sensors_pdrv_registered)
6924 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6926 if (tp_features.platform_drv_registered)
6927 platform_driver_unregister(&tpacpi_pdriver);
6929 if (proc_dir)
6930 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6932 if (tpacpi_wq)
6933 destroy_workqueue(tpacpi_wq);
6935 kfree(thinkpad_id.bios_version_str);
6936 kfree(thinkpad_id.ec_version_str);
6937 kfree(thinkpad_id.model_str);
6941 static int __init thinkpad_acpi_module_init(void)
6943 int ret, i;
6945 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6947 /* Parameter checking */
6948 if (hotkey_report_mode > 2)
6949 return -EINVAL;
6951 /* Driver-level probe */
6953 ret = get_thinkpad_model_data(&thinkpad_id);
6954 if (ret) {
6955 printk(TPACPI_ERR
6956 "unable to get DMI data: %d\n", ret);
6957 thinkpad_acpi_module_exit();
6958 return ret;
6960 ret = probe_for_thinkpad();
6961 if (ret) {
6962 thinkpad_acpi_module_exit();
6963 return ret;
6966 /* Driver initialization */
6968 TPACPI_ACPIHANDLE_INIT(ecrd);
6969 TPACPI_ACPIHANDLE_INIT(ecwr);
6971 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6972 if (!tpacpi_wq) {
6973 thinkpad_acpi_module_exit();
6974 return -ENOMEM;
6977 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6978 if (!proc_dir) {
6979 printk(TPACPI_ERR
6980 "unable to create proc dir " TPACPI_PROC_DIR);
6981 thinkpad_acpi_module_exit();
6982 return -ENODEV;
6984 proc_dir->owner = THIS_MODULE;
6986 ret = platform_driver_register(&tpacpi_pdriver);
6987 if (ret) {
6988 printk(TPACPI_ERR
6989 "unable to register main platform driver\n");
6990 thinkpad_acpi_module_exit();
6991 return ret;
6993 tp_features.platform_drv_registered = 1;
6995 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6996 if (ret) {
6997 printk(TPACPI_ERR
6998 "unable to register hwmon platform driver\n");
6999 thinkpad_acpi_module_exit();
7000 return ret;
7002 tp_features.sensors_pdrv_registered = 1;
7004 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7005 if (!ret) {
7006 tp_features.platform_drv_attrs_registered = 1;
7007 ret = tpacpi_create_driver_attributes(
7008 &tpacpi_hwmon_pdriver.driver);
7010 if (ret) {
7011 printk(TPACPI_ERR
7012 "unable to create sysfs driver attributes\n");
7013 thinkpad_acpi_module_exit();
7014 return ret;
7016 tp_features.sensors_pdrv_attrs_registered = 1;
7019 /* Device initialization */
7020 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7021 NULL, 0);
7022 if (IS_ERR(tpacpi_pdev)) {
7023 ret = PTR_ERR(tpacpi_pdev);
7024 tpacpi_pdev = NULL;
7025 printk(TPACPI_ERR "unable to register platform device\n");
7026 thinkpad_acpi_module_exit();
7027 return ret;
7029 tpacpi_sensors_pdev = platform_device_register_simple(
7030 TPACPI_HWMON_DRVR_NAME,
7031 -1, NULL, 0);
7032 if (IS_ERR(tpacpi_sensors_pdev)) {
7033 ret = PTR_ERR(tpacpi_sensors_pdev);
7034 tpacpi_sensors_pdev = NULL;
7035 printk(TPACPI_ERR
7036 "unable to register hwmon platform device\n");
7037 thinkpad_acpi_module_exit();
7038 return ret;
7040 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7041 &dev_attr_thinkpad_acpi_pdev_name);
7042 if (ret) {
7043 printk(TPACPI_ERR
7044 "unable to create sysfs hwmon device attributes\n");
7045 thinkpad_acpi_module_exit();
7046 return ret;
7048 tp_features.sensors_pdev_attrs_registered = 1;
7049 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7050 if (IS_ERR(tpacpi_hwmon)) {
7051 ret = PTR_ERR(tpacpi_hwmon);
7052 tpacpi_hwmon = NULL;
7053 printk(TPACPI_ERR "unable to register hwmon device\n");
7054 thinkpad_acpi_module_exit();
7055 return ret;
7057 mutex_init(&tpacpi_inputdev_send_mutex);
7058 tpacpi_inputdev = input_allocate_device();
7059 if (!tpacpi_inputdev) {
7060 printk(TPACPI_ERR "unable to allocate input device\n");
7061 thinkpad_acpi_module_exit();
7062 return -ENOMEM;
7063 } else {
7064 /* Prepare input device, but don't register */
7065 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7066 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7067 tpacpi_inputdev->id.bustype = BUS_HOST;
7068 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7069 thinkpad_id.vendor :
7070 PCI_VENDOR_ID_IBM;
7071 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7072 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7074 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7075 ret = ibm_init(&ibms_init[i]);
7076 if (ret >= 0 && *ibms_init[i].param)
7077 ret = ibms_init[i].data->write(ibms_init[i].param);
7078 if (ret < 0) {
7079 thinkpad_acpi_module_exit();
7080 return ret;
7083 ret = input_register_device(tpacpi_inputdev);
7084 if (ret < 0) {
7085 printk(TPACPI_ERR "unable to register input device\n");
7086 thinkpad_acpi_module_exit();
7087 return ret;
7088 } else {
7089 tp_features.input_device_registered = 1;
7092 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7093 return 0;
7096 /* Please remove this in year 2009 */
7097 MODULE_ALIAS("ibm_acpi");
7099 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7102 * DMI matching for module autoloading
7104 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7105 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7107 * Only models listed in thinkwiki will be supported, so add yours
7108 * if it is not there yet.
7110 #define IBM_BIOS_MODULE_ALIAS(__type) \
7111 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7113 /* Non-ancient thinkpads */
7114 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7115 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7117 /* Ancient thinkpad BIOSes have to be identified by
7118 * BIOS type or model number, and there are far less
7119 * BIOS types than model numbers... */
7120 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7121 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7122 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7124 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7125 MODULE_DESCRIPTION(TPACPI_DESC);
7126 MODULE_VERSION(TPACPI_VERSION);
7127 MODULE_LICENSE("GPL");
7129 module_init(thinkpad_acpi_module_init);
7130 module_exit(thinkpad_acpi_module_exit);