ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob8651c805f1815bb201d853a53b4994ce9a41027e
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.19-20080213"
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 <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
98 /* NVRAM bit masks */
99 enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
124 * Main driver
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
172 struct ibm_struct;
174 struct tp_acpi_drv_struct {
175 char *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
184 struct ibm_struct {
185 char *name;
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
206 struct ibm_init_struct {
207 char param[32];
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219 #endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 hotkey_tablet:1;
225 u32 light:1;
226 u32 light_status:1;
227 u32 bright_16levels:1;
228 u32 bright_acpimode:1;
229 u32 wan:1;
230 u32 fan_ctrl_status_undef:1;
231 u32 input_device_registered:1;
232 u32 hotkey_report_mode_locked:1;
233 u32 platform_drv_registered:1;
234 u32 platform_drv_attrs_registered:1;
235 u32 sensors_pdrv_registered:1;
236 u32 sensors_pdrv_attrs_registered:1;
237 u32 sensors_pdev_attrs_registered:1;
238 u32 hotkey_poll_active:1;
239 } tp_features;
241 struct thinkpad_id_data {
242 unsigned int vendor; /* ThinkPad vendor:
243 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
245 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
246 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
248 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
249 u16 ec_model;
251 char *model_str;
253 static struct thinkpad_id_data thinkpad_id;
255 static enum {
256 TPACPI_LIFE_INIT = 0,
257 TPACPI_LIFE_RUNNING,
258 TPACPI_LIFE_EXITING,
259 } tpacpi_lifecycle;
261 static int experimental;
262 static u32 dbg_level;
264 /****************************************************************************
265 ****************************************************************************
267 * ACPI Helpers and device model
269 ****************************************************************************
270 ****************************************************************************/
272 /*************************************************************************
273 * ACPI basic handles
276 static acpi_handle root_handle;
278 #define TPACPI_HANDLE(object, parent, paths...) \
279 static acpi_handle object##_handle; \
280 static acpi_handle *object##_parent = &parent##_handle; \
281 static char *object##_path; \
282 static char *object##_paths[] = { paths }
284 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
285 "\\_SB.PCI.ISA.EC", /* 570 */
286 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
287 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
288 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
289 "\\_SB.PCI0.ICH3.EC0", /* R31 */
290 "\\_SB.PCI0.LPC.EC", /* all others */
293 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
294 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
296 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
297 /* T4x, X31, X40 */
298 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
299 "\\CMS", /* R40, R40e */
300 ); /* all others */
302 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
303 "^HKEY", /* R30, R31 */
304 "HKEY", /* all others */
305 ); /* 570 */
307 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
308 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
309 "\\_SB.PCI0.VID0", /* 770e */
310 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
311 "\\_SB.PCI0.AGP.VID", /* all others */
312 ); /* R30, R31 */
315 /*************************************************************************
316 * ACPI helpers
319 static int acpi_evalf(acpi_handle handle,
320 void *res, char *method, char *fmt, ...)
322 char *fmt0 = fmt;
323 struct acpi_object_list params;
324 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
325 struct acpi_buffer result, *resultp;
326 union acpi_object out_obj;
327 acpi_status status;
328 va_list ap;
329 char res_type;
330 int success;
331 int quiet;
333 if (!*fmt) {
334 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
335 return 0;
338 if (*fmt == 'q') {
339 quiet = 1;
340 fmt++;
341 } else
342 quiet = 0;
344 res_type = *(fmt++);
346 params.count = 0;
347 params.pointer = &in_objs[0];
349 va_start(ap, fmt);
350 while (*fmt) {
351 char c = *(fmt++);
352 switch (c) {
353 case 'd': /* int */
354 in_objs[params.count].integer.value = va_arg(ap, int);
355 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
356 break;
357 /* add more types as needed */
358 default:
359 printk(TPACPI_ERR "acpi_evalf() called "
360 "with invalid format character '%c'\n", c);
361 return 0;
364 va_end(ap);
366 if (res_type != 'v') {
367 result.length = sizeof(out_obj);
368 result.pointer = &out_obj;
369 resultp = &result;
370 } else
371 resultp = NULL;
373 status = acpi_evaluate_object(handle, method, &params, resultp);
375 switch (res_type) {
376 case 'd': /* int */
377 if (res)
378 *(int *)res = out_obj.integer.value;
379 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
380 break;
381 case 'v': /* void */
382 success = status == AE_OK;
383 break;
384 /* add more types as needed */
385 default:
386 printk(TPACPI_ERR "acpi_evalf() called "
387 "with invalid format character '%c'\n", res_type);
388 return 0;
391 if (!success && !quiet)
392 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
393 method, fmt0, status);
395 return success;
398 static int acpi_ec_read(int i, u8 *p)
400 int v;
402 if (ecrd_handle) {
403 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
404 return 0;
405 *p = v;
406 } else {
407 if (ec_read(i, p) < 0)
408 return 0;
411 return 1;
414 static int acpi_ec_write(int i, u8 v)
416 if (ecwr_handle) {
417 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
418 return 0;
419 } else {
420 if (ec_write(i, v) < 0)
421 return 0;
424 return 1;
427 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
428 static int _sta(acpi_handle handle)
430 int status;
432 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
433 status = 0;
435 return status;
437 #endif
439 static int issue_thinkpad_cmos_command(int cmos_cmd)
441 if (!cmos_handle)
442 return -ENXIO;
444 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
445 return -EIO;
447 return 0;
450 /*************************************************************************
451 * ACPI device model
454 #define TPACPI_ACPIHANDLE_INIT(object) \
455 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
456 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
458 static void drv_acpi_handle_init(char *name,
459 acpi_handle *handle, acpi_handle parent,
460 char **paths, int num_paths, char **path)
462 int i;
463 acpi_status status;
465 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
466 name);
468 for (i = 0; i < num_paths; i++) {
469 status = acpi_get_handle(parent, paths[i], handle);
470 if (ACPI_SUCCESS(status)) {
471 *path = paths[i];
472 dbg_printk(TPACPI_DBG_INIT,
473 "Found ACPI handle %s for %s\n",
474 *path, name);
475 return;
479 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
480 name);
481 *handle = NULL;
484 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
486 struct ibm_struct *ibm = data;
488 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
489 return;
491 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
492 return;
494 ibm->acpi->notify(ibm, event);
497 static int __init setup_acpi_notify(struct ibm_struct *ibm)
499 acpi_status status;
500 int rc;
502 BUG_ON(!ibm->acpi);
504 if (!*ibm->acpi->handle)
505 return 0;
507 vdbg_printk(TPACPI_DBG_INIT,
508 "setting up ACPI notify for %s\n", ibm->name);
510 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
511 if (rc < 0) {
512 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
513 ibm->name, rc);
514 return -ENODEV;
517 acpi_driver_data(ibm->acpi->device) = ibm;
518 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
519 TPACPI_ACPI_EVENT_PREFIX,
520 ibm->name);
522 status = acpi_install_notify_handler(*ibm->acpi->handle,
523 ibm->acpi->type, dispatch_acpi_notify, ibm);
524 if (ACPI_FAILURE(status)) {
525 if (status == AE_ALREADY_EXISTS) {
526 printk(TPACPI_NOTICE
527 "another device driver is already "
528 "handling %s events\n", ibm->name);
529 } else {
530 printk(TPACPI_ERR
531 "acpi_install_notify_handler(%s) failed: %d\n",
532 ibm->name, status);
534 return -ENODEV;
536 ibm->flags.acpi_notify_installed = 1;
537 return 0;
540 static int __init tpacpi_device_add(struct acpi_device *device)
542 return 0;
545 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
547 int rc;
549 dbg_printk(TPACPI_DBG_INIT,
550 "registering %s as an ACPI driver\n", ibm->name);
552 BUG_ON(!ibm->acpi);
554 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
555 if (!ibm->acpi->driver) {
556 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
557 return -ENOMEM;
560 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
561 ibm->acpi->driver->ids = ibm->acpi->hid;
562 ibm->acpi->driver->ops.add = &tpacpi_device_add;
564 rc = acpi_bus_register_driver(ibm->acpi->driver);
565 if (rc < 0) {
566 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
567 ibm->acpi->hid, rc);
568 kfree(ibm->acpi->driver);
569 ibm->acpi->driver = NULL;
570 } else if (!rc)
571 ibm->flags.acpi_driver_registered = 1;
573 return rc;
577 /****************************************************************************
578 ****************************************************************************
580 * Procfs Helpers
582 ****************************************************************************
583 ****************************************************************************/
585 static int dispatch_procfs_read(char *page, char **start, off_t off,
586 int count, int *eof, void *data)
588 struct ibm_struct *ibm = data;
589 int len;
591 if (!ibm || !ibm->read)
592 return -EINVAL;
594 len = ibm->read(page);
595 if (len < 0)
596 return len;
598 if (len <= off + count)
599 *eof = 1;
600 *start = page + off;
601 len -= off;
602 if (len > count)
603 len = count;
604 if (len < 0)
605 len = 0;
607 return len;
610 static int dispatch_procfs_write(struct file *file,
611 const char __user *userbuf,
612 unsigned long count, void *data)
614 struct ibm_struct *ibm = data;
615 char *kernbuf;
616 int ret;
618 if (!ibm || !ibm->write)
619 return -EINVAL;
621 kernbuf = kmalloc(count + 2, GFP_KERNEL);
622 if (!kernbuf)
623 return -ENOMEM;
625 if (copy_from_user(kernbuf, userbuf, count)) {
626 kfree(kernbuf);
627 return -EFAULT;
630 kernbuf[count] = 0;
631 strcat(kernbuf, ",");
632 ret = ibm->write(kernbuf);
633 if (ret == 0)
634 ret = count;
636 kfree(kernbuf);
638 return ret;
641 static char *next_cmd(char **cmds)
643 char *start = *cmds;
644 char *end;
646 while ((end = strchr(start, ',')) && end == start)
647 start = end + 1;
649 if (!end)
650 return NULL;
652 *end = 0;
653 *cmds = end + 1;
654 return start;
658 /****************************************************************************
659 ****************************************************************************
661 * Device model: input, hwmon and platform
663 ****************************************************************************
664 ****************************************************************************/
666 static struct platform_device *tpacpi_pdev;
667 static struct platform_device *tpacpi_sensors_pdev;
668 static struct class_device *tpacpi_hwmon;
669 static struct input_dev *tpacpi_inputdev;
670 static struct mutex tpacpi_inputdev_send_mutex;
671 static LIST_HEAD(tpacpi_all_drivers);
673 static int tpacpi_suspend_handler(struct platform_device *pdev,
674 pm_message_t state)
676 struct ibm_struct *ibm, *itmp;
678 list_for_each_entry_safe(ibm, itmp,
679 &tpacpi_all_drivers,
680 all_drivers) {
681 if (ibm->suspend)
682 (ibm->suspend)(state);
685 return 0;
688 static int tpacpi_resume_handler(struct platform_device *pdev)
690 struct ibm_struct *ibm, *itmp;
692 list_for_each_entry_safe(ibm, itmp,
693 &tpacpi_all_drivers,
694 all_drivers) {
695 if (ibm->resume)
696 (ibm->resume)();
699 return 0;
702 static struct platform_driver tpacpi_pdriver = {
703 .driver = {
704 .name = TPACPI_DRVR_NAME,
705 .owner = THIS_MODULE,
707 .suspend = tpacpi_suspend_handler,
708 .resume = tpacpi_resume_handler,
711 static struct platform_driver tpacpi_hwmon_pdriver = {
712 .driver = {
713 .name = TPACPI_HWMON_DRVR_NAME,
714 .owner = THIS_MODULE,
718 /*************************************************************************
719 * sysfs support helpers
722 struct attribute_set {
723 unsigned int members, max_members;
724 struct attribute_group group;
727 struct attribute_set_obj {
728 struct attribute_set s;
729 struct attribute *a;
730 } __attribute__((packed));
732 static struct attribute_set *create_attr_set(unsigned int max_members,
733 const char *name)
735 struct attribute_set_obj *sobj;
737 if (max_members == 0)
738 return NULL;
740 /* Allocates space for implicit NULL at the end too */
741 sobj = kzalloc(sizeof(struct attribute_set_obj) +
742 max_members * sizeof(struct attribute *),
743 GFP_KERNEL);
744 if (!sobj)
745 return NULL;
746 sobj->s.max_members = max_members;
747 sobj->s.group.attrs = &sobj->a;
748 sobj->s.group.name = name;
750 return &sobj->s;
753 #define destroy_attr_set(_set) \
754 kfree(_set);
756 /* not multi-threaded safe, use it in a single thread per set */
757 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
759 if (!s || !attr)
760 return -EINVAL;
762 if (s->members >= s->max_members)
763 return -ENOMEM;
765 s->group.attrs[s->members] = attr;
766 s->members++;
768 return 0;
771 static int add_many_to_attr_set(struct attribute_set *s,
772 struct attribute **attr,
773 unsigned int count)
775 int i, res;
777 for (i = 0; i < count; i++) {
778 res = add_to_attr_set(s, attr[i]);
779 if (res)
780 return res;
783 return 0;
786 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
788 sysfs_remove_group(kobj, &s->group);
789 destroy_attr_set(s);
792 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
793 sysfs_create_group(_kobj, &_attr_set->group)
795 static int parse_strtoul(const char *buf,
796 unsigned long max, unsigned long *value)
798 char *endp;
800 while (*buf && isspace(*buf))
801 buf++;
802 *value = simple_strtoul(buf, &endp, 0);
803 while (*endp && isspace(*endp))
804 endp++;
805 if (*endp || *value > max)
806 return -EINVAL;
808 return 0;
811 /*************************************************************************
812 * thinkpad-acpi driver attributes
815 /* interface_version --------------------------------------------------- */
816 static ssize_t tpacpi_driver_interface_version_show(
817 struct device_driver *drv,
818 char *buf)
820 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
823 static DRIVER_ATTR(interface_version, S_IRUGO,
824 tpacpi_driver_interface_version_show, NULL);
826 /* debug_level --------------------------------------------------------- */
827 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
828 char *buf)
830 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
833 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
834 const char *buf, size_t count)
836 unsigned long t;
838 if (parse_strtoul(buf, 0xffff, &t))
839 return -EINVAL;
841 dbg_level = t;
843 return count;
846 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
847 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
849 /* version ------------------------------------------------------------- */
850 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
851 char *buf)
853 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
854 TPACPI_DESC, TPACPI_VERSION);
857 static DRIVER_ATTR(version, S_IRUGO,
858 tpacpi_driver_version_show, NULL);
860 /* --------------------------------------------------------------------- */
862 static struct driver_attribute *tpacpi_driver_attributes[] = {
863 &driver_attr_debug_level, &driver_attr_version,
864 &driver_attr_interface_version,
867 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
869 int i, res;
871 i = 0;
872 res = 0;
873 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
874 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
875 i++;
878 return res;
881 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
883 int i;
885 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
886 driver_remove_file(drv, tpacpi_driver_attributes[i]);
889 /****************************************************************************
890 ****************************************************************************
892 * Subdrivers
894 ****************************************************************************
895 ****************************************************************************/
897 /*************************************************************************
898 * thinkpad-acpi init subdriver
901 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
903 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
904 printk(TPACPI_INFO "%s\n", TPACPI_URL);
906 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
907 (thinkpad_id.bios_version_str) ?
908 thinkpad_id.bios_version_str : "unknown",
909 (thinkpad_id.ec_version_str) ?
910 thinkpad_id.ec_version_str : "unknown");
912 if (thinkpad_id.vendor && thinkpad_id.model_str)
913 printk(TPACPI_INFO "%s %s\n",
914 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
915 "IBM" : ((thinkpad_id.vendor ==
916 PCI_VENDOR_ID_LENOVO) ?
917 "Lenovo" : "Unknown vendor"),
918 thinkpad_id.model_str);
920 return 0;
923 static int thinkpad_acpi_driver_read(char *p)
925 int len = 0;
927 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
928 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
930 return len;
933 static struct ibm_struct thinkpad_acpi_driver_data = {
934 .name = "driver",
935 .read = thinkpad_acpi_driver_read,
938 /*************************************************************************
939 * Hotkey subdriver
942 enum { /* hot key scan codes (derived from ACPI DSDT) */
943 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
944 TP_ACPI_HOTKEYSCAN_FNF2,
945 TP_ACPI_HOTKEYSCAN_FNF3,
946 TP_ACPI_HOTKEYSCAN_FNF4,
947 TP_ACPI_HOTKEYSCAN_FNF5,
948 TP_ACPI_HOTKEYSCAN_FNF6,
949 TP_ACPI_HOTKEYSCAN_FNF7,
950 TP_ACPI_HOTKEYSCAN_FNF8,
951 TP_ACPI_HOTKEYSCAN_FNF9,
952 TP_ACPI_HOTKEYSCAN_FNF10,
953 TP_ACPI_HOTKEYSCAN_FNF11,
954 TP_ACPI_HOTKEYSCAN_FNF12,
955 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
956 TP_ACPI_HOTKEYSCAN_FNINSERT,
957 TP_ACPI_HOTKEYSCAN_FNDELETE,
958 TP_ACPI_HOTKEYSCAN_FNHOME,
959 TP_ACPI_HOTKEYSCAN_FNEND,
960 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
961 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
962 TP_ACPI_HOTKEYSCAN_FNSPACE,
963 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
964 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
965 TP_ACPI_HOTKEYSCAN_MUTE,
966 TP_ACPI_HOTKEYSCAN_THINKPAD,
969 enum { /* Keys available through NVRAM polling */
970 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
971 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
974 enum { /* Positions of some of the keys in hotkey masks */
975 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
976 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
977 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
978 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
979 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
980 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
981 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
982 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
983 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
984 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
985 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
988 enum { /* NVRAM to ACPI HKEY group map */
989 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
990 TP_ACPI_HKEY_ZOOM_MASK |
991 TP_ACPI_HKEY_DISPSWTCH_MASK |
992 TP_ACPI_HKEY_HIBERNATE_MASK,
993 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
994 TP_ACPI_HKEY_BRGHTDWN_MASK,
995 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
996 TP_ACPI_HKEY_VOLDWN_MASK |
997 TP_ACPI_HKEY_MUTE_MASK,
1000 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1001 struct tp_nvram_state {
1002 u16 thinkpad_toggle:1;
1003 u16 zoom_toggle:1;
1004 u16 display_toggle:1;
1005 u16 thinklight_toggle:1;
1006 u16 hibernate_toggle:1;
1007 u16 displayexp_toggle:1;
1008 u16 display_state:1;
1009 u16 brightness_toggle:1;
1010 u16 volume_toggle:1;
1011 u16 mute:1;
1013 u8 brightness_level;
1014 u8 volume_level;
1017 static struct task_struct *tpacpi_hotkey_task;
1018 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1019 static int hotkey_poll_freq = 10; /* Hz */
1020 static struct mutex hotkey_thread_mutex;
1021 static struct mutex hotkey_thread_data_mutex;
1022 static unsigned int hotkey_config_change;
1024 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1026 #define hotkey_source_mask 0U
1028 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1030 static struct mutex hotkey_mutex;
1032 static enum { /* Reasons for waking up */
1033 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1034 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1035 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1036 } hotkey_wakeup_reason;
1038 static int hotkey_autosleep_ack;
1040 static int hotkey_orig_status;
1041 static u32 hotkey_orig_mask;
1042 static u32 hotkey_all_mask;
1043 static u32 hotkey_reserved_mask;
1044 static u32 hotkey_mask;
1046 static unsigned int hotkey_report_mode;
1048 static u16 *hotkey_keycode_map;
1050 static struct attribute_set *hotkey_dev_attributes;
1052 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1053 #define HOTKEY_CONFIG_CRITICAL_START \
1054 do { \
1055 mutex_lock(&hotkey_thread_data_mutex); \
1056 hotkey_config_change++; \
1057 } while (0);
1058 #define HOTKEY_CONFIG_CRITICAL_END \
1059 mutex_unlock(&hotkey_thread_data_mutex);
1060 #else
1061 #define HOTKEY_CONFIG_CRITICAL_START
1062 #define HOTKEY_CONFIG_CRITICAL_END
1063 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1065 /* HKEY.MHKG() return bits */
1066 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1068 static int hotkey_get_wlsw(int *status)
1070 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1071 return -EIO;
1072 return 0;
1075 static int hotkey_get_tablet_mode(int *status)
1077 int s;
1079 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1080 return -EIO;
1082 return ((s & TP_HOTKEY_TABLET_MASK) != 0);
1086 * Call with hotkey_mutex held
1088 static int hotkey_mask_get(void)
1090 u32 m = 0;
1092 if (tp_features.hotkey_mask) {
1093 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1094 return -EIO;
1096 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1098 return 0;
1102 * Call with hotkey_mutex held
1104 static int hotkey_mask_set(u32 mask)
1106 int i;
1107 int rc = 0;
1109 if (tp_features.hotkey_mask) {
1110 HOTKEY_CONFIG_CRITICAL_START
1111 for (i = 0; i < 32; i++) {
1112 u32 m = 1 << i;
1113 /* enable in firmware mask only keys not in NVRAM
1114 * mode, but enable the key in the cached hotkey_mask
1115 * regardless of mode, or the key will end up
1116 * disabled by hotkey_mask_get() */
1117 if (!acpi_evalf(hkey_handle,
1118 NULL, "MHKM", "vdd", i + 1,
1119 !!((mask & ~hotkey_source_mask) & m))) {
1120 rc = -EIO;
1121 break;
1122 } else {
1123 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1126 HOTKEY_CONFIG_CRITICAL_END
1128 /* hotkey_mask_get must be called unconditionally below */
1129 if (!hotkey_mask_get() && !rc &&
1130 (hotkey_mask & ~hotkey_source_mask) !=
1131 (mask & ~hotkey_source_mask)) {
1132 printk(TPACPI_NOTICE
1133 "requested hot key mask 0x%08x, but "
1134 "firmware forced it to 0x%08x\n",
1135 mask, hotkey_mask);
1137 } else {
1138 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1139 HOTKEY_CONFIG_CRITICAL_START
1140 hotkey_mask = mask & hotkey_source_mask;
1141 HOTKEY_CONFIG_CRITICAL_END
1142 hotkey_mask_get();
1143 if (hotkey_mask != mask) {
1144 printk(TPACPI_NOTICE
1145 "requested hot key mask 0x%08x, "
1146 "forced to 0x%08x (NVRAM poll mask is "
1147 "0x%08x): no firmware mask support\n",
1148 mask, hotkey_mask, hotkey_source_mask);
1150 #else
1151 hotkey_mask_get();
1152 rc = -ENXIO;
1153 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1156 return rc;
1159 static int hotkey_status_get(int *status)
1161 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1162 return -EIO;
1164 return 0;
1167 static int hotkey_status_set(int status)
1169 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1170 return -EIO;
1172 return 0;
1175 static void tpacpi_input_send_radiosw(void)
1177 int wlsw;
1179 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1180 mutex_lock(&tpacpi_inputdev_send_mutex);
1182 input_report_switch(tpacpi_inputdev,
1183 SW_RADIO, !!wlsw);
1184 input_sync(tpacpi_inputdev);
1186 mutex_unlock(&tpacpi_inputdev_send_mutex);
1190 static void tpacpi_input_send_tabletsw(void)
1192 int state;
1194 if (tp_features.hotkey_tablet &&
1195 !hotkey_get_tablet_mode(&state)) {
1196 mutex_lock(&tpacpi_inputdev_send_mutex);
1198 input_report_switch(tpacpi_inputdev,
1199 SW_TABLET_MODE, !!state);
1200 input_sync(tpacpi_inputdev);
1202 mutex_unlock(&tpacpi_inputdev_send_mutex);
1206 static void tpacpi_input_send_key(unsigned int scancode)
1208 unsigned int keycode;
1210 keycode = hotkey_keycode_map[scancode];
1212 if (keycode != KEY_RESERVED) {
1213 mutex_lock(&tpacpi_inputdev_send_mutex);
1215 input_report_key(tpacpi_inputdev, keycode, 1);
1216 if (keycode == KEY_UNKNOWN)
1217 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1218 scancode);
1219 input_sync(tpacpi_inputdev);
1221 input_report_key(tpacpi_inputdev, keycode, 0);
1222 if (keycode == KEY_UNKNOWN)
1223 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1224 scancode);
1225 input_sync(tpacpi_inputdev);
1227 mutex_unlock(&tpacpi_inputdev_send_mutex);
1231 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1232 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1234 static void tpacpi_hotkey_send_key(unsigned int scancode)
1236 tpacpi_input_send_key(scancode);
1237 if (hotkey_report_mode < 2) {
1238 acpi_bus_generate_event(ibm_hotkey_acpidriver.device,
1239 0x80, 0x1001 + scancode);
1243 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1245 u8 d;
1247 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1248 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1249 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1250 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1251 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1252 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1254 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1255 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1256 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1258 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1259 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1260 n->displayexp_toggle =
1261 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1263 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1264 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1265 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1266 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1267 n->brightness_toggle =
1268 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1270 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1271 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1272 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1273 >> TP_NVRAM_POS_LEVEL_VOLUME;
1274 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1275 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1279 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1280 do { \
1281 if ((mask & (1 << __scancode)) && \
1282 oldn->__member != newn->__member) \
1283 tpacpi_hotkey_send_key(__scancode); \
1284 } while (0)
1286 #define TPACPI_MAY_SEND_KEY(__scancode) \
1287 do { if (mask & (1 << __scancode)) \
1288 tpacpi_hotkey_send_key(__scancode); } while (0)
1290 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1291 struct tp_nvram_state *newn,
1292 u32 mask)
1294 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1295 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1296 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1297 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1299 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1301 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1303 /* handle volume */
1304 if (oldn->volume_toggle != newn->volume_toggle) {
1305 if (oldn->mute != newn->mute) {
1306 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1308 if (oldn->volume_level > newn->volume_level) {
1309 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1310 } else if (oldn->volume_level < newn->volume_level) {
1311 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1312 } else if (oldn->mute == newn->mute) {
1313 /* repeated key presses that didn't change state */
1314 if (newn->mute) {
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1316 } else if (newn->volume_level != 0) {
1317 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1318 } else {
1319 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1324 /* handle brightness */
1325 if (oldn->brightness_toggle != newn->brightness_toggle) {
1326 if (oldn->brightness_level < newn->brightness_level) {
1327 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1328 } else if (oldn->brightness_level > newn->brightness_level) {
1329 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1330 } else {
1331 /* repeated key presses that didn't change state */
1332 if (newn->brightness_level != 0) {
1333 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1334 } else {
1335 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1341 #undef TPACPI_COMPARE_KEY
1342 #undef TPACPI_MAY_SEND_KEY
1344 static int hotkey_kthread(void *data)
1346 struct tp_nvram_state s[2];
1347 u32 mask;
1348 unsigned int si, so;
1349 unsigned long t;
1350 unsigned int change_detector, must_reset;
1352 mutex_lock(&hotkey_thread_mutex);
1354 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1355 goto exit;
1357 so = 0;
1358 si = 1;
1359 t = 0;
1361 /* Initial state for compares */
1362 mutex_lock(&hotkey_thread_data_mutex);
1363 change_detector = hotkey_config_change;
1364 mask = hotkey_source_mask & hotkey_mask;
1365 mutex_unlock(&hotkey_thread_data_mutex);
1366 hotkey_read_nvram(&s[so], mask);
1368 while (!kthread_should_stop() && hotkey_poll_freq) {
1369 if (t == 0)
1370 t = 1000/hotkey_poll_freq;
1371 t = msleep_interruptible(t);
1372 if (unlikely(kthread_should_stop()))
1373 break;
1374 must_reset = try_to_freeze();
1375 if (t > 0 && !must_reset)
1376 continue;
1378 mutex_lock(&hotkey_thread_data_mutex);
1379 if (must_reset || hotkey_config_change != change_detector) {
1380 /* forget old state on thaw or config change */
1381 si = so;
1382 t = 0;
1383 change_detector = hotkey_config_change;
1385 mask = hotkey_source_mask & hotkey_mask;
1386 mutex_unlock(&hotkey_thread_data_mutex);
1388 if (likely(mask)) {
1389 hotkey_read_nvram(&s[si], mask);
1390 if (likely(si != so)) {
1391 hotkey_compare_and_issue_event(&s[so], &s[si],
1392 mask);
1396 so = si;
1397 si ^= 1;
1400 exit:
1401 mutex_unlock(&hotkey_thread_mutex);
1402 return 0;
1405 static void hotkey_poll_stop_sync(void)
1407 if (tpacpi_hotkey_task) {
1408 if (frozen(tpacpi_hotkey_task) ||
1409 freezing(tpacpi_hotkey_task))
1410 thaw_process(tpacpi_hotkey_task);
1412 kthread_stop(tpacpi_hotkey_task);
1413 tpacpi_hotkey_task = NULL;
1414 mutex_lock(&hotkey_thread_mutex);
1415 /* at this point, the thread did exit */
1416 mutex_unlock(&hotkey_thread_mutex);
1420 /* call with hotkey_mutex held */
1421 static void hotkey_poll_setup(int may_warn)
1423 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1424 hotkey_poll_freq > 0 &&
1425 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1426 if (!tpacpi_hotkey_task) {
1427 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1428 NULL,
1429 TPACPI_FILE "d");
1430 if (IS_ERR(tpacpi_hotkey_task)) {
1431 tpacpi_hotkey_task = NULL;
1432 printk(TPACPI_ERR
1433 "could not create kernel thread "
1434 "for hotkey polling\n");
1437 } else {
1438 hotkey_poll_stop_sync();
1439 if (may_warn &&
1440 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1441 printk(TPACPI_NOTICE
1442 "hot keys 0x%08x require polling, "
1443 "which is currently disabled\n",
1444 hotkey_source_mask);
1449 static void hotkey_poll_setup_safe(int may_warn)
1451 mutex_lock(&hotkey_mutex);
1452 hotkey_poll_setup(may_warn);
1453 mutex_unlock(&hotkey_mutex);
1456 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1458 static void hotkey_poll_setup_safe(int __unused)
1462 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1464 static int hotkey_inputdev_open(struct input_dev *dev)
1466 switch (tpacpi_lifecycle) {
1467 case TPACPI_LIFE_INIT:
1469 * hotkey_init will call hotkey_poll_setup_safe
1470 * at the appropriate moment
1472 return 0;
1473 case TPACPI_LIFE_EXITING:
1474 return -EBUSY;
1475 case TPACPI_LIFE_RUNNING:
1476 hotkey_poll_setup_safe(0);
1477 return 0;
1480 /* Should only happen if tpacpi_lifecycle is corrupt */
1481 BUG();
1482 return -EBUSY;
1485 static void hotkey_inputdev_close(struct input_dev *dev)
1487 /* disable hotkey polling when possible */
1488 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1489 hotkey_poll_setup_safe(0);
1492 /* sysfs hotkey enable ------------------------------------------------- */
1493 static ssize_t hotkey_enable_show(struct device *dev,
1494 struct device_attribute *attr,
1495 char *buf)
1497 int res, status;
1499 res = hotkey_status_get(&status);
1500 if (res)
1501 return res;
1503 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1506 static ssize_t hotkey_enable_store(struct device *dev,
1507 struct device_attribute *attr,
1508 const char *buf, size_t count)
1510 unsigned long t;
1511 int res;
1513 if (parse_strtoul(buf, 1, &t))
1514 return -EINVAL;
1516 res = hotkey_status_set(t);
1518 return (res) ? res : count;
1521 static struct device_attribute dev_attr_hotkey_enable =
1522 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1523 hotkey_enable_show, hotkey_enable_store);
1525 /* sysfs hotkey mask --------------------------------------------------- */
1526 static ssize_t hotkey_mask_show(struct device *dev,
1527 struct device_attribute *attr,
1528 char *buf)
1530 int res;
1532 if (mutex_lock_interruptible(&hotkey_mutex))
1533 return -ERESTARTSYS;
1534 res = hotkey_mask_get();
1535 mutex_unlock(&hotkey_mutex);
1537 return (res)?
1538 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1541 static ssize_t hotkey_mask_store(struct device *dev,
1542 struct device_attribute *attr,
1543 const char *buf, size_t count)
1545 unsigned long t;
1546 int res;
1548 if (parse_strtoul(buf, 0xffffffffUL, &t))
1549 return -EINVAL;
1551 if (mutex_lock_interruptible(&hotkey_mutex))
1552 return -ERESTARTSYS;
1554 res = hotkey_mask_set(t);
1556 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1557 hotkey_poll_setup(1);
1558 #endif
1560 mutex_unlock(&hotkey_mutex);
1562 return (res) ? res : count;
1565 static struct device_attribute dev_attr_hotkey_mask =
1566 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1567 hotkey_mask_show, hotkey_mask_store);
1569 /* sysfs hotkey bios_enabled ------------------------------------------- */
1570 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1571 struct device_attribute *attr,
1572 char *buf)
1574 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1577 static struct device_attribute dev_attr_hotkey_bios_enabled =
1578 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1580 /* sysfs hotkey bios_mask ---------------------------------------------- */
1581 static ssize_t hotkey_bios_mask_show(struct device *dev,
1582 struct device_attribute *attr,
1583 char *buf)
1585 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1588 static struct device_attribute dev_attr_hotkey_bios_mask =
1589 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1591 /* sysfs hotkey all_mask ----------------------------------------------- */
1592 static ssize_t hotkey_all_mask_show(struct device *dev,
1593 struct device_attribute *attr,
1594 char *buf)
1596 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1597 hotkey_all_mask | hotkey_source_mask);
1600 static struct device_attribute dev_attr_hotkey_all_mask =
1601 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1603 /* sysfs hotkey recommended_mask --------------------------------------- */
1604 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1605 struct device_attribute *attr,
1606 char *buf)
1608 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1609 (hotkey_all_mask | hotkey_source_mask)
1610 & ~hotkey_reserved_mask);
1613 static struct device_attribute dev_attr_hotkey_recommended_mask =
1614 __ATTR(hotkey_recommended_mask, S_IRUGO,
1615 hotkey_recommended_mask_show, NULL);
1617 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1619 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1620 static ssize_t hotkey_source_mask_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1624 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1627 static ssize_t hotkey_source_mask_store(struct device *dev,
1628 struct device_attribute *attr,
1629 const char *buf, size_t count)
1631 unsigned long t;
1633 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1634 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1635 return -EINVAL;
1637 if (mutex_lock_interruptible(&hotkey_mutex))
1638 return -ERESTARTSYS;
1640 HOTKEY_CONFIG_CRITICAL_START
1641 hotkey_source_mask = t;
1642 HOTKEY_CONFIG_CRITICAL_END
1644 hotkey_poll_setup(1);
1646 mutex_unlock(&hotkey_mutex);
1648 return count;
1651 static struct device_attribute dev_attr_hotkey_source_mask =
1652 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1653 hotkey_source_mask_show, hotkey_source_mask_store);
1655 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1656 static ssize_t hotkey_poll_freq_show(struct device *dev,
1657 struct device_attribute *attr,
1658 char *buf)
1660 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1663 static ssize_t hotkey_poll_freq_store(struct device *dev,
1664 struct device_attribute *attr,
1665 const char *buf, size_t count)
1667 unsigned long t;
1669 if (parse_strtoul(buf, 25, &t))
1670 return -EINVAL;
1672 if (mutex_lock_interruptible(&hotkey_mutex))
1673 return -ERESTARTSYS;
1675 hotkey_poll_freq = t;
1677 hotkey_poll_setup(1);
1678 mutex_unlock(&hotkey_mutex);
1680 return count;
1683 static struct device_attribute dev_attr_hotkey_poll_freq =
1684 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1685 hotkey_poll_freq_show, hotkey_poll_freq_store);
1687 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1689 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1690 static ssize_t hotkey_radio_sw_show(struct device *dev,
1691 struct device_attribute *attr,
1692 char *buf)
1694 int res, s;
1695 res = hotkey_get_wlsw(&s);
1696 if (res < 0)
1697 return res;
1699 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1702 static struct device_attribute dev_attr_hotkey_radio_sw =
1703 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1705 static void hotkey_radio_sw_notify_change(void)
1707 if (tp_features.hotkey_wlsw)
1708 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1709 "hotkey_radio_sw");
1712 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1713 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1714 struct device_attribute *attr,
1715 char *buf)
1717 int res, s;
1718 res = hotkey_get_tablet_mode(&s);
1719 if (res < 0)
1720 return res;
1722 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1725 static struct device_attribute dev_attr_hotkey_tablet_mode =
1726 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1728 static void hotkey_tablet_mode_notify_change(void)
1730 if (tp_features.hotkey_tablet)
1731 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1732 "hotkey_tablet_mode");
1735 /* sysfs hotkey report_mode -------------------------------------------- */
1736 static ssize_t hotkey_report_mode_show(struct device *dev,
1737 struct device_attribute *attr,
1738 char *buf)
1740 return snprintf(buf, PAGE_SIZE, "%d\n",
1741 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1744 static ssize_t hotkey_report_mode_store(struct device *dev,
1745 struct device_attribute *attr,
1746 const char *buf, size_t count)
1748 unsigned long t;
1749 int res = 0;
1751 if (parse_strtoul(buf, 2, &t) || (t < 1))
1752 return -EINVAL;
1754 if (t != hotkey_report_mode) {
1755 if (tp_features.hotkey_report_mode_locked)
1756 return -EPERM;
1757 hotkey_report_mode = t;
1758 printk(TPACPI_NOTICE "hot key report mode set to %d\n",
1759 hotkey_report_mode);
1762 return (res) ? res : count;
1765 static struct device_attribute dev_attr_hotkey_report_mode =
1766 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
1767 hotkey_report_mode_show, hotkey_report_mode_store);
1769 /* sysfs wakeup reason (pollable) -------------------------------------- */
1770 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1771 struct device_attribute *attr,
1772 char *buf)
1774 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1777 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1778 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1780 void hotkey_wakeup_reason_notify_change(void)
1782 if (tp_features.hotkey_mask)
1783 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1784 "wakeup_reason");
1787 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1788 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1789 struct device_attribute *attr,
1790 char *buf)
1792 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1795 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1796 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1797 hotkey_wakeup_hotunplug_complete_show, NULL);
1799 void hotkey_wakeup_hotunplug_complete_notify_change(void)
1801 if (tp_features.hotkey_mask)
1802 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1803 "wakeup_hotunplug_complete");
1806 /* --------------------------------------------------------------------- */
1808 static struct attribute *hotkey_attributes[] __initdata = {
1809 &dev_attr_hotkey_enable.attr,
1810 &dev_attr_hotkey_bios_enabled.attr,
1811 &dev_attr_hotkey_report_mode.attr,
1812 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1813 &dev_attr_hotkey_mask.attr,
1814 &dev_attr_hotkey_all_mask.attr,
1815 &dev_attr_hotkey_recommended_mask.attr,
1816 &dev_attr_hotkey_source_mask.attr,
1817 &dev_attr_hotkey_poll_freq.attr,
1818 #endif
1821 static struct attribute *hotkey_mask_attributes[] __initdata = {
1822 &dev_attr_hotkey_bios_mask.attr,
1823 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1824 &dev_attr_hotkey_mask.attr,
1825 &dev_attr_hotkey_all_mask.attr,
1826 &dev_attr_hotkey_recommended_mask.attr,
1827 #endif
1828 &dev_attr_hotkey_wakeup_reason.attr,
1829 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1832 static int __init hotkey_init(struct ibm_init_struct *iibm)
1834 /* Requirements for changing the default keymaps:
1836 * 1. Many of the keys are mapped to KEY_RESERVED for very
1837 * good reasons. Do not change them unless you have deep
1838 * knowledge on the IBM and Lenovo ThinkPad firmware for
1839 * the various ThinkPad models. The driver behaves
1840 * differently for KEY_RESERVED: such keys have their
1841 * hot key mask *unset* in mask_recommended, and also
1842 * in the initial hot key mask programmed into the
1843 * firmware at driver load time, which means the firm-
1844 * ware may react very differently if you change them to
1845 * something else;
1847 * 2. You must be subscribed to the linux-thinkpad and
1848 * ibm-acpi-devel mailing lists, and you should read the
1849 * list archives since 2007 if you want to change the
1850 * keymaps. This requirement exists so that you will
1851 * know the past history of problems with the thinkpad-
1852 * acpi driver keymaps, and also that you will be
1853 * listening to any bug reports;
1855 * 3. Do not send thinkpad-acpi specific patches directly to
1856 * for merging, *ever*. Send them to the linux-acpi
1857 * mailinglist for comments. Merging is to be done only
1858 * through acpi-test and the ACPI maintainer.
1860 * If the above is too much to ask, don't change the keymap.
1861 * Ask the thinkpad-acpi maintainer to do it, instead.
1863 static u16 ibm_keycode_map[] __initdata = {
1864 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1865 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1866 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1867 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1869 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1870 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1871 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1872 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1874 /* brightness: firmware always reacts to them, unless
1875 * X.org did some tricks in the radeon BIOS scratch
1876 * registers of *some* models */
1877 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1878 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1880 /* Thinklight: firmware always react to it */
1881 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1883 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1884 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1886 /* Volume: firmware always react to it and reprograms
1887 * the built-in *extra* mixer. Never map it to control
1888 * another mixer by default. */
1889 KEY_RESERVED, /* 0x14: VOLUME UP */
1890 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1891 KEY_RESERVED, /* 0x16: MUTE */
1893 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1895 /* (assignments unknown, please report if found) */
1896 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1897 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1899 static u16 lenovo_keycode_map[] __initdata = {
1900 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1901 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1902 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1903 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1905 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1906 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1907 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1908 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1910 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1911 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1913 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1915 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1916 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1918 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1919 * react to it and reprograms the built-in *extra* mixer.
1920 * Never map it to control another mixer by default.
1922 * T60?, T61, R60?, R61: firmware and EC tries to send
1923 * these over the regular keyboard, so these are no-ops,
1924 * but there are still weird bugs re. MUTE, so do not
1925 * change unless you get test reports from all Lenovo
1926 * models. May cause the BIOS to interfere with the
1927 * HDA mixer.
1929 KEY_RESERVED, /* 0x14: VOLUME UP */
1930 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1931 KEY_RESERVED, /* 0x16: MUTE */
1933 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1935 /* (assignments unknown, please report if found) */
1936 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1937 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1940 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1941 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1942 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1944 int res, i;
1945 int status;
1946 int hkeyv;
1948 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1950 BUG_ON(!tpacpi_inputdev);
1951 BUG_ON(tpacpi_inputdev->open != NULL ||
1952 tpacpi_inputdev->close != NULL);
1954 TPACPI_ACPIHANDLE_INIT(hkey);
1955 mutex_init(&hotkey_mutex);
1957 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1958 mutex_init(&hotkey_thread_mutex);
1959 mutex_init(&hotkey_thread_data_mutex);
1960 #endif
1962 /* hotkey not supported on 570 */
1963 tp_features.hotkey = hkey_handle != NULL;
1965 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1966 str_supported(tp_features.hotkey));
1968 if (tp_features.hotkey) {
1969 hotkey_dev_attributes = create_attr_set(13, NULL);
1970 if (!hotkey_dev_attributes)
1971 return -ENOMEM;
1972 res = add_many_to_attr_set(hotkey_dev_attributes,
1973 hotkey_attributes,
1974 ARRAY_SIZE(hotkey_attributes));
1975 if (res)
1976 return res;
1978 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1979 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1980 for HKEY interface version 0x100 */
1981 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1982 if ((hkeyv >> 8) != 1) {
1983 printk(TPACPI_ERR "unknown version of the "
1984 "HKEY interface: 0x%x\n", hkeyv);
1985 printk(TPACPI_ERR "please report this to %s\n",
1986 TPACPI_MAIL);
1987 } else {
1989 * MHKV 0x100 in A31, R40, R40e,
1990 * T4x, X31, and later
1992 tp_features.hotkey_mask = 1;
1996 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1997 str_supported(tp_features.hotkey_mask));
1999 if (tp_features.hotkey_mask) {
2000 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2001 "MHKA", "qd")) {
2002 printk(TPACPI_ERR
2003 "missing MHKA handler, "
2004 "please report this to %s\n",
2005 TPACPI_MAIL);
2006 /* FN+F12, FN+F4, FN+F3 */
2007 hotkey_all_mask = 0x080cU;
2011 /* hotkey_source_mask *must* be zero for
2012 * the first hotkey_mask_get */
2013 res = hotkey_status_get(&hotkey_orig_status);
2014 if (!res && tp_features.hotkey_mask) {
2015 res = hotkey_mask_get();
2016 hotkey_orig_mask = hotkey_mask;
2017 if (!res) {
2018 res = add_many_to_attr_set(
2019 hotkey_dev_attributes,
2020 hotkey_mask_attributes,
2021 ARRAY_SIZE(hotkey_mask_attributes));
2025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2026 if (tp_features.hotkey_mask) {
2027 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2028 & ~hotkey_all_mask;
2029 } else {
2030 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2033 vdbg_printk(TPACPI_DBG_INIT,
2034 "hotkey source mask 0x%08x, polling freq %d\n",
2035 hotkey_source_mask, hotkey_poll_freq);
2036 #endif
2038 /* Not all thinkpads have a hardware radio switch */
2039 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2040 tp_features.hotkey_wlsw = 1;
2041 printk(TPACPI_INFO
2042 "radio switch found; radios are %s\n",
2043 enabled(status, 0));
2044 res = add_to_attr_set(hotkey_dev_attributes,
2045 &dev_attr_hotkey_radio_sw.attr);
2048 /* For X41t, X60t, X61t Tablets... */
2049 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2050 tp_features.hotkey_tablet = 1;
2051 printk(TPACPI_INFO
2052 "possible tablet mode switch found; "
2053 "ThinkPad in %s mode\n",
2054 (status & TP_HOTKEY_TABLET_MASK)?
2055 "tablet" : "laptop");
2056 res = add_to_attr_set(hotkey_dev_attributes,
2057 &dev_attr_hotkey_tablet_mode.attr);
2060 if (!res)
2061 res = register_attr_set_with_sysfs(
2062 hotkey_dev_attributes,
2063 &tpacpi_pdev->dev.kobj);
2064 if (res)
2065 return res;
2067 /* Set up key map */
2069 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2070 GFP_KERNEL);
2071 if (!hotkey_keycode_map) {
2072 printk(TPACPI_ERR
2073 "failed to allocate memory for key map\n");
2074 return -ENOMEM;
2077 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2078 dbg_printk(TPACPI_DBG_INIT,
2079 "using Lenovo default hot key map\n");
2080 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2081 TPACPI_HOTKEY_MAP_SIZE);
2082 } else {
2083 dbg_printk(TPACPI_DBG_INIT,
2084 "using IBM default hot key map\n");
2085 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2086 TPACPI_HOTKEY_MAP_SIZE);
2089 /* Do not issue duplicate brightness change events to
2090 * userspace */
2091 if (tp_features.bright_acpimode) {
2092 printk(TPACPI_NOTICE
2093 "Brightness hot keys firmware issues ACPI "
2094 "video events\n");
2095 printk(TPACPI_NOTICE
2096 "Disabling brightness hot keys on event "
2097 "device keymap\n");
2098 hotkey_keycode_map[TP_ACPI_HOTKEYSCAN_FNHOME] =
2099 KEY_RESERVED;
2100 hotkey_keycode_map[TP_ACPI_HOTKEYSCAN_FNEND] =
2101 KEY_RESERVED;
2104 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2105 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2106 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2107 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2108 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2109 tpacpi_inputdev->keycode = hotkey_keycode_map;
2110 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2111 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2112 set_bit(hotkey_keycode_map[i],
2113 tpacpi_inputdev->keybit);
2114 } else {
2115 if (i < sizeof(hotkey_reserved_mask)*8)
2116 hotkey_reserved_mask |= 1 << i;
2120 if (tp_features.hotkey_wlsw) {
2121 set_bit(EV_SW, tpacpi_inputdev->evbit);
2122 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2124 if (tp_features.hotkey_tablet) {
2125 set_bit(EV_SW, tpacpi_inputdev->evbit);
2126 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2129 dbg_printk(TPACPI_DBG_INIT,
2130 "enabling hot key handling\n");
2131 res = hotkey_status_set(1);
2132 if (res)
2133 return res;
2134 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2135 & ~hotkey_reserved_mask)
2136 | hotkey_orig_mask);
2137 if (res < 0 && res != -ENXIO)
2138 return res;
2140 if (hotkey_report_mode > 0) {
2141 tp_features.hotkey_report_mode_locked = 1;
2142 } else {
2143 hotkey_report_mode = 1;
2144 vdbg_printk(TPACPI_DBG_INIT,
2145 "hot key reporting mode can be "
2146 "changed at runtime\n");
2149 dbg_printk(TPACPI_DBG_INIT,
2150 "legacy hot key reporting over procfs %s\n",
2151 (hotkey_report_mode < 2) ?
2152 "enabled" : "disabled");
2154 tpacpi_inputdev->open = &hotkey_inputdev_open;
2155 tpacpi_inputdev->close = &hotkey_inputdev_close;
2157 hotkey_poll_setup_safe(1);
2158 tpacpi_input_send_radiosw();
2159 tpacpi_input_send_tabletsw();
2162 return (tp_features.hotkey)? 0 : 1;
2165 static void hotkey_exit(void)
2167 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2168 hotkey_poll_stop_sync();
2169 #endif
2171 if (tp_features.hotkey) {
2172 dbg_printk(TPACPI_DBG_EXIT,
2173 "restoring original hot key mask\n");
2174 /* no short-circuit boolean operator below! */
2175 if ((hotkey_mask_set(hotkey_orig_mask) |
2176 hotkey_status_set(hotkey_orig_status)) != 0)
2177 printk(TPACPI_ERR
2178 "failed to restore hot key mask "
2179 "to BIOS defaults\n");
2182 if (hotkey_dev_attributes) {
2183 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2184 hotkey_dev_attributes = NULL;
2188 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2190 u32 hkey;
2191 unsigned int scancode;
2192 int send_acpi_ev;
2193 int ignore_acpi_ev;
2194 int unk_ev;
2196 if (event != 0x80) {
2197 printk(TPACPI_ERR
2198 "unknown HKEY notification event %d\n", event);
2199 /* forward it to userspace, maybe it knows how to handle it */
2200 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2202 return;
2205 while (1) {
2206 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2207 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2208 return;
2211 if (hkey == 0) {
2212 /* queue empty */
2213 return;
2216 send_acpi_ev = 1;
2217 ignore_acpi_ev = 0;
2218 unk_ev = 0;
2220 switch (hkey >> 12) {
2221 case 1:
2222 /* 0x1000-0x1FFF: key presses */
2223 scancode = hkey & 0xfff;
2224 if (scancode > 0 && scancode < 0x21) {
2225 scancode--;
2226 if (!(hotkey_source_mask & (1 << scancode))) {
2227 tpacpi_input_send_key(scancode);
2228 send_acpi_ev = 0;
2229 } else {
2230 ignore_acpi_ev = 1;
2232 } else {
2233 unk_ev = 1;
2235 break;
2236 case 2:
2237 /* Wakeup reason */
2238 switch (hkey) {
2239 case 0x2304: /* suspend, undock */
2240 case 0x2404: /* hibernation, undock */
2241 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2242 ignore_acpi_ev = 1;
2243 break;
2244 case 0x2305: /* suspend, bay eject */
2245 case 0x2405: /* hibernation, bay eject */
2246 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2247 ignore_acpi_ev = 1;
2248 break;
2249 default:
2250 unk_ev = 1;
2252 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2253 printk(TPACPI_INFO
2254 "woke up due to a hot-unplug "
2255 "request...\n");
2256 hotkey_wakeup_reason_notify_change();
2258 break;
2259 case 3:
2260 /* bay-related wakeups */
2261 if (hkey == 0x3003) {
2262 hotkey_autosleep_ack = 1;
2263 printk(TPACPI_INFO
2264 "bay ejected\n");
2265 hotkey_wakeup_hotunplug_complete_notify_change();
2266 } else {
2267 unk_ev = 1;
2269 break;
2270 case 4:
2271 /* dock-related wakeups */
2272 if (hkey == 0x4003) {
2273 hotkey_autosleep_ack = 1;
2274 printk(TPACPI_INFO
2275 "undocked\n");
2276 hotkey_wakeup_hotunplug_complete_notify_change();
2277 } else {
2278 unk_ev = 1;
2280 break;
2281 case 5:
2282 /* 0x5000-0x5FFF: human interface helpers */
2283 switch (hkey) {
2284 case 0x5010: /* Lenovo new BIOS: brightness changed */
2285 case 0x500b: /* X61t: tablet pen inserted into bay */
2286 case 0x500c: /* X61t: tablet pen removed from bay */
2287 break;
2288 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2289 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2290 tpacpi_input_send_tabletsw();
2291 hotkey_tablet_mode_notify_change();
2292 send_acpi_ev = 0;
2293 break;
2294 case 0x5001:
2295 case 0x5002:
2296 /* LID switch events. Do not propagate */
2297 ignore_acpi_ev = 1;
2298 break;
2299 default:
2300 unk_ev = 1;
2302 break;
2303 case 7:
2304 /* 0x7000-0x7FFF: misc */
2305 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2306 tpacpi_input_send_radiosw();
2307 hotkey_radio_sw_notify_change();
2308 send_acpi_ev = 0;
2309 break;
2311 /* fallthrough to default */
2312 default:
2313 unk_ev = 1;
2315 if (unk_ev) {
2316 printk(TPACPI_NOTICE
2317 "unhandled HKEY event 0x%04x\n", hkey);
2320 if (!ignore_acpi_ev &&
2321 (send_acpi_ev || hotkey_report_mode < 2)) {
2322 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
2327 static void hotkey_suspend(pm_message_t state)
2329 /* Do these on suspend, we get the events on early resume! */
2330 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2331 hotkey_autosleep_ack = 0;
2334 static void hotkey_resume(void)
2336 if (hotkey_mask_get())
2337 printk(TPACPI_ERR
2338 "error while trying to read hot key mask "
2339 "from firmware\n");
2340 tpacpi_input_send_radiosw();
2341 hotkey_radio_sw_notify_change();
2342 hotkey_tablet_mode_notify_change();
2343 hotkey_wakeup_reason_notify_change();
2344 hotkey_wakeup_hotunplug_complete_notify_change();
2345 hotkey_poll_setup_safe(0);
2348 /* procfs -------------------------------------------------------------- */
2349 static int hotkey_read(char *p)
2351 int res, status;
2352 int len = 0;
2354 if (!tp_features.hotkey) {
2355 len += sprintf(p + len, "status:\t\tnot supported\n");
2356 return len;
2359 if (mutex_lock_interruptible(&hotkey_mutex))
2360 return -ERESTARTSYS;
2361 res = hotkey_status_get(&status);
2362 if (!res)
2363 res = hotkey_mask_get();
2364 mutex_unlock(&hotkey_mutex);
2365 if (res)
2366 return res;
2368 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2369 if (tp_features.hotkey_mask) {
2370 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2371 len += sprintf(p + len,
2372 "commands:\tenable, disable, reset, <mask>\n");
2373 } else {
2374 len += sprintf(p + len, "mask:\t\tnot supported\n");
2375 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2378 return len;
2381 static int hotkey_write(char *buf)
2383 int res, status;
2384 u32 mask;
2385 char *cmd;
2387 if (!tp_features.hotkey)
2388 return -ENODEV;
2390 if (mutex_lock_interruptible(&hotkey_mutex))
2391 return -ERESTARTSYS;
2393 status = -1;
2394 mask = hotkey_mask;
2396 res = 0;
2397 while ((cmd = next_cmd(&buf))) {
2398 if (strlencmp(cmd, "enable") == 0) {
2399 status = 1;
2400 } else if (strlencmp(cmd, "disable") == 0) {
2401 status = 0;
2402 } else if (strlencmp(cmd, "reset") == 0) {
2403 status = hotkey_orig_status;
2404 mask = hotkey_orig_mask;
2405 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2406 /* mask set */
2407 } else if (sscanf(cmd, "%x", &mask) == 1) {
2408 /* mask set */
2409 } else {
2410 res = -EINVAL;
2411 goto errexit;
2414 if (status != -1)
2415 res = hotkey_status_set(status);
2417 if (!res && mask != hotkey_mask)
2418 res = hotkey_mask_set(mask);
2420 errexit:
2421 mutex_unlock(&hotkey_mutex);
2422 return res;
2425 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2426 .hid = TPACPI_ACPI_HKEY_HID,
2427 .notify = hotkey_notify,
2428 .handle = &hkey_handle,
2429 .type = ACPI_DEVICE_NOTIFY,
2432 static struct ibm_struct hotkey_driver_data = {
2433 .name = "hotkey",
2434 .read = hotkey_read,
2435 .write = hotkey_write,
2436 .exit = hotkey_exit,
2437 .resume = hotkey_resume,
2438 .suspend = hotkey_suspend,
2439 .acpi = &ibm_hotkey_acpidriver,
2442 /*************************************************************************
2443 * Bluetooth subdriver
2446 enum {
2447 /* ACPI GBDC/SBDC bits */
2448 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2449 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2450 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2453 static int bluetooth_get_radiosw(void);
2454 static int bluetooth_set_radiosw(int radio_on);
2456 /* sysfs bluetooth enable ---------------------------------------------- */
2457 static ssize_t bluetooth_enable_show(struct device *dev,
2458 struct device_attribute *attr,
2459 char *buf)
2461 int status;
2463 status = bluetooth_get_radiosw();
2464 if (status < 0)
2465 return status;
2467 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2470 static ssize_t bluetooth_enable_store(struct device *dev,
2471 struct device_attribute *attr,
2472 const char *buf, size_t count)
2474 unsigned long t;
2475 int res;
2477 if (parse_strtoul(buf, 1, &t))
2478 return -EINVAL;
2480 res = bluetooth_set_radiosw(t);
2482 return (res) ? res : count;
2485 static struct device_attribute dev_attr_bluetooth_enable =
2486 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2487 bluetooth_enable_show, bluetooth_enable_store);
2489 /* --------------------------------------------------------------------- */
2491 static struct attribute *bluetooth_attributes[] = {
2492 &dev_attr_bluetooth_enable.attr,
2493 NULL
2496 static const struct attribute_group bluetooth_attr_group = {
2497 .attrs = bluetooth_attributes,
2500 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2502 int res;
2503 int status = 0;
2505 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2507 TPACPI_ACPIHANDLE_INIT(hkey);
2509 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2510 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2511 tp_features.bluetooth = hkey_handle &&
2512 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2514 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2515 str_supported(tp_features.bluetooth),
2516 status);
2518 if (tp_features.bluetooth) {
2519 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2520 /* no bluetooth hardware present in system */
2521 tp_features.bluetooth = 0;
2522 dbg_printk(TPACPI_DBG_INIT,
2523 "bluetooth hardware not installed\n");
2524 } else {
2525 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2526 &bluetooth_attr_group);
2527 if (res)
2528 return res;
2532 return (tp_features.bluetooth)? 0 : 1;
2535 static void bluetooth_exit(void)
2537 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2538 &bluetooth_attr_group);
2541 static int bluetooth_get_radiosw(void)
2543 int status;
2545 if (!tp_features.bluetooth)
2546 return -ENODEV;
2548 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2549 return -EIO;
2551 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2554 static int bluetooth_set_radiosw(int radio_on)
2556 int status;
2558 if (!tp_features.bluetooth)
2559 return -ENODEV;
2561 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2562 return -EIO;
2563 if (radio_on)
2564 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2565 else
2566 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2567 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2568 return -EIO;
2570 return 0;
2573 /* procfs -------------------------------------------------------------- */
2574 static int bluetooth_read(char *p)
2576 int len = 0;
2577 int status = bluetooth_get_radiosw();
2579 if (!tp_features.bluetooth)
2580 len += sprintf(p + len, "status:\t\tnot supported\n");
2581 else {
2582 len += sprintf(p + len, "status:\t\t%s\n",
2583 (status)? "enabled" : "disabled");
2584 len += sprintf(p + len, "commands:\tenable, disable\n");
2587 return len;
2590 static int bluetooth_write(char *buf)
2592 char *cmd;
2594 if (!tp_features.bluetooth)
2595 return -ENODEV;
2597 while ((cmd = next_cmd(&buf))) {
2598 if (strlencmp(cmd, "enable") == 0) {
2599 bluetooth_set_radiosw(1);
2600 } else if (strlencmp(cmd, "disable") == 0) {
2601 bluetooth_set_radiosw(0);
2602 } else
2603 return -EINVAL;
2606 return 0;
2609 static struct ibm_struct bluetooth_driver_data = {
2610 .name = "bluetooth",
2611 .read = bluetooth_read,
2612 .write = bluetooth_write,
2613 .exit = bluetooth_exit,
2616 /*************************************************************************
2617 * Wan subdriver
2620 enum {
2621 /* ACPI GWAN/SWAN bits */
2622 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2623 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2624 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2627 static int wan_get_radiosw(void);
2628 static int wan_set_radiosw(int radio_on);
2630 /* sysfs wan enable ---------------------------------------------------- */
2631 static ssize_t wan_enable_show(struct device *dev,
2632 struct device_attribute *attr,
2633 char *buf)
2635 int status;
2637 status = wan_get_radiosw();
2638 if (status < 0)
2639 return status;
2641 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2644 static ssize_t wan_enable_store(struct device *dev,
2645 struct device_attribute *attr,
2646 const char *buf, size_t count)
2648 unsigned long t;
2649 int res;
2651 if (parse_strtoul(buf, 1, &t))
2652 return -EINVAL;
2654 res = wan_set_radiosw(t);
2656 return (res) ? res : count;
2659 static struct device_attribute dev_attr_wan_enable =
2660 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2661 wan_enable_show, wan_enable_store);
2663 /* --------------------------------------------------------------------- */
2665 static struct attribute *wan_attributes[] = {
2666 &dev_attr_wan_enable.attr,
2667 NULL
2670 static const struct attribute_group wan_attr_group = {
2671 .attrs = wan_attributes,
2674 static int __init wan_init(struct ibm_init_struct *iibm)
2676 int res;
2677 int status = 0;
2679 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2681 TPACPI_ACPIHANDLE_INIT(hkey);
2683 tp_features.wan = hkey_handle &&
2684 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2686 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2687 str_supported(tp_features.wan),
2688 status);
2690 if (tp_features.wan) {
2691 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2692 /* no wan hardware present in system */
2693 tp_features.wan = 0;
2694 dbg_printk(TPACPI_DBG_INIT,
2695 "wan hardware not installed\n");
2696 } else {
2697 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2698 &wan_attr_group);
2699 if (res)
2700 return res;
2704 return (tp_features.wan)? 0 : 1;
2707 static void wan_exit(void)
2709 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2710 &wan_attr_group);
2713 static int wan_get_radiosw(void)
2715 int status;
2717 if (!tp_features.wan)
2718 return -ENODEV;
2720 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2721 return -EIO;
2723 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2726 static int wan_set_radiosw(int radio_on)
2728 int status;
2730 if (!tp_features.wan)
2731 return -ENODEV;
2733 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2734 return -EIO;
2735 if (radio_on)
2736 status |= TP_ACPI_WANCARD_RADIOSSW;
2737 else
2738 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2739 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2740 return -EIO;
2742 return 0;
2745 /* procfs -------------------------------------------------------------- */
2746 static int wan_read(char *p)
2748 int len = 0;
2749 int status = wan_get_radiosw();
2751 if (!tp_features.wan)
2752 len += sprintf(p + len, "status:\t\tnot supported\n");
2753 else {
2754 len += sprintf(p + len, "status:\t\t%s\n",
2755 (status)? "enabled" : "disabled");
2756 len += sprintf(p + len, "commands:\tenable, disable\n");
2759 return len;
2762 static int wan_write(char *buf)
2764 char *cmd;
2766 if (!tp_features.wan)
2767 return -ENODEV;
2769 while ((cmd = next_cmd(&buf))) {
2770 if (strlencmp(cmd, "enable") == 0) {
2771 wan_set_radiosw(1);
2772 } else if (strlencmp(cmd, "disable") == 0) {
2773 wan_set_radiosw(0);
2774 } else
2775 return -EINVAL;
2778 return 0;
2781 static struct ibm_struct wan_driver_data = {
2782 .name = "wan",
2783 .read = wan_read,
2784 .write = wan_write,
2785 .exit = wan_exit,
2786 .flags.experimental = 1,
2789 /*************************************************************************
2790 * Video subdriver
2793 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2795 enum video_access_mode {
2796 TPACPI_VIDEO_NONE = 0,
2797 TPACPI_VIDEO_570, /* 570 */
2798 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2799 TPACPI_VIDEO_NEW, /* all others */
2802 enum { /* video status flags, based on VIDEO_570 */
2803 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2804 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2805 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2808 enum { /* TPACPI_VIDEO_570 constants */
2809 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2810 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2811 * video_status_flags */
2812 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2813 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2816 static enum video_access_mode video_supported;
2817 static int video_orig_autosw;
2819 static int video_autosw_get(void);
2820 static int video_autosw_set(int enable);
2822 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2824 static int __init video_init(struct ibm_init_struct *iibm)
2826 int ivga;
2828 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2830 TPACPI_ACPIHANDLE_INIT(vid);
2831 TPACPI_ACPIHANDLE_INIT(vid2);
2833 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2834 /* G41, assume IVGA doesn't change */
2835 vid_handle = vid2_handle;
2837 if (!vid_handle)
2838 /* video switching not supported on R30, R31 */
2839 video_supported = TPACPI_VIDEO_NONE;
2840 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2841 /* 570 */
2842 video_supported = TPACPI_VIDEO_570;
2843 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2844 /* 600e/x, 770e, 770x */
2845 video_supported = TPACPI_VIDEO_770;
2846 else
2847 /* all others */
2848 video_supported = TPACPI_VIDEO_NEW;
2850 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2851 str_supported(video_supported != TPACPI_VIDEO_NONE),
2852 video_supported);
2854 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2857 static void video_exit(void)
2859 dbg_printk(TPACPI_DBG_EXIT,
2860 "restoring original video autoswitch mode\n");
2861 if (video_autosw_set(video_orig_autosw))
2862 printk(TPACPI_ERR "error while trying to restore original "
2863 "video autoswitch mode\n");
2866 static int video_outputsw_get(void)
2868 int status = 0;
2869 int i;
2871 switch (video_supported) {
2872 case TPACPI_VIDEO_570:
2873 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2874 TP_ACPI_VIDEO_570_PHSCMD))
2875 return -EIO;
2876 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2877 break;
2878 case TPACPI_VIDEO_770:
2879 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2880 return -EIO;
2881 if (i)
2882 status |= TP_ACPI_VIDEO_S_LCD;
2883 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2884 return -EIO;
2885 if (i)
2886 status |= TP_ACPI_VIDEO_S_CRT;
2887 break;
2888 case TPACPI_VIDEO_NEW:
2889 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2890 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2891 return -EIO;
2892 if (i)
2893 status |= TP_ACPI_VIDEO_S_CRT;
2895 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2896 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2897 return -EIO;
2898 if (i)
2899 status |= TP_ACPI_VIDEO_S_LCD;
2900 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2901 return -EIO;
2902 if (i)
2903 status |= TP_ACPI_VIDEO_S_DVI;
2904 break;
2905 default:
2906 return -ENOSYS;
2909 return status;
2912 static int video_outputsw_set(int status)
2914 int autosw;
2915 int res = 0;
2917 switch (video_supported) {
2918 case TPACPI_VIDEO_570:
2919 res = acpi_evalf(NULL, NULL,
2920 "\\_SB.PHS2", "vdd",
2921 TP_ACPI_VIDEO_570_PHS2CMD,
2922 status | TP_ACPI_VIDEO_570_PHS2SET);
2923 break;
2924 case TPACPI_VIDEO_770:
2925 autosw = video_autosw_get();
2926 if (autosw < 0)
2927 return autosw;
2929 res = video_autosw_set(1);
2930 if (res)
2931 return res;
2932 res = acpi_evalf(vid_handle, NULL,
2933 "ASWT", "vdd", status * 0x100, 0);
2934 if (!autosw && video_autosw_set(autosw)) {
2935 printk(TPACPI_ERR
2936 "video auto-switch left enabled due to error\n");
2937 return -EIO;
2939 break;
2940 case TPACPI_VIDEO_NEW:
2941 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2942 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2943 break;
2944 default:
2945 return -ENOSYS;
2948 return (res)? 0 : -EIO;
2951 static int video_autosw_get(void)
2953 int autosw = 0;
2955 switch (video_supported) {
2956 case TPACPI_VIDEO_570:
2957 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2958 return -EIO;
2959 break;
2960 case TPACPI_VIDEO_770:
2961 case TPACPI_VIDEO_NEW:
2962 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2963 return -EIO;
2964 break;
2965 default:
2966 return -ENOSYS;
2969 return autosw & 1;
2972 static int video_autosw_set(int enable)
2974 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2975 return -EIO;
2976 return 0;
2979 static int video_outputsw_cycle(void)
2981 int autosw = video_autosw_get();
2982 int res;
2984 if (autosw < 0)
2985 return autosw;
2987 switch (video_supported) {
2988 case TPACPI_VIDEO_570:
2989 res = video_autosw_set(1);
2990 if (res)
2991 return res;
2992 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2993 break;
2994 case TPACPI_VIDEO_770:
2995 case TPACPI_VIDEO_NEW:
2996 res = video_autosw_set(1);
2997 if (res)
2998 return res;
2999 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3000 break;
3001 default:
3002 return -ENOSYS;
3004 if (!autosw && video_autosw_set(autosw)) {
3005 printk(TPACPI_ERR
3006 "video auto-switch left enabled due to error\n");
3007 return -EIO;
3010 return (res)? 0 : -EIO;
3013 static int video_expand_toggle(void)
3015 switch (video_supported) {
3016 case TPACPI_VIDEO_570:
3017 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3018 0 : -EIO;
3019 case TPACPI_VIDEO_770:
3020 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3021 0 : -EIO;
3022 case TPACPI_VIDEO_NEW:
3023 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3024 0 : -EIO;
3025 default:
3026 return -ENOSYS;
3028 /* not reached */
3031 static int video_read(char *p)
3033 int status, autosw;
3034 int len = 0;
3036 if (video_supported == TPACPI_VIDEO_NONE) {
3037 len += sprintf(p + len, "status:\t\tnot supported\n");
3038 return len;
3041 status = video_outputsw_get();
3042 if (status < 0)
3043 return status;
3045 autosw = video_autosw_get();
3046 if (autosw < 0)
3047 return autosw;
3049 len += sprintf(p + len, "status:\t\tsupported\n");
3050 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3051 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3052 if (video_supported == TPACPI_VIDEO_NEW)
3053 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3054 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3055 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3056 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3057 if (video_supported == TPACPI_VIDEO_NEW)
3058 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3059 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3060 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3062 return len;
3065 static int video_write(char *buf)
3067 char *cmd;
3068 int enable, disable, status;
3069 int res;
3071 if (video_supported == TPACPI_VIDEO_NONE)
3072 return -ENODEV;
3074 enable = 0;
3075 disable = 0;
3077 while ((cmd = next_cmd(&buf))) {
3078 if (strlencmp(cmd, "lcd_enable") == 0) {
3079 enable |= TP_ACPI_VIDEO_S_LCD;
3080 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3081 disable |= TP_ACPI_VIDEO_S_LCD;
3082 } else if (strlencmp(cmd, "crt_enable") == 0) {
3083 enable |= TP_ACPI_VIDEO_S_CRT;
3084 } else if (strlencmp(cmd, "crt_disable") == 0) {
3085 disable |= TP_ACPI_VIDEO_S_CRT;
3086 } else if (video_supported == TPACPI_VIDEO_NEW &&
3087 strlencmp(cmd, "dvi_enable") == 0) {
3088 enable |= TP_ACPI_VIDEO_S_DVI;
3089 } else if (video_supported == TPACPI_VIDEO_NEW &&
3090 strlencmp(cmd, "dvi_disable") == 0) {
3091 disable |= TP_ACPI_VIDEO_S_DVI;
3092 } else if (strlencmp(cmd, "auto_enable") == 0) {
3093 res = video_autosw_set(1);
3094 if (res)
3095 return res;
3096 } else if (strlencmp(cmd, "auto_disable") == 0) {
3097 res = video_autosw_set(0);
3098 if (res)
3099 return res;
3100 } else if (strlencmp(cmd, "video_switch") == 0) {
3101 res = video_outputsw_cycle();
3102 if (res)
3103 return res;
3104 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3105 res = video_expand_toggle();
3106 if (res)
3107 return res;
3108 } else
3109 return -EINVAL;
3112 if (enable || disable) {
3113 status = video_outputsw_get();
3114 if (status < 0)
3115 return status;
3116 res = video_outputsw_set((status & ~disable) | enable);
3117 if (res)
3118 return res;
3121 return 0;
3124 static struct ibm_struct video_driver_data = {
3125 .name = "video",
3126 .read = video_read,
3127 .write = video_write,
3128 .exit = video_exit,
3131 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3133 /*************************************************************************
3134 * Light (thinklight) subdriver
3137 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3138 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3140 static int __init light_init(struct ibm_init_struct *iibm)
3142 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3144 TPACPI_ACPIHANDLE_INIT(ledb);
3145 TPACPI_ACPIHANDLE_INIT(lght);
3146 TPACPI_ACPIHANDLE_INIT(cmos);
3148 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3149 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3151 if (tp_features.light)
3152 /* light status not supported on
3153 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3154 tp_features.light_status =
3155 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3157 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3158 str_supported(tp_features.light));
3160 return (tp_features.light)? 0 : 1;
3163 static int light_read(char *p)
3165 int len = 0;
3166 int status = 0;
3168 if (!tp_features.light) {
3169 len += sprintf(p + len, "status:\t\tnot supported\n");
3170 } else if (!tp_features.light_status) {
3171 len += sprintf(p + len, "status:\t\tunknown\n");
3172 len += sprintf(p + len, "commands:\ton, off\n");
3173 } else {
3174 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3175 return -EIO;
3176 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3177 len += sprintf(p + len, "commands:\ton, off\n");
3180 return len;
3183 static int light_write(char *buf)
3185 int cmos_cmd, lght_cmd;
3186 char *cmd;
3187 int success;
3189 if (!tp_features.light)
3190 return -ENODEV;
3192 while ((cmd = next_cmd(&buf))) {
3193 if (strlencmp(cmd, "on") == 0) {
3194 cmos_cmd = 0x0c;
3195 lght_cmd = 1;
3196 } else if (strlencmp(cmd, "off") == 0) {
3197 cmos_cmd = 0x0d;
3198 lght_cmd = 0;
3199 } else
3200 return -EINVAL;
3202 success = cmos_handle ?
3203 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3204 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3205 if (!success)
3206 return -EIO;
3209 return 0;
3212 static struct ibm_struct light_driver_data = {
3213 .name = "light",
3214 .read = light_read,
3215 .write = light_write,
3218 /*************************************************************************
3219 * Dock subdriver
3222 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3224 static void dock_notify(struct ibm_struct *ibm, u32 event);
3225 static int dock_read(char *p);
3226 static int dock_write(char *buf);
3228 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3229 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3230 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3231 "\\_SB.PCI.ISA.SLCE", /* 570 */
3232 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3234 /* don't list other alternatives as we install a notify handler on the 570 */
3235 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3237 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3239 .notify = dock_notify,
3240 .handle = &dock_handle,
3241 .type = ACPI_SYSTEM_NOTIFY,
3244 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3245 * We just use it to get notifications of dock hotplug
3246 * in very old thinkpads */
3247 .hid = PCI_ROOT_HID_STRING,
3248 .notify = dock_notify,
3249 .handle = &pci_handle,
3250 .type = ACPI_SYSTEM_NOTIFY,
3254 static struct ibm_struct dock_driver_data[2] = {
3256 .name = "dock",
3257 .read = dock_read,
3258 .write = dock_write,
3259 .acpi = &ibm_dock_acpidriver[0],
3262 .name = "dock",
3263 .acpi = &ibm_dock_acpidriver[1],
3267 #define dock_docked() (_sta(dock_handle) & 1)
3269 static int __init dock_init(struct ibm_init_struct *iibm)
3271 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3273 TPACPI_ACPIHANDLE_INIT(dock);
3275 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3276 str_supported(dock_handle != NULL));
3278 return (dock_handle)? 0 : 1;
3281 static int __init dock_init2(struct ibm_init_struct *iibm)
3283 int dock2_needed;
3285 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3287 if (dock_driver_data[0].flags.acpi_driver_registered &&
3288 dock_driver_data[0].flags.acpi_notify_installed) {
3289 TPACPI_ACPIHANDLE_INIT(pci);
3290 dock2_needed = (pci_handle != NULL);
3291 vdbg_printk(TPACPI_DBG_INIT,
3292 "dock PCI handler for the TP 570 is %s\n",
3293 str_supported(dock2_needed));
3294 } else {
3295 vdbg_printk(TPACPI_DBG_INIT,
3296 "dock subdriver part 2 not required\n");
3297 dock2_needed = 0;
3300 return (dock2_needed)? 0 : 1;
3303 static void dock_notify(struct ibm_struct *ibm, u32 event)
3305 int docked = dock_docked();
3306 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
3308 if (event == 1 && !pci) /* 570 */
3309 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3310 else if (event == 1 && pci) /* 570 */
3311 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3312 else if (event == 3 && docked)
3313 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3314 else if (event == 3 && !docked)
3315 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
3316 else if (event == 0 && docked)
3317 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3318 else {
3319 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3320 event, _sta(dock_handle));
3321 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
3325 static int dock_read(char *p)
3327 int len = 0;
3328 int docked = dock_docked();
3330 if (!dock_handle)
3331 len += sprintf(p + len, "status:\t\tnot supported\n");
3332 else if (!docked)
3333 len += sprintf(p + len, "status:\t\tundocked\n");
3334 else {
3335 len += sprintf(p + len, "status:\t\tdocked\n");
3336 len += sprintf(p + len, "commands:\tdock, undock\n");
3339 return len;
3342 static int dock_write(char *buf)
3344 char *cmd;
3346 if (!dock_docked())
3347 return -ENODEV;
3349 while ((cmd = next_cmd(&buf))) {
3350 if (strlencmp(cmd, "undock") == 0) {
3351 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3352 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3353 return -EIO;
3354 } else if (strlencmp(cmd, "dock") == 0) {
3355 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3356 return -EIO;
3357 } else
3358 return -EINVAL;
3361 return 0;
3364 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3366 /*************************************************************************
3367 * Bay subdriver
3370 #ifdef CONFIG_THINKPAD_ACPI_BAY
3372 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3373 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3374 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3375 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3376 ); /* A21e, R30, R31 */
3377 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3378 "_EJ0", /* all others */
3379 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3380 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3381 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3382 ); /* all others */
3383 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3384 "_EJ0", /* 770x */
3385 ); /* all others */
3387 static int __init bay_init(struct ibm_init_struct *iibm)
3389 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3391 TPACPI_ACPIHANDLE_INIT(bay);
3392 if (bay_handle)
3393 TPACPI_ACPIHANDLE_INIT(bay_ej);
3394 TPACPI_ACPIHANDLE_INIT(bay2);
3395 if (bay2_handle)
3396 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3398 tp_features.bay_status = bay_handle &&
3399 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3400 tp_features.bay_status2 = bay2_handle &&
3401 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3403 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3404 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3405 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3406 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3408 vdbg_printk(TPACPI_DBG_INIT,
3409 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3410 str_supported(tp_features.bay_status),
3411 str_supported(tp_features.bay_eject),
3412 str_supported(tp_features.bay_status2),
3413 str_supported(tp_features.bay_eject2));
3415 return (tp_features.bay_status || tp_features.bay_eject ||
3416 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3419 static void bay_notify(struct ibm_struct *ibm, u32 event)
3421 acpi_bus_generate_event(ibm->acpi->device, event, 0);
3424 #define bay_occupied(b) (_sta(b##_handle) & 1)
3426 static int bay_read(char *p)
3428 int len = 0;
3429 int occupied = bay_occupied(bay);
3430 int occupied2 = bay_occupied(bay2);
3431 int eject, eject2;
3433 len += sprintf(p + len, "status:\t\t%s\n",
3434 tp_features.bay_status ?
3435 (occupied ? "occupied" : "unoccupied") :
3436 "not supported");
3437 if (tp_features.bay_status2)
3438 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3439 "occupied" : "unoccupied");
3441 eject = tp_features.bay_eject && occupied;
3442 eject2 = tp_features.bay_eject2 && occupied2;
3444 if (eject && eject2)
3445 len += sprintf(p + len, "commands:\teject, eject2\n");
3446 else if (eject)
3447 len += sprintf(p + len, "commands:\teject\n");
3448 else if (eject2)
3449 len += sprintf(p + len, "commands:\teject2\n");
3451 return len;
3454 static int bay_write(char *buf)
3456 char *cmd;
3458 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3459 return -ENODEV;
3461 while ((cmd = next_cmd(&buf))) {
3462 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3463 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3464 return -EIO;
3465 } else if (tp_features.bay_eject2 &&
3466 strlencmp(cmd, "eject2") == 0) {
3467 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3468 return -EIO;
3469 } else
3470 return -EINVAL;
3473 return 0;
3476 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3477 .notify = bay_notify,
3478 .handle = &bay_handle,
3479 .type = ACPI_SYSTEM_NOTIFY,
3482 static struct ibm_struct bay_driver_data = {
3483 .name = "bay",
3484 .read = bay_read,
3485 .write = bay_write,
3486 .acpi = &ibm_bay_acpidriver,
3489 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3491 /*************************************************************************
3492 * CMOS subdriver
3495 /* sysfs cmos_command -------------------------------------------------- */
3496 static ssize_t cmos_command_store(struct device *dev,
3497 struct device_attribute *attr,
3498 const char *buf, size_t count)
3500 unsigned long cmos_cmd;
3501 int res;
3503 if (parse_strtoul(buf, 21, &cmos_cmd))
3504 return -EINVAL;
3506 res = issue_thinkpad_cmos_command(cmos_cmd);
3507 return (res)? res : count;
3510 static struct device_attribute dev_attr_cmos_command =
3511 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3513 /* --------------------------------------------------------------------- */
3515 static int __init cmos_init(struct ibm_init_struct *iibm)
3517 int res;
3519 vdbg_printk(TPACPI_DBG_INIT,
3520 "initializing cmos commands subdriver\n");
3522 TPACPI_ACPIHANDLE_INIT(cmos);
3524 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3525 str_supported(cmos_handle != NULL));
3527 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3528 if (res)
3529 return res;
3531 return (cmos_handle)? 0 : 1;
3534 static void cmos_exit(void)
3536 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3539 static int cmos_read(char *p)
3541 int len = 0;
3543 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3544 R30, R31, T20-22, X20-21 */
3545 if (!cmos_handle)
3546 len += sprintf(p + len, "status:\t\tnot supported\n");
3547 else {
3548 len += sprintf(p + len, "status:\t\tsupported\n");
3549 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3552 return len;
3555 static int cmos_write(char *buf)
3557 char *cmd;
3558 int cmos_cmd, res;
3560 while ((cmd = next_cmd(&buf))) {
3561 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3562 cmos_cmd >= 0 && cmos_cmd <= 21) {
3563 /* cmos_cmd set */
3564 } else
3565 return -EINVAL;
3567 res = issue_thinkpad_cmos_command(cmos_cmd);
3568 if (res)
3569 return res;
3572 return 0;
3575 static struct ibm_struct cmos_driver_data = {
3576 .name = "cmos",
3577 .read = cmos_read,
3578 .write = cmos_write,
3579 .exit = cmos_exit,
3582 /*************************************************************************
3583 * LED subdriver
3586 enum led_access_mode {
3587 TPACPI_LED_NONE = 0,
3588 TPACPI_LED_570, /* 570 */
3589 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3590 TPACPI_LED_NEW, /* all others */
3593 enum { /* For TPACPI_LED_OLD */
3594 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3595 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3596 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3599 static enum led_access_mode led_supported;
3601 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3602 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3603 /* T20-22, X20-21 */
3604 "LED", /* all others */
3605 ); /* R30, R31 */
3607 static int __init led_init(struct ibm_init_struct *iibm)
3609 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3611 TPACPI_ACPIHANDLE_INIT(led);
3613 if (!led_handle)
3614 /* led not supported on R30, R31 */
3615 led_supported = TPACPI_LED_NONE;
3616 else if (strlencmp(led_path, "SLED") == 0)
3617 /* 570 */
3618 led_supported = TPACPI_LED_570;
3619 else if (strlencmp(led_path, "SYSL") == 0)
3620 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3621 led_supported = TPACPI_LED_OLD;
3622 else
3623 /* all others */
3624 led_supported = TPACPI_LED_NEW;
3626 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3627 str_supported(led_supported), led_supported);
3629 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3632 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3634 static int led_read(char *p)
3636 int len = 0;
3638 if (!led_supported) {
3639 len += sprintf(p + len, "status:\t\tnot supported\n");
3640 return len;
3642 len += sprintf(p + len, "status:\t\tsupported\n");
3644 if (led_supported == TPACPI_LED_570) {
3645 /* 570 */
3646 int i, status;
3647 for (i = 0; i < 8; i++) {
3648 if (!acpi_evalf(ec_handle,
3649 &status, "GLED", "dd", 1 << i))
3650 return -EIO;
3651 len += sprintf(p + len, "%d:\t\t%s\n",
3652 i, led_status(status));
3656 len += sprintf(p + len, "commands:\t"
3657 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3659 return len;
3662 /* off, on, blink */
3663 static const int led_sled_arg1[] = { 0, 1, 3 };
3664 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3665 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3666 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3668 static int led_write(char *buf)
3670 char *cmd;
3671 int led, ind, ret;
3673 if (!led_supported)
3674 return -ENODEV;
3676 while ((cmd = next_cmd(&buf))) {
3677 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3678 return -EINVAL;
3680 if (strstr(cmd, "off")) {
3681 ind = 0;
3682 } else if (strstr(cmd, "on")) {
3683 ind = 1;
3684 } else if (strstr(cmd, "blink")) {
3685 ind = 2;
3686 } else
3687 return -EINVAL;
3689 if (led_supported == TPACPI_LED_570) {
3690 /* 570 */
3691 led = 1 << led;
3692 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3693 led, led_sled_arg1[ind]))
3694 return -EIO;
3695 } else if (led_supported == TPACPI_LED_OLD) {
3696 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3697 led = 1 << led;
3698 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3699 if (ret >= 0)
3700 ret = ec_write(TPACPI_LED_EC_HLBL,
3701 led * led_exp_hlbl[ind]);
3702 if (ret >= 0)
3703 ret = ec_write(TPACPI_LED_EC_HLCL,
3704 led * led_exp_hlcl[ind]);
3705 if (ret < 0)
3706 return ret;
3707 } else {
3708 /* all others */
3709 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3710 led, led_led_arg1[ind]))
3711 return -EIO;
3715 return 0;
3718 static struct ibm_struct led_driver_data = {
3719 .name = "led",
3720 .read = led_read,
3721 .write = led_write,
3724 /*************************************************************************
3725 * Beep subdriver
3728 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3730 static int __init beep_init(struct ibm_init_struct *iibm)
3732 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3734 TPACPI_ACPIHANDLE_INIT(beep);
3736 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3737 str_supported(beep_handle != NULL));
3739 return (beep_handle)? 0 : 1;
3742 static int beep_read(char *p)
3744 int len = 0;
3746 if (!beep_handle)
3747 len += sprintf(p + len, "status:\t\tnot supported\n");
3748 else {
3749 len += sprintf(p + len, "status:\t\tsupported\n");
3750 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3753 return len;
3756 static int beep_write(char *buf)
3758 char *cmd;
3759 int beep_cmd;
3761 if (!beep_handle)
3762 return -ENODEV;
3764 while ((cmd = next_cmd(&buf))) {
3765 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3766 beep_cmd >= 0 && beep_cmd <= 17) {
3767 /* beep_cmd set */
3768 } else
3769 return -EINVAL;
3770 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3771 return -EIO;
3774 return 0;
3777 static struct ibm_struct beep_driver_data = {
3778 .name = "beep",
3779 .read = beep_read,
3780 .write = beep_write,
3783 /*************************************************************************
3784 * Thermal subdriver
3787 enum thermal_access_mode {
3788 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3789 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3790 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3791 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3792 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3795 enum { /* TPACPI_THERMAL_TPEC_* */
3796 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3797 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3798 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3801 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3802 struct ibm_thermal_sensors_struct {
3803 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3806 static enum thermal_access_mode thermal_read_mode;
3808 /* idx is zero-based */
3809 static int thermal_get_sensor(int idx, s32 *value)
3811 int t;
3812 s8 tmp;
3813 char tmpi[5];
3815 t = TP_EC_THERMAL_TMP0;
3817 switch (thermal_read_mode) {
3818 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3819 case TPACPI_THERMAL_TPEC_16:
3820 if (idx >= 8 && idx <= 15) {
3821 t = TP_EC_THERMAL_TMP8;
3822 idx -= 8;
3824 /* fallthrough */
3825 #endif
3826 case TPACPI_THERMAL_TPEC_8:
3827 if (idx <= 7) {
3828 if (!acpi_ec_read(t + idx, &tmp))
3829 return -EIO;
3830 *value = tmp * 1000;
3831 return 0;
3833 break;
3835 case TPACPI_THERMAL_ACPI_UPDT:
3836 if (idx <= 7) {
3837 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3838 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3839 return -EIO;
3840 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3841 return -EIO;
3842 *value = (t - 2732) * 100;
3843 return 0;
3845 break;
3847 case TPACPI_THERMAL_ACPI_TMP07:
3848 if (idx <= 7) {
3849 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3850 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3851 return -EIO;
3852 if (t > 127 || t < -127)
3853 t = TP_EC_THERMAL_TMP_NA;
3854 *value = t * 1000;
3855 return 0;
3857 break;
3859 case TPACPI_THERMAL_NONE:
3860 default:
3861 return -ENOSYS;
3864 return -EINVAL;
3867 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3869 int res, i;
3870 int n;
3872 n = 8;
3873 i = 0;
3875 if (!s)
3876 return -EINVAL;
3878 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3879 n = 16;
3881 for (i = 0 ; i < n; i++) {
3882 res = thermal_get_sensor(i, &s->temp[i]);
3883 if (res)
3884 return res;
3887 return n;
3890 /* sysfs temp##_input -------------------------------------------------- */
3892 static ssize_t thermal_temp_input_show(struct device *dev,
3893 struct device_attribute *attr,
3894 char *buf)
3896 struct sensor_device_attribute *sensor_attr =
3897 to_sensor_dev_attr(attr);
3898 int idx = sensor_attr->index;
3899 s32 value;
3900 int res;
3902 res = thermal_get_sensor(idx, &value);
3903 if (res)
3904 return res;
3905 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3906 return -ENXIO;
3908 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3911 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3912 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3913 thermal_temp_input_show, NULL, _idxB)
3915 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3916 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3917 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3918 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3919 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3920 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3921 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3922 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3923 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3924 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3925 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3926 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3927 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3928 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3929 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3930 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3931 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3934 #define THERMAL_ATTRS(X) \
3935 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3937 static struct attribute *thermal_temp_input_attr[] = {
3938 THERMAL_ATTRS(8),
3939 THERMAL_ATTRS(9),
3940 THERMAL_ATTRS(10),
3941 THERMAL_ATTRS(11),
3942 THERMAL_ATTRS(12),
3943 THERMAL_ATTRS(13),
3944 THERMAL_ATTRS(14),
3945 THERMAL_ATTRS(15),
3946 THERMAL_ATTRS(0),
3947 THERMAL_ATTRS(1),
3948 THERMAL_ATTRS(2),
3949 THERMAL_ATTRS(3),
3950 THERMAL_ATTRS(4),
3951 THERMAL_ATTRS(5),
3952 THERMAL_ATTRS(6),
3953 THERMAL_ATTRS(7),
3954 NULL
3957 static const struct attribute_group thermal_temp_input16_group = {
3958 .attrs = thermal_temp_input_attr
3961 static const struct attribute_group thermal_temp_input8_group = {
3962 .attrs = &thermal_temp_input_attr[8]
3965 #undef THERMAL_SENSOR_ATTR_TEMP
3966 #undef THERMAL_ATTRS
3968 /* --------------------------------------------------------------------- */
3970 static int __init thermal_init(struct ibm_init_struct *iibm)
3972 u8 t, ta1, ta2;
3973 int i;
3974 int acpi_tmp7;
3975 int res;
3977 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3979 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3981 if (thinkpad_id.ec_model) {
3983 * Direct EC access mode: sensors at registers
3984 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3985 * non-implemented, thermal sensors return 0x80 when
3986 * not available
3989 ta1 = ta2 = 0;
3990 for (i = 0; i < 8; i++) {
3991 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3992 ta1 |= t;
3993 } else {
3994 ta1 = 0;
3995 break;
3997 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3998 ta2 |= t;
3999 } else {
4000 ta1 = 0;
4001 break;
4004 if (ta1 == 0) {
4005 /* This is sheer paranoia, but we handle it anyway */
4006 if (acpi_tmp7) {
4007 printk(TPACPI_ERR
4008 "ThinkPad ACPI EC access misbehaving, "
4009 "falling back to ACPI TMPx access "
4010 "mode\n");
4011 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4012 } else {
4013 printk(TPACPI_ERR
4014 "ThinkPad ACPI EC access misbehaving, "
4015 "disabling thermal sensors access\n");
4016 thermal_read_mode = TPACPI_THERMAL_NONE;
4018 } else {
4019 thermal_read_mode =
4020 (ta2 != 0) ?
4021 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4023 } else if (acpi_tmp7) {
4024 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4025 /* 600e/x, 770e, 770x */
4026 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4027 } else {
4028 /* Standard ACPI TMPx access, max 8 sensors */
4029 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4031 } else {
4032 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4033 thermal_read_mode = TPACPI_THERMAL_NONE;
4036 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4037 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4038 thermal_read_mode);
4040 switch (thermal_read_mode) {
4041 case TPACPI_THERMAL_TPEC_16:
4042 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4043 &thermal_temp_input16_group);
4044 if (res)
4045 return res;
4046 break;
4047 case TPACPI_THERMAL_TPEC_8:
4048 case TPACPI_THERMAL_ACPI_TMP07:
4049 case TPACPI_THERMAL_ACPI_UPDT:
4050 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4051 &thermal_temp_input8_group);
4052 if (res)
4053 return res;
4054 break;
4055 case TPACPI_THERMAL_NONE:
4056 default:
4057 return 1;
4060 return 0;
4063 static void thermal_exit(void)
4065 switch (thermal_read_mode) {
4066 case TPACPI_THERMAL_TPEC_16:
4067 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4068 &thermal_temp_input16_group);
4069 break;
4070 case TPACPI_THERMAL_TPEC_8:
4071 case TPACPI_THERMAL_ACPI_TMP07:
4072 case TPACPI_THERMAL_ACPI_UPDT:
4073 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4074 &thermal_temp_input16_group);
4075 break;
4076 case TPACPI_THERMAL_NONE:
4077 default:
4078 break;
4082 static int thermal_read(char *p)
4084 int len = 0;
4085 int n, i;
4086 struct ibm_thermal_sensors_struct t;
4088 n = thermal_get_sensors(&t);
4089 if (unlikely(n < 0))
4090 return n;
4092 len += sprintf(p + len, "temperatures:\t");
4094 if (n > 0) {
4095 for (i = 0; i < (n - 1); i++)
4096 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4097 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4098 } else
4099 len += sprintf(p + len, "not supported\n");
4101 return len;
4104 static struct ibm_struct thermal_driver_data = {
4105 .name = "thermal",
4106 .read = thermal_read,
4107 .exit = thermal_exit,
4110 /*************************************************************************
4111 * EC Dump subdriver
4114 static u8 ecdump_regs[256];
4116 static int ecdump_read(char *p)
4118 int len = 0;
4119 int i, j;
4120 u8 v;
4122 len += sprintf(p + len, "EC "
4123 " +00 +01 +02 +03 +04 +05 +06 +07"
4124 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4125 for (i = 0; i < 256; i += 16) {
4126 len += sprintf(p + len, "EC 0x%02x:", i);
4127 for (j = 0; j < 16; j++) {
4128 if (!acpi_ec_read(i + j, &v))
4129 break;
4130 if (v != ecdump_regs[i + j])
4131 len += sprintf(p + len, " *%02x", v);
4132 else
4133 len += sprintf(p + len, " %02x", v);
4134 ecdump_regs[i + j] = v;
4136 len += sprintf(p + len, "\n");
4137 if (j != 16)
4138 break;
4141 /* These are way too dangerous to advertise openly... */
4142 #if 0
4143 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4144 " (<offset> is 00-ff, <value> is 00-ff)\n");
4145 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4146 " (<offset> is 00-ff, <value> is 0-255)\n");
4147 #endif
4148 return len;
4151 static int ecdump_write(char *buf)
4153 char *cmd;
4154 int i, v;
4156 while ((cmd = next_cmd(&buf))) {
4157 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4158 /* i and v set */
4159 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4160 /* i and v set */
4161 } else
4162 return -EINVAL;
4163 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4164 if (!acpi_ec_write(i, v))
4165 return -EIO;
4166 } else
4167 return -EINVAL;
4170 return 0;
4173 static struct ibm_struct ecdump_driver_data = {
4174 .name = "ecdump",
4175 .read = ecdump_read,
4176 .write = ecdump_write,
4177 .flags.experimental = 1,
4180 /*************************************************************************
4181 * Backlight/brightness subdriver
4184 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4186 static struct backlight_device *ibm_backlight_device;
4187 static int brightness_offset = 0x31;
4188 static int brightness_mode;
4189 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4191 static struct mutex brightness_mutex;
4194 * ThinkPads can read brightness from two places: EC 0x31, or
4195 * CMOS NVRAM byte 0x5E, bits 0-3.
4197 static int brightness_get(struct backlight_device *bd)
4199 u8 lec = 0, lcmos = 0, level = 0;
4201 if (brightness_mode & 1) {
4202 if (!acpi_ec_read(brightness_offset, &lec))
4203 return -EIO;
4204 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4205 level = lec;
4207 if (brightness_mode & 2) {
4208 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4209 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4210 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4211 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4212 level = lcmos;
4215 if (brightness_mode == 3 && lec != lcmos) {
4216 printk(TPACPI_ERR
4217 "CMOS NVRAM (%u) and EC (%u) do not agree "
4218 "on display brightness level\n",
4219 (unsigned int) lcmos,
4220 (unsigned int) lec);
4221 return -EIO;
4224 return level;
4227 /* May return EINTR which can always be mapped to ERESTARTSYS */
4228 static int brightness_set(int value)
4230 int cmos_cmd, inc, i, res;
4231 int current_value;
4233 if (value > ((tp_features.bright_16levels)? 15 : 7))
4234 return -EINVAL;
4236 res = mutex_lock_interruptible(&brightness_mutex);
4237 if (res < 0)
4238 return res;
4240 current_value = brightness_get(NULL);
4241 if (current_value < 0) {
4242 res = current_value;
4243 goto errout;
4246 cmos_cmd = value > current_value ?
4247 TP_CMOS_BRIGHTNESS_UP :
4248 TP_CMOS_BRIGHTNESS_DOWN;
4249 inc = (value > current_value)? 1 : -1;
4251 res = 0;
4252 for (i = current_value; i != value; i += inc) {
4253 if ((brightness_mode & 2) &&
4254 issue_thinkpad_cmos_command(cmos_cmd)) {
4255 res = -EIO;
4256 goto errout;
4258 if ((brightness_mode & 1) &&
4259 !acpi_ec_write(brightness_offset, i + inc)) {
4260 res = -EIO;
4261 goto errout;;
4265 errout:
4266 mutex_unlock(&brightness_mutex);
4267 return res;
4270 /* sysfs backlight class ----------------------------------------------- */
4272 static int brightness_update_status(struct backlight_device *bd)
4274 /* it is the backlight class's job (caller) to handle
4275 * EINTR and other errors properly */
4276 return brightness_set(
4277 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4278 bd->props.power == FB_BLANK_UNBLANK) ?
4279 bd->props.brightness : 0);
4282 static struct backlight_ops ibm_backlight_data = {
4283 .get_brightness = brightness_get,
4284 .update_status = brightness_update_status,
4287 /* --------------------------------------------------------------------- */
4289 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
4291 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4292 union acpi_object *obj;
4293 int rc;
4295 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4296 obj = (union acpi_object *)buffer.pointer;
4297 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4298 printk(TPACPI_ERR "Unknown _BCL data, "
4299 "please report this to %s\n", TPACPI_MAIL);
4300 rc = 0;
4301 } else {
4302 rc = obj->package.count;
4304 } else {
4305 return 0;
4308 kfree(buffer.pointer);
4309 return rc;
4312 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4313 void *context, void **rv)
4315 char name[ACPI_PATH_SEGMENT_LENGTH];
4316 struct acpi_buffer buffer = { sizeof(name), &name };
4318 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4319 !strncmp("_BCL", name, sizeof(name) - 1)) {
4320 BUG_ON(!rv || !*rv);
4321 **(int **)rv = tpacpi_query_bcl_levels(handle);
4322 return AE_CTRL_TERMINATE;
4323 } else {
4324 return AE_OK;
4329 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
4331 static int __init brightness_check_std_acpi_support(void)
4333 int status;
4334 int bcl_levels = 0;
4335 void *bcl_ptr = &bcl_levels;
4337 if (!vid_handle) {
4338 TPACPI_ACPIHANDLE_INIT(vid);
4340 if (!vid_handle)
4341 return 0;
4344 * Search for a _BCL method, and execute it. This is safe on all
4345 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
4346 * BIOS in ACPI backlight control mode. We do NOT have to care
4347 * about calling the _BCL method in an enabled video device, any
4348 * will do for our purposes.
4351 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4352 brightness_find_bcl, NULL, &bcl_ptr);
4354 if (ACPI_SUCCESS(status) && bcl_levels > 2)
4355 return (bcl_levels - 2);
4357 return 0;
4360 static int __init brightness_init(struct ibm_init_struct *iibm)
4362 int b;
4364 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4366 mutex_init(&brightness_mutex);
4369 * We always attempt to detect acpi support, so as to switch
4370 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4371 * going to publish a backlight interface
4373 b = brightness_check_std_acpi_support();
4374 if (b > 0) {
4375 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4376 printk(TPACPI_NOTICE
4377 "Lenovo BIOS switched to ACPI backlight "
4378 "control mode\n");
4380 if (brightness_enable > 1) {
4381 printk(TPACPI_NOTICE
4382 "standard ACPI backlight interface "
4383 "available, not loading native one...\n");
4384 return 1;
4388 if (!brightness_enable) {
4389 dbg_printk(TPACPI_DBG_INIT,
4390 "brightness support disabled by "
4391 "module parameter\n");
4392 return 1;
4395 if (b > 16) {
4396 printk(TPACPI_ERR
4397 "Unsupported brightness interface, "
4398 "please contact %s\n", TPACPI_MAIL);
4399 return 1;
4401 if (b == 16)
4402 tp_features.bright_16levels = 1;
4404 if (!brightness_mode) {
4405 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4406 brightness_mode = 2;
4407 else
4408 brightness_mode = 3;
4410 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4411 brightness_mode);
4414 if (brightness_mode > 3)
4415 return -EINVAL;
4417 b = brightness_get(NULL);
4418 if (b < 0)
4419 return 1;
4421 if (tp_features.bright_16levels)
4422 printk(TPACPI_INFO
4423 "detected a 16-level brightness capable ThinkPad\n");
4425 ibm_backlight_device = backlight_device_register(
4426 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4427 &ibm_backlight_data);
4428 if (IS_ERR(ibm_backlight_device)) {
4429 printk(TPACPI_ERR "Could not register backlight device\n");
4430 return PTR_ERR(ibm_backlight_device);
4432 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4434 ibm_backlight_device->props.max_brightness =
4435 (tp_features.bright_16levels)? 15 : 7;
4436 ibm_backlight_device->props.brightness = b;
4437 backlight_update_status(ibm_backlight_device);
4439 return 0;
4442 static void brightness_exit(void)
4444 if (ibm_backlight_device) {
4445 vdbg_printk(TPACPI_DBG_EXIT,
4446 "calling backlight_device_unregister()\n");
4447 backlight_device_unregister(ibm_backlight_device);
4448 ibm_backlight_device = NULL;
4452 static int brightness_read(char *p)
4454 int len = 0;
4455 int level;
4457 level = brightness_get(NULL);
4458 if (level < 0) {
4459 len += sprintf(p + len, "level:\t\tunreadable\n");
4460 } else {
4461 len += sprintf(p + len, "level:\t\t%d\n", level);
4462 len += sprintf(p + len, "commands:\tup, down\n");
4463 len += sprintf(p + len, "commands:\tlevel <level>"
4464 " (<level> is 0-%d)\n",
4465 (tp_features.bright_16levels) ? 15 : 7);
4468 return len;
4471 static int brightness_write(char *buf)
4473 int level;
4474 int rc;
4475 char *cmd;
4476 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4478 level = brightness_get(NULL);
4479 if (level < 0)
4480 return level;
4482 while ((cmd = next_cmd(&buf))) {
4483 if (strlencmp(cmd, "up") == 0) {
4484 if (level < max_level)
4485 level++;
4486 } else if (strlencmp(cmd, "down") == 0) {
4487 if (level > 0)
4488 level--;
4489 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4490 level >= 0 && level <= max_level) {
4491 /* new level set */
4492 } else
4493 return -EINVAL;
4497 * Now we know what the final level should be, so we try to set it.
4498 * Doing it this way makes the syscall restartable in case of EINTR
4500 rc = brightness_set(level);
4501 return (rc == -EINTR)? ERESTARTSYS : rc;
4504 static struct ibm_struct brightness_driver_data = {
4505 .name = "brightness",
4506 .read = brightness_read,
4507 .write = brightness_write,
4508 .exit = brightness_exit,
4511 /*************************************************************************
4512 * Volume subdriver
4515 static int volume_offset = 0x30;
4517 static int volume_read(char *p)
4519 int len = 0;
4520 u8 level;
4522 if (!acpi_ec_read(volume_offset, &level)) {
4523 len += sprintf(p + len, "level:\t\tunreadable\n");
4524 } else {
4525 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4526 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4527 len += sprintf(p + len, "commands:\tup, down, mute\n");
4528 len += sprintf(p + len, "commands:\tlevel <level>"
4529 " (<level> is 0-15)\n");
4532 return len;
4535 static int volume_write(char *buf)
4537 int cmos_cmd, inc, i;
4538 u8 level, mute;
4539 int new_level, new_mute;
4540 char *cmd;
4542 while ((cmd = next_cmd(&buf))) {
4543 if (!acpi_ec_read(volume_offset, &level))
4544 return -EIO;
4545 new_mute = mute = level & 0x40;
4546 new_level = level = level & 0xf;
4548 if (strlencmp(cmd, "up") == 0) {
4549 if (mute)
4550 new_mute = 0;
4551 else
4552 new_level = level == 15 ? 15 : level + 1;
4553 } else if (strlencmp(cmd, "down") == 0) {
4554 if (mute)
4555 new_mute = 0;
4556 else
4557 new_level = level == 0 ? 0 : level - 1;
4558 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4559 new_level >= 0 && new_level <= 15) {
4560 /* new_level set */
4561 } else if (strlencmp(cmd, "mute") == 0) {
4562 new_mute = 0x40;
4563 } else
4564 return -EINVAL;
4566 if (new_level != level) {
4567 /* mute doesn't change */
4569 cmos_cmd = (new_level > level) ?
4570 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4571 inc = new_level > level ? 1 : -1;
4573 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4574 !acpi_ec_write(volume_offset, level)))
4575 return -EIO;
4577 for (i = level; i != new_level; i += inc)
4578 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4579 !acpi_ec_write(volume_offset, i + inc))
4580 return -EIO;
4582 if (mute &&
4583 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4584 !acpi_ec_write(volume_offset, new_level + mute))) {
4585 return -EIO;
4589 if (new_mute != mute) {
4590 /* level doesn't change */
4592 cmos_cmd = (new_mute) ?
4593 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4595 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4596 !acpi_ec_write(volume_offset, level + new_mute))
4597 return -EIO;
4601 return 0;
4604 static struct ibm_struct volume_driver_data = {
4605 .name = "volume",
4606 .read = volume_read,
4607 .write = volume_write,
4610 /*************************************************************************
4611 * Fan subdriver
4615 * FAN ACCESS MODES
4617 * TPACPI_FAN_RD_ACPI_GFAN:
4618 * ACPI GFAN method: returns fan level
4620 * see TPACPI_FAN_WR_ACPI_SFAN
4621 * EC 0x2f (HFSP) not available if GFAN exists
4623 * TPACPI_FAN_WR_ACPI_SFAN:
4624 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4626 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4627 * it for writing.
4629 * TPACPI_FAN_WR_TPEC:
4630 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4631 * Supported on almost all ThinkPads
4633 * Fan speed changes of any sort (including those caused by the
4634 * disengaged mode) are usually done slowly by the firmware as the
4635 * maximum ammount of fan duty cycle change per second seems to be
4636 * limited.
4638 * Reading is not available if GFAN exists.
4639 * Writing is not available if SFAN exists.
4641 * Bits
4642 * 7 automatic mode engaged;
4643 * (default operation mode of the ThinkPad)
4644 * fan level is ignored in this mode.
4645 * 6 full speed mode (takes precedence over bit 7);
4646 * not available on all thinkpads. May disable
4647 * the tachometer while the fan controller ramps up
4648 * the speed (which can take up to a few *minutes*).
4649 * Speeds up fan to 100% duty-cycle, which is far above
4650 * the standard RPM levels. It is not impossible that
4651 * it could cause hardware damage.
4652 * 5-3 unused in some models. Extra bits for fan level
4653 * in others, but still useless as all values above
4654 * 7 map to the same speed as level 7 in these models.
4655 * 2-0 fan level (0..7 usually)
4656 * 0x00 = stop
4657 * 0x07 = max (set when temperatures critical)
4658 * Some ThinkPads may have other levels, see
4659 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4661 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4662 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4663 * does so, its initial value is meaningless (0x07).
4665 * For firmware bugs, refer to:
4666 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4668 * ----
4670 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4671 * Main fan tachometer reading (in RPM)
4673 * This register is present on all ThinkPads with a new-style EC, and
4674 * it is known not to be present on the A21m/e, and T22, as there is
4675 * something else in offset 0x84 according to the ACPI DSDT. Other
4676 * ThinkPads from this same time period (and earlier) probably lack the
4677 * tachometer as well.
4679 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4680 * was never fixed by IBM to report the EC firmware version string
4681 * probably support the tachometer (like the early X models), so
4682 * detecting it is quite hard. We need more data to know for sure.
4684 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4685 * might result.
4687 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4688 * mode.
4690 * For firmware bugs, refer to:
4691 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4693 * TPACPI_FAN_WR_ACPI_FANS:
4694 * ThinkPad X31, X40, X41. Not available in the X60.
4696 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4697 * high speed. ACPI DSDT seems to map these three speeds to levels
4698 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4699 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4701 * The speeds are stored on handles
4702 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4704 * There are three default speed sets, acessible as handles:
4705 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4707 * ACPI DSDT switches which set is in use depending on various
4708 * factors.
4710 * TPACPI_FAN_WR_TPEC is also available and should be used to
4711 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4712 * but the ACPI tables just mention level 7.
4715 enum { /* Fan control constants */
4716 fan_status_offset = 0x2f, /* EC register 0x2f */
4717 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4718 * 0x84 must be read before 0x85 */
4720 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4721 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4723 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4726 enum fan_status_access_mode {
4727 TPACPI_FAN_NONE = 0, /* No fan status or control */
4728 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4729 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4732 enum fan_control_access_mode {
4733 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4734 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4735 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4736 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4739 enum fan_control_commands {
4740 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4741 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4742 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4743 * and also watchdog cmd */
4746 static int fan_control_allowed;
4748 static enum fan_status_access_mode fan_status_access_mode;
4749 static enum fan_control_access_mode fan_control_access_mode;
4750 static enum fan_control_commands fan_control_commands;
4752 static u8 fan_control_initial_status;
4753 static u8 fan_control_desired_level;
4754 static int fan_watchdog_maxinterval;
4756 static struct mutex fan_mutex;
4758 static void fan_watchdog_fire(struct work_struct *ignored);
4759 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4761 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4762 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4763 "\\FSPD", /* 600e/x, 770e, 770x */
4764 ); /* all others */
4765 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4766 "JFNS", /* 770x-JL */
4767 ); /* all others */
4770 * Call with fan_mutex held
4772 static void fan_update_desired_level(u8 status)
4774 if ((status &
4775 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4776 if (status > 7)
4777 fan_control_desired_level = 7;
4778 else
4779 fan_control_desired_level = status;
4783 static int fan_get_status(u8 *status)
4785 u8 s;
4787 /* TODO:
4788 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4790 switch (fan_status_access_mode) {
4791 case TPACPI_FAN_RD_ACPI_GFAN:
4792 /* 570, 600e/x, 770e, 770x */
4794 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4795 return -EIO;
4797 if (likely(status))
4798 *status = s & 0x07;
4800 break;
4802 case TPACPI_FAN_RD_TPEC:
4803 /* all except 570, 600e/x, 770e, 770x */
4804 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4805 return -EIO;
4807 if (likely(status))
4808 *status = s;
4810 break;
4812 default:
4813 return -ENXIO;
4816 return 0;
4819 static int fan_get_status_safe(u8 *status)
4821 int rc;
4822 u8 s;
4824 if (mutex_lock_interruptible(&fan_mutex))
4825 return -ERESTARTSYS;
4826 rc = fan_get_status(&s);
4827 if (!rc)
4828 fan_update_desired_level(s);
4829 mutex_unlock(&fan_mutex);
4831 if (status)
4832 *status = s;
4834 return rc;
4837 static int fan_get_speed(unsigned int *speed)
4839 u8 hi, lo;
4841 switch (fan_status_access_mode) {
4842 case TPACPI_FAN_RD_TPEC:
4843 /* all except 570, 600e/x, 770e, 770x */
4844 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4845 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4846 return -EIO;
4848 if (likely(speed))
4849 *speed = (hi << 8) | lo;
4851 break;
4853 default:
4854 return -ENXIO;
4857 return 0;
4860 static int fan_set_level(int level)
4862 if (!fan_control_allowed)
4863 return -EPERM;
4865 switch (fan_control_access_mode) {
4866 case TPACPI_FAN_WR_ACPI_SFAN:
4867 if (level >= 0 && level <= 7) {
4868 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4869 return -EIO;
4870 } else
4871 return -EINVAL;
4872 break;
4874 case TPACPI_FAN_WR_ACPI_FANS:
4875 case TPACPI_FAN_WR_TPEC:
4876 if ((level != TP_EC_FAN_AUTO) &&
4877 (level != TP_EC_FAN_FULLSPEED) &&
4878 ((level < 0) || (level > 7)))
4879 return -EINVAL;
4881 /* safety net should the EC not support AUTO
4882 * or FULLSPEED mode bits and just ignore them */
4883 if (level & TP_EC_FAN_FULLSPEED)
4884 level |= 7; /* safety min speed 7 */
4885 else if (level & TP_EC_FAN_AUTO)
4886 level |= 4; /* safety min speed 4 */
4888 if (!acpi_ec_write(fan_status_offset, level))
4889 return -EIO;
4890 else
4891 tp_features.fan_ctrl_status_undef = 0;
4892 break;
4894 default:
4895 return -ENXIO;
4897 return 0;
4900 static int fan_set_level_safe(int level)
4902 int rc;
4904 if (!fan_control_allowed)
4905 return -EPERM;
4907 if (mutex_lock_interruptible(&fan_mutex))
4908 return -ERESTARTSYS;
4910 if (level == TPACPI_FAN_LAST_LEVEL)
4911 level = fan_control_desired_level;
4913 rc = fan_set_level(level);
4914 if (!rc)
4915 fan_update_desired_level(level);
4917 mutex_unlock(&fan_mutex);
4918 return rc;
4921 static int fan_set_enable(void)
4923 u8 s;
4924 int rc;
4926 if (!fan_control_allowed)
4927 return -EPERM;
4929 if (mutex_lock_interruptible(&fan_mutex))
4930 return -ERESTARTSYS;
4932 switch (fan_control_access_mode) {
4933 case TPACPI_FAN_WR_ACPI_FANS:
4934 case TPACPI_FAN_WR_TPEC:
4935 rc = fan_get_status(&s);
4936 if (rc < 0)
4937 break;
4939 /* Don't go out of emergency fan mode */
4940 if (s != 7) {
4941 s &= 0x07;
4942 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4945 if (!acpi_ec_write(fan_status_offset, s))
4946 rc = -EIO;
4947 else {
4948 tp_features.fan_ctrl_status_undef = 0;
4949 rc = 0;
4951 break;
4953 case TPACPI_FAN_WR_ACPI_SFAN:
4954 rc = fan_get_status(&s);
4955 if (rc < 0)
4956 break;
4958 s &= 0x07;
4960 /* Set fan to at least level 4 */
4961 s |= 4;
4963 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4964 rc = -EIO;
4965 else
4966 rc = 0;
4967 break;
4969 default:
4970 rc = -ENXIO;
4973 mutex_unlock(&fan_mutex);
4974 return rc;
4977 static int fan_set_disable(void)
4979 int rc;
4981 if (!fan_control_allowed)
4982 return -EPERM;
4984 if (mutex_lock_interruptible(&fan_mutex))
4985 return -ERESTARTSYS;
4987 rc = 0;
4988 switch (fan_control_access_mode) {
4989 case TPACPI_FAN_WR_ACPI_FANS:
4990 case TPACPI_FAN_WR_TPEC:
4991 if (!acpi_ec_write(fan_status_offset, 0x00))
4992 rc = -EIO;
4993 else {
4994 fan_control_desired_level = 0;
4995 tp_features.fan_ctrl_status_undef = 0;
4997 break;
4999 case TPACPI_FAN_WR_ACPI_SFAN:
5000 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5001 rc = -EIO;
5002 else
5003 fan_control_desired_level = 0;
5004 break;
5006 default:
5007 rc = -ENXIO;
5011 mutex_unlock(&fan_mutex);
5012 return rc;
5015 static int fan_set_speed(int speed)
5017 int rc;
5019 if (!fan_control_allowed)
5020 return -EPERM;
5022 if (mutex_lock_interruptible(&fan_mutex))
5023 return -ERESTARTSYS;
5025 rc = 0;
5026 switch (fan_control_access_mode) {
5027 case TPACPI_FAN_WR_ACPI_FANS:
5028 if (speed >= 0 && speed <= 65535) {
5029 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5030 speed, speed, speed))
5031 rc = -EIO;
5032 } else
5033 rc = -EINVAL;
5034 break;
5036 default:
5037 rc = -ENXIO;
5040 mutex_unlock(&fan_mutex);
5041 return rc;
5044 static void fan_watchdog_reset(void)
5046 static int fan_watchdog_active;
5048 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5049 return;
5051 if (fan_watchdog_active)
5052 cancel_delayed_work(&fan_watchdog_task);
5054 if (fan_watchdog_maxinterval > 0 &&
5055 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5056 fan_watchdog_active = 1;
5057 if (!schedule_delayed_work(&fan_watchdog_task,
5058 msecs_to_jiffies(fan_watchdog_maxinterval
5059 * 1000))) {
5060 printk(TPACPI_ERR
5061 "failed to schedule the fan watchdog, "
5062 "watchdog will not trigger\n");
5064 } else
5065 fan_watchdog_active = 0;
5068 static void fan_watchdog_fire(struct work_struct *ignored)
5070 int rc;
5072 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5073 return;
5075 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5076 rc = fan_set_enable();
5077 if (rc < 0) {
5078 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5079 "will try again later...\n", -rc);
5080 /* reschedule for later */
5081 fan_watchdog_reset();
5086 * SYSFS fan layout: hwmon compatible (device)
5088 * pwm*_enable:
5089 * 0: "disengaged" mode
5090 * 1: manual mode
5091 * 2: native EC "auto" mode (recommended, hardware default)
5093 * pwm*: set speed in manual mode, ignored otherwise.
5094 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5095 * interpolation.
5097 * fan*_input: tachometer reading, RPM
5100 * SYSFS fan layout: extensions
5102 * fan_watchdog (driver):
5103 * fan watchdog interval in seconds, 0 disables (default), max 120
5106 /* sysfs fan pwm1_enable ----------------------------------------------- */
5107 static ssize_t fan_pwm1_enable_show(struct device *dev,
5108 struct device_attribute *attr,
5109 char *buf)
5111 int res, mode;
5112 u8 status;
5114 res = fan_get_status_safe(&status);
5115 if (res)
5116 return res;
5118 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5119 if (status != fan_control_initial_status) {
5120 tp_features.fan_ctrl_status_undef = 0;
5121 } else {
5122 /* Return most likely status. In fact, it
5123 * might be the only possible status */
5124 status = TP_EC_FAN_AUTO;
5128 if (status & TP_EC_FAN_FULLSPEED) {
5129 mode = 0;
5130 } else if (status & TP_EC_FAN_AUTO) {
5131 mode = 2;
5132 } else
5133 mode = 1;
5135 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5138 static ssize_t fan_pwm1_enable_store(struct device *dev,
5139 struct device_attribute *attr,
5140 const char *buf, size_t count)
5142 unsigned long t;
5143 int res, level;
5145 if (parse_strtoul(buf, 2, &t))
5146 return -EINVAL;
5148 switch (t) {
5149 case 0:
5150 level = TP_EC_FAN_FULLSPEED;
5151 break;
5152 case 1:
5153 level = TPACPI_FAN_LAST_LEVEL;
5154 break;
5155 case 2:
5156 level = TP_EC_FAN_AUTO;
5157 break;
5158 case 3:
5159 /* reserved for software-controlled auto mode */
5160 return -ENOSYS;
5161 default:
5162 return -EINVAL;
5165 res = fan_set_level_safe(level);
5166 if (res == -ENXIO)
5167 return -EINVAL;
5168 else if (res < 0)
5169 return res;
5171 fan_watchdog_reset();
5173 return count;
5176 static struct device_attribute dev_attr_fan_pwm1_enable =
5177 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5178 fan_pwm1_enable_show, fan_pwm1_enable_store);
5180 /* sysfs fan pwm1 ------------------------------------------------------ */
5181 static ssize_t fan_pwm1_show(struct device *dev,
5182 struct device_attribute *attr,
5183 char *buf)
5185 int res;
5186 u8 status;
5188 res = fan_get_status_safe(&status);
5189 if (res)
5190 return res;
5192 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5193 if (status != fan_control_initial_status) {
5194 tp_features.fan_ctrl_status_undef = 0;
5195 } else {
5196 status = TP_EC_FAN_AUTO;
5200 if ((status &
5201 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5202 status = fan_control_desired_level;
5204 if (status > 7)
5205 status = 7;
5207 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5210 static ssize_t fan_pwm1_store(struct device *dev,
5211 struct device_attribute *attr,
5212 const char *buf, size_t count)
5214 unsigned long s;
5215 int rc;
5216 u8 status, newlevel;
5218 if (parse_strtoul(buf, 255, &s))
5219 return -EINVAL;
5221 /* scale down from 0-255 to 0-7 */
5222 newlevel = (s >> 5) & 0x07;
5224 if (mutex_lock_interruptible(&fan_mutex))
5225 return -ERESTARTSYS;
5227 rc = fan_get_status(&status);
5228 if (!rc && (status &
5229 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5230 rc = fan_set_level(newlevel);
5231 if (rc == -ENXIO)
5232 rc = -EINVAL;
5233 else if (!rc) {
5234 fan_update_desired_level(newlevel);
5235 fan_watchdog_reset();
5239 mutex_unlock(&fan_mutex);
5240 return (rc)? rc : count;
5243 static struct device_attribute dev_attr_fan_pwm1 =
5244 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5245 fan_pwm1_show, fan_pwm1_store);
5247 /* sysfs fan fan1_input ------------------------------------------------ */
5248 static ssize_t fan_fan1_input_show(struct device *dev,
5249 struct device_attribute *attr,
5250 char *buf)
5252 int res;
5253 unsigned int speed;
5255 res = fan_get_speed(&speed);
5256 if (res < 0)
5257 return res;
5259 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5262 static struct device_attribute dev_attr_fan_fan1_input =
5263 __ATTR(fan1_input, S_IRUGO,
5264 fan_fan1_input_show, NULL);
5266 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5267 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5268 char *buf)
5270 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5273 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5274 const char *buf, size_t count)
5276 unsigned long t;
5278 if (parse_strtoul(buf, 120, &t))
5279 return -EINVAL;
5281 if (!fan_control_allowed)
5282 return -EPERM;
5284 fan_watchdog_maxinterval = t;
5285 fan_watchdog_reset();
5287 return count;
5290 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5291 fan_fan_watchdog_show, fan_fan_watchdog_store);
5293 /* --------------------------------------------------------------------- */
5294 static struct attribute *fan_attributes[] = {
5295 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5296 &dev_attr_fan_fan1_input.attr,
5297 NULL
5300 static const struct attribute_group fan_attr_group = {
5301 .attrs = fan_attributes,
5304 static int __init fan_init(struct ibm_init_struct *iibm)
5306 int rc;
5308 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5310 mutex_init(&fan_mutex);
5311 fan_status_access_mode = TPACPI_FAN_NONE;
5312 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5313 fan_control_commands = 0;
5314 fan_watchdog_maxinterval = 0;
5315 tp_features.fan_ctrl_status_undef = 0;
5316 fan_control_desired_level = 7;
5318 TPACPI_ACPIHANDLE_INIT(fans);
5319 TPACPI_ACPIHANDLE_INIT(gfan);
5320 TPACPI_ACPIHANDLE_INIT(sfan);
5322 if (gfan_handle) {
5323 /* 570, 600e/x, 770e, 770x */
5324 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5325 } else {
5326 /* all other ThinkPads: note that even old-style
5327 * ThinkPad ECs supports the fan control register */
5328 if (likely(acpi_ec_read(fan_status_offset,
5329 &fan_control_initial_status))) {
5330 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5332 /* In some ThinkPads, neither the EC nor the ACPI
5333 * DSDT initialize the fan status, and it ends up
5334 * being set to 0x07 when it *could* be either
5335 * 0x07 or 0x80.
5337 * Enable for TP-1Y (T43), TP-78 (R51e),
5338 * TP-76 (R52), TP-70 (T43, R52), which are known
5339 * to be buggy. */
5340 if (fan_control_initial_status == 0x07) {
5341 switch (thinkpad_id.ec_model) {
5342 case 0x5931: /* TP-1Y */
5343 case 0x3837: /* TP-78 */
5344 case 0x3637: /* TP-76 */
5345 case 0x3037: /* TP-70 */
5346 printk(TPACPI_NOTICE
5347 "fan_init: initial fan status "
5348 "is unknown, assuming it is "
5349 "in auto mode\n");
5350 tp_features.fan_ctrl_status_undef = 1;
5354 } else {
5355 printk(TPACPI_ERR
5356 "ThinkPad ACPI EC access misbehaving, "
5357 "fan status and control unavailable\n");
5358 return 1;
5362 if (sfan_handle) {
5363 /* 570, 770x-JL */
5364 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5365 fan_control_commands |=
5366 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5367 } else {
5368 if (!gfan_handle) {
5369 /* gfan without sfan means no fan control */
5370 /* all other models implement TP EC 0x2f control */
5372 if (fans_handle) {
5373 /* X31, X40, X41 */
5374 fan_control_access_mode =
5375 TPACPI_FAN_WR_ACPI_FANS;
5376 fan_control_commands |=
5377 TPACPI_FAN_CMD_SPEED |
5378 TPACPI_FAN_CMD_LEVEL |
5379 TPACPI_FAN_CMD_ENABLE;
5380 } else {
5381 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5382 fan_control_commands |=
5383 TPACPI_FAN_CMD_LEVEL |
5384 TPACPI_FAN_CMD_ENABLE;
5389 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5390 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5391 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5392 fan_status_access_mode, fan_control_access_mode);
5394 /* fan control master switch */
5395 if (!fan_control_allowed) {
5396 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5397 fan_control_commands = 0;
5398 dbg_printk(TPACPI_DBG_INIT,
5399 "fan control features disabled by parameter\n");
5402 /* update fan_control_desired_level */
5403 if (fan_status_access_mode != TPACPI_FAN_NONE)
5404 fan_get_status_safe(NULL);
5406 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5407 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5408 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5409 &fan_attr_group);
5410 if (!(rc < 0))
5411 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5412 &driver_attr_fan_watchdog);
5413 if (rc < 0)
5414 return rc;
5415 return 0;
5416 } else
5417 return 1;
5420 static void fan_exit(void)
5422 vdbg_printk(TPACPI_DBG_EXIT,
5423 "cancelling any pending fan watchdog tasks\n");
5425 /* FIXME: can we really do this unconditionally? */
5426 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5427 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5428 &driver_attr_fan_watchdog);
5430 cancel_delayed_work(&fan_watchdog_task);
5431 flush_scheduled_work();
5434 static int fan_read(char *p)
5436 int len = 0;
5437 int rc;
5438 u8 status;
5439 unsigned int speed = 0;
5441 switch (fan_status_access_mode) {
5442 case TPACPI_FAN_RD_ACPI_GFAN:
5443 /* 570, 600e/x, 770e, 770x */
5444 rc = fan_get_status_safe(&status);
5445 if (rc < 0)
5446 return rc;
5448 len += sprintf(p + len, "status:\t\t%s\n"
5449 "level:\t\t%d\n",
5450 (status != 0) ? "enabled" : "disabled", status);
5451 break;
5453 case TPACPI_FAN_RD_TPEC:
5454 /* all except 570, 600e/x, 770e, 770x */
5455 rc = fan_get_status_safe(&status);
5456 if (rc < 0)
5457 return rc;
5459 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5460 if (status != fan_control_initial_status)
5461 tp_features.fan_ctrl_status_undef = 0;
5462 else
5463 /* Return most likely status. In fact, it
5464 * might be the only possible status */
5465 status = TP_EC_FAN_AUTO;
5468 len += sprintf(p + len, "status:\t\t%s\n",
5469 (status != 0) ? "enabled" : "disabled");
5471 rc = fan_get_speed(&speed);
5472 if (rc < 0)
5473 return rc;
5475 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5477 if (status & TP_EC_FAN_FULLSPEED)
5478 /* Disengaged mode takes precedence */
5479 len += sprintf(p + len, "level:\t\tdisengaged\n");
5480 else if (status & TP_EC_FAN_AUTO)
5481 len += sprintf(p + len, "level:\t\tauto\n");
5482 else
5483 len += sprintf(p + len, "level:\t\t%d\n", status);
5484 break;
5486 case TPACPI_FAN_NONE:
5487 default:
5488 len += sprintf(p + len, "status:\t\tnot supported\n");
5491 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5492 len += sprintf(p + len, "commands:\tlevel <level>");
5494 switch (fan_control_access_mode) {
5495 case TPACPI_FAN_WR_ACPI_SFAN:
5496 len += sprintf(p + len, " (<level> is 0-7)\n");
5497 break;
5499 default:
5500 len += sprintf(p + len, " (<level> is 0-7, "
5501 "auto, disengaged, full-speed)\n");
5502 break;
5506 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5507 len += sprintf(p + len, "commands:\tenable, disable\n"
5508 "commands:\twatchdog <timeout> (<timeout> "
5509 "is 0 (off), 1-120 (seconds))\n");
5511 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5512 len += sprintf(p + len, "commands:\tspeed <speed>"
5513 " (<speed> is 0-65535)\n");
5515 return len;
5518 static int fan_write_cmd_level(const char *cmd, int *rc)
5520 int level;
5522 if (strlencmp(cmd, "level auto") == 0)
5523 level = TP_EC_FAN_AUTO;
5524 else if ((strlencmp(cmd, "level disengaged") == 0) |
5525 (strlencmp(cmd, "level full-speed") == 0))
5526 level = TP_EC_FAN_FULLSPEED;
5527 else if (sscanf(cmd, "level %d", &level) != 1)
5528 return 0;
5530 *rc = fan_set_level_safe(level);
5531 if (*rc == -ENXIO)
5532 printk(TPACPI_ERR "level command accepted for unsupported "
5533 "access mode %d", fan_control_access_mode);
5535 return 1;
5538 static int fan_write_cmd_enable(const char *cmd, int *rc)
5540 if (strlencmp(cmd, "enable") != 0)
5541 return 0;
5543 *rc = fan_set_enable();
5544 if (*rc == -ENXIO)
5545 printk(TPACPI_ERR "enable command accepted for unsupported "
5546 "access mode %d", fan_control_access_mode);
5548 return 1;
5551 static int fan_write_cmd_disable(const char *cmd, int *rc)
5553 if (strlencmp(cmd, "disable") != 0)
5554 return 0;
5556 *rc = fan_set_disable();
5557 if (*rc == -ENXIO)
5558 printk(TPACPI_ERR "disable command accepted for unsupported "
5559 "access mode %d", fan_control_access_mode);
5561 return 1;
5564 static int fan_write_cmd_speed(const char *cmd, int *rc)
5566 int speed;
5568 /* TODO:
5569 * Support speed <low> <medium> <high> ? */
5571 if (sscanf(cmd, "speed %d", &speed) != 1)
5572 return 0;
5574 *rc = fan_set_speed(speed);
5575 if (*rc == -ENXIO)
5576 printk(TPACPI_ERR "speed command accepted for unsupported "
5577 "access mode %d", fan_control_access_mode);
5579 return 1;
5582 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5584 int interval;
5586 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5587 return 0;
5589 if (interval < 0 || interval > 120)
5590 *rc = -EINVAL;
5591 else
5592 fan_watchdog_maxinterval = interval;
5594 return 1;
5597 static int fan_write(char *buf)
5599 char *cmd;
5600 int rc = 0;
5602 while (!rc && (cmd = next_cmd(&buf))) {
5603 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5604 fan_write_cmd_level(cmd, &rc)) &&
5605 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5606 (fan_write_cmd_enable(cmd, &rc) ||
5607 fan_write_cmd_disable(cmd, &rc) ||
5608 fan_write_cmd_watchdog(cmd, &rc))) &&
5609 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5610 fan_write_cmd_speed(cmd, &rc))
5612 rc = -EINVAL;
5613 else if (!rc)
5614 fan_watchdog_reset();
5617 return rc;
5620 static struct ibm_struct fan_driver_data = {
5621 .name = "fan",
5622 .read = fan_read,
5623 .write = fan_write,
5624 .exit = fan_exit,
5627 /****************************************************************************
5628 ****************************************************************************
5630 * Infrastructure
5632 ****************************************************************************
5633 ****************************************************************************/
5635 /* sysfs name ---------------------------------------------------------- */
5636 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5637 struct device_attribute *attr,
5638 char *buf)
5640 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5643 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5644 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5646 /* --------------------------------------------------------------------- */
5648 /* /proc support */
5649 static struct proc_dir_entry *proc_dir;
5652 * Module and infrastructure proble, init and exit handling
5655 static int force_load;
5657 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5658 static const char * __init str_supported(int is_supported)
5660 static char text_unsupported[] __initdata = "not supported";
5662 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5664 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5666 static void ibm_exit(struct ibm_struct *ibm)
5668 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5670 list_del_init(&ibm->all_drivers);
5672 if (ibm->flags.acpi_notify_installed) {
5673 dbg_printk(TPACPI_DBG_EXIT,
5674 "%s: acpi_remove_notify_handler\n", ibm->name);
5675 BUG_ON(!ibm->acpi);
5676 acpi_remove_notify_handler(*ibm->acpi->handle,
5677 ibm->acpi->type,
5678 dispatch_acpi_notify);
5679 ibm->flags.acpi_notify_installed = 0;
5680 ibm->flags.acpi_notify_installed = 0;
5683 if (ibm->flags.proc_created) {
5684 dbg_printk(TPACPI_DBG_EXIT,
5685 "%s: remove_proc_entry\n", ibm->name);
5686 remove_proc_entry(ibm->name, proc_dir);
5687 ibm->flags.proc_created = 0;
5690 if (ibm->flags.acpi_driver_registered) {
5691 dbg_printk(TPACPI_DBG_EXIT,
5692 "%s: acpi_bus_unregister_driver\n", ibm->name);
5693 BUG_ON(!ibm->acpi);
5694 acpi_bus_unregister_driver(ibm->acpi->driver);
5695 kfree(ibm->acpi->driver);
5696 ibm->acpi->driver = NULL;
5697 ibm->flags.acpi_driver_registered = 0;
5700 if (ibm->flags.init_called && ibm->exit) {
5701 ibm->exit();
5702 ibm->flags.init_called = 0;
5705 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5708 static int __init ibm_init(struct ibm_init_struct *iibm)
5710 int ret;
5711 struct ibm_struct *ibm = iibm->data;
5712 struct proc_dir_entry *entry;
5714 BUG_ON(ibm == NULL);
5716 INIT_LIST_HEAD(&ibm->all_drivers);
5718 if (ibm->flags.experimental && !experimental)
5719 return 0;
5721 dbg_printk(TPACPI_DBG_INIT,
5722 "probing for %s\n", ibm->name);
5724 if (iibm->init) {
5725 ret = iibm->init(iibm);
5726 if (ret > 0)
5727 return 0; /* probe failed */
5728 if (ret)
5729 return ret;
5731 ibm->flags.init_called = 1;
5734 if (ibm->acpi) {
5735 if (ibm->acpi->hid) {
5736 ret = register_tpacpi_subdriver(ibm);
5737 if (ret)
5738 goto err_out;
5741 if (ibm->acpi->notify) {
5742 ret = setup_acpi_notify(ibm);
5743 if (ret == -ENODEV) {
5744 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5745 ibm->name);
5746 ret = 0;
5747 goto err_out;
5749 if (ret < 0)
5750 goto err_out;
5754 dbg_printk(TPACPI_DBG_INIT,
5755 "%s installed\n", ibm->name);
5757 if (ibm->read) {
5758 entry = create_proc_entry(ibm->name,
5759 S_IFREG | S_IRUGO | S_IWUSR,
5760 proc_dir);
5761 if (!entry) {
5762 printk(TPACPI_ERR "unable to create proc entry %s\n",
5763 ibm->name);
5764 ret = -ENODEV;
5765 goto err_out;
5767 entry->owner = THIS_MODULE;
5768 entry->data = ibm;
5769 entry->read_proc = &dispatch_procfs_read;
5770 if (ibm->write)
5771 entry->write_proc = &dispatch_procfs_write;
5772 ibm->flags.proc_created = 1;
5775 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5777 return 0;
5779 err_out:
5780 dbg_printk(TPACPI_DBG_INIT,
5781 "%s: at error exit path with result %d\n",
5782 ibm->name, ret);
5784 ibm_exit(ibm);
5785 return (ret < 0)? ret : 0;
5788 /* Probing */
5790 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5792 struct dmi_device *dev = NULL;
5793 char ec_fw_string[18];
5795 if (!tp)
5796 return;
5798 memset(tp, 0, sizeof(*tp));
5800 if (dmi_name_in_vendors("IBM"))
5801 tp->vendor = PCI_VENDOR_ID_IBM;
5802 else if (dmi_name_in_vendors("LENOVO"))
5803 tp->vendor = PCI_VENDOR_ID_LENOVO;
5804 else
5805 return;
5807 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5808 GFP_KERNEL);
5809 if (!tp->bios_version_str)
5810 return;
5811 tp->bios_model = tp->bios_version_str[0]
5812 | (tp->bios_version_str[1] << 8);
5815 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5816 * X32 or newer, all Z series; Some models must have an
5817 * up-to-date BIOS or they will not be detected.
5819 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5821 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5822 if (sscanf(dev->name,
5823 "IBM ThinkPad Embedded Controller -[%17c",
5824 ec_fw_string) == 1) {
5825 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5826 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5828 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5829 tp->ec_model = ec_fw_string[0]
5830 | (ec_fw_string[1] << 8);
5831 break;
5835 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5836 GFP_KERNEL);
5837 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5838 kfree(tp->model_str);
5839 tp->model_str = NULL;
5843 static int __init probe_for_thinkpad(void)
5845 int is_thinkpad;
5847 if (acpi_disabled)
5848 return -ENODEV;
5851 * Non-ancient models have better DMI tagging, but very old models
5852 * don't.
5854 is_thinkpad = (thinkpad_id.model_str != NULL);
5856 /* ec is required because many other handles are relative to it */
5857 TPACPI_ACPIHANDLE_INIT(ec);
5858 if (!ec_handle) {
5859 if (is_thinkpad)
5860 printk(TPACPI_ERR
5861 "Not yet supported ThinkPad detected!\n");
5862 return -ENODEV;
5866 * Risks a regression on very old machines, but reduces potential
5867 * false positives a damn great deal
5869 if (!is_thinkpad)
5870 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5872 if (!is_thinkpad && !force_load)
5873 return -ENODEV;
5875 return 0;
5879 /* Module init, exit, parameters */
5881 static struct ibm_init_struct ibms_init[] __initdata = {
5883 .init = thinkpad_acpi_driver_init,
5884 .data = &thinkpad_acpi_driver_data,
5887 .init = hotkey_init,
5888 .data = &hotkey_driver_data,
5891 .init = bluetooth_init,
5892 .data = &bluetooth_driver_data,
5895 .init = wan_init,
5896 .data = &wan_driver_data,
5898 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5900 .init = video_init,
5901 .data = &video_driver_data,
5903 #endif
5905 .init = light_init,
5906 .data = &light_driver_data,
5908 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5910 .init = dock_init,
5911 .data = &dock_driver_data[0],
5914 .init = dock_init2,
5915 .data = &dock_driver_data[1],
5917 #endif
5918 #ifdef CONFIG_THINKPAD_ACPI_BAY
5920 .init = bay_init,
5921 .data = &bay_driver_data,
5923 #endif
5925 .init = cmos_init,
5926 .data = &cmos_driver_data,
5929 .init = led_init,
5930 .data = &led_driver_data,
5933 .init = beep_init,
5934 .data = &beep_driver_data,
5937 .init = thermal_init,
5938 .data = &thermal_driver_data,
5941 .data = &ecdump_driver_data,
5944 .init = brightness_init,
5945 .data = &brightness_driver_data,
5948 .data = &volume_driver_data,
5951 .init = fan_init,
5952 .data = &fan_driver_data,
5956 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5958 unsigned int i;
5959 struct ibm_struct *ibm;
5961 if (!kp || !kp->name || !val)
5962 return -EINVAL;
5964 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5965 ibm = ibms_init[i].data;
5966 WARN_ON(ibm == NULL);
5968 if (!ibm || !ibm->name)
5969 continue;
5971 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5972 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5973 return -ENOSPC;
5974 strcpy(ibms_init[i].param, val);
5975 strcat(ibms_init[i].param, ",");
5976 return 0;
5980 return -EINVAL;
5983 module_param(experimental, int, 0);
5984 MODULE_PARM_DESC(experimental,
5985 "Enables experimental features when non-zero");
5987 module_param_named(debug, dbg_level, uint, 0);
5988 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5990 module_param(force_load, bool, 0);
5991 MODULE_PARM_DESC(force_load,
5992 "Attempts to load the driver even on a "
5993 "mis-identified ThinkPad when true");
5995 module_param_named(fan_control, fan_control_allowed, bool, 0);
5996 MODULE_PARM_DESC(fan_control,
5997 "Enables setting fan parameters features when true");
5999 module_param_named(brightness_mode, brightness_mode, int, 0);
6000 MODULE_PARM_DESC(brightness_mode,
6001 "Selects brightness control strategy: "
6002 "0=auto, 1=EC, 2=CMOS, 3=both");
6004 module_param(brightness_enable, uint, 0);
6005 MODULE_PARM_DESC(brightness_enable,
6006 "Enables backlight control when 1, disables when 0");
6008 module_param(hotkey_report_mode, uint, 0);
6009 MODULE_PARM_DESC(hotkey_report_mode,
6010 "used for backwards compatibility with userspace, "
6011 "see documentation");
6013 #define TPACPI_PARAM(feature) \
6014 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6015 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
6016 "at module load, see documentation")
6018 TPACPI_PARAM(hotkey);
6019 TPACPI_PARAM(bluetooth);
6020 TPACPI_PARAM(video);
6021 TPACPI_PARAM(light);
6022 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6023 TPACPI_PARAM(dock);
6024 #endif
6025 #ifdef CONFIG_THINKPAD_ACPI_BAY
6026 TPACPI_PARAM(bay);
6027 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6028 TPACPI_PARAM(cmos);
6029 TPACPI_PARAM(led);
6030 TPACPI_PARAM(beep);
6031 TPACPI_PARAM(ecdump);
6032 TPACPI_PARAM(brightness);
6033 TPACPI_PARAM(volume);
6034 TPACPI_PARAM(fan);
6036 static void thinkpad_acpi_module_exit(void)
6038 struct ibm_struct *ibm, *itmp;
6040 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6042 list_for_each_entry_safe_reverse(ibm, itmp,
6043 &tpacpi_all_drivers,
6044 all_drivers) {
6045 ibm_exit(ibm);
6048 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6050 if (tpacpi_inputdev) {
6051 if (tp_features.input_device_registered)
6052 input_unregister_device(tpacpi_inputdev);
6053 else
6054 input_free_device(tpacpi_inputdev);
6057 if (tpacpi_hwmon)
6058 hwmon_device_unregister(tpacpi_hwmon);
6060 if (tp_features.sensors_pdev_attrs_registered)
6061 device_remove_file(&tpacpi_sensors_pdev->dev,
6062 &dev_attr_thinkpad_acpi_pdev_name);
6063 if (tpacpi_sensors_pdev)
6064 platform_device_unregister(tpacpi_sensors_pdev);
6065 if (tpacpi_pdev)
6066 platform_device_unregister(tpacpi_pdev);
6068 if (tp_features.sensors_pdrv_attrs_registered)
6069 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6070 if (tp_features.platform_drv_attrs_registered)
6071 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6073 if (tp_features.sensors_pdrv_registered)
6074 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6076 if (tp_features.platform_drv_registered)
6077 platform_driver_unregister(&tpacpi_pdriver);
6079 if (proc_dir)
6080 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6082 kfree(thinkpad_id.bios_version_str);
6083 kfree(thinkpad_id.ec_version_str);
6084 kfree(thinkpad_id.model_str);
6088 static int __init thinkpad_acpi_module_init(void)
6090 int ret, i;
6092 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6094 /* Parameter checking */
6095 if (hotkey_report_mode > 2)
6096 return -EINVAL;
6098 /* Driver-level probe */
6100 get_thinkpad_model_data(&thinkpad_id);
6101 ret = probe_for_thinkpad();
6102 if (ret) {
6103 thinkpad_acpi_module_exit();
6104 return ret;
6107 /* Driver initialization */
6109 TPACPI_ACPIHANDLE_INIT(ecrd);
6110 TPACPI_ACPIHANDLE_INIT(ecwr);
6112 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6113 if (!proc_dir) {
6114 printk(TPACPI_ERR
6115 "unable to create proc dir " TPACPI_PROC_DIR);
6116 thinkpad_acpi_module_exit();
6117 return -ENODEV;
6119 proc_dir->owner = THIS_MODULE;
6121 ret = platform_driver_register(&tpacpi_pdriver);
6122 if (ret) {
6123 printk(TPACPI_ERR
6124 "unable to register main platform driver\n");
6125 thinkpad_acpi_module_exit();
6126 return ret;
6128 tp_features.platform_drv_registered = 1;
6130 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6131 if (ret) {
6132 printk(TPACPI_ERR
6133 "unable to register hwmon platform driver\n");
6134 thinkpad_acpi_module_exit();
6135 return ret;
6137 tp_features.sensors_pdrv_registered = 1;
6139 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6140 if (!ret) {
6141 tp_features.platform_drv_attrs_registered = 1;
6142 ret = tpacpi_create_driver_attributes(
6143 &tpacpi_hwmon_pdriver.driver);
6145 if (ret) {
6146 printk(TPACPI_ERR
6147 "unable to create sysfs driver attributes\n");
6148 thinkpad_acpi_module_exit();
6149 return ret;
6151 tp_features.sensors_pdrv_attrs_registered = 1;
6154 /* Device initialization */
6155 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6156 NULL, 0);
6157 if (IS_ERR(tpacpi_pdev)) {
6158 ret = PTR_ERR(tpacpi_pdev);
6159 tpacpi_pdev = NULL;
6160 printk(TPACPI_ERR "unable to register platform device\n");
6161 thinkpad_acpi_module_exit();
6162 return ret;
6164 tpacpi_sensors_pdev = platform_device_register_simple(
6165 TPACPI_HWMON_DRVR_NAME,
6166 -1, NULL, 0);
6167 if (IS_ERR(tpacpi_sensors_pdev)) {
6168 ret = PTR_ERR(tpacpi_sensors_pdev);
6169 tpacpi_sensors_pdev = NULL;
6170 printk(TPACPI_ERR
6171 "unable to register hwmon platform device\n");
6172 thinkpad_acpi_module_exit();
6173 return ret;
6175 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6176 &dev_attr_thinkpad_acpi_pdev_name);
6177 if (ret) {
6178 printk(TPACPI_ERR
6179 "unable to create sysfs hwmon device attributes\n");
6180 thinkpad_acpi_module_exit();
6181 return ret;
6183 tp_features.sensors_pdev_attrs_registered = 1;
6184 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6185 if (IS_ERR(tpacpi_hwmon)) {
6186 ret = PTR_ERR(tpacpi_hwmon);
6187 tpacpi_hwmon = NULL;
6188 printk(TPACPI_ERR "unable to register hwmon device\n");
6189 thinkpad_acpi_module_exit();
6190 return ret;
6192 mutex_init(&tpacpi_inputdev_send_mutex);
6193 tpacpi_inputdev = input_allocate_device();
6194 if (!tpacpi_inputdev) {
6195 printk(TPACPI_ERR "unable to allocate input device\n");
6196 thinkpad_acpi_module_exit();
6197 return -ENOMEM;
6198 } else {
6199 /* Prepare input device, but don't register */
6200 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6201 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6202 tpacpi_inputdev->id.bustype = BUS_HOST;
6203 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6204 thinkpad_id.vendor :
6205 PCI_VENDOR_ID_IBM;
6206 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6207 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6209 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6210 ret = ibm_init(&ibms_init[i]);
6211 if (ret >= 0 && *ibms_init[i].param)
6212 ret = ibms_init[i].data->write(ibms_init[i].param);
6213 if (ret < 0) {
6214 thinkpad_acpi_module_exit();
6215 return ret;
6218 ret = input_register_device(tpacpi_inputdev);
6219 if (ret < 0) {
6220 printk(TPACPI_ERR "unable to register input device\n");
6221 thinkpad_acpi_module_exit();
6222 return ret;
6223 } else {
6224 tp_features.input_device_registered = 1;
6227 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6228 return 0;
6231 /* Please remove this in year 2009 */
6232 MODULE_ALIAS("ibm_acpi");
6235 * DMI matching for module autoloading
6237 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6238 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6240 * Only models listed in thinkwiki will be supported, so add yours
6241 * if it is not there yet.
6243 #define IBM_BIOS_MODULE_ALIAS(__type) \
6244 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6246 /* Non-ancient thinkpads */
6247 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6248 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6250 /* Ancient thinkpad BIOSes have to be identified by
6251 * BIOS type or model number, and there are far less
6252 * BIOS types than model numbers... */
6253 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6254 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6255 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6257 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6258 MODULE_DESCRIPTION(TPACPI_DESC);
6259 MODULE_VERSION(TPACPI_VERSION);
6260 MODULE_LICENSE("GPL");
6262 module_init(thinkpad_acpi_module_init);
6263 module_exit(thinkpad_acpi_module_exit);