ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobc24a043a179d9ebe337fcfa44330ec39267c6440
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.19-20080213"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
98 /* NVRAM bit masks */
99 enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
124 * Main driver
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
172 struct ibm_struct;
174 struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
184 struct ibm_struct {
185 char *name;
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
206 struct ibm_init_struct {
207 char param[32];
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219 #endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 hotkey_tablet:1;
225 u32 light:1;
226 u32 light_status:1;
227 u32 bright_16levels:1;
228 u32 bright_acpimode:1;
229 u32 wan:1;
230 u32 fan_ctrl_status_undef:1;
231 u32 input_device_registered:1;
232 u32 platform_drv_registered:1;
233 u32 platform_drv_attrs_registered:1;
234 u32 sensors_pdrv_registered:1;
235 u32 sensors_pdrv_attrs_registered:1;
236 u32 sensors_pdev_attrs_registered:1;
237 u32 hotkey_poll_active:1;
238 } tp_features;
240 struct thinkpad_id_data {
241 unsigned int vendor; /* ThinkPad vendor:
242 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
244 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
245 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
247 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
248 u16 ec_model;
250 char *model_str;
252 static struct thinkpad_id_data thinkpad_id;
254 static enum {
255 TPACPI_LIFE_INIT = 0,
256 TPACPI_LIFE_RUNNING,
257 TPACPI_LIFE_EXITING,
258 } tpacpi_lifecycle;
260 static int experimental;
261 static u32 dbg_level;
263 /****************************************************************************
264 ****************************************************************************
266 * ACPI Helpers and device model
268 ****************************************************************************
269 ****************************************************************************/
271 /*************************************************************************
272 * ACPI basic handles
275 static acpi_handle root_handle;
277 #define TPACPI_HANDLE(object, parent, paths...) \
278 static acpi_handle object##_handle; \
279 static acpi_handle *object##_parent = &parent##_handle; \
280 static char *object##_path; \
281 static char *object##_paths[] = { paths }
283 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
284 "\\_SB.PCI.ISA.EC", /* 570 */
285 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
286 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
287 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
288 "\\_SB.PCI0.ICH3.EC0", /* R31 */
289 "\\_SB.PCI0.LPC.EC", /* all others */
292 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
293 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
295 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
296 /* T4x, X31, X40 */
297 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
298 "\\CMS", /* R40, R40e */
299 ); /* all others */
301 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
302 "^HKEY", /* R30, R31 */
303 "HKEY", /* all others */
304 ); /* 570 */
306 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
307 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
308 "\\_SB.PCI0.VID0", /* 770e */
309 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
310 "\\_SB.PCI0.AGP.VID", /* all others */
311 ); /* R30, R31 */
314 /*************************************************************************
315 * ACPI helpers
318 static int acpi_evalf(acpi_handle handle,
319 void *res, char *method, char *fmt, ...)
321 char *fmt0 = fmt;
322 struct acpi_object_list params;
323 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
324 struct acpi_buffer result, *resultp;
325 union acpi_object out_obj;
326 acpi_status status;
327 va_list ap;
328 char res_type;
329 int success;
330 int quiet;
332 if (!*fmt) {
333 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
334 return 0;
337 if (*fmt == 'q') {
338 quiet = 1;
339 fmt++;
340 } else
341 quiet = 0;
343 res_type = *(fmt++);
345 params.count = 0;
346 params.pointer = &in_objs[0];
348 va_start(ap, fmt);
349 while (*fmt) {
350 char c = *(fmt++);
351 switch (c) {
352 case 'd': /* int */
353 in_objs[params.count].integer.value = va_arg(ap, int);
354 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
355 break;
356 /* add more types as needed */
357 default:
358 printk(TPACPI_ERR "acpi_evalf() called "
359 "with invalid format character '%c'\n", c);
360 return 0;
363 va_end(ap);
365 if (res_type != 'v') {
366 result.length = sizeof(out_obj);
367 result.pointer = &out_obj;
368 resultp = &result;
369 } else
370 resultp = NULL;
372 status = acpi_evaluate_object(handle, method, &params, resultp);
374 switch (res_type) {
375 case 'd': /* int */
376 if (res)
377 *(int *)res = out_obj.integer.value;
378 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
379 break;
380 case 'v': /* void */
381 success = status == AE_OK;
382 break;
383 /* add more types as needed */
384 default:
385 printk(TPACPI_ERR "acpi_evalf() called "
386 "with invalid format character '%c'\n", res_type);
387 return 0;
390 if (!success && !quiet)
391 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
392 method, fmt0, status);
394 return success;
397 static int acpi_ec_read(int i, u8 *p)
399 int v;
401 if (ecrd_handle) {
402 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
403 return 0;
404 *p = v;
405 } else {
406 if (ec_read(i, p) < 0)
407 return 0;
410 return 1;
413 static int acpi_ec_write(int i, u8 v)
415 if (ecwr_handle) {
416 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
417 return 0;
418 } else {
419 if (ec_write(i, v) < 0)
420 return 0;
423 return 1;
426 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
427 static int _sta(acpi_handle handle)
429 int status;
431 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
432 status = 0;
434 return status;
436 #endif
438 static int issue_thinkpad_cmos_command(int cmos_cmd)
440 if (!cmos_handle)
441 return -ENXIO;
443 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
444 return -EIO;
446 return 0;
449 /*************************************************************************
450 * ACPI device model
453 #define TPACPI_ACPIHANDLE_INIT(object) \
454 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
455 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
457 static void drv_acpi_handle_init(char *name,
458 acpi_handle *handle, acpi_handle parent,
459 char **paths, int num_paths, char **path)
461 int i;
462 acpi_status status;
464 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
465 name);
467 for (i = 0; i < num_paths; i++) {
468 status = acpi_get_handle(parent, paths[i], handle);
469 if (ACPI_SUCCESS(status)) {
470 *path = paths[i];
471 dbg_printk(TPACPI_DBG_INIT,
472 "Found ACPI handle %s for %s\n",
473 *path, name);
474 return;
478 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
479 name);
480 *handle = NULL;
483 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
485 struct ibm_struct *ibm = data;
487 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
488 return;
490 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
491 return;
493 ibm->acpi->notify(ibm, event);
496 static int __init setup_acpi_notify(struct ibm_struct *ibm)
498 acpi_status status;
499 int rc;
501 BUG_ON(!ibm->acpi);
503 if (!*ibm->acpi->handle)
504 return 0;
506 vdbg_printk(TPACPI_DBG_INIT,
507 "setting up ACPI notify for %s\n", ibm->name);
509 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
510 if (rc < 0) {
511 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
512 ibm->name, rc);
513 return -ENODEV;
516 acpi_driver_data(ibm->acpi->device) = ibm;
517 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
518 TPACPI_ACPI_EVENT_PREFIX,
519 ibm->name);
521 status = acpi_install_notify_handler(*ibm->acpi->handle,
522 ibm->acpi->type, dispatch_acpi_notify, ibm);
523 if (ACPI_FAILURE(status)) {
524 if (status == AE_ALREADY_EXISTS) {
525 printk(TPACPI_NOTICE
526 "another device driver is already "
527 "handling %s events\n", ibm->name);
528 } else {
529 printk(TPACPI_ERR
530 "acpi_install_notify_handler(%s) failed: %d\n",
531 ibm->name, status);
533 return -ENODEV;
535 ibm->flags.acpi_notify_installed = 1;
536 return 0;
539 static int __init tpacpi_device_add(struct acpi_device *device)
541 return 0;
544 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
546 int rc;
548 dbg_printk(TPACPI_DBG_INIT,
549 "registering %s as an ACPI driver\n", ibm->name);
551 BUG_ON(!ibm->acpi);
553 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
554 if (!ibm->acpi->driver) {
555 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
556 return -ENOMEM;
559 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
560 ibm->acpi->driver->ids = ibm->acpi->hid;
562 ibm->acpi->driver->ops.add = &tpacpi_device_add;
564 rc = acpi_bus_register_driver(ibm->acpi->driver);
565 if (rc < 0) {
566 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
567 ibm->name, rc);
568 kfree(ibm->acpi->driver);
569 ibm->acpi->driver = NULL;
570 } else if (!rc)
571 ibm->flags.acpi_driver_registered = 1;
573 return rc;
577 /****************************************************************************
578 ****************************************************************************
580 * Procfs Helpers
582 ****************************************************************************
583 ****************************************************************************/
585 static int dispatch_procfs_read(char *page, char **start, off_t off,
586 int count, int *eof, void *data)
588 struct ibm_struct *ibm = data;
589 int len;
591 if (!ibm || !ibm->read)
592 return -EINVAL;
594 len = ibm->read(page);
595 if (len < 0)
596 return len;
598 if (len <= off + count)
599 *eof = 1;
600 *start = page + off;
601 len -= off;
602 if (len > count)
603 len = count;
604 if (len < 0)
605 len = 0;
607 return len;
610 static int dispatch_procfs_write(struct file *file,
611 const char __user *userbuf,
612 unsigned long count, void *data)
614 struct ibm_struct *ibm = data;
615 char *kernbuf;
616 int ret;
618 if (!ibm || !ibm->write)
619 return -EINVAL;
621 kernbuf = kmalloc(count + 2, GFP_KERNEL);
622 if (!kernbuf)
623 return -ENOMEM;
625 if (copy_from_user(kernbuf, userbuf, count)) {
626 kfree(kernbuf);
627 return -EFAULT;
630 kernbuf[count] = 0;
631 strcat(kernbuf, ",");
632 ret = ibm->write(kernbuf);
633 if (ret == 0)
634 ret = count;
636 kfree(kernbuf);
638 return ret;
641 static char *next_cmd(char **cmds)
643 char *start = *cmds;
644 char *end;
646 while ((end = strchr(start, ',')) && end == start)
647 start = end + 1;
649 if (!end)
650 return NULL;
652 *end = 0;
653 *cmds = end + 1;
654 return start;
658 /****************************************************************************
659 ****************************************************************************
661 * Device model: input, hwmon and platform
663 ****************************************************************************
664 ****************************************************************************/
666 static struct platform_device *tpacpi_pdev;
667 static struct platform_device *tpacpi_sensors_pdev;
668 static struct device *tpacpi_hwmon;
669 static struct input_dev *tpacpi_inputdev;
670 static struct mutex tpacpi_inputdev_send_mutex;
671 static LIST_HEAD(tpacpi_all_drivers);
673 static int tpacpi_suspend_handler(struct platform_device *pdev,
674 pm_message_t state)
676 struct ibm_struct *ibm, *itmp;
678 list_for_each_entry_safe(ibm, itmp,
679 &tpacpi_all_drivers,
680 all_drivers) {
681 if (ibm->suspend)
682 (ibm->suspend)(state);
685 return 0;
688 static int tpacpi_resume_handler(struct platform_device *pdev)
690 struct ibm_struct *ibm, *itmp;
692 list_for_each_entry_safe(ibm, itmp,
693 &tpacpi_all_drivers,
694 all_drivers) {
695 if (ibm->resume)
696 (ibm->resume)();
699 return 0;
702 static struct platform_driver tpacpi_pdriver = {
703 .driver = {
704 .name = TPACPI_DRVR_NAME,
705 .owner = THIS_MODULE,
707 .suspend = tpacpi_suspend_handler,
708 .resume = tpacpi_resume_handler,
711 static struct platform_driver tpacpi_hwmon_pdriver = {
712 .driver = {
713 .name = TPACPI_HWMON_DRVR_NAME,
714 .owner = THIS_MODULE,
718 /*************************************************************************
719 * sysfs support helpers
722 struct attribute_set {
723 unsigned int members, max_members;
724 struct attribute_group group;
727 struct attribute_set_obj {
728 struct attribute_set s;
729 struct attribute *a;
730 } __attribute__((packed));
732 static struct attribute_set *create_attr_set(unsigned int max_members,
733 const char *name)
735 struct attribute_set_obj *sobj;
737 if (max_members == 0)
738 return NULL;
740 /* Allocates space for implicit NULL at the end too */
741 sobj = kzalloc(sizeof(struct attribute_set_obj) +
742 max_members * sizeof(struct attribute *),
743 GFP_KERNEL);
744 if (!sobj)
745 return NULL;
746 sobj->s.max_members = max_members;
747 sobj->s.group.attrs = &sobj->a;
748 sobj->s.group.name = name;
750 return &sobj->s;
753 #define destroy_attr_set(_set) \
754 kfree(_set);
756 /* not multi-threaded safe, use it in a single thread per set */
757 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
759 if (!s || !attr)
760 return -EINVAL;
762 if (s->members >= s->max_members)
763 return -ENOMEM;
765 s->group.attrs[s->members] = attr;
766 s->members++;
768 return 0;
771 static int add_many_to_attr_set(struct attribute_set *s,
772 struct attribute **attr,
773 unsigned int count)
775 int i, res;
777 for (i = 0; i < count; i++) {
778 res = add_to_attr_set(s, attr[i]);
779 if (res)
780 return res;
783 return 0;
786 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
788 sysfs_remove_group(kobj, &s->group);
789 destroy_attr_set(s);
792 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
793 sysfs_create_group(_kobj, &_attr_set->group)
795 static int parse_strtoul(const char *buf,
796 unsigned long max, unsigned long *value)
798 char *endp;
800 while (*buf && isspace(*buf))
801 buf++;
802 *value = simple_strtoul(buf, &endp, 0);
803 while (*endp && isspace(*endp))
804 endp++;
805 if (*endp || *value > max)
806 return -EINVAL;
808 return 0;
811 /*************************************************************************
812 * thinkpad-acpi driver attributes
815 /* interface_version --------------------------------------------------- */
816 static ssize_t tpacpi_driver_interface_version_show(
817 struct device_driver *drv,
818 char *buf)
820 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
823 static DRIVER_ATTR(interface_version, S_IRUGO,
824 tpacpi_driver_interface_version_show, NULL);
826 /* debug_level --------------------------------------------------------- */
827 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
828 char *buf)
830 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
833 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
834 const char *buf, size_t count)
836 unsigned long t;
838 if (parse_strtoul(buf, 0xffff, &t))
839 return -EINVAL;
841 dbg_level = t;
843 return count;
846 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
847 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
849 /* version ------------------------------------------------------------- */
850 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
851 char *buf)
853 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
854 TPACPI_DESC, TPACPI_VERSION);
857 static DRIVER_ATTR(version, S_IRUGO,
858 tpacpi_driver_version_show, NULL);
860 /* --------------------------------------------------------------------- */
862 static struct driver_attribute *tpacpi_driver_attributes[] = {
863 &driver_attr_debug_level, &driver_attr_version,
864 &driver_attr_interface_version,
867 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
869 int i, res;
871 i = 0;
872 res = 0;
873 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
874 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
875 i++;
878 return res;
881 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
883 int i;
885 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
886 driver_remove_file(drv, tpacpi_driver_attributes[i]);
889 /****************************************************************************
890 ****************************************************************************
892 * Subdrivers
894 ****************************************************************************
895 ****************************************************************************/
897 /*************************************************************************
898 * thinkpad-acpi init subdriver
901 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
903 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
904 printk(TPACPI_INFO "%s\n", TPACPI_URL);
906 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
907 (thinkpad_id.bios_version_str) ?
908 thinkpad_id.bios_version_str : "unknown",
909 (thinkpad_id.ec_version_str) ?
910 thinkpad_id.ec_version_str : "unknown");
912 if (thinkpad_id.vendor && thinkpad_id.model_str)
913 printk(TPACPI_INFO "%s %s\n",
914 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
915 "IBM" : ((thinkpad_id.vendor ==
916 PCI_VENDOR_ID_LENOVO) ?
917 "Lenovo" : "Unknown vendor"),
918 thinkpad_id.model_str);
920 return 0;
923 static int thinkpad_acpi_driver_read(char *p)
925 int len = 0;
927 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
928 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
930 return len;
933 static struct ibm_struct thinkpad_acpi_driver_data = {
934 .name = "driver",
935 .read = thinkpad_acpi_driver_read,
938 /*************************************************************************
939 * Hotkey subdriver
942 enum { /* hot key scan codes (derived from ACPI DSDT) */
943 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
944 TP_ACPI_HOTKEYSCAN_FNF2,
945 TP_ACPI_HOTKEYSCAN_FNF3,
946 TP_ACPI_HOTKEYSCAN_FNF4,
947 TP_ACPI_HOTKEYSCAN_FNF5,
948 TP_ACPI_HOTKEYSCAN_FNF6,
949 TP_ACPI_HOTKEYSCAN_FNF7,
950 TP_ACPI_HOTKEYSCAN_FNF8,
951 TP_ACPI_HOTKEYSCAN_FNF9,
952 TP_ACPI_HOTKEYSCAN_FNF10,
953 TP_ACPI_HOTKEYSCAN_FNF11,
954 TP_ACPI_HOTKEYSCAN_FNF12,
955 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
956 TP_ACPI_HOTKEYSCAN_FNINSERT,
957 TP_ACPI_HOTKEYSCAN_FNDELETE,
958 TP_ACPI_HOTKEYSCAN_FNHOME,
959 TP_ACPI_HOTKEYSCAN_FNEND,
960 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
961 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
962 TP_ACPI_HOTKEYSCAN_FNSPACE,
963 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
964 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
965 TP_ACPI_HOTKEYSCAN_MUTE,
966 TP_ACPI_HOTKEYSCAN_THINKPAD,
969 enum { /* Keys available through NVRAM polling */
970 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
971 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
974 enum { /* Positions of some of the keys in hotkey masks */
975 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
976 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
977 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
978 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
979 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
980 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
981 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
982 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
983 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
984 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
985 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
988 enum { /* NVRAM to ACPI HKEY group map */
989 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
990 TP_ACPI_HKEY_ZOOM_MASK |
991 TP_ACPI_HKEY_DISPSWTCH_MASK |
992 TP_ACPI_HKEY_HIBERNATE_MASK,
993 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
994 TP_ACPI_HKEY_BRGHTDWN_MASK,
995 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
996 TP_ACPI_HKEY_VOLDWN_MASK |
997 TP_ACPI_HKEY_MUTE_MASK,
1000 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1001 struct tp_nvram_state {
1002 u16 thinkpad_toggle:1;
1003 u16 zoom_toggle:1;
1004 u16 display_toggle:1;
1005 u16 thinklight_toggle:1;
1006 u16 hibernate_toggle:1;
1007 u16 displayexp_toggle:1;
1008 u16 display_state:1;
1009 u16 brightness_toggle:1;
1010 u16 volume_toggle:1;
1011 u16 mute:1;
1013 u8 brightness_level;
1014 u8 volume_level;
1017 static struct task_struct *tpacpi_hotkey_task;
1018 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1019 static int hotkey_poll_freq = 10; /* Hz */
1020 static struct mutex hotkey_thread_mutex;
1021 static struct mutex hotkey_thread_data_mutex;
1022 static unsigned int hotkey_config_change;
1024 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1026 #define hotkey_source_mask 0U
1028 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1030 static struct mutex hotkey_mutex;
1032 static enum { /* Reasons for waking up */
1033 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1034 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1035 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1036 } hotkey_wakeup_reason;
1038 static int hotkey_autosleep_ack;
1040 static int hotkey_orig_status;
1041 static u32 hotkey_orig_mask;
1042 static u32 hotkey_all_mask;
1043 static u32 hotkey_reserved_mask;
1044 static u32 hotkey_mask;
1046 static unsigned int hotkey_report_mode;
1048 static u16 *hotkey_keycode_map;
1050 static struct attribute_set *hotkey_dev_attributes;
1052 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1053 #define HOTKEY_CONFIG_CRITICAL_START \
1054 do { \
1055 mutex_lock(&hotkey_thread_data_mutex); \
1056 hotkey_config_change++; \
1057 } while (0);
1058 #define HOTKEY_CONFIG_CRITICAL_END \
1059 mutex_unlock(&hotkey_thread_data_mutex);
1060 #else
1061 #define HOTKEY_CONFIG_CRITICAL_START
1062 #define HOTKEY_CONFIG_CRITICAL_END
1063 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1065 /* HKEY.MHKG() return bits */
1066 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1068 static int hotkey_get_wlsw(int *status)
1070 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1071 return -EIO;
1072 return 0;
1075 static int hotkey_get_tablet_mode(int *status)
1077 int s;
1079 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1080 return -EIO;
1082 return ((s & TP_HOTKEY_TABLET_MASK) != 0);
1086 * Call with hotkey_mutex held
1088 static int hotkey_mask_get(void)
1090 u32 m = 0;
1092 if (tp_features.hotkey_mask) {
1093 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1094 return -EIO;
1096 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1098 return 0;
1102 * Call with hotkey_mutex held
1104 static int hotkey_mask_set(u32 mask)
1106 int i;
1107 int rc = 0;
1109 if (tp_features.hotkey_mask) {
1110 HOTKEY_CONFIG_CRITICAL_START
1111 for (i = 0; i < 32; i++) {
1112 u32 m = 1 << i;
1113 /* enable in firmware mask only keys not in NVRAM
1114 * mode, but enable the key in the cached hotkey_mask
1115 * regardless of mode, or the key will end up
1116 * disabled by hotkey_mask_get() */
1117 if (!acpi_evalf(hkey_handle,
1118 NULL, "MHKM", "vdd", i + 1,
1119 !!((mask & ~hotkey_source_mask) & m))) {
1120 rc = -EIO;
1121 break;
1122 } else {
1123 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1126 HOTKEY_CONFIG_CRITICAL_END
1128 /* hotkey_mask_get must be called unconditionally below */
1129 if (!hotkey_mask_get() && !rc &&
1130 (hotkey_mask & ~hotkey_source_mask) !=
1131 (mask & ~hotkey_source_mask)) {
1132 printk(TPACPI_NOTICE
1133 "requested hot key mask 0x%08x, but "
1134 "firmware forced it to 0x%08x\n",
1135 mask, hotkey_mask);
1137 } else {
1138 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1139 HOTKEY_CONFIG_CRITICAL_START
1140 hotkey_mask = mask & hotkey_source_mask;
1141 HOTKEY_CONFIG_CRITICAL_END
1142 hotkey_mask_get();
1143 if (hotkey_mask != mask) {
1144 printk(TPACPI_NOTICE
1145 "requested hot key mask 0x%08x, "
1146 "forced to 0x%08x (NVRAM poll mask is "
1147 "0x%08x): no firmware mask support\n",
1148 mask, hotkey_mask, hotkey_source_mask);
1150 #else
1151 hotkey_mask_get();
1152 rc = -ENXIO;
1153 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1156 return rc;
1159 static int hotkey_status_get(int *status)
1161 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1162 return -EIO;
1164 return 0;
1167 static int hotkey_status_set(int status)
1169 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1170 return -EIO;
1172 return 0;
1175 static void tpacpi_input_send_radiosw(void)
1177 int wlsw;
1179 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1180 mutex_lock(&tpacpi_inputdev_send_mutex);
1182 input_report_switch(tpacpi_inputdev,
1183 SW_RADIO, !!wlsw);
1184 input_sync(tpacpi_inputdev);
1186 mutex_unlock(&tpacpi_inputdev_send_mutex);
1190 static void tpacpi_input_send_tabletsw(void)
1192 int state;
1194 if (tp_features.hotkey_tablet &&
1195 !hotkey_get_tablet_mode(&state)) {
1196 mutex_lock(&tpacpi_inputdev_send_mutex);
1198 input_report_switch(tpacpi_inputdev,
1199 SW_TABLET_MODE, !!state);
1200 input_sync(tpacpi_inputdev);
1202 mutex_unlock(&tpacpi_inputdev_send_mutex);
1206 static void tpacpi_input_send_key(unsigned int scancode)
1208 unsigned int keycode;
1210 keycode = hotkey_keycode_map[scancode];
1212 if (keycode != KEY_RESERVED) {
1213 mutex_lock(&tpacpi_inputdev_send_mutex);
1215 input_report_key(tpacpi_inputdev, keycode, 1);
1216 if (keycode == KEY_UNKNOWN)
1217 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1218 scancode);
1219 input_sync(tpacpi_inputdev);
1221 input_report_key(tpacpi_inputdev, keycode, 0);
1222 if (keycode == KEY_UNKNOWN)
1223 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1224 scancode);
1225 input_sync(tpacpi_inputdev);
1227 mutex_unlock(&tpacpi_inputdev_send_mutex);
1231 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1232 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1234 static void tpacpi_hotkey_send_key(unsigned int scancode)
1236 tpacpi_input_send_key(scancode);
1237 if (hotkey_report_mode < 2) {
1238 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1239 0x80, 0x1001 + scancode);
1243 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1245 u8 d;
1247 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1248 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1249 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1250 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1251 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1252 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1254 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1255 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1256 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1258 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1259 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1260 n->displayexp_toggle =
1261 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1263 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1264 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1265 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1266 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1267 n->brightness_toggle =
1268 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1270 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1271 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1272 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1273 >> TP_NVRAM_POS_LEVEL_VOLUME;
1274 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1275 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1279 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1280 do { \
1281 if ((mask & (1 << __scancode)) && \
1282 oldn->__member != newn->__member) \
1283 tpacpi_hotkey_send_key(__scancode); \
1284 } while (0)
1286 #define TPACPI_MAY_SEND_KEY(__scancode) \
1287 do { if (mask & (1 << __scancode)) \
1288 tpacpi_hotkey_send_key(__scancode); } while (0)
1290 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1291 struct tp_nvram_state *newn,
1292 u32 mask)
1294 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1295 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1296 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1297 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1299 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1301 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1303 /* handle volume */
1304 if (oldn->volume_toggle != newn->volume_toggle) {
1305 if (oldn->mute != newn->mute) {
1306 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1308 if (oldn->volume_level > newn->volume_level) {
1309 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1310 } else if (oldn->volume_level < newn->volume_level) {
1311 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1312 } else if (oldn->mute == newn->mute) {
1313 /* repeated key presses that didn't change state */
1314 if (newn->mute) {
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1316 } else if (newn->volume_level != 0) {
1317 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1318 } else {
1319 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1324 /* handle brightness */
1325 if (oldn->brightness_toggle != newn->brightness_toggle) {
1326 if (oldn->brightness_level < newn->brightness_level) {
1327 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1328 } else if (oldn->brightness_level > newn->brightness_level) {
1329 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1330 } else {
1331 /* repeated key presses that didn't change state */
1332 if (newn->brightness_level != 0) {
1333 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1334 } else {
1335 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1341 #undef TPACPI_COMPARE_KEY
1342 #undef TPACPI_MAY_SEND_KEY
1344 static int hotkey_kthread(void *data)
1346 struct tp_nvram_state s[2];
1347 u32 mask;
1348 unsigned int si, so;
1349 unsigned long t;
1350 unsigned int change_detector, must_reset;
1352 mutex_lock(&hotkey_thread_mutex);
1354 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1355 goto exit;
1357 set_freezable();
1359 so = 0;
1360 si = 1;
1361 t = 0;
1363 /* Initial state for compares */
1364 mutex_lock(&hotkey_thread_data_mutex);
1365 change_detector = hotkey_config_change;
1366 mask = hotkey_source_mask & hotkey_mask;
1367 mutex_unlock(&hotkey_thread_data_mutex);
1368 hotkey_read_nvram(&s[so], mask);
1370 while (!kthread_should_stop() && hotkey_poll_freq) {
1371 if (t == 0)
1372 t = 1000/hotkey_poll_freq;
1373 t = msleep_interruptible(t);
1374 if (unlikely(kthread_should_stop()))
1375 break;
1376 must_reset = try_to_freeze();
1377 if (t > 0 && !must_reset)
1378 continue;
1380 mutex_lock(&hotkey_thread_data_mutex);
1381 if (must_reset || hotkey_config_change != change_detector) {
1382 /* forget old state on thaw or config change */
1383 si = so;
1384 t = 0;
1385 change_detector = hotkey_config_change;
1387 mask = hotkey_source_mask & hotkey_mask;
1388 mutex_unlock(&hotkey_thread_data_mutex);
1390 if (likely(mask)) {
1391 hotkey_read_nvram(&s[si], mask);
1392 if (likely(si != so)) {
1393 hotkey_compare_and_issue_event(&s[so], &s[si],
1394 mask);
1398 so = si;
1399 si ^= 1;
1402 exit:
1403 mutex_unlock(&hotkey_thread_mutex);
1404 return 0;
1407 static void hotkey_poll_stop_sync(void)
1409 if (tpacpi_hotkey_task) {
1410 if (frozen(tpacpi_hotkey_task) ||
1411 freezing(tpacpi_hotkey_task))
1412 thaw_process(tpacpi_hotkey_task);
1414 kthread_stop(tpacpi_hotkey_task);
1415 tpacpi_hotkey_task = NULL;
1416 mutex_lock(&hotkey_thread_mutex);
1417 /* at this point, the thread did exit */
1418 mutex_unlock(&hotkey_thread_mutex);
1422 /* call with hotkey_mutex held */
1423 static void hotkey_poll_setup(int may_warn)
1425 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1426 hotkey_poll_freq > 0 &&
1427 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1428 if (!tpacpi_hotkey_task) {
1429 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1430 NULL,
1431 TPACPI_FILE "d");
1432 if (IS_ERR(tpacpi_hotkey_task)) {
1433 tpacpi_hotkey_task = NULL;
1434 printk(TPACPI_ERR
1435 "could not create kernel thread "
1436 "for hotkey polling\n");
1439 } else {
1440 hotkey_poll_stop_sync();
1441 if (may_warn &&
1442 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1443 printk(TPACPI_NOTICE
1444 "hot keys 0x%08x require polling, "
1445 "which is currently disabled\n",
1446 hotkey_source_mask);
1451 static void hotkey_poll_setup_safe(int may_warn)
1453 mutex_lock(&hotkey_mutex);
1454 hotkey_poll_setup(may_warn);
1455 mutex_unlock(&hotkey_mutex);
1458 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1460 static void hotkey_poll_setup_safe(int __unused)
1464 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1466 static int hotkey_inputdev_open(struct input_dev *dev)
1468 switch (tpacpi_lifecycle) {
1469 case TPACPI_LIFE_INIT:
1471 * hotkey_init will call hotkey_poll_setup_safe
1472 * at the appropriate moment
1474 return 0;
1475 case TPACPI_LIFE_EXITING:
1476 return -EBUSY;
1477 case TPACPI_LIFE_RUNNING:
1478 hotkey_poll_setup_safe(0);
1479 return 0;
1482 /* Should only happen if tpacpi_lifecycle is corrupt */
1483 BUG();
1484 return -EBUSY;
1487 static void hotkey_inputdev_close(struct input_dev *dev)
1489 /* disable hotkey polling when possible */
1490 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1491 hotkey_poll_setup_safe(0);
1494 /* sysfs hotkey enable ------------------------------------------------- */
1495 static ssize_t hotkey_enable_show(struct device *dev,
1496 struct device_attribute *attr,
1497 char *buf)
1499 int res, status;
1501 res = hotkey_status_get(&status);
1502 if (res)
1503 return res;
1505 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1508 static ssize_t hotkey_enable_store(struct device *dev,
1509 struct device_attribute *attr,
1510 const char *buf, size_t count)
1512 unsigned long t;
1513 int res;
1515 if (parse_strtoul(buf, 1, &t))
1516 return -EINVAL;
1518 res = hotkey_status_set(t);
1520 return (res) ? res : count;
1523 static struct device_attribute dev_attr_hotkey_enable =
1524 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1525 hotkey_enable_show, hotkey_enable_store);
1527 /* sysfs hotkey mask --------------------------------------------------- */
1528 static ssize_t hotkey_mask_show(struct device *dev,
1529 struct device_attribute *attr,
1530 char *buf)
1532 int res;
1534 if (mutex_lock_interruptible(&hotkey_mutex))
1535 return -ERESTARTSYS;
1536 res = hotkey_mask_get();
1537 mutex_unlock(&hotkey_mutex);
1539 return (res)?
1540 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1543 static ssize_t hotkey_mask_store(struct device *dev,
1544 struct device_attribute *attr,
1545 const char *buf, size_t count)
1547 unsigned long t;
1548 int res;
1550 if (parse_strtoul(buf, 0xffffffffUL, &t))
1551 return -EINVAL;
1553 if (mutex_lock_interruptible(&hotkey_mutex))
1554 return -ERESTARTSYS;
1556 res = hotkey_mask_set(t);
1558 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1559 hotkey_poll_setup(1);
1560 #endif
1562 mutex_unlock(&hotkey_mutex);
1564 return (res) ? res : count;
1567 static struct device_attribute dev_attr_hotkey_mask =
1568 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1569 hotkey_mask_show, hotkey_mask_store);
1571 /* sysfs hotkey bios_enabled ------------------------------------------- */
1572 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1573 struct device_attribute *attr,
1574 char *buf)
1576 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1579 static struct device_attribute dev_attr_hotkey_bios_enabled =
1580 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1582 /* sysfs hotkey bios_mask ---------------------------------------------- */
1583 static ssize_t hotkey_bios_mask_show(struct device *dev,
1584 struct device_attribute *attr,
1585 char *buf)
1587 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1590 static struct device_attribute dev_attr_hotkey_bios_mask =
1591 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1593 /* sysfs hotkey all_mask ----------------------------------------------- */
1594 static ssize_t hotkey_all_mask_show(struct device *dev,
1595 struct device_attribute *attr,
1596 char *buf)
1598 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1599 hotkey_all_mask | hotkey_source_mask);
1602 static struct device_attribute dev_attr_hotkey_all_mask =
1603 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1605 /* sysfs hotkey recommended_mask --------------------------------------- */
1606 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1607 struct device_attribute *attr,
1608 char *buf)
1610 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1611 (hotkey_all_mask | hotkey_source_mask)
1612 & ~hotkey_reserved_mask);
1615 static struct device_attribute dev_attr_hotkey_recommended_mask =
1616 __ATTR(hotkey_recommended_mask, S_IRUGO,
1617 hotkey_recommended_mask_show, NULL);
1619 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1621 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1622 static ssize_t hotkey_source_mask_show(struct device *dev,
1623 struct device_attribute *attr,
1624 char *buf)
1626 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1629 static ssize_t hotkey_source_mask_store(struct device *dev,
1630 struct device_attribute *attr,
1631 const char *buf, size_t count)
1633 unsigned long t;
1635 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1636 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1637 return -EINVAL;
1639 if (mutex_lock_interruptible(&hotkey_mutex))
1640 return -ERESTARTSYS;
1642 HOTKEY_CONFIG_CRITICAL_START
1643 hotkey_source_mask = t;
1644 HOTKEY_CONFIG_CRITICAL_END
1646 hotkey_poll_setup(1);
1648 mutex_unlock(&hotkey_mutex);
1650 return count;
1653 static struct device_attribute dev_attr_hotkey_source_mask =
1654 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1655 hotkey_source_mask_show, hotkey_source_mask_store);
1657 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1658 static ssize_t hotkey_poll_freq_show(struct device *dev,
1659 struct device_attribute *attr,
1660 char *buf)
1662 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1665 static ssize_t hotkey_poll_freq_store(struct device *dev,
1666 struct device_attribute *attr,
1667 const char *buf, size_t count)
1669 unsigned long t;
1671 if (parse_strtoul(buf, 25, &t))
1672 return -EINVAL;
1674 if (mutex_lock_interruptible(&hotkey_mutex))
1675 return -ERESTARTSYS;
1677 hotkey_poll_freq = t;
1679 hotkey_poll_setup(1);
1680 mutex_unlock(&hotkey_mutex);
1682 return count;
1685 static struct device_attribute dev_attr_hotkey_poll_freq =
1686 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1687 hotkey_poll_freq_show, hotkey_poll_freq_store);
1689 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1691 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1692 static ssize_t hotkey_radio_sw_show(struct device *dev,
1693 struct device_attribute *attr,
1694 char *buf)
1696 int res, s;
1697 res = hotkey_get_wlsw(&s);
1698 if (res < 0)
1699 return res;
1701 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1704 static struct device_attribute dev_attr_hotkey_radio_sw =
1705 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1707 static void hotkey_radio_sw_notify_change(void)
1709 if (tp_features.hotkey_wlsw)
1710 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1711 "hotkey_radio_sw");
1714 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1715 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1716 struct device_attribute *attr,
1717 char *buf)
1719 int res, s;
1720 res = hotkey_get_tablet_mode(&s);
1721 if (res < 0)
1722 return res;
1724 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1727 static struct device_attribute dev_attr_hotkey_tablet_mode =
1728 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1730 static void hotkey_tablet_mode_notify_change(void)
1732 if (tp_features.hotkey_tablet)
1733 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1734 "hotkey_tablet_mode");
1737 /* sysfs hotkey report_mode -------------------------------------------- */
1738 static ssize_t hotkey_report_mode_show(struct device *dev,
1739 struct device_attribute *attr,
1740 char *buf)
1742 return snprintf(buf, PAGE_SIZE, "%d\n",
1743 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1746 static struct device_attribute dev_attr_hotkey_report_mode =
1747 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1749 /* sysfs wakeup reason (pollable) -------------------------------------- */
1750 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1751 struct device_attribute *attr,
1752 char *buf)
1754 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1757 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1758 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1760 void hotkey_wakeup_reason_notify_change(void)
1762 if (tp_features.hotkey_mask)
1763 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1764 "wakeup_reason");
1767 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1768 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1769 struct device_attribute *attr,
1770 char *buf)
1772 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1775 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1776 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1777 hotkey_wakeup_hotunplug_complete_show, NULL);
1779 void hotkey_wakeup_hotunplug_complete_notify_change(void)
1781 if (tp_features.hotkey_mask)
1782 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1783 "wakeup_hotunplug_complete");
1786 /* --------------------------------------------------------------------- */
1788 static struct attribute *hotkey_attributes[] __initdata = {
1789 &dev_attr_hotkey_enable.attr,
1790 &dev_attr_hotkey_bios_enabled.attr,
1791 &dev_attr_hotkey_report_mode.attr,
1792 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1793 &dev_attr_hotkey_mask.attr,
1794 &dev_attr_hotkey_all_mask.attr,
1795 &dev_attr_hotkey_recommended_mask.attr,
1796 &dev_attr_hotkey_source_mask.attr,
1797 &dev_attr_hotkey_poll_freq.attr,
1798 #endif
1801 static struct attribute *hotkey_mask_attributes[] __initdata = {
1802 &dev_attr_hotkey_bios_mask.attr,
1803 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1804 &dev_attr_hotkey_mask.attr,
1805 &dev_attr_hotkey_all_mask.attr,
1806 &dev_attr_hotkey_recommended_mask.attr,
1807 #endif
1808 &dev_attr_hotkey_wakeup_reason.attr,
1809 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1812 static int __init hotkey_init(struct ibm_init_struct *iibm)
1814 /* Requirements for changing the default keymaps:
1816 * 1. Many of the keys are mapped to KEY_RESERVED for very
1817 * good reasons. Do not change them unless you have deep
1818 * knowledge on the IBM and Lenovo ThinkPad firmware for
1819 * the various ThinkPad models. The driver behaves
1820 * differently for KEY_RESERVED: such keys have their
1821 * hot key mask *unset* in mask_recommended, and also
1822 * in the initial hot key mask programmed into the
1823 * firmware at driver load time, which means the firm-
1824 * ware may react very differently if you change them to
1825 * something else;
1827 * 2. You must be subscribed to the linux-thinkpad and
1828 * ibm-acpi-devel mailing lists, and you should read the
1829 * list archives since 2007 if you want to change the
1830 * keymaps. This requirement exists so that you will
1831 * know the past history of problems with the thinkpad-
1832 * acpi driver keymaps, and also that you will be
1833 * listening to any bug reports;
1835 * 3. Do not send thinkpad-acpi specific patches directly to
1836 * for merging, *ever*. Send them to the linux-acpi
1837 * mailinglist for comments. Merging is to be done only
1838 * through acpi-test and the ACPI maintainer.
1840 * If the above is too much to ask, don't change the keymap.
1841 * Ask the thinkpad-acpi maintainer to do it, instead.
1843 static u16 ibm_keycode_map[] __initdata = {
1844 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1845 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1846 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1847 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1849 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1850 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1851 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1852 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1854 /* brightness: firmware always reacts to them, unless
1855 * X.org did some tricks in the radeon BIOS scratch
1856 * registers of *some* models */
1857 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1858 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1860 /* Thinklight: firmware always react to it */
1861 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1863 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1864 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1866 /* Volume: firmware always react to it and reprograms
1867 * the built-in *extra* mixer. Never map it to control
1868 * another mixer by default. */
1869 KEY_RESERVED, /* 0x14: VOLUME UP */
1870 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1871 KEY_RESERVED, /* 0x16: MUTE */
1873 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1875 /* (assignments unknown, please report if found) */
1876 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1877 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1879 static u16 lenovo_keycode_map[] __initdata = {
1880 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1881 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1882 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1883 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1885 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1886 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1887 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1888 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1890 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1891 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1893 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1895 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1896 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1898 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1899 * react to it and reprograms the built-in *extra* mixer.
1900 * Never map it to control another mixer by default.
1902 * T60?, T61, R60?, R61: firmware and EC tries to send
1903 * these over the regular keyboard, so these are no-ops,
1904 * but there are still weird bugs re. MUTE, so do not
1905 * change unless you get test reports from all Lenovo
1906 * models. May cause the BIOS to interfere with the
1907 * HDA mixer.
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,
1920 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1921 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1922 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1924 int res, i;
1925 int status;
1926 int hkeyv;
1928 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1930 BUG_ON(!tpacpi_inputdev);
1931 BUG_ON(tpacpi_inputdev->open != NULL ||
1932 tpacpi_inputdev->close != NULL);
1934 TPACPI_ACPIHANDLE_INIT(hkey);
1935 mutex_init(&hotkey_mutex);
1937 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1938 mutex_init(&hotkey_thread_mutex);
1939 mutex_init(&hotkey_thread_data_mutex);
1940 #endif
1942 /* hotkey not supported on 570 */
1943 tp_features.hotkey = hkey_handle != NULL;
1945 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1946 str_supported(tp_features.hotkey));
1948 if (tp_features.hotkey) {
1949 hotkey_dev_attributes = create_attr_set(13, NULL);
1950 if (!hotkey_dev_attributes)
1951 return -ENOMEM;
1952 res = add_many_to_attr_set(hotkey_dev_attributes,
1953 hotkey_attributes,
1954 ARRAY_SIZE(hotkey_attributes));
1955 if (res)
1956 return res;
1958 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1959 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1960 for HKEY interface version 0x100 */
1961 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1962 if ((hkeyv >> 8) != 1) {
1963 printk(TPACPI_ERR "unknown version of the "
1964 "HKEY interface: 0x%x\n", hkeyv);
1965 printk(TPACPI_ERR "please report this to %s\n",
1966 TPACPI_MAIL);
1967 } else {
1969 * MHKV 0x100 in A31, R40, R40e,
1970 * T4x, X31, and later
1972 tp_features.hotkey_mask = 1;
1976 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1977 str_supported(tp_features.hotkey_mask));
1979 if (tp_features.hotkey_mask) {
1980 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1981 "MHKA", "qd")) {
1982 printk(TPACPI_ERR
1983 "missing MHKA handler, "
1984 "please report this to %s\n",
1985 TPACPI_MAIL);
1986 /* FN+F12, FN+F4, FN+F3 */
1987 hotkey_all_mask = 0x080cU;
1991 /* hotkey_source_mask *must* be zero for
1992 * the first hotkey_mask_get */
1993 res = hotkey_status_get(&hotkey_orig_status);
1994 if (!res && tp_features.hotkey_mask) {
1995 res = hotkey_mask_get();
1996 hotkey_orig_mask = hotkey_mask;
1997 if (!res) {
1998 res = add_many_to_attr_set(
1999 hotkey_dev_attributes,
2000 hotkey_mask_attributes,
2001 ARRAY_SIZE(hotkey_mask_attributes));
2005 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2006 if (tp_features.hotkey_mask) {
2007 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2008 & ~hotkey_all_mask;
2009 } else {
2010 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2013 vdbg_printk(TPACPI_DBG_INIT,
2014 "hotkey source mask 0x%08x, polling freq %d\n",
2015 hotkey_source_mask, hotkey_poll_freq);
2016 #endif
2018 /* Not all thinkpads have a hardware radio switch */
2019 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2020 tp_features.hotkey_wlsw = 1;
2021 printk(TPACPI_INFO
2022 "radio switch found; radios are %s\n",
2023 enabled(status, 0));
2024 res = add_to_attr_set(hotkey_dev_attributes,
2025 &dev_attr_hotkey_radio_sw.attr);
2028 /* For X41t, X60t, X61t Tablets... */
2029 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2030 tp_features.hotkey_tablet = 1;
2031 printk(TPACPI_INFO
2032 "possible tablet mode switch found; "
2033 "ThinkPad in %s mode\n",
2034 (status & TP_HOTKEY_TABLET_MASK)?
2035 "tablet" : "laptop");
2036 res = add_to_attr_set(hotkey_dev_attributes,
2037 &dev_attr_hotkey_tablet_mode.attr);
2040 if (!res)
2041 res = register_attr_set_with_sysfs(
2042 hotkey_dev_attributes,
2043 &tpacpi_pdev->dev.kobj);
2044 if (res)
2045 return res;
2047 /* Set up key map */
2049 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2050 GFP_KERNEL);
2051 if (!hotkey_keycode_map) {
2052 printk(TPACPI_ERR
2053 "failed to allocate memory for key map\n");
2054 return -ENOMEM;
2057 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2058 dbg_printk(TPACPI_DBG_INIT,
2059 "using Lenovo default hot key map\n");
2060 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2061 TPACPI_HOTKEY_MAP_SIZE);
2062 } else {
2063 dbg_printk(TPACPI_DBG_INIT,
2064 "using IBM default hot key map\n");
2065 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2066 TPACPI_HOTKEY_MAP_SIZE);
2069 /* Do not issue duplicate brightness change events to
2070 * userspace */
2071 if (tp_features.bright_acpimode) {
2072 printk(TPACPI_NOTICE
2073 "Brightness hot keys firmware issues ACPI "
2074 "video events\n");
2075 printk(TPACPI_NOTICE
2076 "Disabling brightness hot keys on event "
2077 "device keymap\n");
2078 hotkey_keycode_map[TP_ACPI_HOTKEYSCAN_FNHOME] =
2079 KEY_RESERVED;
2080 hotkey_keycode_map[TP_ACPI_HOTKEYSCAN_FNEND] =
2081 KEY_RESERVED;
2084 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2085 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2086 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2087 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2088 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2089 tpacpi_inputdev->keycode = hotkey_keycode_map;
2090 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2091 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2092 set_bit(hotkey_keycode_map[i],
2093 tpacpi_inputdev->keybit);
2094 } else {
2095 if (i < sizeof(hotkey_reserved_mask)*8)
2096 hotkey_reserved_mask |= 1 << i;
2100 if (tp_features.hotkey_wlsw) {
2101 set_bit(EV_SW, tpacpi_inputdev->evbit);
2102 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2104 if (tp_features.hotkey_tablet) {
2105 set_bit(EV_SW, tpacpi_inputdev->evbit);
2106 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2109 dbg_printk(TPACPI_DBG_INIT,
2110 "enabling hot key handling\n");
2111 res = hotkey_status_set(1);
2112 if (res)
2113 return res;
2114 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2115 & ~hotkey_reserved_mask)
2116 | hotkey_orig_mask);
2117 if (res < 0 && res != -ENXIO)
2118 return res;
2120 dbg_printk(TPACPI_DBG_INIT,
2121 "legacy hot key reporting over procfs %s\n",
2122 (hotkey_report_mode < 2) ?
2123 "enabled" : "disabled");
2125 tpacpi_inputdev->open = &hotkey_inputdev_open;
2126 tpacpi_inputdev->close = &hotkey_inputdev_close;
2128 hotkey_poll_setup_safe(1);
2129 tpacpi_input_send_radiosw();
2130 tpacpi_input_send_tabletsw();
2133 return (tp_features.hotkey)? 0 : 1;
2136 static void hotkey_exit(void)
2138 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2139 hotkey_poll_stop_sync();
2140 #endif
2142 if (tp_features.hotkey) {
2143 dbg_printk(TPACPI_DBG_EXIT,
2144 "restoring original hot key mask\n");
2145 /* no short-circuit boolean operator below! */
2146 if ((hotkey_mask_set(hotkey_orig_mask) |
2147 hotkey_status_set(hotkey_orig_status)) != 0)
2148 printk(TPACPI_ERR
2149 "failed to restore hot key mask "
2150 "to BIOS defaults\n");
2153 if (hotkey_dev_attributes) {
2154 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2155 hotkey_dev_attributes = NULL;
2159 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2161 u32 hkey;
2162 unsigned int scancode;
2163 int send_acpi_ev;
2164 int ignore_acpi_ev;
2165 int unk_ev;
2167 if (event != 0x80) {
2168 printk(TPACPI_ERR
2169 "unknown HKEY notification event %d\n", event);
2170 /* forward it to userspace, maybe it knows how to handle it */
2171 acpi_bus_generate_netlink_event(
2172 ibm->acpi->device->pnp.device_class,
2173 ibm->acpi->device->dev.bus_id,
2174 event, 0);
2175 return;
2178 while (1) {
2179 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2180 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2181 return;
2184 if (hkey == 0) {
2185 /* queue empty */
2186 return;
2189 send_acpi_ev = 1;
2190 ignore_acpi_ev = 0;
2191 unk_ev = 0;
2193 switch (hkey >> 12) {
2194 case 1:
2195 /* 0x1000-0x1FFF: key presses */
2196 scancode = hkey & 0xfff;
2197 if (scancode > 0 && scancode < 0x21) {
2198 scancode--;
2199 if (!(hotkey_source_mask & (1 << scancode))) {
2200 tpacpi_input_send_key(scancode);
2201 send_acpi_ev = 0;
2202 } else {
2203 ignore_acpi_ev = 1;
2205 } else {
2206 unk_ev = 1;
2208 break;
2209 case 2:
2210 /* Wakeup reason */
2211 switch (hkey) {
2212 case 0x2304: /* suspend, undock */
2213 case 0x2404: /* hibernation, undock */
2214 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2215 ignore_acpi_ev = 1;
2216 break;
2217 case 0x2305: /* suspend, bay eject */
2218 case 0x2405: /* hibernation, bay eject */
2219 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2220 ignore_acpi_ev = 1;
2221 break;
2222 default:
2223 unk_ev = 1;
2225 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2226 printk(TPACPI_INFO
2227 "woke up due to a hot-unplug "
2228 "request...\n");
2229 hotkey_wakeup_reason_notify_change();
2231 break;
2232 case 3:
2233 /* bay-related wakeups */
2234 if (hkey == 0x3003) {
2235 hotkey_autosleep_ack = 1;
2236 printk(TPACPI_INFO
2237 "bay ejected\n");
2238 hotkey_wakeup_hotunplug_complete_notify_change();
2239 } else {
2240 unk_ev = 1;
2242 break;
2243 case 4:
2244 /* dock-related wakeups */
2245 if (hkey == 0x4003) {
2246 hotkey_autosleep_ack = 1;
2247 printk(TPACPI_INFO
2248 "undocked\n");
2249 hotkey_wakeup_hotunplug_complete_notify_change();
2250 } else {
2251 unk_ev = 1;
2253 break;
2254 case 5:
2255 /* 0x5000-0x5FFF: human interface helpers */
2256 switch (hkey) {
2257 case 0x5010: /* Lenovo new BIOS: brightness changed */
2258 case 0x500b: /* X61t: tablet pen inserted into bay */
2259 case 0x500c: /* X61t: tablet pen removed from bay */
2260 break;
2261 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2262 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2263 tpacpi_input_send_tabletsw();
2264 hotkey_tablet_mode_notify_change();
2265 send_acpi_ev = 0;
2266 break;
2267 case 0x5001:
2268 case 0x5002:
2269 /* LID switch events. Do not propagate */
2270 ignore_acpi_ev = 1;
2271 break;
2272 default:
2273 unk_ev = 1;
2275 break;
2276 case 7:
2277 /* 0x7000-0x7FFF: misc */
2278 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2279 tpacpi_input_send_radiosw();
2280 hotkey_radio_sw_notify_change();
2281 send_acpi_ev = 0;
2282 break;
2284 /* fallthrough to default */
2285 default:
2286 unk_ev = 1;
2288 if (unk_ev) {
2289 printk(TPACPI_NOTICE
2290 "unhandled HKEY event 0x%04x\n", hkey);
2293 /* Legacy events */
2294 if (!ignore_acpi_ev &&
2295 (send_acpi_ev || hotkey_report_mode < 2)) {
2296 acpi_bus_generate_proc_event(ibm->acpi->device,
2297 event, hkey);
2300 /* netlink events */
2301 if (!ignore_acpi_ev && send_acpi_ev) {
2302 acpi_bus_generate_netlink_event(
2303 ibm->acpi->device->pnp.device_class,
2304 ibm->acpi->device->dev.bus_id,
2305 event, hkey);
2310 static void hotkey_suspend(pm_message_t state)
2312 /* Do these on suspend, we get the events on early resume! */
2313 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2314 hotkey_autosleep_ack = 0;
2317 static void hotkey_resume(void)
2319 if (hotkey_mask_get())
2320 printk(TPACPI_ERR
2321 "error while trying to read hot key mask "
2322 "from firmware\n");
2323 tpacpi_input_send_radiosw();
2324 hotkey_radio_sw_notify_change();
2325 hotkey_tablet_mode_notify_change();
2326 hotkey_wakeup_reason_notify_change();
2327 hotkey_wakeup_hotunplug_complete_notify_change();
2328 hotkey_poll_setup_safe(0);
2331 /* procfs -------------------------------------------------------------- */
2332 static int hotkey_read(char *p)
2334 int res, status;
2335 int len = 0;
2337 if (!tp_features.hotkey) {
2338 len += sprintf(p + len, "status:\t\tnot supported\n");
2339 return len;
2342 if (mutex_lock_interruptible(&hotkey_mutex))
2343 return -ERESTARTSYS;
2344 res = hotkey_status_get(&status);
2345 if (!res)
2346 res = hotkey_mask_get();
2347 mutex_unlock(&hotkey_mutex);
2348 if (res)
2349 return res;
2351 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2352 if (tp_features.hotkey_mask) {
2353 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2354 len += sprintf(p + len,
2355 "commands:\tenable, disable, reset, <mask>\n");
2356 } else {
2357 len += sprintf(p + len, "mask:\t\tnot supported\n");
2358 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2361 return len;
2364 static int hotkey_write(char *buf)
2366 int res, status;
2367 u32 mask;
2368 char *cmd;
2370 if (!tp_features.hotkey)
2371 return -ENODEV;
2373 if (mutex_lock_interruptible(&hotkey_mutex))
2374 return -ERESTARTSYS;
2376 status = -1;
2377 mask = hotkey_mask;
2379 res = 0;
2380 while ((cmd = next_cmd(&buf))) {
2381 if (strlencmp(cmd, "enable") == 0) {
2382 status = 1;
2383 } else if (strlencmp(cmd, "disable") == 0) {
2384 status = 0;
2385 } else if (strlencmp(cmd, "reset") == 0) {
2386 status = hotkey_orig_status;
2387 mask = hotkey_orig_mask;
2388 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2389 /* mask set */
2390 } else if (sscanf(cmd, "%x", &mask) == 1) {
2391 /* mask set */
2392 } else {
2393 res = -EINVAL;
2394 goto errexit;
2397 if (status != -1)
2398 res = hotkey_status_set(status);
2400 if (!res && mask != hotkey_mask)
2401 res = hotkey_mask_set(mask);
2403 errexit:
2404 mutex_unlock(&hotkey_mutex);
2405 return res;
2408 static const struct acpi_device_id ibm_htk_device_ids[] = {
2409 {TPACPI_ACPI_HKEY_HID, 0},
2410 {"", 0},
2413 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2414 .hid = ibm_htk_device_ids,
2415 .notify = hotkey_notify,
2416 .handle = &hkey_handle,
2417 .type = ACPI_DEVICE_NOTIFY,
2420 static struct ibm_struct hotkey_driver_data = {
2421 .name = "hotkey",
2422 .read = hotkey_read,
2423 .write = hotkey_write,
2424 .exit = hotkey_exit,
2425 .resume = hotkey_resume,
2426 .suspend = hotkey_suspend,
2427 .acpi = &ibm_hotkey_acpidriver,
2430 /*************************************************************************
2431 * Bluetooth subdriver
2434 enum {
2435 /* ACPI GBDC/SBDC bits */
2436 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2437 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2438 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2441 static int bluetooth_get_radiosw(void);
2442 static int bluetooth_set_radiosw(int radio_on);
2444 /* sysfs bluetooth enable ---------------------------------------------- */
2445 static ssize_t bluetooth_enable_show(struct device *dev,
2446 struct device_attribute *attr,
2447 char *buf)
2449 int status;
2451 status = bluetooth_get_radiosw();
2452 if (status < 0)
2453 return status;
2455 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2458 static ssize_t bluetooth_enable_store(struct device *dev,
2459 struct device_attribute *attr,
2460 const char *buf, size_t count)
2462 unsigned long t;
2463 int res;
2465 if (parse_strtoul(buf, 1, &t))
2466 return -EINVAL;
2468 res = bluetooth_set_radiosw(t);
2470 return (res) ? res : count;
2473 static struct device_attribute dev_attr_bluetooth_enable =
2474 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2475 bluetooth_enable_show, bluetooth_enable_store);
2477 /* --------------------------------------------------------------------- */
2479 static struct attribute *bluetooth_attributes[] = {
2480 &dev_attr_bluetooth_enable.attr,
2481 NULL
2484 static const struct attribute_group bluetooth_attr_group = {
2485 .attrs = bluetooth_attributes,
2488 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2490 int res;
2491 int status = 0;
2493 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2495 TPACPI_ACPIHANDLE_INIT(hkey);
2497 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2498 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2499 tp_features.bluetooth = hkey_handle &&
2500 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2502 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2503 str_supported(tp_features.bluetooth),
2504 status);
2506 if (tp_features.bluetooth) {
2507 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2508 /* no bluetooth hardware present in system */
2509 tp_features.bluetooth = 0;
2510 dbg_printk(TPACPI_DBG_INIT,
2511 "bluetooth hardware not installed\n");
2512 } else {
2513 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2514 &bluetooth_attr_group);
2515 if (res)
2516 return res;
2520 return (tp_features.bluetooth)? 0 : 1;
2523 static void bluetooth_exit(void)
2525 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2526 &bluetooth_attr_group);
2529 static int bluetooth_get_radiosw(void)
2531 int status;
2533 if (!tp_features.bluetooth)
2534 return -ENODEV;
2536 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2537 return -EIO;
2539 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2542 static int bluetooth_set_radiosw(int radio_on)
2544 int status;
2546 if (!tp_features.bluetooth)
2547 return -ENODEV;
2549 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2550 return -EIO;
2551 if (radio_on)
2552 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2553 else
2554 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2555 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2556 return -EIO;
2558 return 0;
2561 /* procfs -------------------------------------------------------------- */
2562 static int bluetooth_read(char *p)
2564 int len = 0;
2565 int status = bluetooth_get_radiosw();
2567 if (!tp_features.bluetooth)
2568 len += sprintf(p + len, "status:\t\tnot supported\n");
2569 else {
2570 len += sprintf(p + len, "status:\t\t%s\n",
2571 (status)? "enabled" : "disabled");
2572 len += sprintf(p + len, "commands:\tenable, disable\n");
2575 return len;
2578 static int bluetooth_write(char *buf)
2580 char *cmd;
2582 if (!tp_features.bluetooth)
2583 return -ENODEV;
2585 while ((cmd = next_cmd(&buf))) {
2586 if (strlencmp(cmd, "enable") == 0) {
2587 bluetooth_set_radiosw(1);
2588 } else if (strlencmp(cmd, "disable") == 0) {
2589 bluetooth_set_radiosw(0);
2590 } else
2591 return -EINVAL;
2594 return 0;
2597 static struct ibm_struct bluetooth_driver_data = {
2598 .name = "bluetooth",
2599 .read = bluetooth_read,
2600 .write = bluetooth_write,
2601 .exit = bluetooth_exit,
2604 /*************************************************************************
2605 * Wan subdriver
2608 enum {
2609 /* ACPI GWAN/SWAN bits */
2610 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2611 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2612 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2615 static int wan_get_radiosw(void);
2616 static int wan_set_radiosw(int radio_on);
2618 /* sysfs wan enable ---------------------------------------------------- */
2619 static ssize_t wan_enable_show(struct device *dev,
2620 struct device_attribute *attr,
2621 char *buf)
2623 int status;
2625 status = wan_get_radiosw();
2626 if (status < 0)
2627 return status;
2629 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2632 static ssize_t wan_enable_store(struct device *dev,
2633 struct device_attribute *attr,
2634 const char *buf, size_t count)
2636 unsigned long t;
2637 int res;
2639 if (parse_strtoul(buf, 1, &t))
2640 return -EINVAL;
2642 res = wan_set_radiosw(t);
2644 return (res) ? res : count;
2647 static struct device_attribute dev_attr_wan_enable =
2648 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2649 wan_enable_show, wan_enable_store);
2651 /* --------------------------------------------------------------------- */
2653 static struct attribute *wan_attributes[] = {
2654 &dev_attr_wan_enable.attr,
2655 NULL
2658 static const struct attribute_group wan_attr_group = {
2659 .attrs = wan_attributes,
2662 static int __init wan_init(struct ibm_init_struct *iibm)
2664 int res;
2665 int status = 0;
2667 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2669 TPACPI_ACPIHANDLE_INIT(hkey);
2671 tp_features.wan = hkey_handle &&
2672 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2674 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2675 str_supported(tp_features.wan),
2676 status);
2678 if (tp_features.wan) {
2679 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2680 /* no wan hardware present in system */
2681 tp_features.wan = 0;
2682 dbg_printk(TPACPI_DBG_INIT,
2683 "wan hardware not installed\n");
2684 } else {
2685 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2686 &wan_attr_group);
2687 if (res)
2688 return res;
2692 return (tp_features.wan)? 0 : 1;
2695 static void wan_exit(void)
2697 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2698 &wan_attr_group);
2701 static int wan_get_radiosw(void)
2703 int status;
2705 if (!tp_features.wan)
2706 return -ENODEV;
2708 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2709 return -EIO;
2711 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2714 static int wan_set_radiosw(int radio_on)
2716 int status;
2718 if (!tp_features.wan)
2719 return -ENODEV;
2721 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2722 return -EIO;
2723 if (radio_on)
2724 status |= TP_ACPI_WANCARD_RADIOSSW;
2725 else
2726 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2727 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2728 return -EIO;
2730 return 0;
2733 /* procfs -------------------------------------------------------------- */
2734 static int wan_read(char *p)
2736 int len = 0;
2737 int status = wan_get_radiosw();
2739 if (!tp_features.wan)
2740 len += sprintf(p + len, "status:\t\tnot supported\n");
2741 else {
2742 len += sprintf(p + len, "status:\t\t%s\n",
2743 (status)? "enabled" : "disabled");
2744 len += sprintf(p + len, "commands:\tenable, disable\n");
2747 return len;
2750 static int wan_write(char *buf)
2752 char *cmd;
2754 if (!tp_features.wan)
2755 return -ENODEV;
2757 while ((cmd = next_cmd(&buf))) {
2758 if (strlencmp(cmd, "enable") == 0) {
2759 wan_set_radiosw(1);
2760 } else if (strlencmp(cmd, "disable") == 0) {
2761 wan_set_radiosw(0);
2762 } else
2763 return -EINVAL;
2766 return 0;
2769 static struct ibm_struct wan_driver_data = {
2770 .name = "wan",
2771 .read = wan_read,
2772 .write = wan_write,
2773 .exit = wan_exit,
2774 .flags.experimental = 1,
2777 /*************************************************************************
2778 * Video subdriver
2781 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2783 enum video_access_mode {
2784 TPACPI_VIDEO_NONE = 0,
2785 TPACPI_VIDEO_570, /* 570 */
2786 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2787 TPACPI_VIDEO_NEW, /* all others */
2790 enum { /* video status flags, based on VIDEO_570 */
2791 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2792 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2793 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2796 enum { /* TPACPI_VIDEO_570 constants */
2797 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2798 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2799 * video_status_flags */
2800 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2801 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2804 static enum video_access_mode video_supported;
2805 static int video_orig_autosw;
2807 static int video_autosw_get(void);
2808 static int video_autosw_set(int enable);
2810 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2812 static int __init video_init(struct ibm_init_struct *iibm)
2814 int ivga;
2816 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2818 TPACPI_ACPIHANDLE_INIT(vid);
2819 TPACPI_ACPIHANDLE_INIT(vid2);
2821 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2822 /* G41, assume IVGA doesn't change */
2823 vid_handle = vid2_handle;
2825 if (!vid_handle)
2826 /* video switching not supported on R30, R31 */
2827 video_supported = TPACPI_VIDEO_NONE;
2828 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2829 /* 570 */
2830 video_supported = TPACPI_VIDEO_570;
2831 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2832 /* 600e/x, 770e, 770x */
2833 video_supported = TPACPI_VIDEO_770;
2834 else
2835 /* all others */
2836 video_supported = TPACPI_VIDEO_NEW;
2838 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2839 str_supported(video_supported != TPACPI_VIDEO_NONE),
2840 video_supported);
2842 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2845 static void video_exit(void)
2847 dbg_printk(TPACPI_DBG_EXIT,
2848 "restoring original video autoswitch mode\n");
2849 if (video_autosw_set(video_orig_autosw))
2850 printk(TPACPI_ERR "error while trying to restore original "
2851 "video autoswitch mode\n");
2854 static int video_outputsw_get(void)
2856 int status = 0;
2857 int i;
2859 switch (video_supported) {
2860 case TPACPI_VIDEO_570:
2861 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2862 TP_ACPI_VIDEO_570_PHSCMD))
2863 return -EIO;
2864 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2865 break;
2866 case TPACPI_VIDEO_770:
2867 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2868 return -EIO;
2869 if (i)
2870 status |= TP_ACPI_VIDEO_S_LCD;
2871 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2872 return -EIO;
2873 if (i)
2874 status |= TP_ACPI_VIDEO_S_CRT;
2875 break;
2876 case TPACPI_VIDEO_NEW:
2877 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2878 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2879 return -EIO;
2880 if (i)
2881 status |= TP_ACPI_VIDEO_S_CRT;
2883 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2884 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2885 return -EIO;
2886 if (i)
2887 status |= TP_ACPI_VIDEO_S_LCD;
2888 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2889 return -EIO;
2890 if (i)
2891 status |= TP_ACPI_VIDEO_S_DVI;
2892 break;
2893 default:
2894 return -ENOSYS;
2897 return status;
2900 static int video_outputsw_set(int status)
2902 int autosw;
2903 int res = 0;
2905 switch (video_supported) {
2906 case TPACPI_VIDEO_570:
2907 res = acpi_evalf(NULL, NULL,
2908 "\\_SB.PHS2", "vdd",
2909 TP_ACPI_VIDEO_570_PHS2CMD,
2910 status | TP_ACPI_VIDEO_570_PHS2SET);
2911 break;
2912 case TPACPI_VIDEO_770:
2913 autosw = video_autosw_get();
2914 if (autosw < 0)
2915 return autosw;
2917 res = video_autosw_set(1);
2918 if (res)
2919 return res;
2920 res = acpi_evalf(vid_handle, NULL,
2921 "ASWT", "vdd", status * 0x100, 0);
2922 if (!autosw && video_autosw_set(autosw)) {
2923 printk(TPACPI_ERR
2924 "video auto-switch left enabled due to error\n");
2925 return -EIO;
2927 break;
2928 case TPACPI_VIDEO_NEW:
2929 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2930 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2931 break;
2932 default:
2933 return -ENOSYS;
2936 return (res)? 0 : -EIO;
2939 static int video_autosw_get(void)
2941 int autosw = 0;
2943 switch (video_supported) {
2944 case TPACPI_VIDEO_570:
2945 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2946 return -EIO;
2947 break;
2948 case TPACPI_VIDEO_770:
2949 case TPACPI_VIDEO_NEW:
2950 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2951 return -EIO;
2952 break;
2953 default:
2954 return -ENOSYS;
2957 return autosw & 1;
2960 static int video_autosw_set(int enable)
2962 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2963 return -EIO;
2964 return 0;
2967 static int video_outputsw_cycle(void)
2969 int autosw = video_autosw_get();
2970 int res;
2972 if (autosw < 0)
2973 return autosw;
2975 switch (video_supported) {
2976 case TPACPI_VIDEO_570:
2977 res = video_autosw_set(1);
2978 if (res)
2979 return res;
2980 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2981 break;
2982 case TPACPI_VIDEO_770:
2983 case TPACPI_VIDEO_NEW:
2984 res = video_autosw_set(1);
2985 if (res)
2986 return res;
2987 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2988 break;
2989 default:
2990 return -ENOSYS;
2992 if (!autosw && video_autosw_set(autosw)) {
2993 printk(TPACPI_ERR
2994 "video auto-switch left enabled due to error\n");
2995 return -EIO;
2998 return (res)? 0 : -EIO;
3001 static int video_expand_toggle(void)
3003 switch (video_supported) {
3004 case TPACPI_VIDEO_570:
3005 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3006 0 : -EIO;
3007 case TPACPI_VIDEO_770:
3008 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3009 0 : -EIO;
3010 case TPACPI_VIDEO_NEW:
3011 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3012 0 : -EIO;
3013 default:
3014 return -ENOSYS;
3016 /* not reached */
3019 static int video_read(char *p)
3021 int status, autosw;
3022 int len = 0;
3024 if (video_supported == TPACPI_VIDEO_NONE) {
3025 len += sprintf(p + len, "status:\t\tnot supported\n");
3026 return len;
3029 status = video_outputsw_get();
3030 if (status < 0)
3031 return status;
3033 autosw = video_autosw_get();
3034 if (autosw < 0)
3035 return autosw;
3037 len += sprintf(p + len, "status:\t\tsupported\n");
3038 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3039 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3040 if (video_supported == TPACPI_VIDEO_NEW)
3041 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3042 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3043 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3044 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3045 if (video_supported == TPACPI_VIDEO_NEW)
3046 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3047 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3048 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3050 return len;
3053 static int video_write(char *buf)
3055 char *cmd;
3056 int enable, disable, status;
3057 int res;
3059 if (video_supported == TPACPI_VIDEO_NONE)
3060 return -ENODEV;
3062 enable = 0;
3063 disable = 0;
3065 while ((cmd = next_cmd(&buf))) {
3066 if (strlencmp(cmd, "lcd_enable") == 0) {
3067 enable |= TP_ACPI_VIDEO_S_LCD;
3068 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3069 disable |= TP_ACPI_VIDEO_S_LCD;
3070 } else if (strlencmp(cmd, "crt_enable") == 0) {
3071 enable |= TP_ACPI_VIDEO_S_CRT;
3072 } else if (strlencmp(cmd, "crt_disable") == 0) {
3073 disable |= TP_ACPI_VIDEO_S_CRT;
3074 } else if (video_supported == TPACPI_VIDEO_NEW &&
3075 strlencmp(cmd, "dvi_enable") == 0) {
3076 enable |= TP_ACPI_VIDEO_S_DVI;
3077 } else if (video_supported == TPACPI_VIDEO_NEW &&
3078 strlencmp(cmd, "dvi_disable") == 0) {
3079 disable |= TP_ACPI_VIDEO_S_DVI;
3080 } else if (strlencmp(cmd, "auto_enable") == 0) {
3081 res = video_autosw_set(1);
3082 if (res)
3083 return res;
3084 } else if (strlencmp(cmd, "auto_disable") == 0) {
3085 res = video_autosw_set(0);
3086 if (res)
3087 return res;
3088 } else if (strlencmp(cmd, "video_switch") == 0) {
3089 res = video_outputsw_cycle();
3090 if (res)
3091 return res;
3092 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3093 res = video_expand_toggle();
3094 if (res)
3095 return res;
3096 } else
3097 return -EINVAL;
3100 if (enable || disable) {
3101 status = video_outputsw_get();
3102 if (status < 0)
3103 return status;
3104 res = video_outputsw_set((status & ~disable) | enable);
3105 if (res)
3106 return res;
3109 return 0;
3112 static struct ibm_struct video_driver_data = {
3113 .name = "video",
3114 .read = video_read,
3115 .write = video_write,
3116 .exit = video_exit,
3119 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3121 /*************************************************************************
3122 * Light (thinklight) subdriver
3125 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3126 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3128 static int __init light_init(struct ibm_init_struct *iibm)
3130 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3132 TPACPI_ACPIHANDLE_INIT(ledb);
3133 TPACPI_ACPIHANDLE_INIT(lght);
3134 TPACPI_ACPIHANDLE_INIT(cmos);
3136 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3137 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3139 if (tp_features.light)
3140 /* light status not supported on
3141 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3142 tp_features.light_status =
3143 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3145 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3146 str_supported(tp_features.light));
3148 return (tp_features.light)? 0 : 1;
3151 static int light_read(char *p)
3153 int len = 0;
3154 int status = 0;
3156 if (!tp_features.light) {
3157 len += sprintf(p + len, "status:\t\tnot supported\n");
3158 } else if (!tp_features.light_status) {
3159 len += sprintf(p + len, "status:\t\tunknown\n");
3160 len += sprintf(p + len, "commands:\ton, off\n");
3161 } else {
3162 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3163 return -EIO;
3164 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3165 len += sprintf(p + len, "commands:\ton, off\n");
3168 return len;
3171 static int light_write(char *buf)
3173 int cmos_cmd, lght_cmd;
3174 char *cmd;
3175 int success;
3177 if (!tp_features.light)
3178 return -ENODEV;
3180 while ((cmd = next_cmd(&buf))) {
3181 if (strlencmp(cmd, "on") == 0) {
3182 cmos_cmd = 0x0c;
3183 lght_cmd = 1;
3184 } else if (strlencmp(cmd, "off") == 0) {
3185 cmos_cmd = 0x0d;
3186 lght_cmd = 0;
3187 } else
3188 return -EINVAL;
3190 success = cmos_handle ?
3191 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3192 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3193 if (!success)
3194 return -EIO;
3197 return 0;
3200 static struct ibm_struct light_driver_data = {
3201 .name = "light",
3202 .read = light_read,
3203 .write = light_write,
3206 /*************************************************************************
3207 * Dock subdriver
3210 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3212 static void dock_notify(struct ibm_struct *ibm, u32 event);
3213 static int dock_read(char *p);
3214 static int dock_write(char *buf);
3216 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3217 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3218 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3219 "\\_SB.PCI.ISA.SLCE", /* 570 */
3220 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3222 /* don't list other alternatives as we install a notify handler on the 570 */
3223 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3225 static const struct acpi_device_id ibm_pci_device_ids[] = {
3226 {PCI_ROOT_HID_STRING, 0},
3227 {"", 0},
3230 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3232 .notify = dock_notify,
3233 .handle = &dock_handle,
3234 .type = ACPI_SYSTEM_NOTIFY,
3237 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3238 * We just use it to get notifications of dock hotplug
3239 * in very old thinkpads */
3240 .hid = ibm_pci_device_ids,
3241 .notify = dock_notify,
3242 .handle = &pci_handle,
3243 .type = ACPI_SYSTEM_NOTIFY,
3247 static struct ibm_struct dock_driver_data[2] = {
3249 .name = "dock",
3250 .read = dock_read,
3251 .write = dock_write,
3252 .acpi = &ibm_dock_acpidriver[0],
3255 .name = "dock",
3256 .acpi = &ibm_dock_acpidriver[1],
3260 #define dock_docked() (_sta(dock_handle) & 1)
3262 static int __init dock_init(struct ibm_init_struct *iibm)
3264 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3266 TPACPI_ACPIHANDLE_INIT(dock);
3268 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3269 str_supported(dock_handle != NULL));
3271 return (dock_handle)? 0 : 1;
3274 static int __init dock_init2(struct ibm_init_struct *iibm)
3276 int dock2_needed;
3278 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3280 if (dock_driver_data[0].flags.acpi_driver_registered &&
3281 dock_driver_data[0].flags.acpi_notify_installed) {
3282 TPACPI_ACPIHANDLE_INIT(pci);
3283 dock2_needed = (pci_handle != NULL);
3284 vdbg_printk(TPACPI_DBG_INIT,
3285 "dock PCI handler for the TP 570 is %s\n",
3286 str_supported(dock2_needed));
3287 } else {
3288 vdbg_printk(TPACPI_DBG_INIT,
3289 "dock subdriver part 2 not required\n");
3290 dock2_needed = 0;
3293 return (dock2_needed)? 0 : 1;
3296 static void dock_notify(struct ibm_struct *ibm, u32 event)
3298 int docked = dock_docked();
3299 int pci = ibm->acpi->hid && ibm->acpi->device &&
3300 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3301 int data;
3303 if (event == 1 && !pci) /* 570 */
3304 data = 1; /* button */
3305 else if (event == 1 && pci) /* 570 */
3306 data = 3; /* dock */
3307 else if (event == 3 && docked)
3308 data = 1; /* button */
3309 else if (event == 3 && !docked)
3310 data = 2; /* undock */
3311 else if (event == 0 && docked)
3312 data = 3; /* dock */
3313 else {
3314 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3315 event, _sta(dock_handle));
3316 data = 0; /* unknown */
3318 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3319 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3320 ibm->acpi->device->dev.bus_id,
3321 event, data);
3324 static int dock_read(char *p)
3326 int len = 0;
3327 int docked = dock_docked();
3329 if (!dock_handle)
3330 len += sprintf(p + len, "status:\t\tnot supported\n");
3331 else if (!docked)
3332 len += sprintf(p + len, "status:\t\tundocked\n");
3333 else {
3334 len += sprintf(p + len, "status:\t\tdocked\n");
3335 len += sprintf(p + len, "commands:\tdock, undock\n");
3338 return len;
3341 static int dock_write(char *buf)
3343 char *cmd;
3345 if (!dock_docked())
3346 return -ENODEV;
3348 while ((cmd = next_cmd(&buf))) {
3349 if (strlencmp(cmd, "undock") == 0) {
3350 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3351 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3352 return -EIO;
3353 } else if (strlencmp(cmd, "dock") == 0) {
3354 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3355 return -EIO;
3356 } else
3357 return -EINVAL;
3360 return 0;
3363 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3365 /*************************************************************************
3366 * Bay subdriver
3369 #ifdef CONFIG_THINKPAD_ACPI_BAY
3371 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3372 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3373 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3374 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3375 ); /* A21e, R30, R31 */
3376 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3377 "_EJ0", /* all others */
3378 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3379 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3380 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3381 ); /* all others */
3382 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3383 "_EJ0", /* 770x */
3384 ); /* all others */
3386 static int __init bay_init(struct ibm_init_struct *iibm)
3388 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3390 TPACPI_ACPIHANDLE_INIT(bay);
3391 if (bay_handle)
3392 TPACPI_ACPIHANDLE_INIT(bay_ej);
3393 TPACPI_ACPIHANDLE_INIT(bay2);
3394 if (bay2_handle)
3395 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3397 tp_features.bay_status = bay_handle &&
3398 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3399 tp_features.bay_status2 = bay2_handle &&
3400 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3402 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3403 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3404 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3405 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3407 vdbg_printk(TPACPI_DBG_INIT,
3408 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3409 str_supported(tp_features.bay_status),
3410 str_supported(tp_features.bay_eject),
3411 str_supported(tp_features.bay_status2),
3412 str_supported(tp_features.bay_eject2));
3414 return (tp_features.bay_status || tp_features.bay_eject ||
3415 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3418 static void bay_notify(struct ibm_struct *ibm, u32 event)
3420 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3421 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3422 ibm->acpi->device->dev.bus_id,
3423 event, 0);
3426 #define bay_occupied(b) (_sta(b##_handle) & 1)
3428 static int bay_read(char *p)
3430 int len = 0;
3431 int occupied = bay_occupied(bay);
3432 int occupied2 = bay_occupied(bay2);
3433 int eject, eject2;
3435 len += sprintf(p + len, "status:\t\t%s\n",
3436 tp_features.bay_status ?
3437 (occupied ? "occupied" : "unoccupied") :
3438 "not supported");
3439 if (tp_features.bay_status2)
3440 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3441 "occupied" : "unoccupied");
3443 eject = tp_features.bay_eject && occupied;
3444 eject2 = tp_features.bay_eject2 && occupied2;
3446 if (eject && eject2)
3447 len += sprintf(p + len, "commands:\teject, eject2\n");
3448 else if (eject)
3449 len += sprintf(p + len, "commands:\teject\n");
3450 else if (eject2)
3451 len += sprintf(p + len, "commands:\teject2\n");
3453 return len;
3456 static int bay_write(char *buf)
3458 char *cmd;
3460 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3461 return -ENODEV;
3463 while ((cmd = next_cmd(&buf))) {
3464 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3465 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3466 return -EIO;
3467 } else if (tp_features.bay_eject2 &&
3468 strlencmp(cmd, "eject2") == 0) {
3469 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3470 return -EIO;
3471 } else
3472 return -EINVAL;
3475 return 0;
3478 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3479 .notify = bay_notify,
3480 .handle = &bay_handle,
3481 .type = ACPI_SYSTEM_NOTIFY,
3484 static struct ibm_struct bay_driver_data = {
3485 .name = "bay",
3486 .read = bay_read,
3487 .write = bay_write,
3488 .acpi = &ibm_bay_acpidriver,
3491 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3493 /*************************************************************************
3494 * CMOS subdriver
3497 /* sysfs cmos_command -------------------------------------------------- */
3498 static ssize_t cmos_command_store(struct device *dev,
3499 struct device_attribute *attr,
3500 const char *buf, size_t count)
3502 unsigned long cmos_cmd;
3503 int res;
3505 if (parse_strtoul(buf, 21, &cmos_cmd))
3506 return -EINVAL;
3508 res = issue_thinkpad_cmos_command(cmos_cmd);
3509 return (res)? res : count;
3512 static struct device_attribute dev_attr_cmos_command =
3513 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3515 /* --------------------------------------------------------------------- */
3517 static int __init cmos_init(struct ibm_init_struct *iibm)
3519 int res;
3521 vdbg_printk(TPACPI_DBG_INIT,
3522 "initializing cmos commands subdriver\n");
3524 TPACPI_ACPIHANDLE_INIT(cmos);
3526 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3527 str_supported(cmos_handle != NULL));
3529 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3530 if (res)
3531 return res;
3533 return (cmos_handle)? 0 : 1;
3536 static void cmos_exit(void)
3538 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3541 static int cmos_read(char *p)
3543 int len = 0;
3545 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3546 R30, R31, T20-22, X20-21 */
3547 if (!cmos_handle)
3548 len += sprintf(p + len, "status:\t\tnot supported\n");
3549 else {
3550 len += sprintf(p + len, "status:\t\tsupported\n");
3551 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3554 return len;
3557 static int cmos_write(char *buf)
3559 char *cmd;
3560 int cmos_cmd, res;
3562 while ((cmd = next_cmd(&buf))) {
3563 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3564 cmos_cmd >= 0 && cmos_cmd <= 21) {
3565 /* cmos_cmd set */
3566 } else
3567 return -EINVAL;
3569 res = issue_thinkpad_cmos_command(cmos_cmd);
3570 if (res)
3571 return res;
3574 return 0;
3577 static struct ibm_struct cmos_driver_data = {
3578 .name = "cmos",
3579 .read = cmos_read,
3580 .write = cmos_write,
3581 .exit = cmos_exit,
3584 /*************************************************************************
3585 * LED subdriver
3588 enum led_access_mode {
3589 TPACPI_LED_NONE = 0,
3590 TPACPI_LED_570, /* 570 */
3591 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3592 TPACPI_LED_NEW, /* all others */
3595 enum { /* For TPACPI_LED_OLD */
3596 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3597 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3598 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3601 static enum led_access_mode led_supported;
3603 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3604 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3605 /* T20-22, X20-21 */
3606 "LED", /* all others */
3607 ); /* R30, R31 */
3609 static int __init led_init(struct ibm_init_struct *iibm)
3611 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3613 TPACPI_ACPIHANDLE_INIT(led);
3615 if (!led_handle)
3616 /* led not supported on R30, R31 */
3617 led_supported = TPACPI_LED_NONE;
3618 else if (strlencmp(led_path, "SLED") == 0)
3619 /* 570 */
3620 led_supported = TPACPI_LED_570;
3621 else if (strlencmp(led_path, "SYSL") == 0)
3622 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3623 led_supported = TPACPI_LED_OLD;
3624 else
3625 /* all others */
3626 led_supported = TPACPI_LED_NEW;
3628 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3629 str_supported(led_supported), led_supported);
3631 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3634 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3636 static int led_read(char *p)
3638 int len = 0;
3640 if (!led_supported) {
3641 len += sprintf(p + len, "status:\t\tnot supported\n");
3642 return len;
3644 len += sprintf(p + len, "status:\t\tsupported\n");
3646 if (led_supported == TPACPI_LED_570) {
3647 /* 570 */
3648 int i, status;
3649 for (i = 0; i < 8; i++) {
3650 if (!acpi_evalf(ec_handle,
3651 &status, "GLED", "dd", 1 << i))
3652 return -EIO;
3653 len += sprintf(p + len, "%d:\t\t%s\n",
3654 i, led_status(status));
3658 len += sprintf(p + len, "commands:\t"
3659 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3661 return len;
3664 /* off, on, blink */
3665 static const int led_sled_arg1[] = { 0, 1, 3 };
3666 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3667 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3668 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3670 static int led_write(char *buf)
3672 char *cmd;
3673 int led, ind, ret;
3675 if (!led_supported)
3676 return -ENODEV;
3678 while ((cmd = next_cmd(&buf))) {
3679 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3680 return -EINVAL;
3682 if (strstr(cmd, "off")) {
3683 ind = 0;
3684 } else if (strstr(cmd, "on")) {
3685 ind = 1;
3686 } else if (strstr(cmd, "blink")) {
3687 ind = 2;
3688 } else
3689 return -EINVAL;
3691 if (led_supported == TPACPI_LED_570) {
3692 /* 570 */
3693 led = 1 << led;
3694 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3695 led, led_sled_arg1[ind]))
3696 return -EIO;
3697 } else if (led_supported == TPACPI_LED_OLD) {
3698 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3699 led = 1 << led;
3700 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3701 if (ret >= 0)
3702 ret = ec_write(TPACPI_LED_EC_HLBL,
3703 led * led_exp_hlbl[ind]);
3704 if (ret >= 0)
3705 ret = ec_write(TPACPI_LED_EC_HLCL,
3706 led * led_exp_hlcl[ind]);
3707 if (ret < 0)
3708 return ret;
3709 } else {
3710 /* all others */
3711 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3712 led, led_led_arg1[ind]))
3713 return -EIO;
3717 return 0;
3720 static struct ibm_struct led_driver_data = {
3721 .name = "led",
3722 .read = led_read,
3723 .write = led_write,
3726 /*************************************************************************
3727 * Beep subdriver
3730 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3732 static int __init beep_init(struct ibm_init_struct *iibm)
3734 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3736 TPACPI_ACPIHANDLE_INIT(beep);
3738 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3739 str_supported(beep_handle != NULL));
3741 return (beep_handle)? 0 : 1;
3744 static int beep_read(char *p)
3746 int len = 0;
3748 if (!beep_handle)
3749 len += sprintf(p + len, "status:\t\tnot supported\n");
3750 else {
3751 len += sprintf(p + len, "status:\t\tsupported\n");
3752 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3755 return len;
3758 static int beep_write(char *buf)
3760 char *cmd;
3761 int beep_cmd;
3763 if (!beep_handle)
3764 return -ENODEV;
3766 while ((cmd = next_cmd(&buf))) {
3767 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3768 beep_cmd >= 0 && beep_cmd <= 17) {
3769 /* beep_cmd set */
3770 } else
3771 return -EINVAL;
3772 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3773 return -EIO;
3776 return 0;
3779 static struct ibm_struct beep_driver_data = {
3780 .name = "beep",
3781 .read = beep_read,
3782 .write = beep_write,
3785 /*************************************************************************
3786 * Thermal subdriver
3789 enum thermal_access_mode {
3790 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3791 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3792 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3793 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3794 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3797 enum { /* TPACPI_THERMAL_TPEC_* */
3798 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3799 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3800 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3803 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3804 struct ibm_thermal_sensors_struct {
3805 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3808 static enum thermal_access_mode thermal_read_mode;
3810 /* idx is zero-based */
3811 static int thermal_get_sensor(int idx, s32 *value)
3813 int t;
3814 s8 tmp;
3815 char tmpi[5];
3817 t = TP_EC_THERMAL_TMP0;
3819 switch (thermal_read_mode) {
3820 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3821 case TPACPI_THERMAL_TPEC_16:
3822 if (idx >= 8 && idx <= 15) {
3823 t = TP_EC_THERMAL_TMP8;
3824 idx -= 8;
3826 /* fallthrough */
3827 #endif
3828 case TPACPI_THERMAL_TPEC_8:
3829 if (idx <= 7) {
3830 if (!acpi_ec_read(t + idx, &tmp))
3831 return -EIO;
3832 *value = tmp * 1000;
3833 return 0;
3835 break;
3837 case TPACPI_THERMAL_ACPI_UPDT:
3838 if (idx <= 7) {
3839 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3840 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3841 return -EIO;
3842 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3843 return -EIO;
3844 *value = (t - 2732) * 100;
3845 return 0;
3847 break;
3849 case TPACPI_THERMAL_ACPI_TMP07:
3850 if (idx <= 7) {
3851 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3852 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3853 return -EIO;
3854 if (t > 127 || t < -127)
3855 t = TP_EC_THERMAL_TMP_NA;
3856 *value = t * 1000;
3857 return 0;
3859 break;
3861 case TPACPI_THERMAL_NONE:
3862 default:
3863 return -ENOSYS;
3866 return -EINVAL;
3869 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3871 int res, i;
3872 int n;
3874 n = 8;
3875 i = 0;
3877 if (!s)
3878 return -EINVAL;
3880 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3881 n = 16;
3883 for (i = 0 ; i < n; i++) {
3884 res = thermal_get_sensor(i, &s->temp[i]);
3885 if (res)
3886 return res;
3889 return n;
3892 /* sysfs temp##_input -------------------------------------------------- */
3894 static ssize_t thermal_temp_input_show(struct device *dev,
3895 struct device_attribute *attr,
3896 char *buf)
3898 struct sensor_device_attribute *sensor_attr =
3899 to_sensor_dev_attr(attr);
3900 int idx = sensor_attr->index;
3901 s32 value;
3902 int res;
3904 res = thermal_get_sensor(idx, &value);
3905 if (res)
3906 return res;
3907 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3908 return -ENXIO;
3910 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3913 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3914 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3915 thermal_temp_input_show, NULL, _idxB)
3917 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3918 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3919 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3920 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3921 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3922 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3923 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3924 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3925 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3926 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3927 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3928 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3929 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3930 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3931 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3932 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3933 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3936 #define THERMAL_ATTRS(X) \
3937 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3939 static struct attribute *thermal_temp_input_attr[] = {
3940 THERMAL_ATTRS(8),
3941 THERMAL_ATTRS(9),
3942 THERMAL_ATTRS(10),
3943 THERMAL_ATTRS(11),
3944 THERMAL_ATTRS(12),
3945 THERMAL_ATTRS(13),
3946 THERMAL_ATTRS(14),
3947 THERMAL_ATTRS(15),
3948 THERMAL_ATTRS(0),
3949 THERMAL_ATTRS(1),
3950 THERMAL_ATTRS(2),
3951 THERMAL_ATTRS(3),
3952 THERMAL_ATTRS(4),
3953 THERMAL_ATTRS(5),
3954 THERMAL_ATTRS(6),
3955 THERMAL_ATTRS(7),
3956 NULL
3959 static const struct attribute_group thermal_temp_input16_group = {
3960 .attrs = thermal_temp_input_attr
3963 static const struct attribute_group thermal_temp_input8_group = {
3964 .attrs = &thermal_temp_input_attr[8]
3967 #undef THERMAL_SENSOR_ATTR_TEMP
3968 #undef THERMAL_ATTRS
3970 /* --------------------------------------------------------------------- */
3972 static int __init thermal_init(struct ibm_init_struct *iibm)
3974 u8 t, ta1, ta2;
3975 int i;
3976 int acpi_tmp7;
3977 int res;
3979 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3981 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3983 if (thinkpad_id.ec_model) {
3985 * Direct EC access mode: sensors at registers
3986 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3987 * non-implemented, thermal sensors return 0x80 when
3988 * not available
3991 ta1 = ta2 = 0;
3992 for (i = 0; i < 8; i++) {
3993 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3994 ta1 |= t;
3995 } else {
3996 ta1 = 0;
3997 break;
3999 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4000 ta2 |= t;
4001 } else {
4002 ta1 = 0;
4003 break;
4006 if (ta1 == 0) {
4007 /* This is sheer paranoia, but we handle it anyway */
4008 if (acpi_tmp7) {
4009 printk(TPACPI_ERR
4010 "ThinkPad ACPI EC access misbehaving, "
4011 "falling back to ACPI TMPx access "
4012 "mode\n");
4013 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4014 } else {
4015 printk(TPACPI_ERR
4016 "ThinkPad ACPI EC access misbehaving, "
4017 "disabling thermal sensors access\n");
4018 thermal_read_mode = TPACPI_THERMAL_NONE;
4020 } else {
4021 thermal_read_mode =
4022 (ta2 != 0) ?
4023 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4025 } else if (acpi_tmp7) {
4026 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4027 /* 600e/x, 770e, 770x */
4028 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4029 } else {
4030 /* Standard ACPI TMPx access, max 8 sensors */
4031 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4033 } else {
4034 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4035 thermal_read_mode = TPACPI_THERMAL_NONE;
4038 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4039 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4040 thermal_read_mode);
4042 switch (thermal_read_mode) {
4043 case TPACPI_THERMAL_TPEC_16:
4044 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4045 &thermal_temp_input16_group);
4046 if (res)
4047 return res;
4048 break;
4049 case TPACPI_THERMAL_TPEC_8:
4050 case TPACPI_THERMAL_ACPI_TMP07:
4051 case TPACPI_THERMAL_ACPI_UPDT:
4052 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4053 &thermal_temp_input8_group);
4054 if (res)
4055 return res;
4056 break;
4057 case TPACPI_THERMAL_NONE:
4058 default:
4059 return 1;
4062 return 0;
4065 static void thermal_exit(void)
4067 switch (thermal_read_mode) {
4068 case TPACPI_THERMAL_TPEC_16:
4069 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4070 &thermal_temp_input16_group);
4071 break;
4072 case TPACPI_THERMAL_TPEC_8:
4073 case TPACPI_THERMAL_ACPI_TMP07:
4074 case TPACPI_THERMAL_ACPI_UPDT:
4075 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4076 &thermal_temp_input16_group);
4077 break;
4078 case TPACPI_THERMAL_NONE:
4079 default:
4080 break;
4084 static int thermal_read(char *p)
4086 int len = 0;
4087 int n, i;
4088 struct ibm_thermal_sensors_struct t;
4090 n = thermal_get_sensors(&t);
4091 if (unlikely(n < 0))
4092 return n;
4094 len += sprintf(p + len, "temperatures:\t");
4096 if (n > 0) {
4097 for (i = 0; i < (n - 1); i++)
4098 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4099 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4100 } else
4101 len += sprintf(p + len, "not supported\n");
4103 return len;
4106 static struct ibm_struct thermal_driver_data = {
4107 .name = "thermal",
4108 .read = thermal_read,
4109 .exit = thermal_exit,
4112 /*************************************************************************
4113 * EC Dump subdriver
4116 static u8 ecdump_regs[256];
4118 static int ecdump_read(char *p)
4120 int len = 0;
4121 int i, j;
4122 u8 v;
4124 len += sprintf(p + len, "EC "
4125 " +00 +01 +02 +03 +04 +05 +06 +07"
4126 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4127 for (i = 0; i < 256; i += 16) {
4128 len += sprintf(p + len, "EC 0x%02x:", i);
4129 for (j = 0; j < 16; j++) {
4130 if (!acpi_ec_read(i + j, &v))
4131 break;
4132 if (v != ecdump_regs[i + j])
4133 len += sprintf(p + len, " *%02x", v);
4134 else
4135 len += sprintf(p + len, " %02x", v);
4136 ecdump_regs[i + j] = v;
4138 len += sprintf(p + len, "\n");
4139 if (j != 16)
4140 break;
4143 /* These are way too dangerous to advertise openly... */
4144 #if 0
4145 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4146 " (<offset> is 00-ff, <value> is 00-ff)\n");
4147 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4148 " (<offset> is 00-ff, <value> is 0-255)\n");
4149 #endif
4150 return len;
4153 static int ecdump_write(char *buf)
4155 char *cmd;
4156 int i, v;
4158 while ((cmd = next_cmd(&buf))) {
4159 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4160 /* i and v set */
4161 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4162 /* i and v set */
4163 } else
4164 return -EINVAL;
4165 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4166 if (!acpi_ec_write(i, v))
4167 return -EIO;
4168 } else
4169 return -EINVAL;
4172 return 0;
4175 static struct ibm_struct ecdump_driver_data = {
4176 .name = "ecdump",
4177 .read = ecdump_read,
4178 .write = ecdump_write,
4179 .flags.experimental = 1,
4182 /*************************************************************************
4183 * Backlight/brightness subdriver
4186 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4188 static struct backlight_device *ibm_backlight_device;
4189 static int brightness_offset = 0x31;
4190 static int brightness_mode;
4191 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4193 static struct mutex brightness_mutex;
4196 * ThinkPads can read brightness from two places: EC 0x31, or
4197 * CMOS NVRAM byte 0x5E, bits 0-3.
4199 static int brightness_get(struct backlight_device *bd)
4201 u8 lec = 0, lcmos = 0, level = 0;
4203 if (brightness_mode & 1) {
4204 if (!acpi_ec_read(brightness_offset, &lec))
4205 return -EIO;
4206 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4207 level = lec;
4209 if (brightness_mode & 2) {
4210 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4211 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4212 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4213 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4214 level = lcmos;
4217 if (brightness_mode == 3 && lec != lcmos) {
4218 printk(TPACPI_ERR
4219 "CMOS NVRAM (%u) and EC (%u) do not agree "
4220 "on display brightness level\n",
4221 (unsigned int) lcmos,
4222 (unsigned int) lec);
4223 return -EIO;
4226 return level;
4229 /* May return EINTR which can always be mapped to ERESTARTSYS */
4230 static int brightness_set(int value)
4232 int cmos_cmd, inc, i, res;
4233 int current_value;
4235 if (value > ((tp_features.bright_16levels)? 15 : 7))
4236 return -EINVAL;
4238 res = mutex_lock_interruptible(&brightness_mutex);
4239 if (res < 0)
4240 return res;
4242 current_value = brightness_get(NULL);
4243 if (current_value < 0) {
4244 res = current_value;
4245 goto errout;
4248 cmos_cmd = value > current_value ?
4249 TP_CMOS_BRIGHTNESS_UP :
4250 TP_CMOS_BRIGHTNESS_DOWN;
4251 inc = (value > current_value)? 1 : -1;
4253 res = 0;
4254 for (i = current_value; i != value; i += inc) {
4255 if ((brightness_mode & 2) &&
4256 issue_thinkpad_cmos_command(cmos_cmd)) {
4257 res = -EIO;
4258 goto errout;
4260 if ((brightness_mode & 1) &&
4261 !acpi_ec_write(brightness_offset, i + inc)) {
4262 res = -EIO;
4263 goto errout;;
4267 errout:
4268 mutex_unlock(&brightness_mutex);
4269 return res;
4272 /* sysfs backlight class ----------------------------------------------- */
4274 static int brightness_update_status(struct backlight_device *bd)
4276 /* it is the backlight class's job (caller) to handle
4277 * EINTR and other errors properly */
4278 return brightness_set(
4279 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4280 bd->props.power == FB_BLANK_UNBLANK) ?
4281 bd->props.brightness : 0);
4284 static struct backlight_ops ibm_backlight_data = {
4285 .get_brightness = brightness_get,
4286 .update_status = brightness_update_status,
4289 /* --------------------------------------------------------------------- */
4291 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
4293 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4294 union acpi_object *obj;
4295 int rc;
4297 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4298 obj = (union acpi_object *)buffer.pointer;
4299 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4300 printk(TPACPI_ERR "Unknown _BCL data, "
4301 "please report this to %s\n", TPACPI_MAIL);
4302 rc = 0;
4303 } else {
4304 rc = obj->package.count;
4306 } else {
4307 return 0;
4310 kfree(buffer.pointer);
4311 return rc;
4314 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4315 void *context, void **rv)
4317 char name[ACPI_PATH_SEGMENT_LENGTH];
4318 struct acpi_buffer buffer = { sizeof(name), &name };
4320 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4321 !strncmp("_BCL", name, sizeof(name) - 1)) {
4322 BUG_ON(!rv || !*rv);
4323 **(int **)rv = tpacpi_query_bcl_levels(handle);
4324 return AE_CTRL_TERMINATE;
4325 } else {
4326 return AE_OK;
4331 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
4333 static int __init brightness_check_std_acpi_support(void)
4335 int status;
4336 int bcl_levels = 0;
4337 void *bcl_ptr = &bcl_levels;
4339 if (!vid_handle) {
4340 TPACPI_ACPIHANDLE_INIT(vid);
4342 if (!vid_handle)
4343 return 0;
4346 * Search for a _BCL method, and execute it. This is safe on all
4347 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
4348 * BIOS in ACPI backlight control mode. We do NOT have to care
4349 * about calling the _BCL method in an enabled video device, any
4350 * will do for our purposes.
4353 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4354 brightness_find_bcl, NULL, &bcl_ptr);
4356 if (ACPI_SUCCESS(status) && bcl_levels > 2)
4357 return (bcl_levels - 2);
4359 return 0;
4362 static int __init brightness_init(struct ibm_init_struct *iibm)
4364 int b;
4366 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4368 mutex_init(&brightness_mutex);
4371 * We always attempt to detect acpi support, so as to switch
4372 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4373 * going to publish a backlight interface
4375 b = brightness_check_std_acpi_support();
4376 if (b > 0) {
4377 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4378 printk(TPACPI_NOTICE
4379 "Lenovo BIOS switched to ACPI backlight "
4380 "control mode\n");
4382 if (brightness_enable > 1) {
4383 printk(TPACPI_NOTICE
4384 "standard ACPI backlight interface "
4385 "available, not loading native one...\n");
4386 return 1;
4390 if (!brightness_enable) {
4391 dbg_printk(TPACPI_DBG_INIT,
4392 "brightness support disabled by "
4393 "module parameter\n");
4394 return 1;
4397 if (b > 16) {
4398 printk(TPACPI_ERR
4399 "Unsupported brightness interface, "
4400 "please contact %s\n", TPACPI_MAIL);
4401 return 1;
4403 if (b == 16)
4404 tp_features.bright_16levels = 1;
4406 if (!brightness_mode) {
4407 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4408 brightness_mode = 2;
4409 else
4410 brightness_mode = 3;
4412 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4413 brightness_mode);
4416 if (brightness_mode > 3)
4417 return -EINVAL;
4419 b = brightness_get(NULL);
4420 if (b < 0)
4421 return 1;
4423 if (tp_features.bright_16levels)
4424 printk(TPACPI_INFO
4425 "detected a 16-level brightness capable ThinkPad\n");
4427 ibm_backlight_device = backlight_device_register(
4428 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4429 &ibm_backlight_data);
4430 if (IS_ERR(ibm_backlight_device)) {
4431 printk(TPACPI_ERR "Could not register backlight device\n");
4432 return PTR_ERR(ibm_backlight_device);
4434 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4436 ibm_backlight_device->props.max_brightness =
4437 (tp_features.bright_16levels)? 15 : 7;
4438 ibm_backlight_device->props.brightness = b;
4439 backlight_update_status(ibm_backlight_device);
4441 return 0;
4444 static void brightness_exit(void)
4446 if (ibm_backlight_device) {
4447 vdbg_printk(TPACPI_DBG_EXIT,
4448 "calling backlight_device_unregister()\n");
4449 backlight_device_unregister(ibm_backlight_device);
4450 ibm_backlight_device = NULL;
4454 static int brightness_read(char *p)
4456 int len = 0;
4457 int level;
4459 level = brightness_get(NULL);
4460 if (level < 0) {
4461 len += sprintf(p + len, "level:\t\tunreadable\n");
4462 } else {
4463 len += sprintf(p + len, "level:\t\t%d\n", level);
4464 len += sprintf(p + len, "commands:\tup, down\n");
4465 len += sprintf(p + len, "commands:\tlevel <level>"
4466 " (<level> is 0-%d)\n",
4467 (tp_features.bright_16levels) ? 15 : 7);
4470 return len;
4473 static int brightness_write(char *buf)
4475 int level;
4476 int rc;
4477 char *cmd;
4478 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4480 level = brightness_get(NULL);
4481 if (level < 0)
4482 return level;
4484 while ((cmd = next_cmd(&buf))) {
4485 if (strlencmp(cmd, "up") == 0) {
4486 if (level < max_level)
4487 level++;
4488 } else if (strlencmp(cmd, "down") == 0) {
4489 if (level > 0)
4490 level--;
4491 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4492 level >= 0 && level <= max_level) {
4493 /* new level set */
4494 } else
4495 return -EINVAL;
4499 * Now we know what the final level should be, so we try to set it.
4500 * Doing it this way makes the syscall restartable in case of EINTR
4502 rc = brightness_set(level);
4503 return (rc == -EINTR)? ERESTARTSYS : rc;
4506 static struct ibm_struct brightness_driver_data = {
4507 .name = "brightness",
4508 .read = brightness_read,
4509 .write = brightness_write,
4510 .exit = brightness_exit,
4513 /*************************************************************************
4514 * Volume subdriver
4517 static int volume_offset = 0x30;
4519 static int volume_read(char *p)
4521 int len = 0;
4522 u8 level;
4524 if (!acpi_ec_read(volume_offset, &level)) {
4525 len += sprintf(p + len, "level:\t\tunreadable\n");
4526 } else {
4527 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4528 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4529 len += sprintf(p + len, "commands:\tup, down, mute\n");
4530 len += sprintf(p + len, "commands:\tlevel <level>"
4531 " (<level> is 0-15)\n");
4534 return len;
4537 static int volume_write(char *buf)
4539 int cmos_cmd, inc, i;
4540 u8 level, mute;
4541 int new_level, new_mute;
4542 char *cmd;
4544 while ((cmd = next_cmd(&buf))) {
4545 if (!acpi_ec_read(volume_offset, &level))
4546 return -EIO;
4547 new_mute = mute = level & 0x40;
4548 new_level = level = level & 0xf;
4550 if (strlencmp(cmd, "up") == 0) {
4551 if (mute)
4552 new_mute = 0;
4553 else
4554 new_level = level == 15 ? 15 : level + 1;
4555 } else if (strlencmp(cmd, "down") == 0) {
4556 if (mute)
4557 new_mute = 0;
4558 else
4559 new_level = level == 0 ? 0 : level - 1;
4560 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4561 new_level >= 0 && new_level <= 15) {
4562 /* new_level set */
4563 } else if (strlencmp(cmd, "mute") == 0) {
4564 new_mute = 0x40;
4565 } else
4566 return -EINVAL;
4568 if (new_level != level) {
4569 /* mute doesn't change */
4571 cmos_cmd = (new_level > level) ?
4572 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4573 inc = new_level > level ? 1 : -1;
4575 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4576 !acpi_ec_write(volume_offset, level)))
4577 return -EIO;
4579 for (i = level; i != new_level; i += inc)
4580 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4581 !acpi_ec_write(volume_offset, i + inc))
4582 return -EIO;
4584 if (mute &&
4585 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4586 !acpi_ec_write(volume_offset, new_level + mute))) {
4587 return -EIO;
4591 if (new_mute != mute) {
4592 /* level doesn't change */
4594 cmos_cmd = (new_mute) ?
4595 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4597 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4598 !acpi_ec_write(volume_offset, level + new_mute))
4599 return -EIO;
4603 return 0;
4606 static struct ibm_struct volume_driver_data = {
4607 .name = "volume",
4608 .read = volume_read,
4609 .write = volume_write,
4612 /*************************************************************************
4613 * Fan subdriver
4617 * FAN ACCESS MODES
4619 * TPACPI_FAN_RD_ACPI_GFAN:
4620 * ACPI GFAN method: returns fan level
4622 * see TPACPI_FAN_WR_ACPI_SFAN
4623 * EC 0x2f (HFSP) not available if GFAN exists
4625 * TPACPI_FAN_WR_ACPI_SFAN:
4626 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4628 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4629 * it for writing.
4631 * TPACPI_FAN_WR_TPEC:
4632 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4633 * Supported on almost all ThinkPads
4635 * Fan speed changes of any sort (including those caused by the
4636 * disengaged mode) are usually done slowly by the firmware as the
4637 * maximum ammount of fan duty cycle change per second seems to be
4638 * limited.
4640 * Reading is not available if GFAN exists.
4641 * Writing is not available if SFAN exists.
4643 * Bits
4644 * 7 automatic mode engaged;
4645 * (default operation mode of the ThinkPad)
4646 * fan level is ignored in this mode.
4647 * 6 full speed mode (takes precedence over bit 7);
4648 * not available on all thinkpads. May disable
4649 * the tachometer while the fan controller ramps up
4650 * the speed (which can take up to a few *minutes*).
4651 * Speeds up fan to 100% duty-cycle, which is far above
4652 * the standard RPM levels. It is not impossible that
4653 * it could cause hardware damage.
4654 * 5-3 unused in some models. Extra bits for fan level
4655 * in others, but still useless as all values above
4656 * 7 map to the same speed as level 7 in these models.
4657 * 2-0 fan level (0..7 usually)
4658 * 0x00 = stop
4659 * 0x07 = max (set when temperatures critical)
4660 * Some ThinkPads may have other levels, see
4661 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4663 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4664 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4665 * does so, its initial value is meaningless (0x07).
4667 * For firmware bugs, refer to:
4668 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4670 * ----
4672 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4673 * Main fan tachometer reading (in RPM)
4675 * This register is present on all ThinkPads with a new-style EC, and
4676 * it is known not to be present on the A21m/e, and T22, as there is
4677 * something else in offset 0x84 according to the ACPI DSDT. Other
4678 * ThinkPads from this same time period (and earlier) probably lack the
4679 * tachometer as well.
4681 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4682 * was never fixed by IBM to report the EC firmware version string
4683 * probably support the tachometer (like the early X models), so
4684 * detecting it is quite hard. We need more data to know for sure.
4686 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4687 * might result.
4689 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4690 * mode.
4692 * For firmware bugs, refer to:
4693 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4695 * TPACPI_FAN_WR_ACPI_FANS:
4696 * ThinkPad X31, X40, X41. Not available in the X60.
4698 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4699 * high speed. ACPI DSDT seems to map these three speeds to levels
4700 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4701 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4703 * The speeds are stored on handles
4704 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4706 * There are three default speed sets, acessible as handles:
4707 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4709 * ACPI DSDT switches which set is in use depending on various
4710 * factors.
4712 * TPACPI_FAN_WR_TPEC is also available and should be used to
4713 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4714 * but the ACPI tables just mention level 7.
4717 enum { /* Fan control constants */
4718 fan_status_offset = 0x2f, /* EC register 0x2f */
4719 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4720 * 0x84 must be read before 0x85 */
4722 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4723 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4725 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4728 enum fan_status_access_mode {
4729 TPACPI_FAN_NONE = 0, /* No fan status or control */
4730 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4731 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4734 enum fan_control_access_mode {
4735 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4736 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4737 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4738 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4741 enum fan_control_commands {
4742 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4743 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4744 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4745 * and also watchdog cmd */
4748 static int fan_control_allowed;
4750 static enum fan_status_access_mode fan_status_access_mode;
4751 static enum fan_control_access_mode fan_control_access_mode;
4752 static enum fan_control_commands fan_control_commands;
4754 static u8 fan_control_initial_status;
4755 static u8 fan_control_desired_level;
4756 static int fan_watchdog_maxinterval;
4758 static struct mutex fan_mutex;
4760 static void fan_watchdog_fire(struct work_struct *ignored);
4761 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4763 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4764 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4765 "\\FSPD", /* 600e/x, 770e, 770x */
4766 ); /* all others */
4767 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4768 "JFNS", /* 770x-JL */
4769 ); /* all others */
4772 * Call with fan_mutex held
4774 static void fan_update_desired_level(u8 status)
4776 if ((status &
4777 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4778 if (status > 7)
4779 fan_control_desired_level = 7;
4780 else
4781 fan_control_desired_level = status;
4785 static int fan_get_status(u8 *status)
4787 u8 s;
4789 /* TODO:
4790 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4792 switch (fan_status_access_mode) {
4793 case TPACPI_FAN_RD_ACPI_GFAN:
4794 /* 570, 600e/x, 770e, 770x */
4796 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4797 return -EIO;
4799 if (likely(status))
4800 *status = s & 0x07;
4802 break;
4804 case TPACPI_FAN_RD_TPEC:
4805 /* all except 570, 600e/x, 770e, 770x */
4806 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4807 return -EIO;
4809 if (likely(status))
4810 *status = s;
4812 break;
4814 default:
4815 return -ENXIO;
4818 return 0;
4821 static int fan_get_status_safe(u8 *status)
4823 int rc;
4824 u8 s;
4826 if (mutex_lock_interruptible(&fan_mutex))
4827 return -ERESTARTSYS;
4828 rc = fan_get_status(&s);
4829 if (!rc)
4830 fan_update_desired_level(s);
4831 mutex_unlock(&fan_mutex);
4833 if (status)
4834 *status = s;
4836 return rc;
4839 static int fan_get_speed(unsigned int *speed)
4841 u8 hi, lo;
4843 switch (fan_status_access_mode) {
4844 case TPACPI_FAN_RD_TPEC:
4845 /* all except 570, 600e/x, 770e, 770x */
4846 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4847 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4848 return -EIO;
4850 if (likely(speed))
4851 *speed = (hi << 8) | lo;
4853 break;
4855 default:
4856 return -ENXIO;
4859 return 0;
4862 static int fan_set_level(int level)
4864 if (!fan_control_allowed)
4865 return -EPERM;
4867 switch (fan_control_access_mode) {
4868 case TPACPI_FAN_WR_ACPI_SFAN:
4869 if (level >= 0 && level <= 7) {
4870 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4871 return -EIO;
4872 } else
4873 return -EINVAL;
4874 break;
4876 case TPACPI_FAN_WR_ACPI_FANS:
4877 case TPACPI_FAN_WR_TPEC:
4878 if ((level != TP_EC_FAN_AUTO) &&
4879 (level != TP_EC_FAN_FULLSPEED) &&
4880 ((level < 0) || (level > 7)))
4881 return -EINVAL;
4883 /* safety net should the EC not support AUTO
4884 * or FULLSPEED mode bits and just ignore them */
4885 if (level & TP_EC_FAN_FULLSPEED)
4886 level |= 7; /* safety min speed 7 */
4887 else if (level & TP_EC_FAN_AUTO)
4888 level |= 4; /* safety min speed 4 */
4890 if (!acpi_ec_write(fan_status_offset, level))
4891 return -EIO;
4892 else
4893 tp_features.fan_ctrl_status_undef = 0;
4894 break;
4896 default:
4897 return -ENXIO;
4899 return 0;
4902 static int fan_set_level_safe(int level)
4904 int rc;
4906 if (!fan_control_allowed)
4907 return -EPERM;
4909 if (mutex_lock_interruptible(&fan_mutex))
4910 return -ERESTARTSYS;
4912 if (level == TPACPI_FAN_LAST_LEVEL)
4913 level = fan_control_desired_level;
4915 rc = fan_set_level(level);
4916 if (!rc)
4917 fan_update_desired_level(level);
4919 mutex_unlock(&fan_mutex);
4920 return rc;
4923 static int fan_set_enable(void)
4925 u8 s;
4926 int rc;
4928 if (!fan_control_allowed)
4929 return -EPERM;
4931 if (mutex_lock_interruptible(&fan_mutex))
4932 return -ERESTARTSYS;
4934 switch (fan_control_access_mode) {
4935 case TPACPI_FAN_WR_ACPI_FANS:
4936 case TPACPI_FAN_WR_TPEC:
4937 rc = fan_get_status(&s);
4938 if (rc < 0)
4939 break;
4941 /* Don't go out of emergency fan mode */
4942 if (s != 7) {
4943 s &= 0x07;
4944 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4947 if (!acpi_ec_write(fan_status_offset, s))
4948 rc = -EIO;
4949 else {
4950 tp_features.fan_ctrl_status_undef = 0;
4951 rc = 0;
4953 break;
4955 case TPACPI_FAN_WR_ACPI_SFAN:
4956 rc = fan_get_status(&s);
4957 if (rc < 0)
4958 break;
4960 s &= 0x07;
4962 /* Set fan to at least level 4 */
4963 s |= 4;
4965 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4966 rc = -EIO;
4967 else
4968 rc = 0;
4969 break;
4971 default:
4972 rc = -ENXIO;
4975 mutex_unlock(&fan_mutex);
4976 return rc;
4979 static int fan_set_disable(void)
4981 int rc;
4983 if (!fan_control_allowed)
4984 return -EPERM;
4986 if (mutex_lock_interruptible(&fan_mutex))
4987 return -ERESTARTSYS;
4989 rc = 0;
4990 switch (fan_control_access_mode) {
4991 case TPACPI_FAN_WR_ACPI_FANS:
4992 case TPACPI_FAN_WR_TPEC:
4993 if (!acpi_ec_write(fan_status_offset, 0x00))
4994 rc = -EIO;
4995 else {
4996 fan_control_desired_level = 0;
4997 tp_features.fan_ctrl_status_undef = 0;
4999 break;
5001 case TPACPI_FAN_WR_ACPI_SFAN:
5002 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5003 rc = -EIO;
5004 else
5005 fan_control_desired_level = 0;
5006 break;
5008 default:
5009 rc = -ENXIO;
5013 mutex_unlock(&fan_mutex);
5014 return rc;
5017 static int fan_set_speed(int speed)
5019 int rc;
5021 if (!fan_control_allowed)
5022 return -EPERM;
5024 if (mutex_lock_interruptible(&fan_mutex))
5025 return -ERESTARTSYS;
5027 rc = 0;
5028 switch (fan_control_access_mode) {
5029 case TPACPI_FAN_WR_ACPI_FANS:
5030 if (speed >= 0 && speed <= 65535) {
5031 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5032 speed, speed, speed))
5033 rc = -EIO;
5034 } else
5035 rc = -EINVAL;
5036 break;
5038 default:
5039 rc = -ENXIO;
5042 mutex_unlock(&fan_mutex);
5043 return rc;
5046 static void fan_watchdog_reset(void)
5048 static int fan_watchdog_active;
5050 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5051 return;
5053 if (fan_watchdog_active)
5054 cancel_delayed_work(&fan_watchdog_task);
5056 if (fan_watchdog_maxinterval > 0 &&
5057 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5058 fan_watchdog_active = 1;
5059 if (!schedule_delayed_work(&fan_watchdog_task,
5060 msecs_to_jiffies(fan_watchdog_maxinterval
5061 * 1000))) {
5062 printk(TPACPI_ERR
5063 "failed to schedule the fan watchdog, "
5064 "watchdog will not trigger\n");
5066 } else
5067 fan_watchdog_active = 0;
5070 static void fan_watchdog_fire(struct work_struct *ignored)
5072 int rc;
5074 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5075 return;
5077 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5078 rc = fan_set_enable();
5079 if (rc < 0) {
5080 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5081 "will try again later...\n", -rc);
5082 /* reschedule for later */
5083 fan_watchdog_reset();
5088 * SYSFS fan layout: hwmon compatible (device)
5090 * pwm*_enable:
5091 * 0: "disengaged" mode
5092 * 1: manual mode
5093 * 2: native EC "auto" mode (recommended, hardware default)
5095 * pwm*: set speed in manual mode, ignored otherwise.
5096 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5097 * interpolation.
5099 * fan*_input: tachometer reading, RPM
5102 * SYSFS fan layout: extensions
5104 * fan_watchdog (driver):
5105 * fan watchdog interval in seconds, 0 disables (default), max 120
5108 /* sysfs fan pwm1_enable ----------------------------------------------- */
5109 static ssize_t fan_pwm1_enable_show(struct device *dev,
5110 struct device_attribute *attr,
5111 char *buf)
5113 int res, mode;
5114 u8 status;
5116 res = fan_get_status_safe(&status);
5117 if (res)
5118 return res;
5120 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5121 if (status != fan_control_initial_status) {
5122 tp_features.fan_ctrl_status_undef = 0;
5123 } else {
5124 /* Return most likely status. In fact, it
5125 * might be the only possible status */
5126 status = TP_EC_FAN_AUTO;
5130 if (status & TP_EC_FAN_FULLSPEED) {
5131 mode = 0;
5132 } else if (status & TP_EC_FAN_AUTO) {
5133 mode = 2;
5134 } else
5135 mode = 1;
5137 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5140 static ssize_t fan_pwm1_enable_store(struct device *dev,
5141 struct device_attribute *attr,
5142 const char *buf, size_t count)
5144 unsigned long t;
5145 int res, level;
5147 if (parse_strtoul(buf, 2, &t))
5148 return -EINVAL;
5150 switch (t) {
5151 case 0:
5152 level = TP_EC_FAN_FULLSPEED;
5153 break;
5154 case 1:
5155 level = TPACPI_FAN_LAST_LEVEL;
5156 break;
5157 case 2:
5158 level = TP_EC_FAN_AUTO;
5159 break;
5160 case 3:
5161 /* reserved for software-controlled auto mode */
5162 return -ENOSYS;
5163 default:
5164 return -EINVAL;
5167 res = fan_set_level_safe(level);
5168 if (res == -ENXIO)
5169 return -EINVAL;
5170 else if (res < 0)
5171 return res;
5173 fan_watchdog_reset();
5175 return count;
5178 static struct device_attribute dev_attr_fan_pwm1_enable =
5179 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5180 fan_pwm1_enable_show, fan_pwm1_enable_store);
5182 /* sysfs fan pwm1 ------------------------------------------------------ */
5183 static ssize_t fan_pwm1_show(struct device *dev,
5184 struct device_attribute *attr,
5185 char *buf)
5187 int res;
5188 u8 status;
5190 res = fan_get_status_safe(&status);
5191 if (res)
5192 return res;
5194 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5195 if (status != fan_control_initial_status) {
5196 tp_features.fan_ctrl_status_undef = 0;
5197 } else {
5198 status = TP_EC_FAN_AUTO;
5202 if ((status &
5203 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5204 status = fan_control_desired_level;
5206 if (status > 7)
5207 status = 7;
5209 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5212 static ssize_t fan_pwm1_store(struct device *dev,
5213 struct device_attribute *attr,
5214 const char *buf, size_t count)
5216 unsigned long s;
5217 int rc;
5218 u8 status, newlevel;
5220 if (parse_strtoul(buf, 255, &s))
5221 return -EINVAL;
5223 /* scale down from 0-255 to 0-7 */
5224 newlevel = (s >> 5) & 0x07;
5226 if (mutex_lock_interruptible(&fan_mutex))
5227 return -ERESTARTSYS;
5229 rc = fan_get_status(&status);
5230 if (!rc && (status &
5231 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5232 rc = fan_set_level(newlevel);
5233 if (rc == -ENXIO)
5234 rc = -EINVAL;
5235 else if (!rc) {
5236 fan_update_desired_level(newlevel);
5237 fan_watchdog_reset();
5241 mutex_unlock(&fan_mutex);
5242 return (rc)? rc : count;
5245 static struct device_attribute dev_attr_fan_pwm1 =
5246 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5247 fan_pwm1_show, fan_pwm1_store);
5249 /* sysfs fan fan1_input ------------------------------------------------ */
5250 static ssize_t fan_fan1_input_show(struct device *dev,
5251 struct device_attribute *attr,
5252 char *buf)
5254 int res;
5255 unsigned int speed;
5257 res = fan_get_speed(&speed);
5258 if (res < 0)
5259 return res;
5261 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5264 static struct device_attribute dev_attr_fan_fan1_input =
5265 __ATTR(fan1_input, S_IRUGO,
5266 fan_fan1_input_show, NULL);
5268 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5269 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5270 char *buf)
5272 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5275 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5276 const char *buf, size_t count)
5278 unsigned long t;
5280 if (parse_strtoul(buf, 120, &t))
5281 return -EINVAL;
5283 if (!fan_control_allowed)
5284 return -EPERM;
5286 fan_watchdog_maxinterval = t;
5287 fan_watchdog_reset();
5289 return count;
5292 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5293 fan_fan_watchdog_show, fan_fan_watchdog_store);
5295 /* --------------------------------------------------------------------- */
5296 static struct attribute *fan_attributes[] = {
5297 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5298 &dev_attr_fan_fan1_input.attr,
5299 NULL
5302 static const struct attribute_group fan_attr_group = {
5303 .attrs = fan_attributes,
5306 static int __init fan_init(struct ibm_init_struct *iibm)
5308 int rc;
5310 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5312 mutex_init(&fan_mutex);
5313 fan_status_access_mode = TPACPI_FAN_NONE;
5314 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5315 fan_control_commands = 0;
5316 fan_watchdog_maxinterval = 0;
5317 tp_features.fan_ctrl_status_undef = 0;
5318 fan_control_desired_level = 7;
5320 TPACPI_ACPIHANDLE_INIT(fans);
5321 TPACPI_ACPIHANDLE_INIT(gfan);
5322 TPACPI_ACPIHANDLE_INIT(sfan);
5324 if (gfan_handle) {
5325 /* 570, 600e/x, 770e, 770x */
5326 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5327 } else {
5328 /* all other ThinkPads: note that even old-style
5329 * ThinkPad ECs supports the fan control register */
5330 if (likely(acpi_ec_read(fan_status_offset,
5331 &fan_control_initial_status))) {
5332 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5334 /* In some ThinkPads, neither the EC nor the ACPI
5335 * DSDT initialize the fan status, and it ends up
5336 * being set to 0x07 when it *could* be either
5337 * 0x07 or 0x80.
5339 * Enable for TP-1Y (T43), TP-78 (R51e),
5340 * TP-76 (R52), TP-70 (T43, R52), which are known
5341 * to be buggy. */
5342 if (fan_control_initial_status == 0x07) {
5343 switch (thinkpad_id.ec_model) {
5344 case 0x5931: /* TP-1Y */
5345 case 0x3837: /* TP-78 */
5346 case 0x3637: /* TP-76 */
5347 case 0x3037: /* TP-70 */
5348 printk(TPACPI_NOTICE
5349 "fan_init: initial fan status "
5350 "is unknown, assuming it is "
5351 "in auto mode\n");
5352 tp_features.fan_ctrl_status_undef = 1;
5356 } else {
5357 printk(TPACPI_ERR
5358 "ThinkPad ACPI EC access misbehaving, "
5359 "fan status and control unavailable\n");
5360 return 1;
5364 if (sfan_handle) {
5365 /* 570, 770x-JL */
5366 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5367 fan_control_commands |=
5368 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5369 } else {
5370 if (!gfan_handle) {
5371 /* gfan without sfan means no fan control */
5372 /* all other models implement TP EC 0x2f control */
5374 if (fans_handle) {
5375 /* X31, X40, X41 */
5376 fan_control_access_mode =
5377 TPACPI_FAN_WR_ACPI_FANS;
5378 fan_control_commands |=
5379 TPACPI_FAN_CMD_SPEED |
5380 TPACPI_FAN_CMD_LEVEL |
5381 TPACPI_FAN_CMD_ENABLE;
5382 } else {
5383 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5384 fan_control_commands |=
5385 TPACPI_FAN_CMD_LEVEL |
5386 TPACPI_FAN_CMD_ENABLE;
5391 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5392 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5393 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5394 fan_status_access_mode, fan_control_access_mode);
5396 /* fan control master switch */
5397 if (!fan_control_allowed) {
5398 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5399 fan_control_commands = 0;
5400 dbg_printk(TPACPI_DBG_INIT,
5401 "fan control features disabled by parameter\n");
5404 /* update fan_control_desired_level */
5405 if (fan_status_access_mode != TPACPI_FAN_NONE)
5406 fan_get_status_safe(NULL);
5408 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5409 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5410 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5411 &fan_attr_group);
5412 if (!(rc < 0))
5413 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5414 &driver_attr_fan_watchdog);
5415 if (rc < 0)
5416 return rc;
5417 return 0;
5418 } else
5419 return 1;
5422 static void fan_exit(void)
5424 vdbg_printk(TPACPI_DBG_EXIT,
5425 "cancelling any pending fan watchdog tasks\n");
5427 /* FIXME: can we really do this unconditionally? */
5428 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5429 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5430 &driver_attr_fan_watchdog);
5432 cancel_delayed_work(&fan_watchdog_task);
5433 flush_scheduled_work();
5436 static int fan_read(char *p)
5438 int len = 0;
5439 int rc;
5440 u8 status;
5441 unsigned int speed = 0;
5443 switch (fan_status_access_mode) {
5444 case TPACPI_FAN_RD_ACPI_GFAN:
5445 /* 570, 600e/x, 770e, 770x */
5446 rc = fan_get_status_safe(&status);
5447 if (rc < 0)
5448 return rc;
5450 len += sprintf(p + len, "status:\t\t%s\n"
5451 "level:\t\t%d\n",
5452 (status != 0) ? "enabled" : "disabled", status);
5453 break;
5455 case TPACPI_FAN_RD_TPEC:
5456 /* all except 570, 600e/x, 770e, 770x */
5457 rc = fan_get_status_safe(&status);
5458 if (rc < 0)
5459 return rc;
5461 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5462 if (status != fan_control_initial_status)
5463 tp_features.fan_ctrl_status_undef = 0;
5464 else
5465 /* Return most likely status. In fact, it
5466 * might be the only possible status */
5467 status = TP_EC_FAN_AUTO;
5470 len += sprintf(p + len, "status:\t\t%s\n",
5471 (status != 0) ? "enabled" : "disabled");
5473 rc = fan_get_speed(&speed);
5474 if (rc < 0)
5475 return rc;
5477 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5479 if (status & TP_EC_FAN_FULLSPEED)
5480 /* Disengaged mode takes precedence */
5481 len += sprintf(p + len, "level:\t\tdisengaged\n");
5482 else if (status & TP_EC_FAN_AUTO)
5483 len += sprintf(p + len, "level:\t\tauto\n");
5484 else
5485 len += sprintf(p + len, "level:\t\t%d\n", status);
5486 break;
5488 case TPACPI_FAN_NONE:
5489 default:
5490 len += sprintf(p + len, "status:\t\tnot supported\n");
5493 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5494 len += sprintf(p + len, "commands:\tlevel <level>");
5496 switch (fan_control_access_mode) {
5497 case TPACPI_FAN_WR_ACPI_SFAN:
5498 len += sprintf(p + len, " (<level> is 0-7)\n");
5499 break;
5501 default:
5502 len += sprintf(p + len, " (<level> is 0-7, "
5503 "auto, disengaged, full-speed)\n");
5504 break;
5508 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5509 len += sprintf(p + len, "commands:\tenable, disable\n"
5510 "commands:\twatchdog <timeout> (<timeout> "
5511 "is 0 (off), 1-120 (seconds))\n");
5513 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5514 len += sprintf(p + len, "commands:\tspeed <speed>"
5515 " (<speed> is 0-65535)\n");
5517 return len;
5520 static int fan_write_cmd_level(const char *cmd, int *rc)
5522 int level;
5524 if (strlencmp(cmd, "level auto") == 0)
5525 level = TP_EC_FAN_AUTO;
5526 else if ((strlencmp(cmd, "level disengaged") == 0) |
5527 (strlencmp(cmd, "level full-speed") == 0))
5528 level = TP_EC_FAN_FULLSPEED;
5529 else if (sscanf(cmd, "level %d", &level) != 1)
5530 return 0;
5532 *rc = fan_set_level_safe(level);
5533 if (*rc == -ENXIO)
5534 printk(TPACPI_ERR "level command accepted for unsupported "
5535 "access mode %d", fan_control_access_mode);
5537 return 1;
5540 static int fan_write_cmd_enable(const char *cmd, int *rc)
5542 if (strlencmp(cmd, "enable") != 0)
5543 return 0;
5545 *rc = fan_set_enable();
5546 if (*rc == -ENXIO)
5547 printk(TPACPI_ERR "enable command accepted for unsupported "
5548 "access mode %d", fan_control_access_mode);
5550 return 1;
5553 static int fan_write_cmd_disable(const char *cmd, int *rc)
5555 if (strlencmp(cmd, "disable") != 0)
5556 return 0;
5558 *rc = fan_set_disable();
5559 if (*rc == -ENXIO)
5560 printk(TPACPI_ERR "disable command accepted for unsupported "
5561 "access mode %d", fan_control_access_mode);
5563 return 1;
5566 static int fan_write_cmd_speed(const char *cmd, int *rc)
5568 int speed;
5570 /* TODO:
5571 * Support speed <low> <medium> <high> ? */
5573 if (sscanf(cmd, "speed %d", &speed) != 1)
5574 return 0;
5576 *rc = fan_set_speed(speed);
5577 if (*rc == -ENXIO)
5578 printk(TPACPI_ERR "speed command accepted for unsupported "
5579 "access mode %d", fan_control_access_mode);
5581 return 1;
5584 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5586 int interval;
5588 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5589 return 0;
5591 if (interval < 0 || interval > 120)
5592 *rc = -EINVAL;
5593 else
5594 fan_watchdog_maxinterval = interval;
5596 return 1;
5599 static int fan_write(char *buf)
5601 char *cmd;
5602 int rc = 0;
5604 while (!rc && (cmd = next_cmd(&buf))) {
5605 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5606 fan_write_cmd_level(cmd, &rc)) &&
5607 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5608 (fan_write_cmd_enable(cmd, &rc) ||
5609 fan_write_cmd_disable(cmd, &rc) ||
5610 fan_write_cmd_watchdog(cmd, &rc))) &&
5611 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5612 fan_write_cmd_speed(cmd, &rc))
5614 rc = -EINVAL;
5615 else if (!rc)
5616 fan_watchdog_reset();
5619 return rc;
5622 static struct ibm_struct fan_driver_data = {
5623 .name = "fan",
5624 .read = fan_read,
5625 .write = fan_write,
5626 .exit = fan_exit,
5629 /****************************************************************************
5630 ****************************************************************************
5632 * Infrastructure
5634 ****************************************************************************
5635 ****************************************************************************/
5637 /* sysfs name ---------------------------------------------------------- */
5638 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5639 struct device_attribute *attr,
5640 char *buf)
5642 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5645 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5646 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5648 /* --------------------------------------------------------------------- */
5650 /* /proc support */
5651 static struct proc_dir_entry *proc_dir;
5654 * Module and infrastructure proble, init and exit handling
5657 static int force_load;
5659 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5660 static const char * __init str_supported(int is_supported)
5662 static char text_unsupported[] __initdata = "not supported";
5664 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5666 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5668 static void ibm_exit(struct ibm_struct *ibm)
5670 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5672 list_del_init(&ibm->all_drivers);
5674 if (ibm->flags.acpi_notify_installed) {
5675 dbg_printk(TPACPI_DBG_EXIT,
5676 "%s: acpi_remove_notify_handler\n", ibm->name);
5677 BUG_ON(!ibm->acpi);
5678 acpi_remove_notify_handler(*ibm->acpi->handle,
5679 ibm->acpi->type,
5680 dispatch_acpi_notify);
5681 ibm->flags.acpi_notify_installed = 0;
5682 ibm->flags.acpi_notify_installed = 0;
5685 if (ibm->flags.proc_created) {
5686 dbg_printk(TPACPI_DBG_EXIT,
5687 "%s: remove_proc_entry\n", ibm->name);
5688 remove_proc_entry(ibm->name, proc_dir);
5689 ibm->flags.proc_created = 0;
5692 if (ibm->flags.acpi_driver_registered) {
5693 dbg_printk(TPACPI_DBG_EXIT,
5694 "%s: acpi_bus_unregister_driver\n", ibm->name);
5695 BUG_ON(!ibm->acpi);
5696 acpi_bus_unregister_driver(ibm->acpi->driver);
5697 kfree(ibm->acpi->driver);
5698 ibm->acpi->driver = NULL;
5699 ibm->flags.acpi_driver_registered = 0;
5702 if (ibm->flags.init_called && ibm->exit) {
5703 ibm->exit();
5704 ibm->flags.init_called = 0;
5707 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5710 static int __init ibm_init(struct ibm_init_struct *iibm)
5712 int ret;
5713 struct ibm_struct *ibm = iibm->data;
5714 struct proc_dir_entry *entry;
5716 BUG_ON(ibm == NULL);
5718 INIT_LIST_HEAD(&ibm->all_drivers);
5720 if (ibm->flags.experimental && !experimental)
5721 return 0;
5723 dbg_printk(TPACPI_DBG_INIT,
5724 "probing for %s\n", ibm->name);
5726 if (iibm->init) {
5727 ret = iibm->init(iibm);
5728 if (ret > 0)
5729 return 0; /* probe failed */
5730 if (ret)
5731 return ret;
5733 ibm->flags.init_called = 1;
5736 if (ibm->acpi) {
5737 if (ibm->acpi->hid) {
5738 ret = register_tpacpi_subdriver(ibm);
5739 if (ret)
5740 goto err_out;
5743 if (ibm->acpi->notify) {
5744 ret = setup_acpi_notify(ibm);
5745 if (ret == -ENODEV) {
5746 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5747 ibm->name);
5748 ret = 0;
5749 goto err_out;
5751 if (ret < 0)
5752 goto err_out;
5756 dbg_printk(TPACPI_DBG_INIT,
5757 "%s installed\n", ibm->name);
5759 if (ibm->read) {
5760 entry = create_proc_entry(ibm->name,
5761 S_IFREG | S_IRUGO | S_IWUSR,
5762 proc_dir);
5763 if (!entry) {
5764 printk(TPACPI_ERR "unable to create proc entry %s\n",
5765 ibm->name);
5766 ret = -ENODEV;
5767 goto err_out;
5769 entry->owner = THIS_MODULE;
5770 entry->data = ibm;
5771 entry->read_proc = &dispatch_procfs_read;
5772 if (ibm->write)
5773 entry->write_proc = &dispatch_procfs_write;
5774 ibm->flags.proc_created = 1;
5777 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5779 return 0;
5781 err_out:
5782 dbg_printk(TPACPI_DBG_INIT,
5783 "%s: at error exit path with result %d\n",
5784 ibm->name, ret);
5786 ibm_exit(ibm);
5787 return (ret < 0)? ret : 0;
5790 /* Probing */
5792 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5794 const struct dmi_device *dev = NULL;
5795 char ec_fw_string[18];
5797 if (!tp)
5798 return;
5800 memset(tp, 0, sizeof(*tp));
5802 if (dmi_name_in_vendors("IBM"))
5803 tp->vendor = PCI_VENDOR_ID_IBM;
5804 else if (dmi_name_in_vendors("LENOVO"))
5805 tp->vendor = PCI_VENDOR_ID_LENOVO;
5806 else
5807 return;
5809 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5810 GFP_KERNEL);
5811 if (!tp->bios_version_str)
5812 return;
5813 tp->bios_model = tp->bios_version_str[0]
5814 | (tp->bios_version_str[1] << 8);
5817 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5818 * X32 or newer, all Z series; Some models must have an
5819 * up-to-date BIOS or they will not be detected.
5821 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5823 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5824 if (sscanf(dev->name,
5825 "IBM ThinkPad Embedded Controller -[%17c",
5826 ec_fw_string) == 1) {
5827 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5828 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5830 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5831 tp->ec_model = ec_fw_string[0]
5832 | (ec_fw_string[1] << 8);
5833 break;
5837 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5838 GFP_KERNEL);
5839 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5840 kfree(tp->model_str);
5841 tp->model_str = NULL;
5845 static int __init probe_for_thinkpad(void)
5847 int is_thinkpad;
5849 if (acpi_disabled)
5850 return -ENODEV;
5853 * Non-ancient models have better DMI tagging, but very old models
5854 * don't.
5856 is_thinkpad = (thinkpad_id.model_str != NULL);
5858 /* ec is required because many other handles are relative to it */
5859 TPACPI_ACPIHANDLE_INIT(ec);
5860 if (!ec_handle) {
5861 if (is_thinkpad)
5862 printk(TPACPI_ERR
5863 "Not yet supported ThinkPad detected!\n");
5864 return -ENODEV;
5868 * Risks a regression on very old machines, but reduces potential
5869 * false positives a damn great deal
5871 if (!is_thinkpad)
5872 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5874 if (!is_thinkpad && !force_load)
5875 return -ENODEV;
5877 return 0;
5881 /* Module init, exit, parameters */
5883 static struct ibm_init_struct ibms_init[] __initdata = {
5885 .init = thinkpad_acpi_driver_init,
5886 .data = &thinkpad_acpi_driver_data,
5889 .init = hotkey_init,
5890 .data = &hotkey_driver_data,
5893 .init = bluetooth_init,
5894 .data = &bluetooth_driver_data,
5897 .init = wan_init,
5898 .data = &wan_driver_data,
5900 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5902 .init = video_init,
5903 .data = &video_driver_data,
5905 #endif
5907 .init = light_init,
5908 .data = &light_driver_data,
5910 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5912 .init = dock_init,
5913 .data = &dock_driver_data[0],
5916 .init = dock_init2,
5917 .data = &dock_driver_data[1],
5919 #endif
5920 #ifdef CONFIG_THINKPAD_ACPI_BAY
5922 .init = bay_init,
5923 .data = &bay_driver_data,
5925 #endif
5927 .init = cmos_init,
5928 .data = &cmos_driver_data,
5931 .init = led_init,
5932 .data = &led_driver_data,
5935 .init = beep_init,
5936 .data = &beep_driver_data,
5939 .init = thermal_init,
5940 .data = &thermal_driver_data,
5943 .data = &ecdump_driver_data,
5946 .init = brightness_init,
5947 .data = &brightness_driver_data,
5950 .data = &volume_driver_data,
5953 .init = fan_init,
5954 .data = &fan_driver_data,
5958 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5960 unsigned int i;
5961 struct ibm_struct *ibm;
5963 if (!kp || !kp->name || !val)
5964 return -EINVAL;
5966 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5967 ibm = ibms_init[i].data;
5968 WARN_ON(ibm == NULL);
5970 if (!ibm || !ibm->name)
5971 continue;
5973 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5974 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5975 return -ENOSPC;
5976 strcpy(ibms_init[i].param, val);
5977 strcat(ibms_init[i].param, ",");
5978 return 0;
5982 return -EINVAL;
5985 module_param(experimental, int, 0);
5986 MODULE_PARM_DESC(experimental,
5987 "Enables experimental features when non-zero");
5989 module_param_named(debug, dbg_level, uint, 0);
5990 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5992 module_param(force_load, bool, 0);
5993 MODULE_PARM_DESC(force_load,
5994 "Attempts to load the driver even on a "
5995 "mis-identified ThinkPad when true");
5997 module_param_named(fan_control, fan_control_allowed, bool, 0);
5998 MODULE_PARM_DESC(fan_control,
5999 "Enables setting fan parameters features when true");
6001 module_param_named(brightness_mode, brightness_mode, int, 0);
6002 MODULE_PARM_DESC(brightness_mode,
6003 "Selects brightness control strategy: "
6004 "0=auto, 1=EC, 2=CMOS, 3=both");
6006 module_param(brightness_enable, uint, 0);
6007 MODULE_PARM_DESC(brightness_enable,
6008 "Enables backlight control when 1, disables when 0");
6010 module_param(hotkey_report_mode, uint, 0);
6011 MODULE_PARM_DESC(hotkey_report_mode,
6012 "used for backwards compatibility with userspace, "
6013 "see documentation");
6015 #define TPACPI_PARAM(feature) \
6016 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6017 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
6018 "at module load, see documentation")
6020 TPACPI_PARAM(hotkey);
6021 TPACPI_PARAM(bluetooth);
6022 TPACPI_PARAM(video);
6023 TPACPI_PARAM(light);
6024 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6025 TPACPI_PARAM(dock);
6026 #endif
6027 #ifdef CONFIG_THINKPAD_ACPI_BAY
6028 TPACPI_PARAM(bay);
6029 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6030 TPACPI_PARAM(cmos);
6031 TPACPI_PARAM(led);
6032 TPACPI_PARAM(beep);
6033 TPACPI_PARAM(ecdump);
6034 TPACPI_PARAM(brightness);
6035 TPACPI_PARAM(volume);
6036 TPACPI_PARAM(fan);
6038 static void thinkpad_acpi_module_exit(void)
6040 struct ibm_struct *ibm, *itmp;
6042 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6044 list_for_each_entry_safe_reverse(ibm, itmp,
6045 &tpacpi_all_drivers,
6046 all_drivers) {
6047 ibm_exit(ibm);
6050 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6052 if (tpacpi_inputdev) {
6053 if (tp_features.input_device_registered)
6054 input_unregister_device(tpacpi_inputdev);
6055 else
6056 input_free_device(tpacpi_inputdev);
6059 if (tpacpi_hwmon)
6060 hwmon_device_unregister(tpacpi_hwmon);
6062 if (tp_features.sensors_pdev_attrs_registered)
6063 device_remove_file(&tpacpi_sensors_pdev->dev,
6064 &dev_attr_thinkpad_acpi_pdev_name);
6065 if (tpacpi_sensors_pdev)
6066 platform_device_unregister(tpacpi_sensors_pdev);
6067 if (tpacpi_pdev)
6068 platform_device_unregister(tpacpi_pdev);
6070 if (tp_features.sensors_pdrv_attrs_registered)
6071 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6072 if (tp_features.platform_drv_attrs_registered)
6073 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6075 if (tp_features.sensors_pdrv_registered)
6076 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6078 if (tp_features.platform_drv_registered)
6079 platform_driver_unregister(&tpacpi_pdriver);
6081 if (proc_dir)
6082 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6084 kfree(thinkpad_id.bios_version_str);
6085 kfree(thinkpad_id.ec_version_str);
6086 kfree(thinkpad_id.model_str);
6090 static int __init thinkpad_acpi_module_init(void)
6092 int ret, i;
6094 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6096 /* Parameter checking */
6097 if (hotkey_report_mode > 2)
6098 return -EINVAL;
6100 /* Driver-level probe */
6102 get_thinkpad_model_data(&thinkpad_id);
6103 ret = probe_for_thinkpad();
6104 if (ret) {
6105 thinkpad_acpi_module_exit();
6106 return ret;
6109 /* Driver initialization */
6111 TPACPI_ACPIHANDLE_INIT(ecrd);
6112 TPACPI_ACPIHANDLE_INIT(ecwr);
6114 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6115 if (!proc_dir) {
6116 printk(TPACPI_ERR
6117 "unable to create proc dir " TPACPI_PROC_DIR);
6118 thinkpad_acpi_module_exit();
6119 return -ENODEV;
6121 proc_dir->owner = THIS_MODULE;
6123 ret = platform_driver_register(&tpacpi_pdriver);
6124 if (ret) {
6125 printk(TPACPI_ERR
6126 "unable to register main platform driver\n");
6127 thinkpad_acpi_module_exit();
6128 return ret;
6130 tp_features.platform_drv_registered = 1;
6132 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6133 if (ret) {
6134 printk(TPACPI_ERR
6135 "unable to register hwmon platform driver\n");
6136 thinkpad_acpi_module_exit();
6137 return ret;
6139 tp_features.sensors_pdrv_registered = 1;
6141 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6142 if (!ret) {
6143 tp_features.platform_drv_attrs_registered = 1;
6144 ret = tpacpi_create_driver_attributes(
6145 &tpacpi_hwmon_pdriver.driver);
6147 if (ret) {
6148 printk(TPACPI_ERR
6149 "unable to create sysfs driver attributes\n");
6150 thinkpad_acpi_module_exit();
6151 return ret;
6153 tp_features.sensors_pdrv_attrs_registered = 1;
6156 /* Device initialization */
6157 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6158 NULL, 0);
6159 if (IS_ERR(tpacpi_pdev)) {
6160 ret = PTR_ERR(tpacpi_pdev);
6161 tpacpi_pdev = NULL;
6162 printk(TPACPI_ERR "unable to register platform device\n");
6163 thinkpad_acpi_module_exit();
6164 return ret;
6166 tpacpi_sensors_pdev = platform_device_register_simple(
6167 TPACPI_HWMON_DRVR_NAME,
6168 -1, NULL, 0);
6169 if (IS_ERR(tpacpi_sensors_pdev)) {
6170 ret = PTR_ERR(tpacpi_sensors_pdev);
6171 tpacpi_sensors_pdev = NULL;
6172 printk(TPACPI_ERR
6173 "unable to register hwmon platform device\n");
6174 thinkpad_acpi_module_exit();
6175 return ret;
6177 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6178 &dev_attr_thinkpad_acpi_pdev_name);
6179 if (ret) {
6180 printk(TPACPI_ERR
6181 "unable to create sysfs hwmon device attributes\n");
6182 thinkpad_acpi_module_exit();
6183 return ret;
6185 tp_features.sensors_pdev_attrs_registered = 1;
6186 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6187 if (IS_ERR(tpacpi_hwmon)) {
6188 ret = PTR_ERR(tpacpi_hwmon);
6189 tpacpi_hwmon = NULL;
6190 printk(TPACPI_ERR "unable to register hwmon device\n");
6191 thinkpad_acpi_module_exit();
6192 return ret;
6194 mutex_init(&tpacpi_inputdev_send_mutex);
6195 tpacpi_inputdev = input_allocate_device();
6196 if (!tpacpi_inputdev) {
6197 printk(TPACPI_ERR "unable to allocate input device\n");
6198 thinkpad_acpi_module_exit();
6199 return -ENOMEM;
6200 } else {
6201 /* Prepare input device, but don't register */
6202 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6203 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6204 tpacpi_inputdev->id.bustype = BUS_HOST;
6205 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6206 thinkpad_id.vendor :
6207 PCI_VENDOR_ID_IBM;
6208 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6209 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6211 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6212 ret = ibm_init(&ibms_init[i]);
6213 if (ret >= 0 && *ibms_init[i].param)
6214 ret = ibms_init[i].data->write(ibms_init[i].param);
6215 if (ret < 0) {
6216 thinkpad_acpi_module_exit();
6217 return ret;
6220 ret = input_register_device(tpacpi_inputdev);
6221 if (ret < 0) {
6222 printk(TPACPI_ERR "unable to register input device\n");
6223 thinkpad_acpi_module_exit();
6224 return ret;
6225 } else {
6226 tp_features.input_device_registered = 1;
6229 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6230 return 0;
6233 /* Please remove this in year 2009 */
6234 MODULE_ALIAS("ibm_acpi");
6237 * DMI matching for module autoloading
6239 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6240 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6242 * Only models listed in thinkwiki will be supported, so add yours
6243 * if it is not there yet.
6245 #define IBM_BIOS_MODULE_ALIAS(__type) \
6246 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6248 /* Non-ancient thinkpads */
6249 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6250 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6252 /* Ancient thinkpad BIOSes have to be identified by
6253 * BIOS type or model number, and there are far less
6254 * BIOS types than model numbers... */
6255 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6256 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6257 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6259 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6260 MODULE_DESCRIPTION(TPACPI_DESC);
6261 MODULE_VERSION(TPACPI_VERSION);
6262 MODULE_LICENSE("GPL");
6264 module_init(thinkpad_acpi_module_init);
6265 module_exit(thinkpad_acpi_module_exit);