ACPI: thinkpad-acpi: rename IBM in defines
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
bloba3d3f427734ed2bc087c0a4de0d07baa64d9a3cf
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 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.18"
25 #define TPACPI_SYSFS_VERSION 0x020101
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); } while (0)
154 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
155 #define vdbg_printk(a_dbg_level, format, arg...) \
156 dbg_printk(a_dbg_level, format, ## arg)
157 static const char *str_supported(int is_supported);
158 #else
159 #define vdbg_printk(a_dbg_level, format, arg...)
160 #endif
162 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
163 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
164 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
167 /****************************************************************************
168 * Driver-wide structs and misc. variables
171 struct ibm_struct;
173 struct tp_acpi_drv_struct {
174 const struct acpi_device_id *hid;
175 struct acpi_driver *driver;
177 void (*notify) (struct ibm_struct *, u32);
178 acpi_handle *handle;
179 u32 type;
180 struct acpi_device *device;
183 struct ibm_struct {
184 char *name;
186 int (*read) (char *);
187 int (*write) (char *);
188 void (*exit) (void);
189 void (*resume) (void);
191 struct list_head all_drivers;
193 struct tp_acpi_drv_struct *acpi;
195 struct {
196 u8 acpi_driver_registered:1;
197 u8 acpi_notify_installed:1;
198 u8 proc_created:1;
199 u8 init_called:1;
200 u8 experimental:1;
201 } flags;
204 struct ibm_init_struct {
205 char param[32];
207 int (*init) (struct ibm_init_struct *);
208 struct ibm_struct *data;
211 static struct {
212 #ifdef CONFIG_THINKPAD_ACPI_BAY
213 u32 bay_status:1;
214 u32 bay_eject:1;
215 u32 bay_status2:1;
216 u32 bay_eject2:1;
217 #endif
218 u32 bluetooth:1;
219 u32 hotkey:1;
220 u32 hotkey_mask:1;
221 u32 hotkey_wlsw:1;
222 u32 light:1;
223 u32 light_status:1;
224 u32 bright_16levels:1;
225 u32 wan:1;
226 u32 fan_ctrl_status_undef:1;
227 u32 input_device_registered:1;
228 u32 platform_drv_registered:1;
229 u32 platform_drv_attrs_registered:1;
230 u32 sensors_pdrv_registered:1;
231 u32 sensors_pdrv_attrs_registered:1;
232 u32 sensors_pdev_attrs_registered:1;
233 u32 hotkey_poll_active:1;
234 } tp_features;
236 struct thinkpad_id_data {
237 unsigned int vendor; /* ThinkPad vendor:
238 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
240 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
241 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
243 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
244 u16 ec_model;
246 char *model_str;
248 static struct thinkpad_id_data thinkpad_id;
250 static enum {
251 TPACPI_LIFE_INIT = 0,
252 TPACPI_LIFE_RUNNING,
253 TPACPI_LIFE_EXITING,
254 } tpacpi_lifecycle;
256 static int experimental;
257 static u32 dbg_level;
259 /****************************************************************************
260 ****************************************************************************
262 * ACPI Helpers and device model
264 ****************************************************************************
265 ****************************************************************************/
267 /*************************************************************************
268 * ACPI basic handles
271 static acpi_handle root_handle;
273 #define TPACPI_HANDLE(object, parent, paths...) \
274 static acpi_handle object##_handle; \
275 static acpi_handle *object##_parent = &parent##_handle; \
276 static char *object##_path; \
277 static char *object##_paths[] = { paths }
279 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
280 "\\_SB.PCI.ISA.EC", /* 570 */
281 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
282 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
283 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
284 "\\_SB.PCI0.ICH3.EC0", /* R31 */
285 "\\_SB.PCI0.LPC.EC", /* all others */
288 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
289 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
291 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
292 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
293 "\\CMS", /* R40, R40e */
294 ); /* all others */
296 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
297 "^HKEY", /* R30, R31 */
298 "HKEY", /* all others */
299 ); /* 570 */
302 /*************************************************************************
303 * ACPI helpers
306 static int acpi_evalf(acpi_handle handle,
307 void *res, char *method, char *fmt, ...)
309 char *fmt0 = fmt;
310 struct acpi_object_list params;
311 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
312 struct acpi_buffer result, *resultp;
313 union acpi_object out_obj;
314 acpi_status status;
315 va_list ap;
316 char res_type;
317 int success;
318 int quiet;
320 if (!*fmt) {
321 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
322 return 0;
325 if (*fmt == 'q') {
326 quiet = 1;
327 fmt++;
328 } else
329 quiet = 0;
331 res_type = *(fmt++);
333 params.count = 0;
334 params.pointer = &in_objs[0];
336 va_start(ap, fmt);
337 while (*fmt) {
338 char c = *(fmt++);
339 switch (c) {
340 case 'd': /* int */
341 in_objs[params.count].integer.value = va_arg(ap, int);
342 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
343 break;
344 /* add more types as needed */
345 default:
346 printk(TPACPI_ERR "acpi_evalf() called "
347 "with invalid format character '%c'\n", c);
348 return 0;
351 va_end(ap);
353 if (res_type != 'v') {
354 result.length = sizeof(out_obj);
355 result.pointer = &out_obj;
356 resultp = &result;
357 } else
358 resultp = NULL;
360 status = acpi_evaluate_object(handle, method, &params, resultp);
362 switch (res_type) {
363 case 'd': /* int */
364 if (res)
365 *(int *)res = out_obj.integer.value;
366 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
367 break;
368 case 'v': /* void */
369 success = status == AE_OK;
370 break;
371 /* add more types as needed */
372 default:
373 printk(TPACPI_ERR "acpi_evalf() called "
374 "with invalid format character '%c'\n", res_type);
375 return 0;
378 if (!success && !quiet)
379 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
380 method, fmt0, status);
382 return success;
385 static int acpi_ec_read(int i, u8 * p)
387 int v;
389 if (ecrd_handle) {
390 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
391 return 0;
392 *p = v;
393 } else {
394 if (ec_read(i, p) < 0)
395 return 0;
398 return 1;
401 static int acpi_ec_write(int i, u8 v)
403 if (ecwr_handle) {
404 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
405 return 0;
406 } else {
407 if (ec_write(i, v) < 0)
408 return 0;
411 return 1;
414 static int _sta(acpi_handle handle)
416 int status;
418 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
419 status = 0;
421 return status;
424 static int issue_thinkpad_cmos_command(int cmos_cmd)
426 if (!cmos_handle)
427 return -ENXIO;
429 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
430 return -EIO;
432 return 0;
435 /*************************************************************************
436 * ACPI device model
439 #define TPACPI_ACPIHANDLE_INIT(object) \
440 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
441 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
443 static void drv_acpi_handle_init(char *name,
444 acpi_handle *handle, acpi_handle parent,
445 char **paths, int num_paths, char **path)
447 int i;
448 acpi_status status;
450 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
451 name);
453 for (i = 0; i < num_paths; i++) {
454 status = acpi_get_handle(parent, paths[i], handle);
455 if (ACPI_SUCCESS(status)) {
456 *path = paths[i];
457 dbg_printk(TPACPI_DBG_INIT,
458 "Found ACPI handle %s for %s\n",
459 *path, name);
460 return;
464 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
465 name);
466 *handle = NULL;
469 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
471 struct ibm_struct *ibm = data;
473 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
474 return;
476 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
477 return;
479 ibm->acpi->notify(ibm, event);
482 static int __init setup_acpi_notify(struct ibm_struct *ibm)
484 acpi_status status;
485 int rc;
487 BUG_ON(!ibm->acpi);
489 if (!*ibm->acpi->handle)
490 return 0;
492 vdbg_printk(TPACPI_DBG_INIT,
493 "setting up ACPI notify for %s\n", ibm->name);
495 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
496 if (rc < 0) {
497 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
498 ibm->name, rc);
499 return -ENODEV;
502 acpi_driver_data(ibm->acpi->device) = ibm;
503 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
504 TPACPI_ACPI_EVENT_PREFIX,
505 ibm->name);
507 status = acpi_install_notify_handler(*ibm->acpi->handle,
508 ibm->acpi->type, dispatch_acpi_notify, ibm);
509 if (ACPI_FAILURE(status)) {
510 if (status == AE_ALREADY_EXISTS) {
511 printk(TPACPI_NOTICE "another device driver is already handling %s events\n",
512 ibm->name);
513 } else {
514 printk(TPACPI_ERR "acpi_install_notify_handler(%s) failed: %d\n",
515 ibm->name, status);
517 return -ENODEV;
519 ibm->flags.acpi_notify_installed = 1;
520 return 0;
523 static int __init tpacpi_device_add(struct acpi_device *device)
525 return 0;
528 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
530 int rc;
532 dbg_printk(TPACPI_DBG_INIT,
533 "registering %s as an ACPI driver\n", ibm->name);
535 BUG_ON(!ibm->acpi);
537 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
538 if (!ibm->acpi->driver) {
539 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
540 return -ENOMEM;
543 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
544 ibm->acpi->driver->ids = ibm->acpi->hid;
546 ibm->acpi->driver->ops.add = &tpacpi_device_add;
548 rc = acpi_bus_register_driver(ibm->acpi->driver);
549 if (rc < 0) {
550 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
551 ibm->name, rc);
552 kfree(ibm->acpi->driver);
553 ibm->acpi->driver = NULL;
554 } else if (!rc)
555 ibm->flags.acpi_driver_registered = 1;
557 return rc;
561 /****************************************************************************
562 ****************************************************************************
564 * Procfs Helpers
566 ****************************************************************************
567 ****************************************************************************/
569 static int dispatch_procfs_read(char *page, char **start, off_t off,
570 int count, int *eof, void *data)
572 struct ibm_struct *ibm = data;
573 int len;
575 if (!ibm || !ibm->read)
576 return -EINVAL;
578 len = ibm->read(page);
579 if (len < 0)
580 return len;
582 if (len <= off + count)
583 *eof = 1;
584 *start = page + off;
585 len -= off;
586 if (len > count)
587 len = count;
588 if (len < 0)
589 len = 0;
591 return len;
594 static int dispatch_procfs_write(struct file *file,
595 const char __user * userbuf,
596 unsigned long count, void *data)
598 struct ibm_struct *ibm = data;
599 char *kernbuf;
600 int ret;
602 if (!ibm || !ibm->write)
603 return -EINVAL;
605 kernbuf = kmalloc(count + 2, GFP_KERNEL);
606 if (!kernbuf)
607 return -ENOMEM;
609 if (copy_from_user(kernbuf, userbuf, count)) {
610 kfree(kernbuf);
611 return -EFAULT;
614 kernbuf[count] = 0;
615 strcat(kernbuf, ",");
616 ret = ibm->write(kernbuf);
617 if (ret == 0)
618 ret = count;
620 kfree(kernbuf);
622 return ret;
625 static char *next_cmd(char **cmds)
627 char *start = *cmds;
628 char *end;
630 while ((end = strchr(start, ',')) && end == start)
631 start = end + 1;
633 if (!end)
634 return NULL;
636 *end = 0;
637 *cmds = end + 1;
638 return start;
642 /****************************************************************************
643 ****************************************************************************
645 * Device model: input, hwmon and platform
647 ****************************************************************************
648 ****************************************************************************/
650 static struct platform_device *tpacpi_pdev;
651 static struct platform_device *tpacpi_sensors_pdev;
652 static struct device *tpacpi_hwmon;
653 static struct input_dev *tpacpi_inputdev;
654 static struct mutex tpacpi_inputdev_send_mutex;
655 static LIST_HEAD(tpacpi_all_drivers);
657 static int tpacpi_resume_handler(struct platform_device *pdev)
659 struct ibm_struct *ibm, *itmp;
661 list_for_each_entry_safe(ibm, itmp,
662 &tpacpi_all_drivers,
663 all_drivers) {
664 if (ibm->resume)
665 (ibm->resume)();
668 return 0;
671 static struct platform_driver tpacpi_pdriver = {
672 .driver = {
673 .name = TPACPI_DRVR_NAME,
674 .owner = THIS_MODULE,
676 .resume = tpacpi_resume_handler,
679 static struct platform_driver tpacpi_hwmon_pdriver = {
680 .driver = {
681 .name = TPACPI_HWMON_DRVR_NAME,
682 .owner = THIS_MODULE,
686 /*************************************************************************
687 * sysfs support helpers
690 struct attribute_set {
691 unsigned int members, max_members;
692 struct attribute_group group;
695 struct attribute_set_obj {
696 struct attribute_set s;
697 struct attribute *a;
698 } __attribute__((packed));
700 static struct attribute_set *create_attr_set(unsigned int max_members,
701 const char* name)
703 struct attribute_set_obj *sobj;
705 if (max_members == 0)
706 return NULL;
708 /* Allocates space for implicit NULL at the end too */
709 sobj = kzalloc(sizeof(struct attribute_set_obj) +
710 max_members * sizeof(struct attribute *),
711 GFP_KERNEL);
712 if (!sobj)
713 return NULL;
714 sobj->s.max_members = max_members;
715 sobj->s.group.attrs = &sobj->a;
716 sobj->s.group.name = name;
718 return &sobj->s;
721 #define destroy_attr_set(_set) \
722 kfree(_set);
724 /* not multi-threaded safe, use it in a single thread per set */
725 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
727 if (!s || !attr)
728 return -EINVAL;
730 if (s->members >= s->max_members)
731 return -ENOMEM;
733 s->group.attrs[s->members] = attr;
734 s->members++;
736 return 0;
739 static int add_many_to_attr_set(struct attribute_set* s,
740 struct attribute **attr,
741 unsigned int count)
743 int i, res;
745 for (i = 0; i < count; i++) {
746 res = add_to_attr_set(s, attr[i]);
747 if (res)
748 return res;
751 return 0;
754 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
756 sysfs_remove_group(kobj, &s->group);
757 destroy_attr_set(s);
760 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
761 sysfs_create_group(_kobj, &_attr_set->group)
763 static int parse_strtoul(const char *buf,
764 unsigned long max, unsigned long *value)
766 char *endp;
768 while (*buf && isspace(*buf))
769 buf++;
770 *value = simple_strtoul(buf, &endp, 0);
771 while (*endp && isspace(*endp))
772 endp++;
773 if (*endp || *value > max)
774 return -EINVAL;
776 return 0;
779 /*************************************************************************
780 * thinkpad-acpi driver attributes
783 /* interface_version --------------------------------------------------- */
784 static ssize_t tpacpi_driver_interface_version_show(
785 struct device_driver *drv,
786 char *buf)
788 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
791 static DRIVER_ATTR(interface_version, S_IRUGO,
792 tpacpi_driver_interface_version_show, NULL);
794 /* debug_level --------------------------------------------------------- */
795 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
796 char *buf)
798 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
801 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
802 const char *buf, size_t count)
804 unsigned long t;
806 if (parse_strtoul(buf, 0xffff, &t))
807 return -EINVAL;
809 dbg_level = t;
811 return count;
814 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
815 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
817 /* version ------------------------------------------------------------- */
818 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
819 char *buf)
821 return snprintf(buf, PAGE_SIZE, "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
824 static DRIVER_ATTR(version, S_IRUGO,
825 tpacpi_driver_version_show, NULL);
827 /* --------------------------------------------------------------------- */
829 static struct driver_attribute* tpacpi_driver_attributes[] = {
830 &driver_attr_debug_level, &driver_attr_version,
831 &driver_attr_interface_version,
834 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
836 int i, res;
838 i = 0;
839 res = 0;
840 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
841 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
842 i++;
845 return res;
848 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
850 int i;
852 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
853 driver_remove_file(drv, tpacpi_driver_attributes[i]);
856 /****************************************************************************
857 ****************************************************************************
859 * Subdrivers
861 ****************************************************************************
862 ****************************************************************************/
864 /*************************************************************************
865 * thinkpad-acpi init subdriver
868 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
870 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
871 printk(TPACPI_INFO "%s\n", TPACPI_URL);
873 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
874 (thinkpad_id.bios_version_str) ?
875 thinkpad_id.bios_version_str : "unknown",
876 (thinkpad_id.ec_version_str) ?
877 thinkpad_id.ec_version_str : "unknown");
879 if (thinkpad_id.vendor && thinkpad_id.model_str)
880 printk(TPACPI_INFO "%s %s\n",
881 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
882 "IBM" : ((thinkpad_id.vendor ==
883 PCI_VENDOR_ID_LENOVO) ?
884 "Lenovo" : "Unknown vendor"),
885 thinkpad_id.model_str);
887 return 0;
890 static int thinkpad_acpi_driver_read(char *p)
892 int len = 0;
894 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
895 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
897 return len;
900 static struct ibm_struct thinkpad_acpi_driver_data = {
901 .name = "driver",
902 .read = thinkpad_acpi_driver_read,
905 /*************************************************************************
906 * Hotkey subdriver
909 enum { /* hot key scan codes (derived from ACPI DSDT) */
910 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
911 TP_ACPI_HOTKEYSCAN_FNF2,
912 TP_ACPI_HOTKEYSCAN_FNF3,
913 TP_ACPI_HOTKEYSCAN_FNF4,
914 TP_ACPI_HOTKEYSCAN_FNF5,
915 TP_ACPI_HOTKEYSCAN_FNF6,
916 TP_ACPI_HOTKEYSCAN_FNF7,
917 TP_ACPI_HOTKEYSCAN_FNF8,
918 TP_ACPI_HOTKEYSCAN_FNF9,
919 TP_ACPI_HOTKEYSCAN_FNF10,
920 TP_ACPI_HOTKEYSCAN_FNF11,
921 TP_ACPI_HOTKEYSCAN_FNF12,
922 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
923 TP_ACPI_HOTKEYSCAN_FNINSERT,
924 TP_ACPI_HOTKEYSCAN_FNDELETE,
925 TP_ACPI_HOTKEYSCAN_FNHOME,
926 TP_ACPI_HOTKEYSCAN_FNEND,
927 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
928 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
929 TP_ACPI_HOTKEYSCAN_FNSPACE,
930 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
931 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
932 TP_ACPI_HOTKEYSCAN_MUTE,
933 TP_ACPI_HOTKEYSCAN_THINKPAD,
936 enum { /* Keys available through NVRAM polling */
937 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
938 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
941 enum { /* Positions of some of the keys in hotkey masks */
942 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
943 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
944 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
945 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
946 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
947 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
948 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
949 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
950 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
951 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
952 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
955 enum { /* NVRAM to ACPI HKEY group map */
956 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
957 TP_ACPI_HKEY_ZOOM_MASK |
958 TP_ACPI_HKEY_DISPSWTCH_MASK |
959 TP_ACPI_HKEY_HIBERNATE_MASK,
960 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
961 TP_ACPI_HKEY_BRGHTDWN_MASK,
962 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
963 TP_ACPI_HKEY_VOLDWN_MASK |
964 TP_ACPI_HKEY_MUTE_MASK,
967 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
968 struct tp_nvram_state {
969 u16 thinkpad_toggle:1;
970 u16 zoom_toggle:1;
971 u16 display_toggle:1;
972 u16 thinklight_toggle:1;
973 u16 hibernate_toggle:1;
974 u16 displayexp_toggle:1;
975 u16 display_state:1;
976 u16 brightness_toggle:1;
977 u16 volume_toggle:1;
978 u16 mute:1;
980 u8 brightness_level;
981 u8 volume_level;
984 static struct task_struct *tpacpi_hotkey_task;
985 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
986 static int hotkey_poll_freq = 10; /* Hz */
987 static struct mutex hotkey_thread_mutex;
988 static struct mutex hotkey_thread_data_mutex;
989 static unsigned int hotkey_config_change;
991 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
993 #define hotkey_source_mask 0U
995 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
997 static struct mutex hotkey_mutex;
999 static int hotkey_orig_status;
1000 static u32 hotkey_orig_mask;
1001 static u32 hotkey_all_mask;
1002 static u32 hotkey_reserved_mask;
1003 static u32 hotkey_mask;
1005 static unsigned int hotkey_report_mode;
1007 static u16 *hotkey_keycode_map;
1009 static struct attribute_set *hotkey_dev_attributes;
1011 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1012 #define HOTKEY_CONFIG_CRITICAL_START \
1013 mutex_lock(&hotkey_thread_data_mutex); \
1014 hotkey_config_change++;
1015 #define HOTKEY_CONFIG_CRITICAL_END \
1016 mutex_unlock(&hotkey_thread_data_mutex);
1017 #else
1018 #define HOTKEY_CONFIG_CRITICAL_START
1019 #define HOTKEY_CONFIG_CRITICAL_END
1020 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1022 static int hotkey_get_wlsw(int *status)
1024 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1025 return -EIO;
1026 return 0;
1030 * Call with hotkey_mutex held
1032 static int hotkey_mask_get(void)
1034 u32 m = 0;
1036 if (tp_features.hotkey_mask) {
1037 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1038 return -EIO;
1040 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1042 return 0;
1046 * Call with hotkey_mutex held
1048 static int hotkey_mask_set(u32 mask)
1050 int i;
1051 int rc = 0;
1053 if (tp_features.hotkey_mask) {
1054 HOTKEY_CONFIG_CRITICAL_START
1055 for (i = 0; i < 32; i++) {
1056 u32 m = 1 << i;
1057 /* enable in firmware mask only keys not in NVRAM
1058 * mode, but enable the key in the cached hotkey_mask
1059 * regardless of mode, or the key will end up
1060 * disabled by hotkey_mask_get() */
1061 if (!acpi_evalf(hkey_handle,
1062 NULL, "MHKM", "vdd", i + 1,
1063 !!((mask & ~hotkey_source_mask) & m))) {
1064 rc = -EIO;
1065 break;
1066 } else {
1067 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1070 HOTKEY_CONFIG_CRITICAL_END
1072 /* hotkey_mask_get must be called unconditionally below */
1073 if (!hotkey_mask_get() && !rc &&
1074 (hotkey_mask & ~hotkey_source_mask) !=
1075 (mask & ~hotkey_source_mask)) {
1076 printk(TPACPI_NOTICE
1077 "requested hot key mask 0x%08x, but "
1078 "firmware forced it to 0x%08x\n",
1079 mask, hotkey_mask);
1081 } else {
1082 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1083 HOTKEY_CONFIG_CRITICAL_START
1084 hotkey_mask = mask & hotkey_source_mask;
1085 HOTKEY_CONFIG_CRITICAL_END
1086 hotkey_mask_get();
1087 if (hotkey_mask != mask) {
1088 printk(TPACPI_NOTICE
1089 "requested hot key mask 0x%08x, "
1090 "forced to 0x%08x (NVRAM poll mask is "
1091 "0x%08x): no firmware mask support\n",
1092 mask, hotkey_mask, hotkey_source_mask);
1094 #else
1095 hotkey_mask_get();
1096 rc = -ENXIO;
1097 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1100 return rc;
1103 static int hotkey_status_get(int *status)
1105 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1106 return -EIO;
1108 return 0;
1111 static int hotkey_status_set(int status)
1113 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1114 return -EIO;
1116 return 0;
1119 static void tpacpi_input_send_radiosw(void)
1121 int wlsw;
1123 mutex_lock(&tpacpi_inputdev_send_mutex);
1125 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1126 input_report_switch(tpacpi_inputdev,
1127 SW_RADIO, !!wlsw);
1128 input_sync(tpacpi_inputdev);
1131 mutex_unlock(&tpacpi_inputdev_send_mutex);
1134 static void tpacpi_input_send_key(unsigned int scancode)
1136 unsigned int keycode;
1138 keycode = hotkey_keycode_map[scancode];
1140 if (keycode != KEY_RESERVED) {
1141 mutex_lock(&tpacpi_inputdev_send_mutex);
1143 input_report_key(tpacpi_inputdev, keycode, 1);
1144 if (keycode == KEY_UNKNOWN)
1145 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1146 scancode);
1147 input_sync(tpacpi_inputdev);
1149 input_report_key(tpacpi_inputdev, keycode, 0);
1150 if (keycode == KEY_UNKNOWN)
1151 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1152 scancode);
1153 input_sync(tpacpi_inputdev);
1155 mutex_unlock(&tpacpi_inputdev_send_mutex);
1159 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1160 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1162 static void tpacpi_hotkey_send_key(unsigned int scancode)
1164 tpacpi_input_send_key(scancode);
1165 if (hotkey_report_mode < 2) {
1166 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1167 0x80, 0x1001 + scancode);
1171 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1173 u8 d;
1175 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1176 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1177 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1178 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1179 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1180 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1182 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1183 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1184 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1186 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1187 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1188 n->displayexp_toggle =
1189 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1191 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1192 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1193 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1194 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1195 n->brightness_toggle =
1196 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1198 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1199 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1200 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1201 >> TP_NVRAM_POS_LEVEL_VOLUME;
1202 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1203 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1207 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1208 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1209 tpacpi_hotkey_send_key(__scancode); } while (0)
1211 #define TPACPI_MAY_SEND_KEY(__scancode) \
1212 do { if (mask & (1 << __scancode)) \
1213 tpacpi_hotkey_send_key(__scancode); } while (0)
1215 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1216 struct tp_nvram_state *newn,
1217 u32 mask)
1219 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1220 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1221 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1222 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1224 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1226 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1228 /* handle volume */
1229 if (oldn->volume_toggle != newn->volume_toggle) {
1230 if (oldn->mute != newn->mute) {
1231 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1233 if (oldn->volume_level > newn->volume_level) {
1234 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1235 } else if (oldn->volume_level < newn->volume_level) {
1236 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1237 } else if (oldn->mute == newn->mute) {
1238 /* repeated key presses that didn't change state */
1239 if (newn->mute) {
1240 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1241 } else if (newn->volume_level != 0) {
1242 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1243 } else {
1244 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1249 /* handle brightness */
1250 if (oldn->brightness_toggle != newn->brightness_toggle) {
1251 if (oldn->brightness_level < newn->brightness_level) {
1252 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1253 } else if (oldn->brightness_level > newn->brightness_level) {
1254 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1255 } else {
1256 /* repeated key presses that didn't change state */
1257 if (newn->brightness_level != 0) {
1258 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1259 } else {
1260 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1266 #undef TPACPI_COMPARE_KEY
1267 #undef TPACPI_MAY_SEND_KEY
1269 static int hotkey_kthread(void *data)
1271 struct tp_nvram_state s[2];
1272 u32 mask;
1273 unsigned int si, so;
1274 unsigned long t;
1275 unsigned int change_detector, must_reset;
1277 mutex_lock(&hotkey_thread_mutex);
1279 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1280 goto exit;
1282 set_freezable();
1284 so = 0;
1285 si = 1;
1286 t = 0;
1288 /* Initial state for compares */
1289 mutex_lock(&hotkey_thread_data_mutex);
1290 change_detector = hotkey_config_change;
1291 mask = hotkey_source_mask & hotkey_mask;
1292 mutex_unlock(&hotkey_thread_data_mutex);
1293 hotkey_read_nvram(&s[so], mask);
1295 while (!kthread_should_stop() && hotkey_poll_freq) {
1296 if (t == 0)
1297 t = 1000/hotkey_poll_freq;
1298 t = msleep_interruptible(t);
1299 if (unlikely(kthread_should_stop()))
1300 break;
1301 must_reset = try_to_freeze();
1302 if (t > 0 && !must_reset)
1303 continue;
1305 mutex_lock(&hotkey_thread_data_mutex);
1306 if (must_reset || hotkey_config_change != change_detector) {
1307 /* forget old state on thaw or config change */
1308 si = so;
1309 t = 0;
1310 change_detector = hotkey_config_change;
1312 mask = hotkey_source_mask & hotkey_mask;
1313 mutex_unlock(&hotkey_thread_data_mutex);
1315 if (likely(mask)) {
1316 hotkey_read_nvram(&s[si], mask);
1317 if (likely(si != so)) {
1318 hotkey_compare_and_issue_event(&s[so], &s[si],
1319 mask);
1323 so = si;
1324 si ^= 1;
1327 exit:
1328 mutex_unlock(&hotkey_thread_mutex);
1329 return 0;
1332 static void hotkey_poll_stop_sync(void)
1334 if (tpacpi_hotkey_task) {
1335 if (frozen(tpacpi_hotkey_task) ||
1336 freezing(tpacpi_hotkey_task))
1337 thaw_process(tpacpi_hotkey_task);
1339 kthread_stop(tpacpi_hotkey_task);
1340 tpacpi_hotkey_task = NULL;
1341 mutex_lock(&hotkey_thread_mutex);
1342 /* at this point, the thread did exit */
1343 mutex_unlock(&hotkey_thread_mutex);
1347 /* call with hotkey_mutex held */
1348 static void hotkey_poll_setup(int may_warn)
1350 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1351 hotkey_poll_freq > 0 &&
1352 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1353 if (!tpacpi_hotkey_task) {
1354 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1355 NULL, TPACPI_FILE "d");
1356 if (IS_ERR(tpacpi_hotkey_task)) {
1357 tpacpi_hotkey_task = NULL;
1358 printk(TPACPI_ERR "could not create kernel thread "
1359 "for hotkey polling\n");
1362 } else {
1363 hotkey_poll_stop_sync();
1364 if (may_warn &&
1365 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1366 printk(TPACPI_NOTICE "hot keys 0x%08x require polling, "
1367 "which is currently disabled\n",
1368 hotkey_source_mask);
1373 static void hotkey_poll_setup_safe(int may_warn)
1375 mutex_lock(&hotkey_mutex);
1376 hotkey_poll_setup(may_warn);
1377 mutex_unlock(&hotkey_mutex);
1380 static int hotkey_inputdev_open(struct input_dev *dev)
1382 switch (tpacpi_lifecycle) {
1383 case TPACPI_LIFE_INIT:
1385 * hotkey_init will call hotkey_poll_setup_safe
1386 * at the appropriate moment
1388 return 0;
1389 case TPACPI_LIFE_EXITING:
1390 return -EBUSY;
1391 case TPACPI_LIFE_RUNNING:
1392 hotkey_poll_setup_safe(0);
1393 return 0;
1396 /* Should only happen if tpacpi_lifecycle is corrupt */
1397 BUG();
1398 return -EBUSY;
1401 static void hotkey_inputdev_close(struct input_dev *dev)
1403 /* disable hotkey polling when possible */
1404 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1405 hotkey_poll_setup_safe(0);
1407 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1409 /* sysfs hotkey enable ------------------------------------------------- */
1410 static ssize_t hotkey_enable_show(struct device *dev,
1411 struct device_attribute *attr,
1412 char *buf)
1414 int res, status;
1416 res = hotkey_status_get(&status);
1417 if (res)
1418 return res;
1420 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1423 static ssize_t hotkey_enable_store(struct device *dev,
1424 struct device_attribute *attr,
1425 const char *buf, size_t count)
1427 unsigned long t;
1428 int res;
1430 if (parse_strtoul(buf, 1, &t))
1431 return -EINVAL;
1433 res = hotkey_status_set(t);
1435 return (res) ? res : count;
1438 static struct device_attribute dev_attr_hotkey_enable =
1439 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1440 hotkey_enable_show, hotkey_enable_store);
1442 /* sysfs hotkey mask --------------------------------------------------- */
1443 static ssize_t hotkey_mask_show(struct device *dev,
1444 struct device_attribute *attr,
1445 char *buf)
1447 int res;
1449 if (mutex_lock_interruptible(&hotkey_mutex))
1450 return -ERESTARTSYS;
1451 res = hotkey_mask_get();
1452 mutex_unlock(&hotkey_mutex);
1454 return (res)?
1455 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1458 static ssize_t hotkey_mask_store(struct device *dev,
1459 struct device_attribute *attr,
1460 const char *buf, size_t count)
1462 unsigned long t;
1463 int res;
1465 if (parse_strtoul(buf, 0xffffffffUL, &t))
1466 return -EINVAL;
1468 if (mutex_lock_interruptible(&hotkey_mutex))
1469 return -ERESTARTSYS;
1471 res = hotkey_mask_set(t);
1473 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1474 hotkey_poll_setup(1);
1475 #endif
1477 mutex_unlock(&hotkey_mutex);
1479 return (res) ? res : count;
1482 static struct device_attribute dev_attr_hotkey_mask =
1483 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1484 hotkey_mask_show, hotkey_mask_store);
1486 /* sysfs hotkey bios_enabled ------------------------------------------- */
1487 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1488 struct device_attribute *attr,
1489 char *buf)
1491 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1494 static struct device_attribute dev_attr_hotkey_bios_enabled =
1495 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1497 /* sysfs hotkey bios_mask ---------------------------------------------- */
1498 static ssize_t hotkey_bios_mask_show(struct device *dev,
1499 struct device_attribute *attr,
1500 char *buf)
1502 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1505 static struct device_attribute dev_attr_hotkey_bios_mask =
1506 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1508 /* sysfs hotkey all_mask ----------------------------------------------- */
1509 static ssize_t hotkey_all_mask_show(struct device *dev,
1510 struct device_attribute *attr,
1511 char *buf)
1513 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1514 hotkey_all_mask | hotkey_source_mask);
1517 static struct device_attribute dev_attr_hotkey_all_mask =
1518 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1520 /* sysfs hotkey recommended_mask --------------------------------------- */
1521 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1522 struct device_attribute *attr,
1523 char *buf)
1525 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1526 (hotkey_all_mask | hotkey_source_mask)
1527 & ~hotkey_reserved_mask);
1530 static struct device_attribute dev_attr_hotkey_recommended_mask =
1531 __ATTR(hotkey_recommended_mask, S_IRUGO,
1532 hotkey_recommended_mask_show, NULL);
1534 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1536 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1537 static ssize_t hotkey_source_mask_show(struct device *dev,
1538 struct device_attribute *attr,
1539 char *buf)
1541 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1544 static ssize_t hotkey_source_mask_store(struct device *dev,
1545 struct device_attribute *attr,
1546 const char *buf, size_t count)
1548 unsigned long t;
1550 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1551 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1552 return -EINVAL;
1554 if (mutex_lock_interruptible(&hotkey_mutex))
1555 return -ERESTARTSYS;
1557 HOTKEY_CONFIG_CRITICAL_START
1558 hotkey_source_mask = t;
1559 HOTKEY_CONFIG_CRITICAL_END
1561 hotkey_poll_setup(1);
1563 mutex_unlock(&hotkey_mutex);
1565 return count;
1568 static struct device_attribute dev_attr_hotkey_source_mask =
1569 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1570 hotkey_source_mask_show, hotkey_source_mask_store);
1572 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1573 static ssize_t hotkey_poll_freq_show(struct device *dev,
1574 struct device_attribute *attr,
1575 char *buf)
1577 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1580 static ssize_t hotkey_poll_freq_store(struct device *dev,
1581 struct device_attribute *attr,
1582 const char *buf, size_t count)
1584 unsigned long t;
1586 if (parse_strtoul(buf, 25, &t))
1587 return -EINVAL;
1589 if (mutex_lock_interruptible(&hotkey_mutex))
1590 return -ERESTARTSYS;
1592 hotkey_poll_freq = t;
1594 hotkey_poll_setup(1);
1595 mutex_unlock(&hotkey_mutex);
1597 return count;
1600 static struct device_attribute dev_attr_hotkey_poll_freq =
1601 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1602 hotkey_poll_freq_show, hotkey_poll_freq_store);
1604 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1606 /* sysfs hotkey radio_sw ----------------------------------------------- */
1607 static ssize_t hotkey_radio_sw_show(struct device *dev,
1608 struct device_attribute *attr,
1609 char *buf)
1611 int res, s;
1612 res = hotkey_get_wlsw(&s);
1613 if (res < 0)
1614 return res;
1616 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1619 static struct device_attribute dev_attr_hotkey_radio_sw =
1620 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1622 /* sysfs hotkey report_mode -------------------------------------------- */
1623 static ssize_t hotkey_report_mode_show(struct device *dev,
1624 struct device_attribute *attr,
1625 char *buf)
1627 return snprintf(buf, PAGE_SIZE, "%d\n",
1628 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1631 static struct device_attribute dev_attr_hotkey_report_mode =
1632 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1634 /* --------------------------------------------------------------------- */
1636 static struct attribute *hotkey_attributes[] __initdata = {
1637 &dev_attr_hotkey_enable.attr,
1638 &dev_attr_hotkey_bios_enabled.attr,
1639 &dev_attr_hotkey_report_mode.attr,
1640 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1641 &dev_attr_hotkey_mask.attr,
1642 &dev_attr_hotkey_all_mask.attr,
1643 &dev_attr_hotkey_recommended_mask.attr,
1644 &dev_attr_hotkey_source_mask.attr,
1645 &dev_attr_hotkey_poll_freq.attr,
1646 #endif
1649 static struct attribute *hotkey_mask_attributes[] __initdata = {
1650 &dev_attr_hotkey_bios_mask.attr,
1651 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1652 &dev_attr_hotkey_mask.attr,
1653 &dev_attr_hotkey_all_mask.attr,
1654 &dev_attr_hotkey_recommended_mask.attr,
1655 #endif
1658 static int __init hotkey_init(struct ibm_init_struct *iibm)
1661 /* Requirements for changing the default keymaps:
1663 * 1. Many of the keys are mapped to KEY_RESERVED for very
1664 * good reasons. Do not change them unless you have deep
1665 * knowledge on the IBM and Lenovo ThinkPad firmware for
1666 * the various ThinkPad models;
1668 * 2. You must be subscribed to the linux-thinkpad and
1669 * ibm-acpi-devel mailing lists, and you should read the
1670 * list archives since 2007. This requirement exists so
1671 * that you know the past history of problems with the
1672 * thinkpad-acpi driver keymaps, and also that you will
1673 * be listening to any bug reports;
1675 * 3. Do not send thinkpad-acpi specific patches directly to
1676 * Linus or to Andrew for merging. Send them to the
1677 * linux-acpi mailinglist, for comments. Merging is to
1678 * be done through acpi-test and the ACPI maintainer.
1680 * If the above is too much to ask, please consider sending a
1681 * simple email to the thinkpad-acpi maintainer describing the
1682 * changes you wanted to make and the rationale behind them,
1683 * instead.
1685 static u16 ibm_keycode_map[] __initdata = {
1686 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1687 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1688 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1689 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1690 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1691 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1692 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1693 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1694 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1695 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1696 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1697 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1698 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1699 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1700 KEY_RESERVED, /* 0x14: VOLUME UP */
1701 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1702 KEY_RESERVED, /* 0x16: MUTE */
1703 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1704 /* (assignments unknown, please report if found) */
1705 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1706 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1708 static u16 lenovo_keycode_map[] __initdata = {
1709 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1710 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1711 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1712 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1713 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1714 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1715 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1716 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1717 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1718 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1719 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1720 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1721 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1722 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1723 KEY_RESERVED, /* 0x14: VOLUME UP */
1724 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1725 KEY_RESERVED, /* 0x16: MUTE */
1726 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1727 /* (assignments unknown, please report if found) */
1728 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1729 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1732 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1733 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1734 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1736 int res, i;
1737 int status;
1738 int hkeyv;
1740 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1742 BUG_ON(!tpacpi_inputdev);
1743 BUG_ON(tpacpi_inputdev->open != NULL ||
1744 tpacpi_inputdev->close != NULL);
1746 TPACPI_ACPIHANDLE_INIT(hkey);
1747 mutex_init(&hotkey_mutex);
1749 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1750 mutex_init(&hotkey_thread_mutex);
1751 mutex_init(&hotkey_thread_data_mutex);
1752 #endif
1754 /* hotkey not supported on 570 */
1755 tp_features.hotkey = hkey_handle != NULL;
1757 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1758 str_supported(tp_features.hotkey));
1760 if (tp_features.hotkey) {
1761 hotkey_dev_attributes = create_attr_set(10, NULL);
1762 if (!hotkey_dev_attributes)
1763 return -ENOMEM;
1764 res = add_many_to_attr_set(hotkey_dev_attributes,
1765 hotkey_attributes,
1766 ARRAY_SIZE(hotkey_attributes));
1767 if (res)
1768 return res;
1770 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1771 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1772 for HKEY interface version 0x100 */
1773 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1774 if ((hkeyv >> 8) != 1) {
1775 printk(TPACPI_ERR "unknown version of the "
1776 "HKEY interface: 0x%x\n", hkeyv);
1777 printk(TPACPI_ERR "please report this to %s\n",
1778 TPACPI_MAIL);
1779 } else {
1781 * MHKV 0x100 in A31, R40, R40e,
1782 * T4x, X31, and later
1784 tp_features.hotkey_mask = 1;
1788 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1789 str_supported(tp_features.hotkey_mask));
1791 if (tp_features.hotkey_mask) {
1792 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1793 "MHKA", "qd")) {
1794 printk(TPACPI_ERR
1795 "missing MHKA handler, "
1796 "please report this to %s\n",
1797 TPACPI_MAIL);
1798 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1802 /* hotkey_source_mask *must* be zero for
1803 * the first hotkey_mask_get */
1804 res = hotkey_status_get(&hotkey_orig_status);
1805 if (!res && tp_features.hotkey_mask) {
1806 res = hotkey_mask_get();
1807 hotkey_orig_mask = hotkey_mask;
1808 if (!res) {
1809 res = add_many_to_attr_set(
1810 hotkey_dev_attributes,
1811 hotkey_mask_attributes,
1812 ARRAY_SIZE(hotkey_mask_attributes));
1816 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1817 if (tp_features.hotkey_mask) {
1818 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1819 & ~hotkey_all_mask;
1820 } else {
1821 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1824 vdbg_printk(TPACPI_DBG_INIT,
1825 "hotkey source mask 0x%08x, polling freq %d\n",
1826 hotkey_source_mask, hotkey_poll_freq);
1827 #endif
1829 /* Not all thinkpads have a hardware radio switch */
1830 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1831 tp_features.hotkey_wlsw = 1;
1832 printk(TPACPI_INFO
1833 "radio switch found; radios are %s\n",
1834 enabled(status, 0));
1835 res = add_to_attr_set(hotkey_dev_attributes,
1836 &dev_attr_hotkey_radio_sw.attr);
1839 if (!res)
1840 res = register_attr_set_with_sysfs(
1841 hotkey_dev_attributes,
1842 &tpacpi_pdev->dev.kobj);
1843 if (res)
1844 return res;
1846 /* Set up key map */
1848 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1849 GFP_KERNEL);
1850 if (!hotkey_keycode_map) {
1851 printk(TPACPI_ERR "failed to allocate memory for key map\n");
1852 return -ENOMEM;
1855 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1856 dbg_printk(TPACPI_DBG_INIT,
1857 "using Lenovo default hot key map\n");
1858 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1859 TPACPI_HOTKEY_MAP_SIZE);
1860 } else {
1861 dbg_printk(TPACPI_DBG_INIT,
1862 "using IBM default hot key map\n");
1863 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1864 TPACPI_HOTKEY_MAP_SIZE);
1867 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1868 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1869 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1870 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1871 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1872 tpacpi_inputdev->keycode = hotkey_keycode_map;
1873 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1874 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1875 set_bit(hotkey_keycode_map[i],
1876 tpacpi_inputdev->keybit);
1877 } else {
1878 if (i < sizeof(hotkey_reserved_mask)*8)
1879 hotkey_reserved_mask |= 1 << i;
1883 if (tp_features.hotkey_wlsw) {
1884 set_bit(EV_SW, tpacpi_inputdev->evbit);
1885 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1888 dbg_printk(TPACPI_DBG_INIT,
1889 "enabling hot key handling\n");
1890 res = hotkey_status_set(1);
1891 if (res)
1892 return res;
1893 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1894 & ~hotkey_reserved_mask)
1895 | hotkey_orig_mask);
1896 if (res < 0 && res != -ENXIO)
1897 return res;
1899 dbg_printk(TPACPI_DBG_INIT,
1900 "legacy hot key reporting over procfs %s\n",
1901 (hotkey_report_mode < 2) ?
1902 "enabled" : "disabled");
1904 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1905 tpacpi_inputdev->open = &hotkey_inputdev_open;
1906 tpacpi_inputdev->close = &hotkey_inputdev_close;
1908 hotkey_poll_setup_safe(1);
1909 #endif
1912 return (tp_features.hotkey)? 0 : 1;
1915 static void hotkey_exit(void)
1917 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1918 hotkey_poll_stop_sync();
1919 #endif
1921 if (tp_features.hotkey) {
1922 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
1923 /* no short-circuit boolean operator below! */
1924 if ((hotkey_mask_set(hotkey_orig_mask) |
1925 hotkey_status_set(hotkey_orig_status)) != 0)
1926 printk(TPACPI_ERR "failed to restore hot key mask to BIOS defaults\n");
1929 if (hotkey_dev_attributes) {
1930 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1931 hotkey_dev_attributes = NULL;
1935 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1937 u32 hkey;
1938 unsigned int scancode;
1939 int send_acpi_ev;
1940 int ignore_acpi_ev;
1942 if (event != 0x80) {
1943 printk(TPACPI_ERR "unknown HKEY notification event %d\n", event);
1944 /* forward it to userspace, maybe it knows how to handle it */
1945 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1946 ibm->acpi->device->dev.bus_id,
1947 event, 0);
1948 return;
1951 while (1) {
1952 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1953 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
1954 return;
1957 if (hkey == 0) {
1958 /* queue empty */
1959 return;
1962 send_acpi_ev = 0;
1963 ignore_acpi_ev = 0;
1965 switch (hkey >> 12) {
1966 case 1:
1967 /* 0x1000-0x1FFF: key presses */
1968 scancode = hkey & 0xfff;
1969 if (scancode > 0 && scancode < 0x21) {
1970 scancode--;
1971 if (!(hotkey_source_mask & (1 << scancode))) {
1972 tpacpi_input_send_key(scancode);
1973 } else {
1974 ignore_acpi_ev = 1;
1976 } else {
1977 printk(TPACPI_ERR
1978 "hotkey 0x%04x out of range for keyboard map\n",
1979 hkey);
1980 send_acpi_ev = 1;
1982 break;
1983 case 5:
1984 /* 0x5000-0x5FFF: LID */
1985 /* we don't handle it through this path, just
1986 * eat up known LID events */
1987 if (hkey != 0x5001 && hkey != 0x5002) {
1988 printk(TPACPI_ERR
1989 "unknown LID-related HKEY event: 0x%04x\n",
1990 hkey);
1991 send_acpi_ev = 1;
1992 } else {
1993 ignore_acpi_ev = 1;
1995 break;
1996 case 7:
1997 /* 0x7000-0x7FFF: misc */
1998 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1999 tpacpi_input_send_radiosw();
2000 break;
2002 /* fallthrough to default */
2003 default:
2004 /* case 2: dock-related */
2005 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
2006 /* case 3: ultra-bay related. maybe bay in dock? */
2007 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
2008 printk(TPACPI_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
2009 send_acpi_ev = 1;
2012 /* Legacy events */
2013 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
2014 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
2017 /* netlink events */
2018 if (!ignore_acpi_ev && send_acpi_ev) {
2019 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2020 ibm->acpi->device->dev.bus_id,
2021 event, hkey);
2026 static void hotkey_resume(void)
2028 if (hotkey_mask_get())
2029 printk(TPACPI_ERR "error while trying to read hot key mask from firmware\n");
2030 tpacpi_input_send_radiosw();
2031 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2032 hotkey_poll_setup_safe(0);
2033 #endif
2036 /* procfs -------------------------------------------------------------- */
2037 static int hotkey_read(char *p)
2039 int res, status;
2040 int len = 0;
2042 if (!tp_features.hotkey) {
2043 len += sprintf(p + len, "status:\t\tnot supported\n");
2044 return len;
2047 if (mutex_lock_interruptible(&hotkey_mutex))
2048 return -ERESTARTSYS;
2049 res = hotkey_status_get(&status);
2050 if (!res)
2051 res = hotkey_mask_get();
2052 mutex_unlock(&hotkey_mutex);
2053 if (res)
2054 return res;
2056 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2057 if (tp_features.hotkey_mask) {
2058 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2059 len += sprintf(p + len,
2060 "commands:\tenable, disable, reset, <mask>\n");
2061 } else {
2062 len += sprintf(p + len, "mask:\t\tnot supported\n");
2063 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2066 return len;
2069 static int hotkey_write(char *buf)
2071 int res, status;
2072 u32 mask;
2073 char *cmd;
2075 if (!tp_features.hotkey)
2076 return -ENODEV;
2078 if (mutex_lock_interruptible(&hotkey_mutex))
2079 return -ERESTARTSYS;
2081 status = -1;
2082 mask = hotkey_mask;
2084 res = 0;
2085 while ((cmd = next_cmd(&buf))) {
2086 if (strlencmp(cmd, "enable") == 0) {
2087 status = 1;
2088 } else if (strlencmp(cmd, "disable") == 0) {
2089 status = 0;
2090 } else if (strlencmp(cmd, "reset") == 0) {
2091 status = hotkey_orig_status;
2092 mask = hotkey_orig_mask;
2093 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2094 /* mask set */
2095 } else if (sscanf(cmd, "%x", &mask) == 1) {
2096 /* mask set */
2097 } else {
2098 res = -EINVAL;
2099 goto errexit;
2102 if (status != -1)
2103 res = hotkey_status_set(status);
2105 if (!res && mask != hotkey_mask)
2106 res = hotkey_mask_set(mask);
2108 errexit:
2109 mutex_unlock(&hotkey_mutex);
2110 return res;
2113 static const struct acpi_device_id ibm_htk_device_ids[] = {
2114 {TPACPI_ACPI_HKEY_HID, 0},
2115 {"", 0},
2118 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2119 .hid = ibm_htk_device_ids,
2120 .notify = hotkey_notify,
2121 .handle = &hkey_handle,
2122 .type = ACPI_DEVICE_NOTIFY,
2125 static struct ibm_struct hotkey_driver_data = {
2126 .name = "hotkey",
2127 .read = hotkey_read,
2128 .write = hotkey_write,
2129 .exit = hotkey_exit,
2130 .resume = hotkey_resume,
2131 .acpi = &ibm_hotkey_acpidriver,
2134 /*************************************************************************
2135 * Bluetooth subdriver
2138 enum {
2139 /* ACPI GBDC/SBDC bits */
2140 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2141 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2142 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2145 static int bluetooth_get_radiosw(void);
2146 static int bluetooth_set_radiosw(int radio_on);
2148 /* sysfs bluetooth enable ---------------------------------------------- */
2149 static ssize_t bluetooth_enable_show(struct device *dev,
2150 struct device_attribute *attr,
2151 char *buf)
2153 int status;
2155 status = bluetooth_get_radiosw();
2156 if (status < 0)
2157 return status;
2159 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2162 static ssize_t bluetooth_enable_store(struct device *dev,
2163 struct device_attribute *attr,
2164 const char *buf, size_t count)
2166 unsigned long t;
2167 int res;
2169 if (parse_strtoul(buf, 1, &t))
2170 return -EINVAL;
2172 res = bluetooth_set_radiosw(t);
2174 return (res) ? res : count;
2177 static struct device_attribute dev_attr_bluetooth_enable =
2178 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2179 bluetooth_enable_show, bluetooth_enable_store);
2181 /* --------------------------------------------------------------------- */
2183 static struct attribute *bluetooth_attributes[] = {
2184 &dev_attr_bluetooth_enable.attr,
2185 NULL
2188 static const struct attribute_group bluetooth_attr_group = {
2189 .attrs = bluetooth_attributes,
2192 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2194 int res;
2195 int status = 0;
2197 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2199 TPACPI_ACPIHANDLE_INIT(hkey);
2201 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2202 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2203 tp_features.bluetooth = hkey_handle &&
2204 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2206 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2207 str_supported(tp_features.bluetooth),
2208 status);
2210 if (tp_features.bluetooth) {
2211 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2212 /* no bluetooth hardware present in system */
2213 tp_features.bluetooth = 0;
2214 dbg_printk(TPACPI_DBG_INIT,
2215 "bluetooth hardware not installed\n");
2216 } else {
2217 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2218 &bluetooth_attr_group);
2219 if (res)
2220 return res;
2224 return (tp_features.bluetooth)? 0 : 1;
2227 static void bluetooth_exit(void)
2229 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2230 &bluetooth_attr_group);
2233 static int bluetooth_get_radiosw(void)
2235 int status;
2237 if (!tp_features.bluetooth)
2238 return -ENODEV;
2240 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2241 return -EIO;
2243 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2246 static int bluetooth_set_radiosw(int radio_on)
2248 int status;
2250 if (!tp_features.bluetooth)
2251 return -ENODEV;
2253 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2254 return -EIO;
2255 if (radio_on)
2256 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2257 else
2258 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2259 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2260 return -EIO;
2262 return 0;
2265 /* procfs -------------------------------------------------------------- */
2266 static int bluetooth_read(char *p)
2268 int len = 0;
2269 int status = bluetooth_get_radiosw();
2271 if (!tp_features.bluetooth)
2272 len += sprintf(p + len, "status:\t\tnot supported\n");
2273 else {
2274 len += sprintf(p + len, "status:\t\t%s\n",
2275 (status)? "enabled" : "disabled");
2276 len += sprintf(p + len, "commands:\tenable, disable\n");
2279 return len;
2282 static int bluetooth_write(char *buf)
2284 char *cmd;
2286 if (!tp_features.bluetooth)
2287 return -ENODEV;
2289 while ((cmd = next_cmd(&buf))) {
2290 if (strlencmp(cmd, "enable") == 0) {
2291 bluetooth_set_radiosw(1);
2292 } else if (strlencmp(cmd, "disable") == 0) {
2293 bluetooth_set_radiosw(0);
2294 } else
2295 return -EINVAL;
2298 return 0;
2301 static struct ibm_struct bluetooth_driver_data = {
2302 .name = "bluetooth",
2303 .read = bluetooth_read,
2304 .write = bluetooth_write,
2305 .exit = bluetooth_exit,
2308 /*************************************************************************
2309 * Wan subdriver
2312 enum {
2313 /* ACPI GWAN/SWAN bits */
2314 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2315 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2316 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2319 static int wan_get_radiosw(void);
2320 static int wan_set_radiosw(int radio_on);
2322 /* sysfs wan enable ---------------------------------------------------- */
2323 static ssize_t wan_enable_show(struct device *dev,
2324 struct device_attribute *attr,
2325 char *buf)
2327 int status;
2329 status = wan_get_radiosw();
2330 if (status < 0)
2331 return status;
2333 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2336 static ssize_t wan_enable_store(struct device *dev,
2337 struct device_attribute *attr,
2338 const char *buf, size_t count)
2340 unsigned long t;
2341 int res;
2343 if (parse_strtoul(buf, 1, &t))
2344 return -EINVAL;
2346 res = wan_set_radiosw(t);
2348 return (res) ? res : count;
2351 static struct device_attribute dev_attr_wan_enable =
2352 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2353 wan_enable_show, wan_enable_store);
2355 /* --------------------------------------------------------------------- */
2357 static struct attribute *wan_attributes[] = {
2358 &dev_attr_wan_enable.attr,
2359 NULL
2362 static const struct attribute_group wan_attr_group = {
2363 .attrs = wan_attributes,
2366 static int __init wan_init(struct ibm_init_struct *iibm)
2368 int res;
2369 int status = 0;
2371 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2373 TPACPI_ACPIHANDLE_INIT(hkey);
2375 tp_features.wan = hkey_handle &&
2376 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2378 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2379 str_supported(tp_features.wan),
2380 status);
2382 if (tp_features.wan) {
2383 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2384 /* no wan hardware present in system */
2385 tp_features.wan = 0;
2386 dbg_printk(TPACPI_DBG_INIT,
2387 "wan hardware not installed\n");
2388 } else {
2389 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2390 &wan_attr_group);
2391 if (res)
2392 return res;
2396 return (tp_features.wan)? 0 : 1;
2399 static void wan_exit(void)
2401 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2402 &wan_attr_group);
2405 static int wan_get_radiosw(void)
2407 int status;
2409 if (!tp_features.wan)
2410 return -ENODEV;
2412 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2413 return -EIO;
2415 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2418 static int wan_set_radiosw(int radio_on)
2420 int status;
2422 if (!tp_features.wan)
2423 return -ENODEV;
2425 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2426 return -EIO;
2427 if (radio_on)
2428 status |= TP_ACPI_WANCARD_RADIOSSW;
2429 else
2430 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2431 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2432 return -EIO;
2434 return 0;
2437 /* procfs -------------------------------------------------------------- */
2438 static int wan_read(char *p)
2440 int len = 0;
2441 int status = wan_get_radiosw();
2443 if (!tp_features.wan)
2444 len += sprintf(p + len, "status:\t\tnot supported\n");
2445 else {
2446 len += sprintf(p + len, "status:\t\t%s\n",
2447 (status)? "enabled" : "disabled");
2448 len += sprintf(p + len, "commands:\tenable, disable\n");
2451 return len;
2454 static int wan_write(char *buf)
2456 char *cmd;
2458 if (!tp_features.wan)
2459 return -ENODEV;
2461 while ((cmd = next_cmd(&buf))) {
2462 if (strlencmp(cmd, "enable") == 0) {
2463 wan_set_radiosw(1);
2464 } else if (strlencmp(cmd, "disable") == 0) {
2465 wan_set_radiosw(0);
2466 } else
2467 return -EINVAL;
2470 return 0;
2473 static struct ibm_struct wan_driver_data = {
2474 .name = "wan",
2475 .read = wan_read,
2476 .write = wan_write,
2477 .exit = wan_exit,
2478 .flags.experimental = 1,
2481 /*************************************************************************
2482 * Video subdriver
2485 enum video_access_mode {
2486 TPACPI_VIDEO_NONE = 0,
2487 TPACPI_VIDEO_570, /* 570 */
2488 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2489 TPACPI_VIDEO_NEW, /* all others */
2492 enum { /* video status flags, based on VIDEO_570 */
2493 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2494 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2495 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2498 enum { /* TPACPI_VIDEO_570 constants */
2499 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2500 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2501 * video_status_flags */
2502 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2503 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2506 static enum video_access_mode video_supported;
2507 static int video_orig_autosw;
2509 static int video_autosw_get(void);
2510 static int video_autosw_set(int enable);
2512 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2513 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2514 "\\_SB.PCI0.VID0", /* 770e */
2515 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2516 "\\_SB.PCI0.AGP.VID", /* all others */
2517 ); /* R30, R31 */
2519 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2521 static int __init video_init(struct ibm_init_struct *iibm)
2523 int ivga;
2525 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2527 TPACPI_ACPIHANDLE_INIT(vid);
2528 TPACPI_ACPIHANDLE_INIT(vid2);
2530 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2531 /* G41, assume IVGA doesn't change */
2532 vid_handle = vid2_handle;
2534 if (!vid_handle)
2535 /* video switching not supported on R30, R31 */
2536 video_supported = TPACPI_VIDEO_NONE;
2537 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2538 /* 570 */
2539 video_supported = TPACPI_VIDEO_570;
2540 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2541 /* 600e/x, 770e, 770x */
2542 video_supported = TPACPI_VIDEO_770;
2543 else
2544 /* all others */
2545 video_supported = TPACPI_VIDEO_NEW;
2547 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2548 str_supported(video_supported != TPACPI_VIDEO_NONE),
2549 video_supported);
2551 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2554 static void video_exit(void)
2556 dbg_printk(TPACPI_DBG_EXIT,
2557 "restoring original video autoswitch mode\n");
2558 if (video_autosw_set(video_orig_autosw))
2559 printk(TPACPI_ERR "error while trying to restore original "
2560 "video autoswitch mode\n");
2563 static int video_outputsw_get(void)
2565 int status = 0;
2566 int i;
2568 switch (video_supported) {
2569 case TPACPI_VIDEO_570:
2570 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2571 TP_ACPI_VIDEO_570_PHSCMD))
2572 return -EIO;
2573 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2574 break;
2575 case TPACPI_VIDEO_770:
2576 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2577 return -EIO;
2578 if (i)
2579 status |= TP_ACPI_VIDEO_S_LCD;
2580 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2581 return -EIO;
2582 if (i)
2583 status |= TP_ACPI_VIDEO_S_CRT;
2584 break;
2585 case TPACPI_VIDEO_NEW:
2586 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2587 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2588 return -EIO;
2589 if (i)
2590 status |= TP_ACPI_VIDEO_S_CRT;
2592 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2593 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2594 return -EIO;
2595 if (i)
2596 status |= TP_ACPI_VIDEO_S_LCD;
2597 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2598 return -EIO;
2599 if (i)
2600 status |= TP_ACPI_VIDEO_S_DVI;
2601 break;
2602 default:
2603 return -ENOSYS;
2606 return status;
2609 static int video_outputsw_set(int status)
2611 int autosw;
2612 int res = 0;
2614 switch (video_supported) {
2615 case TPACPI_VIDEO_570:
2616 res = acpi_evalf(NULL, NULL,
2617 "\\_SB.PHS2", "vdd",
2618 TP_ACPI_VIDEO_570_PHS2CMD,
2619 status | TP_ACPI_VIDEO_570_PHS2SET);
2620 break;
2621 case TPACPI_VIDEO_770:
2622 autosw = video_autosw_get();
2623 if (autosw < 0)
2624 return autosw;
2626 res = video_autosw_set(1);
2627 if (res)
2628 return res;
2629 res = acpi_evalf(vid_handle, NULL,
2630 "ASWT", "vdd", status * 0x100, 0);
2631 if (!autosw && video_autosw_set(autosw)) {
2632 printk(TPACPI_ERR "video auto-switch left enabled due to error\n");
2633 return -EIO;
2635 break;
2636 case TPACPI_VIDEO_NEW:
2637 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2638 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2639 break;
2640 default:
2641 return -ENOSYS;
2644 return (res)? 0 : -EIO;
2647 static int video_autosw_get(void)
2649 int autosw = 0;
2651 switch (video_supported) {
2652 case TPACPI_VIDEO_570:
2653 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2654 return -EIO;
2655 break;
2656 case TPACPI_VIDEO_770:
2657 case TPACPI_VIDEO_NEW:
2658 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2659 return -EIO;
2660 break;
2661 default:
2662 return -ENOSYS;
2665 return autosw & 1;
2668 static int video_autosw_set(int enable)
2670 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2671 return -EIO;
2672 return 0;
2675 static int video_outputsw_cycle(void)
2677 int autosw = video_autosw_get();
2678 int res;
2680 if (autosw < 0)
2681 return autosw;
2683 switch (video_supported) {
2684 case TPACPI_VIDEO_570:
2685 res = video_autosw_set(1);
2686 if (res)
2687 return res;
2688 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2689 break;
2690 case TPACPI_VIDEO_770:
2691 case TPACPI_VIDEO_NEW:
2692 res = video_autosw_set(1);
2693 if (res)
2694 return res;
2695 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2696 break;
2697 default:
2698 return -ENOSYS;
2700 if (!autosw && video_autosw_set(autosw)) {
2701 printk(TPACPI_ERR "video auto-switch left enabled due to error\n");
2702 return -EIO;
2705 return (res)? 0 : -EIO;
2708 static int video_expand_toggle(void)
2710 switch (video_supported) {
2711 case TPACPI_VIDEO_570:
2712 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2713 0 : -EIO;
2714 case TPACPI_VIDEO_770:
2715 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2716 0 : -EIO;
2717 case TPACPI_VIDEO_NEW:
2718 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2719 0 : -EIO;
2720 default:
2721 return -ENOSYS;
2723 /* not reached */
2726 static int video_read(char *p)
2728 int status, autosw;
2729 int len = 0;
2731 if (video_supported == TPACPI_VIDEO_NONE) {
2732 len += sprintf(p + len, "status:\t\tnot supported\n");
2733 return len;
2736 status = video_outputsw_get();
2737 if (status < 0)
2738 return status;
2740 autosw = video_autosw_get();
2741 if (autosw < 0)
2742 return autosw;
2744 len += sprintf(p + len, "status:\t\tsupported\n");
2745 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2746 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2747 if (video_supported == TPACPI_VIDEO_NEW)
2748 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2749 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2750 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2751 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2752 if (video_supported == TPACPI_VIDEO_NEW)
2753 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2754 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2755 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2757 return len;
2760 static int video_write(char *buf)
2762 char *cmd;
2763 int enable, disable, status;
2764 int res;
2766 if (video_supported == TPACPI_VIDEO_NONE)
2767 return -ENODEV;
2769 enable = 0;
2770 disable = 0;
2772 while ((cmd = next_cmd(&buf))) {
2773 if (strlencmp(cmd, "lcd_enable") == 0) {
2774 enable |= TP_ACPI_VIDEO_S_LCD;
2775 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2776 disable |= TP_ACPI_VIDEO_S_LCD;
2777 } else if (strlencmp(cmd, "crt_enable") == 0) {
2778 enable |= TP_ACPI_VIDEO_S_CRT;
2779 } else if (strlencmp(cmd, "crt_disable") == 0) {
2780 disable |= TP_ACPI_VIDEO_S_CRT;
2781 } else if (video_supported == TPACPI_VIDEO_NEW &&
2782 strlencmp(cmd, "dvi_enable") == 0) {
2783 enable |= TP_ACPI_VIDEO_S_DVI;
2784 } else if (video_supported == TPACPI_VIDEO_NEW &&
2785 strlencmp(cmd, "dvi_disable") == 0) {
2786 disable |= TP_ACPI_VIDEO_S_DVI;
2787 } else if (strlencmp(cmd, "auto_enable") == 0) {
2788 res = video_autosw_set(1);
2789 if (res)
2790 return res;
2791 } else if (strlencmp(cmd, "auto_disable") == 0) {
2792 res = video_autosw_set(0);
2793 if (res)
2794 return res;
2795 } else if (strlencmp(cmd, "video_switch") == 0) {
2796 res = video_outputsw_cycle();
2797 if (res)
2798 return res;
2799 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2800 res = video_expand_toggle();
2801 if (res)
2802 return res;
2803 } else
2804 return -EINVAL;
2807 if (enable || disable) {
2808 status = video_outputsw_get();
2809 if (status < 0)
2810 return status;
2811 res = video_outputsw_set((status & ~disable) | enable);
2812 if (res)
2813 return res;
2816 return 0;
2819 static struct ibm_struct video_driver_data = {
2820 .name = "video",
2821 .read = video_read,
2822 .write = video_write,
2823 .exit = video_exit,
2826 /*************************************************************************
2827 * Light (thinklight) subdriver
2830 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2831 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
2833 static int __init light_init(struct ibm_init_struct *iibm)
2835 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2837 TPACPI_ACPIHANDLE_INIT(ledb);
2838 TPACPI_ACPIHANDLE_INIT(lght);
2839 TPACPI_ACPIHANDLE_INIT(cmos);
2841 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2842 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2844 if (tp_features.light)
2845 /* light status not supported on
2846 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2847 tp_features.light_status =
2848 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2850 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2851 str_supported(tp_features.light));
2853 return (tp_features.light)? 0 : 1;
2856 static int light_read(char *p)
2858 int len = 0;
2859 int status = 0;
2861 if (!tp_features.light) {
2862 len += sprintf(p + len, "status:\t\tnot supported\n");
2863 } else if (!tp_features.light_status) {
2864 len += sprintf(p + len, "status:\t\tunknown\n");
2865 len += sprintf(p + len, "commands:\ton, off\n");
2866 } else {
2867 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2868 return -EIO;
2869 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2870 len += sprintf(p + len, "commands:\ton, off\n");
2873 return len;
2876 static int light_write(char *buf)
2878 int cmos_cmd, lght_cmd;
2879 char *cmd;
2880 int success;
2882 if (!tp_features.light)
2883 return -ENODEV;
2885 while ((cmd = next_cmd(&buf))) {
2886 if (strlencmp(cmd, "on") == 0) {
2887 cmos_cmd = 0x0c;
2888 lght_cmd = 1;
2889 } else if (strlencmp(cmd, "off") == 0) {
2890 cmos_cmd = 0x0d;
2891 lght_cmd = 0;
2892 } else
2893 return -EINVAL;
2895 success = cmos_handle ?
2896 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2897 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2898 if (!success)
2899 return -EIO;
2902 return 0;
2905 static struct ibm_struct light_driver_data = {
2906 .name = "light",
2907 .read = light_read,
2908 .write = light_write,
2911 /*************************************************************************
2912 * Dock subdriver
2915 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2917 static void dock_notify(struct ibm_struct *ibm, u32 event);
2918 static int dock_read(char *p);
2919 static int dock_write(char *buf);
2921 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2922 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2923 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2924 "\\_SB.PCI.ISA.SLCE", /* 570 */
2925 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2927 /* don't list other alternatives as we install a notify handler on the 570 */
2928 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2930 static const struct acpi_device_id ibm_pci_device_ids[] = {
2931 {PCI_ROOT_HID_STRING, 0},
2932 {"", 0},
2935 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2937 .notify = dock_notify,
2938 .handle = &dock_handle,
2939 .type = ACPI_SYSTEM_NOTIFY,
2942 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2943 * We just use it to get notifications of dock hotplug
2944 * in very old thinkpads */
2945 .hid = ibm_pci_device_ids,
2946 .notify = dock_notify,
2947 .handle = &pci_handle,
2948 .type = ACPI_SYSTEM_NOTIFY,
2952 static struct ibm_struct dock_driver_data[2] = {
2954 .name = "dock",
2955 .read = dock_read,
2956 .write = dock_write,
2957 .acpi = &ibm_dock_acpidriver[0],
2960 .name = "dock",
2961 .acpi = &ibm_dock_acpidriver[1],
2965 #define dock_docked() (_sta(dock_handle) & 1)
2967 static int __init dock_init(struct ibm_init_struct *iibm)
2969 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2971 TPACPI_ACPIHANDLE_INIT(dock);
2973 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2974 str_supported(dock_handle != NULL));
2976 return (dock_handle)? 0 : 1;
2979 static int __init dock_init2(struct ibm_init_struct *iibm)
2981 int dock2_needed;
2983 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2985 if (dock_driver_data[0].flags.acpi_driver_registered &&
2986 dock_driver_data[0].flags.acpi_notify_installed) {
2987 TPACPI_ACPIHANDLE_INIT(pci);
2988 dock2_needed = (pci_handle != NULL);
2989 vdbg_printk(TPACPI_DBG_INIT,
2990 "dock PCI handler for the TP 570 is %s\n",
2991 str_supported(dock2_needed));
2992 } else {
2993 vdbg_printk(TPACPI_DBG_INIT,
2994 "dock subdriver part 2 not required\n");
2995 dock2_needed = 0;
2998 return (dock2_needed)? 0 : 1;
3001 static void dock_notify(struct ibm_struct *ibm, u32 event)
3003 int docked = dock_docked();
3004 int pci = ibm->acpi->hid && ibm->acpi->device &&
3005 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3006 int data;
3008 if (event == 1 && !pci) /* 570 */
3009 data = 1; /* button */
3010 else if (event == 1 && pci) /* 570 */
3011 data = 3; /* dock */
3012 else if (event == 3 && docked)
3013 data = 1; /* button */
3014 else if (event == 3 && !docked)
3015 data = 2; /* undock */
3016 else if (event == 0 && docked)
3017 data = 3; /* dock */
3018 else {
3019 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3020 event, _sta(dock_handle));
3021 data = 0; /* unknown */
3023 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3024 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3025 ibm->acpi->device->dev.bus_id,
3026 event, data);
3029 static int dock_read(char *p)
3031 int len = 0;
3032 int docked = dock_docked();
3034 if (!dock_handle)
3035 len += sprintf(p + len, "status:\t\tnot supported\n");
3036 else if (!docked)
3037 len += sprintf(p + len, "status:\t\tundocked\n");
3038 else {
3039 len += sprintf(p + len, "status:\t\tdocked\n");
3040 len += sprintf(p + len, "commands:\tdock, undock\n");
3043 return len;
3046 static int dock_write(char *buf)
3048 char *cmd;
3050 if (!dock_docked())
3051 return -ENODEV;
3053 while ((cmd = next_cmd(&buf))) {
3054 if (strlencmp(cmd, "undock") == 0) {
3055 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3056 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3057 return -EIO;
3058 } else if (strlencmp(cmd, "dock") == 0) {
3059 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3060 return -EIO;
3061 } else
3062 return -EINVAL;
3065 return 0;
3068 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3070 /*************************************************************************
3071 * Bay subdriver
3074 #ifdef CONFIG_THINKPAD_ACPI_BAY
3076 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3077 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3078 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3079 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3080 ); /* A21e, R30, R31 */
3081 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3082 "_EJ0", /* all others */
3083 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3084 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3085 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3086 ); /* all others */
3087 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3088 "_EJ0", /* 770x */
3089 ); /* all others */
3091 static int __init bay_init(struct ibm_init_struct *iibm)
3093 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3095 TPACPI_ACPIHANDLE_INIT(bay);
3096 if (bay_handle)
3097 TPACPI_ACPIHANDLE_INIT(bay_ej);
3098 TPACPI_ACPIHANDLE_INIT(bay2);
3099 if (bay2_handle)
3100 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3102 tp_features.bay_status = bay_handle &&
3103 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3104 tp_features.bay_status2 = bay2_handle &&
3105 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3107 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3108 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3109 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3110 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3112 vdbg_printk(TPACPI_DBG_INIT,
3113 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3114 str_supported(tp_features.bay_status),
3115 str_supported(tp_features.bay_eject),
3116 str_supported(tp_features.bay_status2),
3117 str_supported(tp_features.bay_eject2));
3119 return (tp_features.bay_status || tp_features.bay_eject ||
3120 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3123 static void bay_notify(struct ibm_struct *ibm, u32 event)
3125 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3126 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3127 ibm->acpi->device->dev.bus_id,
3128 event, 0);
3131 #define bay_occupied(b) (_sta(b##_handle) & 1)
3133 static int bay_read(char *p)
3135 int len = 0;
3136 int occupied = bay_occupied(bay);
3137 int occupied2 = bay_occupied(bay2);
3138 int eject, eject2;
3140 len += sprintf(p + len, "status:\t\t%s\n",
3141 tp_features.bay_status ?
3142 (occupied ? "occupied" : "unoccupied") :
3143 "not supported");
3144 if (tp_features.bay_status2)
3145 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3146 "occupied" : "unoccupied");
3148 eject = tp_features.bay_eject && occupied;
3149 eject2 = tp_features.bay_eject2 && occupied2;
3151 if (eject && eject2)
3152 len += sprintf(p + len, "commands:\teject, eject2\n");
3153 else if (eject)
3154 len += sprintf(p + len, "commands:\teject\n");
3155 else if (eject2)
3156 len += sprintf(p + len, "commands:\teject2\n");
3158 return len;
3161 static int bay_write(char *buf)
3163 char *cmd;
3165 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3166 return -ENODEV;
3168 while ((cmd = next_cmd(&buf))) {
3169 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3170 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3171 return -EIO;
3172 } else if (tp_features.bay_eject2 &&
3173 strlencmp(cmd, "eject2") == 0) {
3174 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3175 return -EIO;
3176 } else
3177 return -EINVAL;
3180 return 0;
3183 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3184 .notify = bay_notify,
3185 .handle = &bay_handle,
3186 .type = ACPI_SYSTEM_NOTIFY,
3189 static struct ibm_struct bay_driver_data = {
3190 .name = "bay",
3191 .read = bay_read,
3192 .write = bay_write,
3193 .acpi = &ibm_bay_acpidriver,
3196 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3198 /*************************************************************************
3199 * CMOS subdriver
3202 /* sysfs cmos_command -------------------------------------------------- */
3203 static ssize_t cmos_command_store(struct device *dev,
3204 struct device_attribute *attr,
3205 const char *buf, size_t count)
3207 unsigned long cmos_cmd;
3208 int res;
3210 if (parse_strtoul(buf, 21, &cmos_cmd))
3211 return -EINVAL;
3213 res = issue_thinkpad_cmos_command(cmos_cmd);
3214 return (res)? res : count;
3217 static struct device_attribute dev_attr_cmos_command =
3218 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3220 /* --------------------------------------------------------------------- */
3222 static int __init cmos_init(struct ibm_init_struct *iibm)
3224 int res;
3226 vdbg_printk(TPACPI_DBG_INIT,
3227 "initializing cmos commands subdriver\n");
3229 TPACPI_ACPIHANDLE_INIT(cmos);
3231 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3232 str_supported(cmos_handle != NULL));
3234 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3235 if (res)
3236 return res;
3238 return (cmos_handle)? 0 : 1;
3241 static void cmos_exit(void)
3243 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3246 static int cmos_read(char *p)
3248 int len = 0;
3250 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3251 R30, R31, T20-22, X20-21 */
3252 if (!cmos_handle)
3253 len += sprintf(p + len, "status:\t\tnot supported\n");
3254 else {
3255 len += sprintf(p + len, "status:\t\tsupported\n");
3256 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3259 return len;
3262 static int cmos_write(char *buf)
3264 char *cmd;
3265 int cmos_cmd, res;
3267 while ((cmd = next_cmd(&buf))) {
3268 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3269 cmos_cmd >= 0 && cmos_cmd <= 21) {
3270 /* cmos_cmd set */
3271 } else
3272 return -EINVAL;
3274 res = issue_thinkpad_cmos_command(cmos_cmd);
3275 if (res)
3276 return res;
3279 return 0;
3282 static struct ibm_struct cmos_driver_data = {
3283 .name = "cmos",
3284 .read = cmos_read,
3285 .write = cmos_write,
3286 .exit = cmos_exit,
3289 /*************************************************************************
3290 * LED subdriver
3293 enum led_access_mode {
3294 TPACPI_LED_NONE = 0,
3295 TPACPI_LED_570, /* 570 */
3296 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3297 TPACPI_LED_NEW, /* all others */
3300 enum { /* For TPACPI_LED_OLD */
3301 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3302 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3303 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3306 static enum led_access_mode led_supported;
3308 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3309 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3310 "LED", /* all others */
3311 ); /* R30, R31 */
3313 static int __init led_init(struct ibm_init_struct *iibm)
3315 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3317 TPACPI_ACPIHANDLE_INIT(led);
3319 if (!led_handle)
3320 /* led not supported on R30, R31 */
3321 led_supported = TPACPI_LED_NONE;
3322 else if (strlencmp(led_path, "SLED") == 0)
3323 /* 570 */
3324 led_supported = TPACPI_LED_570;
3325 else if (strlencmp(led_path, "SYSL") == 0)
3326 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3327 led_supported = TPACPI_LED_OLD;
3328 else
3329 /* all others */
3330 led_supported = TPACPI_LED_NEW;
3332 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3333 str_supported(led_supported), led_supported);
3335 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3338 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3340 static int led_read(char *p)
3342 int len = 0;
3344 if (!led_supported) {
3345 len += sprintf(p + len, "status:\t\tnot supported\n");
3346 return len;
3348 len += sprintf(p + len, "status:\t\tsupported\n");
3350 if (led_supported == TPACPI_LED_570) {
3351 /* 570 */
3352 int i, status;
3353 for (i = 0; i < 8; i++) {
3354 if (!acpi_evalf(ec_handle,
3355 &status, "GLED", "dd", 1 << i))
3356 return -EIO;
3357 len += sprintf(p + len, "%d:\t\t%s\n",
3358 i, led_status(status));
3362 len += sprintf(p + len, "commands:\t"
3363 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3365 return len;
3368 /* off, on, blink */
3369 static const int led_sled_arg1[] = { 0, 1, 3 };
3370 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3371 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3372 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3374 static int led_write(char *buf)
3376 char *cmd;
3377 int led, ind, ret;
3379 if (!led_supported)
3380 return -ENODEV;
3382 while ((cmd = next_cmd(&buf))) {
3383 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3384 return -EINVAL;
3386 if (strstr(cmd, "off")) {
3387 ind = 0;
3388 } else if (strstr(cmd, "on")) {
3389 ind = 1;
3390 } else if (strstr(cmd, "blink")) {
3391 ind = 2;
3392 } else
3393 return -EINVAL;
3395 if (led_supported == TPACPI_LED_570) {
3396 /* 570 */
3397 led = 1 << led;
3398 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3399 led, led_sled_arg1[ind]))
3400 return -EIO;
3401 } else if (led_supported == TPACPI_LED_OLD) {
3402 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3403 led = 1 << led;
3404 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3405 if (ret >= 0)
3406 ret =
3407 ec_write(TPACPI_LED_EC_HLBL,
3408 led * led_exp_hlbl[ind]);
3409 if (ret >= 0)
3410 ret =
3411 ec_write(TPACPI_LED_EC_HLCL,
3412 led * led_exp_hlcl[ind]);
3413 if (ret < 0)
3414 return ret;
3415 } else {
3416 /* all others */
3417 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3418 led, led_led_arg1[ind]))
3419 return -EIO;
3423 return 0;
3426 static struct ibm_struct led_driver_data = {
3427 .name = "led",
3428 .read = led_read,
3429 .write = led_write,
3432 /*************************************************************************
3433 * Beep subdriver
3436 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3438 static int __init beep_init(struct ibm_init_struct *iibm)
3440 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3442 TPACPI_ACPIHANDLE_INIT(beep);
3444 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3445 str_supported(beep_handle != NULL));
3447 return (beep_handle)? 0 : 1;
3450 static int beep_read(char *p)
3452 int len = 0;
3454 if (!beep_handle)
3455 len += sprintf(p + len, "status:\t\tnot supported\n");
3456 else {
3457 len += sprintf(p + len, "status:\t\tsupported\n");
3458 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3461 return len;
3464 static int beep_write(char *buf)
3466 char *cmd;
3467 int beep_cmd;
3469 if (!beep_handle)
3470 return -ENODEV;
3472 while ((cmd = next_cmd(&buf))) {
3473 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3474 beep_cmd >= 0 && beep_cmd <= 17) {
3475 /* beep_cmd set */
3476 } else
3477 return -EINVAL;
3478 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3479 return -EIO;
3482 return 0;
3485 static struct ibm_struct beep_driver_data = {
3486 .name = "beep",
3487 .read = beep_read,
3488 .write = beep_write,
3491 /*************************************************************************
3492 * Thermal subdriver
3495 enum thermal_access_mode {
3496 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3497 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3498 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3499 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3500 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3503 enum { /* TPACPI_THERMAL_TPEC_* */
3504 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3505 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3506 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3509 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3510 struct ibm_thermal_sensors_struct {
3511 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3514 static enum thermal_access_mode thermal_read_mode;
3516 /* idx is zero-based */
3517 static int thermal_get_sensor(int idx, s32 *value)
3519 int t;
3520 s8 tmp;
3521 char tmpi[5];
3523 t = TP_EC_THERMAL_TMP0;
3525 switch (thermal_read_mode) {
3526 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3527 case TPACPI_THERMAL_TPEC_16:
3528 if (idx >= 8 && idx <= 15) {
3529 t = TP_EC_THERMAL_TMP8;
3530 idx -= 8;
3532 /* fallthrough */
3533 #endif
3534 case TPACPI_THERMAL_TPEC_8:
3535 if (idx <= 7) {
3536 if (!acpi_ec_read(t + idx, &tmp))
3537 return -EIO;
3538 *value = tmp * 1000;
3539 return 0;
3541 break;
3543 case TPACPI_THERMAL_ACPI_UPDT:
3544 if (idx <= 7) {
3545 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3546 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3547 return -EIO;
3548 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3549 return -EIO;
3550 *value = (t - 2732) * 100;
3551 return 0;
3553 break;
3555 case TPACPI_THERMAL_ACPI_TMP07:
3556 if (idx <= 7) {
3557 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3558 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3559 return -EIO;
3560 if (t > 127 || t < -127)
3561 t = TP_EC_THERMAL_TMP_NA;
3562 *value = t * 1000;
3563 return 0;
3565 break;
3567 case TPACPI_THERMAL_NONE:
3568 default:
3569 return -ENOSYS;
3572 return -EINVAL;
3575 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3577 int res, i;
3578 int n;
3580 n = 8;
3581 i = 0;
3583 if (!s)
3584 return -EINVAL;
3586 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3587 n = 16;
3589 for(i = 0 ; i < n; i++) {
3590 res = thermal_get_sensor(i, &s->temp[i]);
3591 if (res)
3592 return res;
3595 return n;
3598 /* sysfs temp##_input -------------------------------------------------- */
3600 static ssize_t thermal_temp_input_show(struct device *dev,
3601 struct device_attribute *attr,
3602 char *buf)
3604 struct sensor_device_attribute *sensor_attr =
3605 to_sensor_dev_attr(attr);
3606 int idx = sensor_attr->index;
3607 s32 value;
3608 int res;
3610 res = thermal_get_sensor(idx, &value);
3611 if (res)
3612 return res;
3613 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3614 return -ENXIO;
3616 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3619 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3620 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3622 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3623 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3624 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3625 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3626 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3627 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3628 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3629 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3630 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3631 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3632 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3633 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3634 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3635 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3636 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3637 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3638 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3641 #define THERMAL_ATTRS(X) \
3642 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3644 static struct attribute *thermal_temp_input_attr[] = {
3645 THERMAL_ATTRS(8),
3646 THERMAL_ATTRS(9),
3647 THERMAL_ATTRS(10),
3648 THERMAL_ATTRS(11),
3649 THERMAL_ATTRS(12),
3650 THERMAL_ATTRS(13),
3651 THERMAL_ATTRS(14),
3652 THERMAL_ATTRS(15),
3653 THERMAL_ATTRS(0),
3654 THERMAL_ATTRS(1),
3655 THERMAL_ATTRS(2),
3656 THERMAL_ATTRS(3),
3657 THERMAL_ATTRS(4),
3658 THERMAL_ATTRS(5),
3659 THERMAL_ATTRS(6),
3660 THERMAL_ATTRS(7),
3661 NULL
3664 static const struct attribute_group thermal_temp_input16_group = {
3665 .attrs = thermal_temp_input_attr
3668 static const struct attribute_group thermal_temp_input8_group = {
3669 .attrs = &thermal_temp_input_attr[8]
3672 #undef THERMAL_SENSOR_ATTR_TEMP
3673 #undef THERMAL_ATTRS
3675 /* --------------------------------------------------------------------- */
3677 static int __init thermal_init(struct ibm_init_struct *iibm)
3679 u8 t, ta1, ta2;
3680 int i;
3681 int acpi_tmp7;
3682 int res;
3684 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3686 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3688 if (thinkpad_id.ec_model) {
3690 * Direct EC access mode: sensors at registers
3691 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3692 * non-implemented, thermal sensors return 0x80 when
3693 * not available
3696 ta1 = ta2 = 0;
3697 for (i = 0; i < 8; i++) {
3698 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3699 ta1 |= t;
3700 } else {
3701 ta1 = 0;
3702 break;
3704 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3705 ta2 |= t;
3706 } else {
3707 ta1 = 0;
3708 break;
3711 if (ta1 == 0) {
3712 /* This is sheer paranoia, but we handle it anyway */
3713 if (acpi_tmp7) {
3714 printk(TPACPI_ERR
3715 "ThinkPad ACPI EC access misbehaving, "
3716 "falling back to ACPI TMPx access mode\n");
3717 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3718 } else {
3719 printk(TPACPI_ERR
3720 "ThinkPad ACPI EC access misbehaving, "
3721 "disabling thermal sensors access\n");
3722 thermal_read_mode = TPACPI_THERMAL_NONE;
3724 } else {
3725 thermal_read_mode =
3726 (ta2 != 0) ?
3727 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3729 } else if (acpi_tmp7) {
3730 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3731 /* 600e/x, 770e, 770x */
3732 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3733 } else {
3734 /* Standard ACPI TMPx access, max 8 sensors */
3735 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3737 } else {
3738 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3739 thermal_read_mode = TPACPI_THERMAL_NONE;
3742 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3743 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3744 thermal_read_mode);
3746 switch(thermal_read_mode) {
3747 case TPACPI_THERMAL_TPEC_16:
3748 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3749 &thermal_temp_input16_group);
3750 if (res)
3751 return res;
3752 break;
3753 case TPACPI_THERMAL_TPEC_8:
3754 case TPACPI_THERMAL_ACPI_TMP07:
3755 case TPACPI_THERMAL_ACPI_UPDT:
3756 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3757 &thermal_temp_input8_group);
3758 if (res)
3759 return res;
3760 break;
3761 case TPACPI_THERMAL_NONE:
3762 default:
3763 return 1;
3766 return 0;
3769 static void thermal_exit(void)
3771 switch(thermal_read_mode) {
3772 case TPACPI_THERMAL_TPEC_16:
3773 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3774 &thermal_temp_input16_group);
3775 break;
3776 case TPACPI_THERMAL_TPEC_8:
3777 case TPACPI_THERMAL_ACPI_TMP07:
3778 case TPACPI_THERMAL_ACPI_UPDT:
3779 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3780 &thermal_temp_input16_group);
3781 break;
3782 case TPACPI_THERMAL_NONE:
3783 default:
3784 break;
3788 static int thermal_read(char *p)
3790 int len = 0;
3791 int n, i;
3792 struct ibm_thermal_sensors_struct t;
3794 n = thermal_get_sensors(&t);
3795 if (unlikely(n < 0))
3796 return n;
3798 len += sprintf(p + len, "temperatures:\t");
3800 if (n > 0) {
3801 for (i = 0; i < (n - 1); i++)
3802 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3803 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3804 } else
3805 len += sprintf(p + len, "not supported\n");
3807 return len;
3810 static struct ibm_struct thermal_driver_data = {
3811 .name = "thermal",
3812 .read = thermal_read,
3813 .exit = thermal_exit,
3816 /*************************************************************************
3817 * EC Dump subdriver
3820 static u8 ecdump_regs[256];
3822 static int ecdump_read(char *p)
3824 int len = 0;
3825 int i, j;
3826 u8 v;
3828 len += sprintf(p + len, "EC "
3829 " +00 +01 +02 +03 +04 +05 +06 +07"
3830 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3831 for (i = 0; i < 256; i += 16) {
3832 len += sprintf(p + len, "EC 0x%02x:", i);
3833 for (j = 0; j < 16; j++) {
3834 if (!acpi_ec_read(i + j, &v))
3835 break;
3836 if (v != ecdump_regs[i + j])
3837 len += sprintf(p + len, " *%02x", v);
3838 else
3839 len += sprintf(p + len, " %02x", v);
3840 ecdump_regs[i + j] = v;
3842 len += sprintf(p + len, "\n");
3843 if (j != 16)
3844 break;
3847 /* These are way too dangerous to advertise openly... */
3848 #if 0
3849 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3850 " (<offset> is 00-ff, <value> is 00-ff)\n");
3851 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3852 " (<offset> is 00-ff, <value> is 0-255)\n");
3853 #endif
3854 return len;
3857 static int ecdump_write(char *buf)
3859 char *cmd;
3860 int i, v;
3862 while ((cmd = next_cmd(&buf))) {
3863 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3864 /* i and v set */
3865 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3866 /* i and v set */
3867 } else
3868 return -EINVAL;
3869 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3870 if (!acpi_ec_write(i, v))
3871 return -EIO;
3872 } else
3873 return -EINVAL;
3876 return 0;
3879 static struct ibm_struct ecdump_driver_data = {
3880 .name = "ecdump",
3881 .read = ecdump_read,
3882 .write = ecdump_write,
3883 .flags.experimental = 1,
3886 /*************************************************************************
3887 * Backlight/brightness subdriver
3890 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3892 static struct backlight_device *ibm_backlight_device;
3893 static int brightness_offset = 0x31;
3894 static int brightness_mode;
3895 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3897 static struct mutex brightness_mutex;
3900 * ThinkPads can read brightness from two places: EC 0x31, or
3901 * CMOS NVRAM byte 0x5E, bits 0-3.
3903 static int brightness_get(struct backlight_device *bd)
3905 u8 lec = 0, lcmos = 0, level = 0;
3907 if (brightness_mode & 1) {
3908 if (!acpi_ec_read(brightness_offset, &lec))
3909 return -EIO;
3910 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3911 level = lec;
3913 if (brightness_mode & 2) {
3914 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3915 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3916 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3917 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3918 level = lcmos;
3921 if (brightness_mode == 3 && lec != lcmos) {
3922 printk(TPACPI_ERR
3923 "CMOS NVRAM (%u) and EC (%u) do not agree "
3924 "on display brightness level\n",
3925 (unsigned int) lcmos,
3926 (unsigned int) lec);
3927 return -EIO;
3930 return level;
3933 /* May return EINTR which can always be mapped to ERESTARTSYS */
3934 static int brightness_set(int value)
3936 int cmos_cmd, inc, i, res;
3937 int current_value;
3939 if (value > ((tp_features.bright_16levels)? 15 : 7))
3940 return -EINVAL;
3942 res = mutex_lock_interruptible(&brightness_mutex);
3943 if (res < 0)
3944 return res;
3946 current_value = brightness_get(NULL);
3947 if (current_value < 0) {
3948 res = current_value;
3949 goto errout;
3952 cmos_cmd = value > current_value ?
3953 TP_CMOS_BRIGHTNESS_UP :
3954 TP_CMOS_BRIGHTNESS_DOWN;
3955 inc = (value > current_value)? 1 : -1;
3957 res = 0;
3958 for (i = current_value; i != value; i += inc) {
3959 if ((brightness_mode & 2) &&
3960 issue_thinkpad_cmos_command(cmos_cmd)) {
3961 res = -EIO;
3962 goto errout;
3964 if ((brightness_mode & 1) &&
3965 !acpi_ec_write(brightness_offset, i + inc)) {
3966 res = -EIO;
3967 goto errout;;
3971 errout:
3972 mutex_unlock(&brightness_mutex);
3973 return res;
3976 /* sysfs backlight class ----------------------------------------------- */
3978 static int brightness_update_status(struct backlight_device *bd)
3980 /* it is the backlight class's job (caller) to handle
3981 * EINTR and other errors properly */
3982 return brightness_set(
3983 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3984 bd->props.power == FB_BLANK_UNBLANK) ?
3985 bd->props.brightness : 0);
3988 static struct backlight_ops ibm_backlight_data = {
3989 .get_brightness = brightness_get,
3990 .update_status = brightness_update_status,
3993 /* --------------------------------------------------------------------- */
3995 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3997 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3998 union acpi_object *obj;
3999 int rc;
4001 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4002 obj = (union acpi_object *)buffer.pointer;
4003 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4004 printk(TPACPI_ERR "Unknown BCLL data, "
4005 "please report this to %s\n", TPACPI_MAIL);
4006 rc = 0;
4007 } else {
4008 rc = obj->package.count;
4010 } else {
4011 return 0;
4014 kfree(buffer.pointer);
4015 return rc;
4018 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4019 void *context, void **rv)
4021 char name[ACPI_PATH_SEGMENT_LENGTH];
4022 struct acpi_buffer buffer = { sizeof(name), &name };
4024 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4025 !strncmp("BCLL", name, sizeof(name) - 1)) {
4026 if (tpacpi_query_bcll_levels(handle) == 16) {
4027 *rv = handle;
4028 return AE_CTRL_TERMINATE;
4029 } else {
4030 return AE_OK;
4032 } else {
4033 return AE_OK;
4037 static int __init brightness_check_levels(void)
4039 int status;
4040 void *found_node = NULL;
4042 if (!vid_handle) {
4043 TPACPI_ACPIHANDLE_INIT(vid);
4045 if (!vid_handle)
4046 return 0;
4048 /* Search for a BCLL package with 16 levels */
4049 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4050 brightness_find_bcll, NULL, &found_node);
4052 return (ACPI_SUCCESS(status) && found_node != NULL);
4055 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4056 void *context, void **rv)
4058 char name[ACPI_PATH_SEGMENT_LENGTH];
4059 struct acpi_buffer buffer = { sizeof(name), &name };
4061 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4062 !strncmp("_BCL", name, sizeof(name) - 1)) {
4063 *rv = handle;
4064 return AE_CTRL_TERMINATE;
4065 } else {
4066 return AE_OK;
4070 static int __init brightness_check_std_acpi_support(void)
4072 int status;
4073 void *found_node = NULL;
4075 if (!vid_handle) {
4076 TPACPI_ACPIHANDLE_INIT(vid);
4078 if (!vid_handle)
4079 return 0;
4081 /* Search for a _BCL method, but don't execute it */
4082 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4083 brightness_find_bcl, NULL, &found_node);
4085 return (ACPI_SUCCESS(status) && found_node != NULL);
4088 static int __init brightness_init(struct ibm_init_struct *iibm)
4090 int b;
4092 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4094 mutex_init(&brightness_mutex);
4096 if (!brightness_enable) {
4097 dbg_printk(TPACPI_DBG_INIT,
4098 "brightness support disabled by module parameter\n");
4099 return 1;
4100 } else if (brightness_enable > 1) {
4101 if (brightness_check_std_acpi_support()) {
4102 printk(TPACPI_NOTICE
4103 "standard ACPI backlight interface available, not loading native one...\n");
4104 return 1;
4108 if (!brightness_mode) {
4109 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4110 brightness_mode = 2;
4111 else
4112 brightness_mode = 3;
4114 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4115 brightness_mode);
4118 if (brightness_mode > 3)
4119 return -EINVAL;
4121 tp_features.bright_16levels =
4122 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4123 brightness_check_levels();
4125 b = brightness_get(NULL);
4126 if (b < 0)
4127 return 1;
4129 if (tp_features.bright_16levels)
4130 printk(TPACPI_INFO "detected a 16-level brightness capable ThinkPad\n");
4132 ibm_backlight_device = backlight_device_register(
4133 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4134 &ibm_backlight_data);
4135 if (IS_ERR(ibm_backlight_device)) {
4136 printk(TPACPI_ERR "Could not register backlight device\n");
4137 return PTR_ERR(ibm_backlight_device);
4139 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4141 ibm_backlight_device->props.max_brightness =
4142 (tp_features.bright_16levels)? 15 : 7;
4143 ibm_backlight_device->props.brightness = b;
4144 backlight_update_status(ibm_backlight_device);
4146 return 0;
4149 static void brightness_exit(void)
4151 if (ibm_backlight_device) {
4152 vdbg_printk(TPACPI_DBG_EXIT,
4153 "calling backlight_device_unregister()\n");
4154 backlight_device_unregister(ibm_backlight_device);
4155 ibm_backlight_device = NULL;
4159 static int brightness_read(char *p)
4161 int len = 0;
4162 int level;
4164 if ((level = brightness_get(NULL)) < 0) {
4165 len += sprintf(p + len, "level:\t\tunreadable\n");
4166 } else {
4167 len += sprintf(p + len, "level:\t\t%d\n", level);
4168 len += sprintf(p + len, "commands:\tup, down\n");
4169 len += sprintf(p + len, "commands:\tlevel <level>"
4170 " (<level> is 0-%d)\n",
4171 (tp_features.bright_16levels) ? 15 : 7);
4174 return len;
4177 static int brightness_write(char *buf)
4179 int level;
4180 int rc;
4181 char *cmd;
4182 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4184 level = brightness_get(NULL);
4185 if (level < 0)
4186 return level;
4188 while ((cmd = next_cmd(&buf))) {
4189 if (strlencmp(cmd, "up") == 0) {
4190 if (level < max_level)
4191 level++;
4192 } else if (strlencmp(cmd, "down") == 0) {
4193 if (level > 0)
4194 level--;
4195 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4196 level >= 0 && level <= max_level) {
4197 /* new level set */
4198 } else
4199 return -EINVAL;
4203 * Now we know what the final level should be, so we try to set it.
4204 * Doing it this way makes the syscall restartable in case of EINTR
4206 rc = brightness_set(level);
4207 return (rc == -EINTR)? ERESTARTSYS : rc;
4210 static struct ibm_struct brightness_driver_data = {
4211 .name = "brightness",
4212 .read = brightness_read,
4213 .write = brightness_write,
4214 .exit = brightness_exit,
4217 /*************************************************************************
4218 * Volume subdriver
4221 static int volume_offset = 0x30;
4223 static int volume_read(char *p)
4225 int len = 0;
4226 u8 level;
4228 if (!acpi_ec_read(volume_offset, &level)) {
4229 len += sprintf(p + len, "level:\t\tunreadable\n");
4230 } else {
4231 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4232 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4233 len += sprintf(p + len, "commands:\tup, down, mute\n");
4234 len += sprintf(p + len, "commands:\tlevel <level>"
4235 " (<level> is 0-15)\n");
4238 return len;
4241 static int volume_write(char *buf)
4243 int cmos_cmd, inc, i;
4244 u8 level, mute;
4245 int new_level, new_mute;
4246 char *cmd;
4248 while ((cmd = next_cmd(&buf))) {
4249 if (!acpi_ec_read(volume_offset, &level))
4250 return -EIO;
4251 new_mute = mute = level & 0x40;
4252 new_level = level = level & 0xf;
4254 if (strlencmp(cmd, "up") == 0) {
4255 if (mute)
4256 new_mute = 0;
4257 else
4258 new_level = level == 15 ? 15 : level + 1;
4259 } else if (strlencmp(cmd, "down") == 0) {
4260 if (mute)
4261 new_mute = 0;
4262 else
4263 new_level = level == 0 ? 0 : level - 1;
4264 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4265 new_level >= 0 && new_level <= 15) {
4266 /* new_level set */
4267 } else if (strlencmp(cmd, "mute") == 0) {
4268 new_mute = 0x40;
4269 } else
4270 return -EINVAL;
4272 if (new_level != level) { /* mute doesn't change */
4273 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4274 inc = new_level > level ? 1 : -1;
4276 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4277 !acpi_ec_write(volume_offset, level)))
4278 return -EIO;
4280 for (i = level; i != new_level; i += inc)
4281 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4282 !acpi_ec_write(volume_offset, i + inc))
4283 return -EIO;
4285 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4286 !acpi_ec_write(volume_offset,
4287 new_level + mute)))
4288 return -EIO;
4291 if (new_mute != mute) { /* level doesn't change */
4292 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4294 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4295 !acpi_ec_write(volume_offset, level + new_mute))
4296 return -EIO;
4300 return 0;
4303 static struct ibm_struct volume_driver_data = {
4304 .name = "volume",
4305 .read = volume_read,
4306 .write = volume_write,
4309 /*************************************************************************
4310 * Fan subdriver
4314 * FAN ACCESS MODES
4316 * TPACPI_FAN_RD_ACPI_GFAN:
4317 * ACPI GFAN method: returns fan level
4319 * see TPACPI_FAN_WR_ACPI_SFAN
4320 * EC 0x2f (HFSP) not available if GFAN exists
4322 * TPACPI_FAN_WR_ACPI_SFAN:
4323 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4325 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4326 * it for writing.
4328 * TPACPI_FAN_WR_TPEC:
4329 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4330 * Supported on almost all ThinkPads
4332 * Fan speed changes of any sort (including those caused by the
4333 * disengaged mode) are usually done slowly by the firmware as the
4334 * maximum ammount of fan duty cycle change per second seems to be
4335 * limited.
4337 * Reading is not available if GFAN exists.
4338 * Writing is not available if SFAN exists.
4340 * Bits
4341 * 7 automatic mode engaged;
4342 * (default operation mode of the ThinkPad)
4343 * fan level is ignored in this mode.
4344 * 6 full speed mode (takes precedence over bit 7);
4345 * not available on all thinkpads. May disable
4346 * the tachometer while the fan controller ramps up
4347 * the speed (which can take up to a few *minutes*).
4348 * Speeds up fan to 100% duty-cycle, which is far above
4349 * the standard RPM levels. It is not impossible that
4350 * it could cause hardware damage.
4351 * 5-3 unused in some models. Extra bits for fan level
4352 * in others, but still useless as all values above
4353 * 7 map to the same speed as level 7 in these models.
4354 * 2-0 fan level (0..7 usually)
4355 * 0x00 = stop
4356 * 0x07 = max (set when temperatures critical)
4357 * Some ThinkPads may have other levels, see
4358 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4360 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4361 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4362 * does so, its initial value is meaningless (0x07).
4364 * For firmware bugs, refer to:
4365 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4367 * ----
4369 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4370 * Main fan tachometer reading (in RPM)
4372 * This register is present on all ThinkPads with a new-style EC, and
4373 * it is known not to be present on the A21m/e, and T22, as there is
4374 * something else in offset 0x84 according to the ACPI DSDT. Other
4375 * ThinkPads from this same time period (and earlier) probably lack the
4376 * tachometer as well.
4378 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4379 * was never fixed by IBM to report the EC firmware version string
4380 * probably support the tachometer (like the early X models), so
4381 * detecting it is quite hard. We need more data to know for sure.
4383 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4384 * might result.
4386 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4387 * mode.
4389 * For firmware bugs, refer to:
4390 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4392 * TPACPI_FAN_WR_ACPI_FANS:
4393 * ThinkPad X31, X40, X41. Not available in the X60.
4395 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4396 * high speed. ACPI DSDT seems to map these three speeds to levels
4397 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4398 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4400 * The speeds are stored on handles
4401 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4403 * There are three default speed sets, acessible as handles:
4404 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4406 * ACPI DSDT switches which set is in use depending on various
4407 * factors.
4409 * TPACPI_FAN_WR_TPEC is also available and should be used to
4410 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4411 * but the ACPI tables just mention level 7.
4414 enum { /* Fan control constants */
4415 fan_status_offset = 0x2f, /* EC register 0x2f */
4416 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4417 * 0x84 must be read before 0x85 */
4419 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4420 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4422 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4425 enum fan_status_access_mode {
4426 TPACPI_FAN_NONE = 0, /* No fan status or control */
4427 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4428 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4431 enum fan_control_access_mode {
4432 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4433 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4434 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4435 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4438 enum fan_control_commands {
4439 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4440 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4441 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4442 * and also watchdog cmd */
4445 static int fan_control_allowed;
4447 static enum fan_status_access_mode fan_status_access_mode;
4448 static enum fan_control_access_mode fan_control_access_mode;
4449 static enum fan_control_commands fan_control_commands;
4451 static u8 fan_control_initial_status;
4452 static u8 fan_control_desired_level;
4453 static int fan_watchdog_maxinterval;
4455 static struct mutex fan_mutex;
4457 static void fan_watchdog_fire(struct work_struct *ignored);
4458 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4460 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4461 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4462 "\\FSPD", /* 600e/x, 770e, 770x */
4463 ); /* all others */
4464 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4465 "JFNS", /* 770x-JL */
4466 ); /* all others */
4469 * Call with fan_mutex held
4471 static void fan_update_desired_level(u8 status)
4473 if ((status &
4474 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4475 if (status > 7)
4476 fan_control_desired_level = 7;
4477 else
4478 fan_control_desired_level = status;
4482 static int fan_get_status(u8 *status)
4484 u8 s;
4486 /* TODO:
4487 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4489 switch (fan_status_access_mode) {
4490 case TPACPI_FAN_RD_ACPI_GFAN:
4491 /* 570, 600e/x, 770e, 770x */
4493 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4494 return -EIO;
4496 if (likely(status))
4497 *status = s & 0x07;
4499 break;
4501 case TPACPI_FAN_RD_TPEC:
4502 /* all except 570, 600e/x, 770e, 770x */
4503 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4504 return -EIO;
4506 if (likely(status))
4507 *status = s;
4509 break;
4511 default:
4512 return -ENXIO;
4515 return 0;
4518 static int fan_get_status_safe(u8 *status)
4520 int rc;
4521 u8 s;
4523 if (mutex_lock_interruptible(&fan_mutex))
4524 return -ERESTARTSYS;
4525 rc = fan_get_status(&s);
4526 if (!rc)
4527 fan_update_desired_level(s);
4528 mutex_unlock(&fan_mutex);
4530 if (status)
4531 *status = s;
4533 return rc;
4536 static int fan_get_speed(unsigned int *speed)
4538 u8 hi, lo;
4540 switch (fan_status_access_mode) {
4541 case TPACPI_FAN_RD_TPEC:
4542 /* all except 570, 600e/x, 770e, 770x */
4543 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4544 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4545 return -EIO;
4547 if (likely(speed))
4548 *speed = (hi << 8) | lo;
4550 break;
4552 default:
4553 return -ENXIO;
4556 return 0;
4559 static int fan_set_level(int level)
4561 if (!fan_control_allowed)
4562 return -EPERM;
4564 switch (fan_control_access_mode) {
4565 case TPACPI_FAN_WR_ACPI_SFAN:
4566 if (level >= 0 && level <= 7) {
4567 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4568 return -EIO;
4569 } else
4570 return -EINVAL;
4571 break;
4573 case TPACPI_FAN_WR_ACPI_FANS:
4574 case TPACPI_FAN_WR_TPEC:
4575 if ((level != TP_EC_FAN_AUTO) &&
4576 (level != TP_EC_FAN_FULLSPEED) &&
4577 ((level < 0) || (level > 7)))
4578 return -EINVAL;
4580 /* safety net should the EC not support AUTO
4581 * or FULLSPEED mode bits and just ignore them */
4582 if (level & TP_EC_FAN_FULLSPEED)
4583 level |= 7; /* safety min speed 7 */
4584 else if (level & TP_EC_FAN_FULLSPEED)
4585 level |= 4; /* safety min speed 4 */
4587 if (!acpi_ec_write(fan_status_offset, level))
4588 return -EIO;
4589 else
4590 tp_features.fan_ctrl_status_undef = 0;
4591 break;
4593 default:
4594 return -ENXIO;
4596 return 0;
4599 static int fan_set_level_safe(int level)
4601 int rc;
4603 if (!fan_control_allowed)
4604 return -EPERM;
4606 if (mutex_lock_interruptible(&fan_mutex))
4607 return -ERESTARTSYS;
4609 if (level == TPACPI_FAN_LAST_LEVEL)
4610 level = fan_control_desired_level;
4612 rc = fan_set_level(level);
4613 if (!rc)
4614 fan_update_desired_level(level);
4616 mutex_unlock(&fan_mutex);
4617 return rc;
4620 static int fan_set_enable(void)
4622 u8 s;
4623 int rc;
4625 if (!fan_control_allowed)
4626 return -EPERM;
4628 if (mutex_lock_interruptible(&fan_mutex))
4629 return -ERESTARTSYS;
4631 switch (fan_control_access_mode) {
4632 case TPACPI_FAN_WR_ACPI_FANS:
4633 case TPACPI_FAN_WR_TPEC:
4634 rc = fan_get_status(&s);
4635 if (rc < 0)
4636 break;
4638 /* Don't go out of emergency fan mode */
4639 if (s != 7) {
4640 s &= 0x07;
4641 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4644 if (!acpi_ec_write(fan_status_offset, s))
4645 rc = -EIO;
4646 else {
4647 tp_features.fan_ctrl_status_undef = 0;
4648 rc = 0;
4650 break;
4652 case TPACPI_FAN_WR_ACPI_SFAN:
4653 rc = fan_get_status(&s);
4654 if (rc < 0)
4655 break;
4657 s &= 0x07;
4659 /* Set fan to at least level 4 */
4660 s |= 4;
4662 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4663 rc= -EIO;
4664 else
4665 rc = 0;
4666 break;
4668 default:
4669 rc = -ENXIO;
4672 mutex_unlock(&fan_mutex);
4673 return rc;
4676 static int fan_set_disable(void)
4678 int rc;
4680 if (!fan_control_allowed)
4681 return -EPERM;
4683 if (mutex_lock_interruptible(&fan_mutex))
4684 return -ERESTARTSYS;
4686 rc = 0;
4687 switch (fan_control_access_mode) {
4688 case TPACPI_FAN_WR_ACPI_FANS:
4689 case TPACPI_FAN_WR_TPEC:
4690 if (!acpi_ec_write(fan_status_offset, 0x00))
4691 rc = -EIO;
4692 else {
4693 fan_control_desired_level = 0;
4694 tp_features.fan_ctrl_status_undef = 0;
4696 break;
4698 case TPACPI_FAN_WR_ACPI_SFAN:
4699 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4700 rc = -EIO;
4701 else
4702 fan_control_desired_level = 0;
4703 break;
4705 default:
4706 rc = -ENXIO;
4710 mutex_unlock(&fan_mutex);
4711 return rc;
4714 static int fan_set_speed(int speed)
4716 int rc;
4718 if (!fan_control_allowed)
4719 return -EPERM;
4721 if (mutex_lock_interruptible(&fan_mutex))
4722 return -ERESTARTSYS;
4724 rc = 0;
4725 switch (fan_control_access_mode) {
4726 case TPACPI_FAN_WR_ACPI_FANS:
4727 if (speed >= 0 && speed <= 65535) {
4728 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4729 speed, speed, speed))
4730 rc = -EIO;
4731 } else
4732 rc = -EINVAL;
4733 break;
4735 default:
4736 rc = -ENXIO;
4739 mutex_unlock(&fan_mutex);
4740 return rc;
4743 static void fan_watchdog_reset(void)
4745 static int fan_watchdog_active;
4747 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4748 return;
4750 if (fan_watchdog_active)
4751 cancel_delayed_work(&fan_watchdog_task);
4753 if (fan_watchdog_maxinterval > 0 &&
4754 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4755 fan_watchdog_active = 1;
4756 if (!schedule_delayed_work(&fan_watchdog_task,
4757 msecs_to_jiffies(fan_watchdog_maxinterval
4758 * 1000))) {
4759 printk(TPACPI_ERR "failed to schedule the fan watchdog, "
4760 "watchdog will not trigger\n");
4762 } else
4763 fan_watchdog_active = 0;
4766 static void fan_watchdog_fire(struct work_struct *ignored)
4768 int rc;
4770 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4771 return;
4773 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4774 rc = fan_set_enable();
4775 if (rc < 0) {
4776 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4777 "will try again later...\n", -rc);
4778 /* reschedule for later */
4779 fan_watchdog_reset();
4784 * SYSFS fan layout: hwmon compatible (device)
4786 * pwm*_enable:
4787 * 0: "disengaged" mode
4788 * 1: manual mode
4789 * 2: native EC "auto" mode (recommended, hardware default)
4791 * pwm*: set speed in manual mode, ignored otherwise.
4792 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4793 * interpolation.
4795 * fan*_input: tachometer reading, RPM
4798 * SYSFS fan layout: extensions
4800 * fan_watchdog (driver):
4801 * fan watchdog interval in seconds, 0 disables (default), max 120
4804 /* sysfs fan pwm1_enable ----------------------------------------------- */
4805 static ssize_t fan_pwm1_enable_show(struct device *dev,
4806 struct device_attribute *attr,
4807 char *buf)
4809 int res, mode;
4810 u8 status;
4812 res = fan_get_status_safe(&status);
4813 if (res)
4814 return res;
4816 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4817 if (status != fan_control_initial_status) {
4818 tp_features.fan_ctrl_status_undef = 0;
4819 } else {
4820 /* Return most likely status. In fact, it
4821 * might be the only possible status */
4822 status = TP_EC_FAN_AUTO;
4826 if (status & TP_EC_FAN_FULLSPEED) {
4827 mode = 0;
4828 } else if (status & TP_EC_FAN_AUTO) {
4829 mode = 2;
4830 } else
4831 mode = 1;
4833 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4836 static ssize_t fan_pwm1_enable_store(struct device *dev,
4837 struct device_attribute *attr,
4838 const char *buf, size_t count)
4840 unsigned long t;
4841 int res, level;
4843 if (parse_strtoul(buf, 2, &t))
4844 return -EINVAL;
4846 switch (t) {
4847 case 0:
4848 level = TP_EC_FAN_FULLSPEED;
4849 break;
4850 case 1:
4851 level = TPACPI_FAN_LAST_LEVEL;
4852 break;
4853 case 2:
4854 level = TP_EC_FAN_AUTO;
4855 break;
4856 case 3:
4857 /* reserved for software-controlled auto mode */
4858 return -ENOSYS;
4859 default:
4860 return -EINVAL;
4863 res = fan_set_level_safe(level);
4864 if (res == -ENXIO)
4865 return -EINVAL;
4866 else if (res < 0)
4867 return res;
4869 fan_watchdog_reset();
4871 return count;
4874 static struct device_attribute dev_attr_fan_pwm1_enable =
4875 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4876 fan_pwm1_enable_show, fan_pwm1_enable_store);
4878 /* sysfs fan pwm1 ------------------------------------------------------ */
4879 static ssize_t fan_pwm1_show(struct device *dev,
4880 struct device_attribute *attr,
4881 char *buf)
4883 int res;
4884 u8 status;
4886 res = fan_get_status_safe(&status);
4887 if (res)
4888 return res;
4890 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4891 if (status != fan_control_initial_status) {
4892 tp_features.fan_ctrl_status_undef = 0;
4893 } else {
4894 status = TP_EC_FAN_AUTO;
4898 if ((status &
4899 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4900 status = fan_control_desired_level;
4902 if (status > 7)
4903 status = 7;
4905 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4908 static ssize_t fan_pwm1_store(struct device *dev,
4909 struct device_attribute *attr,
4910 const char *buf, size_t count)
4912 unsigned long s;
4913 int rc;
4914 u8 status, newlevel;
4916 if (parse_strtoul(buf, 255, &s))
4917 return -EINVAL;
4919 /* scale down from 0-255 to 0-7 */
4920 newlevel = (s >> 5) & 0x07;
4922 if (mutex_lock_interruptible(&fan_mutex))
4923 return -ERESTARTSYS;
4925 rc = fan_get_status(&status);
4926 if (!rc && (status &
4927 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4928 rc = fan_set_level(newlevel);
4929 if (rc == -ENXIO)
4930 rc = -EINVAL;
4931 else if (!rc) {
4932 fan_update_desired_level(newlevel);
4933 fan_watchdog_reset();
4937 mutex_unlock(&fan_mutex);
4938 return (rc)? rc : count;
4941 static struct device_attribute dev_attr_fan_pwm1 =
4942 __ATTR(pwm1, S_IWUSR | S_IRUGO,
4943 fan_pwm1_show, fan_pwm1_store);
4945 /* sysfs fan fan1_input ------------------------------------------------ */
4946 static ssize_t fan_fan1_input_show(struct device *dev,
4947 struct device_attribute *attr,
4948 char *buf)
4950 int res;
4951 unsigned int speed;
4953 res = fan_get_speed(&speed);
4954 if (res < 0)
4955 return res;
4957 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
4960 static struct device_attribute dev_attr_fan_fan1_input =
4961 __ATTR(fan1_input, S_IRUGO,
4962 fan_fan1_input_show, NULL);
4964 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
4965 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
4966 char *buf)
4968 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
4971 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
4972 const char *buf, size_t count)
4974 unsigned long t;
4976 if (parse_strtoul(buf, 120, &t))
4977 return -EINVAL;
4979 if (!fan_control_allowed)
4980 return -EPERM;
4982 fan_watchdog_maxinterval = t;
4983 fan_watchdog_reset();
4985 return count;
4988 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
4989 fan_fan_watchdog_show, fan_fan_watchdog_store);
4991 /* --------------------------------------------------------------------- */
4992 static struct attribute *fan_attributes[] = {
4993 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
4994 &dev_attr_fan_fan1_input.attr,
4995 NULL
4998 static const struct attribute_group fan_attr_group = {
4999 .attrs = fan_attributes,
5002 static int __init fan_init(struct ibm_init_struct *iibm)
5004 int rc;
5006 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5008 mutex_init(&fan_mutex);
5009 fan_status_access_mode = TPACPI_FAN_NONE;
5010 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5011 fan_control_commands = 0;
5012 fan_watchdog_maxinterval = 0;
5013 tp_features.fan_ctrl_status_undef = 0;
5014 fan_control_desired_level = 7;
5016 TPACPI_ACPIHANDLE_INIT(fans);
5017 TPACPI_ACPIHANDLE_INIT(gfan);
5018 TPACPI_ACPIHANDLE_INIT(sfan);
5020 if (gfan_handle) {
5021 /* 570, 600e/x, 770e, 770x */
5022 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5023 } else {
5024 /* all other ThinkPads: note that even old-style
5025 * ThinkPad ECs supports the fan control register */
5026 if (likely(acpi_ec_read(fan_status_offset,
5027 &fan_control_initial_status))) {
5028 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5030 /* In some ThinkPads, neither the EC nor the ACPI
5031 * DSDT initialize the fan status, and it ends up
5032 * being set to 0x07 when it *could* be either
5033 * 0x07 or 0x80.
5035 * Enable for TP-1Y (T43), TP-78 (R51e),
5036 * TP-76 (R52), TP-70 (T43, R52), which are known
5037 * to be buggy. */
5038 if (fan_control_initial_status == 0x07) {
5039 switch (thinkpad_id.ec_model) {
5040 case 0x5931: /* TP-1Y */
5041 case 0x3837: /* TP-78 */
5042 case 0x3637: /* TP-76 */
5043 case 0x3037: /* TP-70 */
5044 printk(TPACPI_NOTICE
5045 "fan_init: initial fan status is "
5046 "unknown, assuming it is in auto "
5047 "mode\n");
5048 tp_features.fan_ctrl_status_undef = 1;
5052 } else {
5053 printk(TPACPI_ERR
5054 "ThinkPad ACPI EC access misbehaving, "
5055 "fan status and control unavailable\n");
5056 return 1;
5060 if (sfan_handle) {
5061 /* 570, 770x-JL */
5062 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5063 fan_control_commands |=
5064 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5065 } else {
5066 if (!gfan_handle) {
5067 /* gfan without sfan means no fan control */
5068 /* all other models implement TP EC 0x2f control */
5070 if (fans_handle) {
5071 /* X31, X40, X41 */
5072 fan_control_access_mode =
5073 TPACPI_FAN_WR_ACPI_FANS;
5074 fan_control_commands |=
5075 TPACPI_FAN_CMD_SPEED |
5076 TPACPI_FAN_CMD_LEVEL |
5077 TPACPI_FAN_CMD_ENABLE;
5078 } else {
5079 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5080 fan_control_commands |=
5081 TPACPI_FAN_CMD_LEVEL |
5082 TPACPI_FAN_CMD_ENABLE;
5087 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5088 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5089 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5090 fan_status_access_mode, fan_control_access_mode);
5092 /* fan control master switch */
5093 if (!fan_control_allowed) {
5094 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5095 fan_control_commands = 0;
5096 dbg_printk(TPACPI_DBG_INIT,
5097 "fan control features disabled by parameter\n");
5100 /* update fan_control_desired_level */
5101 if (fan_status_access_mode != TPACPI_FAN_NONE)
5102 fan_get_status_safe(NULL);
5104 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5105 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5106 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5107 &fan_attr_group);
5108 if (!(rc < 0))
5109 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5110 &driver_attr_fan_watchdog);
5111 if (rc < 0)
5112 return rc;
5113 return 0;
5114 } else
5115 return 1;
5118 static void fan_exit(void)
5120 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
5122 /* FIXME: can we really do this unconditionally? */
5123 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5124 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
5126 cancel_delayed_work(&fan_watchdog_task);
5127 flush_scheduled_work();
5130 static int fan_read(char *p)
5132 int len = 0;
5133 int rc;
5134 u8 status;
5135 unsigned int speed = 0;
5137 switch (fan_status_access_mode) {
5138 case TPACPI_FAN_RD_ACPI_GFAN:
5139 /* 570, 600e/x, 770e, 770x */
5140 if ((rc = fan_get_status_safe(&status)) < 0)
5141 return rc;
5143 len += sprintf(p + len, "status:\t\t%s\n"
5144 "level:\t\t%d\n",
5145 (status != 0) ? "enabled" : "disabled", status);
5146 break;
5148 case TPACPI_FAN_RD_TPEC:
5149 /* all except 570, 600e/x, 770e, 770x */
5150 if ((rc = fan_get_status_safe(&status)) < 0)
5151 return rc;
5153 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5154 if (status != fan_control_initial_status)
5155 tp_features.fan_ctrl_status_undef = 0;
5156 else
5157 /* Return most likely status. In fact, it
5158 * might be the only possible status */
5159 status = TP_EC_FAN_AUTO;
5162 len += sprintf(p + len, "status:\t\t%s\n",
5163 (status != 0) ? "enabled" : "disabled");
5165 if ((rc = fan_get_speed(&speed)) < 0)
5166 return rc;
5168 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5170 if (status & TP_EC_FAN_FULLSPEED)
5171 /* Disengaged mode takes precedence */
5172 len += sprintf(p + len, "level:\t\tdisengaged\n");
5173 else if (status & TP_EC_FAN_AUTO)
5174 len += sprintf(p + len, "level:\t\tauto\n");
5175 else
5176 len += sprintf(p + len, "level:\t\t%d\n", status);
5177 break;
5179 case TPACPI_FAN_NONE:
5180 default:
5181 len += sprintf(p + len, "status:\t\tnot supported\n");
5184 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5185 len += sprintf(p + len, "commands:\tlevel <level>");
5187 switch (fan_control_access_mode) {
5188 case TPACPI_FAN_WR_ACPI_SFAN:
5189 len += sprintf(p + len, " (<level> is 0-7)\n");
5190 break;
5192 default:
5193 len += sprintf(p + len, " (<level> is 0-7, "
5194 "auto, disengaged, full-speed)\n");
5195 break;
5199 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5200 len += sprintf(p + len, "commands:\tenable, disable\n"
5201 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
5202 "1-120 (seconds))\n");
5204 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5205 len += sprintf(p + len, "commands:\tspeed <speed>"
5206 " (<speed> is 0-65535)\n");
5208 return len;
5211 static int fan_write_cmd_level(const char *cmd, int *rc)
5213 int level;
5215 if (strlencmp(cmd, "level auto") == 0)
5216 level = TP_EC_FAN_AUTO;
5217 else if ((strlencmp(cmd, "level disengaged") == 0) |
5218 (strlencmp(cmd, "level full-speed") == 0))
5219 level = TP_EC_FAN_FULLSPEED;
5220 else if (sscanf(cmd, "level %d", &level) != 1)
5221 return 0;
5223 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
5224 printk(TPACPI_ERR "level command accepted for unsupported "
5225 "access mode %d", fan_control_access_mode);
5227 return 1;
5230 static int fan_write_cmd_enable(const char *cmd, int *rc)
5232 if (strlencmp(cmd, "enable") != 0)
5233 return 0;
5235 if ((*rc = fan_set_enable()) == -ENXIO)
5236 printk(TPACPI_ERR "enable command accepted for unsupported "
5237 "access mode %d", fan_control_access_mode);
5239 return 1;
5242 static int fan_write_cmd_disable(const char *cmd, int *rc)
5244 if (strlencmp(cmd, "disable") != 0)
5245 return 0;
5247 if ((*rc = fan_set_disable()) == -ENXIO)
5248 printk(TPACPI_ERR "disable command accepted for unsupported "
5249 "access mode %d", fan_control_access_mode);
5251 return 1;
5254 static int fan_write_cmd_speed(const char *cmd, int *rc)
5256 int speed;
5258 /* TODO:
5259 * Support speed <low> <medium> <high> ? */
5261 if (sscanf(cmd, "speed %d", &speed) != 1)
5262 return 0;
5264 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5265 printk(TPACPI_ERR "speed command accepted for unsupported "
5266 "access mode %d", fan_control_access_mode);
5268 return 1;
5271 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5273 int interval;
5275 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5276 return 0;
5278 if (interval < 0 || interval > 120)
5279 *rc = -EINVAL;
5280 else
5281 fan_watchdog_maxinterval = interval;
5283 return 1;
5286 static int fan_write(char *buf)
5288 char *cmd;
5289 int rc = 0;
5291 while (!rc && (cmd = next_cmd(&buf))) {
5292 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5293 fan_write_cmd_level(cmd, &rc)) &&
5294 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5295 (fan_write_cmd_enable(cmd, &rc) ||
5296 fan_write_cmd_disable(cmd, &rc) ||
5297 fan_write_cmd_watchdog(cmd, &rc))) &&
5298 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5299 fan_write_cmd_speed(cmd, &rc))
5301 rc = -EINVAL;
5302 else if (!rc)
5303 fan_watchdog_reset();
5306 return rc;
5309 static struct ibm_struct fan_driver_data = {
5310 .name = "fan",
5311 .read = fan_read,
5312 .write = fan_write,
5313 .exit = fan_exit,
5316 /****************************************************************************
5317 ****************************************************************************
5319 * Infrastructure
5321 ****************************************************************************
5322 ****************************************************************************/
5324 /* sysfs name ---------------------------------------------------------- */
5325 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5326 struct device_attribute *attr,
5327 char *buf)
5329 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5332 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5333 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5335 /* --------------------------------------------------------------------- */
5337 /* /proc support */
5338 static struct proc_dir_entry *proc_dir;
5341 * Module and infrastructure proble, init and exit handling
5344 static int force_load;
5346 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5347 static const char * __init str_supported(int is_supported)
5349 static char text_unsupported[] __initdata = "not supported";
5351 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5353 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5355 static void ibm_exit(struct ibm_struct *ibm)
5357 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5359 list_del_init(&ibm->all_drivers);
5361 if (ibm->flags.acpi_notify_installed) {
5362 dbg_printk(TPACPI_DBG_EXIT,
5363 "%s: acpi_remove_notify_handler\n", ibm->name);
5364 BUG_ON(!ibm->acpi);
5365 acpi_remove_notify_handler(*ibm->acpi->handle,
5366 ibm->acpi->type,
5367 dispatch_acpi_notify);
5368 ibm->flags.acpi_notify_installed = 0;
5369 ibm->flags.acpi_notify_installed = 0;
5372 if (ibm->flags.proc_created) {
5373 dbg_printk(TPACPI_DBG_EXIT,
5374 "%s: remove_proc_entry\n", ibm->name);
5375 remove_proc_entry(ibm->name, proc_dir);
5376 ibm->flags.proc_created = 0;
5379 if (ibm->flags.acpi_driver_registered) {
5380 dbg_printk(TPACPI_DBG_EXIT,
5381 "%s: acpi_bus_unregister_driver\n", ibm->name);
5382 BUG_ON(!ibm->acpi);
5383 acpi_bus_unregister_driver(ibm->acpi->driver);
5384 kfree(ibm->acpi->driver);
5385 ibm->acpi->driver = NULL;
5386 ibm->flags.acpi_driver_registered = 0;
5389 if (ibm->flags.init_called && ibm->exit) {
5390 ibm->exit();
5391 ibm->flags.init_called = 0;
5394 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5397 static int __init ibm_init(struct ibm_init_struct *iibm)
5399 int ret;
5400 struct ibm_struct *ibm = iibm->data;
5401 struct proc_dir_entry *entry;
5403 BUG_ON(ibm == NULL);
5405 INIT_LIST_HEAD(&ibm->all_drivers);
5407 if (ibm->flags.experimental && !experimental)
5408 return 0;
5410 dbg_printk(TPACPI_DBG_INIT,
5411 "probing for %s\n", ibm->name);
5413 if (iibm->init) {
5414 ret = iibm->init(iibm);
5415 if (ret > 0)
5416 return 0; /* probe failed */
5417 if (ret)
5418 return ret;
5420 ibm->flags.init_called = 1;
5423 if (ibm->acpi) {
5424 if (ibm->acpi->hid) {
5425 ret = register_tpacpi_subdriver(ibm);
5426 if (ret)
5427 goto err_out;
5430 if (ibm->acpi->notify) {
5431 ret = setup_acpi_notify(ibm);
5432 if (ret == -ENODEV) {
5433 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5434 ibm->name);
5435 ret = 0;
5436 goto err_out;
5438 if (ret < 0)
5439 goto err_out;
5443 dbg_printk(TPACPI_DBG_INIT,
5444 "%s installed\n", ibm->name);
5446 if (ibm->read) {
5447 entry = create_proc_entry(ibm->name,
5448 S_IFREG | S_IRUGO | S_IWUSR,
5449 proc_dir);
5450 if (!entry) {
5451 printk(TPACPI_ERR "unable to create proc entry %s\n",
5452 ibm->name);
5453 ret = -ENODEV;
5454 goto err_out;
5456 entry->owner = THIS_MODULE;
5457 entry->data = ibm;
5458 entry->read_proc = &dispatch_procfs_read;
5459 if (ibm->write)
5460 entry->write_proc = &dispatch_procfs_write;
5461 ibm->flags.proc_created = 1;
5464 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5466 return 0;
5468 err_out:
5469 dbg_printk(TPACPI_DBG_INIT,
5470 "%s: at error exit path with result %d\n",
5471 ibm->name, ret);
5473 ibm_exit(ibm);
5474 return (ret < 0)? ret : 0;
5477 /* Probing */
5479 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5481 const struct dmi_device *dev = NULL;
5482 char ec_fw_string[18];
5484 if (!tp)
5485 return;
5487 memset(tp, 0, sizeof(*tp));
5489 if (dmi_name_in_vendors("IBM"))
5490 tp->vendor = PCI_VENDOR_ID_IBM;
5491 else if (dmi_name_in_vendors("LENOVO"))
5492 tp->vendor = PCI_VENDOR_ID_LENOVO;
5493 else
5494 return;
5496 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5497 GFP_KERNEL);
5498 if (!tp->bios_version_str)
5499 return;
5500 tp->bios_model = tp->bios_version_str[0]
5501 | (tp->bios_version_str[1] << 8);
5504 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5505 * X32 or newer, all Z series; Some models must have an
5506 * up-to-date BIOS or they will not be detected.
5508 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5510 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5511 if (sscanf(dev->name,
5512 "IBM ThinkPad Embedded Controller -[%17c",
5513 ec_fw_string) == 1) {
5514 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5515 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5517 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5518 tp->ec_model = ec_fw_string[0]
5519 | (ec_fw_string[1] << 8);
5520 break;
5524 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5525 GFP_KERNEL);
5526 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5527 kfree(tp->model_str);
5528 tp->model_str = NULL;
5532 static int __init probe_for_thinkpad(void)
5534 int is_thinkpad;
5536 if (acpi_disabled)
5537 return -ENODEV;
5540 * Non-ancient models have better DMI tagging, but very old models
5541 * don't.
5543 is_thinkpad = (thinkpad_id.model_str != NULL);
5545 /* ec is required because many other handles are relative to it */
5546 TPACPI_ACPIHANDLE_INIT(ec);
5547 if (!ec_handle) {
5548 if (is_thinkpad)
5549 printk(TPACPI_ERR
5550 "Not yet supported ThinkPad detected!\n");
5551 return -ENODEV;
5555 * Risks a regression on very old machines, but reduces potential
5556 * false positives a damn great deal
5558 if (!is_thinkpad)
5559 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5561 if (!is_thinkpad && !force_load)
5562 return -ENODEV;
5564 return 0;
5568 /* Module init, exit, parameters */
5570 static struct ibm_init_struct ibms_init[] __initdata = {
5572 .init = thinkpad_acpi_driver_init,
5573 .data = &thinkpad_acpi_driver_data,
5576 .init = hotkey_init,
5577 .data = &hotkey_driver_data,
5580 .init = bluetooth_init,
5581 .data = &bluetooth_driver_data,
5584 .init = wan_init,
5585 .data = &wan_driver_data,
5588 .init = video_init,
5589 .data = &video_driver_data,
5592 .init = light_init,
5593 .data = &light_driver_data,
5595 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5597 .init = dock_init,
5598 .data = &dock_driver_data[0],
5601 .init = dock_init2,
5602 .data = &dock_driver_data[1],
5604 #endif
5605 #ifdef CONFIG_THINKPAD_ACPI_BAY
5607 .init = bay_init,
5608 .data = &bay_driver_data,
5610 #endif
5612 .init = cmos_init,
5613 .data = &cmos_driver_data,
5616 .init = led_init,
5617 .data = &led_driver_data,
5620 .init = beep_init,
5621 .data = &beep_driver_data,
5624 .init = thermal_init,
5625 .data = &thermal_driver_data,
5628 .data = &ecdump_driver_data,
5631 .init = brightness_init,
5632 .data = &brightness_driver_data,
5635 .data = &volume_driver_data,
5638 .init = fan_init,
5639 .data = &fan_driver_data,
5643 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5645 unsigned int i;
5646 struct ibm_struct *ibm;
5648 if (!kp || !kp->name || !val)
5649 return -EINVAL;
5651 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5652 ibm = ibms_init[i].data;
5653 WARN_ON(ibm == NULL);
5655 if (!ibm || !ibm->name)
5656 continue;
5658 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5659 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5660 return -ENOSPC;
5661 strcpy(ibms_init[i].param, val);
5662 strcat(ibms_init[i].param, ",");
5663 return 0;
5667 return -EINVAL;
5670 module_param(experimental, int, 0);
5671 MODULE_PARM_DESC(experimental,
5672 "Enables experimental features when non-zero");
5674 module_param_named(debug, dbg_level, uint, 0);
5675 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5677 module_param(force_load, bool, 0);
5678 MODULE_PARM_DESC(force_load,
5679 "Attempts to load the driver even on a "
5680 "mis-identified ThinkPad when true");
5682 module_param_named(fan_control, fan_control_allowed, bool, 0);
5683 MODULE_PARM_DESC(fan_control,
5684 "Enables setting fan parameters features when true");
5686 module_param_named(brightness_mode, brightness_mode, int, 0);
5687 MODULE_PARM_DESC(brightness_mode,
5688 "Selects brightness control strategy: "
5689 "0=auto, 1=EC, 2=CMOS, 3=both");
5691 module_param(brightness_enable, uint, 0);
5692 MODULE_PARM_DESC(brightness_enable,
5693 "Enables backlight control when 1, disables when 0");
5695 module_param(hotkey_report_mode, uint, 0);
5696 MODULE_PARM_DESC(hotkey_report_mode,
5697 "used for backwards compatibility with userspace, "
5698 "see documentation");
5700 #define TPACPI_PARAM(feature) \
5701 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5702 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5703 "at module load, see documentation")
5705 TPACPI_PARAM(hotkey);
5706 TPACPI_PARAM(bluetooth);
5707 TPACPI_PARAM(video);
5708 TPACPI_PARAM(light);
5709 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5710 TPACPI_PARAM(dock);
5711 #endif
5712 #ifdef CONFIG_THINKPAD_ACPI_BAY
5713 TPACPI_PARAM(bay);
5714 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5715 TPACPI_PARAM(cmos);
5716 TPACPI_PARAM(led);
5717 TPACPI_PARAM(beep);
5718 TPACPI_PARAM(ecdump);
5719 TPACPI_PARAM(brightness);
5720 TPACPI_PARAM(volume);
5721 TPACPI_PARAM(fan);
5723 static void thinkpad_acpi_module_exit(void)
5725 struct ibm_struct *ibm, *itmp;
5727 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5729 list_for_each_entry_safe_reverse(ibm, itmp,
5730 &tpacpi_all_drivers,
5731 all_drivers) {
5732 ibm_exit(ibm);
5735 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5737 if (tpacpi_inputdev) {
5738 if (tp_features.input_device_registered)
5739 input_unregister_device(tpacpi_inputdev);
5740 else
5741 input_free_device(tpacpi_inputdev);
5744 if (tpacpi_hwmon)
5745 hwmon_device_unregister(tpacpi_hwmon);
5747 if (tp_features.sensors_pdev_attrs_registered)
5748 device_remove_file(&tpacpi_sensors_pdev->dev,
5749 &dev_attr_thinkpad_acpi_pdev_name);
5750 if (tpacpi_sensors_pdev)
5751 platform_device_unregister(tpacpi_sensors_pdev);
5752 if (tpacpi_pdev)
5753 platform_device_unregister(tpacpi_pdev);
5755 if (tp_features.sensors_pdrv_attrs_registered)
5756 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5757 if (tp_features.platform_drv_attrs_registered)
5758 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5760 if (tp_features.sensors_pdrv_registered)
5761 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5763 if (tp_features.platform_drv_registered)
5764 platform_driver_unregister(&tpacpi_pdriver);
5766 if (proc_dir)
5767 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5769 kfree(thinkpad_id.bios_version_str);
5770 kfree(thinkpad_id.ec_version_str);
5771 kfree(thinkpad_id.model_str);
5775 static int __init thinkpad_acpi_module_init(void)
5777 int ret, i;
5779 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5781 /* Parameter checking */
5782 if (hotkey_report_mode > 2)
5783 return -EINVAL;
5785 /* Driver-level probe */
5787 get_thinkpad_model_data(&thinkpad_id);
5788 ret = probe_for_thinkpad();
5789 if (ret) {
5790 thinkpad_acpi_module_exit();
5791 return ret;
5794 /* Driver initialization */
5796 TPACPI_ACPIHANDLE_INIT(ecrd);
5797 TPACPI_ACPIHANDLE_INIT(ecwr);
5799 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5800 if (!proc_dir) {
5801 printk(TPACPI_ERR "unable to create proc dir " TPACPI_PROC_DIR);
5802 thinkpad_acpi_module_exit();
5803 return -ENODEV;
5805 proc_dir->owner = THIS_MODULE;
5807 ret = platform_driver_register(&tpacpi_pdriver);
5808 if (ret) {
5809 printk(TPACPI_ERR "unable to register main platform driver\n");
5810 thinkpad_acpi_module_exit();
5811 return ret;
5813 tp_features.platform_drv_registered = 1;
5815 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5816 if (ret) {
5817 printk(TPACPI_ERR "unable to register hwmon platform driver\n");
5818 thinkpad_acpi_module_exit();
5819 return ret;
5821 tp_features.sensors_pdrv_registered = 1;
5823 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5824 if (!ret) {
5825 tp_features.platform_drv_attrs_registered = 1;
5826 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5828 if (ret) {
5829 printk(TPACPI_ERR "unable to create sysfs driver attributes\n");
5830 thinkpad_acpi_module_exit();
5831 return ret;
5833 tp_features.sensors_pdrv_attrs_registered = 1;
5836 /* Device initialization */
5837 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
5838 NULL, 0);
5839 if (IS_ERR(tpacpi_pdev)) {
5840 ret = PTR_ERR(tpacpi_pdev);
5841 tpacpi_pdev = NULL;
5842 printk(TPACPI_ERR "unable to register platform device\n");
5843 thinkpad_acpi_module_exit();
5844 return ret;
5846 tpacpi_sensors_pdev = platform_device_register_simple(
5847 TPACPI_HWMON_DRVR_NAME,
5848 -1, NULL, 0);
5849 if (IS_ERR(tpacpi_sensors_pdev)) {
5850 ret = PTR_ERR(tpacpi_sensors_pdev);
5851 tpacpi_sensors_pdev = NULL;
5852 printk(TPACPI_ERR "unable to register hwmon platform device\n");
5853 thinkpad_acpi_module_exit();
5854 return ret;
5856 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5857 &dev_attr_thinkpad_acpi_pdev_name);
5858 if (ret) {
5859 printk(TPACPI_ERR
5860 "unable to create sysfs hwmon device attributes\n");
5861 thinkpad_acpi_module_exit();
5862 return ret;
5864 tp_features.sensors_pdev_attrs_registered = 1;
5865 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5866 if (IS_ERR(tpacpi_hwmon)) {
5867 ret = PTR_ERR(tpacpi_hwmon);
5868 tpacpi_hwmon = NULL;
5869 printk(TPACPI_ERR "unable to register hwmon device\n");
5870 thinkpad_acpi_module_exit();
5871 return ret;
5873 mutex_init(&tpacpi_inputdev_send_mutex);
5874 tpacpi_inputdev = input_allocate_device();
5875 if (!tpacpi_inputdev) {
5876 printk(TPACPI_ERR "unable to allocate input device\n");
5877 thinkpad_acpi_module_exit();
5878 return -ENOMEM;
5879 } else {
5880 /* Prepare input device, but don't register */
5881 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5882 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
5883 tpacpi_inputdev->id.bustype = BUS_HOST;
5884 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5885 thinkpad_id.vendor :
5886 PCI_VENDOR_ID_IBM;
5887 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5888 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5890 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5891 ret = ibm_init(&ibms_init[i]);
5892 if (ret >= 0 && *ibms_init[i].param)
5893 ret = ibms_init[i].data->write(ibms_init[i].param);
5894 if (ret < 0) {
5895 thinkpad_acpi_module_exit();
5896 return ret;
5899 ret = input_register_device(tpacpi_inputdev);
5900 if (ret < 0) {
5901 printk(TPACPI_ERR "unable to register input device\n");
5902 thinkpad_acpi_module_exit();
5903 return ret;
5904 } else {
5905 tp_features.input_device_registered = 1;
5908 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5909 return 0;
5912 /* Please remove this in year 2009 */
5913 MODULE_ALIAS("ibm_acpi");
5916 * DMI matching for module autoloading
5918 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5919 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
5921 * Only models listed in thinkwiki will be supported, so add yours
5922 * if it is not there yet.
5924 #define IBM_BIOS_MODULE_ALIAS(__type) \
5925 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
5927 /* Non-ancient thinkpads */
5928 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
5929 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
5931 /* Ancient thinkpad BIOSes have to be identified by
5932 * BIOS type or model number, and there are far less
5933 * BIOS types than model numbers... */
5934 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
5935 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
5936 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
5938 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
5939 MODULE_DESCRIPTION(TPACPI_DESC);
5940 MODULE_VERSION(TPACPI_VERSION);
5941 MODULE_LICENSE("GPL");
5943 module_init(thinkpad_acpi_module_init);
5944 module_exit(thinkpad_acpi_module_exit);