ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob7cba1f696cd3ea8992c7f680a40dcf463d4b06e4
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.18-20071112"
25 #define TPACPI_SYSFS_VERSION 0x020100
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 /* NVRAM Addresses */
90 enum tp_nvram_addr {
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
98 /* NVRAM bit masks */
99 enum {
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
115 /* ACPI HIDs */
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
118 /* Input IDs */
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
124 * Main driver
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
140 /* Debugging */
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
154 } while (0)
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
159 #else
160 #define vdbg_printk(a_dbg_level, format, arg...)
161 #endif
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
172 struct ibm_struct;
174 struct tp_acpi_drv_struct {
175 char *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
179 acpi_handle *handle;
180 u32 type;
181 struct acpi_device *device;
184 struct ibm_struct {
185 char *name;
187 int (*read) (char *);
188 int (*write) (char *);
189 void (*exit) (void);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
197 struct {
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
200 u8 proc_created:1;
201 u8 init_called:1;
202 u8 experimental:1;
203 } flags;
206 struct ibm_init_struct {
207 char param[32];
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
213 static struct {
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
215 u32 bay_status:1;
216 u32 bay_eject:1;
217 u32 bay_status2:1;
218 u32 bay_eject2:1;
219 #endif
220 u32 bluetooth:1;
221 u32 hotkey:1;
222 u32 hotkey_mask:1;
223 u32 hotkey_wlsw:1;
224 u32 light:1;
225 u32 light_status:1;
226 u32 bright_16levels:1;
227 u32 wan:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 hotkey_report_mode_locked:1;
231 u32 platform_drv_registered:1;
232 u32 platform_drv_attrs_registered:1;
233 u32 sensors_pdrv_registered:1;
234 u32 sensors_pdrv_attrs_registered:1;
235 u32 sensors_pdev_attrs_registered:1;
236 u32 hotkey_poll_active:1;
237 } tp_features;
239 struct thinkpad_id_data {
240 unsigned int vendor; /* ThinkPad vendor:
241 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
243 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
244 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
246 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
247 u16 ec_model;
249 char *model_str;
251 static struct thinkpad_id_data thinkpad_id;
253 static enum {
254 TPACPI_LIFE_INIT = 0,
255 TPACPI_LIFE_RUNNING,
256 TPACPI_LIFE_EXITING,
257 } tpacpi_lifecycle;
259 static int experimental;
260 static u32 dbg_level;
262 /****************************************************************************
263 ****************************************************************************
265 * ACPI Helpers and device model
267 ****************************************************************************
268 ****************************************************************************/
270 /*************************************************************************
271 * ACPI basic handles
274 static acpi_handle root_handle;
276 #define TPACPI_HANDLE(object, parent, paths...) \
277 static acpi_handle object##_handle; \
278 static acpi_handle *object##_parent = &parent##_handle; \
279 static char *object##_path; \
280 static char *object##_paths[] = { paths }
282 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
283 "\\_SB.PCI.ISA.EC", /* 570 */
284 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
285 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
286 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
287 "\\_SB.PCI0.ICH3.EC0", /* R31 */
288 "\\_SB.PCI0.LPC.EC", /* all others */
291 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
292 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
294 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
295 /* T4x, X31, X40 */
296 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
297 "\\CMS", /* R40, R40e */
298 ); /* all others */
300 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
301 "^HKEY", /* R30, R31 */
302 "HKEY", /* all others */
303 ); /* 570 */
306 /*************************************************************************
307 * ACPI helpers
310 static int acpi_evalf(acpi_handle handle,
311 void *res, char *method, char *fmt, ...)
313 char *fmt0 = fmt;
314 struct acpi_object_list params;
315 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
316 struct acpi_buffer result, *resultp;
317 union acpi_object out_obj;
318 acpi_status status;
319 va_list ap;
320 char res_type;
321 int success;
322 int quiet;
324 if (!*fmt) {
325 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
326 return 0;
329 if (*fmt == 'q') {
330 quiet = 1;
331 fmt++;
332 } else
333 quiet = 0;
335 res_type = *(fmt++);
337 params.count = 0;
338 params.pointer = &in_objs[0];
340 va_start(ap, fmt);
341 while (*fmt) {
342 char c = *(fmt++);
343 switch (c) {
344 case 'd': /* int */
345 in_objs[params.count].integer.value = va_arg(ap, int);
346 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
347 break;
348 /* add more types as needed */
349 default:
350 printk(TPACPI_ERR "acpi_evalf() called "
351 "with invalid format character '%c'\n", c);
352 return 0;
355 va_end(ap);
357 if (res_type != 'v') {
358 result.length = sizeof(out_obj);
359 result.pointer = &out_obj;
360 resultp = &result;
361 } else
362 resultp = NULL;
364 status = acpi_evaluate_object(handle, method, &params, resultp);
366 switch (res_type) {
367 case 'd': /* int */
368 if (res)
369 *(int *)res = out_obj.integer.value;
370 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
371 break;
372 case 'v': /* void */
373 success = status == AE_OK;
374 break;
375 /* add more types as needed */
376 default:
377 printk(TPACPI_ERR "acpi_evalf() called "
378 "with invalid format character '%c'\n", res_type);
379 return 0;
382 if (!success && !quiet)
383 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
384 method, fmt0, status);
386 return success;
389 static int acpi_ec_read(int i, u8 *p)
391 int v;
393 if (ecrd_handle) {
394 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
395 return 0;
396 *p = v;
397 } else {
398 if (ec_read(i, p) < 0)
399 return 0;
402 return 1;
405 static int acpi_ec_write(int i, u8 v)
407 if (ecwr_handle) {
408 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
409 return 0;
410 } else {
411 if (ec_write(i, v) < 0)
412 return 0;
415 return 1;
418 static int _sta(acpi_handle handle)
420 int status;
422 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
423 status = 0;
425 return status;
428 static int issue_thinkpad_cmos_command(int cmos_cmd)
430 if (!cmos_handle)
431 return -ENXIO;
433 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
434 return -EIO;
436 return 0;
439 /*************************************************************************
440 * ACPI device model
443 #define TPACPI_ACPIHANDLE_INIT(object) \
444 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
445 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
447 static void drv_acpi_handle_init(char *name,
448 acpi_handle *handle, acpi_handle parent,
449 char **paths, int num_paths, char **path)
451 int i;
452 acpi_status status;
454 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
455 name);
457 for (i = 0; i < num_paths; i++) {
458 status = acpi_get_handle(parent, paths[i], handle);
459 if (ACPI_SUCCESS(status)) {
460 *path = paths[i];
461 dbg_printk(TPACPI_DBG_INIT,
462 "Found ACPI handle %s for %s\n",
463 *path, name);
464 return;
468 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
469 name);
470 *handle = NULL;
473 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
475 struct ibm_struct *ibm = data;
477 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
478 return;
480 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
481 return;
483 ibm->acpi->notify(ibm, event);
486 static int __init setup_acpi_notify(struct ibm_struct *ibm)
488 acpi_status status;
489 int rc;
491 BUG_ON(!ibm->acpi);
493 if (!*ibm->acpi->handle)
494 return 0;
496 vdbg_printk(TPACPI_DBG_INIT,
497 "setting up ACPI notify for %s\n", ibm->name);
499 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
500 if (rc < 0) {
501 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
502 ibm->name, rc);
503 return -ENODEV;
506 acpi_driver_data(ibm->acpi->device) = ibm;
507 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
508 TPACPI_ACPI_EVENT_PREFIX,
509 ibm->name);
511 status = acpi_install_notify_handler(*ibm->acpi->handle,
512 ibm->acpi->type, dispatch_acpi_notify, ibm);
513 if (ACPI_FAILURE(status)) {
514 if (status == AE_ALREADY_EXISTS) {
515 printk(TPACPI_NOTICE
516 "another device driver is already "
517 "handling %s events\n", ibm->name);
518 } else {
519 printk(TPACPI_ERR
520 "acpi_install_notify_handler(%s) failed: %d\n",
521 ibm->name, status);
523 return -ENODEV;
525 ibm->flags.acpi_notify_installed = 1;
526 return 0;
529 static int __init tpacpi_device_add(struct acpi_device *device)
531 return 0;
534 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
536 int rc;
538 dbg_printk(TPACPI_DBG_INIT,
539 "registering %s as an ACPI driver\n", ibm->name);
541 BUG_ON(!ibm->acpi);
543 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
544 if (!ibm->acpi->driver) {
545 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
546 return -ENOMEM;
549 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
550 ibm->acpi->driver->ids = ibm->acpi->hid;
551 ibm->acpi->driver->ops.add = &tpacpi_device_add;
553 rc = acpi_bus_register_driver(ibm->acpi->driver);
554 if (rc < 0) {
555 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
556 ibm->acpi->hid, rc);
557 kfree(ibm->acpi->driver);
558 ibm->acpi->driver = NULL;
559 } else if (!rc)
560 ibm->flags.acpi_driver_registered = 1;
562 return rc;
566 /****************************************************************************
567 ****************************************************************************
569 * Procfs Helpers
571 ****************************************************************************
572 ****************************************************************************/
574 static int dispatch_procfs_read(char *page, char **start, off_t off,
575 int count, int *eof, void *data)
577 struct ibm_struct *ibm = data;
578 int len;
580 if (!ibm || !ibm->read)
581 return -EINVAL;
583 len = ibm->read(page);
584 if (len < 0)
585 return len;
587 if (len <= off + count)
588 *eof = 1;
589 *start = page + off;
590 len -= off;
591 if (len > count)
592 len = count;
593 if (len < 0)
594 len = 0;
596 return len;
599 static int dispatch_procfs_write(struct file *file,
600 const char __user *userbuf,
601 unsigned long count, void *data)
603 struct ibm_struct *ibm = data;
604 char *kernbuf;
605 int ret;
607 if (!ibm || !ibm->write)
608 return -EINVAL;
610 kernbuf = kmalloc(count + 2, GFP_KERNEL);
611 if (!kernbuf)
612 return -ENOMEM;
614 if (copy_from_user(kernbuf, userbuf, count)) {
615 kfree(kernbuf);
616 return -EFAULT;
619 kernbuf[count] = 0;
620 strcat(kernbuf, ",");
621 ret = ibm->write(kernbuf);
622 if (ret == 0)
623 ret = count;
625 kfree(kernbuf);
627 return ret;
630 static char *next_cmd(char **cmds)
632 char *start = *cmds;
633 char *end;
635 while ((end = strchr(start, ',')) && end == start)
636 start = end + 1;
638 if (!end)
639 return NULL;
641 *end = 0;
642 *cmds = end + 1;
643 return start;
647 /****************************************************************************
648 ****************************************************************************
650 * Device model: input, hwmon and platform
652 ****************************************************************************
653 ****************************************************************************/
655 static struct platform_device *tpacpi_pdev;
656 static struct platform_device *tpacpi_sensors_pdev;
657 static struct class_device *tpacpi_hwmon;
658 static struct input_dev *tpacpi_inputdev;
659 static struct mutex tpacpi_inputdev_send_mutex;
660 static LIST_HEAD(tpacpi_all_drivers);
662 static int tpacpi_suspend_handler(struct platform_device *pdev,
663 pm_message_t state)
665 struct ibm_struct *ibm, *itmp;
667 list_for_each_entry_safe(ibm, itmp,
668 &tpacpi_all_drivers,
669 all_drivers) {
670 if (ibm->suspend)
671 (ibm->suspend)(state);
674 return 0;
677 static int tpacpi_resume_handler(struct platform_device *pdev)
679 struct ibm_struct *ibm, *itmp;
681 list_for_each_entry_safe(ibm, itmp,
682 &tpacpi_all_drivers,
683 all_drivers) {
684 if (ibm->resume)
685 (ibm->resume)();
688 return 0;
691 static struct platform_driver tpacpi_pdriver = {
692 .driver = {
693 .name = TPACPI_DRVR_NAME,
694 .owner = THIS_MODULE,
696 .suspend = tpacpi_suspend_handler,
697 .resume = tpacpi_resume_handler,
700 static struct platform_driver tpacpi_hwmon_pdriver = {
701 .driver = {
702 .name = TPACPI_HWMON_DRVR_NAME,
703 .owner = THIS_MODULE,
707 /*************************************************************************
708 * sysfs support helpers
711 struct attribute_set {
712 unsigned int members, max_members;
713 struct attribute_group group;
716 struct attribute_set_obj {
717 struct attribute_set s;
718 struct attribute *a;
719 } __attribute__((packed));
721 static struct attribute_set *create_attr_set(unsigned int max_members,
722 const char *name)
724 struct attribute_set_obj *sobj;
726 if (max_members == 0)
727 return NULL;
729 /* Allocates space for implicit NULL at the end too */
730 sobj = kzalloc(sizeof(struct attribute_set_obj) +
731 max_members * sizeof(struct attribute *),
732 GFP_KERNEL);
733 if (!sobj)
734 return NULL;
735 sobj->s.max_members = max_members;
736 sobj->s.group.attrs = &sobj->a;
737 sobj->s.group.name = name;
739 return &sobj->s;
742 #define destroy_attr_set(_set) \
743 kfree(_set);
745 /* not multi-threaded safe, use it in a single thread per set */
746 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
748 if (!s || !attr)
749 return -EINVAL;
751 if (s->members >= s->max_members)
752 return -ENOMEM;
754 s->group.attrs[s->members] = attr;
755 s->members++;
757 return 0;
760 static int add_many_to_attr_set(struct attribute_set *s,
761 struct attribute **attr,
762 unsigned int count)
764 int i, res;
766 for (i = 0; i < count; i++) {
767 res = add_to_attr_set(s, attr[i]);
768 if (res)
769 return res;
772 return 0;
775 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
777 sysfs_remove_group(kobj, &s->group);
778 destroy_attr_set(s);
781 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
782 sysfs_create_group(_kobj, &_attr_set->group)
784 static int parse_strtoul(const char *buf,
785 unsigned long max, unsigned long *value)
787 char *endp;
789 while (*buf && isspace(*buf))
790 buf++;
791 *value = simple_strtoul(buf, &endp, 0);
792 while (*endp && isspace(*endp))
793 endp++;
794 if (*endp || *value > max)
795 return -EINVAL;
797 return 0;
800 /*************************************************************************
801 * thinkpad-acpi driver attributes
804 /* interface_version --------------------------------------------------- */
805 static ssize_t tpacpi_driver_interface_version_show(
806 struct device_driver *drv,
807 char *buf)
809 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
812 static DRIVER_ATTR(interface_version, S_IRUGO,
813 tpacpi_driver_interface_version_show, NULL);
815 /* debug_level --------------------------------------------------------- */
816 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
817 char *buf)
819 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
822 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
823 const char *buf, size_t count)
825 unsigned long t;
827 if (parse_strtoul(buf, 0xffff, &t))
828 return -EINVAL;
830 dbg_level = t;
832 return count;
835 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
836 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
838 /* version ------------------------------------------------------------- */
839 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
840 char *buf)
842 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
843 TPACPI_DESC, TPACPI_VERSION);
846 static DRIVER_ATTR(version, S_IRUGO,
847 tpacpi_driver_version_show, NULL);
849 /* --------------------------------------------------------------------- */
851 static struct driver_attribute *tpacpi_driver_attributes[] = {
852 &driver_attr_debug_level, &driver_attr_version,
853 &driver_attr_interface_version,
856 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
858 int i, res;
860 i = 0;
861 res = 0;
862 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
863 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
864 i++;
867 return res;
870 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
872 int i;
874 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
875 driver_remove_file(drv, tpacpi_driver_attributes[i]);
878 /****************************************************************************
879 ****************************************************************************
881 * Subdrivers
883 ****************************************************************************
884 ****************************************************************************/
886 /*************************************************************************
887 * thinkpad-acpi init subdriver
890 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
892 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
893 printk(TPACPI_INFO "%s\n", TPACPI_URL);
895 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
896 (thinkpad_id.bios_version_str) ?
897 thinkpad_id.bios_version_str : "unknown",
898 (thinkpad_id.ec_version_str) ?
899 thinkpad_id.ec_version_str : "unknown");
901 if (thinkpad_id.vendor && thinkpad_id.model_str)
902 printk(TPACPI_INFO "%s %s\n",
903 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
904 "IBM" : ((thinkpad_id.vendor ==
905 PCI_VENDOR_ID_LENOVO) ?
906 "Lenovo" : "Unknown vendor"),
907 thinkpad_id.model_str);
909 return 0;
912 static int thinkpad_acpi_driver_read(char *p)
914 int len = 0;
916 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
917 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
919 return len;
922 static struct ibm_struct thinkpad_acpi_driver_data = {
923 .name = "driver",
924 .read = thinkpad_acpi_driver_read,
927 /*************************************************************************
928 * Hotkey subdriver
931 enum { /* hot key scan codes (derived from ACPI DSDT) */
932 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
933 TP_ACPI_HOTKEYSCAN_FNF2,
934 TP_ACPI_HOTKEYSCAN_FNF3,
935 TP_ACPI_HOTKEYSCAN_FNF4,
936 TP_ACPI_HOTKEYSCAN_FNF5,
937 TP_ACPI_HOTKEYSCAN_FNF6,
938 TP_ACPI_HOTKEYSCAN_FNF7,
939 TP_ACPI_HOTKEYSCAN_FNF8,
940 TP_ACPI_HOTKEYSCAN_FNF9,
941 TP_ACPI_HOTKEYSCAN_FNF10,
942 TP_ACPI_HOTKEYSCAN_FNF11,
943 TP_ACPI_HOTKEYSCAN_FNF12,
944 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
945 TP_ACPI_HOTKEYSCAN_FNINSERT,
946 TP_ACPI_HOTKEYSCAN_FNDELETE,
947 TP_ACPI_HOTKEYSCAN_FNHOME,
948 TP_ACPI_HOTKEYSCAN_FNEND,
949 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
950 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
951 TP_ACPI_HOTKEYSCAN_FNSPACE,
952 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
953 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
954 TP_ACPI_HOTKEYSCAN_MUTE,
955 TP_ACPI_HOTKEYSCAN_THINKPAD,
958 enum { /* Keys available through NVRAM polling */
959 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
960 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
963 enum { /* Positions of some of the keys in hotkey masks */
964 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
965 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
966 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
967 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
968 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
969 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
970 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
971 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
972 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
973 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
974 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
977 enum { /* NVRAM to ACPI HKEY group map */
978 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
979 TP_ACPI_HKEY_ZOOM_MASK |
980 TP_ACPI_HKEY_DISPSWTCH_MASK |
981 TP_ACPI_HKEY_HIBERNATE_MASK,
982 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
983 TP_ACPI_HKEY_BRGHTDWN_MASK,
984 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
985 TP_ACPI_HKEY_VOLDWN_MASK |
986 TP_ACPI_HKEY_MUTE_MASK,
989 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
990 struct tp_nvram_state {
991 u16 thinkpad_toggle:1;
992 u16 zoom_toggle:1;
993 u16 display_toggle:1;
994 u16 thinklight_toggle:1;
995 u16 hibernate_toggle:1;
996 u16 displayexp_toggle:1;
997 u16 display_state:1;
998 u16 brightness_toggle:1;
999 u16 volume_toggle:1;
1000 u16 mute:1;
1002 u8 brightness_level;
1003 u8 volume_level;
1006 static struct task_struct *tpacpi_hotkey_task;
1007 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1008 static int hotkey_poll_freq = 10; /* Hz */
1009 static struct mutex hotkey_thread_mutex;
1010 static struct mutex hotkey_thread_data_mutex;
1011 static unsigned int hotkey_config_change;
1013 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1015 #define hotkey_source_mask 0U
1017 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1019 static struct mutex hotkey_mutex;
1021 static enum { /* Reasons for waking up */
1022 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1023 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1024 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1025 } hotkey_wakeup_reason;
1027 static int hotkey_autosleep_ack;
1029 static int hotkey_orig_status;
1030 static u32 hotkey_orig_mask;
1031 static u32 hotkey_all_mask;
1032 static u32 hotkey_reserved_mask;
1033 static u32 hotkey_mask;
1035 static unsigned int hotkey_report_mode;
1037 static u16 *hotkey_keycode_map;
1039 static struct attribute_set *hotkey_dev_attributes;
1041 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1042 #define HOTKEY_CONFIG_CRITICAL_START \
1043 do { \
1044 mutex_lock(&hotkey_thread_data_mutex); \
1045 hotkey_config_change++; \
1046 } while (0);
1047 #define HOTKEY_CONFIG_CRITICAL_END \
1048 mutex_unlock(&hotkey_thread_data_mutex);
1049 #else
1050 #define HOTKEY_CONFIG_CRITICAL_START
1051 #define HOTKEY_CONFIG_CRITICAL_END
1052 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1054 static int hotkey_get_wlsw(int *status)
1056 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1057 return -EIO;
1058 return 0;
1062 * Call with hotkey_mutex held
1064 static int hotkey_mask_get(void)
1066 u32 m;
1068 if (tp_features.hotkey_mask) {
1069 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1070 return -EIO;
1071 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1074 return 0;
1078 * Call with hotkey_mutex held
1080 static int hotkey_mask_set(u32 mask)
1082 int i;
1083 int rc = 0;
1085 if (tp_features.hotkey_mask) {
1086 HOTKEY_CONFIG_CRITICAL_START
1087 for (i = 0; i < 32; i++) {
1088 u32 m = 1 << i;
1089 if (!acpi_evalf(hkey_handle,
1090 NULL, "MHKM", "vdd", i + 1,
1091 !!(mask & m))) {
1092 rc = -EIO;
1093 break;
1094 } else {
1095 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1098 HOTKEY_CONFIG_CRITICAL_END
1100 /* hotkey_mask_get must be called unconditionally below */
1101 if (!hotkey_mask_get() && !rc &&
1102 (hotkey_mask & ~hotkey_source_mask) !=
1103 (mask & ~hotkey_source_mask)) {
1104 printk(TPACPI_NOTICE
1105 "requested hot key mask 0x%08x, but "
1106 "firmware forced it to 0x%08x\n",
1107 mask, hotkey_mask);
1111 return rc;
1114 static int hotkey_status_get(int *status)
1116 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1117 return -EIO;
1119 return 0;
1122 static int hotkey_status_set(int status)
1124 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1125 return -EIO;
1127 return 0;
1130 static void tpacpi_input_send_radiosw(void)
1132 int wlsw;
1134 mutex_lock(&tpacpi_inputdev_send_mutex);
1136 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1137 input_report_switch(tpacpi_inputdev,
1138 SW_RADIO, !!wlsw);
1139 input_sync(tpacpi_inputdev);
1142 mutex_unlock(&tpacpi_inputdev_send_mutex);
1145 static void tpacpi_input_send_key(unsigned int scancode)
1147 unsigned int keycode;
1149 keycode = hotkey_keycode_map[scancode];
1151 if (keycode != KEY_RESERVED) {
1152 mutex_lock(&tpacpi_inputdev_send_mutex);
1154 input_report_key(tpacpi_inputdev, keycode, 1);
1155 if (keycode == KEY_UNKNOWN)
1156 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1157 scancode);
1158 input_sync(tpacpi_inputdev);
1160 input_report_key(tpacpi_inputdev, keycode, 0);
1161 if (keycode == KEY_UNKNOWN)
1162 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1163 scancode);
1164 input_sync(tpacpi_inputdev);
1166 mutex_unlock(&tpacpi_inputdev_send_mutex);
1170 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1171 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1173 static void tpacpi_hotkey_send_key(unsigned int scancode)
1175 tpacpi_input_send_key(scancode);
1176 if (hotkey_report_mode < 2) {
1177 acpi_bus_generate_event(ibm_hotkey_acpidriver.device,
1178 0x80, 0x1001 + scancode);
1182 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1184 u8 d;
1186 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1187 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1188 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1189 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1190 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1191 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1193 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1194 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1195 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1197 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1198 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1199 n->displayexp_toggle =
1200 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1202 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1203 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1204 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1205 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1206 n->brightness_toggle =
1207 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1209 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1210 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1211 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1212 >> TP_NVRAM_POS_LEVEL_VOLUME;
1213 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1214 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1218 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1219 do { \
1220 if ((mask & (1 << __scancode)) && \
1221 oldn->__member != newn->__member) \
1222 tpacpi_hotkey_send_key(__scancode); \
1223 } while (0)
1225 #define TPACPI_MAY_SEND_KEY(__scancode) \
1226 do { if (mask & (1 << __scancode)) \
1227 tpacpi_hotkey_send_key(__scancode); } while (0)
1229 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1230 struct tp_nvram_state *newn,
1231 u32 mask)
1233 if (oldn == newn)
1234 return;
1236 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1237 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1238 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1239 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1241 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1243 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1245 /* handle volume */
1246 if (oldn->volume_toggle != newn->volume_toggle) {
1247 if (oldn->mute != newn->mute) {
1248 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1250 if (oldn->volume_level > newn->volume_level) {
1251 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1252 } else if (oldn->volume_level < newn->volume_level) {
1253 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1254 } else if (oldn->mute == newn->mute) {
1255 /* repeated key presses that didn't change state */
1256 if (newn->mute) {
1257 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1258 } else if (newn->volume_level != 0) {
1259 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1260 } else {
1261 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1266 /* handle brightness */
1267 if (oldn->brightness_toggle != newn->brightness_toggle) {
1268 if (oldn->brightness_level < newn->brightness_level) {
1269 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1270 } else if (oldn->brightness_level > newn->brightness_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1272 } else {
1273 /* repeated key presses that didn't change state */
1274 if (newn->brightness_level != 0) {
1275 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1276 } else {
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1283 #undef TPACPI_COMPARE_KEY
1284 #undef TPACPI_MAY_SEND_KEY
1286 static int hotkey_kthread(void *data)
1288 struct tp_nvram_state s[2];
1289 u32 mask;
1290 unsigned int si, so;
1291 unsigned long t;
1292 unsigned int change_detector, must_reset;
1294 mutex_lock(&hotkey_thread_mutex);
1296 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1297 goto exit;
1299 so = 0;
1300 si = 1;
1301 t = 0;
1303 /* Initial state for compares */
1304 mutex_lock(&hotkey_thread_data_mutex);
1305 change_detector = hotkey_config_change;
1306 mask = hotkey_source_mask & hotkey_mask;
1307 mutex_unlock(&hotkey_thread_data_mutex);
1308 hotkey_read_nvram(&s[so], mask);
1310 while (!kthread_should_stop() && hotkey_poll_freq) {
1311 if (t == 0)
1312 t = 1000/hotkey_poll_freq;
1313 t = msleep_interruptible(t);
1314 if (unlikely(kthread_should_stop()))
1315 break;
1316 must_reset = try_to_freeze();
1317 if (t > 0 && !must_reset)
1318 continue;
1320 mutex_lock(&hotkey_thread_data_mutex);
1321 if (must_reset || hotkey_config_change != change_detector) {
1322 /* forget old state on thaw or config change */
1323 si = so;
1324 t = 0;
1325 change_detector = hotkey_config_change;
1327 mask = hotkey_source_mask & hotkey_mask;
1328 mutex_unlock(&hotkey_thread_data_mutex);
1330 hotkey_read_nvram(&s[si], mask);
1331 hotkey_compare_and_issue_event(&s[so], &s[si], mask);
1333 so = si;
1334 si ^= 1;
1337 exit:
1338 mutex_unlock(&hotkey_thread_mutex);
1339 return 0;
1342 static void hotkey_poll_stop_sync(void)
1344 if (tpacpi_hotkey_task) {
1345 if (frozen(tpacpi_hotkey_task) ||
1346 freezing(tpacpi_hotkey_task))
1347 thaw_process(tpacpi_hotkey_task);
1349 kthread_stop(tpacpi_hotkey_task);
1350 tpacpi_hotkey_task = NULL;
1351 mutex_lock(&hotkey_thread_mutex);
1352 /* at this point, the thread did exit */
1353 mutex_unlock(&hotkey_thread_mutex);
1357 /* call with hotkey_mutex held */
1358 static void hotkey_poll_setup(int may_warn)
1360 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1361 hotkey_poll_freq > 0 &&
1362 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1363 if (!tpacpi_hotkey_task) {
1364 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1365 NULL,
1366 TPACPI_FILE "d");
1367 if (IS_ERR(tpacpi_hotkey_task)) {
1368 tpacpi_hotkey_task = NULL;
1369 printk(TPACPI_ERR
1370 "could not create kernel thread "
1371 "for hotkey polling\n");
1374 } else {
1375 hotkey_poll_stop_sync();
1376 if (may_warn &&
1377 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1378 printk(TPACPI_NOTICE
1379 "hot keys 0x%08x require polling, "
1380 "which is currently disabled\n",
1381 hotkey_source_mask);
1386 static void hotkey_poll_setup_safe(int may_warn)
1388 mutex_lock(&hotkey_mutex);
1389 hotkey_poll_setup(may_warn);
1390 mutex_unlock(&hotkey_mutex);
1393 static int hotkey_inputdev_open(struct input_dev *dev)
1395 switch (tpacpi_lifecycle) {
1396 case TPACPI_LIFE_INIT:
1398 * hotkey_init will call hotkey_poll_setup_safe
1399 * at the appropriate moment
1401 return 0;
1402 case TPACPI_LIFE_EXITING:
1403 return -EBUSY;
1404 case TPACPI_LIFE_RUNNING:
1405 hotkey_poll_setup_safe(0);
1406 return 0;
1409 /* Should only happen if tpacpi_lifecycle is corrupt */
1410 BUG();
1411 return -EBUSY;
1414 static void hotkey_inputdev_close(struct input_dev *dev)
1416 /* disable hotkey polling when possible */
1417 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1418 hotkey_poll_setup_safe(0);
1420 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1422 /* sysfs hotkey enable ------------------------------------------------- */
1423 static ssize_t hotkey_enable_show(struct device *dev,
1424 struct device_attribute *attr,
1425 char *buf)
1427 int res, status;
1429 res = hotkey_status_get(&status);
1430 if (res)
1431 return res;
1433 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1436 static ssize_t hotkey_enable_store(struct device *dev,
1437 struct device_attribute *attr,
1438 const char *buf, size_t count)
1440 unsigned long t;
1441 int res;
1443 if (parse_strtoul(buf, 1, &t))
1444 return -EINVAL;
1446 res = hotkey_status_set(t);
1448 return (res) ? res : count;
1451 static struct device_attribute dev_attr_hotkey_enable =
1452 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1453 hotkey_enable_show, hotkey_enable_store);
1455 /* sysfs hotkey mask --------------------------------------------------- */
1456 static ssize_t hotkey_mask_show(struct device *dev,
1457 struct device_attribute *attr,
1458 char *buf)
1460 int res;
1462 if (mutex_lock_interruptible(&hotkey_mutex))
1463 return -ERESTARTSYS;
1464 res = hotkey_mask_get();
1465 mutex_unlock(&hotkey_mutex);
1467 return (res)?
1468 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1471 static ssize_t hotkey_mask_store(struct device *dev,
1472 struct device_attribute *attr,
1473 const char *buf, size_t count)
1475 unsigned long t;
1476 int res;
1478 if (parse_strtoul(buf, 0xffffffffUL, &t))
1479 return -EINVAL;
1481 if (mutex_lock_interruptible(&hotkey_mutex))
1482 return -ERESTARTSYS;
1484 res = hotkey_mask_set(t);
1486 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1487 hotkey_poll_setup(1);
1488 #endif
1490 mutex_unlock(&hotkey_mutex);
1492 return (res) ? res : count;
1495 static struct device_attribute dev_attr_hotkey_mask =
1496 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1497 hotkey_mask_show, hotkey_mask_store);
1499 /* sysfs hotkey bios_enabled ------------------------------------------- */
1500 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1501 struct device_attribute *attr,
1502 char *buf)
1504 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1507 static struct device_attribute dev_attr_hotkey_bios_enabled =
1508 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1510 /* sysfs hotkey bios_mask ---------------------------------------------- */
1511 static ssize_t hotkey_bios_mask_show(struct device *dev,
1512 struct device_attribute *attr,
1513 char *buf)
1515 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1518 static struct device_attribute dev_attr_hotkey_bios_mask =
1519 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1521 /* sysfs hotkey all_mask ----------------------------------------------- */
1522 static ssize_t hotkey_all_mask_show(struct device *dev,
1523 struct device_attribute *attr,
1524 char *buf)
1526 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1527 hotkey_all_mask | hotkey_source_mask);
1530 static struct device_attribute dev_attr_hotkey_all_mask =
1531 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1533 /* sysfs hotkey recommended_mask --------------------------------------- */
1534 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1535 struct device_attribute *attr,
1536 char *buf)
1538 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1539 (hotkey_all_mask | hotkey_source_mask)
1540 & ~hotkey_reserved_mask);
1543 static struct device_attribute dev_attr_hotkey_recommended_mask =
1544 __ATTR(hotkey_recommended_mask, S_IRUGO,
1545 hotkey_recommended_mask_show, NULL);
1547 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1549 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1550 static ssize_t hotkey_source_mask_show(struct device *dev,
1551 struct device_attribute *attr,
1552 char *buf)
1554 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1557 static ssize_t hotkey_source_mask_store(struct device *dev,
1558 struct device_attribute *attr,
1559 const char *buf, size_t count)
1561 unsigned long t;
1563 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1564 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1565 return -EINVAL;
1567 if (mutex_lock_interruptible(&hotkey_mutex))
1568 return -ERESTARTSYS;
1570 HOTKEY_CONFIG_CRITICAL_START
1571 hotkey_source_mask = t;
1572 HOTKEY_CONFIG_CRITICAL_END
1574 hotkey_poll_setup(1);
1576 mutex_unlock(&hotkey_mutex);
1578 return count;
1581 static struct device_attribute dev_attr_hotkey_source_mask =
1582 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1583 hotkey_source_mask_show, hotkey_source_mask_store);
1585 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1586 static ssize_t hotkey_poll_freq_show(struct device *dev,
1587 struct device_attribute *attr,
1588 char *buf)
1590 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1593 static ssize_t hotkey_poll_freq_store(struct device *dev,
1594 struct device_attribute *attr,
1595 const char *buf, size_t count)
1597 unsigned long t;
1599 if (parse_strtoul(buf, 25, &t))
1600 return -EINVAL;
1602 if (mutex_lock_interruptible(&hotkey_mutex))
1603 return -ERESTARTSYS;
1605 hotkey_poll_freq = t;
1607 hotkey_poll_setup(1);
1608 mutex_unlock(&hotkey_mutex);
1610 return count;
1613 static struct device_attribute dev_attr_hotkey_poll_freq =
1614 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1615 hotkey_poll_freq_show, hotkey_poll_freq_store);
1617 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1619 /* sysfs hotkey radio_sw ----------------------------------------------- */
1620 static ssize_t hotkey_radio_sw_show(struct device *dev,
1621 struct device_attribute *attr,
1622 char *buf)
1624 int res, s;
1625 res = hotkey_get_wlsw(&s);
1626 if (res < 0)
1627 return res;
1629 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1632 static struct device_attribute dev_attr_hotkey_radio_sw =
1633 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1635 /* sysfs hotkey report_mode -------------------------------------------- */
1636 static ssize_t hotkey_report_mode_show(struct device *dev,
1637 struct device_attribute *attr,
1638 char *buf)
1640 return snprintf(buf, PAGE_SIZE, "%d\n",
1641 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1644 static ssize_t hotkey_report_mode_store(struct device *dev,
1645 struct device_attribute *attr,
1646 const char *buf, size_t count)
1648 unsigned long t;
1649 int res = 0;
1651 if (parse_strtoul(buf, 2, &t) || (t < 1))
1652 return -EINVAL;
1654 if (t != hotkey_report_mode) {
1655 if (tp_features.hotkey_report_mode_locked)
1656 return -EPERM;
1657 hotkey_report_mode = t;
1658 printk(TPACPI_NOTICE "hot key report mode set to %d\n",
1659 hotkey_report_mode);
1662 return (res) ? res : count;
1665 static struct device_attribute dev_attr_hotkey_report_mode =
1666 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
1667 hotkey_report_mode_show, hotkey_report_mode_store);
1669 /* sysfs wakeup reason ------------------------------------------------- */
1670 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1671 struct device_attribute *attr,
1672 char *buf)
1674 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1677 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1678 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1680 /* sysfs wakeup hotunplug_complete ------------------------------------- */
1681 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1682 struct device_attribute *attr,
1683 char *buf)
1685 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1688 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1689 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1690 hotkey_wakeup_hotunplug_complete_show, NULL);
1692 /* --------------------------------------------------------------------- */
1694 static struct attribute *hotkey_attributes[] __initdata = {
1695 &dev_attr_hotkey_enable.attr,
1696 &dev_attr_hotkey_report_mode.attr,
1699 static struct attribute *hotkey_mask_attributes[] __initdata = {
1700 &dev_attr_hotkey_mask.attr,
1701 &dev_attr_hotkey_bios_enabled.attr,
1702 &dev_attr_hotkey_bios_mask.attr,
1703 &dev_attr_hotkey_all_mask.attr,
1704 &dev_attr_hotkey_recommended_mask.attr,
1705 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1706 &dev_attr_hotkey_source_mask.attr,
1707 &dev_attr_hotkey_poll_freq.attr,
1708 #endif
1709 &dev_attr_hotkey_wakeup_reason.attr,
1710 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1713 static int __init hotkey_init(struct ibm_init_struct *iibm)
1716 static u16 ibm_keycode_map[] __initdata = {
1717 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1718 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1719 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1720 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1721 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1722 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1723 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1724 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1725 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1726 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1727 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1728 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1729 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1730 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1731 KEY_RESERVED, /* 0x14: VOLUME UP */
1732 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1733 KEY_RESERVED, /* 0x16: MUTE */
1734 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1735 /* (assignments unknown, please report if found) */
1736 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1737 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1739 static u16 lenovo_keycode_map[] __initdata = {
1740 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1741 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1742 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1743 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1744 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1745 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1746 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1747 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1748 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1749 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1750 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1751 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1752 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1753 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1754 KEY_RESERVED, /* 0x14: VOLUME UP */
1755 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1756 KEY_RESERVED, /* 0x16: MUTE */
1757 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1758 /* (assignments unknown, please report if found) */
1759 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1760 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1763 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1764 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1765 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1767 int res, i;
1768 int status;
1769 int hkeyv;
1771 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1773 BUG_ON(!tpacpi_inputdev);
1774 BUG_ON(tpacpi_inputdev->open != NULL ||
1775 tpacpi_inputdev->close != NULL);
1777 TPACPI_ACPIHANDLE_INIT(hkey);
1778 mutex_init(&hotkey_mutex);
1780 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1781 mutex_init(&hotkey_thread_mutex);
1782 mutex_init(&hotkey_thread_data_mutex);
1783 #endif
1785 /* hotkey not supported on 570 */
1786 tp_features.hotkey = hkey_handle != NULL;
1788 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1789 str_supported(tp_features.hotkey));
1791 if (tp_features.hotkey) {
1792 hotkey_dev_attributes = create_attr_set(12, NULL);
1793 if (!hotkey_dev_attributes)
1794 return -ENOMEM;
1795 res = add_many_to_attr_set(hotkey_dev_attributes,
1796 hotkey_attributes,
1797 ARRAY_SIZE(hotkey_attributes));
1798 if (res)
1799 return res;
1801 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1802 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1803 for HKEY interface version 0x100 */
1804 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1805 if ((hkeyv >> 8) != 1) {
1806 printk(TPACPI_ERR "unknown version of the "
1807 "HKEY interface: 0x%x\n", hkeyv);
1808 printk(TPACPI_ERR "please report this to %s\n",
1809 TPACPI_MAIL);
1810 } else {
1812 * MHKV 0x100 in A31, R40, R40e,
1813 * T4x, X31, and later
1815 tp_features.hotkey_mask = 1;
1819 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1820 str_supported(tp_features.hotkey_mask));
1822 if (tp_features.hotkey_mask) {
1823 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1824 "MHKA", "qd")) {
1825 printk(TPACPI_ERR
1826 "missing MHKA handler, "
1827 "please report this to %s\n",
1828 TPACPI_MAIL);
1829 /* FN+F12, FN+F4, FN+F3 */
1830 hotkey_all_mask = 0x080cU;
1834 /* hotkey_source_mask *must* be zero for
1835 * the first hotkey_mask_get */
1836 res = hotkey_status_get(&hotkey_orig_status);
1837 if (!res && tp_features.hotkey_mask) {
1838 res = hotkey_mask_get();
1839 hotkey_orig_mask = hotkey_mask;
1840 if (!res) {
1841 res = add_many_to_attr_set(
1842 hotkey_dev_attributes,
1843 hotkey_mask_attributes,
1844 ARRAY_SIZE(hotkey_mask_attributes));
1848 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1849 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1850 & ~hotkey_all_mask;
1852 vdbg_printk(TPACPI_DBG_INIT,
1853 "hotkey source mask 0x%08x, polling freq %d\n",
1854 hotkey_source_mask, hotkey_poll_freq);
1855 #endif
1857 /* Not all thinkpads have a hardware radio switch */
1858 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1859 tp_features.hotkey_wlsw = 1;
1860 printk(TPACPI_INFO
1861 "radio switch found; radios are %s\n",
1862 enabled(status, 0));
1863 res = add_to_attr_set(hotkey_dev_attributes,
1864 &dev_attr_hotkey_radio_sw.attr);
1867 if (!res)
1868 res = register_attr_set_with_sysfs(
1869 hotkey_dev_attributes,
1870 &tpacpi_pdev->dev.kobj);
1871 if (res)
1872 return res;
1874 /* Set up key map */
1876 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1877 GFP_KERNEL);
1878 if (!hotkey_keycode_map) {
1879 printk(TPACPI_ERR
1880 "failed to allocate memory for key map\n");
1881 return -ENOMEM;
1884 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1885 dbg_printk(TPACPI_DBG_INIT,
1886 "using Lenovo default hot key map\n");
1887 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1888 TPACPI_HOTKEY_MAP_SIZE);
1889 } else {
1890 dbg_printk(TPACPI_DBG_INIT,
1891 "using IBM default hot key map\n");
1892 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1893 TPACPI_HOTKEY_MAP_SIZE);
1896 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1897 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1898 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1899 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1900 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1901 tpacpi_inputdev->keycode = hotkey_keycode_map;
1902 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1903 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1904 set_bit(hotkey_keycode_map[i],
1905 tpacpi_inputdev->keybit);
1906 } else {
1907 if (i < sizeof(hotkey_reserved_mask)*8)
1908 hotkey_reserved_mask |= 1 << i;
1912 if (tp_features.hotkey_wlsw) {
1913 set_bit(EV_SW, tpacpi_inputdev->evbit);
1914 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1917 dbg_printk(TPACPI_DBG_INIT,
1918 "enabling hot key handling\n");
1919 res = hotkey_status_set(1);
1920 if (res)
1921 return res;
1922 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
1923 & ~hotkey_reserved_mask)
1924 | hotkey_orig_mask);
1925 if (res)
1926 return res;
1928 if (hotkey_report_mode > 0) {
1929 tp_features.hotkey_report_mode_locked = 1;
1930 } else {
1931 hotkey_report_mode = 1;
1932 vdbg_printk(TPACPI_DBG_INIT,
1933 "hot key reporting mode can be "
1934 "changed at runtime\n");
1937 dbg_printk(TPACPI_DBG_INIT,
1938 "legacy hot key reporting over procfs %s\n",
1939 (hotkey_report_mode < 2) ?
1940 "enabled" : "disabled");
1942 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1943 tpacpi_inputdev->open = &hotkey_inputdev_open;
1944 tpacpi_inputdev->close = &hotkey_inputdev_close;
1946 hotkey_poll_setup_safe(1);
1947 #endif
1950 return (tp_features.hotkey)? 0 : 1;
1953 static void hotkey_exit(void)
1955 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1956 hotkey_poll_stop_sync();
1957 #endif
1959 if (tp_features.hotkey) {
1960 dbg_printk(TPACPI_DBG_EXIT,
1961 "restoring original hot key mask\n");
1962 /* no short-circuit boolean operator below! */
1963 if ((hotkey_mask_set(hotkey_orig_mask) |
1964 hotkey_status_set(hotkey_orig_status)) != 0)
1965 printk(TPACPI_ERR
1966 "failed to restore hot key mask "
1967 "to BIOS defaults\n");
1970 if (hotkey_dev_attributes) {
1971 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1972 hotkey_dev_attributes = NULL;
1976 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1978 u32 hkey;
1979 unsigned int scancode;
1980 int send_acpi_ev;
1981 int ignore_acpi_ev;
1982 int unk_ev;
1984 if (event != 0x80) {
1985 printk(TPACPI_ERR
1986 "unknown HKEY notification event %d\n", event);
1987 /* forward it to userspace, maybe it knows how to handle it */
1988 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1990 return;
1993 while (1) {
1994 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1995 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
1996 return;
1999 if (hkey == 0) {
2000 /* queue empty */
2001 return;
2004 send_acpi_ev = 1;
2005 ignore_acpi_ev = 0;
2006 unk_ev = 0;
2008 switch (hkey >> 12) {
2009 case 1:
2010 /* 0x1000-0x1FFF: key presses */
2011 scancode = hkey & 0xfff;
2012 if (scancode > 0 && scancode < 0x21) {
2013 scancode--;
2014 if (!(hotkey_source_mask & (1 << scancode))) {
2015 tpacpi_input_send_key(scancode);
2016 send_acpi_ev = 0;
2017 } else {
2018 ignore_acpi_ev = 1;
2020 } else {
2021 unk_ev = 1;
2023 break;
2024 case 2:
2025 /* Wakeup reason */
2026 switch (hkey) {
2027 case 0x2304: /* suspend, undock */
2028 case 0x2404: /* hibernation, undock */
2029 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2030 ignore_acpi_ev = 1;
2031 break;
2032 case 0x2305: /* suspend, bay eject */
2033 case 0x2405: /* hibernation, bay eject */
2034 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2035 ignore_acpi_ev = 1;
2036 break;
2037 default:
2038 unk_ev = 1;
2040 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2041 printk(TPACPI_INFO
2042 "woke up due to a hot-unplug "
2043 "request...\n");
2045 break;
2046 case 3:
2047 /* bay-related wakeups */
2048 if (hkey == 0x3003) {
2049 hotkey_autosleep_ack = 1;
2050 printk(TPACPI_INFO
2051 "bay ejected\n");
2052 } else {
2053 unk_ev = 1;
2055 break;
2056 case 4:
2057 /* dock-related wakeups */
2058 if (hkey == 0x4003) {
2059 hotkey_autosleep_ack = 1;
2060 printk(TPACPI_INFO
2061 "undocked\n");
2062 } else {
2063 unk_ev = 1;
2065 break;
2066 case 5:
2067 /* 0x5000-0x5FFF: On screen display helpers */
2068 switch (hkey) {
2069 case 0x5010:
2070 /* Lenovo Vista BIOS: brightness changed */
2071 break;
2072 case 0x5001:
2073 case 0x5002:
2074 /* LID switch events. Do not propagate */
2075 ignore_acpi_ev = 1;
2076 break;
2077 default:
2078 unk_ev = 1;
2080 break;
2081 case 7:
2082 /* 0x7000-0x7FFF: misc */
2083 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2084 tpacpi_input_send_radiosw();
2085 send_acpi_ev = 0;
2086 break;
2088 /* fallthrough to default */
2089 default:
2090 unk_ev = 1;
2092 if (unk_ev) {
2093 printk(TPACPI_NOTICE
2094 "unhandled HKEY event 0x%04x\n", hkey);
2097 if (!ignore_acpi_ev &&
2098 (send_acpi_ev || hotkey_report_mode < 2)) {
2099 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
2104 static void hotkey_suspend(pm_message_t state)
2106 /* Do these on suspend, we get the events on early resume! */
2107 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2108 hotkey_autosleep_ack = 0;
2111 static void hotkey_resume(void)
2113 if (hotkey_mask_get())
2114 printk(TPACPI_ERR
2115 "error while trying to read hot key mask "
2116 "from firmware\n");
2117 tpacpi_input_send_radiosw();
2118 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2119 hotkey_poll_setup_safe(0);
2120 #endif
2123 /* procfs -------------------------------------------------------------- */
2124 static int hotkey_read(char *p)
2126 int res, status;
2127 int len = 0;
2129 if (!tp_features.hotkey) {
2130 len += sprintf(p + len, "status:\t\tnot supported\n");
2131 return len;
2134 if (mutex_lock_interruptible(&hotkey_mutex))
2135 return -ERESTARTSYS;
2136 res = hotkey_status_get(&status);
2137 if (!res)
2138 res = hotkey_mask_get();
2139 mutex_unlock(&hotkey_mutex);
2140 if (res)
2141 return res;
2143 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2144 if (tp_features.hotkey_mask) {
2145 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2146 len += sprintf(p + len,
2147 "commands:\tenable, disable, reset, <mask>\n");
2148 } else {
2149 len += sprintf(p + len, "mask:\t\tnot supported\n");
2150 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2153 return len;
2156 static int hotkey_write(char *buf)
2158 int res, status;
2159 u32 mask;
2160 char *cmd;
2162 if (!tp_features.hotkey)
2163 return -ENODEV;
2165 if (mutex_lock_interruptible(&hotkey_mutex))
2166 return -ERESTARTSYS;
2168 status = -1;
2169 mask = hotkey_mask;
2171 res = 0;
2172 while ((cmd = next_cmd(&buf))) {
2173 if (strlencmp(cmd, "enable") == 0) {
2174 status = 1;
2175 } else if (strlencmp(cmd, "disable") == 0) {
2176 status = 0;
2177 } else if (strlencmp(cmd, "reset") == 0) {
2178 status = hotkey_orig_status;
2179 mask = hotkey_orig_mask;
2180 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2181 /* mask set */
2182 } else if (sscanf(cmd, "%x", &mask) == 1) {
2183 /* mask set */
2184 } else {
2185 res = -EINVAL;
2186 goto errexit;
2189 if (status != -1)
2190 res = hotkey_status_set(status);
2192 if (!res && mask != hotkey_mask)
2193 res = hotkey_mask_set(mask);
2195 errexit:
2196 mutex_unlock(&hotkey_mutex);
2197 return res;
2200 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2201 .hid = TPACPI_ACPI_HKEY_HID,
2202 .notify = hotkey_notify,
2203 .handle = &hkey_handle,
2204 .type = ACPI_DEVICE_NOTIFY,
2207 static struct ibm_struct hotkey_driver_data = {
2208 .name = "hotkey",
2209 .read = hotkey_read,
2210 .write = hotkey_write,
2211 .exit = hotkey_exit,
2212 .resume = hotkey_resume,
2213 .suspend = hotkey_suspend,
2214 .acpi = &ibm_hotkey_acpidriver,
2217 /*************************************************************************
2218 * Bluetooth subdriver
2221 enum {
2222 /* ACPI GBDC/SBDC bits */
2223 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2224 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2225 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2228 static int bluetooth_get_radiosw(void);
2229 static int bluetooth_set_radiosw(int radio_on);
2231 /* sysfs bluetooth enable ---------------------------------------------- */
2232 static ssize_t bluetooth_enable_show(struct device *dev,
2233 struct device_attribute *attr,
2234 char *buf)
2236 int status;
2238 status = bluetooth_get_radiosw();
2239 if (status < 0)
2240 return status;
2242 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2245 static ssize_t bluetooth_enable_store(struct device *dev,
2246 struct device_attribute *attr,
2247 const char *buf, size_t count)
2249 unsigned long t;
2250 int res;
2252 if (parse_strtoul(buf, 1, &t))
2253 return -EINVAL;
2255 res = bluetooth_set_radiosw(t);
2257 return (res) ? res : count;
2260 static struct device_attribute dev_attr_bluetooth_enable =
2261 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2262 bluetooth_enable_show, bluetooth_enable_store);
2264 /* --------------------------------------------------------------------- */
2266 static struct attribute *bluetooth_attributes[] = {
2267 &dev_attr_bluetooth_enable.attr,
2268 NULL
2271 static const struct attribute_group bluetooth_attr_group = {
2272 .attrs = bluetooth_attributes,
2275 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2277 int res;
2278 int status = 0;
2280 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2282 TPACPI_ACPIHANDLE_INIT(hkey);
2284 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2285 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2286 tp_features.bluetooth = hkey_handle &&
2287 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2289 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2290 str_supported(tp_features.bluetooth),
2291 status);
2293 if (tp_features.bluetooth) {
2294 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2295 /* no bluetooth hardware present in system */
2296 tp_features.bluetooth = 0;
2297 dbg_printk(TPACPI_DBG_INIT,
2298 "bluetooth hardware not installed\n");
2299 } else {
2300 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2301 &bluetooth_attr_group);
2302 if (res)
2303 return res;
2307 return (tp_features.bluetooth)? 0 : 1;
2310 static void bluetooth_exit(void)
2312 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2313 &bluetooth_attr_group);
2316 static int bluetooth_get_radiosw(void)
2318 int status;
2320 if (!tp_features.bluetooth)
2321 return -ENODEV;
2323 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2324 return -EIO;
2326 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2329 static int bluetooth_set_radiosw(int radio_on)
2331 int status;
2333 if (!tp_features.bluetooth)
2334 return -ENODEV;
2336 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2337 return -EIO;
2338 if (radio_on)
2339 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2340 else
2341 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2342 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2343 return -EIO;
2345 return 0;
2348 /* procfs -------------------------------------------------------------- */
2349 static int bluetooth_read(char *p)
2351 int len = 0;
2352 int status = bluetooth_get_radiosw();
2354 if (!tp_features.bluetooth)
2355 len += sprintf(p + len, "status:\t\tnot supported\n");
2356 else {
2357 len += sprintf(p + len, "status:\t\t%s\n",
2358 (status)? "enabled" : "disabled");
2359 len += sprintf(p + len, "commands:\tenable, disable\n");
2362 return len;
2365 static int bluetooth_write(char *buf)
2367 char *cmd;
2369 if (!tp_features.bluetooth)
2370 return -ENODEV;
2372 while ((cmd = next_cmd(&buf))) {
2373 if (strlencmp(cmd, "enable") == 0) {
2374 bluetooth_set_radiosw(1);
2375 } else if (strlencmp(cmd, "disable") == 0) {
2376 bluetooth_set_radiosw(0);
2377 } else
2378 return -EINVAL;
2381 return 0;
2384 static struct ibm_struct bluetooth_driver_data = {
2385 .name = "bluetooth",
2386 .read = bluetooth_read,
2387 .write = bluetooth_write,
2388 .exit = bluetooth_exit,
2391 /*************************************************************************
2392 * Wan subdriver
2395 enum {
2396 /* ACPI GWAN/SWAN bits */
2397 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2398 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2399 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2402 static int wan_get_radiosw(void);
2403 static int wan_set_radiosw(int radio_on);
2405 /* sysfs wan enable ---------------------------------------------------- */
2406 static ssize_t wan_enable_show(struct device *dev,
2407 struct device_attribute *attr,
2408 char *buf)
2410 int status;
2412 status = wan_get_radiosw();
2413 if (status < 0)
2414 return status;
2416 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2419 static ssize_t wan_enable_store(struct device *dev,
2420 struct device_attribute *attr,
2421 const char *buf, size_t count)
2423 unsigned long t;
2424 int res;
2426 if (parse_strtoul(buf, 1, &t))
2427 return -EINVAL;
2429 res = wan_set_radiosw(t);
2431 return (res) ? res : count;
2434 static struct device_attribute dev_attr_wan_enable =
2435 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2436 wan_enable_show, wan_enable_store);
2438 /* --------------------------------------------------------------------- */
2440 static struct attribute *wan_attributes[] = {
2441 &dev_attr_wan_enable.attr,
2442 NULL
2445 static const struct attribute_group wan_attr_group = {
2446 .attrs = wan_attributes,
2449 static int __init wan_init(struct ibm_init_struct *iibm)
2451 int res;
2452 int status = 0;
2454 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2456 TPACPI_ACPIHANDLE_INIT(hkey);
2458 tp_features.wan = hkey_handle &&
2459 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2461 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2462 str_supported(tp_features.wan),
2463 status);
2465 if (tp_features.wan) {
2466 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2467 /* no wan hardware present in system */
2468 tp_features.wan = 0;
2469 dbg_printk(TPACPI_DBG_INIT,
2470 "wan hardware not installed\n");
2471 } else {
2472 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2473 &wan_attr_group);
2474 if (res)
2475 return res;
2479 return (tp_features.wan)? 0 : 1;
2482 static void wan_exit(void)
2484 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2485 &wan_attr_group);
2488 static int wan_get_radiosw(void)
2490 int status;
2492 if (!tp_features.wan)
2493 return -ENODEV;
2495 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2496 return -EIO;
2498 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2501 static int wan_set_radiosw(int radio_on)
2503 int status;
2505 if (!tp_features.wan)
2506 return -ENODEV;
2508 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2509 return -EIO;
2510 if (radio_on)
2511 status |= TP_ACPI_WANCARD_RADIOSSW;
2512 else
2513 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2514 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2515 return -EIO;
2517 return 0;
2520 /* procfs -------------------------------------------------------------- */
2521 static int wan_read(char *p)
2523 int len = 0;
2524 int status = wan_get_radiosw();
2526 if (!tp_features.wan)
2527 len += sprintf(p + len, "status:\t\tnot supported\n");
2528 else {
2529 len += sprintf(p + len, "status:\t\t%s\n",
2530 (status)? "enabled" : "disabled");
2531 len += sprintf(p + len, "commands:\tenable, disable\n");
2534 return len;
2537 static int wan_write(char *buf)
2539 char *cmd;
2541 if (!tp_features.wan)
2542 return -ENODEV;
2544 while ((cmd = next_cmd(&buf))) {
2545 if (strlencmp(cmd, "enable") == 0) {
2546 wan_set_radiosw(1);
2547 } else if (strlencmp(cmd, "disable") == 0) {
2548 wan_set_radiosw(0);
2549 } else
2550 return -EINVAL;
2553 return 0;
2556 static struct ibm_struct wan_driver_data = {
2557 .name = "wan",
2558 .read = wan_read,
2559 .write = wan_write,
2560 .exit = wan_exit,
2561 .flags.experimental = 1,
2564 /*************************************************************************
2565 * Video subdriver
2568 enum video_access_mode {
2569 TPACPI_VIDEO_NONE = 0,
2570 TPACPI_VIDEO_570, /* 570 */
2571 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2572 TPACPI_VIDEO_NEW, /* all others */
2575 enum { /* video status flags, based on VIDEO_570 */
2576 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2577 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2578 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2581 enum { /* TPACPI_VIDEO_570 constants */
2582 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2583 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2584 * video_status_flags */
2585 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2586 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2589 static enum video_access_mode video_supported;
2590 static int video_orig_autosw;
2592 static int video_autosw_get(void);
2593 static int video_autosw_set(int enable);
2595 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2596 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2597 "\\_SB.PCI0.VID0", /* 770e */
2598 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2599 "\\_SB.PCI0.AGP.VID", /* all others */
2600 ); /* R30, R31 */
2602 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2604 static int __init video_init(struct ibm_init_struct *iibm)
2606 int ivga;
2608 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2610 TPACPI_ACPIHANDLE_INIT(vid);
2611 TPACPI_ACPIHANDLE_INIT(vid2);
2613 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2614 /* G41, assume IVGA doesn't change */
2615 vid_handle = vid2_handle;
2617 if (!vid_handle)
2618 /* video switching not supported on R30, R31 */
2619 video_supported = TPACPI_VIDEO_NONE;
2620 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2621 /* 570 */
2622 video_supported = TPACPI_VIDEO_570;
2623 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2624 /* 600e/x, 770e, 770x */
2625 video_supported = TPACPI_VIDEO_770;
2626 else
2627 /* all others */
2628 video_supported = TPACPI_VIDEO_NEW;
2630 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2631 str_supported(video_supported != TPACPI_VIDEO_NONE),
2632 video_supported);
2634 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2637 static void video_exit(void)
2639 dbg_printk(TPACPI_DBG_EXIT,
2640 "restoring original video autoswitch mode\n");
2641 if (video_autosw_set(video_orig_autosw))
2642 printk(TPACPI_ERR "error while trying to restore original "
2643 "video autoswitch mode\n");
2646 static int video_outputsw_get(void)
2648 int status = 0;
2649 int i;
2651 switch (video_supported) {
2652 case TPACPI_VIDEO_570:
2653 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2654 TP_ACPI_VIDEO_570_PHSCMD))
2655 return -EIO;
2656 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2657 break;
2658 case TPACPI_VIDEO_770:
2659 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2660 return -EIO;
2661 if (i)
2662 status |= TP_ACPI_VIDEO_S_LCD;
2663 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2664 return -EIO;
2665 if (i)
2666 status |= TP_ACPI_VIDEO_S_CRT;
2667 break;
2668 case TPACPI_VIDEO_NEW:
2669 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2670 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2671 return -EIO;
2672 if (i)
2673 status |= TP_ACPI_VIDEO_S_CRT;
2675 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2676 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2677 return -EIO;
2678 if (i)
2679 status |= TP_ACPI_VIDEO_S_LCD;
2680 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2681 return -EIO;
2682 if (i)
2683 status |= TP_ACPI_VIDEO_S_DVI;
2684 break;
2685 default:
2686 return -ENOSYS;
2689 return status;
2692 static int video_outputsw_set(int status)
2694 int autosw;
2695 int res = 0;
2697 switch (video_supported) {
2698 case TPACPI_VIDEO_570:
2699 res = acpi_evalf(NULL, NULL,
2700 "\\_SB.PHS2", "vdd",
2701 TP_ACPI_VIDEO_570_PHS2CMD,
2702 status | TP_ACPI_VIDEO_570_PHS2SET);
2703 break;
2704 case TPACPI_VIDEO_770:
2705 autosw = video_autosw_get();
2706 if (autosw < 0)
2707 return autosw;
2709 res = video_autosw_set(1);
2710 if (res)
2711 return res;
2712 res = acpi_evalf(vid_handle, NULL,
2713 "ASWT", "vdd", status * 0x100, 0);
2714 if (!autosw && video_autosw_set(autosw)) {
2715 printk(TPACPI_ERR
2716 "video auto-switch left enabled due to error\n");
2717 return -EIO;
2719 break;
2720 case TPACPI_VIDEO_NEW:
2721 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2722 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2723 break;
2724 default:
2725 return -ENOSYS;
2728 return (res)? 0 : -EIO;
2731 static int video_autosw_get(void)
2733 int autosw = 0;
2735 switch (video_supported) {
2736 case TPACPI_VIDEO_570:
2737 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2738 return -EIO;
2739 break;
2740 case TPACPI_VIDEO_770:
2741 case TPACPI_VIDEO_NEW:
2742 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2743 return -EIO;
2744 break;
2745 default:
2746 return -ENOSYS;
2749 return autosw & 1;
2752 static int video_autosw_set(int enable)
2754 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2755 return -EIO;
2756 return 0;
2759 static int video_outputsw_cycle(void)
2761 int autosw = video_autosw_get();
2762 int res;
2764 if (autosw < 0)
2765 return autosw;
2767 switch (video_supported) {
2768 case TPACPI_VIDEO_570:
2769 res = video_autosw_set(1);
2770 if (res)
2771 return res;
2772 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2773 break;
2774 case TPACPI_VIDEO_770:
2775 case TPACPI_VIDEO_NEW:
2776 res = video_autosw_set(1);
2777 if (res)
2778 return res;
2779 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2780 break;
2781 default:
2782 return -ENOSYS;
2784 if (!autosw && video_autosw_set(autosw)) {
2785 printk(TPACPI_ERR
2786 "video auto-switch left enabled due to error\n");
2787 return -EIO;
2790 return (res)? 0 : -EIO;
2793 static int video_expand_toggle(void)
2795 switch (video_supported) {
2796 case TPACPI_VIDEO_570:
2797 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2798 0 : -EIO;
2799 case TPACPI_VIDEO_770:
2800 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2801 0 : -EIO;
2802 case TPACPI_VIDEO_NEW:
2803 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2804 0 : -EIO;
2805 default:
2806 return -ENOSYS;
2808 /* not reached */
2811 static int video_read(char *p)
2813 int status, autosw;
2814 int len = 0;
2816 if (video_supported == TPACPI_VIDEO_NONE) {
2817 len += sprintf(p + len, "status:\t\tnot supported\n");
2818 return len;
2821 status = video_outputsw_get();
2822 if (status < 0)
2823 return status;
2825 autosw = video_autosw_get();
2826 if (autosw < 0)
2827 return autosw;
2829 len += sprintf(p + len, "status:\t\tsupported\n");
2830 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2831 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2832 if (video_supported == TPACPI_VIDEO_NEW)
2833 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2834 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2835 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2836 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2837 if (video_supported == TPACPI_VIDEO_NEW)
2838 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2839 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2840 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2842 return len;
2845 static int video_write(char *buf)
2847 char *cmd;
2848 int enable, disable, status;
2849 int res;
2851 if (video_supported == TPACPI_VIDEO_NONE)
2852 return -ENODEV;
2854 enable = 0;
2855 disable = 0;
2857 while ((cmd = next_cmd(&buf))) {
2858 if (strlencmp(cmd, "lcd_enable") == 0) {
2859 enable |= TP_ACPI_VIDEO_S_LCD;
2860 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2861 disable |= TP_ACPI_VIDEO_S_LCD;
2862 } else if (strlencmp(cmd, "crt_enable") == 0) {
2863 enable |= TP_ACPI_VIDEO_S_CRT;
2864 } else if (strlencmp(cmd, "crt_disable") == 0) {
2865 disable |= TP_ACPI_VIDEO_S_CRT;
2866 } else if (video_supported == TPACPI_VIDEO_NEW &&
2867 strlencmp(cmd, "dvi_enable") == 0) {
2868 enable |= TP_ACPI_VIDEO_S_DVI;
2869 } else if (video_supported == TPACPI_VIDEO_NEW &&
2870 strlencmp(cmd, "dvi_disable") == 0) {
2871 disable |= TP_ACPI_VIDEO_S_DVI;
2872 } else if (strlencmp(cmd, "auto_enable") == 0) {
2873 res = video_autosw_set(1);
2874 if (res)
2875 return res;
2876 } else if (strlencmp(cmd, "auto_disable") == 0) {
2877 res = video_autosw_set(0);
2878 if (res)
2879 return res;
2880 } else if (strlencmp(cmd, "video_switch") == 0) {
2881 res = video_outputsw_cycle();
2882 if (res)
2883 return res;
2884 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2885 res = video_expand_toggle();
2886 if (res)
2887 return res;
2888 } else
2889 return -EINVAL;
2892 if (enable || disable) {
2893 status = video_outputsw_get();
2894 if (status < 0)
2895 return status;
2896 res = video_outputsw_set((status & ~disable) | enable);
2897 if (res)
2898 return res;
2901 return 0;
2904 static struct ibm_struct video_driver_data = {
2905 .name = "video",
2906 .read = video_read,
2907 .write = video_write,
2908 .exit = video_exit,
2911 /*************************************************************************
2912 * Light (thinklight) subdriver
2915 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2916 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
2918 static int __init light_init(struct ibm_init_struct *iibm)
2920 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2922 TPACPI_ACPIHANDLE_INIT(ledb);
2923 TPACPI_ACPIHANDLE_INIT(lght);
2924 TPACPI_ACPIHANDLE_INIT(cmos);
2926 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2927 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2929 if (tp_features.light)
2930 /* light status not supported on
2931 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2932 tp_features.light_status =
2933 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2935 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2936 str_supported(tp_features.light));
2938 return (tp_features.light)? 0 : 1;
2941 static int light_read(char *p)
2943 int len = 0;
2944 int status = 0;
2946 if (!tp_features.light) {
2947 len += sprintf(p + len, "status:\t\tnot supported\n");
2948 } else if (!tp_features.light_status) {
2949 len += sprintf(p + len, "status:\t\tunknown\n");
2950 len += sprintf(p + len, "commands:\ton, off\n");
2951 } else {
2952 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2953 return -EIO;
2954 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2955 len += sprintf(p + len, "commands:\ton, off\n");
2958 return len;
2961 static int light_write(char *buf)
2963 int cmos_cmd, lght_cmd;
2964 char *cmd;
2965 int success;
2967 if (!tp_features.light)
2968 return -ENODEV;
2970 while ((cmd = next_cmd(&buf))) {
2971 if (strlencmp(cmd, "on") == 0) {
2972 cmos_cmd = 0x0c;
2973 lght_cmd = 1;
2974 } else if (strlencmp(cmd, "off") == 0) {
2975 cmos_cmd = 0x0d;
2976 lght_cmd = 0;
2977 } else
2978 return -EINVAL;
2980 success = cmos_handle ?
2981 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2982 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2983 if (!success)
2984 return -EIO;
2987 return 0;
2990 static struct ibm_struct light_driver_data = {
2991 .name = "light",
2992 .read = light_read,
2993 .write = light_write,
2996 /*************************************************************************
2997 * Dock subdriver
3000 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3002 static void dock_notify(struct ibm_struct *ibm, u32 event);
3003 static int dock_read(char *p);
3004 static int dock_write(char *buf);
3006 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3007 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3008 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3009 "\\_SB.PCI.ISA.SLCE", /* 570 */
3010 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3012 /* don't list other alternatives as we install a notify handler on the 570 */
3013 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3015 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3017 .notify = dock_notify,
3018 .handle = &dock_handle,
3019 .type = ACPI_SYSTEM_NOTIFY,
3022 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3023 * We just use it to get notifications of dock hotplug
3024 * in very old thinkpads */
3025 .hid = PCI_ROOT_HID_STRING,
3026 .notify = dock_notify,
3027 .handle = &pci_handle,
3028 .type = ACPI_SYSTEM_NOTIFY,
3032 static struct ibm_struct dock_driver_data[2] = {
3034 .name = "dock",
3035 .read = dock_read,
3036 .write = dock_write,
3037 .acpi = &ibm_dock_acpidriver[0],
3040 .name = "dock",
3041 .acpi = &ibm_dock_acpidriver[1],
3045 #define dock_docked() (_sta(dock_handle) & 1)
3047 static int __init dock_init(struct ibm_init_struct *iibm)
3049 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3051 TPACPI_ACPIHANDLE_INIT(dock);
3053 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3054 str_supported(dock_handle != NULL));
3056 return (dock_handle)? 0 : 1;
3059 static int __init dock_init2(struct ibm_init_struct *iibm)
3061 int dock2_needed;
3063 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3065 if (dock_driver_data[0].flags.acpi_driver_registered &&
3066 dock_driver_data[0].flags.acpi_notify_installed) {
3067 TPACPI_ACPIHANDLE_INIT(pci);
3068 dock2_needed = (pci_handle != NULL);
3069 vdbg_printk(TPACPI_DBG_INIT,
3070 "dock PCI handler for the TP 570 is %s\n",
3071 str_supported(dock2_needed));
3072 } else {
3073 vdbg_printk(TPACPI_DBG_INIT,
3074 "dock subdriver part 2 not required\n");
3075 dock2_needed = 0;
3078 return (dock2_needed)? 0 : 1;
3081 static void dock_notify(struct ibm_struct *ibm, u32 event)
3083 int docked = dock_docked();
3084 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
3086 if (event == 1 && !pci) /* 570 */
3087 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3088 else if (event == 1 && pci) /* 570 */
3089 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3090 else if (event == 3 && docked)
3091 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3092 else if (event == 3 && !docked)
3093 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
3094 else if (event == 0 && docked)
3095 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3096 else {
3097 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3098 event, _sta(dock_handle));
3099 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
3103 static int dock_read(char *p)
3105 int len = 0;
3106 int docked = dock_docked();
3108 if (!dock_handle)
3109 len += sprintf(p + len, "status:\t\tnot supported\n");
3110 else if (!docked)
3111 len += sprintf(p + len, "status:\t\tundocked\n");
3112 else {
3113 len += sprintf(p + len, "status:\t\tdocked\n");
3114 len += sprintf(p + len, "commands:\tdock, undock\n");
3117 return len;
3120 static int dock_write(char *buf)
3122 char *cmd;
3124 if (!dock_docked())
3125 return -ENODEV;
3127 while ((cmd = next_cmd(&buf))) {
3128 if (strlencmp(cmd, "undock") == 0) {
3129 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3130 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3131 return -EIO;
3132 } else if (strlencmp(cmd, "dock") == 0) {
3133 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3134 return -EIO;
3135 } else
3136 return -EINVAL;
3139 return 0;
3142 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3144 /*************************************************************************
3145 * Bay subdriver
3148 #ifdef CONFIG_THINKPAD_ACPI_BAY
3150 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3151 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3152 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3153 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3154 ); /* A21e, R30, R31 */
3155 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3156 "_EJ0", /* all others */
3157 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3158 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3159 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3160 ); /* all others */
3161 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3162 "_EJ0", /* 770x */
3163 ); /* all others */
3165 static int __init bay_init(struct ibm_init_struct *iibm)
3167 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3169 TPACPI_ACPIHANDLE_INIT(bay);
3170 if (bay_handle)
3171 TPACPI_ACPIHANDLE_INIT(bay_ej);
3172 TPACPI_ACPIHANDLE_INIT(bay2);
3173 if (bay2_handle)
3174 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3176 tp_features.bay_status = bay_handle &&
3177 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3178 tp_features.bay_status2 = bay2_handle &&
3179 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3181 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3182 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3183 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3184 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3186 vdbg_printk(TPACPI_DBG_INIT,
3187 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3188 str_supported(tp_features.bay_status),
3189 str_supported(tp_features.bay_eject),
3190 str_supported(tp_features.bay_status2),
3191 str_supported(tp_features.bay_eject2));
3193 return (tp_features.bay_status || tp_features.bay_eject ||
3194 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3197 static void bay_notify(struct ibm_struct *ibm, u32 event)
3199 acpi_bus_generate_event(ibm->acpi->device, event, 0);
3202 #define bay_occupied(b) (_sta(b##_handle) & 1)
3204 static int bay_read(char *p)
3206 int len = 0;
3207 int occupied = bay_occupied(bay);
3208 int occupied2 = bay_occupied(bay2);
3209 int eject, eject2;
3211 len += sprintf(p + len, "status:\t\t%s\n",
3212 tp_features.bay_status ?
3213 (occupied ? "occupied" : "unoccupied") :
3214 "not supported");
3215 if (tp_features.bay_status2)
3216 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3217 "occupied" : "unoccupied");
3219 eject = tp_features.bay_eject && occupied;
3220 eject2 = tp_features.bay_eject2 && occupied2;
3222 if (eject && eject2)
3223 len += sprintf(p + len, "commands:\teject, eject2\n");
3224 else if (eject)
3225 len += sprintf(p + len, "commands:\teject\n");
3226 else if (eject2)
3227 len += sprintf(p + len, "commands:\teject2\n");
3229 return len;
3232 static int bay_write(char *buf)
3234 char *cmd;
3236 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3237 return -ENODEV;
3239 while ((cmd = next_cmd(&buf))) {
3240 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3241 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3242 return -EIO;
3243 } else if (tp_features.bay_eject2 &&
3244 strlencmp(cmd, "eject2") == 0) {
3245 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3246 return -EIO;
3247 } else
3248 return -EINVAL;
3251 return 0;
3254 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3255 .notify = bay_notify,
3256 .handle = &bay_handle,
3257 .type = ACPI_SYSTEM_NOTIFY,
3260 static struct ibm_struct bay_driver_data = {
3261 .name = "bay",
3262 .read = bay_read,
3263 .write = bay_write,
3264 .acpi = &ibm_bay_acpidriver,
3267 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3269 /*************************************************************************
3270 * CMOS subdriver
3273 /* sysfs cmos_command -------------------------------------------------- */
3274 static ssize_t cmos_command_store(struct device *dev,
3275 struct device_attribute *attr,
3276 const char *buf, size_t count)
3278 unsigned long cmos_cmd;
3279 int res;
3281 if (parse_strtoul(buf, 21, &cmos_cmd))
3282 return -EINVAL;
3284 res = issue_thinkpad_cmos_command(cmos_cmd);
3285 return (res)? res : count;
3288 static struct device_attribute dev_attr_cmos_command =
3289 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3291 /* --------------------------------------------------------------------- */
3293 static int __init cmos_init(struct ibm_init_struct *iibm)
3295 int res;
3297 vdbg_printk(TPACPI_DBG_INIT,
3298 "initializing cmos commands subdriver\n");
3300 TPACPI_ACPIHANDLE_INIT(cmos);
3302 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3303 str_supported(cmos_handle != NULL));
3305 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3306 if (res)
3307 return res;
3309 return (cmos_handle)? 0 : 1;
3312 static void cmos_exit(void)
3314 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3317 static int cmos_read(char *p)
3319 int len = 0;
3321 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3322 R30, R31, T20-22, X20-21 */
3323 if (!cmos_handle)
3324 len += sprintf(p + len, "status:\t\tnot supported\n");
3325 else {
3326 len += sprintf(p + len, "status:\t\tsupported\n");
3327 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3330 return len;
3333 static int cmos_write(char *buf)
3335 char *cmd;
3336 int cmos_cmd, res;
3338 while ((cmd = next_cmd(&buf))) {
3339 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3340 cmos_cmd >= 0 && cmos_cmd <= 21) {
3341 /* cmos_cmd set */
3342 } else
3343 return -EINVAL;
3345 res = issue_thinkpad_cmos_command(cmos_cmd);
3346 if (res)
3347 return res;
3350 return 0;
3353 static struct ibm_struct cmos_driver_data = {
3354 .name = "cmos",
3355 .read = cmos_read,
3356 .write = cmos_write,
3357 .exit = cmos_exit,
3360 /*************************************************************************
3361 * LED subdriver
3364 enum led_access_mode {
3365 TPACPI_LED_NONE = 0,
3366 TPACPI_LED_570, /* 570 */
3367 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3368 TPACPI_LED_NEW, /* all others */
3371 enum { /* For TPACPI_LED_OLD */
3372 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3373 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3374 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3377 static enum led_access_mode led_supported;
3379 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3380 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3381 /* T20-22, X20-21 */
3382 "LED", /* all others */
3383 ); /* R30, R31 */
3385 static int __init led_init(struct ibm_init_struct *iibm)
3387 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3389 TPACPI_ACPIHANDLE_INIT(led);
3391 if (!led_handle)
3392 /* led not supported on R30, R31 */
3393 led_supported = TPACPI_LED_NONE;
3394 else if (strlencmp(led_path, "SLED") == 0)
3395 /* 570 */
3396 led_supported = TPACPI_LED_570;
3397 else if (strlencmp(led_path, "SYSL") == 0)
3398 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3399 led_supported = TPACPI_LED_OLD;
3400 else
3401 /* all others */
3402 led_supported = TPACPI_LED_NEW;
3404 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3405 str_supported(led_supported), led_supported);
3407 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3410 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3412 static int led_read(char *p)
3414 int len = 0;
3416 if (!led_supported) {
3417 len += sprintf(p + len, "status:\t\tnot supported\n");
3418 return len;
3420 len += sprintf(p + len, "status:\t\tsupported\n");
3422 if (led_supported == TPACPI_LED_570) {
3423 /* 570 */
3424 int i, status;
3425 for (i = 0; i < 8; i++) {
3426 if (!acpi_evalf(ec_handle,
3427 &status, "GLED", "dd", 1 << i))
3428 return -EIO;
3429 len += sprintf(p + len, "%d:\t\t%s\n",
3430 i, led_status(status));
3434 len += sprintf(p + len, "commands:\t"
3435 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3437 return len;
3440 /* off, on, blink */
3441 static const int led_sled_arg1[] = { 0, 1, 3 };
3442 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3443 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3444 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3446 static int led_write(char *buf)
3448 char *cmd;
3449 int led, ind, ret;
3451 if (!led_supported)
3452 return -ENODEV;
3454 while ((cmd = next_cmd(&buf))) {
3455 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3456 return -EINVAL;
3458 if (strstr(cmd, "off")) {
3459 ind = 0;
3460 } else if (strstr(cmd, "on")) {
3461 ind = 1;
3462 } else if (strstr(cmd, "blink")) {
3463 ind = 2;
3464 } else
3465 return -EINVAL;
3467 if (led_supported == TPACPI_LED_570) {
3468 /* 570 */
3469 led = 1 << led;
3470 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3471 led, led_sled_arg1[ind]))
3472 return -EIO;
3473 } else if (led_supported == TPACPI_LED_OLD) {
3474 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3475 led = 1 << led;
3476 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3477 if (ret >= 0)
3478 ret = ec_write(TPACPI_LED_EC_HLBL,
3479 led * led_exp_hlbl[ind]);
3480 if (ret >= 0)
3481 ret = ec_write(TPACPI_LED_EC_HLCL,
3482 led * led_exp_hlcl[ind]);
3483 if (ret < 0)
3484 return ret;
3485 } else {
3486 /* all others */
3487 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3488 led, led_led_arg1[ind]))
3489 return -EIO;
3493 return 0;
3496 static struct ibm_struct led_driver_data = {
3497 .name = "led",
3498 .read = led_read,
3499 .write = led_write,
3502 /*************************************************************************
3503 * Beep subdriver
3506 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3508 static int __init beep_init(struct ibm_init_struct *iibm)
3510 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3512 TPACPI_ACPIHANDLE_INIT(beep);
3514 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3515 str_supported(beep_handle != NULL));
3517 return (beep_handle)? 0 : 1;
3520 static int beep_read(char *p)
3522 int len = 0;
3524 if (!beep_handle)
3525 len += sprintf(p + len, "status:\t\tnot supported\n");
3526 else {
3527 len += sprintf(p + len, "status:\t\tsupported\n");
3528 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3531 return len;
3534 static int beep_write(char *buf)
3536 char *cmd;
3537 int beep_cmd;
3539 if (!beep_handle)
3540 return -ENODEV;
3542 while ((cmd = next_cmd(&buf))) {
3543 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3544 beep_cmd >= 0 && beep_cmd <= 17) {
3545 /* beep_cmd set */
3546 } else
3547 return -EINVAL;
3548 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3549 return -EIO;
3552 return 0;
3555 static struct ibm_struct beep_driver_data = {
3556 .name = "beep",
3557 .read = beep_read,
3558 .write = beep_write,
3561 /*************************************************************************
3562 * Thermal subdriver
3565 enum thermal_access_mode {
3566 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3567 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3568 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3569 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3570 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3573 enum { /* TPACPI_THERMAL_TPEC_* */
3574 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3575 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3576 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3579 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3580 struct ibm_thermal_sensors_struct {
3581 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3584 static enum thermal_access_mode thermal_read_mode;
3586 /* idx is zero-based */
3587 static int thermal_get_sensor(int idx, s32 *value)
3589 int t;
3590 s8 tmp;
3591 char tmpi[5];
3593 t = TP_EC_THERMAL_TMP0;
3595 switch (thermal_read_mode) {
3596 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3597 case TPACPI_THERMAL_TPEC_16:
3598 if (idx >= 8 && idx <= 15) {
3599 t = TP_EC_THERMAL_TMP8;
3600 idx -= 8;
3602 /* fallthrough */
3603 #endif
3604 case TPACPI_THERMAL_TPEC_8:
3605 if (idx <= 7) {
3606 if (!acpi_ec_read(t + idx, &tmp))
3607 return -EIO;
3608 *value = tmp * 1000;
3609 return 0;
3611 break;
3613 case TPACPI_THERMAL_ACPI_UPDT:
3614 if (idx <= 7) {
3615 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3616 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3617 return -EIO;
3618 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3619 return -EIO;
3620 *value = (t - 2732) * 100;
3621 return 0;
3623 break;
3625 case TPACPI_THERMAL_ACPI_TMP07:
3626 if (idx <= 7) {
3627 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3628 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3629 return -EIO;
3630 if (t > 127 || t < -127)
3631 t = TP_EC_THERMAL_TMP_NA;
3632 *value = t * 1000;
3633 return 0;
3635 break;
3637 case TPACPI_THERMAL_NONE:
3638 default:
3639 return -ENOSYS;
3642 return -EINVAL;
3645 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3647 int res, i;
3648 int n;
3650 n = 8;
3651 i = 0;
3653 if (!s)
3654 return -EINVAL;
3656 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3657 n = 16;
3659 for (i = 0 ; i < n; i++) {
3660 res = thermal_get_sensor(i, &s->temp[i]);
3661 if (res)
3662 return res;
3665 return n;
3668 /* sysfs temp##_input -------------------------------------------------- */
3670 static ssize_t thermal_temp_input_show(struct device *dev,
3671 struct device_attribute *attr,
3672 char *buf)
3674 struct sensor_device_attribute *sensor_attr =
3675 to_sensor_dev_attr(attr);
3676 int idx = sensor_attr->index;
3677 s32 value;
3678 int res;
3680 res = thermal_get_sensor(idx, &value);
3681 if (res)
3682 return res;
3683 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3684 return -ENXIO;
3686 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3689 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3690 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3691 thermal_temp_input_show, NULL, _idxB)
3693 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3694 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3695 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3696 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3697 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3698 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3699 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3700 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3701 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3702 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3703 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3704 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3705 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3706 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3707 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3708 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3709 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3712 #define THERMAL_ATTRS(X) \
3713 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3715 static struct attribute *thermal_temp_input_attr[] = {
3716 THERMAL_ATTRS(8),
3717 THERMAL_ATTRS(9),
3718 THERMAL_ATTRS(10),
3719 THERMAL_ATTRS(11),
3720 THERMAL_ATTRS(12),
3721 THERMAL_ATTRS(13),
3722 THERMAL_ATTRS(14),
3723 THERMAL_ATTRS(15),
3724 THERMAL_ATTRS(0),
3725 THERMAL_ATTRS(1),
3726 THERMAL_ATTRS(2),
3727 THERMAL_ATTRS(3),
3728 THERMAL_ATTRS(4),
3729 THERMAL_ATTRS(5),
3730 THERMAL_ATTRS(6),
3731 THERMAL_ATTRS(7),
3732 NULL
3735 static const struct attribute_group thermal_temp_input16_group = {
3736 .attrs = thermal_temp_input_attr
3739 static const struct attribute_group thermal_temp_input8_group = {
3740 .attrs = &thermal_temp_input_attr[8]
3743 #undef THERMAL_SENSOR_ATTR_TEMP
3744 #undef THERMAL_ATTRS
3746 /* --------------------------------------------------------------------- */
3748 static int __init thermal_init(struct ibm_init_struct *iibm)
3750 u8 t, ta1, ta2;
3751 int i;
3752 int acpi_tmp7;
3753 int res;
3755 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3757 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3759 if (thinkpad_id.ec_model) {
3761 * Direct EC access mode: sensors at registers
3762 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3763 * non-implemented, thermal sensors return 0x80 when
3764 * not available
3767 ta1 = ta2 = 0;
3768 for (i = 0; i < 8; i++) {
3769 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3770 ta1 |= t;
3771 } else {
3772 ta1 = 0;
3773 break;
3775 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3776 ta2 |= t;
3777 } else {
3778 ta1 = 0;
3779 break;
3782 if (ta1 == 0) {
3783 /* This is sheer paranoia, but we handle it anyway */
3784 if (acpi_tmp7) {
3785 printk(TPACPI_ERR
3786 "ThinkPad ACPI EC access misbehaving, "
3787 "falling back to ACPI TMPx access "
3788 "mode\n");
3789 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3790 } else {
3791 printk(TPACPI_ERR
3792 "ThinkPad ACPI EC access misbehaving, "
3793 "disabling thermal sensors access\n");
3794 thermal_read_mode = TPACPI_THERMAL_NONE;
3796 } else {
3797 thermal_read_mode =
3798 (ta2 != 0) ?
3799 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3801 } else if (acpi_tmp7) {
3802 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3803 /* 600e/x, 770e, 770x */
3804 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3805 } else {
3806 /* Standard ACPI TMPx access, max 8 sensors */
3807 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3809 } else {
3810 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3811 thermal_read_mode = TPACPI_THERMAL_NONE;
3814 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3815 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3816 thermal_read_mode);
3818 switch (thermal_read_mode) {
3819 case TPACPI_THERMAL_TPEC_16:
3820 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3821 &thermal_temp_input16_group);
3822 if (res)
3823 return res;
3824 break;
3825 case TPACPI_THERMAL_TPEC_8:
3826 case TPACPI_THERMAL_ACPI_TMP07:
3827 case TPACPI_THERMAL_ACPI_UPDT:
3828 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3829 &thermal_temp_input8_group);
3830 if (res)
3831 return res;
3832 break;
3833 case TPACPI_THERMAL_NONE:
3834 default:
3835 return 1;
3838 return 0;
3841 static void thermal_exit(void)
3843 switch (thermal_read_mode) {
3844 case TPACPI_THERMAL_TPEC_16:
3845 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3846 &thermal_temp_input16_group);
3847 break;
3848 case TPACPI_THERMAL_TPEC_8:
3849 case TPACPI_THERMAL_ACPI_TMP07:
3850 case TPACPI_THERMAL_ACPI_UPDT:
3851 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3852 &thermal_temp_input16_group);
3853 break;
3854 case TPACPI_THERMAL_NONE:
3855 default:
3856 break;
3860 static int thermal_read(char *p)
3862 int len = 0;
3863 int n, i;
3864 struct ibm_thermal_sensors_struct t;
3866 n = thermal_get_sensors(&t);
3867 if (unlikely(n < 0))
3868 return n;
3870 len += sprintf(p + len, "temperatures:\t");
3872 if (n > 0) {
3873 for (i = 0; i < (n - 1); i++)
3874 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3875 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3876 } else
3877 len += sprintf(p + len, "not supported\n");
3879 return len;
3882 static struct ibm_struct thermal_driver_data = {
3883 .name = "thermal",
3884 .read = thermal_read,
3885 .exit = thermal_exit,
3888 /*************************************************************************
3889 * EC Dump subdriver
3892 static u8 ecdump_regs[256];
3894 static int ecdump_read(char *p)
3896 int len = 0;
3897 int i, j;
3898 u8 v;
3900 len += sprintf(p + len, "EC "
3901 " +00 +01 +02 +03 +04 +05 +06 +07"
3902 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3903 for (i = 0; i < 256; i += 16) {
3904 len += sprintf(p + len, "EC 0x%02x:", i);
3905 for (j = 0; j < 16; j++) {
3906 if (!acpi_ec_read(i + j, &v))
3907 break;
3908 if (v != ecdump_regs[i + j])
3909 len += sprintf(p + len, " *%02x", v);
3910 else
3911 len += sprintf(p + len, " %02x", v);
3912 ecdump_regs[i + j] = v;
3914 len += sprintf(p + len, "\n");
3915 if (j != 16)
3916 break;
3919 /* These are way too dangerous to advertise openly... */
3920 #if 0
3921 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3922 " (<offset> is 00-ff, <value> is 00-ff)\n");
3923 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3924 " (<offset> is 00-ff, <value> is 0-255)\n");
3925 #endif
3926 return len;
3929 static int ecdump_write(char *buf)
3931 char *cmd;
3932 int i, v;
3934 while ((cmd = next_cmd(&buf))) {
3935 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3936 /* i and v set */
3937 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3938 /* i and v set */
3939 } else
3940 return -EINVAL;
3941 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3942 if (!acpi_ec_write(i, v))
3943 return -EIO;
3944 } else
3945 return -EINVAL;
3948 return 0;
3951 static struct ibm_struct ecdump_driver_data = {
3952 .name = "ecdump",
3953 .read = ecdump_read,
3954 .write = ecdump_write,
3955 .flags.experimental = 1,
3958 /*************************************************************************
3959 * Backlight/brightness subdriver
3962 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
3964 static struct backlight_device *ibm_backlight_device;
3965 static int brightness_offset = 0x31;
3966 static int brightness_mode;
3967 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
3969 static struct mutex brightness_mutex;
3972 * ThinkPads can read brightness from two places: EC 0x31, or
3973 * CMOS NVRAM byte 0x5E, bits 0-3.
3975 static int brightness_get(struct backlight_device *bd)
3977 u8 lec = 0, lcmos = 0, level = 0;
3979 if (brightness_mode & 1) {
3980 if (!acpi_ec_read(brightness_offset, &lec))
3981 return -EIO;
3982 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3983 level = lec;
3985 if (brightness_mode & 2) {
3986 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3987 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3988 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3989 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3990 level = lcmos;
3993 if (brightness_mode == 3 && lec != lcmos) {
3994 printk(TPACPI_ERR
3995 "CMOS NVRAM (%u) and EC (%u) do not agree "
3996 "on display brightness level\n",
3997 (unsigned int) lcmos,
3998 (unsigned int) lec);
3999 return -EIO;
4002 return level;
4005 /* May return EINTR which can always be mapped to ERESTARTSYS */
4006 static int brightness_set(int value)
4008 int cmos_cmd, inc, i, res;
4009 int current_value;
4011 if (value > ((tp_features.bright_16levels)? 15 : 7))
4012 return -EINVAL;
4014 res = mutex_lock_interruptible(&brightness_mutex);
4015 if (res < 0)
4016 return res;
4018 current_value = brightness_get(NULL);
4019 if (current_value < 0) {
4020 res = current_value;
4021 goto errout;
4024 cmos_cmd = value > current_value ?
4025 TP_CMOS_BRIGHTNESS_UP :
4026 TP_CMOS_BRIGHTNESS_DOWN;
4027 inc = (value > current_value)? 1 : -1;
4029 res = 0;
4030 for (i = current_value; i != value; i += inc) {
4031 if ((brightness_mode & 2) &&
4032 issue_thinkpad_cmos_command(cmos_cmd)) {
4033 res = -EIO;
4034 goto errout;
4036 if ((brightness_mode & 1) &&
4037 !acpi_ec_write(brightness_offset, i + inc)) {
4038 res = -EIO;
4039 goto errout;;
4043 errout:
4044 mutex_unlock(&brightness_mutex);
4045 return res;
4048 /* sysfs backlight class ----------------------------------------------- */
4050 static int brightness_update_status(struct backlight_device *bd)
4052 /* it is the backlight class's job (caller) to handle
4053 * EINTR and other errors properly */
4054 return brightness_set(
4055 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4056 bd->props.power == FB_BLANK_UNBLANK) ?
4057 bd->props.brightness : 0);
4060 static struct backlight_ops ibm_backlight_data = {
4061 .get_brightness = brightness_get,
4062 .update_status = brightness_update_status,
4065 /* --------------------------------------------------------------------- */
4067 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4069 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4070 union acpi_object *obj;
4071 int rc;
4073 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4074 obj = (union acpi_object *)buffer.pointer;
4075 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4076 printk(TPACPI_ERR "Unknown BCLL data, "
4077 "please report this to %s\n", TPACPI_MAIL);
4078 rc = 0;
4079 } else {
4080 rc = obj->package.count;
4082 } else {
4083 return 0;
4086 kfree(buffer.pointer);
4087 return rc;
4090 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4091 void *context, void **rv)
4093 char name[ACPI_PATH_SEGMENT_LENGTH];
4094 struct acpi_buffer buffer = { sizeof(name), &name };
4096 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4097 !strncmp("BCLL", name, sizeof(name) - 1)) {
4098 if (tpacpi_query_bcll_levels(handle) == 16) {
4099 *rv = handle;
4100 return AE_CTRL_TERMINATE;
4101 } else {
4102 return AE_OK;
4104 } else {
4105 return AE_OK;
4109 static int __init brightness_check_levels(void)
4111 int status;
4112 void *found_node = NULL;
4114 if (!vid_handle) {
4115 TPACPI_ACPIHANDLE_INIT(vid);
4117 if (!vid_handle)
4118 return 0;
4120 /* Search for a BCLL package with 16 levels */
4121 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4122 brightness_find_bcll, NULL,
4123 &found_node);
4125 return (ACPI_SUCCESS(status) && found_node != NULL);
4128 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4129 void *context, void **rv)
4131 char name[ACPI_PATH_SEGMENT_LENGTH];
4132 struct acpi_buffer buffer = { sizeof(name), &name };
4134 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4135 !strncmp("_BCL", name, sizeof(name) - 1)) {
4136 *rv = handle;
4137 return AE_CTRL_TERMINATE;
4138 } else {
4139 return AE_OK;
4143 static int __init brightness_check_std_acpi_support(void)
4145 int status;
4146 void *found_node = NULL;
4148 if (!vid_handle) {
4149 TPACPI_ACPIHANDLE_INIT(vid);
4151 if (!vid_handle)
4152 return 0;
4154 /* Search for a _BCL method, but don't execute it */
4155 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4156 brightness_find_bcl, NULL, &found_node);
4158 return (ACPI_SUCCESS(status) && found_node != NULL);
4161 static int __init brightness_init(struct ibm_init_struct *iibm)
4163 int b;
4165 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4167 mutex_init(&brightness_mutex);
4169 if (!brightness_enable) {
4170 dbg_printk(TPACPI_DBG_INIT,
4171 "brightness support disabled by module parameter\n");
4172 return 1;
4173 } else if (brightness_enable > 1) {
4174 if (brightness_check_std_acpi_support()) {
4175 printk(TPACPI_NOTICE
4176 "standard ACPI backlight interface "
4177 "available, not loading native one...\n");
4178 return 1;
4182 if (!brightness_mode) {
4183 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4184 brightness_mode = 2;
4185 else
4186 brightness_mode = 3;
4188 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4189 brightness_mode);
4192 if (brightness_mode > 3)
4193 return -EINVAL;
4195 tp_features.bright_16levels =
4196 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4197 brightness_check_levels();
4199 b = brightness_get(NULL);
4200 if (b < 0)
4201 return 1;
4203 if (tp_features.bright_16levels)
4204 printk(TPACPI_INFO
4205 "detected a 16-level brightness capable ThinkPad\n");
4207 ibm_backlight_device = backlight_device_register(
4208 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4209 &ibm_backlight_data);
4210 if (IS_ERR(ibm_backlight_device)) {
4211 printk(TPACPI_ERR "Could not register backlight device\n");
4212 return PTR_ERR(ibm_backlight_device);
4214 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4216 ibm_backlight_device->props.max_brightness =
4217 (tp_features.bright_16levels)? 15 : 7;
4218 ibm_backlight_device->props.brightness = b;
4219 backlight_update_status(ibm_backlight_device);
4221 return 0;
4224 static void brightness_exit(void)
4226 if (ibm_backlight_device) {
4227 vdbg_printk(TPACPI_DBG_EXIT,
4228 "calling backlight_device_unregister()\n");
4229 backlight_device_unregister(ibm_backlight_device);
4230 ibm_backlight_device = NULL;
4234 static int brightness_read(char *p)
4236 int len = 0;
4237 int level;
4239 level = brightness_get(NULL);
4240 if (level < 0) {
4241 len += sprintf(p + len, "level:\t\tunreadable\n");
4242 } else {
4243 len += sprintf(p + len, "level:\t\t%d\n", level);
4244 len += sprintf(p + len, "commands:\tup, down\n");
4245 len += sprintf(p + len, "commands:\tlevel <level>"
4246 " (<level> is 0-%d)\n",
4247 (tp_features.bright_16levels) ? 15 : 7);
4250 return len;
4253 static int brightness_write(char *buf)
4255 int level;
4256 int rc;
4257 char *cmd;
4258 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4260 level = brightness_get(NULL);
4261 if (level < 0)
4262 return level;
4264 while ((cmd = next_cmd(&buf))) {
4265 if (strlencmp(cmd, "up") == 0) {
4266 if (level < max_level)
4267 level++;
4268 } else if (strlencmp(cmd, "down") == 0) {
4269 if (level > 0)
4270 level--;
4271 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4272 level >= 0 && level <= max_level) {
4273 /* new level set */
4274 } else
4275 return -EINVAL;
4279 * Now we know what the final level should be, so we try to set it.
4280 * Doing it this way makes the syscall restartable in case of EINTR
4282 rc = brightness_set(level);
4283 return (rc == -EINTR)? ERESTARTSYS : rc;
4286 static struct ibm_struct brightness_driver_data = {
4287 .name = "brightness",
4288 .read = brightness_read,
4289 .write = brightness_write,
4290 .exit = brightness_exit,
4293 /*************************************************************************
4294 * Volume subdriver
4297 static int volume_offset = 0x30;
4299 static int volume_read(char *p)
4301 int len = 0;
4302 u8 level;
4304 if (!acpi_ec_read(volume_offset, &level)) {
4305 len += sprintf(p + len, "level:\t\tunreadable\n");
4306 } else {
4307 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4308 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4309 len += sprintf(p + len, "commands:\tup, down, mute\n");
4310 len += sprintf(p + len, "commands:\tlevel <level>"
4311 " (<level> is 0-15)\n");
4314 return len;
4317 static int volume_write(char *buf)
4319 int cmos_cmd, inc, i;
4320 u8 level, mute;
4321 int new_level, new_mute;
4322 char *cmd;
4324 while ((cmd = next_cmd(&buf))) {
4325 if (!acpi_ec_read(volume_offset, &level))
4326 return -EIO;
4327 new_mute = mute = level & 0x40;
4328 new_level = level = level & 0xf;
4330 if (strlencmp(cmd, "up") == 0) {
4331 if (mute)
4332 new_mute = 0;
4333 else
4334 new_level = level == 15 ? 15 : level + 1;
4335 } else if (strlencmp(cmd, "down") == 0) {
4336 if (mute)
4337 new_mute = 0;
4338 else
4339 new_level = level == 0 ? 0 : level - 1;
4340 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4341 new_level >= 0 && new_level <= 15) {
4342 /* new_level set */
4343 } else if (strlencmp(cmd, "mute") == 0) {
4344 new_mute = 0x40;
4345 } else
4346 return -EINVAL;
4348 if (new_level != level) { /* mute doesn't change */
4349 cmos_cmd = (new_level > level) ?
4350 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4351 inc = new_level > level ? 1 : -1;
4353 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4354 !acpi_ec_write(volume_offset, level)))
4355 return -EIO;
4357 for (i = level; i != new_level; i += inc)
4358 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4359 !acpi_ec_write(volume_offset, i + inc))
4360 return -EIO;
4362 if (mute &&
4363 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4364 !acpi_ec_write(volume_offset, new_level + mute))) {
4365 return -EIO;
4369 if (new_mute != mute) { /* level doesn't change */
4370 cmos_cmd = (new_mute) ?
4371 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4373 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4374 !acpi_ec_write(volume_offset, level + new_mute))
4375 return -EIO;
4379 return 0;
4382 static struct ibm_struct volume_driver_data = {
4383 .name = "volume",
4384 .read = volume_read,
4385 .write = volume_write,
4388 /*************************************************************************
4389 * Fan subdriver
4393 * FAN ACCESS MODES
4395 * TPACPI_FAN_RD_ACPI_GFAN:
4396 * ACPI GFAN method: returns fan level
4398 * see TPACPI_FAN_WR_ACPI_SFAN
4399 * EC 0x2f (HFSP) not available if GFAN exists
4401 * TPACPI_FAN_WR_ACPI_SFAN:
4402 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4404 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4405 * it for writing.
4407 * TPACPI_FAN_WR_TPEC:
4408 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4409 * Supported on almost all ThinkPads
4411 * Fan speed changes of any sort (including those caused by the
4412 * disengaged mode) are usually done slowly by the firmware as the
4413 * maximum ammount of fan duty cycle change per second seems to be
4414 * limited.
4416 * Reading is not available if GFAN exists.
4417 * Writing is not available if SFAN exists.
4419 * Bits
4420 * 7 automatic mode engaged;
4421 * (default operation mode of the ThinkPad)
4422 * fan level is ignored in this mode.
4423 * 6 full speed mode (takes precedence over bit 7);
4424 * not available on all thinkpads. May disable
4425 * the tachometer while the fan controller ramps up
4426 * the speed (which can take up to a few *minutes*).
4427 * Speeds up fan to 100% duty-cycle, which is far above
4428 * the standard RPM levels. It is not impossible that
4429 * it could cause hardware damage.
4430 * 5-3 unused in some models. Extra bits for fan level
4431 * in others, but still useless as all values above
4432 * 7 map to the same speed as level 7 in these models.
4433 * 2-0 fan level (0..7 usually)
4434 * 0x00 = stop
4435 * 0x07 = max (set when temperatures critical)
4436 * Some ThinkPads may have other levels, see
4437 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4439 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4440 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4441 * does so, its initial value is meaningless (0x07).
4443 * For firmware bugs, refer to:
4444 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4446 * ----
4448 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4449 * Main fan tachometer reading (in RPM)
4451 * This register is present on all ThinkPads with a new-style EC, and
4452 * it is known not to be present on the A21m/e, and T22, as there is
4453 * something else in offset 0x84 according to the ACPI DSDT. Other
4454 * ThinkPads from this same time period (and earlier) probably lack the
4455 * tachometer as well.
4457 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4458 * was never fixed by IBM to report the EC firmware version string
4459 * probably support the tachometer (like the early X models), so
4460 * detecting it is quite hard. We need more data to know for sure.
4462 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4463 * might result.
4465 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4466 * mode.
4468 * For firmware bugs, refer to:
4469 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4471 * TPACPI_FAN_WR_ACPI_FANS:
4472 * ThinkPad X31, X40, X41. Not available in the X60.
4474 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4475 * high speed. ACPI DSDT seems to map these three speeds to levels
4476 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4477 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4479 * The speeds are stored on handles
4480 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4482 * There are three default speed sets, acessible as handles:
4483 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4485 * ACPI DSDT switches which set is in use depending on various
4486 * factors.
4488 * TPACPI_FAN_WR_TPEC is also available and should be used to
4489 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4490 * but the ACPI tables just mention level 7.
4493 enum { /* Fan control constants */
4494 fan_status_offset = 0x2f, /* EC register 0x2f */
4495 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4496 * 0x84 must be read before 0x85 */
4498 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4499 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4501 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4504 enum fan_status_access_mode {
4505 TPACPI_FAN_NONE = 0, /* No fan status or control */
4506 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4507 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4510 enum fan_control_access_mode {
4511 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4512 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4513 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4514 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4517 enum fan_control_commands {
4518 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4519 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4520 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4521 * and also watchdog cmd */
4524 static int fan_control_allowed;
4526 static enum fan_status_access_mode fan_status_access_mode;
4527 static enum fan_control_access_mode fan_control_access_mode;
4528 static enum fan_control_commands fan_control_commands;
4530 static u8 fan_control_initial_status;
4531 static u8 fan_control_desired_level;
4532 static int fan_watchdog_maxinterval;
4534 static struct mutex fan_mutex;
4536 static void fan_watchdog_fire(struct work_struct *ignored);
4537 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4539 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4540 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4541 "\\FSPD", /* 600e/x, 770e, 770x */
4542 ); /* all others */
4543 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4544 "JFNS", /* 770x-JL */
4545 ); /* all others */
4548 * Call with fan_mutex held
4550 static void fan_update_desired_level(u8 status)
4552 if ((status &
4553 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4554 if (status > 7)
4555 fan_control_desired_level = 7;
4556 else
4557 fan_control_desired_level = status;
4561 static int fan_get_status(u8 *status)
4563 u8 s;
4565 /* TODO:
4566 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4568 switch (fan_status_access_mode) {
4569 case TPACPI_FAN_RD_ACPI_GFAN:
4570 /* 570, 600e/x, 770e, 770x */
4572 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4573 return -EIO;
4575 if (likely(status))
4576 *status = s & 0x07;
4578 break;
4580 case TPACPI_FAN_RD_TPEC:
4581 /* all except 570, 600e/x, 770e, 770x */
4582 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4583 return -EIO;
4585 if (likely(status))
4586 *status = s;
4588 break;
4590 default:
4591 return -ENXIO;
4594 return 0;
4597 static int fan_get_status_safe(u8 *status)
4599 int rc;
4600 u8 s;
4602 if (mutex_lock_interruptible(&fan_mutex))
4603 return -ERESTARTSYS;
4604 rc = fan_get_status(&s);
4605 if (!rc)
4606 fan_update_desired_level(s);
4607 mutex_unlock(&fan_mutex);
4609 if (status)
4610 *status = s;
4612 return rc;
4615 static int fan_get_speed(unsigned int *speed)
4617 u8 hi, lo;
4619 switch (fan_status_access_mode) {
4620 case TPACPI_FAN_RD_TPEC:
4621 /* all except 570, 600e/x, 770e, 770x */
4622 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4623 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4624 return -EIO;
4626 if (likely(speed))
4627 *speed = (hi << 8) | lo;
4629 break;
4631 default:
4632 return -ENXIO;
4635 return 0;
4638 static int fan_set_level(int level)
4640 if (!fan_control_allowed)
4641 return -EPERM;
4643 switch (fan_control_access_mode) {
4644 case TPACPI_FAN_WR_ACPI_SFAN:
4645 if (level >= 0 && level <= 7) {
4646 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4647 return -EIO;
4648 } else
4649 return -EINVAL;
4650 break;
4652 case TPACPI_FAN_WR_ACPI_FANS:
4653 case TPACPI_FAN_WR_TPEC:
4654 if ((level != TP_EC_FAN_AUTO) &&
4655 (level != TP_EC_FAN_FULLSPEED) &&
4656 ((level < 0) || (level > 7)))
4657 return -EINVAL;
4659 /* safety net should the EC not support AUTO
4660 * or FULLSPEED mode bits and just ignore them */
4661 if (level & TP_EC_FAN_FULLSPEED)
4662 level |= 7; /* safety min speed 7 */
4663 else if (level & TP_EC_FAN_FULLSPEED)
4664 level |= 4; /* safety min speed 4 */
4666 if (!acpi_ec_write(fan_status_offset, level))
4667 return -EIO;
4668 else
4669 tp_features.fan_ctrl_status_undef = 0;
4670 break;
4672 default:
4673 return -ENXIO;
4675 return 0;
4678 static int fan_set_level_safe(int level)
4680 int rc;
4682 if (!fan_control_allowed)
4683 return -EPERM;
4685 if (mutex_lock_interruptible(&fan_mutex))
4686 return -ERESTARTSYS;
4688 if (level == TPACPI_FAN_LAST_LEVEL)
4689 level = fan_control_desired_level;
4691 rc = fan_set_level(level);
4692 if (!rc)
4693 fan_update_desired_level(level);
4695 mutex_unlock(&fan_mutex);
4696 return rc;
4699 static int fan_set_enable(void)
4701 u8 s;
4702 int rc;
4704 if (!fan_control_allowed)
4705 return -EPERM;
4707 if (mutex_lock_interruptible(&fan_mutex))
4708 return -ERESTARTSYS;
4710 switch (fan_control_access_mode) {
4711 case TPACPI_FAN_WR_ACPI_FANS:
4712 case TPACPI_FAN_WR_TPEC:
4713 rc = fan_get_status(&s);
4714 if (rc < 0)
4715 break;
4717 /* Don't go out of emergency fan mode */
4718 if (s != 7) {
4719 s &= 0x07;
4720 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4723 if (!acpi_ec_write(fan_status_offset, s))
4724 rc = -EIO;
4725 else {
4726 tp_features.fan_ctrl_status_undef = 0;
4727 rc = 0;
4729 break;
4731 case TPACPI_FAN_WR_ACPI_SFAN:
4732 rc = fan_get_status(&s);
4733 if (rc < 0)
4734 break;
4736 s &= 0x07;
4738 /* Set fan to at least level 4 */
4739 s |= 4;
4741 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4742 rc = -EIO;
4743 else
4744 rc = 0;
4745 break;
4747 default:
4748 rc = -ENXIO;
4751 mutex_unlock(&fan_mutex);
4752 return rc;
4755 static int fan_set_disable(void)
4757 int rc;
4759 if (!fan_control_allowed)
4760 return -EPERM;
4762 if (mutex_lock_interruptible(&fan_mutex))
4763 return -ERESTARTSYS;
4765 rc = 0;
4766 switch (fan_control_access_mode) {
4767 case TPACPI_FAN_WR_ACPI_FANS:
4768 case TPACPI_FAN_WR_TPEC:
4769 if (!acpi_ec_write(fan_status_offset, 0x00))
4770 rc = -EIO;
4771 else {
4772 fan_control_desired_level = 0;
4773 tp_features.fan_ctrl_status_undef = 0;
4775 break;
4777 case TPACPI_FAN_WR_ACPI_SFAN:
4778 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4779 rc = -EIO;
4780 else
4781 fan_control_desired_level = 0;
4782 break;
4784 default:
4785 rc = -ENXIO;
4789 mutex_unlock(&fan_mutex);
4790 return rc;
4793 static int fan_set_speed(int speed)
4795 int rc;
4797 if (!fan_control_allowed)
4798 return -EPERM;
4800 if (mutex_lock_interruptible(&fan_mutex))
4801 return -ERESTARTSYS;
4803 rc = 0;
4804 switch (fan_control_access_mode) {
4805 case TPACPI_FAN_WR_ACPI_FANS:
4806 if (speed >= 0 && speed <= 65535) {
4807 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4808 speed, speed, speed))
4809 rc = -EIO;
4810 } else
4811 rc = -EINVAL;
4812 break;
4814 default:
4815 rc = -ENXIO;
4818 mutex_unlock(&fan_mutex);
4819 return rc;
4822 static void fan_watchdog_reset(void)
4824 static int fan_watchdog_active;
4826 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4827 return;
4829 if (fan_watchdog_active)
4830 cancel_delayed_work(&fan_watchdog_task);
4832 if (fan_watchdog_maxinterval > 0 &&
4833 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4834 fan_watchdog_active = 1;
4835 if (!schedule_delayed_work(&fan_watchdog_task,
4836 msecs_to_jiffies(fan_watchdog_maxinterval
4837 * 1000))) {
4838 printk(TPACPI_ERR
4839 "failed to schedule the fan watchdog, "
4840 "watchdog will not trigger\n");
4842 } else
4843 fan_watchdog_active = 0;
4846 static void fan_watchdog_fire(struct work_struct *ignored)
4848 int rc;
4850 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4851 return;
4853 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4854 rc = fan_set_enable();
4855 if (rc < 0) {
4856 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4857 "will try again later...\n", -rc);
4858 /* reschedule for later */
4859 fan_watchdog_reset();
4864 * SYSFS fan layout: hwmon compatible (device)
4866 * pwm*_enable:
4867 * 0: "disengaged" mode
4868 * 1: manual mode
4869 * 2: native EC "auto" mode (recommended, hardware default)
4871 * pwm*: set speed in manual mode, ignored otherwise.
4872 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4873 * interpolation.
4875 * fan*_input: tachometer reading, RPM
4878 * SYSFS fan layout: extensions
4880 * fan_watchdog (driver):
4881 * fan watchdog interval in seconds, 0 disables (default), max 120
4884 /* sysfs fan pwm1_enable ----------------------------------------------- */
4885 static ssize_t fan_pwm1_enable_show(struct device *dev,
4886 struct device_attribute *attr,
4887 char *buf)
4889 int res, mode;
4890 u8 status;
4892 res = fan_get_status_safe(&status);
4893 if (res)
4894 return res;
4896 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4897 if (status != fan_control_initial_status) {
4898 tp_features.fan_ctrl_status_undef = 0;
4899 } else {
4900 /* Return most likely status. In fact, it
4901 * might be the only possible status */
4902 status = TP_EC_FAN_AUTO;
4906 if (status & TP_EC_FAN_FULLSPEED) {
4907 mode = 0;
4908 } else if (status & TP_EC_FAN_AUTO) {
4909 mode = 2;
4910 } else
4911 mode = 1;
4913 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4916 static ssize_t fan_pwm1_enable_store(struct device *dev,
4917 struct device_attribute *attr,
4918 const char *buf, size_t count)
4920 unsigned long t;
4921 int res, level;
4923 if (parse_strtoul(buf, 2, &t))
4924 return -EINVAL;
4926 switch (t) {
4927 case 0:
4928 level = TP_EC_FAN_FULLSPEED;
4929 break;
4930 case 1:
4931 level = TPACPI_FAN_LAST_LEVEL;
4932 break;
4933 case 2:
4934 level = TP_EC_FAN_AUTO;
4935 break;
4936 case 3:
4937 /* reserved for software-controlled auto mode */
4938 return -ENOSYS;
4939 default:
4940 return -EINVAL;
4943 res = fan_set_level_safe(level);
4944 if (res == -ENXIO)
4945 return -EINVAL;
4946 else if (res < 0)
4947 return res;
4949 fan_watchdog_reset();
4951 return count;
4954 static struct device_attribute dev_attr_fan_pwm1_enable =
4955 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4956 fan_pwm1_enable_show, fan_pwm1_enable_store);
4958 /* sysfs fan pwm1 ------------------------------------------------------ */
4959 static ssize_t fan_pwm1_show(struct device *dev,
4960 struct device_attribute *attr,
4961 char *buf)
4963 int res;
4964 u8 status;
4966 res = fan_get_status_safe(&status);
4967 if (res)
4968 return res;
4970 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4971 if (status != fan_control_initial_status) {
4972 tp_features.fan_ctrl_status_undef = 0;
4973 } else {
4974 status = TP_EC_FAN_AUTO;
4978 if ((status &
4979 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4980 status = fan_control_desired_level;
4982 if (status > 7)
4983 status = 7;
4985 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4988 static ssize_t fan_pwm1_store(struct device *dev,
4989 struct device_attribute *attr,
4990 const char *buf, size_t count)
4992 unsigned long s;
4993 int rc;
4994 u8 status, newlevel;
4996 if (parse_strtoul(buf, 255, &s))
4997 return -EINVAL;
4999 /* scale down from 0-255 to 0-7 */
5000 newlevel = (s >> 5) & 0x07;
5002 if (mutex_lock_interruptible(&fan_mutex))
5003 return -ERESTARTSYS;
5005 rc = fan_get_status(&status);
5006 if (!rc && (status &
5007 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5008 rc = fan_set_level(newlevel);
5009 if (rc == -ENXIO)
5010 rc = -EINVAL;
5011 else if (!rc) {
5012 fan_update_desired_level(newlevel);
5013 fan_watchdog_reset();
5017 mutex_unlock(&fan_mutex);
5018 return (rc)? rc : count;
5021 static struct device_attribute dev_attr_fan_pwm1 =
5022 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5023 fan_pwm1_show, fan_pwm1_store);
5025 /* sysfs fan fan1_input ------------------------------------------------ */
5026 static ssize_t fan_fan1_input_show(struct device *dev,
5027 struct device_attribute *attr,
5028 char *buf)
5030 int res;
5031 unsigned int speed;
5033 res = fan_get_speed(&speed);
5034 if (res < 0)
5035 return res;
5037 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5040 static struct device_attribute dev_attr_fan_fan1_input =
5041 __ATTR(fan1_input, S_IRUGO,
5042 fan_fan1_input_show, NULL);
5044 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5045 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5046 char *buf)
5048 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5051 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5052 const char *buf, size_t count)
5054 unsigned long t;
5056 if (parse_strtoul(buf, 120, &t))
5057 return -EINVAL;
5059 if (!fan_control_allowed)
5060 return -EPERM;
5062 fan_watchdog_maxinterval = t;
5063 fan_watchdog_reset();
5065 return count;
5068 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5069 fan_fan_watchdog_show, fan_fan_watchdog_store);
5071 /* --------------------------------------------------------------------- */
5072 static struct attribute *fan_attributes[] = {
5073 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5074 &dev_attr_fan_fan1_input.attr,
5075 NULL
5078 static const struct attribute_group fan_attr_group = {
5079 .attrs = fan_attributes,
5082 static int __init fan_init(struct ibm_init_struct *iibm)
5084 int rc;
5086 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5088 mutex_init(&fan_mutex);
5089 fan_status_access_mode = TPACPI_FAN_NONE;
5090 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5091 fan_control_commands = 0;
5092 fan_watchdog_maxinterval = 0;
5093 tp_features.fan_ctrl_status_undef = 0;
5094 fan_control_desired_level = 7;
5096 TPACPI_ACPIHANDLE_INIT(fans);
5097 TPACPI_ACPIHANDLE_INIT(gfan);
5098 TPACPI_ACPIHANDLE_INIT(sfan);
5100 if (gfan_handle) {
5101 /* 570, 600e/x, 770e, 770x */
5102 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5103 } else {
5104 /* all other ThinkPads: note that even old-style
5105 * ThinkPad ECs supports the fan control register */
5106 if (likely(acpi_ec_read(fan_status_offset,
5107 &fan_control_initial_status))) {
5108 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5110 /* In some ThinkPads, neither the EC nor the ACPI
5111 * DSDT initialize the fan status, and it ends up
5112 * being set to 0x07 when it *could* be either
5113 * 0x07 or 0x80.
5115 * Enable for TP-1Y (T43), TP-78 (R51e),
5116 * TP-76 (R52), TP-70 (T43, R52), which are known
5117 * to be buggy. */
5118 if (fan_control_initial_status == 0x07) {
5119 switch (thinkpad_id.ec_model) {
5120 case 0x5931: /* TP-1Y */
5121 case 0x3837: /* TP-78 */
5122 case 0x3637: /* TP-76 */
5123 case 0x3037: /* TP-70 */
5124 printk(TPACPI_NOTICE
5125 "fan_init: initial fan status "
5126 "is unknown, assuming it is "
5127 "in auto mode\n");
5128 tp_features.fan_ctrl_status_undef = 1;
5132 } else {
5133 printk(TPACPI_ERR
5134 "ThinkPad ACPI EC access misbehaving, "
5135 "fan status and control unavailable\n");
5136 return 1;
5140 if (sfan_handle) {
5141 /* 570, 770x-JL */
5142 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5143 fan_control_commands |=
5144 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5145 } else {
5146 if (!gfan_handle) {
5147 /* gfan without sfan means no fan control */
5148 /* all other models implement TP EC 0x2f control */
5150 if (fans_handle) {
5151 /* X31, X40, X41 */
5152 fan_control_access_mode =
5153 TPACPI_FAN_WR_ACPI_FANS;
5154 fan_control_commands |=
5155 TPACPI_FAN_CMD_SPEED |
5156 TPACPI_FAN_CMD_LEVEL |
5157 TPACPI_FAN_CMD_ENABLE;
5158 } else {
5159 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5160 fan_control_commands |=
5161 TPACPI_FAN_CMD_LEVEL |
5162 TPACPI_FAN_CMD_ENABLE;
5167 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5168 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5169 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5170 fan_status_access_mode, fan_control_access_mode);
5172 /* fan control master switch */
5173 if (!fan_control_allowed) {
5174 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5175 fan_control_commands = 0;
5176 dbg_printk(TPACPI_DBG_INIT,
5177 "fan control features disabled by parameter\n");
5180 /* update fan_control_desired_level */
5181 if (fan_status_access_mode != TPACPI_FAN_NONE)
5182 fan_get_status_safe(NULL);
5184 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5185 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5186 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5187 &fan_attr_group);
5188 if (!(rc < 0))
5189 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5190 &driver_attr_fan_watchdog);
5191 if (rc < 0)
5192 return rc;
5193 return 0;
5194 } else
5195 return 1;
5198 static void fan_exit(void)
5200 vdbg_printk(TPACPI_DBG_EXIT,
5201 "cancelling any pending fan watchdog tasks\n");
5203 /* FIXME: can we really do this unconditionally? */
5204 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5205 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5206 &driver_attr_fan_watchdog);
5208 cancel_delayed_work(&fan_watchdog_task);
5209 flush_scheduled_work();
5212 static int fan_read(char *p)
5214 int len = 0;
5215 int rc;
5216 u8 status;
5217 unsigned int speed = 0;
5219 switch (fan_status_access_mode) {
5220 case TPACPI_FAN_RD_ACPI_GFAN:
5221 /* 570, 600e/x, 770e, 770x */
5222 rc = fan_get_status_safe(&status);
5223 if (rc < 0)
5224 return rc;
5226 len += sprintf(p + len, "status:\t\t%s\n"
5227 "level:\t\t%d\n",
5228 (status != 0) ? "enabled" : "disabled", status);
5229 break;
5231 case TPACPI_FAN_RD_TPEC:
5232 /* all except 570, 600e/x, 770e, 770x */
5233 rc = fan_get_status_safe(&status);
5234 if (rc < 0)
5235 return rc;
5237 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5238 if (status != fan_control_initial_status)
5239 tp_features.fan_ctrl_status_undef = 0;
5240 else
5241 /* Return most likely status. In fact, it
5242 * might be the only possible status */
5243 status = TP_EC_FAN_AUTO;
5246 len += sprintf(p + len, "status:\t\t%s\n",
5247 (status != 0) ? "enabled" : "disabled");
5249 rc = fan_get_speed(&speed);
5250 if (rc < 0)
5251 return rc;
5253 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5255 if (status & TP_EC_FAN_FULLSPEED)
5256 /* Disengaged mode takes precedence */
5257 len += sprintf(p + len, "level:\t\tdisengaged\n");
5258 else if (status & TP_EC_FAN_AUTO)
5259 len += sprintf(p + len, "level:\t\tauto\n");
5260 else
5261 len += sprintf(p + len, "level:\t\t%d\n", status);
5262 break;
5264 case TPACPI_FAN_NONE:
5265 default:
5266 len += sprintf(p + len, "status:\t\tnot supported\n");
5269 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5270 len += sprintf(p + len, "commands:\tlevel <level>");
5272 switch (fan_control_access_mode) {
5273 case TPACPI_FAN_WR_ACPI_SFAN:
5274 len += sprintf(p + len, " (<level> is 0-7)\n");
5275 break;
5277 default:
5278 len += sprintf(p + len, " (<level> is 0-7, "
5279 "auto, disengaged, full-speed)\n");
5280 break;
5284 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5285 len += sprintf(p + len, "commands:\tenable, disable\n"
5286 "commands:\twatchdog <timeout> (<timeout> "
5287 "is 0 (off), 1-120 (seconds))\n");
5289 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5290 len += sprintf(p + len, "commands:\tspeed <speed>"
5291 " (<speed> is 0-65535)\n");
5293 return len;
5296 static int fan_write_cmd_level(const char *cmd, int *rc)
5298 int level;
5300 if (strlencmp(cmd, "level auto") == 0)
5301 level = TP_EC_FAN_AUTO;
5302 else if ((strlencmp(cmd, "level disengaged") == 0) |
5303 (strlencmp(cmd, "level full-speed") == 0))
5304 level = TP_EC_FAN_FULLSPEED;
5305 else if (sscanf(cmd, "level %d", &level) != 1)
5306 return 0;
5308 *rc = fan_set_level_safe(level);
5309 if (*rc == -ENXIO)
5310 printk(TPACPI_ERR "level command accepted for unsupported "
5311 "access mode %d", fan_control_access_mode);
5313 return 1;
5316 static int fan_write_cmd_enable(const char *cmd, int *rc)
5318 if (strlencmp(cmd, "enable") != 0)
5319 return 0;
5321 *rc = fan_set_enable();
5322 if (*rc == -ENXIO)
5323 printk(TPACPI_ERR "enable command accepted for unsupported "
5324 "access mode %d", fan_control_access_mode);
5326 return 1;
5329 static int fan_write_cmd_disable(const char *cmd, int *rc)
5331 if (strlencmp(cmd, "disable") != 0)
5332 return 0;
5334 *rc = fan_set_disable();
5335 if (*rc == -ENXIO)
5336 printk(TPACPI_ERR "disable command accepted for unsupported "
5337 "access mode %d", fan_control_access_mode);
5339 return 1;
5342 static int fan_write_cmd_speed(const char *cmd, int *rc)
5344 int speed;
5346 /* TODO:
5347 * Support speed <low> <medium> <high> ? */
5349 if (sscanf(cmd, "speed %d", &speed) != 1)
5350 return 0;
5352 *rc = fan_set_speed(speed);
5353 if (*rc == -ENXIO)
5354 printk(TPACPI_ERR "speed command accepted for unsupported "
5355 "access mode %d", fan_control_access_mode);
5357 return 1;
5360 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5362 int interval;
5364 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5365 return 0;
5367 if (interval < 0 || interval > 120)
5368 *rc = -EINVAL;
5369 else
5370 fan_watchdog_maxinterval = interval;
5372 return 1;
5375 static int fan_write(char *buf)
5377 char *cmd;
5378 int rc = 0;
5380 while (!rc && (cmd = next_cmd(&buf))) {
5381 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5382 fan_write_cmd_level(cmd, &rc)) &&
5383 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5384 (fan_write_cmd_enable(cmd, &rc) ||
5385 fan_write_cmd_disable(cmd, &rc) ||
5386 fan_write_cmd_watchdog(cmd, &rc))) &&
5387 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5388 fan_write_cmd_speed(cmd, &rc))
5390 rc = -EINVAL;
5391 else if (!rc)
5392 fan_watchdog_reset();
5395 return rc;
5398 static struct ibm_struct fan_driver_data = {
5399 .name = "fan",
5400 .read = fan_read,
5401 .write = fan_write,
5402 .exit = fan_exit,
5405 /****************************************************************************
5406 ****************************************************************************
5408 * Infrastructure
5410 ****************************************************************************
5411 ****************************************************************************/
5413 /* sysfs name ---------------------------------------------------------- */
5414 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5415 struct device_attribute *attr,
5416 char *buf)
5418 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5421 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5422 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5424 /* --------------------------------------------------------------------- */
5426 /* /proc support */
5427 static struct proc_dir_entry *proc_dir;
5430 * Module and infrastructure proble, init and exit handling
5433 static int force_load;
5435 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5436 static const char * __init str_supported(int is_supported)
5438 static char text_unsupported[] __initdata = "not supported";
5440 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5442 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5444 static void ibm_exit(struct ibm_struct *ibm)
5446 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5448 list_del_init(&ibm->all_drivers);
5450 if (ibm->flags.acpi_notify_installed) {
5451 dbg_printk(TPACPI_DBG_EXIT,
5452 "%s: acpi_remove_notify_handler\n", ibm->name);
5453 BUG_ON(!ibm->acpi);
5454 acpi_remove_notify_handler(*ibm->acpi->handle,
5455 ibm->acpi->type,
5456 dispatch_acpi_notify);
5457 ibm->flags.acpi_notify_installed = 0;
5458 ibm->flags.acpi_notify_installed = 0;
5461 if (ibm->flags.proc_created) {
5462 dbg_printk(TPACPI_DBG_EXIT,
5463 "%s: remove_proc_entry\n", ibm->name);
5464 remove_proc_entry(ibm->name, proc_dir);
5465 ibm->flags.proc_created = 0;
5468 if (ibm->flags.acpi_driver_registered) {
5469 dbg_printk(TPACPI_DBG_EXIT,
5470 "%s: acpi_bus_unregister_driver\n", ibm->name);
5471 BUG_ON(!ibm->acpi);
5472 acpi_bus_unregister_driver(ibm->acpi->driver);
5473 kfree(ibm->acpi->driver);
5474 ibm->acpi->driver = NULL;
5475 ibm->flags.acpi_driver_registered = 0;
5478 if (ibm->flags.init_called && ibm->exit) {
5479 ibm->exit();
5480 ibm->flags.init_called = 0;
5483 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5486 static int __init ibm_init(struct ibm_init_struct *iibm)
5488 int ret;
5489 struct ibm_struct *ibm = iibm->data;
5490 struct proc_dir_entry *entry;
5492 BUG_ON(ibm == NULL);
5494 INIT_LIST_HEAD(&ibm->all_drivers);
5496 if (ibm->flags.experimental && !experimental)
5497 return 0;
5499 dbg_printk(TPACPI_DBG_INIT,
5500 "probing for %s\n", ibm->name);
5502 if (iibm->init) {
5503 ret = iibm->init(iibm);
5504 if (ret > 0)
5505 return 0; /* probe failed */
5506 if (ret)
5507 return ret;
5509 ibm->flags.init_called = 1;
5512 if (ibm->acpi) {
5513 if (ibm->acpi->hid) {
5514 ret = register_tpacpi_subdriver(ibm);
5515 if (ret)
5516 goto err_out;
5519 if (ibm->acpi->notify) {
5520 ret = setup_acpi_notify(ibm);
5521 if (ret == -ENODEV) {
5522 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5523 ibm->name);
5524 ret = 0;
5525 goto err_out;
5527 if (ret < 0)
5528 goto err_out;
5532 dbg_printk(TPACPI_DBG_INIT,
5533 "%s installed\n", ibm->name);
5535 if (ibm->read) {
5536 entry = create_proc_entry(ibm->name,
5537 S_IFREG | S_IRUGO | S_IWUSR,
5538 proc_dir);
5539 if (!entry) {
5540 printk(TPACPI_ERR "unable to create proc entry %s\n",
5541 ibm->name);
5542 ret = -ENODEV;
5543 goto err_out;
5545 entry->owner = THIS_MODULE;
5546 entry->data = ibm;
5547 entry->read_proc = &dispatch_procfs_read;
5548 if (ibm->write)
5549 entry->write_proc = &dispatch_procfs_write;
5550 ibm->flags.proc_created = 1;
5553 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5555 return 0;
5557 err_out:
5558 dbg_printk(TPACPI_DBG_INIT,
5559 "%s: at error exit path with result %d\n",
5560 ibm->name, ret);
5562 ibm_exit(ibm);
5563 return (ret < 0)? ret : 0;
5566 /* Probing */
5568 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5570 struct dmi_device *dev = NULL;
5571 char ec_fw_string[18];
5573 if (!tp)
5574 return;
5576 memset(tp, 0, sizeof(*tp));
5578 if (dmi_name_in_vendors("IBM"))
5579 tp->vendor = PCI_VENDOR_ID_IBM;
5580 else if (dmi_name_in_vendors("LENOVO"))
5581 tp->vendor = PCI_VENDOR_ID_LENOVO;
5582 else
5583 return;
5585 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5586 GFP_KERNEL);
5587 if (!tp->bios_version_str)
5588 return;
5589 tp->bios_model = tp->bios_version_str[0]
5590 | (tp->bios_version_str[1] << 8);
5593 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5594 * X32 or newer, all Z series; Some models must have an
5595 * up-to-date BIOS or they will not be detected.
5597 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5599 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5600 if (sscanf(dev->name,
5601 "IBM ThinkPad Embedded Controller -[%17c",
5602 ec_fw_string) == 1) {
5603 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5604 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5606 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5607 tp->ec_model = ec_fw_string[0]
5608 | (ec_fw_string[1] << 8);
5609 break;
5613 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5614 GFP_KERNEL);
5615 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5616 kfree(tp->model_str);
5617 tp->model_str = NULL;
5621 static int __init probe_for_thinkpad(void)
5623 int is_thinkpad;
5625 if (acpi_disabled)
5626 return -ENODEV;
5629 * Non-ancient models have better DMI tagging, but very old models
5630 * don't.
5632 is_thinkpad = (thinkpad_id.model_str != NULL);
5634 /* ec is required because many other handles are relative to it */
5635 TPACPI_ACPIHANDLE_INIT(ec);
5636 if (!ec_handle) {
5637 if (is_thinkpad)
5638 printk(TPACPI_ERR
5639 "Not yet supported ThinkPad detected!\n");
5640 return -ENODEV;
5644 * Risks a regression on very old machines, but reduces potential
5645 * false positives a damn great deal
5647 if (!is_thinkpad)
5648 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5650 if (!is_thinkpad && !force_load)
5651 return -ENODEV;
5653 return 0;
5657 /* Module init, exit, parameters */
5659 static struct ibm_init_struct ibms_init[] __initdata = {
5661 .init = thinkpad_acpi_driver_init,
5662 .data = &thinkpad_acpi_driver_data,
5665 .init = hotkey_init,
5666 .data = &hotkey_driver_data,
5669 .init = bluetooth_init,
5670 .data = &bluetooth_driver_data,
5673 .init = wan_init,
5674 .data = &wan_driver_data,
5677 .init = video_init,
5678 .data = &video_driver_data,
5681 .init = light_init,
5682 .data = &light_driver_data,
5684 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5686 .init = dock_init,
5687 .data = &dock_driver_data[0],
5690 .init = dock_init2,
5691 .data = &dock_driver_data[1],
5693 #endif
5694 #ifdef CONFIG_THINKPAD_ACPI_BAY
5696 .init = bay_init,
5697 .data = &bay_driver_data,
5699 #endif
5701 .init = cmos_init,
5702 .data = &cmos_driver_data,
5705 .init = led_init,
5706 .data = &led_driver_data,
5709 .init = beep_init,
5710 .data = &beep_driver_data,
5713 .init = thermal_init,
5714 .data = &thermal_driver_data,
5717 .data = &ecdump_driver_data,
5720 .init = brightness_init,
5721 .data = &brightness_driver_data,
5724 .data = &volume_driver_data,
5727 .init = fan_init,
5728 .data = &fan_driver_data,
5732 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5734 unsigned int i;
5735 struct ibm_struct *ibm;
5737 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5738 ibm = ibms_init[i].data;
5739 BUG_ON(ibm == NULL);
5741 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5742 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5743 return -ENOSPC;
5744 strcpy(ibms_init[i].param, val);
5745 strcat(ibms_init[i].param, ",");
5746 return 0;
5750 return -EINVAL;
5753 module_param(experimental, int, 0);
5754 MODULE_PARM_DESC(experimental,
5755 "Enables experimental features when non-zero");
5757 module_param_named(debug, dbg_level, uint, 0);
5758 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5760 module_param(force_load, bool, 0);
5761 MODULE_PARM_DESC(force_load,
5762 "Attempts to load the driver even on a "
5763 "mis-identified ThinkPad when true");
5765 module_param_named(fan_control, fan_control_allowed, bool, 0);
5766 MODULE_PARM_DESC(fan_control,
5767 "Enables setting fan parameters features when true");
5769 module_param_named(brightness_mode, brightness_mode, int, 0);
5770 MODULE_PARM_DESC(brightness_mode,
5771 "Selects brightness control strategy: "
5772 "0=auto, 1=EC, 2=CMOS, 3=both");
5774 module_param(brightness_enable, uint, 0);
5775 MODULE_PARM_DESC(brightness_enable,
5776 "Enables backlight control when 1, disables when 0");
5778 module_param(hotkey_report_mode, uint, 0);
5779 MODULE_PARM_DESC(hotkey_report_mode,
5780 "used for backwards compatibility with userspace, "
5781 "see documentation");
5783 #define TPACPI_PARAM(feature) \
5784 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5785 MODULE_PARM_DESC(feature, "Simulates thinkpad-aci procfs command " \
5786 "at module load, see documentation")
5788 TPACPI_PARAM(hotkey);
5789 TPACPI_PARAM(bluetooth);
5790 TPACPI_PARAM(video);
5791 TPACPI_PARAM(light);
5792 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5793 TPACPI_PARAM(dock);
5794 #endif
5795 #ifdef CONFIG_THINKPAD_ACPI_BAY
5796 TPACPI_PARAM(bay);
5797 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5798 TPACPI_PARAM(cmos);
5799 TPACPI_PARAM(led);
5800 TPACPI_PARAM(beep);
5801 TPACPI_PARAM(ecdump);
5802 TPACPI_PARAM(brightness);
5803 TPACPI_PARAM(volume);
5804 TPACPI_PARAM(fan);
5806 static void thinkpad_acpi_module_exit(void)
5808 struct ibm_struct *ibm, *itmp;
5810 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5812 list_for_each_entry_safe_reverse(ibm, itmp,
5813 &tpacpi_all_drivers,
5814 all_drivers) {
5815 ibm_exit(ibm);
5818 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5820 if (tpacpi_inputdev) {
5821 if (tp_features.input_device_registered)
5822 input_unregister_device(tpacpi_inputdev);
5823 else
5824 input_free_device(tpacpi_inputdev);
5827 if (tpacpi_hwmon)
5828 hwmon_device_unregister(tpacpi_hwmon);
5830 if (tp_features.sensors_pdev_attrs_registered)
5831 device_remove_file(&tpacpi_sensors_pdev->dev,
5832 &dev_attr_thinkpad_acpi_pdev_name);
5833 if (tpacpi_sensors_pdev)
5834 platform_device_unregister(tpacpi_sensors_pdev);
5835 if (tpacpi_pdev)
5836 platform_device_unregister(tpacpi_pdev);
5838 if (tp_features.sensors_pdrv_attrs_registered)
5839 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5840 if (tp_features.platform_drv_attrs_registered)
5841 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5843 if (tp_features.sensors_pdrv_registered)
5844 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5846 if (tp_features.platform_drv_registered)
5847 platform_driver_unregister(&tpacpi_pdriver);
5849 if (proc_dir)
5850 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5852 kfree(thinkpad_id.bios_version_str);
5853 kfree(thinkpad_id.ec_version_str);
5854 kfree(thinkpad_id.model_str);
5858 static int __init thinkpad_acpi_module_init(void)
5860 int ret, i;
5862 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5864 /* Parameter checking */
5865 if (hotkey_report_mode > 2)
5866 return -EINVAL;
5868 /* Driver-level probe */
5870 get_thinkpad_model_data(&thinkpad_id);
5871 ret = probe_for_thinkpad();
5872 if (ret) {
5873 thinkpad_acpi_module_exit();
5874 return ret;
5877 /* Driver initialization */
5879 TPACPI_ACPIHANDLE_INIT(ecrd);
5880 TPACPI_ACPIHANDLE_INIT(ecwr);
5882 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
5883 if (!proc_dir) {
5884 printk(TPACPI_ERR
5885 "unable to create proc dir " TPACPI_PROC_DIR);
5886 thinkpad_acpi_module_exit();
5887 return -ENODEV;
5889 proc_dir->owner = THIS_MODULE;
5891 ret = platform_driver_register(&tpacpi_pdriver);
5892 if (ret) {
5893 printk(TPACPI_ERR
5894 "unable to register main platform driver\n");
5895 thinkpad_acpi_module_exit();
5896 return ret;
5898 tp_features.platform_drv_registered = 1;
5900 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5901 if (ret) {
5902 printk(TPACPI_ERR
5903 "unable to register hwmon platform driver\n");
5904 thinkpad_acpi_module_exit();
5905 return ret;
5907 tp_features.sensors_pdrv_registered = 1;
5909 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5910 if (!ret) {
5911 tp_features.platform_drv_attrs_registered = 1;
5912 ret = tpacpi_create_driver_attributes(
5913 &tpacpi_hwmon_pdriver.driver);
5915 if (ret) {
5916 printk(TPACPI_ERR
5917 "unable to create sysfs driver attributes\n");
5918 thinkpad_acpi_module_exit();
5919 return ret;
5921 tp_features.sensors_pdrv_attrs_registered = 1;
5924 /* Device initialization */
5925 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
5926 NULL, 0);
5927 if (IS_ERR(tpacpi_pdev)) {
5928 ret = PTR_ERR(tpacpi_pdev);
5929 tpacpi_pdev = NULL;
5930 printk(TPACPI_ERR "unable to register platform device\n");
5931 thinkpad_acpi_module_exit();
5932 return ret;
5934 tpacpi_sensors_pdev = platform_device_register_simple(
5935 TPACPI_HWMON_DRVR_NAME,
5936 -1, NULL, 0);
5937 if (IS_ERR(tpacpi_sensors_pdev)) {
5938 ret = PTR_ERR(tpacpi_sensors_pdev);
5939 tpacpi_sensors_pdev = NULL;
5940 printk(TPACPI_ERR
5941 "unable to register hwmon platform device\n");
5942 thinkpad_acpi_module_exit();
5943 return ret;
5945 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5946 &dev_attr_thinkpad_acpi_pdev_name);
5947 if (ret) {
5948 printk(TPACPI_ERR
5949 "unable to create sysfs hwmon device attributes\n");
5950 thinkpad_acpi_module_exit();
5951 return ret;
5953 tp_features.sensors_pdev_attrs_registered = 1;
5954 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5955 if (IS_ERR(tpacpi_hwmon)) {
5956 ret = PTR_ERR(tpacpi_hwmon);
5957 tpacpi_hwmon = NULL;
5958 printk(TPACPI_ERR "unable to register hwmon device\n");
5959 thinkpad_acpi_module_exit();
5960 return ret;
5962 mutex_init(&tpacpi_inputdev_send_mutex);
5963 tpacpi_inputdev = input_allocate_device();
5964 if (!tpacpi_inputdev) {
5965 printk(TPACPI_ERR "unable to allocate input device\n");
5966 thinkpad_acpi_module_exit();
5967 return -ENOMEM;
5968 } else {
5969 /* Prepare input device, but don't register */
5970 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5971 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
5972 tpacpi_inputdev->id.bustype = BUS_HOST;
5973 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5974 thinkpad_id.vendor :
5975 PCI_VENDOR_ID_IBM;
5976 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5977 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5979 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5980 ret = ibm_init(&ibms_init[i]);
5981 if (ret >= 0 && *ibms_init[i].param)
5982 ret = ibms_init[i].data->write(ibms_init[i].param);
5983 if (ret < 0) {
5984 thinkpad_acpi_module_exit();
5985 return ret;
5988 ret = input_register_device(tpacpi_inputdev);
5989 if (ret < 0) {
5990 printk(TPACPI_ERR "unable to register input device\n");
5991 thinkpad_acpi_module_exit();
5992 return ret;
5993 } else {
5994 tp_features.input_device_registered = 1;
5997 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5998 return 0;
6001 /* Please remove this in year 2009 */
6002 MODULE_ALIAS("ibm_acpi");
6005 * DMI matching for module autoloading
6007 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6008 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6010 * Only models listed in thinkwiki will be supported, so add yours
6011 * if it is not there yet.
6013 #define IBM_BIOS_MODULE_ALIAS(__type) \
6014 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6016 /* Non-ancient thinkpads */
6017 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6018 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6020 /* Ancient thinkpad BIOSes have to be identified by
6021 * BIOS type or model number, and there are far less
6022 * BIOS types than model numbers... */
6023 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6024 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6025 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6027 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6028 MODULE_DESCRIPTION(TPACPI_DESC);
6029 MODULE_VERSION(TPACPI_VERSION);
6030 MODULE_LICENSE("GPL");
6032 module_init(thinkpad_acpi_module_init);
6033 module_exit(thinkpad_acpi_module_exit);