ACPI: thinkpad-acpi: always track input device open/close
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob1af0d7557d0346e128f14f6b3c37ac4e8de8734d
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.19"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
98 /* NVRAM bit masks */
99 enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
124 * Main driver
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
172 struct ibm_struct;
174 struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
184 struct ibm_struct {
185 char *name;
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
206 struct ibm_init_struct {
207 char param[32];
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219 #endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 light:1;
225 u32 light_status:1;
226 u32 bright_16levels:1;
227 u32 wan:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 platform_drv_registered:1;
231 u32 platform_drv_attrs_registered:1;
232 u32 sensors_pdrv_registered:1;
233 u32 sensors_pdrv_attrs_registered:1;
234 u32 sensors_pdev_attrs_registered:1;
235 u32 hotkey_poll_active:1;
236 } tp_features;
238 struct thinkpad_id_data {
239 unsigned int vendor; /* ThinkPad vendor:
240 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
242 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
245 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
246 u16 ec_model;
248 char *model_str;
250 static struct thinkpad_id_data thinkpad_id;
252 static enum {
253 TPACPI_LIFE_INIT = 0,
254 TPACPI_LIFE_RUNNING,
255 TPACPI_LIFE_EXITING,
256 } tpacpi_lifecycle;
258 static int experimental;
259 static u32 dbg_level;
261 /****************************************************************************
262 ****************************************************************************
264 * ACPI Helpers and device model
266 ****************************************************************************
267 ****************************************************************************/
269 /*************************************************************************
270 * ACPI basic handles
273 static acpi_handle root_handle;
275 #define TPACPI_HANDLE(object, parent, paths...) \
276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
278 static char *object##_path; \
279 static char *object##_paths[] = { paths }
281 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
290 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
293 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
294 /* T4x, X31, X40 */
295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
297 ); /* all others */
299 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
302 ); /* 570 */
305 /*************************************************************************
306 * ACPI helpers
309 static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
312 char *fmt0 = fmt;
313 struct acpi_object_list params;
314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
317 acpi_status status;
318 va_list ap;
319 char res_type;
320 int success;
321 int quiet;
323 if (!*fmt) {
324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
325 return 0;
328 if (*fmt == 'q') {
329 quiet = 1;
330 fmt++;
331 } else
332 quiet = 0;
334 res_type = *(fmt++);
336 params.count = 0;
337 params.pointer = &in_objs[0];
339 va_start(ap, fmt);
340 while (*fmt) {
341 char c = *(fmt++);
342 switch (c) {
343 case 'd': /* int */
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
346 break;
347 /* add more types as needed */
348 default:
349 printk(TPACPI_ERR "acpi_evalf() called "
350 "with invalid format character '%c'\n", c);
351 return 0;
354 va_end(ap);
356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
359 resultp = &result;
360 } else
361 resultp = NULL;
363 status = acpi_evaluate_object(handle, method, &params, resultp);
365 switch (res_type) {
366 case 'd': /* int */
367 if (res)
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
370 break;
371 case 'v': /* void */
372 success = status == AE_OK;
373 break;
374 /* add more types as needed */
375 default:
376 printk(TPACPI_ERR "acpi_evalf() called "
377 "with invalid format character '%c'\n", res_type);
378 return 0;
381 if (!success && !quiet)
382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
383 method, fmt0, status);
385 return success;
388 static int acpi_ec_read(int i, u8 *p)
390 int v;
392 if (ecrd_handle) {
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
394 return 0;
395 *p = v;
396 } else {
397 if (ec_read(i, p) < 0)
398 return 0;
401 return 1;
404 static int acpi_ec_write(int i, u8 v)
406 if (ecwr_handle) {
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
408 return 0;
409 } else {
410 if (ec_write(i, v) < 0)
411 return 0;
414 return 1;
417 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
418 static int _sta(acpi_handle handle)
420 int status;
422 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
423 status = 0;
425 return status;
427 #endif
429 static int issue_thinkpad_cmos_command(int cmos_cmd)
431 if (!cmos_handle)
432 return -ENXIO;
434 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
435 return -EIO;
437 return 0;
440 /*************************************************************************
441 * ACPI device model
444 #define TPACPI_ACPIHANDLE_INIT(object) \
445 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
446 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
448 static void drv_acpi_handle_init(char *name,
449 acpi_handle *handle, acpi_handle parent,
450 char **paths, int num_paths, char **path)
452 int i;
453 acpi_status status;
455 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
456 name);
458 for (i = 0; i < num_paths; i++) {
459 status = acpi_get_handle(parent, paths[i], handle);
460 if (ACPI_SUCCESS(status)) {
461 *path = paths[i];
462 dbg_printk(TPACPI_DBG_INIT,
463 "Found ACPI handle %s for %s\n",
464 *path, name);
465 return;
469 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
470 name);
471 *handle = NULL;
474 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
476 struct ibm_struct *ibm = data;
478 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
479 return;
481 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
482 return;
484 ibm->acpi->notify(ibm, event);
487 static int __init setup_acpi_notify(struct ibm_struct *ibm)
489 acpi_status status;
490 int rc;
492 BUG_ON(!ibm->acpi);
494 if (!*ibm->acpi->handle)
495 return 0;
497 vdbg_printk(TPACPI_DBG_INIT,
498 "setting up ACPI notify for %s\n", ibm->name);
500 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
501 if (rc < 0) {
502 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
503 ibm->name, rc);
504 return -ENODEV;
507 acpi_driver_data(ibm->acpi->device) = ibm;
508 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
509 TPACPI_ACPI_EVENT_PREFIX,
510 ibm->name);
512 status = acpi_install_notify_handler(*ibm->acpi->handle,
513 ibm->acpi->type, dispatch_acpi_notify, ibm);
514 if (ACPI_FAILURE(status)) {
515 if (status == AE_ALREADY_EXISTS) {
516 printk(TPACPI_NOTICE
517 "another device driver is already "
518 "handling %s events\n", ibm->name);
519 } else {
520 printk(TPACPI_ERR
521 "acpi_install_notify_handler(%s) failed: %d\n",
522 ibm->name, status);
524 return -ENODEV;
526 ibm->flags.acpi_notify_installed = 1;
527 return 0;
530 static int __init tpacpi_device_add(struct acpi_device *device)
532 return 0;
535 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
537 int rc;
539 dbg_printk(TPACPI_DBG_INIT,
540 "registering %s as an ACPI driver\n", ibm->name);
542 BUG_ON(!ibm->acpi);
544 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
545 if (!ibm->acpi->driver) {
546 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
547 return -ENOMEM;
550 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
551 ibm->acpi->driver->ids = ibm->acpi->hid;
553 ibm->acpi->driver->ops.add = &tpacpi_device_add;
555 rc = acpi_bus_register_driver(ibm->acpi->driver);
556 if (rc < 0) {
557 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
558 ibm->name, rc);
559 kfree(ibm->acpi->driver);
560 ibm->acpi->driver = NULL;
561 } else if (!rc)
562 ibm->flags.acpi_driver_registered = 1;
564 return rc;
568 /****************************************************************************
569 ****************************************************************************
571 * Procfs Helpers
573 ****************************************************************************
574 ****************************************************************************/
576 static int dispatch_procfs_read(char *page, char **start, off_t off,
577 int count, int *eof, void *data)
579 struct ibm_struct *ibm = data;
580 int len;
582 if (!ibm || !ibm->read)
583 return -EINVAL;
585 len = ibm->read(page);
586 if (len < 0)
587 return len;
589 if (len <= off + count)
590 *eof = 1;
591 *start = page + off;
592 len -= off;
593 if (len > count)
594 len = count;
595 if (len < 0)
596 len = 0;
598 return len;
601 static int dispatch_procfs_write(struct file *file,
602 const char __user *userbuf,
603 unsigned long count, void *data)
605 struct ibm_struct *ibm = data;
606 char *kernbuf;
607 int ret;
609 if (!ibm || !ibm->write)
610 return -EINVAL;
612 kernbuf = kmalloc(count + 2, GFP_KERNEL);
613 if (!kernbuf)
614 return -ENOMEM;
616 if (copy_from_user(kernbuf, userbuf, count)) {
617 kfree(kernbuf);
618 return -EFAULT;
621 kernbuf[count] = 0;
622 strcat(kernbuf, ",");
623 ret = ibm->write(kernbuf);
624 if (ret == 0)
625 ret = count;
627 kfree(kernbuf);
629 return ret;
632 static char *next_cmd(char **cmds)
634 char *start = *cmds;
635 char *end;
637 while ((end = strchr(start, ',')) && end == start)
638 start = end + 1;
640 if (!end)
641 return NULL;
643 *end = 0;
644 *cmds = end + 1;
645 return start;
649 /****************************************************************************
650 ****************************************************************************
652 * Device model: input, hwmon and platform
654 ****************************************************************************
655 ****************************************************************************/
657 static struct platform_device *tpacpi_pdev;
658 static struct platform_device *tpacpi_sensors_pdev;
659 static struct class_device *tpacpi_hwmon;
660 static struct input_dev *tpacpi_inputdev;
661 static struct mutex tpacpi_inputdev_send_mutex;
662 static LIST_HEAD(tpacpi_all_drivers);
664 static int tpacpi_suspend_handler(struct platform_device *pdev,
665 pm_message_t state)
667 struct ibm_struct *ibm, *itmp;
669 list_for_each_entry_safe(ibm, itmp,
670 &tpacpi_all_drivers,
671 all_drivers) {
672 if (ibm->suspend)
673 (ibm->suspend)(state);
676 return 0;
679 static int tpacpi_resume_handler(struct platform_device *pdev)
681 struct ibm_struct *ibm, *itmp;
683 list_for_each_entry_safe(ibm, itmp,
684 &tpacpi_all_drivers,
685 all_drivers) {
686 if (ibm->resume)
687 (ibm->resume)();
690 return 0;
693 static struct platform_driver tpacpi_pdriver = {
694 .driver = {
695 .name = TPACPI_DRVR_NAME,
696 .owner = THIS_MODULE,
698 .suspend = tpacpi_suspend_handler,
699 .resume = tpacpi_resume_handler,
702 static struct platform_driver tpacpi_hwmon_pdriver = {
703 .driver = {
704 .name = TPACPI_HWMON_DRVR_NAME,
705 .owner = THIS_MODULE,
709 /*************************************************************************
710 * sysfs support helpers
713 struct attribute_set {
714 unsigned int members, max_members;
715 struct attribute_group group;
718 struct attribute_set_obj {
719 struct attribute_set s;
720 struct attribute *a;
721 } __attribute__((packed));
723 static struct attribute_set *create_attr_set(unsigned int max_members,
724 const char *name)
726 struct attribute_set_obj *sobj;
728 if (max_members == 0)
729 return NULL;
731 /* Allocates space for implicit NULL at the end too */
732 sobj = kzalloc(sizeof(struct attribute_set_obj) +
733 max_members * sizeof(struct attribute *),
734 GFP_KERNEL);
735 if (!sobj)
736 return NULL;
737 sobj->s.max_members = max_members;
738 sobj->s.group.attrs = &sobj->a;
739 sobj->s.group.name = name;
741 return &sobj->s;
744 #define destroy_attr_set(_set) \
745 kfree(_set);
747 /* not multi-threaded safe, use it in a single thread per set */
748 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
750 if (!s || !attr)
751 return -EINVAL;
753 if (s->members >= s->max_members)
754 return -ENOMEM;
756 s->group.attrs[s->members] = attr;
757 s->members++;
759 return 0;
762 static int add_many_to_attr_set(struct attribute_set *s,
763 struct attribute **attr,
764 unsigned int count)
766 int i, res;
768 for (i = 0; i < count; i++) {
769 res = add_to_attr_set(s, attr[i]);
770 if (res)
771 return res;
774 return 0;
777 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
779 sysfs_remove_group(kobj, &s->group);
780 destroy_attr_set(s);
783 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
784 sysfs_create_group(_kobj, &_attr_set->group)
786 static int parse_strtoul(const char *buf,
787 unsigned long max, unsigned long *value)
789 char *endp;
791 while (*buf && isspace(*buf))
792 buf++;
793 *value = simple_strtoul(buf, &endp, 0);
794 while (*endp && isspace(*endp))
795 endp++;
796 if (*endp || *value > max)
797 return -EINVAL;
799 return 0;
802 /*************************************************************************
803 * thinkpad-acpi driver attributes
806 /* interface_version --------------------------------------------------- */
807 static ssize_t tpacpi_driver_interface_version_show(
808 struct device_driver *drv,
809 char *buf)
811 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
814 static DRIVER_ATTR(interface_version, S_IRUGO,
815 tpacpi_driver_interface_version_show, NULL);
817 /* debug_level --------------------------------------------------------- */
818 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
819 char *buf)
821 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
824 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
825 const char *buf, size_t count)
827 unsigned long t;
829 if (parse_strtoul(buf, 0xffff, &t))
830 return -EINVAL;
832 dbg_level = t;
834 return count;
837 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
838 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
840 /* version ------------------------------------------------------------- */
841 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
842 char *buf)
844 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
845 TPACPI_DESC, TPACPI_VERSION);
848 static DRIVER_ATTR(version, S_IRUGO,
849 tpacpi_driver_version_show, NULL);
851 /* --------------------------------------------------------------------- */
853 static struct driver_attribute *tpacpi_driver_attributes[] = {
854 &driver_attr_debug_level, &driver_attr_version,
855 &driver_attr_interface_version,
858 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
860 int i, res;
862 i = 0;
863 res = 0;
864 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
865 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
866 i++;
869 return res;
872 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
874 int i;
876 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
877 driver_remove_file(drv, tpacpi_driver_attributes[i]);
880 /****************************************************************************
881 ****************************************************************************
883 * Subdrivers
885 ****************************************************************************
886 ****************************************************************************/
888 /*************************************************************************
889 * thinkpad-acpi init subdriver
892 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
894 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
895 printk(TPACPI_INFO "%s\n", TPACPI_URL);
897 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
898 (thinkpad_id.bios_version_str) ?
899 thinkpad_id.bios_version_str : "unknown",
900 (thinkpad_id.ec_version_str) ?
901 thinkpad_id.ec_version_str : "unknown");
903 if (thinkpad_id.vendor && thinkpad_id.model_str)
904 printk(TPACPI_INFO "%s %s\n",
905 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
906 "IBM" : ((thinkpad_id.vendor ==
907 PCI_VENDOR_ID_LENOVO) ?
908 "Lenovo" : "Unknown vendor"),
909 thinkpad_id.model_str);
911 return 0;
914 static int thinkpad_acpi_driver_read(char *p)
916 int len = 0;
918 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
919 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
921 return len;
924 static struct ibm_struct thinkpad_acpi_driver_data = {
925 .name = "driver",
926 .read = thinkpad_acpi_driver_read,
929 /*************************************************************************
930 * Hotkey subdriver
933 enum { /* hot key scan codes (derived from ACPI DSDT) */
934 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
935 TP_ACPI_HOTKEYSCAN_FNF2,
936 TP_ACPI_HOTKEYSCAN_FNF3,
937 TP_ACPI_HOTKEYSCAN_FNF4,
938 TP_ACPI_HOTKEYSCAN_FNF5,
939 TP_ACPI_HOTKEYSCAN_FNF6,
940 TP_ACPI_HOTKEYSCAN_FNF7,
941 TP_ACPI_HOTKEYSCAN_FNF8,
942 TP_ACPI_HOTKEYSCAN_FNF9,
943 TP_ACPI_HOTKEYSCAN_FNF10,
944 TP_ACPI_HOTKEYSCAN_FNF11,
945 TP_ACPI_HOTKEYSCAN_FNF12,
946 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
947 TP_ACPI_HOTKEYSCAN_FNINSERT,
948 TP_ACPI_HOTKEYSCAN_FNDELETE,
949 TP_ACPI_HOTKEYSCAN_FNHOME,
950 TP_ACPI_HOTKEYSCAN_FNEND,
951 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
952 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
953 TP_ACPI_HOTKEYSCAN_FNSPACE,
954 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956 TP_ACPI_HOTKEYSCAN_MUTE,
957 TP_ACPI_HOTKEYSCAN_THINKPAD,
960 enum { /* Keys available through NVRAM polling */
961 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
962 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
965 enum { /* Positions of some of the keys in hotkey masks */
966 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
967 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
968 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
969 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
970 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
971 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
972 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
973 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
974 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
975 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
976 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
979 enum { /* NVRAM to ACPI HKEY group map */
980 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
981 TP_ACPI_HKEY_ZOOM_MASK |
982 TP_ACPI_HKEY_DISPSWTCH_MASK |
983 TP_ACPI_HKEY_HIBERNATE_MASK,
984 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
985 TP_ACPI_HKEY_BRGHTDWN_MASK,
986 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
987 TP_ACPI_HKEY_VOLDWN_MASK |
988 TP_ACPI_HKEY_MUTE_MASK,
991 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
992 struct tp_nvram_state {
993 u16 thinkpad_toggle:1;
994 u16 zoom_toggle:1;
995 u16 display_toggle:1;
996 u16 thinklight_toggle:1;
997 u16 hibernate_toggle:1;
998 u16 displayexp_toggle:1;
999 u16 display_state:1;
1000 u16 brightness_toggle:1;
1001 u16 volume_toggle:1;
1002 u16 mute:1;
1004 u8 brightness_level;
1005 u8 volume_level;
1008 static struct task_struct *tpacpi_hotkey_task;
1009 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1010 static int hotkey_poll_freq = 10; /* Hz */
1011 static struct mutex hotkey_thread_mutex;
1012 static struct mutex hotkey_thread_data_mutex;
1013 static unsigned int hotkey_config_change;
1015 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1017 #define hotkey_source_mask 0U
1019 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1021 static struct mutex hotkey_mutex;
1023 static enum { /* Reasons for waking up */
1024 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1025 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1026 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1027 } hotkey_wakeup_reason;
1029 static int hotkey_autosleep_ack;
1031 static int hotkey_orig_status;
1032 static u32 hotkey_orig_mask;
1033 static u32 hotkey_all_mask;
1034 static u32 hotkey_reserved_mask;
1035 static u32 hotkey_mask;
1037 static unsigned int hotkey_report_mode;
1039 static u16 *hotkey_keycode_map;
1041 static struct attribute_set *hotkey_dev_attributes;
1043 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1044 #define HOTKEY_CONFIG_CRITICAL_START \
1045 do { \
1046 mutex_lock(&hotkey_thread_data_mutex); \
1047 hotkey_config_change++; \
1048 } while (0);
1049 #define HOTKEY_CONFIG_CRITICAL_END \
1050 mutex_unlock(&hotkey_thread_data_mutex);
1051 #else
1052 #define HOTKEY_CONFIG_CRITICAL_START
1053 #define HOTKEY_CONFIG_CRITICAL_END
1054 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1056 static int hotkey_get_wlsw(int *status)
1058 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1059 return -EIO;
1060 return 0;
1064 * Call with hotkey_mutex held
1066 static int hotkey_mask_get(void)
1068 u32 m = 0;
1070 if (tp_features.hotkey_mask) {
1071 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1072 return -EIO;
1074 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1076 return 0;
1080 * Call with hotkey_mutex held
1082 static int hotkey_mask_set(u32 mask)
1084 int i;
1085 int rc = 0;
1087 if (tp_features.hotkey_mask) {
1088 HOTKEY_CONFIG_CRITICAL_START
1089 for (i = 0; i < 32; i++) {
1090 u32 m = 1 << i;
1091 /* enable in firmware mask only keys not in NVRAM
1092 * mode, but enable the key in the cached hotkey_mask
1093 * regardless of mode, or the key will end up
1094 * disabled by hotkey_mask_get() */
1095 if (!acpi_evalf(hkey_handle,
1096 NULL, "MHKM", "vdd", i + 1,
1097 !!((mask & ~hotkey_source_mask) & m))) {
1098 rc = -EIO;
1099 break;
1100 } else {
1101 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1104 HOTKEY_CONFIG_CRITICAL_END
1106 /* hotkey_mask_get must be called unconditionally below */
1107 if (!hotkey_mask_get() && !rc &&
1108 (hotkey_mask & ~hotkey_source_mask) !=
1109 (mask & ~hotkey_source_mask)) {
1110 printk(TPACPI_NOTICE
1111 "requested hot key mask 0x%08x, but "
1112 "firmware forced it to 0x%08x\n",
1113 mask, hotkey_mask);
1115 } else {
1116 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1117 HOTKEY_CONFIG_CRITICAL_START
1118 hotkey_mask = mask & hotkey_source_mask;
1119 HOTKEY_CONFIG_CRITICAL_END
1120 hotkey_mask_get();
1121 if (hotkey_mask != mask) {
1122 printk(TPACPI_NOTICE
1123 "requested hot key mask 0x%08x, "
1124 "forced to 0x%08x (NVRAM poll mask is "
1125 "0x%08x): no firmware mask support\n",
1126 mask, hotkey_mask, hotkey_source_mask);
1128 #else
1129 hotkey_mask_get();
1130 rc = -ENXIO;
1131 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1134 return rc;
1137 static int hotkey_status_get(int *status)
1139 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1140 return -EIO;
1142 return 0;
1145 static int hotkey_status_set(int status)
1147 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1148 return -EIO;
1150 return 0;
1153 static void tpacpi_input_send_radiosw(void)
1155 int wlsw;
1157 mutex_lock(&tpacpi_inputdev_send_mutex);
1159 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1160 input_report_switch(tpacpi_inputdev,
1161 SW_RADIO, !!wlsw);
1162 input_sync(tpacpi_inputdev);
1165 mutex_unlock(&tpacpi_inputdev_send_mutex);
1168 static void tpacpi_input_send_key(unsigned int scancode)
1170 unsigned int keycode;
1172 keycode = hotkey_keycode_map[scancode];
1174 if (keycode != KEY_RESERVED) {
1175 mutex_lock(&tpacpi_inputdev_send_mutex);
1177 input_report_key(tpacpi_inputdev, keycode, 1);
1178 if (keycode == KEY_UNKNOWN)
1179 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1180 scancode);
1181 input_sync(tpacpi_inputdev);
1183 input_report_key(tpacpi_inputdev, keycode, 0);
1184 if (keycode == KEY_UNKNOWN)
1185 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1186 scancode);
1187 input_sync(tpacpi_inputdev);
1189 mutex_unlock(&tpacpi_inputdev_send_mutex);
1193 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1194 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1196 static void tpacpi_hotkey_send_key(unsigned int scancode)
1198 tpacpi_input_send_key(scancode);
1199 if (hotkey_report_mode < 2) {
1200 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1201 0x80, 0x1001 + scancode);
1205 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1207 u8 d;
1209 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1210 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1211 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1212 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1213 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1214 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1216 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1217 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1218 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1220 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1221 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1222 n->displayexp_toggle =
1223 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1225 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1226 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1227 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1228 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1229 n->brightness_toggle =
1230 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1232 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1233 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1234 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1235 >> TP_NVRAM_POS_LEVEL_VOLUME;
1236 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1237 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1241 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1242 do { \
1243 if ((mask & (1 << __scancode)) && \
1244 oldn->__member != newn->__member) \
1245 tpacpi_hotkey_send_key(__scancode); \
1246 } while (0)
1248 #define TPACPI_MAY_SEND_KEY(__scancode) \
1249 do { if (mask & (1 << __scancode)) \
1250 tpacpi_hotkey_send_key(__scancode); } while (0)
1252 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1253 struct tp_nvram_state *newn,
1254 u32 mask)
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1257 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1258 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1259 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1261 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1263 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1265 /* handle volume */
1266 if (oldn->volume_toggle != newn->volume_toggle) {
1267 if (oldn->mute != newn->mute) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1270 if (oldn->volume_level > newn->volume_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272 } else if (oldn->volume_level < newn->volume_level) {
1273 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1274 } else if (oldn->mute == newn->mute) {
1275 /* repeated key presses that didn't change state */
1276 if (newn->mute) {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1278 } else if (newn->volume_level != 0) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1280 } else {
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1286 /* handle brightness */
1287 if (oldn->brightness_toggle != newn->brightness_toggle) {
1288 if (oldn->brightness_level < newn->brightness_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1290 } else if (oldn->brightness_level > newn->brightness_level) {
1291 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1292 } else {
1293 /* repeated key presses that didn't change state */
1294 if (newn->brightness_level != 0) {
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1296 } else {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1303 #undef TPACPI_COMPARE_KEY
1304 #undef TPACPI_MAY_SEND_KEY
1306 static int hotkey_kthread(void *data)
1308 struct tp_nvram_state s[2];
1309 u32 mask;
1310 unsigned int si, so;
1311 unsigned long t;
1312 unsigned int change_detector, must_reset;
1314 mutex_lock(&hotkey_thread_mutex);
1316 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1317 goto exit;
1319 set_freezable();
1321 so = 0;
1322 si = 1;
1323 t = 0;
1325 /* Initial state for compares */
1326 mutex_lock(&hotkey_thread_data_mutex);
1327 change_detector = hotkey_config_change;
1328 mask = hotkey_source_mask & hotkey_mask;
1329 mutex_unlock(&hotkey_thread_data_mutex);
1330 hotkey_read_nvram(&s[so], mask);
1332 while (!kthread_should_stop() && hotkey_poll_freq) {
1333 if (t == 0)
1334 t = 1000/hotkey_poll_freq;
1335 t = msleep_interruptible(t);
1336 if (unlikely(kthread_should_stop()))
1337 break;
1338 must_reset = try_to_freeze();
1339 if (t > 0 && !must_reset)
1340 continue;
1342 mutex_lock(&hotkey_thread_data_mutex);
1343 if (must_reset || hotkey_config_change != change_detector) {
1344 /* forget old state on thaw or config change */
1345 si = so;
1346 t = 0;
1347 change_detector = hotkey_config_change;
1349 mask = hotkey_source_mask & hotkey_mask;
1350 mutex_unlock(&hotkey_thread_data_mutex);
1352 if (likely(mask)) {
1353 hotkey_read_nvram(&s[si], mask);
1354 if (likely(si != so)) {
1355 hotkey_compare_and_issue_event(&s[so], &s[si],
1356 mask);
1360 so = si;
1361 si ^= 1;
1364 exit:
1365 mutex_unlock(&hotkey_thread_mutex);
1366 return 0;
1369 static void hotkey_poll_stop_sync(void)
1371 if (tpacpi_hotkey_task) {
1372 if (frozen(tpacpi_hotkey_task) ||
1373 freezing(tpacpi_hotkey_task))
1374 thaw_process(tpacpi_hotkey_task);
1376 kthread_stop(tpacpi_hotkey_task);
1377 tpacpi_hotkey_task = NULL;
1378 mutex_lock(&hotkey_thread_mutex);
1379 /* at this point, the thread did exit */
1380 mutex_unlock(&hotkey_thread_mutex);
1384 /* call with hotkey_mutex held */
1385 static void hotkey_poll_setup(int may_warn)
1387 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1388 hotkey_poll_freq > 0 &&
1389 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1390 if (!tpacpi_hotkey_task) {
1391 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1392 NULL,
1393 TPACPI_FILE "d");
1394 if (IS_ERR(tpacpi_hotkey_task)) {
1395 tpacpi_hotkey_task = NULL;
1396 printk(TPACPI_ERR
1397 "could not create kernel thread "
1398 "for hotkey polling\n");
1401 } else {
1402 hotkey_poll_stop_sync();
1403 if (may_warn &&
1404 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1405 printk(TPACPI_NOTICE
1406 "hot keys 0x%08x require polling, "
1407 "which is currently disabled\n",
1408 hotkey_source_mask);
1413 static void hotkey_poll_setup_safe(int may_warn)
1415 mutex_lock(&hotkey_mutex);
1416 hotkey_poll_setup(may_warn);
1417 mutex_unlock(&hotkey_mutex);
1420 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1422 static void hotkey_poll_setup_safe(int __unused)
1426 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1428 static int hotkey_inputdev_open(struct input_dev *dev)
1430 switch (tpacpi_lifecycle) {
1431 case TPACPI_LIFE_INIT:
1433 * hotkey_init will call hotkey_poll_setup_safe
1434 * at the appropriate moment
1436 return 0;
1437 case TPACPI_LIFE_EXITING:
1438 return -EBUSY;
1439 case TPACPI_LIFE_RUNNING:
1440 hotkey_poll_setup_safe(0);
1441 return 0;
1444 /* Should only happen if tpacpi_lifecycle is corrupt */
1445 BUG();
1446 return -EBUSY;
1449 static void hotkey_inputdev_close(struct input_dev *dev)
1451 /* disable hotkey polling when possible */
1452 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1453 hotkey_poll_setup_safe(0);
1456 /* sysfs hotkey enable ------------------------------------------------- */
1457 static ssize_t hotkey_enable_show(struct device *dev,
1458 struct device_attribute *attr,
1459 char *buf)
1461 int res, status;
1463 res = hotkey_status_get(&status);
1464 if (res)
1465 return res;
1467 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1470 static ssize_t hotkey_enable_store(struct device *dev,
1471 struct device_attribute *attr,
1472 const char *buf, size_t count)
1474 unsigned long t;
1475 int res;
1477 if (parse_strtoul(buf, 1, &t))
1478 return -EINVAL;
1480 res = hotkey_status_set(t);
1482 return (res) ? res : count;
1485 static struct device_attribute dev_attr_hotkey_enable =
1486 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1487 hotkey_enable_show, hotkey_enable_store);
1489 /* sysfs hotkey mask --------------------------------------------------- */
1490 static ssize_t hotkey_mask_show(struct device *dev,
1491 struct device_attribute *attr,
1492 char *buf)
1494 int res;
1496 if (mutex_lock_interruptible(&hotkey_mutex))
1497 return -ERESTARTSYS;
1498 res = hotkey_mask_get();
1499 mutex_unlock(&hotkey_mutex);
1501 return (res)?
1502 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1505 static ssize_t hotkey_mask_store(struct device *dev,
1506 struct device_attribute *attr,
1507 const char *buf, size_t count)
1509 unsigned long t;
1510 int res;
1512 if (parse_strtoul(buf, 0xffffffffUL, &t))
1513 return -EINVAL;
1515 if (mutex_lock_interruptible(&hotkey_mutex))
1516 return -ERESTARTSYS;
1518 res = hotkey_mask_set(t);
1520 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1521 hotkey_poll_setup(1);
1522 #endif
1524 mutex_unlock(&hotkey_mutex);
1526 return (res) ? res : count;
1529 static struct device_attribute dev_attr_hotkey_mask =
1530 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1531 hotkey_mask_show, hotkey_mask_store);
1533 /* sysfs hotkey bios_enabled ------------------------------------------- */
1534 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1535 struct device_attribute *attr,
1536 char *buf)
1538 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1541 static struct device_attribute dev_attr_hotkey_bios_enabled =
1542 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1544 /* sysfs hotkey bios_mask ---------------------------------------------- */
1545 static ssize_t hotkey_bios_mask_show(struct device *dev,
1546 struct device_attribute *attr,
1547 char *buf)
1549 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1552 static struct device_attribute dev_attr_hotkey_bios_mask =
1553 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1555 /* sysfs hotkey all_mask ----------------------------------------------- */
1556 static ssize_t hotkey_all_mask_show(struct device *dev,
1557 struct device_attribute *attr,
1558 char *buf)
1560 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1561 hotkey_all_mask | hotkey_source_mask);
1564 static struct device_attribute dev_attr_hotkey_all_mask =
1565 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1567 /* sysfs hotkey recommended_mask --------------------------------------- */
1568 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1569 struct device_attribute *attr,
1570 char *buf)
1572 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1573 (hotkey_all_mask | hotkey_source_mask)
1574 & ~hotkey_reserved_mask);
1577 static struct device_attribute dev_attr_hotkey_recommended_mask =
1578 __ATTR(hotkey_recommended_mask, S_IRUGO,
1579 hotkey_recommended_mask_show, NULL);
1581 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1583 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1584 static ssize_t hotkey_source_mask_show(struct device *dev,
1585 struct device_attribute *attr,
1586 char *buf)
1588 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1591 static ssize_t hotkey_source_mask_store(struct device *dev,
1592 struct device_attribute *attr,
1593 const char *buf, size_t count)
1595 unsigned long t;
1597 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1598 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1599 return -EINVAL;
1601 if (mutex_lock_interruptible(&hotkey_mutex))
1602 return -ERESTARTSYS;
1604 HOTKEY_CONFIG_CRITICAL_START
1605 hotkey_source_mask = t;
1606 HOTKEY_CONFIG_CRITICAL_END
1608 hotkey_poll_setup(1);
1610 mutex_unlock(&hotkey_mutex);
1612 return count;
1615 static struct device_attribute dev_attr_hotkey_source_mask =
1616 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1617 hotkey_source_mask_show, hotkey_source_mask_store);
1619 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1620 static ssize_t hotkey_poll_freq_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1624 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1627 static ssize_t hotkey_poll_freq_store(struct device *dev,
1628 struct device_attribute *attr,
1629 const char *buf, size_t count)
1631 unsigned long t;
1633 if (parse_strtoul(buf, 25, &t))
1634 return -EINVAL;
1636 if (mutex_lock_interruptible(&hotkey_mutex))
1637 return -ERESTARTSYS;
1639 hotkey_poll_freq = t;
1641 hotkey_poll_setup(1);
1642 mutex_unlock(&hotkey_mutex);
1644 return count;
1647 static struct device_attribute dev_attr_hotkey_poll_freq =
1648 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1649 hotkey_poll_freq_show, hotkey_poll_freq_store);
1651 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1653 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1654 static ssize_t hotkey_radio_sw_show(struct device *dev,
1655 struct device_attribute *attr,
1656 char *buf)
1658 int res, s;
1659 res = hotkey_get_wlsw(&s);
1660 if (res < 0)
1661 return res;
1663 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1666 static struct device_attribute dev_attr_hotkey_radio_sw =
1667 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1669 static void hotkey_radio_sw_notify_change(void)
1671 if (tp_features.hotkey_wlsw)
1672 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1673 "hotkey_radio_sw");
1676 /* sysfs hotkey report_mode -------------------------------------------- */
1677 static ssize_t hotkey_report_mode_show(struct device *dev,
1678 struct device_attribute *attr,
1679 char *buf)
1681 return snprintf(buf, PAGE_SIZE, "%d\n",
1682 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1685 static struct device_attribute dev_attr_hotkey_report_mode =
1686 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1688 /* sysfs wakeup reason (pollable) -------------------------------------- */
1689 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1690 struct device_attribute *attr,
1691 char *buf)
1693 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1696 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1697 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1699 static void hotkey_wakeup_reason_notify_change(void)
1701 if (tp_features.hotkey_mask)
1702 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1703 "wakeup_reason");
1706 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1707 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1708 struct device_attribute *attr,
1709 char *buf)
1711 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1714 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1715 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1716 hotkey_wakeup_hotunplug_complete_show, NULL);
1718 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1720 if (tp_features.hotkey_mask)
1721 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1722 "wakeup_hotunplug_complete");
1725 /* --------------------------------------------------------------------- */
1727 static struct attribute *hotkey_attributes[] __initdata = {
1728 &dev_attr_hotkey_enable.attr,
1729 &dev_attr_hotkey_bios_enabled.attr,
1730 &dev_attr_hotkey_report_mode.attr,
1731 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1732 &dev_attr_hotkey_mask.attr,
1733 &dev_attr_hotkey_all_mask.attr,
1734 &dev_attr_hotkey_recommended_mask.attr,
1735 &dev_attr_hotkey_source_mask.attr,
1736 &dev_attr_hotkey_poll_freq.attr,
1737 #endif
1740 static struct attribute *hotkey_mask_attributes[] __initdata = {
1741 &dev_attr_hotkey_bios_mask.attr,
1742 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1743 &dev_attr_hotkey_mask.attr,
1744 &dev_attr_hotkey_all_mask.attr,
1745 &dev_attr_hotkey_recommended_mask.attr,
1746 #endif
1747 &dev_attr_hotkey_wakeup_reason.attr,
1748 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1751 static int __init hotkey_init(struct ibm_init_struct *iibm)
1753 /* Requirements for changing the default keymaps:
1755 * 1. Many of the keys are mapped to KEY_RESERVED for very
1756 * good reasons. Do not change them unless you have deep
1757 * knowledge on the IBM and Lenovo ThinkPad firmware for
1758 * the various ThinkPad models. The driver behaves
1759 * differently for KEY_RESERVED: such keys have their
1760 * hot key mask *unset* in mask_recommended, and also
1761 * in the initial hot key mask programmed into the
1762 * firmware at driver load time, which means the firm-
1763 * ware may react very differently if you change them to
1764 * something else;
1766 * 2. You must be subscribed to the linux-thinkpad and
1767 * ibm-acpi-devel mailing lists, and you should read the
1768 * list archives since 2007 if you want to change the
1769 * keymaps. This requirement exists so that you will
1770 * know the past history of problems with the thinkpad-
1771 * acpi driver keymaps, and also that you will be
1772 * listening to any bug reports;
1774 * 3. Do not send thinkpad-acpi specific patches directly to
1775 * for merging, *ever*. Send them to the linux-acpi
1776 * mailinglist for comments. Merging is to be done only
1777 * through acpi-test and the ACPI maintainer.
1779 * If the above is too much to ask, don't change the keymap.
1780 * Ask the thinkpad-acpi maintainer to do it, instead.
1782 static u16 ibm_keycode_map[] __initdata = {
1783 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1784 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1785 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1786 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1788 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1789 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1790 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1791 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1793 /* brightness: firmware always reacts to them, unless
1794 * X.org did some tricks in the radeon BIOS scratch
1795 * registers of *some* models */
1796 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1797 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1799 /* Thinklight: firmware always react to it */
1800 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1802 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1803 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1805 /* Volume: firmware always react to it and reprograms
1806 * the built-in *extra* mixer. Never map it to control
1807 * another mixer by default. */
1808 KEY_RESERVED, /* 0x14: VOLUME UP */
1809 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1810 KEY_RESERVED, /* 0x16: MUTE */
1812 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1814 /* (assignments unknown, please report if found) */
1815 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1816 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1818 static u16 lenovo_keycode_map[] __initdata = {
1819 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1820 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1821 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1822 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1824 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1825 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1826 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1827 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1829 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1830 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1832 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1834 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1835 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1837 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1838 * react to it and reprograms the built-in *extra* mixer.
1839 * Never map it to control another mixer by default.
1841 * T60?, T61, R60?, R61: firmware and EC tries to send
1842 * these over the regular keyboard, so these are no-ops,
1843 * but there are still weird bugs re. MUTE, so do not
1844 * change unless you get test reports from all Lenovo
1845 * models. May cause the BIOS to interfere with the
1846 * HDA mixer.
1848 KEY_RESERVED, /* 0x14: VOLUME UP */
1849 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1850 KEY_RESERVED, /* 0x16: MUTE */
1852 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1854 /* (assignments unknown, please report if found) */
1855 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1856 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1859 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1860 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1861 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1863 int res, i;
1864 int status;
1865 int hkeyv;
1867 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1869 BUG_ON(!tpacpi_inputdev);
1870 BUG_ON(tpacpi_inputdev->open != NULL ||
1871 tpacpi_inputdev->close != NULL);
1873 TPACPI_ACPIHANDLE_INIT(hkey);
1874 mutex_init(&hotkey_mutex);
1876 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1877 mutex_init(&hotkey_thread_mutex);
1878 mutex_init(&hotkey_thread_data_mutex);
1879 #endif
1881 /* hotkey not supported on 570 */
1882 tp_features.hotkey = hkey_handle != NULL;
1884 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1885 str_supported(tp_features.hotkey));
1887 if (tp_features.hotkey) {
1888 hotkey_dev_attributes = create_attr_set(12, NULL);
1889 if (!hotkey_dev_attributes)
1890 return -ENOMEM;
1891 res = add_many_to_attr_set(hotkey_dev_attributes,
1892 hotkey_attributes,
1893 ARRAY_SIZE(hotkey_attributes));
1894 if (res)
1895 return res;
1897 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1898 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1899 for HKEY interface version 0x100 */
1900 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1901 if ((hkeyv >> 8) != 1) {
1902 printk(TPACPI_ERR "unknown version of the "
1903 "HKEY interface: 0x%x\n", hkeyv);
1904 printk(TPACPI_ERR "please report this to %s\n",
1905 TPACPI_MAIL);
1906 } else {
1908 * MHKV 0x100 in A31, R40, R40e,
1909 * T4x, X31, and later
1911 tp_features.hotkey_mask = 1;
1915 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1916 str_supported(tp_features.hotkey_mask));
1918 if (tp_features.hotkey_mask) {
1919 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1920 "MHKA", "qd")) {
1921 printk(TPACPI_ERR
1922 "missing MHKA handler, "
1923 "please report this to %s\n",
1924 TPACPI_MAIL);
1925 /* FN+F12, FN+F4, FN+F3 */
1926 hotkey_all_mask = 0x080cU;
1930 /* hotkey_source_mask *must* be zero for
1931 * the first hotkey_mask_get */
1932 res = hotkey_status_get(&hotkey_orig_status);
1933 if (!res && tp_features.hotkey_mask) {
1934 res = hotkey_mask_get();
1935 hotkey_orig_mask = hotkey_mask;
1936 if (!res) {
1937 res = add_many_to_attr_set(
1938 hotkey_dev_attributes,
1939 hotkey_mask_attributes,
1940 ARRAY_SIZE(hotkey_mask_attributes));
1944 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1945 if (tp_features.hotkey_mask) {
1946 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1947 & ~hotkey_all_mask;
1948 } else {
1949 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1952 vdbg_printk(TPACPI_DBG_INIT,
1953 "hotkey source mask 0x%08x, polling freq %d\n",
1954 hotkey_source_mask, hotkey_poll_freq);
1955 #endif
1957 /* Not all thinkpads have a hardware radio switch */
1958 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1959 tp_features.hotkey_wlsw = 1;
1960 printk(TPACPI_INFO
1961 "radio switch found; radios are %s\n",
1962 enabled(status, 0));
1963 res = add_to_attr_set(hotkey_dev_attributes,
1964 &dev_attr_hotkey_radio_sw.attr);
1967 if (!res)
1968 res = register_attr_set_with_sysfs(
1969 hotkey_dev_attributes,
1970 &tpacpi_pdev->dev.kobj);
1971 if (res)
1972 return res;
1974 /* Set up key map */
1976 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1977 GFP_KERNEL);
1978 if (!hotkey_keycode_map) {
1979 printk(TPACPI_ERR
1980 "failed to allocate memory for key map\n");
1981 return -ENOMEM;
1984 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1985 dbg_printk(TPACPI_DBG_INIT,
1986 "using Lenovo default hot key map\n");
1987 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1988 TPACPI_HOTKEY_MAP_SIZE);
1989 } else {
1990 dbg_printk(TPACPI_DBG_INIT,
1991 "using IBM default hot key map\n");
1992 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1993 TPACPI_HOTKEY_MAP_SIZE);
1996 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1997 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1998 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1999 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2000 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2001 tpacpi_inputdev->keycode = hotkey_keycode_map;
2002 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2003 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2004 set_bit(hotkey_keycode_map[i],
2005 tpacpi_inputdev->keybit);
2006 } else {
2007 if (i < sizeof(hotkey_reserved_mask)*8)
2008 hotkey_reserved_mask |= 1 << i;
2012 if (tp_features.hotkey_wlsw) {
2013 set_bit(EV_SW, tpacpi_inputdev->evbit);
2014 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2017 dbg_printk(TPACPI_DBG_INIT,
2018 "enabling hot key handling\n");
2019 res = hotkey_status_set(1);
2020 if (res)
2021 return res;
2022 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2023 & ~hotkey_reserved_mask)
2024 | hotkey_orig_mask);
2025 if (res < 0 && res != -ENXIO)
2026 return res;
2028 dbg_printk(TPACPI_DBG_INIT,
2029 "legacy hot key reporting over procfs %s\n",
2030 (hotkey_report_mode < 2) ?
2031 "enabled" : "disabled");
2033 tpacpi_inputdev->open = &hotkey_inputdev_open;
2034 tpacpi_inputdev->close = &hotkey_inputdev_close;
2036 hotkey_poll_setup_safe(1);
2039 return (tp_features.hotkey)? 0 : 1;
2042 static void hotkey_exit(void)
2044 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2045 hotkey_poll_stop_sync();
2046 #endif
2048 if (tp_features.hotkey) {
2049 dbg_printk(TPACPI_DBG_EXIT,
2050 "restoring original hot key mask\n");
2051 /* no short-circuit boolean operator below! */
2052 if ((hotkey_mask_set(hotkey_orig_mask) |
2053 hotkey_status_set(hotkey_orig_status)) != 0)
2054 printk(TPACPI_ERR
2055 "failed to restore hot key mask "
2056 "to BIOS defaults\n");
2059 if (hotkey_dev_attributes) {
2060 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2061 hotkey_dev_attributes = NULL;
2065 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2067 u32 hkey;
2068 unsigned int scancode;
2069 int send_acpi_ev;
2070 int ignore_acpi_ev;
2071 int unk_ev;
2073 if (event != 0x80) {
2074 printk(TPACPI_ERR
2075 "unknown HKEY notification event %d\n", event);
2076 /* forward it to userspace, maybe it knows how to handle it */
2077 acpi_bus_generate_netlink_event(
2078 ibm->acpi->device->pnp.device_class,
2079 ibm->acpi->device->dev.bus_id,
2080 event, 0);
2081 return;
2084 while (1) {
2085 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2086 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2087 return;
2090 if (hkey == 0) {
2091 /* queue empty */
2092 return;
2095 send_acpi_ev = 1;
2096 ignore_acpi_ev = 0;
2097 unk_ev = 0;
2099 switch (hkey >> 12) {
2100 case 1:
2101 /* 0x1000-0x1FFF: key presses */
2102 scancode = hkey & 0xfff;
2103 if (scancode > 0 && scancode < 0x21) {
2104 scancode--;
2105 if (!(hotkey_source_mask & (1 << scancode))) {
2106 tpacpi_input_send_key(scancode);
2107 send_acpi_ev = 0;
2108 } else {
2109 ignore_acpi_ev = 1;
2111 } else {
2112 unk_ev = 1;
2114 break;
2115 case 2:
2116 /* Wakeup reason */
2117 switch (hkey) {
2118 case 0x2304: /* suspend, undock */
2119 case 0x2404: /* hibernation, undock */
2120 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2121 ignore_acpi_ev = 1;
2122 break;
2123 case 0x2305: /* suspend, bay eject */
2124 case 0x2405: /* hibernation, bay eject */
2125 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2126 ignore_acpi_ev = 1;
2127 break;
2128 default:
2129 unk_ev = 1;
2131 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2132 printk(TPACPI_INFO
2133 "woke up due to a hot-unplug "
2134 "request...\n");
2135 hotkey_wakeup_reason_notify_change();
2137 break;
2138 case 3:
2139 /* bay-related wakeups */
2140 if (hkey == 0x3003) {
2141 hotkey_autosleep_ack = 1;
2142 printk(TPACPI_INFO
2143 "bay ejected\n");
2144 hotkey_wakeup_hotunplug_complete_notify_change();
2145 } else {
2146 unk_ev = 1;
2148 break;
2149 case 4:
2150 /* dock-related wakeups */
2151 if (hkey == 0x4003) {
2152 hotkey_autosleep_ack = 1;
2153 printk(TPACPI_INFO
2154 "undocked\n");
2155 hotkey_wakeup_hotunplug_complete_notify_change();
2156 } else {
2157 unk_ev = 1;
2159 break;
2160 case 5:
2161 /* 0x5000-0x5FFF: human interface helpers */
2162 switch (hkey) {
2163 case 0x5010: /* Lenovo new BIOS: brightness changed */
2164 case 0x5009: /* X61t: swivel up (tablet mode) */
2165 case 0x500a: /* X61t: swivel down (normal mode) */
2166 case 0x500b: /* X61t: tablet pen inserted into bay */
2167 case 0x500c: /* X61t: tablet pen removed from bay */
2168 break;
2169 case 0x5001:
2170 case 0x5002:
2171 /* LID switch events. Do not propagate */
2172 ignore_acpi_ev = 1;
2173 break;
2174 default:
2175 unk_ev = 1;
2177 break;
2178 case 7:
2179 /* 0x7000-0x7FFF: misc */
2180 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2181 tpacpi_input_send_radiosw();
2182 hotkey_radio_sw_notify_change();
2183 send_acpi_ev = 0;
2184 break;
2186 /* fallthrough to default */
2187 default:
2188 unk_ev = 1;
2190 if (unk_ev) {
2191 printk(TPACPI_NOTICE
2192 "unhandled HKEY event 0x%04x\n", hkey);
2195 /* Legacy events */
2196 if (!ignore_acpi_ev &&
2197 (send_acpi_ev || hotkey_report_mode < 2)) {
2198 acpi_bus_generate_proc_event(ibm->acpi->device,
2199 event, hkey);
2202 /* netlink events */
2203 if (!ignore_acpi_ev && send_acpi_ev) {
2204 acpi_bus_generate_netlink_event(
2205 ibm->acpi->device->pnp.device_class,
2206 ibm->acpi->device->dev.bus_id,
2207 event, hkey);
2212 static void hotkey_suspend(pm_message_t state)
2214 /* Do these on suspend, we get the events on early resume! */
2215 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2216 hotkey_autosleep_ack = 0;
2219 static void hotkey_resume(void)
2221 if (hotkey_mask_get())
2222 printk(TPACPI_ERR
2223 "error while trying to read hot key mask "
2224 "from firmware\n");
2225 tpacpi_input_send_radiosw();
2226 hotkey_radio_sw_notify_change();
2227 hotkey_wakeup_reason_notify_change();
2228 hotkey_wakeup_hotunplug_complete_notify_change();
2229 hotkey_poll_setup_safe(0);
2232 /* procfs -------------------------------------------------------------- */
2233 static int hotkey_read(char *p)
2235 int res, status;
2236 int len = 0;
2238 if (!tp_features.hotkey) {
2239 len += sprintf(p + len, "status:\t\tnot supported\n");
2240 return len;
2243 if (mutex_lock_interruptible(&hotkey_mutex))
2244 return -ERESTARTSYS;
2245 res = hotkey_status_get(&status);
2246 if (!res)
2247 res = hotkey_mask_get();
2248 mutex_unlock(&hotkey_mutex);
2249 if (res)
2250 return res;
2252 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2253 if (tp_features.hotkey_mask) {
2254 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2255 len += sprintf(p + len,
2256 "commands:\tenable, disable, reset, <mask>\n");
2257 } else {
2258 len += sprintf(p + len, "mask:\t\tnot supported\n");
2259 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2262 return len;
2265 static int hotkey_write(char *buf)
2267 int res, status;
2268 u32 mask;
2269 char *cmd;
2271 if (!tp_features.hotkey)
2272 return -ENODEV;
2274 if (mutex_lock_interruptible(&hotkey_mutex))
2275 return -ERESTARTSYS;
2277 status = -1;
2278 mask = hotkey_mask;
2280 res = 0;
2281 while ((cmd = next_cmd(&buf))) {
2282 if (strlencmp(cmd, "enable") == 0) {
2283 status = 1;
2284 } else if (strlencmp(cmd, "disable") == 0) {
2285 status = 0;
2286 } else if (strlencmp(cmd, "reset") == 0) {
2287 status = hotkey_orig_status;
2288 mask = hotkey_orig_mask;
2289 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2290 /* mask set */
2291 } else if (sscanf(cmd, "%x", &mask) == 1) {
2292 /* mask set */
2293 } else {
2294 res = -EINVAL;
2295 goto errexit;
2298 if (status != -1)
2299 res = hotkey_status_set(status);
2301 if (!res && mask != hotkey_mask)
2302 res = hotkey_mask_set(mask);
2304 errexit:
2305 mutex_unlock(&hotkey_mutex);
2306 return res;
2309 static const struct acpi_device_id ibm_htk_device_ids[] = {
2310 {TPACPI_ACPI_HKEY_HID, 0},
2311 {"", 0},
2314 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2315 .hid = ibm_htk_device_ids,
2316 .notify = hotkey_notify,
2317 .handle = &hkey_handle,
2318 .type = ACPI_DEVICE_NOTIFY,
2321 static struct ibm_struct hotkey_driver_data = {
2322 .name = "hotkey",
2323 .read = hotkey_read,
2324 .write = hotkey_write,
2325 .exit = hotkey_exit,
2326 .resume = hotkey_resume,
2327 .suspend = hotkey_suspend,
2328 .acpi = &ibm_hotkey_acpidriver,
2331 /*************************************************************************
2332 * Bluetooth subdriver
2335 enum {
2336 /* ACPI GBDC/SBDC bits */
2337 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2338 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2339 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2342 static int bluetooth_get_radiosw(void);
2343 static int bluetooth_set_radiosw(int radio_on);
2345 /* sysfs bluetooth enable ---------------------------------------------- */
2346 static ssize_t bluetooth_enable_show(struct device *dev,
2347 struct device_attribute *attr,
2348 char *buf)
2350 int status;
2352 status = bluetooth_get_radiosw();
2353 if (status < 0)
2354 return status;
2356 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2359 static ssize_t bluetooth_enable_store(struct device *dev,
2360 struct device_attribute *attr,
2361 const char *buf, size_t count)
2363 unsigned long t;
2364 int res;
2366 if (parse_strtoul(buf, 1, &t))
2367 return -EINVAL;
2369 res = bluetooth_set_radiosw(t);
2371 return (res) ? res : count;
2374 static struct device_attribute dev_attr_bluetooth_enable =
2375 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2376 bluetooth_enable_show, bluetooth_enable_store);
2378 /* --------------------------------------------------------------------- */
2380 static struct attribute *bluetooth_attributes[] = {
2381 &dev_attr_bluetooth_enable.attr,
2382 NULL
2385 static const struct attribute_group bluetooth_attr_group = {
2386 .attrs = bluetooth_attributes,
2389 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2391 int res;
2392 int status = 0;
2394 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2396 TPACPI_ACPIHANDLE_INIT(hkey);
2398 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2399 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2400 tp_features.bluetooth = hkey_handle &&
2401 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2403 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2404 str_supported(tp_features.bluetooth),
2405 status);
2407 if (tp_features.bluetooth) {
2408 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2409 /* no bluetooth hardware present in system */
2410 tp_features.bluetooth = 0;
2411 dbg_printk(TPACPI_DBG_INIT,
2412 "bluetooth hardware not installed\n");
2413 } else {
2414 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2415 &bluetooth_attr_group);
2416 if (res)
2417 return res;
2421 return (tp_features.bluetooth)? 0 : 1;
2424 static void bluetooth_exit(void)
2426 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2427 &bluetooth_attr_group);
2430 static int bluetooth_get_radiosw(void)
2432 int status;
2434 if (!tp_features.bluetooth)
2435 return -ENODEV;
2437 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2438 return -EIO;
2440 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2443 static int bluetooth_set_radiosw(int radio_on)
2445 int status;
2447 if (!tp_features.bluetooth)
2448 return -ENODEV;
2450 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2451 return -EIO;
2452 if (radio_on)
2453 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2454 else
2455 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2456 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2457 return -EIO;
2459 return 0;
2462 /* procfs -------------------------------------------------------------- */
2463 static int bluetooth_read(char *p)
2465 int len = 0;
2466 int status = bluetooth_get_radiosw();
2468 if (!tp_features.bluetooth)
2469 len += sprintf(p + len, "status:\t\tnot supported\n");
2470 else {
2471 len += sprintf(p + len, "status:\t\t%s\n",
2472 (status)? "enabled" : "disabled");
2473 len += sprintf(p + len, "commands:\tenable, disable\n");
2476 return len;
2479 static int bluetooth_write(char *buf)
2481 char *cmd;
2483 if (!tp_features.bluetooth)
2484 return -ENODEV;
2486 while ((cmd = next_cmd(&buf))) {
2487 if (strlencmp(cmd, "enable") == 0) {
2488 bluetooth_set_radiosw(1);
2489 } else if (strlencmp(cmd, "disable") == 0) {
2490 bluetooth_set_radiosw(0);
2491 } else
2492 return -EINVAL;
2495 return 0;
2498 static struct ibm_struct bluetooth_driver_data = {
2499 .name = "bluetooth",
2500 .read = bluetooth_read,
2501 .write = bluetooth_write,
2502 .exit = bluetooth_exit,
2505 /*************************************************************************
2506 * Wan subdriver
2509 enum {
2510 /* ACPI GWAN/SWAN bits */
2511 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2512 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2513 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2516 static int wan_get_radiosw(void);
2517 static int wan_set_radiosw(int radio_on);
2519 /* sysfs wan enable ---------------------------------------------------- */
2520 static ssize_t wan_enable_show(struct device *dev,
2521 struct device_attribute *attr,
2522 char *buf)
2524 int status;
2526 status = wan_get_radiosw();
2527 if (status < 0)
2528 return status;
2530 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2533 static ssize_t wan_enable_store(struct device *dev,
2534 struct device_attribute *attr,
2535 const char *buf, size_t count)
2537 unsigned long t;
2538 int res;
2540 if (parse_strtoul(buf, 1, &t))
2541 return -EINVAL;
2543 res = wan_set_radiosw(t);
2545 return (res) ? res : count;
2548 static struct device_attribute dev_attr_wan_enable =
2549 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2550 wan_enable_show, wan_enable_store);
2552 /* --------------------------------------------------------------------- */
2554 static struct attribute *wan_attributes[] = {
2555 &dev_attr_wan_enable.attr,
2556 NULL
2559 static const struct attribute_group wan_attr_group = {
2560 .attrs = wan_attributes,
2563 static int __init wan_init(struct ibm_init_struct *iibm)
2565 int res;
2566 int status = 0;
2568 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2570 TPACPI_ACPIHANDLE_INIT(hkey);
2572 tp_features.wan = hkey_handle &&
2573 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2575 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2576 str_supported(tp_features.wan),
2577 status);
2579 if (tp_features.wan) {
2580 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2581 /* no wan hardware present in system */
2582 tp_features.wan = 0;
2583 dbg_printk(TPACPI_DBG_INIT,
2584 "wan hardware not installed\n");
2585 } else {
2586 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2587 &wan_attr_group);
2588 if (res)
2589 return res;
2593 return (tp_features.wan)? 0 : 1;
2596 static void wan_exit(void)
2598 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2599 &wan_attr_group);
2602 static int wan_get_radiosw(void)
2604 int status;
2606 if (!tp_features.wan)
2607 return -ENODEV;
2609 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2610 return -EIO;
2612 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2615 static int wan_set_radiosw(int radio_on)
2617 int status;
2619 if (!tp_features.wan)
2620 return -ENODEV;
2622 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2623 return -EIO;
2624 if (radio_on)
2625 status |= TP_ACPI_WANCARD_RADIOSSW;
2626 else
2627 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2628 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2629 return -EIO;
2631 return 0;
2634 /* procfs -------------------------------------------------------------- */
2635 static int wan_read(char *p)
2637 int len = 0;
2638 int status = wan_get_radiosw();
2640 if (!tp_features.wan)
2641 len += sprintf(p + len, "status:\t\tnot supported\n");
2642 else {
2643 len += sprintf(p + len, "status:\t\t%s\n",
2644 (status)? "enabled" : "disabled");
2645 len += sprintf(p + len, "commands:\tenable, disable\n");
2648 return len;
2651 static int wan_write(char *buf)
2653 char *cmd;
2655 if (!tp_features.wan)
2656 return -ENODEV;
2658 while ((cmd = next_cmd(&buf))) {
2659 if (strlencmp(cmd, "enable") == 0) {
2660 wan_set_radiosw(1);
2661 } else if (strlencmp(cmd, "disable") == 0) {
2662 wan_set_radiosw(0);
2663 } else
2664 return -EINVAL;
2667 return 0;
2670 static struct ibm_struct wan_driver_data = {
2671 .name = "wan",
2672 .read = wan_read,
2673 .write = wan_write,
2674 .exit = wan_exit,
2675 .flags.experimental = 1,
2678 /*************************************************************************
2679 * Video subdriver
2682 enum video_access_mode {
2683 TPACPI_VIDEO_NONE = 0,
2684 TPACPI_VIDEO_570, /* 570 */
2685 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2686 TPACPI_VIDEO_NEW, /* all others */
2689 enum { /* video status flags, based on VIDEO_570 */
2690 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2691 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2692 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2695 enum { /* TPACPI_VIDEO_570 constants */
2696 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2697 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2698 * video_status_flags */
2699 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2700 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2703 static enum video_access_mode video_supported;
2704 static int video_orig_autosw;
2706 static int video_autosw_get(void);
2707 static int video_autosw_set(int enable);
2709 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2710 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2711 "\\_SB.PCI0.VID0", /* 770e */
2712 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2713 "\\_SB.PCI0.AGP.VID", /* all others */
2714 ); /* R30, R31 */
2716 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2718 static int __init video_init(struct ibm_init_struct *iibm)
2720 int ivga;
2722 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2724 TPACPI_ACPIHANDLE_INIT(vid);
2725 TPACPI_ACPIHANDLE_INIT(vid2);
2727 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2728 /* G41, assume IVGA doesn't change */
2729 vid_handle = vid2_handle;
2731 if (!vid_handle)
2732 /* video switching not supported on R30, R31 */
2733 video_supported = TPACPI_VIDEO_NONE;
2734 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2735 /* 570 */
2736 video_supported = TPACPI_VIDEO_570;
2737 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2738 /* 600e/x, 770e, 770x */
2739 video_supported = TPACPI_VIDEO_770;
2740 else
2741 /* all others */
2742 video_supported = TPACPI_VIDEO_NEW;
2744 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2745 str_supported(video_supported != TPACPI_VIDEO_NONE),
2746 video_supported);
2748 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2751 static void video_exit(void)
2753 dbg_printk(TPACPI_DBG_EXIT,
2754 "restoring original video autoswitch mode\n");
2755 if (video_autosw_set(video_orig_autosw))
2756 printk(TPACPI_ERR "error while trying to restore original "
2757 "video autoswitch mode\n");
2760 static int video_outputsw_get(void)
2762 int status = 0;
2763 int i;
2765 switch (video_supported) {
2766 case TPACPI_VIDEO_570:
2767 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2768 TP_ACPI_VIDEO_570_PHSCMD))
2769 return -EIO;
2770 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2771 break;
2772 case TPACPI_VIDEO_770:
2773 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2774 return -EIO;
2775 if (i)
2776 status |= TP_ACPI_VIDEO_S_LCD;
2777 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2778 return -EIO;
2779 if (i)
2780 status |= TP_ACPI_VIDEO_S_CRT;
2781 break;
2782 case TPACPI_VIDEO_NEW:
2783 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2784 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2785 return -EIO;
2786 if (i)
2787 status |= TP_ACPI_VIDEO_S_CRT;
2789 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2790 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2791 return -EIO;
2792 if (i)
2793 status |= TP_ACPI_VIDEO_S_LCD;
2794 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2795 return -EIO;
2796 if (i)
2797 status |= TP_ACPI_VIDEO_S_DVI;
2798 break;
2799 default:
2800 return -ENOSYS;
2803 return status;
2806 static int video_outputsw_set(int status)
2808 int autosw;
2809 int res = 0;
2811 switch (video_supported) {
2812 case TPACPI_VIDEO_570:
2813 res = acpi_evalf(NULL, NULL,
2814 "\\_SB.PHS2", "vdd",
2815 TP_ACPI_VIDEO_570_PHS2CMD,
2816 status | TP_ACPI_VIDEO_570_PHS2SET);
2817 break;
2818 case TPACPI_VIDEO_770:
2819 autosw = video_autosw_get();
2820 if (autosw < 0)
2821 return autosw;
2823 res = video_autosw_set(1);
2824 if (res)
2825 return res;
2826 res = acpi_evalf(vid_handle, NULL,
2827 "ASWT", "vdd", status * 0x100, 0);
2828 if (!autosw && video_autosw_set(autosw)) {
2829 printk(TPACPI_ERR
2830 "video auto-switch left enabled due to error\n");
2831 return -EIO;
2833 break;
2834 case TPACPI_VIDEO_NEW:
2835 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2836 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2837 break;
2838 default:
2839 return -ENOSYS;
2842 return (res)? 0 : -EIO;
2845 static int video_autosw_get(void)
2847 int autosw = 0;
2849 switch (video_supported) {
2850 case TPACPI_VIDEO_570:
2851 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2852 return -EIO;
2853 break;
2854 case TPACPI_VIDEO_770:
2855 case TPACPI_VIDEO_NEW:
2856 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2857 return -EIO;
2858 break;
2859 default:
2860 return -ENOSYS;
2863 return autosw & 1;
2866 static int video_autosw_set(int enable)
2868 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2869 return -EIO;
2870 return 0;
2873 static int video_outputsw_cycle(void)
2875 int autosw = video_autosw_get();
2876 int res;
2878 if (autosw < 0)
2879 return autosw;
2881 switch (video_supported) {
2882 case TPACPI_VIDEO_570:
2883 res = video_autosw_set(1);
2884 if (res)
2885 return res;
2886 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2887 break;
2888 case TPACPI_VIDEO_770:
2889 case TPACPI_VIDEO_NEW:
2890 res = video_autosw_set(1);
2891 if (res)
2892 return res;
2893 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2894 break;
2895 default:
2896 return -ENOSYS;
2898 if (!autosw && video_autosw_set(autosw)) {
2899 printk(TPACPI_ERR
2900 "video auto-switch left enabled due to error\n");
2901 return -EIO;
2904 return (res)? 0 : -EIO;
2907 static int video_expand_toggle(void)
2909 switch (video_supported) {
2910 case TPACPI_VIDEO_570:
2911 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2912 0 : -EIO;
2913 case TPACPI_VIDEO_770:
2914 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2915 0 : -EIO;
2916 case TPACPI_VIDEO_NEW:
2917 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2918 0 : -EIO;
2919 default:
2920 return -ENOSYS;
2922 /* not reached */
2925 static int video_read(char *p)
2927 int status, autosw;
2928 int len = 0;
2930 if (video_supported == TPACPI_VIDEO_NONE) {
2931 len += sprintf(p + len, "status:\t\tnot supported\n");
2932 return len;
2935 status = video_outputsw_get();
2936 if (status < 0)
2937 return status;
2939 autosw = video_autosw_get();
2940 if (autosw < 0)
2941 return autosw;
2943 len += sprintf(p + len, "status:\t\tsupported\n");
2944 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2945 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2946 if (video_supported == TPACPI_VIDEO_NEW)
2947 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2948 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2949 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2950 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2951 if (video_supported == TPACPI_VIDEO_NEW)
2952 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2953 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2954 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2956 return len;
2959 static int video_write(char *buf)
2961 char *cmd;
2962 int enable, disable, status;
2963 int res;
2965 if (video_supported == TPACPI_VIDEO_NONE)
2966 return -ENODEV;
2968 enable = 0;
2969 disable = 0;
2971 while ((cmd = next_cmd(&buf))) {
2972 if (strlencmp(cmd, "lcd_enable") == 0) {
2973 enable |= TP_ACPI_VIDEO_S_LCD;
2974 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2975 disable |= TP_ACPI_VIDEO_S_LCD;
2976 } else if (strlencmp(cmd, "crt_enable") == 0) {
2977 enable |= TP_ACPI_VIDEO_S_CRT;
2978 } else if (strlencmp(cmd, "crt_disable") == 0) {
2979 disable |= TP_ACPI_VIDEO_S_CRT;
2980 } else if (video_supported == TPACPI_VIDEO_NEW &&
2981 strlencmp(cmd, "dvi_enable") == 0) {
2982 enable |= TP_ACPI_VIDEO_S_DVI;
2983 } else if (video_supported == TPACPI_VIDEO_NEW &&
2984 strlencmp(cmd, "dvi_disable") == 0) {
2985 disable |= TP_ACPI_VIDEO_S_DVI;
2986 } else if (strlencmp(cmd, "auto_enable") == 0) {
2987 res = video_autosw_set(1);
2988 if (res)
2989 return res;
2990 } else if (strlencmp(cmd, "auto_disable") == 0) {
2991 res = video_autosw_set(0);
2992 if (res)
2993 return res;
2994 } else if (strlencmp(cmd, "video_switch") == 0) {
2995 res = video_outputsw_cycle();
2996 if (res)
2997 return res;
2998 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2999 res = video_expand_toggle();
3000 if (res)
3001 return res;
3002 } else
3003 return -EINVAL;
3006 if (enable || disable) {
3007 status = video_outputsw_get();
3008 if (status < 0)
3009 return status;
3010 res = video_outputsw_set((status & ~disable) | enable);
3011 if (res)
3012 return res;
3015 return 0;
3018 static struct ibm_struct video_driver_data = {
3019 .name = "video",
3020 .read = video_read,
3021 .write = video_write,
3022 .exit = video_exit,
3025 /*************************************************************************
3026 * Light (thinklight) subdriver
3029 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3030 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3032 static int __init light_init(struct ibm_init_struct *iibm)
3034 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3036 TPACPI_ACPIHANDLE_INIT(ledb);
3037 TPACPI_ACPIHANDLE_INIT(lght);
3038 TPACPI_ACPIHANDLE_INIT(cmos);
3040 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3041 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3043 if (tp_features.light)
3044 /* light status not supported on
3045 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3046 tp_features.light_status =
3047 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3049 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3050 str_supported(tp_features.light));
3052 return (tp_features.light)? 0 : 1;
3055 static int light_read(char *p)
3057 int len = 0;
3058 int status = 0;
3060 if (!tp_features.light) {
3061 len += sprintf(p + len, "status:\t\tnot supported\n");
3062 } else if (!tp_features.light_status) {
3063 len += sprintf(p + len, "status:\t\tunknown\n");
3064 len += sprintf(p + len, "commands:\ton, off\n");
3065 } else {
3066 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3067 return -EIO;
3068 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3069 len += sprintf(p + len, "commands:\ton, off\n");
3072 return len;
3075 static int light_write(char *buf)
3077 int cmos_cmd, lght_cmd;
3078 char *cmd;
3079 int success;
3081 if (!tp_features.light)
3082 return -ENODEV;
3084 while ((cmd = next_cmd(&buf))) {
3085 if (strlencmp(cmd, "on") == 0) {
3086 cmos_cmd = 0x0c;
3087 lght_cmd = 1;
3088 } else if (strlencmp(cmd, "off") == 0) {
3089 cmos_cmd = 0x0d;
3090 lght_cmd = 0;
3091 } else
3092 return -EINVAL;
3094 success = cmos_handle ?
3095 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3096 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3097 if (!success)
3098 return -EIO;
3101 return 0;
3104 static struct ibm_struct light_driver_data = {
3105 .name = "light",
3106 .read = light_read,
3107 .write = light_write,
3110 /*************************************************************************
3111 * Dock subdriver
3114 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3116 static void dock_notify(struct ibm_struct *ibm, u32 event);
3117 static int dock_read(char *p);
3118 static int dock_write(char *buf);
3120 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3121 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3122 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3123 "\\_SB.PCI.ISA.SLCE", /* 570 */
3124 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3126 /* don't list other alternatives as we install a notify handler on the 570 */
3127 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3129 static const struct acpi_device_id ibm_pci_device_ids[] = {
3130 {PCI_ROOT_HID_STRING, 0},
3131 {"", 0},
3134 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3136 .notify = dock_notify,
3137 .handle = &dock_handle,
3138 .type = ACPI_SYSTEM_NOTIFY,
3141 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3142 * We just use it to get notifications of dock hotplug
3143 * in very old thinkpads */
3144 .hid = ibm_pci_device_ids,
3145 .notify = dock_notify,
3146 .handle = &pci_handle,
3147 .type = ACPI_SYSTEM_NOTIFY,
3151 static struct ibm_struct dock_driver_data[2] = {
3153 .name = "dock",
3154 .read = dock_read,
3155 .write = dock_write,
3156 .acpi = &ibm_dock_acpidriver[0],
3159 .name = "dock",
3160 .acpi = &ibm_dock_acpidriver[1],
3164 #define dock_docked() (_sta(dock_handle) & 1)
3166 static int __init dock_init(struct ibm_init_struct *iibm)
3168 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3170 TPACPI_ACPIHANDLE_INIT(dock);
3172 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3173 str_supported(dock_handle != NULL));
3175 return (dock_handle)? 0 : 1;
3178 static int __init dock_init2(struct ibm_init_struct *iibm)
3180 int dock2_needed;
3182 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3184 if (dock_driver_data[0].flags.acpi_driver_registered &&
3185 dock_driver_data[0].flags.acpi_notify_installed) {
3186 TPACPI_ACPIHANDLE_INIT(pci);
3187 dock2_needed = (pci_handle != NULL);
3188 vdbg_printk(TPACPI_DBG_INIT,
3189 "dock PCI handler for the TP 570 is %s\n",
3190 str_supported(dock2_needed));
3191 } else {
3192 vdbg_printk(TPACPI_DBG_INIT,
3193 "dock subdriver part 2 not required\n");
3194 dock2_needed = 0;
3197 return (dock2_needed)? 0 : 1;
3200 static void dock_notify(struct ibm_struct *ibm, u32 event)
3202 int docked = dock_docked();
3203 int pci = ibm->acpi->hid && ibm->acpi->device &&
3204 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3205 int data;
3207 if (event == 1 && !pci) /* 570 */
3208 data = 1; /* button */
3209 else if (event == 1 && pci) /* 570 */
3210 data = 3; /* dock */
3211 else if (event == 3 && docked)
3212 data = 1; /* button */
3213 else if (event == 3 && !docked)
3214 data = 2; /* undock */
3215 else if (event == 0 && docked)
3216 data = 3; /* dock */
3217 else {
3218 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3219 event, _sta(dock_handle));
3220 data = 0; /* unknown */
3222 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3223 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3224 ibm->acpi->device->dev.bus_id,
3225 event, data);
3228 static int dock_read(char *p)
3230 int len = 0;
3231 int docked = dock_docked();
3233 if (!dock_handle)
3234 len += sprintf(p + len, "status:\t\tnot supported\n");
3235 else if (!docked)
3236 len += sprintf(p + len, "status:\t\tundocked\n");
3237 else {
3238 len += sprintf(p + len, "status:\t\tdocked\n");
3239 len += sprintf(p + len, "commands:\tdock, undock\n");
3242 return len;
3245 static int dock_write(char *buf)
3247 char *cmd;
3249 if (!dock_docked())
3250 return -ENODEV;
3252 while ((cmd = next_cmd(&buf))) {
3253 if (strlencmp(cmd, "undock") == 0) {
3254 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3255 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3256 return -EIO;
3257 } else if (strlencmp(cmd, "dock") == 0) {
3258 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3259 return -EIO;
3260 } else
3261 return -EINVAL;
3264 return 0;
3267 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3269 /*************************************************************************
3270 * Bay subdriver
3273 #ifdef CONFIG_THINKPAD_ACPI_BAY
3275 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3276 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3277 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3278 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3279 ); /* A21e, R30, R31 */
3280 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3281 "_EJ0", /* all others */
3282 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3283 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3284 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3285 ); /* all others */
3286 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3287 "_EJ0", /* 770x */
3288 ); /* all others */
3290 static int __init bay_init(struct ibm_init_struct *iibm)
3292 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3294 TPACPI_ACPIHANDLE_INIT(bay);
3295 if (bay_handle)
3296 TPACPI_ACPIHANDLE_INIT(bay_ej);
3297 TPACPI_ACPIHANDLE_INIT(bay2);
3298 if (bay2_handle)
3299 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3301 tp_features.bay_status = bay_handle &&
3302 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3303 tp_features.bay_status2 = bay2_handle &&
3304 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3306 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3307 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3308 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3309 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3311 vdbg_printk(TPACPI_DBG_INIT,
3312 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3313 str_supported(tp_features.bay_status),
3314 str_supported(tp_features.bay_eject),
3315 str_supported(tp_features.bay_status2),
3316 str_supported(tp_features.bay_eject2));
3318 return (tp_features.bay_status || tp_features.bay_eject ||
3319 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3322 static void bay_notify(struct ibm_struct *ibm, u32 event)
3324 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3325 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3326 ibm->acpi->device->dev.bus_id,
3327 event, 0);
3330 #define bay_occupied(b) (_sta(b##_handle) & 1)
3332 static int bay_read(char *p)
3334 int len = 0;
3335 int occupied = bay_occupied(bay);
3336 int occupied2 = bay_occupied(bay2);
3337 int eject, eject2;
3339 len += sprintf(p + len, "status:\t\t%s\n",
3340 tp_features.bay_status ?
3341 (occupied ? "occupied" : "unoccupied") :
3342 "not supported");
3343 if (tp_features.bay_status2)
3344 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3345 "occupied" : "unoccupied");
3347 eject = tp_features.bay_eject && occupied;
3348 eject2 = tp_features.bay_eject2 && occupied2;
3350 if (eject && eject2)
3351 len += sprintf(p + len, "commands:\teject, eject2\n");
3352 else if (eject)
3353 len += sprintf(p + len, "commands:\teject\n");
3354 else if (eject2)
3355 len += sprintf(p + len, "commands:\teject2\n");
3357 return len;
3360 static int bay_write(char *buf)
3362 char *cmd;
3364 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3365 return -ENODEV;
3367 while ((cmd = next_cmd(&buf))) {
3368 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3369 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3370 return -EIO;
3371 } else if (tp_features.bay_eject2 &&
3372 strlencmp(cmd, "eject2") == 0) {
3373 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3374 return -EIO;
3375 } else
3376 return -EINVAL;
3379 return 0;
3382 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3383 .notify = bay_notify,
3384 .handle = &bay_handle,
3385 .type = ACPI_SYSTEM_NOTIFY,
3388 static struct ibm_struct bay_driver_data = {
3389 .name = "bay",
3390 .read = bay_read,
3391 .write = bay_write,
3392 .acpi = &ibm_bay_acpidriver,
3395 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3397 /*************************************************************************
3398 * CMOS subdriver
3401 /* sysfs cmos_command -------------------------------------------------- */
3402 static ssize_t cmos_command_store(struct device *dev,
3403 struct device_attribute *attr,
3404 const char *buf, size_t count)
3406 unsigned long cmos_cmd;
3407 int res;
3409 if (parse_strtoul(buf, 21, &cmos_cmd))
3410 return -EINVAL;
3412 res = issue_thinkpad_cmos_command(cmos_cmd);
3413 return (res)? res : count;
3416 static struct device_attribute dev_attr_cmos_command =
3417 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3419 /* --------------------------------------------------------------------- */
3421 static int __init cmos_init(struct ibm_init_struct *iibm)
3423 int res;
3425 vdbg_printk(TPACPI_DBG_INIT,
3426 "initializing cmos commands subdriver\n");
3428 TPACPI_ACPIHANDLE_INIT(cmos);
3430 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3431 str_supported(cmos_handle != NULL));
3433 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3434 if (res)
3435 return res;
3437 return (cmos_handle)? 0 : 1;
3440 static void cmos_exit(void)
3442 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3445 static int cmos_read(char *p)
3447 int len = 0;
3449 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3450 R30, R31, T20-22, X20-21 */
3451 if (!cmos_handle)
3452 len += sprintf(p + len, "status:\t\tnot supported\n");
3453 else {
3454 len += sprintf(p + len, "status:\t\tsupported\n");
3455 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3458 return len;
3461 static int cmos_write(char *buf)
3463 char *cmd;
3464 int cmos_cmd, res;
3466 while ((cmd = next_cmd(&buf))) {
3467 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3468 cmos_cmd >= 0 && cmos_cmd <= 21) {
3469 /* cmos_cmd set */
3470 } else
3471 return -EINVAL;
3473 res = issue_thinkpad_cmos_command(cmos_cmd);
3474 if (res)
3475 return res;
3478 return 0;
3481 static struct ibm_struct cmos_driver_data = {
3482 .name = "cmos",
3483 .read = cmos_read,
3484 .write = cmos_write,
3485 .exit = cmos_exit,
3488 /*************************************************************************
3489 * LED subdriver
3492 enum led_access_mode {
3493 TPACPI_LED_NONE = 0,
3494 TPACPI_LED_570, /* 570 */
3495 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3496 TPACPI_LED_NEW, /* all others */
3499 enum { /* For TPACPI_LED_OLD */
3500 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3501 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3502 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3505 static enum led_access_mode led_supported;
3507 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3508 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3509 /* T20-22, X20-21 */
3510 "LED", /* all others */
3511 ); /* R30, R31 */
3513 static int __init led_init(struct ibm_init_struct *iibm)
3515 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3517 TPACPI_ACPIHANDLE_INIT(led);
3519 if (!led_handle)
3520 /* led not supported on R30, R31 */
3521 led_supported = TPACPI_LED_NONE;
3522 else if (strlencmp(led_path, "SLED") == 0)
3523 /* 570 */
3524 led_supported = TPACPI_LED_570;
3525 else if (strlencmp(led_path, "SYSL") == 0)
3526 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3527 led_supported = TPACPI_LED_OLD;
3528 else
3529 /* all others */
3530 led_supported = TPACPI_LED_NEW;
3532 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3533 str_supported(led_supported), led_supported);
3535 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3538 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3540 static int led_read(char *p)
3542 int len = 0;
3544 if (!led_supported) {
3545 len += sprintf(p + len, "status:\t\tnot supported\n");
3546 return len;
3548 len += sprintf(p + len, "status:\t\tsupported\n");
3550 if (led_supported == TPACPI_LED_570) {
3551 /* 570 */
3552 int i, status;
3553 for (i = 0; i < 8; i++) {
3554 if (!acpi_evalf(ec_handle,
3555 &status, "GLED", "dd", 1 << i))
3556 return -EIO;
3557 len += sprintf(p + len, "%d:\t\t%s\n",
3558 i, led_status(status));
3562 len += sprintf(p + len, "commands:\t"
3563 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3565 return len;
3568 /* off, on, blink */
3569 static const int led_sled_arg1[] = { 0, 1, 3 };
3570 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3571 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3572 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3574 static int led_write(char *buf)
3576 char *cmd;
3577 int led, ind, ret;
3579 if (!led_supported)
3580 return -ENODEV;
3582 while ((cmd = next_cmd(&buf))) {
3583 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3584 return -EINVAL;
3586 if (strstr(cmd, "off")) {
3587 ind = 0;
3588 } else if (strstr(cmd, "on")) {
3589 ind = 1;
3590 } else if (strstr(cmd, "blink")) {
3591 ind = 2;
3592 } else
3593 return -EINVAL;
3595 if (led_supported == TPACPI_LED_570) {
3596 /* 570 */
3597 led = 1 << led;
3598 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3599 led, led_sled_arg1[ind]))
3600 return -EIO;
3601 } else if (led_supported == TPACPI_LED_OLD) {
3602 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3603 led = 1 << led;
3604 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3605 if (ret >= 0)
3606 ret = ec_write(TPACPI_LED_EC_HLBL,
3607 led * led_exp_hlbl[ind]);
3608 if (ret >= 0)
3609 ret = ec_write(TPACPI_LED_EC_HLCL,
3610 led * led_exp_hlcl[ind]);
3611 if (ret < 0)
3612 return ret;
3613 } else {
3614 /* all others */
3615 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3616 led, led_led_arg1[ind]))
3617 return -EIO;
3621 return 0;
3624 static struct ibm_struct led_driver_data = {
3625 .name = "led",
3626 .read = led_read,
3627 .write = led_write,
3630 /*************************************************************************
3631 * Beep subdriver
3634 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3636 static int __init beep_init(struct ibm_init_struct *iibm)
3638 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3640 TPACPI_ACPIHANDLE_INIT(beep);
3642 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3643 str_supported(beep_handle != NULL));
3645 return (beep_handle)? 0 : 1;
3648 static int beep_read(char *p)
3650 int len = 0;
3652 if (!beep_handle)
3653 len += sprintf(p + len, "status:\t\tnot supported\n");
3654 else {
3655 len += sprintf(p + len, "status:\t\tsupported\n");
3656 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3659 return len;
3662 static int beep_write(char *buf)
3664 char *cmd;
3665 int beep_cmd;
3667 if (!beep_handle)
3668 return -ENODEV;
3670 while ((cmd = next_cmd(&buf))) {
3671 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3672 beep_cmd >= 0 && beep_cmd <= 17) {
3673 /* beep_cmd set */
3674 } else
3675 return -EINVAL;
3676 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3677 return -EIO;
3680 return 0;
3683 static struct ibm_struct beep_driver_data = {
3684 .name = "beep",
3685 .read = beep_read,
3686 .write = beep_write,
3689 /*************************************************************************
3690 * Thermal subdriver
3693 enum thermal_access_mode {
3694 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3695 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3696 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3697 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3698 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3701 enum { /* TPACPI_THERMAL_TPEC_* */
3702 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3703 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3704 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3707 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3708 struct ibm_thermal_sensors_struct {
3709 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3712 static enum thermal_access_mode thermal_read_mode;
3714 /* idx is zero-based */
3715 static int thermal_get_sensor(int idx, s32 *value)
3717 int t;
3718 s8 tmp;
3719 char tmpi[5];
3721 t = TP_EC_THERMAL_TMP0;
3723 switch (thermal_read_mode) {
3724 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3725 case TPACPI_THERMAL_TPEC_16:
3726 if (idx >= 8 && idx <= 15) {
3727 t = TP_EC_THERMAL_TMP8;
3728 idx -= 8;
3730 /* fallthrough */
3731 #endif
3732 case TPACPI_THERMAL_TPEC_8:
3733 if (idx <= 7) {
3734 if (!acpi_ec_read(t + idx, &tmp))
3735 return -EIO;
3736 *value = tmp * 1000;
3737 return 0;
3739 break;
3741 case TPACPI_THERMAL_ACPI_UPDT:
3742 if (idx <= 7) {
3743 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3744 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3745 return -EIO;
3746 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3747 return -EIO;
3748 *value = (t - 2732) * 100;
3749 return 0;
3751 break;
3753 case TPACPI_THERMAL_ACPI_TMP07:
3754 if (idx <= 7) {
3755 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3756 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3757 return -EIO;
3758 if (t > 127 || t < -127)
3759 t = TP_EC_THERMAL_TMP_NA;
3760 *value = t * 1000;
3761 return 0;
3763 break;
3765 case TPACPI_THERMAL_NONE:
3766 default:
3767 return -ENOSYS;
3770 return -EINVAL;
3773 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3775 int res, i;
3776 int n;
3778 n = 8;
3779 i = 0;
3781 if (!s)
3782 return -EINVAL;
3784 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3785 n = 16;
3787 for (i = 0 ; i < n; i++) {
3788 res = thermal_get_sensor(i, &s->temp[i]);
3789 if (res)
3790 return res;
3793 return n;
3796 /* sysfs temp##_input -------------------------------------------------- */
3798 static ssize_t thermal_temp_input_show(struct device *dev,
3799 struct device_attribute *attr,
3800 char *buf)
3802 struct sensor_device_attribute *sensor_attr =
3803 to_sensor_dev_attr(attr);
3804 int idx = sensor_attr->index;
3805 s32 value;
3806 int res;
3808 res = thermal_get_sensor(idx, &value);
3809 if (res)
3810 return res;
3811 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3812 return -ENXIO;
3814 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3817 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3818 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3819 thermal_temp_input_show, NULL, _idxB)
3821 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3822 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3823 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3824 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3825 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3826 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3827 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3828 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3829 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3830 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3831 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3832 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3833 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3834 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3835 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3836 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3837 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3840 #define THERMAL_ATTRS(X) \
3841 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3843 static struct attribute *thermal_temp_input_attr[] = {
3844 THERMAL_ATTRS(8),
3845 THERMAL_ATTRS(9),
3846 THERMAL_ATTRS(10),
3847 THERMAL_ATTRS(11),
3848 THERMAL_ATTRS(12),
3849 THERMAL_ATTRS(13),
3850 THERMAL_ATTRS(14),
3851 THERMAL_ATTRS(15),
3852 THERMAL_ATTRS(0),
3853 THERMAL_ATTRS(1),
3854 THERMAL_ATTRS(2),
3855 THERMAL_ATTRS(3),
3856 THERMAL_ATTRS(4),
3857 THERMAL_ATTRS(5),
3858 THERMAL_ATTRS(6),
3859 THERMAL_ATTRS(7),
3860 NULL
3863 static const struct attribute_group thermal_temp_input16_group = {
3864 .attrs = thermal_temp_input_attr
3867 static const struct attribute_group thermal_temp_input8_group = {
3868 .attrs = &thermal_temp_input_attr[8]
3871 #undef THERMAL_SENSOR_ATTR_TEMP
3872 #undef THERMAL_ATTRS
3874 /* --------------------------------------------------------------------- */
3876 static int __init thermal_init(struct ibm_init_struct *iibm)
3878 u8 t, ta1, ta2;
3879 int i;
3880 int acpi_tmp7;
3881 int res;
3883 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3885 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3887 if (thinkpad_id.ec_model) {
3889 * Direct EC access mode: sensors at registers
3890 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3891 * non-implemented, thermal sensors return 0x80 when
3892 * not available
3895 ta1 = ta2 = 0;
3896 for (i = 0; i < 8; i++) {
3897 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3898 ta1 |= t;
3899 } else {
3900 ta1 = 0;
3901 break;
3903 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3904 ta2 |= t;
3905 } else {
3906 ta1 = 0;
3907 break;
3910 if (ta1 == 0) {
3911 /* This is sheer paranoia, but we handle it anyway */
3912 if (acpi_tmp7) {
3913 printk(TPACPI_ERR
3914 "ThinkPad ACPI EC access misbehaving, "
3915 "falling back to ACPI TMPx access "
3916 "mode\n");
3917 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3918 } else {
3919 printk(TPACPI_ERR
3920 "ThinkPad ACPI EC access misbehaving, "
3921 "disabling thermal sensors access\n");
3922 thermal_read_mode = TPACPI_THERMAL_NONE;
3924 } else {
3925 thermal_read_mode =
3926 (ta2 != 0) ?
3927 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3929 } else if (acpi_tmp7) {
3930 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3931 /* 600e/x, 770e, 770x */
3932 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3933 } else {
3934 /* Standard ACPI TMPx access, max 8 sensors */
3935 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3937 } else {
3938 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3939 thermal_read_mode = TPACPI_THERMAL_NONE;
3942 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3943 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3944 thermal_read_mode);
3946 switch (thermal_read_mode) {
3947 case TPACPI_THERMAL_TPEC_16:
3948 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3949 &thermal_temp_input16_group);
3950 if (res)
3951 return res;
3952 break;
3953 case TPACPI_THERMAL_TPEC_8:
3954 case TPACPI_THERMAL_ACPI_TMP07:
3955 case TPACPI_THERMAL_ACPI_UPDT:
3956 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3957 &thermal_temp_input8_group);
3958 if (res)
3959 return res;
3960 break;
3961 case TPACPI_THERMAL_NONE:
3962 default:
3963 return 1;
3966 return 0;
3969 static void thermal_exit(void)
3971 switch (thermal_read_mode) {
3972 case TPACPI_THERMAL_TPEC_16:
3973 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3974 &thermal_temp_input16_group);
3975 break;
3976 case TPACPI_THERMAL_TPEC_8:
3977 case TPACPI_THERMAL_ACPI_TMP07:
3978 case TPACPI_THERMAL_ACPI_UPDT:
3979 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3980 &thermal_temp_input16_group);
3981 break;
3982 case TPACPI_THERMAL_NONE:
3983 default:
3984 break;
3988 static int thermal_read(char *p)
3990 int len = 0;
3991 int n, i;
3992 struct ibm_thermal_sensors_struct t;
3994 n = thermal_get_sensors(&t);
3995 if (unlikely(n < 0))
3996 return n;
3998 len += sprintf(p + len, "temperatures:\t");
4000 if (n > 0) {
4001 for (i = 0; i < (n - 1); i++)
4002 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4003 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4004 } else
4005 len += sprintf(p + len, "not supported\n");
4007 return len;
4010 static struct ibm_struct thermal_driver_data = {
4011 .name = "thermal",
4012 .read = thermal_read,
4013 .exit = thermal_exit,
4016 /*************************************************************************
4017 * EC Dump subdriver
4020 static u8 ecdump_regs[256];
4022 static int ecdump_read(char *p)
4024 int len = 0;
4025 int i, j;
4026 u8 v;
4028 len += sprintf(p + len, "EC "
4029 " +00 +01 +02 +03 +04 +05 +06 +07"
4030 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4031 for (i = 0; i < 256; i += 16) {
4032 len += sprintf(p + len, "EC 0x%02x:", i);
4033 for (j = 0; j < 16; j++) {
4034 if (!acpi_ec_read(i + j, &v))
4035 break;
4036 if (v != ecdump_regs[i + j])
4037 len += sprintf(p + len, " *%02x", v);
4038 else
4039 len += sprintf(p + len, " %02x", v);
4040 ecdump_regs[i + j] = v;
4042 len += sprintf(p + len, "\n");
4043 if (j != 16)
4044 break;
4047 /* These are way too dangerous to advertise openly... */
4048 #if 0
4049 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4050 " (<offset> is 00-ff, <value> is 00-ff)\n");
4051 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4052 " (<offset> is 00-ff, <value> is 0-255)\n");
4053 #endif
4054 return len;
4057 static int ecdump_write(char *buf)
4059 char *cmd;
4060 int i, v;
4062 while ((cmd = next_cmd(&buf))) {
4063 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4064 /* i and v set */
4065 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4066 /* i and v set */
4067 } else
4068 return -EINVAL;
4069 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4070 if (!acpi_ec_write(i, v))
4071 return -EIO;
4072 } else
4073 return -EINVAL;
4076 return 0;
4079 static struct ibm_struct ecdump_driver_data = {
4080 .name = "ecdump",
4081 .read = ecdump_read,
4082 .write = ecdump_write,
4083 .flags.experimental = 1,
4086 /*************************************************************************
4087 * Backlight/brightness subdriver
4090 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4092 static struct backlight_device *ibm_backlight_device;
4093 static int brightness_offset = 0x31;
4094 static int brightness_mode;
4095 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4097 static struct mutex brightness_mutex;
4100 * ThinkPads can read brightness from two places: EC 0x31, or
4101 * CMOS NVRAM byte 0x5E, bits 0-3.
4103 static int brightness_get(struct backlight_device *bd)
4105 u8 lec = 0, lcmos = 0, level = 0;
4107 if (brightness_mode & 1) {
4108 if (!acpi_ec_read(brightness_offset, &lec))
4109 return -EIO;
4110 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4111 level = lec;
4113 if (brightness_mode & 2) {
4114 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4115 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4116 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4117 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4118 level = lcmos;
4121 if (brightness_mode == 3 && lec != lcmos) {
4122 printk(TPACPI_ERR
4123 "CMOS NVRAM (%u) and EC (%u) do not agree "
4124 "on display brightness level\n",
4125 (unsigned int) lcmos,
4126 (unsigned int) lec);
4127 return -EIO;
4130 return level;
4133 /* May return EINTR which can always be mapped to ERESTARTSYS */
4134 static int brightness_set(int value)
4136 int cmos_cmd, inc, i, res;
4137 int current_value;
4139 if (value > ((tp_features.bright_16levels)? 15 : 7))
4140 return -EINVAL;
4142 res = mutex_lock_interruptible(&brightness_mutex);
4143 if (res < 0)
4144 return res;
4146 current_value = brightness_get(NULL);
4147 if (current_value < 0) {
4148 res = current_value;
4149 goto errout;
4152 cmos_cmd = value > current_value ?
4153 TP_CMOS_BRIGHTNESS_UP :
4154 TP_CMOS_BRIGHTNESS_DOWN;
4155 inc = (value > current_value)? 1 : -1;
4157 res = 0;
4158 for (i = current_value; i != value; i += inc) {
4159 if ((brightness_mode & 2) &&
4160 issue_thinkpad_cmos_command(cmos_cmd)) {
4161 res = -EIO;
4162 goto errout;
4164 if ((brightness_mode & 1) &&
4165 !acpi_ec_write(brightness_offset, i + inc)) {
4166 res = -EIO;
4167 goto errout;;
4171 errout:
4172 mutex_unlock(&brightness_mutex);
4173 return res;
4176 /* sysfs backlight class ----------------------------------------------- */
4178 static int brightness_update_status(struct backlight_device *bd)
4180 /* it is the backlight class's job (caller) to handle
4181 * EINTR and other errors properly */
4182 return brightness_set(
4183 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4184 bd->props.power == FB_BLANK_UNBLANK) ?
4185 bd->props.brightness : 0);
4188 static struct backlight_ops ibm_backlight_data = {
4189 .get_brightness = brightness_get,
4190 .update_status = brightness_update_status,
4193 /* --------------------------------------------------------------------- */
4195 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4197 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4198 union acpi_object *obj;
4199 int rc;
4201 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4202 obj = (union acpi_object *)buffer.pointer;
4203 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4204 printk(TPACPI_ERR "Unknown BCLL data, "
4205 "please report this to %s\n", TPACPI_MAIL);
4206 rc = 0;
4207 } else {
4208 rc = obj->package.count;
4210 } else {
4211 return 0;
4214 kfree(buffer.pointer);
4215 return rc;
4218 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4219 void *context, void **rv)
4221 char name[ACPI_PATH_SEGMENT_LENGTH];
4222 struct acpi_buffer buffer = { sizeof(name), &name };
4224 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4225 !strncmp("BCLL", name, sizeof(name) - 1)) {
4226 if (tpacpi_query_bcll_levels(handle) == 16) {
4227 *rv = handle;
4228 return AE_CTRL_TERMINATE;
4229 } else {
4230 return AE_OK;
4232 } else {
4233 return AE_OK;
4237 static int __init brightness_check_levels(void)
4239 int status;
4240 void *found_node = NULL;
4242 if (!vid_handle) {
4243 TPACPI_ACPIHANDLE_INIT(vid);
4245 if (!vid_handle)
4246 return 0;
4248 /* Search for a BCLL package with 16 levels */
4249 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4250 brightness_find_bcll, NULL,
4251 &found_node);
4253 return (ACPI_SUCCESS(status) && found_node != NULL);
4256 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4257 void *context, void **rv)
4259 char name[ACPI_PATH_SEGMENT_LENGTH];
4260 struct acpi_buffer buffer = { sizeof(name), &name };
4262 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4263 !strncmp("_BCL", name, sizeof(name) - 1)) {
4264 *rv = handle;
4265 return AE_CTRL_TERMINATE;
4266 } else {
4267 return AE_OK;
4271 static int __init brightness_check_std_acpi_support(void)
4273 int status;
4274 void *found_node = NULL;
4276 if (!vid_handle) {
4277 TPACPI_ACPIHANDLE_INIT(vid);
4279 if (!vid_handle)
4280 return 0;
4282 /* Search for a _BCL method, but don't execute it */
4283 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4284 brightness_find_bcl, NULL, &found_node);
4286 return (ACPI_SUCCESS(status) && found_node != NULL);
4289 static int __init brightness_init(struct ibm_init_struct *iibm)
4291 int b;
4293 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4295 mutex_init(&brightness_mutex);
4297 if (!brightness_enable) {
4298 dbg_printk(TPACPI_DBG_INIT,
4299 "brightness support disabled by "
4300 "module parameter\n");
4301 return 1;
4302 } else if (brightness_enable > 1) {
4303 if (brightness_check_std_acpi_support()) {
4304 printk(TPACPI_NOTICE
4305 "standard ACPI backlight interface "
4306 "available, not loading native one...\n");
4307 return 1;
4311 if (!brightness_mode) {
4312 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4313 brightness_mode = 2;
4314 else
4315 brightness_mode = 3;
4317 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4318 brightness_mode);
4321 if (brightness_mode > 3)
4322 return -EINVAL;
4324 tp_features.bright_16levels =
4325 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4326 brightness_check_levels();
4328 b = brightness_get(NULL);
4329 if (b < 0)
4330 return 1;
4332 if (tp_features.bright_16levels)
4333 printk(TPACPI_INFO
4334 "detected a 16-level brightness capable ThinkPad\n");
4336 ibm_backlight_device = backlight_device_register(
4337 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4338 &ibm_backlight_data);
4339 if (IS_ERR(ibm_backlight_device)) {
4340 printk(TPACPI_ERR "Could not register backlight device\n");
4341 return PTR_ERR(ibm_backlight_device);
4343 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4345 ibm_backlight_device->props.max_brightness =
4346 (tp_features.bright_16levels)? 15 : 7;
4347 ibm_backlight_device->props.brightness = b;
4348 backlight_update_status(ibm_backlight_device);
4350 return 0;
4353 static void brightness_exit(void)
4355 if (ibm_backlight_device) {
4356 vdbg_printk(TPACPI_DBG_EXIT,
4357 "calling backlight_device_unregister()\n");
4358 backlight_device_unregister(ibm_backlight_device);
4359 ibm_backlight_device = NULL;
4363 static int brightness_read(char *p)
4365 int len = 0;
4366 int level;
4368 level = brightness_get(NULL);
4369 if (level < 0) {
4370 len += sprintf(p + len, "level:\t\tunreadable\n");
4371 } else {
4372 len += sprintf(p + len, "level:\t\t%d\n", level);
4373 len += sprintf(p + len, "commands:\tup, down\n");
4374 len += sprintf(p + len, "commands:\tlevel <level>"
4375 " (<level> is 0-%d)\n",
4376 (tp_features.bright_16levels) ? 15 : 7);
4379 return len;
4382 static int brightness_write(char *buf)
4384 int level;
4385 int rc;
4386 char *cmd;
4387 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4389 level = brightness_get(NULL);
4390 if (level < 0)
4391 return level;
4393 while ((cmd = next_cmd(&buf))) {
4394 if (strlencmp(cmd, "up") == 0) {
4395 if (level < max_level)
4396 level++;
4397 } else if (strlencmp(cmd, "down") == 0) {
4398 if (level > 0)
4399 level--;
4400 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4401 level >= 0 && level <= max_level) {
4402 /* new level set */
4403 } else
4404 return -EINVAL;
4408 * Now we know what the final level should be, so we try to set it.
4409 * Doing it this way makes the syscall restartable in case of EINTR
4411 rc = brightness_set(level);
4412 return (rc == -EINTR)? ERESTARTSYS : rc;
4415 static struct ibm_struct brightness_driver_data = {
4416 .name = "brightness",
4417 .read = brightness_read,
4418 .write = brightness_write,
4419 .exit = brightness_exit,
4422 /*************************************************************************
4423 * Volume subdriver
4426 static int volume_offset = 0x30;
4428 static int volume_read(char *p)
4430 int len = 0;
4431 u8 level;
4433 if (!acpi_ec_read(volume_offset, &level)) {
4434 len += sprintf(p + len, "level:\t\tunreadable\n");
4435 } else {
4436 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4437 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4438 len += sprintf(p + len, "commands:\tup, down, mute\n");
4439 len += sprintf(p + len, "commands:\tlevel <level>"
4440 " (<level> is 0-15)\n");
4443 return len;
4446 static int volume_write(char *buf)
4448 int cmos_cmd, inc, i;
4449 u8 level, mute;
4450 int new_level, new_mute;
4451 char *cmd;
4453 while ((cmd = next_cmd(&buf))) {
4454 if (!acpi_ec_read(volume_offset, &level))
4455 return -EIO;
4456 new_mute = mute = level & 0x40;
4457 new_level = level = level & 0xf;
4459 if (strlencmp(cmd, "up") == 0) {
4460 if (mute)
4461 new_mute = 0;
4462 else
4463 new_level = level == 15 ? 15 : level + 1;
4464 } else if (strlencmp(cmd, "down") == 0) {
4465 if (mute)
4466 new_mute = 0;
4467 else
4468 new_level = level == 0 ? 0 : level - 1;
4469 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4470 new_level >= 0 && new_level <= 15) {
4471 /* new_level set */
4472 } else if (strlencmp(cmd, "mute") == 0) {
4473 new_mute = 0x40;
4474 } else
4475 return -EINVAL;
4477 if (new_level != level) {
4478 /* mute doesn't change */
4480 cmos_cmd = (new_level > level) ?
4481 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4482 inc = new_level > level ? 1 : -1;
4484 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4485 !acpi_ec_write(volume_offset, level)))
4486 return -EIO;
4488 for (i = level; i != new_level; i += inc)
4489 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4490 !acpi_ec_write(volume_offset, i + inc))
4491 return -EIO;
4493 if (mute &&
4494 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4495 !acpi_ec_write(volume_offset, new_level + mute))) {
4496 return -EIO;
4500 if (new_mute != mute) {
4501 /* level doesn't change */
4503 cmos_cmd = (new_mute) ?
4504 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4506 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4507 !acpi_ec_write(volume_offset, level + new_mute))
4508 return -EIO;
4512 return 0;
4515 static struct ibm_struct volume_driver_data = {
4516 .name = "volume",
4517 .read = volume_read,
4518 .write = volume_write,
4521 /*************************************************************************
4522 * Fan subdriver
4526 * FAN ACCESS MODES
4528 * TPACPI_FAN_RD_ACPI_GFAN:
4529 * ACPI GFAN method: returns fan level
4531 * see TPACPI_FAN_WR_ACPI_SFAN
4532 * EC 0x2f (HFSP) not available if GFAN exists
4534 * TPACPI_FAN_WR_ACPI_SFAN:
4535 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4537 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4538 * it for writing.
4540 * TPACPI_FAN_WR_TPEC:
4541 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4542 * Supported on almost all ThinkPads
4544 * Fan speed changes of any sort (including those caused by the
4545 * disengaged mode) are usually done slowly by the firmware as the
4546 * maximum ammount of fan duty cycle change per second seems to be
4547 * limited.
4549 * Reading is not available if GFAN exists.
4550 * Writing is not available if SFAN exists.
4552 * Bits
4553 * 7 automatic mode engaged;
4554 * (default operation mode of the ThinkPad)
4555 * fan level is ignored in this mode.
4556 * 6 full speed mode (takes precedence over bit 7);
4557 * not available on all thinkpads. May disable
4558 * the tachometer while the fan controller ramps up
4559 * the speed (which can take up to a few *minutes*).
4560 * Speeds up fan to 100% duty-cycle, which is far above
4561 * the standard RPM levels. It is not impossible that
4562 * it could cause hardware damage.
4563 * 5-3 unused in some models. Extra bits for fan level
4564 * in others, but still useless as all values above
4565 * 7 map to the same speed as level 7 in these models.
4566 * 2-0 fan level (0..7 usually)
4567 * 0x00 = stop
4568 * 0x07 = max (set when temperatures critical)
4569 * Some ThinkPads may have other levels, see
4570 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4572 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4573 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4574 * does so, its initial value is meaningless (0x07).
4576 * For firmware bugs, refer to:
4577 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4579 * ----
4581 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4582 * Main fan tachometer reading (in RPM)
4584 * This register is present on all ThinkPads with a new-style EC, and
4585 * it is known not to be present on the A21m/e, and T22, as there is
4586 * something else in offset 0x84 according to the ACPI DSDT. Other
4587 * ThinkPads from this same time period (and earlier) probably lack the
4588 * tachometer as well.
4590 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4591 * was never fixed by IBM to report the EC firmware version string
4592 * probably support the tachometer (like the early X models), so
4593 * detecting it is quite hard. We need more data to know for sure.
4595 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4596 * might result.
4598 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4599 * mode.
4601 * For firmware bugs, refer to:
4602 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4604 * TPACPI_FAN_WR_ACPI_FANS:
4605 * ThinkPad X31, X40, X41. Not available in the X60.
4607 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4608 * high speed. ACPI DSDT seems to map these three speeds to levels
4609 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4610 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4612 * The speeds are stored on handles
4613 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4615 * There are three default speed sets, acessible as handles:
4616 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4618 * ACPI DSDT switches which set is in use depending on various
4619 * factors.
4621 * TPACPI_FAN_WR_TPEC is also available and should be used to
4622 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4623 * but the ACPI tables just mention level 7.
4626 enum { /* Fan control constants */
4627 fan_status_offset = 0x2f, /* EC register 0x2f */
4628 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4629 * 0x84 must be read before 0x85 */
4631 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4632 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4634 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4637 enum fan_status_access_mode {
4638 TPACPI_FAN_NONE = 0, /* No fan status or control */
4639 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4640 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4643 enum fan_control_access_mode {
4644 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4645 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4646 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4647 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4650 enum fan_control_commands {
4651 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4652 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4653 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4654 * and also watchdog cmd */
4657 static int fan_control_allowed;
4659 static enum fan_status_access_mode fan_status_access_mode;
4660 static enum fan_control_access_mode fan_control_access_mode;
4661 static enum fan_control_commands fan_control_commands;
4663 static u8 fan_control_initial_status;
4664 static u8 fan_control_desired_level;
4665 static int fan_watchdog_maxinterval;
4667 static struct mutex fan_mutex;
4669 static void fan_watchdog_fire(struct work_struct *ignored);
4670 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4672 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4673 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4674 "\\FSPD", /* 600e/x, 770e, 770x */
4675 ); /* all others */
4676 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4677 "JFNS", /* 770x-JL */
4678 ); /* all others */
4681 * Call with fan_mutex held
4683 static void fan_update_desired_level(u8 status)
4685 if ((status &
4686 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4687 if (status > 7)
4688 fan_control_desired_level = 7;
4689 else
4690 fan_control_desired_level = status;
4694 static int fan_get_status(u8 *status)
4696 u8 s;
4698 /* TODO:
4699 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4701 switch (fan_status_access_mode) {
4702 case TPACPI_FAN_RD_ACPI_GFAN:
4703 /* 570, 600e/x, 770e, 770x */
4705 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4706 return -EIO;
4708 if (likely(status))
4709 *status = s & 0x07;
4711 break;
4713 case TPACPI_FAN_RD_TPEC:
4714 /* all except 570, 600e/x, 770e, 770x */
4715 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4716 return -EIO;
4718 if (likely(status))
4719 *status = s;
4721 break;
4723 default:
4724 return -ENXIO;
4727 return 0;
4730 static int fan_get_status_safe(u8 *status)
4732 int rc;
4733 u8 s;
4735 if (mutex_lock_interruptible(&fan_mutex))
4736 return -ERESTARTSYS;
4737 rc = fan_get_status(&s);
4738 if (!rc)
4739 fan_update_desired_level(s);
4740 mutex_unlock(&fan_mutex);
4742 if (status)
4743 *status = s;
4745 return rc;
4748 static int fan_get_speed(unsigned int *speed)
4750 u8 hi, lo;
4752 switch (fan_status_access_mode) {
4753 case TPACPI_FAN_RD_TPEC:
4754 /* all except 570, 600e/x, 770e, 770x */
4755 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4756 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4757 return -EIO;
4759 if (likely(speed))
4760 *speed = (hi << 8) | lo;
4762 break;
4764 default:
4765 return -ENXIO;
4768 return 0;
4771 static int fan_set_level(int level)
4773 if (!fan_control_allowed)
4774 return -EPERM;
4776 switch (fan_control_access_mode) {
4777 case TPACPI_FAN_WR_ACPI_SFAN:
4778 if (level >= 0 && level <= 7) {
4779 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4780 return -EIO;
4781 } else
4782 return -EINVAL;
4783 break;
4785 case TPACPI_FAN_WR_ACPI_FANS:
4786 case TPACPI_FAN_WR_TPEC:
4787 if ((level != TP_EC_FAN_AUTO) &&
4788 (level != TP_EC_FAN_FULLSPEED) &&
4789 ((level < 0) || (level > 7)))
4790 return -EINVAL;
4792 /* safety net should the EC not support AUTO
4793 * or FULLSPEED mode bits and just ignore them */
4794 if (level & TP_EC_FAN_FULLSPEED)
4795 level |= 7; /* safety min speed 7 */
4796 else if (level & TP_EC_FAN_AUTO)
4797 level |= 4; /* safety min speed 4 */
4799 if (!acpi_ec_write(fan_status_offset, level))
4800 return -EIO;
4801 else
4802 tp_features.fan_ctrl_status_undef = 0;
4803 break;
4805 default:
4806 return -ENXIO;
4808 return 0;
4811 static int fan_set_level_safe(int level)
4813 int rc;
4815 if (!fan_control_allowed)
4816 return -EPERM;
4818 if (mutex_lock_interruptible(&fan_mutex))
4819 return -ERESTARTSYS;
4821 if (level == TPACPI_FAN_LAST_LEVEL)
4822 level = fan_control_desired_level;
4824 rc = fan_set_level(level);
4825 if (!rc)
4826 fan_update_desired_level(level);
4828 mutex_unlock(&fan_mutex);
4829 return rc;
4832 static int fan_set_enable(void)
4834 u8 s;
4835 int rc;
4837 if (!fan_control_allowed)
4838 return -EPERM;
4840 if (mutex_lock_interruptible(&fan_mutex))
4841 return -ERESTARTSYS;
4843 switch (fan_control_access_mode) {
4844 case TPACPI_FAN_WR_ACPI_FANS:
4845 case TPACPI_FAN_WR_TPEC:
4846 rc = fan_get_status(&s);
4847 if (rc < 0)
4848 break;
4850 /* Don't go out of emergency fan mode */
4851 if (s != 7) {
4852 s &= 0x07;
4853 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4856 if (!acpi_ec_write(fan_status_offset, s))
4857 rc = -EIO;
4858 else {
4859 tp_features.fan_ctrl_status_undef = 0;
4860 rc = 0;
4862 break;
4864 case TPACPI_FAN_WR_ACPI_SFAN:
4865 rc = fan_get_status(&s);
4866 if (rc < 0)
4867 break;
4869 s &= 0x07;
4871 /* Set fan to at least level 4 */
4872 s |= 4;
4874 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4875 rc = -EIO;
4876 else
4877 rc = 0;
4878 break;
4880 default:
4881 rc = -ENXIO;
4884 mutex_unlock(&fan_mutex);
4885 return rc;
4888 static int fan_set_disable(void)
4890 int rc;
4892 if (!fan_control_allowed)
4893 return -EPERM;
4895 if (mutex_lock_interruptible(&fan_mutex))
4896 return -ERESTARTSYS;
4898 rc = 0;
4899 switch (fan_control_access_mode) {
4900 case TPACPI_FAN_WR_ACPI_FANS:
4901 case TPACPI_FAN_WR_TPEC:
4902 if (!acpi_ec_write(fan_status_offset, 0x00))
4903 rc = -EIO;
4904 else {
4905 fan_control_desired_level = 0;
4906 tp_features.fan_ctrl_status_undef = 0;
4908 break;
4910 case TPACPI_FAN_WR_ACPI_SFAN:
4911 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4912 rc = -EIO;
4913 else
4914 fan_control_desired_level = 0;
4915 break;
4917 default:
4918 rc = -ENXIO;
4922 mutex_unlock(&fan_mutex);
4923 return rc;
4926 static int fan_set_speed(int speed)
4928 int rc;
4930 if (!fan_control_allowed)
4931 return -EPERM;
4933 if (mutex_lock_interruptible(&fan_mutex))
4934 return -ERESTARTSYS;
4936 rc = 0;
4937 switch (fan_control_access_mode) {
4938 case TPACPI_FAN_WR_ACPI_FANS:
4939 if (speed >= 0 && speed <= 65535) {
4940 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4941 speed, speed, speed))
4942 rc = -EIO;
4943 } else
4944 rc = -EINVAL;
4945 break;
4947 default:
4948 rc = -ENXIO;
4951 mutex_unlock(&fan_mutex);
4952 return rc;
4955 static void fan_watchdog_reset(void)
4957 static int fan_watchdog_active;
4959 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4960 return;
4962 if (fan_watchdog_active)
4963 cancel_delayed_work(&fan_watchdog_task);
4965 if (fan_watchdog_maxinterval > 0 &&
4966 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4967 fan_watchdog_active = 1;
4968 if (!schedule_delayed_work(&fan_watchdog_task,
4969 msecs_to_jiffies(fan_watchdog_maxinterval
4970 * 1000))) {
4971 printk(TPACPI_ERR
4972 "failed to schedule the fan watchdog, "
4973 "watchdog will not trigger\n");
4975 } else
4976 fan_watchdog_active = 0;
4979 static void fan_watchdog_fire(struct work_struct *ignored)
4981 int rc;
4983 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4984 return;
4986 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4987 rc = fan_set_enable();
4988 if (rc < 0) {
4989 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4990 "will try again later...\n", -rc);
4991 /* reschedule for later */
4992 fan_watchdog_reset();
4997 * SYSFS fan layout: hwmon compatible (device)
4999 * pwm*_enable:
5000 * 0: "disengaged" mode
5001 * 1: manual mode
5002 * 2: native EC "auto" mode (recommended, hardware default)
5004 * pwm*: set speed in manual mode, ignored otherwise.
5005 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5006 * interpolation.
5008 * fan*_input: tachometer reading, RPM
5011 * SYSFS fan layout: extensions
5013 * fan_watchdog (driver):
5014 * fan watchdog interval in seconds, 0 disables (default), max 120
5017 /* sysfs fan pwm1_enable ----------------------------------------------- */
5018 static ssize_t fan_pwm1_enable_show(struct device *dev,
5019 struct device_attribute *attr,
5020 char *buf)
5022 int res, mode;
5023 u8 status;
5025 res = fan_get_status_safe(&status);
5026 if (res)
5027 return res;
5029 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5030 if (status != fan_control_initial_status) {
5031 tp_features.fan_ctrl_status_undef = 0;
5032 } else {
5033 /* Return most likely status. In fact, it
5034 * might be the only possible status */
5035 status = TP_EC_FAN_AUTO;
5039 if (status & TP_EC_FAN_FULLSPEED) {
5040 mode = 0;
5041 } else if (status & TP_EC_FAN_AUTO) {
5042 mode = 2;
5043 } else
5044 mode = 1;
5046 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5049 static ssize_t fan_pwm1_enable_store(struct device *dev,
5050 struct device_attribute *attr,
5051 const char *buf, size_t count)
5053 unsigned long t;
5054 int res, level;
5056 if (parse_strtoul(buf, 2, &t))
5057 return -EINVAL;
5059 switch (t) {
5060 case 0:
5061 level = TP_EC_FAN_FULLSPEED;
5062 break;
5063 case 1:
5064 level = TPACPI_FAN_LAST_LEVEL;
5065 break;
5066 case 2:
5067 level = TP_EC_FAN_AUTO;
5068 break;
5069 case 3:
5070 /* reserved for software-controlled auto mode */
5071 return -ENOSYS;
5072 default:
5073 return -EINVAL;
5076 res = fan_set_level_safe(level);
5077 if (res == -ENXIO)
5078 return -EINVAL;
5079 else if (res < 0)
5080 return res;
5082 fan_watchdog_reset();
5084 return count;
5087 static struct device_attribute dev_attr_fan_pwm1_enable =
5088 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5089 fan_pwm1_enable_show, fan_pwm1_enable_store);
5091 /* sysfs fan pwm1 ------------------------------------------------------ */
5092 static ssize_t fan_pwm1_show(struct device *dev,
5093 struct device_attribute *attr,
5094 char *buf)
5096 int res;
5097 u8 status;
5099 res = fan_get_status_safe(&status);
5100 if (res)
5101 return res;
5103 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5104 if (status != fan_control_initial_status) {
5105 tp_features.fan_ctrl_status_undef = 0;
5106 } else {
5107 status = TP_EC_FAN_AUTO;
5111 if ((status &
5112 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5113 status = fan_control_desired_level;
5115 if (status > 7)
5116 status = 7;
5118 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5121 static ssize_t fan_pwm1_store(struct device *dev,
5122 struct device_attribute *attr,
5123 const char *buf, size_t count)
5125 unsigned long s;
5126 int rc;
5127 u8 status, newlevel;
5129 if (parse_strtoul(buf, 255, &s))
5130 return -EINVAL;
5132 /* scale down from 0-255 to 0-7 */
5133 newlevel = (s >> 5) & 0x07;
5135 if (mutex_lock_interruptible(&fan_mutex))
5136 return -ERESTARTSYS;
5138 rc = fan_get_status(&status);
5139 if (!rc && (status &
5140 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5141 rc = fan_set_level(newlevel);
5142 if (rc == -ENXIO)
5143 rc = -EINVAL;
5144 else if (!rc) {
5145 fan_update_desired_level(newlevel);
5146 fan_watchdog_reset();
5150 mutex_unlock(&fan_mutex);
5151 return (rc)? rc : count;
5154 static struct device_attribute dev_attr_fan_pwm1 =
5155 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5156 fan_pwm1_show, fan_pwm1_store);
5158 /* sysfs fan fan1_input ------------------------------------------------ */
5159 static ssize_t fan_fan1_input_show(struct device *dev,
5160 struct device_attribute *attr,
5161 char *buf)
5163 int res;
5164 unsigned int speed;
5166 res = fan_get_speed(&speed);
5167 if (res < 0)
5168 return res;
5170 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5173 static struct device_attribute dev_attr_fan_fan1_input =
5174 __ATTR(fan1_input, S_IRUGO,
5175 fan_fan1_input_show, NULL);
5177 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5178 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5179 char *buf)
5181 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5184 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5185 const char *buf, size_t count)
5187 unsigned long t;
5189 if (parse_strtoul(buf, 120, &t))
5190 return -EINVAL;
5192 if (!fan_control_allowed)
5193 return -EPERM;
5195 fan_watchdog_maxinterval = t;
5196 fan_watchdog_reset();
5198 return count;
5201 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5202 fan_fan_watchdog_show, fan_fan_watchdog_store);
5204 /* --------------------------------------------------------------------- */
5205 static struct attribute *fan_attributes[] = {
5206 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5207 &dev_attr_fan_fan1_input.attr,
5208 NULL
5211 static const struct attribute_group fan_attr_group = {
5212 .attrs = fan_attributes,
5215 static int __init fan_init(struct ibm_init_struct *iibm)
5217 int rc;
5219 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5221 mutex_init(&fan_mutex);
5222 fan_status_access_mode = TPACPI_FAN_NONE;
5223 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5224 fan_control_commands = 0;
5225 fan_watchdog_maxinterval = 0;
5226 tp_features.fan_ctrl_status_undef = 0;
5227 fan_control_desired_level = 7;
5229 TPACPI_ACPIHANDLE_INIT(fans);
5230 TPACPI_ACPIHANDLE_INIT(gfan);
5231 TPACPI_ACPIHANDLE_INIT(sfan);
5233 if (gfan_handle) {
5234 /* 570, 600e/x, 770e, 770x */
5235 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5236 } else {
5237 /* all other ThinkPads: note that even old-style
5238 * ThinkPad ECs supports the fan control register */
5239 if (likely(acpi_ec_read(fan_status_offset,
5240 &fan_control_initial_status))) {
5241 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5243 /* In some ThinkPads, neither the EC nor the ACPI
5244 * DSDT initialize the fan status, and it ends up
5245 * being set to 0x07 when it *could* be either
5246 * 0x07 or 0x80.
5248 * Enable for TP-1Y (T43), TP-78 (R51e),
5249 * TP-76 (R52), TP-70 (T43, R52), which are known
5250 * to be buggy. */
5251 if (fan_control_initial_status == 0x07) {
5252 switch (thinkpad_id.ec_model) {
5253 case 0x5931: /* TP-1Y */
5254 case 0x3837: /* TP-78 */
5255 case 0x3637: /* TP-76 */
5256 case 0x3037: /* TP-70 */
5257 printk(TPACPI_NOTICE
5258 "fan_init: initial fan status "
5259 "is unknown, assuming it is "
5260 "in auto mode\n");
5261 tp_features.fan_ctrl_status_undef = 1;
5265 } else {
5266 printk(TPACPI_ERR
5267 "ThinkPad ACPI EC access misbehaving, "
5268 "fan status and control unavailable\n");
5269 return 1;
5273 if (sfan_handle) {
5274 /* 570, 770x-JL */
5275 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5276 fan_control_commands |=
5277 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5278 } else {
5279 if (!gfan_handle) {
5280 /* gfan without sfan means no fan control */
5281 /* all other models implement TP EC 0x2f control */
5283 if (fans_handle) {
5284 /* X31, X40, X41 */
5285 fan_control_access_mode =
5286 TPACPI_FAN_WR_ACPI_FANS;
5287 fan_control_commands |=
5288 TPACPI_FAN_CMD_SPEED |
5289 TPACPI_FAN_CMD_LEVEL |
5290 TPACPI_FAN_CMD_ENABLE;
5291 } else {
5292 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5293 fan_control_commands |=
5294 TPACPI_FAN_CMD_LEVEL |
5295 TPACPI_FAN_CMD_ENABLE;
5300 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5301 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5302 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5303 fan_status_access_mode, fan_control_access_mode);
5305 /* fan control master switch */
5306 if (!fan_control_allowed) {
5307 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5308 fan_control_commands = 0;
5309 dbg_printk(TPACPI_DBG_INIT,
5310 "fan control features disabled by parameter\n");
5313 /* update fan_control_desired_level */
5314 if (fan_status_access_mode != TPACPI_FAN_NONE)
5315 fan_get_status_safe(NULL);
5317 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5318 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5319 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5320 &fan_attr_group);
5321 if (!(rc < 0))
5322 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5323 &driver_attr_fan_watchdog);
5324 if (rc < 0)
5325 return rc;
5326 return 0;
5327 } else
5328 return 1;
5331 static void fan_exit(void)
5333 vdbg_printk(TPACPI_DBG_EXIT,
5334 "cancelling any pending fan watchdog tasks\n");
5336 /* FIXME: can we really do this unconditionally? */
5337 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5338 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5339 &driver_attr_fan_watchdog);
5341 cancel_delayed_work(&fan_watchdog_task);
5342 flush_scheduled_work();
5345 static int fan_read(char *p)
5347 int len = 0;
5348 int rc;
5349 u8 status;
5350 unsigned int speed = 0;
5352 switch (fan_status_access_mode) {
5353 case TPACPI_FAN_RD_ACPI_GFAN:
5354 /* 570, 600e/x, 770e, 770x */
5355 rc = fan_get_status_safe(&status);
5356 if (rc < 0)
5357 return rc;
5359 len += sprintf(p + len, "status:\t\t%s\n"
5360 "level:\t\t%d\n",
5361 (status != 0) ? "enabled" : "disabled", status);
5362 break;
5364 case TPACPI_FAN_RD_TPEC:
5365 /* all except 570, 600e/x, 770e, 770x */
5366 rc = fan_get_status_safe(&status);
5367 if (rc < 0)
5368 return rc;
5370 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5371 if (status != fan_control_initial_status)
5372 tp_features.fan_ctrl_status_undef = 0;
5373 else
5374 /* Return most likely status. In fact, it
5375 * might be the only possible status */
5376 status = TP_EC_FAN_AUTO;
5379 len += sprintf(p + len, "status:\t\t%s\n",
5380 (status != 0) ? "enabled" : "disabled");
5382 rc = fan_get_speed(&speed);
5383 if (rc < 0)
5384 return rc;
5386 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5388 if (status & TP_EC_FAN_FULLSPEED)
5389 /* Disengaged mode takes precedence */
5390 len += sprintf(p + len, "level:\t\tdisengaged\n");
5391 else if (status & TP_EC_FAN_AUTO)
5392 len += sprintf(p + len, "level:\t\tauto\n");
5393 else
5394 len += sprintf(p + len, "level:\t\t%d\n", status);
5395 break;
5397 case TPACPI_FAN_NONE:
5398 default:
5399 len += sprintf(p + len, "status:\t\tnot supported\n");
5402 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5403 len += sprintf(p + len, "commands:\tlevel <level>");
5405 switch (fan_control_access_mode) {
5406 case TPACPI_FAN_WR_ACPI_SFAN:
5407 len += sprintf(p + len, " (<level> is 0-7)\n");
5408 break;
5410 default:
5411 len += sprintf(p + len, " (<level> is 0-7, "
5412 "auto, disengaged, full-speed)\n");
5413 break;
5417 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5418 len += sprintf(p + len, "commands:\tenable, disable\n"
5419 "commands:\twatchdog <timeout> (<timeout> "
5420 "is 0 (off), 1-120 (seconds))\n");
5422 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5423 len += sprintf(p + len, "commands:\tspeed <speed>"
5424 " (<speed> is 0-65535)\n");
5426 return len;
5429 static int fan_write_cmd_level(const char *cmd, int *rc)
5431 int level;
5433 if (strlencmp(cmd, "level auto") == 0)
5434 level = TP_EC_FAN_AUTO;
5435 else if ((strlencmp(cmd, "level disengaged") == 0) |
5436 (strlencmp(cmd, "level full-speed") == 0))
5437 level = TP_EC_FAN_FULLSPEED;
5438 else if (sscanf(cmd, "level %d", &level) != 1)
5439 return 0;
5441 *rc = fan_set_level_safe(level);
5442 if (*rc == -ENXIO)
5443 printk(TPACPI_ERR "level command accepted for unsupported "
5444 "access mode %d", fan_control_access_mode);
5446 return 1;
5449 static int fan_write_cmd_enable(const char *cmd, int *rc)
5451 if (strlencmp(cmd, "enable") != 0)
5452 return 0;
5454 *rc = fan_set_enable();
5455 if (*rc == -ENXIO)
5456 printk(TPACPI_ERR "enable command accepted for unsupported "
5457 "access mode %d", fan_control_access_mode);
5459 return 1;
5462 static int fan_write_cmd_disable(const char *cmd, int *rc)
5464 if (strlencmp(cmd, "disable") != 0)
5465 return 0;
5467 *rc = fan_set_disable();
5468 if (*rc == -ENXIO)
5469 printk(TPACPI_ERR "disable command accepted for unsupported "
5470 "access mode %d", fan_control_access_mode);
5472 return 1;
5475 static int fan_write_cmd_speed(const char *cmd, int *rc)
5477 int speed;
5479 /* TODO:
5480 * Support speed <low> <medium> <high> ? */
5482 if (sscanf(cmd, "speed %d", &speed) != 1)
5483 return 0;
5485 *rc = fan_set_speed(speed);
5486 if (*rc == -ENXIO)
5487 printk(TPACPI_ERR "speed command accepted for unsupported "
5488 "access mode %d", fan_control_access_mode);
5490 return 1;
5493 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5495 int interval;
5497 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5498 return 0;
5500 if (interval < 0 || interval > 120)
5501 *rc = -EINVAL;
5502 else
5503 fan_watchdog_maxinterval = interval;
5505 return 1;
5508 static int fan_write(char *buf)
5510 char *cmd;
5511 int rc = 0;
5513 while (!rc && (cmd = next_cmd(&buf))) {
5514 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5515 fan_write_cmd_level(cmd, &rc)) &&
5516 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5517 (fan_write_cmd_enable(cmd, &rc) ||
5518 fan_write_cmd_disable(cmd, &rc) ||
5519 fan_write_cmd_watchdog(cmd, &rc))) &&
5520 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5521 fan_write_cmd_speed(cmd, &rc))
5523 rc = -EINVAL;
5524 else if (!rc)
5525 fan_watchdog_reset();
5528 return rc;
5531 static struct ibm_struct fan_driver_data = {
5532 .name = "fan",
5533 .read = fan_read,
5534 .write = fan_write,
5535 .exit = fan_exit,
5538 /****************************************************************************
5539 ****************************************************************************
5541 * Infrastructure
5543 ****************************************************************************
5544 ****************************************************************************/
5546 /* sysfs name ---------------------------------------------------------- */
5547 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5548 struct device_attribute *attr,
5549 char *buf)
5551 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5554 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5555 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5557 /* --------------------------------------------------------------------- */
5559 /* /proc support */
5560 static struct proc_dir_entry *proc_dir;
5563 * Module and infrastructure proble, init and exit handling
5566 static int force_load;
5568 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5569 static const char * __init str_supported(int is_supported)
5571 static char text_unsupported[] __initdata = "not supported";
5573 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5575 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5577 static void ibm_exit(struct ibm_struct *ibm)
5579 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5581 list_del_init(&ibm->all_drivers);
5583 if (ibm->flags.acpi_notify_installed) {
5584 dbg_printk(TPACPI_DBG_EXIT,
5585 "%s: acpi_remove_notify_handler\n", ibm->name);
5586 BUG_ON(!ibm->acpi);
5587 acpi_remove_notify_handler(*ibm->acpi->handle,
5588 ibm->acpi->type,
5589 dispatch_acpi_notify);
5590 ibm->flags.acpi_notify_installed = 0;
5591 ibm->flags.acpi_notify_installed = 0;
5594 if (ibm->flags.proc_created) {
5595 dbg_printk(TPACPI_DBG_EXIT,
5596 "%s: remove_proc_entry\n", ibm->name);
5597 remove_proc_entry(ibm->name, proc_dir);
5598 ibm->flags.proc_created = 0;
5601 if (ibm->flags.acpi_driver_registered) {
5602 dbg_printk(TPACPI_DBG_EXIT,
5603 "%s: acpi_bus_unregister_driver\n", ibm->name);
5604 BUG_ON(!ibm->acpi);
5605 acpi_bus_unregister_driver(ibm->acpi->driver);
5606 kfree(ibm->acpi->driver);
5607 ibm->acpi->driver = NULL;
5608 ibm->flags.acpi_driver_registered = 0;
5611 if (ibm->flags.init_called && ibm->exit) {
5612 ibm->exit();
5613 ibm->flags.init_called = 0;
5616 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5619 static int __init ibm_init(struct ibm_init_struct *iibm)
5621 int ret;
5622 struct ibm_struct *ibm = iibm->data;
5623 struct proc_dir_entry *entry;
5625 BUG_ON(ibm == NULL);
5627 INIT_LIST_HEAD(&ibm->all_drivers);
5629 if (ibm->flags.experimental && !experimental)
5630 return 0;
5632 dbg_printk(TPACPI_DBG_INIT,
5633 "probing for %s\n", ibm->name);
5635 if (iibm->init) {
5636 ret = iibm->init(iibm);
5637 if (ret > 0)
5638 return 0; /* probe failed */
5639 if (ret)
5640 return ret;
5642 ibm->flags.init_called = 1;
5645 if (ibm->acpi) {
5646 if (ibm->acpi->hid) {
5647 ret = register_tpacpi_subdriver(ibm);
5648 if (ret)
5649 goto err_out;
5652 if (ibm->acpi->notify) {
5653 ret = setup_acpi_notify(ibm);
5654 if (ret == -ENODEV) {
5655 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5656 ibm->name);
5657 ret = 0;
5658 goto err_out;
5660 if (ret < 0)
5661 goto err_out;
5665 dbg_printk(TPACPI_DBG_INIT,
5666 "%s installed\n", ibm->name);
5668 if (ibm->read) {
5669 entry = create_proc_entry(ibm->name,
5670 S_IFREG | S_IRUGO | S_IWUSR,
5671 proc_dir);
5672 if (!entry) {
5673 printk(TPACPI_ERR "unable to create proc entry %s\n",
5674 ibm->name);
5675 ret = -ENODEV;
5676 goto err_out;
5678 entry->owner = THIS_MODULE;
5679 entry->data = ibm;
5680 entry->read_proc = &dispatch_procfs_read;
5681 if (ibm->write)
5682 entry->write_proc = &dispatch_procfs_write;
5683 ibm->flags.proc_created = 1;
5686 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5688 return 0;
5690 err_out:
5691 dbg_printk(TPACPI_DBG_INIT,
5692 "%s: at error exit path with result %d\n",
5693 ibm->name, ret);
5695 ibm_exit(ibm);
5696 return (ret < 0)? ret : 0;
5699 /* Probing */
5701 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5703 struct dmi_device *dev = NULL;
5704 char ec_fw_string[18];
5706 if (!tp)
5707 return;
5709 memset(tp, 0, sizeof(*tp));
5711 if (dmi_name_in_vendors("IBM"))
5712 tp->vendor = PCI_VENDOR_ID_IBM;
5713 else if (dmi_name_in_vendors("LENOVO"))
5714 tp->vendor = PCI_VENDOR_ID_LENOVO;
5715 else
5716 return;
5718 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5719 GFP_KERNEL);
5720 if (!tp->bios_version_str)
5721 return;
5722 tp->bios_model = tp->bios_version_str[0]
5723 | (tp->bios_version_str[1] << 8);
5726 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5727 * X32 or newer, all Z series; Some models must have an
5728 * up-to-date BIOS or they will not be detected.
5730 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5732 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5733 if (sscanf(dev->name,
5734 "IBM ThinkPad Embedded Controller -[%17c",
5735 ec_fw_string) == 1) {
5736 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5737 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5739 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5740 tp->ec_model = ec_fw_string[0]
5741 | (ec_fw_string[1] << 8);
5742 break;
5746 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5747 GFP_KERNEL);
5748 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5749 kfree(tp->model_str);
5750 tp->model_str = NULL;
5754 static int __init probe_for_thinkpad(void)
5756 int is_thinkpad;
5758 if (acpi_disabled)
5759 return -ENODEV;
5762 * Non-ancient models have better DMI tagging, but very old models
5763 * don't.
5765 is_thinkpad = (thinkpad_id.model_str != NULL);
5767 /* ec is required because many other handles are relative to it */
5768 TPACPI_ACPIHANDLE_INIT(ec);
5769 if (!ec_handle) {
5770 if (is_thinkpad)
5771 printk(TPACPI_ERR
5772 "Not yet supported ThinkPad detected!\n");
5773 return -ENODEV;
5777 * Risks a regression on very old machines, but reduces potential
5778 * false positives a damn great deal
5780 if (!is_thinkpad)
5781 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5783 if (!is_thinkpad && !force_load)
5784 return -ENODEV;
5786 return 0;
5790 /* Module init, exit, parameters */
5792 static struct ibm_init_struct ibms_init[] __initdata = {
5794 .init = thinkpad_acpi_driver_init,
5795 .data = &thinkpad_acpi_driver_data,
5798 .init = hotkey_init,
5799 .data = &hotkey_driver_data,
5802 .init = bluetooth_init,
5803 .data = &bluetooth_driver_data,
5806 .init = wan_init,
5807 .data = &wan_driver_data,
5810 .init = video_init,
5811 .data = &video_driver_data,
5814 .init = light_init,
5815 .data = &light_driver_data,
5817 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5819 .init = dock_init,
5820 .data = &dock_driver_data[0],
5823 .init = dock_init2,
5824 .data = &dock_driver_data[1],
5826 #endif
5827 #ifdef CONFIG_THINKPAD_ACPI_BAY
5829 .init = bay_init,
5830 .data = &bay_driver_data,
5832 #endif
5834 .init = cmos_init,
5835 .data = &cmos_driver_data,
5838 .init = led_init,
5839 .data = &led_driver_data,
5842 .init = beep_init,
5843 .data = &beep_driver_data,
5846 .init = thermal_init,
5847 .data = &thermal_driver_data,
5850 .data = &ecdump_driver_data,
5853 .init = brightness_init,
5854 .data = &brightness_driver_data,
5857 .data = &volume_driver_data,
5860 .init = fan_init,
5861 .data = &fan_driver_data,
5865 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5867 unsigned int i;
5868 struct ibm_struct *ibm;
5870 if (!kp || !kp->name || !val)
5871 return -EINVAL;
5873 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5874 ibm = ibms_init[i].data;
5875 WARN_ON(ibm == NULL);
5877 if (!ibm || !ibm->name)
5878 continue;
5880 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5881 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5882 return -ENOSPC;
5883 strcpy(ibms_init[i].param, val);
5884 strcat(ibms_init[i].param, ",");
5885 return 0;
5889 return -EINVAL;
5892 module_param(experimental, int, 0);
5893 MODULE_PARM_DESC(experimental,
5894 "Enables experimental features when non-zero");
5896 module_param_named(debug, dbg_level, uint, 0);
5897 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5899 module_param(force_load, bool, 0);
5900 MODULE_PARM_DESC(force_load,
5901 "Attempts to load the driver even on a "
5902 "mis-identified ThinkPad when true");
5904 module_param_named(fan_control, fan_control_allowed, bool, 0);
5905 MODULE_PARM_DESC(fan_control,
5906 "Enables setting fan parameters features when true");
5908 module_param_named(brightness_mode, brightness_mode, int, 0);
5909 MODULE_PARM_DESC(brightness_mode,
5910 "Selects brightness control strategy: "
5911 "0=auto, 1=EC, 2=CMOS, 3=both");
5913 module_param(brightness_enable, uint, 0);
5914 MODULE_PARM_DESC(brightness_enable,
5915 "Enables backlight control when 1, disables when 0");
5917 module_param(hotkey_report_mode, uint, 0);
5918 MODULE_PARM_DESC(hotkey_report_mode,
5919 "used for backwards compatibility with userspace, "
5920 "see documentation");
5922 #define TPACPI_PARAM(feature) \
5923 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5924 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
5925 "at module load, see documentation")
5927 TPACPI_PARAM(hotkey);
5928 TPACPI_PARAM(bluetooth);
5929 TPACPI_PARAM(video);
5930 TPACPI_PARAM(light);
5931 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5932 TPACPI_PARAM(dock);
5933 #endif
5934 #ifdef CONFIG_THINKPAD_ACPI_BAY
5935 TPACPI_PARAM(bay);
5936 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5937 TPACPI_PARAM(cmos);
5938 TPACPI_PARAM(led);
5939 TPACPI_PARAM(beep);
5940 TPACPI_PARAM(ecdump);
5941 TPACPI_PARAM(brightness);
5942 TPACPI_PARAM(volume);
5943 TPACPI_PARAM(fan);
5945 static void thinkpad_acpi_module_exit(void)
5947 struct ibm_struct *ibm, *itmp;
5949 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5951 list_for_each_entry_safe_reverse(ibm, itmp,
5952 &tpacpi_all_drivers,
5953 all_drivers) {
5954 ibm_exit(ibm);
5957 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5959 if (tpacpi_inputdev) {
5960 if (tp_features.input_device_registered)
5961 input_unregister_device(tpacpi_inputdev);
5962 else
5963 input_free_device(tpacpi_inputdev);
5966 if (tpacpi_hwmon)
5967 hwmon_device_unregister(tpacpi_hwmon);
5969 if (tp_features.sensors_pdev_attrs_registered)
5970 device_remove_file(&tpacpi_sensors_pdev->dev,
5971 &dev_attr_thinkpad_acpi_pdev_name);
5972 if (tpacpi_sensors_pdev)
5973 platform_device_unregister(tpacpi_sensors_pdev);
5974 if (tpacpi_pdev)
5975 platform_device_unregister(tpacpi_pdev);
5977 if (tp_features.sensors_pdrv_attrs_registered)
5978 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5979 if (tp_features.platform_drv_attrs_registered)
5980 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5982 if (tp_features.sensors_pdrv_registered)
5983 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5985 if (tp_features.platform_drv_registered)
5986 platform_driver_unregister(&tpacpi_pdriver);
5988 if (proc_dir)
5989 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5991 kfree(thinkpad_id.bios_version_str);
5992 kfree(thinkpad_id.ec_version_str);
5993 kfree(thinkpad_id.model_str);
5997 static int __init thinkpad_acpi_module_init(void)
5999 int ret, i;
6001 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6003 /* Parameter checking */
6004 if (hotkey_report_mode > 2)
6005 return -EINVAL;
6007 /* Driver-level probe */
6009 get_thinkpad_model_data(&thinkpad_id);
6010 ret = probe_for_thinkpad();
6011 if (ret) {
6012 thinkpad_acpi_module_exit();
6013 return ret;
6016 /* Driver initialization */
6018 TPACPI_ACPIHANDLE_INIT(ecrd);
6019 TPACPI_ACPIHANDLE_INIT(ecwr);
6021 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6022 if (!proc_dir) {
6023 printk(TPACPI_ERR
6024 "unable to create proc dir " TPACPI_PROC_DIR);
6025 thinkpad_acpi_module_exit();
6026 return -ENODEV;
6028 proc_dir->owner = THIS_MODULE;
6030 ret = platform_driver_register(&tpacpi_pdriver);
6031 if (ret) {
6032 printk(TPACPI_ERR
6033 "unable to register main platform driver\n");
6034 thinkpad_acpi_module_exit();
6035 return ret;
6037 tp_features.platform_drv_registered = 1;
6039 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6040 if (ret) {
6041 printk(TPACPI_ERR
6042 "unable to register hwmon platform driver\n");
6043 thinkpad_acpi_module_exit();
6044 return ret;
6046 tp_features.sensors_pdrv_registered = 1;
6048 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6049 if (!ret) {
6050 tp_features.platform_drv_attrs_registered = 1;
6051 ret = tpacpi_create_driver_attributes(
6052 &tpacpi_hwmon_pdriver.driver);
6054 if (ret) {
6055 printk(TPACPI_ERR
6056 "unable to create sysfs driver attributes\n");
6057 thinkpad_acpi_module_exit();
6058 return ret;
6060 tp_features.sensors_pdrv_attrs_registered = 1;
6063 /* Device initialization */
6064 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6065 NULL, 0);
6066 if (IS_ERR(tpacpi_pdev)) {
6067 ret = PTR_ERR(tpacpi_pdev);
6068 tpacpi_pdev = NULL;
6069 printk(TPACPI_ERR "unable to register platform device\n");
6070 thinkpad_acpi_module_exit();
6071 return ret;
6073 tpacpi_sensors_pdev = platform_device_register_simple(
6074 TPACPI_HWMON_DRVR_NAME,
6075 -1, NULL, 0);
6076 if (IS_ERR(tpacpi_sensors_pdev)) {
6077 ret = PTR_ERR(tpacpi_sensors_pdev);
6078 tpacpi_sensors_pdev = NULL;
6079 printk(TPACPI_ERR
6080 "unable to register hwmon platform device\n");
6081 thinkpad_acpi_module_exit();
6082 return ret;
6084 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6085 &dev_attr_thinkpad_acpi_pdev_name);
6086 if (ret) {
6087 printk(TPACPI_ERR
6088 "unable to create sysfs hwmon device attributes\n");
6089 thinkpad_acpi_module_exit();
6090 return ret;
6092 tp_features.sensors_pdev_attrs_registered = 1;
6093 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6094 if (IS_ERR(tpacpi_hwmon)) {
6095 ret = PTR_ERR(tpacpi_hwmon);
6096 tpacpi_hwmon = NULL;
6097 printk(TPACPI_ERR "unable to register hwmon device\n");
6098 thinkpad_acpi_module_exit();
6099 return ret;
6101 mutex_init(&tpacpi_inputdev_send_mutex);
6102 tpacpi_inputdev = input_allocate_device();
6103 if (!tpacpi_inputdev) {
6104 printk(TPACPI_ERR "unable to allocate input device\n");
6105 thinkpad_acpi_module_exit();
6106 return -ENOMEM;
6107 } else {
6108 /* Prepare input device, but don't register */
6109 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6110 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6111 tpacpi_inputdev->id.bustype = BUS_HOST;
6112 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6113 thinkpad_id.vendor :
6114 PCI_VENDOR_ID_IBM;
6115 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6116 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6118 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6119 ret = ibm_init(&ibms_init[i]);
6120 if (ret >= 0 && *ibms_init[i].param)
6121 ret = ibms_init[i].data->write(ibms_init[i].param);
6122 if (ret < 0) {
6123 thinkpad_acpi_module_exit();
6124 return ret;
6127 ret = input_register_device(tpacpi_inputdev);
6128 if (ret < 0) {
6129 printk(TPACPI_ERR "unable to register input device\n");
6130 thinkpad_acpi_module_exit();
6131 return ret;
6132 } else {
6133 tp_features.input_device_registered = 1;
6136 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6137 return 0;
6140 /* Please remove this in year 2009 */
6141 MODULE_ALIAS("ibm_acpi");
6144 * DMI matching for module autoloading
6146 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6147 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6149 * Only models listed in thinkwiki will be supported, so add yours
6150 * if it is not there yet.
6152 #define IBM_BIOS_MODULE_ALIAS(__type) \
6153 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6155 /* Non-ancient thinkpads */
6156 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6157 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6159 /* Ancient thinkpad BIOSes have to be identified by
6160 * BIOS type or model number, and there are far less
6161 * BIOS types than model numbers... */
6162 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6163 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6164 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6166 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6167 MODULE_DESCRIPTION(TPACPI_DESC);
6168 MODULE_VERSION(TPACPI_VERSION);
6169 MODULE_LICENSE("GPL");
6171 module_init(thinkpad_acpi_module_init);
6172 module_exit(thinkpad_acpi_module_exit);