Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / misc / thinkpad_acpi.c
blob27988516b3301c8add7070ad73c083d9bab1849e
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.19"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
98 /* NVRAM bit masks */
99 enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
124 * Main driver
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
172 struct ibm_struct;
174 struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
184 struct ibm_struct {
185 char *name;
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
206 struct ibm_init_struct {
207 char param[32];
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219 #endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
225 =======
226 u32 hotkey_tablet:1;
227 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
228 u32 light:1;
229 u32 light_status:1;
230 u32 bright_16levels:1;
231 u32 wan:1;
232 u32 fan_ctrl_status_undef:1;
233 u32 input_device_registered:1;
234 u32 platform_drv_registered:1;
235 u32 platform_drv_attrs_registered:1;
236 u32 sensors_pdrv_registered:1;
237 u32 sensors_pdrv_attrs_registered:1;
238 u32 sensors_pdev_attrs_registered:1;
239 u32 hotkey_poll_active:1;
240 } tp_features;
242 struct thinkpad_id_data {
243 unsigned int vendor; /* ThinkPad vendor:
244 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
246 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
247 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
249 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
250 u16 ec_model;
252 char *model_str;
254 static struct thinkpad_id_data thinkpad_id;
256 static enum {
257 TPACPI_LIFE_INIT = 0,
258 TPACPI_LIFE_RUNNING,
259 TPACPI_LIFE_EXITING,
260 } tpacpi_lifecycle;
262 static int experimental;
263 static u32 dbg_level;
265 /****************************************************************************
266 ****************************************************************************
268 * ACPI Helpers and device model
270 ****************************************************************************
271 ****************************************************************************/
273 /*************************************************************************
274 * ACPI basic handles
277 static acpi_handle root_handle;
279 #define TPACPI_HANDLE(object, parent, paths...) \
280 static acpi_handle object##_handle; \
281 static acpi_handle *object##_parent = &parent##_handle; \
282 static char *object##_path; \
283 static char *object##_paths[] = { paths }
285 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
286 "\\_SB.PCI.ISA.EC", /* 570 */
287 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
288 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
289 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
290 "\\_SB.PCI0.ICH3.EC0", /* R31 */
291 "\\_SB.PCI0.LPC.EC", /* all others */
294 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
295 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
297 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
298 /* T4x, X31, X40 */
299 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
300 "\\CMS", /* R40, R40e */
301 ); /* all others */
303 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
304 "^HKEY", /* R30, R31 */
305 "HKEY", /* all others */
306 ); /* 570 */
308 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
309 =======
310 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
311 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
312 "\\_SB.PCI0.VID0", /* 770e */
313 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
314 "\\_SB.PCI0.AGP.VID", /* all others */
315 ); /* R30, R31 */
317 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
319 /*************************************************************************
320 * ACPI helpers
323 static int acpi_evalf(acpi_handle handle,
324 void *res, char *method, char *fmt, ...)
326 char *fmt0 = fmt;
327 struct acpi_object_list params;
328 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
329 struct acpi_buffer result, *resultp;
330 union acpi_object out_obj;
331 acpi_status status;
332 va_list ap;
333 char res_type;
334 int success;
335 int quiet;
337 if (!*fmt) {
338 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
339 return 0;
342 if (*fmt == 'q') {
343 quiet = 1;
344 fmt++;
345 } else
346 quiet = 0;
348 res_type = *(fmt++);
350 params.count = 0;
351 params.pointer = &in_objs[0];
353 va_start(ap, fmt);
354 while (*fmt) {
355 char c = *(fmt++);
356 switch (c) {
357 case 'd': /* int */
358 in_objs[params.count].integer.value = va_arg(ap, int);
359 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
360 break;
361 /* add more types as needed */
362 default:
363 printk(TPACPI_ERR "acpi_evalf() called "
364 "with invalid format character '%c'\n", c);
365 return 0;
368 va_end(ap);
370 if (res_type != 'v') {
371 result.length = sizeof(out_obj);
372 result.pointer = &out_obj;
373 resultp = &result;
374 } else
375 resultp = NULL;
377 status = acpi_evaluate_object(handle, method, &params, resultp);
379 switch (res_type) {
380 case 'd': /* int */
381 if (res)
382 *(int *)res = out_obj.integer.value;
383 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
384 break;
385 case 'v': /* void */
386 success = status == AE_OK;
387 break;
388 /* add more types as needed */
389 default:
390 printk(TPACPI_ERR "acpi_evalf() called "
391 "with invalid format character '%c'\n", res_type);
392 return 0;
395 if (!success && !quiet)
396 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
397 method, fmt0, status);
399 return success;
402 static int acpi_ec_read(int i, u8 *p)
404 int v;
406 if (ecrd_handle) {
407 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
408 return 0;
409 *p = v;
410 } else {
411 if (ec_read(i, p) < 0)
412 return 0;
415 return 1;
418 static int acpi_ec_write(int i, u8 v)
420 if (ecwr_handle) {
421 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
422 return 0;
423 } else {
424 if (ec_write(i, v) < 0)
425 return 0;
428 return 1;
431 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
432 static int _sta(acpi_handle handle)
434 int status;
436 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
437 status = 0;
439 return status;
441 #endif
443 static int issue_thinkpad_cmos_command(int cmos_cmd)
445 if (!cmos_handle)
446 return -ENXIO;
448 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
449 return -EIO;
451 return 0;
454 /*************************************************************************
455 * ACPI device model
458 #define TPACPI_ACPIHANDLE_INIT(object) \
459 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
460 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
462 static void drv_acpi_handle_init(char *name,
463 acpi_handle *handle, acpi_handle parent,
464 char **paths, int num_paths, char **path)
466 int i;
467 acpi_status status;
469 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
470 name);
472 for (i = 0; i < num_paths; i++) {
473 status = acpi_get_handle(parent, paths[i], handle);
474 if (ACPI_SUCCESS(status)) {
475 *path = paths[i];
476 dbg_printk(TPACPI_DBG_INIT,
477 "Found ACPI handle %s for %s\n",
478 *path, name);
479 return;
483 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
484 name);
485 *handle = NULL;
488 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
490 struct ibm_struct *ibm = data;
492 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
493 return;
495 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
496 return;
498 ibm->acpi->notify(ibm, event);
501 static int __init setup_acpi_notify(struct ibm_struct *ibm)
503 acpi_status status;
504 int rc;
506 BUG_ON(!ibm->acpi);
508 if (!*ibm->acpi->handle)
509 return 0;
511 vdbg_printk(TPACPI_DBG_INIT,
512 "setting up ACPI notify for %s\n", ibm->name);
514 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
515 if (rc < 0) {
516 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
517 ibm->name, rc);
518 return -ENODEV;
521 acpi_driver_data(ibm->acpi->device) = ibm;
522 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
523 TPACPI_ACPI_EVENT_PREFIX,
524 ibm->name);
526 status = acpi_install_notify_handler(*ibm->acpi->handle,
527 ibm->acpi->type, dispatch_acpi_notify, ibm);
528 if (ACPI_FAILURE(status)) {
529 if (status == AE_ALREADY_EXISTS) {
530 printk(TPACPI_NOTICE
531 "another device driver is already "
532 "handling %s events\n", ibm->name);
533 } else {
534 printk(TPACPI_ERR
535 "acpi_install_notify_handler(%s) failed: %d\n",
536 ibm->name, status);
538 return -ENODEV;
540 ibm->flags.acpi_notify_installed = 1;
541 return 0;
544 static int __init tpacpi_device_add(struct acpi_device *device)
546 return 0;
549 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
551 int rc;
553 dbg_printk(TPACPI_DBG_INIT,
554 "registering %s as an ACPI driver\n", ibm->name);
556 BUG_ON(!ibm->acpi);
558 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
559 if (!ibm->acpi->driver) {
560 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
561 return -ENOMEM;
564 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
565 ibm->acpi->driver->ids = ibm->acpi->hid;
567 ibm->acpi->driver->ops.add = &tpacpi_device_add;
569 rc = acpi_bus_register_driver(ibm->acpi->driver);
570 if (rc < 0) {
571 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
572 ibm->name, rc);
573 kfree(ibm->acpi->driver);
574 ibm->acpi->driver = NULL;
575 } else if (!rc)
576 ibm->flags.acpi_driver_registered = 1;
578 return rc;
582 /****************************************************************************
583 ****************************************************************************
585 * Procfs Helpers
587 ****************************************************************************
588 ****************************************************************************/
590 static int dispatch_procfs_read(char *page, char **start, off_t off,
591 int count, int *eof, void *data)
593 struct ibm_struct *ibm = data;
594 int len;
596 if (!ibm || !ibm->read)
597 return -EINVAL;
599 len = ibm->read(page);
600 if (len < 0)
601 return len;
603 if (len <= off + count)
604 *eof = 1;
605 *start = page + off;
606 len -= off;
607 if (len > count)
608 len = count;
609 if (len < 0)
610 len = 0;
612 return len;
615 static int dispatch_procfs_write(struct file *file,
616 const char __user *userbuf,
617 unsigned long count, void *data)
619 struct ibm_struct *ibm = data;
620 char *kernbuf;
621 int ret;
623 if (!ibm || !ibm->write)
624 return -EINVAL;
626 kernbuf = kmalloc(count + 2, GFP_KERNEL);
627 if (!kernbuf)
628 return -ENOMEM;
630 if (copy_from_user(kernbuf, userbuf, count)) {
631 kfree(kernbuf);
632 return -EFAULT;
635 kernbuf[count] = 0;
636 strcat(kernbuf, ",");
637 ret = ibm->write(kernbuf);
638 if (ret == 0)
639 ret = count;
641 kfree(kernbuf);
643 return ret;
646 static char *next_cmd(char **cmds)
648 char *start = *cmds;
649 char *end;
651 while ((end = strchr(start, ',')) && end == start)
652 start = end + 1;
654 if (!end)
655 return NULL;
657 *end = 0;
658 *cmds = end + 1;
659 return start;
663 /****************************************************************************
664 ****************************************************************************
666 * Device model: input, hwmon and platform
668 ****************************************************************************
669 ****************************************************************************/
671 static struct platform_device *tpacpi_pdev;
672 static struct platform_device *tpacpi_sensors_pdev;
673 static struct device *tpacpi_hwmon;
674 static struct input_dev *tpacpi_inputdev;
675 static struct mutex tpacpi_inputdev_send_mutex;
676 static LIST_HEAD(tpacpi_all_drivers);
678 static int tpacpi_suspend_handler(struct platform_device *pdev,
679 pm_message_t state)
681 struct ibm_struct *ibm, *itmp;
683 list_for_each_entry_safe(ibm, itmp,
684 &tpacpi_all_drivers,
685 all_drivers) {
686 if (ibm->suspend)
687 (ibm->suspend)(state);
690 return 0;
693 static int tpacpi_resume_handler(struct platform_device *pdev)
695 struct ibm_struct *ibm, *itmp;
697 list_for_each_entry_safe(ibm, itmp,
698 &tpacpi_all_drivers,
699 all_drivers) {
700 if (ibm->resume)
701 (ibm->resume)();
704 return 0;
707 static struct platform_driver tpacpi_pdriver = {
708 .driver = {
709 .name = TPACPI_DRVR_NAME,
710 .owner = THIS_MODULE,
712 .suspend = tpacpi_suspend_handler,
713 .resume = tpacpi_resume_handler,
716 static struct platform_driver tpacpi_hwmon_pdriver = {
717 .driver = {
718 .name = TPACPI_HWMON_DRVR_NAME,
719 .owner = THIS_MODULE,
723 /*************************************************************************
724 * sysfs support helpers
727 struct attribute_set {
728 unsigned int members, max_members;
729 struct attribute_group group;
732 struct attribute_set_obj {
733 struct attribute_set s;
734 struct attribute *a;
735 } __attribute__((packed));
737 static struct attribute_set *create_attr_set(unsigned int max_members,
738 const char *name)
740 struct attribute_set_obj *sobj;
742 if (max_members == 0)
743 return NULL;
745 /* Allocates space for implicit NULL at the end too */
746 sobj = kzalloc(sizeof(struct attribute_set_obj) +
747 max_members * sizeof(struct attribute *),
748 GFP_KERNEL);
749 if (!sobj)
750 return NULL;
751 sobj->s.max_members = max_members;
752 sobj->s.group.attrs = &sobj->a;
753 sobj->s.group.name = name;
755 return &sobj->s;
758 #define destroy_attr_set(_set) \
759 kfree(_set);
761 /* not multi-threaded safe, use it in a single thread per set */
762 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
764 if (!s || !attr)
765 return -EINVAL;
767 if (s->members >= s->max_members)
768 return -ENOMEM;
770 s->group.attrs[s->members] = attr;
771 s->members++;
773 return 0;
776 static int add_many_to_attr_set(struct attribute_set *s,
777 struct attribute **attr,
778 unsigned int count)
780 int i, res;
782 for (i = 0; i < count; i++) {
783 res = add_to_attr_set(s, attr[i]);
784 if (res)
785 return res;
788 return 0;
791 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
793 sysfs_remove_group(kobj, &s->group);
794 destroy_attr_set(s);
797 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
798 sysfs_create_group(_kobj, &_attr_set->group)
800 static int parse_strtoul(const char *buf,
801 unsigned long max, unsigned long *value)
803 char *endp;
805 while (*buf && isspace(*buf))
806 buf++;
807 *value = simple_strtoul(buf, &endp, 0);
808 while (*endp && isspace(*endp))
809 endp++;
810 if (*endp || *value > max)
811 return -EINVAL;
813 return 0;
816 /*************************************************************************
817 * thinkpad-acpi driver attributes
820 /* interface_version --------------------------------------------------- */
821 static ssize_t tpacpi_driver_interface_version_show(
822 struct device_driver *drv,
823 char *buf)
825 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
828 static DRIVER_ATTR(interface_version, S_IRUGO,
829 tpacpi_driver_interface_version_show, NULL);
831 /* debug_level --------------------------------------------------------- */
832 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
833 char *buf)
835 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
838 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
839 const char *buf, size_t count)
841 unsigned long t;
843 if (parse_strtoul(buf, 0xffff, &t))
844 return -EINVAL;
846 dbg_level = t;
848 return count;
851 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
852 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
854 /* version ------------------------------------------------------------- */
855 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
856 char *buf)
858 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
859 TPACPI_DESC, TPACPI_VERSION);
862 static DRIVER_ATTR(version, S_IRUGO,
863 tpacpi_driver_version_show, NULL);
865 /* --------------------------------------------------------------------- */
867 static struct driver_attribute *tpacpi_driver_attributes[] = {
868 &driver_attr_debug_level, &driver_attr_version,
869 &driver_attr_interface_version,
872 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
874 int i, res;
876 i = 0;
877 res = 0;
878 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
879 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
880 i++;
883 return res;
886 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
888 int i;
890 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
891 driver_remove_file(drv, tpacpi_driver_attributes[i]);
894 /****************************************************************************
895 ****************************************************************************
897 * Subdrivers
899 ****************************************************************************
900 ****************************************************************************/
902 /*************************************************************************
903 * thinkpad-acpi init subdriver
906 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
908 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
909 printk(TPACPI_INFO "%s\n", TPACPI_URL);
911 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
912 (thinkpad_id.bios_version_str) ?
913 thinkpad_id.bios_version_str : "unknown",
914 (thinkpad_id.ec_version_str) ?
915 thinkpad_id.ec_version_str : "unknown");
917 if (thinkpad_id.vendor && thinkpad_id.model_str)
918 printk(TPACPI_INFO "%s %s\n",
919 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
920 "IBM" : ((thinkpad_id.vendor ==
921 PCI_VENDOR_ID_LENOVO) ?
922 "Lenovo" : "Unknown vendor"),
923 thinkpad_id.model_str);
925 return 0;
928 static int thinkpad_acpi_driver_read(char *p)
930 int len = 0;
932 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
933 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
935 return len;
938 static struct ibm_struct thinkpad_acpi_driver_data = {
939 .name = "driver",
940 .read = thinkpad_acpi_driver_read,
943 /*************************************************************************
944 * Hotkey subdriver
947 enum { /* hot key scan codes (derived from ACPI DSDT) */
948 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
949 TP_ACPI_HOTKEYSCAN_FNF2,
950 TP_ACPI_HOTKEYSCAN_FNF3,
951 TP_ACPI_HOTKEYSCAN_FNF4,
952 TP_ACPI_HOTKEYSCAN_FNF5,
953 TP_ACPI_HOTKEYSCAN_FNF6,
954 TP_ACPI_HOTKEYSCAN_FNF7,
955 TP_ACPI_HOTKEYSCAN_FNF8,
956 TP_ACPI_HOTKEYSCAN_FNF9,
957 TP_ACPI_HOTKEYSCAN_FNF10,
958 TP_ACPI_HOTKEYSCAN_FNF11,
959 TP_ACPI_HOTKEYSCAN_FNF12,
960 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
961 TP_ACPI_HOTKEYSCAN_FNINSERT,
962 TP_ACPI_HOTKEYSCAN_FNDELETE,
963 TP_ACPI_HOTKEYSCAN_FNHOME,
964 TP_ACPI_HOTKEYSCAN_FNEND,
965 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
966 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
967 TP_ACPI_HOTKEYSCAN_FNSPACE,
968 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
969 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
970 TP_ACPI_HOTKEYSCAN_MUTE,
971 TP_ACPI_HOTKEYSCAN_THINKPAD,
974 enum { /* Keys available through NVRAM polling */
975 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
976 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
979 enum { /* Positions of some of the keys in hotkey masks */
980 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
981 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
982 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
983 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
984 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
985 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
986 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
987 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
988 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
989 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
990 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
993 enum { /* NVRAM to ACPI HKEY group map */
994 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
995 TP_ACPI_HKEY_ZOOM_MASK |
996 TP_ACPI_HKEY_DISPSWTCH_MASK |
997 TP_ACPI_HKEY_HIBERNATE_MASK,
998 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
999 TP_ACPI_HKEY_BRGHTDWN_MASK,
1000 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1001 TP_ACPI_HKEY_VOLDWN_MASK |
1002 TP_ACPI_HKEY_MUTE_MASK,
1005 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1006 struct tp_nvram_state {
1007 u16 thinkpad_toggle:1;
1008 u16 zoom_toggle:1;
1009 u16 display_toggle:1;
1010 u16 thinklight_toggle:1;
1011 u16 hibernate_toggle:1;
1012 u16 displayexp_toggle:1;
1013 u16 display_state:1;
1014 u16 brightness_toggle:1;
1015 u16 volume_toggle:1;
1016 u16 mute:1;
1018 u8 brightness_level;
1019 u8 volume_level;
1022 static struct task_struct *tpacpi_hotkey_task;
1023 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1024 static int hotkey_poll_freq = 10; /* Hz */
1025 static struct mutex hotkey_thread_mutex;
1026 static struct mutex hotkey_thread_data_mutex;
1027 static unsigned int hotkey_config_change;
1029 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1031 #define hotkey_source_mask 0U
1033 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1035 static struct mutex hotkey_mutex;
1037 static enum { /* Reasons for waking up */
1038 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1039 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1040 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1041 } hotkey_wakeup_reason;
1043 static int hotkey_autosleep_ack;
1045 static int hotkey_orig_status;
1046 static u32 hotkey_orig_mask;
1047 static u32 hotkey_all_mask;
1048 static u32 hotkey_reserved_mask;
1049 static u32 hotkey_mask;
1051 static unsigned int hotkey_report_mode;
1053 static u16 *hotkey_keycode_map;
1055 static struct attribute_set *hotkey_dev_attributes;
1057 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1058 #define HOTKEY_CONFIG_CRITICAL_START \
1059 do { \
1060 mutex_lock(&hotkey_thread_data_mutex); \
1061 hotkey_config_change++; \
1062 } while (0);
1063 #define HOTKEY_CONFIG_CRITICAL_END \
1064 mutex_unlock(&hotkey_thread_data_mutex);
1065 #else
1066 #define HOTKEY_CONFIG_CRITICAL_START
1067 #define HOTKEY_CONFIG_CRITICAL_END
1068 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1070 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1071 =======
1072 /* HKEY.MHKG() return bits */
1073 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1075 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1076 static int hotkey_get_wlsw(int *status)
1078 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1079 return -EIO;
1080 return 0;
1083 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1084 =======
1085 static int hotkey_get_tablet_mode(int *status)
1087 int s;
1089 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1090 return -EIO;
1092 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1093 return 0;
1096 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1098 * Call with hotkey_mutex held
1100 static int hotkey_mask_get(void)
1102 u32 m = 0;
1104 if (tp_features.hotkey_mask) {
1105 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1106 return -EIO;
1108 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1110 return 0;
1114 * Call with hotkey_mutex held
1116 static int hotkey_mask_set(u32 mask)
1118 int i;
1119 int rc = 0;
1121 if (tp_features.hotkey_mask) {
1122 HOTKEY_CONFIG_CRITICAL_START
1123 for (i = 0; i < 32; i++) {
1124 u32 m = 1 << i;
1125 /* enable in firmware mask only keys not in NVRAM
1126 * mode, but enable the key in the cached hotkey_mask
1127 * regardless of mode, or the key will end up
1128 * disabled by hotkey_mask_get() */
1129 if (!acpi_evalf(hkey_handle,
1130 NULL, "MHKM", "vdd", i + 1,
1131 !!((mask & ~hotkey_source_mask) & m))) {
1132 rc = -EIO;
1133 break;
1134 } else {
1135 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1138 HOTKEY_CONFIG_CRITICAL_END
1140 /* hotkey_mask_get must be called unconditionally below */
1141 if (!hotkey_mask_get() && !rc &&
1142 (hotkey_mask & ~hotkey_source_mask) !=
1143 (mask & ~hotkey_source_mask)) {
1144 printk(TPACPI_NOTICE
1145 "requested hot key mask 0x%08x, but "
1146 "firmware forced it to 0x%08x\n",
1147 mask, hotkey_mask);
1149 } else {
1150 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1151 HOTKEY_CONFIG_CRITICAL_START
1152 hotkey_mask = mask & hotkey_source_mask;
1153 HOTKEY_CONFIG_CRITICAL_END
1154 hotkey_mask_get();
1155 if (hotkey_mask != mask) {
1156 printk(TPACPI_NOTICE
1157 "requested hot key mask 0x%08x, "
1158 "forced to 0x%08x (NVRAM poll mask is "
1159 "0x%08x): no firmware mask support\n",
1160 mask, hotkey_mask, hotkey_source_mask);
1162 #else
1163 hotkey_mask_get();
1164 rc = -ENXIO;
1165 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1168 return rc;
1171 static int hotkey_status_get(int *status)
1173 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1174 return -EIO;
1176 return 0;
1179 static int hotkey_status_set(int status)
1181 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1182 return -EIO;
1184 return 0;
1187 static void tpacpi_input_send_radiosw(void)
1189 int wlsw;
1191 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1192 mutex_lock(&tpacpi_inputdev_send_mutex);
1194 =======
1195 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1196 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1197 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1198 =======
1199 mutex_lock(&tpacpi_inputdev_send_mutex);
1201 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1202 input_report_switch(tpacpi_inputdev,
1203 SW_RADIO, !!wlsw);
1204 input_sync(tpacpi_inputdev);
1205 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1206 =======
1208 mutex_unlock(&tpacpi_inputdev_send_mutex);
1209 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1211 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1212 =======
1215 static void tpacpi_input_send_tabletsw(void)
1217 int state;
1219 if (tp_features.hotkey_tablet &&
1220 !hotkey_get_tablet_mode(&state)) {
1221 mutex_lock(&tpacpi_inputdev_send_mutex);
1222 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1224 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1225 mutex_unlock(&tpacpi_inputdev_send_mutex);
1226 =======
1227 input_report_switch(tpacpi_inputdev,
1228 SW_TABLET_MODE, !!state);
1229 input_sync(tpacpi_inputdev);
1231 mutex_unlock(&tpacpi_inputdev_send_mutex);
1233 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1236 static void tpacpi_input_send_key(unsigned int scancode)
1238 unsigned int keycode;
1240 keycode = hotkey_keycode_map[scancode];
1242 if (keycode != KEY_RESERVED) {
1243 mutex_lock(&tpacpi_inputdev_send_mutex);
1245 input_report_key(tpacpi_inputdev, keycode, 1);
1246 if (keycode == KEY_UNKNOWN)
1247 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1248 scancode);
1249 input_sync(tpacpi_inputdev);
1251 input_report_key(tpacpi_inputdev, keycode, 0);
1252 if (keycode == KEY_UNKNOWN)
1253 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1254 scancode);
1255 input_sync(tpacpi_inputdev);
1257 mutex_unlock(&tpacpi_inputdev_send_mutex);
1261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1262 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1264 static void tpacpi_hotkey_send_key(unsigned int scancode)
1266 tpacpi_input_send_key(scancode);
1267 if (hotkey_report_mode < 2) {
1268 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1269 0x80, 0x1001 + scancode);
1273 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1275 u8 d;
1277 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1278 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1279 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1280 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1281 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1282 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1284 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1285 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1286 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1288 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1289 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1290 n->displayexp_toggle =
1291 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1293 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1294 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1295 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1296 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1297 n->brightness_toggle =
1298 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1300 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1301 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1302 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1303 >> TP_NVRAM_POS_LEVEL_VOLUME;
1304 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1305 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1309 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1310 do { \
1311 if ((mask & (1 << __scancode)) && \
1312 oldn->__member != newn->__member) \
1313 tpacpi_hotkey_send_key(__scancode); \
1314 } while (0)
1316 #define TPACPI_MAY_SEND_KEY(__scancode) \
1317 do { if (mask & (1 << __scancode)) \
1318 tpacpi_hotkey_send_key(__scancode); } while (0)
1320 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1321 struct tp_nvram_state *newn,
1322 u32 mask)
1324 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1325 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1326 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1327 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1329 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1331 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1333 /* handle volume */
1334 if (oldn->volume_toggle != newn->volume_toggle) {
1335 if (oldn->mute != newn->mute) {
1336 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1338 if (oldn->volume_level > newn->volume_level) {
1339 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1340 } else if (oldn->volume_level < newn->volume_level) {
1341 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1342 } else if (oldn->mute == newn->mute) {
1343 /* repeated key presses that didn't change state */
1344 if (newn->mute) {
1345 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1346 } else if (newn->volume_level != 0) {
1347 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1348 } else {
1349 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1354 /* handle brightness */
1355 if (oldn->brightness_toggle != newn->brightness_toggle) {
1356 if (oldn->brightness_level < newn->brightness_level) {
1357 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1358 } else if (oldn->brightness_level > newn->brightness_level) {
1359 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1360 } else {
1361 /* repeated key presses that didn't change state */
1362 if (newn->brightness_level != 0) {
1363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1364 } else {
1365 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1371 #undef TPACPI_COMPARE_KEY
1372 #undef TPACPI_MAY_SEND_KEY
1374 static int hotkey_kthread(void *data)
1376 struct tp_nvram_state s[2];
1377 u32 mask;
1378 unsigned int si, so;
1379 unsigned long t;
1380 unsigned int change_detector, must_reset;
1382 mutex_lock(&hotkey_thread_mutex);
1384 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1385 goto exit;
1387 set_freezable();
1389 so = 0;
1390 si = 1;
1391 t = 0;
1393 /* Initial state for compares */
1394 mutex_lock(&hotkey_thread_data_mutex);
1395 change_detector = hotkey_config_change;
1396 mask = hotkey_source_mask & hotkey_mask;
1397 mutex_unlock(&hotkey_thread_data_mutex);
1398 hotkey_read_nvram(&s[so], mask);
1400 while (!kthread_should_stop() && hotkey_poll_freq) {
1401 if (t == 0)
1402 t = 1000/hotkey_poll_freq;
1403 t = msleep_interruptible(t);
1404 if (unlikely(kthread_should_stop()))
1405 break;
1406 must_reset = try_to_freeze();
1407 if (t > 0 && !must_reset)
1408 continue;
1410 mutex_lock(&hotkey_thread_data_mutex);
1411 if (must_reset || hotkey_config_change != change_detector) {
1412 /* forget old state on thaw or config change */
1413 si = so;
1414 t = 0;
1415 change_detector = hotkey_config_change;
1417 mask = hotkey_source_mask & hotkey_mask;
1418 mutex_unlock(&hotkey_thread_data_mutex);
1420 if (likely(mask)) {
1421 hotkey_read_nvram(&s[si], mask);
1422 if (likely(si != so)) {
1423 hotkey_compare_and_issue_event(&s[so], &s[si],
1424 mask);
1428 so = si;
1429 si ^= 1;
1432 exit:
1433 mutex_unlock(&hotkey_thread_mutex);
1434 return 0;
1437 static void hotkey_poll_stop_sync(void)
1439 if (tpacpi_hotkey_task) {
1440 if (frozen(tpacpi_hotkey_task) ||
1441 freezing(tpacpi_hotkey_task))
1442 thaw_process(tpacpi_hotkey_task);
1444 kthread_stop(tpacpi_hotkey_task);
1445 tpacpi_hotkey_task = NULL;
1446 mutex_lock(&hotkey_thread_mutex);
1447 /* at this point, the thread did exit */
1448 mutex_unlock(&hotkey_thread_mutex);
1452 /* call with hotkey_mutex held */
1453 static void hotkey_poll_setup(int may_warn)
1455 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1456 hotkey_poll_freq > 0 &&
1457 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1458 if (!tpacpi_hotkey_task) {
1459 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1460 NULL,
1461 TPACPI_FILE "d");
1462 if (IS_ERR(tpacpi_hotkey_task)) {
1463 tpacpi_hotkey_task = NULL;
1464 printk(TPACPI_ERR
1465 "could not create kernel thread "
1466 "for hotkey polling\n");
1469 } else {
1470 hotkey_poll_stop_sync();
1471 if (may_warn &&
1472 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1473 printk(TPACPI_NOTICE
1474 "hot keys 0x%08x require polling, "
1475 "which is currently disabled\n",
1476 hotkey_source_mask);
1481 static void hotkey_poll_setup_safe(int may_warn)
1483 mutex_lock(&hotkey_mutex);
1484 hotkey_poll_setup(may_warn);
1485 mutex_unlock(&hotkey_mutex);
1488 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1489 =======
1490 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1492 static void hotkey_poll_setup_safe(int __unused)
1496 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1498 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1499 static int hotkey_inputdev_open(struct input_dev *dev)
1501 switch (tpacpi_lifecycle) {
1502 case TPACPI_LIFE_INIT:
1504 * hotkey_init will call hotkey_poll_setup_safe
1505 * at the appropriate moment
1507 return 0;
1508 case TPACPI_LIFE_EXITING:
1509 return -EBUSY;
1510 case TPACPI_LIFE_RUNNING:
1511 hotkey_poll_setup_safe(0);
1512 return 0;
1515 /* Should only happen if tpacpi_lifecycle is corrupt */
1516 BUG();
1517 return -EBUSY;
1520 static void hotkey_inputdev_close(struct input_dev *dev)
1522 /* disable hotkey polling when possible */
1523 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1524 hotkey_poll_setup_safe(0);
1526 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1527 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1528 =======
1529 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1531 /* sysfs hotkey enable ------------------------------------------------- */
1532 static ssize_t hotkey_enable_show(struct device *dev,
1533 struct device_attribute *attr,
1534 char *buf)
1536 int res, status;
1538 res = hotkey_status_get(&status);
1539 if (res)
1540 return res;
1542 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1545 static ssize_t hotkey_enable_store(struct device *dev,
1546 struct device_attribute *attr,
1547 const char *buf, size_t count)
1549 unsigned long t;
1550 int res;
1552 if (parse_strtoul(buf, 1, &t))
1553 return -EINVAL;
1555 res = hotkey_status_set(t);
1557 return (res) ? res : count;
1560 static struct device_attribute dev_attr_hotkey_enable =
1561 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1562 hotkey_enable_show, hotkey_enable_store);
1564 /* sysfs hotkey mask --------------------------------------------------- */
1565 static ssize_t hotkey_mask_show(struct device *dev,
1566 struct device_attribute *attr,
1567 char *buf)
1569 int res;
1571 if (mutex_lock_interruptible(&hotkey_mutex))
1572 return -ERESTARTSYS;
1573 res = hotkey_mask_get();
1574 mutex_unlock(&hotkey_mutex);
1576 return (res)?
1577 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1580 static ssize_t hotkey_mask_store(struct device *dev,
1581 struct device_attribute *attr,
1582 const char *buf, size_t count)
1584 unsigned long t;
1585 int res;
1587 if (parse_strtoul(buf, 0xffffffffUL, &t))
1588 return -EINVAL;
1590 if (mutex_lock_interruptible(&hotkey_mutex))
1591 return -ERESTARTSYS;
1593 res = hotkey_mask_set(t);
1595 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1596 hotkey_poll_setup(1);
1597 #endif
1599 mutex_unlock(&hotkey_mutex);
1601 return (res) ? res : count;
1604 static struct device_attribute dev_attr_hotkey_mask =
1605 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1606 hotkey_mask_show, hotkey_mask_store);
1608 /* sysfs hotkey bios_enabled ------------------------------------------- */
1609 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1610 struct device_attribute *attr,
1611 char *buf)
1613 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1616 static struct device_attribute dev_attr_hotkey_bios_enabled =
1617 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1619 /* sysfs hotkey bios_mask ---------------------------------------------- */
1620 static ssize_t hotkey_bios_mask_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1624 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1627 static struct device_attribute dev_attr_hotkey_bios_mask =
1628 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1630 /* sysfs hotkey all_mask ----------------------------------------------- */
1631 static ssize_t hotkey_all_mask_show(struct device *dev,
1632 struct device_attribute *attr,
1633 char *buf)
1635 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1636 hotkey_all_mask | hotkey_source_mask);
1639 static struct device_attribute dev_attr_hotkey_all_mask =
1640 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1642 /* sysfs hotkey recommended_mask --------------------------------------- */
1643 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1644 struct device_attribute *attr,
1645 char *buf)
1647 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1648 (hotkey_all_mask | hotkey_source_mask)
1649 & ~hotkey_reserved_mask);
1652 static struct device_attribute dev_attr_hotkey_recommended_mask =
1653 __ATTR(hotkey_recommended_mask, S_IRUGO,
1654 hotkey_recommended_mask_show, NULL);
1656 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1658 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1659 static ssize_t hotkey_source_mask_show(struct device *dev,
1660 struct device_attribute *attr,
1661 char *buf)
1663 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1666 static ssize_t hotkey_source_mask_store(struct device *dev,
1667 struct device_attribute *attr,
1668 const char *buf, size_t count)
1670 unsigned long t;
1672 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1673 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1674 return -EINVAL;
1676 if (mutex_lock_interruptible(&hotkey_mutex))
1677 return -ERESTARTSYS;
1679 HOTKEY_CONFIG_CRITICAL_START
1680 hotkey_source_mask = t;
1681 HOTKEY_CONFIG_CRITICAL_END
1683 hotkey_poll_setup(1);
1685 mutex_unlock(&hotkey_mutex);
1687 return count;
1690 static struct device_attribute dev_attr_hotkey_source_mask =
1691 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1692 hotkey_source_mask_show, hotkey_source_mask_store);
1694 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1695 static ssize_t hotkey_poll_freq_show(struct device *dev,
1696 struct device_attribute *attr,
1697 char *buf)
1699 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1702 static ssize_t hotkey_poll_freq_store(struct device *dev,
1703 struct device_attribute *attr,
1704 const char *buf, size_t count)
1706 unsigned long t;
1708 if (parse_strtoul(buf, 25, &t))
1709 return -EINVAL;
1711 if (mutex_lock_interruptible(&hotkey_mutex))
1712 return -ERESTARTSYS;
1714 hotkey_poll_freq = t;
1716 hotkey_poll_setup(1);
1717 mutex_unlock(&hotkey_mutex);
1719 return count;
1722 static struct device_attribute dev_attr_hotkey_poll_freq =
1723 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1724 hotkey_poll_freq_show, hotkey_poll_freq_store);
1726 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1728 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1729 static ssize_t hotkey_radio_sw_show(struct device *dev,
1730 struct device_attribute *attr,
1731 char *buf)
1733 int res, s;
1734 res = hotkey_get_wlsw(&s);
1735 if (res < 0)
1736 return res;
1738 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1741 static struct device_attribute dev_attr_hotkey_radio_sw =
1742 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1744 static void hotkey_radio_sw_notify_change(void)
1746 if (tp_features.hotkey_wlsw)
1747 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1748 "hotkey_radio_sw");
1751 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1752 =======
1753 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1754 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1755 struct device_attribute *attr,
1756 char *buf)
1758 int res, s;
1759 res = hotkey_get_tablet_mode(&s);
1760 if (res < 0)
1761 return res;
1763 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1766 static struct device_attribute dev_attr_hotkey_tablet_mode =
1767 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1769 static void hotkey_tablet_mode_notify_change(void)
1771 if (tp_features.hotkey_tablet)
1772 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1773 "hotkey_tablet_mode");
1776 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1777 /* sysfs hotkey report_mode -------------------------------------------- */
1778 static ssize_t hotkey_report_mode_show(struct device *dev,
1779 struct device_attribute *attr,
1780 char *buf)
1782 return snprintf(buf, PAGE_SIZE, "%d\n",
1783 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1786 static struct device_attribute dev_attr_hotkey_report_mode =
1787 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1789 /* sysfs wakeup reason (pollable) -------------------------------------- */
1790 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1791 struct device_attribute *attr,
1792 char *buf)
1794 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1797 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1798 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1800 static void hotkey_wakeup_reason_notify_change(void)
1802 if (tp_features.hotkey_mask)
1803 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1804 "wakeup_reason");
1807 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1808 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1809 struct device_attribute *attr,
1810 char *buf)
1812 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1815 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1816 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1817 hotkey_wakeup_hotunplug_complete_show, NULL);
1819 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1821 if (tp_features.hotkey_mask)
1822 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1823 "wakeup_hotunplug_complete");
1826 /* --------------------------------------------------------------------- */
1828 static struct attribute *hotkey_attributes[] __initdata = {
1829 &dev_attr_hotkey_enable.attr,
1830 &dev_attr_hotkey_bios_enabled.attr,
1831 &dev_attr_hotkey_report_mode.attr,
1832 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1833 &dev_attr_hotkey_mask.attr,
1834 &dev_attr_hotkey_all_mask.attr,
1835 &dev_attr_hotkey_recommended_mask.attr,
1836 &dev_attr_hotkey_source_mask.attr,
1837 &dev_attr_hotkey_poll_freq.attr,
1838 #endif
1841 static struct attribute *hotkey_mask_attributes[] __initdata = {
1842 &dev_attr_hotkey_bios_mask.attr,
1843 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1844 &dev_attr_hotkey_mask.attr,
1845 &dev_attr_hotkey_all_mask.attr,
1846 &dev_attr_hotkey_recommended_mask.attr,
1847 #endif
1848 &dev_attr_hotkey_wakeup_reason.attr,
1849 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1852 static int __init hotkey_init(struct ibm_init_struct *iibm)
1854 /* Requirements for changing the default keymaps:
1856 * 1. Many of the keys are mapped to KEY_RESERVED for very
1857 * good reasons. Do not change them unless you have deep
1858 * knowledge on the IBM and Lenovo ThinkPad firmware for
1859 * the various ThinkPad models. The driver behaves
1860 * differently for KEY_RESERVED: such keys have their
1861 * hot key mask *unset* in mask_recommended, and also
1862 * in the initial hot key mask programmed into the
1863 * firmware at driver load time, which means the firm-
1864 * ware may react very differently if you change them to
1865 * something else;
1867 * 2. You must be subscribed to the linux-thinkpad and
1868 * ibm-acpi-devel mailing lists, and you should read the
1869 * list archives since 2007 if you want to change the
1870 * keymaps. This requirement exists so that you will
1871 * know the past history of problems with the thinkpad-
1872 * acpi driver keymaps, and also that you will be
1873 * listening to any bug reports;
1875 * 3. Do not send thinkpad-acpi specific patches directly to
1876 * for merging, *ever*. Send them to the linux-acpi
1877 * mailinglist for comments. Merging is to be done only
1878 * through acpi-test and the ACPI maintainer.
1880 * If the above is too much to ask, don't change the keymap.
1881 * Ask the thinkpad-acpi maintainer to do it, instead.
1883 static u16 ibm_keycode_map[] __initdata = {
1884 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1885 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1886 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1887 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1889 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1890 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1891 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1892 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1894 /* brightness: firmware always reacts to them, unless
1895 * X.org did some tricks in the radeon BIOS scratch
1896 * registers of *some* models */
1897 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1898 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1900 /* Thinklight: firmware always react to it */
1901 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1903 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1904 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1906 /* Volume: firmware always react to it and reprograms
1907 * the built-in *extra* mixer. Never map it to control
1908 * another mixer by default. */
1909 KEY_RESERVED, /* 0x14: VOLUME UP */
1910 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1911 KEY_RESERVED, /* 0x16: MUTE */
1913 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1915 /* (assignments unknown, please report if found) */
1916 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1917 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1919 static u16 lenovo_keycode_map[] __initdata = {
1920 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1921 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1922 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1923 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1925 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1926 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1927 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1928 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1930 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1931 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1933 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1935 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1936 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1938 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1939 * react to it and reprograms the built-in *extra* mixer.
1940 * Never map it to control another mixer by default.
1942 * T60?, T61, R60?, R61: firmware and EC tries to send
1943 * these over the regular keyboard, so these are no-ops,
1944 * but there are still weird bugs re. MUTE, so do not
1945 * change unless you get test reports from all Lenovo
1946 * models. May cause the BIOS to interfere with the
1947 * HDA mixer.
1949 KEY_RESERVED, /* 0x14: VOLUME UP */
1950 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1951 KEY_RESERVED, /* 0x16: MUTE */
1953 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1955 /* (assignments unknown, please report if found) */
1956 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1957 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1960 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1961 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1962 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1964 int res, i;
1965 int status;
1966 int hkeyv;
1968 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1970 BUG_ON(!tpacpi_inputdev);
1971 BUG_ON(tpacpi_inputdev->open != NULL ||
1972 tpacpi_inputdev->close != NULL);
1974 TPACPI_ACPIHANDLE_INIT(hkey);
1975 mutex_init(&hotkey_mutex);
1977 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1978 mutex_init(&hotkey_thread_mutex);
1979 mutex_init(&hotkey_thread_data_mutex);
1980 #endif
1982 /* hotkey not supported on 570 */
1983 tp_features.hotkey = hkey_handle != NULL;
1985 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1986 str_supported(tp_features.hotkey));
1988 if (tp_features.hotkey) {
1989 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
1990 hotkey_dev_attributes = create_attr_set(12, NULL);
1991 =======
1992 hotkey_dev_attributes = create_attr_set(13, NULL);
1993 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
1994 if (!hotkey_dev_attributes)
1995 return -ENOMEM;
1996 res = add_many_to_attr_set(hotkey_dev_attributes,
1997 hotkey_attributes,
1998 ARRAY_SIZE(hotkey_attributes));
1999 if (res)
2000 return res;
2002 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2003 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2004 for HKEY interface version 0x100 */
2005 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2006 if ((hkeyv >> 8) != 1) {
2007 printk(TPACPI_ERR "unknown version of the "
2008 "HKEY interface: 0x%x\n", hkeyv);
2009 printk(TPACPI_ERR "please report this to %s\n",
2010 TPACPI_MAIL);
2011 } else {
2013 * MHKV 0x100 in A31, R40, R40e,
2014 * T4x, X31, and later
2016 tp_features.hotkey_mask = 1;
2020 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2021 str_supported(tp_features.hotkey_mask));
2023 if (tp_features.hotkey_mask) {
2024 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2025 "MHKA", "qd")) {
2026 printk(TPACPI_ERR
2027 "missing MHKA handler, "
2028 "please report this to %s\n",
2029 TPACPI_MAIL);
2030 /* FN+F12, FN+F4, FN+F3 */
2031 hotkey_all_mask = 0x080cU;
2035 /* hotkey_source_mask *must* be zero for
2036 * the first hotkey_mask_get */
2037 res = hotkey_status_get(&hotkey_orig_status);
2038 if (!res && tp_features.hotkey_mask) {
2039 res = hotkey_mask_get();
2040 hotkey_orig_mask = hotkey_mask;
2041 if (!res) {
2042 res = add_many_to_attr_set(
2043 hotkey_dev_attributes,
2044 hotkey_mask_attributes,
2045 ARRAY_SIZE(hotkey_mask_attributes));
2049 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2050 if (tp_features.hotkey_mask) {
2051 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2052 & ~hotkey_all_mask;
2053 } else {
2054 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2057 vdbg_printk(TPACPI_DBG_INIT,
2058 "hotkey source mask 0x%08x, polling freq %d\n",
2059 hotkey_source_mask, hotkey_poll_freq);
2060 #endif
2062 /* Not all thinkpads have a hardware radio switch */
2063 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2064 tp_features.hotkey_wlsw = 1;
2065 printk(TPACPI_INFO
2066 "radio switch found; radios are %s\n",
2067 enabled(status, 0));
2068 res = add_to_attr_set(hotkey_dev_attributes,
2069 &dev_attr_hotkey_radio_sw.attr);
2072 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2073 =======
2074 /* For X41t, X60t, X61t Tablets... */
2075 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2076 tp_features.hotkey_tablet = 1;
2077 printk(TPACPI_INFO
2078 "possible tablet mode switch found; "
2079 "ThinkPad in %s mode\n",
2080 (status & TP_HOTKEY_TABLET_MASK)?
2081 "tablet" : "laptop");
2082 res = add_to_attr_set(hotkey_dev_attributes,
2083 &dev_attr_hotkey_tablet_mode.attr);
2086 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2087 if (!res)
2088 res = register_attr_set_with_sysfs(
2089 hotkey_dev_attributes,
2090 &tpacpi_pdev->dev.kobj);
2091 if (res)
2092 return res;
2094 /* Set up key map */
2096 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2097 GFP_KERNEL);
2098 if (!hotkey_keycode_map) {
2099 printk(TPACPI_ERR
2100 "failed to allocate memory for key map\n");
2101 return -ENOMEM;
2104 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2105 dbg_printk(TPACPI_DBG_INIT,
2106 "using Lenovo default hot key map\n");
2107 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2108 TPACPI_HOTKEY_MAP_SIZE);
2109 } else {
2110 dbg_printk(TPACPI_DBG_INIT,
2111 "using IBM default hot key map\n");
2112 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2113 TPACPI_HOTKEY_MAP_SIZE);
2116 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2117 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2118 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2119 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2120 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2121 tpacpi_inputdev->keycode = hotkey_keycode_map;
2122 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2123 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2124 set_bit(hotkey_keycode_map[i],
2125 tpacpi_inputdev->keybit);
2126 } else {
2127 if (i < sizeof(hotkey_reserved_mask)*8)
2128 hotkey_reserved_mask |= 1 << i;
2132 if (tp_features.hotkey_wlsw) {
2133 set_bit(EV_SW, tpacpi_inputdev->evbit);
2134 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2136 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2137 =======
2138 if (tp_features.hotkey_tablet) {
2139 set_bit(EV_SW, tpacpi_inputdev->evbit);
2140 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2142 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2144 dbg_printk(TPACPI_DBG_INIT,
2145 "enabling hot key handling\n");
2146 res = hotkey_status_set(1);
2147 if (res)
2148 return res;
2149 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2150 & ~hotkey_reserved_mask)
2151 | hotkey_orig_mask);
2152 if (res < 0 && res != -ENXIO)
2153 return res;
2155 dbg_printk(TPACPI_DBG_INIT,
2156 "legacy hot key reporting over procfs %s\n",
2157 (hotkey_report_mode < 2) ?
2158 "enabled" : "disabled");
2160 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2161 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2162 =======
2163 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2164 tpacpi_inputdev->open = &hotkey_inputdev_open;
2165 tpacpi_inputdev->close = &hotkey_inputdev_close;
2167 hotkey_poll_setup_safe(1);
2168 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2169 #endif
2170 =======
2171 tpacpi_input_send_radiosw();
2172 tpacpi_input_send_tabletsw();
2173 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2176 return (tp_features.hotkey)? 0 : 1;
2179 static void hotkey_exit(void)
2181 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2182 hotkey_poll_stop_sync();
2183 #endif
2185 if (tp_features.hotkey) {
2186 dbg_printk(TPACPI_DBG_EXIT,
2187 "restoring original hot key mask\n");
2188 /* no short-circuit boolean operator below! */
2189 if ((hotkey_mask_set(hotkey_orig_mask) |
2190 hotkey_status_set(hotkey_orig_status)) != 0)
2191 printk(TPACPI_ERR
2192 "failed to restore hot key mask "
2193 "to BIOS defaults\n");
2196 if (hotkey_dev_attributes) {
2197 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2198 hotkey_dev_attributes = NULL;
2202 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2204 u32 hkey;
2205 unsigned int scancode;
2206 int send_acpi_ev;
2207 int ignore_acpi_ev;
2208 int unk_ev;
2210 if (event != 0x80) {
2211 printk(TPACPI_ERR
2212 "unknown HKEY notification event %d\n", event);
2213 /* forward it to userspace, maybe it knows how to handle it */
2214 acpi_bus_generate_netlink_event(
2215 ibm->acpi->device->pnp.device_class,
2216 ibm->acpi->device->dev.bus_id,
2217 event, 0);
2218 return;
2221 while (1) {
2222 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2223 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2224 return;
2227 if (hkey == 0) {
2228 /* queue empty */
2229 return;
2232 send_acpi_ev = 1;
2233 ignore_acpi_ev = 0;
2234 unk_ev = 0;
2236 switch (hkey >> 12) {
2237 case 1:
2238 /* 0x1000-0x1FFF: key presses */
2239 scancode = hkey & 0xfff;
2240 if (scancode > 0 && scancode < 0x21) {
2241 scancode--;
2242 if (!(hotkey_source_mask & (1 << scancode))) {
2243 tpacpi_input_send_key(scancode);
2244 send_acpi_ev = 0;
2245 } else {
2246 ignore_acpi_ev = 1;
2248 } else {
2249 unk_ev = 1;
2251 break;
2252 case 2:
2253 /* Wakeup reason */
2254 switch (hkey) {
2255 case 0x2304: /* suspend, undock */
2256 case 0x2404: /* hibernation, undock */
2257 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2258 ignore_acpi_ev = 1;
2259 break;
2260 case 0x2305: /* suspend, bay eject */
2261 case 0x2405: /* hibernation, bay eject */
2262 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2263 ignore_acpi_ev = 1;
2264 break;
2265 default:
2266 unk_ev = 1;
2268 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2269 printk(TPACPI_INFO
2270 "woke up due to a hot-unplug "
2271 "request...\n");
2272 hotkey_wakeup_reason_notify_change();
2274 break;
2275 case 3:
2276 /* bay-related wakeups */
2277 if (hkey == 0x3003) {
2278 hotkey_autosleep_ack = 1;
2279 printk(TPACPI_INFO
2280 "bay ejected\n");
2281 hotkey_wakeup_hotunplug_complete_notify_change();
2282 } else {
2283 unk_ev = 1;
2285 break;
2286 case 4:
2287 /* dock-related wakeups */
2288 if (hkey == 0x4003) {
2289 hotkey_autosleep_ack = 1;
2290 printk(TPACPI_INFO
2291 "undocked\n");
2292 hotkey_wakeup_hotunplug_complete_notify_change();
2293 } else {
2294 unk_ev = 1;
2296 break;
2297 case 5:
2298 /* 0x5000-0x5FFF: human interface helpers */
2299 switch (hkey) {
2300 case 0x5010: /* Lenovo new BIOS: brightness changed */
2301 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2302 case 0x5009: /* X61t: swivel up (tablet mode) */
2303 case 0x500a: /* X61t: swivel down (normal mode) */
2304 =======
2305 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2306 case 0x500b: /* X61t: tablet pen inserted into bay */
2307 case 0x500c: /* X61t: tablet pen removed from bay */
2308 break;
2309 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2310 =======
2311 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2312 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2313 tpacpi_input_send_tabletsw();
2314 hotkey_tablet_mode_notify_change();
2315 send_acpi_ev = 0;
2316 break;
2317 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2318 case 0x5001:
2319 case 0x5002:
2320 /* LID switch events. Do not propagate */
2321 ignore_acpi_ev = 1;
2322 break;
2323 default:
2324 unk_ev = 1;
2326 break;
2327 case 7:
2328 /* 0x7000-0x7FFF: misc */
2329 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2330 tpacpi_input_send_radiosw();
2331 hotkey_radio_sw_notify_change();
2332 send_acpi_ev = 0;
2333 break;
2335 /* fallthrough to default */
2336 default:
2337 unk_ev = 1;
2339 if (unk_ev) {
2340 printk(TPACPI_NOTICE
2341 "unhandled HKEY event 0x%04x\n", hkey);
2344 /* Legacy events */
2345 if (!ignore_acpi_ev &&
2346 (send_acpi_ev || hotkey_report_mode < 2)) {
2347 acpi_bus_generate_proc_event(ibm->acpi->device,
2348 event, hkey);
2351 /* netlink events */
2352 if (!ignore_acpi_ev && send_acpi_ev) {
2353 acpi_bus_generate_netlink_event(
2354 ibm->acpi->device->pnp.device_class,
2355 ibm->acpi->device->dev.bus_id,
2356 event, hkey);
2361 static void hotkey_suspend(pm_message_t state)
2363 /* Do these on suspend, we get the events on early resume! */
2364 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2365 hotkey_autosleep_ack = 0;
2368 static void hotkey_resume(void)
2370 if (hotkey_mask_get())
2371 printk(TPACPI_ERR
2372 "error while trying to read hot key mask "
2373 "from firmware\n");
2374 tpacpi_input_send_radiosw();
2375 hotkey_radio_sw_notify_change();
2376 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2377 =======
2378 hotkey_tablet_mode_notify_change();
2379 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2380 hotkey_wakeup_reason_notify_change();
2381 hotkey_wakeup_hotunplug_complete_notify_change();
2382 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2383 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2384 =======
2385 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2386 hotkey_poll_setup_safe(0);
2387 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2388 #endif
2389 =======
2390 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2393 /* procfs -------------------------------------------------------------- */
2394 static int hotkey_read(char *p)
2396 int res, status;
2397 int len = 0;
2399 if (!tp_features.hotkey) {
2400 len += sprintf(p + len, "status:\t\tnot supported\n");
2401 return len;
2404 if (mutex_lock_interruptible(&hotkey_mutex))
2405 return -ERESTARTSYS;
2406 res = hotkey_status_get(&status);
2407 if (!res)
2408 res = hotkey_mask_get();
2409 mutex_unlock(&hotkey_mutex);
2410 if (res)
2411 return res;
2413 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2414 if (tp_features.hotkey_mask) {
2415 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2416 len += sprintf(p + len,
2417 "commands:\tenable, disable, reset, <mask>\n");
2418 } else {
2419 len += sprintf(p + len, "mask:\t\tnot supported\n");
2420 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2423 return len;
2426 static int hotkey_write(char *buf)
2428 int res, status;
2429 u32 mask;
2430 char *cmd;
2432 if (!tp_features.hotkey)
2433 return -ENODEV;
2435 if (mutex_lock_interruptible(&hotkey_mutex))
2436 return -ERESTARTSYS;
2438 status = -1;
2439 mask = hotkey_mask;
2441 res = 0;
2442 while ((cmd = next_cmd(&buf))) {
2443 if (strlencmp(cmd, "enable") == 0) {
2444 status = 1;
2445 } else if (strlencmp(cmd, "disable") == 0) {
2446 status = 0;
2447 } else if (strlencmp(cmd, "reset") == 0) {
2448 status = hotkey_orig_status;
2449 mask = hotkey_orig_mask;
2450 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2451 /* mask set */
2452 } else if (sscanf(cmd, "%x", &mask) == 1) {
2453 /* mask set */
2454 } else {
2455 res = -EINVAL;
2456 goto errexit;
2459 if (status != -1)
2460 res = hotkey_status_set(status);
2462 if (!res && mask != hotkey_mask)
2463 res = hotkey_mask_set(mask);
2465 errexit:
2466 mutex_unlock(&hotkey_mutex);
2467 return res;
2470 static const struct acpi_device_id ibm_htk_device_ids[] = {
2471 {TPACPI_ACPI_HKEY_HID, 0},
2472 {"", 0},
2475 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2476 .hid = ibm_htk_device_ids,
2477 .notify = hotkey_notify,
2478 .handle = &hkey_handle,
2479 .type = ACPI_DEVICE_NOTIFY,
2482 static struct ibm_struct hotkey_driver_data = {
2483 .name = "hotkey",
2484 .read = hotkey_read,
2485 .write = hotkey_write,
2486 .exit = hotkey_exit,
2487 .resume = hotkey_resume,
2488 .suspend = hotkey_suspend,
2489 .acpi = &ibm_hotkey_acpidriver,
2492 /*************************************************************************
2493 * Bluetooth subdriver
2496 enum {
2497 /* ACPI GBDC/SBDC bits */
2498 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2499 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2500 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2503 static int bluetooth_get_radiosw(void);
2504 static int bluetooth_set_radiosw(int radio_on);
2506 /* sysfs bluetooth enable ---------------------------------------------- */
2507 static ssize_t bluetooth_enable_show(struct device *dev,
2508 struct device_attribute *attr,
2509 char *buf)
2511 int status;
2513 status = bluetooth_get_radiosw();
2514 if (status < 0)
2515 return status;
2517 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2520 static ssize_t bluetooth_enable_store(struct device *dev,
2521 struct device_attribute *attr,
2522 const char *buf, size_t count)
2524 unsigned long t;
2525 int res;
2527 if (parse_strtoul(buf, 1, &t))
2528 return -EINVAL;
2530 res = bluetooth_set_radiosw(t);
2532 return (res) ? res : count;
2535 static struct device_attribute dev_attr_bluetooth_enable =
2536 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2537 bluetooth_enable_show, bluetooth_enable_store);
2539 /* --------------------------------------------------------------------- */
2541 static struct attribute *bluetooth_attributes[] = {
2542 &dev_attr_bluetooth_enable.attr,
2543 NULL
2546 static const struct attribute_group bluetooth_attr_group = {
2547 .attrs = bluetooth_attributes,
2550 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2552 int res;
2553 int status = 0;
2555 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2557 TPACPI_ACPIHANDLE_INIT(hkey);
2559 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2560 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2561 tp_features.bluetooth = hkey_handle &&
2562 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2564 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2565 str_supported(tp_features.bluetooth),
2566 status);
2568 if (tp_features.bluetooth) {
2569 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2570 /* no bluetooth hardware present in system */
2571 tp_features.bluetooth = 0;
2572 dbg_printk(TPACPI_DBG_INIT,
2573 "bluetooth hardware not installed\n");
2574 } else {
2575 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2576 &bluetooth_attr_group);
2577 if (res)
2578 return res;
2582 return (tp_features.bluetooth)? 0 : 1;
2585 static void bluetooth_exit(void)
2587 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2588 &bluetooth_attr_group);
2591 static int bluetooth_get_radiosw(void)
2593 int status;
2595 if (!tp_features.bluetooth)
2596 return -ENODEV;
2598 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2599 return -EIO;
2601 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2604 static int bluetooth_set_radiosw(int radio_on)
2606 int status;
2608 if (!tp_features.bluetooth)
2609 return -ENODEV;
2611 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2612 return -EIO;
2613 if (radio_on)
2614 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2615 else
2616 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2617 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2618 return -EIO;
2620 return 0;
2623 /* procfs -------------------------------------------------------------- */
2624 static int bluetooth_read(char *p)
2626 int len = 0;
2627 int status = bluetooth_get_radiosw();
2629 if (!tp_features.bluetooth)
2630 len += sprintf(p + len, "status:\t\tnot supported\n");
2631 else {
2632 len += sprintf(p + len, "status:\t\t%s\n",
2633 (status)? "enabled" : "disabled");
2634 len += sprintf(p + len, "commands:\tenable, disable\n");
2637 return len;
2640 static int bluetooth_write(char *buf)
2642 char *cmd;
2644 if (!tp_features.bluetooth)
2645 return -ENODEV;
2647 while ((cmd = next_cmd(&buf))) {
2648 if (strlencmp(cmd, "enable") == 0) {
2649 bluetooth_set_radiosw(1);
2650 } else if (strlencmp(cmd, "disable") == 0) {
2651 bluetooth_set_radiosw(0);
2652 } else
2653 return -EINVAL;
2656 return 0;
2659 static struct ibm_struct bluetooth_driver_data = {
2660 .name = "bluetooth",
2661 .read = bluetooth_read,
2662 .write = bluetooth_write,
2663 .exit = bluetooth_exit,
2666 /*************************************************************************
2667 * Wan subdriver
2670 enum {
2671 /* ACPI GWAN/SWAN bits */
2672 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2673 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2674 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2677 static int wan_get_radiosw(void);
2678 static int wan_set_radiosw(int radio_on);
2680 /* sysfs wan enable ---------------------------------------------------- */
2681 static ssize_t wan_enable_show(struct device *dev,
2682 struct device_attribute *attr,
2683 char *buf)
2685 int status;
2687 status = wan_get_radiosw();
2688 if (status < 0)
2689 return status;
2691 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2694 static ssize_t wan_enable_store(struct device *dev,
2695 struct device_attribute *attr,
2696 const char *buf, size_t count)
2698 unsigned long t;
2699 int res;
2701 if (parse_strtoul(buf, 1, &t))
2702 return -EINVAL;
2704 res = wan_set_radiosw(t);
2706 return (res) ? res : count;
2709 static struct device_attribute dev_attr_wan_enable =
2710 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2711 wan_enable_show, wan_enable_store);
2713 /* --------------------------------------------------------------------- */
2715 static struct attribute *wan_attributes[] = {
2716 &dev_attr_wan_enable.attr,
2717 NULL
2720 static const struct attribute_group wan_attr_group = {
2721 .attrs = wan_attributes,
2724 static int __init wan_init(struct ibm_init_struct *iibm)
2726 int res;
2727 int status = 0;
2729 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2731 TPACPI_ACPIHANDLE_INIT(hkey);
2733 tp_features.wan = hkey_handle &&
2734 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2736 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2737 str_supported(tp_features.wan),
2738 status);
2740 if (tp_features.wan) {
2741 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2742 /* no wan hardware present in system */
2743 tp_features.wan = 0;
2744 dbg_printk(TPACPI_DBG_INIT,
2745 "wan hardware not installed\n");
2746 } else {
2747 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2748 &wan_attr_group);
2749 if (res)
2750 return res;
2754 return (tp_features.wan)? 0 : 1;
2757 static void wan_exit(void)
2759 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2760 &wan_attr_group);
2763 static int wan_get_radiosw(void)
2765 int status;
2767 if (!tp_features.wan)
2768 return -ENODEV;
2770 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2771 return -EIO;
2773 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2776 static int wan_set_radiosw(int radio_on)
2778 int status;
2780 if (!tp_features.wan)
2781 return -ENODEV;
2783 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2784 return -EIO;
2785 if (radio_on)
2786 status |= TP_ACPI_WANCARD_RADIOSSW;
2787 else
2788 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2789 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2790 return -EIO;
2792 return 0;
2795 /* procfs -------------------------------------------------------------- */
2796 static int wan_read(char *p)
2798 int len = 0;
2799 int status = wan_get_radiosw();
2801 if (!tp_features.wan)
2802 len += sprintf(p + len, "status:\t\tnot supported\n");
2803 else {
2804 len += sprintf(p + len, "status:\t\t%s\n",
2805 (status)? "enabled" : "disabled");
2806 len += sprintf(p + len, "commands:\tenable, disable\n");
2809 return len;
2812 static int wan_write(char *buf)
2814 char *cmd;
2816 if (!tp_features.wan)
2817 return -ENODEV;
2819 while ((cmd = next_cmd(&buf))) {
2820 if (strlencmp(cmd, "enable") == 0) {
2821 wan_set_radiosw(1);
2822 } else if (strlencmp(cmd, "disable") == 0) {
2823 wan_set_radiosw(0);
2824 } else
2825 return -EINVAL;
2828 return 0;
2831 static struct ibm_struct wan_driver_data = {
2832 .name = "wan",
2833 .read = wan_read,
2834 .write = wan_write,
2835 .exit = wan_exit,
2836 .flags.experimental = 1,
2839 /*************************************************************************
2840 * Video subdriver
2843 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2844 =======
2845 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2847 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2848 enum video_access_mode {
2849 TPACPI_VIDEO_NONE = 0,
2850 TPACPI_VIDEO_570, /* 570 */
2851 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2852 TPACPI_VIDEO_NEW, /* all others */
2855 enum { /* video status flags, based on VIDEO_570 */
2856 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2857 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2858 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2861 enum { /* TPACPI_VIDEO_570 constants */
2862 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2863 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2864 * video_status_flags */
2865 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2866 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2869 static enum video_access_mode video_supported;
2870 static int video_orig_autosw;
2872 static int video_autosw_get(void);
2873 static int video_autosw_set(int enable);
2875 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
2876 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2877 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2878 "\\_SB.PCI0.VID0", /* 770e */
2879 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2880 "\\_SB.PCI0.AGP.VID", /* all others */
2881 ); /* R30, R31 */
2883 =======
2884 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
2885 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2887 static int __init video_init(struct ibm_init_struct *iibm)
2889 int ivga;
2891 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2893 TPACPI_ACPIHANDLE_INIT(vid);
2894 TPACPI_ACPIHANDLE_INIT(vid2);
2896 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2897 /* G41, assume IVGA doesn't change */
2898 vid_handle = vid2_handle;
2900 if (!vid_handle)
2901 /* video switching not supported on R30, R31 */
2902 video_supported = TPACPI_VIDEO_NONE;
2903 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2904 /* 570 */
2905 video_supported = TPACPI_VIDEO_570;
2906 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2907 /* 600e/x, 770e, 770x */
2908 video_supported = TPACPI_VIDEO_770;
2909 else
2910 /* all others */
2911 video_supported = TPACPI_VIDEO_NEW;
2913 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2914 str_supported(video_supported != TPACPI_VIDEO_NONE),
2915 video_supported);
2917 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2920 static void video_exit(void)
2922 dbg_printk(TPACPI_DBG_EXIT,
2923 "restoring original video autoswitch mode\n");
2924 if (video_autosw_set(video_orig_autosw))
2925 printk(TPACPI_ERR "error while trying to restore original "
2926 "video autoswitch mode\n");
2929 static int video_outputsw_get(void)
2931 int status = 0;
2932 int i;
2934 switch (video_supported) {
2935 case TPACPI_VIDEO_570:
2936 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2937 TP_ACPI_VIDEO_570_PHSCMD))
2938 return -EIO;
2939 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2940 break;
2941 case TPACPI_VIDEO_770:
2942 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2943 return -EIO;
2944 if (i)
2945 status |= TP_ACPI_VIDEO_S_LCD;
2946 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2947 return -EIO;
2948 if (i)
2949 status |= TP_ACPI_VIDEO_S_CRT;
2950 break;
2951 case TPACPI_VIDEO_NEW:
2952 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2953 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2954 return -EIO;
2955 if (i)
2956 status |= TP_ACPI_VIDEO_S_CRT;
2958 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2959 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2960 return -EIO;
2961 if (i)
2962 status |= TP_ACPI_VIDEO_S_LCD;
2963 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2964 return -EIO;
2965 if (i)
2966 status |= TP_ACPI_VIDEO_S_DVI;
2967 break;
2968 default:
2969 return -ENOSYS;
2972 return status;
2975 static int video_outputsw_set(int status)
2977 int autosw;
2978 int res = 0;
2980 switch (video_supported) {
2981 case TPACPI_VIDEO_570:
2982 res = acpi_evalf(NULL, NULL,
2983 "\\_SB.PHS2", "vdd",
2984 TP_ACPI_VIDEO_570_PHS2CMD,
2985 status | TP_ACPI_VIDEO_570_PHS2SET);
2986 break;
2987 case TPACPI_VIDEO_770:
2988 autosw = video_autosw_get();
2989 if (autosw < 0)
2990 return autosw;
2992 res = video_autosw_set(1);
2993 if (res)
2994 return res;
2995 res = acpi_evalf(vid_handle, NULL,
2996 "ASWT", "vdd", status * 0x100, 0);
2997 if (!autosw && video_autosw_set(autosw)) {
2998 printk(TPACPI_ERR
2999 "video auto-switch left enabled due to error\n");
3000 return -EIO;
3002 break;
3003 case TPACPI_VIDEO_NEW:
3004 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3005 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3006 break;
3007 default:
3008 return -ENOSYS;
3011 return (res)? 0 : -EIO;
3014 static int video_autosw_get(void)
3016 int autosw = 0;
3018 switch (video_supported) {
3019 case TPACPI_VIDEO_570:
3020 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3021 return -EIO;
3022 break;
3023 case TPACPI_VIDEO_770:
3024 case TPACPI_VIDEO_NEW:
3025 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3026 return -EIO;
3027 break;
3028 default:
3029 return -ENOSYS;
3032 return autosw & 1;
3035 static int video_autosw_set(int enable)
3037 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3038 return -EIO;
3039 return 0;
3042 static int video_outputsw_cycle(void)
3044 int autosw = video_autosw_get();
3045 int res;
3047 if (autosw < 0)
3048 return autosw;
3050 switch (video_supported) {
3051 case TPACPI_VIDEO_570:
3052 res = video_autosw_set(1);
3053 if (res)
3054 return res;
3055 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3056 break;
3057 case TPACPI_VIDEO_770:
3058 case TPACPI_VIDEO_NEW:
3059 res = video_autosw_set(1);
3060 if (res)
3061 return res;
3062 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3063 break;
3064 default:
3065 return -ENOSYS;
3067 if (!autosw && video_autosw_set(autosw)) {
3068 printk(TPACPI_ERR
3069 "video auto-switch left enabled due to error\n");
3070 return -EIO;
3073 return (res)? 0 : -EIO;
3076 static int video_expand_toggle(void)
3078 switch (video_supported) {
3079 case TPACPI_VIDEO_570:
3080 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3081 0 : -EIO;
3082 case TPACPI_VIDEO_770:
3083 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3084 0 : -EIO;
3085 case TPACPI_VIDEO_NEW:
3086 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3087 0 : -EIO;
3088 default:
3089 return -ENOSYS;
3091 /* not reached */
3094 static int video_read(char *p)
3096 int status, autosw;
3097 int len = 0;
3099 if (video_supported == TPACPI_VIDEO_NONE) {
3100 len += sprintf(p + len, "status:\t\tnot supported\n");
3101 return len;
3104 status = video_outputsw_get();
3105 if (status < 0)
3106 return status;
3108 autosw = video_autosw_get();
3109 if (autosw < 0)
3110 return autosw;
3112 len += sprintf(p + len, "status:\t\tsupported\n");
3113 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3114 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3115 if (video_supported == TPACPI_VIDEO_NEW)
3116 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3117 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3118 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3119 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3120 if (video_supported == TPACPI_VIDEO_NEW)
3121 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3122 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3123 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3125 return len;
3128 static int video_write(char *buf)
3130 char *cmd;
3131 int enable, disable, status;
3132 int res;
3134 if (video_supported == TPACPI_VIDEO_NONE)
3135 return -ENODEV;
3137 enable = 0;
3138 disable = 0;
3140 while ((cmd = next_cmd(&buf))) {
3141 if (strlencmp(cmd, "lcd_enable") == 0) {
3142 enable |= TP_ACPI_VIDEO_S_LCD;
3143 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3144 disable |= TP_ACPI_VIDEO_S_LCD;
3145 } else if (strlencmp(cmd, "crt_enable") == 0) {
3146 enable |= TP_ACPI_VIDEO_S_CRT;
3147 } else if (strlencmp(cmd, "crt_disable") == 0) {
3148 disable |= TP_ACPI_VIDEO_S_CRT;
3149 } else if (video_supported == TPACPI_VIDEO_NEW &&
3150 strlencmp(cmd, "dvi_enable") == 0) {
3151 enable |= TP_ACPI_VIDEO_S_DVI;
3152 } else if (video_supported == TPACPI_VIDEO_NEW &&
3153 strlencmp(cmd, "dvi_disable") == 0) {
3154 disable |= TP_ACPI_VIDEO_S_DVI;
3155 } else if (strlencmp(cmd, "auto_enable") == 0) {
3156 res = video_autosw_set(1);
3157 if (res)
3158 return res;
3159 } else if (strlencmp(cmd, "auto_disable") == 0) {
3160 res = video_autosw_set(0);
3161 if (res)
3162 return res;
3163 } else if (strlencmp(cmd, "video_switch") == 0) {
3164 res = video_outputsw_cycle();
3165 if (res)
3166 return res;
3167 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3168 res = video_expand_toggle();
3169 if (res)
3170 return res;
3171 } else
3172 return -EINVAL;
3175 if (enable || disable) {
3176 status = video_outputsw_get();
3177 if (status < 0)
3178 return status;
3179 res = video_outputsw_set((status & ~disable) | enable);
3180 if (res)
3181 return res;
3184 return 0;
3187 static struct ibm_struct video_driver_data = {
3188 .name = "video",
3189 .read = video_read,
3190 .write = video_write,
3191 .exit = video_exit,
3194 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
3195 =======
3196 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3198 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
3199 /*************************************************************************
3200 * Light (thinklight) subdriver
3203 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3204 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3206 static int __init light_init(struct ibm_init_struct *iibm)
3208 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3210 TPACPI_ACPIHANDLE_INIT(ledb);
3211 TPACPI_ACPIHANDLE_INIT(lght);
3212 TPACPI_ACPIHANDLE_INIT(cmos);
3214 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3215 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3217 if (tp_features.light)
3218 /* light status not supported on
3219 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3220 tp_features.light_status =
3221 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3223 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3224 str_supported(tp_features.light));
3226 return (tp_features.light)? 0 : 1;
3229 static int light_read(char *p)
3231 int len = 0;
3232 int status = 0;
3234 if (!tp_features.light) {
3235 len += sprintf(p + len, "status:\t\tnot supported\n");
3236 } else if (!tp_features.light_status) {
3237 len += sprintf(p + len, "status:\t\tunknown\n");
3238 len += sprintf(p + len, "commands:\ton, off\n");
3239 } else {
3240 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3241 return -EIO;
3242 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3243 len += sprintf(p + len, "commands:\ton, off\n");
3246 return len;
3249 static int light_write(char *buf)
3251 int cmos_cmd, lght_cmd;
3252 char *cmd;
3253 int success;
3255 if (!tp_features.light)
3256 return -ENODEV;
3258 while ((cmd = next_cmd(&buf))) {
3259 if (strlencmp(cmd, "on") == 0) {
3260 cmos_cmd = 0x0c;
3261 lght_cmd = 1;
3262 } else if (strlencmp(cmd, "off") == 0) {
3263 cmos_cmd = 0x0d;
3264 lght_cmd = 0;
3265 } else
3266 return -EINVAL;
3268 success = cmos_handle ?
3269 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3270 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3271 if (!success)
3272 return -EIO;
3275 return 0;
3278 static struct ibm_struct light_driver_data = {
3279 .name = "light",
3280 .read = light_read,
3281 .write = light_write,
3284 /*************************************************************************
3285 * Dock subdriver
3288 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3290 static void dock_notify(struct ibm_struct *ibm, u32 event);
3291 static int dock_read(char *p);
3292 static int dock_write(char *buf);
3294 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3295 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3296 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3297 "\\_SB.PCI.ISA.SLCE", /* 570 */
3298 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3300 /* don't list other alternatives as we install a notify handler on the 570 */
3301 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3303 static const struct acpi_device_id ibm_pci_device_ids[] = {
3304 {PCI_ROOT_HID_STRING, 0},
3305 {"", 0},
3308 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3310 .notify = dock_notify,
3311 .handle = &dock_handle,
3312 .type = ACPI_SYSTEM_NOTIFY,
3315 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3316 * We just use it to get notifications of dock hotplug
3317 * in very old thinkpads */
3318 .hid = ibm_pci_device_ids,
3319 .notify = dock_notify,
3320 .handle = &pci_handle,
3321 .type = ACPI_SYSTEM_NOTIFY,
3325 static struct ibm_struct dock_driver_data[2] = {
3327 .name = "dock",
3328 .read = dock_read,
3329 .write = dock_write,
3330 .acpi = &ibm_dock_acpidriver[0],
3333 .name = "dock",
3334 .acpi = &ibm_dock_acpidriver[1],
3338 #define dock_docked() (_sta(dock_handle) & 1)
3340 static int __init dock_init(struct ibm_init_struct *iibm)
3342 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3344 TPACPI_ACPIHANDLE_INIT(dock);
3346 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3347 str_supported(dock_handle != NULL));
3349 return (dock_handle)? 0 : 1;
3352 static int __init dock_init2(struct ibm_init_struct *iibm)
3354 int dock2_needed;
3356 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3358 if (dock_driver_data[0].flags.acpi_driver_registered &&
3359 dock_driver_data[0].flags.acpi_notify_installed) {
3360 TPACPI_ACPIHANDLE_INIT(pci);
3361 dock2_needed = (pci_handle != NULL);
3362 vdbg_printk(TPACPI_DBG_INIT,
3363 "dock PCI handler for the TP 570 is %s\n",
3364 str_supported(dock2_needed));
3365 } else {
3366 vdbg_printk(TPACPI_DBG_INIT,
3367 "dock subdriver part 2 not required\n");
3368 dock2_needed = 0;
3371 return (dock2_needed)? 0 : 1;
3374 static void dock_notify(struct ibm_struct *ibm, u32 event)
3376 int docked = dock_docked();
3377 int pci = ibm->acpi->hid && ibm->acpi->device &&
3378 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3379 int data;
3381 if (event == 1 && !pci) /* 570 */
3382 data = 1; /* button */
3383 else if (event == 1 && pci) /* 570 */
3384 data = 3; /* dock */
3385 else if (event == 3 && docked)
3386 data = 1; /* button */
3387 else if (event == 3 && !docked)
3388 data = 2; /* undock */
3389 else if (event == 0 && docked)
3390 data = 3; /* dock */
3391 else {
3392 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3393 event, _sta(dock_handle));
3394 data = 0; /* unknown */
3396 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3397 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3398 ibm->acpi->device->dev.bus_id,
3399 event, data);
3402 static int dock_read(char *p)
3404 int len = 0;
3405 int docked = dock_docked();
3407 if (!dock_handle)
3408 len += sprintf(p + len, "status:\t\tnot supported\n");
3409 else if (!docked)
3410 len += sprintf(p + len, "status:\t\tundocked\n");
3411 else {
3412 len += sprintf(p + len, "status:\t\tdocked\n");
3413 len += sprintf(p + len, "commands:\tdock, undock\n");
3416 return len;
3419 static int dock_write(char *buf)
3421 char *cmd;
3423 if (!dock_docked())
3424 return -ENODEV;
3426 while ((cmd = next_cmd(&buf))) {
3427 if (strlencmp(cmd, "undock") == 0) {
3428 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3429 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3430 return -EIO;
3431 } else if (strlencmp(cmd, "dock") == 0) {
3432 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3433 return -EIO;
3434 } else
3435 return -EINVAL;
3438 return 0;
3441 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3443 /*************************************************************************
3444 * Bay subdriver
3447 #ifdef CONFIG_THINKPAD_ACPI_BAY
3449 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3450 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3451 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3452 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3453 ); /* A21e, R30, R31 */
3454 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3455 "_EJ0", /* all others */
3456 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3457 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3458 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3459 ); /* all others */
3460 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3461 "_EJ0", /* 770x */
3462 ); /* all others */
3464 static int __init bay_init(struct ibm_init_struct *iibm)
3466 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3468 TPACPI_ACPIHANDLE_INIT(bay);
3469 if (bay_handle)
3470 TPACPI_ACPIHANDLE_INIT(bay_ej);
3471 TPACPI_ACPIHANDLE_INIT(bay2);
3472 if (bay2_handle)
3473 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3475 tp_features.bay_status = bay_handle &&
3476 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3477 tp_features.bay_status2 = bay2_handle &&
3478 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3480 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3481 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3482 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3483 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3485 vdbg_printk(TPACPI_DBG_INIT,
3486 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3487 str_supported(tp_features.bay_status),
3488 str_supported(tp_features.bay_eject),
3489 str_supported(tp_features.bay_status2),
3490 str_supported(tp_features.bay_eject2));
3492 return (tp_features.bay_status || tp_features.bay_eject ||
3493 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3496 static void bay_notify(struct ibm_struct *ibm, u32 event)
3498 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3499 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3500 ibm->acpi->device->dev.bus_id,
3501 event, 0);
3504 #define bay_occupied(b) (_sta(b##_handle) & 1)
3506 static int bay_read(char *p)
3508 int len = 0;
3509 int occupied = bay_occupied(bay);
3510 int occupied2 = bay_occupied(bay2);
3511 int eject, eject2;
3513 len += sprintf(p + len, "status:\t\t%s\n",
3514 tp_features.bay_status ?
3515 (occupied ? "occupied" : "unoccupied") :
3516 "not supported");
3517 if (tp_features.bay_status2)
3518 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3519 "occupied" : "unoccupied");
3521 eject = tp_features.bay_eject && occupied;
3522 eject2 = tp_features.bay_eject2 && occupied2;
3524 if (eject && eject2)
3525 len += sprintf(p + len, "commands:\teject, eject2\n");
3526 else if (eject)
3527 len += sprintf(p + len, "commands:\teject\n");
3528 else if (eject2)
3529 len += sprintf(p + len, "commands:\teject2\n");
3531 return len;
3534 static int bay_write(char *buf)
3536 char *cmd;
3538 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3539 return -ENODEV;
3541 while ((cmd = next_cmd(&buf))) {
3542 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3543 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3544 return -EIO;
3545 } else if (tp_features.bay_eject2 &&
3546 strlencmp(cmd, "eject2") == 0) {
3547 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3548 return -EIO;
3549 } else
3550 return -EINVAL;
3553 return 0;
3556 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3557 .notify = bay_notify,
3558 .handle = &bay_handle,
3559 .type = ACPI_SYSTEM_NOTIFY,
3562 static struct ibm_struct bay_driver_data = {
3563 .name = "bay",
3564 .read = bay_read,
3565 .write = bay_write,
3566 .acpi = &ibm_bay_acpidriver,
3569 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3571 /*************************************************************************
3572 * CMOS subdriver
3575 /* sysfs cmos_command -------------------------------------------------- */
3576 static ssize_t cmos_command_store(struct device *dev,
3577 struct device_attribute *attr,
3578 const char *buf, size_t count)
3580 unsigned long cmos_cmd;
3581 int res;
3583 if (parse_strtoul(buf, 21, &cmos_cmd))
3584 return -EINVAL;
3586 res = issue_thinkpad_cmos_command(cmos_cmd);
3587 return (res)? res : count;
3590 static struct device_attribute dev_attr_cmos_command =
3591 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3593 /* --------------------------------------------------------------------- */
3595 static int __init cmos_init(struct ibm_init_struct *iibm)
3597 int res;
3599 vdbg_printk(TPACPI_DBG_INIT,
3600 "initializing cmos commands subdriver\n");
3602 TPACPI_ACPIHANDLE_INIT(cmos);
3604 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3605 str_supported(cmos_handle != NULL));
3607 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3608 if (res)
3609 return res;
3611 return (cmos_handle)? 0 : 1;
3614 static void cmos_exit(void)
3616 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3619 static int cmos_read(char *p)
3621 int len = 0;
3623 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3624 R30, R31, T20-22, X20-21 */
3625 if (!cmos_handle)
3626 len += sprintf(p + len, "status:\t\tnot supported\n");
3627 else {
3628 len += sprintf(p + len, "status:\t\tsupported\n");
3629 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3632 return len;
3635 static int cmos_write(char *buf)
3637 char *cmd;
3638 int cmos_cmd, res;
3640 while ((cmd = next_cmd(&buf))) {
3641 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3642 cmos_cmd >= 0 && cmos_cmd <= 21) {
3643 /* cmos_cmd set */
3644 } else
3645 return -EINVAL;
3647 res = issue_thinkpad_cmos_command(cmos_cmd);
3648 if (res)
3649 return res;
3652 return 0;
3655 static struct ibm_struct cmos_driver_data = {
3656 .name = "cmos",
3657 .read = cmos_read,
3658 .write = cmos_write,
3659 .exit = cmos_exit,
3662 /*************************************************************************
3663 * LED subdriver
3666 enum led_access_mode {
3667 TPACPI_LED_NONE = 0,
3668 TPACPI_LED_570, /* 570 */
3669 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3670 TPACPI_LED_NEW, /* all others */
3673 enum { /* For TPACPI_LED_OLD */
3674 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3675 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3676 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3679 static enum led_access_mode led_supported;
3681 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3682 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3683 /* T20-22, X20-21 */
3684 "LED", /* all others */
3685 ); /* R30, R31 */
3687 static int __init led_init(struct ibm_init_struct *iibm)
3689 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3691 TPACPI_ACPIHANDLE_INIT(led);
3693 if (!led_handle)
3694 /* led not supported on R30, R31 */
3695 led_supported = TPACPI_LED_NONE;
3696 else if (strlencmp(led_path, "SLED") == 0)
3697 /* 570 */
3698 led_supported = TPACPI_LED_570;
3699 else if (strlencmp(led_path, "SYSL") == 0)
3700 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3701 led_supported = TPACPI_LED_OLD;
3702 else
3703 /* all others */
3704 led_supported = TPACPI_LED_NEW;
3706 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3707 str_supported(led_supported), led_supported);
3709 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3712 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3714 static int led_read(char *p)
3716 int len = 0;
3718 if (!led_supported) {
3719 len += sprintf(p + len, "status:\t\tnot supported\n");
3720 return len;
3722 len += sprintf(p + len, "status:\t\tsupported\n");
3724 if (led_supported == TPACPI_LED_570) {
3725 /* 570 */
3726 int i, status;
3727 for (i = 0; i < 8; i++) {
3728 if (!acpi_evalf(ec_handle,
3729 &status, "GLED", "dd", 1 << i))
3730 return -EIO;
3731 len += sprintf(p + len, "%d:\t\t%s\n",
3732 i, led_status(status));
3736 len += sprintf(p + len, "commands:\t"
3737 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3739 return len;
3742 /* off, on, blink */
3743 static const int led_sled_arg1[] = { 0, 1, 3 };
3744 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3745 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3746 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3748 static int led_write(char *buf)
3750 char *cmd;
3751 int led, ind, ret;
3753 if (!led_supported)
3754 return -ENODEV;
3756 while ((cmd = next_cmd(&buf))) {
3757 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3758 return -EINVAL;
3760 if (strstr(cmd, "off")) {
3761 ind = 0;
3762 } else if (strstr(cmd, "on")) {
3763 ind = 1;
3764 } else if (strstr(cmd, "blink")) {
3765 ind = 2;
3766 } else
3767 return -EINVAL;
3769 if (led_supported == TPACPI_LED_570) {
3770 /* 570 */
3771 led = 1 << led;
3772 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3773 led, led_sled_arg1[ind]))
3774 return -EIO;
3775 } else if (led_supported == TPACPI_LED_OLD) {
3776 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3777 led = 1 << led;
3778 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3779 if (ret >= 0)
3780 ret = ec_write(TPACPI_LED_EC_HLBL,
3781 led * led_exp_hlbl[ind]);
3782 if (ret >= 0)
3783 ret = ec_write(TPACPI_LED_EC_HLCL,
3784 led * led_exp_hlcl[ind]);
3785 if (ret < 0)
3786 return ret;
3787 } else {
3788 /* all others */
3789 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3790 led, led_led_arg1[ind]))
3791 return -EIO;
3795 return 0;
3798 static struct ibm_struct led_driver_data = {
3799 .name = "led",
3800 .read = led_read,
3801 .write = led_write,
3804 /*************************************************************************
3805 * Beep subdriver
3808 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3810 static int __init beep_init(struct ibm_init_struct *iibm)
3812 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3814 TPACPI_ACPIHANDLE_INIT(beep);
3816 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3817 str_supported(beep_handle != NULL));
3819 return (beep_handle)? 0 : 1;
3822 static int beep_read(char *p)
3824 int len = 0;
3826 if (!beep_handle)
3827 len += sprintf(p + len, "status:\t\tnot supported\n");
3828 else {
3829 len += sprintf(p + len, "status:\t\tsupported\n");
3830 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3833 return len;
3836 static int beep_write(char *buf)
3838 char *cmd;
3839 int beep_cmd;
3841 if (!beep_handle)
3842 return -ENODEV;
3844 while ((cmd = next_cmd(&buf))) {
3845 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3846 beep_cmd >= 0 && beep_cmd <= 17) {
3847 /* beep_cmd set */
3848 } else
3849 return -EINVAL;
3850 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3851 return -EIO;
3854 return 0;
3857 static struct ibm_struct beep_driver_data = {
3858 .name = "beep",
3859 .read = beep_read,
3860 .write = beep_write,
3863 /*************************************************************************
3864 * Thermal subdriver
3867 enum thermal_access_mode {
3868 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3869 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3870 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3871 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3872 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3875 enum { /* TPACPI_THERMAL_TPEC_* */
3876 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3877 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3878 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3881 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3882 struct ibm_thermal_sensors_struct {
3883 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3886 static enum thermal_access_mode thermal_read_mode;
3888 /* idx is zero-based */
3889 static int thermal_get_sensor(int idx, s32 *value)
3891 int t;
3892 s8 tmp;
3893 char tmpi[5];
3895 t = TP_EC_THERMAL_TMP0;
3897 switch (thermal_read_mode) {
3898 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3899 case TPACPI_THERMAL_TPEC_16:
3900 if (idx >= 8 && idx <= 15) {
3901 t = TP_EC_THERMAL_TMP8;
3902 idx -= 8;
3904 /* fallthrough */
3905 #endif
3906 case TPACPI_THERMAL_TPEC_8:
3907 if (idx <= 7) {
3908 if (!acpi_ec_read(t + idx, &tmp))
3909 return -EIO;
3910 *value = tmp * 1000;
3911 return 0;
3913 break;
3915 case TPACPI_THERMAL_ACPI_UPDT:
3916 if (idx <= 7) {
3917 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3918 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3919 return -EIO;
3920 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3921 return -EIO;
3922 *value = (t - 2732) * 100;
3923 return 0;
3925 break;
3927 case TPACPI_THERMAL_ACPI_TMP07:
3928 if (idx <= 7) {
3929 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3930 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3931 return -EIO;
3932 if (t > 127 || t < -127)
3933 t = TP_EC_THERMAL_TMP_NA;
3934 *value = t * 1000;
3935 return 0;
3937 break;
3939 case TPACPI_THERMAL_NONE:
3940 default:
3941 return -ENOSYS;
3944 return -EINVAL;
3947 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3949 int res, i;
3950 int n;
3952 n = 8;
3953 i = 0;
3955 if (!s)
3956 return -EINVAL;
3958 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3959 n = 16;
3961 for (i = 0 ; i < n; i++) {
3962 res = thermal_get_sensor(i, &s->temp[i]);
3963 if (res)
3964 return res;
3967 return n;
3970 /* sysfs temp##_input -------------------------------------------------- */
3972 static ssize_t thermal_temp_input_show(struct device *dev,
3973 struct device_attribute *attr,
3974 char *buf)
3976 struct sensor_device_attribute *sensor_attr =
3977 to_sensor_dev_attr(attr);
3978 int idx = sensor_attr->index;
3979 s32 value;
3980 int res;
3982 res = thermal_get_sensor(idx, &value);
3983 if (res)
3984 return res;
3985 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3986 return -ENXIO;
3988 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3991 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3992 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3993 thermal_temp_input_show, NULL, _idxB)
3995 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3996 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3997 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3998 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3999 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4000 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4001 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4002 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4003 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4004 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4005 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4006 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4007 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4008 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4009 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4010 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4011 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4014 #define THERMAL_ATTRS(X) \
4015 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4017 static struct attribute *thermal_temp_input_attr[] = {
4018 THERMAL_ATTRS(8),
4019 THERMAL_ATTRS(9),
4020 THERMAL_ATTRS(10),
4021 THERMAL_ATTRS(11),
4022 THERMAL_ATTRS(12),
4023 THERMAL_ATTRS(13),
4024 THERMAL_ATTRS(14),
4025 THERMAL_ATTRS(15),
4026 THERMAL_ATTRS(0),
4027 THERMAL_ATTRS(1),
4028 THERMAL_ATTRS(2),
4029 THERMAL_ATTRS(3),
4030 THERMAL_ATTRS(4),
4031 THERMAL_ATTRS(5),
4032 THERMAL_ATTRS(6),
4033 THERMAL_ATTRS(7),
4034 NULL
4037 static const struct attribute_group thermal_temp_input16_group = {
4038 .attrs = thermal_temp_input_attr
4041 static const struct attribute_group thermal_temp_input8_group = {
4042 .attrs = &thermal_temp_input_attr[8]
4045 #undef THERMAL_SENSOR_ATTR_TEMP
4046 #undef THERMAL_ATTRS
4048 /* --------------------------------------------------------------------- */
4050 static int __init thermal_init(struct ibm_init_struct *iibm)
4052 u8 t, ta1, ta2;
4053 int i;
4054 int acpi_tmp7;
4055 int res;
4057 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4059 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4061 if (thinkpad_id.ec_model) {
4063 * Direct EC access mode: sensors at registers
4064 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4065 * non-implemented, thermal sensors return 0x80 when
4066 * not available
4069 ta1 = ta2 = 0;
4070 for (i = 0; i < 8; i++) {
4071 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4072 ta1 |= t;
4073 } else {
4074 ta1 = 0;
4075 break;
4077 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4078 ta2 |= t;
4079 } else {
4080 ta1 = 0;
4081 break;
4084 if (ta1 == 0) {
4085 /* This is sheer paranoia, but we handle it anyway */
4086 if (acpi_tmp7) {
4087 printk(TPACPI_ERR
4088 "ThinkPad ACPI EC access misbehaving, "
4089 "falling back to ACPI TMPx access "
4090 "mode\n");
4091 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4092 } else {
4093 printk(TPACPI_ERR
4094 "ThinkPad ACPI EC access misbehaving, "
4095 "disabling thermal sensors access\n");
4096 thermal_read_mode = TPACPI_THERMAL_NONE;
4098 } else {
4099 thermal_read_mode =
4100 (ta2 != 0) ?
4101 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4103 } else if (acpi_tmp7) {
4104 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4105 /* 600e/x, 770e, 770x */
4106 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4107 } else {
4108 /* Standard ACPI TMPx access, max 8 sensors */
4109 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4111 } else {
4112 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4113 thermal_read_mode = TPACPI_THERMAL_NONE;
4116 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4117 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4118 thermal_read_mode);
4120 switch (thermal_read_mode) {
4121 case TPACPI_THERMAL_TPEC_16:
4122 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4123 &thermal_temp_input16_group);
4124 if (res)
4125 return res;
4126 break;
4127 case TPACPI_THERMAL_TPEC_8:
4128 case TPACPI_THERMAL_ACPI_TMP07:
4129 case TPACPI_THERMAL_ACPI_UPDT:
4130 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4131 &thermal_temp_input8_group);
4132 if (res)
4133 return res;
4134 break;
4135 case TPACPI_THERMAL_NONE:
4136 default:
4137 return 1;
4140 return 0;
4143 static void thermal_exit(void)
4145 switch (thermal_read_mode) {
4146 case TPACPI_THERMAL_TPEC_16:
4147 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4148 &thermal_temp_input16_group);
4149 break;
4150 case TPACPI_THERMAL_TPEC_8:
4151 case TPACPI_THERMAL_ACPI_TMP07:
4152 case TPACPI_THERMAL_ACPI_UPDT:
4153 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4154 &thermal_temp_input16_group);
4155 break;
4156 case TPACPI_THERMAL_NONE:
4157 default:
4158 break;
4162 static int thermal_read(char *p)
4164 int len = 0;
4165 int n, i;
4166 struct ibm_thermal_sensors_struct t;
4168 n = thermal_get_sensors(&t);
4169 if (unlikely(n < 0))
4170 return n;
4172 len += sprintf(p + len, "temperatures:\t");
4174 if (n > 0) {
4175 for (i = 0; i < (n - 1); i++)
4176 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4177 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4178 } else
4179 len += sprintf(p + len, "not supported\n");
4181 return len;
4184 static struct ibm_struct thermal_driver_data = {
4185 .name = "thermal",
4186 .read = thermal_read,
4187 .exit = thermal_exit,
4190 /*************************************************************************
4191 * EC Dump subdriver
4194 static u8 ecdump_regs[256];
4196 static int ecdump_read(char *p)
4198 int len = 0;
4199 int i, j;
4200 u8 v;
4202 len += sprintf(p + len, "EC "
4203 " +00 +01 +02 +03 +04 +05 +06 +07"
4204 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4205 for (i = 0; i < 256; i += 16) {
4206 len += sprintf(p + len, "EC 0x%02x:", i);
4207 for (j = 0; j < 16; j++) {
4208 if (!acpi_ec_read(i + j, &v))
4209 break;
4210 if (v != ecdump_regs[i + j])
4211 len += sprintf(p + len, " *%02x", v);
4212 else
4213 len += sprintf(p + len, " %02x", v);
4214 ecdump_regs[i + j] = v;
4216 len += sprintf(p + len, "\n");
4217 if (j != 16)
4218 break;
4221 /* These are way too dangerous to advertise openly... */
4222 #if 0
4223 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4224 " (<offset> is 00-ff, <value> is 00-ff)\n");
4225 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4226 " (<offset> is 00-ff, <value> is 0-255)\n");
4227 #endif
4228 return len;
4231 static int ecdump_write(char *buf)
4233 char *cmd;
4234 int i, v;
4236 while ((cmd = next_cmd(&buf))) {
4237 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4238 /* i and v set */
4239 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4240 /* i and v set */
4241 } else
4242 return -EINVAL;
4243 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4244 if (!acpi_ec_write(i, v))
4245 return -EIO;
4246 } else
4247 return -EINVAL;
4250 return 0;
4253 static struct ibm_struct ecdump_driver_data = {
4254 .name = "ecdump",
4255 .read = ecdump_read,
4256 .write = ecdump_write,
4257 .flags.experimental = 1,
4260 /*************************************************************************
4261 * Backlight/brightness subdriver
4264 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4266 static struct backlight_device *ibm_backlight_device;
4267 static int brightness_offset = 0x31;
4268 static int brightness_mode;
4269 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4271 static struct mutex brightness_mutex;
4274 * ThinkPads can read brightness from two places: EC 0x31, or
4275 * CMOS NVRAM byte 0x5E, bits 0-3.
4277 static int brightness_get(struct backlight_device *bd)
4279 u8 lec = 0, lcmos = 0, level = 0;
4281 if (brightness_mode & 1) {
4282 if (!acpi_ec_read(brightness_offset, &lec))
4283 return -EIO;
4284 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4285 level = lec;
4287 if (brightness_mode & 2) {
4288 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4289 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4290 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4291 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4292 level = lcmos;
4295 if (brightness_mode == 3 && lec != lcmos) {
4296 printk(TPACPI_ERR
4297 "CMOS NVRAM (%u) and EC (%u) do not agree "
4298 "on display brightness level\n",
4299 (unsigned int) lcmos,
4300 (unsigned int) lec);
4301 return -EIO;
4304 return level;
4307 /* May return EINTR which can always be mapped to ERESTARTSYS */
4308 static int brightness_set(int value)
4310 int cmos_cmd, inc, i, res;
4311 int current_value;
4313 if (value > ((tp_features.bright_16levels)? 15 : 7))
4314 return -EINVAL;
4316 res = mutex_lock_interruptible(&brightness_mutex);
4317 if (res < 0)
4318 return res;
4320 current_value = brightness_get(NULL);
4321 if (current_value < 0) {
4322 res = current_value;
4323 goto errout;
4326 cmos_cmd = value > current_value ?
4327 TP_CMOS_BRIGHTNESS_UP :
4328 TP_CMOS_BRIGHTNESS_DOWN;
4329 inc = (value > current_value)? 1 : -1;
4331 res = 0;
4332 for (i = current_value; i != value; i += inc) {
4333 if ((brightness_mode & 2) &&
4334 issue_thinkpad_cmos_command(cmos_cmd)) {
4335 res = -EIO;
4336 goto errout;
4338 if ((brightness_mode & 1) &&
4339 !acpi_ec_write(brightness_offset, i + inc)) {
4340 res = -EIO;
4341 goto errout;;
4345 errout:
4346 mutex_unlock(&brightness_mutex);
4347 return res;
4350 /* sysfs backlight class ----------------------------------------------- */
4352 static int brightness_update_status(struct backlight_device *bd)
4354 /* it is the backlight class's job (caller) to handle
4355 * EINTR and other errors properly */
4356 return brightness_set(
4357 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4358 bd->props.power == FB_BLANK_UNBLANK) ?
4359 bd->props.brightness : 0);
4362 static struct backlight_ops ibm_backlight_data = {
4363 .get_brightness = brightness_get,
4364 .update_status = brightness_update_status,
4367 /* --------------------------------------------------------------------- */
4369 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4371 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4372 union acpi_object *obj;
4373 int rc;
4375 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4376 obj = (union acpi_object *)buffer.pointer;
4377 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4378 printk(TPACPI_ERR "Unknown BCLL data, "
4379 "please report this to %s\n", TPACPI_MAIL);
4380 rc = 0;
4381 } else {
4382 rc = obj->package.count;
4384 } else {
4385 return 0;
4388 kfree(buffer.pointer);
4389 return rc;
4392 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4393 void *context, void **rv)
4395 char name[ACPI_PATH_SEGMENT_LENGTH];
4396 struct acpi_buffer buffer = { sizeof(name), &name };
4398 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4399 !strncmp("BCLL", name, sizeof(name) - 1)) {
4400 if (tpacpi_query_bcll_levels(handle) == 16) {
4401 *rv = handle;
4402 return AE_CTRL_TERMINATE;
4403 } else {
4404 return AE_OK;
4406 } else {
4407 return AE_OK;
4411 static int __init brightness_check_levels(void)
4413 int status;
4414 void *found_node = NULL;
4416 if (!vid_handle) {
4417 TPACPI_ACPIHANDLE_INIT(vid);
4419 if (!vid_handle)
4420 return 0;
4422 /* Search for a BCLL package with 16 levels */
4423 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4424 brightness_find_bcll, NULL,
4425 &found_node);
4427 return (ACPI_SUCCESS(status) && found_node != NULL);
4430 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4431 void *context, void **rv)
4433 char name[ACPI_PATH_SEGMENT_LENGTH];
4434 struct acpi_buffer buffer = { sizeof(name), &name };
4436 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4437 !strncmp("_BCL", name, sizeof(name) - 1)) {
4438 *rv = handle;
4439 return AE_CTRL_TERMINATE;
4440 } else {
4441 return AE_OK;
4445 static int __init brightness_check_std_acpi_support(void)
4447 int status;
4448 void *found_node = NULL;
4450 if (!vid_handle) {
4451 TPACPI_ACPIHANDLE_INIT(vid);
4453 if (!vid_handle)
4454 return 0;
4456 /* Search for a _BCL method, but don't execute it */
4457 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4458 brightness_find_bcl, NULL, &found_node);
4460 return (ACPI_SUCCESS(status) && found_node != NULL);
4463 static int __init brightness_init(struct ibm_init_struct *iibm)
4465 int b;
4467 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4469 mutex_init(&brightness_mutex);
4471 if (!brightness_enable) {
4472 dbg_printk(TPACPI_DBG_INIT,
4473 "brightness support disabled by "
4474 "module parameter\n");
4475 return 1;
4476 } else if (brightness_enable > 1) {
4477 if (brightness_check_std_acpi_support()) {
4478 printk(TPACPI_NOTICE
4479 "standard ACPI backlight interface "
4480 "available, not loading native one...\n");
4481 return 1;
4485 if (!brightness_mode) {
4486 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4487 brightness_mode = 2;
4488 else
4489 brightness_mode = 3;
4491 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4492 brightness_mode);
4495 if (brightness_mode > 3)
4496 return -EINVAL;
4498 tp_features.bright_16levels =
4499 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4500 brightness_check_levels();
4502 b = brightness_get(NULL);
4503 if (b < 0)
4504 return 1;
4506 if (tp_features.bright_16levels)
4507 printk(TPACPI_INFO
4508 "detected a 16-level brightness capable ThinkPad\n");
4510 ibm_backlight_device = backlight_device_register(
4511 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4512 &ibm_backlight_data);
4513 if (IS_ERR(ibm_backlight_device)) {
4514 printk(TPACPI_ERR "Could not register backlight device\n");
4515 return PTR_ERR(ibm_backlight_device);
4517 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4519 ibm_backlight_device->props.max_brightness =
4520 (tp_features.bright_16levels)? 15 : 7;
4521 ibm_backlight_device->props.brightness = b;
4522 backlight_update_status(ibm_backlight_device);
4524 return 0;
4527 static void brightness_exit(void)
4529 if (ibm_backlight_device) {
4530 vdbg_printk(TPACPI_DBG_EXIT,
4531 "calling backlight_device_unregister()\n");
4532 backlight_device_unregister(ibm_backlight_device);
4533 ibm_backlight_device = NULL;
4537 static int brightness_read(char *p)
4539 int len = 0;
4540 int level;
4542 level = brightness_get(NULL);
4543 if (level < 0) {
4544 len += sprintf(p + len, "level:\t\tunreadable\n");
4545 } else {
4546 len += sprintf(p + len, "level:\t\t%d\n", level);
4547 len += sprintf(p + len, "commands:\tup, down\n");
4548 len += sprintf(p + len, "commands:\tlevel <level>"
4549 " (<level> is 0-%d)\n",
4550 (tp_features.bright_16levels) ? 15 : 7);
4553 return len;
4556 static int brightness_write(char *buf)
4558 int level;
4559 int rc;
4560 char *cmd;
4561 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4563 level = brightness_get(NULL);
4564 if (level < 0)
4565 return level;
4567 while ((cmd = next_cmd(&buf))) {
4568 if (strlencmp(cmd, "up") == 0) {
4569 if (level < max_level)
4570 level++;
4571 } else if (strlencmp(cmd, "down") == 0) {
4572 if (level > 0)
4573 level--;
4574 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4575 level >= 0 && level <= max_level) {
4576 /* new level set */
4577 } else
4578 return -EINVAL;
4582 * Now we know what the final level should be, so we try to set it.
4583 * Doing it this way makes the syscall restartable in case of EINTR
4585 rc = brightness_set(level);
4586 return (rc == -EINTR)? ERESTARTSYS : rc;
4589 static struct ibm_struct brightness_driver_data = {
4590 .name = "brightness",
4591 .read = brightness_read,
4592 .write = brightness_write,
4593 .exit = brightness_exit,
4596 /*************************************************************************
4597 * Volume subdriver
4600 static int volume_offset = 0x30;
4602 static int volume_read(char *p)
4604 int len = 0;
4605 u8 level;
4607 if (!acpi_ec_read(volume_offset, &level)) {
4608 len += sprintf(p + len, "level:\t\tunreadable\n");
4609 } else {
4610 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4611 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4612 len += sprintf(p + len, "commands:\tup, down, mute\n");
4613 len += sprintf(p + len, "commands:\tlevel <level>"
4614 " (<level> is 0-15)\n");
4617 return len;
4620 static int volume_write(char *buf)
4622 int cmos_cmd, inc, i;
4623 u8 level, mute;
4624 int new_level, new_mute;
4625 char *cmd;
4627 while ((cmd = next_cmd(&buf))) {
4628 if (!acpi_ec_read(volume_offset, &level))
4629 return -EIO;
4630 new_mute = mute = level & 0x40;
4631 new_level = level = level & 0xf;
4633 if (strlencmp(cmd, "up") == 0) {
4634 if (mute)
4635 new_mute = 0;
4636 else
4637 new_level = level == 15 ? 15 : level + 1;
4638 } else if (strlencmp(cmd, "down") == 0) {
4639 if (mute)
4640 new_mute = 0;
4641 else
4642 new_level = level == 0 ? 0 : level - 1;
4643 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4644 new_level >= 0 && new_level <= 15) {
4645 /* new_level set */
4646 } else if (strlencmp(cmd, "mute") == 0) {
4647 new_mute = 0x40;
4648 } else
4649 return -EINVAL;
4651 if (new_level != level) {
4652 /* mute doesn't change */
4654 cmos_cmd = (new_level > level) ?
4655 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4656 inc = new_level > level ? 1 : -1;
4658 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4659 !acpi_ec_write(volume_offset, level)))
4660 return -EIO;
4662 for (i = level; i != new_level; i += inc)
4663 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4664 !acpi_ec_write(volume_offset, i + inc))
4665 return -EIO;
4667 if (mute &&
4668 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4669 !acpi_ec_write(volume_offset, new_level + mute))) {
4670 return -EIO;
4674 if (new_mute != mute) {
4675 /* level doesn't change */
4677 cmos_cmd = (new_mute) ?
4678 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4680 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4681 !acpi_ec_write(volume_offset, level + new_mute))
4682 return -EIO;
4686 return 0;
4689 static struct ibm_struct volume_driver_data = {
4690 .name = "volume",
4691 .read = volume_read,
4692 .write = volume_write,
4695 /*************************************************************************
4696 * Fan subdriver
4700 * FAN ACCESS MODES
4702 * TPACPI_FAN_RD_ACPI_GFAN:
4703 * ACPI GFAN method: returns fan level
4705 * see TPACPI_FAN_WR_ACPI_SFAN
4706 * EC 0x2f (HFSP) not available if GFAN exists
4708 * TPACPI_FAN_WR_ACPI_SFAN:
4709 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4711 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4712 * it for writing.
4714 * TPACPI_FAN_WR_TPEC:
4715 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4716 * Supported on almost all ThinkPads
4718 * Fan speed changes of any sort (including those caused by the
4719 * disengaged mode) are usually done slowly by the firmware as the
4720 * maximum ammount of fan duty cycle change per second seems to be
4721 * limited.
4723 * Reading is not available if GFAN exists.
4724 * Writing is not available if SFAN exists.
4726 * Bits
4727 * 7 automatic mode engaged;
4728 * (default operation mode of the ThinkPad)
4729 * fan level is ignored in this mode.
4730 * 6 full speed mode (takes precedence over bit 7);
4731 * not available on all thinkpads. May disable
4732 * the tachometer while the fan controller ramps up
4733 * the speed (which can take up to a few *minutes*).
4734 * Speeds up fan to 100% duty-cycle, which is far above
4735 * the standard RPM levels. It is not impossible that
4736 * it could cause hardware damage.
4737 * 5-3 unused in some models. Extra bits for fan level
4738 * in others, but still useless as all values above
4739 * 7 map to the same speed as level 7 in these models.
4740 * 2-0 fan level (0..7 usually)
4741 * 0x00 = stop
4742 * 0x07 = max (set when temperatures critical)
4743 * Some ThinkPads may have other levels, see
4744 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4746 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4747 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4748 * does so, its initial value is meaningless (0x07).
4750 * For firmware bugs, refer to:
4751 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4753 * ----
4755 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4756 * Main fan tachometer reading (in RPM)
4758 * This register is present on all ThinkPads with a new-style EC, and
4759 * it is known not to be present on the A21m/e, and T22, as there is
4760 * something else in offset 0x84 according to the ACPI DSDT. Other
4761 * ThinkPads from this same time period (and earlier) probably lack the
4762 * tachometer as well.
4764 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4765 * was never fixed by IBM to report the EC firmware version string
4766 * probably support the tachometer (like the early X models), so
4767 * detecting it is quite hard. We need more data to know for sure.
4769 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4770 * might result.
4772 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4773 * mode.
4775 * For firmware bugs, refer to:
4776 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4778 * TPACPI_FAN_WR_ACPI_FANS:
4779 * ThinkPad X31, X40, X41. Not available in the X60.
4781 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4782 * high speed. ACPI DSDT seems to map these three speeds to levels
4783 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4784 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4786 * The speeds are stored on handles
4787 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4789 * There are three default speed sets, acessible as handles:
4790 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4792 * ACPI DSDT switches which set is in use depending on various
4793 * factors.
4795 * TPACPI_FAN_WR_TPEC is also available and should be used to
4796 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4797 * but the ACPI tables just mention level 7.
4800 enum { /* Fan control constants */
4801 fan_status_offset = 0x2f, /* EC register 0x2f */
4802 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4803 * 0x84 must be read before 0x85 */
4805 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4806 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4808 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4811 enum fan_status_access_mode {
4812 TPACPI_FAN_NONE = 0, /* No fan status or control */
4813 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4814 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4817 enum fan_control_access_mode {
4818 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4819 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4820 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4821 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4824 enum fan_control_commands {
4825 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4826 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4827 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4828 * and also watchdog cmd */
4831 static int fan_control_allowed;
4833 static enum fan_status_access_mode fan_status_access_mode;
4834 static enum fan_control_access_mode fan_control_access_mode;
4835 static enum fan_control_commands fan_control_commands;
4837 static u8 fan_control_initial_status;
4838 static u8 fan_control_desired_level;
4839 static int fan_watchdog_maxinterval;
4841 static struct mutex fan_mutex;
4843 static void fan_watchdog_fire(struct work_struct *ignored);
4844 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4846 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4847 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4848 "\\FSPD", /* 600e/x, 770e, 770x */
4849 ); /* all others */
4850 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4851 "JFNS", /* 770x-JL */
4852 ); /* all others */
4855 * Call with fan_mutex held
4857 static void fan_update_desired_level(u8 status)
4859 if ((status &
4860 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4861 if (status > 7)
4862 fan_control_desired_level = 7;
4863 else
4864 fan_control_desired_level = status;
4868 static int fan_get_status(u8 *status)
4870 u8 s;
4872 /* TODO:
4873 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4875 switch (fan_status_access_mode) {
4876 case TPACPI_FAN_RD_ACPI_GFAN:
4877 /* 570, 600e/x, 770e, 770x */
4879 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4880 return -EIO;
4882 if (likely(status))
4883 *status = s & 0x07;
4885 break;
4887 case TPACPI_FAN_RD_TPEC:
4888 /* all except 570, 600e/x, 770e, 770x */
4889 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4890 return -EIO;
4892 if (likely(status))
4893 *status = s;
4895 break;
4897 default:
4898 return -ENXIO;
4901 return 0;
4904 static int fan_get_status_safe(u8 *status)
4906 int rc;
4907 u8 s;
4909 if (mutex_lock_interruptible(&fan_mutex))
4910 return -ERESTARTSYS;
4911 rc = fan_get_status(&s);
4912 if (!rc)
4913 fan_update_desired_level(s);
4914 mutex_unlock(&fan_mutex);
4916 if (status)
4917 *status = s;
4919 return rc;
4922 static int fan_get_speed(unsigned int *speed)
4924 u8 hi, lo;
4926 switch (fan_status_access_mode) {
4927 case TPACPI_FAN_RD_TPEC:
4928 /* all except 570, 600e/x, 770e, 770x */
4929 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4930 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4931 return -EIO;
4933 if (likely(speed))
4934 *speed = (hi << 8) | lo;
4936 break;
4938 default:
4939 return -ENXIO;
4942 return 0;
4945 static int fan_set_level(int level)
4947 if (!fan_control_allowed)
4948 return -EPERM;
4950 switch (fan_control_access_mode) {
4951 case TPACPI_FAN_WR_ACPI_SFAN:
4952 if (level >= 0 && level <= 7) {
4953 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4954 return -EIO;
4955 } else
4956 return -EINVAL;
4957 break;
4959 case TPACPI_FAN_WR_ACPI_FANS:
4960 case TPACPI_FAN_WR_TPEC:
4961 if ((level != TP_EC_FAN_AUTO) &&
4962 (level != TP_EC_FAN_FULLSPEED) &&
4963 ((level < 0) || (level > 7)))
4964 return -EINVAL;
4966 /* safety net should the EC not support AUTO
4967 * or FULLSPEED mode bits and just ignore them */
4968 if (level & TP_EC_FAN_FULLSPEED)
4969 level |= 7; /* safety min speed 7 */
4970 else if (level & TP_EC_FAN_AUTO)
4971 level |= 4; /* safety min speed 4 */
4973 if (!acpi_ec_write(fan_status_offset, level))
4974 return -EIO;
4975 else
4976 tp_features.fan_ctrl_status_undef = 0;
4977 break;
4979 default:
4980 return -ENXIO;
4982 return 0;
4985 static int fan_set_level_safe(int level)
4987 int rc;
4989 if (!fan_control_allowed)
4990 return -EPERM;
4992 if (mutex_lock_interruptible(&fan_mutex))
4993 return -ERESTARTSYS;
4995 if (level == TPACPI_FAN_LAST_LEVEL)
4996 level = fan_control_desired_level;
4998 rc = fan_set_level(level);
4999 if (!rc)
5000 fan_update_desired_level(level);
5002 mutex_unlock(&fan_mutex);
5003 return rc;
5006 static int fan_set_enable(void)
5008 u8 s;
5009 int rc;
5011 if (!fan_control_allowed)
5012 return -EPERM;
5014 if (mutex_lock_interruptible(&fan_mutex))
5015 return -ERESTARTSYS;
5017 switch (fan_control_access_mode) {
5018 case TPACPI_FAN_WR_ACPI_FANS:
5019 case TPACPI_FAN_WR_TPEC:
5020 rc = fan_get_status(&s);
5021 if (rc < 0)
5022 break;
5024 /* Don't go out of emergency fan mode */
5025 if (s != 7) {
5026 s &= 0x07;
5027 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5030 if (!acpi_ec_write(fan_status_offset, s))
5031 rc = -EIO;
5032 else {
5033 tp_features.fan_ctrl_status_undef = 0;
5034 rc = 0;
5036 break;
5038 case TPACPI_FAN_WR_ACPI_SFAN:
5039 rc = fan_get_status(&s);
5040 if (rc < 0)
5041 break;
5043 s &= 0x07;
5045 /* Set fan to at least level 4 */
5046 s |= 4;
5048 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5049 rc = -EIO;
5050 else
5051 rc = 0;
5052 break;
5054 default:
5055 rc = -ENXIO;
5058 mutex_unlock(&fan_mutex);
5059 return rc;
5062 static int fan_set_disable(void)
5064 int rc;
5066 if (!fan_control_allowed)
5067 return -EPERM;
5069 if (mutex_lock_interruptible(&fan_mutex))
5070 return -ERESTARTSYS;
5072 rc = 0;
5073 switch (fan_control_access_mode) {
5074 case TPACPI_FAN_WR_ACPI_FANS:
5075 case TPACPI_FAN_WR_TPEC:
5076 if (!acpi_ec_write(fan_status_offset, 0x00))
5077 rc = -EIO;
5078 else {
5079 fan_control_desired_level = 0;
5080 tp_features.fan_ctrl_status_undef = 0;
5082 break;
5084 case TPACPI_FAN_WR_ACPI_SFAN:
5085 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5086 rc = -EIO;
5087 else
5088 fan_control_desired_level = 0;
5089 break;
5091 default:
5092 rc = -ENXIO;
5096 mutex_unlock(&fan_mutex);
5097 return rc;
5100 static int fan_set_speed(int speed)
5102 int rc;
5104 if (!fan_control_allowed)
5105 return -EPERM;
5107 if (mutex_lock_interruptible(&fan_mutex))
5108 return -ERESTARTSYS;
5110 rc = 0;
5111 switch (fan_control_access_mode) {
5112 case TPACPI_FAN_WR_ACPI_FANS:
5113 if (speed >= 0 && speed <= 65535) {
5114 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5115 speed, speed, speed))
5116 rc = -EIO;
5117 } else
5118 rc = -EINVAL;
5119 break;
5121 default:
5122 rc = -ENXIO;
5125 mutex_unlock(&fan_mutex);
5126 return rc;
5129 static void fan_watchdog_reset(void)
5131 static int fan_watchdog_active;
5133 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5134 return;
5136 if (fan_watchdog_active)
5137 cancel_delayed_work(&fan_watchdog_task);
5139 if (fan_watchdog_maxinterval > 0 &&
5140 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5141 fan_watchdog_active = 1;
5142 if (!schedule_delayed_work(&fan_watchdog_task,
5143 msecs_to_jiffies(fan_watchdog_maxinterval
5144 * 1000))) {
5145 printk(TPACPI_ERR
5146 "failed to schedule the fan watchdog, "
5147 "watchdog will not trigger\n");
5149 } else
5150 fan_watchdog_active = 0;
5153 static void fan_watchdog_fire(struct work_struct *ignored)
5155 int rc;
5157 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5158 return;
5160 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5161 rc = fan_set_enable();
5162 if (rc < 0) {
5163 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5164 "will try again later...\n", -rc);
5165 /* reschedule for later */
5166 fan_watchdog_reset();
5171 * SYSFS fan layout: hwmon compatible (device)
5173 * pwm*_enable:
5174 * 0: "disengaged" mode
5175 * 1: manual mode
5176 * 2: native EC "auto" mode (recommended, hardware default)
5178 * pwm*: set speed in manual mode, ignored otherwise.
5179 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5180 * interpolation.
5182 * fan*_input: tachometer reading, RPM
5185 * SYSFS fan layout: extensions
5187 * fan_watchdog (driver):
5188 * fan watchdog interval in seconds, 0 disables (default), max 120
5191 /* sysfs fan pwm1_enable ----------------------------------------------- */
5192 static ssize_t fan_pwm1_enable_show(struct device *dev,
5193 struct device_attribute *attr,
5194 char *buf)
5196 int res, mode;
5197 u8 status;
5199 res = fan_get_status_safe(&status);
5200 if (res)
5201 return res;
5203 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5204 if (status != fan_control_initial_status) {
5205 tp_features.fan_ctrl_status_undef = 0;
5206 } else {
5207 /* Return most likely status. In fact, it
5208 * might be the only possible status */
5209 status = TP_EC_FAN_AUTO;
5213 if (status & TP_EC_FAN_FULLSPEED) {
5214 mode = 0;
5215 } else if (status & TP_EC_FAN_AUTO) {
5216 mode = 2;
5217 } else
5218 mode = 1;
5220 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5223 static ssize_t fan_pwm1_enable_store(struct device *dev,
5224 struct device_attribute *attr,
5225 const char *buf, size_t count)
5227 unsigned long t;
5228 int res, level;
5230 if (parse_strtoul(buf, 2, &t))
5231 return -EINVAL;
5233 switch (t) {
5234 case 0:
5235 level = TP_EC_FAN_FULLSPEED;
5236 break;
5237 case 1:
5238 level = TPACPI_FAN_LAST_LEVEL;
5239 break;
5240 case 2:
5241 level = TP_EC_FAN_AUTO;
5242 break;
5243 case 3:
5244 /* reserved for software-controlled auto mode */
5245 return -ENOSYS;
5246 default:
5247 return -EINVAL;
5250 res = fan_set_level_safe(level);
5251 if (res == -ENXIO)
5252 return -EINVAL;
5253 else if (res < 0)
5254 return res;
5256 fan_watchdog_reset();
5258 return count;
5261 static struct device_attribute dev_attr_fan_pwm1_enable =
5262 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5263 fan_pwm1_enable_show, fan_pwm1_enable_store);
5265 /* sysfs fan pwm1 ------------------------------------------------------ */
5266 static ssize_t fan_pwm1_show(struct device *dev,
5267 struct device_attribute *attr,
5268 char *buf)
5270 int res;
5271 u8 status;
5273 res = fan_get_status_safe(&status);
5274 if (res)
5275 return res;
5277 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5278 if (status != fan_control_initial_status) {
5279 tp_features.fan_ctrl_status_undef = 0;
5280 } else {
5281 status = TP_EC_FAN_AUTO;
5285 if ((status &
5286 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5287 status = fan_control_desired_level;
5289 if (status > 7)
5290 status = 7;
5292 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5295 static ssize_t fan_pwm1_store(struct device *dev,
5296 struct device_attribute *attr,
5297 const char *buf, size_t count)
5299 unsigned long s;
5300 int rc;
5301 u8 status, newlevel;
5303 if (parse_strtoul(buf, 255, &s))
5304 return -EINVAL;
5306 /* scale down from 0-255 to 0-7 */
5307 newlevel = (s >> 5) & 0x07;
5309 if (mutex_lock_interruptible(&fan_mutex))
5310 return -ERESTARTSYS;
5312 rc = fan_get_status(&status);
5313 if (!rc && (status &
5314 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5315 rc = fan_set_level(newlevel);
5316 if (rc == -ENXIO)
5317 rc = -EINVAL;
5318 else if (!rc) {
5319 fan_update_desired_level(newlevel);
5320 fan_watchdog_reset();
5324 mutex_unlock(&fan_mutex);
5325 return (rc)? rc : count;
5328 static struct device_attribute dev_attr_fan_pwm1 =
5329 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5330 fan_pwm1_show, fan_pwm1_store);
5332 /* sysfs fan fan1_input ------------------------------------------------ */
5333 static ssize_t fan_fan1_input_show(struct device *dev,
5334 struct device_attribute *attr,
5335 char *buf)
5337 int res;
5338 unsigned int speed;
5340 res = fan_get_speed(&speed);
5341 if (res < 0)
5342 return res;
5344 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5347 static struct device_attribute dev_attr_fan_fan1_input =
5348 __ATTR(fan1_input, S_IRUGO,
5349 fan_fan1_input_show, NULL);
5351 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5352 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5353 char *buf)
5355 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5358 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5359 const char *buf, size_t count)
5361 unsigned long t;
5363 if (parse_strtoul(buf, 120, &t))
5364 return -EINVAL;
5366 if (!fan_control_allowed)
5367 return -EPERM;
5369 fan_watchdog_maxinterval = t;
5370 fan_watchdog_reset();
5372 return count;
5375 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5376 fan_fan_watchdog_show, fan_fan_watchdog_store);
5378 /* --------------------------------------------------------------------- */
5379 static struct attribute *fan_attributes[] = {
5380 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5381 &dev_attr_fan_fan1_input.attr,
5382 NULL
5385 static const struct attribute_group fan_attr_group = {
5386 .attrs = fan_attributes,
5389 static int __init fan_init(struct ibm_init_struct *iibm)
5391 int rc;
5393 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5395 mutex_init(&fan_mutex);
5396 fan_status_access_mode = TPACPI_FAN_NONE;
5397 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5398 fan_control_commands = 0;
5399 fan_watchdog_maxinterval = 0;
5400 tp_features.fan_ctrl_status_undef = 0;
5401 fan_control_desired_level = 7;
5403 TPACPI_ACPIHANDLE_INIT(fans);
5404 TPACPI_ACPIHANDLE_INIT(gfan);
5405 TPACPI_ACPIHANDLE_INIT(sfan);
5407 if (gfan_handle) {
5408 /* 570, 600e/x, 770e, 770x */
5409 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5410 } else {
5411 /* all other ThinkPads: note that even old-style
5412 * ThinkPad ECs supports the fan control register */
5413 if (likely(acpi_ec_read(fan_status_offset,
5414 &fan_control_initial_status))) {
5415 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5417 /* In some ThinkPads, neither the EC nor the ACPI
5418 * DSDT initialize the fan status, and it ends up
5419 * being set to 0x07 when it *could* be either
5420 * 0x07 or 0x80.
5422 * Enable for TP-1Y (T43), TP-78 (R51e),
5423 * TP-76 (R52), TP-70 (T43, R52), which are known
5424 * to be buggy. */
5425 if (fan_control_initial_status == 0x07) {
5426 switch (thinkpad_id.ec_model) {
5427 case 0x5931: /* TP-1Y */
5428 case 0x3837: /* TP-78 */
5429 case 0x3637: /* TP-76 */
5430 case 0x3037: /* TP-70 */
5431 printk(TPACPI_NOTICE
5432 "fan_init: initial fan status "
5433 "is unknown, assuming it is "
5434 "in auto mode\n");
5435 tp_features.fan_ctrl_status_undef = 1;
5439 } else {
5440 printk(TPACPI_ERR
5441 "ThinkPad ACPI EC access misbehaving, "
5442 "fan status and control unavailable\n");
5443 return 1;
5447 if (sfan_handle) {
5448 /* 570, 770x-JL */
5449 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5450 fan_control_commands |=
5451 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5452 } else {
5453 if (!gfan_handle) {
5454 /* gfan without sfan means no fan control */
5455 /* all other models implement TP EC 0x2f control */
5457 if (fans_handle) {
5458 /* X31, X40, X41 */
5459 fan_control_access_mode =
5460 TPACPI_FAN_WR_ACPI_FANS;
5461 fan_control_commands |=
5462 TPACPI_FAN_CMD_SPEED |
5463 TPACPI_FAN_CMD_LEVEL |
5464 TPACPI_FAN_CMD_ENABLE;
5465 } else {
5466 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5467 fan_control_commands |=
5468 TPACPI_FAN_CMD_LEVEL |
5469 TPACPI_FAN_CMD_ENABLE;
5474 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5475 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5476 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5477 fan_status_access_mode, fan_control_access_mode);
5479 /* fan control master switch */
5480 if (!fan_control_allowed) {
5481 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5482 fan_control_commands = 0;
5483 dbg_printk(TPACPI_DBG_INIT,
5484 "fan control features disabled by parameter\n");
5487 /* update fan_control_desired_level */
5488 if (fan_status_access_mode != TPACPI_FAN_NONE)
5489 fan_get_status_safe(NULL);
5491 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5492 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5493 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5494 &fan_attr_group);
5495 if (!(rc < 0))
5496 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5497 &driver_attr_fan_watchdog);
5498 if (rc < 0)
5499 return rc;
5500 return 0;
5501 } else
5502 return 1;
5505 static void fan_exit(void)
5507 vdbg_printk(TPACPI_DBG_EXIT,
5508 "cancelling any pending fan watchdog tasks\n");
5510 /* FIXME: can we really do this unconditionally? */
5511 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5512 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5513 &driver_attr_fan_watchdog);
5515 cancel_delayed_work(&fan_watchdog_task);
5516 flush_scheduled_work();
5519 static int fan_read(char *p)
5521 int len = 0;
5522 int rc;
5523 u8 status;
5524 unsigned int speed = 0;
5526 switch (fan_status_access_mode) {
5527 case TPACPI_FAN_RD_ACPI_GFAN:
5528 /* 570, 600e/x, 770e, 770x */
5529 rc = fan_get_status_safe(&status);
5530 if (rc < 0)
5531 return rc;
5533 len += sprintf(p + len, "status:\t\t%s\n"
5534 "level:\t\t%d\n",
5535 (status != 0) ? "enabled" : "disabled", status);
5536 break;
5538 case TPACPI_FAN_RD_TPEC:
5539 /* all except 570, 600e/x, 770e, 770x */
5540 rc = fan_get_status_safe(&status);
5541 if (rc < 0)
5542 return rc;
5544 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5545 if (status != fan_control_initial_status)
5546 tp_features.fan_ctrl_status_undef = 0;
5547 else
5548 /* Return most likely status. In fact, it
5549 * might be the only possible status */
5550 status = TP_EC_FAN_AUTO;
5553 len += sprintf(p + len, "status:\t\t%s\n",
5554 (status != 0) ? "enabled" : "disabled");
5556 rc = fan_get_speed(&speed);
5557 if (rc < 0)
5558 return rc;
5560 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5562 if (status & TP_EC_FAN_FULLSPEED)
5563 /* Disengaged mode takes precedence */
5564 len += sprintf(p + len, "level:\t\tdisengaged\n");
5565 else if (status & TP_EC_FAN_AUTO)
5566 len += sprintf(p + len, "level:\t\tauto\n");
5567 else
5568 len += sprintf(p + len, "level:\t\t%d\n", status);
5569 break;
5571 case TPACPI_FAN_NONE:
5572 default:
5573 len += sprintf(p + len, "status:\t\tnot supported\n");
5576 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5577 len += sprintf(p + len, "commands:\tlevel <level>");
5579 switch (fan_control_access_mode) {
5580 case TPACPI_FAN_WR_ACPI_SFAN:
5581 len += sprintf(p + len, " (<level> is 0-7)\n");
5582 break;
5584 default:
5585 len += sprintf(p + len, " (<level> is 0-7, "
5586 "auto, disengaged, full-speed)\n");
5587 break;
5591 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5592 len += sprintf(p + len, "commands:\tenable, disable\n"
5593 "commands:\twatchdog <timeout> (<timeout> "
5594 "is 0 (off), 1-120 (seconds))\n");
5596 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5597 len += sprintf(p + len, "commands:\tspeed <speed>"
5598 " (<speed> is 0-65535)\n");
5600 return len;
5603 static int fan_write_cmd_level(const char *cmd, int *rc)
5605 int level;
5607 if (strlencmp(cmd, "level auto") == 0)
5608 level = TP_EC_FAN_AUTO;
5609 else if ((strlencmp(cmd, "level disengaged") == 0) |
5610 (strlencmp(cmd, "level full-speed") == 0))
5611 level = TP_EC_FAN_FULLSPEED;
5612 else if (sscanf(cmd, "level %d", &level) != 1)
5613 return 0;
5615 *rc = fan_set_level_safe(level);
5616 if (*rc == -ENXIO)
5617 printk(TPACPI_ERR "level command accepted for unsupported "
5618 "access mode %d", fan_control_access_mode);
5620 return 1;
5623 static int fan_write_cmd_enable(const char *cmd, int *rc)
5625 if (strlencmp(cmd, "enable") != 0)
5626 return 0;
5628 *rc = fan_set_enable();
5629 if (*rc == -ENXIO)
5630 printk(TPACPI_ERR "enable command accepted for unsupported "
5631 "access mode %d", fan_control_access_mode);
5633 return 1;
5636 static int fan_write_cmd_disable(const char *cmd, int *rc)
5638 if (strlencmp(cmd, "disable") != 0)
5639 return 0;
5641 *rc = fan_set_disable();
5642 if (*rc == -ENXIO)
5643 printk(TPACPI_ERR "disable command accepted for unsupported "
5644 "access mode %d", fan_control_access_mode);
5646 return 1;
5649 static int fan_write_cmd_speed(const char *cmd, int *rc)
5651 int speed;
5653 /* TODO:
5654 * Support speed <low> <medium> <high> ? */
5656 if (sscanf(cmd, "speed %d", &speed) != 1)
5657 return 0;
5659 *rc = fan_set_speed(speed);
5660 if (*rc == -ENXIO)
5661 printk(TPACPI_ERR "speed command accepted for unsupported "
5662 "access mode %d", fan_control_access_mode);
5664 return 1;
5667 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5669 int interval;
5671 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5672 return 0;
5674 if (interval < 0 || interval > 120)
5675 *rc = -EINVAL;
5676 else
5677 fan_watchdog_maxinterval = interval;
5679 return 1;
5682 static int fan_write(char *buf)
5684 char *cmd;
5685 int rc = 0;
5687 while (!rc && (cmd = next_cmd(&buf))) {
5688 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5689 fan_write_cmd_level(cmd, &rc)) &&
5690 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5691 (fan_write_cmd_enable(cmd, &rc) ||
5692 fan_write_cmd_disable(cmd, &rc) ||
5693 fan_write_cmd_watchdog(cmd, &rc))) &&
5694 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5695 fan_write_cmd_speed(cmd, &rc))
5697 rc = -EINVAL;
5698 else if (!rc)
5699 fan_watchdog_reset();
5702 return rc;
5705 static struct ibm_struct fan_driver_data = {
5706 .name = "fan",
5707 .read = fan_read,
5708 .write = fan_write,
5709 .exit = fan_exit,
5712 /****************************************************************************
5713 ****************************************************************************
5715 * Infrastructure
5717 ****************************************************************************
5718 ****************************************************************************/
5720 /* sysfs name ---------------------------------------------------------- */
5721 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5722 struct device_attribute *attr,
5723 char *buf)
5725 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5728 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5729 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5731 /* --------------------------------------------------------------------- */
5733 /* /proc support */
5734 static struct proc_dir_entry *proc_dir;
5737 * Module and infrastructure proble, init and exit handling
5740 static int force_load;
5742 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5743 static const char * __init str_supported(int is_supported)
5745 static char text_unsupported[] __initdata = "not supported";
5747 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5749 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5751 static void ibm_exit(struct ibm_struct *ibm)
5753 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5755 list_del_init(&ibm->all_drivers);
5757 if (ibm->flags.acpi_notify_installed) {
5758 dbg_printk(TPACPI_DBG_EXIT,
5759 "%s: acpi_remove_notify_handler\n", ibm->name);
5760 BUG_ON(!ibm->acpi);
5761 acpi_remove_notify_handler(*ibm->acpi->handle,
5762 ibm->acpi->type,
5763 dispatch_acpi_notify);
5764 ibm->flags.acpi_notify_installed = 0;
5765 ibm->flags.acpi_notify_installed = 0;
5768 if (ibm->flags.proc_created) {
5769 dbg_printk(TPACPI_DBG_EXIT,
5770 "%s: remove_proc_entry\n", ibm->name);
5771 remove_proc_entry(ibm->name, proc_dir);
5772 ibm->flags.proc_created = 0;
5775 if (ibm->flags.acpi_driver_registered) {
5776 dbg_printk(TPACPI_DBG_EXIT,
5777 "%s: acpi_bus_unregister_driver\n", ibm->name);
5778 BUG_ON(!ibm->acpi);
5779 acpi_bus_unregister_driver(ibm->acpi->driver);
5780 kfree(ibm->acpi->driver);
5781 ibm->acpi->driver = NULL;
5782 ibm->flags.acpi_driver_registered = 0;
5785 if (ibm->flags.init_called && ibm->exit) {
5786 ibm->exit();
5787 ibm->flags.init_called = 0;
5790 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5793 static int __init ibm_init(struct ibm_init_struct *iibm)
5795 int ret;
5796 struct ibm_struct *ibm = iibm->data;
5797 struct proc_dir_entry *entry;
5799 BUG_ON(ibm == NULL);
5801 INIT_LIST_HEAD(&ibm->all_drivers);
5803 if (ibm->flags.experimental && !experimental)
5804 return 0;
5806 dbg_printk(TPACPI_DBG_INIT,
5807 "probing for %s\n", ibm->name);
5809 if (iibm->init) {
5810 ret = iibm->init(iibm);
5811 if (ret > 0)
5812 return 0; /* probe failed */
5813 if (ret)
5814 return ret;
5816 ibm->flags.init_called = 1;
5819 if (ibm->acpi) {
5820 if (ibm->acpi->hid) {
5821 ret = register_tpacpi_subdriver(ibm);
5822 if (ret)
5823 goto err_out;
5826 if (ibm->acpi->notify) {
5827 ret = setup_acpi_notify(ibm);
5828 if (ret == -ENODEV) {
5829 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5830 ibm->name);
5831 ret = 0;
5832 goto err_out;
5834 if (ret < 0)
5835 goto err_out;
5839 dbg_printk(TPACPI_DBG_INIT,
5840 "%s installed\n", ibm->name);
5842 if (ibm->read) {
5843 entry = create_proc_entry(ibm->name,
5844 S_IFREG | S_IRUGO | S_IWUSR,
5845 proc_dir);
5846 if (!entry) {
5847 printk(TPACPI_ERR "unable to create proc entry %s\n",
5848 ibm->name);
5849 ret = -ENODEV;
5850 goto err_out;
5852 entry->owner = THIS_MODULE;
5853 entry->data = ibm;
5854 entry->read_proc = &dispatch_procfs_read;
5855 if (ibm->write)
5856 entry->write_proc = &dispatch_procfs_write;
5857 ibm->flags.proc_created = 1;
5860 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5862 return 0;
5864 err_out:
5865 dbg_printk(TPACPI_DBG_INIT,
5866 "%s: at error exit path with result %d\n",
5867 ibm->name, ret);
5869 ibm_exit(ibm);
5870 return (ret < 0)? ret : 0;
5873 /* Probing */
5875 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5877 const struct dmi_device *dev = NULL;
5878 char ec_fw_string[18];
5880 if (!tp)
5881 return;
5883 memset(tp, 0, sizeof(*tp));
5885 if (dmi_name_in_vendors("IBM"))
5886 tp->vendor = PCI_VENDOR_ID_IBM;
5887 else if (dmi_name_in_vendors("LENOVO"))
5888 tp->vendor = PCI_VENDOR_ID_LENOVO;
5889 else
5890 return;
5892 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5893 GFP_KERNEL);
5894 if (!tp->bios_version_str)
5895 return;
5896 tp->bios_model = tp->bios_version_str[0]
5897 | (tp->bios_version_str[1] << 8);
5900 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5901 * X32 or newer, all Z series; Some models must have an
5902 * up-to-date BIOS or they will not be detected.
5904 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5906 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5907 if (sscanf(dev->name,
5908 "IBM ThinkPad Embedded Controller -[%17c",
5909 ec_fw_string) == 1) {
5910 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5911 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5913 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5914 tp->ec_model = ec_fw_string[0]
5915 | (ec_fw_string[1] << 8);
5916 break;
5920 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5921 GFP_KERNEL);
5922 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5923 kfree(tp->model_str);
5924 tp->model_str = NULL;
5928 static int __init probe_for_thinkpad(void)
5930 int is_thinkpad;
5932 if (acpi_disabled)
5933 return -ENODEV;
5936 * Non-ancient models have better DMI tagging, but very old models
5937 * don't.
5939 is_thinkpad = (thinkpad_id.model_str != NULL);
5941 /* ec is required because many other handles are relative to it */
5942 TPACPI_ACPIHANDLE_INIT(ec);
5943 if (!ec_handle) {
5944 if (is_thinkpad)
5945 printk(TPACPI_ERR
5946 "Not yet supported ThinkPad detected!\n");
5947 return -ENODEV;
5951 * Risks a regression on very old machines, but reduces potential
5952 * false positives a damn great deal
5954 if (!is_thinkpad)
5955 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5957 if (!is_thinkpad && !force_load)
5958 return -ENODEV;
5960 return 0;
5964 /* Module init, exit, parameters */
5966 static struct ibm_init_struct ibms_init[] __initdata = {
5968 .init = thinkpad_acpi_driver_init,
5969 .data = &thinkpad_acpi_driver_data,
5972 .init = hotkey_init,
5973 .data = &hotkey_driver_data,
5976 .init = bluetooth_init,
5977 .data = &bluetooth_driver_data,
5980 .init = wan_init,
5981 .data = &wan_driver_data,
5983 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
5984 =======
5985 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5986 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
5988 .init = video_init,
5989 .data = &video_driver_data,
5991 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
5992 =======
5993 #endif
5994 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
5996 .init = light_init,
5997 .data = &light_driver_data,
5999 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6001 .init = dock_init,
6002 .data = &dock_driver_data[0],
6005 .init = dock_init2,
6006 .data = &dock_driver_data[1],
6008 #endif
6009 #ifdef CONFIG_THINKPAD_ACPI_BAY
6011 .init = bay_init,
6012 .data = &bay_driver_data,
6014 #endif
6016 .init = cmos_init,
6017 .data = &cmos_driver_data,
6020 .init = led_init,
6021 .data = &led_driver_data,
6024 .init = beep_init,
6025 .data = &beep_driver_data,
6028 .init = thermal_init,
6029 .data = &thermal_driver_data,
6032 .data = &ecdump_driver_data,
6035 .init = brightness_init,
6036 .data = &brightness_driver_data,
6039 .data = &volume_driver_data,
6042 .init = fan_init,
6043 .data = &fan_driver_data,
6047 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6049 unsigned int i;
6050 struct ibm_struct *ibm;
6052 if (!kp || !kp->name || !val)
6053 return -EINVAL;
6055 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6056 ibm = ibms_init[i].data;
6057 WARN_ON(ibm == NULL);
6059 if (!ibm || !ibm->name)
6060 continue;
6062 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6063 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6064 return -ENOSPC;
6065 strcpy(ibms_init[i].param, val);
6066 strcat(ibms_init[i].param, ",");
6067 return 0;
6071 return -EINVAL;
6074 module_param(experimental, int, 0);
6075 MODULE_PARM_DESC(experimental,
6076 "Enables experimental features when non-zero");
6078 module_param_named(debug, dbg_level, uint, 0);
6079 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6081 module_param(force_load, bool, 0);
6082 MODULE_PARM_DESC(force_load,
6083 "Attempts to load the driver even on a "
6084 "mis-identified ThinkPad when true");
6086 module_param_named(fan_control, fan_control_allowed, bool, 0);
6087 MODULE_PARM_DESC(fan_control,
6088 "Enables setting fan parameters features when true");
6090 module_param_named(brightness_mode, brightness_mode, int, 0);
6091 MODULE_PARM_DESC(brightness_mode,
6092 "Selects brightness control strategy: "
6093 "0=auto, 1=EC, 2=CMOS, 3=both");
6095 module_param(brightness_enable, uint, 0);
6096 MODULE_PARM_DESC(brightness_enable,
6097 "Enables backlight control when 1, disables when 0");
6099 module_param(hotkey_report_mode, uint, 0);
6100 MODULE_PARM_DESC(hotkey_report_mode,
6101 "used for backwards compatibility with userspace, "
6102 "see documentation");
6104 #define TPACPI_PARAM(feature) \
6105 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6106 <<<<<<< HEAD:drivers/misc/thinkpad_acpi.c
6107 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
6108 =======
6109 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6110 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/misc/thinkpad_acpi.c
6111 "at module load, see documentation")
6113 TPACPI_PARAM(hotkey);
6114 TPACPI_PARAM(bluetooth);
6115 TPACPI_PARAM(video);
6116 TPACPI_PARAM(light);
6117 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6118 TPACPI_PARAM(dock);
6119 #endif
6120 #ifdef CONFIG_THINKPAD_ACPI_BAY
6121 TPACPI_PARAM(bay);
6122 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6123 TPACPI_PARAM(cmos);
6124 TPACPI_PARAM(led);
6125 TPACPI_PARAM(beep);
6126 TPACPI_PARAM(ecdump);
6127 TPACPI_PARAM(brightness);
6128 TPACPI_PARAM(volume);
6129 TPACPI_PARAM(fan);
6131 static void thinkpad_acpi_module_exit(void)
6133 struct ibm_struct *ibm, *itmp;
6135 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6137 list_for_each_entry_safe_reverse(ibm, itmp,
6138 &tpacpi_all_drivers,
6139 all_drivers) {
6140 ibm_exit(ibm);
6143 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6145 if (tpacpi_inputdev) {
6146 if (tp_features.input_device_registered)
6147 input_unregister_device(tpacpi_inputdev);
6148 else
6149 input_free_device(tpacpi_inputdev);
6152 if (tpacpi_hwmon)
6153 hwmon_device_unregister(tpacpi_hwmon);
6155 if (tp_features.sensors_pdev_attrs_registered)
6156 device_remove_file(&tpacpi_sensors_pdev->dev,
6157 &dev_attr_thinkpad_acpi_pdev_name);
6158 if (tpacpi_sensors_pdev)
6159 platform_device_unregister(tpacpi_sensors_pdev);
6160 if (tpacpi_pdev)
6161 platform_device_unregister(tpacpi_pdev);
6163 if (tp_features.sensors_pdrv_attrs_registered)
6164 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6165 if (tp_features.platform_drv_attrs_registered)
6166 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6168 if (tp_features.sensors_pdrv_registered)
6169 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6171 if (tp_features.platform_drv_registered)
6172 platform_driver_unregister(&tpacpi_pdriver);
6174 if (proc_dir)
6175 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6177 kfree(thinkpad_id.bios_version_str);
6178 kfree(thinkpad_id.ec_version_str);
6179 kfree(thinkpad_id.model_str);
6183 static int __init thinkpad_acpi_module_init(void)
6185 int ret, i;
6187 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6189 /* Parameter checking */
6190 if (hotkey_report_mode > 2)
6191 return -EINVAL;
6193 /* Driver-level probe */
6195 get_thinkpad_model_data(&thinkpad_id);
6196 ret = probe_for_thinkpad();
6197 if (ret) {
6198 thinkpad_acpi_module_exit();
6199 return ret;
6202 /* Driver initialization */
6204 TPACPI_ACPIHANDLE_INIT(ecrd);
6205 TPACPI_ACPIHANDLE_INIT(ecwr);
6207 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6208 if (!proc_dir) {
6209 printk(TPACPI_ERR
6210 "unable to create proc dir " TPACPI_PROC_DIR);
6211 thinkpad_acpi_module_exit();
6212 return -ENODEV;
6214 proc_dir->owner = THIS_MODULE;
6216 ret = platform_driver_register(&tpacpi_pdriver);
6217 if (ret) {
6218 printk(TPACPI_ERR
6219 "unable to register main platform driver\n");
6220 thinkpad_acpi_module_exit();
6221 return ret;
6223 tp_features.platform_drv_registered = 1;
6225 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6226 if (ret) {
6227 printk(TPACPI_ERR
6228 "unable to register hwmon platform driver\n");
6229 thinkpad_acpi_module_exit();
6230 return ret;
6232 tp_features.sensors_pdrv_registered = 1;
6234 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6235 if (!ret) {
6236 tp_features.platform_drv_attrs_registered = 1;
6237 ret = tpacpi_create_driver_attributes(
6238 &tpacpi_hwmon_pdriver.driver);
6240 if (ret) {
6241 printk(TPACPI_ERR
6242 "unable to create sysfs driver attributes\n");
6243 thinkpad_acpi_module_exit();
6244 return ret;
6246 tp_features.sensors_pdrv_attrs_registered = 1;
6249 /* Device initialization */
6250 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6251 NULL, 0);
6252 if (IS_ERR(tpacpi_pdev)) {
6253 ret = PTR_ERR(tpacpi_pdev);
6254 tpacpi_pdev = NULL;
6255 printk(TPACPI_ERR "unable to register platform device\n");
6256 thinkpad_acpi_module_exit();
6257 return ret;
6259 tpacpi_sensors_pdev = platform_device_register_simple(
6260 TPACPI_HWMON_DRVR_NAME,
6261 -1, NULL, 0);
6262 if (IS_ERR(tpacpi_sensors_pdev)) {
6263 ret = PTR_ERR(tpacpi_sensors_pdev);
6264 tpacpi_sensors_pdev = NULL;
6265 printk(TPACPI_ERR
6266 "unable to register hwmon platform device\n");
6267 thinkpad_acpi_module_exit();
6268 return ret;
6270 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6271 &dev_attr_thinkpad_acpi_pdev_name);
6272 if (ret) {
6273 printk(TPACPI_ERR
6274 "unable to create sysfs hwmon device attributes\n");
6275 thinkpad_acpi_module_exit();
6276 return ret;
6278 tp_features.sensors_pdev_attrs_registered = 1;
6279 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6280 if (IS_ERR(tpacpi_hwmon)) {
6281 ret = PTR_ERR(tpacpi_hwmon);
6282 tpacpi_hwmon = NULL;
6283 printk(TPACPI_ERR "unable to register hwmon device\n");
6284 thinkpad_acpi_module_exit();
6285 return ret;
6287 mutex_init(&tpacpi_inputdev_send_mutex);
6288 tpacpi_inputdev = input_allocate_device();
6289 if (!tpacpi_inputdev) {
6290 printk(TPACPI_ERR "unable to allocate input device\n");
6291 thinkpad_acpi_module_exit();
6292 return -ENOMEM;
6293 } else {
6294 /* Prepare input device, but don't register */
6295 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6296 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6297 tpacpi_inputdev->id.bustype = BUS_HOST;
6298 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6299 thinkpad_id.vendor :
6300 PCI_VENDOR_ID_IBM;
6301 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6302 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6304 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6305 ret = ibm_init(&ibms_init[i]);
6306 if (ret >= 0 && *ibms_init[i].param)
6307 ret = ibms_init[i].data->write(ibms_init[i].param);
6308 if (ret < 0) {
6309 thinkpad_acpi_module_exit();
6310 return ret;
6313 ret = input_register_device(tpacpi_inputdev);
6314 if (ret < 0) {
6315 printk(TPACPI_ERR "unable to register input device\n");
6316 thinkpad_acpi_module_exit();
6317 return ret;
6318 } else {
6319 tp_features.input_device_registered = 1;
6322 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6323 return 0;
6326 /* Please remove this in year 2009 */
6327 MODULE_ALIAS("ibm_acpi");
6330 * DMI matching for module autoloading
6332 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6333 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6335 * Only models listed in thinkwiki will be supported, so add yours
6336 * if it is not there yet.
6338 #define IBM_BIOS_MODULE_ALIAS(__type) \
6339 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6341 /* Non-ancient thinkpads */
6342 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6343 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6345 /* Ancient thinkpad BIOSes have to be identified by
6346 * BIOS type or model number, and there are far less
6347 * BIOS types than model numbers... */
6348 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6349 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6350 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6352 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6353 MODULE_DESCRIPTION(TPACPI_DESC);
6354 MODULE_VERSION(TPACPI_VERSION);
6355 MODULE_LICENSE("GPL");
6357 module_init(thinkpad_acpi_module_init);
6358 module_exit(thinkpad_acpi_module_exit);