ACPI: thinkpad-acpi: wakeup on hotunplug reporting
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobe06c9df1034b87e8e4078626761e63d16e7f546e
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.18"
25 #define TPACPI_SYSFS_VERSION 0x020100
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 static int _sta(acpi_handle handle)
419 int status;
421 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
422 status = 0;
424 return status;
427 static int issue_thinkpad_cmos_command(int cmos_cmd)
429 if (!cmos_handle)
430 return -ENXIO;
432 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
433 return -EIO;
435 return 0;
438 /*************************************************************************
439 * ACPI device model
442 #define TPACPI_ACPIHANDLE_INIT(object) \
443 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
444 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
446 static void drv_acpi_handle_init(char *name,
447 acpi_handle *handle, acpi_handle parent,
448 char **paths, int num_paths, char **path)
450 int i;
451 acpi_status status;
453 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
454 name);
456 for (i = 0; i < num_paths; i++) {
457 status = acpi_get_handle(parent, paths[i], handle);
458 if (ACPI_SUCCESS(status)) {
459 *path = paths[i];
460 dbg_printk(TPACPI_DBG_INIT,
461 "Found ACPI handle %s for %s\n",
462 *path, name);
463 return;
467 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
468 name);
469 *handle = NULL;
472 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
474 struct ibm_struct *ibm = data;
476 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
477 return;
479 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
480 return;
482 ibm->acpi->notify(ibm, event);
485 static int __init setup_acpi_notify(struct ibm_struct *ibm)
487 acpi_status status;
488 int rc;
490 BUG_ON(!ibm->acpi);
492 if (!*ibm->acpi->handle)
493 return 0;
495 vdbg_printk(TPACPI_DBG_INIT,
496 "setting up ACPI notify for %s\n", ibm->name);
498 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
499 if (rc < 0) {
500 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
501 ibm->name, rc);
502 return -ENODEV;
505 acpi_driver_data(ibm->acpi->device) = ibm;
506 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
507 TPACPI_ACPI_EVENT_PREFIX,
508 ibm->name);
510 status = acpi_install_notify_handler(*ibm->acpi->handle,
511 ibm->acpi->type, dispatch_acpi_notify, ibm);
512 if (ACPI_FAILURE(status)) {
513 if (status == AE_ALREADY_EXISTS) {
514 printk(TPACPI_NOTICE
515 "another device driver is already "
516 "handling %s events\n", ibm->name);
517 } else {
518 printk(TPACPI_ERR
519 "acpi_install_notify_handler(%s) failed: %d\n",
520 ibm->name, status);
522 return -ENODEV;
524 ibm->flags.acpi_notify_installed = 1;
525 return 0;
528 static int __init tpacpi_device_add(struct acpi_device *device)
530 return 0;
533 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
535 int rc;
537 dbg_printk(TPACPI_DBG_INIT,
538 "registering %s as an ACPI driver\n", ibm->name);
540 BUG_ON(!ibm->acpi);
542 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
543 if (!ibm->acpi->driver) {
544 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
545 return -ENOMEM;
548 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
549 ibm->acpi->driver->ids = ibm->acpi->hid;
551 ibm->acpi->driver->ops.add = &tpacpi_device_add;
553 rc = acpi_bus_register_driver(ibm->acpi->driver);
554 if (rc < 0) {
555 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
556 ibm->name, rc);
557 kfree(ibm->acpi->driver);
558 ibm->acpi->driver = NULL;
559 } else if (!rc)
560 ibm->flags.acpi_driver_registered = 1;
562 return rc;
566 /****************************************************************************
567 ****************************************************************************
569 * Procfs Helpers
571 ****************************************************************************
572 ****************************************************************************/
574 static int dispatch_procfs_read(char *page, char **start, off_t off,
575 int count, int *eof, void *data)
577 struct ibm_struct *ibm = data;
578 int len;
580 if (!ibm || !ibm->read)
581 return -EINVAL;
583 len = ibm->read(page);
584 if (len < 0)
585 return len;
587 if (len <= off + count)
588 *eof = 1;
589 *start = page + off;
590 len -= off;
591 if (len > count)
592 len = count;
593 if (len < 0)
594 len = 0;
596 return len;
599 static int dispatch_procfs_write(struct file *file,
600 const char __user *userbuf,
601 unsigned long count, void *data)
603 struct ibm_struct *ibm = data;
604 char *kernbuf;
605 int ret;
607 if (!ibm || !ibm->write)
608 return -EINVAL;
610 kernbuf = kmalloc(count + 2, GFP_KERNEL);
611 if (!kernbuf)
612 return -ENOMEM;
614 if (copy_from_user(kernbuf, userbuf, count)) {
615 kfree(kernbuf);
616 return -EFAULT;
619 kernbuf[count] = 0;
620 strcat(kernbuf, ",");
621 ret = ibm->write(kernbuf);
622 if (ret == 0)
623 ret = count;
625 kfree(kernbuf);
627 return ret;
630 static char *next_cmd(char **cmds)
632 char *start = *cmds;
633 char *end;
635 while ((end = strchr(start, ',')) && end == start)
636 start = end + 1;
638 if (!end)
639 return NULL;
641 *end = 0;
642 *cmds = end + 1;
643 return start;
647 /****************************************************************************
648 ****************************************************************************
650 * Device model: input, hwmon and platform
652 ****************************************************************************
653 ****************************************************************************/
655 static struct platform_device *tpacpi_pdev;
656 static struct platform_device *tpacpi_sensors_pdev;
657 static struct device *tpacpi_hwmon;
658 static struct input_dev *tpacpi_inputdev;
659 static struct mutex tpacpi_inputdev_send_mutex;
660 static LIST_HEAD(tpacpi_all_drivers);
662 static int tpacpi_suspend_handler(struct platform_device *pdev,
663 pm_message_t state)
665 struct ibm_struct *ibm, *itmp;
667 list_for_each_entry_safe(ibm, itmp,
668 &tpacpi_all_drivers,
669 all_drivers) {
670 if (ibm->suspend)
671 (ibm->suspend)(state);
674 return 0;
677 static int tpacpi_resume_handler(struct platform_device *pdev)
679 struct ibm_struct *ibm, *itmp;
681 list_for_each_entry_safe(ibm, itmp,
682 &tpacpi_all_drivers,
683 all_drivers) {
684 if (ibm->resume)
685 (ibm->resume)();
688 return 0;
691 static struct platform_driver tpacpi_pdriver = {
692 .driver = {
693 .name = TPACPI_DRVR_NAME,
694 .owner = THIS_MODULE,
696 .suspend = tpacpi_suspend_handler,
697 .resume = tpacpi_resume_handler,
700 static struct platform_driver tpacpi_hwmon_pdriver = {
701 .driver = {
702 .name = TPACPI_HWMON_DRVR_NAME,
703 .owner = THIS_MODULE,
707 /*************************************************************************
708 * sysfs support helpers
711 struct attribute_set {
712 unsigned int members, max_members;
713 struct attribute_group group;
716 struct attribute_set_obj {
717 struct attribute_set s;
718 struct attribute *a;
719 } __attribute__((packed));
721 static struct attribute_set *create_attr_set(unsigned int max_members,
722 const char *name)
724 struct attribute_set_obj *sobj;
726 if (max_members == 0)
727 return NULL;
729 /* Allocates space for implicit NULL at the end too */
730 sobj = kzalloc(sizeof(struct attribute_set_obj) +
731 max_members * sizeof(struct attribute *),
732 GFP_KERNEL);
733 if (!sobj)
734 return NULL;
735 sobj->s.max_members = max_members;
736 sobj->s.group.attrs = &sobj->a;
737 sobj->s.group.name = name;
739 return &sobj->s;
742 #define destroy_attr_set(_set) \
743 kfree(_set);
745 /* not multi-threaded safe, use it in a single thread per set */
746 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
748 if (!s || !attr)
749 return -EINVAL;
751 if (s->members >= s->max_members)
752 return -ENOMEM;
754 s->group.attrs[s->members] = attr;
755 s->members++;
757 return 0;
760 static int add_many_to_attr_set(struct attribute_set *s,
761 struct attribute **attr,
762 unsigned int count)
764 int i, res;
766 for (i = 0; i < count; i++) {
767 res = add_to_attr_set(s, attr[i]);
768 if (res)
769 return res;
772 return 0;
775 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
777 sysfs_remove_group(kobj, &s->group);
778 destroy_attr_set(s);
781 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
782 sysfs_create_group(_kobj, &_attr_set->group)
784 static int parse_strtoul(const char *buf,
785 unsigned long max, unsigned long *value)
787 char *endp;
789 while (*buf && isspace(*buf))
790 buf++;
791 *value = simple_strtoul(buf, &endp, 0);
792 while (*endp && isspace(*endp))
793 endp++;
794 if (*endp || *value > max)
795 return -EINVAL;
797 return 0;
800 /*************************************************************************
801 * thinkpad-acpi driver attributes
804 /* interface_version --------------------------------------------------- */
805 static ssize_t tpacpi_driver_interface_version_show(
806 struct device_driver *drv,
807 char *buf)
809 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
812 static DRIVER_ATTR(interface_version, S_IRUGO,
813 tpacpi_driver_interface_version_show, NULL);
815 /* debug_level --------------------------------------------------------- */
816 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
817 char *buf)
819 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
822 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
823 const char *buf, size_t count)
825 unsigned long t;
827 if (parse_strtoul(buf, 0xffff, &t))
828 return -EINVAL;
830 dbg_level = t;
832 return count;
835 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
836 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
838 /* version ------------------------------------------------------------- */
839 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
840 char *buf)
842 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
843 TPACPI_DESC, TPACPI_VERSION);
846 static DRIVER_ATTR(version, S_IRUGO,
847 tpacpi_driver_version_show, NULL);
849 /* --------------------------------------------------------------------- */
851 static struct driver_attribute *tpacpi_driver_attributes[] = {
852 &driver_attr_debug_level, &driver_attr_version,
853 &driver_attr_interface_version,
856 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
858 int i, res;
860 i = 0;
861 res = 0;
862 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
863 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
864 i++;
867 return res;
870 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
872 int i;
874 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
875 driver_remove_file(drv, tpacpi_driver_attributes[i]);
878 /****************************************************************************
879 ****************************************************************************
881 * Subdrivers
883 ****************************************************************************
884 ****************************************************************************/
886 /*************************************************************************
887 * thinkpad-acpi init subdriver
890 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
892 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
893 printk(TPACPI_INFO "%s\n", TPACPI_URL);
895 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
896 (thinkpad_id.bios_version_str) ?
897 thinkpad_id.bios_version_str : "unknown",
898 (thinkpad_id.ec_version_str) ?
899 thinkpad_id.ec_version_str : "unknown");
901 if (thinkpad_id.vendor && thinkpad_id.model_str)
902 printk(TPACPI_INFO "%s %s\n",
903 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
904 "IBM" : ((thinkpad_id.vendor ==
905 PCI_VENDOR_ID_LENOVO) ?
906 "Lenovo" : "Unknown vendor"),
907 thinkpad_id.model_str);
909 return 0;
912 static int thinkpad_acpi_driver_read(char *p)
914 int len = 0;
916 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
917 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
919 return len;
922 static struct ibm_struct thinkpad_acpi_driver_data = {
923 .name = "driver",
924 .read = thinkpad_acpi_driver_read,
927 /*************************************************************************
928 * Hotkey subdriver
931 enum { /* hot key scan codes (derived from ACPI DSDT) */
932 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
933 TP_ACPI_HOTKEYSCAN_FNF2,
934 TP_ACPI_HOTKEYSCAN_FNF3,
935 TP_ACPI_HOTKEYSCAN_FNF4,
936 TP_ACPI_HOTKEYSCAN_FNF5,
937 TP_ACPI_HOTKEYSCAN_FNF6,
938 TP_ACPI_HOTKEYSCAN_FNF7,
939 TP_ACPI_HOTKEYSCAN_FNF8,
940 TP_ACPI_HOTKEYSCAN_FNF9,
941 TP_ACPI_HOTKEYSCAN_FNF10,
942 TP_ACPI_HOTKEYSCAN_FNF11,
943 TP_ACPI_HOTKEYSCAN_FNF12,
944 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
945 TP_ACPI_HOTKEYSCAN_FNINSERT,
946 TP_ACPI_HOTKEYSCAN_FNDELETE,
947 TP_ACPI_HOTKEYSCAN_FNHOME,
948 TP_ACPI_HOTKEYSCAN_FNEND,
949 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
950 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
951 TP_ACPI_HOTKEYSCAN_FNSPACE,
952 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
953 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
954 TP_ACPI_HOTKEYSCAN_MUTE,
955 TP_ACPI_HOTKEYSCAN_THINKPAD,
958 enum { /* Keys available through NVRAM polling */
959 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
960 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
963 enum { /* Positions of some of the keys in hotkey masks */
964 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
965 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
966 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
967 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
968 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
969 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
970 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
971 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
972 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
973 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
974 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
977 enum { /* NVRAM to ACPI HKEY group map */
978 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
979 TP_ACPI_HKEY_ZOOM_MASK |
980 TP_ACPI_HKEY_DISPSWTCH_MASK |
981 TP_ACPI_HKEY_HIBERNATE_MASK,
982 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
983 TP_ACPI_HKEY_BRGHTDWN_MASK,
984 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
985 TP_ACPI_HKEY_VOLDWN_MASK |
986 TP_ACPI_HKEY_MUTE_MASK,
989 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
990 struct tp_nvram_state {
991 u16 thinkpad_toggle:1;
992 u16 zoom_toggle:1;
993 u16 display_toggle:1;
994 u16 thinklight_toggle:1;
995 u16 hibernate_toggle:1;
996 u16 displayexp_toggle:1;
997 u16 display_state:1;
998 u16 brightness_toggle:1;
999 u16 volume_toggle:1;
1000 u16 mute:1;
1002 u8 brightness_level;
1003 u8 volume_level;
1006 static struct task_struct *tpacpi_hotkey_task;
1007 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1008 static int hotkey_poll_freq = 10; /* Hz */
1009 static struct mutex hotkey_thread_mutex;
1010 static struct mutex hotkey_thread_data_mutex;
1011 static unsigned int hotkey_config_change;
1013 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1015 #define hotkey_source_mask 0U
1017 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1019 static struct mutex hotkey_mutex;
1021 static enum { /* Reasons for waking up */
1022 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1023 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1024 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1025 } hotkey_wakeup_reason;
1027 static int hotkey_autosleep_ack;
1029 static int hotkey_orig_status;
1030 static u32 hotkey_orig_mask;
1031 static u32 hotkey_all_mask;
1032 static u32 hotkey_reserved_mask;
1033 static u32 hotkey_mask;
1035 static unsigned int hotkey_report_mode;
1037 static u16 *hotkey_keycode_map;
1039 static struct attribute_set *hotkey_dev_attributes;
1041 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1042 #define HOTKEY_CONFIG_CRITICAL_START \
1043 do { \
1044 mutex_lock(&hotkey_thread_data_mutex); \
1045 hotkey_config_change++; \
1046 } while (0);
1047 #define HOTKEY_CONFIG_CRITICAL_END \
1048 mutex_unlock(&hotkey_thread_data_mutex);
1049 #else
1050 #define HOTKEY_CONFIG_CRITICAL_START
1051 #define HOTKEY_CONFIG_CRITICAL_END
1052 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1054 static int hotkey_get_wlsw(int *status)
1056 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1057 return -EIO;
1058 return 0;
1062 * Call with hotkey_mutex held
1064 static int hotkey_mask_get(void)
1066 u32 m;
1068 if (tp_features.hotkey_mask) {
1069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1070 return -EIO;
1071 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1074 return 0;
1078 * Call with hotkey_mutex held
1080 static int hotkey_mask_set(u32 mask)
1082 int i;
1083 int rc = 0;
1085 if (tp_features.hotkey_mask) {
1086 HOTKEY_CONFIG_CRITICAL_START
1087 for (i = 0; i < 32; i++) {
1088 u32 m = 1 << i;
1089 if (!acpi_evalf(hkey_handle,
1090 NULL, "MHKM", "vdd", i + 1,
1091 !!(mask & m))) {
1092 rc = -EIO;
1093 break;
1094 } else {
1095 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1098 HOTKEY_CONFIG_CRITICAL_END
1100 /* hotkey_mask_get must be called unconditionally below */
1101 if (!hotkey_mask_get() && !rc &&
1102 (hotkey_mask & ~hotkey_source_mask) !=
1103 (mask & ~hotkey_source_mask)) {
1104 printk(TPACPI_NOTICE
1105 "requested hot key mask 0x%08x, but "
1106 "firmware forced it to 0x%08x\n",
1107 mask, hotkey_mask);
1111 return rc;
1114 static int hotkey_status_get(int *status)
1116 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1117 return -EIO;
1119 return 0;
1122 static int hotkey_status_set(int status)
1124 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1125 return -EIO;
1127 return 0;
1130 static void tpacpi_input_send_radiosw(void)
1132 int wlsw;
1134 mutex_lock(&tpacpi_inputdev_send_mutex);
1136 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1137 input_report_switch(tpacpi_inputdev,
1138 SW_RADIO, !!wlsw);
1139 input_sync(tpacpi_inputdev);
1142 mutex_unlock(&tpacpi_inputdev_send_mutex);
1145 static void tpacpi_input_send_key(unsigned int scancode)
1147 unsigned int keycode;
1149 keycode = hotkey_keycode_map[scancode];
1151 if (keycode != KEY_RESERVED) {
1152 mutex_lock(&tpacpi_inputdev_send_mutex);
1154 input_report_key(tpacpi_inputdev, keycode, 1);
1155 if (keycode == KEY_UNKNOWN)
1156 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1157 scancode);
1158 input_sync(tpacpi_inputdev);
1160 input_report_key(tpacpi_inputdev, keycode, 0);
1161 if (keycode == KEY_UNKNOWN)
1162 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1163 scancode);
1164 input_sync(tpacpi_inputdev);
1166 mutex_unlock(&tpacpi_inputdev_send_mutex);
1170 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1171 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1173 static void tpacpi_hotkey_send_key(unsigned int scancode)
1175 tpacpi_input_send_key(scancode);
1176 if (hotkey_report_mode < 2) {
1177 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1178 0x80, 0x1001 + scancode);
1182 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1184 u8 d;
1186 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1187 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1188 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1189 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1190 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1191 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1193 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1194 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1195 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1197 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1198 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1199 n->displayexp_toggle =
1200 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1202 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1203 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1204 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1205 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1206 n->brightness_toggle =
1207 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1209 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1210 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1211 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1212 >> TP_NVRAM_POS_LEVEL_VOLUME;
1213 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1214 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1218 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1219 do { \
1220 if ((mask & (1 << __scancode)) && \
1221 oldn->__member != newn->__member) \
1222 tpacpi_hotkey_send_key(__scancode); \
1223 } while (0)
1225 #define TPACPI_MAY_SEND_KEY(__scancode) \
1226 do { if (mask & (1 << __scancode)) \
1227 tpacpi_hotkey_send_key(__scancode); } while (0)
1229 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1230 struct tp_nvram_state *newn,
1231 u32 mask)
1233 if (oldn == newn)
1234 return;
1236 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1237 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1238 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1239 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1241 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1243 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1245 /* handle volume */
1246 if (oldn->volume_toggle != newn->volume_toggle) {
1247 if (oldn->mute != newn->mute) {
1248 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1250 if (oldn->volume_level > newn->volume_level) {
1251 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1252 } else if (oldn->volume_level < newn->volume_level) {
1253 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1254 } else if (oldn->mute == newn->mute) {
1255 /* repeated key presses that didn't change state */
1256 if (newn->mute) {
1257 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1258 } else if (newn->volume_level != 0) {
1259 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1260 } else {
1261 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1266 /* handle brightness */
1267 if (oldn->brightness_toggle != newn->brightness_toggle) {
1268 if (oldn->brightness_level < newn->brightness_level) {
1269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1270 } else if (oldn->brightness_level > newn->brightness_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1272 } else {
1273 /* repeated key presses that didn't change state */
1274 if (newn->brightness_level != 0) {
1275 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1276 } else {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1283 #undef TPACPI_COMPARE_KEY
1284 #undef TPACPI_MAY_SEND_KEY
1286 static int hotkey_kthread(void *data)
1288 struct tp_nvram_state s[2];
1289 u32 mask;
1290 unsigned int si, so;
1291 unsigned long t;
1292 unsigned int change_detector, must_reset;
1294 mutex_lock(&hotkey_thread_mutex);
1296 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1297 goto exit;
1299 set_freezable();
1301 so = 0;
1302 si = 1;
1303 t = 0;
1305 /* Initial state for compares */
1306 mutex_lock(&hotkey_thread_data_mutex);
1307 change_detector = hotkey_config_change;
1308 mask = hotkey_source_mask & hotkey_mask;
1309 mutex_unlock(&hotkey_thread_data_mutex);
1310 hotkey_read_nvram(&s[so], mask);
1312 while (!kthread_should_stop() && hotkey_poll_freq) {
1313 if (t == 0)
1314 t = 1000/hotkey_poll_freq;
1315 t = msleep_interruptible(t);
1316 if (unlikely(kthread_should_stop()))
1317 break;
1318 must_reset = try_to_freeze();
1319 if (t > 0 && !must_reset)
1320 continue;
1322 mutex_lock(&hotkey_thread_data_mutex);
1323 if (must_reset || hotkey_config_change != change_detector) {
1324 /* forget old state on thaw or config change */
1325 si = so;
1326 t = 0;
1327 change_detector = hotkey_config_change;
1329 mask = hotkey_source_mask & hotkey_mask;
1330 mutex_unlock(&hotkey_thread_data_mutex);
1332 hotkey_read_nvram(&s[si], mask);
1333 hotkey_compare_and_issue_event(&s[so], &s[si], mask);
1335 so = si;
1336 si ^= 1;
1339 exit:
1340 mutex_unlock(&hotkey_thread_mutex);
1341 return 0;
1344 static void hotkey_poll_stop_sync(void)
1346 if (tpacpi_hotkey_task) {
1347 if (frozen(tpacpi_hotkey_task) ||
1348 freezing(tpacpi_hotkey_task))
1349 thaw_process(tpacpi_hotkey_task);
1351 kthread_stop(tpacpi_hotkey_task);
1352 tpacpi_hotkey_task = NULL;
1353 mutex_lock(&hotkey_thread_mutex);
1354 /* at this point, the thread did exit */
1355 mutex_unlock(&hotkey_thread_mutex);
1359 /* call with hotkey_mutex held */
1360 static void hotkey_poll_setup(int may_warn)
1362 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1363 hotkey_poll_freq > 0 &&
1364 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1365 if (!tpacpi_hotkey_task) {
1366 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1367 NULL,
1368 TPACPI_FILE "d");
1369 if (IS_ERR(tpacpi_hotkey_task)) {
1370 tpacpi_hotkey_task = NULL;
1371 printk(TPACPI_ERR
1372 "could not create kernel thread "
1373 "for hotkey polling\n");
1376 } else {
1377 hotkey_poll_stop_sync();
1378 if (may_warn &&
1379 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1380 printk(TPACPI_NOTICE
1381 "hot keys 0x%08x require polling, "
1382 "which is currently disabled\n",
1383 hotkey_source_mask);
1388 static void hotkey_poll_setup_safe(int may_warn)
1390 mutex_lock(&hotkey_mutex);
1391 hotkey_poll_setup(may_warn);
1392 mutex_unlock(&hotkey_mutex);
1395 static int hotkey_inputdev_open(struct input_dev *dev)
1397 switch (tpacpi_lifecycle) {
1398 case TPACPI_LIFE_INIT:
1400 * hotkey_init will call hotkey_poll_setup_safe
1401 * at the appropriate moment
1403 return 0;
1404 case TPACPI_LIFE_EXITING:
1405 return -EBUSY;
1406 case TPACPI_LIFE_RUNNING:
1407 hotkey_poll_setup_safe(0);
1408 return 0;
1411 /* Should only happen if tpacpi_lifecycle is corrupt */
1412 BUG();
1413 return -EBUSY;
1416 static void hotkey_inputdev_close(struct input_dev *dev)
1418 /* disable hotkey polling when possible */
1419 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1420 hotkey_poll_setup_safe(0);
1422 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1424 /* sysfs hotkey enable ------------------------------------------------- */
1425 static ssize_t hotkey_enable_show(struct device *dev,
1426 struct device_attribute *attr,
1427 char *buf)
1429 int res, status;
1431 res = hotkey_status_get(&status);
1432 if (res)
1433 return res;
1435 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1438 static ssize_t hotkey_enable_store(struct device *dev,
1439 struct device_attribute *attr,
1440 const char *buf, size_t count)
1442 unsigned long t;
1443 int res;
1445 if (parse_strtoul(buf, 1, &t))
1446 return -EINVAL;
1448 res = hotkey_status_set(t);
1450 return (res) ? res : count;
1453 static struct device_attribute dev_attr_hotkey_enable =
1454 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1455 hotkey_enable_show, hotkey_enable_store);
1457 /* sysfs hotkey mask --------------------------------------------------- */
1458 static ssize_t hotkey_mask_show(struct device *dev,
1459 struct device_attribute *attr,
1460 char *buf)
1462 int res;
1464 if (mutex_lock_interruptible(&hotkey_mutex))
1465 return -ERESTARTSYS;
1466 res = hotkey_mask_get();
1467 mutex_unlock(&hotkey_mutex);
1469 return (res)?
1470 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1473 static ssize_t hotkey_mask_store(struct device *dev,
1474 struct device_attribute *attr,
1475 const char *buf, size_t count)
1477 unsigned long t;
1478 int res;
1480 if (parse_strtoul(buf, 0xffffffffUL, &t))
1481 return -EINVAL;
1483 if (mutex_lock_interruptible(&hotkey_mutex))
1484 return -ERESTARTSYS;
1486 res = hotkey_mask_set(t);
1488 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1489 hotkey_poll_setup(1);
1490 #endif
1492 mutex_unlock(&hotkey_mutex);
1494 return (res) ? res : count;
1497 static struct device_attribute dev_attr_hotkey_mask =
1498 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1499 hotkey_mask_show, hotkey_mask_store);
1501 /* sysfs hotkey bios_enabled ------------------------------------------- */
1502 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1503 struct device_attribute *attr,
1504 char *buf)
1506 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1509 static struct device_attribute dev_attr_hotkey_bios_enabled =
1510 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1512 /* sysfs hotkey bios_mask ---------------------------------------------- */
1513 static ssize_t hotkey_bios_mask_show(struct device *dev,
1514 struct device_attribute *attr,
1515 char *buf)
1517 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1520 static struct device_attribute dev_attr_hotkey_bios_mask =
1521 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1523 /* sysfs hotkey all_mask ----------------------------------------------- */
1524 static ssize_t hotkey_all_mask_show(struct device *dev,
1525 struct device_attribute *attr,
1526 char *buf)
1528 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1529 hotkey_all_mask | hotkey_source_mask);
1532 static struct device_attribute dev_attr_hotkey_all_mask =
1533 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1535 /* sysfs hotkey recommended_mask --------------------------------------- */
1536 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1537 struct device_attribute *attr,
1538 char *buf)
1540 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1541 (hotkey_all_mask | hotkey_source_mask)
1542 & ~hotkey_reserved_mask);
1545 static struct device_attribute dev_attr_hotkey_recommended_mask =
1546 __ATTR(hotkey_recommended_mask, S_IRUGO,
1547 hotkey_recommended_mask_show, NULL);
1549 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1551 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1552 static ssize_t hotkey_source_mask_show(struct device *dev,
1553 struct device_attribute *attr,
1554 char *buf)
1556 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1559 static ssize_t hotkey_source_mask_store(struct device *dev,
1560 struct device_attribute *attr,
1561 const char *buf, size_t count)
1563 unsigned long t;
1565 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1566 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1567 return -EINVAL;
1569 if (mutex_lock_interruptible(&hotkey_mutex))
1570 return -ERESTARTSYS;
1572 HOTKEY_CONFIG_CRITICAL_START
1573 hotkey_source_mask = t;
1574 HOTKEY_CONFIG_CRITICAL_END
1576 hotkey_poll_setup(1);
1578 mutex_unlock(&hotkey_mutex);
1580 return count;
1583 static struct device_attribute dev_attr_hotkey_source_mask =
1584 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1585 hotkey_source_mask_show, hotkey_source_mask_store);
1587 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1588 static ssize_t hotkey_poll_freq_show(struct device *dev,
1589 struct device_attribute *attr,
1590 char *buf)
1592 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1595 static ssize_t hotkey_poll_freq_store(struct device *dev,
1596 struct device_attribute *attr,
1597 const char *buf, size_t count)
1599 unsigned long t;
1601 if (parse_strtoul(buf, 25, &t))
1602 return -EINVAL;
1604 if (mutex_lock_interruptible(&hotkey_mutex))
1605 return -ERESTARTSYS;
1607 hotkey_poll_freq = t;
1609 hotkey_poll_setup(1);
1610 mutex_unlock(&hotkey_mutex);
1612 return count;
1615 static struct device_attribute dev_attr_hotkey_poll_freq =
1616 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1617 hotkey_poll_freq_show, hotkey_poll_freq_store);
1619 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1621 /* sysfs hotkey radio_sw ----------------------------------------------- */
1622 static ssize_t hotkey_radio_sw_show(struct device *dev,
1623 struct device_attribute *attr,
1624 char *buf)
1626 int res, s;
1627 res = hotkey_get_wlsw(&s);
1628 if (res < 0)
1629 return res;
1631 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1634 static struct device_attribute dev_attr_hotkey_radio_sw =
1635 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1637 /* sysfs hotkey report_mode -------------------------------------------- */
1638 static ssize_t hotkey_report_mode_show(struct device *dev,
1639 struct device_attribute *attr,
1640 char *buf)
1642 return snprintf(buf, PAGE_SIZE, "%d\n",
1643 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1646 static struct device_attribute dev_attr_hotkey_report_mode =
1647 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1649 /* sysfs wakeup reason ------------------------------------------------- */
1650 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1651 struct device_attribute *attr,
1652 char *buf)
1654 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1657 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1658 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1660 /* sysfs wakeup hotunplug_complete ------------------------------------- */
1661 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1662 struct device_attribute *attr,
1663 char *buf)
1665 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1668 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1669 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1670 hotkey_wakeup_hotunplug_complete_show, NULL);
1672 /* --------------------------------------------------------------------- */
1674 static struct attribute *hotkey_attributes[] __initdata = {
1675 &dev_attr_hotkey_enable.attr,
1676 &dev_attr_hotkey_report_mode.attr,
1679 static struct attribute *hotkey_mask_attributes[] __initdata = {
1680 &dev_attr_hotkey_mask.attr,
1681 &dev_attr_hotkey_bios_enabled.attr,
1682 &dev_attr_hotkey_bios_mask.attr,
1683 &dev_attr_hotkey_all_mask.attr,
1684 &dev_attr_hotkey_recommended_mask.attr,
1685 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1686 &dev_attr_hotkey_source_mask.attr,
1687 &dev_attr_hotkey_poll_freq.attr,
1688 #endif
1689 &dev_attr_hotkey_wakeup_reason.attr,
1690 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1693 static int __init hotkey_init(struct ibm_init_struct *iibm)
1696 static u16 ibm_keycode_map[] __initdata = {
1697 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1698 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1699 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1700 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1701 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1702 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1703 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1704 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1705 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1706 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1707 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1708 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1709 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1710 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1711 KEY_RESERVED, /* 0x14: VOLUME UP */
1712 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1713 KEY_RESERVED, /* 0x16: MUTE */
1714 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1715 /* (assignments unknown, please report if found) */
1716 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1717 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1719 static u16 lenovo_keycode_map[] __initdata = {
1720 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1721 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1722 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1723 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1724 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1725 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1726 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1727 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1728 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1729 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1730 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1731 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1732 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1733 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1734 KEY_RESERVED, /* 0x14: VOLUME UP */
1735 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1736 KEY_RESERVED, /* 0x16: MUTE */
1737 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1738 /* (assignments unknown, please report if found) */
1739 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1740 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1743 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1744 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1745 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1747 int res, i;
1748 int status;
1749 int hkeyv;
1751 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1753 BUG_ON(!tpacpi_inputdev);
1754 BUG_ON(tpacpi_inputdev->open != NULL ||
1755 tpacpi_inputdev->close != NULL);
1757 TPACPI_ACPIHANDLE_INIT(hkey);
1758 mutex_init(&hotkey_mutex);
1760 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1761 mutex_init(&hotkey_thread_mutex);
1762 mutex_init(&hotkey_thread_data_mutex);
1763 #endif
1765 /* hotkey not supported on 570 */
1766 tp_features.hotkey = hkey_handle != NULL;
1768 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1769 str_supported(tp_features.hotkey));
1771 if (tp_features.hotkey) {
1772 hotkey_dev_attributes = create_attr_set(12, NULL);
1773 if (!hotkey_dev_attributes)
1774 return -ENOMEM;
1775 res = add_many_to_attr_set(hotkey_dev_attributes,
1776 hotkey_attributes,
1777 ARRAY_SIZE(hotkey_attributes));
1778 if (res)
1779 return res;
1781 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1782 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1783 for HKEY interface version 0x100 */
1784 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1785 if ((hkeyv >> 8) != 1) {
1786 printk(TPACPI_ERR "unknown version of the "
1787 "HKEY interface: 0x%x\n", hkeyv);
1788 printk(TPACPI_ERR "please report this to %s\n",
1789 TPACPI_MAIL);
1790 } else {
1792 * MHKV 0x100 in A31, R40, R40e,
1793 * T4x, X31, and later
1795 tp_features.hotkey_mask = 1;
1799 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1800 str_supported(tp_features.hotkey_mask));
1802 if (tp_features.hotkey_mask) {
1803 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1804 "MHKA", "qd")) {
1805 printk(TPACPI_ERR
1806 "missing MHKA handler, "
1807 "please report this to %s\n",
1808 TPACPI_MAIL);
1809 /* FN+F12, FN+F4, FN+F3 */
1810 hotkey_all_mask = 0x080cU;
1814 /* hotkey_source_mask *must* be zero for
1815 * the first hotkey_mask_get */
1816 res = hotkey_status_get(&hotkey_orig_status);
1817 if (!res && tp_features.hotkey_mask) {
1818 res = hotkey_mask_get();
1819 hotkey_orig_mask = hotkey_mask;
1820 if (!res) {
1821 res = add_many_to_attr_set(
1822 hotkey_dev_attributes,
1823 hotkey_mask_attributes,
1824 ARRAY_SIZE(hotkey_mask_attributes));
1828 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1829 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1830 & ~hotkey_all_mask;
1832 vdbg_printk(TPACPI_DBG_INIT,
1833 "hotkey source mask 0x%08x, polling freq %d\n",
1834 hotkey_source_mask, hotkey_poll_freq);
1835 #endif
1837 /* Not all thinkpads have a hardware radio switch */
1838 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1839 tp_features.hotkey_wlsw = 1;
1840 printk(TPACPI_INFO
1841 "radio switch found; radios are %s\n",
1842 enabled(status, 0));
1843 res = add_to_attr_set(hotkey_dev_attributes,
1844 &dev_attr_hotkey_radio_sw.attr);
1847 if (!res)
1848 res = register_attr_set_with_sysfs(
1849 hotkey_dev_attributes,
1850 &tpacpi_pdev->dev.kobj);
1851 if (res)
1852 return res;
1854 /* Set up key map */
1856 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1857 GFP_KERNEL);
1858 if (!hotkey_keycode_map) {
1859 printk(TPACPI_ERR
1860 "failed to allocate memory for key map\n");
1861 return -ENOMEM;
1864 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1865 dbg_printk(TPACPI_DBG_INIT,
1866 "using Lenovo default hot key map\n");
1867 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1868 TPACPI_HOTKEY_MAP_SIZE);
1869 } else {
1870 dbg_printk(TPACPI_DBG_INIT,
1871 "using IBM default hot key map\n");
1872 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1873 TPACPI_HOTKEY_MAP_SIZE);
1876 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1877 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1878 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1879 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1880 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1881 tpacpi_inputdev->keycode = hotkey_keycode_map;
1882 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1883 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1884 set_bit(hotkey_keycode_map[i],
1885 tpacpi_inputdev->keybit);
1886 } else {
1887 if (i < sizeof(hotkey_reserved_mask)*8)
1888 hotkey_reserved_mask |= 1 << i;
1892 if (tp_features.hotkey_wlsw) {
1893 set_bit(EV_SW, tpacpi_inputdev->evbit);
1894 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1897 dbg_printk(TPACPI_DBG_INIT,
1898 "enabling hot key handling\n");
1899 res = hotkey_status_set(1);
1900 if (res)
1901 return res;
1902 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1903 & ~hotkey_reserved_mask)
1904 | hotkey_orig_mask);
1905 if (res)
1906 return res;
1908 dbg_printk(TPACPI_DBG_INIT,
1909 "legacy hot key reporting over procfs %s\n",
1910 (hotkey_report_mode < 2) ?
1911 "enabled" : "disabled");
1913 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1914 tpacpi_inputdev->open = &hotkey_inputdev_open;
1915 tpacpi_inputdev->close = &hotkey_inputdev_close;
1917 hotkey_poll_setup_safe(1);
1918 #endif
1921 return (tp_features.hotkey)? 0 : 1;
1924 static void hotkey_exit(void)
1926 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1927 hotkey_poll_stop_sync();
1928 #endif
1930 if (tp_features.hotkey) {
1931 dbg_printk(TPACPI_DBG_EXIT,
1932 "restoring original hot key mask\n");
1933 /* no short-circuit boolean operator below! */
1934 if ((hotkey_mask_set(hotkey_orig_mask) |
1935 hotkey_status_set(hotkey_orig_status)) != 0)
1936 printk(TPACPI_ERR
1937 "failed to restore hot key mask "
1938 "to BIOS defaults\n");
1941 if (hotkey_dev_attributes) {
1942 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1943 hotkey_dev_attributes = NULL;
1947 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1949 u32 hkey;
1950 unsigned int scancode;
1951 int send_acpi_ev;
1952 int ignore_acpi_ev;
1953 int unk_ev;
1955 if (event != 0x80) {
1956 printk(TPACPI_ERR
1957 "unknown HKEY notification event %d\n", event);
1958 /* forward it to userspace, maybe it knows how to handle it */
1959 acpi_bus_generate_netlink_event(
1960 ibm->acpi->device->pnp.device_class,
1961 ibm->acpi->device->dev.bus_id,
1962 event, 0);
1963 return;
1966 while (1) {
1967 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1968 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
1969 return;
1972 if (hkey == 0) {
1973 /* queue empty */
1974 return;
1977 send_acpi_ev = 1;
1978 ignore_acpi_ev = 0;
1979 unk_ev = 0;
1981 switch (hkey >> 12) {
1982 case 1:
1983 /* 0x1000-0x1FFF: key presses */
1984 scancode = hkey & 0xfff;
1985 if (scancode > 0 && scancode < 0x21) {
1986 scancode--;
1987 if (!(hotkey_source_mask & (1 << scancode))) {
1988 tpacpi_input_send_key(scancode);
1989 send_acpi_ev = 0;
1990 } else {
1991 ignore_acpi_ev = 1;
1993 } else {
1994 unk_ev = 1;
1996 break;
1997 case 2:
1998 /* Wakeup reason */
1999 switch (hkey) {
2000 case 0x2304: /* suspend, undock */
2001 case 0x2404: /* hibernation, undock */
2002 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2003 ignore_acpi_ev = 1;
2004 break;
2005 case 0x2305: /* suspend, bay eject */
2006 case 0x2405: /* hibernation, bay eject */
2007 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2008 ignore_acpi_ev = 1;
2009 break;
2010 default:
2011 unk_ev = 1;
2013 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2014 printk(TPACPI_INFO
2015 "woke up due to a hot-unplug "
2016 "request...\n");
2018 break;
2019 case 3:
2020 /* bay-related wakeups */
2021 if (hkey == 0x3003) {
2022 hotkey_autosleep_ack = 1;
2023 printk(TPACPI_INFO
2024 "bay ejected\n");
2025 } else {
2026 unk_ev = 1;
2028 break;
2029 case 4:
2030 /* dock-related wakeups */
2031 if (hkey == 0x4003) {
2032 hotkey_autosleep_ack = 1;
2033 printk(TPACPI_INFO
2034 "undocked\n");
2035 } else {
2036 unk_ev = 1;
2038 break;
2039 case 5:
2040 /* 0x5000-0x5FFF: On screen display helpers */
2041 switch (hkey) {
2042 case 0x5010:
2043 /* Lenovo Vista BIOS: brightness changed */
2044 break;
2045 case 0x5001:
2046 case 0x5002:
2047 /* LID switch events. Do not propagate */
2048 ignore_acpi_ev = 1;
2049 break;
2050 default:
2051 unk_ev = 1;
2053 break;
2054 case 7:
2055 /* 0x7000-0x7FFF: misc */
2056 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2057 tpacpi_input_send_radiosw();
2058 send_acpi_ev = 0;
2059 break;
2061 /* fallthrough to default */
2062 default:
2063 unk_ev = 1;
2065 if (unk_ev) {
2066 printk(TPACPI_NOTICE
2067 "unhandled HKEY event 0x%04x\n", hkey);
2070 /* Legacy events */
2071 if (!ignore_acpi_ev &&
2072 (send_acpi_ev || hotkey_report_mode < 2)) {
2073 acpi_bus_generate_proc_event(ibm->acpi->device,
2074 event, hkey);
2077 /* netlink events */
2078 if (!ignore_acpi_ev && send_acpi_ev) {
2079 acpi_bus_generate_netlink_event(
2080 ibm->acpi->device->pnp.device_class,
2081 ibm->acpi->device->dev.bus_id,
2082 event, hkey);
2087 static void hotkey_suspend(pm_message_t state)
2089 /* Do these on suspend, we get the events on early resume! */
2090 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2091 hotkey_autosleep_ack = 0;
2094 static void hotkey_resume(void)
2096 if (hotkey_mask_get())
2097 printk(TPACPI_ERR
2098 "error while trying to read hot key mask "
2099 "from firmware\n");
2100 tpacpi_input_send_radiosw();
2101 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2102 hotkey_poll_setup_safe(0);
2103 #endif
2106 /* procfs -------------------------------------------------------------- */
2107 static int hotkey_read(char *p)
2109 int res, status;
2110 int len = 0;
2112 if (!tp_features.hotkey) {
2113 len += sprintf(p + len, "status:\t\tnot supported\n");
2114 return len;
2117 if (mutex_lock_interruptible(&hotkey_mutex))
2118 return -ERESTARTSYS;
2119 res = hotkey_status_get(&status);
2120 if (!res)
2121 res = hotkey_mask_get();
2122 mutex_unlock(&hotkey_mutex);
2123 if (res)
2124 return res;
2126 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2127 if (tp_features.hotkey_mask) {
2128 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2129 len += sprintf(p + len,
2130 "commands:\tenable, disable, reset, <mask>\n");
2131 } else {
2132 len += sprintf(p + len, "mask:\t\tnot supported\n");
2133 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2136 return len;
2139 static int hotkey_write(char *buf)
2141 int res, status;
2142 u32 mask;
2143 char *cmd;
2145 if (!tp_features.hotkey)
2146 return -ENODEV;
2148 if (mutex_lock_interruptible(&hotkey_mutex))
2149 return -ERESTARTSYS;
2151 status = -1;
2152 mask = hotkey_mask;
2154 res = 0;
2155 while ((cmd = next_cmd(&buf))) {
2156 if (strlencmp(cmd, "enable") == 0) {
2157 status = 1;
2158 } else if (strlencmp(cmd, "disable") == 0) {
2159 status = 0;
2160 } else if (strlencmp(cmd, "reset") == 0) {
2161 status = hotkey_orig_status;
2162 mask = hotkey_orig_mask;
2163 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2164 /* mask set */
2165 } else if (sscanf(cmd, "%x", &mask) == 1) {
2166 /* mask set */
2167 } else {
2168 res = -EINVAL;
2169 goto errexit;
2172 if (status != -1)
2173 res = hotkey_status_set(status);
2175 if (!res && mask != hotkey_mask)
2176 res = hotkey_mask_set(mask);
2178 errexit:
2179 mutex_unlock(&hotkey_mutex);
2180 return res;
2183 static const struct acpi_device_id ibm_htk_device_ids[] = {
2184 {TPACPI_ACPI_HKEY_HID, 0},
2185 {"", 0},
2188 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2189 .hid = ibm_htk_device_ids,
2190 .notify = hotkey_notify,
2191 .handle = &hkey_handle,
2192 .type = ACPI_DEVICE_NOTIFY,
2195 static struct ibm_struct hotkey_driver_data = {
2196 .name = "hotkey",
2197 .read = hotkey_read,
2198 .write = hotkey_write,
2199 .exit = hotkey_exit,
2200 .resume = hotkey_resume,
2201 .suspend = hotkey_suspend,
2202 .acpi = &ibm_hotkey_acpidriver,
2205 /*************************************************************************
2206 * Bluetooth subdriver
2209 enum {
2210 /* ACPI GBDC/SBDC bits */
2211 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2212 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2213 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2216 static int bluetooth_get_radiosw(void);
2217 static int bluetooth_set_radiosw(int radio_on);
2219 /* sysfs bluetooth enable ---------------------------------------------- */
2220 static ssize_t bluetooth_enable_show(struct device *dev,
2221 struct device_attribute *attr,
2222 char *buf)
2224 int status;
2226 status = bluetooth_get_radiosw();
2227 if (status < 0)
2228 return status;
2230 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2233 static ssize_t bluetooth_enable_store(struct device *dev,
2234 struct device_attribute *attr,
2235 const char *buf, size_t count)
2237 unsigned long t;
2238 int res;
2240 if (parse_strtoul(buf, 1, &t))
2241 return -EINVAL;
2243 res = bluetooth_set_radiosw(t);
2245 return (res) ? res : count;
2248 static struct device_attribute dev_attr_bluetooth_enable =
2249 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2250 bluetooth_enable_show, bluetooth_enable_store);
2252 /* --------------------------------------------------------------------- */
2254 static struct attribute *bluetooth_attributes[] = {
2255 &dev_attr_bluetooth_enable.attr,
2256 NULL
2259 static const struct attribute_group bluetooth_attr_group = {
2260 .attrs = bluetooth_attributes,
2263 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2265 int res;
2266 int status = 0;
2268 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2270 TPACPI_ACPIHANDLE_INIT(hkey);
2272 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2273 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2274 tp_features.bluetooth = hkey_handle &&
2275 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2277 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2278 str_supported(tp_features.bluetooth),
2279 status);
2281 if (tp_features.bluetooth) {
2282 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2283 /* no bluetooth hardware present in system */
2284 tp_features.bluetooth = 0;
2285 dbg_printk(TPACPI_DBG_INIT,
2286 "bluetooth hardware not installed\n");
2287 } else {
2288 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2289 &bluetooth_attr_group);
2290 if (res)
2291 return res;
2295 return (tp_features.bluetooth)? 0 : 1;
2298 static void bluetooth_exit(void)
2300 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2301 &bluetooth_attr_group);
2304 static int bluetooth_get_radiosw(void)
2306 int status;
2308 if (!tp_features.bluetooth)
2309 return -ENODEV;
2311 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2312 return -EIO;
2314 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2317 static int bluetooth_set_radiosw(int radio_on)
2319 int status;
2321 if (!tp_features.bluetooth)
2322 return -ENODEV;
2324 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2325 return -EIO;
2326 if (radio_on)
2327 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2328 else
2329 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2330 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2331 return -EIO;
2333 return 0;
2336 /* procfs -------------------------------------------------------------- */
2337 static int bluetooth_read(char *p)
2339 int len = 0;
2340 int status = bluetooth_get_radiosw();
2342 if (!tp_features.bluetooth)
2343 len += sprintf(p + len, "status:\t\tnot supported\n");
2344 else {
2345 len += sprintf(p + len, "status:\t\t%s\n",
2346 (status)? "enabled" : "disabled");
2347 len += sprintf(p + len, "commands:\tenable, disable\n");
2350 return len;
2353 static int bluetooth_write(char *buf)
2355 char *cmd;
2357 if (!tp_features.bluetooth)
2358 return -ENODEV;
2360 while ((cmd = next_cmd(&buf))) {
2361 if (strlencmp(cmd, "enable") == 0) {
2362 bluetooth_set_radiosw(1);
2363 } else if (strlencmp(cmd, "disable") == 0) {
2364 bluetooth_set_radiosw(0);
2365 } else
2366 return -EINVAL;
2369 return 0;
2372 static struct ibm_struct bluetooth_driver_data = {
2373 .name = "bluetooth",
2374 .read = bluetooth_read,
2375 .write = bluetooth_write,
2376 .exit = bluetooth_exit,
2379 /*************************************************************************
2380 * Wan subdriver
2383 enum {
2384 /* ACPI GWAN/SWAN bits */
2385 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2386 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2387 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2390 static int wan_get_radiosw(void);
2391 static int wan_set_radiosw(int radio_on);
2393 /* sysfs wan enable ---------------------------------------------------- */
2394 static ssize_t wan_enable_show(struct device *dev,
2395 struct device_attribute *attr,
2396 char *buf)
2398 int status;
2400 status = wan_get_radiosw();
2401 if (status < 0)
2402 return status;
2404 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2407 static ssize_t wan_enable_store(struct device *dev,
2408 struct device_attribute *attr,
2409 const char *buf, size_t count)
2411 unsigned long t;
2412 int res;
2414 if (parse_strtoul(buf, 1, &t))
2415 return -EINVAL;
2417 res = wan_set_radiosw(t);
2419 return (res) ? res : count;
2422 static struct device_attribute dev_attr_wan_enable =
2423 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2424 wan_enable_show, wan_enable_store);
2426 /* --------------------------------------------------------------------- */
2428 static struct attribute *wan_attributes[] = {
2429 &dev_attr_wan_enable.attr,
2430 NULL
2433 static const struct attribute_group wan_attr_group = {
2434 .attrs = wan_attributes,
2437 static int __init wan_init(struct ibm_init_struct *iibm)
2439 int res;
2440 int status = 0;
2442 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2444 TPACPI_ACPIHANDLE_INIT(hkey);
2446 tp_features.wan = hkey_handle &&
2447 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2449 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2450 str_supported(tp_features.wan),
2451 status);
2453 if (tp_features.wan) {
2454 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2455 /* no wan hardware present in system */
2456 tp_features.wan = 0;
2457 dbg_printk(TPACPI_DBG_INIT,
2458 "wan hardware not installed\n");
2459 } else {
2460 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2461 &wan_attr_group);
2462 if (res)
2463 return res;
2467 return (tp_features.wan)? 0 : 1;
2470 static void wan_exit(void)
2472 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2473 &wan_attr_group);
2476 static int wan_get_radiosw(void)
2478 int status;
2480 if (!tp_features.wan)
2481 return -ENODEV;
2483 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2484 return -EIO;
2486 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2489 static int wan_set_radiosw(int radio_on)
2491 int status;
2493 if (!tp_features.wan)
2494 return -ENODEV;
2496 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2497 return -EIO;
2498 if (radio_on)
2499 status |= TP_ACPI_WANCARD_RADIOSSW;
2500 else
2501 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2502 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2503 return -EIO;
2505 return 0;
2508 /* procfs -------------------------------------------------------------- */
2509 static int wan_read(char *p)
2511 int len = 0;
2512 int status = wan_get_radiosw();
2514 if (!tp_features.wan)
2515 len += sprintf(p + len, "status:\t\tnot supported\n");
2516 else {
2517 len += sprintf(p + len, "status:\t\t%s\n",
2518 (status)? "enabled" : "disabled");
2519 len += sprintf(p + len, "commands:\tenable, disable\n");
2522 return len;
2525 static int wan_write(char *buf)
2527 char *cmd;
2529 if (!tp_features.wan)
2530 return -ENODEV;
2532 while ((cmd = next_cmd(&buf))) {
2533 if (strlencmp(cmd, "enable") == 0) {
2534 wan_set_radiosw(1);
2535 } else if (strlencmp(cmd, "disable") == 0) {
2536 wan_set_radiosw(0);
2537 } else
2538 return -EINVAL;
2541 return 0;
2544 static struct ibm_struct wan_driver_data = {
2545 .name = "wan",
2546 .read = wan_read,
2547 .write = wan_write,
2548 .exit = wan_exit,
2549 .flags.experimental = 1,
2552 /*************************************************************************
2553 * Video subdriver
2556 enum video_access_mode {
2557 TPACPI_VIDEO_NONE = 0,
2558 TPACPI_VIDEO_570, /* 570 */
2559 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2560 TPACPI_VIDEO_NEW, /* all others */
2563 enum { /* video status flags, based on VIDEO_570 */
2564 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2565 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2566 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2569 enum { /* TPACPI_VIDEO_570 constants */
2570 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2571 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2572 * video_status_flags */
2573 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2574 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2577 static enum video_access_mode video_supported;
2578 static int video_orig_autosw;
2580 static int video_autosw_get(void);
2581 static int video_autosw_set(int enable);
2583 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2584 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2585 "\\_SB.PCI0.VID0", /* 770e */
2586 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2587 "\\_SB.PCI0.AGP.VID", /* all others */
2588 ); /* R30, R31 */
2590 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2592 static int __init video_init(struct ibm_init_struct *iibm)
2594 int ivga;
2596 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2598 TPACPI_ACPIHANDLE_INIT(vid);
2599 TPACPI_ACPIHANDLE_INIT(vid2);
2601 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2602 /* G41, assume IVGA doesn't change */
2603 vid_handle = vid2_handle;
2605 if (!vid_handle)
2606 /* video switching not supported on R30, R31 */
2607 video_supported = TPACPI_VIDEO_NONE;
2608 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2609 /* 570 */
2610 video_supported = TPACPI_VIDEO_570;
2611 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2612 /* 600e/x, 770e, 770x */
2613 video_supported = TPACPI_VIDEO_770;
2614 else
2615 /* all others */
2616 video_supported = TPACPI_VIDEO_NEW;
2618 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2619 str_supported(video_supported != TPACPI_VIDEO_NONE),
2620 video_supported);
2622 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2625 static void video_exit(void)
2627 dbg_printk(TPACPI_DBG_EXIT,
2628 "restoring original video autoswitch mode\n");
2629 if (video_autosw_set(video_orig_autosw))
2630 printk(TPACPI_ERR "error while trying to restore original "
2631 "video autoswitch mode\n");
2634 static int video_outputsw_get(void)
2636 int status = 0;
2637 int i;
2639 switch (video_supported) {
2640 case TPACPI_VIDEO_570:
2641 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2642 TP_ACPI_VIDEO_570_PHSCMD))
2643 return -EIO;
2644 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2645 break;
2646 case TPACPI_VIDEO_770:
2647 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2648 return -EIO;
2649 if (i)
2650 status |= TP_ACPI_VIDEO_S_LCD;
2651 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2652 return -EIO;
2653 if (i)
2654 status |= TP_ACPI_VIDEO_S_CRT;
2655 break;
2656 case TPACPI_VIDEO_NEW:
2657 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2658 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2659 return -EIO;
2660 if (i)
2661 status |= TP_ACPI_VIDEO_S_CRT;
2663 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2664 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2665 return -EIO;
2666 if (i)
2667 status |= TP_ACPI_VIDEO_S_LCD;
2668 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2669 return -EIO;
2670 if (i)
2671 status |= TP_ACPI_VIDEO_S_DVI;
2672 break;
2673 default:
2674 return -ENOSYS;
2677 return status;
2680 static int video_outputsw_set(int status)
2682 int autosw;
2683 int res = 0;
2685 switch (video_supported) {
2686 case TPACPI_VIDEO_570:
2687 res = acpi_evalf(NULL, NULL,
2688 "\\_SB.PHS2", "vdd",
2689 TP_ACPI_VIDEO_570_PHS2CMD,
2690 status | TP_ACPI_VIDEO_570_PHS2SET);
2691 break;
2692 case TPACPI_VIDEO_770:
2693 autosw = video_autosw_get();
2694 if (autosw < 0)
2695 return autosw;
2697 res = video_autosw_set(1);
2698 if (res)
2699 return res;
2700 res = acpi_evalf(vid_handle, NULL,
2701 "ASWT", "vdd", status * 0x100, 0);
2702 if (!autosw && video_autosw_set(autosw)) {
2703 printk(TPACPI_ERR
2704 "video auto-switch left enabled due to error\n");
2705 return -EIO;
2707 break;
2708 case TPACPI_VIDEO_NEW:
2709 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2710 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2711 break;
2712 default:
2713 return -ENOSYS;
2716 return (res)? 0 : -EIO;
2719 static int video_autosw_get(void)
2721 int autosw = 0;
2723 switch (video_supported) {
2724 case TPACPI_VIDEO_570:
2725 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2726 return -EIO;
2727 break;
2728 case TPACPI_VIDEO_770:
2729 case TPACPI_VIDEO_NEW:
2730 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2731 return -EIO;
2732 break;
2733 default:
2734 return -ENOSYS;
2737 return autosw & 1;
2740 static int video_autosw_set(int enable)
2742 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2743 return -EIO;
2744 return 0;
2747 static int video_outputsw_cycle(void)
2749 int autosw = video_autosw_get();
2750 int res;
2752 if (autosw < 0)
2753 return autosw;
2755 switch (video_supported) {
2756 case TPACPI_VIDEO_570:
2757 res = video_autosw_set(1);
2758 if (res)
2759 return res;
2760 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2761 break;
2762 case TPACPI_VIDEO_770:
2763 case TPACPI_VIDEO_NEW:
2764 res = video_autosw_set(1);
2765 if (res)
2766 return res;
2767 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2768 break;
2769 default:
2770 return -ENOSYS;
2772 if (!autosw && video_autosw_set(autosw)) {
2773 printk(TPACPI_ERR
2774 "video auto-switch left enabled due to error\n");
2775 return -EIO;
2778 return (res)? 0 : -EIO;
2781 static int video_expand_toggle(void)
2783 switch (video_supported) {
2784 case TPACPI_VIDEO_570:
2785 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2786 0 : -EIO;
2787 case TPACPI_VIDEO_770:
2788 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2789 0 : -EIO;
2790 case TPACPI_VIDEO_NEW:
2791 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2792 0 : -EIO;
2793 default:
2794 return -ENOSYS;
2796 /* not reached */
2799 static int video_read(char *p)
2801 int status, autosw;
2802 int len = 0;
2804 if (video_supported == TPACPI_VIDEO_NONE) {
2805 len += sprintf(p + len, "status:\t\tnot supported\n");
2806 return len;
2809 status = video_outputsw_get();
2810 if (status < 0)
2811 return status;
2813 autosw = video_autosw_get();
2814 if (autosw < 0)
2815 return autosw;
2817 len += sprintf(p + len, "status:\t\tsupported\n");
2818 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2819 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2820 if (video_supported == TPACPI_VIDEO_NEW)
2821 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2822 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2823 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2824 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2825 if (video_supported == TPACPI_VIDEO_NEW)
2826 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2827 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2828 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2830 return len;
2833 static int video_write(char *buf)
2835 char *cmd;
2836 int enable, disable, status;
2837 int res;
2839 if (video_supported == TPACPI_VIDEO_NONE)
2840 return -ENODEV;
2842 enable = 0;
2843 disable = 0;
2845 while ((cmd = next_cmd(&buf))) {
2846 if (strlencmp(cmd, "lcd_enable") == 0) {
2847 enable |= TP_ACPI_VIDEO_S_LCD;
2848 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2849 disable |= TP_ACPI_VIDEO_S_LCD;
2850 } else if (strlencmp(cmd, "crt_enable") == 0) {
2851 enable |= TP_ACPI_VIDEO_S_CRT;
2852 } else if (strlencmp(cmd, "crt_disable") == 0) {
2853 disable |= TP_ACPI_VIDEO_S_CRT;
2854 } else if (video_supported == TPACPI_VIDEO_NEW &&
2855 strlencmp(cmd, "dvi_enable") == 0) {
2856 enable |= TP_ACPI_VIDEO_S_DVI;
2857 } else if (video_supported == TPACPI_VIDEO_NEW &&
2858 strlencmp(cmd, "dvi_disable") == 0) {
2859 disable |= TP_ACPI_VIDEO_S_DVI;
2860 } else if (strlencmp(cmd, "auto_enable") == 0) {
2861 res = video_autosw_set(1);
2862 if (res)
2863 return res;
2864 } else if (strlencmp(cmd, "auto_disable") == 0) {
2865 res = video_autosw_set(0);
2866 if (res)
2867 return res;
2868 } else if (strlencmp(cmd, "video_switch") == 0) {
2869 res = video_outputsw_cycle();
2870 if (res)
2871 return res;
2872 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2873 res = video_expand_toggle();
2874 if (res)
2875 return res;
2876 } else
2877 return -EINVAL;
2880 if (enable || disable) {
2881 status = video_outputsw_get();
2882 if (status < 0)
2883 return status;
2884 res = video_outputsw_set((status & ~disable) | enable);
2885 if (res)
2886 return res;
2889 return 0;
2892 static struct ibm_struct video_driver_data = {
2893 .name = "video",
2894 .read = video_read,
2895 .write = video_write,
2896 .exit = video_exit,
2899 /*************************************************************************
2900 * Light (thinklight) subdriver
2903 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2904 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
2906 static int __init light_init(struct ibm_init_struct *iibm)
2908 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2910 TPACPI_ACPIHANDLE_INIT(ledb);
2911 TPACPI_ACPIHANDLE_INIT(lght);
2912 TPACPI_ACPIHANDLE_INIT(cmos);
2914 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2915 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2917 if (tp_features.light)
2918 /* light status not supported on
2919 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2920 tp_features.light_status =
2921 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2923 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2924 str_supported(tp_features.light));
2926 return (tp_features.light)? 0 : 1;
2929 static int light_read(char *p)
2931 int len = 0;
2932 int status = 0;
2934 if (!tp_features.light) {
2935 len += sprintf(p + len, "status:\t\tnot supported\n");
2936 } else if (!tp_features.light_status) {
2937 len += sprintf(p + len, "status:\t\tunknown\n");
2938 len += sprintf(p + len, "commands:\ton, off\n");
2939 } else {
2940 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2941 return -EIO;
2942 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2943 len += sprintf(p + len, "commands:\ton, off\n");
2946 return len;
2949 static int light_write(char *buf)
2951 int cmos_cmd, lght_cmd;
2952 char *cmd;
2953 int success;
2955 if (!tp_features.light)
2956 return -ENODEV;
2958 while ((cmd = next_cmd(&buf))) {
2959 if (strlencmp(cmd, "on") == 0) {
2960 cmos_cmd = 0x0c;
2961 lght_cmd = 1;
2962 } else if (strlencmp(cmd, "off") == 0) {
2963 cmos_cmd = 0x0d;
2964 lght_cmd = 0;
2965 } else
2966 return -EINVAL;
2968 success = cmos_handle ?
2969 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2970 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2971 if (!success)
2972 return -EIO;
2975 return 0;
2978 static struct ibm_struct light_driver_data = {
2979 .name = "light",
2980 .read = light_read,
2981 .write = light_write,
2984 /*************************************************************************
2985 * Dock subdriver
2988 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2990 static void dock_notify(struct ibm_struct *ibm, u32 event);
2991 static int dock_read(char *p);
2992 static int dock_write(char *buf);
2994 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2995 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2996 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2997 "\\_SB.PCI.ISA.SLCE", /* 570 */
2998 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3000 /* don't list other alternatives as we install a notify handler on the 570 */
3001 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3003 static const struct acpi_device_id ibm_pci_device_ids[] = {
3004 {PCI_ROOT_HID_STRING, 0},
3005 {"", 0},
3008 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3010 .notify = dock_notify,
3011 .handle = &dock_handle,
3012 .type = ACPI_SYSTEM_NOTIFY,
3015 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3016 * We just use it to get notifications of dock hotplug
3017 * in very old thinkpads */
3018 .hid = ibm_pci_device_ids,
3019 .notify = dock_notify,
3020 .handle = &pci_handle,
3021 .type = ACPI_SYSTEM_NOTIFY,
3025 static struct ibm_struct dock_driver_data[2] = {
3027 .name = "dock",
3028 .read = dock_read,
3029 .write = dock_write,
3030 .acpi = &ibm_dock_acpidriver[0],
3033 .name = "dock",
3034 .acpi = &ibm_dock_acpidriver[1],
3038 #define dock_docked() (_sta(dock_handle) & 1)
3040 static int __init dock_init(struct ibm_init_struct *iibm)
3042 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3044 TPACPI_ACPIHANDLE_INIT(dock);
3046 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3047 str_supported(dock_handle != NULL));
3049 return (dock_handle)? 0 : 1;
3052 static int __init dock_init2(struct ibm_init_struct *iibm)
3054 int dock2_needed;
3056 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3058 if (dock_driver_data[0].flags.acpi_driver_registered &&
3059 dock_driver_data[0].flags.acpi_notify_installed) {
3060 TPACPI_ACPIHANDLE_INIT(pci);
3061 dock2_needed = (pci_handle != NULL);
3062 vdbg_printk(TPACPI_DBG_INIT,
3063 "dock PCI handler for the TP 570 is %s\n",
3064 str_supported(dock2_needed));
3065 } else {
3066 vdbg_printk(TPACPI_DBG_INIT,
3067 "dock subdriver part 2 not required\n");
3068 dock2_needed = 0;
3071 return (dock2_needed)? 0 : 1;
3074 static void dock_notify(struct ibm_struct *ibm, u32 event)
3076 int docked = dock_docked();
3077 int pci = ibm->acpi->hid && ibm->acpi->device &&
3078 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3079 int data;
3081 if (event == 1 && !pci) /* 570 */
3082 data = 1; /* button */
3083 else if (event == 1 && pci) /* 570 */
3084 data = 3; /* dock */
3085 else if (event == 3 && docked)
3086 data = 1; /* button */
3087 else if (event == 3 && !docked)
3088 data = 2; /* undock */
3089 else if (event == 0 && docked)
3090 data = 3; /* dock */
3091 else {
3092 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3093 event, _sta(dock_handle));
3094 data = 0; /* unknown */
3096 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3097 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3098 ibm->acpi->device->dev.bus_id,
3099 event, data);
3102 static int dock_read(char *p)
3104 int len = 0;
3105 int docked = dock_docked();
3107 if (!dock_handle)
3108 len += sprintf(p + len, "status:\t\tnot supported\n");
3109 else if (!docked)
3110 len += sprintf(p + len, "status:\t\tundocked\n");
3111 else {
3112 len += sprintf(p + len, "status:\t\tdocked\n");
3113 len += sprintf(p + len, "commands:\tdock, undock\n");
3116 return len;
3119 static int dock_write(char *buf)
3121 char *cmd;
3123 if (!dock_docked())
3124 return -ENODEV;
3126 while ((cmd = next_cmd(&buf))) {
3127 if (strlencmp(cmd, "undock") == 0) {
3128 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3129 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3130 return -EIO;
3131 } else if (strlencmp(cmd, "dock") == 0) {
3132 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3133 return -EIO;
3134 } else
3135 return -EINVAL;
3138 return 0;
3141 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3143 /*************************************************************************
3144 * Bay subdriver
3147 #ifdef CONFIG_THINKPAD_ACPI_BAY
3149 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3150 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3151 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3152 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3153 ); /* A21e, R30, R31 */
3154 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3155 "_EJ0", /* all others */
3156 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3157 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3158 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3159 ); /* all others */
3160 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3161 "_EJ0", /* 770x */
3162 ); /* all others */
3164 static int __init bay_init(struct ibm_init_struct *iibm)
3166 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3168 TPACPI_ACPIHANDLE_INIT(bay);
3169 if (bay_handle)
3170 TPACPI_ACPIHANDLE_INIT(bay_ej);
3171 TPACPI_ACPIHANDLE_INIT(bay2);
3172 if (bay2_handle)
3173 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3175 tp_features.bay_status = bay_handle &&
3176 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3177 tp_features.bay_status2 = bay2_handle &&
3178 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3180 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3181 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3182 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3183 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3185 vdbg_printk(TPACPI_DBG_INIT,
3186 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3187 str_supported(tp_features.bay_status),
3188 str_supported(tp_features.bay_eject),
3189 str_supported(tp_features.bay_status2),
3190 str_supported(tp_features.bay_eject2));
3192 return (tp_features.bay_status || tp_features.bay_eject ||
3193 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3196 static void bay_notify(struct ibm_struct *ibm, u32 event)
3198 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3199 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3200 ibm->acpi->device->dev.bus_id,
3201 event, 0);
3204 #define bay_occupied(b) (_sta(b##_handle) & 1)
3206 static int bay_read(char *p)
3208 int len = 0;
3209 int occupied = bay_occupied(bay);
3210 int occupied2 = bay_occupied(bay2);
3211 int eject, eject2;
3213 len += sprintf(p + len, "status:\t\t%s\n",
3214 tp_features.bay_status ?
3215 (occupied ? "occupied" : "unoccupied") :
3216 "not supported");
3217 if (tp_features.bay_status2)
3218 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3219 "occupied" : "unoccupied");
3221 eject = tp_features.bay_eject && occupied;
3222 eject2 = tp_features.bay_eject2 && occupied2;
3224 if (eject && eject2)
3225 len += sprintf(p + len, "commands:\teject, eject2\n");
3226 else if (eject)
3227 len += sprintf(p + len, "commands:\teject\n");
3228 else if (eject2)
3229 len += sprintf(p + len, "commands:\teject2\n");
3231 return len;
3234 static int bay_write(char *buf)
3236 char *cmd;
3238 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3239 return -ENODEV;
3241 while ((cmd = next_cmd(&buf))) {
3242 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3243 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3244 return -EIO;
3245 } else if (tp_features.bay_eject2 &&
3246 strlencmp(cmd, "eject2") == 0) {
3247 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3248 return -EIO;
3249 } else
3250 return -EINVAL;
3253 return 0;
3256 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3257 .notify = bay_notify,
3258 .handle = &bay_handle,
3259 .type = ACPI_SYSTEM_NOTIFY,
3262 static struct ibm_struct bay_driver_data = {
3263 .name = "bay",
3264 .read = bay_read,
3265 .write = bay_write,
3266 .acpi = &ibm_bay_acpidriver,
3269 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3271 /*************************************************************************
3272 * CMOS subdriver
3275 /* sysfs cmos_command -------------------------------------------------- */
3276 static ssize_t cmos_command_store(struct device *dev,
3277 struct device_attribute *attr,
3278 const char *buf, size_t count)
3280 unsigned long cmos_cmd;
3281 int res;
3283 if (parse_strtoul(buf, 21, &cmos_cmd))
3284 return -EINVAL;
3286 res = issue_thinkpad_cmos_command(cmos_cmd);
3287 return (res)? res : count;
3290 static struct device_attribute dev_attr_cmos_command =
3291 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3293 /* --------------------------------------------------------------------- */
3295 static int __init cmos_init(struct ibm_init_struct *iibm)
3297 int res;
3299 vdbg_printk(TPACPI_DBG_INIT,
3300 "initializing cmos commands subdriver\n");
3302 TPACPI_ACPIHANDLE_INIT(cmos);
3304 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3305 str_supported(cmos_handle != NULL));
3307 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3308 if (res)
3309 return res;
3311 return (cmos_handle)? 0 : 1;
3314 static void cmos_exit(void)
3316 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3319 static int cmos_read(char *p)
3321 int len = 0;
3323 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3324 R30, R31, T20-22, X20-21 */
3325 if (!cmos_handle)
3326 len += sprintf(p + len, "status:\t\tnot supported\n");
3327 else {
3328 len += sprintf(p + len, "status:\t\tsupported\n");
3329 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3332 return len;
3335 static int cmos_write(char *buf)
3337 char *cmd;
3338 int cmos_cmd, res;
3340 while ((cmd = next_cmd(&buf))) {
3341 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3342 cmos_cmd >= 0 && cmos_cmd <= 21) {
3343 /* cmos_cmd set */
3344 } else
3345 return -EINVAL;
3347 res = issue_thinkpad_cmos_command(cmos_cmd);
3348 if (res)
3349 return res;
3352 return 0;
3355 static struct ibm_struct cmos_driver_data = {
3356 .name = "cmos",
3357 .read = cmos_read,
3358 .write = cmos_write,
3359 .exit = cmos_exit,
3362 /*************************************************************************
3363 * LED subdriver
3366 enum led_access_mode {
3367 TPACPI_LED_NONE = 0,
3368 TPACPI_LED_570, /* 570 */
3369 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3370 TPACPI_LED_NEW, /* all others */
3373 enum { /* For TPACPI_LED_OLD */
3374 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3375 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3376 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3379 static enum led_access_mode led_supported;
3381 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3382 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3383 /* T20-22, X20-21 */
3384 "LED", /* all others */
3385 ); /* R30, R31 */
3387 static int __init led_init(struct ibm_init_struct *iibm)
3389 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3391 TPACPI_ACPIHANDLE_INIT(led);
3393 if (!led_handle)
3394 /* led not supported on R30, R31 */
3395 led_supported = TPACPI_LED_NONE;
3396 else if (strlencmp(led_path, "SLED") == 0)
3397 /* 570 */
3398 led_supported = TPACPI_LED_570;
3399 else if (strlencmp(led_path, "SYSL") == 0)
3400 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3401 led_supported = TPACPI_LED_OLD;
3402 else
3403 /* all others */
3404 led_supported = TPACPI_LED_NEW;
3406 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3407 str_supported(led_supported), led_supported);
3409 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3412 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3414 static int led_read(char *p)
3416 int len = 0;
3418 if (!led_supported) {
3419 len += sprintf(p + len, "status:\t\tnot supported\n");
3420 return len;
3422 len += sprintf(p + len, "status:\t\tsupported\n");
3424 if (led_supported == TPACPI_LED_570) {
3425 /* 570 */
3426 int i, status;
3427 for (i = 0; i < 8; i++) {
3428 if (!acpi_evalf(ec_handle,
3429 &status, "GLED", "dd", 1 << i))
3430 return -EIO;
3431 len += sprintf(p + len, "%d:\t\t%s\n",
3432 i, led_status(status));
3436 len += sprintf(p + len, "commands:\t"
3437 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3439 return len;
3442 /* off, on, blink */
3443 static const int led_sled_arg1[] = { 0, 1, 3 };
3444 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3445 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3446 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3448 static int led_write(char *buf)
3450 char *cmd;
3451 int led, ind, ret;
3453 if (!led_supported)
3454 return -ENODEV;
3456 while ((cmd = next_cmd(&buf))) {
3457 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3458 return -EINVAL;
3460 if (strstr(cmd, "off")) {
3461 ind = 0;
3462 } else if (strstr(cmd, "on")) {
3463 ind = 1;
3464 } else if (strstr(cmd, "blink")) {
3465 ind = 2;
3466 } else
3467 return -EINVAL;
3469 if (led_supported == TPACPI_LED_570) {
3470 /* 570 */
3471 led = 1 << led;
3472 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3473 led, led_sled_arg1[ind]))
3474 return -EIO;
3475 } else if (led_supported == TPACPI_LED_OLD) {
3476 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3477 led = 1 << led;
3478 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3479 if (ret >= 0)
3480 ret = ec_write(TPACPI_LED_EC_HLBL,
3481 led * led_exp_hlbl[ind]);
3482 if (ret >= 0)
3483 ret = ec_write(TPACPI_LED_EC_HLCL,
3484 led * led_exp_hlcl[ind]);
3485 if (ret < 0)
3486 return ret;
3487 } else {
3488 /* all others */
3489 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3490 led, led_led_arg1[ind]))
3491 return -EIO;
3495 return 0;
3498 static struct ibm_struct led_driver_data = {
3499 .name = "led",
3500 .read = led_read,
3501 .write = led_write,
3504 /*************************************************************************
3505 * Beep subdriver
3508 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3510 static int __init beep_init(struct ibm_init_struct *iibm)
3512 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3514 TPACPI_ACPIHANDLE_INIT(beep);
3516 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3517 str_supported(beep_handle != NULL));
3519 return (beep_handle)? 0 : 1;
3522 static int beep_read(char *p)
3524 int len = 0;
3526 if (!beep_handle)
3527 len += sprintf(p + len, "status:\t\tnot supported\n");
3528 else {
3529 len += sprintf(p + len, "status:\t\tsupported\n");
3530 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3533 return len;
3536 static int beep_write(char *buf)
3538 char *cmd;
3539 int beep_cmd;
3541 if (!beep_handle)
3542 return -ENODEV;
3544 while ((cmd = next_cmd(&buf))) {
3545 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3546 beep_cmd >= 0 && beep_cmd <= 17) {
3547 /* beep_cmd set */
3548 } else
3549 return -EINVAL;
3550 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3551 return -EIO;
3554 return 0;
3557 static struct ibm_struct beep_driver_data = {
3558 .name = "beep",
3559 .read = beep_read,
3560 .write = beep_write,
3563 /*************************************************************************
3564 * Thermal subdriver
3567 enum thermal_access_mode {
3568 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3569 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3570 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3571 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3572 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3575 enum { /* TPACPI_THERMAL_TPEC_* */
3576 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3577 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3578 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3581 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3582 struct ibm_thermal_sensors_struct {
3583 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3586 static enum thermal_access_mode thermal_read_mode;
3588 /* idx is zero-based */
3589 static int thermal_get_sensor(int idx, s32 *value)
3591 int t;
3592 s8 tmp;
3593 char tmpi[5];
3595 t = TP_EC_THERMAL_TMP0;
3597 switch (thermal_read_mode) {
3598 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3599 case TPACPI_THERMAL_TPEC_16:
3600 if (idx >= 8 && idx <= 15) {
3601 t = TP_EC_THERMAL_TMP8;
3602 idx -= 8;
3604 /* fallthrough */
3605 #endif
3606 case TPACPI_THERMAL_TPEC_8:
3607 if (idx <= 7) {
3608 if (!acpi_ec_read(t + idx, &tmp))
3609 return -EIO;
3610 *value = tmp * 1000;
3611 return 0;
3613 break;
3615 case TPACPI_THERMAL_ACPI_UPDT:
3616 if (idx <= 7) {
3617 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3618 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3619 return -EIO;
3620 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3621 return -EIO;
3622 *value = (t - 2732) * 100;
3623 return 0;
3625 break;
3627 case TPACPI_THERMAL_ACPI_TMP07:
3628 if (idx <= 7) {
3629 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3630 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3631 return -EIO;
3632 if (t > 127 || t < -127)
3633 t = TP_EC_THERMAL_TMP_NA;
3634 *value = t * 1000;
3635 return 0;
3637 break;
3639 case TPACPI_THERMAL_NONE:
3640 default:
3641 return -ENOSYS;
3644 return -EINVAL;
3647 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3649 int res, i;
3650 int n;
3652 n = 8;
3653 i = 0;
3655 if (!s)
3656 return -EINVAL;
3658 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3659 n = 16;
3661 for (i = 0 ; i < n; i++) {
3662 res = thermal_get_sensor(i, &s->temp[i]);
3663 if (res)
3664 return res;
3667 return n;
3670 /* sysfs temp##_input -------------------------------------------------- */
3672 static ssize_t thermal_temp_input_show(struct device *dev,
3673 struct device_attribute *attr,
3674 char *buf)
3676 struct sensor_device_attribute *sensor_attr =
3677 to_sensor_dev_attr(attr);
3678 int idx = sensor_attr->index;
3679 s32 value;
3680 int res;
3682 res = thermal_get_sensor(idx, &value);
3683 if (res)
3684 return res;
3685 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3686 return -ENXIO;
3688 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3691 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3692 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3693 thermal_temp_input_show, NULL, _idxB)
3695 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3696 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3697 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3698 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3699 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3700 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3701 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3702 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3703 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3704 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3705 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3706 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3707 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3708 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3709 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3710 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3711 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3714 #define THERMAL_ATTRS(X) \
3715 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3717 static struct attribute *thermal_temp_input_attr[] = {
3718 THERMAL_ATTRS(8),
3719 THERMAL_ATTRS(9),
3720 THERMAL_ATTRS(10),
3721 THERMAL_ATTRS(11),
3722 THERMAL_ATTRS(12),
3723 THERMAL_ATTRS(13),
3724 THERMAL_ATTRS(14),
3725 THERMAL_ATTRS(15),
3726 THERMAL_ATTRS(0),
3727 THERMAL_ATTRS(1),
3728 THERMAL_ATTRS(2),
3729 THERMAL_ATTRS(3),
3730 THERMAL_ATTRS(4),
3731 THERMAL_ATTRS(5),
3732 THERMAL_ATTRS(6),
3733 THERMAL_ATTRS(7),
3734 NULL
3737 static const struct attribute_group thermal_temp_input16_group = {
3738 .attrs = thermal_temp_input_attr
3741 static const struct attribute_group thermal_temp_input8_group = {
3742 .attrs = &thermal_temp_input_attr[8]
3745 #undef THERMAL_SENSOR_ATTR_TEMP
3746 #undef THERMAL_ATTRS
3748 /* --------------------------------------------------------------------- */
3750 static int __init thermal_init(struct ibm_init_struct *iibm)
3752 u8 t, ta1, ta2;
3753 int i;
3754 int acpi_tmp7;
3755 int res;
3757 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3759 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3761 if (thinkpad_id.ec_model) {
3763 * Direct EC access mode: sensors at registers
3764 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3765 * non-implemented, thermal sensors return 0x80 when
3766 * not available
3769 ta1 = ta2 = 0;
3770 for (i = 0; i < 8; i++) {
3771 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3772 ta1 |= t;
3773 } else {
3774 ta1 = 0;
3775 break;
3777 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3778 ta2 |= t;
3779 } else {
3780 ta1 = 0;
3781 break;
3784 if (ta1 == 0) {
3785 /* This is sheer paranoia, but we handle it anyway */
3786 if (acpi_tmp7) {
3787 printk(TPACPI_ERR
3788 "ThinkPad ACPI EC access misbehaving, "
3789 "falling back to ACPI TMPx access "
3790 "mode\n");
3791 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3792 } else {
3793 printk(TPACPI_ERR
3794 "ThinkPad ACPI EC access misbehaving, "
3795 "disabling thermal sensors access\n");
3796 thermal_read_mode = TPACPI_THERMAL_NONE;
3798 } else {
3799 thermal_read_mode =
3800 (ta2 != 0) ?
3801 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3803 } else if (acpi_tmp7) {
3804 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3805 /* 600e/x, 770e, 770x */
3806 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3807 } else {
3808 /* Standard ACPI TMPx access, max 8 sensors */
3809 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3811 } else {
3812 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3813 thermal_read_mode = TPACPI_THERMAL_NONE;
3816 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3817 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3818 thermal_read_mode);
3820 switch (thermal_read_mode) {
3821 case TPACPI_THERMAL_TPEC_16:
3822 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3823 &thermal_temp_input16_group);
3824 if (res)
3825 return res;
3826 break;
3827 case TPACPI_THERMAL_TPEC_8:
3828 case TPACPI_THERMAL_ACPI_TMP07:
3829 case TPACPI_THERMAL_ACPI_UPDT:
3830 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3831 &thermal_temp_input8_group);
3832 if (res)
3833 return res;
3834 break;
3835 case TPACPI_THERMAL_NONE:
3836 default:
3837 return 1;
3840 return 0;
3843 static void thermal_exit(void)
3845 switch (thermal_read_mode) {
3846 case TPACPI_THERMAL_TPEC_16:
3847 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3848 &thermal_temp_input16_group);
3849 break;
3850 case TPACPI_THERMAL_TPEC_8:
3851 case TPACPI_THERMAL_ACPI_TMP07:
3852 case TPACPI_THERMAL_ACPI_UPDT:
3853 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3854 &thermal_temp_input16_group);
3855 break;
3856 case TPACPI_THERMAL_NONE:
3857 default:
3858 break;
3862 static int thermal_read(char *p)
3864 int len = 0;
3865 int n, i;
3866 struct ibm_thermal_sensors_struct t;
3868 n = thermal_get_sensors(&t);
3869 if (unlikely(n < 0))
3870 return n;
3872 len += sprintf(p + len, "temperatures:\t");
3874 if (n > 0) {
3875 for (i = 0; i < (n - 1); i++)
3876 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3877 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3878 } else
3879 len += sprintf(p + len, "not supported\n");
3881 return len;
3884 static struct ibm_struct thermal_driver_data = {
3885 .name = "thermal",
3886 .read = thermal_read,
3887 .exit = thermal_exit,
3890 /*************************************************************************
3891 * EC Dump subdriver
3894 static u8 ecdump_regs[256];
3896 static int ecdump_read(char *p)
3898 int len = 0;
3899 int i, j;
3900 u8 v;
3902 len += sprintf(p + len, "EC "
3903 " +00 +01 +02 +03 +04 +05 +06 +07"
3904 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3905 for (i = 0; i < 256; i += 16) {
3906 len += sprintf(p + len, "EC 0x%02x:", i);
3907 for (j = 0; j < 16; j++) {
3908 if (!acpi_ec_read(i + j, &v))
3909 break;
3910 if (v != ecdump_regs[i + j])
3911 len += sprintf(p + len, " *%02x", v);
3912 else
3913 len += sprintf(p + len, " %02x", v);
3914 ecdump_regs[i + j] = v;
3916 len += sprintf(p + len, "\n");
3917 if (j != 16)
3918 break;
3921 /* These are way too dangerous to advertise openly... */
3922 #if 0
3923 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3924 " (<offset> is 00-ff, <value> is 00-ff)\n");
3925 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3926 " (<offset> is 00-ff, <value> is 0-255)\n");
3927 #endif
3928 return len;
3931 static int ecdump_write(char *buf)
3933 char *cmd;
3934 int i, v;
3936 while ((cmd = next_cmd(&buf))) {
3937 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3938 /* i and v set */
3939 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3940 /* i and v set */
3941 } else
3942 return -EINVAL;
3943 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3944 if (!acpi_ec_write(i, v))
3945 return -EIO;
3946 } else
3947 return -EINVAL;
3950 return 0;
3953 static struct ibm_struct ecdump_driver_data = {
3954 .name = "ecdump",
3955 .read = ecdump_read,
3956 .write = ecdump_write,
3957 .flags.experimental = 1,
3960 /*************************************************************************
3961 * Backlight/brightness subdriver
3964 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3966 static struct backlight_device *ibm_backlight_device;
3967 static int brightness_offset = 0x31;
3968 static int brightness_mode;
3969 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3971 static struct mutex brightness_mutex;
3974 * ThinkPads can read brightness from two places: EC 0x31, or
3975 * CMOS NVRAM byte 0x5E, bits 0-3.
3977 static int brightness_get(struct backlight_device *bd)
3979 u8 lec = 0, lcmos = 0, level = 0;
3981 if (brightness_mode & 1) {
3982 if (!acpi_ec_read(brightness_offset, &lec))
3983 return -EIO;
3984 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3985 level = lec;
3987 if (brightness_mode & 2) {
3988 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3989 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3990 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3991 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3992 level = lcmos;
3995 if (brightness_mode == 3 && lec != lcmos) {
3996 printk(TPACPI_ERR
3997 "CMOS NVRAM (%u) and EC (%u) do not agree "
3998 "on display brightness level\n",
3999 (unsigned int) lcmos,
4000 (unsigned int) lec);
4001 return -EIO;
4004 return level;
4007 /* May return EINTR which can always be mapped to ERESTARTSYS */
4008 static int brightness_set(int value)
4010 int cmos_cmd, inc, i, res;
4011 int current_value;
4013 if (value > ((tp_features.bright_16levels)? 15 : 7))
4014 return -EINVAL;
4016 res = mutex_lock_interruptible(&brightness_mutex);
4017 if (res < 0)
4018 return res;
4020 current_value = brightness_get(NULL);
4021 if (current_value < 0) {
4022 res = current_value;
4023 goto errout;
4026 cmos_cmd = value > current_value ?
4027 TP_CMOS_BRIGHTNESS_UP :
4028 TP_CMOS_BRIGHTNESS_DOWN;
4029 inc = (value > current_value)? 1 : -1;
4031 res = 0;
4032 for (i = current_value; i != value; i += inc) {
4033 if ((brightness_mode & 2) &&
4034 issue_thinkpad_cmos_command(cmos_cmd)) {
4035 res = -EIO;
4036 goto errout;
4038 if ((brightness_mode & 1) &&
4039 !acpi_ec_write(brightness_offset, i + inc)) {
4040 res = -EIO;
4041 goto errout;;
4045 errout:
4046 mutex_unlock(&brightness_mutex);
4047 return res;
4050 /* sysfs backlight class ----------------------------------------------- */
4052 static int brightness_update_status(struct backlight_device *bd)
4054 /* it is the backlight class's job (caller) to handle
4055 * EINTR and other errors properly */
4056 return brightness_set(
4057 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4058 bd->props.power == FB_BLANK_UNBLANK) ?
4059 bd->props.brightness : 0);
4062 static struct backlight_ops ibm_backlight_data = {
4063 .get_brightness = brightness_get,
4064 .update_status = brightness_update_status,
4067 /* --------------------------------------------------------------------- */
4069 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4071 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4072 union acpi_object *obj;
4073 int rc;
4075 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4076 obj = (union acpi_object *)buffer.pointer;
4077 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4078 printk(TPACPI_ERR "Unknown BCLL data, "
4079 "please report this to %s\n", TPACPI_MAIL);
4080 rc = 0;
4081 } else {
4082 rc = obj->package.count;
4084 } else {
4085 return 0;
4088 kfree(buffer.pointer);
4089 return rc;
4092 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4093 void *context, void **rv)
4095 char name[ACPI_PATH_SEGMENT_LENGTH];
4096 struct acpi_buffer buffer = { sizeof(name), &name };
4098 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4099 !strncmp("BCLL", name, sizeof(name) - 1)) {
4100 if (tpacpi_query_bcll_levels(handle) == 16) {
4101 *rv = handle;
4102 return AE_CTRL_TERMINATE;
4103 } else {
4104 return AE_OK;
4106 } else {
4107 return AE_OK;
4111 static int __init brightness_check_levels(void)
4113 int status;
4114 void *found_node = NULL;
4116 if (!vid_handle) {
4117 TPACPI_ACPIHANDLE_INIT(vid);
4119 if (!vid_handle)
4120 return 0;
4122 /* Search for a BCLL package with 16 levels */
4123 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4124 brightness_find_bcll, NULL,
4125 &found_node);
4127 return (ACPI_SUCCESS(status) && found_node != NULL);
4130 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4131 void *context, void **rv)
4133 char name[ACPI_PATH_SEGMENT_LENGTH];
4134 struct acpi_buffer buffer = { sizeof(name), &name };
4136 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4137 !strncmp("_BCL", name, sizeof(name) - 1)) {
4138 *rv = handle;
4139 return AE_CTRL_TERMINATE;
4140 } else {
4141 return AE_OK;
4145 static int __init brightness_check_std_acpi_support(void)
4147 int status;
4148 void *found_node = NULL;
4150 if (!vid_handle) {
4151 TPACPI_ACPIHANDLE_INIT(vid);
4153 if (!vid_handle)
4154 return 0;
4156 /* Search for a _BCL method, but don't execute it */
4157 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4158 brightness_find_bcl, NULL, &found_node);
4160 return (ACPI_SUCCESS(status) && found_node != NULL);
4163 static int __init brightness_init(struct ibm_init_struct *iibm)
4165 int b;
4167 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4169 mutex_init(&brightness_mutex);
4171 if (!brightness_enable) {
4172 dbg_printk(TPACPI_DBG_INIT,
4173 "brightness support disabled by module parameter\n");
4174 return 1;
4175 } else if (brightness_enable > 1) {
4176 if (brightness_check_std_acpi_support()) {
4177 printk(TPACPI_NOTICE
4178 "standard ACPI backlight interface "
4179 "available, not loading native one...\n");
4180 return 1;
4184 if (!brightness_mode) {
4185 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4186 brightness_mode = 2;
4187 else
4188 brightness_mode = 3;
4190 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4191 brightness_mode);
4194 if (brightness_mode > 3)
4195 return -EINVAL;
4197 tp_features.bright_16levels =
4198 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4199 brightness_check_levels();
4201 b = brightness_get(NULL);
4202 if (b < 0)
4203 return 1;
4205 if (tp_features.bright_16levels)
4206 printk(TPACPI_INFO
4207 "detected a 16-level brightness capable ThinkPad\n");
4209 ibm_backlight_device = backlight_device_register(
4210 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4211 &ibm_backlight_data);
4212 if (IS_ERR(ibm_backlight_device)) {
4213 printk(TPACPI_ERR "Could not register backlight device\n");
4214 return PTR_ERR(ibm_backlight_device);
4216 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4218 ibm_backlight_device->props.max_brightness =
4219 (tp_features.bright_16levels)? 15 : 7;
4220 ibm_backlight_device->props.brightness = b;
4221 backlight_update_status(ibm_backlight_device);
4223 return 0;
4226 static void brightness_exit(void)
4228 if (ibm_backlight_device) {
4229 vdbg_printk(TPACPI_DBG_EXIT,
4230 "calling backlight_device_unregister()\n");
4231 backlight_device_unregister(ibm_backlight_device);
4232 ibm_backlight_device = NULL;
4236 static int brightness_read(char *p)
4238 int len = 0;
4239 int level;
4241 level = brightness_get(NULL);
4242 if (level < 0) {
4243 len += sprintf(p + len, "level:\t\tunreadable\n");
4244 } else {
4245 len += sprintf(p + len, "level:\t\t%d\n", level);
4246 len += sprintf(p + len, "commands:\tup, down\n");
4247 len += sprintf(p + len, "commands:\tlevel <level>"
4248 " (<level> is 0-%d)\n",
4249 (tp_features.bright_16levels) ? 15 : 7);
4252 return len;
4255 static int brightness_write(char *buf)
4257 int level;
4258 int rc;
4259 char *cmd;
4260 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4262 level = brightness_get(NULL);
4263 if (level < 0)
4264 return level;
4266 while ((cmd = next_cmd(&buf))) {
4267 if (strlencmp(cmd, "up") == 0) {
4268 if (level < max_level)
4269 level++;
4270 } else if (strlencmp(cmd, "down") == 0) {
4271 if (level > 0)
4272 level--;
4273 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4274 level >= 0 && level <= max_level) {
4275 /* new level set */
4276 } else
4277 return -EINVAL;
4281 * Now we know what the final level should be, so we try to set it.
4282 * Doing it this way makes the syscall restartable in case of EINTR
4284 rc = brightness_set(level);
4285 return (rc == -EINTR)? ERESTARTSYS : rc;
4288 static struct ibm_struct brightness_driver_data = {
4289 .name = "brightness",
4290 .read = brightness_read,
4291 .write = brightness_write,
4292 .exit = brightness_exit,
4295 /*************************************************************************
4296 * Volume subdriver
4299 static int volume_offset = 0x30;
4301 static int volume_read(char *p)
4303 int len = 0;
4304 u8 level;
4306 if (!acpi_ec_read(volume_offset, &level)) {
4307 len += sprintf(p + len, "level:\t\tunreadable\n");
4308 } else {
4309 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4310 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4311 len += sprintf(p + len, "commands:\tup, down, mute\n");
4312 len += sprintf(p + len, "commands:\tlevel <level>"
4313 " (<level> is 0-15)\n");
4316 return len;
4319 static int volume_write(char *buf)
4321 int cmos_cmd, inc, i;
4322 u8 level, mute;
4323 int new_level, new_mute;
4324 char *cmd;
4326 while ((cmd = next_cmd(&buf))) {
4327 if (!acpi_ec_read(volume_offset, &level))
4328 return -EIO;
4329 new_mute = mute = level & 0x40;
4330 new_level = level = level & 0xf;
4332 if (strlencmp(cmd, "up") == 0) {
4333 if (mute)
4334 new_mute = 0;
4335 else
4336 new_level = level == 15 ? 15 : level + 1;
4337 } else if (strlencmp(cmd, "down") == 0) {
4338 if (mute)
4339 new_mute = 0;
4340 else
4341 new_level = level == 0 ? 0 : level - 1;
4342 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4343 new_level >= 0 && new_level <= 15) {
4344 /* new_level set */
4345 } else if (strlencmp(cmd, "mute") == 0) {
4346 new_mute = 0x40;
4347 } else
4348 return -EINVAL;
4350 if (new_level != level) { /* mute doesn't change */
4351 cmos_cmd = (new_level > level) ?
4352 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4353 inc = new_level > level ? 1 : -1;
4355 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4356 !acpi_ec_write(volume_offset, level)))
4357 return -EIO;
4359 for (i = level; i != new_level; i += inc)
4360 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4361 !acpi_ec_write(volume_offset, i + inc))
4362 return -EIO;
4364 if (mute &&
4365 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4366 !acpi_ec_write(volume_offset, new_level + mute))) {
4367 return -EIO;
4371 if (new_mute != mute) { /* level doesn't change */
4372 cmos_cmd = (new_mute) ?
4373 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4375 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4376 !acpi_ec_write(volume_offset, level + new_mute))
4377 return -EIO;
4381 return 0;
4384 static struct ibm_struct volume_driver_data = {
4385 .name = "volume",
4386 .read = volume_read,
4387 .write = volume_write,
4390 /*************************************************************************
4391 * Fan subdriver
4395 * FAN ACCESS MODES
4397 * TPACPI_FAN_RD_ACPI_GFAN:
4398 * ACPI GFAN method: returns fan level
4400 * see TPACPI_FAN_WR_ACPI_SFAN
4401 * EC 0x2f (HFSP) not available if GFAN exists
4403 * TPACPI_FAN_WR_ACPI_SFAN:
4404 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4406 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4407 * it for writing.
4409 * TPACPI_FAN_WR_TPEC:
4410 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4411 * Supported on almost all ThinkPads
4413 * Fan speed changes of any sort (including those caused by the
4414 * disengaged mode) are usually done slowly by the firmware as the
4415 * maximum ammount of fan duty cycle change per second seems to be
4416 * limited.
4418 * Reading is not available if GFAN exists.
4419 * Writing is not available if SFAN exists.
4421 * Bits
4422 * 7 automatic mode engaged;
4423 * (default operation mode of the ThinkPad)
4424 * fan level is ignored in this mode.
4425 * 6 full speed mode (takes precedence over bit 7);
4426 * not available on all thinkpads. May disable
4427 * the tachometer while the fan controller ramps up
4428 * the speed (which can take up to a few *minutes*).
4429 * Speeds up fan to 100% duty-cycle, which is far above
4430 * the standard RPM levels. It is not impossible that
4431 * it could cause hardware damage.
4432 * 5-3 unused in some models. Extra bits for fan level
4433 * in others, but still useless as all values above
4434 * 7 map to the same speed as level 7 in these models.
4435 * 2-0 fan level (0..7 usually)
4436 * 0x00 = stop
4437 * 0x07 = max (set when temperatures critical)
4438 * Some ThinkPads may have other levels, see
4439 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4441 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4442 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4443 * does so, its initial value is meaningless (0x07).
4445 * For firmware bugs, refer to:
4446 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4448 * ----
4450 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4451 * Main fan tachometer reading (in RPM)
4453 * This register is present on all ThinkPads with a new-style EC, and
4454 * it is known not to be present on the A21m/e, and T22, as there is
4455 * something else in offset 0x84 according to the ACPI DSDT. Other
4456 * ThinkPads from this same time period (and earlier) probably lack the
4457 * tachometer as well.
4459 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4460 * was never fixed by IBM to report the EC firmware version string
4461 * probably support the tachometer (like the early X models), so
4462 * detecting it is quite hard. We need more data to know for sure.
4464 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4465 * might result.
4467 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4468 * mode.
4470 * For firmware bugs, refer to:
4471 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4473 * TPACPI_FAN_WR_ACPI_FANS:
4474 * ThinkPad X31, X40, X41. Not available in the X60.
4476 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4477 * high speed. ACPI DSDT seems to map these three speeds to levels
4478 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4479 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4481 * The speeds are stored on handles
4482 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4484 * There are three default speed sets, acessible as handles:
4485 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4487 * ACPI DSDT switches which set is in use depending on various
4488 * factors.
4490 * TPACPI_FAN_WR_TPEC is also available and should be used to
4491 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4492 * but the ACPI tables just mention level 7.
4495 enum { /* Fan control constants */
4496 fan_status_offset = 0x2f, /* EC register 0x2f */
4497 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4498 * 0x84 must be read before 0x85 */
4500 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4501 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4503 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4506 enum fan_status_access_mode {
4507 TPACPI_FAN_NONE = 0, /* No fan status or control */
4508 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4509 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4512 enum fan_control_access_mode {
4513 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4514 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4515 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4516 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4519 enum fan_control_commands {
4520 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4521 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4522 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4523 * and also watchdog cmd */
4526 static int fan_control_allowed;
4528 static enum fan_status_access_mode fan_status_access_mode;
4529 static enum fan_control_access_mode fan_control_access_mode;
4530 static enum fan_control_commands fan_control_commands;
4532 static u8 fan_control_initial_status;
4533 static u8 fan_control_desired_level;
4534 static int fan_watchdog_maxinterval;
4536 static struct mutex fan_mutex;
4538 static void fan_watchdog_fire(struct work_struct *ignored);
4539 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4541 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4542 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4543 "\\FSPD", /* 600e/x, 770e, 770x */
4544 ); /* all others */
4545 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4546 "JFNS", /* 770x-JL */
4547 ); /* all others */
4550 * Call with fan_mutex held
4552 static void fan_update_desired_level(u8 status)
4554 if ((status &
4555 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4556 if (status > 7)
4557 fan_control_desired_level = 7;
4558 else
4559 fan_control_desired_level = status;
4563 static int fan_get_status(u8 *status)
4565 u8 s;
4567 /* TODO:
4568 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4570 switch (fan_status_access_mode) {
4571 case TPACPI_FAN_RD_ACPI_GFAN:
4572 /* 570, 600e/x, 770e, 770x */
4574 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4575 return -EIO;
4577 if (likely(status))
4578 *status = s & 0x07;
4580 break;
4582 case TPACPI_FAN_RD_TPEC:
4583 /* all except 570, 600e/x, 770e, 770x */
4584 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4585 return -EIO;
4587 if (likely(status))
4588 *status = s;
4590 break;
4592 default:
4593 return -ENXIO;
4596 return 0;
4599 static int fan_get_status_safe(u8 *status)
4601 int rc;
4602 u8 s;
4604 if (mutex_lock_interruptible(&fan_mutex))
4605 return -ERESTARTSYS;
4606 rc = fan_get_status(&s);
4607 if (!rc)
4608 fan_update_desired_level(s);
4609 mutex_unlock(&fan_mutex);
4611 if (status)
4612 *status = s;
4614 return rc;
4617 static int fan_get_speed(unsigned int *speed)
4619 u8 hi, lo;
4621 switch (fan_status_access_mode) {
4622 case TPACPI_FAN_RD_TPEC:
4623 /* all except 570, 600e/x, 770e, 770x */
4624 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4625 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4626 return -EIO;
4628 if (likely(speed))
4629 *speed = (hi << 8) | lo;
4631 break;
4633 default:
4634 return -ENXIO;
4637 return 0;
4640 static int fan_set_level(int level)
4642 if (!fan_control_allowed)
4643 return -EPERM;
4645 switch (fan_control_access_mode) {
4646 case TPACPI_FAN_WR_ACPI_SFAN:
4647 if (level >= 0 && level <= 7) {
4648 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4649 return -EIO;
4650 } else
4651 return -EINVAL;
4652 break;
4654 case TPACPI_FAN_WR_ACPI_FANS:
4655 case TPACPI_FAN_WR_TPEC:
4656 if ((level != TP_EC_FAN_AUTO) &&
4657 (level != TP_EC_FAN_FULLSPEED) &&
4658 ((level < 0) || (level > 7)))
4659 return -EINVAL;
4661 /* safety net should the EC not support AUTO
4662 * or FULLSPEED mode bits and just ignore them */
4663 if (level & TP_EC_FAN_FULLSPEED)
4664 level |= 7; /* safety min speed 7 */
4665 else if (level & TP_EC_FAN_FULLSPEED)
4666 level |= 4; /* safety min speed 4 */
4668 if (!acpi_ec_write(fan_status_offset, level))
4669 return -EIO;
4670 else
4671 tp_features.fan_ctrl_status_undef = 0;
4672 break;
4674 default:
4675 return -ENXIO;
4677 return 0;
4680 static int fan_set_level_safe(int level)
4682 int rc;
4684 if (!fan_control_allowed)
4685 return -EPERM;
4687 if (mutex_lock_interruptible(&fan_mutex))
4688 return -ERESTARTSYS;
4690 if (level == TPACPI_FAN_LAST_LEVEL)
4691 level = fan_control_desired_level;
4693 rc = fan_set_level(level);
4694 if (!rc)
4695 fan_update_desired_level(level);
4697 mutex_unlock(&fan_mutex);
4698 return rc;
4701 static int fan_set_enable(void)
4703 u8 s;
4704 int rc;
4706 if (!fan_control_allowed)
4707 return -EPERM;
4709 if (mutex_lock_interruptible(&fan_mutex))
4710 return -ERESTARTSYS;
4712 switch (fan_control_access_mode) {
4713 case TPACPI_FAN_WR_ACPI_FANS:
4714 case TPACPI_FAN_WR_TPEC:
4715 rc = fan_get_status(&s);
4716 if (rc < 0)
4717 break;
4719 /* Don't go out of emergency fan mode */
4720 if (s != 7) {
4721 s &= 0x07;
4722 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4725 if (!acpi_ec_write(fan_status_offset, s))
4726 rc = -EIO;
4727 else {
4728 tp_features.fan_ctrl_status_undef = 0;
4729 rc = 0;
4731 break;
4733 case TPACPI_FAN_WR_ACPI_SFAN:
4734 rc = fan_get_status(&s);
4735 if (rc < 0)
4736 break;
4738 s &= 0x07;
4740 /* Set fan to at least level 4 */
4741 s |= 4;
4743 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4744 rc = -EIO;
4745 else
4746 rc = 0;
4747 break;
4749 default:
4750 rc = -ENXIO;
4753 mutex_unlock(&fan_mutex);
4754 return rc;
4757 static int fan_set_disable(void)
4759 int rc;
4761 if (!fan_control_allowed)
4762 return -EPERM;
4764 if (mutex_lock_interruptible(&fan_mutex))
4765 return -ERESTARTSYS;
4767 rc = 0;
4768 switch (fan_control_access_mode) {
4769 case TPACPI_FAN_WR_ACPI_FANS:
4770 case TPACPI_FAN_WR_TPEC:
4771 if (!acpi_ec_write(fan_status_offset, 0x00))
4772 rc = -EIO;
4773 else {
4774 fan_control_desired_level = 0;
4775 tp_features.fan_ctrl_status_undef = 0;
4777 break;
4779 case TPACPI_FAN_WR_ACPI_SFAN:
4780 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4781 rc = -EIO;
4782 else
4783 fan_control_desired_level = 0;
4784 break;
4786 default:
4787 rc = -ENXIO;
4791 mutex_unlock(&fan_mutex);
4792 return rc;
4795 static int fan_set_speed(int speed)
4797 int rc;
4799 if (!fan_control_allowed)
4800 return -EPERM;
4802 if (mutex_lock_interruptible(&fan_mutex))
4803 return -ERESTARTSYS;
4805 rc = 0;
4806 switch (fan_control_access_mode) {
4807 case TPACPI_FAN_WR_ACPI_FANS:
4808 if (speed >= 0 && speed <= 65535) {
4809 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4810 speed, speed, speed))
4811 rc = -EIO;
4812 } else
4813 rc = -EINVAL;
4814 break;
4816 default:
4817 rc = -ENXIO;
4820 mutex_unlock(&fan_mutex);
4821 return rc;
4824 static void fan_watchdog_reset(void)
4826 static int fan_watchdog_active;
4828 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4829 return;
4831 if (fan_watchdog_active)
4832 cancel_delayed_work(&fan_watchdog_task);
4834 if (fan_watchdog_maxinterval > 0 &&
4835 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4836 fan_watchdog_active = 1;
4837 if (!schedule_delayed_work(&fan_watchdog_task,
4838 msecs_to_jiffies(fan_watchdog_maxinterval
4839 * 1000))) {
4840 printk(TPACPI_ERR
4841 "failed to schedule the fan watchdog, "
4842 "watchdog will not trigger\n");
4844 } else
4845 fan_watchdog_active = 0;
4848 static void fan_watchdog_fire(struct work_struct *ignored)
4850 int rc;
4852 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4853 return;
4855 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4856 rc = fan_set_enable();
4857 if (rc < 0) {
4858 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4859 "will try again later...\n", -rc);
4860 /* reschedule for later */
4861 fan_watchdog_reset();
4866 * SYSFS fan layout: hwmon compatible (device)
4868 * pwm*_enable:
4869 * 0: "disengaged" mode
4870 * 1: manual mode
4871 * 2: native EC "auto" mode (recommended, hardware default)
4873 * pwm*: set speed in manual mode, ignored otherwise.
4874 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4875 * interpolation.
4877 * fan*_input: tachometer reading, RPM
4880 * SYSFS fan layout: extensions
4882 * fan_watchdog (driver):
4883 * fan watchdog interval in seconds, 0 disables (default), max 120
4886 /* sysfs fan pwm1_enable ----------------------------------------------- */
4887 static ssize_t fan_pwm1_enable_show(struct device *dev,
4888 struct device_attribute *attr,
4889 char *buf)
4891 int res, mode;
4892 u8 status;
4894 res = fan_get_status_safe(&status);
4895 if (res)
4896 return res;
4898 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4899 if (status != fan_control_initial_status) {
4900 tp_features.fan_ctrl_status_undef = 0;
4901 } else {
4902 /* Return most likely status. In fact, it
4903 * might be the only possible status */
4904 status = TP_EC_FAN_AUTO;
4908 if (status & TP_EC_FAN_FULLSPEED) {
4909 mode = 0;
4910 } else if (status & TP_EC_FAN_AUTO) {
4911 mode = 2;
4912 } else
4913 mode = 1;
4915 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4918 static ssize_t fan_pwm1_enable_store(struct device *dev,
4919 struct device_attribute *attr,
4920 const char *buf, size_t count)
4922 unsigned long t;
4923 int res, level;
4925 if (parse_strtoul(buf, 2, &t))
4926 return -EINVAL;
4928 switch (t) {
4929 case 0:
4930 level = TP_EC_FAN_FULLSPEED;
4931 break;
4932 case 1:
4933 level = TPACPI_FAN_LAST_LEVEL;
4934 break;
4935 case 2:
4936 level = TP_EC_FAN_AUTO;
4937 break;
4938 case 3:
4939 /* reserved for software-controlled auto mode */
4940 return -ENOSYS;
4941 default:
4942 return -EINVAL;
4945 res = fan_set_level_safe(level);
4946 if (res == -ENXIO)
4947 return -EINVAL;
4948 else if (res < 0)
4949 return res;
4951 fan_watchdog_reset();
4953 return count;
4956 static struct device_attribute dev_attr_fan_pwm1_enable =
4957 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4958 fan_pwm1_enable_show, fan_pwm1_enable_store);
4960 /* sysfs fan pwm1 ------------------------------------------------------ */
4961 static ssize_t fan_pwm1_show(struct device *dev,
4962 struct device_attribute *attr,
4963 char *buf)
4965 int res;
4966 u8 status;
4968 res = fan_get_status_safe(&status);
4969 if (res)
4970 return res;
4972 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4973 if (status != fan_control_initial_status) {
4974 tp_features.fan_ctrl_status_undef = 0;
4975 } else {
4976 status = TP_EC_FAN_AUTO;
4980 if ((status &
4981 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4982 status = fan_control_desired_level;
4984 if (status > 7)
4985 status = 7;
4987 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4990 static ssize_t fan_pwm1_store(struct device *dev,
4991 struct device_attribute *attr,
4992 const char *buf, size_t count)
4994 unsigned long s;
4995 int rc;
4996 u8 status, newlevel;
4998 if (parse_strtoul(buf, 255, &s))
4999 return -EINVAL;
5001 /* scale down from 0-255 to 0-7 */
5002 newlevel = (s >> 5) & 0x07;
5004 if (mutex_lock_interruptible(&fan_mutex))
5005 return -ERESTARTSYS;
5007 rc = fan_get_status(&status);
5008 if (!rc && (status &
5009 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5010 rc = fan_set_level(newlevel);
5011 if (rc == -ENXIO)
5012 rc = -EINVAL;
5013 else if (!rc) {
5014 fan_update_desired_level(newlevel);
5015 fan_watchdog_reset();
5019 mutex_unlock(&fan_mutex);
5020 return (rc)? rc : count;
5023 static struct device_attribute dev_attr_fan_pwm1 =
5024 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5025 fan_pwm1_show, fan_pwm1_store);
5027 /* sysfs fan fan1_input ------------------------------------------------ */
5028 static ssize_t fan_fan1_input_show(struct device *dev,
5029 struct device_attribute *attr,
5030 char *buf)
5032 int res;
5033 unsigned int speed;
5035 res = fan_get_speed(&speed);
5036 if (res < 0)
5037 return res;
5039 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5042 static struct device_attribute dev_attr_fan_fan1_input =
5043 __ATTR(fan1_input, S_IRUGO,
5044 fan_fan1_input_show, NULL);
5046 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5047 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5048 char *buf)
5050 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5053 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5054 const char *buf, size_t count)
5056 unsigned long t;
5058 if (parse_strtoul(buf, 120, &t))
5059 return -EINVAL;
5061 if (!fan_control_allowed)
5062 return -EPERM;
5064 fan_watchdog_maxinterval = t;
5065 fan_watchdog_reset();
5067 return count;
5070 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5071 fan_fan_watchdog_show, fan_fan_watchdog_store);
5073 /* --------------------------------------------------------------------- */
5074 static struct attribute *fan_attributes[] = {
5075 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5076 &dev_attr_fan_fan1_input.attr,
5077 NULL
5080 static const struct attribute_group fan_attr_group = {
5081 .attrs = fan_attributes,
5084 static int __init fan_init(struct ibm_init_struct *iibm)
5086 int rc;
5088 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5090 mutex_init(&fan_mutex);
5091 fan_status_access_mode = TPACPI_FAN_NONE;
5092 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5093 fan_control_commands = 0;
5094 fan_watchdog_maxinterval = 0;
5095 tp_features.fan_ctrl_status_undef = 0;
5096 fan_control_desired_level = 7;
5098 TPACPI_ACPIHANDLE_INIT(fans);
5099 TPACPI_ACPIHANDLE_INIT(gfan);
5100 TPACPI_ACPIHANDLE_INIT(sfan);
5102 if (gfan_handle) {
5103 /* 570, 600e/x, 770e, 770x */
5104 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5105 } else {
5106 /* all other ThinkPads: note that even old-style
5107 * ThinkPad ECs supports the fan control register */
5108 if (likely(acpi_ec_read(fan_status_offset,
5109 &fan_control_initial_status))) {
5110 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5112 /* In some ThinkPads, neither the EC nor the ACPI
5113 * DSDT initialize the fan status, and it ends up
5114 * being set to 0x07 when it *could* be either
5115 * 0x07 or 0x80.
5117 * Enable for TP-1Y (T43), TP-78 (R51e),
5118 * TP-76 (R52), TP-70 (T43, R52), which are known
5119 * to be buggy. */
5120 if (fan_control_initial_status == 0x07) {
5121 switch (thinkpad_id.ec_model) {
5122 case 0x5931: /* TP-1Y */
5123 case 0x3837: /* TP-78 */
5124 case 0x3637: /* TP-76 */
5125 case 0x3037: /* TP-70 */
5126 printk(TPACPI_NOTICE
5127 "fan_init: initial fan status "
5128 "is unknown, assuming it is "
5129 "in auto mode\n");
5130 tp_features.fan_ctrl_status_undef = 1;
5134 } else {
5135 printk(TPACPI_ERR
5136 "ThinkPad ACPI EC access misbehaving, "
5137 "fan status and control unavailable\n");
5138 return 1;
5142 if (sfan_handle) {
5143 /* 570, 770x-JL */
5144 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5145 fan_control_commands |=
5146 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5147 } else {
5148 if (!gfan_handle) {
5149 /* gfan without sfan means no fan control */
5150 /* all other models implement TP EC 0x2f control */
5152 if (fans_handle) {
5153 /* X31, X40, X41 */
5154 fan_control_access_mode =
5155 TPACPI_FAN_WR_ACPI_FANS;
5156 fan_control_commands |=
5157 TPACPI_FAN_CMD_SPEED |
5158 TPACPI_FAN_CMD_LEVEL |
5159 TPACPI_FAN_CMD_ENABLE;
5160 } else {
5161 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5162 fan_control_commands |=
5163 TPACPI_FAN_CMD_LEVEL |
5164 TPACPI_FAN_CMD_ENABLE;
5169 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5170 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5171 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5172 fan_status_access_mode, fan_control_access_mode);
5174 /* fan control master switch */
5175 if (!fan_control_allowed) {
5176 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5177 fan_control_commands = 0;
5178 dbg_printk(TPACPI_DBG_INIT,
5179 "fan control features disabled by parameter\n");
5182 /* update fan_control_desired_level */
5183 if (fan_status_access_mode != TPACPI_FAN_NONE)
5184 fan_get_status_safe(NULL);
5186 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5187 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5188 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5189 &fan_attr_group);
5190 if (!(rc < 0))
5191 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5192 &driver_attr_fan_watchdog);
5193 if (rc < 0)
5194 return rc;
5195 return 0;
5196 } else
5197 return 1;
5200 static void fan_exit(void)
5202 vdbg_printk(TPACPI_DBG_EXIT,
5203 "cancelling any pending fan watchdog tasks\n");
5205 /* FIXME: can we really do this unconditionally? */
5206 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5207 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5208 &driver_attr_fan_watchdog);
5210 cancel_delayed_work(&fan_watchdog_task);
5211 flush_scheduled_work();
5214 static int fan_read(char *p)
5216 int len = 0;
5217 int rc;
5218 u8 status;
5219 unsigned int speed = 0;
5221 switch (fan_status_access_mode) {
5222 case TPACPI_FAN_RD_ACPI_GFAN:
5223 /* 570, 600e/x, 770e, 770x */
5224 rc = fan_get_status_safe(&status);
5225 if (rc < 0)
5226 return rc;
5228 len += sprintf(p + len, "status:\t\t%s\n"
5229 "level:\t\t%d\n",
5230 (status != 0) ? "enabled" : "disabled", status);
5231 break;
5233 case TPACPI_FAN_RD_TPEC:
5234 /* all except 570, 600e/x, 770e, 770x */
5235 rc = fan_get_status_safe(&status);
5236 if (rc < 0)
5237 return rc;
5239 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5240 if (status != fan_control_initial_status)
5241 tp_features.fan_ctrl_status_undef = 0;
5242 else
5243 /* Return most likely status. In fact, it
5244 * might be the only possible status */
5245 status = TP_EC_FAN_AUTO;
5248 len += sprintf(p + len, "status:\t\t%s\n",
5249 (status != 0) ? "enabled" : "disabled");
5251 rc = fan_get_speed(&speed);
5252 if (rc < 0)
5253 return rc;
5255 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5257 if (status & TP_EC_FAN_FULLSPEED)
5258 /* Disengaged mode takes precedence */
5259 len += sprintf(p + len, "level:\t\tdisengaged\n");
5260 else if (status & TP_EC_FAN_AUTO)
5261 len += sprintf(p + len, "level:\t\tauto\n");
5262 else
5263 len += sprintf(p + len, "level:\t\t%d\n", status);
5264 break;
5266 case TPACPI_FAN_NONE:
5267 default:
5268 len += sprintf(p + len, "status:\t\tnot supported\n");
5271 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5272 len += sprintf(p + len, "commands:\tlevel <level>");
5274 switch (fan_control_access_mode) {
5275 case TPACPI_FAN_WR_ACPI_SFAN:
5276 len += sprintf(p + len, " (<level> is 0-7)\n");
5277 break;
5279 default:
5280 len += sprintf(p + len, " (<level> is 0-7, "
5281 "auto, disengaged, full-speed)\n");
5282 break;
5286 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5287 len += sprintf(p + len, "commands:\tenable, disable\n"
5288 "commands:\twatchdog <timeout> (<timeout> "
5289 "is 0 (off), 1-120 (seconds))\n");
5291 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5292 len += sprintf(p + len, "commands:\tspeed <speed>"
5293 " (<speed> is 0-65535)\n");
5295 return len;
5298 static int fan_write_cmd_level(const char *cmd, int *rc)
5300 int level;
5302 if (strlencmp(cmd, "level auto") == 0)
5303 level = TP_EC_FAN_AUTO;
5304 else if ((strlencmp(cmd, "level disengaged") == 0) |
5305 (strlencmp(cmd, "level full-speed") == 0))
5306 level = TP_EC_FAN_FULLSPEED;
5307 else if (sscanf(cmd, "level %d", &level) != 1)
5308 return 0;
5310 *rc = fan_set_level_safe(level);
5311 if (*rc == -ENXIO)
5312 printk(TPACPI_ERR "level command accepted for unsupported "
5313 "access mode %d", fan_control_access_mode);
5315 return 1;
5318 static int fan_write_cmd_enable(const char *cmd, int *rc)
5320 if (strlencmp(cmd, "enable") != 0)
5321 return 0;
5323 *rc = fan_set_enable();
5324 if (*rc == -ENXIO)
5325 printk(TPACPI_ERR "enable command accepted for unsupported "
5326 "access mode %d", fan_control_access_mode);
5328 return 1;
5331 static int fan_write_cmd_disable(const char *cmd, int *rc)
5333 if (strlencmp(cmd, "disable") != 0)
5334 return 0;
5336 *rc = fan_set_disable();
5337 if (*rc == -ENXIO)
5338 printk(TPACPI_ERR "disable command accepted for unsupported "
5339 "access mode %d", fan_control_access_mode);
5341 return 1;
5344 static int fan_write_cmd_speed(const char *cmd, int *rc)
5346 int speed;
5348 /* TODO:
5349 * Support speed <low> <medium> <high> ? */
5351 if (sscanf(cmd, "speed %d", &speed) != 1)
5352 return 0;
5354 *rc = fan_set_speed(speed);
5355 if (*rc == -ENXIO)
5356 printk(TPACPI_ERR "speed command accepted for unsupported "
5357 "access mode %d", fan_control_access_mode);
5359 return 1;
5362 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5364 int interval;
5366 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5367 return 0;
5369 if (interval < 0 || interval > 120)
5370 *rc = -EINVAL;
5371 else
5372 fan_watchdog_maxinterval = interval;
5374 return 1;
5377 static int fan_write(char *buf)
5379 char *cmd;
5380 int rc = 0;
5382 while (!rc && (cmd = next_cmd(&buf))) {
5383 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5384 fan_write_cmd_level(cmd, &rc)) &&
5385 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5386 (fan_write_cmd_enable(cmd, &rc) ||
5387 fan_write_cmd_disable(cmd, &rc) ||
5388 fan_write_cmd_watchdog(cmd, &rc))) &&
5389 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5390 fan_write_cmd_speed(cmd, &rc))
5392 rc = -EINVAL;
5393 else if (!rc)
5394 fan_watchdog_reset();
5397 return rc;
5400 static struct ibm_struct fan_driver_data = {
5401 .name = "fan",
5402 .read = fan_read,
5403 .write = fan_write,
5404 .exit = fan_exit,
5407 /****************************************************************************
5408 ****************************************************************************
5410 * Infrastructure
5412 ****************************************************************************
5413 ****************************************************************************/
5415 /* sysfs name ---------------------------------------------------------- */
5416 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5417 struct device_attribute *attr,
5418 char *buf)
5420 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5423 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5424 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5426 /* --------------------------------------------------------------------- */
5428 /* /proc support */
5429 static struct proc_dir_entry *proc_dir;
5432 * Module and infrastructure proble, init and exit handling
5435 static int force_load;
5437 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5438 static const char * __init str_supported(int is_supported)
5440 static char text_unsupported[] __initdata = "not supported";
5442 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5444 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5446 static void ibm_exit(struct ibm_struct *ibm)
5448 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5450 list_del_init(&ibm->all_drivers);
5452 if (ibm->flags.acpi_notify_installed) {
5453 dbg_printk(TPACPI_DBG_EXIT,
5454 "%s: acpi_remove_notify_handler\n", ibm->name);
5455 BUG_ON(!ibm->acpi);
5456 acpi_remove_notify_handler(*ibm->acpi->handle,
5457 ibm->acpi->type,
5458 dispatch_acpi_notify);
5459 ibm->flags.acpi_notify_installed = 0;
5460 ibm->flags.acpi_notify_installed = 0;
5463 if (ibm->flags.proc_created) {
5464 dbg_printk(TPACPI_DBG_EXIT,
5465 "%s: remove_proc_entry\n", ibm->name);
5466 remove_proc_entry(ibm->name, proc_dir);
5467 ibm->flags.proc_created = 0;
5470 if (ibm->flags.acpi_driver_registered) {
5471 dbg_printk(TPACPI_DBG_EXIT,
5472 "%s: acpi_bus_unregister_driver\n", ibm->name);
5473 BUG_ON(!ibm->acpi);
5474 acpi_bus_unregister_driver(ibm->acpi->driver);
5475 kfree(ibm->acpi->driver);
5476 ibm->acpi->driver = NULL;
5477 ibm->flags.acpi_driver_registered = 0;
5480 if (ibm->flags.init_called && ibm->exit) {
5481 ibm->exit();
5482 ibm->flags.init_called = 0;
5485 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5488 static int __init ibm_init(struct ibm_init_struct *iibm)
5490 int ret;
5491 struct ibm_struct *ibm = iibm->data;
5492 struct proc_dir_entry *entry;
5494 BUG_ON(ibm == NULL);
5496 INIT_LIST_HEAD(&ibm->all_drivers);
5498 if (ibm->flags.experimental && !experimental)
5499 return 0;
5501 dbg_printk(TPACPI_DBG_INIT,
5502 "probing for %s\n", ibm->name);
5504 if (iibm->init) {
5505 ret = iibm->init(iibm);
5506 if (ret > 0)
5507 return 0; /* probe failed */
5508 if (ret)
5509 return ret;
5511 ibm->flags.init_called = 1;
5514 if (ibm->acpi) {
5515 if (ibm->acpi->hid) {
5516 ret = register_tpacpi_subdriver(ibm);
5517 if (ret)
5518 goto err_out;
5521 if (ibm->acpi->notify) {
5522 ret = setup_acpi_notify(ibm);
5523 if (ret == -ENODEV) {
5524 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5525 ibm->name);
5526 ret = 0;
5527 goto err_out;
5529 if (ret < 0)
5530 goto err_out;
5534 dbg_printk(TPACPI_DBG_INIT,
5535 "%s installed\n", ibm->name);
5537 if (ibm->read) {
5538 entry = create_proc_entry(ibm->name,
5539 S_IFREG | S_IRUGO | S_IWUSR,
5540 proc_dir);
5541 if (!entry) {
5542 printk(TPACPI_ERR "unable to create proc entry %s\n",
5543 ibm->name);
5544 ret = -ENODEV;
5545 goto err_out;
5547 entry->owner = THIS_MODULE;
5548 entry->data = ibm;
5549 entry->read_proc = &dispatch_procfs_read;
5550 if (ibm->write)
5551 entry->write_proc = &dispatch_procfs_write;
5552 ibm->flags.proc_created = 1;
5555 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5557 return 0;
5559 err_out:
5560 dbg_printk(TPACPI_DBG_INIT,
5561 "%s: at error exit path with result %d\n",
5562 ibm->name, ret);
5564 ibm_exit(ibm);
5565 return (ret < 0)? ret : 0;
5568 /* Probing */
5570 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5572 const struct dmi_device *dev = NULL;
5573 char ec_fw_string[18];
5575 if (!tp)
5576 return;
5578 memset(tp, 0, sizeof(*tp));
5580 if (dmi_name_in_vendors("IBM"))
5581 tp->vendor = PCI_VENDOR_ID_IBM;
5582 else if (dmi_name_in_vendors("LENOVO"))
5583 tp->vendor = PCI_VENDOR_ID_LENOVO;
5584 else
5585 return;
5587 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5588 GFP_KERNEL);
5589 if (!tp->bios_version_str)
5590 return;
5591 tp->bios_model = tp->bios_version_str[0]
5592 | (tp->bios_version_str[1] << 8);
5595 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5596 * X32 or newer, all Z series; Some models must have an
5597 * up-to-date BIOS or they will not be detected.
5599 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5601 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5602 if (sscanf(dev->name,
5603 "IBM ThinkPad Embedded Controller -[%17c",
5604 ec_fw_string) == 1) {
5605 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5606 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5608 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5609 tp->ec_model = ec_fw_string[0]
5610 | (ec_fw_string[1] << 8);
5611 break;
5615 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5616 GFP_KERNEL);
5617 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5618 kfree(tp->model_str);
5619 tp->model_str = NULL;
5623 static int __init probe_for_thinkpad(void)
5625 int is_thinkpad;
5627 if (acpi_disabled)
5628 return -ENODEV;
5631 * Non-ancient models have better DMI tagging, but very old models
5632 * don't.
5634 is_thinkpad = (thinkpad_id.model_str != NULL);
5636 /* ec is required because many other handles are relative to it */
5637 TPACPI_ACPIHANDLE_INIT(ec);
5638 if (!ec_handle) {
5639 if (is_thinkpad)
5640 printk(TPACPI_ERR
5641 "Not yet supported ThinkPad detected!\n");
5642 return -ENODEV;
5646 * Risks a regression on very old machines, but reduces potential
5647 * false positives a damn great deal
5649 if (!is_thinkpad)
5650 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5652 if (!is_thinkpad && !force_load)
5653 return -ENODEV;
5655 return 0;
5659 /* Module init, exit, parameters */
5661 static struct ibm_init_struct ibms_init[] __initdata = {
5663 .init = thinkpad_acpi_driver_init,
5664 .data = &thinkpad_acpi_driver_data,
5667 .init = hotkey_init,
5668 .data = &hotkey_driver_data,
5671 .init = bluetooth_init,
5672 .data = &bluetooth_driver_data,
5675 .init = wan_init,
5676 .data = &wan_driver_data,
5679 .init = video_init,
5680 .data = &video_driver_data,
5683 .init = light_init,
5684 .data = &light_driver_data,
5686 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5688 .init = dock_init,
5689 .data = &dock_driver_data[0],
5692 .init = dock_init2,
5693 .data = &dock_driver_data[1],
5695 #endif
5696 #ifdef CONFIG_THINKPAD_ACPI_BAY
5698 .init = bay_init,
5699 .data = &bay_driver_data,
5701 #endif
5703 .init = cmos_init,
5704 .data = &cmos_driver_data,
5707 .init = led_init,
5708 .data = &led_driver_data,
5711 .init = beep_init,
5712 .data = &beep_driver_data,
5715 .init = thermal_init,
5716 .data = &thermal_driver_data,
5719 .data = &ecdump_driver_data,
5722 .init = brightness_init,
5723 .data = &brightness_driver_data,
5726 .data = &volume_driver_data,
5729 .init = fan_init,
5730 .data = &fan_driver_data,
5734 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5736 unsigned int i;
5737 struct ibm_struct *ibm;
5739 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5740 ibm = ibms_init[i].data;
5741 BUG_ON(ibm == NULL);
5743 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5744 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5745 return -ENOSPC;
5746 strcpy(ibms_init[i].param, val);
5747 strcat(ibms_init[i].param, ",");
5748 return 0;
5752 return -EINVAL;
5755 module_param(experimental, int, 0);
5756 MODULE_PARM_DESC(experimental,
5757 "Enables experimental features when non-zero");
5759 module_param_named(debug, dbg_level, uint, 0);
5760 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5762 module_param(force_load, bool, 0);
5763 MODULE_PARM_DESC(force_load,
5764 "Attempts to load the driver even on a "
5765 "mis-identified ThinkPad when true");
5767 module_param_named(fan_control, fan_control_allowed, bool, 0);
5768 MODULE_PARM_DESC(fan_control,
5769 "Enables setting fan parameters features when true");
5771 module_param_named(brightness_mode, brightness_mode, int, 0);
5772 MODULE_PARM_DESC(brightness_mode,
5773 "Selects brightness control strategy: "
5774 "0=auto, 1=EC, 2=CMOS, 3=both");
5776 module_param(brightness_enable, uint, 0);
5777 MODULE_PARM_DESC(brightness_enable,
5778 "Enables backlight control when 1, disables when 0");
5780 module_param(hotkey_report_mode, uint, 0);
5781 MODULE_PARM_DESC(hotkey_report_mode,
5782 "used for backwards compatibility with userspace, "
5783 "see documentation");
5785 #define TPACPI_PARAM(feature) \
5786 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5787 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5788 "at module load, see documentation")
5790 TPACPI_PARAM(hotkey);
5791 TPACPI_PARAM(bluetooth);
5792 TPACPI_PARAM(video);
5793 TPACPI_PARAM(light);
5794 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5795 TPACPI_PARAM(dock);
5796 #endif
5797 #ifdef CONFIG_THINKPAD_ACPI_BAY
5798 TPACPI_PARAM(bay);
5799 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5800 TPACPI_PARAM(cmos);
5801 TPACPI_PARAM(led);
5802 TPACPI_PARAM(beep);
5803 TPACPI_PARAM(ecdump);
5804 TPACPI_PARAM(brightness);
5805 TPACPI_PARAM(volume);
5806 TPACPI_PARAM(fan);
5808 static void thinkpad_acpi_module_exit(void)
5810 struct ibm_struct *ibm, *itmp;
5812 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5814 list_for_each_entry_safe_reverse(ibm, itmp,
5815 &tpacpi_all_drivers,
5816 all_drivers) {
5817 ibm_exit(ibm);
5820 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5822 if (tpacpi_inputdev) {
5823 if (tp_features.input_device_registered)
5824 input_unregister_device(tpacpi_inputdev);
5825 else
5826 input_free_device(tpacpi_inputdev);
5829 if (tpacpi_hwmon)
5830 hwmon_device_unregister(tpacpi_hwmon);
5832 if (tp_features.sensors_pdev_attrs_registered)
5833 device_remove_file(&tpacpi_sensors_pdev->dev,
5834 &dev_attr_thinkpad_acpi_pdev_name);
5835 if (tpacpi_sensors_pdev)
5836 platform_device_unregister(tpacpi_sensors_pdev);
5837 if (tpacpi_pdev)
5838 platform_device_unregister(tpacpi_pdev);
5840 if (tp_features.sensors_pdrv_attrs_registered)
5841 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5842 if (tp_features.platform_drv_attrs_registered)
5843 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5845 if (tp_features.sensors_pdrv_registered)
5846 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5848 if (tp_features.platform_drv_registered)
5849 platform_driver_unregister(&tpacpi_pdriver);
5851 if (proc_dir)
5852 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5854 kfree(thinkpad_id.bios_version_str);
5855 kfree(thinkpad_id.ec_version_str);
5856 kfree(thinkpad_id.model_str);
5860 static int __init thinkpad_acpi_module_init(void)
5862 int ret, i;
5864 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5866 /* Parameter checking */
5867 if (hotkey_report_mode > 2)
5868 return -EINVAL;
5870 /* Driver-level probe */
5872 get_thinkpad_model_data(&thinkpad_id);
5873 ret = probe_for_thinkpad();
5874 if (ret) {
5875 thinkpad_acpi_module_exit();
5876 return ret;
5879 /* Driver initialization */
5881 TPACPI_ACPIHANDLE_INIT(ecrd);
5882 TPACPI_ACPIHANDLE_INIT(ecwr);
5884 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5885 if (!proc_dir) {
5886 printk(TPACPI_ERR
5887 "unable to create proc dir " TPACPI_PROC_DIR);
5888 thinkpad_acpi_module_exit();
5889 return -ENODEV;
5891 proc_dir->owner = THIS_MODULE;
5893 ret = platform_driver_register(&tpacpi_pdriver);
5894 if (ret) {
5895 printk(TPACPI_ERR
5896 "unable to register main platform driver\n");
5897 thinkpad_acpi_module_exit();
5898 return ret;
5900 tp_features.platform_drv_registered = 1;
5902 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5903 if (ret) {
5904 printk(TPACPI_ERR
5905 "unable to register hwmon platform driver\n");
5906 thinkpad_acpi_module_exit();
5907 return ret;
5909 tp_features.sensors_pdrv_registered = 1;
5911 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5912 if (!ret) {
5913 tp_features.platform_drv_attrs_registered = 1;
5914 ret = tpacpi_create_driver_attributes(
5915 &tpacpi_hwmon_pdriver.driver);
5917 if (ret) {
5918 printk(TPACPI_ERR
5919 "unable to create sysfs driver attributes\n");
5920 thinkpad_acpi_module_exit();
5921 return ret;
5923 tp_features.sensors_pdrv_attrs_registered = 1;
5926 /* Device initialization */
5927 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
5928 NULL, 0);
5929 if (IS_ERR(tpacpi_pdev)) {
5930 ret = PTR_ERR(tpacpi_pdev);
5931 tpacpi_pdev = NULL;
5932 printk(TPACPI_ERR "unable to register platform device\n");
5933 thinkpad_acpi_module_exit();
5934 return ret;
5936 tpacpi_sensors_pdev = platform_device_register_simple(
5937 TPACPI_HWMON_DRVR_NAME,
5938 -1, NULL, 0);
5939 if (IS_ERR(tpacpi_sensors_pdev)) {
5940 ret = PTR_ERR(tpacpi_sensors_pdev);
5941 tpacpi_sensors_pdev = NULL;
5942 printk(TPACPI_ERR
5943 "unable to register hwmon platform device\n");
5944 thinkpad_acpi_module_exit();
5945 return ret;
5947 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5948 &dev_attr_thinkpad_acpi_pdev_name);
5949 if (ret) {
5950 printk(TPACPI_ERR
5951 "unable to create sysfs hwmon device attributes\n");
5952 thinkpad_acpi_module_exit();
5953 return ret;
5955 tp_features.sensors_pdev_attrs_registered = 1;
5956 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5957 if (IS_ERR(tpacpi_hwmon)) {
5958 ret = PTR_ERR(tpacpi_hwmon);
5959 tpacpi_hwmon = NULL;
5960 printk(TPACPI_ERR "unable to register hwmon device\n");
5961 thinkpad_acpi_module_exit();
5962 return ret;
5964 mutex_init(&tpacpi_inputdev_send_mutex);
5965 tpacpi_inputdev = input_allocate_device();
5966 if (!tpacpi_inputdev) {
5967 printk(TPACPI_ERR "unable to allocate input device\n");
5968 thinkpad_acpi_module_exit();
5969 return -ENOMEM;
5970 } else {
5971 /* Prepare input device, but don't register */
5972 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5973 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
5974 tpacpi_inputdev->id.bustype = BUS_HOST;
5975 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5976 thinkpad_id.vendor :
5977 PCI_VENDOR_ID_IBM;
5978 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5979 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5981 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5982 ret = ibm_init(&ibms_init[i]);
5983 if (ret >= 0 && *ibms_init[i].param)
5984 ret = ibms_init[i].data->write(ibms_init[i].param);
5985 if (ret < 0) {
5986 thinkpad_acpi_module_exit();
5987 return ret;
5990 ret = input_register_device(tpacpi_inputdev);
5991 if (ret < 0) {
5992 printk(TPACPI_ERR "unable to register input device\n");
5993 thinkpad_acpi_module_exit();
5994 return ret;
5995 } else {
5996 tp_features.input_device_registered = 1;
5999 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6000 return 0;
6003 /* Please remove this in year 2009 */
6004 MODULE_ALIAS("ibm_acpi");
6007 * DMI matching for module autoloading
6009 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6010 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6012 * Only models listed in thinkwiki will be supported, so add yours
6013 * if it is not there yet.
6015 #define IBM_BIOS_MODULE_ALIAS(__type) \
6016 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6018 /* Non-ancient thinkpads */
6019 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6020 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6022 /* Ancient thinkpad BIOSes have to be identified by
6023 * BIOS type or model number, and there are far less
6024 * BIOS types than model numbers... */
6025 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6026 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6027 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6029 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6030 MODULE_DESCRIPTION(TPACPI_DESC);
6031 MODULE_VERSION(TPACPI_VERSION);
6032 MODULE_LICENSE("GPL");
6034 module_init(thinkpad_acpi_module_init);
6035 module_exit(thinkpad_acpi_module_exit);